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>
24 #include <linux/vmalloc.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 sensor_init_width /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT sensor_init_height
55 #define SENSOR_INIT_WINSEQADR sensor_init_winseq_p
56 #define SENSOR_INIT_PIXFMT sensor_init_pixelcode
57 #define SENSOR_BUS_PARAM sensor_init_busparam
59 #define CONFIG_SENSOR_WhiteBalance 1
60 #define CONFIG_SENSOR_Brightness 0
61 #define CONFIG_SENSOR_Contrast 0
62 #define CONFIG_SENSOR_Saturation 0
63 #define CONFIG_SENSOR_Effect 1
64 #define CONFIG_SENSOR_Scene 1
65 #define CONFIG_SENSOR_DigitalZoom 0
66 #define CONFIG_SENSOR_Exposure 0
67 #define CONFIG_SENSOR_Flash 0
68 #define CONFIG_SENSOR_Mirror 0
69 #define CONFIG_SENSOR_Flip 0
70 #ifdef CONFIG_OV5640_AUTOFOCUS
71 #define CONFIG_SENSOR_Focus 1
72 #include "ov5640_af_firmware.c"
74 #define CONFIG_SENSOR_Focus 0
77 #define CONFIG_SENSOR_I2C_SPEED 250000 /* Hz */
78 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
79 #define CONFIG_SENSOR_I2C_NOSCHED 0
80 #define CONFIG_SENSOR_I2C_RDWRCHK 0
82 #define CONFIG_SENSOR_WRITE_REGS 1
83 #define WRITE_REGS_NUM 100
86 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
87 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
88 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
89 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
90 #define COLOR_TEMPERATURE_OFFICE_DN 3500
91 #define COLOR_TEMPERATURE_OFFICE_UP 5000
92 #define COLOR_TEMPERATURE_HOME_DN 2500
93 #define COLOR_TEMPERATURE_HOME_UP 3500
95 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
96 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
98 #define SENSOR_AF_IS_ERR (0x00<<0)
99 #define SENSOR_AF_IS_OK (0x01<<0)
100 #define SENSOR_INIT_IS_ERR (0x00<<28)
101 #define SENSOR_INIT_IS_OK (0x01<<28)
103 #if CONFIG_SENSOR_Focus
104 /*#define SENSOR_AF_MODE_INFINITY 0
105 #define SENSOR_AF_MODE_MACRO 1
106 #define SENSOR_AF_MODE_FIXED 2
107 #define SENSOR_AF_MODE_AUTO 3
108 #define SENSOR_AF_MODE_CONTINUOUS 4
109 #define SENSOR_AF_MODE_CLOSE 5*/
110 #define SENSOR_AF_MODE_AUTO 0
111 #define SENSOR_AF_MODE_CLOSE 1
114 #if CONFIG_SENSOR_Focus
115 /* ov5640 VCM Command and Status Registers */
116 #define CMD_MAIN_Reg 0x3022
117 //#define CMD_TAG_Reg 0x3023
118 #define CMD_ACK_Reg 0x3023
119 #define CMD_PARA0_Reg 0x3024
120 #define CMD_PARA1_Reg 0x3025
121 #define CMD_PARA2_Reg 0x3026
122 #define CMD_PARA3_Reg 0x3027
123 #define CMD_PARA4_Reg 0x3028
125 //#define STA_ZONE_Reg 0x3026
126 #define STA_FOCUS_Reg 0x3029
128 /* ov5640 VCM Command */
129 #define OverlayEn_Cmd 0x01
130 #define OverlayDis_Cmd 0x02
131 #define SingleFocus_Cmd 0x03
132 #define ConstFocus_Cmd 0x04
133 #define StepMode_Cmd 0x05
134 #define PauseFocus_Cmd 0x06
135 #define ReturnIdle_Cmd 0x08
136 #define SetZone_Cmd 0x10
137 #define UpdateZone_Cmd 0x12
138 #define SetMotor_Cmd 0x20
141 #define Zone_configuration_Cmd 0x12
142 #define Trig_autofocus_Cmd 0x03
143 #define Get_focus_result_Cmd 0x07
145 /* ov5640 Focus State */
146 //#define S_FIRWRE 0xFF /*Firmware is downloaded and not run*/
147 #define S_STARTUP 0x7e /*Firmware is initializing*/
149 #define S_IDLE 0x70 /*Idle state, focus is released; lens is located at the furthest position.*/
150 #define S_FOCUSING 0x00 /*Auto Focus is running.*/
151 #define S_FOCUSED 0x10 /*Auto Focus is completed.*/
153 #define S_CAPTURE 0x12
156 /* ov5640 Zone State */
157 #define Zone_Is_Focused(a, zone_val) (zone_val&(1<<(a-3)))
158 #define Zone_Get_ID(zone_val) (zone_val&0x03)
160 #define Zone_CenterMode 0x01
161 #define Zone_5xMode 0x02
162 #define Zone_5PlusMode 0x03
163 #define Zone_4fMode 0x04
165 #define ZoneSel_Auto 0x0b
166 #define ZoneSel_SemiAuto 0x0c
167 #define ZoneSel_Manual 0x0d
168 #define ZoneSel_Rotate 0x0e
170 /* ov5640 Step Focus Commands */
171 #define StepFocus_Near_Tag 0x01
172 #define StepFocus_Far_Tag 0x02
173 #define StepFocus_Furthest_Tag 0x03
174 #define StepFocus_Nearest_Tag 0x04
175 #define StepFocus_Spec_Tag 0x10
178 /* init 800X600 SVGA */
179 static struct reginfo sensor_init_data[] =
439 /* 720p 15fps @ 1280x720 */
441 static struct reginfo sensor_720p[]=
482 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
484 static struct reginfo sensor_1080p[]=
490 /* 2592X1944 QSXGA */
491 static struct reginfo sensor_qsxga[] =
532 static struct reginfo sensor_qxga[] =
574 static struct reginfo sensor_uxga[] =
616 static struct reginfo sensor_sxga[] =
657 static struct reginfo sensor_xga[] =
697 static struct reginfo sensor_svga[] =
743 static struct reginfo sensor_vga[] =
749 static struct reginfo sensor_cif[] =
756 static struct reginfo sensor_qvga[] =
763 static struct reginfo sensor_qcif[] =
769 static struct reginfo sensor_ClrFmt_YUYV[]=
775 static struct reginfo sensor_ClrFmt_UYVY[]=
782 #if CONFIG_SENSOR_WhiteBalance
783 static struct reginfo sensor_WhiteB_Auto[]=
810 /* Cloudy Colour Temperature : 6500K - 8000K */
811 static struct reginfo sensor_WhiteB_Cloudy[]=
822 /* ClearDay Colour Temperature : 5000K - 6500K */
823 static struct reginfo sensor_WhiteB_ClearDay[]=
834 /* Office Colour Temperature : 3500K - 5000K */
835 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
846 /* Home Colour Temperature : 2500K - 3500K */
847 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
858 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
859 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
863 #if CONFIG_SENSOR_Brightness
864 static struct reginfo sensor_Brightness0[]=
868 static struct reginfo sensor_Brightness1[]=
873 static struct reginfo sensor_Brightness2[]=
877 static struct reginfo sensor_Brightness3[]=
881 static struct reginfo sensor_Brightness4[]=
886 static struct reginfo sensor_Brightness5[]=
890 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
891 sensor_Brightness4, sensor_Brightness5,NULL,
896 #if CONFIG_SENSOR_Effect
897 static struct reginfo sensor_Effect_Normal[] =
903 static struct reginfo sensor_Effect_WandB[] =
911 static struct reginfo sensor_Effect_Sepia[] =
920 static struct reginfo sensor_Effect_Negative[] =
926 };static struct reginfo sensor_Effect_Bluish[] =
936 static struct reginfo sensor_Effect_Green[] =
945 /*static struct reginfo sensor_Effect_Reddish[] =
955 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
956 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
959 #if CONFIG_SENSOR_Exposure
960 static struct reginfo sensor_Exposure0[]=
964 static struct reginfo sensor_Exposure1[]=
968 static struct reginfo sensor_Exposure2[]=
973 static struct reginfo sensor_Exposure3[]=
977 static struct reginfo sensor_Exposure4[]=
981 static struct reginfo sensor_Exposure5[]=
985 static struct reginfo sensor_Exposure6[]=
989 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
990 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
993 #if CONFIG_SENSOR_Saturation
994 static struct reginfo sensor_Saturation0[]=
999 static struct reginfo sensor_Saturation1[]=
1001 {SEQUENCE_END, 0x00}
1004 static struct reginfo sensor_Saturation2[]=
1006 {SEQUENCE_END, 0x00}
1007 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1010 #if CONFIG_SENSOR_Contrast
1011 static struct reginfo sensor_Contrast0[]=
1013 {SEQUENCE_END, 0x00}
1016 static struct reginfo sensor_Contrast1[]=
1018 {SEQUENCE_END, 0x00}
1020 static struct reginfo sensor_Contrast2[]=
1022 {SEQUENCE_END, 0x00}
1025 static struct reginfo sensor_Contrast3[]=
1027 {SEQUENCE_END, 0x00}
1030 static struct reginfo sensor_Contrast4[]=
1032 {SEQUENCE_END, 0x00}
1036 static struct reginfo sensor_Contrast5[]=
1038 {SEQUENCE_END, 0x00}
1041 static struct reginfo sensor_Contrast6[]=
1043 {SEQUENCE_END, 0x00}
1045 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1046 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1050 #if CONFIG_SENSOR_Mirror
1051 static struct reginfo sensor_MirrorOn[]=
1053 {SEQUENCE_END, 0x00}
1055 static struct reginfo sensor_MirrorOff[]=
1057 {SEQUENCE_END, 0x00}
1059 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1061 #if CONFIG_SENSOR_Flip
1062 static struct reginfo sensor_FlipOn[]=
1064 {SEQUENCE_END, 0x00}
1067 static struct reginfo sensor_FlipOff[]=
1069 {SEQUENCE_END, 0x00}
1071 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1074 #if CONFIG_SENSOR_Scene
1075 static struct reginfo sensor_SceneAuto[] =
1078 {SEQUENCE_END, 0x00}
1080 static struct reginfo sensor_SceneNight[] =
1082 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1100 {SEQUENCE_END, 0x00}
1102 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1105 #if CONFIG_SENSOR_DigitalZoom
1106 static struct reginfo sensor_Zoom0[] =
1108 {SEQUENCE_END, 0x00}
1110 static struct reginfo sensor_Zoom1[] =
1112 {SEQUENCE_END, 0x00}
1115 static struct reginfo sensor_Zoom2[] =
1117 {SEQUENCE_END, 0x00}
1121 static struct reginfo sensor_Zoom3[] =
1123 {SEQUENCE_END, 0x00}
1125 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1127 static const struct v4l2_querymenu sensor_menus[] =
1129 #if CONFIG_SENSOR_WhiteBalance
1130 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1131 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1132 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1135 #if CONFIG_SENSOR_Effect
1136 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1137 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1138 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1141 #if CONFIG_SENSOR_Scene
1142 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1145 #if CONFIG_SENSOR_Flash
1146 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1147 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1151 static const struct v4l2_queryctrl sensor_controls[] =
1153 #if CONFIG_SENSOR_WhiteBalance
1155 .id = V4L2_CID_DO_WHITE_BALANCE,
1156 .type = V4L2_CTRL_TYPE_MENU,
1157 .name = "White Balance Control",
1165 #if CONFIG_SENSOR_Brightness
1167 .id = V4L2_CID_BRIGHTNESS,
1168 .type = V4L2_CTRL_TYPE_INTEGER,
1169 .name = "Brightness Control",
1177 #if CONFIG_SENSOR_Effect
1179 .id = V4L2_CID_EFFECT,
1180 .type = V4L2_CTRL_TYPE_MENU,
1181 .name = "Effect Control",
1189 #if CONFIG_SENSOR_Exposure
1191 .id = V4L2_CID_EXPOSURE,
1192 .type = V4L2_CTRL_TYPE_INTEGER,
1193 .name = "Exposure Control",
1201 #if CONFIG_SENSOR_Saturation
1203 .id = V4L2_CID_SATURATION,
1204 .type = V4L2_CTRL_TYPE_INTEGER,
1205 .name = "Saturation Control",
1213 #if CONFIG_SENSOR_Contrast
1215 .id = V4L2_CID_CONTRAST,
1216 .type = V4L2_CTRL_TYPE_INTEGER,
1217 .name = "Contrast Control",
1225 #if CONFIG_SENSOR_Mirror
1227 .id = V4L2_CID_HFLIP,
1228 .type = V4L2_CTRL_TYPE_BOOLEAN,
1229 .name = "Mirror Control",
1237 #if CONFIG_SENSOR_Flip
1239 .id = V4L2_CID_VFLIP,
1240 .type = V4L2_CTRL_TYPE_BOOLEAN,
1241 .name = "Flip Control",
1249 #if CONFIG_SENSOR_Scene
1251 .id = V4L2_CID_SCENE,
1252 .type = V4L2_CTRL_TYPE_MENU,
1253 .name = "Scene Control",
1261 #if CONFIG_SENSOR_DigitalZoom
1263 .id = V4L2_CID_ZOOM_RELATIVE,
1264 .type = V4L2_CTRL_TYPE_INTEGER,
1265 .name = "DigitalZoom Control",
1271 .id = V4L2_CID_ZOOM_ABSOLUTE,
1272 .type = V4L2_CTRL_TYPE_INTEGER,
1273 .name = "DigitalZoom Control",
1281 #if CONFIG_SENSOR_Focus
1283 .id = V4L2_CID_FOCUS_RELATIVE,
1284 .type = V4L2_CTRL_TYPE_INTEGER,
1285 .name = "Focus Control",
1291 .id = V4L2_CID_FOCUS_ABSOLUTE,
1292 .type = V4L2_CTRL_TYPE_INTEGER,
1293 .name = "Focus Control",
1297 .default_value = 125,
1300 .id = V4L2_CID_FOCUS_AUTO,
1301 .type = V4L2_CTRL_TYPE_BOOLEAN,
1302 .name = "Focus Control",
1308 .id = V4L2_CID_FOCUS_CONTINUOUS,
1309 .type = V4L2_CTRL_TYPE_BOOLEAN,
1310 .name = "Focus Control",
1318 #if CONFIG_SENSOR_Flash
1320 .id = V4L2_CID_FLASH,
1321 .type = V4L2_CTRL_TYPE_MENU,
1322 .name = "Flash Control",
1331 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1332 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1333 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1334 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1335 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1336 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1337 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1338 static int sensor_resume(struct soc_camera_device *icd);
1339 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1340 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1341 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1342 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1343 static int sensor_deactivate(struct i2c_client *client);
1344 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1345 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1347 static struct soc_camera_ops sensor_ops =
1349 .suspend = sensor_suspend,
1350 .resume = sensor_resume,
1351 .set_bus_param = sensor_set_bus_param,
1352 .query_bus_param = sensor_query_bus_param,
1353 .controls = sensor_controls,
1354 .menus = sensor_menus,
1355 .num_controls = ARRAY_SIZE(sensor_controls),
1356 .num_menus = ARRAY_SIZE(sensor_menus),
1358 /* only one fixed colorspace per pixelcode */
1359 struct sensor_datafmt {
1360 enum v4l2_mbus_pixelcode code;
1361 enum v4l2_colorspace colorspace;
1364 /* Find a data format by a pixel code in an array */
1365 static const struct sensor_datafmt *sensor_find_datafmt(
1366 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1370 for (i = 0; i < n; i++)
1371 if (fmt[i].code == code)
1377 static const struct sensor_datafmt sensor_colour_fmts[] = {
1378 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1379 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1385 WqCmd_af_special_pos,
1389 WqCmd_af_return_idle,
1391 enum sensor_wq_result
1399 struct i2c_client *client;
1400 struct delayed_work dwork;
1401 enum sensor_wq_cmd cmd;
1402 wait_queue_head_t done;
1403 enum sensor_wq_result result;
1408 typedef struct sensor_info_priv_s
1424 unsigned char mirror; /* HFLIP */
1425 unsigned char flip; /* VFLIP */
1426 struct reginfo *winseqe_cur_addr;
1427 struct sensor_datafmt fmt;
1428 unsigned int enable;
1429 unsigned int funmodule_state;
1430 } sensor_info_priv_t;
1434 struct sensor_parameter
1436 unsigned short int preview_maxlines;
1437 unsigned short int preview_exposure;
1438 unsigned short int preview_line_width;
1439 unsigned short int preview_gain;
1441 unsigned short int capture_framerate;
1442 unsigned short int preview_framerate;
1448 struct v4l2_subdev subdev;
1449 struct i2c_client *client;
1450 sensor_info_priv_t info_priv;
1451 struct sensor_parameter parameter;
1452 struct workqueue_struct *sensor_wq;
1453 struct mutex wq_lock;
1454 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1455 #if CONFIG_SENSOR_I2C_NOSCHED
1456 atomic_t tasklock_cnt;
1458 struct rk29camera_platform_data *sensor_io_request;
1459 struct rk29camera_gpio_res *sensor_gpio_res;
1462 static struct sensor* to_sensor(const struct i2c_client *client)
1464 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1467 static int sensor_task_lock(struct i2c_client *client, int lock)
1469 #if CONFIG_SENSOR_I2C_NOSCHED
1471 struct sensor *sensor = to_sensor(client);
1474 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1475 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1476 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1480 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1481 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1482 goto sensor_task_lock_err;
1487 atomic_add(1, &sensor->tasklock_cnt);
1489 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1490 atomic_sub(1, &sensor->tasklock_cnt);
1492 if (atomic_read(&sensor->tasklock_cnt) == 0)
1497 sensor_task_lock_err:
1506 #if CONFIG_SENSOR_WRITE_REGS
1507 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
1510 struct i2c_msg msg[1];
1513 msg->addr = client->addr;
1514 msg->flags = client->flags;
1515 msg->buf = reg_info;
1516 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1517 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1523 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1524 err = i2c_transfer(client->adapter, msg, 1);
1528 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
1542 /* sensor register write */
1543 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1547 struct i2c_msg msg[1];
1550 buf[1] = reg & 0xFF;
1553 msg->addr = client->addr;
1554 msg->flags = client->flags;
1556 msg->len = sizeof(buf);
1557 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1558 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1563 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1564 err = i2c_transfer(client->adapter, msg, 1);
1569 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1577 /* sensor register read */
1578 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1582 struct i2c_msg msg[2];
1585 buf[1] = reg & 0xFF;
1587 msg[0].addr = client->addr;
1588 msg[0].flags = client->flags;
1590 msg[0].len = sizeof(buf);
1591 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1592 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1594 msg[1].addr = client->addr;
1595 msg[1].flags = client->flags|I2C_M_RD;
1598 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1599 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1603 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1604 err = i2c_transfer(client->adapter, msg, 2);
1610 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1618 /* write a array of registers */
1619 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1623 #if CONFIG_SENSOR_Focus
1624 struct sensor *sensor = to_sensor(client);
1626 #if CONFIG_SENSOR_I2C_RDWRCHK
1629 #if CONFIG_SENSOR_WRITE_REGS
1636 if (sensor_task_lock(client, 1) < 0)
1637 goto sensor_write_array_end;
1638 while (regarray[i].reg != SEQUENCE_END) {
1639 #if CONFIG_SENSOR_Focus
1640 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1641 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1643 goto sensor_write_array_end;
1647 #if CONFIG_SENSOR_WRITE_REGS
1651 reg_length = 0x0001;
1653 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
1656 if(reg_num >= WRITE_REGS_NUM)
1663 size_num = reg_num + 1;
1665 ptemp = phead = (u8*)kmalloc((size_num+10)*sizeof(u8),GFP_KERNEL);
1667 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1669 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1671 *phead = regarray[j].reg >> 8;
1672 *(ptemp+1) = regarray[j].reg & 0xFF;
1674 for( ; reg_num > 0; reg_num --, j++) {
1675 *ptemp ++ = regarray[j].val;
1679 err = sensor_write_regs(client, ptemp,size_num);
1683 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1686 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1691 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1695 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1697 goto sensor_write_array_end;
1700 #if CONFIG_SENSOR_I2C_RDWRCHK
1701 sensor_read(client, regarray[i].reg, &valchk);
1702 if (valchk != regarray[i].val)
1703 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1709 #if CONFIG_SENSOR_Focus
1710 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1711 || (regarray == sensor_init_data)) {
1712 sensor->info_priv.affm_reinit = 1;
1716 sensor_write_array_end:
1717 sensor_task_lock(client,0);
1720 #if CONFIG_SENSOR_I2C_RDWRCHK
1721 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1729 while (regarray[i].reg != 0)
1731 sensor_read(client, regarray[i].reg, &valchk);
1732 if (valchk != regarray[i].val)
1733 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1740 #if CONFIG_SENSOR_Focus
1747 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1750 char read_tag=0xff,cnt;
1753 if (cmdinfo->validate_bit & 0x80) {
1754 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1755 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1756 goto sensor_af_cmdset_err;
1758 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1760 for (i=0; i<4; i++) {
1761 if (cmdinfo->validate_bit & (1<<i)) {
1762 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1763 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]);
1764 goto sensor_af_cmdset_err;
1766 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]);
1770 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
1771 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1772 goto sensor_af_cmdset_err;
1774 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1777 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1778 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1779 goto sensor_af_cmdset_err;
1786 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1787 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1790 } while((read_tag != 0x00)&& (cnt++<100));
1792 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1794 sensor_af_cmdset_err:
1798 static int sensor_af_idlechk(struct i2c_client *client)
1802 struct af_cmdinfo cmdinfo;
1804 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1806 cmdinfo.cmd_tag = 0x01;
1807 cmdinfo.validate_bit = 0x80;
1808 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1810 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1812 goto sensor_af_idlechk_end;
1817 ret = sensor_read(client, CMD_ACK_Reg, &state);
1819 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1821 goto sensor_af_idlechk_end;
1823 }while(0x00 != state);
1826 sensor_af_idlechk_end:
1830 static int sensor_af_single(struct i2c_client *client)
1834 struct af_cmdinfo cmdinfo;
1836 cmdinfo.cmd_tag = 0x01;
1837 cmdinfo.validate_bit = 0x80;
1838 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
1840 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1842 goto sensor_af_single_end;
1852 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1854 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1856 goto sensor_af_single_end;
1858 }while((state == S_FOCUSING) && (cnt<100));
1860 if (state != S_FOCUSED) {
1861 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1863 goto sensor_af_single_end;
1866 sensor_af_single_end:
1870 static int sensor_af_const(struct i2c_client *client)
1879 static int sensor_af_init(struct i2c_client *client)
1884 ret = sensor_write_array(client, sensor_af_firmware);
1886 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1888 goto sensor_af_init_end;
1898 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1900 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1902 goto sensor_af_init_end;
1904 }while((state == S_STARTUP) && (cnt<100));
1906 if (state != S_IDLE) {
1907 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1909 goto sensor_af_init_end;
1913 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1917 static int sensor_af_downfirmware(struct i2c_client *client)
1919 struct sensor *sensor = to_sensor(client);
1920 struct af_cmdinfo cmdinfo;
1921 int ret=0, focus_pos = 0xfe;
1922 struct soc_camera_device *icd = client->dev.platform_data;
1923 struct v4l2_mbus_framefmt mf;
1925 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1927 if (sensor_af_init(client)) {
1928 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1931 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1933 mf.width = icd->user_width;
1934 mf.height = icd->user_height;
1935 mf.code = sensor->info_priv.fmt.code;
1936 mf.colorspace = sensor->info_priv.fmt.colorspace;
1937 mf.field = V4L2_FIELD_NONE;
1938 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
1939 ret = sensor_af_const(client);
1941 switch (sensor->info_priv.auto_focus)
1943 /*case SENSOR_AF_MODE_INFINITY:
1947 case SENSOR_AF_MODE_MACRO:
1949 if (focus_pos != 0x00)
1952 sensor_af_idlechk(client);
1953 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
1954 cmdinfo.cmd_para[0] = focus_pos;
1955 cmdinfo.validate_bit = 0x81;
1956 //ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
1959 case SENSOR_AF_MODE_AUTO:
1961 ret = sensor_af_single(client);
1964 /*case SENSOR_AF_MODE_CONTINUOUS:
1966 ret = sensor_af_const(client);
1969 case SENSOR_AF_MODE_CLOSE:
1976 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1977 goto sensor_af_downfirmware_end;
1981 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
1984 sensor_af_downfirmware_end:
1988 static void sensor_af_workqueue(struct work_struct *work)
1990 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
1991 struct i2c_client *client = sensor_work->client;
1992 struct sensor *sensor = to_sensor(client);
1993 struct af_cmdinfo cmdinfo;
1995 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
1997 mutex_lock(&sensor->wq_lock);
1998 switch (sensor_work->cmd)
2002 if (sensor_af_downfirmware(client) < 0) {
2003 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2007 case WqCmd_af_single:
2009 if (sensor_af_single(client) < 0) {
2010 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2011 sensor_work->result = WqRet_fail;
2013 sensor_work->result = WqRet_success;
2017 case WqCmd_af_special_pos:
2019 sensor_af_idlechk(client);
2021 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2022 cmdinfo.cmd_para[0] = sensor_work->var;
2023 cmdinfo.validate_bit = 0x81;
2024 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2025 sensor_work->result = WqRet_fail;
2027 sensor_work->result = WqRet_success;
2030 case WqCmd_af_near_pos:
2032 sensor_af_idlechk(client);
2033 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2034 cmdinfo.validate_bit = 0x80;
2035 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2036 sensor_work->result = WqRet_fail;
2038 sensor_work->result = WqRet_success;
2041 case WqCmd_af_far_pos:
2043 sensor_af_idlechk(client);
2044 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2045 cmdinfo.validate_bit = 0x80;
2046 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2047 sensor_work->result = WqRet_fail;
2049 sensor_work->result = WqRet_success;
2052 case WqCmd_af_continues:
2054 if (sensor_af_const(client) < 0)
2055 sensor_work->result = WqRet_fail;
2057 sensor_work->result = WqRet_success;
2060 case WqCmd_af_return_idle:
2062 if (sensor_af_idlechk(client) < 0)
2063 sensor_work->result = WqRet_fail;
2065 sensor_work->result = WqRet_success;
2069 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
2073 if (sensor_work->wait == false) {
2074 kfree((void*)sensor_work);
2076 wake_up(&sensor_work->done);
2078 mutex_unlock(&sensor->wq_lock);
2082 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait)
2084 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2085 struct sensor *sensor = to_sensor(client);
2086 struct sensor_work *wk;
2089 if (sensor->sensor_wq == NULL) {
2091 goto sensor_af_workqueue_set_end;
2094 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
2096 wk->client = client;
2097 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
2099 wk->result = WqRet_inval;
2102 init_waitqueue_head(&wk->done);
2104 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
2106 /* ddl@rock-chips.com:
2107 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
2108 * As a result any other ioctl calls will proceed very, very slowly since each call
2109 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
2110 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
2113 mutex_unlock(&icd->video_lock);
2114 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(2500)) == 0) {
2115 SENSOR_TR("%s %s cmd(%d) is timeout!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2119 mutex_lock(&icd->video_lock);
2123 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2126 sensor_af_workqueue_set_end:
2130 static int sensor_parameter_record(struct i2c_client *client)
2132 u8 ret_l,ret_m,ret_h;
2134 struct sensor *sensor = to_sensor(client);
2136 sensor_write(client,0x3503,0x07); //stop AE/AG
2137 sensor_write(client,0x3406,0x01); //stop AWB
2139 sensor_read(client,0x3500,&ret_h);
2140 sensor_read(client,0x3501, &ret_m);
2141 sensor_read(client,0x3502, &ret_l);
2145 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
2146 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
2147 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
2149 sensor_read(client,0x350c, &ret_h);
2150 sensor_read(client,0x350d, &ret_l);
2151 sensor->parameter.preview_line_width = ret_h & 0xff;
2152 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
2153 //Read back AGC Gain for preview
2154 sensor_read(client,0x350a, &ret_h);
2155 sensor_read(client,0x350b, &tp_l);
2156 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
2158 sensor_read(client,0x380e, &ret_h);
2159 sensor->parameter.preview_maxlines = ret_h;
2160 sensor->parameter.preview_maxlines <<= 8;
2161 sensor_read(client,0x380f, &tp_l);
2162 sensor->parameter.preview_maxlines += tp_l;
2164 sensor->parameter.capture_framerate = 375;
2165 sensor->parameter.preview_framerate = 1500;
2167 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
2168 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2169 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2170 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2171 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2172 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2174 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);
2177 static int sensor_ae_transfer(struct i2c_client *client)
2182 u16 ulCapture_Exposure;
2183 u32 ulCapture_Exposure_Gain;
2187 u8 reg_l = 0,reg_h =0;
2188 u16 Preview_Maxlines;
2190 u32 Capture_MaxLines;
2191 struct sensor *sensor = to_sensor(client);
2193 //Preview_Maxlines = sensor->parameter.preview_line_width;
2194 Preview_Maxlines = sensor->parameter.preview_maxlines;
2195 Gain = sensor->parameter.preview_gain;
2197 sensor_read(client,0x350c, ®_h);
2198 sensor_read(client,0x350d, ®_l);
2199 Capture_MaxLines = reg_h & 0xff;
2200 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2203 sensor_read(client,0x380e, ®_h);
2204 Capture_MaxLines = reg_h;
2205 Capture_MaxLines <<= 8;
2206 sensor_read(client,0x380f, ®_l);
2207 Capture_MaxLines += reg_l;
2210 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2212 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2214 Lines_10ms = Lines_10ms & 0xffff;
2216 if(Preview_Maxlines == 0)
2217 Preview_Maxlines = 1;
2219 //ulCapture_Exposure =
2220 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2222 ulCapture_Exposure =
2223 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2224 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2226 iCapture_Gain = (Gain & 0x0f) + 16;
2228 iCapture_Gain = iCapture_Gain << 1;
2231 iCapture_Gain = iCapture_Gain << 1;
2234 iCapture_Gain = iCapture_Gain << 1;
2237 iCapture_Gain = iCapture_Gain << 1;
2240 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
2241 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2243 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2244 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2245 if (ulCapture_Exposure > Lines_10ms)
2247 //ulCapture_Exposure *= 1.7;
2248 ulCapture_Exposure /= Lines_10ms;
2249 ulCapture_Exposure *= Lines_10ms;
2252 ulCapture_Exposure = Capture_MaxLines;
2253 //ulCapture_Exposure_Gain *= 1.5;
2256 if(ulCapture_Exposure == 0)
2257 ulCapture_Exposure = 1;
2259 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2260 iCapture_Gain = iCapture_Gain & 0xffff;
2262 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2263 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2264 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2268 if (iCapture_Gain > 31) {
2270 iCapture_Gain = iCapture_Gain >> 1;
2272 if (iCapture_Gain > 31) {
2274 iCapture_Gain = iCapture_Gain >> 1;
2276 if (iCapture_Gain > 31) {
2278 iCapture_Gain = iCapture_Gain >> 1;
2280 if (iCapture_Gain > 31) {
2282 iCapture_Gain = iCapture_Gain >> 1;
2284 if (iCapture_Gain > 16)
2285 Gain |= ((iCapture_Gain -16) & 0x0f);
2288 // write the gain and exposure to 0x350* registers
2289 //m_iWrite0x350b=Gain;
2290 sensor_write(client,0x350b, Gain);
2291 Gain = (Gain >> 8) & 0x01;
2292 sensor_write(client,0x350a, Gain);
2293 //m_iWrite0x3502=ExposureLow;
2294 sensor_write(client,0x3502, ExposureLow);
2295 //m_iWrite0x3501=ExposureMid;
2296 sensor_write(client,0x3501, ExposureMid);
2297 //m_iWrite0x3500=ExposureHigh;
2298 sensor_write(client,0x3500, ExposureHigh);
2299 // SendToFile("Gain = 0x%x\r\n", Gain);
2300 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2301 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2302 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2303 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2304 //camera_timed_wait(200);
2305 //linzhk camera_timed_wait(500);
2307 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2308 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2309 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2310 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2311 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2312 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2314 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2318 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2320 struct soc_camera_link *icl = to_soc_camera_link(icd);
2323 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2327 case Sensor_PowerDown:
2329 if (icl->powerdown) {
2330 ret = icl->powerdown(icd->pdev, on);
2331 if (ret == RK29_CAM_IO_SUCCESS) {
2335 icl->reset(icd->pdev);
2337 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2339 goto sensor_power_end;
2346 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2347 struct sensor *sensor = to_sensor(client);
2349 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2350 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2356 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2364 static s32 sensor_init_width = 800;
2365 static s32 sensor_init_height = 600;
2366 static unsigned long sensor_init_busparam = (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING|SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ);
2367 static enum v4l2_mbus_pixelcode sensor_init_pixelcode = V4L2_MBUS_FMT_YUYV8_2X8;
2368 static struct reginfo* sensor_init_data_p = sensor_init_data;
2369 static struct reginfo* sensor_init_winseq_p = sensor_svga;
2370 static struct reginfo* sensor_init_winseq_board = NULL;
2371 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2373 struct i2c_client *client = v4l2_get_subdevdata(sd);
2374 struct soc_camera_device *icd = client->dev.platform_data;
2375 struct sensor *sensor = to_sensor(client);
2376 const struct v4l2_queryctrl *qctrl;
2377 const struct sensor_datafmt *fmt;
2379 int ret,pid = 0,i = 0,j=0;
2380 //if val ==1,mean that sensor need to be reinit
2381 struct rk29camera_platform_data* tmp_plat_data =(struct rk29camera_platform_data*)val;
2382 sensor_init_data_p = sensor_init_data;
2383 sensor_init_winseq_p = sensor_svga;
2384 sensor_init_width = 800;
2385 sensor_init_height = 600;
2386 if (tmp_plat_data != NULL && (u32)tmp_plat_data != 1) {
2387 for(i = 0;i < RK_CAM_NUM;i++){
2388 if ((tmp_plat_data->sensor_init_data[i])&& tmp_plat_data->info[i].dev_name &&
2389 (strcmp(tmp_plat_data->info[i].dev_name, dev_name(icd->pdev)) == 0))
2393 if(tmp_plat_data && ((u32)tmp_plat_data != 1) &&(i < RK_CAM_NUM) && tmp_plat_data->sensor_init_data[i]){
2394 //user has defined the init data
2396 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data && (sizeof(struct reginfo) != sizeof(struct reginfo_t))){
2397 for(j = 0;j< sizeof(sensor_init_data)/sizeof(struct reginfo);j++){
2398 sensor_init_data[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].reg;
2399 sensor_init_data[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].val;
2401 sensor_init_data_p = sensor_init_data;
2403 else if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data){
2404 sensor_init_data_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data);
2407 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq && (sizeof(struct reginfo) != sizeof(struct reginfo_t))){
2408 int tmp_winseq_size = tmp_plat_data->sensor_init_data[i]->rk_sensor_winseq_size;
2409 if(sensor_init_winseq_board)
2411 vfree(sensor_init_winseq_board);
2412 sensor_init_winseq_board = NULL;
2414 sensor_init_winseq_board = (struct reginfo*)vmalloc(tmp_winseq_size);
2415 if(!sensor_init_winseq_board)
2416 SENSOR_TR("%s :vmalloc erro !",__FUNCTION__);
2417 for(j = 0;j< tmp_winseq_size;j++){
2418 sensor_init_winseq_board[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].reg;
2419 sensor_init_winseq_board[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].val;
2421 sensor_init_winseq_p = sensor_init_winseq_board;
2423 else if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq){
2424 sensor_init_winseq_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq);
2426 //init width,height,bus,pixelcode
2427 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width != INVALID_VALUE)
2428 sensor_init_width = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width;
2429 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height != INVALID_VALUE)
2430 sensor_init_height = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height;
2431 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param != INVALID_VALUE)
2432 sensor_init_busparam = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param;
2433 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode != INVALID_VALUE)
2434 sensor_init_pixelcode = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode;
2437 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2439 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2441 goto sensor_INIT_ERR;
2445 if (sensor_task_lock(client,1)<0)
2446 goto sensor_INIT_ERR;
2447 ret = sensor_write(client, 0x3008, 0x80);
2449 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2451 goto sensor_INIT_ERR;
2454 mdelay(5); //delay 5 microseconds
2455 /* check if it is an sensor sensor */
2456 ret = sensor_read(client, 0x300a, &value);
2458 SENSOR_TR("read chip id high byte failed\n");
2460 goto sensor_INIT_ERR;
2463 pid |= (value << 8);
2465 ret = sensor_read(client, 0x300b, &value);
2467 SENSOR_TR("read chip id low byte failed\n");
2469 goto sensor_INIT_ERR;
2472 pid |= (value & 0xff);
2473 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2475 if (pid == SENSOR_ID) {
2476 sensor->model = SENSOR_V4L2_IDENT;
2478 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2480 goto sensor_INIT_ERR;
2483 ret = sensor_write_array(client, sensor_init_data_p);
2485 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2486 goto sensor_INIT_ERR;
2488 sensor_task_lock(client,0);
2489 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2490 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2492 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2494 goto sensor_INIT_ERR;
2496 sensor->info_priv.fmt = *fmt;
2498 /* sensor sensor information for initialization */
2499 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2501 sensor->info_priv.whiteBalance = qctrl->default_value;
2502 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2504 sensor->info_priv.brightness = qctrl->default_value;
2505 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2507 sensor->info_priv.effect = qctrl->default_value;
2508 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2510 sensor->info_priv.exposure = qctrl->default_value;
2512 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2514 sensor->info_priv.saturation = qctrl->default_value;
2515 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2517 sensor->info_priv.contrast = qctrl->default_value;
2518 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2520 sensor->info_priv.mirror = qctrl->default_value;
2521 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2523 sensor->info_priv.flip = qctrl->default_value;
2524 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2526 sensor->info_priv.scene = qctrl->default_value;
2527 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2529 sensor->info_priv.digitalzoom = qctrl->default_value;
2531 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2532 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2534 sensor->info_priv.focus = qctrl->default_value;
2536 #if CONFIG_SENSOR_Flash
2537 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2539 sensor->info_priv.flash = qctrl->default_value;
2541 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);
2543 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
2547 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2548 sensor_task_lock(client,0);
2549 sensor_deactivate(client);
2552 static int sensor_deactivate(struct i2c_client *client)
2554 struct soc_camera_device *icd = client->dev.platform_data;
2555 struct sensor *sensor = to_sensor(client);
2557 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2559 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2560 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2561 sensor_task_lock(client, 1);
2562 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
2563 sensor_write(client, 0x3018, 0x03); // D5-D0
2564 sensor_write(client,0x3019,0x00); // STROBE,SDA
2565 sensor_task_lock(client, 0);
2567 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2569 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2570 icd->user_width = SENSOR_INIT_WIDTH;
2571 icd->user_height = SENSOR_INIT_HEIGHT;
2572 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2576 static struct reginfo sensor_power_down_sequence[]=
2581 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2584 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2586 if (pm_msg.event == PM_EVENT_SUSPEND) {
2587 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2588 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2590 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2593 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2595 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2600 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2607 static int sensor_resume(struct soc_camera_device *icd)
2611 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2613 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2617 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2621 static int sensor_set_bus_param(struct soc_camera_device *icd,
2622 unsigned long flags)
2628 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2630 struct soc_camera_link *icl = to_soc_camera_link(icd);
2631 unsigned long flags = SENSOR_BUS_PARAM;
2633 return soc_camera_apply_sensor_flags(icl, flags);
2636 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2638 struct i2c_client *client = v4l2_get_subdevdata(sd);
2639 struct soc_camera_device *icd = client->dev.platform_data;
2640 struct sensor *sensor = to_sensor(client);
2642 mf->width = icd->user_width;
2643 mf->height = icd->user_height;
2644 mf->code = sensor->info_priv.fmt.code;
2645 mf->colorspace = sensor->info_priv.fmt.colorspace;
2646 mf->field = V4L2_FIELD_NONE;
2650 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2654 if ((mf->width == 1024) && (mf->height == 768)) {
2656 } else if ((mf->width == 1280) && (mf->height == 1024)) {
2658 } else if ((mf->width == 1600) && (mf->height == 1200)) {
2660 } else if ((mf->width == 2048) && (mf->height == 1536)) {
2662 } else if ((mf->width == 2592) && (mf->height == 1944)) {
2667 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2671 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2675 if ((mf->width == 1280) && (mf->height == 720)) {
2677 } else if ((mf->width == 1920) && (mf->height == 1080)) {
2682 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2685 static int sensor_s_fmt(struct v4l2_subdev *sd,struct v4l2_mbus_framefmt *mf)
2687 struct i2c_client *client = v4l2_get_subdevdata(sd);
2688 const struct sensor_datafmt *fmt;
2689 struct sensor *sensor = to_sensor(client);
2690 const struct v4l2_queryctrl *qctrl;
2691 struct soc_camera_device *icd = client->dev.platform_data;
2692 struct reginfo *winseqe_set_addr=NULL;
2693 int ret = 0, set_w,set_h;
2695 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2696 ARRAY_SIZE(sensor_colour_fmts));
2699 goto sensor_s_fmt_end;
2702 if (sensor->info_priv.fmt.code != mf->code) {
2705 case V4L2_MBUS_FMT_YUYV8_2X8:
2707 winseqe_set_addr = sensor_ClrFmt_YUYV;
2710 case V4L2_MBUS_FMT_UYVY8_2X8:
2712 winseqe_set_addr = sensor_ClrFmt_UYVY;
2718 if (winseqe_set_addr != NULL) {
2719 sensor_write_array(client, winseqe_set_addr);
2720 sensor->info_priv.fmt.code = mf->code;
2721 sensor->info_priv.fmt.colorspace= mf->colorspace;
2722 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2724 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2731 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2733 winseqe_set_addr = sensor_qcif;
2737 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2739 winseqe_set_addr = sensor_qvga;
2743 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2745 winseqe_set_addr = sensor_cif;
2749 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2751 winseqe_set_addr = sensor_vga;
2755 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2757 winseqe_set_addr = sensor_svga;
2761 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2763 winseqe_set_addr = sensor_xga;
2767 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2769 winseqe_set_addr = sensor_720p;
2773 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2775 winseqe_set_addr = sensor_sxga;
2779 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2781 winseqe_set_addr = sensor_uxga;
2785 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2787 winseqe_set_addr = sensor_1080p;
2791 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2793 winseqe_set_addr = sensor_qxga;
2797 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2799 winseqe_set_addr = sensor_qsxga;
2805 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2806 set_w = SENSOR_INIT_WIDTH;
2807 set_h = SENSOR_INIT_HEIGHT;
2808 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2811 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
2813 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2814 sensor_parameter_record(client);
2815 /*#if CONFIG_SENSOR_Focus
2816 sensor_af_idlechk(client);
2817 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2818 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2820 #if CONFIG_SENSOR_Flash
2821 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2822 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2823 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2826 }else { /* ddl@rock-chips.com : Video */
2827 #if CONFIG_SENSOR_Flash
2828 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2829 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2830 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2834 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2835 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2836 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2837 ret |= sensor_write_array(client,sensor_init_data);
2838 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2841 ret |= sensor_write_array(client, winseqe_set_addr);
2843 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2844 #if CONFIG_SENSOR_Flash
2845 if (sensor_fmt_capturechk(sd,mf) == true) {
2846 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2847 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2848 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2852 goto sensor_s_fmt_end;
2854 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
2856 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2857 sensor_ae_transfer(client);
2858 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2859 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2860 if (sensor->info_priv.whiteBalance != 0) {
2861 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2862 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2864 sensor->info_priv.snap2preview = true;
2865 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2866 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2867 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2869 sensor->info_priv.video2preview = true;
2870 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2871 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2872 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2873 if (sensor->info_priv.snap2preview == true) {
2874 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2875 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2877 #if CONFIG_SENSOR_Focus
2878 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
2879 sensor_af_workqueue_set(icd,WqCmd_af_return_idle,0,true);
2887 sensor->info_priv.video2preview = false;
2888 sensor->info_priv.snap2preview = false;
2890 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2894 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2902 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2904 struct i2c_client *client = v4l2_get_subdevdata(sd);
2905 struct sensor *sensor = to_sensor(client);
2906 const struct sensor_datafmt *fmt;
2909 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2910 ARRAY_SIZE(sensor_colour_fmts));
2912 fmt = &sensor->info_priv.fmt;
2913 mf->code = fmt->code;
2916 if (mf->height > SENSOR_MAX_HEIGHT)
2917 mf->height = SENSOR_MAX_HEIGHT;
2918 else if (mf->height < SENSOR_MIN_HEIGHT)
2919 mf->height = SENSOR_MIN_HEIGHT;
2921 if (mf->width > SENSOR_MAX_WIDTH)
2922 mf->width = SENSOR_MAX_WIDTH;
2923 else if (mf->width < SENSOR_MIN_WIDTH)
2924 mf->width = SENSOR_MIN_WIDTH;
2926 mf->colorspace = fmt->colorspace;
2931 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2933 struct i2c_client *client = v4l2_get_subdevdata(sd);
2935 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2938 if (id->match.addr != client->addr)
2941 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2946 #if CONFIG_SENSOR_Brightness
2947 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2949 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2951 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2953 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2955 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2957 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2960 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2964 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2968 #if CONFIG_SENSOR_Effect
2969 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2971 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2973 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2975 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2977 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2979 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2982 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2986 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2990 #if CONFIG_SENSOR_Exposure
2991 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2993 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2995 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2997 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2999 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3001 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3004 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3008 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3012 #if CONFIG_SENSOR_Saturation
3013 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3015 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3017 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3019 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3021 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3023 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3026 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3030 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3034 #if CONFIG_SENSOR_Contrast
3035 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3037 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3039 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3041 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3043 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3045 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3048 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3052 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3056 #if CONFIG_SENSOR_Mirror
3057 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3059 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3061 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3063 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3065 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3067 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3070 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3074 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3078 #if CONFIG_SENSOR_Flip
3079 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3081 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3083 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3085 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3087 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3089 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3092 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3096 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3100 #if CONFIG_SENSOR_Scene
3101 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3103 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3105 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3107 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3109 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3111 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3114 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3118 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3122 #if CONFIG_SENSOR_WhiteBalance
3123 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3125 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3127 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3129 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3131 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3133 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3136 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3140 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3144 #if CONFIG_SENSOR_DigitalZoom
3145 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3147 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3148 struct sensor *sensor = to_sensor(client);
3149 const struct v4l2_queryctrl *qctrl_info;
3150 int digitalzoom_cur, digitalzoom_total;
3152 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3156 digitalzoom_cur = sensor->info_priv.digitalzoom;
3157 digitalzoom_total = qctrl_info->maximum;
3159 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3161 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3165 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3167 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3171 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3173 *value = digitalzoom_total - digitalzoom_cur;
3176 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3178 *value = 0 - digitalzoom_cur;
3181 digitalzoom_cur += *value;
3183 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3185 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3187 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3190 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3197 #if CONFIG_SENSOR_Focus
3198 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3200 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3201 struct sensor *sensor = to_sensor(client);
3202 const struct v4l2_queryctrl *qctrl_info;
3205 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3209 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3210 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3211 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true);
3212 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3215 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3219 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3220 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3225 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3227 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3228 struct sensor *sensor = to_sensor(client);
3229 const struct v4l2_queryctrl *qctrl_info;
3232 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3236 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3237 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3239 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true);
3241 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true);
3243 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3246 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3250 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3251 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3256 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3258 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3259 struct sensor *sensor = to_sensor(client);
3262 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3265 case SENSOR_AF_MODE_AUTO:
3267 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true);
3271 /*case SENSOR_AF_MODE_MACRO:
3273 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3277 case SENSOR_AF_MODE_INFINITY:
3279 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3283 case SENSOR_AF_MODE_CONTINUOUS:
3285 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true);
3289 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3294 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3297 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3298 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3304 #if CONFIG_SENSOR_Flash
3305 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3307 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3308 if (value == 3) { /* ddl@rock-chips.com: torch */
3309 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3311 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3313 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3317 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3321 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3323 struct i2c_client *client = v4l2_get_subdevdata(sd);
3324 struct sensor *sensor = to_sensor(client);
3325 const struct v4l2_queryctrl *qctrl;
3327 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3331 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3337 case V4L2_CID_BRIGHTNESS:
3339 ctrl->value = sensor->info_priv.brightness;
3342 case V4L2_CID_SATURATION:
3344 ctrl->value = sensor->info_priv.saturation;
3347 case V4L2_CID_CONTRAST:
3349 ctrl->value = sensor->info_priv.contrast;
3352 case V4L2_CID_DO_WHITE_BALANCE:
3354 ctrl->value = sensor->info_priv.whiteBalance;
3357 case V4L2_CID_EXPOSURE:
3359 ctrl->value = sensor->info_priv.exposure;
3362 case V4L2_CID_HFLIP:
3364 ctrl->value = sensor->info_priv.mirror;
3367 case V4L2_CID_VFLIP:
3369 ctrl->value = sensor->info_priv.flip;
3380 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3382 struct i2c_client *client = v4l2_get_subdevdata(sd);
3383 struct sensor *sensor = to_sensor(client);
3384 struct soc_camera_device *icd = client->dev.platform_data;
3385 const struct v4l2_queryctrl *qctrl;
3388 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3392 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3398 #if CONFIG_SENSOR_Brightness
3399 case V4L2_CID_BRIGHTNESS:
3401 if (ctrl->value != sensor->info_priv.brightness)
3403 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3407 sensor->info_priv.brightness = ctrl->value;
3412 #if CONFIG_SENSOR_Exposure
3413 case V4L2_CID_EXPOSURE:
3415 if (ctrl->value != sensor->info_priv.exposure)
3417 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3421 sensor->info_priv.exposure = ctrl->value;
3426 #if CONFIG_SENSOR_Saturation
3427 case V4L2_CID_SATURATION:
3429 if (ctrl->value != sensor->info_priv.saturation)
3431 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3435 sensor->info_priv.saturation = ctrl->value;
3440 #if CONFIG_SENSOR_Contrast
3441 case V4L2_CID_CONTRAST:
3443 if (ctrl->value != sensor->info_priv.contrast)
3445 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3449 sensor->info_priv.contrast = ctrl->value;
3454 #if CONFIG_SENSOR_WhiteBalance
3455 case V4L2_CID_DO_WHITE_BALANCE:
3457 if (ctrl->value != sensor->info_priv.whiteBalance)
3459 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3463 sensor->info_priv.whiteBalance = ctrl->value;
3468 #if CONFIG_SENSOR_Mirror
3469 case V4L2_CID_HFLIP:
3471 if (ctrl->value != sensor->info_priv.mirror)
3473 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3475 sensor->info_priv.mirror = ctrl->value;
3480 #if CONFIG_SENSOR_Flip
3481 case V4L2_CID_VFLIP:
3483 if (ctrl->value != sensor->info_priv.flip)
3485 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3487 sensor->info_priv.flip = ctrl->value;
3498 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3500 const struct v4l2_queryctrl *qctrl;
3501 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3502 struct sensor *sensor = to_sensor(client);
3504 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3508 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3512 switch (ext_ctrl->id)
3514 case V4L2_CID_SCENE:
3516 ext_ctrl->value = sensor->info_priv.scene;
3519 case V4L2_CID_EFFECT:
3521 ext_ctrl->value = sensor->info_priv.effect;
3524 case V4L2_CID_ZOOM_ABSOLUTE:
3526 ext_ctrl->value = sensor->info_priv.digitalzoom;
3529 case V4L2_CID_ZOOM_RELATIVE:
3533 case V4L2_CID_FOCUS_ABSOLUTE:
3537 case V4L2_CID_FOCUS_RELATIVE:
3541 case V4L2_CID_FLASH:
3543 ext_ctrl->value = sensor->info_priv.flash;
3551 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3553 const struct v4l2_queryctrl *qctrl;
3554 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3555 struct sensor *sensor = to_sensor(client);
3558 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3562 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3567 switch (ext_ctrl->id)
3569 #if CONFIG_SENSOR_Scene
3570 case V4L2_CID_SCENE:
3572 if (ext_ctrl->value != sensor->info_priv.scene)
3574 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3576 sensor->info_priv.scene = ext_ctrl->value;
3581 #if CONFIG_SENSOR_Effect
3582 case V4L2_CID_EFFECT:
3584 if (ext_ctrl->value != sensor->info_priv.effect)
3586 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3588 sensor->info_priv.effect= ext_ctrl->value;
3593 #if CONFIG_SENSOR_DigitalZoom
3594 case V4L2_CID_ZOOM_ABSOLUTE:
3596 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3599 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3601 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3603 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3605 sensor->info_priv.digitalzoom += val_offset;
3607 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3612 case V4L2_CID_ZOOM_RELATIVE:
3614 if (ext_ctrl->value)
3616 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3618 sensor->info_priv.digitalzoom += ext_ctrl->value;
3620 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3625 #if CONFIG_SENSOR_Focus
3626 case V4L2_CID_FOCUS_ABSOLUTE:
3628 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3633 case V4L2_CID_FOCUS_RELATIVE:
3635 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3638 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3641 case V4L2_CID_FOCUS_AUTO:
3643 if (ext_ctrl->value == 1) {
3644 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
3645 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
3646 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3650 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3651 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3652 if (ext_ctrl->value == 0)
3653 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3657 case V4L2_CID_FOCUS_CONTINUOUS:
3659 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3660 if (ext_ctrl->value == 1) {
3661 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3663 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3666 if (ext_ctrl->value == 0)
3667 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3672 #if CONFIG_SENSOR_Flash
3673 case V4L2_CID_FLASH:
3675 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3677 sensor->info_priv.flash = ext_ctrl->value;
3679 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3690 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3692 struct i2c_client *client = v4l2_get_subdevdata(sd);
3693 struct soc_camera_device *icd = client->dev.platform_data;
3694 int i, error_cnt=0, error_idx=-1;
3697 for (i=0; i<ext_ctrl->count; i++) {
3698 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3705 error_idx = ext_ctrl->count;
3707 if (error_idx != -1) {
3708 ext_ctrl->error_idx = error_idx;
3715 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3717 struct i2c_client *client = v4l2_get_subdevdata(sd);
3718 struct soc_camera_device *icd = client->dev.platform_data;
3719 int i, error_cnt=0, error_idx=-1;
3721 for (i=0; i<ext_ctrl->count; i++) {
3722 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3729 error_idx = ext_ctrl->count;
3731 if (error_idx != -1) {
3732 ext_ctrl->error_idx = error_idx;
3739 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3741 struct i2c_client *client = v4l2_get_subdevdata(sd);
3742 struct sensor *sensor = to_sensor(client);
3743 #if CONFIG_SENSOR_Focus
3744 struct soc_camera_device *icd = client->dev.platform_data;
3745 struct v4l2_mbus_framefmt mf;
3749 sensor->info_priv.enable = 1;
3750 #if CONFIG_SENSOR_Focus
3751 mf.width = icd->user_width;
3752 mf.height = icd->user_height;
3753 mf.code = sensor->info_priv.fmt.code;
3754 mf.colorspace = sensor->info_priv.fmt.colorspace;
3755 mf.field = V4L2_FIELD_NONE;
3756 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3757 if (sensor_fmt_capturechk(sd, &mf) == false) {
3758 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3759 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false);
3760 sensor->info_priv.affm_reinit = 0;
3764 } else if (enable == 0) {
3765 sensor->info_priv.enable = 0;
3766 #if CONFIG_SENSOR_Focus
3767 flush_workqueue(sensor->sensor_wq);
3773 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3774 * this wasn't our capture interface, so, we wait for the right one */
3775 static int sensor_video_probe(struct soc_camera_device *icd,
3776 struct i2c_client *client)
3780 struct sensor *sensor = to_sensor(client);
3782 /* We must have a parent by now. And it cannot be a wrong one.
3783 * So this entire test is completely redundant. */
3784 if (!icd->dev.parent ||
3785 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3788 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3790 goto sensor_video_probe_err;
3793 ret = sensor_write(client, 0x3008, 0x80);
3795 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3797 goto sensor_video_probe_err;
3799 mdelay(5); //delay 5 microseconds
3801 /* check if it is an sensor sensor */
3802 ret = sensor_read(client, 0x300a, &value);
3804 SENSOR_TR("read chip id high byte failed\n");
3806 goto sensor_video_probe_err;
3809 pid |= (value << 8);
3811 ret = sensor_read(client, 0x300b, &value);
3813 SENSOR_TR("read chip id low byte failed\n");
3815 goto sensor_video_probe_err;
3818 pid |= (value & 0xff);
3819 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3820 if (pid == SENSOR_ID) {
3821 sensor->model = SENSOR_V4L2_IDENT;
3823 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3825 goto sensor_video_probe_err;
3830 sensor_video_probe_err:
3833 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3835 struct i2c_client *client = v4l2_get_subdevdata(sd);
3836 struct soc_camera_device *icd = client->dev.platform_data;
3837 struct sensor *sensor = to_sensor(client);
3840 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3843 case RK29_CAM_SUBDEV_DEACTIVATE:
3845 sensor_deactivate(client);
3848 case RK29_CAM_SUBDEV_IOREQUEST:
3850 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3851 if (sensor->sensor_io_request != NULL) {
3853 for(j = 0;j < RK_CAM_NUM;j++){
3854 if (sensor->sensor_io_request->gpio_res[j].dev_name &&
3855 (strcmp(sensor->sensor_io_request->gpio_res[j].dev_name, dev_name(icd->pdev)) == 0)) {
3856 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[j];
3860 if(j == RK_CAM_NUM){
3861 SENSOR_TR("%s %s RK_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3863 goto sensor_ioctl_end;
3866 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3868 #if CONFIG_SENSOR_Flash
3869 if (sensor->sensor_gpio_res) {
3870 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3871 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3872 for (i = 0; i < icd->ops->num_controls; i++) {
3873 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3874 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3877 sensor->info_priv.flash = 0xff;
3878 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3886 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3895 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3896 enum v4l2_mbus_pixelcode *code)
3898 if (index >= ARRAY_SIZE(sensor_colour_fmts))
3901 *code = sensor_colour_fmts[index].code;
3904 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3905 .init = sensor_init,
3906 .g_ctrl = sensor_g_control,
3907 .s_ctrl = sensor_s_control,
3908 .g_ext_ctrls = sensor_g_ext_controls,
3909 .s_ext_ctrls = sensor_s_ext_controls,
3910 .g_chip_ident = sensor_g_chip_ident,
3911 .ioctl = sensor_ioctl,
3914 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3915 .s_mbus_fmt = sensor_s_fmt,
3916 .g_mbus_fmt = sensor_g_fmt,
3917 .try_mbus_fmt = sensor_try_fmt,
3918 .enum_mbus_fmt = sensor_enum_fmt,
3919 .s_stream = sensor_s_stream,
3922 static struct v4l2_subdev_ops sensor_subdev_ops = {
3923 .core = &sensor_subdev_core_ops,
3924 .video = &sensor_subdev_video_ops,
3927 static int sensor_probe(struct i2c_client *client,
3928 const struct i2c_device_id *did)
3930 struct sensor *sensor;
3931 struct soc_camera_device *icd = client->dev.platform_data;
3932 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3933 struct soc_camera_link *icl;
3936 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3938 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3942 icl = to_soc_camera_link(icd);
3944 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3948 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3949 dev_warn(&adapter->dev,
3950 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3954 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3958 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3960 /* Second stage probe - when a capture adapter is there */
3961 icd->ops = &sensor_ops;
3962 sensor->info_priv.fmt = sensor_colour_fmts[0];
3963 #if CONFIG_SENSOR_I2C_NOSCHED
3964 atomic_set(&sensor->tasklock_cnt,0);
3967 ret = sensor_video_probe(icd, client);
3970 i2c_set_clientdata(client, NULL);
3974 #if CONFIG_SENSOR_Focus
3975 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
3976 if (sensor->sensor_wq == NULL)
3977 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
3978 mutex_init(&sensor->wq_lock);
3982 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3986 static int sensor_remove(struct i2c_client *client)
3988 struct sensor *sensor = to_sensor(client);
3989 struct soc_camera_device *icd = client->dev.platform_data;
3991 #if CONFIG_SENSOR_Focus
3992 if (sensor->sensor_wq) {
3993 destroy_workqueue(sensor->sensor_wq);
3994 sensor->sensor_wq = NULL;
3999 i2c_set_clientdata(client, NULL);
4000 client->driver = NULL;
4006 static const struct i2c_device_id sensor_id[] = {
4007 {SENSOR_NAME_STRING(), 0 },
4010 MODULE_DEVICE_TABLE(i2c, sensor_id);
4012 static struct i2c_driver sensor_i2c_driver = {
4014 .name = SENSOR_NAME_STRING(),
4016 .probe = sensor_probe,
4017 .remove = sensor_remove,
4018 .id_table = sensor_id,
4021 static int __init sensor_mod_init(void)
4023 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4024 return i2c_add_driver(&sensor_i2c_driver);
4027 static void __exit sensor_mod_exit(void)
4029 i2c_del_driver(&sensor_i2c_driver);
4032 device_initcall_sync(sensor_mod_init);
4033 module_exit(sensor_mod_exit);
4035 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4036 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4037 MODULE_LICENSE("GPL");