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