1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Copyright (C) 2014-2017 Mentor Graphics Inc.
8 #include <linux/clk-provider.h>
9 #include <linux/clkdev.h>
10 #include <linux/ctype.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-fwnode.h>
27 #include <media/v4l2-subdev.h>
29 /* min/typical/max system clock (xclk) frequencies */
30 #define OV5640_XCLK_MIN 6000000
31 #define OV5640_XCLK_MAX 54000000
33 #define OV5640_NATIVE_WIDTH 2624
34 #define OV5640_NATIVE_HEIGHT 1964
35 #define OV5640_PIXEL_ARRAY_TOP 14
36 #define OV5640_PIXEL_ARRAY_LEFT 16
37 #define OV5640_PIXEL_ARRAY_WIDTH 2592
38 #define OV5640_PIXEL_ARRAY_HEIGHT 1944
40 /* FIXME: not documented. */
41 #define OV5640_MIN_VBLANK 24
42 #define OV5640_MAX_VTS 3375
44 #define OV5640_DEFAULT_SLAVE_ID 0x3c
46 #define OV5640_LINK_RATE_MAX 490000000U
48 #define OV5640_REG_SYS_RESET02 0x3002
49 #define OV5640_REG_SYS_CLOCK_ENABLE02 0x3006
50 #define OV5640_REG_SYS_CTRL0 0x3008
51 #define OV5640_REG_SYS_CTRL0_SW_PWDN 0x42
52 #define OV5640_REG_SYS_CTRL0_SW_PWUP 0x02
53 #define OV5640_REG_CHIP_ID 0x300a
54 #define OV5640_REG_IO_MIPI_CTRL00 0x300e
55 #define OV5640_REG_PAD_OUTPUT_ENABLE01 0x3017
56 #define OV5640_REG_PAD_OUTPUT_ENABLE02 0x3018
57 #define OV5640_REG_PAD_OUTPUT00 0x3019
58 #define OV5640_REG_SYSTEM_CONTROL1 0x302e
59 #define OV5640_REG_SC_PLL_CTRL0 0x3034
60 #define OV5640_REG_SC_PLL_CTRL1 0x3035
61 #define OV5640_REG_SC_PLL_CTRL2 0x3036
62 #define OV5640_REG_SC_PLL_CTRL3 0x3037
63 #define OV5640_REG_SLAVE_ID 0x3100
64 #define OV5640_REG_SCCB_SYS_CTRL1 0x3103
65 #define OV5640_REG_SYS_ROOT_DIVIDER 0x3108
66 #define OV5640_REG_AWB_R_GAIN 0x3400
67 #define OV5640_REG_AWB_G_GAIN 0x3402
68 #define OV5640_REG_AWB_B_GAIN 0x3404
69 #define OV5640_REG_AWB_MANUAL_CTRL 0x3406
70 #define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500
71 #define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501
72 #define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502
73 #define OV5640_REG_AEC_PK_MANUAL 0x3503
74 #define OV5640_REG_AEC_PK_REAL_GAIN 0x350a
75 #define OV5640_REG_AEC_PK_VTS 0x350c
76 #define OV5640_REG_TIMING_HS 0x3800
77 #define OV5640_REG_TIMING_VS 0x3802
78 #define OV5640_REG_TIMING_HW 0x3804
79 #define OV5640_REG_TIMING_VH 0x3806
80 #define OV5640_REG_TIMING_DVPHO 0x3808
81 #define OV5640_REG_TIMING_DVPVO 0x380a
82 #define OV5640_REG_TIMING_HTS 0x380c
83 #define OV5640_REG_TIMING_VTS 0x380e
84 #define OV5640_REG_TIMING_HOFFS 0x3810
85 #define OV5640_REG_TIMING_VOFFS 0x3812
86 #define OV5640_REG_TIMING_TC_REG20 0x3820
87 #define OV5640_REG_TIMING_TC_REG21 0x3821
88 #define OV5640_REG_AEC_CTRL00 0x3a00
89 #define OV5640_REG_AEC_B50_STEP 0x3a08
90 #define OV5640_REG_AEC_B60_STEP 0x3a0a
91 #define OV5640_REG_AEC_CTRL0D 0x3a0d
92 #define OV5640_REG_AEC_CTRL0E 0x3a0e
93 #define OV5640_REG_AEC_CTRL0F 0x3a0f
94 #define OV5640_REG_AEC_CTRL10 0x3a10
95 #define OV5640_REG_AEC_CTRL11 0x3a11
96 #define OV5640_REG_AEC_CTRL1B 0x3a1b
97 #define OV5640_REG_AEC_CTRL1E 0x3a1e
98 #define OV5640_REG_AEC_CTRL1F 0x3a1f
99 #define OV5640_REG_HZ5060_CTRL00 0x3c00
100 #define OV5640_REG_HZ5060_CTRL01 0x3c01
101 #define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c
102 #define OV5640_REG_FRAME_CTRL01 0x4202
103 #define OV5640_REG_FORMAT_CONTROL00 0x4300
104 #define OV5640_REG_VFIFO_HSIZE 0x4602
105 #define OV5640_REG_VFIFO_VSIZE 0x4604
106 #define OV5640_REG_JPG_MODE_SELECT 0x4713
107 #define OV5640_REG_CCIR656_CTRL00 0x4730
108 #define OV5640_REG_POLARITY_CTRL00 0x4740
109 #define OV5640_REG_MIPI_CTRL00 0x4800
110 #define OV5640_REG_DEBUG_MODE 0x4814
111 #define OV5640_REG_PCLK_PERIOD 0x4837
112 #define OV5640_REG_ISP_FORMAT_MUX_CTRL 0x501f
113 #define OV5640_REG_PRE_ISP_TEST_SET1 0x503d
114 #define OV5640_REG_SDE_CTRL0 0x5580
115 #define OV5640_REG_SDE_CTRL1 0x5581
116 #define OV5640_REG_SDE_CTRL3 0x5583
117 #define OV5640_REG_SDE_CTRL4 0x5584
118 #define OV5640_REG_SDE_CTRL5 0x5585
119 #define OV5640_REG_AVG_READOUT 0x56a1
121 enum ov5640_mode_id {
122 OV5640_MODE_QQVGA_160_120 = 0,
123 OV5640_MODE_QCIF_176_144,
124 OV5640_MODE_QVGA_320_240,
125 OV5640_MODE_VGA_640_480,
126 OV5640_MODE_NTSC_720_480,
127 OV5640_MODE_PAL_720_576,
128 OV5640_MODE_XGA_1024_768,
129 OV5640_MODE_720P_1280_720,
130 OV5640_MODE_1080P_1920_1080,
131 OV5640_MODE_QSXGA_2592_1944,
135 enum ov5640_frame_rate {
139 OV5640_NUM_FRAMERATES,
142 enum ov5640_pixel_rate_id {
143 OV5640_PIXEL_RATE_168M,
144 OV5640_PIXEL_RATE_148M,
145 OV5640_PIXEL_RATE_124M,
146 OV5640_PIXEL_RATE_96M,
147 OV5640_PIXEL_RATE_48M,
148 OV5640_NUM_PIXEL_RATES,
152 * The chip manual suggests 24/48/96/192 MHz pixel clocks.
154 * 192MHz exceeds the sysclk limits; use 168MHz as maximum pixel rate for
155 * full resolution mode @15 FPS.
157 static const u32 ov5640_pixel_rates[] = {
158 [OV5640_PIXEL_RATE_168M] = 168000000,
159 [OV5640_PIXEL_RATE_148M] = 148000000,
160 [OV5640_PIXEL_RATE_124M] = 124000000,
161 [OV5640_PIXEL_RATE_96M] = 96000000,
162 [OV5640_PIXEL_RATE_48M] = 48000000,
166 * MIPI CSI-2 link frequencies.
168 * Derived from the above defined pixel rate for bpp = (8, 16, 24) and
169 * data_lanes = (1, 2)
171 * link_freq = (pixel_rate * bpp) / (2 * data_lanes)
173 static const s64 ov5640_csi2_link_freqs[] = {
174 992000000, 888000000, 768000000, 744000000, 672000000, 672000000,
175 592000000, 592000000, 576000000, 576000000, 496000000, 496000000,
176 384000000, 384000000, 384000000, 336000000, 296000000, 288000000,
177 248000000, 192000000, 192000000, 192000000, 96000000,
180 /* Link freq for default mode: UYVY 16 bpp, 2 data lanes. */
181 #define OV5640_DEFAULT_LINK_FREQ 13
183 enum ov5640_format_mux {
184 OV5640_FMT_MUX_YUV422 = 0,
186 OV5640_FMT_MUX_DITHER,
187 OV5640_FMT_MUX_RAW_DPC,
188 OV5640_FMT_MUX_SNR_RAW,
189 OV5640_FMT_MUX_RAW_CIP,
192 struct ov5640_pixfmt {
197 enum ov5640_format_mux mux;
200 static const struct ov5640_pixfmt ov5640_dvp_formats[] = {
203 .code = MEDIA_BUS_FMT_JPEG_1X8,
204 .colorspace = V4L2_COLORSPACE_JPEG,
207 .mux = OV5640_FMT_MUX_YUV422,
210 .code = MEDIA_BUS_FMT_UYVY8_2X8,
211 .colorspace = V4L2_COLORSPACE_SRGB,
214 .mux = OV5640_FMT_MUX_YUV422,
217 .code = MEDIA_BUS_FMT_YUYV8_2X8,
218 .colorspace = V4L2_COLORSPACE_SRGB,
221 .mux = OV5640_FMT_MUX_YUV422,
223 /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
224 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
225 .colorspace = V4L2_COLORSPACE_SRGB,
228 .mux = OV5640_FMT_MUX_RGB,
230 /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
231 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
232 .colorspace = V4L2_COLORSPACE_SRGB,
235 .mux = OV5640_FMT_MUX_RGB,
237 /* Raw, BGBG... / GRGR... */
238 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
239 .colorspace = V4L2_COLORSPACE_SRGB,
242 .mux = OV5640_FMT_MUX_RAW_DPC,
244 /* Raw bayer, GBGB... / RGRG... */
245 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
246 .colorspace = V4L2_COLORSPACE_SRGB,
249 .mux = OV5640_FMT_MUX_RAW_DPC,
251 /* Raw bayer, GRGR... / BGBG... */
252 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
253 .colorspace = V4L2_COLORSPACE_SRGB,
256 .mux = OV5640_FMT_MUX_RAW_DPC,
258 /* Raw bayer, RGRG... / GBGB... */
259 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
260 .colorspace = V4L2_COLORSPACE_SRGB,
263 .mux = OV5640_FMT_MUX_RAW_DPC,
268 static const struct ov5640_pixfmt ov5640_csi2_formats[] = {
271 .code = MEDIA_BUS_FMT_JPEG_1X8,
272 .colorspace = V4L2_COLORSPACE_JPEG,
275 .mux = OV5640_FMT_MUX_YUV422,
278 .code = MEDIA_BUS_FMT_UYVY8_1X16,
279 .colorspace = V4L2_COLORSPACE_SRGB,
282 .mux = OV5640_FMT_MUX_YUV422,
285 .code = MEDIA_BUS_FMT_YUYV8_1X16,
286 .colorspace = V4L2_COLORSPACE_SRGB,
289 .mux = OV5640_FMT_MUX_YUV422,
291 /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
292 .code = MEDIA_BUS_FMT_RGB565_1X16,
293 .colorspace = V4L2_COLORSPACE_SRGB,
296 .mux = OV5640_FMT_MUX_RGB,
299 .code = MEDIA_BUS_FMT_BGR888_1X24,
300 .colorspace = V4L2_COLORSPACE_SRGB,
303 .mux = OV5640_FMT_MUX_RGB,
305 /* Raw, BGBG... / GRGR... */
306 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
307 .colorspace = V4L2_COLORSPACE_SRGB,
310 .mux = OV5640_FMT_MUX_RAW_DPC,
312 /* Raw bayer, GBGB... / RGRG... */
313 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
314 .colorspace = V4L2_COLORSPACE_SRGB,
317 .mux = OV5640_FMT_MUX_RAW_DPC,
319 /* Raw bayer, GRGR... / BGBG... */
320 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
321 .colorspace = V4L2_COLORSPACE_SRGB,
324 .mux = OV5640_FMT_MUX_RAW_DPC,
326 /* Raw bayer, RGRG... / GBGB... */
327 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
328 .colorspace = V4L2_COLORSPACE_SRGB,
331 .mux = OV5640_FMT_MUX_RAW_DPC,
337 * FIXME: remove this when a subdev API becomes available
338 * to set the MIPI CSI-2 virtual channel.
340 static unsigned int virtual_channel;
341 module_param(virtual_channel, uint, 0444);
342 MODULE_PARM_DESC(virtual_channel,
343 "MIPI CSI-2 virtual channel (0..3), default 0");
345 static const int ov5640_framerates[] = {
346 [OV5640_15_FPS] = 15,
347 [OV5640_30_FPS] = 30,
348 [OV5640_60_FPS] = 60,
351 /* regulator supplies */
352 static const char * const ov5640_supply_name[] = {
353 "DOVDD", /* Digital I/O (1.8V) supply */
354 "AVDD", /* Analog (2.8V) supply */
355 "DVDD", /* Digital Core (1.5V) supply */
358 #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
361 * Image size under 1280 * 960 are SUBSAMPLING
362 * Image size upper 1280 * 960 are SCALING
364 enum ov5640_downsize_mode {
376 struct ov5640_timings {
377 /* Analog crop rectangle. */
378 struct v4l2_rect analog_crop;
379 /* Visibile crop: from analog crop top-left corner. */
380 struct v4l2_rect crop;
381 /* Total pixels per line: width + fixed hblank. */
383 /* Default vertical blanking: frame height = height + vblank. */
387 struct ov5640_mode_info {
388 enum ov5640_mode_id id;
389 enum ov5640_downsize_mode dn_mode;
390 enum ov5640_pixel_rate_id pixel_rate;
395 struct ov5640_timings dvp_timings;
396 struct ov5640_timings csi2_timings;
398 const struct reg_value *reg_data;
401 /* Used by s_frame_interval only. */
406 struct ov5640_ctrls {
407 struct v4l2_ctrl_handler handler;
408 struct v4l2_ctrl *pixel_rate;
409 struct v4l2_ctrl *link_freq;
410 struct v4l2_ctrl *hblank;
411 struct v4l2_ctrl *vblank;
413 struct v4l2_ctrl *auto_exp;
414 struct v4l2_ctrl *exposure;
417 struct v4l2_ctrl *auto_wb;
418 struct v4l2_ctrl *blue_balance;
419 struct v4l2_ctrl *red_balance;
422 struct v4l2_ctrl *auto_gain;
423 struct v4l2_ctrl *gain;
425 struct v4l2_ctrl *brightness;
426 struct v4l2_ctrl *light_freq;
427 struct v4l2_ctrl *saturation;
428 struct v4l2_ctrl *contrast;
429 struct v4l2_ctrl *hue;
430 struct v4l2_ctrl *test_pattern;
431 struct v4l2_ctrl *hflip;
432 struct v4l2_ctrl *vflip;
436 struct i2c_client *i2c_client;
437 struct v4l2_subdev sd;
438 struct media_pad pad;
439 struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
440 struct clk *xclk; /* system clock to OV5640 */
443 struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
444 struct gpio_desc *reset_gpio;
445 struct gpio_desc *pwdn_gpio;
448 /* lock to protect all members below */
451 struct v4l2_mbus_framefmt fmt;
452 bool pending_fmt_change;
454 const struct ov5640_mode_info *current_mode;
455 const struct ov5640_mode_info *last_mode;
456 enum ov5640_frame_rate current_fr;
457 struct v4l2_fract frame_interval;
458 s64 current_link_freq;
460 struct ov5640_ctrls ctrls;
462 u32 prev_sysclk, prev_hts;
463 u32 ae_low, ae_high, ae_target;
465 bool pending_mode_change;
469 static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
471 return container_of(sd, struct ov5640_dev, sd);
474 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
476 return &container_of(ctrl->handler, struct ov5640_dev,
480 static inline bool ov5640_is_csi2(const struct ov5640_dev *sensor)
482 return sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY;
485 static inline const struct ov5640_pixfmt *
486 ov5640_formats(struct ov5640_dev *sensor)
488 return ov5640_is_csi2(sensor) ? ov5640_csi2_formats
489 : ov5640_dvp_formats;
492 static const struct ov5640_pixfmt *
493 ov5640_code_to_pixfmt(struct ov5640_dev *sensor, u32 code)
495 const struct ov5640_pixfmt *formats = ov5640_formats(sensor);
498 for (i = 0; formats[i].code; ++i) {
499 if (formats[i].code == code)
506 static u32 ov5640_code_to_bpp(struct ov5640_dev *sensor, u32 code)
508 const struct ov5640_pixfmt *format = ov5640_code_to_pixfmt(sensor,
515 * FIXME: all of these register tables are likely filled with
516 * entries that set the register to their power-on default values,
517 * and which are otherwise not touched by this driver. Those entries
518 * should be identified and removed to speed register load time
521 /* YUV422 UYVY VGA@30fps */
523 static const struct v4l2_mbus_framefmt ov5640_default_fmt = {
524 .code = MEDIA_BUS_FMT_UYVY8_2X8,
527 .colorspace = V4L2_COLORSPACE_SRGB,
528 .ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB),
529 .quantization = V4L2_QUANTIZATION_FULL_RANGE,
530 .xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB),
531 .field = V4L2_FIELD_NONE,
534 static const struct reg_value ov5640_init_setting[] = {
535 {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
536 {0x3103, 0x03, 0, 0}, {0x3630, 0x36, 0, 0},
537 {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
538 {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
539 {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
540 {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
541 {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
542 {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
543 {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
544 {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
545 {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
546 {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
547 {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
548 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
549 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
550 {0x3815, 0x31, 0, 0},
551 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
552 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
553 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
554 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
555 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
556 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
557 {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
558 {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
559 {0x501f, 0x00, 0, 0}, {0x4407, 0x04, 0, 0},
560 {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
561 {0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
562 {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
563 {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
564 {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
565 {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
566 {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
567 {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
568 {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
569 {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
570 {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
571 {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
572 {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
573 {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
574 {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
575 {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
576 {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
577 {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
578 {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
579 {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
580 {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
581 {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
582 {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
583 {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
584 {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
585 {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
586 {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
587 {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
588 {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
589 {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
590 {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
591 {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
592 {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
593 {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
594 {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
595 {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
596 {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
597 {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
598 {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
599 {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
600 {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
601 {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
602 {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
603 {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
604 {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
605 {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
606 {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
607 {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
608 {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
609 {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
610 {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
611 {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
614 static const struct reg_value ov5640_setting_low_res[] = {
615 {0x3c07, 0x08, 0, 0},
616 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
617 {0x3814, 0x31, 0, 0},
618 {0x3815, 0x31, 0, 0},
619 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
620 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
621 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
622 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
623 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
624 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
625 {0x4407, 0x04, 0, 0}, {0x5001, 0xa3, 0, 0},
628 static const struct reg_value ov5640_setting_720P_1280_720[] = {
629 {0x3c07, 0x07, 0, 0},
630 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
631 {0x3814, 0x31, 0, 0},
632 {0x3815, 0x31, 0, 0},
633 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
634 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
635 {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
636 {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
637 {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
638 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
639 {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
640 {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
643 static const struct reg_value ov5640_setting_1080P_1920_1080[] = {
644 {0x3c07, 0x08, 0, 0},
645 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
646 {0x3814, 0x11, 0, 0},
647 {0x3815, 0x11, 0, 0},
648 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
649 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
650 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
651 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
652 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
653 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
654 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
655 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
656 {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
657 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
658 {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
659 {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
660 {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
661 {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
662 {0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0},
663 {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
664 {0x4005, 0x1a, 0, 0},
667 static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = {
668 {0x3c07, 0x08, 0, 0},
669 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
670 {0x3814, 0x11, 0, 0},
671 {0x3815, 0x11, 0, 0},
672 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
673 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
674 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
675 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
676 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
677 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
678 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
679 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
682 static const struct ov5640_mode_info ov5640_mode_data[OV5640_NUM_MODES] = {
685 .id = OV5640_MODE_QQVGA_160_120,
686 .dn_mode = SUBSAMPLING,
687 .pixel_rate = OV5640_PIXEL_RATE_48M,
707 /* Feed the full valid pixel array to the ISP. */
709 .left = OV5640_PIXEL_ARRAY_LEFT,
710 .top = OV5640_PIXEL_ARRAY_TOP,
711 .width = OV5640_PIXEL_ARRAY_WIDTH,
712 .height = OV5640_PIXEL_ARRAY_HEIGHT,
714 /* Maintain a minimum processing margin. */
724 .reg_data = ov5640_setting_low_res,
725 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
726 .max_fps = OV5640_30_FPS,
727 .def_fps = OV5640_30_FPS
730 .id = OV5640_MODE_QCIF_176_144,
731 .dn_mode = SUBSAMPLING,
732 .pixel_rate = OV5640_PIXEL_RATE_48M,
752 /* Feed the full valid pixel array to the ISP. */
754 .left = OV5640_PIXEL_ARRAY_LEFT,
755 .top = OV5640_PIXEL_ARRAY_TOP,
756 .width = OV5640_PIXEL_ARRAY_WIDTH,
757 .height = OV5640_PIXEL_ARRAY_HEIGHT,
759 /* Maintain a minimum processing margin. */
769 .reg_data = ov5640_setting_low_res,
770 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
771 .max_fps = OV5640_30_FPS,
772 .def_fps = OV5640_30_FPS
775 .id = OV5640_MODE_QVGA_320_240,
776 .dn_mode = SUBSAMPLING,
779 .pixel_rate = OV5640_PIXEL_RATE_48M,
797 /* Feed the full valid pixel array to the ISP. */
799 .left = OV5640_PIXEL_ARRAY_LEFT,
800 .top = OV5640_PIXEL_ARRAY_TOP,
801 .width = OV5640_PIXEL_ARRAY_WIDTH,
802 .height = OV5640_PIXEL_ARRAY_HEIGHT,
804 /* Maintain a minimum processing margin. */
814 .reg_data = ov5640_setting_low_res,
815 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
816 .max_fps = OV5640_30_FPS,
817 .def_fps = OV5640_30_FPS
820 .id = OV5640_MODE_VGA_640_480,
821 .dn_mode = SUBSAMPLING,
822 .pixel_rate = OV5640_PIXEL_RATE_48M,
842 /* Feed the full valid pixel array to the ISP. */
844 .left = OV5640_PIXEL_ARRAY_LEFT,
845 .top = OV5640_PIXEL_ARRAY_TOP,
846 .width = OV5640_PIXEL_ARRAY_WIDTH,
847 .height = OV5640_PIXEL_ARRAY_HEIGHT,
849 /* Maintain a minimum processing margin. */
859 .reg_data = ov5640_setting_low_res,
860 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
861 .max_fps = OV5640_60_FPS,
862 .def_fps = OV5640_30_FPS
865 .id = OV5640_MODE_NTSC_720_480,
866 .dn_mode = SUBSAMPLING,
869 .pixel_rate = OV5640_PIXEL_RATE_96M,
887 /* Feed the full valid pixel array to the ISP. */
889 .left = OV5640_PIXEL_ARRAY_LEFT,
890 .top = OV5640_PIXEL_ARRAY_TOP,
891 .width = OV5640_PIXEL_ARRAY_WIDTH,
892 .height = OV5640_PIXEL_ARRAY_HEIGHT,
903 .reg_data = ov5640_setting_low_res,
904 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
905 .max_fps = OV5640_30_FPS,
906 .def_fps = OV5640_30_FPS
909 .id = OV5640_MODE_PAL_720_576,
910 .dn_mode = SUBSAMPLING,
913 .pixel_rate = OV5640_PIXEL_RATE_96M,
931 /* Feed the full valid pixel array to the ISP. */
933 .left = OV5640_PIXEL_ARRAY_LEFT,
934 .top = OV5640_PIXEL_ARRAY_TOP,
935 .width = OV5640_PIXEL_ARRAY_WIDTH,
936 .height = OV5640_PIXEL_ARRAY_HEIGHT,
947 .reg_data = ov5640_setting_low_res,
948 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
949 .max_fps = OV5640_30_FPS,
950 .def_fps = OV5640_30_FPS
953 .id = OV5640_MODE_XGA_1024_768,
954 .dn_mode = SUBSAMPLING,
955 .pixel_rate = OV5640_PIXEL_RATE_96M,
978 .width = OV5640_NATIVE_WIDTH,
979 .height = OV5640_PIXEL_ARRAY_HEIGHT,
990 .reg_data = ov5640_setting_low_res,
991 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
992 .max_fps = OV5640_30_FPS,
993 .def_fps = OV5640_30_FPS
996 .id = OV5640_MODE_720P_1280_720,
997 .dn_mode = SUBSAMPLING,
998 .pixel_rate = OV5640_PIXEL_RATE_124M,
1033 .reg_data = ov5640_setting_720P_1280_720,
1034 .reg_data_size = ARRAY_SIZE(ov5640_setting_720P_1280_720),
1035 .max_fps = OV5640_30_FPS,
1036 .def_fps = OV5640_30_FPS
1039 .id = OV5640_MODE_1080P_1920_1080,
1041 .pixel_rate = OV5640_PIXEL_RATE_148M,
1061 /* Crop the full valid pixel array in the center. */
1068 /* Maintain a larger processing margins. */
1078 .reg_data = ov5640_setting_1080P_1920_1080,
1079 .reg_data_size = ARRAY_SIZE(ov5640_setting_1080P_1920_1080),
1080 .max_fps = OV5640_30_FPS,
1081 .def_fps = OV5640_30_FPS
1084 .id = OV5640_MODE_QSXGA_2592_1944,
1086 .pixel_rate = OV5640_PIXEL_RATE_168M,
1087 .width = OV5640_PIXEL_ARRAY_WIDTH,
1088 .height = OV5640_PIXEL_ARRAY_HEIGHT,
1106 /* Give more processing margin to full resolution. */
1110 .width = OV5640_NATIVE_WIDTH,
1122 .reg_data = ov5640_setting_QSXGA_2592_1944,
1123 .reg_data_size = ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944),
1124 .max_fps = OV5640_15_FPS,
1125 .def_fps = OV5640_15_FPS
1129 static const struct ov5640_timings *
1130 ov5640_timings(const struct ov5640_dev *sensor,
1131 const struct ov5640_mode_info *mode)
1133 if (ov5640_is_csi2(sensor))
1134 return &mode->csi2_timings;
1136 return &mode->dvp_timings;
1139 static int ov5640_init_slave_id(struct ov5640_dev *sensor)
1141 struct i2c_client *client = sensor->i2c_client;
1146 if (client->addr == OV5640_DEFAULT_SLAVE_ID)
1149 buf[0] = OV5640_REG_SLAVE_ID >> 8;
1150 buf[1] = OV5640_REG_SLAVE_ID & 0xff;
1151 buf[2] = client->addr << 1;
1153 msg.addr = OV5640_DEFAULT_SLAVE_ID;
1156 msg.len = sizeof(buf);
1158 ret = i2c_transfer(client->adapter, &msg, 1);
1160 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
1167 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
1169 struct i2c_client *client = sensor->i2c_client;
1175 buf[1] = reg & 0xff;
1178 msg.addr = client->addr;
1179 msg.flags = client->flags;
1181 msg.len = sizeof(buf);
1183 ret = i2c_transfer(client->adapter, &msg, 1);
1185 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
1186 __func__, reg, val);
1193 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
1195 struct i2c_client *client = sensor->i2c_client;
1196 struct i2c_msg msg[2];
1201 buf[1] = reg & 0xff;
1203 msg[0].addr = client->addr;
1204 msg[0].flags = client->flags;
1206 msg[0].len = sizeof(buf);
1208 msg[1].addr = client->addr;
1209 msg[1].flags = client->flags | I2C_M_RD;
1213 ret = i2c_transfer(client->adapter, msg, 2);
1215 dev_err(&client->dev, "%s: error: reg=%x\n",
1224 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
1229 ret = ov5640_read_reg(sensor, reg, &hi);
1232 ret = ov5640_read_reg(sensor, reg + 1, &lo);
1236 *val = ((u16)hi << 8) | (u16)lo;
1240 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
1244 ret = ov5640_write_reg(sensor, reg, val >> 8);
1248 return ov5640_write_reg(sensor, reg + 1, val & 0xff);
1251 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
1257 ret = ov5640_read_reg(sensor, reg, &readval);
1265 return ov5640_write_reg(sensor, reg, val);
1269 * After trying the various combinations, reading various
1270 * documentations spread around the net, and from the various
1271 * feedback, the clock tree is probably as follows:
1277 * +->| PLL1 | - reg 0x3036, for the multiplier
1278 * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
1279 * | +--------------+
1280 * +->| System Clock | - reg 0x3035, bits 4-7
1282 * | +--------------+
1283 * +->| MIPI Divider | - reg 0x3035, bits 0-3
1284 * | +-+------------+
1285 * | +----------------> MIPI SCLK
1287 * | +->| / 2 |-------> MIPI BIT CLK
1289 * | +--------------+
1290 * +->| PLL Root Div | - reg 0x3037, bit 4
1293 * +->| Bit Div | - reg 0x3034, bits 0-3
1296 * +->| SCLK Div | - reg 0x3108, bits 0-1
1298 * | +---------------> SCLK
1300 * +->| SCLK 2X Div | - reg 0x3108, bits 2-3
1302 * | +---------------> SCLK 2X
1304 * +->| PCLK Div | - reg 0x3108, bits 4-5
1307 * +->| P_DIV | - reg 0x3035, bits 0-3
1309 * +------------> PCLK
1311 * There seems to be also constraints:
1312 * - the PLL pre-divider output rate should be in the 4-27MHz range
1313 * - the PLL multiplier output rate should be in the 500-1000MHz range
1314 * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
1318 * This is supposed to be ranging from 1 to 8, but the value is always
1319 * set to 3 in the vendor kernels.
1321 #define OV5640_PLL_PREDIV 3
1323 #define OV5640_PLL_MULT_MIN 4
1324 #define OV5640_PLL_MULT_MAX 252
1327 * This is supposed to be ranging from 1 to 16, but the value is
1328 * always set to either 1 or 2 in the vendor kernels.
1330 #define OV5640_SYSDIV_MIN 1
1331 #define OV5640_SYSDIV_MAX 16
1334 * This is supposed to be ranging from 1 to 2, but the value is always
1335 * set to 2 in the vendor kernels.
1337 #define OV5640_PLL_ROOT_DIV 2
1338 #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4)
1341 * We only supports 8-bit formats at the moment
1343 #define OV5640_BIT_DIV 2
1344 #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08
1347 * This is supposed to be ranging from 1 to 8, but the value is always
1348 * set to 2 in the vendor kernels.
1350 #define OV5640_SCLK_ROOT_DIV 2
1353 * This is hardcoded so that the consistency is maintained between SCLK and
1356 #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
1359 * This is supposed to be ranging from 1 to 8, but the value is always
1360 * set to 1 in the vendor kernels.
1362 #define OV5640_PCLK_ROOT_DIV 1
1363 #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00
1365 static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
1366 u8 pll_prediv, u8 pll_mult,
1369 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
1371 /* PLL1 output cannot exceed 1GHz. */
1372 if (sysclk / 1000000 > 1000)
1375 return sysclk / sysdiv;
1378 static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
1380 u8 *pll_prediv, u8 *pll_mult,
1383 unsigned long best = ~0;
1384 u8 best_sysdiv = 1, best_mult = 1;
1385 u8 _sysdiv, _pll_mult;
1387 for (_sysdiv = OV5640_SYSDIV_MIN;
1388 _sysdiv <= OV5640_SYSDIV_MAX;
1390 for (_pll_mult = OV5640_PLL_MULT_MIN;
1391 _pll_mult <= OV5640_PLL_MULT_MAX;
1393 unsigned long _rate;
1396 * The PLL multiplier cannot be odd if above
1399 if (_pll_mult > 127 && (_pll_mult % 2))
1402 _rate = ov5640_compute_sys_clk(sensor,
1404 _pll_mult, _sysdiv);
1407 * We have reached the maximum allowed PLL1 output,
1414 * Prefer rates above the expected clock rate than
1415 * below, even if that means being less precise.
1420 if (abs(rate - _rate) < abs(rate - best)) {
1422 best_sysdiv = _sysdiv;
1423 best_mult = _pll_mult;
1432 *sysdiv = best_sysdiv;
1433 *pll_prediv = OV5640_PLL_PREDIV;
1434 *pll_mult = best_mult;
1440 * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
1441 * for the MIPI CSI-2 output.
1443 static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor)
1445 u8 bit_div, mipi_div, pclk_div, sclk_div, sclk2x_div, root_div;
1446 u8 prediv, mult, sysdiv;
1447 unsigned long link_freq;
1448 unsigned long sysclk;
1454 /* Use the link freq computed at ov5640_update_pixel_rate() time. */
1455 link_freq = sensor->current_link_freq;
1458 * - mipi_div - Additional divider for the MIPI lane clock.
1460 * Higher link frequencies would make sysclk > 1GHz.
1461 * Keep the sysclk low and do not divide in the MIPI domain.
1463 if (link_freq > OV5640_LINK_RATE_MAX)
1468 sysclk = link_freq * mipi_div;
1469 ov5640_calc_sys_clk(sensor, sysclk, &prediv, &mult, &sysdiv);
1472 * Adjust PLL parameters to maintain the MIPI_SCLK-to-PCLK ratio.
1474 * - root_div = 2 (fixed)
1475 * - bit_div : MIPI 8-bit = 2; MIPI 10-bit = 2.5
1476 * - pclk_div = 1 (fixed)
1477 * - p_div = (2 lanes ? mipi_div : 2 * mipi_div)
1479 * This results in the following MIPI_SCLK depending on the number
1482 * - 2 lanes: MIPI_SCLK = (4 or 5) * PCLK
1483 * - 1 lanes: MIPI_SCLK = (8 or 10) * PCLK
1485 root_div = OV5640_PLL_CTRL3_PLL_ROOT_DIV_2;
1486 bit_div = OV5640_PLL_CTRL0_MIPI_MODE_8BIT;
1487 pclk_div = ilog2(OV5640_PCLK_ROOT_DIV);
1491 * - YUV: PCLK >= 2 * SCLK
1492 * - RAW or JPEG: PCLK >= SCLK
1493 * - sclk2x_div = sclk_div / 2
1495 sclk_div = ilog2(OV5640_SCLK_ROOT_DIV);
1496 sclk2x_div = ilog2(OV5640_SCLK2X_ROOT_DIV);
1499 * Set the pixel clock period expressed in ns with 1-bit decimal
1502 * The register is very briefly documented. In the OV5645 datasheet it
1503 * is described as (2 * pclk period), and from testing it seems the
1504 * actual definition is 2 * 8-bit sample period.
1506 * 2 * sample_period = (mipi_clk * 2 * num_lanes / bpp) * (bpp / 8) / 2
1508 num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
1509 sample_rate = (link_freq * mipi_div * num_lanes * 2) / 16;
1510 pclk_period = 2000000000UL / sample_rate;
1512 /* Program the clock tree registers. */
1513 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 0x0f, bit_div);
1517 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0xff,
1518 (sysdiv << 4) | mipi_div);
1522 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
1526 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 0x1f,
1531 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
1532 (pclk_div << 4) | (sclk2x_div << 2) | sclk_div);
1536 return ov5640_write_reg(sensor, OV5640_REG_PCLK_PERIOD, pclk_period);
1539 static u32 ov5640_calc_pixel_rate(struct ov5640_dev *sensor)
1541 const struct ov5640_mode_info *mode = sensor->current_mode;
1542 const struct ov5640_timings *timings = &mode->dvp_timings;
1545 rate = timings->htot * (timings->crop.height + timings->vblank_def);
1546 rate *= ov5640_framerates[sensor->current_fr];
1551 static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
1553 u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
1554 u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
1556 unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
1557 OV5640_PCLK_ROOT_DIV;
1559 _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
1561 *pll_rdiv = OV5640_PLL_ROOT_DIV;
1562 *bit_div = OV5640_BIT_DIV;
1563 *pclk_div = OV5640_PCLK_ROOT_DIV;
1565 return _rate / *pll_rdiv / *bit_div / *pclk_div;
1568 static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor)
1570 u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1574 rate = ov5640_calc_pixel_rate(sensor);
1575 rate *= ov5640_code_to_bpp(sensor, sensor->fmt.code);
1576 rate /= sensor->ep.bus.parallel.bus_width;
1578 ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1579 &bit_div, &pclk_div);
1584 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1590 * We need to set sysdiv according to the clock, and to clear
1593 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1598 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1603 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1604 0x1f, prediv | ((pll_rdiv - 1) << 4));
1608 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1609 (ilog2(pclk_div) << 4));
1612 /* set JPEG framing sizes */
1613 static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor,
1614 const struct ov5640_mode_info *mode)
1619 * compression mode 3 timing
1621 * Data is transmitted with programmable width (VFIFO_HSIZE).
1622 * No padding done. Last line may have less data. Varying
1623 * number of lines per frame, depending on amount of data.
1625 ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3);
1629 ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->width);
1633 return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->height);
1636 /* download ov5640 settings to sensor through i2c */
1637 static int ov5640_set_timings(struct ov5640_dev *sensor,
1638 const struct ov5640_mode_info *mode)
1640 const struct ov5640_timings *timings;
1641 const struct v4l2_rect *analog_crop;
1642 const struct v4l2_rect *crop;
1645 if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) {
1646 ret = ov5640_set_jpeg_timings(sensor, mode);
1651 timings = ov5640_timings(sensor, mode);
1652 analog_crop = &timings->analog_crop;
1653 crop = &timings->crop;
1655 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HS,
1660 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VS,
1665 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HW,
1666 analog_crop->left + analog_crop->width - 1);
1670 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VH,
1671 analog_crop->top + analog_crop->height - 1);
1675 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HOFFS, crop->left);
1679 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VOFFS, crop->top);
1683 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->width);
1687 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->height);
1691 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, timings->htot);
1695 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
1696 mode->height + timings->vblank_def);
1703 static void ov5640_load_regs(struct ov5640_dev *sensor,
1704 const struct reg_value *regs, unsigned int regnum)
1712 for (i = 0; i < regnum; ++i, ++regs) {
1713 delay_ms = regs->delay_ms;
1714 reg_addr = regs->reg_addr;
1718 /* remain in power down mode for DVP */
1719 if (regs->reg_addr == OV5640_REG_SYS_CTRL0 &&
1720 val == OV5640_REG_SYS_CTRL0_SW_PWUP &&
1721 !ov5640_is_csi2(sensor))
1725 ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
1727 ret = ov5640_write_reg(sensor, reg_addr, val);
1732 usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
1736 static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1738 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1739 BIT(0), on ? 0 : BIT(0));
1742 /* read exposure, in number of line periods */
1743 static int ov5640_get_exposure(struct ov5640_dev *sensor)
1748 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
1751 exp = ((int)temp & 0x0f) << 16;
1752 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
1755 exp |= ((int)temp << 8);
1756 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
1764 /* write exposure, given number of line periods */
1765 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
1771 ret = ov5640_write_reg(sensor,
1772 OV5640_REG_AEC_PK_EXPOSURE_LO,
1776 ret = ov5640_write_reg(sensor,
1777 OV5640_REG_AEC_PK_EXPOSURE_MED,
1778 (exposure >> 8) & 0xff);
1781 return ov5640_write_reg(sensor,
1782 OV5640_REG_AEC_PK_EXPOSURE_HI,
1783 (exposure >> 16) & 0x0f);
1786 static int ov5640_get_gain(struct ov5640_dev *sensor)
1791 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1795 return gain & 0x3ff;
1798 static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
1800 return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1804 static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
1806 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1807 BIT(1), on ? 0 : BIT(1));
1810 static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1812 return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ?
1813 OV5640_REG_SYS_CTRL0_SW_PWUP :
1814 OV5640_REG_SYS_CTRL0_SW_PWDN);
1817 static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
1822 * Enable/disable the MIPI interface
1824 * 0x300e = on ? 0x45 : 0x40
1826 * FIXME: the sensor manual (version 2.03) reports
1827 * [7:5] = 000 : 1 data lane mode
1828 * [7:5] = 001 : 2 data lanes mode
1829 * But this settings do not work, while the following ones
1830 * have been validated for 2 data lanes mode.
1832 * [7:5] = 010 : 2 data lanes mode
1833 * [4] = 0 : Power up MIPI HS Tx
1834 * [3] = 0 : Power up MIPI LS Rx
1835 * [2] = 1/0 : MIPI interface enable/disable
1836 * [1:0] = 01/00: FIXME: 'debug'
1838 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1843 return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1847 static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1849 /* calculate sysclk */
1850 u32 xvclk = sensor->xclk_freq / 10000;
1851 u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1852 u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1853 u32 bit_div2x = 1, sclk_rdiv, sysclk;
1857 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1860 temp2 = temp1 & 0x0f;
1861 if (temp2 == 8 || temp2 == 10)
1862 bit_div2x = temp2 / 2;
1864 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1867 sysdiv = temp1 >> 4;
1871 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1876 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1879 prediv = temp1 & 0x0f;
1880 pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1882 ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1885 temp2 = temp1 & 0x03;
1886 sclk_rdiv = sclk_rdiv_map[temp2];
1888 if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1891 VCO = xvclk * multiplier / prediv;
1893 sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1898 static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1900 /* read HTS from register settings */
1904 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1908 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1911 static int ov5640_get_hts(struct ov5640_dev *sensor)
1913 /* read HTS from register settings */
1917 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1923 static int ov5640_get_vts(struct ov5640_dev *sensor)
1928 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1934 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1936 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1939 static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1941 /* get banding filter value */
1942 int ret, light_freq = 0;
1945 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1951 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1964 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1980 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1982 u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1985 /* read preview PCLK */
1986 ret = ov5640_get_sysclk(sensor);
1991 sensor->prev_sysclk = ret;
1992 /* read preview HTS */
1993 ret = ov5640_get_hts(sensor);
1998 sensor->prev_hts = ret;
2000 /* read preview VTS */
2001 ret = ov5640_get_vts(sensor);
2006 /* calculate banding filter */
2008 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
2009 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
2014 max_band60 = (int)((prev_vts - 4) / band_step60);
2015 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
2020 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
2021 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
2026 max_band50 = (int)((prev_vts - 4) / band_step50);
2027 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
2030 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
2032 /* stable in high */
2033 u32 fast_high, fast_low;
2036 sensor->ae_low = target * 23 / 25; /* 0.92 */
2037 sensor->ae_high = target * 27 / 25; /* 1.08 */
2039 fast_high = sensor->ae_high << 1;
2040 if (fast_high > 255)
2043 fast_low = sensor->ae_low >> 1;
2045 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
2048 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
2051 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
2054 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
2057 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
2060 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
2063 static int ov5640_get_binning(struct ov5640_dev *sensor)
2068 ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
2072 return temp & BIT(0);
2075 static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
2081 * - [0]: Horizontal binning enable
2083 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2084 BIT(0), enable ? BIT(0) : 0);
2089 * - [0]: Undocumented, but hardcoded init sequences
2090 * are always setting REG21/REG20 bit 0 to same value...
2092 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2093 BIT(0), enable ? BIT(0) : 0);
2096 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
2098 struct i2c_client *client = sensor->i2c_client;
2099 u8 temp, channel = virtual_channel;
2103 dev_err(&client->dev,
2104 "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
2109 ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
2113 temp |= (channel << 6);
2114 return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
2117 static const struct ov5640_mode_info *
2118 ov5640_find_mode(struct ov5640_dev *sensor, int width, int height, bool nearest)
2120 const struct ov5640_mode_info *mode;
2122 mode = v4l2_find_nearest_size(ov5640_mode_data,
2123 ARRAY_SIZE(ov5640_mode_data),
2124 width, height, width, height);
2128 (mode->width != width || mode->height != height)))
2135 * sensor changes between scaling and subsampling, go through
2136 * exposure calculation
2138 static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
2139 const struct ov5640_mode_info *mode)
2141 u32 prev_shutter, prev_gain16;
2142 u32 cap_shutter, cap_gain16;
2143 u32 cap_sysclk, cap_hts, cap_vts;
2144 u32 light_freq, cap_bandfilt, cap_maxband;
2145 u32 cap_gain16_shutter;
2149 if (!mode->reg_data)
2152 /* read preview shutter */
2153 ret = ov5640_get_exposure(sensor);
2157 ret = ov5640_get_binning(sensor);
2160 if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
2161 mode->id != OV5640_MODE_1080P_1920_1080)
2164 /* read preview gain */
2165 ret = ov5640_get_gain(sensor);
2171 ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
2175 /* turn off night mode for capture */
2176 ret = ov5640_set_night_mode(sensor);
2180 /* Write capture setting */
2181 ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
2182 ret = ov5640_set_timings(sensor, mode);
2186 /* read capture VTS */
2187 ret = ov5640_get_vts(sensor);
2191 ret = ov5640_get_hts(sensor);
2198 ret = ov5640_get_sysclk(sensor);
2205 /* calculate capture banding filter */
2206 ret = ov5640_get_light_freq(sensor);
2211 if (light_freq == 60) {
2213 cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
2216 cap_bandfilt = cap_sysclk * 100 / cap_hts;
2219 if (!sensor->prev_sysclk) {
2220 ret = ov5640_get_sysclk(sensor);
2225 sensor->prev_sysclk = ret;
2231 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
2233 /* calculate capture shutter/gain16 */
2234 if (average > sensor->ae_low && average < sensor->ae_high) {
2235 /* in stable range */
2236 cap_gain16_shutter =
2237 prev_gain16 * prev_shutter *
2238 cap_sysclk / sensor->prev_sysclk *
2239 sensor->prev_hts / cap_hts *
2240 sensor->ae_target / average;
2242 cap_gain16_shutter =
2243 prev_gain16 * prev_shutter *
2244 cap_sysclk / sensor->prev_sysclk *
2245 sensor->prev_hts / cap_hts;
2248 /* gain to shutter */
2249 if (cap_gain16_shutter < (cap_bandfilt * 16)) {
2250 /* shutter < 1/100 */
2251 cap_shutter = cap_gain16_shutter / 16;
2252 if (cap_shutter < 1)
2255 cap_gain16 = cap_gain16_shutter / cap_shutter;
2256 if (cap_gain16 < 16)
2259 if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
2260 /* exposure reach max */
2261 cap_shutter = cap_bandfilt * cap_maxband;
2265 cap_gain16 = cap_gain16_shutter / cap_shutter;
2267 /* 1/100 < (cap_shutter = n/100) =< max */
2269 ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
2274 cap_gain16 = cap_gain16_shutter / cap_shutter;
2278 /* set capture gain */
2279 ret = ov5640_set_gain(sensor, cap_gain16);
2283 /* write capture shutter */
2284 if (cap_shutter > (cap_vts - 4)) {
2285 cap_vts = cap_shutter + 4;
2286 ret = ov5640_set_vts(sensor, cap_vts);
2292 return ov5640_set_exposure(sensor, cap_shutter);
2296 * if sensor changes inside scaling or subsampling
2297 * change mode directly
2299 static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
2300 const struct ov5640_mode_info *mode)
2302 if (!mode->reg_data)
2305 /* Write capture setting */
2306 ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
2307 return ov5640_set_timings(sensor, mode);
2310 static int ov5640_set_mode(struct ov5640_dev *sensor)
2312 const struct ov5640_mode_info *mode = sensor->current_mode;
2313 const struct ov5640_mode_info *orig_mode = sensor->last_mode;
2314 enum ov5640_downsize_mode dn_mode, orig_dn_mode;
2315 bool auto_gain = sensor->ctrls.auto_gain->val == 1;
2316 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
2319 dn_mode = mode->dn_mode;
2320 orig_dn_mode = orig_mode->dn_mode;
2322 /* auto gain and exposure must be turned off when changing modes */
2324 ret = ov5640_set_autogain(sensor, false);
2330 ret = ov5640_set_autoexposure(sensor, false);
2332 goto restore_auto_gain;
2335 if (ov5640_is_csi2(sensor))
2336 ret = ov5640_set_mipi_pclk(sensor);
2338 ret = ov5640_set_dvp_pclk(sensor);
2342 if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
2343 (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
2345 * change between subsampling and scaling
2346 * go through exposure calculation
2348 ret = ov5640_set_mode_exposure_calc(sensor, mode);
2351 * change inside subsampling or scaling
2352 * download firmware directly
2354 ret = ov5640_set_mode_direct(sensor, mode);
2357 goto restore_auto_exp_gain;
2359 /* restore auto gain and exposure */
2361 ov5640_set_autogain(sensor, true);
2363 ov5640_set_autoexposure(sensor, true);
2365 ret = ov5640_set_binning(sensor, dn_mode != SCALING);
2368 ret = ov5640_set_ae_target(sensor, sensor->ae_target);
2371 ret = ov5640_get_light_freq(sensor);
2374 ret = ov5640_set_bandingfilter(sensor);
2377 ret = ov5640_set_virtual_channel(sensor);
2381 sensor->pending_mode_change = false;
2382 sensor->last_mode = mode;
2386 restore_auto_exp_gain:
2388 ov5640_set_autoexposure(sensor, true);
2391 ov5640_set_autogain(sensor, true);
2396 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2397 struct v4l2_mbus_framefmt *format);
2399 /* restore the last set video mode after chip power-on */
2400 static int ov5640_restore_mode(struct ov5640_dev *sensor)
2404 /* first load the initial register values */
2405 ov5640_load_regs(sensor, ov5640_init_setting,
2406 ARRAY_SIZE(ov5640_init_setting));
2408 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
2409 (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) |
2410 ilog2(OV5640_SCLK_ROOT_DIV));
2414 /* now restore the last capture mode */
2415 ret = ov5640_set_mode(sensor);
2419 return ov5640_set_framefmt(sensor, &sensor->fmt);
2422 static void ov5640_power(struct ov5640_dev *sensor, bool enable)
2424 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
2427 static void ov5640_reset(struct ov5640_dev *sensor)
2429 if (!sensor->reset_gpio)
2432 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2434 /* camera power cycle */
2435 ov5640_power(sensor, false);
2436 usleep_range(5000, 10000);
2437 ov5640_power(sensor, true);
2438 usleep_range(5000, 10000);
2440 gpiod_set_value_cansleep(sensor->reset_gpio, 1);
2441 usleep_range(1000, 2000);
2443 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2444 usleep_range(20000, 25000);
2447 static int ov5640_set_power_on(struct ov5640_dev *sensor)
2449 struct i2c_client *client = sensor->i2c_client;
2452 ret = clk_prepare_enable(sensor->xclk);
2454 dev_err(&client->dev, "%s: failed to enable clock\n",
2459 ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
2462 dev_err(&client->dev, "%s: failed to enable regulators\n",
2467 ov5640_reset(sensor);
2468 ov5640_power(sensor, true);
2470 ret = ov5640_init_slave_id(sensor);
2477 ov5640_power(sensor, false);
2478 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2480 clk_disable_unprepare(sensor->xclk);
2484 static void ov5640_set_power_off(struct ov5640_dev *sensor)
2486 ov5640_power(sensor, false);
2487 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2488 clk_disable_unprepare(sensor->xclk);
2491 static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on)
2496 /* Reset MIPI bus settings to their default values. */
2497 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2498 ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04);
2499 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00);
2504 * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
2507 * [7:5] = 010 : 2 data lanes mode (see FIXME note in
2508 * "ov5640_set_stream_mipi()")
2509 * [4] = 0 : Power up MIPI HS Tx
2510 * [3] = 0 : Power up MIPI LS Rx
2511 * [2] = 0 : MIPI interface disabled
2513 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x40);
2518 * Gate clock and set LP11 in 'no packets mode' (idle)
2521 * [5] = 1 : Gate clock when 'no packets'
2522 * [2] = 1 : MIPI bus in LP11 when 'no packets'
2524 ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24);
2529 * Set data lanes and clock in LP11 when 'sleeping'
2532 * [6] = 1 : MIPI data lane 2 in LP11 when 'sleeping'
2533 * [5] = 1 : MIPI data lane 1 in LP11 when 'sleeping'
2534 * [4] = 1 : MIPI clock lane in LP11 when 'sleeping'
2536 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70);
2540 /* Give lanes some time to coax into LP11 state. */
2541 usleep_range(500, 1000);
2546 static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on)
2548 unsigned int flags = sensor->ep.bus.parallel.flags;
2549 bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656;
2554 /* Reset settings to their default values. */
2555 ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00);
2556 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2557 ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20);
2558 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00);
2559 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00);
2564 * Note about parallel port configuration.
2566 * When configured in parallel mode, the OV5640 will
2567 * output 10 bits data on DVP data lines [9:0].
2568 * If only 8 bits data are wanted, the 8 bits data lines
2569 * of the camera interface must be physically connected
2570 * on the DVP data lines [9:2].
2572 * Control lines polarity can be configured through
2573 * devicetree endpoint control lines properties.
2574 * If no endpoint control lines properties are set,
2575 * polarity will be as below:
2576 * - VSYNC: active high
2577 * - HREF: active low
2578 * - PCLK: active low
2580 * VSYNC & HREF are not configured if BT656 bus mode is selected
2584 * BT656 embedded synchronization configuration
2587 * - [7]: SYNC code selection (0: auto generate sync code,
2588 * 1: sync code from regs 0x4732-0x4735)
2589 * - [6]: f value in CCIR656 SYNC code when fixed f value
2590 * - [5]: Fixed f value
2591 * - [4:3]: Blank toggle data options (00: data=1'h040/1'h200,
2592 * 01: data from regs 0x4736-0x4738, 10: always keep 0)
2593 * - [1]: Clip data disable
2594 * - [0]: CCIR656 mode enable
2596 * Default CCIR656 SAV/EAV mode with default codes
2597 * SAV=0xff000080 & EAV=0xff00009d is enabled here with settings:
2598 * - CCIR656 mode enable
2599 * - auto generation of sync codes
2600 * - blank toggle data 1'h040/1'h200
2601 * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe)
2603 ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00,
2604 bt656 ? 0x01 : 0x00);
2609 * configure parallel port control lines polarity
2612 * - [5]: PCLK polarity (0: active low, 1: active high)
2613 * - [1]: HREF polarity (0: active low, 1: active high)
2614 * - [0]: VSYNC polarity (mismatch here between
2615 * datasheet and hardware, 0 is active high
2616 * and 1 is active low...)
2619 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2620 polarities |= BIT(1);
2621 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
2622 polarities |= BIT(0);
2624 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
2625 polarities |= BIT(5);
2627 ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities);
2632 * powerdown MIPI TX/RX PHY & enable DVP
2635 * [4] = 1 : Power down MIPI HS Tx
2636 * [3] = 1 : Power down MIPI LS Rx
2637 * [2] = 0 : DVP enable (MIPI disable)
2639 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18);
2644 * enable VSYNC/HREF/PCLK DVP control lines
2645 * & D[9:6] DVP data lines
2647 * PAD OUTPUT ENABLE 01
2648 * - 6: VSYNC output enable
2649 * - 5: HREF output enable
2650 * - 4: PCLK output enable
2651 * - [3:0]: D[9:6] output enable
2653 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01,
2654 bt656 ? 0x1f : 0x7f);
2659 * enable D[5:0] DVP data lines
2661 * PAD OUTPUT ENABLE 02
2662 * - [7:2]: D[5:0] output enable
2664 return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc);
2667 static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
2672 ret = ov5640_set_power_on(sensor);
2676 ret = ov5640_restore_mode(sensor);
2681 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2682 ret = ov5640_set_power_mipi(sensor, on);
2684 ret = ov5640_set_power_dvp(sensor, on);
2689 ov5640_set_power_off(sensor);
2694 ov5640_set_power_off(sensor);
2698 static int ov5640_sensor_suspend(struct device *dev)
2700 struct v4l2_subdev *sd = dev_get_drvdata(dev);
2701 struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
2703 return ov5640_set_power(ov5640, false);
2706 static int ov5640_sensor_resume(struct device *dev)
2708 struct v4l2_subdev *sd = dev_get_drvdata(dev);
2709 struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
2711 return ov5640_set_power(ov5640, true);
2714 /* --------------- Subdev Operations --------------- */
2716 static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
2717 struct v4l2_fract *fi,
2718 u32 width, u32 height)
2720 const struct ov5640_mode_info *mode;
2721 enum ov5640_frame_rate rate = OV5640_15_FPS;
2722 int minfps, maxfps, best_fps, fps;
2725 minfps = ov5640_framerates[OV5640_15_FPS];
2726 maxfps = ov5640_framerates[OV5640_60_FPS];
2728 if (fi->numerator == 0) {
2729 fi->denominator = maxfps;
2731 rate = OV5640_60_FPS;
2735 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
2739 for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) {
2740 int curr_fps = ov5640_framerates[i];
2742 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
2743 best_fps = curr_fps;
2749 fi->denominator = best_fps;
2752 mode = ov5640_find_mode(sensor, width, height, false);
2753 return mode ? rate : -EINVAL;
2756 static int ov5640_get_fmt(struct v4l2_subdev *sd,
2757 struct v4l2_subdev_state *sd_state,
2758 struct v4l2_subdev_format *format)
2760 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2761 struct v4l2_mbus_framefmt *fmt;
2763 if (format->pad != 0)
2766 mutex_lock(&sensor->lock);
2768 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2769 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
2774 format->format = *fmt;
2776 mutex_unlock(&sensor->lock);
2781 static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
2782 struct v4l2_mbus_framefmt *fmt,
2783 enum ov5640_frame_rate fr,
2784 const struct ov5640_mode_info **new_mode)
2786 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2787 const struct ov5640_mode_info *mode;
2788 const struct ov5640_pixfmt *pixfmt;
2791 mode = ov5640_find_mode(sensor, fmt->width, fmt->height, true);
2795 pixfmt = ov5640_code_to_pixfmt(sensor, fmt->code);
2799 * Adjust mode according to bpp:
2800 * - 8bpp modes work for resolution >= 1280x720
2801 * - 24bpp modes work resolution < 1280x720
2803 if (bpp == 8 && mode->width < 1280)
2804 mode = &ov5640_mode_data[OV5640_MODE_720P_1280_720];
2805 else if (bpp == 24 && mode->width > 1024)
2806 mode = &ov5640_mode_data[OV5640_MODE_XGA_1024_768];
2808 fmt->width = mode->width;
2809 fmt->height = mode->height;
2814 fmt->code = pixfmt->code;
2815 fmt->colorspace = pixfmt->colorspace;
2816 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2817 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2818 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2823 static int ov5640_update_pixel_rate(struct ov5640_dev *sensor)
2825 const struct ov5640_mode_info *mode = sensor->current_mode;
2826 enum ov5640_pixel_rate_id pixel_rate_id = mode->pixel_rate;
2827 struct v4l2_mbus_framefmt *fmt = &sensor->fmt;
2828 const struct ov5640_timings *timings;
2829 s32 exposure_val, exposure_max;
2830 unsigned int hblank;
2839 * Update the pixel rate control value.
2841 * For DVP mode, maintain the pixel rate calculation using fixed FPS.
2843 if (!ov5640_is_csi2(sensor)) {
2844 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2845 ov5640_calc_pixel_rate(sensor));
2851 * The MIPI CSI-2 link frequency should comply with the CSI-2
2852 * specification and be lower than 1GHz.
2854 * Start from the suggested pixel_rate for the current mode and
2855 * progressively slow it down if it exceeds 1GHz.
2857 num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
2858 bpp = ov5640_code_to_bpp(sensor, fmt->code);
2860 pixel_rate = ov5640_pixel_rates[pixel_rate_id];
2861 link_freq = pixel_rate * bpp / (2 * num_lanes);
2862 } while (link_freq >= 1000000000U &&
2863 ++pixel_rate_id < OV5640_NUM_PIXEL_RATES);
2865 sensor->current_link_freq = link_freq;
2868 * Higher link rates require the clock tree to be programmed with
2869 * 'mipi_div' = 1; this has the effect of halving the actual output
2870 * pixel rate in the MIPI domain.
2872 * Adjust the pixel rate and link frequency control value to report it
2873 * correctly to userspace.
2875 if (link_freq > OV5640_LINK_RATE_MAX) {
2880 for (i = 0; i < ARRAY_SIZE(ov5640_csi2_link_freqs); ++i) {
2881 if (ov5640_csi2_link_freqs[i] == link_freq)
2884 WARN_ON(i == ARRAY_SIZE(ov5640_csi2_link_freqs));
2886 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, pixel_rate);
2887 __v4l2_ctrl_s_ctrl(sensor->ctrls.link_freq, i);
2889 timings = ov5640_timings(sensor, mode);
2890 hblank = timings->htot - mode->width;
2891 __v4l2_ctrl_modify_range(sensor->ctrls.hblank,
2892 hblank, hblank, 1, hblank);
2894 vblank = timings->vblank_def;
2896 if (sensor->current_fr != mode->def_fps) {
2898 * Compute the vertical blanking according to the framerate
2899 * configured with s_frame_interval.
2901 int fie_num = sensor->frame_interval.numerator;
2902 int fie_denom = sensor->frame_interval.denominator;
2904 vblank = ((fie_num * pixel_rate / fie_denom) / timings->htot) -
2908 __v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV5640_MIN_VBLANK,
2909 OV5640_MAX_VTS - mode->height, 1, vblank);
2910 __v4l2_ctrl_s_ctrl(sensor->ctrls.vblank, vblank);
2912 exposure_max = timings->crop.height + vblank - 4;
2913 exposure_val = clamp_t(s32, sensor->ctrls.exposure->val,
2914 sensor->ctrls.exposure->minimum,
2917 __v4l2_ctrl_modify_range(sensor->ctrls.exposure,
2918 sensor->ctrls.exposure->minimum,
2919 exposure_max, 1, exposure_val);
2924 static int ov5640_set_fmt(struct v4l2_subdev *sd,
2925 struct v4l2_subdev_state *sd_state,
2926 struct v4l2_subdev_format *format)
2928 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2929 const struct ov5640_mode_info *new_mode;
2930 struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
2933 if (format->pad != 0)
2936 mutex_lock(&sensor->lock);
2938 if (sensor->streaming) {
2943 ret = ov5640_try_fmt_internal(sd, mbus_fmt,
2944 sensor->current_fr, &new_mode);
2948 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2949 *v4l2_subdev_get_try_format(sd, sd_state, 0) = *mbus_fmt;
2953 if (new_mode != sensor->current_mode) {
2954 sensor->current_fr = new_mode->def_fps;
2955 sensor->current_mode = new_mode;
2956 sensor->pending_mode_change = true;
2958 if (mbus_fmt->code != sensor->fmt.code)
2959 sensor->pending_fmt_change = true;
2961 /* update format even if code is unchanged, resolution might change */
2962 sensor->fmt = *mbus_fmt;
2964 ov5640_update_pixel_rate(sensor);
2967 mutex_unlock(&sensor->lock);
2971 static int ov5640_get_selection(struct v4l2_subdev *sd,
2972 struct v4l2_subdev_state *sd_state,
2973 struct v4l2_subdev_selection *sel)
2975 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2976 const struct ov5640_mode_info *mode = sensor->current_mode;
2977 const struct ov5640_timings *timings;
2979 switch (sel->target) {
2980 case V4L2_SEL_TGT_CROP: {
2981 mutex_lock(&sensor->lock);
2982 timings = ov5640_timings(sensor, mode);
2983 sel->r = timings->analog_crop;
2984 mutex_unlock(&sensor->lock);
2989 case V4L2_SEL_TGT_NATIVE_SIZE:
2990 case V4L2_SEL_TGT_CROP_BOUNDS:
2993 sel->r.width = OV5640_NATIVE_WIDTH;
2994 sel->r.height = OV5640_NATIVE_HEIGHT;
2998 case V4L2_SEL_TGT_CROP_DEFAULT:
2999 sel->r.top = OV5640_PIXEL_ARRAY_TOP;
3000 sel->r.left = OV5640_PIXEL_ARRAY_LEFT;
3001 sel->r.width = OV5640_PIXEL_ARRAY_WIDTH;
3002 sel->r.height = OV5640_PIXEL_ARRAY_HEIGHT;
3010 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
3011 struct v4l2_mbus_framefmt *format)
3013 bool is_jpeg = format->code == MEDIA_BUS_FMT_JPEG_1X8;
3014 const struct ov5640_pixfmt *pixfmt;
3017 pixfmt = ov5640_code_to_pixfmt(sensor, format->code);
3019 /* FORMAT CONTROL00: YUV and RGB formatting */
3020 ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00,
3025 /* FORMAT MUX CONTROL: ISP YUV or RGB */
3026 ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
3033 * - [5]: JPEG enable
3035 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
3036 BIT(5), is_jpeg ? BIT(5) : 0);
3042 * - [4]: Reset JFIFO
3043 * - [3]: Reset SFIFO
3046 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
3047 BIT(4) | BIT(3) | BIT(2),
3048 is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
3054 * - [5]: Enable JPEG 2x clock
3055 * - [3]: Enable JPEG clock
3057 return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
3059 is_jpeg ? (BIT(5) | BIT(3)) : 0);
3066 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
3071 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3075 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
3077 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
3083 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
3088 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3092 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
3095 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
3101 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
3106 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3110 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
3114 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
3117 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
3123 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
3127 ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
3128 BIT(0), awb ? 0 : 1);
3133 u16 red = (u16)sensor->ctrls.red_balance->val;
3134 u16 blue = (u16)sensor->ctrls.blue_balance->val;
3136 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
3139 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
3145 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
3146 enum v4l2_exposure_auto_type auto_exposure)
3148 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3149 bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
3152 if (ctrls->auto_exp->is_new) {
3153 ret = ov5640_set_autoexposure(sensor, auto_exp);
3158 if (!auto_exp && ctrls->exposure->is_new) {
3161 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
3165 ret = ov5640_get_vts(sensor);
3171 if (ctrls->exposure->val < max_exp)
3172 ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
3178 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
3180 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3183 if (ctrls->auto_gain->is_new) {
3184 ret = ov5640_set_autogain(sensor, auto_gain);
3189 if (!auto_gain && ctrls->gain->is_new)
3190 ret = ov5640_set_gain(sensor, ctrls->gain->val);
3195 static const char * const test_pattern_menu[] = {
3198 "Color bars w/ rolling bar",
3200 "Color squares w/ rolling bar",
3203 #define OV5640_TEST_ENABLE BIT(7)
3204 #define OV5640_TEST_ROLLING BIT(6) /* rolling horizontal bar */
3205 #define OV5640_TEST_TRANSPARENT BIT(5)
3206 #define OV5640_TEST_SQUARE_BW BIT(4) /* black & white squares */
3207 #define OV5640_TEST_BAR_STANDARD (0 << 2)
3208 #define OV5640_TEST_BAR_VERT_CHANGE_1 (1 << 2)
3209 #define OV5640_TEST_BAR_HOR_CHANGE (2 << 2)
3210 #define OV5640_TEST_BAR_VERT_CHANGE_2 (3 << 2)
3211 #define OV5640_TEST_BAR (0 << 0)
3212 #define OV5640_TEST_RANDOM (1 << 0)
3213 #define OV5640_TEST_SQUARE (2 << 0)
3214 #define OV5640_TEST_BLACK (3 << 0)
3216 static const u8 test_pattern_val[] = {
3218 OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 |
3220 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING |
3221 OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR,
3222 OV5640_TEST_ENABLE | OV5640_TEST_SQUARE,
3223 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE,
3226 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
3228 return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
3229 test_pattern_val[value]);
3232 static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
3236 ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
3237 (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
3242 return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
3243 (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
3247 static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
3250 * If sensor is mounted upside down, mirror logic is inversed.
3252 * Sensor is a BSI (Back Side Illuminated) one,
3253 * so image captured is physically mirrored.
3254 * This is why mirror logic is inversed in
3255 * order to cancel this mirror effect.
3261 * - [1]: Sensor mirror
3263 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
3265 (!(value ^ sensor->upside_down)) ?
3266 (BIT(2) | BIT(1)) : 0);
3269 static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
3271 /* If sensor is mounted upside down, flip logic is inversed */
3276 * - [1]: Sensor vflip
3278 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
3280 (value ^ sensor->upside_down) ?
3281 (BIT(2) | BIT(1)) : 0);
3284 static int ov5640_set_ctrl_vblank(struct ov5640_dev *sensor, int value)
3286 const struct ov5640_mode_info *mode = sensor->current_mode;
3288 /* Update the VTOT timing register value. */
3289 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
3290 mode->height + value);
3293 static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
3295 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3296 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3299 /* v4l2_ctrl_lock() locks our own mutex */
3301 if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
3305 case V4L2_CID_AUTOGAIN:
3306 val = ov5640_get_gain(sensor);
3309 sensor->ctrls.gain->val = val;
3311 case V4L2_CID_EXPOSURE_AUTO:
3312 val = ov5640_get_exposure(sensor);
3315 sensor->ctrls.exposure->val = val;
3319 pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3324 static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
3326 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3327 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3328 const struct ov5640_mode_info *mode = sensor->current_mode;
3329 const struct ov5640_timings *timings;
3330 unsigned int exp_max;
3333 /* v4l2_ctrl_lock() locks our own mutex */
3336 case V4L2_CID_VBLANK:
3337 /* Update the exposure range to the newly programmed vblank. */
3338 timings = ov5640_timings(sensor, mode);
3339 exp_max = mode->height + ctrl->val - 4;
3340 __v4l2_ctrl_modify_range(sensor->ctrls.exposure,
3341 sensor->ctrls.exposure->minimum,
3342 exp_max, sensor->ctrls.exposure->step,
3343 timings->vblank_def);
3348 * If the device is not powered up by the host driver do
3349 * not apply any controls to H/W at this time. Instead
3350 * the controls will be restored at start streaming time.
3352 if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
3356 case V4L2_CID_AUTOGAIN:
3357 ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
3359 case V4L2_CID_EXPOSURE_AUTO:
3360 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
3362 case V4L2_CID_AUTO_WHITE_BALANCE:
3363 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
3366 ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
3368 case V4L2_CID_CONTRAST:
3369 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
3371 case V4L2_CID_SATURATION:
3372 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
3374 case V4L2_CID_TEST_PATTERN:
3375 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
3377 case V4L2_CID_POWER_LINE_FREQUENCY:
3378 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
3380 case V4L2_CID_HFLIP:
3381 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
3383 case V4L2_CID_VFLIP:
3384 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
3386 case V4L2_CID_VBLANK:
3387 ret = ov5640_set_ctrl_vblank(sensor, ctrl->val);
3394 pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3399 static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
3400 .g_volatile_ctrl = ov5640_g_volatile_ctrl,
3401 .s_ctrl = ov5640_s_ctrl,
3404 static int ov5640_init_controls(struct ov5640_dev *sensor)
3406 const struct ov5640_mode_info *mode = sensor->current_mode;
3407 const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
3408 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3409 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
3410 struct v4l2_fwnode_device_properties props;
3411 const struct ov5640_timings *timings;
3412 unsigned int max_vblank;
3413 unsigned int hblank;
3416 v4l2_ctrl_handler_init(hdl, 32);
3418 /* we can use our own mutex for the ctrl lock */
3419 hdl->lock = &sensor->lock;
3421 /* Clock related controls */
3422 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
3423 ov5640_pixel_rates[OV5640_NUM_PIXEL_RATES - 1],
3424 ov5640_pixel_rates[0], 1,
3425 ov5640_pixel_rates[mode->pixel_rate]);
3427 ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, ops,
3429 ARRAY_SIZE(ov5640_csi2_link_freqs) - 1,
3430 OV5640_DEFAULT_LINK_FREQ,
3431 ov5640_csi2_link_freqs);
3433 timings = ov5640_timings(sensor, mode);
3434 hblank = timings->htot - mode->width;
3435 ctrls->hblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HBLANK, hblank,
3438 max_vblank = OV5640_MAX_VTS - mode->height;
3439 ctrls->vblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VBLANK,
3440 OV5640_MIN_VBLANK, max_vblank,
3441 1, timings->vblank_def);
3443 /* Auto/manual white balance */
3444 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
3445 V4L2_CID_AUTO_WHITE_BALANCE,
3447 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
3449 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
3451 /* Auto/manual exposure */
3452 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
3453 V4L2_CID_EXPOSURE_AUTO,
3454 V4L2_EXPOSURE_MANUAL, 0,
3455 V4L2_EXPOSURE_AUTO);
3456 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
3458 /* Auto/manual gain */
3459 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
3461 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
3464 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
3466 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
3468 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
3470 ctrls->test_pattern =
3471 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
3472 ARRAY_SIZE(test_pattern_menu) - 1,
3473 0, 0, test_pattern_menu);
3474 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
3476 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
3480 v4l2_ctrl_new_std_menu(hdl, ops,
3481 V4L2_CID_POWER_LINE_FREQUENCY,
3482 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
3483 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
3490 ret = v4l2_fwnode_device_parse(&sensor->i2c_client->dev, &props);
3494 if (props.rotation == 180)
3495 sensor->upside_down = true;
3497 ret = v4l2_ctrl_new_fwnode_properties(hdl, ops, &props);
3501 ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3502 ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3503 ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3504 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
3505 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
3507 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
3508 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
3509 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
3511 sensor->sd.ctrl_handler = hdl;
3515 v4l2_ctrl_handler_free(hdl);
3519 static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
3520 struct v4l2_subdev_state *sd_state,
3521 struct v4l2_subdev_frame_size_enum *fse)
3523 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3524 u32 bpp = ov5640_code_to_bpp(sensor, fse->code);
3525 unsigned int index = fse->index;
3532 /* Only low-resolution modes are supported for 24bpp formats. */
3533 if (bpp == 24 && index >= OV5640_MODE_720P_1280_720)
3536 /* FIXME: Low resolution modes don't work in 8bpp formats. */
3538 index += OV5640_MODE_720P_1280_720;
3540 if (index >= OV5640_NUM_MODES)
3543 fse->min_width = ov5640_mode_data[index].width;
3544 fse->max_width = fse->min_width;
3545 fse->min_height = ov5640_mode_data[index].height;
3546 fse->max_height = fse->min_height;
3551 static int ov5640_enum_frame_interval(
3552 struct v4l2_subdev *sd,
3553 struct v4l2_subdev_state *sd_state,
3554 struct v4l2_subdev_frame_interval_enum *fie)
3556 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3557 struct v4l2_fract tpf;
3562 if (fie->index >= OV5640_NUM_FRAMERATES)
3566 tpf.denominator = ov5640_framerates[fie->index];
3568 ret = ov5640_try_frame_interval(sensor, &tpf,
3569 fie->width, fie->height);
3573 fie->interval = tpf;
3577 static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
3578 struct v4l2_subdev_frame_interval *fi)
3580 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3582 mutex_lock(&sensor->lock);
3583 fi->interval = sensor->frame_interval;
3584 mutex_unlock(&sensor->lock);
3589 static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
3590 struct v4l2_subdev_frame_interval *fi)
3592 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3593 const struct ov5640_mode_info *mode;
3594 int frame_rate, ret = 0;
3599 mutex_lock(&sensor->lock);
3601 if (sensor->streaming) {
3606 mode = sensor->current_mode;
3608 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
3611 if (frame_rate < 0) {
3612 /* Always return a valid frame interval value */
3613 fi->interval = sensor->frame_interval;
3617 mode = ov5640_find_mode(sensor, mode->width, mode->height, true);
3623 if (ov5640_framerates[frame_rate] > ov5640_framerates[mode->max_fps]) {
3628 if (mode != sensor->current_mode ||
3629 frame_rate != sensor->current_fr) {
3630 sensor->current_fr = frame_rate;
3631 sensor->frame_interval = fi->interval;
3632 sensor->current_mode = mode;
3633 sensor->pending_mode_change = true;
3635 ov5640_update_pixel_rate(sensor);
3638 mutex_unlock(&sensor->lock);
3642 static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
3643 struct v4l2_subdev_state *sd_state,
3644 struct v4l2_subdev_mbus_code_enum *code)
3646 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3647 const struct ov5640_pixfmt *formats;
3648 unsigned int num_formats;
3650 if (ov5640_is_csi2(sensor)) {
3651 formats = ov5640_csi2_formats;
3652 num_formats = ARRAY_SIZE(ov5640_csi2_formats) - 1;
3654 formats = ov5640_dvp_formats;
3655 num_formats = ARRAY_SIZE(ov5640_dvp_formats) - 1;
3658 if (code->index >= num_formats)
3661 code->code = formats[code->index].code;
3666 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
3668 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3672 ret = pm_runtime_resume_and_get(&sensor->i2c_client->dev);
3676 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
3678 pm_runtime_put(&sensor->i2c_client->dev);
3683 mutex_lock(&sensor->lock);
3685 if (sensor->streaming == !enable) {
3686 if (enable && sensor->pending_mode_change) {
3687 ret = ov5640_set_mode(sensor);
3692 if (enable && sensor->pending_fmt_change) {
3693 ret = ov5640_set_framefmt(sensor, &sensor->fmt);
3696 sensor->pending_fmt_change = false;
3699 if (ov5640_is_csi2(sensor))
3700 ret = ov5640_set_stream_mipi(sensor, enable);
3702 ret = ov5640_set_stream_dvp(sensor, enable);
3705 sensor->streaming = enable;
3709 mutex_unlock(&sensor->lock);
3712 pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3717 static int ov5640_init_cfg(struct v4l2_subdev *sd,
3718 struct v4l2_subdev_state *state)
3720 struct v4l2_mbus_framefmt *fmt =
3721 v4l2_subdev_get_try_format(sd, state, 0);
3722 struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, state, 0);
3724 *fmt = ov5640_default_fmt;
3726 crop->left = OV5640_PIXEL_ARRAY_LEFT;
3727 crop->top = OV5640_PIXEL_ARRAY_TOP;
3728 crop->width = OV5640_PIXEL_ARRAY_WIDTH;
3729 crop->height = OV5640_PIXEL_ARRAY_HEIGHT;
3734 static const struct v4l2_subdev_core_ops ov5640_core_ops = {
3735 .log_status = v4l2_ctrl_subdev_log_status,
3736 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
3737 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
3740 static const struct v4l2_subdev_video_ops ov5640_video_ops = {
3741 .g_frame_interval = ov5640_g_frame_interval,
3742 .s_frame_interval = ov5640_s_frame_interval,
3743 .s_stream = ov5640_s_stream,
3746 static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
3747 .init_cfg = ov5640_init_cfg,
3748 .enum_mbus_code = ov5640_enum_mbus_code,
3749 .get_fmt = ov5640_get_fmt,
3750 .set_fmt = ov5640_set_fmt,
3751 .get_selection = ov5640_get_selection,
3752 .enum_frame_size = ov5640_enum_frame_size,
3753 .enum_frame_interval = ov5640_enum_frame_interval,
3756 static const struct v4l2_subdev_ops ov5640_subdev_ops = {
3757 .core = &ov5640_core_ops,
3758 .video = &ov5640_video_ops,
3759 .pad = &ov5640_pad_ops,
3762 static int ov5640_get_regulators(struct ov5640_dev *sensor)
3766 for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
3767 sensor->supplies[i].supply = ov5640_supply_name[i];
3769 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
3770 OV5640_NUM_SUPPLIES,
3774 static int ov5640_check_chip_id(struct ov5640_dev *sensor)
3776 struct i2c_client *client = sensor->i2c_client;
3780 ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
3782 dev_err(&client->dev, "%s: failed to read chip identifier\n",
3787 if (chip_id != 0x5640) {
3788 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
3796 static int ov5640_probe(struct i2c_client *client)
3798 struct device *dev = &client->dev;
3799 struct fwnode_handle *endpoint;
3800 struct ov5640_dev *sensor;
3803 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
3807 sensor->i2c_client = client;
3810 * default init sequence initialize sensor to
3811 * YUV422 UYVY VGA@30fps
3813 sensor->fmt = ov5640_default_fmt;
3814 sensor->frame_interval.numerator = 1;
3815 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
3816 sensor->current_fr = OV5640_30_FPS;
3817 sensor->current_mode =
3818 &ov5640_mode_data[OV5640_MODE_VGA_640_480];
3819 sensor->last_mode = sensor->current_mode;
3820 sensor->current_link_freq = OV5640_DEFAULT_LINK_FREQ;
3822 sensor->ae_target = 52;
3824 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
3827 dev_err(dev, "endpoint node not found\n");
3831 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
3832 fwnode_handle_put(endpoint);
3834 dev_err(dev, "Could not parse endpoint\n");
3838 if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
3839 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
3840 sensor->ep.bus_type != V4L2_MBUS_BT656) {
3841 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
3845 /* get system clock (xclk) */
3846 sensor->xclk = devm_clk_get(dev, "xclk");
3847 if (IS_ERR(sensor->xclk)) {
3848 dev_err(dev, "failed to get xclk\n");
3849 return PTR_ERR(sensor->xclk);
3852 sensor->xclk_freq = clk_get_rate(sensor->xclk);
3853 if (sensor->xclk_freq < OV5640_XCLK_MIN ||
3854 sensor->xclk_freq > OV5640_XCLK_MAX) {
3855 dev_err(dev, "xclk frequency out of range: %d Hz\n",
3860 /* request optional power down pin */
3861 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
3863 if (IS_ERR(sensor->pwdn_gpio))
3864 return PTR_ERR(sensor->pwdn_gpio);
3866 /* request optional reset pin */
3867 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
3869 if (IS_ERR(sensor->reset_gpio))
3870 return PTR_ERR(sensor->reset_gpio);
3872 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
3874 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
3875 V4L2_SUBDEV_FL_HAS_EVENTS;
3876 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
3877 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3878 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
3882 ret = ov5640_get_regulators(sensor);
3884 goto entity_cleanup;
3886 mutex_init(&sensor->lock);
3888 ret = ov5640_init_controls(sensor);
3890 goto entity_cleanup;
3892 ret = ov5640_sensor_resume(dev);
3894 dev_err(dev, "failed to power on\n");
3895 goto entity_cleanup;
3898 pm_runtime_set_active(dev);
3899 pm_runtime_get_noresume(dev);
3900 pm_runtime_enable(dev);
3902 ret = ov5640_check_chip_id(sensor);
3904 goto err_pm_runtime;
3906 ret = v4l2_async_register_subdev_sensor(&sensor->sd);
3908 goto err_pm_runtime;
3910 pm_runtime_set_autosuspend_delay(dev, 1000);
3911 pm_runtime_use_autosuspend(dev);
3912 pm_runtime_put_autosuspend(dev);
3917 pm_runtime_put_noidle(dev);
3918 pm_runtime_disable(dev);
3919 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3920 ov5640_sensor_suspend(dev);
3922 media_entity_cleanup(&sensor->sd.entity);
3923 mutex_destroy(&sensor->lock);
3927 static int ov5640_remove(struct i2c_client *client)
3929 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3930 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3931 struct device *dev = &client->dev;
3933 pm_runtime_disable(dev);
3934 if (!pm_runtime_status_suspended(dev))
3935 ov5640_sensor_suspend(dev);
3936 pm_runtime_set_suspended(dev);
3938 v4l2_async_unregister_subdev(&sensor->sd);
3939 media_entity_cleanup(&sensor->sd.entity);
3940 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3941 mutex_destroy(&sensor->lock);
3946 static const struct dev_pm_ops ov5640_pm_ops = {
3947 SET_RUNTIME_PM_OPS(ov5640_sensor_suspend, ov5640_sensor_resume, NULL)
3950 static const struct i2c_device_id ov5640_id[] = {
3954 MODULE_DEVICE_TABLE(i2c, ov5640_id);
3956 static const struct of_device_id ov5640_dt_ids[] = {
3957 { .compatible = "ovti,ov5640" },
3960 MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
3962 static struct i2c_driver ov5640_i2c_driver = {
3965 .of_match_table = ov5640_dt_ids,
3966 .pm = &ov5640_pm_ops,
3968 .id_table = ov5640_id,
3969 .probe_new = ov5640_probe,
3970 .remove = ov5640_remove,
3973 module_i2c_driver(ov5640_i2c_driver);
3975 MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
3976 MODULE_LICENSE("GPL");