Merge branch 'core-objtool-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.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 #define VIMC_CID_MEAN_WIN_SIZE          (VIMC_CID_VIMC_BASE + 1)
23
24 #define VIMC_FRAME_MAX_WIDTH 4096
25 #define VIMC_FRAME_MAX_HEIGHT 2160
26 #define VIMC_FRAME_MIN_WIDTH 16
27 #define VIMC_FRAME_MIN_HEIGHT 16
28
29 #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
30
31 /* Source and sink pad checks */
32 #define VIMC_IS_SRC(pad)        (pad)
33 #define VIMC_IS_SINK(pad)       (!(pad))
34
35 /**
36  * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
37  *
38  * @fmt:                the pointer to struct v4l2_pix_format or
39  *                      struct v4l2_mbus_framefmt
40  *
41  * Entities must check if colorimetry given by the userspace is valid, if not
42  * then set them as DEFAULT
43  */
44 #define vimc_colorimetry_clamp(fmt)                                     \
45 do {                                                                    \
46         if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT                \
47             || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) {            \
48                 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT;            \
49                 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;              \
50                 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;        \
51                 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;              \
52         }                                                               \
53         if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M)                \
54                 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;              \
55         if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE)          \
56                 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;        \
57         if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084)                \
58                 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;              \
59 } while (0)
60
61 /**
62  * struct vimc_pix_map - maps media bus code with v4l2 pixel format
63  *
64  * @code:               media bus format code defined by MEDIA_BUS_FMT_* macros
65  * @bbp:                number of bytes each pixel occupies
66  * @pixelformat:        pixel format devined by V4L2_PIX_FMT_* macros
67  *
68  * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
69  * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
70  */
71 struct vimc_pix_map {
72         unsigned int code;
73         unsigned int bpp;
74         u32 pixelformat;
75         bool bayer;
76 };
77
78 /**
79  * struct vimc_ent_device - core struct that represents an entity in the
80  * topology
81  *
82  * @dev:                a pointer of the device struct of the driver
83  * @ent:                the pointer to struct media_entity for the node
84  * @process_frame:      callback send a frame to that node
85  * @vdev_get_format:    callback that returns the current format a pad, used
86  *                      only when is_media_entity_v4l2_video_device(ent) returns
87  *                      true
88  *
89  * Each node of the topology must create a vimc_ent_device struct. Depending on
90  * the node it will be of an instance of v4l2_subdev or video_device struct
91  * where both contains a struct media_entity.
92  * Those structures should embedded the vimc_ent_device struct through
93  * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
94  * vimc_ent_device struct to be retrieved from the corresponding struct
95  * media_entity
96  */
97 struct vimc_ent_device {
98         struct device *dev;
99         struct media_entity *ent;
100         void * (*process_frame)(struct vimc_ent_device *ved,
101                                 const void *frame);
102         void (*vdev_get_format)(struct vimc_ent_device *ved,
103                               struct v4l2_pix_format *fmt);
104 };
105
106 /**
107  * struct vimc_device - main device for vimc driver
108  *
109  * @pipe_cfg    pointer to the vimc pipeline configuration structure
110  * @ent_devs    array of vimc_ent_device pointers
111  * @mdev        the associated media_device parent
112  * @v4l2_dev    Internal v4l2 parent device
113  */
114 struct vimc_device {
115         const struct vimc_pipeline_config *pipe_cfg;
116         struct vimc_ent_device **ent_devs;
117         struct media_device mdev;
118         struct v4l2_device v4l2_dev;
119 };
120
121 /**
122  * struct vimc_ent_config       Structure which describes individual
123  *                              configuration for each entity
124  *
125  * @name                        entity name
126  * @ved                         pointer to vimc_ent_device (a node in the
127  *                                      topology)
128  * @add                         initializes and registers
129  *                                      vim entity - called from vimc-core
130  * @unregister                  unregisters vimc entity - called from vimc-core
131  * @release                     releases vimc entity - called from the v4l2_dev
132  *                                      release callback
133  */
134 struct vimc_ent_config {
135         const char *name;
136         struct vimc_ent_device *(*add)(struct vimc_device *vimc,
137                                        const char *vcfg_name);
138         void (*unregister)(struct vimc_ent_device *ved);
139         void (*release)(struct vimc_ent_device *ved);
140 };
141
142 /**
143  * vimc_is_source - returns true if the entity has only source pads
144  *
145  * @ent: pointer to &struct media_entity
146  *
147  */
148 bool vimc_is_source(struct media_entity *ent);
149
150 /* prototypes for vimc_ent_config hooks */
151 struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
152                                      const char *vcfg_name);
153 void vimc_cap_unregister(struct vimc_ent_device *ved);
154 void vimc_cap_release(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_release(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_release(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_release(struct vimc_ent_device *ved);
167
168 /**
169  * vimc_pix_map_by_index - get vimc_pix_map struct by its index
170  *
171  * @i:                  index of the vimc_pix_map struct in vimc_pix_map_list
172  */
173 const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
174
175 /**
176  * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
177  *
178  * @code:               media bus format code defined by MEDIA_BUS_FMT_* macros
179  */
180 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
181
182 /**
183  * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
184  *
185  * @pixelformat:        pixel format devined by V4L2_PIX_FMT_* macros
186  */
187 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
188
189 /**
190  * vimc_ent_sd_register - initialize and register a subdev node
191  *
192  * @ved:        the vimc_ent_device struct to be initialize
193  * @sd:         the v4l2_subdev struct to be initialize and registered
194  * @v4l2_dev:   the v4l2 device to register the v4l2_subdev
195  * @name:       name of the sub-device. Please notice that the name must be
196  *              unique.
197  * @function:   media entity function defined by MEDIA_ENT_F_* macros
198  * @num_pads:   number of pads to initialize
199  * @pads:       the array of pads of the entity, the caller should set the
200                 flags of the pads
201  * @sd_ops:     pointer to &struct v4l2_subdev_ops.
202  *
203  * Helper function initialize and register the struct vimc_ent_device and struct
204  * v4l2_subdev which represents a subdev node in the topology
205  */
206 int vimc_ent_sd_register(struct vimc_ent_device *ved,
207                          struct v4l2_subdev *sd,
208                          struct v4l2_device *v4l2_dev,
209                          const char *const name,
210                          u32 function,
211                          u16 num_pads,
212                          struct media_pad *pads,
213                          const struct v4l2_subdev_ops *sd_ops);
214
215 /**
216  * vimc_vdev_link_validate - validates a media link
217  *
218  * @link: pointer to &struct media_link
219  *
220  * This function calls validates if a media link is valid for streaming.
221  */
222 int vimc_vdev_link_validate(struct media_link *link);
223
224 #endif