2 * vimc-debayer.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/component.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/platform_device.h>
22 #include <linux/vmalloc.h>
23 #include <linux/v4l2-mediabus.h>
24 #include <media/v4l2-subdev.h>
26 #include "vimc-common.h"
28 #define VIMC_DEB_DRV_NAME "vimc-debayer"
29 /* This module only supports tranforming a bayer format to V4L2_PIX_FMT_RGB24 */
30 #define VIMC_DEB_SRC_PIXFMT V4L2_PIX_FMT_RGB24
31 #define VIMC_DEB_SRC_MBUS_FMT_DEFAULT MEDIA_BUS_FMT_RGB888_1X24
33 static unsigned int deb_mean_win_size = 3;
34 module_param(deb_mean_win_size, uint, 0000);
35 MODULE_PARM_DESC(deb_mean_win_size, " the window size to calculate the mean.\n"
36 "NOTE: the window size need to be an odd number, as the main pixel "
37 "stays in the center of the window, otherwise the next odd number "
40 #define IS_SINK(pad) (!pad)
41 #define IS_SRC(pad) (pad)
43 enum vimc_deb_rgb_colors {
49 struct vimc_deb_pix_map {
52 enum vimc_deb_rgb_colors order[2][2];
55 struct vimc_deb_device {
56 struct vimc_ent_device ved;
57 struct v4l2_subdev sd;
59 /* The active format */
60 struct v4l2_mbus_framefmt sink_fmt;
62 void (*set_rgb_src)(struct vimc_deb_device *vdeb, unsigned int lin,
63 unsigned int col, unsigned int rgb[3]);
64 /* Values calculated when the stream starts */
66 const struct vimc_deb_pix_map *sink_pix_map;
67 unsigned int sink_bpp;
70 static const struct v4l2_mbus_framefmt sink_fmt_default = {
73 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
74 .field = V4L2_FIELD_NONE,
75 .colorspace = V4L2_COLORSPACE_DEFAULT,
78 static const struct vimc_deb_pix_map vimc_deb_pix_map_list[] = {
80 .pixelformat = V4L2_PIX_FMT_SBGGR8,
81 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
82 .order = { { VIMC_DEB_BLUE, VIMC_DEB_GREEN },
83 { VIMC_DEB_GREEN, VIMC_DEB_RED } }
86 .pixelformat = V4L2_PIX_FMT_SGBRG8,
87 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
88 .order = { { VIMC_DEB_GREEN, VIMC_DEB_BLUE },
89 { VIMC_DEB_RED, VIMC_DEB_GREEN } }
92 .pixelformat = V4L2_PIX_FMT_SGRBG8,
93 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
94 .order = { { VIMC_DEB_GREEN, VIMC_DEB_RED },
95 { VIMC_DEB_BLUE, VIMC_DEB_GREEN } }
98 .pixelformat = V4L2_PIX_FMT_SRGGB8,
99 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
100 .order = { { VIMC_DEB_RED, VIMC_DEB_GREEN },
101 { VIMC_DEB_GREEN, VIMC_DEB_BLUE } }
104 .pixelformat = V4L2_PIX_FMT_SBGGR10,
105 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
106 .order = { { VIMC_DEB_BLUE, VIMC_DEB_GREEN },
107 { VIMC_DEB_GREEN, VIMC_DEB_RED } }
110 .pixelformat = V4L2_PIX_FMT_SGBRG10,
111 .code = MEDIA_BUS_FMT_SGBRG10_1X10,
112 .order = { { VIMC_DEB_GREEN, VIMC_DEB_BLUE },
113 { VIMC_DEB_RED, VIMC_DEB_GREEN } }
116 .pixelformat = V4L2_PIX_FMT_SGRBG10,
117 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
118 .order = { { VIMC_DEB_GREEN, VIMC_DEB_RED },
119 { VIMC_DEB_BLUE, VIMC_DEB_GREEN } }
122 .pixelformat = V4L2_PIX_FMT_SRGGB10,
123 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
124 .order = { { VIMC_DEB_RED, VIMC_DEB_GREEN },
125 { VIMC_DEB_GREEN, VIMC_DEB_BLUE } }
128 .pixelformat = V4L2_PIX_FMT_SBGGR12,
129 .code = MEDIA_BUS_FMT_SBGGR12_1X12,
130 .order = { { VIMC_DEB_BLUE, VIMC_DEB_GREEN },
131 { VIMC_DEB_GREEN, VIMC_DEB_RED } }
134 .pixelformat = V4L2_PIX_FMT_SGBRG12,
135 .code = MEDIA_BUS_FMT_SGBRG12_1X12,
136 .order = { { VIMC_DEB_GREEN, VIMC_DEB_BLUE },
137 { VIMC_DEB_RED, VIMC_DEB_GREEN } }
140 .pixelformat = V4L2_PIX_FMT_SGRBG12,
141 .code = MEDIA_BUS_FMT_SGRBG12_1X12,
142 .order = { { VIMC_DEB_GREEN, VIMC_DEB_RED },
143 { VIMC_DEB_BLUE, VIMC_DEB_GREEN } }
146 .pixelformat = V4L2_PIX_FMT_SRGGB12,
147 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
148 .order = { { VIMC_DEB_RED, VIMC_DEB_GREEN },
149 { VIMC_DEB_GREEN, VIMC_DEB_BLUE } }
153 static const struct vimc_deb_pix_map *vimc_deb_pix_map_by_code(u32 code)
157 for (i = 0; i < ARRAY_SIZE(vimc_deb_pix_map_list); i++)
158 if (vimc_deb_pix_map_list[i].code == code)
159 return &vimc_deb_pix_map_list[i];
164 static int vimc_deb_init_cfg(struct v4l2_subdev *sd,
165 struct v4l2_subdev_pad_config *cfg)
167 struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
168 struct v4l2_mbus_framefmt *mf;
171 mf = v4l2_subdev_get_try_format(sd, cfg, 0);
172 *mf = sink_fmt_default;
174 for (i = 1; i < sd->entity.num_pads; i++) {
175 mf = v4l2_subdev_get_try_format(sd, cfg, i);
176 *mf = sink_fmt_default;
177 mf->code = vdeb->src_code;
183 static int vimc_deb_enum_mbus_code(struct v4l2_subdev *sd,
184 struct v4l2_subdev_pad_config *cfg,
185 struct v4l2_subdev_mbus_code_enum *code)
187 /* For the sink pad we only support codes in the map_list */
188 if (IS_SINK(code->pad)) {
189 if (code->index >= ARRAY_SIZE(vimc_deb_pix_map_list))
192 code->code = vimc_deb_pix_map_list[code->index].code;
196 return vimc_enum_mbus_code(sd, cfg, code);
199 static int vimc_deb_enum_frame_size(struct v4l2_subdev *sd,
200 struct v4l2_subdev_pad_config *cfg,
201 struct v4l2_subdev_frame_size_enum *fse)
206 /* For the sink pad we only support codes in the map_list */
207 if (IS_SINK(fse->pad)) {
208 const struct vimc_deb_pix_map *vpix =
209 vimc_deb_pix_map_by_code(fse->code);
215 fse->min_width = VIMC_FRAME_MIN_WIDTH;
216 fse->max_width = VIMC_FRAME_MAX_WIDTH;
217 fse->min_height = VIMC_FRAME_MIN_HEIGHT;
218 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
223 static int vimc_deb_get_fmt(struct v4l2_subdev *sd,
224 struct v4l2_subdev_pad_config *cfg,
225 struct v4l2_subdev_format *fmt)
227 struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
229 /* Get the current sink format */
230 fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
231 *v4l2_subdev_get_try_format(sd, cfg, 0) :
234 /* Set the right code for the source pad */
235 if (IS_SRC(fmt->pad))
236 fmt->format.code = vdeb->src_code;
241 static void vimc_deb_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
243 const struct vimc_deb_pix_map *vpix;
245 /* Don't accept a code that is not on the debayer table */
246 vpix = vimc_deb_pix_map_by_code(fmt->code);
248 fmt->code = sink_fmt_default.code;
250 fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
251 VIMC_FRAME_MAX_WIDTH) & ~1;
252 fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
253 VIMC_FRAME_MAX_HEIGHT) & ~1;
255 if (fmt->field == V4L2_FIELD_ANY)
256 fmt->field = sink_fmt_default.field;
258 vimc_colorimetry_clamp(fmt);
261 static int vimc_deb_set_fmt(struct v4l2_subdev *sd,
262 struct v4l2_subdev_pad_config *cfg,
263 struct v4l2_subdev_format *fmt)
265 struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
266 struct v4l2_mbus_framefmt *sink_fmt;
268 if (!vimc_mbus_code_supported(fmt->format.code))
269 fmt->format.code = sink_fmt_default.code;
271 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
272 /* Do not change the format while stream is on */
276 sink_fmt = &vdeb->sink_fmt;
278 sink_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
282 * Do not change the format of the source pad,
283 * it is propagated from the sink (except for the code)
285 if (IS_SRC(fmt->pad)) {
286 vdeb->src_code = fmt->format.code;
287 fmt->format = *sink_fmt;
288 fmt->format.code = vdeb->src_code;
290 /* Set the new format in the sink pad */
291 vimc_deb_adjust_sink_fmt(&fmt->format);
293 dev_dbg(vdeb->dev, "%s: sink format update: "
294 "old:%dx%d (0x%x, %d, %d, %d, %d) "
295 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vdeb->sd.name,
297 sink_fmt->width, sink_fmt->height, sink_fmt->code,
298 sink_fmt->colorspace, sink_fmt->quantization,
299 sink_fmt->xfer_func, sink_fmt->ycbcr_enc,
301 fmt->format.width, fmt->format.height, fmt->format.code,
302 fmt->format.colorspace, fmt->format.quantization,
303 fmt->format.xfer_func, fmt->format.ycbcr_enc);
305 *sink_fmt = fmt->format;
311 static const struct v4l2_subdev_pad_ops vimc_deb_pad_ops = {
312 .init_cfg = vimc_deb_init_cfg,
313 .enum_mbus_code = vimc_deb_enum_mbus_code,
314 .enum_frame_size = vimc_deb_enum_frame_size,
315 .get_fmt = vimc_deb_get_fmt,
316 .set_fmt = vimc_deb_set_fmt,
319 static void vimc_deb_set_rgb_pix_rgb24(struct vimc_deb_device *vdeb,
324 unsigned int i, index;
326 index = VIMC_FRAME_INDEX(lin, col, vdeb->sink_fmt.width, 3);
327 for (i = 0; i < 3; i++)
328 vdeb->src_frame[index + i] = rgb[i];
331 static int vimc_deb_s_stream(struct v4l2_subdev *sd, int enable)
333 struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
336 u32 src_pixelformat = vdeb->ved.stream->producer_pixfmt;
337 const struct v4l2_format_info *pix_info;
338 unsigned int frame_size;
343 /* We only support translating bayer to RGB24 */
344 if (src_pixelformat != V4L2_PIX_FMT_RGB24) {
346 "translating to pixfmt (0x%08x) is not supported\n",
351 /* Get the corresponding pixel map from the table */
353 vimc_deb_pix_map_by_code(vdeb->sink_fmt.code);
355 /* Request bayer format from the pipeline for the sink pad */
356 vdeb->ved.stream->producer_pixfmt =
357 vdeb->sink_pix_map->pixelformat;
359 /* Calculate frame_size of the source */
360 pix_info = v4l2_format_info(src_pixelformat);
361 frame_size = vdeb->sink_fmt.width * vdeb->sink_fmt.height *
364 /* Get bpp from the sink */
365 pix_info = v4l2_format_info(vdeb->sink_pix_map->pixelformat);
366 vdeb->sink_bpp = pix_info->bpp[0];
369 * Allocate the frame buffer. Use vmalloc to be able to
370 * allocate a large amount of memory
372 vdeb->src_frame = vmalloc(frame_size);
373 if (!vdeb->src_frame)
377 if (!vdeb->src_frame)
380 vfree(vdeb->src_frame);
381 vdeb->src_frame = NULL;
387 static const struct v4l2_subdev_video_ops vimc_deb_video_ops = {
388 .s_stream = vimc_deb_s_stream,
391 static const struct v4l2_subdev_ops vimc_deb_ops = {
392 .pad = &vimc_deb_pad_ops,
393 .video = &vimc_deb_video_ops,
396 static unsigned int vimc_deb_get_val(const u8 *bytes,
397 const unsigned int n_bytes)
400 unsigned int acc = 0;
402 for (i = 0; i < n_bytes; i++)
403 acc = acc + (bytes[i] << (8 * i));
408 static void vimc_deb_calc_rgb_sink(struct vimc_deb_device *vdeb,
410 const unsigned int lin,
411 const unsigned int col,
414 unsigned int i, seek, wlin, wcol;
415 unsigned int n_rgb[3] = {0, 0, 0};
417 for (i = 0; i < 3; i++)
421 * Calculate how many we need to subtract to get to the pixel in
422 * the top left corner of the mean window (considering the current
423 * pixel as the center)
425 seek = deb_mean_win_size / 2;
427 /* Sum the values of the colors in the mean window */
430 "deb: %s: --- Calc pixel %dx%d, window mean %d, seek %d ---\n",
431 vdeb->sd.name, lin, col, vdeb->sink_fmt.height, seek);
434 * Iterate through all the lines in the mean window, start
435 * with zero if the pixel is outside the frame and don't pass
436 * the height when the pixel is in the bottom border of the
439 for (wlin = seek > lin ? 0 : lin - seek;
440 wlin < lin + seek + 1 && wlin < vdeb->sink_fmt.height;
444 * Iterate through all the columns in the mean window, start
445 * with zero if the pixel is outside the frame and don't pass
446 * the width when the pixel is in the right border of the
449 for (wcol = seek > col ? 0 : col - seek;
450 wcol < col + seek + 1 && wcol < vdeb->sink_fmt.width;
452 enum vimc_deb_rgb_colors color;
455 /* Check which color this pixel is */
456 color = vdeb->sink_pix_map->order[wlin % 2][wcol % 2];
458 index = VIMC_FRAME_INDEX(wlin, wcol,
459 vdeb->sink_fmt.width,
463 "deb: %s: RGB CALC: frame index %d, win pos %dx%d, color %d\n",
464 vdeb->sd.name, index, wlin, wcol, color);
467 rgb[color] = rgb[color] +
468 vimc_deb_get_val(&frame[index], vdeb->sink_bpp);
470 /* Save how many values we already added */
473 dev_dbg(vdeb->dev, "deb: %s: RGB CALC: val %d, n %d\n",
474 vdeb->sd.name, rgb[color], n_rgb[color]);
478 /* Calculate the mean */
479 for (i = 0; i < 3; i++) {
481 "deb: %s: PRE CALC: %dx%d Color %d, val %d, n %d\n",
482 vdeb->sd.name, lin, col, i, rgb[i], n_rgb[i]);
485 rgb[i] = rgb[i] / n_rgb[i];
488 "deb: %s: FINAL CALC: %dx%d Color %d, val %d\n",
489 vdeb->sd.name, lin, col, i, rgb[i]);
493 static void *vimc_deb_process_frame(struct vimc_ent_device *ved,
494 const void *sink_frame)
496 struct vimc_deb_device *vdeb = container_of(ved, struct vimc_deb_device,
501 /* If the stream in this node is not active, just return */
502 if (!vdeb->src_frame)
503 return ERR_PTR(-EINVAL);
505 for (i = 0; i < vdeb->sink_fmt.height; i++)
506 for (j = 0; j < vdeb->sink_fmt.width; j++) {
507 vimc_deb_calc_rgb_sink(vdeb, sink_frame, i, j, rgb);
508 vdeb->set_rgb_src(vdeb, i, j, rgb);
511 return vdeb->src_frame;
515 static void vimc_deb_release(struct v4l2_subdev *sd)
517 struct vimc_deb_device *vdeb =
518 container_of(sd, struct vimc_deb_device, sd);
523 static const struct v4l2_subdev_internal_ops vimc_deb_int_ops = {
524 .release = vimc_deb_release,
527 static void vimc_deb_comp_unbind(struct device *comp, struct device *master,
530 struct vimc_ent_device *ved = dev_get_drvdata(comp);
531 struct vimc_deb_device *vdeb = container_of(ved, struct vimc_deb_device,
534 vimc_ent_sd_unregister(ved, &vdeb->sd);
537 static int vimc_deb_comp_bind(struct device *comp, struct device *master,
540 struct v4l2_device *v4l2_dev = master_data;
541 struct vimc_platform_data *pdata = comp->platform_data;
542 struct vimc_deb_device *vdeb;
545 /* Allocate the vdeb struct */
546 vdeb = kzalloc(sizeof(*vdeb), GFP_KERNEL);
550 /* Initialize ved and sd */
551 ret = vimc_ent_sd_register(&vdeb->ved, &vdeb->sd, v4l2_dev,
553 MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV, 2,
554 (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
555 MEDIA_PAD_FL_SOURCE},
556 &vimc_deb_int_ops, &vimc_deb_ops);
562 vdeb->ved.process_frame = vimc_deb_process_frame;
563 dev_set_drvdata(comp, &vdeb->ved);
566 /* Initialize the frame format */
567 vdeb->sink_fmt = sink_fmt_default;
568 vdeb->src_code = VIMC_DEB_SRC_MBUS_FMT_DEFAULT;
570 * TODO: Add support for more output formats, we only support
572 * NOTE: the src format is always the same as the sink, except
575 vdeb->set_rgb_src = vimc_deb_set_rgb_pix_rgb24;
580 static const struct component_ops vimc_deb_comp_ops = {
581 .bind = vimc_deb_comp_bind,
582 .unbind = vimc_deb_comp_unbind,
585 static int vimc_deb_probe(struct platform_device *pdev)
587 return component_add(&pdev->dev, &vimc_deb_comp_ops);
590 static int vimc_deb_remove(struct platform_device *pdev)
592 component_del(&pdev->dev, &vimc_deb_comp_ops);
597 static const struct platform_device_id vimc_deb_driver_ids[] = {
599 .name = VIMC_DEB_DRV_NAME,
604 static struct platform_driver vimc_deb_pdrv = {
605 .probe = vimc_deb_probe,
606 .remove = vimc_deb_remove,
607 .id_table = vimc_deb_driver_ids,
609 .name = VIMC_DEB_DRV_NAME,
613 module_platform_driver(vimc_deb_pdrv);
615 MODULE_DEVICE_TABLE(platform, vimc_deb_driver_ids);
617 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Debayer");
618 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
619 MODULE_LICENSE("GPL");