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