camera: fix sensor driver obtain i2c fail in atomic, so check i2c bus is locked or...
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / ov5640.c
1 /*
2  * Driver for OV5640 CMOS Image Sensor from OmniVision
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/miscdevice.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/soc_camera.h>
22 #include <mach/rk29_camera.h>
23 #include "ov5640.h"
24
25 static int debug;
26 module_param(debug, int, S_IRUGO|S_IWUSR);
27
28 #define dprintk(level, fmt, arg...) do {                        \
29         if (debug >= level)                                     \
30         printk(KERN_WARNING fmt , ## arg); } while (0)
31
32 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
33 #define SENSOR_DG(format, ...) dprintk(0, format, ## __VA_ARGS__)
34
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
37
38 #define __STR(x) #x
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
41
42 #define MIN(x,y)   ((x<y) ? x: y)
43 #define MAX(x,y)    ((x>y) ? x: y)
44
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME ov5640
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV5640
48 #define SENSOR_ID 0x5640
49 #define SENSOR_MIN_WIDTH    176
50 #define SENSOR_MIN_HEIGHT   144
51 #define SENSOR_MAX_WIDTH    2592
52 #define SENSOR_MAX_HEIGHT   1944
53 #define SENSOR_INIT_WIDTH       800                     /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT  600
55 #define SENSOR_INIT_WINSEQADR sensor_svga
56 #define SENSOR_INIT_PIXFMT V4L2_PIX_FMT_UYVY
57
58 #define CONFIG_SENSOR_WhiteBalance      1
59 #define CONFIG_SENSOR_Brightness        0
60 #define CONFIG_SENSOR_Contrast      0
61 #define CONFIG_SENSOR_Saturation    0
62 #define CONFIG_SENSOR_Effect        1
63 #define CONFIG_SENSOR_Scene         1
64 #define CONFIG_SENSOR_DigitalZoom   0
65 #define CONFIG_SENSOR_Exposure      0
66 #define CONFIG_SENSOR_Flash         0
67 #define CONFIG_SENSOR_Mirror        0
68 #define CONFIG_SENSOR_Flip          0
69 #ifdef CONFIG_OV5640_AUTOFOCUS
70 #define CONFIG_SENSOR_Focus         1
71 #include "ov5640_af_firmware.c"
72 #else
73 #define CONFIG_SENSOR_Focus         0
74 #endif
75
76 #define CONFIG_SENSOR_I2C_SPEED     100000       /* Hz */
77 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
78 #define CONFIG_SENSOR_I2C_NOSCHED   0
79 #define CONFIG_SENSOR_I2C_RDWRCHK   0
80
81
82 #define SENSOR_BUS_PARAM  (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
83                           SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
84                           SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ)
85
86 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
87 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
88 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
89 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
90 #define COLOR_TEMPERATURE_OFFICE_DN     3500
91 #define COLOR_TEMPERATURE_OFFICE_UP     5000
92 #define COLOR_TEMPERATURE_HOME_DN       2500
93 #define COLOR_TEMPERATURE_HOME_UP       3500
94
95 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
96 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
97
98 #define SENSOR_AF_IS_ERR    (0x00<<0)
99 #define SENSOR_AF_IS_OK         (0x01<<0)
100
101 #if CONFIG_SENSOR_Focus
102 #define SENSOR_AF_MODE_INFINITY    0
103 #define SENSOR_AF_MODE_MACRO       1
104 #define SENSOR_AF_MODE_FIXED       2
105 #define SENSOR_AF_MODE_AUTO        3
106 #define SENSOR_AF_MODE_CONTINUOUS  4
107 #define SENSOR_AF_MODE_CLOSE       5
108 #endif
109
110 #if CONFIG_SENSOR_Focus
111 /* ov5640 VCM Command and Status Registers */
112 #define CMD_MAIN_Reg      0x3024
113 #define CMD_TAG_Reg       0x3025
114 #define CMD_PARA0_Reg     0x5085
115 #define CMD_PARA1_Reg     0x5084
116 #define CMD_PARA2_Reg     0x5083
117 #define CMD_PARA3_Reg     0x5082
118 #define STA_ZONE_Reg      0x3026
119 #define STA_FOCUS_Reg     0x3027
120
121 /* ov5640 VCM Command  */
122 #define OverlayEn_Cmd     0x01
123 #define OverlayDis_Cmd    0x02
124 #define SingleFocus_Cmd   0x03
125 #define ConstFocus_Cmd    0x04
126 #define StepMode_Cmd      0x05
127 #define PauseFocus_Cmd    0x06
128 #define ReturnIdle_Cmd    0x08
129 #define SetZone_Cmd       0x10
130 #define UpdateZone_Cmd    0x12
131 #define SetMotor_Cmd      0x20
132
133 /* ov5640 Focus State */
134 #define S_FIRWRE          0xFF
135 #define S_STARTUP         0xfa
136 #define S_ERROR           0xfe
137 #define S_DRVICERR        0xee
138 #define S_IDLE            0x00
139 #define S_FOCUSING        0x01
140 #define S_FOCUSED         0x02
141 #define S_CAPTURE         0x12
142 #define S_STEP            0x20
143
144 /* ov5640 Zone State */
145 #define Zone_Is_Focused(a, zone_val)    (zone_val&(1<<(a-3)))
146 #define Zone_Get_ID(zone_val)           (zone_val&0x03)
147
148 #define Zone_CenterMode   0x01
149 #define Zone_5xMode       0x02
150 #define Zone_5PlusMode    0x03
151 #define Zone_4fMode       0x04
152
153 #define ZoneSel_Auto      0x0b
154 #define ZoneSel_SemiAuto  0x0c
155 #define ZoneSel_Manual    0x0d
156 #define ZoneSel_Rotate    0x0e
157
158 /* ov5640 Step Focus Commands */
159 #define StepFocus_Near_Tag       0x01
160 #define StepFocus_Far_Tag        0x02
161 #define StepFocus_Furthest_Tag   0x03
162 #define StepFocus_Nearest_Tag    0x04
163 #define StepFocus_Spec_Tag       0x10
164 #endif
165
166 /* init 800X600 SVGA */
167 static struct reginfo sensor_init_data[] =
168 {
169         {0x3008,0x82},
170         {0x3103,0x03},
171         {0x3017,0xff},
172         {0x3018,0xff},
173         {0x3108,0x01},
174         {0x3037,0x13},
175         {0x3630,0x2e},
176         {0x3632,0xe2},
177         {0x3633,0x23},
178         {0x3634,0x44},
179         {0x3621,0xe0},
180         {0x3704,0xa0},
181         {0x3703,0x5a},
182         {0x3715,0x78},
183         {0x3717,0x01},
184         {0x370b,0x60},
185         {0x3705,0x1a},
186         {0x3905,0x02},
187         {0x3906,0x10},
188         {0x3901,0x0a},
189         {0x3731,0x12},
190         {0x3600,0x08},
191         {0x3601,0x33},
192         {0x471c,0x50},
193         {0x3820,0x41},
194         {0x3821,0x07},
195         {0x3814,0x31},
196         {0x3815,0x31},
197         {0x3800,0x00},
198         {0x3801,0x00},
199         {0x3802,0x00},
200         {0x3803,0x04},
201         {0x3804,0x0a},
202         {0x3805,0x3f},
203         {0x3806,0x07},
204         {0x3807,0x9b},
205         {0x3808,0x02},
206         {0x3809,0x80},
207         {0x380a,0x01},
208         {0x380b,0xe0},
209         {0x380c,0x07},
210         {0x380d,0x68},
211         {0x380e,0x03},
212         {0x380f,0xd8},
213         {0x3810,0x00},
214         {0x3811,0x10},
215         {0x3812,0x00},
216         {0x3813,0x06},
217         {0x3618,0x00},
218         {0x3612,0x49},
219         {0x3708,0x62},
220         {0x3709,0x52},
221         {0x370c,0x03},
222         {0x3a02,0x03},
223         {0x3a03,0xd8},
224         {0x3a08,0x01},
225         {0x3a09,0x27},
226         {0x3a0a,0x00},
227         {0x3a0b,0xf6},
228         {0x3a0e,0x03},
229         {0x3a0d,0x04},
230         {0x3a14,0x03},
231         {0x3a15,0xd8},
232         {0x4001,0x02},
233         {0x4004,0x02},
234         {0x3002,0x1c},
235         {0x3006,0xc3},
236         {0x4300,0x30},
237         {0x501f,0x00},
238         {0x4713,0x03},
239         {0x3035,0x11},
240         {0x3036,0x46},
241         {0x4407,0x04},
242         {0x460b,0x35},
243         {0x460c,0x22},
244         {0x3824,0x02},
245         {0x5000,0xa7},
246         {0x5001,0xa3},
247         {0x5000,0xa7},
248         {0x3622,0x01},
249         {0x3635,0x1c},
250         {0x3634,0x40},
251         {0x3c01,0x34},
252         {0x3c00,0x00},
253         {0x3c04,0x28},
254         {0x3c05,0x98},
255         {0x3c06,0x00},
256         {0x3c07,0x08},
257         {0x3c08,0x00},
258         {0x3c09,0x1c},
259         {0x300c,0x22},
260         {0x3c0a,0x9c},
261         {0x3c0b,0x40},
262         {0x5180,0xff},
263         {0x5181,0xf2},
264         {0x5182,0x00},
265         {0x5183,0x94},
266         {0x5184,0x25},
267         {0x5185,0x24},
268         {0x5186,0x06},
269         {0x5187,0x08},
270         {0x5188,0x08},
271         {0x5189,0x78},
272         {0x518a,0x54},
273         {0x518b,0xb2},
274         {0x518c,0xb2},
275         {0x518d,0x44},
276         {0x518e,0x3d},
277         {0x518f,0x58},
278         {0x5190,0x46},
279         {0x5191,0xf8},
280         {0x5192,0x04},
281         {0x5193,0x70},
282         {0x5194,0xf0},
283         {0x5195,0xf0},
284         {0x5196,0x03},
285         {0x5197,0x01},
286         {0x5198,0x04},
287         {0x5199,0x12},
288         {0x519a,0x04},
289         {0x519b,0x00},
290         {0x519c,0x06},
291         {0x519d,0x82},
292         {0x519e,0x38},
293         {0x5381,0x1c},
294         {0x5382,0x5a},
295         {0x5383,0x06},
296         {0x5384,0x20},
297         {0x5385,0x80},
298         {0x5386,0xa0},
299         {0x5387,0xa2},
300         {0x5388,0xa0},
301         {0x5389,0x02},
302         {0x538a,0x01},
303         {0x538b,0x98},
304         {0x5300,0x08},
305         {0x5301,0x30},
306         {0x5302,0x10},
307         {0x5303,0x00},
308         {0x5304,0x08},
309         {0x5305,0x30},
310         {0x5306,0x08},
311         {0x5307,0x16},
312         {0x5309,0x08},
313         {0x530a,0x30},
314         {0x530b,0x04},
315         {0x530c,0x06},
316         {0x5480,0x01},
317         {0x5481,0x08},
318         {0x5482,0x14},
319         {0x5483,0x28},
320         {0x5484,0x51},
321         {0x5485,0x65},
322         {0x5486,0x71},
323         {0x5487,0x7d},
324         {0x5488,0x87},
325         {0x5489,0x91},
326         {0x548a,0x9a},
327         {0x548b,0xaa},
328         {0x548c,0xb8},
329         {0x548d,0xcd},
330         {0x548e,0xdd},
331         {0x548f,0xea},
332         {0x5490,0x1d},
333         {0x5580,0x02},
334         {0x5583,0x40},
335         {0x5584,0x10},
336         {0x5589,0x10},
337         {0x558a,0x00},
338         {0x558b,0xf8},
339         {0x5800,0x23},
340         {0x5801,0x15},
341         {0x5802,0x10},
342         {0x5803,0x10},
343         {0x5804,0x15},
344         {0x5805,0x23},
345         {0x5806,0x0c},
346         {0x5807,0x08},
347         {0x5808,0x05},
348         {0x5809,0x05},
349         {0x580a,0x08},
350         {0x580b,0x0c},
351         {0x580c,0x07},
352         {0x580d,0x03},
353         {0x580e,0x00},
354         {0x580f,0x00},
355         {0x5810,0x03},
356         {0x5811,0x07},
357         {0x5812,0x07},
358         {0x5813,0x03},
359         {0x5814,0x00},
360         {0x5815,0x00},
361         {0x5816,0x03},
362         {0x5817,0x07},
363         {0x5818,0x0b},
364         {0x5819,0x08},
365         {0x581a,0x05},
366         {0x581b,0x05},
367         {0x581c,0x07},
368         {0x581d,0x0b},
369         {0x581e,0x2a},
370         {0x581f,0x16},
371         {0x5820,0x11},
372         {0x5821,0x11},
373         {0x5822,0x15},
374         {0x5823,0x29},
375         {0x5824,0xbf},
376         {0x5825,0xaf},
377         {0x5826,0x9f},
378         {0x5827,0xaf},
379         {0x5828,0xdf},
380         {0x5829,0x6f},
381         {0x582a,0x8e},
382         {0x582b,0xab},
383         {0x582c,0x9e},
384         {0x582d,0x7f},
385         {0x582e,0x4f},
386         {0x582f,0x89},
387         {0x5830,0x86},
388         {0x5831,0x98},
389         {0x5832,0x6f},
390         {0x5833,0x4f},
391         {0x5834,0x6e},
392         {0x5835,0x7b},
393         {0x5836,0x7e},
394         {0x5837,0x6f},
395         {0x5838,0xde},
396         {0x5839,0xbf},
397         {0x583a,0x9f},
398         {0x583b,0xbf},
399         {0x583c,0xec},
400         {0x5025,0x00},
401         {0x3a0f,0x30},
402         {0x3a10,0x28},
403         {0x3a1b,0x30},
404         {0x3a1e,0x26},
405         {0x3a11,0x60},
406         {0x3a1f,0x14},
407         {0x3a18,0x00},
408         {0x3a19,0xf8},
409         {0x3035,0x21},
410
411         {0x3800 ,0x0 },
412         {0x3801 ,0x0 },
413         {0x3802 ,0x0 },
414         {0x3803 ,0x4 },
415         {0x3804 ,0xa },
416         {0x3805 ,0x3f},
417         {0x3806 ,0x7 },
418         {0x3807 ,0x9b},
419         {0x3808 ,0x3 },
420         {0x3809 ,0x20},
421         {0x380a ,0x2 },
422         {0x380b ,0x58},
423         {0x380c ,0x7 },
424         {0x380d ,0x68},
425         {0x380e ,0x3 },
426         {SEQUENCE_END, 0x00}
427 };
428
429 /* 720p 15fps @ 1280x720 */
430
431 static struct reginfo sensor_720p[]=
432 {
433
434         {SEQUENCE_END, 0x00}
435 };
436
437 /*      1080p, 0x15fps, 0xyuv @1920x1080 */
438
439 static struct reginfo sensor_1080p[]=
440 {
441
442         {SEQUENCE_END, 0x00}
443 };
444
445 /* 2592X1944 QSXGA */
446 static struct reginfo sensor_qsxga[] =
447 {
448         {0x3820 ,0x40},
449         {0x3821 ,0x06},
450         {0x3814 ,0x11},
451         {0x3815 ,0x11},
452         {0x3803 ,0x00},
453         {0x3807 ,0x9f},
454         {0x3808 ,0x0a},
455         {0x3809 ,0x20},
456         {0x380a ,0x07},
457         {0x380b ,0x98},
458         {0x380c ,0x0b},
459         {0x380d ,0x1c},
460         {0x380e ,0x07},
461         {0x380f ,0xb0},
462         {0x3813 ,0x04},
463         {0x3618 ,0x04},
464         {0x3612 ,0x4b},
465         {0x3708 ,0x21},
466         {0x3709 ,0x12},
467         {0x370c ,0x00},
468         {0x3a02 ,0x07},
469         {0x3a03 ,0xb0},
470         {0x3a0e ,0x06},
471         {0x3a0d ,0x08},
472         {0x3a14 ,0x07},
473         {0x3a15 ,0xb0},
474         {0x4004 ,0x06},
475         {0x5000 ,0x07},
476         {0x5181 ,0x52},
477         {0x5182 ,0x00},
478         {0x5197 ,0x01},
479         {0x519e ,0x38},
480         {0x3035 ,0x21},
481         {0x5000 ,0x27},
482         {0x5001 ,0x83},
483         {0x3035 ,0x71},
484         {0x4713 ,0x02},
485         {0x3036 ,0x69},
486         {0x4407 ,0x0c},
487         {0x460b ,0x37},
488         {0x460c ,0x20},
489         {0x3824 ,0x01},
490         {SEQUENCE_END, 0x00}
491 };
492 /* 2048*1536 QXGA */
493 static struct reginfo sensor_qxga[] =
494 {
495         {0x3820 ,0x40},
496         {0x3821 ,0x06},
497         {0x3814 ,0x11},
498         {0x3815 ,0x11},
499         {0x3803 ,0x00},
500         {0x3807 ,0x9f},
501         {0x3808 ,0x0a},
502         {0x3809 ,0x20},
503         {0x380a ,0x07},
504         {0x380b ,0x98},
505         {0x380c ,0x0b},
506         {0x380d ,0x1c},
507         {0x380e ,0x07},
508         {0x380f ,0xb0},
509         {0x3813 ,0x04},
510         {0x3618 ,0x04},
511         {0x3612 ,0x4b},
512         {0x3708 ,0x21},
513         {0x3709 ,0x12},
514         {0x370c ,0x00},
515         {0x3a02 ,0x07},
516         {0x3a03 ,0xb0},
517         {0x3a0e ,0x06},
518         {0x3a0d ,0x08},
519         {0x3a14 ,0x07},
520         {0x3a15 ,0xb0},
521         {0x4004 ,0x06},
522         {0x5000 ,0x07},
523         {0x5181 ,0x52},
524         {0x5182 ,0x00},
525         {0x5197 ,0x01},
526         {0x519e ,0x38},
527         {0x3035 ,0x21},
528         {0x5000 ,0x27},
529         {0x5001 ,0x83},
530         {0x3035 ,0x71},
531         {0x4713 ,0x02},
532         {0x3036 ,0x69},
533         {0x4407 ,0x0c},
534         {0x460b ,0x37},
535         {0x460c ,0x20},
536         {0x3824 ,0x01},
537
538         {0x3800 ,0x1 },
539         {0x3801 ,0x8A},
540         {0x3802 ,0x0 },
541         {0x3803 ,0xA },
542         {0x3804 ,0xA },
543         {0x3805 ,0x20},
544         {0x3806 ,0x7 },
545         {0x3807 ,0x98},
546         {0x3808 ,0x8 },
547         {0x3809 ,0x0 },
548         {0x380a ,0x6 },
549         {0x380b ,0x0 },
550         {0x380c ,0xc },
551         {0x380d ,0x80},
552         {0x380e ,0x7 },
553         {0x380f ,0xd0},
554         {0x5001 ,0x7f},
555         {0x5680 ,0x0 },
556         {0x5681 ,0x0 },
557         {0x5682 ,0xA },
558         {0x5683 ,0x20},
559         {0x5684 ,0x0 },
560         {0x5685 ,0x0 },
561         {0x5686 ,0x7 },
562         {0x5687 ,0x98},
563         {SEQUENCE_END, 0x00}
564 };
565
566 /* 1600X1200 UXGA */
567 static struct reginfo sensor_uxga[] =
568 {
569         {0x3820 ,0x40},
570         {0x3821 ,0x06},
571         {0x3814 ,0x11},
572         {0x3815 ,0x11},
573         {0x3803 ,0x00},
574         {0x3807 ,0x9f},
575         {0x3808 ,0x0a},
576         {0x3809 ,0x20},
577         {0x380a ,0x07},
578         {0x380b ,0x98},
579         {0x380c ,0x0b},
580         {0x380d ,0x1c},
581         {0x380e ,0x07},
582         {0x380f ,0xb0},
583         {0x3813 ,0x04},
584         {0x3618 ,0x04},
585         {0x3612 ,0x4b},
586         {0x3708 ,0x21},
587         {0x3709 ,0x12},
588         {0x370c ,0x00},
589         {0x3a02 ,0x07},
590         {0x3a03 ,0xb0},
591         {0x3a0e ,0x06},
592         {0x3a0d ,0x08},
593         {0x3a14 ,0x07},
594         {0x3a15 ,0xb0},
595         {0x4004 ,0x06},
596         {0x5000 ,0x07},
597         {0x5181 ,0x52},
598         {0x5182 ,0x00},
599         {0x5197 ,0x01},
600         {0x519e ,0x38},
601         {0x3035 ,0x21},
602         {0x5000 ,0x27},
603         {0x5001 ,0x83},
604         {0x3035 ,0x71},
605         {0x4713 ,0x02},
606         {0x3036 ,0x69},
607         {0x4407 ,0x0c},
608         {0x460b ,0x37},
609         {0x460c ,0x20},
610         {0x3824 ,0x01},
611
612         {0x3800 ,0x1 },
613         {0x3801 ,0x8A},
614         {0x3802 ,0x0 },
615         {0x3803 ,0xA },
616         {0x3804 ,0xA },
617         {0x3805 ,0x20},
618         {0x3806 ,0x7 },
619         {0x3807 ,0x98},
620         {0x3808 ,0x6 },
621         {0x3809 ,0x40},
622         {0x380a ,0x4 },
623         {0x380b ,0xb0},
624         {0x380c ,0xc },
625         {0x380d ,0x80},
626         {0x380e ,0x7 },
627         {0x380f ,0xd0},
628         {0x5001 ,0x7f},
629         {0x5680 ,0x0 },
630         {0x5681 ,0x0 },
631         {0x5682 ,0xA },
632         {0x5683 ,0x20},
633         {0x5684 ,0x0 },
634         {0x5685 ,0x0 },
635         {0x5686 ,0x7 },
636         {0x5687 ,0x98},
637         {SEQUENCE_END, 0x00}
638 };
639
640 /* 1280X1024 SXGA */
641 static struct reginfo sensor_sxga[] =
642 {
643
644         {SEQUENCE_END, 0x00}
645 };
646
647 /* 800X600 SVGA*/
648 static struct reginfo sensor_svga[] =
649 {
650         {0x3820 ,0x41},
651         {0x3821 ,0x7 },
652         {0x3814 ,0x31},
653         {0x3815 ,0x31},
654         {0x3803 ,0x4 },
655         {0x3807 ,0x9b},
656         {0x3808 ,0x2 },
657         {0x3809 ,0x80},
658         {0x380a ,0x1 },
659         {0x380b ,0xe0},
660         {0x380c ,0x7 },
661         {0x380d ,0x68},
662         {0x380e ,0x3 },
663         {0x380f ,0xd8},
664         {0x3813 ,0x6 },
665         {0x3618 ,0x0 },
666         {0x3612 ,0x49},
667         {0x3708 ,0x62},
668         {0x3709 ,0x52},
669         {0x370c ,0x3 },
670         {0x3a02 ,0x3 },
671         {0x3a03 ,0xd8},
672         {0x3a0e ,0x3 },
673         {0x3a0d ,0x4 },
674         {0x3a14 ,0x3 },
675         {0x3a15 ,0xd8},
676         {0x4004 ,0x2 },
677         {0x5000 ,0xa7},
678         {0x5181 ,0xf2},
679         {0x5182 ,0x0 },
680         {0x5197 ,0x1 },
681         {0x519e ,0x38},
682         {0x3035 ,0x21},
683         {0x5000 ,0xa7},
684         {0x5001 ,0xa3},
685         {0x3035 ,0x21},
686         {0x4713 ,0x3 },
687         {0x3036 ,0x46},
688         {0x4407 ,0x4 },
689         {0x460b ,0x35},
690         {0x460c ,0x22},
691         {0x3824 ,0x2 },
692         {SEQUENCE_END, 0x00}
693 };
694
695 /* 640X480 VGA */
696 static struct reginfo sensor_vga[] =
697 {
698
699         {SEQUENCE_END, 0x00}
700 };
701 /* 352X288 CIF */
702 static struct reginfo sensor_cif[] =
703 {
704
705         {SEQUENCE_END, 0x00}
706 };
707
708 /* 320*240 QVGA */
709 static  struct reginfo sensor_qvga[] =
710 {
711
712         {SEQUENCE_END, 0x00}
713 };
714
715 /* 176X144 QCIF*/
716 static struct reginfo sensor_qcif[] =
717 {
718
719         {SEQUENCE_END, 0x00}
720 };
721
722 static  struct reginfo sensor_ClrFmt_YUYV[]=
723 {
724         {0x4300,0x30},
725         {SEQUENCE_END, 0x00}
726 };
727
728 static  struct reginfo sensor_ClrFmt_UYVY[]=
729 {
730         {0x4300,0x32},
731         {SEQUENCE_END, 0x00}
732 };
733
734
735 #if CONFIG_SENSOR_WhiteBalance
736 static  struct reginfo sensor_WhiteB_Auto[]=
737 {
738         {0x3406 ,0x00},
739         {0x5183 ,0x94},
740         {0x5191 ,0xff},
741         {0x5192 ,0x00},
742         {SEQUENCE_END, 0x00}
743 };
744 /* Cloudy Colour Temperature : 6500K - 8000K  */
745 static  struct reginfo sensor_WhiteB_Cloudy[]=
746 {
747         {0x3406 ,0x1 },
748         {0x3400 ,0x6 },
749         {0x3401 ,0x48},
750         {0x3402 ,0x4 },
751         {0x3403 ,0x0 },
752         {0x3404 ,0x4 },
753         {0x3405 ,0xd3 },
754         {SEQUENCE_END, 0x00}
755 };
756 /* ClearDay Colour Temperature : 5000K - 6500K  */
757 static  struct reginfo sensor_WhiteB_ClearDay[]=
758 {
759         {0x3406 ,0x1 },
760         {0x3400 ,0x6 },
761         {0x3401 ,0x1c},
762         {0x3402 ,0x4 },
763         {0x3403 ,0x0 },
764         {0x3404 ,0x4 },
765         {0x3405 ,0xf3},
766         {SEQUENCE_END, 0x00}
767 };
768 /* Office Colour Temperature : 3500K - 5000K  */
769 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
770 {
771         {0x3406 ,0x1 },
772         {0x3400 ,0x5 },
773         {0x3401 ,0x48},
774         {0x3402 ,0x4 },
775         {0x3403 ,0x0 },
776         {0x3404 ,0x7 },
777         {0x3405 ,0xcf},
778         {SEQUENCE_END, 0x00}
779 };
780 /* Home Colour Temperature : 2500K - 3500K  */
781 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
782 {
783         {0x3406 ,0x1 },
784         {0x3400 ,0x4 },
785         {0x3401 ,0x10},
786         {0x3402 ,0x4 },
787         {0x3403 ,0x0 },
788         {0x3404 ,0x8 },
789         {0x3405 ,0xb6},
790         {SEQUENCE_END, 0x00}
791 };
792 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
793     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
794 };
795 #endif
796
797 #if CONFIG_SENSOR_Brightness
798 static  struct reginfo sensor_Brightness0[]=
799 {
800         {SEQUENCE_END, 0x00}
801 };
802 static  struct reginfo sensor_Brightness1[]=
803 {
804         {SEQUENCE_END, 0x00}
805 };
806
807 static  struct reginfo sensor_Brightness2[]=
808 {
809         {SEQUENCE_END, 0x00}
810 };
811 static  struct reginfo sensor_Brightness3[]=
812 {
813         {SEQUENCE_END, 0x00}
814 };
815 static  struct reginfo sensor_Brightness4[]=
816 {
817         {SEQUENCE_END, 0x00}
818 };
819
820 static  struct reginfo sensor_Brightness5[]=
821 {
822         {SEQUENCE_END, 0x00}
823 };
824 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
825     sensor_Brightness4, sensor_Brightness5,NULL,
826 };
827
828 #endif
829
830 #if CONFIG_SENSOR_Effect
831 static  struct reginfo sensor_Effect_Normal[] =
832 {
833     {0x5001, 0x7f},
834         {0x5580, 0x00},
835         {SEQUENCE_END, 0x00}
836 };
837 static  struct reginfo sensor_Effect_WandB[] =
838 {
839     {0x5001, 0xff},
840         {0x5580, 0x18},
841         {0x5585, 0x80},
842         {0x5586, 0x80},
843         {SEQUENCE_END, 0x00}
844 };
845 static  struct reginfo sensor_Effect_Sepia[] =
846 {
847     {0x5001, 0xff},
848         {0x5580, 0x18},
849         {0x5585, 0x40},
850         {0x5586, 0xa0},
851         {SEQUENCE_END, 0x00}
852 };
853
854 static  struct reginfo sensor_Effect_Negative[] =
855 {
856     //Negative
857     {0x5001, 0xff},
858         {0x5580, 0x40},
859         {SEQUENCE_END, 0x00}
860 };static  struct reginfo sensor_Effect_Bluish[] =
861 {
862     // Bluish
863     {0x5001, 0xff},
864         {0x5580, 0x18},
865         {0x5585, 0xa0},
866         {0x5586, 0x40},
867         {SEQUENCE_END, 0x00}
868 };
869
870 static  struct reginfo sensor_Effect_Green[] =
871 {
872     //  Greenish
873     {0x5001, 0xff},
874         {0x5580, 0x18},
875         {0x5585, 0x60},
876         {0x5586, 0x60},
877         {SEQUENCE_END, 0x00}
878 };
879 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
880     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
881 };
882 #endif
883 #if CONFIG_SENSOR_Exposure
884 static  struct reginfo sensor_Exposure0[]=
885 {
886         {SEQUENCE_END, 0x00}
887 };
888 static  struct reginfo sensor_Exposure1[]=
889 {
890         {SEQUENCE_END, 0x00}
891 };
892 static  struct reginfo sensor_Exposure2[]=
893 {
894         {SEQUENCE_END, 0x00}
895 };
896
897 static  struct reginfo sensor_Exposure3[]=
898 {
899         {SEQUENCE_END, 0x00}
900 };
901 static  struct reginfo sensor_Exposure4[]=
902 {
903         {SEQUENCE_END, 0x00}
904 };
905 static  struct reginfo sensor_Exposure5[]=
906 {
907         {SEQUENCE_END, 0x00}
908 };
909 static  struct reginfo sensor_Exposure6[]=
910 {
911         {SEQUENCE_END, 0x00}
912 };
913 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
914     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
915 };
916 #endif
917 #if CONFIG_SENSOR_Saturation
918 static  struct reginfo sensor_Saturation0[]=
919 {
920         {SEQUENCE_END, 0x00}
921 };
922
923 static  struct reginfo sensor_Saturation1[]=
924 {
925         {SEQUENCE_END, 0x00}
926 };
927
928 static  struct reginfo sensor_Saturation2[]=
929 {
930         {SEQUENCE_END, 0x00}
931 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
932
933 #endif
934 #if CONFIG_SENSOR_Contrast
935 static  struct reginfo sensor_Contrast0[]=
936 {
937         {SEQUENCE_END, 0x00}
938 };
939
940 static  struct reginfo sensor_Contrast1[]=
941 {
942         {SEQUENCE_END, 0x00}
943 };
944 static  struct reginfo sensor_Contrast2[]=
945 {
946         {SEQUENCE_END, 0x00}
947 };
948
949 static  struct reginfo sensor_Contrast3[]=
950 {
951         {SEQUENCE_END, 0x00}
952 };
953
954 static  struct reginfo sensor_Contrast4[]=
955 {
956         {SEQUENCE_END, 0x00}
957 };
958
959
960 static  struct reginfo sensor_Contrast5[]=
961 {
962         {SEQUENCE_END, 0x00}
963 };
964
965 static  struct reginfo sensor_Contrast6[]=
966 {
967         {SEQUENCE_END, 0x00}
968 };
969 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
970     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
971 };
972
973 #endif
974 #if CONFIG_SENSOR_Mirror
975 static  struct reginfo sensor_MirrorOn[]=
976 {
977         {SEQUENCE_END, 0x00}
978 };
979 static  struct reginfo sensor_MirrorOff[]=
980 {
981         {SEQUENCE_END, 0x00}
982 };
983 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
984 #endif
985 #if CONFIG_SENSOR_Flip
986 static  struct reginfo sensor_FlipOn[]=
987 {
988         {SEQUENCE_END, 0x00}
989 };
990
991 static  struct reginfo sensor_FlipOff[]=
992 {
993         {SEQUENCE_END, 0x00}
994 };
995 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
996
997 #endif
998 #if CONFIG_SENSOR_Scene
999 static  struct reginfo sensor_SceneAuto[] =
1000 {
1001         {0x3a00 , 0x78},
1002         {SEQUENCE_END, 0x00}
1003 };
1004 static  struct reginfo sensor_SceneNight[] =
1005 {
1006     //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1007         {0x3034 ,0x1a},
1008         {0x3035 ,0x21},
1009         {0x3036 ,0x46},
1010         {0x3037 ,0x13},
1011         {0x3038 ,0x00},
1012         {0x3039 ,0x00},
1013         {0x3a00 ,0x7c},
1014         {0x3a08 ,0x01},
1015         {0x3a09 ,0x27},
1016         {0x3a0a ,0x00},
1017         {0x3a0b ,0xf6},
1018         {0x3a0d ,0x04},
1019         {0x3a0e ,0x04},
1020         {0x3a02 ,0x0b},
1021         {0x3a03 ,0x88},
1022         {0x3a14 ,0x0b},
1023         {0x3a15 ,0x88},
1024         {SEQUENCE_END, 0x00}
1025 };
1026 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1027
1028 #endif
1029 #if CONFIG_SENSOR_DigitalZoom
1030 static struct reginfo sensor_Zoom0[] =
1031 {
1032         {SEQUENCE_END, 0x00}
1033 };
1034 static struct reginfo sensor_Zoom1[] =
1035 {
1036         {SEQUENCE_END, 0x00}
1037 };
1038
1039 static struct reginfo sensor_Zoom2[] =
1040 {
1041         {SEQUENCE_END, 0x00}
1042 };
1043
1044
1045 static struct reginfo sensor_Zoom3[] =
1046 {
1047         {SEQUENCE_END, 0x00}
1048 };
1049 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1050 #endif
1051 static const struct v4l2_querymenu sensor_menus[] =
1052 {
1053         #if CONFIG_SENSOR_WhiteBalance
1054     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1055     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1056     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1057     #endif
1058
1059         #if CONFIG_SENSOR_Effect
1060     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1061     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1062     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
1063     #endif
1064
1065         #if CONFIG_SENSOR_Scene
1066     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1067     #endif
1068
1069         #if CONFIG_SENSOR_Flash
1070     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1071     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1072     #endif
1073 };
1074
1075 static const struct v4l2_queryctrl sensor_controls[] =
1076 {
1077         #if CONFIG_SENSOR_WhiteBalance
1078     {
1079         .id             = V4L2_CID_DO_WHITE_BALANCE,
1080         .type           = V4L2_CTRL_TYPE_MENU,
1081         .name           = "White Balance Control",
1082         .minimum        = 0,
1083         .maximum        = 4,
1084         .step           = 1,
1085         .default_value = 0,
1086     },
1087     #endif
1088
1089         #if CONFIG_SENSOR_Brightness
1090         {
1091         .id             = V4L2_CID_BRIGHTNESS,
1092         .type           = V4L2_CTRL_TYPE_INTEGER,
1093         .name           = "Brightness Control",
1094         .minimum        = -3,
1095         .maximum        = 2,
1096         .step           = 1,
1097         .default_value = 0,
1098     },
1099     #endif
1100
1101         #if CONFIG_SENSOR_Effect
1102         {
1103         .id             = V4L2_CID_EFFECT,
1104         .type           = V4L2_CTRL_TYPE_MENU,
1105         .name           = "Effect Control",
1106         .minimum        = 0,
1107         .maximum        = 5,
1108         .step           = 1,
1109         .default_value = 0,
1110     },
1111         #endif
1112
1113         #if CONFIG_SENSOR_Exposure
1114         {
1115         .id             = V4L2_CID_EXPOSURE,
1116         .type           = V4L2_CTRL_TYPE_INTEGER,
1117         .name           = "Exposure Control",
1118         .minimum        = 0,
1119         .maximum        = 6,
1120         .step           = 1,
1121         .default_value = 0,
1122     },
1123         #endif
1124
1125         #if CONFIG_SENSOR_Saturation
1126         {
1127         .id             = V4L2_CID_SATURATION,
1128         .type           = V4L2_CTRL_TYPE_INTEGER,
1129         .name           = "Saturation Control",
1130         .minimum        = 0,
1131         .maximum        = 2,
1132         .step           = 1,
1133         .default_value = 0,
1134     },
1135     #endif
1136
1137         #if CONFIG_SENSOR_Contrast
1138         {
1139         .id             = V4L2_CID_CONTRAST,
1140         .type           = V4L2_CTRL_TYPE_INTEGER,
1141         .name           = "Contrast Control",
1142         .minimum        = -3,
1143         .maximum        = 3,
1144         .step           = 1,
1145         .default_value = 0,
1146     },
1147         #endif
1148
1149         #if CONFIG_SENSOR_Mirror
1150         {
1151         .id             = V4L2_CID_HFLIP,
1152         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1153         .name           = "Mirror Control",
1154         .minimum        = 0,
1155         .maximum        = 1,
1156         .step           = 1,
1157         .default_value = 1,
1158     },
1159     #endif
1160
1161         #if CONFIG_SENSOR_Flip
1162         {
1163         .id             = V4L2_CID_VFLIP,
1164         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1165         .name           = "Flip Control",
1166         .minimum        = 0,
1167         .maximum        = 1,
1168         .step           = 1,
1169         .default_value = 1,
1170     },
1171     #endif
1172
1173         #if CONFIG_SENSOR_Scene
1174     {
1175         .id             = V4L2_CID_SCENE,
1176         .type           = V4L2_CTRL_TYPE_MENU,
1177         .name           = "Scene Control",
1178         .minimum        = 0,
1179         .maximum        = 1,
1180         .step           = 1,
1181         .default_value = 0,
1182     },
1183     #endif
1184
1185         #if CONFIG_SENSOR_DigitalZoom
1186     {
1187         .id             = V4L2_CID_ZOOM_RELATIVE,
1188         .type           = V4L2_CTRL_TYPE_INTEGER,
1189         .name           = "DigitalZoom Control",
1190         .minimum        = -1,
1191         .maximum        = 1,
1192         .step           = 1,
1193         .default_value = 0,
1194     }, {
1195         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1196         .type           = V4L2_CTRL_TYPE_INTEGER,
1197         .name           = "DigitalZoom Control",
1198         .minimum        = 0,
1199         .maximum        = 3,
1200         .step           = 1,
1201         .default_value = 0,
1202     },
1203     #endif
1204
1205         #if CONFIG_SENSOR_Focus
1206         {
1207         .id             = V4L2_CID_FOCUS_RELATIVE,
1208         .type           = V4L2_CTRL_TYPE_INTEGER,
1209         .name           = "Focus Control",
1210         .minimum        = -1,
1211         .maximum        = 1,
1212         .step           = 1,
1213         .default_value = 0,
1214     }, {
1215         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1216         .type           = V4L2_CTRL_TYPE_INTEGER,
1217         .name           = "Focus Control",
1218         .minimum        = 0,
1219         .maximum        = 255,
1220         .step           = 1,
1221         .default_value = 125,
1222     },
1223         {
1224         .id             = V4L2_CID_FOCUS_AUTO,
1225         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1226         .name           = "Focus Control",
1227         .minimum        = 0,
1228         .maximum        = 1,
1229         .step           = 1,
1230         .default_value = 0,
1231     },{
1232         .id             = V4L2_CID_FOCUS_CONTINUOUS,
1233         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1234         .name           = "Focus Control",
1235         .minimum        = 0,
1236         .maximum        = 1,
1237         .step           = 1,
1238         .default_value = 0,
1239     },
1240     #endif
1241
1242         #if CONFIG_SENSOR_Flash
1243         {
1244         .id             = V4L2_CID_FLASH,
1245         .type           = V4L2_CTRL_TYPE_MENU,
1246         .name           = "Flash Control",
1247         .minimum        = 0,
1248         .maximum        = 3,
1249         .step           = 1,
1250         .default_value = 0,
1251     },
1252         #endif
1253 };
1254
1255 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1256 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1257 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1258 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1259 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1260 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1261 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1262 static int sensor_resume(struct soc_camera_device *icd);
1263 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1264 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1265
1266 static struct soc_camera_ops sensor_ops =
1267 {
1268     .suspend                     = sensor_suspend,
1269     .resume                       = sensor_resume,
1270     .set_bus_param              = sensor_set_bus_param,
1271     .query_bus_param    = sensor_query_bus_param,
1272     .controls           = sensor_controls,
1273     .menus                         = sensor_menus,
1274     .num_controls               = ARRAY_SIZE(sensor_controls),
1275     .num_menus          = ARRAY_SIZE(sensor_menus),
1276 };
1277
1278 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1279         { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1280         .colorspace = _colorspace }
1281
1282 #define JPG_FMT(_name, _depth, _fourcc) \
1283         COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1284
1285 static const struct soc_camera_data_format sensor_colour_formats[] = {
1286         JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1287         JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1288 };
1289 enum sensor_work_state
1290 {
1291         sensor_work_ready = 0,
1292         sensor_working,
1293 };
1294 struct sensor_work
1295 {
1296         struct i2c_client *client;
1297         struct delayed_work dwork;
1298         enum sensor_work_state state;
1299 };
1300
1301 typedef struct sensor_info_priv_s
1302 {
1303     int whiteBalance;
1304     int brightness;
1305     int contrast;
1306     int saturation;
1307     int effect;
1308     int scene;
1309     int digitalzoom;
1310     int focus;
1311         int auto_focus;
1312         int affm_reinit;
1313     int flash;
1314     int exposure;
1315     unsigned char mirror;                                        /* HFLIP */
1316     unsigned char flip;                                          /* VFLIP */
1317     struct reginfo *winseqe_cur_addr;
1318         unsigned int pixfmt;
1319         unsigned int enable;
1320         unsigned int funmodule_state;
1321 } sensor_info_priv_t;
1322
1323
1324
1325 struct sensor_parameter
1326 {
1327         unsigned short int preview_maxlines;
1328         unsigned short int preview_exposure;
1329         unsigned short int preview_line_width;
1330         unsigned short int preview_gain;
1331
1332         unsigned short int capture_framerate;
1333         unsigned short int preview_framerate;
1334         char awb[6];
1335 };
1336
1337 struct sensor
1338 {
1339     struct v4l2_subdev subdev;
1340     struct i2c_client *client;
1341     sensor_info_priv_t info_priv;
1342         struct sensor_parameter parameter;
1343         struct workqueue_struct *sensor_wq;
1344         struct sensor_work sensor_wk;
1345         struct mutex wq_lock;
1346     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1347 #if CONFIG_SENSOR_I2C_NOSCHED
1348         atomic_t tasklock_cnt;
1349 #endif
1350         struct rk29camera_platform_data *sensor_io_request;
1351 };
1352
1353 static int sensor_deactivate(struct i2c_client *client);
1354
1355
1356 static struct sensor* to_sensor(const struct i2c_client *client)
1357 {
1358     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1359 }
1360
1361 static int sensor_task_lock(struct i2c_client *client, int lock)
1362 {
1363 #if CONFIG_SENSOR_I2C_NOSCHED
1364         int cnt = 3;
1365     struct sensor *sensor = to_sensor(client);
1366
1367         if (lock) {
1368                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1369                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1370                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1371                                 msleep(35);
1372                                 cnt--;
1373                         }
1374                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1375                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1376                                 goto sensor_task_lock_err;
1377                         }
1378                         preempt_disable();
1379                 }
1380
1381                 atomic_add(1, &sensor->tasklock_cnt);
1382         } else {
1383                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1384                         atomic_sub(1, &sensor->tasklock_cnt);
1385
1386                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1387                                 preempt_enable();
1388                 }
1389         }
1390 #endif
1391         return 0;
1392 sensor_task_lock_err:
1393         return -1;
1394 }
1395
1396 /* sensor register write */
1397 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1398 {
1399     int err,cnt;
1400     u8 buf[3];
1401     struct i2c_msg msg[1];
1402
1403     buf[0] = reg >> 8;
1404     buf[1] = reg & 0xFF;
1405     buf[2] = val;
1406
1407     msg->addr = client->addr;
1408     msg->flags = client->flags;
1409     msg->buf = buf;
1410     msg->len = sizeof(buf);
1411     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1412     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1413
1414     cnt = 3;
1415     err = -EAGAIN;
1416
1417     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1418         err = i2c_transfer(client->adapter, msg, 1);
1419
1420         if (err >= 0) {
1421             return 0;
1422         } else {
1423             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1424             udelay(10);
1425         }
1426     }
1427
1428     return err;
1429 }
1430
1431 /* sensor register read */
1432 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1433 {
1434     int err,cnt;
1435     u8 buf[2];
1436     struct i2c_msg msg[2];
1437
1438     buf[0] = reg >> 8;
1439     buf[1] = reg & 0xFF;
1440
1441     msg[0].addr = client->addr;
1442     msg[0].flags = client->flags;
1443     msg[0].buf = buf;
1444     msg[0].len = sizeof(buf);
1445     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1446     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1447
1448     msg[1].addr = client->addr;
1449     msg[1].flags = client->flags|I2C_M_RD;
1450     msg[1].buf = buf;
1451     msg[1].len = 1;
1452     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1453     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1454
1455     cnt = 3;
1456     err = -EAGAIN;
1457     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1458         err = i2c_transfer(client->adapter, msg, 2);
1459
1460         if (err >= 0) {
1461             *val = buf[0];
1462             return 0;
1463         } else {
1464                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1465             udelay(10);
1466         }
1467     }
1468
1469     return err;
1470 }
1471
1472 /* write a array of registers  */
1473 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1474 {
1475     int err = 0, cnt;
1476     int i = 0;
1477 #if CONFIG_SENSOR_Focus
1478         struct sensor *sensor = to_sensor(client);
1479 #endif
1480 #if CONFIG_SENSOR_I2C_RDWRCHK
1481         char valchk;
1482 #endif
1483
1484         cnt = 0;
1485         if (sensor_task_lock(client, 1) < 0)
1486                 goto sensor_write_array_end;
1487     while (regarray[i].reg != SEQUENCE_END)
1488     {
1489         #if CONFIG_SENSOR_Focus
1490         if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1491                         SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1492                         err = -EINVAL;
1493                         goto sensor_write_array_end;
1494         }
1495                 #endif
1496
1497         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1498         if (err < 0)
1499         {
1500             if (cnt-- > 0) {
1501                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1502                                 i = 0;
1503                                 continue;
1504             } else {
1505                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1506                 err = -EPERM;
1507                                 goto sensor_write_array_end;
1508             }
1509         } else {
1510         #if CONFIG_SENSOR_I2C_RDWRCHK
1511                         sensor_read(client, regarray[i].reg, &valchk);
1512                         if (valchk != regarray[i].val)
1513                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1514                 #endif
1515         }
1516
1517         i++;
1518     }
1519
1520         #if CONFIG_SENSOR_Focus
1521         if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1522                 || (regarray == sensor_init_data)) {
1523                 sensor->info_priv.affm_reinit = 1;
1524         }
1525         #endif
1526
1527 sensor_write_array_end:
1528         sensor_task_lock(client,0);
1529     return err;
1530 }
1531 #if CONFIG_SENSOR_I2C_RDWRCHK
1532 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1533 {
1534     int cnt;
1535     int i = 0;
1536         char valchk;
1537
1538         cnt = 0;
1539         valchk = 0;
1540     while (regarray[i].reg != 0)
1541     {
1542                 sensor_read(client, regarray[i].reg, &valchk);
1543                 if (valchk != regarray[i].val)
1544                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1545
1546         i++;
1547     }
1548     return 0;
1549 }
1550 #endif
1551 #if CONFIG_SENSOR_Focus
1552 struct af_cmdinfo
1553 {
1554         char cmd_tag;
1555         char cmd_para[4];
1556         char validate_bit;
1557 };
1558 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1559 {
1560         int i;
1561         char read_tag=0xff,cnt;
1562
1563         if (cmdinfo) {
1564                 if (cmdinfo->validate_bit & 0x80) {
1565                         if (sensor_write(client, CMD_TAG_Reg, cmdinfo->cmd_tag)) {
1566                                 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1567                                 goto sensor_af_cmdset_err;
1568                         }
1569                         SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1570                 }
1571                 for (i=0; i<4; i++) {
1572                         if (cmdinfo->validate_bit & (1<<i)) {
1573                                 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1574                                         SENSOR_TR("%s write CMD_PARA_Reg(main:0x%x para%d:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,i,cmdinfo->cmd_para[i]);
1575                                         goto sensor_af_cmdset_err;
1576                                 }
1577                                 SENSOR_DG("%s write CMD_PARA_Reg(main:0x%x para%d:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,i,cmdinfo->cmd_para[i]);
1578                         }
1579                 }
1580         } else {
1581                 if (sensor_write(client, CMD_TAG_Reg, 0xff)) {
1582                         SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1583                         goto sensor_af_cmdset_err;
1584                 }
1585                 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1586         }
1587
1588         if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1589                 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1590                 goto sensor_af_cmdset_err;
1591         }
1592
1593         cnt = 0;
1594         do
1595         {
1596                 msleep(5);
1597                 if (sensor_read(client,CMD_TAG_Reg,&read_tag)){
1598                    SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1599                    break;
1600                 }
1601     } while((read_tag != 0x00)&& (cnt++<100));
1602
1603         SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1604         return 0;
1605 sensor_af_cmdset_err:
1606         return -1;
1607 }
1608
1609 static int sensor_af_idlechk(struct i2c_client *client)
1610 {
1611         int ret = 0;
1612         char state,cnt;
1613
1614         cnt = 0;
1615         do
1616         {
1617                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1618                 if (ret != 0){
1619                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1620                    ret = -1;
1621                    goto sensor_af_idlechk_end;
1622                 }
1623
1624                 if (state != S_IDLE) {
1625                         sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
1626                         msleep(1);
1627                         cnt++;
1628                 }
1629     } while((state != S_IDLE)&& (cnt<100));
1630
1631         ret = (state == S_IDLE) ? 0 : -1;
1632
1633 sensor_af_idlechk_end:
1634         return ret;
1635 }
1636
1637 static int sensor_af_single(struct i2c_client *client)
1638 {
1639         int ret = 0;
1640         char state,cnt;
1641
1642         if (sensor_af_idlechk(client))
1643                 goto sensor_af_single_end;
1644
1645         if (sensor_af_cmdset(client, SingleFocus_Cmd, NULL)) {
1646                 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1647                 ret = -1;
1648                 goto sensor_af_single_end;
1649         }
1650
1651         cnt = 0;
1652     do
1653     {
1654         if (cnt != 0) {
1655                         msleep(1);
1656         }
1657         cnt++;
1658                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1659                 if (ret != 0){
1660                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1661                    ret = -1;
1662                    goto sensor_af_single_end;
1663                 }
1664     }while((state == S_FOCUSING) && (cnt<100));
1665
1666         if (state != S_FOCUSED) {
1667         SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1668                 ret = -1;
1669                 goto sensor_af_single_end;
1670     }
1671
1672         sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
1673 sensor_af_single_end:
1674         return ret;
1675 }
1676
1677 static int sensor_af_const(struct i2c_client *client)
1678 {
1679         int ret = 0;
1680
1681         if (sensor_af_idlechk(client))
1682                 goto sensor_af_const_end;
1683
1684         if (sensor_af_cmdset(client, ConstFocus_Cmd, NULL)) {
1685                 SENSOR_TR("%s const focus mode set error!\n",SENSOR_NAME_STRING());
1686                 ret = -1;
1687                 goto sensor_af_const_end;
1688         }
1689 sensor_af_const_end:
1690         return ret;
1691 }
1692 static int sensor_af_pause2capture(struct i2c_client *client)
1693 {
1694         int ret = 0;
1695         char state,cnt;
1696
1697         if (sensor_af_cmdset(client, PauseFocus_Cmd, NULL)) {
1698                 SENSOR_TR("%s pause focus mode set error!\n",SENSOR_NAME_STRING());
1699                 ret = -1;
1700                 goto sensor_af_pause_end;
1701         }
1702
1703         cnt = 0;
1704     do
1705     {
1706         if (cnt != 0) {
1707                         msleep(1);
1708         }
1709         cnt++;
1710                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1711                 if (ret != 0){
1712                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1713                    ret = -1;
1714                    goto sensor_af_pause_end;
1715                 }
1716     }while((state != S_CAPTURE) && (cnt<100));
1717
1718         if (state != S_CAPTURE) {
1719         SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1720                 ret = -1;
1721                 goto sensor_af_pause_end;
1722     }
1723 sensor_af_pause_end:
1724         return ret;
1725 }
1726 static int sensor_af_zoneupdate(struct i2c_client *client)
1727 {
1728         int ret = 0;
1729
1730         if (sensor_af_idlechk(client))
1731                 goto sensor_af_zoneupdate_end;
1732
1733         if (sensor_af_cmdset(client, UpdateZone_Cmd, NULL)) {
1734                 SENSOR_TR("%s update zone fail!\n",SENSOR_NAME_STRING());
1735                 ret = -1;
1736                 goto sensor_af_zoneupdate_end;
1737         }
1738
1739 sensor_af_zoneupdate_end:
1740         return ret;
1741 }
1742 static int sensor_af_init(struct i2c_client *client)
1743 {
1744         int ret = 0;
1745         char state,cnt;
1746
1747         ret = sensor_write_array(client, sensor_af_firmware);
1748     if (ret != 0) {
1749        SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1750        ret = -1;
1751            goto sensor_af_init_end;
1752     }
1753
1754         cnt = 0;
1755     do
1756     {
1757         if (cnt != 0) {
1758                         msleep(1);
1759         }
1760         cnt++;
1761                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1762                 if (ret != 0){
1763                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1764                    ret = -1;
1765                    goto sensor_af_init_end;
1766                 }
1767     }while((state == S_STARTUP) && (cnt<100));
1768
1769     if (state != S_IDLE) {
1770         SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1771                 ret = -1;
1772                 goto sensor_af_init_end;
1773     }
1774
1775 sensor_af_init_end:
1776         SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1777         return ret;
1778 }
1779
1780 static int sensor_af_wq_function(struct i2c_client *client)
1781 {
1782         struct sensor *sensor = to_sensor(client);
1783         struct af_cmdinfo cmdinfo;
1784         int ret=0, focus_pos = 0xfe;
1785
1786         SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1787
1788         mutex_lock(&sensor->wq_lock);
1789         if (sensor_af_init(client)) {
1790                 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1791                 ret = -1;
1792         } else {
1793                 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1794
1795                 switch (sensor->info_priv.auto_focus)
1796                 {
1797                         case SENSOR_AF_MODE_INFINITY:
1798                         {
1799                                 focus_pos = 0x00;
1800                         }
1801                         case SENSOR_AF_MODE_MACRO:
1802                         {
1803                                 if (focus_pos != 0x00)
1804                                         focus_pos = 0xff;
1805
1806                                 sensor_af_idlechk(client);
1807                                 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
1808                                 cmdinfo.cmd_para[0] = focus_pos;
1809                                 cmdinfo.validate_bit = 0x81;
1810                                 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
1811                                 break;
1812                         }
1813                         case SENSOR_AF_MODE_AUTO:
1814                         {
1815                                 ret = sensor_af_single(client);
1816                                 break;
1817                         }
1818                         case SENSOR_AF_MODE_CONTINUOUS:
1819                         {
1820                                 ret = sensor_af_const(client);
1821                                 break;
1822                         }
1823                         case SENSOR_AF_MODE_CLOSE:
1824                         {
1825                                 ret = 0;
1826                                 break;
1827                         }
1828                         default:
1829                                 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1830                 }
1831
1832                 SENSOR_DG("%s sensor_af_wq_function set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
1833         }
1834
1835 sensor_af_wq_function_end:
1836         sensor->sensor_wk.state = sensor_work_ready;
1837         mutex_unlock(&sensor->wq_lock);
1838         return ret;
1839 }
1840 static void sensor_af_workqueue(struct work_struct *work)
1841 {
1842         struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
1843         struct i2c_client *client = sensor_work->client;
1844         struct sensor *sensor = to_sensor(client);
1845
1846         if (sensor_af_wq_function(client) < 0) {
1847                 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
1848         }
1849 }
1850 #endif
1851 static int sensor_parameter_record(struct i2c_client *client)
1852 {
1853         u8 ret_l,ret_m,ret_h;
1854         u8 tp_l,tp_m,tp_h;
1855         struct sensor *sensor = to_sensor(client);
1856
1857         sensor_write(client,0x3503,0x07);       //stop AE/AG
1858         sensor_write(client,0x3406,0x01);   //stop AWB
1859
1860         sensor_read(client,0x3500,&ret_h);
1861         sensor_read(client,0x3501, &ret_m);
1862         sensor_read(client,0x3502, &ret_l);
1863         tp_l = ret_l;
1864         tp_m = ret_m;
1865         tp_h = ret_h;
1866         SENSOR_DG(" %s Read 0x3500 = 0x%02x  0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
1867         sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
1868         sensor_read(client,0x350c, &ret_h);
1869         sensor_read(client,0x350d, &ret_l);
1870         sensor->parameter.preview_line_width = ret_h & 0xff;
1871         sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
1872         //Read back AGC Gain for preview
1873         sensor_read(client,0x350b, &tp_l);
1874         sensor->parameter.preview_gain = tp_l;
1875
1876         sensor->parameter.capture_framerate = 900;
1877         sensor->parameter.preview_framerate = 1500;
1878
1879         sensor_read(client,0x3400,&sensor->parameter.awb[0]);           //record awb value
1880         sensor_read(client,0x3401,&sensor->parameter.awb[1]);
1881         sensor_read(client,0x3402,&sensor->parameter.awb[2]);
1882         sensor_read(client,0x3403,&sensor->parameter.awb[3]);
1883         sensor_read(client,0x3404,&sensor->parameter.awb[4]);
1884         sensor_read(client,0x3405,&sensor->parameter.awb[5]);
1885
1886         SENSOR_DG(" %s Read 0x350c = 0x%02x  0x350d = 0x%02x 0x350b=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_l, sensor->parameter.preview_gain);
1887         return 0;
1888 }
1889 static int sensor_ae_transfer(struct i2c_client *client)
1890 {
1891         u8  ExposureLow;
1892         u8  ExposureMid;
1893         u8  ExposureHigh;
1894         u16 ulCapture_Exposure;
1895         u32 ulCapture_Exposure_Gain;
1896         u16  iCapture_Gain;
1897         u8   Lines_10ms;
1898         bool m_60Hz = 0;
1899         u8  reg_l = 0,reg_h =0;
1900         u16 Preview_Maxlines;
1901         u8  Gain;
1902         u32  Capture_MaxLines;
1903         struct sensor *sensor = to_sensor(client);
1904
1905         Preview_Maxlines = sensor->parameter.preview_line_width;
1906         Gain = sensor->parameter.preview_gain;
1907         sensor_read(client,0x350c, &reg_h);
1908         sensor_read(client,0x350d, &reg_l);
1909         Capture_MaxLines = reg_h & 0xff;
1910         Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
1911
1912         if(m_60Hz== 1) {
1913                 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
1914         } else {
1915                 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
1916         }
1917
1918         if(Preview_Maxlines == 0)
1919                 Preview_Maxlines = 1;
1920
1921         ulCapture_Exposure =
1922                 (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
1923         iCapture_Gain = (Gain & 0x0f) + 16;
1924         if (Gain & 0x10) {
1925                 iCapture_Gain = iCapture_Gain << 1;
1926         }
1927         if (Gain & 0x20) {
1928                 iCapture_Gain = iCapture_Gain << 1;
1929         }
1930         if (Gain & 0x40) {
1931                 iCapture_Gain = iCapture_Gain << 1;
1932         }
1933         if (Gain & 0x80) {
1934                 iCapture_Gain = iCapture_Gain << 1;
1935         }
1936         ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5);   //0ld value 2.5, ½â¾ö¹ýÁÁ
1937         if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
1938                 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
1939                 if (ulCapture_Exposure > Lines_10ms)
1940                 {
1941                         //ulCapture_Exposure *= 1.7;
1942                         ulCapture_Exposure /= Lines_10ms;
1943                         ulCapture_Exposure *= Lines_10ms;
1944                 }
1945         } else {
1946                 ulCapture_Exposure = Capture_MaxLines;
1947                 //ulCapture_Exposure_Gain *= 1.5;
1948         }
1949         if(ulCapture_Exposure == 0)
1950                 ulCapture_Exposure = 1;
1951         iCapture_Gain = (ulCapture_Exposure_Gain*2/ulCapture_Exposure + 1)/2;
1952         ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
1953         ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
1954         ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
1955
1956         Gain = 0;
1957         if (iCapture_Gain > 31) {
1958                 Gain |= 0x10;
1959                 iCapture_Gain = iCapture_Gain >> 1;
1960         }
1961         if (iCapture_Gain > 31) {
1962                 Gain |= 0x20;
1963                 iCapture_Gain = iCapture_Gain >> 1;
1964         }
1965         if (iCapture_Gain > 31) {
1966                 Gain |= 0x40;
1967                 iCapture_Gain = iCapture_Gain >> 1;
1968         }
1969         if (iCapture_Gain > 31) {
1970                 Gain |= 0x80;
1971                 iCapture_Gain = iCapture_Gain >> 1;
1972         }
1973         if (iCapture_Gain > 16)
1974                 Gain |= ((iCapture_Gain -16) & 0x0f);
1975         if(Gain == 0x10)
1976                 Gain = 0x11;
1977         // write the gain and exposure to 0x350* registers
1978         //m_iWrite0x350b=Gain;
1979         sensor_write(client,0x350b, Gain);
1980         //m_iWrite0x3502=ExposureLow;
1981         sensor_write(client,0x3502, ExposureLow);
1982         //m_iWrite0x3501=ExposureMid;
1983         sensor_write(client,0x3501, ExposureMid);
1984         //m_iWrite0x3500=ExposureHigh;
1985         sensor_write(client,0x3500, ExposureHigh);
1986         // SendToFile("Gain = 0x%x\r\n", Gain);
1987         // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
1988         // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
1989         // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
1990         //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
1991         //camera_timed_wait(200);
1992         //linzhk camera_timed_wait(500);
1993
1994         sensor_write(client,0x3400,sensor->parameter.awb[0]);           // resume awb value
1995         sensor_write(client,0x3401,sensor->parameter.awb[1]);
1996         sensor_write(client,0x3402,sensor->parameter.awb[2]);
1997         sensor_write(client,0x3403,sensor->parameter.awb[3]);
1998         sensor_write(client,0x3404,sensor->parameter.awb[4]);
1999         sensor_write(client,0x3405,sensor->parameter.awb[5]);
2000
2001         SENSOR_DG(" %s Write 0x350b = 0x%02x  0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2002         mdelay(100);
2003         return 0;
2004 }
2005 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2006 {
2007         struct soc_camera_link *icl = to_soc_camera_link(icd);
2008         int ret = 0;
2009
2010
2011         switch (cmd)
2012         {
2013                 case Sensor_PowerDown:
2014                 {
2015                         if (icl->powerdown) {
2016                                 ret = icl->powerdown(icd->pdev, on);
2017                                 if (ret == RK29_CAM_IO_SUCCESS) {
2018                                         if (on == 0) {
2019                                                 mdelay(2);
2020                                                 if (icl->reset)
2021                                                         icl->reset(icd->pdev);
2022                                         }
2023                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2024                                         ret = -ENODEV;
2025                                         goto sensor_power_end;
2026                                 }
2027                         }
2028                         break;
2029                 }
2030                 case Sensor_Flash:
2031                 {
2032                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2033                 struct sensor *sensor = to_sensor(client);
2034
2035                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2036                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2037                         }
2038                         break;
2039                 }
2040                 default:
2041                 {
2042                         SENSOR_TR("%s power cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2043                         break;
2044                 }
2045         }
2046
2047 sensor_power_end:
2048         return ret;
2049 }
2050 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2051 {
2052     struct i2c_client *client = sd->priv;
2053     struct soc_camera_device *icd = client->dev.platform_data;
2054     struct sensor *sensor = to_sensor(client);
2055         const struct v4l2_queryctrl *qctrl;
2056     char value;
2057     int ret,pid = 0;
2058
2059     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2060
2061         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2062                 ret = -ENODEV;
2063                 goto sensor_INIT_ERR;
2064         }
2065
2066     /* soft reset */
2067         if (sensor_task_lock(client,1)<0)
2068                 goto sensor_INIT_ERR;
2069     ret = sensor_write(client, 0x3008, 0x80);
2070     if (ret != 0) {
2071         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2072         ret = -ENODEV;
2073                 goto sensor_INIT_ERR;
2074     }
2075
2076     mdelay(5);  //delay 5 microseconds
2077         /* check if it is an sensor sensor */
2078     ret = sensor_read(client, 0x300a, &value);
2079     if (ret != 0) {
2080         SENSOR_TR("read chip id high byte failed\n");
2081         ret = -ENODEV;
2082         goto sensor_INIT_ERR;
2083     }
2084
2085     pid |= (value << 8);
2086
2087     ret = sensor_read(client, 0x300b, &value);
2088     if (ret != 0) {
2089         SENSOR_TR("read chip id low byte failed\n");
2090         ret = -ENODEV;
2091         goto sensor_INIT_ERR;
2092     }
2093
2094     pid |= (value & 0xff);
2095     SENSOR_DG("\n %s  pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2096
2097     if (pid == SENSOR_ID) {
2098         sensor->model = SENSOR_V4L2_IDENT;
2099     } else {
2100         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2101         ret = -ENODEV;
2102         goto sensor_INIT_ERR;
2103     }
2104
2105     ret = sensor_write_array(client, sensor_init_data);
2106     if (ret != 0)
2107     {
2108         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2109         goto sensor_INIT_ERR;
2110     }
2111         sensor_task_lock(client,0);
2112     //icd->user_width = SENSOR_INIT_WIDTH;
2113     //icd->user_height = SENSOR_INIT_HEIGHT;
2114     sensor->info_priv.winseqe_cur_addr  = SENSOR_INIT_WINSEQADR;
2115         sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
2116
2117     /* sensor sensor information for initialization  */
2118         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2119         if (qctrl)
2120         sensor->info_priv.whiteBalance = qctrl->default_value;
2121         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2122         if (qctrl)
2123         sensor->info_priv.brightness = qctrl->default_value;
2124         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2125         if (qctrl)
2126         sensor->info_priv.effect = qctrl->default_value;
2127         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2128         if (qctrl)
2129         sensor->info_priv.exposure = qctrl->default_value;
2130
2131         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2132         if (qctrl)
2133         sensor->info_priv.saturation = qctrl->default_value;
2134         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2135         if (qctrl)
2136         sensor->info_priv.contrast = qctrl->default_value;
2137         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2138         if (qctrl)
2139         sensor->info_priv.mirror = qctrl->default_value;
2140         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2141         if (qctrl)
2142         sensor->info_priv.flip = qctrl->default_value;
2143         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2144         if (qctrl)
2145         sensor->info_priv.scene = qctrl->default_value;
2146         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2147         if (qctrl)
2148         sensor->info_priv.digitalzoom = qctrl->default_value;
2149
2150     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
2151         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2152         if (qctrl)
2153         sensor->info_priv.focus = qctrl->default_value;
2154
2155         #if CONFIG_SENSOR_Flash
2156         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2157         if (qctrl)
2158         sensor->info_priv.flash = qctrl->default_value;
2159     #endif
2160     SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),((val == 0)?__FUNCTION__:"sensor_reinit"),icd->user_width,icd->user_height);
2161
2162     return 0;
2163 sensor_INIT_ERR:
2164         sensor_task_lock(client,0);
2165         sensor_deactivate(client);
2166     return ret;
2167 }
2168 static int sensor_deactivate(struct i2c_client *client)
2169 {
2170         struct soc_camera_device *icd = client->dev.platform_data;
2171
2172         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2173
2174         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2175         sensor_task_lock(client, 1);
2176     sensor_write(client, 0x3017, 0x00);  // FREX,VSYNC,HREF,PCLK,D9-D6
2177         sensor_write(client, 0x3018, 0x03);  // D5-D0
2178         sensor_write(client,0x3019,0x00);    // STROBE,SDA
2179         sensor_task_lock(client, 0);
2180         sensor_ioctrl(icd, Sensor_PowerDown, 1);
2181         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2182         icd->user_width = SENSOR_INIT_WIDTH;
2183     icd->user_height = SENSOR_INIT_HEIGHT;
2184         msleep(100);
2185         return 0;
2186 }
2187 static  struct reginfo sensor_power_down_sequence[]=
2188 {
2189     {0x00,0x00}
2190 };
2191 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2192 {
2193     int ret;
2194     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2195
2196     if (pm_msg.event == PM_EVENT_SUSPEND) {
2197         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2198         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2199         if (ret != 0) {
2200             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2201             return ret;
2202         } else {
2203             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2204             if (ret < 0) {
2205                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2206                 return -EINVAL;
2207             }
2208         }
2209     } else {
2210         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2211         return -EINVAL;
2212     }
2213
2214     return 0;
2215 }
2216
2217 static int sensor_resume(struct soc_camera_device *icd)
2218 {
2219         int ret;
2220
2221     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2222     if (ret < 0) {
2223                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2224         return -EINVAL;
2225     }
2226
2227         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2228         return 0;
2229 }
2230
2231 static int sensor_set_bus_param(struct soc_camera_device *icd,
2232                                 unsigned long flags)
2233 {
2234
2235     return 0;
2236 }
2237
2238 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2239 {
2240     struct soc_camera_link *icl = to_soc_camera_link(icd);
2241     unsigned long flags = SENSOR_BUS_PARAM;
2242
2243     return soc_camera_apply_sensor_flags(icl, flags);
2244 }
2245
2246 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2247 {
2248     struct i2c_client *client = sd->priv;
2249     struct soc_camera_device *icd = client->dev.platform_data;
2250     struct sensor *sensor = to_sensor(client);
2251     struct v4l2_pix_format *pix = &f->fmt.pix;
2252
2253     pix->width          = icd->user_width;
2254     pix->height         = icd->user_height;
2255     pix->pixelformat    = sensor->info_priv.pixfmt;
2256     pix->field          = V4L2_FIELD_NONE;
2257     pix->colorspace             = V4L2_COLORSPACE_JPEG;
2258
2259     return 0;
2260 }
2261 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
2262 {
2263     bool ret = false;
2264
2265         if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
2266                 ret = true;
2267         } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
2268                 ret = true;
2269         } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
2270                 ret = true;
2271         } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
2272                 ret = true;
2273         }
2274
2275         if (ret == true)
2276                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2277         return ret;
2278 }
2279 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2280 {
2281     struct i2c_client *client = sd->priv;
2282     struct sensor *sensor = to_sensor(client);
2283     struct v4l2_pix_format *pix = &f->fmt.pix;
2284     struct reginfo *winseqe_set_addr=NULL;
2285     int ret = 0, set_w,set_h;
2286
2287         if (sensor->info_priv.pixfmt != pix->pixelformat) {
2288                 switch (pix->pixelformat)
2289                 {
2290                         case V4L2_PIX_FMT_YUYV:
2291                         {
2292                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2293                                 break;
2294                         }
2295                         case V4L2_PIX_FMT_UYVY:
2296                         {
2297                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2298                                 break;
2299                         }
2300                         default:
2301                                 break;
2302                 }
2303                 if (winseqe_set_addr != NULL) {
2304             sensor_write_array(client, winseqe_set_addr);
2305                         sensor->info_priv.pixfmt = pix->pixelformat;
2306
2307                         SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2308                 } else {
2309                         SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2310                 }
2311         }
2312
2313     set_w = pix->width;
2314     set_h = pix->height;
2315
2316         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2317         {
2318                 winseqe_set_addr = sensor_qcif;
2319         set_w = 176;
2320         set_h = 144;
2321         }
2322         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2323     {
2324         winseqe_set_addr = sensor_qvga;
2325         set_w = 320;
2326         set_h = 240;
2327     }
2328     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2329     {
2330         winseqe_set_addr = sensor_cif;
2331         set_w = 352;
2332         set_h = 288;
2333     }
2334     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2335     {
2336         winseqe_set_addr = sensor_vga;
2337         set_w = 640;
2338         set_h = 480;
2339     }
2340     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2341     {
2342         winseqe_set_addr = sensor_svga;
2343         set_w = 800;
2344         set_h = 600;
2345     }
2346         else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
2347     {
2348         winseqe_set_addr = sensor_720p;
2349         set_w = 1280;
2350         set_h = 720;
2351     }
2352     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2353     {
2354         winseqe_set_addr = sensor_sxga;
2355         set_w = 1280;
2356         set_h = 1024;
2357     }
2358     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2359     {
2360         winseqe_set_addr = sensor_uxga;
2361         set_w = 1600;
2362         set_h = 1200;
2363     }
2364     else if (((set_w <= 1920) && (set_h <= 1080)) && sensor_1080p[0].reg)
2365     {
2366         winseqe_set_addr = sensor_1080p;
2367         set_w = 1920;
2368         set_h = 1080;
2369     }
2370         else if (((set_w <= 2048) && (set_h <= 1536)) && sensor_qxga[0].reg)
2371     {
2372         winseqe_set_addr = sensor_qxga;
2373         set_w = 2048;
2374         set_h = 1536;
2375     }
2376         else if (((set_w <= 2592) && (set_h <= 1944)) && sensor_qsxga[0].reg)
2377     {
2378         winseqe_set_addr = sensor_qsxga;
2379         set_w = 2592;
2380         set_h = 1944;
2381     }
2382     else
2383     {
2384         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2385         set_w = SENSOR_INIT_WIDTH;
2386         set_h = SENSOR_INIT_HEIGHT;
2387                 ret = -1;
2388                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
2389     }
2390
2391     if (winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr)
2392     {
2393                 if (sensor_fmt_capturechk(sd,f) == true) {                                      /* ddl@rock-chips.com : Capture */
2394                         sensor_parameter_record(client);
2395                 #if CONFIG_SENSOR_Focus
2396                         sensor_af_idlechk(client);
2397                         if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2398                                 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2399                 #endif
2400                 }
2401
2402                 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2403                         if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2404                                 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2405                                 ret |= sensor_write_array(client,sensor_init_data);
2406                                 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2407                         }
2408                 }
2409
2410         ret |= sensor_write_array(client, winseqe_set_addr);
2411         if (ret != 0) {
2412             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2413             goto sensor_s_fmt_end;
2414         }
2415
2416         sensor->info_priv.winseqe_cur_addr  = winseqe_set_addr;
2417
2418                 if (sensor_fmt_capturechk(sd,f) == true) {                                  /* ddl@rock-chips.com : Capture */
2419                         sensor_ae_transfer(client);
2420                 }
2421         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2422     }
2423     else
2424     {
2425         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2426     }
2427
2428         pix->width = set_w;
2429         pix->height = set_h;
2430 sensor_s_fmt_end:
2431     return ret;
2432 }
2433
2434 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2435 {
2436     struct v4l2_pix_format *pix = &f->fmt.pix;
2437     bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
2438         pix->pixelformat == V4L2_PIX_FMT_YUYV;
2439
2440     /*
2441     * With Bayer format enforce even side lengths, but let the user play
2442     * with the starting pixel
2443     */
2444
2445     if (pix->height > SENSOR_MAX_HEIGHT)
2446         pix->height = SENSOR_MAX_HEIGHT;
2447     else if (pix->height < SENSOR_MIN_HEIGHT)
2448         pix->height = SENSOR_MIN_HEIGHT;
2449     else if (bayer)
2450         pix->height = ALIGN(pix->height, 2);
2451
2452     if (pix->width > SENSOR_MAX_WIDTH)
2453         pix->width = SENSOR_MAX_WIDTH;
2454     else if (pix->width < SENSOR_MIN_WIDTH)
2455         pix->width = SENSOR_MIN_WIDTH;
2456     else if (bayer)
2457         pix->width = ALIGN(pix->width, 2);
2458
2459     return 0;
2460 }
2461
2462  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2463 {
2464     struct i2c_client *client = sd->priv;
2465
2466     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2467         return -EINVAL;
2468
2469     if (id->match.addr != client->addr)
2470         return -ENODEV;
2471
2472     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2473     id->revision = 0;
2474
2475     return 0;
2476 }
2477 #if CONFIG_SENSOR_Brightness
2478 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2479 {
2480     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2481
2482     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2483     {
2484         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2485         {
2486             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2487             {
2488                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2489                 return -EINVAL;
2490             }
2491             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2492             return 0;
2493         }
2494     }
2495         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2496     return -EINVAL;
2497 }
2498 #endif
2499 #if CONFIG_SENSOR_Effect
2500 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2501 {
2502     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2503
2504     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2505     {
2506         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2507         {
2508             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2509             {
2510                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2511                 return -EINVAL;
2512             }
2513             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2514             return 0;
2515         }
2516     }
2517         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2518     return -EINVAL;
2519 }
2520 #endif
2521 #if CONFIG_SENSOR_Exposure
2522 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2523 {
2524     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2525
2526     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2527     {
2528         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2529         {
2530             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2531             {
2532                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2533                 return -EINVAL;
2534             }
2535             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2536             return 0;
2537         }
2538     }
2539         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2540     return -EINVAL;
2541 }
2542 #endif
2543 #if CONFIG_SENSOR_Saturation
2544 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2545 {
2546     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2547
2548     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2549     {
2550         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2551         {
2552             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2553             {
2554                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2555                 return -EINVAL;
2556             }
2557             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2558             return 0;
2559         }
2560     }
2561     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2562     return -EINVAL;
2563 }
2564 #endif
2565 #if CONFIG_SENSOR_Contrast
2566 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2567 {
2568     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2569
2570     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2571     {
2572         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2573         {
2574             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2575             {
2576                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2577                 return -EINVAL;
2578             }
2579             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2580             return 0;
2581         }
2582     }
2583     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2584     return -EINVAL;
2585 }
2586 #endif
2587 #if CONFIG_SENSOR_Mirror
2588 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2589 {
2590     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2591
2592     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2593     {
2594         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2595         {
2596             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2597             {
2598                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2599                 return -EINVAL;
2600             }
2601             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2602             return 0;
2603         }
2604     }
2605     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2606     return -EINVAL;
2607 }
2608 #endif
2609 #if CONFIG_SENSOR_Flip
2610 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2611 {
2612     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2613
2614     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2615     {
2616         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2617         {
2618             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2619             {
2620                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2621                 return -EINVAL;
2622             }
2623             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2624             return 0;
2625         }
2626     }
2627     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2628     return -EINVAL;
2629 }
2630 #endif
2631 #if CONFIG_SENSOR_Scene
2632 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2633 {
2634     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2635
2636     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2637     {
2638         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2639         {
2640             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2641             {
2642                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2643                 return -EINVAL;
2644             }
2645             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2646             return 0;
2647         }
2648     }
2649     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2650     return -EINVAL;
2651 }
2652 #endif
2653 #if CONFIG_SENSOR_WhiteBalance
2654 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2655 {
2656     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2657
2658     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2659     {
2660         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2661         {
2662             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2663             {
2664                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2665                 return -EINVAL;
2666             }
2667             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2668             return 0;
2669         }
2670     }
2671         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2672     return -EINVAL;
2673 }
2674 #endif
2675 #if CONFIG_SENSOR_DigitalZoom
2676 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2677 {
2678     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2679     struct sensor *sensor = to_sensor(client);
2680         const struct v4l2_queryctrl *qctrl_info;
2681     int digitalzoom_cur, digitalzoom_total;
2682
2683         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2684         if (qctrl_info)
2685                 return -EINVAL;
2686
2687     digitalzoom_cur = sensor->info_priv.digitalzoom;
2688     digitalzoom_total = qctrl_info->maximum;
2689
2690     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2691     {
2692         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2693         return -EINVAL;
2694     }
2695
2696     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2697     {
2698         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2699         return -EINVAL;
2700     }
2701
2702     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2703     {
2704         *value = digitalzoom_total - digitalzoom_cur;
2705     }
2706
2707     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2708     {
2709         *value = 0 - digitalzoom_cur;
2710     }
2711
2712     digitalzoom_cur += *value;
2713
2714     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2715     {
2716         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2717         {
2718             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2719             return -EINVAL;
2720         }
2721         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2722         return 0;
2723     }
2724
2725     return -EINVAL;
2726 }
2727 #endif
2728 #if CONFIG_SENSOR_Focus
2729 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2730 {
2731         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2732     struct sensor *sensor = to_sensor(client);
2733         const struct v4l2_queryctrl *qctrl_info;
2734         struct af_cmdinfo cmdinfo;
2735         int ret = 0;
2736
2737         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2738         if (!qctrl_info)
2739                 return -EINVAL;
2740
2741         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2742                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2743
2744                         if (sensor_af_idlechk(client))
2745                                 goto sensor_set_focus_absolute_end;
2746
2747                         cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2748                         cmdinfo.cmd_para[0] = value;
2749                         cmdinfo.validate_bit = 0x81;
2750                         ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
2751                         //ret |= sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
2752                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2753                 } else {
2754                         ret = -EINVAL;
2755                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2756                 }
2757         } else {
2758                 ret = -EACCES;
2759                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2760                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2761         }
2762
2763 sensor_set_focus_absolute_end:
2764         return ret;
2765 }
2766 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2767 {
2768         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2769         struct sensor *sensor = to_sensor(client);
2770         const struct v4l2_queryctrl *qctrl_info;
2771         struct af_cmdinfo cmdinfo;
2772         int ret = 0;
2773
2774         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
2775         if (!qctrl_info)
2776                 return -EINVAL;
2777
2778         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2779                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2780
2781                         if (sensor_af_idlechk(client))
2782                                 goto sensor_set_focus_relative_end;
2783
2784                         if (value > 0) {
2785                                 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2786                         } else if (value < 0) {
2787                                 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2788                         }
2789                         cmdinfo.validate_bit = 0x80;
2790                         ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
2791
2792                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2793                 } else {
2794                         ret = -EINVAL;
2795                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2796                 }
2797         } else {
2798                 ret = -EACCES;
2799                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2800                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2801         }
2802 sensor_set_focus_relative_end:
2803         return ret;
2804 }
2805
2806 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2807 {
2808         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2809         struct sensor *sensor = to_sensor(client);
2810         int ret = 0;
2811
2812         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)  && (sensor->info_priv.affm_reinit == 0)) {
2813                 switch (value)
2814                 {
2815                         case SENSOR_AF_MODE_AUTO:
2816                         {
2817                                 ret = sensor_af_single(client);
2818                                 break;
2819                         }
2820
2821                         case SENSOR_AF_MODE_MACRO:
2822                         {
2823                                 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
2824                                 break;
2825                         }
2826
2827                         case SENSOR_AF_MODE_INFINITY:
2828                         {
2829                                 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
2830                                 break;
2831                         }
2832
2833                         case SENSOR_AF_MODE_CONTINUOUS:
2834                         {
2835                                 ret = sensor_af_const(client);
2836                                 break;
2837                         }
2838                         default:
2839                                 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2840                                 break;
2841
2842                 }
2843
2844                 SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2845         } else {
2846                 ret = -EACCES;
2847                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2848                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2849         }
2850
2851         return ret;
2852 }
2853 #endif
2854 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2855 {
2856     struct i2c_client *client = sd->priv;
2857     struct sensor *sensor = to_sensor(client);
2858     const struct v4l2_queryctrl *qctrl;
2859
2860     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2861
2862     if (!qctrl)
2863     {
2864         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2865         return -EINVAL;
2866     }
2867
2868     switch (ctrl->id)
2869     {
2870         case V4L2_CID_BRIGHTNESS:
2871             {
2872                 ctrl->value = sensor->info_priv.brightness;
2873                 break;
2874             }
2875         case V4L2_CID_SATURATION:
2876             {
2877                 ctrl->value = sensor->info_priv.saturation;
2878                 break;
2879             }
2880         case V4L2_CID_CONTRAST:
2881             {
2882                 ctrl->value = sensor->info_priv.contrast;
2883                 break;
2884             }
2885         case V4L2_CID_DO_WHITE_BALANCE:
2886             {
2887                 ctrl->value = sensor->info_priv.whiteBalance;
2888                 break;
2889             }
2890         case V4L2_CID_EXPOSURE:
2891             {
2892                 ctrl->value = sensor->info_priv.exposure;
2893                 break;
2894             }
2895         case V4L2_CID_HFLIP:
2896             {
2897                 ctrl->value = sensor->info_priv.mirror;
2898                 break;
2899             }
2900         case V4L2_CID_VFLIP:
2901             {
2902                 ctrl->value = sensor->info_priv.flip;
2903                 break;
2904             }
2905         default :
2906                 break;
2907     }
2908     return 0;
2909 }
2910
2911
2912
2913 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2914 {
2915     struct i2c_client *client = sd->priv;
2916     struct sensor *sensor = to_sensor(client);
2917     struct soc_camera_device *icd = client->dev.platform_data;
2918     const struct v4l2_queryctrl *qctrl;
2919
2920
2921     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2922
2923     if (!qctrl)
2924     {
2925         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2926         return -EINVAL;
2927     }
2928
2929     switch (ctrl->id)
2930     {
2931 #if CONFIG_SENSOR_Brightness
2932         case V4L2_CID_BRIGHTNESS:
2933             {
2934                 if (ctrl->value != sensor->info_priv.brightness)
2935                 {
2936                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2937                     {
2938                         return -EINVAL;
2939                     }
2940                     sensor->info_priv.brightness = ctrl->value;
2941                 }
2942                 break;
2943             }
2944 #endif
2945 #if CONFIG_SENSOR_Exposure
2946         case V4L2_CID_EXPOSURE:
2947             {
2948                 if (ctrl->value != sensor->info_priv.exposure)
2949                 {
2950                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2951                     {
2952                         return -EINVAL;
2953                     }
2954                     sensor->info_priv.exposure = ctrl->value;
2955                 }
2956                 break;
2957             }
2958 #endif
2959 #if CONFIG_SENSOR_Saturation
2960         case V4L2_CID_SATURATION:
2961             {
2962                 if (ctrl->value != sensor->info_priv.saturation)
2963                 {
2964                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2965                     {
2966                         return -EINVAL;
2967                     }
2968                     sensor->info_priv.saturation = ctrl->value;
2969                 }
2970                 break;
2971             }
2972 #endif
2973 #if CONFIG_SENSOR_Contrast
2974         case V4L2_CID_CONTRAST:
2975             {
2976                 if (ctrl->value != sensor->info_priv.contrast)
2977                 {
2978                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2979                     {
2980                         return -EINVAL;
2981                     }
2982                     sensor->info_priv.contrast = ctrl->value;
2983                 }
2984                 break;
2985             }
2986 #endif
2987 #if CONFIG_SENSOR_WhiteBalance
2988         case V4L2_CID_DO_WHITE_BALANCE:
2989             {
2990                 if (ctrl->value != sensor->info_priv.whiteBalance)
2991                 {
2992                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2993                     {
2994                         return -EINVAL;
2995                     }
2996                     sensor->info_priv.whiteBalance = ctrl->value;
2997                 }
2998                 break;
2999             }
3000 #endif
3001 #if CONFIG_SENSOR_Mirror
3002         case V4L2_CID_HFLIP:
3003             {
3004                 if (ctrl->value != sensor->info_priv.mirror)
3005                 {
3006                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3007                         return -EINVAL;
3008                     sensor->info_priv.mirror = ctrl->value;
3009                 }
3010                 break;
3011             }
3012 #endif
3013 #if CONFIG_SENSOR_Flip
3014         case V4L2_CID_VFLIP:
3015             {
3016                 if (ctrl->value != sensor->info_priv.flip)
3017                 {
3018                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3019                         return -EINVAL;
3020                     sensor->info_priv.flip = ctrl->value;
3021                 }
3022                 break;
3023             }
3024 #endif
3025         default:
3026             break;
3027     }
3028
3029     return 0;
3030 }
3031 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3032 {
3033     const struct v4l2_queryctrl *qctrl;
3034     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3035     struct sensor *sensor = to_sensor(client);
3036
3037     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3038
3039     if (!qctrl)
3040     {
3041         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3042         return -EINVAL;
3043     }
3044
3045     switch (ext_ctrl->id)
3046     {
3047         case V4L2_CID_SCENE:
3048             {
3049                 ext_ctrl->value = sensor->info_priv.scene;
3050                 break;
3051             }
3052         case V4L2_CID_EFFECT:
3053             {
3054                 ext_ctrl->value = sensor->info_priv.effect;
3055                 break;
3056             }
3057         case V4L2_CID_ZOOM_ABSOLUTE:
3058             {
3059                 ext_ctrl->value = sensor->info_priv.digitalzoom;
3060                 break;
3061             }
3062         case V4L2_CID_ZOOM_RELATIVE:
3063             {
3064                 return -EINVAL;
3065             }
3066         case V4L2_CID_FOCUS_ABSOLUTE:
3067             {
3068                 return -EINVAL;
3069             }
3070         case V4L2_CID_FOCUS_RELATIVE:
3071             {
3072                 return -EINVAL;
3073             }
3074         case V4L2_CID_FLASH:
3075             {
3076                 ext_ctrl->value = sensor->info_priv.flash;
3077                 break;
3078             }
3079         default :
3080             break;
3081     }
3082     return 0;
3083 }
3084 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3085 {
3086     const struct v4l2_queryctrl *qctrl;
3087     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3088     struct sensor *sensor = to_sensor(client);
3089     int val_offset;
3090
3091     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3092
3093     if (!qctrl)
3094     {
3095         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3096         return -EINVAL;
3097     }
3098
3099         val_offset = 0;
3100     switch (ext_ctrl->id)
3101     {
3102 #if CONFIG_SENSOR_Scene
3103         case V4L2_CID_SCENE:
3104             {
3105                 if (ext_ctrl->value != sensor->info_priv.scene)
3106                 {
3107                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3108                         return -EINVAL;
3109                     sensor->info_priv.scene = ext_ctrl->value;
3110                 }
3111                 break;
3112             }
3113 #endif
3114 #if CONFIG_SENSOR_Effect
3115         case V4L2_CID_EFFECT:
3116             {
3117                 if (ext_ctrl->value != sensor->info_priv.effect)
3118                 {
3119                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3120                         return -EINVAL;
3121                     sensor->info_priv.effect= ext_ctrl->value;
3122                 }
3123                 break;
3124             }
3125 #endif
3126 #if CONFIG_SENSOR_DigitalZoom
3127         case V4L2_CID_ZOOM_ABSOLUTE:
3128             {
3129                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3130                     return -EINVAL;
3131
3132                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3133                 {
3134                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3135
3136                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3137                         return -EINVAL;
3138                     sensor->info_priv.digitalzoom += val_offset;
3139
3140                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
3141                 }
3142
3143                 break;
3144             }
3145         case V4L2_CID_ZOOM_RELATIVE:
3146             {
3147                 if (ext_ctrl->value)
3148                 {
3149                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3150                         return -EINVAL;
3151                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3152
3153                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3154                 }
3155                 break;
3156             }
3157 #endif
3158 #if CONFIG_SENSOR_Focus
3159         case V4L2_CID_FOCUS_ABSOLUTE:
3160             {
3161                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3162                     return -EINVAL;
3163
3164                                 if (sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value) == 0) {
3165                                         if (ext_ctrl->value == qctrl->minimum) {
3166                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
3167                                         } else if (ext_ctrl->value == qctrl->maximum) {
3168                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
3169                                         } else {
3170                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
3171                                         }
3172                                 }
3173
3174                 break;
3175             }
3176         case V4L2_CID_FOCUS_RELATIVE:
3177             {
3178                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3179                     return -EINVAL;
3180
3181                 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3182                 break;
3183             }
3184                 case V4L2_CID_FOCUS_AUTO:
3185                         {
3186                                 if (ext_ctrl->value == 1) {
3187                                         if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
3188                                                 return -EINVAL;
3189                                         sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3190                                 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3191                                         if (ext_ctrl->value == 0)
3192                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3193                                 }
3194                                 break;
3195                         }
3196                 case V4L2_CID_FOCUS_CONTINUOUS:
3197                         {
3198                                 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3199                                         if (ext_ctrl->value == 1) {
3200                                                 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3201                                                         return -EINVAL;
3202                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3203                                         }
3204                                 } else {
3205                                         if (ext_ctrl->value == 0)
3206                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3207                                 }
3208                                 break;
3209                         }
3210 #endif
3211 #if CONFIG_SENSOR_Flash
3212         case V4L2_CID_FLASH:
3213             {
3214                 sensor->info_priv.flash = ext_ctrl->value;
3215
3216                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3217                 break;
3218             }
3219 #endif
3220         default:
3221             break;
3222     }
3223
3224     return 0;
3225 }
3226
3227 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3228 {
3229     struct i2c_client *client = sd->priv;
3230     struct soc_camera_device *icd = client->dev.platform_data;
3231     int i, error_cnt=0, error_idx=-1;
3232
3233
3234     for (i=0; i<ext_ctrl->count; i++) {
3235         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3236             error_cnt++;
3237             error_idx = i;
3238         }
3239     }
3240
3241     if (error_cnt > 1)
3242         error_idx = ext_ctrl->count;
3243
3244     if (error_idx != -1) {
3245         ext_ctrl->error_idx = error_idx;
3246         return -EINVAL;
3247     } else {
3248         return 0;
3249     }
3250 }
3251
3252 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3253 {
3254     struct i2c_client *client = sd->priv;
3255     struct soc_camera_device *icd = client->dev.platform_data;
3256     int i, error_cnt=0, error_idx=-1;
3257
3258     for (i=0; i<ext_ctrl->count; i++) {
3259         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3260             error_cnt++;
3261             error_idx = i;
3262         }
3263     }
3264
3265     if (error_cnt > 1)
3266         error_idx = ext_ctrl->count;
3267
3268     if (error_idx != -1) {
3269         ext_ctrl->error_idx = error_idx;
3270         return -EINVAL;
3271     } else {
3272         return 0;
3273     }
3274 }
3275
3276 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3277 {
3278         struct i2c_client *client = sd->priv;
3279     struct sensor *sensor = to_sensor(client);
3280         #if CONFIG_SENSOR_Focus
3281         struct soc_camera_device *icd = client->dev.platform_data;
3282         struct v4l2_format fmt;
3283         #endif
3284
3285         if (enable == 1) {
3286                 sensor->info_priv.enable = 1;
3287                 #if CONFIG_SENSOR_Focus
3288                 fmt.fmt.pix.width = icd->user_width;
3289                 fmt.fmt.pix.height = icd->user_height;
3290                 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3291                 if (sensor_fmt_capturechk(sd, &fmt) == false) {
3292                         if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3293                                 if (sensor->sensor_wq != NULL) {
3294                                         mutex_lock(&sensor->wq_lock);
3295                                         if (sensor->sensor_wk.state == sensor_working) {
3296                                                 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
3297                                                 mutex_unlock(&sensor->wq_lock);
3298                                                 goto sensor_s_stream_end;
3299                                         }
3300                                         sensor->sensor_wk.state = sensor_working;
3301                                         mutex_unlock(&sensor->wq_lock);
3302                                         sensor->sensor_wk.client = client;
3303                                         INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
3304                                         queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
3305                                 }
3306                                 sensor->info_priv.affm_reinit = 0;
3307                         }
3308                 }
3309                 #endif
3310         } else if (enable == 0) {
3311                 sensor->info_priv.enable = 0;
3312                 #if CONFIG_SENSOR_Focus
3313                 flush_work(&(sensor->sensor_wk.dwork.work));
3314                 mutex_lock(&sensor->wq_lock);
3315                 sensor->sensor_wk.state = sensor_work_ready;
3316                 mutex_unlock(&sensor->wq_lock);
3317                 #endif
3318         }
3319
3320 sensor_s_stream_end:
3321         return 0;
3322 }
3323
3324 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3325  * this wasn't our capture interface, so, we wait for the right one */
3326 static int sensor_video_probe(struct soc_camera_device *icd,
3327                                struct i2c_client *client)
3328 {
3329     char value;
3330     int ret,pid = 0;
3331     struct sensor *sensor = to_sensor(client);
3332
3333     /* We must have a parent by now. And it cannot be a wrong one.
3334      * So this entire test is completely redundant. */
3335     if (!icd->dev.parent ||
3336             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3337                 return -ENODEV;
3338
3339         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3340                 ret = -ENODEV;
3341                 goto sensor_video_probe_err;
3342         }
3343     /* soft reset */
3344     ret = sensor_write(client, 0x3012, 0x80);
3345     if (ret != 0) {
3346         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3347         ret = -ENODEV;
3348                 goto sensor_video_probe_err;
3349     }
3350     mdelay(5);          //delay 5 microseconds
3351
3352     /* check if it is an sensor sensor */
3353     ret = sensor_read(client, 0x300a, &value);
3354     if (ret != 0) {
3355         SENSOR_TR("read chip id high byte failed\n");
3356         ret = -ENODEV;
3357         goto sensor_video_probe_err;
3358     }
3359
3360     pid |= (value << 8);
3361
3362     ret = sensor_read(client, 0x300b, &value);
3363     if (ret != 0) {
3364         SENSOR_TR("read chip id low byte failed\n");
3365         ret = -ENODEV;
3366         goto sensor_video_probe_err;
3367     }
3368
3369     pid |= (value & 0xff);
3370     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3371     if (pid == SENSOR_ID) {
3372         sensor->model = SENSOR_V4L2_IDENT;
3373     } else {
3374         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3375         ret = -ENODEV;
3376         goto sensor_video_probe_err;
3377     }
3378
3379     icd->formats = sensor_colour_formats;
3380     icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
3381
3382     return 0;
3383
3384 sensor_video_probe_err:
3385     return ret;
3386 }
3387 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3388 {
3389         struct i2c_client *client = sd->priv;
3390     struct sensor *sensor = to_sensor(client);
3391
3392         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3393         switch (cmd)
3394         {
3395                 case RK29_CAM_SUBDEV_DEACTIVATE:
3396                 {
3397                         sensor_deactivate(client);
3398                         break;
3399                 }
3400                 case RK29_CAM_SUBDEV_IOREQUEST:
3401                 {
3402                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3403                         break;
3404                 }
3405                 default:
3406                 {
3407                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3408                         break;
3409                 }
3410         }
3411
3412         return 0;
3413
3414 }
3415
3416 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3417         .init           = sensor_init,
3418         .g_ctrl         = sensor_g_control,
3419         .s_ctrl         = sensor_s_control,
3420         .g_ext_ctrls          = sensor_g_ext_controls,
3421         .s_ext_ctrls          = sensor_s_ext_controls,
3422         .g_chip_ident   = sensor_g_chip_ident,
3423         .ioctl = sensor_ioctl,
3424 };
3425
3426 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3427         .s_fmt          = sensor_s_fmt,
3428         .g_fmt          = sensor_g_fmt,
3429         .try_fmt        = sensor_try_fmt,
3430         .s_stream   = sensor_s_stream,
3431 };
3432
3433 static struct v4l2_subdev_ops sensor_subdev_ops = {
3434         .core   = &sensor_subdev_core_ops,
3435         .video = &sensor_subdev_video_ops,
3436 };
3437
3438 static int sensor_probe(struct i2c_client *client,
3439                          const struct i2c_device_id *did)
3440 {
3441     struct sensor *sensor;
3442     struct soc_camera_device *icd = client->dev.platform_data;
3443     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3444     struct soc_camera_link *icl;
3445     int ret;
3446
3447     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3448     if (!icd) {
3449         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3450         return -EINVAL;
3451     }
3452
3453     icl = to_soc_camera_link(icd);
3454     if (!icl) {
3455         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3456         return -EINVAL;
3457     }
3458
3459     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3460         dev_warn(&adapter->dev,
3461                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3462         return -EIO;
3463     }
3464
3465     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3466     if (!sensor)
3467         return -ENOMEM;
3468
3469     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3470
3471     /* Second stage probe - when a capture adapter is there */
3472     icd->ops            = &sensor_ops;
3473     icd->y_skip_top             = 0;
3474         #if CONFIG_SENSOR_I2C_NOSCHED
3475         atomic_set(&sensor->tasklock_cnt,0);
3476         #endif
3477
3478     ret = sensor_video_probe(icd, client);
3479     if (ret) {
3480         icd->ops = NULL;
3481         i2c_set_clientdata(client, NULL);
3482         kfree(sensor);
3483                 sensor = NULL;
3484     } else {
3485                 #if CONFIG_SENSOR_Focus
3486                 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
3487                 if (sensor->sensor_wq == NULL)
3488                         SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
3489                 mutex_init(&sensor->wq_lock);
3490                 sensor->sensor_wk.state = sensor_work_ready;
3491                 #endif
3492     }
3493
3494     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3495     return ret;
3496 }
3497
3498 static int sensor_remove(struct i2c_client *client)
3499 {
3500     struct sensor *sensor = to_sensor(client);
3501     struct soc_camera_device *icd = client->dev.platform_data;
3502
3503         #if CONFIG_SENSOR_Focus
3504         if (sensor->sensor_wq) {
3505                 destroy_workqueue(sensor->sensor_wq);
3506                 sensor->sensor_wq = NULL;
3507         }
3508         #endif
3509
3510     icd->ops = NULL;
3511     i2c_set_clientdata(client, NULL);
3512     client->driver = NULL;
3513     kfree(sensor);
3514         sensor = NULL;
3515     return 0;
3516 }
3517
3518 static const struct i2c_device_id sensor_id[] = {
3519         {SENSOR_NAME_STRING(), 0 },
3520         { }
3521 };
3522 MODULE_DEVICE_TABLE(i2c, sensor_id);
3523
3524 static struct i2c_driver sensor_i2c_driver = {
3525         .driver = {
3526                 .name = SENSOR_NAME_STRING(),
3527         },
3528         .probe          = sensor_probe,
3529         .remove         = sensor_remove,
3530         .id_table       = sensor_id,
3531 };
3532
3533 static int __init sensor_mod_init(void)
3534 {
3535     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3536     return i2c_add_driver(&sensor_i2c_driver);
3537 }
3538
3539 static void __exit sensor_mod_exit(void)
3540 {
3541     i2c_del_driver(&sensor_i2c_driver);
3542 }
3543
3544 device_initcall_sync(sensor_mod_init);
3545 module_exit(sensor_mod_exit);
3546
3547 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3548 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3549 MODULE_LICENSE("GPL");
3550
3551