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 0
66 #define CONFIG_SENSOR_Flash 1
67 #define CONFIG_SENSOR_Mirror 0
68 #define CONFIG_SENSOR_Flip 0
69 #ifdef CONFIG_OV5640_AUTOFOCUS
70 #define CONFIG_SENSOR_Focus 1
71 #include "ov5640_af_firmware.c"
73 #define CONFIG_SENSOR_Focus 0
76 #define CONFIG_SENSOR_I2C_SPEED 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 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 #if CONFIG_OV5640_USER_DEFINED_SERIES
190 #include "ov5640_user_series.c"
193 /* init 800X600 SVGA */
194 static struct reginfo sensor_init_data[] =
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[]=
814 static struct reginfo sensor_ClrFmt_UYVY[]=
821 #if CONFIG_SENSOR_WhiteBalance
822 static struct reginfo sensor_WhiteB_Auto[]=
849 /* Cloudy Colour Temperature : 6500K - 8000K */
850 static struct reginfo sensor_WhiteB_Cloudy[]=
861 /* ClearDay Colour Temperature : 5000K - 6500K */
862 static struct reginfo sensor_WhiteB_ClearDay[]=
873 /* Office Colour Temperature : 3500K - 5000K */
874 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
885 /* Home Colour Temperature : 2500K - 3500K */
886 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
897 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
898 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
902 #if CONFIG_SENSOR_Brightness
903 static struct reginfo sensor_Brightness0[]=
907 static struct reginfo sensor_Brightness1[]=
912 static struct reginfo sensor_Brightness2[]=
916 static struct reginfo sensor_Brightness3[]=
920 static struct reginfo sensor_Brightness4[]=
925 static struct reginfo sensor_Brightness5[]=
929 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
930 sensor_Brightness4, sensor_Brightness5,NULL,
935 #if CONFIG_SENSOR_Effect
936 static struct reginfo sensor_Effect_Normal[] =
942 static struct reginfo sensor_Effect_WandB[] =
950 static struct reginfo sensor_Effect_Sepia[] =
959 static struct reginfo sensor_Effect_Negative[] =
965 };static struct reginfo sensor_Effect_Bluish[] =
975 static struct reginfo sensor_Effect_Green[] =
984 /*static struct reginfo sensor_Effect_Reddish[] =
994 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
995 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
998 #if CONFIG_SENSOR_Exposure
999 static struct reginfo sensor_Exposure0[]=
1001 {SEQUENCE_END, 0x00}
1003 static struct reginfo sensor_Exposure1[]=
1005 {SEQUENCE_END, 0x00}
1007 static struct reginfo sensor_Exposure2[]=
1009 {SEQUENCE_END, 0x00}
1012 static struct reginfo sensor_Exposure3[]=
1014 {SEQUENCE_END, 0x00}
1016 static struct reginfo sensor_Exposure4[]=
1018 {SEQUENCE_END, 0x00}
1020 static struct reginfo sensor_Exposure5[]=
1022 {SEQUENCE_END, 0x00}
1024 static struct reginfo sensor_Exposure6[]=
1026 {SEQUENCE_END, 0x00}
1028 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1029 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1032 #if CONFIG_SENSOR_Saturation
1033 static struct reginfo sensor_Saturation0[]=
1035 {SEQUENCE_END, 0x00}
1038 static struct reginfo sensor_Saturation1[]=
1040 {SEQUENCE_END, 0x00}
1043 static struct reginfo sensor_Saturation2[]=
1045 {SEQUENCE_END, 0x00}
1046 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1049 #if CONFIG_SENSOR_Contrast
1050 static struct reginfo sensor_Contrast0[]=
1052 {SEQUENCE_END, 0x00}
1055 static struct reginfo sensor_Contrast1[]=
1057 {SEQUENCE_END, 0x00}
1059 static struct reginfo sensor_Contrast2[]=
1061 {SEQUENCE_END, 0x00}
1064 static struct reginfo sensor_Contrast3[]=
1066 {SEQUENCE_END, 0x00}
1069 static struct reginfo sensor_Contrast4[]=
1071 {SEQUENCE_END, 0x00}
1075 static struct reginfo sensor_Contrast5[]=
1077 {SEQUENCE_END, 0x00}
1080 static struct reginfo sensor_Contrast6[]=
1082 {SEQUENCE_END, 0x00}
1084 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1085 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1089 #if CONFIG_SENSOR_Mirror
1090 static struct reginfo sensor_MirrorOn[]=
1092 {SEQUENCE_END, 0x00}
1094 static struct reginfo sensor_MirrorOff[]=
1096 {SEQUENCE_END, 0x00}
1098 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1100 #if CONFIG_SENSOR_Flip
1101 static struct reginfo sensor_FlipOn[]=
1103 {SEQUENCE_END, 0x00}
1106 static struct reginfo sensor_FlipOff[]=
1108 {SEQUENCE_END, 0x00}
1110 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1113 #if CONFIG_SENSOR_Scene
1114 static struct reginfo sensor_SceneAuto[] =
1117 {SEQUENCE_END, 0x00}
1119 static struct reginfo sensor_SceneNight[] =
1121 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1139 {SEQUENCE_END, 0x00}
1141 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1144 #if CONFIG_SENSOR_DigitalZoom
1145 static struct reginfo sensor_Zoom0[] =
1147 {SEQUENCE_END, 0x00}
1149 static struct reginfo sensor_Zoom1[] =
1151 {SEQUENCE_END, 0x00}
1154 static struct reginfo sensor_Zoom2[] =
1156 {SEQUENCE_END, 0x00}
1160 static struct reginfo sensor_Zoom3[] =
1162 {SEQUENCE_END, 0x00}
1164 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1166 static const struct v4l2_querymenu sensor_menus[] =
1168 #if CONFIG_SENSOR_WhiteBalance
1169 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1170 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1171 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1174 #if CONFIG_SENSOR_Effect
1175 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1176 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1177 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1180 #if CONFIG_SENSOR_Scene
1181 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1184 #if CONFIG_SENSOR_Flash
1185 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1186 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1190 static struct v4l2_queryctrl sensor_controls[] =
1192 #if CONFIG_SENSOR_WhiteBalance
1194 .id = V4L2_CID_DO_WHITE_BALANCE,
1195 .type = V4L2_CTRL_TYPE_MENU,
1196 .name = "White Balance Control",
1204 #if CONFIG_SENSOR_Brightness
1206 .id = V4L2_CID_BRIGHTNESS,
1207 .type = V4L2_CTRL_TYPE_INTEGER,
1208 .name = "Brightness Control",
1216 #if CONFIG_SENSOR_Effect
1218 .id = V4L2_CID_EFFECT,
1219 .type = V4L2_CTRL_TYPE_MENU,
1220 .name = "Effect Control",
1228 #if CONFIG_SENSOR_Exposure
1230 .id = V4L2_CID_EXPOSURE,
1231 .type = V4L2_CTRL_TYPE_INTEGER,
1232 .name = "Exposure Control",
1240 #if CONFIG_SENSOR_Saturation
1242 .id = V4L2_CID_SATURATION,
1243 .type = V4L2_CTRL_TYPE_INTEGER,
1244 .name = "Saturation Control",
1252 #if CONFIG_SENSOR_Contrast
1254 .id = V4L2_CID_CONTRAST,
1255 .type = V4L2_CTRL_TYPE_INTEGER,
1256 .name = "Contrast Control",
1264 #if CONFIG_SENSOR_Mirror
1266 .id = V4L2_CID_HFLIP,
1267 .type = V4L2_CTRL_TYPE_BOOLEAN,
1268 .name = "Mirror Control",
1276 #if CONFIG_SENSOR_Flip
1278 .id = V4L2_CID_VFLIP,
1279 .type = V4L2_CTRL_TYPE_BOOLEAN,
1280 .name = "Flip Control",
1288 #if CONFIG_SENSOR_Scene
1290 .id = V4L2_CID_SCENE,
1291 .type = V4L2_CTRL_TYPE_MENU,
1292 .name = "Scene Control",
1300 #if CONFIG_SENSOR_DigitalZoom
1302 .id = V4L2_CID_ZOOM_RELATIVE,
1303 .type = V4L2_CTRL_TYPE_INTEGER,
1304 .name = "DigitalZoom Control",
1310 .id = V4L2_CID_ZOOM_ABSOLUTE,
1311 .type = V4L2_CTRL_TYPE_INTEGER,
1312 .name = "DigitalZoom Control",
1320 #if CONFIG_SENSOR_Focus
1322 .id = V4L2_CID_FOCUS_RELATIVE,
1323 .type = V4L2_CTRL_TYPE_INTEGER,
1324 .name = "Focus Control",
1330 .id = V4L2_CID_FOCUS_ABSOLUTE,
1331 .type = V4L2_CTRL_TYPE_INTEGER,
1332 .name = "Focus Control",
1336 .default_value = 125,
1339 .id = V4L2_CID_FOCUS_AUTO,
1340 .type = V4L2_CTRL_TYPE_BOOLEAN,
1341 .name = "Focus Control",
1347 .id = V4L2_CID_FOCUS_CONTINUOUS,
1348 .type = V4L2_CTRL_TYPE_BOOLEAN,
1349 .name = "Focus Control",
1357 #if CONFIG_SENSOR_Flash
1359 .id = V4L2_CID_FLASH,
1360 .type = V4L2_CTRL_TYPE_MENU,
1361 .name = "Flash Control",
1370 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1371 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1372 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1373 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1374 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1375 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1376 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1377 static int sensor_resume(struct soc_camera_device *icd);
1378 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1379 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1380 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1381 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1382 static int sensor_deactivate(struct i2c_client *client);
1383 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1384 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1386 static struct soc_camera_ops sensor_ops =
1388 .suspend = sensor_suspend,
1389 .resume = sensor_resume,
1390 .set_bus_param = sensor_set_bus_param,
1391 .query_bus_param = sensor_query_bus_param,
1392 .controls = sensor_controls,
1393 .menus = sensor_menus,
1394 .num_controls = ARRAY_SIZE(sensor_controls),
1395 .num_menus = ARRAY_SIZE(sensor_menus),
1397 /* only one fixed colorspace per pixelcode */
1398 struct sensor_datafmt {
1399 enum v4l2_mbus_pixelcode code;
1400 enum v4l2_colorspace colorspace;
1403 /* Find a data format by a pixel code in an array */
1404 static const struct sensor_datafmt *sensor_find_datafmt(
1405 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1409 for (i = 0; i < n; i++)
1410 if (fmt[i].code == code)
1416 static const struct sensor_datafmt sensor_colour_fmts[] = {
1417 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1418 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1424 WqCmd_af_special_pos,
1428 WqCmd_af_return_idle,
1430 enum sensor_wq_result
1438 struct i2c_client *client;
1439 struct delayed_work dwork;
1440 enum sensor_wq_cmd cmd;
1441 wait_queue_head_t done;
1442 enum sensor_wq_result result;
1447 typedef struct sensor_info_priv_s
1463 unsigned char mirror; /* HFLIP */
1464 unsigned char flip; /* VFLIP */
1465 struct reginfo *winseqe_cur_addr;
1466 struct sensor_datafmt fmt;
1467 unsigned int enable;
1468 unsigned int funmodule_state;
1469 } sensor_info_priv_t;
1473 struct sensor_parameter
1475 unsigned short int preview_maxlines;
1476 unsigned short int preview_exposure;
1477 unsigned short int preview_line_width;
1478 unsigned short int preview_gain;
1480 unsigned short int capture_framerate;
1481 unsigned short int preview_framerate;
1487 struct v4l2_subdev subdev;
1488 struct i2c_client *client;
1489 sensor_info_priv_t info_priv;
1490 struct sensor_parameter parameter;
1491 struct workqueue_struct *sensor_wq;
1492 struct mutex wq_lock;
1493 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1494 #if CONFIG_SENSOR_I2C_NOSCHED
1495 atomic_t tasklock_cnt;
1497 struct rk29camera_platform_data *sensor_io_request;
1498 struct rk29camera_gpio_res *sensor_gpio_res;
1501 static struct sensor* to_sensor(const struct i2c_client *client)
1503 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1506 static int sensor_task_lock(struct i2c_client *client, int lock)
1508 #if CONFIG_SENSOR_I2C_NOSCHED
1510 struct sensor *sensor = to_sensor(client);
1513 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1514 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1515 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1519 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1520 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1521 goto sensor_task_lock_err;
1526 atomic_add(1, &sensor->tasklock_cnt);
1528 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1529 atomic_sub(1, &sensor->tasklock_cnt);
1531 if (atomic_read(&sensor->tasklock_cnt) == 0)
1536 sensor_task_lock_err:
1545 #if CONFIG_SENSOR_WRITE_REGS
1546 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
1549 struct i2c_msg msg[1];
1552 msg->addr = client->addr;
1553 msg->flags = client->flags;
1554 msg->buf = reg_info;
1555 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1556 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1562 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1563 err = i2c_transfer(client->adapter, msg, 1);
1567 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
1581 /* sensor register write */
1582 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1586 struct i2c_msg msg[1];
1589 buf[1] = reg & 0xFF;
1592 msg->addr = client->addr;
1593 msg->flags = client->flags;
1595 msg->len = sizeof(buf);
1596 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1597 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1602 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1603 err = i2c_transfer(client->adapter, msg, 1);
1608 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1616 /* sensor register read */
1617 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1621 struct i2c_msg msg[2];
1624 buf[1] = reg & 0xFF;
1626 msg[0].addr = client->addr;
1627 msg[0].flags = client->flags;
1629 msg[0].len = sizeof(buf);
1630 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1631 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1633 msg[1].addr = client->addr;
1634 msg[1].flags = client->flags|I2C_M_RD;
1637 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1638 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1642 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1643 err = i2c_transfer(client->adapter, msg, 2);
1649 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1657 /* write a array of registers */
1658 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1662 #if CONFIG_SENSOR_Focus
1663 struct sensor *sensor = to_sensor(client);
1665 #if CONFIG_SENSOR_I2C_RDWRCHK
1668 #if CONFIG_SENSOR_WRITE_REGS
1675 if (sensor_task_lock(client, 1) < 0)
1676 goto sensor_write_array_end;
1677 while (regarray[i].reg != SEQUENCE_END) {
1678 #if CONFIG_SENSOR_Focus
1679 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1680 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1682 goto sensor_write_array_end;
1686 #if CONFIG_SENSOR_WRITE_REGS
1690 reg_length = 0x0001;
1692 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
1695 if(reg_num >= WRITE_REGS_NUM)
1702 size_num = reg_num + 1;
1704 ptemp = phead = (u8*)kmalloc((size_num+10)*sizeof(u8),GFP_KERNEL);
1706 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1708 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1710 *phead = regarray[j].reg >> 8;
1711 *(ptemp+1) = regarray[j].reg & 0xFF;
1713 for( ; reg_num > 0; reg_num --, j++) {
1714 *ptemp ++ = regarray[j].val;
1718 err = sensor_write_regs(client, ptemp,size_num);
1722 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1725 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1730 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1734 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1736 goto sensor_write_array_end;
1739 #if CONFIG_SENSOR_I2C_RDWRCHK
1740 sensor_read(client, regarray[i].reg, &valchk);
1741 if (valchk != regarray[i].val)
1742 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1748 #if CONFIG_SENSOR_Focus
1749 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1750 || (regarray == sensor_init_data)) {
1751 sensor->info_priv.affm_reinit = 1;
1755 sensor_write_array_end:
1756 sensor_task_lock(client,0);
1759 #if CONFIG_SENSOR_I2C_RDWRCHK
1760 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1768 while (regarray[i].reg != 0)
1770 sensor_read(client, regarray[i].reg, &valchk);
1771 if (valchk != regarray[i].val)
1772 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1779 #if CONFIG_SENSOR_Focus
1786 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1789 char read_tag=0xff,cnt;
1792 if (cmdinfo->validate_bit & 0x80) {
1793 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1794 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1795 goto sensor_af_cmdset_err;
1797 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1799 for (i=0; i<4; i++) {
1800 if (cmdinfo->validate_bit & (1<<i)) {
1801 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1802 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]);
1803 goto sensor_af_cmdset_err;
1805 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]);
1809 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
1810 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1811 goto sensor_af_cmdset_err;
1813 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1816 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1817 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1818 goto sensor_af_cmdset_err;
1825 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1826 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1829 } while((read_tag != 0x00)&& (cnt++<100));
1831 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1833 sensor_af_cmdset_err:
1837 static int sensor_af_idlechk(struct i2c_client *client)
1841 struct af_cmdinfo cmdinfo;
1843 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1845 cmdinfo.cmd_tag = 0x01;
1846 cmdinfo.validate_bit = 0x80;
1847 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1849 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1851 goto sensor_af_idlechk_end;
1856 ret = sensor_read(client, CMD_ACK_Reg, &state);
1858 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1860 goto sensor_af_idlechk_end;
1862 }while(0x00 != state);
1865 sensor_af_idlechk_end:
1869 static int sensor_af_single(struct i2c_client *client)
1873 struct af_cmdinfo cmdinfo;
1875 cmdinfo.cmd_tag = 0x01;
1876 cmdinfo.validate_bit = 0x80;
1877 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
1879 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1881 goto sensor_af_single_end;
1891 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1893 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1895 goto sensor_af_single_end;
1897 }while((state == S_FOCUSING) && (cnt<100));
1899 if (state != S_FOCUSED) {
1900 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1902 goto sensor_af_single_end;
1905 sensor_af_single_end:
1909 static int sensor_af_const(struct i2c_client *client)
1918 static int sensor_af_init(struct i2c_client *client)
1923 ret = sensor_write_array(client, sensor_af_firmware);
1925 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1927 goto sensor_af_init_end;
1937 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1939 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1941 goto sensor_af_init_end;
1943 }while((state == S_STARTUP) && (cnt<100));
1945 if (state != S_IDLE) {
1946 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1948 goto sensor_af_init_end;
1952 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1956 static int sensor_af_downfirmware(struct i2c_client *client)
1958 struct sensor *sensor = to_sensor(client);
1960 struct soc_camera_device *icd = client->dev.platform_data;
1961 struct v4l2_mbus_framefmt mf;
1963 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1965 if (sensor_af_init(client)) {
1966 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1969 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1971 mf.width = icd->user_width;
1972 mf.height = icd->user_height;
1973 mf.code = sensor->info_priv.fmt.code;
1974 mf.colorspace = sensor->info_priv.fmt.colorspace;
1975 mf.field = V4L2_FIELD_NONE;
1976 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
1977 ret = sensor_af_const(client);
1979 switch (sensor->info_priv.auto_focus)
1981 case SENSOR_AF_MODE_AUTO:
1983 ret = sensor_af_single(client);
1986 case SENSOR_AF_MODE_CLOSE:
1993 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1994 goto sensor_af_downfirmware_end;
1998 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
2001 sensor_af_downfirmware_end:
2005 static void sensor_af_workqueue(struct work_struct *work)
2007 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
2008 struct i2c_client *client = sensor_work->client;
2009 struct sensor *sensor = to_sensor(client);
2010 struct af_cmdinfo cmdinfo;
2012 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
2014 mutex_lock(&sensor->wq_lock);
2016 switch (sensor_work->cmd)
2020 if (sensor_af_downfirmware(client) < 0) {
2021 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2025 case WqCmd_af_single:
2027 if (sensor_af_single(client) < 0) {
2028 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2029 sensor_work->result = WqRet_fail;
2031 sensor_work->result = WqRet_success;
2035 case WqCmd_af_special_pos:
2037 sensor_af_idlechk(client);
2039 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2040 cmdinfo.cmd_para[0] = sensor_work->var;
2041 cmdinfo.validate_bit = 0x81;
2042 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2043 sensor_work->result = WqRet_fail;
2045 sensor_work->result = WqRet_success;
2048 case WqCmd_af_near_pos:
2050 sensor_af_idlechk(client);
2051 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2052 cmdinfo.validate_bit = 0x80;
2053 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2054 sensor_work->result = WqRet_fail;
2056 sensor_work->result = WqRet_success;
2059 case WqCmd_af_far_pos:
2061 sensor_af_idlechk(client);
2062 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2063 cmdinfo.validate_bit = 0x80;
2064 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2065 sensor_work->result = WqRet_fail;
2067 sensor_work->result = WqRet_success;
2070 case WqCmd_af_continues:
2072 if (sensor_af_const(client) < 0)
2073 sensor_work->result = WqRet_fail;
2075 sensor_work->result = WqRet_success;
2078 case WqCmd_af_return_idle:
2080 if (sensor_af_idlechk(client) < 0)
2081 sensor_work->result = WqRet_fail;
2083 sensor_work->result = WqRet_success;
2087 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
2091 if (sensor_work->wait == false) {
2092 kfree((void*)sensor_work);
2094 wake_up(&sensor_work->done);
2096 mutex_unlock(&sensor->wq_lock);
2100 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait)
2102 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2103 struct sensor *sensor = to_sensor(client);
2104 struct sensor_work *wk;
2107 if (sensor->sensor_wq == NULL) {
2109 goto sensor_af_workqueue_set_end;
2112 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) != SENSOR_AF_IS_OK) {
2113 if (cmd != WqCmd_af_init) {
2114 SENSOR_TR("%s %s cmd(%d) ingore,because af module isn't ready!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2116 goto sensor_af_workqueue_set_end;
2120 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
2122 wk->client = client;
2123 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
2125 wk->result = WqRet_inval;
2128 init_waitqueue_head(&wk->done);
2130 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
2132 /* ddl@rock-chips.com:
2133 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
2134 * As a result any other ioctl calls will proceed very, very slowly since each call
2135 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
2136 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
2139 mutex_unlock(&icd->video_lock);
2140 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(2500)) == 0) {
2141 SENSOR_TR("%s %s cmd(%d) is timeout!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2145 mutex_lock(&icd->video_lock);
2149 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2152 sensor_af_workqueue_set_end:
2156 static int sensor_parameter_record(struct i2c_client *client)
2158 u8 ret_l,ret_m,ret_h;
2160 struct sensor *sensor = to_sensor(client);
2162 sensor_write(client,0x3503,0x07); //stop AE/AG
2163 sensor_write(client,0x3406,0x01); //stop AWB
2165 sensor_read(client,0x3500,&ret_h);
2166 sensor_read(client,0x3501, &ret_m);
2167 sensor_read(client,0x3502, &ret_l);
2171 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
2172 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
2173 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
2175 sensor_read(client,0x350c, &ret_h);
2176 sensor_read(client,0x350d, &ret_l);
2177 sensor->parameter.preview_line_width = ret_h & 0xff;
2178 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
2179 //Read back AGC Gain for preview
2180 sensor_read(client,0x350a, &ret_h);
2181 sensor_read(client,0x350b, &tp_l);
2182 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
2184 sensor_read(client,0x380e, &ret_h);
2185 sensor->parameter.preview_maxlines = ret_h;
2186 sensor->parameter.preview_maxlines <<= 8;
2187 sensor_read(client,0x380f, &tp_l);
2188 sensor->parameter.preview_maxlines += tp_l;
2190 sensor->parameter.capture_framerate = 375;
2191 sensor->parameter.preview_framerate = 1500;
2193 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
2194 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2195 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2196 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2197 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2198 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2200 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);
2203 static int sensor_ae_transfer(struct i2c_client *client)
2208 u16 ulCapture_Exposure;
2209 u32 ulCapture_Exposure_Gain;
2213 u8 reg_l = 0,reg_h =0;
2214 u16 Preview_Maxlines;
2216 u32 Capture_MaxLines;
2217 struct sensor *sensor = to_sensor(client);
2219 //Preview_Maxlines = sensor->parameter.preview_line_width;
2220 Preview_Maxlines = sensor->parameter.preview_maxlines;
2221 Gain = sensor->parameter.preview_gain;
2223 sensor_read(client,0x350c, ®_h);
2224 sensor_read(client,0x350d, ®_l);
2225 Capture_MaxLines = reg_h & 0xff;
2226 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2229 sensor_read(client,0x380e, ®_h);
2230 Capture_MaxLines = reg_h;
2231 Capture_MaxLines <<= 8;
2232 sensor_read(client,0x380f, ®_l);
2233 Capture_MaxLines += reg_l;
2236 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2238 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2240 Lines_10ms = Lines_10ms & 0xffff;
2242 if(Preview_Maxlines == 0)
2243 Preview_Maxlines = 1;
2245 //ulCapture_Exposure =
2246 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2248 ulCapture_Exposure =
2249 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2250 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2252 iCapture_Gain = (Gain & 0x0f) + 16;
2254 iCapture_Gain = iCapture_Gain << 1;
2257 iCapture_Gain = iCapture_Gain << 1;
2260 iCapture_Gain = iCapture_Gain << 1;
2263 iCapture_Gain = iCapture_Gain << 1;
2266 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
2267 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2269 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2270 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2271 if (ulCapture_Exposure > Lines_10ms)
2273 //ulCapture_Exposure *= 1.7;
2274 ulCapture_Exposure /= Lines_10ms;
2275 ulCapture_Exposure *= Lines_10ms;
2278 ulCapture_Exposure = Capture_MaxLines;
2279 //ulCapture_Exposure_Gain *= 1.5;
2282 if(ulCapture_Exposure == 0)
2283 ulCapture_Exposure = 1;
2285 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2286 iCapture_Gain = iCapture_Gain & 0xffff;
2288 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2289 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2290 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2294 if (iCapture_Gain > 31) {
2296 iCapture_Gain = iCapture_Gain >> 1;
2298 if (iCapture_Gain > 31) {
2300 iCapture_Gain = iCapture_Gain >> 1;
2302 if (iCapture_Gain > 31) {
2304 iCapture_Gain = iCapture_Gain >> 1;
2306 if (iCapture_Gain > 31) {
2308 iCapture_Gain = iCapture_Gain >> 1;
2310 if (iCapture_Gain > 16)
2311 Gain |= ((iCapture_Gain -16) & 0x0f);
2314 // write the gain and exposure to 0x350* registers
2315 //m_iWrite0x350b=Gain;
2316 sensor_write(client,0x350b, Gain);
2317 Gain = (Gain >> 8) & 0x01;
2318 sensor_write(client,0x350a, Gain);
2319 //m_iWrite0x3502=ExposureLow;
2320 sensor_write(client,0x3502, ExposureLow);
2321 //m_iWrite0x3501=ExposureMid;
2322 sensor_write(client,0x3501, ExposureMid);
2323 //m_iWrite0x3500=ExposureHigh;
2324 sensor_write(client,0x3500, ExposureHigh);
2325 // SendToFile("Gain = 0x%x\r\n", Gain);
2326 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2327 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2328 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2329 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2330 //camera_timed_wait(200);
2331 //linzhk camera_timed_wait(500);
2333 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2334 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2335 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2336 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2337 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2338 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2340 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2344 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2346 struct soc_camera_link *icl = to_soc_camera_link(icd);
2349 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2353 case Sensor_PowerDown:
2355 if (icl->powerdown) {
2356 ret = icl->powerdown(icd->pdev, on);
2357 if (ret == RK29_CAM_IO_SUCCESS) {
2361 icl->reset(icd->pdev);
2363 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2365 goto sensor_power_end;
2372 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2373 struct sensor *sensor = to_sensor(client);
2375 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2376 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2378 //flash off after 2 secs
2379 hrtimer_cancel(&(flash_off_timer.timer));
2380 hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
2387 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2396 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
2397 struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
2398 sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
2399 SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
2404 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2406 struct i2c_client *client = v4l2_get_subdevdata(sd);
2407 struct soc_camera_device *icd = client->dev.platform_data;
2408 struct sensor *sensor = to_sensor(client);
2409 const struct v4l2_queryctrl *qctrl;
2410 const struct sensor_datafmt *fmt;
2414 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2416 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2418 goto sensor_INIT_ERR;
2422 if (sensor_task_lock(client,1)<0)
2423 goto sensor_INIT_ERR;
2424 ret = sensor_write(client, 0x3008, 0x80);
2426 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2428 goto sensor_INIT_ERR;
2431 mdelay(5); //delay 5 microseconds
2432 /* check if it is an sensor sensor */
2433 ret = sensor_read(client, 0x300a, &value);
2435 SENSOR_TR("read chip id high byte failed\n");
2437 goto sensor_INIT_ERR;
2440 pid |= (value << 8);
2442 ret = sensor_read(client, 0x300b, &value);
2444 SENSOR_TR("read chip id low byte failed\n");
2446 goto sensor_INIT_ERR;
2449 pid |= (value & 0xff);
2450 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2452 if (pid == SENSOR_ID) {
2453 sensor->model = SENSOR_V4L2_IDENT;
2455 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2457 goto sensor_INIT_ERR;
2460 ret = sensor_write_array(client, sensor_init_data);
2462 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2463 goto sensor_INIT_ERR;
2465 sensor_task_lock(client,0);
2466 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2467 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2469 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2471 goto sensor_INIT_ERR;
2473 sensor->info_priv.fmt = *fmt;
2475 /* sensor sensor information for initialization */
2476 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2478 sensor->info_priv.whiteBalance = qctrl->default_value;
2479 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2481 sensor->info_priv.brightness = qctrl->default_value;
2482 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2484 sensor->info_priv.effect = qctrl->default_value;
2485 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2487 sensor->info_priv.exposure = qctrl->default_value;
2489 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2491 sensor->info_priv.saturation = qctrl->default_value;
2492 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2494 sensor->info_priv.contrast = qctrl->default_value;
2495 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2497 sensor->info_priv.mirror = qctrl->default_value;
2498 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2500 sensor->info_priv.flip = qctrl->default_value;
2501 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2503 sensor->info_priv.scene = qctrl->default_value;
2504 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2506 sensor->info_priv.digitalzoom = qctrl->default_value;
2508 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2509 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2511 sensor->info_priv.focus = qctrl->default_value;
2513 #if CONFIG_SENSOR_Flash
2514 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2516 sensor->info_priv.flash = qctrl->default_value;
2517 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2518 flash_off_timer.icd = icd;
2519 flash_off_timer.timer.function = flash_off_func;
2521 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);
2523 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
2527 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2528 sensor_task_lock(client,0);
2529 sensor_deactivate(client);
2532 static int sensor_deactivate(struct i2c_client *client)
2534 struct soc_camera_device *icd = client->dev.platform_data;
2535 struct sensor *sensor = to_sensor(client);
2537 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2539 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2540 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2541 sensor_task_lock(client, 1);
2542 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2543 sensor_write(client, 0x3018, 0x03); // D5-D0
2544 sensor_write(client,0x3019,0x00); // STROBE,SDA
2545 sensor_task_lock(client, 0);
2547 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2549 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2550 icd->user_width = SENSOR_INIT_WIDTH;
2551 icd->user_height = SENSOR_INIT_HEIGHT;
2552 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2556 static struct reginfo sensor_power_down_sequence[]=
2561 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2564 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2566 if (pm_msg.event == PM_EVENT_SUSPEND) {
2567 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2568 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2570 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2573 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2575 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2580 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2587 static int sensor_resume(struct soc_camera_device *icd)
2591 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2593 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2597 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2601 static int sensor_set_bus_param(struct soc_camera_device *icd,
2602 unsigned long flags)
2608 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2610 struct soc_camera_link *icl = to_soc_camera_link(icd);
2611 unsigned long flags = SENSOR_BUS_PARAM;
2613 return soc_camera_apply_sensor_flags(icl, flags);
2616 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2618 struct i2c_client *client = v4l2_get_subdevdata(sd);
2619 struct soc_camera_device *icd = client->dev.platform_data;
2620 struct sensor *sensor = to_sensor(client);
2622 mf->width = icd->user_width;
2623 mf->height = icd->user_height;
2624 mf->code = sensor->info_priv.fmt.code;
2625 mf->colorspace = sensor->info_priv.fmt.colorspace;
2626 mf->field = V4L2_FIELD_NONE;
2630 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2634 if ((mf->width == 1024) && (mf->height == 768)) {
2636 } else if ((mf->width == 1280) && (mf->height == 1024)) {
2638 } else if ((mf->width == 1600) && (mf->height == 1200)) {
2640 } else if ((mf->width == 2048) && (mf->height == 1536)) {
2642 } else if ((mf->width == 2592) && (mf->height == 1944)) {
2647 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2651 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2655 if ((mf->width == 1280) && (mf->height == 720)) {
2657 } else if ((mf->width == 1920) && (mf->height == 1080)) {
2662 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2665 static int sensor_s_fmt(struct v4l2_subdev *sd,struct v4l2_mbus_framefmt *mf)
2667 struct i2c_client *client = v4l2_get_subdevdata(sd);
2668 const struct sensor_datafmt *fmt;
2669 struct sensor *sensor = to_sensor(client);
2670 const struct v4l2_queryctrl *qctrl;
2671 struct soc_camera_device *icd = client->dev.platform_data;
2672 struct reginfo *winseqe_set_addr=NULL;
2673 int ret = 0, set_w,set_h;
2675 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2676 ARRAY_SIZE(sensor_colour_fmts));
2679 goto sensor_s_fmt_end;
2682 if (sensor->info_priv.fmt.code != mf->code) {
2685 case V4L2_MBUS_FMT_YUYV8_2X8:
2687 winseqe_set_addr = sensor_ClrFmt_YUYV;
2690 case V4L2_MBUS_FMT_UYVY8_2X8:
2692 winseqe_set_addr = sensor_ClrFmt_UYVY;
2698 if (winseqe_set_addr != NULL) {
2699 sensor_write_array(client, winseqe_set_addr);
2700 sensor->info_priv.fmt.code = mf->code;
2701 sensor->info_priv.fmt.colorspace= mf->colorspace;
2702 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2704 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2711 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2713 winseqe_set_addr = sensor_qcif;
2717 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2719 winseqe_set_addr = sensor_qvga;
2723 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2725 winseqe_set_addr = sensor_cif;
2729 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2731 winseqe_set_addr = sensor_vga;
2735 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2737 winseqe_set_addr = sensor_svga;
2741 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2743 winseqe_set_addr = sensor_xga;
2747 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2749 winseqe_set_addr = sensor_720p;
2753 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2755 winseqe_set_addr = sensor_sxga;
2759 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2761 winseqe_set_addr = sensor_uxga;
2765 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2767 winseqe_set_addr = sensor_1080p;
2771 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2773 winseqe_set_addr = sensor_qxga;
2777 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2779 winseqe_set_addr = sensor_qsxga;
2785 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2786 set_w = SENSOR_INIT_WIDTH;
2787 set_h = SENSOR_INIT_HEIGHT;
2788 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2791 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2793 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2794 sensor_parameter_record(client);
2795 /*#if CONFIG_SENSOR_Focus
2796 sensor_af_idlechk(client);
2797 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2798 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2800 #if CONFIG_SENSOR_Flash
2801 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2802 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2803 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2806 }else { /* ddl@rock-chips.com : Video */
2807 #if CONFIG_SENSOR_Flash
2808 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2809 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2810 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2814 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2815 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2816 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2817 ret |= sensor_write_array(client,sensor_init_data);
2818 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2821 ret |= sensor_write_array(client, winseqe_set_addr);
2823 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2824 #if CONFIG_SENSOR_Flash
2825 if (sensor_fmt_capturechk(sd,mf) == true) {
2826 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2827 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2828 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2832 goto sensor_s_fmt_end;
2834 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2836 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2837 sensor_ae_transfer(client);
2838 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2839 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2840 if (sensor->info_priv.whiteBalance != 0) {
2841 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2842 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2844 sensor->info_priv.snap2preview = true;
2845 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2846 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2847 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2849 sensor->info_priv.video2preview = true;
2850 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2851 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2852 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2853 if (sensor->info_priv.snap2preview == true) {
2854 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2855 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2857 #if CONFIG_SENSOR_Focus
2858 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
2859 sensor_af_workqueue_set(icd,WqCmd_af_return_idle,0,true);
2867 sensor->info_priv.video2preview = false;
2868 sensor->info_priv.snap2preview = false;
2870 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2874 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2882 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2884 struct i2c_client *client = v4l2_get_subdevdata(sd);
2885 struct sensor *sensor = to_sensor(client);
2886 const struct sensor_datafmt *fmt;
2887 int ret = 0,set_w,set_h;
2889 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2890 ARRAY_SIZE(sensor_colour_fmts));
2892 fmt = &sensor->info_priv.fmt;
2893 mf->code = fmt->code;
2896 if (mf->height > SENSOR_MAX_HEIGHT)
2897 mf->height = SENSOR_MAX_HEIGHT;
2898 else if (mf->height < SENSOR_MIN_HEIGHT)
2899 mf->height = SENSOR_MIN_HEIGHT;
2901 if (mf->width > SENSOR_MAX_WIDTH)
2902 mf->width = SENSOR_MAX_WIDTH;
2903 else if (mf->width < SENSOR_MIN_WIDTH)
2904 mf->width = SENSOR_MIN_WIDTH;
2909 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2914 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2919 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2924 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2929 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2934 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2939 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2944 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2949 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2954 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2959 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2964 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2971 set_w = SENSOR_INIT_WIDTH;
2972 set_h = SENSOR_INIT_HEIGHT;
2978 mf->colorspace = fmt->colorspace;
2983 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2985 struct i2c_client *client = v4l2_get_subdevdata(sd);
2987 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2990 if (id->match.addr != client->addr)
2993 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2998 #if CONFIG_SENSOR_Brightness
2999 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3001 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3003 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3005 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
3007 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
3009 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3012 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3016 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3020 #if CONFIG_SENSOR_Effect
3021 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3023 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3025 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3027 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
3029 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
3031 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3034 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3038 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3042 #if CONFIG_SENSOR_Exposure
3043 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3045 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3047 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3049 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
3051 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3053 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3056 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3060 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3064 #if CONFIG_SENSOR_Saturation
3065 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3067 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3069 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3071 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3073 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3075 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3078 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3082 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3086 #if CONFIG_SENSOR_Contrast
3087 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3089 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3091 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3093 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3095 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3097 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3100 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3104 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3108 #if CONFIG_SENSOR_Mirror
3109 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3111 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3113 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3115 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3117 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3119 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3122 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3126 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3130 #if CONFIG_SENSOR_Flip
3131 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3133 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3135 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3137 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3139 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3141 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3144 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3148 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3152 #if CONFIG_SENSOR_Scene
3153 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3155 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3157 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3159 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3161 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3163 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3166 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3170 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3174 #if CONFIG_SENSOR_WhiteBalance
3175 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3177 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3179 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3181 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3183 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3185 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3188 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3192 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3196 #if CONFIG_SENSOR_DigitalZoom
3197 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3199 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3200 struct sensor *sensor = to_sensor(client);
3201 const struct v4l2_queryctrl *qctrl_info;
3202 int digitalzoom_cur, digitalzoom_total;
3204 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3208 digitalzoom_cur = sensor->info_priv.digitalzoom;
3209 digitalzoom_total = qctrl_info->maximum;
3211 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3213 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3217 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3219 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3223 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3225 *value = digitalzoom_total - digitalzoom_cur;
3228 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3230 *value = 0 - digitalzoom_cur;
3233 digitalzoom_cur += *value;
3235 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3237 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3239 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3242 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3249 #if CONFIG_SENSOR_Focus
3250 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3252 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3253 struct sensor *sensor = to_sensor(client);
3254 const struct v4l2_queryctrl *qctrl_info;
3257 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3261 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3262 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3263 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true);
3264 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3267 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3271 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3272 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3277 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3279 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3280 struct sensor *sensor = to_sensor(client);
3281 const struct v4l2_queryctrl *qctrl_info;
3284 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3288 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3289 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3291 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true);
3293 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true);
3295 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3298 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3302 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3303 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3308 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3310 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3311 struct sensor *sensor = to_sensor(client);
3314 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3317 case SENSOR_AF_MODE_AUTO:
3319 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true);
3323 /*case SENSOR_AF_MODE_MACRO:
3325 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3329 case SENSOR_AF_MODE_INFINITY:
3331 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3335 case SENSOR_AF_MODE_CONTINUOUS:
3337 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true);
3341 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3346 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3349 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3350 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3356 #if CONFIG_SENSOR_Flash
3357 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3359 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3360 if (value == 3) { /* ddl@rock-chips.com: torch */
3361 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3363 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3365 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3369 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3373 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3375 struct i2c_client *client = v4l2_get_subdevdata(sd);
3376 struct sensor *sensor = to_sensor(client);
3377 const struct v4l2_queryctrl *qctrl;
3379 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3383 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3389 case V4L2_CID_BRIGHTNESS:
3391 ctrl->value = sensor->info_priv.brightness;
3394 case V4L2_CID_SATURATION:
3396 ctrl->value = sensor->info_priv.saturation;
3399 case V4L2_CID_CONTRAST:
3401 ctrl->value = sensor->info_priv.contrast;
3404 case V4L2_CID_DO_WHITE_BALANCE:
3406 ctrl->value = sensor->info_priv.whiteBalance;
3409 case V4L2_CID_EXPOSURE:
3411 ctrl->value = sensor->info_priv.exposure;
3414 case V4L2_CID_HFLIP:
3416 ctrl->value = sensor->info_priv.mirror;
3419 case V4L2_CID_VFLIP:
3421 ctrl->value = sensor->info_priv.flip;
3432 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3434 struct i2c_client *client = v4l2_get_subdevdata(sd);
3435 struct sensor *sensor = to_sensor(client);
3436 struct soc_camera_device *icd = client->dev.platform_data;
3437 const struct v4l2_queryctrl *qctrl;
3440 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3444 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3450 #if CONFIG_SENSOR_Brightness
3451 case V4L2_CID_BRIGHTNESS:
3453 if (ctrl->value != sensor->info_priv.brightness)
3455 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3459 sensor->info_priv.brightness = ctrl->value;
3464 #if CONFIG_SENSOR_Exposure
3465 case V4L2_CID_EXPOSURE:
3467 if (ctrl->value != sensor->info_priv.exposure)
3469 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3473 sensor->info_priv.exposure = ctrl->value;
3478 #if CONFIG_SENSOR_Saturation
3479 case V4L2_CID_SATURATION:
3481 if (ctrl->value != sensor->info_priv.saturation)
3483 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3487 sensor->info_priv.saturation = ctrl->value;
3492 #if CONFIG_SENSOR_Contrast
3493 case V4L2_CID_CONTRAST:
3495 if (ctrl->value != sensor->info_priv.contrast)
3497 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3501 sensor->info_priv.contrast = ctrl->value;
3506 #if CONFIG_SENSOR_WhiteBalance
3507 case V4L2_CID_DO_WHITE_BALANCE:
3509 if (ctrl->value != sensor->info_priv.whiteBalance)
3511 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3515 sensor->info_priv.whiteBalance = ctrl->value;
3520 #if CONFIG_SENSOR_Mirror
3521 case V4L2_CID_HFLIP:
3523 if (ctrl->value != sensor->info_priv.mirror)
3525 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3527 sensor->info_priv.mirror = ctrl->value;
3532 #if CONFIG_SENSOR_Flip
3533 case V4L2_CID_VFLIP:
3535 if (ctrl->value != sensor->info_priv.flip)
3537 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3539 sensor->info_priv.flip = ctrl->value;
3550 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3552 const struct v4l2_queryctrl *qctrl;
3553 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3554 struct sensor *sensor = to_sensor(client);
3556 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3560 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3564 switch (ext_ctrl->id)
3566 case V4L2_CID_SCENE:
3568 ext_ctrl->value = sensor->info_priv.scene;
3571 case V4L2_CID_EFFECT:
3573 ext_ctrl->value = sensor->info_priv.effect;
3576 case V4L2_CID_ZOOM_ABSOLUTE:
3578 ext_ctrl->value = sensor->info_priv.digitalzoom;
3581 case V4L2_CID_ZOOM_RELATIVE:
3585 case V4L2_CID_FOCUS_ABSOLUTE:
3589 case V4L2_CID_FOCUS_RELATIVE:
3593 case V4L2_CID_FLASH:
3595 ext_ctrl->value = sensor->info_priv.flash;
3603 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3605 const struct v4l2_queryctrl *qctrl;
3606 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3607 struct sensor *sensor = to_sensor(client);
3610 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3614 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3619 switch (ext_ctrl->id)
3621 #if CONFIG_SENSOR_Scene
3622 case V4L2_CID_SCENE:
3624 if (ext_ctrl->value != sensor->info_priv.scene)
3626 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3628 sensor->info_priv.scene = ext_ctrl->value;
3633 #if CONFIG_SENSOR_Effect
3634 case V4L2_CID_EFFECT:
3636 if (ext_ctrl->value != sensor->info_priv.effect)
3638 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3640 sensor->info_priv.effect= ext_ctrl->value;
3645 #if CONFIG_SENSOR_DigitalZoom
3646 case V4L2_CID_ZOOM_ABSOLUTE:
3648 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3651 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3653 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3655 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3657 sensor->info_priv.digitalzoom += val_offset;
3659 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3664 case V4L2_CID_ZOOM_RELATIVE:
3666 if (ext_ctrl->value)
3668 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3670 sensor->info_priv.digitalzoom += ext_ctrl->value;
3672 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3677 #if CONFIG_SENSOR_Focus
3678 case V4L2_CID_FOCUS_ABSOLUTE:
3680 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3685 case V4L2_CID_FOCUS_RELATIVE:
3687 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3690 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3693 case V4L2_CID_FOCUS_AUTO:
3695 if (ext_ctrl->value == 1) {
3696 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
3697 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
3698 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3702 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3703 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3704 if (ext_ctrl->value == 0)
3705 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3709 case V4L2_CID_FOCUS_CONTINUOUS:
3711 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3712 if (ext_ctrl->value == 1) {
3713 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3715 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3718 if (ext_ctrl->value == 0)
3719 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3724 #if CONFIG_SENSOR_Flash
3725 case V4L2_CID_FLASH:
3727 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3729 sensor->info_priv.flash = ext_ctrl->value;
3731 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3742 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3744 struct i2c_client *client = v4l2_get_subdevdata(sd);
3745 struct soc_camera_device *icd = client->dev.platform_data;
3746 int i, error_cnt=0, error_idx=-1;
3749 for (i=0; i<ext_ctrl->count; i++) {
3750 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3757 error_idx = ext_ctrl->count;
3759 if (error_idx != -1) {
3760 ext_ctrl->error_idx = error_idx;
3767 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3769 struct i2c_client *client = v4l2_get_subdevdata(sd);
3770 struct soc_camera_device *icd = client->dev.platform_data;
3771 int i, error_cnt=0, error_idx=-1;
3773 for (i=0; i<ext_ctrl->count; i++) {
3774 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3781 error_idx = ext_ctrl->count;
3783 if (error_idx != -1) {
3784 ext_ctrl->error_idx = error_idx;
3791 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3793 struct i2c_client *client = v4l2_get_subdevdata(sd);
3794 struct sensor *sensor = to_sensor(client);
3795 #if CONFIG_SENSOR_Focus
3796 struct soc_camera_device *icd = client->dev.platform_data;
3797 struct v4l2_mbus_framefmt mf;
3801 sensor->info_priv.enable = 1;
3802 #if CONFIG_SENSOR_Focus
3803 mf.width = icd->user_width;
3804 mf.height = icd->user_height;
3805 mf.code = sensor->info_priv.fmt.code;
3806 mf.colorspace = sensor->info_priv.fmt.colorspace;
3807 mf.field = V4L2_FIELD_NONE;
3808 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3809 if (sensor_fmt_capturechk(sd, &mf) == false) {
3810 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3811 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false);
3812 sensor->info_priv.affm_reinit = 0;
3816 } else if (enable == 0) {
3817 sensor->info_priv.enable = 0;
3818 #if CONFIG_SENSOR_Focus
3819 flush_workqueue(sensor->sensor_wq);
3825 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3826 * this wasn't our capture interface, so, we wait for the right one */
3827 static int sensor_video_probe(struct soc_camera_device *icd,
3828 struct i2c_client *client)
3832 struct sensor *sensor = to_sensor(client);
3834 /* We must have a parent by now. And it cannot be a wrong one.
3835 * So this entire test is completely redundant. */
3836 if (!icd->dev.parent ||
3837 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3840 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3842 goto sensor_video_probe_err;
3845 ret = sensor_write(client, 0x3008, 0x80);
3847 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3849 goto sensor_video_probe_err;
3851 mdelay(5); //delay 5 microseconds
3853 /* check if it is an sensor sensor */
3854 ret = sensor_read(client, 0x300a, &value);
3856 SENSOR_TR("read chip id high byte failed\n");
3858 goto sensor_video_probe_err;
3861 pid |= (value << 8);
3863 ret = sensor_read(client, 0x300b, &value);
3865 SENSOR_TR("read chip id low byte failed\n");
3867 goto sensor_video_probe_err;
3870 pid |= (value & 0xff);
3871 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3872 if (pid == SENSOR_ID) {
3873 sensor->model = SENSOR_V4L2_IDENT;
3875 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3877 goto sensor_video_probe_err;
3882 sensor_video_probe_err:
3885 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3887 struct i2c_client *client = v4l2_get_subdevdata(sd);
3888 struct soc_camera_device *icd = client->dev.platform_data;
3889 struct sensor *sensor = to_sensor(client);
3892 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3895 case RK29_CAM_SUBDEV_DEACTIVATE:
3897 sensor_deactivate(client);
3900 case RK29_CAM_SUBDEV_IOREQUEST:
3902 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3903 if (sensor->sensor_io_request != NULL) {
3904 sensor->sensor_gpio_res = NULL;
3905 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
3906 if (sensor->sensor_io_request->gpio_res[i].dev_name &&
3907 (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
3908 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
3911 if (sensor->sensor_gpio_res == NULL) {
3912 SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
3914 goto sensor_ioctl_end;
3917 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3919 goto sensor_ioctl_end;
3921 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3923 #if CONFIG_SENSOR_Flash
3924 if (sensor->sensor_gpio_res) {
3925 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3926 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3927 for (i = 0; i < icd->ops->num_controls; i++) {
3928 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3929 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3930 sensor_controls[i].id=0xffff;
3933 sensor->info_priv.flash = 0xff;
3934 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3935 }else{ //two cameras are the same,need to deal diffrently ,zyc
3936 for (i = 0; i < icd->ops->num_controls; i++) {
3937 if(0xffff == icd->ops->controls[i].id){
3938 sensor_controls[i].id=V4L2_CID_FLASH;
3948 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3957 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3958 enum v4l2_mbus_pixelcode *code)
3960 if (index >= ARRAY_SIZE(sensor_colour_fmts))
3963 *code = sensor_colour_fmts[index].code;
3966 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3967 .init = sensor_init,
3968 .g_ctrl = sensor_g_control,
3969 .s_ctrl = sensor_s_control,
3970 .g_ext_ctrls = sensor_g_ext_controls,
3971 .s_ext_ctrls = sensor_s_ext_controls,
3972 .g_chip_ident = sensor_g_chip_ident,
3973 .ioctl = sensor_ioctl,
3976 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3977 .s_mbus_fmt = sensor_s_fmt,
3978 .g_mbus_fmt = sensor_g_fmt,
3979 .try_mbus_fmt = sensor_try_fmt,
3980 .enum_mbus_fmt = sensor_enum_fmt,
3981 .s_stream = sensor_s_stream,
3984 static struct v4l2_subdev_ops sensor_subdev_ops = {
3985 .core = &sensor_subdev_core_ops,
3986 .video = &sensor_subdev_video_ops,
3989 static int sensor_probe(struct i2c_client *client,
3990 const struct i2c_device_id *did)
3992 struct sensor *sensor;
3993 struct soc_camera_device *icd = client->dev.platform_data;
3994 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3995 struct soc_camera_link *icl;
3998 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
4000 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
4004 icl = to_soc_camera_link(icd);
4006 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
4010 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
4011 dev_warn(&adapter->dev,
4012 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
4016 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
4020 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
4022 /* Second stage probe - when a capture adapter is there */
4023 icd->ops = &sensor_ops;
4024 sensor->info_priv.fmt = sensor_colour_fmts[0];
4025 #if CONFIG_SENSOR_I2C_NOSCHED
4026 atomic_set(&sensor->tasklock_cnt,0);
4029 ret = sensor_video_probe(icd, client);
4032 i2c_set_clientdata(client, NULL);
4036 #if CONFIG_SENSOR_Focus
4037 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
4038 if (sensor->sensor_wq == NULL)
4039 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
4040 mutex_init(&sensor->wq_lock);
4044 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
4048 static int sensor_remove(struct i2c_client *client)
4050 struct sensor *sensor = to_sensor(client);
4051 struct soc_camera_device *icd = client->dev.platform_data;
4053 #if CONFIG_SENSOR_Focus
4054 if (sensor->sensor_wq) {
4055 destroy_workqueue(sensor->sensor_wq);
4056 sensor->sensor_wq = NULL;
4061 i2c_set_clientdata(client, NULL);
4062 client->driver = NULL;
4068 static const struct i2c_device_id sensor_id[] = {
4069 {SENSOR_NAME_STRING(), 0 },
4072 MODULE_DEVICE_TABLE(i2c, sensor_id);
4074 static struct i2c_driver sensor_i2c_driver = {
4076 .name = SENSOR_NAME_STRING(),
4078 .probe = sensor_probe,
4079 .remove = sensor_remove,
4080 .id_table = sensor_id,
4083 static int __init sensor_mod_init(void)
4085 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4086 return i2c_add_driver(&sensor_i2c_driver);
4089 static void __exit sensor_mod_exit(void)
4091 i2c_del_driver(&sensor_i2c_driver);
4094 device_initcall_sync(sensor_mod_init);
4095 module_exit(sensor_mod_exit);
4097 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4098 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4099 MODULE_LICENSE("GPL");