9e6778858896f6bba8e2dc0f5473cef3931b6b9f
[openwrt/staging/linusw.git] /
1 From c144d907a9067f948706a4cb8709672b0708c4c4 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Wed, 8 Dec 2021 13:22:48 +0100
4 Subject: [PATCH] media: imx219: Advertise embedded data node on media
5 pad 1
6
7 This commit updates the imx219 driver to adverise support for embedded
8 data streams. This can then be used by the bcm2835-unicam driver, which
9 has recently been updated to expose the embedded data stream to
10 userland.
11
12 The imx219 sensor subdevice overloads the media pad to differentiate
13 between image stream (pad 0) and embedded data stream (pad 1) when
14 performing the v4l2_subdev_pad_ops functions.
15
16 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
17 [JMH: Adapt to the mainline 5.16 kernel]
18 Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>
19 ---
20 drivers/media/i2c/imx219.c | 254 ++++++++++++++++++++++++-------------
21 1 file changed, 169 insertions(+), 85 deletions(-)
22
23 --- a/drivers/media/i2c/imx219.c
24 +++ b/drivers/media/i2c/imx219.c
25 @@ -124,6 +124,16 @@
26 #define IMX219_PIXEL_ARRAY_WIDTH 3280U
27 #define IMX219_PIXEL_ARRAY_HEIGHT 2464U
28
29 +/* Embedded metadata stream structure */
30 +#define IMX219_EMBEDDED_LINE_WIDTH 16384
31 +#define IMX219_NUM_EMBEDDED_LINES 1
32 +
33 +enum pad_types {
34 + IMAGE_PAD,
35 + METADATA_PAD,
36 + NUM_PADS
37 +};
38 +
39 struct imx219_reg {
40 u16 address;
41 u8 val;
42 @@ -448,7 +458,7 @@ static const struct imx219_mode supporte
43
44 struct imx219 {
45 struct v4l2_subdev sd;
46 - struct media_pad pad;
47 + struct media_pad pad[NUM_PADS];
48
49 struct v4l2_mbus_framefmt fmt;
50
51 @@ -598,18 +608,26 @@ static void imx219_set_default_format(st
52 static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
53 {
54 struct imx219 *imx219 = to_imx219(sd);
55 - struct v4l2_mbus_framefmt *try_fmt =
56 - v4l2_subdev_get_try_format(sd, fh->state, 0);
57 + struct v4l2_mbus_framefmt *try_fmt_img =
58 + v4l2_subdev_get_try_format(sd, fh->state, IMAGE_PAD);
59 + struct v4l2_mbus_framefmt *try_fmt_meta =
60 + v4l2_subdev_get_try_format(sd, fh->state, METADATA_PAD);
61 struct v4l2_rect *try_crop;
62
63 mutex_lock(&imx219->mutex);
64
65 - /* Initialize try_fmt */
66 - try_fmt->width = supported_modes[0].width;
67 - try_fmt->height = supported_modes[0].height;
68 - try_fmt->code = imx219_get_format_code(imx219,
69 - MEDIA_BUS_FMT_SRGGB10_1X10);
70 - try_fmt->field = V4L2_FIELD_NONE;
71 + /* Initialize try_fmt for the image pad */
72 + try_fmt_img->width = supported_modes[0].width;
73 + try_fmt_img->height = supported_modes[0].height;
74 + try_fmt_img->code = imx219_get_format_code(imx219,
75 + MEDIA_BUS_FMT_SRGGB10_1X10);
76 + try_fmt_img->field = V4L2_FIELD_NONE;
77 +
78 + /* Initialize try_fmt for the embedded metadata pad */
79 + try_fmt_meta->width = IMX219_EMBEDDED_LINE_WIDTH;
80 + try_fmt_meta->height = IMX219_NUM_EMBEDDED_LINES;
81 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
82 + try_fmt_meta->field = V4L2_FIELD_NONE;
83
84 /* Initialize try_crop rectangle. */
85 try_crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
86 @@ -718,12 +736,21 @@ static int imx219_enum_mbus_code(struct
87 {
88 struct imx219 *imx219 = to_imx219(sd);
89
90 - if (code->index >= (ARRAY_SIZE(codes) / 4))
91 + if (code->pad >= NUM_PADS)
92 return -EINVAL;
93
94 - mutex_lock(&imx219->mutex);
95 - code->code = imx219_get_format_code(imx219, codes[code->index * 4]);
96 - mutex_unlock(&imx219->mutex);
97 + if (code->pad == IMAGE_PAD) {
98 + if (code->index >= (ARRAY_SIZE(codes) / 4))
99 + return -EINVAL;
100 +
101 + code->code = imx219_get_format_code(imx219,
102 + codes[code->index * 4]);
103 + } else {
104 + if (code->index > 0)
105 + return -EINVAL;
106 +
107 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
108 + }
109
110 return 0;
111 }
112 @@ -733,21 +760,30 @@ static int imx219_enum_frame_size(struct
113 struct v4l2_subdev_frame_size_enum *fse)
114 {
115 struct imx219 *imx219 = to_imx219(sd);
116 - u32 code;
117
118 - if (fse->index >= ARRAY_SIZE(supported_modes))
119 + if (fse->pad >= NUM_PADS)
120 return -EINVAL;
121
122 - mutex_lock(&imx219->mutex);
123 - code = imx219_get_format_code(imx219, fse->code);
124 - mutex_unlock(&imx219->mutex);
125 - if (fse->code != code)
126 - return -EINVAL;
127 + if (fse->pad == IMAGE_PAD) {
128 + if (fse->index >= ARRAY_SIZE(supported_modes))
129 + return -EINVAL;
130 +
131 + if (fse->code != imx219_get_format_code(imx219, fse->code))
132 + return -EINVAL;
133 +
134 + fse->min_width = supported_modes[fse->index].width;
135 + fse->max_width = fse->min_width;
136 + fse->min_height = supported_modes[fse->index].height;
137 + fse->max_height = fse->min_height;
138 + } else {
139 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
140 + return -EINVAL;
141
142 - fse->min_width = supported_modes[fse->index].width;
143 - fse->max_width = fse->min_width;
144 - fse->min_height = supported_modes[fse->index].height;
145 - fse->max_height = fse->min_height;
146 + fse->min_width = IMX219_EMBEDDED_LINE_WIDTH;
147 + fse->max_width = fse->min_width;
148 + fse->min_height = IMX219_NUM_EMBEDDED_LINES;
149 + fse->max_height = fse->min_height;
150 + }
151
152 return 0;
153 }
154 @@ -762,9 +798,9 @@ static void imx219_reset_colorspace(stru
155 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
156 }
157
158 -static void imx219_update_pad_format(struct imx219 *imx219,
159 - const struct imx219_mode *mode,
160 - struct v4l2_subdev_format *fmt)
161 +static void imx219_update_image_pad_format(struct imx219 *imx219,
162 + const struct imx219_mode *mode,
163 + struct v4l2_subdev_format *fmt)
164 {
165 fmt->format.width = mode->width;
166 fmt->format.height = mode->height;
167 @@ -772,21 +808,39 @@ static void imx219_update_pad_format(str
168 imx219_reset_colorspace(&fmt->format);
169 }
170
171 +static void imx219_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
172 +{
173 + fmt->format.width = IMX219_EMBEDDED_LINE_WIDTH;
174 + fmt->format.height = IMX219_NUM_EMBEDDED_LINES;
175 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
176 + fmt->format.field = V4L2_FIELD_NONE;
177 +}
178 +
179 static int __imx219_get_pad_format(struct imx219 *imx219,
180 struct v4l2_subdev_state *sd_state,
181 struct v4l2_subdev_format *fmt)
182 {
183 + if (fmt->pad >= NUM_PADS)
184 + return -EINVAL;
185 +
186 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
187 struct v4l2_mbus_framefmt *try_fmt =
188 v4l2_subdev_get_try_format(&imx219->sd, sd_state,
189 fmt->pad);
190 /* update the code which could change due to vflip or hflip: */
191 - try_fmt->code = imx219_get_format_code(imx219, try_fmt->code);
192 + try_fmt->code = fmt->pad == IMAGE_PAD ?
193 + imx219_get_format_code(imx219, try_fmt->code) :
194 + MEDIA_BUS_FMT_SENSOR_DATA;
195 fmt->format = *try_fmt;
196 } else {
197 - imx219_update_pad_format(imx219, imx219->mode, fmt);
198 - fmt->format.code = imx219_get_format_code(imx219,
199 - imx219->fmt.code);
200 + if (fmt->pad == IMAGE_PAD) {
201 + imx219_update_image_pad_format(imx219, imx219->mode,
202 + fmt);
203 + fmt->format.code = imx219_get_format_code(imx219,
204 + imx219->fmt.code);
205 + } else {
206 + imx219_update_metadata_pad_format(fmt);
207 + }
208 }
209
210 return 0;
211 @@ -816,51 +870,74 @@ static int imx219_set_pad_format(struct
212 int exposure_max, exposure_def, hblank;
213 unsigned int i;
214
215 - mutex_lock(&imx219->mutex);
216 -
217 - for (i = 0; i < ARRAY_SIZE(codes); i++)
218 - if (codes[i] == fmt->format.code)
219 - break;
220 - if (i >= ARRAY_SIZE(codes))
221 - i = 0;
222 + if (fmt->pad >= NUM_PADS)
223 + return -EINVAL;
224
225 - /* Bayer order varies with flips */
226 - fmt->format.code = imx219_get_format_code(imx219, codes[i]);
227 + mutex_lock(&imx219->mutex);
228
229 - mode = v4l2_find_nearest_size(supported_modes,
230 - ARRAY_SIZE(supported_modes),
231 - width, height,
232 - fmt->format.width, fmt->format.height);
233 - imx219_update_pad_format(imx219, mode, fmt);
234 - if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
235 - framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
236 - *framefmt = fmt->format;
237 - } else if (imx219->mode != mode ||
238 - imx219->fmt.code != fmt->format.code) {
239 - imx219->fmt = fmt->format;
240 - imx219->mode = mode;
241 - /* Update limits and set FPS to default */
242 - __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
243 - IMX219_VTS_MAX - mode->height, 1,
244 - mode->vts_def - mode->height);
245 - __v4l2_ctrl_s_ctrl(imx219->vblank,
246 - mode->vts_def - mode->height);
247 - /* Update max exposure while meeting expected vblanking */
248 - exposure_max = mode->vts_def - 4;
249 - exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
250 - exposure_max : IMX219_EXPOSURE_DEFAULT;
251 - __v4l2_ctrl_modify_range(imx219->exposure,
252 - imx219->exposure->minimum,
253 - exposure_max, imx219->exposure->step,
254 - exposure_def);
255 - /*
256 - * Currently PPL is fixed to IMX219_PPL_DEFAULT, so hblank
257 - * depends on mode->width only, and is not changeble in any
258 - * way other than changing the mode.
259 - */
260 - hblank = IMX219_PPL_DEFAULT - mode->width;
261 - __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank, 1,
262 - hblank);
263 + if (fmt->pad == IMAGE_PAD) {
264 + for (i = 0; i < ARRAY_SIZE(codes); i++)
265 + if (codes[i] == fmt->format.code)
266 + break;
267 + if (i >= ARRAY_SIZE(codes))
268 + i = 0;
269 +
270 + /* Bayer order varies with flips */
271 + fmt->format.code = imx219_get_format_code(imx219, codes[i]);
272 +
273 + mode = v4l2_find_nearest_size(supported_modes,
274 + ARRAY_SIZE(supported_modes),
275 + width, height,
276 + fmt->format.width,
277 + fmt->format.height);
278 + imx219_update_image_pad_format(imx219, mode, fmt);
279 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
280 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
281 + fmt->pad);
282 + *framefmt = fmt->format;
283 + } else if (imx219->mode != mode ||
284 + imx219->fmt.code != fmt->format.code) {
285 + imx219->fmt = fmt->format;
286 + imx219->mode = mode;
287 + /* Update limits and set FPS to default */
288 + __v4l2_ctrl_modify_range(imx219->vblank,
289 + IMX219_VBLANK_MIN,
290 + IMX219_VTS_MAX - mode->height,
291 + 1,
292 + mode->vts_def - mode->height);
293 + __v4l2_ctrl_s_ctrl(imx219->vblank,
294 + mode->vts_def - mode->height);
295 + /*
296 + * Update max exposure while meeting
297 + * expected vblanking
298 + */
299 + exposure_max = mode->vts_def - 4;
300 + exposure_def =
301 + (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
302 + exposure_max : IMX219_EXPOSURE_DEFAULT;
303 + __v4l2_ctrl_modify_range(imx219->exposure,
304 + imx219->exposure->minimum,
305 + exposure_max,
306 + imx219->exposure->step,
307 + exposure_def);
308 + /*
309 + * Currently PPL is fixed to IMX219_PPL_DEFAULT, so
310 + * hblank depends on mode->width only, and is not
311 + * changeble in any way other than changing the mode.
312 + */
313 + hblank = IMX219_PPL_DEFAULT - mode->width;
314 + __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank,
315 + 1, hblank);
316 + }
317 + } else {
318 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
319 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
320 + fmt->pad);
321 + *framefmt = fmt->format;
322 + } else {
323 + /* Only one embedded data mode is supported */
324 + imx219_update_metadata_pad_format(fmt);
325 + }
326 }
327
328 mutex_unlock(&imx219->mutex);
329 @@ -976,9 +1053,11 @@ static int imx219_start_streaming(struct
330 const struct imx219_reg_list *reg_list;
331 int ret;
332
333 - ret = pm_runtime_resume_and_get(&client->dev);
334 - if (ret < 0)
335 + ret = pm_runtime_get_sync(&client->dev);
336 + if (ret < 0) {
337 + pm_runtime_put_noidle(&client->dev);
338 return ret;
339 + }
340
341 /* Send all registers that are common to all modes */
342 ret = imx219_write_regs(imx219, imx219_common_regs, ARRAY_SIZE(imx219_common_regs));
343 @@ -1086,21 +1165,22 @@ err_unlock:
344 /* Power/clock management functions */
345 static int imx219_power_on(struct device *dev)
346 {
347 - struct v4l2_subdev *sd = dev_get_drvdata(dev);
348 + struct i2c_client *client = to_i2c_client(dev);
349 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
350 struct imx219 *imx219 = to_imx219(sd);
351 int ret;
352
353 ret = regulator_bulk_enable(IMX219_NUM_SUPPLIES,
354 imx219->supplies);
355 if (ret) {
356 - dev_err(dev, "%s: failed to enable regulators\n",
357 + dev_err(&client->dev, "%s: failed to enable regulators\n",
358 __func__);
359 return ret;
360 }
361
362 ret = clk_prepare_enable(imx219->xclk);
363 if (ret) {
364 - dev_err(dev, "%s: failed to enable clock\n",
365 + dev_err(&client->dev, "%s: failed to enable clock\n",
366 __func__);
367 goto reg_off;
368 }
369 @@ -1119,7 +1199,8 @@ reg_off:
370
371 static int imx219_power_off(struct device *dev)
372 {
373 - struct v4l2_subdev *sd = dev_get_drvdata(dev);
374 + struct i2c_client *client = to_i2c_client(dev);
375 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
376 struct imx219 *imx219 = to_imx219(sd);
377
378 gpiod_set_value_cansleep(imx219->reset_gpio, 0);
379 @@ -1131,7 +1212,8 @@ static int imx219_power_off(struct devic
380
381 static int __maybe_unused imx219_suspend(struct device *dev)
382 {
383 - struct v4l2_subdev *sd = dev_get_drvdata(dev);
384 + struct i2c_client *client = to_i2c_client(dev);
385 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
386 struct imx219 *imx219 = to_imx219(sd);
387
388 if (imx219->streaming)
389 @@ -1142,7 +1224,8 @@ static int __maybe_unused imx219_suspend
390
391 static int __maybe_unused imx219_resume(struct device *dev)
392 {
393 - struct v4l2_subdev *sd = dev_get_drvdata(dev);
394 + struct i2c_client *client = to_i2c_client(dev);
395 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
396 struct imx219 *imx219 = to_imx219(sd);
397 int ret;
398
399 @@ -1478,13 +1561,14 @@ static int imx219_probe(struct i2c_clien
400 V4L2_SUBDEV_FL_HAS_EVENTS;
401 imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
402
403 - /* Initialize source pad */
404 - imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
405 + /* Initialize source pads */
406 + imx219->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
407 + imx219->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
408
409 /* Initialize default format */
410 imx219_set_default_format(imx219);
411
412 - ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad);
413 + ret = media_entity_pads_init(&imx219->sd.entity, NUM_PADS, imx219->pad);
414 if (ret) {
415 dev_err(dev, "failed to init entity pads: %d\n", ret);
416 goto error_handler_free;