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