xref: /openbmc/linux/drivers/media/i2c/ov5640.c (revision 160b8e75)
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_SYS_CTRL0		0x3008
38 #define OV5640_REG_CHIP_ID		0x300a
39 #define OV5640_REG_IO_MIPI_CTRL00	0x300e
40 #define OV5640_REG_PAD_OUTPUT_ENABLE01	0x3017
41 #define OV5640_REG_PAD_OUTPUT_ENABLE02	0x3018
42 #define OV5640_REG_PAD_OUTPUT00		0x3019
43 #define OV5640_REG_SYSTEM_CONTROL1	0x302e
44 #define OV5640_REG_SC_PLL_CTRL0		0x3034
45 #define OV5640_REG_SC_PLL_CTRL1		0x3035
46 #define OV5640_REG_SC_PLL_CTRL2		0x3036
47 #define OV5640_REG_SC_PLL_CTRL3		0x3037
48 #define OV5640_REG_SLAVE_ID		0x3100
49 #define OV5640_REG_SCCB_SYS_CTRL1	0x3103
50 #define OV5640_REG_SYS_ROOT_DIVIDER	0x3108
51 #define OV5640_REG_AWB_R_GAIN		0x3400
52 #define OV5640_REG_AWB_G_GAIN		0x3402
53 #define OV5640_REG_AWB_B_GAIN		0x3404
54 #define OV5640_REG_AWB_MANUAL_CTRL	0x3406
55 #define OV5640_REG_AEC_PK_EXPOSURE_HI	0x3500
56 #define OV5640_REG_AEC_PK_EXPOSURE_MED	0x3501
57 #define OV5640_REG_AEC_PK_EXPOSURE_LO	0x3502
58 #define OV5640_REG_AEC_PK_MANUAL	0x3503
59 #define OV5640_REG_AEC_PK_REAL_GAIN	0x350a
60 #define OV5640_REG_AEC_PK_VTS		0x350c
61 #define OV5640_REG_TIMING_HTS		0x380c
62 #define OV5640_REG_TIMING_VTS		0x380e
63 #define OV5640_REG_TIMING_TC_REG21	0x3821
64 #define OV5640_REG_AEC_CTRL00		0x3a00
65 #define OV5640_REG_AEC_B50_STEP		0x3a08
66 #define OV5640_REG_AEC_B60_STEP		0x3a0a
67 #define OV5640_REG_AEC_CTRL0D		0x3a0d
68 #define OV5640_REG_AEC_CTRL0E		0x3a0e
69 #define OV5640_REG_AEC_CTRL0F		0x3a0f
70 #define OV5640_REG_AEC_CTRL10		0x3a10
71 #define OV5640_REG_AEC_CTRL11		0x3a11
72 #define OV5640_REG_AEC_CTRL1B		0x3a1b
73 #define OV5640_REG_AEC_CTRL1E		0x3a1e
74 #define OV5640_REG_AEC_CTRL1F		0x3a1f
75 #define OV5640_REG_HZ5060_CTRL00	0x3c00
76 #define OV5640_REG_HZ5060_CTRL01	0x3c01
77 #define OV5640_REG_SIGMADELTA_CTRL0C	0x3c0c
78 #define OV5640_REG_FRAME_CTRL01		0x4202
79 #define OV5640_REG_FORMAT_CONTROL00	0x4300
80 #define OV5640_REG_POLARITY_CTRL00	0x4740
81 #define OV5640_REG_MIPI_CTRL00		0x4800
82 #define OV5640_REG_DEBUG_MODE		0x4814
83 #define OV5640_REG_ISP_FORMAT_MUX_CTRL	0x501f
84 #define OV5640_REG_PRE_ISP_TEST_SET1	0x503d
85 #define OV5640_REG_SDE_CTRL0		0x5580
86 #define OV5640_REG_SDE_CTRL1		0x5581
87 #define OV5640_REG_SDE_CTRL3		0x5583
88 #define OV5640_REG_SDE_CTRL4		0x5584
89 #define OV5640_REG_SDE_CTRL5		0x5585
90 #define OV5640_REG_AVG_READOUT		0x56a1
91 
92 enum ov5640_mode_id {
93 	OV5640_MODE_QCIF_176_144 = 0,
94 	OV5640_MODE_QVGA_320_240,
95 	OV5640_MODE_VGA_640_480,
96 	OV5640_MODE_NTSC_720_480,
97 	OV5640_MODE_PAL_720_576,
98 	OV5640_MODE_XGA_1024_768,
99 	OV5640_MODE_720P_1280_720,
100 	OV5640_MODE_1080P_1920_1080,
101 	OV5640_MODE_QSXGA_2592_1944,
102 	OV5640_NUM_MODES,
103 };
104 
105 enum ov5640_frame_rate {
106 	OV5640_15_FPS = 0,
107 	OV5640_30_FPS,
108 	OV5640_NUM_FRAMERATES,
109 };
110 
111 struct ov5640_pixfmt {
112 	u32 code;
113 	u32 colorspace;
114 };
115 
116 static const struct ov5640_pixfmt ov5640_formats[] = {
117 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
118 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
119 	{ MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
120 	{ MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
121 };
122 
123 /*
124  * FIXME: remove this when a subdev API becomes available
125  * to set the MIPI CSI-2 virtual channel.
126  */
127 static unsigned int virtual_channel;
128 module_param(virtual_channel, int, 0);
129 MODULE_PARM_DESC(virtual_channel,
130 		 "MIPI CSI-2 virtual channel (0..3), default 0");
131 
132 static const int ov5640_framerates[] = {
133 	[OV5640_15_FPS] = 15,
134 	[OV5640_30_FPS] = 30,
135 };
136 
137 /* regulator supplies */
138 static const char * const ov5640_supply_name[] = {
139 	"DOVDD", /* Digital I/O (1.8V) suppply */
140 	"DVDD",  /* Digital Core (1.5V) supply */
141 	"AVDD",  /* Analog (2.8V) supply */
142 };
143 
144 #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
145 
146 /*
147  * Image size under 1280 * 960 are SUBSAMPLING
148  * Image size upper 1280 * 960 are SCALING
149  */
150 enum ov5640_downsize_mode {
151 	SUBSAMPLING,
152 	SCALING,
153 };
154 
155 struct reg_value {
156 	u16 reg_addr;
157 	u8 val;
158 	u8 mask;
159 	u32 delay_ms;
160 };
161 
162 struct ov5640_mode_info {
163 	enum ov5640_mode_id id;
164 	enum ov5640_downsize_mode dn_mode;
165 	u32 width;
166 	u32 height;
167 	const struct reg_value *reg_data;
168 	u32 reg_data_size;
169 };
170 
171 struct ov5640_ctrls {
172 	struct v4l2_ctrl_handler handler;
173 	struct {
174 		struct v4l2_ctrl *auto_exp;
175 		struct v4l2_ctrl *exposure;
176 	};
177 	struct {
178 		struct v4l2_ctrl *auto_wb;
179 		struct v4l2_ctrl *blue_balance;
180 		struct v4l2_ctrl *red_balance;
181 	};
182 	struct {
183 		struct v4l2_ctrl *auto_gain;
184 		struct v4l2_ctrl *gain;
185 	};
186 	struct v4l2_ctrl *brightness;
187 	struct v4l2_ctrl *saturation;
188 	struct v4l2_ctrl *contrast;
189 	struct v4l2_ctrl *hue;
190 	struct v4l2_ctrl *test_pattern;
191 };
192 
193 struct ov5640_dev {
194 	struct i2c_client *i2c_client;
195 	struct v4l2_subdev sd;
196 	struct media_pad pad;
197 	struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
198 	struct clk *xclk; /* system clock to OV5640 */
199 	u32 xclk_freq;
200 
201 	struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
202 	struct gpio_desc *reset_gpio;
203 	struct gpio_desc *pwdn_gpio;
204 
205 	/* lock to protect all members below */
206 	struct mutex lock;
207 
208 	int power_count;
209 
210 	struct v4l2_mbus_framefmt fmt;
211 
212 	const struct ov5640_mode_info *current_mode;
213 	enum ov5640_frame_rate current_fr;
214 	struct v4l2_fract frame_interval;
215 
216 	struct ov5640_ctrls ctrls;
217 
218 	u32 prev_sysclk, prev_hts;
219 	u32 ae_low, ae_high, ae_target;
220 
221 	bool pending_mode_change;
222 	bool streaming;
223 };
224 
225 static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
226 {
227 	return container_of(sd, struct ov5640_dev, sd);
228 }
229 
230 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
231 {
232 	return &container_of(ctrl->handler, struct ov5640_dev,
233 			     ctrls.handler)->sd;
234 }
235 
236 /*
237  * FIXME: all of these register tables are likely filled with
238  * entries that set the register to their power-on default values,
239  * and which are otherwise not touched by this driver. Those entries
240  * should be identified and removed to speed register load time
241  * over i2c.
242  */
243 
244 static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
245 
246 	{0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
247 	{0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
248 	{0x3034, 0x18, 0, 0}, {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0},
249 	{0x3037, 0x13, 0, 0}, {0x3108, 0x01, 0, 0}, {0x3630, 0x36, 0, 0},
250 	{0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
251 	{0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
252 	{0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
253 	{0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
254 	{0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
255 	{0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
256 	{0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
257 	{0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
258 	{0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
259 	{0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
260 	{0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
261 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
262 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
263 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
264 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
265 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
266 	{0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
267 	{0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
268 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
269 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
270 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
271 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
272 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
273 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
274 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
275 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
276 	{0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
277 	{0x300e, 0x45, 0, 0}, {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
278 	{0x501f, 0x00, 0, 0}, {0x4713, 0x03, 0, 0}, {0x4407, 0x04, 0, 0},
279 	{0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
280 	{0x4837, 0x0a, 0, 0}, {0x4800, 0x04, 0, 0}, {0x3824, 0x02, 0, 0},
281 	{0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
282 	{0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
283 	{0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
284 	{0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
285 	{0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
286 	{0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
287 	{0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
288 	{0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
289 	{0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
290 	{0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
291 	{0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
292 	{0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
293 	{0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
294 	{0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
295 	{0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
296 	{0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
297 	{0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
298 	{0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
299 	{0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
300 	{0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
301 	{0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
302 	{0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
303 	{0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
304 	{0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
305 	{0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
306 	{0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
307 	{0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
308 	{0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
309 	{0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
310 	{0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
311 	{0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
312 	{0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
313 	{0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
314 	{0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
315 	{0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
316 	{0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
317 	{0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
318 	{0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
319 	{0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
320 	{0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
321 	{0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
322 	{0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
323 	{0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
324 	{0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
325 	{0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
326 	{0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
327 	{0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
328 	{0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
329 	{0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
330 	{0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
331 };
332 
333 static const struct reg_value ov5640_setting_30fps_VGA_640_480[] = {
334 
335 	{0x3035, 0x14, 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, 0x04, 0, 0}, {0x380f, 0x38, 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, 0x0e, 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}, {0x3503, 0x00, 0, 0},
353 };
354 
355 static const struct reg_value ov5640_setting_15fps_VGA_640_480[] = {
356 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
357 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
358 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
359 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
360 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
361 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
362 	{0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
363 	{0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
364 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
365 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
366 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
367 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
368 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
369 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
370 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
371 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
372 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
373 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
374 };
375 
376 static const struct reg_value ov5640_setting_30fps_XGA_1024_768[] = {
377 
378 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
379 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
380 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
381 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
382 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
383 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
384 	{0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
385 	{0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
386 	{0x380e, 0x04, 0, 0}, {0x380f, 0x38, 0, 0}, {0x3810, 0x00, 0, 0},
387 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
388 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
389 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
390 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
391 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
392 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
393 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
394 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
395 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
396 	{0x3808, 0x04, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x03, 0, 0},
397 	{0x380b, 0x00, 0, 0}, {0x3035, 0x12, 0, 0},
398 };
399 
400 static const struct reg_value ov5640_setting_15fps_XGA_1024_768[] = {
401 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
402 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
403 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
404 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
405 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
406 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
407 	{0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
408 	{0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
409 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
410 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
411 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
412 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
413 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
414 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
415 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
416 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
417 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
418 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3808, 0x04, 0, 0},
419 	{0x3809, 0x00, 0, 0}, {0x380a, 0x03, 0, 0}, {0x380b, 0x00, 0, 0},
420 };
421 
422 static const struct reg_value ov5640_setting_30fps_QVGA_320_240[] = {
423 	{0x3035, 0x14, 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_15fps_QVGA_320_240[] = {
444 	{0x3035, 0x22, 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, 0x01, 0, 0}, {0x3809, 0x40, 0, 0}, {0x380a, 0x00, 0, 0},
451 	{0x380b, 0xf0, 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 
464 static const struct reg_value ov5640_setting_30fps_QCIF_176_144[] = {
465 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
466 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
467 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
468 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
469 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
470 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
471 	{0x3808, 0x00, 0, 0}, {0x3809, 0xb0, 0, 0}, {0x380a, 0x00, 0, 0},
472 	{0x380b, 0x90, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
473 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
474 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
475 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
476 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
477 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
478 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
479 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
480 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
481 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
482 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
483 };
484 static const struct reg_value ov5640_setting_15fps_QCIF_176_144[] = {
485 	{0x3035, 0x22, 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, 0x00, 0, 0}, {0x3809, 0xb0, 0, 0}, {0x380a, 0x00, 0, 0},
492 	{0x380b, 0x90, 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, 0x06, 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_30fps_NTSC_720_480[] = {
506 	{0x3035, 0x12, 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_15fps_NTSC_720_480[] = {
527 	{0x3035, 0x22, 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, 0x01, 0, 0},
534 	{0x380b, 0xe0, 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, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 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_30fps_PAL_720_576[] = {
548 	{0x3035, 0x12, 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_15fps_PAL_720_576[] = {
569 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
570 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
571 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
572 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
573 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
574 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
575 	{0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x02, 0, 0},
576 	{0x380b, 0x40, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
577 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
578 	{0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
579 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
580 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
581 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
582 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
583 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
584 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
585 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
586 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
587 };
588 
589 static const struct reg_value ov5640_setting_30fps_720P_1280_720[] = {
590 	{0x3008, 0x42, 0, 0},
591 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
592 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
593 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
594 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
595 	{0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
596 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
597 	{0x3808, 0x05, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x02, 0, 0},
598 	{0x380b, 0xd0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x64, 0, 0},
599 	{0x380e, 0x02, 0, 0}, {0x380f, 0xe4, 0, 0}, {0x3810, 0x00, 0, 0},
600 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
601 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
602 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
603 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
604 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
605 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
606 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
607 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
608 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, {0x4005, 0x1a, 0, 0},
609 	{0x3008, 0x02, 0, 0}, {0x3503, 0,    0, 0},
610 };
611 
612 static const struct reg_value ov5640_setting_15fps_720P_1280_720[] = {
613 	{0x3035, 0x41, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
614 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
615 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
616 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
617 	{0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
618 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
619 	{0x3808, 0x05, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x02, 0, 0},
620 	{0x380b, 0xd0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x64, 0, 0},
621 	{0x380e, 0x02, 0, 0}, {0x380f, 0xe4, 0, 0}, {0x3810, 0x00, 0, 0},
622 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
623 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
624 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
625 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
626 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
627 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
628 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
629 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
630 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
631 };
632 
633 static const struct reg_value ov5640_setting_30fps_1080P_1920_1080[] = {
634 	{0x3008, 0x42, 0, 0},
635 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
636 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
637 	{0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
638 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
639 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
640 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
641 	{0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
642 	{0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
643 	{0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
644 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
645 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
646 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
647 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
648 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
649 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
650 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
651 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
652 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x11, 0, 0},
653 	{0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
654 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
655 	{0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
656 	{0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
657 	{0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, {0x3808, 0x07, 0, 0},
658 	{0x3809, 0x80, 0, 0}, {0x380a, 0x04, 0, 0}, {0x380b, 0x38, 0, 0},
659 	{0x380c, 0x09, 0, 0}, {0x380d, 0xc4, 0, 0}, {0x380e, 0x04, 0, 0},
660 	{0x380f, 0x60, 0, 0}, {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
661 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
662 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
663 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
664 	{0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
665 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
666 	{0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0},
667 	{0x3503, 0, 0, 0},
668 };
669 
670 static const struct reg_value ov5640_setting_15fps_1080P_1920_1080[] = {
671 	{0x3008, 0x42, 0, 0},
672 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
673 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
674 	{0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
675 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
676 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
677 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
678 	{0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
679 	{0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
680 	{0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
681 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
682 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
683 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
684 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
685 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
686 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
687 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
688 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
689 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x21, 0, 0},
690 	{0x3036, 0x54, 0, 1}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
691 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
692 	{0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
693 	{0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
694 	{0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, {0x3808, 0x07, 0, 0},
695 	{0x3809, 0x80, 0, 0}, {0x380a, 0x04, 0, 0}, {0x380b, 0x38, 0, 0},
696 	{0x380c, 0x09, 0, 0}, {0x380d, 0xc4, 0, 0}, {0x380e, 0x04, 0, 0},
697 	{0x380f, 0x60, 0, 0}, {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
698 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
699 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
700 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
701 	{0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
702 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
703 	{0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0},
704 };
705 
706 static const struct reg_value ov5640_setting_15fps_QSXGA_2592_1944[] = {
707 	{0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0},
708 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
709 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
710 	{0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
711 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
712 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
713 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
714 	{0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
715 	{0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
716 	{0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
717 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
718 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
719 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
720 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
721 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
722 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
723 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
724 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
725 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
726 };
727 
728 /* power-on sensor init reg table */
729 static const struct ov5640_mode_info ov5640_mode_init_data = {
730 	0, SUBSAMPLING, 640, 480, ov5640_init_setting_30fps_VGA,
731 	ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
732 };
733 
734 static const struct ov5640_mode_info
735 ov5640_mode_data[OV5640_NUM_FRAMERATES][OV5640_NUM_MODES] = {
736 	{
737 		{OV5640_MODE_QCIF_176_144, SUBSAMPLING, 176, 144,
738 		 ov5640_setting_15fps_QCIF_176_144,
739 		 ARRAY_SIZE(ov5640_setting_15fps_QCIF_176_144)},
740 		{OV5640_MODE_QVGA_320_240, SUBSAMPLING, 320,  240,
741 		 ov5640_setting_15fps_QVGA_320_240,
742 		 ARRAY_SIZE(ov5640_setting_15fps_QVGA_320_240)},
743 		{OV5640_MODE_VGA_640_480, SUBSAMPLING, 640,  480,
744 		 ov5640_setting_15fps_VGA_640_480,
745 		 ARRAY_SIZE(ov5640_setting_15fps_VGA_640_480)},
746 		{OV5640_MODE_NTSC_720_480, SUBSAMPLING, 720, 480,
747 		 ov5640_setting_15fps_NTSC_720_480,
748 		 ARRAY_SIZE(ov5640_setting_15fps_NTSC_720_480)},
749 		{OV5640_MODE_PAL_720_576, SUBSAMPLING, 720, 576,
750 		 ov5640_setting_15fps_PAL_720_576,
751 		 ARRAY_SIZE(ov5640_setting_15fps_PAL_720_576)},
752 		{OV5640_MODE_XGA_1024_768, SUBSAMPLING, 1024, 768,
753 		 ov5640_setting_15fps_XGA_1024_768,
754 		 ARRAY_SIZE(ov5640_setting_15fps_XGA_1024_768)},
755 		{OV5640_MODE_720P_1280_720, SUBSAMPLING, 1280, 720,
756 		 ov5640_setting_15fps_720P_1280_720,
757 		 ARRAY_SIZE(ov5640_setting_15fps_720P_1280_720)},
758 		{OV5640_MODE_1080P_1920_1080, SCALING, 1920, 1080,
759 		 ov5640_setting_15fps_1080P_1920_1080,
760 		 ARRAY_SIZE(ov5640_setting_15fps_1080P_1920_1080)},
761 		{OV5640_MODE_QSXGA_2592_1944, SCALING, 2592, 1944,
762 		 ov5640_setting_15fps_QSXGA_2592_1944,
763 		 ARRAY_SIZE(ov5640_setting_15fps_QSXGA_2592_1944)},
764 	}, {
765 		{OV5640_MODE_QCIF_176_144, SUBSAMPLING, 176, 144,
766 		 ov5640_setting_30fps_QCIF_176_144,
767 		 ARRAY_SIZE(ov5640_setting_30fps_QCIF_176_144)},
768 		{OV5640_MODE_QVGA_320_240, SUBSAMPLING, 320,  240,
769 		 ov5640_setting_30fps_QVGA_320_240,
770 		 ARRAY_SIZE(ov5640_setting_30fps_QVGA_320_240)},
771 		{OV5640_MODE_VGA_640_480, SUBSAMPLING, 640,  480,
772 		 ov5640_setting_30fps_VGA_640_480,
773 		 ARRAY_SIZE(ov5640_setting_30fps_VGA_640_480)},
774 		{OV5640_MODE_NTSC_720_480, SUBSAMPLING, 720, 480,
775 		 ov5640_setting_30fps_NTSC_720_480,
776 		 ARRAY_SIZE(ov5640_setting_30fps_NTSC_720_480)},
777 		{OV5640_MODE_PAL_720_576, SUBSAMPLING, 720, 576,
778 		 ov5640_setting_30fps_PAL_720_576,
779 		 ARRAY_SIZE(ov5640_setting_30fps_PAL_720_576)},
780 		{OV5640_MODE_XGA_1024_768, SUBSAMPLING, 1024, 768,
781 		 ov5640_setting_30fps_XGA_1024_768,
782 		 ARRAY_SIZE(ov5640_setting_30fps_XGA_1024_768)},
783 		{OV5640_MODE_720P_1280_720, SUBSAMPLING, 1280, 720,
784 		 ov5640_setting_30fps_720P_1280_720,
785 		 ARRAY_SIZE(ov5640_setting_30fps_720P_1280_720)},
786 		{OV5640_MODE_1080P_1920_1080, SCALING, 1920, 1080,
787 		 ov5640_setting_30fps_1080P_1920_1080,
788 		 ARRAY_SIZE(ov5640_setting_30fps_1080P_1920_1080)},
789 		{OV5640_MODE_QSXGA_2592_1944, -1, 0, 0, NULL, 0},
790 	},
791 };
792 
793 static int ov5640_init_slave_id(struct ov5640_dev *sensor)
794 {
795 	struct i2c_client *client = sensor->i2c_client;
796 	struct i2c_msg msg;
797 	u8 buf[3];
798 	int ret;
799 
800 	if (client->addr == OV5640_DEFAULT_SLAVE_ID)
801 		return 0;
802 
803 	buf[0] = OV5640_REG_SLAVE_ID >> 8;
804 	buf[1] = OV5640_REG_SLAVE_ID & 0xff;
805 	buf[2] = client->addr << 1;
806 
807 	msg.addr = OV5640_DEFAULT_SLAVE_ID;
808 	msg.flags = 0;
809 	msg.buf = buf;
810 	msg.len = sizeof(buf);
811 
812 	ret = i2c_transfer(client->adapter, &msg, 1);
813 	if (ret < 0) {
814 		dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
815 		return ret;
816 	}
817 
818 	return 0;
819 }
820 
821 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
822 {
823 	struct i2c_client *client = sensor->i2c_client;
824 	struct i2c_msg msg;
825 	u8 buf[3];
826 	int ret;
827 
828 	buf[0] = reg >> 8;
829 	buf[1] = reg & 0xff;
830 	buf[2] = val;
831 
832 	msg.addr = client->addr;
833 	msg.flags = client->flags;
834 	msg.buf = buf;
835 	msg.len = sizeof(buf);
836 
837 	ret = i2c_transfer(client->adapter, &msg, 1);
838 	if (ret < 0) {
839 		v4l2_err(&sensor->sd, "%s: error: reg=%x, val=%x\n",
840 			__func__, reg, val);
841 		return ret;
842 	}
843 
844 	return 0;
845 }
846 
847 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
848 {
849 	struct i2c_client *client = sensor->i2c_client;
850 	struct i2c_msg msg[2];
851 	u8 buf[2];
852 	int ret;
853 
854 	buf[0] = reg >> 8;
855 	buf[1] = reg & 0xff;
856 
857 	msg[0].addr = client->addr;
858 	msg[0].flags = client->flags;
859 	msg[0].buf = buf;
860 	msg[0].len = sizeof(buf);
861 
862 	msg[1].addr = client->addr;
863 	msg[1].flags = client->flags | I2C_M_RD;
864 	msg[1].buf = buf;
865 	msg[1].len = 1;
866 
867 	ret = i2c_transfer(client->adapter, msg, 2);
868 	if (ret < 0)
869 		return ret;
870 
871 	*val = buf[0];
872 	return 0;
873 }
874 
875 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
876 {
877 	u8 hi, lo;
878 	int ret;
879 
880 	ret = ov5640_read_reg(sensor, reg, &hi);
881 	if (ret)
882 		return ret;
883 	ret = ov5640_read_reg(sensor, reg+1, &lo);
884 	if (ret)
885 		return ret;
886 
887 	*val = ((u16)hi << 8) | (u16)lo;
888 	return 0;
889 }
890 
891 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
892 {
893 	int ret;
894 
895 	ret = ov5640_write_reg(sensor, reg, val >> 8);
896 	if (ret)
897 		return ret;
898 
899 	return ov5640_write_reg(sensor, reg + 1, val & 0xff);
900 }
901 
902 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
903 			  u8 mask, u8 val)
904 {
905 	u8 readval;
906 	int ret;
907 
908 	ret = ov5640_read_reg(sensor, reg, &readval);
909 	if (ret)
910 		return ret;
911 
912 	readval &= ~mask;
913 	val &= mask;
914 	val |= readval;
915 
916 	return ov5640_write_reg(sensor, reg, val);
917 }
918 
919 /* download ov5640 settings to sensor through i2c */
920 static int ov5640_load_regs(struct ov5640_dev *sensor,
921 			    const struct ov5640_mode_info *mode)
922 {
923 	const struct reg_value *regs = mode->reg_data;
924 	unsigned int i;
925 	u32 delay_ms;
926 	u16 reg_addr;
927 	u8 mask, val;
928 	int ret = 0;
929 
930 	for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
931 		delay_ms = regs->delay_ms;
932 		reg_addr = regs->reg_addr;
933 		val = regs->val;
934 		mask = regs->mask;
935 
936 		if (mask)
937 			ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
938 		else
939 			ret = ov5640_write_reg(sensor, reg_addr, val);
940 		if (ret)
941 			break;
942 
943 		if (delay_ms)
944 			usleep_range(1000*delay_ms, 1000*delay_ms+100);
945 	}
946 
947 	return ret;
948 }
949 
950 /* read exposure, in number of line periods */
951 static int ov5640_get_exposure(struct ov5640_dev *sensor)
952 {
953 	int exp, ret;
954 	u8 temp;
955 
956 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
957 	if (ret)
958 		return ret;
959 	exp = ((int)temp & 0x0f) << 16;
960 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
961 	if (ret)
962 		return ret;
963 	exp |= ((int)temp << 8);
964 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
965 	if (ret)
966 		return ret;
967 	exp |= (int)temp;
968 
969 	return exp >> 4;
970 }
971 
972 /* write exposure, given number of line periods */
973 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
974 {
975 	int ret;
976 
977 	exposure <<= 4;
978 
979 	ret = ov5640_write_reg(sensor,
980 			       OV5640_REG_AEC_PK_EXPOSURE_LO,
981 			       exposure & 0xff);
982 	if (ret)
983 		return ret;
984 	ret = ov5640_write_reg(sensor,
985 			       OV5640_REG_AEC_PK_EXPOSURE_MED,
986 			       (exposure >> 8) & 0xff);
987 	if (ret)
988 		return ret;
989 	return ov5640_write_reg(sensor,
990 				OV5640_REG_AEC_PK_EXPOSURE_HI,
991 				(exposure >> 16) & 0x0f);
992 }
993 
994 static int ov5640_get_gain(struct ov5640_dev *sensor)
995 {
996 	u16 gain;
997 	int ret;
998 
999 	ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1000 	if (ret)
1001 		return ret;
1002 
1003 	return gain & 0x3ff;
1004 }
1005 
1006 static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1007 {
1008 	int ret;
1009 	unsigned int flags = sensor->ep.bus.parallel.flags;
1010 	u8 pclk_pol = 0;
1011 	u8 hsync_pol = 0;
1012 	u8 vsync_pol = 0;
1013 
1014 	/*
1015 	 * Note about parallel port configuration.
1016 	 *
1017 	 * When configured in parallel mode, the OV5640 will
1018 	 * output 10 bits data on DVP data lines [9:0].
1019 	 * If only 8 bits data are wanted, the 8 bits data lines
1020 	 * of the camera interface must be physically connected
1021 	 * on the DVP data lines [9:2].
1022 	 *
1023 	 * Control lines polarity can be configured through
1024 	 * devicetree endpoint control lines properties.
1025 	 * If no endpoint control lines properties are set,
1026 	 * polarity will be as below:
1027 	 * - VSYNC:	active high
1028 	 * - HREF:	active low
1029 	 * - PCLK:	active low
1030 	 */
1031 
1032 	if (on) {
1033 		/*
1034 		 * reset MIPI PCLK/SERCLK divider
1035 		 *
1036 		 * SC PLL CONTRL1 0
1037 		 * - [3..0]:	MIPI PCLK/SERCLK divider
1038 		 */
1039 		ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0x0f, 0);
1040 		if (ret)
1041 			return ret;
1042 
1043 		/*
1044 		 * configure parallel port control lines polarity
1045 		 *
1046 		 * POLARITY CTRL0
1047 		 * - [5]:	PCLK polarity (0: active low, 1: active high)
1048 		 * - [1]:	HREF polarity (0: active low, 1: active high)
1049 		 * - [0]:	VSYNC polarity (mismatch here between
1050 		 *		datasheet and hardware, 0 is active high
1051 		 *		and 1 is active low...)
1052 		 */
1053 		if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1054 			pclk_pol = 1;
1055 		if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1056 			hsync_pol = 1;
1057 		if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1058 			vsync_pol = 1;
1059 
1060 		ret = ov5640_write_reg(sensor,
1061 				       OV5640_REG_POLARITY_CTRL00,
1062 				       (pclk_pol << 5) |
1063 				       (hsync_pol << 1) |
1064 				       vsync_pol);
1065 
1066 		if (ret)
1067 			return ret;
1068 	}
1069 
1070 	/*
1071 	 * powerdown MIPI TX/RX PHY & disable MIPI
1072 	 *
1073 	 * MIPI CONTROL 00
1074 	 * 4:	 PWDN PHY TX
1075 	 * 3:	 PWDN PHY RX
1076 	 * 2:	 MIPI enable
1077 	 */
1078 	ret = ov5640_write_reg(sensor,
1079 			       OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0);
1080 	if (ret)
1081 		return ret;
1082 
1083 	/*
1084 	 * enable VSYNC/HREF/PCLK DVP control lines
1085 	 * & D[9:6] DVP data lines
1086 	 *
1087 	 * PAD OUTPUT ENABLE 01
1088 	 * - 6:		VSYNC output enable
1089 	 * - 5:		HREF output enable
1090 	 * - 4:		PCLK output enable
1091 	 * - [3:0]:	D[9:6] output enable
1092 	 */
1093 	ret = ov5640_write_reg(sensor,
1094 			       OV5640_REG_PAD_OUTPUT_ENABLE01,
1095 			       on ? 0x7f : 0);
1096 	if (ret)
1097 		return ret;
1098 
1099 	/*
1100 	 * enable D[5:0] DVP data lines
1101 	 *
1102 	 * PAD OUTPUT ENABLE 02
1103 	 * - [7:2]:	D[5:0] output enable
1104 	 */
1105 	return ov5640_write_reg(sensor,
1106 				OV5640_REG_PAD_OUTPUT_ENABLE02,
1107 				on ? 0xfc : 0);
1108 }
1109 
1110 static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
1111 {
1112 	int ret;
1113 
1114 	ret = ov5640_mod_reg(sensor, OV5640_REG_MIPI_CTRL00, BIT(5),
1115 			     on ? 0 : BIT(5));
1116 	if (ret)
1117 		return ret;
1118 	ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00,
1119 			       on ? 0x00 : 0x70);
1120 	if (ret)
1121 		return ret;
1122 
1123 	return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1124 				on ? 0x00 : 0x0f);
1125 }
1126 
1127 static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1128 {
1129 	 /* calculate sysclk */
1130 	u32 xvclk = sensor->xclk_freq / 10000;
1131 	u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1132 	u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1133 	u32 bit_div2x = 1, sclk_rdiv, sysclk;
1134 	u8 temp1, temp2;
1135 	int ret;
1136 
1137 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1138 	if (ret)
1139 		return ret;
1140 	temp2 = temp1 & 0x0f;
1141 	if (temp2 == 8 || temp2 == 10)
1142 		bit_div2x = temp2 / 2;
1143 
1144 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1145 	if (ret)
1146 		return ret;
1147 	sysdiv = temp1 >> 4;
1148 	if (sysdiv == 0)
1149 		sysdiv = 16;
1150 
1151 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1152 	if (ret)
1153 		return ret;
1154 	multiplier = temp1;
1155 
1156 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1157 	if (ret)
1158 		return ret;
1159 	prediv = temp1 & 0x0f;
1160 	pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1161 
1162 	ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1163 	if (ret)
1164 		return ret;
1165 	temp2 = temp1 & 0x03;
1166 	sclk_rdiv = sclk_rdiv_map[temp2];
1167 
1168 	if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1169 		return -EINVAL;
1170 
1171 	VCO = xvclk * multiplier / prediv;
1172 
1173 	sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1174 
1175 	return sysclk;
1176 }
1177 
1178 static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1179 {
1180 	 /* read HTS from register settings */
1181 	u8 mode;
1182 	int ret;
1183 
1184 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1185 	if (ret)
1186 		return ret;
1187 	mode &= 0xfb;
1188 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1189 }
1190 
1191 static int ov5640_get_hts(struct ov5640_dev *sensor)
1192 {
1193 	/* read HTS from register settings */
1194 	u16 hts;
1195 	int ret;
1196 
1197 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1198 	if (ret)
1199 		return ret;
1200 	return hts;
1201 }
1202 
1203 static int ov5640_get_vts(struct ov5640_dev *sensor)
1204 {
1205 	u16 vts;
1206 	int ret;
1207 
1208 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1209 	if (ret)
1210 		return ret;
1211 	return vts;
1212 }
1213 
1214 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1215 {
1216 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1217 }
1218 
1219 static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1220 {
1221 	/* get banding filter value */
1222 	int ret, light_freq = 0;
1223 	u8 temp, temp1;
1224 
1225 	ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1226 	if (ret)
1227 		return ret;
1228 
1229 	if (temp & 0x80) {
1230 		/* manual */
1231 		ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1232 				      &temp1);
1233 		if (ret)
1234 			return ret;
1235 		if (temp1 & 0x04) {
1236 			/* 50Hz */
1237 			light_freq = 50;
1238 		} else {
1239 			/* 60Hz */
1240 			light_freq = 60;
1241 		}
1242 	} else {
1243 		/* auto */
1244 		ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1245 				      &temp1);
1246 		if (ret)
1247 			return ret;
1248 
1249 		if (temp1 & 0x01) {
1250 			/* 50Hz */
1251 			light_freq = 50;
1252 		} else {
1253 			/* 60Hz */
1254 		}
1255 	}
1256 
1257 	return light_freq;
1258 }
1259 
1260 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1261 {
1262 	u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1263 	int ret;
1264 
1265 	/* read preview PCLK */
1266 	ret = ov5640_get_sysclk(sensor);
1267 	if (ret < 0)
1268 		return ret;
1269 	if (ret == 0)
1270 		return -EINVAL;
1271 	sensor->prev_sysclk = ret;
1272 	/* read preview HTS */
1273 	ret = ov5640_get_hts(sensor);
1274 	if (ret < 0)
1275 		return ret;
1276 	if (ret == 0)
1277 		return -EINVAL;
1278 	sensor->prev_hts = ret;
1279 
1280 	/* read preview VTS */
1281 	ret = ov5640_get_vts(sensor);
1282 	if (ret < 0)
1283 		return ret;
1284 	prev_vts = ret;
1285 
1286 
1287 	/* calculate banding filter */
1288 	/* 60Hz */
1289 	band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
1290 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
1291 	if (ret)
1292 		return ret;
1293 	if (!band_step60)
1294 		return -EINVAL;
1295 	max_band60 = (int)((prev_vts - 4) / band_step60);
1296 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
1297 	if (ret)
1298 		return ret;
1299 
1300 	/* 50Hz */
1301 	band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
1302 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
1303 	if (ret)
1304 		return ret;
1305 	if (!band_step50)
1306 		return -EINVAL;
1307 	max_band50 = (int)((prev_vts - 4) / band_step50);
1308 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
1309 }
1310 
1311 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
1312 {
1313 	/* stable in high */
1314 	u32 fast_high, fast_low;
1315 	int ret;
1316 
1317 	sensor->ae_low = target * 23 / 25;	/* 0.92 */
1318 	sensor->ae_high = target * 27 / 25;	/* 1.08 */
1319 
1320 	fast_high = sensor->ae_high << 1;
1321 	if (fast_high > 255)
1322 		fast_high = 255;
1323 
1324 	fast_low = sensor->ae_low >> 1;
1325 
1326 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
1327 	if (ret)
1328 		return ret;
1329 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
1330 	if (ret)
1331 		return ret;
1332 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
1333 	if (ret)
1334 		return ret;
1335 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
1336 	if (ret)
1337 		return ret;
1338 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
1339 	if (ret)
1340 		return ret;
1341 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
1342 }
1343 
1344 static int ov5640_binning_on(struct ov5640_dev *sensor)
1345 {
1346 	u8 temp;
1347 	int ret;
1348 
1349 	ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
1350 	if (ret)
1351 		return ret;
1352 	temp &= 0xfe;
1353 	return temp ? 1 : 0;
1354 }
1355 
1356 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
1357 {
1358 	u8 temp, channel = virtual_channel;
1359 	int ret;
1360 
1361 	if (channel > 3)
1362 		return -EINVAL;
1363 
1364 	ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
1365 	if (ret)
1366 		return ret;
1367 	temp &= ~(3 << 6);
1368 	temp |= (channel << 6);
1369 	return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
1370 }
1371 
1372 static const struct ov5640_mode_info *
1373 ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
1374 		 int width, int height, bool nearest)
1375 {
1376 	const struct ov5640_mode_info *mode = NULL;
1377 	int i;
1378 
1379 	for (i = OV5640_NUM_MODES - 1; i >= 0; i--) {
1380 		mode = &ov5640_mode_data[fr][i];
1381 
1382 		if (!mode->reg_data)
1383 			continue;
1384 
1385 		if ((nearest && mode->width <= width &&
1386 		     mode->height <= height) ||
1387 		    (!nearest && mode->width == width &&
1388 		     mode->height == height))
1389 			break;
1390 	}
1391 
1392 	if (nearest && i < 0)
1393 		mode = &ov5640_mode_data[fr][0];
1394 
1395 	return mode;
1396 }
1397 
1398 /*
1399  * sensor changes between scaling and subsampling, go through
1400  * exposure calculation
1401  */
1402 static int ov5640_set_mode_exposure_calc(
1403 	struct ov5640_dev *sensor, const struct ov5640_mode_info *mode)
1404 {
1405 	u32 prev_shutter, prev_gain16;
1406 	u32 cap_shutter, cap_gain16;
1407 	u32 cap_sysclk, cap_hts, cap_vts;
1408 	u32 light_freq, cap_bandfilt, cap_maxband;
1409 	u32 cap_gain16_shutter;
1410 	u8 average;
1411 	int ret;
1412 
1413 	if (mode->reg_data == NULL)
1414 		return -EINVAL;
1415 
1416 	/* read preview shutter */
1417 	ret = ov5640_get_exposure(sensor);
1418 	if (ret < 0)
1419 		return ret;
1420 	prev_shutter = ret;
1421 	ret = ov5640_binning_on(sensor);
1422 	if (ret < 0)
1423 		return ret;
1424 	if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
1425 	    mode->id != OV5640_MODE_1080P_1920_1080)
1426 		prev_shutter *= 2;
1427 
1428 	/* read preview gain */
1429 	ret = ov5640_get_gain(sensor);
1430 	if (ret < 0)
1431 		return ret;
1432 	prev_gain16 = ret;
1433 
1434 	/* get average */
1435 	ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
1436 	if (ret)
1437 		return ret;
1438 
1439 	/* turn off night mode for capture */
1440 	ret = ov5640_set_night_mode(sensor);
1441 	if (ret < 0)
1442 		return ret;
1443 
1444 	/* Write capture setting */
1445 	ret = ov5640_load_regs(sensor, mode);
1446 	if (ret < 0)
1447 		return ret;
1448 
1449 	/* read capture VTS */
1450 	ret = ov5640_get_vts(sensor);
1451 	if (ret < 0)
1452 		return ret;
1453 	cap_vts = ret;
1454 	ret = ov5640_get_hts(sensor);
1455 	if (ret < 0)
1456 		return ret;
1457 	if (ret == 0)
1458 		return -EINVAL;
1459 	cap_hts = ret;
1460 
1461 	ret = ov5640_get_sysclk(sensor);
1462 	if (ret < 0)
1463 		return ret;
1464 	if (ret == 0)
1465 		return -EINVAL;
1466 	cap_sysclk = ret;
1467 
1468 	/* calculate capture banding filter */
1469 	ret = ov5640_get_light_freq(sensor);
1470 	if (ret < 0)
1471 		return ret;
1472 	light_freq = ret;
1473 
1474 	if (light_freq == 60) {
1475 		/* 60Hz */
1476 		cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
1477 	} else {
1478 		/* 50Hz */
1479 		cap_bandfilt = cap_sysclk * 100 / cap_hts;
1480 	}
1481 
1482 	if (!sensor->prev_sysclk) {
1483 		ret = ov5640_get_sysclk(sensor);
1484 		if (ret < 0)
1485 			return ret;
1486 		if (ret == 0)
1487 			return -EINVAL;
1488 		sensor->prev_sysclk = ret;
1489 	}
1490 
1491 	if (!cap_bandfilt)
1492 		return -EINVAL;
1493 
1494 	cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
1495 
1496 	/* calculate capture shutter/gain16 */
1497 	if (average > sensor->ae_low && average < sensor->ae_high) {
1498 		/* in stable range */
1499 		cap_gain16_shutter =
1500 			prev_gain16 * prev_shutter *
1501 			cap_sysclk / sensor->prev_sysclk *
1502 			sensor->prev_hts / cap_hts *
1503 			sensor->ae_target / average;
1504 	} else {
1505 		cap_gain16_shutter =
1506 			prev_gain16 * prev_shutter *
1507 			cap_sysclk / sensor->prev_sysclk *
1508 			sensor->prev_hts / cap_hts;
1509 	}
1510 
1511 	/* gain to shutter */
1512 	if (cap_gain16_shutter < (cap_bandfilt * 16)) {
1513 		/* shutter < 1/100 */
1514 		cap_shutter = cap_gain16_shutter / 16;
1515 		if (cap_shutter < 1)
1516 			cap_shutter = 1;
1517 
1518 		cap_gain16 = cap_gain16_shutter / cap_shutter;
1519 		if (cap_gain16 < 16)
1520 			cap_gain16 = 16;
1521 	} else {
1522 		if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
1523 			/* exposure reach max */
1524 			cap_shutter = cap_bandfilt * cap_maxband;
1525 			if (!cap_shutter)
1526 				return -EINVAL;
1527 
1528 			cap_gain16 = cap_gain16_shutter / cap_shutter;
1529 		} else {
1530 			/* 1/100 < (cap_shutter = n/100) =< max */
1531 			cap_shutter =
1532 				((int)(cap_gain16_shutter / 16 / cap_bandfilt))
1533 				* cap_bandfilt;
1534 			if (!cap_shutter)
1535 				return -EINVAL;
1536 
1537 			cap_gain16 = cap_gain16_shutter / cap_shutter;
1538 		}
1539 	}
1540 
1541 	/* set capture gain */
1542 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.gain, cap_gain16);
1543 	if (ret)
1544 		return ret;
1545 
1546 	/* write capture shutter */
1547 	if (cap_shutter > (cap_vts - 4)) {
1548 		cap_vts = cap_shutter + 4;
1549 		ret = ov5640_set_vts(sensor, cap_vts);
1550 		if (ret < 0)
1551 			return ret;
1552 	}
1553 
1554 	/* set exposure */
1555 	return __v4l2_ctrl_s_ctrl(sensor->ctrls.exposure, cap_shutter);
1556 }
1557 
1558 /*
1559  * if sensor changes inside scaling or subsampling
1560  * change mode directly
1561  */
1562 static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
1563 				  const struct ov5640_mode_info *mode)
1564 {
1565 	int ret;
1566 
1567 	if (mode->reg_data == NULL)
1568 		return -EINVAL;
1569 
1570 	/* Write capture setting */
1571 	ret = ov5640_load_regs(sensor, mode);
1572 	if (ret < 0)
1573 		return ret;
1574 
1575 	/* turn auto gain/exposure back on for direct mode */
1576 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 1);
1577 	if (ret)
1578 		return ret;
1579 	return __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, V4L2_EXPOSURE_AUTO);
1580 }
1581 
1582 static int ov5640_set_mode(struct ov5640_dev *sensor,
1583 			   const struct ov5640_mode_info *orig_mode)
1584 {
1585 	const struct ov5640_mode_info *mode = sensor->current_mode;
1586 	enum ov5640_downsize_mode dn_mode, orig_dn_mode;
1587 	int ret;
1588 
1589 	dn_mode = mode->dn_mode;
1590 	orig_dn_mode = orig_mode->dn_mode;
1591 
1592 	/* auto gain and exposure must be turned off when changing modes */
1593 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 0);
1594 	if (ret)
1595 		return ret;
1596 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, V4L2_EXPOSURE_MANUAL);
1597 	if (ret)
1598 		return ret;
1599 
1600 	if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
1601 	    (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
1602 		/*
1603 		 * change between subsampling and scaling
1604 		 * go through exposure calucation
1605 		 */
1606 		ret = ov5640_set_mode_exposure_calc(sensor, mode);
1607 	} else {
1608 		/*
1609 		 * change inside subsampling or scaling
1610 		 * download firmware directly
1611 		 */
1612 		ret = ov5640_set_mode_direct(sensor, mode);
1613 	}
1614 
1615 	if (ret < 0)
1616 		return ret;
1617 
1618 	ret = ov5640_set_ae_target(sensor, sensor->ae_target);
1619 	if (ret < 0)
1620 		return ret;
1621 	ret = ov5640_get_light_freq(sensor);
1622 	if (ret < 0)
1623 		return ret;
1624 	ret = ov5640_set_bandingfilter(sensor);
1625 	if (ret < 0)
1626 		return ret;
1627 	ret = ov5640_set_virtual_channel(sensor);
1628 	if (ret < 0)
1629 		return ret;
1630 
1631 	sensor->pending_mode_change = false;
1632 
1633 	return 0;
1634 }
1635 
1636 /* restore the last set video mode after chip power-on */
1637 static int ov5640_restore_mode(struct ov5640_dev *sensor)
1638 {
1639 	int ret;
1640 
1641 	/* first load the initial register values */
1642 	ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
1643 	if (ret < 0)
1644 		return ret;
1645 
1646 	/* now restore the last capture mode */
1647 	return ov5640_set_mode(sensor, &ov5640_mode_init_data);
1648 }
1649 
1650 static void ov5640_power(struct ov5640_dev *sensor, bool enable)
1651 {
1652 	gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
1653 }
1654 
1655 static void ov5640_reset(struct ov5640_dev *sensor)
1656 {
1657 	if (!sensor->reset_gpio)
1658 		return;
1659 
1660 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1661 
1662 	/* camera power cycle */
1663 	ov5640_power(sensor, false);
1664 	usleep_range(5000, 10000);
1665 	ov5640_power(sensor, true);
1666 	usleep_range(5000, 10000);
1667 
1668 	gpiod_set_value_cansleep(sensor->reset_gpio, 1);
1669 	usleep_range(1000, 2000);
1670 
1671 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1672 	usleep_range(5000, 10000);
1673 }
1674 
1675 static int ov5640_set_power_on(struct ov5640_dev *sensor)
1676 {
1677 	struct i2c_client *client = sensor->i2c_client;
1678 	int ret;
1679 
1680 	ret = clk_prepare_enable(sensor->xclk);
1681 	if (ret) {
1682 		dev_err(&client->dev, "%s: failed to enable clock\n",
1683 			__func__);
1684 		return ret;
1685 	}
1686 
1687 	ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
1688 				    sensor->supplies);
1689 	if (ret) {
1690 		dev_err(&client->dev, "%s: failed to enable regulators\n",
1691 			__func__);
1692 		goto xclk_off;
1693 	}
1694 
1695 	ov5640_reset(sensor);
1696 	ov5640_power(sensor, true);
1697 
1698 	ret = ov5640_init_slave_id(sensor);
1699 	if (ret)
1700 		goto power_off;
1701 
1702 	return 0;
1703 
1704 power_off:
1705 	ov5640_power(sensor, false);
1706 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
1707 xclk_off:
1708 	clk_disable_unprepare(sensor->xclk);
1709 	return ret;
1710 }
1711 
1712 static void ov5640_set_power_off(struct ov5640_dev *sensor)
1713 {
1714 	ov5640_power(sensor, false);
1715 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
1716 	clk_disable_unprepare(sensor->xclk);
1717 }
1718 
1719 static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
1720 {
1721 	int ret = 0;
1722 
1723 	if (on) {
1724 		ret = ov5640_set_power_on(sensor);
1725 		if (ret)
1726 			return ret;
1727 
1728 		ret = ov5640_restore_mode(sensor);
1729 		if (ret)
1730 			goto power_off;
1731 
1732 		if (sensor->ep.bus_type == V4L2_MBUS_CSI2) {
1733 			/*
1734 			 * start streaming briefly followed by stream off in
1735 			 * order to coax the clock lane into LP-11 state.
1736 			 */
1737 			ret = ov5640_set_stream_mipi(sensor, true);
1738 			if (ret)
1739 				goto power_off;
1740 			usleep_range(1000, 2000);
1741 			ret = ov5640_set_stream_mipi(sensor, false);
1742 			if (ret)
1743 				goto power_off;
1744 		}
1745 
1746 		return 0;
1747 	}
1748 
1749 power_off:
1750 	ov5640_set_power_off(sensor);
1751 	return ret;
1752 }
1753 
1754 /* --------------- Subdev Operations --------------- */
1755 
1756 static int ov5640_s_power(struct v4l2_subdev *sd, int on)
1757 {
1758 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
1759 	int ret = 0;
1760 
1761 	mutex_lock(&sensor->lock);
1762 
1763 	/*
1764 	 * If the power count is modified from 0 to != 0 or from != 0 to 0,
1765 	 * update the power state.
1766 	 */
1767 	if (sensor->power_count == !on) {
1768 		ret = ov5640_set_power(sensor, !!on);
1769 		if (ret)
1770 			goto out;
1771 	}
1772 
1773 	/* Update the power count. */
1774 	sensor->power_count += on ? 1 : -1;
1775 	WARN_ON(sensor->power_count < 0);
1776 out:
1777 	mutex_unlock(&sensor->lock);
1778 
1779 	if (on && !ret && sensor->power_count == 1) {
1780 		/* restore controls */
1781 		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
1782 	}
1783 
1784 	return ret;
1785 }
1786 
1787 static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
1788 				     struct v4l2_fract *fi,
1789 				     u32 width, u32 height)
1790 {
1791 	const struct ov5640_mode_info *mode;
1792 	u32 minfps, maxfps, fps;
1793 	int ret;
1794 
1795 	minfps = ov5640_framerates[OV5640_15_FPS];
1796 	maxfps = ov5640_framerates[OV5640_30_FPS];
1797 
1798 	if (fi->numerator == 0) {
1799 		fi->denominator = maxfps;
1800 		fi->numerator = 1;
1801 		return OV5640_30_FPS;
1802 	}
1803 
1804 	fps = DIV_ROUND_CLOSEST(fi->denominator, fi->numerator);
1805 
1806 	fi->numerator = 1;
1807 	if (fps > maxfps)
1808 		fi->denominator = maxfps;
1809 	else if (fps < minfps)
1810 		fi->denominator = minfps;
1811 	else if (2 * fps >= 2 * minfps + (maxfps - minfps))
1812 		fi->denominator = maxfps;
1813 	else
1814 		fi->denominator = minfps;
1815 
1816 	ret = (fi->denominator == minfps) ? OV5640_15_FPS : OV5640_30_FPS;
1817 
1818 	mode = ov5640_find_mode(sensor, ret, width, height, false);
1819 	return mode ? ret : -EINVAL;
1820 }
1821 
1822 static int ov5640_get_fmt(struct v4l2_subdev *sd,
1823 			  struct v4l2_subdev_pad_config *cfg,
1824 			  struct v4l2_subdev_format *format)
1825 {
1826 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
1827 	struct v4l2_mbus_framefmt *fmt;
1828 
1829 	if (format->pad != 0)
1830 		return -EINVAL;
1831 
1832 	mutex_lock(&sensor->lock);
1833 
1834 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1835 		fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
1836 						 format->pad);
1837 	else
1838 		fmt = &sensor->fmt;
1839 
1840 	format->format = *fmt;
1841 
1842 	mutex_unlock(&sensor->lock);
1843 
1844 	return 0;
1845 }
1846 
1847 static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
1848 				   struct v4l2_mbus_framefmt *fmt,
1849 				   enum ov5640_frame_rate fr,
1850 				   const struct ov5640_mode_info **new_mode)
1851 {
1852 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
1853 	const struct ov5640_mode_info *mode;
1854 	int i;
1855 
1856 	mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
1857 	if (!mode)
1858 		return -EINVAL;
1859 	fmt->width = mode->width;
1860 	fmt->height = mode->height;
1861 
1862 	if (new_mode)
1863 		*new_mode = mode;
1864 
1865 	for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
1866 		if (ov5640_formats[i].code == fmt->code)
1867 			break;
1868 	if (i >= ARRAY_SIZE(ov5640_formats))
1869 		fmt->code = ov5640_formats[0].code;
1870 
1871 	return 0;
1872 }
1873 
1874 static int ov5640_set_fmt(struct v4l2_subdev *sd,
1875 			  struct v4l2_subdev_pad_config *cfg,
1876 			  struct v4l2_subdev_format *format)
1877 {
1878 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
1879 	const struct ov5640_mode_info *new_mode;
1880 	int ret;
1881 
1882 	if (format->pad != 0)
1883 		return -EINVAL;
1884 
1885 	mutex_lock(&sensor->lock);
1886 
1887 	if (sensor->streaming) {
1888 		ret = -EBUSY;
1889 		goto out;
1890 	}
1891 
1892 	ret = ov5640_try_fmt_internal(sd, &format->format,
1893 				      sensor->current_fr, &new_mode);
1894 	if (ret)
1895 		goto out;
1896 
1897 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1898 		struct v4l2_mbus_framefmt *fmt =
1899 			v4l2_subdev_get_try_format(sd, cfg, 0);
1900 
1901 		*fmt = format->format;
1902 		goto out;
1903 	}
1904 
1905 	sensor->current_mode = new_mode;
1906 	sensor->fmt = format->format;
1907 	sensor->pending_mode_change = true;
1908 out:
1909 	mutex_unlock(&sensor->lock);
1910 	return ret;
1911 }
1912 
1913 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
1914 			       struct v4l2_mbus_framefmt *format)
1915 {
1916 	int ret = 0;
1917 	bool is_rgb = false;
1918 	u8 val;
1919 
1920 	switch (format->code) {
1921 	case MEDIA_BUS_FMT_UYVY8_2X8:
1922 		/* YUV422, UYVY */
1923 		val = 0x3f;
1924 		break;
1925 	case MEDIA_BUS_FMT_YUYV8_2X8:
1926 		/* YUV422, YUYV */
1927 		val = 0x30;
1928 		break;
1929 	case MEDIA_BUS_FMT_RGB565_2X8_LE:
1930 		/* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
1931 		val = 0x6F;
1932 		is_rgb = true;
1933 		break;
1934 	case MEDIA_BUS_FMT_RGB565_2X8_BE:
1935 		/* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
1936 		val = 0x61;
1937 		is_rgb = true;
1938 		break;
1939 	default:
1940 		return -EINVAL;
1941 	}
1942 
1943 	/* FORMAT CONTROL00: YUV and RGB formatting */
1944 	ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, val);
1945 	if (ret)
1946 		return ret;
1947 
1948 	/* FORMAT MUX CONTROL: ISP YUV or RGB */
1949 	return ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
1950 				is_rgb ? 0x01 : 0x00);
1951 }
1952 
1953 /*
1954  * Sensor Controls.
1955  */
1956 
1957 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
1958 {
1959 	int ret;
1960 
1961 	if (value) {
1962 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
1963 				     BIT(0), BIT(0));
1964 		if (ret)
1965 			return ret;
1966 		ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
1967 	} else {
1968 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
1969 	}
1970 
1971 	return ret;
1972 }
1973 
1974 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
1975 {
1976 	int ret;
1977 
1978 	if (value) {
1979 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
1980 				     BIT(2), BIT(2));
1981 		if (ret)
1982 			return ret;
1983 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
1984 				       value & 0xff);
1985 	} else {
1986 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
1987 	}
1988 
1989 	return ret;
1990 }
1991 
1992 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
1993 {
1994 	int ret;
1995 
1996 	if (value) {
1997 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
1998 				     BIT(1), BIT(1));
1999 		if (ret)
2000 			return ret;
2001 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
2002 				       value & 0xff);
2003 		if (ret)
2004 			return ret;
2005 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
2006 				       value & 0xff);
2007 	} else {
2008 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
2009 	}
2010 
2011 	return ret;
2012 }
2013 
2014 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
2015 {
2016 	int ret;
2017 
2018 	ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
2019 			     BIT(0), awb ? 0 : 1);
2020 	if (ret)
2021 		return ret;
2022 
2023 	if (!awb) {
2024 		u16 red = (u16)sensor->ctrls.red_balance->val;
2025 		u16 blue = (u16)sensor->ctrls.blue_balance->val;
2026 
2027 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
2028 		if (ret)
2029 			return ret;
2030 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
2031 	}
2032 
2033 	return ret;
2034 }
2035 
2036 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, int exp)
2037 {
2038 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
2039 	bool auto_exposure = (exp == V4L2_EXPOSURE_AUTO);
2040 	int ret = 0;
2041 
2042 	if (ctrls->auto_exp->is_new) {
2043 		ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
2044 				     BIT(0), auto_exposure ? 0 : BIT(0));
2045 		if (ret)
2046 			return ret;
2047 	}
2048 
2049 	if (!auto_exposure && ctrls->exposure->is_new) {
2050 		u16 max_exp;
2051 
2052 		ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
2053 					&max_exp);
2054 		if (ret)
2055 			return ret;
2056 		ret = ov5640_get_vts(sensor);
2057 		if (ret < 0)
2058 			return ret;
2059 		max_exp += ret;
2060 		ret = 0;
2061 
2062 		if (ctrls->exposure->val < max_exp)
2063 			ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
2064 	}
2065 
2066 	return ret;
2067 }
2068 
2069 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, int auto_gain)
2070 {
2071 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
2072 	int ret = 0;
2073 
2074 	if (ctrls->auto_gain->is_new) {
2075 		ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
2076 				     BIT(1), ctrls->auto_gain->val ? 0 : BIT(1));
2077 		if (ret)
2078 			return ret;
2079 	}
2080 
2081 	if (!auto_gain && ctrls->gain->is_new) {
2082 		u16 gain = (u16)ctrls->gain->val;
2083 
2084 		ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
2085 					 gain & 0x3ff);
2086 	}
2087 
2088 	return ret;
2089 }
2090 
2091 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
2092 {
2093 	return ov5640_mod_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
2094 			      0xa4, value ? 0xa4 : 0);
2095 }
2096 
2097 static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
2098 {
2099 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2100 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2101 	int val;
2102 
2103 	/* v4l2_ctrl_lock() locks our own mutex */
2104 
2105 	switch (ctrl->id) {
2106 	case V4L2_CID_AUTOGAIN:
2107 		if (!ctrl->val)
2108 			return 0;
2109 		val = ov5640_get_gain(sensor);
2110 		if (val < 0)
2111 			return val;
2112 		sensor->ctrls.gain->val = val;
2113 		break;
2114 	case V4L2_CID_EXPOSURE_AUTO:
2115 		if (ctrl->val == V4L2_EXPOSURE_MANUAL)
2116 			return 0;
2117 		val = ov5640_get_exposure(sensor);
2118 		if (val < 0)
2119 			return val;
2120 		sensor->ctrls.exposure->val = val;
2121 		break;
2122 	}
2123 
2124 	return 0;
2125 }
2126 
2127 static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
2128 {
2129 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2130 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2131 	int ret;
2132 
2133 	/* v4l2_ctrl_lock() locks our own mutex */
2134 
2135 	/*
2136 	 * If the device is not powered up by the host driver do
2137 	 * not apply any controls to H/W at this time. Instead
2138 	 * the controls will be restored right after power-up.
2139 	 */
2140 	if (sensor->power_count == 0)
2141 		return 0;
2142 
2143 	switch (ctrl->id) {
2144 	case V4L2_CID_AUTOGAIN:
2145 		ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
2146 		break;
2147 	case V4L2_CID_EXPOSURE_AUTO:
2148 		ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
2149 		break;
2150 	case V4L2_CID_AUTO_WHITE_BALANCE:
2151 		ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
2152 		break;
2153 	case V4L2_CID_HUE:
2154 		ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
2155 		break;
2156 	case V4L2_CID_CONTRAST:
2157 		ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
2158 		break;
2159 	case V4L2_CID_SATURATION:
2160 		ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
2161 		break;
2162 	case V4L2_CID_TEST_PATTERN:
2163 		ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
2164 		break;
2165 	default:
2166 		ret = -EINVAL;
2167 		break;
2168 	}
2169 
2170 	return ret;
2171 }
2172 
2173 static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
2174 	.g_volatile_ctrl = ov5640_g_volatile_ctrl,
2175 	.s_ctrl = ov5640_s_ctrl,
2176 };
2177 
2178 static const char * const test_pattern_menu[] = {
2179 	"Disabled",
2180 	"Color bars",
2181 };
2182 
2183 static int ov5640_init_controls(struct ov5640_dev *sensor)
2184 {
2185 	const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
2186 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
2187 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
2188 	int ret;
2189 
2190 	v4l2_ctrl_handler_init(hdl, 32);
2191 
2192 	/* we can use our own mutex for the ctrl lock */
2193 	hdl->lock = &sensor->lock;
2194 
2195 	/* Auto/manual white balance */
2196 	ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
2197 					   V4L2_CID_AUTO_WHITE_BALANCE,
2198 					   0, 1, 1, 1);
2199 	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
2200 						0, 4095, 1, 0);
2201 	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
2202 					       0, 4095, 1, 0);
2203 	/* Auto/manual exposure */
2204 	ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
2205 						 V4L2_CID_EXPOSURE_AUTO,
2206 						 V4L2_EXPOSURE_MANUAL, 0,
2207 						 V4L2_EXPOSURE_AUTO);
2208 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
2209 					    0, 65535, 1, 0);
2210 	/* Auto/manual gain */
2211 	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
2212 					     0, 1, 1, 1);
2213 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
2214 					0, 1023, 1, 0);
2215 
2216 	ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
2217 					      0, 255, 1, 64);
2218 	ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
2219 				       0, 359, 1, 0);
2220 	ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
2221 					    0, 255, 1, 0);
2222 	ctrls->test_pattern =
2223 		v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
2224 					     ARRAY_SIZE(test_pattern_menu) - 1,
2225 					     0, 0, test_pattern_menu);
2226 
2227 	if (hdl->error) {
2228 		ret = hdl->error;
2229 		goto free_ctrls;
2230 	}
2231 
2232 	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
2233 	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
2234 
2235 	v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
2236 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
2237 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
2238 
2239 	sensor->sd.ctrl_handler = hdl;
2240 	return 0;
2241 
2242 free_ctrls:
2243 	v4l2_ctrl_handler_free(hdl);
2244 	return ret;
2245 }
2246 
2247 static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
2248 				  struct v4l2_subdev_pad_config *cfg,
2249 				  struct v4l2_subdev_frame_size_enum *fse)
2250 {
2251 	if (fse->pad != 0)
2252 		return -EINVAL;
2253 	if (fse->index >= OV5640_NUM_MODES)
2254 		return -EINVAL;
2255 
2256 	fse->min_width = fse->max_width =
2257 		ov5640_mode_data[0][fse->index].width;
2258 	fse->min_height = fse->max_height =
2259 		ov5640_mode_data[0][fse->index].height;
2260 
2261 	return 0;
2262 }
2263 
2264 static int ov5640_enum_frame_interval(
2265 	struct v4l2_subdev *sd,
2266 	struct v4l2_subdev_pad_config *cfg,
2267 	struct v4l2_subdev_frame_interval_enum *fie)
2268 {
2269 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2270 	struct v4l2_fract tpf;
2271 	int ret;
2272 
2273 	if (fie->pad != 0)
2274 		return -EINVAL;
2275 	if (fie->index >= OV5640_NUM_FRAMERATES)
2276 		return -EINVAL;
2277 
2278 	tpf.numerator = 1;
2279 	tpf.denominator = ov5640_framerates[fie->index];
2280 
2281 	ret = ov5640_try_frame_interval(sensor, &tpf,
2282 					fie->width, fie->height);
2283 	if (ret < 0)
2284 		return -EINVAL;
2285 
2286 	fie->interval = tpf;
2287 	return 0;
2288 }
2289 
2290 static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
2291 				   struct v4l2_subdev_frame_interval *fi)
2292 {
2293 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2294 
2295 	mutex_lock(&sensor->lock);
2296 	fi->interval = sensor->frame_interval;
2297 	mutex_unlock(&sensor->lock);
2298 
2299 	return 0;
2300 }
2301 
2302 static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
2303 				   struct v4l2_subdev_frame_interval *fi)
2304 {
2305 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2306 	const struct ov5640_mode_info *mode;
2307 	int frame_rate, ret = 0;
2308 
2309 	if (fi->pad != 0)
2310 		return -EINVAL;
2311 
2312 	mutex_lock(&sensor->lock);
2313 
2314 	if (sensor->streaming) {
2315 		ret = -EBUSY;
2316 		goto out;
2317 	}
2318 
2319 	mode = sensor->current_mode;
2320 
2321 	frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
2322 					       mode->width, mode->height);
2323 	if (frame_rate < 0)
2324 		frame_rate = OV5640_15_FPS;
2325 
2326 	sensor->current_fr = frame_rate;
2327 	sensor->frame_interval = fi->interval;
2328 	sensor->pending_mode_change = true;
2329 out:
2330 	mutex_unlock(&sensor->lock);
2331 	return ret;
2332 }
2333 
2334 static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
2335 				  struct v4l2_subdev_pad_config *cfg,
2336 				  struct v4l2_subdev_mbus_code_enum *code)
2337 {
2338 	if (code->pad != 0)
2339 		return -EINVAL;
2340 	if (code->index >= ARRAY_SIZE(ov5640_formats))
2341 		return -EINVAL;
2342 
2343 	code->code = ov5640_formats[code->index].code;
2344 	return 0;
2345 }
2346 
2347 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
2348 {
2349 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2350 	int ret = 0;
2351 
2352 	mutex_lock(&sensor->lock);
2353 
2354 	if (sensor->streaming == !enable) {
2355 		if (enable && sensor->pending_mode_change) {
2356 			ret = ov5640_set_mode(sensor, sensor->current_mode);
2357 			if (ret)
2358 				goto out;
2359 
2360 			ret = ov5640_set_framefmt(sensor, &sensor->fmt);
2361 			if (ret)
2362 				goto out;
2363 		}
2364 
2365 		if (sensor->ep.bus_type == V4L2_MBUS_CSI2)
2366 			ret = ov5640_set_stream_mipi(sensor, enable);
2367 		else
2368 			ret = ov5640_set_stream_dvp(sensor, enable);
2369 
2370 		if (!ret)
2371 			sensor->streaming = enable;
2372 	}
2373 out:
2374 	mutex_unlock(&sensor->lock);
2375 	return ret;
2376 }
2377 
2378 static const struct v4l2_subdev_core_ops ov5640_core_ops = {
2379 	.s_power = ov5640_s_power,
2380 };
2381 
2382 static const struct v4l2_subdev_video_ops ov5640_video_ops = {
2383 	.g_frame_interval = ov5640_g_frame_interval,
2384 	.s_frame_interval = ov5640_s_frame_interval,
2385 	.s_stream = ov5640_s_stream,
2386 };
2387 
2388 static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
2389 	.enum_mbus_code = ov5640_enum_mbus_code,
2390 	.get_fmt = ov5640_get_fmt,
2391 	.set_fmt = ov5640_set_fmt,
2392 	.enum_frame_size = ov5640_enum_frame_size,
2393 	.enum_frame_interval = ov5640_enum_frame_interval,
2394 };
2395 
2396 static const struct v4l2_subdev_ops ov5640_subdev_ops = {
2397 	.core = &ov5640_core_ops,
2398 	.video = &ov5640_video_ops,
2399 	.pad = &ov5640_pad_ops,
2400 };
2401 
2402 static int ov5640_get_regulators(struct ov5640_dev *sensor)
2403 {
2404 	int i;
2405 
2406 	for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
2407 		sensor->supplies[i].supply = ov5640_supply_name[i];
2408 
2409 	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
2410 				       OV5640_NUM_SUPPLIES,
2411 				       sensor->supplies);
2412 }
2413 
2414 static int ov5640_check_chip_id(struct ov5640_dev *sensor)
2415 {
2416 	struct i2c_client *client = sensor->i2c_client;
2417 	int ret = 0;
2418 	u16 chip_id;
2419 
2420 	ret = ov5640_set_power_on(sensor);
2421 	if (ret)
2422 		return ret;
2423 
2424 	ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
2425 	if (ret) {
2426 		dev_err(&client->dev, "%s: failed to read chip identifier\n",
2427 			__func__);
2428 		goto power_off;
2429 	}
2430 
2431 	if (chip_id != 0x5640) {
2432 		dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
2433 			__func__, chip_id);
2434 		ret = -ENXIO;
2435 	}
2436 
2437 power_off:
2438 	ov5640_set_power_off(sensor);
2439 	return ret;
2440 }
2441 
2442 static int ov5640_probe(struct i2c_client *client,
2443 			const struct i2c_device_id *id)
2444 {
2445 	struct device *dev = &client->dev;
2446 	struct fwnode_handle *endpoint;
2447 	struct ov5640_dev *sensor;
2448 	int ret;
2449 
2450 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
2451 	if (!sensor)
2452 		return -ENOMEM;
2453 
2454 	sensor->i2c_client = client;
2455 	sensor->fmt.code = MEDIA_BUS_FMT_UYVY8_2X8;
2456 	sensor->fmt.width = 640;
2457 	sensor->fmt.height = 480;
2458 	sensor->fmt.field = V4L2_FIELD_NONE;
2459 	sensor->frame_interval.numerator = 1;
2460 	sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
2461 	sensor->current_fr = OV5640_30_FPS;
2462 	sensor->current_mode =
2463 		&ov5640_mode_data[OV5640_30_FPS][OV5640_MODE_VGA_640_480];
2464 	sensor->pending_mode_change = true;
2465 
2466 	sensor->ae_target = 52;
2467 
2468 	endpoint = fwnode_graph_get_next_endpoint(
2469 		of_fwnode_handle(client->dev.of_node), NULL);
2470 	if (!endpoint) {
2471 		dev_err(dev, "endpoint node not found\n");
2472 		return -EINVAL;
2473 	}
2474 
2475 	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
2476 	fwnode_handle_put(endpoint);
2477 	if (ret) {
2478 		dev_err(dev, "Could not parse endpoint\n");
2479 		return ret;
2480 	}
2481 
2482 	/* get system clock (xclk) */
2483 	sensor->xclk = devm_clk_get(dev, "xclk");
2484 	if (IS_ERR(sensor->xclk)) {
2485 		dev_err(dev, "failed to get xclk\n");
2486 		return PTR_ERR(sensor->xclk);
2487 	}
2488 
2489 	sensor->xclk_freq = clk_get_rate(sensor->xclk);
2490 	if (sensor->xclk_freq < OV5640_XCLK_MIN ||
2491 	    sensor->xclk_freq > OV5640_XCLK_MAX) {
2492 		dev_err(dev, "xclk frequency out of range: %d Hz\n",
2493 			sensor->xclk_freq);
2494 		return -EINVAL;
2495 	}
2496 
2497 	/* request optional power down pin */
2498 	sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
2499 						    GPIOD_OUT_HIGH);
2500 	/* request optional reset pin */
2501 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2502 						     GPIOD_OUT_HIGH);
2503 
2504 	v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
2505 
2506 	sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2507 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
2508 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2509 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
2510 	if (ret)
2511 		return ret;
2512 
2513 	ret = ov5640_get_regulators(sensor);
2514 	if (ret)
2515 		return ret;
2516 
2517 	mutex_init(&sensor->lock);
2518 
2519 	ret = ov5640_check_chip_id(sensor);
2520 	if (ret)
2521 		goto entity_cleanup;
2522 
2523 	ret = ov5640_init_controls(sensor);
2524 	if (ret)
2525 		goto entity_cleanup;
2526 
2527 	ret = v4l2_async_register_subdev(&sensor->sd);
2528 	if (ret)
2529 		goto free_ctrls;
2530 
2531 	return 0;
2532 
2533 free_ctrls:
2534 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2535 entity_cleanup:
2536 	mutex_destroy(&sensor->lock);
2537 	media_entity_cleanup(&sensor->sd.entity);
2538 	return ret;
2539 }
2540 
2541 static int ov5640_remove(struct i2c_client *client)
2542 {
2543 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
2544 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2545 
2546 	v4l2_async_unregister_subdev(&sensor->sd);
2547 	mutex_destroy(&sensor->lock);
2548 	media_entity_cleanup(&sensor->sd.entity);
2549 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
2550 
2551 	return 0;
2552 }
2553 
2554 static const struct i2c_device_id ov5640_id[] = {
2555 	{"ov5640", 0},
2556 	{},
2557 };
2558 MODULE_DEVICE_TABLE(i2c, ov5640_id);
2559 
2560 static const struct of_device_id ov5640_dt_ids[] = {
2561 	{ .compatible = "ovti,ov5640" },
2562 	{ /* sentinel */ }
2563 };
2564 MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
2565 
2566 static struct i2c_driver ov5640_i2c_driver = {
2567 	.driver = {
2568 		.name  = "ov5640",
2569 		.of_match_table	= ov5640_dt_ids,
2570 	},
2571 	.id_table = ov5640_id,
2572 	.probe    = ov5640_probe,
2573 	.remove   = ov5640_remove,
2574 };
2575 
2576 module_i2c_driver(ov5640_i2c_driver);
2577 
2578 MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
2579 MODULE_LICENSE("GPL");
2580