]> Git Repo - linux.git/blob - drivers/media/platform/vimc/vimc-common.c
ARM: dts: imx7s: Enable SNVS power key according to board design
[linux.git] / drivers / media / platform / vimc / vimc-common.c
1 /*
2  * vimc-common.c Virtual Media Controller Driver
3  *
4  * Copyright (C) 2015-2017 Helen Koike <[email protected]>
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 #include <linux/init.h>
19 #include <linux/module.h>
20
21 #include "vimc-common.h"
22
23 static const __u32 vimc_mbus_list[] = {
24         MEDIA_BUS_FMT_FIXED,
25         MEDIA_BUS_FMT_RGB444_1X12,
26         MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE,
27         MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
28         MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
29         MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
30         MEDIA_BUS_FMT_RGB565_1X16,
31         MEDIA_BUS_FMT_BGR565_2X8_BE,
32         MEDIA_BUS_FMT_BGR565_2X8_LE,
33         MEDIA_BUS_FMT_RGB565_2X8_BE,
34         MEDIA_BUS_FMT_RGB565_2X8_LE,
35         MEDIA_BUS_FMT_RGB666_1X18,
36         MEDIA_BUS_FMT_RBG888_1X24,
37         MEDIA_BUS_FMT_RGB666_1X24_CPADHI,
38         MEDIA_BUS_FMT_RGB666_1X7X3_SPWG,
39         MEDIA_BUS_FMT_BGR888_1X24,
40         MEDIA_BUS_FMT_GBR888_1X24,
41         MEDIA_BUS_FMT_RGB888_1X24,
42         MEDIA_BUS_FMT_RGB888_2X12_BE,
43         MEDIA_BUS_FMT_RGB888_2X12_LE,
44         MEDIA_BUS_FMT_RGB888_1X7X4_SPWG,
45         MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA,
46         MEDIA_BUS_FMT_ARGB8888_1X32,
47         MEDIA_BUS_FMT_RGB888_1X32_PADHI,
48         MEDIA_BUS_FMT_RGB101010_1X30,
49         MEDIA_BUS_FMT_RGB121212_1X36,
50         MEDIA_BUS_FMT_RGB161616_1X48,
51         MEDIA_BUS_FMT_Y8_1X8,
52         MEDIA_BUS_FMT_UV8_1X8,
53         MEDIA_BUS_FMT_UYVY8_1_5X8,
54         MEDIA_BUS_FMT_VYUY8_1_5X8,
55         MEDIA_BUS_FMT_YUYV8_1_5X8,
56         MEDIA_BUS_FMT_YVYU8_1_5X8,
57         MEDIA_BUS_FMT_UYVY8_2X8,
58         MEDIA_BUS_FMT_VYUY8_2X8,
59         MEDIA_BUS_FMT_YUYV8_2X8,
60         MEDIA_BUS_FMT_YVYU8_2X8,
61         MEDIA_BUS_FMT_Y10_1X10,
62         MEDIA_BUS_FMT_Y10_2X8_PADHI_LE,
63         MEDIA_BUS_FMT_UYVY10_2X10,
64         MEDIA_BUS_FMT_VYUY10_2X10,
65         MEDIA_BUS_FMT_YUYV10_2X10,
66         MEDIA_BUS_FMT_YVYU10_2X10,
67         MEDIA_BUS_FMT_Y12_1X12,
68         MEDIA_BUS_FMT_UYVY12_2X12,
69         MEDIA_BUS_FMT_VYUY12_2X12,
70         MEDIA_BUS_FMT_YUYV12_2X12,
71         MEDIA_BUS_FMT_YVYU12_2X12,
72         MEDIA_BUS_FMT_UYVY8_1X16,
73         MEDIA_BUS_FMT_VYUY8_1X16,
74         MEDIA_BUS_FMT_YUYV8_1X16,
75         MEDIA_BUS_FMT_YVYU8_1X16,
76         MEDIA_BUS_FMT_YDYUYDYV8_1X16,
77         MEDIA_BUS_FMT_UYVY10_1X20,
78         MEDIA_BUS_FMT_VYUY10_1X20,
79         MEDIA_BUS_FMT_YUYV10_1X20,
80         MEDIA_BUS_FMT_YVYU10_1X20,
81         MEDIA_BUS_FMT_VUY8_1X24,
82         MEDIA_BUS_FMT_YUV8_1X24,
83         MEDIA_BUS_FMT_UYYVYY8_0_5X24,
84         MEDIA_BUS_FMT_UYVY12_1X24,
85         MEDIA_BUS_FMT_VYUY12_1X24,
86         MEDIA_BUS_FMT_YUYV12_1X24,
87         MEDIA_BUS_FMT_YVYU12_1X24,
88         MEDIA_BUS_FMT_YUV10_1X30,
89         MEDIA_BUS_FMT_UYYVYY10_0_5X30,
90         MEDIA_BUS_FMT_AYUV8_1X32,
91         MEDIA_BUS_FMT_UYYVYY12_0_5X36,
92         MEDIA_BUS_FMT_YUV12_1X36,
93         MEDIA_BUS_FMT_YUV16_1X48,
94         MEDIA_BUS_FMT_UYYVYY16_0_5X48,
95         MEDIA_BUS_FMT_SBGGR8_1X8,
96         MEDIA_BUS_FMT_SGBRG8_1X8,
97         MEDIA_BUS_FMT_SGRBG8_1X8,
98         MEDIA_BUS_FMT_SRGGB8_1X8,
99         MEDIA_BUS_FMT_SBGGR10_ALAW8_1X8,
100         MEDIA_BUS_FMT_SGBRG10_ALAW8_1X8,
101         MEDIA_BUS_FMT_SGRBG10_ALAW8_1X8,
102         MEDIA_BUS_FMT_SRGGB10_ALAW8_1X8,
103         MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
104         MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
105         MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
106         MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
107         MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE,
108         MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE,
109         MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE,
110         MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE,
111         MEDIA_BUS_FMT_SBGGR10_1X10,
112         MEDIA_BUS_FMT_SGBRG10_1X10,
113         MEDIA_BUS_FMT_SGRBG10_1X10,
114         MEDIA_BUS_FMT_SRGGB10_1X10,
115         MEDIA_BUS_FMT_SBGGR12_1X12,
116         MEDIA_BUS_FMT_SGBRG12_1X12,
117         MEDIA_BUS_FMT_SGRBG12_1X12,
118         MEDIA_BUS_FMT_SRGGB12_1X12,
119         MEDIA_BUS_FMT_SBGGR14_1X14,
120         MEDIA_BUS_FMT_SGBRG14_1X14,
121         MEDIA_BUS_FMT_SGRBG14_1X14,
122         MEDIA_BUS_FMT_SRGGB14_1X14,
123         MEDIA_BUS_FMT_SBGGR16_1X16,
124         MEDIA_BUS_FMT_SGBRG16_1X16,
125         MEDIA_BUS_FMT_SGRBG16_1X16,
126         MEDIA_BUS_FMT_SRGGB16_1X16,
127         MEDIA_BUS_FMT_JPEG_1X8,
128         MEDIA_BUS_FMT_S5C_UYVY_JPEG_1X8,
129         MEDIA_BUS_FMT_AHSV8888_1X32,
130 };
131
132 /* Helper function to check mbus codes */
133 bool vimc_mbus_code_supported(__u32 code)
134 {
135         unsigned int i;
136
137         for (i = 0; i < ARRAY_SIZE(vimc_mbus_list); i++)
138                 if (code == vimc_mbus_list[i])
139                         return true;
140         return false;
141 }
142 EXPORT_SYMBOL_GPL(vimc_mbus_code_supported);
143
144 /* Helper function to enumerate mbus codes */
145 int vimc_enum_mbus_code(struct v4l2_subdev *sd,
146                         struct v4l2_subdev_pad_config *cfg,
147                         struct v4l2_subdev_mbus_code_enum *code)
148 {
149         if (code->index >= ARRAY_SIZE(vimc_mbus_list))
150                 return -EINVAL;
151
152         code->code = vimc_mbus_list[code->index];
153         return 0;
154 }
155 EXPORT_SYMBOL_GPL(vimc_enum_mbus_code);
156
157 /* Helper function to allocate and initialize pads */
158 struct media_pad *vimc_pads_init(u16 num_pads, const unsigned long *pads_flag)
159 {
160         struct media_pad *pads;
161         unsigned int i;
162
163         /* Allocate memory for the pads */
164         pads = kcalloc(num_pads, sizeof(*pads), GFP_KERNEL);
165         if (!pads)
166                 return ERR_PTR(-ENOMEM);
167
168         /* Initialize the pads */
169         for (i = 0; i < num_pads; i++) {
170                 pads[i].index = i;
171                 pads[i].flags = pads_flag[i];
172         }
173
174         return pads;
175 }
176 EXPORT_SYMBOL_GPL(vimc_pads_init);
177
178 int vimc_pipeline_s_stream(struct media_entity *ent, int enable)
179 {
180         struct v4l2_subdev *sd;
181         struct media_pad *pad;
182         unsigned int i;
183         int ret;
184
185         for (i = 0; i < ent->num_pads; i++) {
186                 if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
187                         continue;
188
189                 /* Start the stream in the subdevice direct connected */
190                 pad = media_entity_remote_pad(&ent->pads[i]);
191                 if (!pad)
192                         continue;
193
194                 if (!is_media_entity_v4l2_subdev(pad->entity))
195                         return -EINVAL;
196
197                 sd = media_entity_to_v4l2_subdev(pad->entity);
198                 ret = v4l2_subdev_call(sd, video, s_stream, enable);
199                 if (ret && ret != -ENOIOCTLCMD)
200                         return ret;
201         }
202
203         return 0;
204 }
205 EXPORT_SYMBOL_GPL(vimc_pipeline_s_stream);
206
207 static int vimc_get_mbus_format(struct media_pad *pad,
208                                 struct v4l2_subdev_format *fmt)
209 {
210         if (is_media_entity_v4l2_subdev(pad->entity)) {
211                 struct v4l2_subdev *sd =
212                         media_entity_to_v4l2_subdev(pad->entity);
213                 int ret;
214
215                 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
216                 fmt->pad = pad->index;
217
218                 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
219                 if (ret)
220                         return ret;
221
222         } else if (is_media_entity_v4l2_video_device(pad->entity)) {
223                 struct video_device *vdev = container_of(pad->entity,
224                                                          struct video_device,
225                                                          entity);
226                 struct vimc_ent_device *ved = video_get_drvdata(vdev);
227                 struct v4l2_pix_format vdev_fmt;
228
229                 if (!ved->vdev_get_format)
230                         return -ENOIOCTLCMD;
231
232                 ved->vdev_get_format(ved, &vdev_fmt);
233                 v4l2_fill_mbus_format(&fmt->format, &vdev_fmt, 0);
234         } else {
235                 return -EINVAL;
236         }
237
238         return 0;
239 }
240
241 int vimc_link_validate(struct media_link *link)
242 {
243         struct v4l2_subdev_format source_fmt, sink_fmt;
244         int ret;
245
246         ret = vimc_get_mbus_format(link->source, &source_fmt);
247         if (ret)
248                 return ret;
249
250         ret = vimc_get_mbus_format(link->sink, &sink_fmt);
251         if (ret)
252                 return ret;
253
254         pr_info("vimc link validate: "
255                 "%s:src:%dx%d (0x%x, %d, %d, %d, %d) "
256                 "%s:snk:%dx%d (0x%x, %d, %d, %d, %d)\n",
257                 /* src */
258                 link->source->entity->name,
259                 source_fmt.format.width, source_fmt.format.height,
260                 source_fmt.format.code, source_fmt.format.colorspace,
261                 source_fmt.format.quantization, source_fmt.format.xfer_func,
262                 source_fmt.format.ycbcr_enc,
263                 /* sink */
264                 link->sink->entity->name,
265                 sink_fmt.format.width, sink_fmt.format.height,
266                 sink_fmt.format.code, sink_fmt.format.colorspace,
267                 sink_fmt.format.quantization, sink_fmt.format.xfer_func,
268                 sink_fmt.format.ycbcr_enc);
269
270         /* The width, height and code must match. */
271         if (source_fmt.format.width != sink_fmt.format.width
272             || source_fmt.format.height != sink_fmt.format.height
273             || (source_fmt.format.code && sink_fmt.format.code &&
274                 source_fmt.format.code != sink_fmt.format.code)) {
275                 pr_err("vimc: format doesn't match in link %s->%s\n",
276                         link->source->entity->name, link->sink->entity->name);
277                 return -EPIPE;
278         }
279
280         /*
281          * The field order must match, or the sink field order must be NONE
282          * to support interlaced hardware connected to bridges that support
283          * progressive formats only.
284          */
285         if (source_fmt.format.field != sink_fmt.format.field &&
286             sink_fmt.format.field != V4L2_FIELD_NONE)
287                 return -EPIPE;
288
289         /*
290          * If colorspace is DEFAULT, then assume all the colorimetry is also
291          * DEFAULT, return 0 to skip comparing the other colorimetry parameters
292          */
293         if (source_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT
294             || sink_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT)
295                 return 0;
296
297         /* Colorspace must match. */
298         if (source_fmt.format.colorspace != sink_fmt.format.colorspace)
299                 return -EPIPE;
300
301         /* Colorimetry must match if they are not set to DEFAULT */
302         if (source_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
303             && sink_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
304             && source_fmt.format.ycbcr_enc != sink_fmt.format.ycbcr_enc)
305                 return -EPIPE;
306
307         if (source_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
308             && sink_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
309             && source_fmt.format.quantization != sink_fmt.format.quantization)
310                 return -EPIPE;
311
312         if (source_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
313             && sink_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
314             && source_fmt.format.xfer_func != sink_fmt.format.xfer_func)
315                 return -EPIPE;
316
317         return 0;
318 }
319 EXPORT_SYMBOL_GPL(vimc_link_validate);
320
321 static const struct media_entity_operations vimc_ent_sd_mops = {
322         .link_validate = vimc_link_validate,
323 };
324
325 int vimc_ent_sd_register(struct vimc_ent_device *ved,
326                          struct v4l2_subdev *sd,
327                          struct v4l2_device *v4l2_dev,
328                          const char *const name,
329                          u32 function,
330                          u16 num_pads,
331                          const unsigned long *pads_flag,
332                          const struct v4l2_subdev_internal_ops *sd_int_ops,
333                          const struct v4l2_subdev_ops *sd_ops)
334 {
335         int ret;
336
337         /* Allocate the pads */
338         ved->pads = vimc_pads_init(num_pads, pads_flag);
339         if (IS_ERR(ved->pads))
340                 return PTR_ERR(ved->pads);
341
342         /* Fill the vimc_ent_device struct */
343         ved->ent = &sd->entity;
344
345         /* Initialize the subdev */
346         v4l2_subdev_init(sd, sd_ops);
347         sd->internal_ops = sd_int_ops;
348         sd->entity.function = function;
349         sd->entity.ops = &vimc_ent_sd_mops;
350         sd->owner = THIS_MODULE;
351         strscpy(sd->name, name, sizeof(sd->name));
352         v4l2_set_subdevdata(sd, ved);
353
354         /* Expose this subdev to user space */
355         sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
356         if (sd->ctrl_handler)
357                 sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
358
359         /* Initialize the media entity */
360         ret = media_entity_pads_init(&sd->entity, num_pads, ved->pads);
361         if (ret)
362                 goto err_clean_pads;
363
364         /* Register the subdev with the v4l2 and the media framework */
365         ret = v4l2_device_register_subdev(v4l2_dev, sd);
366         if (ret) {
367                 dev_err(v4l2_dev->dev,
368                         "%s: subdev register failed (err=%d)\n",
369                         name, ret);
370                 goto err_clean_m_ent;
371         }
372
373         return 0;
374
375 err_clean_m_ent:
376         media_entity_cleanup(&sd->entity);
377 err_clean_pads:
378         vimc_pads_cleanup(ved->pads);
379         return ret;
380 }
381 EXPORT_SYMBOL_GPL(vimc_ent_sd_register);
382
383 void vimc_ent_sd_unregister(struct vimc_ent_device *ved, struct v4l2_subdev *sd)
384 {
385         media_entity_cleanup(ved->ent);
386         vimc_pads_cleanup(ved->pads);
387         v4l2_device_unregister_subdev(sd);
388 }
389 EXPORT_SYMBOL_GPL(vimc_ent_sd_unregister);
390
391 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Common");
392 MODULE_AUTHOR("Helen Koike <[email protected]>");
393 MODULE_LICENSE("GPL");
This page took 0.052622 seconds and 4 git commands to generate.