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 #define CONFIG_SENSOR_FocusCenterInCapture 0
72 #define CONFIG_SENSOR_FocusContinues 0
73 #include "ov5640_af_firmware.c"
75 #define CONFIG_SENSOR_Focus 0
78 #define CONFIG_SENSOR_I2C_SPEED 100000 /* Hz */
79 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
80 #define CONFIG_SENSOR_I2C_NOSCHED 0
81 #define CONFIG_SENSOR_I2C_RDWRCHK 0
83 #define CONFIG_SENSOR_WRITE_REGS 0
84 #define WRITE_REGS_NUM 3
86 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
87 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
88 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
90 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
91 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
92 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
93 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
94 #define COLOR_TEMPERATURE_OFFICE_DN 3500
95 #define COLOR_TEMPERATURE_OFFICE_UP 5000
96 #define COLOR_TEMPERATURE_HOME_DN 2500
97 #define COLOR_TEMPERATURE_HOME_UP 3500
99 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
100 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
102 #define SENSOR_AF_IS_ERR (0x00<<0)
103 #define SENSOR_AF_IS_OK (0x01<<0)
104 #define SENSOR_INIT_IS_ERR (0x00<<28)
105 #define SENSOR_INIT_IS_OK (0x01<<28)
107 #if CONFIG_SENSOR_Focus
108 /*#define SENSOR_AF_MODE_INFINITY 0
109 #define SENSOR_AF_MODE_MACRO 1
110 #define SENSOR_AF_MODE_FIXED 2
111 #define SENSOR_AF_MODE_AUTO 3
112 #define SENSOR_AF_MODE_CONTINUOUS 4
113 #define SENSOR_AF_MODE_CLOSE 5*/
114 #define SENSOR_AF_MODE_AUTO 0
115 #define SENSOR_AF_MODE_CLOSE 1
116 #define SENSOR_AF_MODE_CONTINUOUS 2
119 #if CONFIG_SENSOR_Focus
120 /* ov5640 VCM Command and Status Registers */
121 #define CMD_MAIN_Reg 0x3022
122 //#define CMD_TAG_Reg 0x3023
123 #define CMD_ACK_Reg 0x3023
124 #define CMD_PARA0_Reg 0x3024
125 #define CMD_PARA1_Reg 0x3025
126 #define CMD_PARA2_Reg 0x3026
127 #define CMD_PARA3_Reg 0x3027
128 #define CMD_PARA4_Reg 0x3028
130 //#define STA_ZONE_Reg 0x3026
131 #define STA_FOCUS_Reg 0x3029
133 /* ov5640 VCM Command */
135 #define ConstFocus_Cmd 0x04
136 #define StepMode_Cmd 0x05
137 #define PauseFocus_Cmd 0x06
138 #define ReturnIdle_Cmd 0x08
139 #define SetZone_Cmd 0x10
140 #define UpdateZone_Cmd 0x12
141 #define SetMotor_Cmd 0x20
142 #define SingleFocus_Cmd 0x03
143 #define GetFocusResult_Cmd 0x07
144 #define ReleaseFocus_Cmd 0x08
145 #define ZoneRelaunch_Cmd 0x12
146 #define DefaultZoneConfig_Cmd 0x80
147 #define TouchZoneConfig_Cmd 0x81
148 #define CustomZoneConfig_Cmd 0x8f
151 /* ov5640 Focus State */
152 //#define S_FIRWRE 0xFF /*Firmware is downloaded and not run*/
153 #define S_STARTUP 0x7e /*Firmware is initializing*/
155 #define S_IDLE 0x70 /*Idle state, focus is released; lens is located at the furthest position.*/
156 #define S_FOCUSING 0x00 /*Auto Focus is running.*/
157 #define S_FOCUSED 0x10 /*Auto Focus is completed.*/
159 #define S_CAPTURE 0x12
162 /* ov5640 Zone State */
163 #define Zone_Is_Focused(a, zone_val) (zone_val&(1<<(a-3)))
164 #define Zone_Get_ID(zone_val) (zone_val&0x03)
166 #define Zone_CenterMode 0x01
167 #define Zone_5xMode 0x02
168 #define Zone_5PlusMode 0x03
169 #define Zone_4fMode 0x04
171 #define ZoneSel_Auto 0x0b
172 #define ZoneSel_SemiAuto 0x0c
173 #define ZoneSel_Manual 0x0d
174 #define ZoneSel_Rotate 0x0e
176 /* ov5640 Step Focus Commands */
177 #define StepFocus_Near_Tag 0x01
178 #define StepFocus_Far_Tag 0x02
179 #define StepFocus_Furthest_Tag 0x03
180 #define StepFocus_Nearest_Tag 0x04
181 #define StepFocus_Spec_Tag 0x10
184 //flash off in fixed time to prevent from too hot , zyc
186 struct soc_camera_device *icd;
187 struct hrtimer timer;
189 static enum hrtimer_restart flash_off_func(struct hrtimer *timer);
191 static struct flash_timer flash_off_timer;
192 //for user defined if user want to customize the series , zyc
193 #ifdef CONFIG_OV5640_USER_DEFINED_SERIES
194 #include "ov5640_user_series.c"
197 /* init 800X600 SVGA */
198 static struct reginfo sensor_init_data[] =
458 /* 720p 15fps @ 1280x720 */
460 static struct reginfo sensor_720p[]=
488 {0x3820, 0x41}, //ddl@rock-chips.com add start: qsxvga -> 720p isn't stream on
509 {0x4713, 0x03},//////ddl@rock-chips.com add end
525 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
527 static struct reginfo sensor_1080p[]=
533 /* 2592X1944 QSXGA */
534 static struct reginfo sensor_qsxga[] =
615 static struct reginfo sensor_qxga[] =
658 static struct reginfo sensor_uxga[] =
701 static struct reginfo sensor_sxga[] =
743 static struct reginfo sensor_xga[] =
785 static struct reginfo sensor_svga[] =
831 static struct reginfo sensor_vga[] =
837 static struct reginfo sensor_cif[] =
844 static struct reginfo sensor_qvga[] =
851 static struct reginfo sensor_qcif[] =
857 static struct reginfo sensor_ClrFmt_YUYV[]=
863 static struct reginfo sensor_ClrFmt_UYVY[]=
870 #if CONFIG_SENSOR_WhiteBalance
871 static struct reginfo sensor_WhiteB_Auto[]=
898 /* Cloudy Colour Temperature : 6500K - 8000K */
899 static struct reginfo sensor_WhiteB_Cloudy[]=
910 /* ClearDay Colour Temperature : 5000K - 6500K */
911 static struct reginfo sensor_WhiteB_ClearDay[]=
922 /* Office Colour Temperature : 3500K - 5000K */
923 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
934 /* Home Colour Temperature : 2500K - 3500K */
935 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
946 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
947 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
951 #if CONFIG_SENSOR_Brightness
952 static struct reginfo sensor_Brightness0[]=
956 static struct reginfo sensor_Brightness1[]=
961 static struct reginfo sensor_Brightness2[]=
965 static struct reginfo sensor_Brightness3[]=
969 static struct reginfo sensor_Brightness4[]=
974 static struct reginfo sensor_Brightness5[]=
978 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
979 sensor_Brightness4, sensor_Brightness5,NULL,
984 #if CONFIG_SENSOR_Effect
985 static struct reginfo sensor_Effect_Normal[] =
991 static struct reginfo sensor_Effect_WandB[] =
999 static struct reginfo sensor_Effect_Sepia[] =
1005 {SEQUENCE_END, 0x00}
1008 static struct reginfo sensor_Effect_Negative[] =
1013 {SEQUENCE_END, 0x00}
1014 };static struct reginfo sensor_Effect_Bluish[] =
1021 {SEQUENCE_END, 0x00}
1024 static struct reginfo sensor_Effect_Green[] =
1031 {SEQUENCE_END, 0x00}
1033 /*static struct reginfo sensor_Effect_Reddish[] =
1040 {SEQUENCE_END, 0x00}
1043 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
1044 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
1047 #if CONFIG_SENSOR_Exposure
1048 static struct reginfo sensor_Exposure0[]=
1056 {SEQUENCE_END, 0x00}
1058 static struct reginfo sensor_Exposure1[]=
1066 {SEQUENCE_END, 0x00}
1068 static struct reginfo sensor_Exposure2[]=
1076 {SEQUENCE_END, 0x00}
1079 static struct reginfo sensor_Exposure3[]=
1087 {SEQUENCE_END, 0x00}
1089 static struct reginfo sensor_Exposure4[]=
1097 {SEQUENCE_END, 0x00}
1099 static struct reginfo sensor_Exposure5[]=
1107 {SEQUENCE_END, 0x00}
1109 static struct reginfo sensor_Exposure6[]=
1117 {SEQUENCE_END, 0x00}
1119 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1120 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1123 #if CONFIG_SENSOR_Saturation
1124 static struct reginfo sensor_Saturation0[]=
1126 {SEQUENCE_END, 0x00}
1129 static struct reginfo sensor_Saturation1[]=
1131 {SEQUENCE_END, 0x00}
1134 static struct reginfo sensor_Saturation2[]=
1136 {SEQUENCE_END, 0x00}
1137 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1140 #if CONFIG_SENSOR_Contrast
1141 static struct reginfo sensor_Contrast0[]=
1143 {SEQUENCE_END, 0x00}
1146 static struct reginfo sensor_Contrast1[]=
1148 {SEQUENCE_END, 0x00}
1150 static struct reginfo sensor_Contrast2[]=
1152 {SEQUENCE_END, 0x00}
1155 static struct reginfo sensor_Contrast3[]=
1157 {SEQUENCE_END, 0x00}
1160 static struct reginfo sensor_Contrast4[]=
1162 {SEQUENCE_END, 0x00}
1166 static struct reginfo sensor_Contrast5[]=
1168 {SEQUENCE_END, 0x00}
1171 static struct reginfo sensor_Contrast6[]=
1173 {SEQUENCE_END, 0x00}
1175 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1176 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1180 #if CONFIG_SENSOR_Mirror
1181 static struct reginfo sensor_MirrorOn[]=
1183 {SEQUENCE_END, 0x00}
1185 static struct reginfo sensor_MirrorOff[]=
1187 {SEQUENCE_END, 0x00}
1189 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1191 #if CONFIG_SENSOR_Flip
1192 static struct reginfo sensor_FlipOn[]=
1194 {SEQUENCE_END, 0x00}
1197 static struct reginfo sensor_FlipOff[]=
1199 {SEQUENCE_END, 0x00}
1201 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1204 #if CONFIG_SENSOR_Scene
1205 static struct reginfo sensor_SceneAuto[] =
1208 {SEQUENCE_END, 0x00}
1210 static struct reginfo sensor_SceneNight[] =
1212 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1230 {SEQUENCE_END, 0x00}
1232 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1235 #if CONFIG_SENSOR_DigitalZoom
1236 static struct reginfo sensor_Zoom0[] =
1238 {SEQUENCE_END, 0x00}
1240 static struct reginfo sensor_Zoom1[] =
1242 {SEQUENCE_END, 0x00}
1245 static struct reginfo sensor_Zoom2[] =
1247 {SEQUENCE_END, 0x00}
1251 static struct reginfo sensor_Zoom3[] =
1253 {SEQUENCE_END, 0x00}
1255 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1257 static const struct v4l2_querymenu sensor_menus[] =
1259 #if CONFIG_SENSOR_WhiteBalance
1260 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1261 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1262 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1265 #if CONFIG_SENSOR_Effect
1266 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1267 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1268 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1271 #if CONFIG_SENSOR_Scene
1272 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1275 #if CONFIG_SENSOR_Flash
1276 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1277 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1281 static struct v4l2_queryctrl sensor_controls[] =
1283 #if CONFIG_SENSOR_WhiteBalance
1285 .id = V4L2_CID_DO_WHITE_BALANCE,
1286 .type = V4L2_CTRL_TYPE_MENU,
1287 .name = "White Balance Control",
1295 #if CONFIG_SENSOR_Brightness
1297 .id = V4L2_CID_BRIGHTNESS,
1298 .type = V4L2_CTRL_TYPE_INTEGER,
1299 .name = "Brightness Control",
1307 #if CONFIG_SENSOR_Effect
1309 .id = V4L2_CID_EFFECT,
1310 .type = V4L2_CTRL_TYPE_MENU,
1311 .name = "Effect Control",
1319 #if CONFIG_SENSOR_Exposure
1321 .id = V4L2_CID_EXPOSURE,
1322 .type = V4L2_CTRL_TYPE_INTEGER,
1323 .name = "Exposure Control",
1331 #if CONFIG_SENSOR_Saturation
1333 .id = V4L2_CID_SATURATION,
1334 .type = V4L2_CTRL_TYPE_INTEGER,
1335 .name = "Saturation Control",
1343 #if CONFIG_SENSOR_Contrast
1345 .id = V4L2_CID_CONTRAST,
1346 .type = V4L2_CTRL_TYPE_INTEGER,
1347 .name = "Contrast Control",
1355 #if CONFIG_SENSOR_Mirror
1357 .id = V4L2_CID_HFLIP,
1358 .type = V4L2_CTRL_TYPE_BOOLEAN,
1359 .name = "Mirror Control",
1367 #if CONFIG_SENSOR_Flip
1369 .id = V4L2_CID_VFLIP,
1370 .type = V4L2_CTRL_TYPE_BOOLEAN,
1371 .name = "Flip Control",
1379 #if CONFIG_SENSOR_Scene
1381 .id = V4L2_CID_SCENE,
1382 .type = V4L2_CTRL_TYPE_MENU,
1383 .name = "Scene Control",
1391 #if CONFIG_SENSOR_DigitalZoom
1393 .id = V4L2_CID_ZOOM_RELATIVE,
1394 .type = V4L2_CTRL_TYPE_INTEGER,
1395 .name = "DigitalZoom Control",
1401 .id = V4L2_CID_ZOOM_ABSOLUTE,
1402 .type = V4L2_CTRL_TYPE_INTEGER,
1403 .name = "DigitalZoom Control",
1411 #if CONFIG_SENSOR_Focus
1413 .id = V4L2_CID_FOCUS_RELATIVE,
1414 .type = V4L2_CTRL_TYPE_INTEGER,
1415 .name = "Focus Control",
1421 .id = V4L2_CID_FOCUS_ABSOLUTE,
1422 .type = V4L2_CTRL_TYPE_INTEGER,
1423 .name = "Focus Control",
1427 .default_value = 125,
1430 .id = V4L2_CID_FOCUSZONE,
1431 .type = V4L2_CTRL_TYPE_INTEGER,
1432 .name = "FocusZone Control",
1438 .id = V4L2_CID_FOCUS_AUTO,
1439 .type = V4L2_CTRL_TYPE_BOOLEAN,
1440 .name = "Focus Control",
1446 #if CONFIG_SENSOR_FocusContinues
1448 .id = V4L2_CID_FOCUS_CONTINUOUS,
1449 .type = V4L2_CTRL_TYPE_BOOLEAN,
1450 .name = "Focus Control",
1459 #if CONFIG_SENSOR_Flash
1461 .id = V4L2_CID_FLASH,
1462 .type = V4L2_CTRL_TYPE_MENU,
1463 .name = "Flash Control",
1472 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1473 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1474 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1475 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1476 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1477 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1478 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1479 static int sensor_resume(struct soc_camera_device *icd);
1480 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1481 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1482 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1483 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1484 static int sensor_deactivate(struct i2c_client *client);
1485 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1486 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1488 static struct soc_camera_ops sensor_ops =
1490 .suspend = sensor_suspend,
1491 .resume = sensor_resume,
1492 .set_bus_param = sensor_set_bus_param,
1493 .query_bus_param = sensor_query_bus_param,
1494 .controls = sensor_controls,
1495 .menus = sensor_menus,
1496 .num_controls = ARRAY_SIZE(sensor_controls),
1497 .num_menus = ARRAY_SIZE(sensor_menus),
1499 /* only one fixed colorspace per pixelcode */
1500 struct sensor_datafmt {
1501 enum v4l2_mbus_pixelcode code;
1502 enum v4l2_colorspace colorspace;
1505 /* Find a data format by a pixel code in an array */
1506 static const struct sensor_datafmt *sensor_find_datafmt(
1507 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1511 for (i = 0; i < n; i++)
1512 if (fmt[i].code == code)
1518 static const struct sensor_datafmt sensor_colour_fmts[] = {
1519 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1520 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1526 WqCmd_af_special_pos,
1530 WqCmd_af_return_idle,
1532 enum sensor_wq_result
1540 struct i2c_client *client;
1541 struct delayed_work dwork;
1542 enum sensor_wq_cmd cmd;
1543 wait_queue_head_t done;
1544 enum sensor_wq_result result;
1547 int zone_center_pos[2];
1550 typedef struct sensor_info_priv_s
1566 unsigned char mirror; /* HFLIP */
1567 unsigned char flip; /* VFLIP */
1568 struct reginfo *winseqe_cur_addr;
1569 struct sensor_datafmt fmt;
1570 unsigned int enable;
1571 unsigned int funmodule_state;
1572 } sensor_info_priv_t;
1576 struct sensor_parameter
1578 unsigned short int preview_maxlines;
1579 unsigned int preview_exposure;
1580 unsigned short int preview_line_width;
1581 unsigned short int preview_gain;
1583 unsigned short int capture_framerate;
1584 unsigned short int preview_framerate;
1590 struct v4l2_subdev subdev;
1591 struct i2c_client *client;
1592 sensor_info_priv_t info_priv;
1593 struct sensor_parameter parameter;
1594 struct workqueue_struct *sensor_wq;
1595 struct mutex wq_lock;
1596 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1597 #if CONFIG_SENSOR_I2C_NOSCHED
1598 atomic_t tasklock_cnt;
1600 struct rk29camera_platform_data *sensor_io_request;
1601 struct rk29camera_gpio_res *sensor_gpio_res;
1604 static struct sensor* to_sensor(const struct i2c_client *client)
1606 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1609 static int sensor_task_lock(struct i2c_client *client, int lock)
1611 #if CONFIG_SENSOR_I2C_NOSCHED
1613 struct sensor *sensor = to_sensor(client);
1616 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1617 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1618 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1622 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1623 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1624 goto sensor_task_lock_err;
1629 atomic_add(1, &sensor->tasklock_cnt);
1631 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1632 atomic_sub(1, &sensor->tasklock_cnt);
1634 if (atomic_read(&sensor->tasklock_cnt) == 0)
1639 sensor_task_lock_err:
1648 #if CONFIG_SENSOR_WRITE_REGS
1649 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
1652 struct i2c_msg msg[1];
1655 msg->addr = client->addr;
1656 msg->flags = client->flags;
1657 msg->buf = reg_info;
1658 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1659 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1665 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1666 err = i2c_transfer(client->adapter, msg, 1);
1670 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
1684 /* sensor register write */
1685 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1689 struct i2c_msg msg[1];
1692 buf[1] = reg & 0xFF;
1695 msg->addr = client->addr;
1696 msg->flags = client->flags;
1698 msg->len = sizeof(buf);
1699 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1700 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1705 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1706 err = i2c_transfer(client->adapter, msg, 1);
1711 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1719 /* sensor register read */
1720 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1724 struct i2c_msg msg[2];
1727 buf[1] = reg & 0xFF;
1729 msg[0].addr = client->addr;
1730 msg[0].flags = client->flags;
1732 msg[0].len = sizeof(buf);
1733 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1734 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1736 msg[1].addr = client->addr;
1737 msg[1].flags = client->flags|I2C_M_RD;
1740 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1741 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1745 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1746 err = i2c_transfer(client->adapter, msg, 2);
1752 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1760 /* write a array of registers */
1761 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1765 #if CONFIG_SENSOR_Focus
1766 struct sensor *sensor = to_sensor(client);
1768 #if CONFIG_SENSOR_I2C_RDWRCHK
1771 #if CONFIG_SENSOR_WRITE_REGS
1778 if (sensor_task_lock(client, 1) < 0)
1779 goto sensor_write_array_end;
1780 while (regarray[i].reg != SEQUENCE_END) {
1781 #if CONFIG_SENSOR_Focus
1782 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1783 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1785 goto sensor_write_array_end;
1789 #if CONFIG_SENSOR_WRITE_REGS
1793 reg_length = 0x0001;
1795 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
1798 if(reg_num >= WRITE_REGS_NUM)
1805 size_num = reg_num + 1;
1807 ptemp = phead = (u8*)kmalloc((size_num+10)*sizeof(u8),GFP_KERNEL);
1809 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1811 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1813 *phead = regarray[j].reg >> 8;
1814 *(ptemp+1) = regarray[j].reg & 0xFF;
1816 for( ; reg_num > 0; reg_num --, j++) {
1817 *ptemp ++ = regarray[j].val;
1821 err = sensor_write_regs(client, ptemp,size_num);
1825 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1828 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1833 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1837 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1839 goto sensor_write_array_end;
1842 #if CONFIG_SENSOR_I2C_RDWRCHK
1843 sensor_read(client, regarray[i].reg, &valchk);
1844 if (valchk != regarray[i].val)
1845 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1851 #if CONFIG_SENSOR_Focus
1852 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1853 || (regarray == sensor_init_data)) {
1854 sensor->info_priv.affm_reinit = 1;
1858 sensor_write_array_end:
1859 sensor_task_lock(client,0);
1862 #if CONFIG_SENSOR_I2C_RDWRCHK
1863 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1871 while (regarray[i].reg != 0)
1873 sensor_read(client, regarray[i].reg, &valchk);
1874 if (valchk != regarray[i].val)
1875 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1882 #if CONFIG_SENSOR_Focus
1889 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1892 char read_tag=0xff,cnt;
1895 for (i=0; i<4; i++) {
1896 if (cmdinfo->validate_bit & (1<<i)) {
1897 if (sensor_write(client, CMD_PARA0_Reg+i, cmdinfo->cmd_para[i])) {
1898 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]);
1899 goto sensor_af_cmdset_err;
1901 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]);
1905 if (cmdinfo->validate_bit & 0x80) {
1906 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1907 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1908 goto sensor_af_cmdset_err;
1910 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1914 if (sensor_write(client, CMD_ACK_Reg, 0x01)) {
1915 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1916 goto sensor_af_cmdset_err;
1918 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1921 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1922 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1923 goto sensor_af_cmdset_err;
1930 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1931 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1934 } while((read_tag != 0x00)&& (cnt++<100));
1936 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1938 sensor_af_cmdset_err:
1942 static int sensor_af_idlechk(struct i2c_client *client)
1946 struct af_cmdinfo cmdinfo;
1948 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1950 cmdinfo.cmd_tag = 0x01;
1951 cmdinfo.validate_bit = 0x80;
1952 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1954 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1956 goto sensor_af_idlechk_end;
1961 ret = sensor_read(client, CMD_ACK_Reg, &state);
1963 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1965 goto sensor_af_idlechk_end;
1967 }while(0x00 != state);
1970 sensor_af_idlechk_end:
1973 static int sensor_af_touch_zone(struct i2c_client *client, int *zone_center_pos)
1976 struct af_cmdinfo cmdinfo;
1978 cmdinfo.cmd_tag = 0x01;
1979 cmdinfo.validate_bit = 0x83;
1980 if (zone_center_pos[0]<=8)
1981 cmdinfo.cmd_para[0] = 0;
1982 else if ((zone_center_pos[0]>8) && (zone_center_pos[0]<72))
1983 cmdinfo.cmd_para[0] = zone_center_pos[0]-8;
1985 cmdinfo.cmd_para[0] = 72;
1987 if (zone_center_pos[1]<=6)
1988 cmdinfo.cmd_para[1] = 0;
1989 else if ((zone_center_pos[1]>6) && (zone_center_pos[1]<54))
1990 cmdinfo.cmd_para[1] = zone_center_pos[1]-6;
1992 cmdinfo.cmd_para[1] = 54;
1994 ret = sensor_af_cmdset(client, TouchZoneConfig_Cmd, &cmdinfo);
1996 SENSOR_TR("%s touch zone config error!\n",SENSOR_NAME_STRING());
1998 goto sensor_af_zone_end;
2003 static int sensor_af_single(struct i2c_client *client)
2007 struct af_cmdinfo cmdinfo;
2010 cmdinfo.cmd_tag = 0x01;
2011 cmdinfo.validate_bit = 0x80;
2012 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
2014 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
2016 goto sensor_af_single_end;
2026 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2028 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2030 goto sensor_af_single_end;
2032 }while((state == S_FOCUSING) && (cnt<100));
2034 if (state != S_FOCUSED) {
2035 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
2037 goto sensor_af_single_end;
2039 SENSOR_DG("%s[%d] single focus mode set success!\n",SENSOR_NAME_STRING(),__LINE__);
2041 sensor_af_single_end:
2045 static int sensor_af_const(struct i2c_client *client)
2048 struct af_cmdinfo cmdinfo;
2051 cmdinfo.cmd_tag = 0x01;
2052 cmdinfo.cmd_para[0] = 0x00;
2053 cmdinfo.validate_bit = 0x81;
2055 if (sensor_af_cmdset(client, ConstFocus_Cmd, &cmdinfo)) {
2056 SENSOR_TR("%s[%d] const focus mode set error!\n",SENSOR_NAME_STRING(),__LINE__);
2058 goto sensor_af_const_end;
2060 SENSOR_DG("%s[%d] const focus mode set success!\n",SENSOR_NAME_STRING(),__LINE__);
2062 sensor_af_const_end:
2065 static int sensor_af_pause2capture(struct i2c_client *client)
2069 if (sensor_af_cmdset(client, PauseFocus_Cmd, NULL)) {
2070 SENSOR_TR("%s pause focus mode set error!\n",SENSOR_NAME_STRING());
2072 goto sensor_af_pause_end;
2074 sensor_af_pause_end:
2078 static int sensor_af_init(struct i2c_client *client)
2083 ret = sensor_write_array(client, sensor_af_firmware);
2085 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
2087 goto sensor_af_init_end;
2096 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2098 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2100 goto sensor_af_init_end;
2102 } while (state != S_IDLE);
2104 if (state != S_IDLE) {
2105 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
2107 goto sensor_af_init_end;
2111 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
2115 static int sensor_af_downfirmware(struct i2c_client *client)
2117 struct sensor *sensor = to_sensor(client);
2119 struct soc_camera_device *icd = client->dev.platform_data;
2120 struct v4l2_mbus_framefmt mf;
2122 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
2124 if (sensor_af_init(client)) {
2125 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
2128 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
2130 mf.width = icd->user_width;
2131 mf.height = icd->user_height;
2132 mf.code = sensor->info_priv.fmt.code;
2133 mf.colorspace = sensor->info_priv.fmt.colorspace;
2134 mf.field = V4L2_FIELD_NONE;
2135 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
2136 ret = sensor_af_const(client);
2138 switch (sensor->info_priv.auto_focus)
2140 case SENSOR_AF_MODE_AUTO:
2142 ret = sensor_af_single(client);
2145 case SENSOR_AF_MODE_CLOSE:
2150 case SENSOR_AF_MODE_CONTINUOUS:
2152 ret = sensor_af_const(client);
2157 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
2158 goto sensor_af_downfirmware_end;
2162 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
2165 sensor_af_downfirmware_end:
2169 static void sensor_af_workqueue(struct work_struct *work)
2171 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
2172 struct i2c_client *client = sensor_work->client;
2173 struct sensor *sensor = to_sensor(client);
2174 struct af_cmdinfo cmdinfo;
2176 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
2178 mutex_lock(&sensor->wq_lock);
2180 switch (sensor_work->cmd)
2184 if (sensor_af_downfirmware(client) < 0) {
2185 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2189 case WqCmd_af_single:
2191 if ((sensor_work->zone_center_pos[0] >=0) && (sensor_work->zone_center_pos[1]>=0))
2192 sensor_af_touch_zone(client,sensor_work->zone_center_pos);
2194 if (sensor_af_single(client) < 0) {
2195 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2196 sensor_work->result = WqRet_fail;
2198 sensor_work->result = WqRet_success;
2202 case WqCmd_af_special_pos:
2204 sensor_af_idlechk(client);
2206 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2207 cmdinfo.cmd_para[0] = sensor_work->var;
2208 cmdinfo.validate_bit = 0x81;
2209 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2210 sensor_work->result = WqRet_fail;
2212 sensor_work->result = WqRet_success;
2215 case WqCmd_af_near_pos:
2217 sensor_af_idlechk(client);
2218 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2219 cmdinfo.validate_bit = 0x80;
2220 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2221 sensor_work->result = WqRet_fail;
2223 sensor_work->result = WqRet_success;
2226 case WqCmd_af_far_pos:
2228 sensor_af_idlechk(client);
2229 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2230 cmdinfo.validate_bit = 0x80;
2231 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2232 sensor_work->result = WqRet_fail;
2234 sensor_work->result = WqRet_success;
2237 case WqCmd_af_continues:
2239 if (sensor_af_const(client) < 0)
2240 sensor_work->result = WqRet_fail;
2242 sensor_work->result = WqRet_success;
2245 case WqCmd_af_return_idle:
2247 if (sensor_af_idlechk(client) < 0)
2248 sensor_work->result = WqRet_fail;
2250 sensor_work->result = WqRet_success;
2254 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
2258 if (sensor_work->wait == false) {
2259 kfree((void*)sensor_work);
2261 wake_up(&sensor_work->done);
2263 mutex_unlock(&sensor->wq_lock);
2267 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait, int *zone_pos)
2269 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2270 struct sensor *sensor = to_sensor(client);
2271 struct sensor_work *wk;
2274 if (sensor->sensor_wq == NULL) {
2276 goto sensor_af_workqueue_set_end;
2279 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) != SENSOR_AF_IS_OK) {
2280 if (cmd != WqCmd_af_init) {
2281 SENSOR_TR("%s %s cmd(%d) ingore,because af module isn't ready!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2283 goto sensor_af_workqueue_set_end;
2287 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
2289 wk->client = client;
2290 INIT_DELAYED_WORK(&wk->dwork, sensor_af_workqueue);
2292 wk->result = WqRet_inval;
2297 if (*zone_pos || *(zone_pos+1) || *(zone_pos+2) || *(zone_pos+3)) {
2299 *(zone_pos+1) += 1000;
2300 *(zone_pos+2) += 1000;
2301 *(zone_pos+3) += 1000;
2302 wk->zone_center_pos[0] = ((*zone_pos + *(zone_pos+2))>>1)*80/2000;
2303 wk->zone_center_pos[1] = ((*(zone_pos+1) + *(zone_pos+3))>>1)*60/2000;
2305 #if CONFIG_SENSOR_FocusCenterInCapture
2306 wk->zone_center_pos[0] = 32;
2307 wk->zone_center_pos[1] = 24;
2309 wk->zone_center_pos[0] = -1;
2310 wk->zone_center_pos[1] = -1;
2315 init_waitqueue_head(&wk->done);
2317 /* ddl@rock-chips.com:
2318 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
2319 * As a result any other ioctl calls will proceed very, very slowly since each call
2320 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
2321 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
2324 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
2325 mutex_unlock(&icd->video_lock);
2326 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(5000)) == 0) { //hhb
2327 SENSOR_TR("%s %s cmd(%d) is timeout!\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2331 mutex_lock(&icd->video_lock);
2333 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),msecs_to_jiffies(10));
2337 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2340 sensor_af_workqueue_set_end:
2344 static int sensor_parameter_record(struct i2c_client *client)
2346 u8 ret_l,ret_m,ret_h;
2348 struct sensor *sensor = to_sensor(client);
2350 sensor_read(client,0x3a00, &ret_l);
2351 sensor_write(client,0x3a00, ret_l&0xfb);
2353 sensor_write(client,0x3503,0x07); //stop AE/AG
2354 sensor_read(client,0x3406, &ret_l);
2355 sensor_write(client,0x3406, ret_l|0x01);
2357 sensor_read(client,0x3500,&ret_h);
2358 sensor_read(client,0x3501, &ret_m);
2359 sensor_read(client,0x3502, &ret_l);
2363 sensor->parameter.preview_exposure = ((tp_h<<12) & 0xF000) | ((tp_m<<4) & 0x0FF0) | ((tp_l>>4) & 0x0F);
2365 //Read back AGC Gain for preview
2366 sensor_read(client,0x350b, &ret_l);
2367 sensor->parameter.preview_gain = ret_l;
2369 SENSOR_DG(" %s Read 0x350b=0x%02x PreviewExposure:%d 0x3500=0x%02x 0x3501=0x%02x 0x3502=0x%02x \n",
2370 SENSOR_NAME_STRING(), tp_l,sensor->parameter.preview_exposure,tp_h, tp_m, tp_l);
2373 #define OV5640_FULL_PERIOD_PIXEL_NUMS_HTS (2844)
2374 #define OV5640_FULL_PERIOD_LINE_NUMS_VTS (1968)
2375 #define OV5640_PV_PERIOD_PIXEL_NUMS_HTS (1896)
2376 #define OV5640_PV_PERIOD_LINE_NUMS_VTS (984)
2377 static int sensor_ae_transfer(struct i2c_client *client)
2382 u16 ulCapture_Exposure;
2383 u16 Preview_Maxlines;
2385 u16 OV5640_g_iExtra_ExpLines;
2386 struct sensor *sensor = to_sensor(client);
2388 //Preview_Maxlines = sensor->parameter.preview_line_width;
2389 Preview_Maxlines = sensor->parameter.preview_maxlines;
2390 Gain = sensor->parameter.preview_gain;
2393 ulCapture_Exposure = (sensor->parameter.preview_exposure*OV5640_PV_PERIOD_PIXEL_NUMS_HTS)/OV5640_FULL_PERIOD_PIXEL_NUMS_HTS;
2395 SENSOR_DG("cap shutter calutaed = %d, 0x%x\n", ulCapture_Exposure,ulCapture_Exposure);
2397 // write the gain and exposure to 0x350* registers
2398 sensor_write(client,0x350b, Gain);
2400 if (ulCapture_Exposure <= 1940) {
2401 OV5640_g_iExtra_ExpLines = 0;
2403 OV5640_g_iExtra_ExpLines = ulCapture_Exposure - 1940;
2405 SENSOR_DG("Set Extra-line = %d, iExp = %d \n", OV5640_g_iExtra_ExpLines, ulCapture_Exposure);
2407 ExposureLow = (ulCapture_Exposure<<4)&0xff;
2408 ExposureMid = (ulCapture_Exposure>>4)&0xff;
2409 ExposureHigh = (ulCapture_Exposure>>12);
2411 sensor_write(client,0x350c, (OV5640_g_iExtra_ExpLines&0xff00)>>8);
2412 sensor_write(client,0x350d, OV5640_g_iExtra_ExpLines&0xff);
2413 sensor_write(client,0x3502, ExposureLow);
2414 sensor_write(client,0x3501, ExposureMid);
2415 sensor_write(client,0x3500, ExposureHigh);
2417 SENSOR_DG(" %s Write 0x350b=0x%02x 0x350c=0x%2x 0x350d=0x%2x 0x3502=0x%02x 0x3501=0x%02x 0x3500=0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2422 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2424 struct soc_camera_link *icl = to_soc_camera_link(icd);
2427 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2431 case Sensor_PowerDown:
2433 if (icl->powerdown) {
2434 ret = icl->powerdown(icd->pdev, on);
2435 if (ret == RK29_CAM_IO_SUCCESS) {
2439 icl->reset(icd->pdev);
2441 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2443 goto sensor_power_end;
2450 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2451 struct sensor *sensor = to_sensor(client);
2453 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2454 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2456 //flash off after 2 secs
2457 hrtimer_cancel(&(flash_off_timer.timer));
2458 hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
2465 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2474 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
2475 struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
2476 sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
2477 SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
2482 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2484 struct i2c_client *client = v4l2_get_subdevdata(sd);
2485 struct soc_camera_device *icd = client->dev.platform_data;
2486 struct sensor *sensor = to_sensor(client);
2487 const struct v4l2_queryctrl *qctrl;
2488 const struct sensor_datafmt *fmt;
2492 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2494 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2496 goto sensor_INIT_ERR;
2500 if (sensor_task_lock(client,1)<0)
2501 goto sensor_INIT_ERR;
2502 ret = sensor_write(client, 0x3008, 0x80);
2504 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2506 goto sensor_INIT_ERR;
2509 mdelay(5); //delay 5 microseconds
2510 /* check if it is an sensor sensor */
2511 ret = sensor_read(client, 0x300a, &value);
2513 SENSOR_TR("read chip id high byte failed\n");
2515 goto sensor_INIT_ERR;
2518 pid |= (value << 8);
2520 ret = sensor_read(client, 0x300b, &value);
2522 SENSOR_TR("read chip id low byte failed\n");
2524 goto sensor_INIT_ERR;
2527 pid |= (value & 0xff);
2528 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2530 if (pid == SENSOR_ID) {
2531 sensor->model = SENSOR_V4L2_IDENT;
2533 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2535 goto sensor_INIT_ERR;
2538 ret = sensor_write_array(client, sensor_init_data);
2539 udelay(1000); //wait sensor power on,so that I2C write sensor registers would sucess hhb@rock-chips.con
2542 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2543 goto sensor_INIT_ERR;
2545 sensor_task_lock(client,0);
2546 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2547 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2549 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2551 goto sensor_INIT_ERR;
2553 sensor->info_priv.fmt = *fmt;
2555 /* sensor sensor information for initialization */
2556 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2558 sensor->info_priv.whiteBalance = qctrl->default_value;
2559 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2561 sensor->info_priv.brightness = qctrl->default_value;
2562 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2564 sensor->info_priv.effect = qctrl->default_value;
2565 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2567 sensor->info_priv.exposure = qctrl->default_value;
2569 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2571 sensor->info_priv.saturation = qctrl->default_value;
2572 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2574 sensor->info_priv.contrast = qctrl->default_value;
2575 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2577 sensor->info_priv.mirror = qctrl->default_value;
2578 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2580 sensor->info_priv.flip = qctrl->default_value;
2581 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2583 sensor->info_priv.scene = qctrl->default_value;
2584 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2586 sensor->info_priv.digitalzoom = qctrl->default_value;
2588 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2589 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2591 sensor->info_priv.focus = qctrl->default_value;
2593 #if CONFIG_SENSOR_Flash
2594 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2596 sensor->info_priv.flash = qctrl->default_value;
2597 flash_off_timer.icd = icd;
2598 flash_off_timer.timer.function = flash_off_func;
2600 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);
2602 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
2606 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2607 sensor_task_lock(client,0);
2608 sensor_deactivate(client);
2611 static int sensor_deactivate(struct i2c_client *client)
2613 struct soc_camera_device *icd = client->dev.platform_data;
2614 struct sensor *sensor = to_sensor(client);
2616 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2618 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2619 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2620 sensor_task_lock(client, 1);
2621 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2622 sensor_write(client, 0x3018, 0x03); // D5-D0
2623 sensor_write(client,0x3019,0x00); // STROBE,SDA
2624 sensor_task_lock(client, 0);
2626 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2628 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2629 icd->user_width = SENSOR_INIT_WIDTH;
2630 icd->user_height = SENSOR_INIT_HEIGHT;
2631 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2635 static struct reginfo sensor_power_down_sequence[]=
2640 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2643 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2645 if (pm_msg.event == PM_EVENT_SUSPEND) {
2646 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2647 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2649 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2652 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2654 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2659 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2666 static int sensor_resume(struct soc_camera_device *icd)
2670 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2672 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2676 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2680 static int sensor_set_bus_param(struct soc_camera_device *icd,
2681 unsigned long flags)
2687 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2689 struct soc_camera_link *icl = to_soc_camera_link(icd);
2690 unsigned long flags = SENSOR_BUS_PARAM;
2692 return soc_camera_apply_sensor_flags(icl, flags);
2695 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2697 struct i2c_client *client = v4l2_get_subdevdata(sd);
2698 struct soc_camera_device *icd = client->dev.platform_data;
2699 struct sensor *sensor = to_sensor(client);
2701 mf->width = icd->user_width;
2702 mf->height = icd->user_height;
2703 mf->code = sensor->info_priv.fmt.code;
2704 mf->colorspace = sensor->info_priv.fmt.colorspace;
2705 mf->field = V4L2_FIELD_NONE;
2709 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2713 if ((mf->width == 1024) && (mf->height == 768)) {
2715 } else if ((mf->width == 1280) && (mf->height == 1024)) {
2717 } else if ((mf->width == 1600) && (mf->height == 1200)) {
2719 } else if ((mf->width == 2048) && (mf->height == 1536)) {
2721 } else if ((mf->width == 2592) && (mf->height == 1944)) {
2726 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2730 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2734 if ((mf->width == 1280) && (mf->height == 720)) {
2736 } else if ((mf->width == 1920) && (mf->height == 1080)) {
2741 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2744 static int sensor_s_fmt(struct v4l2_subdev *sd,struct v4l2_mbus_framefmt *mf)
2746 struct i2c_client *client = v4l2_get_subdevdata(sd);
2747 const struct sensor_datafmt *fmt;
2748 struct sensor *sensor = to_sensor(client);
2749 const struct v4l2_queryctrl *qctrl;
2750 struct soc_camera_device *icd = client->dev.platform_data;
2751 struct reginfo *winseqe_set_addr=NULL;
2752 int ret = 0, set_w,set_h;
2754 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2755 ARRAY_SIZE(sensor_colour_fmts));
2758 goto sensor_s_fmt_end;
2761 if (sensor->info_priv.fmt.code != mf->code) {
2764 case V4L2_MBUS_FMT_YUYV8_2X8:
2766 winseqe_set_addr = sensor_ClrFmt_YUYV;
2769 case V4L2_MBUS_FMT_UYVY8_2X8:
2771 winseqe_set_addr = sensor_ClrFmt_UYVY;
2777 if (winseqe_set_addr != NULL) {
2778 sensor_write_array(client, winseqe_set_addr);
2779 sensor->info_priv.fmt.code = mf->code;
2780 sensor->info_priv.fmt.colorspace= mf->colorspace;
2781 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2783 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2790 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2792 winseqe_set_addr = sensor_qcif;
2796 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2798 winseqe_set_addr = sensor_qvga;
2802 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2804 winseqe_set_addr = sensor_cif;
2808 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2810 winseqe_set_addr = sensor_vga;
2814 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2816 winseqe_set_addr = sensor_svga;
2820 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2822 winseqe_set_addr = sensor_xga;
2826 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2828 winseqe_set_addr = sensor_720p;
2832 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2834 winseqe_set_addr = sensor_sxga;
2838 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2840 winseqe_set_addr = sensor_uxga;
2844 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2846 winseqe_set_addr = sensor_1080p;
2850 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2852 winseqe_set_addr = sensor_qxga;
2856 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2858 winseqe_set_addr = sensor_qsxga;
2864 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2865 set_w = SENSOR_INIT_WIDTH;
2866 set_h = SENSOR_INIT_HEIGHT;
2867 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2870 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2872 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2873 sensor_parameter_record(client);
2874 #if CONFIG_SENSOR_Focus
2875 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2876 sensor_af_pause2capture(client);
2878 #if CONFIG_SENSOR_Flash
2879 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2880 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2881 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2884 }else { /* ddl@rock-chips.com : Video */
2885 #if CONFIG_SENSOR_Flash
2886 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2887 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2888 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2892 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2893 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2894 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2895 ret |= sensor_write_array(client,sensor_init_data);
2896 udelay(1000); //wait sensor power on,so that I2C write sensor registers would sucess hhb@rock-chips.con
2897 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2901 if ((winseqe_set_addr == sensor_qsxga) && (sensor->info_priv.winseqe_cur_addr == sensor_720p)) {
2902 sensor_write_array(client, sensor_svga);
2905 ret |= sensor_write_array(client, winseqe_set_addr);
2907 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2908 #if CONFIG_SENSOR_Flash
2909 if (sensor_fmt_capturechk(sd,mf) == true) {
2910 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2911 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2912 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2916 goto sensor_s_fmt_end;
2918 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2920 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2921 sensor_ae_transfer(client);
2922 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2923 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2924 if (sensor->info_priv.whiteBalance != 0) {
2925 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2926 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2928 sensor->info_priv.snap2preview = true;
2929 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2930 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2931 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2933 sensor->info_priv.video2preview = true;
2934 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2935 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2936 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2937 if (sensor->info_priv.snap2preview == true) {
2938 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2939 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2941 #if CONFIG_SENSOR_Focus
2942 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CLOSE) {
2945 sensor_af_workqueue_set(icd,WqCmd_af_return_idle,0,false,NULL);
2946 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS) {
2947 sensor_af_workqueue_set(icd,WqCmd_af_continues,0,false,NULL);
2955 sensor->info_priv.video2preview = false;
2956 sensor->info_priv.snap2preview = false;
2958 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2962 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2970 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2972 struct i2c_client *client = v4l2_get_subdevdata(sd);
2973 struct sensor *sensor = to_sensor(client);
2974 const struct sensor_datafmt *fmt;
2975 int ret = 0,set_w,set_h;
2977 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2978 ARRAY_SIZE(sensor_colour_fmts));
2980 fmt = &sensor->info_priv.fmt;
2981 mf->code = fmt->code;
2984 if (mf->height > SENSOR_MAX_HEIGHT)
2985 mf->height = SENSOR_MAX_HEIGHT;
2986 else if (mf->height < SENSOR_MIN_HEIGHT)
2987 mf->height = SENSOR_MIN_HEIGHT;
2989 if (mf->width > SENSOR_MAX_WIDTH)
2990 mf->width = SENSOR_MAX_WIDTH;
2991 else if (mf->width < SENSOR_MIN_WIDTH)
2992 mf->width = SENSOR_MIN_WIDTH;
2997 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
3002 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
3007 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
3012 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
3017 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
3022 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
3027 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
3032 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
3037 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
3042 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
3047 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
3052 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
3059 set_w = SENSOR_INIT_WIDTH;
3060 set_h = SENSOR_INIT_HEIGHT;
3066 mf->colorspace = fmt->colorspace;
3071 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
3073 struct i2c_client *client = v4l2_get_subdevdata(sd);
3075 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
3078 if (id->match.addr != client->addr)
3081 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
3086 #if CONFIG_SENSOR_Brightness
3087 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3089 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3091 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3093 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
3095 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
3097 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3100 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3104 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3108 #if CONFIG_SENSOR_Effect
3109 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3111 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3113 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3115 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
3117 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
3119 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3122 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3126 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3130 #if CONFIG_SENSOR_Exposure
3131 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3133 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3135 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3137 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
3139 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3141 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3144 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3148 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3152 #if CONFIG_SENSOR_Saturation
3153 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3155 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3157 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3159 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3161 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3163 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3166 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3170 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3174 #if CONFIG_SENSOR_Contrast
3175 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3177 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3179 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3181 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3183 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3185 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3188 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3192 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3196 #if CONFIG_SENSOR_Mirror
3197 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3199 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3201 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3203 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3205 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3207 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3210 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3214 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3218 #if CONFIG_SENSOR_Flip
3219 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3221 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3223 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3225 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3227 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3229 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3232 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3236 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3240 #if CONFIG_SENSOR_Scene
3241 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3243 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3245 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3247 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3249 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3251 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3254 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3258 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3262 #if CONFIG_SENSOR_WhiteBalance
3263 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3265 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3267 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3269 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3271 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3273 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3276 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3280 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3284 #if CONFIG_SENSOR_DigitalZoom
3285 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3287 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3288 struct sensor *sensor = to_sensor(client);
3289 const struct v4l2_queryctrl *qctrl_info;
3290 int digitalzoom_cur, digitalzoom_total;
3292 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3296 digitalzoom_cur = sensor->info_priv.digitalzoom;
3297 digitalzoom_total = qctrl_info->maximum;
3299 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3301 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3305 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3307 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3311 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3313 *value = digitalzoom_total - digitalzoom_cur;
3316 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3318 *value = 0 - digitalzoom_cur;
3321 digitalzoom_cur += *value;
3323 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3325 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3327 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3330 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3337 #if CONFIG_SENSOR_Focus
3338 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3340 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3341 struct sensor *sensor = to_sensor(client);
3342 const struct v4l2_queryctrl *qctrl_info;
3345 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3349 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3350 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3351 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true,NULL);
3352 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3355 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3359 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3360 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3365 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3367 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3368 struct sensor *sensor = to_sensor(client);
3369 const struct v4l2_queryctrl *qctrl_info;
3372 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3376 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3377 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3379 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true,NULL);
3381 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true,NULL);
3383 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3386 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3390 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3391 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3396 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value, int *zone_pos)
3398 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3399 struct sensor *sensor = to_sensor(client);
3402 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3405 case SENSOR_AF_MODE_AUTO:
3407 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true, zone_pos);
3411 /*case SENSOR_AF_MODE_MACRO:
3413 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3417 case SENSOR_AF_MODE_INFINITY:
3419 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3423 case SENSOR_AF_MODE_CONTINUOUS:
3425 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true,NULL);
3429 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3434 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3437 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3438 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3444 #if CONFIG_SENSOR_Flash
3445 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3447 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3448 if (value == 3) { /* ddl@rock-chips.com: torch */
3449 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3451 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3453 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3457 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3461 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3463 struct i2c_client *client = v4l2_get_subdevdata(sd);
3464 struct sensor *sensor = to_sensor(client);
3465 const struct v4l2_queryctrl *qctrl;
3467 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3471 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3477 case V4L2_CID_BRIGHTNESS:
3479 ctrl->value = sensor->info_priv.brightness;
3482 case V4L2_CID_SATURATION:
3484 ctrl->value = sensor->info_priv.saturation;
3487 case V4L2_CID_CONTRAST:
3489 ctrl->value = sensor->info_priv.contrast;
3492 case V4L2_CID_DO_WHITE_BALANCE:
3494 ctrl->value = sensor->info_priv.whiteBalance;
3497 case V4L2_CID_EXPOSURE:
3499 ctrl->value = sensor->info_priv.exposure;
3502 case V4L2_CID_HFLIP:
3504 ctrl->value = sensor->info_priv.mirror;
3507 case V4L2_CID_VFLIP:
3509 ctrl->value = sensor->info_priv.flip;
3520 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3522 struct i2c_client *client = v4l2_get_subdevdata(sd);
3523 struct sensor *sensor = to_sensor(client);
3524 struct soc_camera_device *icd = client->dev.platform_data;
3525 const struct v4l2_queryctrl *qctrl;
3528 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3532 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3538 #if CONFIG_SENSOR_Brightness
3539 case V4L2_CID_BRIGHTNESS:
3541 if (ctrl->value != sensor->info_priv.brightness)
3543 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3547 sensor->info_priv.brightness = ctrl->value;
3552 #if CONFIG_SENSOR_Exposure
3553 case V4L2_CID_EXPOSURE:
3555 if (ctrl->value != sensor->info_priv.exposure)
3557 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3561 sensor->info_priv.exposure = ctrl->value;
3566 #if CONFIG_SENSOR_Saturation
3567 case V4L2_CID_SATURATION:
3569 if (ctrl->value != sensor->info_priv.saturation)
3571 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3575 sensor->info_priv.saturation = ctrl->value;
3580 #if CONFIG_SENSOR_Contrast
3581 case V4L2_CID_CONTRAST:
3583 if (ctrl->value != sensor->info_priv.contrast)
3585 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3589 sensor->info_priv.contrast = ctrl->value;
3594 #if CONFIG_SENSOR_WhiteBalance
3595 case V4L2_CID_DO_WHITE_BALANCE:
3597 if (ctrl->value != sensor->info_priv.whiteBalance)
3599 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3603 sensor->info_priv.whiteBalance = ctrl->value;
3608 #if CONFIG_SENSOR_Mirror
3609 case V4L2_CID_HFLIP:
3611 if (ctrl->value != sensor->info_priv.mirror)
3613 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3615 sensor->info_priv.mirror = ctrl->value;
3620 #if CONFIG_SENSOR_Flip
3621 case V4L2_CID_VFLIP:
3623 if (ctrl->value != sensor->info_priv.flip)
3625 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3627 sensor->info_priv.flip = ctrl->value;
3638 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3640 const struct v4l2_queryctrl *qctrl;
3641 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3642 struct sensor *sensor = to_sensor(client);
3644 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3648 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3652 switch (ext_ctrl->id)
3654 case V4L2_CID_SCENE:
3656 ext_ctrl->value = sensor->info_priv.scene;
3659 case V4L2_CID_EFFECT:
3661 ext_ctrl->value = sensor->info_priv.effect;
3664 case V4L2_CID_ZOOM_ABSOLUTE:
3666 ext_ctrl->value = sensor->info_priv.digitalzoom;
3669 case V4L2_CID_ZOOM_RELATIVE:
3673 case V4L2_CID_FOCUS_ABSOLUTE:
3677 case V4L2_CID_FOCUS_RELATIVE:
3681 case V4L2_CID_FLASH:
3683 ext_ctrl->value = sensor->info_priv.flash;
3691 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3693 const struct v4l2_queryctrl *qctrl;
3694 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3695 struct sensor *sensor = to_sensor(client);
3698 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3702 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3707 switch (ext_ctrl->id)
3709 #if CONFIG_SENSOR_Scene
3710 case V4L2_CID_SCENE:
3712 if (ext_ctrl->value != sensor->info_priv.scene)
3714 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3716 sensor->info_priv.scene = ext_ctrl->value;
3721 #if CONFIG_SENSOR_Effect
3722 case V4L2_CID_EFFECT:
3724 if (ext_ctrl->value != sensor->info_priv.effect)
3726 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3728 sensor->info_priv.effect= ext_ctrl->value;
3733 #if CONFIG_SENSOR_DigitalZoom
3734 case V4L2_CID_ZOOM_ABSOLUTE:
3736 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3739 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3741 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3743 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3745 sensor->info_priv.digitalzoom += val_offset;
3747 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3752 case V4L2_CID_ZOOM_RELATIVE:
3754 if (ext_ctrl->value)
3756 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3758 sensor->info_priv.digitalzoom += ext_ctrl->value;
3760 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3765 #if CONFIG_SENSOR_Focus
3766 case V4L2_CID_FOCUS_ABSOLUTE:
3768 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3773 case V4L2_CID_FOCUS_RELATIVE:
3775 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3778 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3781 case V4L2_CID_FOCUS_AUTO:
3783 if (ext_ctrl->value) {
3784 if ((ext_ctrl->value==1) || (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus)) {
3785 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO,ext_ctrl->rect) != 0) {
3786 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
3787 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3792 if (ext_ctrl->value == 1)
3793 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3794 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3795 if (ext_ctrl->value == 0)
3796 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3800 case V4L2_CID_FOCUS_CONTINUOUS:
3802 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3803 if (ext_ctrl->value == 1) {
3804 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS,NULL) != 0) {
3805 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
3806 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3810 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3813 if (ext_ctrl->value == 0)
3814 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3819 #if CONFIG_SENSOR_Flash
3820 case V4L2_CID_FLASH:
3822 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3824 sensor->info_priv.flash = ext_ctrl->value;
3826 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3837 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3839 struct i2c_client *client = v4l2_get_subdevdata(sd);
3840 struct soc_camera_device *icd = client->dev.platform_data;
3841 int i, error_cnt=0, error_idx=-1;
3844 for (i=0; i<ext_ctrl->count; i++) {
3845 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3852 error_idx = ext_ctrl->count;
3854 if (error_idx != -1) {
3855 ext_ctrl->error_idx = error_idx;
3862 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3864 struct i2c_client *client = v4l2_get_subdevdata(sd);
3865 struct soc_camera_device *icd = client->dev.platform_data;
3866 int i, error_cnt=0, error_idx=-1;
3868 for (i=0; i<ext_ctrl->count; i++) {
3869 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3876 error_idx = ext_ctrl->count;
3878 if (error_idx != -1) {
3879 ext_ctrl->error_idx = error_idx;
3886 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3888 struct i2c_client *client = v4l2_get_subdevdata(sd);
3889 struct sensor *sensor = to_sensor(client);
3890 #if CONFIG_SENSOR_Focus
3891 struct soc_camera_device *icd = client->dev.platform_data;
3892 struct v4l2_mbus_framefmt mf;
3896 sensor->info_priv.enable = 1;
3897 #if CONFIG_SENSOR_Focus
3898 mf.width = icd->user_width;
3899 mf.height = icd->user_height;
3900 mf.code = sensor->info_priv.fmt.code;
3901 mf.colorspace = sensor->info_priv.fmt.colorspace;
3902 mf.field = V4L2_FIELD_NONE;
3903 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3904 if (sensor_fmt_capturechk(sd, &mf) == false) {
3905 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3906 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false,NULL);
3907 sensor->info_priv.affm_reinit = 0;
3911 } else if (enable == 0) {
3912 sensor->info_priv.enable = 0;
3913 #if CONFIG_SENSOR_Focus
3914 flush_workqueue(sensor->sensor_wq);
3920 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3921 * this wasn't our capture interface, so, we wait for the right one */
3922 static int sensor_video_probe(struct soc_camera_device *icd,
3923 struct i2c_client *client)
3927 struct sensor *sensor = to_sensor(client);
3929 /* We must have a parent by now. And it cannot be a wrong one.
3930 * So this entire test is completely redundant. */
3931 if (!icd->dev.parent ||
3932 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3935 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3937 goto sensor_video_probe_err;
3940 ret = sensor_write(client, 0x3008, 0x80);
3942 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3944 goto sensor_video_probe_err;
3946 mdelay(5); //delay 5 microseconds
3948 /* check if it is an sensor sensor */
3949 ret = sensor_read(client, 0x300a, &value);
3951 SENSOR_TR("read chip id high byte failed\n");
3953 goto sensor_video_probe_err;
3956 pid |= (value << 8);
3958 ret = sensor_read(client, 0x300b, &value);
3960 SENSOR_TR("read chip id low byte failed\n");
3962 goto sensor_video_probe_err;
3965 pid |= (value & 0xff);
3966 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3967 if (pid == SENSOR_ID) {
3968 sensor->model = SENSOR_V4L2_IDENT;
3970 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3972 goto sensor_video_probe_err;
3977 sensor_video_probe_err:
3980 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3982 struct i2c_client *client = v4l2_get_subdevdata(sd);
3983 struct soc_camera_device *icd = client->dev.platform_data;
3984 struct sensor *sensor = to_sensor(client);
3987 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3990 case RK29_CAM_SUBDEV_DEACTIVATE:
3992 #if CONFIG_SENSOR_Flash //hhb
3993 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3995 sensor_deactivate(client);
3998 case RK29_CAM_SUBDEV_IOREQUEST:
4000 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
4001 if (sensor->sensor_io_request != NULL) {
4002 sensor->sensor_gpio_res = NULL;
4003 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
4004 if (sensor->sensor_io_request->gpio_res[i].dev_name &&
4005 (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
4006 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
4009 if (sensor->sensor_gpio_res == NULL) {
4010 SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
4012 goto sensor_ioctl_end;
4015 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
4017 goto sensor_ioctl_end;
4019 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
4021 #if CONFIG_SENSOR_Flash
4022 if (sensor->sensor_gpio_res) {
4023 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
4024 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
4025 for (i = 0; i < icd->ops->num_controls; i++) {
4026 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
4027 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
4028 sensor_controls[i].id=0xffff;
4031 sensor->info_priv.flash = 0xff;
4032 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
4033 }else{ //two cameras are the same,need to deal diffrently ,zyc
4034 for (i = 0; i < icd->ops->num_controls; i++) {
4035 if(0xffff == icd->ops->controls[i].id){
4036 sensor_controls[i].id=V4L2_CID_FLASH;
4046 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4055 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
4056 enum v4l2_mbus_pixelcode *code)
4058 if (index >= ARRAY_SIZE(sensor_colour_fmts))
4061 *code = sensor_colour_fmts[index].code;
4064 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
4065 .init = sensor_init,
4066 .g_ctrl = sensor_g_control,
4067 .s_ctrl = sensor_s_control,
4068 .g_ext_ctrls = sensor_g_ext_controls,
4069 .s_ext_ctrls = sensor_s_ext_controls,
4070 .g_chip_ident = sensor_g_chip_ident,
4071 .ioctl = sensor_ioctl,
4074 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
4075 .s_mbus_fmt = sensor_s_fmt,
4076 .g_mbus_fmt = sensor_g_fmt,
4077 .try_mbus_fmt = sensor_try_fmt,
4078 .enum_mbus_fmt = sensor_enum_fmt,
4079 .s_stream = sensor_s_stream,
4082 static struct v4l2_subdev_ops sensor_subdev_ops = {
4083 .core = &sensor_subdev_core_ops,
4084 .video = &sensor_subdev_video_ops,
4087 static int sensor_probe(struct i2c_client *client,
4088 const struct i2c_device_id *did)
4090 struct sensor *sensor;
4091 struct soc_camera_device *icd = client->dev.platform_data;
4092 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
4093 struct soc_camera_link *icl;
4096 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
4098 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
4102 icl = to_soc_camera_link(icd);
4104 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
4108 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
4109 dev_warn(&adapter->dev,
4110 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
4114 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
4118 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
4120 /* Second stage probe - when a capture adapter is there */
4121 icd->ops = &sensor_ops;
4122 sensor->info_priv.fmt = sensor_colour_fmts[0];
4123 #if CONFIG_SENSOR_I2C_NOSCHED
4124 atomic_set(&sensor->tasklock_cnt,0);
4127 ret = sensor_video_probe(icd, client);
4130 i2c_set_clientdata(client, NULL);
4134 #if CONFIG_SENSOR_Focus
4135 sensor->sensor_wq = create_singlethread_workqueue(SENSOR_NAME_STRING(_af_workqueue));
4136 if (sensor->sensor_wq == NULL)
4137 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
4138 mutex_init(&sensor->wq_lock);
4141 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4142 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
4146 static int sensor_remove(struct i2c_client *client)
4148 struct sensor *sensor = to_sensor(client);
4149 struct soc_camera_device *icd = client->dev.platform_data;
4151 #if CONFIG_SENSOR_Focus
4152 if (sensor->sensor_wq) {
4153 destroy_workqueue(sensor->sensor_wq);
4154 sensor->sensor_wq = NULL;
4159 i2c_set_clientdata(client, NULL);
4160 client->driver = NULL;
4166 static const struct i2c_device_id sensor_id[] = {
4167 {SENSOR_NAME_STRING(), 0 },
4170 MODULE_DEVICE_TABLE(i2c, sensor_id);
4172 static struct i2c_driver sensor_i2c_driver = {
4174 .name = SENSOR_NAME_STRING(),
4176 .probe = sensor_probe,
4177 .remove = sensor_remove,
4178 .id_table = sensor_id,
4181 static int __init sensor_mod_init(void)
4183 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4184 return i2c_add_driver(&sensor_i2c_driver);
4187 static void __exit sensor_mod_exit(void)
4189 i2c_del_driver(&sensor_i2c_driver);
4192 device_initcall_sync(sensor_mod_init);
4193 module_exit(sensor_mod_exit);
4195 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4196 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4197 MODULE_LICENSE("GPL");