1dacca5f0SHans Verkuil // SPDX-License-Identifier: GPL-2.0-or-later
2dacca5f0SHans Verkuil /*
3dacca5f0SHans Verkuil * vimc-debayer.c Virtual Media Controller Driver
4dacca5f0SHans Verkuil *
5dacca5f0SHans Verkuil * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
6dacca5f0SHans Verkuil */
7dacca5f0SHans Verkuil
8dacca5f0SHans Verkuil #include <linux/moduleparam.h>
9dacca5f0SHans Verkuil #include <linux/platform_device.h>
10dacca5f0SHans Verkuil #include <linux/vmalloc.h>
11dacca5f0SHans Verkuil #include <linux/v4l2-mediabus.h>
12dacca5f0SHans Verkuil #include <media/v4l2-ctrls.h>
13dacca5f0SHans Verkuil #include <media/v4l2-event.h>
14dacca5f0SHans Verkuil #include <media/v4l2-subdev.h>
15dacca5f0SHans Verkuil
16dacca5f0SHans Verkuil #include "vimc-common.h"
17dacca5f0SHans Verkuil
18*4c46cb2aSLaurent Pinchart /* TODO: Add support for more output formats, we only support RGB888 for now. */
19*4c46cb2aSLaurent Pinchart #define VIMC_DEBAYER_SOURCE_MBUS_FMT MEDIA_BUS_FMT_RGB888_1X24
20*4c46cb2aSLaurent Pinchart
21ec917d77SDaniel Oakley enum vimc_debayer_rgb_colors {
22ec917d77SDaniel Oakley VIMC_DEBAYER_RED = 0,
23ec917d77SDaniel Oakley VIMC_DEBAYER_GREEN = 1,
24ec917d77SDaniel Oakley VIMC_DEBAYER_BLUE = 2,
25dacca5f0SHans Verkuil };
26dacca5f0SHans Verkuil
27ec917d77SDaniel Oakley struct vimc_debayer_pix_map {
28dacca5f0SHans Verkuil u32 code;
29ec917d77SDaniel Oakley enum vimc_debayer_rgb_colors order[2][2];
30dacca5f0SHans Verkuil };
31dacca5f0SHans Verkuil
32ec917d77SDaniel Oakley struct vimc_debayer_device {
33dacca5f0SHans Verkuil struct vimc_ent_device ved;
34dacca5f0SHans Verkuil struct v4l2_subdev sd;
35*4c46cb2aSLaurent Pinchart struct v4l2_ctrl_handler hdl;
36*4c46cb2aSLaurent Pinchart struct media_pad pads[2];
37*4c46cb2aSLaurent Pinchart
38*4c46cb2aSLaurent Pinchart u8 *src_frame;
39*4c46cb2aSLaurent Pinchart
40ec917d77SDaniel Oakley void (*set_rgb_src)(struct vimc_debayer_device *vdebayer,
41ec917d77SDaniel Oakley unsigned int lin, unsigned int col,
42ec917d77SDaniel Oakley unsigned int rgb[3]);
43*4c46cb2aSLaurent Pinchart
44*4c46cb2aSLaurent Pinchart /*
45*4c46cb2aSLaurent Pinchart * Virtual "hardware" configuration, filled when the stream starts or
46*4c46cb2aSLaurent Pinchart * when controls are set.
47*4c46cb2aSLaurent Pinchart */
48*4c46cb2aSLaurent Pinchart struct {
49ec917d77SDaniel Oakley const struct vimc_debayer_pix_map *sink_pix_map;
50dacca5f0SHans Verkuil unsigned int sink_bpp;
51*4c46cb2aSLaurent Pinchart struct v4l2_area size;
52dacca5f0SHans Verkuil unsigned int mean_win_size;
53*4c46cb2aSLaurent Pinchart u32 src_code;
54*4c46cb2aSLaurent Pinchart } hw;
55dacca5f0SHans Verkuil };
56dacca5f0SHans Verkuil
57dacca5f0SHans Verkuil static const struct v4l2_mbus_framefmt sink_fmt_default = {
58dacca5f0SHans Verkuil .width = 640,
59dacca5f0SHans Verkuil .height = 480,
60dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SRGGB8_1X8,
61dacca5f0SHans Verkuil .field = V4L2_FIELD_NONE,
629a42a5ffSNiklas Söderlund .colorspace = V4L2_COLORSPACE_SRGB,
63dacca5f0SHans Verkuil };
64dacca5f0SHans Verkuil
65ec917d77SDaniel Oakley static const u32 vimc_debayer_src_mbus_codes[] = {
66f4d12d80SNícolas F. R. A. Prado MEDIA_BUS_FMT_GBR888_1X24,
67f4d12d80SNícolas F. R. A. Prado MEDIA_BUS_FMT_BGR888_1X24,
68f4d12d80SNícolas F. R. A. Prado MEDIA_BUS_FMT_BGR888_3X8,
69f4d12d80SNícolas F. R. A. Prado MEDIA_BUS_FMT_RGB888_1X24,
70f4d12d80SNícolas F. R. A. Prado MEDIA_BUS_FMT_RGB888_2X12_BE,
71f4d12d80SNícolas F. R. A. Prado MEDIA_BUS_FMT_RGB888_2X12_LE,
72f4d12d80SNícolas F. R. A. Prado MEDIA_BUS_FMT_RGB888_3X8,
73f4d12d80SNícolas F. R. A. Prado MEDIA_BUS_FMT_RGB888_1X7X4_SPWG,
74f4d12d80SNícolas F. R. A. Prado MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA,
75f4d12d80SNícolas F. R. A. Prado MEDIA_BUS_FMT_RGB888_1X32_PADHI,
76f4d12d80SNícolas F. R. A. Prado };
77f4d12d80SNícolas F. R. A. Prado
78ec917d77SDaniel Oakley static const struct vimc_debayer_pix_map vimc_debayer_pix_map_list[] = {
79dacca5f0SHans Verkuil {
80dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SBGGR8_1X8,
81ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN },
82ec917d77SDaniel Oakley { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED } }
83dacca5f0SHans Verkuil },
84dacca5f0SHans Verkuil {
85dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SGBRG8_1X8,
86ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE },
87ec917d77SDaniel Oakley { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN } }
88dacca5f0SHans Verkuil },
89dacca5f0SHans Verkuil {
90dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SGRBG8_1X8,
91ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED },
92ec917d77SDaniel Oakley { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN } }
93dacca5f0SHans Verkuil },
94dacca5f0SHans Verkuil {
95dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SRGGB8_1X8,
96ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN },
97ec917d77SDaniel Oakley { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE } }
98dacca5f0SHans Verkuil },
99dacca5f0SHans Verkuil {
100dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SBGGR10_1X10,
101ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN },
102ec917d77SDaniel Oakley { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED } }
103dacca5f0SHans Verkuil },
104dacca5f0SHans Verkuil {
105dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SGBRG10_1X10,
106ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE },
107ec917d77SDaniel Oakley { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN } }
108dacca5f0SHans Verkuil },
109dacca5f0SHans Verkuil {
110dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SGRBG10_1X10,
111ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED },
112ec917d77SDaniel Oakley { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN } }
113dacca5f0SHans Verkuil },
114dacca5f0SHans Verkuil {
115dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SRGGB10_1X10,
116ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN },
117ec917d77SDaniel Oakley { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE } }
118dacca5f0SHans Verkuil },
119dacca5f0SHans Verkuil {
120dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SBGGR12_1X12,
121ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN },
122ec917d77SDaniel Oakley { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED } }
123dacca5f0SHans Verkuil },
124dacca5f0SHans Verkuil {
125dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SGBRG12_1X12,
126ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE },
127ec917d77SDaniel Oakley { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN } }
128dacca5f0SHans Verkuil },
129dacca5f0SHans Verkuil {
130dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SGRBG12_1X12,
131ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED },
132ec917d77SDaniel Oakley { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN } }
133dacca5f0SHans Verkuil },
134dacca5f0SHans Verkuil {
135dacca5f0SHans Verkuil .code = MEDIA_BUS_FMT_SRGGB12_1X12,
136ec917d77SDaniel Oakley .order = { { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN },
137ec917d77SDaniel Oakley { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE } }
138dacca5f0SHans Verkuil },
139dacca5f0SHans Verkuil };
140dacca5f0SHans Verkuil
vimc_debayer_pix_map_by_code(u32 code)141ec917d77SDaniel Oakley static const struct vimc_debayer_pix_map *vimc_debayer_pix_map_by_code(u32 code)
142dacca5f0SHans Verkuil {
143dacca5f0SHans Verkuil unsigned int i;
144dacca5f0SHans Verkuil
145ec917d77SDaniel Oakley for (i = 0; i < ARRAY_SIZE(vimc_debayer_pix_map_list); i++)
146ec917d77SDaniel Oakley if (vimc_debayer_pix_map_list[i].code == code)
147ec917d77SDaniel Oakley return &vimc_debayer_pix_map_list[i];
148dacca5f0SHans Verkuil
149dacca5f0SHans Verkuil return NULL;
150dacca5f0SHans Verkuil }
151dacca5f0SHans Verkuil
vimc_debayer_src_code_is_valid(u32 code)152ec917d77SDaniel Oakley static bool vimc_debayer_src_code_is_valid(u32 code)
153f4d12d80SNícolas F. R. A. Prado {
154f4d12d80SNícolas F. R. A. Prado unsigned int i;
155f4d12d80SNícolas F. R. A. Prado
156ec917d77SDaniel Oakley for (i = 0; i < ARRAY_SIZE(vimc_debayer_src_mbus_codes); i++)
157ec917d77SDaniel Oakley if (vimc_debayer_src_mbus_codes[i] == code)
158f4d12d80SNícolas F. R. A. Prado return true;
159f4d12d80SNícolas F. R. A. Prado
160f4d12d80SNícolas F. R. A. Prado return false;
161f4d12d80SNícolas F. R. A. Prado }
162f4d12d80SNícolas F. R. A. Prado
vimc_debayer_init_state(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state)1635755be5fSLaurent Pinchart static int vimc_debayer_init_state(struct v4l2_subdev *sd,
1640d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state)
165dacca5f0SHans Verkuil {
166dacca5f0SHans Verkuil struct v4l2_mbus_framefmt *mf;
167dacca5f0SHans Verkuil
168bc0e8d91SSakari Ailus mf = v4l2_subdev_state_get_format(sd_state, 0);
169dacca5f0SHans Verkuil *mf = sink_fmt_default;
170dacca5f0SHans Verkuil
1712ef9a1e7SLaurent Pinchart mf = v4l2_subdev_state_get_format(sd_state, 1);
172dacca5f0SHans Verkuil *mf = sink_fmt_default;
173*4c46cb2aSLaurent Pinchart mf->code = VIMC_DEBAYER_SOURCE_MBUS_FMT;
174dacca5f0SHans Verkuil
175dacca5f0SHans Verkuil return 0;
176dacca5f0SHans Verkuil }
177dacca5f0SHans Verkuil
vimc_debayer_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)178ec917d77SDaniel Oakley static int vimc_debayer_enum_mbus_code(struct v4l2_subdev *sd,
1790d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
180dacca5f0SHans Verkuil struct v4l2_subdev_mbus_code_enum *code)
181dacca5f0SHans Verkuil {
182dacca5f0SHans Verkuil if (VIMC_IS_SRC(code->pad)) {
183ec917d77SDaniel Oakley if (code->index >= ARRAY_SIZE(vimc_debayer_src_mbus_codes))
184dacca5f0SHans Verkuil return -EINVAL;
185dacca5f0SHans Verkuil
186ec917d77SDaniel Oakley code->code = vimc_debayer_src_mbus_codes[code->index];
187dacca5f0SHans Verkuil } else {
188ec917d77SDaniel Oakley if (code->index >= ARRAY_SIZE(vimc_debayer_pix_map_list))
189dacca5f0SHans Verkuil return -EINVAL;
190dacca5f0SHans Verkuil
191ec917d77SDaniel Oakley code->code = vimc_debayer_pix_map_list[code->index].code;
192dacca5f0SHans Verkuil }
193dacca5f0SHans Verkuil
194dacca5f0SHans Verkuil return 0;
195dacca5f0SHans Verkuil }
196dacca5f0SHans Verkuil
vimc_debayer_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)197ec917d77SDaniel Oakley static int vimc_debayer_enum_frame_size(struct v4l2_subdev *sd,
1980d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
199dacca5f0SHans Verkuil struct v4l2_subdev_frame_size_enum *fse)
200dacca5f0SHans Verkuil {
201dacca5f0SHans Verkuil if (fse->index)
202dacca5f0SHans Verkuil return -EINVAL;
203dacca5f0SHans Verkuil
204dacca5f0SHans Verkuil if (VIMC_IS_SINK(fse->pad)) {
205ec917d77SDaniel Oakley const struct vimc_debayer_pix_map *vpix =
206ec917d77SDaniel Oakley vimc_debayer_pix_map_by_code(fse->code);
207dacca5f0SHans Verkuil
208dacca5f0SHans Verkuil if (!vpix)
209dacca5f0SHans Verkuil return -EINVAL;
210ec917d77SDaniel Oakley } else if (!vimc_debayer_src_code_is_valid(fse->code)) {
211dacca5f0SHans Verkuil return -EINVAL;
212dacca5f0SHans Verkuil }
213dacca5f0SHans Verkuil
214dacca5f0SHans Verkuil fse->min_width = VIMC_FRAME_MIN_WIDTH;
215dacca5f0SHans Verkuil fse->max_width = VIMC_FRAME_MAX_WIDTH;
216dacca5f0SHans Verkuil fse->min_height = VIMC_FRAME_MIN_HEIGHT;
217dacca5f0SHans Verkuil fse->max_height = VIMC_FRAME_MAX_HEIGHT;
218dacca5f0SHans Verkuil
219dacca5f0SHans Verkuil return 0;
220dacca5f0SHans Verkuil }
221dacca5f0SHans Verkuil
vimc_debayer_adjust_sink_fmt(struct v4l2_mbus_framefmt * fmt)222ec917d77SDaniel Oakley static void vimc_debayer_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
223dacca5f0SHans Verkuil {
224ec917d77SDaniel Oakley const struct vimc_debayer_pix_map *vpix;
225dacca5f0SHans Verkuil
226dacca5f0SHans Verkuil /* Don't accept a code that is not on the debayer table */
227ec917d77SDaniel Oakley vpix = vimc_debayer_pix_map_by_code(fmt->code);
228dacca5f0SHans Verkuil if (!vpix)
229dacca5f0SHans Verkuil fmt->code = sink_fmt_default.code;
230dacca5f0SHans Verkuil
231dacca5f0SHans Verkuil fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
232dacca5f0SHans Verkuil VIMC_FRAME_MAX_WIDTH) & ~1;
233dacca5f0SHans Verkuil fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
234dacca5f0SHans Verkuil VIMC_FRAME_MAX_HEIGHT) & ~1;
235dacca5f0SHans Verkuil
236dacca5f0SHans Verkuil if (fmt->field == V4L2_FIELD_ANY)
237dacca5f0SHans Verkuil fmt->field = sink_fmt_default.field;
238dacca5f0SHans Verkuil
239dacca5f0SHans Verkuil vimc_colorimetry_clamp(fmt);
240dacca5f0SHans Verkuil }
241dacca5f0SHans Verkuil
vimc_debayer_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)242ec917d77SDaniel Oakley static int vimc_debayer_set_fmt(struct v4l2_subdev *sd,
2430d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
244dacca5f0SHans Verkuil struct v4l2_subdev_format *fmt)
245dacca5f0SHans Verkuil {
246ec917d77SDaniel Oakley struct vimc_debayer_device *vdebayer = v4l2_get_subdevdata(sd);
247*4c46cb2aSLaurent Pinchart struct v4l2_mbus_framefmt *format;
248dacca5f0SHans Verkuil
249*4c46cb2aSLaurent Pinchart /* Do not change the format while stream is on. */
250*4c46cb2aSLaurent Pinchart if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE && vdebayer->src_frame)
251dacca5f0SHans Verkuil return -EBUSY;
252dacca5f0SHans Verkuil
253dacca5f0SHans Verkuil /*
254*4c46cb2aSLaurent Pinchart * Do not change the format of the source pad, it is propagated from
255*4c46cb2aSLaurent Pinchart * the sink.
256dacca5f0SHans Verkuil */
257*4c46cb2aSLaurent Pinchart if (VIMC_IS_SRC(fmt->pad))
258*4c46cb2aSLaurent Pinchart return v4l2_subdev_get_fmt(sd, sd_state, fmt);
259f4d12d80SNícolas F. R. A. Prado
260*4c46cb2aSLaurent Pinchart /* Set the new format in the sink pad. */
261ec917d77SDaniel Oakley vimc_debayer_adjust_sink_fmt(&fmt->format);
262dacca5f0SHans Verkuil
263*4c46cb2aSLaurent Pinchart format = v4l2_subdev_state_get_format(sd_state, 0);
264*4c46cb2aSLaurent Pinchart
265ec917d77SDaniel Oakley dev_dbg(vdebayer->ved.dev, "%s: sink format update: "
266dacca5f0SHans Verkuil "old:%dx%d (0x%x, %d, %d, %d, %d) "
267ec917d77SDaniel Oakley "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vdebayer->sd.name,
268dacca5f0SHans Verkuil /* old */
269*4c46cb2aSLaurent Pinchart format->width, format->height, format->code,
270*4c46cb2aSLaurent Pinchart format->colorspace, format->quantization,
271*4c46cb2aSLaurent Pinchart format->xfer_func, format->ycbcr_enc,
272dacca5f0SHans Verkuil /* new */
273dacca5f0SHans Verkuil fmt->format.width, fmt->format.height, fmt->format.code,
274dacca5f0SHans Verkuil fmt->format.colorspace, fmt->format.quantization,
275dacca5f0SHans Verkuil fmt->format.xfer_func, fmt->format.ycbcr_enc);
276dacca5f0SHans Verkuil
277*4c46cb2aSLaurent Pinchart *format = fmt->format;
278*4c46cb2aSLaurent Pinchart
279*4c46cb2aSLaurent Pinchart /* Propagate the format to the source pad. */
280*4c46cb2aSLaurent Pinchart format = v4l2_subdev_state_get_format(sd_state, 1);
281*4c46cb2aSLaurent Pinchart *format = fmt->format;
282*4c46cb2aSLaurent Pinchart format->code = VIMC_DEBAYER_SOURCE_MBUS_FMT;
283dacca5f0SHans Verkuil
284dacca5f0SHans Verkuil return 0;
285dacca5f0SHans Verkuil }
286dacca5f0SHans Verkuil
287ec917d77SDaniel Oakley static const struct v4l2_subdev_pad_ops vimc_debayer_pad_ops = {
288ec917d77SDaniel Oakley .enum_mbus_code = vimc_debayer_enum_mbus_code,
289ec917d77SDaniel Oakley .enum_frame_size = vimc_debayer_enum_frame_size,
290*4c46cb2aSLaurent Pinchart .get_fmt = v4l2_subdev_get_fmt,
291ec917d77SDaniel Oakley .set_fmt = vimc_debayer_set_fmt,
292dacca5f0SHans Verkuil };
293dacca5f0SHans Verkuil
vimc_debayer_process_rgb_frame(struct vimc_debayer_device * vdebayer,unsigned int lin,unsigned int col,unsigned int rgb[3])294ec917d77SDaniel Oakley static void vimc_debayer_process_rgb_frame(struct vimc_debayer_device *vdebayer,
295dacca5f0SHans Verkuil unsigned int lin,
296dacca5f0SHans Verkuil unsigned int col,
297dacca5f0SHans Verkuil unsigned int rgb[3])
298dacca5f0SHans Verkuil {
299f4d12d80SNícolas F. R. A. Prado const struct vimc_pix_map *vpix;
300dacca5f0SHans Verkuil unsigned int i, index;
301dacca5f0SHans Verkuil
302*4c46cb2aSLaurent Pinchart vpix = vimc_pix_map_by_code(vdebayer->hw.src_code);
303*4c46cb2aSLaurent Pinchart index = VIMC_FRAME_INDEX(lin, col, vdebayer->hw.size.width, 3);
304f4d12d80SNícolas F. R. A. Prado for (i = 0; i < 3; i++) {
305f4d12d80SNícolas F. R. A. Prado switch (vpix->pixelformat) {
306f4d12d80SNícolas F. R. A. Prado case V4L2_PIX_FMT_RGB24:
307ec917d77SDaniel Oakley vdebayer->src_frame[index + i] = rgb[i];
308f4d12d80SNícolas F. R. A. Prado break;
309f4d12d80SNícolas F. R. A. Prado case V4L2_PIX_FMT_BGR24:
310ec917d77SDaniel Oakley vdebayer->src_frame[index + i] = rgb[2 - i];
311f4d12d80SNícolas F. R. A. Prado break;
312f4d12d80SNícolas F. R. A. Prado }
313f4d12d80SNícolas F. R. A. Prado }
314dacca5f0SHans Verkuil }
315dacca5f0SHans Verkuil
vimc_debayer_s_stream(struct v4l2_subdev * sd,int enable)316ec917d77SDaniel Oakley static int vimc_debayer_s_stream(struct v4l2_subdev *sd, int enable)
317dacca5f0SHans Verkuil {
318ec917d77SDaniel Oakley struct vimc_debayer_device *vdebayer = v4l2_get_subdevdata(sd);
319dacca5f0SHans Verkuil
320dacca5f0SHans Verkuil if (enable) {
321*4c46cb2aSLaurent Pinchart const struct v4l2_mbus_framefmt *sink_fmt;
322*4c46cb2aSLaurent Pinchart const struct v4l2_mbus_framefmt *src_fmt;
323*4c46cb2aSLaurent Pinchart struct v4l2_subdev_state *state;
324dacca5f0SHans Verkuil const struct vimc_pix_map *vpix;
325dacca5f0SHans Verkuil unsigned int frame_size;
326dacca5f0SHans Verkuil
327ec917d77SDaniel Oakley if (vdebayer->src_frame)
328dacca5f0SHans Verkuil return 0;
329dacca5f0SHans Verkuil
330*4c46cb2aSLaurent Pinchart state = v4l2_subdev_lock_and_get_active_state(sd);
331*4c46cb2aSLaurent Pinchart sink_fmt = v4l2_subdev_state_get_format(state, 0);
332*4c46cb2aSLaurent Pinchart src_fmt = v4l2_subdev_state_get_format(state, 1);
333*4c46cb2aSLaurent Pinchart
334dacca5f0SHans Verkuil /* Calculate the frame size of the source pad */
335*4c46cb2aSLaurent Pinchart vpix = vimc_pix_map_by_code(src_fmt->code);
336*4c46cb2aSLaurent Pinchart frame_size = src_fmt->width * src_fmt->height * vpix->bpp;
337dacca5f0SHans Verkuil
338dacca5f0SHans Verkuil /* Save the bytes per pixel of the sink */
339*4c46cb2aSLaurent Pinchart vpix = vimc_pix_map_by_code(sink_fmt->code);
340*4c46cb2aSLaurent Pinchart vdebayer->hw.sink_bpp = vpix->bpp;
341dacca5f0SHans Verkuil
342dacca5f0SHans Verkuil /* Get the corresponding pixel map from the table */
343*4c46cb2aSLaurent Pinchart vdebayer->hw.sink_pix_map =
344*4c46cb2aSLaurent Pinchart vimc_debayer_pix_map_by_code(sink_fmt->code);
345*4c46cb2aSLaurent Pinchart
346*4c46cb2aSLaurent Pinchart vdebayer->hw.size.width = sink_fmt->width;
347*4c46cb2aSLaurent Pinchart vdebayer->hw.size.height = sink_fmt->height;
348*4c46cb2aSLaurent Pinchart
349*4c46cb2aSLaurent Pinchart vdebayer->hw.src_code = src_fmt->code;
350*4c46cb2aSLaurent Pinchart
351*4c46cb2aSLaurent Pinchart v4l2_subdev_unlock_state(state);
352dacca5f0SHans Verkuil
353dacca5f0SHans Verkuil /*
354dacca5f0SHans Verkuil * Allocate the frame buffer. Use vmalloc to be able to
355dacca5f0SHans Verkuil * allocate a large amount of memory
356dacca5f0SHans Verkuil */
357ec917d77SDaniel Oakley vdebayer->src_frame = vmalloc(frame_size);
358ec917d77SDaniel Oakley if (!vdebayer->src_frame)
359dacca5f0SHans Verkuil return -ENOMEM;
360dacca5f0SHans Verkuil } else {
361ec917d77SDaniel Oakley if (!vdebayer->src_frame)
362dacca5f0SHans Verkuil return 0;
363dacca5f0SHans Verkuil
364ec917d77SDaniel Oakley vfree(vdebayer->src_frame);
365ec917d77SDaniel Oakley vdebayer->src_frame = NULL;
366dacca5f0SHans Verkuil }
367dacca5f0SHans Verkuil
368dacca5f0SHans Verkuil return 0;
369dacca5f0SHans Verkuil }
370dacca5f0SHans Verkuil
371ec917d77SDaniel Oakley static const struct v4l2_subdev_core_ops vimc_debayer_core_ops = {
372dacca5f0SHans Verkuil .log_status = v4l2_ctrl_subdev_log_status,
373dacca5f0SHans Verkuil .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
374dacca5f0SHans Verkuil .unsubscribe_event = v4l2_event_subdev_unsubscribe,
375dacca5f0SHans Verkuil };
376dacca5f0SHans Verkuil
377ec917d77SDaniel Oakley static const struct v4l2_subdev_video_ops vimc_debayer_video_ops = {
378ec917d77SDaniel Oakley .s_stream = vimc_debayer_s_stream,
379dacca5f0SHans Verkuil };
380dacca5f0SHans Verkuil
381ec917d77SDaniel Oakley static const struct v4l2_subdev_ops vimc_debayer_ops = {
382ec917d77SDaniel Oakley .core = &vimc_debayer_core_ops,
383ec917d77SDaniel Oakley .pad = &vimc_debayer_pad_ops,
384ec917d77SDaniel Oakley .video = &vimc_debayer_video_ops,
385dacca5f0SHans Verkuil };
386dacca5f0SHans Verkuil
3875755be5fSLaurent Pinchart static const struct v4l2_subdev_internal_ops vimc_debayer_internal_ops = {
3885755be5fSLaurent Pinchart .init_state = vimc_debayer_init_state,
3895755be5fSLaurent Pinchart };
3905755be5fSLaurent Pinchart
vimc_debayer_get_val(const u8 * bytes,const unsigned int n_bytes)391ec917d77SDaniel Oakley static unsigned int vimc_debayer_get_val(const u8 *bytes,
392dacca5f0SHans Verkuil const unsigned int n_bytes)
393dacca5f0SHans Verkuil {
394dacca5f0SHans Verkuil unsigned int i;
395dacca5f0SHans Verkuil unsigned int acc = 0;
396dacca5f0SHans Verkuil
397dacca5f0SHans Verkuil for (i = 0; i < n_bytes; i++)
398dacca5f0SHans Verkuil acc = acc + (bytes[i] << (8 * i));
399dacca5f0SHans Verkuil
400dacca5f0SHans Verkuil return acc;
401dacca5f0SHans Verkuil }
402dacca5f0SHans Verkuil
vimc_debayer_calc_rgb_sink(struct vimc_debayer_device * vdebayer,const u8 * frame,const unsigned int lin,const unsigned int col,unsigned int rgb[3])403ec917d77SDaniel Oakley static void vimc_debayer_calc_rgb_sink(struct vimc_debayer_device *vdebayer,
404dacca5f0SHans Verkuil const u8 *frame,
405dacca5f0SHans Verkuil const unsigned int lin,
406dacca5f0SHans Verkuil const unsigned int col,
407dacca5f0SHans Verkuil unsigned int rgb[3])
408dacca5f0SHans Verkuil {
409dacca5f0SHans Verkuil unsigned int i, seek, wlin, wcol;
410dacca5f0SHans Verkuil unsigned int n_rgb[3] = {0, 0, 0};
411dacca5f0SHans Verkuil
412dacca5f0SHans Verkuil for (i = 0; i < 3; i++)
413dacca5f0SHans Verkuil rgb[i] = 0;
414dacca5f0SHans Verkuil
415dacca5f0SHans Verkuil /*
416dacca5f0SHans Verkuil * Calculate how many we need to subtract to get to the pixel in
417dacca5f0SHans Verkuil * the top left corner of the mean window (considering the current
418dacca5f0SHans Verkuil * pixel as the center)
419dacca5f0SHans Verkuil */
420*4c46cb2aSLaurent Pinchart seek = vdebayer->hw.mean_win_size / 2;
421dacca5f0SHans Verkuil
422dacca5f0SHans Verkuil /* Sum the values of the colors in the mean window */
423dacca5f0SHans Verkuil
424ec917d77SDaniel Oakley dev_dbg(vdebayer->ved.dev,
425dacca5f0SHans Verkuil "deb: %s: --- Calc pixel %dx%d, window mean %d, seek %d ---\n",
426*4c46cb2aSLaurent Pinchart vdebayer->sd.name, lin, col, vdebayer->hw.size.height, seek);
427dacca5f0SHans Verkuil
428dacca5f0SHans Verkuil /*
429dacca5f0SHans Verkuil * Iterate through all the lines in the mean window, start
430dacca5f0SHans Verkuil * with zero if the pixel is outside the frame and don't pass
431dacca5f0SHans Verkuil * the height when the pixel is in the bottom border of the
432dacca5f0SHans Verkuil * frame
433dacca5f0SHans Verkuil */
434dacca5f0SHans Verkuil for (wlin = seek > lin ? 0 : lin - seek;
435*4c46cb2aSLaurent Pinchart wlin < lin + seek + 1 && wlin < vdebayer->hw.size.height;
436dacca5f0SHans Verkuil wlin++) {
437dacca5f0SHans Verkuil
438dacca5f0SHans Verkuil /*
439dacca5f0SHans Verkuil * Iterate through all the columns in the mean window, start
440dacca5f0SHans Verkuil * with zero if the pixel is outside the frame and don't pass
441dacca5f0SHans Verkuil * the width when the pixel is in the right border of the
442dacca5f0SHans Verkuil * frame
443dacca5f0SHans Verkuil */
444dacca5f0SHans Verkuil for (wcol = seek > col ? 0 : col - seek;
445*4c46cb2aSLaurent Pinchart wcol < col + seek + 1 && wcol < vdebayer->hw.size.width;
446dacca5f0SHans Verkuil wcol++) {
447ec917d77SDaniel Oakley enum vimc_debayer_rgb_colors color;
448dacca5f0SHans Verkuil unsigned int index;
449dacca5f0SHans Verkuil
450dacca5f0SHans Verkuil /* Check which color this pixel is */
451*4c46cb2aSLaurent Pinchart color = vdebayer->hw.sink_pix_map->order[wlin % 2][wcol % 2];
452dacca5f0SHans Verkuil
453dacca5f0SHans Verkuil index = VIMC_FRAME_INDEX(wlin, wcol,
454*4c46cb2aSLaurent Pinchart vdebayer->hw.size.width,
455*4c46cb2aSLaurent Pinchart vdebayer->hw.sink_bpp);
456dacca5f0SHans Verkuil
457ec917d77SDaniel Oakley dev_dbg(vdebayer->ved.dev,
458dacca5f0SHans Verkuil "deb: %s: RGB CALC: frame index %d, win pos %dx%d, color %d\n",
459ec917d77SDaniel Oakley vdebayer->sd.name, index, wlin, wcol, color);
460dacca5f0SHans Verkuil
461dacca5f0SHans Verkuil /* Get its value */
462dacca5f0SHans Verkuil rgb[color] = rgb[color] +
463ec917d77SDaniel Oakley vimc_debayer_get_val(&frame[index],
464*4c46cb2aSLaurent Pinchart vdebayer->hw.sink_bpp);
465dacca5f0SHans Verkuil
466dacca5f0SHans Verkuil /* Save how many values we already added */
467dacca5f0SHans Verkuil n_rgb[color]++;
468dacca5f0SHans Verkuil
469ec917d77SDaniel Oakley dev_dbg(vdebayer->ved.dev, "deb: %s: RGB CALC: val %d, n %d\n",
470ec917d77SDaniel Oakley vdebayer->sd.name, rgb[color], n_rgb[color]);
471dacca5f0SHans Verkuil }
472dacca5f0SHans Verkuil }
473dacca5f0SHans Verkuil
474dacca5f0SHans Verkuil /* Calculate the mean */
475dacca5f0SHans Verkuil for (i = 0; i < 3; i++) {
476ec917d77SDaniel Oakley dev_dbg(vdebayer->ved.dev,
477dacca5f0SHans Verkuil "deb: %s: PRE CALC: %dx%d Color %d, val %d, n %d\n",
478ec917d77SDaniel Oakley vdebayer->sd.name, lin, col, i, rgb[i], n_rgb[i]);
479dacca5f0SHans Verkuil
480dacca5f0SHans Verkuil if (n_rgb[i])
481dacca5f0SHans Verkuil rgb[i] = rgb[i] / n_rgb[i];
482dacca5f0SHans Verkuil
483ec917d77SDaniel Oakley dev_dbg(vdebayer->ved.dev,
484dacca5f0SHans Verkuil "deb: %s: FINAL CALC: %dx%d Color %d, val %d\n",
485ec917d77SDaniel Oakley vdebayer->sd.name, lin, col, i, rgb[i]);
486dacca5f0SHans Verkuil }
487dacca5f0SHans Verkuil }
488dacca5f0SHans Verkuil
vimc_debayer_process_frame(struct vimc_ent_device * ved,const void * sink_frame)489ec917d77SDaniel Oakley static void *vimc_debayer_process_frame(struct vimc_ent_device *ved,
490dacca5f0SHans Verkuil const void *sink_frame)
491dacca5f0SHans Verkuil {
492ec917d77SDaniel Oakley struct vimc_debayer_device *vdebayer =
493ec917d77SDaniel Oakley container_of(ved, struct vimc_debayer_device, ved);
494ec917d77SDaniel Oakley
495dacca5f0SHans Verkuil unsigned int rgb[3];
496dacca5f0SHans Verkuil unsigned int i, j;
497dacca5f0SHans Verkuil
498dacca5f0SHans Verkuil /* If the stream in this node is not active, just return */
499ec917d77SDaniel Oakley if (!vdebayer->src_frame)
500dacca5f0SHans Verkuil return ERR_PTR(-EINVAL);
501dacca5f0SHans Verkuil
502*4c46cb2aSLaurent Pinchart for (i = 0; i < vdebayer->hw.size.height; i++)
503*4c46cb2aSLaurent Pinchart for (j = 0; j < vdebayer->hw.size.width; j++) {
504ec917d77SDaniel Oakley vimc_debayer_calc_rgb_sink(vdebayer, sink_frame, i, j, rgb);
505ec917d77SDaniel Oakley vdebayer->set_rgb_src(vdebayer, i, j, rgb);
506dacca5f0SHans Verkuil }
507dacca5f0SHans Verkuil
508ec917d77SDaniel Oakley return vdebayer->src_frame;
509dacca5f0SHans Verkuil }
510dacca5f0SHans Verkuil
vimc_debayer_s_ctrl(struct v4l2_ctrl * ctrl)511ec917d77SDaniel Oakley static int vimc_debayer_s_ctrl(struct v4l2_ctrl *ctrl)
512dacca5f0SHans Verkuil {
513ec917d77SDaniel Oakley struct vimc_debayer_device *vdebayer =
514ec917d77SDaniel Oakley container_of(ctrl->handler, struct vimc_debayer_device, hdl);
515dacca5f0SHans Verkuil
516dacca5f0SHans Verkuil switch (ctrl->id) {
517dacca5f0SHans Verkuil case VIMC_CID_MEAN_WIN_SIZE:
518*4c46cb2aSLaurent Pinchart vdebayer->hw.mean_win_size = ctrl->val;
519dacca5f0SHans Verkuil break;
520dacca5f0SHans Verkuil default:
521dacca5f0SHans Verkuil return -EINVAL;
522dacca5f0SHans Verkuil }
523dacca5f0SHans Verkuil return 0;
524dacca5f0SHans Verkuil }
525dacca5f0SHans Verkuil
526ec917d77SDaniel Oakley static const struct v4l2_ctrl_ops vimc_debayer_ctrl_ops = {
527ec917d77SDaniel Oakley .s_ctrl = vimc_debayer_s_ctrl,
528dacca5f0SHans Verkuil };
529dacca5f0SHans Verkuil
vimc_debayer_release(struct vimc_ent_device * ved)530ec917d77SDaniel Oakley static void vimc_debayer_release(struct vimc_ent_device *ved)
531dacca5f0SHans Verkuil {
532ec917d77SDaniel Oakley struct vimc_debayer_device *vdebayer =
533ec917d77SDaniel Oakley container_of(ved, struct vimc_debayer_device, ved);
534dacca5f0SHans Verkuil
535ec917d77SDaniel Oakley v4l2_ctrl_handler_free(&vdebayer->hdl);
536b3f73b21SLaurent Pinchart v4l2_subdev_cleanup(&vdebayer->sd);
537ec917d77SDaniel Oakley media_entity_cleanup(vdebayer->ved.ent);
538ec917d77SDaniel Oakley kfree(vdebayer);
539dacca5f0SHans Verkuil }
540dacca5f0SHans Verkuil
541ec917d77SDaniel Oakley static const struct v4l2_ctrl_config vimc_debayer_ctrl_class = {
542dacca5f0SHans Verkuil .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY,
543dacca5f0SHans Verkuil .id = VIMC_CID_VIMC_CLASS,
544dacca5f0SHans Verkuil .name = "VIMC Controls",
545dacca5f0SHans Verkuil .type = V4L2_CTRL_TYPE_CTRL_CLASS,
546dacca5f0SHans Verkuil };
547dacca5f0SHans Verkuil
548ec917d77SDaniel Oakley static const struct v4l2_ctrl_config vimc_debayer_ctrl_mean_win_size = {
549ec917d77SDaniel Oakley .ops = &vimc_debayer_ctrl_ops,
550dacca5f0SHans Verkuil .id = VIMC_CID_MEAN_WIN_SIZE,
551dacca5f0SHans Verkuil .name = "Debayer Mean Window Size",
552dacca5f0SHans Verkuil .type = V4L2_CTRL_TYPE_INTEGER,
553dacca5f0SHans Verkuil .min = 1,
554dacca5f0SHans Verkuil .max = 25,
555dacca5f0SHans Verkuil .step = 2,
556dacca5f0SHans Verkuil .def = 3,
557dacca5f0SHans Verkuil };
558dacca5f0SHans Verkuil
vimc_debayer_add(struct vimc_device * vimc,const char * vcfg_name)559ec917d77SDaniel Oakley static struct vimc_ent_device *vimc_debayer_add(struct vimc_device *vimc,
560dacca5f0SHans Verkuil const char *vcfg_name)
561dacca5f0SHans Verkuil {
562dacca5f0SHans Verkuil struct v4l2_device *v4l2_dev = &vimc->v4l2_dev;
563ec917d77SDaniel Oakley struct vimc_debayer_device *vdebayer;
564dacca5f0SHans Verkuil int ret;
565dacca5f0SHans Verkuil
566ec917d77SDaniel Oakley /* Allocate the vdebayer struct */
567ec917d77SDaniel Oakley vdebayer = kzalloc(sizeof(*vdebayer), GFP_KERNEL);
568ec917d77SDaniel Oakley if (!vdebayer)
569dacca5f0SHans Verkuil return ERR_PTR(-ENOMEM);
570dacca5f0SHans Verkuil
571dacca5f0SHans Verkuil /* Create controls: */
572ec917d77SDaniel Oakley v4l2_ctrl_handler_init(&vdebayer->hdl, 2);
573ec917d77SDaniel Oakley v4l2_ctrl_new_custom(&vdebayer->hdl, &vimc_debayer_ctrl_class, NULL);
574ec917d77SDaniel Oakley v4l2_ctrl_new_custom(&vdebayer->hdl, &vimc_debayer_ctrl_mean_win_size, NULL);
575ec917d77SDaniel Oakley vdebayer->sd.ctrl_handler = &vdebayer->hdl;
576ec917d77SDaniel Oakley if (vdebayer->hdl.error) {
577ec917d77SDaniel Oakley ret = vdebayer->hdl.error;
578ec917d77SDaniel Oakley goto err_free_vdebayer;
579dacca5f0SHans Verkuil }
580dacca5f0SHans Verkuil
581dacca5f0SHans Verkuil /* Initialize ved and sd */
582ec917d77SDaniel Oakley vdebayer->pads[0].flags = MEDIA_PAD_FL_SINK;
583ec917d77SDaniel Oakley vdebayer->pads[1].flags = MEDIA_PAD_FL_SOURCE;
584dacca5f0SHans Verkuil
585ec917d77SDaniel Oakley ret = vimc_ent_sd_register(&vdebayer->ved, &vdebayer->sd, v4l2_dev,
586dacca5f0SHans Verkuil vcfg_name,
587dacca5f0SHans Verkuil MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV, 2,
588556d821aSLaurent Pinchart vdebayer->pads, &vimc_debayer_internal_ops,
589556d821aSLaurent Pinchart &vimc_debayer_ops);
590dacca5f0SHans Verkuil if (ret)
591dacca5f0SHans Verkuil goto err_free_hdl;
592dacca5f0SHans Verkuil
593ec917d77SDaniel Oakley vdebayer->ved.process_frame = vimc_debayer_process_frame;
594ec917d77SDaniel Oakley vdebayer->ved.dev = vimc->mdev.dev;
595*4c46cb2aSLaurent Pinchart vdebayer->hw.mean_win_size = vimc_debayer_ctrl_mean_win_size.def;
596dacca5f0SHans Verkuil
597ec917d77SDaniel Oakley vdebayer->set_rgb_src = vimc_debayer_process_rgb_frame;
598dacca5f0SHans Verkuil
599ec917d77SDaniel Oakley return &vdebayer->ved;
600dacca5f0SHans Verkuil
601dacca5f0SHans Verkuil err_free_hdl:
602ec917d77SDaniel Oakley v4l2_ctrl_handler_free(&vdebayer->hdl);
603ec917d77SDaniel Oakley err_free_vdebayer:
604ec917d77SDaniel Oakley kfree(vdebayer);
605dacca5f0SHans Verkuil
606dacca5f0SHans Verkuil return ERR_PTR(ret);
607dacca5f0SHans Verkuil }
608dacca5f0SHans Verkuil
60973a4385cSLaurent Pinchart const struct vimc_ent_type vimc_debayer_type = {
610ec917d77SDaniel Oakley .add = vimc_debayer_add,
611ec917d77SDaniel Oakley .release = vimc_debayer_release
612dacca5f0SHans Verkuil };
613