]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/media/platform/tegra/ar0330.c
media:ar0330: add 4 lanes setting 2048x1296
[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 static struct ar0330_reg mode_2048x1296[] = {
503         {0x3052, 0xa114},
504         {0x304A, 0x0070},
505         {AR0330_TABLE_WAIT_MS, AR0330_WAIT_MS},
506         {0x301A, 0x0058},
507         {0x302A, 0x0005},
508         {0x302C, 0x0001},
509         {0x302E, 0x0002},
510         {0x3030, 0x0029},
511         {0x3036, 0x000A},
512         {0x3038, 0x0001},
513         {0x31AC, 0x0A0A},
514         {0x31AE, 0x0204},
515         {0x31B0, 0x002F},
516         {0x31B2, 0x0013},
517         {0x31B4, 0x3C44},
518         {0x31B6, 0x314D},
519         {0x31B8, 0x208A},
520         {0x31BA, 0x0207},
521         {0x31BC, 0x8005},
522         {0x3002, 0x007E},
523         {0x3004, 0x0086},
524         {0x3006, 0x058D},
525         {0x3008, 0x0885},
526         {0x300A, 0x05BB},
527         {0x300C, 0x045E},
528         {0x3012, 0x05BA},
529         {0x3014, 0x0000},
530         {0x30A2, 0x0001},
531         {0x30A6, 0x0001},
532         {0x3040, 0x0000},
533         {0x3042, 0x0000},
534         {0x30BA, 0x002C},
535         {0x31E0, 0x0303},
536         {0x3064, 0x1802},
537         {0x3ED2, 0x0146},
538         {0x3ED4, 0x8F6C},
539         {0x3ED6, 0x66CC},
540         {0x3ED8, 0x8C42},
541         {0x3EDA, 0x88BC},
542         {0x3EDC, 0xAA63},
543         {0x305E, 0x00A0},
544         {0x3088, 0x80BA},
545         {0x3086, 0x0253},
546         {0x30CE, 0x0010},
547         {0x301A, 0x035C},
548         {AR0330_TABLE_END, 0x00}
549 };
550
551 enum {
552         AR0330_MODE_2304X1536,
553         AR0330_MODE_1280X720,
554         AR0330_MODE_1280X960,
555         AR0330_MODE_2048X1296,
556 };
557
558 static struct ar0330_reg *mode_table[] = {
559         [AR0330_MODE_2304X1536] = mode_2304x1536,
560         [AR0330_MODE_1280X720] = mode_1280x720,
561         [AR0330_MODE_1280X960] = mode_1280x960,
562         [AR0330_MODE_2048X1296] = mode_2048x1296,
563 };
564
565 static inline void
566 msleep_range(unsigned int delay_base)
567 {
568         usleep_range(delay_base*1000, delay_base*1000+500);
569 }
570
571 static inline void
572 ar0330_get_frame_length_regs(struct ar0330_reg *regs, u32 frame_length)
573 {
574         regs->addr = AR0330_FRAME_LENGTH_ADDR;
575         regs->val = frame_length & 0xffff;
576 }
577
578 static inline void
579 ar0330_get_coarse_time_regs(struct ar0330_reg *regs, u32 coarse_time)
580 {
581         regs->addr = AR0330_COARSE_TIME_ADDR;
582         regs->val = coarse_time & 0xffff;
583 }
584
585 static inline void
586 ar0330_get_gain_reg(struct ar0330_reg *regs, u16 gain)
587 {
588         regs->addr = AR0330_GAIN_ADDR;
589         regs->val = gain;
590 }
591
592 static inline int
593 ar0330_read_reg(struct ar0330_info *info, u16 addr, u16 *val)
594 {
595         return regmap_read(info->regmap, addr, (unsigned int *) val);
596 }
597
598 static int
599 ar0330_write_reg(struct ar0330_info *info, u16 addr, u16 val)
600 {
601         int err;
602
603         err = regmap_write(info->regmap, addr, val);
604
605         if (err)
606                 pr_err("%s:i2c write failed, %x = %x\n",
607                         __func__, addr, val);
608
609         return err;
610 }
611
612 static int
613 ar0330_write_table(struct ar0330_info *info,
614                                  const struct ar0330_reg table[],
615                                  const struct ar0330_reg override_list[],
616                                  int num_override_regs)
617 {
618         int err;
619         const struct ar0330_reg *next;
620         int i;
621         u16 val;
622
623         for (next = table; next->addr != AR0330_TABLE_END; next++) {
624                 if (next->addr == AR0330_TABLE_WAIT_MS) {
625                         msleep_range(next->val);
626                         continue;
627                 }
628
629                 val = next->val;
630
631                 /* When an override list is passed in, replace the reg */
632                 /* value to write if the reg is in the list            */
633                 if (override_list) {
634                         for (i = 0; i < num_override_regs; i++) {
635                                 if (next->addr == override_list[i].addr) {
636                                         val = override_list[i].val;
637                                         break;
638                                 }
639                         }
640                 }
641
642                 err = ar0330_write_reg(info, next->addr, val);
643                 if (err) {
644                         pr_err("%s:ar0330_write_table:%d", __func__, err);
645                         return err;
646                 }
647         }
648         return 0;
649 }
650
651 static int ar0330_get_flash_cap(struct ar0330_info *info)
652 {
653         struct ar0330_flash_control *fctl;
654
655         dev_dbg(&info->i2c_client->dev, "%s: %p\n", __func__, info->pdata);
656         if (info->pdata) {
657                 fctl = &info->pdata->flash_cap;
658                 dev_dbg(&info->i2c_client->dev,
659                         "edg: %x, st: %x, rpt: %x, dl: %x\n",
660                         fctl->edge_trig_en,
661                         fctl->start_edge,
662                         fctl->repeat,
663                         fctl->delay_frm);
664
665                 if (fctl->enable)
666                         return 0;
667         }
668         return -ENODEV;
669 }
670
671 static inline int ar0330_set_flash_control(
672         struct ar0330_info *info, struct ar0330_flash_control *fc)
673 {
674         dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
675         return 0;
676 }
677
678 static int
679 ar0330_set_mode(struct ar0330_info *info, struct ar0330_mode *mode)
680 {
681         int sensor_mode;
682         int err;
683         struct ar0330_reg reg_list[8];
684
685         pr_info("%s: xres %u yres %u framelength %u coarsetime %u gain %u\n",
686                          __func__, mode->xres, mode->yres, mode->frame_length,
687                          mode->coarse_time, mode->gain);
688
689         if (mode->xres == 2304 && mode->yres == 1520) {
690                 sensor_mode = AR0330_MODE_2304X1536;
691         } else if (mode->xres == 1280 && mode->yres == 720) {
692                 sensor_mode = AR0330_MODE_1280X720;
693         } else if (mode->xres == 1280 && mode->yres == 960) {
694                 sensor_mode = AR0330_MODE_1280X960;
695         } else if (mode->xres == 2048 && mode->yres == 1296) {
696                 sensor_mode = AR0330_MODE_2048X1296;
697         } else {
698                 pr_err("%s: invalid resolution supplied to set mode %d %d\n",
699                          __func__, mode->xres, mode->yres);
700                 return -EINVAL;
701         }
702
703         /* get a list of override regs for the asking frame length, */
704         /* coarse integration time, and gain.                       */
705         ar0330_get_frame_length_regs(reg_list, mode->frame_length);
706         ar0330_get_coarse_time_regs(reg_list + 1, mode->coarse_time);
707         ar0330_get_gain_reg(reg_list + 2, mode->gain);
708
709         err = ar0330_write_table(info,
710                                 mode_table[sensor_mode],
711                                 reg_list, 3);
712         if (err)
713                 return err;
714         info->mode = sensor_mode;
715         pr_info("[AR0330]: stream on.\n");
716         return 0;
717 }
718
719 static int
720 ar0330_get_status(struct ar0330_info *info, u8 *dev_status)
721 {
722         *dev_status = 0;
723         return 0;
724 }
725
726 static int
727 ar0330_set_frame_length(struct ar0330_info *info, u32 frame_length,
728                                                  bool group_hold)
729 {
730         struct ar0330_reg reg_list[2];
731         int i = 0;
732         int ret;
733
734         ar0330_get_frame_length_regs(reg_list, frame_length);
735
736         for (i = 0; i < 1; i++) {
737                 ret = ar0330_write_reg(info, reg_list[i].addr,
738                          reg_list[i].val);
739                 if (ret)
740                         return ret;
741         }
742
743         return 0;
744 }
745
746 static int
747 ar0330_set_coarse_time(struct ar0330_info *info, u32 coarse_time,
748                                                  bool group_hold)
749 {
750         int ret;
751
752         struct ar0330_reg reg_list[2];
753         int i = 0;
754
755         ar0330_get_coarse_time_regs(reg_list, coarse_time);
756
757         for (i = 0; i < 1; i++) {
758                 ret = ar0330_write_reg(info, reg_list[i].addr,
759                          reg_list[i].val);
760                 if (ret)
761                         return ret;
762         }
763
764         return 0;
765 }
766
767 static int
768 ar0330_set_gain(struct ar0330_info *info, u16 gain, bool group_hold)
769 {
770         int ret;
771         struct ar0330_reg reg_list;
772
773         ar0330_get_gain_reg(&reg_list, gain);
774
775         ret = ar0330_write_reg(info, reg_list.addr, reg_list.val);
776         if (ret)
777                 return ret;
778
779         return 0;
780 }
781
782 static int
783 ar0330_set_group_hold(struct ar0330_info *info, struct ar0330_ae *ae)
784 {
785         int count = 0;
786         bool group_hold_enabled = false;
787
788         if (ae->gain_enable)
789                 count++;
790         if (ae->coarse_time_enable)
791                 count++;
792         if (ae->frame_length_enable)
793                 count++;
794         if (count >= 2)
795                 group_hold_enabled = true;
796
797         if (ae->gain_enable)
798                 ar0330_set_gain(info, ae->gain, false);
799         if (ae->coarse_time_enable)
800                 ar0330_set_coarse_time(info, ae->coarse_time, false);
801         if (ae->frame_length_enable)
802                 ar0330_set_frame_length(info, ae->frame_length, false);
803
804         return 0;
805 }
806
807 static int ar0330_get_sensor_id(struct ar0330_info *info)
808 {
809         int ret = 0;
810
811         pr_info("%s\n", __func__);
812         if (info->sensor_data.fuse_id_size)
813                 return 0;
814
815         /* Note 1: If the sensor does not have power at this point
816         Need to supply the power, e.g. by calling power on function */
817
818         /*ret |= ar0330_write_reg(info, 0x3B02, 0x00);
819         ret |= ar0330_write_reg(info, 0x3B00, 0x01);
820         for (i = 0; i < 9; i++) {
821                 ret |= ar0330_read_reg(info, 0x3B24 + i, &bak);
822                 info->sensor_data.fuse_id[i] = bak;
823         }
824
825         if (!ret)
826                 info->sensor_data.fuse_id_size = i;*/
827
828         /* Note 2: Need to clean up any action carried out in Note 1 */
829
830         return ret;
831 }
832
833 static void ar0330_mclk_disable(struct ar0330_info *info)
834 {
835         dev_dbg(&info->i2c_client->dev, "%s: disable MCLK\n", __func__);
836         clk_disable_unprepare(info->mclk);
837 }
838
839 static int ar0330_mclk_enable(struct ar0330_info *info)
840 {
841         int err;
842         unsigned long mclk_init_rate = 24000000;
843
844         dev_dbg(&info->i2c_client->dev, "%s: enable MCLK with %lu Hz\n",
845                 __func__, mclk_init_rate);
846
847         err = clk_set_rate(info->mclk, mclk_init_rate);
848         if (!err)
849                 err = clk_prepare_enable(info->mclk);
850         return err;
851 }
852
853 static long
854 ar0330_ioctl(struct file *file,
855                          unsigned int cmd, unsigned long arg)
856 {
857         int err = 0;
858         struct ar0330_info *info = file->private_data;
859
860         switch (cmd) {
861         case AR0330_IOCTL_SET_POWER:
862                 if (!info->pdata)
863                         break;
864                 if (arg && info->pdata->power_on) {
865                         err = ar0330_mclk_enable(info);
866                         if (!err)
867                                 err = info->pdata->power_on(&info->power);
868                         if (err < 0)
869                                 ar0330_mclk_disable(info);
870                 }
871                 if (!arg && info->pdata->power_off) {
872                         info->pdata->power_off(&info->power);
873                         ar0330_mclk_disable(info);
874                 }
875                 break;
876         case AR0330_IOCTL_SET_MODE:
877         {
878                 struct ar0330_mode mode;
879                 if (copy_from_user(&mode, (const void __user *)arg,
880                         sizeof(struct ar0330_mode))) {
881                         pr_err("%s:Failed to get mode from user.\n", __func__);
882                         return -EFAULT;
883                 }
884                 return ar0330_set_mode(info, &mode);
885         }
886         case AR0330_IOCTL_SET_FRAME_LENGTH:
887                 return ar0330_set_frame_length(info, (u32)arg, true);
888         case AR0330_IOCTL_SET_COARSE_TIME:
889                 return ar0330_set_coarse_time(info, (u32)arg, true);
890         case AR0330_IOCTL_SET_GAIN:
891                 return ar0330_set_gain(info, (u16)arg, true);
892         case AR0330_IOCTL_GET_STATUS:
893         {
894                 u8 status;
895
896                 err = ar0330_get_status(info, &status);
897                 if (err)
898                         return err;
899                 if (copy_to_user((void __user *)arg, &status, 1)) {
900                         pr_err("%s:Failed to copy status to user\n", __func__);
901                         return -EFAULT;
902                 }
903                 return 0;
904         }
905         case AR0330_IOCTL_GET_SENSORDATA:
906         {
907                 err = ar0330_get_sensor_id(info);
908
909                 if (err) {
910                         pr_err("%s:Failed to get fuse id info.\n", __func__);
911                         return err;
912                 }
913                 if (copy_to_user((void __user *)arg, &info->sensor_data,
914                                 sizeof(struct ar0330_sensordata))) {
915                         pr_info("%s:Failed to copy fuse id to user space\n",
916                                 __func__);
917                         return -EFAULT;
918                 }
919                 return 0;
920         }
921         case AR0330_IOCTL_SET_GROUP_HOLD:
922         {
923                 struct ar0330_ae ae;
924                 if (copy_from_user(&ae, (const void __user *)arg,
925                         sizeof(struct ar0330_ae))) {
926                         pr_info("%s:fail group hold\n", __func__);
927                         return -EFAULT;
928                 }
929                 return ar0330_set_group_hold(info, &ae);
930         }
931         case AR0330_IOCTL_SET_FLASH_MODE:
932         {
933                 struct ar0330_flash_control values;
934
935                 dev_dbg(&info->i2c_client->dev,
936                         "AR0330_IOCTL_SET_FLASH_MODE\n");
937                 if (copy_from_user(&values,
938                         (const void __user *)arg,
939                         sizeof(struct ar0330_flash_control))) {
940                         err = -EFAULT;
941                         break;
942                 }
943                 err = ar0330_set_flash_control(info, &values);
944                 break;
945         }
946         case AR0330_IOCTL_GET_FLASH_CAP:
947                 err = ar0330_get_flash_cap(info);
948                 break;
949         default:
950                 pr_err("%s:unknown cmd.\n", __func__);
951                 err = -EINVAL;
952         }
953
954         return err;
955 }
956
957 static int ar0330_power_on(struct ar0330_power_rail *pw)
958 {
959         int err;
960         struct ar0330_info *info = container_of(pw, struct ar0330_info, power);
961
962         if (unlikely(WARN_ON(!pw || !pw->iovdd || !pw->avdd || !pw->dvdd)))
963                 return -EFAULT;
964
965         gpio_set_value(info->pdata->cam2_gpio, 0);
966         usleep_range(10, 20);
967
968         err = regulator_enable(pw->avdd);
969         if (err)
970                 goto ar0330_avdd_fail;
971
972         err = regulator_enable(pw->dvdd);
973         if (err)
974                 goto ar0330_dvdd_fail;
975
976         err = regulator_enable(pw->iovdd);
977         if (err)
978                 goto ar0330_iovdd_fail;
979
980         usleep_range(1, 2);
981         gpio_set_value(info->pdata->cam2_gpio, 1);
982
983         usleep_range(300, 310);
984
985         return 1;
986
987
988 ar0330_iovdd_fail:
989         regulator_disable(pw->dvdd);
990
991 ar0330_dvdd_fail:
992         regulator_disable(pw->avdd);
993
994 ar0330_avdd_fail:
995         pr_err("%s failed.\n", __func__);
996         return -ENODEV;
997 }
998
999 static int ar0330_power_off(struct ar0330_power_rail *pw)
1000 {
1001         struct ar0330_info *info = container_of(pw, struct ar0330_info, power);
1002
1003         if (unlikely(WARN_ON(!pw || !pw->iovdd || !pw->avdd || !pw->dvdd)))
1004                 return -EFAULT;
1005
1006         usleep_range(1, 2);
1007         gpio_set_value(info->pdata->cam2_gpio, 0);
1008         usleep_range(1, 2);
1009
1010         regulator_disable(pw->iovdd);
1011         regulator_disable(pw->dvdd);
1012         regulator_disable(pw->avdd);
1013
1014         return 0;
1015 }
1016
1017 static int
1018 ar0330_open(struct inode *inode, struct file *file)
1019 {
1020         struct miscdevice       *miscdev = file->private_data;
1021         struct ar0330_info *info;
1022
1023         info = container_of(miscdev, struct ar0330_info, miscdev_info);
1024         /* check if the device is in use */
1025         if (atomic_xchg(&info->in_use, 1)) {
1026                 pr_info("%s:BUSY!\n", __func__);
1027                 return -EBUSY;
1028         }
1029
1030         file->private_data = info;
1031
1032         return 0;
1033 }
1034
1035 static int
1036 ar0330_release(struct inode *inode, struct file *file)
1037 {
1038         struct ar0330_info *info = file->private_data;
1039
1040         file->private_data = NULL;
1041
1042         /* warn if device is already released */
1043         WARN_ON(!atomic_xchg(&info->in_use, 0));
1044         return 0;
1045 }
1046
1047 static int ar0330_power_put(struct ar0330_power_rail *pw)
1048 {
1049         if (unlikely(!pw))
1050                 return -EFAULT;
1051
1052         if (likely(pw->avdd))
1053                 regulator_put(pw->avdd);
1054
1055         if (likely(pw->iovdd))
1056                 regulator_put(pw->iovdd);
1057
1058         if (likely(pw->dvdd))
1059                 regulator_put(pw->dvdd);
1060
1061         pw->avdd = NULL;
1062         pw->iovdd = NULL;
1063         pw->dvdd = NULL;
1064
1065         return 0;
1066 }
1067
1068 static int ar0330_regulator_get(struct ar0330_info *info,
1069         struct regulator **vreg, char vreg_name[])
1070 {
1071         struct regulator *reg = NULL;
1072         int err = 0;
1073
1074         reg = regulator_get(&info->i2c_client->dev, vreg_name);
1075         if (unlikely(IS_ERR(reg))) {
1076                 dev_err(&info->i2c_client->dev, "%s %s ERR: %d\n",
1077                         __func__, vreg_name, (int)reg);
1078                 err = PTR_ERR(reg);
1079                 reg = NULL;
1080         } else
1081                 dev_dbg(&info->i2c_client->dev, "%s: %s\n",
1082                         __func__, vreg_name);
1083
1084         *vreg = reg;
1085         return err;
1086 }
1087
1088 static int ar0330_power_get(struct ar0330_info *info)
1089 {
1090         struct ar0330_power_rail *pw = &info->power;
1091         int err = 0;
1092
1093         err |= ar0330_regulator_get(info, &pw->avdd, "vana"); /* ananlog 2.7v */
1094         err |= ar0330_regulator_get(info, &pw->dvdd, "vdig"); /* digital 1.2v */
1095         err |= ar0330_regulator_get(info, &pw->iovdd, "vif"); /* IO 1.8v */
1096
1097         return err;
1098 }
1099
1100 static const struct file_operations ar0330_fileops = {
1101         .owner = THIS_MODULE,
1102         .open = ar0330_open,
1103         .unlocked_ioctl = ar0330_ioctl,
1104         .release = ar0330_release,
1105 };
1106
1107 static struct miscdevice ar0330_device = {
1108         .minor = MISC_DYNAMIC_MINOR,
1109         .name = "ar0330",
1110         .fops = &ar0330_fileops,
1111 };
1112
1113 static struct of_device_id ar0330_of_match[] = {
1114         { .compatible = "nvidia,ar0330", },
1115         { },
1116 };
1117
1118 MODULE_DEVICE_TABLE(of, ar0330_of_match);
1119
1120 static struct ar0330_platform_data *ar0330_parse_dt(struct i2c_client *client)
1121 {
1122         struct device_node *np = client->dev.of_node;
1123         struct ar0330_platform_data *board_info_pdata;
1124         const struct of_device_id *match;
1125
1126         match = of_match_device(ar0330_of_match, &client->dev);
1127         if (!match) {
1128                 dev_err(&client->dev, "Failed to find matching dt id\n");
1129                 return NULL;
1130         }
1131
1132         board_info_pdata = devm_kzalloc(&client->dev, sizeof(*board_info_pdata),
1133                         GFP_KERNEL);
1134         if (!board_info_pdata) {
1135                 dev_err(&client->dev, "Failed to allocate pdata\n");
1136                 return NULL;
1137         }
1138
1139         board_info_pdata->cam2_gpio = of_get_named_gpio(np, "cam1-gpios", 0);
1140         board_info_pdata->ext_reg = of_property_read_bool(np, "nvidia,ext_reg");
1141
1142         board_info_pdata->power_on = ar0330_power_on;
1143         board_info_pdata->power_off = ar0330_power_off;
1144
1145         return board_info_pdata;
1146 }
1147
1148 static int
1149 ar0330_probe(struct i2c_client *client,
1150                         const struct i2c_device_id *id)
1151 {
1152         struct ar0330_info *info;
1153         int err;
1154         const char *mclk_name;
1155
1156         pr_err("[AR0330]: probing sensor.\n");
1157
1158         info = devm_kzalloc(&client->dev,
1159                         sizeof(struct ar0330_info), GFP_KERNEL);
1160         if (!info) {
1161                 pr_err("%s:Unable to allocate memory!\n", __func__);
1162                 return -ENOMEM;
1163         }
1164
1165         info->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
1166         if (IS_ERR(info->regmap)) {
1167                 dev_err(&client->dev,
1168                         "regmap init failed: %ld\n", PTR_ERR(info->regmap));
1169                 return -ENODEV;
1170         }
1171
1172         if (client->dev.of_node)
1173                 info->pdata = ar0330_parse_dt(client);
1174         else
1175                 info->pdata = client->dev.platform_data;
1176
1177         if (!info->pdata) {
1178                 pr_err("[AR0330]:%s:Unable to get platform data\n", __func__);
1179                 return -EFAULT;
1180         }
1181
1182         info->i2c_client = client;
1183         atomic_set(&info->in_use, 0);
1184         info->mode = -1;
1185
1186         mclk_name = info->pdata->mclk_name ?
1187                     info->pdata->mclk_name : "default_mclk";
1188         info->mclk = devm_clk_get(&client->dev, mclk_name);
1189         if (IS_ERR(info->mclk)) {
1190                 dev_err(&client->dev, "%s: unable to get clock %s\n",
1191                         __func__, mclk_name);
1192                 return PTR_ERR(info->mclk);
1193         }
1194
1195         if (info->pdata->dev_name != NULL)
1196                 strncpy(info->devname, info->pdata->dev_name,
1197                         sizeof(info->devname) - 1);
1198         else
1199                 strncpy(info->devname, "ar0330", sizeof(info->devname) - 1);
1200
1201         ar0330_power_get(info);
1202
1203         memcpy(&info->miscdev_info,
1204                 &ar0330_device,
1205                 sizeof(struct miscdevice));
1206
1207         info->miscdev_info.name = info->devname;
1208
1209         err = misc_register(&info->miscdev_info);
1210         if (err) {
1211                 pr_err("%s:Unable to register misc device!\n", __func__);
1212                 goto ar0330_probe_fail;
1213         }
1214
1215         i2c_set_clientdata(client, info);
1216
1217         mutex_init(&info->ar0330_camera_lock);
1218         pr_err("[AR0330]: end of probing sensor.\n");
1219         return 0;
1220
1221 ar0330_probe_fail:
1222         ar0330_power_put(&info->power);
1223
1224         return err;
1225 }
1226
1227 static int
1228 ar0330_remove(struct i2c_client *client)
1229 {
1230         struct ar0330_info *info;
1231         info = i2c_get_clientdata(client);
1232         misc_deregister(&ar0330_device);
1233         mutex_destroy(&info->ar0330_camera_lock);
1234
1235         ar0330_power_put(&info->power);
1236
1237         return 0;
1238 }
1239
1240 static const struct i2c_device_id ar0330_id[] = {
1241         { "ar0330", 0 },
1242         { "ar0330.1", 0 },
1243         { }
1244 };
1245
1246 MODULE_DEVICE_TABLE(i2c, ar0330_id);
1247
1248 static struct i2c_driver ar0330_i2c_driver = {
1249         .driver = {
1250                 .name = "ar0330",
1251                 .owner = THIS_MODULE,
1252         },
1253         .probe = ar0330_probe,
1254         .remove = ar0330_remove,
1255         .id_table = ar0330_id,
1256 };
1257
1258 static int __init ar0330_init(void)
1259 {
1260         pr_info("[AR0330] sensor driver loading\n");
1261         return i2c_add_driver(&ar0330_i2c_driver);
1262 }
1263
1264 static void __exit ar0330_exit(void)
1265 {
1266         i2c_del_driver(&ar0330_i2c_driver);
1267 }
1268
1269 module_init(ar0330_init);
1270 module_exit(ar0330_exit);