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