3 o* Driver for MT9M001 CMOS Image Sensor from Micron
5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/videodev2.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/log2.h>
16 #include <linux/platform_device.h>
17 #include <linux/delay.h>
18 #include <linux/circ_buf.h>
19 #include <linux/miscdevice.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-chip-ident.h>
22 #include <media/soc_camera.h>
23 #include <plat/rk_camera.h>
26 module_param(debug, int, S_IRUGO|S_IWUSR);
28 #define dprintk(level, fmt, arg...) do { \
30 printk(KERN_WARNING fmt , ## arg); } while (0)
32 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
33 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
36 #define _CONS(a,b) a##b
37 #define CONS(a,b) _CONS(a,b)
40 #define _STR(x) __STR(x)
41 #define STR(x) _STR(x)
43 #define MIN(x,y) ((x<y) ? x: y)
44 #define MAX(x,y) ((x>y) ? x: y)
46 /* Sensor Driver Configuration */
47 #define SENSOR_NAME RK29_CAM_SENSOR_GC0307
48 #define SENSOR_V4L2_IDENT V4L2_IDENT_GC0307
49 #define SENSOR_ID 0x99
50 #define SENSOR_MIN_WIDTH 640//176
51 #define SENSOR_MIN_HEIGHT 480//144
52 #define SENSOR_MAX_WIDTH 800//1600
53 #define SENSOR_MAX_HEIGHT 600//1200
54 #define SENSOR_INIT_WIDTH 640 /* Sensor pixel size for sensor_init_data array */
55 #define SENSOR_INIT_HEIGHT 480
56 #define SENSOR_INIT_WINSEQADR sensor_vga
57 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_YUYV8_2X8
59 #define CONFIG_SENSOR_WhiteBalance 1
60 #define CONFIG_SENSOR_Brightness 0
61 #define CONFIG_SENSOR_Contrast 0
62 #define CONFIG_SENSOR_Saturation 0
63 #define CONFIG_SENSOR_Effect 1
64 #define CONFIG_SENSOR_Scene 0
65 #define CONFIG_SENSOR_DigitalZoom 0
66 #define CONFIG_SENSOR_Focus 0
67 #define CONFIG_SENSOR_Exposure 0
68 #define CONFIG_SENSOR_Flash 1
69 #define CONFIG_SENSOR_Mirror 0
70 #define CONFIG_SENSOR_Flip 0
72 #define CONFIG_SENSOR_I2C_SPEED 100000//250000 /* Hz */
73 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
74 #define CONFIG_SENSOR_I2C_NOSCHED 0
75 #define CONFIG_SENSOR_I2C_RDWRCHK 0
77 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
78 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
79 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
81 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
82 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
83 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
84 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
85 #define COLOR_TEMPERATURE_OFFICE_DN 3500
86 #define COLOR_TEMPERATURE_OFFICE_UP 5000
87 #define COLOR_TEMPERATURE_HOME_DN 2500
88 #define COLOR_TEMPERATURE_HOME_UP 3500
90 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
91 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
93 #define SENSOR_AF_IS_ERR (0x00<<0)
94 #define SENSOR_AF_IS_OK (0x01<<0)
95 #define SENSOR_INIT_IS_ERR (0x00<<28)
96 #define SENSOR_INIT_IS_OK (0x01<<28)
103 //flash off in fixed time to prevent from too hot , zyc
105 struct soc_camera_device *icd;
106 struct hrtimer timer;
108 static enum hrtimer_restart flash_off_func(struct hrtimer *timer);
110 static struct flash_timer flash_off_timer;
111 //for user defined if user want to customize the series , zyc
112 #ifdef CONFIG_GC0307_USER_DEFINED_SERIES
113 #include "gc0307_user_series.c"
115 /* init 640X480 VGA */
116 static struct reginfo sensor_init_data[] =
118 //========= close output
122 //========= close some functions
123 // open them after configure their parmameters
127 {0x47 ,0x00}, //mode1,
128 {0x48 ,0xc3}, //mode2,
129 {0x49 ,0x00}, //dither_mode
130 {0x4a ,0x00}, //clock_gating_en
131 {0x4b ,0x00}, //mode_reg3
132 {0x4E ,0x22},//0x23}, //sync mode yaowei
133 {0x4F ,0x01}, //AWB, AEC, every N frame
135 //========= frame timing
138 {0x1C ,0x00}, //Vs_st
139 {0x1D ,0x00}, //Vs_et
140 {0x10 ,0x00}, //high 4 bits of VB, HB
141 {0x11 ,0x05}, //row_tail, AD_pipe_number
147 //========= windowing
148 {0x05 ,0x00}, //row_start
150 {0x07 ,0x00}, //col start
152 {0x09 ,0x01}, //win height
154 {0x0B ,0x02}, //win width, pixel array only 640
158 {0x0D ,0x22}, //rsh_width
160 {0x0E ,0x02}, //CISCTL mode2,
163 {0x12 ,0x70}, //7 hrst, 6_4 darsg,
164 {0x13 ,0x00}, //7 CISCTL_restart, 0 apwd
166 {0x15 ,0xba}, //7_4 vref
167 {0x16 ,0x13}, //5to4 _coln_r, __1to0__da18
168 {0x17 ,0x52}, //opa_r, ref_r, sRef_r
169 //{0x18 ,0xc0}, //analog_mode, best case for left band.
171 {0x1E ,0x0d}, //tsp_width
172 {0x1F ,0x32}, //sh_delay
175 {0x47 ,0x00}, //7__test_image, __6__fixed_pga, __5__auto_DN, __4__CbCr_fix,
176 //__3to2__dark_sequence, __1__allow_pclk_vcync, __0__LSC_test_image
177 {0x19 ,0x06}, //pga_o
178 {0x1a ,0x06}, //pga_e
180 {0x31 ,0x00}, //4 //pga_oFFset , high 8bits of 11bits
181 {0x3B ,0x00}, //global_oFFset, low 8bits of 11bits
183 {0x59 ,0x0f}, //offset_mode
184 {0x58 ,0x88}, //DARK_VALUE_RATIO_G, DARK_VALUE_RATIO_RB
185 {0x57 ,0x08}, //DARK_CURRENT_RATE
186 {0x56 ,0x77}, //PGA_OFFSET_EVEN_RATIO, PGA_OFFSET_ODD_RATIO
189 {0x35 ,0xd8}, //blk_mode
205 {0x7e ,0x50},//0x45 ylz++
212 //========= manual_gain
213 {0x61 ,0x80}, //manual_gain_g1
214 {0x63 ,0x80}, //manual_gain_r
215 {0x65 ,0x98}, //manual_gai_b, 0xa0=1.25, 0x98=1.1875
216 {0x67 ,0x80}, //manual_gain_g2
217 {0x68 ,0x18}, //global_manual_gain 2.4bits
239 {0x7d ,0x2f}, //dn_mode
240 {0x80 ,0x0c}, //when auto_dn, check 7e,7f
245 {0x83 ,0x18}, //DD_TH1
246 {0x84 ,0x18}, //DD_TH2
247 {0x85 ,0x04}, //DD_TH3
248 {0x87 ,0x34}, //32 b DNDD_low_range X16, DNDD_low_range_C_weight_center
271 {0x77 ,0x80}, //contrast_center
272 {0x78 ,0x00}, //fixed_Cb
273 {0x79 ,0x00}, //fixed_Cr
274 {0x7a ,0x00}, //luma_offset
275 {0x7b ,0x40}, //hue_cos
276 {0x7c ,0x00}, //hue_sin
279 {0xa0 ,0x40}, //global_saturation
280 {0xa1 ,0x42}, //luma_contrast
281 {0xa2 ,0x40}, //saturation_Cb //ylz 34
282 {0xa3 ,0x34}, //saturation_Cr
317 {0xc4 ,0x1c}, //18//20
325 {0xcd ,0x80}, //R_ratio
326 {0xce ,0x80}, //G_ratio , cold_white white
327 {0xcf ,0x80}, //B_ratio
339 {0x28 ,0x02}, //AEC_exp_level_1bit11to8
340 {0x29 ,0x58}, //AEC_exp_level_1bit7to0
341 {0x2a ,0x03}, //AEC_exp_level_2bit11to8
342 {0x2b ,0x84}, //AEC_exp_level_2bit7to0
343 {0x2c ,0x09}, //AEC_exp_level_3bit11to8 659 - 8FPS, 8ca - 6FPS //
344 {0x2d ,0x60}, //AEC_exp_level_3bit7to0
345 {0x2e ,0x0a}, //AEC_exp_level_4bit11to8 4FPS
346 {0x2f ,0x8c}, //AEC_exp_level_4bit7to0
356 {0xd1 ,0x40}, //AEC_target_Y
359 {0xd5 ,0x01}, // william 0318
360 {0xd6 ,0x96}, //antiflicker_step
361 {0xd7 ,0x03}, //AEC_exp_time_min ,william 20090312
366 //========= measure window
377 //========= close_frame
378 {0xed ,0x00}, //close_frame_num1 ,can be use to reduce FPS
379 {0xee ,0x00}, //close_frame_num2
380 {0xef ,0x00}, //close_frame_num
383 {0xf0 ,0x01}, //select page1
417 //=============================lscP
442 //============================= ccP
444 {0x69 ,0x03}, //cc_mode
463 //============================= AGP
489 //about gamma1 is hex r oct
508 //========= open functions
509 {0xf0 ,0x00}, //set back to page0
513 ///// Çë×¢Ò⣬µ÷ÕûGC0307µÄ¾µÏñºÍ·×ª£¬ÐèҪͬʱÐÞ¸ÄÈý¸ö¼Ä´æÆ÷£¬ÈçÏÂ:
519 { 0x01 ,0xfa}, // 24M
537 ///// ËÄÖÖ²»Í¬µÄ·×ªºÍ¾µÏñÉ趨£¬¿Í»§¿ÉÖ±½Ó¸´ÖÆ!!!!!!
556 // IMAGE_HV_MIRROR: // 180
566 static struct reginfo sensor_720p[]=
572 static struct reginfo sensor_sxga[] =
579 static struct reginfo sensor_svga[] =
587 static struct reginfo sensor_vga[] =
593 { 0x08 , 0x00},//0x10 james 20100715
597 { 0x0c , 0x88},//0x80 james 20100715
598 { 0x45 , 0x24}, // bit[7:2]=001001
599 { 0x48 , 0x84}, // bit[7]=1
624 static struct reginfo sensor_cif[] =
631 static struct reginfo sensor_qvga[] =
638 static struct reginfo sensor_qcif[] =
644 static struct reginfo sensor_ClrFmt_YUYV[]=
650 static struct reginfo sensor_ClrFmt_UYVY[]=
656 #if CONFIG_SENSOR_WhiteBalance
657 static struct reginfo sensor_WhiteB_Auto[]=
659 {0xc7,0x4c}, //for AWB can adjust back
668 /* Cloudy Colour Temperature : 6500K - 8000K */
669 static struct reginfo sensor_WhiteB_Cloudy[]=
671 {0x41,0x2b}, // Enable AWB
672 {0xc7,0x5a}, //WB_manual_gain
679 /* ClearDay Colour Temperature : 5000K - 6500K */
680 static struct reginfo sensor_WhiteB_ClearDay[]=
683 {0x41,0x2b}, // Enable AWB
691 /* Office Colour Temperature : 3500K - 5000K */
692 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
695 {0x41,0x2b}, // Enable AWB
705 /* Home Colour Temperature : 2500K - 3500K */
706 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
709 {0x41,0x2b}, // Enable AWB
717 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
718 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
722 #if CONFIG_SENSOR_Brightness
723 static struct reginfo sensor_Brightness0[]=
731 static struct reginfo sensor_Brightness1[]=
738 static struct reginfo sensor_Brightness2[]=
745 static struct reginfo sensor_Brightness3[]=
752 static struct reginfo sensor_Brightness4[]=
759 static struct reginfo sensor_Brightness5[]=
765 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
766 sensor_Brightness4, sensor_Brightness5,NULL,
771 #if CONFIG_SENSOR_Effect
772 static struct reginfo sensor_Effect_Normal[] =
794 static struct reginfo sensor_Effect_WandB[] =
796 {0x41,0x2f}, // danse
815 static struct reginfo sensor_Effect_Sepia[] =
839 static struct reginfo sensor_Effect_Negative[] =
861 static struct reginfo sensor_Effect_Bluish[] =
884 static struct reginfo sensor_Effect_Green[] =
906 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
907 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
910 #if CONFIG_SENSOR_Exposure
911 static struct reginfo sensor_Exposure0[]=
917 static struct reginfo sensor_Exposure1[]=
923 static struct reginfo sensor_Exposure2[]=
929 static struct reginfo sensor_Exposure3[]=
935 static struct reginfo sensor_Exposure4[]=
941 static struct reginfo sensor_Exposure5[]=
947 static struct reginfo sensor_Exposure6[]=
953 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
954 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
957 #if CONFIG_SENSOR_Saturation
958 static struct reginfo sensor_Saturation0[]=
963 static struct reginfo sensor_Saturation1[]=
968 static struct reginfo sensor_Saturation2[]=
972 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
975 #if CONFIG_SENSOR_Contrast
976 static struct reginfo sensor_Contrast0[]=
981 static struct reginfo sensor_Contrast1[]=
986 static struct reginfo sensor_Contrast2[]=
991 static struct reginfo sensor_Contrast3[]=
996 static struct reginfo sensor_Contrast4[]=
1002 static struct reginfo sensor_Contrast5[]=
1007 static struct reginfo sensor_Contrast6[]=
1011 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1012 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1016 #if CONFIG_SENSOR_Mirror
1017 static struct reginfo sensor_MirrorOn[]=
1022 static struct reginfo sensor_MirrorOff[]=
1026 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1028 #if CONFIG_SENSOR_Flip
1029 static struct reginfo sensor_FlipOn[]=
1034 static struct reginfo sensor_FlipOff[]=
1038 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1041 #if CONFIG_SENSOR_Scene
1042 static struct reginfo sensor_SceneAuto[] =
1044 { 0xdd ,0x22}, //0x12
1051 static struct reginfo sensor_SceneNight[] =
1060 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1063 #if CONFIG_SENSOR_DigitalZoom
1064 static struct reginfo sensor_Zoom0[] =
1070 static struct reginfo sensor_Zoom1[] =
1076 static struct reginfo sensor_Zoom2[] =
1083 static struct reginfo sensor_Zoom3[] =
1088 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1090 static const struct v4l2_querymenu sensor_menus[] =
1092 #if CONFIG_SENSOR_WhiteBalance
1093 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1094 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1095 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1098 #if CONFIG_SENSOR_Effect
1099 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1100 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1101 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1104 #if CONFIG_SENSOR_Scene
1105 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1108 #if CONFIG_SENSOR_Flash
1109 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1110 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1114 static struct v4l2_queryctrl sensor_controls[] =
1116 #if CONFIG_SENSOR_WhiteBalance
1118 .id = V4L2_CID_DO_WHITE_BALANCE,
1119 .type = V4L2_CTRL_TYPE_MENU,
1120 .name = "White Balance Control",
1128 #if CONFIG_SENSOR_Brightness
1130 .id = V4L2_CID_BRIGHTNESS,
1131 .type = V4L2_CTRL_TYPE_INTEGER,
1132 .name = "Brightness Control",
1140 #if CONFIG_SENSOR_Effect
1142 .id = V4L2_CID_EFFECT,
1143 .type = V4L2_CTRL_TYPE_MENU,
1144 .name = "Effect Control",
1152 #if CONFIG_SENSOR_Exposure
1154 .id = V4L2_CID_EXPOSURE,
1155 .type = V4L2_CTRL_TYPE_INTEGER,
1156 .name = "Exposure Control",
1164 #if CONFIG_SENSOR_Saturation
1166 .id = V4L2_CID_SATURATION,
1167 .type = V4L2_CTRL_TYPE_INTEGER,
1168 .name = "Saturation Control",
1176 #if CONFIG_SENSOR_Contrast
1178 .id = V4L2_CID_CONTRAST,
1179 .type = V4L2_CTRL_TYPE_INTEGER,
1180 .name = "Contrast Control",
1188 #if CONFIG_SENSOR_Mirror
1190 .id = V4L2_CID_HFLIP,
1191 .type = V4L2_CTRL_TYPE_BOOLEAN,
1192 .name = "Mirror Control",
1200 #if CONFIG_SENSOR_Flip
1202 .id = V4L2_CID_VFLIP,
1203 .type = V4L2_CTRL_TYPE_BOOLEAN,
1204 .name = "Flip Control",
1212 #if CONFIG_SENSOR_Scene
1214 .id = V4L2_CID_SCENE,
1215 .type = V4L2_CTRL_TYPE_MENU,
1216 .name = "Scene Control",
1224 #if CONFIG_SENSOR_DigitalZoom
1226 .id = V4L2_CID_ZOOM_RELATIVE,
1227 .type = V4L2_CTRL_TYPE_INTEGER,
1228 .name = "DigitalZoom Control",
1234 .id = V4L2_CID_ZOOM_ABSOLUTE,
1235 .type = V4L2_CTRL_TYPE_INTEGER,
1236 .name = "DigitalZoom Control",
1244 #if CONFIG_SENSOR_Focus
1246 .id = V4L2_CID_FOCUS_RELATIVE,
1247 .type = V4L2_CTRL_TYPE_INTEGER,
1248 .name = "Focus Control",
1254 .id = V4L2_CID_FOCUS_ABSOLUTE,
1255 .type = V4L2_CTRL_TYPE_INTEGER,
1256 .name = "Focus Control",
1260 .default_value = 125,
1264 #if CONFIG_SENSOR_Flash
1266 .id = V4L2_CID_FLASH,
1267 .type = V4L2_CTRL_TYPE_MENU,
1268 .name = "Flash Control",
1277 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1278 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1279 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1280 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1281 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1282 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1283 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1284 static int sensor_resume(struct soc_camera_device *icd);
1285 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1286 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1287 #if CONFIG_SENSOR_Effect
1288 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1290 #if CONFIG_SENSOR_WhiteBalance
1291 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1293 static int sensor_deactivate(struct i2c_client *client);
1295 static struct soc_camera_ops sensor_ops =
1297 .suspend = sensor_suspend,
1298 .resume = sensor_resume,
1299 .set_bus_param = sensor_set_bus_param,
1300 .query_bus_param = sensor_query_bus_param,
1301 .controls = sensor_controls,
1302 .menus = sensor_menus,
1303 .num_controls = ARRAY_SIZE(sensor_controls),
1304 .num_menus = ARRAY_SIZE(sensor_menus),
1307 /* only one fixed colorspace per pixelcode */
1308 struct sensor_datafmt {
1309 enum v4l2_mbus_pixelcode code;
1310 enum v4l2_colorspace colorspace;
1313 /* Find a data format by a pixel code in an array */
1314 static const struct sensor_datafmt *sensor_find_datafmt(
1315 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1319 for (i = 0; i < n; i++)
1320 if (fmt[i].code == code)
1326 static const struct sensor_datafmt sensor_colour_fmts[] = {
1327 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG},
1328 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG}
1330 typedef struct sensor_info_priv_s
1344 unsigned char mirror; /* HFLIP */
1345 unsigned char flip; /* VFLIP */
1346 unsigned int winseqe_cur_addr;
1347 struct sensor_datafmt fmt;
1348 unsigned int funmodule_state;
1349 } sensor_info_priv_t;
1353 struct v4l2_subdev subdev;
1354 struct i2c_client *client;
1355 sensor_info_priv_t info_priv;
1356 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1357 #if CONFIG_SENSOR_I2C_NOSCHED
1358 atomic_t tasklock_cnt;
1360 struct rk29camera_platform_data *sensor_io_request;
1361 struct rk29camera_gpio_res *sensor_gpio_res;
1364 static struct sensor* to_sensor(const struct i2c_client *client)
1366 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1369 static int sensor_task_lock(struct i2c_client *client, int lock)
1371 #if CONFIG_SENSOR_I2C_NOSCHED
1373 struct sensor *sensor = to_sensor(client);
1376 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1377 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1378 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1382 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1383 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1384 goto sensor_task_lock_err;
1389 atomic_add(1, &sensor->tasklock_cnt);
1391 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1392 atomic_sub(1, &sensor->tasklock_cnt);
1394 if (atomic_read(&sensor->tasklock_cnt) == 0)
1399 sensor_task_lock_err:
1407 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val);
1409 /* sensor register write */
1410 static int sensor_write_internal(struct i2c_client *client, u8 reg, u8 val)
1414 struct i2c_msg msg[1];
1416 buf[0] = reg & 0xFF;
1419 msg->addr = client->addr;
1420 msg->flags = client->flags;
1422 msg->len = sizeof(buf);
1423 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1424 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1429 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1430 err = i2c_transfer(client->adapter, msg, 1);
1435 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1444 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1448 for(cnt=0; cnt<1; cnt++)
1451 ret=sensor_write_internal(client, reg, val);
1459 printk("sensor_write() : reg %02x 0x%02x\n", reg, val);
1460 sensor_write_internal(client, reg, val);
1462 sensor_read(client, reg, &val_tmp);
1465 printk("sensor_write() error: reg %02x 0x%02x != 0x%02x \n", reg, val, val_tmp);
1469 while(val_tmp != val);
1474 /* sensor register read */
1475 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1480 struct i2c_msg msg[2];
1482 //buf[0] = reg >> 8;
1484 // buf[1] = reg & 0xFF;
1486 msg[0].addr = client->addr;
1487 msg[0].flags = client->flags;
1489 msg[0].len = sizeof(buf);
1490 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1491 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1493 msg[1].addr = client->addr;
1494 msg[1].flags = client->flags|I2C_M_RD;
1497 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1498 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1502 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1503 err = i2c_transfer(client->adapter, msg, 2);
1509 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1517 /* write a array of registers */
1519 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1524 for(i=0; regarray[i].reg!=0xff;i++)
1526 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1529 SENSOR_TR("%s..write failed current i = %d\n", SENSOR_NAME_STRING(),i);
1536 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1541 while (regarray[i].reg != 0)
1543 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1546 SENSOR_TR("%s..write failed current i = %d\n", SENSOR_NAME_STRING(),i);
1549 err = sensor_read(client, regarray[i].reg, &val_read);
1550 SENSOR_TR("%s..reg[0x%x]=0x%x,0x%x\n", SENSOR_NAME_STRING(),regarray[i].reg, val_read, regarray[i].val);
1558 static int sensor_check_array(struct i2c_client *client, struct reginfo *regarray)
1565 SENSOR_DG("%s >>>>>>>>>>>>>>>>>>>>>>\n",__FUNCTION__);
1566 for(i=0;i<sizeof(sensor_init_data) / 2;i++)
1568 ret = sensor_read(client,regarray[i].reg,&value);
1571 SENSOR_TR("read value failed\n");
1574 if(regarray[i].val != value)
1576 SENSOR_DG("%s reg[0x%x] check err,writte :0x%x read:0x%x\n",__FUNCTION__,regarray[i].reg,regarray[i].val,value);
1584 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1586 struct soc_camera_link *icl = to_soc_camera_link(icd);
1589 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1592 case Sensor_PowerDown:
1594 if (icl->powerdown) {
1595 ret = icl->powerdown(icd->pdev, on);
1596 if (ret == RK29_CAM_IO_SUCCESS) {
1600 icl->reset(icd->pdev);
1602 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1604 goto sensor_power_end;
1611 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1612 struct sensor *sensor = to_sensor(client);
1614 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1615 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1617 //flash off after 2 secs
1618 hrtimer_cancel(&(flash_off_timer.timer));
1619 hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
1626 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1634 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
1635 struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
1636 sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
1637 SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
1641 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1643 struct i2c_client *client = v4l2_get_subdevdata(sd);;
1644 struct soc_camera_device *icd = client->dev.platform_data;
1645 struct sensor *sensor = to_sensor(client);
1646 const struct v4l2_queryctrl *qctrl;
1647 const struct sensor_datafmt *fmt;
1651 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1653 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1655 goto sensor_INIT_ERR;
1659 if (sensor_task_lock(client,1)<0)
1660 goto sensor_INIT_ERR;
1661 /* ret = sensor_write(client, 0x12, 0x80);
1664 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1666 goto sensor_INIT_ERR;
1669 mdelay(5); */ //delay 5 microseconds
1671 ret = sensor_write_array(client, sensor_init_data);
1674 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1675 goto sensor_INIT_ERR;
1681 printk("****************** check init data\n");
1682 for(i=0; sensor_init_data[i].reg!=0xff; i++)
1684 sensor_read(client, sensor_init_data[i].reg, &val);
1685 printk("reg 0x%02x: org=0x%02x, val=0x%02x, %s\n",
1686 sensor_init_data[i].reg,
1687 sensor_init_data[i].val,
1689 sensor_init_data[i].val==val?"O":"X");
1691 printk("**********************************\n");
1695 sensor_task_lock(client,0);
1696 //icd->user_width = SENSOR_INIT_WIDTH;
1697 //icd->user_height = SENSOR_INIT_HEIGHT;
1698 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1699 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1701 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1703 goto sensor_INIT_ERR;
1705 sensor->info_priv.fmt = *fmt;
1707 /* sensor sensor information for initialization */
1708 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1710 sensor->info_priv.whiteBalance = qctrl->default_value;
1711 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1713 sensor->info_priv.brightness = qctrl->default_value;
1714 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1716 sensor->info_priv.effect = qctrl->default_value;
1717 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1719 sensor->info_priv.exposure = qctrl->default_value;
1721 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1723 sensor->info_priv.saturation = qctrl->default_value;
1724 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1726 sensor->info_priv.contrast = qctrl->default_value;
1727 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1729 sensor->info_priv.mirror = qctrl->default_value;
1730 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1732 sensor->info_priv.flip = qctrl->default_value;
1733 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1735 sensor->info_priv.scene = qctrl->default_value;
1736 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1738 sensor->info_priv.digitalzoom = qctrl->default_value;
1740 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1741 #if CONFIG_SENSOR_Focus
1743 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1745 sensor->info_priv.focus = qctrl->default_value;
1748 #if CONFIG_SENSOR_Flash
1749 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1751 sensor->info_priv.flash = qctrl->default_value;
1752 flash_off_timer.icd = icd;
1753 flash_off_timer.timer.function = flash_off_func;
1756 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);
1757 sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
1760 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1761 sensor_task_lock(client,0);
1762 sensor_deactivate(client);
1766 static int sensor_deactivate(struct i2c_client *client)
1768 struct soc_camera_device *icd = client->dev.platform_data;
1770 struct sensor *sensor = to_sensor(client);
1771 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1773 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1774 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
1775 sensor_task_lock(client, 1);
1776 sensor_task_lock(client, 0);
1778 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1781 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1782 icd->user_width = SENSOR_INIT_WIDTH;
1783 icd->user_height = SENSOR_INIT_HEIGHT;
1784 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1788 static struct reginfo sensor_power_down_sequence[]=
1792 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1795 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1797 if (pm_msg.event == PM_EVENT_SUSPEND) {
1798 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1799 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1801 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1804 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1806 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1811 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1817 static int sensor_resume(struct soc_camera_device *icd)
1821 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1823 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1827 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1833 static int sensor_set_bus_param(struct soc_camera_device *icd,
1834 unsigned long flags)
1840 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1842 struct soc_camera_link *icl = to_soc_camera_link(icd);
1843 unsigned long flags = SENSOR_BUS_PARAM;
1845 return soc_camera_apply_sensor_flags(icl, flags);
1848 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1850 struct i2c_client *client = v4l2_get_subdevdata(sd);
1851 struct soc_camera_device *icd = client->dev.platform_data;
1852 struct sensor *sensor = to_sensor(client);
1854 mf->width = icd->user_width;
1855 mf->height = icd->user_height;
1856 mf->code = sensor->info_priv.fmt.code;
1857 mf->colorspace = sensor->info_priv.fmt.colorspace;
1858 mf->field = V4L2_FIELD_NONE;
1862 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1866 if ((mf->width == 1024) && (mf->height == 768)) {
1868 } else if ((mf->width == 1280) && (mf->height == 1024)) {
1870 } else if ((mf->width == 1600) && (mf->height == 1200)) {
1872 } else if ((mf->width == 2048) && (mf->height == 1536)) {
1874 } else if ((mf->width == 2592) && (mf->height == 1944)) {
1879 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1883 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1887 if ((mf->width == 1280) && (mf->height == 720)) {
1889 } else if ((mf->width == 1920) && (mf->height == 1080)) {
1894 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1897 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1899 struct i2c_client *client = v4l2_get_subdevdata(sd);
1900 const struct sensor_datafmt *fmt;
1901 struct sensor *sensor = to_sensor(client);
1902 const struct v4l2_queryctrl *qctrl;
1903 struct soc_camera_device *icd = client->dev.platform_data;
1904 struct reginfo *winseqe_set_addr=NULL;
1905 int ret=0, set_w,set_h;
1907 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1908 ARRAY_SIZE(sensor_colour_fmts));
1911 goto sensor_s_fmt_end;
1914 if (sensor->info_priv.fmt.code != mf->code) {
1917 case V4L2_MBUS_FMT_YUYV8_2X8:
1919 winseqe_set_addr = sensor_ClrFmt_YUYV;
1922 case V4L2_MBUS_FMT_UYVY8_2X8:
1924 winseqe_set_addr = sensor_ClrFmt_UYVY;
1930 if (winseqe_set_addr != NULL) {
1931 sensor_write_array(client, winseqe_set_addr);
1932 sensor->info_priv.fmt.code = mf->code;
1933 sensor->info_priv.fmt.colorspace= mf->colorspace;
1934 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1936 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1943 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg!=0xff)
1945 winseqe_set_addr = sensor_qcif;
1949 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg!=0xff)
1951 winseqe_set_addr = sensor_qvga;
1955 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg!=0xff)
1957 winseqe_set_addr = sensor_cif;
1961 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg!=0xff)
1963 winseqe_set_addr = sensor_vga;
1967 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg!=0xff)
1969 winseqe_set_addr = sensor_svga;
1973 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg!=0xff)
1975 winseqe_set_addr = sensor_720p;
1979 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg!=0xff)
1981 winseqe_set_addr = sensor_sxga;
1987 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1988 set_w = SENSOR_INIT_WIDTH;
1989 set_h = SENSOR_INIT_HEIGHT;
1990 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1993 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1994 #if CONFIG_SENSOR_Flash
1995 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
1996 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1997 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1998 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2000 } else { /* ddl@rock-chips.com : Video */
2001 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2002 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2003 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2007 ret |= sensor_write_array(client, winseqe_set_addr);
2009 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2010 #if CONFIG_SENSOR_Flash
2011 if (sensor_fmt_capturechk(sd,mf) == true) {
2012 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2013 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2014 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2018 goto sensor_s_fmt_end;
2021 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
2023 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2024 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2025 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2026 if (sensor->info_priv.whiteBalance != 0) {
2027 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2028 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2030 sensor->info_priv.snap2preview = true;
2031 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2032 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2033 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2034 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2035 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2036 sensor->info_priv.video2preview = true;
2037 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2038 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2039 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2040 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2041 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2042 sensor->info_priv.video2preview = false;
2043 sensor->info_priv.snap2preview = false;
2046 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2050 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2060 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2062 struct i2c_client *client = v4l2_get_subdevdata(sd);
2063 struct sensor *sensor = to_sensor(client);
2064 const struct sensor_datafmt *fmt;
2065 int ret = 0,set_w,set_h;
2067 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2068 ARRAY_SIZE(sensor_colour_fmts));
2070 fmt = &sensor->info_priv.fmt;
2071 mf->code = fmt->code;
2074 if (mf->height > SENSOR_MAX_HEIGHT)
2075 mf->height = SENSOR_MAX_HEIGHT;
2076 else if (mf->height < SENSOR_MIN_HEIGHT)
2077 mf->height = SENSOR_MIN_HEIGHT;
2079 if (mf->width > SENSOR_MAX_WIDTH)
2080 mf->width = SENSOR_MAX_WIDTH;
2081 else if (mf->width < SENSOR_MIN_WIDTH)
2082 mf->width = SENSOR_MIN_WIDTH;
2087 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg!=0xff)
2092 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg!=0xff)
2097 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg!=0xff)
2102 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg!=0xff)
2107 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg!=0xff)
2112 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg!=0xff)
2117 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg!=0xff)
2124 set_w = SENSOR_INIT_WIDTH;
2125 set_h = SENSOR_INIT_HEIGHT;
2130 mf->colorspace = fmt->colorspace;
2135 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2137 struct i2c_client *client = v4l2_get_subdevdata(sd);;
2139 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2142 if (id->match.addr != client->addr)
2145 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV9650 identifier */
2150 #if CONFIG_SENSOR_Brightness
2151 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2153 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2155 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2157 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2159 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2161 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2164 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2168 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2172 #if CONFIG_SENSOR_Effect
2173 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2175 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2177 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2179 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2181 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2183 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2186 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2190 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2194 #if CONFIG_SENSOR_Exposure
2195 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2197 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2199 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2201 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2203 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2205 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2208 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2212 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2216 #if CONFIG_SENSOR_Saturation
2217 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2219 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2221 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2223 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2225 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2227 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2230 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2234 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2238 #if CONFIG_SENSOR_Contrast
2239 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2241 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2243 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2245 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2247 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2249 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2252 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2256 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2260 #if CONFIG_SENSOR_Mirror
2261 static int sensor_mirror(struct i2c_client *client, int on)
2266 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2268 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2270 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2272 if (sensor_mirror(client,value) != 0)
2273 SENSOR_TR("%s(%d): sensor_mirror failed, value:0x%x",__FUNCTION__, __LINE__,value);
2275 SENSOR_DG("%s(%d): sensor_mirror success, value:0x%x",__FUNCTION__, __LINE__,value);
2278 SENSOR_TR("\n %s..%s value = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2282 #if CONFIG_SENSOR_Flip
2283 static int sensor_flip(struct i2c_client *client, int on)
2288 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2290 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2292 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2294 if (sensor_flip(client,value) != 0)
2295 SENSOR_TR("%s(%d): sensor_flip failed, value:0x%x",__FUNCTION__, __LINE__,value);
2297 SENSOR_DG("%s(%d): sensor_flip success, value:0x%x",__FUNCTION__, __LINE__,value);
2300 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2304 #if CONFIG_SENSOR_Scene
2305 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2307 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2309 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2311 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2313 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2315 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2318 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2322 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2326 #if CONFIG_SENSOR_WhiteBalance
2327 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2329 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2331 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2333 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2335 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2337 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2340 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2344 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2348 #if CONFIG_SENSOR_DigitalZoom
2349 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2351 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2352 struct sensor *sensor = to_sensor(client);
2353 const struct v4l2_queryctrl *qctrl_info;
2354 int digitalzoom_cur, digitalzoom_total;
2356 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2360 digitalzoom_cur = sensor->info_priv.digitalzoom;
2361 digitalzoom_total = qctrl_info->maximum;
2363 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2365 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2369 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2371 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2375 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2377 *value = digitalzoom_total - digitalzoom_cur;
2380 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2382 *value = 0 - digitalzoom_cur;
2385 digitalzoom_cur += *value;
2387 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2389 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2391 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2394 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2401 #if CONFIG_SENSOR_Flash
2402 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2404 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2405 if (value == 3) { /* ddl@rock-chips.com: torch */
2406 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2408 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2410 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2414 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2419 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2421 struct i2c_client *client = v4l2_get_subdevdata(sd);;
2422 struct sensor *sensor = to_sensor(client);
2423 const struct v4l2_queryctrl *qctrl;
2425 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2429 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2435 case V4L2_CID_BRIGHTNESS:
2437 ctrl->value = sensor->info_priv.brightness;
2440 case V4L2_CID_SATURATION:
2442 ctrl->value = sensor->info_priv.saturation;
2445 case V4L2_CID_CONTRAST:
2447 ctrl->value = sensor->info_priv.contrast;
2450 case V4L2_CID_DO_WHITE_BALANCE:
2452 ctrl->value = sensor->info_priv.whiteBalance;
2455 case V4L2_CID_EXPOSURE:
2457 ctrl->value = sensor->info_priv.exposure;
2460 case V4L2_CID_HFLIP:
2462 ctrl->value = sensor->info_priv.mirror;
2465 case V4L2_CID_VFLIP:
2467 ctrl->value = sensor->info_priv.flip;
2478 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2480 struct i2c_client *client = v4l2_get_subdevdata(sd);;
2481 struct sensor *sensor = to_sensor(client);
2482 struct soc_camera_device *icd = client->dev.platform_data;
2483 const struct v4l2_queryctrl *qctrl;
2486 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2490 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2496 #if CONFIG_SENSOR_Brightness
2497 case V4L2_CID_BRIGHTNESS:
2499 if (ctrl->value != sensor->info_priv.brightness)
2501 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2505 sensor->info_priv.brightness = ctrl->value;
2510 #if CONFIG_SENSOR_Exposure
2511 case V4L2_CID_EXPOSURE:
2513 if (ctrl->value != sensor->info_priv.exposure)
2515 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2519 sensor->info_priv.exposure = ctrl->value;
2524 #if CONFIG_SENSOR_Saturation
2525 case V4L2_CID_SATURATION:
2527 if (ctrl->value != sensor->info_priv.saturation)
2529 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2533 sensor->info_priv.saturation = ctrl->value;
2538 #if CONFIG_SENSOR_Contrast
2539 case V4L2_CID_CONTRAST:
2541 if (ctrl->value != sensor->info_priv.contrast)
2543 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2547 sensor->info_priv.contrast = ctrl->value;
2552 #if CONFIG_SENSOR_WhiteBalance
2553 case V4L2_CID_DO_WHITE_BALANCE:
2555 if (ctrl->value != sensor->info_priv.whiteBalance)
2557 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2561 sensor->info_priv.whiteBalance = ctrl->value;
2566 #if CONFIG_SENSOR_Mirror
2567 case V4L2_CID_HFLIP:
2569 if (ctrl->value != sensor->info_priv.mirror)
2571 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2573 sensor->info_priv.mirror = ctrl->value;
2578 #if CONFIG_SENSOR_Flip
2579 case V4L2_CID_VFLIP:
2581 if (ctrl->value != sensor->info_priv.flip)
2583 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2585 sensor->info_priv.flip = ctrl->value;
2596 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2598 const struct v4l2_queryctrl *qctrl;
2599 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2600 struct sensor *sensor = to_sensor(client);
2602 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2606 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2610 switch (ext_ctrl->id)
2612 case V4L2_CID_SCENE:
2614 ext_ctrl->value = sensor->info_priv.scene;
2617 case V4L2_CID_EFFECT:
2619 ext_ctrl->value = sensor->info_priv.effect;
2622 case V4L2_CID_ZOOM_ABSOLUTE:
2624 ext_ctrl->value = sensor->info_priv.digitalzoom;
2627 case V4L2_CID_ZOOM_RELATIVE:
2631 case V4L2_CID_FOCUS_ABSOLUTE:
2633 ext_ctrl->value = sensor->info_priv.focus;
2636 case V4L2_CID_FOCUS_RELATIVE:
2640 case V4L2_CID_FLASH:
2642 ext_ctrl->value = sensor->info_priv.flash;
2650 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2652 const struct v4l2_queryctrl *qctrl;
2653 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2654 struct sensor *sensor = to_sensor(client);
2657 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2661 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2666 switch (ext_ctrl->id)
2668 #if CONFIG_SENSOR_Scene
2669 case V4L2_CID_SCENE:
2671 if (ext_ctrl->value != sensor->info_priv.scene)
2673 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2675 sensor->info_priv.scene = ext_ctrl->value;
2680 #if CONFIG_SENSOR_Effect
2681 case V4L2_CID_EFFECT:
2683 if (ext_ctrl->value != sensor->info_priv.effect)
2685 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2687 sensor->info_priv.effect= ext_ctrl->value;
2692 #if CONFIG_SENSOR_DigitalZoom
2693 case V4L2_CID_ZOOM_ABSOLUTE:
2695 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2698 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2700 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2702 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2704 sensor->info_priv.digitalzoom += val_offset;
2706 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2711 case V4L2_CID_ZOOM_RELATIVE:
2713 if (ext_ctrl->value)
2715 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2717 sensor->info_priv.digitalzoom += ext_ctrl->value;
2719 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2724 #if CONFIG_SENSOR_Focus
2725 case V4L2_CID_FOCUS_ABSOLUTE:
2727 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2730 if (ext_ctrl->value != sensor->info_priv.focus)
2732 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2734 sensor->info_priv.focus += val_offset;
2739 case V4L2_CID_FOCUS_RELATIVE:
2741 if (ext_ctrl->value)
2743 sensor->info_priv.focus += ext_ctrl->value;
2745 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2750 #if CONFIG_SENSOR_Flash
2751 case V4L2_CID_FLASH:
2753 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2755 sensor->info_priv.flash = ext_ctrl->value;
2757 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2768 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2770 struct i2c_client *client = v4l2_get_subdevdata(sd);;
2771 struct soc_camera_device *icd = client->dev.platform_data;
2772 int i, error_cnt=0, error_idx=-1;
2775 for (i=0; i<ext_ctrl->count; i++) {
2776 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2783 error_idx = ext_ctrl->count;
2785 if (error_idx != -1) {
2786 ext_ctrl->error_idx = error_idx;
2793 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2795 struct i2c_client *client = v4l2_get_subdevdata(sd);;
2796 struct soc_camera_device *icd = client->dev.platform_data;
2797 int i, error_cnt=0, error_idx=-1;
2800 for (i=0; i<ext_ctrl->count; i++) {
2801 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2808 error_idx = ext_ctrl->count;
2810 if (error_idx != -1) {
2811 ext_ctrl->error_idx = error_idx;
2818 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2819 * this wasn't our capture interface, so, we wait for the right one */
2820 static int sensor_video_probe(struct soc_camera_device *icd,
2821 struct i2c_client *client)
2825 struct sensor *sensor = to_sensor(client);
2827 /* We must have a parent by now. And it cannot be a wrong one.
2828 * So this entire test is completely redundant. */
2829 if (!icd->dev.parent ||
2830 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2833 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2835 printk("%s(): set powerdn failed\n", __FUNCTION__);
2836 goto sensor_video_probe_err;
2840 /* ret = sensor_write(client, 0x12, 0x80);
2843 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2846 mdelay(50); *///delay 5 microseconds
2848 /* check if it is an sensor sensor */
2849 ret = sensor_read(client, 0x00, &pid);
2851 SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2855 goto sensor_video_probe_err;
2858 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2859 if (pid == SENSOR_ID) {
2860 sensor->model = SENSOR_V4L2_IDENT;
2862 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2865 goto sensor_video_probe_err;
2871 sensor_video_probe_err:
2876 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2878 struct i2c_client *client = v4l2_get_subdevdata(sd);;
2879 struct soc_camera_device *icd = client->dev.platform_data;
2880 struct sensor *sensor = to_sensor(client);
2882 #if CONFIG_SENSOR_Flash
2886 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2889 case RK29_CAM_SUBDEV_DEACTIVATE:
2891 sensor_deactivate(client);
2895 case RK29_CAM_SUBDEV_IOREQUEST:
2897 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2898 if (sensor->sensor_io_request != NULL) {
2899 sensor->sensor_gpio_res = NULL;
2900 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
2901 if (sensor->sensor_io_request->gpio_res[i].dev_name &&
2902 (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
2903 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
2906 if (sensor->sensor_gpio_res == NULL) {
2907 SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
2909 goto sensor_ioctl_end;
2912 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2914 goto sensor_ioctl_end;
2916 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2918 #if CONFIG_SENSOR_Flash
2919 if (sensor->sensor_gpio_res) {
2920 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2921 for (i = 0; i < icd->ops->num_controls; i++) {
2922 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2923 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2924 sensor_controls[i].id=0xffff;
2927 sensor->info_priv.flash = 0xff;
2928 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2929 }else{ //two cameras are the same,need to deal diffrently ,zyc
2930 for (i = 0; i < icd->ops->num_controls; i++) {
2931 if(0xffff == icd->ops->controls[i].id){
2932 sensor_controls[i].id=V4L2_CID_FLASH;
2942 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2950 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2951 enum v4l2_mbus_pixelcode *code)
2953 if (index >= ARRAY_SIZE(sensor_colour_fmts))
2956 *code = sensor_colour_fmts[index].code;
2959 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2960 .init = sensor_init,
2961 .g_ctrl = sensor_g_control,
2962 .s_ctrl = sensor_s_control,
2963 .g_ext_ctrls = sensor_g_ext_controls,
2964 .s_ext_ctrls = sensor_s_ext_controls,
2965 .g_chip_ident = sensor_g_chip_ident,
2966 .ioctl = sensor_ioctl,
2969 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2970 .s_mbus_fmt = sensor_s_fmt,
2971 .g_mbus_fmt = sensor_g_fmt,
2972 .try_mbus_fmt = sensor_try_fmt,
2973 .enum_mbus_fmt = sensor_enum_fmt,
2976 static struct v4l2_subdev_ops sensor_subdev_ops = {
2977 .core = &sensor_subdev_core_ops,
2978 .video = &sensor_subdev_video_ops,
2981 static int sensor_probe(struct i2c_client *client,
2982 const struct i2c_device_id *did)
2984 struct sensor *sensor;
2985 struct soc_camera_device *icd = client->dev.platform_data;
2986 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2987 struct soc_camera_link *icl;
2990 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2992 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2996 icl = to_soc_camera_link(icd);
2998 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3002 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3003 dev_warn(&adapter->dev,
3004 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3008 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3012 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3014 /* Second stage probe - when a capture adapter is there */
3015 icd->ops = &sensor_ops;
3016 sensor->info_priv.fmt = sensor_colour_fmts[0];
3017 #if CONFIG_SENSOR_I2C_NOSCHED
3018 atomic_set(&sensor->tasklock_cnt,0);
3021 ret = sensor_video_probe(icd, client);
3024 i2c_set_clientdata(client, NULL);
3028 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3029 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3033 static int sensor_remove(struct i2c_client *client)
3035 struct sensor *sensor = to_sensor(client);
3036 struct soc_camera_device *icd = client->dev.platform_data;
3039 i2c_set_clientdata(client, NULL);
3040 client->driver = NULL;
3046 static const struct i2c_device_id sensor_id[] = {
3047 {SENSOR_NAME_STRING(), 0 },
3050 MODULE_DEVICE_TABLE(i2c, sensor_id);
3052 static struct i2c_driver sensor_i2c_driver = {
3054 .name = SENSOR_NAME_STRING(),
3056 .probe = sensor_probe,
3057 .remove = sensor_remove,
3058 .id_table = sensor_id,
3061 static int __init sensor_mod_init(void)
3063 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3064 return i2c_add_driver(&sensor_i2c_driver);
3067 static void __exit sensor_mod_exit(void)
3069 i2c_del_driver(&sensor_i2c_driver);
3072 device_initcall_sync(sensor_mod_init);
3073 module_exit(sensor_mod_exit);
3075 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3076 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3077 MODULE_LICENSE("GPL");