2 * Driver for OV5640 CMOS Image Sensor from OmniVision
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/miscdevice.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/soc_camera.h>
22 #include <plat/rk_camera.h>
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__)
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
42 #define MIN(x,y) ((x<y) ? x: y)
43 #define MAX(x,y) ((x>y) ? x: y)
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK29_CAM_SENSOR_OV5640
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV5640
48 #define SENSOR_ID 0x5640
49 #define SENSOR_MIN_WIDTH 176
50 #define SENSOR_MIN_HEIGHT 144
51 #define SENSOR_MAX_WIDTH 2592
52 #define SENSOR_MAX_HEIGHT 1944
53 #define SENSOR_INIT_WIDTH 800 /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT 600
55 #define SENSOR_INIT_WINSEQADR sensor_svga
56 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_YUYV8_2X8
58 #define CONFIG_SENSOR_WhiteBalance 1
59 #define CONFIG_SENSOR_Brightness 0
60 #define CONFIG_SENSOR_Contrast 0
61 #define CONFIG_SENSOR_Saturation 0
62 #define CONFIG_SENSOR_Effect 1
63 #define CONFIG_SENSOR_Scene 1
64 #define CONFIG_SENSOR_DigitalZoom 0
65 #define CONFIG_SENSOR_Exposure 0
66 #define CONFIG_SENSOR_Flash 1
67 #define CONFIG_SENSOR_Mirror 0
68 #define CONFIG_SENSOR_Flip 0
69 #ifdef CONFIG_OV5640_AUTOFOCUS
70 #define CONFIG_SENSOR_Focus 1
71 #include "ov5640_af_firmware.c"
73 #define CONFIG_SENSOR_Focus 0
76 #define CONFIG_SENSOR_I2C_SPEED 250000 /* Hz */
77 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
78 #define CONFIG_SENSOR_I2C_NOSCHED 0
79 #define CONFIG_SENSOR_I2C_RDWRCHK 0
81 #define CONFIG_SENSOR_WRITE_REGS 1
82 #define WRITE_REGS_NUM 3
84 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
85 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
86 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
88 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
89 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
90 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
91 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
92 #define COLOR_TEMPERATURE_OFFICE_DN 3500
93 #define COLOR_TEMPERATURE_OFFICE_UP 5000
94 #define COLOR_TEMPERATURE_HOME_DN 2500
95 #define COLOR_TEMPERATURE_HOME_UP 3500
97 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
98 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
100 #define SENSOR_AF_IS_ERR (0x00<<0)
101 #define SENSOR_AF_IS_OK (0x01<<0)
102 #define SENSOR_INIT_IS_ERR (0x00<<28)
103 #define SENSOR_INIT_IS_OK (0x01<<28)
105 #if CONFIG_SENSOR_Focus
106 /*#define SENSOR_AF_MODE_INFINITY 0
107 #define SENSOR_AF_MODE_MACRO 1
108 #define SENSOR_AF_MODE_FIXED 2
109 #define SENSOR_AF_MODE_AUTO 3
110 #define SENSOR_AF_MODE_CONTINUOUS 4
111 #define SENSOR_AF_MODE_CLOSE 5*/
112 #define SENSOR_AF_MODE_AUTO 0
113 #define SENSOR_AF_MODE_CLOSE 1
116 #if CONFIG_SENSOR_Focus
117 /* ov5640 VCM Command and Status Registers */
118 #define CMD_MAIN_Reg 0x3022
119 //#define CMD_TAG_Reg 0x3023
120 #define CMD_ACK_Reg 0x3023
121 #define CMD_PARA0_Reg 0x3024
122 #define CMD_PARA1_Reg 0x3025
123 #define CMD_PARA2_Reg 0x3026
124 #define CMD_PARA3_Reg 0x3027
125 #define CMD_PARA4_Reg 0x3028
127 //#define STA_ZONE_Reg 0x3026
128 #define STA_FOCUS_Reg 0x3029
130 /* ov5640 VCM Command */
131 #define OverlayEn_Cmd 0x01
132 #define OverlayDis_Cmd 0x02
133 #define SingleFocus_Cmd 0x03
134 #define ConstFocus_Cmd 0x04
135 #define StepMode_Cmd 0x05
136 #define PauseFocus_Cmd 0x06
137 #define ReturnIdle_Cmd 0x08
138 #define SetZone_Cmd 0x10
139 #define UpdateZone_Cmd 0x12
140 #define SetMotor_Cmd 0x20
143 #define Zone_configuration_Cmd 0x12
144 #define Trig_autofocus_Cmd 0x03
145 #define Get_focus_result_Cmd 0x07
147 /* ov5640 Focus State */
148 //#define S_FIRWRE 0xFF /*Firmware is downloaded and not run*/
149 #define S_STARTUP 0x7e /*Firmware is initializing*/
151 #define S_IDLE 0x70 /*Idle state, focus is released; lens is located at the furthest position.*/
152 #define S_FOCUSING 0x00 /*Auto Focus is running.*/
153 #define S_FOCUSED 0x10 /*Auto Focus is completed.*/
155 #define S_CAPTURE 0x12
158 /* ov5640 Zone State */
159 #define Zone_Is_Focused(a, zone_val) (zone_val&(1<<(a-3)))
160 #define Zone_Get_ID(zone_val) (zone_val&0x03)
162 #define Zone_CenterMode 0x01
163 #define Zone_5xMode 0x02
164 #define Zone_5PlusMode 0x03
165 #define Zone_4fMode 0x04
167 #define ZoneSel_Auto 0x0b
168 #define ZoneSel_SemiAuto 0x0c
169 #define ZoneSel_Manual 0x0d
170 #define ZoneSel_Rotate 0x0e
172 /* ov5640 Step Focus Commands */
173 #define StepFocus_Near_Tag 0x01
174 #define StepFocus_Far_Tag 0x02
175 #define StepFocus_Furthest_Tag 0x03
176 #define StepFocus_Nearest_Tag 0x04
177 #define StepFocus_Spec_Tag 0x10
180 //flash off in fixed time to prevent from too hot , zyc
182 struct soc_camera_device *icd;
183 struct hrtimer timer;
185 static enum hrtimer_restart flash_off_func(struct hrtimer *timer);
187 static struct flash_timer flash_off_timer;
188 //for user defined if user want to customize the series , zyc
189 #ifdef CONFIG_OV5640_USER_DEFINED_SERIES
190 #include "ov5640_user_series.c"
193 /* init 800X600 SVGA */
194 static struct reginfo sensor_init_data[] =
454 /* 720p 15fps @ 1280x720 */
456 static struct reginfo sensor_720p[]=
484 {0x3820, 0x41}, //ddl@rock-chips.com add start: qsxvga -> 720p isn't stream on
505 {0x4713, 0x03},//////ddl@rock-chips.com add end
521 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
523 static struct reginfo sensor_1080p[]=
529 /* 2592X1944 QSXGA */
530 static struct reginfo sensor_qsxga[] =
571 static struct reginfo sensor_qxga[] =
613 static struct reginfo sensor_uxga[] =
655 static struct reginfo sensor_sxga[] =
696 static struct reginfo sensor_xga[] =
736 static struct reginfo sensor_svga[] =
782 static struct reginfo sensor_vga[] =
788 static struct reginfo sensor_cif[] =
795 static struct reginfo sensor_qvga[] =
802 static struct reginfo sensor_qcif[] =
808 static struct reginfo sensor_ClrFmt_YUYV[]=
814 static struct reginfo sensor_ClrFmt_UYVY[]=
821 #if CONFIG_SENSOR_WhiteBalance
822 static struct reginfo sensor_WhiteB_Auto[]=
849 /* Cloudy Colour Temperature : 6500K - 8000K */
850 static struct reginfo sensor_WhiteB_Cloudy[]=
861 /* ClearDay Colour Temperature : 5000K - 6500K */
862 static struct reginfo sensor_WhiteB_ClearDay[]=
873 /* Office Colour Temperature : 3500K - 5000K */
874 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
885 /* Home Colour Temperature : 2500K - 3500K */
886 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
897 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
898 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
902 #if CONFIG_SENSOR_Brightness
903 static struct reginfo sensor_Brightness0[]=
907 static struct reginfo sensor_Brightness1[]=
912 static struct reginfo sensor_Brightness2[]=
916 static struct reginfo sensor_Brightness3[]=
920 static struct reginfo sensor_Brightness4[]=
925 static struct reginfo sensor_Brightness5[]=
929 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
930 sensor_Brightness4, sensor_Brightness5,NULL,
935 #if CONFIG_SENSOR_Effect
936 static struct reginfo sensor_Effect_Normal[] =
942 static struct reginfo sensor_Effect_WandB[] =
950 static struct reginfo sensor_Effect_Sepia[] =
959 static struct reginfo sensor_Effect_Negative[] =
965 };static struct reginfo sensor_Effect_Bluish[] =
975 static struct reginfo sensor_Effect_Green[] =
984 /*static struct reginfo sensor_Effect_Reddish[] =
994 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
995 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
998 #if CONFIG_SENSOR_Exposure
999 static struct reginfo sensor_Exposure0[]=
1001 {SEQUENCE_END, 0x00}
1003 static struct reginfo sensor_Exposure1[]=
1005 {SEQUENCE_END, 0x00}
1007 static struct reginfo sensor_Exposure2[]=
1009 {SEQUENCE_END, 0x00}
1012 static struct reginfo sensor_Exposure3[]=
1014 {SEQUENCE_END, 0x00}
1016 static struct reginfo sensor_Exposure4[]=
1018 {SEQUENCE_END, 0x00}
1020 static struct reginfo sensor_Exposure5[]=
1022 {SEQUENCE_END, 0x00}
1024 static struct reginfo sensor_Exposure6[]=
1026 {SEQUENCE_END, 0x00}
1028 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1029 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1032 #if CONFIG_SENSOR_Saturation
1033 static struct reginfo sensor_Saturation0[]=
1035 {SEQUENCE_END, 0x00}
1038 static struct reginfo sensor_Saturation1[]=
1040 {SEQUENCE_END, 0x00}
1043 static struct reginfo sensor_Saturation2[]=
1045 {SEQUENCE_END, 0x00}
1046 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1049 #if CONFIG_SENSOR_Contrast
1050 static struct reginfo sensor_Contrast0[]=
1052 {SEQUENCE_END, 0x00}
1055 static struct reginfo sensor_Contrast1[]=
1057 {SEQUENCE_END, 0x00}
1059 static struct reginfo sensor_Contrast2[]=
1061 {SEQUENCE_END, 0x00}
1064 static struct reginfo sensor_Contrast3[]=
1066 {SEQUENCE_END, 0x00}
1069 static struct reginfo sensor_Contrast4[]=
1071 {SEQUENCE_END, 0x00}
1075 static struct reginfo sensor_Contrast5[]=
1077 {SEQUENCE_END, 0x00}
1080 static struct reginfo sensor_Contrast6[]=
1082 {SEQUENCE_END, 0x00}
1084 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1085 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1089 #if CONFIG_SENSOR_Mirror
1090 static struct reginfo sensor_MirrorOn[]=
1092 {SEQUENCE_END, 0x00}
1094 static struct reginfo sensor_MirrorOff[]=
1096 {SEQUENCE_END, 0x00}
1098 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1100 #if CONFIG_SENSOR_Flip
1101 static struct reginfo sensor_FlipOn[]=
1103 {SEQUENCE_END, 0x00}
1106 static struct reginfo sensor_FlipOff[]=
1108 {SEQUENCE_END, 0x00}
1110 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1113 #if CONFIG_SENSOR_Scene
1114 static struct reginfo sensor_SceneAuto[] =
1117 {SEQUENCE_END, 0x00}
1119 static struct reginfo sensor_SceneNight[] =
1121 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1139 {SEQUENCE_END, 0x00}
1141 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1144 #if CONFIG_SENSOR_DigitalZoom
1145 static struct reginfo sensor_Zoom0[] =
1147 {SEQUENCE_END, 0x00}
1149 static struct reginfo sensor_Zoom1[] =
1151 {SEQUENCE_END, 0x00}
1154 static struct reginfo sensor_Zoom2[] =
1156 {SEQUENCE_END, 0x00}
1160 static struct reginfo sensor_Zoom3[] =
1162 {SEQUENCE_END, 0x00}
1164 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1166 static const struct v4l2_querymenu sensor_menus[] =
1168 #if CONFIG_SENSOR_WhiteBalance
1169 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1170 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1171 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1174 #if CONFIG_SENSOR_Effect
1175 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1176 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1177 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1180 #if CONFIG_SENSOR_Scene
1181 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1184 #if CONFIG_SENSOR_Flash
1185 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1186 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1190 static struct v4l2_queryctrl sensor_controls[] =
1192 #if CONFIG_SENSOR_WhiteBalance
1194 .id = V4L2_CID_DO_WHITE_BALANCE,
1195 .type = V4L2_CTRL_TYPE_MENU,
1196 .name = "White Balance Control",
1204 #if CONFIG_SENSOR_Brightness
1206 .id = V4L2_CID_BRIGHTNESS,
1207 .type = V4L2_CTRL_TYPE_INTEGER,
1208 .name = "Brightness Control",
1216 #if CONFIG_SENSOR_Effect
1218 .id = V4L2_CID_EFFECT,
1219 .type = V4L2_CTRL_TYPE_MENU,
1220 .name = "Effect Control",
1228 #if CONFIG_SENSOR_Exposure
1230 .id = V4L2_CID_EXPOSURE,
1231 .type = V4L2_CTRL_TYPE_INTEGER,
1232 .name = "Exposure Control",
1240 #if CONFIG_SENSOR_Saturation
1242 .id = V4L2_CID_SATURATION,
1243 .type = V4L2_CTRL_TYPE_INTEGER,
1244 .name = "Saturation Control",
1252 #if CONFIG_SENSOR_Contrast
1254 .id = V4L2_CID_CONTRAST,
1255 .type = V4L2_CTRL_TYPE_INTEGER,
1256 .name = "Contrast Control",
1264 #if CONFIG_SENSOR_Mirror
1266 .id = V4L2_CID_HFLIP,
1267 .type = V4L2_CTRL_TYPE_BOOLEAN,
1268 .name = "Mirror Control",
1276 #if CONFIG_SENSOR_Flip
1278 .id = V4L2_CID_VFLIP,
1279 .type = V4L2_CTRL_TYPE_BOOLEAN,
1280 .name = "Flip Control",
1288 #if CONFIG_SENSOR_Scene
1290 .id = V4L2_CID_SCENE,
1291 .type = V4L2_CTRL_TYPE_MENU,
1292 .name = "Scene Control",
1300 #if CONFIG_SENSOR_DigitalZoom
1302 .id = V4L2_CID_ZOOM_RELATIVE,
1303 .type = V4L2_CTRL_TYPE_INTEGER,
1304 .name = "DigitalZoom Control",
1310 .id = V4L2_CID_ZOOM_ABSOLUTE,
1311 .type = V4L2_CTRL_TYPE_INTEGER,
1312 .name = "DigitalZoom Control",
1320 #if CONFIG_SENSOR_Focus
1322 .id = V4L2_CID_FOCUS_RELATIVE,
1323 .type = V4L2_CTRL_TYPE_INTEGER,
1324 .name = "Focus Control",
1330 .id = V4L2_CID_FOCUS_ABSOLUTE,
1331 .type = V4L2_CTRL_TYPE_INTEGER,
1332 .name = "Focus Control",
1336 .default_value = 125,
1339 .id = V4L2_CID_FOCUS_AUTO,
1340 .type = V4L2_CTRL_TYPE_BOOLEAN,
1341 .name = "Focus Control",
1347 .id = V4L2_CID_FOCUS_CONTINUOUS,
1348 .type = V4L2_CTRL_TYPE_BOOLEAN,
1349 .name = "Focus Control",
1357 #if CONFIG_SENSOR_Flash
1359 .id = V4L2_CID_FLASH,
1360 .type = V4L2_CTRL_TYPE_MENU,
1361 .name = "Flash Control",
1370 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1371 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1372 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1373 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1374 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1375 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1376 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1377 static int sensor_resume(struct soc_camera_device *icd);
1378 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1379 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1380 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1381 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1382 static int sensor_deactivate(struct i2c_client *client);
1383 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1384 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1386 static struct soc_camera_ops sensor_ops =
1388 .suspend = sensor_suspend,
1389 .resume = sensor_resume,
1390 .set_bus_param = sensor_set_bus_param,
1391 .query_bus_param = sensor_query_bus_param,
1392 .controls = sensor_controls,
1393 .menus = sensor_menus,
1394 .num_controls = ARRAY_SIZE(sensor_controls),
1395 .num_menus = ARRAY_SIZE(sensor_menus),
1397 /* only one fixed colorspace per pixelcode */
1398 struct sensor_datafmt {
1399 enum v4l2_mbus_pixelcode code;
1400 enum v4l2_colorspace colorspace;
1403 /* Find a data format by a pixel code in an array */
1404 static const struct sensor_datafmt *sensor_find_datafmt(
1405 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1409 for (i = 0; i < n; i++)
1410 if (fmt[i].code == code)
1416 static const struct sensor_datafmt sensor_colour_fmts[] = {
1417 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1418 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1424 WqCmd_af_special_pos,
1428 WqCmd_af_return_idle,
1430 enum sensor_wq_result
1438 struct i2c_client *client;
1439 struct delayed_work dwork;
1440 enum sensor_wq_cmd cmd;
1441 wait_queue_head_t done;
1442 enum sensor_wq_result result;
1447 typedef struct sensor_info_priv_s
1463 unsigned char mirror; /* HFLIP */
1464 unsigned char flip; /* VFLIP */
1465 struct reginfo *winseqe_cur_addr;
1466 struct sensor_datafmt fmt;
1467 unsigned int enable;
1468 unsigned int funmodule_state;
1469 } sensor_info_priv_t;
1473 struct sensor_parameter
1475 unsigned short int preview_maxlines;
1476 unsigned short int preview_exposure;
1477 unsigned short int preview_line_width;
1478 unsigned short int preview_gain;
1480 unsigned short int capture_framerate;
1481 unsigned short int preview_framerate;
1487 struct v4l2_subdev subdev;
1488 struct i2c_client *client;
1489 sensor_info_priv_t info_priv;
1490 struct sensor_parameter parameter;
1491 struct workqueue_struct *sensor_wq;
1492 struct mutex wq_lock;
1493 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1494 #if CONFIG_SENSOR_I2C_NOSCHED
1495 atomic_t tasklock_cnt;
1497 struct rk29camera_platform_data *sensor_io_request;
1498 struct rk29camera_gpio_res *sensor_gpio_res;
1501 static struct sensor* to_sensor(const struct i2c_client *client)
1503 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1506 static int sensor_task_lock(struct i2c_client *client, int lock)
1508 #if CONFIG_SENSOR_I2C_NOSCHED
1510 struct sensor *sensor = to_sensor(client);
1513 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1514 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1515 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1519 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1520 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1521 goto sensor_task_lock_err;
1526 atomic_add(1, &sensor->tasklock_cnt);
1528 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1529 atomic_sub(1, &sensor->tasklock_cnt);
1531 if (atomic_read(&sensor->tasklock_cnt) == 0)
1536 sensor_task_lock_err:
1545 #if CONFIG_SENSOR_WRITE_REGS
1546 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
1549 struct i2c_msg msg[1];
1552 msg->addr = client->addr;
1553 msg->flags = client->flags;
1554 msg->buf = reg_info;
1555 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1556 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1562 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1563 err = i2c_transfer(client->adapter, msg, 1);
1567 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
1581 /* sensor register write */
1582 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1586 struct i2c_msg msg[1];
1589 buf[1] = reg & 0xFF;
1592 msg->addr = client->addr;
1593 msg->flags = client->flags;
1595 msg->len = sizeof(buf);
1596 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1597 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1602 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1603 err = i2c_transfer(client->adapter, msg, 1);
1608 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1616 /* sensor register read */
1617 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1621 struct i2c_msg msg[2];
1624 buf[1] = reg & 0xFF;
1626 msg[0].addr = client->addr;
1627 msg[0].flags = client->flags;
1629 msg[0].len = sizeof(buf);
1630 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1631 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1633 msg[1].addr = client->addr;
1634 msg[1].flags = client->flags|I2C_M_RD;
1637 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1638 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1642 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1643 err = i2c_transfer(client->adapter, msg, 2);
1649 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1657 /* write a array of registers */
1658 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1662 #if CONFIG_SENSOR_Focus
1663 struct sensor *sensor = to_sensor(client);
1665 #if CONFIG_SENSOR_I2C_RDWRCHK
1668 #if CONFIG_SENSOR_WRITE_REGS
1675 if (sensor_task_lock(client, 1) < 0)
1676 goto sensor_write_array_end;
1677 while (regarray[i].reg != SEQUENCE_END) {
1678 #if CONFIG_SENSOR_Focus
1679 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1680 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1682 goto sensor_write_array_end;
1686 #if CONFIG_SENSOR_WRITE_REGS
1690 reg_length = 0x0001;
1692 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
1695 if(reg_num >= WRITE_REGS_NUM)
1702 size_num = reg_num + 1;
1704 ptemp = phead = (u8*)kmalloc((size_num+10)*sizeof(u8),GFP_KERNEL);
1706 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1708 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1710 *phead = regarray[j].reg >> 8;
1711 *(ptemp+1) = regarray[j].reg & 0xFF;
1713 for( ; reg_num > 0; reg_num --, j++) {
1714 *ptemp ++ = regarray[j].val;
1718 err = sensor_write_regs(client, ptemp,size_num);
1722 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1725 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1730 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1734 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1736 goto sensor_write_array_end;
1739 #if CONFIG_SENSOR_I2C_RDWRCHK
1740 sensor_read(client, regarray[i].reg, &valchk);
1741 if (valchk != regarray[i].val)
1742 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1748 #if CONFIG_SENSOR_Focus
1749 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1750 || (regarray == sensor_init_data)) {
1751 sensor->info_priv.affm_reinit = 1;
1755 sensor_write_array_end:
1756 sensor_task_lock(client,0);
1759 #if CONFIG_SENSOR_I2C_RDWRCHK
1760 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1768 while (regarray[i].reg != 0)
1770 sensor_read(client, regarray[i].reg, &valchk);
1771 if (valchk != regarray[i].val)
1772 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1779 #if CONFIG_SENSOR_Focus
1786 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1789 char read_tag=0xff,cnt;
1792 if (cmdinfo->validate_bit & 0x80) {
1793 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1794 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1795 goto sensor_af_cmdset_err;
1797 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1799 for (i=0; i<4; i++) {
1800 if (cmdinfo->validate_bit & (1<<i)) {
1801 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1802 SENSOR_TR("%s write CMD_PARA_Reg(main:0x%x para%d:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,i,cmdinfo->cmd_para[i]);
1803 goto sensor_af_cmdset_err;
1805 SENSOR_DG("%s write CMD_PARA_Reg(main:0x%x para%d:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,i,cmdinfo->cmd_para[i]);
1809 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
1810 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1811 goto sensor_af_cmdset_err;
1813 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1816 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1817 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1818 goto sensor_af_cmdset_err;
1825 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1826 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1829 } while((read_tag != 0x00)&& (cnt++<100));
1831 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1833 sensor_af_cmdset_err:
1837 static int sensor_af_idlechk(struct i2c_client *client)
1841 struct af_cmdinfo cmdinfo;
1843 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1845 cmdinfo.cmd_tag = 0x01;
1846 cmdinfo.validate_bit = 0x80;
1847 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1849 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1851 goto sensor_af_idlechk_end;
1856 ret = sensor_read(client, CMD_ACK_Reg, &state);
1858 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1860 goto sensor_af_idlechk_end;
1862 }while(0x00 != state);
1865 sensor_af_idlechk_end:
1869 static int sensor_af_single(struct i2c_client *client)
1873 struct af_cmdinfo cmdinfo;
1875 cmdinfo.cmd_tag = 0x01;
1876 cmdinfo.validate_bit = 0x80;
1877 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
1879 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1881 goto sensor_af_single_end;
1891 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1893 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1895 goto sensor_af_single_end;
1897 }while((state == S_FOCUSING) && (cnt<100));
1899 if (state != S_FOCUSED) {
1900 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1902 goto sensor_af_single_end;
1905 sensor_af_single_end:
1909 static int sensor_af_const(struct i2c_client *client)
1918 static int sensor_af_init(struct i2c_client *client)
1923 ret = sensor_write_array(client, sensor_af_firmware);
1925 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1927 goto sensor_af_init_end;
1937 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1939 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1941 goto sensor_af_init_end;
1943 }while((state == S_STARTUP) && (cnt<100));
1945 if (state != S_IDLE) {
1946 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1948 goto sensor_af_init_end;
1952 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1956 static int sensor_af_downfirmware(struct i2c_client *client)
1958 struct sensor *sensor = to_sensor(client);
1960 struct soc_camera_device *icd = client->dev.platform_data;
1961 struct v4l2_mbus_framefmt mf;
1963 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1965 if (sensor_af_init(client)) {
1966 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1969 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1971 mf.width = icd->user_width;
1972 mf.height = icd->user_height;
1973 mf.code = sensor->info_priv.fmt.code;
1974 mf.colorspace = sensor->info_priv.fmt.colorspace;
1975 mf.field = V4L2_FIELD_NONE;
1976 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
1977 ret = sensor_af_const(client);
1979 switch (sensor->info_priv.auto_focus)
1981 case SENSOR_AF_MODE_AUTO:
1983 ret = sensor_af_single(client);
1986 case SENSOR_AF_MODE_CLOSE:
1993 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1994 goto sensor_af_downfirmware_end;
1998 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
2001 sensor_af_downfirmware_end:
2005 static void sensor_af_workqueue(struct work_struct *work)
2007 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
2008 struct i2c_client *client = sensor_work->client;
2009 struct sensor *sensor = to_sensor(client);
2010 struct af_cmdinfo cmdinfo;
2012 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
2014 mutex_lock(&sensor->wq_lock);
2016 switch (sensor_work->cmd)
2020 if (sensor_af_downfirmware(client) < 0) {
2021 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2025 case WqCmd_af_single:
2027 if (sensor_af_single(client) < 0) {
2028 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2029 sensor_work->result = WqRet_fail;
2031 sensor_work->result = WqRet_success;
2035 case WqCmd_af_special_pos:
2037 sensor_af_idlechk(client);
2039 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2040 cmdinfo.cmd_para[0] = sensor_work->var;
2041 cmdinfo.validate_bit = 0x81;
2042 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2043 sensor_work->result = WqRet_fail;
2045 sensor_work->result = WqRet_success;
2048 case WqCmd_af_near_pos:
2050 sensor_af_idlechk(client);
2051 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2052 cmdinfo.validate_bit = 0x80;
2053 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2054 sensor_work->result = WqRet_fail;
2056 sensor_work->result = WqRet_success;
2059 case WqCmd_af_far_pos:
2061 sensor_af_idlechk(client);
2062 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2063 cmdinfo.validate_bit = 0x80;
2064 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2065 sensor_work->result = WqRet_fail;
2067 sensor_work->result = WqRet_success;
2070 case WqCmd_af_continues:
2072 if (sensor_af_const(client) < 0)
2073 sensor_work->result = WqRet_fail;
2075 sensor_work->result = WqRet_success;
2078 case WqCmd_af_return_idle:
2080 if (sensor_af_idlechk(client) < 0)
2081 sensor_work->result = WqRet_fail;
2083 sensor_work->result = WqRet_success;
2087 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
2091 if (sensor_work->wait == false) {
2092 kfree((void*)sensor_work);
2094 wake_up(&sensor_work->done);
2096 mutex_unlock(&sensor->wq_lock);
2100 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait)
2102 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2103 struct sensor *sensor = to_sensor(client);
2104 struct sensor_work *wk;
2107 if (sensor->sensor_wq == NULL) {
2109 goto sensor_af_workqueue_set_end;
2112 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) != SENSOR_AF_IS_OK) {
2113 if (cmd != WqCmd_af_init) {
2114 SENSOR_TR("%s %s cmd(%d) ingore,because af module isn't ready!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2116 goto sensor_af_workqueue_set_end;
2120 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
2122 wk->client = client;
2123 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
2125 wk->result = WqRet_inval;
2128 init_waitqueue_head(&wk->done);
2130 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
2132 /* ddl@rock-chips.com:
2133 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
2134 * As a result any other ioctl calls will proceed very, very slowly since each call
2135 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
2136 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
2139 mutex_unlock(&icd->video_lock);
2140 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(2500)) == 0) {
2141 SENSOR_TR("%s %s cmd(%d) is timeout!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2145 mutex_lock(&icd->video_lock);
2149 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2152 sensor_af_workqueue_set_end:
2156 static int sensor_parameter_record(struct i2c_client *client)
2158 u8 ret_l,ret_m,ret_h;
2160 struct sensor *sensor = to_sensor(client);
2162 sensor_write(client,0x3503,0x07); //stop AE/AG
2163 sensor_write(client,0x3406,0x01); //stop AWB
2165 sensor_read(client,0x3500,&ret_h);
2166 sensor_read(client,0x3501, &ret_m);
2167 sensor_read(client,0x3502, &ret_l);
2171 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
2172 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
2173 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
2175 sensor_read(client,0x350c, &ret_h);
2176 sensor_read(client,0x350d, &ret_l);
2177 sensor->parameter.preview_line_width = ret_h & 0xff;
2178 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
2179 //Read back AGC Gain for preview
2180 sensor_read(client,0x350a, &ret_h);
2181 sensor_read(client,0x350b, &tp_l);
2182 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
2184 sensor_read(client,0x380e, &ret_h);
2185 sensor->parameter.preview_maxlines = ret_h;
2186 sensor->parameter.preview_maxlines <<= 8;
2187 sensor_read(client,0x380f, &tp_l);
2188 sensor->parameter.preview_maxlines += tp_l;
2190 sensor->parameter.capture_framerate = 375;
2191 sensor->parameter.preview_framerate = 1500;
2193 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
2194 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2195 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2196 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2197 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2198 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2200 SENSOR_DG(" %s Read 0x350c = 0x%02x 0x350d = 0x%02x 0x350b=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_l, sensor->parameter.preview_gain);
2203 static int sensor_ae_transfer(struct i2c_client *client)
2208 u16 ulCapture_Exposure;
2209 u32 ulCapture_Exposure_Gain;
2213 u8 reg_l = 0,reg_h =0;
2214 u16 Preview_Maxlines;
2216 u32 Capture_MaxLines;
2217 struct sensor *sensor = to_sensor(client);
2219 //Preview_Maxlines = sensor->parameter.preview_line_width;
2220 Preview_Maxlines = sensor->parameter.preview_maxlines;
2221 Gain = sensor->parameter.preview_gain;
2223 sensor_read(client,0x350c, ®_h);
2224 sensor_read(client,0x350d, ®_l);
2225 Capture_MaxLines = reg_h & 0xff;
2226 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2229 sensor_read(client,0x380e, ®_h);
2230 Capture_MaxLines = reg_h;
2231 Capture_MaxLines <<= 8;
2232 sensor_read(client,0x380f, ®_l);
2233 Capture_MaxLines += reg_l;
2236 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2238 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2240 Lines_10ms = Lines_10ms & 0xffff;
2242 if(Preview_Maxlines == 0)
2243 Preview_Maxlines = 1;
2245 //ulCapture_Exposure =
2246 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2248 ulCapture_Exposure =
2249 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2250 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2252 iCapture_Gain = (Gain & 0x0f) + 16;
2254 iCapture_Gain = iCapture_Gain << 1;
2257 iCapture_Gain = iCapture_Gain << 1;
2260 iCapture_Gain = iCapture_Gain << 1;
2263 iCapture_Gain = iCapture_Gain << 1;
2266 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
2267 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2269 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2270 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2271 if (ulCapture_Exposure > Lines_10ms)
2273 //ulCapture_Exposure *= 1.7;
2274 ulCapture_Exposure /= Lines_10ms;
2275 ulCapture_Exposure *= Lines_10ms;
2278 ulCapture_Exposure = Capture_MaxLines;
2279 //ulCapture_Exposure_Gain *= 1.5;
2282 if(ulCapture_Exposure == 0)
2283 ulCapture_Exposure = 1;
2285 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2286 iCapture_Gain = iCapture_Gain & 0xffff;
2288 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2289 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2290 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2294 if (iCapture_Gain > 31) {
2296 iCapture_Gain = iCapture_Gain >> 1;
2298 if (iCapture_Gain > 31) {
2300 iCapture_Gain = iCapture_Gain >> 1;
2302 if (iCapture_Gain > 31) {
2304 iCapture_Gain = iCapture_Gain >> 1;
2306 if (iCapture_Gain > 31) {
2308 iCapture_Gain = iCapture_Gain >> 1;
2310 if (iCapture_Gain > 16)
2311 Gain |= ((iCapture_Gain -16) & 0x0f);
2314 // write the gain and exposure to 0x350* registers
2315 //m_iWrite0x350b=Gain;
2316 sensor_write(client,0x350b, Gain);
2317 Gain = (Gain >> 8) & 0x01;
2318 sensor_write(client,0x350a, Gain);
2319 //m_iWrite0x3502=ExposureLow;
2320 sensor_write(client,0x3502, ExposureLow);
2321 //m_iWrite0x3501=ExposureMid;
2322 sensor_write(client,0x3501, ExposureMid);
2323 //m_iWrite0x3500=ExposureHigh;
2324 sensor_write(client,0x3500, ExposureHigh);
2325 // SendToFile("Gain = 0x%x\r\n", Gain);
2326 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2327 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2328 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2329 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2330 //camera_timed_wait(200);
2331 //linzhk camera_timed_wait(500);
2333 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2334 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2335 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2336 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2337 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2338 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2340 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2344 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2346 struct soc_camera_link *icl = to_soc_camera_link(icd);
2349 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2353 case Sensor_PowerDown:
2355 if (icl->powerdown) {
2356 ret = icl->powerdown(icd->pdev, on);
2357 if (ret == RK29_CAM_IO_SUCCESS) {
2361 icl->reset(icd->pdev);
2363 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2365 goto sensor_power_end;
2372 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2373 struct sensor *sensor = to_sensor(client);
2375 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2376 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2378 //flash off after 2 secs
2379 hrtimer_cancel(&(flash_off_timer.timer));
2380 hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
2387 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2396 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
2397 struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
2398 sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
2399 SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
2404 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2406 struct i2c_client *client = v4l2_get_subdevdata(sd);
2407 struct soc_camera_device *icd = client->dev.platform_data;
2408 struct sensor *sensor = to_sensor(client);
2409 const struct v4l2_queryctrl *qctrl;
2410 const struct sensor_datafmt *fmt;
2414 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2416 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2418 goto sensor_INIT_ERR;
2422 if (sensor_task_lock(client,1)<0)
2423 goto sensor_INIT_ERR;
2424 ret = sensor_write(client, 0x3008, 0x80);
2426 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2428 goto sensor_INIT_ERR;
2431 mdelay(5); //delay 5 microseconds
2432 /* check if it is an sensor sensor */
2433 ret = sensor_read(client, 0x300a, &value);
2435 SENSOR_TR("read chip id high byte failed\n");
2437 goto sensor_INIT_ERR;
2440 pid |= (value << 8);
2442 ret = sensor_read(client, 0x300b, &value);
2444 SENSOR_TR("read chip id low byte failed\n");
2446 goto sensor_INIT_ERR;
2449 pid |= (value & 0xff);
2450 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2452 if (pid == SENSOR_ID) {
2453 sensor->model = SENSOR_V4L2_IDENT;
2455 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2457 goto sensor_INIT_ERR;
2460 ret = sensor_write_array(client, sensor_init_data);
2462 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2463 goto sensor_INIT_ERR;
2465 sensor_task_lock(client,0);
2466 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2467 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2469 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2471 goto sensor_INIT_ERR;
2473 sensor->info_priv.fmt = *fmt;
2475 /* sensor sensor information for initialization */
2476 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2478 sensor->info_priv.whiteBalance = qctrl->default_value;
2479 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2481 sensor->info_priv.brightness = qctrl->default_value;
2482 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2484 sensor->info_priv.effect = qctrl->default_value;
2485 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2487 sensor->info_priv.exposure = qctrl->default_value;
2489 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2491 sensor->info_priv.saturation = qctrl->default_value;
2492 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2494 sensor->info_priv.contrast = qctrl->default_value;
2495 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2497 sensor->info_priv.mirror = qctrl->default_value;
2498 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2500 sensor->info_priv.flip = qctrl->default_value;
2501 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2503 sensor->info_priv.scene = qctrl->default_value;
2504 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2506 sensor->info_priv.digitalzoom = qctrl->default_value;
2508 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2509 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2511 sensor->info_priv.focus = qctrl->default_value;
2513 #if CONFIG_SENSOR_Flash
2514 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2516 sensor->info_priv.flash = qctrl->default_value;
2517 flash_off_timer.icd = icd;
2518 flash_off_timer.timer.function = flash_off_func;
2520 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);
2522 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
2526 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2527 sensor_task_lock(client,0);
2528 sensor_deactivate(client);
2531 static int sensor_deactivate(struct i2c_client *client)
2533 struct soc_camera_device *icd = client->dev.platform_data;
2534 struct sensor *sensor = to_sensor(client);
2536 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2538 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2539 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2540 sensor_task_lock(client, 1);
2541 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2542 sensor_write(client, 0x3018, 0x03); // D5-D0
2543 sensor_write(client,0x3019,0x00); // STROBE,SDA
2544 sensor_task_lock(client, 0);
2546 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2548 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2549 icd->user_width = SENSOR_INIT_WIDTH;
2550 icd->user_height = SENSOR_INIT_HEIGHT;
2551 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2555 static struct reginfo sensor_power_down_sequence[]=
2560 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2563 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2565 if (pm_msg.event == PM_EVENT_SUSPEND) {
2566 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2567 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2569 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2572 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2574 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2579 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2586 static int sensor_resume(struct soc_camera_device *icd)
2590 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2592 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2596 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2600 static int sensor_set_bus_param(struct soc_camera_device *icd,
2601 unsigned long flags)
2607 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2609 struct soc_camera_link *icl = to_soc_camera_link(icd);
2610 unsigned long flags = SENSOR_BUS_PARAM;
2612 return soc_camera_apply_sensor_flags(icl, flags);
2615 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2617 struct i2c_client *client = v4l2_get_subdevdata(sd);
2618 struct soc_camera_device *icd = client->dev.platform_data;
2619 struct sensor *sensor = to_sensor(client);
2621 mf->width = icd->user_width;
2622 mf->height = icd->user_height;
2623 mf->code = sensor->info_priv.fmt.code;
2624 mf->colorspace = sensor->info_priv.fmt.colorspace;
2625 mf->field = V4L2_FIELD_NONE;
2629 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2633 if ((mf->width == 1024) && (mf->height == 768)) {
2635 } else if ((mf->width == 1280) && (mf->height == 1024)) {
2637 } else if ((mf->width == 1600) && (mf->height == 1200)) {
2639 } else if ((mf->width == 2048) && (mf->height == 1536)) {
2641 } else if ((mf->width == 2592) && (mf->height == 1944)) {
2646 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2650 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2654 if ((mf->width == 1280) && (mf->height == 720)) {
2656 } else if ((mf->width == 1920) && (mf->height == 1080)) {
2661 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2664 static int sensor_s_fmt(struct v4l2_subdev *sd,struct v4l2_mbus_framefmt *mf)
2666 struct i2c_client *client = v4l2_get_subdevdata(sd);
2667 const struct sensor_datafmt *fmt;
2668 struct sensor *sensor = to_sensor(client);
2669 const struct v4l2_queryctrl *qctrl;
2670 struct soc_camera_device *icd = client->dev.platform_data;
2671 struct reginfo *winseqe_set_addr=NULL;
2672 int ret = 0, set_w,set_h;
2674 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2675 ARRAY_SIZE(sensor_colour_fmts));
2678 goto sensor_s_fmt_end;
2681 if (sensor->info_priv.fmt.code != mf->code) {
2684 case V4L2_MBUS_FMT_YUYV8_2X8:
2686 winseqe_set_addr = sensor_ClrFmt_YUYV;
2689 case V4L2_MBUS_FMT_UYVY8_2X8:
2691 winseqe_set_addr = sensor_ClrFmt_UYVY;
2697 if (winseqe_set_addr != NULL) {
2698 sensor_write_array(client, winseqe_set_addr);
2699 sensor->info_priv.fmt.code = mf->code;
2700 sensor->info_priv.fmt.colorspace= mf->colorspace;
2701 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2703 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2710 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2712 winseqe_set_addr = sensor_qcif;
2716 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2718 winseqe_set_addr = sensor_qvga;
2722 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2724 winseqe_set_addr = sensor_cif;
2728 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2730 winseqe_set_addr = sensor_vga;
2734 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2736 winseqe_set_addr = sensor_svga;
2740 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2742 winseqe_set_addr = sensor_xga;
2746 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2748 winseqe_set_addr = sensor_720p;
2752 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2754 winseqe_set_addr = sensor_sxga;
2758 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2760 winseqe_set_addr = sensor_uxga;
2764 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2766 winseqe_set_addr = sensor_1080p;
2770 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2772 winseqe_set_addr = sensor_qxga;
2776 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2778 winseqe_set_addr = sensor_qsxga;
2784 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2785 set_w = SENSOR_INIT_WIDTH;
2786 set_h = SENSOR_INIT_HEIGHT;
2787 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2790 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2792 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2793 sensor_parameter_record(client);
2794 /*#if CONFIG_SENSOR_Focus
2795 sensor_af_idlechk(client);
2796 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2797 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2799 #if CONFIG_SENSOR_Flash
2800 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2801 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2802 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2805 }else { /* ddl@rock-chips.com : Video */
2806 #if CONFIG_SENSOR_Flash
2807 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2808 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2809 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2813 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2814 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2815 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2816 ret |= sensor_write_array(client,sensor_init_data);
2817 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2820 ret |= sensor_write_array(client, winseqe_set_addr);
2822 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2823 #if CONFIG_SENSOR_Flash
2824 if (sensor_fmt_capturechk(sd,mf) == true) {
2825 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2826 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2827 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2831 goto sensor_s_fmt_end;
2833 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2835 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2836 sensor_ae_transfer(client);
2837 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2838 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2839 if (sensor->info_priv.whiteBalance != 0) {
2840 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2841 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2843 sensor->info_priv.snap2preview = true;
2844 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2845 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2846 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2848 sensor->info_priv.video2preview = true;
2849 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2850 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2851 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2852 if (sensor->info_priv.snap2preview == true) {
2853 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2854 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2856 #if CONFIG_SENSOR_Focus
2857 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
2858 sensor_af_workqueue_set(icd,WqCmd_af_return_idle,0,true);
2866 sensor->info_priv.video2preview = false;
2867 sensor->info_priv.snap2preview = false;
2869 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2873 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2881 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2883 struct i2c_client *client = v4l2_get_subdevdata(sd);
2884 struct sensor *sensor = to_sensor(client);
2885 const struct sensor_datafmt *fmt;
2886 int ret = 0,set_w,set_h;
2888 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2889 ARRAY_SIZE(sensor_colour_fmts));
2891 fmt = &sensor->info_priv.fmt;
2892 mf->code = fmt->code;
2895 if (mf->height > SENSOR_MAX_HEIGHT)
2896 mf->height = SENSOR_MAX_HEIGHT;
2897 else if (mf->height < SENSOR_MIN_HEIGHT)
2898 mf->height = SENSOR_MIN_HEIGHT;
2900 if (mf->width > SENSOR_MAX_WIDTH)
2901 mf->width = SENSOR_MAX_WIDTH;
2902 else if (mf->width < SENSOR_MIN_WIDTH)
2903 mf->width = SENSOR_MIN_WIDTH;
2908 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2913 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2918 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2923 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2928 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2933 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2938 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2943 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2948 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2953 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2958 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2963 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2970 set_w = SENSOR_INIT_WIDTH;
2971 set_h = SENSOR_INIT_HEIGHT;
2977 mf->colorspace = fmt->colorspace;
2982 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2984 struct i2c_client *client = v4l2_get_subdevdata(sd);
2986 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2989 if (id->match.addr != client->addr)
2992 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2997 #if CONFIG_SENSOR_Brightness
2998 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3000 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3002 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3004 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
3006 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
3008 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3011 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3015 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3019 #if CONFIG_SENSOR_Effect
3020 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3022 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3024 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3026 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
3028 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
3030 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3033 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3037 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3041 #if CONFIG_SENSOR_Exposure
3042 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3044 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3046 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3048 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
3050 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3052 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3055 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3059 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3063 #if CONFIG_SENSOR_Saturation
3064 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3066 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3068 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3070 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3072 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3074 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3077 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3081 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3085 #if CONFIG_SENSOR_Contrast
3086 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3088 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3090 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3092 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3094 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3096 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3099 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3103 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3107 #if CONFIG_SENSOR_Mirror
3108 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3110 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3112 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3114 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3116 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3118 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3121 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3125 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3129 #if CONFIG_SENSOR_Flip
3130 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3132 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3134 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3136 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3138 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3140 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3143 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3147 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3151 #if CONFIG_SENSOR_Scene
3152 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3154 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3156 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3158 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3160 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3162 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3165 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3169 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3173 #if CONFIG_SENSOR_WhiteBalance
3174 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3176 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3178 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3180 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3182 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3184 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3187 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3191 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3195 #if CONFIG_SENSOR_DigitalZoom
3196 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3198 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3199 struct sensor *sensor = to_sensor(client);
3200 const struct v4l2_queryctrl *qctrl_info;
3201 int digitalzoom_cur, digitalzoom_total;
3203 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3207 digitalzoom_cur = sensor->info_priv.digitalzoom;
3208 digitalzoom_total = qctrl_info->maximum;
3210 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3212 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3216 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3218 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3222 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3224 *value = digitalzoom_total - digitalzoom_cur;
3227 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3229 *value = 0 - digitalzoom_cur;
3232 digitalzoom_cur += *value;
3234 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3236 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3238 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3241 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3248 #if CONFIG_SENSOR_Focus
3249 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3251 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3252 struct sensor *sensor = to_sensor(client);
3253 const struct v4l2_queryctrl *qctrl_info;
3256 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3260 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3261 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3262 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true);
3263 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3266 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3270 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3271 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3276 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3278 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3279 struct sensor *sensor = to_sensor(client);
3280 const struct v4l2_queryctrl *qctrl_info;
3283 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3287 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3288 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3290 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true);
3292 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true);
3294 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3297 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3301 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3302 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3307 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3309 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3310 struct sensor *sensor = to_sensor(client);
3313 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3316 case SENSOR_AF_MODE_AUTO:
3318 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true);
3322 /*case SENSOR_AF_MODE_MACRO:
3324 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3328 case SENSOR_AF_MODE_INFINITY:
3330 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3334 case SENSOR_AF_MODE_CONTINUOUS:
3336 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true);
3340 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3345 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3348 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3349 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3355 #if CONFIG_SENSOR_Flash
3356 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3358 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3359 if (value == 3) { /* ddl@rock-chips.com: torch */
3360 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3362 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3364 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3368 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3372 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3374 struct i2c_client *client = v4l2_get_subdevdata(sd);
3375 struct sensor *sensor = to_sensor(client);
3376 const struct v4l2_queryctrl *qctrl;
3378 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3382 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3388 case V4L2_CID_BRIGHTNESS:
3390 ctrl->value = sensor->info_priv.brightness;
3393 case V4L2_CID_SATURATION:
3395 ctrl->value = sensor->info_priv.saturation;
3398 case V4L2_CID_CONTRAST:
3400 ctrl->value = sensor->info_priv.contrast;
3403 case V4L2_CID_DO_WHITE_BALANCE:
3405 ctrl->value = sensor->info_priv.whiteBalance;
3408 case V4L2_CID_EXPOSURE:
3410 ctrl->value = sensor->info_priv.exposure;
3413 case V4L2_CID_HFLIP:
3415 ctrl->value = sensor->info_priv.mirror;
3418 case V4L2_CID_VFLIP:
3420 ctrl->value = sensor->info_priv.flip;
3431 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3433 struct i2c_client *client = v4l2_get_subdevdata(sd);
3434 struct sensor *sensor = to_sensor(client);
3435 struct soc_camera_device *icd = client->dev.platform_data;
3436 const struct v4l2_queryctrl *qctrl;
3439 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3443 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3449 #if CONFIG_SENSOR_Brightness
3450 case V4L2_CID_BRIGHTNESS:
3452 if (ctrl->value != sensor->info_priv.brightness)
3454 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3458 sensor->info_priv.brightness = ctrl->value;
3463 #if CONFIG_SENSOR_Exposure
3464 case V4L2_CID_EXPOSURE:
3466 if (ctrl->value != sensor->info_priv.exposure)
3468 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3472 sensor->info_priv.exposure = ctrl->value;
3477 #if CONFIG_SENSOR_Saturation
3478 case V4L2_CID_SATURATION:
3480 if (ctrl->value != sensor->info_priv.saturation)
3482 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3486 sensor->info_priv.saturation = ctrl->value;
3491 #if CONFIG_SENSOR_Contrast
3492 case V4L2_CID_CONTRAST:
3494 if (ctrl->value != sensor->info_priv.contrast)
3496 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3500 sensor->info_priv.contrast = ctrl->value;
3505 #if CONFIG_SENSOR_WhiteBalance
3506 case V4L2_CID_DO_WHITE_BALANCE:
3508 if (ctrl->value != sensor->info_priv.whiteBalance)
3510 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3514 sensor->info_priv.whiteBalance = ctrl->value;
3519 #if CONFIG_SENSOR_Mirror
3520 case V4L2_CID_HFLIP:
3522 if (ctrl->value != sensor->info_priv.mirror)
3524 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3526 sensor->info_priv.mirror = ctrl->value;
3531 #if CONFIG_SENSOR_Flip
3532 case V4L2_CID_VFLIP:
3534 if (ctrl->value != sensor->info_priv.flip)
3536 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3538 sensor->info_priv.flip = ctrl->value;
3549 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3551 const struct v4l2_queryctrl *qctrl;
3552 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3553 struct sensor *sensor = to_sensor(client);
3555 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3559 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3563 switch (ext_ctrl->id)
3565 case V4L2_CID_SCENE:
3567 ext_ctrl->value = sensor->info_priv.scene;
3570 case V4L2_CID_EFFECT:
3572 ext_ctrl->value = sensor->info_priv.effect;
3575 case V4L2_CID_ZOOM_ABSOLUTE:
3577 ext_ctrl->value = sensor->info_priv.digitalzoom;
3580 case V4L2_CID_ZOOM_RELATIVE:
3584 case V4L2_CID_FOCUS_ABSOLUTE:
3588 case V4L2_CID_FOCUS_RELATIVE:
3592 case V4L2_CID_FLASH:
3594 ext_ctrl->value = sensor->info_priv.flash;
3602 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3604 const struct v4l2_queryctrl *qctrl;
3605 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3606 struct sensor *sensor = to_sensor(client);
3609 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3613 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3618 switch (ext_ctrl->id)
3620 #if CONFIG_SENSOR_Scene
3621 case V4L2_CID_SCENE:
3623 if (ext_ctrl->value != sensor->info_priv.scene)
3625 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3627 sensor->info_priv.scene = ext_ctrl->value;
3632 #if CONFIG_SENSOR_Effect
3633 case V4L2_CID_EFFECT:
3635 if (ext_ctrl->value != sensor->info_priv.effect)
3637 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3639 sensor->info_priv.effect= ext_ctrl->value;
3644 #if CONFIG_SENSOR_DigitalZoom
3645 case V4L2_CID_ZOOM_ABSOLUTE:
3647 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3650 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3652 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3654 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3656 sensor->info_priv.digitalzoom += val_offset;
3658 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3663 case V4L2_CID_ZOOM_RELATIVE:
3665 if (ext_ctrl->value)
3667 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3669 sensor->info_priv.digitalzoom += ext_ctrl->value;
3671 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3676 #if CONFIG_SENSOR_Focus
3677 case V4L2_CID_FOCUS_ABSOLUTE:
3679 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3684 case V4L2_CID_FOCUS_RELATIVE:
3686 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3689 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3692 case V4L2_CID_FOCUS_AUTO:
3694 if (ext_ctrl->value == 1) {
3695 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
3696 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
3697 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3701 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3702 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3703 if (ext_ctrl->value == 0)
3704 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3708 case V4L2_CID_FOCUS_CONTINUOUS:
3710 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3711 if (ext_ctrl->value == 1) {
3712 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3714 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3717 if (ext_ctrl->value == 0)
3718 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3723 #if CONFIG_SENSOR_Flash
3724 case V4L2_CID_FLASH:
3726 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3728 sensor->info_priv.flash = ext_ctrl->value;
3730 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3741 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3743 struct i2c_client *client = v4l2_get_subdevdata(sd);
3744 struct soc_camera_device *icd = client->dev.platform_data;
3745 int i, error_cnt=0, error_idx=-1;
3748 for (i=0; i<ext_ctrl->count; i++) {
3749 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3756 error_idx = ext_ctrl->count;
3758 if (error_idx != -1) {
3759 ext_ctrl->error_idx = error_idx;
3766 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3768 struct i2c_client *client = v4l2_get_subdevdata(sd);
3769 struct soc_camera_device *icd = client->dev.platform_data;
3770 int i, error_cnt=0, error_idx=-1;
3772 for (i=0; i<ext_ctrl->count; i++) {
3773 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3780 error_idx = ext_ctrl->count;
3782 if (error_idx != -1) {
3783 ext_ctrl->error_idx = error_idx;
3790 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3792 struct i2c_client *client = v4l2_get_subdevdata(sd);
3793 struct sensor *sensor = to_sensor(client);
3794 #if CONFIG_SENSOR_Focus
3795 struct soc_camera_device *icd = client->dev.platform_data;
3796 struct v4l2_mbus_framefmt mf;
3800 sensor->info_priv.enable = 1;
3801 #if CONFIG_SENSOR_Focus
3802 mf.width = icd->user_width;
3803 mf.height = icd->user_height;
3804 mf.code = sensor->info_priv.fmt.code;
3805 mf.colorspace = sensor->info_priv.fmt.colorspace;
3806 mf.field = V4L2_FIELD_NONE;
3807 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3808 if (sensor_fmt_capturechk(sd, &mf) == false) {
3809 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3810 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false);
3811 sensor->info_priv.affm_reinit = 0;
3815 } else if (enable == 0) {
3816 sensor->info_priv.enable = 0;
3817 #if CONFIG_SENSOR_Focus
3818 flush_workqueue(sensor->sensor_wq);
3824 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3825 * this wasn't our capture interface, so, we wait for the right one */
3826 static int sensor_video_probe(struct soc_camera_device *icd,
3827 struct i2c_client *client)
3831 struct sensor *sensor = to_sensor(client);
3833 /* We must have a parent by now. And it cannot be a wrong one.
3834 * So this entire test is completely redundant. */
3835 if (!icd->dev.parent ||
3836 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3839 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3841 goto sensor_video_probe_err;
3844 ret = sensor_write(client, 0x3008, 0x80);
3846 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3848 goto sensor_video_probe_err;
3850 mdelay(5); //delay 5 microseconds
3852 /* check if it is an sensor sensor */
3853 ret = sensor_read(client, 0x300a, &value);
3855 SENSOR_TR("read chip id high byte failed\n");
3857 goto sensor_video_probe_err;
3860 pid |= (value << 8);
3862 ret = sensor_read(client, 0x300b, &value);
3864 SENSOR_TR("read chip id low byte failed\n");
3866 goto sensor_video_probe_err;
3869 pid |= (value & 0xff);
3870 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3871 if (pid == SENSOR_ID) {
3872 sensor->model = SENSOR_V4L2_IDENT;
3874 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3876 goto sensor_video_probe_err;
3881 sensor_video_probe_err:
3884 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3886 struct i2c_client *client = v4l2_get_subdevdata(sd);
3887 struct soc_camera_device *icd = client->dev.platform_data;
3888 struct sensor *sensor = to_sensor(client);
3891 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3894 case RK29_CAM_SUBDEV_DEACTIVATE:
3896 sensor_deactivate(client);
3899 case RK29_CAM_SUBDEV_IOREQUEST:
3901 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3902 if (sensor->sensor_io_request != NULL) {
3903 sensor->sensor_gpio_res = NULL;
3904 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
3905 if (sensor->sensor_io_request->gpio_res[i].dev_name &&
3906 (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
3907 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
3910 if (sensor->sensor_gpio_res == NULL) {
3911 SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
3913 goto sensor_ioctl_end;
3916 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3918 goto sensor_ioctl_end;
3920 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3922 #if CONFIG_SENSOR_Flash
3923 if (sensor->sensor_gpio_res) {
3924 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3925 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3926 for (i = 0; i < icd->ops->num_controls; i++) {
3927 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3928 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3929 sensor_controls[i].id=0xffff;
3932 sensor->info_priv.flash = 0xff;
3933 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3934 }else{ //two cameras are the same,need to deal diffrently ,zyc
3935 for (i = 0; i < icd->ops->num_controls; i++) {
3936 if(0xffff == icd->ops->controls[i].id){
3937 sensor_controls[i].id=V4L2_CID_FLASH;
3947 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3956 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3957 enum v4l2_mbus_pixelcode *code)
3959 if (index >= ARRAY_SIZE(sensor_colour_fmts))
3962 *code = sensor_colour_fmts[index].code;
3965 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3966 .init = sensor_init,
3967 .g_ctrl = sensor_g_control,
3968 .s_ctrl = sensor_s_control,
3969 .g_ext_ctrls = sensor_g_ext_controls,
3970 .s_ext_ctrls = sensor_s_ext_controls,
3971 .g_chip_ident = sensor_g_chip_ident,
3972 .ioctl = sensor_ioctl,
3975 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3976 .s_mbus_fmt = sensor_s_fmt,
3977 .g_mbus_fmt = sensor_g_fmt,
3978 .try_mbus_fmt = sensor_try_fmt,
3979 .enum_mbus_fmt = sensor_enum_fmt,
3980 .s_stream = sensor_s_stream,
3983 static struct v4l2_subdev_ops sensor_subdev_ops = {
3984 .core = &sensor_subdev_core_ops,
3985 .video = &sensor_subdev_video_ops,
3988 static int sensor_probe(struct i2c_client *client,
3989 const struct i2c_device_id *did)
3991 struct sensor *sensor;
3992 struct soc_camera_device *icd = client->dev.platform_data;
3993 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3994 struct soc_camera_link *icl;
3997 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3999 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
4003 icl = to_soc_camera_link(icd);
4005 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
4009 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
4010 dev_warn(&adapter->dev,
4011 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
4015 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
4019 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
4021 /* Second stage probe - when a capture adapter is there */
4022 icd->ops = &sensor_ops;
4023 sensor->info_priv.fmt = sensor_colour_fmts[0];
4024 #if CONFIG_SENSOR_I2C_NOSCHED
4025 atomic_set(&sensor->tasklock_cnt,0);
4028 ret = sensor_video_probe(icd, client);
4031 i2c_set_clientdata(client, NULL);
4035 #if CONFIG_SENSOR_Focus
4036 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
4037 if (sensor->sensor_wq == NULL)
4038 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
4039 mutex_init(&sensor->wq_lock);
4042 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4043 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
4047 static int sensor_remove(struct i2c_client *client)
4049 struct sensor *sensor = to_sensor(client);
4050 struct soc_camera_device *icd = client->dev.platform_data;
4052 #if CONFIG_SENSOR_Focus
4053 if (sensor->sensor_wq) {
4054 destroy_workqueue(sensor->sensor_wq);
4055 sensor->sensor_wq = NULL;
4060 i2c_set_clientdata(client, NULL);
4061 client->driver = NULL;
4067 static const struct i2c_device_id sensor_id[] = {
4068 {SENSOR_NAME_STRING(), 0 },
4071 MODULE_DEVICE_TABLE(i2c, sensor_id);
4073 static struct i2c_driver sensor_i2c_driver = {
4075 .name = SENSOR_NAME_STRING(),
4077 .probe = sensor_probe,
4078 .remove = sensor_remove,
4079 .id_table = sensor_id,
4082 static int __init sensor_mod_init(void)
4084 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4085 return i2c_add_driver(&sensor_i2c_driver);
4088 static void __exit sensor_mod_exit(void)
4090 i2c_del_driver(&sensor_i2c_driver);
4093 device_initcall_sync(sensor_mod_init);
4094 module_exit(sensor_mod_exit);
4096 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4097 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4098 MODULE_LICENSE("GPL");