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