blob: 281f9c1a7249ad1d4cd2d33fb52e495d063bc090 [file] [log] [blame]
Helen Fornazier88f42bf2017-06-19 14:00:19 -03001/*
2 * vimc-debayer.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
18#include <linux/component.h>
19#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070020#include <linux/mod_devicetable.h>
Helen Fornazier88f42bf2017-06-19 14:00:19 -030021#include <linux/platform_device.h>
22#include <linux/vmalloc.h>
23#include <linux/v4l2-mediabus.h>
24#include <media/v4l2-subdev.h>
25
26#include "vimc-common.h"
27
28#define VIMC_DEB_DRV_NAME "vimc-debayer"
Helen Fornazierb6c61a62019-03-13 14:29:37 -040029/* 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
Helen Fornazier88f42bf2017-06-19 14:00:19 -030032
33static unsigned int deb_mean_win_size = 3;
34module_param(deb_mean_win_size, uint, 0000);
35MODULE_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 "
38 "is considered");
39
40#define IS_SINK(pad) (!pad)
41#define IS_SRC(pad) (pad)
42
43enum vimc_deb_rgb_colors {
44 VIMC_DEB_RED = 0,
45 VIMC_DEB_GREEN = 1,
46 VIMC_DEB_BLUE = 2,
47};
48
49struct vimc_deb_pix_map {
Helen Fornazierb6c61a62019-03-13 14:29:37 -040050 u32 pixelformat;
Helen Fornazier88f42bf2017-06-19 14:00:19 -030051 u32 code;
52 enum vimc_deb_rgb_colors order[2][2];
53};
54
55struct vimc_deb_device {
56 struct vimc_ent_device ved;
57 struct v4l2_subdev sd;
58 struct device *dev;
59 /* The active format */
60 struct v4l2_mbus_framefmt sink_fmt;
61 u32 src_code;
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 */
65 u8 *src_frame;
66 const struct vimc_deb_pix_map *sink_pix_map;
67 unsigned int sink_bpp;
68};
69
70static const struct v4l2_mbus_framefmt sink_fmt_default = {
71 .width = 640,
72 .height = 480,
Helen Fornazier85ab1aa2019-03-06 17:42:37 -050073 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
Helen Fornazier88f42bf2017-06-19 14:00:19 -030074 .field = V4L2_FIELD_NONE,
75 .colorspace = V4L2_COLORSPACE_DEFAULT,
76};
77
78static const struct vimc_deb_pix_map vimc_deb_pix_map_list[] = {
79 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -040080 .pixelformat = V4L2_PIX_FMT_SBGGR8,
Helen Fornazier88f42bf2017-06-19 14:00:19 -030081 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
82 .order = { { VIMC_DEB_BLUE, VIMC_DEB_GREEN },
83 { VIMC_DEB_GREEN, VIMC_DEB_RED } }
84 },
85 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -040086 .pixelformat = V4L2_PIX_FMT_SGBRG8,
Helen Fornazier88f42bf2017-06-19 14:00:19 -030087 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
88 .order = { { VIMC_DEB_GREEN, VIMC_DEB_BLUE },
89 { VIMC_DEB_RED, VIMC_DEB_GREEN } }
90 },
91 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -040092 .pixelformat = V4L2_PIX_FMT_SGRBG8,
Helen Fornazier88f42bf2017-06-19 14:00:19 -030093 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
94 .order = { { VIMC_DEB_GREEN, VIMC_DEB_RED },
95 { VIMC_DEB_BLUE, VIMC_DEB_GREEN } }
96 },
97 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -040098 .pixelformat = V4L2_PIX_FMT_SRGGB8,
Helen Fornazier88f42bf2017-06-19 14:00:19 -030099 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
100 .order = { { VIMC_DEB_RED, VIMC_DEB_GREEN },
101 { VIMC_DEB_GREEN, VIMC_DEB_BLUE } }
102 },
103 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400104 .pixelformat = V4L2_PIX_FMT_SBGGR10,
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300105 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
106 .order = { { VIMC_DEB_BLUE, VIMC_DEB_GREEN },
107 { VIMC_DEB_GREEN, VIMC_DEB_RED } }
108 },
109 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400110 .pixelformat = V4L2_PIX_FMT_SGBRG10,
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300111 .code = MEDIA_BUS_FMT_SGBRG10_1X10,
112 .order = { { VIMC_DEB_GREEN, VIMC_DEB_BLUE },
113 { VIMC_DEB_RED, VIMC_DEB_GREEN } }
114 },
115 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400116 .pixelformat = V4L2_PIX_FMT_SGRBG10,
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300117 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
118 .order = { { VIMC_DEB_GREEN, VIMC_DEB_RED },
119 { VIMC_DEB_BLUE, VIMC_DEB_GREEN } }
120 },
121 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400122 .pixelformat = V4L2_PIX_FMT_SRGGB10,
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300123 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
124 .order = { { VIMC_DEB_RED, VIMC_DEB_GREEN },
125 { VIMC_DEB_GREEN, VIMC_DEB_BLUE } }
126 },
127 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400128 .pixelformat = V4L2_PIX_FMT_SBGGR12,
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300129 .code = MEDIA_BUS_FMT_SBGGR12_1X12,
130 .order = { { VIMC_DEB_BLUE, VIMC_DEB_GREEN },
131 { VIMC_DEB_GREEN, VIMC_DEB_RED } }
132 },
133 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400134 .pixelformat = V4L2_PIX_FMT_SGBRG12,
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300135 .code = MEDIA_BUS_FMT_SGBRG12_1X12,
136 .order = { { VIMC_DEB_GREEN, VIMC_DEB_BLUE },
137 { VIMC_DEB_RED, VIMC_DEB_GREEN } }
138 },
139 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400140 .pixelformat = V4L2_PIX_FMT_SGRBG12,
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300141 .code = MEDIA_BUS_FMT_SGRBG12_1X12,
142 .order = { { VIMC_DEB_GREEN, VIMC_DEB_RED },
143 { VIMC_DEB_BLUE, VIMC_DEB_GREEN } }
144 },
145 {
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400146 .pixelformat = V4L2_PIX_FMT_SRGGB12,
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300147 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
148 .order = { { VIMC_DEB_RED, VIMC_DEB_GREEN },
149 { VIMC_DEB_GREEN, VIMC_DEB_BLUE } }
150 },
151};
152
153static const struct vimc_deb_pix_map *vimc_deb_pix_map_by_code(u32 code)
154{
155 unsigned int i;
156
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];
160
161 return NULL;
162}
163
164static int vimc_deb_init_cfg(struct v4l2_subdev *sd,
165 struct v4l2_subdev_pad_config *cfg)
166{
167 struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
168 struct v4l2_mbus_framefmt *mf;
169 unsigned int i;
170
171 mf = v4l2_subdev_get_try_format(sd, cfg, 0);
172 *mf = sink_fmt_default;
173
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;
178 }
179
180 return 0;
181}
182
183static 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)
186{
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400187 /* For the sink pad we only support codes in the map_list */
188 if (IS_SINK(code->pad)) {
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300189 if (code->index >= ARRAY_SIZE(vimc_deb_pix_map_list))
190 return -EINVAL;
191
192 code->code = vimc_deb_pix_map_list[code->index].code;
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400193 return 0;
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300194 }
195
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400196 return vimc_enum_mbus_code(sd, cfg, code);
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300197}
198
199static 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)
202{
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300203 if (fse->index)
204 return -EINVAL;
205
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400206 /* For the sink pad we only support codes in the map_list */
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300207 if (IS_SINK(fse->pad)) {
208 const struct vimc_deb_pix_map *vpix =
209 vimc_deb_pix_map_by_code(fse->code);
210
211 if (!vpix)
212 return -EINVAL;
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300213 }
214
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;
219
220 return 0;
221}
222
223static int vimc_deb_get_fmt(struct v4l2_subdev *sd,
224 struct v4l2_subdev_pad_config *cfg,
225 struct v4l2_subdev_format *fmt)
226{
227 struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
228
229 /* Get the current sink format */
230 fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
231 *v4l2_subdev_get_try_format(sd, cfg, 0) :
232 vdeb->sink_fmt;
233
234 /* Set the right code for the source pad */
235 if (IS_SRC(fmt->pad))
236 fmt->format.code = vdeb->src_code;
237
238 return 0;
239}
240
241static void vimc_deb_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
242{
243 const struct vimc_deb_pix_map *vpix;
244
245 /* Don't accept a code that is not on the debayer table */
246 vpix = vimc_deb_pix_map_by_code(fmt->code);
247 if (!vpix)
248 fmt->code = sink_fmt_default.code;
249
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;
254
255 if (fmt->field == V4L2_FIELD_ANY)
256 fmt->field = sink_fmt_default.field;
257
258 vimc_colorimetry_clamp(fmt);
259}
260
261static int vimc_deb_set_fmt(struct v4l2_subdev *sd,
262 struct v4l2_subdev_pad_config *cfg,
263 struct v4l2_subdev_format *fmt)
264{
265 struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
266 struct v4l2_mbus_framefmt *sink_fmt;
267
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400268 if (!vimc_mbus_code_supported(fmt->format.code))
269 fmt->format.code = sink_fmt_default.code;
270
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300271 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
272 /* Do not change the format while stream is on */
273 if (vdeb->src_frame)
274 return -EBUSY;
275
276 sink_fmt = &vdeb->sink_fmt;
277 } else {
278 sink_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
279 }
280
281 /*
282 * Do not change the format of the source pad,
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400283 * it is propagated from the sink (except for the code)
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300284 */
285 if (IS_SRC(fmt->pad)) {
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400286 vdeb->src_code = fmt->format.code;
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300287 fmt->format = *sink_fmt;
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300288 fmt->format.code = vdeb->src_code;
289 } else {
290 /* Set the new format in the sink pad */
291 vimc_deb_adjust_sink_fmt(&fmt->format);
292
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,
296 /* old */
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,
300 /* new */
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);
304
305 *sink_fmt = fmt->format;
306 }
307
308 return 0;
309}
310
311static 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,
317};
318
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400319static void vimc_deb_set_rgb_pix_rgb24(struct vimc_deb_device *vdeb,
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300320 unsigned int lin,
321 unsigned int col,
322 unsigned int rgb[3])
323{
324 unsigned int i, index;
325
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];
329}
330
331static int vimc_deb_s_stream(struct v4l2_subdev *sd, int enable)
332{
333 struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300334
335 if (enable) {
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400336 u32 src_pixelformat = vdeb->ved.stream->producer_pixfmt;
337 const struct v4l2_format_info *pix_info;
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300338 unsigned int frame_size;
339
340 if (vdeb->src_frame)
341 return 0;
342
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400343 /* We only support translating bayer to RGB24 */
344 if (src_pixelformat != V4L2_PIX_FMT_RGB24) {
345 dev_err(vdeb->dev,
346 "translating to pixfmt (0x%08x) is not supported\n",
347 src_pixelformat);
348 return -EINVAL;
349 }
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300350
351 /* Get the corresponding pixel map from the table */
352 vdeb->sink_pix_map =
353 vimc_deb_pix_map_by_code(vdeb->sink_fmt.code);
354
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400355 /* Request bayer format from the pipeline for the sink pad */
356 vdeb->ved.stream->producer_pixfmt =
357 vdeb->sink_pix_map->pixelformat;
358
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 *
362 pix_info->bpp[0];
363
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];
367
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300368 /*
369 * Allocate the frame buffer. Use vmalloc to be able to
370 * allocate a large amount of memory
371 */
372 vdeb->src_frame = vmalloc(frame_size);
373 if (!vdeb->src_frame)
374 return -ENOMEM;
375
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300376 } else {
377 if (!vdeb->src_frame)
378 return 0;
379
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300380 vfree(vdeb->src_frame);
381 vdeb->src_frame = NULL;
382 }
383
384 return 0;
385}
386
Julia Lawallebf7a642017-08-08 06:58:28 -0400387static const struct v4l2_subdev_video_ops vimc_deb_video_ops = {
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300388 .s_stream = vimc_deb_s_stream,
389};
390
391static const struct v4l2_subdev_ops vimc_deb_ops = {
392 .pad = &vimc_deb_pad_ops,
393 .video = &vimc_deb_video_ops,
394};
395
396static unsigned int vimc_deb_get_val(const u8 *bytes,
397 const unsigned int n_bytes)
398{
399 unsigned int i;
400 unsigned int acc = 0;
401
402 for (i = 0; i < n_bytes; i++)
403 acc = acc + (bytes[i] << (8 * i));
404
405 return acc;
406}
407
408static void vimc_deb_calc_rgb_sink(struct vimc_deb_device *vdeb,
409 const u8 *frame,
410 const unsigned int lin,
411 const unsigned int col,
412 unsigned int rgb[3])
413{
414 unsigned int i, seek, wlin, wcol;
415 unsigned int n_rgb[3] = {0, 0, 0};
416
417 for (i = 0; i < 3; i++)
418 rgb[i] = 0;
419
420 /*
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)
424 */
425 seek = deb_mean_win_size / 2;
426
427 /* Sum the values of the colors in the mean window */
428
429 dev_dbg(vdeb->dev,
430 "deb: %s: --- Calc pixel %dx%d, window mean %d, seek %d ---\n",
431 vdeb->sd.name, lin, col, vdeb->sink_fmt.height, seek);
432
433 /*
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
437 * frame
438 */
439 for (wlin = seek > lin ? 0 : lin - seek;
440 wlin < lin + seek + 1 && wlin < vdeb->sink_fmt.height;
441 wlin++) {
442
443 /*
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
447 * frame
448 */
449 for (wcol = seek > col ? 0 : col - seek;
450 wcol < col + seek + 1 && wcol < vdeb->sink_fmt.width;
451 wcol++) {
452 enum vimc_deb_rgb_colors color;
453 unsigned int index;
454
455 /* Check which color this pixel is */
456 color = vdeb->sink_pix_map->order[wlin % 2][wcol % 2];
457
458 index = VIMC_FRAME_INDEX(wlin, wcol,
459 vdeb->sink_fmt.width,
460 vdeb->sink_bpp);
461
462 dev_dbg(vdeb->dev,
463 "deb: %s: RGB CALC: frame index %d, win pos %dx%d, color %d\n",
464 vdeb->sd.name, index, wlin, wcol, color);
465
466 /* Get its value */
467 rgb[color] = rgb[color] +
468 vimc_deb_get_val(&frame[index], vdeb->sink_bpp);
469
470 /* Save how many values we already added */
471 n_rgb[color]++;
472
473 dev_dbg(vdeb->dev, "deb: %s: RGB CALC: val %d, n %d\n",
474 vdeb->sd.name, rgb[color], n_rgb[color]);
475 }
476 }
477
478 /* Calculate the mean */
479 for (i = 0; i < 3; i++) {
480 dev_dbg(vdeb->dev,
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]);
483
484 if (n_rgb[i])
485 rgb[i] = rgb[i] / n_rgb[i];
486
487 dev_dbg(vdeb->dev,
488 "deb: %s: FINAL CALC: %dx%d Color %d, val %d\n",
489 vdeb->sd.name, lin, col, i, rgb[i]);
490 }
491}
492
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500493static void *vimc_deb_process_frame(struct vimc_ent_device *ved,
494 const void *sink_frame)
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300495{
496 struct vimc_deb_device *vdeb = container_of(ved, struct vimc_deb_device,
497 ved);
498 unsigned int rgb[3];
499 unsigned int i, j;
500
501 /* If the stream in this node is not active, just return */
502 if (!vdeb->src_frame)
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500503 return ERR_PTR(-EINVAL);
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300504
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);
509 }
510
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500511 return vdeb->src_frame;
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300512
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300513}
514
Hans Verkuil2b177f22019-03-05 03:36:20 -0500515static void vimc_deb_release(struct v4l2_subdev *sd)
516{
517 struct vimc_deb_device *vdeb =
518 container_of(sd, struct vimc_deb_device, sd);
519
520 kfree(vdeb);
521}
522
523static const struct v4l2_subdev_internal_ops vimc_deb_int_ops = {
524 .release = vimc_deb_release,
525};
526
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300527static void vimc_deb_comp_unbind(struct device *comp, struct device *master,
528 void *master_data)
529{
530 struct vimc_ent_device *ved = dev_get_drvdata(comp);
531 struct vimc_deb_device *vdeb = container_of(ved, struct vimc_deb_device,
532 ved);
533
534 vimc_ent_sd_unregister(ved, &vdeb->sd);
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300535}
536
537static int vimc_deb_comp_bind(struct device *comp, struct device *master,
538 void *master_data)
539{
540 struct v4l2_device *v4l2_dev = master_data;
541 struct vimc_platform_data *pdata = comp->platform_data;
542 struct vimc_deb_device *vdeb;
543 int ret;
544
545 /* Allocate the vdeb struct */
546 vdeb = kzalloc(sizeof(*vdeb), GFP_KERNEL);
547 if (!vdeb)
548 return -ENOMEM;
549
550 /* Initialize ved and sd */
551 ret = vimc_ent_sd_register(&vdeb->ved, &vdeb->sd, v4l2_dev,
552 pdata->entity_name,
Hans Verkuil1ceda322018-02-07 12:06:30 -0500553 MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV, 2,
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300554 (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
555 MEDIA_PAD_FL_SOURCE},
Hans Verkuil2b177f22019-03-05 03:36:20 -0500556 &vimc_deb_int_ops, &vimc_deb_ops);
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300557 if (ret) {
558 kfree(vdeb);
559 return ret;
560 }
561
562 vdeb->ved.process_frame = vimc_deb_process_frame;
563 dev_set_drvdata(comp, &vdeb->ved);
564 vdeb->dev = comp;
565
566 /* Initialize the frame format */
567 vdeb->sink_fmt = sink_fmt_default;
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400568 vdeb->src_code = VIMC_DEB_SRC_MBUS_FMT_DEFAULT;
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300569 /*
570 * TODO: Add support for more output formats, we only support
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400571 * RGB24 for now.
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300572 * NOTE: the src format is always the same as the sink, except
573 * for the code
574 */
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400575 vdeb->set_rgb_src = vimc_deb_set_rgb_pix_rgb24;
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300576
577 return 0;
578}
579
580static const struct component_ops vimc_deb_comp_ops = {
581 .bind = vimc_deb_comp_bind,
582 .unbind = vimc_deb_comp_unbind,
583};
584
585static int vimc_deb_probe(struct platform_device *pdev)
586{
587 return component_add(&pdev->dev, &vimc_deb_comp_ops);
588}
589
590static int vimc_deb_remove(struct platform_device *pdev)
591{
592 component_del(&pdev->dev, &vimc_deb_comp_ops);
593
594 return 0;
595}
596
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300597static const struct platform_device_id vimc_deb_driver_ids[] = {
598 {
599 .name = VIMC_DEB_DRV_NAME,
600 },
601 { }
602};
603
Javier Martinez Canillasbb3abbb2017-07-14 05:58:39 -0300604static struct platform_driver vimc_deb_pdrv = {
605 .probe = vimc_deb_probe,
606 .remove = vimc_deb_remove,
607 .id_table = vimc_deb_driver_ids,
608 .driver = {
609 .name = VIMC_DEB_DRV_NAME,
610 },
611};
612
Helen Fornazier88f42bf2017-06-19 14:00:19 -0300613module_platform_driver(vimc_deb_pdrv);
614
615MODULE_DEVICE_TABLE(platform, vimc_deb_driver_ids);
616
617MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Debayer");
618MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
619MODULE_LICENSE("GPL");