]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/media/platform/tegra/imx185.c
kernel: add imx185 sensor support
[sojka/nv-tegra/linux-3.10.git] / drivers / media / platform / tegra / imx185.c
1 /*
2  * imx185.c - imx185 sensor driver
3  *
4  * Copyright (c) 2014-2015, 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/delay.h>
20 #include <linux/fs.h>
21 #include <linux/i2c.h>
22 #include <linux/clk.h>
23 #include <linux/miscdevice.h>
24 #include <linux/slab.h>
25 #include <linux/uaccess.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/regmap.h>
28 #include <media/imx185.h>
29 #include <linux/gpio.h>
30 #include <linux/module.h>
31
32 #include <linux/kernel.h>
33 #include <linux/debugfs.h>
34 #include <linux/seq_file.h>
35 #include <linux/of.h>
36 #include <linux/of_device.h>
37 #include <linux/of_gpio.h>
38 #include <mach/io_dpd.h>
39
40 #include "nvc_utilities.h"
41
42 struct imx185_reg {
43         u16 addr;
44         u8 val;
45 };
46
47 struct imx185_info {
48         struct miscdevice               miscdev_info;
49         int                             mode;
50         struct imx185_power_rail        power;
51         struct imx185_sensordata        sensor_data;
52         struct i2c_client               *i2c_client;
53         struct imx185_platform_data     *pdata;
54         struct clk                      *mclk;
55         struct regmap                   *regmap;
56         struct mutex                    imx185_camera_lock;
57         struct dentry                   *debugdir;
58         atomic_t                        in_use;
59         u32                             frame_length;
60 };
61
62 static const struct regmap_config sensor_regmap_config = {
63         .reg_bits = 16,
64         .val_bits = 8,
65         .cache_type = REGCACHE_RBTREE,
66 };
67
68 #define IMX185_TABLE_WAIT_MS 0
69 #define IMX185_TABLE_END 1
70 #define IMX185_MAX_RETRIES 3
71 #define IMX185_WAIT_MS 3
72
73 #define MAX_BUFFER_SIZE 32
74 #define IMX185_FRAME_LENGTH_ADDR_MSB 0x3019
75 #define IMX185_FRAME_LENGTH_ADDR_LSB 0x3018
76 #define IMX185_COARSE_TIME_ADDR_MSB 0x3021
77 #define IMX185_COARSE_TIME_ADDR_LSB 0x3020
78 #define IMX185_GAIN_ADDR 0x3014
79 #define IMX185_REG_HOLD 0x3001
80
81 static struct imx185_reg mode_1920x1200[] = {
82         /* standby */
83         {0x3000, 0x01},
84         {IMX185_TABLE_WAIT_MS, IMX185_WAIT_MS},
85
86         {0x3002, 0x01}, /* master mode operation stop */
87         {0x3005, 0x01}, /* AD conversion 12 bits */
88         {0x3006, 0x00}, /* All-pixel scan mode */
89         {0x3007, 0x00}, /* WUXGA mode 1920x1200 */
90         {0x3009, 0x01}, /* frame rate 30fps */
91         {0x300A, 0xF0}, /* Black level */
92         {0x300B, 0x00},
93         {0x300C, 0x00},
94         {0x300F, 0x01},
95
96         {0x3010, 0x39},
97         {0x3012, 0x50},
98         {0x3084, 0x00},
99         {0x3065, 0x20},
100         {0x3086, 0x01},
101         {0x30CF, 0xD1},
102         {0x30D0, 0x1B},
103         {0x30D2, 0x5F},
104         {0x30D3, 0x00},
105         {0x3018, 0x28}, /* VMAX=1320 */
106         {0x3019, 0x05},
107         {0x301A, 0x00},
108         {0x301B, 0x53}, /* HMAX=1875 */
109         {0x301C, 0x07},
110         {0x301D, 0x08},
111         {0x301E, 0x02},
112         {0x3048, 0x33},
113         {0x3044, 0xE1}, /* MIPI output mode */
114         {0x305C, 0x20},
115         {0x305D, 0x00},
116         {0x305E, 0x18},
117         {0x305F, 0x00},
118         {0x3063, 0x74}, /* INCLK */
119
120         {0x311D, 0x0A},
121         {0x3123, 0x0F},
122         {0x3147, 0x87},
123         {0x31E1, 0x9E},
124         {0x31E2, 0x01},
125         {0x31E5, 0x05},
126         {0x31E6, 0x05},
127         {0x31E7, 0x3A},
128         {0x31E8, 0x3A},
129
130         {0x3203, 0xC8},
131         {0x3207, 0x54},
132         {0x3213, 0x16},
133         {0x3215, 0xF6},
134         {0x321A, 0x14},
135         {0x321B, 0x51},
136         {0x3229, 0xE7},
137         {0x322A, 0xF0},
138         {0x322B, 0x10},
139         {0x3231, 0xE7},
140         {0x3232, 0xF0},
141         {0x3233, 0x10},
142         {0x323C, 0xE8},
143         {0x323D, 0x70},
144         {0x3243, 0x08},
145         {0x3244, 0xE1},
146         {0x3245, 0x10},
147         {0x3247, 0xE7},
148         {0x3248, 0x60},
149         {0x3249, 0x1E},
150         {0x324B, 0x00},
151         {0x324C, 0x41},
152         {0x3250, 0x30},
153         {0x3251, 0x0A},
154         {0x3252, 0xFF},
155         {0x3253, 0xFF},
156         {0x3254, 0xFF},
157         {0x3255, 0x02},
158         {0x3257, 0xF0},
159         {0x325D, 0xEE},
160         {0x325E, 0xA0},
161         {0x3260, 0x03},
162         {0x3261, 0x61},
163         {0x3266, 0x30},
164         {0x3267, 0x05},
165         {0x3275, 0xE7},
166         {0x3281, 0xEA},
167         {0x3282, 0x70},
168         {0x3285, 0xFF},
169         {0x328A, 0xF0},
170         {0x328D, 0xB6},
171         {0x328E, 0x40},
172         {0x3290, 0x42},
173         {0x3291, 0x51},
174         {0x3292, 0x1E},
175         {0x3294, 0xC4},
176         {0x3295, 0x20},
177         {0x3297, 0x50},
178         {0x3298, 0x31},
179         {0x3299, 0x1F},
180         {0x329B, 0xC0},
181         {0x329C, 0x60},
182         {0x329E, 0x4C},
183         {0x329F, 0x71},
184         {0x32A0, 0x1F},
185         {0x32A2, 0xB6},
186         {0x32A3, 0xC0},
187         {0x32A4, 0x0B},
188         {0x32A9, 0x24},
189         {0x32AA, 0x41},
190         {0x32B0, 0x25},
191         {0x32B1, 0x51},
192         {0x32B7, 0x1C},
193         {0x32B8, 0xC1},
194         {0x32B9, 0x12},
195         {0x32BE, 0x1D},
196         {0x32BF, 0xD1},
197         {0x32C0, 0x12},
198         {0x32C2, 0xA8},
199         {0x32C3, 0xC0},
200         {0x32C4, 0x0A},
201         {0x32C5, 0x1E},
202         {0x32C6, 0x21},
203         {0x32C9, 0xB0},
204         {0x32CA, 0x40},
205         {0x32CC, 0x26},
206         {0x32CD, 0xA1},
207         {0x32D0, 0xB6},
208         {0x32D1, 0xC0},
209         {0x32D2, 0x0B},
210         {0x32D4, 0xE2},
211         {0x32D5, 0x40},
212         {0x32D8, 0x4E},
213         {0x32D9, 0xA1},
214         {0x32EC, 0xF0},
215
216         {0x332D, 0x20},
217         {0x3303, 0x00}, /* REPETITION */
218         {0x3305, 0x03}, /* MIPI 4 Lanes */
219         {0x3314, 0x0E},
220         {0x3315, 0x01},
221         {0x3316, 0x04},
222         {0x3317, 0x04},
223         {0x3318, 0xC1}, /* V effective pixel=1217 */
224         {0x3319, 0x04},
225         {0x332C, 0x40},
226         {0x332D, 0x20},
227         {0x332E, 0x03},
228         {0x333E, 0x0C}, /* CSI_DT_FMT RAM 12 */
229         {0x333F, 0x0C},
230         {0x3340, 0x03}, /* CSI Lane 4 */
231         {0x3341, 0x20}, /* INCLK = 37.125MHz */
232         {0x3342, 0x25},
233         {0x334E, 0xB4},
234         {0x334F, 0x01},
235         {0x3343, 0x68},
236         {0x3344, 0x20},
237         {0x3345, 0x40},
238         {0x3346, 0x28},
239         {0x3347, 0x20},
240         {0x3348, 0x18},
241         {0x3349, 0x78},
242         {0x334A, 0x28},
243         {0x338C, 0x0C},
244         {0x338D, 0x0C},
245         {0x33D8, 0x03},
246
247         /* Exit standby */
248         {0x3000, 0x00},
249         {IMX185_TABLE_WAIT_MS, 25},
250         /* Master mode start */
251         {0x3002, 0x00},
252         {0x3049, 0x0A}, /* XVS VSYNC and XHS HSYNC */
253         {IMX185_TABLE_WAIT_MS, 200},
254         {IMX185_TABLE_END, 0x00}
255 };
256
257 static struct imx185_reg mode_1920x1080[] = {
258         /* standby */
259         {0x3000, 0x01},
260         {IMX185_TABLE_WAIT_MS, IMX185_WAIT_MS},
261
262         {0x3002, 0x01}, /* master mode operation stop */
263         {0x3005, 0x01}, /* AD conversion 12 bits */
264         {0x3006, 0x00}, /* All-pixel scan mode */
265         {0x3007, 0x50}, /* 1080p-HD cropping mode 1920x1080 */
266         {0x3009, 0x02}, /* frame rate 30fps */
267 #if 0
268         /* IMX185 PG1 */
269         {0x300A, 0x00}, /* Black level */
270         {0x300E, 0x00},
271         {0x3089, 0x00},
272         {0x308C, 0x13},
273 #else
274         {0x300A, 0xf0}, /* Black level */
275 #endif
276         {0x300B, 0x00},
277         {0x300C, 0x00},
278         {0x300F, 0x01},
279
280         {0x3010, 0x39},
281         {0x3012, 0x50},
282         {0x3084, 0x00},
283         {0x3065, 0x20},
284         {0x3086, 0x01},
285         {0x30CF, 0xD1},
286         {0x30D0, 0x1B},
287         {0x30D2, 0x5F},
288         {0x30D3, 0x00},
289         {0x3018, 0x65}, /* VMAX=1125 */
290         {0x3019, 0x04},
291         {0x301A, 0x00},
292         {0x301B, 0x98}, /* HMAX=2200 */
293         {0x301C, 0x08},
294         {0x301D, 0x08},
295         {0x301E, 0x02},
296         {0x3038, 0x08}, /* WINPV=8 */
297         {0x3039, 0x00},
298         {0x303a, 0x40}, /* WINWV=1088 */
299         {0x303b, 0x04},
300         {0x303c, 0x0c}, /* WINPH=12 */
301         {0x303d, 0x00},
302         {0x303e, 0x7c}, /* WINWH=1916 */
303         {0x303f, 0x07},
304         {0x3048, 0x33},
305         {0x3044, 0xE1}, /* MIPI output mode */
306         {0x305C, 0x20},
307         {0x305D, 0x00},
308         {0x305E, 0x18},
309         {0x305F, 0x00},
310         {0x3063, 0x74}, /* INCLK */
311
312         {0x311D, 0x0A},
313         {0x3123, 0x0F},
314         {0x3147, 0x87},
315         {0x31E1, 0x9E},
316         {0x31E2, 0x01},
317         {0x31E5, 0x05},
318         {0x31E6, 0x05},
319         {0x31E7, 0x3A},
320         {0x31E8, 0x3A},
321
322         {0x3203, 0xC8},
323         {0x3207, 0x54},
324         {0x3213, 0x16},
325         {0x3215, 0xF6},
326         {0x321A, 0x14},
327         {0x321B, 0x51},
328         {0x3229, 0xE7},
329         {0x322A, 0xF0},
330         {0x322B, 0x10},
331         {0x3231, 0xE7},
332         {0x3232, 0xF0},
333         {0x3233, 0x10},
334         {0x323C, 0xE8},
335         {0x323D, 0x70},
336         {0x3243, 0x08},
337         {0x3244, 0xE1},
338         {0x3245, 0x10},
339         {0x3247, 0xE7},
340         {0x3248, 0x60},
341         {0x3249, 0x1E},
342         {0x324B, 0x00},
343         {0x324C, 0x41},
344         {0x3250, 0x30},
345         {0x3251, 0x0A},
346         {0x3252, 0xFF},
347         {0x3253, 0xFF},
348         {0x3254, 0xFF},
349         {0x3255, 0x02},
350         {0x3257, 0xF0},
351         {0x325D, 0xEE},
352         {0x325E, 0xA0},
353         {0x3260, 0x03},
354         {0x3261, 0x61},
355         {0x3266, 0x30},
356         {0x3267, 0x05},
357         {0x3275, 0xE7},
358         {0x3281, 0xEA},
359         {0x3282, 0x70},
360         {0x3285, 0xFF},
361         {0x328A, 0xF0},
362         {0x328D, 0xB6},
363         {0x328E, 0x40},
364         {0x3290, 0x42},
365         {0x3291, 0x51},
366         {0x3292, 0x1E},
367         {0x3294, 0xC4},
368         {0x3295, 0x20},
369         {0x3297, 0x50},
370         {0x3298, 0x31},
371         {0x3299, 0x1F},
372         {0x329B, 0xC0},
373         {0x329C, 0x60},
374         {0x329E, 0x4C},
375         {0x329F, 0x71},
376         {0x32A0, 0x1F},
377         {0x32A2, 0xB6},
378         {0x32A3, 0xC0},
379         {0x32A4, 0x0B},
380         {0x32A9, 0x24},
381         {0x32AA, 0x41},
382         {0x32B0, 0x25},
383         {0x32B1, 0x51},
384         {0x32B7, 0x1C},
385         {0x32B8, 0xC1},
386         {0x32B9, 0x12},
387         {0x32BE, 0x1D},
388         {0x32BF, 0xD1},
389         {0x32C0, 0x12},
390         {0x32C2, 0xA8},
391         {0x32C3, 0xC0},
392         {0x32C4, 0x0A},
393         {0x32C5, 0x1E},
394         {0x32C6, 0x21},
395         {0x32C9, 0xB0},
396         {0x32CA, 0x40},
397         {0x32CC, 0x26},
398         {0x32CD, 0xA1},
399         {0x32D0, 0xB6},
400         {0x32D1, 0xC0},
401         {0x32D2, 0x0B},
402         {0x32D4, 0xE2},
403         {0x32D5, 0x40},
404         {0x32D8, 0x4E},
405         {0x32D9, 0xA1},
406         {0x32EC, 0xF0},
407
408         {0x332D, 0x20},
409         {0x334E, 0xB4}, /*INCK_FREQ */
410         {0x334F, 0x01},
411         {0x3303, 0x10}, /* REPETITION */
412         {0x3305, 0x03}, /* MIPI 4 Lanes */
413         {0x3314, 0x08},
414         {0x3315, 0x01},
415         {0x3316, 0x04},
416         {0x3317, 0x04},
417         {0x3318, 0x38}, /* V effective pixel=1080 */
418         {0x3319, 0x04},
419         {0x332C, 0x30},
420         {0x332D, 0x20},
421         {0x332E, 0x03},
422         {0x333E, 0x0C}, /* CSI_DT_FMT RAM 10 */
423         {0x333F, 0x0C},
424         {0x3340, 0x03}, /* CSI 4Lane */
425         {0x3341, 0x20}, /* INCLK = 37.125MHz */
426         {0x3342, 0x25},
427         /* Global Timing */
428         {0x3343, 0x58}, /* TCLK_POST */
429         {0x3344, 0x10}, /* THS_PREPARE */
430         {0x3345, 0x30}, /* THS_ZERO_MIN */
431         {0x3346, 0x18}, /* THS_TRAIL */
432         {0x3347, 0x10}, /* TCLK_TRAIL_MIN */
433         {0x3348, 0x10}, /* TCLK_PREPARE */
434         {0x3349, 0x48}, /* TCLK_ZERO */
435         {0x334A, 0x28}, /* TLP_X */
436         {0x338C, 0x0C}, /* ADC 12bit */
437         {0x338D, 0x0C},
438         {0x33D8, 0x03},
439
440         /* Exit standby */
441         {0x3000, 0x00},
442         {IMX185_TABLE_WAIT_MS, 25},
443         /* Master mode start */
444         {0x3002, 0x00},
445         {0x3049, 0x0A}, /* XVS VSYNC and XHS HSYNC */
446         {IMX185_TABLE_WAIT_MS, 210},
447         {IMX185_TABLE_END, 0x00}
448 };
449
450 enum {
451         IMX185_MODE_1920X1200,
452         IMX185_MODE_1920X1080,
453 };
454
455 static struct imx185_reg *mode_table[] = {
456         [IMX185_MODE_1920X1200] = mode_1920x1200,
457         [IMX185_MODE_1920X1080] = mode_1920x1080,
458 };
459
460 static inline void
461 msleep_range(unsigned int delay_base)
462 {
463         usleep_range(delay_base*1000, delay_base*1000+500);
464 }
465
466 static inline void
467 imx185_get_frame_length_regs(struct imx185_reg *regs, u32 frame_length)
468 {
469         /* vertical span setting */
470         regs->addr = IMX185_FRAME_LENGTH_ADDR_MSB;
471         regs->val = (frame_length >> 8) & 0xff;
472         (regs + 1)->addr = IMX185_FRAME_LENGTH_ADDR_LSB;
473         (regs + 1)->val = (frame_length) & 0xff;
474 }
475
476 static inline void
477 imx185_get_coarse_time_regs(struct imx185_reg *regs, u32 coarse_time)
478 {
479         /* storage time adjustment */
480         regs->addr = IMX185_COARSE_TIME_ADDR_MSB;
481         regs->val = (coarse_time >> 8) & 0xff;
482         (regs + 1)->addr = IMX185_COARSE_TIME_ADDR_LSB;
483         (regs + 1)->val = (coarse_time) & 0xff;
484 }
485
486 static inline void
487 imx185_get_gain_reg(struct imx185_reg *regs, u16 gain)
488 {
489         regs->addr = IMX185_GAIN_ADDR;
490         regs->val = gain;
491 }
492
493 static int imx185_read_reg(struct imx185_info *info, u16 addr, u8 *val)
494 {
495         int err;
496         struct i2c_msg msg[2];
497         unsigned char data[3];
498         struct i2c_client *client = info->i2c_client;
499         if (!client->adapter)
500                 return -ENODEV;
501
502         msg[0].addr = client->addr;
503         msg[0].flags = 0;
504         msg[0].len = 2;
505         msg[0].buf = data;
506
507         /* high byte goes out first */
508         data[0] = (u8) (addr >> 8);
509         data[1] = (u8) (addr & 0xff);
510
511         msg[1].addr = client->addr;
512         msg[1].flags = I2C_M_RD;
513         msg[1].len = 1;
514         msg[1].buf = data + 2;
515
516         err = i2c_transfer(client->adapter, msg, 2);
517
518         if (err != 2)
519                 return -EINVAL;
520
521         *val = data[2];
522         return 0;
523 }
524
525         static int
526 imx185_write_reg(struct imx185_info *info, u16 addr, u8 val)
527 {
528         int err;
529         struct i2c_msg msg;
530         unsigned char data[3];
531         struct i2c_client *client =     info->i2c_client;
532         if (!client->adapter)
533                 return -ENODEV;
534
535         pr_debug("imx185_write_reg: %x,%x\n", addr, val);
536
537         data[0] = (u8) (addr >> 8);
538         data[1] = (u8) (addr & 0xff);
539         data[2] = (u8) (val & 0xff);
540
541         msg.addr = client->addr;
542         msg.flags = 0;
543         msg.len = 3;
544         msg.buf = data;
545
546         err = i2c_transfer(client->adapter, &msg, 1);
547         if (err == 1)
548                 return 0;
549
550         pr_err("%s:i2c write failed, client->addr = 0x%x %x = %x\n",
551                         __func__, client->addr, addr, val);
552
553         return err;
554 }
555 static int
556 imx185_write_table(struct imx185_info *info,
557                                  const struct imx185_reg table[],
558                                  const struct imx185_reg override_list[],
559                                  int num_override_regs)
560 {
561         int err;
562         const struct imx185_reg *next;
563         int i;
564         u16 val;
565
566         for (next = table; next->addr != IMX185_TABLE_END; next++) {
567                 if (next->addr == IMX185_TABLE_WAIT_MS) {
568                         msleep_range(next->val);
569                         continue;
570                 }
571
572                 val = next->val;
573
574                 /* When an override list is passed in, replace the reg */
575                 /* value to write if the reg is in the list            */
576                 if (override_list) {
577                         for (i = 0; i < num_override_regs; i++) {
578                                 if (next->addr == override_list[i].addr) {
579                                         val = override_list[i].val;
580                                         break;
581                                 }
582                         }
583                 }
584
585                 err = imx185_write_reg(info, next->addr, val);
586                 if (err) {
587                         pr_err("%s:imx185_write_table:%d", __func__, err);
588                         return err;
589                 }
590         }
591         return 0;
592 }
593
594 static int imx185_power_on(struct imx185_power_rail *pw)
595 {
596         int err;
597         struct imx185_info *info = container_of(pw, struct imx185_info, power);
598
599         if (info->pdata->cam1_gpio > 0)
600                 gpio_set_value(info->pdata->cam1_gpio, 0);
601         usleep_range(10, 20);
602
603         err = regulator_enable(pw->avdd);
604         if (err)
605                 goto imx185_avdd_fail;
606
607         err = regulator_enable(pw->iovdd);
608         if (err)
609                 goto imx185_iovdd_fail;
610
611         usleep_range(1, 2);
612         if (info->pdata->cam1_gpio > 0)
613                 gpio_set_value(info->pdata->cam1_gpio, 1);
614
615         usleep_range(300, 310);
616
617         return 1;
618
619
620 imx185_iovdd_fail:
621         regulator_disable(pw->avdd);
622
623 imx185_avdd_fail:
624         gpio_set_value(info->pdata->af_gpio, 0);
625
626         pr_err("%s failed.\n", __func__);
627         return -ENODEV;
628 }
629
630 static int imx185_power_off(struct imx185_power_rail *pw)
631 {
632         struct imx185_info *info = container_of(pw, struct imx185_info, power);
633
634         gpio_set_value(info->pdata->cam1_gpio, 0);
635         usleep_range(1, 2);
636
637         regulator_disable(pw->iovdd);
638         regulator_disable(pw->avdd);
639
640         return 0;
641 }
642
643 static int imx185_get_flash_cap(struct imx185_info *info)
644 {
645         return -ENODEV;
646 }
647
648 static inline int imx185_set_flash_control(
649         struct imx185_info *info, struct imx185_flash_control *fc)
650 {
651         return -ENODEV;
652 }
653
654 static int
655 imx185_set_mode(struct imx185_info *info, struct imx185_mode *mode)
656 {
657         int sensor_mode;
658         int err;
659         struct imx185_reg reg_list[8];
660
661         pr_info("%s: xres %u yres %u framelength %u coarsetime %u gain %u\n",
662                          __func__, mode->xres, mode->yres, mode->frame_length,
663                          mode->coarse_time, mode->gain);
664
665         if (mode->xres == 1920 && mode->yres == 1200) {
666                 sensor_mode = IMX185_MODE_1920X1200;
667         } else if (mode->xres == 1920 && mode->yres == 1080) {
668                 sensor_mode = IMX185_MODE_1920X1080;
669         } else {
670                 pr_err("%s: invalid resolution supplied to set mode %d %d\n",
671                          __func__, mode->xres, mode->yres);
672                 return -EINVAL;
673         }
674
675         /* get a list of override regs for the asking frame length, */
676         /* coarse integration time, and gain.                       */
677         imx185_get_frame_length_regs(reg_list, mode->frame_length);
678         imx185_get_coarse_time_regs(reg_list + 2,
679                 mode->frame_length - mode->coarse_time - 1);
680         imx185_get_gain_reg(reg_list + 4, mode->gain);
681
682         err = imx185_write_table(info,
683                                 mode_table[sensor_mode],
684                                 reg_list, 0);
685         if (err)
686                 return err;
687         info->mode = sensor_mode;
688         info->frame_length = mode->frame_length;
689         pr_info("[IMX185]: stream on.\n");
690
691         return 0;
692 }
693
694 static int
695 imx185_get_status(struct imx185_info *info, u8 *dev_status)
696 {
697         *dev_status = 0;
698         return 0;
699 }
700
701 static int
702 imx185_set_frame_length(struct imx185_info *info, u32 frame_length,
703                                                  bool group_hold)
704 {
705         struct imx185_reg reg_list[2];
706         int i = 0;
707         int ret;
708
709         pr_debug("%s: framelength %d\n", __func__, frame_length);
710
711         imx185_get_frame_length_regs(reg_list, frame_length);
712
713         if (group_hold) {
714                 ret = imx185_write_reg(info, 0x0104, 0x01);
715                 if (ret)
716                         return ret;
717         }
718
719         for (i = 0; i < 2; i++) {
720                 ret = imx185_write_reg(info, reg_list[i].addr,
721                          reg_list[i].val);
722                 if (ret)
723                         return ret;
724         }
725
726         if (group_hold) {
727                 ret = imx185_write_reg(info, 0x0104, 0x0);
728                 if (ret)
729                         return ret;
730         }
731         info->frame_length = frame_length;
732
733         return 0;
734 }
735
736 static int
737 imx185_set_coarse_time(struct imx185_info *info, u32 coarse_time,
738                                                  bool group_hold)
739 {
740         int ret;
741
742         struct imx185_reg reg_list[2];
743         int i = 0;
744
745         pr_debug("%s: coarsetime %d\n", __func__, coarse_time);
746
747         imx185_get_coarse_time_regs(reg_list,
748                 info->frame_length - coarse_time - 1);
749
750         if (group_hold) {
751                 ret = imx185_write_reg(info, 0x104, 0x01);
752                 if (ret)
753                         return ret;
754         }
755
756         for (i = 0; i < 2; i++) {
757                 ret = imx185_write_reg(info, reg_list[i].addr,
758                          reg_list[i].val);
759                 if (ret)
760                         return ret;
761         }
762
763         if (group_hold) {
764                 ret = imx185_write_reg(info, 0x104, 0x0);
765                 if (ret)
766                         return ret;
767         }
768         return 0;
769 }
770
771 static int
772 imx185_set_gain(struct imx185_info *info, u16 gain, bool group_hold)
773 {
774         int ret;
775         struct imx185_reg reg_list;
776
777         pr_debug("%s: gain %d\n", __func__, gain);
778
779         imx185_get_gain_reg(&reg_list, gain);
780
781         if (group_hold) {
782                 ret = imx185_write_reg(info, 0x104, 0x1);
783                 if (ret)
784                         return ret;
785         }
786
787         ret = imx185_write_reg(info, reg_list.addr, reg_list.val);
788         if (ret)
789                 return ret;
790
791         if (group_hold) {
792                 ret = imx185_write_reg(info, 0x104, 0x0);
793                 if (ret)
794                         return ret;
795         }
796         return 0;
797 }
798
799 static int
800 imx185_set_group_hold(struct imx185_info *info, struct imx185_ae *ae)
801 {
802         int ret;
803         int count = 0;
804         bool group_hold_enabled = false;
805
806         if (ae->gain_enable)
807                 count++;
808         if (ae->coarse_time_enable)
809                 count++;
810         if (ae->frame_length_enable)
811                 count++;
812         if (count >= 2)
813                 group_hold_enabled = true;
814
815         if (group_hold_enabled) {
816                 ret = imx185_write_reg(info, IMX185_REG_HOLD, 0x1);
817                 if (ret)
818                         return ret;
819         }
820
821         if (ae->gain_enable)
822                 imx185_set_gain(info, ae->gain, false);
823         if (ae->frame_length_enable)
824                 imx185_set_frame_length(info, ae->frame_length, false);
825         if (ae->coarse_time_enable)
826                 imx185_set_coarse_time(info, ae->coarse_time, false);
827
828         if (group_hold_enabled) {
829                 ret = imx185_write_reg(info, IMX185_REG_HOLD, 0x0);
830                 if (ret)
831                         return ret;
832         }
833
834         return 0;
835 }
836
837 static int imx185_get_sensor_id(struct imx185_info *info)
838 {
839         int ret = 0;
840         int i;
841
842         pr_debug("%s\n", __func__);
843         if (info->sensor_data.fuse_id_size)
844                 return 0;
845
846         for (i = 0; i < 9; i++) {
847                 info->sensor_data.fuse_id[i] = 0xaa;
848         }
849
850         if (!ret)
851                 info->sensor_data.fuse_id_size = i;
852
853         return ret;
854 }
855
856 static void imx185_mclk_disable(struct imx185_info *info)
857 {
858         dev_err(&info->i2c_client->dev, "%s: disable MCLK\n", __func__);
859         clk_disable_unprepare(info->mclk);
860 }
861
862 static int imx185_mclk_enable(struct imx185_info *info)
863 {
864         int err;
865         unsigned long mclk_init_rate = 37125000;
866
867         dev_err(&info->i2c_client->dev, "%s: enable MCLK with %lu Hz\n",
868                 __func__, mclk_init_rate);
869
870         err = clk_set_rate(info->mclk, mclk_init_rate);
871         if (!err)
872                 err = clk_prepare_enable(info->mclk);
873         return err;
874 }
875
876 static long
877 imx185_ioctl(struct file *file,
878                          unsigned int cmd, unsigned long arg)
879 {
880         int err = 0;
881         struct imx185_info *info = file->private_data;
882
883         switch (cmd) {
884         case IMX185_IOCTL_SET_POWER:
885                 if (!info->pdata)
886                         break;
887                 if (arg && info->pdata->power_on) {
888                         err = imx185_mclk_enable(info);
889                         if (!err)
890                                 err = info->pdata->power_on(&info->power);
891                         if (err < 0)
892                                 imx185_mclk_disable(info);
893                 }
894                 if (!arg) {
895                         info->pdata->power_off(&info->power);
896                         imx185_mclk_disable(info);
897                 }
898                 break;
899         case IMX185_IOCTL_SET_MODE:
900         {
901                 struct imx185_mode mode;
902                 if (copy_from_user(&mode, (const void __user *)arg,
903                         sizeof(struct imx185_mode))) {
904                         pr_err("%s:Failed to get mode from user.\n", __func__);
905                         return -EFAULT;
906                 }
907                 return imx185_set_mode(info, &mode);
908         }
909         case IMX185_IOCTL_SET_FRAME_LENGTH:
910                 return imx185_set_frame_length(info, (u32)arg, true);
911         case IMX185_IOCTL_SET_COARSE_TIME:
912                 return imx185_set_coarse_time(info, (u32)arg, true);
913         case IMX185_IOCTL_SET_GAIN:
914                 return imx185_set_gain(info, (u16)arg, true);
915         case IMX185_IOCTL_GET_STATUS:
916         {
917                 u8 status;
918
919                 err = imx185_get_status(info, &status);
920                 if (err)
921                         return err;
922                 if (copy_to_user((void __user *)arg, &status, 1)) {
923                         pr_err("%s:Failed to copy status to user\n", __func__);
924                         return -EFAULT;
925                 }
926                 return 0;
927         }
928         case IMX185_IOCTL_GET_SENSORDATA:
929         {
930                 err = imx185_get_sensor_id(info);
931
932                 if (err) {
933                         pr_err("%s:Failed to get fuse id info.\n", __func__);
934                         return err;
935                 }
936                 if (copy_to_user((void __user *)arg, &info->sensor_data,
937                                 sizeof(struct imx185_sensordata))) {
938                         pr_info("%s:Failed to copy fuse id to user space\n",
939                                 __func__);
940                         return -EFAULT;
941                 }
942                 return 0;
943         }
944         case IMX185_IOCTL_SET_GROUP_HOLD:
945         {
946                 struct imx185_ae ae;
947                 if (copy_from_user(&ae, (const void __user *)arg,
948                         sizeof(struct imx185_ae))) {
949                         pr_info("%s:fail group hold\n", __func__);
950                         return -EFAULT;
951                 }
952                 return imx185_set_group_hold(info, &ae);
953         }
954         case IMX185_IOCTL_SET_FLASH_MODE:
955         {
956                 struct imx185_flash_control values;
957
958                 dev_dbg(&info->i2c_client->dev,
959                         "IMX185_IOCTL_SET_FLASH_MODE\n");
960                 if (copy_from_user(&values,
961                         (const void __user *)arg,
962                         sizeof(struct imx185_flash_control))) {
963                         err = -EFAULT;
964                         break;
965                 }
966                 err = imx185_set_flash_control(info, &values);
967                 break;
968         }
969         case IMX185_IOCTL_GET_FLASH_CAP:
970                 err = imx185_get_flash_cap(info);
971                 break;
972         default:
973                 pr_err("%s:unknown cmd.\n", __func__);
974                 err = -EINVAL;
975         }
976
977         return err;
978 }
979
980 static int imx185_debugfs_show(struct seq_file *s, void *unused)
981 {
982         struct imx185_info *dev = s->private;
983
984         dev_dbg(&dev->i2c_client->dev, "%s: ++\n", __func__);
985
986         mutex_lock(&dev->imx185_camera_lock);
987         mutex_unlock(&dev->imx185_camera_lock);
988
989         return 0;
990 }
991
992 static ssize_t imx185_debugfs_write(
993         struct file *file,
994         char const __user *buf,
995         size_t count,
996         loff_t *offset)
997 {
998         struct imx185_info *dev =
999                         ((struct seq_file *)file->private_data)->private;
1000         struct i2c_client *i2c_client = dev->i2c_client;
1001         int ret = 0;
1002         char buffer[MAX_BUFFER_SIZE];
1003         u32 address;
1004         u32 data;
1005         u8 readback = 0;
1006         dev_dbg(&i2c_client->dev, "%s: ++\n", __func__);
1007
1008         if (copy_from_user(&buffer, buf, sizeof(buffer)))
1009                 goto debugfs_write_fail;
1010
1011         if (sscanf(buf, "0x%x 0x%x", &address, &data) == 2)
1012                 goto set_attr;
1013         if (sscanf(buf, "0X%x 0X%x", &address, &data) == 2)
1014                 goto set_attr;
1015         if (sscanf(buf, "%d %d", &address, &data) == 2)
1016                 goto set_attr;
1017
1018         if (sscanf(buf, "0x%x 0x%x", &address, &data) == 1)
1019                 goto read;
1020         if (sscanf(buf, "0X%x 0X%x", &address, &data) == 1)
1021                 goto read;
1022         if (sscanf(buf, "%d %d", &address, &data) == 1)
1023                 goto read;
1024
1025         dev_err(&i2c_client->dev, "SYNTAX ERROR: %s\n", buf);
1026         return -EFAULT;
1027
1028 set_attr:
1029         dev_info(&i2c_client->dev,
1030                         "new address = %x, data = %x\n", address, data);
1031         ret |= imx185_write_reg(dev, address, data);
1032 read:
1033         ret |= imx185_read_reg(dev, address, &readback);
1034         dev_dbg(&i2c_client->dev,
1035                         "wrote to address 0x%x with value 0x%x\n",
1036                         address, readback);
1037
1038         if (ret)
1039                 goto debugfs_write_fail;
1040
1041         return count;
1042
1043 debugfs_write_fail:
1044         dev_err(&i2c_client->dev,
1045                         "%s: test pattern write failed\n", __func__);
1046         return -EFAULT;
1047 }
1048
1049 static int imx185_debugfs_open(struct inode *inode, struct file *file)
1050 {
1051         struct imx185_info *dev = inode->i_private;
1052         struct i2c_client *i2c_client = dev->i2c_client;
1053
1054         dev_dbg(&i2c_client->dev, "%s: ++\n", __func__);
1055
1056         return single_open(file, imx185_debugfs_show, inode->i_private);
1057 }
1058
1059 static const struct file_operations imx185_debugfs_fops = {
1060         .open           = imx185_debugfs_open,
1061         .read           = seq_read,
1062         .write          = imx185_debugfs_write,
1063         .llseek         = seq_lseek,
1064         .release        = single_release,
1065 };
1066
1067 static void imx185_remove_debugfs(struct imx185_info *dev)
1068 {
1069         struct i2c_client *i2c_client = dev->i2c_client;
1070
1071         dev_dbg(&i2c_client->dev, "%s: ++\n", __func__);
1072
1073         debugfs_remove_recursive(dev->debugdir);
1074         dev->debugdir = NULL;
1075 }
1076
1077 static void imx185_create_debugfs(struct imx185_info *dev)
1078 {
1079         struct dentry *ret;
1080         struct i2c_client *i2c_client = dev->i2c_client;
1081
1082         dev_dbg(&i2c_client->dev, "%s\n", __func__);
1083
1084         dev->debugdir =
1085                 debugfs_create_dir(dev->miscdev_info.this_device->kobj.name,
1086                                                         NULL);
1087         if (!dev->debugdir)
1088                 goto remove_debugfs;
1089
1090         ret = debugfs_create_file("d",
1091                                 S_IWUSR | S_IRUGO,
1092                                 dev->debugdir, dev,
1093                                 &imx185_debugfs_fops);
1094         if (!ret)
1095                 goto remove_debugfs;
1096
1097         return;
1098 remove_debugfs:
1099         dev_err(&i2c_client->dev, "couldn't create debugfs\n");
1100         imx185_remove_debugfs(dev);
1101 }
1102
1103
1104 static int
1105 imx185_open(struct inode *inode, struct file *file)
1106 {
1107         struct miscdevice       *miscdev = file->private_data;
1108         struct imx185_info *info;
1109
1110         info = container_of(miscdev, struct imx185_info, miscdev_info);
1111         /* check if the device is in use */
1112         if (atomic_xchg(&info->in_use, 1)) {
1113                 pr_info("%s:BUSY!\n", __func__);
1114                 return -EBUSY;
1115         }
1116
1117         file->private_data = info;
1118
1119         return 0;
1120 }
1121
1122 static int
1123 imx185_release(struct inode *inode, struct file *file)
1124 {
1125         struct imx185_info *info = file->private_data;
1126
1127         file->private_data = NULL;
1128
1129         /* warn if device is already released */
1130         WARN_ON(!atomic_xchg(&info->in_use, 0));
1131         return 0;
1132 }
1133
1134 static int imx185_power_put(struct imx185_power_rail *pw)
1135 {
1136         if (unlikely(!pw))
1137                 return -EFAULT;
1138
1139         if (likely(pw->avdd))
1140                 regulator_put(pw->avdd);
1141
1142         if (likely(pw->iovdd))
1143                 regulator_put(pw->iovdd);
1144
1145         if (likely(pw->dvdd))
1146                 regulator_put(pw->dvdd);
1147
1148         if (likely(pw->ext_reg1))
1149                 regulator_put(pw->ext_reg1);
1150
1151         if (likely(pw->ext_reg2))
1152                 regulator_put(pw->ext_reg2);
1153
1154         pw->avdd = NULL;
1155         pw->iovdd = NULL;
1156         pw->dvdd = NULL;
1157         pw->ext_reg1 = NULL;
1158         pw->ext_reg2 = NULL;
1159
1160         return 0;
1161 }
1162
1163 static int imx185_regulator_get(struct imx185_info *info,
1164         struct regulator **vreg, char vreg_name[])
1165 {
1166         struct regulator *reg = NULL;
1167         int err = 0;
1168
1169         reg = regulator_get(&info->i2c_client->dev, vreg_name);
1170         if (unlikely(IS_ERR(reg))) {
1171                 dev_err(&info->i2c_client->dev, "%s %s ERR: %d\n",
1172                         __func__, vreg_name, (int)reg);
1173                 err = PTR_ERR(reg);
1174                 reg = NULL;
1175         } else
1176                 dev_dbg(&info->i2c_client->dev, "%s: %s\n",
1177                         __func__, vreg_name);
1178
1179         *vreg = reg;
1180         return err;
1181 }
1182
1183 static int imx185_power_get(struct imx185_info *info)
1184 {
1185         struct imx185_power_rail *pw = &info->power;
1186         int err = 0;
1187
1188         err |= imx185_regulator_get(info, &pw->dvdd, "vdig"); /* digital 1.2v */
1189         err |= imx185_regulator_get(info, &pw->iovdd, "vif"); /* IO 1.8v */
1190         err |= imx185_regulator_get(info, &pw->avdd, "vana"); /* analog 2.7v */
1191
1192         return err;
1193 }
1194
1195 static const struct file_operations imx185_fileops = {
1196         .owner = THIS_MODULE,
1197         .open = imx185_open,
1198         .unlocked_ioctl = imx185_ioctl,
1199         .release = imx185_release,
1200 };
1201
1202 static struct miscdevice imx185_device = {
1203         .minor = MISC_DYNAMIC_MINOR,
1204         .name = "imx185",
1205         .fops = &imx185_fileops,
1206 };
1207
1208 static struct of_device_id imx185_of_match[] = {
1209         { .compatible = "nvidia,imx185", },
1210         { },
1211 };
1212
1213 MODULE_DEVICE_TABLE(of, imx185_of_match);
1214
1215 static struct imx185_platform_data *imx185_parse_dt(struct i2c_client *client)
1216 {
1217         struct device_node *np = client->dev.of_node;
1218         struct imx185_platform_data *board_info_pdata;
1219
1220         board_info_pdata = devm_kzalloc(&client->dev, sizeof(*board_info_pdata),
1221                         GFP_KERNEL);
1222         if (!board_info_pdata) {
1223                 dev_err(&client->dev, "Failed to allocate pdata\n");
1224                 return NULL;
1225         }
1226
1227         of_property_read_string(np, "clocks", &board_info_pdata->mclk_name);
1228         board_info_pdata->cam1_gpio = of_get_named_gpio(np, "cam1-gpios", 0);
1229         board_info_pdata->reset_gpio = of_get_named_gpio(np, "reset-gpios", 0);
1230         board_info_pdata->af_gpio = of_get_named_gpio(np, "af-gpios", 0);
1231
1232         board_info_pdata->ext_reg = of_property_read_bool(np, "nvidia,ext_reg");
1233
1234         board_info_pdata->power_on = imx185_power_on;
1235         board_info_pdata->power_off = imx185_power_off;
1236
1237         return board_info_pdata;
1238 }
1239
1240 static int
1241 imx185_probe(struct i2c_client *client,
1242                         const struct i2c_device_id *id)
1243 {
1244         struct imx185_info *info;
1245         int err;
1246         const char *mclk_name;
1247
1248         pr_info("[IMX185]: probing sensor.\n");
1249
1250         info = devm_kzalloc(&client->dev,
1251                         sizeof(struct imx185_info), GFP_KERNEL);
1252         if (!info) {
1253                 pr_err("%s:Unable to allocate memory!\n", __func__);
1254                 return -ENOMEM;
1255         }
1256
1257         info->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
1258         if (IS_ERR(info->regmap)) {
1259                 dev_err(&client->dev,
1260                         "regmap init failed: %ld\n", PTR_ERR(info->regmap));
1261                 return -ENODEV;
1262         }
1263
1264         if (client->dev.of_node) {
1265                 info->pdata = imx185_parse_dt(client);
1266         } else {
1267                 info->pdata = client->dev.platform_data;
1268         }
1269
1270         if (!info->pdata) {
1271                 pr_err("[IMX185]:%s:Unable to get platform data\n", __func__);
1272                 return -EFAULT;
1273         }
1274
1275         info->i2c_client = client;
1276         atomic_set(&info->in_use, 0);
1277         info->mode = -1;
1278
1279         mclk_name = info->pdata->mclk_name ?
1280                     info->pdata->mclk_name : "default_mclk";
1281         info->mclk = devm_clk_get(&client->dev, mclk_name);
1282         if (IS_ERR(info->mclk)) {
1283                 dev_err(&client->dev, "%s: unable to get clock %s\n",
1284                         __func__, mclk_name);
1285                 return PTR_ERR(info->mclk);
1286         }
1287
1288         imx185_power_get(info);
1289
1290         memcpy(&info->miscdev_info,
1291                 &imx185_device,
1292                 sizeof(struct miscdevice));
1293
1294         err = misc_register(&info->miscdev_info);
1295         if (err) {
1296                 pr_err("%s:Unable to register misc device!\n", __func__);
1297                 goto imx185_probe_fail;
1298         }
1299
1300         i2c_set_clientdata(client, info);
1301         /* create debugfs interface */
1302         imx185_create_debugfs(info);
1303         pr_err("[IMX185]: end of probing sensor.\n");
1304         return 0;
1305
1306 imx185_probe_fail:
1307         imx185_power_put(&info->power);
1308
1309         return err;
1310 }
1311
1312 static int
1313 imx185_remove(struct i2c_client *client)
1314 {
1315         struct imx185_info *info;
1316         info = i2c_get_clientdata(client);
1317         misc_deregister(&imx185_device);
1318
1319         imx185_power_put(&info->power);
1320
1321         imx185_remove_debugfs(info);
1322         return 0;
1323 }
1324
1325 static const struct i2c_device_id imx185_id[] = {
1326         { "imx185", 0 },
1327         { }
1328 };
1329
1330 MODULE_DEVICE_TABLE(i2c, imx185_id);
1331
1332 static struct i2c_driver imx185_i2c_driver = {
1333         .driver = {
1334                 .name = "imx185",
1335                 .owner = THIS_MODULE,
1336                 .of_match_table = imx185_of_match,
1337         },
1338         .probe = imx185_probe,
1339         .remove = imx185_remove,
1340         .id_table = imx185_id,
1341 };
1342
1343 static int __init imx185_init(void)
1344 {
1345         pr_info("[IMX185] sensor driver loading\n");
1346         return i2c_add_driver(&imx185_i2c_driver);
1347 }
1348
1349 static void __exit imx185_exit(void)
1350 {
1351         i2c_del_driver(&imx185_i2c_driver);
1352 }
1353
1354 module_init(imx185_init);
1355 module_exit(imx185_exit);