1 From 16a061b6cf7229db782615393f020d723640da69 Mon Sep 17 00:00:00 2001
2 From: Arducam <admin@arducam.com>
3 Date: Wed, 15 Sep 2021 09:02:08 +0800
4 Subject: [PATCH] media: i2c: imx519: Advertise embedded data node on
7 This commit updates the imx519 driver to adverise support for embedded
10 The imx519 sensor subdevice overloads the media pad to differentiate
11 between image stream (pad 0) and embedded data stream (pad 1) when
12 performing the v4l2_subdev_pad_ops functions.
14 Signed-off-by: Lee Jackson <info@arducam.com>
16 drivers/media/i2c/imx519.c | 138 +++++++++++++++++++++++++++----------
17 1 file changed, 103 insertions(+), 35 deletions(-)
19 --- a/drivers/media/i2c/imx519.c
20 +++ b/drivers/media/i2c/imx519.c
22 #define IMX519_TEST_PATTERN_B_DEFAULT 0
23 #define IMX519_TEST_PATTERN_GB_DEFAULT 0
25 +/* Embedded metadata stream structure */
26 +#define IMX519_EMBEDDED_LINE_WIDTH 16384
27 +#define IMX519_NUM_EMBEDDED_LINES 1
35 /* IMX519 native and active pixel array size. */
36 #define IMX519_NATIVE_WIDTH 4672U
37 #define IMX519_NATIVE_HEIGHT 3648U
38 @@ -970,7 +980,7 @@ static const char * const imx519_supply_
41 struct v4l2_subdev sd;
42 - struct media_pad pad;
43 + struct media_pad pad[NUM_PADS];
45 unsigned int fmt_code;
47 @@ -1101,7 +1111,9 @@ static int imx519_open(struct v4l2_subde
49 struct imx519 *imx519 = to_imx519(sd);
50 struct v4l2_mbus_framefmt *try_fmt_img =
51 - v4l2_subdev_get_try_format(sd, fh->state, 0);
52 + v4l2_subdev_get_try_format(sd, fh->state, IMAGE_PAD);
53 + struct v4l2_mbus_framefmt *try_fmt_meta =
54 + v4l2_subdev_get_try_format(sd, fh->state, METADATA_PAD);
55 struct v4l2_rect *try_crop;
57 mutex_lock(&imx519->mutex);
58 @@ -1112,8 +1124,14 @@ static int imx519_open(struct v4l2_subde
59 try_fmt_img->code = imx519_get_format_code(imx519);
60 try_fmt_img->field = V4L2_FIELD_NONE;
62 + /* Initialize try_fmt for the embedded metadata pad */
63 + try_fmt_meta->width = IMX519_EMBEDDED_LINE_WIDTH;
64 + try_fmt_meta->height = IMX519_NUM_EMBEDDED_LINES;
65 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
66 + try_fmt_meta->field = V4L2_FIELD_NONE;
68 /* Initialize try_crop */
69 - try_crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
70 + try_crop = v4l2_subdev_get_try_crop(sd, fh->state, IMAGE_PAD);
71 try_crop->left = IMX519_PIXEL_ARRAY_LEFT;
72 try_crop->top = IMX519_PIXEL_ARRAY_TOP;
73 try_crop->width = IMX519_PIXEL_ARRAY_WIDTH;
74 @@ -1246,10 +1264,20 @@ static int imx519_enum_mbus_code(struct
76 struct imx519 *imx519 = to_imx519(sd);
78 - if (code->index > 0)
79 + if (code->pad >= NUM_PADS)
82 - code->code = imx519_get_format_code(imx519);
83 + if (code->pad == IMAGE_PAD) {
84 + if (code->index > 0)
87 + code->code = imx519_get_format_code(imx519);
89 + if (code->index > 0)
92 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
97 @@ -1260,16 +1288,29 @@ static int imx519_enum_frame_size(struct
99 struct imx519 *imx519 = to_imx519(sd);
101 - if (fse->index >= ARRAY_SIZE(supported_modes_10bit))
102 + if (fse->pad >= NUM_PADS)
105 - if (fse->code != imx519_get_format_code(imx519))
107 + if (fse->pad == IMAGE_PAD) {
108 + if (fse->index >= ARRAY_SIZE(supported_modes_10bit))
111 + if (fse->code != imx519_get_format_code(imx519))
114 + fse->min_width = supported_modes_10bit[fse->index].width;
115 + fse->max_width = fse->min_width;
116 + fse->min_height = supported_modes_10bit[fse->index].height;
117 + fse->max_height = fse->min_height;
119 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
122 - fse->min_width = supported_modes_10bit[fse->index].width;
123 - fse->max_width = fse->min_width;
124 - fse->min_height = supported_modes_10bit[fse->index].height;
125 - fse->max_height = fse->min_height;
126 + fse->min_width = IMX519_EMBEDDED_LINE_WIDTH;
127 + fse->max_width = fse->min_width;
128 + fse->min_height = IMX519_NUM_EMBEDDED_LINES;
129 + fse->max_height = fse->min_height;
134 @@ -1294,13 +1335,21 @@ static void imx519_update_image_pad_form
135 imx519_reset_colorspace(&fmt->format);
138 +static void imx519_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
140 + fmt->format.width = IMX519_EMBEDDED_LINE_WIDTH;
141 + fmt->format.height = IMX519_NUM_EMBEDDED_LINES;
142 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
143 + fmt->format.field = V4L2_FIELD_NONE;
146 static int imx519_get_pad_format(struct v4l2_subdev *sd,
147 struct v4l2_subdev_state *sd_state,
148 struct v4l2_subdev_format *fmt)
150 struct imx519 *imx519 = to_imx519(sd);
153 + if (fmt->pad >= NUM_PADS)
156 mutex_lock(&imx519->mutex);
157 @@ -1310,12 +1359,19 @@ static int imx519_get_pad_format(struct
158 v4l2_subdev_get_try_format(&imx519->sd, sd_state,
160 /* update the code which could change due to vflip or hflip: */
161 - try_fmt->code = imx519_get_format_code(imx519);
162 + try_fmt->code = fmt->pad == IMAGE_PAD ?
163 + imx519_get_format_code(imx519) :
164 + MEDIA_BUS_FMT_SENSOR_DATA;
165 fmt->format = *try_fmt;
167 - imx519_update_image_pad_format(imx519, imx519->mode,
169 - fmt->format.code = imx519_get_format_code(imx519);
170 + if (fmt->pad == IMAGE_PAD) {
171 + imx519_update_image_pad_format(imx519, imx519->mode,
174 + imx519_get_format_code(imx519);
176 + imx519_update_metadata_pad_format(fmt);
180 mutex_unlock(&imx519->mutex);
181 @@ -1376,28 +1432,39 @@ static int imx519_set_pad_format(struct
182 const struct imx519_mode *mode;
183 struct imx519 *imx519 = to_imx519(sd);
186 + if (fmt->pad >= NUM_PADS)
189 mutex_lock(&imx519->mutex);
191 - /* Bayer order varies with flips */
192 - fmt->format.code = imx519_get_format_code(imx519);
193 + if (fmt->pad == IMAGE_PAD) {
194 + /* Bayer order varies with flips */
195 + fmt->format.code = imx519_get_format_code(imx519);
197 - mode = v4l2_find_nearest_size(supported_modes_10bit,
198 - ARRAY_SIZE(supported_modes_10bit),
201 - fmt->format.height);
202 - imx519_update_image_pad_format(imx519, mode, fmt);
203 - if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
204 - framefmt = v4l2_subdev_get_try_format(sd, sd_state,
206 - *framefmt = fmt->format;
207 + mode = v4l2_find_nearest_size(supported_modes_10bit,
208 + ARRAY_SIZE(supported_modes_10bit),
211 + fmt->format.height);
212 + imx519_update_image_pad_format(imx519, mode, fmt);
213 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
214 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
216 + *framefmt = fmt->format;
218 + imx519->mode = mode;
219 + imx519->fmt_code = fmt->format.code;
220 + imx519_set_framing_limits(imx519);
223 - imx519->mode = mode;
224 - imx519->fmt_code = fmt->format.code;
225 - imx519_set_framing_limits(imx519);
226 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
227 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
229 + *framefmt = fmt->format;
231 + /* Only one embedded data mode is supported */
232 + imx519_update_metadata_pad_format(fmt);
236 mutex_unlock(&imx519->mutex);
237 @@ -1953,9 +2020,10 @@ static int imx519_probe(struct i2c_clien
238 imx519->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
240 /* Initialize source pads */
241 - imx519->pad.flags = MEDIA_PAD_FL_SOURCE;
242 + imx519->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
243 + imx519->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
245 - ret = media_entity_pads_init(&imx519->sd.entity, 1, &imx519->pad);
246 + ret = media_entity_pads_init(&imx519->sd.entity, NUM_PADS, imx519->pad);
248 dev_err(dev, "failed to init entity pads: %d\n", ret);
249 goto error_handler_free;