2 * vimc-common.c Virtual Media Controller Driver
4 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
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.
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.
18 #include <linux/init.h>
19 #include <linux/module.h>
21 #include "vimc-common.h"
24 * NOTE: non-bayer formats need to come first (necessary for enum_mbus_code
27 static const struct vimc_pix_map vimc_pix_map_list
[] = {
28 /* TODO: add all missing formats */
32 .code
= MEDIA_BUS_FMT_BGR888_1X24
,
33 .pixelformat
= V4L2_PIX_FMT_BGR24
,
38 .code
= MEDIA_BUS_FMT_RGB888_1X24
,
39 .pixelformat
= V4L2_PIX_FMT_RGB24
,
44 .code
= MEDIA_BUS_FMT_ARGB8888_1X32
,
45 .pixelformat
= V4L2_PIX_FMT_ARGB32
,
52 .code
= MEDIA_BUS_FMT_SBGGR8_1X8
,
53 .pixelformat
= V4L2_PIX_FMT_SBGGR8
,
58 .code
= MEDIA_BUS_FMT_SGBRG8_1X8
,
59 .pixelformat
= V4L2_PIX_FMT_SGBRG8
,
64 .code
= MEDIA_BUS_FMT_SGRBG8_1X8
,
65 .pixelformat
= V4L2_PIX_FMT_SGRBG8
,
70 .code
= MEDIA_BUS_FMT_SRGGB8_1X8
,
71 .pixelformat
= V4L2_PIX_FMT_SRGGB8
,
76 .code
= MEDIA_BUS_FMT_SBGGR10_1X10
,
77 .pixelformat
= V4L2_PIX_FMT_SBGGR10
,
82 .code
= MEDIA_BUS_FMT_SGBRG10_1X10
,
83 .pixelformat
= V4L2_PIX_FMT_SGBRG10
,
88 .code
= MEDIA_BUS_FMT_SGRBG10_1X10
,
89 .pixelformat
= V4L2_PIX_FMT_SGRBG10
,
94 .code
= MEDIA_BUS_FMT_SRGGB10_1X10
,
95 .pixelformat
= V4L2_PIX_FMT_SRGGB10
,
100 /* 10bit raw bayer a-law compressed to 8 bits */
102 .code
= MEDIA_BUS_FMT_SBGGR10_ALAW8_1X8
,
103 .pixelformat
= V4L2_PIX_FMT_SBGGR10ALAW8
,
108 .code
= MEDIA_BUS_FMT_SGBRG10_ALAW8_1X8
,
109 .pixelformat
= V4L2_PIX_FMT_SGBRG10ALAW8
,
114 .code
= MEDIA_BUS_FMT_SGRBG10_ALAW8_1X8
,
115 .pixelformat
= V4L2_PIX_FMT_SGRBG10ALAW8
,
120 .code
= MEDIA_BUS_FMT_SRGGB10_ALAW8_1X8
,
121 .pixelformat
= V4L2_PIX_FMT_SRGGB10ALAW8
,
126 /* 10bit raw bayer DPCM compressed to 8 bits */
128 .code
= MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8
,
129 .pixelformat
= V4L2_PIX_FMT_SBGGR10DPCM8
,
134 .code
= MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8
,
135 .pixelformat
= V4L2_PIX_FMT_SGBRG10DPCM8
,
140 .code
= MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8
,
141 .pixelformat
= V4L2_PIX_FMT_SGRBG10DPCM8
,
146 .code
= MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8
,
147 .pixelformat
= V4L2_PIX_FMT_SRGGB10DPCM8
,
152 .code
= MEDIA_BUS_FMT_SBGGR12_1X12
,
153 .pixelformat
= V4L2_PIX_FMT_SBGGR12
,
158 .code
= MEDIA_BUS_FMT_SGBRG12_1X12
,
159 .pixelformat
= V4L2_PIX_FMT_SGBRG12
,
164 .code
= MEDIA_BUS_FMT_SGRBG12_1X12
,
165 .pixelformat
= V4L2_PIX_FMT_SGRBG12
,
170 .code
= MEDIA_BUS_FMT_SRGGB12_1X12
,
171 .pixelformat
= V4L2_PIX_FMT_SRGGB12
,
177 const struct vimc_pix_map
*vimc_pix_map_by_index(unsigned int i
)
179 if (i
>= ARRAY_SIZE(vimc_pix_map_list
))
182 return &vimc_pix_map_list
[i
];
184 EXPORT_SYMBOL_GPL(vimc_pix_map_by_index
);
186 const struct vimc_pix_map
*vimc_pix_map_by_code(u32 code
)
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
];
196 EXPORT_SYMBOL_GPL(vimc_pix_map_by_code
);
198 const struct vimc_pix_map
*vimc_pix_map_by_pixelformat(u32 pixelformat
)
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
];
208 EXPORT_SYMBOL_GPL(vimc_pix_map_by_pixelformat
);
210 int vimc_propagate_frame(struct media_pad
*src
, const void *frame
)
212 struct media_link
*link
;
214 if (!(src
->flags
& MEDIA_PAD_FL_SOURCE
))
217 /* Send this frame to all sink pads that are direct linked */
218 list_for_each_entry(link
, &src
->entity
->links
, list
) {
219 if (link
->source
== src
&&
220 (link
->flags
& MEDIA_LNK_FL_ENABLED
)) {
221 struct vimc_ent_device
*ved
= NULL
;
222 struct media_entity
*entity
= link
->sink
->entity
;
224 if (is_media_entity_v4l2_subdev(entity
)) {
225 struct v4l2_subdev
*sd
=
226 container_of(entity
, struct v4l2_subdev
,
228 ved
= v4l2_get_subdevdata(sd
);
229 } else if (is_media_entity_v4l2_video_device(entity
)) {
230 struct video_device
*vdev
=
234 ved
= video_get_drvdata(vdev
);
236 if (ved
&& ved
->process_frame
)
237 ved
->process_frame(ved
, link
->sink
, frame
);
243 EXPORT_SYMBOL_GPL(vimc_propagate_frame
);
245 /* Helper function to allocate and initialize pads */
246 struct media_pad
*vimc_pads_init(u16 num_pads
, const unsigned long *pads_flag
)
248 struct media_pad
*pads
;
251 /* Allocate memory for the pads */
252 pads
= kcalloc(num_pads
, sizeof(*pads
), GFP_KERNEL
);
254 return ERR_PTR(-ENOMEM
);
256 /* Initialize the pads */
257 for (i
= 0; i
< num_pads
; i
++) {
259 pads
[i
].flags
= pads_flag
[i
];
264 EXPORT_SYMBOL_GPL(vimc_pads_init
);
266 int vimc_pipeline_s_stream(struct media_entity
*ent
, int enable
)
268 struct v4l2_subdev
*sd
;
269 struct media_pad
*pad
;
273 for (i
= 0; i
< ent
->num_pads
; i
++) {
274 if (ent
->pads
[i
].flags
& MEDIA_PAD_FL_SOURCE
)
277 /* Start the stream in the subdevice direct connected */
278 pad
= media_entity_remote_pad(&ent
->pads
[i
]);
280 if (!is_media_entity_v4l2_subdev(pad
->entity
))
283 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
284 ret
= v4l2_subdev_call(sd
, video
, s_stream
, enable
);
285 if (ret
&& ret
!= -ENOIOCTLCMD
)
291 EXPORT_SYMBOL_GPL(vimc_pipeline_s_stream
);
293 static int vimc_get_mbus_format(struct media_pad
*pad
,
294 struct v4l2_subdev_format
*fmt
)
296 if (is_media_entity_v4l2_subdev(pad
->entity
)) {
297 struct v4l2_subdev
*sd
=
298 media_entity_to_v4l2_subdev(pad
->entity
);
301 fmt
->which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
302 fmt
->pad
= pad
->index
;
304 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, fmt
);
308 } else if (is_media_entity_v4l2_video_device(pad
->entity
)) {
309 struct video_device
*vdev
= container_of(pad
->entity
,
312 struct vimc_ent_device
*ved
= video_get_drvdata(vdev
);
313 const struct vimc_pix_map
*vpix
;
314 struct v4l2_pix_format vdev_fmt
;
316 if (!ved
->vdev_get_format
)
319 ved
->vdev_get_format(ved
, &vdev_fmt
);
320 vpix
= vimc_pix_map_by_pixelformat(vdev_fmt
.pixelformat
);
321 v4l2_fill_mbus_format(&fmt
->format
, &vdev_fmt
, vpix
->code
);
329 int vimc_link_validate(struct media_link
*link
)
331 struct v4l2_subdev_format source_fmt
, sink_fmt
;
334 ret
= vimc_get_mbus_format(link
->source
, &source_fmt
);
338 ret
= vimc_get_mbus_format(link
->sink
, &sink_fmt
);
342 pr_info("vimc link validate: "
343 "%s:src:%dx%d (0x%x, %d, %d, %d, %d) "
344 "%s:snk:%dx%d (0x%x, %d, %d, %d, %d)\n",
346 link
->source
->entity
->name
,
347 source_fmt
.format
.width
, source_fmt
.format
.height
,
348 source_fmt
.format
.code
, source_fmt
.format
.colorspace
,
349 source_fmt
.format
.quantization
, source_fmt
.format
.xfer_func
,
350 source_fmt
.format
.ycbcr_enc
,
352 link
->sink
->entity
->name
,
353 sink_fmt
.format
.width
, sink_fmt
.format
.height
,
354 sink_fmt
.format
.code
, sink_fmt
.format
.colorspace
,
355 sink_fmt
.format
.quantization
, sink_fmt
.format
.xfer_func
,
356 sink_fmt
.format
.ycbcr_enc
);
358 /* The width, height and code must match. */
359 if (source_fmt
.format
.width
!= sink_fmt
.format
.width
360 || source_fmt
.format
.height
!= sink_fmt
.format
.height
361 || source_fmt
.format
.code
!= sink_fmt
.format
.code
)
365 * The field order must match, or the sink field order must be NONE
366 * to support interlaced hardware connected to bridges that support
367 * progressive formats only.
369 if (source_fmt
.format
.field
!= sink_fmt
.format
.field
&&
370 sink_fmt
.format
.field
!= V4L2_FIELD_NONE
)
374 * If colorspace is DEFAULT, then assume all the colorimetry is also
375 * DEFAULT, return 0 to skip comparing the other colorimetry parameters
377 if (source_fmt
.format
.colorspace
== V4L2_COLORSPACE_DEFAULT
378 || sink_fmt
.format
.colorspace
== V4L2_COLORSPACE_DEFAULT
)
381 /* Colorspace must match. */
382 if (source_fmt
.format
.colorspace
!= sink_fmt
.format
.colorspace
)
385 /* Colorimetry must match if they are not set to DEFAULT */
386 if (source_fmt
.format
.ycbcr_enc
!= V4L2_YCBCR_ENC_DEFAULT
387 && sink_fmt
.format
.ycbcr_enc
!= V4L2_YCBCR_ENC_DEFAULT
388 && source_fmt
.format
.ycbcr_enc
!= sink_fmt
.format
.ycbcr_enc
)
391 if (source_fmt
.format
.quantization
!= V4L2_QUANTIZATION_DEFAULT
392 && sink_fmt
.format
.quantization
!= V4L2_QUANTIZATION_DEFAULT
393 && source_fmt
.format
.quantization
!= sink_fmt
.format
.quantization
)
396 if (source_fmt
.format
.xfer_func
!= V4L2_XFER_FUNC_DEFAULT
397 && sink_fmt
.format
.xfer_func
!= V4L2_XFER_FUNC_DEFAULT
398 && source_fmt
.format
.xfer_func
!= sink_fmt
.format
.xfer_func
)
403 EXPORT_SYMBOL_GPL(vimc_link_validate
);
405 static const struct media_entity_operations vimc_ent_sd_mops
= {
406 .link_validate
= vimc_link_validate
,
409 int vimc_ent_sd_register(struct vimc_ent_device
*ved
,
410 struct v4l2_subdev
*sd
,
411 struct v4l2_device
*v4l2_dev
,
412 const char *const name
,
415 const unsigned long *pads_flag
,
416 const struct v4l2_subdev_ops
*sd_ops
)
420 /* Allocate the pads */
421 ved
->pads
= vimc_pads_init(num_pads
, pads_flag
);
422 if (IS_ERR(ved
->pads
))
423 return PTR_ERR(ved
->pads
);
425 /* Fill the vimc_ent_device struct */
426 ved
->ent
= &sd
->entity
;
428 /* Initialize the subdev */
429 v4l2_subdev_init(sd
, sd_ops
);
430 sd
->entity
.function
= function
;
431 sd
->entity
.ops
= &vimc_ent_sd_mops
;
432 sd
->owner
= THIS_MODULE
;
433 strlcpy(sd
->name
, name
, sizeof(sd
->name
));
434 v4l2_set_subdevdata(sd
, ved
);
436 /* Expose this subdev to user space */
437 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
438 if (sd
->ctrl_handler
)
439 sd
->flags
|= V4L2_SUBDEV_FL_HAS_EVENTS
;
441 /* Initialize the media entity */
442 ret
= media_entity_pads_init(&sd
->entity
, num_pads
, ved
->pads
);
446 /* Register the subdev with the v4l2 and the media framework */
447 ret
= v4l2_device_register_subdev(v4l2_dev
, sd
);
449 dev_err(v4l2_dev
->dev
,
450 "%s: subdev register failed (err=%d)\n",
452 goto err_clean_m_ent
;
458 media_entity_cleanup(&sd
->entity
);
460 vimc_pads_cleanup(ved
->pads
);
463 EXPORT_SYMBOL_GPL(vimc_ent_sd_register
);
465 void vimc_ent_sd_unregister(struct vimc_ent_device
*ved
, struct v4l2_subdev
*sd
)
467 v4l2_device_unregister_subdev(sd
);
468 media_entity_cleanup(ved
->ent
);
469 vimc_pads_cleanup(ved
->pads
);
471 EXPORT_SYMBOL_GPL(vimc_ent_sd_unregister
);
473 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Common");
474 MODULE_AUTHOR("Helen Koike <helen.fornazier@gmail.com>");
475 MODULE_LICENSE("GPL");