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