]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/media/platform/tegra/ar0330.c
kernel: add ar0330 2304x1520 resolution
[sojka/nv-tegra/linux-3.10.git] / drivers / media / platform / tegra / ar0330.c
1 /*
2  * ar0330.c - ar0330 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/ar0330.h>
29 #include <linux/gpio.h>
30 #include <linux/module.h>
31
32 #include <linux/kernel.h>
33 #include <linux/seq_file.h>
34 #include <linux/of.h>
35 #include <linux/of_device.h>
36 #include <linux/of_gpio.h>
37
38 #include "nvc_utilities.h"
39
40 struct ar0330_reg {
41         u16 addr;
42         u16 val;
43 };
44
45 struct ar0330_info {
46         struct miscdevice               miscdev_info;
47         int                             mode;
48         struct ar0330_power_rail        power;
49         struct ar0330_sensordata        sensor_data;
50         struct i2c_client               *i2c_client;
51         struct ar0330_platform_data     *pdata;
52         struct clk                      *mclk;
53         struct regmap                   *regmap;
54         struct mutex                    ar0330_camera_lock;
55         atomic_t                        in_use;
56         char devname[16];
57 };
58
59 static const struct regmap_config sensor_regmap_config = {
60         .reg_bits = 16,
61         .val_bits = 16,
62         .cache_type = REGCACHE_RBTREE,
63 };
64
65 #define AR0330_TABLE_WAIT_MS 0
66 #define AR0330_TABLE_END 1
67 #define AR0330_MAX_RETRIES 3
68 #define AR0330_WAIT_MS 100
69
70 #define MAX_BUFFER_SIZE 32
71 #define AR0330_FRAME_LENGTH_ADDR 0x300A
72 #define AR0330_COARSE_TIME_ADDR 0x3012
73 #define AR0330_GAIN_ADDR 0x3060
74
75 static struct ar0330_reg mode_2304x1536[] = {
76         {0x3052, 0xa114},
77         {0x304A, 0x0070},
78         {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
79         {0x301A, 0x0058},
80         {0x302A, 0x0005},
81         {0x302C, 0x0004},
82         {0x302E, 0x0003},
83         {0x3030, 0x005F},
84         {0x3036, 0x000A},
85         {0x3038, 0x0001},
86         {0x31AC, 0x0A0A},
87         {0x31AE, 0x0201},
88         {0x31B0, 0x003D},
89         {0x31B2, 0x0018},
90         {0x31B4, 0x4F56},
91         {0x31B6, 0x4214},
92         {0x31B8, 0x308B},
93         {0x31BA, 0x028A},
94         {0x31BC, 0x8008},
95         {0x3002, 0x0006},
96         {0x3004, 0x0006},
97         {0x3006, 0x0605},
98         {0x3008, 0x0905},
99         {0x300A, 0x0611},
100         {0x300C, 0x04E0},
101         {0x3012, 0x0610},
102         {0x3014, 0x0000},
103         {0x30A2, 0x0001},
104         {0x30A6, 0x0001},
105         {0x3040, 0x0000},
106         {0x3042, 0x0000},
107         {0x30BA, 0x006C},
108         {0x31E0, 0x0303},
109         {0x3064, 0x1802},
110         {0x3ED2, 0x0146},
111         {0x3ED4, 0x8F6C},
112         {0x3ED6, 0x66CC},
113         {0x3ED8, 0x8C42},
114         {0x3EDA, 0x88BC},
115         {0x3EDC, 0xAA63},
116         {0x305E, 0x00A0},
117         {0x3088, 0x80BA},
118         {0x3086, 0x0253},
119         {0x30CE, 0x0010},
120         {0x301A, 0x035C},
121         {AR0330_TABLE_END, 0x00}
122 };
123
124 static struct ar0330_reg mode_1280x720[] = {
125         {0x301A, 0x0059},
126         {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
127         {0x31AE, 0x0204},
128         {0x301A, 0x0059},
129         {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
130         {0x301A, 0x0058},
131         {0x3064, 0x1802},
132         {0x3078, 0x0001},
133         {0x30BA, 0x002C},
134         {0x30FE, 0x0080},
135         {0x31E0, 0x0003},
136         {0x3ECE, 0x09FF},
137         {0x3ED0, 0xE4F6},
138         {0x3ED2, 0x0146},
139         {0x3ED4, 0x8F6C},
140         {0x3ED6, 0x66CC},
141         {0x3ED8, 0x8C42},
142         {0x3EDA, 0x889B},
143         {0x3EDC, 0x8863},
144         {0x3EDE, 0xAA04},
145         {0x3EE0, 0x15F0},
146         {0x3EE6, 0x008C},
147         {0x3EE8, 0x2024},
148         {0x3EEA, 0xFF1F},
149         {0x3F06, 0x046A},
150         {0x3046, 0x4038},
151         {0x3048, 0x8480},
152         {0x31E0, 0x0003},
153         {0x301A, 0x0058},
154         {0x31AE, 0x0201},
155         {0x31AC, 0x0A0A},
156         {0x31B0, 0x0028},
157         {0x31B2, 0x000E},
158         {0x31B4, 0x2743},
159         {0x31B6, 0x114E},
160         {0x31B8, 0x2049},
161         {0x31BA, 0x0186},
162         {0x31BC, 0x8005},
163         {0x31BE, 0x2003},
164         {0x302A, 0x0005},
165         {0x302C, 0x0004},
166         {0x302E, 0x0004},
167         {0x3030, 0x0052},
168         {0x3036, 0x000A},
169         {0x3038, 0x0001},
170         {0x31AC, 0x0A0A},
171         {0x3004, 0x0200},
172         {0x3008, 0x06FF},
173         {0x3002, 0x019C},
174         {0x3006, 0x046B},
175         {0x30A2, 0x0001},
176         {0x30A6, 0x0001},
177         {0x3040, 0x0000},
178         {0x300C, 0x03F6},
179         {0x300A, 0x0328},
180         {0x3014, 0x0000},
181         {0x3012, 0x0327},
182         {0x3042, 0x02B0},
183         {0x30BA, 0x002C},
184         {0x301A, 0x0058},
185         {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
186         {0x3088, 0x8000},
187         {0x3086, 0x4A03},
188         {0x3086, 0x4316},
189         {0x3086, 0x0443},
190         {0x3086, 0x1645},
191         {0x3086, 0x4045},
192         {0x3086, 0x6017},
193         {0x3086, 0x2045},
194         {0x3086, 0x404B},
195         {0x3086, 0x1244},
196         {0x3086, 0x6134},
197         {0x3086, 0x4A31},
198         {0x3086, 0x4342},
199         {0x3086, 0x4560},
200         {0x3086, 0x2714},
201         {0x3086, 0x3DFF},
202         {0x3086, 0x3DFF},
203         {0x3086, 0x3DEA},
204         {0x3086, 0x2704},
205         {0x3086, 0x3D10},
206         {0x3086, 0x2705},
207         {0x3086, 0x3D10},
208         {0x3086, 0x2715},
209         {0x3086, 0x3527},
210         {0x3086, 0x053D},
211         {0x3086, 0x1045},
212         {0x3086, 0x4027},
213         {0x3086, 0x0427},
214         {0x3086, 0x143D},
215         {0x3086, 0xFF3D},
216         {0x3086, 0xFF3D},
217         {0x3086, 0xEA62},
218         {0x3086, 0x2728},
219         {0x3086, 0x3627},
220         {0x3086, 0x083D},
221         {0x3086, 0x6444},
222         {0x3086, 0x2C2C},
223         {0x3086, 0x2C2C},
224         {0x3086, 0x4B01},
225         {0x3086, 0x432D},
226         {0x3086, 0x4643},
227         {0x3086, 0x1647},
228         {0x3086, 0x435F},
229         {0x3086, 0x4F50},
230         {0x3086, 0x2604},
231         {0x3086, 0x2684},
232         {0x3086, 0x2027},
233         {0x3086, 0xFC53},
234         {0x3086, 0x0D5C},
235         {0x3086, 0x0D57},
236         {0x3086, 0x5417},
237         {0x3086, 0x0955},
238         {0x3086, 0x5649},
239         {0x3086, 0x5307},
240         {0x3086, 0x5302},
241         {0x3086, 0x4D28},
242         {0x3086, 0x6C4C},
243         {0x3086, 0x0928},
244         {0x3086, 0x2C28},
245         {0x3086, 0x294E},
246         {0x3086, 0x5C09},
247         {0x3086, 0x6045},
248         {0x3086, 0x0045},
249         {0x3086, 0x8026},
250         {0x3086, 0xA627},
251         {0x3086, 0xF817},
252         {0x3086, 0x0227},
253         {0x3086, 0xFA5C},
254         {0x3086, 0x0B17},
255         {0x3086, 0x1826},
256         {0x3086, 0xA25C},
257         {0x3086, 0x0317},
258         {0x3086, 0x4427},
259         {0x3086, 0xF25F},
260         {0x3086, 0x2809},
261         {0x3086, 0x1714},
262         {0x3086, 0x2808},
263         {0x3086, 0x1701},
264         {0x3086, 0x4D1A},
265         {0x3086, 0x2683},
266         {0x3086, 0x1701},
267         {0x3086, 0x27FA},
268         {0x3086, 0x45A0},
269         {0x3086, 0x1707},
270         {0x3086, 0x27FB},
271         {0x3086, 0x1729},
272         {0x3086, 0x4580},
273         {0x3086, 0x1708},
274         {0x3086, 0x27FA},
275         {0x3086, 0x1728},
276         {0x3086, 0x5D17},
277         {0x3086, 0x0E26},
278         {0x3086, 0x8153},
279         {0x3086, 0x0117},
280         {0x3086, 0xE653},
281         {0x3086, 0x0217},
282         {0x3086, 0x1026},
283         {0x3086, 0x8326},
284         {0x3086, 0x8248},
285         {0x3086, 0x4D4E},
286         {0x3086, 0x2809},
287         {0x3086, 0x4C0B},
288         {0x3086, 0x6017},
289         {0x3086, 0x2027},
290         {0x3086, 0xF217},
291         {0x3086, 0x535F},
292         {0x3086, 0x2808},
293         {0x3086, 0x164D},
294         {0x3086, 0x1A17},
295         {0x3086, 0x0127},
296         {0x3086, 0xFA26},
297         {0x3086, 0x035C},
298         {0x3086, 0x0145},
299         {0x3086, 0x4027},
300         {0x3086, 0x9817},
301         {0x3086, 0x2A4A},
302         {0x3086, 0x0A43},
303         {0x3086, 0x160B},
304         {0x3086, 0x4327},
305         {0x3086, 0x9C45},
306         {0x3086, 0x6017},
307         {0x3086, 0x0727},
308         {0x3086, 0x9D17},
309         {0x3086, 0x2545},
310         {0x3086, 0x4017},
311         {0x3086, 0x0827},
312         {0x3086, 0x985D},
313         {0x3086, 0x2645},
314         {0x3086, 0x4B17},
315         {0x3086, 0x0A28},
316         {0x3086, 0x0853},
317         {0x3086, 0x0D52},
318         {0x3086, 0x5112},
319         {0x3086, 0x4460},
320         {0x3086, 0x184A},
321         {0x3086, 0x0343},
322         {0x3086, 0x1604},
323         {0x3086, 0x4316},
324         {0x3086, 0x5843},
325         {0x3086, 0x1659},
326         {0x3086, 0x4316},
327         {0x3086, 0x5A43},
328         {0x3086, 0x165B},
329         {0x3086, 0x4327},
330         {0x3086, 0x9C45},
331         {0x3086, 0x6017},
332         {0x3086, 0x0727},
333         {0x3086, 0x9D17},
334         {0x3086, 0x2545},
335         {0x3086, 0x4017},
336         {0x3086, 0x1027},
337         {0x3086, 0x9817},
338         {0x3086, 0x2022},
339         {0x3086, 0x4B12},
340         {0x3086, 0x442C},
341         {0x3086, 0x2C2C},
342         {0x3086, 0x2C00},
343         {0x3086, 0x0000},
344         {0x3086, 0x0000},
345         {0x3086, 0x0000},
346         {0x3086, 0x0000},
347         {0x3086, 0x0000},
348         {0x3086, 0x0000},
349         {0x3086, 0x0000},
350         {0x3086, 0x0000},
351         {0x3086, 0x0000},
352         {0x3086, 0x0000},
353         {0x3086, 0x0000},
354         {0x3086, 0x0000},
355         {0x3086, 0x0000},
356         {0x3086, 0x0000},
357         {0x3086, 0x0000},
358         {0x3086, 0x0000},
359         {0x3086, 0x0000},
360         {0x3086, 0x0000},
361         {0x3086, 0x0000},
362         {0x3086, 0x0000},
363         {0x3086, 0x0000},
364         {0x3086, 0x0000},
365         {0x3086, 0x0000},
366         {0x3086, 0x0000},
367         {0x3086, 0x0000},
368         {0x3086, 0x0000},
369         {0x3086, 0x0000},
370         {0x3086, 0x0000},
371         {0x3086, 0x0000},
372         {0x3086, 0x0000},
373         {0x3086, 0x0000},
374         {0x3086, 0x0000},
375         {0x3086, 0x0000},
376         {0x3086, 0x0000},
377         {0x3086, 0x0000},
378         {0x3086, 0x0000},
379         {0x3086, 0x0000},
380         {0x3086, 0x0000},
381         {0x3086, 0x0000},
382         {0x3086, 0x0000},
383         {0x3086, 0x0000},
384         {0x3086, 0x0000},
385         {0x3086, 0x0000},
386         {0x3086, 0x0000},
387         {0x3086, 0x0000},
388         {0x3086, 0x0000},
389         {0x3086, 0x0000},
390         {0x3086, 0x0000},
391         {0x3086, 0x0000},
392         {0x3086, 0x0000},
393         {0x3086, 0x0000},
394         {0x3086, 0x0000},
395         {0x3086, 0x0000},
396         {0x3086, 0x0000},
397         {0x3086, 0x0000},
398         {0x3086, 0x0000},
399         {0x3086, 0x0000},
400         {0x3086, 0x0000},
401         {0x3086, 0x0000},
402         {0x3086, 0x0000},
403         {0x3086, 0x0000},
404         {0x3086, 0x0000},
405         {0x3086, 0x0000},
406         {0x3086, 0x0000},
407         {0x3086, 0x0000},
408         {0x3086, 0x0000},
409         {0x3086, 0x0000},
410         {0x3086, 0x0000},
411         {0x3086, 0x0000},
412         {0x3086, 0x0000},
413         {0x3086, 0x0000},
414         {0x3086, 0x0000},
415         {0x3086, 0x0000},
416         {0x3086, 0x0000},
417         {0x3086, 0x0000},
418         {0x3086, 0x0000},
419         {0x3086, 0x0000},
420         {0x3086, 0x0000},
421         {0x3086, 0x0000},
422         {0x3086, 0x0000},
423         {0x3086, 0x0000},
424         {0x3086, 0x0000},
425         {0x3086, 0x0000},
426         {0x3086, 0x0000},
427         {0x3086, 0x0000},
428         {0x3086, 0x0000},
429         {0x3086, 0x0000},
430         {0x3086, 0x0000},
431         {0x3086, 0x0000},
432         {0x3086, 0x0000},
433         {0x3086, 0x0000},
434         {0x3086, 0x0000},
435         {0x3086, 0x0000},
436         {0x3086, 0x0000},
437         {0x3086, 0x0000},
438         {0x3086, 0x0000},
439         {0x3086, 0x0000},
440         {0x3086, 0x0000},
441         {0x3086, 0x0000},
442         {0x3086, 0x0000},
443         /* stream on */
444         {0x301A, 0x0058},
445         {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
446         {0x3088, 0x80BA},
447         {0x3086, 0x0253},
448         {0x30CE, 0x0010},
449         {0x301A, 0x015C},
450         {AR0330_TABLE_END, 0x00}
451 };
452
453 static struct ar0330_reg mode_1280x960[] = {
454         {0x3052, 0xa114},
455         {0x304A, 0x0070},
456         {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
457         {0x301A, 0x0058},
458         {0x302A, 0x0005},
459         {0x302C, 0x0004},
460         {0x302E, 0x0003},
461         {0x3030, 0x005F},
462         {0x3036, 0x000A},
463         {0x3038, 0x0001},
464         {0x31AC, 0x0A0A},
465         {0x31AE, 0x0201},
466         {0x31B0, 0x003D},
467         {0x31B2, 0x0018},
468         {0x31B4, 0x4F56},
469         {0x31B6, 0x4214},
470         {0x31B8, 0x308B},
471         {0x31BA, 0x028A},
472         {0x31BC, 0x8008},
473         {0x3002, 0x0126},
474         {0x3004, 0x0206},
475         {0x3006, 0x04E5},
476         {0x3008, 0x0705},
477         {0x300A, 0x0449},
478         {0x300C, 0x0482},
479         {0x3012, 0x0448},
480         {0x3014, 0x0000},
481         {0x30A2, 0x0001},
482         {0x30A6, 0x0001},
483         {0x3040, 0x0000},
484         {0x3042, 0x0000},
485         {0x30BA, 0x006C},
486         {0x31E0, 0x0303},
487         {0x3064, 0x1802},
488         {0x3ED2, 0x0146},
489         {0x3ED4, 0x8F6C},
490         {0x3ED6, 0x66CC},
491         {0x3ED8, 0x8C42},
492         {0x3EDA, 0x88BC},
493         {0x3EDC, 0xAA63},
494         {0x305E, 0x00A0},
495         {0x3088, 0x80BA},
496         {0x3086, 0x0253},
497         {0x30CE, 0x0010},
498         {0x301A, 0x035C},
499         {AR0330_TABLE_END, 0x00}
500 };
501
502 enum {
503         AR0330_MODE_2304X1536,
504         AR0330_MODE_1280X720,
505         AR0330_MODE_1280X960,
506 };
507
508 static struct ar0330_reg *mode_table[] = {
509         [AR0330_MODE_2304X1536] = mode_2304x1536,
510         [AR0330_MODE_1280X720] = mode_1280x720,
511         [AR0330_MODE_1280X960] = mode_1280x960,
512 };
513
514 static inline void
515 msleep_range(unsigned int delay_base)
516 {
517         usleep_range(delay_base*1000, delay_base*1000+500);
518 }
519
520 static inline void
521 ar0330_get_frame_length_regs(struct ar0330_reg *regs, u32 frame_length)
522 {
523         regs->addr = AR0330_FRAME_LENGTH_ADDR;
524         regs->val = frame_length & 0xffff;
525 }
526
527 static inline void
528 ar0330_get_coarse_time_regs(struct ar0330_reg *regs, u32 coarse_time)
529 {
530         regs->addr = AR0330_COARSE_TIME_ADDR;
531         regs->val = coarse_time & 0xffff;
532 }
533
534 static inline void
535 ar0330_get_gain_reg(struct ar0330_reg *regs, u16 gain)
536 {
537         regs->addr = AR0330_GAIN_ADDR;
538         regs->val = gain;
539 }
540
541 static inline int
542 ar0330_read_reg(struct ar0330_info *info, u16 addr, u16 *val)
543 {
544         return regmap_read(info->regmap, addr, (unsigned int *) val);
545 }
546
547 static int
548 ar0330_write_reg(struct ar0330_info *info, u16 addr, u16 val)
549 {
550         int err;
551
552         err = regmap_write(info->regmap, addr, val);
553
554         if (err)
555                 pr_err("%s:i2c write failed, %x = %x\n",
556                         __func__, addr, val);
557
558         return err;
559 }
560
561 static int
562 ar0330_write_table(struct ar0330_info *info,
563                                  const struct ar0330_reg table[],
564                                  const struct ar0330_reg override_list[],
565                                  int num_override_regs)
566 {
567         int err;
568         const struct ar0330_reg *next;
569         int i;
570         u16 val;
571
572         for (next = table; next->addr != AR0330_TABLE_END; next++) {
573                 if (next->addr == AR0330_TABLE_WAIT_MS) {
574                         msleep_range(next->val);
575                         continue;
576                 }
577
578                 val = next->val;
579
580                 /* When an override list is passed in, replace the reg */
581                 /* value to write if the reg is in the list            */
582                 if (override_list) {
583                         for (i = 0; i < num_override_regs; i++) {
584                                 if (next->addr == override_list[i].addr) {
585                                         val = override_list[i].val;
586                                         break;
587                                 }
588                         }
589                 }
590
591                 err = ar0330_write_reg(info, next->addr, val);
592                 if (err) {
593                         pr_err("%s:ar0330_write_table:%d", __func__, err);
594                         return err;
595                 }
596         }
597         return 0;
598 }
599
600 static int ar0330_get_flash_cap(struct ar0330_info *info)
601 {
602         struct ar0330_flash_control *fctl;
603
604         dev_dbg(&info->i2c_client->dev, "%s: %p\n", __func__, info->pdata);
605         if (info->pdata) {
606                 fctl = &info->pdata->flash_cap;
607                 dev_dbg(&info->i2c_client->dev,
608                         "edg: %x, st: %x, rpt: %x, dl: %x\n",
609                         fctl->edge_trig_en,
610                         fctl->start_edge,
611                         fctl->repeat,
612                         fctl->delay_frm);
613
614                 if (fctl->enable)
615                         return 0;
616         }
617         return -ENODEV;
618 }
619
620 static inline int ar0330_set_flash_control(
621         struct ar0330_info *info, struct ar0330_flash_control *fc)
622 {
623         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
624         return 0;
625 }
626
627 static int
628 ar0330_set_mode(struct ar0330_info *info, struct ar0330_mode *mode)
629 {
630         int sensor_mode;
631         int err;
632         struct ar0330_reg reg_list[8];
633
634         pr_info("%s: xres %u yres %u framelength %u coarsetime %u gain %u\n",
635                          __func__, mode->xres, mode->yres, mode->frame_length,
636                          mode->coarse_time, mode->gain);
637
638         if (mode->xres == 2304 && mode->yres == 1520) {
639                 sensor_mode = AR0330_MODE_2304X1536;
640         } else if (mode->xres == 1280 && mode->yres == 720) {
641                 sensor_mode = AR0330_MODE_1280X720;
642         } else if (mode->xres == 1280 && mode->yres == 960) {
643                 sensor_mode = AR0330_MODE_1280X960;
644         } else {
645                 pr_err("%s: invalid resolution supplied to set mode %d %d\n",
646                          __func__, mode->xres, mode->yres);
647                 return -EINVAL;
648         }
649
650         /* get a list of override regs for the asking frame length, */
651         /* coarse integration time, and gain.                       */
652         ar0330_get_frame_length_regs(reg_list, mode->frame_length);
653         ar0330_get_coarse_time_regs(reg_list + 1, mode->coarse_time);
654         ar0330_get_gain_reg(reg_list + 2, mode->gain);
655
656         err = ar0330_write_table(info,
657                                 mode_table[sensor_mode],
658                                 reg_list, 3);
659         if (err)
660                 return err;
661         info->mode = sensor_mode;
662         pr_info("[AR0330]: stream on.\n");
663         return 0;
664 }
665
666 static int
667 ar0330_get_status(struct ar0330_info *info, u8 *dev_status)
668 {
669         *dev_status = 0;
670         return 0;
671 }
672
673 static int
674 ar0330_set_frame_length(struct ar0330_info *info, u32 frame_length,
675                                                  bool group_hold)
676 {
677         struct ar0330_reg reg_list[2];
678         int i = 0;
679         int ret;
680
681         ar0330_get_frame_length_regs(reg_list, frame_length);
682
683         for (i = 0; i < 1; i++) {
684                 ret = ar0330_write_reg(info, reg_list[i].addr,
685                          reg_list[i].val);
686                 if (ret)
687                         return ret;
688         }
689
690         return 0;
691 }
692
693 static int
694 ar0330_set_coarse_time(struct ar0330_info *info, u32 coarse_time,
695                                                  bool group_hold)
696 {
697         int ret;
698
699         struct ar0330_reg reg_list[2];
700         int i = 0;
701
702         ar0330_get_coarse_time_regs(reg_list, coarse_time);
703
704         for (i = 0; i < 1; i++) {
705                 ret = ar0330_write_reg(info, reg_list[i].addr,
706                          reg_list[i].val);
707                 if (ret)
708                         return ret;
709         }
710
711         return 0;
712 }
713
714 static int
715 ar0330_set_gain(struct ar0330_info *info, u16 gain, bool group_hold)
716 {
717         int ret;
718         struct ar0330_reg reg_list;
719
720         ar0330_get_gain_reg(&reg_list, gain);
721
722         ret = ar0330_write_reg(info, reg_list.addr, reg_list.val);
723         if (ret)
724                 return ret;
725
726         return 0;
727 }
728
729 static int
730 ar0330_set_group_hold(struct ar0330_info *info, struct ar0330_ae *ae)
731 {
732         int count = 0;
733         bool group_hold_enabled = false;
734
735         if (ae->gain_enable)
736                 count++;
737         if (ae->coarse_time_enable)
738                 count++;
739         if (ae->frame_length_enable)
740                 count++;
741         if (count >= 2)
742                 group_hold_enabled = true;
743
744         if (ae->gain_enable)
745                 ar0330_set_gain(info, ae->gain, false);
746         if (ae->coarse_time_enable)
747                 ar0330_set_coarse_time(info, ae->coarse_time, false);
748         if (ae->frame_length_enable)
749                 ar0330_set_frame_length(info, ae->frame_length, false);
750
751         return 0;
752 }
753
754 static int ar0330_get_sensor_id(struct ar0330_info *info)
755 {
756         int ret = 0;
757
758         pr_info("%s\n", __func__);
759         if (info->sensor_data.fuse_id_size)
760                 return 0;
761
762         /* Note 1: If the sensor does not have power at this point
763         Need to supply the power, e.g. by calling power on function */
764
765         /*ret |= ar0330_write_reg(info, 0x3B02, 0x00);
766         ret |= ar0330_write_reg(info, 0x3B00, 0x01);
767         for (i = 0; i < 9; i++) {
768                 ret |= ar0330_read_reg(info, 0x3B24 + i, &bak);
769                 info->sensor_data.fuse_id[i] = bak;
770         }
771
772         if (!ret)
773                 info->sensor_data.fuse_id_size = i;*/
774
775         /* Note 2: Need to clean up any action carried out in Note 1 */
776
777         return ret;
778 }
779
780 static void ar0330_mclk_disable(struct ar0330_info *info)
781 {
782         dev_dbg(&info->i2c_client->dev, "%s: disable MCLK\n", __func__);
783         clk_disable_unprepare(info->mclk);
784 }
785
786 static int ar0330_mclk_enable(struct ar0330_info *info)
787 {
788         int err;
789         unsigned long mclk_init_rate = 24000000;
790
791         dev_dbg(&info->i2c_client->dev, "%s: enable MCLK with %lu Hz\n",
792                 __func__, mclk_init_rate);
793
794         err = clk_set_rate(info->mclk, mclk_init_rate);
795         if (!err)
796                 err = clk_prepare_enable(info->mclk);
797         return err;
798 }
799
800 static long
801 ar0330_ioctl(struct file *file,
802                          unsigned int cmd, unsigned long arg)
803 {
804         int err = 0;
805         struct ar0330_info *info = file->private_data;
806
807         switch (cmd) {
808         case AR0330_IOCTL_SET_POWER:
809                 if (!info->pdata)
810                         break;
811                 if (arg && info->pdata->power_on) {
812                         err = ar0330_mclk_enable(info);
813                         if (!err)
814                                 err = info->pdata->power_on(&info->power);
815                         if (err < 0)
816                                 ar0330_mclk_disable(info);
817                 }
818                 if (!arg && info->pdata->power_off) {
819                         info->pdata->power_off(&info->power);
820                         ar0330_mclk_disable(info);
821                 }
822                 break;
823         case AR0330_IOCTL_SET_MODE:
824         {
825                 struct ar0330_mode mode;
826                 if (copy_from_user(&mode, (const void __user *)arg,
827                         sizeof(struct ar0330_mode))) {
828                         pr_err("%s:Failed to get mode from user.\n", __func__);
829                         return -EFAULT;
830                 }
831                 return ar0330_set_mode(info, &mode);
832         }
833         case AR0330_IOCTL_SET_FRAME_LENGTH:
834                 return ar0330_set_frame_length(info, (u32)arg, true);
835         case AR0330_IOCTL_SET_COARSE_TIME:
836                 return ar0330_set_coarse_time(info, (u32)arg, true);
837         case AR0330_IOCTL_SET_GAIN:
838                 return ar0330_set_gain(info, (u16)arg, true);
839         case AR0330_IOCTL_GET_STATUS:
840         {
841                 u8 status;
842
843                 err = ar0330_get_status(info, &status);
844                 if (err)
845                         return err;
846                 if (copy_to_user((void __user *)arg, &status, 1)) {
847                         pr_err("%s:Failed to copy status to user\n", __func__);
848                         return -EFAULT;
849                 }
850                 return 0;
851         }
852         case AR0330_IOCTL_GET_SENSORDATA:
853         {
854                 err = ar0330_get_sensor_id(info);
855
856                 if (err) {
857                         pr_err("%s:Failed to get fuse id info.\n", __func__);
858                         return err;
859                 }
860                 if (copy_to_user((void __user *)arg, &info->sensor_data,
861                                 sizeof(struct ar0330_sensordata))) {
862                         pr_info("%s:Failed to copy fuse id to user space\n",
863                                 __func__);
864                         return -EFAULT;
865                 }
866                 return 0;
867         }
868         case AR0330_IOCTL_SET_GROUP_HOLD:
869         {
870                 struct ar0330_ae ae;
871                 if (copy_from_user(&ae, (const void __user *)arg,
872                         sizeof(struct ar0330_ae))) {
873                         pr_info("%s:fail group hold\n", __func__);
874                         return -EFAULT;
875                 }
876                 return ar0330_set_group_hold(info, &ae);
877         }
878         case AR0330_IOCTL_SET_FLASH_MODE:
879         {
880                 struct ar0330_flash_control values;
881
882                 dev_dbg(&info->i2c_client->dev,
883                         "AR0330_IOCTL_SET_FLASH_MODE\n");
884                 if (copy_from_user(&values,
885                         (const void __user *)arg,
886                         sizeof(struct ar0330_flash_control))) {
887                         err = -EFAULT;
888                         break;
889                 }
890                 err = ar0330_set_flash_control(info, &values);
891                 break;
892         }
893         case AR0330_IOCTL_GET_FLASH_CAP:
894                 err = ar0330_get_flash_cap(info);
895                 break;
896         default:
897                 pr_err("%s:unknown cmd.\n", __func__);
898                 err = -EINVAL;
899         }
900
901         return err;
902 }
903
904 static int ar0330_power_on(struct ar0330_power_rail *pw)
905 {
906         int err;
907         struct ar0330_info *info = container_of(pw, struct ar0330_info, power);
908
909         if (unlikely(WARN_ON(!pw || !pw->iovdd || !pw->avdd || !pw->dvdd)))
910                 return -EFAULT;
911
912         gpio_set_value(info->pdata->cam2_gpio, 0);
913         usleep_range(10, 20);
914
915         err = regulator_enable(pw->avdd);
916         if (err)
917                 goto ar0330_avdd_fail;
918
919         err = regulator_enable(pw->dvdd);
920         if (err)
921                 goto ar0330_dvdd_fail;
922
923         err = regulator_enable(pw->iovdd);
924         if (err)
925                 goto ar0330_iovdd_fail;
926
927         usleep_range(1, 2);
928         gpio_set_value(info->pdata->cam2_gpio, 1);
929
930         usleep_range(300, 310);
931
932         return 1;
933
934
935 ar0330_iovdd_fail:
936         regulator_disable(pw->dvdd);
937
938 ar0330_dvdd_fail:
939         regulator_disable(pw->avdd);
940
941 ar0330_avdd_fail:
942         pr_err("%s failed.\n", __func__);
943         return -ENODEV;
944 }
945
946 static int ar0330_power_off(struct ar0330_power_rail *pw)
947 {
948         struct ar0330_info *info = container_of(pw, struct ar0330_info, power);
949
950         if (unlikely(WARN_ON(!pw || !pw->iovdd || !pw->avdd || !pw->dvdd)))
951                 return -EFAULT;
952
953         usleep_range(1, 2);
954         gpio_set_value(info->pdata->cam2_gpio, 0);
955         usleep_range(1, 2);
956
957         regulator_disable(pw->iovdd);
958         regulator_disable(pw->dvdd);
959         regulator_disable(pw->avdd);
960
961         return 0;
962 }
963
964 static int
965 ar0330_open(struct inode *inode, struct file *file)
966 {
967         struct miscdevice       *miscdev = file->private_data;
968         struct ar0330_info *info;
969
970         info = container_of(miscdev, struct ar0330_info, miscdev_info);
971         /* check if the device is in use */
972         if (atomic_xchg(&info->in_use, 1)) {
973                 pr_info("%s:BUSY!\n", __func__);
974                 return -EBUSY;
975         }
976
977         file->private_data = info;
978
979         return 0;
980 }
981
982 static int
983 ar0330_release(struct inode *inode, struct file *file)
984 {
985         struct ar0330_info *info = file->private_data;
986
987         file->private_data = NULL;
988
989         /* warn if device is already released */
990         WARN_ON(!atomic_xchg(&info->in_use, 0));
991         return 0;
992 }
993
994 static int ar0330_power_put(struct ar0330_power_rail *pw)
995 {
996         if (unlikely(!pw))
997                 return -EFAULT;
998
999         if (likely(pw->avdd))
1000                 regulator_put(pw->avdd);
1001
1002         if (likely(pw->iovdd))
1003                 regulator_put(pw->iovdd);
1004
1005         if (likely(pw->dvdd))
1006                 regulator_put(pw->dvdd);
1007
1008         pw->avdd = NULL;
1009         pw->iovdd = NULL;
1010         pw->dvdd = NULL;
1011
1012         return 0;
1013 }
1014
1015 static int ar0330_regulator_get(struct ar0330_info *info,
1016         struct regulator **vreg, char vreg_name[])
1017 {
1018         struct regulator *reg = NULL;
1019         int err = 0;
1020
1021         reg = regulator_get(&info->i2c_client->dev, vreg_name);
1022         if (unlikely(IS_ERR(reg))) {
1023                 dev_err(&info->i2c_client->dev, "%s %s ERR: %d\n",
1024                         __func__, vreg_name, (int)reg);
1025                 err = PTR_ERR(reg);
1026                 reg = NULL;
1027         } else
1028                 dev_dbg(&info->i2c_client->dev, "%s: %s\n",
1029                         __func__, vreg_name);
1030
1031         *vreg = reg;
1032         return err;
1033 }
1034
1035 static int ar0330_power_get(struct ar0330_info *info)
1036 {
1037         struct ar0330_power_rail *pw = &info->power;
1038         int err = 0;
1039
1040         err |= ar0330_regulator_get(info, &pw->avdd, "vana"); /* ananlog 2.7v */
1041         err |= ar0330_regulator_get(info, &pw->dvdd, "vdig"); /* digital 1.2v */
1042         err |= ar0330_regulator_get(info, &pw->iovdd, "vif"); /* IO 1.8v */
1043
1044         return err;
1045 }
1046
1047 static const struct file_operations ar0330_fileops = {
1048         .owner = THIS_MODULE,
1049         .open = ar0330_open,
1050         .unlocked_ioctl = ar0330_ioctl,
1051         .release = ar0330_release,
1052 };
1053
1054 static struct miscdevice ar0330_device = {
1055         .minor = MISC_DYNAMIC_MINOR,
1056         .name = "ar0330",
1057         .fops = &ar0330_fileops,
1058 };
1059
1060 static struct of_device_id ar0330_of_match[] = {
1061         { .compatible = "nvidia,ar0330", },
1062         { },
1063 };
1064
1065 MODULE_DEVICE_TABLE(of, ar0330_of_match);
1066
1067 static struct ar0330_platform_data *ar0330_parse_dt(struct i2c_client *client)
1068 {
1069         struct device_node *np = client->dev.of_node;
1070         struct ar0330_platform_data *board_info_pdata;
1071         const struct of_device_id *match;
1072
1073         match = of_match_device(ar0330_of_match, &client->dev);
1074         if (!match) {
1075                 dev_err(&client->dev, "Failed to find matching dt id\n");
1076                 return NULL;
1077         }
1078
1079         board_info_pdata = devm_kzalloc(&client->dev, sizeof(*board_info_pdata),
1080                         GFP_KERNEL);
1081         if (!board_info_pdata) {
1082                 dev_err(&client->dev, "Failed to allocate pdata\n");
1083                 return NULL;
1084         }
1085
1086         board_info_pdata->cam2_gpio = of_get_named_gpio(np, "cam1-gpios", 0);
1087         board_info_pdata->ext_reg = of_property_read_bool(np, "nvidia,ext_reg");
1088
1089         board_info_pdata->power_on = ar0330_power_on;
1090         board_info_pdata->power_off = ar0330_power_off;
1091
1092         return board_info_pdata;
1093 }
1094
1095 static int
1096 ar0330_probe(struct i2c_client *client,
1097                         const struct i2c_device_id *id)
1098 {
1099         struct ar0330_info *info;
1100         int err;
1101         const char *mclk_name;
1102
1103         pr_err("[AR0330]: probing sensor.\n");
1104
1105         info = devm_kzalloc(&client->dev,
1106                         sizeof(struct ar0330_info), GFP_KERNEL);
1107         if (!info) {
1108                 pr_err("%s:Unable to allocate memory!\n", __func__);
1109                 return -ENOMEM;
1110         }
1111
1112         info->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
1113         if (IS_ERR(info->regmap)) {
1114                 dev_err(&client->dev,
1115                         "regmap init failed: %ld\n", PTR_ERR(info->regmap));
1116                 return -ENODEV;
1117         }
1118
1119         if (client->dev.of_node)
1120                 info->pdata = ar0330_parse_dt(client);
1121         else
1122                 info->pdata = client->dev.platform_data;
1123
1124         if (!info->pdata) {
1125                 pr_err("[AR0330]:%s:Unable to get platform data\n", __func__);
1126                 return -EFAULT;
1127         }
1128
1129         info->i2c_client = client;
1130         atomic_set(&info->in_use, 0);
1131         info->mode = -1;
1132
1133         mclk_name = info->pdata->mclk_name ?
1134                     info->pdata->mclk_name : "default_mclk";
1135         info->mclk = devm_clk_get(&client->dev, mclk_name);
1136         if (IS_ERR(info->mclk)) {
1137                 dev_err(&client->dev, "%s: unable to get clock %s\n",
1138                         __func__, mclk_name);
1139                 return PTR_ERR(info->mclk);
1140         }
1141
1142         if (info->pdata->dev_name != NULL)
1143                 strncpy(info->devname, info->pdata->dev_name,
1144                         sizeof(info->devname) - 1);
1145         else
1146                 strncpy(info->devname, "ar0330", sizeof(info->devname) - 1);
1147
1148         ar0330_power_get(info);
1149
1150         memcpy(&info->miscdev_info,
1151                 &ar0330_device,
1152                 sizeof(struct miscdevice));
1153
1154         info->miscdev_info.name = info->devname;
1155
1156         err = misc_register(&info->miscdev_info);
1157         if (err) {
1158                 pr_err("%s:Unable to register misc device!\n", __func__);
1159                 goto ar0330_probe_fail;
1160         }
1161
1162         i2c_set_clientdata(client, info);
1163
1164         mutex_init(&info->ar0330_camera_lock);
1165         pr_err("[AR0330]: end of probing sensor.\n");
1166         return 0;
1167
1168 ar0330_probe_fail:
1169         ar0330_power_put(&info->power);
1170
1171         return err;
1172 }
1173
1174 static int
1175 ar0330_remove(struct i2c_client *client)
1176 {
1177         struct ar0330_info *info;
1178         info = i2c_get_clientdata(client);
1179         misc_deregister(&ar0330_device);
1180         mutex_destroy(&info->ar0330_camera_lock);
1181
1182         ar0330_power_put(&info->power);
1183
1184         return 0;
1185 }
1186
1187 static const struct i2c_device_id ar0330_id[] = {
1188         { "ar0330", 0 },
1189         { "ar0330.1", 0 },
1190         { }
1191 };
1192
1193 MODULE_DEVICE_TABLE(i2c, ar0330_id);
1194
1195 static struct i2c_driver ar0330_i2c_driver = {
1196         .driver = {
1197                 .name = "ar0330",
1198                 .owner = THIS_MODULE,
1199         },
1200         .probe = ar0330_probe,
1201         .remove = ar0330_remove,
1202         .id_table = ar0330_id,
1203 };
1204
1205 static int __init ar0330_init(void)
1206 {
1207         pr_info("[AR0330] sensor driver loading\n");
1208         return i2c_add_driver(&ar0330_i2c_driver);
1209 }
1210
1211 static void __exit ar0330_exit(void)
1212 {
1213         i2c_del_driver(&ar0330_i2c_driver);
1214 }
1215
1216 module_init(ar0330_init);
1217 module_exit(ar0330_exit);