GNU Linux-libre 4.14.295-gnu1
[releases.git] / drivers / media / platform / vimc / vimc-common.h
1 /*
2  * vimc-common.h Virtual Media Controller Driver
3  *
4  * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 #ifndef _VIMC_COMMON_H_
19 #define _VIMC_COMMON_H_
20
21 #include <linux/slab.h>
22 #include <media/media-device.h>
23 #include <media/v4l2-device.h>
24
25 #define VIMC_FRAME_MAX_WIDTH 4096
26 #define VIMC_FRAME_MAX_HEIGHT 2160
27 #define VIMC_FRAME_MIN_WIDTH 16
28 #define VIMC_FRAME_MIN_HEIGHT 16
29
30 #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
31
32 /**
33  * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
34  *
35  * @fmt:                the pointer to struct v4l2_pix_format or
36  *                      struct v4l2_mbus_framefmt
37  *
38  * Entities must check if colorimetry given by the userspace is valid, if not
39  * then set them as DEFAULT
40  */
41 #define vimc_colorimetry_clamp(fmt)                                     \
42 do {                                                                    \
43         if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT                \
44             || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) {            \
45                 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT;            \
46                 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;              \
47                 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;        \
48                 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;              \
49         }                                                               \
50         if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M)                \
51                 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;              \
52         if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE)          \
53                 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;        \
54         if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084)                \
55                 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;              \
56 } while (0)
57
58 /**
59  * struct vimc_platform_data - platform data to components
60  *
61  * @entity_name:        The name of the entity to be created
62  *
63  * Board setup code will often provide additional information using the device's
64  * platform_data field to hold additional information.
65  * When injecting a new platform_device in the component system the core needs
66  * to provide to the corresponding submodules the name of the entity that should
67  * be used when registering the subdevice in the Media Controller system.
68  */
69 struct vimc_platform_data {
70         char entity_name[32];
71 };
72
73 /**
74  * struct vimc_pix_map - maps media bus code with v4l2 pixel format
75  *
76  * @code:               media bus format code defined by MEDIA_BUS_FMT_* macros
77  * @bbp:                number of bytes each pixel occupies
78  * @pixelformat:        pixel format devined by V4L2_PIX_FMT_* macros
79  *
80  * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
81  * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
82  */
83 struct vimc_pix_map {
84         unsigned int code;
85         unsigned int bpp;
86         u32 pixelformat;
87         bool bayer;
88 };
89
90 /**
91  * struct vimc_ent_device - core struct that represents a node in the topology
92  *
93  * @ent:                the pointer to struct media_entity for the node
94  * @pads:               the list of pads of the node
95  * @process_frame:      callback send a frame to that node
96  * @vdev_get_format:    callback that returns the current format a pad, used
97  *                      only when is_media_entity_v4l2_video_device(ent) returns
98  *                      true
99  *
100  * Each node of the topology must create a vimc_ent_device struct. Depending on
101  * the node it will be of an instance of v4l2_subdev or video_device struct
102  * where both contains a struct media_entity.
103  * Those structures should embedded the vimc_ent_device struct through
104  * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
105  * vimc_ent_device struct to be retrieved from the corresponding struct
106  * media_entity
107  */
108 struct vimc_ent_device {
109         struct media_entity *ent;
110         struct media_pad *pads;
111         void * (*process_frame)(struct vimc_ent_device *ved,
112                                 const void *frame);
113         void (*vdev_get_format)(struct vimc_ent_device *ved,
114                               struct v4l2_pix_format *fmt);
115 };
116
117 /**
118  * vimc_pads_init - initialize pads
119  *
120  * @num_pads:   number of pads to initialize
121  * @pads_flags: flags to use in each pad
122  *
123  * Helper functions to allocate/initialize pads
124  */
125 struct media_pad *vimc_pads_init(u16 num_pads,
126                                  const unsigned long *pads_flag);
127
128 /**
129  * vimc_pads_cleanup - free pads
130  *
131  * @pads: pointer to the pads
132  *
133  * Helper function to free the pads initialized with vimc_pads_init
134  */
135 static inline void vimc_pads_cleanup(struct media_pad *pads)
136 {
137         kfree(pads);
138 }
139
140 /**
141  * vimc_pipeline_s_stream - start stream through the pipeline
142  *
143  * @ent:                the pointer to struct media_entity for the node
144  * @enable:             1 to start the stream and 0 to stop
145  *
146  * Helper function to call the s_stream of the subdevices connected
147  * in all the sink pads of the entity
148  */
149 int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
150
151 /**
152  * vimc_pix_map_by_index - get vimc_pix_map struct by its index
153  *
154  * @i:                  index of the vimc_pix_map struct in vimc_pix_map_list
155  */
156 const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
157
158 /**
159  * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
160  *
161  * @code:               media bus format code defined by MEDIA_BUS_FMT_* macros
162  */
163 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
164
165 /**
166  * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
167  *
168  * @pixelformat:        pixel format devined by V4L2_PIX_FMT_* macros
169  */
170 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
171
172 /**
173  * vimc_ent_sd_register - initialize and register a subdev node
174  *
175  * @ved:        the vimc_ent_device struct to be initialize
176  * @sd:         the v4l2_subdev struct to be initialize and registered
177  * @v4l2_dev:   the v4l2 device to register the v4l2_subdev
178  * @name:       name of the sub-device. Please notice that the name must be
179  *              unique.
180  * @function:   media entity function defined by MEDIA_ENT_F_* macros
181  * @num_pads:   number of pads to initialize
182  * @pads_flag:  flags to use in each pad
183  * @sd_ops:     pointer to &struct v4l2_subdev_ops.
184  *
185  * Helper function initialize and register the struct vimc_ent_device and struct
186  * v4l2_subdev which represents a subdev node in the topology
187  */
188 int vimc_ent_sd_register(struct vimc_ent_device *ved,
189                          struct v4l2_subdev *sd,
190                          struct v4l2_device *v4l2_dev,
191                          const char *const name,
192                          u32 function,
193                          u16 num_pads,
194                          const unsigned long *pads_flag,
195                          const struct v4l2_subdev_ops *sd_ops);
196
197 /**
198  * vimc_ent_sd_unregister - cleanup and unregister a subdev node
199  *
200  * @ved:        the vimc_ent_device struct to be cleaned up
201  * @sd:         the v4l2_subdev struct to be unregistered
202  *
203  * Helper function cleanup and unregister the struct vimc_ent_device and struct
204  * v4l2_subdev which represents a subdev node in the topology
205  */
206 void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
207                             struct v4l2_subdev *sd);
208
209 /**
210  * vimc_link_validate - validates a media link
211  *
212  * @link: pointer to &struct media_link
213  *
214  * This function calls validates if a media link is valid for streaming.
215  */
216 int vimc_link_validate(struct media_link *link);
217
218 #endif