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