PM / devfreq: rk3399_dmc: rename driver and internals to rockchip
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / ov2659_old.c
1 /*
2 o* Driver for MT9M001 CMOS Image Sensor from Micron
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/miscdevice.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/soc_camera.h>
22 #include <plat/rk_camera.h>
23 #include <linux/vmalloc.h>
24 static int debug;
25 module_param(debug, int, S_IRUGO|S_IWUSR);
26
27 #define dprintk(level, fmt, arg...) do {                        \
28         if (debug >= level)                                     \
29         printk(KERN_WARNING fmt , ## arg); } while (0)
30
31 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
32 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
33
34
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
37
38 #define __STR(x) #x
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
41
42 #define MIN(x,y)   ((x<y) ? x: y)
43 #define MAX(x,y)    ((x>y) ? x: y)
44
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK29_CAM_SENSOR_OV2659
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV2659
48 #define SENSOR_ID 0x2656
49 #define SENSOR_MIN_WIDTH    800
50 #define SENSOR_MIN_HEIGHT   600
51 #define SENSOR_MAX_WIDTH    1600
52 #define SENSOR_MAX_HEIGHT   1200
53 #define SENSOR_INIT_WIDTH       sensor_init_width                       /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT      sensor_init_height
55 #define SENSOR_INIT_WINSEQADR  sensor_init_winseq_p
56 #define SENSOR_INIT_PIXFMT sensor_init_pixelcode
57 #define SENSOR_BUS_PARAM  sensor_init_busparam
58
59 #define CONFIG_SENSOR_WhiteBalance      1
60 #define CONFIG_SENSOR_Brightness        0
61 #define CONFIG_SENSOR_Contrast      0
62 #define CONFIG_SENSOR_Saturation    0
63 #define CONFIG_SENSOR_Effect        1
64 #define CONFIG_SENSOR_Scene         1
65 #define CONFIG_SENSOR_DigitalZoom   0
66 #define CONFIG_SENSOR_Focus         0
67 #define CONFIG_SENSOR_Exposure      0
68 #define CONFIG_SENSOR_Flash         1
69 #define CONFIG_SENSOR_Mirror        0 
70 #define CONFIG_SENSOR_Flip          0
71
72 #define CONFIG_SENSOR_I2C_SPEED     350000       /* Hz */
73 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
74 #define CONFIG_SENSOR_I2C_NOSCHED   0
75 #define CONFIG_SENSOR_I2C_RDWRCHK   0
76
77 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
78 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
79 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
80 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
81 #define COLOR_TEMPERATURE_OFFICE_DN     3500
82 #define COLOR_TEMPERATURE_OFFICE_UP     5000
83 #define COLOR_TEMPERATURE_HOME_DN       2500
84 #define COLOR_TEMPERATURE_HOME_UP       3500
85
86 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
87 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
88
89 #define SENSOR_AF_IS_ERR    (0x00<<0)
90 #define SENSOR_AF_IS_OK         (0x01<<0)
91 #define SENSOR_INIT_IS_ERR   (0x00<<28)
92 #define SENSOR_INIT_IS_OK    (0x01<<28)
93
94 struct reginfo
95 {
96     u16 reg;
97     u8 val;
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_OV2659_USER_DEFINED_SERIES
109 #include "ov2659_user_series.c"
110 #else
111 /* init 800*600 SVGA */
112 static struct reginfo sensor_init_data[] =
113 {
114         {0x3000, 0x0f},
115         {0x3001, 0xff},
116         {0x3002, 0xff},
117         //{0x0100, 0x01},       //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array
118         {0x3633, 0x3d},
119         {0x3620, 0x02},
120         {0x3631, 0x11},
121         {0x3612, 0x04},
122         {0x3630, 0x20},
123         {0x4702, 0x02},
124         {0x370c, 0x34},
125         {0x3004, 0x10},
126         {0x3005, 0x18},
127         {0x3800, 0x00},
128         {0x3801, 0x00},
129         {0x3802, 0x00},
130         {0x3803, 0x00},
131         {0x3804, 0x06},
132         {0x3805, 0x5f},
133         {0x3806, 0x04},
134         {0x3807, 0xb7},
135         {0x3808, 0x03},
136         {0x3809, 0x20},
137         {0x380a, 0x02},
138         {0x380b, 0x58},
139         {0x380c, 0x05},
140         {0x380d, 0x14},
141         {0x380e, 0x02},
142         {0x380f, 0x68},
143         {0x3811, 0x08},
144         {0x3813, 0x02},
145         {0x3814, 0x31},
146         {0x3815, 0x31},
147         {0x3a02, 0x02},
148         {0x3a03, 0x68},
149         {0x3a08, 0x00},
150         {0x3a09, 0x5c},
151         {0x3a0a, 0x00},
152         {0x3a0b, 0x4d},
153         {0x3a0d, 0x08},
154         {0x3a0e, 0x06},
155         {0x3a14, 0x02},
156         {0x3a15, 0x28},
157                 {0x4708, 0x01},
158         {0x3623, 0x00},
159         {0x3634, 0x76},
160         {0x3701, 0x44},
161         {0x3702, 0x18},
162         {0x3703, 0x24},
163         {0x3704, 0x24},
164         {0x3705, 0x0c},
165         {0x3820, 0x81},
166         {0x3821, 0x01},
167         {0x370a, 0x52},
168         {0x4608, 0x00},
169         {0x4609, 0x80},
170         {0x4300, 0x32},
171         {0x5086, 0x02},
172         {0x5000, 0xfb},
173         {0x5001, 0x1f},
174         {0x5002, 0x00},
175         {0x5025, 0x0e},
176         {0x5026, 0x18},
177         {0x5027, 0x34},
178         {0x5028, 0x4c},
179         {0x5029, 0x62},
180         {0x502a, 0x74},
181         {0x502b, 0x85},
182         {0x502c, 0x92},
183         {0x502d, 0x9e},
184         {0x502e, 0xb2},
185         {0x502f, 0xc0},
186         {0x5030, 0xcc},
187         {0x5031, 0xe0},
188         {0x5032, 0xee},
189         {0x5033, 0xf6},
190         {0x5034, 0x11},
191         {0x5070, 0x1c},
192         {0x5071, 0x5b},
193         {0x5072, 0x05},
194         {0x5073, 0x20},
195         {0x5074, 0x94},
196         {0x5075, 0xb4},
197         {0x5076, 0xb4},
198         {0x5077, 0xaf},
199         {0x5078, 0x05},
200         {0x5079, 0x98},
201         {0x507a, 0x21},
202         {0x5035, 0x6a},
203         {0x5036, 0x11},
204         {0x5037, 0x92},
205         {0x5038, 0x21},
206
207         {0x5039, 0xe1},
208         {0x503a, 0x01},
209         {0x503c, 0x05},
210         {0x503d, 0x08},
211         {0x503e, 0x08},
212         {0x503f, 0x64},
213         {0x5040, 0x58},
214         {0x5041, 0x2a},
215         {0x5042, 0xc5},
216         {0x5043, 0x2e},
217         {0x5044, 0x3a},
218         {0x5045, 0x3c},
219         {0x5046, 0x44},
220         {0x5047, 0xf8},
221         {0x5048, 0x08},
222         {0x5049, 0x70},
223         {0x504a, 0xf0},
224         {0x504b, 0xf0},
225         {0x500c, 0x03},
226         {0x500d, 0x20},
227         {0x500e, 0x02},
228         {0x500f, 0x5c},
229         {0x5010, 0x48},
230         {0x5011, 0x00},
231         {0x5012, 0x66},
232         {0x5013, 0x03},
233         {0x5014, 0x30},
234         {0x5015, 0x02},
235         {0x5016, 0x7c},
236         {0x5017, 0x40},
237         {0x5018, 0x00},
238         {0x5019, 0x66},
239         {0x501a, 0x03},
240         {0x501b, 0x10},
241         {0x501c, 0x02},
242         {0x501d, 0x7c},
243         {0x501e, 0x3a},
244         {0x501f, 0x00},
245         {0x5020, 0x66},
246         {0x506e, 0x44},
247         {0x5064, 0x08},
248         {0x5065, 0x10},
249         {0x5066, 0x12},
250         {0x5067, 0x02},
251         {0x506c, 0x08},
252         {0x506d, 0x10},
253         {0x506f, 0xa6},
254         {0x5068, 0x08},
255
256
257         {0x5069, 0x10},
258         {0x506a, 0x04},
259         {0x506b, 0x12},
260         {0x507e, 0x40},
261         {0x507f, 0x20},
262         {0x507b, 0x02},
263         {0x507a, 0x01},
264         {0x5084, 0x0c},
265         {0x5085, 0x3e},
266         {0x5005, 0x80},
267         {0x3a0f, 0x30},
268         {0x3a10, 0x28},
269         {0x3a1b, 0x32},
270         {0x3a1e, 0x26},
271         {0x3a11, 0x60},
272         {0x3a1f, 0x14},
273         {0x5060, 0x69},
274         {0x5061, 0x7d},
275         {0x5062, 0x7d},
276         {0x5063, 0x69},
277         {0x3004, 0x20},
278                 {0x0100, 0x01},
279
280         {0x0000, 0x00}
281 };
282
283 /* 1280x720 */
284 static struct reginfo sensor_720p[]=
285 {
286         {0x0103, 0x01   },
287         {0x3000, 0x0f   },
288         {0x3001, 0xff   },
289         {0x3002, 0xff   },
290         //{0x0100, 0x01 },  //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array
291         {0x3633, 0x3d   },
292         {0x3620, 0x02   },
293         {0x3631, 0x11   },
294         {0x3612, 0x04   },
295         {0x3630, 0x20   },
296         {0x4702, 0x02   },
297         {0x370c, 0x34   },
298         {0x3004, 0x10   },
299         {0x3005, 0x24   },
300         {0x3800, 0x00   },
301         {0x3801, 0xa0   },
302         {0x3802, 0x00   },
303         {0x3803, 0xf0   },
304         {0x3804, 0x05   },
305         {0x3805, 0xbf   },
306         {0x3806, 0x03   },
307         {0x3807, 0xcb   },
308         {0x3808, 0x05   },
309         {0x3809, 0x00   },
310         {0x380a, 0x02   },
311         {0x380b, 0xd0   },
312         {0x380c, 0x06   },
313         {0x380d, 0x4c   },
314         {0x380e, 0x02   },
315         {0x380f, 0xe8   },
316         {0x3811, 0x10   },
317         {0x3813, 0x06   },
318         {0x3814, 0x11   },
319         {0x3815, 0x11   },
320         {0x3a02, 0x02   },
321         {0x3a03, 0xe8   },
322         {0x3a08, 0x00   },
323         {0x3a09, 0x6f   },
324         {0x3a0a, 0x00   },
325         {0x3a0b, 0x5d   },
326         {0x3a0d, 0x08   },
327         {0x3a0e, 0x06   },
328         {0x3a14, 0x02   },
329         {0x3a15, 0x9a   },
330             {0x4708, 0x01  },
331         {0x3623, 0x02   },
332         {0x3634, 0x44   },
333         {0x3701, 0x41   },
334         {0x3702, 0x30   },
335         {0x3703, 0x48   },
336         {0x3704, 0x48   },
337         {0x3705, 0x18   },
338         {0x3820, 0x80   },
339         {0x3821, 0x00   },
340         {0x370a, 0x12   },
341         {0x4608, 0x00   },
342         {0x4609, 0x80   },
343         {0x4300, 0x32   },
344         {0x5086, 0x02   },
345         {0x5000, 0xfb   },
346         {0x5001, 0x1f   },
347         {0x5002, 0x00   },
348         {0x5025, 0x0e   },
349         {0x5026, 0x18   },
350         {0x5027, 0x34   },
351         {0x5028, 0x4c   },
352         {0x5029, 0x62   },
353         {0x502a, 0x74   },
354         {0x502b, 0x85   },
355         {0x502c, 0x92   },
356         {0x502d, 0x9e   },
357         {0x502e, 0xb2   },
358         {0x502f, 0xc0   },
359         {0x5030, 0xcc   },
360         {0x5031, 0xe0   },
361         {0x5032, 0xee   },
362         {0x5033, 0xf6   },
363         {0x5034, 0x11   },
364         {0x5070, 0x1c   },
365         {0x5071, 0x5b   },
366         {0x5072, 0x05   },
367         {0x5073, 0x20   },
368         {0x5074, 0x94   },
369         {0x5075, 0xb4   },
370         {0x5076, 0xb4   },
371         {0x5077, 0xaf   },
372         {0x5078, 0x05   },
373         {0x5079, 0x98   },
374         {0x507a, 0x21   },
375         {0x5035, 0x6a   },
376         {0x5036, 0x11   },
377         {0x5037, 0x92   },
378         {0x5038, 0x21   },
379         {0x5039, 0xe1   },
380         {0x503a, 0x01   },
381         {0x503c, 0x05   },
382         {0x503d, 0x08   },
383         {0x503e, 0x08   },
384         {0x503f, 0x64   },
385         {0x5040, 0x58   },
386         {0x5041, 0x2a   },
387         {0x5042, 0xc5   },
388         {0x5043, 0x2e   },
389         {0x5044, 0x3a   },
390         {0x5045, 0x3c   },
391         {0x5046, 0x44   },
392         {0x5047, 0xf8   },
393         {0x5048, 0x08   },
394         {0x5049, 0x70   },
395         {0x504a, 0xf0   },
396         {0x504b, 0xf0   },
397         {0x500c, 0x03   },
398         {0x500d, 0x20   },
399         {0x500e, 0x02   },
400         {0x500f, 0x5c   },
401         {0x5010, 0x48   },
402         {0x5011, 0x00   },
403         {0x5012, 0x66   },
404         {0x5013, 0x03   },
405         {0x5014, 0x30   },
406         {0x5015, 0x02   },
407         {0x5016, 0x7c   },
408         {0x5017, 0x40   },
409         {0x5018, 0x00   },
410         {0x5019, 0x66   },
411         {0x501a, 0x03   },
412         {0x501b, 0x10   },
413         {0x501c, 0x02   },
414         {0x501d, 0x7c   },
415         {0x501e, 0x3a   },
416         {0x501f, 0x00   },
417         {0x5020, 0x66   },
418         {0x506e, 0x44   },
419         {0x5064, 0x08   },
420         {0x5065, 0x10   },
421         {0x5066, 0x12   },
422         {0x5067, 0x02   },
423         {0x506c, 0x08   },
424         {0x506d, 0x10   },
425         {0x506f, 0xa6   },
426         {0x5068, 0x08   },
427         {0x5069, 0x10   },
428         {0x506a, 0x04   },
429         {0x506b, 0x12   },
430         {0x507e, 0x40   },
431         {0x507f, 0x20   },
432         {0x507b, 0x02   },
433         {0x507a, 0x01   },
434         {0x5084, 0x0c   },
435         {0x5085, 0x3e   },
436         {0x5005, 0x80   },
437         {0x3a0f, 0x30   },
438         {0x3a10, 0x28   },
439         {0x3a1b, 0x32   },
440         {0x3a1e, 0x26   },
441         {0x3a11, 0x60   },
442         {0x3a1f, 0x14   },
443         {0x5060, 0x69   },
444         {0x5061, 0x7d   },
445         {0x5062, 0x7d   },
446         {0x5063, 0x69   },
447         {0x0100, 0x01   },
448         {0x0000 ,0x00}
449
450 };
451
452 /* 1600X1200 UXGA */
453 static struct reginfo sensor_uxga[] =
454 {
455 #if 0
456     {0x3800, 0x00},
457         {0x3801, 0x00},
458         {0x3802, 0x00},
459         {0x3803, 0x00},
460         {0x3804, 0x06},
461         {0x3805, 0x5f},
462         {0x3806, 0x04},
463         {0x3807, 0xbb},
464         {0x3808, 0x06},
465         {0x3809, 0x40},
466         {0x380a, 0x04},
467         {0x380b, 0xb0},
468         {0x380c, 0x07},
469         {0x380d, 0x9f},
470         {0x380e, 0x04},
471         {0x380f, 0xd0},
472         {0x3811, 0x10},
473         {0x3813, 0x06},
474         {0x3814, 0x11},
475         {0x3815, 0x11},
476         {0x3a02, 0x04},
477         {0x3a03, 0xd0},
478         {0x3a08, 0x00},
479         {0x3a09, 0xb8},
480         {0x3a0a, 0x00},
481         {0x3a0b, 0x9a},
482         {0x3a0d, 0x08},
483         {0x3a0e, 0x06},
484         {0x3a14, 0x04},
485         {0x3a15, 0x50},
486         {0x3623, 0x00},
487         {0x3634, 0x44},
488         {0x3701, 0x44},
489         {0x3702, 0x30},
490         {0x3703, 0x48},
491         {0x3704, 0x48},
492         {0x3705, 0x18},
493         {0x3820, 0x80},
494         {0x3821, 0x00},
495         {0x370a, 0x12},
496         {0x4608, 0x00},
497         {0x4609, 0x80},
498         {0x5002, 0x00},
499         {0x3005, 0x24},
500         {0x3004, 0x20},
501 #else
502     //{0x3a00,OV2659ReadReg(0x3a00)&0xfb}, 
503     {0x3503,0x03}, 
504     //{0x3406,OV2659ReadReg(0x3406)|0x01}, 
505
506     {0x506e,0x44},      
507     {0x5064,0x08},      
508     {0x5065,0x10},
509     {0x5066,0x18},      // zenghaihui 20110920 16
510     {0x5067,0x10},
511     {0x506c,0x08},
512     {0x506d,0x10},      
513     {0x506f,0xa6},      
514     {0x5068,0x08},
515     {0x5069,0x10},      
516     {0x506a,0x08},
517     {0x506b,0x28},
518     {0x5084,0x14},//0c
519     {0x5085,0x3c},//34  
520     {0x5005,0x80}, 
521
522
523
524     {0x5066, 0x3c},         
525     {0x5067, 0x1a}, 
526     {0x506a, 0x0e},    
527     {0x506b, 0x2e},    
528
529     {0x3800, 0x00}, 
530     {0x3801, 0x00}, 
531     {0x3802, 0x00}, 
532     {0x3803, 0x00}, 
533     {0x3804, 0x06}, 
534     {0x3805, 0x5f}, 
535     {0x3806, 0x04}, 
536     {0x3807, 0xbb}, 
537     {0x3808, 0x06}, 
538     {0x3809, 0x40}, 
539     {0x380a, 0x04}, 
540     {0x380b, 0xb0}, 
541     {0x3811, 0x10}, 
542     {0x3813, 0x06}, 
543     {0x3814, 0x11}, 
544     {0x3815, 0x11}, 
545
546     {0x3623, 0x00}, 
547     {0x3634, 0x44}, 
548     {0x3701, 0x44}, 
549     {0x3208, 0xa2}, 
550     {0x3705, 0x18},      
551     {0x3820, 0x80}, 
552     {0x3821, 0x00}, 
553
554     {0x3003, 0x80},//10fps 
555     {0x3004, 0x20}, //10         
556     {0x3005, 0x18}, 
557     {0x3006, 0x0d}, 
558
559     {0x380c, 0x07}, 
560     {0x380d, 0x9f}, 
561     {0x380e, 0x04}, 
562     {0x380f, 0xd0}, 
563
564     {0x370a, 0x12}, 
565     {0x4608, 0x00}, 
566     {0x4609, 0x80}, 
567     {0x5002, 0x00}, 
568
569     {0x3a08, 0x00}, 
570     {0x3a09, 0x3e},//7b 
571     {0x3a0e, 0x13},//0a 
572
573     {0x3a0a, 0x00}, 
574     {0x3a0b, 0x3e},//7b                 
575     {0x3a0d, 0x13},//0a         
576
577     {0x4003, 0x88}, 
578 #endif
579         {0x0000, 0x00}
580 };
581
582 /* 1280X1024 SXGA */
583 static struct reginfo sensor_sxga[] =
584 {
585         {0x0, 0x0}
586 };
587 /* 1024X768 SXGA */
588 static struct reginfo sensor_xga[] =
589 {
590         {0x0, 0x0}
591 };
592 /* 800X600 SVGA*/
593 static struct reginfo sensor_svga[] =
594 {
595         {0x0100, 0x00},    //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
596         {0x3800, 0x00},
597         {0x3801, 0x00},
598         {0x3802, 0x00},
599         {0x3803, 0x00},
600         {0x3804, 0x06},
601         {0x3805, 0x5f},
602         {0x3806, 0x04},
603         {0x3807, 0xb7},
604         {0x3808, 0x03},
605         {0x3809, 0x20},
606         {0x380a, 0x02},
607         {0x380b, 0x58},
608         {0x380c, 0x05},
609         {0x380d, 0x14},
610         {0x380e, 0x02},
611         {0x380f, 0x68},
612         {0x3811, 0x08},
613         {0x3813, 0x02},
614         {0x3814, 0x31},
615         {0x3815, 0x31},
616         {0x3a02, 0x02},
617         {0x3a03, 0x68},
618         {0x3a08, 0x00},
619         {0x3a09, 0x5c},
620         {0x3a0a, 0x00},
621         {0x3a0b, 0x4d},
622         {0x3a0d, 0x08},
623         {0x3a0e, 0x06},
624         {0x3a14, 0x02},
625         {0x3a15, 0x28},
626         {0x3623, 0x00},
627         {0x3634, 0x76},
628         {0x3701, 0x44},
629         {0x3702, 0x18},
630         {0x3703, 0x24},
631         {0x3704, 0x24},
632         {0x3705, 0x0c},
633         {0x3820, 0x81},
634         {0x3821, 0x01},
635         {0x370a, 0x52},
636         {0x4608, 0x00},
637         {0x4609, 0x80},
638         {0x5002, 0x10},
639         {0x3005, 0x18},
640         {0x3004, 0x20},
641         {0x3503,0x00},
642         {0x0100, 0x01},         //software wake
643         {0x0000, 0x00}
644 };
645
646 /* 640X480 VGA */
647 static struct reginfo sensor_vga[] =
648 {
649         {0x0, 0x0}
650 };
651
652 /* 352X288 CIF */
653 static struct reginfo sensor_cif[] =
654 {
655         {0x0, 0x0}
656 };
657
658 /* 320*240 QVGA */
659 static  struct reginfo sensor_qvga[] =
660 {
661         {0x0, 0x0}
662 };
663
664 /* 176X144 QCIF*/
665 static struct reginfo sensor_qcif[] =
666 {
667         {0x0, 0x0}
668 };
669 #endif
670 #if 0
671 /* 160X120 QQVGA*/
672 static struct reginfo ov2655_qqvga[] =
673 {
674
675     {0x300E, 0x34},
676     {0x3011, 0x01},
677     {0x3012, 0x10},
678     {0x302a, 0x02},
679     {0x302b, 0xE6},
680     {0x306f, 0x14},
681     {0x3362, 0x90},
682
683     {0x3070, 0x5d},
684     {0x3072, 0x5d},
685     {0x301c, 0x07},
686     {0x301d, 0x07},
687
688     {0x3020, 0x01},
689     {0x3021, 0x18},
690     {0x3022, 0x00},
691     {0x3023, 0x06},
692     {0x3024, 0x06},
693     {0x3025, 0x58},
694     {0x3026, 0x02},
695     {0x3027, 0x61},
696     {0x3088, 0x00},
697     {0x3089, 0xa0},
698     {0x308a, 0x00},
699     {0x308b, 0x78},
700     {0x3316, 0x64},
701     {0x3317, 0x25},
702     {0x3318, 0x80},
703     {0x3319, 0x08},
704     {0x331a, 0x0a},
705     {0x331b, 0x07},
706     {0x331c, 0x80},
707     {0x331d, 0x38},
708     {0x3100, 0x00},
709     {0x3302, 0x11},
710
711     {0x0, 0x0},
712 };
713
714
715
716 static  struct reginfo ov2655_Sharpness_auto[] =
717 {
718     {0x3306, 0x00},
719 };
720
721 static  struct reginfo ov2655_Sharpness1[] =
722 {
723     {0x3306, 0x08},
724     {0x3371, 0x00},
725 };
726
727 static  struct reginfo ov2655_Sharpness2[][3] =
728 {
729     //Sharpness 2
730     {0x3306, 0x08},
731     {0x3371, 0x01},
732 };
733
734 static  struct reginfo ov2655_Sharpness3[] =
735 {
736     //default
737     {0x3306, 0x08},
738     {0x332d, 0x02},
739 };
740 static  struct reginfo ov2655_Sharpness4[]=
741 {
742     //Sharpness 4
743     {0x3306, 0x08},
744     {0x332d, 0x03},
745 };
746
747 static  struct reginfo ov2655_Sharpness5[] =
748 {
749     //Sharpness 5
750     {0x3306, 0x08},
751     {0x332d, 0x04},
752 };
753 #endif
754
755 static  struct reginfo sensor_ClrFmt_YUYV[]=
756 {
757     {0x4300, 0x30},
758     {0x0000, 0x00}
759 };
760
761 static  struct reginfo sensor_ClrFmt_UYVY[]=
762 {
763     {0x4300, 0x32},
764     {0x0000, 0x00}
765 };
766
767 #if CONFIG_SENSOR_WhiteBalance
768 static  struct reginfo sensor_WhiteB_Auto[]=
769 {
770     {0x3406, 0x00},  //AWB auto, bit[1]:0,auto
771     {0x0000, 0x00}
772 };
773 /* Cloudy Colour Temperature : 6500K - 8000K  */
774 static  struct reginfo sensor_WhiteB_Cloudy[]=
775 {
776     {0x3406, 0x01},
777     {0x3400, 0x07},
778     {0x3401, 0x08},
779     {0x3402, 0x04},
780     {0x3403, 0x00},
781     {0x3404, 0x05},
782     {0x3405, 0x00},
783     {0x0000, 0x00}
784 };
785 /* ClearDay Colour Temperature : 5000K - 6500K  */
786 static  struct reginfo sensor_WhiteB_ClearDay[]=
787 {
788     //Sunny
789     {0x3406, 0x01},
790     {0x3400, 0x07},
791     {0x3401, 0x02},
792     {0x3402, 0x04},
793     {0x3403, 0x00},
794     {0x3404, 0x05},
795     {0x3405, 0x15},
796     {0x0000, 0x00}
797 };
798 /* Office Colour Temperature : 3500K - 5000K  */
799 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
800 {
801     //Office
802     {0x3406, 0x01},
803     {0x3400, 0x06},
804     {0x3401, 0x2a},
805     {0x3402, 0x04},
806     {0x3403, 0x00},
807     {0x3404, 0x07},
808     {0x3405, 0x24},
809     {0x0000, 0x00}
810
811 };
812 /* Home Colour Temperature : 2500K - 3500K  */
813 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
814 {
815     //Home
816     {0x3406, 0x01},
817     {0x3400, 0x04},
818     {0x3401, 0x58},
819     {0x3402, 0x04},
820     {0x3403, 0x00},
821     {0x3404, 0x07},
822     {0x3405, 0x24},
823     {0x0000, 0x00}
824 };
825 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
826     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
827 };
828 #endif
829
830 #if CONFIG_SENSOR_Brightness
831 static  struct reginfo sensor_Brightness0[]=
832 {
833     // Brightness -2
834     {0x0000, 0x00}
835 };
836
837 static  struct reginfo sensor_Brightness1[]=
838 {
839     // Brightness -1
840
841     {0x0000, 0x00}
842 };
843
844 static  struct reginfo sensor_Brightness2[]=
845 {
846     //  Brightness 0
847
848     {0x0000, 0x00}
849 };
850
851 static  struct reginfo sensor_Brightness3[]=
852 {
853     // Brightness +1
854
855     {0x0000, 0x00}
856 };
857
858 static  struct reginfo sensor_Brightness4[]=
859 {
860     //  Brightness +2
861
862     {0x0000, 0x00}
863 };
864
865 static  struct reginfo sensor_Brightness5[]=
866 {
867     //  Brightness +3
868
869     {0x0000, 0x00}
870 };
871 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
872     sensor_Brightness4, sensor_Brightness5,NULL,
873 };
874
875 #endif
876
877 #if CONFIG_SENSOR_Effect
878 static  struct reginfo sensor_Effect_Normal[] =
879 {
880     {0x507b, 0x00},
881     {0x0000, 0x00}
882 };
883
884 static  struct reginfo sensor_Effect_WandB[] =
885 {
886     {0x507b, 0x20},
887     {0x0000, 0x00}
888 };
889
890 static  struct reginfo sensor_Effect_Sepia[] =
891 {
892     {0x507b, 0x18},
893     {0x507e, 0x40},
894     {0x507f, 0xa0},
895     {0x0000, 0x00}
896 };
897
898 static  struct reginfo sensor_Effect_Negative[] =
899 {
900     //Negative
901     {0x507b, 0x40}, //bit[6] negative
902     {0x0000, 0x00}
903 };
904 static  struct reginfo sensor_Effect_Bluish[] =
905 {
906     // Bluish
907     {0x507b, 0x18},
908     {0x507e, 0xa0},
909     {0x507f, 0x40},
910     {0x0000, 0x00}
911 };
912
913 static  struct reginfo sensor_Effect_Green[] =
914 {
915     //  Greenish
916     {0x507b, 0x18},
917     {0x507e, 0x60},
918     {0x507f, 0x60},
919     {0x0000, 0x00}
920 };
921 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
922     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
923 };
924 #endif
925 #if CONFIG_SENSOR_Exposure
926 static  struct reginfo sensor_Exposure0[]=
927 {
928         {0x0000, 0x00}
929 };
930
931 static  struct reginfo sensor_Exposure1[]=
932 {
933     {0x0000, 0x00}
934 };
935
936 static  struct reginfo sensor_Exposure2[]=
937 {
938     {0x0000, 0x00}
939 };
940
941 static  struct reginfo sensor_Exposure3[]=
942 {
943     {0x0000, 0x00}
944 };
945
946 static  struct reginfo sensor_Exposure4[]=
947 {
948     {0x0000, 0x00}
949 };
950
951 static  struct reginfo sensor_Exposure5[]=
952 {
953     {0x0000, 0x00}
954 };
955
956 static  struct reginfo sensor_Exposure6[]=
957 {
958     {0x0000, 0x00}
959 };
960
961 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
962     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
963 };
964 #endif
965 #if CONFIG_SENSOR_Saturation
966 static  struct reginfo sensor_Saturation0[]=
967 {
968     {0x0000, 0x00}
969 };
970
971 static  struct reginfo sensor_Saturation1[]=
972 {
973     {0x0000, 0x00}
974 };
975
976 static  struct reginfo sensor_Saturation2[]=
977 {
978     {0x0000, 0x00}
979 };
980 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
981
982 #endif
983 #if CONFIG_SENSOR_Contrast
984 static  struct reginfo sensor_Contrast0[]=
985 {
986     {0x0000, 0x00}
987 };
988
989 static  struct reginfo sensor_Contrast1[]=
990 {
991     {0x0000, 0x00}
992 };
993
994 static  struct reginfo sensor_Contrast2[]=
995 {
996     {0x0000, 0x00}
997 };
998
999 static  struct reginfo sensor_Contrast3[]=
1000 {
1001     {0x0000, 0x00}
1002 };
1003
1004 static  struct reginfo sensor_Contrast4[]=
1005 {
1006     {0x0000, 0x00}
1007 };
1008
1009
1010 static  struct reginfo sensor_Contrast5[]=
1011 {
1012     {0x0000, 0x00}
1013 };
1014
1015 static  struct reginfo sensor_Contrast6[]=
1016 {
1017     {0x0000, 0x00}
1018 };
1019 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1020     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1021 };
1022
1023 #endif
1024 #if CONFIG_SENSOR_Mirror
1025 static  struct reginfo sensor_MirrorOn[]=
1026 {
1027     {0x0000, 0x00}
1028 };
1029
1030 static  struct reginfo sensor_MirrorOff[]=
1031 {
1032     {0x0000, 0x00}
1033 };
1034 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1035 #endif
1036 #if CONFIG_SENSOR_Flip
1037 static  struct reginfo sensor_FlipOn[]=
1038 {
1039     {0x0000, 0x00}
1040 };
1041
1042 static  struct reginfo sensor_FlipOff[]=
1043 {
1044     {0x0000, 0x00}
1045 };
1046 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1047
1048 #endif
1049 #if CONFIG_SENSOR_Scene
1050 static  struct reginfo sensor_SceneAuto[] =
1051 {
1052     {0x3a00, 0x78},
1053     {0x0000, 0x00}
1054 };
1055
1056 static  struct reginfo sensor_SceneNight[] =
1057 {
1058     {0x3003, 0x80},
1059         {0x3004, 0x20},
1060         {0x3005, 0x18},
1061         {0x3006, 0x0d},
1062         {0x3a00, 0x7c},
1063         {0x3a02 ,0x07},
1064         {0x3a03 ,0x38},
1065         {0x3a14 ,0x07},
1066         {0x3a15 ,0x38},
1067     {0x0000, 0x00}
1068 };
1069 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1070
1071 #endif
1072 #if CONFIG_SENSOR_DigitalZoom
1073 static struct reginfo sensor_Zoom0[] =
1074 {
1075     {0x0, 0x0},
1076 };
1077
1078 static struct reginfo sensor_Zoom1[] =
1079 {
1080      {0x0, 0x0},
1081 };
1082
1083 static struct reginfo sensor_Zoom2[] =
1084 {
1085     {0x0, 0x0},
1086 };
1087
1088
1089 static struct reginfo sensor_Zoom3[] =
1090 {
1091     {0x0, 0x0},
1092 };
1093 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1094 #endif
1095 static const struct v4l2_querymenu sensor_menus[] =
1096 {
1097         #if CONFIG_SENSOR_WhiteBalance
1098     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1099     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1100     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1101     #endif
1102
1103         #if CONFIG_SENSOR_Effect
1104     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1105     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1106     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
1107     #endif
1108
1109         #if CONFIG_SENSOR_Scene
1110     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1111     #endif
1112
1113         #if CONFIG_SENSOR_Flash
1114     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1115     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1116     #endif
1117 };
1118
1119 static  struct v4l2_queryctrl sensor_controls[] =
1120 {
1121         #if CONFIG_SENSOR_WhiteBalance
1122     {
1123         .id             = V4L2_CID_DO_WHITE_BALANCE,
1124         .type           = V4L2_CTRL_TYPE_MENU,
1125         .name           = "White Balance Control",
1126         .minimum        = 0,
1127         .maximum        = 4,
1128         .step           = 1,
1129         .default_value = 0,
1130     },
1131     #endif
1132
1133         #if CONFIG_SENSOR_Brightness
1134         {
1135         .id             = V4L2_CID_BRIGHTNESS,
1136         .type           = V4L2_CTRL_TYPE_INTEGER,
1137         .name           = "Brightness Control",
1138         .minimum        = -3,
1139         .maximum        = 2,
1140         .step           = 1,
1141         .default_value = 0,
1142     },
1143     #endif
1144
1145         #if CONFIG_SENSOR_Effect
1146         {
1147         .id             = V4L2_CID_EFFECT,
1148         .type           = V4L2_CTRL_TYPE_MENU,
1149         .name           = "Effect Control",
1150         .minimum        = 0,
1151         .maximum        = 5,
1152         .step           = 1,
1153         .default_value = 0,
1154     },
1155         #endif
1156
1157         #if CONFIG_SENSOR_Exposure
1158         {
1159         .id             = V4L2_CID_EXPOSURE,
1160         .type           = V4L2_CTRL_TYPE_INTEGER,
1161         .name           = "Exposure Control",
1162         .minimum        = 0,
1163         .maximum        = 6,
1164         .step           = 1,
1165         .default_value = 0,
1166     },
1167         #endif
1168
1169         #if CONFIG_SENSOR_Saturation
1170         {
1171         .id             = V4L2_CID_SATURATION,
1172         .type           = V4L2_CTRL_TYPE_INTEGER,
1173         .name           = "Saturation Control",
1174         .minimum        = 0,
1175         .maximum        = 2,
1176         .step           = 1,
1177         .default_value = 0,
1178     },
1179     #endif
1180
1181         #if CONFIG_SENSOR_Contrast
1182         {
1183         .id             = V4L2_CID_CONTRAST,
1184         .type           = V4L2_CTRL_TYPE_INTEGER,
1185         .name           = "Contrast Control",
1186         .minimum        = -3,
1187         .maximum        = 3,
1188         .step           = 1,
1189         .default_value = 0,
1190     },
1191         #endif
1192
1193         #if CONFIG_SENSOR_Mirror
1194         {
1195         .id             = V4L2_CID_HFLIP,
1196         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1197         .name           = "Mirror Control",
1198         .minimum        = 0,
1199         .maximum        = 1,
1200         .step           = 1,
1201         .default_value = 0,
1202     },
1203     #endif
1204
1205         #if CONFIG_SENSOR_Flip
1206         {
1207         .id             = V4L2_CID_VFLIP,
1208         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1209         .name           = "Flip Control",
1210         .minimum        = 0,
1211         .maximum        = 1,
1212         .step           = 1,
1213         .default_value = 0,
1214     },
1215     #endif
1216
1217         #if CONFIG_SENSOR_Scene
1218     {
1219         .id             = V4L2_CID_SCENE,
1220         .type           = V4L2_CTRL_TYPE_MENU,
1221         .name           = "Scene Control",
1222         .minimum        = 0,
1223         .maximum        = 1,
1224         .step           = 1,
1225         .default_value = 0,
1226     },
1227     #endif
1228
1229         #if CONFIG_SENSOR_DigitalZoom
1230     {
1231         .id             = V4L2_CID_ZOOM_RELATIVE,
1232         .type           = V4L2_CTRL_TYPE_INTEGER,
1233         .name           = "DigitalZoom Control",
1234         .minimum        = -1,
1235         .maximum        = 1,
1236         .step           = 1,
1237         .default_value = 0,
1238     }, {
1239         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1240         .type           = V4L2_CTRL_TYPE_INTEGER,
1241         .name           = "DigitalZoom Control",
1242         .minimum        = 0,
1243         .maximum        = 3,
1244         .step           = 1,
1245         .default_value = 0,
1246     },
1247     #endif
1248
1249         #if CONFIG_SENSOR_Focus
1250         {
1251         .id             = V4L2_CID_FOCUS_RELATIVE,
1252         .type           = V4L2_CTRL_TYPE_INTEGER,
1253         .name           = "Focus Control",
1254         .minimum        = -1,
1255         .maximum        = 1,
1256         .step           = 1,
1257         .default_value = 0,
1258     }, {
1259         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1260         .type           = V4L2_CTRL_TYPE_INTEGER,
1261         .name           = "Focus Control",
1262         .minimum        = 0,
1263         .maximum        = 255,
1264         .step           = 1,
1265         .default_value = 125,
1266     },
1267     #endif
1268
1269         #if CONFIG_SENSOR_Flash
1270         {
1271         .id             = V4L2_CID_FLASH,
1272         .type           = V4L2_CTRL_TYPE_MENU,
1273         .name           = "Flash Control",
1274         .minimum        = 0,
1275         .maximum        = 3,
1276         .step           = 1,
1277         .default_value = 0,
1278     },
1279         #endif
1280 };
1281
1282 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1283 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1284 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1285 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1286 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1287 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1288 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1289 static int sensor_resume(struct soc_camera_device *icd);
1290 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1291 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1292 #if CONFIG_SENSOR_Effect
1293 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1294 #endif
1295 #if CONFIG_SENSOR_WhiteBalance
1296 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1297 #endif
1298 static int sensor_deactivate(struct i2c_client *client);
1299
1300 static struct soc_camera_ops sensor_ops =
1301 {
1302     .suspend                     = sensor_suspend,
1303     .resume                       = sensor_resume,
1304     .set_bus_param              = sensor_set_bus_param,
1305     .query_bus_param    = sensor_query_bus_param,
1306     .controls           = sensor_controls,
1307     .menus                         = sensor_menus,
1308     .num_controls               = ARRAY_SIZE(sensor_controls),
1309     .num_menus          = ARRAY_SIZE(sensor_menus),
1310 };
1311
1312 /* only one fixed colorspace per pixelcode */
1313 struct sensor_datafmt {
1314         enum v4l2_mbus_pixelcode code;
1315         enum v4l2_colorspace colorspace;
1316 };
1317
1318 /* Find a data format by a pixel code in an array */
1319 static const struct sensor_datafmt *sensor_find_datafmt(
1320         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1321         int n)
1322 {
1323         int i;
1324         for (i = 0; i < n; i++)
1325                 if (fmt[i].code == code)
1326                         return fmt + i;
1327
1328         return NULL;
1329 }
1330
1331 static const struct sensor_datafmt sensor_colour_fmts[] = {
1332     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1333     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1334 };
1335
1336 typedef struct sensor_info_priv_s
1337 {
1338     int whiteBalance;
1339     int brightness;
1340     int contrast;
1341     int saturation;
1342     int effect;
1343     int scene;
1344     int digitalzoom;
1345     int focus;
1346     int flash;
1347     int exposure;
1348         bool snap2preview;
1349         bool video2preview;
1350     unsigned char mirror;                                        /* HFLIP */
1351     unsigned char flip;                                          /* VFLIP */
1352     unsigned int winseqe_cur_addr;
1353     struct sensor_datafmt fmt;
1354     unsigned int funmodule_state;
1355 } sensor_info_priv_t;
1356
1357 struct sensor_parameter
1358 {
1359         unsigned int PreviewDummyPixels;
1360     unsigned int CaptureDummyPixels;
1361         unsigned int preview_exposure;
1362         unsigned short int preview_line_width;
1363         unsigned short int preview_gain;
1364
1365         unsigned short int PreviewPclk;
1366         unsigned short int CapturePclk;
1367         char awb[6];
1368 };
1369
1370 struct sensor
1371 {
1372     struct v4l2_subdev subdev;
1373     struct i2c_client *client;
1374     sensor_info_priv_t info_priv;
1375     struct sensor_parameter parameter;
1376     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1377 #if CONFIG_SENSOR_I2C_NOSCHED
1378         atomic_t tasklock_cnt;
1379 #endif
1380         struct rk29camera_platform_data *sensor_io_request;
1381     struct rk29camera_gpio_res *sensor_gpio_res;
1382 };
1383
1384
1385 static struct sensor* to_sensor(const struct i2c_client *client)
1386 {
1387     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1388 }
1389
1390 static int sensor_task_lock(struct i2c_client *client, int lock)
1391 {
1392 #if CONFIG_SENSOR_I2C_NOSCHED
1393         int cnt = 3;
1394     struct sensor *sensor = to_sensor(client);
1395
1396         if (lock) {
1397                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1398                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1399                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1400                                 msleep(35);
1401                                 cnt--;
1402                         }
1403                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1404                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1405                                 goto sensor_task_lock_err;
1406                         }
1407                         preempt_disable();
1408                 }
1409
1410                 atomic_add(1, &sensor->tasklock_cnt);
1411         } else {
1412                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1413                         atomic_sub(1, &sensor->tasklock_cnt);
1414
1415                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1416                                 preempt_enable();
1417                 }
1418         }
1419         return 0;
1420 sensor_task_lock_err:
1421         return -1;  
1422 #else
1423     return 0;
1424 #endif
1425
1426 }
1427
1428 /* sensor register write */
1429 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1430 {
1431     int err,cnt;
1432     u8 buf[3];
1433     struct i2c_msg msg[1];
1434
1435     buf[0] = reg >> 8;
1436     buf[1] = reg & 0xFF;
1437     buf[2] = val;
1438
1439     msg->addr = client->addr;
1440     msg->flags = client->flags;
1441     msg->buf = buf;
1442     msg->len = sizeof(buf);
1443     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1444     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1445
1446     cnt = 3;
1447     err = -EAGAIN;
1448
1449     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1450         err = i2c_transfer(client->adapter, msg, 1);
1451
1452         if (err >= 0) {
1453             return 0;
1454         } else {
1455                 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1456                 udelay(10);
1457         }
1458     }
1459
1460     return err;
1461 }
1462
1463 /* sensor register read */
1464 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1465 {
1466     int err,cnt;
1467     u8 buf[2];
1468     struct i2c_msg msg[2];
1469
1470     buf[0] = reg >> 8;
1471     buf[1] = reg & 0xFF;
1472
1473     msg[0].addr = client->addr;
1474     msg[0].flags = client->flags;
1475     msg[0].buf = buf;
1476     msg[0].len = sizeof(buf);
1477     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1478     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1479
1480     msg[1].addr = client->addr;
1481     msg[1].flags = client->flags|I2C_M_RD;
1482     msg[1].buf = buf;
1483     msg[1].len = 1;
1484     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1485     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1486
1487     cnt = 3;
1488     err = -EAGAIN;
1489     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1490         err = i2c_transfer(client->adapter, msg, 2);
1491
1492         if (err >= 0) {
1493             *val = buf[0];
1494             return 0;
1495         } else {
1496                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1497             udelay(10);
1498         }
1499     }
1500
1501     return err;
1502 }
1503
1504 /* write a array of registers  */
1505 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1506 {
1507     int err = 0, cnt;
1508     int i = 0;
1509 #if CONFIG_SENSOR_I2C_RDWRCHK    
1510         char valchk;
1511 #endif
1512
1513         cnt = 0;
1514         if (sensor_task_lock(client, 1) < 0)
1515                 goto sensor_write_array_end;
1516
1517     while (regarray[i].reg != 0)
1518     {
1519         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1520         if (err < 0)
1521         {
1522             if (cnt-- > 0) {
1523                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1524                                 i = 0;
1525                                 continue;
1526             } else {
1527                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1528                 err = -EPERM;
1529                                 goto sensor_write_array_end;
1530             }
1531         } else {
1532         #if CONFIG_SENSOR_I2C_RDWRCHK
1533                         sensor_read(client, regarray[i].reg, &valchk);
1534                         if (valchk != regarray[i].val)
1535                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1536                 #endif
1537         }
1538         i++;
1539     }
1540
1541 sensor_write_array_end:
1542         sensor_task_lock(client,0);
1543         return err;
1544 }
1545 #if CONFIG_SENSOR_I2C_RDWRCHK
1546 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1547 {
1548     int cnt;
1549     int i = 0;
1550         char valchk;
1551
1552         cnt = 0;
1553         valchk = 0;
1554     while (regarray[i].reg != 0)
1555     {
1556                 sensor_read(client, regarray[i].reg, &valchk);
1557                 if (valchk != regarray[i].val)
1558                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1559
1560         i++;
1561     }
1562     return 0;
1563 }
1564 #endif
1565
1566 static int sensor_parameter_record(struct i2c_client *client)
1567 {
1568         u8 ret_l,ret_m,ret_h;
1569         int tp_l,tp_m,tp_h;
1570         struct sensor *sensor = to_sensor(client);
1571
1572     sensor_read(client,0x3a00, &ret_l);
1573     sensor_write(client,0x3a00, ret_l&0xfb);
1574
1575         sensor_write(client,0x3503,0x07);       //stop AE/AG
1576
1577         sensor_read(client,0x3500,&ret_h);
1578         sensor_read(client,0x3501, &ret_m);
1579         sensor_read(client,0x3502, &ret_l);
1580         tp_l = ret_l;
1581         tp_m = ret_m;
1582         tp_h = ret_h;
1583     sensor->parameter.preview_exposure = ((tp_h<<12) & 0xF000) | ((tp_m<<4) & 0x0FF0) | ((tp_l>>4) & 0x0F);
1584         
1585         //Read back AGC Gain for preview
1586         sensor_read(client,0x350b, &ret_l);
1587         sensor->parameter.preview_gain = ret_l;
1588
1589     sensor->parameter.CapturePclk = 24000;
1590     sensor->parameter.PreviewPclk = 24000;
1591     sensor->parameter.PreviewDummyPixels = 0;
1592     sensor->parameter.CaptureDummyPixels = 0;
1593         SENSOR_DG(" %s Read 0x350b=0x%02x  PreviewExposure:%d 0x3500=0x%02x  0x3501=0x%02x 0x3502=0x%02x \n",
1594      SENSOR_NAME_STRING(), ret_l,sensor->parameter.preview_exposure,tp_h, tp_m, tp_l);
1595         return 0;
1596 }
1597 #define OV2659_FULL_PERIOD_PIXEL_NUMS  (1940)  // default pixel#(w/o dummy pixels) in UXGA mode
1598 #define OV2659_FULL_PERIOD_LINE_NUMS   (1238)  // default line#(w/o dummy lines) in UXGA mode
1599 #define OV2659_PV_PERIOD_PIXEL_NUMS       (970)  // default pixel#(w/o dummy pixels) in SVGA mode
1600 #define OV2659_PV_PERIOD_LINE_NUMS        (618)   // default line#(w/o dummy lines) in SVGA mode
1601
1602 /* SENSOR EXPOSURE LINE LIMITATION */
1603 #define OV2659_FULL_EXPOSURE_LIMITATION   (1236)
1604 #define OV2659_PV_EXPOSURE_LIMITATION     (618)
1605
1606 // SENSOR UXGA SIZE
1607 #define OV2659_IMAGE_SENSOR_FULL_WIDTH    (1600)
1608 #define OV2659_IMAGE_SENSOR_FULL_HEIGHT   (1200)
1609
1610 #define OV2659_FULL_GRAB_WIDTH                          (OV2659_IMAGE_SENSOR_FULL_WIDTH - 16)
1611 #define OV2659_FULL_GRAB_HEIGHT                         (OV2659_IMAGE_SENSOR_FULL_HEIGHT - 12)
1612
1613 static void OV2659SetDummy(struct i2c_client *client,unsigned int dummy_pixels, unsigned int dummy_lines)
1614 {
1615     unsigned char val;
1616         unsigned int temp_reg1, temp_reg2;
1617         unsigned int temp_reg;
1618         
1619         if (dummy_pixels > 0)
1620         {
1621                 sensor_read(client,0x380D,&val);    // HTS[b7~b0]
1622                 temp_reg1 = val;
1623                 sensor_read(client,0x380C,&val);    // HTS[b15~b8]
1624                 temp_reg2 = val;
1625                 temp_reg = (temp_reg1 & 0xFF) | (temp_reg2 << 8);
1626         
1627                 temp_reg += dummy_pixels;
1628         
1629                 sensor_write(client,0x380D,(temp_reg&0xFF));         //HTS[7:0]
1630                 sensor_write(client,0x380C,((temp_reg&0xFF00)>>8));  //HTS[15:8]
1631         }
1632
1633         if (dummy_lines > 0)
1634         {
1635                 sensor_read(client,0x380F,&val);    // VTS[b7~b0]
1636                 temp_reg1 = val;
1637                 sensor_read(client,0x380E,&val);    // VTS[b15~b8]
1638                 temp_reg2 = val;
1639                 temp_reg = (temp_reg1 & 0xFF) | (temp_reg2 << 8);
1640         
1641                 temp_reg += dummy_lines;
1642         
1643                 sensor_write(client,0x380F,(temp_reg&0xFF));         //VTS[7:0]
1644                 sensor_write(client,0x380E,((temp_reg&0xFF00)>>8));  //VTS[15:8]
1645         }
1646 }    /* OV2659_set_dummy */
1647 static void OV2659WriteShutter(struct i2c_client *client,bool is_preview, unsigned int shutter)
1648 {
1649         unsigned int extra_exposure_lines = 0;
1650
1651     if (shutter < 1)
1652         {
1653                 shutter = 1;
1654         }
1655         
1656         if (is_preview) 
1657         {
1658                 if (shutter <= OV2659_PV_EXPOSURE_LIMITATION) 
1659                 {
1660                         extra_exposure_lines = 0;
1661                 }
1662                 else 
1663                 {
1664                         extra_exposure_lines=shutter - OV2659_PV_EXPOSURE_LIMITATION;
1665                 }
1666                 
1667         }
1668         else 
1669         {
1670                 if (shutter <= OV2659_FULL_EXPOSURE_LIMITATION) 
1671                 {
1672                         extra_exposure_lines = 0;
1673                 }
1674                 else 
1675                 {
1676                         extra_exposure_lines = shutter - OV2659_FULL_EXPOSURE_LIMITATION;
1677                 }
1678                 
1679         }
1680         
1681         //AEC PK EXPOSURE
1682         shutter*=16;
1683         sensor_write(client,0x3502, (shutter & 0x00FF));           //AEC[7:0]
1684         sensor_write(client,0x3501, ((shutter & 0x0FF00) >>8));  //AEC[15:8]
1685         sensor_write(client,0x3500, ((shutter & 0xFF0000) >> 16));      
1686         
1687         if(extra_exposure_lines>0)
1688         {
1689                 // set extra exposure line [aec add vts]
1690                 sensor_write(client,0x3507, extra_exposure_lines & 0xFF);          // EXVTS[b7~b0]
1691                 sensor_write(client,0x3506, (extra_exposure_lines & 0xFF00) >> 8); // EXVTS[b15~b8]
1692         }
1693         else
1694         {
1695                 // set extra exposure line [aec add vts]
1696                 sensor_write(client,0x3507, 0x00);          // EXVTS[b7~b0]
1697                 sensor_write(client,0x3506, 0x00); // EXVTS[b15~b8]
1698         }
1699         
1700 }    /* OV2659_write_shutter */
1701 static int sensor_ae_transfer(struct i2c_client *client)
1702 {
1703         unsigned int prev_line_len,cap_line_len,shutter;
1704         struct sensor *sensor = to_sensor(client);
1705
1706     mdelay(100);
1707     shutter = sensor->parameter.preview_exposure;
1708
1709     OV2659SetDummy(client,600,0);       
1710         
1711     prev_line_len = OV2659_PV_PERIOD_PIXEL_NUMS + sensor->parameter.PreviewDummyPixels;
1712         cap_line_len = OV2659_FULL_PERIOD_PIXEL_NUMS + sensor->parameter.CaptureDummyPixels;
1713         shutter = (shutter * sensor->parameter.CapturePclk) / sensor->parameter.PreviewPclk;
1714         shutter = (shutter * prev_line_len) / cap_line_len;
1715         shutter*=2;
1716
1717     OV2659WriteShutter(client,0,shutter);
1718     
1719         
1720         return 0;
1721 }
1722
1723
1724 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1725 {
1726         struct soc_camera_link *icl = to_soc_camera_link(icd);
1727         int ret = 0;
1728
1729     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1730         switch (cmd)
1731         {
1732                 case Sensor_PowerDown:
1733                 {
1734                         if (icl->powerdown) {
1735                                 ret = icl->powerdown(icd->pdev, on);
1736                                 if (ret == RK29_CAM_IO_SUCCESS) {
1737                                         if (on == 0) {
1738                                                 mdelay(2);
1739                                                 if (icl->reset)
1740                                                         icl->reset(icd->pdev);
1741                                         }
1742                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1743                                         ret = -ENODEV;
1744                                         goto sensor_power_end;
1745                                 }
1746                         }
1747                         break;
1748                 }
1749                 case Sensor_Flash:
1750                 {
1751                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1752                 struct sensor *sensor = to_sensor(client);
1753
1754                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1755                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1756                 if(on){
1757                     //flash off after 2 secs
1758                         hrtimer_cancel(&(flash_off_timer.timer));
1759                         hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
1760                     }
1761                         }
1762             break;
1763                 }
1764                 default:
1765                 {
1766                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1767                         break;
1768                 }
1769         }
1770 sensor_power_end:
1771         return ret;
1772 }
1773
1774 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
1775         struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
1776     sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
1777         SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
1778     return 0;
1779     
1780 }
1781
1782 static s32 sensor_init_width = 800;
1783 static s32 sensor_init_height = 600;
1784 static unsigned long sensor_init_busparam = (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING|SOCAM_HSYNC_ACTIVE_HIGH| SOCAM_VSYNC_ACTIVE_LOW|SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ);
1785 static enum v4l2_mbus_pixelcode sensor_init_pixelcode = V4L2_MBUS_FMT_YUYV8_2X8;
1786 static struct reginfo* sensor_init_data_p = sensor_init_data;
1787 static struct reginfo* sensor_init_winseq_p = sensor_svga;
1788 static struct reginfo* sensor_init_winseq_board = NULL;
1789 static struct reginfo* sensor_init_data_board = NULL;
1790 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1791 {
1792     struct i2c_client *client = v4l2_get_subdevdata(sd);
1793     struct soc_camera_device *icd = client->dev.platform_data;
1794     struct sensor *sensor = to_sensor(client);
1795         const struct v4l2_queryctrl *qctrl;
1796     const struct sensor_datafmt *fmt;
1797     char value;
1798     int ret,pid = 0,i = 0,j=0;
1799         //if val ==1,mean that sensor need to be reinit
1800     struct rk29camera_platform_data* tmp_plat_data =sensor->sensor_io_request;
1801     
1802     sensor_init_data_p = sensor_init_data;
1803         sensor_init_winseq_p = sensor_svga;
1804         sensor_init_width = 800;
1805         sensor_init_height = 600;
1806
1807         if (tmp_plat_data != NULL) { 
1808                 for(i = 0;i < RK_CAM_NUM;i++){
1809                         if ((tmp_plat_data->sensor_init_data[i])&& tmp_plat_data->info[i].dev_name &&
1810                                 (strcmp(tmp_plat_data->info[i].dev_name, dev_name(icd->pdev)) == 0)) {
1811                                 break;
1812                         }
1813                 
1814                 }
1815         }
1816         if(tmp_plat_data &&(i < RK_CAM_NUM) && tmp_plat_data->sensor_init_data[i]){
1817         //user has defined the init data
1818                 //init reg
1819                 int tmp_init_data_size = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data_size;
1820         if(tmp_init_data_size > 2){//init data is valid 
1821                 if((sizeof(struct reginfo) != sizeof(struct reginfo_t))){
1822                         if(sensor_init_data_board) {
1823                                 vfree(sensor_init_data_board);
1824                                 sensor_init_data_board = NULL;
1825                         }
1826                         sensor_init_data_board = (struct reginfo*)vmalloc(tmp_init_data_size);
1827                         if(!sensor_init_data_board)
1828                                 SENSOR_TR("%s :vmalloc init data erro !",__FUNCTION__);
1829                         for(j = 0;j< tmp_init_data_size;j++) {
1830                                 sensor_init_data_board[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].reg;
1831                                 sensor_init_data_board[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].val;
1832                         }
1833                         sensor_init_data_p = sensor_init_data_board;
1834                 } else{
1835                         sensor_init_data_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data);
1836                 }
1837             }
1838                 //init winseq
1839                 int tmp_winseq_size = tmp_plat_data->sensor_init_data[i]->rk_sensor_winseq_size;
1840         if(tmp_winseq_size > 2){
1841                 if(sizeof(struct reginfo) != sizeof(struct reginfo_t)){
1842                         if(sensor_init_winseq_board) {
1843                                 vfree(sensor_init_winseq_board);
1844                                 sensor_init_winseq_board = NULL;
1845                         }
1846                         sensor_init_winseq_board = (struct reginfo*)vmalloc(tmp_winseq_size);
1847                         if(!sensor_init_winseq_board)
1848                                 SENSOR_TR("%s :vmalloc erro !",__FUNCTION__);
1849                         for(j = 0;j< tmp_winseq_size;j++){
1850                                 sensor_init_winseq_board[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].reg;
1851                                 sensor_init_winseq_board[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].val;
1852                         }
1853                         sensor_init_winseq_p = sensor_init_winseq_board;
1854                 } else{
1855                         sensor_init_winseq_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq);
1856                 }
1857             }
1858                 //init width,height,bus,pixelcode
1859                 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width != INVALID_VALUE)
1860                         sensor_init_width = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width;
1861                 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height != INVALID_VALUE)
1862                         sensor_init_height = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height;
1863                 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param != INVALID_VALUE)
1864                         sensor_init_busparam = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param;
1865                 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode != INVALID_VALUE)
1866                         sensor_init_pixelcode = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode;
1867         }
1868     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1869
1870         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1871                 ret = -ENODEV;
1872                 goto sensor_INIT_ERR;
1873         }
1874
1875     /* soft reset */
1876         if (sensor_task_lock(client,1)<0)
1877                 goto sensor_INIT_ERR;
1878
1879     ret = sensor_write(client, 0x0103, 0x01);
1880     if (ret != 0)
1881     {
1882         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1883         ret = -ENODEV;
1884                 goto sensor_INIT_ERR;
1885     }
1886     mdelay(5);  //delay 5 microseconds
1887
1888         /* check if it is an sensor sensor */
1889     ret = sensor_read(client, 0x300a, &value);
1890     if (ret != 0) {
1891         SENSOR_TR("read chip id high byte failed\n");
1892         ret = -ENODEV;
1893         goto sensor_INIT_ERR;
1894     }
1895
1896     pid |= (value << 8);
1897
1898     ret = sensor_read(client, 0x300b, &value);
1899     if (ret != 0) {
1900         SENSOR_TR("read chip id low byte failed\n");
1901         ret = -ENODEV;
1902         goto sensor_INIT_ERR;
1903     }
1904
1905     pid |= (value & 0xff);
1906     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1907     if (pid == SENSOR_ID) {
1908         sensor->model = SENSOR_V4L2_IDENT;
1909     } else {
1910         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1911         ret = -ENODEV;
1912         goto sensor_INIT_ERR;
1913     }
1914
1915     ret = sensor_write_array(client, sensor_init_data_p);
1916     if (ret != 0)
1917     {
1918         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1919         goto sensor_INIT_ERR;
1920     }
1921         sensor_task_lock(client,0);
1922     
1923     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
1924     fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1925     if (!fmt) {
1926         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1927         ret = -EINVAL;
1928         goto sensor_INIT_ERR;
1929     }
1930         sensor->info_priv.fmt = *fmt;
1931
1932     /* sensor sensor information for initialization  */
1933         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1934         if (qctrl)
1935         sensor->info_priv.whiteBalance = qctrl->default_value;
1936         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1937         if (qctrl)
1938         sensor->info_priv.brightness = qctrl->default_value;
1939         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1940         if (qctrl)
1941         sensor->info_priv.effect = qctrl->default_value;
1942         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1943         if (qctrl)
1944         sensor->info_priv.exposure = qctrl->default_value;
1945
1946         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1947         if (qctrl)
1948         sensor->info_priv.saturation = qctrl->default_value;
1949         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1950         if (qctrl)
1951         sensor->info_priv.contrast = qctrl->default_value;
1952         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1953         if (qctrl)
1954         sensor->info_priv.mirror = qctrl->default_value;
1955         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1956         if (qctrl)
1957         sensor->info_priv.flip = qctrl->default_value;
1958         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1959         if (qctrl)
1960         sensor->info_priv.scene = qctrl->default_value;
1961         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1962         if (qctrl)
1963         sensor->info_priv.digitalzoom = qctrl->default_value;
1964
1965     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
1966         #if CONFIG_SENSOR_Focus
1967     sensor_set_focus();
1968     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1969         if (qctrl)
1970         sensor->info_priv.focus = qctrl->default_value;
1971         #endif
1972
1973         #if CONFIG_SENSOR_Flash 
1974         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1975         if (qctrl)
1976         sensor->info_priv.flash = qctrl->default_value;
1977     flash_off_timer.icd = icd;
1978         flash_off_timer.timer.function = flash_off_func;
1979     #endif
1980     msleep(800);
1981     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);
1982     sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
1983     return 0;
1984 sensor_INIT_ERR:
1985     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1986         sensor_task_lock(client,0);
1987         sensor_deactivate(client);
1988     return ret;
1989 }
1990
1991 static int sensor_deactivate(struct i2c_client *client)
1992 {
1993         struct soc_camera_device *icd = client->dev.platform_data;
1994         u8 reg_val;
1995     struct sensor *sensor = to_sensor(client);
1996         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1997     
1998         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1999     if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2000         sensor_task_lock(client, 1);
2001         sensor_read(client,0x3000,&reg_val);
2002         sensor_write(client, 0x3000, reg_val&0xfc);
2003         sensor_write(client, 0x3001, 0x00);
2004         sensor_read(client,0x3002,&reg_val);
2005         sensor_write(client, 0x3002, reg_val&0x1f);
2006         sensor_task_lock(client, 0);        
2007     }
2008     sensor_ioctrl(icd, Sensor_PowerDown, 1); 
2009     msleep(10); 
2010         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2011         icd->user_width = SENSOR_INIT_WIDTH;
2012     icd->user_height = SENSOR_INIT_HEIGHT;
2013     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2014         
2015         return 0;
2016 }
2017
2018 static  struct reginfo sensor_power_down_sequence[]=
2019 {
2020     {0x00,0x00}
2021 };
2022 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2023 {
2024     int ret;
2025     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2026
2027     if (pm_msg.event == PM_EVENT_SUSPEND) {
2028         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2029         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2030         if (ret != 0) {
2031             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2032             return ret;
2033         } else {
2034             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2035             if (ret < 0) {
2036                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2037                 return -EINVAL;
2038             }
2039         }
2040     } else {
2041         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2042         return -EINVAL;
2043     }
2044     return 0;
2045 }
2046
2047 static int sensor_resume(struct soc_camera_device *icd)
2048 {
2049         int ret;
2050
2051     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2052     if (ret < 0) {
2053                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2054         return -EINVAL;
2055     }
2056
2057         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2058
2059     return 0;
2060
2061 }
2062
2063 static int sensor_set_bus_param(struct soc_camera_device *icd,
2064                                 unsigned long flags)
2065 {
2066
2067     return 0;
2068 }
2069
2070 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2071 {
2072     struct soc_camera_link *icl = to_soc_camera_link(icd);
2073     unsigned long flags = SENSOR_BUS_PARAM;
2074
2075     return soc_camera_apply_sensor_flags(icl, flags);
2076 }
2077
2078 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2079 {
2080     struct i2c_client *client = v4l2_get_subdevdata(sd);
2081     struct soc_camera_device *icd = client->dev.platform_data;
2082     struct sensor *sensor = to_sensor(client);
2083
2084     mf->width   = icd->user_width;
2085         mf->height      = icd->user_height;
2086         mf->code        = sensor->info_priv.fmt.code;
2087         mf->colorspace  = sensor->info_priv.fmt.colorspace;
2088         mf->field       = V4L2_FIELD_NONE;
2089
2090     return 0;
2091 }
2092 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2093 {
2094     bool ret = false;
2095
2096         if ((mf->width == 1024) && (mf->height == 768)) {
2097                 ret = true;
2098         } else if ((mf->width == 1280) && (mf->height == 1024)) {
2099                 ret = true;
2100         } else if ((mf->width == 1600) && (mf->height == 1200)) {
2101                 ret = true;
2102         } else if ((mf->width == 2048) && (mf->height == 1536)) {
2103                 ret = true;
2104         } else if ((mf->width == 2592) && (mf->height == 1944)) {
2105                 ret = true;
2106         }
2107
2108         if (ret == true)
2109                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2110         return ret;
2111 }
2112
2113 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2114 {
2115     bool ret = false;
2116
2117         if ((mf->width == 1280) && (mf->height == 720)) {
2118                 ret = true;
2119         } else if ((mf->width == 1920) && (mf->height == 1080)) {
2120                 ret = true;
2121         }
2122
2123         if (ret == true)
2124                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2125         return ret;
2126 }
2127 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2128 {
2129     struct i2c_client *client = v4l2_get_subdevdata(sd);
2130     const struct sensor_datafmt *fmt;
2131     struct sensor *sensor = to_sensor(client);
2132         const struct v4l2_queryctrl *qctrl;
2133         struct soc_camera_device *icd = client->dev.platform_data;
2134     struct reginfo *winseqe_set_addr=NULL;
2135     int ret=0, set_w,set_h;
2136
2137         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2138                                    ARRAY_SIZE(sensor_colour_fmts));
2139         if (!fmt) {
2140         ret = -EINVAL;
2141         goto sensor_s_fmt_end;
2142     }
2143
2144         if (sensor->info_priv.fmt.code != mf->code) {
2145                 switch (mf->code)
2146                 {
2147                         case V4L2_MBUS_FMT_YUYV8_2X8:
2148                         {
2149                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2150                                 break;
2151                         }
2152                         case V4L2_MBUS_FMT_UYVY8_2X8:
2153                         {
2154                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2155                                 break;
2156                         }
2157                         default:
2158                                 break;
2159                 }
2160                 if (winseqe_set_addr != NULL) {
2161             sensor_write_array(client, winseqe_set_addr);
2162                         sensor->info_priv.fmt.code = mf->code;
2163             sensor->info_priv.fmt.colorspace= mf->colorspace;            
2164                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2165                 } else {
2166                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2167                 }
2168         }
2169
2170     set_w = mf->width;
2171     set_h = mf->height;
2172
2173         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2174         {
2175                 winseqe_set_addr = sensor_qcif;
2176         set_w = 176;
2177         set_h = 144;
2178         }
2179         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2180     {
2181         winseqe_set_addr = sensor_qvga;
2182         set_w = 320;
2183         set_h = 240;
2184     }
2185     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2186     {
2187         winseqe_set_addr = sensor_cif;
2188         set_w = 352;
2189         set_h = 288;
2190     }
2191     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2192     {
2193         winseqe_set_addr = sensor_vga;
2194         set_w = 640;
2195         set_h = 480;
2196     }
2197     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2198     {
2199         winseqe_set_addr = sensor_svga;
2200         set_w = 800;
2201         set_h = 600;
2202     }
2203     else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
2204     {
2205         winseqe_set_addr = sensor_720p;
2206         set_w = 1280;
2207         set_h = 720;
2208     }
2209         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
2210     {
2211         winseqe_set_addr = sensor_xga;
2212         set_w = 1024;
2213         set_h = 768;
2214     }
2215     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2216     {
2217         winseqe_set_addr = sensor_sxga;
2218         set_w = 1280;
2219         set_h = 1024;
2220     }
2221     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2222     {
2223         winseqe_set_addr = sensor_uxga;
2224         set_w = 1600;
2225         set_h = 1200;
2226     }
2227     else
2228     {
2229         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2230         set_w = SENSOR_INIT_WIDTH;
2231         set_h = SENSOR_INIT_HEIGHT;
2232                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2233     }
2234
2235     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
2236         #if CONFIG_SENSOR_Flash
2237         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
2238             sensor_parameter_record(client);
2239             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2240                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2241                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2242             }           
2243         } else {                                        /* ddl@rock-chips.com : Video */
2244             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2245                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2246                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2247             }
2248         }
2249         #endif
2250         ret |= sensor_write_array(client, winseqe_set_addr);
2251         if (ret != 0) {
2252             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2253             #if CONFIG_SENSOR_Flash
2254             if (sensor_fmt_capturechk(sd,mf) == true) {
2255                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2256                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2257                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2258                 }
2259             }
2260             #endif
2261             goto sensor_s_fmt_end;
2262         }
2263
2264         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
2265
2266                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
2267             sensor_ae_transfer(client);
2268                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2269                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2270                         if (sensor->info_priv.whiteBalance != 0) {
2271                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2272                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2273                         }
2274             msleep(600);
2275                         sensor->info_priv.snap2preview = true;
2276                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
2277                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2278                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2279                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2280                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2281                         sensor->info_priv.video2preview = true;
2282                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2283                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2284                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2285                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2286                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2287             msleep(600);
2288                         sensor->info_priv.video2preview = false;
2289                         sensor->info_priv.snap2preview = false;
2290                 }
2291
2292         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2293     }
2294     else
2295     {
2296         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2297     }
2298
2299         mf->width = set_w;
2300     mf->height = set_h;
2301
2302 sensor_s_fmt_end:
2303     return ret;
2304 }
2305
2306 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2307 {
2308     struct i2c_client *client = v4l2_get_subdevdata(sd);
2309     struct sensor *sensor = to_sensor(client);
2310     const struct sensor_datafmt *fmt;
2311     int ret = 0,set_w,set_h;
2312    
2313         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2314                                    ARRAY_SIZE(sensor_colour_fmts));
2315         if (fmt == NULL) {
2316                 fmt = &sensor->info_priv.fmt;
2317         mf->code = fmt->code;
2318         } 
2319
2320     if (mf->height > SENSOR_MAX_HEIGHT)
2321         mf->height = SENSOR_MAX_HEIGHT;
2322     else if (mf->height < SENSOR_MIN_HEIGHT)
2323         mf->height = SENSOR_MIN_HEIGHT;
2324
2325     if (mf->width > SENSOR_MAX_WIDTH)
2326         mf->width = SENSOR_MAX_WIDTH;
2327     else if (mf->width < SENSOR_MIN_WIDTH)
2328         mf->width = SENSOR_MIN_WIDTH;
2329
2330     set_w = mf->width;
2331     set_h = mf->height;
2332
2333         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2334         {
2335         set_w = 176;
2336         set_h = 144;
2337         }
2338         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2339     {
2340         set_w = 320;
2341         set_h = 240;
2342     }
2343     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2344     {
2345         set_w = 352;
2346         set_h = 288;
2347     }
2348     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2349     {
2350         set_w = 640;
2351         set_h = 480;
2352     }
2353     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2354     {
2355         set_w = 800;
2356         set_h = 600;
2357     }
2358     else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
2359     {
2360         set_w = 1280;
2361         set_h = 720;
2362     }
2363         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
2364     {
2365         set_w = 1024;
2366         set_h = 768;
2367     }
2368     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2369     {
2370         set_w = 1280;
2371         set_h = 1024;
2372     }
2373     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2374     {
2375         set_w = 1600;
2376         set_h = 1200;
2377     }
2378     else
2379     {              /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2380         set_w = SENSOR_INIT_WIDTH;
2381         set_h = SENSOR_INIT_HEIGHT;             
2382     }
2383
2384     mf->width = set_w;
2385     mf->height = set_h;
2386     mf->colorspace = fmt->colorspace;
2387     
2388     return ret;
2389 }
2390
2391  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2392 {
2393     struct i2c_client *client = v4l2_get_subdevdata(sd);
2394
2395     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2396         return -EINVAL;
2397
2398     if (id->match.addr != client->addr)
2399         return -ENODEV;
2400
2401     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2402     id->revision = 0;
2403
2404     return 0;
2405 }
2406 #if CONFIG_SENSOR_Brightness
2407 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2408 {
2409     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2410
2411     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2412     {
2413         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2414         {
2415             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2416             {
2417                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2418                 return -EINVAL;
2419             }
2420             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2421             return 0;
2422         }
2423     }
2424         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2425     return -EINVAL;
2426 }
2427 #endif
2428 #if CONFIG_SENSOR_Effect
2429 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2430 {
2431     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2432
2433     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2434     {
2435         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2436         {
2437             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2438             {
2439                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2440                 return -EINVAL;
2441             }
2442             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2443             return 0;
2444         }
2445     }
2446         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2447     return -EINVAL;
2448 }
2449 #endif
2450 #if CONFIG_SENSOR_Exposure
2451 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2452 {
2453     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2454
2455     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2456     {
2457         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2458         {
2459             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2460             {
2461                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2462                 return -EINVAL;
2463             }
2464             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2465             return 0;
2466         }
2467     }
2468         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2469     return -EINVAL;
2470 }
2471 #endif
2472 #if CONFIG_SENSOR_Saturation
2473 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2474 {
2475     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2476
2477     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2478     {
2479         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2480         {
2481             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2482             {
2483                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2484                 return -EINVAL;
2485             }
2486             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2487             return 0;
2488         }
2489     }
2490     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2491     return -EINVAL;
2492 }
2493 #endif
2494 #if CONFIG_SENSOR_Contrast
2495 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2496 {
2497     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2498
2499     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2500     {
2501         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2502         {
2503             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2504             {
2505                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2506                 return -EINVAL;
2507             }
2508             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2509             return 0;
2510         }
2511     }
2512     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2513     return -EINVAL;
2514 }
2515 #endif
2516 #if CONFIG_SENSOR_Mirror
2517 static int sensor_mirror(struct i2c_client *client, int on)
2518 {
2519     char val;
2520     int err = 0;    
2521
2522     if (on) {
2523         err = sensor_read(client, 0x3821, &val);
2524         if (err == 0) {
2525             val |= 0x06;
2526             err = sensor_write(client, 0x3821, val);
2527         }
2528     } else {
2529         err = sensor_read(client, 0x3821, &val);
2530         if (err == 0) {
2531             val &= 0xf9;
2532             err = sensor_write(client, 0x3821, val);
2533         }
2534     }
2535
2536     return err;    
2537 }
2538 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2539 {
2540     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2541
2542     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2543     {
2544         if (sensor_mirror(client,value) != 0)
2545             SENSOR_TR("%s(%d): sensor_mirror failed, value:0x%x",__FUNCTION__, __LINE__,value);
2546         
2547         SENSOR_DG("%s(%d): sensor_mirror success, value:0x%x",__FUNCTION__, __LINE__,value);
2548         return 0;
2549     }
2550     SENSOR_TR("\n %s..%s value = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2551     return -EINVAL;
2552 }
2553 #endif
2554 #if CONFIG_SENSOR_Flip
2555 static int sensor_flip(struct i2c_client *client, int on)
2556 {
2557     char val;
2558     int err = 0;    
2559
2560     if (on) {
2561         err = sensor_read(client, 0x3820, &val);
2562         if (err == 0) {
2563             val |= 0x06;
2564             err = sensor_write(client, 0x3820, val);
2565         }
2566     } else {
2567         err = sensor_read(client, 0x3820, &val);
2568         if (err == 0) {
2569             val &= 0xf9;
2570             err = sensor_write(client, 0x3820, val);
2571         }
2572     }
2573
2574     return err;    
2575 }
2576 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2577 {
2578     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2579
2580     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2581     {
2582         if (sensor_flip(client,value) != 0)
2583             SENSOR_TR("%s(%d): sensor_flip failed, value:0x%x",__FUNCTION__, __LINE__,value);
2584         
2585         SENSOR_DG("%s(%d): sensor_flip success, value:0x%x",__FUNCTION__, __LINE__,value);
2586         return 0;
2587     }
2588     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2589     return -EINVAL;
2590 }
2591 #endif
2592 #if CONFIG_SENSOR_Scene
2593 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2594 {
2595     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2596
2597     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2598     {
2599         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2600         {
2601             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2602             {
2603                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2604                 return -EINVAL;
2605             }
2606             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2607             return 0;
2608         }
2609     }
2610     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2611     return -EINVAL;
2612 }
2613 #endif
2614 #if CONFIG_SENSOR_WhiteBalance
2615 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2616 {
2617     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2618
2619     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2620     {
2621         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2622         {
2623             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2624             {
2625                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2626                 return -EINVAL;
2627             }
2628             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2629             return 0;
2630         }
2631     }
2632         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2633     return -EINVAL;
2634 }
2635 #endif
2636 #if CONFIG_SENSOR_DigitalZoom
2637 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2638 {
2639     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2640     struct sensor *sensor = to_sensor(client);
2641         const struct v4l2_queryctrl *qctrl_info;
2642     int digitalzoom_cur, digitalzoom_total;
2643
2644         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2645         if (qctrl_info)
2646                 return -EINVAL;
2647
2648     digitalzoom_cur = sensor->info_priv.digitalzoom;
2649     digitalzoom_total = qctrl_info->maximum;
2650
2651     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
2652     {
2653         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2654         return -EINVAL;
2655     }
2656
2657     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2658     {
2659         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2660         return -EINVAL;
2661     }
2662
2663     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
2664     {
2665         value = digitalzoom_total - digitalzoom_cur;
2666     }
2667
2668     if ((value < 0) && ((digitalzoom_cur + value) < 0))
2669     {
2670         value = 0 - digitalzoom_cur;
2671     }
2672
2673     digitalzoom_cur += value;
2674
2675     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2676     {
2677         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2678         {
2679             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2680             return -EINVAL;
2681         }
2682         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2683         return 0;
2684     }
2685
2686     return -EINVAL;
2687 }
2688 #endif
2689 #if CONFIG_SENSOR_Flash
2690 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2691 {    
2692     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2693         if (value == 3) {       /* ddl@rock-chips.com: torch */
2694             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2695         } else {
2696             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2697         }
2698         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2699         return 0;
2700     }
2701     
2702         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2703     return -EINVAL;
2704 }
2705 #endif
2706
2707 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2708 {
2709     struct i2c_client *client = v4l2_get_subdevdata(sd);
2710     struct sensor *sensor = to_sensor(client);
2711     const struct v4l2_queryctrl *qctrl;
2712
2713     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2714
2715     if (!qctrl)
2716     {
2717         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2718         return -EINVAL;
2719     }
2720
2721     switch (ctrl->id)
2722     {
2723         case V4L2_CID_BRIGHTNESS:
2724             {
2725                 ctrl->value = sensor->info_priv.brightness;
2726                 break;
2727             }
2728         case V4L2_CID_SATURATION:
2729             {
2730                 ctrl->value = sensor->info_priv.saturation;
2731                 break;
2732             }
2733         case V4L2_CID_CONTRAST:
2734             {
2735                 ctrl->value = sensor->info_priv.contrast;
2736                 break;
2737             }
2738         case V4L2_CID_DO_WHITE_BALANCE:
2739             {
2740                 ctrl->value = sensor->info_priv.whiteBalance;
2741                 break;
2742             }
2743         case V4L2_CID_EXPOSURE:
2744             {
2745                 ctrl->value = sensor->info_priv.exposure;
2746                 break;
2747             }
2748         case V4L2_CID_HFLIP:
2749             {
2750                 ctrl->value = sensor->info_priv.mirror;
2751                 break;
2752             }
2753         case V4L2_CID_VFLIP:
2754             {
2755                 ctrl->value = sensor->info_priv.flip;
2756                 break;
2757             }
2758         default :
2759                 break;
2760     }
2761     return 0;
2762 }
2763
2764
2765
2766 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2767 {
2768     struct i2c_client *client = v4l2_get_subdevdata(sd);
2769     struct sensor *sensor = to_sensor(client);
2770     struct soc_camera_device *icd = client->dev.platform_data;
2771     const struct v4l2_queryctrl *qctrl;
2772
2773
2774     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2775
2776     if (!qctrl)
2777     {
2778         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2779         return -EINVAL;
2780     }
2781
2782     switch (ctrl->id)
2783     {
2784 #if CONFIG_SENSOR_Brightness
2785         case V4L2_CID_BRIGHTNESS:
2786             {
2787                 if (ctrl->value != sensor->info_priv.brightness)
2788                 {
2789                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2790                     {
2791                         return -EINVAL;
2792                     }
2793                     sensor->info_priv.brightness = ctrl->value;
2794                 }
2795                 break;
2796             }
2797 #endif
2798 #if CONFIG_SENSOR_Exposure
2799         case V4L2_CID_EXPOSURE:
2800             {
2801                 if (ctrl->value != sensor->info_priv.exposure)
2802                 {
2803                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2804                     {
2805                         return -EINVAL;
2806                     }
2807                     sensor->info_priv.exposure = ctrl->value;
2808                 }
2809                 break;
2810             }
2811 #endif
2812 #if CONFIG_SENSOR_Saturation
2813         case V4L2_CID_SATURATION:
2814             {
2815                 if (ctrl->value != sensor->info_priv.saturation)
2816                 {
2817                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2818                     {
2819                         return -EINVAL;
2820                     }
2821                     sensor->info_priv.saturation = ctrl->value;
2822                 }
2823                 break;
2824             }
2825 #endif
2826 #if CONFIG_SENSOR_Contrast
2827         case V4L2_CID_CONTRAST:
2828             {
2829                 if (ctrl->value != sensor->info_priv.contrast)
2830                 {
2831                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2832                     {
2833                         return -EINVAL;
2834                     }
2835                     sensor->info_priv.contrast = ctrl->value;
2836                 }
2837                 break;
2838             }
2839 #endif
2840 #if CONFIG_SENSOR_WhiteBalance
2841         case V4L2_CID_DO_WHITE_BALANCE:
2842             {
2843                 if (ctrl->value != sensor->info_priv.whiteBalance)
2844                 {
2845                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2846                     {
2847                         return -EINVAL;
2848                     }
2849                     sensor->info_priv.whiteBalance = ctrl->value;
2850                 }
2851                 break;
2852             }
2853 #endif
2854 #if CONFIG_SENSOR_Mirror
2855         case V4L2_CID_HFLIP:
2856             {
2857                 if (ctrl->value != sensor->info_priv.mirror)
2858                 {
2859                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2860                         return -EINVAL;
2861                     sensor->info_priv.mirror = ctrl->value;
2862                 }
2863                 break;
2864             }
2865 #endif
2866 #if CONFIG_SENSOR_Flip
2867         case V4L2_CID_VFLIP:
2868             {
2869                 if (ctrl->value != sensor->info_priv.flip)
2870                 {
2871                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2872                         return -EINVAL;
2873                     sensor->info_priv.flip = ctrl->value;
2874                 }
2875                 break;
2876             }
2877 #endif
2878         default:
2879             break;
2880     }
2881
2882     return 0;
2883 }
2884 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2885 {
2886     const struct v4l2_queryctrl *qctrl;
2887     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2888     struct sensor *sensor = to_sensor(client);
2889
2890     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2891
2892     if (!qctrl)
2893     {
2894         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2895         return -EINVAL;
2896     }
2897
2898     switch (ext_ctrl->id)
2899     {
2900         case V4L2_CID_SCENE:
2901             {
2902                 ext_ctrl->value = sensor->info_priv.scene;
2903                 break;
2904             }
2905         case V4L2_CID_EFFECT:
2906             {
2907                 ext_ctrl->value = sensor->info_priv.effect;
2908                 break;
2909             }
2910         case V4L2_CID_ZOOM_ABSOLUTE:
2911             {
2912                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2913                 break;
2914             }
2915         case V4L2_CID_ZOOM_RELATIVE:
2916             {
2917                 return -EINVAL;
2918             }
2919         case V4L2_CID_FOCUS_ABSOLUTE:
2920             {
2921                 ext_ctrl->value = sensor->info_priv.focus;
2922                 break;
2923             }
2924         case V4L2_CID_FOCUS_RELATIVE:
2925             {
2926                 return -EINVAL;
2927             }
2928         case V4L2_CID_FLASH:
2929             {
2930                 ext_ctrl->value = sensor->info_priv.flash;
2931                 break;
2932             }
2933         default :
2934             break;
2935     }
2936     return 0;
2937 }
2938 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2939 {
2940     const struct v4l2_queryctrl *qctrl;
2941     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2942     struct sensor *sensor = to_sensor(client);
2943     int val_offset;
2944
2945     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2946     
2947     if (!qctrl)
2948     {
2949         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2950         return -EINVAL;
2951     }
2952
2953         val_offset = 0;
2954     switch (ext_ctrl->id)
2955     {
2956 #if CONFIG_SENSOR_Scene
2957         case V4L2_CID_SCENE:
2958             {
2959                 if (ext_ctrl->value != sensor->info_priv.scene)
2960                 {
2961                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2962                         return -EINVAL;
2963                     sensor->info_priv.scene = ext_ctrl->value;
2964                 }
2965                 break;
2966             }
2967 #endif
2968 #if CONFIG_SENSOR_Effect
2969         case V4L2_CID_EFFECT:
2970             {
2971                 if (ext_ctrl->value != sensor->info_priv.effect)
2972                 {
2973                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2974                         return -EINVAL;
2975                     sensor->info_priv.effect= ext_ctrl->value;
2976                 }
2977                 break;
2978             }
2979 #endif
2980 #if CONFIG_SENSOR_DigitalZoom
2981         case V4L2_CID_ZOOM_ABSOLUTE:
2982             {
2983                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2984                     return -EINVAL;
2985
2986                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2987                 {
2988                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2989
2990                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2991                         return -EINVAL;
2992                     sensor->info_priv.digitalzoom += val_offset;
2993
2994                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2995                 }
2996
2997                 break;
2998             }
2999         case V4L2_CID_ZOOM_RELATIVE:
3000             {
3001                 if (ext_ctrl->value)
3002                 {
3003                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3004                         return -EINVAL;
3005                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3006
3007                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3008                 }
3009                 break;
3010             }
3011 #endif
3012 #if CONFIG_SENSOR_Focus
3013         case V4L2_CID_FOCUS_ABSOLUTE:
3014             {
3015                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3016                     return -EINVAL;
3017
3018                 if (ext_ctrl->value != sensor->info_priv.focus)
3019                 {
3020                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
3021
3022                     sensor->info_priv.focus += val_offset;
3023                 }
3024
3025                 break;
3026             }
3027         case V4L2_CID_FOCUS_RELATIVE:
3028             {
3029                 if (ext_ctrl->value)
3030                 {
3031                     sensor->info_priv.focus += ext_ctrl->value;
3032
3033                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
3034                 }
3035                 break;
3036             }
3037 #endif
3038 #if CONFIG_SENSOR_Flash
3039         case V4L2_CID_FLASH:
3040             {
3041                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3042                     return -EINVAL;
3043                 sensor->info_priv.flash = ext_ctrl->value;
3044
3045                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3046                 break;
3047             }
3048 #endif
3049         default:
3050             break;
3051     }
3052
3053     return 0;
3054 }
3055
3056 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3057 {
3058     struct i2c_client *client = v4l2_get_subdevdata(sd);
3059     struct soc_camera_device *icd = client->dev.platform_data;
3060     int i, error_cnt=0, error_idx=-1;
3061
3062
3063     for (i=0; i<ext_ctrl->count; i++) {
3064         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3065             error_cnt++;
3066             error_idx = i;
3067         }
3068     }
3069
3070     if (error_cnt > 1)
3071         error_idx = ext_ctrl->count;
3072
3073     if (error_idx != -1) {
3074         ext_ctrl->error_idx = error_idx;
3075         return -EINVAL;
3076     } else {
3077         return 0;
3078     }
3079 }
3080
3081 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3082 {
3083     struct i2c_client *client = v4l2_get_subdevdata(sd);
3084     struct soc_camera_device *icd = client->dev.platform_data;
3085     int i, error_cnt=0, error_idx=-1;
3086
3087
3088     for (i=0; i<ext_ctrl->count; i++) {
3089         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3090             error_cnt++;
3091             error_idx = i;
3092         }
3093     }
3094
3095     if (error_cnt > 1)
3096         error_idx = ext_ctrl->count;
3097
3098     if (error_idx != -1) {
3099         ext_ctrl->error_idx = error_idx;
3100         return -EINVAL;
3101     } else {
3102         return 0;
3103     }
3104 }
3105
3106 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3107  * this wasn't our capture interface, so, we wait for the right one */
3108 static int sensor_video_probe(struct soc_camera_device *icd,
3109                                struct i2c_client *client)
3110 {
3111     char value;
3112     int ret,pid = 0;
3113     struct sensor *sensor = to_sensor(client);
3114
3115     /* We must have a parent by now. And it cannot be a wrong one.
3116      * So this entire test is completely redundant. */
3117     if (!icd->dev.parent ||
3118             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3119                 return -ENODEV;
3120
3121         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3122         SENSOR_TR("power down %s failed\n",SENSOR_NAME_STRING());
3123                 ret = -ENODEV;
3124                 goto sensor_video_probe_err;
3125         }
3126
3127     /* soft reset */
3128     ret = sensor_write(client, 0x0103, 0x01);
3129     if (ret != 0) {
3130         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3131         ret = -ENODEV;
3132                 goto sensor_video_probe_err;
3133         }
3134     mdelay(5);          //delay 5 microseconds
3135
3136     /* check if it is an sensor sensor */
3137     ret = sensor_read(client, 0x300a, &value);
3138     if (ret != 0) {
3139         SENSOR_TR("read chip id high byte failed\n");
3140         ret = -ENODEV;
3141         goto sensor_video_probe_err;
3142     }
3143
3144     pid |= (value << 8);
3145
3146     ret = sensor_read(client, 0x300b, &value);
3147     if (ret != 0) {
3148         SENSOR_TR("read chip id low byte failed\n");
3149         ret = -ENODEV;
3150         goto sensor_video_probe_err;
3151     }
3152
3153     pid |= (value & 0xff);
3154     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3155     if (pid == SENSOR_ID) {
3156         sensor->model = SENSOR_V4L2_IDENT;
3157     } else {
3158         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3159         ret = -ENODEV;
3160         goto sensor_video_probe_err;
3161     }
3162
3163     return 0;
3164
3165 sensor_video_probe_err:
3166
3167     return ret;
3168 }
3169
3170 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3171 {
3172         struct i2c_client *client = v4l2_get_subdevdata(sd);
3173     struct soc_camera_device *icd = client->dev.platform_data;
3174     struct sensor *sensor = to_sensor(client);
3175     int ret = 0;
3176 #if CONFIG_SENSOR_Flash 
3177     int i;
3178 #endif
3179     
3180         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3181         switch (cmd)
3182         {
3183                 case RK29_CAM_SUBDEV_DEACTIVATE:
3184                 {
3185                         sensor_deactivate(client);
3186                         break;
3187                 }
3188
3189                 case RK29_CAM_SUBDEV_IOREQUEST:
3190                 {
3191                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
3192             if (sensor->sensor_io_request != NULL) { 
3193                         int j = 0;
3194                         for(j = 0;j < RK_CAM_NUM;j++){
3195                         if (sensor->sensor_io_request->gpio_res[j].dev_name && 
3196                             (strcmp(sensor->sensor_io_request->gpio_res[j].dev_name, dev_name(icd->pdev)) == 0)) {
3197                             sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[j];
3198                                 break;
3199                           } 
3200                         }
3201                         if(j == RK_CAM_NUM){
3202                                 SENSOR_TR("%s %s RK_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3203                                 ret = -EINVAL;
3204                                 goto sensor_ioctl_end;
3205                                 }
3206                 }
3207                         
3208             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
3209                for this project */
3210             #if CONFIG_SENSOR_Flash     
3211                 if (sensor->sensor_gpio_res) { 
3212                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3213                     for (i = 0; i < icd->ops->num_controls; i++) {
3214                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3215                                         //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));  
3216                               sensor_controls[i].id=0xffff;                             
3217                                 }
3218                     }
3219                     sensor->info_priv.flash = 0xff;
3220                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3221                 }else{ //two cameras are the same,need to deal diffrently ,zyc
3222                     for (i = 0; i < icd->ops->num_controls; i++) {
3223                            if(0xffff == icd->ops->controls[i].id){
3224                               sensor_controls[i].id=V4L2_CID_FLASH;
3225                            }               
3226                     }
3227                 }
3228                 }
3229             #endif
3230                         break;
3231                 }
3232                 default:
3233                 {
3234                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3235                         break;
3236                 }
3237         }
3238 sensor_ioctl_end:
3239         return ret;
3240
3241 }
3242 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3243                             enum v4l2_mbus_pixelcode *code)
3244 {
3245         if (index >= ARRAY_SIZE(sensor_colour_fmts))
3246                 return -EINVAL;
3247
3248         *code = sensor_colour_fmts[index].code;
3249         return 0;
3250 }
3251 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3252         .init           = sensor_init,
3253         .g_ctrl         = sensor_g_control,
3254         .s_ctrl         = sensor_s_control,
3255         .g_ext_ctrls          = sensor_g_ext_controls,
3256         .s_ext_ctrls          = sensor_s_ext_controls,
3257         .g_chip_ident   = sensor_g_chip_ident,
3258         .ioctl = sensor_ioctl,  
3259 };
3260
3261 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3262         .s_mbus_fmt     = sensor_s_fmt,
3263         .g_mbus_fmt     = sensor_g_fmt,
3264         .try_mbus_fmt   = sensor_try_fmt,
3265         .enum_mbus_fmt  = sensor_enum_fmt,
3266 };
3267 static struct v4l2_subdev_ops sensor_subdev_ops = {
3268         .core   = &sensor_subdev_core_ops,
3269         .video = &sensor_subdev_video_ops,
3270 };
3271
3272 static int sensor_probe(struct i2c_client *client,
3273                          const struct i2c_device_id *did)
3274 {
3275     struct sensor *sensor;
3276     struct soc_camera_device *icd = client->dev.platform_data;
3277     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3278     struct soc_camera_link *icl;
3279     int ret;
3280
3281     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3282     if (!icd) {
3283         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3284         return -EINVAL;
3285     }
3286
3287     icl = to_soc_camera_link(icd);
3288     if (!icl) {
3289         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3290         return -EINVAL;
3291     }
3292
3293     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3294         dev_warn(&adapter->dev,
3295                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3296         return -EIO;
3297     }
3298
3299     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3300     if (!sensor)
3301         return -ENOMEM;
3302
3303     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3304
3305     /* Second stage probe - when a capture adapter is there */
3306     icd->ops            = &sensor_ops;
3307
3308     sensor->info_priv.fmt = sensor_colour_fmts[0];
3309     
3310         #if CONFIG_SENSOR_I2C_NOSCHED
3311         atomic_set(&sensor->tasklock_cnt,0);
3312         #endif
3313
3314     ret = sensor_video_probe(icd, client);
3315     if (ret < 0) {
3316         icd->ops = NULL;
3317         i2c_set_clientdata(client, NULL);
3318         kfree(sensor);
3319                 sensor = NULL;
3320     }
3321         hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3322     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3323     return ret;
3324 }
3325
3326 static int sensor_remove(struct i2c_client *client)
3327 {
3328     struct sensor *sensor = to_sensor(client);
3329     struct soc_camera_device *icd = client->dev.platform_data;
3330
3331     icd->ops = NULL;
3332     i2c_set_clientdata(client, NULL);
3333     client->driver = NULL;
3334     kfree(sensor);
3335         sensor = NULL;
3336     return 0;
3337 }
3338
3339 static const struct i2c_device_id sensor_id[] = {
3340         {SENSOR_NAME_STRING(), 0 },
3341         { }
3342 };
3343 MODULE_DEVICE_TABLE(i2c, sensor_id);
3344
3345 static struct i2c_driver sensor_i2c_driver = {
3346         .driver = {
3347                 .name = SENSOR_NAME_STRING(),
3348         },
3349         .probe          = sensor_probe,
3350         .remove         = sensor_remove,
3351         .id_table       = sensor_id,
3352 };
3353
3354 static int __init sensor_mod_init(void)
3355 {
3356     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3357     return i2c_add_driver(&sensor_i2c_driver);
3358 }
3359
3360 static void __exit sensor_mod_exit(void)
3361 {
3362     i2c_del_driver(&sensor_i2c_driver);
3363 }
3364
3365 device_initcall_sync(sensor_mod_init);
3366 module_exit(sensor_mod_exit);
3367
3368 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3369 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3370 MODULE_LICENSE("GPL");
3371
3372