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