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 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 /* init 800X600 SVGA */
181 static struct reginfo sensor_init_data[] =
441 /* 720p 15fps @ 1280x720 */
443 static struct reginfo sensor_720p[]=
471 {0x3820, 0x41}, //ddl@rock-chips.com add start: qsxvga -> 720p isn't stream on
492 {0x4713, 0x03},//////ddl@rock-chips.com add end
508 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
510 static struct reginfo sensor_1080p[]=
516 /* 2592X1944 QSXGA */
517 static struct reginfo sensor_qsxga[] =
558 static struct reginfo sensor_qxga[] =
600 static struct reginfo sensor_uxga[] =
642 static struct reginfo sensor_sxga[] =
683 static struct reginfo sensor_xga[] =
723 static struct reginfo sensor_svga[] =
769 static struct reginfo sensor_vga[] =
775 static struct reginfo sensor_cif[] =
782 static struct reginfo sensor_qvga[] =
789 static struct reginfo sensor_qcif[] =
795 static struct reginfo sensor_ClrFmt_YUYV[]=
801 static struct reginfo sensor_ClrFmt_UYVY[]=
808 #if CONFIG_SENSOR_WhiteBalance
809 static struct reginfo sensor_WhiteB_Auto[]=
836 /* Cloudy Colour Temperature : 6500K - 8000K */
837 static struct reginfo sensor_WhiteB_Cloudy[]=
848 /* ClearDay Colour Temperature : 5000K - 6500K */
849 static struct reginfo sensor_WhiteB_ClearDay[]=
860 /* Office Colour Temperature : 3500K - 5000K */
861 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
872 /* Home Colour Temperature : 2500K - 3500K */
873 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
884 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
885 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
889 #if CONFIG_SENSOR_Brightness
890 static struct reginfo sensor_Brightness0[]=
894 static struct reginfo sensor_Brightness1[]=
899 static struct reginfo sensor_Brightness2[]=
903 static struct reginfo sensor_Brightness3[]=
907 static struct reginfo sensor_Brightness4[]=
912 static struct reginfo sensor_Brightness5[]=
916 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
917 sensor_Brightness4, sensor_Brightness5,NULL,
922 #if CONFIG_SENSOR_Effect
923 static struct reginfo sensor_Effect_Normal[] =
929 static struct reginfo sensor_Effect_WandB[] =
937 static struct reginfo sensor_Effect_Sepia[] =
946 static struct reginfo sensor_Effect_Negative[] =
952 };static struct reginfo sensor_Effect_Bluish[] =
962 static struct reginfo sensor_Effect_Green[] =
971 /*static struct reginfo sensor_Effect_Reddish[] =
981 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
982 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
985 #if CONFIG_SENSOR_Exposure
986 static struct reginfo sensor_Exposure0[]=
996 static struct reginfo sensor_Exposure1[]=
1004 {SEQUENCE_END, 0x00}
1006 static struct reginfo sensor_Exposure2[]=
1014 {SEQUENCE_END, 0x00}
1017 static struct reginfo sensor_Exposure3[]=
1025 {SEQUENCE_END, 0x00}
1027 static struct reginfo sensor_Exposure4[]=
1035 {SEQUENCE_END, 0x00}
1037 static struct reginfo sensor_Exposure5[]=
1045 {SEQUENCE_END, 0x00}
1047 static struct reginfo sensor_Exposure6[]=
1055 {SEQUENCE_END, 0x00}
1057 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1058 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1061 #if CONFIG_SENSOR_Saturation
1062 static struct reginfo sensor_Saturation0[]=
1064 {SEQUENCE_END, 0x00}
1067 static struct reginfo sensor_Saturation1[]=
1069 {SEQUENCE_END, 0x00}
1072 static struct reginfo sensor_Saturation2[]=
1074 {SEQUENCE_END, 0x00}
1075 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1078 #if CONFIG_SENSOR_Contrast
1079 static struct reginfo sensor_Contrast0[]=
1081 {SEQUENCE_END, 0x00}
1084 static struct reginfo sensor_Contrast1[]=
1086 {SEQUENCE_END, 0x00}
1088 static struct reginfo sensor_Contrast2[]=
1090 {SEQUENCE_END, 0x00}
1093 static struct reginfo sensor_Contrast3[]=
1095 {SEQUENCE_END, 0x00}
1098 static struct reginfo sensor_Contrast4[]=
1100 {SEQUENCE_END, 0x00}
1104 static struct reginfo sensor_Contrast5[]=
1106 {SEQUENCE_END, 0x00}
1109 static struct reginfo sensor_Contrast6[]=
1111 {SEQUENCE_END, 0x00}
1113 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1114 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1118 #if CONFIG_SENSOR_Mirror
1119 static struct reginfo sensor_MirrorOn[]=
1121 {SEQUENCE_END, 0x00}
1123 static struct reginfo sensor_MirrorOff[]=
1125 {SEQUENCE_END, 0x00}
1127 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1129 #if CONFIG_SENSOR_Flip
1130 static struct reginfo sensor_FlipOn[]=
1132 {SEQUENCE_END, 0x00}
1135 static struct reginfo sensor_FlipOff[]=
1137 {SEQUENCE_END, 0x00}
1139 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1142 #if CONFIG_SENSOR_Scene
1143 static struct reginfo sensor_SceneAuto[] =
1146 {SEQUENCE_END, 0x00}
1148 static struct reginfo sensor_SceneNight[] =
1150 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1168 {SEQUENCE_END, 0x00}
1170 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1173 #if CONFIG_SENSOR_DigitalZoom
1174 static struct reginfo sensor_Zoom0[] =
1176 {SEQUENCE_END, 0x00}
1178 static struct reginfo sensor_Zoom1[] =
1180 {SEQUENCE_END, 0x00}
1183 static struct reginfo sensor_Zoom2[] =
1185 {SEQUENCE_END, 0x00}
1189 static struct reginfo sensor_Zoom3[] =
1191 {SEQUENCE_END, 0x00}
1193 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1195 static const struct v4l2_querymenu sensor_menus[] =
1197 #if CONFIG_SENSOR_WhiteBalance
1198 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1199 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1200 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1203 #if CONFIG_SENSOR_Effect
1204 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1205 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1206 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1209 #if CONFIG_SENSOR_Scene
1210 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1213 #if CONFIG_SENSOR_Flash
1214 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1215 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1219 static const struct v4l2_queryctrl sensor_controls[] =
1221 #if CONFIG_SENSOR_WhiteBalance
1223 .id = V4L2_CID_DO_WHITE_BALANCE,
1224 .type = V4L2_CTRL_TYPE_MENU,
1225 .name = "White Balance Control",
1233 #if CONFIG_SENSOR_Brightness
1235 .id = V4L2_CID_BRIGHTNESS,
1236 .type = V4L2_CTRL_TYPE_INTEGER,
1237 .name = "Brightness Control",
1245 #if CONFIG_SENSOR_Effect
1247 .id = V4L2_CID_EFFECT,
1248 .type = V4L2_CTRL_TYPE_MENU,
1249 .name = "Effect Control",
1257 #if CONFIG_SENSOR_Exposure
1259 .id = V4L2_CID_EXPOSURE,
1260 .type = V4L2_CTRL_TYPE_INTEGER,
1261 .name = "Exposure Control",
1269 #if CONFIG_SENSOR_Saturation
1271 .id = V4L2_CID_SATURATION,
1272 .type = V4L2_CTRL_TYPE_INTEGER,
1273 .name = "Saturation Control",
1281 #if CONFIG_SENSOR_Contrast
1283 .id = V4L2_CID_CONTRAST,
1284 .type = V4L2_CTRL_TYPE_INTEGER,
1285 .name = "Contrast Control",
1293 #if CONFIG_SENSOR_Mirror
1295 .id = V4L2_CID_HFLIP,
1296 .type = V4L2_CTRL_TYPE_BOOLEAN,
1297 .name = "Mirror Control",
1305 #if CONFIG_SENSOR_Flip
1307 .id = V4L2_CID_VFLIP,
1308 .type = V4L2_CTRL_TYPE_BOOLEAN,
1309 .name = "Flip Control",
1317 #if CONFIG_SENSOR_Scene
1319 .id = V4L2_CID_SCENE,
1320 .type = V4L2_CTRL_TYPE_MENU,
1321 .name = "Scene Control",
1329 #if CONFIG_SENSOR_DigitalZoom
1331 .id = V4L2_CID_ZOOM_RELATIVE,
1332 .type = V4L2_CTRL_TYPE_INTEGER,
1333 .name = "DigitalZoom Control",
1339 .id = V4L2_CID_ZOOM_ABSOLUTE,
1340 .type = V4L2_CTRL_TYPE_INTEGER,
1341 .name = "DigitalZoom Control",
1349 #if CONFIG_SENSOR_Focus
1351 .id = V4L2_CID_FOCUS_RELATIVE,
1352 .type = V4L2_CTRL_TYPE_INTEGER,
1353 .name = "Focus Control",
1359 .id = V4L2_CID_FOCUS_ABSOLUTE,
1360 .type = V4L2_CTRL_TYPE_INTEGER,
1361 .name = "Focus Control",
1365 .default_value = 125,
1368 .id = V4L2_CID_FOCUS_AUTO,
1369 .type = V4L2_CTRL_TYPE_BOOLEAN,
1370 .name = "Focus Control",
1376 .id = V4L2_CID_FOCUS_CONTINUOUS,
1377 .type = V4L2_CTRL_TYPE_BOOLEAN,
1378 .name = "Focus Control",
1386 #if CONFIG_SENSOR_Flash
1388 .id = V4L2_CID_FLASH,
1389 .type = V4L2_CTRL_TYPE_MENU,
1390 .name = "Flash Control",
1399 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1400 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1401 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1402 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1403 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1404 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1405 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1406 static int sensor_resume(struct soc_camera_device *icd);
1407 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1408 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1409 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1410 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1411 static int sensor_deactivate(struct i2c_client *client);
1412 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1413 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1415 static struct soc_camera_ops sensor_ops =
1417 .suspend = sensor_suspend,
1418 .resume = sensor_resume,
1419 .set_bus_param = sensor_set_bus_param,
1420 .query_bus_param = sensor_query_bus_param,
1421 .controls = sensor_controls,
1422 .menus = sensor_menus,
1423 .num_controls = ARRAY_SIZE(sensor_controls),
1424 .num_menus = ARRAY_SIZE(sensor_menus),
1426 /* only one fixed colorspace per pixelcode */
1427 struct sensor_datafmt {
1428 enum v4l2_mbus_pixelcode code;
1429 enum v4l2_colorspace colorspace;
1432 /* Find a data format by a pixel code in an array */
1433 static const struct sensor_datafmt *sensor_find_datafmt(
1434 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1438 for (i = 0; i < n; i++)
1439 if (fmt[i].code == code)
1445 static const struct sensor_datafmt sensor_colour_fmts[] = {
1446 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1447 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1453 WqCmd_af_special_pos,
1457 WqCmd_af_return_idle,
1459 enum sensor_wq_result
1467 struct i2c_client *client;
1468 struct delayed_work dwork;
1469 enum sensor_wq_cmd cmd;
1470 wait_queue_head_t done;
1471 enum sensor_wq_result result;
1476 typedef struct sensor_info_priv_s
1492 unsigned char mirror; /* HFLIP */
1493 unsigned char flip; /* VFLIP */
1494 struct reginfo *winseqe_cur_addr;
1495 struct sensor_datafmt fmt;
1496 unsigned int enable;
1497 unsigned int funmodule_state;
1498 } sensor_info_priv_t;
1502 struct sensor_parameter
1504 unsigned short int preview_maxlines;
1505 unsigned short int preview_exposure;
1506 unsigned short int preview_line_width;
1507 unsigned short int preview_gain;
1509 unsigned short int capture_framerate;
1510 unsigned short int preview_framerate;
1516 struct v4l2_subdev subdev;
1517 struct i2c_client *client;
1518 sensor_info_priv_t info_priv;
1519 struct sensor_parameter parameter;
1520 struct workqueue_struct *sensor_wq;
1521 struct mutex wq_lock;
1522 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1523 #if CONFIG_SENSOR_I2C_NOSCHED
1524 atomic_t tasklock_cnt;
1526 struct rk29camera_platform_data *sensor_io_request;
1527 struct rk29camera_gpio_res *sensor_gpio_res;
1530 static struct sensor* to_sensor(const struct i2c_client *client)
1532 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1535 static int sensor_task_lock(struct i2c_client *client, int lock)
1537 #if CONFIG_SENSOR_I2C_NOSCHED
1539 struct sensor *sensor = to_sensor(client);
1542 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1543 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1544 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1548 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1549 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1550 goto sensor_task_lock_err;
1555 atomic_add(1, &sensor->tasklock_cnt);
1557 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1558 atomic_sub(1, &sensor->tasklock_cnt);
1560 if (atomic_read(&sensor->tasklock_cnt) == 0)
1565 sensor_task_lock_err:
1574 #if CONFIG_SENSOR_WRITE_REGS
1575 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
1578 struct i2c_msg msg[1];
1581 msg->addr = client->addr;
1582 msg->flags = client->flags;
1583 msg->buf = reg_info;
1584 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1585 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1591 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1592 err = i2c_transfer(client->adapter, msg, 1);
1596 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
1610 /* sensor register write */
1611 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1615 struct i2c_msg msg[1];
1618 buf[1] = reg & 0xFF;
1621 msg->addr = client->addr;
1622 msg->flags = client->flags;
1624 msg->len = sizeof(buf);
1625 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1626 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1631 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1632 err = i2c_transfer(client->adapter, msg, 1);
1637 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1645 /* sensor register read */
1646 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1650 struct i2c_msg msg[2];
1653 buf[1] = reg & 0xFF;
1655 msg[0].addr = client->addr;
1656 msg[0].flags = client->flags;
1658 msg[0].len = sizeof(buf);
1659 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1660 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1662 msg[1].addr = client->addr;
1663 msg[1].flags = client->flags|I2C_M_RD;
1666 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1667 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1671 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1672 err = i2c_transfer(client->adapter, msg, 2);
1678 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1686 /* write a array of registers */
1687 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1691 #if CONFIG_SENSOR_Focus
1692 struct sensor *sensor = to_sensor(client);
1694 #if CONFIG_SENSOR_I2C_RDWRCHK
1697 #if CONFIG_SENSOR_WRITE_REGS
1704 if (sensor_task_lock(client, 1) < 0)
1705 goto sensor_write_array_end;
1706 while (regarray[i].reg != SEQUENCE_END) {
1707 #if CONFIG_SENSOR_Focus
1708 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1709 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1711 goto sensor_write_array_end;
1715 #if CONFIG_SENSOR_WRITE_REGS
1719 reg_length = 0x0001;
1721 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
1724 if(reg_num >= WRITE_REGS_NUM)
1731 size_num = reg_num + 1;
1733 ptemp = phead = (u8*)kmalloc((size_num+10)*sizeof(u8),GFP_KERNEL);
1735 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1737 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1739 *phead = regarray[j].reg >> 8;
1740 *(ptemp+1) = regarray[j].reg & 0xFF;
1742 for( ; reg_num > 0; reg_num --, j++) {
1743 *ptemp ++ = regarray[j].val;
1747 err = sensor_write_regs(client, ptemp,size_num);
1751 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1754 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1759 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1763 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1765 goto sensor_write_array_end;
1768 #if CONFIG_SENSOR_I2C_RDWRCHK
1769 sensor_read(client, regarray[i].reg, &valchk);
1770 if (valchk != regarray[i].val)
1771 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1777 #if CONFIG_SENSOR_Focus
1778 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1779 || (regarray == sensor_init_data)) {
1780 sensor->info_priv.affm_reinit = 1;
1784 sensor_write_array_end:
1785 sensor_task_lock(client,0);
1788 #if CONFIG_SENSOR_I2C_RDWRCHK
1789 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1797 while (regarray[i].reg != 0)
1799 sensor_read(client, regarray[i].reg, &valchk);
1800 if (valchk != regarray[i].val)
1801 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1808 #if CONFIG_SENSOR_Focus
1815 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1818 char read_tag=0xff,cnt;
1821 if (cmdinfo->validate_bit & 0x80) {
1822 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1823 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1824 goto sensor_af_cmdset_err;
1826 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1828 for (i=0; i<4; i++) {
1829 if (cmdinfo->validate_bit & (1<<i)) {
1830 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1831 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]);
1832 goto sensor_af_cmdset_err;
1834 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]);
1838 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
1839 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1840 goto sensor_af_cmdset_err;
1842 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1845 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1846 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1847 goto sensor_af_cmdset_err;
1854 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1855 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1858 } while((read_tag != 0x00)&& (cnt++<100));
1860 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1862 sensor_af_cmdset_err:
1866 static int sensor_af_idlechk(struct i2c_client *client)
1870 struct af_cmdinfo cmdinfo;
1872 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1874 cmdinfo.cmd_tag = 0x01;
1875 cmdinfo.validate_bit = 0x80;
1876 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1878 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1880 goto sensor_af_idlechk_end;
1885 ret = sensor_read(client, CMD_ACK_Reg, &state);
1887 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1889 goto sensor_af_idlechk_end;
1891 }while(0x00 != state);
1894 sensor_af_idlechk_end:
1898 static int sensor_af_single(struct i2c_client *client)
1902 struct af_cmdinfo cmdinfo;
1904 cmdinfo.cmd_tag = 0x01;
1905 cmdinfo.validate_bit = 0x80;
1906 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
1908 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1910 goto sensor_af_single_end;
1920 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1922 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1924 goto sensor_af_single_end;
1926 }while((state == S_FOCUSING) && (cnt<100));
1928 if (state != S_FOCUSED) {
1929 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1931 goto sensor_af_single_end;
1934 sensor_af_single_end:
1938 static int sensor_af_const(struct i2c_client *client)
1947 static int sensor_af_init(struct i2c_client *client)
1952 ret = sensor_write_array(client, sensor_af_firmware);
1954 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1956 goto sensor_af_init_end;
1966 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1968 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1970 goto sensor_af_init_end;
1972 }while((state == S_STARTUP) && (cnt<100));
1974 if (state != S_IDLE) {
1975 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1977 goto sensor_af_init_end;
1981 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1985 static int sensor_af_downfirmware(struct i2c_client *client)
1987 struct sensor *sensor = to_sensor(client);
1989 struct soc_camera_device *icd = client->dev.platform_data;
1990 struct v4l2_mbus_framefmt mf;
1992 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1994 if (sensor_af_init(client)) {
1995 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1998 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
2000 mf.width = icd->user_width;
2001 mf.height = icd->user_height;
2002 mf.code = sensor->info_priv.fmt.code;
2003 mf.colorspace = sensor->info_priv.fmt.colorspace;
2004 mf.field = V4L2_FIELD_NONE;
2005 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
2006 ret = sensor_af_const(client);
2008 switch (sensor->info_priv.auto_focus)
2010 case SENSOR_AF_MODE_AUTO:
2012 ret = sensor_af_single(client);
2015 case SENSOR_AF_MODE_CLOSE:
2022 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
2023 goto sensor_af_downfirmware_end;
2027 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
2030 sensor_af_downfirmware_end:
2034 static void sensor_af_workqueue(struct work_struct *work)
2036 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
2037 struct i2c_client *client = sensor_work->client;
2038 struct sensor *sensor = to_sensor(client);
2039 struct af_cmdinfo cmdinfo;
2041 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
2043 mutex_lock(&sensor->wq_lock);
2045 switch (sensor_work->cmd)
2049 if (sensor_af_downfirmware(client) < 0) {
2050 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2054 case WqCmd_af_single:
2056 if (sensor_af_single(client) < 0) {
2057 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2058 sensor_work->result = WqRet_fail;
2060 sensor_work->result = WqRet_success;
2064 case WqCmd_af_special_pos:
2066 sensor_af_idlechk(client);
2068 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2069 cmdinfo.cmd_para[0] = sensor_work->var;
2070 cmdinfo.validate_bit = 0x81;
2071 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2072 sensor_work->result = WqRet_fail;
2074 sensor_work->result = WqRet_success;
2077 case WqCmd_af_near_pos:
2079 sensor_af_idlechk(client);
2080 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2081 cmdinfo.validate_bit = 0x80;
2082 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2083 sensor_work->result = WqRet_fail;
2085 sensor_work->result = WqRet_success;
2088 case WqCmd_af_far_pos:
2090 sensor_af_idlechk(client);
2091 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2092 cmdinfo.validate_bit = 0x80;
2093 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2094 sensor_work->result = WqRet_fail;
2096 sensor_work->result = WqRet_success;
2099 case WqCmd_af_continues:
2101 if (sensor_af_const(client) < 0)
2102 sensor_work->result = WqRet_fail;
2104 sensor_work->result = WqRet_success;
2107 case WqCmd_af_return_idle:
2109 if (sensor_af_idlechk(client) < 0)
2110 sensor_work->result = WqRet_fail;
2112 sensor_work->result = WqRet_success;
2116 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
2120 if (sensor_work->wait == false) {
2121 kfree((void*)sensor_work);
2123 wake_up(&sensor_work->done);
2125 mutex_unlock(&sensor->wq_lock);
2129 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait)
2131 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2132 struct sensor *sensor = to_sensor(client);
2133 struct sensor_work *wk;
2136 if (sensor->sensor_wq == NULL) {
2138 goto sensor_af_workqueue_set_end;
2141 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) != SENSOR_AF_IS_OK) {
2142 if (cmd != WqCmd_af_init) {
2143 SENSOR_TR("%s %s cmd(%d) ingore,because af module isn't ready!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2145 goto sensor_af_workqueue_set_end;
2149 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
2151 wk->client = client;
2152 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
2154 wk->result = WqRet_inval;
2157 init_waitqueue_head(&wk->done);
2159 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
2161 /* ddl@rock-chips.com:
2162 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
2163 * As a result any other ioctl calls will proceed very, very slowly since each call
2164 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
2165 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
2168 mutex_unlock(&icd->video_lock);
2169 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(2500)) == 0) {
2170 SENSOR_TR("%s %s cmd(%d) is timeout!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2174 mutex_lock(&icd->video_lock);
2178 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2181 sensor_af_workqueue_set_end:
2185 static int sensor_parameter_record(struct i2c_client *client)
2187 u8 ret_l,ret_m,ret_h;
2189 struct sensor *sensor = to_sensor(client);
2191 sensor_write(client,0x3503,0x07); //stop AE/AG
2192 sensor_write(client,0x3406,0x01); //stop AWB
2194 sensor_read(client,0x3500,&ret_h);
2195 sensor_read(client,0x3501, &ret_m);
2196 sensor_read(client,0x3502, &ret_l);
2200 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
2201 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
2202 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
2204 sensor_read(client,0x350c, &ret_h);
2205 sensor_read(client,0x350d, &ret_l);
2206 sensor->parameter.preview_line_width = ret_h & 0xff;
2207 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
2208 //Read back AGC Gain for preview
2209 sensor_read(client,0x350a, &ret_h);
2210 sensor_read(client,0x350b, &tp_l);
2211 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
2213 sensor_read(client,0x380e, &ret_h);
2214 sensor->parameter.preview_maxlines = ret_h;
2215 sensor->parameter.preview_maxlines <<= 8;
2216 sensor_read(client,0x380f, &tp_l);
2217 sensor->parameter.preview_maxlines += tp_l;
2219 sensor->parameter.capture_framerate = 375;
2220 sensor->parameter.preview_framerate = 1500;
2222 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
2223 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2224 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2225 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2226 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2227 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2229 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);
2232 static int sensor_ae_transfer(struct i2c_client *client)
2237 u16 ulCapture_Exposure;
2238 u32 ulCapture_Exposure_Gain;
2242 u8 reg_l = 0,reg_h =0;
2243 u16 Preview_Maxlines;
2245 u32 Capture_MaxLines;
2246 struct sensor *sensor = to_sensor(client);
2248 //Preview_Maxlines = sensor->parameter.preview_line_width;
2249 Preview_Maxlines = sensor->parameter.preview_maxlines;
2250 Gain = sensor->parameter.preview_gain;
2252 sensor_read(client,0x350c, ®_h);
2253 sensor_read(client,0x350d, ®_l);
2254 Capture_MaxLines = reg_h & 0xff;
2255 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2258 sensor_read(client,0x380e, ®_h);
2259 Capture_MaxLines = reg_h;
2260 Capture_MaxLines <<= 8;
2261 sensor_read(client,0x380f, ®_l);
2262 Capture_MaxLines += reg_l;
2265 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2267 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2269 Lines_10ms = Lines_10ms & 0xffff;
2271 if(Preview_Maxlines == 0)
2272 Preview_Maxlines = 1;
2274 //ulCapture_Exposure =
2275 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2277 ulCapture_Exposure =
2278 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2279 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2281 iCapture_Gain = (Gain & 0x0f) + 16;
2283 iCapture_Gain = iCapture_Gain << 1;
2286 iCapture_Gain = iCapture_Gain << 1;
2289 iCapture_Gain = iCapture_Gain << 1;
2292 iCapture_Gain = iCapture_Gain << 1;
2295 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
2296 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2298 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2299 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2300 if (ulCapture_Exposure > Lines_10ms)
2302 //ulCapture_Exposure *= 1.7;
2303 ulCapture_Exposure /= Lines_10ms;
2304 ulCapture_Exposure *= Lines_10ms;
2307 ulCapture_Exposure = Capture_MaxLines;
2308 //ulCapture_Exposure_Gain *= 1.5;
2311 if(ulCapture_Exposure == 0)
2312 ulCapture_Exposure = 1;
2314 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2315 iCapture_Gain = iCapture_Gain & 0xffff;
2317 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2318 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2319 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2323 if (iCapture_Gain > 31) {
2325 iCapture_Gain = iCapture_Gain >> 1;
2327 if (iCapture_Gain > 31) {
2329 iCapture_Gain = iCapture_Gain >> 1;
2331 if (iCapture_Gain > 31) {
2333 iCapture_Gain = iCapture_Gain >> 1;
2335 if (iCapture_Gain > 31) {
2337 iCapture_Gain = iCapture_Gain >> 1;
2339 if (iCapture_Gain > 16)
2340 Gain |= ((iCapture_Gain -16) & 0x0f);
2343 // write the gain and exposure to 0x350* registers
2344 //m_iWrite0x350b=Gain;
2345 sensor_write(client,0x350b, Gain);
2346 Gain = (Gain >> 8) & 0x01;
2347 sensor_write(client,0x350a, Gain);
2348 //m_iWrite0x3502=ExposureLow;
2349 sensor_write(client,0x3502, ExposureLow);
2350 //m_iWrite0x3501=ExposureMid;
2351 sensor_write(client,0x3501, ExposureMid);
2352 //m_iWrite0x3500=ExposureHigh;
2353 sensor_write(client,0x3500, ExposureHigh);
2354 // SendToFile("Gain = 0x%x\r\n", Gain);
2355 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2356 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2357 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2358 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2359 //camera_timed_wait(200);
2360 //linzhk camera_timed_wait(500);
2362 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2363 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2364 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2365 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2366 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2367 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2369 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2373 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2375 struct soc_camera_link *icl = to_soc_camera_link(icd);
2378 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2382 case Sensor_PowerDown:
2384 if (icl->powerdown) {
2385 ret = icl->powerdown(icd->pdev, on);
2386 if (ret == RK29_CAM_IO_SUCCESS) {
2390 icl->reset(icd->pdev);
2392 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2394 goto sensor_power_end;
2401 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2402 struct sensor *sensor = to_sensor(client);
2404 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2405 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2411 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2419 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2421 struct i2c_client *client = v4l2_get_subdevdata(sd);
2422 struct soc_camera_device *icd = client->dev.platform_data;
2423 struct sensor *sensor = to_sensor(client);
2424 const struct v4l2_queryctrl *qctrl;
2425 const struct sensor_datafmt *fmt;
2429 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2431 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2433 goto sensor_INIT_ERR;
2437 if (sensor_task_lock(client,1)<0)
2438 goto sensor_INIT_ERR;
2439 ret = sensor_write(client, 0x3008, 0x80);
2441 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2443 goto sensor_INIT_ERR;
2446 mdelay(5); //delay 5 microseconds
2447 /* check if it is an sensor sensor */
2448 ret = sensor_read(client, 0x300a, &value);
2450 SENSOR_TR("read chip id high byte failed\n");
2452 goto sensor_INIT_ERR;
2455 pid |= (value << 8);
2457 ret = sensor_read(client, 0x300b, &value);
2459 SENSOR_TR("read chip id low byte failed\n");
2461 goto sensor_INIT_ERR;
2464 pid |= (value & 0xff);
2465 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2467 if (pid == SENSOR_ID) {
2468 sensor->model = SENSOR_V4L2_IDENT;
2470 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2472 goto sensor_INIT_ERR;
2475 ret = sensor_write_array(client, sensor_init_data);
2477 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2478 goto sensor_INIT_ERR;
2480 sensor_task_lock(client,0);
2481 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2482 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2484 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2486 goto sensor_INIT_ERR;
2488 sensor->info_priv.fmt = *fmt;
2490 /* sensor sensor information for initialization */
2491 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2493 sensor->info_priv.whiteBalance = qctrl->default_value;
2494 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2496 sensor->info_priv.brightness = qctrl->default_value;
2497 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2499 sensor->info_priv.effect = qctrl->default_value;
2500 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2502 sensor->info_priv.exposure = qctrl->default_value;
2504 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2506 sensor->info_priv.saturation = qctrl->default_value;
2507 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2509 sensor->info_priv.contrast = qctrl->default_value;
2510 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2512 sensor->info_priv.mirror = qctrl->default_value;
2513 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2515 sensor->info_priv.flip = qctrl->default_value;
2516 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2518 sensor->info_priv.scene = qctrl->default_value;
2519 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2521 sensor->info_priv.digitalzoom = qctrl->default_value;
2523 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2524 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2526 sensor->info_priv.focus = qctrl->default_value;
2528 #if CONFIG_SENSOR_Flash
2529 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2531 sensor->info_priv.flash = qctrl->default_value;
2533 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);
2535 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
2539 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2540 sensor_task_lock(client,0);
2541 sensor_deactivate(client);
2544 static int sensor_deactivate(struct i2c_client *client)
2546 struct soc_camera_device *icd = client->dev.platform_data;
2547 struct sensor *sensor = to_sensor(client);
2549 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2551 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2552 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2553 sensor_task_lock(client, 1);
2554 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2555 sensor_write(client, 0x3018, 0x03); // D5-D0
2556 sensor_write(client,0x3019,0x00); // STROBE,SDA
2557 sensor_task_lock(client, 0);
2559 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2561 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2562 icd->user_width = SENSOR_INIT_WIDTH;
2563 icd->user_height = SENSOR_INIT_HEIGHT;
2564 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2568 static struct reginfo sensor_power_down_sequence[]=
2573 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2576 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2578 if (pm_msg.event == PM_EVENT_SUSPEND) {
2579 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2580 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2582 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2585 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2587 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2592 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2599 static int sensor_resume(struct soc_camera_device *icd)
2603 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2605 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2609 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2613 static int sensor_set_bus_param(struct soc_camera_device *icd,
2614 unsigned long flags)
2620 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2622 struct soc_camera_link *icl = to_soc_camera_link(icd);
2623 unsigned long flags = SENSOR_BUS_PARAM;
2625 return soc_camera_apply_sensor_flags(icl, flags);
2628 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2630 struct i2c_client *client = v4l2_get_subdevdata(sd);
2631 struct soc_camera_device *icd = client->dev.platform_data;
2632 struct sensor *sensor = to_sensor(client);
2634 mf->width = icd->user_width;
2635 mf->height = icd->user_height;
2636 mf->code = sensor->info_priv.fmt.code;
2637 mf->colorspace = sensor->info_priv.fmt.colorspace;
2638 mf->field = V4L2_FIELD_NONE;
2642 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2646 if ((mf->width == 1024) && (mf->height == 768)) {
2648 } else if ((mf->width == 1280) && (mf->height == 1024)) {
2650 } else if ((mf->width == 1600) && (mf->height == 1200)) {
2652 } else if ((mf->width == 2048) && (mf->height == 1536)) {
2654 } else if ((mf->width == 2592) && (mf->height == 1944)) {
2659 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2663 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2667 if ((mf->width == 1280) && (mf->height == 720)) {
2669 } else if ((mf->width == 1920) && (mf->height == 1080)) {
2674 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2677 static int sensor_s_fmt(struct v4l2_subdev *sd,struct v4l2_mbus_framefmt *mf)
2679 struct i2c_client *client = v4l2_get_subdevdata(sd);
2680 const struct sensor_datafmt *fmt;
2681 struct sensor *sensor = to_sensor(client);
2682 const struct v4l2_queryctrl *qctrl;
2683 struct soc_camera_device *icd = client->dev.platform_data;
2684 struct reginfo *winseqe_set_addr=NULL;
2685 int ret = 0, set_w,set_h;
2687 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2688 ARRAY_SIZE(sensor_colour_fmts));
2691 goto sensor_s_fmt_end;
2694 if (sensor->info_priv.fmt.code != mf->code) {
2697 case V4L2_MBUS_FMT_YUYV8_2X8:
2699 winseqe_set_addr = sensor_ClrFmt_YUYV;
2702 case V4L2_MBUS_FMT_UYVY8_2X8:
2704 winseqe_set_addr = sensor_ClrFmt_UYVY;
2710 if (winseqe_set_addr != NULL) {
2711 sensor_write_array(client, winseqe_set_addr);
2712 sensor->info_priv.fmt.code = mf->code;
2713 sensor->info_priv.fmt.colorspace= mf->colorspace;
2714 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2716 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2723 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2725 winseqe_set_addr = sensor_qcif;
2729 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2731 winseqe_set_addr = sensor_qvga;
2735 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2737 winseqe_set_addr = sensor_cif;
2741 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2743 winseqe_set_addr = sensor_vga;
2747 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2749 winseqe_set_addr = sensor_svga;
2753 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2755 winseqe_set_addr = sensor_xga;
2759 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2761 winseqe_set_addr = sensor_720p;
2765 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2767 winseqe_set_addr = sensor_sxga;
2771 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2773 winseqe_set_addr = sensor_uxga;
2777 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2779 winseqe_set_addr = sensor_1080p;
2783 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2785 winseqe_set_addr = sensor_qxga;
2789 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2791 winseqe_set_addr = sensor_qsxga;
2797 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2798 set_w = SENSOR_INIT_WIDTH;
2799 set_h = SENSOR_INIT_HEIGHT;
2800 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2803 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2805 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2806 sensor_parameter_record(client);
2807 /*#if CONFIG_SENSOR_Focus
2808 sensor_af_idlechk(client);
2809 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2810 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2812 #if CONFIG_SENSOR_Flash
2813 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2814 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2815 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2818 }else { /* ddl@rock-chips.com : Video */
2819 #if CONFIG_SENSOR_Flash
2820 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2821 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2822 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2826 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2827 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2828 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2829 ret |= sensor_write_array(client,sensor_init_data);
2830 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2833 ret |= sensor_write_array(client, winseqe_set_addr);
2835 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2836 #if CONFIG_SENSOR_Flash
2837 if (sensor_fmt_capturechk(sd,mf) == true) {
2838 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2839 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2840 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2844 goto sensor_s_fmt_end;
2846 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2848 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2849 sensor_ae_transfer(client);
2850 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2851 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2852 if (sensor->info_priv.whiteBalance != 0) {
2853 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2854 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2856 sensor->info_priv.snap2preview = true;
2857 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2858 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2859 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2861 sensor->info_priv.video2preview = true;
2862 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2863 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2864 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2865 if (sensor->info_priv.snap2preview == true) {
2866 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2867 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2869 #if CONFIG_SENSOR_Focus
2870 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
2871 sensor_af_workqueue_set(icd,WqCmd_af_return_idle,0,true);
2879 sensor->info_priv.video2preview = false;
2880 sensor->info_priv.snap2preview = false;
2882 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2886 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2894 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2896 struct i2c_client *client = v4l2_get_subdevdata(sd);
2897 struct sensor *sensor = to_sensor(client);
2898 const struct sensor_datafmt *fmt;
2899 int ret = 0,set_w,set_h;
2901 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2902 ARRAY_SIZE(sensor_colour_fmts));
2904 fmt = &sensor->info_priv.fmt;
2905 mf->code = fmt->code;
2908 if (mf->height > SENSOR_MAX_HEIGHT)
2909 mf->height = SENSOR_MAX_HEIGHT;
2910 else if (mf->height < SENSOR_MIN_HEIGHT)
2911 mf->height = SENSOR_MIN_HEIGHT;
2913 if (mf->width > SENSOR_MAX_WIDTH)
2914 mf->width = SENSOR_MAX_WIDTH;
2915 else if (mf->width < SENSOR_MIN_WIDTH)
2916 mf->width = SENSOR_MIN_WIDTH;
2921 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2926 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2931 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2936 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2941 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2946 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2951 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2956 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2961 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2966 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2971 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2976 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2983 set_w = SENSOR_INIT_WIDTH;
2984 set_h = SENSOR_INIT_HEIGHT;
2990 mf->colorspace = fmt->colorspace;
2995 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2997 struct i2c_client *client = v4l2_get_subdevdata(sd);
2999 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
3002 if (id->match.addr != client->addr)
3005 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
3010 #if CONFIG_SENSOR_Brightness
3011 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3013 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3015 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3017 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
3019 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
3021 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3024 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3028 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3032 #if CONFIG_SENSOR_Effect
3033 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3035 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3037 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3039 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
3041 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
3043 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3046 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3050 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3054 #if CONFIG_SENSOR_Exposure
3055 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3057 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3059 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3061 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
3063 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3065 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3068 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3072 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3076 #if CONFIG_SENSOR_Saturation
3077 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3079 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3081 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3083 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3085 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3087 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3090 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3094 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3098 #if CONFIG_SENSOR_Contrast
3099 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3101 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3103 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3105 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3107 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3109 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3112 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3116 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3120 #if CONFIG_SENSOR_Mirror
3121 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3123 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3125 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3127 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3129 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3131 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3134 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3138 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3142 #if CONFIG_SENSOR_Flip
3143 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3145 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3147 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3149 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3151 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3153 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3156 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3160 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3164 #if CONFIG_SENSOR_Scene
3165 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3167 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3169 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3171 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3173 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3175 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3178 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3182 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3186 #if CONFIG_SENSOR_WhiteBalance
3187 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3189 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3191 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3193 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3195 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3197 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3200 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3204 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3208 #if CONFIG_SENSOR_DigitalZoom
3209 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3211 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3212 struct sensor *sensor = to_sensor(client);
3213 const struct v4l2_queryctrl *qctrl_info;
3214 int digitalzoom_cur, digitalzoom_total;
3216 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3220 digitalzoom_cur = sensor->info_priv.digitalzoom;
3221 digitalzoom_total = qctrl_info->maximum;
3223 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3225 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3229 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3231 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3235 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3237 *value = digitalzoom_total - digitalzoom_cur;
3240 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3242 *value = 0 - digitalzoom_cur;
3245 digitalzoom_cur += *value;
3247 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3249 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3251 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3254 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3261 #if CONFIG_SENSOR_Focus
3262 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3264 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3265 struct sensor *sensor = to_sensor(client);
3266 const struct v4l2_queryctrl *qctrl_info;
3269 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3273 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3274 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3275 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true);
3276 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3279 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3283 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3284 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3289 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3291 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3292 struct sensor *sensor = to_sensor(client);
3293 const struct v4l2_queryctrl *qctrl_info;
3296 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3300 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3301 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3303 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true);
3305 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true);
3307 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3310 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3314 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3315 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3320 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3322 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3323 struct sensor *sensor = to_sensor(client);
3326 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3329 case SENSOR_AF_MODE_AUTO:
3331 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true);
3335 /*case SENSOR_AF_MODE_MACRO:
3337 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3341 case SENSOR_AF_MODE_INFINITY:
3343 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3347 case SENSOR_AF_MODE_CONTINUOUS:
3349 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true);
3353 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3358 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3361 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3362 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3368 #if CONFIG_SENSOR_Flash
3369 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3371 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3372 if (value == 3) { /* ddl@rock-chips.com: torch */
3373 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3375 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3377 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3381 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3385 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3387 struct i2c_client *client = v4l2_get_subdevdata(sd);
3388 struct sensor *sensor = to_sensor(client);
3389 const struct v4l2_queryctrl *qctrl;
3391 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3395 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3401 case V4L2_CID_BRIGHTNESS:
3403 ctrl->value = sensor->info_priv.brightness;
3406 case V4L2_CID_SATURATION:
3408 ctrl->value = sensor->info_priv.saturation;
3411 case V4L2_CID_CONTRAST:
3413 ctrl->value = sensor->info_priv.contrast;
3416 case V4L2_CID_DO_WHITE_BALANCE:
3418 ctrl->value = sensor->info_priv.whiteBalance;
3421 case V4L2_CID_EXPOSURE:
3423 ctrl->value = sensor->info_priv.exposure;
3426 case V4L2_CID_HFLIP:
3428 ctrl->value = sensor->info_priv.mirror;
3431 case V4L2_CID_VFLIP:
3433 ctrl->value = sensor->info_priv.flip;
3444 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3446 struct i2c_client *client = v4l2_get_subdevdata(sd);
3447 struct sensor *sensor = to_sensor(client);
3448 struct soc_camera_device *icd = client->dev.platform_data;
3449 const struct v4l2_queryctrl *qctrl;
3452 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3456 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3462 #if CONFIG_SENSOR_Brightness
3463 case V4L2_CID_BRIGHTNESS:
3465 if (ctrl->value != sensor->info_priv.brightness)
3467 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3471 sensor->info_priv.brightness = ctrl->value;
3476 #if CONFIG_SENSOR_Exposure
3477 case V4L2_CID_EXPOSURE:
3479 if (ctrl->value != sensor->info_priv.exposure)
3481 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3485 sensor->info_priv.exposure = ctrl->value;
3490 #if CONFIG_SENSOR_Saturation
3491 case V4L2_CID_SATURATION:
3493 if (ctrl->value != sensor->info_priv.saturation)
3495 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3499 sensor->info_priv.saturation = ctrl->value;
3504 #if CONFIG_SENSOR_Contrast
3505 case V4L2_CID_CONTRAST:
3507 if (ctrl->value != sensor->info_priv.contrast)
3509 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3513 sensor->info_priv.contrast = ctrl->value;
3518 #if CONFIG_SENSOR_WhiteBalance
3519 case V4L2_CID_DO_WHITE_BALANCE:
3521 if (ctrl->value != sensor->info_priv.whiteBalance)
3523 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3527 sensor->info_priv.whiteBalance = ctrl->value;
3532 #if CONFIG_SENSOR_Mirror
3533 case V4L2_CID_HFLIP:
3535 if (ctrl->value != sensor->info_priv.mirror)
3537 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3539 sensor->info_priv.mirror = ctrl->value;
3544 #if CONFIG_SENSOR_Flip
3545 case V4L2_CID_VFLIP:
3547 if (ctrl->value != sensor->info_priv.flip)
3549 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3551 sensor->info_priv.flip = ctrl->value;
3562 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3564 const struct v4l2_queryctrl *qctrl;
3565 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3566 struct sensor *sensor = to_sensor(client);
3568 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3572 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3576 switch (ext_ctrl->id)
3578 case V4L2_CID_SCENE:
3580 ext_ctrl->value = sensor->info_priv.scene;
3583 case V4L2_CID_EFFECT:
3585 ext_ctrl->value = sensor->info_priv.effect;
3588 case V4L2_CID_ZOOM_ABSOLUTE:
3590 ext_ctrl->value = sensor->info_priv.digitalzoom;
3593 case V4L2_CID_ZOOM_RELATIVE:
3597 case V4L2_CID_FOCUS_ABSOLUTE:
3601 case V4L2_CID_FOCUS_RELATIVE:
3605 case V4L2_CID_FLASH:
3607 ext_ctrl->value = sensor->info_priv.flash;
3615 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3617 const struct v4l2_queryctrl *qctrl;
3618 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3619 struct sensor *sensor = to_sensor(client);
3622 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3626 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3631 switch (ext_ctrl->id)
3633 #if CONFIG_SENSOR_Scene
3634 case V4L2_CID_SCENE:
3636 if (ext_ctrl->value != sensor->info_priv.scene)
3638 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3640 sensor->info_priv.scene = ext_ctrl->value;
3645 #if CONFIG_SENSOR_Effect
3646 case V4L2_CID_EFFECT:
3648 if (ext_ctrl->value != sensor->info_priv.effect)
3650 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3652 sensor->info_priv.effect= ext_ctrl->value;
3657 #if CONFIG_SENSOR_DigitalZoom
3658 case V4L2_CID_ZOOM_ABSOLUTE:
3660 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3663 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3665 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3667 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3669 sensor->info_priv.digitalzoom += val_offset;
3671 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3676 case V4L2_CID_ZOOM_RELATIVE:
3678 if (ext_ctrl->value)
3680 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3682 sensor->info_priv.digitalzoom += ext_ctrl->value;
3684 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3689 #if CONFIG_SENSOR_Focus
3690 case V4L2_CID_FOCUS_ABSOLUTE:
3692 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3697 case V4L2_CID_FOCUS_RELATIVE:
3699 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3702 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3705 case V4L2_CID_FOCUS_AUTO:
3707 if (ext_ctrl->value == 1) {
3708 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
3709 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
3710 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3714 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3715 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3716 if (ext_ctrl->value == 0)
3717 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3721 case V4L2_CID_FOCUS_CONTINUOUS:
3723 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3724 if (ext_ctrl->value == 1) {
3725 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3727 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3730 if (ext_ctrl->value == 0)
3731 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3736 #if CONFIG_SENSOR_Flash
3737 case V4L2_CID_FLASH:
3739 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3741 sensor->info_priv.flash = ext_ctrl->value;
3743 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3754 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3756 struct i2c_client *client = v4l2_get_subdevdata(sd);
3757 struct soc_camera_device *icd = client->dev.platform_data;
3758 int i, error_cnt=0, error_idx=-1;
3761 for (i=0; i<ext_ctrl->count; i++) {
3762 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3769 error_idx = ext_ctrl->count;
3771 if (error_idx != -1) {
3772 ext_ctrl->error_idx = error_idx;
3779 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3781 struct i2c_client *client = v4l2_get_subdevdata(sd);
3782 struct soc_camera_device *icd = client->dev.platform_data;
3783 int i, error_cnt=0, error_idx=-1;
3785 for (i=0; i<ext_ctrl->count; i++) {
3786 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3793 error_idx = ext_ctrl->count;
3795 if (error_idx != -1) {
3796 ext_ctrl->error_idx = error_idx;
3803 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3805 struct i2c_client *client = v4l2_get_subdevdata(sd);
3806 struct sensor *sensor = to_sensor(client);
3807 #if CONFIG_SENSOR_Focus
3808 struct soc_camera_device *icd = client->dev.platform_data;
3809 struct v4l2_mbus_framefmt mf;
3813 sensor->info_priv.enable = 1;
3814 #if CONFIG_SENSOR_Focus
3815 mf.width = icd->user_width;
3816 mf.height = icd->user_height;
3817 mf.code = sensor->info_priv.fmt.code;
3818 mf.colorspace = sensor->info_priv.fmt.colorspace;
3819 mf.field = V4L2_FIELD_NONE;
3820 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3821 if (sensor_fmt_capturechk(sd, &mf) == false) {
3822 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3823 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false);
3824 sensor->info_priv.affm_reinit = 0;
3828 } else if (enable == 0) {
3829 sensor->info_priv.enable = 0;
3830 #if CONFIG_SENSOR_Focus
3831 flush_workqueue(sensor->sensor_wq);
3837 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3838 * this wasn't our capture interface, so, we wait for the right one */
3839 static int sensor_video_probe(struct soc_camera_device *icd,
3840 struct i2c_client *client)
3844 struct sensor *sensor = to_sensor(client);
3846 /* We must have a parent by now. And it cannot be a wrong one.
3847 * So this entire test is completely redundant. */
3848 if (!icd->dev.parent ||
3849 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3852 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3854 goto sensor_video_probe_err;
3857 ret = sensor_write(client, 0x3008, 0x80);
3859 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3861 goto sensor_video_probe_err;
3863 mdelay(5); //delay 5 microseconds
3865 /* check if it is an sensor sensor */
3866 ret = sensor_read(client, 0x300a, &value);
3868 SENSOR_TR("read chip id high byte failed\n");
3870 goto sensor_video_probe_err;
3873 pid |= (value << 8);
3875 ret = sensor_read(client, 0x300b, &value);
3877 SENSOR_TR("read chip id low byte failed\n");
3879 goto sensor_video_probe_err;
3882 pid |= (value & 0xff);
3883 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3884 if (pid == SENSOR_ID) {
3885 sensor->model = SENSOR_V4L2_IDENT;
3887 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3889 goto sensor_video_probe_err;
3894 sensor_video_probe_err:
3897 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3899 struct i2c_client *client = v4l2_get_subdevdata(sd);
3900 struct soc_camera_device *icd = client->dev.platform_data;
3901 struct sensor *sensor = to_sensor(client);
3904 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3907 case RK29_CAM_SUBDEV_DEACTIVATE:
3909 sensor_deactivate(client);
3912 case RK29_CAM_SUBDEV_IOREQUEST:
3914 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3915 if (sensor->sensor_io_request != NULL) {
3916 sensor->sensor_gpio_res = NULL;
3917 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
3918 if (sensor->sensor_io_request->gpio_res[i].dev_name &&
3919 (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
3920 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
3923 if (sensor->sensor_gpio_res == NULL) {
3924 SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
3926 goto sensor_ioctl_end;
3929 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3931 goto sensor_ioctl_end;
3933 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3935 #if CONFIG_SENSOR_Flash
3936 if (sensor->sensor_gpio_res) {
3937 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3938 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3939 for (i = 0; i < icd->ops->num_controls; i++) {
3940 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3941 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3944 sensor->info_priv.flash = 0xff;
3945 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3953 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3962 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3963 enum v4l2_mbus_pixelcode *code)
3965 if (index >= ARRAY_SIZE(sensor_colour_fmts))
3968 *code = sensor_colour_fmts[index].code;
3971 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3972 .init = sensor_init,
3973 .g_ctrl = sensor_g_control,
3974 .s_ctrl = sensor_s_control,
3975 .g_ext_ctrls = sensor_g_ext_controls,
3976 .s_ext_ctrls = sensor_s_ext_controls,
3977 .g_chip_ident = sensor_g_chip_ident,
3978 .ioctl = sensor_ioctl,
3981 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3982 .s_mbus_fmt = sensor_s_fmt,
3983 .g_mbus_fmt = sensor_g_fmt,
3984 .try_mbus_fmt = sensor_try_fmt,
3985 .enum_mbus_fmt = sensor_enum_fmt,
3986 .s_stream = sensor_s_stream,
3989 static struct v4l2_subdev_ops sensor_subdev_ops = {
3990 .core = &sensor_subdev_core_ops,
3991 .video = &sensor_subdev_video_ops,
3994 static int sensor_probe(struct i2c_client *client,
3995 const struct i2c_device_id *did)
3997 struct sensor *sensor;
3998 struct soc_camera_device *icd = client->dev.platform_data;
3999 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
4000 struct soc_camera_link *icl;
4003 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
4005 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
4009 icl = to_soc_camera_link(icd);
4011 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
4015 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
4016 dev_warn(&adapter->dev,
4017 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
4021 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
4025 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
4027 /* Second stage probe - when a capture adapter is there */
4028 icd->ops = &sensor_ops;
4029 sensor->info_priv.fmt = sensor_colour_fmts[0];
4030 #if CONFIG_SENSOR_I2C_NOSCHED
4031 atomic_set(&sensor->tasklock_cnt,0);
4034 ret = sensor_video_probe(icd, client);
4037 i2c_set_clientdata(client, NULL);
4041 #if CONFIG_SENSOR_Focus
4042 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
4043 if (sensor->sensor_wq == NULL)
4044 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
4045 mutex_init(&sensor->wq_lock);
4049 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
4053 static int sensor_remove(struct i2c_client *client)
4055 struct sensor *sensor = to_sensor(client);
4056 struct soc_camera_device *icd = client->dev.platform_data;
4058 #if CONFIG_SENSOR_Focus
4059 if (sensor->sensor_wq) {
4060 destroy_workqueue(sensor->sensor_wq);
4061 sensor->sensor_wq = NULL;
4066 i2c_set_clientdata(client, NULL);
4067 client->driver = NULL;
4073 static const struct i2c_device_id sensor_id[] = {
4074 {SENSOR_NAME_STRING(), 0 },
4077 MODULE_DEVICE_TABLE(i2c, sensor_id);
4079 static struct i2c_driver sensor_i2c_driver = {
4081 .name = SENSOR_NAME_STRING(),
4083 .probe = sensor_probe,
4084 .remove = sensor_remove,
4085 .id_table = sensor_id,
4088 static int __init sensor_mod_init(void)
4090 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4091 return i2c_add_driver(&sensor_i2c_driver);
4094 static void __exit sensor_mod_exit(void)
4096 i2c_del_driver(&sensor_i2c_driver);
4099 device_initcall_sync(sensor_mod_init);
4100 module_exit(sensor_mod_exit);
4102 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4103 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4104 MODULE_LICENSE("GPL");