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[] =
746 static struct reginfo sensor_vga[] =
752 static struct reginfo sensor_cif[] =
759 static struct reginfo sensor_qvga[] =
766 static struct reginfo sensor_qcif[] =
772 static struct reginfo sensor_ClrFmt_YUYV[]=
778 static struct reginfo sensor_ClrFmt_UYVY[]=
785 #if CONFIG_SENSOR_WhiteBalance
786 static struct reginfo sensor_WhiteB_Auto[]=
813 /* Cloudy Colour Temperature : 6500K - 8000K */
814 static struct reginfo sensor_WhiteB_Cloudy[]=
825 /* ClearDay Colour Temperature : 5000K - 6500K */
826 static struct reginfo sensor_WhiteB_ClearDay[]=
837 /* Office Colour Temperature : 3500K - 5000K */
838 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
849 /* Home Colour Temperature : 2500K - 3500K */
850 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
861 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
862 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
866 #if CONFIG_SENSOR_Brightness
867 static struct reginfo sensor_Brightness0[]=
871 static struct reginfo sensor_Brightness1[]=
876 static struct reginfo sensor_Brightness2[]=
880 static struct reginfo sensor_Brightness3[]=
884 static struct reginfo sensor_Brightness4[]=
889 static struct reginfo sensor_Brightness5[]=
893 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
894 sensor_Brightness4, sensor_Brightness5,NULL,
899 #if CONFIG_SENSOR_Effect
900 static struct reginfo sensor_Effect_Normal[] =
906 static struct reginfo sensor_Effect_WandB[] =
914 static struct reginfo sensor_Effect_Sepia[] =
923 static struct reginfo sensor_Effect_Negative[] =
929 };static struct reginfo sensor_Effect_Bluish[] =
939 static struct reginfo sensor_Effect_Green[] =
948 /*static struct reginfo sensor_Effect_Reddish[] =
958 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
959 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
962 #if CONFIG_SENSOR_Exposure
963 static struct reginfo sensor_Exposure0[]=
967 static struct reginfo sensor_Exposure1[]=
971 static struct reginfo sensor_Exposure2[]=
976 static struct reginfo sensor_Exposure3[]=
980 static struct reginfo sensor_Exposure4[]=
984 static struct reginfo sensor_Exposure5[]=
988 static struct reginfo sensor_Exposure6[]=
992 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
993 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
996 #if CONFIG_SENSOR_Saturation
997 static struct reginfo sensor_Saturation0[]=
1002 static struct reginfo sensor_Saturation1[]=
1004 {SEQUENCE_END, 0x00}
1007 static struct reginfo sensor_Saturation2[]=
1009 {SEQUENCE_END, 0x00}
1010 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1013 #if CONFIG_SENSOR_Contrast
1014 static struct reginfo sensor_Contrast0[]=
1016 {SEQUENCE_END, 0x00}
1019 static struct reginfo sensor_Contrast1[]=
1021 {SEQUENCE_END, 0x00}
1023 static struct reginfo sensor_Contrast2[]=
1025 {SEQUENCE_END, 0x00}
1028 static struct reginfo sensor_Contrast3[]=
1030 {SEQUENCE_END, 0x00}
1033 static struct reginfo sensor_Contrast4[]=
1035 {SEQUENCE_END, 0x00}
1039 static struct reginfo sensor_Contrast5[]=
1041 {SEQUENCE_END, 0x00}
1044 static struct reginfo sensor_Contrast6[]=
1046 {SEQUENCE_END, 0x00}
1048 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1049 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1053 #if CONFIG_SENSOR_Mirror
1054 static struct reginfo sensor_MirrorOn[]=
1056 {SEQUENCE_END, 0x00}
1058 static struct reginfo sensor_MirrorOff[]=
1060 {SEQUENCE_END, 0x00}
1062 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1064 #if CONFIG_SENSOR_Flip
1065 static struct reginfo sensor_FlipOn[]=
1067 {SEQUENCE_END, 0x00}
1070 static struct reginfo sensor_FlipOff[]=
1072 {SEQUENCE_END, 0x00}
1074 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1077 #if CONFIG_SENSOR_Scene
1078 static struct reginfo sensor_SceneAuto[] =
1081 {SEQUENCE_END, 0x00}
1083 static struct reginfo sensor_SceneNight[] =
1085 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1103 {SEQUENCE_END, 0x00}
1105 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1108 #if CONFIG_SENSOR_DigitalZoom
1109 static struct reginfo sensor_Zoom0[] =
1111 {SEQUENCE_END, 0x00}
1113 static struct reginfo sensor_Zoom1[] =
1115 {SEQUENCE_END, 0x00}
1118 static struct reginfo sensor_Zoom2[] =
1120 {SEQUENCE_END, 0x00}
1124 static struct reginfo sensor_Zoom3[] =
1126 {SEQUENCE_END, 0x00}
1128 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1130 static const struct v4l2_querymenu sensor_menus[] =
1132 #if CONFIG_SENSOR_WhiteBalance
1133 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1134 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1135 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1138 #if CONFIG_SENSOR_Effect
1139 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1140 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1141 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1144 #if CONFIG_SENSOR_Scene
1145 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1148 #if CONFIG_SENSOR_Flash
1149 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1150 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1154 static const struct v4l2_queryctrl sensor_controls[] =
1156 #if CONFIG_SENSOR_WhiteBalance
1158 .id = V4L2_CID_DO_WHITE_BALANCE,
1159 .type = V4L2_CTRL_TYPE_MENU,
1160 .name = "White Balance Control",
1168 #if CONFIG_SENSOR_Brightness
1170 .id = V4L2_CID_BRIGHTNESS,
1171 .type = V4L2_CTRL_TYPE_INTEGER,
1172 .name = "Brightness Control",
1180 #if CONFIG_SENSOR_Effect
1182 .id = V4L2_CID_EFFECT,
1183 .type = V4L2_CTRL_TYPE_MENU,
1184 .name = "Effect Control",
1192 #if CONFIG_SENSOR_Exposure
1194 .id = V4L2_CID_EXPOSURE,
1195 .type = V4L2_CTRL_TYPE_INTEGER,
1196 .name = "Exposure Control",
1204 #if CONFIG_SENSOR_Saturation
1206 .id = V4L2_CID_SATURATION,
1207 .type = V4L2_CTRL_TYPE_INTEGER,
1208 .name = "Saturation Control",
1216 #if CONFIG_SENSOR_Contrast
1218 .id = V4L2_CID_CONTRAST,
1219 .type = V4L2_CTRL_TYPE_INTEGER,
1220 .name = "Contrast Control",
1228 #if CONFIG_SENSOR_Mirror
1230 .id = V4L2_CID_HFLIP,
1231 .type = V4L2_CTRL_TYPE_BOOLEAN,
1232 .name = "Mirror Control",
1240 #if CONFIG_SENSOR_Flip
1242 .id = V4L2_CID_VFLIP,
1243 .type = V4L2_CTRL_TYPE_BOOLEAN,
1244 .name = "Flip Control",
1252 #if CONFIG_SENSOR_Scene
1254 .id = V4L2_CID_SCENE,
1255 .type = V4L2_CTRL_TYPE_MENU,
1256 .name = "Scene Control",
1264 #if CONFIG_SENSOR_DigitalZoom
1266 .id = V4L2_CID_ZOOM_RELATIVE,
1267 .type = V4L2_CTRL_TYPE_INTEGER,
1268 .name = "DigitalZoom Control",
1274 .id = V4L2_CID_ZOOM_ABSOLUTE,
1275 .type = V4L2_CTRL_TYPE_INTEGER,
1276 .name = "DigitalZoom Control",
1284 #if CONFIG_SENSOR_Focus
1286 .id = V4L2_CID_FOCUS_RELATIVE,
1287 .type = V4L2_CTRL_TYPE_INTEGER,
1288 .name = "Focus Control",
1294 .id = V4L2_CID_FOCUS_ABSOLUTE,
1295 .type = V4L2_CTRL_TYPE_INTEGER,
1296 .name = "Focus Control",
1300 .default_value = 125,
1303 .id = V4L2_CID_FOCUS_AUTO,
1304 .type = V4L2_CTRL_TYPE_BOOLEAN,
1305 .name = "Focus Control",
1311 .id = V4L2_CID_FOCUS_CONTINUOUS,
1312 .type = V4L2_CTRL_TYPE_BOOLEAN,
1313 .name = "Focus Control",
1321 #if CONFIG_SENSOR_Flash
1323 .id = V4L2_CID_FLASH,
1324 .type = V4L2_CTRL_TYPE_MENU,
1325 .name = "Flash Control",
1334 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1335 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1336 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1337 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1338 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1339 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1340 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1341 static int sensor_resume(struct soc_camera_device *icd);
1342 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1343 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1344 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1345 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1346 static int sensor_deactivate(struct i2c_client *client);
1348 static struct soc_camera_ops sensor_ops =
1350 .suspend = sensor_suspend,
1351 .resume = sensor_resume,
1352 .set_bus_param = sensor_set_bus_param,
1353 .query_bus_param = sensor_query_bus_param,
1354 .controls = sensor_controls,
1355 .menus = sensor_menus,
1356 .num_controls = ARRAY_SIZE(sensor_controls),
1357 .num_menus = ARRAY_SIZE(sensor_menus),
1360 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1361 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1362 .colorspace = _colorspace }
1364 #define JPG_FMT(_name, _depth, _fourcc) \
1365 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1367 static const struct soc_camera_data_format sensor_colour_formats[] = {
1368 JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1369 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1371 enum sensor_work_state
1373 sensor_work_ready = 0,
1378 struct i2c_client *client;
1379 struct delayed_work dwork;
1380 enum sensor_work_state state;
1383 typedef struct sensor_info_priv_s
1399 unsigned char mirror; /* HFLIP */
1400 unsigned char flip; /* VFLIP */
1401 struct reginfo *winseqe_cur_addr;
1402 unsigned int pixfmt;
1403 unsigned int enable;
1404 unsigned int funmodule_state;
1405 } sensor_info_priv_t;
1409 struct sensor_parameter
1411 unsigned short int preview_maxlines;
1412 unsigned short int preview_exposure;
1413 unsigned short int preview_line_width;
1414 unsigned short int preview_gain;
1416 unsigned short int capture_framerate;
1417 unsigned short int preview_framerate;
1423 struct v4l2_subdev subdev;
1424 struct i2c_client *client;
1425 sensor_info_priv_t info_priv;
1426 struct sensor_parameter parameter;
1427 struct workqueue_struct *sensor_wq;
1428 struct sensor_work sensor_wk;
1429 struct mutex wq_lock;
1430 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1431 #if CONFIG_SENSOR_I2C_NOSCHED
1432 atomic_t tasklock_cnt;
1434 struct rk29camera_platform_data *sensor_io_request;
1435 struct rk29camera_gpio_res *sensor_gpio_res;
1438 static struct sensor* to_sensor(const struct i2c_client *client)
1440 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1443 static int sensor_task_lock(struct i2c_client *client, int lock)
1445 #if CONFIG_SENSOR_I2C_NOSCHED
1447 struct sensor *sensor = to_sensor(client);
1450 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1451 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1452 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1456 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1457 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1458 goto sensor_task_lock_err;
1463 atomic_add(1, &sensor->tasklock_cnt);
1465 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1466 atomic_sub(1, &sensor->tasklock_cnt);
1468 if (atomic_read(&sensor->tasklock_cnt) == 0)
1473 sensor_task_lock_err:
1482 #if CONFIG_SENSOR_WRITE_REGS
1483 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
1486 struct i2c_msg msg[1];
1489 msg->addr = client->addr;
1490 msg->flags = client->flags;
1491 msg->buf = reg_info;
1492 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1493 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1499 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1500 err = i2c_transfer(client->adapter, msg, 1);
1504 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
1518 /* sensor register write */
1519 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1523 struct i2c_msg msg[1];
1526 buf[1] = reg & 0xFF;
1529 msg->addr = client->addr;
1530 msg->flags = client->flags;
1532 msg->len = sizeof(buf);
1533 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1534 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1539 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1540 err = i2c_transfer(client->adapter, msg, 1);
1545 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1553 /* sensor register read */
1554 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1558 struct i2c_msg msg[2];
1561 buf[1] = reg & 0xFF;
1563 msg[0].addr = client->addr;
1564 msg[0].flags = client->flags;
1566 msg[0].len = sizeof(buf);
1567 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1568 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1570 msg[1].addr = client->addr;
1571 msg[1].flags = client->flags|I2C_M_RD;
1574 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1575 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1579 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1580 err = i2c_transfer(client->adapter, msg, 2);
1586 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1594 /* write a array of registers */
1595 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1599 #if CONFIG_SENSOR_Focus
1600 struct sensor *sensor = to_sensor(client);
1602 #if CONFIG_SENSOR_I2C_RDWRCHK
1605 #if CONFIG_SENSOR_WRITE_REGS
1612 if (sensor_task_lock(client, 1) < 0)
1613 goto sensor_write_array_end;
1614 while (regarray[i].reg != SEQUENCE_END) {
1615 #if CONFIG_SENSOR_Focus
1616 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1617 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1619 goto sensor_write_array_end;
1623 #if CONFIG_SENSOR_WRITE_REGS
1627 reg_length = 0x0001;
1629 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
1632 if(reg_num >= WRITE_REGS_NUM)
1639 size_num = reg_num + 1;
1641 ptemp = phead = (u8*)kmalloc(size_num*sizeof(u8),GFP_KERNEL);
1643 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1645 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1647 *phead = regarray[j].reg >> 8;
1648 *(ptemp+1) = regarray[j].reg & 0xFF;
1650 for( ; reg_num > 0; reg_num --, j++) {
1651 *ptemp ++ = regarray[j].val;
1655 err = sensor_write_regs(client, ptemp,size_num);
1659 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1662 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1667 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1671 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1673 goto sensor_write_array_end;
1676 #if CONFIG_SENSOR_I2C_RDWRCHK
1677 sensor_read(client, regarray[i].reg, &valchk);
1678 if (valchk != regarray[i].val)
1679 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1685 #if CONFIG_SENSOR_Focus
1686 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1687 || (regarray == sensor_init_data)) {
1688 sensor->info_priv.affm_reinit = 1;
1692 sensor_write_array_end:
1693 sensor_task_lock(client,0);
1696 #if CONFIG_SENSOR_I2C_RDWRCHK
1697 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1705 while (regarray[i].reg != 0)
1707 sensor_read(client, regarray[i].reg, &valchk);
1708 if (valchk != regarray[i].val)
1709 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1716 #if CONFIG_SENSOR_Focus
1723 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1726 char read_tag=0xff,cnt;
1729 if (cmdinfo->validate_bit & 0x80) {
1730 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1731 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1732 goto sensor_af_cmdset_err;
1734 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1736 for (i=0; i<4; i++) {
1737 if (cmdinfo->validate_bit & (1<<i)) {
1738 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1739 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]);
1740 goto sensor_af_cmdset_err;
1742 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]);
1746 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
1747 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1748 goto sensor_af_cmdset_err;
1750 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1753 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1754 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1755 goto sensor_af_cmdset_err;
1762 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1763 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1766 } while((read_tag != 0x00)&& (cnt++<100));
1768 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1770 sensor_af_cmdset_err:
1774 static int sensor_af_idlechk(struct i2c_client *client)
1778 struct af_cmdinfo cmdinfo;
1780 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1782 cmdinfo.cmd_tag = 0x01;
1783 cmdinfo.validate_bit = 0x80;
1784 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1786 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1788 goto sensor_af_idlechk_end;
1793 ret = sensor_read(client, CMD_ACK_Reg, &state);
1795 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1797 goto sensor_af_idlechk_end;
1799 }while(0x00 != state);
1802 sensor_af_idlechk_end:
1806 static int sensor_af_single(struct i2c_client *client)
1810 struct af_cmdinfo cmdinfo;
1812 cmdinfo.cmd_tag = 0x01;
1813 cmdinfo.validate_bit = 0x80;
1814 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
1816 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1818 goto sensor_af_single_end;
1828 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1830 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1832 goto sensor_af_single_end;
1834 }while((state == S_FOCUSING) && (cnt<100));
1836 if (state != S_FOCUSED) {
1837 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1839 goto sensor_af_single_end;
1842 sensor_af_single_end:
1846 static int sensor_af_const(struct i2c_client *client)
1855 static int sensor_af_init(struct i2c_client *client)
1860 ret = sensor_write_array(client, sensor_af_firmware);
1862 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1864 goto sensor_af_init_end;
1874 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1876 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1878 goto sensor_af_init_end;
1880 }while((state == S_STARTUP) && (cnt<100));
1882 if (state != S_IDLE) {
1883 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1885 goto sensor_af_init_end;
1889 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1893 static int sensor_af_wq_function(struct i2c_client *client)
1895 struct sensor *sensor = to_sensor(client);
1898 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1900 mutex_lock(&sensor->wq_lock);
1901 if (sensor_af_init(client)) {
1902 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1905 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1906 switch (sensor->info_priv.auto_focus)
1908 /*case SENSOR_AF_MODE_INFINITY:
1912 case SENSOR_AF_MODE_MACRO:
1914 if (focus_pos != 0x00)
1917 sensor_af_idlechk(client);
1918 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
1919 cmdinfo.cmd_para[0] = focus_pos;
1920 cmdinfo.validate_bit = 0x81;
1921 //ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
1924 case SENSOR_AF_MODE_AUTO:
1926 ret = sensor_af_single(client);
1929 /*case SENSOR_AF_MODE_CONTINUOUS:
1931 ret = sensor_af_const(client);
1934 case SENSOR_AF_MODE_CLOSE:
1941 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1942 goto sensor_af_wq_function_end;
1946 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);
1949 sensor_af_wq_function_end:
1950 sensor->sensor_wk.state = sensor_work_ready;
1951 mutex_unlock(&sensor->wq_lock);
1954 static void sensor_af_workqueue(struct work_struct *work)
1956 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
1957 struct i2c_client *client = sensor_work->client;
1959 if (sensor_af_wq_function(client) < 0) {
1960 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
1964 static int sensor_parameter_record(struct i2c_client *client)
1966 u8 ret_l,ret_m,ret_h;
1968 struct sensor *sensor = to_sensor(client);
1970 sensor_write(client,0x3503,0x07); //stop AE/AG
1971 sensor_write(client,0x3406,0x01); //stop AWB
1973 sensor_read(client,0x3500,&ret_h);
1974 sensor_read(client,0x3501, &ret_m);
1975 sensor_read(client,0x3502, &ret_l);
1979 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
1980 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
1981 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
1983 sensor_read(client,0x350c, &ret_h);
1984 sensor_read(client,0x350d, &ret_l);
1985 sensor->parameter.preview_line_width = ret_h & 0xff;
1986 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
1987 //Read back AGC Gain for preview
1988 sensor_read(client,0x350a, &ret_h);
1989 sensor_read(client,0x350b, &tp_l);
1990 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
1992 sensor_read(client,0x380e, &ret_h);
1993 sensor->parameter.preview_maxlines = ret_h;
1994 sensor->parameter.preview_maxlines <<= 8;
1995 sensor_read(client,0x380f, &tp_l);
1996 sensor->parameter.preview_maxlines += tp_l;
1998 sensor->parameter.capture_framerate = 375;
1999 sensor->parameter.preview_framerate = 1500;
2001 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
2002 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2003 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2004 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2005 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2006 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2008 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);
2011 static int sensor_ae_transfer(struct i2c_client *client)
2016 u16 ulCapture_Exposure;
2017 u32 ulCapture_Exposure_Gain;
2021 u8 reg_l = 0,reg_h =0;
2022 u16 Preview_Maxlines;
2024 u32 Capture_MaxLines;
2025 struct sensor *sensor = to_sensor(client);
2027 //Preview_Maxlines = sensor->parameter.preview_line_width;
2028 Preview_Maxlines = sensor->parameter.preview_maxlines;
2029 Gain = sensor->parameter.preview_gain;
2031 sensor_read(client,0x350c, ®_h);
2032 sensor_read(client,0x350d, ®_l);
2033 Capture_MaxLines = reg_h & 0xff;
2034 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2037 sensor_read(client,0x380e, ®_h);
2038 Capture_MaxLines = reg_h;
2039 Capture_MaxLines <<= 8;
2040 sensor_read(client,0x380f, ®_l);
2041 Capture_MaxLines += reg_l;
2044 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2046 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2048 Lines_10ms = Lines_10ms & 0xffff;
2050 if(Preview_Maxlines == 0)
2051 Preview_Maxlines = 1;
2053 //ulCapture_Exposure =
2054 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2056 ulCapture_Exposure =
2057 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2058 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2060 iCapture_Gain = (Gain & 0x0f) + 16;
2062 iCapture_Gain = iCapture_Gain << 1;
2065 iCapture_Gain = iCapture_Gain << 1;
2068 iCapture_Gain = iCapture_Gain << 1;
2071 iCapture_Gain = iCapture_Gain << 1;
2074 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
2075 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2077 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2078 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2079 if (ulCapture_Exposure > Lines_10ms)
2081 //ulCapture_Exposure *= 1.7;
2082 ulCapture_Exposure /= Lines_10ms;
2083 ulCapture_Exposure *= Lines_10ms;
2086 ulCapture_Exposure = Capture_MaxLines;
2087 //ulCapture_Exposure_Gain *= 1.5;
2090 if(ulCapture_Exposure == 0)
2091 ulCapture_Exposure = 1;
2093 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2094 iCapture_Gain = iCapture_Gain & 0xffff;
2096 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2097 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2098 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
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 > 31) {
2112 iCapture_Gain = iCapture_Gain >> 1;
2114 if (iCapture_Gain > 31) {
2116 iCapture_Gain = iCapture_Gain >> 1;
2118 if (iCapture_Gain > 16)
2119 Gain |= ((iCapture_Gain -16) & 0x0f);
2122 // write the gain and exposure to 0x350* registers
2123 //m_iWrite0x350b=Gain;
2124 sensor_write(client,0x350b, Gain);
2125 Gain = (Gain >> 8) & 0x01;
2126 sensor_write(client,0x350a, Gain);
2127 //m_iWrite0x3502=ExposureLow;
2128 sensor_write(client,0x3502, ExposureLow);
2129 //m_iWrite0x3501=ExposureMid;
2130 sensor_write(client,0x3501, ExposureMid);
2131 //m_iWrite0x3500=ExposureHigh;
2132 sensor_write(client,0x3500, ExposureHigh);
2133 // SendToFile("Gain = 0x%x\r\n", Gain);
2134 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2135 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2136 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2137 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2138 //camera_timed_wait(200);
2139 //linzhk camera_timed_wait(500);
2141 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2142 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2143 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2144 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2145 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2146 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2148 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2152 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2154 struct soc_camera_link *icl = to_soc_camera_link(icd);
2157 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2161 case Sensor_PowerDown:
2163 if (icl->powerdown) {
2164 ret = icl->powerdown(icd->pdev, on);
2165 if (ret == RK29_CAM_IO_SUCCESS) {
2169 icl->reset(icd->pdev);
2171 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2173 goto sensor_power_end;
2180 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2181 struct sensor *sensor = to_sensor(client);
2183 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2184 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2190 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2198 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2200 struct i2c_client *client = sd->priv;
2201 struct soc_camera_device *icd = client->dev.platform_data;
2202 struct sensor *sensor = to_sensor(client);
2203 const struct v4l2_queryctrl *qctrl;
2207 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2209 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2211 goto sensor_INIT_ERR;
2215 if (sensor_task_lock(client,1)<0)
2216 goto sensor_INIT_ERR;
2217 ret = sensor_write(client, 0x3008, 0x80);
2219 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2221 goto sensor_INIT_ERR;
2224 mdelay(5); //delay 5 microseconds
2225 /* check if it is an sensor sensor */
2226 ret = sensor_read(client, 0x300a, &value);
2228 SENSOR_TR("read chip id high byte failed\n");
2230 goto sensor_INIT_ERR;
2233 pid |= (value << 8);
2235 ret = sensor_read(client, 0x300b, &value);
2237 SENSOR_TR("read chip id low byte failed\n");
2239 goto sensor_INIT_ERR;
2242 pid |= (value & 0xff);
2243 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2245 if (pid == SENSOR_ID) {
2246 sensor->model = SENSOR_V4L2_IDENT;
2248 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2250 goto sensor_INIT_ERR;
2253 ret = sensor_write_array(client, sensor_init_data);
2256 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2257 goto sensor_INIT_ERR;
2259 sensor_task_lock(client,0);
2260 //icd->user_width = SENSOR_INIT_WIDTH;
2261 //icd->user_height = SENSOR_INIT_HEIGHT;
2262 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2263 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
2265 /* sensor sensor information for initialization */
2266 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2268 sensor->info_priv.whiteBalance = qctrl->default_value;
2269 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2271 sensor->info_priv.brightness = qctrl->default_value;
2272 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2274 sensor->info_priv.effect = qctrl->default_value;
2275 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2277 sensor->info_priv.exposure = qctrl->default_value;
2279 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2281 sensor->info_priv.saturation = qctrl->default_value;
2282 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2284 sensor->info_priv.contrast = qctrl->default_value;
2285 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2287 sensor->info_priv.mirror = qctrl->default_value;
2288 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2290 sensor->info_priv.flip = qctrl->default_value;
2291 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2293 sensor->info_priv.scene = qctrl->default_value;
2294 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2296 sensor->info_priv.digitalzoom = qctrl->default_value;
2298 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2299 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2301 sensor->info_priv.focus = qctrl->default_value;
2303 #if CONFIG_SENSOR_Flash
2304 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2306 sensor->info_priv.flash = qctrl->default_value;
2308 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);
2310 sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
2314 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2315 sensor_task_lock(client,0);
2316 sensor_deactivate(client);
2319 static int sensor_deactivate(struct i2c_client *client)
2321 struct soc_camera_device *icd = client->dev.platform_data;
2322 struct sensor *sensor = to_sensor(client);
2324 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2326 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2327 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2328 sensor_task_lock(client, 1);
2329 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2330 sensor_write(client, 0x3018, 0x03); // D5-D0
2331 sensor_write(client,0x3019,0x00); // STROBE,SDA
2332 sensor_task_lock(client, 0);
2334 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2336 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2337 icd->user_width = SENSOR_INIT_WIDTH;
2338 icd->user_height = SENSOR_INIT_HEIGHT;
2339 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2343 static struct reginfo sensor_power_down_sequence[]=
2348 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2351 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2353 if (pm_msg.event == PM_EVENT_SUSPEND) {
2354 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2355 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2357 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2360 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2362 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2367 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2374 static int sensor_resume(struct soc_camera_device *icd)
2378 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2380 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2384 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2388 static int sensor_set_bus_param(struct soc_camera_device *icd,
2389 unsigned long flags)
2395 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2397 struct soc_camera_link *icl = to_soc_camera_link(icd);
2398 unsigned long flags = SENSOR_BUS_PARAM;
2400 return soc_camera_apply_sensor_flags(icl, flags);
2403 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2405 struct i2c_client *client = sd->priv;
2406 struct soc_camera_device *icd = client->dev.platform_data;
2407 struct sensor *sensor = to_sensor(client);
2408 struct v4l2_pix_format *pix = &f->fmt.pix;
2410 pix->width = icd->user_width;
2411 pix->height = icd->user_height;
2412 pix->pixelformat = sensor->info_priv.pixfmt;
2413 pix->field = V4L2_FIELD_NONE;
2414 pix->colorspace = V4L2_COLORSPACE_JPEG;
2418 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
2422 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
2424 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
2426 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
2428 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
2430 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
2435 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2438 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
2442 if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
2444 } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
2449 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2452 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2454 struct i2c_client *client = sd->priv;
2455 struct sensor *sensor = to_sensor(client);
2456 struct v4l2_pix_format *pix = &f->fmt.pix;
2457 const struct v4l2_queryctrl *qctrl;
2458 struct soc_camera_device *icd = client->dev.platform_data;
2459 struct reginfo *winseqe_set_addr=NULL;
2460 int ret = 0, set_w,set_h;
2462 if (sensor->info_priv.pixfmt != pix->pixelformat) {
2463 switch (pix->pixelformat)
2465 case V4L2_PIX_FMT_YUYV:
2467 winseqe_set_addr = sensor_ClrFmt_YUYV;
2470 case V4L2_PIX_FMT_UYVY:
2472 winseqe_set_addr = sensor_ClrFmt_UYVY;
2478 if (winseqe_set_addr != NULL) {
2479 sensor_write_array(client, winseqe_set_addr);
2480 sensor->info_priv.pixfmt = pix->pixelformat;
2482 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2484 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2489 set_h = pix->height;
2491 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2493 winseqe_set_addr = sensor_qcif;
2497 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2499 winseqe_set_addr = sensor_qvga;
2503 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2505 winseqe_set_addr = sensor_cif;
2509 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2511 winseqe_set_addr = sensor_vga;
2515 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2517 winseqe_set_addr = sensor_svga;
2521 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2523 winseqe_set_addr = sensor_xga;
2527 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2529 winseqe_set_addr = sensor_720p;
2533 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2535 winseqe_set_addr = sensor_sxga;
2539 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2541 winseqe_set_addr = sensor_uxga;
2545 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2547 winseqe_set_addr = sensor_1080p;
2551 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2553 winseqe_set_addr = sensor_qxga;
2557 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2559 winseqe_set_addr = sensor_qsxga;
2565 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2566 set_w = SENSOR_INIT_WIDTH;
2567 set_h = SENSOR_INIT_HEIGHT;
2568 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
2571 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2573 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
2574 sensor_parameter_record(client);
2575 /*#if CONFIG_SENSOR_Focus
2576 sensor_af_idlechk(client);
2577 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2578 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2580 #if CONFIG_SENSOR_Flash
2581 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2582 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2583 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2586 }else { /* ddl@rock-chips.com : Video */
2587 #if CONFIG_SENSOR_Flash
2588 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2589 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2590 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2594 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2595 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2596 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2597 ret |= sensor_write_array(client,sensor_init_data);
2598 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2601 ret |= sensor_write_array(client, winseqe_set_addr);
2603 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2604 #if CONFIG_SENSOR_Flash
2605 if (sensor_fmt_capturechk(sd,f) == true) {
2606 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2607 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2608 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2612 goto sensor_s_fmt_end;
2614 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2616 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
2617 sensor_ae_transfer(client);
2618 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2619 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2620 if (sensor->info_priv.whiteBalance != 0) {
2621 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2622 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2624 sensor->info_priv.snap2preview = true;
2625 } else if (sensor_fmt_videochk(sd,f) == true) { /* ddl@rock-chips.com : Video */
2626 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2627 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2629 sensor->info_priv.video2preview = true;
2630 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2631 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2632 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2633 if (sensor->info_priv.snap2preview == true) {
2634 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2635 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2637 #if CONFIG_SENSOR_Focus
2638 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
2639 sensor_af_idlechk(client);
2647 sensor->info_priv.video2preview = false;
2648 sensor->info_priv.snap2preview = false;
2650 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2654 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2657 pix->height = set_h;
2662 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2664 struct v4l2_pix_format *pix = &f->fmt.pix;
2665 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
2666 pix->pixelformat == V4L2_PIX_FMT_YUYV;
2669 * With Bayer format enforce even side lengths, but let the user play
2670 * with the starting pixel
2673 if (pix->height > SENSOR_MAX_HEIGHT)
2674 pix->height = SENSOR_MAX_HEIGHT;
2675 else if (pix->height < SENSOR_MIN_HEIGHT)
2676 pix->height = SENSOR_MIN_HEIGHT;
2678 pix->height = ALIGN(pix->height, 2);
2680 if (pix->width > SENSOR_MAX_WIDTH)
2681 pix->width = SENSOR_MAX_WIDTH;
2682 else if (pix->width < SENSOR_MIN_WIDTH)
2683 pix->width = SENSOR_MIN_WIDTH;
2685 pix->width = ALIGN(pix->width, 2);
2690 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2692 struct i2c_client *client = sd->priv;
2694 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2697 if (id->match.addr != client->addr)
2700 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2705 #if CONFIG_SENSOR_Brightness
2706 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2708 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2710 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2712 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2714 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2716 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2719 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2723 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2727 #if CONFIG_SENSOR_Effect
2728 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2730 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2732 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2734 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2736 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2738 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2741 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2745 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2749 #if CONFIG_SENSOR_Exposure
2750 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2752 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2754 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2756 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2758 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2760 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2763 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2767 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2771 #if CONFIG_SENSOR_Saturation
2772 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2774 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2776 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2778 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2780 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2782 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2785 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2789 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2793 #if CONFIG_SENSOR_Contrast
2794 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2796 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2798 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2800 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2802 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2804 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2807 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2811 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2815 #if CONFIG_SENSOR_Mirror
2816 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2818 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2820 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2822 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2824 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2826 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2829 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2833 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2837 #if CONFIG_SENSOR_Flip
2838 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2840 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2842 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2844 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2846 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2848 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2851 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2855 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2859 #if CONFIG_SENSOR_Scene
2860 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2862 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2864 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2866 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2868 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2870 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2873 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2877 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2881 #if CONFIG_SENSOR_WhiteBalance
2882 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2884 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2886 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2888 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2890 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2892 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2895 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2899 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2903 #if CONFIG_SENSOR_DigitalZoom
2904 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2906 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2907 struct sensor *sensor = to_sensor(client);
2908 const struct v4l2_queryctrl *qctrl_info;
2909 int digitalzoom_cur, digitalzoom_total;
2911 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2915 digitalzoom_cur = sensor->info_priv.digitalzoom;
2916 digitalzoom_total = qctrl_info->maximum;
2918 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2920 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2924 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2926 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2930 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2932 *value = digitalzoom_total - digitalzoom_cur;
2935 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2937 *value = 0 - digitalzoom_cur;
2940 digitalzoom_cur += *value;
2942 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2944 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2946 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2949 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2956 #if CONFIG_SENSOR_Focus
2957 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2959 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2960 struct sensor *sensor = to_sensor(client);
2961 const struct v4l2_queryctrl *qctrl_info;
2964 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2968 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2969 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2970 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2973 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2977 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2978 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2981 sensor_set_focus_absolute_end:
2984 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2986 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2987 struct sensor *sensor = to_sensor(client);
2988 const struct v4l2_queryctrl *qctrl_info;
2991 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
2995 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2996 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2997 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3000 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3004 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3005 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3007 sensor_set_focus_relative_end:
3011 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3013 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3014 struct sensor *sensor = to_sensor(client);
3017 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3020 case SENSOR_AF_MODE_AUTO:
3022 ret = sensor_af_single(client);
3026 /*case SENSOR_AF_MODE_MACRO:
3028 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3032 case SENSOR_AF_MODE_INFINITY:
3034 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3038 case SENSOR_AF_MODE_CONTINUOUS:
3040 ret = sensor_af_const(client);
3044 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3049 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3052 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3053 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3059 #if CONFIG_SENSOR_Flash
3060 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3062 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3063 if (value == 3) { /* ddl@rock-chips.com: torch */
3064 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3066 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3068 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3072 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3076 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3078 struct i2c_client *client = sd->priv;
3079 struct sensor *sensor = to_sensor(client);
3080 const struct v4l2_queryctrl *qctrl;
3082 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3086 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3092 case V4L2_CID_BRIGHTNESS:
3094 ctrl->value = sensor->info_priv.brightness;
3097 case V4L2_CID_SATURATION:
3099 ctrl->value = sensor->info_priv.saturation;
3102 case V4L2_CID_CONTRAST:
3104 ctrl->value = sensor->info_priv.contrast;
3107 case V4L2_CID_DO_WHITE_BALANCE:
3109 ctrl->value = sensor->info_priv.whiteBalance;
3112 case V4L2_CID_EXPOSURE:
3114 ctrl->value = sensor->info_priv.exposure;
3117 case V4L2_CID_HFLIP:
3119 ctrl->value = sensor->info_priv.mirror;
3122 case V4L2_CID_VFLIP:
3124 ctrl->value = sensor->info_priv.flip;
3135 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3137 struct i2c_client *client = sd->priv;
3138 struct sensor *sensor = to_sensor(client);
3139 struct soc_camera_device *icd = client->dev.platform_data;
3140 const struct v4l2_queryctrl *qctrl;
3143 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3147 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3153 #if CONFIG_SENSOR_Brightness
3154 case V4L2_CID_BRIGHTNESS:
3156 if (ctrl->value != sensor->info_priv.brightness)
3158 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3162 sensor->info_priv.brightness = ctrl->value;
3167 #if CONFIG_SENSOR_Exposure
3168 case V4L2_CID_EXPOSURE:
3170 if (ctrl->value != sensor->info_priv.exposure)
3172 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3176 sensor->info_priv.exposure = ctrl->value;
3181 #if CONFIG_SENSOR_Saturation
3182 case V4L2_CID_SATURATION:
3184 if (ctrl->value != sensor->info_priv.saturation)
3186 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3190 sensor->info_priv.saturation = ctrl->value;
3195 #if CONFIG_SENSOR_Contrast
3196 case V4L2_CID_CONTRAST:
3198 if (ctrl->value != sensor->info_priv.contrast)
3200 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3204 sensor->info_priv.contrast = ctrl->value;
3209 #if CONFIG_SENSOR_WhiteBalance
3210 case V4L2_CID_DO_WHITE_BALANCE:
3212 if (ctrl->value != sensor->info_priv.whiteBalance)
3214 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3218 sensor->info_priv.whiteBalance = ctrl->value;
3223 #if CONFIG_SENSOR_Mirror
3224 case V4L2_CID_HFLIP:
3226 if (ctrl->value != sensor->info_priv.mirror)
3228 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3230 sensor->info_priv.mirror = ctrl->value;
3235 #if CONFIG_SENSOR_Flip
3236 case V4L2_CID_VFLIP:
3238 if (ctrl->value != sensor->info_priv.flip)
3240 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3242 sensor->info_priv.flip = ctrl->value;
3253 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3255 const struct v4l2_queryctrl *qctrl;
3256 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3257 struct sensor *sensor = to_sensor(client);
3259 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3263 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3267 switch (ext_ctrl->id)
3269 case V4L2_CID_SCENE:
3271 ext_ctrl->value = sensor->info_priv.scene;
3274 case V4L2_CID_EFFECT:
3276 ext_ctrl->value = sensor->info_priv.effect;
3279 case V4L2_CID_ZOOM_ABSOLUTE:
3281 ext_ctrl->value = sensor->info_priv.digitalzoom;
3284 case V4L2_CID_ZOOM_RELATIVE:
3288 case V4L2_CID_FOCUS_ABSOLUTE:
3292 case V4L2_CID_FOCUS_RELATIVE:
3296 case V4L2_CID_FLASH:
3298 ext_ctrl->value = sensor->info_priv.flash;
3306 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3308 const struct v4l2_queryctrl *qctrl;
3309 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3310 struct sensor *sensor = to_sensor(client);
3313 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3317 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3322 switch (ext_ctrl->id)
3324 #if CONFIG_SENSOR_Scene
3325 case V4L2_CID_SCENE:
3327 if (ext_ctrl->value != sensor->info_priv.scene)
3329 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3331 sensor->info_priv.scene = ext_ctrl->value;
3336 #if CONFIG_SENSOR_Effect
3337 case V4L2_CID_EFFECT:
3339 if (ext_ctrl->value != sensor->info_priv.effect)
3341 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3343 sensor->info_priv.effect= ext_ctrl->value;
3348 #if CONFIG_SENSOR_DigitalZoom
3349 case V4L2_CID_ZOOM_ABSOLUTE:
3351 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3354 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3356 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3358 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3360 sensor->info_priv.digitalzoom += val_offset;
3362 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3367 case V4L2_CID_ZOOM_RELATIVE:
3369 if (ext_ctrl->value)
3371 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3373 sensor->info_priv.digitalzoom += ext_ctrl->value;
3375 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3380 #if CONFIG_SENSOR_Focus
3381 case V4L2_CID_FOCUS_ABSOLUTE:
3383 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3388 case V4L2_CID_FOCUS_RELATIVE:
3390 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3393 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3396 case V4L2_CID_FOCUS_AUTO:
3398 if (ext_ctrl->value == 1) {
3399 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
3401 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3402 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3403 if (ext_ctrl->value == 0)
3404 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3408 case V4L2_CID_FOCUS_CONTINUOUS:
3410 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3411 if (ext_ctrl->value == 1) {
3412 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3414 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3417 if (ext_ctrl->value == 0)
3418 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3423 #if CONFIG_SENSOR_Flash
3424 case V4L2_CID_FLASH:
3426 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3428 sensor->info_priv.flash = ext_ctrl->value;
3430 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3441 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3443 struct i2c_client *client = sd->priv;
3444 struct soc_camera_device *icd = client->dev.platform_data;
3445 int i, error_cnt=0, error_idx=-1;
3448 for (i=0; i<ext_ctrl->count; i++) {
3449 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3456 error_idx = ext_ctrl->count;
3458 if (error_idx != -1) {
3459 ext_ctrl->error_idx = error_idx;
3466 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3468 struct i2c_client *client = sd->priv;
3469 struct soc_camera_device *icd = client->dev.platform_data;
3470 int i, error_cnt=0, error_idx=-1;
3472 for (i=0; i<ext_ctrl->count; i++) {
3473 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3480 error_idx = ext_ctrl->count;
3482 if (error_idx != -1) {
3483 ext_ctrl->error_idx = error_idx;
3490 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3492 struct i2c_client *client = sd->priv;
3493 struct sensor *sensor = to_sensor(client);
3494 #if CONFIG_SENSOR_Focus
3495 struct soc_camera_device *icd = client->dev.platform_data;
3496 struct v4l2_format fmt;
3500 sensor->info_priv.enable = 1;
3501 #if CONFIG_SENSOR_Focus
3502 fmt.fmt.pix.width = icd->user_width;
3503 fmt.fmt.pix.height = icd->user_height;
3504 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3505 if (sensor_fmt_capturechk(sd, &fmt) == false) {
3506 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3507 if (sensor->sensor_wq != NULL) {
3508 mutex_lock(&sensor->wq_lock);
3509 if (sensor->sensor_wk.state == sensor_working) {
3510 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
3511 mutex_unlock(&sensor->wq_lock);
3512 goto sensor_s_stream_end;
3514 sensor->sensor_wk.state = sensor_working;
3515 mutex_unlock(&sensor->wq_lock);
3516 sensor->sensor_wk.client = client;
3517 INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
3518 queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
3520 sensor->info_priv.affm_reinit = 0;
3524 } else if (enable == 0) {
3525 sensor->info_priv.enable = 0;
3526 #if CONFIG_SENSOR_Focus
3527 flush_work(&(sensor->sensor_wk.dwork.work));
3528 mutex_lock(&sensor->wq_lock);
3529 sensor->sensor_wk.state = sensor_work_ready;
3530 mutex_unlock(&sensor->wq_lock);
3534 sensor_s_stream_end:
3538 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3539 * this wasn't our capture interface, so, we wait for the right one */
3540 static int sensor_video_probe(struct soc_camera_device *icd,
3541 struct i2c_client *client)
3545 struct sensor *sensor = to_sensor(client);
3547 /* We must have a parent by now. And it cannot be a wrong one.
3548 * So this entire test is completely redundant. */
3549 if (!icd->dev.parent ||
3550 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3553 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3555 goto sensor_video_probe_err;
3558 ret = sensor_write(client, 0x3008, 0x80);
3560 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3562 goto sensor_video_probe_err;
3564 mdelay(5); //delay 5 microseconds
3566 /* check if it is an sensor sensor */
3567 ret = sensor_read(client, 0x300a, &value);
3569 SENSOR_TR("read chip id high byte failed\n");
3571 goto sensor_video_probe_err;
3574 pid |= (value << 8);
3576 ret = sensor_read(client, 0x300b, &value);
3578 SENSOR_TR("read chip id low byte failed\n");
3580 goto sensor_video_probe_err;
3583 pid |= (value & 0xff);
3584 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3585 if (pid == SENSOR_ID) {
3586 sensor->model = SENSOR_V4L2_IDENT;
3588 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3590 goto sensor_video_probe_err;
3593 icd->formats = sensor_colour_formats;
3594 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
3598 sensor_video_probe_err:
3601 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3603 struct i2c_client *client = sd->priv;
3604 struct soc_camera_device *icd = client->dev.platform_data;
3605 struct sensor *sensor = to_sensor(client);
3608 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3611 case RK29_CAM_SUBDEV_DEACTIVATE:
3613 sensor_deactivate(client);
3616 case RK29_CAM_SUBDEV_IOREQUEST:
3618 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3619 if (sensor->sensor_io_request != NULL) {
3620 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
3621 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
3622 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
3623 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
3624 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
3625 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
3628 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3630 goto sensor_ioctl_end;
3632 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3634 #if CONFIG_SENSOR_Flash
3636 if (sensor->sensor_gpio_res) {
3637 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3638 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3639 for (i = 0; i < icd->ops->num_controls; i++) {
3640 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3641 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3644 sensor->info_priv.flash = 0xff;
3645 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3653 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3663 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3664 .init = sensor_init,
3665 .g_ctrl = sensor_g_control,
3666 .s_ctrl = sensor_s_control,
3667 .g_ext_ctrls = sensor_g_ext_controls,
3668 .s_ext_ctrls = sensor_s_ext_controls,
3669 .g_chip_ident = sensor_g_chip_ident,
3670 .ioctl = sensor_ioctl,
3673 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3674 .s_fmt = sensor_s_fmt,
3675 .g_fmt = sensor_g_fmt,
3676 .try_fmt = sensor_try_fmt,
3677 .s_stream = sensor_s_stream,
3680 static struct v4l2_subdev_ops sensor_subdev_ops = {
3681 .core = &sensor_subdev_core_ops,
3682 .video = &sensor_subdev_video_ops,
3685 static int sensor_probe(struct i2c_client *client,
3686 const struct i2c_device_id *did)
3688 struct sensor *sensor;
3689 struct soc_camera_device *icd = client->dev.platform_data;
3690 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3691 struct soc_camera_link *icl;
3694 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3696 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3700 icl = to_soc_camera_link(icd);
3702 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3706 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3707 dev_warn(&adapter->dev,
3708 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3712 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3716 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3718 /* Second stage probe - when a capture adapter is there */
3719 icd->ops = &sensor_ops;
3720 icd->y_skip_top = 0;
3721 #if CONFIG_SENSOR_I2C_NOSCHED
3722 atomic_set(&sensor->tasklock_cnt,0);
3725 ret = sensor_video_probe(icd, client);
3728 i2c_set_clientdata(client, NULL);
3732 #if CONFIG_SENSOR_Focus
3733 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
3734 if (sensor->sensor_wq == NULL)
3735 SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
3736 mutex_init(&sensor->wq_lock);
3737 sensor->sensor_wk.state = sensor_work_ready;
3741 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3745 static int sensor_remove(struct i2c_client *client)
3747 struct sensor *sensor = to_sensor(client);
3748 struct soc_camera_device *icd = client->dev.platform_data;
3750 #if CONFIG_SENSOR_Focus
3751 if (sensor->sensor_wq) {
3752 destroy_workqueue(sensor->sensor_wq);
3753 sensor->sensor_wq = NULL;
3758 i2c_set_clientdata(client, NULL);
3759 client->driver = NULL;
3765 static const struct i2c_device_id sensor_id[] = {
3766 {SENSOR_NAME_STRING(), 0 },
3769 MODULE_DEVICE_TABLE(i2c, sensor_id);
3771 static struct i2c_driver sensor_i2c_driver = {
3773 .name = SENSOR_NAME_STRING(),
3775 .probe = sensor_probe,
3776 .remove = sensor_remove,
3777 .id_table = sensor_id,
3780 static int __init sensor_mod_init(void)
3782 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3783 return i2c_add_driver(&sensor_i2c_driver);
3786 static void __exit sensor_mod_exit(void)
3788 i2c_del_driver(&sensor_i2c_driver);
3791 device_initcall_sync(sensor_mod_init);
3792 module_exit(sensor_mod_exit);
3794 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3795 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3796 MODULE_LICENSE("GPL");