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 <mach/rk29_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_PIX_FMT_UYVY
57 #define SENSOR_INIT_PIXFMT V4L2_PIX_FMT_YUYV
59 #define CONFIG_SENSOR_WhiteBalance 1
60 #define CONFIG_SENSOR_Brightness 0
61 #define CONFIG_SENSOR_Contrast 0
62 #define CONFIG_SENSOR_Saturation 0
63 #define CONFIG_SENSOR_Effect 1
64 #define CONFIG_SENSOR_Scene 1
65 #define CONFIG_SENSOR_DigitalZoom 0
66 #define CONFIG_SENSOR_Exposure 0
67 #define CONFIG_SENSOR_Flash 0
68 #define CONFIG_SENSOR_Mirror 0
69 #define CONFIG_SENSOR_Flip 0
70 #ifdef CONFIG_OV5640_AUTOFOCUS
71 #define CONFIG_SENSOR_Focus 1
72 #include "ov5640_af_firmware.c"
74 #define CONFIG_SENSOR_Focus 0
77 #define CONFIG_SENSOR_I2C_SPEED 250000 /* Hz */
78 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
79 #define CONFIG_SENSOR_I2C_NOSCHED 0
80 #define CONFIG_SENSOR_I2C_RDWRCHK 0
82 #define CONFIG_SENSOR_WRITE_REGS 1
83 #define WRITE_REGS_NUM 100
85 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
86 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
87 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
89 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
90 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
91 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
92 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
93 #define COLOR_TEMPERATURE_OFFICE_DN 3500
94 #define COLOR_TEMPERATURE_OFFICE_UP 5000
95 #define COLOR_TEMPERATURE_HOME_DN 2500
96 #define COLOR_TEMPERATURE_HOME_UP 3500
98 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
99 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
101 #define SENSOR_AF_IS_ERR (0x00<<0)
102 #define SENSOR_AF_IS_OK (0x01<<0)
103 #define SENSOR_INIT_IS_ERR (0x00<<28)
104 #define SENSOR_INIT_IS_OK (0x01<<28)
106 #if CONFIG_SENSOR_Focus
107 /*#define SENSOR_AF_MODE_INFINITY 0
108 #define SENSOR_AF_MODE_MACRO 1
109 #define SENSOR_AF_MODE_FIXED 2
110 #define SENSOR_AF_MODE_AUTO 3
111 #define SENSOR_AF_MODE_CONTINUOUS 4
112 #define SENSOR_AF_MODE_CLOSE 5*/
113 #define SENSOR_AF_MODE_AUTO 0
114 #define SENSOR_AF_MODE_CLOSE 1
117 #if CONFIG_SENSOR_Focus
118 /* ov5640 VCM Command and Status Registers */
119 #define CMD_MAIN_Reg 0x3022
120 //#define CMD_TAG_Reg 0x3023
121 #define CMD_ACK_Reg 0x3023
122 #define CMD_PARA0_Reg 0x3024
123 #define CMD_PARA1_Reg 0x3025
124 #define CMD_PARA2_Reg 0x3026
125 #define CMD_PARA3_Reg 0x3027
126 #define CMD_PARA4_Reg 0x3028
128 //#define STA_ZONE_Reg 0x3026
129 #define STA_FOCUS_Reg 0x3029
131 /* ov5640 VCM Command */
132 #define OverlayEn_Cmd 0x01
133 #define OverlayDis_Cmd 0x02
134 #define SingleFocus_Cmd 0x03
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
144 #define Zone_configuration_Cmd 0x12
145 #define Trig_autofocus_Cmd 0x03
146 #define Get_focus_result_Cmd 0x07
148 /* ov5640 Focus State */
149 //#define S_FIRWRE 0xFF /*Firmware is downloaded and not run*/
150 #define S_STARTUP 0x7e /*Firmware is initializing*/
152 #define S_IDLE 0x70 /*Idle state, focus is released; lens is located at the furthest position.*/
153 #define S_FOCUSING 0x00 /*Auto Focus is running.*/
154 #define S_FOCUSED 0x10 /*Auto Focus is completed.*/
156 #define S_CAPTURE 0x12
159 /* ov5640 Zone State */
160 #define Zone_Is_Focused(a, zone_val) (zone_val&(1<<(a-3)))
161 #define Zone_Get_ID(zone_val) (zone_val&0x03)
163 #define Zone_CenterMode 0x01
164 #define Zone_5xMode 0x02
165 #define Zone_5PlusMode 0x03
166 #define Zone_4fMode 0x04
168 #define ZoneSel_Auto 0x0b
169 #define ZoneSel_SemiAuto 0x0c
170 #define ZoneSel_Manual 0x0d
171 #define ZoneSel_Rotate 0x0e
173 /* ov5640 Step Focus Commands */
174 #define StepFocus_Near_Tag 0x01
175 #define StepFocus_Far_Tag 0x02
176 #define StepFocus_Furthest_Tag 0x03
177 #define StepFocus_Nearest_Tag 0x04
178 #define StepFocus_Spec_Tag 0x10
181 /* init 800X600 SVGA */
182 static struct reginfo sensor_init_data[] =
442 /* 720p 15fps @ 1280x720 */
444 static struct reginfo sensor_720p[]=
485 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
487 static struct reginfo sensor_1080p[]=
493 /* 2592X1944 QSXGA */
494 static struct reginfo sensor_qsxga[] =
535 static struct reginfo sensor_qxga[] =
577 static struct reginfo sensor_uxga[] =
619 static struct reginfo sensor_sxga[] =
660 static struct reginfo sensor_xga[] =
700 static struct reginfo sensor_svga[] =
738 static struct reginfo sensor_vga[] =
744 static struct reginfo sensor_cif[] =
751 static struct reginfo sensor_qvga[] =
758 static struct reginfo sensor_qcif[] =
764 static struct reginfo sensor_ClrFmt_YUYV[]=
770 static struct reginfo sensor_ClrFmt_UYVY[]=
777 #if CONFIG_SENSOR_WhiteBalance
778 static struct reginfo sensor_WhiteB_Auto[]=
805 /* Cloudy Colour Temperature : 6500K - 8000K */
806 static struct reginfo sensor_WhiteB_Cloudy[]=
817 /* ClearDay Colour Temperature : 5000K - 6500K */
818 static struct reginfo sensor_WhiteB_ClearDay[]=
829 /* Office Colour Temperature : 3500K - 5000K */
830 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
841 /* Home Colour Temperature : 2500K - 3500K */
842 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
853 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
854 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
858 #if CONFIG_SENSOR_Brightness
859 static struct reginfo sensor_Brightness0[]=
863 static struct reginfo sensor_Brightness1[]=
868 static struct reginfo sensor_Brightness2[]=
872 static struct reginfo sensor_Brightness3[]=
876 static struct reginfo sensor_Brightness4[]=
881 static struct reginfo sensor_Brightness5[]=
885 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
886 sensor_Brightness4, sensor_Brightness5,NULL,
891 #if CONFIG_SENSOR_Effect
892 static struct reginfo sensor_Effect_Normal[] =
898 static struct reginfo sensor_Effect_WandB[] =
906 static struct reginfo sensor_Effect_Sepia[] =
915 static struct reginfo sensor_Effect_Negative[] =
921 };static struct reginfo sensor_Effect_Bluish[] =
931 static struct reginfo sensor_Effect_Green[] =
940 /*static struct reginfo sensor_Effect_Reddish[] =
950 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
951 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
954 #if CONFIG_SENSOR_Exposure
955 static struct reginfo sensor_Exposure0[]=
959 static struct reginfo sensor_Exposure1[]=
963 static struct reginfo sensor_Exposure2[]=
968 static struct reginfo sensor_Exposure3[]=
972 static struct reginfo sensor_Exposure4[]=
976 static struct reginfo sensor_Exposure5[]=
980 static struct reginfo sensor_Exposure6[]=
984 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
985 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
988 #if CONFIG_SENSOR_Saturation
989 static struct reginfo sensor_Saturation0[]=
994 static struct reginfo sensor_Saturation1[]=
999 static struct reginfo sensor_Saturation2[]=
1001 {SEQUENCE_END, 0x00}
1002 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1005 #if CONFIG_SENSOR_Contrast
1006 static struct reginfo sensor_Contrast0[]=
1008 {SEQUENCE_END, 0x00}
1011 static struct reginfo sensor_Contrast1[]=
1013 {SEQUENCE_END, 0x00}
1015 static struct reginfo sensor_Contrast2[]=
1017 {SEQUENCE_END, 0x00}
1020 static struct reginfo sensor_Contrast3[]=
1022 {SEQUENCE_END, 0x00}
1025 static struct reginfo sensor_Contrast4[]=
1027 {SEQUENCE_END, 0x00}
1031 static struct reginfo sensor_Contrast5[]=
1033 {SEQUENCE_END, 0x00}
1036 static struct reginfo sensor_Contrast6[]=
1038 {SEQUENCE_END, 0x00}
1040 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1041 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1045 #if CONFIG_SENSOR_Mirror
1046 static struct reginfo sensor_MirrorOn[]=
1048 {SEQUENCE_END, 0x00}
1050 static struct reginfo sensor_MirrorOff[]=
1052 {SEQUENCE_END, 0x00}
1054 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1056 #if CONFIG_SENSOR_Flip
1057 static struct reginfo sensor_FlipOn[]=
1059 {SEQUENCE_END, 0x00}
1062 static struct reginfo sensor_FlipOff[]=
1064 {SEQUENCE_END, 0x00}
1066 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1069 #if CONFIG_SENSOR_Scene
1070 static struct reginfo sensor_SceneAuto[] =
1073 {SEQUENCE_END, 0x00}
1075 static struct reginfo sensor_SceneNight[] =
1077 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1095 {SEQUENCE_END, 0x00}
1097 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1100 #if CONFIG_SENSOR_DigitalZoom
1101 static struct reginfo sensor_Zoom0[] =
1103 {SEQUENCE_END, 0x00}
1105 static struct reginfo sensor_Zoom1[] =
1107 {SEQUENCE_END, 0x00}
1110 static struct reginfo sensor_Zoom2[] =
1112 {SEQUENCE_END, 0x00}
1116 static struct reginfo sensor_Zoom3[] =
1118 {SEQUENCE_END, 0x00}
1120 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1122 static const struct v4l2_querymenu sensor_menus[] =
1124 #if CONFIG_SENSOR_WhiteBalance
1125 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1126 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1127 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1130 #if CONFIG_SENSOR_Effect
1131 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1132 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1133 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1136 #if CONFIG_SENSOR_Scene
1137 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1140 #if CONFIG_SENSOR_Flash
1141 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1142 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1146 static const struct v4l2_queryctrl sensor_controls[] =
1148 #if CONFIG_SENSOR_WhiteBalance
1150 .id = V4L2_CID_DO_WHITE_BALANCE,
1151 .type = V4L2_CTRL_TYPE_MENU,
1152 .name = "White Balance Control",
1160 #if CONFIG_SENSOR_Brightness
1162 .id = V4L2_CID_BRIGHTNESS,
1163 .type = V4L2_CTRL_TYPE_INTEGER,
1164 .name = "Brightness Control",
1172 #if CONFIG_SENSOR_Effect
1174 .id = V4L2_CID_EFFECT,
1175 .type = V4L2_CTRL_TYPE_MENU,
1176 .name = "Effect Control",
1184 #if CONFIG_SENSOR_Exposure
1186 .id = V4L2_CID_EXPOSURE,
1187 .type = V4L2_CTRL_TYPE_INTEGER,
1188 .name = "Exposure Control",
1196 #if CONFIG_SENSOR_Saturation
1198 .id = V4L2_CID_SATURATION,
1199 .type = V4L2_CTRL_TYPE_INTEGER,
1200 .name = "Saturation Control",
1208 #if CONFIG_SENSOR_Contrast
1210 .id = V4L2_CID_CONTRAST,
1211 .type = V4L2_CTRL_TYPE_INTEGER,
1212 .name = "Contrast Control",
1220 #if CONFIG_SENSOR_Mirror
1222 .id = V4L2_CID_HFLIP,
1223 .type = V4L2_CTRL_TYPE_BOOLEAN,
1224 .name = "Mirror Control",
1232 #if CONFIG_SENSOR_Flip
1234 .id = V4L2_CID_VFLIP,
1235 .type = V4L2_CTRL_TYPE_BOOLEAN,
1236 .name = "Flip Control",
1244 #if CONFIG_SENSOR_Scene
1246 .id = V4L2_CID_SCENE,
1247 .type = V4L2_CTRL_TYPE_MENU,
1248 .name = "Scene Control",
1256 #if CONFIG_SENSOR_DigitalZoom
1258 .id = V4L2_CID_ZOOM_RELATIVE,
1259 .type = V4L2_CTRL_TYPE_INTEGER,
1260 .name = "DigitalZoom Control",
1266 .id = V4L2_CID_ZOOM_ABSOLUTE,
1267 .type = V4L2_CTRL_TYPE_INTEGER,
1268 .name = "DigitalZoom Control",
1276 #if CONFIG_SENSOR_Focus
1278 .id = V4L2_CID_FOCUS_RELATIVE,
1279 .type = V4L2_CTRL_TYPE_INTEGER,
1280 .name = "Focus Control",
1286 .id = V4L2_CID_FOCUS_ABSOLUTE,
1287 .type = V4L2_CTRL_TYPE_INTEGER,
1288 .name = "Focus Control",
1292 .default_value = 125,
1295 .id = V4L2_CID_FOCUS_AUTO,
1296 .type = V4L2_CTRL_TYPE_BOOLEAN,
1297 .name = "Focus Control",
1303 .id = V4L2_CID_FOCUS_CONTINUOUS,
1304 .type = V4L2_CTRL_TYPE_BOOLEAN,
1305 .name = "Focus Control",
1313 #if CONFIG_SENSOR_Flash
1315 .id = V4L2_CID_FLASH,
1316 .type = V4L2_CTRL_TYPE_MENU,
1317 .name = "Flash Control",
1326 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1327 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1328 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1329 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1330 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1331 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1332 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1333 static int sensor_resume(struct soc_camera_device *icd);
1334 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1335 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1336 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1337 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1338 static int sensor_deactivate(struct i2c_client *client);
1340 static struct soc_camera_ops sensor_ops =
1342 .suspend = sensor_suspend,
1343 .resume = sensor_resume,
1344 .set_bus_param = sensor_set_bus_param,
1345 .query_bus_param = sensor_query_bus_param,
1346 .controls = sensor_controls,
1347 .menus = sensor_menus,
1348 .num_controls = ARRAY_SIZE(sensor_controls),
1349 .num_menus = ARRAY_SIZE(sensor_menus),
1352 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1353 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1354 .colorspace = _colorspace }
1356 #define JPG_FMT(_name, _depth, _fourcc) \
1357 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1359 static const struct soc_camera_data_format sensor_colour_formats[] = {
1360 JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1361 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1363 enum sensor_work_state
1365 sensor_work_ready = 0,
1370 struct i2c_client *client;
1371 struct delayed_work dwork;
1372 enum sensor_work_state state;
1375 typedef struct sensor_info_priv_s
1391 unsigned char mirror; /* HFLIP */
1392 unsigned char flip; /* VFLIP */
1393 struct reginfo *winseqe_cur_addr;
1394 unsigned int pixfmt;
1395 unsigned int enable;
1396 unsigned int funmodule_state;
1397 } sensor_info_priv_t;
1401 struct sensor_parameter
1403 unsigned short int preview_maxlines;
1404 unsigned short int preview_exposure;
1405 unsigned short int preview_line_width;
1406 unsigned short int preview_gain;
1408 unsigned short int capture_framerate;
1409 unsigned short int preview_framerate;
1415 struct v4l2_subdev subdev;
1416 struct i2c_client *client;
1417 sensor_info_priv_t info_priv;
1418 struct sensor_parameter parameter;
1419 struct workqueue_struct *sensor_wq;
1420 struct sensor_work sensor_wk;
1421 struct mutex wq_lock;
1422 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1423 #if CONFIG_SENSOR_I2C_NOSCHED
1424 atomic_t tasklock_cnt;
1426 struct rk29camera_platform_data *sensor_io_request;
1427 struct rk29camera_gpio_res *sensor_gpio_res;
1430 static struct sensor* to_sensor(const struct i2c_client *client)
1432 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1435 static int sensor_task_lock(struct i2c_client *client, int lock)
1437 #if CONFIG_SENSOR_I2C_NOSCHED
1439 struct sensor *sensor = to_sensor(client);
1442 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1443 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1444 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1448 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1449 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1450 goto sensor_task_lock_err;
1455 atomic_add(1, &sensor->tasklock_cnt);
1457 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1458 atomic_sub(1, &sensor->tasklock_cnt);
1460 if (atomic_read(&sensor->tasklock_cnt) == 0)
1465 sensor_task_lock_err:
1474 #if CONFIG_SENSOR_WRITE_REGS
1475 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
1478 struct i2c_msg msg[1];
1481 msg->addr = client->addr;
1482 msg->flags = client->flags;
1483 msg->buf = reg_info;
1484 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1485 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1491 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1492 err = i2c_transfer(client->adapter, msg, 1);
1496 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
1510 /* sensor register write */
1511 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1515 struct i2c_msg msg[1];
1518 buf[1] = reg & 0xFF;
1521 msg->addr = client->addr;
1522 msg->flags = client->flags;
1524 msg->len = sizeof(buf);
1525 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1526 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1531 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1532 err = i2c_transfer(client->adapter, msg, 1);
1537 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1545 /* sensor register read */
1546 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1550 struct i2c_msg msg[2];
1553 buf[1] = reg & 0xFF;
1555 msg[0].addr = client->addr;
1556 msg[0].flags = client->flags;
1558 msg[0].len = sizeof(buf);
1559 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1560 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1562 msg[1].addr = client->addr;
1563 msg[1].flags = client->flags|I2C_M_RD;
1566 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1567 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1571 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1572 err = i2c_transfer(client->adapter, msg, 2);
1578 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1586 /* write a array of registers */
1587 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1591 #if CONFIG_SENSOR_Focus
1592 struct sensor *sensor = to_sensor(client);
1594 #if CONFIG_SENSOR_I2C_RDWRCHK
1597 #if CONFIG_SENSOR_WRITE_REGS
1604 if (sensor_task_lock(client, 1) < 0)
1605 goto sensor_write_array_end;
1606 while (regarray[i].reg != SEQUENCE_END) {
1607 #if CONFIG_SENSOR_Focus
1608 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1609 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1611 goto sensor_write_array_end;
1615 #if CONFIG_SENSOR_WRITE_REGS
1619 reg_length = 0x0001;
1621 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
1624 if(reg_num >= WRITE_REGS_NUM)
1631 size_num = reg_num + 1;
1633 ptemp = phead = (u8*)kmalloc(size_num*sizeof(u8),GFP_KERNEL);
1635 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1637 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1639 *phead = regarray[j].reg >> 8;
1640 *(ptemp+1) = regarray[j].reg & 0xFF;
1642 for( ; reg_num > 0; reg_num --, j++) {
1643 *ptemp ++ = regarray[j].val;
1647 err = sensor_write_regs(client, ptemp,size_num);
1651 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1654 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1659 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1663 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1665 goto sensor_write_array_end;
1668 #if CONFIG_SENSOR_I2C_RDWRCHK
1669 sensor_read(client, regarray[i].reg, &valchk);
1670 if (valchk != regarray[i].val)
1671 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1677 #if CONFIG_SENSOR_Focus
1678 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1679 || (regarray == sensor_init_data)) {
1680 sensor->info_priv.affm_reinit = 1;
1684 sensor_write_array_end:
1685 sensor_task_lock(client,0);
1688 #if CONFIG_SENSOR_I2C_RDWRCHK
1689 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1697 while (regarray[i].reg != 0)
1699 sensor_read(client, regarray[i].reg, &valchk);
1700 if (valchk != regarray[i].val)
1701 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1708 #if CONFIG_SENSOR_Focus
1715 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1718 char read_tag=0xff,cnt;
1721 if (cmdinfo->validate_bit & 0x80) {
1722 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1723 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1724 goto sensor_af_cmdset_err;
1726 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1728 for (i=0; i<4; i++) {
1729 if (cmdinfo->validate_bit & (1<<i)) {
1730 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1731 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]);
1732 goto sensor_af_cmdset_err;
1734 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]);
1738 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
1739 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1740 goto sensor_af_cmdset_err;
1742 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1745 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1746 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1747 goto sensor_af_cmdset_err;
1754 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1755 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1758 } while((read_tag != 0x00)&& (cnt++<100));
1760 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1762 sensor_af_cmdset_err:
1766 static int sensor_af_idlechk(struct i2c_client *client)
1770 struct af_cmdinfo cmdinfo;
1772 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1774 cmdinfo.cmd_tag = 0x01;
1775 cmdinfo.validate_bit = 0x80;
1776 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1778 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1780 goto sensor_af_idlechk_end;
1785 ret = sensor_read(client, CMD_ACK_Reg, &state);
1787 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1789 goto sensor_af_idlechk_end;
1791 }while(0x00 != state);
1794 sensor_af_idlechk_end:
1798 static int sensor_af_single(struct i2c_client *client)
1802 struct af_cmdinfo cmdinfo;
1804 cmdinfo.cmd_tag = 0x01;
1805 cmdinfo.validate_bit = 0x80;
1806 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
1808 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1810 goto sensor_af_single_end;
1820 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1822 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1824 goto sensor_af_single_end;
1826 }while((state == S_FOCUSING) && (cnt<100));
1828 if (state != S_FOCUSED) {
1829 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1831 goto sensor_af_single_end;
1834 sensor_af_single_end:
1838 static int sensor_af_const(struct i2c_client *client)
1847 static int sensor_af_init(struct i2c_client *client)
1852 ret = sensor_write_array(client, sensor_af_firmware);
1854 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1856 goto sensor_af_init_end;
1866 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1868 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1870 goto sensor_af_init_end;
1872 }while((state == S_STARTUP) && (cnt<100));
1874 if (state != S_IDLE) {
1875 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1877 goto sensor_af_init_end;
1881 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1885 static int sensor_af_wq_function(struct i2c_client *client)
1887 struct sensor *sensor = to_sensor(client);
1890 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1892 mutex_lock(&sensor->wq_lock);
1893 if (sensor_af_init(client)) {
1894 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1897 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1898 switch (sensor->info_priv.auto_focus)
1900 /*case SENSOR_AF_MODE_INFINITY:
1904 case SENSOR_AF_MODE_MACRO:
1906 if (focus_pos != 0x00)
1909 sensor_af_idlechk(client);
1910 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
1911 cmdinfo.cmd_para[0] = focus_pos;
1912 cmdinfo.validate_bit = 0x81;
1913 //ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
1916 case SENSOR_AF_MODE_AUTO:
1918 ret = sensor_af_single(client);
1921 /*case SENSOR_AF_MODE_CONTINUOUS:
1923 ret = sensor_af_const(client);
1926 case SENSOR_AF_MODE_CLOSE:
1933 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1934 goto sensor_af_wq_function_end;
1938 SENSOR_DG("%s sensor_af_wq_function set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
1941 sensor_af_wq_function_end:
1942 sensor->sensor_wk.state = sensor_work_ready;
1943 mutex_unlock(&sensor->wq_lock);
1946 static void sensor_af_workqueue(struct work_struct *work)
1948 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
1949 struct i2c_client *client = sensor_work->client;
1951 if (sensor_af_wq_function(client) < 0) {
1952 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
1956 static int sensor_parameter_record(struct i2c_client *client)
1958 u8 ret_l,ret_m,ret_h;
1960 struct sensor *sensor = to_sensor(client);
1962 sensor_write(client,0x3503,0x07); //stop AE/AG
1963 sensor_write(client,0x3406,0x01); //stop AWB
1965 sensor_read(client,0x3500,&ret_h);
1966 sensor_read(client,0x3501, &ret_m);
1967 sensor_read(client,0x3502, &ret_l);
1971 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
1972 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
1973 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
1975 sensor_read(client,0x350c, &ret_h);
1976 sensor_read(client,0x350d, &ret_l);
1977 sensor->parameter.preview_line_width = ret_h & 0xff;
1978 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
1979 //Read back AGC Gain for preview
1980 sensor_read(client,0x350a, &ret_h);
1981 sensor_read(client,0x350b, &tp_l);
1982 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
1984 sensor_read(client,0x380e, &ret_h);
1985 sensor->parameter.preview_maxlines = ret_h;
1986 sensor->parameter.preview_maxlines <<= 8;
1987 sensor_read(client,0x380f, &tp_l);
1988 sensor->parameter.preview_maxlines += tp_l;
1990 sensor->parameter.capture_framerate = 375;
1991 sensor->parameter.preview_framerate = 1500;
1993 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
1994 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
1995 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
1996 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
1997 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
1998 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2000 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);
2003 static int sensor_ae_transfer(struct i2c_client *client)
2008 u16 ulCapture_Exposure;
2009 u32 ulCapture_Exposure_Gain;
2013 u8 reg_l = 0,reg_h =0;
2014 u16 Preview_Maxlines;
2016 u32 Capture_MaxLines;
2017 struct sensor *sensor = to_sensor(client);
2019 //Preview_Maxlines = sensor->parameter.preview_line_width;
2020 Preview_Maxlines = sensor->parameter.preview_maxlines;
2021 Gain = sensor->parameter.preview_gain;
2023 sensor_read(client,0x350c, ®_h);
2024 sensor_read(client,0x350d, ®_l);
2025 Capture_MaxLines = reg_h & 0xff;
2026 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2029 sensor_read(client,0x380e, ®_h);
2030 Capture_MaxLines = reg_h;
2031 Capture_MaxLines <<= 8;
2032 sensor_read(client,0x380f, ®_l);
2033 Capture_MaxLines += reg_l;
2036 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2038 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2040 Lines_10ms = Lines_10ms & 0xffff;
2042 if(Preview_Maxlines == 0)
2043 Preview_Maxlines = 1;
2045 //ulCapture_Exposure =
2046 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2048 ulCapture_Exposure =
2049 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2050 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2052 iCapture_Gain = (Gain & 0x0f) + 16;
2054 iCapture_Gain = iCapture_Gain << 1;
2057 iCapture_Gain = iCapture_Gain << 1;
2060 iCapture_Gain = iCapture_Gain << 1;
2063 iCapture_Gain = iCapture_Gain << 1;
2066 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
2067 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2069 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2070 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2071 if (ulCapture_Exposure > Lines_10ms)
2073 //ulCapture_Exposure *= 1.7;
2074 ulCapture_Exposure /= Lines_10ms;
2075 ulCapture_Exposure *= Lines_10ms;
2078 ulCapture_Exposure = Capture_MaxLines;
2079 //ulCapture_Exposure_Gain *= 1.5;
2082 if(ulCapture_Exposure == 0)
2083 ulCapture_Exposure = 1;
2085 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2086 iCapture_Gain = iCapture_Gain & 0xffff;
2088 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2089 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2090 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2094 if (iCapture_Gain > 31) {
2096 iCapture_Gain = iCapture_Gain >> 1;
2098 if (iCapture_Gain > 31) {
2100 iCapture_Gain = iCapture_Gain >> 1;
2102 if (iCapture_Gain > 31) {
2104 iCapture_Gain = iCapture_Gain >> 1;
2106 if (iCapture_Gain > 31) {
2108 iCapture_Gain = iCapture_Gain >> 1;
2110 if (iCapture_Gain > 16)
2111 Gain |= ((iCapture_Gain -16) & 0x0f);
2114 // write the gain and exposure to 0x350* registers
2115 //m_iWrite0x350b=Gain;
2116 sensor_write(client,0x350b, Gain);
2117 Gain = (Gain >> 8) & 0x01;
2118 sensor_write(client,0x350a, Gain);
2119 //m_iWrite0x3502=ExposureLow;
2120 sensor_write(client,0x3502, ExposureLow);
2121 //m_iWrite0x3501=ExposureMid;
2122 sensor_write(client,0x3501, ExposureMid);
2123 //m_iWrite0x3500=ExposureHigh;
2124 sensor_write(client,0x3500, ExposureHigh);
2125 // SendToFile("Gain = 0x%x\r\n", Gain);
2126 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2127 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2128 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2129 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2130 //camera_timed_wait(200);
2131 //linzhk camera_timed_wait(500);
2133 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2134 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2135 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2136 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2137 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2138 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2140 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2144 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2146 struct soc_camera_link *icl = to_soc_camera_link(icd);
2149 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2153 case Sensor_PowerDown:
2155 if (icl->powerdown) {
2156 ret = icl->powerdown(icd->pdev, on);
2157 if (ret == RK29_CAM_IO_SUCCESS) {
2161 icl->reset(icd->pdev);
2163 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2165 goto sensor_power_end;
2172 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2173 struct sensor *sensor = to_sensor(client);
2175 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2176 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2182 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2190 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2192 struct i2c_client *client = sd->priv;
2193 struct soc_camera_device *icd = client->dev.platform_data;
2194 struct sensor *sensor = to_sensor(client);
2195 const struct v4l2_queryctrl *qctrl;
2199 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2201 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2203 goto sensor_INIT_ERR;
2207 if (sensor_task_lock(client,1)<0)
2208 goto sensor_INIT_ERR;
2209 ret = sensor_write(client, 0x3008, 0x80);
2211 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2213 goto sensor_INIT_ERR;
2216 mdelay(5); //delay 5 microseconds
2217 /* check if it is an sensor sensor */
2218 ret = sensor_read(client, 0x300a, &value);
2220 SENSOR_TR("read chip id high byte failed\n");
2222 goto sensor_INIT_ERR;
2225 pid |= (value << 8);
2227 ret = sensor_read(client, 0x300b, &value);
2229 SENSOR_TR("read chip id low byte failed\n");
2231 goto sensor_INIT_ERR;
2234 pid |= (value & 0xff);
2235 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2237 if (pid == SENSOR_ID) {
2238 sensor->model = SENSOR_V4L2_IDENT;
2240 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2242 goto sensor_INIT_ERR;
2245 ret = sensor_write_array(client, sensor_init_data);
2248 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2249 goto sensor_INIT_ERR;
2251 sensor_task_lock(client,0);
2252 //icd->user_width = SENSOR_INIT_WIDTH;
2253 //icd->user_height = SENSOR_INIT_HEIGHT;
2254 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2255 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
2257 /* sensor sensor information for initialization */
2258 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2260 sensor->info_priv.whiteBalance = qctrl->default_value;
2261 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2263 sensor->info_priv.brightness = qctrl->default_value;
2264 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2266 sensor->info_priv.effect = qctrl->default_value;
2267 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2269 sensor->info_priv.exposure = qctrl->default_value;
2271 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2273 sensor->info_priv.saturation = qctrl->default_value;
2274 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2276 sensor->info_priv.contrast = qctrl->default_value;
2277 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2279 sensor->info_priv.mirror = qctrl->default_value;
2280 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2282 sensor->info_priv.flip = qctrl->default_value;
2283 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2285 sensor->info_priv.scene = qctrl->default_value;
2286 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2288 sensor->info_priv.digitalzoom = qctrl->default_value;
2290 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2291 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2293 sensor->info_priv.focus = qctrl->default_value;
2295 #if CONFIG_SENSOR_Flash
2296 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2298 sensor->info_priv.flash = qctrl->default_value;
2300 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);
2302 sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
2306 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2307 sensor_task_lock(client,0);
2308 sensor_deactivate(client);
2311 static int sensor_deactivate(struct i2c_client *client)
2313 struct soc_camera_device *icd = client->dev.platform_data;
2314 struct sensor *sensor = to_sensor(client);
2316 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2318 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2319 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2320 sensor_task_lock(client, 1);
2321 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2322 sensor_write(client, 0x3018, 0x03); // D5-D0
2323 sensor_write(client,0x3019,0x00); // STROBE,SDA
2324 sensor_task_lock(client, 0);
2326 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2328 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2329 icd->user_width = SENSOR_INIT_WIDTH;
2330 icd->user_height = SENSOR_INIT_HEIGHT;
2331 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2335 static struct reginfo sensor_power_down_sequence[]=
2340 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2343 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2345 if (pm_msg.event == PM_EVENT_SUSPEND) {
2346 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2347 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2349 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2352 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2354 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2359 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2366 static int sensor_resume(struct soc_camera_device *icd)
2370 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2372 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2376 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2380 static int sensor_set_bus_param(struct soc_camera_device *icd,
2381 unsigned long flags)
2387 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2389 struct soc_camera_link *icl = to_soc_camera_link(icd);
2390 unsigned long flags = SENSOR_BUS_PARAM;
2392 return soc_camera_apply_sensor_flags(icl, flags);
2395 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2397 struct i2c_client *client = sd->priv;
2398 struct soc_camera_device *icd = client->dev.platform_data;
2399 struct sensor *sensor = to_sensor(client);
2400 struct v4l2_pix_format *pix = &f->fmt.pix;
2402 pix->width = icd->user_width;
2403 pix->height = icd->user_height;
2404 pix->pixelformat = sensor->info_priv.pixfmt;
2405 pix->field = V4L2_FIELD_NONE;
2406 pix->colorspace = V4L2_COLORSPACE_JPEG;
2410 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
2414 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
2416 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
2418 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
2420 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
2422 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
2427 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2430 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
2434 if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
2436 } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
2441 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2444 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2446 struct i2c_client *client = sd->priv;
2447 struct sensor *sensor = to_sensor(client);
2448 struct v4l2_pix_format *pix = &f->fmt.pix;
2449 const struct v4l2_queryctrl *qctrl;
2450 struct soc_camera_device *icd = client->dev.platform_data;
2451 struct reginfo *winseqe_set_addr=NULL;
2452 int ret = 0, set_w,set_h;
2454 if (sensor->info_priv.pixfmt != pix->pixelformat) {
2455 switch (pix->pixelformat)
2457 case V4L2_PIX_FMT_YUYV:
2459 winseqe_set_addr = sensor_ClrFmt_YUYV;
2462 case V4L2_PIX_FMT_UYVY:
2464 winseqe_set_addr = sensor_ClrFmt_UYVY;
2470 if (winseqe_set_addr != NULL) {
2471 sensor_write_array(client, winseqe_set_addr);
2472 sensor->info_priv.pixfmt = pix->pixelformat;
2474 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2476 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2481 set_h = pix->height;
2483 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2485 winseqe_set_addr = sensor_qcif;
2489 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2491 winseqe_set_addr = sensor_qvga;
2495 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2497 winseqe_set_addr = sensor_cif;
2501 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2503 winseqe_set_addr = sensor_vga;
2507 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2509 winseqe_set_addr = sensor_svga;
2513 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2515 winseqe_set_addr = sensor_xga;
2519 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2521 winseqe_set_addr = sensor_720p;
2525 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2527 winseqe_set_addr = sensor_sxga;
2531 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2533 winseqe_set_addr = sensor_uxga;
2537 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2539 winseqe_set_addr = sensor_1080p;
2543 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2545 winseqe_set_addr = sensor_qxga;
2549 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2551 winseqe_set_addr = sensor_qsxga;
2557 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2558 set_w = SENSOR_INIT_WIDTH;
2559 set_h = SENSOR_INIT_HEIGHT;
2560 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
2563 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2565 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
2566 sensor_parameter_record(client);
2567 /*#if CONFIG_SENSOR_Focus
2568 sensor_af_idlechk(client);
2569 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2570 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2572 #if CONFIG_SENSOR_Flash
2573 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2574 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2575 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2578 }else { /* ddl@rock-chips.com : Video */
2579 #if CONFIG_SENSOR_Flash
2580 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2581 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2582 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2586 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2587 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2588 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2589 ret |= sensor_write_array(client,sensor_init_data);
2590 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2593 ret |= sensor_write_array(client, winseqe_set_addr);
2595 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2596 #if CONFIG_SENSOR_Flash
2597 if (sensor_fmt_capturechk(sd,f) == true) {
2598 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2599 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2600 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2604 goto sensor_s_fmt_end;
2606 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2608 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
2609 sensor_ae_transfer(client);
2610 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2611 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2612 if (sensor->info_priv.whiteBalance != 0) {
2613 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2614 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2616 sensor->info_priv.snap2preview = true;
2617 } else if (sensor_fmt_videochk(sd,f) == true) { /* ddl@rock-chips.com : Video */
2618 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2619 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2621 sensor->info_priv.video2preview = true;
2622 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2623 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2624 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2625 if (sensor->info_priv.snap2preview == true) {
2626 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2627 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2630 sensor->info_priv.video2preview = false;
2631 sensor->info_priv.snap2preview = false;
2633 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2637 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2640 pix->height = set_h;
2645 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2647 struct v4l2_pix_format *pix = &f->fmt.pix;
2648 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
2649 pix->pixelformat == V4L2_PIX_FMT_YUYV;
2652 * With Bayer format enforce even side lengths, but let the user play
2653 * with the starting pixel
2656 if (pix->height > SENSOR_MAX_HEIGHT)
2657 pix->height = SENSOR_MAX_HEIGHT;
2658 else if (pix->height < SENSOR_MIN_HEIGHT)
2659 pix->height = SENSOR_MIN_HEIGHT;
2661 pix->height = ALIGN(pix->height, 2);
2663 if (pix->width > SENSOR_MAX_WIDTH)
2664 pix->width = SENSOR_MAX_WIDTH;
2665 else if (pix->width < SENSOR_MIN_WIDTH)
2666 pix->width = SENSOR_MIN_WIDTH;
2668 pix->width = ALIGN(pix->width, 2);
2673 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2675 struct i2c_client *client = sd->priv;
2677 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2680 if (id->match.addr != client->addr)
2683 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2688 #if CONFIG_SENSOR_Brightness
2689 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2691 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2693 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2695 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2697 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2699 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2702 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2706 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2710 #if CONFIG_SENSOR_Effect
2711 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2713 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2715 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2717 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2719 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2721 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2724 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2728 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2732 #if CONFIG_SENSOR_Exposure
2733 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2735 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2737 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2739 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2741 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2743 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2746 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2750 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2754 #if CONFIG_SENSOR_Saturation
2755 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2757 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2759 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2761 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2763 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2765 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2768 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2772 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2776 #if CONFIG_SENSOR_Contrast
2777 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2779 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2781 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2783 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2785 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2787 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2790 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2794 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2798 #if CONFIG_SENSOR_Mirror
2799 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2801 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2803 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2805 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2807 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2809 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2812 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2816 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2820 #if CONFIG_SENSOR_Flip
2821 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2823 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2825 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2827 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2829 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2831 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2834 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2838 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2842 #if CONFIG_SENSOR_Scene
2843 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2845 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2847 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2849 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2851 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2853 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2856 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2860 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2864 #if CONFIG_SENSOR_WhiteBalance
2865 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2867 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2869 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2871 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2873 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2875 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2878 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2882 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2886 #if CONFIG_SENSOR_DigitalZoom
2887 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2889 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2890 struct sensor *sensor = to_sensor(client);
2891 const struct v4l2_queryctrl *qctrl_info;
2892 int digitalzoom_cur, digitalzoom_total;
2894 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2898 digitalzoom_cur = sensor->info_priv.digitalzoom;
2899 digitalzoom_total = qctrl_info->maximum;
2901 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2903 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2907 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2909 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2913 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2915 *value = digitalzoom_total - digitalzoom_cur;
2918 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2920 *value = 0 - digitalzoom_cur;
2923 digitalzoom_cur += *value;
2925 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2927 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2929 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2932 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2939 #if CONFIG_SENSOR_Focus
2940 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2942 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2943 struct sensor *sensor = to_sensor(client);
2944 const struct v4l2_queryctrl *qctrl_info;
2947 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2951 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2952 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2953 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2956 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2960 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2961 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2964 sensor_set_focus_absolute_end:
2967 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2969 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2970 struct sensor *sensor = to_sensor(client);
2971 const struct v4l2_queryctrl *qctrl_info;
2974 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
2978 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2979 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2980 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2983 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2987 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2988 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2990 sensor_set_focus_relative_end:
2994 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2996 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2997 struct sensor *sensor = to_sensor(client);
3000 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3003 case SENSOR_AF_MODE_AUTO:
3005 ret = sensor_af_single(client);
3009 /*case SENSOR_AF_MODE_MACRO:
3011 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3015 case SENSOR_AF_MODE_INFINITY:
3017 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3021 case SENSOR_AF_MODE_CONTINUOUS:
3023 ret = sensor_af_const(client);
3027 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3032 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3035 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3036 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3042 #if CONFIG_SENSOR_Flash
3043 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3045 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3046 if (value == 3) { /* ddl@rock-chips.com: torch */
3047 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3049 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3051 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3055 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3059 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3061 struct i2c_client *client = sd->priv;
3062 struct sensor *sensor = to_sensor(client);
3063 const struct v4l2_queryctrl *qctrl;
3065 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3069 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3075 case V4L2_CID_BRIGHTNESS:
3077 ctrl->value = sensor->info_priv.brightness;
3080 case V4L2_CID_SATURATION:
3082 ctrl->value = sensor->info_priv.saturation;
3085 case V4L2_CID_CONTRAST:
3087 ctrl->value = sensor->info_priv.contrast;
3090 case V4L2_CID_DO_WHITE_BALANCE:
3092 ctrl->value = sensor->info_priv.whiteBalance;
3095 case V4L2_CID_EXPOSURE:
3097 ctrl->value = sensor->info_priv.exposure;
3100 case V4L2_CID_HFLIP:
3102 ctrl->value = sensor->info_priv.mirror;
3105 case V4L2_CID_VFLIP:
3107 ctrl->value = sensor->info_priv.flip;
3118 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3120 struct i2c_client *client = sd->priv;
3121 struct sensor *sensor = to_sensor(client);
3122 struct soc_camera_device *icd = client->dev.platform_data;
3123 const struct v4l2_queryctrl *qctrl;
3126 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3130 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3136 #if CONFIG_SENSOR_Brightness
3137 case V4L2_CID_BRIGHTNESS:
3139 if (ctrl->value != sensor->info_priv.brightness)
3141 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3145 sensor->info_priv.brightness = ctrl->value;
3150 #if CONFIG_SENSOR_Exposure
3151 case V4L2_CID_EXPOSURE:
3153 if (ctrl->value != sensor->info_priv.exposure)
3155 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3159 sensor->info_priv.exposure = ctrl->value;
3164 #if CONFIG_SENSOR_Saturation
3165 case V4L2_CID_SATURATION:
3167 if (ctrl->value != sensor->info_priv.saturation)
3169 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3173 sensor->info_priv.saturation = ctrl->value;
3178 #if CONFIG_SENSOR_Contrast
3179 case V4L2_CID_CONTRAST:
3181 if (ctrl->value != sensor->info_priv.contrast)
3183 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3187 sensor->info_priv.contrast = ctrl->value;
3192 #if CONFIG_SENSOR_WhiteBalance
3193 case V4L2_CID_DO_WHITE_BALANCE:
3195 if (ctrl->value != sensor->info_priv.whiteBalance)
3197 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3201 sensor->info_priv.whiteBalance = ctrl->value;
3206 #if CONFIG_SENSOR_Mirror
3207 case V4L2_CID_HFLIP:
3209 if (ctrl->value != sensor->info_priv.mirror)
3211 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3213 sensor->info_priv.mirror = ctrl->value;
3218 #if CONFIG_SENSOR_Flip
3219 case V4L2_CID_VFLIP:
3221 if (ctrl->value != sensor->info_priv.flip)
3223 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3225 sensor->info_priv.flip = ctrl->value;
3236 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3238 const struct v4l2_queryctrl *qctrl;
3239 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3240 struct sensor *sensor = to_sensor(client);
3242 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3246 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3250 switch (ext_ctrl->id)
3252 case V4L2_CID_SCENE:
3254 ext_ctrl->value = sensor->info_priv.scene;
3257 case V4L2_CID_EFFECT:
3259 ext_ctrl->value = sensor->info_priv.effect;
3262 case V4L2_CID_ZOOM_ABSOLUTE:
3264 ext_ctrl->value = sensor->info_priv.digitalzoom;
3267 case V4L2_CID_ZOOM_RELATIVE:
3271 case V4L2_CID_FOCUS_ABSOLUTE:
3275 case V4L2_CID_FOCUS_RELATIVE:
3279 case V4L2_CID_FLASH:
3281 ext_ctrl->value = sensor->info_priv.flash;
3289 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3291 const struct v4l2_queryctrl *qctrl;
3292 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3293 struct sensor *sensor = to_sensor(client);
3296 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3300 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3305 switch (ext_ctrl->id)
3307 #if CONFIG_SENSOR_Scene
3308 case V4L2_CID_SCENE:
3310 if (ext_ctrl->value != sensor->info_priv.scene)
3312 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3314 sensor->info_priv.scene = ext_ctrl->value;
3319 #if CONFIG_SENSOR_Effect
3320 case V4L2_CID_EFFECT:
3322 if (ext_ctrl->value != sensor->info_priv.effect)
3324 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3326 sensor->info_priv.effect= ext_ctrl->value;
3331 #if CONFIG_SENSOR_DigitalZoom
3332 case V4L2_CID_ZOOM_ABSOLUTE:
3334 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3337 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3339 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3341 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3343 sensor->info_priv.digitalzoom += val_offset;
3345 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3350 case V4L2_CID_ZOOM_RELATIVE:
3352 if (ext_ctrl->value)
3354 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3356 sensor->info_priv.digitalzoom += ext_ctrl->value;
3358 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3363 #if CONFIG_SENSOR_Focus
3364 case V4L2_CID_FOCUS_ABSOLUTE:
3366 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3371 case V4L2_CID_FOCUS_RELATIVE:
3373 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3376 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3379 case V4L2_CID_FOCUS_AUTO:
3381 if (ext_ctrl->value == 1) {
3382 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
3384 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3385 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3386 if (ext_ctrl->value == 0)
3387 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3391 case V4L2_CID_FOCUS_CONTINUOUS:
3393 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3394 if (ext_ctrl->value == 1) {
3395 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3397 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3400 if (ext_ctrl->value == 0)
3401 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3406 #if CONFIG_SENSOR_Flash
3407 case V4L2_CID_FLASH:
3409 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3411 sensor->info_priv.flash = ext_ctrl->value;
3413 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3424 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3426 struct i2c_client *client = sd->priv;
3427 struct soc_camera_device *icd = client->dev.platform_data;
3428 int i, error_cnt=0, error_idx=-1;
3431 for (i=0; i<ext_ctrl->count; i++) {
3432 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3439 error_idx = ext_ctrl->count;
3441 if (error_idx != -1) {
3442 ext_ctrl->error_idx = error_idx;
3449 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3451 struct i2c_client *client = sd->priv;
3452 struct soc_camera_device *icd = client->dev.platform_data;
3453 int i, error_cnt=0, error_idx=-1;
3455 for (i=0; i<ext_ctrl->count; i++) {
3456 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3463 error_idx = ext_ctrl->count;
3465 if (error_idx != -1) {
3466 ext_ctrl->error_idx = error_idx;
3473 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3475 struct i2c_client *client = sd->priv;
3476 struct sensor *sensor = to_sensor(client);
3477 #if CONFIG_SENSOR_Focus
3478 struct soc_camera_device *icd = client->dev.platform_data;
3479 struct v4l2_format fmt;
3483 sensor->info_priv.enable = 1;
3484 #if CONFIG_SENSOR_Focus
3485 fmt.fmt.pix.width = icd->user_width;
3486 fmt.fmt.pix.height = icd->user_height;
3487 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3488 if (sensor_fmt_capturechk(sd, &fmt) == false) {
3489 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3490 if (sensor->sensor_wq != NULL) {
3491 mutex_lock(&sensor->wq_lock);
3492 if (sensor->sensor_wk.state == sensor_working) {
3493 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
3494 mutex_unlock(&sensor->wq_lock);
3495 goto sensor_s_stream_end;
3497 sensor->sensor_wk.state = sensor_working;
3498 mutex_unlock(&sensor->wq_lock);
3499 sensor->sensor_wk.client = client;
3500 INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
3501 queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
3503 sensor->info_priv.affm_reinit = 0;
3507 } else if (enable == 0) {
3508 sensor->info_priv.enable = 0;
3509 #if CONFIG_SENSOR_Focus
3510 flush_work(&(sensor->sensor_wk.dwork.work));
3511 mutex_lock(&sensor->wq_lock);
3512 sensor->sensor_wk.state = sensor_work_ready;
3513 mutex_unlock(&sensor->wq_lock);
3517 sensor_s_stream_end:
3521 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3522 * this wasn't our capture interface, so, we wait for the right one */
3523 static int sensor_video_probe(struct soc_camera_device *icd,
3524 struct i2c_client *client)
3528 struct sensor *sensor = to_sensor(client);
3530 /* We must have a parent by now. And it cannot be a wrong one.
3531 * So this entire test is completely redundant. */
3532 if (!icd->dev.parent ||
3533 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3536 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3538 goto sensor_video_probe_err;
3541 ret = sensor_write(client, 0x3008, 0x80);
3543 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3545 goto sensor_video_probe_err;
3547 mdelay(5); //delay 5 microseconds
3549 /* check if it is an sensor sensor */
3550 ret = sensor_read(client, 0x300a, &value);
3552 SENSOR_TR("read chip id high byte failed\n");
3554 goto sensor_video_probe_err;
3557 pid |= (value << 8);
3559 ret = sensor_read(client, 0x300b, &value);
3561 SENSOR_TR("read chip id low byte failed\n");
3563 goto sensor_video_probe_err;
3566 pid |= (value & 0xff);
3567 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3568 if (pid == SENSOR_ID) {
3569 sensor->model = SENSOR_V4L2_IDENT;
3571 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3573 goto sensor_video_probe_err;
3576 icd->formats = sensor_colour_formats;
3577 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
3581 sensor_video_probe_err:
3584 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3586 struct i2c_client *client = sd->priv;
3587 struct soc_camera_device *icd = client->dev.platform_data;
3588 struct sensor *sensor = to_sensor(client);
3591 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3594 case RK29_CAM_SUBDEV_DEACTIVATE:
3596 sensor_deactivate(client);
3599 case RK29_CAM_SUBDEV_IOREQUEST:
3601 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3602 if (sensor->sensor_io_request != NULL) {
3603 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
3604 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
3605 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
3606 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
3607 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
3608 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
3611 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3613 goto sensor_ioctl_end;
3615 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3617 #if CONFIG_SENSOR_Flash
3619 if (sensor->sensor_gpio_res) {
3620 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3621 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3622 for (i = 0; i < icd->ops->num_controls; i++) {
3623 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3624 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3627 sensor->info_priv.flash = 0xff;
3628 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3636 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3646 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3647 .init = sensor_init,
3648 .g_ctrl = sensor_g_control,
3649 .s_ctrl = sensor_s_control,
3650 .g_ext_ctrls = sensor_g_ext_controls,
3651 .s_ext_ctrls = sensor_s_ext_controls,
3652 .g_chip_ident = sensor_g_chip_ident,
3653 .ioctl = sensor_ioctl,
3656 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3657 .s_fmt = sensor_s_fmt,
3658 .g_fmt = sensor_g_fmt,
3659 .try_fmt = sensor_try_fmt,
3660 .s_stream = sensor_s_stream,
3663 static struct v4l2_subdev_ops sensor_subdev_ops = {
3664 .core = &sensor_subdev_core_ops,
3665 .video = &sensor_subdev_video_ops,
3668 static int sensor_probe(struct i2c_client *client,
3669 const struct i2c_device_id *did)
3671 struct sensor *sensor;
3672 struct soc_camera_device *icd = client->dev.platform_data;
3673 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3674 struct soc_camera_link *icl;
3677 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3679 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3683 icl = to_soc_camera_link(icd);
3685 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3689 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3690 dev_warn(&adapter->dev,
3691 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3695 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3699 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3701 /* Second stage probe - when a capture adapter is there */
3702 icd->ops = &sensor_ops;
3703 icd->y_skip_top = 0;
3704 #if CONFIG_SENSOR_I2C_NOSCHED
3705 atomic_set(&sensor->tasklock_cnt,0);
3708 ret = sensor_video_probe(icd, client);
3711 i2c_set_clientdata(client, NULL);
3715 #if CONFIG_SENSOR_Focus
3716 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
3717 if (sensor->sensor_wq == NULL)
3718 SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
3719 mutex_init(&sensor->wq_lock);
3720 sensor->sensor_wk.state = sensor_work_ready;
3724 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3728 static int sensor_remove(struct i2c_client *client)
3730 struct sensor *sensor = to_sensor(client);
3731 struct soc_camera_device *icd = client->dev.platform_data;
3733 #if CONFIG_SENSOR_Focus
3734 if (sensor->sensor_wq) {
3735 destroy_workqueue(sensor->sensor_wq);
3736 sensor->sensor_wq = NULL;
3741 i2c_set_clientdata(client, NULL);
3742 client->driver = NULL;
3748 static const struct i2c_device_id sensor_id[] = {
3749 {SENSOR_NAME_STRING(), 0 },
3752 MODULE_DEVICE_TABLE(i2c, sensor_id);
3754 static struct i2c_driver sensor_i2c_driver = {
3756 .name = SENSOR_NAME_STRING(),
3758 .probe = sensor_probe,
3759 .remove = sensor_remove,
3760 .id_table = sensor_id,
3763 static int __init sensor_mod_init(void)
3765 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3766 return i2c_add_driver(&sensor_i2c_driver);
3769 static void __exit sensor_mod_exit(void)
3771 i2c_del_driver(&sensor_i2c_driver);
3774 device_initcall_sync(sensor_mod_init);
3775 module_exit(sensor_mod_exit);
3777 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3778 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3779 MODULE_LICENSE("GPL");