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