2 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright (C) 2014-2017 Mentor Graphics Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #include <linux/clk.h>
12 #include <linux/clk-provider.h>
13 #include <linux/clkdev.h>
14 #include <linux/ctype.h>
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/slab.h>
22 #include <linux/types.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/regulator/consumer.h>
25 #include <media/v4l2-async.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-fwnode.h>
29 #include <media/v4l2-subdev.h>
31 /* min/typical/max system clock (xclk) frequencies */
32 #define OV5640_XCLK_MIN 6000000
33 #define OV5640_XCLK_MAX 24000000
35 #define OV5640_DEFAULT_SLAVE_ID 0x3c
37 #define OV5640_REG_CHIP_ID 0x300a
38 #define OV5640_REG_PAD_OUTPUT00 0x3019
39 #define OV5640_REG_SC_PLL_CTRL0 0x3034
40 #define OV5640_REG_SC_PLL_CTRL1 0x3035
41 #define OV5640_REG_SC_PLL_CTRL2 0x3036
42 #define OV5640_REG_SC_PLL_CTRL3 0x3037
43 #define OV5640_REG_SLAVE_ID 0x3100
44 #define OV5640_REG_SYS_ROOT_DIVIDER 0x3108
45 #define OV5640_REG_AWB_R_GAIN 0x3400
46 #define OV5640_REG_AWB_G_GAIN 0x3402
47 #define OV5640_REG_AWB_B_GAIN 0x3404
48 #define OV5640_REG_AWB_MANUAL_CTRL 0x3406
49 #define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500
50 #define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501
51 #define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502
52 #define OV5640_REG_AEC_PK_MANUAL 0x3503
53 #define OV5640_REG_AEC_PK_REAL_GAIN 0x350a
54 #define OV5640_REG_AEC_PK_VTS 0x350c
55 #define OV5640_REG_TIMING_HTS 0x380c
56 #define OV5640_REG_TIMING_VTS 0x380e
57 #define OV5640_REG_TIMING_TC_REG21 0x3821
58 #define OV5640_REG_AEC_CTRL00 0x3a00
59 #define OV5640_REG_AEC_B50_STEP 0x3a08
60 #define OV5640_REG_AEC_B60_STEP 0x3a0a
61 #define OV5640_REG_AEC_CTRL0D 0x3a0d
62 #define OV5640_REG_AEC_CTRL0E 0x3a0e
63 #define OV5640_REG_AEC_CTRL0F 0x3a0f
64 #define OV5640_REG_AEC_CTRL10 0x3a10
65 #define OV5640_REG_AEC_CTRL11 0x3a11
66 #define OV5640_REG_AEC_CTRL1B 0x3a1b
67 #define OV5640_REG_AEC_CTRL1E 0x3a1e
68 #define OV5640_REG_AEC_CTRL1F 0x3a1f
69 #define OV5640_REG_HZ5060_CTRL00 0x3c00
70 #define OV5640_REG_HZ5060_CTRL01 0x3c01
71 #define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c
72 #define OV5640_REG_FRAME_CTRL01 0x4202
73 #define OV5640_REG_MIPI_CTRL00 0x4800
74 #define OV5640_REG_DEBUG_MODE 0x4814
75 #define OV5640_REG_PRE_ISP_TEST_SET1 0x503d
76 #define OV5640_REG_SDE_CTRL0 0x5580
77 #define OV5640_REG_SDE_CTRL1 0x5581
78 #define OV5640_REG_SDE_CTRL3 0x5583
79 #define OV5640_REG_SDE_CTRL4 0x5584
80 #define OV5640_REG_SDE_CTRL5 0x5585
81 #define OV5640_REG_AVG_READOUT 0x56a1
84 OV5640_MODE_QCIF_176_144 = 0,
85 OV5640_MODE_QVGA_320_240,
86 OV5640_MODE_VGA_640_480,
87 OV5640_MODE_NTSC_720_480,
88 OV5640_MODE_PAL_720_576,
89 OV5640_MODE_XGA_1024_768,
90 OV5640_MODE_720P_1280_720,
91 OV5640_MODE_1080P_1920_1080,
92 OV5640_MODE_QSXGA_2592_1944,
96 enum ov5640_frame_rate {
99 OV5640_NUM_FRAMERATES,
103 * FIXME: remove this when a subdev API becomes available
104 * to set the MIPI CSI-2 virtual channel.
106 static unsigned int virtual_channel;
107 module_param(virtual_channel, int, 0);
108 MODULE_PARM_DESC(virtual_channel,
109 "MIPI CSI-2 virtual channel (0..3), default 0");
111 static const int ov5640_framerates[] = {
112 [OV5640_15_FPS] = 15,
113 [OV5640_30_FPS] = 30,
116 /* regulator supplies */
117 static const char * const ov5640_supply_name[] = {
118 "DOVDD", /* Digital I/O (1.8V) suppply */
119 "DVDD", /* Digital Core (1.5V) supply */
120 "AVDD", /* Analog (2.8V) supply */
123 #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
126 * Image size under 1280 * 960 are SUBSAMPLING
127 * Image size upper 1280 * 960 are SCALING
129 enum ov5640_downsize_mode {
141 struct ov5640_mode_info {
142 enum ov5640_mode_id id;
143 enum ov5640_downsize_mode dn_mode;
146 const struct reg_value *reg_data;
150 struct ov5640_ctrls {
151 struct v4l2_ctrl_handler handler;
153 struct v4l2_ctrl *auto_exp;
154 struct v4l2_ctrl *exposure;
157 struct v4l2_ctrl *auto_wb;
158 struct v4l2_ctrl *blue_balance;
159 struct v4l2_ctrl *red_balance;
162 struct v4l2_ctrl *auto_gain;
163 struct v4l2_ctrl *gain;
165 struct v4l2_ctrl *brightness;
166 struct v4l2_ctrl *saturation;
167 struct v4l2_ctrl *contrast;
168 struct v4l2_ctrl *hue;
169 struct v4l2_ctrl *test_pattern;
173 struct i2c_client *i2c_client;
174 struct v4l2_subdev sd;
175 struct media_pad pad;
176 struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
177 struct clk *xclk; /* system clock to OV5640 */
180 struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
181 struct gpio_desc *reset_gpio;
182 struct gpio_desc *pwdn_gpio;
184 /* lock to protect all members below */
189 struct v4l2_mbus_framefmt fmt;
191 const struct ov5640_mode_info *current_mode;
192 enum ov5640_frame_rate current_fr;
193 struct v4l2_fract frame_interval;
195 struct ov5640_ctrls ctrls;
197 u32 prev_sysclk, prev_hts;
198 u32 ae_low, ae_high, ae_target;
200 bool pending_mode_change;
204 static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
206 return container_of(sd, struct ov5640_dev, sd);
209 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
211 return &container_of(ctrl->handler, struct ov5640_dev,
216 * FIXME: all of these register tables are likely filled with
217 * entries that set the register to their power-on default values,
218 * and which are otherwise not touched by this driver. Those entries
219 * should be identified and removed to speed register load time
223 static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
225 {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
226 {0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
227 {0x3034, 0x18, 0, 0}, {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0},
228 {0x3037, 0x13, 0, 0}, {0x3108, 0x01, 0, 0}, {0x3630, 0x36, 0, 0},
229 {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
230 {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
231 {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
232 {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
233 {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
234 {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
235 {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
236 {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
237 {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
238 {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
239 {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
240 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
241 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
242 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
243 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
244 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
245 {0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
246 {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
247 {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
248 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
249 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
250 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
251 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
252 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
253 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
254 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
255 {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
256 {0x300e, 0x45, 0, 0}, {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
257 {0x501f, 0x00, 0, 0}, {0x4713, 0x03, 0, 0}, {0x4407, 0x04, 0, 0},
258 {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
259 {0x4837, 0x0a, 0, 0}, {0x4800, 0x04, 0, 0}, {0x3824, 0x02, 0, 0},
260 {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
261 {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
262 {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
263 {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
264 {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
265 {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
266 {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
267 {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
268 {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
269 {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
270 {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
271 {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
272 {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
273 {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
274 {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
275 {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
276 {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
277 {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
278 {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
279 {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
280 {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
281 {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
282 {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
283 {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
284 {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
285 {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
286 {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
287 {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
288 {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
289 {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
290 {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
291 {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
292 {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
293 {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
294 {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
295 {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
296 {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
297 {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
298 {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
299 {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
300 {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
301 {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
302 {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
303 {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
304 {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
305 {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
306 {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
307 {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
308 {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
309 {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
312 static const struct reg_value ov5640_setting_30fps_VGA_640_480[] = {
314 {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
315 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
316 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
317 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
318 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
319 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
320 {0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
321 {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
322 {0x380e, 0x04, 0, 0}, {0x380f, 0x38, 0, 0}, {0x3810, 0x00, 0, 0},
323 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
324 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
325 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
326 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
327 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
328 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
329 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
330 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
331 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
334 static const struct reg_value ov5640_setting_15fps_VGA_640_480[] = {
335 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
336 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
337 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
338 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
339 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
340 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
341 {0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
342 {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
343 {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
344 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
345 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
346 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
347 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
348 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
349 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
350 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
351 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
352 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
355 static const struct reg_value ov5640_setting_30fps_XGA_1024_768[] = {
357 {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
358 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
359 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
360 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
361 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
362 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
363 {0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
364 {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
365 {0x380e, 0x04, 0, 0}, {0x380f, 0x38, 0, 0}, {0x3810, 0x00, 0, 0},
366 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
367 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
368 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
369 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
370 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
371 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
372 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
373 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
374 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
375 {0x3808, 0x04, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x03, 0, 0},
376 {0x380b, 0x00, 0, 0}, {0x3035, 0x12, 0, 0},
379 static const struct reg_value ov5640_setting_15fps_XGA_1024_768[] = {
380 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
381 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
382 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
383 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
384 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
385 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
386 {0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
387 {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
388 {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
389 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
390 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
391 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
392 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
393 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
394 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
395 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
396 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
397 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3808, 0x04, 0, 0},
398 {0x3809, 0x00, 0, 0}, {0x380a, 0x03, 0, 0}, {0x380b, 0x00, 0, 0},
401 static const struct reg_value ov5640_setting_30fps_QVGA_320_240[] = {
402 {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
403 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
404 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
405 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
406 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
407 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
408 {0x3808, 0x01, 0, 0}, {0x3809, 0x40, 0, 0}, {0x380a, 0x00, 0, 0},
409 {0x380b, 0xf0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
410 {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
411 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
412 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
413 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
414 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
415 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
416 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
417 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
418 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
419 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
422 static const struct reg_value ov5640_setting_15fps_QVGA_320_240[] = {
423 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
424 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
425 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
426 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
427 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
428 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
429 {0x3808, 0x01, 0, 0}, {0x3809, 0x40, 0, 0}, {0x380a, 0x00, 0, 0},
430 {0x380b, 0xf0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
431 {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
432 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
433 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
434 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
435 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
436 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
437 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
438 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
439 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
440 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
443 static const struct reg_value ov5640_setting_30fps_QCIF_176_144[] = {
444 {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
445 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
446 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
447 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
448 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
449 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
450 {0x3808, 0x00, 0, 0}, {0x3809, 0xb0, 0, 0}, {0x380a, 0x00, 0, 0},
451 {0x380b, 0x90, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
452 {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
453 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
454 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
455 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
456 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
457 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
458 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
459 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
460 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
461 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
463 static const struct reg_value ov5640_setting_15fps_QCIF_176_144[] = {
464 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
465 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
466 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
467 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
468 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
469 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
470 {0x3808, 0x00, 0, 0}, {0x3809, 0xb0, 0, 0}, {0x380a, 0x00, 0, 0},
471 {0x380b, 0x90, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
472 {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
473 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
474 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
475 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
476 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
477 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
478 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
479 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
480 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
481 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
484 static const struct reg_value ov5640_setting_30fps_NTSC_720_480[] = {
485 {0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
486 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
487 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
488 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
489 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
490 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
491 {0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x01, 0, 0},
492 {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
493 {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
494 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
495 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
496 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
497 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
498 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
499 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
500 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
501 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
502 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
505 static const struct reg_value ov5640_setting_15fps_NTSC_720_480[] = {
506 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
507 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
508 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
509 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
510 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
511 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
512 {0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x01, 0, 0},
513 {0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
514 {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
515 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
516 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
517 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
518 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
519 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
520 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
521 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
522 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
523 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
526 static const struct reg_value ov5640_setting_30fps_PAL_720_576[] = {
527 {0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
528 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
529 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
530 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
531 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
532 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
533 {0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x02, 0, 0},
534 {0x380b, 0x40, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
535 {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
536 {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
537 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
538 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
539 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
540 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
541 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
542 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
543 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
544 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
547 static const struct reg_value ov5640_setting_15fps_PAL_720_576[] = {
548 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
549 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
550 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
551 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
552 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
553 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
554 {0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x02, 0, 0},
555 {0x380b, 0x40, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
556 {0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
557 {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
558 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
559 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
560 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
561 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
562 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
563 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
564 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
565 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
568 static const struct reg_value ov5640_setting_30fps_720P_1280_720[] = {
569 {0x3008, 0x42, 0, 0},
570 {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
571 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
572 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
573 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
574 {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
575 {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
576 {0x3808, 0x05, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x02, 0, 0},
577 {0x380b, 0xd0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x64, 0, 0},
578 {0x380e, 0x02, 0, 0}, {0x380f, 0xe4, 0, 0}, {0x3810, 0x00, 0, 0},
579 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
580 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
581 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
582 {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
583 {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
584 {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
585 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
586 {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
587 {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, {0x4005, 0x1a, 0, 0},
588 {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0},
591 static const struct reg_value ov5640_setting_15fps_720P_1280_720[] = {
592 {0x3035, 0x41, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
593 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
594 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
595 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
596 {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
597 {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
598 {0x3808, 0x05, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x02, 0, 0},
599 {0x380b, 0xd0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x64, 0, 0},
600 {0x380e, 0x02, 0, 0}, {0x380f, 0xe4, 0, 0}, {0x3810, 0x00, 0, 0},
601 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
602 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
603 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
604 {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
605 {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
606 {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
607 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
608 {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
609 {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
612 static const struct reg_value ov5640_setting_30fps_1080P_1920_1080[] = {
613 {0x3008, 0x42, 0, 0},
614 {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
615 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
616 {0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
617 {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
618 {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
619 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
620 {0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
621 {0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
622 {0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
623 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
624 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
625 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
626 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
627 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
628 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
629 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
630 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
631 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x11, 0, 0},
632 {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
633 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
634 {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
635 {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
636 {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, {0x3808, 0x07, 0, 0},
637 {0x3809, 0x80, 0, 0}, {0x380a, 0x04, 0, 0}, {0x380b, 0x38, 0, 0},
638 {0x380c, 0x09, 0, 0}, {0x380d, 0xc4, 0, 0}, {0x380e, 0x04, 0, 0},
639 {0x380f, 0x60, 0, 0}, {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
640 {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
641 {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
642 {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
643 {0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
644 {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
645 {0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0},
649 static const struct reg_value ov5640_setting_15fps_1080P_1920_1080[] = {
650 {0x3008, 0x42, 0, 0},
651 {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
652 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
653 {0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
654 {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
655 {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
656 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
657 {0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
658 {0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
659 {0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
660 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
661 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
662 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
663 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
664 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
665 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
666 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
667 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
668 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x21, 0, 0},
669 {0x3036, 0x54, 0, 1}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
670 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
671 {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
672 {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
673 {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, {0x3808, 0x07, 0, 0},
674 {0x3809, 0x80, 0, 0}, {0x380a, 0x04, 0, 0}, {0x380b, 0x38, 0, 0},
675 {0x380c, 0x09, 0, 0}, {0x380d, 0xc4, 0, 0}, {0x380e, 0x04, 0, 0},
676 {0x380f, 0x60, 0, 0}, {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
677 {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
678 {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
679 {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
680 {0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
681 {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
682 {0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0},
685 static const struct reg_value ov5640_setting_15fps_QSXGA_2592_1944[] = {
686 {0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0},
687 {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
688 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
689 {0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
690 {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
691 {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
692 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
693 {0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
694 {0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
695 {0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
696 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
697 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
698 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
699 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
700 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
701 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
702 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
703 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
704 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
707 /* power-on sensor init reg table */
708 static const struct ov5640_mode_info ov5640_mode_init_data = {
709 0, SUBSAMPLING, 640, 480, ov5640_init_setting_30fps_VGA,
710 ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
713 static const struct ov5640_mode_info
714 ov5640_mode_data[OV5640_NUM_FRAMERATES][OV5640_NUM_MODES] = {
716 {OV5640_MODE_QCIF_176_144, SUBSAMPLING, 176, 144,
717 ov5640_setting_15fps_QCIF_176_144,
718 ARRAY_SIZE(ov5640_setting_15fps_QCIF_176_144)},
719 {OV5640_MODE_QVGA_320_240, SUBSAMPLING, 320, 240,
720 ov5640_setting_15fps_QVGA_320_240,
721 ARRAY_SIZE(ov5640_setting_15fps_QVGA_320_240)},
722 {OV5640_MODE_VGA_640_480, SUBSAMPLING, 640, 480,
723 ov5640_setting_15fps_VGA_640_480,
724 ARRAY_SIZE(ov5640_setting_15fps_VGA_640_480)},
725 {OV5640_MODE_NTSC_720_480, SUBSAMPLING, 720, 480,
726 ov5640_setting_15fps_NTSC_720_480,
727 ARRAY_SIZE(ov5640_setting_15fps_NTSC_720_480)},
728 {OV5640_MODE_PAL_720_576, SUBSAMPLING, 720, 576,
729 ov5640_setting_15fps_PAL_720_576,
730 ARRAY_SIZE(ov5640_setting_15fps_PAL_720_576)},
731 {OV5640_MODE_XGA_1024_768, SUBSAMPLING, 1024, 768,
732 ov5640_setting_15fps_XGA_1024_768,
733 ARRAY_SIZE(ov5640_setting_15fps_XGA_1024_768)},
734 {OV5640_MODE_720P_1280_720, SUBSAMPLING, 1280, 720,
735 ov5640_setting_15fps_720P_1280_720,
736 ARRAY_SIZE(ov5640_setting_15fps_720P_1280_720)},
737 {OV5640_MODE_1080P_1920_1080, SCALING, 1920, 1080,
738 ov5640_setting_15fps_1080P_1920_1080,
739 ARRAY_SIZE(ov5640_setting_15fps_1080P_1920_1080)},
740 {OV5640_MODE_QSXGA_2592_1944, SCALING, 2592, 1944,
741 ov5640_setting_15fps_QSXGA_2592_1944,
742 ARRAY_SIZE(ov5640_setting_15fps_QSXGA_2592_1944)},
744 {OV5640_MODE_QCIF_176_144, SUBSAMPLING, 176, 144,
745 ov5640_setting_30fps_QCIF_176_144,
746 ARRAY_SIZE(ov5640_setting_30fps_QCIF_176_144)},
747 {OV5640_MODE_QVGA_320_240, SUBSAMPLING, 320, 240,
748 ov5640_setting_30fps_QVGA_320_240,
749 ARRAY_SIZE(ov5640_setting_30fps_QVGA_320_240)},
750 {OV5640_MODE_VGA_640_480, SUBSAMPLING, 640, 480,
751 ov5640_setting_30fps_VGA_640_480,
752 ARRAY_SIZE(ov5640_setting_30fps_VGA_640_480)},
753 {OV5640_MODE_NTSC_720_480, SUBSAMPLING, 720, 480,
754 ov5640_setting_30fps_NTSC_720_480,
755 ARRAY_SIZE(ov5640_setting_30fps_NTSC_720_480)},
756 {OV5640_MODE_PAL_720_576, SUBSAMPLING, 720, 576,
757 ov5640_setting_30fps_PAL_720_576,
758 ARRAY_SIZE(ov5640_setting_30fps_PAL_720_576)},
759 {OV5640_MODE_XGA_1024_768, SUBSAMPLING, 1024, 768,
760 ov5640_setting_30fps_XGA_1024_768,
761 ARRAY_SIZE(ov5640_setting_30fps_XGA_1024_768)},
762 {OV5640_MODE_720P_1280_720, SUBSAMPLING, 1280, 720,
763 ov5640_setting_30fps_720P_1280_720,
764 ARRAY_SIZE(ov5640_setting_30fps_720P_1280_720)},
765 {OV5640_MODE_1080P_1920_1080, SCALING, 1920, 1080,
766 ov5640_setting_30fps_1080P_1920_1080,
767 ARRAY_SIZE(ov5640_setting_30fps_1080P_1920_1080)},
768 {OV5640_MODE_QSXGA_2592_1944, -1, 0, 0, NULL, 0},
772 static int ov5640_init_slave_id(struct ov5640_dev *sensor)
774 struct i2c_client *client = sensor->i2c_client;
779 if (client->addr == OV5640_DEFAULT_SLAVE_ID)
782 buf[0] = OV5640_REG_SLAVE_ID >> 8;
783 buf[1] = OV5640_REG_SLAVE_ID & 0xff;
784 buf[2] = client->addr << 1;
786 msg.addr = OV5640_DEFAULT_SLAVE_ID;
789 msg.len = sizeof(buf);
791 ret = i2c_transfer(client->adapter, &msg, 1);
793 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
800 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
802 struct i2c_client *client = sensor->i2c_client;
811 msg.addr = client->addr;
812 msg.flags = client->flags;
814 msg.len = sizeof(buf);
816 ret = i2c_transfer(client->adapter, &msg, 1);
818 v4l2_err(&sensor->sd, "%s: error: reg=%x, val=%x\n",
826 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
828 struct i2c_client *client = sensor->i2c_client;
829 struct i2c_msg msg[2];
836 msg[0].addr = client->addr;
837 msg[0].flags = client->flags;
839 msg[0].len = sizeof(buf);
841 msg[1].addr = client->addr;
842 msg[1].flags = client->flags | I2C_M_RD;
846 ret = i2c_transfer(client->adapter, msg, 2);
854 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
859 ret = ov5640_read_reg(sensor, reg, &hi);
862 ret = ov5640_read_reg(sensor, reg+1, &lo);
866 *val = ((u16)hi << 8) | (u16)lo;
870 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
874 ret = ov5640_write_reg(sensor, reg, val >> 8);
878 return ov5640_write_reg(sensor, reg + 1, val & 0xff);
881 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
887 ret = ov5640_read_reg(sensor, reg, &readval);
895 return ov5640_write_reg(sensor, reg, val);
898 /* download ov5640 settings to sensor through i2c */
899 static int ov5640_load_regs(struct ov5640_dev *sensor,
900 const struct ov5640_mode_info *mode)
902 const struct reg_value *regs = mode->reg_data;
909 for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
910 delay_ms = regs->delay_ms;
911 reg_addr = regs->reg_addr;
916 ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
918 ret = ov5640_write_reg(sensor, reg_addr, val);
923 usleep_range(1000*delay_ms, 1000*delay_ms+100);
929 /* read exposure, in number of line periods */
930 static int ov5640_get_exposure(struct ov5640_dev *sensor)
935 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
938 exp = ((int)temp & 0x0f) << 16;
939 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
942 exp |= ((int)temp << 8);
943 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
951 /* write exposure, given number of line periods */
952 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
958 ret = ov5640_write_reg(sensor,
959 OV5640_REG_AEC_PK_EXPOSURE_LO,
963 ret = ov5640_write_reg(sensor,
964 OV5640_REG_AEC_PK_EXPOSURE_MED,
965 (exposure >> 8) & 0xff);
968 return ov5640_write_reg(sensor,
969 OV5640_REG_AEC_PK_EXPOSURE_HI,
970 (exposure >> 16) & 0x0f);
973 static int ov5640_get_gain(struct ov5640_dev *sensor)
978 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
985 static int ov5640_set_stream(struct ov5640_dev *sensor, bool on)
989 ret = ov5640_mod_reg(sensor, OV5640_REG_MIPI_CTRL00, BIT(5),
993 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00,
998 return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1002 static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1004 /* calculate sysclk */
1005 u32 xvclk = sensor->xclk_freq / 10000;
1006 u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1007 u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1008 u32 bit_div2x = 1, sclk_rdiv, sysclk;
1012 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1015 temp2 = temp1 & 0x0f;
1016 if (temp2 == 8 || temp2 == 10)
1017 bit_div2x = temp2 / 2;
1019 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1022 sysdiv = temp1 >> 4;
1026 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1031 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1034 prediv = temp1 & 0x0f;
1035 pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1037 ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1040 temp2 = temp1 & 0x03;
1041 sclk_rdiv = sclk_rdiv_map[temp2];
1043 if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1046 VCO = xvclk * multiplier / prediv;
1048 sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1053 static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1055 /* read HTS from register settings */
1059 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1063 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1066 static int ov5640_get_hts(struct ov5640_dev *sensor)
1068 /* read HTS from register settings */
1072 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1078 static int ov5640_get_vts(struct ov5640_dev *sensor)
1083 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1089 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1091 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1094 static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1096 /* get banding filter value */
1097 int ret, light_freq = 0;
1100 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1106 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1119 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1135 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1137 u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1140 /* read preview PCLK */
1141 ret = ov5640_get_sysclk(sensor);
1146 sensor->prev_sysclk = ret;
1147 /* read preview HTS */
1148 ret = ov5640_get_hts(sensor);
1153 sensor->prev_hts = ret;
1155 /* read preview VTS */
1156 ret = ov5640_get_vts(sensor);
1162 /* calculate banding filter */
1164 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
1165 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
1170 max_band60 = (int)((prev_vts - 4) / band_step60);
1171 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
1176 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
1177 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
1182 max_band50 = (int)((prev_vts - 4) / band_step50);
1183 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
1186 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
1188 /* stable in high */
1189 u32 fast_high, fast_low;
1192 sensor->ae_low = target * 23 / 25; /* 0.92 */
1193 sensor->ae_high = target * 27 / 25; /* 1.08 */
1195 fast_high = sensor->ae_high << 1;
1196 if (fast_high > 255)
1199 fast_low = sensor->ae_low >> 1;
1201 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
1204 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
1207 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
1210 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
1213 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
1216 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
1219 static int ov5640_get_binning(struct ov5640_dev *sensor)
1224 ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
1228 return temp & BIT(0);
1231 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
1233 u8 temp, channel = virtual_channel;
1239 ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
1243 temp |= (channel << 6);
1244 return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
1247 static const struct ov5640_mode_info *
1248 ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
1249 int width, int height, bool nearest)
1251 const struct ov5640_mode_info *mode = NULL;
1254 for (i = OV5640_NUM_MODES - 1; i >= 0; i--) {
1255 mode = &ov5640_mode_data[fr][i];
1257 if (!mode->reg_data)
1260 if ((nearest && mode->width <= width &&
1261 mode->height <= height) ||
1262 (!nearest && mode->width == width &&
1263 mode->height == height))
1267 if (nearest && i < 0)
1268 mode = &ov5640_mode_data[fr][0];
1274 * sensor changes between scaling and subsampling, go through
1275 * exposure calculation
1277 static int ov5640_set_mode_exposure_calc(
1278 struct ov5640_dev *sensor, const struct ov5640_mode_info *mode)
1280 u32 prev_shutter, prev_gain16;
1281 u32 cap_shutter, cap_gain16;
1282 u32 cap_sysclk, cap_hts, cap_vts;
1283 u32 light_freq, cap_bandfilt, cap_maxband;
1284 u32 cap_gain16_shutter;
1288 if (mode->reg_data == NULL)
1291 /* read preview shutter */
1292 ret = ov5640_get_exposure(sensor);
1296 ret = ov5640_get_binning(sensor);
1299 if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
1300 mode->id != OV5640_MODE_1080P_1920_1080)
1303 /* read preview gain */
1304 ret = ov5640_get_gain(sensor);
1310 ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
1314 /* turn off night mode for capture */
1315 ret = ov5640_set_night_mode(sensor);
1319 /* Write capture setting */
1320 ret = ov5640_load_regs(sensor, mode);
1324 /* read capture VTS */
1325 ret = ov5640_get_vts(sensor);
1329 ret = ov5640_get_hts(sensor);
1336 ret = ov5640_get_sysclk(sensor);
1343 /* calculate capture banding filter */
1344 ret = ov5640_get_light_freq(sensor);
1349 if (light_freq == 60) {
1351 cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
1354 cap_bandfilt = cap_sysclk * 100 / cap_hts;
1357 if (!sensor->prev_sysclk) {
1358 ret = ov5640_get_sysclk(sensor);
1363 sensor->prev_sysclk = ret;
1369 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
1371 /* calculate capture shutter/gain16 */
1372 if (average > sensor->ae_low && average < sensor->ae_high) {
1373 /* in stable range */
1374 cap_gain16_shutter =
1375 prev_gain16 * prev_shutter *
1376 cap_sysclk / sensor->prev_sysclk *
1377 sensor->prev_hts / cap_hts *
1378 sensor->ae_target / average;
1380 cap_gain16_shutter =
1381 prev_gain16 * prev_shutter *
1382 cap_sysclk / sensor->prev_sysclk *
1383 sensor->prev_hts / cap_hts;
1386 /* gain to shutter */
1387 if (cap_gain16_shutter < (cap_bandfilt * 16)) {
1388 /* shutter < 1/100 */
1389 cap_shutter = cap_gain16_shutter / 16;
1390 if (cap_shutter < 1)
1393 cap_gain16 = cap_gain16_shutter / cap_shutter;
1394 if (cap_gain16 < 16)
1397 if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
1398 /* exposure reach max */
1399 cap_shutter = cap_bandfilt * cap_maxband;
1403 cap_gain16 = cap_gain16_shutter / cap_shutter;
1405 /* 1/100 < (cap_shutter = n/100) =< max */
1407 ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
1412 cap_gain16 = cap_gain16_shutter / cap_shutter;
1416 /* set capture gain */
1417 ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.gain, cap_gain16);
1421 /* write capture shutter */
1422 if (cap_shutter > (cap_vts - 4)) {
1423 cap_vts = cap_shutter + 4;
1424 ret = ov5640_set_vts(sensor, cap_vts);
1430 return __v4l2_ctrl_s_ctrl(sensor->ctrls.exposure, cap_shutter);
1434 * if sensor changes inside scaling or subsampling
1435 * change mode directly
1437 static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
1438 const struct ov5640_mode_info *mode)
1442 if (mode->reg_data == NULL)
1445 /* Write capture setting */
1446 ret = ov5640_load_regs(sensor, mode);
1450 /* turn auto gain/exposure back on for direct mode */
1451 ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 1);
1454 return __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, V4L2_EXPOSURE_AUTO);
1457 static int ov5640_set_mode(struct ov5640_dev *sensor,
1458 const struct ov5640_mode_info *orig_mode)
1460 const struct ov5640_mode_info *mode = sensor->current_mode;
1461 enum ov5640_downsize_mode dn_mode, orig_dn_mode;
1464 dn_mode = mode->dn_mode;
1465 orig_dn_mode = orig_mode->dn_mode;
1467 /* auto gain and exposure must be turned off when changing modes */
1468 ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 0);
1471 ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, V4L2_EXPOSURE_MANUAL);
1475 if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
1476 (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
1478 * change between subsampling and scaling
1479 * go through exposure calucation
1481 ret = ov5640_set_mode_exposure_calc(sensor, mode);
1484 * change inside subsampling or scaling
1485 * download firmware directly
1487 ret = ov5640_set_mode_direct(sensor, mode);
1493 ret = ov5640_set_ae_target(sensor, sensor->ae_target);
1496 ret = ov5640_get_light_freq(sensor);
1499 ret = ov5640_set_bandingfilter(sensor);
1502 ret = ov5640_set_virtual_channel(sensor);
1506 sensor->pending_mode_change = false;
1511 /* restore the last set video mode after chip power-on */
1512 static int ov5640_restore_mode(struct ov5640_dev *sensor)
1516 /* first load the initial register values */
1517 ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
1521 /* now restore the last capture mode */
1522 return ov5640_set_mode(sensor, &ov5640_mode_init_data);
1525 static void ov5640_power(struct ov5640_dev *sensor, bool enable)
1527 gpiod_set_value(sensor->pwdn_gpio, enable ? 0 : 1);
1530 static void ov5640_reset(struct ov5640_dev *sensor)
1532 if (!sensor->reset_gpio)
1535 gpiod_set_value(sensor->reset_gpio, 0);
1537 /* camera power cycle */
1538 ov5640_power(sensor, false);
1539 usleep_range(5000, 10000);
1540 ov5640_power(sensor, true);
1541 usleep_range(5000, 10000);
1543 gpiod_set_value(sensor->reset_gpio, 1);
1544 usleep_range(1000, 2000);
1546 gpiod_set_value(sensor->reset_gpio, 0);
1547 usleep_range(5000, 10000);
1550 static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
1555 clk_prepare_enable(sensor->xclk);
1557 ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
1562 ov5640_reset(sensor);
1563 ov5640_power(sensor, true);
1565 ret = ov5640_init_slave_id(sensor);
1569 ret = ov5640_restore_mode(sensor);
1574 * start streaming briefly followed by stream off in
1575 * order to coax the clock lane into LP-11 state.
1577 ret = ov5640_set_stream(sensor, true);
1580 usleep_range(1000, 2000);
1581 ret = ov5640_set_stream(sensor, false);
1589 ov5640_power(sensor, false);
1590 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
1592 clk_disable_unprepare(sensor->xclk);
1596 /* --------------- Subdev Operations --------------- */
1598 static int ov5640_s_power(struct v4l2_subdev *sd, int on)
1600 struct ov5640_dev *sensor = to_ov5640_dev(sd);
1603 mutex_lock(&sensor->lock);
1606 * If the power count is modified from 0 to != 0 or from != 0 to 0,
1607 * update the power state.
1609 if (sensor->power_count == !on) {
1610 ret = ov5640_set_power(sensor, !!on);
1615 /* Update the power count. */
1616 sensor->power_count += on ? 1 : -1;
1617 WARN_ON(sensor->power_count < 0);
1619 mutex_unlock(&sensor->lock);
1621 if (on && !ret && sensor->power_count == 1) {
1622 /* restore controls */
1623 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
1629 static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
1630 struct v4l2_fract *fi,
1631 u32 width, u32 height)
1633 const struct ov5640_mode_info *mode;
1634 u32 minfps, maxfps, fps;
1637 minfps = ov5640_framerates[OV5640_15_FPS];
1638 maxfps = ov5640_framerates[OV5640_30_FPS];
1640 if (fi->numerator == 0) {
1641 fi->denominator = maxfps;
1643 return OV5640_30_FPS;
1646 fps = DIV_ROUND_CLOSEST(fi->denominator, fi->numerator);
1650 fi->denominator = maxfps;
1651 else if (fps < minfps)
1652 fi->denominator = minfps;
1653 else if (2 * fps >= 2 * minfps + (maxfps - minfps))
1654 fi->denominator = maxfps;
1656 fi->denominator = minfps;
1658 ret = (fi->denominator == minfps) ? OV5640_15_FPS : OV5640_30_FPS;
1660 mode = ov5640_find_mode(sensor, ret, width, height, false);
1661 return mode ? ret : -EINVAL;
1664 static int ov5640_get_fmt(struct v4l2_subdev *sd,
1665 struct v4l2_subdev_pad_config *cfg,
1666 struct v4l2_subdev_format *format)
1668 struct ov5640_dev *sensor = to_ov5640_dev(sd);
1669 struct v4l2_mbus_framefmt *fmt;
1671 if (format->pad != 0)
1674 mutex_lock(&sensor->lock);
1676 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1677 fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
1682 format->format = *fmt;
1684 mutex_unlock(&sensor->lock);
1689 static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
1690 struct v4l2_mbus_framefmt *fmt,
1691 enum ov5640_frame_rate fr,
1692 const struct ov5640_mode_info **new_mode)
1694 struct ov5640_dev *sensor = to_ov5640_dev(sd);
1695 const struct ov5640_mode_info *mode;
1697 mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
1701 fmt->width = mode->width;
1702 fmt->height = mode->height;
1703 fmt->code = sensor->fmt.code;
1710 static int ov5640_set_fmt(struct v4l2_subdev *sd,
1711 struct v4l2_subdev_pad_config *cfg,
1712 struct v4l2_subdev_format *format)
1714 struct ov5640_dev *sensor = to_ov5640_dev(sd);
1715 const struct ov5640_mode_info *new_mode;
1718 if (format->pad != 0)
1721 mutex_lock(&sensor->lock);
1723 if (sensor->streaming) {
1728 ret = ov5640_try_fmt_internal(sd, &format->format,
1729 sensor->current_fr, &new_mode);
1733 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1734 struct v4l2_mbus_framefmt *fmt =
1735 v4l2_subdev_get_try_format(sd, cfg, 0);
1737 *fmt = format->format;
1741 sensor->current_mode = new_mode;
1742 sensor->fmt = format->format;
1743 sensor->pending_mode_change = true;
1745 mutex_unlock(&sensor->lock);
1754 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
1759 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
1763 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
1765 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
1771 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
1776 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
1780 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
1783 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
1789 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
1794 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
1798 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
1802 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
1805 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
1811 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
1815 ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
1816 BIT(0), awb ? 0 : 1);
1821 u16 red = (u16)sensor->ctrls.red_balance->val;
1822 u16 blue = (u16)sensor->ctrls.blue_balance->val;
1824 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
1827 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
1833 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, int exp)
1835 struct ov5640_ctrls *ctrls = &sensor->ctrls;
1836 bool auto_exposure = (exp == V4L2_EXPOSURE_AUTO);
1839 if (ctrls->auto_exp->is_new) {
1840 ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1841 BIT(0), auto_exposure ? 0 : BIT(0));
1846 if (!auto_exposure && ctrls->exposure->is_new) {
1849 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
1853 ret = ov5640_get_vts(sensor);
1858 if (ctrls->exposure->val < max_exp)
1859 ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
1865 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, int auto_gain)
1867 struct ov5640_ctrls *ctrls = &sensor->ctrls;
1870 if (ctrls->auto_gain->is_new) {
1871 ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1872 BIT(1), ctrls->auto_gain->val ? 0 : BIT(1));
1877 if (!auto_gain && ctrls->gain->is_new) {
1878 u16 gain = (u16)ctrls->gain->val;
1880 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1887 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
1889 return ov5640_mod_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
1890 0xa4, value ? 0xa4 : 0);
1893 static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1895 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1896 struct ov5640_dev *sensor = to_ov5640_dev(sd);
1899 /* v4l2_ctrl_lock() locks our own mutex */
1902 case V4L2_CID_AUTOGAIN:
1903 val = ov5640_get_gain(sensor);
1906 sensor->ctrls.gain->val = val;
1908 case V4L2_CID_EXPOSURE_AUTO:
1909 val = ov5640_get_exposure(sensor);
1912 sensor->ctrls.exposure->val = val;
1919 static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
1921 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1922 struct ov5640_dev *sensor = to_ov5640_dev(sd);
1925 /* v4l2_ctrl_lock() locks our own mutex */
1928 * If the device is not powered up by the host driver do
1929 * not apply any controls to H/W at this time. Instead
1930 * the controls will be restored right after power-up.
1932 if (sensor->power_count == 0)
1936 case V4L2_CID_AUTOGAIN:
1937 ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
1939 case V4L2_CID_EXPOSURE_AUTO:
1940 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
1942 case V4L2_CID_AUTO_WHITE_BALANCE:
1943 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
1946 ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
1948 case V4L2_CID_CONTRAST:
1949 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
1951 case V4L2_CID_SATURATION:
1952 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
1954 case V4L2_CID_TEST_PATTERN:
1955 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
1965 static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
1966 .g_volatile_ctrl = ov5640_g_volatile_ctrl,
1967 .s_ctrl = ov5640_s_ctrl,
1970 static const char * const test_pattern_menu[] = {
1975 static int ov5640_init_controls(struct ov5640_dev *sensor)
1977 const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
1978 struct ov5640_ctrls *ctrls = &sensor->ctrls;
1979 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
1982 v4l2_ctrl_handler_init(hdl, 32);
1984 /* we can use our own mutex for the ctrl lock */
1985 hdl->lock = &sensor->lock;
1987 /* Auto/manual white balance */
1988 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
1989 V4L2_CID_AUTO_WHITE_BALANCE,
1991 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
1993 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
1995 /* Auto/manual exposure */
1996 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
1997 V4L2_CID_EXPOSURE_AUTO,
1998 V4L2_EXPOSURE_MANUAL, 0,
1999 V4L2_EXPOSURE_AUTO);
2000 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
2002 /* Auto/manual gain */
2003 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
2005 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
2008 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
2010 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
2012 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
2014 ctrls->test_pattern =
2015 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
2016 ARRAY_SIZE(test_pattern_menu) - 1,
2017 0, 0, test_pattern_menu);
2024 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
2025 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
2027 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
2028 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
2029 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
2031 sensor->sd.ctrl_handler = hdl;
2035 v4l2_ctrl_handler_free(hdl);
2039 static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
2040 struct v4l2_subdev_pad_config *cfg,
2041 struct v4l2_subdev_frame_size_enum *fse)
2045 if (fse->index >= OV5640_NUM_MODES)
2048 fse->min_width = fse->max_width =
2049 ov5640_mode_data[0][fse->index].width;
2050 fse->min_height = fse->max_height =
2051 ov5640_mode_data[0][fse->index].height;
2056 static int ov5640_enum_frame_interval(
2057 struct v4l2_subdev *sd,
2058 struct v4l2_subdev_pad_config *cfg,
2059 struct v4l2_subdev_frame_interval_enum *fie)
2061 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2062 struct v4l2_fract tpf;
2067 if (fie->index >= OV5640_NUM_FRAMERATES)
2071 tpf.denominator = ov5640_framerates[fie->index];
2073 ret = ov5640_try_frame_interval(sensor, &tpf,
2074 fie->width, fie->height);
2078 fie->interval = tpf;
2082 static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
2083 struct v4l2_subdev_frame_interval *fi)
2085 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2087 mutex_lock(&sensor->lock);
2088 fi->interval = sensor->frame_interval;
2089 mutex_unlock(&sensor->lock);
2094 static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
2095 struct v4l2_subdev_frame_interval *fi)
2097 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2098 const struct ov5640_mode_info *mode;
2099 int frame_rate, ret = 0;
2104 mutex_lock(&sensor->lock);
2106 if (sensor->streaming) {
2111 mode = sensor->current_mode;
2113 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
2114 mode->width, mode->height);
2116 frame_rate = OV5640_15_FPS;
2118 sensor->current_fr = frame_rate;
2119 sensor->frame_interval = fi->interval;
2120 sensor->pending_mode_change = true;
2122 mutex_unlock(&sensor->lock);
2126 static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
2127 struct v4l2_subdev_pad_config *cfg,
2128 struct v4l2_subdev_mbus_code_enum *code)
2130 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2134 if (code->index != 0)
2137 code->code = sensor->fmt.code;
2142 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
2144 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2147 mutex_lock(&sensor->lock);
2149 if (sensor->streaming == !enable) {
2150 if (enable && sensor->pending_mode_change) {
2151 ret = ov5640_set_mode(sensor, sensor->current_mode);
2156 ret = ov5640_set_stream(sensor, enable);
2158 sensor->streaming = enable;
2161 mutex_unlock(&sensor->lock);
2165 static const struct v4l2_subdev_core_ops ov5640_core_ops = {
2166 .s_power = ov5640_s_power,
2169 static const struct v4l2_subdev_video_ops ov5640_video_ops = {
2170 .g_frame_interval = ov5640_g_frame_interval,
2171 .s_frame_interval = ov5640_s_frame_interval,
2172 .s_stream = ov5640_s_stream,
2175 static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
2176 .enum_mbus_code = ov5640_enum_mbus_code,
2177 .get_fmt = ov5640_get_fmt,
2178 .set_fmt = ov5640_set_fmt,
2179 .enum_frame_size = ov5640_enum_frame_size,
2180 .enum_frame_interval = ov5640_enum_frame_interval,
2183 static const struct v4l2_subdev_ops ov5640_subdev_ops = {
2184 .core = &ov5640_core_ops,
2185 .video = &ov5640_video_ops,
2186 .pad = &ov5640_pad_ops,
2189 static int ov5640_get_regulators(struct ov5640_dev *sensor)
2193 for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
2194 sensor->supplies[i].supply = ov5640_supply_name[i];
2196 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
2197 OV5640_NUM_SUPPLIES,
2201 static int ov5640_probe(struct i2c_client *client,
2202 const struct i2c_device_id *id)
2204 struct device *dev = &client->dev;
2205 struct fwnode_handle *endpoint;
2206 struct ov5640_dev *sensor;
2209 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
2213 sensor->i2c_client = client;
2214 sensor->fmt.code = MEDIA_BUS_FMT_UYVY8_2X8;
2215 sensor->fmt.width = 640;
2216 sensor->fmt.height = 480;
2217 sensor->fmt.field = V4L2_FIELD_NONE;
2218 sensor->frame_interval.numerator = 1;
2219 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
2220 sensor->current_fr = OV5640_30_FPS;
2221 sensor->current_mode =
2222 &ov5640_mode_data[OV5640_30_FPS][OV5640_MODE_VGA_640_480];
2223 sensor->pending_mode_change = true;
2225 sensor->ae_target = 52;
2227 endpoint = fwnode_graph_get_next_endpoint(
2228 of_fwnode_handle(client->dev.of_node), NULL);
2230 dev_err(dev, "endpoint node not found\n");
2234 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
2235 fwnode_handle_put(endpoint);
2237 dev_err(dev, "Could not parse endpoint\n");
2241 if (sensor->ep.bus_type != V4L2_MBUS_CSI2) {
2242 dev_err(dev, "invalid bus type, must be MIPI CSI2\n");
2246 /* get system clock (xclk) */
2247 sensor->xclk = devm_clk_get(dev, "xclk");
2248 if (IS_ERR(sensor->xclk)) {
2249 dev_err(dev, "failed to get xclk\n");
2250 return PTR_ERR(sensor->xclk);
2253 sensor->xclk_freq = clk_get_rate(sensor->xclk);
2254 if (sensor->xclk_freq < OV5640_XCLK_MIN ||
2255 sensor->xclk_freq > OV5640_XCLK_MAX) {
2256 dev_err(dev, "xclk frequency out of range: %d Hz\n",
2261 /* request optional power down pin */
2262 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
2264 if (IS_ERR(sensor->pwdn_gpio))
2265 return PTR_ERR(sensor->pwdn_gpio);
2267 /* request optional reset pin */
2268 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2270 if (IS_ERR(sensor->reset_gpio))
2271 return PTR_ERR(sensor->reset_gpio);
2273 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
2275 sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
2276 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
2277 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2278 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
2282 ret = ov5640_get_regulators(sensor);
2286 mutex_init(&sensor->lock);
2288 ret = ov5640_init_controls(sensor);
2290 goto entity_cleanup;
2292 ret = v4l2_async_register_subdev(&sensor->sd);
2299 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2301 media_entity_cleanup(&sensor->sd.entity);
2302 mutex_destroy(&sensor->lock);
2306 static int ov5640_remove(struct i2c_client *client)
2308 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2309 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2311 v4l2_async_unregister_subdev(&sensor->sd);
2312 media_entity_cleanup(&sensor->sd.entity);
2313 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2314 mutex_destroy(&sensor->lock);
2319 static const struct i2c_device_id ov5640_id[] = {
2323 MODULE_DEVICE_TABLE(i2c, ov5640_id);
2325 static const struct of_device_id ov5640_dt_ids[] = {
2326 { .compatible = "ovti,ov5640" },
2329 MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
2331 static struct i2c_driver ov5640_i2c_driver = {
2334 .of_match_table = ov5640_dt_ids,
2336 .id_table = ov5640_id,
2337 .probe = ov5640_probe,
2338 .remove = ov5640_remove,
2341 module_i2c_driver(ov5640_i2c_driver);
2343 MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
2344 MODULE_LICENSE("GPL");