c8adc724758ac29848d065641359729d8bd47039
[openwrt/staging/pepe2k.git] /
1 From 41d7a5a226cc5a649b08d7f9ba3997730d878dbb Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Fri, 10 Mar 2023 17:43:57 +0000
4 Subject: [PATCH 0500/1085] media: imx219: Advertise embedded data node on
5 media 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 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
18 ---
19 drivers/media/i2c/imx219.c | 167 ++++++++++++++++++++++++-------------
20 1 file changed, 111 insertions(+), 56 deletions(-)
21
22 --- a/drivers/media/i2c/imx219.c
23 +++ b/drivers/media/i2c/imx219.c
24 @@ -161,6 +161,21 @@
25 #define IMX219_PIXEL_ARRAY_WIDTH 3280U
26 #define IMX219_PIXEL_ARRAY_HEIGHT 2464U
27
28 +/* Embedded metadata stream structure */
29 +#define IMX219_EMBEDDED_LINE_WIDTH 16384
30 +#define IMX219_NUM_EMBEDDED_LINES 1
31 +
32 +enum pad_types {
33 + IMAGE_PAD,
34 + METADATA_PAD,
35 + NUM_PADS
36 +};
37 +
38 +struct imx219_reg {
39 + u16 address;
40 + u8 val;
41 +};
42 +
43 struct imx219_reg_list {
44 unsigned int num_of_regs;
45 const struct cci_reg_sequence *regs;
46 @@ -445,7 +460,7 @@ static const struct imx219_mode supporte
47
48 struct imx219 {
49 struct v4l2_subdev sd;
50 - struct media_pad pad;
51 + struct media_pad pad[NUM_PADS];
52
53 struct regmap *regmap;
54 struct clk *xclk; /* system clock to IMX219 */
55 @@ -620,6 +635,13 @@ static int imx219_init_cfg(struct v4l2_s
56 crop->width = IMX219_PIXEL_ARRAY_WIDTH;
57 crop->height = IMX219_PIXEL_ARRAY_HEIGHT;
58
59 + /* Initialize try_fmt for the embedded metadata pad */
60 + format = v4l2_subdev_get_pad_format(sd, state, 1);
61 + format->code = MEDIA_BUS_FMT_SENSOR_DATA;
62 + format->width = IMX219_EMBEDDED_LINE_WIDTH;
63 + format->height = IMX219_NUM_EMBEDDED_LINES;
64 + format->field = V4L2_FIELD_NONE;
65 +
66 return 0;
67 }
68
69 @@ -629,10 +651,20 @@ static int imx219_enum_mbus_code(struct
70 {
71 struct imx219 *imx219 = to_imx219(sd);
72
73 - if (code->index >= (ARRAY_SIZE(imx219_mbus_formats) / 4))
74 + if (code->pad >= NUM_PADS)
75 return -EINVAL;
76
77 - code->code = imx219_get_format_code(imx219, imx219_mbus_formats[code->index * 4]);
78 + if (code->pad == IMAGE_PAD) {
79 + if (code->index >= (ARRAY_SIZE(imx219_mbus_formats) / 4))
80 + return -EINVAL;
81 +
82 + code->code = imx219_get_format_code(imx219, imx219_mbus_formats[code->index * 4]);
83 + } else {
84 + if (code->index > 0)
85 + return -EINVAL;
86 +
87 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
88 + }
89
90 return 0;
91 }
92 @@ -644,17 +676,30 @@ static int imx219_enum_frame_size(struct
93 struct imx219 *imx219 = to_imx219(sd);
94 u32 code;
95
96 - if (fse->index >= ARRAY_SIZE(supported_modes))
97 + if (fse->pad >= NUM_PADS)
98 return -EINVAL;
99
100 - code = imx219_get_format_code(imx219, fse->code);
101 - if (fse->code != code)
102 - return -EINVAL;
103 + if (fse->pad == IMAGE_PAD) {
104 + if (fse->index >= ARRAY_SIZE(supported_modes))
105 + return -EINVAL;
106 +
107 + code = imx219_get_format_code(imx219, fse->code);
108 + if (fse->code != code)
109 + return -EINVAL;
110 +
111 + fse->min_width = supported_modes[fse->index].width;
112 + fse->max_width = fse->min_width;
113 + fse->min_height = supported_modes[fse->index].height;
114 + fse->max_height = fse->min_height;
115 + } else {
116 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
117 + return -EINVAL;
118
119 - fse->min_width = supported_modes[fse->index].width;
120 - fse->max_width = fse->min_width;
121 - fse->min_height = supported_modes[fse->index].height;
122 - fse->max_height = fse->min_height;
123 + fse->min_width = IMX219_EMBEDDED_LINE_WIDTH;
124 + fse->max_width = fse->min_width;
125 + fse->min_height = IMX219_NUM_EMBEDDED_LINES;
126 + fse->max_height = fse->min_height;
127 + }
128
129 return 0;
130 }
131 @@ -669,50 +714,59 @@ static int imx219_set_pad_format(struct
132 struct v4l2_mbus_framefmt *format;
133 struct v4l2_rect *crop;
134
135 - mode = v4l2_find_nearest_size(supported_modes,
136 - ARRAY_SIZE(supported_modes),
137 - width, height,
138 - fmt->format.width, fmt->format.height);
139 -
140 - imx219_update_pad_format(imx219, mode, &fmt->format, fmt->format.code);
141 -
142 - format = v4l2_subdev_get_pad_format(sd, sd_state, 0);
143 - crop = v4l2_subdev_get_pad_crop(sd, sd_state, 0);
144 -
145 - *format = fmt->format;
146 - *crop = mode->crop;
147 -
148 - if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
149 - u32 prev_hts = imx219->mode->width + imx219->hblank->val;
150 -
151 - imx219->mode = mode;
152 - /* Update limits and set FPS to default */
153 - __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
154 - IMX219_VTS_MAX - mode->height, 1,
155 - mode->vts_def - mode->height);
156 - __v4l2_ctrl_s_ctrl(imx219->vblank,
157 - mode->vts_def - mode->height);
158 - /* Update max exposure while meeting expected vblanking */
159 - exposure_max = mode->vts_def - 4;
160 - exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
161 - exposure_max : IMX219_EXPOSURE_DEFAULT;
162 - __v4l2_ctrl_modify_range(imx219->exposure,
163 - imx219->exposure->minimum,
164 - exposure_max, imx219->exposure->step,
165 - exposure_def);
166 - /*
167 - * Retain PPL setting from previous mode so that the
168 - * line time does not change on a mode change.
169 - * Limits have to be recomputed as the controls define
170 - * the blanking only, so PPL values need to have the
171 - * mode width subtracted.
172 - */
173 - hblank = prev_hts - mode->width;
174 - __v4l2_ctrl_modify_range(imx219->hblank,
175 - IMX219_PPL_MIN - mode->width,
176 - IMX219_PPL_MAX - mode->width,
177 - 1, IMX219_PPL_MIN - mode->width);
178 - __v4l2_ctrl_s_ctrl(imx219->hblank, hblank);
179 + if (fmt->pad >= NUM_PADS)
180 + return -EINVAL;
181 +
182 + if (fmt->pad == IMAGE_PAD) {
183 + mode = v4l2_find_nearest_size(supported_modes,
184 + ARRAY_SIZE(supported_modes),
185 + width, height,
186 + fmt->format.width, fmt->format.height);
187 +
188 + imx219_update_pad_format(imx219, mode, &fmt->format, fmt->format.code);
189 +
190 + format = v4l2_subdev_get_pad_format(sd, sd_state, 0);
191 + crop = v4l2_subdev_get_pad_crop(sd, sd_state, 0);
192 +
193 + *format = fmt->format;
194 + *crop = mode->crop;
195 +
196 + if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
197 + u32 prev_hts = imx219->mode->width + imx219->hblank->val;
198 +
199 + imx219->mode = mode;
200 + /* Update limits and set FPS to default */
201 + __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
202 + IMX219_VTS_MAX - mode->height, 1,
203 + mode->vts_def - mode->height);
204 + __v4l2_ctrl_s_ctrl(imx219->vblank,
205 + mode->vts_def - mode->height);
206 + /* Update max exposure while meeting expected vblanking */
207 + exposure_max = mode->vts_def - 4;
208 + exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
209 + exposure_max : IMX219_EXPOSURE_DEFAULT;
210 + __v4l2_ctrl_modify_range(imx219->exposure,
211 + imx219->exposure->minimum,
212 + exposure_max, imx219->exposure->step,
213 + exposure_def);
214 + /*
215 + * Retain PPL setting from previous mode so that the
216 + * line time does not change on a mode change.
217 + * Limits have to be recomputed as the controls define
218 + * the blanking only, so PPL values need to have the
219 + * mode width subtracted.
220 + */
221 + hblank = prev_hts - mode->width;
222 + __v4l2_ctrl_modify_range(imx219->hblank,
223 + IMX219_PPL_MIN - mode->width,
224 + IMX219_PPL_MAX - mode->width,
225 + 1, IMX219_PPL_MIN - mode->width);
226 + __v4l2_ctrl_s_ctrl(imx219->hblank, hblank);
227 + }
228 + } else {
229 + format = v4l2_subdev_get_pad_format(sd, sd_state, 1);
230 + /* Don't allow the embedded data format to be changed */
231 + fmt->format = *format;
232 }
233
234 return 0;
235 @@ -1331,9 +1385,10 @@ static int imx219_probe(struct i2c_clien
236 imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
237
238 /* Initialize source pad */
239 - imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
240 + imx219->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
241 + imx219->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
242
243 - ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad);
244 + ret = media_entity_pads_init(&imx219->sd.entity, NUM_PADS, imx219->pad);
245 if (ret) {
246 dev_err(dev, "failed to init entity pads: %d\n", ret);
247 goto error_handler_free;