xref: /openbmc/linux/drivers/media/i2c/ov5640.c (revision 4da722ca)
1 /*
2  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
3  * Copyright (C) 2014-2017 Mentor Graphics Inc.
4  *
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.
9  */
10 
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>
30 
31 /* min/typical/max system clock (xclk) frequencies */
32 #define OV5640_XCLK_MIN  6000000
33 #define OV5640_XCLK_MAX 24000000
34 
35 #define OV5640_DEFAULT_SLAVE_ID 0x3c
36 
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
82 
83 enum ov5640_mode_id {
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,
93 	OV5640_NUM_MODES,
94 };
95 
96 enum ov5640_frame_rate {
97 	OV5640_15_FPS = 0,
98 	OV5640_30_FPS,
99 	OV5640_NUM_FRAMERATES,
100 };
101 
102 /*
103  * FIXME: remove this when a subdev API becomes available
104  * to set the MIPI CSI-2 virtual channel.
105  */
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");
110 
111 static const int ov5640_framerates[] = {
112 	[OV5640_15_FPS] = 15,
113 	[OV5640_30_FPS] = 30,
114 };
115 
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 */
121 };
122 
123 #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
124 
125 /*
126  * Image size under 1280 * 960 are SUBSAMPLING
127  * Image size upper 1280 * 960 are SCALING
128  */
129 enum ov5640_downsize_mode {
130 	SUBSAMPLING,
131 	SCALING,
132 };
133 
134 struct reg_value {
135 	u16 reg_addr;
136 	u8 val;
137 	u8 mask;
138 	u32 delay_ms;
139 };
140 
141 struct ov5640_mode_info {
142 	enum ov5640_mode_id id;
143 	enum ov5640_downsize_mode dn_mode;
144 	u32 width;
145 	u32 height;
146 	const struct reg_value *reg_data;
147 	u32 reg_data_size;
148 };
149 
150 struct ov5640_ctrls {
151 	struct v4l2_ctrl_handler handler;
152 	struct {
153 		struct v4l2_ctrl *auto_exp;
154 		struct v4l2_ctrl *exposure;
155 	};
156 	struct {
157 		struct v4l2_ctrl *auto_wb;
158 		struct v4l2_ctrl *blue_balance;
159 		struct v4l2_ctrl *red_balance;
160 	};
161 	struct {
162 		struct v4l2_ctrl *auto_gain;
163 		struct v4l2_ctrl *gain;
164 	};
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;
170 };
171 
172 struct ov5640_dev {
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 */
178 	u32 xclk_freq;
179 
180 	struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
181 	struct gpio_desc *reset_gpio;
182 	struct gpio_desc *pwdn_gpio;
183 
184 	/* lock to protect all members below */
185 	struct mutex lock;
186 
187 	int power_count;
188 
189 	struct v4l2_mbus_framefmt fmt;
190 
191 	const struct ov5640_mode_info *current_mode;
192 	enum ov5640_frame_rate current_fr;
193 	struct v4l2_fract frame_interval;
194 
195 	struct ov5640_ctrls ctrls;
196 
197 	u32 prev_sysclk, prev_hts;
198 	u32 ae_low, ae_high, ae_target;
199 
200 	bool pending_mode_change;
201 	bool streaming;
202 };
203 
204 static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
205 {
206 	return container_of(sd, struct ov5640_dev, sd);
207 }
208 
209 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
210 {
211 	return &container_of(ctrl->handler, struct ov5640_dev,
212 			     ctrls.handler)->sd;
213 }
214 
215 /*
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
220  * over i2c.
221  */
222 
223 static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
224 
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},
310 };
311 
312 static const struct reg_value ov5640_setting_30fps_VGA_640_480[] = {
313 
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},
332 };
333 
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},
353 };
354 
355 static const struct reg_value ov5640_setting_30fps_XGA_1024_768[] = {
356 
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},
377 };
378 
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},
399 };
400 
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},
420 };
421 
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},
441 };
442 
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},
462 };
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},
482 };
483 
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},
503 };
504 
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},
524 };
525 
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},
545 };
546 
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},
566 };
567 
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},
589 };
590 
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},
610 };
611 
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},
646 	{0x3503, 0, 0, 0},
647 };
648 
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},
683 };
684 
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},
705 };
706 
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),
711 };
712 
713 static const struct ov5640_mode_info
714 ov5640_mode_data[OV5640_NUM_FRAMERATES][OV5640_NUM_MODES] = {
715 	{
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)},
743 	}, {
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},
769 	},
770 };
771 
772 static int ov5640_init_slave_id(struct ov5640_dev *sensor)
773 {
774 	struct i2c_client *client = sensor->i2c_client;
775 	struct i2c_msg msg;
776 	u8 buf[3];
777 	int ret;
778 
779 	if (client->addr == OV5640_DEFAULT_SLAVE_ID)
780 		return 0;
781 
782 	buf[0] = OV5640_REG_SLAVE_ID >> 8;
783 	buf[1] = OV5640_REG_SLAVE_ID & 0xff;
784 	buf[2] = client->addr << 1;
785 
786 	msg.addr = OV5640_DEFAULT_SLAVE_ID;
787 	msg.flags = 0;
788 	msg.buf = buf;
789 	msg.len = sizeof(buf);
790 
791 	ret = i2c_transfer(client->adapter, &msg, 1);
792 	if (ret < 0) {
793 		dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
794 		return ret;
795 	}
796 
797 	return 0;
798 }
799 
800 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
801 {
802 	struct i2c_client *client = sensor->i2c_client;
803 	struct i2c_msg msg;
804 	u8 buf[3];
805 	int ret;
806 
807 	buf[0] = reg >> 8;
808 	buf[1] = reg & 0xff;
809 	buf[2] = val;
810 
811 	msg.addr = client->addr;
812 	msg.flags = client->flags;
813 	msg.buf = buf;
814 	msg.len = sizeof(buf);
815 
816 	ret = i2c_transfer(client->adapter, &msg, 1);
817 	if (ret < 0) {
818 		v4l2_err(&sensor->sd, "%s: error: reg=%x, val=%x\n",
819 			__func__, reg, val);
820 		return ret;
821 	}
822 
823 	return 0;
824 }
825 
826 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
827 {
828 	struct i2c_client *client = sensor->i2c_client;
829 	struct i2c_msg msg[2];
830 	u8 buf[2];
831 	int ret;
832 
833 	buf[0] = reg >> 8;
834 	buf[1] = reg & 0xff;
835 
836 	msg[0].addr = client->addr;
837 	msg[0].flags = client->flags;
838 	msg[0].buf = buf;
839 	msg[0].len = sizeof(buf);
840 
841 	msg[1].addr = client->addr;
842 	msg[1].flags = client->flags | I2C_M_RD;
843 	msg[1].buf = buf;
844 	msg[1].len = 1;
845 
846 	ret = i2c_transfer(client->adapter, msg, 2);
847 	if (ret < 0)
848 		return ret;
849 
850 	*val = buf[0];
851 	return 0;
852 }
853 
854 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
855 {
856 	u8 hi, lo;
857 	int ret;
858 
859 	ret = ov5640_read_reg(sensor, reg, &hi);
860 	if (ret)
861 		return ret;
862 	ret = ov5640_read_reg(sensor, reg+1, &lo);
863 	if (ret)
864 		return ret;
865 
866 	*val = ((u16)hi << 8) | (u16)lo;
867 	return 0;
868 }
869 
870 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
871 {
872 	int ret;
873 
874 	ret = ov5640_write_reg(sensor, reg, val >> 8);
875 	if (ret)
876 		return ret;
877 
878 	return ov5640_write_reg(sensor, reg + 1, val & 0xff);
879 }
880 
881 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
882 			  u8 mask, u8 val)
883 {
884 	u8 readval;
885 	int ret;
886 
887 	ret = ov5640_read_reg(sensor, reg, &readval);
888 	if (ret)
889 		return ret;
890 
891 	readval &= ~mask;
892 	val &= mask;
893 	val |= readval;
894 
895 	return ov5640_write_reg(sensor, reg, val);
896 }
897 
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)
901 {
902 	const struct reg_value *regs = mode->reg_data;
903 	unsigned int i;
904 	u32 delay_ms;
905 	u16 reg_addr;
906 	u8 mask, val;
907 	int ret = 0;
908 
909 	for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
910 		delay_ms = regs->delay_ms;
911 		reg_addr = regs->reg_addr;
912 		val = regs->val;
913 		mask = regs->mask;
914 
915 		if (mask)
916 			ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
917 		else
918 			ret = ov5640_write_reg(sensor, reg_addr, val);
919 		if (ret)
920 			break;
921 
922 		if (delay_ms)
923 			usleep_range(1000*delay_ms, 1000*delay_ms+100);
924 	}
925 
926 	return ret;
927 }
928 
929 /* read exposure, in number of line periods */
930 static int ov5640_get_exposure(struct ov5640_dev *sensor)
931 {
932 	int exp, ret;
933 	u8 temp;
934 
935 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
936 	if (ret)
937 		return ret;
938 	exp = ((int)temp & 0x0f) << 16;
939 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
940 	if (ret)
941 		return ret;
942 	exp |= ((int)temp << 8);
943 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
944 	if (ret)
945 		return ret;
946 	exp |= (int)temp;
947 
948 	return exp >> 4;
949 }
950 
951 /* write exposure, given number of line periods */
952 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
953 {
954 	int ret;
955 
956 	exposure <<= 4;
957 
958 	ret = ov5640_write_reg(sensor,
959 			       OV5640_REG_AEC_PK_EXPOSURE_LO,
960 			       exposure & 0xff);
961 	if (ret)
962 		return ret;
963 	ret = ov5640_write_reg(sensor,
964 			       OV5640_REG_AEC_PK_EXPOSURE_MED,
965 			       (exposure >> 8) & 0xff);
966 	if (ret)
967 		return ret;
968 	return ov5640_write_reg(sensor,
969 				OV5640_REG_AEC_PK_EXPOSURE_HI,
970 				(exposure >> 16) & 0x0f);
971 }
972 
973 static int ov5640_get_gain(struct ov5640_dev *sensor)
974 {
975 	u16 gain;
976 	int ret;
977 
978 	ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
979 	if (ret)
980 		return ret;
981 
982 	return gain & 0x3ff;
983 }
984 
985 static int ov5640_set_stream(struct ov5640_dev *sensor, bool on)
986 {
987 	int ret;
988 
989 	ret = ov5640_mod_reg(sensor, OV5640_REG_MIPI_CTRL00, BIT(5),
990 			     on ? 0 : BIT(5));
991 	if (ret)
992 		return ret;
993 	ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00,
994 			       on ? 0x00 : 0x70);
995 	if (ret)
996 		return ret;
997 
998 	return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
999 				on ? 0x00 : 0x0f);
1000 }
1001 
1002 static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1003 {
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;
1009 	u8 temp1, temp2;
1010 	int ret;
1011 
1012 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1013 	if (ret)
1014 		return ret;
1015 	temp2 = temp1 & 0x0f;
1016 	if (temp2 == 8 || temp2 == 10)
1017 		bit_div2x = temp2 / 2;
1018 
1019 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1020 	if (ret)
1021 		return ret;
1022 	sysdiv = temp1 >> 4;
1023 	if (sysdiv == 0)
1024 		sysdiv = 16;
1025 
1026 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1027 	if (ret)
1028 		return ret;
1029 	multiplier = temp1;
1030 
1031 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1032 	if (ret)
1033 		return ret;
1034 	prediv = temp1 & 0x0f;
1035 	pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1036 
1037 	ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1038 	if (ret)
1039 		return ret;
1040 	temp2 = temp1 & 0x03;
1041 	sclk_rdiv = sclk_rdiv_map[temp2];
1042 
1043 	if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1044 		return -EINVAL;
1045 
1046 	VCO = xvclk * multiplier / prediv;
1047 
1048 	sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1049 
1050 	return sysclk;
1051 }
1052 
1053 static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1054 {
1055 	 /* read HTS from register settings */
1056 	u8 mode;
1057 	int ret;
1058 
1059 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1060 	if (ret)
1061 		return ret;
1062 	mode &= 0xfb;
1063 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1064 }
1065 
1066 static int ov5640_get_hts(struct ov5640_dev *sensor)
1067 {
1068 	/* read HTS from register settings */
1069 	u16 hts;
1070 	int ret;
1071 
1072 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1073 	if (ret)
1074 		return ret;
1075 	return hts;
1076 }
1077 
1078 static int ov5640_get_vts(struct ov5640_dev *sensor)
1079 {
1080 	u16 vts;
1081 	int ret;
1082 
1083 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1084 	if (ret)
1085 		return ret;
1086 	return vts;
1087 }
1088 
1089 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1090 {
1091 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1092 }
1093 
1094 static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1095 {
1096 	/* get banding filter value */
1097 	int ret, light_freq = 0;
1098 	u8 temp, temp1;
1099 
1100 	ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1101 	if (ret)
1102 		return ret;
1103 
1104 	if (temp & 0x80) {
1105 		/* manual */
1106 		ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1107 				      &temp1);
1108 		if (ret)
1109 			return ret;
1110 		if (temp1 & 0x04) {
1111 			/* 50Hz */
1112 			light_freq = 50;
1113 		} else {
1114 			/* 60Hz */
1115 			light_freq = 60;
1116 		}
1117 	} else {
1118 		/* auto */
1119 		ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1120 				      &temp1);
1121 		if (ret)
1122 			return ret;
1123 
1124 		if (temp1 & 0x01) {
1125 			/* 50Hz */
1126 			light_freq = 50;
1127 		} else {
1128 			/* 60Hz */
1129 		}
1130 	}
1131 
1132 	return light_freq;
1133 }
1134 
1135 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1136 {
1137 	u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1138 	int ret;
1139 
1140 	/* read preview PCLK */
1141 	ret = ov5640_get_sysclk(sensor);
1142 	if (ret < 0)
1143 		return ret;
1144 	if (ret == 0)
1145 		return -EINVAL;
1146 	sensor->prev_sysclk = ret;
1147 	/* read preview HTS */
1148 	ret = ov5640_get_hts(sensor);
1149 	if (ret < 0)
1150 		return ret;
1151 	if (ret == 0)
1152 		return -EINVAL;
1153 	sensor->prev_hts = ret;
1154 
1155 	/* read preview VTS */
1156 	ret = ov5640_get_vts(sensor);
1157 	if (ret < 0)
1158 		return ret;
1159 	prev_vts = ret;
1160 
1161 
1162 	/* calculate banding filter */
1163 	/* 60Hz */
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);
1166 	if (ret)
1167 		return ret;
1168 	if (!band_step60)
1169 		return -EINVAL;
1170 	max_band60 = (int)((prev_vts - 4) / band_step60);
1171 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
1172 	if (ret)
1173 		return ret;
1174 
1175 	/* 50Hz */
1176 	band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
1177 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
1178 	if (ret)
1179 		return ret;
1180 	if (!band_step50)
1181 		return -EINVAL;
1182 	max_band50 = (int)((prev_vts - 4) / band_step50);
1183 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
1184 }
1185 
1186 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
1187 {
1188 	/* stable in high */
1189 	u32 fast_high, fast_low;
1190 	int ret;
1191 
1192 	sensor->ae_low = target * 23 / 25;	/* 0.92 */
1193 	sensor->ae_high = target * 27 / 25;	/* 1.08 */
1194 
1195 	fast_high = sensor->ae_high << 1;
1196 	if (fast_high > 255)
1197 		fast_high = 255;
1198 
1199 	fast_low = sensor->ae_low >> 1;
1200 
1201 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
1202 	if (ret)
1203 		return ret;
1204 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
1205 	if (ret)
1206 		return ret;
1207 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
1208 	if (ret)
1209 		return ret;
1210 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
1211 	if (ret)
1212 		return ret;
1213 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
1214 	if (ret)
1215 		return ret;
1216 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
1217 }
1218 
1219 static int ov5640_binning_on(struct ov5640_dev *sensor)
1220 {
1221 	u8 temp;
1222 	int ret;
1223 
1224 	ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
1225 	if (ret)
1226 		return ret;
1227 	temp &= 0xfe;
1228 	return temp ? 1 : 0;
1229 }
1230 
1231 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
1232 {
1233 	u8 temp, channel = virtual_channel;
1234 	int ret;
1235 
1236 	if (channel > 3)
1237 		return -EINVAL;
1238 
1239 	ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
1240 	if (ret)
1241 		return ret;
1242 	temp &= ~(3 << 6);
1243 	temp |= (channel << 6);
1244 	return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
1245 }
1246 
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)
1250 {
1251 	const struct ov5640_mode_info *mode = NULL;
1252 	int i;
1253 
1254 	for (i = OV5640_NUM_MODES - 1; i >= 0; i--) {
1255 		mode = &ov5640_mode_data[fr][i];
1256 
1257 		if (!mode->reg_data)
1258 			continue;
1259 
1260 		if ((nearest && mode->width <= width &&
1261 		     mode->height <= height) ||
1262 		    (!nearest && mode->width == width &&
1263 		     mode->height == height))
1264 			break;
1265 	}
1266 
1267 	if (nearest && i < 0)
1268 		mode = &ov5640_mode_data[fr][0];
1269 
1270 	return mode;
1271 }
1272 
1273 /*
1274  * sensor changes between scaling and subsampling, go through
1275  * exposure calculation
1276  */
1277 static int ov5640_set_mode_exposure_calc(
1278 	struct ov5640_dev *sensor, const struct ov5640_mode_info *mode)
1279 {
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;
1285 	u8 average;
1286 	int ret;
1287 
1288 	if (mode->reg_data == NULL)
1289 		return -EINVAL;
1290 
1291 	/* read preview shutter */
1292 	ret = ov5640_get_exposure(sensor);
1293 	if (ret < 0)
1294 		return ret;
1295 	prev_shutter = ret;
1296 	ret = ov5640_binning_on(sensor);
1297 	if (ret < 0)
1298 		return ret;
1299 	if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
1300 	    mode->id != OV5640_MODE_1080P_1920_1080)
1301 		prev_shutter *= 2;
1302 
1303 	/* read preview gain */
1304 	ret = ov5640_get_gain(sensor);
1305 	if (ret < 0)
1306 		return ret;
1307 	prev_gain16 = ret;
1308 
1309 	/* get average */
1310 	ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
1311 	if (ret)
1312 		return ret;
1313 
1314 	/* turn off night mode for capture */
1315 	ret = ov5640_set_night_mode(sensor);
1316 	if (ret < 0)
1317 		return ret;
1318 
1319 	/* Write capture setting */
1320 	ret = ov5640_load_regs(sensor, mode);
1321 	if (ret < 0)
1322 		return ret;
1323 
1324 	/* read capture VTS */
1325 	ret = ov5640_get_vts(sensor);
1326 	if (ret < 0)
1327 		return ret;
1328 	cap_vts = ret;
1329 	ret = ov5640_get_hts(sensor);
1330 	if (ret < 0)
1331 		return ret;
1332 	if (ret == 0)
1333 		return -EINVAL;
1334 	cap_hts = ret;
1335 
1336 	ret = ov5640_get_sysclk(sensor);
1337 	if (ret < 0)
1338 		return ret;
1339 	if (ret == 0)
1340 		return -EINVAL;
1341 	cap_sysclk = ret;
1342 
1343 	/* calculate capture banding filter */
1344 	ret = ov5640_get_light_freq(sensor);
1345 	if (ret < 0)
1346 		return ret;
1347 	light_freq = ret;
1348 
1349 	if (light_freq == 60) {
1350 		/* 60Hz */
1351 		cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
1352 	} else {
1353 		/* 50Hz */
1354 		cap_bandfilt = cap_sysclk * 100 / cap_hts;
1355 	}
1356 
1357 	if (!sensor->prev_sysclk) {
1358 		ret = ov5640_get_sysclk(sensor);
1359 		if (ret < 0)
1360 			return ret;
1361 		if (ret == 0)
1362 			return -EINVAL;
1363 		sensor->prev_sysclk = ret;
1364 	}
1365 
1366 	if (!cap_bandfilt)
1367 		return -EINVAL;
1368 
1369 	cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
1370 
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;
1379 	} else {
1380 		cap_gain16_shutter =
1381 			prev_gain16 * prev_shutter *
1382 			cap_sysclk / sensor->prev_sysclk *
1383 			sensor->prev_hts / cap_hts;
1384 	}
1385 
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)
1391 			cap_shutter = 1;
1392 
1393 		cap_gain16 = cap_gain16_shutter / cap_shutter;
1394 		if (cap_gain16 < 16)
1395 			cap_gain16 = 16;
1396 	} else {
1397 		if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
1398 			/* exposure reach max */
1399 			cap_shutter = cap_bandfilt * cap_maxband;
1400 			if (!cap_shutter)
1401 				return -EINVAL;
1402 
1403 			cap_gain16 = cap_gain16_shutter / cap_shutter;
1404 		} else {
1405 			/* 1/100 < (cap_shutter = n/100) =< max */
1406 			cap_shutter =
1407 				((int)(cap_gain16_shutter / 16 / cap_bandfilt))
1408 				* cap_bandfilt;
1409 			if (!cap_shutter)
1410 				return -EINVAL;
1411 
1412 			cap_gain16 = cap_gain16_shutter / cap_shutter;
1413 		}
1414 	}
1415 
1416 	/* set capture gain */
1417 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.gain, cap_gain16);
1418 	if (ret)
1419 		return ret;
1420 
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);
1425 		if (ret < 0)
1426 			return ret;
1427 	}
1428 
1429 	/* set exposure */
1430 	return __v4l2_ctrl_s_ctrl(sensor->ctrls.exposure, cap_shutter);
1431 }
1432 
1433 /*
1434  * if sensor changes inside scaling or subsampling
1435  * change mode directly
1436  */
1437 static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
1438 				  const struct ov5640_mode_info *mode)
1439 {
1440 	int ret;
1441 
1442 	if (mode->reg_data == NULL)
1443 		return -EINVAL;
1444 
1445 	/* Write capture setting */
1446 	ret = ov5640_load_regs(sensor, mode);
1447 	if (ret < 0)
1448 		return ret;
1449 
1450 	/* turn auto gain/exposure back on for direct mode */
1451 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 1);
1452 	if (ret)
1453 		return ret;
1454 	return __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, V4L2_EXPOSURE_AUTO);
1455 }
1456 
1457 static int ov5640_set_mode(struct ov5640_dev *sensor,
1458 			   const struct ov5640_mode_info *orig_mode)
1459 {
1460 	const struct ov5640_mode_info *mode = sensor->current_mode;
1461 	enum ov5640_downsize_mode dn_mode, orig_dn_mode;
1462 	int ret;
1463 
1464 	dn_mode = mode->dn_mode;
1465 	orig_dn_mode = orig_mode->dn_mode;
1466 
1467 	/* auto gain and exposure must be turned off when changing modes */
1468 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 0);
1469 	if (ret)
1470 		return ret;
1471 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, V4L2_EXPOSURE_MANUAL);
1472 	if (ret)
1473 		return ret;
1474 
1475 	if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
1476 	    (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
1477 		/*
1478 		 * change between subsampling and scaling
1479 		 * go through exposure calucation
1480 		 */
1481 		ret = ov5640_set_mode_exposure_calc(sensor, mode);
1482 	} else {
1483 		/*
1484 		 * change inside subsampling or scaling
1485 		 * download firmware directly
1486 		 */
1487 		ret = ov5640_set_mode_direct(sensor, mode);
1488 	}
1489 
1490 	if (ret < 0)
1491 		return ret;
1492 
1493 	ret = ov5640_set_ae_target(sensor, sensor->ae_target);
1494 	if (ret < 0)
1495 		return ret;
1496 	ret = ov5640_get_light_freq(sensor);
1497 	if (ret < 0)
1498 		return ret;
1499 	ret = ov5640_set_bandingfilter(sensor);
1500 	if (ret < 0)
1501 		return ret;
1502 	ret = ov5640_set_virtual_channel(sensor);
1503 	if (ret < 0)
1504 		return ret;
1505 
1506 	sensor->pending_mode_change = false;
1507 
1508 	return 0;
1509 }
1510 
1511 /* restore the last set video mode after chip power-on */
1512 static int ov5640_restore_mode(struct ov5640_dev *sensor)
1513 {
1514 	int ret;
1515 
1516 	/* first load the initial register values */
1517 	ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
1518 	if (ret < 0)
1519 		return ret;
1520 
1521 	/* now restore the last capture mode */
1522 	return ov5640_set_mode(sensor, &ov5640_mode_init_data);
1523 }
1524 
1525 static void ov5640_power(struct ov5640_dev *sensor, bool enable)
1526 {
1527 	if (sensor->pwdn_gpio)
1528 		gpiod_set_value(sensor->pwdn_gpio, enable ? 0 : 1);
1529 }
1530 
1531 static void ov5640_reset(struct ov5640_dev *sensor)
1532 {
1533 	if (!sensor->reset_gpio)
1534 		return;
1535 
1536 	gpiod_set_value(sensor->reset_gpio, 0);
1537 
1538 	/* camera power cycle */
1539 	ov5640_power(sensor, false);
1540 	usleep_range(5000, 10000);
1541 	ov5640_power(sensor, true);
1542 	usleep_range(5000, 10000);
1543 
1544 	gpiod_set_value(sensor->reset_gpio, 1);
1545 	usleep_range(1000, 2000);
1546 
1547 	gpiod_set_value(sensor->reset_gpio, 0);
1548 	usleep_range(5000, 10000);
1549 }
1550 
1551 static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
1552 {
1553 	int ret = 0;
1554 
1555 	if (on) {
1556 		clk_prepare_enable(sensor->xclk);
1557 
1558 		ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
1559 					    sensor->supplies);
1560 		if (ret)
1561 			goto xclk_off;
1562 
1563 		ov5640_reset(sensor);
1564 		ov5640_power(sensor, true);
1565 
1566 		ret = ov5640_init_slave_id(sensor);
1567 		if (ret)
1568 			goto power_off;
1569 
1570 		ret = ov5640_restore_mode(sensor);
1571 		if (ret)
1572 			goto power_off;
1573 
1574 		/*
1575 		 * start streaming briefly followed by stream off in
1576 		 * order to coax the clock lane into LP-11 state.
1577 		 */
1578 		ret = ov5640_set_stream(sensor, true);
1579 		if (ret)
1580 			goto power_off;
1581 		usleep_range(1000, 2000);
1582 		ret = ov5640_set_stream(sensor, false);
1583 		if (ret)
1584 			goto power_off;
1585 
1586 		return 0;
1587 	}
1588 
1589 power_off:
1590 	ov5640_power(sensor, false);
1591 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
1592 xclk_off:
1593 	clk_disable_unprepare(sensor->xclk);
1594 	return ret;
1595 }
1596 
1597 /* --------------- Subdev Operations --------------- */
1598 
1599 static int ov5640_s_power(struct v4l2_subdev *sd, int on)
1600 {
1601 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
1602 	int ret = 0;
1603 
1604 	mutex_lock(&sensor->lock);
1605 
1606 	/*
1607 	 * If the power count is modified from 0 to != 0 or from != 0 to 0,
1608 	 * update the power state.
1609 	 */
1610 	if (sensor->power_count == !on) {
1611 		ret = ov5640_set_power(sensor, !!on);
1612 		if (ret)
1613 			goto out;
1614 	}
1615 
1616 	/* Update the power count. */
1617 	sensor->power_count += on ? 1 : -1;
1618 	WARN_ON(sensor->power_count < 0);
1619 out:
1620 	mutex_unlock(&sensor->lock);
1621 
1622 	if (on && !ret && sensor->power_count == 1) {
1623 		/* restore controls */
1624 		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
1625 	}
1626 
1627 	return ret;
1628 }
1629 
1630 static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
1631 				     struct v4l2_fract *fi,
1632 				     u32 width, u32 height)
1633 {
1634 	const struct ov5640_mode_info *mode;
1635 	u32 minfps, maxfps, fps;
1636 	int ret;
1637 
1638 	minfps = ov5640_framerates[OV5640_15_FPS];
1639 	maxfps = ov5640_framerates[OV5640_30_FPS];
1640 
1641 	if (fi->numerator == 0) {
1642 		fi->denominator = maxfps;
1643 		fi->numerator = 1;
1644 		return OV5640_30_FPS;
1645 	}
1646 
1647 	fps = DIV_ROUND_CLOSEST(fi->denominator, fi->numerator);
1648 
1649 	fi->numerator = 1;
1650 	if (fps > maxfps)
1651 		fi->denominator = maxfps;
1652 	else if (fps < minfps)
1653 		fi->denominator = minfps;
1654 	else if (2 * fps >= 2 * minfps + (maxfps - minfps))
1655 		fi->denominator = maxfps;
1656 	else
1657 		fi->denominator = minfps;
1658 
1659 	ret = (fi->denominator == minfps) ? OV5640_15_FPS : OV5640_30_FPS;
1660 
1661 	mode = ov5640_find_mode(sensor, ret, width, height, false);
1662 	return mode ? ret : -EINVAL;
1663 }
1664 
1665 static int ov5640_get_fmt(struct v4l2_subdev *sd,
1666 			  struct v4l2_subdev_pad_config *cfg,
1667 			  struct v4l2_subdev_format *format)
1668 {
1669 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
1670 	struct v4l2_mbus_framefmt *fmt;
1671 
1672 	if (format->pad != 0)
1673 		return -EINVAL;
1674 
1675 	mutex_lock(&sensor->lock);
1676 
1677 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1678 		fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
1679 						 format->pad);
1680 	else
1681 		fmt = &sensor->fmt;
1682 
1683 	format->format = *fmt;
1684 
1685 	mutex_unlock(&sensor->lock);
1686 
1687 	return 0;
1688 }
1689 
1690 static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
1691 				   struct v4l2_mbus_framefmt *fmt,
1692 				   enum ov5640_frame_rate fr,
1693 				   const struct ov5640_mode_info **new_mode)
1694 {
1695 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
1696 	const struct ov5640_mode_info *mode;
1697 
1698 	mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
1699 	if (!mode)
1700 		return -EINVAL;
1701 
1702 	fmt->width = mode->width;
1703 	fmt->height = mode->height;
1704 	fmt->code = sensor->fmt.code;
1705 
1706 	if (new_mode)
1707 		*new_mode = mode;
1708 	return 0;
1709 }
1710 
1711 static int ov5640_set_fmt(struct v4l2_subdev *sd,
1712 			  struct v4l2_subdev_pad_config *cfg,
1713 			  struct v4l2_subdev_format *format)
1714 {
1715 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
1716 	const struct ov5640_mode_info *new_mode;
1717 	int ret;
1718 
1719 	if (format->pad != 0)
1720 		return -EINVAL;
1721 
1722 	mutex_lock(&sensor->lock);
1723 
1724 	if (sensor->streaming) {
1725 		ret = -EBUSY;
1726 		goto out;
1727 	}
1728 
1729 	ret = ov5640_try_fmt_internal(sd, &format->format,
1730 				      sensor->current_fr, &new_mode);
1731 	if (ret)
1732 		goto out;
1733 
1734 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1735 		struct v4l2_mbus_framefmt *fmt =
1736 			v4l2_subdev_get_try_format(sd, cfg, 0);
1737 
1738 		*fmt = format->format;
1739 		goto out;
1740 	}
1741 
1742 	sensor->current_mode = new_mode;
1743 	sensor->fmt = format->format;
1744 	sensor->pending_mode_change = true;
1745 out:
1746 	mutex_unlock(&sensor->lock);
1747 	return ret;
1748 }
1749 
1750 
1751 /*
1752  * Sensor Controls.
1753  */
1754 
1755 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
1756 {
1757 	int ret;
1758 
1759 	if (value) {
1760 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
1761 				     BIT(0), BIT(0));
1762 		if (ret)
1763 			return ret;
1764 		ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
1765 	} else {
1766 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
1767 	}
1768 
1769 	return ret;
1770 }
1771 
1772 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
1773 {
1774 	int ret;
1775 
1776 	if (value) {
1777 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
1778 				     BIT(2), BIT(2));
1779 		if (ret)
1780 			return ret;
1781 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
1782 				       value & 0xff);
1783 	} else {
1784 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
1785 	}
1786 
1787 	return ret;
1788 }
1789 
1790 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
1791 {
1792 	int ret;
1793 
1794 	if (value) {
1795 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
1796 				     BIT(1), BIT(1));
1797 		if (ret)
1798 			return ret;
1799 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
1800 				       value & 0xff);
1801 		if (ret)
1802 			return ret;
1803 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
1804 				       value & 0xff);
1805 	} else {
1806 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
1807 	}
1808 
1809 	return ret;
1810 }
1811 
1812 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
1813 {
1814 	int ret;
1815 
1816 	ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
1817 			     BIT(0), awb ? 0 : 1);
1818 	if (ret)
1819 		return ret;
1820 
1821 	if (!awb) {
1822 		u16 red = (u16)sensor->ctrls.red_balance->val;
1823 		u16 blue = (u16)sensor->ctrls.blue_balance->val;
1824 
1825 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
1826 		if (ret)
1827 			return ret;
1828 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
1829 	}
1830 
1831 	return ret;
1832 }
1833 
1834 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, int exp)
1835 {
1836 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
1837 	bool auto_exposure = (exp == V4L2_EXPOSURE_AUTO);
1838 	int ret = 0;
1839 
1840 	if (ctrls->auto_exp->is_new) {
1841 		ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1842 				     BIT(0), auto_exposure ? 0 : BIT(0));
1843 		if (ret)
1844 			return ret;
1845 	}
1846 
1847 	if (!auto_exposure && ctrls->exposure->is_new) {
1848 		u16 max_exp;
1849 
1850 		ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
1851 					&max_exp);
1852 		if (ret)
1853 			return ret;
1854 		ret = ov5640_get_vts(sensor);
1855 		if (ret < 0)
1856 			return ret;
1857 		max_exp += ret;
1858 
1859 		if (ctrls->exposure->val < max_exp)
1860 			ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
1861 	}
1862 
1863 	return ret;
1864 }
1865 
1866 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, int auto_gain)
1867 {
1868 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
1869 	int ret = 0;
1870 
1871 	if (ctrls->auto_gain->is_new) {
1872 		ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1873 				     BIT(1), ctrls->auto_gain->val ? 0 : BIT(1));
1874 		if (ret)
1875 			return ret;
1876 	}
1877 
1878 	if (!auto_gain && ctrls->gain->is_new) {
1879 		u16 gain = (u16)ctrls->gain->val;
1880 
1881 		ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1882 					 gain & 0x3ff);
1883 	}
1884 
1885 	return ret;
1886 }
1887 
1888 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
1889 {
1890 	return ov5640_mod_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
1891 			      0xa4, value ? 0xa4 : 0);
1892 }
1893 
1894 static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1895 {
1896 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1897 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
1898 	int val;
1899 
1900 	/* v4l2_ctrl_lock() locks our own mutex */
1901 
1902 	switch (ctrl->id) {
1903 	case V4L2_CID_AUTOGAIN:
1904 		if (!ctrl->val)
1905 			return 0;
1906 		val = ov5640_get_gain(sensor);
1907 		if (val < 0)
1908 			return val;
1909 		sensor->ctrls.gain->val = val;
1910 		break;
1911 	case V4L2_CID_EXPOSURE_AUTO:
1912 		if (ctrl->val == V4L2_EXPOSURE_MANUAL)
1913 			return 0;
1914 		val = ov5640_get_exposure(sensor);
1915 		if (val < 0)
1916 			return val;
1917 		sensor->ctrls.exposure->val = val;
1918 		break;
1919 	}
1920 
1921 	return 0;
1922 }
1923 
1924 static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
1925 {
1926 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1927 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
1928 	int ret;
1929 
1930 	/* v4l2_ctrl_lock() locks our own mutex */
1931 
1932 	/*
1933 	 * If the device is not powered up by the host driver do
1934 	 * not apply any controls to H/W at this time. Instead
1935 	 * the controls will be restored right after power-up.
1936 	 */
1937 	if (sensor->power_count == 0)
1938 		return 0;
1939 
1940 	switch (ctrl->id) {
1941 	case V4L2_CID_AUTOGAIN:
1942 		ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
1943 		break;
1944 	case V4L2_CID_EXPOSURE_AUTO:
1945 		ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
1946 		break;
1947 	case V4L2_CID_AUTO_WHITE_BALANCE:
1948 		ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
1949 		break;
1950 	case V4L2_CID_HUE:
1951 		ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
1952 		break;
1953 	case V4L2_CID_CONTRAST:
1954 		ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
1955 		break;
1956 	case V4L2_CID_SATURATION:
1957 		ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
1958 		break;
1959 	case V4L2_CID_TEST_PATTERN:
1960 		ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
1961 		break;
1962 	default:
1963 		ret = -EINVAL;
1964 		break;
1965 	}
1966 
1967 	return ret;
1968 }
1969 
1970 static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
1971 	.g_volatile_ctrl = ov5640_g_volatile_ctrl,
1972 	.s_ctrl = ov5640_s_ctrl,
1973 };
1974 
1975 static const char * const test_pattern_menu[] = {
1976 	"Disabled",
1977 	"Color bars",
1978 };
1979 
1980 static int ov5640_init_controls(struct ov5640_dev *sensor)
1981 {
1982 	const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
1983 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
1984 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
1985 	int ret;
1986 
1987 	v4l2_ctrl_handler_init(hdl, 32);
1988 
1989 	/* we can use our own mutex for the ctrl lock */
1990 	hdl->lock = &sensor->lock;
1991 
1992 	/* Auto/manual white balance */
1993 	ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
1994 					   V4L2_CID_AUTO_WHITE_BALANCE,
1995 					   0, 1, 1, 1);
1996 	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
1997 						0, 4095, 1, 0);
1998 	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
1999 					       0, 4095, 1, 0);
2000 	/* Auto/manual exposure */
2001 	ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
2002 						 V4L2_CID_EXPOSURE_AUTO,
2003 						 V4L2_EXPOSURE_MANUAL, 0,
2004 						 V4L2_EXPOSURE_AUTO);
2005 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
2006 					    0, 65535, 1, 0);
2007 	/* Auto/manual gain */
2008 	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
2009 					     0, 1, 1, 1);
2010 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
2011 					0, 1023, 1, 0);
2012 
2013 	ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
2014 					      0, 255, 1, 64);
2015 	ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
2016 				       0, 359, 1, 0);
2017 	ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
2018 					    0, 255, 1, 0);
2019 	ctrls->test_pattern =
2020 		v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
2021 					     ARRAY_SIZE(test_pattern_menu) - 1,
2022 					     0, 0, test_pattern_menu);
2023 
2024 	if (hdl->error) {
2025 		ret = hdl->error;
2026 		goto free_ctrls;
2027 	}
2028 
2029 	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
2030 	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
2031 
2032 	v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
2033 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
2034 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
2035 
2036 	sensor->sd.ctrl_handler = hdl;
2037 	return 0;
2038 
2039 free_ctrls:
2040 	v4l2_ctrl_handler_free(hdl);
2041 	return ret;
2042 }
2043 
2044 static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
2045 				  struct v4l2_subdev_pad_config *cfg,
2046 				  struct v4l2_subdev_frame_size_enum *fse)
2047 {
2048 	if (fse->pad != 0)
2049 		return -EINVAL;
2050 	if (fse->index >= OV5640_NUM_MODES)
2051 		return -EINVAL;
2052 
2053 	fse->min_width = fse->max_width =
2054 		ov5640_mode_data[0][fse->index].width;
2055 	fse->min_height = fse->max_height =
2056 		ov5640_mode_data[0][fse->index].height;
2057 
2058 	return 0;
2059 }
2060 
2061 static int ov5640_enum_frame_interval(
2062 	struct v4l2_subdev *sd,
2063 	struct v4l2_subdev_pad_config *cfg,
2064 	struct v4l2_subdev_frame_interval_enum *fie)
2065 {
2066 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2067 	struct v4l2_fract tpf;
2068 	int ret;
2069 
2070 	if (fie->pad != 0)
2071 		return -EINVAL;
2072 	if (fie->index >= OV5640_NUM_FRAMERATES)
2073 		return -EINVAL;
2074 
2075 	tpf.numerator = 1;
2076 	tpf.denominator = ov5640_framerates[fie->index];
2077 
2078 	ret = ov5640_try_frame_interval(sensor, &tpf,
2079 					fie->width, fie->height);
2080 	if (ret < 0)
2081 		return -EINVAL;
2082 
2083 	fie->interval = tpf;
2084 	return 0;
2085 }
2086 
2087 static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
2088 				   struct v4l2_subdev_frame_interval *fi)
2089 {
2090 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2091 
2092 	mutex_lock(&sensor->lock);
2093 	fi->interval = sensor->frame_interval;
2094 	mutex_unlock(&sensor->lock);
2095 
2096 	return 0;
2097 }
2098 
2099 static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
2100 				   struct v4l2_subdev_frame_interval *fi)
2101 {
2102 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2103 	const struct ov5640_mode_info *mode;
2104 	int frame_rate, ret = 0;
2105 
2106 	if (fi->pad != 0)
2107 		return -EINVAL;
2108 
2109 	mutex_lock(&sensor->lock);
2110 
2111 	if (sensor->streaming) {
2112 		ret = -EBUSY;
2113 		goto out;
2114 	}
2115 
2116 	mode = sensor->current_mode;
2117 
2118 	frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
2119 					       mode->width, mode->height);
2120 	if (frame_rate < 0)
2121 		frame_rate = OV5640_15_FPS;
2122 
2123 	sensor->current_fr = frame_rate;
2124 	sensor->frame_interval = fi->interval;
2125 	sensor->pending_mode_change = true;
2126 out:
2127 	mutex_unlock(&sensor->lock);
2128 	return ret;
2129 }
2130 
2131 static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
2132 				  struct v4l2_subdev_pad_config *cfg,
2133 				  struct v4l2_subdev_mbus_code_enum *code)
2134 {
2135 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2136 
2137 	if (code->pad != 0)
2138 		return -EINVAL;
2139 	if (code->index != 0)
2140 		return -EINVAL;
2141 
2142 	code->code = sensor->fmt.code;
2143 
2144 	return 0;
2145 }
2146 
2147 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
2148 {
2149 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2150 	int ret = 0;
2151 
2152 	mutex_lock(&sensor->lock);
2153 
2154 	if (sensor->streaming == !enable) {
2155 		if (enable && sensor->pending_mode_change) {
2156 			ret = ov5640_set_mode(sensor, sensor->current_mode);
2157 			if (ret)
2158 				goto out;
2159 		}
2160 
2161 		ret = ov5640_set_stream(sensor, enable);
2162 		if (!ret)
2163 			sensor->streaming = enable;
2164 	}
2165 out:
2166 	mutex_unlock(&sensor->lock);
2167 	return ret;
2168 }
2169 
2170 static const struct v4l2_subdev_core_ops ov5640_core_ops = {
2171 	.s_power = ov5640_s_power,
2172 };
2173 
2174 static const struct v4l2_subdev_video_ops ov5640_video_ops = {
2175 	.g_frame_interval = ov5640_g_frame_interval,
2176 	.s_frame_interval = ov5640_s_frame_interval,
2177 	.s_stream = ov5640_s_stream,
2178 };
2179 
2180 static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
2181 	.enum_mbus_code = ov5640_enum_mbus_code,
2182 	.get_fmt = ov5640_get_fmt,
2183 	.set_fmt = ov5640_set_fmt,
2184 	.enum_frame_size = ov5640_enum_frame_size,
2185 	.enum_frame_interval = ov5640_enum_frame_interval,
2186 };
2187 
2188 static const struct v4l2_subdev_ops ov5640_subdev_ops = {
2189 	.core = &ov5640_core_ops,
2190 	.video = &ov5640_video_ops,
2191 	.pad = &ov5640_pad_ops,
2192 };
2193 
2194 static int ov5640_get_regulators(struct ov5640_dev *sensor)
2195 {
2196 	int i;
2197 
2198 	for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
2199 		sensor->supplies[i].supply = ov5640_supply_name[i];
2200 
2201 	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
2202 				       OV5640_NUM_SUPPLIES,
2203 				       sensor->supplies);
2204 }
2205 
2206 static int ov5640_probe(struct i2c_client *client,
2207 			const struct i2c_device_id *id)
2208 {
2209 	struct device *dev = &client->dev;
2210 	struct fwnode_handle *endpoint;
2211 	struct ov5640_dev *sensor;
2212 	int ret;
2213 
2214 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
2215 	if (!sensor)
2216 		return -ENOMEM;
2217 
2218 	sensor->i2c_client = client;
2219 	sensor->fmt.code = MEDIA_BUS_FMT_UYVY8_2X8;
2220 	sensor->fmt.width = 640;
2221 	sensor->fmt.height = 480;
2222 	sensor->fmt.field = V4L2_FIELD_NONE;
2223 	sensor->frame_interval.numerator = 1;
2224 	sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
2225 	sensor->current_fr = OV5640_30_FPS;
2226 	sensor->current_mode =
2227 		&ov5640_mode_data[OV5640_30_FPS][OV5640_MODE_VGA_640_480];
2228 	sensor->pending_mode_change = true;
2229 
2230 	sensor->ae_target = 52;
2231 
2232 	endpoint = fwnode_graph_get_next_endpoint(
2233 		of_fwnode_handle(client->dev.of_node), NULL);
2234 	if (!endpoint) {
2235 		dev_err(dev, "endpoint node not found\n");
2236 		return -EINVAL;
2237 	}
2238 
2239 	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
2240 	fwnode_handle_put(endpoint);
2241 	if (ret) {
2242 		dev_err(dev, "Could not parse endpoint\n");
2243 		return ret;
2244 	}
2245 
2246 	if (sensor->ep.bus_type != V4L2_MBUS_CSI2) {
2247 		dev_err(dev, "invalid bus type, must be MIPI CSI2\n");
2248 		return -EINVAL;
2249 	}
2250 
2251 	/* get system clock (xclk) */
2252 	sensor->xclk = devm_clk_get(dev, "xclk");
2253 	if (IS_ERR(sensor->xclk)) {
2254 		dev_err(dev, "failed to get xclk\n");
2255 		return PTR_ERR(sensor->xclk);
2256 	}
2257 
2258 	sensor->xclk_freq = clk_get_rate(sensor->xclk);
2259 	if (sensor->xclk_freq < OV5640_XCLK_MIN ||
2260 	    sensor->xclk_freq > OV5640_XCLK_MAX) {
2261 		dev_err(dev, "xclk frequency out of range: %d Hz\n",
2262 			sensor->xclk_freq);
2263 		return -EINVAL;
2264 	}
2265 
2266 	/* request optional power down pin */
2267 	sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
2268 						    GPIOD_OUT_HIGH);
2269 	/* request optional reset pin */
2270 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2271 						     GPIOD_OUT_HIGH);
2272 
2273 	v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
2274 
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);
2279 	if (ret)
2280 		return ret;
2281 
2282 	ret = ov5640_get_regulators(sensor);
2283 	if (ret)
2284 		return ret;
2285 
2286 	mutex_init(&sensor->lock);
2287 
2288 	ret = ov5640_init_controls(sensor);
2289 	if (ret)
2290 		goto entity_cleanup;
2291 
2292 	ret = v4l2_async_register_subdev(&sensor->sd);
2293 	if (ret)
2294 		goto free_ctrls;
2295 
2296 	return 0;
2297 
2298 free_ctrls:
2299 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2300 entity_cleanup:
2301 	mutex_destroy(&sensor->lock);
2302 	media_entity_cleanup(&sensor->sd.entity);
2303 	return ret;
2304 }
2305 
2306 static int ov5640_remove(struct i2c_client *client)
2307 {
2308 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
2309 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2310 
2311 	v4l2_async_unregister_subdev(&sensor->sd);
2312 	mutex_destroy(&sensor->lock);
2313 	media_entity_cleanup(&sensor->sd.entity);
2314 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2315 
2316 	return 0;
2317 }
2318 
2319 static const struct i2c_device_id ov5640_id[] = {
2320 	{"ov5640", 0},
2321 	{},
2322 };
2323 MODULE_DEVICE_TABLE(i2c, ov5640_id);
2324 
2325 static const struct of_device_id ov5640_dt_ids[] = {
2326 	{ .compatible = "ovti,ov5640" },
2327 	{ /* sentinel */ }
2328 };
2329 MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
2330 
2331 static struct i2c_driver ov5640_i2c_driver = {
2332 	.driver = {
2333 		.name  = "ov5640",
2334 		.of_match_table	= ov5640_dt_ids,
2335 	},
2336 	.id_table = ov5640_id,
2337 	.probe    = ov5640_probe,
2338 	.remove   = ov5640_remove,
2339 };
2340 
2341 module_i2c_driver(ov5640_i2c_driver);
2342 
2343 MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
2344 MODULE_LICENSE("GPL");
2345