7d7c8b870c87e671600665cb68ca03947ad9c777
[linux-3.10.git] / drivers / media / video / tegra / imx091.c
1 /*
2  * imx091.c - imx091 sensor driver
3  *
4  * Copyright (c) 2012-2013, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <linux/fs.h>
20 #include <linux/i2c.h>
21 #include <linux/miscdevice.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24 #include <linux/uaccess.h>
25 #include <linux/atomic.h>
26 #include <linux/gpio.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/module.h>
29 #include <linux/list.h>
30 #include <linux/edp.h>
31 #include <media/imx091.h>
32
33 #ifdef CONFIG_DEBUG_FS
34 #include <media/nvc_debugfs.h>
35 #endif
36
37 #define IMX091_ID                       0x0091
38 #define IMX091_ID_ADDRESS   0x0000
39 #define IMX091_STREAM_CONTROL_REG 0x0100
40 #define IMX091_STREAM_ENABLE 0x01
41 #define IMX091_STREAM_DISABLE 0x00
42 #define IMX091_SENSOR_TYPE              NVC_IMAGER_TYPE_RAW
43 #define IMX091_STARTUP_DELAY_MS         50
44 #define IMX091_RES_CHG_WAIT_TIME_MS     100
45 #define IMX091_SIZEOF_I2C_BUF           16
46 #define IMX091_TABLE_WAIT_MS            0
47 #define IMX091_TABLE_END                1
48 #define IMX091_NUM_MODES                ARRAY_SIZE(imx091_mode_table)
49 #define IMX091_MODE_UNKNOWN             (IMX091_NUM_MODES + 1)
50 #define IMX091_LENS_MAX_APERTURE        0 /* / _INT2FLOAT_DIVISOR */
51 #define IMX091_LENS_FNUMBER             0 /* / _INT2FLOAT_DIVISOR */
52 #define IMX091_LENS_FOCAL_LENGTH        4760 /* / _INT2FLOAT_DIVISOR */
53 #define IMX091_LENS_VIEW_ANGLE_H        60400 /* / _INT2FLOAT_DIVISOR */
54 #define IMX091_LENS_VIEW_ANGLE_V        60400 /* / _INT2FLOAT_DIVISOR */
55 #define IMX091_WAIT_MS 3
56 #define IMX091_I2C_TABLE_MAX_ENTRIES    400
57
58 static u16 imx091_ids[] = {
59         0x0091,
60 };
61
62 static struct nvc_gpio_init imx091_gpios[] = {
63         {IMX091_GPIO_RESET, GPIOF_OUT_INIT_LOW, "reset", false, true},
64         {IMX091_GPIO_PWDN, GPIOF_OUT_INIT_LOW, "pwdn", false, true},
65         {IMX091_GPIO_GP1, 0, "gp1", false, false},
66 };
67
68 static struct nvc_regulator_init imx091_vregs[] = {
69         { IMX091_VREG_DVDD, "vdig", },
70         { IMX091_VREG_AVDD, "vana", },
71         { IMX091_VREG_IOVDD, "vif", },
72 };
73
74 struct imx091_info {
75         atomic_t in_use;
76         struct i2c_client *i2c_client;
77         struct imx091_platform_data *pdata;
78         struct nvc_imager_cap *cap;
79         struct miscdevice miscdev;
80         struct list_head list;
81         struct nvc_gpio gpio[ARRAY_SIZE(imx091_gpios)];
82         struct nvc_regulator vreg[ARRAY_SIZE(imx091_vregs)];
83         struct edp_client *edpc;
84         unsigned edp_state;
85         int pwr_api;
86         int pwr_dev;
87         u8 s_mode;
88         struct imx091_info *s_info;
89         u32 mode_index;
90         bool mode_valid;
91         bool mode_enable;
92         bool reset_flag;
93         unsigned test_pattern;
94         struct nvc_imager_static_nvc sdata;
95         u8 i2c_buf[IMX091_SIZEOF_I2C_BUF];
96         u8 bin_en;
97 #ifdef CONFIG_DEBUG_FS
98         struct nvc_debugfs_info debugfs_info;
99 #endif
100 };
101
102 struct imx091_reg {
103         u16 addr;
104         u16 val;
105 };
106
107 struct imx091_mode_data {
108         struct nvc_imager_mode sensor_mode;
109         struct nvc_imager_dynamic_nvc sensor_dnvc;
110         struct imx091_reg *p_mode_i2c;
111 };
112
113 static struct nvc_imager_cap imx091_dflt_cap = {
114         .identifier             = "IMX091",
115         .sensor_nvc_interface   = 3,
116         .pixel_types[0]         = 0x100,
117         .orientation            = 0,
118         .direction              = 0,
119         .initial_clock_rate_khz = 6000,
120         .clock_profiles[0] = {
121                 .external_clock_khz     = 24000,
122                 .clock_multiplier       = 8500000, /* value / 1,000,000 */
123         },
124         .clock_profiles[1] = {
125                 .external_clock_khz     = 0,
126                 .clock_multiplier       = 0,
127         },
128         .h_sync_edge            = 0,
129         .v_sync_edge            = 0,
130         .mclk_on_vgp0           = 0,
131         .csi_port               = 0,
132         .data_lanes             = 4,
133         .virtual_channel_id     = 0,
134 #ifdef CONFIG_TEGRA_FPGA_PLATFORM
135         .discontinuous_clk_mode = 0,
136         .cil_threshold_settle   = 0xd,
137 #else
138         .discontinuous_clk_mode = 1,
139         .cil_threshold_settle   = 0x0,
140 #endif
141         .min_blank_time_width   = 16,
142         .min_blank_time_height  = 16,
143         .preferred_mode_index   = 1,
144 #ifdef CONFIG_TEGRA_FPGA_PLATFORM
145         .focuser_guid   = 0,
146         .torch_guid             = 0,
147 #else
148         .focuser_guid   = NVC_FOCUS_GUID(0),
149         .torch_guid             = NVC_TORCH_GUID(0),
150 #endif
151         .cap_version            = NVC_IMAGER_CAPABILITIES_VERSION2,
152 };
153
154 static struct imx091_platform_data imx091_dflt_pdata = {
155         .cfg                    = 0,
156         .num                    = 0,
157         .sync                   = 0,
158         .dev_name               = "camera",
159         .cap                    = &imx091_dflt_cap,
160 };
161
162         /* NOTE: static vs dynamic
163          * If a member in the nvc_imager_static_nvc structure is not actually
164          * static data, then leave blank and add the parameter to the parameter
165          * read function that dynamically reads the data.  The NVC user driver
166          * will call the parameter read for the data if the member data is 0.
167          * If the dynamic data becomes static during probe (a one time read
168          * such as device ID) then add the dynamic read to the _sdata_init
169          * function.
170          */
171 static struct nvc_imager_static_nvc imx091_dflt_sdata = {
172         .api_version            = NVC_IMAGER_API_STATIC_VER,
173         .sensor_type            = IMX091_SENSOR_TYPE,
174         .bits_per_pixel         = 10,
175         .sensor_id              = IMX091_ID,
176         .sensor_id_minor        = 0,
177         .focal_len              = IMX091_LENS_FOCAL_LENGTH,
178         .max_aperture           = IMX091_LENS_MAX_APERTURE,
179         .fnumber                = IMX091_LENS_FNUMBER,
180         .view_angle_h           = IMX091_LENS_VIEW_ANGLE_H,
181         .view_angle_v           = IMX091_LENS_VIEW_ANGLE_V,
182         .res_chg_wait_time      = IMX091_RES_CHG_WAIT_TIME_MS,
183 };
184
185 static LIST_HEAD(imx091_info_list);
186 static DEFINE_SPINLOCK(imx091_spinlock);
187
188
189 static struct imx091_reg tp_none_seq[] = {
190         {IMX091_TABLE_END, 0x0000}
191 };
192
193 static struct imx091_reg tp_cbars_seq[] = {
194         {IMX091_TABLE_END, 0x0000}
195 };
196
197 static struct imx091_reg tp_checker_seq[] = {
198         {IMX091_TABLE_END, 0x0000}
199 };
200
201 static struct imx091_reg *test_patterns[] = {
202         tp_none_seq,
203         tp_cbars_seq,
204         tp_checker_seq,
205 };
206
207 #ifdef CONFIG_TEGRA_FPGA_PLATFORM
208 #define IMX091_WAIT_1000_MS 1000
209 static struct imx091_reg imx091_FPGA_1052x1560_i2c[] = {
210         /* Stand by */
211         {0x0100, 0x00},
212         {IMX091_TABLE_WAIT_MS, IMX091_WAIT_MS},
213
214         /* global settings */
215         {0x3087, 0x53},
216         {0x309D, 0x94},
217         {0x30A1, 0x08},
218         {0x30C7, 0x00},
219         {0x3115, 0x0E},
220         {0x3118, 0x42},
221         {0x311D, 0x34},
222         {0x3121, 0x0D},
223         {0x3212, 0xF2},
224         {0x3213, 0x0F},
225         {0x3215, 0x0F},
226         {0x3217, 0x0B},
227         {0x3219, 0x0B},
228         {0x321B, 0x0D},
229         {0x321D, 0x0D},
230
231         /* black level */
232         {0x3032, 0x00},
233
234         /*
235          * PLL (MCLK) = 13MHz
236          * Previously, this was calibrated for a 24MHz system, which has
237          * PREPLLCLK_DIV of 2 (1/2), RGPLTD of 2 (1/1), and PLL_MPY of 47.
238          * In the standard setting, this is set to 564MHz.
239          * In the 13MHz (FPGA) setting, set for a PREPLLCLK_DIV of 1 (1/1),
240          * a PLL_MPY of 43, and a RGPLTD of 2 (1/1), for an output PLL frequency
241          * of 559MHz.
242          */
243         {0x0305, 0x01}, /* PREPLLCLK_DIV */
244         {0x0307, 0x18}, /* PLL_MPY */
245         {0x30A4, 0x01}, /* RGPLTD */
246         {0x303C, 0x28}, /* PLSTATIM */
247
248         /* Mode Settings */
249         {0x0112, 0x0A}, /* RAW 10 */
250         {0x0113, 0x0A},
251         {0x0340, 0x07}, /* FrameLength 0x700 = 1792 */
252         {0x0341, 0x00},
253         {0x0342, 0x12}, /* LineLength = 0x120C = 4620 */
254         {0x0343, 0x0C},
255
256         /* Imaging Area Determination */
257         {0x0344, 0x00}, /* ReadOut start horizontal position = 8 */
258         {0x0345, 0x08},
259         {0x0346, 0x00}, /* ReadOut start vertical position = 0x30 = 48 */
260         {0x0347, 0x30},
261         {0x0348, 0x10}, /* ReadOut end horizontal position = 0x1077 */
262         {0x0349, 0x77},
263         {0x034A, 0x0C}, /* ReadOut end vertical position = 0xc5f */
264         {0x034B, 0x5F},
265         {0x034C, 0x04}, /* ReadOut size horizontal position = 0x041C = 1052 */
266         {0x034D, 0x1C},
267         {0x034E, 0x06}, /* ReadOut size vertical position = 0x0618 = 1560 */
268         {0x034F, 0x18},
269         {0x0381, 0x05},
270         {0x0383, 0x03},
271         {0x0385, 0x01},
272         {0x0387, 0x03},
273         {0x3033, 0x00}, /* HD mode off */
274         {0x303D, 0x10}, /* standby end immediate */
275         {0x303E, 0xD0},
276         {0x3040, 0x08}, /* OPB start and end address vert */
277         {0x3041, 0x97},
278         {0x3048, 0x01}, /* vertical addition enabled */
279         {0x304C, 0x7F}, /* readout pixels set to 0x04_78 */
280         {0x304D, 0x04},
281         {0x3064, 0x12},
282         {0x309B, 0x28},
283         {0x309E, 0x00},
284         {0x30D5, 0x09},
285         {0x30D6, 0x00},
286         {0x30D7, 0x00},
287         {0x30D8, 0x00},
288         {0x30D9, 0x00},
289         {0x30DA, 0x00},
290         {0x30DB, 0x00},
291         {0x30DC, 0x00},
292         {0x30DD, 0x00},
293         {0x30DE, 0x04},
294         {0x3102, 0x10},
295         {0x3103, 0x44},
296         {0x3104, 0x40},
297         {0x3105, 0x00},
298         {0x3106, 0x0D},
299         {0x3107, 0x01},
300         {0x310A, 0x0A},
301         {0x315C, 0x99},
302         {0x315D, 0x98},
303         {0x316E, 0x9A},
304         {0x316F, 0x99},
305         {0x3301, 0x03}, /* 4 Lane */
306         {0x3304, 0x05},
307         {0x3305, 0x04},
308         {0x3306, 0x12},
309         {0x3307, 0x03},
310         {0x3308, 0x0D},
311         {0x3309, 0x05},
312         {0x330A, 0x09},
313         {0x330B, 0x04},
314         {0x330C, 0x08},
315         {0x330D, 0x05},
316         {0x330E, 0x03},
317         {0x3318, 0x73},
318         {0x3322, 0x02},
319         {0x3342, 0x0F},
320         {0x3348, 0xE0},
321         {0x0600, 0x00}, /* colorbar fade-to-gray */
322         {0x0601, 0x00},
323         {0x0101, 0x03}, /* image orientation */
324         {0x0100, 0x01},
325
326         {IMX091_TABLE_WAIT_MS, IMX091_WAIT_MS},
327         {IMX091_TABLE_END, 0x00}
328 };
329 #else
330 static struct imx091_reg imx091_4208x3120_i2c[] = {
331         /* Reset */
332         {0x0103, 0x01},
333         {IMX091_TABLE_WAIT_MS, IMX091_WAIT_MS},
334
335         /* global settings */
336         {0x3087, 0x53},
337         {0x309D, 0x94},
338         {0x30A1, 0x08},
339         {0x30C7, 0x00},
340         {0x3115, 0x0E},
341         {0x3118, 0x42},
342         {0x311D, 0x34},
343         {0x3121, 0x0D},
344         {0x3212, 0xF2},
345         {0x3213, 0x0F},
346         {0x3215, 0x0F},
347         {0x3217, 0x0B},
348         {0x3219, 0x0B},
349         {0x321B, 0x0D},
350         {0x321D, 0x0D},
351
352         /* black level setting */
353         {0x3032, 0x40},
354
355         /* PLL */
356         {0x0305, 0x02},
357         {0x0307, 0x2A}, /* PLL Multipiler = 42 */
358         {0x30A4, 0x02},
359         {0x303C, 0x4B},
360
361         /* Mode Settings */
362         {0x0112, 0x0A},
363         {0x0113, 0x0A},
364         {0x0340, 0x0C},
365         {0x0341, 0x58},
366         {0x0342, 0x12},
367         {0x0343, 0x0C},
368         {0x0344, 0x00},
369         {0x0345, 0x08},
370         {0x0346, 0x00},
371         {0x0347, 0x30},
372         {0x0348, 0x10},
373         {0x0349, 0x77},
374         {0x034A, 0x0C},
375         {0x034B, 0x5F},
376         {0x034C, 0x10},
377         {0x034D, 0x70},
378         {0x034E, 0x0C},
379         {0x034F, 0x30},
380         {0x0381, 0x01},
381         {0x0383, 0x01},
382         {0x0385, 0x01},
383         {0x0387, 0x01},
384         {0x3033, 0x00},
385         {0x303D, 0x10},
386         {0x303E, 0xD0},
387         {0x3040, 0x08},
388         {0x3041, 0x97},
389         {0x3048, 0x00},
390         {0x304C, 0x7F},
391         {0x304D, 0x04},
392         {0x3064, 0x12},
393         {0x309B, 0x20},
394         {0x309E, 0x00},
395         {0x30D5, 0x00},
396         {0x30D6, 0x85},
397         {0x30D7, 0x2A},
398         {0x30D8, 0x64},
399         {0x30D9, 0x89},
400         {0x30DE, 0x00},
401         {0x3102, 0x10},
402         {0x3103, 0x44},
403         {0x3104, 0x40},
404         {0x3105, 0x00},
405         {0x3106, 0x0D},
406         {0x3107, 0x01},
407         {0x310A, 0x0A},
408         {0x315C, 0x99},
409         {0x315D, 0x98},
410         {0x316E, 0x9A},
411         {0x316F, 0x99},
412         {0x3301, 0x03},
413         {0x3304, 0x05},
414         {0x3305, 0x04},
415         {0x3306, 0x12},
416         {0x3307, 0x03},
417         {0x3308, 0x0D},
418         {0x3309, 0x05},
419         {0x330A, 0x09},
420         {0x330B, 0x04},
421         {0x330C, 0x08},
422         {0x330D, 0x05},
423         {0x330E, 0x03},
424         {0x3318, 0x65},
425         {0x3322, 0x02},
426         {0x3342, 0x0F},
427         {0x3348, 0xE0},
428
429         {0x0202, 0x0B},
430         {0x0203, 0xB8},
431         {0x0205, 0x00},
432
433         {IMX091_TABLE_END, 0x00}
434 };
435
436 static struct imx091_reg imx091_1948x1096_i2c[] = {
437         /* Reset */
438         {0x0103, 0x01},
439         {IMX091_TABLE_WAIT_MS, IMX091_WAIT_MS},
440
441         /* global settings */
442         {0x3087, 0x53},
443         {0x309D, 0x94},
444         {0x30A1, 0x08},
445         {0x30C7, 0x00},
446         {0x3115, 0x0E},
447         {0x3118, 0x42},
448         {0x311D, 0x34},
449         {0x3121, 0x0D},
450         {0x3212, 0xF2},
451         {0x3213, 0x0F},
452         {0x3215, 0x0F},
453         {0x3217, 0x0B},
454         {0x3219, 0x0B},
455         {0x321B, 0x0D},
456         {0x321D, 0x0D},
457
458         /* black level setting */
459         {0x3032, 0x40},
460
461         /* PLL */
462         {0x0305, 0x02},
463         {0x0307, 0x20},
464         {0x30A4, 0x02},
465         {0x303C, 0x4B},
466
467         /* Mode Settings */
468         {0x0112, 0x0A},
469         {0x0113, 0x0A},
470         {0x0340, 0x08},
471         {0x0341, 0xA6},
472         {0x0342, 0x09},
473         {0x0343, 0x06},
474         {0x0344, 0x00},
475         {0x0345, 0xA4},
476         {0x0346, 0x02},
477         {0x0347, 0x00},
478         {0x0348, 0x0F},
479         {0x0349, 0xDB},
480         {0x034A, 0x0A},
481         {0x034B, 0x8F},
482         {0x034C, 0x07},
483         {0x034D, 0x9C},
484         {0x034E, 0x04},
485         {0x034F, 0x48},
486         {0x0381, 0x01},
487         {0x0383, 0x01},
488         {0x0385, 0x01},
489         {0x0387, 0x03},
490         {0x3033, 0x84},
491         {0x303D, 0x10},
492         {0x303E, 0xD0},
493         {0x3040, 0x08},
494         {0x3041, 0x97},
495         {0x3048, 0x01},
496         {0x304C, 0x3F},
497         {0x304D, 0x02},
498         {0x3064, 0x12},
499         {0x309B, 0x48},
500         {0x309E, 0x04},
501         {0x30D5, 0x04},
502         {0x30D6, 0x85},
503         {0x30D7, 0x2A},
504         {0x30D8, 0x64},
505         {0x30D9, 0x89},
506         {0x30DE, 0x00},
507         {0x3102, 0x09},
508         {0x3103, 0x23},
509         {0x3104, 0x24},
510         {0x3105, 0x00},
511         {0x3106, 0x8B},
512         {0x3107, 0x00},
513         {0x310A, 0x0A},
514         {0x315C, 0x4A},
515         {0x315D, 0x49},
516         {0x316E, 0x4B},
517         {0x316F, 0x4A},
518         {0x3301, 0x03},
519         {0x3304, 0x05},
520         {0x3305, 0x04},
521         {0x3306, 0x12},
522         {0x3307, 0x03},
523         {0x3308, 0x0D},
524         {0x3309, 0x05},
525         {0x330A, 0x09},
526         {0x330B, 0x04},
527         {0x330C, 0x08},
528         {0x330D, 0x05},
529         {0x330E, 0x03},
530         {0x3318, 0x67},
531         {0x3322, 0x02},
532         {0x3342, 0x0F},
533         {0x3348, 0xE0},
534
535         {0x0202, 0x00},
536         {0x0203, 0x00},
537         {0x0205, 0x00},
538
539         {IMX091_TABLE_END, 0x00}
540 };
541
542 static struct imx091_reg imx091_1308x736_i2c[] = {
543         /* Reset */
544         {0x0103, 0x01},
545         {IMX091_TABLE_WAIT_MS, IMX091_WAIT_MS},
546
547         /* global settings */
548         {0x3087, 0x53},
549         {0x309D, 0x94},
550         {0x30A1, 0x08},
551         {0x30C7, 0x00},
552         {0x3115, 0x0E},
553         {0x3118, 0x42},
554         {0x311D, 0x34},
555         {0x3121, 0x0D},
556         {0x3212, 0xF2},
557         {0x3213, 0x0F},
558         {0x3215, 0x0F},
559         {0x3217, 0x0B},
560         {0x3219, 0x0B},
561         {0x321B, 0x0D},
562         {0x321D, 0x0D},
563
564         /* black level setting */
565         {0x3032, 0x40},
566
567         /* PLL */
568         {0x0305, 0x02},
569         {0x0307, 0x20},
570         {0x30A4, 0x02},
571         {0x303C, 0x4B},
572
573         /* Mode Settings */
574         {0x0112, 0x0A},
575         {0x0113, 0x0A},
576         {0x0340, 0x04},
577         {0x0341, 0x4E},
578         {0x0342, 0x12},
579         {0x0343, 0x0C},
580         {0x0344, 0x00},
581         {0x0345, 0x96},
582         {0x0346, 0x01},
583         {0x0347, 0xF8},
584         {0x0348, 0x0F},
585         {0x0349, 0xE9},
586         {0x034A, 0x0A},
587         {0x034B, 0x97},
588         {0x034C, 0x05},
589         {0x034D, 0x1C},
590         {0x034E, 0x02},
591         {0x034F, 0xE0},
592         {0x0381, 0x03},
593         {0x0383, 0x03},
594         {0x0385, 0x03},
595         {0x0387, 0x03},
596         {0x3033, 0x00},
597         {0x303D, 0x10},
598         {0x303E, 0xD0},
599         {0x3040, 0x08},
600         {0x3041, 0x97},
601         {0x3048, 0x22},
602         {0x304C, 0x7F},
603         {0x304D, 0x04},
604         {0x3064, 0x12},
605         {0x309B, 0x60},
606         {0x309E, 0x04},
607         {0x30D5, 0x09},
608         {0x30D6, 0x00},
609         {0x30D7, 0x00},
610         {0x30D8, 0x00},
611         {0x30D9, 0x89},
612         {0x30DE, 0x03},
613         {0x3102, 0x09},
614         {0x3103, 0x23},
615         {0x3104, 0x24},
616         {0x3105, 0x00},
617         {0x3106, 0x8B},
618         {0x3107, 0x00},
619         {0x310A, 0x0A},
620         {0x315C, 0x4A},
621         {0x315D, 0x49},
622         {0x316E, 0x4B},
623         {0x316F, 0x4A},
624         {0x3301, 0x03},
625         {0x3304, 0x05},
626         {0x3305, 0x04},
627         {0x3306, 0x12},
628         {0x3307, 0x03},
629         {0x3308, 0x0D},
630         {0x3309, 0x05},
631         {0x330A, 0x09},
632         {0x330B, 0x04},
633         {0x330C, 0x08},
634         {0x330D, 0x05},
635         {0x330E, 0x03},
636         {0x3318, 0x6C},
637         {0x3322, 0x02},
638         {0x3342, 0x0F},
639         {0x3348, 0xE0},
640
641         {0x0202, 0x00},
642         {0x0203, 0x00},
643         {0x0205, 0x00},
644
645         {IMX091_TABLE_END, 0x00}
646 };
647
648 static struct imx091_reg imx091_2104x1560_i2c[] = {
649         /* Software reset */
650         {0x0103, 0x01},
651         {IMX091_TABLE_WAIT_MS, IMX091_WAIT_MS},
652
653         /* global settings */
654         {0x3087, 0x53},
655         {0x309D, 0x94},
656         {0x30A1, 0x08},
657         {0x30C7, 0x00},
658         {0x3115, 0x0E},
659         {0x3118, 0x42},
660         {0x311D, 0x34},
661         {0x3121, 0x0D},
662         {0x3212, 0xF2},
663         {0x3213, 0x0F},
664         {0x3215, 0x0F},
665         {0x3217, 0x0B},
666         {0x3219, 0x0B},
667         {0x321B, 0x0D},
668         {0x321D, 0x0D},
669
670         /* black level setting */
671         {0x3032, 0x40},
672
673         /* PLL */
674         {0x0305, 0x02},
675         {0x0307, 0x2F},
676         {0x30A4, 0x02},
677         {0x303C, 0x4B},
678
679         /* Mode Settings */
680         {0x0112, 0x0A},
681         {0x0113, 0x0A},
682         {0x0340, 0x06},
683         {0x0341, 0x58},
684         {0x0342, 0x12},
685         {0x0343, 0x0C},
686         {0x0344, 0x00},
687         {0x0345, 0x08},
688         {0x0346, 0x00},
689         {0x0347, 0x30},
690         {0x0348, 0x10},
691         {0x0349, 0x77},
692
693         {0x034A, 0x0C},
694         {0x034B, 0x5F},
695         {0x034C, 0x08},
696         {0x034D, 0x38},
697         {0x034E, 0x06},
698         {0x034F, 0x18},
699         {0x0381, 0x01},
700         {0x0383, 0x03},
701         {0x0385, 0x01},
702         {0x0387, 0x03},
703         {0x3033, 0x00},
704         {0x303D, 0x10},
705         {0x303E, 0xD0},
706         {0x3040, 0x08},
707         {0x3041, 0x97},
708         {0x3048, 0x01},
709         {0x304C, 0x7F},
710         {0x304D, 0x04},
711         {0x3064, 0x12},
712         {0x309B, 0x28},
713         {0x309E, 0x00},
714         {0x30D5, 0x09},
715         {0x30D6, 0x01},
716         {0x30D7, 0x01},
717         {0x30D8, 0x64},
718         {0x30D9, 0x89},
719         {0x30DE, 0x02},
720         {0x3102, 0x10},
721         {0x3103, 0x44},
722         {0x3104, 0x40},
723         {0x3105, 0x00},
724         {0x3106, 0x0D},
725         {0x3107, 0x01},
726         {0x310A, 0x0A},
727         {0x315C, 0x99},
728         {0x315D, 0x98},
729         {0x316E, 0x9A},
730         {0x316F, 0x99},
731         {0x3301, 0x03},
732         {0x3304, 0x05},
733         {0x3305, 0x04},
734         {0x3306, 0x12},
735         {0x3307, 0x03},
736         {0x3308, 0x0D},
737         {0x3309, 0x05},
738         {0x330A, 0x09},
739         {0x330B, 0x04},
740         {0x330C, 0x08},
741         {0x330D, 0x05},
742         {0x330E, 0x03},
743         {0x3318, 0x73},
744         {0x3322, 0x02},
745         {0x3342, 0x0F},
746         {0x3348, 0xE0},
747
748         {0x0202, 0x06},
749         {0x0203, 0x00},
750         {0x0205, 0x00},
751
752         {IMX091_TABLE_WAIT_MS, IMX091_WAIT_MS},
753         {IMX091_TABLE_END, 0x00}
754 };
755
756 static struct imx091_reg imx091_524X390_i2c[] = {
757         /* Reset */
758         {0x0103, 0x01},
759         {IMX091_TABLE_WAIT_MS, IMX091_WAIT_MS},
760
761         /* global settings */
762         {0x3087, 0x53},
763         {0x309D, 0x94},
764         {0x30A1, 0x08},
765         {0x30C7, 0x00},
766         {0x3115, 0x0E},
767         {0x3118, 0x42},
768         {0x311D, 0x34},
769         {0x3121, 0x0D},
770         {0x3212, 0xF2},
771         {0x3213, 0x0F},
772         {0x3215, 0x0F},
773         {0x3217, 0x0B},
774         {0x3219, 0x0B},
775         {0x321B, 0x0D},
776         {0x321D, 0x0D},
777
778         /* black level setting */
779         {0x3032, 0x40},
780
781         /* PLL */
782         {0x0305, 0x02},
783         {0x0307, 0x2F},
784         {0x30A4, 0x02},
785         {0x303C, 0x4B},
786
787         /* Mode Settings */
788         {0x0112, 0x0A},
789         {0x0113, 0x0A},
790         {0x0340, 0x01},
791         {0x0341, 0x96},
792         {0x0342, 0x12},
793         {0x0343, 0x0C},
794         {0x0344, 0x00},
795         {0x0345, 0x10},
796         {0x0346, 0x00},
797         {0x0347, 0x30},
798         {0x0348, 0x10},
799         {0x0349, 0x6F},
800         {0x034A, 0x0C},
801         {0x034B, 0x5F},
802         {0x034C, 0x02},
803         {0x034D, 0x0C},
804         {0x034E, 0x01},
805         {0x034F, 0x86},
806         {0x0381, 0x09},
807         {0x0383, 0x07},
808         {0x0385, 0x09},
809         {0x0387, 0x07},
810         {0x3033, 0x00},
811         {0x303D, 0x10},
812         {0x303E, 0xD0},
813         {0x3040, 0x08},
814         {0x3041, 0x97},
815         {0x3048, 0x01},
816         {0x304C, 0x7F},
817         {0x304D, 0x04},
818         {0x3064, 0x12},
819         {0x309B, 0x28},
820         {0x309E, 0x00},
821         {0x30D5, 0x09},
822         {0x30D6, 0x00},
823         {0x30D7, 0x00},
824         {0x30D8, 0x00},
825         {0x30D9, 0x00},
826         {0x30DE, 0x08},
827         {0x3102, 0x10},
828         {0x3103, 0x44},
829         {0x3104, 0x40},
830         {0x3105, 0x00},
831         {0x3106, 0x0D},
832         {0x3107, 0x01},
833         {0x310A, 0x0A},
834         {0x315C, 0x99},
835         {0x315D, 0x98},
836         {0x316E, 0x9A},
837         {0x316F, 0x99},
838         {0x3301, 0x03},
839         {0x3304, 0x03},
840         {0x3305, 0x02},
841         {0x3306, 0x09},
842         {0x3307, 0x06},
843         {0x3308, 0x1E},
844         {0x3309, 0x05},
845         {0x330A, 0x05},
846         {0x330B, 0x04},
847         {0x330C, 0x07},
848         {0x330D, 0x06},
849         {0x330E, 0x01},
850         {0x3318, 0x44},
851         {0x3322, 0x0E},
852         {0x3342, 0x00},
853         {0x3348, 0xE0},
854
855         {0x0202, 0x00},
856         {0x0203, 0x00},
857         {0x0205, 0x00},
858
859         {IMX091_TABLE_END, 0x00}
860 };
861 #endif /* !CONFIG_TEGRA_FPGA_PLATFORM */
862
863 /* Each resolution requires the below data table setup and the corresponding
864  * I2C data table.
865  * If more NVC data is needed for the NVC driver, be sure and modify the
866  * nvc_imager_nvc structure in nvc_imager.h
867  * If more data sets are needed per resolution, they can be added to the
868  * table format below with the imx091_mode_data structure.  New data sets
869  * should conform to an already defined NVC structure.  If it's data for the
870  * NVC driver, then it should be added to the nvc_imager_nvc structure.
871  * Steps to add a resolution:
872  * 1. Add I2C data table
873  * 2. Add imx091_mode_data table
874  * 3. Add entry to the imx091_mode_table
875  */
876 #ifdef CONFIG_TEGRA_FPGA_PLATFORM
877 static struct imx091_mode_data imx091_FPGA_1052x1560 = {
878         .sensor_mode = {
879                 .res_x                  = 1032,
880                 .res_y                  = 1540,
881                 .active_start_x         = 0,
882                 .active_stary_y         = 0,
883                 .peak_frame_rate        = 30000, /* / _INT2FLOAT_DIVISOR */
884                 .pixel_aspect_ratio     = 1000,  /* / _INT2FLOAT_DIVISOR */
885                 .pll_multiplier         = 5000,  /* / _INT2FLOAT_DIVISOR */
886                 .crop_mode              = NVC_IMAGER_CROPMODE_NONE,
887         },
888         .sensor_dnvc = {
889                 .api_version            = NVC_IMAGER_API_DYNAMIC_VER,
890                 .region_start_x         = 0,
891                 .region_start_y         = 0,
892                 .x_scale                = 1,
893                 .y_scale                = 1,
894                 .bracket_caps           = 1,
895                 .flush_count            = 2,
896                 .init_intra_frame_skip  = 0,
897                 .ss_intra_frame_skip    = 2,
898                 .ss_frame_number        = 3,
899                 .coarse_time            = 0x06FB,
900                 .max_coarse_diff        = 5,
901                 .min_exposure_course    = 2,
902                 .max_exposure_course    = 0xFFFC,
903                 .diff_integration_time  = 110, /* / _INT2FLOAT_DIVISOR */
904                 .line_length            = 0x120C,
905                 .frame_length           = 0x0700,
906                 .min_frame_length       = 0x0700,
907                 .max_frame_length       = 0xFFFF,
908                 .min_gain               = 1, /* / _INT2FLOAT_DIVISOR */
909                 .max_gain               = 16000, /* / _INT2FLOAT_DIVISOR */
910                 .inherent_gain          = 1000, /* / _INT2FLOAT_DIVISOR */
911                 .inherent_gain_bin_en   = 1000, /* / _INT2FLOAT_DIVISOR */
912                 .support_bin_control    = 0,
913                 .support_fast_mode      = 0,
914                 .pll_mult               = 0x20,
915                 .pll_div                = 0x2,
916         },
917         .p_mode_i2c                     = imx091_FPGA_1052x1560_i2c,
918 };
919 #else
920 static struct imx091_mode_data imx091_4208x3120 = {
921         .sensor_mode = {
922                 .res_x                  = 4096,
923                 .res_y                  = 3072,
924                 .active_start_x         = 0,
925                 .active_stary_y         = 0,
926                 .peak_frame_rate        = 15000, /* / _INT2FLOAT_DIVISOR */
927                 .pixel_aspect_ratio     = 1000, /* / _INT2FLOAT_DIVISOR */
928                 .pll_multiplier         = 10000, /* / _INT2FLOAT_DIVISOR */
929                 .crop_mode              = NVC_IMAGER_CROPMODE_NONE,
930         },
931         .sensor_dnvc = {
932                 .api_version            = NVC_IMAGER_API_DYNAMIC_VER,
933                 .region_start_x         = 0,
934                 .region_start_y         = 0,
935                 .x_scale                = 1,
936                 .y_scale                = 1,
937                 .bracket_caps           = 1,
938                 .flush_count            = 2,
939                 .init_intra_frame_skip  = 0,
940                 .ss_intra_frame_skip    = 2,
941                 .ss_frame_number        = 3,
942                 .coarse_time            = 0x0C53,
943                 .max_coarse_diff        = 5,
944                 .min_exposure_course    = 2,
945                 .max_exposure_course    = 0xFFFC,
946                 .diff_integration_time  = 110, /* / _INT2FLOAT_DIVISOR */
947                 .line_length            = 0x120C,
948                 .frame_length           = 0x0C58,
949                 .min_frame_length       = 0x0C58,
950                 .max_frame_length       = 0xFFFF,
951                 .min_gain               = 1, /* / _INT2FLOAT_DIVISOR */
952                 .max_gain               = 16000, /* / _INT2FLOAT_DIVISOR */
953                 .inherent_gain          = 1000, /* / _INT2FLOAT_DIVISOR */
954                 .inherent_gain_bin_en   = 1000, /* / _INT2FLOAT_DIVISOR */
955                 .support_bin_control    = 0,
956                 .support_fast_mode      = 0,
957                 .pll_mult               = 0x2A,
958                 .pll_div                = 0x2,
959         },
960         .p_mode_i2c                     = imx091_4208x3120_i2c,
961 };
962
963 static struct imx091_mode_data imx091_1948x1096 = {
964         .sensor_mode = {
965                 .res_x                  = 1920,
966                 .res_y                  = 1080,
967                 .active_start_x         = 0,
968                 .active_stary_y         = 0,
969                 .peak_frame_rate        = 30000, /* / _INT2FLOAT_DIVISOR */
970                 .pixel_aspect_ratio     = 1000, /* / _INT2FLOAT_DIVISOR */
971                 .pll_multiplier         = 7000, /* / _INT2FLOAT_DIVISOR */
972                 .crop_mode              = NVC_IMAGER_CROPMODE_PARTIAL,
973         },
974         .sensor_dnvc = {
975                 .api_version            = NVC_IMAGER_API_DYNAMIC_VER,
976                 .region_start_x         = 0,
977                 .region_start_y         = 0,
978                 .x_scale                = 1,
979                 .y_scale                = 1,
980                 .bracket_caps           = 1,
981                 .flush_count            = 2,
982                 .init_intra_frame_skip  = 0,
983                 .ss_intra_frame_skip    = 2,
984                 .ss_frame_number        = 3,
985         .coarse_time            = 0x08A1,
986         .max_coarse_diff        = 5,
987                 .min_exposure_course    = 2,
988                 .max_exposure_course    = 0xFFFC,
989                 .diff_integration_time  = 110, /* / _INT2FLOAT_DIVISOR */
990                 .line_length            = 0x0906,
991                 .frame_length           = 0x08A6,
992                 .min_frame_length       = 0x08A6,
993                 .max_frame_length       = 0xFFFF,
994                 .min_gain               = 1, /* / _INT2FLOAT_DIVISOR */
995                 .max_gain               = 16000, /* / _INT2FLOAT_DIVISOR */
996                 .inherent_gain          = 1000, /* / _INT2FLOAT_DIVISOR */
997                 .inherent_gain_bin_en   = 1000, /* / _INT2FLOAT_DIVISOR */
998                 .support_bin_control    = 0,
999                 .support_fast_mode      = 0,
1000                 .pll_mult               = 0x20,
1001                 .pll_div                = 0x2,
1002         },
1003         .p_mode_i2c                     = imx091_1948x1096_i2c,
1004 };
1005
1006 static struct imx091_mode_data imx091_1308x736 = {
1007         .sensor_mode = {
1008                 .res_x                  = 1280,
1009                 .res_y                  = 720,
1010                 .active_start_x         = 0,
1011                 .active_stary_y         = 0,
1012                 .peak_frame_rate        = 30000, /* / _INT2FLOAT_DIVISOR */
1013                 .pixel_aspect_ratio     = 1000, /* / _INT2FLOAT_DIVISOR */
1014                 .pll_multiplier         = 5000, /* / _INT2FLOAT_DIVISOR */
1015                 .crop_mode              = NVC_IMAGER_CROPMODE_PARTIAL,
1016         },
1017         .sensor_dnvc = {
1018                 .api_version            = NVC_IMAGER_API_DYNAMIC_VER,
1019                 .region_start_x         = 0,
1020                 .region_start_y         = 0,
1021                 .x_scale                = 1,
1022                 .y_scale                = 1,
1023                 .bracket_caps           = 1,
1024                 .flush_count            = 2,
1025                 .init_intra_frame_skip  = 0,
1026                 .ss_intra_frame_skip    = 2,
1027                 .ss_frame_number        = 3,
1028                 .coarse_time            = 0x0448,
1029                 .max_coarse_diff        = 5,
1030                 .min_exposure_course    = 2,
1031                 .max_exposure_course    = 0xFFFC,
1032                 .diff_integration_time  = 110, /* / _INT2FLOAT_DIVISOR */
1033                 .line_length            = 0x120C,
1034                 .frame_length           = 0x044e,
1035                 .min_frame_length       = 0x044e,
1036                 .max_frame_length       = 0xFFFF,
1037                 .min_gain               = 1, /* / _INT2FLOAT_DIVISOR */
1038                 .max_gain               = 16000, /* / _INT2FLOAT_DIVISOR */
1039                 .inherent_gain          = 1000, /* / _INT2FLOAT_DIVISOR */
1040                 .inherent_gain_bin_en   = 1000, /* / _INT2FLOAT_DIVISOR */
1041                 .support_bin_control    = 0,
1042                 .support_fast_mode      = 0,
1043                 .pll_mult               = 0x20,
1044                 .pll_div                = 0x2,
1045         },
1046         .p_mode_i2c                     = imx091_1308x736_i2c,
1047 };
1048
1049 static struct imx091_mode_data imx091_2104x1560 = {
1050         .sensor_mode = {
1051                 .res_x                  = 2048,
1052                 .res_y                  = 1536,
1053                 .active_start_x         = 0,
1054                 .active_stary_y         = 0,
1055                 .peak_frame_rate        = 30000, /* / _INT2FLOAT_DIVISOR */
1056                 .pixel_aspect_ratio     = 1000, /* / _INT2FLOAT_DIVISOR */
1057                 .pll_multiplier         = 6000, /* / _INT2FLOAT_DIVISOR */
1058                 .crop_mode              = NVC_IMAGER_CROPMODE_NONE,
1059         },
1060         .sensor_dnvc = {
1061                 .api_version            = NVC_IMAGER_API_DYNAMIC_VER,
1062                 .region_start_x         = 0,
1063                 .region_start_y         = 0,
1064                 .x_scale                = 1,
1065                 .y_scale                = 1,
1066                 .bracket_caps           = 1,
1067                 .flush_count            = 2,
1068                 .init_intra_frame_skip  = 0,
1069                 .ss_intra_frame_skip    = 2,
1070                 .ss_frame_number        = 3,
1071                 .coarse_time            = 0x0653,
1072                 .max_coarse_diff        = 5,
1073                 .min_exposure_course    = 2,
1074                 .max_exposure_course    = 0xFFFC,
1075                 .diff_integration_time  = 110, /* / _INT2FLOAT_DIVISOR */
1076                 .line_length            = 0x120C,
1077                 .frame_length           = 0x0658,
1078                 .min_frame_length       = 0x0658,
1079                 .max_frame_length       = 0xFFFF,
1080                 .min_gain               = 1, /* / _INT2FLOAT_DIVISOR */
1081                 .max_gain               = 16000, /* / _INT2FLOAT_DIVISOR */
1082                 .inherent_gain          = 1000, /* / _INT2FLOAT_DIVISOR */
1083                 .inherent_gain_bin_en   = 1000, /* / _INT2FLOAT_DIVISOR */
1084                 .support_bin_control    = 0,
1085                 .support_fast_mode      = 0,
1086                 .pll_mult               = 0x2F,
1087                 .pll_div                = 0x2,
1088         },
1089         .p_mode_i2c                     = imx091_2104x1560_i2c,
1090 };
1091
1092 static struct imx091_mode_data imx091_524x390 = {
1093         .sensor_mode = {
1094                 .res_x                  = 524,
1095                 .res_y                  = 374,
1096                 .active_start_x         = 0,
1097                 .active_stary_y         = 0,
1098                 .peak_frame_rate        = 120000, /* / _INT2FLOAT_DIVISOR */
1099                 .pixel_aspect_ratio     = 1000, /* / _INT2FLOAT_DIVISOR */
1100                 .pll_multiplier         = 5000, /* / _INT2FLOAT_DIVISOR */
1101                 .crop_mode              = NVC_IMAGER_CROPMODE_PARTIAL,
1102         },
1103         .sensor_dnvc = {
1104                 .api_version            = NVC_IMAGER_API_DYNAMIC_VER,
1105                 .region_start_x         = 0,
1106                 .region_start_y         = 0,
1107                 .x_scale                = 1,
1108                 .y_scale                = 1,
1109                 .bracket_caps           = 1,
1110                 .flush_count            = 2,
1111                 .init_intra_frame_skip  = 0,
1112                 .ss_intra_frame_skip    = 2,
1113                 .ss_frame_number        = 3,
1114                 .coarse_time            = 0x0191,
1115                 .max_coarse_diff        = 5,
1116                 .min_exposure_course    = 2,
1117                 .max_exposure_course    = 0xFFFC,
1118                 .diff_integration_time  = 110, /* / _INT2FLOAT_DIVISOR */
1119                 .line_length            = 0x120C,
1120                 .frame_length           = 0x0196,
1121                 .min_frame_length       = 0x0196,
1122                 .max_frame_length       = 0xFFFF,
1123                 .min_gain               = 1, /* / _INT2FLOAT_DIVISOR */
1124                 .max_gain               = 16000, /* / _INT2FLOAT_DIVISOR */
1125                 .inherent_gain          = 1000, /* / _INT2FLOAT_DIVISOR */
1126                 .inherent_gain_bin_en   = 1000, /* / _INT2FLOAT_DIVISOR */
1127                 .support_bin_control    = 0,
1128                 .support_fast_mode      = 0,
1129                 .pll_mult               = 0x2F,
1130                 .pll_div                = 0x2,
1131         },
1132         .p_mode_i2c                     = imx091_524X390_i2c,
1133 };
1134 #endif /* CONFIG_TEGRA_FPGA_PLATFORM */
1135
1136 static struct imx091_mode_data *imx091_mode_table[] = {
1137 #ifdef CONFIG_TEGRA_FPGA_PLATFORM
1138         &imx091_FPGA_1052x1560,
1139 #else
1140         &imx091_4208x3120,
1141         &imx091_1948x1096,
1142         &imx091_1308x736,
1143         &imx091_2104x1560,
1144         &imx091_524x390,
1145 #endif
1146 };
1147
1148
1149 static int imx091_i2c_rd8(struct i2c_client *client, u16 reg, u8 *val)
1150 {
1151         struct i2c_msg msg[2];
1152         u8 buf[3];
1153
1154         buf[0] = (reg >> 8);
1155         buf[1] = (reg & 0x00FF);
1156         msg[0].addr = client->addr;
1157         msg[0].flags = 0;
1158         msg[0].len = 2;
1159         msg[0].buf = &buf[0];
1160         msg[1].addr = client->addr;
1161         msg[1].flags = I2C_M_RD;
1162         msg[1].len = 1;
1163         msg[1].buf = &buf[2];
1164         *val = 0;
1165         if (i2c_transfer(client->adapter, msg, 2) != 2)
1166                 return -EIO;
1167
1168         *val = buf[2];
1169         return 0;
1170 }
1171
1172 static int imx091_i2c_rd16(struct i2c_client *client, u16 reg, u16 *val)
1173 {
1174         struct i2c_msg msg[2];
1175         u8 buf[4];
1176
1177         buf[0] = (reg >> 8);
1178         buf[1] = (reg & 0x00FF);
1179         msg[0].addr = client->addr;
1180         msg[0].flags = 0;
1181         msg[0].len = 2;
1182         msg[0].buf = &buf[0];
1183         msg[1].addr = client->addr;
1184         msg[1].flags = I2C_M_RD;
1185         msg[1].len = 2;
1186         msg[1].buf = &buf[2];
1187         *val = 0;
1188         if (i2c_transfer(client->adapter, msg, 2) != 2)
1189                 return -EIO;
1190
1191         *val = (((u16)buf[2] << 8) | (u16)buf[3]);
1192         return 0;
1193 }
1194
1195 static int imx091_i2c_wr8(struct i2c_client *client, u16 reg, u8 val)
1196 {
1197         struct i2c_msg msg;
1198         u8 buf[3];
1199
1200         buf[0] = (reg >> 8);
1201         buf[1] = (reg & 0x00FF);
1202         buf[2] = val;
1203         msg.addr = client->addr;
1204         msg.flags = 0;
1205         msg.len = 3;
1206         msg.buf = &buf[0];
1207         if (i2c_transfer(client->adapter, &msg, 1) != 1)
1208                 return -EIO;
1209
1210         return 0;
1211 }
1212
1213 static int imx091_i2c_wr16(struct i2c_client *client, u16 reg, u16 val)
1214 {
1215         struct i2c_msg msg;
1216         u8 buf[4];
1217
1218         buf[0] = (reg >> 8);
1219         buf[1] = (reg & 0x00FF);
1220         buf[2] = (val & 0x00FF);
1221         buf[3] = (val >> 8);
1222         msg.addr = client->addr;
1223         msg.flags = 0;
1224         msg.len = 4;
1225         msg.buf = &buf[0];
1226         if (i2c_transfer(client->adapter, &msg, 1) != 1)
1227                 return -EIO;
1228
1229         return 0;
1230 }
1231
1232 static int imx091_i2c_rd_table(struct imx091_info *info,
1233                                struct imx091_reg table[])
1234 {
1235         struct imx091_reg *p_table = table;
1236         u8 val;
1237         int err = 0;
1238
1239         while (p_table->addr != IMX091_TABLE_END) {
1240                 err = imx091_i2c_rd8(info->i2c_client, p_table->addr, &val);
1241                 if (err)
1242                         return err;
1243
1244                 p_table->val = (u16)val;
1245                 p_table++;
1246         }
1247
1248         return err;
1249 }
1250
1251 static int imx091_i2c_wr_blk(struct imx091_info *info, u8 *buf, int len)
1252 {
1253         struct i2c_msg msg;
1254
1255         msg.addr = info->i2c_client->addr;
1256         msg.flags = 0;
1257         msg.len = len;
1258         msg.buf = buf;
1259         if (i2c_transfer(info->i2c_client->adapter, &msg, 1) != 1)
1260                 return -EIO;
1261
1262         return 0;
1263 }
1264
1265 static int imx091_i2c_wr_table(struct imx091_info *info,
1266                                struct imx091_reg table[])
1267 {
1268         int err;
1269         const struct imx091_reg *next;
1270         const struct imx091_reg *n_next;
1271         u8 *b_ptr = info->i2c_buf;
1272         u16 buf_count = 0;
1273
1274         for (next = table; next->addr != IMX091_TABLE_END; next++) {
1275                 if (next->addr == IMX091_TABLE_WAIT_MS) {
1276                         msleep(next->val);
1277                         continue;
1278                 }
1279
1280                 if (!buf_count) {
1281                         b_ptr = info->i2c_buf;
1282                         *b_ptr++ = next->addr >> 8;
1283                         *b_ptr++ = next->addr & 0xFF;
1284                         buf_count = 2;
1285                 }
1286                 *b_ptr++ = next->val;
1287                 buf_count++;
1288                 n_next = next + 1;
1289                 if (n_next->addr == next->addr + 1 &&
1290                                 n_next->addr != IMX091_TABLE_WAIT_MS &&
1291                                 buf_count < IMX091_SIZEOF_I2C_BUF &&
1292                                 n_next->addr != IMX091_TABLE_END)
1293                         continue;
1294
1295                 err = imx091_i2c_wr_blk(info, info->i2c_buf, buf_count);
1296                 if (err)
1297                         return err;
1298
1299                 buf_count = 0;
1300         }
1301
1302         return 0;
1303 }
1304
1305 static void imx091_edp_lowest(struct imx091_info *info)
1306 {
1307         if (!info->edpc)
1308                 return;
1309
1310         info->edp_state = info->edpc->num_states - 1;
1311         dev_dbg(&info->i2c_client->dev, "%s %d\n", __func__, info->edp_state);
1312         if (edp_update_client_request(info->edpc, info->edp_state, NULL)) {
1313                 dev_err(&info->i2c_client->dev, "THIS IS NOT LIKELY HAPPEN!\n");
1314                 dev_err(&info->i2c_client->dev,
1315                         "UNABLE TO SET LOWEST EDP STATE!\n");
1316         }
1317 }
1318
1319 static void imx091_edp_register(struct imx091_info *info)
1320 {
1321         struct edp_manager *edp_manager;
1322         struct edp_client *edpc = &info->pdata->edpc_config;
1323         int ret;
1324
1325         info->edpc = NULL;
1326         if (!edpc->num_states) {
1327                 dev_warn(&info->i2c_client->dev,
1328                         "%s: NO edp states defined.\n", __func__);
1329                 return;
1330         }
1331
1332         strncpy(edpc->name, "imx091", EDP_NAME_LEN - 1);
1333         edpc->name[EDP_NAME_LEN - 1] = 0;
1334         edpc->private_data = info;
1335
1336         dev_dbg(&info->i2c_client->dev, "%s: %s, e0 = %d, p %d\n",
1337                 __func__, edpc->name, edpc->e0_index, edpc->priority);
1338         for (ret = 0; ret < edpc->num_states; ret++)
1339                 dev_dbg(&info->i2c_client->dev, "e%d = %d mA",
1340                         ret - edpc->e0_index, edpc->states[ret]);
1341
1342         edp_manager = edp_get_manager("battery");
1343         if (!edp_manager) {
1344                 dev_err(&info->i2c_client->dev,
1345                         "unable to get edp manager: battery\n");
1346                 return;
1347         }
1348
1349         ret = edp_register_client(edp_manager, edpc);
1350         if (ret) {
1351                 dev_err(&info->i2c_client->dev,
1352                         "unable to register edp client\n");
1353                 return;
1354         }
1355
1356         info->edpc = edpc;
1357         /* set to lowest state at init */
1358         imx091_edp_lowest(info);
1359 }
1360
1361 static int imx091_edp_req(struct imx091_info *info, unsigned new_state)
1362 {
1363         unsigned approved;
1364         int ret = 0;
1365
1366         if (!info->edpc)
1367                 return 0;
1368
1369         dev_dbg(&info->i2c_client->dev, "%s %d\n", __func__, new_state);
1370         ret = edp_update_client_request(info->edpc, new_state, &approved);
1371         if (ret) {
1372                 dev_err(&info->i2c_client->dev, "E state transition failed\n");
1373                 return ret;
1374         }
1375
1376         if (approved > new_state) {
1377                 dev_err(&info->i2c_client->dev, "EDP no enough current\n");
1378                 return -ENODEV;
1379         }
1380
1381         info->edp_state = approved;
1382         return 0;
1383 }
1384
1385 static inline void imx091_frame_length_reg(struct imx091_reg *regs,
1386                                            u32 frame_length)
1387 {
1388         regs->addr = 0x0340;
1389         regs->val = (frame_length >> 8) & 0xFF;
1390         (regs + 1)->addr = 0x0341;
1391         (regs + 1)->val = (frame_length) & 0xFF;
1392 }
1393
1394 static inline void imx091_coarse_time_reg(struct imx091_reg *regs,
1395                                           u32 coarse_time)
1396 {
1397         regs->addr = 0x0202;
1398         regs->val = (coarse_time >> 8) & 0xFF;
1399         (regs + 1)->addr = 0x0203;
1400         (regs + 1)->val = (coarse_time) & 0xFF;
1401 }
1402
1403 static inline void imx091_gain_reg(struct imx091_reg *regs, u32 gain)
1404 {
1405         regs->addr = 0x0205;
1406         regs->val = gain & 0xFF;
1407 }
1408
1409 static int imx091_bin_wr(struct imx091_info *info, u8 enable)
1410 {
1411         int err = 0;
1412
1413         if (enable == info->bin_en)
1414                 return 0;
1415
1416         if (!info->mode_valid || !imx091_mode_table[info->mode_index]->
1417                                   sensor_dnvc.support_bin_control)
1418                 return -EINVAL;
1419
1420         if (!err)
1421                 info->bin_en = enable;
1422         dev_dbg(&info->i2c_client->dev, "%s bin_en=%x err=%d\n",
1423                 __func__, info->bin_en, err);
1424         return err;
1425 }
1426
1427 static int imx091_exposure_wr(struct imx091_info *info,
1428                               struct nvc_imager_bayer *mode)
1429 {
1430         struct imx091_reg reg_list[8];
1431         int err;
1432
1433         reg_list[0].addr = 0x0104;
1434         reg_list[0].val = 0x01;
1435         imx091_frame_length_reg(reg_list+1, mode->frame_length);
1436         imx091_coarse_time_reg(reg_list + 3, mode->coarse_time);
1437         imx091_gain_reg(reg_list + 5, mode->gain);
1438         reg_list[6].addr = 0x0104;
1439         reg_list[6].val = 0x00;
1440         reg_list[7].addr = IMX091_TABLE_END;
1441         err = imx091_i2c_wr_table(info, reg_list);
1442         if (!err)
1443                 err = imx091_bin_wr(info, mode->bin_en);
1444         return err;
1445 }
1446
1447 static int imx091_gain_wr(struct imx091_info *info, u32 gain)
1448 {
1449         int err;
1450
1451         gain &= 0xFF;
1452         err = imx091_i2c_wr16(info->i2c_client, 0x0205, (u16)gain);
1453         return err;
1454 }
1455
1456 static int imx091_gain_rd(struct imx091_info *info, u32 *gain)
1457 {
1458         int err;
1459
1460         *gain = 0;
1461         err = imx091_i2c_rd8(info->i2c_client, 0x0205, (u8 *)gain);
1462         return err;
1463 }
1464
1465 static int imx091_group_hold_wr(struct imx091_info *info,
1466                                 struct nvc_imager_ae *ae)
1467 {
1468         int err;
1469         bool groupHoldEnable;
1470         struct imx091_reg reg_list[6];
1471         int count = 0;
1472
1473         groupHoldEnable = ae->gain_enable |
1474                                         ae->frame_length_enable |
1475                                         ae->coarse_time_enable;
1476
1477         if (groupHoldEnable) {
1478                 err = imx091_i2c_wr8(info->i2c_client, 0x104, 1);
1479                 if (err) {
1480                         dev_err(&info->i2c_client->dev,
1481                                 "Error: %s fail to enable grouphold\n",
1482                                 __func__);
1483                         return err;
1484                 }
1485         }
1486
1487         if (ae->gain_enable) {
1488                 imx091_gain_reg(reg_list + count, ae->gain);
1489                 count += 1;
1490         }
1491         if (ae->coarse_time_enable) {
1492                 imx091_coarse_time_reg(reg_list + count, ae->coarse_time);
1493                 count += 2;
1494         }
1495         if (ae->frame_length_enable) {
1496                 imx091_frame_length_reg(reg_list + count, ae->frame_length);
1497                 count += 2;
1498         }
1499         reg_list[count].addr = IMX091_TABLE_END;
1500         err = imx091_i2c_wr_table(info, reg_list);
1501         if (err) {
1502                 dev_err(&info->i2c_client->dev, "Error: %s i2c wr_table fail\n",
1503                         __func__);
1504         }
1505
1506         if (groupHoldEnable) {
1507                 err = imx091_i2c_wr8(info->i2c_client, 0x104, 0);
1508                 if (err) {
1509                         dev_err(&info->i2c_client->dev,
1510                                 "Error: %s fail to release grouphold\n",
1511                                 __func__);
1512                 }
1513         }
1514         return err;
1515 }
1516
1517 static int imx091_test_pattern_wr(struct imx091_info *info, unsigned pattern)
1518 {
1519         if (pattern >= ARRAY_SIZE(test_patterns))
1520                 return -EINVAL;
1521
1522         return imx091_i2c_wr_table(info, test_patterns[pattern]);
1523 }
1524
1525 static int imx091_set_flash_output(struct imx091_info *info)
1526 {
1527         struct imx091_flash_config *fcfg;
1528         u8 val = 0;
1529         int ret = 0;
1530
1531         if (!info->pdata)
1532                 return 0;
1533
1534         fcfg = &info->pdata->flash_cap;
1535         if (fcfg->xvs_trigger_enabled)
1536                 val |= 0x0c;
1537         if (fcfg->sdo_trigger_enabled)
1538                 val |= 0x02;
1539         dev_dbg(&info->i2c_client->dev, "%s: %02x\n", __func__, val);
1540         /* disable all flash pulse output */
1541         ret = imx091_i2c_wr8(info->i2c_client, 0x304A, 0);
1542         /* config XVS/SDO pin output mode */
1543         ret |= imx091_i2c_wr8(info->i2c_client, 0x3240, val);
1544         /* set the control pulse width settings - Gain + Step
1545          * Pulse width(sec) = 64 * 2^(Gain) * (Step + 1) / Logic Clk
1546          * Logic Clk = ExtClk * PLL Multipiler / Pre_Div / Post_Div
1547          * / Logic Clk Division Ratio
1548          * Logic Clk Division Ratio = 5 @4lane, 10 @2lane, 20 @1lane
1549          */
1550         ret |= imx091_i2c_wr8(info->i2c_client, 0x307C, 0x07);
1551         ret |= imx091_i2c_wr8(info->i2c_client, 0x307D, 0x3F);
1552         return ret;
1553 }
1554
1555 static void imx091_get_flash_cap(struct imx091_info *info)
1556 {
1557         struct nvc_imager_cap *fcap = info->cap;
1558         struct imx091_flash_config *fcfg;
1559
1560         if (!info->pdata)
1561                 return;
1562
1563         fcfg = &info->pdata->flash_cap;
1564         fcap->flash_control_enabled =
1565                 fcfg->xvs_trigger_enabled | fcfg->sdo_trigger_enabled;
1566         fcap->adjustable_flash_timing = fcfg->adjustable_flash_timing;
1567 }
1568
1569 static int imx091_flash_control(
1570         struct imx091_info *info, union nvc_imager_flash_control *fm)
1571 {
1572         int ret;
1573         u8 f_cntl;
1574         u8 f_tim;
1575
1576         if (!info->pdata)
1577                 return -EFAULT;
1578
1579         ret = imx091_i2c_wr8(info->i2c_client, 0x304A, 0);
1580         f_tim = 0;
1581         f_cntl = 0;
1582         if (fm->settings.enable) {
1583                 if (fm->settings.edge_trig_en) {
1584                         f_cntl |= 0x10;
1585                         if (fm->settings.start_edge)
1586                                 f_tim |= 0x08;
1587                         if (fm->settings.repeat)
1588                                 f_tim |= 0x04;
1589                         f_tim |= fm->settings.delay_frm & 0x03;
1590                 } else
1591                         f_cntl |= 0x20;
1592         }
1593         ret |= imx091_i2c_wr8(info->i2c_client, 0x307B, f_tim);
1594         ret |= imx091_i2c_wr8(info->i2c_client, 0x304A, f_cntl);
1595
1596         dev_dbg(&info->i2c_client->dev,
1597                 "%s: %04x %02x %02x\n", __func__, fm->mode, f_tim, f_cntl);
1598         return ret;
1599 }
1600
1601 static int imx091_gpio_rd(struct imx091_info *info,
1602                           enum imx091_gpio i)
1603 {
1604         int val = -EINVAL;
1605
1606         if (info->gpio[i].flag) {
1607                 val = gpio_get_value_cansleep(info->gpio[i].gpio);
1608                 if (val)
1609                         val = 1;
1610                 dev_dbg(&info->i2c_client->dev, "%s %u %d\n",
1611                         __func__, info->gpio[i].gpio, val);
1612                 if (!info->gpio[i].active_high)
1613                         val = !val;
1614                 val &= 1;
1615         }
1616         return val; /* return read value or error */
1617 }
1618
1619 static int imx091_gpio_wr(struct imx091_info *info,
1620                           enum imx091_gpio i,
1621                           int val) /* val: 0=deassert, 1=assert */
1622 {
1623         int err = -EINVAL;
1624
1625         if (info->gpio[i].flag) {
1626                 if (val)
1627                         val = 1;
1628                 if (!info->gpio[i].active_high)
1629                         val = !val;
1630                 val &= 1;
1631                 err = val;
1632                 gpio_set_value_cansleep(info->gpio[i].gpio, val);
1633                 dev_dbg(&info->i2c_client->dev, "%s %u %d\n",
1634                         __func__, info->gpio[i].gpio, val);
1635         }
1636         return err; /* return value written or error */
1637 }
1638
1639 static int imx091_gpio_pwrdn(struct imx091_info *info, int val)
1640 {
1641         int prev_val;
1642
1643         prev_val = imx091_gpio_rd(info, IMX091_GPIO_PWDN);
1644         if (prev_val < 0)
1645                 return 1; /* assume PWRDN hardwired deasserted */
1646
1647         if (val == prev_val)
1648                 return 0; /* no change */
1649
1650         imx091_gpio_wr(info, IMX091_GPIO_PWDN, val);
1651         return 1; /* return state change */
1652 }
1653
1654 static int imx091_gpio_reset(struct imx091_info *info, int val)
1655 {
1656         int err = 0;
1657
1658         if (val) {
1659                 if (!info->reset_flag) {
1660                         info->reset_flag = true;
1661                         err = imx091_gpio_wr(info, IMX091_GPIO_RESET, 1);
1662                         if (err < 0)
1663                                 return 0; /* flag no reset */
1664
1665                         usleep_range(1000, 1500);
1666                         imx091_gpio_wr(info, IMX091_GPIO_RESET, 0);
1667                         msleep(IMX091_STARTUP_DELAY_MS); /* startup delay */
1668                         err = 1; /* flag that a reset was done */
1669                 }
1670         } else {
1671                 info->reset_flag = false;
1672         }
1673         return err;
1674 }
1675
1676 static void imx091_gpio_able(struct imx091_info *info, int val)
1677 {
1678         if (val)
1679                 imx091_gpio_wr(info, IMX091_GPIO_GP1, val);
1680         else
1681                 imx091_gpio_wr(info, IMX091_GPIO_GP1, val);
1682 }
1683
1684 static void imx091_gpio_exit(struct imx091_info *info)
1685 {
1686         unsigned i;
1687
1688         for (i = 0; i < ARRAY_SIZE(imx091_gpios); i++) {
1689                 if (info->gpio[i].flag && info->gpio[i].own) {
1690                         gpio_free(info->gpio[i].gpio);
1691                         info->gpio[i].own = false;
1692                 }
1693         }
1694 }
1695
1696 static void imx091_gpio_init(struct imx091_info *info)
1697 {
1698         char label[32];
1699         unsigned long flags;
1700         unsigned type;
1701         unsigned i;
1702         unsigned j;
1703         int err;
1704
1705         for (i = 0; i < ARRAY_SIZE(imx091_gpios); i++)
1706                 info->gpio[i].flag = false;
1707         if (!info->pdata->gpio_count || !info->pdata->gpio)
1708                 return;
1709
1710         for (i = 0; i < ARRAY_SIZE(imx091_gpios); i++) {
1711                 type = imx091_gpios[i].gpio_type;
1712                 for (j = 0; j < info->pdata->gpio_count; j++) {
1713                         if (type == info->pdata->gpio[j].gpio_type)
1714                                 break;
1715                 }
1716                 if (j == info->pdata->gpio_count)
1717                         continue;
1718
1719                 info->gpio[type].gpio = info->pdata->gpio[j].gpio;
1720                 info->gpio[type].flag = true;
1721                 if (imx091_gpios[i].use_flags) {
1722                         flags = imx091_gpios[i].flags;
1723                         info->gpio[type].active_high =
1724                                                    imx091_gpios[i].active_high;
1725                 } else {
1726                         info->gpio[type].active_high =
1727                                               info->pdata->gpio[j].active_high;
1728                         if (info->gpio[type].active_high)
1729                                 flags = GPIOF_OUT_INIT_LOW;
1730                         else
1731                                 flags = GPIOF_OUT_INIT_HIGH;
1732                 }
1733                 if (!info->pdata->gpio[j].init_en)
1734                         continue;
1735
1736                 snprintf(label, sizeof(label), "imx091_%u_%s",
1737                          info->pdata->num, imx091_gpios[i].label);
1738                 err = gpio_request_one(info->gpio[type].gpio, flags, label);
1739                 if (err) {
1740                         dev_err(&info->i2c_client->dev, "%s ERR %s %u\n",
1741                                 __func__, label, info->gpio[type].gpio);
1742                 } else {
1743                         info->gpio[type].own = true;
1744                         dev_dbg(&info->i2c_client->dev, "%s %s %u\n",
1745                                 __func__, label, info->gpio[type].gpio);
1746                 }
1747         }
1748 }
1749
1750 static int imx091_vreg_dis_all(struct imx091_info *info)
1751 {
1752         if (!info->pdata || !info->pdata->power_off)
1753                 return -EFAULT;
1754
1755         return info->pdata->power_off(info->vreg);
1756 }
1757
1758 static int imx091_vreg_en_all(struct imx091_info *info)
1759 {
1760         if (!info->pdata || !info->pdata->power_on)
1761                 return -EFAULT;
1762
1763         return info->pdata->power_on(info->vreg);
1764 }
1765
1766 static void imx091_vreg_exit(struct imx091_info *info)
1767 {
1768         unsigned i;
1769
1770         for (i = 0; i < ARRAY_SIZE(imx091_vregs); i++) {
1771                 regulator_put(info->vreg[i].vreg);
1772                 info->vreg[i].vreg = NULL;
1773         }
1774 }
1775
1776 static int imx091_vreg_init(struct imx091_info *info)
1777 {
1778         unsigned i;
1779         unsigned j;
1780         int err = 0;
1781
1782         for (i = 0; i < ARRAY_SIZE(imx091_vregs); i++) {
1783                 j = imx091_vregs[i].vreg_num;
1784                 info->vreg[j].vreg_name = imx091_vregs[i].vreg_name;
1785                 info->vreg[j].vreg_flag = false;
1786                 info->vreg[j].vreg = regulator_get(&info->i2c_client->dev,
1787                                                    info->vreg[j].vreg_name);
1788                 if (IS_ERR(info->vreg[j].vreg)) {
1789                         dev_dbg(&info->i2c_client->dev, "%s %s ERR: %d\n",
1790                                 __func__, info->vreg[j].vreg_name,
1791                                 (int)info->vreg[j].vreg);
1792                         err |= PTR_ERR(info->vreg[j].vreg);
1793                         info->vreg[j].vreg = NULL;
1794                 } else {
1795                         dev_dbg(&info->i2c_client->dev, "%s: %s\n",
1796                                 __func__, info->vreg[j].vreg_name);
1797                 }
1798         }
1799         return err;
1800 }
1801
1802 static int imx091_pm_wr(struct imx091_info *info, int pwr)
1803 {
1804         int ret;
1805         int err = 0;
1806
1807         if ((info->pdata->cfg & (NVC_CFG_OFF2STDBY | NVC_CFG_BOOT_INIT)) &&
1808                         (pwr == NVC_PWR_OFF ||
1809                          pwr == NVC_PWR_STDBY_OFF))
1810                 pwr = NVC_PWR_STDBY;
1811         if (pwr == info->pwr_dev)
1812                 return 0;
1813
1814         switch (pwr) {
1815         case NVC_PWR_OFF_FORCE:
1816         case NVC_PWR_OFF:
1817         case NVC_PWR_STDBY_OFF:
1818                 imx091_gpio_pwrdn(info, 1);
1819                 err = imx091_vreg_dis_all(info);
1820                 imx091_gpio_able(info, 0);
1821                 imx091_gpio_reset(info, 0);
1822                 info->mode_valid = false;
1823                 info->bin_en = 0;
1824                 imx091_edp_lowest(info);
1825                 break;
1826
1827         case NVC_PWR_STDBY:
1828                 imx091_gpio_pwrdn(info, 1);
1829                 err = imx091_vreg_en_all(info);
1830                 imx091_gpio_able(info, 1);
1831                 break;
1832
1833         case NVC_PWR_COMM:
1834         case NVC_PWR_ON:
1835                 if (info->pwr_dev != NVC_PWR_ON &&
1836                                 info->pwr_dev != NVC_PWR_COMM)
1837                         imx091_gpio_pwrdn(info, 1);
1838                 err = imx091_vreg_en_all(info);
1839                 imx091_gpio_able(info, 1);
1840                 ret = imx091_gpio_pwrdn(info, 0);
1841                 ret &= !imx091_gpio_reset(info, 1);
1842                 if (ret) /* if no reset && pwrdn changed states then delay */
1843                         msleep(IMX091_STARTUP_DELAY_MS);
1844                 break;
1845
1846         default:
1847                 err = -EINVAL;
1848                 break;
1849         }
1850
1851         if (err < 0) {
1852                 dev_err(&info->i2c_client->dev, "%s err %d\n", __func__, err);
1853                 pwr = NVC_PWR_ERR;
1854         }
1855         info->pwr_dev = pwr;
1856         dev_dbg(&info->i2c_client->dev, "%s pwr_dev=%d\n",
1857                 __func__, info->pwr_dev);
1858         if (err > 0)
1859                 return 0;
1860
1861         return err;
1862 }
1863
1864 static int imx091_pm_wr_s(struct imx091_info *info, int pwr)
1865 {
1866         int err1 = 0;
1867         int err2 = 0;
1868
1869         if ((info->s_mode == NVC_SYNC_OFF) ||
1870                         (info->s_mode == NVC_SYNC_MASTER) ||
1871                         (info->s_mode == NVC_SYNC_STEREO))
1872                 err1 = imx091_pm_wr(info, pwr);
1873         if ((info->s_mode == NVC_SYNC_SLAVE) ||
1874                         (info->s_mode == NVC_SYNC_STEREO))
1875                 err2 = imx091_pm_wr(info->s_info, pwr);
1876         return err1 | err2;
1877 }
1878
1879 static int imx091_pm_api_wr(struct imx091_info *info, int pwr)
1880 {
1881         int err = 0;
1882
1883         if (!pwr || (pwr > NVC_PWR_ON))
1884                 return 0;
1885
1886         if (pwr > info->pwr_dev)
1887                 err = imx091_pm_wr_s(info, pwr);
1888         if (!err)
1889                 info->pwr_api = pwr;
1890         else
1891                 info->pwr_api = NVC_PWR_ERR;
1892         if (info->pdata->cfg & NVC_CFG_NOERR)
1893                 return 0;
1894
1895         return err;
1896 }
1897
1898 static int imx091_pm_dev_wr(struct imx091_info *info, int pwr)
1899 {
1900         if (pwr < info->pwr_api)
1901                 pwr = info->pwr_api;
1902         if (info->mode_enable)
1903                 pwr = NVC_PWR_ON;
1904         return imx091_pm_wr(info, pwr);
1905 }
1906
1907 static void imx091_pm_exit(struct imx091_info *info)
1908 {
1909         imx091_pm_wr(info, NVC_PWR_OFF_FORCE);
1910         imx091_vreg_exit(info);
1911         imx091_gpio_exit(info);
1912 }
1913
1914 static void imx091_pm_init(struct imx091_info *info)
1915 {
1916         imx091_gpio_init(info);
1917         imx091_vreg_init(info);
1918 }
1919
1920 static int imx091_reset(struct imx091_info *info, u32 level)
1921 {
1922         int err;
1923
1924         if (level == NVC_RESET_SOFT) {
1925                 err = imx091_pm_wr(info, NVC_PWR_COMM);
1926                 /* SW reset */
1927                 err |= imx091_i2c_wr8(info->i2c_client, 0x0103, 0x01);
1928         } else {
1929                 err = imx091_pm_wr(info, NVC_PWR_OFF_FORCE);
1930         }
1931         err |= imx091_pm_wr(info, info->pwr_api);
1932         return err;
1933 }
1934
1935 static int imx091_dev_id(struct imx091_info *info)
1936 {
1937         u16 val = 0;
1938         unsigned i;
1939         int err;
1940
1941         dev_dbg(&info->i2c_client->dev, "%s +++++\n",
1942                         __func__);
1943         imx091_pm_dev_wr(info, NVC_PWR_COMM);
1944         dev_dbg(&info->i2c_client->dev, "DUCK:%s:%d\n",
1945                         __func__, __LINE__);
1946         err = imx091_i2c_rd16(info->i2c_client, IMX091_ID_ADDRESS, &val);
1947         if (!err) {
1948                 dev_dbg(&info->i2c_client->dev, "%s found devId: %x\n",
1949                         __func__, val);
1950                 info->sdata.sensor_id_minor = 0;
1951                 for (i = 0; i < ARRAY_SIZE(imx091_ids); i++) {
1952                         if (val == imx091_ids[i]) {
1953                                 info->sdata.sensor_id_minor = val;
1954                                 break;
1955                         }
1956                 }
1957                 if (!info->sdata.sensor_id_minor) {
1958                         err = -ENODEV;
1959                         dev_dbg(&info->i2c_client->dev, "%s No devId match\n",
1960                                 __func__);
1961                 }
1962         }
1963         imx091_pm_dev_wr(info, NVC_PWR_OFF);
1964         dev_dbg(&info->i2c_client->dev, "%s -----\n",
1965                         __func__);
1966         return err;
1967 }
1968
1969 static int imx091_mode_able(struct imx091_info *info, bool mode_enable)
1970 {
1971         u8 val;
1972         int err;
1973
1974         if (mode_enable)
1975                 val = IMX091_STREAM_ENABLE;
1976         else
1977                 val = IMX091_STREAM_DISABLE;
1978         err = imx091_i2c_wr8(info->i2c_client, IMX091_STREAM_CONTROL_REG, val);
1979         if (!err) {
1980                 info->mode_enable = mode_enable;
1981                 dev_dbg(&info->i2c_client->dev, "%s streaming=%x\n",
1982                         __func__, info->mode_enable);
1983                 if (!mode_enable)
1984                         imx091_pm_dev_wr(info, NVC_PWR_OFF);
1985         }
1986         msleep(IMX091_WAIT_MS);
1987         return err;
1988 }
1989
1990 static int imx091_mode_rd(struct imx091_info *info,
1991                           s32 res_x,
1992                           s32 res_y,
1993                           u32 *index)
1994 {
1995         int i;
1996
1997         if (!res_x && !res_y) {
1998                 *index = info->cap->preferred_mode_index;
1999                 return 0;
2000         }
2001
2002         for (i = 0; i < IMX091_NUM_MODES; i++) {
2003                 if ((res_x == imx091_mode_table[i]->sensor_mode.res_x) &&
2004                     (res_y == imx091_mode_table[i]->sensor_mode.res_y)) {
2005                         break;
2006                 }
2007         }
2008
2009         if (i == IMX091_NUM_MODES) {
2010                 dev_err(&info->i2c_client->dev,
2011                         "%s invalid resolution: %dx%d\n",
2012                         __func__, res_x, res_y);
2013                 return -EINVAL;
2014         }
2015
2016         *index = i;
2017         return 0;
2018 }
2019
2020 static int imx091_mode_wr_full(struct imx091_info *info, u32 mode_index)
2021 {
2022         int err;
2023
2024         /* the state num is temporary assigned, should be updated later as
2025         per-mode basis */
2026         err = imx091_edp_req(info, 0);
2027         if (err) {
2028                 dev_err(&info->i2c_client->dev,
2029                         "%s: ERROR cannot set edp state! %d\n", __func__, err);
2030                 goto mode_wr_full_end;
2031         }
2032
2033         imx091_pm_dev_wr(info, NVC_PWR_ON);
2034         imx091_bin_wr(info, 0);
2035         err = imx091_i2c_wr_table(info,
2036                                   imx091_mode_table[mode_index]->p_mode_i2c);
2037         if (!err) {
2038                 info->mode_index = mode_index;
2039                 info->mode_valid = true;
2040         } else {
2041                 info->mode_valid = false;
2042         }
2043
2044 mode_wr_full_end:
2045         return err;
2046 }
2047
2048 static int imx091_mode_wr(struct imx091_info *info,
2049                           struct nvc_imager_bayer *mode)
2050 {
2051         u32 mode_index;
2052         int err;
2053
2054         err = imx091_mode_rd(info, mode->res_x, mode->res_y, &mode_index);
2055         if (err < 0)
2056                 return err;
2057
2058         if (!mode->res_x && !mode->res_y) {
2059                 if (mode->frame_length || mode->coarse_time || mode->gain) {
2060                         /* write exposure only */
2061                         err = imx091_exposure_wr(info, mode);
2062                         return err;
2063                 } else {
2064                         /* turn off streaming */
2065                         err = imx091_mode_able(info, false);
2066                         return err;
2067                 }
2068         }
2069
2070         if (!info->mode_valid || (info->mode_index != mode_index))
2071                 err = imx091_mode_wr_full(info, mode_index);
2072         else
2073                 dev_dbg(&info->i2c_client->dev, "%s short mode\n", __func__);
2074         err |= imx091_exposure_wr(info, mode);
2075         if (err < 0) {
2076                 info->mode_valid = false;
2077                 goto imx091_mode_wr_err;
2078         }
2079
2080         err = imx091_set_flash_output(info);
2081
2082         err |= imx091_mode_able(info, true);
2083         if (err < 0)
2084                 goto imx091_mode_wr_err;
2085
2086         return 0;
2087
2088 imx091_mode_wr_err:
2089         if (!info->mode_enable)
2090                 imx091_pm_dev_wr(info, NVC_PWR_STDBY);
2091         return err;
2092 }
2093
2094
2095 static int imx091_param_rd(struct imx091_info *info, unsigned long arg)
2096 {
2097         struct nvc_param params;
2098         struct imx091_reg *p_i2c_table;
2099         const void *data_ptr;
2100         u32 data_size = 0;
2101         u32 u32val;
2102         int err;
2103
2104         if (copy_from_user(&params,
2105                            (const void __user *)arg,
2106                            sizeof(struct nvc_param))) {
2107                 dev_err(&info->i2c_client->dev,
2108                         "%s copy_from_user err line %d\n", __func__, __LINE__);
2109                 return -EFAULT;
2110         }
2111
2112         if (info->s_mode == NVC_SYNC_SLAVE)
2113                 info = info->s_info;
2114
2115         switch (params.param) {
2116         case NVC_PARAM_GAIN:
2117                 imx091_pm_dev_wr(info, NVC_PWR_COMM);
2118                 err = imx091_gain_rd(info, &u32val);
2119                 imx091_pm_dev_wr(info, NVC_PWR_OFF);
2120                 dev_dbg(&info->i2c_client->dev, "%s GAIN: %u err: %d\n",
2121                         __func__, u32val, err);
2122                 if (err)
2123                         return err;
2124
2125                 data_ptr = &u32val;
2126                 data_size = sizeof(u32val);
2127                 break;
2128
2129         case NVC_PARAM_STEREO_CAP:
2130                 if (info->s_info != NULL)
2131                         err = 0;
2132                 else
2133                         err = -ENODEV;
2134                 dev_dbg(&info->i2c_client->dev, "%s STEREO_CAP: %d\n",
2135                         __func__, err);
2136                 data_ptr = &err;
2137                 data_size = sizeof(err);
2138                 break;
2139
2140         case NVC_PARAM_STEREO:
2141                 dev_dbg(&info->i2c_client->dev, "%s STEREO: %d\n",
2142                         __func__, info->s_mode);
2143                 data_ptr = &info->s_mode;
2144                 data_size = sizeof(info->s_mode);
2145                 break;
2146
2147         case NVC_PARAM_STS:
2148                 err = imx091_dev_id(info);
2149                 dev_dbg(&info->i2c_client->dev, "%s STS: %d\n",
2150                         __func__, err);
2151                 data_ptr = &err;
2152                 data_size = sizeof(err);
2153                 break;
2154
2155         case NVC_PARAM_DEV_ID:
2156                 if (!info->sdata.sensor_id_minor)
2157                         imx091_dev_id(info);
2158                 data_ptr = &info->sdata.sensor_id;
2159                 data_size = sizeof(info->sdata.sensor_id) * 2;
2160                 dev_dbg(&info->i2c_client->dev, "%s DEV_ID: %x-%x\n",
2161                         __func__, info->sdata.sensor_id,
2162                         info->sdata.sensor_id_minor);
2163                 break;
2164
2165         case NVC_PARAM_SENSOR_TYPE:
2166                 data_ptr = &info->sdata.sensor_type;
2167                 data_size = sizeof(info->sdata.sensor_type);
2168                 dev_dbg(&info->i2c_client->dev, "%s SENSOR_TYPE: %d\n",
2169                         __func__, info->sdata.sensor_type);
2170                 break;
2171
2172         case NVC_PARAM_FOCAL_LEN:
2173                 data_ptr = &info->sdata.focal_len;
2174                 data_size = sizeof(info->sdata.focal_len);
2175                 dev_dbg(&info->i2c_client->dev, "%s FOCAL_LEN: %u\n",
2176                         __func__, info->sdata.focal_len);
2177                 break;
2178
2179         case NVC_PARAM_MAX_APERTURE:
2180                 data_ptr = &info->sdata.max_aperture;
2181                 data_size = sizeof(info->sdata.max_aperture);
2182                 dev_dbg(&info->i2c_client->dev, "%s MAX_APERTURE: %u\n",
2183                         __func__, info->sdata.max_aperture);
2184                 break;
2185
2186         case NVC_PARAM_FNUMBER:
2187                 data_ptr = &info->sdata.fnumber;
2188                 data_size = sizeof(info->sdata.fnumber);
2189                 dev_dbg(&info->i2c_client->dev, "%s FNUMBER: %u\n",
2190                         __func__, info->sdata.fnumber);
2191                 break;
2192
2193         case NVC_PARAM_VIEW_ANGLE_H:
2194                 data_ptr = &info->sdata.view_angle_h;
2195                 data_size = sizeof(info->sdata.view_angle_h);
2196                 dev_dbg(&info->i2c_client->dev, "%s VIEW_ANGLE_H: %u\n",
2197                         __func__, info->sdata.view_angle_h);
2198                 break;
2199
2200         case NVC_PARAM_VIEW_ANGLE_V:
2201                 data_ptr = &info->sdata.view_angle_v;
2202                 data_size = sizeof(info->sdata.view_angle_v);
2203                 dev_dbg(&info->i2c_client->dev, "%s VIEW_ANGLE_V: %u\n",
2204                         __func__, info->sdata.view_angle_v);
2205                 break;
2206
2207         case NVC_PARAM_I2C:
2208                 dev_dbg(&info->i2c_client->dev, "%s I2C\n", __func__);
2209                 if (params.sizeofvalue > IMX091_I2C_TABLE_MAX_ENTRIES) {
2210                         dev_err(&info->i2c_client->dev,
2211                                 "%s NVC_PARAM_I2C request size too large\n",
2212                                 __func__);
2213                         return -EINVAL;
2214                 }
2215                 p_i2c_table = kzalloc(sizeof(params.sizeofvalue), GFP_KERNEL);
2216                 if (p_i2c_table == NULL) {
2217                         pr_err("%s: kzalloc error\n", __func__);
2218                         return -ENOMEM;
2219                 }
2220
2221                 if (copy_from_user(p_i2c_table,
2222                                    (const void __user *)params.p_value,
2223                                    params.sizeofvalue)) {
2224                         dev_err(&info->i2c_client->dev,
2225                                 "%s copy_from_user err line %d\n",
2226                                 __func__, __LINE__);
2227                         kfree(p_i2c_table);
2228                         return -EINVAL;
2229                 }
2230
2231                 imx091_pm_dev_wr(info, NVC_PWR_COMM);
2232                 err = imx091_i2c_rd_table(info, p_i2c_table);
2233                 imx091_pm_dev_wr(info, NVC_PWR_OFF);
2234                 if (copy_to_user((void __user *)params.p_value,
2235                                  p_i2c_table,
2236                                  params.sizeofvalue)) {
2237                         dev_err(&info->i2c_client->dev,
2238                                 "%s copy_to_user err line %d\n",
2239                                 __func__, __LINE__);
2240                         err = -EINVAL;
2241                 }
2242                 kfree(p_i2c_table);
2243                 return err;
2244         default:
2245                 dev_dbg(&info->i2c_client->dev,
2246                         "%s unsupported parameter: %d\n",
2247                         __func__, params.param);
2248                 return -EINVAL;
2249         }
2250
2251         if (params.sizeofvalue < data_size) {
2252                 dev_err(&info->i2c_client->dev,
2253                         "%s data size mismatch %d != %d Param: %d\n",
2254                         __func__, params.sizeofvalue, data_size, params.param);
2255                 return -EINVAL;
2256         }
2257
2258         if (copy_to_user((void __user *)params.p_value,
2259                          data_ptr,
2260                          data_size)) {
2261                 dev_err(&info->i2c_client->dev,
2262                         "%s copy_to_user err line %d\n", __func__, __LINE__);
2263                 return -EFAULT;
2264         }
2265
2266         return 0;
2267 }
2268
2269 static int imx091_param_wr_s(struct imx091_info *info,
2270                              struct nvc_param *params,
2271                              u32 u32val)
2272 {
2273         struct imx091_reg *p_i2c_table;
2274         u8 u8val;
2275         int err;
2276
2277         u8val = (u8)u32val;
2278         switch (params->param) {
2279         case NVC_PARAM_GAIN:
2280                 dev_dbg(&info->i2c_client->dev, "%s GAIN: %u\n",
2281                         __func__, u32val);
2282                 imx091_pm_dev_wr(info, NVC_PWR_COMM);
2283                 err = imx091_gain_wr(info, u32val);
2284                 if (err) {
2285                         dev_err(&info->i2c_client->dev, "Error: %s SET GAIN ERR",
2286                                                         __func__);
2287                 }
2288                 imx091_pm_dev_wr(info, NVC_PWR_STDBY);
2289                 return err;
2290
2291         case NVC_PARAM_RESET:
2292                 err = imx091_reset(info, u32val);
2293                 dev_dbg(&info->i2c_client->dev, "%s RESET=%d err=%d\n",
2294                         __func__, u32val, err);
2295                 return err;
2296
2297         case NVC_PARAM_TESTMODE:
2298                 dev_dbg(&info->i2c_client->dev, "%s TESTMODE: %u\n",
2299                         __func__, (unsigned)u8val);
2300                 if (u8val)
2301                         u32val = info->test_pattern;
2302                 else
2303                         u32val = 0;
2304                 imx091_pm_dev_wr(info, NVC_PWR_ON);
2305                 err = imx091_test_pattern_wr(info, u32val);
2306                 if (!u8val)
2307                         imx091_pm_dev_wr(info, NVC_PWR_OFF);
2308                 return err;
2309
2310         case NVC_PARAM_TEST_PATTERN:
2311                 dev_dbg(&info->i2c_client->dev, "%s TEST_PATTERN: %d\n",
2312                         __func__, u32val);
2313                 info->test_pattern = u32val;
2314                 return 0;
2315
2316         case NVC_PARAM_SELF_TEST:
2317                 err = imx091_dev_id(info);
2318                 dev_dbg(&info->i2c_client->dev, "%s SELF_TEST: %d\n",
2319                         __func__, err);
2320                 return err;
2321
2322         case NVC_PARAM_I2C:
2323                 dev_dbg(&info->i2c_client->dev, "%s I2C\n", __func__);
2324                 if (params->sizeofvalue > IMX091_I2C_TABLE_MAX_ENTRIES) {
2325                         dev_err(&info->i2c_client->dev,
2326                                 "%s NVC_PARAM_I2C request size too large\n",
2327                                 __func__);
2328                         return -EINVAL;
2329                 }
2330                 p_i2c_table = kzalloc(sizeof(params->sizeofvalue), GFP_KERNEL);
2331                 if (p_i2c_table == NULL) {
2332                         dev_err(&info->i2c_client->dev,
2333                                 "%s kzalloc err line %d\n",
2334                                 __func__, __LINE__);
2335                         return -ENOMEM;
2336                 }
2337
2338                 if (copy_from_user(p_i2c_table,
2339                                    (const void __user *)params->p_value,
2340                                    params->sizeofvalue)) {
2341                         dev_err(&info->i2c_client->dev,
2342                                 "%s copy_from_user err line %d\n",
2343                                 __func__, __LINE__);
2344                         kfree(p_i2c_table);
2345                         return -EFAULT;
2346                 }
2347
2348                 imx091_pm_dev_wr(info, NVC_PWR_ON);
2349                 err = imx091_i2c_wr_table(info, p_i2c_table);
2350                 kfree(p_i2c_table);
2351                 return err;
2352
2353         case NVC_PARAM_SET_SENSOR_FLASH_MODE:
2354         {
2355                 union nvc_imager_flash_control fm;
2356                 if (copy_from_user(&fm,
2357                         (const void __user *)params->p_value, sizeof(fm))) {
2358                         pr_info("%s:fail set flash mode.\n", __func__);
2359                         return -EFAULT;
2360                 }
2361                 return imx091_flash_control(info, &fm);
2362         }
2363
2364         default:
2365                 dev_dbg(&info->i2c_client->dev,
2366                         "%s unsupported parameter: %d\n",
2367                         __func__, params->param);
2368                 return -EINVAL;
2369         }
2370 }
2371
2372 static int imx091_param_wr(struct imx091_info *info, unsigned long arg)
2373 {
2374         struct nvc_param params;
2375         u8 u8val;
2376         u32 u32val;
2377         int err = 0;
2378
2379         if (copy_from_user(&params, (const void __user *)arg,
2380                            sizeof(struct nvc_param))) {
2381                 dev_err(&info->i2c_client->dev,
2382                         "%s copy_from_user err line %d\n", __func__, __LINE__);
2383                 return -EFAULT;
2384         }
2385
2386         if (copy_from_user(&u32val, (const void __user *)params.p_value,
2387                            sizeof(u32val))) {
2388                 dev_err(&info->i2c_client->dev, "%s %d copy_from_user err\n",
2389                         __func__, __LINE__);
2390                 return -EFAULT;
2391         }
2392
2393         u8val = (u8)u32val;
2394         /* parameters independent of sync mode */
2395         switch (params.param) {
2396         case NVC_PARAM_STEREO:
2397                 dev_dbg(&info->i2c_client->dev, "%s STEREO: %d\n",
2398                         __func__, u8val);
2399                 if (u8val == info->s_mode)
2400                         return 0;
2401
2402                 switch (u8val) {
2403                 case NVC_SYNC_OFF:
2404                         info->s_mode = u8val;
2405                         if (info->s_info != NULL) {
2406                                 info->s_info->s_mode = u8val;
2407                                 imx091_pm_wr(info->s_info, NVC_PWR_OFF);
2408                         }
2409                         break;
2410
2411                 case NVC_SYNC_MASTER:
2412                         info->s_mode = u8val;
2413                         if (info->s_info != NULL)
2414                                 info->s_info->s_mode = u8val;
2415                         break;
2416
2417                 case NVC_SYNC_SLAVE:
2418                         if (info->s_info != NULL) {
2419                                 /* sync power */
2420                                 info->s_info->pwr_api = info->pwr_api;
2421                                 err = imx091_pm_wr(info->s_info,
2422                                                    info->pwr_dev);
2423                                 if (!err) {
2424                                         info->s_mode = u8val;
2425                                         info->s_info->s_mode = u8val;
2426                                 } else {
2427                                         if (info->s_mode != NVC_SYNC_STEREO)
2428                                                 imx091_pm_wr(info->s_info,
2429                                                              NVC_PWR_OFF);
2430                                         err = -EIO;
2431                                 }
2432                         } else {
2433                                 err = -EINVAL;
2434                         }
2435                         break;
2436
2437                 case NVC_SYNC_STEREO:
2438                         if (info->s_info != NULL) {
2439                                 /* sync power */
2440                                 info->s_info->pwr_api = info->pwr_api;
2441                                 err = imx091_pm_wr(info->s_info,
2442                                                    info->pwr_dev);
2443                                 if (!err) {
2444                                         info->s_mode = u8val;
2445                                         info->s_info->s_mode = u8val;
2446                                 } else {
2447                                         if (info->s_mode != NVC_SYNC_SLAVE)
2448                                                 imx091_pm_wr(info->s_info,
2449                                                              NVC_PWR_OFF);
2450                                         err = -EIO;
2451                                 }
2452                         } else {
2453                                 err = -EINVAL;
2454                         }
2455                         break;
2456
2457                 default:
2458                         err = -EINVAL;
2459                 }
2460                 if (info->pdata->cfg & NVC_CFG_NOERR)
2461                         return 0;
2462
2463                 return err;
2464
2465         case NVC_PARAM_GROUP_HOLD:
2466         {
2467                 struct nvc_imager_ae ae;
2468                 dev_dbg(&info->i2c_client->dev, "%s GROUP_HOLD\n",
2469                         __func__);
2470                 if (copy_from_user(&ae, (const void __user *)params.p_value,
2471                                 sizeof(struct nvc_imager_ae))) {
2472                         dev_err(&info->i2c_client->dev, "Error: %s %d copy_from_user err\n",
2473                                 __func__, __LINE__);
2474                         return -EFAULT;
2475                 }
2476                 imx091_pm_dev_wr(info, NVC_PWR_COMM);
2477                 err = imx091_group_hold_wr(info, &ae);
2478                 imx091_pm_dev_wr(info, NVC_PWR_STDBY);
2479                 return err;
2480         }
2481
2482         default:
2483         /* parameters dependent on sync mode */
2484                 switch (info->s_mode) {
2485                 case NVC_SYNC_OFF:
2486                 case NVC_SYNC_MASTER:
2487                         return imx091_param_wr_s(info, &params, u32val);
2488
2489                 case NVC_SYNC_SLAVE:
2490                         return imx091_param_wr_s(info->s_info, &params,
2491                                                  u32val);
2492
2493                 case NVC_SYNC_STEREO:
2494                         err = imx091_param_wr_s(info, &params, u32val);
2495                         if (!(info->pdata->cfg & NVC_CFG_SYNC_I2C_MUX))
2496                                 err |= imx091_param_wr_s(info->s_info,
2497                                                          &params, u32val);
2498                         return err;
2499
2500                 default:
2501                         dev_err(&info->i2c_client->dev, "%s %d internal err\n",
2502                                 __func__, __LINE__);
2503                         return -EINVAL;
2504                 }
2505         }
2506 }
2507
2508 static long imx091_ioctl(struct file *file,
2509                          unsigned int cmd,
2510                          unsigned long arg)
2511 {
2512         struct imx091_info *info = file->private_data;
2513         struct nvc_imager_bayer mode;
2514         struct nvc_imager_mode_list mode_list;
2515         struct nvc_imager_mode mode_table[IMX091_NUM_MODES];
2516         struct nvc_imager_dnvc dnvc;
2517         const void *data_ptr;
2518         s32 num_modes;
2519         u32 i;
2520         int pwr;
2521         int err;
2522
2523         switch (cmd) {
2524         case NVC_IOCTL_PARAM_WR:
2525                 err = imx091_param_wr(info, arg);
2526                 return err;
2527
2528         case NVC_IOCTL_PARAM_RD:
2529                 err = imx091_param_rd(info, arg);
2530                 return err;
2531
2532         case NVC_IOCTL_DYNAMIC_RD:
2533                 if (copy_from_user(&dnvc, (const void __user *)arg,
2534                                    sizeof(struct nvc_imager_dnvc))) {
2535                         dev_err(&info->i2c_client->dev,
2536                                 "%s copy_from_user err line %d\n",
2537                                 __func__, __LINE__);
2538                         return -EFAULT;
2539                 }
2540
2541                 dev_dbg(&info->i2c_client->dev, "%s DYNAMIC_RD x=%d y=%d\n",
2542                         __func__, dnvc.res_x, dnvc.res_y);
2543                 err = imx091_mode_rd(info, dnvc.res_x, dnvc.res_y, &i);
2544                 if (err)
2545                         return -EINVAL;
2546
2547                 if (dnvc.p_mode) {
2548                         if (copy_to_user((void __user *)dnvc.p_mode,
2549                                          &imx091_mode_table[i]->sensor_mode,
2550                                          sizeof(struct nvc_imager_mode))) {
2551                                 dev_err(&info->i2c_client->dev,
2552                                         "%s copy_to_user err line %d\n",
2553                                         __func__, __LINE__);
2554                                 return -EFAULT;
2555                         }
2556                 }
2557
2558                 if (dnvc.p_dnvc) {
2559                         if (copy_to_user((void __user *)dnvc.p_dnvc,
2560                                       &imx091_mode_table[i]->sensor_dnvc,
2561                                       sizeof(struct nvc_imager_dynamic_nvc))) {
2562                                 dev_err(&info->i2c_client->dev,
2563                                         "%s copy_to_user err line %d\n",
2564                                         __func__, __LINE__);
2565                                 return -EFAULT;
2566                         }
2567                 }
2568
2569                 return 0;
2570
2571         case NVC_IOCTL_MODE_WR:
2572                 if (copy_from_user(&mode, (const void __user *)arg,
2573                                    sizeof(struct nvc_imager_bayer))) {
2574                         dev_err(&info->i2c_client->dev,
2575                                 "%s copy_from_user err line %d\n",
2576                                 __func__, __LINE__);
2577                         return -EFAULT;
2578                 }
2579
2580                 dev_info(&info->i2c_client->dev,
2581                         "%s MODE_WR x=%d y=%d coarse=%u frame=%u gain=%u\n",
2582                         __func__, mode.res_x, mode.res_y,
2583                         mode.coarse_time, mode.frame_length, mode.gain);
2584                 err = imx091_mode_wr(info, &mode);
2585                 return err;
2586
2587         case NVC_IOCTL_MODE_RD:
2588                 /*
2589                  * Return a list of modes that sensor bayer supports.
2590                  * If called with a NULL ptr to pModes,
2591                  * then it just returns the count.
2592                  */
2593                 dev_dbg(&info->i2c_client->dev, "%s MODE_RD n=%d\n",
2594                         __func__, IMX091_NUM_MODES);
2595                 if (copy_from_user(&mode_list, (const void __user *)arg,
2596                                    sizeof(struct nvc_imager_mode_list))) {
2597                         dev_err(&info->i2c_client->dev,
2598                                 "%s copy_from_user err line %d\n",
2599                                 __func__, __LINE__);
2600                         return -EFAULT;
2601                 }
2602
2603                 num_modes = IMX091_NUM_MODES;
2604                 if (mode_list.p_num_mode != NULL) {
2605                         if (copy_to_user((void __user *)mode_list.p_num_mode,
2606                                          &num_modes, sizeof(num_modes))) {
2607                                 dev_err(&info->i2c_client->dev,
2608                                         "%s copy_to_user err line %d\n",
2609                                         __func__, __LINE__);
2610                                 return -EFAULT;
2611                         }
2612                 }
2613
2614                 if (mode_list.p_modes != NULL) {
2615                         for (i = 0; i < IMX091_NUM_MODES; i++) {
2616                                 mode_table[i] =
2617                                              imx091_mode_table[i]->sensor_mode;
2618                         }
2619                         if (copy_to_user((void __user *)mode_list.p_modes,
2620                                          (const void *)&mode_table,
2621                                          sizeof(mode_table))) {
2622                                 dev_err(&info->i2c_client->dev,
2623                                         "%s copy_to_user err line %d\n",
2624                                         __func__, __LINE__);
2625                                 return -EFAULT;
2626                         }
2627                 }
2628
2629                 return 0;
2630
2631         case NVC_IOCTL_PWR_WR:
2632                 /* This is a Guaranteed Level of Service (GLOS) call */
2633                 pwr = (int)arg * 2;
2634                 dev_dbg(&info->i2c_client->dev, "%s PWR_WR: %d\n",
2635                         __func__, pwr);
2636                 err = imx091_pm_api_wr(info, pwr);
2637                 return err;
2638
2639         case NVC_IOCTL_PWR_RD:
2640                 if (info->s_mode == NVC_SYNC_SLAVE)
2641                         pwr = info->s_info->pwr_api / 2;
2642                 else
2643                         pwr = info->pwr_api / 2;
2644                 dev_dbg(&info->i2c_client->dev, "%s PWR_RD: %d\n",
2645                         __func__, pwr);
2646                 if (copy_to_user((void __user *)arg, (const void *)&pwr,
2647                                  sizeof(pwr))) {
2648                         dev_err(&info->i2c_client->dev,
2649                                         "%s copy_to_user err line %d\n",
2650                                         __func__, __LINE__);
2651                         return -EFAULT;
2652                 }
2653
2654                 return 0;
2655
2656         case NVC_IOCTL_CAPS_RD:
2657                 dev_dbg(&info->i2c_client->dev, "%s CAPS_RD n=%d\n",
2658                         __func__, sizeof(imx091_dflt_cap));
2659                 data_ptr = info->cap;
2660                 if (copy_to_user((void __user *)arg,
2661                                  data_ptr,
2662                                  sizeof(imx091_dflt_cap))) {
2663                         dev_err(&info->i2c_client->dev,
2664                                 "%s copy_to_user err line %d\n",
2665                                 __func__, __LINE__);
2666                         return -EFAULT;
2667                 }
2668
2669                 return 0;
2670
2671         case NVC_IOCTL_STATIC_RD:
2672                 dev_dbg(&info->i2c_client->dev, "%s STATIC_RD n=%d\n",
2673                         __func__, sizeof(struct nvc_imager_static_nvc));
2674                 data_ptr = &info->sdata;
2675                 if (copy_to_user((void __user *)arg,
2676                                  data_ptr,
2677                                  sizeof(struct nvc_imager_static_nvc))) {
2678                         dev_err(&info->i2c_client->dev,
2679                                 "%s copy_to_user err line %d\n",
2680                                 __func__, __LINE__);
2681                         return -EFAULT;
2682                 }
2683
2684                 return 0;
2685
2686         default:
2687                 dev_dbg(&info->i2c_client->dev, "%s unsupported ioctl: %x\n",
2688                         __func__, cmd);
2689         }
2690
2691         return -EINVAL;
2692 }
2693
2694 static void imx091_sdata_init(struct imx091_info *info)
2695 {
2696         if (info->pdata->cap)
2697                 info->cap = info->pdata->cap;
2698         else
2699                 info->cap = &imx091_dflt_cap;
2700         memcpy(&info->sdata, &imx091_dflt_sdata, sizeof(info->sdata));
2701         if (info->pdata->lens_focal_length)
2702                 info->sdata.focal_len = info->pdata->lens_focal_length;
2703         if (info->pdata->lens_max_aperture)
2704                 info->sdata.max_aperture = info->pdata->lens_max_aperture;
2705         if (info->pdata->lens_fnumber)
2706                 info->sdata.fnumber = info->pdata->lens_fnumber;
2707         if (info->pdata->lens_view_angle_h)
2708                 info->sdata.view_angle_h = info->pdata->lens_view_angle_h;
2709         if (info->pdata->lens_view_angle_v)
2710                 info->sdata.view_angle_v = info->pdata->lens_view_angle_v;
2711 }
2712
2713 static int imx091_sync_en(unsigned num, unsigned sync)
2714 {
2715         struct imx091_info *master = NULL;
2716         struct imx091_info *slave = NULL;
2717         struct imx091_info *pos = NULL;
2718
2719         rcu_read_lock();
2720         list_for_each_entry_rcu(pos, &imx091_info_list, list) {
2721                 if (pos->pdata->num == num) {
2722                         master = pos;
2723                         break;
2724                 }
2725         }
2726         pos = NULL;
2727         list_for_each_entry_rcu(pos, &imx091_info_list, list) {
2728                 if (pos->pdata->num == sync) {
2729                         slave = pos;
2730                         break;
2731                 }
2732         }
2733         rcu_read_unlock();
2734         if (master != NULL)
2735                 master->s_info = NULL;
2736         if (slave != NULL)
2737                 slave->s_info = NULL;
2738         if (!sync)
2739                 return 0; /* no err if sync disabled */
2740
2741         if (num == sync)
2742                 return -EINVAL; /* err if sync instance is itself */
2743
2744         if ((master != NULL) && (slave != NULL)) {
2745                 master->s_info = slave;
2746                 slave->s_info = master;
2747         }
2748         return 0;
2749 }
2750
2751 static int imx091_sync_dis(struct imx091_info *info)
2752 {
2753         if (info->s_info != NULL) {
2754                 info->s_info->s_mode = 0;
2755                 info->s_info->s_info = NULL;
2756                 info->s_mode = 0;
2757                 info->s_info = NULL;
2758                 return 0;
2759         }
2760
2761         return -EINVAL;
2762 }
2763
2764 static int imx091_open(struct inode *inode, struct file *file)
2765 {
2766         struct imx091_info *info = NULL;
2767         struct imx091_info *pos = NULL;
2768         int err;
2769
2770
2771         rcu_read_lock();
2772         list_for_each_entry_rcu(pos, &imx091_info_list, list) {
2773                 if (pos->miscdev.minor == iminor(inode)) {
2774                         info = pos;
2775                         break;
2776                 }
2777         }
2778         rcu_read_unlock();
2779         if (!info) {
2780                 pr_err("%s err @%d info is null\n", __func__, __LINE__);
2781                 return -ENODEV;
2782         }
2783
2784         dev_dbg(&info->i2c_client->dev, "%s +++++\n", __func__);
2785         err = imx091_sync_en(info->pdata->num, info->pdata->sync);
2786         if (err == -EINVAL)
2787                 dev_err(&info->i2c_client->dev,
2788                         "%s err: invalid num (%u) and sync (%u) instance\n",
2789                         __func__, info->pdata->num, info->pdata->sync);
2790         if (atomic_xchg(&info->in_use, 1)) {
2791                 dev_err(&info->i2c_client->dev, "%s err @%d device is busy\n",
2792                         __func__, __LINE__);
2793                 return -EBUSY;
2794         }
2795         if (info->s_info != NULL) {
2796                 if (atomic_xchg(&info->s_info->in_use, 1)) {
2797                         dev_err(&info->i2c_client->dev, "%s err @%d sync device is busy\n",
2798                                         __func__, __LINE__);
2799                         return -EBUSY;
2800                 }
2801                 info->sdata.stereo_cap = 1;
2802         }
2803
2804         file->private_data = info;
2805         dev_dbg(&info->i2c_client->dev, "%s -----\n", __func__);
2806         return 0;
2807 }
2808
2809 static int imx091_release(struct inode *inode, struct file *file)
2810 {
2811         struct imx091_info *info = file->private_data;
2812
2813         dev_dbg(&info->i2c_client->dev, "%s +++++\n", __func__);
2814         imx091_pm_wr_s(info, NVC_PWR_OFF);
2815         file->private_data = NULL;
2816         WARN_ON(!atomic_xchg(&info->in_use, 0));
2817         if (info->s_info != NULL)
2818                 WARN_ON(!atomic_xchg(&info->s_info->in_use, 0));
2819         imx091_sync_dis(info);
2820         dev_dbg(&info->i2c_client->dev, "%s -----\n", __func__);
2821         return 0;
2822 }
2823
2824 static const struct file_operations imx091_fileops = {
2825         .owner = THIS_MODULE,
2826         .open = imx091_open,
2827         .unlocked_ioctl = imx091_ioctl,
2828         .release = imx091_release,
2829 };
2830
2831 static void imx091_del(struct imx091_info *info)
2832 {
2833         imx091_pm_exit(info);
2834         if ((info->s_mode == NVC_SYNC_SLAVE) ||
2835                                              (info->s_mode == NVC_SYNC_STEREO))
2836                 imx091_pm_exit(info->s_info);
2837         imx091_sync_dis(info);
2838         spin_lock(&imx091_spinlock);
2839         list_del_rcu(&info->list);
2840         spin_unlock(&imx091_spinlock);
2841         synchronize_rcu();
2842 }
2843
2844 static int imx091_remove(struct i2c_client *client)
2845 {
2846         struct imx091_info *info = i2c_get_clientdata(client);
2847
2848         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
2849 #ifdef CONFIG_DEBUG_FS
2850         nvc_debugfs_remove(&info->debugfs_info);
2851 #endif
2852         misc_deregister(&info->miscdev);
2853         imx091_del(info);
2854         return 0;
2855 }
2856
2857 static int imx091_probe(
2858         struct i2c_client *client,
2859         const struct i2c_device_id *id)
2860 {
2861         struct imx091_info *info;
2862         char dname[16];
2863         unsigned long clock_probe_rate;
2864         int err;
2865
2866         dev_dbg(&client->dev, "%s +++++\n", __func__);
2867         info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
2868         if (info == NULL) {
2869                 dev_err(&client->dev, "%s: kzalloc error\n", __func__);
2870                 return -ENOMEM;
2871         }
2872
2873         info->i2c_client = client;
2874         if (client->dev.platform_data) {
2875                 info->pdata = client->dev.platform_data;
2876         } else {
2877                 info->pdata = &imx091_dflt_pdata;
2878                 dev_dbg(&client->dev,
2879                         "%s No platform data.  Using defaults.\n", __func__);
2880         }
2881         i2c_set_clientdata(client, info);
2882         INIT_LIST_HEAD(&info->list);
2883         spin_lock(&imx091_spinlock);
2884         list_add_rcu(&info->list, &imx091_info_list);
2885         spin_unlock(&imx091_spinlock);
2886         imx091_pm_init(info);
2887         imx091_sdata_init(info);
2888         imx091_get_flash_cap(info);
2889         if (info->pdata->cfg & (NVC_CFG_NODEV | NVC_CFG_BOOT_INIT)) {
2890                 if (info->pdata->probe_clock) {
2891                         if (info->cap->initial_clock_rate_khz)
2892                                 clock_probe_rate = info->cap->
2893                                                         initial_clock_rate_khz;
2894                         else
2895                                 clock_probe_rate = imx091_dflt_cap.
2896                                                         initial_clock_rate_khz;
2897                         clock_probe_rate *= 1000;
2898                         info->pdata->probe_clock(clock_probe_rate);
2899                 }
2900                 err = imx091_dev_id(info);
2901                 if (err < 0) {
2902                         if (info->pdata->cfg & NVC_CFG_NODEV) {
2903                                 imx091_del(info);
2904                                 if (info->pdata->probe_clock)
2905                                         info->pdata->probe_clock(0);
2906                                 return -ENODEV;
2907                         } else {
2908                                 dev_err(&client->dev, "%s device not found\n",
2909                                         __func__);
2910                         }
2911                 } else {
2912                         dev_dbg(&client->dev, "%s device found\n", __func__);
2913                         if (info->pdata->cfg & NVC_CFG_BOOT_INIT)
2914                                 imx091_mode_wr_full(info, info->cap->
2915                                                     preferred_mode_index);
2916                 }
2917                 imx091_pm_dev_wr(info, NVC_PWR_OFF);
2918                 if (info->pdata->probe_clock)
2919                         info->pdata->probe_clock(0);
2920         }
2921
2922         imx091_edp_register(info);
2923
2924         if (info->pdata->dev_name != 0)
2925                 strcpy(dname, info->pdata->dev_name);
2926         else
2927                 strcpy(dname, "imx091");
2928         if (info->pdata->num)
2929                 snprintf(dname, sizeof(dname), "%s.%u",
2930                          dname, info->pdata->num);
2931         info->miscdev.name = dname;
2932         info->miscdev.fops = &imx091_fileops;
2933         info->miscdev.minor = MISC_DYNAMIC_MINOR;
2934         if (misc_register(&info->miscdev)) {
2935                 dev_err(&client->dev, "%s unable to register misc device %s\n",
2936                         __func__, dname);
2937                 imx091_del(info);
2938                 return -ENODEV;
2939         }
2940
2941 #ifdef CONFIG_DEBUG_FS
2942         info->debugfs_info.name = dname;
2943         info->debugfs_info.i2c_client = info->i2c_client;
2944         info->debugfs_info.i2c_addr_limit = 0xFFFF;
2945         info->debugfs_info.i2c_rd8 = imx091_i2c_rd8;
2946         info->debugfs_info.i2c_wr8 = imx091_i2c_wr8;
2947         nvc_debugfs_init(&info->debugfs_info);
2948 #endif
2949         dev_dbg(&client->dev, "%s -----\n", __func__);
2950         return 0;
2951 }
2952
2953 static const struct i2c_device_id imx091_id[] = {
2954         { "imx091", 0 },
2955         { },
2956 };
2957
2958 MODULE_DEVICE_TABLE(i2c, imx091_id);
2959
2960 static struct i2c_driver imx091_i2c_driver = {
2961         .driver = {
2962                 .name = "imx091",
2963                 .owner = THIS_MODULE,
2964         },
2965         .id_table = imx091_id,
2966         .probe = imx091_probe,
2967         .remove = imx091_remove,
2968 };
2969
2970 module_i2c_driver(imx091_i2c_driver);
2971 MODULE_LICENSE("GPL v2");