]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/platform/vimc/vimc-common.h
media: vimc: Collapse component structure into a single monolithic driver
[linux.git] / drivers / media / platform / vimc / vimc-common.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * vimc-common.h Virtual Media Controller Driver
4  *
5  * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
6  */
7
8 #ifndef _VIMC_COMMON_H_
9 #define _VIMC_COMMON_H_
10
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <media/media-device.h>
14 #include <media/v4l2-device.h>
15
16 #define VIMC_PDEV_NAME "vimc"
17
18 /* VIMC-specific controls */
19 #define VIMC_CID_VIMC_BASE              (0x00f00000 | 0xf000)
20 #define VIMC_CID_VIMC_CLASS             (0x00f00000 | 1)
21 #define VIMC_CID_TEST_PATTERN           (VIMC_CID_VIMC_BASE + 0)
22
23 #define VIMC_FRAME_MAX_WIDTH 4096
24 #define VIMC_FRAME_MAX_HEIGHT 2160
25 #define VIMC_FRAME_MIN_WIDTH 16
26 #define VIMC_FRAME_MIN_HEIGHT 16
27
28 #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
29
30 /**
31  * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
32  *
33  * @fmt:                the pointer to struct v4l2_pix_format or
34  *                      struct v4l2_mbus_framefmt
35  *
36  * Entities must check if colorimetry given by the userspace is valid, if not
37  * then set them as DEFAULT
38  */
39 #define vimc_colorimetry_clamp(fmt)                                     \
40 do {                                                                    \
41         if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT                \
42             || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) {            \
43                 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT;            \
44                 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;              \
45                 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;        \
46                 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;              \
47         }                                                               \
48         if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M)                \
49                 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;              \
50         if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE)          \
51                 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;        \
52         if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084)                \
53                 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;              \
54 } while (0)
55
56 /**
57  * struct vimc_platform_data - platform data to components
58  *
59  * @entity_name:        The name of the entity to be created
60  *
61  * Board setup code will often provide additional information using the device's
62  * platform_data field to hold additional information.
63  * When injecting a new platform_device in the component system the core needs
64  * to provide to the corresponding submodules the name of the entity that should
65  * be used when registering the subdevice in the Media Controller system.
66  */
67 struct vimc_platform_data {
68         char entity_name[32];
69 };
70
71 /**
72  * struct vimc_pix_map - maps media bus code with v4l2 pixel format
73  *
74  * @code:               media bus format code defined by MEDIA_BUS_FMT_* macros
75  * @bbp:                number of bytes each pixel occupies
76  * @pixelformat:        pixel format devined by V4L2_PIX_FMT_* macros
77  *
78  * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
79  * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
80  */
81 struct vimc_pix_map {
82         unsigned int code;
83         unsigned int bpp;
84         u32 pixelformat;
85         bool bayer;
86 };
87
88 /**
89  * struct vimc_ent_device - core struct that represents a node in the topology
90  *
91  * @ent:                the pointer to struct media_entity for the node
92  * @pads:               the list of pads of the node
93  * @process_frame:      callback send a frame to that node
94  * @vdev_get_format:    callback that returns the current format a pad, used
95  *                      only when is_media_entity_v4l2_video_device(ent) returns
96  *                      true
97  *
98  * Each node of the topology must create a vimc_ent_device struct. Depending on
99  * the node it will be of an instance of v4l2_subdev or video_device struct
100  * where both contains a struct media_entity.
101  * Those structures should embedded the vimc_ent_device struct through
102  * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
103  * vimc_ent_device struct to be retrieved from the corresponding struct
104  * media_entity
105  */
106 struct vimc_ent_device {
107         struct media_entity *ent;
108         struct media_pad *pads;
109         void * (*process_frame)(struct vimc_ent_device *ved,
110                                 const void *frame);
111         void (*vdev_get_format)(struct vimc_ent_device *ved,
112                               struct v4l2_pix_format *fmt);
113 };
114
115 /**
116  * struct vimc_device - main device for vimc driver
117  *
118  * @pdev        pointer to the platform device
119  * @pipe_cfg    pointer to the vimc pipeline configuration structure
120  * @ent_devs    array of vimc_ent_device pointers
121  * @mdev        the associated media_device parent
122  * @v4l2_dev    Internal v4l2 parent device
123  */
124 struct vimc_device {
125         struct platform_device pdev;
126         const struct vimc_pipeline_config *pipe_cfg;
127         struct vimc_ent_device **ent_devs;
128         struct media_device mdev;
129         struct v4l2_device v4l2_dev;
130 };
131
132 /**
133  * struct vimc_ent_config       Structure which describes individual
134  *                              configuration for each entity
135  *
136  * @name                        entity name
137  * @ved                         pointer to vimc_ent_device (a node in the
138  *                                      topology)
139  * @add                         subdev add hook - initializes and registers
140  *                                      subdev called from vimc-core
141  * @rm                          subdev rm hook - unregisters and frees
142  *                                      subdev called from vimc-core
143  */
144 struct vimc_ent_config {
145         const char *name;
146         struct vimc_ent_device *(*add)(struct vimc_device *vimc,
147                                        const char *vcfg_name);
148         void (*rm)(struct vimc_device *vimc, struct vimc_ent_device *ved);
149 };
150
151 /* prototypes for vimc_ent_config add and rm hooks */
152 struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
153                                      const char *vcfg_name);
154 void vimc_cap_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
155
156 struct vimc_ent_device *vimc_deb_add(struct vimc_device *vimc,
157                                      const char *vcfg_name);
158 void vimc_deb_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
159
160 struct vimc_ent_device *vimc_sca_add(struct vimc_device *vimc,
161                                      const char *vcfg_name);
162 void vimc_sca_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
163
164 struct vimc_ent_device *vimc_sen_add(struct vimc_device *vimc,
165                                      const char *vcfg_name);
166 void vimc_sen_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
167
168 /**
169  * vimc_pads_init - initialize pads
170  *
171  * @num_pads:   number of pads to initialize
172  * @pads_flags: flags to use in each pad
173  *
174  * Helper functions to allocate/initialize pads
175  */
176 struct media_pad *vimc_pads_init(u16 num_pads,
177                                  const unsigned long *pads_flag);
178
179 /**
180  * vimc_pads_cleanup - free pads
181  *
182  * @pads: pointer to the pads
183  *
184  * Helper function to free the pads initialized with vimc_pads_init
185  */
186 static inline void vimc_pads_cleanup(struct media_pad *pads)
187 {
188         kfree(pads);
189 }
190
191 /**
192  * vimc_pipeline_s_stream - start stream through the pipeline
193  *
194  * @ent:                the pointer to struct media_entity for the node
195  * @enable:             1 to start the stream and 0 to stop
196  *
197  * Helper function to call the s_stream of the subdevices connected
198  * in all the sink pads of the entity
199  */
200 int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
201
202 /**
203  * vimc_pix_map_by_index - get vimc_pix_map struct by its index
204  *
205  * @i:                  index of the vimc_pix_map struct in vimc_pix_map_list
206  */
207 const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
208
209 /**
210  * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
211  *
212  * @code:               media bus format code defined by MEDIA_BUS_FMT_* macros
213  */
214 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
215
216 /**
217  * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
218  *
219  * @pixelformat:        pixel format devined by V4L2_PIX_FMT_* macros
220  */
221 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
222
223 /**
224  * vimc_ent_sd_register - initialize and register a subdev node
225  *
226  * @ved:        the vimc_ent_device struct to be initialize
227  * @sd:         the v4l2_subdev struct to be initialize and registered
228  * @v4l2_dev:   the v4l2 device to register the v4l2_subdev
229  * @name:       name of the sub-device. Please notice that the name must be
230  *              unique.
231  * @function:   media entity function defined by MEDIA_ENT_F_* macros
232  * @num_pads:   number of pads to initialize
233  * @pads_flag:  flags to use in each pad
234  * @sd_int_ops: pointer to &struct v4l2_subdev_internal_ops
235  * @sd_ops:     pointer to &struct v4l2_subdev_ops.
236  *
237  * Helper function initialize and register the struct vimc_ent_device and struct
238  * v4l2_subdev which represents a subdev node in the topology
239  */
240 int vimc_ent_sd_register(struct vimc_ent_device *ved,
241                          struct v4l2_subdev *sd,
242                          struct v4l2_device *v4l2_dev,
243                          const char *const name,
244                          u32 function,
245                          u16 num_pads,
246                          const unsigned long *pads_flag,
247                          const struct v4l2_subdev_internal_ops *sd_int_ops,
248                          const struct v4l2_subdev_ops *sd_ops);
249
250 /**
251  * vimc_ent_sd_unregister - cleanup and unregister a subdev node
252  *
253  * @ved:        the vimc_ent_device struct to be cleaned up
254  * @sd:         the v4l2_subdev struct to be unregistered
255  *
256  * Helper function cleanup and unregister the struct vimc_ent_device and struct
257  * v4l2_subdev which represents a subdev node in the topology
258  */
259 void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
260                             struct v4l2_subdev *sd);
261
262 /**
263  * vimc_link_validate - validates a media link
264  *
265  * @link: pointer to &struct media_link
266  *
267  * This function calls validates if a media link is valid for streaming.
268  */
269 int vimc_link_validate(struct media_link *link);
270
271 #endif