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