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