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/mod_devicetable.h>
17 #include <linux/module.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_SYS_CTRL0_SW_RST 0x82
54 #define OV5640_REG_CHIP_ID 0x300a
55 #define OV5640_REG_IO_MIPI_CTRL00 0x300e
56 #define OV5640_REG_PAD_OUTPUT_ENABLE01 0x3017
57 #define OV5640_REG_PAD_OUTPUT_ENABLE02 0x3018
58 #define OV5640_REG_PAD_OUTPUT00 0x3019
59 #define OV5640_REG_SYSTEM_CONTROL1 0x302e
60 #define OV5640_REG_SC_PLL_CTRL0 0x3034
61 #define OV5640_REG_SC_PLL_CTRL1 0x3035
62 #define OV5640_REG_SC_PLL_CTRL2 0x3036
63 #define OV5640_REG_SC_PLL_CTRL3 0x3037
64 #define OV5640_REG_SLAVE_ID 0x3100
65 #define OV5640_REG_SCCB_SYS_CTRL1 0x3103
66 #define OV5640_REG_SYS_ROOT_DIVIDER 0x3108
67 #define OV5640_REG_AWB_R_GAIN 0x3400
68 #define OV5640_REG_AWB_G_GAIN 0x3402
69 #define OV5640_REG_AWB_B_GAIN 0x3404
70 #define OV5640_REG_AWB_MANUAL_CTRL 0x3406
71 #define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500
72 #define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501
73 #define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502
74 #define OV5640_REG_AEC_PK_MANUAL 0x3503
75 #define OV5640_REG_AEC_PK_REAL_GAIN 0x350a
76 #define OV5640_REG_AEC_PK_VTS 0x350c
77 #define OV5640_REG_TIMING_HS 0x3800
78 #define OV5640_REG_TIMING_VS 0x3802
79 #define OV5640_REG_TIMING_HW 0x3804
80 #define OV5640_REG_TIMING_VH 0x3806
81 #define OV5640_REG_TIMING_DVPHO 0x3808
82 #define OV5640_REG_TIMING_DVPVO 0x380a
83 #define OV5640_REG_TIMING_HTS 0x380c
84 #define OV5640_REG_TIMING_VTS 0x380e
85 #define OV5640_REG_TIMING_HOFFS 0x3810
86 #define OV5640_REG_TIMING_VOFFS 0x3812
87 #define OV5640_REG_TIMING_TC_REG20 0x3820
88 #define OV5640_REG_TIMING_TC_REG21 0x3821
89 #define OV5640_REG_AEC_CTRL00 0x3a00
90 #define OV5640_REG_AEC_B50_STEP 0x3a08
91 #define OV5640_REG_AEC_B60_STEP 0x3a0a
92 #define OV5640_REG_AEC_CTRL0D 0x3a0d
93 #define OV5640_REG_AEC_CTRL0E 0x3a0e
94 #define OV5640_REG_AEC_CTRL0F 0x3a0f
95 #define OV5640_REG_AEC_CTRL10 0x3a10
96 #define OV5640_REG_AEC_CTRL11 0x3a11
97 #define OV5640_REG_AEC_CTRL1B 0x3a1b
98 #define OV5640_REG_AEC_CTRL1E 0x3a1e
99 #define OV5640_REG_AEC_CTRL1F 0x3a1f
100 #define OV5640_REG_HZ5060_CTRL00 0x3c00
101 #define OV5640_REG_HZ5060_CTRL01 0x3c01
102 #define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c
103 #define OV5640_REG_FRAME_CTRL01 0x4202
104 #define OV5640_REG_FORMAT_CONTROL00 0x4300
105 #define OV5640_REG_VFIFO_HSIZE 0x4602
106 #define OV5640_REG_VFIFO_VSIZE 0x4604
107 #define OV5640_REG_JPG_MODE_SELECT 0x4713
108 #define OV5640_REG_CCIR656_CTRL00 0x4730
109 #define OV5640_REG_POLARITY_CTRL00 0x4740
110 #define OV5640_REG_MIPI_CTRL00 0x4800
111 #define OV5640_REG_DEBUG_MODE 0x4814
112 #define OV5640_REG_PCLK_PERIOD 0x4837
113 #define OV5640_REG_ISP_FORMAT_MUX_CTRL 0x501f
114 #define OV5640_REG_PRE_ISP_TEST_SET1 0x503d
115 #define OV5640_REG_SDE_CTRL0 0x5580
116 #define OV5640_REG_SDE_CTRL1 0x5581
117 #define OV5640_REG_SDE_CTRL3 0x5583
118 #define OV5640_REG_SDE_CTRL4 0x5584
119 #define OV5640_REG_SDE_CTRL5 0x5585
120 #define OV5640_REG_AVG_READOUT 0x56a1
122 enum ov5640_mode_id {
123 OV5640_MODE_QQVGA_160_120 = 0,
124 OV5640_MODE_QCIF_176_144,
125 OV5640_MODE_QVGA_320_240,
126 OV5640_MODE_VGA_640_480,
127 OV5640_MODE_NTSC_720_480,
128 OV5640_MODE_PAL_720_576,
129 OV5640_MODE_XGA_1024_768,
130 OV5640_MODE_720P_1280_720,
131 OV5640_MODE_1080P_1920_1080,
132 OV5640_MODE_QSXGA_2592_1944,
136 enum ov5640_frame_rate {
140 OV5640_NUM_FRAMERATES,
143 enum ov5640_pixel_rate_id {
144 OV5640_PIXEL_RATE_168M,
145 OV5640_PIXEL_RATE_148M,
146 OV5640_PIXEL_RATE_124M,
147 OV5640_PIXEL_RATE_96M,
148 OV5640_PIXEL_RATE_48M,
149 OV5640_NUM_PIXEL_RATES,
153 * The chip manual suggests 24/48/96/192 MHz pixel clocks.
155 * 192MHz exceeds the sysclk limits; use 168MHz as maximum pixel rate for
156 * full resolution mode @15 FPS.
158 static const u32 ov5640_pixel_rates[] = {
159 [OV5640_PIXEL_RATE_168M] = 168000000,
160 [OV5640_PIXEL_RATE_148M] = 148000000,
161 [OV5640_PIXEL_RATE_124M] = 124000000,
162 [OV5640_PIXEL_RATE_96M] = 96000000,
163 [OV5640_PIXEL_RATE_48M] = 48000000,
167 * MIPI CSI-2 link frequencies.
169 * Derived from the above defined pixel rate for bpp = (8, 16, 24) and
170 * data_lanes = (1, 2)
172 * link_freq = (pixel_rate * bpp) / (2 * data_lanes)
174 static const s64 ov5640_csi2_link_freqs[] = {
175 992000000, 888000000, 768000000, 744000000, 672000000, 672000000,
176 592000000, 592000000, 576000000, 576000000, 496000000, 496000000,
177 384000000, 384000000, 384000000, 336000000, 296000000, 288000000,
178 248000000, 192000000, 192000000, 192000000, 96000000,
181 /* Link freq for default mode: UYVY 16 bpp, 2 data lanes. */
182 #define OV5640_DEFAULT_LINK_FREQ 13
184 enum ov5640_format_mux {
185 OV5640_FMT_MUX_YUV422 = 0,
187 OV5640_FMT_MUX_DITHER,
188 OV5640_FMT_MUX_RAW_DPC,
189 OV5640_FMT_MUX_SNR_RAW,
190 OV5640_FMT_MUX_RAW_CIP,
193 struct ov5640_pixfmt {
198 enum ov5640_format_mux mux;
201 static const struct ov5640_pixfmt ov5640_dvp_formats[] = {
204 .code = MEDIA_BUS_FMT_JPEG_1X8,
205 .colorspace = V4L2_COLORSPACE_JPEG,
208 .mux = OV5640_FMT_MUX_YUV422,
211 .code = MEDIA_BUS_FMT_UYVY8_2X8,
212 .colorspace = V4L2_COLORSPACE_SRGB,
215 .mux = OV5640_FMT_MUX_YUV422,
218 .code = MEDIA_BUS_FMT_YUYV8_2X8,
219 .colorspace = V4L2_COLORSPACE_SRGB,
222 .mux = OV5640_FMT_MUX_YUV422,
224 /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
225 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
226 .colorspace = V4L2_COLORSPACE_SRGB,
229 .mux = OV5640_FMT_MUX_RGB,
231 /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
232 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
233 .colorspace = V4L2_COLORSPACE_SRGB,
236 .mux = OV5640_FMT_MUX_RGB,
238 /* Raw, BGBG... / GRGR... */
239 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
240 .colorspace = V4L2_COLORSPACE_SRGB,
243 .mux = OV5640_FMT_MUX_RAW_DPC,
245 /* Raw bayer, GBGB... / RGRG... */
246 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
247 .colorspace = V4L2_COLORSPACE_SRGB,
250 .mux = OV5640_FMT_MUX_RAW_DPC,
252 /* Raw bayer, GRGR... / BGBG... */
253 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
254 .colorspace = V4L2_COLORSPACE_SRGB,
257 .mux = OV5640_FMT_MUX_RAW_DPC,
259 /* Raw bayer, RGRG... / GBGB... */
260 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
261 .colorspace = V4L2_COLORSPACE_SRGB,
264 .mux = OV5640_FMT_MUX_RAW_DPC,
269 static const struct ov5640_pixfmt ov5640_csi2_formats[] = {
272 .code = MEDIA_BUS_FMT_JPEG_1X8,
273 .colorspace = V4L2_COLORSPACE_JPEG,
276 .mux = OV5640_FMT_MUX_YUV422,
279 .code = MEDIA_BUS_FMT_UYVY8_1X16,
280 .colorspace = V4L2_COLORSPACE_SRGB,
283 .mux = OV5640_FMT_MUX_YUV422,
286 .code = MEDIA_BUS_FMT_YUYV8_1X16,
287 .colorspace = V4L2_COLORSPACE_SRGB,
290 .mux = OV5640_FMT_MUX_YUV422,
292 /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
293 .code = MEDIA_BUS_FMT_RGB565_1X16,
294 .colorspace = V4L2_COLORSPACE_SRGB,
297 .mux = OV5640_FMT_MUX_RGB,
300 .code = MEDIA_BUS_FMT_BGR888_1X24,
301 .colorspace = V4L2_COLORSPACE_SRGB,
304 .mux = OV5640_FMT_MUX_RGB,
306 /* Raw, BGBG... / GRGR... */
307 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
308 .colorspace = V4L2_COLORSPACE_SRGB,
311 .mux = OV5640_FMT_MUX_RAW_DPC,
313 /* Raw bayer, GBGB... / RGRG... */
314 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
315 .colorspace = V4L2_COLORSPACE_SRGB,
318 .mux = OV5640_FMT_MUX_RAW_DPC,
320 /* Raw bayer, GRGR... / BGBG... */
321 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
322 .colorspace = V4L2_COLORSPACE_SRGB,
325 .mux = OV5640_FMT_MUX_RAW_DPC,
327 /* Raw bayer, RGRG... / GBGB... */
328 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
329 .colorspace = V4L2_COLORSPACE_SRGB,
332 .mux = OV5640_FMT_MUX_RAW_DPC,
338 * FIXME: remove this when a subdev API becomes available
339 * to set the MIPI CSI-2 virtual channel.
341 static unsigned int virtual_channel;
342 module_param(virtual_channel, uint, 0444);
343 MODULE_PARM_DESC(virtual_channel,
344 "MIPI CSI-2 virtual channel (0..3), default 0");
346 static const int ov5640_framerates[] = {
347 [OV5640_15_FPS] = 15,
348 [OV5640_30_FPS] = 30,
349 [OV5640_60_FPS] = 60,
352 /* regulator supplies */
353 static const char * const ov5640_supply_name[] = {
354 "DOVDD", /* Digital I/O (1.8V) supply */
355 "AVDD", /* Analog (2.8V) supply */
356 "DVDD", /* Digital Core (1.5V) supply */
359 #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
362 * Image size under 1280 * 960 are SUBSAMPLING
363 * Image size upper 1280 * 960 are SCALING
365 enum ov5640_downsize_mode {
377 struct ov5640_timings {
378 /* Analog crop rectangle. */
379 struct v4l2_rect analog_crop;
380 /* Visibile crop: from analog crop top-left corner. */
381 struct v4l2_rect crop;
382 /* Total pixels per line: width + fixed hblank. */
384 /* Default vertical blanking: frame height = height + vblank. */
388 struct ov5640_mode_info {
389 enum ov5640_mode_id id;
390 enum ov5640_downsize_mode dn_mode;
391 enum ov5640_pixel_rate_id pixel_rate;
396 struct ov5640_timings dvp_timings;
397 struct ov5640_timings csi2_timings;
399 const struct reg_value *reg_data;
402 /* Used by s_frame_interval only. */
407 struct ov5640_ctrls {
408 struct v4l2_ctrl_handler handler;
409 struct v4l2_ctrl *pixel_rate;
410 struct v4l2_ctrl *link_freq;
411 struct v4l2_ctrl *hblank;
412 struct v4l2_ctrl *vblank;
414 struct v4l2_ctrl *auto_exp;
415 struct v4l2_ctrl *exposure;
418 struct v4l2_ctrl *auto_wb;
419 struct v4l2_ctrl *blue_balance;
420 struct v4l2_ctrl *red_balance;
423 struct v4l2_ctrl *auto_gain;
424 struct v4l2_ctrl *gain;
426 struct v4l2_ctrl *brightness;
427 struct v4l2_ctrl *light_freq;
428 struct v4l2_ctrl *saturation;
429 struct v4l2_ctrl *contrast;
430 struct v4l2_ctrl *hue;
431 struct v4l2_ctrl *test_pattern;
432 struct v4l2_ctrl *hflip;
433 struct v4l2_ctrl *vflip;
437 struct i2c_client *i2c_client;
438 struct v4l2_subdev sd;
439 struct media_pad pad;
440 struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
441 struct clk *xclk; /* system clock to OV5640 */
444 struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
445 struct gpio_desc *reset_gpio;
446 struct gpio_desc *pwdn_gpio;
449 /* lock to protect all members below */
452 struct v4l2_mbus_framefmt fmt;
453 bool pending_fmt_change;
455 const struct ov5640_mode_info *current_mode;
456 const struct ov5640_mode_info *last_mode;
457 enum ov5640_frame_rate current_fr;
458 struct v4l2_fract frame_interval;
459 s64 current_link_freq;
461 struct ov5640_ctrls ctrls;
463 u32 prev_sysclk, prev_hts;
464 u32 ae_low, ae_high, ae_target;
466 bool pending_mode_change;
470 static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
472 return container_of(sd, struct ov5640_dev, sd);
475 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
477 return &container_of(ctrl->handler, struct ov5640_dev,
481 static inline bool ov5640_is_csi2(const struct ov5640_dev *sensor)
483 return sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY;
486 static inline const struct ov5640_pixfmt *
487 ov5640_formats(struct ov5640_dev *sensor)
489 return ov5640_is_csi2(sensor) ? ov5640_csi2_formats
490 : ov5640_dvp_formats;
493 static const struct ov5640_pixfmt *
494 ov5640_code_to_pixfmt(struct ov5640_dev *sensor, u32 code)
496 const struct ov5640_pixfmt *formats = ov5640_formats(sensor);
499 for (i = 0; formats[i].code; ++i) {
500 if (formats[i].code == code)
507 static u32 ov5640_code_to_bpp(struct ov5640_dev *sensor, u32 code)
509 const struct ov5640_pixfmt *format = ov5640_code_to_pixfmt(sensor,
516 * FIXME: all of these register tables are likely filled with
517 * entries that set the register to their power-on default values,
518 * and which are otherwise not touched by this driver. Those entries
519 * should be identified and removed to speed register load time
522 /* YUV422 UYVY VGA@30fps */
524 static const struct v4l2_mbus_framefmt ov5640_csi2_default_fmt = {
525 .code = MEDIA_BUS_FMT_UYVY8_1X16,
528 .colorspace = V4L2_COLORSPACE_SRGB,
529 .ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB),
530 .quantization = V4L2_QUANTIZATION_FULL_RANGE,
531 .xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB),
532 .field = V4L2_FIELD_NONE,
535 static const struct v4l2_mbus_framefmt ov5640_dvp_default_fmt = {
536 .code = MEDIA_BUS_FMT_UYVY8_2X8,
539 .colorspace = V4L2_COLORSPACE_SRGB,
540 .ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB),
541 .quantization = V4L2_QUANTIZATION_FULL_RANGE,
542 .xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB),
543 .field = V4L2_FIELD_NONE,
546 static const struct reg_value ov5640_init_setting[] = {
547 {0x3103, 0x11, 0, 0},
548 {0x3103, 0x03, 0, 0}, {0x3630, 0x36, 0, 0},
549 {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
550 {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
551 {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
552 {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
553 {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
554 {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
555 {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
556 {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
557 {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
558 {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
559 {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
560 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
561 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
562 {0x3815, 0x31, 0, 0},
563 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
564 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
565 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
566 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
567 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
568 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
569 {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
570 {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
571 {0x501f, 0x00, 0, 0}, {0x440e, 0x00, 0, 0}, {0x4837, 0x0a, 0, 0},
572 {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
573 {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
574 {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
575 {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
576 {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
577 {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
578 {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
579 {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
580 {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
581 {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
582 {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
583 {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
584 {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
585 {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
586 {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
587 {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
588 {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
589 {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
590 {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
591 {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
592 {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
593 {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
594 {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
595 {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
596 {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
597 {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
598 {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
599 {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
600 {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
601 {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
602 {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
603 {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
604 {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
605 {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
606 {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
607 {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
608 {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
609 {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
610 {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
611 {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
612 {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
613 {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
614 {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
615 {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
616 {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
617 {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
618 {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
619 {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
620 {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
621 {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
624 static const struct reg_value ov5640_setting_low_res[] = {
625 {0x3c07, 0x08, 0, 0},
626 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
627 {0x3814, 0x31, 0, 0},
628 {0x3815, 0x31, 0, 0},
629 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
630 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
631 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
632 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
633 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
634 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
635 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
636 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
639 static const struct reg_value ov5640_setting_720P_1280_720[] = {
640 {0x3c07, 0x07, 0, 0},
641 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
642 {0x3814, 0x31, 0, 0},
643 {0x3815, 0x31, 0, 0},
644 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
645 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
646 {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
647 {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
648 {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
649 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
650 {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
651 {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
654 static const struct reg_value ov5640_setting_1080P_1920_1080[] = {
655 {0x3c07, 0x08, 0, 0},
656 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
657 {0x3814, 0x11, 0, 0},
658 {0x3815, 0x11, 0, 0},
659 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
660 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
661 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
662 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
663 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
664 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
665 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
666 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
667 {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
668 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
669 {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
670 {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
671 {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
672 {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
673 {0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0},
674 {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
675 {0x4005, 0x1a, 0, 0},
678 static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = {
679 {0x3c07, 0x08, 0, 0},
680 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
681 {0x3814, 0x11, 0, 0},
682 {0x3815, 0x11, 0, 0},
683 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
684 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
685 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
686 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
687 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
688 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
689 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
690 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
693 static const struct ov5640_mode_info ov5640_mode_data[OV5640_NUM_MODES] = {
696 .id = OV5640_MODE_QQVGA_160_120,
697 .dn_mode = SUBSAMPLING,
698 .pixel_rate = OV5640_PIXEL_RATE_48M,
718 /* Feed the full valid pixel array to the ISP. */
720 .left = OV5640_PIXEL_ARRAY_LEFT,
721 .top = OV5640_PIXEL_ARRAY_TOP,
722 .width = OV5640_PIXEL_ARRAY_WIDTH,
723 .height = OV5640_PIXEL_ARRAY_HEIGHT,
725 /* Maintain a minimum processing margin. */
735 .reg_data = ov5640_setting_low_res,
736 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
737 .max_fps = OV5640_30_FPS,
738 .def_fps = OV5640_30_FPS
741 .id = OV5640_MODE_QCIF_176_144,
742 .dn_mode = SUBSAMPLING,
743 .pixel_rate = OV5640_PIXEL_RATE_48M,
763 /* Feed the full valid pixel array to the ISP. */
765 .left = OV5640_PIXEL_ARRAY_LEFT,
766 .top = OV5640_PIXEL_ARRAY_TOP,
767 .width = OV5640_PIXEL_ARRAY_WIDTH,
768 .height = OV5640_PIXEL_ARRAY_HEIGHT,
770 /* Maintain a minimum processing margin. */
780 .reg_data = ov5640_setting_low_res,
781 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
782 .max_fps = OV5640_30_FPS,
783 .def_fps = OV5640_30_FPS
786 .id = OV5640_MODE_QVGA_320_240,
787 .dn_mode = SUBSAMPLING,
790 .pixel_rate = OV5640_PIXEL_RATE_48M,
808 /* Feed the full valid pixel array to the ISP. */
810 .left = OV5640_PIXEL_ARRAY_LEFT,
811 .top = OV5640_PIXEL_ARRAY_TOP,
812 .width = OV5640_PIXEL_ARRAY_WIDTH,
813 .height = OV5640_PIXEL_ARRAY_HEIGHT,
815 /* Maintain a minimum processing margin. */
825 .reg_data = ov5640_setting_low_res,
826 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
827 .max_fps = OV5640_30_FPS,
828 .def_fps = OV5640_30_FPS
831 .id = OV5640_MODE_VGA_640_480,
832 .dn_mode = SUBSAMPLING,
833 .pixel_rate = OV5640_PIXEL_RATE_48M,
853 /* Feed the full valid pixel array to the ISP. */
855 .left = OV5640_PIXEL_ARRAY_LEFT,
856 .top = OV5640_PIXEL_ARRAY_TOP,
857 .width = OV5640_PIXEL_ARRAY_WIDTH,
858 .height = OV5640_PIXEL_ARRAY_HEIGHT,
860 /* Maintain a minimum processing margin. */
870 .reg_data = ov5640_setting_low_res,
871 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
872 .max_fps = OV5640_60_FPS,
873 .def_fps = OV5640_30_FPS
876 .id = OV5640_MODE_NTSC_720_480,
877 .dn_mode = SUBSAMPLING,
880 .pixel_rate = OV5640_PIXEL_RATE_96M,
898 /* Feed the full valid pixel array to the ISP. */
900 .left = OV5640_PIXEL_ARRAY_LEFT,
901 .top = OV5640_PIXEL_ARRAY_TOP,
902 .width = OV5640_PIXEL_ARRAY_WIDTH,
903 .height = OV5640_PIXEL_ARRAY_HEIGHT,
914 .reg_data = ov5640_setting_low_res,
915 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
916 .max_fps = OV5640_30_FPS,
917 .def_fps = OV5640_30_FPS
920 .id = OV5640_MODE_PAL_720_576,
921 .dn_mode = SUBSAMPLING,
924 .pixel_rate = OV5640_PIXEL_RATE_96M,
942 /* Feed the full valid pixel array to the ISP. */
944 .left = OV5640_PIXEL_ARRAY_LEFT,
945 .top = OV5640_PIXEL_ARRAY_TOP,
946 .width = OV5640_PIXEL_ARRAY_WIDTH,
947 .height = OV5640_PIXEL_ARRAY_HEIGHT,
958 .reg_data = ov5640_setting_low_res,
959 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
960 .max_fps = OV5640_30_FPS,
961 .def_fps = OV5640_30_FPS
964 .id = OV5640_MODE_XGA_1024_768,
965 .dn_mode = SUBSAMPLING,
966 .pixel_rate = OV5640_PIXEL_RATE_96M,
989 .width = OV5640_NATIVE_WIDTH,
990 .height = OV5640_PIXEL_ARRAY_HEIGHT,
1001 .reg_data = ov5640_setting_low_res,
1002 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
1003 .max_fps = OV5640_30_FPS,
1004 .def_fps = OV5640_30_FPS
1007 .id = OV5640_MODE_720P_1280_720,
1008 .dn_mode = SUBSAMPLING,
1009 .pixel_rate = OV5640_PIXEL_RATE_124M,
1044 .reg_data = ov5640_setting_720P_1280_720,
1045 .reg_data_size = ARRAY_SIZE(ov5640_setting_720P_1280_720),
1046 .max_fps = OV5640_30_FPS,
1047 .def_fps = OV5640_30_FPS
1050 .id = OV5640_MODE_1080P_1920_1080,
1052 .pixel_rate = OV5640_PIXEL_RATE_148M,
1072 /* Crop the full valid pixel array in the center. */
1079 /* Maintain a larger processing margins. */
1089 .reg_data = ov5640_setting_1080P_1920_1080,
1090 .reg_data_size = ARRAY_SIZE(ov5640_setting_1080P_1920_1080),
1091 .max_fps = OV5640_30_FPS,
1092 .def_fps = OV5640_30_FPS
1095 .id = OV5640_MODE_QSXGA_2592_1944,
1097 .pixel_rate = OV5640_PIXEL_RATE_168M,
1098 .width = OV5640_PIXEL_ARRAY_WIDTH,
1099 .height = OV5640_PIXEL_ARRAY_HEIGHT,
1117 /* Give more processing margin to full resolution. */
1121 .width = OV5640_NATIVE_WIDTH,
1133 .reg_data = ov5640_setting_QSXGA_2592_1944,
1134 .reg_data_size = ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944),
1135 .max_fps = OV5640_15_FPS,
1136 .def_fps = OV5640_15_FPS
1140 static const struct ov5640_timings *
1141 ov5640_timings(const struct ov5640_dev *sensor,
1142 const struct ov5640_mode_info *mode)
1144 if (ov5640_is_csi2(sensor))
1145 return &mode->csi2_timings;
1147 return &mode->dvp_timings;
1150 static int ov5640_init_slave_id(struct ov5640_dev *sensor)
1152 struct i2c_client *client = sensor->i2c_client;
1157 if (client->addr == OV5640_DEFAULT_SLAVE_ID)
1160 buf[0] = OV5640_REG_SLAVE_ID >> 8;
1161 buf[1] = OV5640_REG_SLAVE_ID & 0xff;
1162 buf[2] = client->addr << 1;
1164 msg.addr = OV5640_DEFAULT_SLAVE_ID;
1167 msg.len = sizeof(buf);
1169 ret = i2c_transfer(client->adapter, &msg, 1);
1171 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
1178 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
1180 struct i2c_client *client = sensor->i2c_client;
1186 buf[1] = reg & 0xff;
1189 msg.addr = client->addr;
1190 msg.flags = client->flags;
1192 msg.len = sizeof(buf);
1194 ret = i2c_transfer(client->adapter, &msg, 1);
1196 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
1197 __func__, reg, val);
1204 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
1206 struct i2c_client *client = sensor->i2c_client;
1207 struct i2c_msg msg[2];
1212 buf[1] = reg & 0xff;
1214 msg[0].addr = client->addr;
1215 msg[0].flags = client->flags;
1217 msg[0].len = sizeof(buf);
1219 msg[1].addr = client->addr;
1220 msg[1].flags = client->flags | I2C_M_RD;
1224 ret = i2c_transfer(client->adapter, msg, 2);
1226 dev_err(&client->dev, "%s: error: reg=%x\n",
1235 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
1240 ret = ov5640_read_reg(sensor, reg, &hi);
1243 ret = ov5640_read_reg(sensor, reg + 1, &lo);
1247 *val = ((u16)hi << 8) | (u16)lo;
1251 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
1255 ret = ov5640_write_reg(sensor, reg, val >> 8);
1259 return ov5640_write_reg(sensor, reg + 1, val & 0xff);
1262 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
1268 ret = ov5640_read_reg(sensor, reg, &readval);
1276 return ov5640_write_reg(sensor, reg, val);
1280 * After trying the various combinations, reading various
1281 * documentations spread around the net, and from the various
1282 * feedback, the clock tree is probably as follows:
1288 * +->| PLL1 | - reg 0x3036, for the multiplier
1289 * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
1290 * | +--------------+
1291 * +->| System Clock | - reg 0x3035, bits 4-7
1293 * | +--------------+
1294 * +->| MIPI Divider | - reg 0x3035, bits 0-3
1295 * | +-+------------+
1296 * | +----------------> MIPI SCLK
1298 * | +->| / 2 |-------> MIPI BIT CLK
1300 * | +--------------+
1301 * +->| PLL Root Div | - reg 0x3037, bit 4
1304 * +->| Bit Div | - reg 0x3034, bits 0-3
1307 * +->| SCLK Div | - reg 0x3108, bits 0-1
1309 * | +---------------> SCLK
1311 * +->| SCLK 2X Div | - reg 0x3108, bits 2-3
1313 * | +---------------> SCLK 2X
1315 * +->| PCLK Div | - reg 0x3108, bits 4-5
1318 * +->| P_DIV | - reg 0x3035, bits 0-3
1320 * +------------> PCLK
1322 * There seems to be also constraints:
1323 * - the PLL pre-divider output rate should be in the 4-27MHz range
1324 * - the PLL multiplier output rate should be in the 500-1000MHz range
1325 * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
1329 * This is supposed to be ranging from 1 to 8, but the value is always
1330 * set to 3 in the vendor kernels.
1332 #define OV5640_PLL_PREDIV 3
1334 #define OV5640_PLL_MULT_MIN 4
1335 #define OV5640_PLL_MULT_MAX 252
1338 * This is supposed to be ranging from 1 to 16, but the value is
1339 * always set to either 1 or 2 in the vendor kernels.
1341 #define OV5640_SYSDIV_MIN 1
1342 #define OV5640_SYSDIV_MAX 16
1345 * This is supposed to be ranging from 1 to 2, but the value is always
1346 * set to 2 in the vendor kernels.
1348 #define OV5640_PLL_ROOT_DIV 2
1349 #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4)
1352 * We only supports 8-bit formats at the moment
1354 #define OV5640_BIT_DIV 2
1355 #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08
1358 * This is supposed to be ranging from 1 to 8, but the value is always
1359 * set to 2 in the vendor kernels.
1361 #define OV5640_SCLK_ROOT_DIV 2
1364 * This is hardcoded so that the consistency is maintained between SCLK and
1367 #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
1370 * This is supposed to be ranging from 1 to 8, but the value is always
1371 * set to 1 in the vendor kernels.
1373 #define OV5640_PCLK_ROOT_DIV 1
1374 #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00
1376 static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
1377 u8 pll_prediv, u8 pll_mult,
1380 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
1382 /* PLL1 output cannot exceed 1GHz. */
1383 if (sysclk / 1000000 > 1000)
1386 return sysclk / sysdiv;
1389 static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
1391 u8 *pll_prediv, u8 *pll_mult,
1394 unsigned long best = ~0;
1395 u8 best_sysdiv = 1, best_mult = 1;
1396 u8 _sysdiv, _pll_mult;
1398 for (_sysdiv = OV5640_SYSDIV_MIN;
1399 _sysdiv <= OV5640_SYSDIV_MAX;
1401 for (_pll_mult = OV5640_PLL_MULT_MIN;
1402 _pll_mult <= OV5640_PLL_MULT_MAX;
1404 unsigned long _rate;
1407 * The PLL multiplier cannot be odd if above
1410 if (_pll_mult > 127 && (_pll_mult % 2))
1413 _rate = ov5640_compute_sys_clk(sensor,
1415 _pll_mult, _sysdiv);
1418 * We have reached the maximum allowed PLL1 output,
1425 * Prefer rates above the expected clock rate than
1426 * below, even if that means being less precise.
1431 if (abs(rate - _rate) < abs(rate - best)) {
1433 best_sysdiv = _sysdiv;
1434 best_mult = _pll_mult;
1443 *sysdiv = best_sysdiv;
1444 *pll_prediv = OV5640_PLL_PREDIV;
1445 *pll_mult = best_mult;
1451 * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
1452 * for the MIPI CSI-2 output.
1454 static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor)
1456 u8 bit_div, mipi_div, pclk_div, sclk_div, sclk2x_div, root_div;
1457 u8 prediv, mult, sysdiv;
1458 unsigned long link_freq;
1459 unsigned long sysclk;
1465 /* Use the link freq computed at ov5640_update_pixel_rate() time. */
1466 link_freq = sensor->current_link_freq;
1469 * - mipi_div - Additional divider for the MIPI lane clock.
1471 * Higher link frequencies would make sysclk > 1GHz.
1472 * Keep the sysclk low and do not divide in the MIPI domain.
1474 if (link_freq > OV5640_LINK_RATE_MAX)
1479 sysclk = link_freq * mipi_div;
1480 ov5640_calc_sys_clk(sensor, sysclk, &prediv, &mult, &sysdiv);
1483 * Adjust PLL parameters to maintain the MIPI_SCLK-to-PCLK ratio.
1485 * - root_div = 2 (fixed)
1486 * - bit_div : MIPI 8-bit = 2; MIPI 10-bit = 2.5
1487 * - pclk_div = 1 (fixed)
1488 * - p_div = (2 lanes ? mipi_div : 2 * mipi_div)
1490 * This results in the following MIPI_SCLK depending on the number
1493 * - 2 lanes: MIPI_SCLK = (4 or 5) * PCLK
1494 * - 1 lanes: MIPI_SCLK = (8 or 10) * PCLK
1496 root_div = OV5640_PLL_CTRL3_PLL_ROOT_DIV_2;
1497 bit_div = OV5640_PLL_CTRL0_MIPI_MODE_8BIT;
1498 pclk_div = ilog2(OV5640_PCLK_ROOT_DIV);
1502 * - YUV: PCLK >= 2 * SCLK
1503 * - RAW or JPEG: PCLK >= SCLK
1504 * - sclk2x_div = sclk_div / 2
1506 sclk_div = ilog2(OV5640_SCLK_ROOT_DIV);
1507 sclk2x_div = ilog2(OV5640_SCLK2X_ROOT_DIV);
1510 * Set the pixel clock period expressed in ns with 1-bit decimal
1513 * The register is very briefly documented. In the OV5645 datasheet it
1514 * is described as (2 * pclk period), and from testing it seems the
1515 * actual definition is 2 * 8-bit sample period.
1517 * 2 * sample_period = (mipi_clk * 2 * num_lanes / bpp) * (bpp / 8) / 2
1519 num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
1520 sample_rate = (link_freq * mipi_div * num_lanes * 2) / 16;
1521 pclk_period = 2000000000UL / sample_rate;
1523 /* Program the clock tree registers. */
1524 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 0x0f, bit_div);
1528 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0xff,
1529 (sysdiv << 4) | mipi_div);
1533 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
1537 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 0x1f,
1542 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
1543 (pclk_div << 4) | (sclk2x_div << 2) | sclk_div);
1547 return ov5640_write_reg(sensor, OV5640_REG_PCLK_PERIOD, pclk_period);
1550 static u32 ov5640_calc_pixel_rate(struct ov5640_dev *sensor)
1552 const struct ov5640_mode_info *mode = sensor->current_mode;
1553 const struct ov5640_timings *timings = &mode->dvp_timings;
1556 rate = timings->htot * (timings->crop.height + timings->vblank_def);
1557 rate *= ov5640_framerates[sensor->current_fr];
1562 static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
1564 u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
1565 u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
1567 unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
1568 OV5640_PCLK_ROOT_DIV;
1570 _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
1572 *pll_rdiv = OV5640_PLL_ROOT_DIV;
1573 *bit_div = OV5640_BIT_DIV;
1574 *pclk_div = OV5640_PCLK_ROOT_DIV;
1576 return _rate / *pll_rdiv / *bit_div / *pclk_div;
1579 static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor)
1581 u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1585 rate = ov5640_calc_pixel_rate(sensor);
1586 rate *= ov5640_code_to_bpp(sensor, sensor->fmt.code);
1587 rate /= sensor->ep.bus.parallel.bus_width;
1589 ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1590 &bit_div, &pclk_div);
1595 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1601 * We need to set sysdiv according to the clock, and to clear
1604 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1609 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1614 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1615 0x1f, prediv | ((pll_rdiv - 1) << 4));
1619 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1620 (ilog2(pclk_div) << 4));
1623 /* set JPEG framing sizes */
1624 static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor,
1625 const struct ov5640_mode_info *mode)
1630 * compression mode 3 timing
1632 * Data is transmitted with programmable width (VFIFO_HSIZE).
1633 * No padding done. Last line may have less data. Varying
1634 * number of lines per frame, depending on amount of data.
1636 ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3);
1640 ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->width);
1644 return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->height);
1647 /* download ov5640 settings to sensor through i2c */
1648 static int ov5640_set_timings(struct ov5640_dev *sensor,
1649 const struct ov5640_mode_info *mode)
1651 const struct ov5640_timings *timings;
1652 const struct v4l2_rect *analog_crop;
1653 const struct v4l2_rect *crop;
1656 if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) {
1657 ret = ov5640_set_jpeg_timings(sensor, mode);
1662 timings = ov5640_timings(sensor, mode);
1663 analog_crop = &timings->analog_crop;
1664 crop = &timings->crop;
1666 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HS,
1671 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VS,
1676 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HW,
1677 analog_crop->left + analog_crop->width - 1);
1681 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VH,
1682 analog_crop->top + analog_crop->height - 1);
1686 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HOFFS, crop->left);
1690 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VOFFS, crop->top);
1694 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->width);
1698 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->height);
1702 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, timings->htot);
1706 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
1707 mode->height + timings->vblank_def);
1714 static void ov5640_load_regs(struct ov5640_dev *sensor,
1715 const struct reg_value *regs, unsigned int regnum)
1723 for (i = 0; i < regnum; ++i, ++regs) {
1724 delay_ms = regs->delay_ms;
1725 reg_addr = regs->reg_addr;
1729 /* remain in power down mode for DVP */
1730 if (regs->reg_addr == OV5640_REG_SYS_CTRL0 &&
1731 val == OV5640_REG_SYS_CTRL0_SW_PWUP &&
1732 !ov5640_is_csi2(sensor))
1736 ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
1738 ret = ov5640_write_reg(sensor, reg_addr, val);
1743 usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
1747 static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1749 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1750 BIT(0), on ? 0 : BIT(0));
1753 /* read exposure, in number of line periods */
1754 static int ov5640_get_exposure(struct ov5640_dev *sensor)
1759 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
1762 exp = ((int)temp & 0x0f) << 16;
1763 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
1766 exp |= ((int)temp << 8);
1767 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
1775 /* write exposure, given number of line periods */
1776 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
1782 ret = ov5640_write_reg(sensor,
1783 OV5640_REG_AEC_PK_EXPOSURE_LO,
1787 ret = ov5640_write_reg(sensor,
1788 OV5640_REG_AEC_PK_EXPOSURE_MED,
1789 (exposure >> 8) & 0xff);
1792 return ov5640_write_reg(sensor,
1793 OV5640_REG_AEC_PK_EXPOSURE_HI,
1794 (exposure >> 16) & 0x0f);
1797 static int ov5640_get_gain(struct ov5640_dev *sensor)
1802 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1806 return gain & 0x3ff;
1809 static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
1811 return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1815 static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
1817 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1818 BIT(1), on ? 0 : BIT(1));
1821 static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1823 return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ?
1824 OV5640_REG_SYS_CTRL0_SW_PWUP :
1825 OV5640_REG_SYS_CTRL0_SW_PWDN);
1828 static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
1833 * Enable/disable the MIPI interface
1835 * 0x300e = on ? 0x45 : 0x40
1837 * FIXME: the sensor manual (version 2.03) reports
1838 * [7:5] = 000 : 1 data lane mode
1839 * [7:5] = 001 : 2 data lanes mode
1840 * But this settings do not work, while the following ones
1841 * have been validated for 2 data lanes mode.
1843 * [7:5] = 010 : 2 data lanes mode
1844 * [4] = 0 : Power up MIPI HS Tx
1845 * [3] = 0 : Power up MIPI LS Rx
1846 * [2] = 1/0 : MIPI interface enable/disable
1847 * [1:0] = 01/00: FIXME: 'debug'
1849 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1854 return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1858 static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1860 /* calculate sysclk */
1861 u32 xvclk = sensor->xclk_freq / 10000;
1862 u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1863 u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1864 u32 bit_div2x = 1, sclk_rdiv, sysclk;
1868 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1871 temp2 = temp1 & 0x0f;
1872 if (temp2 == 8 || temp2 == 10)
1873 bit_div2x = temp2 / 2;
1875 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1878 sysdiv = temp1 >> 4;
1882 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1887 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1890 prediv = temp1 & 0x0f;
1891 pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1893 ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1896 temp2 = temp1 & 0x03;
1897 sclk_rdiv = sclk_rdiv_map[temp2];
1899 if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1902 VCO = xvclk * multiplier / prediv;
1904 sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1909 static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1911 /* read HTS from register settings */
1915 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1919 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1922 static int ov5640_get_hts(struct ov5640_dev *sensor)
1924 /* read HTS from register settings */
1928 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1934 static int ov5640_get_vts(struct ov5640_dev *sensor)
1939 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1945 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1947 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1950 static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1952 /* get banding filter value */
1953 int ret, light_freq = 0;
1956 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1962 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1975 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1991 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1993 u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1996 /* read preview PCLK */
1997 ret = ov5640_get_sysclk(sensor);
2002 sensor->prev_sysclk = ret;
2003 /* read preview HTS */
2004 ret = ov5640_get_hts(sensor);
2009 sensor->prev_hts = ret;
2011 /* read preview VTS */
2012 ret = ov5640_get_vts(sensor);
2017 /* calculate banding filter */
2019 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
2020 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
2025 max_band60 = (int)((prev_vts - 4) / band_step60);
2026 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
2031 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
2032 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
2037 max_band50 = (int)((prev_vts - 4) / band_step50);
2038 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
2041 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
2043 /* stable in high */
2044 u32 fast_high, fast_low;
2047 sensor->ae_low = target * 23 / 25; /* 0.92 */
2048 sensor->ae_high = target * 27 / 25; /* 1.08 */
2050 fast_high = sensor->ae_high << 1;
2051 if (fast_high > 255)
2054 fast_low = sensor->ae_low >> 1;
2056 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
2059 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
2062 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
2065 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
2068 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
2071 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
2074 static int ov5640_get_binning(struct ov5640_dev *sensor)
2079 ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
2083 return temp & BIT(0);
2086 static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
2092 * - [0]: Horizontal binning enable
2094 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2095 BIT(0), enable ? BIT(0) : 0);
2100 * - [0]: Undocumented, but hardcoded init sequences
2101 * are always setting REG21/REG20 bit 0 to same value...
2103 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2104 BIT(0), enable ? BIT(0) : 0);
2107 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
2109 struct i2c_client *client = sensor->i2c_client;
2110 u8 temp, channel = virtual_channel;
2114 dev_err(&client->dev,
2115 "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
2120 ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
2124 temp |= (channel << 6);
2125 return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
2128 static const struct ov5640_mode_info *
2129 ov5640_find_mode(struct ov5640_dev *sensor, int width, int height, bool nearest)
2131 const struct ov5640_mode_info *mode;
2133 mode = v4l2_find_nearest_size(ov5640_mode_data,
2134 ARRAY_SIZE(ov5640_mode_data),
2135 width, height, width, height);
2139 (mode->width != width || mode->height != height)))
2146 * sensor changes between scaling and subsampling, go through
2147 * exposure calculation
2149 static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
2150 const struct ov5640_mode_info *mode)
2152 u32 prev_shutter, prev_gain16;
2153 u32 cap_shutter, cap_gain16;
2154 u32 cap_sysclk, cap_hts, cap_vts;
2155 u32 light_freq, cap_bandfilt, cap_maxband;
2156 u32 cap_gain16_shutter;
2160 if (!mode->reg_data)
2163 /* read preview shutter */
2164 ret = ov5640_get_exposure(sensor);
2168 ret = ov5640_get_binning(sensor);
2171 if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
2172 mode->id != OV5640_MODE_1080P_1920_1080)
2175 /* read preview gain */
2176 ret = ov5640_get_gain(sensor);
2182 ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
2186 /* turn off night mode for capture */
2187 ret = ov5640_set_night_mode(sensor);
2191 /* Write capture setting */
2192 ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
2193 ret = ov5640_set_timings(sensor, mode);
2197 /* read capture VTS */
2198 ret = ov5640_get_vts(sensor);
2202 ret = ov5640_get_hts(sensor);
2209 ret = ov5640_get_sysclk(sensor);
2216 /* calculate capture banding filter */
2217 ret = ov5640_get_light_freq(sensor);
2222 if (light_freq == 60) {
2224 cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
2227 cap_bandfilt = cap_sysclk * 100 / cap_hts;
2230 if (!sensor->prev_sysclk) {
2231 ret = ov5640_get_sysclk(sensor);
2236 sensor->prev_sysclk = ret;
2242 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
2244 /* calculate capture shutter/gain16 */
2245 if (average > sensor->ae_low && average < sensor->ae_high) {
2246 /* in stable range */
2247 cap_gain16_shutter =
2248 prev_gain16 * prev_shutter *
2249 cap_sysclk / sensor->prev_sysclk *
2250 sensor->prev_hts / cap_hts *
2251 sensor->ae_target / average;
2253 cap_gain16_shutter =
2254 prev_gain16 * prev_shutter *
2255 cap_sysclk / sensor->prev_sysclk *
2256 sensor->prev_hts / cap_hts;
2259 /* gain to shutter */
2260 if (cap_gain16_shutter < (cap_bandfilt * 16)) {
2261 /* shutter < 1/100 */
2262 cap_shutter = cap_gain16_shutter / 16;
2263 if (cap_shutter < 1)
2266 cap_gain16 = cap_gain16_shutter / cap_shutter;
2267 if (cap_gain16 < 16)
2270 if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
2271 /* exposure reach max */
2272 cap_shutter = cap_bandfilt * cap_maxband;
2276 cap_gain16 = cap_gain16_shutter / cap_shutter;
2278 /* 1/100 < (cap_shutter = n/100) =< max */
2280 ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
2285 cap_gain16 = cap_gain16_shutter / cap_shutter;
2289 /* set capture gain */
2290 ret = ov5640_set_gain(sensor, cap_gain16);
2294 /* write capture shutter */
2295 if (cap_shutter > (cap_vts - 4)) {
2296 cap_vts = cap_shutter + 4;
2297 ret = ov5640_set_vts(sensor, cap_vts);
2303 return ov5640_set_exposure(sensor, cap_shutter);
2307 * if sensor changes inside scaling or subsampling
2308 * change mode directly
2310 static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
2311 const struct ov5640_mode_info *mode)
2313 if (!mode->reg_data)
2316 /* Write capture setting */
2317 ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
2318 return ov5640_set_timings(sensor, mode);
2321 static int ov5640_set_mode(struct ov5640_dev *sensor)
2323 const struct ov5640_mode_info *mode = sensor->current_mode;
2324 const struct ov5640_mode_info *orig_mode = sensor->last_mode;
2325 enum ov5640_downsize_mode dn_mode, orig_dn_mode;
2326 bool auto_gain = sensor->ctrls.auto_gain->val == 1;
2327 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
2330 dn_mode = mode->dn_mode;
2331 orig_dn_mode = orig_mode->dn_mode;
2333 /* auto gain and exposure must be turned off when changing modes */
2335 ret = ov5640_set_autogain(sensor, false);
2341 ret = ov5640_set_autoexposure(sensor, false);
2343 goto restore_auto_gain;
2346 if (ov5640_is_csi2(sensor))
2347 ret = ov5640_set_mipi_pclk(sensor);
2349 ret = ov5640_set_dvp_pclk(sensor);
2353 if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
2354 (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
2356 * change between subsampling and scaling
2357 * go through exposure calculation
2359 ret = ov5640_set_mode_exposure_calc(sensor, mode);
2362 * change inside subsampling or scaling
2363 * download firmware directly
2365 ret = ov5640_set_mode_direct(sensor, mode);
2368 goto restore_auto_exp_gain;
2370 /* restore auto gain and exposure */
2372 ov5640_set_autogain(sensor, true);
2374 ov5640_set_autoexposure(sensor, true);
2376 ret = ov5640_set_binning(sensor, dn_mode != SCALING);
2379 ret = ov5640_set_ae_target(sensor, sensor->ae_target);
2382 ret = ov5640_get_light_freq(sensor);
2385 ret = ov5640_set_bandingfilter(sensor);
2388 ret = ov5640_set_virtual_channel(sensor);
2392 sensor->pending_mode_change = false;
2393 sensor->last_mode = mode;
2397 restore_auto_exp_gain:
2399 ov5640_set_autoexposure(sensor, true);
2402 ov5640_set_autogain(sensor, true);
2407 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2408 struct v4l2_mbus_framefmt *format);
2410 /* restore the last set video mode after chip power-on */
2411 static int ov5640_restore_mode(struct ov5640_dev *sensor)
2415 /* first load the initial register values */
2416 ov5640_load_regs(sensor, ov5640_init_setting,
2417 ARRAY_SIZE(ov5640_init_setting));
2419 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
2420 (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) |
2421 ilog2(OV5640_SCLK_ROOT_DIV));
2425 /* now restore the last capture mode */
2426 ret = ov5640_set_mode(sensor);
2430 return ov5640_set_framefmt(sensor, &sensor->fmt);
2433 static void ov5640_power(struct ov5640_dev *sensor, bool enable)
2435 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
2439 * From section 2.7 power up sequence:
2440 * t0 + t1 + t2 >= 5ms Delay from DOVDD stable to PWDN pull down
2441 * t3 >= 1ms Delay from PWDN pull down to RESETB pull up
2442 * t4 >= 20ms Delay from RESETB pull up to SCCB (i2c) stable
2444 * Some modules don't expose RESETB/PWDN pins directly, instead providing a
2445 * "PWUP" GPIO which is wired through appropriate delays and inverters to the
2448 * In such cases, this gpio should be mapped to pwdn_gpio in the driver, and we
2449 * should still toggle the pwdn_gpio below with the appropriate delays, while
2450 * the calls to reset_gpio will be ignored.
2452 static void ov5640_powerup_sequence(struct ov5640_dev *sensor)
2454 if (sensor->pwdn_gpio) {
2455 gpiod_set_value_cansleep(sensor->reset_gpio, 1);
2457 /* camera power cycle */
2458 ov5640_power(sensor, false);
2459 usleep_range(5000, 10000); /* t2 */
2460 ov5640_power(sensor, true);
2461 usleep_range(1000, 2000); /* t3 */
2463 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2465 /* software reset */
2466 ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0,
2467 OV5640_REG_SYS_CTRL0_SW_RST);
2469 usleep_range(20000, 25000); /* t4 */
2472 * software standby: allows registers programming;
2473 * exit at restore_mode() for CSI, s_stream(1) for DVP
2475 ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0,
2476 OV5640_REG_SYS_CTRL0_SW_PWDN);
2479 static int ov5640_set_power_on(struct ov5640_dev *sensor)
2481 struct i2c_client *client = sensor->i2c_client;
2484 ret = clk_prepare_enable(sensor->xclk);
2486 dev_err(&client->dev, "%s: failed to enable clock\n",
2491 ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
2494 dev_err(&client->dev, "%s: failed to enable regulators\n",
2499 ov5640_powerup_sequence(sensor);
2501 ret = ov5640_init_slave_id(sensor);
2508 ov5640_power(sensor, false);
2509 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2511 clk_disable_unprepare(sensor->xclk);
2515 static void ov5640_set_power_off(struct ov5640_dev *sensor)
2517 ov5640_power(sensor, false);
2518 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2519 clk_disable_unprepare(sensor->xclk);
2522 static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on)
2527 /* Reset MIPI bus settings to their default values. */
2528 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2529 ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04);
2530 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00);
2535 * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
2538 * [7:5] = 010 : 2 data lanes mode (see FIXME note in
2539 * "ov5640_set_stream_mipi()")
2540 * [4] = 0 : Power up MIPI HS Tx
2541 * [3] = 0 : Power up MIPI LS Rx
2542 * [2] = 1 : MIPI interface enabled
2544 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x44);
2549 * Gate clock and set LP11 in 'no packets mode' (idle)
2552 * [5] = 1 : Gate clock when 'no packets'
2553 * [2] = 1 : MIPI bus in LP11 when 'no packets'
2555 ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24);
2560 * Set data lanes and clock in LP11 when 'sleeping'
2563 * [6] = 1 : MIPI data lane 2 in LP11 when 'sleeping'
2564 * [5] = 1 : MIPI data lane 1 in LP11 when 'sleeping'
2565 * [4] = 1 : MIPI clock lane in LP11 when 'sleeping'
2567 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70);
2571 /* Give lanes some time to coax into LP11 state. */
2572 usleep_range(500, 1000);
2577 static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on)
2579 unsigned int flags = sensor->ep.bus.parallel.flags;
2580 bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656;
2585 /* Reset settings to their default values. */
2586 ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00);
2587 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2588 ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20);
2589 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00);
2590 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00);
2595 * Note about parallel port configuration.
2597 * When configured in parallel mode, the OV5640 will
2598 * output 10 bits data on DVP data lines [9:0].
2599 * If only 8 bits data are wanted, the 8 bits data lines
2600 * of the camera interface must be physically connected
2601 * on the DVP data lines [9:2].
2603 * Control lines polarity can be configured through
2604 * devicetree endpoint control lines properties.
2605 * If no endpoint control lines properties are set,
2606 * polarity will be as below:
2607 * - VSYNC: active high
2608 * - HREF: active low
2609 * - PCLK: active low
2611 * VSYNC & HREF are not configured if BT656 bus mode is selected
2615 * BT656 embedded synchronization configuration
2618 * - [7]: SYNC code selection (0: auto generate sync code,
2619 * 1: sync code from regs 0x4732-0x4735)
2620 * - [6]: f value in CCIR656 SYNC code when fixed f value
2621 * - [5]: Fixed f value
2622 * - [4:3]: Blank toggle data options (00: data=1'h040/1'h200,
2623 * 01: data from regs 0x4736-0x4738, 10: always keep 0)
2624 * - [1]: Clip data disable
2625 * - [0]: CCIR656 mode enable
2627 * Default CCIR656 SAV/EAV mode with default codes
2628 * SAV=0xff000080 & EAV=0xff00009d is enabled here with settings:
2629 * - CCIR656 mode enable
2630 * - auto generation of sync codes
2631 * - blank toggle data 1'h040/1'h200
2632 * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe)
2634 ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00,
2635 bt656 ? 0x01 : 0x00);
2640 * configure parallel port control lines polarity
2643 * - [5]: PCLK polarity (0: active low, 1: active high)
2644 * - [1]: HREF polarity (0: active low, 1: active high)
2645 * - [0]: VSYNC polarity (mismatch here between
2646 * datasheet and hardware, 0 is active high
2647 * and 1 is active low...)
2650 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2651 polarities |= BIT(1);
2652 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
2653 polarities |= BIT(0);
2655 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
2656 polarities |= BIT(5);
2658 ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities);
2663 * powerdown MIPI TX/RX PHY & enable DVP
2666 * [4] = 1 : Power down MIPI HS Tx
2667 * [3] = 1 : Power down MIPI LS Rx
2668 * [2] = 0 : DVP enable (MIPI disable)
2670 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18);
2675 * enable VSYNC/HREF/PCLK DVP control lines
2676 * & D[9:6] DVP data lines
2678 * PAD OUTPUT ENABLE 01
2679 * - 6: VSYNC output enable
2680 * - 5: HREF output enable
2681 * - 4: PCLK output enable
2682 * - [3:0]: D[9:6] output enable
2684 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01,
2685 bt656 ? 0x1f : 0x7f);
2690 * enable D[5:0] DVP data lines
2692 * PAD OUTPUT ENABLE 02
2693 * - [7:2]: D[5:0] output enable
2695 return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc);
2698 static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
2703 ret = ov5640_set_power_on(sensor);
2707 ret = ov5640_restore_mode(sensor);
2712 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2713 ret = ov5640_set_power_mipi(sensor, on);
2715 ret = ov5640_set_power_dvp(sensor, on);
2720 ov5640_set_power_off(sensor);
2725 ov5640_set_power_off(sensor);
2729 static int ov5640_sensor_suspend(struct device *dev)
2731 struct v4l2_subdev *sd = dev_get_drvdata(dev);
2732 struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
2734 return ov5640_set_power(ov5640, false);
2737 static int ov5640_sensor_resume(struct device *dev)
2739 struct v4l2_subdev *sd = dev_get_drvdata(dev);
2740 struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
2742 return ov5640_set_power(ov5640, true);
2745 /* --------------- Subdev Operations --------------- */
2747 static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
2748 struct v4l2_fract *fi,
2749 const struct ov5640_mode_info *mode_info)
2751 const struct ov5640_mode_info *mode = mode_info;
2752 enum ov5640_frame_rate rate = OV5640_15_FPS;
2753 int minfps, maxfps, best_fps, fps;
2756 minfps = ov5640_framerates[OV5640_15_FPS];
2757 maxfps = ov5640_framerates[mode->max_fps];
2759 if (fi->numerator == 0) {
2760 fi->denominator = maxfps;
2762 rate = mode->max_fps;
2766 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
2770 for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) {
2771 int curr_fps = ov5640_framerates[i];
2773 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
2774 best_fps = curr_fps;
2780 fi->denominator = best_fps;
2783 mode = ov5640_find_mode(sensor, mode->width, mode->height, false);
2784 return mode ? rate : -EINVAL;
2787 static int ov5640_get_fmt(struct v4l2_subdev *sd,
2788 struct v4l2_subdev_state *sd_state,
2789 struct v4l2_subdev_format *format)
2791 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2792 struct v4l2_mbus_framefmt *fmt;
2794 if (format->pad != 0)
2797 mutex_lock(&sensor->lock);
2799 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2800 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
2805 format->format = *fmt;
2807 mutex_unlock(&sensor->lock);
2812 static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
2813 struct v4l2_mbus_framefmt *fmt,
2814 const struct ov5640_mode_info **new_mode)
2816 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2817 const struct ov5640_mode_info *mode;
2818 const struct ov5640_pixfmt *pixfmt;
2821 mode = ov5640_find_mode(sensor, fmt->width, fmt->height, true);
2825 pixfmt = ov5640_code_to_pixfmt(sensor, fmt->code);
2829 * Adjust mode according to bpp:
2830 * - 8bpp modes work for resolution >= 1280x720
2831 * - 24bpp modes work resolution < 1280x720
2833 if (bpp == 8 && mode->width < 1280)
2834 mode = &ov5640_mode_data[OV5640_MODE_720P_1280_720];
2835 else if (bpp == 24 && mode->width > 1024)
2836 mode = &ov5640_mode_data[OV5640_MODE_XGA_1024_768];
2838 fmt->width = mode->width;
2839 fmt->height = mode->height;
2844 fmt->code = pixfmt->code;
2845 fmt->colorspace = pixfmt->colorspace;
2846 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2847 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2848 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2853 static void __v4l2_ctrl_vblank_update(struct ov5640_dev *sensor, u32 vblank)
2855 const struct ov5640_mode_info *mode = sensor->current_mode;
2857 __v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV5640_MIN_VBLANK,
2858 OV5640_MAX_VTS - mode->height, 1, vblank);
2860 __v4l2_ctrl_s_ctrl(sensor->ctrls.vblank, vblank);
2863 static int ov5640_update_pixel_rate(struct ov5640_dev *sensor)
2865 const struct ov5640_mode_info *mode = sensor->current_mode;
2866 enum ov5640_pixel_rate_id pixel_rate_id = mode->pixel_rate;
2867 struct v4l2_mbus_framefmt *fmt = &sensor->fmt;
2868 const struct ov5640_timings *timings = ov5640_timings(sensor, mode);
2869 s32 exposure_val, exposure_max;
2870 unsigned int hblank;
2879 * Update the pixel rate control value.
2881 * For DVP mode, maintain the pixel rate calculation using fixed FPS.
2883 if (!ov5640_is_csi2(sensor)) {
2884 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2885 ov5640_calc_pixel_rate(sensor));
2887 __v4l2_ctrl_vblank_update(sensor, timings->vblank_def);
2893 * The MIPI CSI-2 link frequency should comply with the CSI-2
2894 * specification and be lower than 1GHz.
2896 * Start from the suggested pixel_rate for the current mode and
2897 * progressively slow it down if it exceeds 1GHz.
2899 num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
2900 bpp = ov5640_code_to_bpp(sensor, fmt->code);
2902 pixel_rate = ov5640_pixel_rates[pixel_rate_id];
2903 link_freq = pixel_rate * bpp / (2 * num_lanes);
2904 } while (link_freq >= 1000000000U &&
2905 ++pixel_rate_id < OV5640_NUM_PIXEL_RATES);
2907 sensor->current_link_freq = link_freq;
2910 * Higher link rates require the clock tree to be programmed with
2911 * 'mipi_div' = 1; this has the effect of halving the actual output
2912 * pixel rate in the MIPI domain.
2914 * Adjust the pixel rate and link frequency control value to report it
2915 * correctly to userspace.
2917 if (link_freq > OV5640_LINK_RATE_MAX) {
2922 for (i = 0; i < ARRAY_SIZE(ov5640_csi2_link_freqs); ++i) {
2923 if (ov5640_csi2_link_freqs[i] == link_freq)
2926 WARN_ON(i == ARRAY_SIZE(ov5640_csi2_link_freqs));
2928 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, pixel_rate);
2929 __v4l2_ctrl_s_ctrl(sensor->ctrls.link_freq, i);
2931 hblank = timings->htot - mode->width;
2932 __v4l2_ctrl_modify_range(sensor->ctrls.hblank,
2933 hblank, hblank, 1, hblank);
2935 vblank = timings->vblank_def;
2936 __v4l2_ctrl_vblank_update(sensor, vblank);
2938 exposure_max = timings->crop.height + vblank - 4;
2939 exposure_val = clamp_t(s32, sensor->ctrls.exposure->val,
2940 sensor->ctrls.exposure->minimum,
2943 __v4l2_ctrl_modify_range(sensor->ctrls.exposure,
2944 sensor->ctrls.exposure->minimum,
2945 exposure_max, 1, exposure_val);
2950 static int ov5640_set_fmt(struct v4l2_subdev *sd,
2951 struct v4l2_subdev_state *sd_state,
2952 struct v4l2_subdev_format *format)
2954 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2955 const struct ov5640_mode_info *new_mode;
2956 struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
2959 if (format->pad != 0)
2962 mutex_lock(&sensor->lock);
2964 if (sensor->streaming) {
2969 ret = ov5640_try_fmt_internal(sd, mbus_fmt, &new_mode);
2973 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2974 *v4l2_subdev_get_try_format(sd, sd_state, 0) = *mbus_fmt;
2978 if (new_mode != sensor->current_mode) {
2979 sensor->current_fr = new_mode->def_fps;
2980 sensor->current_mode = new_mode;
2981 sensor->pending_mode_change = true;
2983 if (mbus_fmt->code != sensor->fmt.code)
2984 sensor->pending_fmt_change = true;
2986 /* update format even if code is unchanged, resolution might change */
2987 sensor->fmt = *mbus_fmt;
2989 ov5640_update_pixel_rate(sensor);
2992 mutex_unlock(&sensor->lock);
2996 static int ov5640_get_selection(struct v4l2_subdev *sd,
2997 struct v4l2_subdev_state *sd_state,
2998 struct v4l2_subdev_selection *sel)
3000 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3001 const struct ov5640_mode_info *mode = sensor->current_mode;
3002 const struct ov5640_timings *timings;
3004 switch (sel->target) {
3005 case V4L2_SEL_TGT_CROP: {
3006 mutex_lock(&sensor->lock);
3007 timings = ov5640_timings(sensor, mode);
3008 sel->r = timings->analog_crop;
3009 mutex_unlock(&sensor->lock);
3014 case V4L2_SEL_TGT_NATIVE_SIZE:
3015 case V4L2_SEL_TGT_CROP_BOUNDS:
3018 sel->r.width = OV5640_NATIVE_WIDTH;
3019 sel->r.height = OV5640_NATIVE_HEIGHT;
3023 case V4L2_SEL_TGT_CROP_DEFAULT:
3024 sel->r.top = OV5640_PIXEL_ARRAY_TOP;
3025 sel->r.left = OV5640_PIXEL_ARRAY_LEFT;
3026 sel->r.width = OV5640_PIXEL_ARRAY_WIDTH;
3027 sel->r.height = OV5640_PIXEL_ARRAY_HEIGHT;
3035 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
3036 struct v4l2_mbus_framefmt *format)
3038 bool is_jpeg = format->code == MEDIA_BUS_FMT_JPEG_1X8;
3039 const struct ov5640_pixfmt *pixfmt;
3042 pixfmt = ov5640_code_to_pixfmt(sensor, format->code);
3044 /* FORMAT CONTROL00: YUV and RGB formatting */
3045 ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00,
3050 /* FORMAT MUX CONTROL: ISP YUV or RGB */
3051 ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
3058 * - [5]: JPEG enable
3060 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
3061 BIT(5), is_jpeg ? BIT(5) : 0);
3067 * - [4]: Reset JFIFO
3068 * - [3]: Reset SFIFO
3071 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
3072 BIT(4) | BIT(3) | BIT(2),
3073 is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
3079 * - [5]: Enable JPEG 2x clock
3080 * - [3]: Enable JPEG clock
3082 return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
3084 is_jpeg ? (BIT(5) | BIT(3)) : 0);
3091 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
3096 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3100 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
3102 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
3108 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
3113 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3117 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
3120 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
3126 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
3131 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3135 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
3139 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
3142 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
3148 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
3152 ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
3153 BIT(0), awb ? 0 : 1);
3158 u16 red = (u16)sensor->ctrls.red_balance->val;
3159 u16 blue = (u16)sensor->ctrls.blue_balance->val;
3161 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
3164 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
3170 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
3171 enum v4l2_exposure_auto_type auto_exposure)
3173 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3174 bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
3177 if (ctrls->auto_exp->is_new) {
3178 ret = ov5640_set_autoexposure(sensor, auto_exp);
3183 if (!auto_exp && ctrls->exposure->is_new) {
3186 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
3190 ret = ov5640_get_vts(sensor);
3196 if (ctrls->exposure->val < max_exp)
3197 ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
3203 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
3205 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3208 if (ctrls->auto_gain->is_new) {
3209 ret = ov5640_set_autogain(sensor, auto_gain);
3214 if (!auto_gain && ctrls->gain->is_new)
3215 ret = ov5640_set_gain(sensor, ctrls->gain->val);
3220 static const char * const test_pattern_menu[] = {
3223 "Color bars w/ rolling bar",
3225 "Color squares w/ rolling bar",
3228 #define OV5640_TEST_ENABLE BIT(7)
3229 #define OV5640_TEST_ROLLING BIT(6) /* rolling horizontal bar */
3230 #define OV5640_TEST_TRANSPARENT BIT(5)
3231 #define OV5640_TEST_SQUARE_BW BIT(4) /* black & white squares */
3232 #define OV5640_TEST_BAR_STANDARD (0 << 2)
3233 #define OV5640_TEST_BAR_VERT_CHANGE_1 (1 << 2)
3234 #define OV5640_TEST_BAR_HOR_CHANGE (2 << 2)
3235 #define OV5640_TEST_BAR_VERT_CHANGE_2 (3 << 2)
3236 #define OV5640_TEST_BAR (0 << 0)
3237 #define OV5640_TEST_RANDOM (1 << 0)
3238 #define OV5640_TEST_SQUARE (2 << 0)
3239 #define OV5640_TEST_BLACK (3 << 0)
3241 static const u8 test_pattern_val[] = {
3243 OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 |
3245 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING |
3246 OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR,
3247 OV5640_TEST_ENABLE | OV5640_TEST_SQUARE,
3248 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE,
3251 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
3253 return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
3254 test_pattern_val[value]);
3257 static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
3261 ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
3262 (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
3267 return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
3268 (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
3272 static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
3275 * If sensor is mounted upside down, mirror logic is inversed.
3277 * Sensor is a BSI (Back Side Illuminated) one,
3278 * so image captured is physically mirrored.
3279 * This is why mirror logic is inversed in
3280 * order to cancel this mirror effect.
3286 * - [1]: Sensor mirror
3288 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
3290 (!(value ^ sensor->upside_down)) ?
3291 (BIT(2) | BIT(1)) : 0);
3294 static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
3296 /* If sensor is mounted upside down, flip logic is inversed */
3301 * - [1]: Sensor vflip
3303 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
3305 (value ^ sensor->upside_down) ?
3306 (BIT(2) | BIT(1)) : 0);
3309 static int ov5640_set_ctrl_vblank(struct ov5640_dev *sensor, int value)
3311 const struct ov5640_mode_info *mode = sensor->current_mode;
3313 /* Update the VTOT timing register value. */
3314 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
3315 mode->height + value);
3318 static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
3320 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3321 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3324 /* v4l2_ctrl_lock() locks our own mutex */
3326 if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
3330 case V4L2_CID_AUTOGAIN:
3331 val = ov5640_get_gain(sensor);
3334 sensor->ctrls.gain->val = val;
3336 case V4L2_CID_EXPOSURE_AUTO:
3337 val = ov5640_get_exposure(sensor);
3340 sensor->ctrls.exposure->val = val;
3344 pm_runtime_mark_last_busy(&sensor->i2c_client->dev);
3345 pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3350 static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
3352 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3353 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3354 const struct ov5640_mode_info *mode = sensor->current_mode;
3355 const struct ov5640_timings *timings;
3356 unsigned int exp_max;
3359 /* v4l2_ctrl_lock() locks our own mutex */
3362 case V4L2_CID_VBLANK:
3363 /* Update the exposure range to the newly programmed vblank. */
3364 timings = ov5640_timings(sensor, mode);
3365 exp_max = mode->height + ctrl->val - 4;
3366 __v4l2_ctrl_modify_range(sensor->ctrls.exposure,
3367 sensor->ctrls.exposure->minimum,
3368 exp_max, sensor->ctrls.exposure->step,
3369 timings->vblank_def);
3374 * If the device is not powered up by the host driver do
3375 * not apply any controls to H/W at this time. Instead
3376 * the controls will be restored at start streaming time.
3378 if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
3382 case V4L2_CID_AUTOGAIN:
3383 ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
3385 case V4L2_CID_EXPOSURE_AUTO:
3386 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
3388 case V4L2_CID_AUTO_WHITE_BALANCE:
3389 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
3392 ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
3394 case V4L2_CID_CONTRAST:
3395 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
3397 case V4L2_CID_SATURATION:
3398 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
3400 case V4L2_CID_TEST_PATTERN:
3401 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
3403 case V4L2_CID_POWER_LINE_FREQUENCY:
3404 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
3406 case V4L2_CID_HFLIP:
3407 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
3409 case V4L2_CID_VFLIP:
3410 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
3412 case V4L2_CID_VBLANK:
3413 ret = ov5640_set_ctrl_vblank(sensor, ctrl->val);
3420 pm_runtime_mark_last_busy(&sensor->i2c_client->dev);
3421 pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3426 static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
3427 .g_volatile_ctrl = ov5640_g_volatile_ctrl,
3428 .s_ctrl = ov5640_s_ctrl,
3431 static int ov5640_init_controls(struct ov5640_dev *sensor)
3433 const struct ov5640_mode_info *mode = sensor->current_mode;
3434 const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
3435 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3436 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
3437 struct v4l2_fwnode_device_properties props;
3438 const struct ov5640_timings *timings;
3439 unsigned int max_vblank;
3440 unsigned int hblank;
3443 v4l2_ctrl_handler_init(hdl, 32);
3445 /* we can use our own mutex for the ctrl lock */
3446 hdl->lock = &sensor->lock;
3448 /* Clock related controls */
3449 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
3450 ov5640_pixel_rates[OV5640_NUM_PIXEL_RATES - 1],
3451 ov5640_pixel_rates[0], 1,
3452 ov5640_pixel_rates[mode->pixel_rate]);
3454 ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, ops,
3456 ARRAY_SIZE(ov5640_csi2_link_freqs) - 1,
3457 OV5640_DEFAULT_LINK_FREQ,
3458 ov5640_csi2_link_freqs);
3460 timings = ov5640_timings(sensor, mode);
3461 hblank = timings->htot - mode->width;
3462 ctrls->hblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HBLANK, hblank,
3465 max_vblank = OV5640_MAX_VTS - mode->height;
3466 ctrls->vblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VBLANK,
3467 OV5640_MIN_VBLANK, max_vblank,
3468 1, timings->vblank_def);
3470 /* Auto/manual white balance */
3471 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
3472 V4L2_CID_AUTO_WHITE_BALANCE,
3474 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
3476 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
3478 /* Auto/manual exposure */
3479 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
3480 V4L2_CID_EXPOSURE_AUTO,
3481 V4L2_EXPOSURE_MANUAL, 0,
3482 V4L2_EXPOSURE_AUTO);
3483 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
3485 /* Auto/manual gain */
3486 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
3488 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_ANALOGUE_GAIN,
3491 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
3493 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
3495 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
3497 ctrls->test_pattern =
3498 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
3499 ARRAY_SIZE(test_pattern_menu) - 1,
3500 0, 0, test_pattern_menu);
3501 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
3503 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
3507 v4l2_ctrl_new_std_menu(hdl, ops,
3508 V4L2_CID_POWER_LINE_FREQUENCY,
3509 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
3510 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
3517 ret = v4l2_fwnode_device_parse(&sensor->i2c_client->dev, &props);
3521 if (props.rotation == 180)
3522 sensor->upside_down = true;
3524 ret = v4l2_ctrl_new_fwnode_properties(hdl, ops, &props);
3528 ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3529 ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3530 ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3531 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
3532 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
3534 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
3535 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
3536 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
3538 sensor->sd.ctrl_handler = hdl;
3542 v4l2_ctrl_handler_free(hdl);
3546 static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
3547 struct v4l2_subdev_state *sd_state,
3548 struct v4l2_subdev_frame_size_enum *fse)
3550 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3551 u32 bpp = ov5640_code_to_bpp(sensor, fse->code);
3552 unsigned int index = fse->index;
3559 /* Only low-resolution modes are supported for 24bpp formats. */
3560 if (bpp == 24 && index >= OV5640_MODE_720P_1280_720)
3563 /* FIXME: Low resolution modes don't work in 8bpp formats. */
3565 index += OV5640_MODE_720P_1280_720;
3567 if (index >= OV5640_NUM_MODES)
3570 fse->min_width = ov5640_mode_data[index].width;
3571 fse->max_width = fse->min_width;
3572 fse->min_height = ov5640_mode_data[index].height;
3573 fse->max_height = fse->min_height;
3578 static int ov5640_enum_frame_interval(
3579 struct v4l2_subdev *sd,
3580 struct v4l2_subdev_state *sd_state,
3581 struct v4l2_subdev_frame_interval_enum *fie)
3583 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3584 const struct ov5640_mode_info *mode;
3585 struct v4l2_fract tpf;
3590 if (fie->index >= OV5640_NUM_FRAMERATES)
3593 mode = ov5640_find_mode(sensor, fie->width, fie->height, false);
3598 tpf.denominator = ov5640_framerates[fie->index];
3600 ret = ov5640_try_frame_interval(sensor, &tpf, mode);
3604 fie->interval = tpf;
3608 static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
3609 struct v4l2_subdev_frame_interval *fi)
3611 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3613 mutex_lock(&sensor->lock);
3614 fi->interval = sensor->frame_interval;
3615 mutex_unlock(&sensor->lock);
3620 static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
3621 struct v4l2_subdev_frame_interval *fi)
3623 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3624 const struct ov5640_mode_info *mode;
3625 int frame_rate, ret = 0;
3630 mutex_lock(&sensor->lock);
3632 if (sensor->streaming) {
3637 mode = sensor->current_mode;
3639 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval, mode);
3640 if (frame_rate < 0) {
3641 /* Always return a valid frame interval value */
3642 fi->interval = sensor->frame_interval;
3646 mode = ov5640_find_mode(sensor, mode->width, mode->height, true);
3652 if (ov5640_framerates[frame_rate] > ov5640_framerates[mode->max_fps]) {
3657 if (mode != sensor->current_mode ||
3658 frame_rate != sensor->current_fr) {
3659 sensor->current_fr = frame_rate;
3660 sensor->frame_interval = fi->interval;
3661 sensor->current_mode = mode;
3662 sensor->pending_mode_change = true;
3664 ov5640_update_pixel_rate(sensor);
3667 mutex_unlock(&sensor->lock);
3671 static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
3672 struct v4l2_subdev_state *sd_state,
3673 struct v4l2_subdev_mbus_code_enum *code)
3675 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3676 const struct ov5640_pixfmt *formats;
3677 unsigned int num_formats;
3679 if (ov5640_is_csi2(sensor)) {
3680 formats = ov5640_csi2_formats;
3681 num_formats = ARRAY_SIZE(ov5640_csi2_formats) - 1;
3683 formats = ov5640_dvp_formats;
3684 num_formats = ARRAY_SIZE(ov5640_dvp_formats) - 1;
3687 if (code->index >= num_formats)
3690 code->code = formats[code->index].code;
3695 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
3697 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3701 ret = pm_runtime_resume_and_get(&sensor->i2c_client->dev);
3705 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
3707 pm_runtime_put(&sensor->i2c_client->dev);
3712 mutex_lock(&sensor->lock);
3714 if (sensor->streaming == !enable) {
3715 if (enable && sensor->pending_mode_change) {
3716 ret = ov5640_set_mode(sensor);
3721 if (enable && sensor->pending_fmt_change) {
3722 ret = ov5640_set_framefmt(sensor, &sensor->fmt);
3725 sensor->pending_fmt_change = false;
3728 if (ov5640_is_csi2(sensor))
3729 ret = ov5640_set_stream_mipi(sensor, enable);
3731 ret = ov5640_set_stream_dvp(sensor, enable);
3734 sensor->streaming = enable;
3738 mutex_unlock(&sensor->lock);
3740 if (!enable || ret) {
3741 pm_runtime_mark_last_busy(&sensor->i2c_client->dev);
3742 pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3748 static int ov5640_init_cfg(struct v4l2_subdev *sd,
3749 struct v4l2_subdev_state *state)
3751 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3752 struct v4l2_mbus_framefmt *fmt =
3753 v4l2_subdev_get_try_format(sd, state, 0);
3754 struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, state, 0);
3756 *fmt = ov5640_is_csi2(sensor) ? ov5640_csi2_default_fmt :
3757 ov5640_dvp_default_fmt;
3759 crop->left = OV5640_PIXEL_ARRAY_LEFT;
3760 crop->top = OV5640_PIXEL_ARRAY_TOP;
3761 crop->width = OV5640_PIXEL_ARRAY_WIDTH;
3762 crop->height = OV5640_PIXEL_ARRAY_HEIGHT;
3767 static const struct v4l2_subdev_core_ops ov5640_core_ops = {
3768 .log_status = v4l2_ctrl_subdev_log_status,
3769 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
3770 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
3773 static const struct v4l2_subdev_video_ops ov5640_video_ops = {
3774 .g_frame_interval = ov5640_g_frame_interval,
3775 .s_frame_interval = ov5640_s_frame_interval,
3776 .s_stream = ov5640_s_stream,
3779 static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
3780 .init_cfg = ov5640_init_cfg,
3781 .enum_mbus_code = ov5640_enum_mbus_code,
3782 .get_fmt = ov5640_get_fmt,
3783 .set_fmt = ov5640_set_fmt,
3784 .get_selection = ov5640_get_selection,
3785 .enum_frame_size = ov5640_enum_frame_size,
3786 .enum_frame_interval = ov5640_enum_frame_interval,
3789 static const struct v4l2_subdev_ops ov5640_subdev_ops = {
3790 .core = &ov5640_core_ops,
3791 .video = &ov5640_video_ops,
3792 .pad = &ov5640_pad_ops,
3795 static int ov5640_get_regulators(struct ov5640_dev *sensor)
3799 for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
3800 sensor->supplies[i].supply = ov5640_supply_name[i];
3802 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
3803 OV5640_NUM_SUPPLIES,
3807 static int ov5640_check_chip_id(struct ov5640_dev *sensor)
3809 struct i2c_client *client = sensor->i2c_client;
3813 ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
3815 dev_err(&client->dev, "%s: failed to read chip identifier\n",
3820 if (chip_id != 0x5640) {
3821 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
3829 static int ov5640_probe(struct i2c_client *client)
3831 struct device *dev = &client->dev;
3832 struct fwnode_handle *endpoint;
3833 struct ov5640_dev *sensor;
3836 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
3840 sensor->i2c_client = client;
3843 * default init sequence initialize sensor to
3844 * YUV422 UYVY VGA(30FPS in parallel mode, 60 in MIPI CSI-2 mode)
3846 sensor->frame_interval.numerator = 1;
3847 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
3848 sensor->current_fr = OV5640_30_FPS;
3849 sensor->current_mode =
3850 &ov5640_mode_data[OV5640_MODE_VGA_640_480];
3851 sensor->last_mode = sensor->current_mode;
3852 sensor->current_link_freq =
3853 ov5640_csi2_link_freqs[OV5640_DEFAULT_LINK_FREQ];
3855 sensor->ae_target = 52;
3857 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
3860 dev_err(dev, "endpoint node not found\n");
3864 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
3865 fwnode_handle_put(endpoint);
3867 dev_err(dev, "Could not parse endpoint\n");
3871 if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
3872 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
3873 sensor->ep.bus_type != V4L2_MBUS_BT656) {
3874 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
3878 sensor->fmt = ov5640_is_csi2(sensor) ? ov5640_csi2_default_fmt :
3879 ov5640_dvp_default_fmt;
3881 /* get system clock (xclk) */
3882 sensor->xclk = devm_clk_get(dev, "xclk");
3883 if (IS_ERR(sensor->xclk)) {
3884 dev_err(dev, "failed to get xclk\n");
3885 return PTR_ERR(sensor->xclk);
3888 sensor->xclk_freq = clk_get_rate(sensor->xclk);
3889 if (sensor->xclk_freq < OV5640_XCLK_MIN ||
3890 sensor->xclk_freq > OV5640_XCLK_MAX) {
3891 dev_err(dev, "xclk frequency out of range: %d Hz\n",
3896 /* request optional power down pin */
3897 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
3899 if (IS_ERR(sensor->pwdn_gpio))
3900 return PTR_ERR(sensor->pwdn_gpio);
3902 /* request optional reset pin */
3903 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
3905 if (IS_ERR(sensor->reset_gpio))
3906 return PTR_ERR(sensor->reset_gpio);
3908 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
3910 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
3911 V4L2_SUBDEV_FL_HAS_EVENTS;
3912 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
3913 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3914 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
3918 ret = ov5640_get_regulators(sensor);
3920 goto entity_cleanup;
3922 mutex_init(&sensor->lock);
3924 ret = ov5640_init_controls(sensor);
3926 goto entity_cleanup;
3928 ret = ov5640_sensor_resume(dev);
3930 dev_err(dev, "failed to power on\n");
3934 pm_runtime_set_active(dev);
3935 pm_runtime_get_noresume(dev);
3936 pm_runtime_enable(dev);
3938 ret = ov5640_check_chip_id(sensor);
3940 goto err_pm_runtime;
3942 ret = v4l2_async_register_subdev_sensor(&sensor->sd);
3944 goto err_pm_runtime;
3946 pm_runtime_set_autosuspend_delay(dev, 1000);
3947 pm_runtime_use_autosuspend(dev);
3948 pm_runtime_mark_last_busy(dev);
3949 pm_runtime_put_autosuspend(dev);
3954 pm_runtime_put_noidle(dev);
3955 pm_runtime_disable(dev);
3956 ov5640_sensor_suspend(dev);
3958 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3960 media_entity_cleanup(&sensor->sd.entity);
3961 mutex_destroy(&sensor->lock);
3965 static void ov5640_remove(struct i2c_client *client)
3967 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3968 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3969 struct device *dev = &client->dev;
3971 pm_runtime_disable(dev);
3972 if (!pm_runtime_status_suspended(dev))
3973 ov5640_sensor_suspend(dev);
3974 pm_runtime_set_suspended(dev);
3976 v4l2_async_unregister_subdev(&sensor->sd);
3977 media_entity_cleanup(&sensor->sd.entity);
3978 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3979 mutex_destroy(&sensor->lock);
3982 static const struct dev_pm_ops ov5640_pm_ops = {
3983 SET_RUNTIME_PM_OPS(ov5640_sensor_suspend, ov5640_sensor_resume, NULL)
3986 static const struct i2c_device_id ov5640_id[] = {
3990 MODULE_DEVICE_TABLE(i2c, ov5640_id);
3992 static const struct of_device_id ov5640_dt_ids[] = {
3993 { .compatible = "ovti,ov5640" },
3996 MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
3998 static struct i2c_driver ov5640_i2c_driver = {
4001 .of_match_table = ov5640_dt_ids,
4002 .pm = &ov5640_pm_ops,
4004 .id_table = ov5640_id,
4005 .probe = ov5640_probe,
4006 .remove = ov5640_remove,
4009 module_i2c_driver(ov5640_i2c_driver);
4011 MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
4012 MODULE_LICENSE("GPL");