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