PM / devfreq: rk3399_dmc: rename of_get_opp_table
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / ov7690.c
1 /*
2 o* Driver for MT9M001 CMOS Image Sensor from Micron
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 <plat/rk_camera.h>
23
24 static int debug;
25 module_param(debug, int, S_IRUGO|S_IWUSR);
26
27 #define dprintk(level, fmt, arg...) do {                        \
28         if (debug >= level)                                     \
29         printk(KERN_WARNING fmt , ## arg); } while (0)
30
31 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
32 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
33
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_OV7690
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV7690
48 #define SENSOR_ID 0x76
49 #define SENSOR_MIN_WIDTH    176
50 #define SENSOR_MIN_HEIGHT   144
51 #define SENSOR_MAX_WIDTH    640
52 #define SENSOR_MAX_HEIGHT   480
53 #define SENSOR_INIT_WIDTH       640                     /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT  480
55 #define SENSOR_INIT_WINSEQADR sensor_vga
56 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_YUYV8_2X8
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         0
64 #define CONFIG_SENSOR_DigitalZoom   0
65 #define CONFIG_SENSOR_Focus         0
66 #define CONFIG_SENSOR_Exposure      0
67 #define CONFIG_SENSOR_Flash         1
68 #define CONFIG_SENSOR_Mirror        0 
69 #define CONFIG_SENSOR_Flip          0
70
71 #define CONFIG_SENSOR_I2C_SPEED     250000       /* Hz */
72 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
73 #define CONFIG_SENSOR_I2C_NOSCHED   0
74 #define CONFIG_SENSOR_I2C_RDWRCHK   0
75
76 #define SENSOR_BUS_PARAM  (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING|\
77                           SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
78                           SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ)
79
80 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
81 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
82 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
83 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
84 #define COLOR_TEMPERATURE_OFFICE_DN     3500
85 #define COLOR_TEMPERATURE_OFFICE_UP     5000
86 #define COLOR_TEMPERATURE_HOME_DN       2500
87 #define COLOR_TEMPERATURE_HOME_UP       3500
88
89 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
90 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
91
92 struct reginfo
93 {
94     u8 reg;
95     u8 val;
96 };
97
98
99 //flash off in fixed time to prevent from too hot , zyc
100 struct  flash_timer{
101     struct soc_camera_device *icd;
102         struct hrtimer timer;
103 };
104 static enum hrtimer_restart flash_off_func(struct hrtimer *timer);
105
106 static struct  flash_timer flash_off_timer;
107 //for user defined if user want to customize the series , zyc
108 #ifdef CONFIG_OV7690_USER_DEFINED_SERIES
109 #include "ov7690_user_series.c"
110 #else
111
112 /* init 640X480 VGA */
113 static struct reginfo sensor_init_data[] =
114 {
115         {0x12, 0x80},
116         {0x49, 0x0c},
117         {0x0c, 0x06},
118         {0x48, 0x42},
119         {0x41, 0x43},
120         {0x4c, 0x73},
121         {0x81, 0xff},
122         {0x21, 0x44},
123         {0x16, 0x03},
124         {0x39, 0x80},
125         {0x1e, 0xb1},
126 // formate      
127         {0x12, 0x00},
128         {0x82, 0x03},
129         {0xd0, 0x48},
130         {0x80, 0x7F},
131         {0x3e, 0x30},
132         {0x22, 0x00},
133 // resolution   
134         {0x17, 0x69},
135         {0x18, 0xa4},
136         {0x19, 0x0c},
137         {0x1a, 0xf6},
138         
139         {0xc8, 0x02},
140         {0xc9, 0x80},
141         {0xca, 0x01},
142         {0xcb, 0xe0},
143         
144         {0xcc, 0x02},
145         {0xcd, 0x80},
146         {0xce, 0x01},
147         {0xcf, 0xe0},
148 // Lens Correction      
149         {0x85, 0x90},
150         {0x86, 0x00},
151         {0x87, 0x00},
152         {0x88, 0x10},
153         {0x89, 0x30},
154         {0x8a, 0x29},
155         {0x8b, 0x26},
156 // Color Matrix 
157         {0xbb, 0x80},
158         {0xbc, 0x62},
159         {0xbd, 0x1e},
160         {0xbe, 0x26},
161         {0xbf, 0x7b},
162         {0xc0, 0xac},
163         {0xc1, 0x1e},
164 // Edge + Denoise       
165         {0xb7, 0x05c},
166         {0xb8, 0x09},
167         {0xb9, 0x00},       //0x07//0x27
168         {0xba, 0x18},
169 //      UVAdjust
170         {0x5a, 0x4a},
171         {0x5b, 0x9f},
172         {0x5c, 0x48},
173         {0x5d, 0x32},
174 // AEC/AGC target       
175         {0x24, 0x78},
176         {0x25, 0x68},
177         {0x26, 0xb3},
178 // Gamma        
179         {0xa3, 0x0b},
180         {0xa4, 0x15},
181         {0xa5, 0x2a},
182         {0xa6, 0x51},
183         {0xa7, 0x63},
184         {0xa8, 0x74},
185         {0xa9, 0x83},
186         {0xaa, 0x91},
187         {0xab, 0x9e},
188         {0xac, 0xaa},
189         {0xad, 0xbe},
190         {0xae, 0xce},
191         {0xaf, 0xe5},
192         {0xb0, 0xf3},
193         {0xb1, 0xfb},
194         {0xb2, 0x06},
195 //simple        
196         {0x8e, 0x92},
197         {0x96, 0xff},
198         {0x97, 0x00},
199 // Advance      
200         {0x8c, 0x5d},
201         {0x8d, 0x11},
202         {0x8e, 0x12},
203         {0x8f, 0x11},
204         {0x90, 0x50},
205         {0x91, 0x22},
206         {0x92, 0xd1},
207         {0x93, 0xa7},
208         {0x94, 0x23},
209         {0x95, 0x3b},
210         {0x96, 0xff},
211         {0x97, 0x00},
212         {0x98, 0x4a},
213         {0x99, 0x46},
214         {0x9a, 0x3d},
215         {0x9b, 0x3a},
216         {0x9c, 0xf0},
217         {0x9d, 0xf0},
218         {0x9e, 0xf0},
219         {0x9f, 0xff},
220         {0xa0, 0x56},
221         {0xa1, 0x55},
222         {0xa2, 0x13},
223 // General Control      
224         {0x50, 0x4c},
225         {0x51, 0x3f},
226         {0x21, 0x57},
227         {0x20, 0x00},
228         
229         {0x14, 0x29},
230         {0x13, 0xf7},
231         {0x11, 0x01},
232         {0xd2, 0x06},
233         
234         {0x2a, 0x30},
235         {0x2b, 0x0b},
236         {0x2c, 0x00}    
237 };
238
239 /* 1280x720 */
240 static struct reginfo sensor_720p[]=
241 {
242         {0x00 ,0x00}
243
244 };
245
246 /* 1600X1200 UXGA */
247 static struct reginfo sensor_uxga[] =
248 {
249         {0x00, 0x00}
250 };
251
252 /* 1280X1024 SXGA */
253 static struct reginfo sensor_sxga[] =
254 {
255         {0x0, 0x0}
256 };
257 /* 1024X768 SXGA */
258 static struct reginfo sensor_xga[] =
259 {
260         {0x0, 0x0}
261 };
262 /* 800X600 SVGA*/
263 static struct reginfo sensor_svga[] =
264 {
265         {0x00, 0x00}
266 };
267
268 /* 640X480 VGA */
269 static struct reginfo sensor_vga[] =
270 {
271                 {0x16,0x03},
272                 {0x17,0x69},
273                 {0x18,0xa4},
274                 {0x19,0x0c},
275                 {0x1a,0xf6},
276                 {0x22,0x00},
277                 {0xc8,0x02},
278                 {0xc9,0x80},
279                 {0xca,0x01},
280                 {0xcb,0xe0},
281                 {0xcc,0x02},
282                 {0xcd,0x80},
283                 {0xce,0x01},
284                 {0xcf,0xe0},    
285         {0x00,0x00}
286 };
287
288 /* 352X288 CIF */
289 static struct reginfo sensor_cif[] =
290 {
291     {0x00,0x00}
292 };
293
294 /* 320*240 QVGA */
295 static  struct reginfo sensor_qvga[] =
296 {
297                 {0x16,0x03},
298                 {0x17,0x69},
299                 {0x18,0xa4},
300                 {0x19,0x06},
301                 {0x1a,0xf6},
302                 {0x22,0x10},
303                 {0xc8,0x02},
304                 {0xc9,0x80},
305                 {0xca,0x00},
306                 {0xcb,0xf0},
307                 {0xcc,0x01},
308                 {0xcd,0x40},
309                 {0xce,0x00},
310                 {0xcf,0xf0},    
311     {0x00,0x00}
312 };
313
314 /* 176X144 QCIF*/
315 static struct reginfo sensor_qcif[] =
316 {
317                 {0x16,0x40},
318                 {0x17,0x83},
319                 {0x18,0x96},
320                 {0x19,0x06},
321                 {0x1a,0xf6},
322                 {0x22,0x10},
323                 {0xc8,0x02},
324                 {0xc9,0x4b},
325                 {0xca,0x00},
326                 {0xcb,0xf0},
327                 {0xcc,0x00},
328                 {0xcd,0xb0},
329                 {0xce,0x00},
330                 {0xcf,0x90},
331     {0x00,0x00}
332 };
333 #endif
334 #if 0
335 /* 160X120 QQVGA*/
336 static struct reginfo ov2655_qqvga[] =
337 {
338     {0x0, 0x0},
339 };
340
341
342
343 static  struct reginfo ov2655_Sharpness_auto[] =
344 {
345     {0x3306, 0x00},
346 };
347
348 static  struct reginfo ov2655_Sharpness1[] =
349 {
350     {0x3306, 0x08},
351     {0x3371, 0x00},
352 };
353
354 static  struct reginfo ov2655_Sharpness2[][3] =
355 {
356     //Sharpness 2
357     {0x3306, 0x08},
358     {0x3371, 0x01},
359 };
360
361 static  struct reginfo ov2655_Sharpness3[] =
362 {
363     //default
364     {0x3306, 0x08},
365     {0x332d, 0x02},
366 };
367 static  struct reginfo ov2655_Sharpness4[]=
368 {
369     //Sharpness 4
370     {0x3306, 0x08},
371     {0x332d, 0x03},
372 };
373
374 static  struct reginfo ov2655_Sharpness5[] =
375 {
376     //Sharpness 5
377     {0x3306, 0x08},
378     {0x332d, 0x04},
379 };
380 #endif
381
382 static  struct reginfo sensor_ClrFmt_YUYV[]=
383 {
384     {0x00, 0x00}
385 };
386
387 static  struct reginfo sensor_ClrFmt_UYVY[]=
388 {
389     {0x00, 0x00}
390 };
391
392
393 #if CONFIG_SENSOR_WhiteBalance
394 static  struct reginfo sensor_WhiteB_Auto[]=
395 {
396                 {0x13, 0xf7},
397     {0x00, 0x00}
398 };
399 /* Cloudy Colour Temperature : 6500K - 8000K  */
400 static  struct reginfo sensor_WhiteB_Cloudy[]=
401 {
402                 {0x13, 0xf5},   
403                 {0x01, 0x40},
404                 {0x02, 0x5d},
405                 {0x03, 0x40},
406     {0x00, 0x00}
407 };
408 /* ClearDay Colour Temperature : 5000K - 6500K  */
409 static  struct reginfo sensor_WhiteB_ClearDay[]=
410 {
411                 {0x13, 0xf5},   
412                 {0x01, 0x44},
413                 {0x02, 0x55},
414                 {0x03, 0x40},
415     {0x00, 0x00}
416 };
417 /* Office Colour Temperature : 3500K - 5000K  */
418 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
419 {
420                 {0x13, 0xf5},   
421                 {0x01, 0x5b},
422                 {0x02, 0x4c},
423                 {0x03, 0x40},   
424     {0x00, 0x00}
425
426 };
427 /* Home Colour Temperature : 2500K - 3500K  */
428 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
429 {
430                 {0x13, 0xf5},   
431                 {0x01, 0x62},
432                 {0x02, 0x40},
433                 {0x03, 0x41},   
434     {0x00, 0x00}
435 };
436 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
437     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
438 };
439 #endif
440
441 #if CONFIG_SENSOR_Brightness
442 static  struct reginfo sensor_Brightness0[]=
443 {
444     // Brightness -2
445     {0x0000, 0x00}
446 };
447
448 static  struct reginfo sensor_Brightness1[]=
449 {
450     // Brightness -1
451
452     {0x0000, 0x00}
453 };
454
455 static  struct reginfo sensor_Brightness2[]=
456 {
457     //  Brightness 0
458
459     {0x0000, 0x00}
460 };
461
462 static  struct reginfo sensor_Brightness3[]=
463 {
464     // Brightness +1
465
466     {0x0000, 0x00}
467 };
468
469 static  struct reginfo sensor_Brightness4[]=
470 {
471     //  Brightness +2
472
473     {0x0000, 0x00}
474 };
475
476 static  struct reginfo sensor_Brightness5[]=
477 {
478     //  Brightness +3
479
480     {0x0000, 0x00}
481 };
482 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
483     sensor_Brightness4, sensor_Brightness5,NULL,
484 };
485
486 #endif
487
488 #if CONFIG_SENSOR_Effect
489 static  struct reginfo sensor_Effect_Normal[] =
490 {
491         {0xd2,0x06},
492     {0x00, 0x00}
493 };
494
495 static  struct reginfo sensor_Effect_WandB[] =
496 {
497         {0xd2,0x1e},
498         {0xda,0x80},
499         {0xdb,0x80},
500     {0x00, 0x00}
501 };
502
503 static  struct reginfo sensor_Effect_Sepia[] =
504 {
505         {0xd2,0x1e},
506         {0xda,0x40},
507         {0xdb,0xa0},
508     {0x00, 0x00}
509 };
510
511 static  struct reginfo sensor_Effect_Negative[] =
512 {
513     //Negative
514         {0xd2,0x46},
515     {0x00, 0x00}
516 };
517 static  struct reginfo sensor_Effect_Bluish[] =
518 {
519     // Bluish
520         {0xd2,0x1e},
521         {0xda,0xa0},
522         {0xdb,0x40},
523     {0x00, 0x00}
524 };
525
526 static  struct reginfo sensor_Effect_Green[] =
527 {
528     //  Greenish
529         {0xd2,0x1e},
530         {0xda,0x60},
531         {0xdb,0x60},
532     {0x00, 0x00}
533 };
534 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
535     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
536 };
537 #endif
538 #if CONFIG_SENSOR_Exposure
539 static  struct reginfo sensor_Exposure0[]=
540 {
541     {0x00, 0x00}
542 };
543
544 static  struct reginfo sensor_Exposure1[]=
545 {
546     {0x00, 0x00}
547 };
548
549 static  struct reginfo sensor_Exposure2[]=
550 {
551     {0x00, 0x00}
552 };
553
554 static  struct reginfo sensor_Exposure3[]=
555 {
556     {0x00, 0x00}
557 };
558
559 static  struct reginfo sensor_Exposure4[]=
560 {
561     {0x00, 0x00}
562 };
563
564 static  struct reginfo sensor_Exposure5[]=
565 {
566     {0x00, 0x00}
567 };
568
569 static  struct reginfo sensor_Exposure6[]=
570 {
571     {0x00, 0x00}
572 };
573
574 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
575     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
576 };
577 #endif
578 #if CONFIG_SENSOR_Saturation
579 static  struct reginfo sensor_Saturation0[]=
580 {
581     {0x00, 0x00}
582 };
583
584 static  struct reginfo sensor_Saturation1[]=
585 {
586     {0x00, 0x00}
587 };
588
589 static  struct reginfo sensor_Saturation2[]=
590 {
591     {0x00, 0x00}
592 };
593 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
594
595 #endif
596 #if CONFIG_SENSOR_Contrast
597 static  struct reginfo sensor_Contrast0[]=
598 {
599     {0x00, 0x00}
600 };
601
602 static  struct reginfo sensor_Contrast1[]=
603 {
604     {0x00, 0x00}
605 };
606
607 static  struct reginfo sensor_Contrast2[]=
608 {
609     {0x00, 0x00}
610 };
611
612 static  struct reginfo sensor_Contrast3[]=
613 {
614     {0x00, 0x00}
615 };
616
617 static  struct reginfo sensor_Contrast4[]=
618 {
619     {0x00, 0x00}
620 };
621
622
623 static  struct reginfo sensor_Contrast5[]=
624 {
625     {0x00, 0x00}
626 };
627
628 static  struct reginfo sensor_Contrast6[]=
629 {
630     {0x00, 0x00}
631 };
632 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
633     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
634 };
635
636 #endif
637 #if CONFIG_SENSOR_Mirror
638 static  struct reginfo sensor_MirrorOn[]=
639 {
640     {0x00, 0x00}
641 };
642
643 static  struct reginfo sensor_MirrorOff[]=
644 {
645     {0x00, 0x00}
646 };
647 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
648 #endif
649 #if CONFIG_SENSOR_Flip
650 static  struct reginfo sensor_FlipOn[]=
651 {
652     {0x00, 0x00}
653 };
654
655 static  struct reginfo sensor_FlipOff[]=
656 {
657     {0x00, 0x00}
658 };
659 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
660
661 #endif
662 #if CONFIG_SENSOR_Scene
663 static  struct reginfo sensor_SceneAuto[] =
664 {
665 {0x00, 0x00}
666 };
667
668 static  struct reginfo sensor_SceneNight[] =
669 {
670 {0x00, 0x00}
671 };
672 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
673
674 #endif
675 #if CONFIG_SENSOR_DigitalZoom
676 static struct reginfo sensor_Zoom0[] =
677 {
678     {0x0, 0x0},
679 };
680
681 static struct reginfo sensor_Zoom1[] =
682 {
683      {0x0, 0x0},
684 };
685
686 static struct reginfo sensor_Zoom2[] =
687 {
688     {0x0, 0x0},
689 };
690
691
692 static struct reginfo sensor_Zoom3[] =
693 {
694     {0x0, 0x0},
695 };
696 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
697 #endif
698 static const struct v4l2_querymenu sensor_menus[] =
699 {
700         #if CONFIG_SENSOR_WhiteBalance
701     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
702     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
703     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
704     #endif
705
706         #if CONFIG_SENSOR_Effect
707     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
708     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
709     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
710     #endif
711
712         #if CONFIG_SENSOR_Scene
713     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
714     #endif
715
716         #if CONFIG_SENSOR_Flash
717     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
718     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
719     #endif
720 };
721
722 static  struct v4l2_queryctrl sensor_controls[] =
723 {
724         #if CONFIG_SENSOR_WhiteBalance
725     {
726         .id             = V4L2_CID_DO_WHITE_BALANCE,
727         .type           = V4L2_CTRL_TYPE_MENU,
728         .name           = "White Balance Control",
729         .minimum        = 0,
730         .maximum        = 4,
731         .step           = 1,
732         .default_value = 0,
733     },
734     #endif
735
736         #if CONFIG_SENSOR_Brightness
737         {
738         .id             = V4L2_CID_BRIGHTNESS,
739         .type           = V4L2_CTRL_TYPE_INTEGER,
740         .name           = "Brightness Control",
741         .minimum        = -3,
742         .maximum        = 2,
743         .step           = 1,
744         .default_value = 0,
745     },
746     #endif
747
748         #if CONFIG_SENSOR_Effect
749         {
750         .id             = V4L2_CID_EFFECT,
751         .type           = V4L2_CTRL_TYPE_MENU,
752         .name           = "Effect Control",
753         .minimum        = 0,
754         .maximum        = 5,
755         .step           = 1,
756         .default_value = 0,
757     },
758         #endif
759
760         #if CONFIG_SENSOR_Exposure
761         {
762         .id             = V4L2_CID_EXPOSURE,
763         .type           = V4L2_CTRL_TYPE_INTEGER,
764         .name           = "Exposure Control",
765         .minimum        = 0,
766         .maximum        = 6,
767         .step           = 1,
768         .default_value = 0,
769     },
770         #endif
771
772         #if CONFIG_SENSOR_Saturation
773         {
774         .id             = V4L2_CID_SATURATION,
775         .type           = V4L2_CTRL_TYPE_INTEGER,
776         .name           = "Saturation Control",
777         .minimum        = 0,
778         .maximum        = 2,
779         .step           = 1,
780         .default_value = 0,
781     },
782     #endif
783
784         #if CONFIG_SENSOR_Contrast
785         {
786         .id             = V4L2_CID_CONTRAST,
787         .type           = V4L2_CTRL_TYPE_INTEGER,
788         .name           = "Contrast Control",
789         .minimum        = -3,
790         .maximum        = 3,
791         .step           = 1,
792         .default_value = 0,
793     },
794         #endif
795
796         #if CONFIG_SENSOR_Mirror
797         {
798         .id             = V4L2_CID_HFLIP,
799         .type           = V4L2_CTRL_TYPE_BOOLEAN,
800         .name           = "Mirror Control",
801         .minimum        = 0,
802         .maximum        = 1,
803         .step           = 1,
804         .default_value = 0,
805     },
806     #endif
807
808         #if CONFIG_SENSOR_Flip
809         {
810         .id             = V4L2_CID_VFLIP,
811         .type           = V4L2_CTRL_TYPE_BOOLEAN,
812         .name           = "Flip Control",
813         .minimum        = 0,
814         .maximum        = 1,
815         .step           = 1,
816         .default_value = 0,
817     },
818     #endif
819
820         #if CONFIG_SENSOR_Scene
821     {
822         .id             = V4L2_CID_SCENE,
823         .type           = V4L2_CTRL_TYPE_MENU,
824         .name           = "Scene Control",
825         .minimum        = 0,
826         .maximum        = 1,
827         .step           = 1,
828         .default_value = 0,
829     },
830     #endif
831
832         #if CONFIG_SENSOR_DigitalZoom
833     {
834         .id             = V4L2_CID_ZOOM_RELATIVE,
835         .type           = V4L2_CTRL_TYPE_INTEGER,
836         .name           = "DigitalZoom Control",
837         .minimum        = -1,
838         .maximum        = 1,
839         .step           = 1,
840         .default_value = 0,
841     }, {
842         .id             = V4L2_CID_ZOOM_ABSOLUTE,
843         .type           = V4L2_CTRL_TYPE_INTEGER,
844         .name           = "DigitalZoom Control",
845         .minimum        = 0,
846         .maximum        = 3,
847         .step           = 1,
848         .default_value = 0,
849     },
850     #endif
851
852         #if CONFIG_SENSOR_Focus
853         {
854         .id             = V4L2_CID_FOCUS_RELATIVE,
855         .type           = V4L2_CTRL_TYPE_INTEGER,
856         .name           = "Focus Control",
857         .minimum        = -1,
858         .maximum        = 1,
859         .step           = 1,
860         .default_value = 0,
861     }, {
862         .id             = V4L2_CID_FOCUS_ABSOLUTE,
863         .type           = V4L2_CTRL_TYPE_INTEGER,
864         .name           = "Focus Control",
865         .minimum        = 0,
866         .maximum        = 255,
867         .step           = 1,
868         .default_value = 125,
869     },
870     #endif
871
872         #if CONFIG_SENSOR_Flash
873         {
874         .id             = V4L2_CID_FLASH,
875         .type           = V4L2_CTRL_TYPE_MENU,
876         .name           = "Flash Control",
877         .minimum        = 0,
878         .maximum        = 3,
879         .step           = 1,
880         .default_value = 0,
881     },
882         #endif
883 };
884
885 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
886 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
887 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
888 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
889 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
890 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
891 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
892 static int sensor_resume(struct soc_camera_device *icd);
893 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
894 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
895 #if CONFIG_SENSOR_Effect
896 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
897 #endif
898 #if CONFIG_SENSOR_WhiteBalance
899 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
900 #endif
901 static int sensor_deactivate(struct i2c_client *client);
902
903 static struct soc_camera_ops sensor_ops =
904 {
905     .suspend                     = sensor_suspend,
906     .resume                       = sensor_resume,
907     .set_bus_param              = sensor_set_bus_param,
908     .query_bus_param    = sensor_query_bus_param,
909     .controls           = sensor_controls,
910     .menus                         = sensor_menus,
911     .num_controls               = ARRAY_SIZE(sensor_controls),
912     .num_menus          = ARRAY_SIZE(sensor_menus),
913 };
914
915 /* only one fixed colorspace per pixelcode */
916 struct sensor_datafmt {
917         enum v4l2_mbus_pixelcode code;
918         enum v4l2_colorspace colorspace;
919 };
920
921 /* Find a data format by a pixel code in an array */
922 static const struct sensor_datafmt *sensor_find_datafmt(
923         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
924         int n)
925 {
926         int i;
927         for (i = 0; i < n; i++)
928                 if (fmt[i].code == code)
929                         return fmt + i;
930
931         return NULL;
932 }
933
934 static const struct sensor_datafmt sensor_colour_fmts[] = {
935     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
936     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
937 };
938
939 typedef struct sensor_info_priv_s
940 {
941     int whiteBalance;
942     int brightness;
943     int contrast;
944     int saturation;
945     int effect;
946     int scene;
947     int digitalzoom;
948     int focus;
949     int flash;
950     int exposure;
951         bool snap2preview;
952         bool video2preview;
953     unsigned char mirror;                                        /* HFLIP */
954     unsigned char flip;                                          /* VFLIP */
955     unsigned int winseqe_cur_addr;
956     struct sensor_datafmt fmt;
957     unsigned int funmodule_state;
958 } sensor_info_priv_t;
959
960 struct sensor
961 {
962     struct v4l2_subdev subdev;
963     struct i2c_client *client;
964     sensor_info_priv_t info_priv;
965     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
966 #if CONFIG_SENSOR_I2C_NOSCHED
967         atomic_t tasklock_cnt;
968 #endif
969         struct rk29camera_platform_data *sensor_io_request;
970     struct rk29camera_gpio_res *sensor_gpio_res;
971 };
972
973
974 static struct sensor* to_sensor(const struct i2c_client *client)
975 {
976     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
977 }
978
979 static int sensor_task_lock(struct i2c_client *client, int lock)
980 {
981 #if CONFIG_SENSOR_I2C_NOSCHED
982         int cnt = 3;
983     struct sensor *sensor = to_sensor(client);
984
985         if (lock) {
986                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
987                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
988                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
989                                 msleep(35);
990                                 cnt--;
991                         }
992                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
993                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
994                                 goto sensor_task_lock_err;
995                         }
996                         preempt_disable();
997                 }
998
999                 atomic_add(1, &sensor->tasklock_cnt);
1000         } else {
1001                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1002                         atomic_sub(1, &sensor->tasklock_cnt);
1003
1004                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1005                                 preempt_enable();
1006                 }
1007         }
1008         return 0;
1009 sensor_task_lock_err:
1010         return -1;  
1011 #else
1012     return 0;
1013 #endif
1014
1015 }
1016
1017 /* sensor register write */
1018 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1019 {
1020     int err,cnt;
1021     u8 buf[2];
1022     struct i2c_msg msg[1];
1023
1024     buf[0] = reg & 0xFF;
1025     buf[1] = val;
1026
1027     msg->addr = client->addr;
1028     msg->flags = client->flags;
1029     msg->buf = buf;
1030     msg->len = sizeof(buf);
1031     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
1032     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1033
1034     cnt = 3;
1035     err = -EAGAIN;
1036
1037     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1038         err = i2c_transfer(client->adapter, msg, 1);
1039                                 udelay(50);
1040         if (err >= 0) {
1041             return 0;
1042         } else {
1043                 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1044                 udelay(10);
1045         }
1046     }
1047
1048     return err;
1049 }
1050
1051 /* sensor register read */
1052 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1053 {
1054     int err,cnt;
1055     u8 buf[1];
1056     struct i2c_msg msg[2];
1057    
1058     buf[0] = reg;
1059
1060     msg[0].addr = client->addr;
1061     msg[0].flags = client->flags;
1062     msg[0].buf = buf;
1063     msg[0].len = sizeof(buf);
1064     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1065     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1066
1067     msg[1].addr = client->addr;
1068     msg[1].flags = client->flags|I2C_M_RD;
1069     msg[1].buf = buf;
1070     msg[1].len = 1;
1071     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1072     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1073
1074     cnt = 1;
1075     err = -EAGAIN;
1076     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1077         err = i2c_transfer(client->adapter, msg, 2);
1078
1079         if (err >= 0) {
1080             *val = buf[0];
1081             return 0;
1082         } else {
1083                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1084             udelay(10);
1085         }
1086     }
1087
1088     return err;
1089 }
1090
1091 /* write a array of registers  */
1092 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1093 {
1094     int err = 0, cnt;
1095     int i = 0;
1096 #if CONFIG_SENSOR_I2C_RDWRCHK    
1097         char valchk;
1098 #endif
1099
1100         cnt = 0;
1101         if (sensor_task_lock(client, 1) < 0)
1102                 goto sensor_write_array_end;
1103
1104     while (regarray[i].reg != 0)
1105     {
1106         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1107         if (err < 0)
1108         {
1109             if (cnt-- > 0) {
1110                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1111                                 i = 0;
1112                                 continue;
1113             } else {
1114                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1115                 err = -EPERM;
1116                                 goto sensor_write_array_end;
1117             }
1118         } else {
1119         #if CONFIG_SENSOR_I2C_RDWRCHK
1120                         sensor_read(client, regarray[i].reg, &valchk);
1121                         if (valchk != regarray[i].val)
1122                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1123                 #endif
1124         }
1125         i++;
1126     }
1127
1128 sensor_write_array_end:
1129         sensor_task_lock(client,0);
1130         return err;
1131 }
1132 #if CONFIG_SENSOR_I2C_RDWRCHK
1133 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1134 {
1135     int cnt;
1136     int i = 0;
1137         char valchk;
1138
1139         cnt = 0;
1140         valchk = 0;
1141     while (regarray[i].reg != 0)
1142     {
1143                 sensor_read(client, regarray[i].reg, &valchk);
1144                 if (valchk != regarray[i].val)
1145                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1146
1147         i++;
1148     }
1149     return 0;
1150 }
1151 #endif
1152 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1153 {
1154         struct soc_camera_link *icl = to_soc_camera_link(icd);
1155         int ret = 0;
1156
1157     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1158         switch (cmd)
1159         {
1160                 case Sensor_PowerDown:
1161                 {
1162                         if (icl->powerdown) {
1163                                 if (on == 0)
1164                                         mdelay(1);
1165                                 ret = icl->powerdown(icd->pdev, on);
1166                                 if (ret == RK29_CAM_IO_SUCCESS) {
1167                                         if (on == 0) {
1168                                                 mdelay(20);
1169                                                 if (icl->reset)
1170                                                         icl->reset(icd->pdev);
1171                                         }
1172                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1173                                         ret = -ENODEV;
1174                                         goto sensor_power_end;
1175                                 }
1176                         }
1177                         break;
1178                 }
1179                 case Sensor_Flash:
1180                 {
1181                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1182                 struct sensor *sensor = to_sensor(client);
1183
1184                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1185                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1186                 if(on){
1187                     //flash off after 2 secs
1188                         hrtimer_cancel(&(flash_off_timer.timer));
1189                         hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
1190                     }
1191                         }
1192             break;
1193                 }
1194                 default:
1195                 {
1196                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1197                         break;
1198                 }
1199         }
1200 sensor_power_end:
1201         return ret;
1202 }
1203
1204 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
1205         struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
1206     sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
1207         SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
1208     return 0;
1209     
1210 }
1211
1212 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1213 {
1214     struct i2c_client *client = v4l2_get_subdevdata(sd);
1215     struct soc_camera_device *icd = client->dev.platform_data;
1216     struct sensor *sensor = to_sensor(client);
1217         const struct v4l2_queryctrl *qctrl;
1218     const struct sensor_datafmt *fmt;
1219     int ret;
1220
1221     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1222
1223         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1224                 ret = -ENODEV;
1225                 goto sensor_INIT_ERR;
1226         }
1227
1228     /* soft reset */
1229         if (sensor_task_lock(client,1)<0)
1230                 goto sensor_INIT_ERR;
1231     ret = sensor_write(client, 0x12, 0x80);
1232     if (ret != 0)
1233     {
1234         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1235         ret = -ENODEV;
1236                 goto sensor_INIT_ERR;
1237     }
1238     mdelay(5);  //delay 5 microseconds
1239
1240     ret = sensor_write_array(client, sensor_init_data);
1241     if (ret != 0)
1242     {
1243         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1244         goto sensor_INIT_ERR;
1245     }
1246         sensor_task_lock(client,0);
1247     
1248     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
1249     fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1250     if (!fmt) {
1251         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1252         ret = -EINVAL;
1253         goto sensor_INIT_ERR;
1254     }
1255         sensor->info_priv.fmt = *fmt;
1256
1257     /* sensor sensor information for initialization  */
1258         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1259         if (qctrl)
1260         sensor->info_priv.whiteBalance = qctrl->default_value;
1261         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1262         if (qctrl)
1263         sensor->info_priv.brightness = qctrl->default_value;
1264         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1265         if (qctrl)
1266         sensor->info_priv.effect = qctrl->default_value;
1267         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1268         if (qctrl)
1269         sensor->info_priv.exposure = qctrl->default_value;
1270
1271         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1272         if (qctrl)
1273         sensor->info_priv.saturation = qctrl->default_value;
1274         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1275         if (qctrl)
1276         sensor->info_priv.contrast = qctrl->default_value;
1277         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1278         if (qctrl)
1279         sensor->info_priv.mirror = qctrl->default_value;
1280         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1281         if (qctrl)
1282         sensor->info_priv.flip = qctrl->default_value;
1283         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1284         if (qctrl)
1285         sensor->info_priv.scene = qctrl->default_value;
1286         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1287         if (qctrl)
1288         sensor->info_priv.digitalzoom = qctrl->default_value;
1289
1290     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
1291         #if CONFIG_SENSOR_Focus
1292     sensor_set_focus();
1293     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1294         if (qctrl)
1295         sensor->info_priv.focus = qctrl->default_value;
1296         #endif
1297
1298         #if CONFIG_SENSOR_Flash 
1299         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1300         if (qctrl)
1301         sensor->info_priv.flash = qctrl->default_value;
1302     flash_off_timer.icd = icd;
1303         flash_off_timer.timer.function = flash_off_func;
1304     #endif
1305
1306     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);
1307
1308     return 0;
1309 sensor_INIT_ERR:
1310         sensor_task_lock(client,0);
1311         sensor_deactivate(client);
1312     return ret;
1313 }
1314
1315 static int sensor_deactivate(struct i2c_client *client)
1316 {
1317         struct soc_camera_device *icd = client->dev.platform_data;
1318         //u8 reg_val;
1319
1320         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1321
1322         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1323         sensor_ioctrl(icd, Sensor_PowerDown, 1);
1324         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1325         icd->user_width = SENSOR_INIT_WIDTH;
1326     icd->user_height = SENSOR_INIT_HEIGHT;
1327         msleep(100);
1328         return 0;
1329 }
1330 static  struct reginfo sensor_power_down_sequence[]=
1331 {
1332     {0x00,0x00}
1333 };
1334 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1335 {
1336     int ret;
1337     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1338
1339     if (pm_msg.event == PM_EVENT_SUSPEND) {
1340         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1341         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1342         if (ret != 0) {
1343             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1344             return ret;
1345         } else {
1346             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1347             if (ret < 0) {
1348                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1349                 return -EINVAL;
1350             }
1351         }
1352     } else {
1353         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1354         return -EINVAL;
1355     }
1356     return 0;
1357 }
1358
1359 static int sensor_resume(struct soc_camera_device *icd)
1360 {
1361         int ret;
1362
1363     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1364     if (ret < 0) {
1365                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1366         return -EINVAL;
1367     }
1368
1369         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1370
1371     return 0;
1372
1373 }
1374
1375 static int sensor_set_bus_param(struct soc_camera_device *icd,
1376                                 unsigned long flags)
1377 {
1378
1379     return 0;
1380 }
1381
1382 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1383 {
1384     struct soc_camera_link *icl = to_soc_camera_link(icd);
1385     unsigned long flags = SENSOR_BUS_PARAM;
1386
1387     return soc_camera_apply_sensor_flags(icl, flags);
1388 }
1389
1390 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1391 {
1392     struct i2c_client *client = v4l2_get_subdevdata(sd);
1393     struct soc_camera_device *icd = client->dev.platform_data;
1394     struct sensor *sensor = to_sensor(client);
1395
1396     mf->width   = icd->user_width;
1397         mf->height      = icd->user_height;
1398         mf->code        = sensor->info_priv.fmt.code;
1399         mf->colorspace  = sensor->info_priv.fmt.colorspace;
1400         mf->field       = V4L2_FIELD_NONE;
1401
1402     return 0;
1403 }
1404 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1405 {
1406     bool ret = false;
1407
1408         if ((mf->width == 1024) && (mf->height == 768)) {
1409                 ret = true;
1410         } else if ((mf->width == 1280) && (mf->height == 1024)) {
1411                 ret = true;
1412         } else if ((mf->width == 1600) && (mf->height == 1200)) {
1413                 ret = true;
1414         } else if ((mf->width == 2048) && (mf->height == 1536)) {
1415                 ret = true;
1416         } else if ((mf->width == 2592) && (mf->height == 1944)) {
1417                 ret = true;
1418         }
1419
1420         if (ret == true)
1421                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1422         return ret;
1423 }
1424
1425 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1426 {
1427     bool ret = false;
1428
1429         if ((mf->width == 1280) && (mf->height == 720)) {
1430                 ret = true;
1431         } else if ((mf->width == 1920) && (mf->height == 1080)) {
1432                 ret = true;
1433         }
1434
1435         if (ret == true)
1436                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1437         return ret;
1438 }
1439 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1440 {
1441     struct i2c_client *client = v4l2_get_subdevdata(sd);
1442     const struct sensor_datafmt *fmt;
1443     struct sensor *sensor = to_sensor(client);
1444         const struct v4l2_queryctrl *qctrl;
1445         struct soc_camera_device *icd = client->dev.platform_data;
1446     struct reginfo *winseqe_set_addr=NULL;
1447         char readval;
1448     int ret=0, set_w,set_h;
1449
1450         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1451                                    ARRAY_SIZE(sensor_colour_fmts));
1452         if (!fmt) {
1453         ret = -EINVAL;
1454         goto sensor_s_fmt_end;
1455     }
1456
1457         if (sensor->info_priv.fmt.code != mf->code) {
1458                 switch (mf->code)
1459                 {
1460                         case V4L2_MBUS_FMT_YUYV8_2X8:
1461                         {
1462                                 //winseqe_set_addr = sensor_ClrFmt_YUYV;
1463                                 sensor_read(client, 0x3a, &readval);
1464                                 sensor_write(client,0x3a, readval&0xf7);
1465                                 sensor_read(client,0x3d,&readval);
1466                                 sensor_write(client,0x3d,readval&0xfe);
1467                                 break;
1468                         }
1469                         case V4L2_MBUS_FMT_UYVY8_2X8:
1470                         {
1471                                 //winseqe_set_addr = sensor_ClrFmt_UYVY;
1472                                 sensor_read(client, 0x3a, &readval);
1473                                 sensor_write(client,0x3a, readval|0x08);
1474                                 sensor_read(client,0x3d,&readval);
1475                                 sensor_write(client,0x3d,readval&0xfe);
1476                                 break;
1477                         }
1478                         default:
1479                                 break;
1480                 }
1481                 if (winseqe_set_addr != NULL) {
1482             sensor_write_array(client, winseqe_set_addr);
1483                         sensor->info_priv.fmt.code = mf->code;
1484             sensor->info_priv.fmt.colorspace= mf->colorspace;            
1485                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1486                 } else {
1487                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1488                 }
1489         }
1490
1491     set_w = mf->width;
1492     set_h = mf->height;
1493
1494         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1495         {
1496                 winseqe_set_addr = sensor_qcif;
1497         set_w = 176;
1498         set_h = 144;
1499         }
1500         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1501     {
1502         winseqe_set_addr = sensor_qvga;
1503         set_w = 320;
1504         set_h = 240;
1505     }
1506     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1507     {
1508         winseqe_set_addr = sensor_cif;
1509         set_w = 352;
1510         set_h = 288;
1511     }
1512     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1513     {
1514         winseqe_set_addr = sensor_vga;
1515         set_w = 640;
1516         set_h = 480;
1517     }
1518     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1519     {
1520         winseqe_set_addr = sensor_svga;
1521         set_w = 800;
1522         set_h = 600;
1523     }
1524     else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
1525     {
1526         winseqe_set_addr = sensor_720p;
1527         set_w = 1280;
1528         set_h = 720;
1529     }
1530         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
1531     {
1532         winseqe_set_addr = sensor_xga;
1533         set_w = 1024;
1534         set_h = 768;
1535     }
1536     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1537     {
1538                 winseqe_set_addr = sensor_vga; //sensor_sxga;
1539         set_w = 1280;
1540         set_h = 1024;
1541     }
1542     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1543     {
1544         winseqe_set_addr = sensor_uxga;
1545         set_w = 1600;
1546         set_h = 1200;
1547     }
1548     else
1549     {
1550         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
1551         set_w = SENSOR_INIT_WIDTH;
1552         set_h = SENSOR_INIT_HEIGHT;
1553                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1554     }
1555
1556     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
1557         #if CONFIG_SENSOR_Flash
1558         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
1559             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1560                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1561                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1562             }           
1563         } else {                                        /* ddl@rock-chips.com : Video */
1564             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1565                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1566                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1567             }
1568         }
1569         #endif
1570         ret |= sensor_write_array(client, winseqe_set_addr);
1571         if (ret != 0) {
1572             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1573             #if CONFIG_SENSOR_Flash
1574             if (sensor_fmt_capturechk(sd,mf) == true) {
1575                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1576                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1577                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1578                 }
1579             }
1580             #endif
1581             goto sensor_s_fmt_end;
1582         }
1583
1584         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
1585
1586                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
1587         #if CONFIG_SENSOR_Effect
1588                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1589                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1590         #endif
1591         #if CONFIG_SENSOR_WhiteBalance
1592                         if (sensor->info_priv.whiteBalance != 0) {
1593                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1594                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1595                         }
1596         #endif
1597                         sensor->info_priv.snap2preview = true;
1598                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
1599                 #if CONFIG_SENSOR_Effect
1600                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1601                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1602         #endif
1603         #if CONFIG_SENSOR_WhiteBalance
1604                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1605                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1606         #endif
1607                         sensor->info_priv.video2preview = true;
1608                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1609                 #if CONFIG_SENSOR_Effect
1610                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1611                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1612         #endif
1613         #if CONFIG_SENSOR_WhiteBalance
1614                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1615                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1616         #endif
1617                         sensor->info_priv.video2preview = false;
1618                         sensor->info_priv.snap2preview = false;
1619                 }
1620
1621         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1622     }
1623     else
1624     {
1625         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1626     }
1627
1628         mf->width = set_w;
1629     mf->height = set_h;
1630
1631 sensor_s_fmt_end:
1632     return ret;
1633 }
1634
1635 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1636 {
1637     struct i2c_client *client = v4l2_get_subdevdata(sd);
1638     struct sensor *sensor = to_sensor(client);
1639     const struct sensor_datafmt *fmt;
1640     int ret = 0;
1641    
1642         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1643                                    ARRAY_SIZE(sensor_colour_fmts));
1644         if (fmt == NULL) {
1645                 fmt = &sensor->info_priv.fmt;
1646         mf->code = fmt->code;
1647         } 
1648
1649     if (mf->height > SENSOR_MAX_HEIGHT)
1650         mf->height = SENSOR_MAX_HEIGHT;
1651     else if (mf->height < SENSOR_MIN_HEIGHT)
1652         mf->height = SENSOR_MIN_HEIGHT;
1653
1654     if (mf->width > SENSOR_MAX_WIDTH)
1655         mf->width = SENSOR_MAX_WIDTH;
1656     else if (mf->width < SENSOR_MIN_WIDTH)
1657         mf->width = SENSOR_MIN_WIDTH;
1658
1659     mf->colorspace = fmt->colorspace;
1660     
1661     return ret;
1662 }
1663
1664  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1665 {
1666     struct i2c_client *client = v4l2_get_subdevdata(sd);
1667
1668     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1669         return -EINVAL;
1670
1671     if (id->match.addr != client->addr)
1672         return -ENODEV;
1673
1674     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
1675     id->revision = 0;
1676
1677     return 0;
1678 }
1679 #if CONFIG_SENSOR_Brightness
1680 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1681 {
1682     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1683
1684     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1685     {
1686         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1687         {
1688             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1689             {
1690                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1691                 return -EINVAL;
1692             }
1693             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1694             return 0;
1695         }
1696     }
1697         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1698     return -EINVAL;
1699 }
1700 #endif
1701 #if CONFIG_SENSOR_Effect
1702 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1703 {
1704     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1705
1706     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1707     {
1708         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1709         {
1710             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1711             {
1712                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1713                 return -EINVAL;
1714             }
1715             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1716             return 0;
1717         }
1718     }
1719         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1720     return -EINVAL;
1721 }
1722 #endif
1723 #if CONFIG_SENSOR_Exposure
1724 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1725 {
1726     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1727
1728     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1729     {
1730         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1731         {
1732             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1733             {
1734                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1735                 return -EINVAL;
1736             }
1737             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1738             return 0;
1739         }
1740     }
1741         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1742     return -EINVAL;
1743 }
1744 #endif
1745 #if CONFIG_SENSOR_Saturation
1746 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1747 {
1748     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1749
1750     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1751     {
1752         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1753         {
1754             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1755             {
1756                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1757                 return -EINVAL;
1758             }
1759             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1760             return 0;
1761         }
1762     }
1763     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1764     return -EINVAL;
1765 }
1766 #endif
1767 #if CONFIG_SENSOR_Contrast
1768 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1769 {
1770     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1771
1772     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1773     {
1774         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1775         {
1776             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1777             {
1778                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1779                 return -EINVAL;
1780             }
1781             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1782             return 0;
1783         }
1784     }
1785     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1786     return -EINVAL;
1787 }
1788 #endif
1789 #if CONFIG_SENSOR_Mirror
1790 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1791 {
1792     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1793
1794     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1795     {
1796         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1797         {
1798             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1799             {
1800                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1801                 return -EINVAL;
1802             }
1803             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1804             return 0;
1805         }
1806     }
1807     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1808     return -EINVAL;
1809 }
1810 #endif
1811 #if CONFIG_SENSOR_Flip
1812 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1813 {
1814     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1815
1816     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1817     {
1818         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1819         {
1820             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1821             {
1822                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1823                 return -EINVAL;
1824             }
1825             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1826             return 0;
1827         }
1828     }
1829     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1830     return -EINVAL;
1831 }
1832 #endif
1833 #if CONFIG_SENSOR_Scene
1834 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1835 {
1836     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1837
1838     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1839     {
1840         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1841         {
1842             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1843             {
1844                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1845                 return -EINVAL;
1846             }
1847             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1848             return 0;
1849         }
1850     }
1851     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1852     return -EINVAL;
1853 }
1854 #endif
1855 #if CONFIG_SENSOR_WhiteBalance
1856 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1857 {
1858     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1859
1860     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1861     {
1862         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
1863         {
1864             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
1865             {
1866                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1867                 return -EINVAL;
1868             }
1869             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1870             return 0;
1871         }
1872     }
1873         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1874     return -EINVAL;
1875 }
1876 #endif
1877 #if CONFIG_SENSOR_DigitalZoom
1878 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
1879 {
1880     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1881     struct sensor *sensor = to_sensor(client);
1882         const struct v4l2_queryctrl *qctrl_info;
1883     int digitalzoom_cur, digitalzoom_total;
1884
1885         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1886         if (qctrl_info)
1887                 return -EINVAL;
1888
1889     digitalzoom_cur = sensor->info_priv.digitalzoom;
1890     digitalzoom_total = qctrl_info->maximum;
1891
1892     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
1893     {
1894         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1895         return -EINVAL;
1896     }
1897
1898     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
1899     {
1900         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1901         return -EINVAL;
1902     }
1903
1904     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
1905     {
1906         value = digitalzoom_total - digitalzoom_cur;
1907     }
1908
1909     if ((value < 0) && ((digitalzoom_cur + value) < 0))
1910     {
1911         value = 0 - digitalzoom_cur;
1912     }
1913
1914     digitalzoom_cur += value;
1915
1916     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
1917     {
1918         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
1919         {
1920             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1921             return -EINVAL;
1922         }
1923         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1924         return 0;
1925     }
1926
1927     return -EINVAL;
1928 }
1929 #endif
1930 #if CONFIG_SENSOR_Flash
1931 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1932 {    
1933     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
1934         if (value == 3) {       /* ddl@rock-chips.com: torch */
1935             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
1936         } else {
1937             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1938         }
1939         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1940         return 0;
1941     }
1942     
1943         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1944     return -EINVAL;
1945 }
1946 #endif
1947
1948 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1949 {
1950     struct i2c_client *client = v4l2_get_subdevdata(sd);
1951     struct sensor *sensor = to_sensor(client);
1952     const struct v4l2_queryctrl *qctrl;
1953
1954     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
1955
1956     if (!qctrl)
1957     {
1958         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
1959         return -EINVAL;
1960     }
1961
1962     switch (ctrl->id)
1963     {
1964         case V4L2_CID_BRIGHTNESS:
1965             {
1966                 ctrl->value = sensor->info_priv.brightness;
1967                 break;
1968             }
1969         case V4L2_CID_SATURATION:
1970             {
1971                 ctrl->value = sensor->info_priv.saturation;
1972                 break;
1973             }
1974         case V4L2_CID_CONTRAST:
1975             {
1976                 ctrl->value = sensor->info_priv.contrast;
1977                 break;
1978             }
1979         case V4L2_CID_DO_WHITE_BALANCE:
1980             {
1981                 ctrl->value = sensor->info_priv.whiteBalance;
1982                 break;
1983             }
1984         case V4L2_CID_EXPOSURE:
1985             {
1986                 ctrl->value = sensor->info_priv.exposure;
1987                 break;
1988             }
1989         case V4L2_CID_HFLIP:
1990             {
1991                 ctrl->value = sensor->info_priv.mirror;
1992                 break;
1993             }
1994         case V4L2_CID_VFLIP:
1995             {
1996                 ctrl->value = sensor->info_priv.flip;
1997                 break;
1998             }
1999         default :
2000                 break;
2001     }
2002     return 0;
2003 }
2004
2005
2006
2007 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2008 {
2009     struct i2c_client *client = v4l2_get_subdevdata(sd);
2010     struct sensor *sensor = to_sensor(client);
2011     struct soc_camera_device *icd = client->dev.platform_data;
2012     const struct v4l2_queryctrl *qctrl;
2013
2014
2015     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2016
2017     if (!qctrl)
2018     {
2019         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2020         return -EINVAL;
2021     }
2022
2023     switch (ctrl->id)
2024     {
2025 #if CONFIG_SENSOR_Brightness
2026         case V4L2_CID_BRIGHTNESS:
2027             {
2028                 if (ctrl->value != sensor->info_priv.brightness)
2029                 {
2030                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2031                     {
2032                         return -EINVAL;
2033                     }
2034                     sensor->info_priv.brightness = ctrl->value;
2035                 }
2036                 break;
2037             }
2038 #endif
2039 #if CONFIG_SENSOR_Exposure
2040         case V4L2_CID_EXPOSURE:
2041             {
2042                 if (ctrl->value != sensor->info_priv.exposure)
2043                 {
2044                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2045                     {
2046                         return -EINVAL;
2047                     }
2048                     sensor->info_priv.exposure = ctrl->value;
2049                 }
2050                 break;
2051             }
2052 #endif
2053 #if CONFIG_SENSOR_Saturation
2054         case V4L2_CID_SATURATION:
2055             {
2056                 if (ctrl->value != sensor->info_priv.saturation)
2057                 {
2058                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2059                     {
2060                         return -EINVAL;
2061                     }
2062                     sensor->info_priv.saturation = ctrl->value;
2063                 }
2064                 break;
2065             }
2066 #endif
2067 #if CONFIG_SENSOR_Contrast
2068         case V4L2_CID_CONTRAST:
2069             {
2070                 if (ctrl->value != sensor->info_priv.contrast)
2071                 {
2072                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2073                     {
2074                         return -EINVAL;
2075                     }
2076                     sensor->info_priv.contrast = ctrl->value;
2077                 }
2078                 break;
2079             }
2080 #endif
2081 #if CONFIG_SENSOR_WhiteBalance
2082         case V4L2_CID_DO_WHITE_BALANCE:
2083             {
2084                 if (ctrl->value != sensor->info_priv.whiteBalance)
2085                 {
2086                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2087                     {
2088                         return -EINVAL;
2089                     }
2090                     sensor->info_priv.whiteBalance = ctrl->value;
2091                 }
2092                 break;
2093             }
2094 #endif
2095 #if CONFIG_SENSOR_Mirror
2096         case V4L2_CID_HFLIP:
2097             {
2098                 if (ctrl->value != sensor->info_priv.mirror)
2099                 {
2100                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2101                         return -EINVAL;
2102                     sensor->info_priv.mirror = ctrl->value;
2103                 }
2104                 break;
2105             }
2106 #endif
2107 #if CONFIG_SENSOR_Flip
2108         case V4L2_CID_VFLIP:
2109             {
2110                 if (ctrl->value != sensor->info_priv.flip)
2111                 {
2112                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2113                         return -EINVAL;
2114                     sensor->info_priv.flip = ctrl->value;
2115                 }
2116                 break;
2117             }
2118 #endif
2119         default:
2120             break;
2121     }
2122
2123     return 0;
2124 }
2125 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2126 {
2127     const struct v4l2_queryctrl *qctrl;
2128     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2129     struct sensor *sensor = to_sensor(client);
2130
2131     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2132
2133     if (!qctrl)
2134     {
2135         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2136         return -EINVAL;
2137     }
2138
2139     switch (ext_ctrl->id)
2140     {
2141         case V4L2_CID_SCENE:
2142             {
2143                 ext_ctrl->value = sensor->info_priv.scene;
2144                 break;
2145             }
2146         case V4L2_CID_EFFECT:
2147             {
2148                 ext_ctrl->value = sensor->info_priv.effect;
2149                 break;
2150             }
2151         case V4L2_CID_ZOOM_ABSOLUTE:
2152             {
2153                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2154                 break;
2155             }
2156         case V4L2_CID_ZOOM_RELATIVE:
2157             {
2158                 return -EINVAL;
2159             }
2160         case V4L2_CID_FOCUS_ABSOLUTE:
2161             {
2162                 ext_ctrl->value = sensor->info_priv.focus;
2163                 break;
2164             }
2165         case V4L2_CID_FOCUS_RELATIVE:
2166             {
2167                 return -EINVAL;
2168             }
2169         case V4L2_CID_FLASH:
2170             {
2171                 ext_ctrl->value = sensor->info_priv.flash;
2172                 break;
2173             }
2174         default :
2175             break;
2176     }
2177     return 0;
2178 }
2179 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2180 {
2181     const struct v4l2_queryctrl *qctrl;
2182     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2183     struct sensor *sensor = to_sensor(client);
2184     int val_offset;
2185
2186     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2187     
2188     if (!qctrl)
2189     {
2190         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2191         return -EINVAL;
2192     }
2193
2194         val_offset = 0;
2195     switch (ext_ctrl->id)
2196     {
2197 #if CONFIG_SENSOR_Scene
2198         case V4L2_CID_SCENE:
2199             {
2200                 if (ext_ctrl->value != sensor->info_priv.scene)
2201                 {
2202                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2203                         return -EINVAL;
2204                     sensor->info_priv.scene = ext_ctrl->value;
2205                 }
2206                 break;
2207             }
2208 #endif
2209 #if CONFIG_SENSOR_Effect
2210         case V4L2_CID_EFFECT:
2211             {
2212                 if (ext_ctrl->value != sensor->info_priv.effect)
2213                 {
2214                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2215                         return -EINVAL;
2216                     sensor->info_priv.effect= ext_ctrl->value;
2217                 }
2218                 break;
2219             }
2220 #endif
2221 #if CONFIG_SENSOR_DigitalZoom
2222         case V4L2_CID_ZOOM_ABSOLUTE:
2223             {
2224                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2225                     return -EINVAL;
2226
2227                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2228                 {
2229                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2230
2231                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2232                         return -EINVAL;
2233                     sensor->info_priv.digitalzoom += val_offset;
2234
2235                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2236                 }
2237
2238                 break;
2239             }
2240         case V4L2_CID_ZOOM_RELATIVE:
2241             {
2242                 if (ext_ctrl->value)
2243                 {
2244                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2245                         return -EINVAL;
2246                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2247
2248                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2249                 }
2250                 break;
2251             }
2252 #endif
2253 #if CONFIG_SENSOR_Focus
2254         case V4L2_CID_FOCUS_ABSOLUTE:
2255             {
2256                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2257                     return -EINVAL;
2258
2259                 if (ext_ctrl->value != sensor->info_priv.focus)
2260                 {
2261                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2262
2263                     sensor->info_priv.focus += val_offset;
2264                 }
2265
2266                 break;
2267             }
2268         case V4L2_CID_FOCUS_RELATIVE:
2269             {
2270                 if (ext_ctrl->value)
2271                 {
2272                     sensor->info_priv.focus += ext_ctrl->value;
2273
2274                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2275                 }
2276                 break;
2277             }
2278 #endif
2279 #if CONFIG_SENSOR_Flash
2280         case V4L2_CID_FLASH:
2281             {
2282                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2283                     return -EINVAL;
2284                 sensor->info_priv.flash = ext_ctrl->value;
2285
2286                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2287                 break;
2288             }
2289 #endif
2290         default:
2291             break;
2292     }
2293
2294     return 0;
2295 }
2296
2297 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2298 {
2299     struct i2c_client *client = v4l2_get_subdevdata(sd);
2300     struct soc_camera_device *icd = client->dev.platform_data;
2301     int i, error_cnt=0, error_idx=-1;
2302
2303
2304     for (i=0; i<ext_ctrl->count; i++) {
2305         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2306             error_cnt++;
2307             error_idx = i;
2308         }
2309     }
2310
2311     if (error_cnt > 1)
2312         error_idx = ext_ctrl->count;
2313
2314     if (error_idx != -1) {
2315         ext_ctrl->error_idx = error_idx;
2316         return -EINVAL;
2317     } else {
2318         return 0;
2319     }
2320 }
2321
2322 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2323 {
2324     struct i2c_client *client = v4l2_get_subdevdata(sd);
2325     struct soc_camera_device *icd = client->dev.platform_data;
2326     int i, error_cnt=0, error_idx=-1;
2327
2328
2329     for (i=0; i<ext_ctrl->count; i++) {
2330         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2331             error_cnt++;
2332             error_idx = i;
2333         }
2334     }
2335
2336     if (error_cnt > 1)
2337         error_idx = ext_ctrl->count;
2338
2339     if (error_idx != -1) {
2340         ext_ctrl->error_idx = error_idx;
2341         return -EINVAL;
2342     } else {
2343         return 0;
2344     }
2345 }
2346
2347 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2348  * this wasn't our capture interface, so, we wait for the right one */
2349 static int sensor_video_probe(struct soc_camera_device *icd,
2350                                struct i2c_client *client)
2351 {
2352     char pid = 0;
2353     int ret;
2354     struct sensor *sensor = to_sensor(client);
2355
2356     /* We must have a parent by now. And it cannot be a wrong one.
2357      * So this entire test is completely redundant. */
2358     if (!icd->dev.parent ||
2359             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2360                 return -ENODEV;
2361
2362         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2363                 ret = -ENODEV;
2364                 goto sensor_video_probe_err;
2365         }
2366     /* soft reset */
2367     ret = sensor_write(client, 0x12, 0x80);
2368     if (ret != 0)
2369     {
2370         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2371         return -ENODEV;
2372     }
2373     mdelay(50);          //delay 5 microseconds
2374
2375     /* check if it is an sensor sensor */
2376     ret = sensor_read(client, 0x0a, &pid);
2377     if (ret != 0) {
2378         SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2379         ret = -ENODEV;
2380         goto sensor_video_probe_err;
2381     }
2382
2383     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2384     if (pid == SENSOR_ID) {
2385         sensor->model = SENSOR_V4L2_IDENT;
2386     } else {
2387         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2388         ret = -ENODEV;
2389         goto sensor_video_probe_err;
2390     }
2391
2392     return 0;
2393
2394 sensor_video_probe_err:
2395
2396     return ret;
2397 }
2398
2399 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2400 {
2401         struct i2c_client *client = v4l2_get_subdevdata(sd);
2402     struct soc_camera_device *icd = client->dev.platform_data;
2403     struct sensor *sensor = to_sensor(client);
2404     int ret = 0;
2405 #if CONFIG_SENSOR_Flash 
2406     int i;
2407 #endif
2408     
2409         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2410         switch (cmd)
2411         {
2412                 case RK29_CAM_SUBDEV_DEACTIVATE:
2413                 {
2414                         sensor_deactivate(client);
2415                         break;
2416                 }
2417
2418                 case RK29_CAM_SUBDEV_IOREQUEST:
2419                 {
2420                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2421             if (sensor->sensor_io_request != NULL) { 
2422                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
2423                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2424                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2425                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
2426                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2427                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2428                 }
2429             } else {
2430                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2431                 ret = -EINVAL;
2432                 goto sensor_ioctl_end;
2433             }
2434             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2435                for this project */
2436             #if CONFIG_SENSOR_Flash     
2437                 if (sensor->sensor_gpio_res) { 
2438                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2439                     for (i = 0; i < icd->ops->num_controls; i++) {
2440                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2441                                         //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));  
2442                               sensor_controls[i].id=0xffff;                             
2443                                 }
2444                     }
2445                     sensor->info_priv.flash = 0xff;
2446                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2447                 }else{ //two cameras are the same,need to deal diffrently ,zyc
2448                     for (i = 0; i < icd->ops->num_controls; i++) {
2449                            if(0xffff == icd->ops->controls[i].id){
2450                               sensor_controls[i].id=V4L2_CID_FLASH;
2451                            }               
2452                     }
2453                 }
2454                 }
2455             #endif
2456                         break;
2457                 }
2458                 default:
2459                 {
2460                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2461                         break;
2462                 }
2463         }
2464 sensor_ioctl_end:
2465         return ret;
2466
2467 }
2468 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2469                             enum v4l2_mbus_pixelcode *code)
2470 {
2471         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2472                 return -EINVAL;
2473
2474         *code = sensor_colour_fmts[index].code;
2475         return 0;
2476 }
2477 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2478         .init           = sensor_init,
2479         .g_ctrl         = sensor_g_control,
2480         .s_ctrl         = sensor_s_control,
2481         .g_ext_ctrls          = sensor_g_ext_controls,
2482         .s_ext_ctrls          = sensor_s_ext_controls,
2483         .g_chip_ident   = sensor_g_chip_ident,
2484         .ioctl = sensor_ioctl,  
2485 };
2486
2487 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2488         .s_mbus_fmt     = sensor_s_fmt,
2489         .g_mbus_fmt     = sensor_g_fmt,
2490         .try_mbus_fmt   = sensor_try_fmt,
2491         .enum_mbus_fmt  = sensor_enum_fmt,
2492 };
2493 static struct v4l2_subdev_ops sensor_subdev_ops = {
2494         .core   = &sensor_subdev_core_ops,
2495         .video = &sensor_subdev_video_ops,
2496 };
2497
2498 static int sensor_probe(struct i2c_client *client,
2499                          const struct i2c_device_id *did)
2500 {
2501     struct sensor *sensor;
2502     struct soc_camera_device *icd = client->dev.platform_data;
2503     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2504     struct soc_camera_link *icl;
2505     int ret;
2506
2507     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2508     if (!icd) {
2509         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2510         return -EINVAL;
2511     }
2512
2513     icl = to_soc_camera_link(icd);
2514     if (!icl) {
2515         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2516         return -EINVAL;
2517     }
2518
2519     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2520         dev_warn(&adapter->dev,
2521                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2522         return -EIO;
2523     }
2524
2525     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2526     if (!sensor)
2527         return -ENOMEM;
2528
2529     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2530
2531     /* Second stage probe - when a capture adapter is there */
2532     icd->ops            = &sensor_ops;
2533
2534     sensor->info_priv.fmt = sensor_colour_fmts[0];
2535     
2536         #if CONFIG_SENSOR_I2C_NOSCHED
2537         atomic_set(&sensor->tasklock_cnt,0);
2538         #endif
2539
2540     ret = sensor_video_probe(icd, client);
2541     if (ret < 0) {
2542         icd->ops = NULL;
2543         i2c_set_clientdata(client, NULL);
2544         kfree(sensor);
2545                 sensor = NULL;
2546     }
2547         hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2548     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2549     return ret;
2550 }
2551
2552 static int sensor_remove(struct i2c_client *client)
2553 {
2554     struct sensor *sensor = to_sensor(client);
2555     struct soc_camera_device *icd = client->dev.platform_data;
2556
2557     icd->ops = NULL;
2558     i2c_set_clientdata(client, NULL);
2559     client->driver = NULL;
2560     kfree(sensor);
2561         sensor = NULL;
2562     return 0;
2563 }
2564
2565 static const struct i2c_device_id sensor_id[] = {
2566         {SENSOR_NAME_STRING(), 0 },
2567         { }
2568 };
2569 MODULE_DEVICE_TABLE(i2c, sensor_id);
2570
2571 static struct i2c_driver sensor_i2c_driver = {
2572         .driver = {
2573                 .name = SENSOR_NAME_STRING(),
2574         },
2575         .probe          = sensor_probe,
2576         .remove         = sensor_remove,
2577         .id_table       = sensor_id,
2578 };
2579
2580 static int __init sensor_mod_init(void)
2581 {
2582     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2583     return i2c_add_driver(&sensor_i2c_driver);
2584 }
2585
2586 static void __exit sensor_mod_exit(void)
2587 {
2588     i2c_del_driver(&sensor_i2c_driver);
2589 }
2590
2591 device_initcall_sync(sensor_mod_init);
2592 module_exit(sensor_mod_exit);
2593
2594 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2595 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2596 MODULE_LICENSE("GPL");
2597
2598