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