f1310f2b7768fc6f77debcd46d3715735397f645
[openwrt/staging/blocktrron.git] /
1 From 8acf47ea3641126ed10ec54a8f6d72622dd2c61e Mon Sep 17 00:00:00 2001
2 From: Lee Jackson <info@arducam.com>
3 Date: Wed, 18 May 2022 15:18:59 +0800
4 Subject: [PATCH] media: i2c: arducam_64mp: Advertise embedded data
5 node on media pad 1
6
7 This commit updates the arducam_64mp driver to adverise support for
8 embedded data streams.
9
10 The arducam_64mp 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/arducam_64mp.c | 146 ++++++++++++++++++++++---------
17 1 file changed, 107 insertions(+), 39 deletions(-)
18
19 --- a/drivers/media/i2c/arducam_64mp.c
20 +++ b/drivers/media/i2c/arducam_64mp.c
21 @@ -94,6 +94,16 @@
22 #define ARDUCAM_64MP_TEST_PATTERN_B_DEFAULT 0
23 #define ARDUCAM_64MP_TEST_PATTERN_GB_DEFAULT 0
24
25 +/* Embedded metadata stream structure */
26 +#define ARDUCAM_64MP_EMBEDDED_LINE_WIDTH 16384
27 +#define ARDUCAM_64MP_NUM_EMBEDDED_LINES 1
28 +
29 +enum pad_types {
30 + IMAGE_PAD,
31 + METADATA_PAD,
32 + NUM_PADS
33 +};
34 +
35 /* ARDUCAM_64MP native and active pixel array size. */
36 #define ARDUCAM_64MP_NATIVE_WIDTH 9344U
37 #define ARDUCAM_64MP_NATIVE_HEIGHT 7032U
38 @@ -1273,7 +1283,7 @@ static const char * const arducam_64mp_s
39
40 struct arducam_64mp {
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 @@ -1406,7 +1416,9 @@ static int arducam_64mp_open(struct v4l2
48 {
49 struct arducam_64mp *arducam_64mp = to_arducam_64mp(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(&arducam_64mp->mutex);
58 @@ -1417,8 +1429,14 @@ static int arducam_64mp_open(struct v4l2
59 try_fmt_img->code = arducam_64mp_get_format_code(arducam_64mp);
60 try_fmt_img->field = V4L2_FIELD_NONE;
61
62 + /* Initialize try_fmt for the embedded metadata pad */
63 + try_fmt_meta->width = ARDUCAM_64MP_EMBEDDED_LINE_WIDTH;
64 + try_fmt_meta->height = ARDUCAM_64MP_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 = ARDUCAM_64MP_PIXEL_ARRAY_LEFT;
72 try_crop->top = ARDUCAM_64MP_PIXEL_ARRAY_TOP;
73 try_crop->width = ARDUCAM_64MP_PIXEL_ARRAY_WIDTH;
74 @@ -1574,10 +1592,20 @@ static int arducam_64mp_enum_mbus_code(s
75 {
76 struct arducam_64mp *arducam_64mp = to_arducam_64mp(sd);
77
78 - if (code->index > 0)
79 + if (code->pad >= NUM_PADS)
80 return -EINVAL;
81
82 - code->code = arducam_64mp_get_format_code(arducam_64mp);
83 + if (code->pad == IMAGE_PAD) {
84 + if (code->index > 0)
85 + return -EINVAL;
86 +
87 + code->code = arducam_64mp_get_format_code(arducam_64mp);
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 @@ -1588,16 +1616,29 @@ static int arducam_64mp_enum_frame_size(
98 {
99 struct arducam_64mp *arducam_64mp = to_arducam_64mp(sd);
100
101 - if (fse->index >= ARRAY_SIZE(supported_modes))
102 + if (fse->pad >= NUM_PADS)
103 return -EINVAL;
104
105 - if (fse->code != arducam_64mp_get_format_code(arducam_64mp))
106 - return -EINVAL;
107 + if (fse->pad == IMAGE_PAD) {
108 + if (fse->index >= ARRAY_SIZE(supported_modes))
109 + return -EINVAL;
110 +
111 + if (fse->code != arducam_64mp_get_format_code(arducam_64mp))
112 + return -EINVAL;
113 +
114 + fse->min_width = supported_modes[fse->index].width;
115 + fse->max_width = fse->min_width;
116 + fse->min_height = supported_modes[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[fse->index].width;
123 - fse->max_width = fse->min_width;
124 - fse->min_height = supported_modes[fse->index].height;
125 - fse->max_height = fse->min_height;
126 + fse->min_width = ARDUCAM_64MP_EMBEDDED_LINE_WIDTH;
127 + fse->max_width = fse->min_width;
128 + fse->min_height = ARDUCAM_64MP_NUM_EMBEDDED_LINES;
129 + fse->max_height = fse->min_height;
130 + }
131
132 return 0;
133 }
134 @@ -1623,13 +1664,22 @@ arducam_64mp_update_image_pad_format(str
135 arducam_64mp_reset_colorspace(&fmt->format);
136 }
137
138 +static void
139 +arducam_64mp_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
140 +{
141 + fmt->format.width = ARDUCAM_64MP_EMBEDDED_LINE_WIDTH;
142 + fmt->format.height = ARDUCAM_64MP_NUM_EMBEDDED_LINES;
143 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
144 + fmt->format.field = V4L2_FIELD_NONE;
145 +}
146 +
147 static int arducam_64mp_get_pad_format(struct v4l2_subdev *sd,
148 struct v4l2_subdev_state *sd_state,
149 struct v4l2_subdev_format *fmt)
150 {
151 struct arducam_64mp *arducam_64mp = to_arducam_64mp(sd);
152
153 - if (fmt->pad != 0)
154 + if (fmt->pad >= NUM_PADS)
155 return -EINVAL;
156
157 mutex_lock(&arducam_64mp->mutex);
158 @@ -1639,14 +1689,20 @@ static int arducam_64mp_get_pad_format(s
159 v4l2_subdev_get_try_format(&arducam_64mp->sd, sd_state,
160 fmt->pad);
161 /* update the code which could change due to vflip or hflip: */
162 - try_fmt->code = arducam_64mp_get_format_code(arducam_64mp);
163 + try_fmt->code = fmt->pad == IMAGE_PAD ?
164 + arducam_64mp_get_format_code(arducam_64mp) :
165 + MEDIA_BUS_FMT_SENSOR_DATA;
166 fmt->format = *try_fmt;
167 } else {
168 - arducam_64mp_update_image_pad_format(arducam_64mp,
169 - arducam_64mp->mode,
170 - fmt);
171 - fmt->format.code =
172 - arducam_64mp_get_format_code(arducam_64mp);
173 + if (fmt->pad == IMAGE_PAD) {
174 + arducam_64mp_update_image_pad_format(arducam_64mp,
175 + arducam_64mp->mode,
176 + fmt);
177 + fmt->format.code =
178 + arducam_64mp_get_format_code(arducam_64mp);
179 + } else {
180 + arducam_64mp_update_metadata_pad_format(fmt);
181 + }
182 }
183
184 mutex_unlock(&arducam_64mp->mutex);
185 @@ -1714,28 +1770,39 @@ static int arducam_64mp_set_pad_format(s
186 const struct arducam_64mp_mode *mode;
187 struct arducam_64mp *arducam_64mp = to_arducam_64mp(sd);
188
189 - if (fmt->pad != 0)
190 + if (fmt->pad >= NUM_PADS)
191 return -EINVAL;
192
193 mutex_lock(&arducam_64mp->mutex);
194
195 - /* Bayer order varies with flips */
196 - fmt->format.code = arducam_64mp_get_format_code(arducam_64mp);
197 -
198 - mode = v4l2_find_nearest_size(supported_modes,
199 - ARRAY_SIZE(supported_modes),
200 - width, height,
201 - fmt->format.width,
202 - fmt->format.height);
203 - arducam_64mp_update_image_pad_format(arducam_64mp, mode, fmt);
204 - if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
205 - framefmt = v4l2_subdev_get_try_format(sd, sd_state,
206 - fmt->pad);
207 - *framefmt = fmt->format;
208 + if (fmt->pad == IMAGE_PAD) {
209 + /* Bayer order varies with flips */
210 + fmt->format.code = arducam_64mp_get_format_code(arducam_64mp);
211 +
212 + mode = v4l2_find_nearest_size(supported_modes,
213 + ARRAY_SIZE(supported_modes),
214 + width, height,
215 + fmt->format.width,
216 + fmt->format.height);
217 + arducam_64mp_update_image_pad_format(arducam_64mp, mode, fmt);
218 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
219 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
220 + fmt->pad);
221 + *framefmt = fmt->format;
222 + } else {
223 + arducam_64mp->mode = mode;
224 + arducam_64mp->fmt_code = fmt->format.code;
225 + arducam_64mp_set_framing_limits(arducam_64mp);
226 + }
227 } else {
228 - arducam_64mp->mode = mode;
229 - arducam_64mp->fmt_code = fmt->format.code;
230 - arducam_64mp_set_framing_limits(arducam_64mp);
231 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
232 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
233 + fmt->pad);
234 + *framefmt = fmt->format;
235 + } else {
236 + /* Only one embedded data mode is supported */
237 + arducam_64mp_update_metadata_pad_format(fmt);
238 + }
239 }
240
241 mutex_unlock(&arducam_64mp->mutex);
242 @@ -2329,10 +2396,11 @@ static int arducam_64mp_probe(struct i2c
243 arducam_64mp->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
244
245 /* Initialize source pads */
246 - arducam_64mp->pad.flags = MEDIA_PAD_FL_SOURCE;
247 + arducam_64mp->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
248 + arducam_64mp->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
249
250 - ret = media_entity_pads_init(&arducam_64mp->sd.entity, 1,
251 - &arducam_64mp->pad);
252 + ret = media_entity_pads_init(&arducam_64mp->sd.entity, NUM_PADS,
253 + arducam_64mp->pad);
254 if (ret) {
255 dev_err(dev, "failed to init entity pads: %d\n", ret);
256 goto error_handler_free;