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