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