xref: /openbmc/linux/drivers/media/i2c/ov9282.c (revision 408a2a05)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * OmniVision ov9282 Camera Sensor Driver
4  *
5  * Copyright (C) 2021 Intel Corporation
6  */
7 #include <asm/unaligned.h>
8 
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regulator/consumer.h>
15 
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-event.h>
18 #include <media/v4l2-fwnode.h>
19 #include <media/v4l2-subdev.h>
20 
21 /* Streaming Mode */
22 #define OV9282_REG_MODE_SELECT	0x0100
23 #define OV9282_MODE_STANDBY	0x00
24 #define OV9282_MODE_STREAMING	0x01
25 
26 #define OV9282_REG_PLL_CTRL_0D	0x030d
27 #define OV9282_PLL_CTRL_0D_RAW8		0x60
28 #define OV9282_PLL_CTRL_0D_RAW10	0x50
29 
30 #define OV9282_REG_TIMING_HTS	0x380c
31 #define OV9282_TIMING_HTS_MAX	0x7fff
32 
33 /* Lines per frame */
34 #define OV9282_REG_LPFR		0x380e
35 
36 /* Chip ID */
37 #define OV9282_REG_ID		0x300a
38 #define OV9282_ID		0x9281
39 
40 /* Exposure control */
41 #define OV9282_REG_EXPOSURE	0x3500
42 #define OV9282_EXPOSURE_MIN	1
43 #define OV9282_EXPOSURE_OFFSET	12
44 #define OV9282_EXPOSURE_STEP	1
45 #define OV9282_EXPOSURE_DEFAULT	0x0282
46 
47 /* Analog gain control */
48 #define OV9282_REG_AGAIN	0x3509
49 #define OV9282_AGAIN_MIN	0x10
50 #define OV9282_AGAIN_MAX	0xff
51 #define OV9282_AGAIN_STEP	1
52 #define OV9282_AGAIN_DEFAULT	0x10
53 
54 /* Group hold register */
55 #define OV9282_REG_HOLD		0x3308
56 
57 #define OV9282_REG_ANA_CORE_2	0x3662
58 #define OV9282_ANA_CORE2_RAW8	0x07
59 #define OV9282_ANA_CORE2_RAW10	0x05
60 
61 #define OV9282_REG_TIMING_FORMAT_1	0x3820
62 #define OV9282_REG_TIMING_FORMAT_2	0x3821
63 #define OV9282_FLIP_BIT			BIT(2)
64 
65 #define OV9282_REG_MIPI_CTRL00	0x4800
66 #define OV9282_GATED_CLOCK	BIT(5)
67 
68 /* Input clock rate */
69 #define OV9282_INCLK_RATE	24000000
70 
71 /* CSI2 HW configuration */
72 #define OV9282_LINK_FREQ	400000000
73 #define OV9282_NUM_DATA_LANES	2
74 
75 /* Pixel rate */
76 #define OV9282_PIXEL_RATE_10BIT		(OV9282_LINK_FREQ * 2 * \
77 					 OV9282_NUM_DATA_LANES / 10)
78 #define OV9282_PIXEL_RATE_8BIT		(OV9282_LINK_FREQ * 2 * \
79 					 OV9282_NUM_DATA_LANES / 8)
80 
81 /*
82  * OV9282 native and active pixel array size.
83  * 8 dummy rows/columns on each edge of a 1280x800 active array
84  */
85 #define OV9282_NATIVE_WIDTH		1296U
86 #define OV9282_NATIVE_HEIGHT		816U
87 #define OV9282_PIXEL_ARRAY_LEFT		8U
88 #define OV9282_PIXEL_ARRAY_TOP		8U
89 #define OV9282_PIXEL_ARRAY_WIDTH	1280U
90 #define OV9282_PIXEL_ARRAY_HEIGHT	800U
91 
92 #define OV9282_REG_MIN		0x00
93 #define OV9282_REG_MAX		0xfffff
94 
95 static const char * const ov9282_supply_names[] = {
96 	"avdd",		/* Analog power */
97 	"dovdd",	/* Digital I/O power */
98 	"dvdd",		/* Digital core power */
99 };
100 
101 #define OV9282_NUM_SUPPLIES ARRAY_SIZE(ov9282_supply_names)
102 
103 /**
104  * struct ov9282_reg - ov9282 sensor register
105  * @address: Register address
106  * @val: Register value
107  */
108 struct ov9282_reg {
109 	u16 address;
110 	u8 val;
111 };
112 
113 /**
114  * struct ov9282_reg_list - ov9282 sensor register list
115  * @num_of_regs: Number of registers in the list
116  * @regs: Pointer to register list
117  */
118 struct ov9282_reg_list {
119 	u32 num_of_regs;
120 	const struct ov9282_reg *regs;
121 };
122 
123 /**
124  * struct ov9282_mode - ov9282 sensor mode structure
125  * @width: Frame width
126  * @height: Frame height
127  * @hblank_min: Minimum horizontal blanking in lines for non-continuous[0] and
128  *		continuous[1] clock modes
129  * @vblank: Vertical blanking in lines
130  * @vblank_min: Minimum vertical blanking in lines
131  * @vblank_max: Maximum vertical blanking in lines
132  * @link_freq_idx: Link frequency index
133  * @reg_list: Register list for sensor mode
134  */
135 struct ov9282_mode {
136 	u32 width;
137 	u32 height;
138 	u32 hblank_min[2];
139 	u32 vblank;
140 	u32 vblank_min;
141 	u32 vblank_max;
142 	u32 link_freq_idx;
143 	struct v4l2_rect crop;
144 	struct ov9282_reg_list reg_list;
145 };
146 
147 /**
148  * struct ov9282 - ov9282 sensor device structure
149  * @dev: Pointer to generic device
150  * @client: Pointer to i2c client
151  * @sd: V4L2 sub-device
152  * @pad: Media pad. Only one pad supported
153  * @reset_gpio: Sensor reset gpio
154  * @inclk: Sensor input clock
155  * @ctrl_handler: V4L2 control handler
156  * @link_freq_ctrl: Pointer to link frequency control
157  * @hblank_ctrl: Pointer to horizontal blanking control
158  * @vblank_ctrl: Pointer to vertical blanking control
159  * @exp_ctrl: Pointer to exposure control
160  * @again_ctrl: Pointer to analog gain control
161  * @vblank: Vertical blanking in lines
162  * @cur_mode: Pointer to current selected sensor mode
163  * @code: Mbus code currently selected
164  * @mutex: Mutex for serializing sensor controls
165  * @streaming: Flag indicating streaming state
166  */
167 struct ov9282 {
168 	struct device *dev;
169 	struct i2c_client *client;
170 	struct v4l2_subdev sd;
171 	struct media_pad pad;
172 	struct gpio_desc *reset_gpio;
173 	struct clk *inclk;
174 	struct regulator_bulk_data supplies[OV9282_NUM_SUPPLIES];
175 	struct v4l2_ctrl_handler ctrl_handler;
176 	struct v4l2_ctrl *link_freq_ctrl;
177 	struct v4l2_ctrl *hblank_ctrl;
178 	struct v4l2_ctrl *vblank_ctrl;
179 	struct {
180 		struct v4l2_ctrl *exp_ctrl;
181 		struct v4l2_ctrl *again_ctrl;
182 	};
183 	struct v4l2_ctrl *pixel_rate;
184 	u32 vblank;
185 	bool noncontinuous_clock;
186 	const struct ov9282_mode *cur_mode;
187 	u32 code;
188 	struct mutex mutex;
189 	bool streaming;
190 };
191 
192 static const s64 link_freq[] = {
193 	OV9282_LINK_FREQ,
194 };
195 
196 /*
197  * Common registers
198  *
199  * Note: Do NOT include a software reset (0x0103, 0x01) in any of these
200  * register arrays as some settings are written as part of ov9282_power_on,
201  * and the reset will clear them.
202  */
203 static const struct ov9282_reg common_regs[] = {
204 	{0x0302, 0x32},
205 	{0x030e, 0x02},
206 	{0x3001, 0x00},
207 	{0x3004, 0x00},
208 	{0x3005, 0x00},
209 	{0x3006, 0x04},
210 	{0x3011, 0x0a},
211 	{0x3013, 0x18},
212 	{0x301c, 0xf0},
213 	{0x3022, 0x01},
214 	{0x3030, 0x10},
215 	{0x3039, 0x32},
216 	{0x303a, 0x00},
217 	{0x3503, 0x08},
218 	{0x3505, 0x8c},
219 	{0x3507, 0x03},
220 	{0x3508, 0x00},
221 	{0x3610, 0x80},
222 	{0x3611, 0xa0},
223 	{0x3620, 0x6e},
224 	{0x3632, 0x56},
225 	{0x3633, 0x78},
226 	{0x3666, 0x00},
227 	{0x366f, 0x5a},
228 	{0x3680, 0x84},
229 	{0x3712, 0x80},
230 	{0x372d, 0x22},
231 	{0x3731, 0x80},
232 	{0x3732, 0x30},
233 	{0x377d, 0x22},
234 	{0x3788, 0x02},
235 	{0x3789, 0xa4},
236 	{0x378a, 0x00},
237 	{0x378b, 0x4a},
238 	{0x3799, 0x20},
239 	{0x3881, 0x42},
240 	{0x38a8, 0x02},
241 	{0x38a9, 0x80},
242 	{0x38b1, 0x00},
243 	{0x38c4, 0x00},
244 	{0x38c5, 0xc0},
245 	{0x38c6, 0x04},
246 	{0x38c7, 0x80},
247 	{0x3920, 0xff},
248 	{0x4010, 0x40},
249 	{0x4043, 0x40},
250 	{0x4307, 0x30},
251 	{0x4317, 0x00},
252 	{0x4501, 0x00},
253 	{0x450a, 0x08},
254 	{0x4601, 0x04},
255 	{0x470f, 0x00},
256 	{0x4f07, 0x00},
257 	{0x5000, 0x9f},
258 	{0x5001, 0x00},
259 	{0x5e00, 0x00},
260 	{0x5d00, 0x07},
261 	{0x5d01, 0x00},
262 	{0x0101, 0x01},
263 	{0x1000, 0x03},
264 	{0x5a08, 0x84},
265 };
266 
267 struct ov9282_reg_list common_regs_list = {
268 	.num_of_regs = ARRAY_SIZE(common_regs),
269 	.regs = common_regs,
270 };
271 
272 #define MODE_1280_800		0
273 #define MODE_1280_720		1
274 #define MODE_640_400		2
275 
276 #define DEFAULT_MODE		MODE_1280_720
277 
278 /* Sensor mode registers */
279 static const struct ov9282_reg mode_1280x800_regs[] = {
280 	{0x3778, 0x00},
281 	{0x3800, 0x00},
282 	{0x3801, 0x00},
283 	{0x3802, 0x00},
284 	{0x3803, 0x00},
285 	{0x3804, 0x05},
286 	{0x3805, 0x0f},
287 	{0x3806, 0x03},
288 	{0x3807, 0x2f},
289 	{0x3808, 0x05},
290 	{0x3809, 0x00},
291 	{0x380a, 0x03},
292 	{0x380b, 0x20},
293 	{0x3810, 0x00},
294 	{0x3811, 0x08},
295 	{0x3812, 0x00},
296 	{0x3813, 0x08},
297 	{0x3814, 0x11},
298 	{0x3815, 0x11},
299 	{0x3820, 0x40},
300 	{0x3821, 0x00},
301 	{0x4003, 0x40},
302 	{0x4008, 0x04},
303 	{0x4009, 0x0b},
304 	{0x400c, 0x00},
305 	{0x400d, 0x07},
306 	{0x4507, 0x00},
307 	{0x4509, 0x00},
308 };
309 
310 static const struct ov9282_reg mode_1280x720_regs[] = {
311 	{0x3778, 0x00},
312 	{0x3800, 0x00},
313 	{0x3801, 0x00},
314 	{0x3802, 0x00},
315 	{0x3803, 0x00},
316 	{0x3804, 0x05},
317 	{0x3805, 0x0f},
318 	{0x3806, 0x02},
319 	{0x3807, 0xdf},
320 	{0x3808, 0x05},
321 	{0x3809, 0x00},
322 	{0x380a, 0x02},
323 	{0x380b, 0xd0},
324 	{0x3810, 0x00},
325 	{0x3811, 0x08},
326 	{0x3812, 0x00},
327 	{0x3813, 0x08},
328 	{0x3814, 0x11},
329 	{0x3815, 0x11},
330 	{0x3820, 0x3c},
331 	{0x3821, 0x84},
332 	{0x4003, 0x40},
333 	{0x4008, 0x02},
334 	{0x4009, 0x05},
335 	{0x400c, 0x00},
336 	{0x400d, 0x03},
337 	{0x4507, 0x00},
338 	{0x4509, 0x80},
339 };
340 
341 static const struct ov9282_reg mode_640x400_regs[] = {
342 	{0x3778, 0x10},
343 	{0x3800, 0x00},
344 	{0x3801, 0x00},
345 	{0x3802, 0x00},
346 	{0x3803, 0x00},
347 	{0x3804, 0x05},
348 	{0x3805, 0x0f},
349 	{0x3806, 0x03},
350 	{0x3807, 0x2f},
351 	{0x3808, 0x02},
352 	{0x3809, 0x80},
353 	{0x380a, 0x01},
354 	{0x380b, 0x90},
355 	{0x3810, 0x00},
356 	{0x3811, 0x04},
357 	{0x3812, 0x00},
358 	{0x3813, 0x04},
359 	{0x3814, 0x31},
360 	{0x3815, 0x22},
361 	{0x3820, 0x60},
362 	{0x3821, 0x01},
363 	{0x4008, 0x02},
364 	{0x4009, 0x05},
365 	{0x400c, 0x00},
366 	{0x400d, 0x03},
367 	{0x4507, 0x03},
368 	{0x4509, 0x80},
369 };
370 
371 /* Supported sensor mode configurations */
372 static const struct ov9282_mode supported_modes[] = {
373 	[MODE_1280_800] = {
374 		.width = 1280,
375 		.height = 800,
376 		.hblank_min = { 250, 176 },
377 		.vblank = 1022,
378 		.vblank_min = 110,
379 		.vblank_max = 51540,
380 		.link_freq_idx = 0,
381 		.crop = {
382 			.left = OV9282_PIXEL_ARRAY_LEFT,
383 			.top = OV9282_PIXEL_ARRAY_TOP,
384 			.width = 1280,
385 			.height = 800
386 		},
387 		.reg_list = {
388 			.num_of_regs = ARRAY_SIZE(mode_1280x800_regs),
389 			.regs = mode_1280x800_regs,
390 		},
391 	},
392 	[MODE_1280_720] = {
393 		.width = 1280,
394 		.height = 720,
395 		.hblank_min = { 250, 176 },
396 		.vblank = 1022,
397 		.vblank_min = 41,
398 		.vblank_max = 51540,
399 		.link_freq_idx = 0,
400 		.crop = {
401 			/*
402 			 * Note that this mode takes the top 720 lines from the
403 			 * 800 of the sensor. It does not take a middle crop.
404 			 */
405 			.left = OV9282_PIXEL_ARRAY_LEFT,
406 			.top = OV9282_PIXEL_ARRAY_TOP,
407 			.width = 1280,
408 			.height = 720
409 		},
410 		.reg_list = {
411 			.num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
412 			.regs = mode_1280x720_regs,
413 		},
414 	},
415 	[MODE_640_400] = {
416 		.width = 640,
417 		.height = 400,
418 		.hblank_min = { 890, 816 },
419 		.vblank = 1022,
420 		.vblank_min = 22,
421 		.vblank_max = 51540,
422 		.link_freq_idx = 0,
423 		.crop = {
424 			.left = OV9282_PIXEL_ARRAY_LEFT,
425 			.top = OV9282_PIXEL_ARRAY_TOP,
426 			.width = 1280,
427 			.height = 800
428 		},
429 		.reg_list = {
430 			.num_of_regs = ARRAY_SIZE(mode_640x400_regs),
431 			.regs = mode_640x400_regs,
432 		},
433 	},
434 };
435 
436 /**
437  * to_ov9282() - ov9282 V4L2 sub-device to ov9282 device.
438  * @subdev: pointer to ov9282 V4L2 sub-device
439  *
440  * Return: pointer to ov9282 device
441  */
442 static inline struct ov9282 *to_ov9282(struct v4l2_subdev *subdev)
443 {
444 	return container_of(subdev, struct ov9282, sd);
445 }
446 
447 /**
448  * ov9282_read_reg() - Read registers.
449  * @ov9282: pointer to ov9282 device
450  * @reg: register address
451  * @len: length of bytes to read. Max supported bytes is 4
452  * @val: pointer to register value to be filled.
453  *
454  * Return: 0 if successful, error code otherwise.
455  */
456 static int ov9282_read_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 *val)
457 {
458 	struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
459 	struct i2c_msg msgs[2] = {0};
460 	u8 addr_buf[2] = {0};
461 	u8 data_buf[4] = {0};
462 	int ret;
463 
464 	if (WARN_ON(len > 4))
465 		return -EINVAL;
466 
467 	put_unaligned_be16(reg, addr_buf);
468 
469 	/* Write register address */
470 	msgs[0].addr = client->addr;
471 	msgs[0].flags = 0;
472 	msgs[0].len = ARRAY_SIZE(addr_buf);
473 	msgs[0].buf = addr_buf;
474 
475 	/* Read data from register */
476 	msgs[1].addr = client->addr;
477 	msgs[1].flags = I2C_M_RD;
478 	msgs[1].len = len;
479 	msgs[1].buf = &data_buf[4 - len];
480 
481 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
482 	if (ret != ARRAY_SIZE(msgs))
483 		return -EIO;
484 
485 	*val = get_unaligned_be32(data_buf);
486 
487 	return 0;
488 }
489 
490 /**
491  * ov9282_write_reg() - Write register
492  * @ov9282: pointer to ov9282 device
493  * @reg: register address
494  * @len: length of bytes. Max supported bytes is 4
495  * @val: register value
496  *
497  * Return: 0 if successful, error code otherwise.
498  */
499 static int ov9282_write_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 val)
500 {
501 	struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
502 	u8 buf[6] = {0};
503 
504 	if (WARN_ON(len > 4))
505 		return -EINVAL;
506 
507 	put_unaligned_be16(reg, buf);
508 	put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
509 	if (i2c_master_send(client, buf, len + 2) != len + 2)
510 		return -EIO;
511 
512 	return 0;
513 }
514 
515 /**
516  * ov9282_write_regs() - Write a list of registers
517  * @ov9282: pointer to ov9282 device
518  * @regs: list of registers to be written
519  * @len: length of registers array
520  *
521  * Return: 0 if successful, error code otherwise.
522  */
523 static int ov9282_write_regs(struct ov9282 *ov9282,
524 			     const struct ov9282_reg *regs, u32 len)
525 {
526 	unsigned int i;
527 	int ret;
528 
529 	for (i = 0; i < len; i++) {
530 		ret = ov9282_write_reg(ov9282, regs[i].address, 1, regs[i].val);
531 		if (ret)
532 			return ret;
533 	}
534 
535 	return 0;
536 }
537 
538 /**
539  * ov9282_update_controls() - Update control ranges based on streaming mode
540  * @ov9282: pointer to ov9282 device
541  * @mode: pointer to ov9282_mode sensor mode
542  * @fmt: pointer to the requested mode
543  *
544  * Return: 0 if successful, error code otherwise.
545  */
546 static int ov9282_update_controls(struct ov9282 *ov9282,
547 				  const struct ov9282_mode *mode,
548 				  const struct v4l2_subdev_format *fmt)
549 {
550 	u32 hblank_min;
551 	s64 pixel_rate;
552 	int ret;
553 
554 	ret = __v4l2_ctrl_s_ctrl(ov9282->link_freq_ctrl, mode->link_freq_idx);
555 	if (ret)
556 		return ret;
557 
558 	pixel_rate = (fmt->format.code == MEDIA_BUS_FMT_Y10_1X10) ?
559 		OV9282_PIXEL_RATE_10BIT : OV9282_PIXEL_RATE_8BIT;
560 	ret = __v4l2_ctrl_modify_range(ov9282->pixel_rate, pixel_rate,
561 				       pixel_rate, 1, pixel_rate);
562 	if (ret)
563 		return ret;
564 
565 	hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1];
566 	ret =  __v4l2_ctrl_modify_range(ov9282->hblank_ctrl, hblank_min,
567 					OV9282_TIMING_HTS_MAX - mode->width, 1,
568 					hblank_min);
569 	if (ret)
570 		return ret;
571 
572 	return __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min,
573 					mode->vblank_max, 1, mode->vblank);
574 }
575 
576 /**
577  * ov9282_update_exp_gain() - Set updated exposure and gain
578  * @ov9282: pointer to ov9282 device
579  * @exposure: updated exposure value
580  * @gain: updated analog gain value
581  *
582  * Return: 0 if successful, error code otherwise.
583  */
584 static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
585 {
586 	int ret;
587 
588 	dev_dbg(ov9282->dev, "Set exp %u, analog gain %u",
589 		exposure, gain);
590 
591 	ret = ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 1);
592 	if (ret)
593 		return ret;
594 
595 	ret = ov9282_write_reg(ov9282, OV9282_REG_EXPOSURE, 3, exposure << 4);
596 	if (ret)
597 		goto error_release_group_hold;
598 
599 	ret = ov9282_write_reg(ov9282, OV9282_REG_AGAIN, 1, gain);
600 
601 error_release_group_hold:
602 	ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 0);
603 
604 	return ret;
605 }
606 
607 static int ov9282_set_ctrl_hflip(struct ov9282 *ov9282, int value)
608 {
609 	u32 current_val;
610 	int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1,
611 				  &current_val);
612 	if (ret)
613 		return ret;
614 
615 	if (value)
616 		current_val |= OV9282_FLIP_BIT;
617 	else
618 		current_val &= ~OV9282_FLIP_BIT;
619 
620 	return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1,
621 				current_val);
622 }
623 
624 static int ov9282_set_ctrl_vflip(struct ov9282 *ov9282, int value)
625 {
626 	u32 current_val;
627 	int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1,
628 				  &current_val);
629 	if (ret)
630 		return ret;
631 
632 	if (value)
633 		current_val |= OV9282_FLIP_BIT;
634 	else
635 		current_val &= ~OV9282_FLIP_BIT;
636 
637 	return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1,
638 				current_val);
639 }
640 
641 /**
642  * ov9282_set_ctrl() - Set subdevice control
643  * @ctrl: pointer to v4l2_ctrl structure
644  *
645  * Supported controls:
646  * - V4L2_CID_VBLANK
647  * - cluster controls:
648  *   - V4L2_CID_ANALOGUE_GAIN
649  *   - V4L2_CID_EXPOSURE
650  *
651  * Return: 0 if successful, error code otherwise.
652  */
653 static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
654 {
655 	struct ov9282 *ov9282 =
656 		container_of(ctrl->handler, struct ov9282, ctrl_handler);
657 	u32 analog_gain;
658 	u32 exposure;
659 	u32 lpfr;
660 	int ret;
661 
662 	switch (ctrl->id) {
663 	case V4L2_CID_VBLANK:
664 		ov9282->vblank = ov9282->vblank_ctrl->val;
665 
666 		dev_dbg(ov9282->dev, "Received vblank %u, new lpfr %u",
667 			ov9282->vblank,
668 			ov9282->vblank + ov9282->cur_mode->height);
669 
670 		ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl,
671 					       OV9282_EXPOSURE_MIN,
672 					       ov9282->vblank +
673 					       ov9282->cur_mode->height -
674 					       OV9282_EXPOSURE_OFFSET,
675 					       1, OV9282_EXPOSURE_DEFAULT);
676 		break;
677 	}
678 
679 	/* Set controls only if sensor is in power on state */
680 	if (!pm_runtime_get_if_in_use(ov9282->dev))
681 		return 0;
682 
683 	switch (ctrl->id) {
684 	case V4L2_CID_EXPOSURE:
685 		exposure = ctrl->val;
686 		analog_gain = ov9282->again_ctrl->val;
687 
688 		dev_dbg(ov9282->dev, "Received exp %u, analog gain %u",
689 			exposure, analog_gain);
690 
691 		ret = ov9282_update_exp_gain(ov9282, exposure, analog_gain);
692 		break;
693 	case V4L2_CID_VBLANK:
694 		lpfr = ov9282->vblank + ov9282->cur_mode->height;
695 		ret = ov9282_write_reg(ov9282, OV9282_REG_LPFR, 2, lpfr);
696 		break;
697 	case V4L2_CID_HFLIP:
698 		ret = ov9282_set_ctrl_hflip(ov9282, ctrl->val);
699 		break;
700 	case V4L2_CID_VFLIP:
701 		ret = ov9282_set_ctrl_vflip(ov9282, ctrl->val);
702 		break;
703 	case V4L2_CID_HBLANK:
704 		ret = ov9282_write_reg(ov9282, OV9282_REG_TIMING_HTS, 2,
705 				       (ctrl->val + ov9282->cur_mode->width) >> 1);
706 		break;
707 	default:
708 		dev_err(ov9282->dev, "Invalid control %d", ctrl->id);
709 		ret = -EINVAL;
710 	}
711 
712 	pm_runtime_put(ov9282->dev);
713 
714 	return ret;
715 }
716 
717 /* V4l2 subdevice control ops*/
718 static const struct v4l2_ctrl_ops ov9282_ctrl_ops = {
719 	.s_ctrl = ov9282_set_ctrl,
720 };
721 
722 /**
723  * ov9282_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
724  * @sd: pointer to ov9282 V4L2 sub-device structure
725  * @sd_state: V4L2 sub-device configuration
726  * @code: V4L2 sub-device code enumeration need to be filled
727  *
728  * Return: 0 if successful, error code otherwise.
729  */
730 static int ov9282_enum_mbus_code(struct v4l2_subdev *sd,
731 				 struct v4l2_subdev_state *sd_state,
732 				 struct v4l2_subdev_mbus_code_enum *code)
733 {
734 	switch (code->index) {
735 	case 0:
736 		code->code = MEDIA_BUS_FMT_Y10_1X10;
737 		break;
738 	case 1:
739 		code->code = MEDIA_BUS_FMT_Y8_1X8;
740 		break;
741 	default:
742 		return -EINVAL;
743 	}
744 
745 	return 0;
746 }
747 
748 /**
749  * ov9282_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
750  * @sd: pointer to ov9282 V4L2 sub-device structure
751  * @sd_state: V4L2 sub-device configuration
752  * @fsize: V4L2 sub-device size enumeration need to be filled
753  *
754  * Return: 0 if successful, error code otherwise.
755  */
756 static int ov9282_enum_frame_size(struct v4l2_subdev *sd,
757 				  struct v4l2_subdev_state *sd_state,
758 				  struct v4l2_subdev_frame_size_enum *fsize)
759 {
760 	if (fsize->index >= ARRAY_SIZE(supported_modes))
761 		return -EINVAL;
762 
763 	if (fsize->code != MEDIA_BUS_FMT_Y10_1X10 &&
764 	    fsize->code != MEDIA_BUS_FMT_Y8_1X8)
765 		return -EINVAL;
766 
767 	fsize->min_width = supported_modes[fsize->index].width;
768 	fsize->max_width = fsize->min_width;
769 	fsize->min_height = supported_modes[fsize->index].height;
770 	fsize->max_height = fsize->min_height;
771 
772 	return 0;
773 }
774 
775 /**
776  * ov9282_fill_pad_format() - Fill subdevice pad format
777  *                            from selected sensor mode
778  * @ov9282: pointer to ov9282 device
779  * @mode: pointer to ov9282_mode sensor mode
780  * @code: mbus code to be stored
781  * @fmt: V4L2 sub-device format need to be filled
782  */
783 static void ov9282_fill_pad_format(struct ov9282 *ov9282,
784 				   const struct ov9282_mode *mode,
785 				   u32 code,
786 				   struct v4l2_subdev_format *fmt)
787 {
788 	fmt->format.width = mode->width;
789 	fmt->format.height = mode->height;
790 	fmt->format.code = code;
791 	fmt->format.field = V4L2_FIELD_NONE;
792 	fmt->format.colorspace = V4L2_COLORSPACE_RAW;
793 	fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
794 	fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
795 	fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
796 }
797 
798 /**
799  * ov9282_get_pad_format() - Get subdevice pad format
800  * @sd: pointer to ov9282 V4L2 sub-device structure
801  * @sd_state: V4L2 sub-device configuration
802  * @fmt: V4L2 sub-device format need to be set
803  *
804  * Return: 0 if successful, error code otherwise.
805  */
806 static int ov9282_get_pad_format(struct v4l2_subdev *sd,
807 				 struct v4l2_subdev_state *sd_state,
808 				 struct v4l2_subdev_format *fmt)
809 {
810 	struct ov9282 *ov9282 = to_ov9282(sd);
811 
812 	mutex_lock(&ov9282->mutex);
813 
814 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
815 		struct v4l2_mbus_framefmt *framefmt;
816 
817 		framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
818 		fmt->format = *framefmt;
819 	} else {
820 		ov9282_fill_pad_format(ov9282, ov9282->cur_mode, ov9282->code,
821 				       fmt);
822 	}
823 
824 	mutex_unlock(&ov9282->mutex);
825 
826 	return 0;
827 }
828 
829 /**
830  * ov9282_set_pad_format() - Set subdevice pad format
831  * @sd: pointer to ov9282 V4L2 sub-device structure
832  * @sd_state: V4L2 sub-device configuration
833  * @fmt: V4L2 sub-device format need to be set
834  *
835  * Return: 0 if successful, error code otherwise.
836  */
837 static int ov9282_set_pad_format(struct v4l2_subdev *sd,
838 				 struct v4l2_subdev_state *sd_state,
839 				 struct v4l2_subdev_format *fmt)
840 {
841 	struct ov9282 *ov9282 = to_ov9282(sd);
842 	const struct ov9282_mode *mode;
843 	u32 code;
844 	int ret = 0;
845 
846 	mutex_lock(&ov9282->mutex);
847 
848 	mode = v4l2_find_nearest_size(supported_modes,
849 				      ARRAY_SIZE(supported_modes),
850 				      width, height,
851 				      fmt->format.width,
852 				      fmt->format.height);
853 	if (fmt->format.code == MEDIA_BUS_FMT_Y8_1X8)
854 		code = MEDIA_BUS_FMT_Y8_1X8;
855 	else
856 		code = MEDIA_BUS_FMT_Y10_1X10;
857 
858 	ov9282_fill_pad_format(ov9282, mode, code, fmt);
859 
860 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
861 		struct v4l2_mbus_framefmt *framefmt;
862 
863 		framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
864 		*framefmt = fmt->format;
865 	} else {
866 		ret = ov9282_update_controls(ov9282, mode, fmt);
867 		if (!ret) {
868 			ov9282->cur_mode = mode;
869 			ov9282->code = code;
870 		}
871 	}
872 
873 	mutex_unlock(&ov9282->mutex);
874 
875 	return ret;
876 }
877 
878 /**
879  * ov9282_init_pad_cfg() - Initialize sub-device pad configuration
880  * @sd: pointer to ov9282 V4L2 sub-device structure
881  * @sd_state: V4L2 sub-device configuration
882  *
883  * Return: 0 if successful, error code otherwise.
884  */
885 static int ov9282_init_pad_cfg(struct v4l2_subdev *sd,
886 			       struct v4l2_subdev_state *sd_state)
887 {
888 	struct ov9282 *ov9282 = to_ov9282(sd);
889 	struct v4l2_subdev_format fmt = { 0 };
890 
891 	fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
892 	ov9282_fill_pad_format(ov9282, &supported_modes[DEFAULT_MODE],
893 			       ov9282->code, &fmt);
894 
895 	return ov9282_set_pad_format(sd, sd_state, &fmt);
896 }
897 
898 static const struct v4l2_rect *
899 __ov9282_get_pad_crop(struct ov9282 *ov9282,
900 		      struct v4l2_subdev_state *sd_state,
901 		      unsigned int pad, enum v4l2_subdev_format_whence which)
902 {
903 	switch (which) {
904 	case V4L2_SUBDEV_FORMAT_TRY:
905 		return v4l2_subdev_get_try_crop(&ov9282->sd, sd_state, pad);
906 	case V4L2_SUBDEV_FORMAT_ACTIVE:
907 		return &ov9282->cur_mode->crop;
908 	}
909 
910 	return NULL;
911 }
912 
913 static int ov9282_get_selection(struct v4l2_subdev *sd,
914 				struct v4l2_subdev_state *sd_state,
915 				struct v4l2_subdev_selection *sel)
916 {
917 	switch (sel->target) {
918 	case V4L2_SEL_TGT_CROP: {
919 		struct ov9282 *ov9282 = to_ov9282(sd);
920 
921 		mutex_lock(&ov9282->mutex);
922 		sel->r = *__ov9282_get_pad_crop(ov9282, sd_state, sel->pad,
923 						sel->which);
924 		mutex_unlock(&ov9282->mutex);
925 
926 		return 0;
927 	}
928 
929 	case V4L2_SEL_TGT_NATIVE_SIZE:
930 		sel->r.top = 0;
931 		sel->r.left = 0;
932 		sel->r.width = OV9282_NATIVE_WIDTH;
933 		sel->r.height = OV9282_NATIVE_HEIGHT;
934 
935 		return 0;
936 
937 	case V4L2_SEL_TGT_CROP_DEFAULT:
938 	case V4L2_SEL_TGT_CROP_BOUNDS:
939 		sel->r.top = OV9282_PIXEL_ARRAY_TOP;
940 		sel->r.left = OV9282_PIXEL_ARRAY_LEFT;
941 		sel->r.width = OV9282_PIXEL_ARRAY_WIDTH;
942 		sel->r.height = OV9282_PIXEL_ARRAY_HEIGHT;
943 
944 		return 0;
945 	}
946 
947 	return -EINVAL;
948 }
949 
950 /**
951  * ov9282_start_streaming() - Start sensor stream
952  * @ov9282: pointer to ov9282 device
953  *
954  * Return: 0 if successful, error code otherwise.
955  */
956 static int ov9282_start_streaming(struct ov9282 *ov9282)
957 {
958 	const struct ov9282_reg bitdepth_regs[2][2] = {
959 		{
960 			{OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW10},
961 			{OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW10},
962 		}, {
963 			{OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW8},
964 			{OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW8},
965 		}
966 	};
967 	const struct ov9282_reg_list *reg_list;
968 	int bitdepth_index;
969 	int ret;
970 
971 	/* Write common registers */
972 	ret = ov9282_write_regs(ov9282, common_regs_list.regs,
973 				common_regs_list.num_of_regs);
974 	if (ret) {
975 		dev_err(ov9282->dev, "fail to write common registers");
976 		return ret;
977 	}
978 
979 	bitdepth_index = ov9282->code == MEDIA_BUS_FMT_Y10_1X10 ? 0 : 1;
980 	ret = ov9282_write_regs(ov9282, bitdepth_regs[bitdepth_index], 2);
981 	if (ret) {
982 		dev_err(ov9282->dev, "fail to write bitdepth regs");
983 		return ret;
984 	}
985 
986 	/* Write sensor mode registers */
987 	reg_list = &ov9282->cur_mode->reg_list;
988 	ret = ov9282_write_regs(ov9282, reg_list->regs, reg_list->num_of_regs);
989 	if (ret) {
990 		dev_err(ov9282->dev, "fail to write initial registers");
991 		return ret;
992 	}
993 
994 	/* Setup handler will write actual exposure and gain */
995 	ret =  __v4l2_ctrl_handler_setup(ov9282->sd.ctrl_handler);
996 	if (ret) {
997 		dev_err(ov9282->dev, "fail to setup handler");
998 		return ret;
999 	}
1000 
1001 	/* Start streaming */
1002 	ret = ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
1003 			       1, OV9282_MODE_STREAMING);
1004 	if (ret) {
1005 		dev_err(ov9282->dev, "fail to start streaming");
1006 		return ret;
1007 	}
1008 
1009 	return 0;
1010 }
1011 
1012 /**
1013  * ov9282_stop_streaming() - Stop sensor stream
1014  * @ov9282: pointer to ov9282 device
1015  *
1016  * Return: 0 if successful, error code otherwise.
1017  */
1018 static int ov9282_stop_streaming(struct ov9282 *ov9282)
1019 {
1020 	return ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
1021 				1, OV9282_MODE_STANDBY);
1022 }
1023 
1024 /**
1025  * ov9282_set_stream() - Enable sensor streaming
1026  * @sd: pointer to ov9282 subdevice
1027  * @enable: set to enable sensor streaming
1028  *
1029  * Return: 0 if successful, error code otherwise.
1030  */
1031 static int ov9282_set_stream(struct v4l2_subdev *sd, int enable)
1032 {
1033 	struct ov9282 *ov9282 = to_ov9282(sd);
1034 	int ret;
1035 
1036 	mutex_lock(&ov9282->mutex);
1037 
1038 	if (ov9282->streaming == enable) {
1039 		mutex_unlock(&ov9282->mutex);
1040 		return 0;
1041 	}
1042 
1043 	if (enable) {
1044 		ret = pm_runtime_resume_and_get(ov9282->dev);
1045 		if (ret)
1046 			goto error_unlock;
1047 
1048 		ret = ov9282_start_streaming(ov9282);
1049 		if (ret)
1050 			goto error_power_off;
1051 	} else {
1052 		ov9282_stop_streaming(ov9282);
1053 		pm_runtime_put(ov9282->dev);
1054 	}
1055 
1056 	ov9282->streaming = enable;
1057 
1058 	mutex_unlock(&ov9282->mutex);
1059 
1060 	return 0;
1061 
1062 error_power_off:
1063 	pm_runtime_put(ov9282->dev);
1064 error_unlock:
1065 	mutex_unlock(&ov9282->mutex);
1066 
1067 	return ret;
1068 }
1069 
1070 /**
1071  * ov9282_detect() - Detect ov9282 sensor
1072  * @ov9282: pointer to ov9282 device
1073  *
1074  * Return: 0 if successful, -EIO if sensor id does not match
1075  */
1076 static int ov9282_detect(struct ov9282 *ov9282)
1077 {
1078 	int ret;
1079 	u32 val;
1080 
1081 	ret = ov9282_read_reg(ov9282, OV9282_REG_ID, 2, &val);
1082 	if (ret)
1083 		return ret;
1084 
1085 	if (val != OV9282_ID) {
1086 		dev_err(ov9282->dev, "chip id mismatch: %x!=%x",
1087 			OV9282_ID, val);
1088 		return -ENXIO;
1089 	}
1090 
1091 	return 0;
1092 }
1093 
1094 static int ov9282_configure_regulators(struct ov9282 *ov9282)
1095 {
1096 	unsigned int i;
1097 
1098 	for (i = 0; i < OV9282_NUM_SUPPLIES; i++)
1099 		ov9282->supplies[i].supply = ov9282_supply_names[i];
1100 
1101 	return devm_regulator_bulk_get(ov9282->dev,
1102 				       OV9282_NUM_SUPPLIES,
1103 				       ov9282->supplies);
1104 }
1105 
1106 /**
1107  * ov9282_parse_hw_config() - Parse HW configuration and check if supported
1108  * @ov9282: pointer to ov9282 device
1109  *
1110  * Return: 0 if successful, error code otherwise.
1111  */
1112 static int ov9282_parse_hw_config(struct ov9282 *ov9282)
1113 {
1114 	struct fwnode_handle *fwnode = dev_fwnode(ov9282->dev);
1115 	struct v4l2_fwnode_endpoint bus_cfg = {
1116 		.bus_type = V4L2_MBUS_CSI2_DPHY
1117 	};
1118 	struct fwnode_handle *ep;
1119 	unsigned long rate;
1120 	unsigned int i;
1121 	int ret;
1122 
1123 	if (!fwnode)
1124 		return -ENXIO;
1125 
1126 	/* Request optional reset pin */
1127 	ov9282->reset_gpio = devm_gpiod_get_optional(ov9282->dev, "reset",
1128 						     GPIOD_OUT_LOW);
1129 	if (IS_ERR(ov9282->reset_gpio)) {
1130 		dev_err(ov9282->dev, "failed to get reset gpio %ld",
1131 			PTR_ERR(ov9282->reset_gpio));
1132 		return PTR_ERR(ov9282->reset_gpio);
1133 	}
1134 
1135 	/* Get sensor input clock */
1136 	ov9282->inclk = devm_clk_get(ov9282->dev, NULL);
1137 	if (IS_ERR(ov9282->inclk)) {
1138 		dev_err(ov9282->dev, "could not get inclk");
1139 		return PTR_ERR(ov9282->inclk);
1140 	}
1141 
1142 	ret = ov9282_configure_regulators(ov9282);
1143 	if (ret) {
1144 		dev_err(ov9282->dev, "Failed to get power regulators\n");
1145 		return ret;
1146 	}
1147 
1148 	rate = clk_get_rate(ov9282->inclk);
1149 	if (rate != OV9282_INCLK_RATE) {
1150 		dev_err(ov9282->dev, "inclk frequency mismatch");
1151 		return -EINVAL;
1152 	}
1153 
1154 	ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1155 	if (!ep)
1156 		return -ENXIO;
1157 
1158 	ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1159 	fwnode_handle_put(ep);
1160 	if (ret)
1161 		return ret;
1162 
1163 	ov9282->noncontinuous_clock =
1164 		bus_cfg.bus.mipi_csi2.flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
1165 
1166 	if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV9282_NUM_DATA_LANES) {
1167 		dev_err(ov9282->dev,
1168 			"number of CSI2 data lanes %d is not supported",
1169 			bus_cfg.bus.mipi_csi2.num_data_lanes);
1170 		ret = -EINVAL;
1171 		goto done_endpoint_free;
1172 	}
1173 
1174 	if (!bus_cfg.nr_of_link_frequencies) {
1175 		dev_err(ov9282->dev, "no link frequencies defined");
1176 		ret = -EINVAL;
1177 		goto done_endpoint_free;
1178 	}
1179 
1180 	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
1181 		if (bus_cfg.link_frequencies[i] == OV9282_LINK_FREQ)
1182 			goto done_endpoint_free;
1183 
1184 	ret = -EINVAL;
1185 
1186 done_endpoint_free:
1187 	v4l2_fwnode_endpoint_free(&bus_cfg);
1188 
1189 	return ret;
1190 }
1191 
1192 /* V4l2 subdevice ops */
1193 static const struct v4l2_subdev_core_ops ov9282_core_ops = {
1194 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1195 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
1196 };
1197 
1198 static const struct v4l2_subdev_video_ops ov9282_video_ops = {
1199 	.s_stream = ov9282_set_stream,
1200 };
1201 
1202 static const struct v4l2_subdev_pad_ops ov9282_pad_ops = {
1203 	.init_cfg = ov9282_init_pad_cfg,
1204 	.enum_mbus_code = ov9282_enum_mbus_code,
1205 	.enum_frame_size = ov9282_enum_frame_size,
1206 	.get_fmt = ov9282_get_pad_format,
1207 	.set_fmt = ov9282_set_pad_format,
1208 	.get_selection = ov9282_get_selection,
1209 };
1210 
1211 static const struct v4l2_subdev_ops ov9282_subdev_ops = {
1212 	.core = &ov9282_core_ops,
1213 	.video = &ov9282_video_ops,
1214 	.pad = &ov9282_pad_ops,
1215 };
1216 
1217 /**
1218  * ov9282_power_on() - Sensor power on sequence
1219  * @dev: pointer to i2c device
1220  *
1221  * Return: 0 if successful, error code otherwise.
1222  */
1223 static int ov9282_power_on(struct device *dev)
1224 {
1225 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1226 	struct ov9282 *ov9282 = to_ov9282(sd);
1227 	int ret;
1228 
1229 	ret = regulator_bulk_enable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1230 	if (ret < 0) {
1231 		dev_err(dev, "Failed to enable regulators\n");
1232 		return ret;
1233 	}
1234 
1235 	usleep_range(400, 600);
1236 
1237 	gpiod_set_value_cansleep(ov9282->reset_gpio, 1);
1238 
1239 	ret = clk_prepare_enable(ov9282->inclk);
1240 	if (ret) {
1241 		dev_err(ov9282->dev, "fail to enable inclk");
1242 		goto error_reset;
1243 	}
1244 
1245 	usleep_range(400, 600);
1246 
1247 	ret = ov9282_write_reg(ov9282, OV9282_REG_MIPI_CTRL00, 1,
1248 			       ov9282->noncontinuous_clock ?
1249 					OV9282_GATED_CLOCK : 0);
1250 	if (ret) {
1251 		dev_err(ov9282->dev, "fail to write MIPI_CTRL00");
1252 		return ret;
1253 	}
1254 
1255 	return 0;
1256 
1257 error_reset:
1258 	gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
1259 
1260 	regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1261 
1262 	return ret;
1263 }
1264 
1265 /**
1266  * ov9282_power_off() - Sensor power off sequence
1267  * @dev: pointer to i2c device
1268  *
1269  * Return: 0 if successful, error code otherwise.
1270  */
1271 static int ov9282_power_off(struct device *dev)
1272 {
1273 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1274 	struct ov9282 *ov9282 = to_ov9282(sd);
1275 
1276 	gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
1277 
1278 	clk_disable_unprepare(ov9282->inclk);
1279 
1280 	regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1281 
1282 	return 0;
1283 }
1284 
1285 /**
1286  * ov9282_init_controls() - Initialize sensor subdevice controls
1287  * @ov9282: pointer to ov9282 device
1288  *
1289  * Return: 0 if successful, error code otherwise.
1290  */
1291 static int ov9282_init_controls(struct ov9282 *ov9282)
1292 {
1293 	struct v4l2_ctrl_handler *ctrl_hdlr = &ov9282->ctrl_handler;
1294 	const struct ov9282_mode *mode = ov9282->cur_mode;
1295 	struct v4l2_fwnode_device_properties props;
1296 	u32 hblank_min;
1297 	u32 lpfr;
1298 	int ret;
1299 
1300 	ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
1301 	if (ret)
1302 		return ret;
1303 
1304 	/* Serialize controls with sensor device */
1305 	ctrl_hdlr->lock = &ov9282->mutex;
1306 
1307 	/* Initialize exposure and gain */
1308 	lpfr = mode->vblank + mode->height;
1309 	ov9282->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1310 					     &ov9282_ctrl_ops,
1311 					     V4L2_CID_EXPOSURE,
1312 					     OV9282_EXPOSURE_MIN,
1313 					     lpfr - OV9282_EXPOSURE_OFFSET,
1314 					     OV9282_EXPOSURE_STEP,
1315 					     OV9282_EXPOSURE_DEFAULT);
1316 
1317 	ov9282->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1318 					       &ov9282_ctrl_ops,
1319 					       V4L2_CID_ANALOGUE_GAIN,
1320 					       OV9282_AGAIN_MIN,
1321 					       OV9282_AGAIN_MAX,
1322 					       OV9282_AGAIN_STEP,
1323 					       OV9282_AGAIN_DEFAULT);
1324 
1325 	v4l2_ctrl_cluster(2, &ov9282->exp_ctrl);
1326 
1327 	ov9282->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1328 						&ov9282_ctrl_ops,
1329 						V4L2_CID_VBLANK,
1330 						mode->vblank_min,
1331 						mode->vblank_max,
1332 						1, mode->vblank);
1333 
1334 	v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_VFLIP,
1335 			  0, 1, 1, 1);
1336 
1337 	v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_HFLIP,
1338 			  0, 1, 1, 1);
1339 
1340 	/* Read only controls */
1341 	ov9282->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
1342 					       V4L2_CID_PIXEL_RATE,
1343 					       OV9282_PIXEL_RATE_10BIT,
1344 					       OV9282_PIXEL_RATE_10BIT, 1,
1345 					       OV9282_PIXEL_RATE_10BIT);
1346 
1347 	ov9282->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1348 							&ov9282_ctrl_ops,
1349 							V4L2_CID_LINK_FREQ,
1350 							ARRAY_SIZE(link_freq) -
1351 							1,
1352 							mode->link_freq_idx,
1353 							link_freq);
1354 	if (ov9282->link_freq_ctrl)
1355 		ov9282->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1356 
1357 	hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1];
1358 	ov9282->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1359 						&ov9282_ctrl_ops,
1360 						V4L2_CID_HBLANK,
1361 						hblank_min,
1362 						OV9282_TIMING_HTS_MAX - mode->width,
1363 						1, hblank_min);
1364 
1365 	ret = v4l2_fwnode_device_parse(ov9282->dev, &props);
1366 	if (!ret) {
1367 		/* Failure sets ctrl_hdlr->error, which we check afterwards anyway */
1368 		v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov9282_ctrl_ops,
1369 						&props);
1370 	}
1371 
1372 	if (ctrl_hdlr->error || ret) {
1373 		dev_err(ov9282->dev, "control init failed: %d",
1374 			ctrl_hdlr->error);
1375 		v4l2_ctrl_handler_free(ctrl_hdlr);
1376 		return ctrl_hdlr->error;
1377 	}
1378 
1379 	ov9282->sd.ctrl_handler = ctrl_hdlr;
1380 
1381 	return 0;
1382 }
1383 
1384 /**
1385  * ov9282_probe() - I2C client device binding
1386  * @client: pointer to i2c client device
1387  *
1388  * Return: 0 if successful, error code otherwise.
1389  */
1390 static int ov9282_probe(struct i2c_client *client)
1391 {
1392 	struct ov9282 *ov9282;
1393 	int ret;
1394 
1395 	ov9282 = devm_kzalloc(&client->dev, sizeof(*ov9282), GFP_KERNEL);
1396 	if (!ov9282)
1397 		return -ENOMEM;
1398 
1399 	ov9282->dev = &client->dev;
1400 
1401 	/* Initialize subdev */
1402 	v4l2_i2c_subdev_init(&ov9282->sd, client, &ov9282_subdev_ops);
1403 
1404 	ret = ov9282_parse_hw_config(ov9282);
1405 	if (ret) {
1406 		dev_err(ov9282->dev, "HW configuration is not supported");
1407 		return ret;
1408 	}
1409 
1410 	mutex_init(&ov9282->mutex);
1411 
1412 	ret = ov9282_power_on(ov9282->dev);
1413 	if (ret) {
1414 		dev_err(ov9282->dev, "failed to power-on the sensor");
1415 		goto error_mutex_destroy;
1416 	}
1417 
1418 	/* Check module identity */
1419 	ret = ov9282_detect(ov9282);
1420 	if (ret) {
1421 		dev_err(ov9282->dev, "failed to find sensor: %d", ret);
1422 		goto error_power_off;
1423 	}
1424 
1425 	/* Set default mode to first mode */
1426 	ov9282->cur_mode = &supported_modes[DEFAULT_MODE];
1427 	ov9282->code = MEDIA_BUS_FMT_Y10_1X10;
1428 	ov9282->vblank = ov9282->cur_mode->vblank;
1429 
1430 	ret = ov9282_init_controls(ov9282);
1431 	if (ret) {
1432 		dev_err(ov9282->dev, "failed to init controls: %d", ret);
1433 		goto error_power_off;
1434 	}
1435 
1436 	/* Initialize subdev */
1437 	ov9282->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1438 			    V4L2_SUBDEV_FL_HAS_EVENTS;
1439 	ov9282->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1440 
1441 	/* Initialize source pad */
1442 	ov9282->pad.flags = MEDIA_PAD_FL_SOURCE;
1443 	ret = media_entity_pads_init(&ov9282->sd.entity, 1, &ov9282->pad);
1444 	if (ret) {
1445 		dev_err(ov9282->dev, "failed to init entity pads: %d", ret);
1446 		goto error_handler_free;
1447 	}
1448 
1449 	ret = v4l2_async_register_subdev_sensor(&ov9282->sd);
1450 	if (ret < 0) {
1451 		dev_err(ov9282->dev,
1452 			"failed to register async subdev: %d", ret);
1453 		goto error_media_entity;
1454 	}
1455 
1456 	pm_runtime_set_active(ov9282->dev);
1457 	pm_runtime_enable(ov9282->dev);
1458 	pm_runtime_idle(ov9282->dev);
1459 
1460 	return 0;
1461 
1462 error_media_entity:
1463 	media_entity_cleanup(&ov9282->sd.entity);
1464 error_handler_free:
1465 	v4l2_ctrl_handler_free(ov9282->sd.ctrl_handler);
1466 error_power_off:
1467 	ov9282_power_off(ov9282->dev);
1468 error_mutex_destroy:
1469 	mutex_destroy(&ov9282->mutex);
1470 
1471 	return ret;
1472 }
1473 
1474 /**
1475  * ov9282_remove() - I2C client device unbinding
1476  * @client: pointer to I2C client device
1477  *
1478  * Return: 0 if successful, error code otherwise.
1479  */
1480 static void ov9282_remove(struct i2c_client *client)
1481 {
1482 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1483 	struct ov9282 *ov9282 = to_ov9282(sd);
1484 
1485 	v4l2_async_unregister_subdev(sd);
1486 	media_entity_cleanup(&sd->entity);
1487 	v4l2_ctrl_handler_free(sd->ctrl_handler);
1488 
1489 	pm_runtime_disable(&client->dev);
1490 	if (!pm_runtime_status_suspended(&client->dev))
1491 		ov9282_power_off(&client->dev);
1492 	pm_runtime_set_suspended(&client->dev);
1493 
1494 	mutex_destroy(&ov9282->mutex);
1495 }
1496 
1497 static const struct dev_pm_ops ov9282_pm_ops = {
1498 	SET_RUNTIME_PM_OPS(ov9282_power_off, ov9282_power_on, NULL)
1499 };
1500 
1501 static const struct of_device_id ov9282_of_match[] = {
1502 	{ .compatible = "ovti,ov9282" },
1503 	{ }
1504 };
1505 
1506 MODULE_DEVICE_TABLE(of, ov9282_of_match);
1507 
1508 static struct i2c_driver ov9282_driver = {
1509 	.probe_new = ov9282_probe,
1510 	.remove = ov9282_remove,
1511 	.driver = {
1512 		.name = "ov9282",
1513 		.pm = &ov9282_pm_ops,
1514 		.of_match_table = ov9282_of_match,
1515 	},
1516 };
1517 
1518 module_i2c_driver(ov9282_driver);
1519 
1520 MODULE_DESCRIPTION("OmniVision ov9282 sensor driver");
1521 MODULE_LICENSE("GPL");
1522