1 // SPDX-License-Identifier: GPL-2.0-only
3 * Omnivision OV2659 CMOS Image Sensor driver
5 * Copyright (C) 2015 Texas Instruments, Inc.
7 * Benoit Parrot <bparrot@ti.com>
8 * Lad, Prabhakar <prabhakar.csengg@gmail.com>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/of_graph.h>
17 #include <linux/pm_runtime.h>
19 #include <media/i2c/ov2659.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-fwnode.h>
23 #include <media/v4l2-image-sizes.h>
24 #include <media/v4l2-subdev.h>
26 #define DRIVER_NAME "ov2659"
29 * OV2659 register definitions
31 #define REG_SOFTWARE_STANDBY 0x0100
32 #define REG_SOFTWARE_RESET 0x0103
33 #define REG_IO_CTRL00 0x3000
34 #define REG_IO_CTRL01 0x3001
35 #define REG_IO_CTRL02 0x3002
36 #define REG_OUTPUT_VALUE00 0x3008
37 #define REG_OUTPUT_VALUE01 0x3009
38 #define REG_OUTPUT_VALUE02 0x300d
39 #define REG_OUTPUT_SELECT00 0x300e
40 #define REG_OUTPUT_SELECT01 0x300f
41 #define REG_OUTPUT_SELECT02 0x3010
42 #define REG_OUTPUT_DRIVE 0x3011
43 #define REG_INPUT_READOUT00 0x302d
44 #define REG_INPUT_READOUT01 0x302e
45 #define REG_INPUT_READOUT02 0x302f
47 #define REG_SC_PLL_CTRL0 0x3003
48 #define REG_SC_PLL_CTRL1 0x3004
49 #define REG_SC_PLL_CTRL2 0x3005
50 #define REG_SC_PLL_CTRL3 0x3006
51 #define REG_SC_CHIP_ID_H 0x300a
52 #define REG_SC_CHIP_ID_L 0x300b
53 #define REG_SC_PWC 0x3014
54 #define REG_SC_CLKRST0 0x301a
55 #define REG_SC_CLKRST1 0x301b
56 #define REG_SC_CLKRST2 0x301c
57 #define REG_SC_CLKRST3 0x301d
58 #define REG_SC_SUB_ID 0x302a
59 #define REG_SC_SCCB_ID 0x302b
61 #define REG_GROUP_ADDRESS_00 0x3200
62 #define REG_GROUP_ADDRESS_01 0x3201
63 #define REG_GROUP_ADDRESS_02 0x3202
64 #define REG_GROUP_ADDRESS_03 0x3203
65 #define REG_GROUP_ACCESS 0x3208
67 #define REG_AWB_R_GAIN_H 0x3400
68 #define REG_AWB_R_GAIN_L 0x3401
69 #define REG_AWB_G_GAIN_H 0x3402
70 #define REG_AWB_G_GAIN_L 0x3403
71 #define REG_AWB_B_GAIN_H 0x3404
72 #define REG_AWB_B_GAIN_L 0x3405
73 #define REG_AWB_MANUAL_CONTROL 0x3406
75 #define REG_TIMING_HS_H 0x3800
76 #define REG_TIMING_HS_L 0x3801
77 #define REG_TIMING_VS_H 0x3802
78 #define REG_TIMING_VS_L 0x3803
79 #define REG_TIMING_HW_H 0x3804
80 #define REG_TIMING_HW_L 0x3805
81 #define REG_TIMING_VH_H 0x3806
82 #define REG_TIMING_VH_L 0x3807
83 #define REG_TIMING_DVPHO_H 0x3808
84 #define REG_TIMING_DVPHO_L 0x3809
85 #define REG_TIMING_DVPVO_H 0x380a
86 #define REG_TIMING_DVPVO_L 0x380b
87 #define REG_TIMING_HTS_H 0x380c
88 #define REG_TIMING_HTS_L 0x380d
89 #define REG_TIMING_VTS_H 0x380e
90 #define REG_TIMING_VTS_L 0x380f
91 #define REG_TIMING_HOFFS_H 0x3810
92 #define REG_TIMING_HOFFS_L 0x3811
93 #define REG_TIMING_VOFFS_H 0x3812
94 #define REG_TIMING_VOFFS_L 0x3813
95 #define REG_TIMING_XINC 0x3814
96 #define REG_TIMING_YINC 0x3815
97 #define REG_TIMING_VERT_FORMAT 0x3820
98 #define REG_TIMING_HORIZ_FORMAT 0x3821
100 #define REG_FORMAT_CTRL00 0x4300
102 #define REG_VFIFO_READ_START_H 0x4608
103 #define REG_VFIFO_READ_START_L 0x4609
105 #define REG_DVP_CTRL02 0x4708
107 #define REG_ISP_CTRL00 0x5000
108 #define REG_ISP_CTRL01 0x5001
109 #define REG_ISP_CTRL02 0x5002
111 #define REG_LENC_RED_X0_H 0x500c
112 #define REG_LENC_RED_X0_L 0x500d
113 #define REG_LENC_RED_Y0_H 0x500e
114 #define REG_LENC_RED_Y0_L 0x500f
115 #define REG_LENC_RED_A1 0x5010
116 #define REG_LENC_RED_B1 0x5011
117 #define REG_LENC_RED_A2_B2 0x5012
118 #define REG_LENC_GREEN_X0_H 0x5013
119 #define REG_LENC_GREEN_X0_L 0x5014
120 #define REG_LENC_GREEN_Y0_H 0x5015
121 #define REG_LENC_GREEN_Y0_L 0x5016
122 #define REG_LENC_GREEN_A1 0x5017
123 #define REG_LENC_GREEN_B1 0x5018
124 #define REG_LENC_GREEN_A2_B2 0x5019
125 #define REG_LENC_BLUE_X0_H 0x501a
126 #define REG_LENC_BLUE_X0_L 0x501b
127 #define REG_LENC_BLUE_Y0_H 0x501c
128 #define REG_LENC_BLUE_Y0_L 0x501d
129 #define REG_LENC_BLUE_A1 0x501e
130 #define REG_LENC_BLUE_B1 0x501f
131 #define REG_LENC_BLUE_A2_B2 0x5020
133 #define REG_AWB_CTRL00 0x5035
134 #define REG_AWB_CTRL01 0x5036
135 #define REG_AWB_CTRL02 0x5037
136 #define REG_AWB_CTRL03 0x5038
137 #define REG_AWB_CTRL04 0x5039
138 #define REG_AWB_LOCAL_LIMIT 0x503a
139 #define REG_AWB_CTRL12 0x5049
140 #define REG_AWB_CTRL13 0x504a
141 #define REG_AWB_CTRL14 0x504b
143 #define REG_SHARPENMT_THRESH1 0x5064
144 #define REG_SHARPENMT_THRESH2 0x5065
145 #define REG_SHARPENMT_OFFSET1 0x5066
146 #define REG_SHARPENMT_OFFSET2 0x5067
147 #define REG_DENOISE_THRESH1 0x5068
148 #define REG_DENOISE_THRESH2 0x5069
149 #define REG_DENOISE_OFFSET1 0x506a
150 #define REG_DENOISE_OFFSET2 0x506b
151 #define REG_SHARPEN_THRESH1 0x506c
152 #define REG_SHARPEN_THRESH2 0x506d
153 #define REG_CIP_CTRL00 0x506e
154 #define REG_CIP_CTRL01 0x506f
156 #define REG_CMX_SIGN 0x5079
157 #define REG_CMX_MISC_CTRL 0x507a
159 #define REG_PRE_ISP_CTRL00 0x50a0
160 #define TEST_PATTERN_ENABLE BIT(7)
161 #define VERTICAL_COLOR_BAR_MASK 0x53
163 #define REG_NULL 0x0000 /* Array end token */
165 #define OV265X_ID(_msb, _lsb) ((_msb) << 8 | (_lsb))
166 #define OV2659_ID 0x2656
168 struct sensor_register {
173 struct ov2659_framesize {
177 const struct sensor_register *regs;
180 struct ov2659_pll_ctrl {
186 struct ov2659_pixfmt {
188 /* Output format Register Value (REG_FORMAT_CTRL00) */
189 struct sensor_register *format_ctrl_regs;
192 struct pll_ctrl_reg {
198 struct v4l2_subdev sd;
199 struct media_pad pad;
200 struct v4l2_mbus_framefmt format;
201 unsigned int xvclk_frequency;
202 const struct ov2659_platform_data *pdata;
204 struct i2c_client *client;
205 struct v4l2_ctrl_handler ctrls;
206 struct v4l2_ctrl *link_frequency;
208 const struct ov2659_framesize *frame_size;
209 struct sensor_register *format_ctrl_regs;
210 struct ov2659_pll_ctrl pll;
212 /* used to control the sensor PWDN pin */
213 struct gpio_desc *pwdn_gpio;
214 /* used to control the sensor RESETB pin */
215 struct gpio_desc *resetb_gpio;
218 static const struct sensor_register ov2659_init_regs[] = {
219 { REG_IO_CTRL00, 0x03 },
220 { REG_IO_CTRL01, 0xff },
221 { REG_IO_CTRL02, 0xe0 },
229 { REG_TIMING_HS_H, 0x00 },
230 { REG_TIMING_HS_L, 0x00 },
231 { REG_TIMING_VS_H, 0x00 },
232 { REG_TIMING_VS_L, 0x00 },
233 { REG_TIMING_HW_H, 0x06 },
234 { REG_TIMING_HW_L, 0x5f },
235 { REG_TIMING_VH_H, 0x04 },
236 { REG_TIMING_VH_L, 0xb7 },
237 { REG_TIMING_DVPHO_H, 0x03 },
238 { REG_TIMING_DVPHO_L, 0x20 },
239 { REG_TIMING_DVPVO_H, 0x02 },
240 { REG_TIMING_DVPVO_L, 0x58 },
241 { REG_TIMING_HTS_H, 0x05 },
242 { REG_TIMING_HTS_L, 0x14 },
243 { REG_TIMING_VTS_H, 0x02 },
244 { REG_TIMING_VTS_L, 0x68 },
245 { REG_TIMING_HOFFS_L, 0x08 },
246 { REG_TIMING_VOFFS_L, 0x02 },
247 { REG_TIMING_XINC, 0x31 },
248 { REG_TIMING_YINC, 0x31 },
259 { REG_DVP_CTRL02, 0x01 },
267 { REG_TIMING_VERT_FORMAT, 0x81 },
268 { REG_TIMING_HORIZ_FORMAT, 0x01 },
270 { REG_VFIFO_READ_START_H, 0x00 },
271 { REG_VFIFO_READ_START_L, 0x80 },
272 { REG_FORMAT_CTRL00, 0x30 },
274 { REG_ISP_CTRL00, 0xfb },
275 { REG_ISP_CTRL01, 0x1f },
276 { REG_ISP_CTRL02, 0x00 },
302 { REG_CMX_SIGN, 0x98 },
303 { REG_CMX_MISC_CTRL, 0x21 },
304 { REG_AWB_CTRL00, 0x6a },
305 { REG_AWB_CTRL01, 0x11 },
306 { REG_AWB_CTRL02, 0x92 },
307 { REG_AWB_CTRL03, 0x21 },
308 { REG_AWB_CTRL04, 0xe1 },
309 { REG_AWB_LOCAL_LIMIT, 0x01 },
323 { REG_AWB_CTRL12, 0x70 },
324 { REG_AWB_CTRL13, 0xf0 },
325 { REG_AWB_CTRL14, 0xf0 },
326 { REG_LENC_RED_X0_H, 0x03 },
327 { REG_LENC_RED_X0_L, 0x20 },
328 { REG_LENC_RED_Y0_H, 0x02 },
329 { REG_LENC_RED_Y0_L, 0x5c },
330 { REG_LENC_RED_A1, 0x48 },
331 { REG_LENC_RED_B1, 0x00 },
332 { REG_LENC_RED_A2_B2, 0x66 },
333 { REG_LENC_GREEN_X0_H, 0x03 },
334 { REG_LENC_GREEN_X0_L, 0x30 },
335 { REG_LENC_GREEN_Y0_H, 0x02 },
336 { REG_LENC_GREEN_Y0_L, 0x7c },
337 { REG_LENC_GREEN_A1, 0x40 },
338 { REG_LENC_GREEN_B1, 0x00 },
339 { REG_LENC_GREEN_A2_B2, 0x66 },
340 { REG_LENC_BLUE_X0_H, 0x03 },
341 { REG_LENC_BLUE_X0_L, 0x10 },
342 { REG_LENC_BLUE_Y0_H, 0x02 },
343 { REG_LENC_BLUE_Y0_L, 0x7c },
344 { REG_LENC_BLUE_A1, 0x3a },
345 { REG_LENC_BLUE_B1, 0x00 },
346 { REG_LENC_BLUE_A2_B2, 0x66 },
347 { REG_CIP_CTRL00, 0x44 },
348 { REG_SHARPENMT_THRESH1, 0x08 },
349 { REG_SHARPENMT_THRESH2, 0x10 },
350 { REG_SHARPENMT_OFFSET1, 0x12 },
351 { REG_SHARPENMT_OFFSET2, 0x02 },
352 { REG_SHARPEN_THRESH1, 0x08 },
353 { REG_SHARPEN_THRESH2, 0x10 },
354 { REG_CIP_CTRL01, 0xa6 },
355 { REG_DENOISE_THRESH1, 0x08 },
356 { REG_DENOISE_THRESH2, 0x10 },
357 { REG_DENOISE_OFFSET1, 0x04 },
358 { REG_DENOISE_OFFSET2, 0x12 },
362 { REG_CMX_MISC_CTRL, 0x01 },
380 static struct sensor_register ov2659_720p[] = {
381 { REG_TIMING_HS_H, 0x00 },
382 { REG_TIMING_HS_L, 0xa0 },
383 { REG_TIMING_VS_H, 0x00 },
384 { REG_TIMING_VS_L, 0xf0 },
385 { REG_TIMING_HW_H, 0x05 },
386 { REG_TIMING_HW_L, 0xbf },
387 { REG_TIMING_VH_H, 0x03 },
388 { REG_TIMING_VH_L, 0xcb },
389 { REG_TIMING_DVPHO_H, 0x05 },
390 { REG_TIMING_DVPHO_L, 0x00 },
391 { REG_TIMING_DVPVO_H, 0x02 },
392 { REG_TIMING_DVPVO_L, 0xd0 },
393 { REG_TIMING_HTS_H, 0x06 },
394 { REG_TIMING_HTS_L, 0x4c },
395 { REG_TIMING_VTS_H, 0x02 },
396 { REG_TIMING_VTS_L, 0xe8 },
397 { REG_TIMING_HOFFS_L, 0x10 },
398 { REG_TIMING_VOFFS_L, 0x06 },
399 { REG_TIMING_XINC, 0x11 },
400 { REG_TIMING_YINC, 0x11 },
401 { REG_TIMING_VERT_FORMAT, 0x80 },
402 { REG_TIMING_HORIZ_FORMAT, 0x00 },
408 { REG_VFIFO_READ_START_H, 0x00 },
409 { REG_VFIFO_READ_START_L, 0x80 },
410 { REG_ISP_CTRL02, 0x00 },
415 static struct sensor_register ov2659_uxga[] = {
416 { REG_TIMING_HS_H, 0x00 },
417 { REG_TIMING_HS_L, 0x00 },
418 { REG_TIMING_VS_H, 0x00 },
419 { REG_TIMING_VS_L, 0x00 },
420 { REG_TIMING_HW_H, 0x06 },
421 { REG_TIMING_HW_L, 0x5f },
422 { REG_TIMING_VH_H, 0x04 },
423 { REG_TIMING_VH_L, 0xbb },
424 { REG_TIMING_DVPHO_H, 0x06 },
425 { REG_TIMING_DVPHO_L, 0x40 },
426 { REG_TIMING_DVPVO_H, 0x04 },
427 { REG_TIMING_DVPVO_L, 0xb0 },
428 { REG_TIMING_HTS_H, 0x07 },
429 { REG_TIMING_HTS_L, 0x9f },
430 { REG_TIMING_VTS_H, 0x04 },
431 { REG_TIMING_VTS_L, 0xd0 },
432 { REG_TIMING_HOFFS_L, 0x10 },
433 { REG_TIMING_VOFFS_L, 0x06 },
434 { REG_TIMING_XINC, 0x11 },
435 { REG_TIMING_YINC, 0x11 },
453 { REG_TIMING_VERT_FORMAT, 0x80 },
454 { REG_TIMING_HORIZ_FORMAT, 0x00 },
456 { REG_VFIFO_READ_START_H, 0x00 },
457 { REG_VFIFO_READ_START_L, 0x80 },
458 { REG_ISP_CTRL02, 0x00 },
463 static struct sensor_register ov2659_sxga[] = {
464 { REG_TIMING_HS_H, 0x00 },
465 { REG_TIMING_HS_L, 0x00 },
466 { REG_TIMING_VS_H, 0x00 },
467 { REG_TIMING_VS_L, 0x00 },
468 { REG_TIMING_HW_H, 0x06 },
469 { REG_TIMING_HW_L, 0x5f },
470 { REG_TIMING_VH_H, 0x04 },
471 { REG_TIMING_VH_L, 0xb7 },
472 { REG_TIMING_DVPHO_H, 0x05 },
473 { REG_TIMING_DVPHO_L, 0x00 },
474 { REG_TIMING_DVPVO_H, 0x04 },
475 { REG_TIMING_DVPVO_L, 0x00 },
476 { REG_TIMING_HTS_H, 0x07 },
477 { REG_TIMING_HTS_L, 0x9c },
478 { REG_TIMING_VTS_H, 0x04 },
479 { REG_TIMING_VTS_L, 0xd0 },
480 { REG_TIMING_HOFFS_L, 0x10 },
481 { REG_TIMING_VOFFS_L, 0x06 },
482 { REG_TIMING_XINC, 0x11 },
483 { REG_TIMING_YINC, 0x11 },
501 { REG_TIMING_VERT_FORMAT, 0x80 },
502 { REG_TIMING_HORIZ_FORMAT, 0x00 },
504 { REG_VFIFO_READ_START_H, 0x00 },
505 { REG_VFIFO_READ_START_L, 0x80 },
506 { REG_ISP_CTRL02, 0x00 },
511 static struct sensor_register ov2659_xga[] = {
512 { REG_TIMING_HS_H, 0x00 },
513 { REG_TIMING_HS_L, 0x00 },
514 { REG_TIMING_VS_H, 0x00 },
515 { REG_TIMING_VS_L, 0x00 },
516 { REG_TIMING_HW_H, 0x06 },
517 { REG_TIMING_HW_L, 0x5f },
518 { REG_TIMING_VH_H, 0x04 },
519 { REG_TIMING_VH_L, 0xb7 },
520 { REG_TIMING_DVPHO_H, 0x04 },
521 { REG_TIMING_DVPHO_L, 0x00 },
522 { REG_TIMING_DVPVO_H, 0x03 },
523 { REG_TIMING_DVPVO_L, 0x00 },
524 { REG_TIMING_HTS_H, 0x07 },
525 { REG_TIMING_HTS_L, 0x9c },
526 { REG_TIMING_VTS_H, 0x04 },
527 { REG_TIMING_VTS_L, 0xd0 },
528 { REG_TIMING_HOFFS_L, 0x10 },
529 { REG_TIMING_VOFFS_L, 0x06 },
530 { REG_TIMING_XINC, 0x11 },
531 { REG_TIMING_YINC, 0x11 },
549 { REG_TIMING_VERT_FORMAT, 0x80 },
550 { REG_TIMING_HORIZ_FORMAT, 0x00 },
552 { REG_VFIFO_READ_START_H, 0x00 },
553 { REG_VFIFO_READ_START_L, 0x80 },
554 { REG_ISP_CTRL02, 0x00 },
559 static struct sensor_register ov2659_svga[] = {
560 { REG_TIMING_HS_H, 0x00 },
561 { REG_TIMING_HS_L, 0x00 },
562 { REG_TIMING_VS_H, 0x00 },
563 { REG_TIMING_VS_L, 0x00 },
564 { REG_TIMING_HW_H, 0x06 },
565 { REG_TIMING_HW_L, 0x5f },
566 { REG_TIMING_VH_H, 0x04 },
567 { REG_TIMING_VH_L, 0xb7 },
568 { REG_TIMING_DVPHO_H, 0x03 },
569 { REG_TIMING_DVPHO_L, 0x20 },
570 { REG_TIMING_DVPVO_H, 0x02 },
571 { REG_TIMING_DVPVO_L, 0x58 },
572 { REG_TIMING_HTS_H, 0x05 },
573 { REG_TIMING_HTS_L, 0x14 },
574 { REG_TIMING_VTS_H, 0x02 },
575 { REG_TIMING_VTS_L, 0x68 },
576 { REG_TIMING_HOFFS_L, 0x08 },
577 { REG_TIMING_VOFFS_L, 0x02 },
578 { REG_TIMING_XINC, 0x31 },
579 { REG_TIMING_YINC, 0x31 },
597 { REG_TIMING_VERT_FORMAT, 0x81 },
598 { REG_TIMING_HORIZ_FORMAT, 0x01 },
600 { REG_VFIFO_READ_START_H, 0x00 },
601 { REG_VFIFO_READ_START_L, 0x80 },
602 { REG_ISP_CTRL02, 0x00 },
607 static struct sensor_register ov2659_vga[] = {
608 { REG_TIMING_HS_H, 0x00 },
609 { REG_TIMING_HS_L, 0x00 },
610 { REG_TIMING_VS_H, 0x00 },
611 { REG_TIMING_VS_L, 0x00 },
612 { REG_TIMING_HW_H, 0x06 },
613 { REG_TIMING_HW_L, 0x5f },
614 { REG_TIMING_VH_H, 0x04 },
615 { REG_TIMING_VH_L, 0xb7 },
616 { REG_TIMING_DVPHO_H, 0x02 },
617 { REG_TIMING_DVPHO_L, 0x80 },
618 { REG_TIMING_DVPVO_H, 0x01 },
619 { REG_TIMING_DVPVO_L, 0xe0 },
620 { REG_TIMING_HTS_H, 0x05 },
621 { REG_TIMING_HTS_L, 0x14 },
622 { REG_TIMING_VTS_H, 0x02 },
623 { REG_TIMING_VTS_L, 0x68 },
624 { REG_TIMING_HOFFS_L, 0x08 },
625 { REG_TIMING_VOFFS_L, 0x02 },
626 { REG_TIMING_XINC, 0x31 },
627 { REG_TIMING_YINC, 0x31 },
645 { REG_TIMING_VERT_FORMAT, 0x81 },
646 { REG_TIMING_HORIZ_FORMAT, 0x01 },
648 { REG_VFIFO_READ_START_H, 0x00 },
649 { REG_VFIFO_READ_START_L, 0xa0 },
650 { REG_ISP_CTRL02, 0x10 },
655 static struct sensor_register ov2659_qvga[] = {
656 { REG_TIMING_HS_H, 0x00 },
657 { REG_TIMING_HS_L, 0x00 },
658 { REG_TIMING_VS_H, 0x00 },
659 { REG_TIMING_VS_L, 0x00 },
660 { REG_TIMING_HW_H, 0x06 },
661 { REG_TIMING_HW_L, 0x5f },
662 { REG_TIMING_VH_H, 0x04 },
663 { REG_TIMING_VH_L, 0xb7 },
664 { REG_TIMING_DVPHO_H, 0x01 },
665 { REG_TIMING_DVPHO_L, 0x40 },
666 { REG_TIMING_DVPVO_H, 0x00 },
667 { REG_TIMING_DVPVO_L, 0xf0 },
668 { REG_TIMING_HTS_H, 0x05 },
669 { REG_TIMING_HTS_L, 0x14 },
670 { REG_TIMING_VTS_H, 0x02 },
671 { REG_TIMING_VTS_L, 0x68 },
672 { REG_TIMING_HOFFS_L, 0x08 },
673 { REG_TIMING_VOFFS_L, 0x02 },
674 { REG_TIMING_XINC, 0x31 },
675 { REG_TIMING_YINC, 0x31 },
693 { REG_TIMING_VERT_FORMAT, 0x81 },
694 { REG_TIMING_HORIZ_FORMAT, 0x01 },
696 { REG_VFIFO_READ_START_H, 0x00 },
697 { REG_VFIFO_READ_START_L, 0xa0 },
698 { REG_ISP_CTRL02, 0x10 },
702 static const struct pll_ctrl_reg ctrl3[] = {
719 static const struct pll_ctrl_reg ctrl1[] = {
738 static const struct ov2659_framesize ov2659_framesizes[] = {
743 .max_exp_lines = 248,
748 .max_exp_lines = 498,
753 .max_exp_lines = 498,
758 .max_exp_lines = 498,
763 .max_exp_lines = 498,
768 .max_exp_lines = 1048,
773 .max_exp_lines = 498,
778 static struct sensor_register ov2659_format_yuyv[] = {
779 { REG_FORMAT_CTRL00, 0x30 },
784 static struct sensor_register ov2659_format_uyvy[] = {
785 { REG_FORMAT_CTRL00, 0x32 },
790 static struct sensor_register ov2659_format_bggr[] = {
791 { REG_FORMAT_CTRL00, 0x00 },
796 static struct sensor_register ov2659_format_rgb565[] = {
797 { REG_FORMAT_CTRL00, 0x60 },
801 static const struct ov2659_pixfmt ov2659_formats[] = {
803 .code = MEDIA_BUS_FMT_YUYV8_2X8,
804 .format_ctrl_regs = ov2659_format_yuyv,
806 .code = MEDIA_BUS_FMT_UYVY8_2X8,
807 .format_ctrl_regs = ov2659_format_uyvy,
809 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
810 .format_ctrl_regs = ov2659_format_rgb565,
812 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
813 .format_ctrl_regs = ov2659_format_bggr,
817 static inline struct ov2659 *to_ov2659(struct v4l2_subdev *sd)
819 return container_of(sd, struct ov2659, sd);
822 /* sensor register write */
823 static int ov2659_write(struct i2c_client *client, u16 reg, u8 val)
833 msg.addr = client->addr;
834 msg.flags = client->flags;
836 msg.len = sizeof(buf);
838 ret = i2c_transfer(client->adapter, &msg, 1);
842 dev_dbg(&client->dev,
843 "ov2659 write reg(0x%x val:0x%x) failed !\n", reg, val);
848 /* sensor register read */
849 static int ov2659_read(struct i2c_client *client, u16 reg, u8 *val)
851 struct i2c_msg msg[2];
858 msg[0].addr = client->addr;
859 msg[0].flags = client->flags;
861 msg[0].len = sizeof(buf);
863 msg[1].addr = client->addr;
864 msg[1].flags = client->flags | I2C_M_RD;
868 ret = i2c_transfer(client->adapter, msg, 2);
874 dev_dbg(&client->dev,
875 "ov2659 read reg(0x%x val:0x%x) failed !\n", reg, *val);
880 static int ov2659_write_array(struct i2c_client *client,
881 const struct sensor_register *regs)
885 for (i = 0; ret == 0 && regs[i].addr; i++)
886 ret = ov2659_write(client, regs[i].addr, regs[i].value);
891 static void ov2659_pll_calc_params(struct ov2659 *ov2659)
893 const struct ov2659_platform_data *pdata = ov2659->pdata;
894 u8 ctrl1_reg = 0, ctrl2_reg = 0, ctrl3_reg = 0;
895 struct i2c_client *client = ov2659->client;
896 unsigned int desired = pdata->link_frequency;
897 u32 prediv, postdiv, mult;
902 for (i = 0; ctrl1[i].div != 0; i++) {
903 postdiv = ctrl1[i].div;
904 for (j = 0; ctrl3[j].div != 0; j++) {
905 prediv = ctrl3[j].div;
906 for (mult = 1; mult <= 63; mult++) {
907 actual = ov2659->xvclk_frequency;
911 delta = actual - desired;
914 if ((delta < bestdelta) || (bestdelta == -1)) {
916 ctrl1_reg = ctrl1[i].reg;
918 ctrl3_reg = ctrl3[j].reg;
924 ov2659->pll.ctrl1 = ctrl1_reg;
925 ov2659->pll.ctrl2 = ctrl2_reg;
926 ov2659->pll.ctrl3 = ctrl3_reg;
928 dev_dbg(&client->dev,
929 "Actual reg config: ctrl1_reg: %02x ctrl2_reg: %02x ctrl3_reg: %02x\n",
930 ctrl1_reg, ctrl2_reg, ctrl3_reg);
933 static int ov2659_set_pixel_clock(struct ov2659 *ov2659)
935 struct i2c_client *client = ov2659->client;
936 struct sensor_register pll_regs[] = {
937 {REG_SC_PLL_CTRL1, ov2659->pll.ctrl1},
938 {REG_SC_PLL_CTRL2, ov2659->pll.ctrl2},
939 {REG_SC_PLL_CTRL3, ov2659->pll.ctrl3},
943 dev_dbg(&client->dev, "%s\n", __func__);
945 return ov2659_write_array(client, pll_regs);
948 static void ov2659_get_default_format(struct v4l2_mbus_framefmt *format)
950 format->width = ov2659_framesizes[2].width;
951 format->height = ov2659_framesizes[2].height;
952 format->colorspace = V4L2_COLORSPACE_SRGB;
953 format->code = ov2659_formats[0].code;
954 format->field = V4L2_FIELD_NONE;
957 static void ov2659_set_streaming(struct ov2659 *ov2659, int on)
959 struct i2c_client *client = ov2659->client;
964 dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
966 ret = ov2659_write(client, REG_SOFTWARE_STANDBY, on);
968 dev_err(&client->dev, "ov2659 soft standby failed\n");
971 static int ov2659_init(struct v4l2_subdev *sd, u32 val)
973 struct i2c_client *client = v4l2_get_subdevdata(sd);
975 return ov2659_write_array(client, ov2659_init_regs);
979 * V4L2 subdev video and pad level operations
982 static int ov2659_enum_mbus_code(struct v4l2_subdev *sd,
983 struct v4l2_subdev_state *sd_state,
984 struct v4l2_subdev_mbus_code_enum *code)
986 struct i2c_client *client = v4l2_get_subdevdata(sd);
988 dev_dbg(&client->dev, "%s:\n", __func__);
990 if (code->index >= ARRAY_SIZE(ov2659_formats))
993 code->code = ov2659_formats[code->index].code;
998 static int ov2659_enum_frame_sizes(struct v4l2_subdev *sd,
999 struct v4l2_subdev_state *sd_state,
1000 struct v4l2_subdev_frame_size_enum *fse)
1002 struct i2c_client *client = v4l2_get_subdevdata(sd);
1003 int i = ARRAY_SIZE(ov2659_formats);
1005 dev_dbg(&client->dev, "%s:\n", __func__);
1007 if (fse->index >= ARRAY_SIZE(ov2659_framesizes))
1011 if (fse->code == ov2659_formats[i].code)
1014 fse->code = ov2659_formats[i].code;
1016 fse->min_width = ov2659_framesizes[fse->index].width;
1017 fse->max_width = fse->min_width;
1018 fse->max_height = ov2659_framesizes[fse->index].height;
1019 fse->min_height = fse->max_height;
1024 static int ov2659_get_fmt(struct v4l2_subdev *sd,
1025 struct v4l2_subdev_state *sd_state,
1026 struct v4l2_subdev_format *fmt)
1028 struct i2c_client *client = v4l2_get_subdevdata(sd);
1029 struct ov2659 *ov2659 = to_ov2659(sd);
1031 dev_dbg(&client->dev, "ov2659_get_fmt\n");
1033 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1034 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1035 struct v4l2_mbus_framefmt *mf;
1037 mf = v4l2_subdev_get_try_format(sd, sd_state, 0);
1038 mutex_lock(&ov2659->lock);
1040 mutex_unlock(&ov2659->lock);
1047 mutex_lock(&ov2659->lock);
1048 fmt->format = ov2659->format;
1049 mutex_unlock(&ov2659->lock);
1051 dev_dbg(&client->dev, "ov2659_get_fmt: %x %dx%d\n",
1052 ov2659->format.code, ov2659->format.width,
1053 ov2659->format.height);
1058 static void __ov2659_try_frame_size(struct v4l2_mbus_framefmt *mf,
1059 const struct ov2659_framesize **size)
1061 const struct ov2659_framesize *fsize = &ov2659_framesizes[0];
1062 const struct ov2659_framesize *match = NULL;
1063 int i = ARRAY_SIZE(ov2659_framesizes);
1064 unsigned int min_err = UINT_MAX;
1067 int err = abs(fsize->width - mf->width)
1068 + abs(fsize->height - mf->height);
1069 if ((err < min_err) && (fsize->regs[0].addr)) {
1077 match = &ov2659_framesizes[2];
1079 mf->width = match->width;
1080 mf->height = match->height;
1086 static int ov2659_set_fmt(struct v4l2_subdev *sd,
1087 struct v4l2_subdev_state *sd_state,
1088 struct v4l2_subdev_format *fmt)
1090 struct i2c_client *client = v4l2_get_subdevdata(sd);
1091 int index = ARRAY_SIZE(ov2659_formats);
1092 struct v4l2_mbus_framefmt *mf = &fmt->format;
1093 const struct ov2659_framesize *size = NULL;
1094 struct ov2659 *ov2659 = to_ov2659(sd);
1097 dev_dbg(&client->dev, "ov2659_set_fmt\n");
1099 __ov2659_try_frame_size(mf, &size);
1101 while (--index >= 0)
1102 if (ov2659_formats[index].code == mf->code)
1107 mf->code = ov2659_formats[index].code;
1110 mf->colorspace = V4L2_COLORSPACE_SRGB;
1111 mf->field = V4L2_FIELD_NONE;
1113 mutex_lock(&ov2659->lock);
1115 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1116 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1117 mf = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
1123 if (ov2659->streaming) {
1124 mutex_unlock(&ov2659->lock);
1128 ov2659->frame_size = size;
1129 ov2659->format = fmt->format;
1130 ov2659->format_ctrl_regs =
1131 ov2659_formats[index].format_ctrl_regs;
1133 if (ov2659->format.code != MEDIA_BUS_FMT_SBGGR8_1X8)
1134 val = ov2659->pdata->link_frequency / 2;
1136 val = ov2659->pdata->link_frequency;
1138 ret = v4l2_ctrl_s_ctrl_int64(ov2659->link_frequency, val);
1140 dev_warn(&client->dev,
1141 "failed to set link_frequency rate (%d)\n",
1145 mutex_unlock(&ov2659->lock);
1149 static int ov2659_set_frame_size(struct ov2659 *ov2659)
1151 struct i2c_client *client = ov2659->client;
1153 dev_dbg(&client->dev, "%s\n", __func__);
1155 return ov2659_write_array(ov2659->client, ov2659->frame_size->regs);
1158 static int ov2659_set_format(struct ov2659 *ov2659)
1160 struct i2c_client *client = ov2659->client;
1162 dev_dbg(&client->dev, "%s\n", __func__);
1164 return ov2659_write_array(ov2659->client, ov2659->format_ctrl_regs);
1167 static int ov2659_s_stream(struct v4l2_subdev *sd, int on)
1169 struct i2c_client *client = v4l2_get_subdevdata(sd);
1170 struct ov2659 *ov2659 = to_ov2659(sd);
1173 dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
1175 mutex_lock(&ov2659->lock);
1179 if (ov2659->streaming == on)
1183 /* Stop Streaming Sequence */
1184 ov2659_set_streaming(ov2659, 0);
1185 ov2659->streaming = on;
1186 pm_runtime_put(&client->dev);
1190 ret = pm_runtime_resume_and_get(&client->dev);
1194 ret = ov2659_init(sd, 0);
1196 ret = ov2659_set_pixel_clock(ov2659);
1198 ret = ov2659_set_frame_size(ov2659);
1200 ret = ov2659_set_format(ov2659);
1202 ov2659_set_streaming(ov2659, 1);
1203 ov2659->streaming = on;
1207 mutex_unlock(&ov2659->lock);
1211 static int ov2659_set_test_pattern(struct ov2659 *ov2659, int value)
1213 struct i2c_client *client = v4l2_get_subdevdata(&ov2659->sd);
1217 ret = ov2659_read(client, REG_PRE_ISP_CTRL00, &val);
1223 val &= ~TEST_PATTERN_ENABLE;
1226 val &= VERTICAL_COLOR_BAR_MASK;
1227 val |= TEST_PATTERN_ENABLE;
1231 return ov2659_write(client, REG_PRE_ISP_CTRL00, val);
1234 static int ov2659_s_ctrl(struct v4l2_ctrl *ctrl)
1236 struct ov2659 *ov2659 =
1237 container_of(ctrl->handler, struct ov2659, ctrls);
1238 struct i2c_client *client = ov2659->client;
1240 /* V4L2 controls values will be applied only when power is already up */
1241 if (!pm_runtime_get_if_in_use(&client->dev))
1245 case V4L2_CID_TEST_PATTERN:
1246 return ov2659_set_test_pattern(ov2659, ctrl->val);
1249 pm_runtime_put(&client->dev);
1253 static const struct v4l2_ctrl_ops ov2659_ctrl_ops = {
1254 .s_ctrl = ov2659_s_ctrl,
1257 static const char * const ov2659_test_pattern_menu[] = {
1259 "Vertical Color Bars",
1262 static int ov2659_power_off(struct device *dev)
1264 struct i2c_client *client = to_i2c_client(dev);
1265 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1266 struct ov2659 *ov2659 = to_ov2659(sd);
1268 dev_dbg(&client->dev, "%s:\n", __func__);
1270 gpiod_set_value(ov2659->pwdn_gpio, 1);
1272 clk_disable_unprepare(ov2659->clk);
1277 static int ov2659_power_on(struct device *dev)
1279 struct i2c_client *client = to_i2c_client(dev);
1280 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1281 struct ov2659 *ov2659 = to_ov2659(sd);
1284 dev_dbg(&client->dev, "%s:\n", __func__);
1286 ret = clk_prepare_enable(ov2659->clk);
1288 dev_err(&client->dev, "%s: failed to enable clock\n",
1293 gpiod_set_value(ov2659->pwdn_gpio, 0);
1295 if (ov2659->resetb_gpio) {
1296 gpiod_set_value(ov2659->resetb_gpio, 1);
1297 usleep_range(500, 1000);
1298 gpiod_set_value(ov2659->resetb_gpio, 0);
1299 usleep_range(3000, 5000);
1305 /* -----------------------------------------------------------------------------
1306 * V4L2 subdev internal operations
1309 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1310 static int ov2659_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1312 struct i2c_client *client = v4l2_get_subdevdata(sd);
1313 struct v4l2_mbus_framefmt *format =
1314 v4l2_subdev_get_try_format(sd, fh->state, 0);
1316 dev_dbg(&client->dev, "%s:\n", __func__);
1318 ov2659_get_default_format(format);
1324 static const struct v4l2_subdev_core_ops ov2659_subdev_core_ops = {
1325 .log_status = v4l2_ctrl_subdev_log_status,
1326 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1327 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1330 static const struct v4l2_subdev_video_ops ov2659_subdev_video_ops = {
1331 .s_stream = ov2659_s_stream,
1334 static const struct v4l2_subdev_pad_ops ov2659_subdev_pad_ops = {
1335 .enum_mbus_code = ov2659_enum_mbus_code,
1336 .enum_frame_size = ov2659_enum_frame_sizes,
1337 .get_fmt = ov2659_get_fmt,
1338 .set_fmt = ov2659_set_fmt,
1341 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1342 static const struct v4l2_subdev_ops ov2659_subdev_ops = {
1343 .core = &ov2659_subdev_core_ops,
1344 .video = &ov2659_subdev_video_ops,
1345 .pad = &ov2659_subdev_pad_ops,
1348 static const struct v4l2_subdev_internal_ops ov2659_subdev_internal_ops = {
1349 .open = ov2659_open,
1353 static int ov2659_detect(struct v4l2_subdev *sd)
1355 struct i2c_client *client = v4l2_get_subdevdata(sd);
1360 dev_dbg(&client->dev, "%s:\n", __func__);
1362 ret = ov2659_write(client, REG_SOFTWARE_RESET, 0x01);
1364 dev_err(&client->dev, "Sensor soft reset failed\n");
1367 usleep_range(1000, 2000);
1369 /* Check sensor revision */
1370 ret = ov2659_read(client, REG_SC_CHIP_ID_H, &pid);
1372 ret = ov2659_read(client, REG_SC_CHIP_ID_L, &ver);
1377 id = OV265X_ID(pid, ver);
1378 if (id != OV2659_ID) {
1379 dev_err(&client->dev,
1380 "Sensor detection failed (%04X)\n", id);
1383 dev_info(&client->dev, "Found OV%04X sensor\n", id);
1390 static struct ov2659_platform_data *
1391 ov2659_get_pdata(struct i2c_client *client)
1393 struct ov2659_platform_data *pdata;
1394 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
1395 struct device_node *endpoint;
1398 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
1399 return client->dev.platform_data;
1401 endpoint = of_graph_get_next_endpoint(client->dev.of_node, NULL);
1405 ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(endpoint),
1412 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1416 if (!bus_cfg.nr_of_link_frequencies) {
1417 dev_err(&client->dev,
1418 "link-frequencies property not found or too many\n");
1423 pdata->link_frequency = bus_cfg.link_frequencies[0];
1426 v4l2_fwnode_endpoint_free(&bus_cfg);
1427 of_node_put(endpoint);
1431 static int ov2659_probe(struct i2c_client *client)
1433 const struct ov2659_platform_data *pdata = ov2659_get_pdata(client);
1434 struct v4l2_subdev *sd;
1435 struct ov2659 *ov2659;
1439 dev_err(&client->dev, "platform data not specified\n");
1443 ov2659 = devm_kzalloc(&client->dev, sizeof(*ov2659), GFP_KERNEL);
1447 ov2659->pdata = pdata;
1448 ov2659->client = client;
1450 ov2659->clk = devm_clk_get(&client->dev, "xvclk");
1451 if (IS_ERR(ov2659->clk))
1452 return PTR_ERR(ov2659->clk);
1454 ov2659->xvclk_frequency = clk_get_rate(ov2659->clk);
1455 if (ov2659->xvclk_frequency < 6000000 ||
1456 ov2659->xvclk_frequency > 27000000)
1459 /* Optional gpio don't fail if not present */
1460 ov2659->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
1462 if (IS_ERR(ov2659->pwdn_gpio))
1463 return PTR_ERR(ov2659->pwdn_gpio);
1465 /* Optional gpio don't fail if not present */
1466 ov2659->resetb_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1468 if (IS_ERR(ov2659->resetb_gpio))
1469 return PTR_ERR(ov2659->resetb_gpio);
1471 v4l2_ctrl_handler_init(&ov2659->ctrls, 2);
1472 ov2659->link_frequency =
1473 v4l2_ctrl_new_std(&ov2659->ctrls, &ov2659_ctrl_ops,
1474 V4L2_CID_PIXEL_RATE,
1475 pdata->link_frequency / 2,
1476 pdata->link_frequency, 1,
1477 pdata->link_frequency);
1478 v4l2_ctrl_new_std_menu_items(&ov2659->ctrls, &ov2659_ctrl_ops,
1479 V4L2_CID_TEST_PATTERN,
1480 ARRAY_SIZE(ov2659_test_pattern_menu) - 1,
1481 0, 0, ov2659_test_pattern_menu);
1482 ov2659->sd.ctrl_handler = &ov2659->ctrls;
1484 if (ov2659->ctrls.error) {
1485 dev_err(&client->dev, "%s: control initialization error %d\n",
1486 __func__, ov2659->ctrls.error);
1487 return ov2659->ctrls.error;
1491 client->flags |= I2C_CLIENT_SCCB;
1492 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1493 v4l2_i2c_subdev_init(sd, client, &ov2659_subdev_ops);
1495 sd->internal_ops = &ov2659_subdev_internal_ops;
1496 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1497 V4L2_SUBDEV_FL_HAS_EVENTS;
1500 #if defined(CONFIG_MEDIA_CONTROLLER)
1501 ov2659->pad.flags = MEDIA_PAD_FL_SOURCE;
1502 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1503 ret = media_entity_pads_init(&sd->entity, 1, &ov2659->pad);
1505 v4l2_ctrl_handler_free(&ov2659->ctrls);
1510 mutex_init(&ov2659->lock);
1512 ov2659_get_default_format(&ov2659->format);
1513 ov2659->frame_size = &ov2659_framesizes[2];
1514 ov2659->format_ctrl_regs = ov2659_formats[0].format_ctrl_regs;
1516 ret = ov2659_power_on(&client->dev);
1520 ret = ov2659_detect(sd);
1524 /* Calculate the PLL register value needed */
1525 ov2659_pll_calc_params(ov2659);
1527 ret = v4l2_async_register_subdev(&ov2659->sd);
1531 dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name);
1533 pm_runtime_set_active(&client->dev);
1534 pm_runtime_enable(&client->dev);
1535 pm_runtime_idle(&client->dev);
1540 v4l2_ctrl_handler_free(&ov2659->ctrls);
1541 ov2659_power_off(&client->dev);
1542 media_entity_cleanup(&sd->entity);
1543 mutex_destroy(&ov2659->lock);
1547 static void ov2659_remove(struct i2c_client *client)
1549 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1550 struct ov2659 *ov2659 = to_ov2659(sd);
1552 v4l2_ctrl_handler_free(&ov2659->ctrls);
1553 v4l2_async_unregister_subdev(sd);
1554 media_entity_cleanup(&sd->entity);
1555 mutex_destroy(&ov2659->lock);
1557 pm_runtime_disable(&client->dev);
1558 if (!pm_runtime_status_suspended(&client->dev))
1559 ov2659_power_off(&client->dev);
1560 pm_runtime_set_suspended(&client->dev);
1563 static const struct dev_pm_ops ov2659_pm_ops = {
1564 SET_RUNTIME_PM_OPS(ov2659_power_off, ov2659_power_on, NULL)
1567 static const struct i2c_device_id ov2659_id[] = {
1571 MODULE_DEVICE_TABLE(i2c, ov2659_id);
1573 #if IS_ENABLED(CONFIG_OF)
1574 static const struct of_device_id ov2659_of_match[] = {
1575 { .compatible = "ovti,ov2659", },
1578 MODULE_DEVICE_TABLE(of, ov2659_of_match);
1581 static struct i2c_driver ov2659_i2c_driver = {
1583 .name = DRIVER_NAME,
1584 .pm = &ov2659_pm_ops,
1585 .of_match_table = of_match_ptr(ov2659_of_match),
1587 .probe = ov2659_probe,
1588 .remove = ov2659_remove,
1589 .id_table = ov2659_id,
1592 module_i2c_driver(ov2659_i2c_driver);
1594 MODULE_AUTHOR("Benoit Parrot <bparrot@ti.com>");
1595 MODULE_DESCRIPTION("OV2659 CMOS Image Sensor driver");
1596 MODULE_LICENSE("GPL v2");