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