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(0, 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
58 #define CONFIG_SENSOR_WhiteBalance 1
59 #define CONFIG_SENSOR_Brightness 0
60 #define CONFIG_SENSOR_Contrast 0
61 #define CONFIG_SENSOR_Saturation 0
62 #define CONFIG_SENSOR_Effect 1
63 #define CONFIG_SENSOR_Scene 1
64 #define CONFIG_SENSOR_DigitalZoom 0
65 #define CONFIG_SENSOR_Exposure 0
66 #define CONFIG_SENSOR_Flash 1
67 #define CONFIG_SENSOR_Mirror 0
68 #define CONFIG_SENSOR_Flip 0
69 #ifdef CONFIG_OV5640_AUTOFOCUS
70 #define CONFIG_SENSOR_Focus 1
71 #include "ov5640_af_firmware.c"
73 #define CONFIG_SENSOR_Focus 0
76 #define CONFIG_SENSOR_I2C_SPEED 100000 /* 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
82 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
83 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
84 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
86 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
87 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
88 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
89 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
90 #define COLOR_TEMPERATURE_OFFICE_DN 3500
91 #define COLOR_TEMPERATURE_OFFICE_UP 5000
92 #define COLOR_TEMPERATURE_HOME_DN 2500
93 #define COLOR_TEMPERATURE_HOME_UP 3500
95 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
96 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
98 #define SENSOR_AF_IS_ERR (0x00<<0)
99 #define SENSOR_AF_IS_OK (0x01<<0)
101 #if CONFIG_SENSOR_Focus
102 #define SENSOR_AF_MODE_INFINITY 0
103 #define SENSOR_AF_MODE_MACRO 1
104 #define SENSOR_AF_MODE_FIXED 2
105 #define SENSOR_AF_MODE_AUTO 3
106 #define SENSOR_AF_MODE_CONTINUOUS 4
107 #define SENSOR_AF_MODE_CLOSE 5
110 #if CONFIG_SENSOR_Focus
111 /* ov5640 VCM Command and Status Registers */
112 #define CMD_MAIN_Reg 0x3024
113 #define CMD_TAG_Reg 0x3025
114 #define CMD_PARA0_Reg 0x5085
115 #define CMD_PARA1_Reg 0x5084
116 #define CMD_PARA2_Reg 0x5083
117 #define CMD_PARA3_Reg 0x5082
118 #define STA_ZONE_Reg 0x3026
119 #define STA_FOCUS_Reg 0x3027
121 /* ov5640 VCM Command */
122 #define OverlayEn_Cmd 0x01
123 #define OverlayDis_Cmd 0x02
124 #define SingleFocus_Cmd 0x03
125 #define ConstFocus_Cmd 0x04
126 #define StepMode_Cmd 0x05
127 #define PauseFocus_Cmd 0x06
128 #define ReturnIdle_Cmd 0x08
129 #define SetZone_Cmd 0x10
130 #define UpdateZone_Cmd 0x12
131 #define SetMotor_Cmd 0x20
133 /* ov5640 Focus State */
134 #define S_FIRWRE 0xFF
135 #define S_STARTUP 0xfa
137 #define S_DRVICERR 0xee
139 #define S_FOCUSING 0x01
140 #define S_FOCUSED 0x02
141 #define S_CAPTURE 0x12
144 /* ov5640 Zone State */
145 #define Zone_Is_Focused(a, zone_val) (zone_val&(1<<(a-3)))
146 #define Zone_Get_ID(zone_val) (zone_val&0x03)
148 #define Zone_CenterMode 0x01
149 #define Zone_5xMode 0x02
150 #define Zone_5PlusMode 0x03
151 #define Zone_4fMode 0x04
153 #define ZoneSel_Auto 0x0b
154 #define ZoneSel_SemiAuto 0x0c
155 #define ZoneSel_Manual 0x0d
156 #define ZoneSel_Rotate 0x0e
158 /* ov5640 Step Focus Commands */
159 #define StepFocus_Near_Tag 0x01
160 #define StepFocus_Far_Tag 0x02
161 #define StepFocus_Furthest_Tag 0x03
162 #define StepFocus_Nearest_Tag 0x04
163 #define StepFocus_Spec_Tag 0x10
166 /* init 800X600 SVGA */
167 static struct reginfo sensor_init_data[] =
429 /* 720p 15fps @ 1280x720 */
431 static struct reginfo sensor_720p[]=
437 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
439 static struct reginfo sensor_1080p[]=
445 /* 2592X1944 QSXGA */
446 static struct reginfo sensor_qsxga[] =
493 static struct reginfo sensor_qxga[] =
567 static struct reginfo sensor_uxga[] =
641 static struct reginfo sensor_sxga[] =
647 static struct reginfo sensor_xga[] =
677 static struct reginfo sensor_svga[] =
725 static struct reginfo sensor_vga[] =
731 static struct reginfo sensor_cif[] =
738 static struct reginfo sensor_qvga[] =
745 static struct reginfo sensor_qcif[] =
751 static struct reginfo sensor_ClrFmt_YUYV[]=
757 static struct reginfo sensor_ClrFmt_UYVY[]=
764 #if CONFIG_SENSOR_WhiteBalance
765 static struct reginfo sensor_WhiteB_Auto[]=
773 /* Cloudy Colour Temperature : 6500K - 8000K */
774 static struct reginfo sensor_WhiteB_Cloudy[]=
785 /* ClearDay Colour Temperature : 5000K - 6500K */
786 static struct reginfo sensor_WhiteB_ClearDay[]=
797 /* Office Colour Temperature : 3500K - 5000K */
798 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
809 /* Home Colour Temperature : 2500K - 3500K */
810 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
821 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
822 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
826 #if CONFIG_SENSOR_Brightness
827 static struct reginfo sensor_Brightness0[]=
831 static struct reginfo sensor_Brightness1[]=
836 static struct reginfo sensor_Brightness2[]=
840 static struct reginfo sensor_Brightness3[]=
844 static struct reginfo sensor_Brightness4[]=
849 static struct reginfo sensor_Brightness5[]=
853 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
854 sensor_Brightness4, sensor_Brightness5,NULL,
859 #if CONFIG_SENSOR_Effect
860 static struct reginfo sensor_Effect_Normal[] =
866 static struct reginfo sensor_Effect_WandB[] =
874 static struct reginfo sensor_Effect_Sepia[] =
883 static struct reginfo sensor_Effect_Negative[] =
889 };static struct reginfo sensor_Effect_Bluish[] =
899 static struct reginfo sensor_Effect_Green[] =
908 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
909 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
912 #if CONFIG_SENSOR_Exposure
913 static struct reginfo sensor_Exposure0[]=
917 static struct reginfo sensor_Exposure1[]=
921 static struct reginfo sensor_Exposure2[]=
926 static struct reginfo sensor_Exposure3[]=
930 static struct reginfo sensor_Exposure4[]=
934 static struct reginfo sensor_Exposure5[]=
938 static struct reginfo sensor_Exposure6[]=
942 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
943 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
946 #if CONFIG_SENSOR_Saturation
947 static struct reginfo sensor_Saturation0[]=
952 static struct reginfo sensor_Saturation1[]=
957 static struct reginfo sensor_Saturation2[]=
960 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
963 #if CONFIG_SENSOR_Contrast
964 static struct reginfo sensor_Contrast0[]=
969 static struct reginfo sensor_Contrast1[]=
973 static struct reginfo sensor_Contrast2[]=
978 static struct reginfo sensor_Contrast3[]=
983 static struct reginfo sensor_Contrast4[]=
989 static struct reginfo sensor_Contrast5[]=
994 static struct reginfo sensor_Contrast6[]=
998 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
999 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1003 #if CONFIG_SENSOR_Mirror
1004 static struct reginfo sensor_MirrorOn[]=
1006 {SEQUENCE_END, 0x00}
1008 static struct reginfo sensor_MirrorOff[]=
1010 {SEQUENCE_END, 0x00}
1012 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1014 #if CONFIG_SENSOR_Flip
1015 static struct reginfo sensor_FlipOn[]=
1017 {SEQUENCE_END, 0x00}
1020 static struct reginfo sensor_FlipOff[]=
1022 {SEQUENCE_END, 0x00}
1024 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1027 #if CONFIG_SENSOR_Scene
1028 static struct reginfo sensor_SceneAuto[] =
1031 {SEQUENCE_END, 0x00}
1033 static struct reginfo sensor_SceneNight[] =
1035 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1053 {SEQUENCE_END, 0x00}
1055 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1058 #if CONFIG_SENSOR_DigitalZoom
1059 static struct reginfo sensor_Zoom0[] =
1061 {SEQUENCE_END, 0x00}
1063 static struct reginfo sensor_Zoom1[] =
1065 {SEQUENCE_END, 0x00}
1068 static struct reginfo sensor_Zoom2[] =
1070 {SEQUENCE_END, 0x00}
1074 static struct reginfo sensor_Zoom3[] =
1076 {SEQUENCE_END, 0x00}
1078 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1080 static const struct v4l2_querymenu sensor_menus[] =
1082 #if CONFIG_SENSOR_WhiteBalance
1083 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1084 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1085 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1088 #if CONFIG_SENSOR_Effect
1089 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1090 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1091 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1094 #if CONFIG_SENSOR_Scene
1095 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1098 #if CONFIG_SENSOR_Flash
1099 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1100 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1104 static const struct v4l2_queryctrl sensor_controls[] =
1106 #if CONFIG_SENSOR_WhiteBalance
1108 .id = V4L2_CID_DO_WHITE_BALANCE,
1109 .type = V4L2_CTRL_TYPE_MENU,
1110 .name = "White Balance Control",
1118 #if CONFIG_SENSOR_Brightness
1120 .id = V4L2_CID_BRIGHTNESS,
1121 .type = V4L2_CTRL_TYPE_INTEGER,
1122 .name = "Brightness Control",
1130 #if CONFIG_SENSOR_Effect
1132 .id = V4L2_CID_EFFECT,
1133 .type = V4L2_CTRL_TYPE_MENU,
1134 .name = "Effect Control",
1142 #if CONFIG_SENSOR_Exposure
1144 .id = V4L2_CID_EXPOSURE,
1145 .type = V4L2_CTRL_TYPE_INTEGER,
1146 .name = "Exposure Control",
1154 #if CONFIG_SENSOR_Saturation
1156 .id = V4L2_CID_SATURATION,
1157 .type = V4L2_CTRL_TYPE_INTEGER,
1158 .name = "Saturation Control",
1166 #if CONFIG_SENSOR_Contrast
1168 .id = V4L2_CID_CONTRAST,
1169 .type = V4L2_CTRL_TYPE_INTEGER,
1170 .name = "Contrast Control",
1178 #if CONFIG_SENSOR_Mirror
1180 .id = V4L2_CID_HFLIP,
1181 .type = V4L2_CTRL_TYPE_BOOLEAN,
1182 .name = "Mirror Control",
1190 #if CONFIG_SENSOR_Flip
1192 .id = V4L2_CID_VFLIP,
1193 .type = V4L2_CTRL_TYPE_BOOLEAN,
1194 .name = "Flip Control",
1202 #if CONFIG_SENSOR_Scene
1204 .id = V4L2_CID_SCENE,
1205 .type = V4L2_CTRL_TYPE_MENU,
1206 .name = "Scene Control",
1214 #if CONFIG_SENSOR_DigitalZoom
1216 .id = V4L2_CID_ZOOM_RELATIVE,
1217 .type = V4L2_CTRL_TYPE_INTEGER,
1218 .name = "DigitalZoom Control",
1224 .id = V4L2_CID_ZOOM_ABSOLUTE,
1225 .type = V4L2_CTRL_TYPE_INTEGER,
1226 .name = "DigitalZoom Control",
1234 #if CONFIG_SENSOR_Focus
1236 .id = V4L2_CID_FOCUS_RELATIVE,
1237 .type = V4L2_CTRL_TYPE_INTEGER,
1238 .name = "Focus Control",
1244 .id = V4L2_CID_FOCUS_ABSOLUTE,
1245 .type = V4L2_CTRL_TYPE_INTEGER,
1246 .name = "Focus Control",
1250 .default_value = 125,
1253 .id = V4L2_CID_FOCUS_AUTO,
1254 .type = V4L2_CTRL_TYPE_BOOLEAN,
1255 .name = "Focus Control",
1261 .id = V4L2_CID_FOCUS_CONTINUOUS,
1262 .type = V4L2_CTRL_TYPE_BOOLEAN,
1263 .name = "Focus Control",
1271 #if CONFIG_SENSOR_Flash
1273 .id = V4L2_CID_FLASH,
1274 .type = V4L2_CTRL_TYPE_MENU,
1275 .name = "Flash Control",
1284 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1285 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1286 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1287 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1288 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1289 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1290 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1291 static int sensor_resume(struct soc_camera_device *icd);
1292 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1293 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1294 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1295 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1296 static int sensor_deactivate(struct i2c_client *client);
1298 static struct soc_camera_ops sensor_ops =
1300 .suspend = sensor_suspend,
1301 .resume = sensor_resume,
1302 .set_bus_param = sensor_set_bus_param,
1303 .query_bus_param = sensor_query_bus_param,
1304 .controls = sensor_controls,
1305 .menus = sensor_menus,
1306 .num_controls = ARRAY_SIZE(sensor_controls),
1307 .num_menus = ARRAY_SIZE(sensor_menus),
1310 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1311 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1312 .colorspace = _colorspace }
1314 #define JPG_FMT(_name, _depth, _fourcc) \
1315 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1317 static const struct soc_camera_data_format sensor_colour_formats[] = {
1318 JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1319 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1321 enum sensor_work_state
1323 sensor_work_ready = 0,
1328 struct i2c_client *client;
1329 struct delayed_work dwork;
1330 enum sensor_work_state state;
1333 typedef struct sensor_info_priv_s
1349 unsigned char mirror; /* HFLIP */
1350 unsigned char flip; /* VFLIP */
1351 struct reginfo *winseqe_cur_addr;
1352 unsigned int pixfmt;
1353 unsigned int enable;
1354 unsigned int funmodule_state;
1355 } sensor_info_priv_t;
1359 struct sensor_parameter
1361 unsigned short int preview_maxlines;
1362 unsigned short int preview_exposure;
1363 unsigned short int preview_line_width;
1364 unsigned short int preview_gain;
1366 unsigned short int capture_framerate;
1367 unsigned short int preview_framerate;
1373 struct v4l2_subdev subdev;
1374 struct i2c_client *client;
1375 sensor_info_priv_t info_priv;
1376 struct sensor_parameter parameter;
1377 struct workqueue_struct *sensor_wq;
1378 struct sensor_work sensor_wk;
1379 struct mutex wq_lock;
1380 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1381 #if CONFIG_SENSOR_I2C_NOSCHED
1382 atomic_t tasklock_cnt;
1384 struct rk29camera_platform_data *sensor_io_request;
1385 struct rk29camera_gpio_res *sensor_gpio_res;
1388 static struct sensor* to_sensor(const struct i2c_client *client)
1390 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1393 static int sensor_task_lock(struct i2c_client *client, int lock)
1395 #if CONFIG_SENSOR_I2C_NOSCHED
1397 struct sensor *sensor = to_sensor(client);
1400 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1401 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1402 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1406 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1407 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1408 goto sensor_task_lock_err;
1413 atomic_add(1, &sensor->tasklock_cnt);
1415 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1416 atomic_sub(1, &sensor->tasklock_cnt);
1418 if (atomic_read(&sensor->tasklock_cnt) == 0)
1423 sensor_task_lock_err:
1431 /* sensor register write */
1432 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1436 struct i2c_msg msg[1];
1439 buf[1] = reg & 0xFF;
1442 msg->addr = client->addr;
1443 msg->flags = client->flags;
1445 msg->len = sizeof(buf);
1446 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1447 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1452 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1453 err = i2c_transfer(client->adapter, msg, 1);
1458 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1466 /* sensor register read */
1467 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1471 struct i2c_msg msg[2];
1474 buf[1] = reg & 0xFF;
1476 msg[0].addr = client->addr;
1477 msg[0].flags = client->flags;
1479 msg[0].len = sizeof(buf);
1480 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1481 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1483 msg[1].addr = client->addr;
1484 msg[1].flags = client->flags|I2C_M_RD;
1487 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1488 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1492 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1493 err = i2c_transfer(client->adapter, msg, 2);
1499 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1507 /* write a array of registers */
1508 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1512 #if CONFIG_SENSOR_Focus
1513 struct sensor *sensor = to_sensor(client);
1515 #if CONFIG_SENSOR_I2C_RDWRCHK
1520 if (sensor_task_lock(client, 1) < 0)
1521 goto sensor_write_array_end;
1522 while (regarray[i].reg != SEQUENCE_END)
1524 #if CONFIG_SENSOR_Focus
1525 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1526 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1528 goto sensor_write_array_end;
1532 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1536 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1540 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1542 goto sensor_write_array_end;
1545 #if CONFIG_SENSOR_I2C_RDWRCHK
1546 sensor_read(client, regarray[i].reg, &valchk);
1547 if (valchk != regarray[i].val)
1548 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1555 #if CONFIG_SENSOR_Focus
1556 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1557 || (regarray == sensor_init_data)) {
1558 sensor->info_priv.affm_reinit = 1;
1562 sensor_write_array_end:
1563 sensor_task_lock(client,0);
1566 #if CONFIG_SENSOR_I2C_RDWRCHK
1567 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1575 while (regarray[i].reg != 0)
1577 sensor_read(client, regarray[i].reg, &valchk);
1578 if (valchk != regarray[i].val)
1579 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1586 #if CONFIG_SENSOR_Focus
1593 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1596 char read_tag=0xff,cnt;
1599 if (cmdinfo->validate_bit & 0x80) {
1600 if (sensor_write(client, CMD_TAG_Reg, cmdinfo->cmd_tag)) {
1601 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1602 goto sensor_af_cmdset_err;
1604 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1606 for (i=0; i<4; i++) {
1607 if (cmdinfo->validate_bit & (1<<i)) {
1608 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1609 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]);
1610 goto sensor_af_cmdset_err;
1612 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]);
1616 if (sensor_write(client, CMD_TAG_Reg, 0xff)) {
1617 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1618 goto sensor_af_cmdset_err;
1620 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1623 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1624 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1625 goto sensor_af_cmdset_err;
1632 if (sensor_read(client,CMD_TAG_Reg,&read_tag)){
1633 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1636 } while((read_tag != 0x00)&& (cnt++<100));
1638 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1640 sensor_af_cmdset_err:
1644 static int sensor_af_idlechk(struct i2c_client *client)
1652 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1654 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1656 goto sensor_af_idlechk_end;
1659 if (state != S_IDLE) {
1660 sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
1664 } while((state != S_IDLE)&& (cnt<100));
1666 ret = (state == S_IDLE) ? 0 : -1;
1668 sensor_af_idlechk_end:
1672 static int sensor_af_single(struct i2c_client *client)
1677 if (sensor_af_idlechk(client))
1678 goto sensor_af_single_end;
1680 if (sensor_af_cmdset(client, SingleFocus_Cmd, NULL)) {
1681 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1683 goto sensor_af_single_end;
1693 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1695 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1697 goto sensor_af_single_end;
1699 }while((state == S_FOCUSING) && (cnt<100));
1701 if (state != S_FOCUSED) {
1702 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1704 goto sensor_af_single_end;
1707 sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
1708 sensor_af_single_end:
1712 static int sensor_af_const(struct i2c_client *client)
1716 if (sensor_af_idlechk(client))
1717 goto sensor_af_const_end;
1719 if (sensor_af_cmdset(client, ConstFocus_Cmd, NULL)) {
1720 SENSOR_TR("%s const focus mode set error!\n",SENSOR_NAME_STRING());
1722 goto sensor_af_const_end;
1724 sensor_af_const_end:
1727 static int sensor_af_pause2capture(struct i2c_client *client)
1732 if (sensor_af_cmdset(client, PauseFocus_Cmd, NULL)) {
1733 SENSOR_TR("%s pause focus mode set error!\n",SENSOR_NAME_STRING());
1735 goto sensor_af_pause_end;
1745 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1747 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1749 goto sensor_af_pause_end;
1751 }while((state != S_CAPTURE) && (cnt<100));
1753 if (state != S_CAPTURE) {
1754 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1756 goto sensor_af_pause_end;
1758 sensor_af_pause_end:
1761 static int sensor_af_zoneupdate(struct i2c_client *client)
1765 if (sensor_af_idlechk(client))
1766 goto sensor_af_zoneupdate_end;
1768 if (sensor_af_cmdset(client, UpdateZone_Cmd, NULL)) {
1769 SENSOR_TR("%s update zone fail!\n",SENSOR_NAME_STRING());
1771 goto sensor_af_zoneupdate_end;
1774 sensor_af_zoneupdate_end:
1777 static int sensor_af_init(struct i2c_client *client)
1782 ret = sensor_write_array(client, sensor_af_firmware);
1784 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1786 goto sensor_af_init_end;
1796 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1798 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1800 goto sensor_af_init_end;
1802 }while((state == S_STARTUP) && (cnt<100));
1804 if (state != S_IDLE) {
1805 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1807 goto sensor_af_init_end;
1811 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1815 static int sensor_af_wq_function(struct i2c_client *client)
1817 struct sensor *sensor = to_sensor(client);
1818 struct af_cmdinfo cmdinfo;
1819 int ret=0, focus_pos = 0xfe;
1821 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1823 mutex_lock(&sensor->wq_lock);
1824 if (sensor_af_init(client)) {
1825 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1828 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1830 switch (sensor->info_priv.auto_focus)
1832 case SENSOR_AF_MODE_INFINITY:
1836 case SENSOR_AF_MODE_MACRO:
1838 if (focus_pos != 0x00)
1841 sensor_af_idlechk(client);
1842 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
1843 cmdinfo.cmd_para[0] = focus_pos;
1844 cmdinfo.validate_bit = 0x81;
1845 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
1848 case SENSOR_AF_MODE_AUTO:
1850 ret = sensor_af_single(client);
1853 case SENSOR_AF_MODE_CONTINUOUS:
1855 ret = sensor_af_const(client);
1858 case SENSOR_AF_MODE_CLOSE:
1865 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1866 goto sensor_af_wq_function_end;
1870 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);
1873 sensor_af_wq_function_end:
1874 sensor->sensor_wk.state = sensor_work_ready;
1875 mutex_unlock(&sensor->wq_lock);
1878 static void sensor_af_workqueue(struct work_struct *work)
1880 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
1881 struct i2c_client *client = sensor_work->client;
1883 if (sensor_af_wq_function(client) < 0) {
1884 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
1888 static int sensor_parameter_record(struct i2c_client *client)
1890 u8 ret_l,ret_m,ret_h;
1892 struct sensor *sensor = to_sensor(client);
1894 sensor_write(client,0x3503,0x07); //stop AE/AG
1895 sensor_write(client,0x3406,0x01); //stop AWB
1897 sensor_read(client,0x3500,&ret_h);
1898 sensor_read(client,0x3501, &ret_m);
1899 sensor_read(client,0x3502, &ret_l);
1903 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
1904 sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
1905 sensor_read(client,0x350c, &ret_h);
1906 sensor_read(client,0x350d, &ret_l);
1907 sensor->parameter.preview_line_width = ret_h & 0xff;
1908 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
1909 //Read back AGC Gain for preview
1910 sensor_read(client,0x350b, &tp_l);
1911 sensor->parameter.preview_gain = tp_l;
1913 sensor->parameter.capture_framerate = 900;
1914 sensor->parameter.preview_framerate = 1500;
1916 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
1917 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
1918 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
1919 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
1920 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
1921 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
1923 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);
1926 static int sensor_ae_transfer(struct i2c_client *client)
1931 u16 ulCapture_Exposure;
1932 u32 ulCapture_Exposure_Gain;
1936 u8 reg_l = 0,reg_h =0;
1937 u16 Preview_Maxlines;
1939 u32 Capture_MaxLines;
1940 struct sensor *sensor = to_sensor(client);
1942 Preview_Maxlines = sensor->parameter.preview_line_width;
1943 Gain = sensor->parameter.preview_gain;
1944 sensor_read(client,0x350c, ®_h);
1945 sensor_read(client,0x350d, ®_l);
1946 Capture_MaxLines = reg_h & 0xff;
1947 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
1950 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
1952 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
1955 if(Preview_Maxlines == 0)
1956 Preview_Maxlines = 1;
1958 ulCapture_Exposure =
1959 (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
1960 iCapture_Gain = (Gain & 0x0f) + 16;
1962 iCapture_Gain = iCapture_Gain << 1;
1965 iCapture_Gain = iCapture_Gain << 1;
1968 iCapture_Gain = iCapture_Gain << 1;
1971 iCapture_Gain = iCapture_Gain << 1;
1973 ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
1974 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
1975 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
1976 if (ulCapture_Exposure > Lines_10ms)
1978 //ulCapture_Exposure *= 1.7;
1979 ulCapture_Exposure /= Lines_10ms;
1980 ulCapture_Exposure *= Lines_10ms;
1983 ulCapture_Exposure = Capture_MaxLines;
1984 //ulCapture_Exposure_Gain *= 1.5;
1986 if(ulCapture_Exposure == 0)
1987 ulCapture_Exposure = 1;
1988 iCapture_Gain = (ulCapture_Exposure_Gain*2/ulCapture_Exposure + 1)/2;
1989 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
1990 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
1991 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
1994 if (iCapture_Gain > 31) {
1996 iCapture_Gain = iCapture_Gain >> 1;
1998 if (iCapture_Gain > 31) {
2000 iCapture_Gain = iCapture_Gain >> 1;
2002 if (iCapture_Gain > 31) {
2004 iCapture_Gain = iCapture_Gain >> 1;
2006 if (iCapture_Gain > 31) {
2008 iCapture_Gain = iCapture_Gain >> 1;
2010 if (iCapture_Gain > 16)
2011 Gain |= ((iCapture_Gain -16) & 0x0f);
2014 // write the gain and exposure to 0x350* registers
2015 //m_iWrite0x350b=Gain;
2016 sensor_write(client,0x350b, Gain);
2017 //m_iWrite0x3502=ExposureLow;
2018 sensor_write(client,0x3502, ExposureLow);
2019 //m_iWrite0x3501=ExposureMid;
2020 sensor_write(client,0x3501, ExposureMid);
2021 //m_iWrite0x3500=ExposureHigh;
2022 sensor_write(client,0x3500, ExposureHigh);
2023 // SendToFile("Gain = 0x%x\r\n", Gain);
2024 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2025 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2026 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2027 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2028 //camera_timed_wait(200);
2029 //linzhk camera_timed_wait(500);
2031 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2032 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2033 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2034 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2035 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2036 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2038 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2042 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2044 struct soc_camera_link *icl = to_soc_camera_link(icd);
2047 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2051 case Sensor_PowerDown:
2053 if (icl->powerdown) {
2054 ret = icl->powerdown(icd->pdev, on);
2055 if (ret == RK29_CAM_IO_SUCCESS) {
2059 icl->reset(icd->pdev);
2061 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2063 goto sensor_power_end;
2070 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2071 struct sensor *sensor = to_sensor(client);
2073 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2074 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2080 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2088 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2090 struct i2c_client *client = sd->priv;
2091 struct soc_camera_device *icd = client->dev.platform_data;
2092 struct sensor *sensor = to_sensor(client);
2093 const struct v4l2_queryctrl *qctrl;
2097 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2099 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2101 goto sensor_INIT_ERR;
2105 if (sensor_task_lock(client,1)<0)
2106 goto sensor_INIT_ERR;
2107 ret = sensor_write(client, 0x3008, 0x80);
2109 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2111 goto sensor_INIT_ERR;
2114 mdelay(5); //delay 5 microseconds
2115 /* check if it is an sensor sensor */
2116 ret = sensor_read(client, 0x300a, &value);
2118 SENSOR_TR("read chip id high byte failed\n");
2120 goto sensor_INIT_ERR;
2123 pid |= (value << 8);
2125 ret = sensor_read(client, 0x300b, &value);
2127 SENSOR_TR("read chip id low byte failed\n");
2129 goto sensor_INIT_ERR;
2132 pid |= (value & 0xff);
2133 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2135 if (pid == SENSOR_ID) {
2136 sensor->model = SENSOR_V4L2_IDENT;
2138 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2140 goto sensor_INIT_ERR;
2143 ret = sensor_write_array(client, sensor_init_data);
2146 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2147 goto sensor_INIT_ERR;
2149 sensor_task_lock(client,0);
2150 //icd->user_width = SENSOR_INIT_WIDTH;
2151 //icd->user_height = SENSOR_INIT_HEIGHT;
2152 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2153 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
2155 /* sensor sensor information for initialization */
2156 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2158 sensor->info_priv.whiteBalance = qctrl->default_value;
2159 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2161 sensor->info_priv.brightness = qctrl->default_value;
2162 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2164 sensor->info_priv.effect = qctrl->default_value;
2165 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2167 sensor->info_priv.exposure = qctrl->default_value;
2169 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2171 sensor->info_priv.saturation = qctrl->default_value;
2172 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2174 sensor->info_priv.contrast = qctrl->default_value;
2175 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2177 sensor->info_priv.mirror = qctrl->default_value;
2178 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2180 sensor->info_priv.flip = qctrl->default_value;
2181 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2183 sensor->info_priv.scene = qctrl->default_value;
2184 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2186 sensor->info_priv.digitalzoom = qctrl->default_value;
2188 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2189 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2191 sensor->info_priv.focus = qctrl->default_value;
2193 #if CONFIG_SENSOR_Flash
2194 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2196 sensor->info_priv.flash = qctrl->default_value;
2198 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);
2202 sensor_task_lock(client,0);
2203 sensor_deactivate(client);
2206 static int sensor_deactivate(struct i2c_client *client)
2208 struct soc_camera_device *icd = client->dev.platform_data;
2210 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2212 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2213 sensor_task_lock(client, 1);
2214 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2215 sensor_write(client, 0x3018, 0x03); // D5-D0
2216 sensor_write(client,0x3019,0x00); // STROBE,SDA
2217 sensor_task_lock(client, 0);
2218 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2219 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2220 icd->user_width = SENSOR_INIT_WIDTH;
2221 icd->user_height = SENSOR_INIT_HEIGHT;
2225 static struct reginfo sensor_power_down_sequence[]=
2229 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2232 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2234 if (pm_msg.event == PM_EVENT_SUSPEND) {
2235 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2236 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2238 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2241 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2243 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2248 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2255 static int sensor_resume(struct soc_camera_device *icd)
2259 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2261 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2265 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2269 static int sensor_set_bus_param(struct soc_camera_device *icd,
2270 unsigned long flags)
2276 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2278 struct soc_camera_link *icl = to_soc_camera_link(icd);
2279 unsigned long flags = SENSOR_BUS_PARAM;
2281 return soc_camera_apply_sensor_flags(icl, flags);
2284 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2286 struct i2c_client *client = sd->priv;
2287 struct soc_camera_device *icd = client->dev.platform_data;
2288 struct sensor *sensor = to_sensor(client);
2289 struct v4l2_pix_format *pix = &f->fmt.pix;
2291 pix->width = icd->user_width;
2292 pix->height = icd->user_height;
2293 pix->pixelformat = sensor->info_priv.pixfmt;
2294 pix->field = V4L2_FIELD_NONE;
2295 pix->colorspace = V4L2_COLORSPACE_JPEG;
2299 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
2303 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
2305 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
2307 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
2309 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
2311 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
2316 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2319 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
2323 if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
2325 } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
2330 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2333 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2335 struct i2c_client *client = sd->priv;
2336 struct sensor *sensor = to_sensor(client);
2337 struct v4l2_pix_format *pix = &f->fmt.pix;
2338 const struct v4l2_queryctrl *qctrl;
2339 struct soc_camera_device *icd = client->dev.platform_data;
2340 struct reginfo *winseqe_set_addr=NULL;
2341 int ret = 0, set_w,set_h;
2343 if (sensor->info_priv.pixfmt != pix->pixelformat) {
2344 switch (pix->pixelformat)
2346 case V4L2_PIX_FMT_YUYV:
2348 winseqe_set_addr = sensor_ClrFmt_YUYV;
2351 case V4L2_PIX_FMT_UYVY:
2353 winseqe_set_addr = sensor_ClrFmt_UYVY;
2359 if (winseqe_set_addr != NULL) {
2360 sensor_write_array(client, winseqe_set_addr);
2361 sensor->info_priv.pixfmt = pix->pixelformat;
2363 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2365 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2370 set_h = pix->height;
2372 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2374 winseqe_set_addr = sensor_qcif;
2378 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2380 winseqe_set_addr = sensor_qvga;
2384 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2386 winseqe_set_addr = sensor_cif;
2390 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2392 winseqe_set_addr = sensor_vga;
2396 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2398 winseqe_set_addr = sensor_svga;
2402 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2404 winseqe_set_addr = sensor_xga;
2408 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2410 winseqe_set_addr = sensor_720p;
2414 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2416 winseqe_set_addr = sensor_sxga;
2420 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2422 winseqe_set_addr = sensor_uxga;
2426 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2428 winseqe_set_addr = sensor_1080p;
2432 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2434 winseqe_set_addr = sensor_qxga;
2438 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2440 winseqe_set_addr = sensor_qsxga;
2446 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2447 set_w = SENSOR_INIT_WIDTH;
2448 set_h = SENSOR_INIT_HEIGHT;
2450 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
2453 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2455 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
2456 sensor_parameter_record(client);
2457 #if CONFIG_SENSOR_Focus
2458 sensor_af_idlechk(client);
2459 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2460 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2462 #if CONFIG_SENSOR_Flash
2463 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2464 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2465 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2468 }else { /* ddl@rock-chips.com : Video */
2469 #if CONFIG_SENSOR_Flash
2470 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2471 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2472 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2477 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2478 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2479 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2480 ret |= sensor_write_array(client,sensor_init_data);
2481 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2485 ret |= sensor_write_array(client, winseqe_set_addr);
2487 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2488 #if CONFIG_SENSOR_Flash
2489 if (sensor_fmt_capturechk(sd,f) == true) {
2490 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2491 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2492 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2496 goto sensor_s_fmt_end;
2499 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2501 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
2502 sensor_ae_transfer(client);
2503 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2504 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2505 if (sensor->info_priv.whiteBalance != 0) {
2506 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2507 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2509 sensor->info_priv.snap2preview = true;
2510 } else if (sensor_fmt_videochk(sd,f) == true) { /* ddl@rock-chips.com : Video */
2511 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2512 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2514 sensor->info_priv.video2preview = true;
2515 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2516 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2517 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2518 if (sensor->info_priv.snap2preview == true) {
2519 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2520 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2522 sensor->info_priv.video2preview = false;
2523 sensor->info_priv.snap2preview = false;
2525 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2529 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2533 pix->height = set_h;
2538 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2540 struct v4l2_pix_format *pix = &f->fmt.pix;
2541 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
2542 pix->pixelformat == V4L2_PIX_FMT_YUYV;
2545 * With Bayer format enforce even side lengths, but let the user play
2546 * with the starting pixel
2549 if (pix->height > SENSOR_MAX_HEIGHT)
2550 pix->height = SENSOR_MAX_HEIGHT;
2551 else if (pix->height < SENSOR_MIN_HEIGHT)
2552 pix->height = SENSOR_MIN_HEIGHT;
2554 pix->height = ALIGN(pix->height, 2);
2556 if (pix->width > SENSOR_MAX_WIDTH)
2557 pix->width = SENSOR_MAX_WIDTH;
2558 else if (pix->width < SENSOR_MIN_WIDTH)
2559 pix->width = SENSOR_MIN_WIDTH;
2561 pix->width = ALIGN(pix->width, 2);
2566 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2568 struct i2c_client *client = sd->priv;
2570 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2573 if (id->match.addr != client->addr)
2576 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2581 #if CONFIG_SENSOR_Brightness
2582 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2584 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2586 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2588 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2590 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2592 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2595 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2599 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2603 #if CONFIG_SENSOR_Effect
2604 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2606 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2608 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2610 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2612 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2614 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2617 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2621 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2625 #if CONFIG_SENSOR_Exposure
2626 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2628 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2630 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2632 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2634 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2636 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2639 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2643 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2647 #if CONFIG_SENSOR_Saturation
2648 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2650 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2652 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2654 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2656 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2658 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2661 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2665 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2669 #if CONFIG_SENSOR_Contrast
2670 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2672 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2674 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2676 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2678 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2680 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2683 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2687 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2691 #if CONFIG_SENSOR_Mirror
2692 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2694 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2696 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2698 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2700 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2702 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2705 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2709 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2713 #if CONFIG_SENSOR_Flip
2714 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2716 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2718 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2720 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2722 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2724 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2727 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2731 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2735 #if CONFIG_SENSOR_Scene
2736 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2738 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2740 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2742 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2744 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2746 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2749 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2753 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2757 #if CONFIG_SENSOR_WhiteBalance
2758 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2760 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2762 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2764 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2766 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2768 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2771 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2775 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2779 #if CONFIG_SENSOR_DigitalZoom
2780 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2782 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2783 struct sensor *sensor = to_sensor(client);
2784 const struct v4l2_queryctrl *qctrl_info;
2785 int digitalzoom_cur, digitalzoom_total;
2787 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2791 digitalzoom_cur = sensor->info_priv.digitalzoom;
2792 digitalzoom_total = qctrl_info->maximum;
2794 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2796 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2800 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2802 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2806 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2808 *value = digitalzoom_total - digitalzoom_cur;
2811 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2813 *value = 0 - digitalzoom_cur;
2816 digitalzoom_cur += *value;
2818 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2820 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2822 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2825 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2832 #if CONFIG_SENSOR_Focus
2833 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2835 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2836 struct sensor *sensor = to_sensor(client);
2837 const struct v4l2_queryctrl *qctrl_info;
2838 struct af_cmdinfo cmdinfo;
2841 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2845 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2846 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2848 if (sensor_af_idlechk(client))
2849 goto sensor_set_focus_absolute_end;
2851 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2852 cmdinfo.cmd_para[0] = value;
2853 cmdinfo.validate_bit = 0x81;
2854 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
2855 //ret |= sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
2856 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2859 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2863 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2864 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2867 sensor_set_focus_absolute_end:
2870 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2872 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2873 struct sensor *sensor = to_sensor(client);
2874 const struct v4l2_queryctrl *qctrl_info;
2875 struct af_cmdinfo cmdinfo;
2878 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
2882 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2883 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2885 if (sensor_af_idlechk(client))
2886 goto sensor_set_focus_relative_end;
2889 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2890 } else if (value < 0) {
2891 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2893 cmdinfo.validate_bit = 0x80;
2894 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
2896 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2899 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2903 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2904 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2906 sensor_set_focus_relative_end:
2910 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2912 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2913 struct sensor *sensor = to_sensor(client);
2916 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2919 case SENSOR_AF_MODE_AUTO:
2921 ret = sensor_af_single(client);
2925 case SENSOR_AF_MODE_MACRO:
2927 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
2931 case SENSOR_AF_MODE_INFINITY:
2933 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
2937 case SENSOR_AF_MODE_CONTINUOUS:
2939 ret = sensor_af_const(client);
2943 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2948 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2951 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2952 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2958 #if CONFIG_SENSOR_Flash
2959 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2961 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2962 if (value == 3) { /* ddl@rock-chips.com: torch */
2963 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2965 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2967 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2971 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2975 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2977 struct i2c_client *client = sd->priv;
2978 struct sensor *sensor = to_sensor(client);
2979 const struct v4l2_queryctrl *qctrl;
2981 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2985 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2991 case V4L2_CID_BRIGHTNESS:
2993 ctrl->value = sensor->info_priv.brightness;
2996 case V4L2_CID_SATURATION:
2998 ctrl->value = sensor->info_priv.saturation;
3001 case V4L2_CID_CONTRAST:
3003 ctrl->value = sensor->info_priv.contrast;
3006 case V4L2_CID_DO_WHITE_BALANCE:
3008 ctrl->value = sensor->info_priv.whiteBalance;
3011 case V4L2_CID_EXPOSURE:
3013 ctrl->value = sensor->info_priv.exposure;
3016 case V4L2_CID_HFLIP:
3018 ctrl->value = sensor->info_priv.mirror;
3021 case V4L2_CID_VFLIP:
3023 ctrl->value = sensor->info_priv.flip;
3034 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3036 struct i2c_client *client = sd->priv;
3037 struct sensor *sensor = to_sensor(client);
3038 struct soc_camera_device *icd = client->dev.platform_data;
3039 const struct v4l2_queryctrl *qctrl;
3042 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3046 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3052 #if CONFIG_SENSOR_Brightness
3053 case V4L2_CID_BRIGHTNESS:
3055 if (ctrl->value != sensor->info_priv.brightness)
3057 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3061 sensor->info_priv.brightness = ctrl->value;
3066 #if CONFIG_SENSOR_Exposure
3067 case V4L2_CID_EXPOSURE:
3069 if (ctrl->value != sensor->info_priv.exposure)
3071 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3075 sensor->info_priv.exposure = ctrl->value;
3080 #if CONFIG_SENSOR_Saturation
3081 case V4L2_CID_SATURATION:
3083 if (ctrl->value != sensor->info_priv.saturation)
3085 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3089 sensor->info_priv.saturation = ctrl->value;
3094 #if CONFIG_SENSOR_Contrast
3095 case V4L2_CID_CONTRAST:
3097 if (ctrl->value != sensor->info_priv.contrast)
3099 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3103 sensor->info_priv.contrast = ctrl->value;
3108 #if CONFIG_SENSOR_WhiteBalance
3109 case V4L2_CID_DO_WHITE_BALANCE:
3111 if (ctrl->value != sensor->info_priv.whiteBalance)
3113 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3117 sensor->info_priv.whiteBalance = ctrl->value;
3122 #if CONFIG_SENSOR_Mirror
3123 case V4L2_CID_HFLIP:
3125 if (ctrl->value != sensor->info_priv.mirror)
3127 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3129 sensor->info_priv.mirror = ctrl->value;
3134 #if CONFIG_SENSOR_Flip
3135 case V4L2_CID_VFLIP:
3137 if (ctrl->value != sensor->info_priv.flip)
3139 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3141 sensor->info_priv.flip = ctrl->value;
3152 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3154 const struct v4l2_queryctrl *qctrl;
3155 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3156 struct sensor *sensor = to_sensor(client);
3158 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3162 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3166 switch (ext_ctrl->id)
3168 case V4L2_CID_SCENE:
3170 ext_ctrl->value = sensor->info_priv.scene;
3173 case V4L2_CID_EFFECT:
3175 ext_ctrl->value = sensor->info_priv.effect;
3178 case V4L2_CID_ZOOM_ABSOLUTE:
3180 ext_ctrl->value = sensor->info_priv.digitalzoom;
3183 case V4L2_CID_ZOOM_RELATIVE:
3187 case V4L2_CID_FOCUS_ABSOLUTE:
3191 case V4L2_CID_FOCUS_RELATIVE:
3195 case V4L2_CID_FLASH:
3197 ext_ctrl->value = sensor->info_priv.flash;
3205 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3207 const struct v4l2_queryctrl *qctrl;
3208 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3209 struct sensor *sensor = to_sensor(client);
3212 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3216 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3221 switch (ext_ctrl->id)
3223 #if CONFIG_SENSOR_Scene
3224 case V4L2_CID_SCENE:
3226 if (ext_ctrl->value != sensor->info_priv.scene)
3228 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3230 sensor->info_priv.scene = ext_ctrl->value;
3235 #if CONFIG_SENSOR_Effect
3236 case V4L2_CID_EFFECT:
3238 if (ext_ctrl->value != sensor->info_priv.effect)
3240 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3242 sensor->info_priv.effect= ext_ctrl->value;
3247 #if CONFIG_SENSOR_DigitalZoom
3248 case V4L2_CID_ZOOM_ABSOLUTE:
3250 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3253 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3255 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3257 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3259 sensor->info_priv.digitalzoom += val_offset;
3261 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3266 case V4L2_CID_ZOOM_RELATIVE:
3268 if (ext_ctrl->value)
3270 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3272 sensor->info_priv.digitalzoom += ext_ctrl->value;
3274 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3279 #if CONFIG_SENSOR_Focus
3280 case V4L2_CID_FOCUS_ABSOLUTE:
3282 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3285 if (sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value) == 0) {
3286 if (ext_ctrl->value == qctrl->minimum) {
3287 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
3288 } else if (ext_ctrl->value == qctrl->maximum) {
3289 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
3291 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
3297 case V4L2_CID_FOCUS_RELATIVE:
3299 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3302 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3305 case V4L2_CID_FOCUS_AUTO:
3307 if (ext_ctrl->value == 1) {
3308 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
3310 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3311 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3312 if (ext_ctrl->value == 0)
3313 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3317 case V4L2_CID_FOCUS_CONTINUOUS:
3319 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3320 if (ext_ctrl->value == 1) {
3321 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3323 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3326 if (ext_ctrl->value == 0)
3327 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3332 #if CONFIG_SENSOR_Flash
3333 case V4L2_CID_FLASH:
3335 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3337 sensor->info_priv.flash = ext_ctrl->value;
3339 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3350 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3352 struct i2c_client *client = sd->priv;
3353 struct soc_camera_device *icd = client->dev.platform_data;
3354 int i, error_cnt=0, error_idx=-1;
3357 for (i=0; i<ext_ctrl->count; i++) {
3358 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3365 error_idx = ext_ctrl->count;
3367 if (error_idx != -1) {
3368 ext_ctrl->error_idx = error_idx;
3375 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3377 struct i2c_client *client = sd->priv;
3378 struct soc_camera_device *icd = client->dev.platform_data;
3379 int i, error_cnt=0, error_idx=-1;
3381 for (i=0; i<ext_ctrl->count; i++) {
3382 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3389 error_idx = ext_ctrl->count;
3391 if (error_idx != -1) {
3392 ext_ctrl->error_idx = error_idx;
3399 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3401 struct i2c_client *client = sd->priv;
3402 struct sensor *sensor = to_sensor(client);
3403 #if CONFIG_SENSOR_Focus
3404 struct soc_camera_device *icd = client->dev.platform_data;
3405 struct v4l2_format fmt;
3409 sensor->info_priv.enable = 1;
3410 #if CONFIG_SENSOR_Focus
3411 fmt.fmt.pix.width = icd->user_width;
3412 fmt.fmt.pix.height = icd->user_height;
3413 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3414 if (sensor_fmt_capturechk(sd, &fmt) == false) {
3415 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3416 if (sensor->sensor_wq != NULL) {
3417 mutex_lock(&sensor->wq_lock);
3418 if (sensor->sensor_wk.state == sensor_working) {
3419 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
3420 mutex_unlock(&sensor->wq_lock);
3421 goto sensor_s_stream_end;
3423 sensor->sensor_wk.state = sensor_working;
3424 mutex_unlock(&sensor->wq_lock);
3425 sensor->sensor_wk.client = client;
3426 INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
3427 queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
3429 sensor->info_priv.affm_reinit = 0;
3433 } else if (enable == 0) {
3434 sensor->info_priv.enable = 0;
3435 #if CONFIG_SENSOR_Focus
3436 flush_work(&(sensor->sensor_wk.dwork.work));
3437 mutex_lock(&sensor->wq_lock);
3438 sensor->sensor_wk.state = sensor_work_ready;
3439 mutex_unlock(&sensor->wq_lock);
3443 sensor_s_stream_end:
3447 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3448 * this wasn't our capture interface, so, we wait for the right one */
3449 static int sensor_video_probe(struct soc_camera_device *icd,
3450 struct i2c_client *client)
3454 struct sensor *sensor = to_sensor(client);
3456 /* We must have a parent by now. And it cannot be a wrong one.
3457 * So this entire test is completely redundant. */
3458 if (!icd->dev.parent ||
3459 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3462 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3464 goto sensor_video_probe_err;
3467 ret = sensor_write(client, 0x3012, 0x80);
3469 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3471 goto sensor_video_probe_err;
3473 mdelay(5); //delay 5 microseconds
3475 /* check if it is an sensor sensor */
3476 ret = sensor_read(client, 0x300a, &value);
3478 SENSOR_TR("read chip id high byte failed\n");
3480 goto sensor_video_probe_err;
3483 pid |= (value << 8);
3485 ret = sensor_read(client, 0x300b, &value);
3487 SENSOR_TR("read chip id low byte failed\n");
3489 goto sensor_video_probe_err;
3492 pid |= (value & 0xff);
3493 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3494 if (pid == SENSOR_ID) {
3495 sensor->model = SENSOR_V4L2_IDENT;
3497 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3499 goto sensor_video_probe_err;
3502 icd->formats = sensor_colour_formats;
3503 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
3507 sensor_video_probe_err:
3510 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3512 struct i2c_client *client = sd->priv;
3513 struct soc_camera_device *icd = client->dev.platform_data;
3514 struct sensor *sensor = to_sensor(client);
3517 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3520 case RK29_CAM_SUBDEV_DEACTIVATE:
3522 sensor_deactivate(client);
3525 case RK29_CAM_SUBDEV_IOREQUEST:
3527 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3528 if (sensor->sensor_io_request != NULL) {
3529 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
3530 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
3531 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
3532 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
3533 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
3534 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
3537 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3539 goto sensor_ioctl_end;
3541 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3543 #if CONFIG_SENSOR_Flash
3544 if (sensor->sensor_gpio_res) {
3545 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3546 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3547 for (i = 0; i < icd->ops->num_controls; i++) {
3548 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3549 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3552 sensor->info_priv.flash = 0xff;
3553 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3561 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3571 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3572 .init = sensor_init,
3573 .g_ctrl = sensor_g_control,
3574 .s_ctrl = sensor_s_control,
3575 .g_ext_ctrls = sensor_g_ext_controls,
3576 .s_ext_ctrls = sensor_s_ext_controls,
3577 .g_chip_ident = sensor_g_chip_ident,
3578 .ioctl = sensor_ioctl,
3581 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3582 .s_fmt = sensor_s_fmt,
3583 .g_fmt = sensor_g_fmt,
3584 .try_fmt = sensor_try_fmt,
3585 .s_stream = sensor_s_stream,
3588 static struct v4l2_subdev_ops sensor_subdev_ops = {
3589 .core = &sensor_subdev_core_ops,
3590 .video = &sensor_subdev_video_ops,
3593 static int sensor_probe(struct i2c_client *client,
3594 const struct i2c_device_id *did)
3596 struct sensor *sensor;
3597 struct soc_camera_device *icd = client->dev.platform_data;
3598 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3599 struct soc_camera_link *icl;
3602 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3604 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3608 icl = to_soc_camera_link(icd);
3610 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3614 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3615 dev_warn(&adapter->dev,
3616 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3620 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3624 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3626 /* Second stage probe - when a capture adapter is there */
3627 icd->ops = &sensor_ops;
3628 icd->y_skip_top = 0;
3629 #if CONFIG_SENSOR_I2C_NOSCHED
3630 atomic_set(&sensor->tasklock_cnt,0);
3633 ret = sensor_video_probe(icd, client);
3636 i2c_set_clientdata(client, NULL);
3640 #if CONFIG_SENSOR_Focus
3641 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
3642 if (sensor->sensor_wq == NULL)
3643 SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
3644 mutex_init(&sensor->wq_lock);
3645 sensor->sensor_wk.state = sensor_work_ready;
3649 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3653 static int sensor_remove(struct i2c_client *client)
3655 struct sensor *sensor = to_sensor(client);
3656 struct soc_camera_device *icd = client->dev.platform_data;
3658 #if CONFIG_SENSOR_Focus
3659 if (sensor->sensor_wq) {
3660 destroy_workqueue(sensor->sensor_wq);
3661 sensor->sensor_wq = NULL;
3666 i2c_set_clientdata(client, NULL);
3667 client->driver = NULL;
3673 static const struct i2c_device_id sensor_id[] = {
3674 {SENSOR_NAME_STRING(), 0 },
3677 MODULE_DEVICE_TABLE(i2c, sensor_id);
3679 static struct i2c_driver sensor_i2c_driver = {
3681 .name = SENSOR_NAME_STRING(),
3683 .probe = sensor_probe,
3684 .remove = sensor_remove,
3685 .id_table = sensor_id,
3688 static int __init sensor_mod_init(void)
3690 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3691 return i2c_add_driver(&sensor_i2c_driver);
3694 static void __exit sensor_mod_exit(void)
3696 i2c_del_driver(&sensor_i2c_driver);
3699 device_initcall_sync(sensor_mod_init);
3700 module_exit(sensor_mod_exit);
3702 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3703 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3704 MODULE_LICENSE("GPL");