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/regulator/consumer.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <media/v4l2-async.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-fwnode.h>
26 #include <media/v4l2-subdev.h>
28 /* min/typical/max system clock (xclk) frequencies */
29 #define OV5640_XCLK_MIN 6000000
30 #define OV5640_XCLK_MAX 54000000
32 #define OV5640_NATIVE_WIDTH 2624
33 #define OV5640_NATIVE_HEIGHT 1964
34 #define OV5640_PIXEL_ARRAY_TOP 14
35 #define OV5640_PIXEL_ARRAY_LEFT 16
36 #define OV5640_PIXEL_ARRAY_WIDTH 2592
37 #define OV5640_PIXEL_ARRAY_HEIGHT 1944
39 /* FIXME: not documented. */
40 #define OV5640_MIN_VBLANK 24
41 #define OV5640_MAX_VTS 3375
43 #define OV5640_DEFAULT_SLAVE_ID 0x3c
45 #define OV5640_LINK_RATE_MAX 490000000U
47 #define OV5640_REG_SYS_RESET02 0x3002
48 #define OV5640_REG_SYS_CLOCK_ENABLE02 0x3006
49 #define OV5640_REG_SYS_CTRL0 0x3008
50 #define OV5640_REG_SYS_CTRL0_SW_PWDN 0x42
51 #define OV5640_REG_SYS_CTRL0_SW_PWUP 0x02
52 #define OV5640_REG_CHIP_ID 0x300a
53 #define OV5640_REG_IO_MIPI_CTRL00 0x300e
54 #define OV5640_REG_PAD_OUTPUT_ENABLE01 0x3017
55 #define OV5640_REG_PAD_OUTPUT_ENABLE02 0x3018
56 #define OV5640_REG_PAD_OUTPUT00 0x3019
57 #define OV5640_REG_SYSTEM_CONTROL1 0x302e
58 #define OV5640_REG_SC_PLL_CTRL0 0x3034
59 #define OV5640_REG_SC_PLL_CTRL1 0x3035
60 #define OV5640_REG_SC_PLL_CTRL2 0x3036
61 #define OV5640_REG_SC_PLL_CTRL3 0x3037
62 #define OV5640_REG_SLAVE_ID 0x3100
63 #define OV5640_REG_SCCB_SYS_CTRL1 0x3103
64 #define OV5640_REG_SYS_ROOT_DIVIDER 0x3108
65 #define OV5640_REG_AWB_R_GAIN 0x3400
66 #define OV5640_REG_AWB_G_GAIN 0x3402
67 #define OV5640_REG_AWB_B_GAIN 0x3404
68 #define OV5640_REG_AWB_MANUAL_CTRL 0x3406
69 #define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500
70 #define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501
71 #define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502
72 #define OV5640_REG_AEC_PK_MANUAL 0x3503
73 #define OV5640_REG_AEC_PK_REAL_GAIN 0x350a
74 #define OV5640_REG_AEC_PK_VTS 0x350c
75 #define OV5640_REG_TIMING_HS 0x3800
76 #define OV5640_REG_TIMING_VS 0x3802
77 #define OV5640_REG_TIMING_HW 0x3804
78 #define OV5640_REG_TIMING_VH 0x3806
79 #define OV5640_REG_TIMING_DVPHO 0x3808
80 #define OV5640_REG_TIMING_DVPVO 0x380a
81 #define OV5640_REG_TIMING_HTS 0x380c
82 #define OV5640_REG_TIMING_VTS 0x380e
83 #define OV5640_REG_TIMING_HOFFS 0x3810
84 #define OV5640_REG_TIMING_VOFFS 0x3812
85 #define OV5640_REG_TIMING_TC_REG20 0x3820
86 #define OV5640_REG_TIMING_TC_REG21 0x3821
87 #define OV5640_REG_AEC_CTRL00 0x3a00
88 #define OV5640_REG_AEC_B50_STEP 0x3a08
89 #define OV5640_REG_AEC_B60_STEP 0x3a0a
90 #define OV5640_REG_AEC_CTRL0D 0x3a0d
91 #define OV5640_REG_AEC_CTRL0E 0x3a0e
92 #define OV5640_REG_AEC_CTRL0F 0x3a0f
93 #define OV5640_REG_AEC_CTRL10 0x3a10
94 #define OV5640_REG_AEC_CTRL11 0x3a11
95 #define OV5640_REG_AEC_CTRL1B 0x3a1b
96 #define OV5640_REG_AEC_CTRL1E 0x3a1e
97 #define OV5640_REG_AEC_CTRL1F 0x3a1f
98 #define OV5640_REG_HZ5060_CTRL00 0x3c00
99 #define OV5640_REG_HZ5060_CTRL01 0x3c01
100 #define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c
101 #define OV5640_REG_FRAME_CTRL01 0x4202
102 #define OV5640_REG_FORMAT_CONTROL00 0x4300
103 #define OV5640_REG_VFIFO_HSIZE 0x4602
104 #define OV5640_REG_VFIFO_VSIZE 0x4604
105 #define OV5640_REG_JPG_MODE_SELECT 0x4713
106 #define OV5640_REG_CCIR656_CTRL00 0x4730
107 #define OV5640_REG_POLARITY_CTRL00 0x4740
108 #define OV5640_REG_MIPI_CTRL00 0x4800
109 #define OV5640_REG_DEBUG_MODE 0x4814
110 #define OV5640_REG_PCLK_PERIOD 0x4837
111 #define OV5640_REG_ISP_FORMAT_MUX_CTRL 0x501f
112 #define OV5640_REG_PRE_ISP_TEST_SET1 0x503d
113 #define OV5640_REG_SDE_CTRL0 0x5580
114 #define OV5640_REG_SDE_CTRL1 0x5581
115 #define OV5640_REG_SDE_CTRL3 0x5583
116 #define OV5640_REG_SDE_CTRL4 0x5584
117 #define OV5640_REG_SDE_CTRL5 0x5585
118 #define OV5640_REG_AVG_READOUT 0x56a1
120 enum ov5640_mode_id {
121 OV5640_MODE_QQVGA_160_120 = 0,
122 OV5640_MODE_QCIF_176_144,
123 OV5640_MODE_QVGA_320_240,
124 OV5640_MODE_VGA_640_480,
125 OV5640_MODE_NTSC_720_480,
126 OV5640_MODE_PAL_720_576,
127 OV5640_MODE_XGA_1024_768,
128 OV5640_MODE_720P_1280_720,
129 OV5640_MODE_1080P_1920_1080,
130 OV5640_MODE_QSXGA_2592_1944,
134 enum ov5640_frame_rate {
138 OV5640_NUM_FRAMERATES,
141 enum ov5640_pixel_rate_id {
142 OV5640_PIXEL_RATE_168M,
143 OV5640_PIXEL_RATE_148M,
144 OV5640_PIXEL_RATE_124M,
145 OV5640_PIXEL_RATE_96M,
146 OV5640_PIXEL_RATE_48M,
147 OV5640_NUM_PIXEL_RATES,
151 * The chip manual suggests 24/48/96/192 MHz pixel clocks.
153 * 192MHz exceeds the sysclk limits; use 168MHz as maximum pixel rate for
154 * full resolution mode @15 FPS.
156 static const u32 ov5640_pixel_rates[] = {
157 [OV5640_PIXEL_RATE_168M] = 168000000,
158 [OV5640_PIXEL_RATE_148M] = 148000000,
159 [OV5640_PIXEL_RATE_124M] = 124000000,
160 [OV5640_PIXEL_RATE_96M] = 96000000,
161 [OV5640_PIXEL_RATE_48M] = 48000000,
165 * MIPI CSI-2 link frequencies.
167 * Derived from the above defined pixel rate for bpp = (8, 16, 24) and
168 * data_lanes = (1, 2)
170 * link_freq = (pixel_rate * bpp) / (2 * data_lanes)
172 static const s64 ov5640_csi2_link_freqs[] = {
173 992000000, 888000000, 768000000, 744000000, 672000000, 672000000,
174 592000000, 592000000, 576000000, 576000000, 496000000, 496000000,
175 384000000, 384000000, 384000000, 336000000, 296000000, 288000000,
176 248000000, 192000000, 192000000, 192000000, 96000000,
179 /* Link freq for default mode: UYVY 16 bpp, 2 data lanes. */
180 #define OV5640_DEFAULT_LINK_FREQ 13
182 enum ov5640_format_mux {
183 OV5640_FMT_MUX_YUV422 = 0,
185 OV5640_FMT_MUX_DITHER,
186 OV5640_FMT_MUX_RAW_DPC,
187 OV5640_FMT_MUX_SNR_RAW,
188 OV5640_FMT_MUX_RAW_CIP,
191 static const struct ov5640_pixfmt {
195 } ov5640_formats[] = {
197 .code = MEDIA_BUS_FMT_JPEG_1X8,
198 .colorspace = V4L2_COLORSPACE_JPEG,
201 .code = MEDIA_BUS_FMT_UYVY8_2X8,
202 .colorspace = V4L2_COLORSPACE_SRGB,
205 .code = MEDIA_BUS_FMT_UYVY8_1X16,
206 .colorspace = V4L2_COLORSPACE_SRGB,
209 .code = MEDIA_BUS_FMT_YUYV8_2X8,
210 .colorspace = V4L2_COLORSPACE_SRGB,
213 .code = MEDIA_BUS_FMT_YUYV8_1X16,
214 .colorspace = V4L2_COLORSPACE_SRGB,
217 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
218 .colorspace = V4L2_COLORSPACE_SRGB,
221 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
222 .colorspace = V4L2_COLORSPACE_SRGB,
225 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
226 .colorspace = V4L2_COLORSPACE_SRGB,
229 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
230 .colorspace = V4L2_COLORSPACE_SRGB,
233 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
234 .colorspace = V4L2_COLORSPACE_SRGB,
237 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
238 .colorspace = V4L2_COLORSPACE_SRGB,
243 static u32 ov5640_code_to_bpp(u32 code)
247 for (i = 0; i < ARRAY_SIZE(ov5640_formats); ++i) {
248 if (ov5640_formats[i].code == code)
249 return ov5640_formats[i].bpp;
256 * FIXME: remove this when a subdev API becomes available
257 * to set the MIPI CSI-2 virtual channel.
259 static unsigned int virtual_channel;
260 module_param(virtual_channel, uint, 0444);
261 MODULE_PARM_DESC(virtual_channel,
262 "MIPI CSI-2 virtual channel (0..3), default 0");
264 static const int ov5640_framerates[] = {
265 [OV5640_15_FPS] = 15,
266 [OV5640_30_FPS] = 30,
267 [OV5640_60_FPS] = 60,
270 /* regulator supplies */
271 static const char * const ov5640_supply_name[] = {
272 "DOVDD", /* Digital I/O (1.8V) supply */
273 "AVDD", /* Analog (2.8V) supply */
274 "DVDD", /* Digital Core (1.5V) supply */
277 #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
280 * Image size under 1280 * 960 are SUBSAMPLING
281 * Image size upper 1280 * 960 are SCALING
283 enum ov5640_downsize_mode {
295 struct ov5640_timings {
296 /* Analog crop rectangle. */
297 struct v4l2_rect analog_crop;
298 /* Visibile crop: from analog crop top-left corner. */
299 struct v4l2_rect crop;
300 /* Total pixels per line: width + fixed hblank. */
302 /* Default vertical blanking: frame height = height + vblank. */
306 struct ov5640_mode_info {
307 enum ov5640_mode_id id;
308 enum ov5640_downsize_mode dn_mode;
309 enum ov5640_pixel_rate_id pixel_rate;
314 struct ov5640_timings dvp_timings;
315 struct ov5640_timings csi2_timings;
317 const struct reg_value *reg_data;
320 /* Used by s_frame_interval only. */
324 struct ov5640_ctrls {
325 struct v4l2_ctrl_handler handler;
326 struct v4l2_ctrl *pixel_rate;
327 struct v4l2_ctrl *link_freq;
328 struct v4l2_ctrl *hblank;
329 struct v4l2_ctrl *vblank;
331 struct v4l2_ctrl *auto_exp;
332 struct v4l2_ctrl *exposure;
335 struct v4l2_ctrl *auto_wb;
336 struct v4l2_ctrl *blue_balance;
337 struct v4l2_ctrl *red_balance;
340 struct v4l2_ctrl *auto_gain;
341 struct v4l2_ctrl *gain;
343 struct v4l2_ctrl *brightness;
344 struct v4l2_ctrl *light_freq;
345 struct v4l2_ctrl *saturation;
346 struct v4l2_ctrl *contrast;
347 struct v4l2_ctrl *hue;
348 struct v4l2_ctrl *test_pattern;
349 struct v4l2_ctrl *hflip;
350 struct v4l2_ctrl *vflip;
354 struct i2c_client *i2c_client;
355 struct v4l2_subdev sd;
356 struct media_pad pad;
357 struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
358 struct clk *xclk; /* system clock to OV5640 */
361 struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
362 struct gpio_desc *reset_gpio;
363 struct gpio_desc *pwdn_gpio;
366 /* lock to protect all members below */
371 struct v4l2_mbus_framefmt fmt;
372 bool pending_fmt_change;
374 const struct ov5640_mode_info *current_mode;
375 const struct ov5640_mode_info *last_mode;
376 enum ov5640_frame_rate current_fr;
377 struct v4l2_fract frame_interval;
378 s64 current_link_freq;
380 struct ov5640_ctrls ctrls;
382 u32 prev_sysclk, prev_hts;
383 u32 ae_low, ae_high, ae_target;
385 bool pending_mode_change;
389 static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
391 return container_of(sd, struct ov5640_dev, sd);
394 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
396 return &container_of(ctrl->handler, struct ov5640_dev,
400 static inline bool ov5640_is_csi2(const struct ov5640_dev *sensor)
402 return sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY;
406 * FIXME: all of these register tables are likely filled with
407 * entries that set the register to their power-on default values,
408 * and which are otherwise not touched by this driver. Those entries
409 * should be identified and removed to speed register load time
412 /* YUV422 UYVY VGA@30fps */
413 static const struct reg_value ov5640_init_setting[] = {
414 {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
415 {0x3103, 0x03, 0, 0}, {0x3630, 0x36, 0, 0},
416 {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
417 {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
418 {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
419 {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
420 {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
421 {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
422 {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
423 {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
424 {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
425 {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
426 {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
427 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
428 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
429 {0x3815, 0x31, 0, 0},
430 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
431 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
432 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
433 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
434 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
435 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
436 {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
437 {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
438 {0x501f, 0x00, 0, 0}, {0x4407, 0x04, 0, 0},
439 {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
440 {0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
441 {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
442 {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
443 {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
444 {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
445 {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
446 {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
447 {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
448 {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
449 {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
450 {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
451 {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
452 {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
453 {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
454 {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
455 {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
456 {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
457 {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
458 {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
459 {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
460 {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
461 {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
462 {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
463 {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
464 {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
465 {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
466 {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
467 {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
468 {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
469 {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
470 {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
471 {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
472 {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
473 {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
474 {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
475 {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
476 {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
477 {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
478 {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
479 {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
480 {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
481 {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
482 {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
483 {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
484 {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
485 {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
486 {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
487 {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
488 {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
489 {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
490 {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
493 static const struct reg_value ov5640_setting_low_res[] = {
494 {0x3c07, 0x08, 0, 0},
495 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
496 {0x3814, 0x31, 0, 0},
497 {0x3815, 0x31, 0, 0},
498 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
499 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
500 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
501 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
502 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
503 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
504 {0x4407, 0x04, 0, 0}, {0x5001, 0xa3, 0, 0},
507 static const struct reg_value ov5640_setting_720P_1280_720[] = {
508 {0x3c07, 0x07, 0, 0},
509 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
510 {0x3814, 0x31, 0, 0},
511 {0x3815, 0x31, 0, 0},
512 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
513 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
514 {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
515 {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
516 {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
517 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
518 {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
519 {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
522 static const struct reg_value ov5640_setting_1080P_1920_1080[] = {
523 {0x3c07, 0x08, 0, 0},
524 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
525 {0x3814, 0x11, 0, 0},
526 {0x3815, 0x11, 0, 0},
527 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
528 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
529 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
530 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
531 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
532 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
533 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
534 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
535 {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
536 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
537 {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
538 {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
539 {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
540 {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
541 {0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0},
542 {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
543 {0x4005, 0x1a, 0, 0},
546 static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = {
547 {0x3c07, 0x08, 0, 0},
548 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
549 {0x3814, 0x11, 0, 0},
550 {0x3815, 0x11, 0, 0},
551 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
552 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 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, 0x06, 0, 0},
557 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
558 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
561 static const struct ov5640_mode_info ov5640_mode_data[OV5640_NUM_MODES] = {
564 .id = OV5640_MODE_QQVGA_160_120,
565 .dn_mode = SUBSAMPLING,
566 .pixel_rate = OV5640_PIXEL_RATE_48M,
586 /* Feed the full valid pixel array to the ISP. */
588 .left = OV5640_PIXEL_ARRAY_LEFT,
589 .top = OV5640_PIXEL_ARRAY_TOP,
590 .width = OV5640_PIXEL_ARRAY_WIDTH,
591 .height = OV5640_PIXEL_ARRAY_HEIGHT,
593 /* Maintain a minimum processing margin. */
603 .reg_data = ov5640_setting_low_res,
604 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
605 .max_fps = OV5640_30_FPS
608 .id = OV5640_MODE_QCIF_176_144,
609 .dn_mode = SUBSAMPLING,
610 .pixel_rate = OV5640_PIXEL_RATE_48M,
630 /* Feed the full valid pixel array to the ISP. */
632 .left = OV5640_PIXEL_ARRAY_LEFT,
633 .top = OV5640_PIXEL_ARRAY_TOP,
634 .width = OV5640_PIXEL_ARRAY_WIDTH,
635 .height = OV5640_PIXEL_ARRAY_HEIGHT,
637 /* Maintain a minimum processing margin. */
647 .reg_data = ov5640_setting_low_res,
648 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
649 .max_fps = OV5640_30_FPS
652 .id = OV5640_MODE_QVGA_320_240,
653 .dn_mode = SUBSAMPLING,
656 .pixel_rate = OV5640_PIXEL_RATE_48M,
674 /* Feed the full valid pixel array to the ISP. */
676 .left = OV5640_PIXEL_ARRAY_LEFT,
677 .top = OV5640_PIXEL_ARRAY_TOP,
678 .width = OV5640_PIXEL_ARRAY_WIDTH,
679 .height = OV5640_PIXEL_ARRAY_HEIGHT,
681 /* Maintain a minimum processing margin. */
691 .reg_data = ov5640_setting_low_res,
692 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
693 .max_fps = OV5640_30_FPS
696 .id = OV5640_MODE_VGA_640_480,
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_60_FPS
740 .id = OV5640_MODE_NTSC_720_480,
741 .dn_mode = SUBSAMPLING,
744 .pixel_rate = OV5640_PIXEL_RATE_96M,
762 /* Feed the full valid pixel array to the ISP. */
764 .left = OV5640_PIXEL_ARRAY_LEFT,
765 .top = OV5640_PIXEL_ARRAY_TOP,
766 .width = OV5640_PIXEL_ARRAY_WIDTH,
767 .height = OV5640_PIXEL_ARRAY_HEIGHT,
778 .reg_data = ov5640_setting_low_res,
779 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
780 .max_fps = OV5640_30_FPS
783 .id = OV5640_MODE_PAL_720_576,
784 .dn_mode = SUBSAMPLING,
787 .pixel_rate = OV5640_PIXEL_RATE_96M,
805 /* Feed the full valid pixel array to the ISP. */
807 .left = OV5640_PIXEL_ARRAY_LEFT,
808 .top = OV5640_PIXEL_ARRAY_TOP,
809 .width = OV5640_PIXEL_ARRAY_WIDTH,
810 .height = OV5640_PIXEL_ARRAY_HEIGHT,
821 .reg_data = ov5640_setting_low_res,
822 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
823 .max_fps = OV5640_30_FPS
826 .id = OV5640_MODE_XGA_1024_768,
827 .dn_mode = SUBSAMPLING,
828 .pixel_rate = OV5640_PIXEL_RATE_96M,
851 .width = OV5640_NATIVE_WIDTH,
852 .height = OV5640_PIXEL_ARRAY_HEIGHT,
863 .reg_data = ov5640_setting_low_res,
864 .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
865 .max_fps = OV5640_30_FPS
868 .id = OV5640_MODE_720P_1280_720,
869 .dn_mode = SUBSAMPLING,
870 .pixel_rate = OV5640_PIXEL_RATE_124M,
905 .reg_data = ov5640_setting_720P_1280_720,
906 .reg_data_size = ARRAY_SIZE(ov5640_setting_720P_1280_720),
907 .max_fps = OV5640_30_FPS
910 .id = OV5640_MODE_1080P_1920_1080,
912 .pixel_rate = OV5640_PIXEL_RATE_148M,
932 /* Crop the full valid pixel array in the center. */
939 /* Maintain a larger processing margins. */
949 .reg_data = ov5640_setting_1080P_1920_1080,
950 .reg_data_size = ARRAY_SIZE(ov5640_setting_1080P_1920_1080),
951 .max_fps = OV5640_30_FPS
954 .id = OV5640_MODE_QSXGA_2592_1944,
956 .pixel_rate = OV5640_PIXEL_RATE_168M,
957 .width = OV5640_PIXEL_ARRAY_WIDTH,
958 .height = OV5640_PIXEL_ARRAY_HEIGHT,
976 /* Give more processing margin to full resolution. */
980 .width = OV5640_NATIVE_WIDTH,
992 .reg_data = ov5640_setting_QSXGA_2592_1944,
993 .reg_data_size = ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944),
994 .max_fps = OV5640_15_FPS
998 static const struct ov5640_timings *
999 ov5640_timings(const struct ov5640_dev *sensor,
1000 const struct ov5640_mode_info *mode)
1002 if (ov5640_is_csi2(sensor))
1003 return &mode->csi2_timings;
1005 return &mode->dvp_timings;
1008 static int ov5640_init_slave_id(struct ov5640_dev *sensor)
1010 struct i2c_client *client = sensor->i2c_client;
1015 if (client->addr == OV5640_DEFAULT_SLAVE_ID)
1018 buf[0] = OV5640_REG_SLAVE_ID >> 8;
1019 buf[1] = OV5640_REG_SLAVE_ID & 0xff;
1020 buf[2] = client->addr << 1;
1022 msg.addr = OV5640_DEFAULT_SLAVE_ID;
1025 msg.len = sizeof(buf);
1027 ret = i2c_transfer(client->adapter, &msg, 1);
1029 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
1036 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
1038 struct i2c_client *client = sensor->i2c_client;
1044 buf[1] = reg & 0xff;
1047 msg.addr = client->addr;
1048 msg.flags = client->flags;
1050 msg.len = sizeof(buf);
1052 ret = i2c_transfer(client->adapter, &msg, 1);
1054 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
1055 __func__, reg, val);
1062 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
1064 struct i2c_client *client = sensor->i2c_client;
1065 struct i2c_msg msg[2];
1070 buf[1] = reg & 0xff;
1072 msg[0].addr = client->addr;
1073 msg[0].flags = client->flags;
1075 msg[0].len = sizeof(buf);
1077 msg[1].addr = client->addr;
1078 msg[1].flags = client->flags | I2C_M_RD;
1082 ret = i2c_transfer(client->adapter, msg, 2);
1084 dev_err(&client->dev, "%s: error: reg=%x\n",
1093 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
1098 ret = ov5640_read_reg(sensor, reg, &hi);
1101 ret = ov5640_read_reg(sensor, reg + 1, &lo);
1105 *val = ((u16)hi << 8) | (u16)lo;
1109 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
1113 ret = ov5640_write_reg(sensor, reg, val >> 8);
1117 return ov5640_write_reg(sensor, reg + 1, val & 0xff);
1120 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
1126 ret = ov5640_read_reg(sensor, reg, &readval);
1134 return ov5640_write_reg(sensor, reg, val);
1138 * After trying the various combinations, reading various
1139 * documentations spread around the net, and from the various
1140 * feedback, the clock tree is probably as follows:
1146 * +->| PLL1 | - reg 0x3036, for the multiplier
1147 * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
1148 * | +--------------+
1149 * +->| System Clock | - reg 0x3035, bits 4-7
1151 * | +--------------+
1152 * +->| MIPI Divider | - reg 0x3035, bits 0-3
1153 * | +-+------------+
1154 * | +----------------> MIPI SCLK
1156 * | +->| / 2 |-------> MIPI BIT CLK
1158 * | +--------------+
1159 * +->| PLL Root Div | - reg 0x3037, bit 4
1162 * +->| Bit Div | - reg 0x3034, bits 0-3
1165 * +->| SCLK Div | - reg 0x3108, bits 0-1
1167 * | +---------------> SCLK
1169 * +->| SCLK 2X Div | - reg 0x3108, bits 2-3
1171 * | +---------------> SCLK 2X
1173 * +->| PCLK Div | - reg 0x3108, bits 4-5
1176 * +->| P_DIV | - reg 0x3035, bits 0-3
1178 * +------------> PCLK
1180 * There seems to be also constraints:
1181 * - the PLL pre-divider output rate should be in the 4-27MHz range
1182 * - the PLL multiplier output rate should be in the 500-1000MHz range
1183 * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
1187 * This is supposed to be ranging from 1 to 8, but the value is always
1188 * set to 3 in the vendor kernels.
1190 #define OV5640_PLL_PREDIV 3
1192 #define OV5640_PLL_MULT_MIN 4
1193 #define OV5640_PLL_MULT_MAX 252
1196 * This is supposed to be ranging from 1 to 16, but the value is
1197 * always set to either 1 or 2 in the vendor kernels.
1199 #define OV5640_SYSDIV_MIN 1
1200 #define OV5640_SYSDIV_MAX 16
1203 * This is supposed to be ranging from 1 to 2, but the value is always
1204 * set to 2 in the vendor kernels.
1206 #define OV5640_PLL_ROOT_DIV 2
1207 #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4)
1210 * We only supports 8-bit formats at the moment
1212 #define OV5640_BIT_DIV 2
1213 #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08
1216 * This is supposed to be ranging from 1 to 8, but the value is always
1217 * set to 2 in the vendor kernels.
1219 #define OV5640_SCLK_ROOT_DIV 2
1222 * This is hardcoded so that the consistency is maintained between SCLK and
1225 #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
1228 * This is supposed to be ranging from 1 to 8, but the value is always
1229 * set to 1 in the vendor kernels.
1231 #define OV5640_PCLK_ROOT_DIV 1
1232 #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00
1234 static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
1235 u8 pll_prediv, u8 pll_mult,
1238 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
1240 /* PLL1 output cannot exceed 1GHz. */
1241 if (sysclk / 1000000 > 1000)
1244 return sysclk / sysdiv;
1247 static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
1249 u8 *pll_prediv, u8 *pll_mult,
1252 unsigned long best = ~0;
1253 u8 best_sysdiv = 1, best_mult = 1;
1254 u8 _sysdiv, _pll_mult;
1256 for (_sysdiv = OV5640_SYSDIV_MIN;
1257 _sysdiv <= OV5640_SYSDIV_MAX;
1259 for (_pll_mult = OV5640_PLL_MULT_MIN;
1260 _pll_mult <= OV5640_PLL_MULT_MAX;
1262 unsigned long _rate;
1265 * The PLL multiplier cannot be odd if above
1268 if (_pll_mult > 127 && (_pll_mult % 2))
1271 _rate = ov5640_compute_sys_clk(sensor,
1273 _pll_mult, _sysdiv);
1276 * We have reached the maximum allowed PLL1 output,
1283 * Prefer rates above the expected clock rate than
1284 * below, even if that means being less precise.
1289 if (abs(rate - _rate) < abs(rate - best)) {
1291 best_sysdiv = _sysdiv;
1292 best_mult = _pll_mult;
1301 *sysdiv = best_sysdiv;
1302 *pll_prediv = OV5640_PLL_PREDIV;
1303 *pll_mult = best_mult;
1309 * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
1310 * for the MIPI CSI-2 output.
1312 static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor)
1314 u8 bit_div, mipi_div, pclk_div, sclk_div, sclk2x_div, root_div;
1315 u8 prediv, mult, sysdiv;
1316 unsigned long link_freq;
1317 unsigned long sysclk;
1323 /* Use the link freq computed at ov5640_update_pixel_rate() time. */
1324 link_freq = sensor->current_link_freq;
1327 * - mipi_div - Additional divider for the MIPI lane clock.
1329 * Higher link frequencies would make sysclk > 1GHz.
1330 * Keep the sysclk low and do not divide in the MIPI domain.
1332 if (link_freq > OV5640_LINK_RATE_MAX)
1337 sysclk = link_freq * mipi_div;
1338 ov5640_calc_sys_clk(sensor, sysclk, &prediv, &mult, &sysdiv);
1341 * Adjust PLL parameters to maintain the MIPI_SCLK-to-PCLK ratio.
1343 * - root_div = 2 (fixed)
1344 * - bit_div : MIPI 8-bit = 2; MIPI 10-bit = 2.5
1345 * - pclk_div = 1 (fixed)
1346 * - p_div = (2 lanes ? mipi_div : 2 * mipi_div)
1348 * This results in the following MIPI_SCLK depending on the number
1351 * - 2 lanes: MIPI_SCLK = (4 or 5) * PCLK
1352 * - 1 lanes: MIPI_SCLK = (8 or 10) * PCLK
1354 root_div = OV5640_PLL_CTRL3_PLL_ROOT_DIV_2;
1355 bit_div = OV5640_PLL_CTRL0_MIPI_MODE_8BIT;
1356 pclk_div = ilog2(OV5640_PCLK_ROOT_DIV);
1360 * - YUV: PCLK >= 2 * SCLK
1361 * - RAW or JPEG: PCLK >= SCLK
1362 * - sclk2x_div = sclk_div / 2
1364 sclk_div = ilog2(OV5640_SCLK_ROOT_DIV);
1365 sclk2x_div = ilog2(OV5640_SCLK2X_ROOT_DIV);
1368 * Set the pixel clock period expressed in ns with 1-bit decimal
1371 * The register is very briefly documented. In the OV5645 datasheet it
1372 * is described as (2 * pclk period), and from testing it seems the
1373 * actual definition is 2 * 8-bit sample period.
1375 * 2 * sample_period = (mipi_clk * 2 * num_lanes / bpp) * (bpp / 8) / 2
1377 num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
1378 sample_rate = (link_freq * mipi_div * num_lanes * 2) / 16;
1379 pclk_period = 2000000000UL / sample_rate;
1381 /* Program the clock tree registers. */
1382 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 0x0f, bit_div);
1386 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0xff,
1387 (sysdiv << 4) | mipi_div);
1391 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
1395 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 0x1f,
1400 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
1401 (pclk_div << 4) | (sclk2x_div << 2) | sclk_div);
1405 return ov5640_write_reg(sensor, OV5640_REG_PCLK_PERIOD, pclk_period);
1408 static u32 ov5640_calc_pixel_rate(struct ov5640_dev *sensor)
1410 const struct ov5640_mode_info *mode = sensor->current_mode;
1411 const struct ov5640_timings *timings = &mode->dvp_timings;
1414 rate = timings->htot * (timings->crop.height + timings->vblank_def);
1415 rate *= ov5640_framerates[sensor->current_fr];
1420 static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
1422 u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
1423 u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
1425 unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
1426 OV5640_PCLK_ROOT_DIV;
1428 _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
1430 *pll_rdiv = OV5640_PLL_ROOT_DIV;
1431 *bit_div = OV5640_BIT_DIV;
1432 *pclk_div = OV5640_PCLK_ROOT_DIV;
1434 return _rate / *pll_rdiv / *bit_div / *pclk_div;
1437 static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor)
1439 u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1443 rate = ov5640_calc_pixel_rate(sensor);
1444 rate *= ov5640_code_to_bpp(sensor->fmt.code);
1445 rate /= sensor->ep.bus.parallel.bus_width;
1447 ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1448 &bit_div, &pclk_div);
1453 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1459 * We need to set sysdiv according to the clock, and to clear
1462 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1467 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1472 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1473 0x1f, prediv | ((pll_rdiv - 1) << 4));
1477 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1478 (ilog2(pclk_div) << 4));
1481 /* set JPEG framing sizes */
1482 static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor,
1483 const struct ov5640_mode_info *mode)
1488 * compression mode 3 timing
1490 * Data is transmitted with programmable width (VFIFO_HSIZE).
1491 * No padding done. Last line may have less data. Varying
1492 * number of lines per frame, depending on amount of data.
1494 ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3);
1498 ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->width);
1502 return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->height);
1505 /* download ov5640 settings to sensor through i2c */
1506 static int ov5640_set_timings(struct ov5640_dev *sensor,
1507 const struct ov5640_mode_info *mode)
1509 const struct ov5640_timings *timings;
1510 const struct v4l2_rect *analog_crop;
1511 const struct v4l2_rect *crop;
1514 if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) {
1515 ret = ov5640_set_jpeg_timings(sensor, mode);
1520 timings = ov5640_timings(sensor, mode);
1521 analog_crop = &timings->analog_crop;
1522 crop = &timings->crop;
1524 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HS,
1529 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VS,
1534 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HW,
1535 analog_crop->left + analog_crop->width - 1);
1539 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VH,
1540 analog_crop->top + analog_crop->height - 1);
1544 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HOFFS, crop->left);
1548 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VOFFS, crop->top);
1552 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->width);
1556 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->height);
1560 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, timings->htot);
1564 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
1565 mode->height + timings->vblank_def);
1572 static void ov5640_load_regs(struct ov5640_dev *sensor,
1573 const struct reg_value *regs, unsigned int regnum)
1581 for (i = 0; i < regnum; ++i, ++regs) {
1582 delay_ms = regs->delay_ms;
1583 reg_addr = regs->reg_addr;
1587 /* remain in power down mode for DVP */
1588 if (regs->reg_addr == OV5640_REG_SYS_CTRL0 &&
1589 val == OV5640_REG_SYS_CTRL0_SW_PWUP &&
1590 !ov5640_is_csi2(sensor))
1594 ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
1596 ret = ov5640_write_reg(sensor, reg_addr, val);
1601 usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
1605 static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1607 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1608 BIT(0), on ? 0 : BIT(0));
1611 /* read exposure, in number of line periods */
1612 static int ov5640_get_exposure(struct ov5640_dev *sensor)
1617 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
1620 exp = ((int)temp & 0x0f) << 16;
1621 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
1624 exp |= ((int)temp << 8);
1625 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
1633 /* write exposure, given number of line periods */
1634 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
1640 ret = ov5640_write_reg(sensor,
1641 OV5640_REG_AEC_PK_EXPOSURE_LO,
1645 ret = ov5640_write_reg(sensor,
1646 OV5640_REG_AEC_PK_EXPOSURE_MED,
1647 (exposure >> 8) & 0xff);
1650 return ov5640_write_reg(sensor,
1651 OV5640_REG_AEC_PK_EXPOSURE_HI,
1652 (exposure >> 16) & 0x0f);
1655 static int ov5640_get_gain(struct ov5640_dev *sensor)
1660 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1664 return gain & 0x3ff;
1667 static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
1669 return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1673 static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
1675 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1676 BIT(1), on ? 0 : BIT(1));
1679 static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1681 return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ?
1682 OV5640_REG_SYS_CTRL0_SW_PWUP :
1683 OV5640_REG_SYS_CTRL0_SW_PWDN);
1686 static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
1691 * Enable/disable the MIPI interface
1693 * 0x300e = on ? 0x45 : 0x40
1695 * FIXME: the sensor manual (version 2.03) reports
1696 * [7:5] = 000 : 1 data lane mode
1697 * [7:5] = 001 : 2 data lanes mode
1698 * But this settings do not work, while the following ones
1699 * have been validated for 2 data lanes mode.
1701 * [7:5] = 010 : 2 data lanes mode
1702 * [4] = 0 : Power up MIPI HS Tx
1703 * [3] = 0 : Power up MIPI LS Rx
1704 * [2] = 1/0 : MIPI interface enable/disable
1705 * [1:0] = 01/00: FIXME: 'debug'
1707 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1712 return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1716 static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1718 /* calculate sysclk */
1719 u32 xvclk = sensor->xclk_freq / 10000;
1720 u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1721 u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1722 u32 bit_div2x = 1, sclk_rdiv, sysclk;
1726 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1729 temp2 = temp1 & 0x0f;
1730 if (temp2 == 8 || temp2 == 10)
1731 bit_div2x = temp2 / 2;
1733 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1736 sysdiv = temp1 >> 4;
1740 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1745 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1748 prediv = temp1 & 0x0f;
1749 pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1751 ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1754 temp2 = temp1 & 0x03;
1755 sclk_rdiv = sclk_rdiv_map[temp2];
1757 if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1760 VCO = xvclk * multiplier / prediv;
1762 sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1767 static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1769 /* read HTS from register settings */
1773 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1777 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1780 static int ov5640_get_hts(struct ov5640_dev *sensor)
1782 /* read HTS from register settings */
1786 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1792 static int ov5640_get_vts(struct ov5640_dev *sensor)
1797 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1803 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1805 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1808 static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1810 /* get banding filter value */
1811 int ret, light_freq = 0;
1814 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1820 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1833 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1849 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1851 u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1854 /* read preview PCLK */
1855 ret = ov5640_get_sysclk(sensor);
1860 sensor->prev_sysclk = ret;
1861 /* read preview HTS */
1862 ret = ov5640_get_hts(sensor);
1867 sensor->prev_hts = ret;
1869 /* read preview VTS */
1870 ret = ov5640_get_vts(sensor);
1875 /* calculate banding filter */
1877 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
1878 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
1883 max_band60 = (int)((prev_vts - 4) / band_step60);
1884 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
1889 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
1890 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
1895 max_band50 = (int)((prev_vts - 4) / band_step50);
1896 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
1899 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
1901 /* stable in high */
1902 u32 fast_high, fast_low;
1905 sensor->ae_low = target * 23 / 25; /* 0.92 */
1906 sensor->ae_high = target * 27 / 25; /* 1.08 */
1908 fast_high = sensor->ae_high << 1;
1909 if (fast_high > 255)
1912 fast_low = sensor->ae_low >> 1;
1914 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
1917 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
1920 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
1923 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
1926 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
1929 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
1932 static int ov5640_get_binning(struct ov5640_dev *sensor)
1937 ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
1941 return temp & BIT(0);
1944 static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
1950 * - [0]: Horizontal binning enable
1952 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1953 BIT(0), enable ? BIT(0) : 0);
1958 * - [0]: Undocumented, but hardcoded init sequences
1959 * are always setting REG21/REG20 bit 0 to same value...
1961 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
1962 BIT(0), enable ? BIT(0) : 0);
1965 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
1967 struct i2c_client *client = sensor->i2c_client;
1968 u8 temp, channel = virtual_channel;
1972 dev_err(&client->dev,
1973 "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
1978 ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
1982 temp |= (channel << 6);
1983 return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
1986 static const struct ov5640_mode_info *
1987 ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
1988 int width, int height, bool nearest)
1990 const struct ov5640_mode_info *mode;
1992 mode = v4l2_find_nearest_size(ov5640_mode_data,
1993 ARRAY_SIZE(ov5640_mode_data),
1994 width, height, width, height);
1998 (mode->width != width || mode->height != height)))
2001 /* Check to see if the current mode exceeds the max frame rate */
2002 if (ov5640_framerates[fr] > ov5640_framerates[mode->max_fps])
2009 * sensor changes between scaling and subsampling, go through
2010 * exposure calculation
2012 static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
2013 const struct ov5640_mode_info *mode)
2015 u32 prev_shutter, prev_gain16;
2016 u32 cap_shutter, cap_gain16;
2017 u32 cap_sysclk, cap_hts, cap_vts;
2018 u32 light_freq, cap_bandfilt, cap_maxband;
2019 u32 cap_gain16_shutter;
2023 if (!mode->reg_data)
2026 /* read preview shutter */
2027 ret = ov5640_get_exposure(sensor);
2031 ret = ov5640_get_binning(sensor);
2034 if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
2035 mode->id != OV5640_MODE_1080P_1920_1080)
2038 /* read preview gain */
2039 ret = ov5640_get_gain(sensor);
2045 ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
2049 /* turn off night mode for capture */
2050 ret = ov5640_set_night_mode(sensor);
2054 /* Write capture setting */
2055 ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
2056 ret = ov5640_set_timings(sensor, mode);
2060 /* read capture VTS */
2061 ret = ov5640_get_vts(sensor);
2065 ret = ov5640_get_hts(sensor);
2072 ret = ov5640_get_sysclk(sensor);
2079 /* calculate capture banding filter */
2080 ret = ov5640_get_light_freq(sensor);
2085 if (light_freq == 60) {
2087 cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
2090 cap_bandfilt = cap_sysclk * 100 / cap_hts;
2093 if (!sensor->prev_sysclk) {
2094 ret = ov5640_get_sysclk(sensor);
2099 sensor->prev_sysclk = ret;
2105 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
2107 /* calculate capture shutter/gain16 */
2108 if (average > sensor->ae_low && average < sensor->ae_high) {
2109 /* in stable range */
2110 cap_gain16_shutter =
2111 prev_gain16 * prev_shutter *
2112 cap_sysclk / sensor->prev_sysclk *
2113 sensor->prev_hts / cap_hts *
2114 sensor->ae_target / average;
2116 cap_gain16_shutter =
2117 prev_gain16 * prev_shutter *
2118 cap_sysclk / sensor->prev_sysclk *
2119 sensor->prev_hts / cap_hts;
2122 /* gain to shutter */
2123 if (cap_gain16_shutter < (cap_bandfilt * 16)) {
2124 /* shutter < 1/100 */
2125 cap_shutter = cap_gain16_shutter / 16;
2126 if (cap_shutter < 1)
2129 cap_gain16 = cap_gain16_shutter / cap_shutter;
2130 if (cap_gain16 < 16)
2133 if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
2134 /* exposure reach max */
2135 cap_shutter = cap_bandfilt * cap_maxband;
2139 cap_gain16 = cap_gain16_shutter / cap_shutter;
2141 /* 1/100 < (cap_shutter = n/100) =< max */
2143 ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
2148 cap_gain16 = cap_gain16_shutter / cap_shutter;
2152 /* set capture gain */
2153 ret = ov5640_set_gain(sensor, cap_gain16);
2157 /* write capture shutter */
2158 if (cap_shutter > (cap_vts - 4)) {
2159 cap_vts = cap_shutter + 4;
2160 ret = ov5640_set_vts(sensor, cap_vts);
2166 return ov5640_set_exposure(sensor, cap_shutter);
2170 * if sensor changes inside scaling or subsampling
2171 * change mode directly
2173 static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
2174 const struct ov5640_mode_info *mode)
2176 if (!mode->reg_data)
2179 /* Write capture setting */
2180 ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
2181 return ov5640_set_timings(sensor, mode);
2184 static int ov5640_set_mode(struct ov5640_dev *sensor)
2186 const struct ov5640_mode_info *mode = sensor->current_mode;
2187 const struct ov5640_mode_info *orig_mode = sensor->last_mode;
2188 enum ov5640_downsize_mode dn_mode, orig_dn_mode;
2189 bool auto_gain = sensor->ctrls.auto_gain->val == 1;
2190 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
2193 dn_mode = mode->dn_mode;
2194 orig_dn_mode = orig_mode->dn_mode;
2196 /* auto gain and exposure must be turned off when changing modes */
2198 ret = ov5640_set_autogain(sensor, false);
2204 ret = ov5640_set_autoexposure(sensor, false);
2206 goto restore_auto_gain;
2209 if (ov5640_is_csi2(sensor))
2210 ret = ov5640_set_mipi_pclk(sensor);
2212 ret = ov5640_set_dvp_pclk(sensor);
2216 if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
2217 (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
2219 * change between subsampling and scaling
2220 * go through exposure calculation
2222 ret = ov5640_set_mode_exposure_calc(sensor, mode);
2225 * change inside subsampling or scaling
2226 * download firmware directly
2228 ret = ov5640_set_mode_direct(sensor, mode);
2231 goto restore_auto_exp_gain;
2233 /* restore auto gain and exposure */
2235 ov5640_set_autogain(sensor, true);
2237 ov5640_set_autoexposure(sensor, true);
2239 ret = ov5640_set_binning(sensor, dn_mode != SCALING);
2242 ret = ov5640_set_ae_target(sensor, sensor->ae_target);
2245 ret = ov5640_get_light_freq(sensor);
2248 ret = ov5640_set_bandingfilter(sensor);
2251 ret = ov5640_set_virtual_channel(sensor);
2255 sensor->pending_mode_change = false;
2256 sensor->last_mode = mode;
2260 restore_auto_exp_gain:
2262 ov5640_set_autoexposure(sensor, true);
2265 ov5640_set_autogain(sensor, true);
2270 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2271 struct v4l2_mbus_framefmt *format);
2273 /* restore the last set video mode after chip power-on */
2274 static int ov5640_restore_mode(struct ov5640_dev *sensor)
2278 /* first load the initial register values */
2279 ov5640_load_regs(sensor, ov5640_init_setting,
2280 ARRAY_SIZE(ov5640_init_setting));
2282 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
2283 (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) |
2284 ilog2(OV5640_SCLK_ROOT_DIV));
2288 /* now restore the last capture mode */
2289 ret = ov5640_set_mode(sensor);
2293 return ov5640_set_framefmt(sensor, &sensor->fmt);
2296 static void ov5640_power(struct ov5640_dev *sensor, bool enable)
2298 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
2301 static void ov5640_reset(struct ov5640_dev *sensor)
2303 if (!sensor->reset_gpio)
2306 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2308 /* camera power cycle */
2309 ov5640_power(sensor, false);
2310 usleep_range(5000, 10000);
2311 ov5640_power(sensor, true);
2312 usleep_range(5000, 10000);
2314 gpiod_set_value_cansleep(sensor->reset_gpio, 1);
2315 usleep_range(1000, 2000);
2317 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2318 usleep_range(20000, 25000);
2321 static int ov5640_set_power_on(struct ov5640_dev *sensor)
2323 struct i2c_client *client = sensor->i2c_client;
2326 ret = clk_prepare_enable(sensor->xclk);
2328 dev_err(&client->dev, "%s: failed to enable clock\n",
2333 ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
2336 dev_err(&client->dev, "%s: failed to enable regulators\n",
2341 ov5640_reset(sensor);
2342 ov5640_power(sensor, true);
2344 ret = ov5640_init_slave_id(sensor);
2351 ov5640_power(sensor, false);
2352 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2354 clk_disable_unprepare(sensor->xclk);
2358 static void ov5640_set_power_off(struct ov5640_dev *sensor)
2360 ov5640_power(sensor, false);
2361 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2362 clk_disable_unprepare(sensor->xclk);
2365 static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on)
2370 /* Reset MIPI bus settings to their default values. */
2371 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2372 ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04);
2373 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00);
2378 * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
2381 * [7:5] = 010 : 2 data lanes mode (see FIXME note in
2382 * "ov5640_set_stream_mipi()")
2383 * [4] = 0 : Power up MIPI HS Tx
2384 * [3] = 0 : Power up MIPI LS Rx
2385 * [2] = 0 : MIPI interface disabled
2387 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x40);
2392 * Gate clock and set LP11 in 'no packets mode' (idle)
2395 * [5] = 1 : Gate clock when 'no packets'
2396 * [2] = 1 : MIPI bus in LP11 when 'no packets'
2398 ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24);
2403 * Set data lanes and clock in LP11 when 'sleeping'
2406 * [6] = 1 : MIPI data lane 2 in LP11 when 'sleeping'
2407 * [5] = 1 : MIPI data lane 1 in LP11 when 'sleeping'
2408 * [4] = 1 : MIPI clock lane in LP11 when 'sleeping'
2410 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70);
2414 /* Give lanes some time to coax into LP11 state. */
2415 usleep_range(500, 1000);
2420 static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on)
2422 unsigned int flags = sensor->ep.bus.parallel.flags;
2423 bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656;
2428 /* Reset settings to their default values. */
2429 ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00);
2430 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2431 ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20);
2432 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00);
2433 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00);
2438 * Note about parallel port configuration.
2440 * When configured in parallel mode, the OV5640 will
2441 * output 10 bits data on DVP data lines [9:0].
2442 * If only 8 bits data are wanted, the 8 bits data lines
2443 * of the camera interface must be physically connected
2444 * on the DVP data lines [9:2].
2446 * Control lines polarity can be configured through
2447 * devicetree endpoint control lines properties.
2448 * If no endpoint control lines properties are set,
2449 * polarity will be as below:
2450 * - VSYNC: active high
2451 * - HREF: active low
2452 * - PCLK: active low
2454 * VSYNC & HREF are not configured if BT656 bus mode is selected
2458 * BT656 embedded synchronization configuration
2461 * - [7]: SYNC code selection (0: auto generate sync code,
2462 * 1: sync code from regs 0x4732-0x4735)
2463 * - [6]: f value in CCIR656 SYNC code when fixed f value
2464 * - [5]: Fixed f value
2465 * - [4:3]: Blank toggle data options (00: data=1'h040/1'h200,
2466 * 01: data from regs 0x4736-0x4738, 10: always keep 0)
2467 * - [1]: Clip data disable
2468 * - [0]: CCIR656 mode enable
2470 * Default CCIR656 SAV/EAV mode with default codes
2471 * SAV=0xff000080 & EAV=0xff00009d is enabled here with settings:
2472 * - CCIR656 mode enable
2473 * - auto generation of sync codes
2474 * - blank toggle data 1'h040/1'h200
2475 * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe)
2477 ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00,
2478 bt656 ? 0x01 : 0x00);
2483 * configure parallel port control lines polarity
2486 * - [5]: PCLK polarity (0: active low, 1: active high)
2487 * - [1]: HREF polarity (0: active low, 1: active high)
2488 * - [0]: VSYNC polarity (mismatch here between
2489 * datasheet and hardware, 0 is active high
2490 * and 1 is active low...)
2493 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2494 polarities |= BIT(1);
2495 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
2496 polarities |= BIT(0);
2498 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
2499 polarities |= BIT(5);
2501 ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities);
2506 * powerdown MIPI TX/RX PHY & enable DVP
2509 * [4] = 1 : Power down MIPI HS Tx
2510 * [3] = 1 : Power down MIPI LS Rx
2511 * [2] = 0 : DVP enable (MIPI disable)
2513 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18);
2518 * enable VSYNC/HREF/PCLK DVP control lines
2519 * & D[9:6] DVP data lines
2521 * PAD OUTPUT ENABLE 01
2522 * - 6: VSYNC output enable
2523 * - 5: HREF output enable
2524 * - 4: PCLK output enable
2525 * - [3:0]: D[9:6] output enable
2527 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01,
2528 bt656 ? 0x1f : 0x7f);
2533 * enable D[5:0] DVP data lines
2535 * PAD OUTPUT ENABLE 02
2536 * - [7:2]: D[5:0] output enable
2538 return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc);
2541 static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
2546 ret = ov5640_set_power_on(sensor);
2550 ret = ov5640_restore_mode(sensor);
2555 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2556 ret = ov5640_set_power_mipi(sensor, on);
2558 ret = ov5640_set_power_dvp(sensor, on);
2563 ov5640_set_power_off(sensor);
2568 ov5640_set_power_off(sensor);
2572 /* --------------- Subdev Operations --------------- */
2574 static int ov5640_s_power(struct v4l2_subdev *sd, int on)
2576 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2579 mutex_lock(&sensor->lock);
2582 * If the power count is modified from 0 to != 0 or from != 0 to 0,
2583 * update the power state.
2585 if (sensor->power_count == !on) {
2586 ret = ov5640_set_power(sensor, !!on);
2591 /* Update the power count. */
2592 sensor->power_count += on ? 1 : -1;
2593 WARN_ON(sensor->power_count < 0);
2595 mutex_unlock(&sensor->lock);
2597 if (on && !ret && sensor->power_count == 1) {
2598 /* restore controls */
2599 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
2605 static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
2606 struct v4l2_fract *fi,
2607 u32 width, u32 height)
2609 const struct ov5640_mode_info *mode;
2610 enum ov5640_frame_rate rate = OV5640_15_FPS;
2611 int minfps, maxfps, best_fps, fps;
2614 minfps = ov5640_framerates[OV5640_15_FPS];
2615 maxfps = ov5640_framerates[OV5640_60_FPS];
2617 if (fi->numerator == 0) {
2618 fi->denominator = maxfps;
2620 rate = OV5640_60_FPS;
2624 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
2628 for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) {
2629 int curr_fps = ov5640_framerates[i];
2631 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
2632 best_fps = curr_fps;
2638 fi->denominator = best_fps;
2641 mode = ov5640_find_mode(sensor, rate, width, height, false);
2642 return mode ? rate : -EINVAL;
2645 static int ov5640_get_fmt(struct v4l2_subdev *sd,
2646 struct v4l2_subdev_state *sd_state,
2647 struct v4l2_subdev_format *format)
2649 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2650 struct v4l2_mbus_framefmt *fmt;
2652 if (format->pad != 0)
2655 mutex_lock(&sensor->lock);
2657 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2658 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
2663 format->format = *fmt;
2665 mutex_unlock(&sensor->lock);
2670 static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
2671 struct v4l2_mbus_framefmt *fmt,
2672 enum ov5640_frame_rate fr,
2673 const struct ov5640_mode_info **new_mode)
2675 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2676 const struct ov5640_mode_info *mode;
2679 mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
2682 fmt->width = mode->width;
2683 fmt->height = mode->height;
2688 for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
2689 if (ov5640_formats[i].code == fmt->code)
2691 if (i >= ARRAY_SIZE(ov5640_formats))
2694 fmt->code = ov5640_formats[i].code;
2695 fmt->colorspace = ov5640_formats[i].colorspace;
2696 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2697 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2698 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2703 static int ov5640_update_pixel_rate(struct ov5640_dev *sensor)
2705 const struct ov5640_mode_info *mode = sensor->current_mode;
2706 enum ov5640_pixel_rate_id pixel_rate_id = mode->pixel_rate;
2707 struct v4l2_mbus_framefmt *fmt = &sensor->fmt;
2708 const struct ov5640_timings *timings;
2709 s32 exposure_val, exposure_max;
2710 unsigned int hblank;
2718 * Update the pixel rate control value.
2720 * For DVP mode, maintain the pixel rate calculation using fixed FPS.
2722 if (!ov5640_is_csi2(sensor)) {
2723 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2724 ov5640_calc_pixel_rate(sensor));
2730 * The MIPI CSI-2 link frequency should comply with the CSI-2
2731 * specification and be lower than 1GHz.
2733 * Start from the suggested pixel_rate for the current mode and
2734 * progressively slow it down if it exceeds 1GHz.
2736 num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
2737 bpp = ov5640_code_to_bpp(fmt->code);
2739 pixel_rate = ov5640_pixel_rates[pixel_rate_id];
2740 link_freq = pixel_rate * bpp / (2 * num_lanes);
2741 } while (link_freq >= 1000000000U &&
2742 ++pixel_rate_id < OV5640_NUM_PIXEL_RATES);
2744 sensor->current_link_freq = link_freq;
2747 * Higher link rates require the clock tree to be programmed with
2748 * 'mipi_div' = 1; this has the effect of halving the actual output
2749 * pixel rate in the MIPI domain.
2751 * Adjust the pixel rate and link frequency control value to report it
2752 * correctly to userspace.
2754 if (link_freq > OV5640_LINK_RATE_MAX) {
2759 for (i = 0; i < ARRAY_SIZE(ov5640_csi2_link_freqs); ++i) {
2760 if (ov5640_csi2_link_freqs[i] == link_freq)
2763 WARN_ON(i == ARRAY_SIZE(ov5640_csi2_link_freqs));
2765 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, pixel_rate);
2766 __v4l2_ctrl_s_ctrl(sensor->ctrls.link_freq, i);
2768 timings = ov5640_timings(sensor, mode);
2769 hblank = timings->htot - mode->width;
2770 __v4l2_ctrl_modify_range(sensor->ctrls.hblank,
2771 hblank, hblank, 1, hblank);
2773 __v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV5640_MIN_VBLANK,
2774 OV5640_MAX_VTS - mode->height, 1,
2775 timings->vblank_def);
2776 __v4l2_ctrl_s_ctrl(sensor->ctrls.vblank, timings->vblank_def);
2778 exposure_max = timings->crop.height + timings->vblank_def - 4;
2779 exposure_val = clamp_t(s32, sensor->ctrls.exposure->val,
2780 sensor->ctrls.exposure->minimum,
2782 __v4l2_ctrl_modify_range(sensor->ctrls.exposure,
2783 sensor->ctrls.exposure->minimum,
2784 exposure_max, 1, exposure_val);
2789 static int ov5640_set_fmt(struct v4l2_subdev *sd,
2790 struct v4l2_subdev_state *sd_state,
2791 struct v4l2_subdev_format *format)
2793 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2794 const struct ov5640_mode_info *new_mode;
2795 struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
2798 if (format->pad != 0)
2801 mutex_lock(&sensor->lock);
2803 if (sensor->streaming) {
2808 ret = ov5640_try_fmt_internal(sd, mbus_fmt,
2809 sensor->current_fr, &new_mode);
2813 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2814 *v4l2_subdev_get_try_format(sd, sd_state, 0) = *mbus_fmt;
2818 if (new_mode != sensor->current_mode) {
2819 sensor->current_mode = new_mode;
2820 sensor->pending_mode_change = true;
2822 if (mbus_fmt->code != sensor->fmt.code)
2823 sensor->pending_fmt_change = true;
2825 /* update format even if code is unchanged, resolution might change */
2826 sensor->fmt = *mbus_fmt;
2828 ov5640_update_pixel_rate(sensor);
2831 mutex_unlock(&sensor->lock);
2835 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2836 struct v4l2_mbus_framefmt *format)
2839 bool is_jpeg = false;
2842 switch (format->code) {
2843 case MEDIA_BUS_FMT_UYVY8_1X16:
2844 case MEDIA_BUS_FMT_UYVY8_2X8:
2847 mux = OV5640_FMT_MUX_YUV422;
2849 case MEDIA_BUS_FMT_YUYV8_1X16:
2850 case MEDIA_BUS_FMT_YUYV8_2X8:
2853 mux = OV5640_FMT_MUX_YUV422;
2855 case MEDIA_BUS_FMT_RGB565_2X8_LE:
2856 /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
2858 mux = OV5640_FMT_MUX_RGB;
2860 case MEDIA_BUS_FMT_RGB565_2X8_BE:
2861 /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
2863 mux = OV5640_FMT_MUX_RGB;
2865 case MEDIA_BUS_FMT_JPEG_1X8:
2868 mux = OV5640_FMT_MUX_YUV422;
2871 case MEDIA_BUS_FMT_SBGGR8_1X8:
2872 /* Raw, BGBG... / GRGR... */
2874 mux = OV5640_FMT_MUX_RAW_DPC;
2876 case MEDIA_BUS_FMT_SGBRG8_1X8:
2877 /* Raw bayer, GBGB... / RGRG... */
2879 mux = OV5640_FMT_MUX_RAW_DPC;
2881 case MEDIA_BUS_FMT_SGRBG8_1X8:
2882 /* Raw bayer, GRGR... / BGBG... */
2884 mux = OV5640_FMT_MUX_RAW_DPC;
2886 case MEDIA_BUS_FMT_SRGGB8_1X8:
2887 /* Raw bayer, RGRG... / GBGB... */
2889 mux = OV5640_FMT_MUX_RAW_DPC;
2895 /* FORMAT CONTROL00: YUV and RGB formatting */
2896 ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, fmt);
2900 /* FORMAT MUX CONTROL: ISP YUV or RGB */
2901 ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL, mux);
2907 * - [5]: JPEG enable
2909 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2910 BIT(5), is_jpeg ? BIT(5) : 0);
2916 * - [4]: Reset JFIFO
2917 * - [3]: Reset SFIFO
2920 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
2921 BIT(4) | BIT(3) | BIT(2),
2922 is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2928 * - [5]: Enable JPEG 2x clock
2929 * - [3]: Enable JPEG clock
2931 return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2933 is_jpeg ? (BIT(5) | BIT(3)) : 0);
2940 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
2945 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2949 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
2951 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
2957 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
2962 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2966 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
2969 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
2975 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
2980 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2984 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
2988 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
2991 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
2997 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
3001 ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
3002 BIT(0), awb ? 0 : 1);
3007 u16 red = (u16)sensor->ctrls.red_balance->val;
3008 u16 blue = (u16)sensor->ctrls.blue_balance->val;
3010 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
3013 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
3019 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
3020 enum v4l2_exposure_auto_type auto_exposure)
3022 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3023 bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
3026 if (ctrls->auto_exp->is_new) {
3027 ret = ov5640_set_autoexposure(sensor, auto_exp);
3032 if (!auto_exp && ctrls->exposure->is_new) {
3035 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
3039 ret = ov5640_get_vts(sensor);
3045 if (ctrls->exposure->val < max_exp)
3046 ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
3052 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
3054 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3057 if (ctrls->auto_gain->is_new) {
3058 ret = ov5640_set_autogain(sensor, auto_gain);
3063 if (!auto_gain && ctrls->gain->is_new)
3064 ret = ov5640_set_gain(sensor, ctrls->gain->val);
3069 static const char * const test_pattern_menu[] = {
3072 "Color bars w/ rolling bar",
3074 "Color squares w/ rolling bar",
3077 #define OV5640_TEST_ENABLE BIT(7)
3078 #define OV5640_TEST_ROLLING BIT(6) /* rolling horizontal bar */
3079 #define OV5640_TEST_TRANSPARENT BIT(5)
3080 #define OV5640_TEST_SQUARE_BW BIT(4) /* black & white squares */
3081 #define OV5640_TEST_BAR_STANDARD (0 << 2)
3082 #define OV5640_TEST_BAR_VERT_CHANGE_1 (1 << 2)
3083 #define OV5640_TEST_BAR_HOR_CHANGE (2 << 2)
3084 #define OV5640_TEST_BAR_VERT_CHANGE_2 (3 << 2)
3085 #define OV5640_TEST_BAR (0 << 0)
3086 #define OV5640_TEST_RANDOM (1 << 0)
3087 #define OV5640_TEST_SQUARE (2 << 0)
3088 #define OV5640_TEST_BLACK (3 << 0)
3090 static const u8 test_pattern_val[] = {
3092 OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 |
3094 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING |
3095 OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR,
3096 OV5640_TEST_ENABLE | OV5640_TEST_SQUARE,
3097 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE,
3100 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
3102 return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
3103 test_pattern_val[value]);
3106 static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
3110 ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
3111 (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
3116 return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
3117 (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
3121 static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
3124 * If sensor is mounted upside down, mirror logic is inversed.
3126 * Sensor is a BSI (Back Side Illuminated) one,
3127 * so image captured is physically mirrored.
3128 * This is why mirror logic is inversed in
3129 * order to cancel this mirror effect.
3135 * - [1]: Sensor mirror
3137 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
3139 (!(value ^ sensor->upside_down)) ?
3140 (BIT(2) | BIT(1)) : 0);
3143 static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
3145 /* If sensor is mounted upside down, flip logic is inversed */
3150 * - [1]: Sensor vflip
3152 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
3154 (value ^ sensor->upside_down) ?
3155 (BIT(2) | BIT(1)) : 0);
3158 static int ov5640_set_ctrl_vblank(struct ov5640_dev *sensor, int value)
3160 const struct ov5640_mode_info *mode = sensor->current_mode;
3162 /* Update the VTOT timing register value. */
3163 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
3164 mode->height + value);
3167 static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
3169 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3170 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3173 /* v4l2_ctrl_lock() locks our own mutex */
3176 case V4L2_CID_AUTOGAIN:
3177 val = ov5640_get_gain(sensor);
3180 sensor->ctrls.gain->val = val;
3182 case V4L2_CID_EXPOSURE_AUTO:
3183 val = ov5640_get_exposure(sensor);
3186 sensor->ctrls.exposure->val = val;
3193 static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
3195 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3196 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3197 const struct ov5640_mode_info *mode = sensor->current_mode;
3198 const struct ov5640_timings *timings;
3199 unsigned int exp_max;
3202 /* v4l2_ctrl_lock() locks our own mutex */
3205 case V4L2_CID_VBLANK:
3206 /* Update the exposure range to the newly programmed vblank. */
3207 timings = ov5640_timings(sensor, mode);
3208 exp_max = mode->height + ctrl->val - 4;
3209 __v4l2_ctrl_modify_range(sensor->ctrls.exposure,
3210 sensor->ctrls.exposure->minimum,
3211 exp_max, sensor->ctrls.exposure->step,
3212 timings->vblank_def);
3217 * If the device is not powered up by the host driver do
3218 * not apply any controls to H/W at this time. Instead
3219 * the controls will be restored right after power-up.
3221 if (sensor->power_count == 0)
3225 case V4L2_CID_AUTOGAIN:
3226 ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
3228 case V4L2_CID_EXPOSURE_AUTO:
3229 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
3231 case V4L2_CID_AUTO_WHITE_BALANCE:
3232 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
3235 ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
3237 case V4L2_CID_CONTRAST:
3238 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
3240 case V4L2_CID_SATURATION:
3241 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
3243 case V4L2_CID_TEST_PATTERN:
3244 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
3246 case V4L2_CID_POWER_LINE_FREQUENCY:
3247 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
3249 case V4L2_CID_HFLIP:
3250 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
3252 case V4L2_CID_VFLIP:
3253 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
3255 case V4L2_CID_VBLANK:
3256 ret = ov5640_set_ctrl_vblank(sensor, ctrl->val);
3266 static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
3267 .g_volatile_ctrl = ov5640_g_volatile_ctrl,
3268 .s_ctrl = ov5640_s_ctrl,
3271 static int ov5640_init_controls(struct ov5640_dev *sensor)
3273 const struct ov5640_mode_info *mode = sensor->current_mode;
3274 const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
3275 struct ov5640_ctrls *ctrls = &sensor->ctrls;
3276 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
3277 const struct ov5640_timings *timings;
3278 unsigned int max_vblank;
3279 unsigned int hblank;
3282 v4l2_ctrl_handler_init(hdl, 32);
3284 /* we can use our own mutex for the ctrl lock */
3285 hdl->lock = &sensor->lock;
3287 /* Clock related controls */
3288 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
3289 ov5640_pixel_rates[OV5640_NUM_PIXEL_RATES - 1],
3290 ov5640_pixel_rates[0], 1,
3291 ov5640_pixel_rates[mode->pixel_rate]);
3293 ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, ops,
3295 ARRAY_SIZE(ov5640_csi2_link_freqs) - 1,
3296 OV5640_DEFAULT_LINK_FREQ,
3297 ov5640_csi2_link_freqs);
3299 timings = ov5640_timings(sensor, mode);
3300 hblank = timings->htot - mode->width;
3301 ctrls->hblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HBLANK, hblank,
3304 max_vblank = OV5640_MAX_VTS - mode->height;
3305 ctrls->vblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VBLANK,
3306 OV5640_MIN_VBLANK, max_vblank,
3307 1, timings->vblank_def);
3309 /* Auto/manual white balance */
3310 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
3311 V4L2_CID_AUTO_WHITE_BALANCE,
3313 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
3315 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
3317 /* Auto/manual exposure */
3318 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
3319 V4L2_CID_EXPOSURE_AUTO,
3320 V4L2_EXPOSURE_MANUAL, 0,
3321 V4L2_EXPOSURE_AUTO);
3322 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
3324 /* Auto/manual gain */
3325 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
3327 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
3330 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
3332 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
3334 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
3336 ctrls->test_pattern =
3337 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
3338 ARRAY_SIZE(test_pattern_menu) - 1,
3339 0, 0, test_pattern_menu);
3340 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
3342 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
3346 v4l2_ctrl_new_std_menu(hdl, ops,
3347 V4L2_CID_POWER_LINE_FREQUENCY,
3348 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
3349 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
3356 ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3357 ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3358 ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3359 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
3360 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
3362 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
3363 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
3364 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
3366 sensor->sd.ctrl_handler = hdl;
3370 v4l2_ctrl_handler_free(hdl);
3374 static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
3375 struct v4l2_subdev_state *sd_state,
3376 struct v4l2_subdev_frame_size_enum *fse)
3380 if (fse->index >= OV5640_NUM_MODES)
3383 fse->min_width = ov5640_mode_data[fse->index].width;
3384 fse->max_width = fse->min_width;
3385 fse->min_height = ov5640_mode_data[fse->index].height;
3386 fse->max_height = fse->min_height;
3391 static int ov5640_enum_frame_interval(
3392 struct v4l2_subdev *sd,
3393 struct v4l2_subdev_state *sd_state,
3394 struct v4l2_subdev_frame_interval_enum *fie)
3396 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3397 struct v4l2_fract tpf;
3402 if (fie->index >= OV5640_NUM_FRAMERATES)
3406 tpf.denominator = ov5640_framerates[fie->index];
3408 ret = ov5640_try_frame_interval(sensor, &tpf,
3409 fie->width, fie->height);
3413 fie->interval = tpf;
3417 static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
3418 struct v4l2_subdev_frame_interval *fi)
3420 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3422 mutex_lock(&sensor->lock);
3423 fi->interval = sensor->frame_interval;
3424 mutex_unlock(&sensor->lock);
3429 static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
3430 struct v4l2_subdev_frame_interval *fi)
3432 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3433 const struct ov5640_mode_info *mode;
3434 int frame_rate, ret = 0;
3439 mutex_lock(&sensor->lock);
3441 if (sensor->streaming) {
3446 mode = sensor->current_mode;
3448 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
3451 if (frame_rate < 0) {
3452 /* Always return a valid frame interval value */
3453 fi->interval = sensor->frame_interval;
3457 mode = ov5640_find_mode(sensor, frame_rate, mode->width,
3458 mode->height, true);
3464 if (mode != sensor->current_mode ||
3465 frame_rate != sensor->current_fr) {
3466 sensor->current_fr = frame_rate;
3467 sensor->frame_interval = fi->interval;
3468 sensor->current_mode = mode;
3469 sensor->pending_mode_change = true;
3471 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
3472 ov5640_calc_pixel_rate(sensor));
3475 mutex_unlock(&sensor->lock);
3479 static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
3480 struct v4l2_subdev_state *sd_state,
3481 struct v4l2_subdev_mbus_code_enum *code)
3485 if (code->index >= ARRAY_SIZE(ov5640_formats))
3488 code->code = ov5640_formats[code->index].code;
3492 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
3494 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3497 mutex_lock(&sensor->lock);
3499 if (sensor->streaming == !enable) {
3500 if (enable && sensor->pending_mode_change) {
3501 ret = ov5640_set_mode(sensor);
3506 if (enable && sensor->pending_fmt_change) {
3507 ret = ov5640_set_framefmt(sensor, &sensor->fmt);
3510 sensor->pending_fmt_change = false;
3513 if (ov5640_is_csi2(sensor))
3514 ret = ov5640_set_stream_mipi(sensor, enable);
3516 ret = ov5640_set_stream_dvp(sensor, enable);
3519 sensor->streaming = enable;
3522 mutex_unlock(&sensor->lock);
3526 static const struct v4l2_subdev_core_ops ov5640_core_ops = {
3527 .s_power = ov5640_s_power,
3528 .log_status = v4l2_ctrl_subdev_log_status,
3529 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
3530 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
3533 static const struct v4l2_subdev_video_ops ov5640_video_ops = {
3534 .g_frame_interval = ov5640_g_frame_interval,
3535 .s_frame_interval = ov5640_s_frame_interval,
3536 .s_stream = ov5640_s_stream,
3539 static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
3540 .enum_mbus_code = ov5640_enum_mbus_code,
3541 .get_fmt = ov5640_get_fmt,
3542 .set_fmt = ov5640_set_fmt,
3543 .enum_frame_size = ov5640_enum_frame_size,
3544 .enum_frame_interval = ov5640_enum_frame_interval,
3547 static const struct v4l2_subdev_ops ov5640_subdev_ops = {
3548 .core = &ov5640_core_ops,
3549 .video = &ov5640_video_ops,
3550 .pad = &ov5640_pad_ops,
3553 static int ov5640_get_regulators(struct ov5640_dev *sensor)
3557 for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
3558 sensor->supplies[i].supply = ov5640_supply_name[i];
3560 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
3561 OV5640_NUM_SUPPLIES,
3565 static int ov5640_check_chip_id(struct ov5640_dev *sensor)
3567 struct i2c_client *client = sensor->i2c_client;
3571 ret = ov5640_set_power_on(sensor);
3575 ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
3577 dev_err(&client->dev, "%s: failed to read chip identifier\n",
3582 if (chip_id != 0x5640) {
3583 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
3589 ov5640_set_power_off(sensor);
3593 static int ov5640_probe(struct i2c_client *client)
3595 struct device *dev = &client->dev;
3596 struct fwnode_handle *endpoint;
3597 struct ov5640_dev *sensor;
3598 struct v4l2_mbus_framefmt *fmt;
3602 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
3606 sensor->i2c_client = client;
3609 * default init sequence initialize sensor to
3610 * YUV422 UYVY VGA@30fps
3613 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
3614 fmt->colorspace = V4L2_COLORSPACE_SRGB;
3615 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
3616 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
3617 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
3620 fmt->field = V4L2_FIELD_NONE;
3621 sensor->frame_interval.numerator = 1;
3622 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
3623 sensor->current_fr = OV5640_30_FPS;
3624 sensor->current_mode =
3625 &ov5640_mode_data[OV5640_MODE_VGA_640_480];
3626 sensor->last_mode = sensor->current_mode;
3627 sensor->current_link_freq = OV5640_DEFAULT_LINK_FREQ;
3629 sensor->ae_target = 52;
3631 /* optional indication of physical rotation of sensor */
3632 ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
3637 sensor->upside_down = true;
3642 dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
3647 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
3650 dev_err(dev, "endpoint node not found\n");
3654 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
3655 fwnode_handle_put(endpoint);
3657 dev_err(dev, "Could not parse endpoint\n");
3661 if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
3662 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
3663 sensor->ep.bus_type != V4L2_MBUS_BT656) {
3664 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
3668 /* get system clock (xclk) */
3669 sensor->xclk = devm_clk_get(dev, "xclk");
3670 if (IS_ERR(sensor->xclk)) {
3671 dev_err(dev, "failed to get xclk\n");
3672 return PTR_ERR(sensor->xclk);
3675 sensor->xclk_freq = clk_get_rate(sensor->xclk);
3676 if (sensor->xclk_freq < OV5640_XCLK_MIN ||
3677 sensor->xclk_freq > OV5640_XCLK_MAX) {
3678 dev_err(dev, "xclk frequency out of range: %d Hz\n",
3683 /* request optional power down pin */
3684 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
3686 if (IS_ERR(sensor->pwdn_gpio))
3687 return PTR_ERR(sensor->pwdn_gpio);
3689 /* request optional reset pin */
3690 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
3692 if (IS_ERR(sensor->reset_gpio))
3693 return PTR_ERR(sensor->reset_gpio);
3695 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
3697 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
3698 V4L2_SUBDEV_FL_HAS_EVENTS;
3699 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
3700 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3701 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
3705 ret = ov5640_get_regulators(sensor);
3709 mutex_init(&sensor->lock);
3711 ret = ov5640_check_chip_id(sensor);
3713 goto entity_cleanup;
3715 ret = ov5640_init_controls(sensor);
3717 goto entity_cleanup;
3719 ret = v4l2_async_register_subdev_sensor(&sensor->sd);
3726 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3728 media_entity_cleanup(&sensor->sd.entity);
3729 mutex_destroy(&sensor->lock);
3733 static int ov5640_remove(struct i2c_client *client)
3735 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3736 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3738 v4l2_async_unregister_subdev(&sensor->sd);
3739 media_entity_cleanup(&sensor->sd.entity);
3740 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3741 mutex_destroy(&sensor->lock);
3746 static const struct i2c_device_id ov5640_id[] = {
3750 MODULE_DEVICE_TABLE(i2c, ov5640_id);
3752 static const struct of_device_id ov5640_dt_ids[] = {
3753 { .compatible = "ovti,ov5640" },
3756 MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
3758 static struct i2c_driver ov5640_i2c_driver = {
3761 .of_match_table = ov5640_dt_ids,
3763 .id_table = ov5640_id,
3764 .probe_new = ov5640_probe,
3765 .remove = ov5640_remove,
3768 module_i2c_driver(ov5640_i2c_driver);
3770 MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
3771 MODULE_LICENSE("GPL");