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