cc408334337e1e69d9020ca7e0daea694168b6a0
[openwrt/staging/blogic.git] /
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
5 media pad 1
6
7 This commit updates the imx519 driver to adverise support for embedded
8 data streams.
9
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.
13
14 Signed-off-by: Lee Jackson <info@arducam.com>
15 ---
16 drivers/media/i2c/imx519.c | 138 +++++++++++++++++++++++++++----------
17 1 file changed, 103 insertions(+), 35 deletions(-)
18
19 --- a/drivers/media/i2c/imx519.c
20 +++ b/drivers/media/i2c/imx519.c
21 @@ -93,6 +93,16 @@
22 #define IMX519_TEST_PATTERN_B_DEFAULT 0
23 #define IMX519_TEST_PATTERN_GB_DEFAULT 0
24
25 +/* Embedded metadata stream structure */
26 +#define IMX519_EMBEDDED_LINE_WIDTH 16384
27 +#define IMX519_NUM_EMBEDDED_LINES 1
28 +
29 +enum pad_types {
30 + IMAGE_PAD,
31 + METADATA_PAD,
32 + NUM_PADS
33 +};
34 +
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_
39
40 struct imx519 {
41 struct v4l2_subdev sd;
42 - struct media_pad pad;
43 + struct media_pad pad[NUM_PADS];
44
45 unsigned int fmt_code;
46
47 @@ -1101,7 +1111,9 @@ static int imx519_open(struct v4l2_subde
48 {
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;
56
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;
61
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;
67 +
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
75 {
76 struct imx519 *imx519 = to_imx519(sd);
77
78 - if (code->index > 0)
79 + if (code->pad >= NUM_PADS)
80 return -EINVAL;
81
82 - code->code = imx519_get_format_code(imx519);
83 + if (code->pad == IMAGE_PAD) {
84 + if (code->index > 0)
85 + return -EINVAL;
86 +
87 + code->code = imx519_get_format_code(imx519);
88 + } else {
89 + if (code->index > 0)
90 + return -EINVAL;
91 +
92 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
93 + }
94
95 return 0;
96 }
97 @@ -1260,16 +1288,29 @@ static int imx519_enum_frame_size(struct
98 {
99 struct imx519 *imx519 = to_imx519(sd);
100
101 - if (fse->index >= ARRAY_SIZE(supported_modes_10bit))
102 + if (fse->pad >= NUM_PADS)
103 return -EINVAL;
104
105 - if (fse->code != imx519_get_format_code(imx519))
106 - return -EINVAL;
107 + if (fse->pad == IMAGE_PAD) {
108 + if (fse->index >= ARRAY_SIZE(supported_modes_10bit))
109 + return -EINVAL;
110 +
111 + if (fse->code != imx519_get_format_code(imx519))
112 + return -EINVAL;
113 +
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;
118 + } else {
119 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
120 + return -EINVAL;
121
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;
130 + }
131
132 return 0;
133 }
134 @@ -1294,13 +1335,21 @@ static void imx519_update_image_pad_form
135 imx519_reset_colorspace(&fmt->format);
136 }
137
138 +static void imx519_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
139 +{
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;
144 +}
145 +
146 static int imx519_get_pad_format(struct v4l2_subdev *sd,
147 struct v4l2_subdev_state *sd_state,
148 struct v4l2_subdev_format *fmt)
149 {
150 struct imx519 *imx519 = to_imx519(sd);
151
152 - if (fmt->pad != 0)
153 + if (fmt->pad >= NUM_PADS)
154 return -EINVAL;
155
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,
159 fmt->pad);
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;
166 } else {
167 - imx519_update_image_pad_format(imx519, imx519->mode,
168 - fmt);
169 - fmt->format.code = imx519_get_format_code(imx519);
170 + if (fmt->pad == IMAGE_PAD) {
171 + imx519_update_image_pad_format(imx519, imx519->mode,
172 + fmt);
173 + fmt->format.code =
174 + imx519_get_format_code(imx519);
175 + } else {
176 + imx519_update_metadata_pad_format(fmt);
177 + }
178 }
179
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);
184
185 - if (fmt->pad != 0)
186 + if (fmt->pad >= NUM_PADS)
187 return -EINVAL;
188
189 mutex_lock(&imx519->mutex);
190
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);
196
197 - mode = v4l2_find_nearest_size(supported_modes_10bit,
198 - ARRAY_SIZE(supported_modes_10bit),
199 - width, height,
200 - fmt->format.width,
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,
205 - fmt->pad);
206 - *framefmt = fmt->format;
207 + mode = v4l2_find_nearest_size(supported_modes_10bit,
208 + ARRAY_SIZE(supported_modes_10bit),
209 + width, height,
210 + fmt->format.width,
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,
215 + fmt->pad);
216 + *framefmt = fmt->format;
217 + } else {
218 + imx519->mode = mode;
219 + imx519->fmt_code = fmt->format.code;
220 + imx519_set_framing_limits(imx519);
221 + }
222 } else {
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,
228 + fmt->pad);
229 + *framefmt = fmt->format;
230 + } else {
231 + /* Only one embedded data mode is supported */
232 + imx519_update_metadata_pad_format(fmt);
233 + }
234 }
235
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;
239
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;
244
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);
247 if (ret) {
248 dev_err(dev, "failed to init entity pads: %d\n", ret);
249 goto error_handler_free;