camera: fix sensor driver compolite warning
[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 RK29_CAM_SENSOR_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         1
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 /* 1024X768 XGA */
647 static struct reginfo sensor_xga[] =
648 {
649         {0x3800 ,0x1 },
650         {0x3801 ,0x8A},
651         {0x3802 ,0x0 },
652         {0x3803 ,0xA },
653         {0x3804 ,0xA },
654         {0x3805 ,0x20},
655         {0x3806 ,0x7 },
656         {0x3807 ,0x98},
657         {0x3808 ,0x4 },
658         {0x3809 ,0x0 },
659         {0x380a ,0x3 },
660         {0x380b ,0x0 },
661         {0x380c ,0xc },
662         {0x380d ,0x80},
663         {0x380e ,0x7 },
664         {0x380f ,0xd0},
665         {0x5001 ,0x7f},
666         {0x5680 ,0x0 },
667         {0x5681 ,0x0 },
668         {0x5682 ,0xA },
669         {0x5683 ,0x20},
670         {0x5684 ,0x0 },
671         {0x5685 ,0x0 },
672         {0x5686 ,0x7 },
673         {0x5687 ,0x98},
674         {SEQUENCE_END, 0x00}
675 };
676 /* 800X600 SVGA*/
677 static struct reginfo sensor_svga[] =
678 {
679         {0x3820 ,0x41},
680         {0x3821 ,0x7 },
681         {0x3814 ,0x31},
682         {0x3815 ,0x31},
683         {0x3803 ,0x4 },
684         {0x3807 ,0x9b},
685         {0x3808 ,0x2 },
686         {0x3809 ,0x80},
687         {0x380a ,0x1 },
688         {0x380b ,0xe0},
689         {0x380c ,0x7 },
690         {0x380d ,0x68},
691         {0x380e ,0x3 },
692         {0x380f ,0xd8},
693         {0x3813 ,0x6 },
694         {0x3618 ,0x0 },
695         {0x3612 ,0x49},
696         {0x3708 ,0x62},
697         {0x3709 ,0x52},
698         {0x370c ,0x3 },
699         {0x3a02 ,0x3 },
700         {0x3a03 ,0xd8},
701         {0x3a0e ,0x3 },
702         {0x3a0d ,0x4 },
703         {0x3a14 ,0x3 },
704         {0x3a15 ,0xd8},
705         {0x4004 ,0x2 },
706         {0x5000 ,0xa7},
707         {0x5181 ,0xf2},
708         {0x5182 ,0x0 },
709         {0x5197 ,0x1 },
710         {0x519e ,0x38},
711         {0x3035 ,0x21},
712         {0x5000 ,0xa7},
713         {0x5001 ,0xa3},
714         {0x3035 ,0x21},
715         {0x4713 ,0x3 },
716         {0x3036 ,0x46},
717         {0x4407 ,0x4 },
718         {0x460b ,0x35},
719         {0x460c ,0x22},
720         {0x3824 ,0x2 },
721         {SEQUENCE_END, 0x00}
722 };
723
724 /* 640X480 VGA */
725 static struct reginfo sensor_vga[] =
726 {
727
728         {SEQUENCE_END, 0x00}
729 };
730 /* 352X288 CIF */
731 static struct reginfo sensor_cif[] =
732 {
733
734         {SEQUENCE_END, 0x00}
735 };
736
737 /* 320*240 QVGA */
738 static  struct reginfo sensor_qvga[] =
739 {
740
741         {SEQUENCE_END, 0x00}
742 };
743
744 /* 176X144 QCIF*/
745 static struct reginfo sensor_qcif[] =
746 {
747
748         {SEQUENCE_END, 0x00}
749 };
750
751 static  struct reginfo sensor_ClrFmt_YUYV[]=
752 {
753         {0x4300,0x30},
754         {SEQUENCE_END, 0x00}
755 };
756
757 static  struct reginfo sensor_ClrFmt_UYVY[]=
758 {
759         {0x4300,0x32},
760         {SEQUENCE_END, 0x00}
761 };
762
763
764 #if CONFIG_SENSOR_WhiteBalance
765 static  struct reginfo sensor_WhiteB_Auto[]=
766 {
767         {0x3406 ,0x00},
768         {0x5183 ,0x94},
769         {0x5191 ,0xff},
770         {0x5192 ,0x00},
771         {SEQUENCE_END, 0x00}
772 };
773 /* Cloudy Colour Temperature : 6500K - 8000K  */
774 static  struct reginfo sensor_WhiteB_Cloudy[]=
775 {
776         {0x3406 ,0x1 },
777         {0x3400 ,0x6 },
778         {0x3401 ,0x48},
779         {0x3402 ,0x4 },
780         {0x3403 ,0x0 },
781         {0x3404 ,0x4 },
782         {0x3405 ,0xd3 },
783         {SEQUENCE_END, 0x00}
784 };
785 /* ClearDay Colour Temperature : 5000K - 6500K  */
786 static  struct reginfo sensor_WhiteB_ClearDay[]=
787 {
788         {0x3406 ,0x1 },
789         {0x3400 ,0x6 },
790         {0x3401 ,0x1c},
791         {0x3402 ,0x4 },
792         {0x3403 ,0x0 },
793         {0x3404 ,0x4 },
794         {0x3405 ,0xf3},
795         {SEQUENCE_END, 0x00}
796 };
797 /* Office Colour Temperature : 3500K - 5000K  */
798 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
799 {
800         {0x3406 ,0x1 },
801         {0x3400 ,0x5 },
802         {0x3401 ,0x48},
803         {0x3402 ,0x4 },
804         {0x3403 ,0x0 },
805         {0x3404 ,0x7 },
806         {0x3405 ,0xcf},
807         {SEQUENCE_END, 0x00}
808 };
809 /* Home Colour Temperature : 2500K - 3500K  */
810 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
811 {
812         {0x3406 ,0x1 },
813         {0x3400 ,0x4 },
814         {0x3401 ,0x10},
815         {0x3402 ,0x4 },
816         {0x3403 ,0x0 },
817         {0x3404 ,0x8 },
818         {0x3405 ,0xb6},
819         {SEQUENCE_END, 0x00}
820 };
821 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
822     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
823 };
824 #endif
825
826 #if CONFIG_SENSOR_Brightness
827 static  struct reginfo sensor_Brightness0[]=
828 {
829         {SEQUENCE_END, 0x00}
830 };
831 static  struct reginfo sensor_Brightness1[]=
832 {
833         {SEQUENCE_END, 0x00}
834 };
835
836 static  struct reginfo sensor_Brightness2[]=
837 {
838         {SEQUENCE_END, 0x00}
839 };
840 static  struct reginfo sensor_Brightness3[]=
841 {
842         {SEQUENCE_END, 0x00}
843 };
844 static  struct reginfo sensor_Brightness4[]=
845 {
846         {SEQUENCE_END, 0x00}
847 };
848
849 static  struct reginfo sensor_Brightness5[]=
850 {
851         {SEQUENCE_END, 0x00}
852 };
853 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
854     sensor_Brightness4, sensor_Brightness5,NULL,
855 };
856
857 #endif
858
859 #if CONFIG_SENSOR_Effect
860 static  struct reginfo sensor_Effect_Normal[] =
861 {
862     {0x5001, 0x7f},
863         {0x5580, 0x00},
864         {SEQUENCE_END, 0x00}
865 };
866 static  struct reginfo sensor_Effect_WandB[] =
867 {
868     {0x5001, 0xff},
869         {0x5580, 0x18},
870         {0x5585, 0x80},
871         {0x5586, 0x80},
872         {SEQUENCE_END, 0x00}
873 };
874 static  struct reginfo sensor_Effect_Sepia[] =
875 {
876     {0x5001, 0xff},
877         {0x5580, 0x18},
878         {0x5585, 0x40},
879         {0x5586, 0xa0},
880         {SEQUENCE_END, 0x00}
881 };
882
883 static  struct reginfo sensor_Effect_Negative[] =
884 {
885     //Negative
886     {0x5001, 0xff},
887         {0x5580, 0x40},
888         {SEQUENCE_END, 0x00}
889 };static  struct reginfo sensor_Effect_Bluish[] =
890 {
891     // Bluish
892     {0x5001, 0xff},
893         {0x5580, 0x18},
894         {0x5585, 0xa0},
895         {0x5586, 0x40},
896         {SEQUENCE_END, 0x00}
897 };
898
899 static  struct reginfo sensor_Effect_Green[] =
900 {
901     //  Greenish
902     {0x5001, 0xff},
903         {0x5580, 0x18},
904         {0x5585, 0x60},
905         {0x5586, 0x60},
906         {SEQUENCE_END, 0x00}
907 };
908 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
909     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
910 };
911 #endif
912 #if CONFIG_SENSOR_Exposure
913 static  struct reginfo sensor_Exposure0[]=
914 {
915         {SEQUENCE_END, 0x00}
916 };
917 static  struct reginfo sensor_Exposure1[]=
918 {
919         {SEQUENCE_END, 0x00}
920 };
921 static  struct reginfo sensor_Exposure2[]=
922 {
923         {SEQUENCE_END, 0x00}
924 };
925
926 static  struct reginfo sensor_Exposure3[]=
927 {
928         {SEQUENCE_END, 0x00}
929 };
930 static  struct reginfo sensor_Exposure4[]=
931 {
932         {SEQUENCE_END, 0x00}
933 };
934 static  struct reginfo sensor_Exposure5[]=
935 {
936         {SEQUENCE_END, 0x00}
937 };
938 static  struct reginfo sensor_Exposure6[]=
939 {
940         {SEQUENCE_END, 0x00}
941 };
942 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
943     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
944 };
945 #endif
946 #if CONFIG_SENSOR_Saturation
947 static  struct reginfo sensor_Saturation0[]=
948 {
949         {SEQUENCE_END, 0x00}
950 };
951
952 static  struct reginfo sensor_Saturation1[]=
953 {
954         {SEQUENCE_END, 0x00}
955 };
956
957 static  struct reginfo sensor_Saturation2[]=
958 {
959         {SEQUENCE_END, 0x00}
960 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
961
962 #endif
963 #if CONFIG_SENSOR_Contrast
964 static  struct reginfo sensor_Contrast0[]=
965 {
966         {SEQUENCE_END, 0x00}
967 };
968
969 static  struct reginfo sensor_Contrast1[]=
970 {
971         {SEQUENCE_END, 0x00}
972 };
973 static  struct reginfo sensor_Contrast2[]=
974 {
975         {SEQUENCE_END, 0x00}
976 };
977
978 static  struct reginfo sensor_Contrast3[]=
979 {
980         {SEQUENCE_END, 0x00}
981 };
982
983 static  struct reginfo sensor_Contrast4[]=
984 {
985         {SEQUENCE_END, 0x00}
986 };
987
988
989 static  struct reginfo sensor_Contrast5[]=
990 {
991         {SEQUENCE_END, 0x00}
992 };
993
994 static  struct reginfo sensor_Contrast6[]=
995 {
996         {SEQUENCE_END, 0x00}
997 };
998 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
999     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1000 };
1001
1002 #endif
1003 #if CONFIG_SENSOR_Mirror
1004 static  struct reginfo sensor_MirrorOn[]=
1005 {
1006         {SEQUENCE_END, 0x00}
1007 };
1008 static  struct reginfo sensor_MirrorOff[]=
1009 {
1010         {SEQUENCE_END, 0x00}
1011 };
1012 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1013 #endif
1014 #if CONFIG_SENSOR_Flip
1015 static  struct reginfo sensor_FlipOn[]=
1016 {
1017         {SEQUENCE_END, 0x00}
1018 };
1019
1020 static  struct reginfo sensor_FlipOff[]=
1021 {
1022         {SEQUENCE_END, 0x00}
1023 };
1024 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1025
1026 #endif
1027 #if CONFIG_SENSOR_Scene
1028 static  struct reginfo sensor_SceneAuto[] =
1029 {
1030         {0x3a00 , 0x78},
1031         {SEQUENCE_END, 0x00}
1032 };
1033 static  struct reginfo sensor_SceneNight[] =
1034 {
1035     //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1036         {0x3034 ,0x1a},
1037         {0x3035 ,0x21},
1038         {0x3036 ,0x46},
1039         {0x3037 ,0x13},
1040         {0x3038 ,0x00},
1041         {0x3039 ,0x00},
1042         {0x3a00 ,0x7c},
1043         {0x3a08 ,0x01},
1044         {0x3a09 ,0x27},
1045         {0x3a0a ,0x00},
1046         {0x3a0b ,0xf6},
1047         {0x3a0d ,0x04},
1048         {0x3a0e ,0x04},
1049         {0x3a02 ,0x0b},
1050         {0x3a03 ,0x88},
1051         {0x3a14 ,0x0b},
1052         {0x3a15 ,0x88},
1053         {SEQUENCE_END, 0x00}
1054 };
1055 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1056
1057 #endif
1058 #if CONFIG_SENSOR_DigitalZoom
1059 static struct reginfo sensor_Zoom0[] =
1060 {
1061         {SEQUENCE_END, 0x00}
1062 };
1063 static struct reginfo sensor_Zoom1[] =
1064 {
1065         {SEQUENCE_END, 0x00}
1066 };
1067
1068 static struct reginfo sensor_Zoom2[] =
1069 {
1070         {SEQUENCE_END, 0x00}
1071 };
1072
1073
1074 static struct reginfo sensor_Zoom3[] =
1075 {
1076         {SEQUENCE_END, 0x00}
1077 };
1078 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1079 #endif
1080 static const struct v4l2_querymenu sensor_menus[] =
1081 {
1082         #if CONFIG_SENSOR_WhiteBalance
1083     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1084     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1085     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1086     #endif
1087
1088         #if CONFIG_SENSOR_Effect
1089     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1090     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1091     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
1092     #endif
1093
1094         #if CONFIG_SENSOR_Scene
1095     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1096     #endif
1097
1098         #if CONFIG_SENSOR_Flash
1099     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1100     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1101     #endif
1102 };
1103
1104 static const struct v4l2_queryctrl sensor_controls[] =
1105 {
1106         #if CONFIG_SENSOR_WhiteBalance
1107     {
1108         .id             = V4L2_CID_DO_WHITE_BALANCE,
1109         .type           = V4L2_CTRL_TYPE_MENU,
1110         .name           = "White Balance Control",
1111         .minimum        = 0,
1112         .maximum        = 4,
1113         .step           = 1,
1114         .default_value = 0,
1115     },
1116     #endif
1117
1118         #if CONFIG_SENSOR_Brightness
1119         {
1120         .id             = V4L2_CID_BRIGHTNESS,
1121         .type           = V4L2_CTRL_TYPE_INTEGER,
1122         .name           = "Brightness Control",
1123         .minimum        = -3,
1124         .maximum        = 2,
1125         .step           = 1,
1126         .default_value = 0,
1127     },
1128     #endif
1129
1130         #if CONFIG_SENSOR_Effect
1131         {
1132         .id             = V4L2_CID_EFFECT,
1133         .type           = V4L2_CTRL_TYPE_MENU,
1134         .name           = "Effect Control",
1135         .minimum        = 0,
1136         .maximum        = 5,
1137         .step           = 1,
1138         .default_value = 0,
1139     },
1140         #endif
1141
1142         #if CONFIG_SENSOR_Exposure
1143         {
1144         .id             = V4L2_CID_EXPOSURE,
1145         .type           = V4L2_CTRL_TYPE_INTEGER,
1146         .name           = "Exposure Control",
1147         .minimum        = 0,
1148         .maximum        = 6,
1149         .step           = 1,
1150         .default_value = 0,
1151     },
1152         #endif
1153
1154         #if CONFIG_SENSOR_Saturation
1155         {
1156         .id             = V4L2_CID_SATURATION,
1157         .type           = V4L2_CTRL_TYPE_INTEGER,
1158         .name           = "Saturation Control",
1159         .minimum        = 0,
1160         .maximum        = 2,
1161         .step           = 1,
1162         .default_value = 0,
1163     },
1164     #endif
1165
1166         #if CONFIG_SENSOR_Contrast
1167         {
1168         .id             = V4L2_CID_CONTRAST,
1169         .type           = V4L2_CTRL_TYPE_INTEGER,
1170         .name           = "Contrast Control",
1171         .minimum        = -3,
1172         .maximum        = 3,
1173         .step           = 1,
1174         .default_value = 0,
1175     },
1176         #endif
1177
1178         #if CONFIG_SENSOR_Mirror
1179         {
1180         .id             = V4L2_CID_HFLIP,
1181         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1182         .name           = "Mirror Control",
1183         .minimum        = 0,
1184         .maximum        = 1,
1185         .step           = 1,
1186         .default_value = 1,
1187     },
1188     #endif
1189
1190         #if CONFIG_SENSOR_Flip
1191         {
1192         .id             = V4L2_CID_VFLIP,
1193         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1194         .name           = "Flip Control",
1195         .minimum        = 0,
1196         .maximum        = 1,
1197         .step           = 1,
1198         .default_value = 1,
1199     },
1200     #endif
1201
1202         #if CONFIG_SENSOR_Scene
1203     {
1204         .id             = V4L2_CID_SCENE,
1205         .type           = V4L2_CTRL_TYPE_MENU,
1206         .name           = "Scene Control",
1207         .minimum        = 0,
1208         .maximum        = 1,
1209         .step           = 1,
1210         .default_value = 0,
1211     },
1212     #endif
1213
1214         #if CONFIG_SENSOR_DigitalZoom
1215     {
1216         .id             = V4L2_CID_ZOOM_RELATIVE,
1217         .type           = V4L2_CTRL_TYPE_INTEGER,
1218         .name           = "DigitalZoom Control",
1219         .minimum        = -1,
1220         .maximum        = 1,
1221         .step           = 1,
1222         .default_value = 0,
1223     }, {
1224         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1225         .type           = V4L2_CTRL_TYPE_INTEGER,
1226         .name           = "DigitalZoom Control",
1227         .minimum        = 0,
1228         .maximum        = 3,
1229         .step           = 1,
1230         .default_value = 0,
1231     },
1232     #endif
1233
1234         #if CONFIG_SENSOR_Focus
1235         {
1236         .id             = V4L2_CID_FOCUS_RELATIVE,
1237         .type           = V4L2_CTRL_TYPE_INTEGER,
1238         .name           = "Focus Control",
1239         .minimum        = -1,
1240         .maximum        = 1,
1241         .step           = 1,
1242         .default_value = 0,
1243     }, {
1244         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1245         .type           = V4L2_CTRL_TYPE_INTEGER,
1246         .name           = "Focus Control",
1247         .minimum        = 0,
1248         .maximum        = 255,
1249         .step           = 1,
1250         .default_value = 125,
1251     },
1252         {
1253         .id             = V4L2_CID_FOCUS_AUTO,
1254         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1255         .name           = "Focus Control",
1256         .minimum        = 0,
1257         .maximum        = 1,
1258         .step           = 1,
1259         .default_value = 0,
1260     },{
1261         .id             = V4L2_CID_FOCUS_CONTINUOUS,
1262         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1263         .name           = "Focus Control",
1264         .minimum        = 0,
1265         .maximum        = 1,
1266         .step           = 1,
1267         .default_value = 0,
1268     },
1269     #endif
1270
1271         #if CONFIG_SENSOR_Flash
1272         {
1273         .id             = V4L2_CID_FLASH,
1274         .type           = V4L2_CTRL_TYPE_MENU,
1275         .name           = "Flash Control",
1276         .minimum        = 0,
1277         .maximum        = 3,
1278         .step           = 1,
1279         .default_value = 0,
1280     },
1281         #endif
1282 };
1283
1284 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1285 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1286 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1287 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1288 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1289 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1290 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1291 static int sensor_resume(struct soc_camera_device *icd);
1292 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1293 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1294 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1295 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1296 static int sensor_deactivate(struct i2c_client *client);
1297
1298 static struct soc_camera_ops sensor_ops =
1299 {
1300     .suspend                     = sensor_suspend,
1301     .resume                       = sensor_resume,
1302     .set_bus_param              = sensor_set_bus_param,
1303     .query_bus_param    = sensor_query_bus_param,
1304     .controls           = sensor_controls,
1305     .menus                         = sensor_menus,
1306     .num_controls               = ARRAY_SIZE(sensor_controls),
1307     .num_menus          = ARRAY_SIZE(sensor_menus),
1308 };
1309
1310 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1311         { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1312         .colorspace = _colorspace }
1313
1314 #define JPG_FMT(_name, _depth, _fourcc) \
1315         COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1316
1317 static const struct soc_camera_data_format sensor_colour_formats[] = {
1318         JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1319         JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1320 };
1321 enum sensor_work_state
1322 {
1323         sensor_work_ready = 0,
1324         sensor_working,
1325 };
1326 struct sensor_work
1327 {
1328         struct i2c_client *client;
1329         struct delayed_work dwork;
1330         enum sensor_work_state state;
1331 };
1332
1333 typedef struct sensor_info_priv_s
1334 {
1335     int whiteBalance;
1336     int brightness;
1337     int contrast;
1338     int saturation;
1339     int effect;
1340     int scene;
1341     int digitalzoom;
1342     int focus;
1343         int auto_focus;
1344         int affm_reinit;
1345     int flash;
1346     int exposure;
1347         bool snap2preview;
1348         bool video2preview;
1349     unsigned char mirror;                                        /* HFLIP */
1350     unsigned char flip;                                          /* VFLIP */
1351     struct reginfo *winseqe_cur_addr;
1352         unsigned int pixfmt;
1353         unsigned int enable;
1354         unsigned int funmodule_state;
1355 } sensor_info_priv_t;
1356
1357
1358
1359 struct sensor_parameter
1360 {
1361         unsigned short int preview_maxlines;
1362         unsigned short int preview_exposure;
1363         unsigned short int preview_line_width;
1364         unsigned short int preview_gain;
1365
1366         unsigned short int capture_framerate;
1367         unsigned short int preview_framerate;
1368         char awb[6];
1369 };
1370
1371 struct sensor
1372 {
1373     struct v4l2_subdev subdev;
1374     struct i2c_client *client;
1375     sensor_info_priv_t info_priv;
1376         struct sensor_parameter parameter;
1377         struct workqueue_struct *sensor_wq;
1378         struct sensor_work sensor_wk;
1379         struct mutex wq_lock;
1380     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1381 #if CONFIG_SENSOR_I2C_NOSCHED
1382         atomic_t tasklock_cnt;
1383 #endif
1384         struct rk29camera_platform_data *sensor_io_request;
1385     struct rk29camera_gpio_res *sensor_gpio_res;
1386 };
1387
1388 static struct sensor* to_sensor(const struct i2c_client *client)
1389 {
1390     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1391 }
1392
1393 static int sensor_task_lock(struct i2c_client *client, int lock)
1394 {
1395 #if CONFIG_SENSOR_I2C_NOSCHED
1396         int cnt = 3;
1397     struct sensor *sensor = to_sensor(client);
1398
1399         if (lock) {
1400                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1401                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1402                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1403                                 msleep(35);
1404                                 cnt--;
1405                         }
1406                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1407                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1408                                 goto sensor_task_lock_err;
1409                         }
1410                         preempt_disable();
1411                 }
1412
1413                 atomic_add(1, &sensor->tasklock_cnt);
1414         } else {
1415                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1416                         atomic_sub(1, &sensor->tasklock_cnt);
1417
1418                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1419                                 preempt_enable();
1420                 }
1421         }
1422         return 0;
1423 sensor_task_lock_err:
1424         return -1;   
1425 #else
1426     return 0;
1427 #endif
1428
1429 }
1430
1431 /* sensor register write */
1432 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1433 {
1434     int err,cnt;
1435     u8 buf[3];
1436     struct i2c_msg msg[1];
1437
1438     buf[0] = reg >> 8;
1439     buf[1] = reg & 0xFF;
1440     buf[2] = val;
1441
1442     msg->addr = client->addr;
1443     msg->flags = client->flags;
1444     msg->buf = buf;
1445     msg->len = sizeof(buf);
1446     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1447     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1448
1449     cnt = 3;
1450     err = -EAGAIN;
1451
1452     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1453         err = i2c_transfer(client->adapter, msg, 1);
1454
1455         if (err >= 0) {
1456             return 0;
1457         } else {
1458             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1459             udelay(10);
1460         }
1461     }
1462
1463     return err;
1464 }
1465
1466 /* sensor register read */
1467 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1468 {
1469     int err,cnt;
1470     u8 buf[2];
1471     struct i2c_msg msg[2];
1472
1473     buf[0] = reg >> 8;
1474     buf[1] = reg & 0xFF;
1475
1476     msg[0].addr = client->addr;
1477     msg[0].flags = client->flags;
1478     msg[0].buf = buf;
1479     msg[0].len = sizeof(buf);
1480     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1481     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1482
1483     msg[1].addr = client->addr;
1484     msg[1].flags = client->flags|I2C_M_RD;
1485     msg[1].buf = buf;
1486     msg[1].len = 1;
1487     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1488     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1489
1490     cnt = 3;
1491     err = -EAGAIN;
1492     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1493         err = i2c_transfer(client->adapter, msg, 2);
1494
1495         if (err >= 0) {
1496             *val = buf[0];
1497             return 0;
1498         } else {
1499                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1500             udelay(10);
1501         }
1502     }
1503
1504     return err;
1505 }
1506
1507 /* write a array of registers  */
1508 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1509 {
1510     int err = 0, cnt;
1511     int i = 0;
1512 #if CONFIG_SENSOR_Focus
1513         struct sensor *sensor = to_sensor(client);
1514 #endif
1515 #if CONFIG_SENSOR_I2C_RDWRCHK
1516         char valchk;
1517 #endif
1518
1519         cnt = 0;
1520         if (sensor_task_lock(client, 1) < 0)
1521                 goto sensor_write_array_end;
1522     while (regarray[i].reg != SEQUENCE_END)
1523     {
1524         #if CONFIG_SENSOR_Focus
1525         if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1526                         SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1527                         err = -EINVAL;
1528                         goto sensor_write_array_end;
1529         }
1530                 #endif
1531
1532         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1533         if (err < 0)
1534         {
1535             if (cnt-- > 0) {
1536                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1537                                 i = 0;
1538                                 continue;
1539             } else {
1540                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1541                 err = -EPERM;
1542                                 goto sensor_write_array_end;
1543             }
1544         } else {
1545         #if CONFIG_SENSOR_I2C_RDWRCHK
1546                         sensor_read(client, regarray[i].reg, &valchk);
1547                         if (valchk != regarray[i].val)
1548                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1549                 #endif
1550         }
1551
1552         i++;
1553     }
1554
1555         #if CONFIG_SENSOR_Focus
1556         if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1557                 || (regarray == sensor_init_data)) {
1558                 sensor->info_priv.affm_reinit = 1;
1559         }
1560         #endif
1561
1562 sensor_write_array_end:
1563         sensor_task_lock(client,0);
1564     return err;
1565 }
1566 #if CONFIG_SENSOR_I2C_RDWRCHK
1567 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1568 {
1569     int cnt;
1570     int i = 0;
1571         char valchk;
1572
1573         cnt = 0;
1574         valchk = 0;
1575     while (regarray[i].reg != 0)
1576     {
1577                 sensor_read(client, regarray[i].reg, &valchk);
1578                 if (valchk != regarray[i].val)
1579                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1580
1581         i++;
1582     }
1583     return 0;
1584 }
1585 #endif
1586 #if CONFIG_SENSOR_Focus
1587 struct af_cmdinfo
1588 {
1589         char cmd_tag;
1590         char cmd_para[4];
1591         char validate_bit;
1592 };
1593 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1594 {
1595         int i;
1596         char read_tag=0xff,cnt;
1597
1598         if (cmdinfo) {
1599                 if (cmdinfo->validate_bit & 0x80) {
1600                         if (sensor_write(client, CMD_TAG_Reg, cmdinfo->cmd_tag)) {
1601                                 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1602                                 goto sensor_af_cmdset_err;
1603                         }
1604                         SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1605                 }
1606                 for (i=0; i<4; i++) {
1607                         if (cmdinfo->validate_bit & (1<<i)) {
1608                                 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1609                                         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]);
1610                                         goto sensor_af_cmdset_err;
1611                                 }
1612                                 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]);
1613                         }
1614                 }
1615         } else {
1616                 if (sensor_write(client, CMD_TAG_Reg, 0xff)) {
1617                         SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1618                         goto sensor_af_cmdset_err;
1619                 }
1620                 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1621         }
1622
1623         if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1624                 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1625                 goto sensor_af_cmdset_err;
1626         }
1627
1628         cnt = 0;
1629         do
1630         {
1631                 msleep(5);
1632                 if (sensor_read(client,CMD_TAG_Reg,&read_tag)){
1633                    SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1634                    break;
1635                 }
1636     } while((read_tag != 0x00)&& (cnt++<100));
1637
1638         SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1639         return 0;
1640 sensor_af_cmdset_err:
1641         return -1;
1642 }
1643
1644 static int sensor_af_idlechk(struct i2c_client *client)
1645 {
1646         int ret = 0;
1647         char state,cnt;
1648
1649         cnt = 0;
1650         do
1651         {
1652                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1653                 if (ret != 0){
1654                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1655                    ret = -1;
1656                    goto sensor_af_idlechk_end;
1657                 }
1658
1659                 if (state != S_IDLE) {
1660                         sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
1661                         msleep(1);
1662                         cnt++;
1663                 }
1664     } while((state != S_IDLE)&& (cnt<100));
1665
1666         ret = (state == S_IDLE) ? 0 : -1;
1667
1668 sensor_af_idlechk_end:
1669         return ret;
1670 }
1671
1672 static int sensor_af_single(struct i2c_client *client)
1673 {
1674         int ret = 0;
1675         char state,cnt;
1676
1677         if (sensor_af_idlechk(client))
1678                 goto sensor_af_single_end;
1679
1680         if (sensor_af_cmdset(client, SingleFocus_Cmd, NULL)) {
1681                 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1682                 ret = -1;
1683                 goto sensor_af_single_end;
1684         }
1685
1686         cnt = 0;
1687     do
1688     {
1689         if (cnt != 0) {
1690                         msleep(1);
1691         }
1692         cnt++;
1693                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1694                 if (ret != 0){
1695                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1696                    ret = -1;
1697                    goto sensor_af_single_end;
1698                 }
1699     }while((state == S_FOCUSING) && (cnt<100));
1700
1701         if (state != S_FOCUSED) {
1702         SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1703                 ret = -1;
1704                 goto sensor_af_single_end;
1705     }
1706
1707         sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
1708 sensor_af_single_end:
1709         return ret;
1710 }
1711
1712 static int sensor_af_const(struct i2c_client *client)
1713 {
1714         int ret = 0;
1715
1716         if (sensor_af_idlechk(client))
1717                 goto sensor_af_const_end;
1718
1719         if (sensor_af_cmdset(client, ConstFocus_Cmd, NULL)) {
1720                 SENSOR_TR("%s const focus mode set error!\n",SENSOR_NAME_STRING());
1721                 ret = -1;
1722                 goto sensor_af_const_end;
1723         }
1724 sensor_af_const_end:
1725         return ret;
1726 }
1727 static int sensor_af_pause2capture(struct i2c_client *client)
1728 {
1729         int ret = 0;
1730         char state,cnt;
1731
1732         if (sensor_af_cmdset(client, PauseFocus_Cmd, NULL)) {
1733                 SENSOR_TR("%s pause focus mode set error!\n",SENSOR_NAME_STRING());
1734                 ret = -1;
1735                 goto sensor_af_pause_end;
1736         }
1737
1738         cnt = 0;
1739     do
1740     {
1741         if (cnt != 0) {
1742                         msleep(1);
1743         }
1744         cnt++;
1745                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1746                 if (ret != 0){
1747                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1748                    ret = -1;
1749                    goto sensor_af_pause_end;
1750                 }
1751     }while((state != S_CAPTURE) && (cnt<100));
1752
1753         if (state != S_CAPTURE) {
1754         SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1755                 ret = -1;
1756                 goto sensor_af_pause_end;
1757     }
1758 sensor_af_pause_end:
1759         return ret;
1760 }
1761 static int sensor_af_zoneupdate(struct i2c_client *client)
1762 {
1763         int ret = 0;
1764
1765         if (sensor_af_idlechk(client))
1766                 goto sensor_af_zoneupdate_end;
1767
1768         if (sensor_af_cmdset(client, UpdateZone_Cmd, NULL)) {
1769                 SENSOR_TR("%s update zone fail!\n",SENSOR_NAME_STRING());
1770                 ret = -1;
1771                 goto sensor_af_zoneupdate_end;
1772         }
1773
1774 sensor_af_zoneupdate_end:
1775         return ret;
1776 }
1777 static int sensor_af_init(struct i2c_client *client)
1778 {
1779         int ret = 0;
1780         char state,cnt;
1781
1782         ret = sensor_write_array(client, sensor_af_firmware);
1783     if (ret != 0) {
1784        SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1785        ret = -1;
1786            goto sensor_af_init_end;
1787     }
1788
1789         cnt = 0;
1790     do
1791     {
1792         if (cnt != 0) {
1793                         msleep(1);
1794         }
1795         cnt++;
1796                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1797                 if (ret != 0){
1798                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1799                    ret = -1;
1800                    goto sensor_af_init_end;
1801                 }
1802     }while((state == S_STARTUP) && (cnt<100));
1803
1804     if (state != S_IDLE) {
1805         SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1806                 ret = -1;
1807                 goto sensor_af_init_end;
1808     }
1809
1810 sensor_af_init_end:
1811         SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1812         return ret;
1813 }
1814
1815 static int sensor_af_wq_function(struct i2c_client *client)
1816 {
1817         struct sensor *sensor = to_sensor(client);
1818         struct af_cmdinfo cmdinfo;
1819         int ret=0, focus_pos = 0xfe;
1820
1821         SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1822
1823         mutex_lock(&sensor->wq_lock);
1824         if (sensor_af_init(client)) {
1825                 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1826                 ret = -1;
1827         } else {
1828                 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1829
1830                 switch (sensor->info_priv.auto_focus)
1831                 {
1832                         case SENSOR_AF_MODE_INFINITY:
1833                         {
1834                                 focus_pos = 0x00;
1835                         }
1836                         case SENSOR_AF_MODE_MACRO:
1837                         {
1838                                 if (focus_pos != 0x00)
1839                                         focus_pos = 0xff;
1840
1841                                 sensor_af_idlechk(client);
1842                                 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
1843                                 cmdinfo.cmd_para[0] = focus_pos;
1844                                 cmdinfo.validate_bit = 0x81;
1845                                 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
1846                                 break;
1847                         }
1848                         case SENSOR_AF_MODE_AUTO:
1849                         {
1850                                 ret = sensor_af_single(client);
1851                                 break;
1852                         }
1853                         case SENSOR_AF_MODE_CONTINUOUS:
1854                         {
1855                                 ret = sensor_af_const(client);
1856                                 break;
1857                         }
1858                         case SENSOR_AF_MODE_CLOSE:
1859                         {
1860                                 ret = 0;
1861                                 break;
1862                         }
1863                         default:
1864             {
1865                                 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1866                 goto sensor_af_wq_function_end;
1867                         }
1868                 }
1869
1870                 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);
1871         }
1872
1873 sensor_af_wq_function_end:
1874         sensor->sensor_wk.state = sensor_work_ready;
1875         mutex_unlock(&sensor->wq_lock);
1876         return ret;
1877 }
1878 static void sensor_af_workqueue(struct work_struct *work)
1879 {
1880         struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
1881         struct i2c_client *client = sensor_work->client;
1882
1883         if (sensor_af_wq_function(client) < 0) {
1884                 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
1885         }
1886 }
1887 #endif
1888 static int sensor_parameter_record(struct i2c_client *client)
1889 {
1890         u8 ret_l,ret_m,ret_h;
1891         u8 tp_l,tp_m,tp_h;
1892         struct sensor *sensor = to_sensor(client);
1893
1894         sensor_write(client,0x3503,0x07);       //stop AE/AG
1895         sensor_write(client,0x3406,0x01);   //stop AWB
1896
1897         sensor_read(client,0x3500,&ret_h);
1898         sensor_read(client,0x3501, &ret_m);
1899         sensor_read(client,0x3502, &ret_l);
1900         tp_l = ret_l;
1901         tp_m = ret_m;
1902         tp_h = ret_h;
1903         SENSOR_DG(" %s Read 0x3500 = 0x%02x  0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
1904         sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
1905         sensor_read(client,0x350c, &ret_h);
1906         sensor_read(client,0x350d, &ret_l);
1907         sensor->parameter.preview_line_width = ret_h & 0xff;
1908         sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
1909         //Read back AGC Gain for preview
1910         sensor_read(client,0x350b, &tp_l);
1911         sensor->parameter.preview_gain = tp_l;
1912
1913         sensor->parameter.capture_framerate = 900;
1914         sensor->parameter.preview_framerate = 1500;
1915
1916         sensor_read(client,0x3400,&sensor->parameter.awb[0]);           //record awb value
1917         sensor_read(client,0x3401,&sensor->parameter.awb[1]);
1918         sensor_read(client,0x3402,&sensor->parameter.awb[2]);
1919         sensor_read(client,0x3403,&sensor->parameter.awb[3]);
1920         sensor_read(client,0x3404,&sensor->parameter.awb[4]);
1921         sensor_read(client,0x3405,&sensor->parameter.awb[5]);
1922
1923         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);
1924         return 0;
1925 }
1926 static int sensor_ae_transfer(struct i2c_client *client)
1927 {
1928         u8  ExposureLow;
1929         u8  ExposureMid;
1930         u8  ExposureHigh;
1931         u16 ulCapture_Exposure;
1932         u32 ulCapture_Exposure_Gain;
1933         u16  iCapture_Gain;
1934         u8   Lines_10ms;
1935         bool m_60Hz = 0;
1936         u8  reg_l = 0,reg_h =0;
1937         u16 Preview_Maxlines;
1938         u8  Gain;
1939         u32  Capture_MaxLines;
1940         struct sensor *sensor = to_sensor(client);
1941
1942         Preview_Maxlines = sensor->parameter.preview_line_width;
1943         Gain = sensor->parameter.preview_gain;
1944         sensor_read(client,0x350c, &reg_h);
1945         sensor_read(client,0x350d, &reg_l);
1946         Capture_MaxLines = reg_h & 0xff;
1947         Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
1948
1949         if(m_60Hz== 1) {
1950                 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
1951         } else {
1952                 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
1953         }
1954
1955         if(Preview_Maxlines == 0)
1956                 Preview_Maxlines = 1;
1957
1958         ulCapture_Exposure =
1959                 (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
1960         iCapture_Gain = (Gain & 0x0f) + 16;
1961         if (Gain & 0x10) {
1962                 iCapture_Gain = iCapture_Gain << 1;
1963         }
1964         if (Gain & 0x20) {
1965                 iCapture_Gain = iCapture_Gain << 1;
1966         }
1967         if (Gain & 0x40) {
1968                 iCapture_Gain = iCapture_Gain << 1;
1969         }
1970         if (Gain & 0x80) {
1971                 iCapture_Gain = iCapture_Gain << 1;
1972         }
1973         ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5);   //0ld value 2.5, ½â¾ö¹ýÁÁ
1974         if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
1975                 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
1976                 if (ulCapture_Exposure > Lines_10ms)
1977                 {
1978                         //ulCapture_Exposure *= 1.7;
1979                         ulCapture_Exposure /= Lines_10ms;
1980                         ulCapture_Exposure *= Lines_10ms;
1981                 }
1982         } else {
1983                 ulCapture_Exposure = Capture_MaxLines;
1984                 //ulCapture_Exposure_Gain *= 1.5;
1985         }
1986         if(ulCapture_Exposure == 0)
1987                 ulCapture_Exposure = 1;
1988         iCapture_Gain = (ulCapture_Exposure_Gain*2/ulCapture_Exposure + 1)/2;
1989         ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
1990         ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
1991         ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
1992
1993         Gain = 0;
1994         if (iCapture_Gain > 31) {
1995                 Gain |= 0x10;
1996                 iCapture_Gain = iCapture_Gain >> 1;
1997         }
1998         if (iCapture_Gain > 31) {
1999                 Gain |= 0x20;
2000                 iCapture_Gain = iCapture_Gain >> 1;
2001         }
2002         if (iCapture_Gain > 31) {
2003                 Gain |= 0x40;
2004                 iCapture_Gain = iCapture_Gain >> 1;
2005         }
2006         if (iCapture_Gain > 31) {
2007                 Gain |= 0x80;
2008                 iCapture_Gain = iCapture_Gain >> 1;
2009         }
2010         if (iCapture_Gain > 16)
2011                 Gain |= ((iCapture_Gain -16) & 0x0f);
2012         if(Gain == 0x10)
2013                 Gain = 0x11;
2014         // write the gain and exposure to 0x350* registers
2015         //m_iWrite0x350b=Gain;
2016         sensor_write(client,0x350b, Gain);
2017         //m_iWrite0x3502=ExposureLow;
2018         sensor_write(client,0x3502, ExposureLow);
2019         //m_iWrite0x3501=ExposureMid;
2020         sensor_write(client,0x3501, ExposureMid);
2021         //m_iWrite0x3500=ExposureHigh;
2022         sensor_write(client,0x3500, ExposureHigh);
2023         // SendToFile("Gain = 0x%x\r\n", Gain);
2024         // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2025         // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2026         // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2027         //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2028         //camera_timed_wait(200);
2029         //linzhk camera_timed_wait(500);
2030
2031         sensor_write(client,0x3400,sensor->parameter.awb[0]);           // resume awb value
2032         sensor_write(client,0x3401,sensor->parameter.awb[1]);
2033         sensor_write(client,0x3402,sensor->parameter.awb[2]);
2034         sensor_write(client,0x3403,sensor->parameter.awb[3]);
2035         sensor_write(client,0x3404,sensor->parameter.awb[4]);
2036         sensor_write(client,0x3405,sensor->parameter.awb[5]);
2037
2038         SENSOR_DG(" %s Write 0x350b = 0x%02x  0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2039         mdelay(100);
2040         return 0;
2041 }
2042 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2043 {
2044         struct soc_camera_link *icl = to_soc_camera_link(icd);
2045         int ret = 0;
2046
2047     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2048
2049         switch (cmd)
2050         {
2051                 case Sensor_PowerDown:
2052                 {
2053                         if (icl->powerdown) {
2054                                 ret = icl->powerdown(icd->pdev, on);
2055                                 if (ret == RK29_CAM_IO_SUCCESS) {
2056                                         if (on == 0) {
2057                                                 mdelay(2);
2058                                                 if (icl->reset)
2059                                                         icl->reset(icd->pdev);
2060                                         }
2061                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2062                                         ret = -ENODEV;
2063                                         goto sensor_power_end;
2064                                 }
2065                         }
2066                         break;
2067                 }
2068                 case Sensor_Flash:
2069                 {
2070                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2071                 struct sensor *sensor = to_sensor(client);
2072
2073                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2074                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2075                         }
2076                         break;
2077                 }
2078                 default:
2079                 {
2080                         SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2081                         break;
2082                 }
2083         }
2084
2085 sensor_power_end:
2086         return ret;
2087 }
2088 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2089 {
2090     struct i2c_client *client = sd->priv;
2091     struct soc_camera_device *icd = client->dev.platform_data;
2092     struct sensor *sensor = to_sensor(client);
2093         const struct v4l2_queryctrl *qctrl;
2094     char value;
2095     int ret,pid = 0;
2096
2097     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2098
2099         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2100                 ret = -ENODEV;
2101                 goto sensor_INIT_ERR;
2102         }
2103
2104     /* soft reset */
2105         if (sensor_task_lock(client,1)<0)
2106                 goto sensor_INIT_ERR;
2107     ret = sensor_write(client, 0x3008, 0x80);
2108     if (ret != 0) {
2109         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2110         ret = -ENODEV;
2111                 goto sensor_INIT_ERR;
2112     }
2113
2114     mdelay(5);  //delay 5 microseconds
2115         /* check if it is an sensor sensor */
2116     ret = sensor_read(client, 0x300a, &value);
2117     if (ret != 0) {
2118         SENSOR_TR("read chip id high byte failed\n");
2119         ret = -ENODEV;
2120         goto sensor_INIT_ERR;
2121     }
2122
2123     pid |= (value << 8);
2124
2125     ret = sensor_read(client, 0x300b, &value);
2126     if (ret != 0) {
2127         SENSOR_TR("read chip id low byte failed\n");
2128         ret = -ENODEV;
2129         goto sensor_INIT_ERR;
2130     }
2131
2132     pid |= (value & 0xff);
2133     SENSOR_DG("\n %s  pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2134
2135     if (pid == SENSOR_ID) {
2136         sensor->model = SENSOR_V4L2_IDENT;
2137     } else {
2138         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2139         ret = -ENODEV;
2140         goto sensor_INIT_ERR;
2141     }
2142
2143     ret = sensor_write_array(client, sensor_init_data);
2144     if (ret != 0)
2145     {
2146         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2147         goto sensor_INIT_ERR;
2148     }
2149         sensor_task_lock(client,0);
2150     //icd->user_width = SENSOR_INIT_WIDTH;
2151     //icd->user_height = SENSOR_INIT_HEIGHT;
2152     sensor->info_priv.winseqe_cur_addr  = SENSOR_INIT_WINSEQADR;
2153         sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
2154
2155     /* sensor sensor information for initialization  */
2156         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2157         if (qctrl)
2158         sensor->info_priv.whiteBalance = qctrl->default_value;
2159         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2160         if (qctrl)
2161         sensor->info_priv.brightness = qctrl->default_value;
2162         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2163         if (qctrl)
2164         sensor->info_priv.effect = qctrl->default_value;
2165         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2166         if (qctrl)
2167         sensor->info_priv.exposure = qctrl->default_value;
2168
2169         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2170         if (qctrl)
2171         sensor->info_priv.saturation = qctrl->default_value;
2172         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2173         if (qctrl)
2174         sensor->info_priv.contrast = qctrl->default_value;
2175         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2176         if (qctrl)
2177         sensor->info_priv.mirror = qctrl->default_value;
2178         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2179         if (qctrl)
2180         sensor->info_priv.flip = qctrl->default_value;
2181         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2182         if (qctrl)
2183         sensor->info_priv.scene = qctrl->default_value;
2184         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2185         if (qctrl)
2186         sensor->info_priv.digitalzoom = qctrl->default_value;
2187
2188     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
2189         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2190         if (qctrl)
2191         sensor->info_priv.focus = qctrl->default_value;
2192
2193         #if CONFIG_SENSOR_Flash
2194         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2195         if (qctrl)
2196         sensor->info_priv.flash = qctrl->default_value;
2197     #endif
2198     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);
2199
2200     return 0;
2201 sensor_INIT_ERR:
2202         sensor_task_lock(client,0);
2203         sensor_deactivate(client);
2204     return ret;
2205 }
2206 static int sensor_deactivate(struct i2c_client *client)
2207 {
2208         struct soc_camera_device *icd = client->dev.platform_data;
2209
2210         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2211
2212         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2213         sensor_task_lock(client, 1);
2214     sensor_write(client, 0x3017, 0x00);  // FREX,VSYNC,HREF,PCLK,D9-D6
2215         sensor_write(client, 0x3018, 0x03);  // D5-D0
2216         sensor_write(client,0x3019,0x00);    // STROBE,SDA
2217         sensor_task_lock(client, 0);
2218         sensor_ioctrl(icd, Sensor_PowerDown, 1);
2219         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2220         icd->user_width = SENSOR_INIT_WIDTH;
2221     icd->user_height = SENSOR_INIT_HEIGHT;
2222         msleep(100);
2223         return 0;
2224 }
2225 static  struct reginfo sensor_power_down_sequence[]=
2226 {
2227     {0x00,0x00}
2228 };
2229 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2230 {
2231     int ret;
2232     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2233
2234     if (pm_msg.event == PM_EVENT_SUSPEND) {
2235         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2236         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2237         if (ret != 0) {
2238             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2239             return ret;
2240         } else {
2241             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2242             if (ret < 0) {
2243                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2244                 return -EINVAL;
2245             }
2246         }
2247     } else {
2248         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2249         return -EINVAL;
2250     }
2251
2252     return 0;
2253 }
2254
2255 static int sensor_resume(struct soc_camera_device *icd)
2256 {
2257         int ret;
2258
2259     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2260     if (ret < 0) {
2261                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2262         return -EINVAL;
2263     }
2264
2265         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2266         return 0;
2267 }
2268
2269 static int sensor_set_bus_param(struct soc_camera_device *icd,
2270                                 unsigned long flags)
2271 {
2272
2273     return 0;
2274 }
2275
2276 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2277 {
2278     struct soc_camera_link *icl = to_soc_camera_link(icd);
2279     unsigned long flags = SENSOR_BUS_PARAM;
2280
2281     return soc_camera_apply_sensor_flags(icl, flags);
2282 }
2283
2284 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2285 {
2286     struct i2c_client *client = sd->priv;
2287     struct soc_camera_device *icd = client->dev.platform_data;
2288     struct sensor *sensor = to_sensor(client);
2289     struct v4l2_pix_format *pix = &f->fmt.pix;
2290
2291     pix->width          = icd->user_width;
2292     pix->height         = icd->user_height;
2293     pix->pixelformat    = sensor->info_priv.pixfmt;
2294     pix->field          = V4L2_FIELD_NONE;
2295     pix->colorspace             = V4L2_COLORSPACE_JPEG;
2296
2297     return 0;
2298 }
2299 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
2300 {
2301     bool ret = false;
2302
2303         if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
2304                 ret = true;
2305         } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
2306                 ret = true;
2307         } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
2308                 ret = true;
2309         } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
2310                 ret = true;
2311         } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
2312                 ret = true;
2313         }
2314
2315         if (ret == true)
2316                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2317         return ret;
2318 }
2319 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
2320 {
2321     bool ret = false;
2322
2323         if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
2324                 ret = true;
2325         } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
2326                 ret = true;
2327         }
2328
2329         if (ret == true)
2330                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2331         return ret;
2332 }
2333 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2334 {
2335     struct i2c_client *client = sd->priv;
2336     struct sensor *sensor = to_sensor(client);
2337     struct v4l2_pix_format *pix = &f->fmt.pix;
2338         const struct v4l2_queryctrl *qctrl;
2339         struct soc_camera_device *icd = client->dev.platform_data;
2340     struct reginfo *winseqe_set_addr=NULL;
2341     int ret = 0, set_w,set_h;
2342
2343         if (sensor->info_priv.pixfmt != pix->pixelformat) {
2344                 switch (pix->pixelformat)
2345                 {
2346                         case V4L2_PIX_FMT_YUYV:
2347                         {
2348                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2349                                 break;
2350                         }
2351                         case V4L2_PIX_FMT_UYVY:
2352                         {
2353                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2354                                 break;
2355                         }
2356                         default:
2357                                 break;
2358                 }
2359                 if (winseqe_set_addr != NULL) {
2360             sensor_write_array(client, winseqe_set_addr);
2361                         sensor->info_priv.pixfmt = pix->pixelformat;
2362
2363                         SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2364                 } else {
2365                         SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2366                 }
2367         }
2368
2369     set_w = pix->width;
2370     set_h = pix->height;
2371
2372         if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2373         {
2374                 winseqe_set_addr = sensor_qcif;
2375         set_w = 176;
2376         set_h = 144;
2377         }
2378         else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2379     {
2380         winseqe_set_addr = sensor_qvga;
2381         set_w = 320;
2382         set_h = 240;
2383     }
2384     else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2385     {
2386         winseqe_set_addr = sensor_cif;
2387         set_w = 352;
2388         set_h = 288;
2389     }
2390     else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2391     {
2392         winseqe_set_addr = sensor_vga;
2393         set_w = 640;
2394         set_h = 480;
2395     }
2396     else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2397     {
2398         winseqe_set_addr = sensor_svga;
2399         set_w = 800;
2400         set_h = 600;
2401     }
2402         else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2403     {
2404         winseqe_set_addr = sensor_xga;
2405         set_w = 1024;
2406         set_h = 768;
2407     }
2408         else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2409     {
2410         winseqe_set_addr = sensor_720p;
2411         set_w = 1280;
2412         set_h = 720;
2413     }
2414     else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2415     {
2416         winseqe_set_addr = sensor_sxga;
2417         set_w = 1280;
2418         set_h = 1024;
2419     }
2420     else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2421     {
2422         winseqe_set_addr = sensor_uxga;
2423         set_w = 1600;
2424         set_h = 1200;
2425     }
2426     else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2427     {
2428         winseqe_set_addr = sensor_1080p;
2429         set_w = 1920;
2430         set_h = 1080;
2431     }
2432         else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2433     {
2434         winseqe_set_addr = sensor_qxga;
2435         set_w = 2048;
2436         set_h = 1536;
2437     }
2438         else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2439     {
2440         winseqe_set_addr = sensor_qsxga;
2441         set_w = 2592;
2442         set_h = 1944;
2443     }
2444     else
2445     {
2446         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2447         set_w = SENSOR_INIT_WIDTH;
2448         set_h = SENSOR_INIT_HEIGHT;
2449                 ret = -1;
2450                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
2451     }
2452
2453     if (winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr)
2454     {
2455                 if (sensor_fmt_capturechk(sd,f) == true) {                                      /* ddl@rock-chips.com : Capture */
2456                         sensor_parameter_record(client);
2457                 #if CONFIG_SENSOR_Focus
2458                         sensor_af_idlechk(client);
2459                         if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2460                                 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2461                 #endif
2462                 #if CONFIG_SENSOR_Flash
2463             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2464                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2465                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2466             }
2467         #endif
2468                 }else {                                        /* ddl@rock-chips.com : Video */
2469                 #if CONFIG_SENSOR_Flash
2470             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2471                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2472                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2473             }
2474         #endif
2475         }
2476
2477                 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2478                         if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2479                                 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2480                                 ret |= sensor_write_array(client,sensor_init_data);
2481                                 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2482                         }
2483                 }
2484
2485         ret |= sensor_write_array(client, winseqe_set_addr);
2486         if (ret != 0) {
2487             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2488             #if CONFIG_SENSOR_Flash
2489             if (sensor_fmt_capturechk(sd,f) == true) {
2490                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2491                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2492                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2493                 }
2494             }
2495             #endif
2496             goto sensor_s_fmt_end;
2497         }
2498
2499         sensor->info_priv.winseqe_cur_addr  = winseqe_set_addr;
2500
2501                 if (sensor_fmt_capturechk(sd,f) == true) {                                  /* ddl@rock-chips.com : Capture */
2502                         sensor_ae_transfer(client);
2503                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2504                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2505                         if (sensor->info_priv.whiteBalance != 0) {
2506                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2507                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2508                         }
2509                         sensor->info_priv.snap2preview = true;
2510                 } else if (sensor_fmt_videochk(sd,f) == true) {                 /* ddl@rock-chips.com : Video */
2511                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2512                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2513
2514                         sensor->info_priv.video2preview = true;
2515                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2516                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2517                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2518                         if (sensor->info_priv.snap2preview == true) {
2519                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2520                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2521                         }
2522                         sensor->info_priv.video2preview = false;
2523                         sensor->info_priv.snap2preview = false;
2524                 }
2525         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2526     }
2527     else
2528     {
2529         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2530     }
2531
2532         pix->width = set_w;
2533         pix->height = set_h;
2534 sensor_s_fmt_end:
2535     return ret;
2536 }
2537
2538 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2539 {
2540     struct v4l2_pix_format *pix = &f->fmt.pix;
2541     bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
2542         pix->pixelformat == V4L2_PIX_FMT_YUYV;
2543
2544     /*
2545     * With Bayer format enforce even side lengths, but let the user play
2546     * with the starting pixel
2547     */
2548
2549     if (pix->height > SENSOR_MAX_HEIGHT)
2550         pix->height = SENSOR_MAX_HEIGHT;
2551     else if (pix->height < SENSOR_MIN_HEIGHT)
2552         pix->height = SENSOR_MIN_HEIGHT;
2553     else if (bayer)
2554         pix->height = ALIGN(pix->height, 2);
2555
2556     if (pix->width > SENSOR_MAX_WIDTH)
2557         pix->width = SENSOR_MAX_WIDTH;
2558     else if (pix->width < SENSOR_MIN_WIDTH)
2559         pix->width = SENSOR_MIN_WIDTH;
2560     else if (bayer)
2561         pix->width = ALIGN(pix->width, 2);
2562
2563     return 0;
2564 }
2565
2566  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2567 {
2568     struct i2c_client *client = sd->priv;
2569
2570     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2571         return -EINVAL;
2572
2573     if (id->match.addr != client->addr)
2574         return -ENODEV;
2575
2576     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2577     id->revision = 0;
2578
2579     return 0;
2580 }
2581 #if CONFIG_SENSOR_Brightness
2582 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2583 {
2584     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2585
2586     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2587     {
2588         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2589         {
2590             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2591             {
2592                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2593                 return -EINVAL;
2594             }
2595             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2596             return 0;
2597         }
2598     }
2599         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2600     return -EINVAL;
2601 }
2602 #endif
2603 #if CONFIG_SENSOR_Effect
2604 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2605 {
2606     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2607
2608     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2609     {
2610         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2611         {
2612             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2613             {
2614                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2615                 return -EINVAL;
2616             }
2617             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2618             return 0;
2619         }
2620     }
2621         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2622     return -EINVAL;
2623 }
2624 #endif
2625 #if CONFIG_SENSOR_Exposure
2626 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2627 {
2628     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2629
2630     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2631     {
2632         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2633         {
2634             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2635             {
2636                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2637                 return -EINVAL;
2638             }
2639             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2640             return 0;
2641         }
2642     }
2643         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2644     return -EINVAL;
2645 }
2646 #endif
2647 #if CONFIG_SENSOR_Saturation
2648 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2649 {
2650     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2651
2652     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2653     {
2654         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2655         {
2656             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2657             {
2658                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2659                 return -EINVAL;
2660             }
2661             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2662             return 0;
2663         }
2664     }
2665     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2666     return -EINVAL;
2667 }
2668 #endif
2669 #if CONFIG_SENSOR_Contrast
2670 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2671 {
2672     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2673
2674     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2675     {
2676         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2677         {
2678             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2679             {
2680                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2681                 return -EINVAL;
2682             }
2683             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2684             return 0;
2685         }
2686     }
2687     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2688     return -EINVAL;
2689 }
2690 #endif
2691 #if CONFIG_SENSOR_Mirror
2692 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2693 {
2694     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2695
2696     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2697     {
2698         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2699         {
2700             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2701             {
2702                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2703                 return -EINVAL;
2704             }
2705             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2706             return 0;
2707         }
2708     }
2709     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2710     return -EINVAL;
2711 }
2712 #endif
2713 #if CONFIG_SENSOR_Flip
2714 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2715 {
2716     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2717
2718     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2719     {
2720         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2721         {
2722             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2723             {
2724                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2725                 return -EINVAL;
2726             }
2727             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2728             return 0;
2729         }
2730     }
2731     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2732     return -EINVAL;
2733 }
2734 #endif
2735 #if CONFIG_SENSOR_Scene
2736 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2737 {
2738     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2739
2740     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2741     {
2742         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2743         {
2744             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2745             {
2746                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2747                 return -EINVAL;
2748             }
2749             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2750             return 0;
2751         }
2752     }
2753     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2754     return -EINVAL;
2755 }
2756 #endif
2757 #if CONFIG_SENSOR_WhiteBalance
2758 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2759 {
2760     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2761
2762     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2763     {
2764         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2765         {
2766             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2767             {
2768                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2769                 return -EINVAL;
2770             }
2771             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2772             return 0;
2773         }
2774     }
2775         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2776     return -EINVAL;
2777 }
2778 #endif
2779 #if CONFIG_SENSOR_DigitalZoom
2780 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2781 {
2782     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2783     struct sensor *sensor = to_sensor(client);
2784         const struct v4l2_queryctrl *qctrl_info;
2785     int digitalzoom_cur, digitalzoom_total;
2786
2787         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2788         if (qctrl_info)
2789                 return -EINVAL;
2790
2791     digitalzoom_cur = sensor->info_priv.digitalzoom;
2792     digitalzoom_total = qctrl_info->maximum;
2793
2794     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2795     {
2796         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2797         return -EINVAL;
2798     }
2799
2800     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2801     {
2802         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2803         return -EINVAL;
2804     }
2805
2806     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2807     {
2808         *value = digitalzoom_total - digitalzoom_cur;
2809     }
2810
2811     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2812     {
2813         *value = 0 - digitalzoom_cur;
2814     }
2815
2816     digitalzoom_cur += *value;
2817
2818     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2819     {
2820         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2821         {
2822             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2823             return -EINVAL;
2824         }
2825         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2826         return 0;
2827     }
2828
2829     return -EINVAL;
2830 }
2831 #endif
2832 #if CONFIG_SENSOR_Focus
2833 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2834 {
2835         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2836     struct sensor *sensor = to_sensor(client);
2837         const struct v4l2_queryctrl *qctrl_info;
2838         struct af_cmdinfo cmdinfo;
2839         int ret = 0;
2840
2841         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2842         if (!qctrl_info)
2843                 return -EINVAL;
2844
2845         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2846                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2847
2848                         if (sensor_af_idlechk(client))
2849                                 goto sensor_set_focus_absolute_end;
2850
2851                         cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2852                         cmdinfo.cmd_para[0] = value;
2853                         cmdinfo.validate_bit = 0x81;
2854                         ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
2855                         //ret |= sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
2856                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2857                 } else {
2858                         ret = -EINVAL;
2859                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2860                 }
2861         } else {
2862                 ret = -EACCES;
2863                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2864                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2865         }
2866
2867 sensor_set_focus_absolute_end:
2868         return ret;
2869 }
2870 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2871 {
2872         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2873         struct sensor *sensor = to_sensor(client);
2874         const struct v4l2_queryctrl *qctrl_info;
2875         struct af_cmdinfo cmdinfo;
2876         int ret = 0;
2877
2878         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
2879         if (!qctrl_info)
2880                 return -EINVAL;
2881
2882         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2883                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2884
2885                         if (sensor_af_idlechk(client))
2886                                 goto sensor_set_focus_relative_end;
2887
2888                         if (value > 0) {
2889                                 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2890                         } else if (value < 0) {
2891                                 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2892                         }
2893                         cmdinfo.validate_bit = 0x80;
2894                         ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
2895
2896                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2897                 } else {
2898                         ret = -EINVAL;
2899                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2900                 }
2901         } else {
2902                 ret = -EACCES;
2903                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2904                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2905         }
2906 sensor_set_focus_relative_end:
2907         return ret;
2908 }
2909
2910 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2911 {
2912         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2913         struct sensor *sensor = to_sensor(client);
2914         int ret = 0;
2915
2916         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)  && (sensor->info_priv.affm_reinit == 0)) {
2917                 switch (value)
2918                 {
2919                         case SENSOR_AF_MODE_AUTO:
2920                         {
2921                                 ret = sensor_af_single(client);
2922                                 break;
2923                         }
2924
2925                         case SENSOR_AF_MODE_MACRO:
2926                         {
2927                                 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
2928                                 break;
2929                         }
2930
2931                         case SENSOR_AF_MODE_INFINITY:
2932                         {
2933                                 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
2934                                 break;
2935                         }
2936
2937                         case SENSOR_AF_MODE_CONTINUOUS:
2938                         {
2939                                 ret = sensor_af_const(client);
2940                                 break;
2941                         }
2942                         default:
2943                                 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2944                                 break;
2945
2946                 }
2947
2948                 SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2949         } else {
2950                 ret = -EACCES;
2951                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2952                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2953         }
2954
2955         return ret;
2956 }
2957 #endif
2958 #if CONFIG_SENSOR_Flash
2959 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2960 {    
2961     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2962         if (value == 3) {       /* ddl@rock-chips.com: torch */
2963             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2964         } else {
2965             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2966         }
2967         SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2968         return 0;
2969     }
2970     
2971         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2972     return -EINVAL;
2973 }
2974 #endif
2975 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2976 {
2977     struct i2c_client *client = sd->priv;
2978     struct sensor *sensor = to_sensor(client);
2979     const struct v4l2_queryctrl *qctrl;
2980
2981     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2982
2983     if (!qctrl)
2984     {
2985         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2986         return -EINVAL;
2987     }
2988
2989     switch (ctrl->id)
2990     {
2991         case V4L2_CID_BRIGHTNESS:
2992             {
2993                 ctrl->value = sensor->info_priv.brightness;
2994                 break;
2995             }
2996         case V4L2_CID_SATURATION:
2997             {
2998                 ctrl->value = sensor->info_priv.saturation;
2999                 break;
3000             }
3001         case V4L2_CID_CONTRAST:
3002             {
3003                 ctrl->value = sensor->info_priv.contrast;
3004                 break;
3005             }
3006         case V4L2_CID_DO_WHITE_BALANCE:
3007             {
3008                 ctrl->value = sensor->info_priv.whiteBalance;
3009                 break;
3010             }
3011         case V4L2_CID_EXPOSURE:
3012             {
3013                 ctrl->value = sensor->info_priv.exposure;
3014                 break;
3015             }
3016         case V4L2_CID_HFLIP:
3017             {
3018                 ctrl->value = sensor->info_priv.mirror;
3019                 break;
3020             }
3021         case V4L2_CID_VFLIP:
3022             {
3023                 ctrl->value = sensor->info_priv.flip;
3024                 break;
3025             }
3026         default :
3027                 break;
3028     }
3029     return 0;
3030 }
3031
3032
3033
3034 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3035 {
3036     struct i2c_client *client = sd->priv;
3037     struct sensor *sensor = to_sensor(client);
3038     struct soc_camera_device *icd = client->dev.platform_data;
3039     const struct v4l2_queryctrl *qctrl;
3040
3041
3042     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3043
3044     if (!qctrl)
3045     {
3046         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3047         return -EINVAL;
3048     }
3049
3050     switch (ctrl->id)
3051     {
3052 #if CONFIG_SENSOR_Brightness
3053         case V4L2_CID_BRIGHTNESS:
3054             {
3055                 if (ctrl->value != sensor->info_priv.brightness)
3056                 {
3057                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3058                     {
3059                         return -EINVAL;
3060                     }
3061                     sensor->info_priv.brightness = ctrl->value;
3062                 }
3063                 break;
3064             }
3065 #endif
3066 #if CONFIG_SENSOR_Exposure
3067         case V4L2_CID_EXPOSURE:
3068             {
3069                 if (ctrl->value != sensor->info_priv.exposure)
3070                 {
3071                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3072                     {
3073                         return -EINVAL;
3074                     }
3075                     sensor->info_priv.exposure = ctrl->value;
3076                 }
3077                 break;
3078             }
3079 #endif
3080 #if CONFIG_SENSOR_Saturation
3081         case V4L2_CID_SATURATION:
3082             {
3083                 if (ctrl->value != sensor->info_priv.saturation)
3084                 {
3085                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3086                     {
3087                         return -EINVAL;
3088                     }
3089                     sensor->info_priv.saturation = ctrl->value;
3090                 }
3091                 break;
3092             }
3093 #endif
3094 #if CONFIG_SENSOR_Contrast
3095         case V4L2_CID_CONTRAST:
3096             {
3097                 if (ctrl->value != sensor->info_priv.contrast)
3098                 {
3099                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3100                     {
3101                         return -EINVAL;
3102                     }
3103                     sensor->info_priv.contrast = ctrl->value;
3104                 }
3105                 break;
3106             }
3107 #endif
3108 #if CONFIG_SENSOR_WhiteBalance
3109         case V4L2_CID_DO_WHITE_BALANCE:
3110             {
3111                 if (ctrl->value != sensor->info_priv.whiteBalance)
3112                 {
3113                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3114                     {
3115                         return -EINVAL;
3116                     }
3117                     sensor->info_priv.whiteBalance = ctrl->value;
3118                 }
3119                 break;
3120             }
3121 #endif
3122 #if CONFIG_SENSOR_Mirror
3123         case V4L2_CID_HFLIP:
3124             {
3125                 if (ctrl->value != sensor->info_priv.mirror)
3126                 {
3127                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3128                         return -EINVAL;
3129                     sensor->info_priv.mirror = ctrl->value;
3130                 }
3131                 break;
3132             }
3133 #endif
3134 #if CONFIG_SENSOR_Flip
3135         case V4L2_CID_VFLIP:
3136             {
3137                 if (ctrl->value != sensor->info_priv.flip)
3138                 {
3139                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3140                         return -EINVAL;
3141                     sensor->info_priv.flip = ctrl->value;
3142                 }
3143                 break;
3144             }
3145 #endif
3146         default:
3147             break;
3148     }
3149
3150     return 0;
3151 }
3152 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3153 {
3154     const struct v4l2_queryctrl *qctrl;
3155     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3156     struct sensor *sensor = to_sensor(client);
3157
3158     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3159
3160     if (!qctrl)
3161     {
3162         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3163         return -EINVAL;
3164     }
3165
3166     switch (ext_ctrl->id)
3167     {
3168         case V4L2_CID_SCENE:
3169             {
3170                 ext_ctrl->value = sensor->info_priv.scene;
3171                 break;
3172             }
3173         case V4L2_CID_EFFECT:
3174             {
3175                 ext_ctrl->value = sensor->info_priv.effect;
3176                 break;
3177             }
3178         case V4L2_CID_ZOOM_ABSOLUTE:
3179             {
3180                 ext_ctrl->value = sensor->info_priv.digitalzoom;
3181                 break;
3182             }
3183         case V4L2_CID_ZOOM_RELATIVE:
3184             {
3185                 return -EINVAL;
3186             }
3187         case V4L2_CID_FOCUS_ABSOLUTE:
3188             {
3189                 return -EINVAL;
3190             }
3191         case V4L2_CID_FOCUS_RELATIVE:
3192             {
3193                 return -EINVAL;
3194             }
3195         case V4L2_CID_FLASH:
3196             {
3197                 ext_ctrl->value = sensor->info_priv.flash;
3198                 break;
3199             }
3200         default :
3201             break;
3202     }
3203     return 0;
3204 }
3205 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3206 {
3207     const struct v4l2_queryctrl *qctrl;
3208     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3209     struct sensor *sensor = to_sensor(client);
3210     int val_offset;
3211
3212     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3213
3214     if (!qctrl)
3215     {
3216         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3217         return -EINVAL;
3218     }
3219
3220         val_offset = 0;
3221     switch (ext_ctrl->id)
3222     {
3223 #if CONFIG_SENSOR_Scene
3224         case V4L2_CID_SCENE:
3225             {
3226                 if (ext_ctrl->value != sensor->info_priv.scene)
3227                 {
3228                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3229                         return -EINVAL;
3230                     sensor->info_priv.scene = ext_ctrl->value;
3231                 }
3232                 break;
3233             }
3234 #endif
3235 #if CONFIG_SENSOR_Effect
3236         case V4L2_CID_EFFECT:
3237             {
3238                 if (ext_ctrl->value != sensor->info_priv.effect)
3239                 {
3240                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3241                         return -EINVAL;
3242                     sensor->info_priv.effect= ext_ctrl->value;
3243                 }
3244                 break;
3245             }
3246 #endif
3247 #if CONFIG_SENSOR_DigitalZoom
3248         case V4L2_CID_ZOOM_ABSOLUTE:
3249             {
3250                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3251                     return -EINVAL;
3252
3253                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3254                 {
3255                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3256
3257                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3258                         return -EINVAL;
3259                     sensor->info_priv.digitalzoom += val_offset;
3260
3261                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
3262                 }
3263
3264                 break;
3265             }
3266         case V4L2_CID_ZOOM_RELATIVE:
3267             {
3268                 if (ext_ctrl->value)
3269                 {
3270                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3271                         return -EINVAL;
3272                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3273
3274                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3275                 }
3276                 break;
3277             }
3278 #endif
3279 #if CONFIG_SENSOR_Focus
3280         case V4L2_CID_FOCUS_ABSOLUTE:
3281             {
3282                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3283                     return -EINVAL;
3284
3285                                 if (sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value) == 0) {
3286                                         if (ext_ctrl->value == qctrl->minimum) {
3287                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
3288                                         } else if (ext_ctrl->value == qctrl->maximum) {
3289                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
3290                                         } else {
3291                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
3292                                         }
3293                                 }
3294
3295                 break;
3296             }
3297         case V4L2_CID_FOCUS_RELATIVE:
3298             {
3299                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3300                     return -EINVAL;
3301
3302                 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3303                 break;
3304             }
3305                 case V4L2_CID_FOCUS_AUTO:
3306                         {
3307                                 if (ext_ctrl->value == 1) {
3308                                         if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
3309                                                 return -EINVAL;
3310                                         sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3311                                 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3312                                         if (ext_ctrl->value == 0)
3313                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3314                                 }
3315                                 break;
3316                         }
3317                 case V4L2_CID_FOCUS_CONTINUOUS:
3318                         {
3319                                 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3320                                         if (ext_ctrl->value == 1) {
3321                                                 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3322                                                         return -EINVAL;
3323                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3324                                         }
3325                                 } else {
3326                                         if (ext_ctrl->value == 0)
3327                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3328                                 }
3329                                 break;
3330                         }
3331 #endif
3332 #if CONFIG_SENSOR_Flash
3333         case V4L2_CID_FLASH:
3334             {
3335                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3336                     return -EINVAL;
3337                 sensor->info_priv.flash = ext_ctrl->value;
3338
3339                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3340                 break;
3341             }
3342 #endif
3343         default:
3344             break;
3345     }
3346
3347     return 0;
3348 }
3349
3350 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3351 {
3352     struct i2c_client *client = sd->priv;
3353     struct soc_camera_device *icd = client->dev.platform_data;
3354     int i, error_cnt=0, error_idx=-1;
3355
3356
3357     for (i=0; i<ext_ctrl->count; i++) {
3358         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3359             error_cnt++;
3360             error_idx = i;
3361         }
3362     }
3363
3364     if (error_cnt > 1)
3365         error_idx = ext_ctrl->count;
3366
3367     if (error_idx != -1) {
3368         ext_ctrl->error_idx = error_idx;
3369         return -EINVAL;
3370     } else {
3371         return 0;
3372     }
3373 }
3374
3375 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3376 {
3377     struct i2c_client *client = sd->priv;
3378     struct soc_camera_device *icd = client->dev.platform_data;
3379     int i, error_cnt=0, error_idx=-1;
3380
3381     for (i=0; i<ext_ctrl->count; i++) {
3382         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3383             error_cnt++;
3384             error_idx = i;
3385         }
3386     }
3387
3388     if (error_cnt > 1)
3389         error_idx = ext_ctrl->count;
3390
3391     if (error_idx != -1) {
3392         ext_ctrl->error_idx = error_idx;
3393         return -EINVAL;
3394     } else {
3395         return 0;
3396     }
3397 }
3398
3399 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3400 {
3401         struct i2c_client *client = sd->priv;
3402     struct sensor *sensor = to_sensor(client);
3403         #if CONFIG_SENSOR_Focus
3404         struct soc_camera_device *icd = client->dev.platform_data;
3405         struct v4l2_format fmt;
3406         #endif
3407
3408         if (enable == 1) {
3409                 sensor->info_priv.enable = 1;
3410                 #if CONFIG_SENSOR_Focus
3411                 fmt.fmt.pix.width = icd->user_width;
3412                 fmt.fmt.pix.height = icd->user_height;
3413                 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3414                 if (sensor_fmt_capturechk(sd, &fmt) == false) {
3415                         if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3416                                 if (sensor->sensor_wq != NULL) {
3417                                         mutex_lock(&sensor->wq_lock);
3418                                         if (sensor->sensor_wk.state == sensor_working) {
3419                                                 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
3420                                                 mutex_unlock(&sensor->wq_lock);
3421                                                 goto sensor_s_stream_end;
3422                                         }
3423                                         sensor->sensor_wk.state = sensor_working;
3424                                         mutex_unlock(&sensor->wq_lock);
3425                                         sensor->sensor_wk.client = client;
3426                                         INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
3427                                         queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
3428                                 }
3429                                 sensor->info_priv.affm_reinit = 0;
3430                         }
3431                 }
3432                 #endif
3433         } else if (enable == 0) {
3434                 sensor->info_priv.enable = 0;
3435                 #if CONFIG_SENSOR_Focus
3436                 flush_work(&(sensor->sensor_wk.dwork.work));
3437                 mutex_lock(&sensor->wq_lock);
3438                 sensor->sensor_wk.state = sensor_work_ready;
3439                 mutex_unlock(&sensor->wq_lock);
3440                 #endif
3441         }
3442
3443 sensor_s_stream_end:
3444         return 0;
3445 }
3446
3447 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3448  * this wasn't our capture interface, so, we wait for the right one */
3449 static int sensor_video_probe(struct soc_camera_device *icd,
3450                                struct i2c_client *client)
3451 {
3452     char value;
3453     int ret,pid = 0;
3454     struct sensor *sensor = to_sensor(client);
3455
3456     /* We must have a parent by now. And it cannot be a wrong one.
3457      * So this entire test is completely redundant. */
3458     if (!icd->dev.parent ||
3459             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3460                 return -ENODEV;
3461
3462         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3463                 ret = -ENODEV;
3464                 goto sensor_video_probe_err;
3465         }
3466     /* soft reset */
3467     ret = sensor_write(client, 0x3012, 0x80);
3468     if (ret != 0) {
3469         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3470         ret = -ENODEV;
3471                 goto sensor_video_probe_err;
3472     }
3473     mdelay(5);          //delay 5 microseconds
3474
3475     /* check if it is an sensor sensor */
3476     ret = sensor_read(client, 0x300a, &value);
3477     if (ret != 0) {
3478         SENSOR_TR("read chip id high byte failed\n");
3479         ret = -ENODEV;
3480         goto sensor_video_probe_err;
3481     }
3482
3483     pid |= (value << 8);
3484
3485     ret = sensor_read(client, 0x300b, &value);
3486     if (ret != 0) {
3487         SENSOR_TR("read chip id low byte failed\n");
3488         ret = -ENODEV;
3489         goto sensor_video_probe_err;
3490     }
3491
3492     pid |= (value & 0xff);
3493     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3494     if (pid == SENSOR_ID) {
3495         sensor->model = SENSOR_V4L2_IDENT;
3496     } else {
3497         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3498         ret = -ENODEV;
3499         goto sensor_video_probe_err;
3500     }
3501
3502     icd->formats = sensor_colour_formats;
3503     icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
3504
3505     return 0;
3506
3507 sensor_video_probe_err:
3508     return ret;
3509 }
3510 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3511 {
3512         struct i2c_client *client = sd->priv;
3513     struct soc_camera_device *icd = client->dev.platform_data;
3514     struct sensor *sensor = to_sensor(client);
3515     int ret = 0,i;
3516
3517         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3518         switch (cmd)
3519         {
3520                 case RK29_CAM_SUBDEV_DEACTIVATE:
3521                 {
3522                         sensor_deactivate(client);
3523                         break;
3524                 }
3525                 case RK29_CAM_SUBDEV_IOREQUEST:
3526                 {
3527                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
3528             if (sensor->sensor_io_request != NULL) { 
3529                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
3530                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
3531                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
3532                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
3533                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
3534                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
3535                 }
3536             } else {
3537                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3538                 ret = -EINVAL;
3539                 goto sensor_ioctl_end;
3540             }
3541             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
3542                for this project */
3543             #if CONFIG_SENSOR_Flash     
3544                 if (sensor->sensor_gpio_res) {
3545                 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3546                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3547                     for (i = 0; i < icd->ops->num_controls; i++) {
3548                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3549                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
3550                                 }
3551                     }
3552                     sensor->info_priv.flash = 0xff;
3553                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3554                 }
3555                 }
3556             #endif
3557                         break;
3558                 }
3559                 default:
3560                 {
3561                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3562                         break;
3563                 }
3564         }
3565
3566 sensor_ioctl_end:
3567         return ret;
3568
3569 }
3570
3571 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3572         .init           = sensor_init,
3573         .g_ctrl         = sensor_g_control,
3574         .s_ctrl         = sensor_s_control,
3575         .g_ext_ctrls          = sensor_g_ext_controls,
3576         .s_ext_ctrls          = sensor_s_ext_controls,
3577         .g_chip_ident   = sensor_g_chip_ident,
3578         .ioctl = sensor_ioctl,
3579 };
3580
3581 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3582         .s_fmt          = sensor_s_fmt,
3583         .g_fmt          = sensor_g_fmt,
3584         .try_fmt        = sensor_try_fmt,
3585         .s_stream   = sensor_s_stream,
3586 };
3587
3588 static struct v4l2_subdev_ops sensor_subdev_ops = {
3589         .core   = &sensor_subdev_core_ops,
3590         .video = &sensor_subdev_video_ops,
3591 };
3592
3593 static int sensor_probe(struct i2c_client *client,
3594                          const struct i2c_device_id *did)
3595 {
3596     struct sensor *sensor;
3597     struct soc_camera_device *icd = client->dev.platform_data;
3598     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3599     struct soc_camera_link *icl;
3600     int ret;
3601
3602     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3603     if (!icd) {
3604         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3605         return -EINVAL;
3606     }
3607
3608     icl = to_soc_camera_link(icd);
3609     if (!icl) {
3610         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3611         return -EINVAL;
3612     }
3613
3614     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3615         dev_warn(&adapter->dev,
3616                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3617         return -EIO;
3618     }
3619
3620     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3621     if (!sensor)
3622         return -ENOMEM;
3623
3624     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3625
3626     /* Second stage probe - when a capture adapter is there */
3627     icd->ops            = &sensor_ops;
3628     icd->y_skip_top             = 0;
3629         #if CONFIG_SENSOR_I2C_NOSCHED
3630         atomic_set(&sensor->tasklock_cnt,0);
3631         #endif
3632
3633     ret = sensor_video_probe(icd, client);
3634     if (ret < 0) {
3635         icd->ops = NULL;
3636         i2c_set_clientdata(client, NULL);
3637         kfree(sensor);
3638                 sensor = NULL;
3639     } else {
3640                 #if CONFIG_SENSOR_Focus
3641                 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
3642                 if (sensor->sensor_wq == NULL)
3643                         SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
3644                 mutex_init(&sensor->wq_lock);
3645                 sensor->sensor_wk.state = sensor_work_ready;
3646                 #endif
3647     }
3648
3649     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3650     return ret;
3651 }
3652
3653 static int sensor_remove(struct i2c_client *client)
3654 {
3655     struct sensor *sensor = to_sensor(client);
3656     struct soc_camera_device *icd = client->dev.platform_data;
3657
3658         #if CONFIG_SENSOR_Focus
3659         if (sensor->sensor_wq) {
3660                 destroy_workqueue(sensor->sensor_wq);
3661                 sensor->sensor_wq = NULL;
3662         }
3663         #endif
3664
3665     icd->ops = NULL;
3666     i2c_set_clientdata(client, NULL);
3667     client->driver = NULL;
3668     kfree(sensor);
3669         sensor = NULL;
3670     return 0;
3671 }
3672
3673 static const struct i2c_device_id sensor_id[] = {
3674         {SENSOR_NAME_STRING(), 0 },
3675         { }
3676 };
3677 MODULE_DEVICE_TABLE(i2c, sensor_id);
3678
3679 static struct i2c_driver sensor_i2c_driver = {
3680         .driver = {
3681                 .name = SENSOR_NAME_STRING(),
3682         },
3683         .probe          = sensor_probe,
3684         .remove         = sensor_remove,
3685         .id_table       = sensor_id,
3686 };
3687
3688 static int __init sensor_mod_init(void)
3689 {
3690     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3691     return i2c_add_driver(&sensor_i2c_driver);
3692 }
3693
3694 static void __exit sensor_mod_exit(void)
3695 {
3696     i2c_del_driver(&sensor_i2c_driver);
3697 }
3698
3699 device_initcall_sync(sensor_mod_init);
3700 module_exit(sensor_mod_exit);
3701
3702 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3703 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3704 MODULE_LICENSE("GPL");
3705
3706