552516bd67dcb523aad8b28e383a2eb67a64c570
[openwrt/staging/robimarko.git] /
1 From 1f00b84d993e1f8de17ef936e00f4264266cb5d1 Mon Sep 17 00:00:00 2001
2 From: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
3 Date: Tue, 10 Mar 2020 14:17:08 +0100
4 Subject: [PATCH] media: i2c: imx219: Add support for RAW8 bit bayer
5 format
6
7 Commit 22da1d56e982151e0bdfafe9de6fe94098a51356 upstream.
8
9 IMX219 sensor is capable for RAW8/RAW10 modes. This commit adds support
10 for RAW8 bayer format.
11
12 Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
13 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
14 Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
15 Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
16 ---
17 drivers/media/i2c/imx219.c | 148 +++++++++++++++++++++++++++++--------
18 1 file changed, 116 insertions(+), 32 deletions(-)
19
20 --- a/drivers/media/i2c/imx219.c
21 +++ b/drivers/media/i2c/imx219.c
22 @@ -168,15 +168,12 @@ static const struct imx219_reg mode_3280
23 {0x0171, 0x01},
24 {0x0174, 0x00},
25 {0x0175, 0x00},
26 - {0x018c, 0x0a},
27 - {0x018d, 0x0a},
28 {0x0301, 0x05},
29 {0x0303, 0x01},
30 {0x0304, 0x03},
31 {0x0305, 0x03},
32 {0x0306, 0x00},
33 {0x0307, 0x39},
34 - {0x0309, 0x0a},
35 {0x030b, 0x01},
36 {0x030c, 0x00},
37 {0x030d, 0x72},
38 @@ -230,15 +227,12 @@ static const struct imx219_reg mode_1920
39 {0x0171, 0x01},
40 {0x0174, 0x00},
41 {0x0175, 0x00},
42 - {0x018c, 0x0a},
43 - {0x018d, 0x0a},
44 {0x0301, 0x05},
45 {0x0303, 0x01},
46 {0x0304, 0x03},
47 {0x0305, 0x03},
48 {0x0306, 0x00},
49 {0x0307, 0x39},
50 - {0x0309, 0x0a},
51 {0x030b, 0x01},
52 {0x030c, 0x00},
53 {0x030d, 0x72},
54 @@ -290,15 +284,12 @@ static const struct imx219_reg mode_1640
55 {0x0171, 0x01},
56 {0x0174, 0x01},
57 {0x0175, 0x01},
58 - {0x018c, 0x0a},
59 - {0x018d, 0x0a},
60 {0x0301, 0x05},
61 {0x0303, 0x01},
62 {0x0304, 0x03},
63 {0x0305, 0x03},
64 {0x0306, 0x00},
65 {0x0307, 0x39},
66 - {0x0309, 0x0a},
67 {0x030b, 0x01},
68 {0x030c, 0x00},
69 {0x030d, 0x72},
70 @@ -322,6 +313,18 @@ static const struct imx219_reg mode_1640
71 {0x0163, 0x78},
72 };
73
74 +static const struct imx219_reg raw8_framefmt_regs[] = {
75 + {0x018c, 0x08},
76 + {0x018d, 0x08},
77 + {0x0309, 0x08},
78 +};
79 +
80 +static const struct imx219_reg raw10_framefmt_regs[] = {
81 + {0x018c, 0x0a},
82 + {0x018d, 0x0a},
83 + {0x0309, 0x0a},
84 +};
85 +
86 static const char * const imx219_test_pattern_menu[] = {
87 "Disabled",
88 "Color Bars",
89 @@ -349,6 +352,27 @@ static const char * const imx219_supply_
90 #define IMX219_NUM_SUPPLIES ARRAY_SIZE(imx219_supply_name)
91
92 /*
93 + * The supported formats.
94 + * This table MUST contain 4 entries per format, to cover the various flip
95 + * combinations in the order
96 + * - no flip
97 + * - h flip
98 + * - v flip
99 + * - h&v flips
100 + */
101 +static const u32 codes[] = {
102 + MEDIA_BUS_FMT_SRGGB10_1X10,
103 + MEDIA_BUS_FMT_SGRBG10_1X10,
104 + MEDIA_BUS_FMT_SGBRG10_1X10,
105 + MEDIA_BUS_FMT_SBGGR10_1X10,
106 +
107 + MEDIA_BUS_FMT_SRGGB8_1X8,
108 + MEDIA_BUS_FMT_SGRBG8_1X8,
109 + MEDIA_BUS_FMT_SGBRG8_1X8,
110 + MEDIA_BUS_FMT_SBGGR8_1X8,
111 +};
112 +
113 +/*
114 * Initialisation delay between XCLR low->high and the moment when the sensor
115 * can start capture (i.e. can leave software stanby) must be not less than:
116 * t4 + max(t5, t6 + <time to initialize the sensor register over I2C>)
117 @@ -413,6 +437,8 @@ struct imx219 {
118 struct v4l2_subdev sd;
119 struct media_pad pad;
120
121 + struct v4l2_mbus_framefmt fmt;
122 +
123 struct clk *xclk; /* system clock to IMX219 */
124 u32 xclk_freq;
125
126 @@ -519,19 +545,40 @@ static int imx219_write_regs(struct imx2
127 }
128
129 /* Get bayer order based on flip setting. */
130 -static u32 imx219_get_format_code(struct imx219 *imx219)
131 +static u32 imx219_get_format_code(struct imx219 *imx219, u32 code)
132 {
133 - /*
134 - * Only one bayer order is supported.
135 - * It depends on the flip settings.
136 - */
137 - static const u32 codes[2][2] = {
138 - { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10, },
139 - { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10, },
140 - };
141 + unsigned int i;
142
143 lockdep_assert_held(&imx219->mutex);
144 - return codes[imx219->vflip->val][imx219->hflip->val];
145 +
146 + for (i = 0; i < ARRAY_SIZE(codes); i++)
147 + if (codes[i] == code)
148 + break;
149 +
150 + if (i >= ARRAY_SIZE(codes))
151 + i = 0;
152 +
153 + i = (i & ~3) | (imx219->vflip->val ? 2 : 0) |
154 + (imx219->hflip->val ? 1 : 0);
155 +
156 + return codes[i];
157 +}
158 +
159 +static void imx219_set_default_format(struct imx219 *imx219)
160 +{
161 + struct v4l2_mbus_framefmt *fmt;
162 +
163 + fmt = &imx219->fmt;
164 + fmt->code = MEDIA_BUS_FMT_SRGGB10_1X10;
165 + fmt->colorspace = V4L2_COLORSPACE_SRGB;
166 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
167 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
168 + fmt->colorspace,
169 + fmt->ycbcr_enc);
170 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
171 + fmt->width = supported_modes[0].width;
172 + fmt->height = supported_modes[0].height;
173 + fmt->field = V4L2_FIELD_NONE;
174 }
175
176 static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
177 @@ -545,7 +592,8 @@ static int imx219_open(struct v4l2_subde
178 /* Initialize try_fmt */
179 try_fmt->width = supported_modes[0].width;
180 try_fmt->height = supported_modes[0].height;
181 - try_fmt->code = imx219_get_format_code(imx219);
182 + try_fmt->code = imx219_get_format_code(imx219,
183 + MEDIA_BUS_FMT_SRGGB10_1X10);
184 try_fmt->field = V4L2_FIELD_NONE;
185
186 mutex_unlock(&imx219->mutex);
187 @@ -648,14 +696,10 @@ static int imx219_enum_mbus_code(struct
188 {
189 struct imx219 *imx219 = to_imx219(sd);
190
191 - /*
192 - * Only one bayer order is supported (though it depends on the flip
193 - * settings)
194 - */
195 - if (code->index > 0)
196 + if (code->index >= (ARRAY_SIZE(codes) / 4))
197 return -EINVAL;
198
199 - code->code = imx219_get_format_code(imx219);
200 + code->code = imx219_get_format_code(imx219, codes[code->index * 4]);
201
202 return 0;
203 }
204 @@ -669,7 +713,7 @@ static int imx219_enum_frame_size(struct
205 if (fse->index >= ARRAY_SIZE(supported_modes))
206 return -EINVAL;
207
208 - if (fse->code != imx219_get_format_code(imx219))
209 + if (fse->code != imx219_get_format_code(imx219, imx219->fmt.code))
210 return -EINVAL;
211
212 fse->min_width = supported_modes[fse->index].width;
213 @@ -696,9 +740,7 @@ static void imx219_update_pad_format(str
214 {
215 fmt->format.width = mode->width;
216 fmt->format.height = mode->height;
217 - fmt->format.code = imx219_get_format_code(imx219);
218 fmt->format.field = V4L2_FIELD_NONE;
219 -
220 imx219_reset_colorspace(&fmt->format);
221 }
222
223 @@ -710,10 +752,12 @@ static int __imx219_get_pad_format(struc
224 struct v4l2_mbus_framefmt *try_fmt =
225 v4l2_subdev_get_try_format(&imx219->sd, cfg, fmt->pad);
226 /* update the code which could change due to vflip or hflip: */
227 - try_fmt->code = imx219_get_format_code(imx219);
228 + try_fmt->code = imx219_get_format_code(imx219, try_fmt->code);
229 fmt->format = *try_fmt;
230 } else {
231 imx219_update_pad_format(imx219, imx219->mode, fmt);
232 + fmt->format.code = imx219_get_format_code(imx219,
233 + imx219->fmt.code);
234 }
235
236 return 0;
237 @@ -741,11 +785,18 @@ static int imx219_set_pad_format(struct
238 const struct imx219_mode *mode;
239 struct v4l2_mbus_framefmt *framefmt;
240 int exposure_max, exposure_def, hblank;
241 + unsigned int i;
242
243 mutex_lock(&imx219->mutex);
244
245 + for (i = 0; i < ARRAY_SIZE(codes); i++)
246 + if (codes[i] == fmt->format.code)
247 + break;
248 + if (i >= ARRAY_SIZE(codes))
249 + i = 0;
250 +
251 /* Bayer order varies with flips */
252 - fmt->format.code = imx219_get_format_code(imx219);
253 + fmt->format.code = imx219_get_format_code(imx219, codes[i]);
254
255 mode = v4l2_find_nearest_size(supported_modes,
256 ARRAY_SIZE(supported_modes),
257 @@ -755,7 +806,9 @@ static int imx219_set_pad_format(struct
258 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
259 framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
260 *framefmt = fmt->format;
261 - } else if (imx219->mode != mode) {
262 + } else if (imx219->mode != mode ||
263 + imx219->fmt.code != fmt->format.code) {
264 + imx219->fmt = fmt->format;
265 imx219->mode = mode;
266 /* Update limits and set FPS to default */
267 __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
268 @@ -786,6 +839,27 @@ static int imx219_set_pad_format(struct
269 return 0;
270 }
271
272 +static int imx219_set_framefmt(struct imx219 *imx219)
273 +{
274 + switch (imx219->fmt.code) {
275 + case MEDIA_BUS_FMT_SRGGB8_1X8:
276 + case MEDIA_BUS_FMT_SGRBG8_1X8:
277 + case MEDIA_BUS_FMT_SGBRG8_1X8:
278 + case MEDIA_BUS_FMT_SBGGR8_1X8:
279 + return imx219_write_regs(imx219, raw8_framefmt_regs,
280 + ARRAY_SIZE(raw8_framefmt_regs));
281 +
282 + case MEDIA_BUS_FMT_SRGGB10_1X10:
283 + case MEDIA_BUS_FMT_SGRBG10_1X10:
284 + case MEDIA_BUS_FMT_SGBRG10_1X10:
285 + case MEDIA_BUS_FMT_SBGGR10_1X10:
286 + return imx219_write_regs(imx219, raw10_framefmt_regs,
287 + ARRAY_SIZE(raw10_framefmt_regs));
288 + }
289 +
290 + return -EINVAL;
291 +}
292 +
293 static int imx219_start_streaming(struct imx219 *imx219)
294 {
295 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
296 @@ -800,6 +874,13 @@ static int imx219_start_streaming(struct
297 return ret;
298 }
299
300 + ret = imx219_set_framefmt(imx219);
301 + if (ret) {
302 + dev_err(&client->dev, "%s failed to set frame format: %d\n",
303 + __func__, ret);
304 + return ret;
305 + }
306 +
307 /* Apply customized values from user */
308 ret = __v4l2_ctrl_handler_setup(imx219->sd.ctrl_handler);
309 if (ret)
310 @@ -1253,6 +1334,9 @@ static int imx219_probe(struct i2c_clien
311 /* Initialize source pad */
312 imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
313
314 + /* Initialize default format */
315 + imx219_set_default_format(imx219);
316 +
317 ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad);
318 if (ret) {
319 dev_err(dev, "failed to init entity pads: %d\n", ret);