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