media: video: tegra: ov5693 change the setting value
[linux-2.6.git] / drivers / media / video / tegra / ov5693.c
1 /*
2  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include <linux/delay.h>
18 #include <linux/fs.h>
19 #include <linux/i2c.h>
20 #include <linux/miscdevice.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/gpio.h>
25 #include <linux/module.h>
26 #include <linux/regmap.h>
27 #include <linux/gpio.h>
28 #include <linux/of.h>
29 #include <linux/of_device.h>
30 #include <linux/of_gpio.h>
31 #include <media/ov5693.h>
32
33 #define OV5693_ID                       0x5693
34 #define OV5693_SENSOR_TYPE              NVC_IMAGER_TYPE_RAW
35 #define OV5693_RES_CHG_WAIT_TIME_MS     100
36 #define OV5693_SIZEOF_I2C_BUF           16
37 #define OV5693_TABLE_WAIT_MS            0
38 #define OV5693_TABLE_END                1
39 #define OV5693_TABLE_RESET              2
40 #define OV5693_TABLE_RESET_TIMEOUT      50
41 #define OV5693_LENS_MAX_APERTURE        0       /* _INT2FLOAT_DIVISOR */
42 #define OV5693_LENS_FNUMBER             0       /* _INT2FLOAT_DIVISOR */
43 #define OV5693_LENS_FOCAL_LENGTH        6120    /* _INT2FLOAT_DIVISOR */
44 #define OV5693_LENS_VIEW_ANGLE_H        60000   /* _INT2FLOAT_DIVISOR */
45 #define OV5693_LENS_VIEW_ANGLE_V        60000   /* _INT2FLOAT_DIVISOR */
46 #define OV5693_OTP_BUF_SIZE             16
47
48 static struct nvc_gpio_init ov5693_gpio[] = {
49         { OV5693_GPIO_TYPE_PWRDN, GPIOF_OUT_INIT_LOW, "pwrdn", false, true, },
50 };
51
52 struct ov5693_info {
53         atomic_t in_use;
54         struct i2c_client *i2c_client;
55         struct ov5693_platform_data *pdata;
56         struct miscdevice miscdev;
57         int pwr_api;
58         int pwr_dev;
59         struct nvc_gpio gpio[ARRAY_SIZE(ov5693_gpio)];
60         struct ov5693_power_rail regulators;
61         bool power_on;
62         u32 mode_index;
63         bool mode_valid;
64         bool mode_enable;
65         unsigned test_pattern;
66         struct nvc_imager_static_nvc sdata;
67         u8 bin_en;
68         struct ov5693_fuseid fuseid;
69         struct regmap *regmap;
70         struct regulator *ext_vcm_vdd;
71         struct ov5693_cal_data cal;
72 };
73
74 struct ov5693_reg {
75         u16 addr;
76         u16 val;
77 };
78
79 struct ov5693_mode_data {
80         struct nvc_imager_mode sensor_mode;
81         struct nvc_imager_dynamic_nvc sensor_dnvc;
82         struct ov5693_reg *p_mode_i2c;
83 };
84
85 static struct ov5693_platform_data ov5693_dflt_pdata = {
86         .cfg            = 0,
87         .num            = 0,
88         .dev_name       = "camera",
89 };
90
91 /*
92  * NOTE: static vs dynamic
93  * If a member in the nvc_imager_static_nvc structure is not actually
94  * static data, then leave blank and add the parameter to the parameter
95  * read function that dynamically reads the data.  The NVC user driver
96  * will call the parameter read for the data if the member data is 0.
97  * If the dynamic data becomes static during probe (a one time read
98  * such as device ID) then add the dynamic read to the _sdata_init
99  * function.
100  */
101 static struct nvc_imager_static_nvc ov5693_dflt_sdata = {
102         .api_version            = NVC_IMAGER_API_STATIC_VER,
103         .sensor_type            = OV5693_SENSOR_TYPE,
104         .bits_per_pixel         = 10,
105         .sensor_id              = OV5693_ID,
106         .sensor_id_minor        = 0,
107         .focal_len              = OV5693_LENS_FOCAL_LENGTH,
108         .max_aperture           = OV5693_LENS_MAX_APERTURE,
109         .fnumber                = OV5693_LENS_FNUMBER,
110         .view_angle_h           = OV5693_LENS_VIEW_ANGLE_H,
111         .view_angle_v           = OV5693_LENS_VIEW_ANGLE_V,
112         .res_chg_wait_time      = OV5693_RES_CHG_WAIT_TIME_MS,
113 };
114
115 static const struct ov5693_reg ov5693_2592x1944_i2c[] = {
116         {OV5693_TABLE_RESET, 0},/* Including sw reset */
117         {0x3001, 0x0a},
118         {0x3002, 0x80},
119         {0x3006, 0x00},
120         {0x3011, 0x21},
121         {0x3012, 0x09},
122         {0x3013, 0x10},
123         {0x3014, 0x00},
124         {0x3015, 0x08},
125         {0x3016, 0xf0},
126         {0x3017, 0xf0},
127         {0x3018, 0xf0},
128         {0x301b, 0xb4},
129         {0x301d, 0x02},
130         {0x3021, 0x00},
131         {0x3022, 0x01},
132         {0x3028, 0x44},
133         {0x3090, 0x02},
134         {0x3091, 0x0e},
135         {0x3092, 0x00},
136         {0x3093, 0x00},
137         {0x3098, 0x03},
138         {0x3099, 0x1e},
139         {0x309a, 0x02},
140         {0x309b, 0x01},
141         {0x309c, 0x00},
142         {0x30a0, 0xd2},
143         {0x30a2, 0x01},
144         {0x30b2, 0x00},
145         {0x30b3, 0x64},
146         {0x30b4, 0x03},
147         {0x30b5, 0x04},
148         {0x30b6, 0x01},
149         {0x3104, 0x21},
150         {0x3106, 0x00},
151         {0x3406, 0x01},
152         {0x3500, 0x00},
153         {0x3501, 0x7b},
154         {0x3502, 0x00},
155         {0x3503, 0x07},
156         {0x3504, 0x00},
157         {0x3505, 0x00},
158         {0x3506, 0x00},
159         {0x3507, 0x02},
160         {0x3508, 0x00},
161         {0x3509, 0x10},
162         {0x350a, 0x00},
163         {0x350b, 0x40},
164         {0x3601, 0x0a},
165         {0x3602, 0x18},
166         {0x3612, 0x80},
167         {0x3620, 0x54},
168         {0x3621, 0xc7},
169         {0x3622, 0x0f},
170         {0x3625, 0x10},
171         {0x3630, 0x55},
172         {0x3631, 0xf4},
173         {0x3632, 0x00},
174         {0x3633, 0x34},
175         {0x3634, 0x02},
176         {0x364d, 0x0d},
177         {0x364f, 0xdd},
178         {0x3660, 0x04},
179         {0x3662, 0x10},
180         {0x3663, 0xf1},
181         {0x3665, 0x00},
182         {0x3666, 0x20},
183         {0x3667, 0x00},
184         {0x366a, 0x80},
185         {0x3680, 0xe0},
186         {0x3681, 0x00},
187         {0x3700, 0x42},
188         {0x3701, 0x14},
189         {0x3702, 0xa0},
190         {0x3703, 0xd8},
191         {0x3704, 0x78},
192         {0x3705, 0x02},
193         {0x3708, 0xe2},
194         {0x3709, 0xc3},
195         {0x370a, 0x00},
196         {0x370b, 0x20},
197         {0x370c, 0x0c},
198         {0x370d, 0x11},
199         {0x370e, 0x00},
200         {0x370f, 0x40},
201         {0x3710, 0x00},
202         {0x371a, 0x1c},
203         {0x371b, 0x05},
204         {0x371c, 0x01},
205         {0x371e, 0xa1},
206         {0x371f, 0x0c},
207         {0x3721, 0x00},
208         {0x3726, 0x00},
209         {0x372a, 0x01},
210         {0x3730, 0x10},
211         {0x3738, 0x22},
212         {0x3739, 0xe5},
213         {0x373a, 0x50},
214         {0x373b, 0x02},
215         {0x373c, 0x41},
216         {0x373f, 0x02},
217         {0x3740, 0x42},
218         {0x3741, 0x02},
219         {0x3742, 0x18},
220         {0x3743, 0x01},
221         {0x3744, 0x02},
222         {0x3747, 0x10},
223         {0x374c, 0x04},
224         {0x3751, 0xf0},
225         {0x3752, 0x00},
226         {0x3753, 0x00},
227         {0x3754, 0xc0},
228         {0x3755, 0x00},
229         {0x3756, 0x1a},
230         {0x3758, 0x00},
231         {0x3759, 0x0f},
232         {0x376b, 0x44},
233         {0x375c, 0x04},
234         {0x3776, 0x00},
235         {0x377f, 0x08},
236         {0x3780, 0x22},
237         {0x3781, 0x0c},
238         {0x3784, 0x2c},
239         {0x3785, 0x1e},
240         {0x378f, 0xf5},
241         {0x3791, 0xb0},
242         {0x3795, 0x00},
243         {0x3796, 0x64},
244         {0x3797, 0x11},
245         {0x3798, 0x30},
246         {0x3799, 0x41},
247         {0x379a, 0x07},
248         {0x379b, 0xb0},
249         {0x379c, 0x0c},
250         {0x37c5, 0x00},
251         {0x37c6, 0x00},
252         {0x37c7, 0x00},
253         {0x37c9, 0x00},
254         {0x37ca, 0x00},
255         {0x37cb, 0x00},
256         {0x37de, 0x00},
257         {0x37df, 0x00},
258         {0x3800, 0x00},
259         {0x3801, 0x02},
260         {0x3802, 0x00},
261         {0x3803, 0x02},
262         {0x3804, 0x0a},
263         {0x3805, 0x41},
264         {0x3806, 0x07},
265         {0x3807, 0xa5},
266         {0x3808, 0x0a},
267         {0x3809, 0x20},
268         {0x380a, 0x07},
269         {0x380b, 0x98},
270         {0x380c, 0x0a},
271         {0x380d, 0x80},
272         {0x380e, 0x07},
273         {0x380f, 0xc0},
274         {0x3810, 0x00},
275         {0x3811, 0x02},
276         {0x3812, 0x00},
277         {0x3813, 0x02},
278         {0x3814, 0x11},
279         {0x3815, 0x11},
280         {0x3820, 0x00},
281         {0x3821, 0x1e},
282         {0x3823, 0x00},
283         {0x3824, 0x00},
284         {0x3825, 0x00},
285         {0x3826, 0x00},
286         {0x3827, 0x00},
287         {0x382a, 0x04},
288         {0x3a04, 0x06},
289         {0x3a05, 0x14},
290         {0x3a06, 0x00},
291         {0x3a07, 0xfe},
292         {0x3b00, 0x00},
293         {0x3b02, 0x00},
294         {0x3b03, 0x00},
295         {0x3b04, 0x00},
296         {0x3b05, 0x00},
297         {0x3d00, 0x00},
298         {0x3d01, 0x00},
299         {0x3d02, 0x00},
300         {0x3d03, 0x00},
301         {0x3d04, 0x00},
302         {0x3d05, 0x00},
303         {0x3d06, 0x00},
304         {0x3d07, 0x00},
305         {0x3d08, 0x00},
306         {0x3d09, 0x00},
307         {0x3d0a, 0x00},
308         {0x3d0b, 0x00},
309         {0x3d0c, 0x00},
310         {0x3d0d, 0x00},
311         {0x3d0e, 0x00},
312         {0x3d0f, 0x00},
313         {0x3d80, 0x00},
314         {0x3d81, 0x00},
315         {0x3d84, 0x00},
316         {0x3e07, 0x20},
317         {0x4000, 0x08},
318         {0x4001, 0x04},
319         {0x4002, 0x45},
320         {0x4004, 0x08},
321         {0x4005, 0x18},
322         {0x4006, 0x20},
323         {0x4008, 0x24},
324         {0x4009, 0x10},
325         {0x400c, 0x00},
326         {0x400d, 0x00},
327         {0x4058, 0x00},
328         {0x4101, 0xb2},
329         {0x4303, 0x00},
330         {0x4304, 0x08},
331         {0x4307, 0x30},
332         {0x4311, 0x04},
333         {0x4315, 0x01},
334         {0x4511, 0x05},
335         {0x4512, 0x01},
336         {0x4806, 0x00},
337         {0x4816, 0x52},
338         {0x481f, 0x30},
339         {0x4826, 0x2c},
340         {0x4831, 0x64},
341         {0x4d00, 0x04},
342         {0x4d01, 0x71},
343         {0x4d02, 0xfd},
344         {0x4d03, 0xf5},
345         {0x4d04, 0x0c},
346         {0x4d05, 0xcc},
347         {0x4837, 0x0a},
348         {0x5000, 0x06},
349         {0x5001, 0x01},
350         {0x5002, 0x00},
351         {0x5003, 0x20},
352         {0x5046, 0x0a},
353         {0x5013, 0x00},
354         {0x5046, 0x0a},
355         {0x5780, 0x1c},
356         {0x5786, 0x20},
357         {0x5787, 0x10},
358         {0x5788, 0x18},
359         {0x578a, 0x04},
360         {0x578b, 0x02},
361         {0x578c, 0x02},
362         {0x578e, 0x06},
363         {0x578f, 0x02},
364         {0x5790, 0x02},
365         {0x5791, 0xff},
366         {0x5842, 0x01},
367         {0x5843, 0x2b},
368         {0x5844, 0x01},
369         {0x5845, 0x92},
370         {0x5846, 0x01},
371         {0x5847, 0x8f},
372         {0x5848, 0x01},
373         {0x5849, 0x0c},
374         {0x5e00, 0x00},
375         {0x5e10, 0x0c},
376         {OV5693_TABLE_END, 0x0000}
377 };
378
379 static const struct ov5693_reg ov5693_1296x972_i2c[] = {
380         {OV5693_TABLE_RESET, 0},/* Including sw reset */
381         {0x0103, 0x01},
382         {0x3001, 0x0a},
383         {0x3002, 0x80},
384         {0x3006, 0x00},
385         {0x3011, 0x21},
386         {0x3012, 0x09},
387         {0x3013, 0x10},
388         {0x3014, 0x00},
389         {0x3015, 0x08},
390         {0x3016, 0xf0},
391         {0x3017, 0xf0},
392         {0x3018, 0xf0},
393         {0x301b, 0xb4},
394         {0x301d, 0x02},
395         {0x3021, 0x00},
396         {0x3022, 0x01},
397         {0x3028, 0x44},
398         {0x3098, 0x03},
399         {0x3099, 0x1e},
400         {0x309a, 0x02},
401         {0x309b, 0x01},
402         {0x309c, 0x00},
403         {0x30a0, 0xd2},
404         {0x30a2, 0x01},
405         {0x30b2, 0x00},
406         {0x30b3, 0x64},
407         {0x30b4, 0x03},
408         {0x30b5, 0x04},
409         {0x30b6, 0x01},
410         {0x3104, 0x21},
411         {0x3106, 0x00},
412         {0x3406, 0x01},
413         {0x3500, 0x00},
414         {0x3501, 0x7b},
415         {0x3502, 0x00},
416         {0x3503, 0x07},
417         {0x3504, 0x00},
418         {0x3505, 0x00},
419         {0x3506, 0x00},
420         {0x3507, 0x02},
421         {0x3508, 0x00},
422         {0x3509, 0x10},
423         {0x350a, 0x00},
424         {0x350b, 0x40},
425         {0x3601, 0x0a},
426         {0x3602, 0x38},
427         {0x3612, 0x80},
428         {0x3620, 0x54},
429         {0x3621, 0xc7},
430         {0x3622, 0x0f},
431         {0x3625, 0x10},
432         {0x3630, 0x55},
433         {0x3631, 0xf4},
434         {0x3632, 0x00},
435         {0x3633, 0x34},
436         {0x3634, 0x02},
437         {0x364d, 0x0d},
438         {0x364f, 0xdd},
439         {0x3660, 0x04},
440         {0x3662, 0x10},
441         {0x3663, 0xf1},
442         {0x3665, 0x00},
443         {0x3666, 0x20},
444         {0x3667, 0x00},
445         {0x366a, 0x80},
446         {0x3680, 0xe0},
447         {0x3681, 0x00},
448         {0x3700, 0x42},
449         {0x3701, 0x14},
450         {0x3702, 0xa0},
451         {0x3703, 0xd8},
452         {0x3704, 0x78},
453         {0x3705, 0x02},
454         {0x3708, 0xe6},
455         {0x3709, 0xc3},
456         {0x370a, 0x00},
457         {0x370b, 0x20},
458         {0x370c, 0x0c},
459         {0x370d, 0x11},
460         {0x370e, 0x00},
461         {0x370f, 0x40},
462         {0x3710, 0x00},
463         {0x371a, 0x1c},
464         {0x371b, 0x05},
465         {0x371c, 0x01},
466         {0x371e, 0xa1},
467         {0x371f, 0x0c},
468         {0x3721, 0x00},
469         {0x3724, 0x10},
470         {0x3726, 0x00},
471         {0x372a, 0x01},
472         {0x3730, 0x10},
473         {0x3738, 0x22},
474         {0x3739, 0xe5},
475         {0x373a, 0x50},
476         {0x373b, 0x02},
477         {0x373c, 0x41},
478         {0x373f, 0x02},
479         {0x3740, 0x42},
480         {0x3741, 0x02},
481         {0x3742, 0x18},
482         {0x3743, 0x01},
483         {0x3744, 0x02},
484         {0x3747, 0x10},
485         {0x374c, 0x04},
486         {0x3751, 0xf0},
487         {0x3752, 0x00},
488         {0x3753, 0x00},
489         {0x3754, 0xc0},
490         {0x3755, 0x00},
491         {0x3756, 0x1a},
492         {0x3758, 0x00},
493         {0x3759, 0x0f},
494         {0x376b, 0x44},
495         {0x375c, 0x04},
496         {0x3774, 0x10},
497         {0x3776, 0x00},
498         {0x377f, 0x08},
499         {0x3780, 0x22},
500         {0x3781, 0x0c},
501         {0x3784, 0x2c},
502         {0x3785, 0x1e},
503         {0x378f, 0xf5},
504         {0x3791, 0xb0},
505         {0x3795, 0x00},
506         {0x3796, 0x64},
507         {0x3797, 0x11},
508         {0x3798, 0x30},
509         {0x3799, 0x41},
510         {0x379a, 0x07},
511         {0x379b, 0xb0},
512         {0x379c, 0x0c},
513         {0x37c5, 0x00},
514         {0x37c6, 0x00},
515         {0x37c7, 0x00},
516         {0x37c9, 0x00},
517         {0x37ca, 0x00},
518         {0x37cb, 0x00},
519         {0x37de, 0x00},
520         {0x37df, 0x00},
521         {0x3800, 0x00},
522         {0x3801, 0x00},
523         {0x3802, 0x00},
524         {0x3803, 0x00},
525         {0x3804, 0x0a},
526         {0x3805, 0x3f},
527         {0x3806, 0x07},
528         {0x3807, 0xa3},
529         {0x3808, 0x05},
530         {0x3809, 0x10},
531         {0x380a, 0x03},
532         {0x380b, 0xcc},
533         {0x380c, 0x0a},
534         {0x380d, 0x80},
535         {0x380e, 0x07},
536         {0x380f, 0xc0},
537         {0x3810, 0x00},
538         {0x3811, 0x02},
539         {0x3812, 0x00},
540         {0x3813, 0x02},
541         {0x3814, 0x31},
542         {0x3815, 0x31},
543         {0x3820, 0x01},
544         {0x3821, 0x1f},
545         {0x3823, 0x00},
546         {0x3824, 0x00},
547         {0x3825, 0x00},
548         {0x3826, 0x00},
549         {0x3827, 0x00},
550         {0x382a, 0x04},
551         {0x3a04, 0x06},
552         {0x3a05, 0x14},
553         {0x3a06, 0x00},
554         {0x3a07, 0xfe},
555         {0x3b00, 0x00},
556         {0x3b02, 0x00},
557         {0x3b03, 0x00},
558         {0x3b04, 0x00},
559         {0x3b05, 0x00},
560         {0x3e07, 0x20},
561         {0x4000, 0x08},
562         {0x4001, 0x04},
563         {0x4002, 0x45},
564         {0x4004, 0x08},
565         {0x4005, 0x18},
566         {0x4006, 0x20},
567         {0x4008, 0x24},
568         {0x4009, 0x10},
569         {0x400c, 0x00},
570         {0x400d, 0x00},
571         {0x4058, 0x00},
572         {0x404e, 0x37},
573         {0x404f, 0x8f},
574         {0x4058, 0x00},
575         {0x4101, 0xb2},
576         {0x4303, 0x00},
577         {0x4304, 0x08},
578         {0x4307, 0x30},
579         {0x4311, 0x04},
580         {0x4315, 0x01},
581         {0x4511, 0x05},
582         {0x4512, 0x00},
583         {0x4806, 0x00},
584         {0x4816, 0x52},
585         {0x481f, 0x30},
586         {0x4826, 0x2c},
587         {0x4831, 0x64},
588         {0x4d00, 0x04},
589         {0x4d01, 0x71},
590         {0x4d02, 0xfd},
591         {0x4d03, 0xf5},
592         {0x4d04, 0x0c},
593         {0x4d05, 0xcc},
594         {0x4837, 0x0a},
595         {0x5000, 0x06},
596         {0x5001, 0x01},
597         {0x5002, 0x00},
598         {0x5003, 0x20},
599         {0x5046, 0x0a},
600         {0x5013, 0x00},
601         {0x5046, 0x0a},
602         {0x5780, 0x1c},
603         {0x5786, 0x20},
604         {0x5787, 0x10},
605         {0x5788, 0x18},
606         {0x578a, 0x04},
607         {0x578b, 0x02},
608         {0x578c, 0x02},
609         {0x578e, 0x06},
610         {0x578f, 0x02},
611         {0x5790, 0x02},
612         {0x5791, 0xff},
613         {0x5842, 0x01},
614         {0x5843, 0x2b},
615         {0x5844, 0x01},
616         {0x5845, 0x92},
617         {0x5846, 0x01},
618         {0x5847, 0x8f},
619         {0x5848, 0x01},
620         {0x5849, 0x0c},
621         {0x5e00, 0x00},
622         {0x5e10, 0x0c},
623         {OV5693_TABLE_END, 0x0000}
624 };
625
626 static const struct ov5693_reg ov5693_1920x1080_i2c[] = {
627         {OV5693_TABLE_RESET, 0x0},/*, 0xIncluding, 0xsw, 0xreset, 0x*/
628         {0x3001, 0x0a},
629         {0x3002, 0x80},
630         {0x3006, 0x00},
631         {0x3011, 0x21},
632         {0x3012, 0x09},
633         {0x3013, 0x10},
634         {0x3014, 0x00},
635         {0x3015, 0x08},
636         {0x3016, 0xf0},
637         {0x3017, 0xf0},
638         {0x3018, 0xf0},
639         {0x301b, 0xb4},
640         {0x301d, 0x02},
641         {0x3021, 0x00},
642         {0x3022, 0x01},
643         {0x3028, 0x44},
644         {0x3098, 0x03},
645         {0x3099, 0x1e},
646         {0x309a, 0x02},
647         {0x309b, 0x01},
648         {0x309c, 0x00},
649         {0x30a0, 0xd2},
650         {0x30a2, 0x01},
651         {0x30b2, 0x00},
652         {0x30b3, 0x64},
653         {0x30b4, 0x03},
654         {0x30b5, 0x04},
655         {0x30b6, 0x01},
656         {0x3104, 0x21},
657         {0x3106, 0x00},
658         {0x3406, 0x01},
659         {0x3500, 0x00},
660         {0x3501, 0x7b},
661         {0x3502, 0x00},
662         {0x3503, 0x07},
663         {0x3504, 0x00},
664         {0x3505, 0x00},
665         {0x3506, 0x00},
666         {0x3507, 0x02},
667         {0x3508, 0x00},
668         {0x3509, 0x10},
669         {0x350a, 0x00},
670         {0x350b, 0x40},
671         {0x3601, 0x0a},
672         {0x3602, 0x38},
673         {0x3612, 0x80},
674         {0x3620, 0x54},
675         {0x3621, 0xc7},
676         {0x3622, 0x0f},
677         {0x3625, 0x10},
678         {0x3630, 0x55},
679         {0x3631, 0xf4},
680         {0x3632, 0x00},
681         {0x3633, 0x34},
682         {0x3634, 0x02},
683         {0x364d, 0x0d},
684         {0x364f, 0xdd},
685         {0x3660, 0x04},
686         {0x3662, 0x10},
687         {0x3663, 0xf1},
688         {0x3665, 0x00},
689         {0x3666, 0x20},
690         {0x3667, 0x00},
691         {0x366a, 0x80},
692         {0x3680, 0xe0},
693         {0x3681, 0x00},
694         {0x3700, 0x42},
695         {0x3701, 0x14},
696         {0x3702, 0xa0},
697         {0x3703, 0xd8},
698         {0x3704, 0x78},
699         {0x3705, 0x02},
700         {0x3708, 0xe2},
701         {0x3709, 0xc3},
702         {0x370a, 0x00},
703         {0x370b, 0x20},
704         {0x370c, 0x0c},
705         {0x370d, 0x11},
706         {0x370e, 0x00},
707         {0x370f, 0x40},
708         {0x3710, 0x00},
709         {0x371a, 0x1c},
710         {0x371b, 0x05},
711         {0x371c, 0x01},
712         {0x371e, 0xa1},
713         {0x371f, 0x0c},
714         {0x3721, 0x00},
715         {0x3724, 0x10},
716         {0x3726, 0x00},
717         {0x372a, 0x01},
718         {0x3730, 0x10},
719         {0x3738, 0x22},
720         {0x3739, 0xe5},
721         {0x373a, 0x50},
722         {0x373b, 0x02},
723         {0x373c, 0x41},
724         {0x373f, 0x02},
725         {0x3740, 0x42},
726         {0x3741, 0x02},
727         {0x3742, 0x18},
728         {0x3743, 0x01},
729         {0x3744, 0x02},
730         {0x3747, 0x10},
731         {0x374c, 0x04},
732         {0x3751, 0xf0},
733         {0x3752, 0x00},
734         {0x3753, 0x00},
735         {0x3754, 0xc0},
736         {0x3755, 0x00},
737         {0x3756, 0x1a},
738         {0x3758, 0x00},
739         {0x3759, 0x0f},
740         {0x376b, 0x44},
741         {0x375c, 0x04},
742         {0x3774, 0x10},
743         {0x3776, 0x00},
744         {0x377f, 0x08},
745         {0x3780, 0x22},
746         {0x3781, 0x0c},
747         {0x3784, 0x2c},
748         {0x3785, 0x1e},
749         {0x378f, 0xf5},
750         {0x3791, 0xb0},
751         {0x3795, 0x00},
752         {0x3796, 0x64},
753         {0x3797, 0x11},
754         {0x3798, 0x30},
755         {0x3799, 0x41},
756         {0x379a, 0x07},
757         {0x379b, 0xb0},
758         {0x379c, 0x0c},
759         {0x37c5, 0x00},
760         {0x37c6, 0x00},
761         {0x37c7, 0x00},
762         {0x37c9, 0x00},
763         {0x37ca, 0x00},
764         {0x37cb, 0x00},
765         {0x37de, 0x00},
766         {0x37df, 0x00},
767         {0x3800, 0x00},
768         {0x3801, 0x00},
769         {0x3802, 0x00},
770         {0x3803, 0xf8},
771         {0x3804, 0x0a},
772         {0x3805, 0x3f},
773         {0x3806, 0x06},
774         {0x3807, 0xab},
775         {0x3808, 0x07},
776         {0x3809, 0x80},
777         {0x380a, 0x04},
778         {0x380b, 0x38},
779         {0x380c, 0x0a},
780         {0x380d, 0x80},
781         {0x380e, 0x07},
782         {0x380f, 0xc0},
783         {0x3810, 0x00},
784         {0x3811, 0x02},
785         {0x3812, 0x00},
786         {0x3813, 0x02},
787         {0x3814, 0x11},
788         {0x3815, 0x11},
789         {0x3820, 0x00},
790         {0x3821, 0x1e},
791         {0x3823, 0x00},
792         {0x3824, 0x00},
793         {0x3825, 0x00},
794         {0x3826, 0x00},
795         {0x3827, 0x00},
796         {0x382a, 0x04},
797         {0x3a04, 0x06},
798         {0x3a05, 0x14},
799         {0x3a06, 0x00},
800         {0x3a07, 0xfe},
801         {0x3b00, 0x00},
802         {0x3b02, 0x00},
803         {0x3b03, 0x00},
804         {0x3b04, 0x00},
805         {0x3b05, 0x00},
806         {0x3e07, 0x20},
807         {0x4000, 0x08},
808         {0x4001, 0x04},
809         {0x4002, 0x45},
810         {0x4004, 0x08},
811         {0x4005, 0x18},
812         {0x4006, 0x20},
813         {0x4008, 0x24},
814         {0x4009, 0x10},
815         {0x400c, 0x00},
816         {0x400d, 0x00},
817         {0x4058, 0x00},
818         {0x404e, 0x37},
819         {0x404f, 0x8f},
820         {0x4058, 0x00},
821         {0x4101, 0xb2},
822         {0x4303, 0x00},
823         {0x4304, 0x08},
824         {0x4307, 0x30},
825         {0x4311, 0x04},
826         {0x4315, 0x01},
827         {0x4511, 0x05},
828         {0x4512, 0x01},
829         {0x4806, 0x00},
830         {0x4816, 0x52},
831         {0x481f, 0x30},
832         {0x4826, 0x2c},
833         {0x4831, 0x64},
834         {0x4d00, 0x04},
835         {0x4d01, 0x71},
836         {0x4d02, 0xfd},
837         {0x4d03, 0xf5},
838         {0x4d04, 0x0c},
839         {0x4d05, 0xcc},
840         {0x4837, 0x0a},
841         {0x5000, 0x06},
842         {0x5001, 0x01},
843         {0x5002, 0x80},
844         {0x5003, 0x20},
845         {0x5046, 0x0a},
846         {0x5013, 0x00},
847         {0x5046, 0x0a},
848         {0x5780, 0x1c},
849         {0x5786, 0x20},
850         {0x5787, 0x10},
851         {0x5788, 0x18},
852         {0x578a, 0x04},
853         {0x578b, 0x02},
854         {0x578c, 0x02},
855         {0x578e, 0x06},
856         {0x578f, 0x02},
857         {0x5790, 0x02},
858         {0x5791, 0xff},
859         {0x5842, 0x01},
860         {0x5843, 0x2b},
861         {0x5844, 0x01},
862         {0x5845, 0x92},
863         {0x5846, 0x01},
864         {0x5847, 0x8f},
865         {0x5848, 0x01},
866         {0x5849, 0x0c},
867         {0x5e00, 0x00},
868         {0x5e10, 0x0c},
869         {OV5693_TABLE_END, 0x0000}
870 };
871
872
873 static const struct ov5693_reg ov5693_1280x720_120fps_i2c[] = {
874         {OV5693_TABLE_RESET, 0},/* Including sw reset */
875         {0x3001, 0x0a},
876         {0x3002, 0x80},
877         {0x3006, 0x00},
878         {0x3011, 0x21},
879         {0x3012, 0x09},
880         {0x3013, 0x10},
881         {0x3014, 0x00},
882         {0x3015, 0x08},
883         {0x3016, 0xf0},
884         {0x3017, 0xf0},
885         {0x3018, 0xf0},
886         {0x301b, 0xb4},
887         {0x301d, 0x02},
888         {0x3021, 0x00},
889         {0x3022, 0x01},
890         {0x3028, 0x44},
891         {0x3098, 0x03},
892         {0x3099, 0x1e},
893         {0x309a, 0x02},
894         {0x309b, 0x01},
895         {0x309c, 0x00},
896         {0x30a0, 0xd2},
897         {0x30a2, 0x01},
898         {0x30b2, 0x00},
899         {0x30b3, 0x64},
900         {0x30b4, 0x03},
901         {0x30b5, 0x04},
902         {0x30b6, 0x01},
903         {0x3104, 0x21},
904         {0x3106, 0x00},
905         {0x3406, 0x01},
906         {0x3500, 0x00},
907         {0x3501, 0x2e},
908         {0x3502, 0x80},
909         {0x3503, 0x07},
910         {0x3504, 0x00},
911         {0x3505, 0x00},
912         {0x3506, 0x00},
913         {0x3507, 0x02},
914         {0x3508, 0x00},
915         {0x3509, 0x10},
916         {0x350a, 0x00},
917         {0x350b, 0x40},
918         {0x3601, 0x0a},
919         {0x3602, 0x38},
920         {0x3612, 0x80},
921         {0x3620, 0x54},
922         {0x3621, 0xc7},
923         {0x3622, 0x0f},
924         {0x3625, 0x10},
925         {0x3630, 0x55},
926         {0x3631, 0xf4},
927         {0x3632, 0x00},
928         {0x3633, 0x34},
929         {0x3634, 0x02},
930         {0x364d, 0x0d},
931         {0x364f, 0xdd},
932         {0x3660, 0x04},
933         {0x3662, 0x10},
934         {0x3663, 0xf1},
935         {0x3665, 0x00},
936         {0x3666, 0x20},
937         {0x3667, 0x00},
938         {0x366a, 0x80},
939         {0x3680, 0xe0},
940         {0x3681, 0x00},
941         {0x3700, 0x42},
942         {0x3701, 0x14},
943         {0x3702, 0xa0},
944         {0x3703, 0xd8},
945         {0x3704, 0x78},
946         {0x3705, 0x02},
947         {0x3708, 0xe6},
948         {0x3709, 0xc7},
949         {0x370a, 0x00},
950         {0x370b, 0x20},
951         {0x370c, 0x0c},
952         {0x370d, 0x11},
953         {0x370e, 0x00},
954         {0x370f, 0x40},
955         {0x3710, 0x00},
956         {0x371a, 0x1c},
957         {0x371b, 0x05},
958         {0x371c, 0x01},
959         {0x371e, 0xa1},
960         {0x371f, 0x0c},
961         {0x3721, 0x00},
962         {0x3724, 0x10},
963         {0x3726, 0x00},
964         {0x372a, 0x01},
965         {0x3730, 0x10},
966         {0x3738, 0x22},
967         {0x3739, 0xe5},
968         {0x373a, 0x50},
969         {0x373b, 0x02},
970         {0x373c, 0x41},
971         {0x373f, 0x02},
972         {0x3740, 0x42},
973         {0x3741, 0x02},
974         {0x3742, 0x18},
975         {0x3743, 0x01},
976         {0x3744, 0x02},
977         {0x3747, 0x10},
978         {0x374c, 0x04},
979         {0x3751, 0xf0},
980         {0x3752, 0x00},
981         {0x3753, 0x00},
982         {0x3754, 0xc0},
983         {0x3755, 0x00},
984         {0x3756, 0x1a},
985         {0x3758, 0x00},
986         {0x3759, 0x0f},
987         {0x376b, 0x44},
988         {0x375c, 0x04},
989         {0x3774, 0x10},
990         {0x3776, 0x00},
991         {0x377f, 0x08},
992         {0x3780, 0x22},
993         {0x3781, 0x0c},
994         {0x3784, 0x2c},
995         {0x3785, 0x1e},
996         {0x378f, 0xf5},
997         {0x3791, 0xb0},
998         {0x3795, 0x00},
999         {0x3796, 0x64},
1000         {0x3797, 0x11},
1001         {0x3798, 0x30},
1002         {0x3799, 0x41},
1003         {0x379a, 0x07},
1004         {0x379b, 0xb0},
1005         {0x379c, 0x0c},
1006         {0x37c5, 0x00},
1007         {0x37c6, 0x00},
1008         {0x37c7, 0x00},
1009         {0x37c9, 0x00},
1010         {0x37ca, 0x00},
1011         {0x37cb, 0x00},
1012         {0x37de, 0x00},
1013         {0x37df, 0x00},
1014         {0x3800, 0x00},
1015         {0x3801, 0x00},
1016         {0x3802, 0x00},
1017         {0x3803, 0xf4},
1018         {0x3804, 0x0a},
1019         {0x3805, 0x3f},
1020         {0x3806, 0x06},
1021         {0x3807, 0xab},
1022         {0x3808, 0x05},
1023         {0x3809, 0x00},
1024         {0x380a, 0x02},
1025         {0x380b, 0xd0},
1026         {0x380c, 0x06},
1027         {0x380d, 0xd8},
1028         {0x380e, 0x02},
1029         {0x380f, 0xf8},
1030         {0x3810, 0x00},
1031         {0x3811, 0x02},
1032         {0x3812, 0x00},
1033         {0x3813, 0x02},
1034         {0x3814, 0x31},
1035         {0x3815, 0x31},
1036         {0x3820, 0x04},
1037         {0x3821, 0x1f},
1038         {0x3823, 0x00},
1039         {0x3824, 0x00},
1040         {0x3825, 0x00},
1041         {0x3826, 0x00},
1042         {0x3827, 0x00},
1043         {0x382a, 0x04},
1044         {0x3a04, 0x06},
1045         {0x3a05, 0x14},
1046         {0x3a06, 0x00},
1047         {0x3a07, 0xfe},
1048         {0x3b00, 0x00},
1049         {0x3b02, 0x00},
1050         {0x3b03, 0x00},
1051         {0x3b04, 0x00},
1052         {0x3b05, 0x00},
1053         {0x3e07, 0x20},
1054         {0x4000, 0x08},
1055         {0x4001, 0x04},
1056         {0x4002, 0x45},
1057         {0x4004, 0x08},
1058         {0x4005, 0x18},
1059         {0x4006, 0x20},
1060         {0x4008, 0x24},
1061         {0x4009, 0x10},
1062         {0x400c, 0x00},
1063         {0x400d, 0x00},
1064         {0x4058, 0x00},
1065         {0x404e, 0x37},
1066         {0x404f, 0x8f},
1067         {0x4058, 0x00},
1068         {0x4101, 0xb2},
1069         {0x4303, 0x00},
1070         {0x4304, 0x08},
1071         {0x4307, 0x30},
1072         {0x4311, 0x04},
1073         {0x4315, 0x01},
1074         {0x4511, 0x05},
1075         {0x4512, 0x00},
1076         {0x4806, 0x00},
1077         {0x4816, 0x52},
1078         {0x481f, 0x30},
1079         {0x4826, 0x2c},
1080         {0x4831, 0x64},
1081         {0x4d00, 0x04},
1082         {0x4d01, 0x71},
1083         {0x4d02, 0xfd},
1084         {0x4d03, 0xf5},
1085         {0x4d04, 0x0c},
1086         {0x4d05, 0xcc},
1087         {0x4837, 0x0a},
1088         {0x5000, 0x06},
1089         {0x5001, 0x01},
1090         {0x5002, 0x00},
1091         {0x5003, 0x20},
1092         {0x5046, 0x0a},
1093         {0x5013, 0x00},
1094         {0x5046, 0x0a},
1095         {0x5780, 0x1c},
1096         {0x5786, 0x20},
1097         {0x5787, 0x10},
1098         {0x5788, 0x18},
1099         {0x578a, 0x04},
1100         {0x578b, 0x02},
1101         {0x578c, 0x02},
1102         {0x578e, 0x06},
1103         {0x578f, 0x02},
1104         {0x5790, 0x02},
1105         {0x5791, 0xff},
1106         {0x5842, 0x01},
1107         {0x5843, 0x2b},
1108         {0x5844, 0x01},
1109         {0x5845, 0x92},
1110         {0x5846, 0x01},
1111         {0x5847, 0x8f},
1112         {0x5848, 0x01},
1113         {0x5849, 0x0c},
1114         {0x5e00, 0x00},
1115         {0x5e10, 0x0c},
1116         {OV5693_TABLE_END, 0x0000}
1117 };
1118
1119 static const struct ov5693_reg ov5693_2592x1944_HDR_24fps_i2c[] = {
1120         {OV5693_TABLE_RESET, 0},/* Including sw reset */
1121         {0x0103, 0x01},
1122         {0x3001, 0x0a},
1123         {0x3002, 0x80},
1124         {0x3006, 0x00},
1125         {0x3011, 0x21},
1126         {0x3012, 0x09},
1127         {0x3013, 0x10},
1128         {0x3014, 0x00},
1129         {0x3015, 0x08},
1130         {0x3016, 0xf0},
1131         {0x3017, 0xf0},
1132         {0x3018, 0xf0},
1133         {0x301b, 0xb4},
1134         {0x301d, 0x02},
1135         {0x3021, 0x00},
1136         {0x3022, 0x01},
1137         {0x3028, 0x44},
1138         {0x3098, 0x02},
1139         {0x3099, 0x16},
1140         {0x309a, 0x02},
1141         {0x309b, 0x01},
1142         {0x309c, 0x00},
1143         {0x30b2, 0x00},
1144         {0x30b3, 0x6e},
1145         {0x30b4, 0x03},
1146         {0x30a0, 0xd2},
1147         {0x30a2, 0x01},
1148         {0x30b5, 0x04},
1149         {0x30b6, 0x01},
1150         {0x3104, 0x21},
1151         {0x3106, 0x00},
1152         {0x3406, 0x01},
1153         {0x3500, 0x00},
1154         {0x3501, 0x7b},
1155         {0x3502, 0x80},
1156         {0x3503, 0x07},
1157         {0x3504, 0x00},
1158         {0x3505, 0x00},
1159         {0x3506, 0x00},
1160         {0x3507, 0x01},
1161         {0x3508, 0x80},
1162         {0x3509, 0x10},
1163         {0x350a, 0x00},
1164         {0x350b, 0x40},
1165         {0x3601, 0x0a},
1166         {0x3602, 0x38},
1167         {0x3612, 0x80},
1168         {0x3620, 0x54},
1169         {0x3621, 0xc7},
1170         {0x3622, 0x05},
1171         {0x3625, 0x10},
1172         {0x3630, 0x55},
1173         {0x3631, 0xf4},
1174         {0x3632, 0x00},
1175         {0x3633, 0x34},
1176         {0x3634, 0x02},
1177         {0x364d, 0x0d},
1178         {0x364f, 0xdd},
1179         {0x3660, 0x04},
1180         {0x3662, 0x10},
1181         {0x3663, 0xf1},
1182         {0x3665, 0x00},
1183         {0x3666, 0x20},
1184         {0x3667, 0x00},
1185         {0x366a, 0x80},
1186         {0x3680, 0xe0},
1187         {0x3681, 0x00},
1188         {0x3700, 0x42},
1189         {0x3701, 0x14},
1190         {0x3702, 0xa0},
1191         {0x3703, 0xa8},
1192         {0x3704, 0x78},
1193         {0x3705, 0x02},
1194         {0x3708, 0xe2},
1195         {0x3709, 0xc3},
1196         {0x370a, 0x00},
1197         {0x370b, 0x20},
1198         {0x370c, 0x0c},
1199         {0x370d, 0x11},
1200         {0x370e, 0x00},
1201         {0x370f, 0x40},
1202         {0x3710, 0x00},
1203         {0x371a, 0x0c},
1204         {0x371b, 0x05},
1205         {0x371c, 0x01},
1206         {0x371e, 0xa1},
1207         {0x371f, 0x0c},
1208         {0x3721, 0x00},
1209         {0x3724, 0x10},
1210         {0x3726, 0x00},
1211         {0x372a, 0x01},
1212         {0x3730, 0x10},
1213         {0x3738, 0x22},
1214         {0x3739, 0xe5},
1215         {0x373a, 0x50},
1216         {0x373b, 0x02},
1217         {0x373c, 0x41},
1218         {0x373f, 0x02},
1219         {0x3740, 0x42},
1220         {0x3741, 0x02},
1221         {0x3742, 0x18},
1222         {0x3743, 0x01},
1223         {0x3744, 0x02},
1224         {0x3747, 0x10},
1225         {0x374c, 0x04},
1226         {0x3751, 0xf0},
1227         {0x3752, 0x00},
1228         {0x3753, 0x00},
1229         {0x3754, 0xc0},
1230         {0x3755, 0x00},
1231         {0x3756, 0x1a},
1232         {0x3758, 0x00},
1233         {0x3759, 0x0f},
1234         {0x376b, 0x44},
1235         {0x375c, 0x04},
1236         {0x3774, 0x10},
1237         {0x3776, 0x00},
1238         {0x377f, 0x08},
1239         {0x3780, 0x22},
1240         {0x3781, 0x0c},
1241         {0x3784, 0x2c},
1242         {0x3785, 0x1e},
1243         {0x378f, 0xf5},
1244         {0x3791, 0xb0},
1245         {0x3795, 0x00},
1246         {0x3796, 0x64},
1247         {0x3797, 0x11},
1248         {0x3798, 0x30},
1249         {0x3799, 0x41},
1250         {0x379a, 0x07},
1251         {0x379b, 0xb0},
1252         {0x379c, 0x0c},
1253         {0x37c5, 0x00},
1254         {0x37c6, 0x00},
1255         {0x37c7, 0x00},
1256         {0x37c9, 0x00},
1257         {0x37ca, 0x00},
1258         {0x37cb, 0x00},
1259         {0x37de, 0x00},
1260         {0x37df, 0x00},
1261         {0x3800, 0x00},
1262         {0x3801, 0x02},
1263         {0x3802, 0x00},
1264         {0x3803, 0x06},
1265         {0x3804, 0x0a},
1266         {0x3805, 0x41},
1267         {0x3806, 0x07},
1268         {0x3807, 0xa1},
1269         {0x3808, 0x0a},
1270         {0x3809, 0x20},
1271         {0x380a, 0x07},
1272         {0x380b, 0x98},
1273         {0x380c, 0x0e},
1274         {0x380d, 0x70},
1275         {0x380e, 0x07},
1276         {0x380f, 0xc0},
1277         {0x3810, 0x00},
1278         {0x3811, 0x02},
1279         {0x3812, 0x00},
1280         {0x3813, 0x02},
1281         {0x3814, 0x11},
1282         {0x3815, 0x11},
1283         {0x3820, 0x00},
1284         {0x3821, 0x9e},
1285         {0x3823, 0x00},
1286         {0x3824, 0x00},
1287         {0x3825, 0x00},
1288         {0x3826, 0x00},
1289         {0x3827, 0x00},
1290         {0x382a, 0x04},
1291         {0x3a04, 0x09},
1292         {0x3a05, 0xa9},
1293         {0x3a06, 0x00},
1294         {0x3a07, 0xfe},
1295         {0x3b00, 0x00},
1296         {0x3b02, 0x00},
1297         {0x3b03, 0x00},
1298         {0x3b04, 0x00},
1299         {0x3b05, 0x00},
1300         {0x3e07, 0x20},
1301         {0x4000, 0x08},
1302         {0x4001, 0x04},
1303         {0x4002, 0x45},
1304         {0x4004, 0x08},
1305         {0x4005, 0x18},
1306         {0x4006, 0x20},
1307         {0x4008, 0x24},
1308         {0x4009, 0x10},
1309         {0x400c, 0x00},
1310         {0x400d, 0x00},
1311         {0x4058, 0x00},
1312         {0x404e, 0x37},
1313         {0x404f, 0x8f},
1314         {0x4058, 0x00},
1315         {0x4101, 0xb2},
1316         {0x4303, 0x00},
1317         {0x4304, 0x08},
1318         {0x4307, 0x30},
1319         {0x4311, 0x04},
1320         {0x4315, 0x01},
1321         {0x4511, 0x05},
1322         {0x4512, 0x01},
1323         {0x4806, 0x00},
1324         {0x4816, 0x52},
1325         {0x481f, 0x30},
1326         {0x4826, 0x2c},
1327         {0x4831, 0x64},
1328         {0x4d00, 0x04},
1329         {0x4d01, 0x71},
1330         {0x4d02, 0xfd},
1331         {0x4d03, 0xf5},
1332         {0x4d04, 0x0c},
1333         {0x4d05, 0xcc},
1334         {0x4837, 0x0a},
1335         {0x5000, 0x06},
1336         {0x5001, 0x01},
1337         {0x5002, 0x00},
1338         {0x5003, 0x20},
1339         {0x5046, 0x0a},
1340         {0x5013, 0x00},
1341         {0x5046, 0x0a},
1342         {0x5780, 0x1c},
1343         {0x5786, 0x20},
1344         {0x5787, 0x10},
1345         {0x5788, 0x18},
1346         {0x578a, 0x04},
1347         {0x578b, 0x02},
1348         {0x578c, 0x02},
1349         {0x578e, 0x06},
1350         {0x578f, 0x02},
1351         {0x5790, 0x02},
1352         {0x5791, 0xff},
1353         {0x5842, 0x01},
1354         {0x5843, 0x2b},
1355         {0x5844, 0x01},
1356         {0x5845, 0x92},
1357         {0x5846, 0x01},
1358         {0x5847, 0x8f},
1359         {0x5848, 0x01},
1360         {0x5849, 0x0c},
1361         {0x5e00, 0x00},
1362         {0x5e10, 0x0c},
1363         {OV5693_TABLE_END, 0x0000}
1364 };
1365
1366 static const struct ov5693_reg ov5693_2560x1440_HDR_24fps_i2c[] = {
1367         {OV5693_TABLE_RESET, 0},/* Including sw reset */
1368         {0x0103, 0x01},
1369         {0x3001, 0x0a},
1370         {0x3002, 0x80},
1371         {0x3006, 0x00},
1372         {0x3011, 0x21},
1373         {0x3012, 0x09},
1374         {0x3013, 0x10},
1375         {0x3014, 0x00},
1376         {0x3015, 0x08},
1377         {0x3016, 0xf0},
1378         {0x3017, 0xf0},
1379         {0x3018, 0xf0},
1380         {0x301b, 0xb4},
1381         {0x301d, 0x02},
1382         {0x3021, 0x00},
1383         {0x3022, 0x01},
1384         {0x3028, 0x44},
1385         {0x3098, 0x02},
1386         {0x3099, 0x16},
1387         {0x309a, 0x02},
1388         {0x309b, 0x01},
1389         {0x309c, 0x00},
1390         {0x30b2, 0x00},
1391         {0x30b3, 0x6e},
1392         {0x30b4, 0x03},
1393         {0x30a0, 0xd2},
1394         {0x30a2, 0x01},
1395         {0x30b5, 0x04},
1396         {0x30b6, 0x01},
1397         {0x3104, 0x21},
1398         {0x3106, 0x00},
1399         {0x3400, 0x04},
1400         {0x3401, 0x00},
1401         {0x3402, 0x04},
1402         {0x3403, 0x00},
1403         {0x3404, 0x04},
1404         {0x3405, 0x00},
1405         {0x3406, 0x01},
1406         {0x3500, 0x00},
1407         {0x3501, 0x7b},
1408         {0x3502, 0x80},
1409         {0x3503, 0x07},
1410         {0x3504, 0x00},
1411         {0x3505, 0x00},
1412         {0x3506, 0x00},
1413         {0x3507, 0x01},
1414         {0x3508, 0x80},
1415         {0x3509, 0x10},
1416         {0x350a, 0x00},
1417         {0x350b, 0x40},
1418         {0x3601, 0x0a},
1419         {0x3602, 0x38},
1420         {0x3612, 0x80},
1421         {0x3620, 0x54},
1422         {0x3621, 0xc7},
1423         {0x3622, 0x05},
1424         {0x3625, 0x10},
1425         {0x3630, 0x55},
1426         {0x3631, 0xf4},
1427         {0x3632, 0x00},
1428         {0x3633, 0x34},
1429         {0x3634, 0x02},
1430         {0x364d, 0x0d},
1431         {0x364f, 0xdd},
1432         {0x3660, 0x04},
1433         {0x3662, 0x10},
1434         {0x3663, 0xf1},
1435         {0x3665, 0x00},
1436         {0x3666, 0x20},
1437         {0x3667, 0x00},
1438         {0x366a, 0x80},
1439         {0x3680, 0xe0},
1440         {0x3681, 0x00},
1441         {0x3700, 0x42},
1442         {0x3701, 0x14},
1443         {0x3702, 0xa0},
1444         {0x3703, 0xa8},
1445         {0x3704, 0x78},
1446         {0x3705, 0x02},
1447         {0x3708, 0xe2},
1448         {0x3709, 0xc3},
1449         {0x370a, 0x00},
1450         {0x370b, 0x20},
1451         {0x370c, 0x0c},
1452         {0x370d, 0x11},
1453         {0x370e, 0x00},
1454         {0x370f, 0x40},
1455         {0x3710, 0x00},
1456         {0x371a, 0x0c},
1457         {0x371b, 0x05},
1458         {0x371c, 0x01},
1459         {0x371e, 0xa1},
1460         {0x371f, 0x0c},
1461         {0x3721, 0x00},
1462         {0x3724, 0x10},
1463         {0x3726, 0x00},
1464         {0x372a, 0x01},
1465         {0x3730, 0x10},
1466         {0x3738, 0x22},
1467         {0x3739, 0xe5},
1468         {0x373a, 0x50},
1469         {0x373b, 0x02},
1470         {0x373c, 0x41},
1471         {0x373f, 0x02},
1472         {0x3740, 0x42},
1473         {0x3741, 0x02},
1474         {0x3742, 0x18},
1475         {0x3743, 0x01},
1476         {0x3744, 0x02},
1477         {0x3747, 0x10},
1478         {0x374c, 0x04},
1479         {0x3751, 0xf0},
1480         {0x3752, 0x00},
1481         {0x3753, 0x00},
1482         {0x3754, 0xc0},
1483         {0x3755, 0x00},
1484         {0x3756, 0x1a},
1485         {0x3758, 0x00},
1486         {0x3759, 0x0f},
1487         {0x376b, 0x44},
1488         {0x375c, 0x04},
1489         {0x3774, 0x10},
1490         {0x3776, 0x00},
1491         {0x377f, 0x08},
1492         {0x3780, 0x22},
1493         {0x3781, 0x0c},
1494         {0x3784, 0x2c},
1495         {0x3785, 0x1e},
1496         {0x378f, 0xf5},
1497         {0x3791, 0xb0},
1498         {0x3795, 0x00},
1499         {0x3796, 0x64},
1500         {0x3797, 0x11},
1501         {0x3798, 0x30},
1502         {0x3799, 0x41},
1503         {0x379a, 0x07},
1504         {0x379b, 0xb0},
1505         {0x379c, 0x0c},
1506         {0x37c5, 0x00},
1507         {0x37c6, 0x00},
1508         {0x37c7, 0x00},
1509         {0x37c9, 0x00},
1510         {0x37ca, 0x00},
1511         {0x37cb, 0x00},
1512         {0x37de, 0x00},
1513         {0x37df, 0x00},
1514         {0x3800, 0x00},
1515         {0x3801, 0x12},
1516         {0x3802, 0x01},
1517         {0x3803, 0x02},
1518         {0x3804, 0x0a},
1519         {0x3805, 0x31},
1520         {0x3806, 0x06},
1521         {0x3807, 0xa5},
1522         {0x3808, 0x0a},
1523         {0x3809, 0x00},
1524         {0x380a, 0x05},
1525         {0x380b, 0xa0},
1526         {0x380c, 0x0b},
1527         {0x380d, 0x8e},
1528         {0x380e, 0x09},
1529         {0x380f, 0xaf},
1530         {0x3810, 0x00},
1531         {0x3811, 0x10},
1532         {0x3812, 0x00},
1533         {0x3813, 0x02},
1534         {0x3814, 0x11},
1535         {0x3815, 0x11},
1536         {0x3820, 0x00},
1537         {0x3821, 0x9e},
1538         {0x3823, 0x00},
1539         {0x3824, 0x00},
1540         {0x3825, 0x00},
1541         {0x3826, 0x00},
1542         {0x3827, 0x00},
1543         {0x382a, 0x04},
1544         {0x3a04, 0x09},
1545         {0x3a05, 0xa9},
1546         {0x3a06, 0x00},
1547         {0x3a07, 0xfe},
1548         {0x3b00, 0x00},
1549         {0x3b02, 0x00},
1550         {0x3b03, 0x00},
1551         {0x3b04, 0x00},
1552         {0x3b05, 0x00},
1553         {0x3e07, 0x20},
1554         {0x4000, 0x08},
1555         {0x4001, 0x04},
1556         {0x4002, 0x45},
1557         {0x4004, 0x08},
1558         {0x4005, 0x18},
1559         {0x4006, 0x20},
1560         {0x4008, 0x24},
1561         {0x4009, 0x10},
1562         {0x400c, 0x00},
1563         {0x400d, 0x00},
1564         {0x4058, 0x00},
1565         {0x404e, 0x37},
1566         {0x404f, 0x8f},
1567         {0x4058, 0x00},
1568         {0x4101, 0xb2},
1569         {0x4303, 0x00},
1570         {0x4304, 0x08},
1571         {0x4307, 0x30},
1572         {0x4311, 0x04},
1573         {0x4315, 0x01},
1574         {0x4511, 0x05},
1575         {0x4512, 0x01},
1576         {0x4806, 0x00},
1577         {0x4816, 0x52},
1578         {0x481f, 0x30},
1579         {0x4826, 0x2c},
1580         {0x4831, 0x64},
1581         {0x4d00, 0x04},
1582         {0x4d01, 0x71},
1583         {0x4d02, 0xfd},
1584         {0x4d03, 0xf5},
1585         {0x4d04, 0x0c},
1586         {0x4d05, 0xcc},
1587         {0x4837, 0x0a},
1588         {0x5000, 0x06},
1589         {0x5001, 0x01},
1590         {0x5002, 0x00},
1591         {0x5003, 0x20},
1592         {0x5046, 0x0a},
1593         {0x5013, 0x00},
1594         {0x5046, 0x0a},
1595         {0x5780, 0x1c},
1596         {0x5786, 0x20},
1597         {0x5787, 0x10},
1598         {0x5788, 0x18},
1599         {0x578a, 0x04},
1600         {0x578b, 0x02},
1601         {0x578c, 0x02},
1602         {0x578e, 0x06},
1603         {0x578f, 0x02},
1604         {0x5790, 0x02},
1605         {0x5791, 0xff},
1606         {0x5842, 0x01},
1607         {0x5843, 0x2b},
1608         {0x5844, 0x01},
1609         {0x5845, 0x92},
1610         {0x5846, 0x01},
1611         {0x5847, 0x8f},
1612         {0x5848, 0x01},
1613         {0x5849, 0x0c},
1614         {0x5e00, 0x00},
1615         {0x5e10, 0x0c},
1616         {OV5693_TABLE_END, 0x0000}
1617 };
1618
1619 static const struct ov5693_reg ov5693_1920x1080_HDR_30fps_i2c[] = {
1620         {OV5693_TABLE_RESET, 0},/* Including sw reset */
1621         {0x0103, 0x01},
1622         {0x3001, 0x0a},
1623         {0x3002, 0x80},
1624         {0x3006, 0x00},
1625         {0x3011, 0x21},
1626         {0x3012, 0x09},
1627         {0x3013, 0x10},
1628         {0x3014, 0x00},
1629         {0x3015, 0x08},
1630         {0x3016, 0xf0},
1631         {0x3017, 0xf0},
1632         {0x3018, 0xf0},
1633         {0x301b, 0xb4},
1634         {0x301d, 0x02},
1635         {0x3021, 0x00},
1636         {0x3022, 0x01},
1637         {0x3028, 0x44},
1638         {0x3098, 0x03},
1639         {0x3099, 0x1e},
1640         {0x309a, 0x02},
1641         {0x309b, 0x01},
1642         {0x309c, 0x00},
1643         {0x30a0, 0xd2},
1644         {0x30a2, 0x01},
1645         {0x30b2, 0x00},
1646         {0x30b3, 0x64},
1647         {0x30b4, 0x03},
1648         {0x30b5, 0x04},
1649         {0x30b6, 0x01},
1650         {0x3104, 0x21},
1651         {0x3106, 0x00},
1652         {0x3406, 0x01},
1653         {0x3500, 0x00},
1654         {0x3501, 0x72},
1655         {0x3502, 0x00},
1656         {0x3503, 0x07},
1657         {0x3504, 0x00},
1658         {0x3505, 0x00},
1659         {0x3506, 0x00},
1660         {0x3507, 0x01},
1661         {0x3508, 0x80},
1662         {0x3509, 0x10},
1663         {0x350a, 0x00},
1664         {0x350b, 0x40},
1665         {0x3601, 0x0a},
1666         {0x3602, 0x38},
1667         {0x3612, 0x80},
1668         {0x3620, 0x54},
1669         {0x3621, 0xc7},
1670         {0x3622, 0x0f},
1671         {0x3625, 0x10},
1672         {0x3630, 0x55},
1673         {0x3631, 0xf4},
1674         {0x3632, 0x00},
1675         {0x3633, 0x34},
1676         {0x3634, 0x02},
1677         {0x364d, 0x0d},
1678         {0x364f, 0xdd},
1679         {0x3660, 0x04},
1680         {0x3662, 0x10},
1681         {0x3663, 0xf1},
1682         {0x3665, 0x00},
1683         {0x3666, 0x20},
1684         {0x3667, 0x00},
1685         {0x366a, 0x80},
1686         {0x3680, 0xe0},
1687         {0x3681, 0x00},
1688         {0x3700, 0x42},
1689         {0x3701, 0x14},
1690         {0x3702, 0xa0},
1691         {0x3703, 0xd8},
1692         {0x3704, 0x78},
1693         {0x3705, 0x02},
1694         {0x3708, 0xe2},
1695         {0x3709, 0xc3},
1696         {0x370a, 0x00},
1697         {0x370b, 0x20},
1698         {0x370c, 0x0c},
1699         {0x370d, 0x11},
1700         {0x370e, 0x00},
1701         {0x370f, 0x40},
1702         {0x3710, 0x00},
1703         {0x371a, 0x1c},
1704         {0x371b, 0x05},
1705         {0x371c, 0x01},
1706         {0x371e, 0xa1},
1707         {0x371f, 0x0c},
1708         {0x3721, 0x00},
1709         {0x3724, 0x10},
1710         {0x3726, 0x00},
1711         {0x372a, 0x01},
1712         {0x3730, 0x10},
1713         {0x3738, 0x22},
1714         {0x3739, 0xe5},
1715         {0x373a, 0x50},
1716         {0x373b, 0x02},
1717         {0x373c, 0x41},
1718         {0x373f, 0x02},
1719         {0x3740, 0x42},
1720         {0x3741, 0x02},
1721         {0x3742, 0x18},
1722         {0x3743, 0x01},
1723         {0x3744, 0x02},
1724         {0x3747, 0x10},
1725         {0x374c, 0x04},
1726         {0x3751, 0xf0},
1727         {0x3752, 0x00},
1728         {0x3753, 0x00},
1729         {0x3754, 0xc0},
1730         {0x3755, 0x00},
1731         {0x3756, 0x1a},
1732         {0x3758, 0x00},
1733         {0x3759, 0x0f},
1734         {0x376b, 0x44},
1735         {0x375c, 0x04},
1736         {0x3774, 0x10},
1737         {0x3776, 0x00},
1738         {0x377f, 0x08},
1739         {0x3780, 0x22},
1740         {0x3781, 0x0c},
1741         {0x3784, 0x2c},
1742         {0x3785, 0x1e},
1743         {0x378f, 0xf5},
1744         {0x3791, 0xb0},
1745         {0x3795, 0x00},
1746         {0x3796, 0x64},
1747         {0x3797, 0x11},
1748         {0x3798, 0x30},
1749         {0x3799, 0x41},
1750         {0x379a, 0x07},
1751         {0x379b, 0xb0},
1752         {0x379c, 0x0c},
1753         {0x37c5, 0x00},
1754         {0x37c6, 0x00},
1755         {0x37c7, 0x00},
1756         {0x37c9, 0x00},
1757         {0x37ca, 0x00},
1758         {0x37cb, 0x00},
1759         {0x37de, 0x00},
1760         {0x37df, 0x00},
1761         {0x3800, 0x01},
1762         {0x3801, 0x70},
1763         {0x3802, 0x01},
1764         {0x3803, 0xbc},
1765         {0x3804, 0x09},
1766         {0x3805, 0x0f},
1767         {0x3806, 0x05},
1768         {0x3807, 0xff},
1769         {0x3808, 0x07},
1770         {0x3809, 0x80},
1771         {0x380a, 0x04},
1772         {0x380b, 0x38},
1773         {0x380c, 0x0b},
1774         {0x380d, 0x40},
1775         {0x380e, 0x07},
1776         {0x380f, 0x3a},
1777         {0x3810, 0x00},
1778         {0x3811, 0x02},
1779         {0x3812, 0x00},
1780         {0x3813, 0x02},
1781         {0x3814, 0x11},
1782         {0x3815, 0x11},
1783         {0x3820, 0x00},
1784         {0x3821, 0x9e},
1785         {0x3823, 0x00},
1786         {0x3824, 0x00},
1787         {0x3825, 0x00},
1788         {0x3826, 0x00},
1789         {0x3827, 0x00},
1790         {0x382a, 0x04},
1791         {0x3a04, 0x09},
1792         {0x3a05, 0xa9},
1793         {0x3a06, 0x00},
1794         {0x3a07, 0xfe},
1795         {0x3b00, 0x00},
1796         {0x3b02, 0x00},
1797         {0x3b03, 0x00},
1798         {0x3b04, 0x00},
1799         {0x3b05, 0x00},
1800         {0x3e07, 0x20},
1801         {0x4000, 0x08},
1802         {0x4001, 0x04},
1803         {0x4002, 0x45},
1804         {0x4004, 0x08},
1805         {0x4005, 0x18},
1806         {0x4006, 0x20},
1807         {0x4008, 0x24},
1808         {0x4009, 0x10},
1809         {0x400c, 0x00},
1810         {0x400d, 0x00},
1811         {0x4058, 0x00},
1812         {0x404e, 0x37},
1813         {0x404f, 0x8f},
1814         {0x4058, 0x00},
1815         {0x4101, 0xb2},
1816         {0x4303, 0x00},
1817         {0x4304, 0x08},
1818         {0x4307, 0x30},
1819         {0x4311, 0x04},
1820         {0x4315, 0x01},
1821         {0x4511, 0x05},
1822         {0x4512, 0x01},
1823         {0x4806, 0x00},
1824         {0x4816, 0x52},
1825         {0x481f, 0x30},
1826         {0x4826, 0x2c},
1827         {0x4831, 0x64},
1828         {0x4d00, 0x04},
1829         {0x4d01, 0x71},
1830         {0x4d02, 0xfd},
1831         {0x4d03, 0xf5},
1832         {0x4d04, 0x0c},
1833         {0x4d05, 0xcc},
1834         {0x4837, 0x0a},
1835         {0x5000, 0x06},
1836         {0x5001, 0x01},
1837         {0x5002, 0x00},
1838         {0x5003, 0x20},
1839         {0x5046, 0x0a},
1840         {0x5013, 0x00},
1841         {0x5046, 0x0a},
1842         {0x5780, 0x1c},
1843         {0x5786, 0x20},
1844         {0x5787, 0x10},
1845         {0x5788, 0x18},
1846         {0x578a, 0x04},
1847         {0x578b, 0x02},
1848         {0x578c, 0x02},
1849         {0x578e, 0x06},
1850         {0x578f, 0x02},
1851         {0x5790, 0x02},
1852         {0x5791, 0xff},
1853         {0x5842, 0x01},
1854         {0x5843, 0x2b},
1855         {0x5844, 0x01},
1856         {0x5845, 0x92},
1857         {0x5846, 0x01},
1858         {0x5847, 0x8f},
1859         {0x5848, 0x01},
1860         {0x5849, 0x0c},
1861         {0x5e00, 0x00},
1862         {0x5e10, 0x0c},
1863         {OV5693_TABLE_END, 0x0000}
1864 };
1865
1866 static const struct ov5693_reg ov5693_1296x972_HDR_30fps_i2c[] = {
1867         {OV5693_TABLE_RESET, 0},/* Including sw reset */
1868         {0x0103, 0x01},
1869         {0x3001, 0x0a},
1870         {0x3002, 0x80},
1871         {0x3006, 0x00},
1872         {0x3011, 0x21},
1873         {0x3012, 0x09},
1874         {0x3013, 0x10},
1875         {0x3014, 0x00},
1876         {0x3015, 0x08},
1877         {0x3016, 0xf0},
1878         {0x3017, 0xf0},
1879         {0x3018, 0xf0},
1880         {0x301b, 0xb4},
1881         {0x301d, 0x02},
1882         {0x3021, 0x00},
1883         {0x3022, 0x01},
1884         {0x3028, 0x44},
1885         {0x3098, 0x03},
1886         {0x3099, 0x1e},
1887         {0x309a, 0x02},
1888         {0x309b, 0x01},
1889         {0x309c, 0x00},
1890         {0x30a0, 0xd2},
1891         {0x30a2, 0x01},
1892         {0x30b2, 0x00},
1893         {0x30b3, 0x64},
1894         {0x30b4, 0x03},
1895         {0x30b5, 0x04},
1896         {0x30b6, 0x01},
1897         {0x3104, 0x21},
1898         {0x3106, 0x00},
1899         {0x3406, 0x01},
1900         {0x3500, 0x00},
1901         {0x3501, 0x72},
1902         {0x3502, 0x00},
1903         {0x3503, 0x07},
1904         {0x3504, 0x00},
1905         {0x3505, 0x00},
1906         {0x3506, 0x00},
1907         {0x3507, 0x01},
1908         {0x3508, 0x80},
1909         {0x3509, 0x10},
1910         {0x350a, 0x00},
1911         {0x350b, 0x40},
1912         {0x3601, 0x0a},
1913         {0x3602, 0x38},
1914         {0x3612, 0x80},
1915         {0x3620, 0x54},
1916         {0x3621, 0xc7},
1917         {0x3622, 0x0f},
1918         {0x3625, 0x10},
1919         {0x3630, 0x55},
1920         {0x3631, 0xf4},
1921         {0x3632, 0x00},
1922         {0x3633, 0x34},
1923         {0x3634, 0x02},
1924         {0x364d, 0x0d},
1925         {0x364f, 0xdd},
1926         {0x3660, 0x04},
1927         {0x3662, 0x10},
1928         {0x3663, 0xf1},
1929         {0x3665, 0x00},
1930         {0x3666, 0x20},
1931         {0x3667, 0x00},
1932         {0x366a, 0x80},
1933         {0x3680, 0xe0},
1934         {0x3681, 0x00},
1935         {0x3700, 0x42},
1936         {0x3701, 0x14},
1937         {0x3702, 0xa0},
1938         {0x3703, 0xd8},
1939         {0x3704, 0x78},
1940         {0x3705, 0x02},
1941         {0x3708, 0xe2},
1942         {0x3709, 0xc3},
1943         {0x370a, 0x00},
1944         {0x370b, 0x20},
1945         {0x370c, 0x0c},
1946         {0x370d, 0x11},
1947         {0x370e, 0x00},
1948         {0x370f, 0x40},
1949         {0x3710, 0x00},
1950         {0x371a, 0x1c},
1951         {0x371b, 0x05},
1952         {0x371c, 0x01},
1953         {0x371e, 0xa1},
1954         {0x371f, 0x0c},
1955         {0x3721, 0x00},
1956         {0x3724, 0x10},
1957         {0x3726, 0x00},
1958         {0x372a, 0x01},
1959         {0x3730, 0x10},
1960         {0x3738, 0x22},
1961         {0x3739, 0xe5},
1962         {0x373a, 0x50},
1963         {0x373b, 0x02},
1964         {0x373c, 0x41},
1965         {0x373f, 0x02},
1966         {0x3740, 0x42},
1967         {0x3741, 0x02},
1968         {0x3742, 0x18},
1969         {0x3743, 0x01},
1970         {0x3744, 0x02},
1971         {0x3747, 0x10},
1972         {0x374c, 0x04},
1973         {0x3751, 0xf0},
1974         {0x3752, 0x00},
1975         {0x3753, 0x00},
1976         {0x3754, 0xc0},
1977         {0x3755, 0x00},
1978         {0x3756, 0x1a},
1979         {0x3758, 0x00},
1980         {0x3759, 0x0f},
1981         {0x376b, 0x44},
1982         {0x375c, 0x04},
1983         {0x3774, 0x10},
1984         {0x3776, 0x00},
1985         {0x377f, 0x08},
1986         {0x3780, 0x22},
1987         {0x3781, 0x0c},
1988         {0x3784, 0x2c},
1989         {0x3785, 0x1e},
1990         {0x378f, 0xf5},
1991         {0x3791, 0xb0},
1992         {0x3795, 0x00},
1993         {0x3796, 0x64},
1994         {0x3797, 0x11},
1995         {0x3798, 0x30},
1996         {0x3799, 0x41},
1997         {0x379a, 0x07},
1998         {0x379b, 0xb0},
1999         {0x379c, 0x0c},
2000         {0x37c5, 0x00},
2001         {0x37c6, 0x00},
2002         {0x37c7, 0x00},
2003         {0x37c9, 0x00},
2004         {0x37ca, 0x00},
2005         {0x37cb, 0x00},
2006         {0x37de, 0x00},
2007         {0x37df, 0x00},
2008         {0x3800, 0x02},
2009         {0x3801, 0xa0},
2010         {0x3802, 0x01},
2011         {0x3803, 0xe8},
2012         {0x3804, 0x07},
2013         {0x3805, 0xb7},
2014         {0x3806, 0x05},
2015         {0x3807, 0xb9},
2016         {0x3808, 0x05},
2017         {0x3809, 0x10},
2018         {0x380a, 0x03},
2019         {0x380b, 0xcc},
2020         {0x380c, 0x0b},
2021         {0x380d, 0x40},
2022         {0x380e, 0x07},
2023         {0x380f, 0x3a},
2024         {0x3810, 0x00},
2025         {0x3811, 0x02},
2026         {0x3812, 0x00},
2027         {0x3813, 0x02},
2028         {0x3814, 0x11},
2029         {0x3815, 0x11},
2030         {0x3820, 0x00},
2031         {0x3821, 0x9e},
2032         {0x3823, 0x00},
2033         {0x3824, 0x00},
2034         {0x3825, 0x00},
2035         {0x3826, 0x00},
2036         {0x3827, 0x00},
2037         {0x382a, 0x04},
2038         {0x3a04, 0x09},
2039         {0x3a05, 0xa9},
2040         {0x3a06, 0x00},
2041         {0x3a07, 0xfe},
2042         {0x3b00, 0x00},
2043         {0x3b02, 0x00},
2044         {0x3b03, 0x00},
2045         {0x3b04, 0x00},
2046         {0x3b05, 0x00},
2047         {0x3e07, 0x20},
2048         {0x4000, 0x08},
2049         {0x4001, 0x04},
2050         {0x4002, 0x45},
2051         {0x4004, 0x08},
2052         {0x4005, 0x18},
2053         {0x4006, 0x20},
2054         {0x4008, 0x24},
2055         {0x4009, 0x10},
2056         {0x400c, 0x00},
2057         {0x400d, 0x00},
2058         {0x4058, 0x00},
2059         {0x404e, 0x37},
2060         {0x404f, 0x8f},
2061         {0x4058, 0x00},
2062         {0x4101, 0xb2},
2063         {0x4303, 0x00},
2064         {0x4304, 0x08},
2065         {0x4307, 0x30},
2066         {0x4311, 0x04},
2067         {0x4315, 0x01},
2068         {0x4511, 0x05},
2069         {0x4512, 0x01},
2070         {0x4806, 0x00},
2071         {0x4816, 0x52},
2072         {0x481f, 0x30},
2073         {0x4826, 0x2c},
2074         {0x4831, 0x64},
2075         {0x4d00, 0x04},
2076         {0x4d01, 0x71},
2077         {0x4d02, 0xfd},
2078         {0x4d03, 0xf5},
2079         {0x4d04, 0x0c},
2080         {0x4d05, 0xcc},
2081         {0x4837, 0x0a},
2082         {0x5000, 0x06},
2083         {0x5001, 0x01},
2084         {0x5002, 0x00},
2085         {0x5003, 0x20},
2086         {0x5046, 0x0a},
2087         {0x5013, 0x00},
2088         {0x5046, 0x0a},
2089         {0x5780, 0x1c},
2090         {0x5786, 0x20},
2091         {0x5787, 0x10},
2092         {0x5788, 0x18},
2093         {0x578a, 0x04},
2094         {0x578b, 0x02},
2095         {0x578c, 0x02},
2096         {0x578e, 0x06},
2097         {0x578f, 0x02},
2098         {0x5790, 0x02},
2099         {0x5791, 0xff},
2100         {0x5842, 0x01},
2101         {0x5843, 0x2b},
2102         {0x5844, 0x01},
2103         {0x5845, 0x92},
2104         {0x5846, 0x01},
2105         {0x5847, 0x8f},
2106         {0x5848, 0x01},
2107         {0x5849, 0x0c},
2108         {0x5e00, 0x00},
2109         {0x5e10, 0x0c},
2110         {OV5693_TABLE_END, 0x0000}
2111 };
2112
2113 static const struct ov5693_reg ov5693_1280x720_HDR_60fps_i2c[] = {
2114         {OV5693_TABLE_RESET, 0},/* Including sw reset */
2115         {0x0103, 0x01},
2116         {0x3001, 0x0a},
2117         {0x3002, 0x80},
2118         {0x3006, 0x00},
2119         {0x3011, 0x21},
2120         {0x3012, 0x09},
2121         {0x3013, 0x10},
2122         {0x3014, 0x00},
2123         {0x3015, 0x08},
2124         {0x3016, 0xf0},
2125         {0x3017, 0xf0},
2126         {0x3018, 0xf0},
2127         {0x301b, 0xb4},
2128         {0x301d, 0x02},
2129         {0x3021, 0x00},
2130         {0x3022, 0x01},
2131         {0x3028, 0x44},
2132         {0x3098, 0x03},
2133         {0x3099, 0x1e},
2134         {0x309a, 0x02},
2135         {0x309b, 0x01},
2136         {0x309c, 0x00},
2137         {0x30a0, 0xd2},
2138         {0x30a2, 0x01},
2139         {0x30b2, 0x00},
2140         {0x30b3, 0x64},
2141         {0x30b4, 0x03},
2142         {0x30b5, 0x04},
2143         {0x30b6, 0x01},
2144         {0x3104, 0x21},
2145         {0x3106, 0x00},
2146         {0x3406, 0x01},
2147         {0x3500, 0x00},
2148         {0x3501, 0x39},
2149         {0x3502, 0x00},
2150         {0x3503, 0x07},
2151         {0x3504, 0x00},
2152         {0x3505, 0x00},
2153         {0x3506, 0x00},
2154         {0x3507, 0x01},
2155         {0x3508, 0x80},
2156         {0x3509, 0x10},
2157         {0x350a, 0x00},
2158         {0x350b, 0x40},
2159         {0x3601, 0x0a},
2160         {0x3602, 0x38},
2161         {0x3612, 0x80},
2162         {0x3620, 0x54},
2163         {0x3621, 0xc7},
2164         {0x3622, 0x0f},
2165         {0x3625, 0x10},
2166         {0x3630, 0x55},
2167         {0x3631, 0xf4},
2168         {0x3632, 0x00},
2169         {0x3633, 0x34},
2170         {0x3634, 0x02},
2171         {0x364d, 0x0d},
2172         {0x364f, 0xdd},
2173         {0x3660, 0x04},
2174         {0x3662, 0x10},
2175         {0x3663, 0xf1},
2176         {0x3665, 0x00},
2177         {0x3666, 0x20},
2178         {0x3667, 0x00},
2179         {0x366a, 0x80},
2180         {0x3680, 0xe0},
2181         {0x3681, 0x00},
2182         {0x3700, 0x42},
2183         {0x3701, 0x14},
2184         {0x3702, 0xa0},
2185         {0x3703, 0xd8},
2186         {0x3704, 0x78},
2187         {0x3705, 0x02},
2188         {0x3708, 0xe2},
2189         {0x3709, 0xc3},
2190         {0x370a, 0x00},
2191         {0x370b, 0x20},
2192         {0x370c, 0x0c},
2193         {0x370d, 0x11},
2194         {0x370e, 0x00},
2195         {0x370f, 0x40},
2196         {0x3710, 0x00},
2197         {0x371a, 0x1c},
2198         {0x371b, 0x05},
2199         {0x371c, 0x01},
2200         {0x371e, 0xa1},
2201         {0x371f, 0x0c},
2202         {0x3721, 0x00},
2203         {0x3724, 0x10},
2204         {0x3726, 0x00},
2205         {0x372a, 0x01},
2206         {0x3730, 0x10},
2207         {0x3738, 0x22},
2208         {0x3739, 0xe5},
2209         {0x373a, 0x50},
2210         {0x373b, 0x02},
2211         {0x373c, 0x41},
2212         {0x373f, 0x02},
2213         {0x3740, 0x42},
2214         {0x3741, 0x02},
2215         {0x3742, 0x18},
2216         {0x3743, 0x01},
2217         {0x3744, 0x02},
2218         {0x3747, 0x10},
2219         {0x374c, 0x04},
2220         {0x3751, 0xf0},
2221         {0x3752, 0x00},
2222         {0x3753, 0x00},
2223         {0x3754, 0xc0},
2224         {0x3755, 0x00},
2225         {0x3756, 0x1a},
2226         {0x3758, 0x00},
2227         {0x3759, 0x0f},
2228         {0x376b, 0x44},
2229         {0x375c, 0x04},
2230         {0x3774, 0x10},
2231         {0x3776, 0x00},
2232         {0x377f, 0x08},
2233         {0x3780, 0x22},
2234         {0x3781, 0x0c},
2235         {0x3784, 0x2c},
2236         {0x3785, 0x1e},
2237         {0x378f, 0xf5},
2238         {0x3791, 0xb0},
2239         {0x3795, 0x00},
2240         {0x3796, 0x64},
2241         {0x3797, 0x11},
2242         {0x3798, 0x30},
2243         {0x3799, 0x41},
2244         {0x379a, 0x07},
2245         {0x379b, 0xb0},
2246         {0x379c, 0x0c},
2247         {0x37c5, 0x00},
2248         {0x37c6, 0x00},
2249         {0x37c7, 0x00},
2250         {0x37c9, 0x00},
2251         {0x37ca, 0x00},
2252         {0x37cb, 0x00},
2253         {0x37de, 0x00},
2254         {0x37df, 0x00},
2255         {0x3800, 0x02},
2256         {0x3801, 0xa8},
2257         {0x3802, 0x02},
2258         {0x3803, 0x68},
2259         {0x3804, 0x07},
2260         {0x3805, 0xb7},
2261         {0x3806, 0x05},
2262         {0x3807, 0x3b},
2263         {0x3808, 0x05},
2264         {0x3809, 0x00},
2265         {0x380a, 0x02},
2266         {0x380b, 0xd0},
2267         {0x380c, 0x0b},
2268         {0x380d, 0x40},
2269         {0x380e, 0x03},
2270         {0x380f, 0x9e},
2271         {0x3810, 0x00},
2272         {0x3811, 0x02},
2273         {0x3812, 0x00},
2274         {0x3813, 0x02},
2275         {0x3814, 0x11},
2276         {0x3815, 0x11},
2277         {0x3820, 0x00},
2278         {0x3821, 0x9e},
2279         {0x3823, 0x00},
2280         {0x3824, 0x00},
2281         {0x3825, 0x00},
2282         {0x3826, 0x00},
2283         {0x3827, 0x00},
2284         {0x382a, 0x04},
2285         {0x3a04, 0x09},
2286         {0x3a05, 0xa9},
2287         {0x3a06, 0x00},
2288         {0x3a07, 0xfe},
2289         {0x3b00, 0x00},
2290         {0x3b02, 0x00},
2291         {0x3b03, 0x00},
2292         {0x3b04, 0x00},
2293         {0x3b05, 0x00},
2294         {0x3e07, 0x20},
2295         {0x4000, 0x08},
2296         {0x4001, 0x04},
2297         {0x4002, 0x45},
2298         {0x4004, 0x08},
2299         {0x4005, 0x18},
2300         {0x4006, 0x20},
2301         {0x4008, 0x24},
2302         {0x4009, 0x10},
2303         {0x400c, 0x00},
2304         {0x400d, 0x00},
2305         {0x4058, 0x00},
2306         {0x404e, 0x37},
2307         {0x404f, 0x8f},
2308         {0x4058, 0x00},
2309         {0x4101, 0xb2},
2310         {0x4303, 0x00},
2311         {0x4304, 0x08},
2312         {0x4307, 0x30},
2313         {0x4311, 0x04},
2314         {0x4315, 0x01},
2315         {0x4511, 0x05},
2316         {0x4512, 0x01},
2317         {0x4806, 0x00},
2318         {0x4816, 0x52},
2319         {0x481f, 0x30},
2320         {0x4826, 0x2c},
2321         {0x4831, 0x64},
2322         {0x4d00, 0x04},
2323         {0x4d01, 0x71},
2324         {0x4d02, 0xfd},
2325         {0x4d03, 0xf5},
2326         {0x4d04, 0x0c},
2327         {0x4d05, 0xcc},
2328         {0x4837, 0x0a},
2329         {0x5000, 0x06},
2330         {0x5001, 0x01},
2331         {0x5002, 0x00},
2332         {0x5003, 0x20},
2333         {0x5046, 0x0a},
2334         {0x5013, 0x00},
2335         {0x5046, 0x0a},
2336         {0x5780, 0x1c},
2337         {0x5786, 0x20},
2338         {0x5787, 0x10},
2339         {0x5788, 0x18},
2340         {0x578a, 0x04},
2341         {0x578b, 0x02},
2342         {0x578c, 0x02},
2343         {0x578e, 0x06},
2344         {0x578f, 0x02},
2345         {0x5790, 0x02},
2346         {0x5791, 0xff},
2347         {0x5842, 0x01},
2348         {0x5843, 0x2b},
2349         {0x5844, 0x01},
2350         {0x5845, 0x92},
2351         {0x5846, 0x01},
2352         {0x5847, 0x8f},
2353         {0x5848, 0x01},
2354         {0x5849, 0x0c},
2355         {0x5e00, 0x00},
2356         {0x5e10, 0x0c},
2357         {OV5693_TABLE_END, 0x0000}
2358 };
2359
2360 enum {
2361         OV5693_MODE_2592x1944 = 0,
2362         OV5693_MODE_1920x1080,
2363         OV5693_MODE_1296x972,
2364         OV5693_MODE_1280x720_120FPS,
2365         OV5693_MODE_2560x1440_HDR,
2366         OV5693_MODE_2592x1944_HDR,
2367         OV5693_MODE_1920x1080_HDR,
2368         OV5693_MODE_1296x972_HDR,
2369         OV5693_MODE_1280x720_HDR_60FPS,
2370 };
2371
2372 static const struct ov5693_reg *mode_table[] = {
2373         [OV5693_MODE_2592x1944]         = ov5693_2592x1944_i2c,
2374         [OV5693_MODE_1920x1080]         = ov5693_1920x1080_i2c,
2375         [OV5693_MODE_1296x972]          = ov5693_1296x972_i2c,
2376         [OV5693_MODE_1280x720_120FPS]   = ov5693_1280x720_120fps_i2c,
2377         [OV5693_MODE_2592x1944_HDR]     = ov5693_2592x1944_HDR_24fps_i2c,
2378         [OV5693_MODE_2560x1440_HDR]     = ov5693_2560x1440_HDR_24fps_i2c,
2379         [OV5693_MODE_1920x1080_HDR]     = ov5693_1920x1080_HDR_30fps_i2c,
2380         [OV5693_MODE_1296x972_HDR]      = ov5693_1296x972_HDR_30fps_i2c,
2381         [OV5693_MODE_1280x720_HDR_60FPS] = ov5693_1280x720_HDR_60fps_i2c,
2382 };
2383
2384 static int ov5693_i2c_rd8(struct ov5693_info *info, u16 reg, u8 *val)
2385 {
2386         unsigned int data;
2387         int ret = regmap_read(info->regmap, reg, &data);
2388         *val = data;
2389
2390         return ret;
2391 }
2392
2393 static int ov5693_i2c_wr_table(struct ov5693_info *info,
2394                                 const struct ov5693_reg table[])
2395 {
2396         int err;
2397         int buf_count = 0;
2398         const struct ov5693_reg *next, *n_next;
2399         u16 i2c_reg = 0;
2400         u8 i2c_buf[OV5693_SIZEOF_I2C_BUF];
2401
2402         u8 *b_ptr = i2c_buf;
2403
2404         for (next = table; next->addr != OV5693_TABLE_END; next++) {
2405                 if (next->addr == OV5693_TABLE_WAIT_MS) {
2406                         msleep(next->val);
2407                         continue;
2408                 } else if (next->addr == OV5693_TABLE_RESET) {
2409                         err = regmap_write(info->regmap, 0x0100, 0x00);
2410                         if (err)
2411                                 return err;
2412                         continue;
2413                 }
2414
2415                 if (buf_count == 0) {
2416                         b_ptr = i2c_buf;
2417                         i2c_reg = next->addr;
2418                 }
2419
2420                 *b_ptr++ = next->val;
2421                 buf_count++;
2422                 n_next = next + 1;
2423                 if (n_next->addr == next->addr + 1 &&
2424                         n_next->addr != OV5693_TABLE_WAIT_MS &&
2425                         buf_count < OV5693_SIZEOF_I2C_BUF &&
2426                         n_next->addr != OV5693_TABLE_RESET &&
2427                         n_next->addr != OV5693_TABLE_END)
2428                         continue;
2429
2430                 err = regmap_bulk_write(info->regmap, i2c_reg,
2431                                         i2c_buf, buf_count);
2432                 if (err)
2433                         return err;
2434
2435                 buf_count = 0;
2436         }
2437
2438         return 0;
2439 }
2440
2441
2442 static inline int ov5693_frame_length_reg(struct ov5693_reg *regs,
2443                                         u32 frame_length)
2444 {
2445         regs->addr = 0x380E;
2446         regs->val = (frame_length >> 8) & 0xff;
2447         (regs + 1)->addr = 0x380F;
2448         (regs + 1)->val = (frame_length) & 0xff;
2449
2450         return 2;
2451 }
2452
2453 static inline int ov5693_coarse_time_reg(struct ov5693_reg *regs,
2454                                         u32 coarse_time, u32 coarse_time_short)
2455 {
2456         int ret = 0;
2457
2458         regs->addr = 0x3500;
2459         regs->val = (coarse_time >> 12) & 0xff;
2460         (regs + 1)->addr = 0x3501;
2461         (regs + 1)->val = (coarse_time >> 4) & 0xff;
2462         (regs + 2)->addr = 0x3502;
2463         (regs + 2)->val = (coarse_time & 0xf) << 4;
2464
2465         ret += 3;
2466
2467         if (coarse_time_short != OV5693_INVALID_COARSE_TIME) {
2468                 (regs + 3)->addr = 0x3506;
2469                 (regs + 3)->val = (coarse_time_short >> 12) & 0xff;
2470                 (regs + 4)->addr = 0x3507;
2471                 (regs + 4)->val = (coarse_time_short >> 4) & 0xff;
2472                 (regs + 5)->addr = 0x3508;
2473                 (regs + 5)->val = (coarse_time_short & 0xf) << 4;
2474
2475                 ret += 3;
2476         }
2477
2478         return ret;
2479 }
2480
2481 #define OV5693_ENTER_GROUP_HOLD(group_hold) \
2482         do {    \
2483                 if (group_hold) {   \
2484                         reg_list[offset].addr = 0x3208; \
2485                         reg_list[offset].val = 0x01;\
2486                         offset++;  \
2487                 }   \
2488         } while (0)
2489
2490 #define OV5693_LEAVE_GROUP_HOLD(group_hold) \
2491         do {    \
2492                 if (group_hold) {   \
2493                         reg_list[offset].addr = 0x3208; \
2494                         reg_list[offset].val = 0x11;\
2495                         offset++;  \
2496                         reg_list[offset].addr = 0x3208; \
2497                         reg_list[offset].val = 0x61;\
2498                         offset++;  \
2499                 } \
2500         } while (0)
2501
2502 static int ov5693_set_frame_length(struct ov5693_info *info,
2503                                    u32 frame_length, bool group_hold)
2504 {
2505         struct ov5693_reg reg_list[9];
2506         int err = 0;
2507         int offset = 0;
2508
2509         OV5693_ENTER_GROUP_HOLD(group_hold);
2510         offset += ov5693_frame_length_reg(reg_list + offset, frame_length);
2511         OV5693_LEAVE_GROUP_HOLD(group_hold);
2512
2513         reg_list[offset].addr = OV5693_TABLE_END;
2514         offset++;
2515
2516         err = ov5693_i2c_wr_table(info, reg_list);
2517
2518         return err;
2519 }
2520
2521 static int ov5693_set_coarse_time(struct ov5693_info *info,
2522                                   u32 coarse_time, u32 coarse_time_short,
2523                                   bool group_hold)
2524 {
2525         struct ov5693_reg reg_list[16];
2526         int err = 0;
2527         int offset = 0;
2528
2529         OV5693_ENTER_GROUP_HOLD(group_hold);
2530         offset += ov5693_coarse_time_reg(reg_list + offset,
2531                                                 coarse_time,
2532                                                 coarse_time_short);
2533         OV5693_LEAVE_GROUP_HOLD(group_hold);
2534
2535         reg_list[offset].addr = OV5693_TABLE_END;
2536         offset++;
2537
2538         err = ov5693_i2c_wr_table(info, reg_list);
2539
2540         return err;
2541 }
2542
2543 static inline int ov5693_gain_reg(struct ov5693_reg *regs, u32 gain)
2544 {
2545         (regs)->addr = 0x350A;
2546         (regs)->val = gain >> 8;
2547
2548         (regs + 1)->addr = 0x350B;
2549         (regs + 1)->val = gain & 0x00FF;
2550
2551         return 2;
2552 }
2553
2554 static int ov5693_bin_wr(struct ov5693_info *info, u8 enable)
2555 {
2556         int err = 0;
2557
2558         if (enable == info->bin_en)
2559                 return 0;
2560
2561         if (!err)
2562                 info->bin_en = enable;
2563         dev_dbg(&info->i2c_client->dev, "%s bin_en=%x err=%d\n",
2564                 __func__, info->bin_en, err);
2565         return err;
2566 }
2567
2568 static int ov5693_exposure_wr(struct ov5693_info *info,
2569                                 struct ov5693_mode *mode)
2570 {
2571         struct ov5693_reg reg_list[16];
2572         int err = 0;
2573         int offset = 0;
2574         bool group_hold = true; /* To use GROUP_HOLD macros */
2575
2576         OV5693_ENTER_GROUP_HOLD(group_hold);
2577         offset += ov5693_coarse_time_reg(reg_list + offset,
2578                                                 mode->coarse_time,
2579                                                 mode->coarse_time_short);
2580         offset += ov5693_gain_reg(reg_list + offset, mode->gain);
2581         OV5693_LEAVE_GROUP_HOLD(group_hold);
2582
2583         reg_list[offset].addr = OV5693_TABLE_END;
2584         err = ov5693_i2c_wr_table(info, reg_list);
2585
2586         return err;
2587 }
2588
2589
2590 static int ov5693_set_gain(struct ov5693_info *info, u32 gain, bool group_hold)
2591 {
2592         struct ov5693_reg reg_list[9];
2593         int err = 0;
2594         int offset = 0;
2595
2596         OV5693_ENTER_GROUP_HOLD(group_hold);
2597         offset += ov5693_gain_reg(reg_list + offset, gain);
2598         OV5693_LEAVE_GROUP_HOLD(group_hold);
2599
2600         reg_list[offset].addr = OV5693_TABLE_END;
2601         offset++;
2602
2603         err = ov5693_i2c_wr_table(info, reg_list);
2604
2605         return err;
2606 }
2607
2608 static int ov5693_awb_wr(struct ov5693_info *info)
2609 {
2610         struct ov5693_reg reg_list[10];
2611         int rg, bg, rg_typical, bg_typical;
2612         int R_gain, G_gain, B_gain, G_gain_R, G_gain_B;
2613         int offset;
2614         int err;
2615
2616         if (info->cal.loaded == 0)
2617                 return 0;
2618
2619         /* update AWB calibration data to register lists */
2620         rg = info->cal.rg_ratio;
2621         bg = info->cal.bg_ratio;
2622         rg_typical = info->cal.rg_ratio_typical;
2623         bg_typical = info->cal.bg_ratio_typical;
2624
2625         if ((rg == 0) || (bg == 0) || (rg_typical == 0) || (bg_typical == 0))
2626                 return 0;
2627
2628         if (bg < bg_typical) {
2629                 if (rg < rg_typical) {
2630                         G_gain = 0x400;
2631                         B_gain = 0x400 * bg_typical / bg;
2632                         R_gain = 0x400 * rg_typical / rg;
2633                 } else {
2634                         R_gain = 0x400;
2635                         G_gain = 0x400 * rg / rg_typical;
2636                         B_gain = G_gain * bg_typical / bg;
2637                 }
2638         } else {
2639                 if (rg < rg_typical) {
2640                         B_gain = 0x400;
2641                         G_gain = 0x400 * bg / bg_typical;
2642                         R_gain = G_gain * rg_typical / rg;
2643                 } else {
2644                         G_gain_B = 0x400 * bg / bg_typical;
2645                         G_gain_R = 0x400 * rg / rg_typical;
2646                         if (G_gain_B > G_gain_R) {
2647                                 B_gain = 0x400;
2648                                 G_gain = G_gain_B;
2649                                 R_gain = G_gain * rg_typical / rg;
2650                         } else {
2651                                 R_gain = 0x400;
2652                                 G_gain = G_gain_R;
2653                                 B_gain = G_gain * bg_typical / bg;
2654                         }
2655                 }
2656         }
2657
2658         offset = 0;
2659         if (R_gain > 0x400) {
2660                 reg_list[offset].addr = 0x3400;
2661                 reg_list[offset].val  = R_gain >> 8;
2662                 offset++;
2663                 reg_list[offset].addr = 0x3401;
2664                 reg_list[offset].val  = R_gain & 0x00ff;
2665                 offset++;
2666         }
2667         if (G_gain > 0x400) {
2668                 reg_list[offset].addr = 0x3402;
2669                 reg_list[offset].val  = G_gain >> 8;
2670                 offset++;
2671                 reg_list[offset].addr = 0x3403;
2672                 reg_list[offset].val  = G_gain & 0x00ff;
2673                 offset++;
2674         }
2675         if (B_gain > 0x400) {
2676                 reg_list[offset].addr = 0x3404;
2677                 reg_list[offset].val  = B_gain >> 8;
2678                 offset++;
2679                 reg_list[offset].addr = 0x3405;
2680                 reg_list[offset].val  = B_gain & 0x00ff;
2681                 offset++;
2682         }
2683         reg_list[offset].addr = OV5693_TABLE_END;
2684         offset++;
2685
2686         err = ov5693_i2c_wr_table(info, reg_list);
2687
2688         return err;
2689 }
2690
2691 static int ov5693_lsc_wr(struct ov5693_info *info)
2692 {
2693         struct ov5693_reg reg_list[64];
2694         int offset;
2695         int err;
2696         int i;
2697
2698         if (info->cal.loaded == 0)
2699                 return 0;
2700
2701         offset = 0;
2702         reg_list[offset].addr = 0x5000;
2703         reg_list[offset].val  = 0x86;
2704         offset++;
2705         for (i = 0; i < 62; i++) {
2706                 reg_list[offset].addr = 0x5800 + i;
2707                 reg_list[offset].val  = info->cal.lenc[i];
2708                 offset++;
2709         }
2710         reg_list[offset].addr = OV5693_TABLE_END;
2711         offset++;
2712
2713         err = ov5693_i2c_wr_table(info, reg_list);
2714
2715         return err;
2716 }
2717
2718 static int ov5693_set_group_hold(struct ov5693_info *info,
2719                                 struct ov5693_ae *ae)
2720 {
2721         int err = 0;
2722         struct ov5693_reg reg_list[16];
2723         int offset = 0;
2724         bool group_hold = true; /* To use GROUP_HOLD macros */
2725
2726         OV5693_ENTER_GROUP_HOLD(group_hold);
2727         if (ae->gain_enable)
2728                 offset += ov5693_gain_reg(reg_list + offset,
2729                                           ae->gain);
2730         if (ae->frame_length_enable)
2731                 offset += ov5693_frame_length_reg(reg_list + offset,
2732                                                   ae->frame_length);
2733         if (ae->coarse_time_enable)
2734                 offset += ov5693_coarse_time_reg(reg_list + offset,
2735                         ae->coarse_time, ae->coarse_time_short);
2736         OV5693_LEAVE_GROUP_HOLD(group_hold);
2737
2738         reg_list[offset].addr = OV5693_TABLE_END;
2739         err |= ov5693_i2c_wr_table(info, reg_list);
2740
2741         return err;
2742 }
2743
2744 static int ov5693_gpio_rd(struct ov5693_info *info,
2745                         enum ov5693_gpio_type type)
2746 {
2747         int val = -EINVAL;
2748
2749         if (info->gpio[type].gpio) {
2750                 val = gpio_get_value_cansleep(info->gpio[type].gpio);
2751                 dev_dbg(&info->i2c_client->dev, "%s %u %d\n", __func__,
2752                        info->gpio[type].gpio, val);
2753                 if (!info->gpio[type].active_high)
2754                         val = !val;
2755                 val &= 1;
2756         }
2757         return val; /* return read value or error */
2758 }
2759
2760 static int ov5693_gpio_wr(struct ov5693_info *info,
2761                         enum ov5693_gpio_type type,
2762                         int val) /* val: 0=deassert, 1=assert */
2763 {
2764         int err = -EINVAL;
2765
2766         if (info->gpio[type].gpio) {
2767                 if (!info->gpio[type].active_high)
2768                         val = !val;
2769                 val &= 1;
2770                 err = val;
2771                 gpio_set_value_cansleep(info->gpio[type].gpio, val);
2772                 dev_dbg(&info->i2c_client->dev, "%s %u %d\n", __func__,
2773                        info->gpio[type].gpio, val);
2774         }
2775         return err; /* return value written or error */
2776 }
2777
2778 static void ov5693_gpio_pwrdn(struct ov5693_info *info, int val)
2779 {
2780         int prev_val;
2781
2782         prev_val = ov5693_gpio_rd(info, OV5693_GPIO_TYPE_PWRDN);
2783         if ((prev_val < 0) || (val == prev_val))
2784                 return;
2785
2786         ov5693_gpio_wr(info, OV5693_GPIO_TYPE_PWRDN, val);
2787         if (!val && prev_val)
2788                 /* if transition from assert to deassert then delay for I2C */
2789                 msleep(50);
2790 }
2791
2792 static void ov5693_gpio_exit(struct ov5693_info *info)
2793 {
2794         unsigned int i;
2795
2796         for (i = 0; i < ARRAY_SIZE(ov5693_gpio); i++) {
2797                 if (info->gpio[i].gpio && info->gpio[i].own)
2798                         gpio_free(info->gpio[i].gpio);
2799         }
2800 }
2801
2802 static void ov5693_gpio_init(struct ov5693_info *info)
2803 {
2804         char label[32];
2805         unsigned long flags;
2806         unsigned type;
2807         unsigned i;
2808         unsigned j;
2809         int err;
2810
2811         if (!info->pdata->gpio_count || !info->pdata->gpio)
2812                 return;
2813
2814         for (i = 0; i < ARRAY_SIZE(ov5693_gpio); i++) {
2815                 type = ov5693_gpio[i].gpio_type;
2816                 for (j = 0; j < info->pdata->gpio_count; j++) {
2817                         if (type == info->pdata->gpio[j].gpio_type)
2818                                 break;
2819                 }
2820                 if (j == info->pdata->gpio_count)
2821                         continue;
2822
2823                 info->gpio[type].gpio = info->pdata->gpio[j].gpio;
2824                 if (ov5693_gpio[i].use_flags) {
2825                         flags = ov5693_gpio[i].flags;
2826                         info->gpio[type].active_high =
2827                                                 ov5693_gpio[i].active_high;
2828                 } else {
2829                         info->gpio[type].active_high =
2830                                         info->pdata->gpio[j].active_high;
2831                         if (info->gpio[type].active_high)
2832                                 flags = GPIOF_OUT_INIT_LOW;
2833                         else
2834                                 flags = GPIOF_OUT_INIT_HIGH;
2835                 }
2836                 if (!info->pdata->gpio[j].init_en)
2837                         continue;
2838
2839                 snprintf(label, sizeof(label), "ov5693_%u_%s",
2840                          info->pdata->num, ov5693_gpio[i].label);
2841                 err = gpio_request_one(info->gpio[type].gpio, flags, label);
2842                 if (err) {
2843                         dev_err(&info->i2c_client->dev,
2844                                 "%s ERR %s %u\n", __func__, label,
2845                                 info->gpio[type].gpio);
2846                 } else {
2847                         info->gpio[type].own = true;
2848                         dev_dbg(&info->i2c_client->dev,
2849                                 "%s %s %u\n", __func__, label,
2850                                 info->gpio[type].gpio);
2851                 }
2852         }
2853 }
2854
2855 static int ov5693_power_off(struct ov5693_info *info)
2856 {
2857         struct ov5693_power_rail *pw = &info->regulators;
2858         int err;
2859
2860         if (false == info->power_on)
2861                 return 0;
2862
2863         if (info->pdata && info->pdata->power_off) {
2864                 err = info->pdata->power_off(pw);
2865                 if (0 > err)
2866                         return err;
2867                 info->power_on = false;
2868         } else {
2869                 dev_err(&info->i2c_client->dev,
2870                         "%s ERR: has no power_off function\n", __func__);
2871                 err = -EINVAL;
2872         }
2873         return err;
2874 }
2875
2876 static int ov5693_power_on(struct ov5693_info *info)
2877 {
2878         struct ov5693_power_rail *pw = &info->regulators;
2879         int err;
2880
2881         if (true == info->power_on)
2882                 return 0;
2883
2884         if (info->pdata && info->pdata->power_on) {
2885                 err = info->pdata->power_on(pw);
2886                 if (0 > err)
2887                         return err;
2888                 info->power_on = true;
2889         } else {
2890                 dev_err(&info->i2c_client->dev,
2891                         "%s ERR: has no power_on function\n", __func__);
2892                 err = -EINVAL;
2893         }
2894         return err;
2895 }
2896
2897 static int ov5693_pm_wr(struct ov5693_info *info, int pwr)
2898 {
2899         int err = 0;
2900
2901         if ((info->pdata->cfg & (NVC_CFG_OFF2STDBY | NVC_CFG_BOOT_INIT)) &&
2902                         (pwr == NVC_PWR_OFF ||
2903                          pwr == NVC_PWR_STDBY_OFF))
2904                 pwr = NVC_PWR_STDBY;
2905         if (pwr == info->pwr_dev)
2906                 return 0;
2907
2908         switch (pwr) {
2909         case NVC_PWR_OFF_FORCE:
2910         case NVC_PWR_OFF:
2911         case NVC_PWR_STDBY_OFF:
2912                 err = ov5693_power_off(info);
2913                 info->mode_valid = false;
2914                 info->bin_en = 0;
2915                 break;
2916
2917         case NVC_PWR_STDBY:
2918                 err = ov5693_power_on(info);
2919                 ov5693_gpio_pwrdn(info, 1);
2920                 break;
2921
2922         case NVC_PWR_COMM:
2923         case NVC_PWR_ON:
2924                 err = ov5693_power_on(info);
2925                 break;
2926
2927         default:
2928                 err = -EINVAL;
2929                 break;
2930         }
2931
2932         if (err < 0) {
2933                 dev_err(&info->i2c_client->dev, "%s err %d\n", __func__, err);
2934                 pwr = NVC_PWR_ERR;
2935         }
2936         info->pwr_dev = pwr;
2937         dev_dbg(&info->i2c_client->dev, "%s pwr_dev=%d\n",
2938                 __func__, info->pwr_dev);
2939         if (err > 0)
2940                 return 0;
2941
2942         return err;
2943 }
2944
2945 static int ov5693_pm_dev_wr(struct ov5693_info *info, int pwr)
2946 {
2947         if (info->mode_enable)
2948                 pwr = NVC_PWR_ON;
2949         if (pwr < info->pwr_api)
2950                 pwr = info->pwr_api;
2951         return ov5693_pm_wr(info, pwr);
2952 }
2953
2954 static void ov5693_pm_exit(struct ov5693_info *info)
2955 {
2956         ov5693_pm_wr(info, NVC_PWR_OFF_FORCE);
2957
2958         ov5693_gpio_exit(info);
2959 }
2960
2961 static void ov5693_regulator_get(struct ov5693_info *info,
2962                                  struct regulator **vreg,
2963                                  const char vreg_name[])
2964 {
2965         struct regulator *reg = NULL;
2966         int err = 0;
2967
2968         reg = devm_regulator_get(&info->i2c_client->dev, vreg_name);
2969         if (IS_ERR(reg)) {
2970                 dev_err(&info->i2c_client->dev, "%s %s ERR: %d\n",
2971                         __func__, vreg_name, (int)reg);
2972                 err = PTR_ERR(reg);
2973                 reg = NULL;
2974         } else {
2975                 dev_dbg(&info->i2c_client->dev, "%s: %s\n",
2976                         __func__, vreg_name);
2977         }
2978
2979         *vreg = reg;
2980 }
2981
2982 static void ov5693_pm_init(struct ov5693_info *info)
2983 {
2984         struct ov5693_power_rail *pw = &info->regulators;
2985
2986         ov5693_gpio_init(info);
2987
2988         ov5693_regulator_get(info, &pw->avdd, "avdd");
2989
2990         ov5693_regulator_get(info, &pw->dovdd, "dovdd");
2991         info->power_on = false;
2992 }
2993
2994 static int ov5693_mode_able(struct ov5693_info *info, bool mode_enable)
2995 {
2996         u8 val;
2997         int err;
2998
2999         if (mode_enable)
3000                 val = 0x01;
3001         else
3002                 val = 0x00;
3003         err = regmap_write(info->regmap, 0x0100, val);
3004         if (!err) {
3005                 info->mode_enable = mode_enable;
3006                 dev_dbg(&info->i2c_client->dev, "%s streaming=%x\n",
3007                         __func__, info->mode_enable);
3008                 if (!mode_enable)
3009                         ov5693_pm_dev_wr(info, NVC_PWR_STDBY);
3010         }
3011         return err;
3012 }
3013
3014 static int ov5693_mode_wr_full(struct ov5693_info *info, u32 mode_index)
3015 {
3016         int err;
3017
3018         ov5693_pm_dev_wr(info, NVC_PWR_ON);
3019         ov5693_bin_wr(info, 0);
3020         err = ov5693_i2c_wr_table(info, mode_table[mode_index]);
3021         if (!err) {
3022                 dev_dbg(&info->i2c_client->dev,
3023                         "init done(mode=%d)!!!\n", mode_index);
3024                 info->mode_index = mode_index;
3025                 info->mode_valid = true;
3026         } else {
3027                 dev_dbg(&info->i2c_client->dev,
3028                         "init error(mode=%d)!!!\n", mode_index);
3029                 info->mode_valid = false;
3030         }
3031         return err;
3032 }
3033
3034 static int ov5693_set_mode(struct ov5693_info *info,
3035                 struct ov5693_mode *mode)
3036 {
3037         u32 mode_index = 0;
3038         int err = 0;
3039
3040         if (!mode->res_x && !mode->res_y) {
3041                 if (mode->frame_length || mode->coarse_time || mode->gain) {
3042                         /* write exposure only */
3043                         err = ov5693_exposure_wr(info, mode);
3044                         return err;
3045                 } else {
3046                         /* turn off streaming */
3047                         err = ov5693_mode_able(info, false);
3048                         return err;
3049                 }
3050         }
3051
3052         if (mode->hdr_en == true) {
3053                 if (mode->res_x == 2592 && mode->res_y == 1944)
3054                         mode_index = OV5693_MODE_2592x1944_HDR;
3055                 else if (mode->res_x == 2560 && mode->res_y == 1440)
3056                         mode_index = OV5693_MODE_2560x1440_HDR;
3057                 else if (mode->res_x == 1296 && mode->res_y == 972)
3058                         mode_index = OV5693_MODE_1296x972_HDR;
3059                 else if (mode->res_x == 1920 && mode->res_y == 1080)
3060                         mode_index = OV5693_MODE_1920x1080_HDR;
3061                 else if (mode->res_x == 1280 && mode->res_y == 720)
3062                         mode_index = OV5693_MODE_1280x720_HDR_60FPS;
3063         } else {
3064                 if (mode->res_x == 2592 && mode->res_y == 1944)
3065                         mode_index = OV5693_MODE_2592x1944;
3066                 else if (mode->res_x == 1296 && mode->res_y == 972)
3067                         mode_index = OV5693_MODE_1296x972;
3068                 else if (mode->res_x == 1920 && mode->res_y == 1080)
3069                         mode_index = OV5693_MODE_1920x1080;
3070                 else if (mode->res_x == 1280 && mode->res_y == 720)
3071                         mode_index = OV5693_MODE_1280x720_120FPS;
3072         }
3073
3074         if (!info->mode_valid || (info->mode_index != mode_index))
3075                 err = ov5693_mode_wr_full(info, mode_index);
3076         else
3077                 dev_dbg(&info->i2c_client->dev, "%s short mode\n", __func__);
3078         dev_dbg(&info->i2c_client->dev, "%s: mode #: %d\n",
3079                 __func__, mode_index);
3080         dev_dbg(&info->i2c_client->dev, "%s: AE: %d, %d, %d\n",
3081                 __func__, mode->frame_length,
3082                 mode->coarse_time, mode->gain);
3083         err |= ov5693_exposure_wr(info, mode);
3084         if (err < 0) {
3085                 info->mode_valid = false;
3086                 dev_err(&info->i2c_client->dev,
3087                         "%s set_mode error\n", __func__);
3088                 goto ov5693_mode_wr_err;
3089         }
3090         err = ov5693_awb_wr(info);
3091         if (err < 0) {
3092                 info->mode_valid = false;
3093                 dev_err(&info->i2c_client->dev,
3094                         "%s:%d awb cal write error\n", __func__, __LINE__);
3095                 goto ov5693_mode_wr_err;
3096         }
3097         err = ov5693_lsc_wr(info);
3098         if (err < 0) {
3099                 info->mode_valid = false;
3100                 dev_err(&info->i2c_client->dev,
3101                         "%s:%d lsc cal write error\n", __func__, __LINE__);
3102                 goto ov5693_mode_wr_err;
3103         }
3104         err = regmap_write(info->regmap, 0x0100, 0x01);
3105         if (err) {
3106                 info->mode_valid = false;
3107                 dev_err(&info->i2c_client->dev,
3108                         "%s:%d stream on failed\n", __func__, __LINE__);
3109                 goto ov5693_mode_wr_err;
3110         }
3111
3112         return 0;
3113
3114 ov5693_mode_wr_err:
3115         if (!info->mode_enable)
3116                 ov5693_pm_dev_wr(info, NVC_PWR_OFF);
3117         return err;
3118 }
3119
3120 static int ov5693_get_fuse_id(struct ov5693_info *info)
3121 {
3122         ov5693_i2c_rd8(info, 0x300A, &info->fuseid.id[0]);
3123         ov5693_i2c_rd8(info, 0x300B, &info->fuseid.id[1]);
3124         info->fuseid.size = 2;
3125         dev_dbg(&info->i2c_client->dev, "ov5693 fuse_id: %x,%x\n",
3126                 info->fuseid.id[0], info->fuseid.id[1]);
3127         return 0;
3128 }
3129
3130 static int ov5693_read_otp_bank(struct ov5693_info *info,
3131                                 struct ov5693_otp_bank *bank)
3132 {
3133         int err;
3134
3135         err = regmap_write(info->regmap, 0x0100, 0x01);
3136         if (err != 0) {
3137                 dev_err(&info->i2c_client->dev,
3138                         "%s ERR %d: cannot write stream mode\n", __func__,
3139                         err);
3140                 return err;
3141         }
3142         err = regmap_write(info->regmap, 0x3d84, 0xc0 | bank->id);
3143         if (err != 0) {
3144                 dev_err(&info->i2c_client->dev,
3145                         "%s ERR %d: cannot write bank index %d\n", __func__,
3146                         err, bank->id);
3147                 return err;
3148         }
3149         err = regmap_write(info->regmap, 0x3d81, 0x01);
3150         if (err != 0) {
3151                 dev_err(&info->i2c_client->dev,
3152                         "%s ERR %d: cannot load OTP data\n", __func__, err);
3153                 return err;
3154         }
3155         usleep_range(1000, 11000);
3156         err = regmap_bulk_read(info->regmap, 0x3d00, bank->buf, 16);
3157         if (err != 0) {
3158                 dev_err(&info->i2c_client->dev,
3159                         "%s ERR %d: cannot read OTP buffer\n", __func__,
3160                         err);
3161                 return err;
3162         }
3163
3164         return err;
3165 }
3166
3167 static long ov5693_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3168 {
3169         struct ov5693_info *info = file->private_data;
3170         int err;
3171
3172         switch (cmd) {
3173         case OV5693_IOCTL_SET_MODE:
3174         {
3175                 struct ov5693_mode mode;
3176                 if (copy_from_user(&mode,
3177                         (const void __user *)arg,
3178                         sizeof(struct ov5693_mode))) {
3179                         dev_err(&info->i2c_client->dev,
3180                                 "%s:Failed to get mode from user.\n",
3181                         __func__);
3182                         return -EFAULT;
3183                 }
3184                 return ov5693_set_mode(info, &mode);
3185         }
3186         case OV5693_IOCTL_GET_STATUS: {
3187                 u8 status = 0;
3188                 if (copy_to_user((void __user *)arg, &status, sizeof(status))) {
3189                         dev_err(&info->i2c_client->dev,
3190                                 "%s:Failed to copy status to user.\n",
3191                         __func__);
3192                         return -EFAULT;
3193                 }
3194                 return 0;
3195                 }
3196
3197         case OV5693_IOCTL_SET_GROUP_HOLD: {
3198                 struct ov5693_ae ae;
3199                 if (copy_from_user(&ae, (const void __user *)arg,
3200                                 sizeof(struct ov5693_ae))) {
3201                         dev_dbg(&info->i2c_client->dev,
3202                                 "%s:fail group hold\n", __func__);
3203                         return -EFAULT;
3204                 }
3205
3206                 return ov5693_set_group_hold(info, &ae);
3207                 }
3208
3209         case OV5693_IOCTL_SET_FRAME_LENGTH:
3210                 return ov5693_set_frame_length(info, (u32)arg, true);
3211
3212         case OV5693_IOCTL_SET_COARSE_TIME:
3213                 return ov5693_set_coarse_time(info, (u32)arg,
3214                                         OV5693_INVALID_COARSE_TIME, true);
3215
3216         case OV5693_IOCTL_SET_HDR_COARSE_TIME:
3217         {
3218                 struct ov5693_hdr *hdrcoarse = (struct ov5693_hdr *)arg;
3219                 int ret = ov5693_set_coarse_time(info,
3220                                 hdrcoarse->coarse_time_long,
3221                                 hdrcoarse->coarse_time_short,
3222                                 true);
3223                 return ret;
3224         }
3225
3226
3227         case OV5693_IOCTL_SET_GAIN:
3228                 return ov5693_set_gain(info, (u32)arg, true);
3229
3230         case OV5693_IOCTL_GET_FUSEID:
3231         {
3232                 err = ov5693_get_fuse_id(info);
3233
3234                 if (err) {
3235                         dev_err(&info->i2c_client->dev, "%s:Failed to get fuse id info.\n",
3236                         __func__);
3237                         return err;
3238                 }
3239                 if (copy_to_user((void __user *)arg,
3240                                 &info->fuseid,
3241                                 sizeof(struct ov5693_fuseid))) {
3242                         dev_dbg(&info->i2c_client->dev, "%s:Fail copy fuse id to user space\n",
3243                                 __func__);
3244                         return -EFAULT;
3245                 }
3246                 return 0;
3247         }
3248
3249         case OV5693_IOCTL_READ_OTP_BANK:
3250         {
3251                 struct ov5693_otp_bank bank;
3252                 if (copy_from_user(&bank,
3253                                    (const void __user *)arg,
3254                                    sizeof(bank))) {
3255                         dev_err(&info->i2c_client->dev,
3256                                 "%s %d copy_from_user err\n",
3257                                 __func__, __LINE__);
3258                         return -EINVAL;
3259                 }
3260
3261                 err = ov5693_read_otp_bank(info, &bank);
3262                 if (err != 0)
3263                         return err;
3264
3265                 if (copy_to_user((void __user *)arg,
3266                                  &bank,
3267                                  sizeof(bank))) {
3268                         dev_err(&info->i2c_client->dev,
3269                                 "%s %d copy_to_user err\n",
3270                                 __func__, __LINE__);
3271                         return -EFAULT;
3272                 }
3273                 return 0;
3274         }
3275
3276         case OV5693_IOCTL_SET_CAL_DATA:
3277         {
3278                 if (copy_from_user(&info->cal, (const void __user *)arg,
3279                                         sizeof(info->cal))) {
3280                         dev_err(&info->i2c_client->dev,
3281                                 "%s %d copy_from_user err\n",
3282                                 __func__, __LINE__);
3283                         return -EINVAL;
3284                 }
3285                 return 0;
3286         }
3287
3288         default:
3289                 dev_err(&info->i2c_client->dev, "%s unsupported ioctl: %x\n",
3290                         __func__, cmd);
3291         }
3292         return -EINVAL;
3293 }
3294
3295 static void ov5693_sdata_init(struct ov5693_info *info)
3296 {
3297         memcpy(&info->sdata, &ov5693_dflt_sdata, sizeof(info->sdata));
3298         if (info->pdata->lens_focal_length)
3299                 info->sdata.focal_len = info->pdata->lens_focal_length;
3300         if (info->pdata->lens_max_aperture)
3301                 info->sdata.max_aperture = info->pdata->lens_max_aperture;
3302         if (info->pdata->lens_fnumber)
3303                 info->sdata.fnumber = info->pdata->lens_fnumber;
3304         if (info->pdata->lens_view_angle_h)
3305                 info->sdata.view_angle_h = info->pdata->lens_view_angle_h;
3306         if (info->pdata->lens_view_angle_v)
3307                 info->sdata.view_angle_v = info->pdata->lens_view_angle_v;
3308 }
3309
3310 static int ov5693_open(struct inode *inode, struct file *file)
3311 {
3312         int err;
3313         struct miscdevice *miscdev = file->private_data;
3314         struct ov5693_info *info = dev_get_drvdata(miscdev->parent);
3315
3316         if (atomic_xchg(&info->in_use, 1))
3317                 return -EBUSY;
3318
3319         file->private_data = info;
3320         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
3321
3322         err = ov5693_power_on(info);
3323         return err;
3324 }
3325
3326 int ov5693_release(struct inode *inode, struct file *file)
3327 {
3328         struct ov5693_info *info = file->private_data;
3329
3330         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
3331         ov5693_pm_wr(info, NVC_PWR_OFF);
3332         file->private_data = NULL;
3333         WARN_ON(!atomic_xchg(&info->in_use, 0));
3334         return 0;
3335 }
3336
3337 static const struct file_operations ov5693_fileops = {
3338         .owner = THIS_MODULE,
3339         .open = ov5693_open,
3340         .unlocked_ioctl = ov5693_ioctl,
3341         .release = ov5693_release,
3342 };
3343
3344 static void ov5693_del(struct ov5693_info *info)
3345 {
3346         ov5693_pm_exit(info);
3347         synchronize_rcu();
3348 }
3349
3350 static int ov5693_remove(struct i2c_client *client)
3351 {
3352         struct ov5693_info *info = i2c_get_clientdata(client);
3353
3354         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
3355         misc_deregister(&info->miscdev);
3356         ov5693_del(info);
3357         return 0;
3358 }
3359
3360 static struct of_device_id ov5693_of_match[] = {
3361         { .compatible = "nvidia,ov5693", },
3362         { },
3363 };
3364
3365 MODULE_DEVICE_TABLE(of, ov5693_of_match);
3366
3367 static int ov5693_platform_power_on(struct ov5693_power_rail *pw)
3368 {
3369         int err;
3370         struct ov5693_info *info = container_of(pw, struct ov5693_info,
3371                                                 regulators);
3372
3373         if (info->pdata->use_vcm_vdd) {
3374                 err = regulator_enable(info->ext_vcm_vdd);
3375                 if (unlikely(err))
3376                         goto ov5693_vcm_fail;
3377         }
3378
3379         ov5693_gpio_wr(info, OV5693_GPIO_TYPE_PWRDN, 1);
3380         usleep_range(10, 20);
3381
3382         err = regulator_enable(pw->avdd);
3383         if (err)
3384                 goto ov5693_avdd_fail;
3385
3386         err = regulator_enable(pw->dovdd);
3387         if (err)
3388                 goto ov5693_iovdd_fail;
3389
3390         usleep_range(1, 2);
3391         ov5693_gpio_wr(info, OV5693_GPIO_TYPE_PWRDN, 0);
3392
3393         usleep_range(300, 310);
3394
3395         return 0;
3396
3397 ov5693_iovdd_fail:
3398         regulator_disable(pw->avdd);
3399
3400 ov5693_avdd_fail:
3401         if (info->pdata->use_vcm_vdd)
3402                 regulator_disable(info->ext_vcm_vdd);
3403
3404 ov5693_vcm_fail:
3405         pr_err("%s FAILED\n", __func__);
3406         return err;
3407 }
3408
3409 static int ov5693_platform_power_off(struct ov5693_power_rail *pw)
3410 {
3411         struct ov5693_info *info = container_of(pw, struct ov5693_info,
3412                                                 regulators);
3413
3414         usleep_range(21, 25);
3415         ov5693_gpio_wr(info, OV5693_GPIO_TYPE_PWRDN, 1);
3416         usleep_range(1, 2);
3417
3418         regulator_disable(pw->dovdd);
3419         regulator_disable(pw->avdd);
3420         if (info->pdata->use_vcm_vdd)
3421                 regulator_disable(info->ext_vcm_vdd);
3422
3423         return 0;
3424 }
3425
3426 static int ov5693_parse_dt_gpio(struct device_node *np, const char *name,
3427                                 enum ov5693_gpio_type type,
3428                                 struct nvc_gpio_pdata *pdata)
3429 {
3430         enum of_gpio_flags gpio_flags;
3431
3432         if (of_find_property(np, name, NULL)) {
3433                 pdata->gpio = of_get_named_gpio_flags(np, name, 0, &gpio_flags);
3434                 pdata->gpio_type = type;
3435                 pdata->init_en = true;
3436                 pdata->active_high = !(gpio_flags & OF_GPIO_ACTIVE_LOW);
3437                 return 1;
3438         }
3439         return 0;
3440 }
3441
3442 static struct ov5693_platform_data *ov5693_parse_dt(struct i2c_client *client)
3443 {
3444         struct device_node *np = client->dev.of_node;
3445         struct ov5693_platform_data *pdata;
3446         struct nvc_gpio_pdata *gpio_pdata = NULL;
3447
3448         pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
3449         if (!pdata) {
3450                 dev_err(&client->dev, "Failed to allocate pdata\n");
3451                 return ERR_PTR(-ENOMEM);
3452         }
3453
3454         gpio_pdata = devm_kzalloc(&client->dev,
3455                 sizeof(*gpio_pdata) * ARRAY_SIZE(ov5693_gpio), GFP_KERNEL);
3456         if (!gpio_pdata) {
3457                 dev_err(&client->dev, "cannot allocate gpio data memory\n");
3458                 return ERR_PTR(-ENOMEM);
3459         }
3460
3461         /* init with default platform data values */
3462         memcpy(pdata, &ov5693_dflt_pdata, sizeof(*pdata));
3463
3464         /* extra regulators info */
3465         pdata->use_vcm_vdd = of_property_read_bool(np, "nvidia,use-vcm-vdd");
3466
3467         /* generic info */
3468         of_property_read_u32(np, "nvidia,num", &pdata->num);
3469         of_property_read_string(np, "nvidia,dev-name", &pdata->dev_name);
3470
3471         /* ov5693 gpios */
3472         pdata->gpio_count = 0;
3473         pdata->gpio_count += ov5693_parse_dt_gpio(np,
3474                                 "reset-gpios", OV5693_GPIO_TYPE_PWRDN,
3475                                 &gpio_pdata[pdata->gpio_count]);
3476         pdata->gpio = gpio_pdata;
3477
3478         /* ov5693 power functions */
3479         pdata->power_on = ov5693_platform_power_on;
3480         pdata->power_off = ov5693_platform_power_off;
3481
3482         return pdata;
3483 }
3484
3485 static int ov5693_probe(
3486         struct i2c_client *client,
3487         const struct i2c_device_id *id)
3488 {
3489         struct ov5693_info *info;
3490         char dname[16];
3491         unsigned long clock_probe_rate;
3492         int err;
3493         static struct regmap_config ad5823_regmap_config = {
3494                 .reg_bits = 16,
3495                 .val_bits = 8,
3496         };
3497
3498
3499         dev_dbg(&client->dev, "%s\n", __func__);
3500         info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
3501         if (info == NULL) {
3502                 dev_err(&client->dev, "%s: kzalloc error\n", __func__);
3503                 return -ENOMEM;
3504         }
3505
3506         info->i2c_client = client;
3507         if (client->dev.of_node) {
3508                 info->pdata = ov5693_parse_dt(client);
3509                 if (IS_ERR(info->pdata)) {
3510                         err = PTR_ERR(info->pdata);
3511                         dev_err(&client->dev,
3512                                 "Failed to parse OF node: %d\n", err);
3513                         return err;
3514                 }
3515         } else if (client->dev.platform_data)
3516                 info->pdata = client->dev.platform_data;
3517         else {
3518                 info->pdata = &ov5693_dflt_pdata;
3519                 dev_dbg(&client->dev,
3520                         "%s No platform data. Using defaults.\n",
3521                         __func__);
3522         }
3523         if (info->pdata->use_vcm_vdd) {
3524                 info->ext_vcm_vdd = devm_regulator_get(&info->i2c_client->dev,
3525                                                         "ext_vcm_vdd");
3526                 if (WARN_ON(IS_ERR(info->ext_vcm_vdd))) {
3527                         err = PTR_ERR(info->ext_vcm_vdd);
3528                         dev_err(&client->dev,
3529                                 "ext_vcm_vdd get failed %d\n", err);
3530                         info->ext_vcm_vdd = NULL;
3531                         return err;
3532                 }
3533         }
3534
3535         info->regmap = devm_regmap_init_i2c(client, &ad5823_regmap_config);
3536         if (IS_ERR(info->regmap)) {
3537                 err = PTR_ERR(info->regmap);
3538                 dev_err(&client->dev,
3539                         "Failed to allocate register map: %d\n", err);
3540                 return err;
3541         }
3542
3543
3544         i2c_set_clientdata(client, info);
3545         ov5693_pm_init(info);
3546         if (!info->regulators.avdd || !info->regulators.dovdd)
3547                 return -EFAULT;
3548
3549         ov5693_sdata_init(info);
3550         if (info->pdata->cfg & (NVC_CFG_NODEV | NVC_CFG_BOOT_INIT)) {
3551                 if (info->pdata->probe_clock) {
3552                         clock_probe_rate = 6000;  /* initial_clcok*/
3553                         clock_probe_rate *= 1000;
3554                         info->pdata->probe_clock(clock_probe_rate);
3555                 }
3556                 ov5693_pm_dev_wr(info, NVC_PWR_COMM);
3557                 ov5693_pm_dev_wr(info, NVC_PWR_OFF);
3558                 if (info->pdata->probe_clock)
3559                         info->pdata->probe_clock(0);
3560         }
3561         if (info->pdata->dev_name != NULL)
3562                 strcpy(dname, info->pdata->dev_name);
3563         else
3564                 strcpy(dname, "ov5693");
3565         if (info->pdata->num)
3566                 snprintf(dname, sizeof(dname), "%s.%u",
3567                          dname, info->pdata->num);
3568         info->miscdev.name = dname;
3569         info->miscdev.fops = &ov5693_fileops;
3570         info->miscdev.minor = MISC_DYNAMIC_MINOR;
3571         info->miscdev.parent = &client->dev;
3572         if (misc_register(&info->miscdev)) {
3573                 dev_err(&client->dev, "%s unable to register misc device %s\n",
3574                         __func__, dname);
3575                 ov5693_del(info);
3576                 return -ENODEV;
3577         }
3578
3579         dev_dbg(&client->dev, "ov5693 sensor driver loading done\n");
3580         return 0;
3581 }
3582
3583 static const struct i2c_device_id ov5693_id[] = {
3584         { "ov5693", 0 },
3585         { },
3586 };
3587
3588 MODULE_DEVICE_TABLE(i2c, ov5693_id);
3589
3590 static struct i2c_driver ov5693_i2c_driver = {
3591         .driver = {
3592                 .name = "ov5693",
3593                 .owner = THIS_MODULE,
3594                 .of_match_table = ov5693_of_match,
3595         },
3596         .id_table = ov5693_id,
3597         .probe = ov5693_probe,
3598         .remove = ov5693_remove,
3599 };
3600
3601 module_i2c_driver(ov5693_i2c_driver);
3602 MODULE_LICENSE("GPL v2");