kernel: bump 6.1 to 6.1.53
[openwrt/staging/hauke.git] / target / linux / bcm27xx / patches-6.1 / 950-0393-media-i2c-Add-driver-for-Omnivision-OV2311.patch
1 From 6390e65023f728f3ba3a85d4a6e7ed883bf8e7e8 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Fri, 25 Feb 2022 18:16:13 +0000
4 Subject: [PATCH] media/i2c: Add driver for Omnivision OV2311
5
6 Omnivision OV2311 is a CSI2 1600x1300 global shutter image sensor.
7 Add a driver for it.
8
9 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
10 ---
11 drivers/media/i2c/Kconfig | 11 +
12 drivers/media/i2c/Makefile | 1 +
13 drivers/media/i2c/ov2311.c | 1179 ++++++++++++++++++++++++++++++++++++
14 3 files changed, 1191 insertions(+)
15 create mode 100644 drivers/media/i2c/ov2311.c
16
17 --- a/drivers/media/i2c/Kconfig
18 +++ b/drivers/media/i2c/Kconfig
19 @@ -402,6 +402,17 @@ config VIDEO_OV13B10
20 This is a Video4Linux2 sensor driver for the OmniVision
21 OV13B10 camera.
22
23 +config VIDEO_OV2311
24 + tristate "OmniVision OV2311 sensor support"
25 + depends on I2C && VIDEO_DEV
26 + depends on MEDIA_CAMERA_SUPPORT
27 + help
28 + This is a Video4Linux2 sensor-level driver for the OmniVision
29 + OV2311 camera.
30 +
31 + To compile this driver as a module, choose M here: the
32 + module will be called ov2311.
33 +
34 config VIDEO_OV2640
35 tristate "OmniVision OV2640 sensor support"
36 depends on VIDEO_DEV && I2C
37 --- a/drivers/media/i2c/Makefile
38 +++ b/drivers/media/i2c/Makefile
39 @@ -77,6 +77,7 @@ obj-$(CONFIG_VIDEO_OV02A10) += ov02a10.o
40 obj-$(CONFIG_VIDEO_OV08D10) += ov08d10.o
41 obj-$(CONFIG_VIDEO_OV13858) += ov13858.o
42 obj-$(CONFIG_VIDEO_OV13B10) += ov13b10.o
43 +obj-$(CONFIG_VIDEO_OV2311) += ov2311.o
44 obj-$(CONFIG_VIDEO_OV2640) += ov2640.o
45 obj-$(CONFIG_VIDEO_OV2659) += ov2659.o
46 obj-$(CONFIG_VIDEO_OV2680) += ov2680.o
47 --- /dev/null
48 +++ b/drivers/media/i2c/ov2311.c
49 @@ -0,0 +1,1179 @@
50 +// SPDX-License-Identifier: GPL-2.0
51 +/*
52 + * Omnivision OV2311 1600x1300 global shutter image sensor driver
53 + * Copyright (C) 2022, Raspberry Pi (Trading) Ltd
54 + *
55 + * This driver is based on the OV9281 driver.
56 + * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
57 + * Register configuration from
58 + * https://github.com/ArduCAM/ArduCAM_USB_Camera_Shield/tree/master/Config/USB3.0_UC-425_Rev.C%2BUC-628_Rev.B/OV2311
59 + * with additional exposure and gain register information from
60 + * https://github.com/renesas-rcar/linux-bsp/tree/0cf6e36f5bf49e1c2aab87139ec5b588623c56f8/drivers/media/i2c/imagers
61 + */
62 +
63 +#include <linux/clk.h>
64 +#include <linux/device.h>
65 +#include <linux/delay.h>
66 +#include <linux/gpio/consumer.h>
67 +#include <linux/i2c.h>
68 +#include <linux/module.h>
69 +#include <linux/pm_runtime.h>
70 +#include <linux/regulator/consumer.h>
71 +#include <media/media-entity.h>
72 +#include <media/v4l2-async.h>
73 +#include <media/v4l2-ctrls.h>
74 +#include <media/v4l2-event.h>
75 +#include <media/v4l2-fwnode.h>
76 +#include <media/v4l2-subdev.h>
77 +
78 +#define OV2311_LINK_FREQ 400000000
79 +#define OV2311_LANES 2
80 +
81 +/* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
82 +#define OV2311_PIXEL_RATE_10BIT (OV2311_LINK_FREQ * 2 * \
83 + OV2311_LANES / 10)
84 +#define OV2311_PIXEL_RATE_8BIT (OV2311_LINK_FREQ * 2 * \
85 + OV2311_LANES / 8)
86 +#define OV2311_XVCLK_FREQ 24000000
87 +
88 +#define CHIP_ID 0x2311
89 +#define OV2311_REG_CHIP_ID 0x300a
90 +
91 +#define OV2311_REG_CTRL_MODE 0x0100
92 +#define OV2311_MODE_SW_STANDBY 0x0
93 +#define OV2311_MODE_STREAMING BIT(0)
94 +
95 +#define OV2311_REG_V_FLIP 0x3820
96 +#define OV2311_REG_H_FLIP 0x3821
97 +#define OV2311_FLIP_BIT BIT(2)
98 +
99 +#define OV2311_REG_EXPOSURE 0x3501
100 +#define OV2311_EXPOSURE_MIN 4
101 +#define OV2311_EXPOSURE_STEP 1
102 +#define OV2311_VTS_MAX 0xffff
103 +
104 +#define OV2311_REG_GAIN_H 0x3508
105 +#define OV2311_REG_GAIN_L 0x3509
106 +#define OV2311_GAIN_H_MASK 0x07
107 +#define OV2311_GAIN_H_SHIFT 8
108 +#define OV2311_GAIN_L_MASK 0xff
109 +#define OV2311_GAIN_MIN 0x100
110 +#define OV2311_GAIN_MAX 0x780
111 +#define OV2311_GAIN_STEP 1
112 +#define OV2311_GAIN_DEFAULT OV2311_GAIN_MIN
113 +
114 +#define OV2311_REG_TEST_PATTERN 0x5e00
115 +#define OV2311_TEST_PATTERN_ENABLE 0x80
116 +#define OV2311_TEST_PATTERN_DISABLE 0x0
117 +
118 +#define OV2311_REG_VTS 0x380e
119 +
120 +/*
121 + * OV2311 native and active pixel array size.
122 + * Datasheet not available to confirm these values. renesas-rcar linux-bsp tree
123 + * has these values.
124 + */
125 +#define OV2311_NATIVE_WIDTH 1616U
126 +#define OV2311_NATIVE_HEIGHT 1316U
127 +#define OV2311_PIXEL_ARRAY_LEFT 8U
128 +#define OV2311_PIXEL_ARRAY_TOP 8U
129 +#define OV2311_PIXEL_ARRAY_WIDTH 1600U
130 +#define OV2311_PIXEL_ARRAY_HEIGHT 1300U
131 +
132 +#define REG_NULL 0xFFFF
133 +
134 +#define OV2311_REG_VALUE_08BIT 1
135 +#define OV2311_REG_VALUE_16BIT 2
136 +#define OV2311_REG_VALUE_24BIT 3
137 +
138 +#define OV2311_NAME "ov2311"
139 +
140 +static const char * const ov2311_supply_names[] = {
141 + "avdd", /* Analog power */
142 + "dovdd", /* Digital I/O power */
143 + "dvdd", /* Digital core power */
144 +};
145 +
146 +#define OV2311_NUM_SUPPLIES ARRAY_SIZE(ov2311_supply_names)
147 +
148 +struct regval {
149 + u16 addr;
150 + u8 val;
151 +};
152 +
153 +struct ov2311_mode {
154 + u32 width;
155 + u32 height;
156 + u32 hts_def;
157 + u32 vts_def;
158 + u32 exp_def;
159 + struct v4l2_rect crop;
160 + const struct regval *reg_list;
161 +};
162 +
163 +struct ov2311 {
164 + struct i2c_client *client;
165 + struct clk *xvclk;
166 + struct gpio_desc *reset_gpio;
167 + struct gpio_desc *pwdn_gpio;
168 + struct regulator_bulk_data supplies[OV2311_NUM_SUPPLIES];
169 +
170 + struct v4l2_subdev subdev;
171 + struct media_pad pad;
172 + struct v4l2_ctrl_handler ctrl_handler;
173 + struct v4l2_ctrl *exposure;
174 + struct v4l2_ctrl *hblank;
175 + struct v4l2_ctrl *vblank;
176 + struct v4l2_ctrl *pixel_rate;
177 + /*
178 + * Mutex for serialized access:
179 + * Protect sensor module set pad format and start/stop streaming safely.
180 + */
181 + struct mutex mutex;
182 +
183 + /* Streaming on/off */
184 + bool streaming;
185 +
186 + const struct ov2311_mode *cur_mode;
187 + u32 code;
188 +};
189 +
190 +#define to_ov2311(sd) container_of(sd, struct ov2311, subdev)
191 +
192 +/*
193 + * Xclk 24Mhz
194 + * max_framerate 60fps for 10 bit, 74.6fps for 8 bit.
195 + */
196 +static const struct regval ov2311_common_regs[] = {
197 + { 0x0103, 0x01 },
198 + { 0x0100, 0x00 },
199 + { 0x0300, 0x01 },
200 + { 0x0302, 0x32 },
201 + { 0x0303, 0x00 },
202 + { 0x0304, 0x03 },
203 + { 0x0305, 0x02 },
204 + { 0x0306, 0x01 },
205 + { 0x030e, 0x04 },
206 + { 0x3001, 0x02 },
207 + { 0x3004, 0x00 },
208 + { 0x3005, 0x00 },
209 + { 0x3006, 0x00 },
210 + { 0x3011, 0x0d },
211 + { 0x3014, 0x04 },
212 + { 0x301c, 0xf0 },
213 + { 0x3020, 0x00 },
214 + { 0x302c, 0x00 },
215 + { 0x302d, 0x12 },
216 + { 0x302e, 0x4c },
217 + { 0x302f, 0x8c },
218 + { 0x3030, 0x10 },
219 + { 0x303f, 0x03 },
220 + { 0x3103, 0x00 },
221 + { 0x3106, 0x08 },
222 + { 0x31ff, 0x01 },
223 + { 0x3501, 0x05 },
224 + { 0x3502, 0xba },
225 + { 0x3506, 0x00 },
226 + { 0x3507, 0x00 },
227 + { 0x3620, 0x67 },
228 + { 0x3633, 0x78 },
229 + { 0x3666, 0x00 },
230 + { 0x3670, 0x68 },
231 + { 0x3674, 0x10 },
232 + { 0x3675, 0x00 },
233 + { 0x3680, 0x84 },
234 + { 0x36a2, 0x04 },
235 + { 0x36a3, 0x80 },
236 + { 0x36b0, 0x00 },
237 + { 0x3700, 0x35 },
238 + { 0x3704, 0x59 },
239 + { 0x3712, 0x00 },
240 + { 0x3713, 0x02 },
241 + { 0x379b, 0x01 },
242 + { 0x379c, 0x10 },
243 + { 0x3800, 0x00 },
244 + { 0x3801, 0x00 },
245 + { 0x3804, 0x06 },
246 + { 0x3805, 0x4f },
247 + { 0x3810, 0x00 },
248 + { 0x3811, 0x08 },
249 + { 0x3812, 0x00 },
250 + { 0x3813, 0x08 },
251 + { 0x3814, 0x11 },
252 + { 0x3815, 0x11 },
253 + { 0x3816, 0x00 },
254 + { 0x3817, 0x00 },
255 + { 0x3818, 0x04 },
256 + { 0x3819, 0x00 },
257 + { 0x382b, 0x5a },
258 + { 0x382c, 0x09 },
259 + { 0x382d, 0x9a },
260 + { 0x3882, 0x02 },
261 + { 0x3883, 0x6c },
262 + { 0x3885, 0x07 },
263 + { 0x389d, 0x03 },
264 + { 0x38a6, 0x00 },
265 + { 0x38a7, 0x01 },
266 + { 0x38b3, 0x07 },
267 + { 0x38b1, 0x00 },
268 + { 0x38e5, 0x02 },
269 + { 0x38e7, 0x00 },
270 + { 0x38e8, 0x00 },
271 + { 0x3910, 0xff },
272 + { 0x3911, 0xff },
273 + { 0x3912, 0x08 },
274 + { 0x3913, 0x00 },
275 + { 0x3914, 0x00 },
276 + { 0x3915, 0x00 },
277 + { 0x391c, 0x00 },
278 + { 0x3920, 0xa5 },
279 + { 0x3921, 0x00 },
280 + { 0x3922, 0x00 },
281 + { 0x3923, 0x00 },
282 + { 0x3924, 0x05 },
283 + { 0x3925, 0x00 },
284 + { 0x3926, 0x00 },
285 + { 0x3927, 0x00 },
286 + { 0x3928, 0x1a },
287 + { 0x392d, 0x05 },
288 + { 0x392e, 0xf2 },
289 + { 0x392f, 0x40 },
290 + { 0x4001, 0x00 },
291 + { 0x4003, 0x40 },
292 + { 0x4008, 0x12 },
293 + { 0x4009, 0x1b },
294 + { 0x400c, 0x0c },
295 + { 0x400d, 0x13 },
296 + { 0x4010, 0xf0 },
297 + { 0x4011, 0x00 },
298 + { 0x4016, 0x00 },
299 + { 0x4017, 0x04 },
300 + { 0x4042, 0x11 },
301 + { 0x4043, 0x70 },
302 + { 0x4045, 0x00 },
303 + { 0x4409, 0x5f },
304 + { 0x450b, 0x00 },
305 + { 0x4600, 0x00 },
306 + { 0x4601, 0xa0 },
307 + { 0x4708, 0x09 },
308 + { 0x470c, 0x81 },
309 + { 0x4710, 0x06 },
310 + { 0x4711, 0x00 },
311 + { 0x4800, 0x00 },
312 + { 0x481f, 0x30 },
313 + { 0x4837, 0x14 },
314 + { 0x4f00, 0x00 },
315 + { 0x4f07, 0x00 },
316 + { 0x4f08, 0x03 },
317 + { 0x4f09, 0x08 },
318 + { 0x4f0c, 0x06 },
319 + { 0x4f0d, 0x02 },
320 + { 0x4f10, 0x00 },
321 + { 0x4f11, 0x00 },
322 + { 0x4f12, 0x07 },
323 + { 0x4f13, 0xe2 },
324 + { 0x5000, 0x9f },
325 + { 0x5001, 0x20 },
326 + { 0x5026, 0x00 },
327 + { 0x5c00, 0x00 },
328 + { 0x5c01, 0x2c },
329 + { 0x5c02, 0x00 },
330 + { 0x5c03, 0x7f },
331 + { 0x5e00, 0x00 },
332 + { 0x5e01, 0x41 },
333 + {REG_NULL, 0x00},
334 +};
335 +
336 +static const struct regval ov2311_1600x1300_regs[] = {
337 + { 0x3802, 0x00 },
338 + { 0x3803, 0x00 },
339 + { 0x3806, 0x05 },
340 + { 0x3807, 0x23 },
341 + { 0x3808, 0x06 },
342 + { 0x3809, 0x40 },
343 + { 0x380a, 0x05 },
344 + { 0x380b, 0x14 },
345 + { 0x380c, 0x03 },
346 + { 0x380d, 0x88 },
347 + {REG_NULL, 0x00},
348 +};
349 +
350 +static const struct regval ov2311_1600x1080_regs[] = {
351 + { 0x3802, 0x00 },
352 + { 0x3803, 0x6e },
353 + { 0x3806, 0x04 },
354 + { 0x3807, 0xae },
355 + { 0x3808, 0x06 },
356 + { 0x3809, 0x40 },
357 + { 0x380a, 0x04 },
358 + { 0x380b, 0x38 },
359 + { 0x380c, 0x03 },
360 + { 0x380d, 0x88 },
361 +
362 + { 0x5d01, 0x00 },
363 + { 0x5d02, 0x04 },
364 + { 0x5d03, 0x00 },
365 + { 0x5d04, 0x04 },
366 + { 0x5d05, 0x00 },
367 + {REG_NULL, 0x00},
368 +};
369 +
370 +static const struct regval op_10bit[] = {
371 + { 0x030d, 0x5a },
372 + { 0x3662, 0x65 },
373 + {REG_NULL, 0x00},
374 +};
375 +
376 +static const struct regval op_8bit[] = {
377 + { 0x030d, 0x70 },
378 + { 0x3662, 0x67 },
379 + {REG_NULL, 0x00},
380 +};
381 +
382 +static const struct ov2311_mode supported_modes[] = {
383 + {
384 + .width = 1600,
385 + .height = 1300,
386 + .exp_def = 0x0320,
387 + .hts_def = (0x0388 * 2),/* Registers 0x380c / 0x380d * 2 */
388 + .vts_def = 0x5c2, /* Registers 0x380e / 0x380f
389 + * 60fps for 10bpp
390 + */
391 + .crop = {
392 + .left = OV2311_PIXEL_ARRAY_LEFT,
393 + .top = OV2311_PIXEL_ARRAY_TOP,
394 + .width = 1600,
395 + .height = 1300
396 + },
397 + .reg_list = ov2311_1600x1300_regs,
398 + },
399 + {
400 + .width = 1600,
401 + .height = 1080,
402 + .exp_def = 0x0320,
403 + .hts_def = (0x0388 * 2),/* Registers 0x380c / 0x380d * 2 */
404 + .vts_def = 0x5c2, /* Registers 0x380e / 0x380f
405 + * 60fps for 10bpp
406 + */
407 + .crop = {
408 + .left = OV2311_PIXEL_ARRAY_LEFT,
409 + .top = 110 + OV2311_PIXEL_ARRAY_TOP,
410 + .width = 1600,
411 + .height = 1080
412 + },
413 + .reg_list = ov2311_1600x1080_regs,
414 + },
415 +};
416 +
417 +static const s64 link_freq_menu_items[] = {
418 + OV2311_LINK_FREQ
419 +};
420 +
421 +static const char * const ov2311_test_pattern_menu[] = {
422 + "Disabled",
423 + "Vertical Color Bar Type 1",
424 + "Vertical Color Bar Type 2",
425 + "Vertical Color Bar Type 3",
426 + "Vertical Color Bar Type 4"
427 +};
428 +
429 +/* Write registers up to 4 at a time */
430 +static int ov2311_write_reg(struct i2c_client *client, u16 reg,
431 + u32 len, u32 val)
432 +{
433 + u32 buf_i, val_i;
434 + u8 buf[6];
435 + u8 *val_p;
436 + __be32 val_be;
437 +
438 + if (len > 4)
439 + return -EINVAL;
440 +
441 + buf[0] = reg >> 8;
442 + buf[1] = reg & 0xff;
443 +
444 + val_be = cpu_to_be32(val);
445 + val_p = (u8 *)&val_be;
446 + buf_i = 2;
447 + val_i = 4 - len;
448 +
449 + while (val_i < 4)
450 + buf[buf_i++] = val_p[val_i++];
451 +
452 + if (i2c_master_send(client, buf, len + 2) != len + 2)
453 + return -EIO;
454 +
455 + return 0;
456 +}
457 +
458 +static int ov2311_write_array(struct i2c_client *client,
459 + const struct regval *regs)
460 +{
461 + u32 i;
462 + int ret = 0;
463 +
464 + for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
465 + ret = ov2311_write_reg(client, regs[i].addr,
466 + OV2311_REG_VALUE_08BIT, regs[i].val);
467 +
468 + return ret;
469 +}
470 +
471 +/* Read registers up to 4 at a time */
472 +static int ov2311_read_reg(struct i2c_client *client, u16 reg, unsigned int len,
473 + u32 *val)
474 +{
475 + struct i2c_msg msgs[2];
476 + u8 *data_be_p;
477 + __be32 data_be = 0;
478 + __be16 reg_addr_be = cpu_to_be16(reg);
479 + int ret;
480 +
481 + if (len > 4 || !len)
482 + return -EINVAL;
483 +
484 + data_be_p = (u8 *)&data_be;
485 + /* Write register address */
486 + msgs[0].addr = client->addr;
487 + msgs[0].flags = 0;
488 + msgs[0].len = 2;
489 + msgs[0].buf = (u8 *)&reg_addr_be;
490 +
491 + /* Read data from register */
492 + msgs[1].addr = client->addr;
493 + msgs[1].flags = I2C_M_RD;
494 + msgs[1].len = len;
495 + msgs[1].buf = &data_be_p[4 - len];
496 +
497 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
498 + if (ret != ARRAY_SIZE(msgs))
499 + return -EIO;
500 +
501 + *val = be32_to_cpu(data_be);
502 +
503 + return 0;
504 +}
505 +
506 +static int ov2311_set_fmt(struct v4l2_subdev *sd,
507 + struct v4l2_subdev_state *sd_state,
508 + struct v4l2_subdev_format *fmt)
509 +{
510 + struct ov2311 *ov2311 = to_ov2311(sd);
511 + const struct ov2311_mode *mode;
512 + s64 h_blank, vblank_def, pixel_rate;
513 +
514 + mutex_lock(&ov2311->mutex);
515 +
516 + mode = v4l2_find_nearest_size(supported_modes,
517 + ARRAY_SIZE(supported_modes),
518 + width, height,
519 + fmt->format.width,
520 + fmt->format.height);
521 + if (fmt->format.code != MEDIA_BUS_FMT_Y8_1X8)
522 + fmt->format.code = MEDIA_BUS_FMT_Y10_1X10;
523 + fmt->format.width = mode->width;
524 + fmt->format.height = mode->height;
525 + fmt->format.field = V4L2_FIELD_NONE;
526 + fmt->format.colorspace = V4L2_COLORSPACE_RAW;
527 + fmt->format.ycbcr_enc =
528 + V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->format.colorspace);
529 + fmt->format.quantization =
530 + V4L2_MAP_QUANTIZATION_DEFAULT(true, fmt->format.colorspace,
531 + fmt->format.ycbcr_enc);
532 + fmt->format.xfer_func =
533 + V4L2_MAP_XFER_FUNC_DEFAULT(fmt->format.colorspace);
534 +
535 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
536 + *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) =
537 + fmt->format;
538 + } else {
539 + ov2311->cur_mode = mode;
540 + ov2311->code = fmt->format.code;
541 + h_blank = mode->hts_def - mode->width;
542 + __v4l2_ctrl_modify_range(ov2311->hblank, h_blank,
543 + h_blank, 1, h_blank);
544 + __v4l2_ctrl_s_ctrl(ov2311->hblank, h_blank);
545 + vblank_def = mode->vts_def - mode->height;
546 + __v4l2_ctrl_modify_range(ov2311->vblank, vblank_def,
547 + OV2311_VTS_MAX - mode->height,
548 + 1, vblank_def);
549 + __v4l2_ctrl_s_ctrl(ov2311->vblank, vblank_def);
550 +
551 + pixel_rate = (fmt->format.code == MEDIA_BUS_FMT_Y10_1X10) ?
552 + OV2311_PIXEL_RATE_10BIT : OV2311_PIXEL_RATE_8BIT;
553 + __v4l2_ctrl_modify_range(ov2311->pixel_rate, pixel_rate,
554 + pixel_rate, 1, pixel_rate);
555 + }
556 +
557 + mutex_unlock(&ov2311->mutex);
558 +
559 + return 0;
560 +}
561 +
562 +static int ov2311_get_fmt(struct v4l2_subdev *sd,
563 + struct v4l2_subdev_state *sd_state,
564 + struct v4l2_subdev_format *fmt)
565 +{
566 + struct ov2311 *ov2311 = to_ov2311(sd);
567 + const struct ov2311_mode *mode = ov2311->cur_mode;
568 +
569 + mutex_lock(&ov2311->mutex);
570 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
571 + fmt->format = *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
572 + } else {
573 + fmt->format.width = mode->width;
574 + fmt->format.height = mode->height;
575 + fmt->format.code = ov2311->code;
576 + fmt->format.field = V4L2_FIELD_NONE;
577 + fmt->format.colorspace = V4L2_COLORSPACE_SRGB;
578 + fmt->format.ycbcr_enc =
579 + V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->format.colorspace);
580 + fmt->format.quantization =
581 + V4L2_MAP_QUANTIZATION_DEFAULT(true,
582 + fmt->format.colorspace,
583 + fmt->format.ycbcr_enc);
584 + fmt->format.xfer_func =
585 + V4L2_MAP_XFER_FUNC_DEFAULT(fmt->format.colorspace);
586 + }
587 + mutex_unlock(&ov2311->mutex);
588 +
589 + return 0;
590 +}
591 +
592 +static int ov2311_enum_mbus_code(struct v4l2_subdev *sd,
593 + struct v4l2_subdev_state *sd_state,
594 + struct v4l2_subdev_mbus_code_enum *code)
595 +{
596 + switch (code->index) {
597 + default:
598 + return -EINVAL;
599 + case 0:
600 + code->code = MEDIA_BUS_FMT_Y10_1X10;
601 + break;
602 + case 1:
603 + code->code = MEDIA_BUS_FMT_Y8_1X8;
604 + break;
605 + }
606 +
607 + return 0;
608 +}
609 +
610 +static int ov2311_enum_frame_sizes(struct v4l2_subdev *sd,
611 + struct v4l2_subdev_state *sd_state,
612 + struct v4l2_subdev_frame_size_enum *fse)
613 +{
614 + if (fse->index >= ARRAY_SIZE(supported_modes))
615 + return -EINVAL;
616 +
617 + if (fse->code != MEDIA_BUS_FMT_Y10_1X10 &&
618 + fse->code != MEDIA_BUS_FMT_Y8_1X8)
619 + return -EINVAL;
620 +
621 + fse->min_width = supported_modes[fse->index].width;
622 + fse->max_width = supported_modes[fse->index].width;
623 + fse->max_height = supported_modes[fse->index].height;
624 + fse->min_height = supported_modes[fse->index].height;
625 +
626 + return 0;
627 +}
628 +
629 +static int ov2311_enable_test_pattern(struct ov2311 *ov2311, u32 pattern)
630 +{
631 + u32 val;
632 +
633 + if (pattern)
634 + val = (pattern - 1) | OV2311_TEST_PATTERN_ENABLE;
635 + else
636 + val = OV2311_TEST_PATTERN_DISABLE;
637 +
638 + return ov2311_write_reg(ov2311->client, OV2311_REG_TEST_PATTERN,
639 + OV2311_REG_VALUE_08BIT, val);
640 +}
641 +
642 +static const struct v4l2_rect *
643 +__ov2311_get_pad_crop(struct ov2311 *ov2311, struct v4l2_subdev_state *sd_state,
644 + unsigned int pad, enum v4l2_subdev_format_whence which)
645 +{
646 + switch (which) {
647 + case V4L2_SUBDEV_FORMAT_TRY:
648 + return v4l2_subdev_get_try_crop(&ov2311->subdev, sd_state, pad);
649 + case V4L2_SUBDEV_FORMAT_ACTIVE:
650 + return &ov2311->cur_mode->crop;
651 + }
652 +
653 + return NULL;
654 +}
655 +
656 +static int ov2311_get_selection(struct v4l2_subdev *sd,
657 + struct v4l2_subdev_state *sd_state,
658 + struct v4l2_subdev_selection *sel)
659 +{
660 + switch (sel->target) {
661 + case V4L2_SEL_TGT_CROP: {
662 + struct ov2311 *ov2311 = to_ov2311(sd);
663 +
664 + mutex_lock(&ov2311->mutex);
665 + sel->r = *__ov2311_get_pad_crop(ov2311, sd_state, sel->pad,
666 + sel->which);
667 + mutex_unlock(&ov2311->mutex);
668 +
669 + return 0;
670 + }
671 +
672 + case V4L2_SEL_TGT_NATIVE_SIZE:
673 + sel->r.top = 0;
674 + sel->r.left = 0;
675 + sel->r.width = OV2311_NATIVE_WIDTH;
676 + sel->r.height = OV2311_NATIVE_HEIGHT;
677 +
678 + return 0;
679 +
680 + case V4L2_SEL_TGT_CROP_DEFAULT:
681 + case V4L2_SEL_TGT_CROP_BOUNDS:
682 + sel->r.top = OV2311_PIXEL_ARRAY_TOP;
683 + sel->r.left = OV2311_PIXEL_ARRAY_LEFT;
684 + sel->r.width = OV2311_PIXEL_ARRAY_WIDTH;
685 + sel->r.height = OV2311_PIXEL_ARRAY_HEIGHT;
686 +
687 + return 0;
688 + }
689 +
690 + return -EINVAL;
691 +}
692 +
693 +static int ov2311_start_stream(struct ov2311 *ov2311)
694 +{
695 + int ret;
696 +
697 + ret = ov2311_write_array(ov2311->client, ov2311_common_regs);
698 + if (ret)
699 + return ret;
700 +
701 + ret = ov2311_write_array(ov2311->client, ov2311->cur_mode->reg_list);
702 + if (ret)
703 + return ret;
704 +
705 + if (ov2311->code == MEDIA_BUS_FMT_Y10_1X10)
706 + ret = ov2311_write_array(ov2311->client, op_10bit);
707 + else
708 + ret = ov2311_write_array(ov2311->client, op_8bit);
709 + if (ret)
710 + return ret;
711 +
712 + /* In case these controls are set before streaming */
713 + mutex_unlock(&ov2311->mutex);
714 + ret = v4l2_ctrl_handler_setup(&ov2311->ctrl_handler);
715 + mutex_lock(&ov2311->mutex);
716 + if (ret)
717 + return ret;
718 +
719 + return ov2311_write_reg(ov2311->client, OV2311_REG_CTRL_MODE,
720 + OV2311_REG_VALUE_08BIT, OV2311_MODE_STREAMING);
721 +}
722 +
723 +static int ov2311_stop_stream(struct ov2311 *ov2311)
724 +{
725 + return ov2311_write_reg(ov2311->client, OV2311_REG_CTRL_MODE,
726 + OV2311_REG_VALUE_08BIT, OV2311_MODE_SW_STANDBY);
727 +}
728 +
729 +static int ov2311_s_stream(struct v4l2_subdev *sd, int enable)
730 +{
731 + struct ov2311 *ov2311 = to_ov2311(sd);
732 + struct i2c_client *client = ov2311->client;
733 + int ret = 0;
734 +
735 + mutex_lock(&ov2311->mutex);
736 + if (ov2311->streaming == enable) {
737 + mutex_unlock(&ov2311->mutex);
738 + return 0;
739 + }
740 +
741 + if (enable) {
742 + ret = pm_runtime_resume_and_get(&client->dev);
743 + if (ret < 0)
744 + goto unlock_and_return;
745 +
746 + ret = ov2311_start_stream(ov2311);
747 + if (ret) {
748 + v4l2_err(sd, "start stream failed while write regs\n");
749 + pm_runtime_put(&client->dev);
750 + goto unlock_and_return;
751 + }
752 + } else {
753 + ov2311_stop_stream(ov2311);
754 + pm_runtime_put(&client->dev);
755 + }
756 +
757 + ov2311->streaming = enable;
758 +
759 +unlock_and_return:
760 + mutex_unlock(&ov2311->mutex);
761 +
762 + return ret;
763 +}
764 +
765 +static int ov2311_power_on(struct device *dev)
766 +{
767 + struct v4l2_subdev *sd = dev_get_drvdata(dev);
768 + struct ov2311 *ov2311 = to_ov2311(sd);
769 + int ret;
770 +
771 + ret = clk_set_rate(ov2311->xvclk, OV2311_XVCLK_FREQ);
772 + if (ret < 0)
773 + dev_warn(dev, "Failed to set xvclk rate (24MHz)\n");
774 + if (clk_get_rate(ov2311->xvclk) != OV2311_XVCLK_FREQ)
775 + dev_warn(dev, "xvclk mismatched, modes are based on 24MHz - rate is %lu\n",
776 + clk_get_rate(ov2311->xvclk));
777 +
778 + ret = clk_prepare_enable(ov2311->xvclk);
779 + if (ret < 0) {
780 + dev_err(dev, "Failed to enable xvclk\n");
781 + return ret;
782 + }
783 +
784 + gpiod_set_value_cansleep(ov2311->reset_gpio, 0);
785 +
786 + ret = regulator_bulk_enable(OV2311_NUM_SUPPLIES, ov2311->supplies);
787 + if (ret < 0) {
788 + dev_err(dev, "Failed to enable regulators\n");
789 + goto disable_clk;
790 + }
791 +
792 + gpiod_set_value_cansleep(ov2311->reset_gpio, 1);
793 +
794 + usleep_range(500, 1000);
795 + gpiod_set_value_cansleep(ov2311->pwdn_gpio, 1);
796 +
797 + usleep_range(1000, 2000);
798 +
799 + return 0;
800 +
801 +disable_clk:
802 + clk_disable_unprepare(ov2311->xvclk);
803 +
804 + return ret;
805 +}
806 +
807 +static int ov2311_power_off(struct device *dev)
808 +{
809 + struct v4l2_subdev *sd = dev_get_drvdata(dev);
810 + struct ov2311 *ov2311 = to_ov2311(sd);
811 +
812 + gpiod_set_value_cansleep(ov2311->pwdn_gpio, 0);
813 + clk_disable_unprepare(ov2311->xvclk);
814 + gpiod_set_value_cansleep(ov2311->reset_gpio, 0);
815 + regulator_bulk_disable(OV2311_NUM_SUPPLIES, ov2311->supplies);
816 +
817 + return 0;
818 +}
819 +
820 +static int ov2311_runtime_resume(struct device *dev)
821 +{
822 + struct v4l2_subdev *sd = dev_get_drvdata(dev);
823 + struct ov2311 *ov2311 = to_ov2311(sd);
824 + int ret;
825 +
826 + if (ov2311->streaming) {
827 + ret = ov2311_start_stream(ov2311);
828 + if (ret)
829 + goto error;
830 + }
831 + return 0;
832 +
833 +error:
834 + ov2311_stop_stream(ov2311);
835 + ov2311->streaming = 0;
836 + return ret;
837 +}
838 +
839 +static int ov2311_runtime_suspend(struct device *dev)
840 +{
841 + struct v4l2_subdev *sd = dev_get_drvdata(dev);
842 + struct ov2311 *ov2311 = to_ov2311(sd);
843 +
844 + if (ov2311->streaming)
845 + ov2311_stop_stream(ov2311);
846 +
847 + return 0;
848 +}
849 +
850 +static int ov2311_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
851 +{
852 + struct ov2311 *ov2311 = to_ov2311(sd);
853 + struct v4l2_mbus_framefmt *try_fmt =
854 + v4l2_subdev_get_try_format(sd, fh->state, 0);
855 + const struct ov2311_mode *def_mode = &supported_modes[0];
856 +
857 + mutex_lock(&ov2311->mutex);
858 + /* Initialize try_fmt */
859 + try_fmt->width = def_mode->width;
860 + try_fmt->height = def_mode->height;
861 + try_fmt->code = MEDIA_BUS_FMT_Y10_1X10;
862 + try_fmt->field = V4L2_FIELD_NONE;
863 + try_fmt->colorspace = V4L2_COLORSPACE_RAW;
864 + try_fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(try_fmt->colorspace);
865 + try_fmt->quantization =
866 + V4L2_MAP_QUANTIZATION_DEFAULT(true, try_fmt->colorspace,
867 + try_fmt->ycbcr_enc);
868 + try_fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(try_fmt->colorspace);
869 +
870 + mutex_unlock(&ov2311->mutex);
871 + /* No crop or compose */
872 +
873 + return 0;
874 +}
875 +
876 +static const struct dev_pm_ops ov2311_pm_ops = {
877 + SET_RUNTIME_PM_OPS(ov2311_runtime_suspend, ov2311_runtime_resume, NULL)
878 + SET_RUNTIME_PM_OPS(ov2311_power_off, ov2311_power_on, NULL)
879 +};
880 +
881 +static const struct v4l2_subdev_internal_ops ov2311_internal_ops = {
882 + .open = ov2311_open,
883 +};
884 +
885 +static const struct v4l2_subdev_core_ops ov2311_core_ops = {
886 + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
887 + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
888 +};
889 +
890 +static const struct v4l2_subdev_video_ops ov2311_video_ops = {
891 + .s_stream = ov2311_s_stream,
892 +};
893 +
894 +static const struct v4l2_subdev_pad_ops ov2311_pad_ops = {
895 + .enum_mbus_code = ov2311_enum_mbus_code,
896 + .enum_frame_size = ov2311_enum_frame_sizes,
897 + .get_fmt = ov2311_get_fmt,
898 + .set_fmt = ov2311_set_fmt,
899 + .get_selection = ov2311_get_selection,
900 +};
901 +
902 +static const struct v4l2_subdev_ops ov2311_subdev_ops = {
903 + .core = &ov2311_core_ops,
904 + .video = &ov2311_video_ops,
905 + .pad = &ov2311_pad_ops,
906 +};
907 +
908 +static int ov2311_set_ctrl(struct v4l2_ctrl *ctrl)
909 +{
910 + struct ov2311 *ov2311 = container_of(ctrl->handler,
911 + struct ov2311, ctrl_handler);
912 + struct i2c_client *client = ov2311->client;
913 + s64 max;
914 + int ret = 0;
915 +
916 + /* Propagate change of current control to all related controls */
917 + switch (ctrl->id) {
918 + case V4L2_CID_VBLANK:
919 + /* Update max exposure while meeting expected vblanking */
920 + max = ov2311->cur_mode->height + ctrl->val - 4;
921 + __v4l2_ctrl_modify_range(ov2311->exposure,
922 + ov2311->exposure->minimum, max,
923 + ov2311->exposure->step,
924 + ov2311->exposure->default_value);
925 + break;
926 + }
927 +
928 + if (pm_runtime_get(&client->dev) <= 0)
929 + return 0;
930 +
931 + switch (ctrl->id) {
932 + case V4L2_CID_EXPOSURE:
933 + ret = ov2311_write_reg(ov2311->client, OV2311_REG_EXPOSURE,
934 + OV2311_REG_VALUE_16BIT, ctrl->val);
935 + break;
936 + case V4L2_CID_ANALOGUE_GAIN:
937 + ret = ov2311_write_reg(ov2311->client, OV2311_REG_GAIN_H,
938 + OV2311_REG_VALUE_08BIT,
939 + (ctrl->val >> OV2311_GAIN_H_SHIFT) &
940 + OV2311_GAIN_H_MASK);
941 + ret |= ov2311_write_reg(ov2311->client, OV2311_REG_GAIN_L,
942 + OV2311_REG_VALUE_08BIT,
943 + ctrl->val & OV2311_GAIN_L_MASK);
944 + break;
945 + case V4L2_CID_VBLANK:
946 + ret = ov2311_write_reg(ov2311->client, OV2311_REG_VTS,
947 + OV2311_REG_VALUE_16BIT,
948 + ctrl->val + ov2311->cur_mode->height);
949 + break;
950 + case V4L2_CID_TEST_PATTERN:
951 + ret = ov2311_enable_test_pattern(ov2311, ctrl->val);
952 + break;
953 + case V4L2_CID_HFLIP:
954 + ret = ov2311_write_reg(ov2311->client, OV2311_REG_H_FLIP,
955 + OV2311_REG_VALUE_08BIT,
956 + ctrl->val ? OV2311_FLIP_BIT : 0);
957 + break;
958 + case V4L2_CID_VFLIP:
959 + ret = ov2311_write_reg(ov2311->client, OV2311_REG_V_FLIP,
960 + OV2311_REG_VALUE_08BIT,
961 + ctrl->val ? OV2311_FLIP_BIT : 0);
962 + break;
963 + default:
964 + dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
965 + __func__, ctrl->id, ctrl->val);
966 + break;
967 + }
968 +
969 + pm_runtime_put(&client->dev);
970 +
971 + return ret;
972 +}
973 +
974 +static const struct v4l2_ctrl_ops ov2311_ctrl_ops = {
975 + .s_ctrl = ov2311_set_ctrl,
976 +};
977 +
978 +static int ov2311_initialize_controls(struct ov2311 *ov2311)
979 +{
980 + struct v4l2_fwnode_device_properties props;
981 + const struct ov2311_mode *mode;
982 + struct v4l2_ctrl_handler *handler;
983 + struct v4l2_ctrl *ctrl;
984 + s64 exposure_max, vblank_def;
985 + u32 h_blank;
986 + int ret;
987 +
988 + handler = &ov2311->ctrl_handler;
989 + mode = ov2311->cur_mode;
990 + ret = v4l2_ctrl_handler_init(handler, 11);
991 + if (ret)
992 + return ret;
993 + handler->lock = &ov2311->mutex;
994 +
995 + ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
996 + 0, 0, link_freq_menu_items);
997 + if (ctrl)
998 + ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
999 +
1000 + ov2311->pixel_rate = v4l2_ctrl_new_std(handler, NULL,
1001 + V4L2_CID_PIXEL_RATE,
1002 + OV2311_PIXEL_RATE_10BIT,
1003 + OV2311_PIXEL_RATE_10BIT, 1,
1004 + OV2311_PIXEL_RATE_10BIT);
1005 +
1006 + h_blank = mode->hts_def - mode->width;
1007 + ov2311->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
1008 + h_blank, h_blank, 1, h_blank);
1009 + if (ov2311->hblank)
1010 + ov2311->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1011 +
1012 + vblank_def = mode->vts_def - mode->height;
1013 + ov2311->vblank = v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
1014 + V4L2_CID_VBLANK, vblank_def,
1015 + OV2311_VTS_MAX - mode->height, 1,
1016 + vblank_def);
1017 +
1018 + exposure_max = mode->vts_def - 4;
1019 + ov2311->exposure = v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
1020 + V4L2_CID_EXPOSURE,
1021 + OV2311_EXPOSURE_MIN, exposure_max,
1022 + OV2311_EXPOSURE_STEP,
1023 + mode->exp_def);
1024 +
1025 + v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1026 + OV2311_GAIN_MIN, OV2311_GAIN_MAX, OV2311_GAIN_STEP,
1027 + OV2311_GAIN_DEFAULT);
1028 +
1029 + v4l2_ctrl_new_std_menu_items(handler, &ov2311_ctrl_ops,
1030 + V4L2_CID_TEST_PATTERN,
1031 + ARRAY_SIZE(ov2311_test_pattern_menu) - 1,
1032 + 0, 0, ov2311_test_pattern_menu);
1033 +
1034 + v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
1035 + V4L2_CID_HFLIP, 0, 1, 1, 0);
1036 +
1037 + v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
1038 + V4L2_CID_VFLIP, 0, 1, 1, 0);
1039 +
1040 + ret = v4l2_fwnode_device_parse(&ov2311->client->dev, &props);
1041 + if (ret)
1042 + goto err_free_handler;
1043 +
1044 + ret = v4l2_ctrl_new_fwnode_properties(handler, &ov2311_ctrl_ops,
1045 + &props);
1046 + if (ret)
1047 + goto err_free_handler;
1048 +
1049 + if (handler->error) {
1050 + ret = handler->error;
1051 + dev_err(&ov2311->client->dev,
1052 + "Failed to init controls(%d)\n", ret);
1053 + goto err_free_handler;
1054 + }
1055 +
1056 + ov2311->subdev.ctrl_handler = handler;
1057 +
1058 + return 0;
1059 +
1060 +err_free_handler:
1061 + v4l2_ctrl_handler_free(handler);
1062 +
1063 + return ret;
1064 +}
1065 +
1066 +static int ov2311_check_sensor_id(struct ov2311 *ov2311,
1067 + struct i2c_client *client)
1068 +{
1069 + struct device *dev = &ov2311->client->dev;
1070 + u32 id = 0, id_msb;
1071 + int ret;
1072 +
1073 + ret = ov2311_read_reg(client, OV2311_REG_CHIP_ID + 1,
1074 + OV2311_REG_VALUE_08BIT, &id);
1075 + if (!ret)
1076 + ret = ov2311_read_reg(client, OV2311_REG_CHIP_ID,
1077 + OV2311_REG_VALUE_08BIT, &id_msb);
1078 + id |= (id_msb << 8);
1079 + if (ret || id != CHIP_ID) {
1080 + dev_err(dev, "Unexpected sensor id(%04x), ret(%d)\n", id, ret);
1081 + return -ENODEV;
1082 + }
1083 +
1084 + dev_info(dev, "Detected OV%06x sensor\n", CHIP_ID);
1085 +
1086 + return 0;
1087 +}
1088 +
1089 +static int ov2311_configure_regulators(struct ov2311 *ov2311)
1090 +{
1091 + unsigned int i;
1092 +
1093 + for (i = 0; i < OV2311_NUM_SUPPLIES; i++)
1094 + ov2311->supplies[i].supply = ov2311_supply_names[i];
1095 +
1096 + return devm_regulator_bulk_get(&ov2311->client->dev,
1097 + OV2311_NUM_SUPPLIES,
1098 + ov2311->supplies);
1099 +}
1100 +
1101 +static int ov2311_probe(struct i2c_client *client,
1102 + const struct i2c_device_id *id)
1103 +{
1104 + struct device *dev = &client->dev;
1105 + struct ov2311 *ov2311;
1106 + struct v4l2_subdev *sd;
1107 + int ret;
1108 +
1109 + ov2311 = devm_kzalloc(dev, sizeof(*ov2311), GFP_KERNEL);
1110 + if (!ov2311)
1111 + return -ENOMEM;
1112 +
1113 + ov2311->client = client;
1114 + ov2311->cur_mode = &supported_modes[0];
1115 +
1116 + ov2311->xvclk = devm_clk_get(dev, "xvclk");
1117 + if (IS_ERR(ov2311->xvclk)) {
1118 + dev_err(dev, "Failed to get xvclk\n");
1119 + return -EINVAL;
1120 + }
1121 +
1122 + ov2311->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1123 + GPIOD_OUT_LOW);
1124 + if (IS_ERR(ov2311->reset_gpio))
1125 + dev_warn(dev, "Failed to get reset-gpios\n");
1126 +
1127 + ov2311->pwdn_gpio = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_LOW);
1128 + if (IS_ERR(ov2311->pwdn_gpio))
1129 + dev_warn(dev, "Failed to get pwdn-gpios\n");
1130 +
1131 + ret = ov2311_configure_regulators(ov2311);
1132 + if (ret) {
1133 + dev_err(dev, "Failed to get power regulators\n");
1134 + return ret;
1135 + }
1136 +
1137 + mutex_init(&ov2311->mutex);
1138 +
1139 + sd = &ov2311->subdev;
1140 + v4l2_i2c_subdev_init(sd, client, &ov2311_subdev_ops);
1141 + ret = ov2311_initialize_controls(ov2311);
1142 + if (ret)
1143 + goto err_destroy_mutex;
1144 +
1145 + ret = ov2311_power_on(&client->dev);
1146 + if (ret)
1147 + goto err_free_handler;
1148 +
1149 + ret = ov2311_check_sensor_id(ov2311, client);
1150 + if (ret)
1151 + goto err_power_off;
1152 +
1153 + sd->internal_ops = &ov2311_internal_ops;
1154 + sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1155 +
1156 + ov2311->pad.flags = MEDIA_PAD_FL_SOURCE;
1157 + sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1158 + ret = media_entity_pads_init(&sd->entity, 1, &ov2311->pad);
1159 + if (ret < 0)
1160 + goto err_power_off;
1161 +
1162 + ret = v4l2_async_register_subdev(sd);
1163 + if (ret) {
1164 + dev_err(dev, "v4l2 async register subdev failed\n");
1165 + goto err_clean_entity;
1166 + }
1167 +
1168 + pm_runtime_set_active(dev);
1169 + pm_runtime_enable(dev);
1170 + pm_runtime_idle(dev);
1171 +
1172 + return 0;
1173 +
1174 +err_clean_entity:
1175 + media_entity_cleanup(&sd->entity);
1176 +err_power_off:
1177 + ov2311_power_off(&client->dev);
1178 +err_free_handler:
1179 + v4l2_ctrl_handler_free(&ov2311->ctrl_handler);
1180 +err_destroy_mutex:
1181 + mutex_destroy(&ov2311->mutex);
1182 +
1183 + return ret;
1184 +}
1185 +
1186 +static void ov2311_remove(struct i2c_client *client)
1187 +{
1188 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1189 + struct ov2311 *ov2311 = to_ov2311(sd);
1190 +
1191 + v4l2_async_unregister_subdev(sd);
1192 + media_entity_cleanup(&sd->entity);
1193 + v4l2_ctrl_handler_free(&ov2311->ctrl_handler);
1194 + mutex_destroy(&ov2311->mutex);
1195 +
1196 + pm_runtime_disable(&client->dev);
1197 + if (!pm_runtime_status_suspended(&client->dev))
1198 + ov2311_power_off(&client->dev);
1199 + pm_runtime_set_suspended(&client->dev);
1200 +}
1201 +
1202 +static const struct of_device_id ov2311_of_match[] = {
1203 + { .compatible = "ovti,ov2311" },
1204 + {},
1205 +};
1206 +MODULE_DEVICE_TABLE(of, ov2311_of_match);
1207 +
1208 +static const struct i2c_device_id ov2311_match_id[] = {
1209 + { "ovti,ov2311", 0 },
1210 + { },
1211 +};
1212 +
1213 +static struct i2c_driver ov2311_i2c_driver = {
1214 + .driver = {
1215 + .name = OV2311_NAME,
1216 + .pm = &ov2311_pm_ops,
1217 + .of_match_table = of_match_ptr(ov2311_of_match),
1218 + },
1219 + .probe = &ov2311_probe,
1220 + .remove = &ov2311_remove,
1221 + .id_table = ov2311_match_id,
1222 +};
1223 +
1224 +module_i2c_driver(ov2311_i2c_driver);
1225 +
1226 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com");
1227 +MODULE_DESCRIPTION("OmniVision OV2311 sensor driver");
1228 +MODULE_LICENSE("GPL v2");