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