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 <plat/rk_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 400*1000 /* 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 3
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 //flash off in fixed time to prevent from too hot , zyc
182 struct soc_camera_device *icd;
183 struct hrtimer timer;
185 static enum hrtimer_restart flash_off_func(struct hrtimer *timer);
187 static struct flash_timer flash_off_timer;
188 //for user defined if user want to customize the series , zyc
189 #ifdef CONFIG_OV5640_USER_DEFINED_SERIES
190 #include "ov5640_user_series.c"
193 /* init 800X600 SVGA */
194 static struct reginfo sensor_init_data[] =
197 // {0x3008, 0x82}, //don't do software reset here, or writing some registers may fail
454 /* 720p 15fps @ 1280x720 */
456 static struct reginfo sensor_720p[]=
484 {0x3820, 0x41}, //ddl@rock-chips.com add start: qsxvga -> 720p isn't stream on
505 {0x4713, 0x03},//////ddl@rock-chips.com add end
521 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
523 static struct reginfo sensor_1080p[]=
529 /* 2592X1944 QSXGA */
530 static struct reginfo sensor_qsxga[] =
571 static struct reginfo sensor_qxga[] =
613 static struct reginfo sensor_uxga[] =
655 static struct reginfo sensor_sxga[] =
696 static struct reginfo sensor_xga[] =
736 static struct reginfo sensor_svga[] =
782 static struct reginfo sensor_vga[] =
788 static struct reginfo sensor_cif[] =
795 static struct reginfo sensor_qvga[] =
802 static struct reginfo sensor_qcif[] =
808 static struct reginfo sensor_ClrFmt_YUYV[]=
815 static struct reginfo sensor_ClrFmt_UYVY[]=
823 #if CONFIG_SENSOR_WhiteBalance
824 static struct reginfo sensor_WhiteB_Auto[]=
851 /* Cloudy Colour Temperature : 6500K - 8000K */
852 static struct reginfo sensor_WhiteB_Cloudy[]=
863 /* ClearDay Colour Temperature : 5000K - 6500K */
864 static struct reginfo sensor_WhiteB_ClearDay[]=
875 /* Office Colour Temperature : 3500K - 5000K */
876 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
887 /* Home Colour Temperature : 2500K - 3500K */
888 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
899 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
900 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
904 #if CONFIG_SENSOR_Brightness
905 static struct reginfo sensor_Brightness0[]=
909 static struct reginfo sensor_Brightness1[]=
914 static struct reginfo sensor_Brightness2[]=
918 static struct reginfo sensor_Brightness3[]=
922 static struct reginfo sensor_Brightness4[]=
927 static struct reginfo sensor_Brightness5[]=
931 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
932 sensor_Brightness4, sensor_Brightness5,NULL,
937 #if CONFIG_SENSOR_Effect
938 static struct reginfo sensor_Effect_Normal[] =
944 static struct reginfo sensor_Effect_WandB[] =
952 static struct reginfo sensor_Effect_Sepia[] =
961 static struct reginfo sensor_Effect_Negative[] =
967 };static struct reginfo sensor_Effect_Bluish[] =
977 static struct reginfo sensor_Effect_Green[] =
986 /*static struct reginfo sensor_Effect_Reddish[] =
996 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
997 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
1000 #if CONFIG_SENSOR_Exposure
1001 static struct reginfo sensor_Exposure0[]=
1009 {SEQUENCE_END, 0x00}
1011 static struct reginfo sensor_Exposure1[]=
1019 {SEQUENCE_END, 0x00}
1021 static struct reginfo sensor_Exposure2[]=
1029 {SEQUENCE_END, 0x00}
1032 static struct reginfo sensor_Exposure3[]=
1040 {SEQUENCE_END, 0x00}
1042 static struct reginfo sensor_Exposure4[]=
1050 {SEQUENCE_END, 0x00}
1052 static struct reginfo sensor_Exposure5[]=
1060 {SEQUENCE_END, 0x00}
1062 static struct reginfo sensor_Exposure6[]=
1070 {SEQUENCE_END, 0x00}
1072 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1073 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1076 #if CONFIG_SENSOR_Saturation
1077 static struct reginfo sensor_Saturation0[]=
1079 {SEQUENCE_END, 0x00}
1082 static struct reginfo sensor_Saturation1[]=
1084 {SEQUENCE_END, 0x00}
1087 static struct reginfo sensor_Saturation2[]=
1089 {SEQUENCE_END, 0x00}
1090 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1093 #if CONFIG_SENSOR_Contrast
1094 static struct reginfo sensor_Contrast0[]=
1096 {SEQUENCE_END, 0x00}
1099 static struct reginfo sensor_Contrast1[]=
1101 {SEQUENCE_END, 0x00}
1103 static struct reginfo sensor_Contrast2[]=
1105 {SEQUENCE_END, 0x00}
1108 static struct reginfo sensor_Contrast3[]=
1110 {SEQUENCE_END, 0x00}
1113 static struct reginfo sensor_Contrast4[]=
1115 {SEQUENCE_END, 0x00}
1119 static struct reginfo sensor_Contrast5[]=
1121 {SEQUENCE_END, 0x00}
1124 static struct reginfo sensor_Contrast6[]=
1126 {SEQUENCE_END, 0x00}
1128 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1129 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1133 #if CONFIG_SENSOR_Mirror
1134 static struct reginfo sensor_MirrorOn[]=
1136 {SEQUENCE_END, 0x00}
1138 static struct reginfo sensor_MirrorOff[]=
1140 {SEQUENCE_END, 0x00}
1142 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1144 #if CONFIG_SENSOR_Flip
1145 static struct reginfo sensor_FlipOn[]=
1147 {SEQUENCE_END, 0x00}
1150 static struct reginfo sensor_FlipOff[]=
1152 {SEQUENCE_END, 0x00}
1154 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1157 #if CONFIG_SENSOR_Scene
1158 static struct reginfo sensor_SceneAuto[] =
1161 {SEQUENCE_END, 0x00}
1163 static struct reginfo sensor_SceneNight[] =
1165 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1183 {SEQUENCE_END, 0x00}
1185 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1188 #if CONFIG_SENSOR_DigitalZoom
1189 static struct reginfo sensor_Zoom0[] =
1191 {SEQUENCE_END, 0x00}
1193 static struct reginfo sensor_Zoom1[] =
1195 {SEQUENCE_END, 0x00}
1198 static struct reginfo sensor_Zoom2[] =
1200 {SEQUENCE_END, 0x00}
1204 static struct reginfo sensor_Zoom3[] =
1206 {SEQUENCE_END, 0x00}
1208 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1210 static const struct v4l2_querymenu sensor_menus[] =
1212 #if CONFIG_SENSOR_WhiteBalance
1213 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1214 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1215 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1218 #if CONFIG_SENSOR_Effect
1219 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1220 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1221 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1224 #if CONFIG_SENSOR_Scene
1225 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1228 #if CONFIG_SENSOR_Flash
1229 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1230 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1234 static struct v4l2_queryctrl sensor_controls[] =
1236 #if CONFIG_SENSOR_WhiteBalance
1238 .id = V4L2_CID_DO_WHITE_BALANCE,
1239 .type = V4L2_CTRL_TYPE_MENU,
1240 .name = "White Balance Control",
1248 #if CONFIG_SENSOR_Brightness
1250 .id = V4L2_CID_BRIGHTNESS,
1251 .type = V4L2_CTRL_TYPE_INTEGER,
1252 .name = "Brightness Control",
1260 #if CONFIG_SENSOR_Effect
1262 .id = V4L2_CID_EFFECT,
1263 .type = V4L2_CTRL_TYPE_MENU,
1264 .name = "Effect Control",
1272 #if CONFIG_SENSOR_Exposure
1274 .id = V4L2_CID_EXPOSURE,
1275 .type = V4L2_CTRL_TYPE_INTEGER,
1276 .name = "Exposure Control",
1284 #if CONFIG_SENSOR_Saturation
1286 .id = V4L2_CID_SATURATION,
1287 .type = V4L2_CTRL_TYPE_INTEGER,
1288 .name = "Saturation Control",
1296 #if CONFIG_SENSOR_Contrast
1298 .id = V4L2_CID_CONTRAST,
1299 .type = V4L2_CTRL_TYPE_INTEGER,
1300 .name = "Contrast Control",
1308 #if CONFIG_SENSOR_Mirror
1310 .id = V4L2_CID_HFLIP,
1311 .type = V4L2_CTRL_TYPE_BOOLEAN,
1312 .name = "Mirror Control",
1320 #if CONFIG_SENSOR_Flip
1322 .id = V4L2_CID_VFLIP,
1323 .type = V4L2_CTRL_TYPE_BOOLEAN,
1324 .name = "Flip Control",
1332 #if CONFIG_SENSOR_Scene
1334 .id = V4L2_CID_SCENE,
1335 .type = V4L2_CTRL_TYPE_MENU,
1336 .name = "Scene Control",
1344 #if CONFIG_SENSOR_DigitalZoom
1346 .id = V4L2_CID_ZOOM_RELATIVE,
1347 .type = V4L2_CTRL_TYPE_INTEGER,
1348 .name = "DigitalZoom Control",
1354 .id = V4L2_CID_ZOOM_ABSOLUTE,
1355 .type = V4L2_CTRL_TYPE_INTEGER,
1356 .name = "DigitalZoom Control",
1364 #if CONFIG_SENSOR_Focus
1366 .id = V4L2_CID_FOCUS_RELATIVE,
1367 .type = V4L2_CTRL_TYPE_INTEGER,
1368 .name = "Focus Control",
1374 .id = V4L2_CID_FOCUS_ABSOLUTE,
1375 .type = V4L2_CTRL_TYPE_INTEGER,
1376 .name = "Focus Control",
1380 .default_value = 125,
1383 .id = V4L2_CID_FOCUS_AUTO,
1384 .type = V4L2_CTRL_TYPE_BOOLEAN,
1385 .name = "Focus Control",
1391 .id = V4L2_CID_FOCUS_CONTINUOUS,
1392 .type = V4L2_CTRL_TYPE_BOOLEAN,
1393 .name = "Focus Control",
1401 #if CONFIG_SENSOR_Flash
1403 .id = V4L2_CID_FLASH,
1404 .type = V4L2_CTRL_TYPE_MENU,
1405 .name = "Flash Control",
1414 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1415 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1416 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1417 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1418 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1419 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1420 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1421 static int sensor_resume(struct soc_camera_device *icd);
1422 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1423 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1424 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1425 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1426 static int sensor_deactivate(struct i2c_client *client);
1427 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1428 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1430 static struct soc_camera_ops sensor_ops =
1432 .suspend = sensor_suspend,
1433 .resume = sensor_resume,
1434 .set_bus_param = sensor_set_bus_param,
1435 .query_bus_param = sensor_query_bus_param,
1436 .controls = sensor_controls,
1437 .menus = sensor_menus,
1438 .num_controls = ARRAY_SIZE(sensor_controls),
1439 .num_menus = ARRAY_SIZE(sensor_menus),
1441 /* only one fixed colorspace per pixelcode */
1442 struct sensor_datafmt {
1443 enum v4l2_mbus_pixelcode code;
1444 enum v4l2_colorspace colorspace;
1447 /* Find a data format by a pixel code in an array */
1448 static const struct sensor_datafmt *sensor_find_datafmt(
1449 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1453 for (i = 0; i < n; i++)
1454 if (fmt[i].code == code)
1460 static const struct sensor_datafmt sensor_colour_fmts[] = {
1461 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1462 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1468 WqCmd_af_special_pos,
1472 WqCmd_af_return_idle,
1474 enum sensor_wq_result
1482 struct i2c_client *client;
1483 struct delayed_work dwork;
1484 enum sensor_wq_cmd cmd;
1485 wait_queue_head_t done;
1486 enum sensor_wq_result result;
1491 typedef struct sensor_info_priv_s
1507 unsigned char mirror; /* HFLIP */
1508 unsigned char flip; /* VFLIP */
1509 struct reginfo *winseqe_cur_addr;
1510 struct sensor_datafmt fmt;
1511 unsigned int enable;
1512 unsigned int funmodule_state;
1513 } sensor_info_priv_t;
1517 struct sensor_parameter
1519 unsigned short int preview_maxlines;
1520 unsigned short int preview_exposure;
1521 unsigned short int preview_line_width;
1522 unsigned short int preview_gain;
1524 unsigned short int capture_framerate;
1525 unsigned short int preview_framerate;
1531 struct v4l2_subdev subdev;
1532 struct i2c_client *client;
1533 sensor_info_priv_t info_priv;
1534 struct sensor_parameter parameter;
1535 struct workqueue_struct *sensor_wq;
1536 struct mutex wq_lock;
1537 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1538 #if CONFIG_SENSOR_I2C_NOSCHED
1539 atomic_t tasklock_cnt;
1541 struct rk29camera_platform_data *sensor_io_request;
1542 struct rk29camera_gpio_res *sensor_gpio_res;
1545 static struct sensor* to_sensor(const struct i2c_client *client)
1547 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1550 static int sensor_task_lock(struct i2c_client *client, int lock)
1552 #if CONFIG_SENSOR_I2C_NOSCHED
1554 struct sensor *sensor = to_sensor(client);
1557 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1558 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1559 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1563 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1564 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1565 goto sensor_task_lock_err;
1570 atomic_add(1, &sensor->tasklock_cnt);
1572 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1573 atomic_sub(1, &sensor->tasklock_cnt);
1575 if (atomic_read(&sensor->tasklock_cnt) == 0)
1580 sensor_task_lock_err:
1589 #if CONFIG_SENSOR_WRITE_REGS
1590 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
1593 struct i2c_msg msg[1];
1596 msg->addr = client->addr;
1597 msg->flags = client->flags;
1598 msg->buf = reg_info;
1599 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1600 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1606 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1607 err = i2c_transfer(client->adapter, msg, 1);
1611 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
1625 /* sensor register write */
1626 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1630 struct i2c_msg msg[1];
1633 buf[1] = reg & 0xFF;
1636 msg->addr = client->addr;
1637 msg->flags = client->flags;
1639 msg->len = sizeof(buf);
1640 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1641 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1646 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1647 err = i2c_transfer(client->adapter, msg, 1);
1652 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1660 /* sensor register read */
1661 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1665 struct i2c_msg msg[2];
1668 buf[1] = reg & 0xFF;
1670 msg[0].addr = client->addr;
1671 msg[0].flags = client->flags;
1673 msg[0].len = sizeof(buf);
1674 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1675 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1677 msg[1].addr = client->addr;
1678 msg[1].flags = client->flags|I2C_M_RD;
1681 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1682 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1686 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1687 err = i2c_transfer(client->adapter, msg, 2);
1693 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1701 /* write a array of registers */
1702 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1706 #if CONFIG_SENSOR_Focus
1707 struct sensor *sensor = to_sensor(client);
1709 #if CONFIG_SENSOR_I2C_RDWRCHK
1712 #if CONFIG_SENSOR_WRITE_REGS
1719 if (sensor_task_lock(client, 1) < 0)
1720 goto sensor_write_array_end;
1721 while (regarray[i].reg != SEQUENCE_END) {
1722 #if CONFIG_SENSOR_Focus
1723 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1724 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1726 goto sensor_write_array_end;
1730 #if CONFIG_SENSOR_WRITE_REGS
1734 reg_length = 0x0001;
1736 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
1739 if(reg_num >= WRITE_REGS_NUM)
1746 size_num = reg_num + 1;
1748 ptemp = phead = (u8*)kmalloc((size_num+10)*sizeof(u8),GFP_KERNEL);
1750 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1752 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1754 *phead = regarray[j].reg >> 8;
1755 *(ptemp+1) = regarray[j].reg & 0xFF;
1757 for( ; reg_num > 0; reg_num --, j++) {
1758 *ptemp ++ = regarray[j].val;
1762 err = sensor_write_regs(client, ptemp,size_num);
1766 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1769 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1774 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1778 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1780 goto sensor_write_array_end;
1783 #if CONFIG_SENSOR_I2C_RDWRCHK
1784 sensor_read(client, regarray[i].reg, &valchk);
1785 if (valchk != regarray[i].val)
1786 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1792 #if CONFIG_SENSOR_Focus
1793 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1794 || (regarray == sensor_init_data)) {
1795 sensor->info_priv.affm_reinit = 1;
1799 sensor_write_array_end:
1800 sensor_task_lock(client,0);
1803 #if CONFIG_SENSOR_I2C_RDWRCHK
1804 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1812 while (regarray[i].reg != 0)
1814 sensor_read(client, regarray[i].reg, &valchk);
1815 if (valchk != regarray[i].val)
1816 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1823 #if CONFIG_SENSOR_Focus
1830 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1833 char read_tag=0xff,cnt;
1836 if (cmdinfo->validate_bit & 0x80) {
1837 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1838 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1839 goto sensor_af_cmdset_err;
1841 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1843 for (i=0; i<4; i++) {
1844 if (cmdinfo->validate_bit & (1<<i)) {
1845 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1846 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]);
1847 goto sensor_af_cmdset_err;
1849 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]);
1853 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
1854 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1855 goto sensor_af_cmdset_err;
1857 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1860 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1861 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1862 goto sensor_af_cmdset_err;
1869 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1870 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1873 } while((read_tag != 0x00)&& (cnt++<100));
1875 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1877 sensor_af_cmdset_err:
1881 static int sensor_af_idlechk(struct i2c_client *client)
1885 struct af_cmdinfo cmdinfo;
1887 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1889 cmdinfo.cmd_tag = 0x01;
1890 cmdinfo.validate_bit = 0x80;
1891 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1893 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1895 goto sensor_af_idlechk_end;
1900 ret = sensor_read(client, CMD_ACK_Reg, &state);
1902 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1904 goto sensor_af_idlechk_end;
1906 }while(0x00 != state);
1909 sensor_af_idlechk_end:
1913 static int sensor_af_single(struct i2c_client *client)
1917 struct af_cmdinfo cmdinfo;
1919 cmdinfo.cmd_tag = 0x01;
1920 cmdinfo.validate_bit = 0x80;
1921 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
1923 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1925 goto sensor_af_single_end;
1935 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1937 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1939 goto sensor_af_single_end;
1941 }while((state == S_FOCUSING) && (cnt<100));
1943 if (state != S_FOCUSED) {
1944 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1946 goto sensor_af_single_end;
1949 sensor_af_single_end:
1953 static int sensor_af_const(struct i2c_client *client)
1962 static int sensor_af_init(struct i2c_client *client)
1967 ret = sensor_write_array(client, sensor_af_firmware);
1969 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1971 goto sensor_af_init_end;
1980 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1982 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1984 goto sensor_af_init_end;
1986 } while (state != S_IDLE);
1988 if (state != S_IDLE) {
1989 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1991 goto sensor_af_init_end;
1995 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1999 static int sensor_af_downfirmware(struct i2c_client *client)
2001 struct sensor *sensor = to_sensor(client);
2003 struct soc_camera_device *icd = client->dev.platform_data;
2004 struct v4l2_mbus_framefmt mf;
2006 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
2008 if (sensor_af_init(client)) {
2009 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
2012 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
2014 mf.width = icd->user_width;
2015 mf.height = icd->user_height;
2016 mf.code = sensor->info_priv.fmt.code;
2017 mf.colorspace = sensor->info_priv.fmt.colorspace;
2018 mf.field = V4L2_FIELD_NONE;
2019 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
2020 ret = sensor_af_const(client);
2022 switch (sensor->info_priv.auto_focus)
2024 case SENSOR_AF_MODE_AUTO:
2026 ret = sensor_af_single(client);
2029 case SENSOR_AF_MODE_CLOSE:
2036 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
2037 goto sensor_af_downfirmware_end;
2041 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
2044 sensor_af_downfirmware_end:
2048 static void sensor_af_workqueue(struct work_struct *work)
2050 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
2051 struct i2c_client *client = sensor_work->client;
2052 struct sensor *sensor = to_sensor(client);
2053 struct af_cmdinfo cmdinfo;
2055 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
2057 mutex_lock(&sensor->wq_lock);
2059 switch (sensor_work->cmd)
2063 if (sensor_af_downfirmware(client) < 0) {
2064 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2068 case WqCmd_af_single:
2070 if (sensor_af_single(client) < 0) {
2071 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2072 sensor_work->result = WqRet_fail;
2074 sensor_work->result = WqRet_success;
2078 case WqCmd_af_special_pos:
2080 sensor_af_idlechk(client);
2082 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2083 cmdinfo.cmd_para[0] = sensor_work->var;
2084 cmdinfo.validate_bit = 0x81;
2085 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2086 sensor_work->result = WqRet_fail;
2088 sensor_work->result = WqRet_success;
2091 case WqCmd_af_near_pos:
2093 sensor_af_idlechk(client);
2094 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2095 cmdinfo.validate_bit = 0x80;
2096 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2097 sensor_work->result = WqRet_fail;
2099 sensor_work->result = WqRet_success;
2102 case WqCmd_af_far_pos:
2104 sensor_af_idlechk(client);
2105 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2106 cmdinfo.validate_bit = 0x80;
2107 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2108 sensor_work->result = WqRet_fail;
2110 sensor_work->result = WqRet_success;
2113 case WqCmd_af_continues:
2115 if (sensor_af_const(client) < 0)
2116 sensor_work->result = WqRet_fail;
2118 sensor_work->result = WqRet_success;
2121 case WqCmd_af_return_idle:
2123 if (sensor_af_idlechk(client) < 0)
2124 sensor_work->result = WqRet_fail;
2126 sensor_work->result = WqRet_success;
2130 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
2134 if (sensor_work->wait == false) {
2135 kfree((void*)sensor_work);
2137 wake_up(&sensor_work->done);
2139 mutex_unlock(&sensor->wq_lock);
2143 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait)
2145 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2146 struct sensor *sensor = to_sensor(client);
2147 struct sensor_work *wk;
2150 if (sensor->sensor_wq == NULL) {
2152 goto sensor_af_workqueue_set_end;
2155 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) != SENSOR_AF_IS_OK) {
2156 if (cmd != WqCmd_af_init) {
2157 SENSOR_TR("%s %s cmd(%d) ingore,because af module isn't ready!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2159 goto sensor_af_workqueue_set_end;
2163 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
2165 wk->client = client;
2166 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
2168 wk->result = WqRet_inval;
2171 init_waitqueue_head(&wk->done);
2173 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
2175 /* ddl@rock-chips.com:
2176 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
2177 * As a result any other ioctl calls will proceed very, very slowly since each call
2178 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
2179 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
2182 mutex_unlock(&icd->video_lock);
2183 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(5000)) == 0) { //hhb
2184 SENSOR_TR("%s %s cmd(%d) is timeout!\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2188 mutex_lock(&icd->video_lock);
2192 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2195 sensor_af_workqueue_set_end:
2199 static int sensor_parameter_record(struct i2c_client *client)
2201 u8 ret_l,ret_m,ret_h;
2203 struct sensor *sensor = to_sensor(client);
2205 sensor_write(client,0x3503,0x07); //stop AE/AG
2206 sensor_write(client,0x3406,0x01); //stop AWB
2208 sensor_read(client,0x3500,&ret_h);
2209 sensor_read(client,0x3501, &ret_m);
2210 sensor_read(client,0x3502, &ret_l);
2214 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
2215 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
2216 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
2218 sensor_read(client,0x350c, &ret_h);
2219 sensor_read(client,0x350d, &ret_l);
2220 sensor->parameter.preview_line_width = ret_h & 0xff;
2221 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
2222 //Read back AGC Gain for preview
2223 sensor_read(client,0x350a, &ret_h);
2224 sensor_read(client,0x350b, &tp_l);
2225 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
2227 sensor_read(client,0x380e, &ret_h);
2228 sensor->parameter.preview_maxlines = ret_h;
2229 sensor->parameter.preview_maxlines <<= 8;
2230 sensor_read(client,0x380f, &tp_l);
2231 sensor->parameter.preview_maxlines += tp_l;
2233 sensor->parameter.capture_framerate = 375;
2234 sensor->parameter.preview_framerate = 1500;
2236 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
2237 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2238 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2239 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2240 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2241 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2243 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);
2246 static int sensor_ae_transfer(struct i2c_client *client)
2251 u16 ulCapture_Exposure;
2252 u32 ulCapture_Exposure_Gain;
2256 u8 reg_l = 0,reg_h =0;
2257 u16 Preview_Maxlines;
2259 u32 Capture_MaxLines;
2260 struct sensor *sensor = to_sensor(client);
2262 //Preview_Maxlines = sensor->parameter.preview_line_width;
2263 Preview_Maxlines = sensor->parameter.preview_maxlines;
2264 Gain = sensor->parameter.preview_gain;
2266 sensor_read(client,0x350c, ®_h);
2267 sensor_read(client,0x350d, ®_l);
2268 Capture_MaxLines = reg_h & 0xff;
2269 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2272 sensor_read(client,0x380e, ®_h);
2273 Capture_MaxLines = reg_h;
2274 Capture_MaxLines <<= 8;
2275 sensor_read(client,0x380f, ®_l);
2276 Capture_MaxLines += reg_l;
2279 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2281 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2283 Lines_10ms = Lines_10ms & 0xffff;
2285 if(Preview_Maxlines == 0)
2286 Preview_Maxlines = 1;
2288 //ulCapture_Exposure =
2289 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2291 ulCapture_Exposure =
2292 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2293 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2295 iCapture_Gain = (Gain & 0x0f) + 16;
2297 iCapture_Gain = iCapture_Gain << 1;
2300 iCapture_Gain = iCapture_Gain << 1;
2303 iCapture_Gain = iCapture_Gain << 1;
2306 iCapture_Gain = iCapture_Gain << 1;
2309 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
2310 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2312 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2313 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2314 if (ulCapture_Exposure > Lines_10ms)
2316 //ulCapture_Exposure *= 1.7;
2317 ulCapture_Exposure /= Lines_10ms;
2318 ulCapture_Exposure *= Lines_10ms;
2321 ulCapture_Exposure = Capture_MaxLines;
2322 //ulCapture_Exposure_Gain *= 1.5;
2325 if(ulCapture_Exposure == 0)
2326 ulCapture_Exposure = 1;
2328 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2329 iCapture_Gain = iCapture_Gain & 0xffff;
2331 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2332 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2333 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2337 if (iCapture_Gain > 31) {
2339 iCapture_Gain = iCapture_Gain >> 1;
2341 if (iCapture_Gain > 31) {
2343 iCapture_Gain = iCapture_Gain >> 1;
2345 if (iCapture_Gain > 31) {
2347 iCapture_Gain = iCapture_Gain >> 1;
2349 if (iCapture_Gain > 31) {
2351 iCapture_Gain = iCapture_Gain >> 1;
2353 if (iCapture_Gain > 16)
2354 Gain |= ((iCapture_Gain -16) & 0x0f);
2357 // write the gain and exposure to 0x350* registers
2358 //m_iWrite0x350b=Gain;
2359 sensor_write(client,0x350b, Gain);
2360 Gain = (Gain >> 8) & 0x01;
2361 sensor_write(client,0x350a, Gain);
2362 //m_iWrite0x3502=ExposureLow;
2363 sensor_write(client,0x3502, ExposureLow);
2364 //m_iWrite0x3501=ExposureMid;
2365 sensor_write(client,0x3501, ExposureMid);
2366 //m_iWrite0x3500=ExposureHigh;
2367 sensor_write(client,0x3500, ExposureHigh);
2368 // SendToFile("Gain = 0x%x\r\n", Gain);
2369 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2370 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2371 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2372 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2373 //camera_timed_wait(200);
2374 //linzhk camera_timed_wait(500);
2376 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2377 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2378 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2379 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2380 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2381 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2383 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2387 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2389 struct soc_camera_link *icl = to_soc_camera_link(icd);
2392 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2396 case Sensor_PowerDown:
2398 if (icl->powerdown) {
2399 ret = icl->powerdown(icd->pdev, on);
2400 if (ret == RK29_CAM_IO_SUCCESS) {
2404 icl->reset(icd->pdev);
2406 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2408 goto sensor_power_end;
2415 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2416 struct sensor *sensor = to_sensor(client);
2418 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2419 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2421 //flash off after 2 secs
2422 hrtimer_cancel(&(flash_off_timer.timer));
2423 hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
2430 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2439 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
2440 struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
2441 sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
2442 SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
2447 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2449 struct i2c_client *client = v4l2_get_subdevdata(sd);
2450 struct soc_camera_device *icd = client->dev.platform_data;
2451 struct sensor *sensor = to_sensor(client);
2452 const struct v4l2_queryctrl *qctrl;
2453 const struct sensor_datafmt *fmt;
2457 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2459 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2461 goto sensor_INIT_ERR;
2465 if (sensor_task_lock(client,1)<0)
2466 goto sensor_INIT_ERR;
2467 ret = sensor_write(client, 0x3008, 0x80);
2469 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2471 goto sensor_INIT_ERR;
2474 mdelay(5); //delay 5 microseconds
2475 /* check if it is an sensor sensor */
2476 ret = sensor_read(client, 0x300a, &value);
2478 SENSOR_TR("read chip id high byte failed\n");
2480 goto sensor_INIT_ERR;
2483 pid |= (value << 8);
2485 ret = sensor_read(client, 0x300b, &value);
2487 SENSOR_TR("read chip id low byte failed\n");
2489 goto sensor_INIT_ERR;
2492 pid |= (value & 0xff);
2493 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2495 if (pid == SENSOR_ID) {
2496 sensor->model = SENSOR_V4L2_IDENT;
2498 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2500 goto sensor_INIT_ERR;
2503 ret = sensor_write_array(client, sensor_init_data);
2504 udelay(1000); //wait sensor power on,so that I2C write sensor registers would sucess hhb@rock-chips.con
2507 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2508 goto sensor_INIT_ERR;
2510 sensor_task_lock(client,0);
2511 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2512 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2514 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2516 goto sensor_INIT_ERR;
2518 sensor->info_priv.fmt = *fmt;
2520 /* sensor sensor information for initialization */
2521 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2523 sensor->info_priv.whiteBalance = qctrl->default_value;
2524 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2526 sensor->info_priv.brightness = qctrl->default_value;
2527 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2529 sensor->info_priv.effect = qctrl->default_value;
2530 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2532 sensor->info_priv.exposure = qctrl->default_value;
2534 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2536 sensor->info_priv.saturation = qctrl->default_value;
2537 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2539 sensor->info_priv.contrast = qctrl->default_value;
2540 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2542 sensor->info_priv.mirror = qctrl->default_value;
2543 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2545 sensor->info_priv.flip = qctrl->default_value;
2546 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2548 sensor->info_priv.scene = qctrl->default_value;
2549 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2551 sensor->info_priv.digitalzoom = qctrl->default_value;
2553 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2554 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2556 sensor->info_priv.focus = qctrl->default_value;
2558 #if CONFIG_SENSOR_Flash
2559 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2561 sensor->info_priv.flash = qctrl->default_value;
2562 flash_off_timer.icd = icd;
2563 flash_off_timer.timer.function = flash_off_func;
2565 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);
2567 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
2571 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2572 sensor_task_lock(client,0);
2573 sensor_deactivate(client);
2576 static int sensor_deactivate(struct i2c_client *client)
2578 struct soc_camera_device *icd = client->dev.platform_data;
2579 struct sensor *sensor = to_sensor(client);
2581 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2583 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2584 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2585 sensor_task_lock(client, 1);
2586 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2587 sensor_write(client, 0x3018, 0x03); // D5-D0
2588 sensor_write(client,0x3019,0x00); // STROBE,SDA
2589 sensor_task_lock(client, 0);
2591 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2593 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2594 icd->user_width = SENSOR_INIT_WIDTH;
2595 icd->user_height = SENSOR_INIT_HEIGHT;
2596 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2600 static struct reginfo sensor_power_down_sequence[]=
2605 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2608 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2610 if (pm_msg.event == PM_EVENT_SUSPEND) {
2611 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2612 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2614 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2617 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2619 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2624 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2631 static int sensor_resume(struct soc_camera_device *icd)
2635 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2637 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2641 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2645 static int sensor_set_bus_param(struct soc_camera_device *icd,
2646 unsigned long flags)
2652 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2654 struct soc_camera_link *icl = to_soc_camera_link(icd);
2655 unsigned long flags = SENSOR_BUS_PARAM;
2657 return soc_camera_apply_sensor_flags(icl, flags);
2660 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2662 struct i2c_client *client = v4l2_get_subdevdata(sd);
2663 struct soc_camera_device *icd = client->dev.platform_data;
2664 struct sensor *sensor = to_sensor(client);
2666 mf->width = icd->user_width;
2667 mf->height = icd->user_height;
2668 mf->code = sensor->info_priv.fmt.code;
2669 mf->colorspace = sensor->info_priv.fmt.colorspace;
2670 mf->field = V4L2_FIELD_NONE;
2674 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2678 if ((mf->width == 1024) && (mf->height == 768)) {
2680 } else if ((mf->width == 1280) && (mf->height == 1024)) {
2682 } else if ((mf->width == 1600) && (mf->height == 1200)) {
2684 } else if ((mf->width == 2048) && (mf->height == 1536)) {
2686 } else if ((mf->width == 2592) && (mf->height == 1944)) {
2691 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2695 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2699 if ((mf->width == 1280) && (mf->height == 720)) {
2701 } else if ((mf->width == 1920) && (mf->height == 1080)) {
2706 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2709 static int sensor_s_fmt(struct v4l2_subdev *sd,struct v4l2_mbus_framefmt *mf)
2711 struct i2c_client *client = v4l2_get_subdevdata(sd);
2712 const struct sensor_datafmt *fmt;
2713 struct sensor *sensor = to_sensor(client);
2714 const struct v4l2_queryctrl *qctrl;
2715 struct soc_camera_device *icd = client->dev.platform_data;
2716 struct reginfo *winseqe_set_addr=NULL;
2717 int ret = 0, set_w,set_h;
2719 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2720 ARRAY_SIZE(sensor_colour_fmts));
2723 goto sensor_s_fmt_end;
2726 if (sensor->info_priv.fmt.code != mf->code) {
2729 case V4L2_MBUS_FMT_YUYV8_2X8:
2731 winseqe_set_addr = sensor_ClrFmt_YUYV;
2734 case V4L2_MBUS_FMT_UYVY8_2X8:
2736 winseqe_set_addr = sensor_ClrFmt_UYVY;
2742 if (winseqe_set_addr != NULL) {
2743 sensor_write_array(client, winseqe_set_addr);
2744 sensor->info_priv.fmt.code = mf->code;
2745 sensor->info_priv.fmt.colorspace= mf->colorspace;
2746 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2748 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2755 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2757 winseqe_set_addr = sensor_qcif;
2761 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2763 winseqe_set_addr = sensor_qvga;
2767 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2769 winseqe_set_addr = sensor_cif;
2773 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2775 winseqe_set_addr = sensor_vga;
2779 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2781 winseqe_set_addr = sensor_svga;
2785 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2787 winseqe_set_addr = sensor_xga;
2791 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2793 winseqe_set_addr = sensor_720p;
2797 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2799 winseqe_set_addr = sensor_sxga;
2803 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2805 winseqe_set_addr = sensor_uxga;
2809 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2811 winseqe_set_addr = sensor_1080p;
2815 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2817 winseqe_set_addr = sensor_qxga;
2821 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2823 winseqe_set_addr = sensor_qsxga;
2829 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2830 set_w = SENSOR_INIT_WIDTH;
2831 set_h = SENSOR_INIT_HEIGHT;
2832 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2835 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2837 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2838 sensor_parameter_record(client);
2839 /*#if CONFIG_SENSOR_Focus
2840 sensor_af_idlechk(client);
2841 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2842 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2844 #if CONFIG_SENSOR_Flash
2845 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2846 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2847 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2850 }else { /* ddl@rock-chips.com : Video */
2851 #if CONFIG_SENSOR_Flash
2852 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2853 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2854 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2858 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2859 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2860 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2861 ret |= sensor_write_array(client,sensor_init_data);
2862 udelay(1000); //wait sensor power on,so that I2C write sensor registers would sucess hhb@rock-chips.con
2863 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2866 ret |= sensor_write_array(client, winseqe_set_addr);
2868 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2869 #if CONFIG_SENSOR_Flash
2870 if (sensor_fmt_capturechk(sd,mf) == true) {
2871 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2872 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2873 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2877 goto sensor_s_fmt_end;
2879 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2881 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2882 sensor_ae_transfer(client);
2883 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2884 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2885 if (sensor->info_priv.whiteBalance != 0) {
2886 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2887 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2889 sensor->info_priv.snap2preview = true;
2890 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2891 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2892 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2894 sensor->info_priv.video2preview = true;
2895 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2896 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2897 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2898 if (sensor->info_priv.snap2preview == true) {
2899 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2900 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2902 #if CONFIG_SENSOR_Focus
2903 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
2904 sensor_af_workqueue_set(icd,WqCmd_af_return_idle,0,true);
2912 sensor->info_priv.video2preview = false;
2913 sensor->info_priv.snap2preview = false;
2915 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2919 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2927 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2929 struct i2c_client *client = v4l2_get_subdevdata(sd);
2930 struct sensor *sensor = to_sensor(client);
2931 const struct sensor_datafmt *fmt;
2932 int ret = 0,set_w,set_h;
2934 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2935 ARRAY_SIZE(sensor_colour_fmts));
2937 fmt = &sensor->info_priv.fmt;
2938 mf->code = fmt->code;
2941 if (mf->height > SENSOR_MAX_HEIGHT)
2942 mf->height = SENSOR_MAX_HEIGHT;
2943 else if (mf->height < SENSOR_MIN_HEIGHT)
2944 mf->height = SENSOR_MIN_HEIGHT;
2946 if (mf->width > SENSOR_MAX_WIDTH)
2947 mf->width = SENSOR_MAX_WIDTH;
2948 else if (mf->width < SENSOR_MIN_WIDTH)
2949 mf->width = SENSOR_MIN_WIDTH;
2954 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2959 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2964 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2969 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2974 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2979 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2984 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2989 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2994 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2999 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
3004 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
3009 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
3016 set_w = SENSOR_INIT_WIDTH;
3017 set_h = SENSOR_INIT_HEIGHT;
3023 mf->colorspace = fmt->colorspace;
3028 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
3030 struct i2c_client *client = v4l2_get_subdevdata(sd);
3032 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
3035 if (id->match.addr != client->addr)
3038 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
3043 #if CONFIG_SENSOR_Brightness
3044 static int sensor_set_brightness(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_BrightnessSeqe[value - qctrl->minimum] != NULL)
3052 if (sensor_write_array(client, sensor_BrightnessSeqe[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_Effect
3066 static int sensor_set_effect(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_EffectSeqe[value - qctrl->minimum] != NULL)
3074 if (sensor_write_array(client, sensor_EffectSeqe[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_Exposure
3088 static int sensor_set_exposure(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));
3092 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3094 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
3096 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3098 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3101 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3105 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3109 #if CONFIG_SENSOR_Saturation
3110 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3112 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3114 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3116 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3118 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3120 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3123 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3127 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3131 #if CONFIG_SENSOR_Contrast
3132 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3134 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3136 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3138 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3140 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3142 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3145 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3149 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3153 #if CONFIG_SENSOR_Mirror
3154 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3156 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3158 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3160 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3162 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3164 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3167 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3171 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3175 #if CONFIG_SENSOR_Flip
3176 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3178 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3180 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3182 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3184 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3186 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3189 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3193 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3197 #if CONFIG_SENSOR_Scene
3198 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3200 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3202 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3204 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3206 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3208 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3211 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3215 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3219 #if CONFIG_SENSOR_WhiteBalance
3220 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3222 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3224 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3226 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3228 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3230 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3233 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3237 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3241 #if CONFIG_SENSOR_DigitalZoom
3242 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3244 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3245 struct sensor *sensor = to_sensor(client);
3246 const struct v4l2_queryctrl *qctrl_info;
3247 int digitalzoom_cur, digitalzoom_total;
3249 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3253 digitalzoom_cur = sensor->info_priv.digitalzoom;
3254 digitalzoom_total = qctrl_info->maximum;
3256 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3258 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3262 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3264 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3268 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3270 *value = digitalzoom_total - digitalzoom_cur;
3273 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3275 *value = 0 - digitalzoom_cur;
3278 digitalzoom_cur += *value;
3280 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3282 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3284 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3287 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3294 #if CONFIG_SENSOR_Focus
3295 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3297 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3298 struct sensor *sensor = to_sensor(client);
3299 const struct v4l2_queryctrl *qctrl_info;
3302 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3306 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3307 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3308 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true);
3309 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3312 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3316 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3317 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3322 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3324 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3325 struct sensor *sensor = to_sensor(client);
3326 const struct v4l2_queryctrl *qctrl_info;
3329 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3333 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3334 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3336 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true);
3338 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true);
3340 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3343 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3347 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3348 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3353 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3355 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3356 struct sensor *sensor = to_sensor(client);
3359 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3362 case SENSOR_AF_MODE_AUTO:
3364 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true);
3368 /*case SENSOR_AF_MODE_MACRO:
3370 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3374 case SENSOR_AF_MODE_INFINITY:
3376 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3380 case SENSOR_AF_MODE_CONTINUOUS:
3382 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true);
3386 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3391 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3394 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3395 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3401 #if CONFIG_SENSOR_Flash
3402 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3404 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3405 if (value == 3) { /* ddl@rock-chips.com: torch */
3406 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3408 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3410 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3414 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3418 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3420 struct i2c_client *client = v4l2_get_subdevdata(sd);
3421 struct sensor *sensor = to_sensor(client);
3422 const struct v4l2_queryctrl *qctrl;
3424 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3428 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3434 case V4L2_CID_BRIGHTNESS:
3436 ctrl->value = sensor->info_priv.brightness;
3439 case V4L2_CID_SATURATION:
3441 ctrl->value = sensor->info_priv.saturation;
3444 case V4L2_CID_CONTRAST:
3446 ctrl->value = sensor->info_priv.contrast;
3449 case V4L2_CID_DO_WHITE_BALANCE:
3451 ctrl->value = sensor->info_priv.whiteBalance;
3454 case V4L2_CID_EXPOSURE:
3456 ctrl->value = sensor->info_priv.exposure;
3459 case V4L2_CID_HFLIP:
3461 ctrl->value = sensor->info_priv.mirror;
3464 case V4L2_CID_VFLIP:
3466 ctrl->value = sensor->info_priv.flip;
3477 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3479 struct i2c_client *client = v4l2_get_subdevdata(sd);
3480 struct sensor *sensor = to_sensor(client);
3481 struct soc_camera_device *icd = client->dev.platform_data;
3482 const struct v4l2_queryctrl *qctrl;
3485 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3489 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3495 #if CONFIG_SENSOR_Brightness
3496 case V4L2_CID_BRIGHTNESS:
3498 if (ctrl->value != sensor->info_priv.brightness)
3500 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3504 sensor->info_priv.brightness = ctrl->value;
3509 #if CONFIG_SENSOR_Exposure
3510 case V4L2_CID_EXPOSURE:
3512 if (ctrl->value != sensor->info_priv.exposure)
3514 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3518 sensor->info_priv.exposure = ctrl->value;
3523 #if CONFIG_SENSOR_Saturation
3524 case V4L2_CID_SATURATION:
3526 if (ctrl->value != sensor->info_priv.saturation)
3528 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3532 sensor->info_priv.saturation = ctrl->value;
3537 #if CONFIG_SENSOR_Contrast
3538 case V4L2_CID_CONTRAST:
3540 if (ctrl->value != sensor->info_priv.contrast)
3542 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3546 sensor->info_priv.contrast = ctrl->value;
3551 #if CONFIG_SENSOR_WhiteBalance
3552 case V4L2_CID_DO_WHITE_BALANCE:
3554 if (ctrl->value != sensor->info_priv.whiteBalance)
3556 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3560 sensor->info_priv.whiteBalance = ctrl->value;
3565 #if CONFIG_SENSOR_Mirror
3566 case V4L2_CID_HFLIP:
3568 if (ctrl->value != sensor->info_priv.mirror)
3570 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3572 sensor->info_priv.mirror = ctrl->value;
3577 #if CONFIG_SENSOR_Flip
3578 case V4L2_CID_VFLIP:
3580 if (ctrl->value != sensor->info_priv.flip)
3582 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3584 sensor->info_priv.flip = ctrl->value;
3595 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3597 const struct v4l2_queryctrl *qctrl;
3598 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3599 struct sensor *sensor = to_sensor(client);
3601 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3605 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3609 switch (ext_ctrl->id)
3611 case V4L2_CID_SCENE:
3613 ext_ctrl->value = sensor->info_priv.scene;
3616 case V4L2_CID_EFFECT:
3618 ext_ctrl->value = sensor->info_priv.effect;
3621 case V4L2_CID_ZOOM_ABSOLUTE:
3623 ext_ctrl->value = sensor->info_priv.digitalzoom;
3626 case V4L2_CID_ZOOM_RELATIVE:
3630 case V4L2_CID_FOCUS_ABSOLUTE:
3634 case V4L2_CID_FOCUS_RELATIVE:
3638 case V4L2_CID_FLASH:
3640 ext_ctrl->value = sensor->info_priv.flash;
3648 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3650 const struct v4l2_queryctrl *qctrl;
3651 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3652 struct sensor *sensor = to_sensor(client);
3655 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3659 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3664 switch (ext_ctrl->id)
3666 #if CONFIG_SENSOR_Scene
3667 case V4L2_CID_SCENE:
3669 if (ext_ctrl->value != sensor->info_priv.scene)
3671 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3673 sensor->info_priv.scene = ext_ctrl->value;
3678 #if CONFIG_SENSOR_Effect
3679 case V4L2_CID_EFFECT:
3681 if (ext_ctrl->value != sensor->info_priv.effect)
3683 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3685 sensor->info_priv.effect= ext_ctrl->value;
3690 #if CONFIG_SENSOR_DigitalZoom
3691 case V4L2_CID_ZOOM_ABSOLUTE:
3693 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3696 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3698 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3700 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3702 sensor->info_priv.digitalzoom += val_offset;
3704 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3709 case V4L2_CID_ZOOM_RELATIVE:
3711 if (ext_ctrl->value)
3713 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3715 sensor->info_priv.digitalzoom += ext_ctrl->value;
3717 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3722 #if CONFIG_SENSOR_Focus
3723 case V4L2_CID_FOCUS_ABSOLUTE:
3725 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3730 case V4L2_CID_FOCUS_RELATIVE:
3732 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3735 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3738 case V4L2_CID_FOCUS_AUTO:
3740 if (ext_ctrl->value == 1) {
3741 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
3742 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
3743 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3747 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3748 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3749 if (ext_ctrl->value == 0)
3750 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3754 case V4L2_CID_FOCUS_CONTINUOUS:
3756 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3757 if (ext_ctrl->value == 1) {
3758 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3760 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3763 if (ext_ctrl->value == 0)
3764 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3769 #if CONFIG_SENSOR_Flash
3770 case V4L2_CID_FLASH:
3772 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3774 sensor->info_priv.flash = ext_ctrl->value;
3776 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3787 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3789 struct i2c_client *client = v4l2_get_subdevdata(sd);
3790 struct soc_camera_device *icd = client->dev.platform_data;
3791 int i, error_cnt=0, error_idx=-1;
3794 for (i=0; i<ext_ctrl->count; i++) {
3795 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3802 error_idx = ext_ctrl->count;
3804 if (error_idx != -1) {
3805 ext_ctrl->error_idx = error_idx;
3812 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3814 struct i2c_client *client = v4l2_get_subdevdata(sd);
3815 struct soc_camera_device *icd = client->dev.platform_data;
3816 int i, error_cnt=0, error_idx=-1;
3818 for (i=0; i<ext_ctrl->count; i++) {
3819 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3826 error_idx = ext_ctrl->count;
3828 if (error_idx != -1) {
3829 ext_ctrl->error_idx = error_idx;
3836 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3838 struct i2c_client *client = v4l2_get_subdevdata(sd);
3839 struct sensor *sensor = to_sensor(client);
3840 #if CONFIG_SENSOR_Focus
3841 struct soc_camera_device *icd = client->dev.platform_data;
3842 struct v4l2_mbus_framefmt mf;
3846 sensor->info_priv.enable = 1;
3847 #if CONFIG_SENSOR_Focus
3848 mf.width = icd->user_width;
3849 mf.height = icd->user_height;
3850 mf.code = sensor->info_priv.fmt.code;
3851 mf.colorspace = sensor->info_priv.fmt.colorspace;
3852 mf.field = V4L2_FIELD_NONE;
3853 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3854 if (sensor_fmt_capturechk(sd, &mf) == false) {
3855 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3856 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false);
3857 sensor->info_priv.affm_reinit = 0;
3861 } else if (enable == 0) {
3862 sensor->info_priv.enable = 0;
3863 #if CONFIG_SENSOR_Focus
3864 flush_workqueue(sensor->sensor_wq);
3870 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3871 * this wasn't our capture interface, so, we wait for the right one */
3872 static int sensor_video_probe(struct soc_camera_device *icd,
3873 struct i2c_client *client)
3877 struct sensor *sensor = to_sensor(client);
3879 /* We must have a parent by now. And it cannot be a wrong one.
3880 * So this entire test is completely redundant. */
3881 if (!icd->dev.parent ||
3882 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3885 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3887 goto sensor_video_probe_err;
3890 ret = sensor_write(client, 0x3008, 0x80);
3892 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3894 goto sensor_video_probe_err;
3896 mdelay(5); //delay 5 microseconds
3898 /* check if it is an sensor sensor */
3899 ret = sensor_read(client, 0x300a, &value);
3901 SENSOR_TR("read chip id high byte failed\n");
3903 goto sensor_video_probe_err;
3906 pid |= (value << 8);
3908 ret = sensor_read(client, 0x300b, &value);
3910 SENSOR_TR("read chip id low byte failed\n");
3912 goto sensor_video_probe_err;
3915 pid |= (value & 0xff);
3916 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3917 if (pid == SENSOR_ID) {
3918 sensor->model = SENSOR_V4L2_IDENT;
3920 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3922 goto sensor_video_probe_err;
3927 sensor_video_probe_err:
3930 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3932 struct i2c_client *client = v4l2_get_subdevdata(sd);
3933 struct soc_camera_device *icd = client->dev.platform_data;
3934 struct sensor *sensor = to_sensor(client);
3937 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3940 case RK29_CAM_SUBDEV_DEACTIVATE:
3942 #if CONFIG_SENSOR_Flash //hhb
3943 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3945 sensor_deactivate(client);
3948 case RK29_CAM_SUBDEV_IOREQUEST:
3950 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3951 if (sensor->sensor_io_request != NULL) {
3952 sensor->sensor_gpio_res = NULL;
3953 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
3954 if (sensor->sensor_io_request->gpio_res[i].dev_name &&
3955 (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
3956 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
3959 if (sensor->sensor_gpio_res == NULL) {
3960 SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
3962 goto sensor_ioctl_end;
3965 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3967 goto sensor_ioctl_end;
3969 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3971 #if CONFIG_SENSOR_Flash
3972 if (sensor->sensor_gpio_res) {
3973 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3974 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3975 for (i = 0; i < icd->ops->num_controls; i++) {
3976 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3977 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3978 sensor_controls[i].id=0xffff;
3981 sensor->info_priv.flash = 0xff;
3982 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3983 }else{ //two cameras are the same,need to deal diffrently ,zyc
3984 for (i = 0; i < icd->ops->num_controls; i++) {
3985 if(0xffff == icd->ops->controls[i].id){
3986 sensor_controls[i].id=V4L2_CID_FLASH;
3996 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4005 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
4006 enum v4l2_mbus_pixelcode *code)
4008 if (index >= ARRAY_SIZE(sensor_colour_fmts))
4011 *code = sensor_colour_fmts[index].code;
4014 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
4015 .init = sensor_init,
4016 .g_ctrl = sensor_g_control,
4017 .s_ctrl = sensor_s_control,
4018 .g_ext_ctrls = sensor_g_ext_controls,
4019 .s_ext_ctrls = sensor_s_ext_controls,
4020 .g_chip_ident = sensor_g_chip_ident,
4021 .ioctl = sensor_ioctl,
4024 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
4025 .s_mbus_fmt = sensor_s_fmt,
4026 .g_mbus_fmt = sensor_g_fmt,
4027 .try_mbus_fmt = sensor_try_fmt,
4028 .enum_mbus_fmt = sensor_enum_fmt,
4029 .s_stream = sensor_s_stream,
4032 static struct v4l2_subdev_ops sensor_subdev_ops = {
4033 .core = &sensor_subdev_core_ops,
4034 .video = &sensor_subdev_video_ops,
4037 static int sensor_probe(struct i2c_client *client,
4038 const struct i2c_device_id *did)
4040 struct sensor *sensor;
4041 struct soc_camera_device *icd = client->dev.platform_data;
4042 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
4043 struct soc_camera_link *icl;
4046 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
4048 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
4052 icl = to_soc_camera_link(icd);
4054 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
4058 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
4059 dev_warn(&adapter->dev,
4060 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
4064 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
4068 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
4070 /* Second stage probe - when a capture adapter is there */
4071 icd->ops = &sensor_ops;
4072 sensor->info_priv.fmt = sensor_colour_fmts[0];
4073 #if CONFIG_SENSOR_I2C_NOSCHED
4074 atomic_set(&sensor->tasklock_cnt,0);
4077 ret = sensor_video_probe(icd, client);
4080 i2c_set_clientdata(client, NULL);
4084 #if CONFIG_SENSOR_Focus
4085 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
4086 if (sensor->sensor_wq == NULL)
4087 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
4088 mutex_init(&sensor->wq_lock);
4091 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4092 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
4096 static int sensor_remove(struct i2c_client *client)
4098 struct sensor *sensor = to_sensor(client);
4099 struct soc_camera_device *icd = client->dev.platform_data;
4101 #if CONFIG_SENSOR_Focus
4102 if (sensor->sensor_wq) {
4103 destroy_workqueue(sensor->sensor_wq);
4104 sensor->sensor_wq = NULL;
4109 i2c_set_clientdata(client, NULL);
4110 client->driver = NULL;
4116 static const struct i2c_device_id sensor_id[] = {
4117 {SENSOR_NAME_STRING(), 0 },
4120 MODULE_DEVICE_TABLE(i2c, sensor_id);
4122 static struct i2c_driver sensor_i2c_driver = {
4124 .name = SENSOR_NAME_STRING(),
4126 .probe = sensor_probe,
4127 .remove = sensor_remove,
4128 .id_table = sensor_id,
4131 static int __init sensor_mod_init(void)
4133 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4134 return i2c_add_driver(&sensor_i2c_driver);
4137 static void __exit sensor_mod_exit(void)
4139 i2c_del_driver(&sensor_i2c_driver);
4142 device_initcall_sync(sensor_mod_init);
4143 module_exit(sensor_mod_exit);
4145 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4146 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4147 MODULE_LICENSE("GPL");