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 1
66 #define CONFIG_SENSOR_Flash 1
67 #define CONFIG_SENSOR_Mirror 0
68 #define CONFIG_SENSOR_Flip 0
69 #ifdef CONFIG_OV5640_AUTOFOCUS_FOR_TD8801
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[]=
972 static struct reginfo sensor_Exposure1[]=
982 static struct reginfo sensor_Exposure2[]=
993 static struct reginfo sensor_Exposure3[]=
1001 {SEQUENCE_END, 0x00}
1003 static struct reginfo sensor_Exposure4[]=
1011 {SEQUENCE_END, 0x00}
1013 static struct reginfo sensor_Exposure5[]=
1021 {SEQUENCE_END, 0x00}
1023 static struct reginfo sensor_Exposure6[]=
1031 {SEQUENCE_END, 0x00}
1033 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1034 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1037 #if CONFIG_SENSOR_Saturation
1038 static struct reginfo sensor_Saturation0[]=
1040 {SEQUENCE_END, 0x00}
1043 static struct reginfo sensor_Saturation1[]=
1045 {SEQUENCE_END, 0x00}
1048 static struct reginfo sensor_Saturation2[]=
1050 {SEQUENCE_END, 0x00}
1051 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1054 #if CONFIG_SENSOR_Contrast
1055 static struct reginfo sensor_Contrast0[]=
1057 {SEQUENCE_END, 0x00}
1060 static struct reginfo sensor_Contrast1[]=
1062 {SEQUENCE_END, 0x00}
1064 static struct reginfo sensor_Contrast2[]=
1066 {SEQUENCE_END, 0x00}
1069 static struct reginfo sensor_Contrast3[]=
1071 {SEQUENCE_END, 0x00}
1074 static struct reginfo sensor_Contrast4[]=
1076 {SEQUENCE_END, 0x00}
1080 static struct reginfo sensor_Contrast5[]=
1082 {SEQUENCE_END, 0x00}
1085 static struct reginfo sensor_Contrast6[]=
1087 {SEQUENCE_END, 0x00}
1089 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1090 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1094 #if CONFIG_SENSOR_Mirror
1095 static struct reginfo sensor_MirrorOn[]=
1097 {SEQUENCE_END, 0x00}
1099 static struct reginfo sensor_MirrorOff[]=
1101 {SEQUENCE_END, 0x00}
1103 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1105 #if CONFIG_SENSOR_Flip
1106 static struct reginfo sensor_FlipOn[]=
1108 {SEQUENCE_END, 0x00}
1111 static struct reginfo sensor_FlipOff[]=
1113 {SEQUENCE_END, 0x00}
1115 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1118 #if CONFIG_SENSOR_Scene
1119 static struct reginfo sensor_SceneAuto[] =
1122 {SEQUENCE_END, 0x00}
1124 static struct reginfo sensor_SceneNight[] =
1126 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1144 {SEQUENCE_END, 0x00}
1146 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1149 #if CONFIG_SENSOR_DigitalZoom
1150 static struct reginfo sensor_Zoom0[] =
1152 {SEQUENCE_END, 0x00}
1154 static struct reginfo sensor_Zoom1[] =
1156 {SEQUENCE_END, 0x00}
1159 static struct reginfo sensor_Zoom2[] =
1161 {SEQUENCE_END, 0x00}
1165 static struct reginfo sensor_Zoom3[] =
1167 {SEQUENCE_END, 0x00}
1169 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1171 static const struct v4l2_querymenu sensor_menus[] =
1173 #if CONFIG_SENSOR_WhiteBalance
1174 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1175 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1176 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1179 #if CONFIG_SENSOR_Effect
1180 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1181 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1182 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1185 #if CONFIG_SENSOR_Scene
1186 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1189 #if CONFIG_SENSOR_Flash
1190 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1191 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1195 static const struct v4l2_queryctrl sensor_controls[] =
1197 #if CONFIG_SENSOR_WhiteBalance
1199 .id = V4L2_CID_DO_WHITE_BALANCE,
1200 .type = V4L2_CTRL_TYPE_MENU,
1201 .name = "White Balance Control",
1209 #if CONFIG_SENSOR_Brightness
1211 .id = V4L2_CID_BRIGHTNESS,
1212 .type = V4L2_CTRL_TYPE_INTEGER,
1213 .name = "Brightness Control",
1221 #if CONFIG_SENSOR_Effect
1223 .id = V4L2_CID_EFFECT,
1224 .type = V4L2_CTRL_TYPE_MENU,
1225 .name = "Effect Control",
1233 #if CONFIG_SENSOR_Exposure
1235 .id = V4L2_CID_EXPOSURE,
1236 .type = V4L2_CTRL_TYPE_INTEGER,
1237 .name = "Exposure Control",
1245 #if CONFIG_SENSOR_Saturation
1247 .id = V4L2_CID_SATURATION,
1248 .type = V4L2_CTRL_TYPE_INTEGER,
1249 .name = "Saturation Control",
1257 #if CONFIG_SENSOR_Contrast
1259 .id = V4L2_CID_CONTRAST,
1260 .type = V4L2_CTRL_TYPE_INTEGER,
1261 .name = "Contrast Control",
1269 #if CONFIG_SENSOR_Mirror
1271 .id = V4L2_CID_HFLIP,
1272 .type = V4L2_CTRL_TYPE_BOOLEAN,
1273 .name = "Mirror Control",
1281 #if CONFIG_SENSOR_Flip
1283 .id = V4L2_CID_VFLIP,
1284 .type = V4L2_CTRL_TYPE_BOOLEAN,
1285 .name = "Flip Control",
1293 #if CONFIG_SENSOR_Scene
1295 .id = V4L2_CID_SCENE,
1296 .type = V4L2_CTRL_TYPE_MENU,
1297 .name = "Scene Control",
1305 #if CONFIG_SENSOR_DigitalZoom
1307 .id = V4L2_CID_ZOOM_RELATIVE,
1308 .type = V4L2_CTRL_TYPE_INTEGER,
1309 .name = "DigitalZoom Control",
1315 .id = V4L2_CID_ZOOM_ABSOLUTE,
1316 .type = V4L2_CTRL_TYPE_INTEGER,
1317 .name = "DigitalZoom Control",
1325 #if CONFIG_SENSOR_Focus
1327 .id = V4L2_CID_FOCUS_RELATIVE,
1328 .type = V4L2_CTRL_TYPE_INTEGER,
1329 .name = "Focus Control",
1335 .id = V4L2_CID_FOCUS_ABSOLUTE,
1336 .type = V4L2_CTRL_TYPE_INTEGER,
1337 .name = "Focus Control",
1341 .default_value = 125,
1344 .id = V4L2_CID_FOCUS_AUTO,
1345 .type = V4L2_CTRL_TYPE_BOOLEAN,
1346 .name = "Focus Control",
1352 .id = V4L2_CID_FOCUS_CONTINUOUS,
1353 .type = V4L2_CTRL_TYPE_BOOLEAN,
1354 .name = "Focus Control",
1362 #if CONFIG_SENSOR_Flash
1364 .id = V4L2_CID_FLASH,
1365 .type = V4L2_CTRL_TYPE_MENU,
1366 .name = "Flash Control",
1375 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1376 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1377 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1378 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1379 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1380 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1381 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1382 static int sensor_resume(struct soc_camera_device *icd);
1383 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1384 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1385 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1386 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1387 static int sensor_deactivate(struct i2c_client *client);
1388 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1389 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1391 static struct soc_camera_ops sensor_ops =
1393 .suspend = sensor_suspend,
1394 .resume = sensor_resume,
1395 .set_bus_param = sensor_set_bus_param,
1396 .query_bus_param = sensor_query_bus_param,
1397 .controls = sensor_controls,
1398 .menus = sensor_menus,
1399 .num_controls = ARRAY_SIZE(sensor_controls),
1400 .num_menus = ARRAY_SIZE(sensor_menus),
1402 /* only one fixed colorspace per pixelcode */
1403 struct sensor_datafmt {
1404 enum v4l2_mbus_pixelcode code;
1405 enum v4l2_colorspace colorspace;
1408 /* Find a data format by a pixel code in an array */
1409 static const struct sensor_datafmt *sensor_find_datafmt(
1410 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1414 for (i = 0; i < n; i++)
1415 if (fmt[i].code == code)
1421 static const struct sensor_datafmt sensor_colour_fmts[] = {
1422 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1423 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1429 WqCmd_af_special_pos,
1433 WqCmd_af_return_idle,
1435 enum sensor_wq_result
1443 struct i2c_client *client;
1444 struct delayed_work dwork;
1445 enum sensor_wq_cmd cmd;
1446 wait_queue_head_t done;
1447 enum sensor_wq_result result;
1452 typedef struct sensor_info_priv_s
1468 unsigned char mirror; /* HFLIP */
1469 unsigned char flip; /* VFLIP */
1470 struct reginfo *winseqe_cur_addr;
1471 struct sensor_datafmt fmt;
1472 unsigned int enable;
1473 unsigned int funmodule_state;
1474 } sensor_info_priv_t;
1478 struct sensor_parameter
1480 unsigned short int preview_maxlines;
1481 unsigned short int preview_exposure;
1482 unsigned short int preview_line_width;
1483 unsigned short int preview_gain;
1485 unsigned short int capture_framerate;
1486 unsigned short int preview_framerate;
1492 struct v4l2_subdev subdev;
1493 struct i2c_client *client;
1494 sensor_info_priv_t info_priv;
1495 struct sensor_parameter parameter;
1496 struct workqueue_struct *sensor_wq;
1497 struct mutex wq_lock;
1498 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1499 #if CONFIG_SENSOR_I2C_NOSCHED
1500 atomic_t tasklock_cnt;
1502 struct rk29camera_platform_data *sensor_io_request;
1503 struct rk29camera_gpio_res *sensor_gpio_res;
1506 static struct sensor* to_sensor(const struct i2c_client *client)
1508 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1511 static int sensor_task_lock(struct i2c_client *client, int lock)
1513 #if CONFIG_SENSOR_I2C_NOSCHED
1515 struct sensor *sensor = to_sensor(client);
1518 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1519 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1520 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1524 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1525 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1526 goto sensor_task_lock_err;
1531 atomic_add(1, &sensor->tasklock_cnt);
1533 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1534 atomic_sub(1, &sensor->tasklock_cnt);
1536 if (atomic_read(&sensor->tasklock_cnt) == 0)
1541 sensor_task_lock_err:
1550 #if CONFIG_SENSOR_WRITE_REGS
1551 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
1554 struct i2c_msg msg[1];
1557 msg->addr = client->addr;
1558 msg->flags = client->flags;
1559 msg->buf = reg_info;
1560 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1561 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1567 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1568 err = i2c_transfer(client->adapter, msg, 1);
1572 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
1586 /* sensor register write */
1587 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1591 struct i2c_msg msg[1];
1594 buf[1] = reg & 0xFF;
1597 msg->addr = client->addr;
1598 msg->flags = client->flags;
1600 msg->len = sizeof(buf);
1601 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1602 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1607 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1608 err = i2c_transfer(client->adapter, msg, 1);
1613 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1621 /* sensor register read */
1622 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1626 struct i2c_msg msg[2];
1629 buf[1] = reg & 0xFF;
1631 msg[0].addr = client->addr;
1632 msg[0].flags = client->flags;
1634 msg[0].len = sizeof(buf);
1635 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1636 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1638 msg[1].addr = client->addr;
1639 msg[1].flags = client->flags|I2C_M_RD;
1642 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1643 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1647 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1648 err = i2c_transfer(client->adapter, msg, 2);
1654 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1662 /* write a array of registers */
1663 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1667 #if CONFIG_SENSOR_Focus
1668 struct sensor *sensor = to_sensor(client);
1670 #if CONFIG_SENSOR_I2C_RDWRCHK
1673 #if CONFIG_SENSOR_WRITE_REGS
1680 if (sensor_task_lock(client, 1) < 0)
1681 goto sensor_write_array_end;
1682 while (regarray[i].reg != SEQUENCE_END) {
1683 #if CONFIG_SENSOR_Focus
1684 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1685 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1687 goto sensor_write_array_end;
1691 #if CONFIG_SENSOR_WRITE_REGS
1695 reg_length = 0x0001;
1697 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
1700 if(reg_num >= WRITE_REGS_NUM)
1707 size_num = reg_num + 1;
1709 ptemp = phead = (u8*)kmalloc((size_num+10)*sizeof(u8),GFP_KERNEL);
1711 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1713 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1715 *phead = regarray[j].reg >> 8;
1716 *(ptemp+1) = regarray[j].reg & 0xFF;
1718 for( ; reg_num > 0; reg_num --, j++) {
1719 *ptemp ++ = regarray[j].val;
1723 err = sensor_write_regs(client, ptemp,size_num);
1727 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1730 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1735 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1739 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1741 goto sensor_write_array_end;
1744 #if CONFIG_SENSOR_I2C_RDWRCHK
1745 sensor_read(client, regarray[i].reg, &valchk);
1746 if (valchk != regarray[i].val)
1747 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1753 #if CONFIG_SENSOR_Focus
1754 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1755 || (regarray == sensor_init_data)) {
1756 sensor->info_priv.affm_reinit = 1;
1760 sensor_write_array_end:
1761 sensor_task_lock(client,0);
1764 #if CONFIG_SENSOR_I2C_RDWRCHK
1765 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1773 while (regarray[i].reg != 0)
1775 sensor_read(client, regarray[i].reg, &valchk);
1776 if (valchk != regarray[i].val)
1777 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1784 #if CONFIG_SENSOR_Focus
1791 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1794 char read_tag=0xff,cnt;
1797 if (cmdinfo->validate_bit & 0x80) {
1798 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1799 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1800 goto sensor_af_cmdset_err;
1802 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1804 for (i=0; i<4; i++) {
1805 if (cmdinfo->validate_bit & (1<<i)) {
1806 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1807 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]);
1808 goto sensor_af_cmdset_err;
1810 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]);
1814 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
1815 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1816 goto sensor_af_cmdset_err;
1818 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1821 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1822 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1823 goto sensor_af_cmdset_err;
1830 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1831 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1834 } while((read_tag != 0x00)&& (cnt++<100));
1836 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1838 sensor_af_cmdset_err:
1842 static int sensor_af_idlechk(struct i2c_client *client)
1846 struct af_cmdinfo cmdinfo;
1848 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1850 cmdinfo.cmd_tag = 0x01;
1851 cmdinfo.validate_bit = 0x80;
1852 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1854 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1856 goto sensor_af_idlechk_end;
1861 ret = sensor_read(client, CMD_ACK_Reg, &state);
1863 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1865 goto sensor_af_idlechk_end;
1867 }while(0x00 != state);
1870 sensor_af_idlechk_end:
1874 static int sensor_af_single(struct i2c_client *client)
1878 struct af_cmdinfo cmdinfo;
1880 cmdinfo.cmd_tag = 0x01;
1881 cmdinfo.validate_bit = 0x80;
1882 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
1884 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1886 goto sensor_af_single_end;
1896 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1898 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1900 goto sensor_af_single_end;
1902 }while((state == S_FOCUSING) && (cnt<100));
1904 if (state != S_FOCUSED) {
1905 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1907 goto sensor_af_single_end;
1910 sensor_af_single_end:
1914 static int sensor_af_const(struct i2c_client *client)
1923 static int sensor_af_init(struct i2c_client *client)
1928 ret = sensor_write_array(client, sensor_af_firmware);
1930 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1932 goto sensor_af_init_end;
1942 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1944 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1946 goto sensor_af_init_end;
1948 }while((state == S_STARTUP) && (cnt<100));
1950 if (state != S_IDLE) {
1951 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1953 goto sensor_af_init_end;
1957 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1961 static int sensor_af_downfirmware(struct i2c_client *client)
1963 struct sensor *sensor = to_sensor(client);
1964 struct af_cmdinfo cmdinfo;
1965 int ret=0, focus_pos = 0xfe;
1966 struct soc_camera_device *icd = client->dev.platform_data;
1967 struct v4l2_mbus_framefmt mf;
1969 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1971 if (sensor_af_init(client)) {
1972 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1975 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1977 mf.width = icd->user_width;
1978 mf.height = icd->user_height;
1979 mf.code = sensor->info_priv.fmt.code;
1980 mf.colorspace = sensor->info_priv.fmt.colorspace;
1981 mf.field = V4L2_FIELD_NONE;
1982 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
1983 ret = sensor_af_const(client);
1985 switch (sensor->info_priv.auto_focus)
1987 /*case SENSOR_AF_MODE_INFINITY:
1991 case SENSOR_AF_MODE_MACRO:
1993 if (focus_pos != 0x00)
1996 sensor_af_idlechk(client);
1997 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
1998 cmdinfo.cmd_para[0] = focus_pos;
1999 cmdinfo.validate_bit = 0x81;
2000 //ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
2003 case SENSOR_AF_MODE_AUTO:
2005 ret = sensor_af_single(client);
2008 /*case SENSOR_AF_MODE_CONTINUOUS:
2010 ret = sensor_af_const(client);
2013 case SENSOR_AF_MODE_CLOSE:
2020 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
2021 goto sensor_af_downfirmware_end;
2025 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
2028 sensor_af_downfirmware_end:
2032 static void sensor_af_workqueue(struct work_struct *work)
2034 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
2035 struct i2c_client *client = sensor_work->client;
2036 struct sensor *sensor = to_sensor(client);
2037 struct af_cmdinfo cmdinfo;
2039 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
2041 mutex_lock(&sensor->wq_lock);
2042 switch (sensor_work->cmd)
2046 if (sensor_af_downfirmware(client) < 0) {
2047 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2051 case WqCmd_af_single:
2053 if (sensor_af_single(client) < 0) {
2054 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2055 sensor_work->result = WqRet_fail;
2057 sensor_work->result = WqRet_success;
2061 case WqCmd_af_special_pos:
2063 sensor_af_idlechk(client);
2065 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2066 cmdinfo.cmd_para[0] = sensor_work->var;
2067 cmdinfo.validate_bit = 0x81;
2068 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2069 sensor_work->result = WqRet_fail;
2071 sensor_work->result = WqRet_success;
2074 case WqCmd_af_near_pos:
2076 sensor_af_idlechk(client);
2077 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2078 cmdinfo.validate_bit = 0x80;
2079 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2080 sensor_work->result = WqRet_fail;
2082 sensor_work->result = WqRet_success;
2085 case WqCmd_af_far_pos:
2087 sensor_af_idlechk(client);
2088 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2089 cmdinfo.validate_bit = 0x80;
2090 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2091 sensor_work->result = WqRet_fail;
2093 sensor_work->result = WqRet_success;
2096 case WqCmd_af_continues:
2098 if (sensor_af_const(client) < 0)
2099 sensor_work->result = WqRet_fail;
2101 sensor_work->result = WqRet_success;
2104 case WqCmd_af_return_idle:
2106 if (sensor_af_idlechk(client) < 0)
2107 sensor_work->result = WqRet_fail;
2109 sensor_work->result = WqRet_success;
2113 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
2117 if (sensor_work->wait == false) {
2118 kfree((void*)sensor_work);
2120 wake_up(&sensor_work->done);
2122 mutex_unlock(&sensor->wq_lock);
2126 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait)
2128 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2129 struct sensor *sensor = to_sensor(client);
2130 struct sensor_work *wk;
2133 if (sensor->sensor_wq == NULL) {
2135 goto sensor_af_workqueue_set_end;
2138 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
2140 wk->client = client;
2141 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
2143 wk->result = WqRet_inval;
2146 init_waitqueue_head(&wk->done);
2148 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
2150 /* ddl@rock-chips.com:
2151 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
2152 * As a result any other ioctl calls will proceed very, very slowly since each call
2153 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
2154 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
2157 mutex_unlock(&icd->video_lock);
2158 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(2500)) == 0) {
2159 SENSOR_TR("%s %s cmd(%d) is timeout!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2163 mutex_lock(&icd->video_lock);
2167 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2170 sensor_af_workqueue_set_end:
2174 static int sensor_parameter_record(struct i2c_client *client)
2176 u8 ret_l,ret_m,ret_h;
2178 struct sensor *sensor = to_sensor(client);
2180 sensor_write(client,0x3503,0x07); //stop AE/AG
2181 sensor_write(client,0x3406,0x01); //stop AWB
2183 sensor_read(client,0x3500,&ret_h);
2184 sensor_read(client,0x3501, &ret_m);
2185 sensor_read(client,0x3502, &ret_l);
2189 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
2190 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
2191 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
2193 sensor_read(client,0x350c, &ret_h);
2194 sensor_read(client,0x350d, &ret_l);
2195 sensor->parameter.preview_line_width = ret_h & 0xff;
2196 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
2197 //Read back AGC Gain for preview
2198 sensor_read(client,0x350a, &ret_h);
2199 sensor_read(client,0x350b, &tp_l);
2200 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
2202 sensor_read(client,0x380e, &ret_h);
2203 sensor->parameter.preview_maxlines = ret_h;
2204 sensor->parameter.preview_maxlines <<= 8;
2205 sensor_read(client,0x380f, &tp_l);
2206 sensor->parameter.preview_maxlines += tp_l;
2208 sensor->parameter.capture_framerate = 300;
2209 sensor->parameter.preview_framerate = 2500;
2211 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
2212 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2213 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2214 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2215 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2216 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2218 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);
2221 static int sensor_ae_transfer(struct i2c_client *client)
2226 u16 ulCapture_Exposure;
2227 u32 ulCapture_Exposure_Gain;
2231 u8 reg_l = 0,reg_h =0;
2232 u16 Preview_Maxlines;
2234 u32 Capture_MaxLines;
2235 struct sensor *sensor = to_sensor(client);
2237 //Preview_Maxlines = sensor->parameter.preview_line_width;
2238 Preview_Maxlines = sensor->parameter.preview_maxlines;
2239 Gain = sensor->parameter.preview_gain;
2241 sensor_read(client,0x350c, ®_h);
2242 sensor_read(client,0x350d, ®_l);
2243 Capture_MaxLines = reg_h & 0xff;
2244 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2247 sensor_read(client,0x380e, ®_h);
2248 Capture_MaxLines = reg_h;
2249 Capture_MaxLines <<= 8;
2250 sensor_read(client,0x380f, ®_l);
2251 Capture_MaxLines += reg_l;
2254 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2256 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2258 Lines_10ms = Lines_10ms & 0xffff;
2260 if(Preview_Maxlines == 0)
2261 Preview_Maxlines = 1;
2263 //ulCapture_Exposure =
2264 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2266 ulCapture_Exposure =
2267 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2268 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2270 iCapture_Gain = (Gain & 0x0f) + 16;
2272 iCapture_Gain = iCapture_Gain << 1;
2275 iCapture_Gain = iCapture_Gain << 1;
2278 iCapture_Gain = iCapture_Gain << 1;
2281 iCapture_Gain = iCapture_Gain << 1;
2284 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, œâŸö¹ýÁÁ
2285 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2287 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2288 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2289 if (ulCapture_Exposure > Lines_10ms)
2291 //ulCapture_Exposure *= 1.7;
2292 ulCapture_Exposure /= Lines_10ms;
2293 ulCapture_Exposure *= Lines_10ms;
2296 ulCapture_Exposure = Capture_MaxLines;
2297 //ulCapture_Exposure_Gain *= 1.5;
2300 if(ulCapture_Exposure == 0)
2301 ulCapture_Exposure = 1;
2303 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2304 iCapture_Gain = iCapture_Gain & 0xffff;
2306 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2307 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2308 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2312 if (iCapture_Gain > 31) {
2314 iCapture_Gain = iCapture_Gain >> 1;
2316 if (iCapture_Gain > 31) {
2318 iCapture_Gain = iCapture_Gain >> 1;
2320 if (iCapture_Gain > 31) {
2322 iCapture_Gain = iCapture_Gain >> 1;
2324 if (iCapture_Gain > 31) {
2326 iCapture_Gain = iCapture_Gain >> 1;
2328 if (iCapture_Gain > 16)
2329 Gain |= ((iCapture_Gain -16) & 0x0f);
2332 // write the gain and exposure to 0x350* registers
2333 //m_iWrite0x350b=Gain;
2334 sensor_write(client,0x350b, Gain);
2335 Gain = (Gain >> 8) & 0x01;
2336 sensor_write(client,0x350a, Gain);
2337 //m_iWrite0x3502=ExposureLow;
2338 sensor_write(client,0x3502, ExposureLow);
2339 //m_iWrite0x3501=ExposureMid;
2340 sensor_write(client,0x3501, ExposureMid);
2341 //m_iWrite0x3500=ExposureHigh;
2342 sensor_write(client,0x3500, ExposureHigh);
2343 // SendToFile("Gain = 0x%x\r\n", Gain);
2344 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2345 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2346 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2347 //ŒÓ³€ÑÓʱ£¬±ÜÃâ°µŽŠÅÄÕÕʱµÄÃ÷°µ·ÖœçÎÊÌâ
2348 //camera_timed_wait(200);
2349 //linzhk camera_timed_wait(500);
2351 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2352 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2353 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2354 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2355 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2356 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2358 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2362 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2364 struct soc_camera_link *icl = to_soc_camera_link(icd);
2367 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2371 case Sensor_PowerDown:
2373 if (icl->powerdown) {
2374 ret = icl->powerdown(icd->pdev, on);
2375 if (ret == RK29_CAM_IO_SUCCESS) {
2379 icl->reset(icd->pdev);
2381 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2383 goto sensor_power_end;
2390 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2391 struct sensor *sensor = to_sensor(client);
2393 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2394 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2400 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2408 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2410 struct i2c_client *client = v4l2_get_subdevdata(sd);
2411 struct soc_camera_device *icd = client->dev.platform_data;
2412 struct sensor *sensor = to_sensor(client);
2413 const struct v4l2_queryctrl *qctrl;
2414 const struct sensor_datafmt *fmt;
2418 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2420 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2422 goto sensor_INIT_ERR;
2426 if (sensor_task_lock(client,1)<0)
2427 goto sensor_INIT_ERR;
2428 ret = sensor_write(client, 0x3008, 0x80);
2430 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2432 goto sensor_INIT_ERR;
2435 mdelay(5); //delay 5 microseconds
2436 /* check if it is an sensor sensor */
2437 ret = sensor_read(client, 0x300a, &value);
2439 SENSOR_TR("read chip id high byte failed\n");
2441 goto sensor_INIT_ERR;
2444 pid |= (value << 8);
2446 ret = sensor_read(client, 0x300b, &value);
2448 SENSOR_TR("read chip id low byte failed\n");
2450 goto sensor_INIT_ERR;
2453 pid |= (value & 0xff);
2454 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2456 if (pid == SENSOR_ID) {
2457 sensor->model = SENSOR_V4L2_IDENT;
2459 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2461 goto sensor_INIT_ERR;
2464 ret = sensor_write_array(client, sensor_init_data);
2466 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2467 goto sensor_INIT_ERR;
2469 sensor_task_lock(client,0);
2470 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2471 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2473 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2475 goto sensor_INIT_ERR;
2477 sensor->info_priv.fmt = *fmt;
2479 /* sensor sensor information for initialization */
2480 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2482 sensor->info_priv.whiteBalance = qctrl->default_value;
2483 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2485 sensor->info_priv.brightness = qctrl->default_value;
2486 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2488 sensor->info_priv.effect = qctrl->default_value;
2489 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2491 sensor->info_priv.exposure = qctrl->default_value;
2493 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2495 sensor->info_priv.saturation = qctrl->default_value;
2496 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2498 sensor->info_priv.contrast = qctrl->default_value;
2499 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2501 sensor->info_priv.mirror = qctrl->default_value;
2502 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2504 sensor->info_priv.flip = qctrl->default_value;
2505 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2507 sensor->info_priv.scene = qctrl->default_value;
2508 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2510 sensor->info_priv.digitalzoom = qctrl->default_value;
2512 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2513 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2515 sensor->info_priv.focus = qctrl->default_value;
2517 #if CONFIG_SENSOR_Flash
2518 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2520 sensor->info_priv.flash = qctrl->default_value;
2522 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);
2524 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
2528 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2529 sensor_task_lock(client,0);
2530 sensor_deactivate(client);
2533 static int sensor_deactivate(struct i2c_client *client)
2535 struct soc_camera_device *icd = client->dev.platform_data;
2536 struct sensor *sensor = to_sensor(client);
2538 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2540 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2541 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2542 sensor_task_lock(client, 1);
2543 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2544 sensor_write(client, 0x3018, 0x03); // D5-D0
2545 sensor_write(client,0x3019,0x00); // STROBE,SDA
2546 sensor_task_lock(client, 0);
2548 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2550 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2551 icd->user_width = SENSOR_INIT_WIDTH;
2552 icd->user_height = SENSOR_INIT_HEIGHT;
2553 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2557 static struct reginfo sensor_power_down_sequence[]=
2562 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2565 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2567 if (pm_msg.event == PM_EVENT_SUSPEND) {
2568 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2569 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2571 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2574 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2576 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2581 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2588 static int sensor_resume(struct soc_camera_device *icd)
2592 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2594 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2598 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2602 static int sensor_set_bus_param(struct soc_camera_device *icd,
2603 unsigned long flags)
2609 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2611 struct soc_camera_link *icl = to_soc_camera_link(icd);
2612 unsigned long flags = SENSOR_BUS_PARAM;
2614 return soc_camera_apply_sensor_flags(icl, flags);
2617 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2619 struct i2c_client *client = v4l2_get_subdevdata(sd);
2620 struct soc_camera_device *icd = client->dev.platform_data;
2621 struct sensor *sensor = to_sensor(client);
2623 mf->width = icd->user_width;
2624 mf->height = icd->user_height;
2625 mf->code = sensor->info_priv.fmt.code;
2626 mf->colorspace = sensor->info_priv.fmt.colorspace;
2627 mf->field = V4L2_FIELD_NONE;
2631 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2635 if ((mf->width == 1024) && (mf->height == 768)) {
2637 } else if ((mf->width == 1280) && (mf->height == 1024)) {
2639 } else if ((mf->width == 1600) && (mf->height == 1200)) {
2641 } else if ((mf->width == 2048) && (mf->height == 1536)) {
2643 } else if ((mf->width == 2592) && (mf->height == 1944)) {
2648 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2652 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2656 if ((mf->width == 1280) && (mf->height == 720)) {
2658 } else if ((mf->width == 1920) && (mf->height == 1080)) {
2663 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2666 static int sensor_s_fmt(struct v4l2_subdev *sd,struct v4l2_mbus_framefmt *mf)
2668 struct i2c_client *client = v4l2_get_subdevdata(sd);
2669 const struct sensor_datafmt *fmt;
2670 struct sensor *sensor = to_sensor(client);
2671 const struct v4l2_queryctrl *qctrl;
2672 struct soc_camera_device *icd = client->dev.platform_data;
2673 struct reginfo *winseqe_set_addr=NULL;
2674 int ret = 0, set_w,set_h;
2676 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2677 ARRAY_SIZE(sensor_colour_fmts));
2680 goto sensor_s_fmt_end;
2683 if (sensor->info_priv.fmt.code != mf->code) {
2686 case V4L2_MBUS_FMT_YUYV8_2X8:
2688 winseqe_set_addr = sensor_ClrFmt_YUYV;
2691 case V4L2_MBUS_FMT_UYVY8_2X8:
2693 winseqe_set_addr = sensor_ClrFmt_UYVY;
2699 if (winseqe_set_addr != NULL) {
2700 sensor_write_array(client, winseqe_set_addr);
2701 sensor->info_priv.fmt.code = mf->code;
2702 sensor->info_priv.fmt.colorspace= mf->colorspace;
2703 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2705 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2712 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2714 winseqe_set_addr = sensor_qcif;
2718 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2720 winseqe_set_addr = sensor_qvga;
2724 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2726 winseqe_set_addr = sensor_cif;
2730 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2732 winseqe_set_addr = sensor_vga;
2736 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2738 winseqe_set_addr = sensor_svga;
2742 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2744 winseqe_set_addr = sensor_xga;
2748 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2750 winseqe_set_addr = sensor_720p;
2754 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2756 winseqe_set_addr = sensor_sxga;
2760 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2762 winseqe_set_addr = sensor_uxga;
2766 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2768 winseqe_set_addr = sensor_1080p;
2772 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2774 winseqe_set_addr = sensor_qxga;
2778 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2780 winseqe_set_addr = sensor_qsxga;
2786 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2787 set_w = SENSOR_INIT_WIDTH;
2788 set_h = SENSOR_INIT_HEIGHT;
2789 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2792 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2794 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2795 sensor_parameter_record(client);
2796 /*#if CONFIG_SENSOR_Focus
2797 sensor_af_idlechk(client);
2798 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2799 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2801 #if CONFIG_SENSOR_Flash
2802 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2803 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2804 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2807 }else { /* ddl@rock-chips.com : Video */
2808 #if CONFIG_SENSOR_Flash
2809 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2810 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2811 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2815 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2816 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2817 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2818 ret |= sensor_write_array(client,sensor_init_data);
2819 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2822 ret |= sensor_write_array(client, winseqe_set_addr);
2824 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2825 #if CONFIG_SENSOR_Flash
2826 if (sensor_fmt_capturechk(sd,mf) == true) {
2827 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2828 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2829 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2833 goto sensor_s_fmt_end;
2835 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2837 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2838 sensor_ae_transfer(client);
2839 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2840 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2841 if (sensor->info_priv.whiteBalance != 0) {
2842 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2843 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2845 sensor->info_priv.snap2preview = true;
2846 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2847 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2848 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2850 sensor->info_priv.video2preview = true;
2851 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2852 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2853 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2854 if (sensor->info_priv.snap2preview == true) {
2855 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2856 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2858 #if CONFIG_SENSOR_Focus
2859 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
2860 sensor_af_workqueue_set(icd,WqCmd_af_return_idle,0,true);
2868 sensor->info_priv.video2preview = false;
2869 sensor->info_priv.snap2preview = false;
2871 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2875 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2883 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2885 struct i2c_client *client = v4l2_get_subdevdata(sd);
2886 struct sensor *sensor = to_sensor(client);
2887 const struct sensor_datafmt *fmt;
2890 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2891 ARRAY_SIZE(sensor_colour_fmts));
2893 fmt = &sensor->info_priv.fmt;
2894 mf->code = fmt->code;
2897 if (mf->height > SENSOR_MAX_HEIGHT)
2898 mf->height = SENSOR_MAX_HEIGHT;
2899 else if (mf->height < SENSOR_MIN_HEIGHT)
2900 mf->height = SENSOR_MIN_HEIGHT;
2902 if (mf->width > SENSOR_MAX_WIDTH)
2903 mf->width = SENSOR_MAX_WIDTH;
2904 else if (mf->width < SENSOR_MIN_WIDTH)
2905 mf->width = SENSOR_MIN_WIDTH;
2907 mf->colorspace = fmt->colorspace;
2912 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2914 struct i2c_client *client = v4l2_get_subdevdata(sd);
2916 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2919 if (id->match.addr != client->addr)
2922 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2927 #if CONFIG_SENSOR_Brightness
2928 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2930 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2932 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2934 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2936 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2938 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2941 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2945 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2949 #if CONFIG_SENSOR_Effect
2950 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2952 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2954 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2956 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2958 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2960 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2963 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2967 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2971 #if CONFIG_SENSOR_Exposure
2972 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2974 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2976 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2978 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2980 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2982 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2985 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2989 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2993 #if CONFIG_SENSOR_Saturation
2994 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2996 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2998 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3000 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3002 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3004 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3007 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3011 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3015 #if CONFIG_SENSOR_Contrast
3016 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3018 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3020 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3022 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3024 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3026 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3029 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3033 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3037 #if CONFIG_SENSOR_Mirror
3038 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3040 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3042 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3044 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3046 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3048 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3051 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3055 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3059 #if CONFIG_SENSOR_Flip
3060 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3062 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3064 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3066 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3068 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3070 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3073 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3077 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3081 #if CONFIG_SENSOR_Scene
3082 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3084 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3086 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3088 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3090 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3092 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3095 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3099 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3103 #if CONFIG_SENSOR_WhiteBalance
3104 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3106 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3108 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3110 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3112 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3114 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3117 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3121 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3125 #if CONFIG_SENSOR_DigitalZoom
3126 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3128 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3129 struct sensor *sensor = to_sensor(client);
3130 const struct v4l2_queryctrl *qctrl_info;
3131 int digitalzoom_cur, digitalzoom_total;
3133 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3137 digitalzoom_cur = sensor->info_priv.digitalzoom;
3138 digitalzoom_total = qctrl_info->maximum;
3140 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3142 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3146 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3148 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3152 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3154 *value = digitalzoom_total - digitalzoom_cur;
3157 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3159 *value = 0 - digitalzoom_cur;
3162 digitalzoom_cur += *value;
3164 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3166 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3168 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3171 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3178 #if CONFIG_SENSOR_Focus
3179 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3181 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3182 struct sensor *sensor = to_sensor(client);
3183 const struct v4l2_queryctrl *qctrl_info;
3186 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3190 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3191 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3192 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true);
3193 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3196 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3200 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3201 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3206 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3208 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3209 struct sensor *sensor = to_sensor(client);
3210 const struct v4l2_queryctrl *qctrl_info;
3213 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3217 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3218 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3220 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true);
3222 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true);
3224 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3227 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3231 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3232 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3237 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3239 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3240 struct sensor *sensor = to_sensor(client);
3243 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3246 case SENSOR_AF_MODE_AUTO:
3248 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true);
3252 /*case SENSOR_AF_MODE_MACRO:
3254 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3258 case SENSOR_AF_MODE_INFINITY:
3260 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3264 case SENSOR_AF_MODE_CONTINUOUS:
3266 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true);
3270 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3275 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3278 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3279 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3285 #if CONFIG_SENSOR_Flash
3286 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3288 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3289 if (value == 3) { /* ddl@rock-chips.com: torch */
3290 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3292 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3294 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3298 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3302 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3304 struct i2c_client *client = v4l2_get_subdevdata(sd);
3305 struct sensor *sensor = to_sensor(client);
3306 const struct v4l2_queryctrl *qctrl;
3308 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3312 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3318 case V4L2_CID_BRIGHTNESS:
3320 ctrl->value = sensor->info_priv.brightness;
3323 case V4L2_CID_SATURATION:
3325 ctrl->value = sensor->info_priv.saturation;
3328 case V4L2_CID_CONTRAST:
3330 ctrl->value = sensor->info_priv.contrast;
3333 case V4L2_CID_DO_WHITE_BALANCE:
3335 ctrl->value = sensor->info_priv.whiteBalance;
3338 case V4L2_CID_EXPOSURE:
3340 ctrl->value = sensor->info_priv.exposure;
3343 case V4L2_CID_HFLIP:
3345 ctrl->value = sensor->info_priv.mirror;
3348 case V4L2_CID_VFLIP:
3350 ctrl->value = sensor->info_priv.flip;
3361 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3363 struct i2c_client *client = v4l2_get_subdevdata(sd);
3364 struct sensor *sensor = to_sensor(client);
3365 struct soc_camera_device *icd = client->dev.platform_data;
3366 const struct v4l2_queryctrl *qctrl;
3369 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3373 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3379 #if CONFIG_SENSOR_Brightness
3380 case V4L2_CID_BRIGHTNESS:
3382 if (ctrl->value != sensor->info_priv.brightness)
3384 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3388 sensor->info_priv.brightness = ctrl->value;
3393 #if CONFIG_SENSOR_Exposure
3394 case V4L2_CID_EXPOSURE:
3396 if (ctrl->value != sensor->info_priv.exposure)
3398 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3402 sensor->info_priv.exposure = ctrl->value;
3407 #if CONFIG_SENSOR_Saturation
3408 case V4L2_CID_SATURATION:
3410 if (ctrl->value != sensor->info_priv.saturation)
3412 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3416 sensor->info_priv.saturation = ctrl->value;
3421 #if CONFIG_SENSOR_Contrast
3422 case V4L2_CID_CONTRAST:
3424 if (ctrl->value != sensor->info_priv.contrast)
3426 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3430 sensor->info_priv.contrast = ctrl->value;
3435 #if CONFIG_SENSOR_WhiteBalance
3436 case V4L2_CID_DO_WHITE_BALANCE:
3438 if (ctrl->value != sensor->info_priv.whiteBalance)
3440 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3444 sensor->info_priv.whiteBalance = ctrl->value;
3449 #if CONFIG_SENSOR_Mirror
3450 case V4L2_CID_HFLIP:
3452 if (ctrl->value != sensor->info_priv.mirror)
3454 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3456 sensor->info_priv.mirror = ctrl->value;
3461 #if CONFIG_SENSOR_Flip
3462 case V4L2_CID_VFLIP:
3464 if (ctrl->value != sensor->info_priv.flip)
3466 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3468 sensor->info_priv.flip = ctrl->value;
3479 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3481 const struct v4l2_queryctrl *qctrl;
3482 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3483 struct sensor *sensor = to_sensor(client);
3485 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3489 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3493 switch (ext_ctrl->id)
3495 case V4L2_CID_SCENE:
3497 ext_ctrl->value = sensor->info_priv.scene;
3500 case V4L2_CID_EFFECT:
3502 ext_ctrl->value = sensor->info_priv.effect;
3505 case V4L2_CID_ZOOM_ABSOLUTE:
3507 ext_ctrl->value = sensor->info_priv.digitalzoom;
3510 case V4L2_CID_ZOOM_RELATIVE:
3514 case V4L2_CID_FOCUS_ABSOLUTE:
3518 case V4L2_CID_FOCUS_RELATIVE:
3522 case V4L2_CID_FLASH:
3524 ext_ctrl->value = sensor->info_priv.flash;
3532 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3534 const struct v4l2_queryctrl *qctrl;
3535 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3536 struct sensor *sensor = to_sensor(client);
3539 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3543 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3548 switch (ext_ctrl->id)
3550 #if CONFIG_SENSOR_Scene
3551 case V4L2_CID_SCENE:
3553 if (ext_ctrl->value != sensor->info_priv.scene)
3555 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3557 sensor->info_priv.scene = ext_ctrl->value;
3562 #if CONFIG_SENSOR_Effect
3563 case V4L2_CID_EFFECT:
3565 if (ext_ctrl->value != sensor->info_priv.effect)
3567 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3569 sensor->info_priv.effect= ext_ctrl->value;
3574 #if CONFIG_SENSOR_DigitalZoom
3575 case V4L2_CID_ZOOM_ABSOLUTE:
3577 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3580 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3582 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3584 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3586 sensor->info_priv.digitalzoom += val_offset;
3588 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3593 case V4L2_CID_ZOOM_RELATIVE:
3595 if (ext_ctrl->value)
3597 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3599 sensor->info_priv.digitalzoom += ext_ctrl->value;
3601 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3606 #if CONFIG_SENSOR_Focus
3607 case V4L2_CID_FOCUS_ABSOLUTE:
3609 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3614 case V4L2_CID_FOCUS_RELATIVE:
3616 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3619 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3622 case V4L2_CID_FOCUS_AUTO:
3624 if (ext_ctrl->value == 1) {
3625 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
3626 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
3627 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3631 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3632 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3633 if (ext_ctrl->value == 0)
3634 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3638 case V4L2_CID_FOCUS_CONTINUOUS:
3640 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3641 if (ext_ctrl->value == 1) {
3642 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3644 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3647 if (ext_ctrl->value == 0)
3648 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3653 #if CONFIG_SENSOR_Flash
3654 case V4L2_CID_FLASH:
3656 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3658 sensor->info_priv.flash = ext_ctrl->value;
3660 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3671 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3673 struct i2c_client *client = v4l2_get_subdevdata(sd);
3674 struct soc_camera_device *icd = client->dev.platform_data;
3675 int i, error_cnt=0, error_idx=-1;
3678 for (i=0; i<ext_ctrl->count; i++) {
3679 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3686 error_idx = ext_ctrl->count;
3688 if (error_idx != -1) {
3689 ext_ctrl->error_idx = error_idx;
3696 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3698 struct i2c_client *client = v4l2_get_subdevdata(sd);
3699 struct soc_camera_device *icd = client->dev.platform_data;
3700 int i, error_cnt=0, error_idx=-1;
3702 for (i=0; i<ext_ctrl->count; i++) {
3703 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3710 error_idx = ext_ctrl->count;
3712 if (error_idx != -1) {
3713 ext_ctrl->error_idx = error_idx;
3720 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3722 struct i2c_client *client = v4l2_get_subdevdata(sd);
3723 struct sensor *sensor = to_sensor(client);
3724 #if CONFIG_SENSOR_Focus
3725 struct soc_camera_device *icd = client->dev.platform_data;
3726 struct v4l2_mbus_framefmt mf;
3730 sensor->info_priv.enable = 1;
3731 #if CONFIG_SENSOR_Focus
3732 mf.width = icd->user_width;
3733 mf.height = icd->user_height;
3734 mf.code = sensor->info_priv.fmt.code;
3735 mf.colorspace = sensor->info_priv.fmt.colorspace;
3736 mf.field = V4L2_FIELD_NONE;
3737 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3738 if (sensor_fmt_capturechk(sd, &mf) == false) {
3739 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3740 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false);
3741 sensor->info_priv.affm_reinit = 0;
3745 } else if (enable == 0) {
3746 sensor->info_priv.enable = 0;
3747 #if CONFIG_SENSOR_Focus
3748 flush_workqueue(sensor->sensor_wq);
3754 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3755 * this wasn't our capture interface, so, we wait for the right one */
3756 static int sensor_video_probe(struct soc_camera_device *icd,
3757 struct i2c_client *client)
3761 struct sensor *sensor = to_sensor(client);
3763 /* We must have a parent by now. And it cannot be a wrong one.
3764 * So this entire test is completely redundant. */
3765 if (!icd->dev.parent ||
3766 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3769 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3771 goto sensor_video_probe_err;
3774 ret = sensor_write(client, 0x3008, 0x80);
3776 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3778 goto sensor_video_probe_err;
3780 mdelay(5); //delay 5 microseconds
3782 /* check if it is an sensor sensor */
3783 ret = sensor_read(client, 0x300a, &value);
3785 SENSOR_TR("read chip id high byte failed\n");
3787 goto sensor_video_probe_err;
3790 pid |= (value << 8);
3792 ret = sensor_read(client, 0x300b, &value);
3794 SENSOR_TR("read chip id low byte failed\n");
3796 goto sensor_video_probe_err;
3799 pid |= (value & 0xff);
3800 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3801 if (pid == SENSOR_ID) {
3802 sensor->model = SENSOR_V4L2_IDENT;
3804 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3806 goto sensor_video_probe_err;
3811 sensor_video_probe_err:
3814 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3816 struct i2c_client *client = v4l2_get_subdevdata(sd);
3817 struct soc_camera_device *icd = client->dev.platform_data;
3818 struct sensor *sensor = to_sensor(client);
3821 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3824 case RK29_CAM_SUBDEV_DEACTIVATE:
3826 sensor_deactivate(client);
3829 case RK29_CAM_SUBDEV_IOREQUEST:
3831 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3832 if (sensor->sensor_io_request != NULL) {
3833 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
3834 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
3835 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
3836 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
3837 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
3838 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
3841 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3843 goto sensor_ioctl_end;
3845 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3847 #if CONFIG_SENSOR_Flash
3848 if (sensor->sensor_gpio_res) {
3849 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3850 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3852 for (i = 0; i < icd->ops->num_controls; i++) {
3853 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3854 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3857 sensor->info_priv.flash = 0xff;
3858 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3866 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3875 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3876 enum v4l2_mbus_pixelcode *code)
3878 if (index >= ARRAY_SIZE(sensor_colour_fmts))
3881 *code = sensor_colour_fmts[index].code;
3884 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3885 .init = sensor_init,
3886 .g_ctrl = sensor_g_control,
3887 .s_ctrl = sensor_s_control,
3888 .g_ext_ctrls = sensor_g_ext_controls,
3889 .s_ext_ctrls = sensor_s_ext_controls,
3890 .g_chip_ident = sensor_g_chip_ident,
3891 .ioctl = sensor_ioctl,
3894 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3895 .s_mbus_fmt = sensor_s_fmt,
3896 .g_mbus_fmt = sensor_g_fmt,
3897 .try_mbus_fmt = sensor_try_fmt,
3898 .enum_mbus_fmt = sensor_enum_fmt,
3899 .s_stream = sensor_s_stream,
3902 static struct v4l2_subdev_ops sensor_subdev_ops = {
3903 .core = &sensor_subdev_core_ops,
3904 .video = &sensor_subdev_video_ops,
3907 static int sensor_probe(struct i2c_client *client,
3908 const struct i2c_device_id *did)
3910 struct sensor *sensor;
3911 struct soc_camera_device *icd = client->dev.platform_data;
3912 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3913 struct soc_camera_link *icl;
3916 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3918 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3922 icl = to_soc_camera_link(icd);
3924 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3928 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3929 dev_warn(&adapter->dev,
3930 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3934 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3938 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3940 /* Second stage probe - when a capture adapter is there */
3941 icd->ops = &sensor_ops;
3942 sensor->info_priv.fmt = sensor_colour_fmts[0];
3943 #if CONFIG_SENSOR_I2C_NOSCHED
3944 atomic_set(&sensor->tasklock_cnt,0);
3947 ret = sensor_video_probe(icd, client);
3950 i2c_set_clientdata(client, NULL);
3954 #if CONFIG_SENSOR_Focus
3955 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
3956 if (sensor->sensor_wq == NULL)
3957 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
3958 mutex_init(&sensor->wq_lock);
3962 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3966 static int sensor_remove(struct i2c_client *client)
3968 struct sensor *sensor = to_sensor(client);
3969 struct soc_camera_device *icd = client->dev.platform_data;
3971 #if CONFIG_SENSOR_Focus
3972 if (sensor->sensor_wq) {
3973 destroy_workqueue(sensor->sensor_wq);
3974 sensor->sensor_wq = NULL;
3979 i2c_set_clientdata(client, NULL);
3980 client->driver = NULL;
3986 static const struct i2c_device_id sensor_id[] = {
3987 {SENSOR_NAME_STRING(), 0 },
3990 MODULE_DEVICE_TABLE(i2c, sensor_id);
3992 static struct i2c_driver sensor_i2c_driver = {
3994 .name = SENSOR_NAME_STRING(),
3996 .probe = sensor_probe,
3997 .remove = sensor_remove,
3998 .id_table = sensor_id,
4001 static int __init sensor_mod_init(void)
4003 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4004 return i2c_add_driver(&sensor_i2c_driver);
4007 static void __exit sensor_mod_exit(void)
4009 i2c_del_driver(&sensor_i2c_driver);
4012 device_initcall_sync(sensor_mod_init);
4013 module_exit(sensor_mod_exit);
4015 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4016 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4017 MODULE_LICENSE("GPL");