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_MBUS_FMT_YUYV8_2X8
58 #define CONFIG_SENSOR_WhiteBalance 1
59 #define CONFIG_SENSOR_Brightness 0
60 #define CONFIG_SENSOR_Contrast 0
61 #define CONFIG_SENSOR_Saturation 0
62 #define CONFIG_SENSOR_Effect 1
63 #define CONFIG_SENSOR_Scene 1
64 #define CONFIG_SENSOR_DigitalZoom 0
65 #define CONFIG_SENSOR_Exposure 0
66 #define CONFIG_SENSOR_Flash 0
67 #define CONFIG_SENSOR_Mirror 0
68 #define CONFIG_SENSOR_Flip 0
69 #ifdef CONFIG_OV5640_AUTOFOCUS
70 #define CONFIG_SENSOR_Focus 1
71 #include "ov5640_af_firmware.c"
73 #define CONFIG_SENSOR_Focus 0
76 #define CONFIG_SENSOR_I2C_SPEED 250000 /* Hz */
77 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
78 #define CONFIG_SENSOR_I2C_NOSCHED 0
79 #define CONFIG_SENSOR_I2C_RDWRCHK 0
81 #define CONFIG_SENSOR_WRITE_REGS 1
82 #define WRITE_REGS_NUM 100
84 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
85 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
86 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
88 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
89 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
90 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
91 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
92 #define COLOR_TEMPERATURE_OFFICE_DN 3500
93 #define COLOR_TEMPERATURE_OFFICE_UP 5000
94 #define COLOR_TEMPERATURE_HOME_DN 2500
95 #define COLOR_TEMPERATURE_HOME_UP 3500
97 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
98 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
100 #define SENSOR_AF_IS_ERR (0x00<<0)
101 #define SENSOR_AF_IS_OK (0x01<<0)
102 #define SENSOR_INIT_IS_ERR (0x00<<28)
103 #define SENSOR_INIT_IS_OK (0x01<<28)
105 #if CONFIG_SENSOR_Focus
106 /*#define SENSOR_AF_MODE_INFINITY 0
107 #define SENSOR_AF_MODE_MACRO 1
108 #define SENSOR_AF_MODE_FIXED 2
109 #define SENSOR_AF_MODE_AUTO 3
110 #define SENSOR_AF_MODE_CONTINUOUS 4
111 #define SENSOR_AF_MODE_CLOSE 5*/
112 #define SENSOR_AF_MODE_AUTO 0
113 #define SENSOR_AF_MODE_CLOSE 1
116 #if CONFIG_SENSOR_Focus
117 /* ov5640 VCM Command and Status Registers */
118 #define CMD_MAIN_Reg 0x3022
119 //#define CMD_TAG_Reg 0x3023
120 #define CMD_ACK_Reg 0x3023
121 #define CMD_PARA0_Reg 0x3024
122 #define CMD_PARA1_Reg 0x3025
123 #define CMD_PARA2_Reg 0x3026
124 #define CMD_PARA3_Reg 0x3027
125 #define CMD_PARA4_Reg 0x3028
127 //#define STA_ZONE_Reg 0x3026
128 #define STA_FOCUS_Reg 0x3029
130 /* ov5640 VCM Command */
131 #define OverlayEn_Cmd 0x01
132 #define OverlayDis_Cmd 0x02
133 #define SingleFocus_Cmd 0x03
134 #define ConstFocus_Cmd 0x04
135 #define StepMode_Cmd 0x05
136 #define PauseFocus_Cmd 0x06
137 #define ReturnIdle_Cmd 0x08
138 #define SetZone_Cmd 0x10
139 #define UpdateZone_Cmd 0x12
140 #define SetMotor_Cmd 0x20
143 #define Zone_configuration_Cmd 0x12
144 #define Trig_autofocus_Cmd 0x03
145 #define Get_focus_result_Cmd 0x07
147 /* ov5640 Focus State */
148 //#define S_FIRWRE 0xFF /*Firmware is downloaded and not run*/
149 #define S_STARTUP 0x7e /*Firmware is initializing*/
151 #define S_IDLE 0x70 /*Idle state, focus is released; lens is located at the furthest position.*/
152 #define S_FOCUSING 0x00 /*Auto Focus is running.*/
153 #define S_FOCUSED 0x10 /*Auto Focus is completed.*/
155 #define S_CAPTURE 0x12
158 /* ov5640 Zone State */
159 #define Zone_Is_Focused(a, zone_val) (zone_val&(1<<(a-3)))
160 #define Zone_Get_ID(zone_val) (zone_val&0x03)
162 #define Zone_CenterMode 0x01
163 #define Zone_5xMode 0x02
164 #define Zone_5PlusMode 0x03
165 #define Zone_4fMode 0x04
167 #define ZoneSel_Auto 0x0b
168 #define ZoneSel_SemiAuto 0x0c
169 #define ZoneSel_Manual 0x0d
170 #define ZoneSel_Rotate 0x0e
172 /* ov5640 Step Focus Commands */
173 #define StepFocus_Near_Tag 0x01
174 #define StepFocus_Far_Tag 0x02
175 #define StepFocus_Furthest_Tag 0x03
176 #define StepFocus_Nearest_Tag 0x04
177 #define StepFocus_Spec_Tag 0x10
180 /* init 800X600 SVGA */
181 static struct reginfo sensor_init_data[] =
441 /* 720p 15fps @ 1280x720 */
443 static struct reginfo sensor_720p[]=
484 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
486 static struct reginfo sensor_1080p[]=
492 /* 2592X1944 QSXGA */
493 static struct reginfo sensor_qsxga[] =
534 static struct reginfo sensor_qxga[] =
576 static struct reginfo sensor_uxga[] =
618 static struct reginfo sensor_sxga[] =
659 static struct reginfo sensor_xga[] =
699 static struct reginfo sensor_svga[] =
745 static struct reginfo sensor_vga[] =
751 static struct reginfo sensor_cif[] =
758 static struct reginfo sensor_qvga[] =
765 static struct reginfo sensor_qcif[] =
771 static struct reginfo sensor_ClrFmt_YUYV[]=
777 static struct reginfo sensor_ClrFmt_UYVY[]=
784 #if CONFIG_SENSOR_WhiteBalance
785 static struct reginfo sensor_WhiteB_Auto[]=
812 /* Cloudy Colour Temperature : 6500K - 8000K */
813 static struct reginfo sensor_WhiteB_Cloudy[]=
824 /* ClearDay Colour Temperature : 5000K - 6500K */
825 static struct reginfo sensor_WhiteB_ClearDay[]=
836 /* Office Colour Temperature : 3500K - 5000K */
837 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
848 /* Home Colour Temperature : 2500K - 3500K */
849 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
860 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
861 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
865 #if CONFIG_SENSOR_Brightness
866 static struct reginfo sensor_Brightness0[]=
870 static struct reginfo sensor_Brightness1[]=
875 static struct reginfo sensor_Brightness2[]=
879 static struct reginfo sensor_Brightness3[]=
883 static struct reginfo sensor_Brightness4[]=
888 static struct reginfo sensor_Brightness5[]=
892 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
893 sensor_Brightness4, sensor_Brightness5,NULL,
898 #if CONFIG_SENSOR_Effect
899 static struct reginfo sensor_Effect_Normal[] =
905 static struct reginfo sensor_Effect_WandB[] =
913 static struct reginfo sensor_Effect_Sepia[] =
922 static struct reginfo sensor_Effect_Negative[] =
928 };static struct reginfo sensor_Effect_Bluish[] =
938 static struct reginfo sensor_Effect_Green[] =
947 /*static struct reginfo sensor_Effect_Reddish[] =
957 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
958 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
961 #if CONFIG_SENSOR_Exposure
962 static struct reginfo sensor_Exposure0[]=
966 static struct reginfo sensor_Exposure1[]=
970 static struct reginfo sensor_Exposure2[]=
975 static struct reginfo sensor_Exposure3[]=
979 static struct reginfo sensor_Exposure4[]=
983 static struct reginfo sensor_Exposure5[]=
987 static struct reginfo sensor_Exposure6[]=
991 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
992 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
995 #if CONFIG_SENSOR_Saturation
996 static struct reginfo sensor_Saturation0[]=
1001 static struct reginfo sensor_Saturation1[]=
1003 {SEQUENCE_END, 0x00}
1006 static struct reginfo sensor_Saturation2[]=
1008 {SEQUENCE_END, 0x00}
1009 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1012 #if CONFIG_SENSOR_Contrast
1013 static struct reginfo sensor_Contrast0[]=
1015 {SEQUENCE_END, 0x00}
1018 static struct reginfo sensor_Contrast1[]=
1020 {SEQUENCE_END, 0x00}
1022 static struct reginfo sensor_Contrast2[]=
1024 {SEQUENCE_END, 0x00}
1027 static struct reginfo sensor_Contrast3[]=
1029 {SEQUENCE_END, 0x00}
1032 static struct reginfo sensor_Contrast4[]=
1034 {SEQUENCE_END, 0x00}
1038 static struct reginfo sensor_Contrast5[]=
1040 {SEQUENCE_END, 0x00}
1043 static struct reginfo sensor_Contrast6[]=
1045 {SEQUENCE_END, 0x00}
1047 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1048 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1052 #if CONFIG_SENSOR_Mirror
1053 static struct reginfo sensor_MirrorOn[]=
1055 {SEQUENCE_END, 0x00}
1057 static struct reginfo sensor_MirrorOff[]=
1059 {SEQUENCE_END, 0x00}
1061 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1063 #if CONFIG_SENSOR_Flip
1064 static struct reginfo sensor_FlipOn[]=
1066 {SEQUENCE_END, 0x00}
1069 static struct reginfo sensor_FlipOff[]=
1071 {SEQUENCE_END, 0x00}
1073 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1076 #if CONFIG_SENSOR_Scene
1077 static struct reginfo sensor_SceneAuto[] =
1080 {SEQUENCE_END, 0x00}
1082 static struct reginfo sensor_SceneNight[] =
1084 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1102 {SEQUENCE_END, 0x00}
1104 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1107 #if CONFIG_SENSOR_DigitalZoom
1108 static struct reginfo sensor_Zoom0[] =
1110 {SEQUENCE_END, 0x00}
1112 static struct reginfo sensor_Zoom1[] =
1114 {SEQUENCE_END, 0x00}
1117 static struct reginfo sensor_Zoom2[] =
1119 {SEQUENCE_END, 0x00}
1123 static struct reginfo sensor_Zoom3[] =
1125 {SEQUENCE_END, 0x00}
1127 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1129 static const struct v4l2_querymenu sensor_menus[] =
1131 #if CONFIG_SENSOR_WhiteBalance
1132 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1133 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1134 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1137 #if CONFIG_SENSOR_Effect
1138 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1139 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1140 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1143 #if CONFIG_SENSOR_Scene
1144 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1147 #if CONFIG_SENSOR_Flash
1148 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1149 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1153 static const struct v4l2_queryctrl sensor_controls[] =
1155 #if CONFIG_SENSOR_WhiteBalance
1157 .id = V4L2_CID_DO_WHITE_BALANCE,
1158 .type = V4L2_CTRL_TYPE_MENU,
1159 .name = "White Balance Control",
1167 #if CONFIG_SENSOR_Brightness
1169 .id = V4L2_CID_BRIGHTNESS,
1170 .type = V4L2_CTRL_TYPE_INTEGER,
1171 .name = "Brightness Control",
1179 #if CONFIG_SENSOR_Effect
1181 .id = V4L2_CID_EFFECT,
1182 .type = V4L2_CTRL_TYPE_MENU,
1183 .name = "Effect Control",
1191 #if CONFIG_SENSOR_Exposure
1193 .id = V4L2_CID_EXPOSURE,
1194 .type = V4L2_CTRL_TYPE_INTEGER,
1195 .name = "Exposure Control",
1203 #if CONFIG_SENSOR_Saturation
1205 .id = V4L2_CID_SATURATION,
1206 .type = V4L2_CTRL_TYPE_INTEGER,
1207 .name = "Saturation Control",
1215 #if CONFIG_SENSOR_Contrast
1217 .id = V4L2_CID_CONTRAST,
1218 .type = V4L2_CTRL_TYPE_INTEGER,
1219 .name = "Contrast Control",
1227 #if CONFIG_SENSOR_Mirror
1229 .id = V4L2_CID_HFLIP,
1230 .type = V4L2_CTRL_TYPE_BOOLEAN,
1231 .name = "Mirror Control",
1239 #if CONFIG_SENSOR_Flip
1241 .id = V4L2_CID_VFLIP,
1242 .type = V4L2_CTRL_TYPE_BOOLEAN,
1243 .name = "Flip Control",
1251 #if CONFIG_SENSOR_Scene
1253 .id = V4L2_CID_SCENE,
1254 .type = V4L2_CTRL_TYPE_MENU,
1255 .name = "Scene Control",
1263 #if CONFIG_SENSOR_DigitalZoom
1265 .id = V4L2_CID_ZOOM_RELATIVE,
1266 .type = V4L2_CTRL_TYPE_INTEGER,
1267 .name = "DigitalZoom Control",
1273 .id = V4L2_CID_ZOOM_ABSOLUTE,
1274 .type = V4L2_CTRL_TYPE_INTEGER,
1275 .name = "DigitalZoom Control",
1283 #if CONFIG_SENSOR_Focus
1285 .id = V4L2_CID_FOCUS_RELATIVE,
1286 .type = V4L2_CTRL_TYPE_INTEGER,
1287 .name = "Focus Control",
1293 .id = V4L2_CID_FOCUS_ABSOLUTE,
1294 .type = V4L2_CTRL_TYPE_INTEGER,
1295 .name = "Focus Control",
1299 .default_value = 125,
1302 .id = V4L2_CID_FOCUS_AUTO,
1303 .type = V4L2_CTRL_TYPE_BOOLEAN,
1304 .name = "Focus Control",
1310 .id = V4L2_CID_FOCUS_CONTINUOUS,
1311 .type = V4L2_CTRL_TYPE_BOOLEAN,
1312 .name = "Focus Control",
1320 #if CONFIG_SENSOR_Flash
1322 .id = V4L2_CID_FLASH,
1323 .type = V4L2_CTRL_TYPE_MENU,
1324 .name = "Flash Control",
1333 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1334 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1335 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1336 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1337 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1338 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1339 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1340 static int sensor_resume(struct soc_camera_device *icd);
1341 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1342 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1343 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1344 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1345 static int sensor_deactivate(struct i2c_client *client);
1347 static struct soc_camera_ops sensor_ops =
1349 .suspend = sensor_suspend,
1350 .resume = sensor_resume,
1351 .set_bus_param = sensor_set_bus_param,
1352 .query_bus_param = sensor_query_bus_param,
1353 .controls = sensor_controls,
1354 .menus = sensor_menus,
1355 .num_controls = ARRAY_SIZE(sensor_controls),
1356 .num_menus = ARRAY_SIZE(sensor_menus),
1358 /* only one fixed colorspace per pixelcode */
1359 struct sensor_datafmt {
1360 enum v4l2_mbus_pixelcode code;
1361 enum v4l2_colorspace colorspace;
1364 /* Find a data format by a pixel code in an array */
1365 static const struct sensor_datafmt *sensor_find_datafmt(
1366 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1370 for (i = 0; i < n; i++)
1371 if (fmt[i].code == code)
1377 static const struct sensor_datafmt sensor_colour_fmts[] = {
1378 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1379 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1381 enum sensor_work_state
1383 sensor_work_ready = 0,
1388 struct i2c_client *client;
1389 struct delayed_work dwork;
1390 enum sensor_work_state state;
1393 typedef struct sensor_info_priv_s
1409 unsigned char mirror; /* HFLIP */
1410 unsigned char flip; /* VFLIP */
1411 struct reginfo *winseqe_cur_addr;
1412 struct sensor_datafmt fmt;
1413 unsigned int enable;
1414 unsigned int funmodule_state;
1415 } sensor_info_priv_t;
1419 struct sensor_parameter
1421 unsigned short int preview_maxlines;
1422 unsigned short int preview_exposure;
1423 unsigned short int preview_line_width;
1424 unsigned short int preview_gain;
1426 unsigned short int capture_framerate;
1427 unsigned short int preview_framerate;
1433 struct v4l2_subdev subdev;
1434 struct i2c_client *client;
1435 sensor_info_priv_t info_priv;
1436 struct sensor_parameter parameter;
1437 struct workqueue_struct *sensor_wq;
1438 struct sensor_work sensor_wk;
1439 struct mutex wq_lock;
1440 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1441 #if CONFIG_SENSOR_I2C_NOSCHED
1442 atomic_t tasklock_cnt;
1444 struct rk29camera_platform_data *sensor_io_request;
1445 struct rk29camera_gpio_res *sensor_gpio_res;
1448 static struct sensor* to_sensor(const struct i2c_client *client)
1450 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1453 static int sensor_task_lock(struct i2c_client *client, int lock)
1455 #if CONFIG_SENSOR_I2C_NOSCHED
1457 struct sensor *sensor = to_sensor(client);
1460 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1461 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1462 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1466 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1467 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1468 goto sensor_task_lock_err;
1473 atomic_add(1, &sensor->tasklock_cnt);
1475 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1476 atomic_sub(1, &sensor->tasklock_cnt);
1478 if (atomic_read(&sensor->tasklock_cnt) == 0)
1483 sensor_task_lock_err:
1492 #if CONFIG_SENSOR_WRITE_REGS
1493 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
1496 struct i2c_msg msg[1];
1499 msg->addr = client->addr;
1500 msg->flags = client->flags;
1501 msg->buf = reg_info;
1502 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1503 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1509 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1510 err = i2c_transfer(client->adapter, msg, 1);
1514 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
1528 /* sensor register write */
1529 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1533 struct i2c_msg msg[1];
1536 buf[1] = reg & 0xFF;
1539 msg->addr = client->addr;
1540 msg->flags = client->flags;
1542 msg->len = sizeof(buf);
1543 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1544 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1549 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1550 err = i2c_transfer(client->adapter, msg, 1);
1555 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1563 /* sensor register read */
1564 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1568 struct i2c_msg msg[2];
1571 buf[1] = reg & 0xFF;
1573 msg[0].addr = client->addr;
1574 msg[0].flags = client->flags;
1576 msg[0].len = sizeof(buf);
1577 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1578 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1580 msg[1].addr = client->addr;
1581 msg[1].flags = client->flags|I2C_M_RD;
1584 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1585 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1589 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1590 err = i2c_transfer(client->adapter, msg, 2);
1596 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1604 /* write a array of registers */
1605 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1609 #if CONFIG_SENSOR_Focus
1610 struct sensor *sensor = to_sensor(client);
1612 #if CONFIG_SENSOR_I2C_RDWRCHK
1615 #if CONFIG_SENSOR_WRITE_REGS
1622 if (sensor_task_lock(client, 1) < 0)
1623 goto sensor_write_array_end;
1624 while (regarray[i].reg != SEQUENCE_END) {
1625 #if CONFIG_SENSOR_Focus
1626 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1627 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1629 goto sensor_write_array_end;
1633 #if CONFIG_SENSOR_WRITE_REGS
1637 reg_length = 0x0001;
1639 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
1642 if(reg_num >= WRITE_REGS_NUM)
1649 size_num = reg_num + 1;
1651 ptemp = phead = (u8*)kmalloc(size_num*sizeof(u8),GFP_KERNEL);
1653 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1655 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1657 *phead = regarray[j].reg >> 8;
1658 *(ptemp+1) = regarray[j].reg & 0xFF;
1660 for( ; reg_num > 0; reg_num --, j++) {
1661 *ptemp ++ = regarray[j].val;
1665 err = sensor_write_regs(client, ptemp,size_num);
1669 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1672 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1677 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1681 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1683 goto sensor_write_array_end;
1686 #if CONFIG_SENSOR_I2C_RDWRCHK
1687 sensor_read(client, regarray[i].reg, &valchk);
1688 if (valchk != regarray[i].val)
1689 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1695 #if CONFIG_SENSOR_Focus
1696 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1697 || (regarray == sensor_init_data)) {
1698 sensor->info_priv.affm_reinit = 1;
1702 sensor_write_array_end:
1703 sensor_task_lock(client,0);
1706 #if CONFIG_SENSOR_I2C_RDWRCHK
1707 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1715 while (regarray[i].reg != 0)
1717 sensor_read(client, regarray[i].reg, &valchk);
1718 if (valchk != regarray[i].val)
1719 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1726 #if CONFIG_SENSOR_Focus
1733 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1736 char read_tag=0xff,cnt;
1739 if (cmdinfo->validate_bit & 0x80) {
1740 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1741 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1742 goto sensor_af_cmdset_err;
1744 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1746 for (i=0; i<4; i++) {
1747 if (cmdinfo->validate_bit & (1<<i)) {
1748 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1749 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]);
1750 goto sensor_af_cmdset_err;
1752 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]);
1756 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
1757 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1758 goto sensor_af_cmdset_err;
1760 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1763 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1764 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1765 goto sensor_af_cmdset_err;
1772 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1773 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1776 } while((read_tag != 0x00)&& (cnt++<100));
1778 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1780 sensor_af_cmdset_err:
1784 static int sensor_af_idlechk(struct i2c_client *client)
1788 struct af_cmdinfo cmdinfo;
1790 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1792 cmdinfo.cmd_tag = 0x01;
1793 cmdinfo.validate_bit = 0x80;
1794 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1796 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1798 goto sensor_af_idlechk_end;
1803 ret = sensor_read(client, CMD_ACK_Reg, &state);
1805 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1807 goto sensor_af_idlechk_end;
1809 }while(0x00 != state);
1812 sensor_af_idlechk_end:
1816 static int sensor_af_single(struct i2c_client *client)
1820 struct af_cmdinfo cmdinfo;
1822 cmdinfo.cmd_tag = 0x01;
1823 cmdinfo.validate_bit = 0x80;
1824 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
1826 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1828 goto sensor_af_single_end;
1838 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1840 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1842 goto sensor_af_single_end;
1844 }while((state == S_FOCUSING) && (cnt<100));
1846 if (state != S_FOCUSED) {
1847 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1849 goto sensor_af_single_end;
1852 sensor_af_single_end:
1856 static int sensor_af_const(struct i2c_client *client)
1865 static int sensor_af_init(struct i2c_client *client)
1870 ret = sensor_write_array(client, sensor_af_firmware);
1872 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1874 goto sensor_af_init_end;
1884 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1886 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1888 goto sensor_af_init_end;
1890 }while((state == S_STARTUP) && (cnt<100));
1892 if (state != S_IDLE) {
1893 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1895 goto sensor_af_init_end;
1899 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1903 static int sensor_af_wq_function(struct i2c_client *client)
1905 struct sensor *sensor = to_sensor(client);
1908 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1910 mutex_lock(&sensor->wq_lock);
1911 if (sensor_af_init(client)) {
1912 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1915 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1916 switch (sensor->info_priv.auto_focus)
1918 /*case SENSOR_AF_MODE_INFINITY:
1922 case SENSOR_AF_MODE_MACRO:
1924 if (focus_pos != 0x00)
1927 sensor_af_idlechk(client);
1928 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
1929 cmdinfo.cmd_para[0] = focus_pos;
1930 cmdinfo.validate_bit = 0x81;
1931 //ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
1934 case SENSOR_AF_MODE_AUTO:
1936 ret = sensor_af_single(client);
1939 /*case SENSOR_AF_MODE_CONTINUOUS:
1941 ret = sensor_af_const(client);
1944 case SENSOR_AF_MODE_CLOSE:
1951 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1952 goto sensor_af_wq_function_end;
1956 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);
1959 sensor_af_wq_function_end:
1960 sensor->sensor_wk.state = sensor_work_ready;
1961 mutex_unlock(&sensor->wq_lock);
1964 static void sensor_af_workqueue(struct work_struct *work)
1966 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
1967 struct i2c_client *client = sensor_work->client;
1969 if (sensor_af_wq_function(client) < 0) {
1970 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
1974 static int sensor_parameter_record(struct i2c_client *client)
1976 u8 ret_l,ret_m,ret_h;
1978 struct sensor *sensor = to_sensor(client);
1980 sensor_write(client,0x3503,0x07); //stop AE/AG
1981 sensor_write(client,0x3406,0x01); //stop AWB
1983 sensor_read(client,0x3500,&ret_h);
1984 sensor_read(client,0x3501, &ret_m);
1985 sensor_read(client,0x3502, &ret_l);
1989 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
1990 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
1991 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
1993 sensor_read(client,0x350c, &ret_h);
1994 sensor_read(client,0x350d, &ret_l);
1995 sensor->parameter.preview_line_width = ret_h & 0xff;
1996 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
1997 //Read back AGC Gain for preview
1998 sensor_read(client,0x350a, &ret_h);
1999 sensor_read(client,0x350b, &tp_l);
2000 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
2002 sensor_read(client,0x380e, &ret_h);
2003 sensor->parameter.preview_maxlines = ret_h;
2004 sensor->parameter.preview_maxlines <<= 8;
2005 sensor_read(client,0x380f, &tp_l);
2006 sensor->parameter.preview_maxlines += tp_l;
2008 sensor->parameter.capture_framerate = 375;
2009 sensor->parameter.preview_framerate = 1500;
2011 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
2012 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2013 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2014 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2015 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2016 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2018 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);
2021 static int sensor_ae_transfer(struct i2c_client *client)
2026 u16 ulCapture_Exposure;
2027 u32 ulCapture_Exposure_Gain;
2031 u8 reg_l = 0,reg_h =0;
2032 u16 Preview_Maxlines;
2034 u32 Capture_MaxLines;
2035 struct sensor *sensor = to_sensor(client);
2037 //Preview_Maxlines = sensor->parameter.preview_line_width;
2038 Preview_Maxlines = sensor->parameter.preview_maxlines;
2039 Gain = sensor->parameter.preview_gain;
2041 sensor_read(client,0x350c, ®_h);
2042 sensor_read(client,0x350d, ®_l);
2043 Capture_MaxLines = reg_h & 0xff;
2044 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2047 sensor_read(client,0x380e, ®_h);
2048 Capture_MaxLines = reg_h;
2049 Capture_MaxLines <<= 8;
2050 sensor_read(client,0x380f, ®_l);
2051 Capture_MaxLines += reg_l;
2054 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2056 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2058 Lines_10ms = Lines_10ms & 0xffff;
2060 if(Preview_Maxlines == 0)
2061 Preview_Maxlines = 1;
2063 //ulCapture_Exposure =
2064 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2066 ulCapture_Exposure =
2067 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2068 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2070 iCapture_Gain = (Gain & 0x0f) + 16;
2072 iCapture_Gain = iCapture_Gain << 1;
2075 iCapture_Gain = iCapture_Gain << 1;
2078 iCapture_Gain = iCapture_Gain << 1;
2081 iCapture_Gain = iCapture_Gain << 1;
2084 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
2085 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2087 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2088 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2089 if (ulCapture_Exposure > Lines_10ms)
2091 //ulCapture_Exposure *= 1.7;
2092 ulCapture_Exposure /= Lines_10ms;
2093 ulCapture_Exposure *= Lines_10ms;
2096 ulCapture_Exposure = Capture_MaxLines;
2097 //ulCapture_Exposure_Gain *= 1.5;
2100 if(ulCapture_Exposure == 0)
2101 ulCapture_Exposure = 1;
2103 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2104 iCapture_Gain = iCapture_Gain & 0xffff;
2106 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2107 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2108 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2112 if (iCapture_Gain > 31) {
2114 iCapture_Gain = iCapture_Gain >> 1;
2116 if (iCapture_Gain > 31) {
2118 iCapture_Gain = iCapture_Gain >> 1;
2120 if (iCapture_Gain > 31) {
2122 iCapture_Gain = iCapture_Gain >> 1;
2124 if (iCapture_Gain > 31) {
2126 iCapture_Gain = iCapture_Gain >> 1;
2128 if (iCapture_Gain > 16)
2129 Gain |= ((iCapture_Gain -16) & 0x0f);
2132 // write the gain and exposure to 0x350* registers
2133 //m_iWrite0x350b=Gain;
2134 sensor_write(client,0x350b, Gain);
2135 Gain = (Gain >> 8) & 0x01;
2136 sensor_write(client,0x350a, Gain);
2137 //m_iWrite0x3502=ExposureLow;
2138 sensor_write(client,0x3502, ExposureLow);
2139 //m_iWrite0x3501=ExposureMid;
2140 sensor_write(client,0x3501, ExposureMid);
2141 //m_iWrite0x3500=ExposureHigh;
2142 sensor_write(client,0x3500, ExposureHigh);
2143 // SendToFile("Gain = 0x%x\r\n", Gain);
2144 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2145 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2146 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2147 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2148 //camera_timed_wait(200);
2149 //linzhk camera_timed_wait(500);
2151 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2152 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2153 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2154 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2155 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2156 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2158 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2162 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2164 struct soc_camera_link *icl = to_soc_camera_link(icd);
2167 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2171 case Sensor_PowerDown:
2173 if (icl->powerdown) {
2174 ret = icl->powerdown(icd->pdev, on);
2175 if (ret == RK29_CAM_IO_SUCCESS) {
2179 icl->reset(icd->pdev);
2181 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2183 goto sensor_power_end;
2190 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2191 struct sensor *sensor = to_sensor(client);
2193 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2194 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2200 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2208 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2210 struct i2c_client *client = sd->priv;
2211 struct soc_camera_device *icd = client->dev.platform_data;
2212 struct sensor *sensor = to_sensor(client);
2213 const struct v4l2_queryctrl *qctrl;
2214 const struct sensor_datafmt *fmt;
2218 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2220 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2222 goto sensor_INIT_ERR;
2226 if (sensor_task_lock(client,1)<0)
2227 goto sensor_INIT_ERR;
2228 ret = sensor_write(client, 0x3008, 0x80);
2230 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2232 goto sensor_INIT_ERR;
2235 mdelay(5); //delay 5 microseconds
2236 /* check if it is an sensor sensor */
2237 ret = sensor_read(client, 0x300a, &value);
2239 SENSOR_TR("read chip id high byte failed\n");
2241 goto sensor_INIT_ERR;
2244 pid |= (value << 8);
2246 ret = sensor_read(client, 0x300b, &value);
2248 SENSOR_TR("read chip id low byte failed\n");
2250 goto sensor_INIT_ERR;
2253 pid |= (value & 0xff);
2254 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2256 if (pid == SENSOR_ID) {
2257 sensor->model = SENSOR_V4L2_IDENT;
2259 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2261 goto sensor_INIT_ERR;
2264 ret = sensor_write_array(client, sensor_init_data);
2267 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2268 goto sensor_INIT_ERR;
2270 sensor_task_lock(client,0);
2271 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2272 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2274 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2276 goto sensor_INIT_ERR;
2278 sensor->info_priv.fmt = *fmt;
2280 /* sensor sensor information for initialization */
2281 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2283 sensor->info_priv.whiteBalance = qctrl->default_value;
2284 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2286 sensor->info_priv.brightness = qctrl->default_value;
2287 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2289 sensor->info_priv.effect = qctrl->default_value;
2290 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2292 sensor->info_priv.exposure = qctrl->default_value;
2294 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2296 sensor->info_priv.saturation = qctrl->default_value;
2297 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2299 sensor->info_priv.contrast = qctrl->default_value;
2300 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2302 sensor->info_priv.mirror = qctrl->default_value;
2303 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2305 sensor->info_priv.flip = qctrl->default_value;
2306 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2308 sensor->info_priv.scene = qctrl->default_value;
2309 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2311 sensor->info_priv.digitalzoom = qctrl->default_value;
2313 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2314 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2316 sensor->info_priv.focus = qctrl->default_value;
2318 #if CONFIG_SENSOR_Flash
2319 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2321 sensor->info_priv.flash = qctrl->default_value;
2323 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);
2325 sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
2329 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2330 sensor_task_lock(client,0);
2331 sensor_deactivate(client);
2334 static int sensor_deactivate(struct i2c_client *client)
2336 struct soc_camera_device *icd = client->dev.platform_data;
2337 struct sensor *sensor = to_sensor(client);
2339 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2341 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2342 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2343 sensor_task_lock(client, 1);
2344 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2345 sensor_write(client, 0x3018, 0x03); // D5-D0
2346 sensor_write(client,0x3019,0x00); // STROBE,SDA
2347 sensor_task_lock(client, 0);
2349 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2351 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2352 icd->user_width = SENSOR_INIT_WIDTH;
2353 icd->user_height = SENSOR_INIT_HEIGHT;
2354 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2358 static struct reginfo sensor_power_down_sequence[]=
2363 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2366 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2368 if (pm_msg.event == PM_EVENT_SUSPEND) {
2369 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2370 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2372 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2375 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2377 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2382 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2389 static int sensor_resume(struct soc_camera_device *icd)
2393 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2395 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2399 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2403 static int sensor_set_bus_param(struct soc_camera_device *icd,
2404 unsigned long flags)
2410 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2412 struct soc_camera_link *icl = to_soc_camera_link(icd);
2413 unsigned long flags = SENSOR_BUS_PARAM;
2415 return soc_camera_apply_sensor_flags(icl, flags);
2418 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2420 struct i2c_client *client = sd->priv;
2421 struct soc_camera_device *icd = client->dev.platform_data;
2422 struct sensor *sensor = to_sensor(client);
2424 mf->width = icd->user_width;
2425 mf->height = icd->user_height;
2426 mf->code = sensor->info_priv.fmt.code;
2427 mf->colorspace = sensor->info_priv.fmt.colorspace;
2428 mf->field = V4L2_FIELD_NONE;
2432 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2436 if ((mf->width == 1024) && (mf->height == 768)) {
2438 } else if ((mf->width == 1280) && (mf->height == 1024)) {
2440 } else if ((mf->width == 1600) && (mf->height == 1200)) {
2442 } else if ((mf->width == 2048) && (mf->height == 1536)) {
2444 } else if ((mf->width == 2592) && (mf->height == 1944)) {
2449 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2453 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2457 if ((mf->width == 1280) && (mf->height == 720)) {
2459 } else if ((mf->width == 1920) && (mf->height == 1080)) {
2464 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2467 static int sensor_s_fmt(struct v4l2_subdev *sd,struct v4l2_mbus_framefmt *mf)
2469 struct i2c_client *client = sd->priv;
2470 const struct sensor_datafmt *fmt;
2471 struct sensor *sensor = to_sensor(client);
2472 const struct v4l2_queryctrl *qctrl;
2473 struct soc_camera_device *icd = client->dev.platform_data;
2474 struct reginfo *winseqe_set_addr=NULL;
2475 int ret = 0, set_w,set_h;
2477 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2478 ARRAY_SIZE(sensor_colour_fmts));
2481 goto sensor_s_fmt_end;
2484 if (sensor->info_priv.fmt.code != mf->code) {
2487 case V4L2_MBUS_FMT_YUYV8_2X8:
2489 winseqe_set_addr = sensor_ClrFmt_YUYV;
2492 case V4L2_MBUS_FMT_UYVY8_2X8:
2494 winseqe_set_addr = sensor_ClrFmt_UYVY;
2500 if (winseqe_set_addr != NULL) {
2501 sensor_write_array(client, winseqe_set_addr);
2502 sensor->info_priv.fmt.code = mf->code;
2503 sensor->info_priv.fmt.colorspace= mf->colorspace;
2504 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2506 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2513 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2515 winseqe_set_addr = sensor_qcif;
2519 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2521 winseqe_set_addr = sensor_qvga;
2525 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2527 winseqe_set_addr = sensor_cif;
2531 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2533 winseqe_set_addr = sensor_vga;
2537 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2539 winseqe_set_addr = sensor_svga;
2543 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2545 winseqe_set_addr = sensor_xga;
2549 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2551 winseqe_set_addr = sensor_720p;
2555 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2557 winseqe_set_addr = sensor_sxga;
2561 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2563 winseqe_set_addr = sensor_uxga;
2567 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2569 winseqe_set_addr = sensor_1080p;
2573 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2575 winseqe_set_addr = sensor_qxga;
2579 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2581 winseqe_set_addr = sensor_qsxga;
2587 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2588 set_w = SENSOR_INIT_WIDTH;
2589 set_h = SENSOR_INIT_HEIGHT;
2590 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2593 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2595 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2596 sensor_parameter_record(client);
2597 /*#if CONFIG_SENSOR_Focus
2598 sensor_af_idlechk(client);
2599 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2600 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2602 #if CONFIG_SENSOR_Flash
2603 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2604 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2605 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2608 }else { /* ddl@rock-chips.com : Video */
2609 #if CONFIG_SENSOR_Flash
2610 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2611 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2612 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2616 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2617 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2618 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2619 ret |= sensor_write_array(client,sensor_init_data);
2620 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2623 ret |= sensor_write_array(client, winseqe_set_addr);
2625 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2626 #if CONFIG_SENSOR_Flash
2627 if (sensor_fmt_capturechk(sd,mf) == true) {
2628 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2629 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2630 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2634 goto sensor_s_fmt_end;
2636 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2638 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2639 sensor_ae_transfer(client);
2640 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2641 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2642 if (sensor->info_priv.whiteBalance != 0) {
2643 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2644 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2646 sensor->info_priv.snap2preview = true;
2647 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2648 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2649 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2651 sensor->info_priv.video2preview = true;
2652 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2653 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2654 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2655 if (sensor->info_priv.snap2preview == true) {
2656 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2657 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2659 #if CONFIG_SENSOR_Focus
2660 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
2661 sensor_af_idlechk(client);
2669 sensor->info_priv.video2preview = false;
2670 sensor->info_priv.snap2preview = false;
2672 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2676 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2684 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2686 struct i2c_client *client = sd->priv;
2687 struct sensor *sensor = to_sensor(client);
2688 const struct sensor_datafmt *fmt;
2691 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2692 ARRAY_SIZE(sensor_colour_fmts));
2694 fmt = &sensor->info_priv.fmt;
2695 mf->code = fmt->code;
2698 if (mf->height > SENSOR_MAX_HEIGHT)
2699 mf->height = SENSOR_MAX_HEIGHT;
2700 else if (mf->height < SENSOR_MIN_HEIGHT)
2701 mf->height = SENSOR_MIN_HEIGHT;
2703 if (mf->width > SENSOR_MAX_WIDTH)
2704 mf->width = SENSOR_MAX_WIDTH;
2705 else if (mf->width < SENSOR_MIN_WIDTH)
2706 mf->width = SENSOR_MIN_WIDTH;
2708 mf->colorspace = fmt->colorspace;
2713 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2715 struct i2c_client *client = sd->priv;
2717 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2720 if (id->match.addr != client->addr)
2723 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2728 #if CONFIG_SENSOR_Brightness
2729 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2731 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2733 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2735 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2737 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2739 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2742 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2746 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2750 #if CONFIG_SENSOR_Effect
2751 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2753 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2755 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2757 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2759 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2761 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2764 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2768 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2772 #if CONFIG_SENSOR_Exposure
2773 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2775 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2777 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2779 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2781 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2783 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2786 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2790 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2794 #if CONFIG_SENSOR_Saturation
2795 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2797 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2799 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2801 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2803 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2805 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2808 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2812 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2816 #if CONFIG_SENSOR_Contrast
2817 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2819 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2821 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2823 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2825 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2827 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2830 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2834 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2838 #if CONFIG_SENSOR_Mirror
2839 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2841 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2843 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2845 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2847 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2849 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2852 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2856 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2860 #if CONFIG_SENSOR_Flip
2861 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2863 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2865 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2867 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2869 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2871 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2874 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2878 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2882 #if CONFIG_SENSOR_Scene
2883 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2885 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2887 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2889 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2891 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2893 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2896 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2900 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2904 #if CONFIG_SENSOR_WhiteBalance
2905 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2907 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2909 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2911 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2913 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2915 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2918 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2922 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2926 #if CONFIG_SENSOR_DigitalZoom
2927 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2929 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2930 struct sensor *sensor = to_sensor(client);
2931 const struct v4l2_queryctrl *qctrl_info;
2932 int digitalzoom_cur, digitalzoom_total;
2934 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2938 digitalzoom_cur = sensor->info_priv.digitalzoom;
2939 digitalzoom_total = qctrl_info->maximum;
2941 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2943 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2947 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2949 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2953 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2955 *value = digitalzoom_total - digitalzoom_cur;
2958 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2960 *value = 0 - digitalzoom_cur;
2963 digitalzoom_cur += *value;
2965 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2967 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2969 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2972 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2979 #if CONFIG_SENSOR_Focus
2980 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2982 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2983 struct sensor *sensor = to_sensor(client);
2984 const struct v4l2_queryctrl *qctrl_info;
2987 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2990 goto sensor_set_focus_absolute_end;
2993 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2994 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2995 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2998 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3002 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3003 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3006 sensor_set_focus_absolute_end:
3009 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3011 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3012 struct sensor *sensor = to_sensor(client);
3013 const struct v4l2_queryctrl *qctrl_info;
3016 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3019 goto sensor_set_focus_relative_end;
3022 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3023 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3024 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3027 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3031 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3032 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3034 sensor_set_focus_relative_end:
3038 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3040 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3041 struct sensor *sensor = to_sensor(client);
3044 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3047 case SENSOR_AF_MODE_AUTO:
3049 ret = sensor_af_single(client);
3053 /*case SENSOR_AF_MODE_MACRO:
3055 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3059 case SENSOR_AF_MODE_INFINITY:
3061 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3065 case SENSOR_AF_MODE_CONTINUOUS:
3067 ret = sensor_af_const(client);
3071 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3076 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3079 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3080 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3086 #if CONFIG_SENSOR_Flash
3087 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3089 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3090 if (value == 3) { /* ddl@rock-chips.com: torch */
3091 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3093 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3095 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3099 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3103 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3105 struct i2c_client *client = sd->priv;
3106 struct sensor *sensor = to_sensor(client);
3107 const struct v4l2_queryctrl *qctrl;
3109 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3113 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3119 case V4L2_CID_BRIGHTNESS:
3121 ctrl->value = sensor->info_priv.brightness;
3124 case V4L2_CID_SATURATION:
3126 ctrl->value = sensor->info_priv.saturation;
3129 case V4L2_CID_CONTRAST:
3131 ctrl->value = sensor->info_priv.contrast;
3134 case V4L2_CID_DO_WHITE_BALANCE:
3136 ctrl->value = sensor->info_priv.whiteBalance;
3139 case V4L2_CID_EXPOSURE:
3141 ctrl->value = sensor->info_priv.exposure;
3144 case V4L2_CID_HFLIP:
3146 ctrl->value = sensor->info_priv.mirror;
3149 case V4L2_CID_VFLIP:
3151 ctrl->value = sensor->info_priv.flip;
3162 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3164 struct i2c_client *client = sd->priv;
3165 struct sensor *sensor = to_sensor(client);
3166 struct soc_camera_device *icd = client->dev.platform_data;
3167 const struct v4l2_queryctrl *qctrl;
3170 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3174 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3180 #if CONFIG_SENSOR_Brightness
3181 case V4L2_CID_BRIGHTNESS:
3183 if (ctrl->value != sensor->info_priv.brightness)
3185 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3189 sensor->info_priv.brightness = ctrl->value;
3194 #if CONFIG_SENSOR_Exposure
3195 case V4L2_CID_EXPOSURE:
3197 if (ctrl->value != sensor->info_priv.exposure)
3199 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3203 sensor->info_priv.exposure = ctrl->value;
3208 #if CONFIG_SENSOR_Saturation
3209 case V4L2_CID_SATURATION:
3211 if (ctrl->value != sensor->info_priv.saturation)
3213 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3217 sensor->info_priv.saturation = ctrl->value;
3222 #if CONFIG_SENSOR_Contrast
3223 case V4L2_CID_CONTRAST:
3225 if (ctrl->value != sensor->info_priv.contrast)
3227 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3231 sensor->info_priv.contrast = ctrl->value;
3236 #if CONFIG_SENSOR_WhiteBalance
3237 case V4L2_CID_DO_WHITE_BALANCE:
3239 if (ctrl->value != sensor->info_priv.whiteBalance)
3241 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3245 sensor->info_priv.whiteBalance = ctrl->value;
3250 #if CONFIG_SENSOR_Mirror
3251 case V4L2_CID_HFLIP:
3253 if (ctrl->value != sensor->info_priv.mirror)
3255 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3257 sensor->info_priv.mirror = ctrl->value;
3262 #if CONFIG_SENSOR_Flip
3263 case V4L2_CID_VFLIP:
3265 if (ctrl->value != sensor->info_priv.flip)
3267 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3269 sensor->info_priv.flip = ctrl->value;
3280 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3282 const struct v4l2_queryctrl *qctrl;
3283 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3284 struct sensor *sensor = to_sensor(client);
3286 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3290 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3294 switch (ext_ctrl->id)
3296 case V4L2_CID_SCENE:
3298 ext_ctrl->value = sensor->info_priv.scene;
3301 case V4L2_CID_EFFECT:
3303 ext_ctrl->value = sensor->info_priv.effect;
3306 case V4L2_CID_ZOOM_ABSOLUTE:
3308 ext_ctrl->value = sensor->info_priv.digitalzoom;
3311 case V4L2_CID_ZOOM_RELATIVE:
3315 case V4L2_CID_FOCUS_ABSOLUTE:
3319 case V4L2_CID_FOCUS_RELATIVE:
3323 case V4L2_CID_FLASH:
3325 ext_ctrl->value = sensor->info_priv.flash;
3333 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3335 const struct v4l2_queryctrl *qctrl;
3336 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3337 struct sensor *sensor = to_sensor(client);
3340 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3344 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3349 switch (ext_ctrl->id)
3351 #if CONFIG_SENSOR_Scene
3352 case V4L2_CID_SCENE:
3354 if (ext_ctrl->value != sensor->info_priv.scene)
3356 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3358 sensor->info_priv.scene = ext_ctrl->value;
3363 #if CONFIG_SENSOR_Effect
3364 case V4L2_CID_EFFECT:
3366 if (ext_ctrl->value != sensor->info_priv.effect)
3368 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3370 sensor->info_priv.effect= ext_ctrl->value;
3375 #if CONFIG_SENSOR_DigitalZoom
3376 case V4L2_CID_ZOOM_ABSOLUTE:
3378 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3381 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3383 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3385 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3387 sensor->info_priv.digitalzoom += val_offset;
3389 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3394 case V4L2_CID_ZOOM_RELATIVE:
3396 if (ext_ctrl->value)
3398 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3400 sensor->info_priv.digitalzoom += ext_ctrl->value;
3402 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3407 #if CONFIG_SENSOR_Focus
3408 case V4L2_CID_FOCUS_ABSOLUTE:
3410 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3415 case V4L2_CID_FOCUS_RELATIVE:
3417 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3420 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3423 case V4L2_CID_FOCUS_AUTO:
3425 if (ext_ctrl->value == 1) {
3426 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
3428 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3429 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3430 if (ext_ctrl->value == 0)
3431 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3435 case V4L2_CID_FOCUS_CONTINUOUS:
3437 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3438 if (ext_ctrl->value == 1) {
3439 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3441 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3444 if (ext_ctrl->value == 0)
3445 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3450 #if CONFIG_SENSOR_Flash
3451 case V4L2_CID_FLASH:
3453 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3455 sensor->info_priv.flash = ext_ctrl->value;
3457 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3468 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3470 struct i2c_client *client = sd->priv;
3471 struct soc_camera_device *icd = client->dev.platform_data;
3472 int i, error_cnt=0, error_idx=-1;
3475 for (i=0; i<ext_ctrl->count; i++) {
3476 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3483 error_idx = ext_ctrl->count;
3485 if (error_idx != -1) {
3486 ext_ctrl->error_idx = error_idx;
3493 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3495 struct i2c_client *client = sd->priv;
3496 struct soc_camera_device *icd = client->dev.platform_data;
3497 int i, error_cnt=0, error_idx=-1;
3499 for (i=0; i<ext_ctrl->count; i++) {
3500 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3507 error_idx = ext_ctrl->count;
3509 if (error_idx != -1) {
3510 ext_ctrl->error_idx = error_idx;
3517 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3519 struct i2c_client *client = sd->priv;
3520 struct sensor *sensor = to_sensor(client);
3521 #if CONFIG_SENSOR_Focus
3522 struct soc_camera_device *icd = client->dev.platform_data;
3523 struct v4l2_mbus_framefmt mf;
3527 sensor->info_priv.enable = 1;
3528 #if CONFIG_SENSOR_Focus
3529 mf.width = icd->user_width;
3530 mf.height = icd->user_height;
3531 mf.code = sensor->info_priv.fmt.code;
3532 mf.colorspace = sensor->info_priv.fmt.colorspace;
3533 mf.field = V4L2_FIELD_NONE;
3534 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3535 if (sensor_fmt_capturechk(sd, &mf) == false) {
3536 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3537 if (sensor->sensor_wq != NULL) {
3538 mutex_lock(&sensor->wq_lock);
3539 if (sensor->sensor_wk.state == sensor_working) {
3540 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
3541 mutex_unlock(&sensor->wq_lock);
3542 goto sensor_s_stream_end;
3544 sensor->sensor_wk.state = sensor_working;
3545 mutex_unlock(&sensor->wq_lock);
3546 sensor->sensor_wk.client = client;
3547 INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
3548 queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
3550 sensor->info_priv.affm_reinit = 0;
3554 } else if (enable == 0) {
3555 sensor->info_priv.enable = 0;
3556 #if CONFIG_SENSOR_Focus
3557 flush_work(&(sensor->sensor_wk.dwork.work));
3558 mutex_lock(&sensor->wq_lock);
3559 sensor->sensor_wk.state = sensor_work_ready;
3560 mutex_unlock(&sensor->wq_lock);
3564 sensor_s_stream_end:
3568 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3569 * this wasn't our capture interface, so, we wait for the right one */
3570 static int sensor_video_probe(struct soc_camera_device *icd,
3571 struct i2c_client *client)
3575 struct sensor *sensor = to_sensor(client);
3577 /* We must have a parent by now. And it cannot be a wrong one.
3578 * So this entire test is completely redundant. */
3579 if (!icd->dev.parent ||
3580 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3583 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3585 goto sensor_video_probe_err;
3588 ret = sensor_write(client, 0x3008, 0x80);
3590 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3592 goto sensor_video_probe_err;
3594 mdelay(5); //delay 5 microseconds
3596 /* check if it is an sensor sensor */
3597 ret = sensor_read(client, 0x300a, &value);
3599 SENSOR_TR("read chip id high byte failed\n");
3601 goto sensor_video_probe_err;
3604 pid |= (value << 8);
3606 ret = sensor_read(client, 0x300b, &value);
3608 SENSOR_TR("read chip id low byte failed\n");
3610 goto sensor_video_probe_err;
3613 pid |= (value & 0xff);
3614 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3615 if (pid == SENSOR_ID) {
3616 sensor->model = SENSOR_V4L2_IDENT;
3618 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3620 goto sensor_video_probe_err;
3625 sensor_video_probe_err:
3628 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3630 struct i2c_client *client = sd->priv;
3631 struct soc_camera_device *icd = client->dev.platform_data;
3632 struct sensor *sensor = to_sensor(client);
3635 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3638 case RK29_CAM_SUBDEV_DEACTIVATE:
3640 sensor_deactivate(client);
3643 case RK29_CAM_SUBDEV_IOREQUEST:
3645 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3646 if (sensor->sensor_io_request != NULL) {
3647 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
3648 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
3649 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
3650 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
3651 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
3652 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
3655 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3657 goto sensor_ioctl_end;
3659 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3661 #if CONFIG_SENSOR_Flash
3663 if (sensor->sensor_gpio_res) {
3664 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3665 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3666 for (i = 0; i < icd->ops->num_controls; i++) {
3667 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3668 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3671 sensor->info_priv.flash = 0xff;
3672 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3680 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3689 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3690 enum v4l2_mbus_pixelcode *code)
3692 if (index >= ARRAY_SIZE(sensor_colour_fmts))
3695 *code = sensor_colour_fmts[index].code;
3698 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3699 .init = sensor_init,
3700 .g_ctrl = sensor_g_control,
3701 .s_ctrl = sensor_s_control,
3702 .g_ext_ctrls = sensor_g_ext_controls,
3703 .s_ext_ctrls = sensor_s_ext_controls,
3704 .g_chip_ident = sensor_g_chip_ident,
3705 .ioctl = sensor_ioctl,
3708 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3709 .s_mbus_fmt = sensor_s_fmt,
3710 .g_mbus_fmt = sensor_g_fmt,
3711 .try_mbus_fmt = sensor_try_fmt,
3712 .enum_mbus_fmt = sensor_enum_fmt,
3713 .s_stream = sensor_s_stream,
3716 static struct v4l2_subdev_ops sensor_subdev_ops = {
3717 .core = &sensor_subdev_core_ops,
3718 .video = &sensor_subdev_video_ops,
3721 static int sensor_probe(struct i2c_client *client,
3722 const struct i2c_device_id *did)
3724 struct sensor *sensor;
3725 struct soc_camera_device *icd = client->dev.platform_data;
3726 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3727 struct soc_camera_link *icl;
3730 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3732 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3736 icl = to_soc_camera_link(icd);
3738 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3742 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3743 dev_warn(&adapter->dev,
3744 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3748 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3752 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3754 /* Second stage probe - when a capture adapter is there */
3755 icd->ops = &sensor_ops;
3756 sensor->info_priv.fmt = sensor_colour_fmts[0];
3757 #if CONFIG_SENSOR_I2C_NOSCHED
3758 atomic_set(&sensor->tasklock_cnt,0);
3761 ret = sensor_video_probe(icd, client);
3764 i2c_set_clientdata(client, NULL);
3768 #if CONFIG_SENSOR_Focus
3769 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
3770 if (sensor->sensor_wq == NULL)
3771 SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
3772 mutex_init(&sensor->wq_lock);
3773 sensor->sensor_wk.state = sensor_work_ready;
3777 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3781 static int sensor_remove(struct i2c_client *client)
3783 struct sensor *sensor = to_sensor(client);
3784 struct soc_camera_device *icd = client->dev.platform_data;
3786 #if CONFIG_SENSOR_Focus
3787 if (sensor->sensor_wq) {
3788 destroy_workqueue(sensor->sensor_wq);
3789 sensor->sensor_wq = NULL;
3794 i2c_set_clientdata(client, NULL);
3795 client->driver = NULL;
3801 static const struct i2c_device_id sensor_id[] = {
3802 {SENSOR_NAME_STRING(), 0 },
3805 MODULE_DEVICE_TABLE(i2c, sensor_id);
3807 static struct i2c_driver sensor_i2c_driver = {
3809 .name = SENSOR_NAME_STRING(),
3811 .probe = sensor_probe,
3812 .remove = sensor_remove,
3813 .id_table = sensor_id,
3816 static int __init sensor_mod_init(void)
3818 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3819 return i2c_add_driver(&sensor_i2c_driver);
3822 static void __exit sensor_mod_exit(void)
3824 i2c_del_driver(&sensor_i2c_driver);
3827 device_initcall_sync(sensor_mod_init);
3828 module_exit(sensor_mod_exit);
3830 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3831 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3832 MODULE_LICENSE("GPL");