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