camera rk30:update to v0.x.10,wake up vb when cif have't receive data etc.
[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 #if 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         hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1303     flash_off_timer.icd = icd;
1304         flash_off_timer.timer.function = flash_off_func;
1305     #endif
1306
1307     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);
1308
1309     return 0;
1310 sensor_INIT_ERR:
1311         sensor_task_lock(client,0);
1312         sensor_deactivate(client);
1313     return ret;
1314 }
1315
1316 static int sensor_deactivate(struct i2c_client *client)
1317 {
1318         struct soc_camera_device *icd = client->dev.platform_data;
1319         //u8 reg_val;
1320
1321         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1322
1323         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1324         sensor_ioctrl(icd, Sensor_PowerDown, 1);
1325         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1326         icd->user_width = SENSOR_INIT_WIDTH;
1327     icd->user_height = SENSOR_INIT_HEIGHT;
1328         msleep(100);
1329         return 0;
1330 }
1331 static  struct reginfo sensor_power_down_sequence[]=
1332 {
1333     {0x00,0x00}
1334 };
1335 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1336 {
1337     int ret;
1338     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1339
1340     if (pm_msg.event == PM_EVENT_SUSPEND) {
1341         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1342         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1343         if (ret != 0) {
1344             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1345             return ret;
1346         } else {
1347             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1348             if (ret < 0) {
1349                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1350                 return -EINVAL;
1351             }
1352         }
1353     } else {
1354         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1355         return -EINVAL;
1356     }
1357     return 0;
1358 }
1359
1360 static int sensor_resume(struct soc_camera_device *icd)
1361 {
1362         int ret;
1363
1364     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1365     if (ret < 0) {
1366                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1367         return -EINVAL;
1368     }
1369
1370         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1371
1372     return 0;
1373
1374 }
1375
1376 static int sensor_set_bus_param(struct soc_camera_device *icd,
1377                                 unsigned long flags)
1378 {
1379
1380     return 0;
1381 }
1382
1383 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1384 {
1385     struct soc_camera_link *icl = to_soc_camera_link(icd);
1386     unsigned long flags = SENSOR_BUS_PARAM;
1387
1388     return soc_camera_apply_sensor_flags(icl, flags);
1389 }
1390
1391 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1392 {
1393     struct i2c_client *client = v4l2_get_subdevdata(sd);
1394     struct soc_camera_device *icd = client->dev.platform_data;
1395     struct sensor *sensor = to_sensor(client);
1396
1397     mf->width   = icd->user_width;
1398         mf->height      = icd->user_height;
1399         mf->code        = sensor->info_priv.fmt.code;
1400         mf->colorspace  = sensor->info_priv.fmt.colorspace;
1401         mf->field       = V4L2_FIELD_NONE;
1402
1403     return 0;
1404 }
1405 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1406 {
1407     bool ret = false;
1408
1409         if ((mf->width == 1024) && (mf->height == 768)) {
1410                 ret = true;
1411         } else if ((mf->width == 1280) && (mf->height == 1024)) {
1412                 ret = true;
1413         } else if ((mf->width == 1600) && (mf->height == 1200)) {
1414                 ret = true;
1415         } else if ((mf->width == 2048) && (mf->height == 1536)) {
1416                 ret = true;
1417         } else if ((mf->width == 2592) && (mf->height == 1944)) {
1418                 ret = true;
1419         }
1420
1421         if (ret == true)
1422                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1423         return ret;
1424 }
1425
1426 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1427 {
1428     bool ret = false;
1429
1430         if ((mf->width == 1280) && (mf->height == 720)) {
1431                 ret = true;
1432         } else if ((mf->width == 1920) && (mf->height == 1080)) {
1433                 ret = true;
1434         }
1435
1436         if (ret == true)
1437                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1438         return ret;
1439 }
1440 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1441 {
1442     struct i2c_client *client = v4l2_get_subdevdata(sd);
1443     const struct sensor_datafmt *fmt;
1444     struct sensor *sensor = to_sensor(client);
1445         const struct v4l2_queryctrl *qctrl;
1446         struct soc_camera_device *icd = client->dev.platform_data;
1447     struct reginfo *winseqe_set_addr=NULL;
1448         char readval;
1449     int ret=0, set_w,set_h;
1450
1451         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1452                                    ARRAY_SIZE(sensor_colour_fmts));
1453         if (!fmt) {
1454         ret = -EINVAL;
1455         goto sensor_s_fmt_end;
1456     }
1457
1458         if (sensor->info_priv.fmt.code != mf->code) {
1459                 switch (mf->code)
1460                 {
1461                         case V4L2_MBUS_FMT_YUYV8_2X8:
1462                         {
1463                                 //winseqe_set_addr = sensor_ClrFmt_YUYV;
1464                                 sensor_read(client, 0x3a, &readval);
1465                                 sensor_write(client,0x3a, readval&0xf7);
1466                                 sensor_read(client,0x3d,&readval);
1467                                 sensor_write(client,0x3d,readval&0xfe);
1468                                 break;
1469                         }
1470                         case V4L2_MBUS_FMT_UYVY8_2X8:
1471                         {
1472                                 //winseqe_set_addr = sensor_ClrFmt_UYVY;
1473                                 sensor_read(client, 0x3a, &readval);
1474                                 sensor_write(client,0x3a, readval|0x08);
1475                                 sensor_read(client,0x3d,&readval);
1476                                 sensor_write(client,0x3d,readval&0xfe);
1477                                 break;
1478                         }
1479                         default:
1480                                 break;
1481                 }
1482                 if (winseqe_set_addr != NULL) {
1483             sensor_write_array(client, winseqe_set_addr);
1484                         sensor->info_priv.fmt.code = mf->code;
1485             sensor->info_priv.fmt.colorspace= mf->colorspace;            
1486                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1487                 } else {
1488                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1489                 }
1490         }
1491
1492     set_w = mf->width;
1493     set_h = mf->height;
1494
1495         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1496         {
1497                 winseqe_set_addr = sensor_qcif;
1498         set_w = 176;
1499         set_h = 144;
1500         }
1501         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1502     {
1503         winseqe_set_addr = sensor_qvga;
1504         set_w = 320;
1505         set_h = 240;
1506     }
1507     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1508     {
1509         winseqe_set_addr = sensor_cif;
1510         set_w = 352;
1511         set_h = 288;
1512     }
1513     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1514     {
1515         winseqe_set_addr = sensor_vga;
1516         set_w = 640;
1517         set_h = 480;
1518     }
1519     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1520     {
1521         winseqe_set_addr = sensor_svga;
1522         set_w = 800;
1523         set_h = 600;
1524     }
1525     else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
1526     {
1527         winseqe_set_addr = sensor_720p;
1528         set_w = 1280;
1529         set_h = 720;
1530     }
1531         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
1532     {
1533         winseqe_set_addr = sensor_xga;
1534         set_w = 1024;
1535         set_h = 768;
1536     }
1537     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1538     {
1539                 winseqe_set_addr = sensor_vga; //sensor_sxga;
1540         set_w = 1280;
1541         set_h = 1024;
1542     }
1543     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1544     {
1545         winseqe_set_addr = sensor_uxga;
1546         set_w = 1600;
1547         set_h = 1200;
1548     }
1549     else
1550     {
1551         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
1552         set_w = SENSOR_INIT_WIDTH;
1553         set_h = SENSOR_INIT_HEIGHT;
1554                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1555     }
1556
1557     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
1558         #if CONFIG_SENSOR_Flash
1559         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
1560             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1561                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1562                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1563             }           
1564         } else {                                        /* ddl@rock-chips.com : Video */
1565             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1566                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1567                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1568             }
1569         }
1570         #endif
1571         ret |= sensor_write_array(client, winseqe_set_addr);
1572         if (ret != 0) {
1573             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1574             #if CONFIG_SENSOR_Flash
1575             if (sensor_fmt_capturechk(sd,mf) == true) {
1576                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1577                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1578                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1579                 }
1580             }
1581             #endif
1582             goto sensor_s_fmt_end;
1583         }
1584
1585         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
1586
1587                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
1588         #if CONFIG_SENSOR_Effect
1589                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1590                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1591         #endif
1592         #if CONFIG_SENSOR_WhiteBalance
1593                         if (sensor->info_priv.whiteBalance != 0) {
1594                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1595                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1596                         }
1597         #endif
1598                         sensor->info_priv.snap2preview = true;
1599                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
1600                 #if CONFIG_SENSOR_Effect
1601                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1602                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1603         #endif
1604         #if CONFIG_SENSOR_WhiteBalance
1605                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1606                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1607         #endif
1608                         sensor->info_priv.video2preview = true;
1609                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1610                 #if CONFIG_SENSOR_Effect
1611                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1612                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1613         #endif
1614         #if CONFIG_SENSOR_WhiteBalance
1615                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1616                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1617         #endif
1618                         sensor->info_priv.video2preview = false;
1619                         sensor->info_priv.snap2preview = false;
1620                 }
1621
1622         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1623     }
1624     else
1625     {
1626         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1627     }
1628
1629         mf->width = set_w;
1630     mf->height = set_h;
1631
1632 sensor_s_fmt_end:
1633     return ret;
1634 }
1635
1636 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1637 {
1638     struct i2c_client *client = v4l2_get_subdevdata(sd);
1639     struct sensor *sensor = to_sensor(client);
1640     const struct sensor_datafmt *fmt;
1641     int ret = 0;
1642    
1643         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1644                                    ARRAY_SIZE(sensor_colour_fmts));
1645         if (fmt == NULL) {
1646                 fmt = &sensor->info_priv.fmt;
1647         mf->code = fmt->code;
1648         } 
1649
1650     if (mf->height > SENSOR_MAX_HEIGHT)
1651         mf->height = SENSOR_MAX_HEIGHT;
1652     else if (mf->height < SENSOR_MIN_HEIGHT)
1653         mf->height = SENSOR_MIN_HEIGHT;
1654
1655     if (mf->width > SENSOR_MAX_WIDTH)
1656         mf->width = SENSOR_MAX_WIDTH;
1657     else if (mf->width < SENSOR_MIN_WIDTH)
1658         mf->width = SENSOR_MIN_WIDTH;
1659
1660     mf->colorspace = fmt->colorspace;
1661     
1662     return ret;
1663 }
1664
1665  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1666 {
1667     struct i2c_client *client = v4l2_get_subdevdata(sd);
1668
1669     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1670         return -EINVAL;
1671
1672     if (id->match.addr != client->addr)
1673         return -ENODEV;
1674
1675     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
1676     id->revision = 0;
1677
1678     return 0;
1679 }
1680 #if CONFIG_SENSOR_Brightness
1681 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1682 {
1683     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1684
1685     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1686     {
1687         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1688         {
1689             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1690             {
1691                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1692                 return -EINVAL;
1693             }
1694             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1695             return 0;
1696         }
1697     }
1698         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1699     return -EINVAL;
1700 }
1701 #endif
1702 #if CONFIG_SENSOR_Effect
1703 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1704 {
1705     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1706
1707     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1708     {
1709         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1710         {
1711             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1712             {
1713                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1714                 return -EINVAL;
1715             }
1716             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1717             return 0;
1718         }
1719     }
1720         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1721     return -EINVAL;
1722 }
1723 #endif
1724 #if CONFIG_SENSOR_Exposure
1725 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1726 {
1727     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1728
1729     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1730     {
1731         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1732         {
1733             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1734             {
1735                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1736                 return -EINVAL;
1737             }
1738             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1739             return 0;
1740         }
1741     }
1742         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1743     return -EINVAL;
1744 }
1745 #endif
1746 #if CONFIG_SENSOR_Saturation
1747 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1748 {
1749     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1750
1751     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1752     {
1753         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1754         {
1755             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1756             {
1757                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1758                 return -EINVAL;
1759             }
1760             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1761             return 0;
1762         }
1763     }
1764     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1765     return -EINVAL;
1766 }
1767 #endif
1768 #if CONFIG_SENSOR_Contrast
1769 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1770 {
1771     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1772
1773     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1774     {
1775         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1776         {
1777             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1778             {
1779                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1780                 return -EINVAL;
1781             }
1782             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1783             return 0;
1784         }
1785     }
1786     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1787     return -EINVAL;
1788 }
1789 #endif
1790 #if CONFIG_SENSOR_Mirror
1791 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1792 {
1793     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1794
1795     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1796     {
1797         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1798         {
1799             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1800             {
1801                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1802                 return -EINVAL;
1803             }
1804             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1805             return 0;
1806         }
1807     }
1808     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1809     return -EINVAL;
1810 }
1811 #endif
1812 #if CONFIG_SENSOR_Flip
1813 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1814 {
1815     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1816
1817     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1818     {
1819         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1820         {
1821             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1822             {
1823                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1824                 return -EINVAL;
1825             }
1826             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1827             return 0;
1828         }
1829     }
1830     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1831     return -EINVAL;
1832 }
1833 #endif
1834 #if CONFIG_SENSOR_Scene
1835 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1836 {
1837     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1838
1839     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1840     {
1841         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1842         {
1843             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1844             {
1845                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1846                 return -EINVAL;
1847             }
1848             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1849             return 0;
1850         }
1851     }
1852     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1853     return -EINVAL;
1854 }
1855 #endif
1856 #if CONFIG_SENSOR_WhiteBalance
1857 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1858 {
1859     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1860
1861     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1862     {
1863         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
1864         {
1865             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
1866             {
1867                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1868                 return -EINVAL;
1869             }
1870             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1871             return 0;
1872         }
1873     }
1874         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1875     return -EINVAL;
1876 }
1877 #endif
1878 #if CONFIG_SENSOR_DigitalZoom
1879 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
1880 {
1881     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1882     struct sensor *sensor = to_sensor(client);
1883         const struct v4l2_queryctrl *qctrl_info;
1884     int digitalzoom_cur, digitalzoom_total;
1885
1886         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1887         if (qctrl_info)
1888                 return -EINVAL;
1889
1890     digitalzoom_cur = sensor->info_priv.digitalzoom;
1891     digitalzoom_total = qctrl_info->maximum;
1892
1893     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
1894     {
1895         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1896         return -EINVAL;
1897     }
1898
1899     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
1900     {
1901         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1902         return -EINVAL;
1903     }
1904
1905     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
1906     {
1907         value = digitalzoom_total - digitalzoom_cur;
1908     }
1909
1910     if ((value < 0) && ((digitalzoom_cur + value) < 0))
1911     {
1912         value = 0 - digitalzoom_cur;
1913     }
1914
1915     digitalzoom_cur += value;
1916
1917     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
1918     {
1919         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
1920         {
1921             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1922             return -EINVAL;
1923         }
1924         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1925         return 0;
1926     }
1927
1928     return -EINVAL;
1929 }
1930 #endif
1931 #if CONFIG_SENSOR_Flash
1932 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1933 {    
1934     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
1935         if (value == 3) {       /* ddl@rock-chips.com: torch */
1936             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
1937         } else {
1938             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1939         }
1940         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1941         return 0;
1942     }
1943     
1944         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1945     return -EINVAL;
1946 }
1947 #endif
1948
1949 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1950 {
1951     struct i2c_client *client = v4l2_get_subdevdata(sd);
1952     struct sensor *sensor = to_sensor(client);
1953     const struct v4l2_queryctrl *qctrl;
1954
1955     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
1956
1957     if (!qctrl)
1958     {
1959         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
1960         return -EINVAL;
1961     }
1962
1963     switch (ctrl->id)
1964     {
1965         case V4L2_CID_BRIGHTNESS:
1966             {
1967                 ctrl->value = sensor->info_priv.brightness;
1968                 break;
1969             }
1970         case V4L2_CID_SATURATION:
1971             {
1972                 ctrl->value = sensor->info_priv.saturation;
1973                 break;
1974             }
1975         case V4L2_CID_CONTRAST:
1976             {
1977                 ctrl->value = sensor->info_priv.contrast;
1978                 break;
1979             }
1980         case V4L2_CID_DO_WHITE_BALANCE:
1981             {
1982                 ctrl->value = sensor->info_priv.whiteBalance;
1983                 break;
1984             }
1985         case V4L2_CID_EXPOSURE:
1986             {
1987                 ctrl->value = sensor->info_priv.exposure;
1988                 break;
1989             }
1990         case V4L2_CID_HFLIP:
1991             {
1992                 ctrl->value = sensor->info_priv.mirror;
1993                 break;
1994             }
1995         case V4L2_CID_VFLIP:
1996             {
1997                 ctrl->value = sensor->info_priv.flip;
1998                 break;
1999             }
2000         default :
2001                 break;
2002     }
2003     return 0;
2004 }
2005
2006
2007
2008 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2009 {
2010     struct i2c_client *client = v4l2_get_subdevdata(sd);
2011     struct sensor *sensor = to_sensor(client);
2012     struct soc_camera_device *icd = client->dev.platform_data;
2013     const struct v4l2_queryctrl *qctrl;
2014
2015
2016     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2017
2018     if (!qctrl)
2019     {
2020         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2021         return -EINVAL;
2022     }
2023
2024     switch (ctrl->id)
2025     {
2026 #if CONFIG_SENSOR_Brightness
2027         case V4L2_CID_BRIGHTNESS:
2028             {
2029                 if (ctrl->value != sensor->info_priv.brightness)
2030                 {
2031                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2032                     {
2033                         return -EINVAL;
2034                     }
2035                     sensor->info_priv.brightness = ctrl->value;
2036                 }
2037                 break;
2038             }
2039 #endif
2040 #if CONFIG_SENSOR_Exposure
2041         case V4L2_CID_EXPOSURE:
2042             {
2043                 if (ctrl->value != sensor->info_priv.exposure)
2044                 {
2045                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2046                     {
2047                         return -EINVAL;
2048                     }
2049                     sensor->info_priv.exposure = ctrl->value;
2050                 }
2051                 break;
2052             }
2053 #endif
2054 #if CONFIG_SENSOR_Saturation
2055         case V4L2_CID_SATURATION:
2056             {
2057                 if (ctrl->value != sensor->info_priv.saturation)
2058                 {
2059                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2060                     {
2061                         return -EINVAL;
2062                     }
2063                     sensor->info_priv.saturation = ctrl->value;
2064                 }
2065                 break;
2066             }
2067 #endif
2068 #if CONFIG_SENSOR_Contrast
2069         case V4L2_CID_CONTRAST:
2070             {
2071                 if (ctrl->value != sensor->info_priv.contrast)
2072                 {
2073                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2074                     {
2075                         return -EINVAL;
2076                     }
2077                     sensor->info_priv.contrast = ctrl->value;
2078                 }
2079                 break;
2080             }
2081 #endif
2082 #if CONFIG_SENSOR_WhiteBalance
2083         case V4L2_CID_DO_WHITE_BALANCE:
2084             {
2085                 if (ctrl->value != sensor->info_priv.whiteBalance)
2086                 {
2087                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2088                     {
2089                         return -EINVAL;
2090                     }
2091                     sensor->info_priv.whiteBalance = ctrl->value;
2092                 }
2093                 break;
2094             }
2095 #endif
2096 #if CONFIG_SENSOR_Mirror
2097         case V4L2_CID_HFLIP:
2098             {
2099                 if (ctrl->value != sensor->info_priv.mirror)
2100                 {
2101                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2102                         return -EINVAL;
2103                     sensor->info_priv.mirror = ctrl->value;
2104                 }
2105                 break;
2106             }
2107 #endif
2108 #if CONFIG_SENSOR_Flip
2109         case V4L2_CID_VFLIP:
2110             {
2111                 if (ctrl->value != sensor->info_priv.flip)
2112                 {
2113                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2114                         return -EINVAL;
2115                     sensor->info_priv.flip = ctrl->value;
2116                 }
2117                 break;
2118             }
2119 #endif
2120         default:
2121             break;
2122     }
2123
2124     return 0;
2125 }
2126 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2127 {
2128     const struct v4l2_queryctrl *qctrl;
2129     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2130     struct sensor *sensor = to_sensor(client);
2131
2132     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2133
2134     if (!qctrl)
2135     {
2136         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2137         return -EINVAL;
2138     }
2139
2140     switch (ext_ctrl->id)
2141     {
2142         case V4L2_CID_SCENE:
2143             {
2144                 ext_ctrl->value = sensor->info_priv.scene;
2145                 break;
2146             }
2147         case V4L2_CID_EFFECT:
2148             {
2149                 ext_ctrl->value = sensor->info_priv.effect;
2150                 break;
2151             }
2152         case V4L2_CID_ZOOM_ABSOLUTE:
2153             {
2154                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2155                 break;
2156             }
2157         case V4L2_CID_ZOOM_RELATIVE:
2158             {
2159                 return -EINVAL;
2160             }
2161         case V4L2_CID_FOCUS_ABSOLUTE:
2162             {
2163                 ext_ctrl->value = sensor->info_priv.focus;
2164                 break;
2165             }
2166         case V4L2_CID_FOCUS_RELATIVE:
2167             {
2168                 return -EINVAL;
2169             }
2170         case V4L2_CID_FLASH:
2171             {
2172                 ext_ctrl->value = sensor->info_priv.flash;
2173                 break;
2174             }
2175         default :
2176             break;
2177     }
2178     return 0;
2179 }
2180 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2181 {
2182     const struct v4l2_queryctrl *qctrl;
2183     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2184     struct sensor *sensor = to_sensor(client);
2185     int val_offset;
2186
2187     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2188     
2189     if (!qctrl)
2190     {
2191         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2192         return -EINVAL;
2193     }
2194
2195         val_offset = 0;
2196     switch (ext_ctrl->id)
2197     {
2198 #if CONFIG_SENSOR_Scene
2199         case V4L2_CID_SCENE:
2200             {
2201                 if (ext_ctrl->value != sensor->info_priv.scene)
2202                 {
2203                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2204                         return -EINVAL;
2205                     sensor->info_priv.scene = ext_ctrl->value;
2206                 }
2207                 break;
2208             }
2209 #endif
2210 #if CONFIG_SENSOR_Effect
2211         case V4L2_CID_EFFECT:
2212             {
2213                 if (ext_ctrl->value != sensor->info_priv.effect)
2214                 {
2215                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2216                         return -EINVAL;
2217                     sensor->info_priv.effect= ext_ctrl->value;
2218                 }
2219                 break;
2220             }
2221 #endif
2222 #if CONFIG_SENSOR_DigitalZoom
2223         case V4L2_CID_ZOOM_ABSOLUTE:
2224             {
2225                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2226                     return -EINVAL;
2227
2228                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2229                 {
2230                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2231
2232                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2233                         return -EINVAL;
2234                     sensor->info_priv.digitalzoom += val_offset;
2235
2236                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2237                 }
2238
2239                 break;
2240             }
2241         case V4L2_CID_ZOOM_RELATIVE:
2242             {
2243                 if (ext_ctrl->value)
2244                 {
2245                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2246                         return -EINVAL;
2247                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2248
2249                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2250                 }
2251                 break;
2252             }
2253 #endif
2254 #if CONFIG_SENSOR_Focus
2255         case V4L2_CID_FOCUS_ABSOLUTE:
2256             {
2257                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2258                     return -EINVAL;
2259
2260                 if (ext_ctrl->value != sensor->info_priv.focus)
2261                 {
2262                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2263
2264                     sensor->info_priv.focus += val_offset;
2265                 }
2266
2267                 break;
2268             }
2269         case V4L2_CID_FOCUS_RELATIVE:
2270             {
2271                 if (ext_ctrl->value)
2272                 {
2273                     sensor->info_priv.focus += ext_ctrl->value;
2274
2275                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2276                 }
2277                 break;
2278             }
2279 #endif
2280 #if CONFIG_SENSOR_Flash
2281         case V4L2_CID_FLASH:
2282             {
2283                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2284                     return -EINVAL;
2285                 sensor->info_priv.flash = ext_ctrl->value;
2286
2287                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2288                 break;
2289             }
2290 #endif
2291         default:
2292             break;
2293     }
2294
2295     return 0;
2296 }
2297
2298 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2299 {
2300     struct i2c_client *client = v4l2_get_subdevdata(sd);
2301     struct soc_camera_device *icd = client->dev.platform_data;
2302     int i, error_cnt=0, error_idx=-1;
2303
2304
2305     for (i=0; i<ext_ctrl->count; i++) {
2306         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2307             error_cnt++;
2308             error_idx = i;
2309         }
2310     }
2311
2312     if (error_cnt > 1)
2313         error_idx = ext_ctrl->count;
2314
2315     if (error_idx != -1) {
2316         ext_ctrl->error_idx = error_idx;
2317         return -EINVAL;
2318     } else {
2319         return 0;
2320     }
2321 }
2322
2323 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2324 {
2325     struct i2c_client *client = v4l2_get_subdevdata(sd);
2326     struct soc_camera_device *icd = client->dev.platform_data;
2327     int i, error_cnt=0, error_idx=-1;
2328
2329
2330     for (i=0; i<ext_ctrl->count; i++) {
2331         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2332             error_cnt++;
2333             error_idx = i;
2334         }
2335     }
2336
2337     if (error_cnt > 1)
2338         error_idx = ext_ctrl->count;
2339
2340     if (error_idx != -1) {
2341         ext_ctrl->error_idx = error_idx;
2342         return -EINVAL;
2343     } else {
2344         return 0;
2345     }
2346 }
2347
2348 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2349  * this wasn't our capture interface, so, we wait for the right one */
2350 static int sensor_video_probe(struct soc_camera_device *icd,
2351                                struct i2c_client *client)
2352 {
2353     char pid = 0;
2354     int ret;
2355     struct sensor *sensor = to_sensor(client);
2356
2357     /* We must have a parent by now. And it cannot be a wrong one.
2358      * So this entire test is completely redundant. */
2359     if (!icd->dev.parent ||
2360             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2361                 return -ENODEV;
2362
2363         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2364                 ret = -ENODEV;
2365                 goto sensor_video_probe_err;
2366         }
2367     /* soft reset */
2368     ret = sensor_write(client, 0x12, 0x80);
2369     if (ret != 0)
2370     {
2371         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2372         return -ENODEV;
2373     }
2374     mdelay(50);          //delay 5 microseconds
2375
2376     /* check if it is an sensor sensor */
2377     ret = sensor_read(client, 0x0a, &pid);
2378     if (ret != 0) {
2379         SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2380         ret = -ENODEV;
2381         goto sensor_video_probe_err;
2382     }
2383
2384     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2385     if (pid == SENSOR_ID) {
2386         sensor->model = SENSOR_V4L2_IDENT;
2387     } else {
2388         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2389         ret = -ENODEV;
2390         goto sensor_video_probe_err;
2391     }
2392
2393     return 0;
2394
2395 sensor_video_probe_err:
2396
2397     return ret;
2398 }
2399
2400 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2401 {
2402         struct i2c_client *client = v4l2_get_subdevdata(sd);
2403     struct soc_camera_device *icd = client->dev.platform_data;
2404     struct sensor *sensor = to_sensor(client);
2405     int ret = 0;
2406 #if CONFIG_SENSOR_Flash 
2407     int i;
2408 #endif
2409     
2410         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2411         switch (cmd)
2412         {
2413                 case RK29_CAM_SUBDEV_DEACTIVATE:
2414                 {
2415                         sensor_deactivate(client);
2416                         break;
2417                 }
2418
2419                 case RK29_CAM_SUBDEV_IOREQUEST:
2420                 {
2421                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2422             if (sensor->sensor_io_request != NULL) { 
2423                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
2424                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2425                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2426                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
2427                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2428                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2429                 }
2430             } else {
2431                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2432                 ret = -EINVAL;
2433                 goto sensor_ioctl_end;
2434             }
2435             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2436                for this project */
2437             #if CONFIG_SENSOR_Flash     
2438                 if (sensor->sensor_gpio_res) { 
2439                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2440                     for (i = 0; i < icd->ops->num_controls; i++) {
2441                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2442                                         //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));  
2443                               sensor_controls[i].id=0xffff;                             
2444                                 }
2445                     }
2446                     sensor->info_priv.flash = 0xff;
2447                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2448                 }else{ //two cameras are the same,need to deal diffrently ,zyc
2449                     for (i = 0; i < icd->ops->num_controls; i++) {
2450                            if(0xffff == icd->ops->controls[i].id){
2451                               sensor_controls[i].id=V4L2_CID_FLASH;
2452                            }               
2453                     }
2454                 }
2455                 }
2456             #endif
2457                         break;
2458                 }
2459                 default:
2460                 {
2461                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2462                         break;
2463                 }
2464         }
2465 sensor_ioctl_end:
2466         return ret;
2467
2468 }
2469 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2470                             enum v4l2_mbus_pixelcode *code)
2471 {
2472         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2473                 return -EINVAL;
2474
2475         *code = sensor_colour_fmts[index].code;
2476         return 0;
2477 }
2478 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2479         .init           = sensor_init,
2480         .g_ctrl         = sensor_g_control,
2481         .s_ctrl         = sensor_s_control,
2482         .g_ext_ctrls          = sensor_g_ext_controls,
2483         .s_ext_ctrls          = sensor_s_ext_controls,
2484         .g_chip_ident   = sensor_g_chip_ident,
2485         .ioctl = sensor_ioctl,  
2486 };
2487
2488 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2489         .s_mbus_fmt     = sensor_s_fmt,
2490         .g_mbus_fmt     = sensor_g_fmt,
2491         .try_mbus_fmt   = sensor_try_fmt,
2492         .enum_mbus_fmt  = sensor_enum_fmt,
2493 };
2494 static struct v4l2_subdev_ops sensor_subdev_ops = {
2495         .core   = &sensor_subdev_core_ops,
2496         .video = &sensor_subdev_video_ops,
2497 };
2498
2499 static int sensor_probe(struct i2c_client *client,
2500                          const struct i2c_device_id *did)
2501 {
2502     struct sensor *sensor;
2503     struct soc_camera_device *icd = client->dev.platform_data;
2504     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2505     struct soc_camera_link *icl;
2506     int ret;
2507
2508     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2509     if (!icd) {
2510         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2511         return -EINVAL;
2512     }
2513
2514     icl = to_soc_camera_link(icd);
2515     if (!icl) {
2516         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2517         return -EINVAL;
2518     }
2519
2520     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2521         dev_warn(&adapter->dev,
2522                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2523         return -EIO;
2524     }
2525
2526     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2527     if (!sensor)
2528         return -ENOMEM;
2529
2530     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2531
2532     /* Second stage probe - when a capture adapter is there */
2533     icd->ops            = &sensor_ops;
2534
2535     sensor->info_priv.fmt = sensor_colour_fmts[0];
2536     
2537         #if CONFIG_SENSOR_I2C_NOSCHED
2538         atomic_set(&sensor->tasklock_cnt,0);
2539         #endif
2540
2541     ret = sensor_video_probe(icd, client);
2542     if (ret < 0) {
2543         icd->ops = NULL;
2544         i2c_set_clientdata(client, NULL);
2545         kfree(sensor);
2546                 sensor = NULL;
2547     }
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