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);
1346 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1347 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1349 static struct soc_camera_ops sensor_ops =
1351 .suspend = sensor_suspend,
1352 .resume = sensor_resume,
1353 .set_bus_param = sensor_set_bus_param,
1354 .query_bus_param = sensor_query_bus_param,
1355 .controls = sensor_controls,
1356 .menus = sensor_menus,
1357 .num_controls = ARRAY_SIZE(sensor_controls),
1358 .num_menus = ARRAY_SIZE(sensor_menus),
1360 /* only one fixed colorspace per pixelcode */
1361 struct sensor_datafmt {
1362 enum v4l2_mbus_pixelcode code;
1363 enum v4l2_colorspace colorspace;
1366 /* Find a data format by a pixel code in an array */
1367 static const struct sensor_datafmt *sensor_find_datafmt(
1368 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1372 for (i = 0; i < n; i++)
1373 if (fmt[i].code == code)
1379 static const struct sensor_datafmt sensor_colour_fmts[] = {
1380 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1381 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1387 WqCmd_af_special_pos,
1391 WqCmd_af_return_idle,
1393 enum sensor_wq_result
1401 struct i2c_client *client;
1402 struct delayed_work dwork;
1403 enum sensor_wq_cmd cmd;
1404 wait_queue_head_t done;
1405 enum sensor_wq_result result;
1410 typedef struct sensor_info_priv_s
1426 unsigned char mirror; /* HFLIP */
1427 unsigned char flip; /* VFLIP */
1428 struct reginfo *winseqe_cur_addr;
1429 struct sensor_datafmt fmt;
1430 unsigned int enable;
1431 unsigned int funmodule_state;
1432 } sensor_info_priv_t;
1436 struct sensor_parameter
1438 unsigned short int preview_maxlines;
1439 unsigned short int preview_exposure;
1440 unsigned short int preview_line_width;
1441 unsigned short int preview_gain;
1443 unsigned short int capture_framerate;
1444 unsigned short int preview_framerate;
1450 struct v4l2_subdev subdev;
1451 struct i2c_client *client;
1452 sensor_info_priv_t info_priv;
1453 struct sensor_parameter parameter;
1454 struct workqueue_struct *sensor_wq;
1455 struct mutex wq_lock;
1456 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1457 #if CONFIG_SENSOR_I2C_NOSCHED
1458 atomic_t tasklock_cnt;
1460 struct rk29camera_platform_data *sensor_io_request;
1461 struct rk29camera_gpio_res *sensor_gpio_res;
1464 static struct sensor* to_sensor(const struct i2c_client *client)
1466 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1469 static int sensor_task_lock(struct i2c_client *client, int lock)
1471 #if CONFIG_SENSOR_I2C_NOSCHED
1473 struct sensor *sensor = to_sensor(client);
1476 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1477 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1478 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1482 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1483 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1484 goto sensor_task_lock_err;
1489 atomic_add(1, &sensor->tasklock_cnt);
1491 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1492 atomic_sub(1, &sensor->tasklock_cnt);
1494 if (atomic_read(&sensor->tasklock_cnt) == 0)
1499 sensor_task_lock_err:
1508 #if CONFIG_SENSOR_WRITE_REGS
1509 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
1512 struct i2c_msg msg[1];
1515 msg->addr = client->addr;
1516 msg->flags = client->flags;
1517 msg->buf = reg_info;
1518 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1519 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1525 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1526 err = i2c_transfer(client->adapter, msg, 1);
1530 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
1544 /* sensor register write */
1545 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1549 struct i2c_msg msg[1];
1552 buf[1] = reg & 0xFF;
1555 msg->addr = client->addr;
1556 msg->flags = client->flags;
1558 msg->len = sizeof(buf);
1559 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1560 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1565 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1566 err = i2c_transfer(client->adapter, msg, 1);
1571 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1579 /* sensor register read */
1580 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1584 struct i2c_msg msg[2];
1587 buf[1] = reg & 0xFF;
1589 msg[0].addr = client->addr;
1590 msg[0].flags = client->flags;
1592 msg[0].len = sizeof(buf);
1593 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1594 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1596 msg[1].addr = client->addr;
1597 msg[1].flags = client->flags|I2C_M_RD;
1600 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1601 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1605 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1606 err = i2c_transfer(client->adapter, msg, 2);
1612 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1620 /* write a array of registers */
1621 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1625 #if CONFIG_SENSOR_Focus
1626 struct sensor *sensor = to_sensor(client);
1628 #if CONFIG_SENSOR_I2C_RDWRCHK
1631 #if CONFIG_SENSOR_WRITE_REGS
1638 if (sensor_task_lock(client, 1) < 0)
1639 goto sensor_write_array_end;
1640 while (regarray[i].reg != SEQUENCE_END) {
1641 #if CONFIG_SENSOR_Focus
1642 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1643 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1645 goto sensor_write_array_end;
1649 #if CONFIG_SENSOR_WRITE_REGS
1653 reg_length = 0x0001;
1655 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
1658 if(reg_num >= WRITE_REGS_NUM)
1665 size_num = reg_num + 1;
1667 ptemp = phead = (u8*)kmalloc((size_num+10)*sizeof(u8),GFP_KERNEL);
1669 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1671 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1673 *phead = regarray[j].reg >> 8;
1674 *(ptemp+1) = regarray[j].reg & 0xFF;
1676 for( ; reg_num > 0; reg_num --, j++) {
1677 *ptemp ++ = regarray[j].val;
1681 err = sensor_write_regs(client, ptemp,size_num);
1685 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1688 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1693 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1697 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1699 goto sensor_write_array_end;
1702 #if CONFIG_SENSOR_I2C_RDWRCHK
1703 sensor_read(client, regarray[i].reg, &valchk);
1704 if (valchk != regarray[i].val)
1705 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1711 #if CONFIG_SENSOR_Focus
1712 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1713 || (regarray == sensor_init_data)) {
1714 sensor->info_priv.affm_reinit = 1;
1718 sensor_write_array_end:
1719 sensor_task_lock(client,0);
1722 #if CONFIG_SENSOR_I2C_RDWRCHK
1723 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1731 while (regarray[i].reg != 0)
1733 sensor_read(client, regarray[i].reg, &valchk);
1734 if (valchk != regarray[i].val)
1735 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1742 #if CONFIG_SENSOR_Focus
1749 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1752 char read_tag=0xff,cnt;
1755 if (cmdinfo->validate_bit & 0x80) {
1756 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1757 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1758 goto sensor_af_cmdset_err;
1760 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1762 for (i=0; i<4; i++) {
1763 if (cmdinfo->validate_bit & (1<<i)) {
1764 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1765 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]);
1766 goto sensor_af_cmdset_err;
1768 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]);
1772 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
1773 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1774 goto sensor_af_cmdset_err;
1776 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1779 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1780 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1781 goto sensor_af_cmdset_err;
1788 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1789 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1792 } while((read_tag != 0x00)&& (cnt++<100));
1794 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1796 sensor_af_cmdset_err:
1800 static int sensor_af_idlechk(struct i2c_client *client)
1804 struct af_cmdinfo cmdinfo;
1806 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1808 cmdinfo.cmd_tag = 0x01;
1809 cmdinfo.validate_bit = 0x80;
1810 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1812 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1814 goto sensor_af_idlechk_end;
1819 ret = sensor_read(client, CMD_ACK_Reg, &state);
1821 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1823 goto sensor_af_idlechk_end;
1825 }while(0x00 != state);
1828 sensor_af_idlechk_end:
1832 static int sensor_af_single(struct i2c_client *client)
1836 struct af_cmdinfo cmdinfo;
1838 cmdinfo.cmd_tag = 0x01;
1839 cmdinfo.validate_bit = 0x80;
1840 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
1842 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1844 goto sensor_af_single_end;
1854 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1856 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1858 goto sensor_af_single_end;
1860 }while((state == S_FOCUSING) && (cnt<100));
1862 if (state != S_FOCUSED) {
1863 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1865 goto sensor_af_single_end;
1868 sensor_af_single_end:
1872 static int sensor_af_const(struct i2c_client *client)
1881 static int sensor_af_init(struct i2c_client *client)
1886 ret = sensor_write_array(client, sensor_af_firmware);
1888 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1890 goto sensor_af_init_end;
1900 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1902 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1904 goto sensor_af_init_end;
1906 }while((state == S_STARTUP) && (cnt<100));
1908 if (state != S_IDLE) {
1909 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1911 goto sensor_af_init_end;
1915 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1919 static int sensor_af_downfirmware(struct i2c_client *client)
1921 struct sensor *sensor = to_sensor(client);
1922 struct af_cmdinfo cmdinfo;
1923 int ret=0, focus_pos = 0xfe;
1924 struct soc_camera_device *icd = client->dev.platform_data;
1925 struct v4l2_mbus_framefmt mf;
1927 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1929 if (sensor_af_init(client)) {
1930 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1933 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1935 mf.width = icd->user_width;
1936 mf.height = icd->user_height;
1937 mf.code = sensor->info_priv.fmt.code;
1938 mf.colorspace = sensor->info_priv.fmt.colorspace;
1939 mf.field = V4L2_FIELD_NONE;
1940 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
1941 ret = sensor_af_const(client);
1943 switch (sensor->info_priv.auto_focus)
1945 /*case SENSOR_AF_MODE_INFINITY:
1949 case SENSOR_AF_MODE_MACRO:
1951 if (focus_pos != 0x00)
1954 sensor_af_idlechk(client);
1955 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
1956 cmdinfo.cmd_para[0] = focus_pos;
1957 cmdinfo.validate_bit = 0x81;
1958 //ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
1961 case SENSOR_AF_MODE_AUTO:
1963 ret = sensor_af_single(client);
1966 /*case SENSOR_AF_MODE_CONTINUOUS:
1968 ret = sensor_af_const(client);
1971 case SENSOR_AF_MODE_CLOSE:
1978 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1979 goto sensor_af_downfirmware_end;
1983 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
1986 sensor_af_downfirmware_end:
1990 static void sensor_af_workqueue(struct work_struct *work)
1992 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
1993 struct i2c_client *client = sensor_work->client;
1994 struct sensor *sensor = to_sensor(client);
1995 struct af_cmdinfo cmdinfo;
1997 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
1999 mutex_lock(&sensor->wq_lock);
2000 if((sensor_work->cmd != WqCmd_af_init) && (sensor->info_priv.auto_focus != SENSOR_AF_MODE_AUTO)) {
2001 SENSOR_TR("auto focus status is wrong ,do nothing !");
2004 switch (sensor_work->cmd)
2008 if (sensor_af_downfirmware(client) < 0) {
2009 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2013 case WqCmd_af_single:
2015 if (sensor_af_single(client) < 0) {
2016 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2017 sensor_work->result = WqRet_fail;
2019 sensor_work->result = WqRet_success;
2023 case WqCmd_af_special_pos:
2025 sensor_af_idlechk(client);
2027 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2028 cmdinfo.cmd_para[0] = sensor_work->var;
2029 cmdinfo.validate_bit = 0x81;
2030 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2031 sensor_work->result = WqRet_fail;
2033 sensor_work->result = WqRet_success;
2036 case WqCmd_af_near_pos:
2038 sensor_af_idlechk(client);
2039 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2040 cmdinfo.validate_bit = 0x80;
2041 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2042 sensor_work->result = WqRet_fail;
2044 sensor_work->result = WqRet_success;
2047 case WqCmd_af_far_pos:
2049 sensor_af_idlechk(client);
2050 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2051 cmdinfo.validate_bit = 0x80;
2052 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2053 sensor_work->result = WqRet_fail;
2055 sensor_work->result = WqRet_success;
2058 case WqCmd_af_continues:
2060 if (sensor_af_const(client) < 0)
2061 sensor_work->result = WqRet_fail;
2063 sensor_work->result = WqRet_success;
2066 case WqCmd_af_return_idle:
2068 if (sensor_af_idlechk(client) < 0)
2069 sensor_work->result = WqRet_fail;
2071 sensor_work->result = WqRet_success;
2075 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
2079 if (sensor_work->wait == false) {
2080 kfree((void*)sensor_work);
2082 wake_up(&sensor_work->done);
2084 mutex_unlock(&sensor->wq_lock);
2088 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait)
2090 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2091 struct sensor *sensor = to_sensor(client);
2092 struct sensor_work *wk;
2095 if (sensor->sensor_wq == NULL) {
2097 goto sensor_af_workqueue_set_end;
2100 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
2102 wk->client = client;
2103 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
2105 wk->result = WqRet_inval;
2108 init_waitqueue_head(&wk->done);
2110 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
2112 /* ddl@rock-chips.com:
2113 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
2114 * As a result any other ioctl calls will proceed very, very slowly since each call
2115 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
2116 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
2119 mutex_unlock(&icd->video_lock);
2120 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(2500)) == 0) {
2121 SENSOR_TR("%s %s cmd(%d) is timeout!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2125 mutex_lock(&icd->video_lock);
2129 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2132 sensor_af_workqueue_set_end:
2136 static int sensor_parameter_record(struct i2c_client *client)
2138 u8 ret_l,ret_m,ret_h;
2140 struct sensor *sensor = to_sensor(client);
2142 sensor_write(client,0x3503,0x07); //stop AE/AG
2143 sensor_write(client,0x3406,0x01); //stop AWB
2145 sensor_read(client,0x3500,&ret_h);
2146 sensor_read(client,0x3501, &ret_m);
2147 sensor_read(client,0x3502, &ret_l);
2151 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
2152 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
2153 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
2155 sensor_read(client,0x350c, &ret_h);
2156 sensor_read(client,0x350d, &ret_l);
2157 sensor->parameter.preview_line_width = ret_h & 0xff;
2158 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
2159 //Read back AGC Gain for preview
2160 sensor_read(client,0x350a, &ret_h);
2161 sensor_read(client,0x350b, &tp_l);
2162 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
2164 sensor_read(client,0x380e, &ret_h);
2165 sensor->parameter.preview_maxlines = ret_h;
2166 sensor->parameter.preview_maxlines <<= 8;
2167 sensor_read(client,0x380f, &tp_l);
2168 sensor->parameter.preview_maxlines += tp_l;
2170 sensor->parameter.capture_framerate = 375;
2171 sensor->parameter.preview_framerate = 1500;
2173 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
2174 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2175 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2176 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2177 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2178 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2180 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);
2183 static int sensor_ae_transfer(struct i2c_client *client)
2188 u16 ulCapture_Exposure;
2189 u32 ulCapture_Exposure_Gain;
2193 u8 reg_l = 0,reg_h =0;
2194 u16 Preview_Maxlines;
2196 u32 Capture_MaxLines;
2197 struct sensor *sensor = to_sensor(client);
2199 //Preview_Maxlines = sensor->parameter.preview_line_width;
2200 Preview_Maxlines = sensor->parameter.preview_maxlines;
2201 Gain = sensor->parameter.preview_gain;
2203 sensor_read(client,0x350c, ®_h);
2204 sensor_read(client,0x350d, ®_l);
2205 Capture_MaxLines = reg_h & 0xff;
2206 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2209 sensor_read(client,0x380e, ®_h);
2210 Capture_MaxLines = reg_h;
2211 Capture_MaxLines <<= 8;
2212 sensor_read(client,0x380f, ®_l);
2213 Capture_MaxLines += reg_l;
2216 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2218 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2220 Lines_10ms = Lines_10ms & 0xffff;
2222 if(Preview_Maxlines == 0)
2223 Preview_Maxlines = 1;
2225 //ulCapture_Exposure =
2226 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2228 ulCapture_Exposure =
2229 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2230 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2232 iCapture_Gain = (Gain & 0x0f) + 16;
2234 iCapture_Gain = iCapture_Gain << 1;
2237 iCapture_Gain = iCapture_Gain << 1;
2240 iCapture_Gain = iCapture_Gain << 1;
2243 iCapture_Gain = iCapture_Gain << 1;
2246 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
2247 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2249 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2250 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2251 if (ulCapture_Exposure > Lines_10ms)
2253 //ulCapture_Exposure *= 1.7;
2254 ulCapture_Exposure /= Lines_10ms;
2255 ulCapture_Exposure *= Lines_10ms;
2258 ulCapture_Exposure = Capture_MaxLines;
2259 //ulCapture_Exposure_Gain *= 1.5;
2262 if(ulCapture_Exposure == 0)
2263 ulCapture_Exposure = 1;
2265 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2266 iCapture_Gain = iCapture_Gain & 0xffff;
2268 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2269 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2270 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2274 if (iCapture_Gain > 31) {
2276 iCapture_Gain = iCapture_Gain >> 1;
2278 if (iCapture_Gain > 31) {
2280 iCapture_Gain = iCapture_Gain >> 1;
2282 if (iCapture_Gain > 31) {
2284 iCapture_Gain = iCapture_Gain >> 1;
2286 if (iCapture_Gain > 31) {
2288 iCapture_Gain = iCapture_Gain >> 1;
2290 if (iCapture_Gain > 16)
2291 Gain |= ((iCapture_Gain -16) & 0x0f);
2294 // write the gain and exposure to 0x350* registers
2295 //m_iWrite0x350b=Gain;
2296 sensor_write(client,0x350b, Gain);
2297 Gain = (Gain >> 8) & 0x01;
2298 sensor_write(client,0x350a, Gain);
2299 //m_iWrite0x3502=ExposureLow;
2300 sensor_write(client,0x3502, ExposureLow);
2301 //m_iWrite0x3501=ExposureMid;
2302 sensor_write(client,0x3501, ExposureMid);
2303 //m_iWrite0x3500=ExposureHigh;
2304 sensor_write(client,0x3500, ExposureHigh);
2305 // SendToFile("Gain = 0x%x\r\n", Gain);
2306 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2307 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2308 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2309 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2310 //camera_timed_wait(200);
2311 //linzhk camera_timed_wait(500);
2313 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2314 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2315 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2316 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2317 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2318 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2320 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2324 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2326 struct soc_camera_link *icl = to_soc_camera_link(icd);
2329 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2333 case Sensor_PowerDown:
2335 if (icl->powerdown) {
2336 ret = icl->powerdown(icd->pdev, on);
2337 if (ret == RK29_CAM_IO_SUCCESS) {
2341 icl->reset(icd->pdev);
2343 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2345 goto sensor_power_end;
2352 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2353 struct sensor *sensor = to_sensor(client);
2355 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2356 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2362 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2370 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2372 struct i2c_client *client = v4l2_get_subdevdata(sd);
2373 struct soc_camera_device *icd = client->dev.platform_data;
2374 struct sensor *sensor = to_sensor(client);
2375 const struct v4l2_queryctrl *qctrl;
2376 const struct sensor_datafmt *fmt;
2380 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2382 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2384 goto sensor_INIT_ERR;
2388 if (sensor_task_lock(client,1)<0)
2389 goto sensor_INIT_ERR;
2390 ret = sensor_write(client, 0x3008, 0x80);
2392 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2394 goto sensor_INIT_ERR;
2397 mdelay(5); //delay 5 microseconds
2398 /* check if it is an sensor sensor */
2399 ret = sensor_read(client, 0x300a, &value);
2401 SENSOR_TR("read chip id high byte failed\n");
2403 goto sensor_INIT_ERR;
2406 pid |= (value << 8);
2408 ret = sensor_read(client, 0x300b, &value);
2410 SENSOR_TR("read chip id low byte failed\n");
2412 goto sensor_INIT_ERR;
2415 pid |= (value & 0xff);
2416 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2418 if (pid == SENSOR_ID) {
2419 sensor->model = SENSOR_V4L2_IDENT;
2421 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2423 goto sensor_INIT_ERR;
2426 ret = sensor_write_array(client, sensor_init_data);
2428 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2429 goto sensor_INIT_ERR;
2431 sensor_task_lock(client,0);
2432 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2433 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2435 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2437 goto sensor_INIT_ERR;
2439 sensor->info_priv.fmt = *fmt;
2441 /* sensor sensor information for initialization */
2442 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2444 sensor->info_priv.whiteBalance = qctrl->default_value;
2445 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2447 sensor->info_priv.brightness = qctrl->default_value;
2448 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2450 sensor->info_priv.effect = qctrl->default_value;
2451 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2453 sensor->info_priv.exposure = qctrl->default_value;
2455 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2457 sensor->info_priv.saturation = qctrl->default_value;
2458 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2460 sensor->info_priv.contrast = qctrl->default_value;
2461 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2463 sensor->info_priv.mirror = qctrl->default_value;
2464 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2466 sensor->info_priv.flip = qctrl->default_value;
2467 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2469 sensor->info_priv.scene = qctrl->default_value;
2470 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2472 sensor->info_priv.digitalzoom = qctrl->default_value;
2474 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2475 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2477 sensor->info_priv.focus = qctrl->default_value;
2479 #if CONFIG_SENSOR_Flash
2480 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2482 sensor->info_priv.flash = qctrl->default_value;
2484 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);
2486 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
2490 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2491 sensor_task_lock(client,0);
2492 sensor_deactivate(client);
2495 static int sensor_deactivate(struct i2c_client *client)
2497 struct soc_camera_device *icd = client->dev.platform_data;
2498 struct sensor *sensor = to_sensor(client);
2500 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2502 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2503 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2504 sensor_task_lock(client, 1);
2505 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2506 sensor_write(client, 0x3018, 0x03); // D5-D0
2507 sensor_write(client,0x3019,0x00); // STROBE,SDA
2508 sensor_task_lock(client, 0);
2510 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2512 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2513 icd->user_width = SENSOR_INIT_WIDTH;
2514 icd->user_height = SENSOR_INIT_HEIGHT;
2515 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2519 static struct reginfo sensor_power_down_sequence[]=
2524 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2527 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2529 if (pm_msg.event == PM_EVENT_SUSPEND) {
2530 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2531 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2533 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2536 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2538 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2543 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2550 static int sensor_resume(struct soc_camera_device *icd)
2554 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2556 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2560 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2564 static int sensor_set_bus_param(struct soc_camera_device *icd,
2565 unsigned long flags)
2571 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2573 struct soc_camera_link *icl = to_soc_camera_link(icd);
2574 unsigned long flags = SENSOR_BUS_PARAM;
2576 return soc_camera_apply_sensor_flags(icl, flags);
2579 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2581 struct i2c_client *client = v4l2_get_subdevdata(sd);
2582 struct soc_camera_device *icd = client->dev.platform_data;
2583 struct sensor *sensor = to_sensor(client);
2585 mf->width = icd->user_width;
2586 mf->height = icd->user_height;
2587 mf->code = sensor->info_priv.fmt.code;
2588 mf->colorspace = sensor->info_priv.fmt.colorspace;
2589 mf->field = V4L2_FIELD_NONE;
2593 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2597 if ((mf->width == 1024) && (mf->height == 768)) {
2599 } else if ((mf->width == 1280) && (mf->height == 1024)) {
2601 } else if ((mf->width == 1600) && (mf->height == 1200)) {
2603 } else if ((mf->width == 2048) && (mf->height == 1536)) {
2605 } else if ((mf->width == 2592) && (mf->height == 1944)) {
2610 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2614 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2618 if ((mf->width == 1280) && (mf->height == 720)) {
2620 } else if ((mf->width == 1920) && (mf->height == 1080)) {
2625 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2628 static int sensor_s_fmt(struct v4l2_subdev *sd,struct v4l2_mbus_framefmt *mf)
2630 struct i2c_client *client = v4l2_get_subdevdata(sd);
2631 const struct sensor_datafmt *fmt;
2632 struct sensor *sensor = to_sensor(client);
2633 const struct v4l2_queryctrl *qctrl;
2634 struct soc_camera_device *icd = client->dev.platform_data;
2635 struct reginfo *winseqe_set_addr=NULL;
2636 int ret = 0, set_w,set_h;
2638 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2639 ARRAY_SIZE(sensor_colour_fmts));
2642 goto sensor_s_fmt_end;
2645 if (sensor->info_priv.fmt.code != mf->code) {
2648 case V4L2_MBUS_FMT_YUYV8_2X8:
2650 winseqe_set_addr = sensor_ClrFmt_YUYV;
2653 case V4L2_MBUS_FMT_UYVY8_2X8:
2655 winseqe_set_addr = sensor_ClrFmt_UYVY;
2661 if (winseqe_set_addr != NULL) {
2662 sensor_write_array(client, winseqe_set_addr);
2663 sensor->info_priv.fmt.code = mf->code;
2664 sensor->info_priv.fmt.colorspace= mf->colorspace;
2665 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2667 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2674 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2676 winseqe_set_addr = sensor_qcif;
2680 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2682 winseqe_set_addr = sensor_qvga;
2686 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2688 winseqe_set_addr = sensor_cif;
2692 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2694 winseqe_set_addr = sensor_vga;
2698 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2700 winseqe_set_addr = sensor_svga;
2704 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2706 winseqe_set_addr = sensor_xga;
2710 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2712 winseqe_set_addr = sensor_720p;
2716 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2718 winseqe_set_addr = sensor_sxga;
2722 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2724 winseqe_set_addr = sensor_uxga;
2728 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2730 winseqe_set_addr = sensor_1080p;
2734 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2736 winseqe_set_addr = sensor_qxga;
2740 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2742 winseqe_set_addr = sensor_qsxga;
2748 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2749 set_w = SENSOR_INIT_WIDTH;
2750 set_h = SENSOR_INIT_HEIGHT;
2751 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2754 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2756 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2757 sensor_parameter_record(client);
2758 /*#if CONFIG_SENSOR_Focus
2759 sensor_af_idlechk(client);
2760 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2761 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2763 #if CONFIG_SENSOR_Flash
2764 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2765 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2766 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2769 }else { /* ddl@rock-chips.com : Video */
2770 #if CONFIG_SENSOR_Flash
2771 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2772 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2773 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2777 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2778 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2779 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2780 ret |= sensor_write_array(client,sensor_init_data);
2781 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2784 ret |= sensor_write_array(client, winseqe_set_addr);
2786 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2787 #if CONFIG_SENSOR_Flash
2788 if (sensor_fmt_capturechk(sd,mf) == true) {
2789 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2790 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2791 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2795 goto sensor_s_fmt_end;
2797 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2799 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2800 sensor_ae_transfer(client);
2801 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2802 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2803 if (sensor->info_priv.whiteBalance != 0) {
2804 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2805 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2807 sensor->info_priv.snap2preview = true;
2808 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2809 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2810 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2812 sensor->info_priv.video2preview = true;
2813 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2814 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2815 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2816 if (sensor->info_priv.snap2preview == true) {
2817 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2818 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2820 #if CONFIG_SENSOR_Focus
2821 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
2822 sensor_af_workqueue_set(icd,WqCmd_af_return_idle,0,true);
2830 sensor->info_priv.video2preview = false;
2831 sensor->info_priv.snap2preview = false;
2833 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2837 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2845 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2847 struct i2c_client *client = v4l2_get_subdevdata(sd);
2848 struct sensor *sensor = to_sensor(client);
2849 const struct sensor_datafmt *fmt;
2852 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2853 ARRAY_SIZE(sensor_colour_fmts));
2855 fmt = &sensor->info_priv.fmt;
2856 mf->code = fmt->code;
2859 if (mf->height > SENSOR_MAX_HEIGHT)
2860 mf->height = SENSOR_MAX_HEIGHT;
2861 else if (mf->height < SENSOR_MIN_HEIGHT)
2862 mf->height = SENSOR_MIN_HEIGHT;
2864 if (mf->width > SENSOR_MAX_WIDTH)
2865 mf->width = SENSOR_MAX_WIDTH;
2866 else if (mf->width < SENSOR_MIN_WIDTH)
2867 mf->width = SENSOR_MIN_WIDTH;
2869 mf->colorspace = fmt->colorspace;
2874 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2876 struct i2c_client *client = v4l2_get_subdevdata(sd);
2878 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2881 if (id->match.addr != client->addr)
2884 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2889 #if CONFIG_SENSOR_Brightness
2890 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2892 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2894 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2896 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2898 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2900 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2903 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2907 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2911 #if CONFIG_SENSOR_Effect
2912 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2914 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2916 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2918 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2920 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2922 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2925 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2929 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2933 #if CONFIG_SENSOR_Exposure
2934 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2936 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2938 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2940 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2942 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2944 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2947 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2951 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2955 #if CONFIG_SENSOR_Saturation
2956 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2958 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2960 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2962 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2964 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2966 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2969 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2973 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2977 #if CONFIG_SENSOR_Contrast
2978 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2980 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2982 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2984 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2986 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2988 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2991 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2995 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2999 #if CONFIG_SENSOR_Mirror
3000 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3002 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3004 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3006 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3008 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3010 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3013 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3017 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3021 #if CONFIG_SENSOR_Flip
3022 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3024 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3026 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3028 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3030 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3032 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3035 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3039 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3043 #if CONFIG_SENSOR_Scene
3044 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3046 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3048 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3050 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3052 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3054 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3057 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3061 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3065 #if CONFIG_SENSOR_WhiteBalance
3066 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3068 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3070 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3072 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3074 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3076 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3079 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3083 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3087 #if CONFIG_SENSOR_DigitalZoom
3088 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3090 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3091 struct sensor *sensor = to_sensor(client);
3092 const struct v4l2_queryctrl *qctrl_info;
3093 int digitalzoom_cur, digitalzoom_total;
3095 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3099 digitalzoom_cur = sensor->info_priv.digitalzoom;
3100 digitalzoom_total = qctrl_info->maximum;
3102 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3104 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3108 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3110 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3114 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3116 *value = digitalzoom_total - digitalzoom_cur;
3119 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3121 *value = 0 - digitalzoom_cur;
3124 digitalzoom_cur += *value;
3126 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3128 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3130 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3133 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3140 #if CONFIG_SENSOR_Focus
3141 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3143 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3144 struct sensor *sensor = to_sensor(client);
3145 const struct v4l2_queryctrl *qctrl_info;
3148 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3152 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3153 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3154 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true);
3155 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3158 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3162 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3163 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3168 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3170 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3171 struct sensor *sensor = to_sensor(client);
3172 const struct v4l2_queryctrl *qctrl_info;
3175 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3179 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3180 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3182 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true);
3184 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true);
3186 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3189 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3193 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3194 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3199 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3201 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3202 struct sensor *sensor = to_sensor(client);
3205 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3208 case SENSOR_AF_MODE_AUTO:
3210 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true);
3214 /*case SENSOR_AF_MODE_MACRO:
3216 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3220 case SENSOR_AF_MODE_INFINITY:
3222 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3226 case SENSOR_AF_MODE_CONTINUOUS:
3228 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true);
3232 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3237 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3240 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3241 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3247 #if CONFIG_SENSOR_Flash
3248 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3250 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3251 if (value == 3) { /* ddl@rock-chips.com: torch */
3252 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3254 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3256 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3260 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3264 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3266 struct i2c_client *client = v4l2_get_subdevdata(sd);
3267 struct sensor *sensor = to_sensor(client);
3268 const struct v4l2_queryctrl *qctrl;
3270 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3274 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3280 case V4L2_CID_BRIGHTNESS:
3282 ctrl->value = sensor->info_priv.brightness;
3285 case V4L2_CID_SATURATION:
3287 ctrl->value = sensor->info_priv.saturation;
3290 case V4L2_CID_CONTRAST:
3292 ctrl->value = sensor->info_priv.contrast;
3295 case V4L2_CID_DO_WHITE_BALANCE:
3297 ctrl->value = sensor->info_priv.whiteBalance;
3300 case V4L2_CID_EXPOSURE:
3302 ctrl->value = sensor->info_priv.exposure;
3305 case V4L2_CID_HFLIP:
3307 ctrl->value = sensor->info_priv.mirror;
3310 case V4L2_CID_VFLIP:
3312 ctrl->value = sensor->info_priv.flip;
3323 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3325 struct i2c_client *client = v4l2_get_subdevdata(sd);
3326 struct sensor *sensor = to_sensor(client);
3327 struct soc_camera_device *icd = client->dev.platform_data;
3328 const struct v4l2_queryctrl *qctrl;
3331 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3335 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3341 #if CONFIG_SENSOR_Brightness
3342 case V4L2_CID_BRIGHTNESS:
3344 if (ctrl->value != sensor->info_priv.brightness)
3346 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3350 sensor->info_priv.brightness = ctrl->value;
3355 #if CONFIG_SENSOR_Exposure
3356 case V4L2_CID_EXPOSURE:
3358 if (ctrl->value != sensor->info_priv.exposure)
3360 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3364 sensor->info_priv.exposure = ctrl->value;
3369 #if CONFIG_SENSOR_Saturation
3370 case V4L2_CID_SATURATION:
3372 if (ctrl->value != sensor->info_priv.saturation)
3374 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3378 sensor->info_priv.saturation = ctrl->value;
3383 #if CONFIG_SENSOR_Contrast
3384 case V4L2_CID_CONTRAST:
3386 if (ctrl->value != sensor->info_priv.contrast)
3388 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3392 sensor->info_priv.contrast = ctrl->value;
3397 #if CONFIG_SENSOR_WhiteBalance
3398 case V4L2_CID_DO_WHITE_BALANCE:
3400 if (ctrl->value != sensor->info_priv.whiteBalance)
3402 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3406 sensor->info_priv.whiteBalance = ctrl->value;
3411 #if CONFIG_SENSOR_Mirror
3412 case V4L2_CID_HFLIP:
3414 if (ctrl->value != sensor->info_priv.mirror)
3416 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3418 sensor->info_priv.mirror = ctrl->value;
3423 #if CONFIG_SENSOR_Flip
3424 case V4L2_CID_VFLIP:
3426 if (ctrl->value != sensor->info_priv.flip)
3428 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3430 sensor->info_priv.flip = ctrl->value;
3441 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3443 const struct v4l2_queryctrl *qctrl;
3444 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3445 struct sensor *sensor = to_sensor(client);
3447 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3451 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3455 switch (ext_ctrl->id)
3457 case V4L2_CID_SCENE:
3459 ext_ctrl->value = sensor->info_priv.scene;
3462 case V4L2_CID_EFFECT:
3464 ext_ctrl->value = sensor->info_priv.effect;
3467 case V4L2_CID_ZOOM_ABSOLUTE:
3469 ext_ctrl->value = sensor->info_priv.digitalzoom;
3472 case V4L2_CID_ZOOM_RELATIVE:
3476 case V4L2_CID_FOCUS_ABSOLUTE:
3480 case V4L2_CID_FOCUS_RELATIVE:
3484 case V4L2_CID_FLASH:
3486 ext_ctrl->value = sensor->info_priv.flash;
3494 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3496 const struct v4l2_queryctrl *qctrl;
3497 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3498 struct sensor *sensor = to_sensor(client);
3501 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3505 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3510 switch (ext_ctrl->id)
3512 #if CONFIG_SENSOR_Scene
3513 case V4L2_CID_SCENE:
3515 if (ext_ctrl->value != sensor->info_priv.scene)
3517 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3519 sensor->info_priv.scene = ext_ctrl->value;
3524 #if CONFIG_SENSOR_Effect
3525 case V4L2_CID_EFFECT:
3527 if (ext_ctrl->value != sensor->info_priv.effect)
3529 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3531 sensor->info_priv.effect= ext_ctrl->value;
3536 #if CONFIG_SENSOR_DigitalZoom
3537 case V4L2_CID_ZOOM_ABSOLUTE:
3539 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3542 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3544 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3546 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3548 sensor->info_priv.digitalzoom += val_offset;
3550 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3555 case V4L2_CID_ZOOM_RELATIVE:
3557 if (ext_ctrl->value)
3559 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3561 sensor->info_priv.digitalzoom += ext_ctrl->value;
3563 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3568 #if CONFIG_SENSOR_Focus
3569 case V4L2_CID_FOCUS_ABSOLUTE:
3571 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3576 case V4L2_CID_FOCUS_RELATIVE:
3578 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3581 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3584 case V4L2_CID_FOCUS_AUTO:
3586 if (ext_ctrl->value == 1) {
3587 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
3588 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
3589 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3593 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3594 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3595 if (ext_ctrl->value == 0)
3596 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3600 case V4L2_CID_FOCUS_CONTINUOUS:
3602 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3603 if (ext_ctrl->value == 1) {
3604 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3606 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3609 if (ext_ctrl->value == 0)
3610 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3615 #if CONFIG_SENSOR_Flash
3616 case V4L2_CID_FLASH:
3618 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3620 sensor->info_priv.flash = ext_ctrl->value;
3622 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3633 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3635 struct i2c_client *client = v4l2_get_subdevdata(sd);
3636 struct soc_camera_device *icd = client->dev.platform_data;
3637 int i, error_cnt=0, error_idx=-1;
3640 for (i=0; i<ext_ctrl->count; i++) {
3641 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3648 error_idx = ext_ctrl->count;
3650 if (error_idx != -1) {
3651 ext_ctrl->error_idx = error_idx;
3658 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3660 struct i2c_client *client = v4l2_get_subdevdata(sd);
3661 struct soc_camera_device *icd = client->dev.platform_data;
3662 int i, error_cnt=0, error_idx=-1;
3664 for (i=0; i<ext_ctrl->count; i++) {
3665 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3672 error_idx = ext_ctrl->count;
3674 if (error_idx != -1) {
3675 ext_ctrl->error_idx = error_idx;
3682 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3684 struct i2c_client *client = v4l2_get_subdevdata(sd);
3685 struct sensor *sensor = to_sensor(client);
3686 #if CONFIG_SENSOR_Focus
3687 struct soc_camera_device *icd = client->dev.platform_data;
3688 struct v4l2_mbus_framefmt mf;
3692 sensor->info_priv.enable = 1;
3693 #if CONFIG_SENSOR_Focus
3694 mf.width = icd->user_width;
3695 mf.height = icd->user_height;
3696 mf.code = sensor->info_priv.fmt.code;
3697 mf.colorspace = sensor->info_priv.fmt.colorspace;
3698 mf.field = V4L2_FIELD_NONE;
3699 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3700 if (sensor_fmt_capturechk(sd, &mf) == false) {
3701 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3702 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false);
3703 sensor->info_priv.affm_reinit = 0;
3707 } else if (enable == 0) {
3708 sensor->info_priv.enable = 0;
3709 #if CONFIG_SENSOR_Focus
3710 flush_workqueue(sensor->sensor_wq);
3716 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3717 * this wasn't our capture interface, so, we wait for the right one */
3718 static int sensor_video_probe(struct soc_camera_device *icd,
3719 struct i2c_client *client)
3723 struct sensor *sensor = to_sensor(client);
3725 /* We must have a parent by now. And it cannot be a wrong one.
3726 * So this entire test is completely redundant. */
3727 if (!icd->dev.parent ||
3728 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3731 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3733 goto sensor_video_probe_err;
3736 ret = sensor_write(client, 0x3008, 0x80);
3738 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3740 goto sensor_video_probe_err;
3742 mdelay(5); //delay 5 microseconds
3744 /* check if it is an sensor sensor */
3745 ret = sensor_read(client, 0x300a, &value);
3747 SENSOR_TR("read chip id high byte failed\n");
3749 goto sensor_video_probe_err;
3752 pid |= (value << 8);
3754 ret = sensor_read(client, 0x300b, &value);
3756 SENSOR_TR("read chip id low byte failed\n");
3758 goto sensor_video_probe_err;
3761 pid |= (value & 0xff);
3762 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3763 if (pid == SENSOR_ID) {
3764 sensor->model = SENSOR_V4L2_IDENT;
3766 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3768 goto sensor_video_probe_err;
3773 sensor_video_probe_err:
3776 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3778 struct i2c_client *client = v4l2_get_subdevdata(sd);
3779 struct soc_camera_device *icd = client->dev.platform_data;
3780 struct sensor *sensor = to_sensor(client);
3783 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3786 case RK29_CAM_SUBDEV_DEACTIVATE:
3788 sensor_deactivate(client);
3791 case RK29_CAM_SUBDEV_IOREQUEST:
3793 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3794 if (sensor->sensor_io_request != NULL) {
3795 sensor->sensor_gpio_res = NULL;
3796 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
3797 if (sensor->sensor_io_request->gpio_res[i].dev_name &&
3798 (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
3799 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
3802 if (sensor->sensor_gpio_res == NULL) {
3803 SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
3805 goto sensor_ioctl_end;
3808 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3810 goto sensor_ioctl_end;
3812 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3814 #if CONFIG_SENSOR_Flash
3815 if (sensor->sensor_gpio_res) {
3816 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3817 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3818 for (i = 0; i < icd->ops->num_controls; i++) {
3819 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3820 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3823 sensor->info_priv.flash = 0xff;
3824 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3832 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3841 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3842 enum v4l2_mbus_pixelcode *code)
3844 if (index >= ARRAY_SIZE(sensor_colour_fmts))
3847 *code = sensor_colour_fmts[index].code;
3850 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3851 .init = sensor_init,
3852 .g_ctrl = sensor_g_control,
3853 .s_ctrl = sensor_s_control,
3854 .g_ext_ctrls = sensor_g_ext_controls,
3855 .s_ext_ctrls = sensor_s_ext_controls,
3856 .g_chip_ident = sensor_g_chip_ident,
3857 .ioctl = sensor_ioctl,
3860 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3861 .s_mbus_fmt = sensor_s_fmt,
3862 .g_mbus_fmt = sensor_g_fmt,
3863 .try_mbus_fmt = sensor_try_fmt,
3864 .enum_mbus_fmt = sensor_enum_fmt,
3865 .s_stream = sensor_s_stream,
3868 static struct v4l2_subdev_ops sensor_subdev_ops = {
3869 .core = &sensor_subdev_core_ops,
3870 .video = &sensor_subdev_video_ops,
3873 static int sensor_probe(struct i2c_client *client,
3874 const struct i2c_device_id *did)
3876 struct sensor *sensor;
3877 struct soc_camera_device *icd = client->dev.platform_data;
3878 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3879 struct soc_camera_link *icl;
3882 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3884 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3888 icl = to_soc_camera_link(icd);
3890 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3894 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3895 dev_warn(&adapter->dev,
3896 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3900 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3904 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3906 /* Second stage probe - when a capture adapter is there */
3907 icd->ops = &sensor_ops;
3908 sensor->info_priv.fmt = sensor_colour_fmts[0];
3909 #if CONFIG_SENSOR_I2C_NOSCHED
3910 atomic_set(&sensor->tasklock_cnt,0);
3913 ret = sensor_video_probe(icd, client);
3916 i2c_set_clientdata(client, NULL);
3920 #if CONFIG_SENSOR_Focus
3921 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
3922 if (sensor->sensor_wq == NULL)
3923 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
3924 mutex_init(&sensor->wq_lock);
3928 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3932 static int sensor_remove(struct i2c_client *client)
3934 struct sensor *sensor = to_sensor(client);
3935 struct soc_camera_device *icd = client->dev.platform_data;
3937 #if CONFIG_SENSOR_Focus
3938 if (sensor->sensor_wq) {
3939 destroy_workqueue(sensor->sensor_wq);
3940 sensor->sensor_wq = NULL;
3945 i2c_set_clientdata(client, NULL);
3946 client->driver = NULL;
3952 static const struct i2c_device_id sensor_id[] = {
3953 {SENSOR_NAME_STRING(), 0 },
3956 MODULE_DEVICE_TABLE(i2c, sensor_id);
3958 static struct i2c_driver sensor_i2c_driver = {
3960 .name = SENSOR_NAME_STRING(),
3962 .probe = sensor_probe,
3963 .remove = sensor_remove,
3964 .id_table = sensor_id,
3967 static int __init sensor_mod_init(void)
3969 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3970 return i2c_add_driver(&sensor_i2c_driver);
3973 static void __exit sensor_mod_exit(void)
3975 i2c_del_driver(&sensor_i2c_driver);
3978 device_initcall_sync(sensor_mod_init);
3979 module_exit(sensor_mod_exit);
3981 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3982 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3983 MODULE_LICENSE("GPL");