blob: e8e6c6b95db94eb7b4e391c9b68a93ade215ec97 [file] [log] [blame]
Helen Fornazier5ba0ae42017-06-19 14:00:11 -03001/*
2 * vimc-common.c 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
Helen Fornazier4a29b702017-06-19 14:00:18 -030018#include <linux/init.h>
19#include <linux/module.h>
20
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030021#include "vimc-common.h"
22
Helen Fornazier6856ba72017-06-19 14:00:20 -030023/*
24 * NOTE: non-bayer formats need to come first (necessary for enum_mbus_code
25 * in the scaler)
26 */
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030027static const struct vimc_pix_map vimc_pix_map_list[] = {
28 /* TODO: add all missing formats */
29
30 /* RGB formats */
31 {
32 .code = MEDIA_BUS_FMT_BGR888_1X24,
33 .pixelformat = V4L2_PIX_FMT_BGR24,
34 .bpp = 3,
Helen Fornazier6856ba72017-06-19 14:00:20 -030035 .bayer = false,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030036 },
37 {
38 .code = MEDIA_BUS_FMT_RGB888_1X24,
39 .pixelformat = V4L2_PIX_FMT_RGB24,
40 .bpp = 3,
Helen Fornazier6856ba72017-06-19 14:00:20 -030041 .bayer = false,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030042 },
43 {
44 .code = MEDIA_BUS_FMT_ARGB8888_1X32,
45 .pixelformat = V4L2_PIX_FMT_ARGB32,
46 .bpp = 4,
Helen Fornazier6856ba72017-06-19 14:00:20 -030047 .bayer = false,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030048 },
49
50 /* Bayer formats */
51 {
52 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
53 .pixelformat = V4L2_PIX_FMT_SBGGR8,
54 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -030055 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030056 },
57 {
58 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
59 .pixelformat = V4L2_PIX_FMT_SGBRG8,
60 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -030061 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030062 },
63 {
64 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
65 .pixelformat = V4L2_PIX_FMT_SGRBG8,
66 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -030067 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030068 },
69 {
70 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
71 .pixelformat = V4L2_PIX_FMT_SRGGB8,
72 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -030073 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030074 },
75 {
76 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
77 .pixelformat = V4L2_PIX_FMT_SBGGR10,
78 .bpp = 2,
Helen Fornazier6856ba72017-06-19 14:00:20 -030079 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030080 },
81 {
82 .code = MEDIA_BUS_FMT_SGBRG10_1X10,
83 .pixelformat = V4L2_PIX_FMT_SGBRG10,
84 .bpp = 2,
Helen Fornazier6856ba72017-06-19 14:00:20 -030085 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030086 },
87 {
88 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
89 .pixelformat = V4L2_PIX_FMT_SGRBG10,
90 .bpp = 2,
Helen Fornazier6856ba72017-06-19 14:00:20 -030091 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030092 },
93 {
94 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
95 .pixelformat = V4L2_PIX_FMT_SRGGB10,
96 .bpp = 2,
Helen Fornazier6856ba72017-06-19 14:00:20 -030097 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030098 },
99
100 /* 10bit raw bayer a-law compressed to 8 bits */
101 {
102 .code = MEDIA_BUS_FMT_SBGGR10_ALAW8_1X8,
103 .pixelformat = V4L2_PIX_FMT_SBGGR10ALAW8,
104 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300105 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300106 },
107 {
108 .code = MEDIA_BUS_FMT_SGBRG10_ALAW8_1X8,
109 .pixelformat = V4L2_PIX_FMT_SGBRG10ALAW8,
110 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300111 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300112 },
113 {
114 .code = MEDIA_BUS_FMT_SGRBG10_ALAW8_1X8,
115 .pixelformat = V4L2_PIX_FMT_SGRBG10ALAW8,
116 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300117 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300118 },
119 {
120 .code = MEDIA_BUS_FMT_SRGGB10_ALAW8_1X8,
121 .pixelformat = V4L2_PIX_FMT_SRGGB10ALAW8,
122 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300123 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300124 },
125
126 /* 10bit raw bayer DPCM compressed to 8 bits */
127 {
128 .code = MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
129 .pixelformat = V4L2_PIX_FMT_SBGGR10DPCM8,
130 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300131 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300132 },
133 {
134 .code = MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
135 .pixelformat = V4L2_PIX_FMT_SGBRG10DPCM8,
136 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300137 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300138 },
139 {
140 .code = MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
141 .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
142 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300143 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300144 },
145 {
146 .code = MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
147 .pixelformat = V4L2_PIX_FMT_SRGGB10DPCM8,
148 .bpp = 1,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300149 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300150 },
151 {
152 .code = MEDIA_BUS_FMT_SBGGR12_1X12,
153 .pixelformat = V4L2_PIX_FMT_SBGGR12,
154 .bpp = 2,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300155 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300156 },
157 {
158 .code = MEDIA_BUS_FMT_SGBRG12_1X12,
159 .pixelformat = V4L2_PIX_FMT_SGBRG12,
160 .bpp = 2,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300161 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300162 },
163 {
164 .code = MEDIA_BUS_FMT_SGRBG12_1X12,
165 .pixelformat = V4L2_PIX_FMT_SGRBG12,
166 .bpp = 2,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300167 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300168 },
169 {
170 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
171 .pixelformat = V4L2_PIX_FMT_SRGGB12,
172 .bpp = 2,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300173 .bayer = true,
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300174 },
175};
176
Helen Fornazier88ad71a2017-06-19 14:00:16 -0300177const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i)
178{
179 if (i >= ARRAY_SIZE(vimc_pix_map_list))
180 return NULL;
181
182 return &vimc_pix_map_list[i];
183}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300184EXPORT_SYMBOL_GPL(vimc_pix_map_by_index);
Helen Fornazier88ad71a2017-06-19 14:00:16 -0300185
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300186const struct vimc_pix_map *vimc_pix_map_by_code(u32 code)
187{
188 unsigned int i;
189
190 for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) {
191 if (vimc_pix_map_list[i].code == code)
192 return &vimc_pix_map_list[i];
193 }
194 return NULL;
195}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300196EXPORT_SYMBOL_GPL(vimc_pix_map_by_code);
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300197
198const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat)
199{
200 unsigned int i;
201
202 for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) {
203 if (vimc_pix_map_list[i].pixelformat == pixelformat)
204 return &vimc_pix_map_list[i];
205 }
206 return NULL;
207}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300208EXPORT_SYMBOL_GPL(vimc_pix_map_by_pixelformat);
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300209
Helen Fornazier5ba0ae42017-06-19 14:00:11 -0300210/* Helper function to allocate and initialize pads */
211struct media_pad *vimc_pads_init(u16 num_pads, const unsigned long *pads_flag)
212{
213 struct media_pad *pads;
214 unsigned int i;
215
216 /* Allocate memory for the pads */
217 pads = kcalloc(num_pads, sizeof(*pads), GFP_KERNEL);
218 if (!pads)
219 return ERR_PTR(-ENOMEM);
220
221 /* Initialize the pads */
222 for (i = 0; i < num_pads; i++) {
223 pads[i].index = i;
224 pads[i].flags = pads_flag[i];
225 }
226
227 return pads;
228}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300229EXPORT_SYMBOL_GPL(vimc_pads_init);
Helen Fornazierc1495432017-06-19 14:00:12 -0300230
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300231int vimc_pipeline_s_stream(struct media_entity *ent, int enable)
232{
233 struct v4l2_subdev *sd;
234 struct media_pad *pad;
235 unsigned int i;
236 int ret;
237
238 for (i = 0; i < ent->num_pads; i++) {
239 if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
240 continue;
241
242 /* Start the stream in the subdevice direct connected */
243 pad = media_entity_remote_pad(&ent->pads[i]);
Helen Fornaziere159b602018-12-07 12:56:41 -0500244 if (!pad)
245 continue;
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300246
Helen Fornazier4a29b702017-06-19 14:00:18 -0300247 if (!is_media_entity_v4l2_subdev(pad->entity))
248 return -EINVAL;
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300249
250 sd = media_entity_to_v4l2_subdev(pad->entity);
251 ret = v4l2_subdev_call(sd, video, s_stream, enable);
252 if (ret && ret != -ENOIOCTLCMD)
253 return ret;
254 }
255
256 return 0;
257}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300258EXPORT_SYMBOL_GPL(vimc_pipeline_s_stream);
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300259
Helen Fornazier288a22d2017-06-19 14:00:14 -0300260static int vimc_get_mbus_format(struct media_pad *pad,
261 struct v4l2_subdev_format *fmt)
262{
263 if (is_media_entity_v4l2_subdev(pad->entity)) {
264 struct v4l2_subdev *sd =
265 media_entity_to_v4l2_subdev(pad->entity);
266 int ret;
267
268 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
269 fmt->pad = pad->index;
270
271 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
272 if (ret)
273 return ret;
274
275 } else if (is_media_entity_v4l2_video_device(pad->entity)) {
276 struct video_device *vdev = container_of(pad->entity,
277 struct video_device,
278 entity);
279 struct vimc_ent_device *ved = video_get_drvdata(vdev);
280 const struct vimc_pix_map *vpix;
281 struct v4l2_pix_format vdev_fmt;
282
283 if (!ved->vdev_get_format)
284 return -ENOIOCTLCMD;
285
286 ved->vdev_get_format(ved, &vdev_fmt);
287 vpix = vimc_pix_map_by_pixelformat(vdev_fmt.pixelformat);
288 v4l2_fill_mbus_format(&fmt->format, &vdev_fmt, vpix->code);
289 } else {
290 return -EINVAL;
291 }
292
293 return 0;
294}
295
296int vimc_link_validate(struct media_link *link)
297{
298 struct v4l2_subdev_format source_fmt, sink_fmt;
299 int ret;
300
Helen Fornazier288a22d2017-06-19 14:00:14 -0300301 ret = vimc_get_mbus_format(link->source, &source_fmt);
302 if (ret)
303 return ret;
304
305 ret = vimc_get_mbus_format(link->sink, &sink_fmt);
306 if (ret)
307 return ret;
308
309 pr_info("vimc link validate: "
310 "%s:src:%dx%d (0x%x, %d, %d, %d, %d) "
311 "%s:snk:%dx%d (0x%x, %d, %d, %d, %d)\n",
312 /* src */
313 link->source->entity->name,
314 source_fmt.format.width, source_fmt.format.height,
315 source_fmt.format.code, source_fmt.format.colorspace,
316 source_fmt.format.quantization, source_fmt.format.xfer_func,
317 source_fmt.format.ycbcr_enc,
318 /* sink */
319 link->sink->entity->name,
320 sink_fmt.format.width, sink_fmt.format.height,
321 sink_fmt.format.code, sink_fmt.format.colorspace,
322 sink_fmt.format.quantization, sink_fmt.format.xfer_func,
323 sink_fmt.format.ycbcr_enc);
324
325 /* The width, height and code must match. */
326 if (source_fmt.format.width != sink_fmt.format.width
327 || source_fmt.format.height != sink_fmt.format.height
328 || source_fmt.format.code != sink_fmt.format.code)
329 return -EPIPE;
330
331 /*
332 * The field order must match, or the sink field order must be NONE
333 * to support interlaced hardware connected to bridges that support
334 * progressive formats only.
335 */
336 if (source_fmt.format.field != sink_fmt.format.field &&
337 sink_fmt.format.field != V4L2_FIELD_NONE)
338 return -EPIPE;
339
340 /*
341 * If colorspace is DEFAULT, then assume all the colorimetry is also
342 * DEFAULT, return 0 to skip comparing the other colorimetry parameters
343 */
344 if (source_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT
345 || sink_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT)
346 return 0;
347
348 /* Colorspace must match. */
349 if (source_fmt.format.colorspace != sink_fmt.format.colorspace)
350 return -EPIPE;
351
352 /* Colorimetry must match if they are not set to DEFAULT */
353 if (source_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
354 && sink_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
355 && source_fmt.format.ycbcr_enc != sink_fmt.format.ycbcr_enc)
356 return -EPIPE;
357
358 if (source_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
359 && sink_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
360 && source_fmt.format.quantization != sink_fmt.format.quantization)
361 return -EPIPE;
362
363 if (source_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
364 && sink_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
365 && source_fmt.format.xfer_func != sink_fmt.format.xfer_func)
366 return -EPIPE;
367
368 return 0;
369}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300370EXPORT_SYMBOL_GPL(vimc_link_validate);
Helen Fornazier288a22d2017-06-19 14:00:14 -0300371
Helen Fornazierc1495432017-06-19 14:00:12 -0300372static const struct media_entity_operations vimc_ent_sd_mops = {
Helen Fornazier288a22d2017-06-19 14:00:14 -0300373 .link_validate = vimc_link_validate,
Helen Fornazierc1495432017-06-19 14:00:12 -0300374};
375
376int vimc_ent_sd_register(struct vimc_ent_device *ved,
377 struct v4l2_subdev *sd,
378 struct v4l2_device *v4l2_dev,
379 const char *const name,
380 u32 function,
381 u16 num_pads,
382 const unsigned long *pads_flag,
Hans Verkuil2b177f22019-03-05 03:36:20 -0500383 const struct v4l2_subdev_internal_ops *sd_int_ops,
Helen Fornazier4a29b702017-06-19 14:00:18 -0300384 const struct v4l2_subdev_ops *sd_ops)
Helen Fornazierc1495432017-06-19 14:00:12 -0300385{
386 int ret;
387
388 /* Allocate the pads */
389 ved->pads = vimc_pads_init(num_pads, pads_flag);
390 if (IS_ERR(ved->pads))
391 return PTR_ERR(ved->pads);
392
393 /* Fill the vimc_ent_device struct */
Helen Fornazierc1495432017-06-19 14:00:12 -0300394 ved->ent = &sd->entity;
395
396 /* Initialize the subdev */
397 v4l2_subdev_init(sd, sd_ops);
Hans Verkuil2b177f22019-03-05 03:36:20 -0500398 sd->internal_ops = sd_int_ops;
Helen Fornazierc1495432017-06-19 14:00:12 -0300399 sd->entity.function = function;
400 sd->entity.ops = &vimc_ent_sd_mops;
401 sd->owner = THIS_MODULE;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400402 strscpy(sd->name, name, sizeof(sd->name));
Helen Fornazierc1495432017-06-19 14:00:12 -0300403 v4l2_set_subdevdata(sd, ved);
404
405 /* Expose this subdev to user space */
Hans Verkuil3da7ee92018-02-02 08:00:32 -0500406 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
407 if (sd->ctrl_handler)
408 sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
Helen Fornazierc1495432017-06-19 14:00:12 -0300409
410 /* Initialize the media entity */
411 ret = media_entity_pads_init(&sd->entity, num_pads, ved->pads);
412 if (ret)
413 goto err_clean_pads;
414
415 /* Register the subdev with the v4l2 and the media framework */
416 ret = v4l2_device_register_subdev(v4l2_dev, sd);
417 if (ret) {
418 dev_err(v4l2_dev->dev,
419 "%s: subdev register failed (err=%d)\n",
420 name, ret);
421 goto err_clean_m_ent;
422 }
423
424 return 0;
425
426err_clean_m_ent:
427 media_entity_cleanup(&sd->entity);
428err_clean_pads:
429 vimc_pads_cleanup(ved->pads);
430 return ret;
431}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300432EXPORT_SYMBOL_GPL(vimc_ent_sd_register);
Helen Fornazierc1495432017-06-19 14:00:12 -0300433
434void vimc_ent_sd_unregister(struct vimc_ent_device *ved, struct v4l2_subdev *sd)
435{
Helen Fornazierc1495432017-06-19 14:00:12 -0300436 media_entity_cleanup(ved->ent);
437 vimc_pads_cleanup(ved->pads);
Hans Verkuil2b177f22019-03-05 03:36:20 -0500438 v4l2_device_unregister_subdev(sd);
Helen Fornazierc1495432017-06-19 14:00:12 -0300439}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300440EXPORT_SYMBOL_GPL(vimc_ent_sd_unregister);
441
442MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Common");
443MODULE_AUTHOR("Helen Koike <helen.fornazier@gmail.com>");
444MODULE_LICENSE("GPL");