1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * A V4L2 driver for OmniVision OV5647 cameras.
4  *
5  * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver
6  * Copyright (C) 2011 Sylwester Nawrocki <s.nawrocki@samsung.com>
7  *
8  * Based on Omnivision OV7670 Camera Driver
9  * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
10  *
11  * Copyright (C) 2016, Synopsys, Inc.
12  */
13 
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/io.h>
20 #include <linux/module.h>
21 #include <linux/of_graph.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-fwnode.h>
29 #include <media/v4l2-image-sizes.h>
30 #include <media/v4l2-mediabus.h>
31 
32 /*
33  * From the datasheet, "20ms after PWDN goes low or 20ms after RESETB goes
34  * high if reset is inserted after PWDN goes high, host can access sensor's
35  * SCCB to initialize sensor."
36  */
37 #define PWDN_ACTIVE_DELAY_MS	20
38 
39 #define MIPI_CTRL00_CLOCK_LANE_GATE		BIT(5)
40 #define MIPI_CTRL00_LINE_SYNC_ENABLE		BIT(4)
41 #define MIPI_CTRL00_BUS_IDLE			BIT(2)
42 #define MIPI_CTRL00_CLOCK_LANE_DISABLE		BIT(0)
43 
44 #define OV5647_SW_STANDBY		0x0100
45 #define OV5647_SW_RESET			0x0103
46 #define OV5647_REG_CHIPID_H		0x300a
47 #define OV5647_REG_CHIPID_L		0x300b
48 #define OV5640_REG_PAD_OUT		0x300d
49 #define OV5647_REG_EXP_HI		0x3500
50 #define OV5647_REG_EXP_MID		0x3501
51 #define OV5647_REG_EXP_LO		0x3502
52 #define OV5647_REG_AEC_AGC		0x3503
53 #define OV5647_REG_GAIN_HI		0x350a
54 #define OV5647_REG_GAIN_LO		0x350b
55 #define OV5647_REG_VTS_HI		0x380e
56 #define OV5647_REG_VTS_LO		0x380f
57 #define OV5647_REG_FRAME_OFF_NUMBER	0x4202
58 #define OV5647_REG_MIPI_CTRL00		0x4800
59 #define OV5647_REG_MIPI_CTRL14		0x4814
60 #define OV5647_REG_AWB			0x5001
61 #define OV5647_REG_ISPCTRL3D		0x503d
62 
63 #define REG_TERM 0xfffe
64 #define VAL_TERM 0xfe
65 #define REG_DLY  0xffff
66 
67 /* OV5647 native and active pixel array size */
68 #define OV5647_NATIVE_WIDTH		2624U
69 #define OV5647_NATIVE_HEIGHT		1956U
70 
71 #define OV5647_PIXEL_ARRAY_LEFT		16U
72 #define OV5647_PIXEL_ARRAY_TOP		16U
73 #define OV5647_PIXEL_ARRAY_WIDTH	2592U
74 #define OV5647_PIXEL_ARRAY_HEIGHT	1944U
75 
76 #define OV5647_VBLANK_MIN		4
77 #define OV5647_VTS_MAX			32767
78 
79 #define OV5647_EXPOSURE_MIN		4
80 #define OV5647_EXPOSURE_STEP		1
81 #define OV5647_EXPOSURE_DEFAULT		1000
82 #define OV5647_EXPOSURE_MAX		65535
83 
84 struct regval_list {
85 	u16 addr;
86 	u8 data;
87 };
88 
89 struct ov5647_mode {
90 	struct v4l2_mbus_framefmt	format;
91 	struct v4l2_rect		crop;
92 	u64				pixel_rate;
93 	int				hts;
94 	int				vts;
95 	const struct regval_list	*reg_list;
96 	unsigned int			num_regs;
97 };
98 
99 struct ov5647 {
100 	struct v4l2_subdev		sd;
101 	struct media_pad		pad;
102 	struct mutex			lock;
103 	struct clk			*xclk;
104 	struct gpio_desc		*pwdn;
105 	bool				clock_ncont;
106 	struct v4l2_ctrl_handler	ctrls;
107 	const struct ov5647_mode	*mode;
108 	struct v4l2_ctrl		*pixel_rate;
109 	struct v4l2_ctrl		*hblank;
110 	struct v4l2_ctrl		*vblank;
111 	struct v4l2_ctrl		*exposure;
112 	bool				streaming;
113 };
114 
to_sensor(struct v4l2_subdev * sd)115 static inline struct ov5647 *to_sensor(struct v4l2_subdev *sd)
116 {
117 	return container_of(sd, struct ov5647, sd);
118 }
119 
120 static const char * const ov5647_test_pattern_menu[] = {
121 	"Disabled",
122 	"Color Bars",
123 	"Color Squares",
124 	"Random Data",
125 };
126 
127 static const u8 ov5647_test_pattern_val[] = {
128 	0x00,	/* Disabled */
129 	0x80,	/* Color Bars */
130 	0x82,	/* Color Squares */
131 	0x81,	/* Random Data */
132 };
133 
134 static const struct regval_list sensor_oe_disable_regs[] = {
135 	{0x3000, 0x00},
136 	{0x3001, 0x00},
137 	{0x3002, 0x00},
138 };
139 
140 static const struct regval_list sensor_oe_enable_regs[] = {
141 	{0x3000, 0x0f},
142 	{0x3001, 0xff},
143 	{0x3002, 0xe4},
144 };
145 
146 static struct regval_list ov5647_2592x1944_10bpp[] = {
147 	{0x0100, 0x00},
148 	{0x0103, 0x01},
149 	{0x3034, 0x1a},
150 	{0x3035, 0x21},
151 	{0x3036, 0x69},
152 	{0x303c, 0x11},
153 	{0x3106, 0xf5},
154 	{0x3821, 0x06},
155 	{0x3820, 0x00},
156 	{0x3827, 0xec},
157 	{0x370c, 0x03},
158 	{0x3612, 0x5b},
159 	{0x3618, 0x04},
160 	{0x5000, 0x06},
161 	{0x5002, 0x41},
162 	{0x5003, 0x08},
163 	{0x5a00, 0x08},
164 	{0x3000, 0x00},
165 	{0x3001, 0x00},
166 	{0x3002, 0x00},
167 	{0x3016, 0x08},
168 	{0x3017, 0xe0},
169 	{0x3018, 0x44},
170 	{0x301c, 0xf8},
171 	{0x301d, 0xf0},
172 	{0x3a18, 0x00},
173 	{0x3a19, 0xf8},
174 	{0x3c01, 0x80},
175 	{0x3b07, 0x0c},
176 	{0x380c, 0x0b},
177 	{0x380d, 0x1c},
178 	{0x3814, 0x11},
179 	{0x3815, 0x11},
180 	{0x3708, 0x64},
181 	{0x3709, 0x12},
182 	{0x3808, 0x0a},
183 	{0x3809, 0x20},
184 	{0x380a, 0x07},
185 	{0x380b, 0x98},
186 	{0x3800, 0x00},
187 	{0x3801, 0x00},
188 	{0x3802, 0x00},
189 	{0x3803, 0x00},
190 	{0x3804, 0x0a},
191 	{0x3805, 0x3f},
192 	{0x3806, 0x07},
193 	{0x3807, 0xa3},
194 	{0x3811, 0x10},
195 	{0x3813, 0x06},
196 	{0x3630, 0x2e},
197 	{0x3632, 0xe2},
198 	{0x3633, 0x23},
199 	{0x3634, 0x44},
200 	{0x3636, 0x06},
201 	{0x3620, 0x64},
202 	{0x3621, 0xe0},
203 	{0x3600, 0x37},
204 	{0x3704, 0xa0},
205 	{0x3703, 0x5a},
206 	{0x3715, 0x78},
207 	{0x3717, 0x01},
208 	{0x3731, 0x02},
209 	{0x370b, 0x60},
210 	{0x3705, 0x1a},
211 	{0x3f05, 0x02},
212 	{0x3f06, 0x10},
213 	{0x3f01, 0x0a},
214 	{0x3a08, 0x01},
215 	{0x3a09, 0x28},
216 	{0x3a0a, 0x00},
217 	{0x3a0b, 0xf6},
218 	{0x3a0d, 0x08},
219 	{0x3a0e, 0x06},
220 	{0x3a0f, 0x58},
221 	{0x3a10, 0x50},
222 	{0x3a1b, 0x58},
223 	{0x3a1e, 0x50},
224 	{0x3a11, 0x60},
225 	{0x3a1f, 0x28},
226 	{0x4001, 0x02},
227 	{0x4004, 0x04},
228 	{0x4000, 0x09},
229 	{0x4837, 0x19},
230 	{0x4800, 0x24},
231 	{0x3503, 0x03},
232 	{0x0100, 0x01},
233 };
234 
235 static struct regval_list ov5647_1080p30_10bpp[] = {
236 	{0x0100, 0x00},
237 	{0x0103, 0x01},
238 	{0x3034, 0x1a},
239 	{0x3035, 0x21},
240 	{0x3036, 0x62},
241 	{0x303c, 0x11},
242 	{0x3106, 0xf5},
243 	{0x3821, 0x06},
244 	{0x3820, 0x00},
245 	{0x3827, 0xec},
246 	{0x370c, 0x03},
247 	{0x3612, 0x5b},
248 	{0x3618, 0x04},
249 	{0x5000, 0x06},
250 	{0x5002, 0x41},
251 	{0x5003, 0x08},
252 	{0x5a00, 0x08},
253 	{0x3000, 0x00},
254 	{0x3001, 0x00},
255 	{0x3002, 0x00},
256 	{0x3016, 0x08},
257 	{0x3017, 0xe0},
258 	{0x3018, 0x44},
259 	{0x301c, 0xf8},
260 	{0x301d, 0xf0},
261 	{0x3a18, 0x00},
262 	{0x3a19, 0xf8},
263 	{0x3c01, 0x80},
264 	{0x3b07, 0x0c},
265 	{0x380c, 0x09},
266 	{0x380d, 0x70},
267 	{0x3814, 0x11},
268 	{0x3815, 0x11},
269 	{0x3708, 0x64},
270 	{0x3709, 0x12},
271 	{0x3808, 0x07},
272 	{0x3809, 0x80},
273 	{0x380a, 0x04},
274 	{0x380b, 0x38},
275 	{0x3800, 0x01},
276 	{0x3801, 0x5c},
277 	{0x3802, 0x01},
278 	{0x3803, 0xb2},
279 	{0x3804, 0x08},
280 	{0x3805, 0xe3},
281 	{0x3806, 0x05},
282 	{0x3807, 0xf1},
283 	{0x3811, 0x04},
284 	{0x3813, 0x02},
285 	{0x3630, 0x2e},
286 	{0x3632, 0xe2},
287 	{0x3633, 0x23},
288 	{0x3634, 0x44},
289 	{0x3636, 0x06},
290 	{0x3620, 0x64},
291 	{0x3621, 0xe0},
292 	{0x3600, 0x37},
293 	{0x3704, 0xa0},
294 	{0x3703, 0x5a},
295 	{0x3715, 0x78},
296 	{0x3717, 0x01},
297 	{0x3731, 0x02},
298 	{0x370b, 0x60},
299 	{0x3705, 0x1a},
300 	{0x3f05, 0x02},
301 	{0x3f06, 0x10},
302 	{0x3f01, 0x0a},
303 	{0x3a08, 0x01},
304 	{0x3a09, 0x4b},
305 	{0x3a0a, 0x01},
306 	{0x3a0b, 0x13},
307 	{0x3a0d, 0x04},
308 	{0x3a0e, 0x03},
309 	{0x3a0f, 0x58},
310 	{0x3a10, 0x50},
311 	{0x3a1b, 0x58},
312 	{0x3a1e, 0x50},
313 	{0x3a11, 0x60},
314 	{0x3a1f, 0x28},
315 	{0x4001, 0x02},
316 	{0x4004, 0x04},
317 	{0x4000, 0x09},
318 	{0x4837, 0x19},
319 	{0x4800, 0x34},
320 	{0x3503, 0x03},
321 	{0x0100, 0x01},
322 };
323 
324 static struct regval_list ov5647_2x2binned_10bpp[] = {
325 	{0x0100, 0x00},
326 	{0x0103, 0x01},
327 	{0x3034, 0x1a},
328 	{0x3035, 0x21},
329 	{0x3036, 0x62},
330 	{0x303c, 0x11},
331 	{0x3106, 0xf5},
332 	{0x3827, 0xec},
333 	{0x370c, 0x03},
334 	{0x3612, 0x59},
335 	{0x3618, 0x00},
336 	{0x5000, 0x06},
337 	{0x5002, 0x41},
338 	{0x5003, 0x08},
339 	{0x5a00, 0x08},
340 	{0x3000, 0x00},
341 	{0x3001, 0x00},
342 	{0x3002, 0x00},
343 	{0x3016, 0x08},
344 	{0x3017, 0xe0},
345 	{0x3018, 0x44},
346 	{0x301c, 0xf8},
347 	{0x301d, 0xf0},
348 	{0x3a18, 0x00},
349 	{0x3a19, 0xf8},
350 	{0x3c01, 0x80},
351 	{0x3b07, 0x0c},
352 	{0x3800, 0x00},
353 	{0x3801, 0x00},
354 	{0x3802, 0x00},
355 	{0x3803, 0x00},
356 	{0x3804, 0x0a},
357 	{0x3805, 0x3f},
358 	{0x3806, 0x07},
359 	{0x3807, 0xa3},
360 	{0x3808, 0x05},
361 	{0x3809, 0x10},
362 	{0x380a, 0x03},
363 	{0x380b, 0xcc},
364 	{0x380c, 0x07},
365 	{0x380d, 0x68},
366 	{0x3811, 0x0c},
367 	{0x3813, 0x06},
368 	{0x3814, 0x31},
369 	{0x3815, 0x31},
370 	{0x3630, 0x2e},
371 	{0x3632, 0xe2},
372 	{0x3633, 0x23},
373 	{0x3634, 0x44},
374 	{0x3636, 0x06},
375 	{0x3620, 0x64},
376 	{0x3621, 0xe0},
377 	{0x3600, 0x37},
378 	{0x3704, 0xa0},
379 	{0x3703, 0x5a},
380 	{0x3715, 0x78},
381 	{0x3717, 0x01},
382 	{0x3731, 0x02},
383 	{0x370b, 0x60},
384 	{0x3705, 0x1a},
385 	{0x3f05, 0x02},
386 	{0x3f06, 0x10},
387 	{0x3f01, 0x0a},
388 	{0x3a08, 0x01},
389 	{0x3a09, 0x28},
390 	{0x3a0a, 0x00},
391 	{0x3a0b, 0xf6},
392 	{0x3a0d, 0x08},
393 	{0x3a0e, 0x06},
394 	{0x3a0f, 0x58},
395 	{0x3a10, 0x50},
396 	{0x3a1b, 0x58},
397 	{0x3a1e, 0x50},
398 	{0x3a11, 0x60},
399 	{0x3a1f, 0x28},
400 	{0x4001, 0x02},
401 	{0x4004, 0x04},
402 	{0x4000, 0x09},
403 	{0x4837, 0x16},
404 	{0x4800, 0x24},
405 	{0x3503, 0x03},
406 	{0x3820, 0x41},
407 	{0x3821, 0x07},
408 	{0x350a, 0x00},
409 	{0x350b, 0x10},
410 	{0x3500, 0x00},
411 	{0x3501, 0x1a},
412 	{0x3502, 0xf0},
413 	{0x3212, 0xa0},
414 	{0x0100, 0x01},
415 };
416 
417 static struct regval_list ov5647_640x480_10bpp[] = {
418 	{0x0100, 0x00},
419 	{0x0103, 0x01},
420 	{0x3035, 0x11},
421 	{0x3036, 0x46},
422 	{0x303c, 0x11},
423 	{0x3821, 0x07},
424 	{0x3820, 0x41},
425 	{0x370c, 0x03},
426 	{0x3612, 0x59},
427 	{0x3618, 0x00},
428 	{0x5000, 0x06},
429 	{0x5003, 0x08},
430 	{0x5a00, 0x08},
431 	{0x3000, 0xff},
432 	{0x3001, 0xff},
433 	{0x3002, 0xff},
434 	{0x301d, 0xf0},
435 	{0x3a18, 0x00},
436 	{0x3a19, 0xf8},
437 	{0x3c01, 0x80},
438 	{0x3b07, 0x0c},
439 	{0x380c, 0x07},
440 	{0x380d, 0x3c},
441 	{0x3814, 0x35},
442 	{0x3815, 0x35},
443 	{0x3708, 0x64},
444 	{0x3709, 0x52},
445 	{0x3808, 0x02},
446 	{0x3809, 0x80},
447 	{0x380a, 0x01},
448 	{0x380b, 0xe0},
449 	{0x3800, 0x00},
450 	{0x3801, 0x10},
451 	{0x3802, 0x00},
452 	{0x3803, 0x00},
453 	{0x3804, 0x0a},
454 	{0x3805, 0x2f},
455 	{0x3806, 0x07},
456 	{0x3807, 0x9f},
457 	{0x3630, 0x2e},
458 	{0x3632, 0xe2},
459 	{0x3633, 0x23},
460 	{0x3634, 0x44},
461 	{0x3620, 0x64},
462 	{0x3621, 0xe0},
463 	{0x3600, 0x37},
464 	{0x3704, 0xa0},
465 	{0x3703, 0x5a},
466 	{0x3715, 0x78},
467 	{0x3717, 0x01},
468 	{0x3731, 0x02},
469 	{0x370b, 0x60},
470 	{0x3705, 0x1a},
471 	{0x3f05, 0x02},
472 	{0x3f06, 0x10},
473 	{0x3f01, 0x0a},
474 	{0x3a08, 0x01},
475 	{0x3a09, 0x2e},
476 	{0x3a0a, 0x00},
477 	{0x3a0b, 0xfb},
478 	{0x3a0d, 0x02},
479 	{0x3a0e, 0x01},
480 	{0x3a0f, 0x58},
481 	{0x3a10, 0x50},
482 	{0x3a1b, 0x58},
483 	{0x3a1e, 0x50},
484 	{0x3a11, 0x60},
485 	{0x3a1f, 0x28},
486 	{0x4001, 0x02},
487 	{0x4004, 0x02},
488 	{0x4000, 0x09},
489 	{0x3000, 0x00},
490 	{0x3001, 0x00},
491 	{0x3002, 0x00},
492 	{0x3017, 0xe0},
493 	{0x301c, 0xfc},
494 	{0x3636, 0x06},
495 	{0x3016, 0x08},
496 	{0x3827, 0xec},
497 	{0x3018, 0x44},
498 	{0x3035, 0x21},
499 	{0x3106, 0xf5},
500 	{0x3034, 0x1a},
501 	{0x301c, 0xf8},
502 	{0x4800, 0x34},
503 	{0x3503, 0x03},
504 	{0x0100, 0x01},
505 };
506 
507 static const struct ov5647_mode ov5647_modes[] = {
508 	/* 2592x1944 full resolution full FOV 10-bit mode. */
509 	{
510 		.format = {
511 			.code		= MEDIA_BUS_FMT_SBGGR10_1X10,
512 			.colorspace	= V4L2_COLORSPACE_SRGB,
513 			.field		= V4L2_FIELD_NONE,
514 			.width		= 2592,
515 			.height		= 1944
516 		},
517 		.crop = {
518 			.left		= OV5647_PIXEL_ARRAY_LEFT,
519 			.top		= OV5647_PIXEL_ARRAY_TOP,
520 			.width		= 2592,
521 			.height		= 1944
522 		},
523 		.pixel_rate	= 87500000,
524 		.hts		= 2844,
525 		.vts		= 0x7b0,
526 		.reg_list	= ov5647_2592x1944_10bpp,
527 		.num_regs	= ARRAY_SIZE(ov5647_2592x1944_10bpp)
528 	},
529 	/* 1080p30 10-bit mode. Full resolution centre-cropped down to 1080p. */
530 	{
531 		.format = {
532 			.code		= MEDIA_BUS_FMT_SBGGR10_1X10,
533 			.colorspace	= V4L2_COLORSPACE_SRGB,
534 			.field		= V4L2_FIELD_NONE,
535 			.width		= 1920,
536 			.height		= 1080
537 		},
538 		.crop = {
539 			.left		= 348 + OV5647_PIXEL_ARRAY_LEFT,
540 			.top		= 434 + OV5647_PIXEL_ARRAY_TOP,
541 			.width		= 1928,
542 			.height		= 1080,
543 		},
544 		.pixel_rate	= 81666700,
545 		.hts		= 2416,
546 		.vts		= 0x450,
547 		.reg_list	= ov5647_1080p30_10bpp,
548 		.num_regs	= ARRAY_SIZE(ov5647_1080p30_10bpp)
549 	},
550 	/* 2x2 binned full FOV 10-bit mode. */
551 	{
552 		.format = {
553 			.code		= MEDIA_BUS_FMT_SBGGR10_1X10,
554 			.colorspace	= V4L2_COLORSPACE_SRGB,
555 			.field		= V4L2_FIELD_NONE,
556 			.width		= 1296,
557 			.height		= 972
558 		},
559 		.crop = {
560 			.left		= OV5647_PIXEL_ARRAY_LEFT,
561 			.top		= OV5647_PIXEL_ARRAY_TOP,
562 			.width		= 2592,
563 			.height		= 1944,
564 		},
565 		.pixel_rate	= 81666700,
566 		.hts		= 1896,
567 		.vts		= 0x59b,
568 		.reg_list	= ov5647_2x2binned_10bpp,
569 		.num_regs	= ARRAY_SIZE(ov5647_2x2binned_10bpp)
570 	},
571 	/* 10-bit VGA full FOV 60fps. 2x2 binned and subsampled down to VGA. */
572 	{
573 		.format = {
574 			.code		= MEDIA_BUS_FMT_SBGGR10_1X10,
575 			.colorspace	= V4L2_COLORSPACE_SRGB,
576 			.field		= V4L2_FIELD_NONE,
577 			.width		= 640,
578 			.height		= 480
579 		},
580 		.crop = {
581 			.left		= 16 + OV5647_PIXEL_ARRAY_LEFT,
582 			.top		= OV5647_PIXEL_ARRAY_TOP,
583 			.width		= 2560,
584 			.height		= 1920,
585 		},
586 		.pixel_rate	= 55000000,
587 		.hts		= 1852,
588 		.vts		= 0x1f8,
589 		.reg_list	= ov5647_640x480_10bpp,
590 		.num_regs	= ARRAY_SIZE(ov5647_640x480_10bpp)
591 	},
592 };
593 
594 /* Default sensor mode is 2x2 binned 640x480 SBGGR10_1X10. */
595 #define OV5647_DEFAULT_MODE	(&ov5647_modes[3])
596 #define OV5647_DEFAULT_FORMAT	(ov5647_modes[3].format)
597 
ov5647_write16(struct v4l2_subdev * sd,u16 reg,u16 val)598 static int ov5647_write16(struct v4l2_subdev *sd, u16 reg, u16 val)
599 {
600 	unsigned char data[4] = { reg >> 8, reg & 0xff, val >> 8, val & 0xff};
601 	struct i2c_client *client = v4l2_get_subdevdata(sd);
602 	int ret;
603 
604 	ret = i2c_master_send(client, data, 4);
605 	if (ret < 0) {
606 		dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
607 			__func__, reg);
608 		return ret;
609 	}
610 
611 	return 0;
612 }
613 
ov5647_write(struct v4l2_subdev * sd,u16 reg,u8 val)614 static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
615 {
616 	unsigned char data[3] = { reg >> 8, reg & 0xff, val};
617 	struct i2c_client *client = v4l2_get_subdevdata(sd);
618 	int ret;
619 
620 	ret = i2c_master_send(client, data, 3);
621 	if (ret < 0) {
622 		dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
623 				__func__, reg);
624 		return ret;
625 	}
626 
627 	return 0;
628 }
629 
ov5647_read(struct v4l2_subdev * sd,u16 reg,u8 * val)630 static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val)
631 {
632 	struct i2c_client *client = v4l2_get_subdevdata(sd);
633 	u8 buf[2] = { reg >> 8, reg & 0xff };
634 	struct i2c_msg msg[2];
635 	int ret;
636 
637 	msg[0].addr = client->addr;
638 	msg[0].flags = client->flags;
639 	msg[0].buf = buf;
640 	msg[0].len = sizeof(buf);
641 
642 	msg[1].addr = client->addr;
643 	msg[1].flags = client->flags | I2C_M_RD;
644 	msg[1].buf = buf;
645 	msg[1].len = 1;
646 
647 	ret = i2c_transfer(client->adapter, msg, 2);
648 	if (ret != 2) {
649 		dev_err(&client->dev, "%s: i2c read error, reg: %x = %d\n",
650 			__func__, reg, ret);
651 		return ret >= 0 ? -EINVAL : ret;
652 	}
653 
654 	*val = buf[0];
655 
656 	return 0;
657 }
658 
ov5647_write_array(struct v4l2_subdev * sd,const struct regval_list * regs,int array_size)659 static int ov5647_write_array(struct v4l2_subdev *sd,
660 			      const struct regval_list *regs, int array_size)
661 {
662 	int i, ret;
663 
664 	for (i = 0; i < array_size; i++) {
665 		ret = ov5647_write(sd, regs[i].addr, regs[i].data);
666 		if (ret < 0)
667 			return ret;
668 	}
669 
670 	return 0;
671 }
672 
ov5647_set_virtual_channel(struct v4l2_subdev * sd,int channel)673 static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel)
674 {
675 	u8 channel_id;
676 	int ret;
677 
678 	ret = ov5647_read(sd, OV5647_REG_MIPI_CTRL14, &channel_id);
679 	if (ret < 0)
680 		return ret;
681 
682 	channel_id &= ~(3 << 6);
683 
684 	return ov5647_write(sd, OV5647_REG_MIPI_CTRL14,
685 			    channel_id | (channel << 6));
686 }
687 
ov5647_set_mode(struct v4l2_subdev * sd)688 static int ov5647_set_mode(struct v4l2_subdev *sd)
689 {
690 	struct i2c_client *client = v4l2_get_subdevdata(sd);
691 	struct ov5647 *sensor = to_sensor(sd);
692 	u8 resetval, rdval;
693 	int ret;
694 
695 	ret = ov5647_read(sd, OV5647_SW_STANDBY, &rdval);
696 	if (ret < 0)
697 		return ret;
698 
699 	ret = ov5647_write_array(sd, sensor->mode->reg_list,
700 				 sensor->mode->num_regs);
701 	if (ret < 0) {
702 		dev_err(&client->dev, "write sensor default regs error\n");
703 		return ret;
704 	}
705 
706 	ret = ov5647_set_virtual_channel(sd, 0);
707 	if (ret < 0)
708 		return ret;
709 
710 	ret = ov5647_read(sd, OV5647_SW_STANDBY, &resetval);
711 	if (ret < 0)
712 		return ret;
713 
714 	if (!(resetval & 0x01)) {
715 		dev_err(&client->dev, "Device was in SW standby");
716 		ret = ov5647_write(sd, OV5647_SW_STANDBY, 0x01);
717 		if (ret < 0)
718 			return ret;
719 	}
720 
721 	return 0;
722 }
723 
ov5647_stream_on(struct v4l2_subdev * sd)724 static int ov5647_stream_on(struct v4l2_subdev *sd)
725 {
726 	struct i2c_client *client = v4l2_get_subdevdata(sd);
727 	struct ov5647 *sensor = to_sensor(sd);
728 	u8 val = MIPI_CTRL00_BUS_IDLE;
729 	int ret;
730 
731 	ret = ov5647_set_mode(sd);
732 	if (ret) {
733 		dev_err(&client->dev, "Failed to program sensor mode: %d\n", ret);
734 		return ret;
735 	}
736 
737 	/* Apply customized values from user when stream starts. */
738 	ret =  __v4l2_ctrl_handler_setup(sd->ctrl_handler);
739 	if (ret)
740 		return ret;
741 
742 	if (sensor->clock_ncont)
743 		val |= MIPI_CTRL00_CLOCK_LANE_GATE |
744 		       MIPI_CTRL00_LINE_SYNC_ENABLE;
745 
746 	ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00, val);
747 	if (ret < 0)
748 		return ret;
749 
750 	ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x00);
751 	if (ret < 0)
752 		return ret;
753 
754 	return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x00);
755 }
756 
ov5647_stream_off(struct v4l2_subdev * sd)757 static int ov5647_stream_off(struct v4l2_subdev *sd)
758 {
759 	int ret;
760 
761 	ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00,
762 			   MIPI_CTRL00_CLOCK_LANE_GATE | MIPI_CTRL00_BUS_IDLE |
763 			   MIPI_CTRL00_CLOCK_LANE_DISABLE);
764 	if (ret < 0)
765 		return ret;
766 
767 	ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x0f);
768 	if (ret < 0)
769 		return ret;
770 
771 	return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x01);
772 }
773 
ov5647_power_on(struct device * dev)774 static int ov5647_power_on(struct device *dev)
775 {
776 	struct ov5647 *sensor = dev_get_drvdata(dev);
777 	int ret;
778 
779 	dev_dbg(dev, "OV5647 power on\n");
780 
781 	if (sensor->pwdn) {
782 		gpiod_set_value_cansleep(sensor->pwdn, 0);
783 		msleep(PWDN_ACTIVE_DELAY_MS);
784 	}
785 
786 	ret = clk_prepare_enable(sensor->xclk);
787 	if (ret < 0) {
788 		dev_err(dev, "clk prepare enable failed\n");
789 		goto error_pwdn;
790 	}
791 
792 	ret = ov5647_write_array(&sensor->sd, sensor_oe_enable_regs,
793 				 ARRAY_SIZE(sensor_oe_enable_regs));
794 	if (ret < 0) {
795 		dev_err(dev, "write sensor_oe_enable_regs error\n");
796 		goto error_clk_disable;
797 	}
798 
799 	/* Stream off to coax lanes into LP-11 state. */
800 	ret = ov5647_stream_off(&sensor->sd);
801 	if (ret < 0) {
802 		dev_err(dev, "camera not available, check power\n");
803 		goto error_clk_disable;
804 	}
805 
806 	return 0;
807 
808 error_clk_disable:
809 	clk_disable_unprepare(sensor->xclk);
810 error_pwdn:
811 	gpiod_set_value_cansleep(sensor->pwdn, 1);
812 
813 	return ret;
814 }
815 
ov5647_power_off(struct device * dev)816 static int ov5647_power_off(struct device *dev)
817 {
818 	struct ov5647 *sensor = dev_get_drvdata(dev);
819 	u8 rdval;
820 	int ret;
821 
822 	dev_dbg(dev, "OV5647 power off\n");
823 
824 	ret = ov5647_write_array(&sensor->sd, sensor_oe_disable_regs,
825 				 ARRAY_SIZE(sensor_oe_disable_regs));
826 	if (ret < 0)
827 		dev_dbg(dev, "disable oe failed\n");
828 
829 	/* Enter software standby */
830 	ret = ov5647_read(&sensor->sd, OV5647_SW_STANDBY, &rdval);
831 	if (ret < 0)
832 		dev_dbg(dev, "software standby failed\n");
833 
834 	rdval &= ~0x01;
835 	ret = ov5647_write(&sensor->sd, OV5647_SW_STANDBY, rdval);
836 	if (ret < 0)
837 		dev_dbg(dev, "software standby failed\n");
838 
839 	clk_disable_unprepare(sensor->xclk);
840 	gpiod_set_value_cansleep(sensor->pwdn, 1);
841 
842 	return 0;
843 }
844 
845 #ifdef CONFIG_VIDEO_ADV_DEBUG
ov5647_sensor_get_register(struct v4l2_subdev * sd,struct v4l2_dbg_register * reg)846 static int ov5647_sensor_get_register(struct v4l2_subdev *sd,
847 				      struct v4l2_dbg_register *reg)
848 {
849 	int ret;
850 	u8 val;
851 
852 	ret = ov5647_read(sd, reg->reg & 0xff, &val);
853 	if (ret < 0)
854 		return ret;
855 
856 	reg->val = val;
857 	reg->size = 1;
858 
859 	return 0;
860 }
861 
ov5647_sensor_set_register(struct v4l2_subdev * sd,const struct v4l2_dbg_register * reg)862 static int ov5647_sensor_set_register(struct v4l2_subdev *sd,
863 				      const struct v4l2_dbg_register *reg)
864 {
865 	return ov5647_write(sd, reg->reg & 0xff, reg->val & 0xff);
866 }
867 #endif
868 
869 /* Subdev core operations registration */
870 static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops = {
871 	.subscribe_event	= v4l2_ctrl_subdev_subscribe_event,
872 	.unsubscribe_event	= v4l2_event_subdev_unsubscribe,
873 #ifdef CONFIG_VIDEO_ADV_DEBUG
874 	.g_register		= ov5647_sensor_get_register,
875 	.s_register		= ov5647_sensor_set_register,
876 #endif
877 };
878 
879 static const struct v4l2_rect *
__ov5647_get_pad_crop(struct ov5647 * ov5647,struct v4l2_subdev_state * sd_state,unsigned int pad,enum v4l2_subdev_format_whence which)880 __ov5647_get_pad_crop(struct ov5647 *ov5647,
881 		      struct v4l2_subdev_state *sd_state,
882 		      unsigned int pad, enum v4l2_subdev_format_whence which)
883 {
884 	switch (which) {
885 	case V4L2_SUBDEV_FORMAT_TRY:
886 		return v4l2_subdev_get_try_crop(&ov5647->sd, sd_state, pad);
887 	case V4L2_SUBDEV_FORMAT_ACTIVE:
888 		return &ov5647->mode->crop;
889 	}
890 
891 	return NULL;
892 }
893 
ov5647_s_stream(struct v4l2_subdev * sd,int enable)894 static int ov5647_s_stream(struct v4l2_subdev *sd, int enable)
895 {
896 	struct i2c_client *client = v4l2_get_subdevdata(sd);
897 	struct ov5647 *sensor = to_sensor(sd);
898 	int ret;
899 
900 	mutex_lock(&sensor->lock);
901 	if (sensor->streaming == enable) {
902 		mutex_unlock(&sensor->lock);
903 		return 0;
904 	}
905 
906 	if (enable) {
907 		ret = pm_runtime_resume_and_get(&client->dev);
908 		if (ret < 0)
909 			goto error_unlock;
910 
911 		ret = ov5647_stream_on(sd);
912 		if (ret < 0) {
913 			dev_err(&client->dev, "stream start failed: %d\n", ret);
914 			goto error_pm;
915 		}
916 	} else {
917 		ret = ov5647_stream_off(sd);
918 		if (ret < 0) {
919 			dev_err(&client->dev, "stream stop failed: %d\n", ret);
920 			goto error_pm;
921 		}
922 		pm_runtime_put(&client->dev);
923 	}
924 
925 	sensor->streaming = enable;
926 	mutex_unlock(&sensor->lock);
927 
928 	return 0;
929 
930 error_pm:
931 	pm_runtime_put(&client->dev);
932 error_unlock:
933 	mutex_unlock(&sensor->lock);
934 
935 	return ret;
936 }
937 
938 static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = {
939 	.s_stream =		ov5647_s_stream,
940 };
941 
ov5647_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)942 static int ov5647_enum_mbus_code(struct v4l2_subdev *sd,
943 				 struct v4l2_subdev_state *sd_state,
944 				 struct v4l2_subdev_mbus_code_enum *code)
945 {
946 	if (code->index > 0)
947 		return -EINVAL;
948 
949 	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
950 
951 	return 0;
952 }
953 
ov5647_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)954 static int ov5647_enum_frame_size(struct v4l2_subdev *sd,
955 				  struct v4l2_subdev_state *sd_state,
956 				  struct v4l2_subdev_frame_size_enum *fse)
957 {
958 	const struct v4l2_mbus_framefmt *fmt;
959 
960 	if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10 ||
961 	    fse->index >= ARRAY_SIZE(ov5647_modes))
962 		return -EINVAL;
963 
964 	fmt = &ov5647_modes[fse->index].format;
965 	fse->min_width = fmt->width;
966 	fse->max_width = fmt->width;
967 	fse->min_height = fmt->height;
968 	fse->max_height = fmt->height;
969 
970 	return 0;
971 }
972 
ov5647_get_pad_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)973 static int ov5647_get_pad_fmt(struct v4l2_subdev *sd,
974 			      struct v4l2_subdev_state *sd_state,
975 			      struct v4l2_subdev_format *format)
976 {
977 	struct v4l2_mbus_framefmt *fmt = &format->format;
978 	const struct v4l2_mbus_framefmt *sensor_format;
979 	struct ov5647 *sensor = to_sensor(sd);
980 
981 	mutex_lock(&sensor->lock);
982 	switch (format->which) {
983 	case V4L2_SUBDEV_FORMAT_TRY:
984 		sensor_format = v4l2_subdev_get_try_format(sd, sd_state,
985 							   format->pad);
986 		break;
987 	default:
988 		sensor_format = &sensor->mode->format;
989 		break;
990 	}
991 
992 	*fmt = *sensor_format;
993 	mutex_unlock(&sensor->lock);
994 
995 	return 0;
996 }
997 
ov5647_set_pad_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)998 static int ov5647_set_pad_fmt(struct v4l2_subdev *sd,
999 			      struct v4l2_subdev_state *sd_state,
1000 			      struct v4l2_subdev_format *format)
1001 {
1002 	struct v4l2_mbus_framefmt *fmt = &format->format;
1003 	struct ov5647 *sensor = to_sensor(sd);
1004 	const struct ov5647_mode *mode;
1005 
1006 	mode = v4l2_find_nearest_size(ov5647_modes, ARRAY_SIZE(ov5647_modes),
1007 				      format.width, format.height,
1008 				      fmt->width, fmt->height);
1009 
1010 	/* Update the sensor mode and apply at it at streamon time. */
1011 	mutex_lock(&sensor->lock);
1012 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1013 		*v4l2_subdev_get_try_format(sd, sd_state, format->pad) = mode->format;
1014 	} else {
1015 		int exposure_max, exposure_def;
1016 		int hblank, vblank;
1017 
1018 		sensor->mode = mode;
1019 		__v4l2_ctrl_modify_range(sensor->pixel_rate, mode->pixel_rate,
1020 					 mode->pixel_rate, 1, mode->pixel_rate);
1021 
1022 		hblank = mode->hts - mode->format.width;
1023 		__v4l2_ctrl_modify_range(sensor->hblank, hblank, hblank, 1,
1024 					 hblank);
1025 
1026 		vblank = mode->vts - mode->format.height;
1027 		__v4l2_ctrl_modify_range(sensor->vblank, OV5647_VBLANK_MIN,
1028 					 OV5647_VTS_MAX - mode->format.height,
1029 					 1, vblank);
1030 		__v4l2_ctrl_s_ctrl(sensor->vblank, vblank);
1031 
1032 		exposure_max = mode->vts - 4;
1033 		exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1034 		__v4l2_ctrl_modify_range(sensor->exposure,
1035 					 sensor->exposure->minimum,
1036 					 exposure_max, sensor->exposure->step,
1037 					 exposure_def);
1038 	}
1039 	*fmt = mode->format;
1040 	mutex_unlock(&sensor->lock);
1041 
1042 	return 0;
1043 }
1044 
ov5647_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)1045 static int ov5647_get_selection(struct v4l2_subdev *sd,
1046 				struct v4l2_subdev_state *sd_state,
1047 				struct v4l2_subdev_selection *sel)
1048 {
1049 	switch (sel->target) {
1050 	case V4L2_SEL_TGT_CROP: {
1051 		struct ov5647 *sensor = to_sensor(sd);
1052 
1053 		mutex_lock(&sensor->lock);
1054 		sel->r = *__ov5647_get_pad_crop(sensor, sd_state, sel->pad,
1055 						sel->which);
1056 		mutex_unlock(&sensor->lock);
1057 
1058 		return 0;
1059 	}
1060 
1061 	case V4L2_SEL_TGT_NATIVE_SIZE:
1062 		sel->r.top = 0;
1063 		sel->r.left = 0;
1064 		sel->r.width = OV5647_NATIVE_WIDTH;
1065 		sel->r.height = OV5647_NATIVE_HEIGHT;
1066 
1067 		return 0;
1068 
1069 	case V4L2_SEL_TGT_CROP_DEFAULT:
1070 	case V4L2_SEL_TGT_CROP_BOUNDS:
1071 		sel->r.top = OV5647_PIXEL_ARRAY_TOP;
1072 		sel->r.left = OV5647_PIXEL_ARRAY_LEFT;
1073 		sel->r.width = OV5647_PIXEL_ARRAY_WIDTH;
1074 		sel->r.height = OV5647_PIXEL_ARRAY_HEIGHT;
1075 
1076 		return 0;
1077 	}
1078 
1079 	return -EINVAL;
1080 }
1081 
1082 static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = {
1083 	.enum_mbus_code		= ov5647_enum_mbus_code,
1084 	.enum_frame_size	= ov5647_enum_frame_size,
1085 	.set_fmt		= ov5647_set_pad_fmt,
1086 	.get_fmt		= ov5647_get_pad_fmt,
1087 	.get_selection		= ov5647_get_selection,
1088 };
1089 
1090 static const struct v4l2_subdev_ops ov5647_subdev_ops = {
1091 	.core		= &ov5647_subdev_core_ops,
1092 	.video		= &ov5647_subdev_video_ops,
1093 	.pad		= &ov5647_subdev_pad_ops,
1094 };
1095 
ov5647_detect(struct v4l2_subdev * sd)1096 static int ov5647_detect(struct v4l2_subdev *sd)
1097 {
1098 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1099 	u8 read;
1100 	int ret;
1101 
1102 	ret = ov5647_write(sd, OV5647_SW_RESET, 0x01);
1103 	if (ret < 0)
1104 		return ret;
1105 
1106 	ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &read);
1107 	if (ret < 0)
1108 		return ret;
1109 
1110 	if (read != 0x56) {
1111 		dev_err(&client->dev, "ID High expected 0x56 got %x", read);
1112 		return -ENODEV;
1113 	}
1114 
1115 	ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &read);
1116 	if (ret < 0)
1117 		return ret;
1118 
1119 	if (read != 0x47) {
1120 		dev_err(&client->dev, "ID Low expected 0x47 got %x", read);
1121 		return -ENODEV;
1122 	}
1123 
1124 	return ov5647_write(sd, OV5647_SW_RESET, 0x00);
1125 }
1126 
ov5647_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)1127 static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1128 {
1129 	struct v4l2_mbus_framefmt *format =
1130 				v4l2_subdev_get_try_format(sd, fh->state, 0);
1131 	struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
1132 
1133 	crop->left = OV5647_PIXEL_ARRAY_LEFT;
1134 	crop->top = OV5647_PIXEL_ARRAY_TOP;
1135 	crop->width = OV5647_PIXEL_ARRAY_WIDTH;
1136 	crop->height = OV5647_PIXEL_ARRAY_HEIGHT;
1137 
1138 	*format = OV5647_DEFAULT_FORMAT;
1139 
1140 	return 0;
1141 }
1142 
1143 static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = {
1144 	.open = ov5647_open,
1145 };
1146 
ov5647_s_auto_white_balance(struct v4l2_subdev * sd,u32 val)1147 static int ov5647_s_auto_white_balance(struct v4l2_subdev *sd, u32 val)
1148 {
1149 	return ov5647_write(sd, OV5647_REG_AWB, val ? 1 : 0);
1150 }
1151 
ov5647_s_autogain(struct v4l2_subdev * sd,u32 val)1152 static int ov5647_s_autogain(struct v4l2_subdev *sd, u32 val)
1153 {
1154 	int ret;
1155 	u8 reg;
1156 
1157 	/* Non-zero turns on AGC by clearing bit 1.*/
1158 	ret = ov5647_read(sd, OV5647_REG_AEC_AGC, &reg);
1159 	if (ret)
1160 		return ret;
1161 
1162 	return ov5647_write(sd, OV5647_REG_AEC_AGC, val ? reg & ~BIT(1)
1163 							: reg | BIT(1));
1164 }
1165 
ov5647_s_exposure_auto(struct v4l2_subdev * sd,u32 val)1166 static int ov5647_s_exposure_auto(struct v4l2_subdev *sd, u32 val)
1167 {
1168 	int ret;
1169 	u8 reg;
1170 
1171 	/*
1172 	 * Everything except V4L2_EXPOSURE_MANUAL turns on AEC by
1173 	 * clearing bit 0.
1174 	 */
1175 	ret = ov5647_read(sd, OV5647_REG_AEC_AGC, &reg);
1176 	if (ret)
1177 		return ret;
1178 
1179 	return ov5647_write(sd, OV5647_REG_AEC_AGC,
1180 			    val == V4L2_EXPOSURE_MANUAL ? reg | BIT(0)
1181 							: reg & ~BIT(0));
1182 }
1183 
ov5647_s_analogue_gain(struct v4l2_subdev * sd,u32 val)1184 static int ov5647_s_analogue_gain(struct v4l2_subdev *sd, u32 val)
1185 {
1186 	int ret;
1187 
1188 	/* 10 bits of gain, 2 in the high register. */
1189 	ret = ov5647_write(sd, OV5647_REG_GAIN_HI, (val >> 8) & 3);
1190 	if (ret)
1191 		return ret;
1192 
1193 	return ov5647_write(sd, OV5647_REG_GAIN_LO, val & 0xff);
1194 }
1195 
ov5647_s_exposure(struct v4l2_subdev * sd,u32 val)1196 static int ov5647_s_exposure(struct v4l2_subdev *sd, u32 val)
1197 {
1198 	int ret;
1199 
1200 	/*
1201 	 * Sensor has 20 bits, but the bottom 4 bits are fractions of a line
1202 	 * which we leave as zero (and don't receive in "val").
1203 	 */
1204 	ret = ov5647_write(sd, OV5647_REG_EXP_HI, (val >> 12) & 0xf);
1205 	if (ret)
1206 		return ret;
1207 
1208 	ret = ov5647_write(sd, OV5647_REG_EXP_MID, (val >> 4) & 0xff);
1209 	if (ret)
1210 		return ret;
1211 
1212 	return ov5647_write(sd, OV5647_REG_EXP_LO, (val & 0xf) << 4);
1213 }
1214 
ov5647_s_ctrl(struct v4l2_ctrl * ctrl)1215 static int ov5647_s_ctrl(struct v4l2_ctrl *ctrl)
1216 {
1217 	struct ov5647 *sensor = container_of(ctrl->handler,
1218 					    struct ov5647, ctrls);
1219 	struct v4l2_subdev *sd = &sensor->sd;
1220 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1221 	int ret = 0;
1222 
1223 
1224 	/* v4l2_ctrl_lock() locks our own mutex */
1225 
1226 	if (ctrl->id == V4L2_CID_VBLANK) {
1227 		int exposure_max, exposure_def;
1228 
1229 		/* Update max exposure while meeting expected vblanking */
1230 		exposure_max = sensor->mode->format.height + ctrl->val - 4;
1231 		exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1232 		__v4l2_ctrl_modify_range(sensor->exposure,
1233 					 sensor->exposure->minimum,
1234 					 exposure_max, sensor->exposure->step,
1235 					 exposure_def);
1236 	}
1237 
1238 	/*
1239 	 * If the device is not powered up do not apply any controls
1240 	 * to H/W at this time. Instead the controls will be restored
1241 	 * at s_stream(1) time.
1242 	 */
1243 	if (pm_runtime_get_if_in_use(&client->dev) == 0)
1244 		return 0;
1245 
1246 	switch (ctrl->id) {
1247 	case V4L2_CID_AUTO_WHITE_BALANCE:
1248 		ret = ov5647_s_auto_white_balance(sd, ctrl->val);
1249 		break;
1250 	case V4L2_CID_AUTOGAIN:
1251 		ret = ov5647_s_autogain(sd, ctrl->val);
1252 		break;
1253 	case V4L2_CID_EXPOSURE_AUTO:
1254 		ret = ov5647_s_exposure_auto(sd, ctrl->val);
1255 		break;
1256 	case V4L2_CID_ANALOGUE_GAIN:
1257 		ret =  ov5647_s_analogue_gain(sd, ctrl->val);
1258 		break;
1259 	case V4L2_CID_EXPOSURE:
1260 		ret = ov5647_s_exposure(sd, ctrl->val);
1261 		break;
1262 	case V4L2_CID_VBLANK:
1263 		ret = ov5647_write16(sd, OV5647_REG_VTS_HI,
1264 				     sensor->mode->format.height + ctrl->val);
1265 		break;
1266 	case V4L2_CID_TEST_PATTERN:
1267 		ret = ov5647_write(sd, OV5647_REG_ISPCTRL3D,
1268 				   ov5647_test_pattern_val[ctrl->val]);
1269 		break;
1270 
1271 	/* Read-only, but we adjust it based on mode. */
1272 	case V4L2_CID_PIXEL_RATE:
1273 	case V4L2_CID_HBLANK:
1274 		/* Read-only, but we adjust it based on mode. */
1275 		break;
1276 
1277 	default:
1278 		dev_info(&client->dev,
1279 			 "Control (id:0x%x, val:0x%x) not supported\n",
1280 			 ctrl->id, ctrl->val);
1281 		return -EINVAL;
1282 	}
1283 
1284 	pm_runtime_put(&client->dev);
1285 
1286 	return ret;
1287 }
1288 
1289 static const struct v4l2_ctrl_ops ov5647_ctrl_ops = {
1290 	.s_ctrl = ov5647_s_ctrl,
1291 };
1292 
ov5647_init_controls(struct ov5647 * sensor)1293 static int ov5647_init_controls(struct ov5647 *sensor)
1294 {
1295 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->sd);
1296 	int hblank, exposure_max, exposure_def;
1297 
1298 	v4l2_ctrl_handler_init(&sensor->ctrls, 9);
1299 
1300 	v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1301 			  V4L2_CID_AUTOGAIN, 0, 1, 1, 0);
1302 
1303 	v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1304 			  V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 0);
1305 
1306 	v4l2_ctrl_new_std_menu(&sensor->ctrls, &ov5647_ctrl_ops,
1307 			       V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
1308 			       0, V4L2_EXPOSURE_MANUAL);
1309 
1310 	exposure_max = sensor->mode->vts - 4;
1311 	exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1312 	sensor->exposure = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1313 					     V4L2_CID_EXPOSURE,
1314 					     OV5647_EXPOSURE_MIN,
1315 					     exposure_max, OV5647_EXPOSURE_STEP,
1316 					     exposure_def);
1317 
1318 	/* min: 16 = 1.0x; max (10 bits); default: 32 = 2.0x. */
1319 	v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1320 			  V4L2_CID_ANALOGUE_GAIN, 16, 1023, 1, 32);
1321 
1322 	/* By default, PIXEL_RATE is read only, but it does change per mode */
1323 	sensor->pixel_rate = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1324 					       V4L2_CID_PIXEL_RATE,
1325 					       sensor->mode->pixel_rate,
1326 					       sensor->mode->pixel_rate, 1,
1327 					       sensor->mode->pixel_rate);
1328 
1329 	/* By default, HBLANK is read only, but it does change per mode. */
1330 	hblank = sensor->mode->hts - sensor->mode->format.width;
1331 	sensor->hblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1332 					   V4L2_CID_HBLANK, hblank, hblank, 1,
1333 					   hblank);
1334 
1335 	sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1336 					   V4L2_CID_VBLANK, OV5647_VBLANK_MIN,
1337 					   OV5647_VTS_MAX -
1338 					   sensor->mode->format.height, 1,
1339 					   sensor->mode->vts -
1340 					   sensor->mode->format.height);
1341 
1342 	v4l2_ctrl_new_std_menu_items(&sensor->ctrls, &ov5647_ctrl_ops,
1343 				     V4L2_CID_TEST_PATTERN,
1344 				     ARRAY_SIZE(ov5647_test_pattern_menu) - 1,
1345 				     0, 0, ov5647_test_pattern_menu);
1346 
1347 	if (sensor->ctrls.error)
1348 		goto handler_free;
1349 
1350 	sensor->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1351 	sensor->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1352 	sensor->sd.ctrl_handler = &sensor->ctrls;
1353 
1354 	return 0;
1355 
1356 handler_free:
1357 	dev_err(&client->dev, "%s Controls initialization failed (%d)\n",
1358 		__func__, sensor->ctrls.error);
1359 	v4l2_ctrl_handler_free(&sensor->ctrls);
1360 
1361 	return sensor->ctrls.error;
1362 }
1363 
ov5647_parse_dt(struct ov5647 * sensor,struct device_node * np)1364 static int ov5647_parse_dt(struct ov5647 *sensor, struct device_node *np)
1365 {
1366 	struct v4l2_fwnode_endpoint bus_cfg = {
1367 		.bus_type = V4L2_MBUS_CSI2_DPHY,
1368 	};
1369 	struct device_node *ep;
1370 	int ret;
1371 
1372 	ep = of_graph_get_next_endpoint(np, NULL);
1373 	if (!ep)
1374 		return -EINVAL;
1375 
1376 	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
1377 	if (ret)
1378 		goto out;
1379 
1380 	sensor->clock_ncont = bus_cfg.bus.mipi_csi2.flags &
1381 			      V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
1382 
1383 out:
1384 	of_node_put(ep);
1385 
1386 	return ret;
1387 }
1388 
ov5647_probe(struct i2c_client * client)1389 static int ov5647_probe(struct i2c_client *client)
1390 {
1391 	struct device_node *np = client->dev.of_node;
1392 	struct device *dev = &client->dev;
1393 	struct ov5647 *sensor;
1394 	struct v4l2_subdev *sd;
1395 	u32 xclk_freq;
1396 	int ret;
1397 
1398 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1399 	if (!sensor)
1400 		return -ENOMEM;
1401 
1402 	if (IS_ENABLED(CONFIG_OF) && np) {
1403 		ret = ov5647_parse_dt(sensor, np);
1404 		if (ret) {
1405 			dev_err(dev, "DT parsing error: %d\n", ret);
1406 			return ret;
1407 		}
1408 	}
1409 
1410 	sensor->xclk = devm_clk_get(dev, NULL);
1411 	if (IS_ERR(sensor->xclk)) {
1412 		dev_err(dev, "could not get xclk");
1413 		return PTR_ERR(sensor->xclk);
1414 	}
1415 
1416 	xclk_freq = clk_get_rate(sensor->xclk);
1417 	if (xclk_freq != 25000000) {
1418 		dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq);
1419 		return -EINVAL;
1420 	}
1421 
1422 	/* Request the power down GPIO asserted. */
1423 	sensor->pwdn = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_HIGH);
1424 	if (IS_ERR(sensor->pwdn)) {
1425 		dev_err(dev, "Failed to get 'pwdn' gpio\n");
1426 		return -EINVAL;
1427 	}
1428 
1429 	mutex_init(&sensor->lock);
1430 
1431 	sensor->mode = OV5647_DEFAULT_MODE;
1432 
1433 	ret = ov5647_init_controls(sensor);
1434 	if (ret)
1435 		goto mutex_destroy;
1436 
1437 	sd = &sensor->sd;
1438 	v4l2_i2c_subdev_init(sd, client, &ov5647_subdev_ops);
1439 	sd->internal_ops = &ov5647_subdev_internal_ops;
1440 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1441 
1442 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
1443 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1444 	ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
1445 	if (ret < 0)
1446 		goto ctrl_handler_free;
1447 
1448 	ret = ov5647_power_on(dev);
1449 	if (ret)
1450 		goto entity_cleanup;
1451 
1452 	ret = ov5647_detect(sd);
1453 	if (ret < 0)
1454 		goto power_off;
1455 
1456 	ret = v4l2_async_register_subdev(sd);
1457 	if (ret < 0)
1458 		goto power_off;
1459 
1460 	/* Enable runtime PM and turn off the device */
1461 	pm_runtime_set_active(dev);
1462 	pm_runtime_enable(dev);
1463 	pm_runtime_idle(dev);
1464 
1465 	dev_dbg(dev, "OmniVision OV5647 camera driver probed\n");
1466 
1467 	return 0;
1468 
1469 power_off:
1470 	ov5647_power_off(dev);
1471 entity_cleanup:
1472 	media_entity_cleanup(&sd->entity);
1473 ctrl_handler_free:
1474 	v4l2_ctrl_handler_free(&sensor->ctrls);
1475 mutex_destroy:
1476 	mutex_destroy(&sensor->lock);
1477 
1478 	return ret;
1479 }
1480 
ov5647_remove(struct i2c_client * client)1481 static void ov5647_remove(struct i2c_client *client)
1482 {
1483 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1484 	struct ov5647 *sensor = to_sensor(sd);
1485 
1486 	v4l2_async_unregister_subdev(&sensor->sd);
1487 	media_entity_cleanup(&sensor->sd.entity);
1488 	v4l2_ctrl_handler_free(&sensor->ctrls);
1489 	v4l2_device_unregister_subdev(sd);
1490 	pm_runtime_disable(&client->dev);
1491 	mutex_destroy(&sensor->lock);
1492 }
1493 
1494 static const struct dev_pm_ops ov5647_pm_ops = {
1495 	SET_RUNTIME_PM_OPS(ov5647_power_off, ov5647_power_on, NULL)
1496 };
1497 
1498 static const struct i2c_device_id ov5647_id[] = {
1499 	{ "ov5647", 0 },
1500 	{ /* sentinel */ }
1501 };
1502 MODULE_DEVICE_TABLE(i2c, ov5647_id);
1503 
1504 #if IS_ENABLED(CONFIG_OF)
1505 static const struct of_device_id ov5647_of_match[] = {
1506 	{ .compatible = "ovti,ov5647" },
1507 	{ /* sentinel */ },
1508 };
1509 MODULE_DEVICE_TABLE(of, ov5647_of_match);
1510 #endif
1511 
1512 static struct i2c_driver ov5647_driver = {
1513 	.driver = {
1514 		.of_match_table = of_match_ptr(ov5647_of_match),
1515 		.name	= "ov5647",
1516 		.pm	= &ov5647_pm_ops,
1517 	},
1518 	.probe		= ov5647_probe,
1519 	.remove		= ov5647_remove,
1520 	.id_table	= ov5647_id,
1521 };
1522 
1523 module_i2c_driver(ov5647_driver);
1524 
1525 MODULE_AUTHOR("Ramiro Oliveira <roliveir@synopsys.com>");
1526 MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
1527 MODULE_LICENSE("GPL v2");
1528