2 o* Driver for MT9M001 CMOS Image Sensor from Micron
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>
23 #include <linux/vmalloc.h>
25 module_param(debug, int, S_IRUGO|S_IWUSR);
27 #define dprintk(level, fmt, arg...) do { \
29 printk(KERN_WARNING fmt , ## arg); } while (0)
31 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
32 #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_OV2659
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV2659
48 #define SENSOR_ID 0x2656
49 #define SENSOR_MIN_WIDTH 800
50 #define SENSOR_MIN_HEIGHT 600
51 #define SENSOR_MAX_WIDTH 1600
52 #define SENSOR_MAX_HEIGHT 1200
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_Focus 0
67 #define CONFIG_SENSOR_Exposure 0
68 #define CONFIG_SENSOR_Flash 1
69 #define CONFIG_SENSOR_Mirror 0
70 #define CONFIG_SENSOR_Flip 0
72 #define CONFIG_SENSOR_I2C_SPEED 350000 /* Hz */
73 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
74 #define CONFIG_SENSOR_I2C_NOSCHED 0
75 #define CONFIG_SENSOR_I2C_RDWRCHK 0
77 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
78 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
79 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
80 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
81 #define COLOR_TEMPERATURE_OFFICE_DN 3500
82 #define COLOR_TEMPERATURE_OFFICE_UP 5000
83 #define COLOR_TEMPERATURE_HOME_DN 2500
84 #define COLOR_TEMPERATURE_HOME_UP 3500
86 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
87 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
89 #define SENSOR_AF_IS_ERR (0x00<<0)
90 #define SENSOR_AF_IS_OK (0x01<<0)
91 #define SENSOR_INIT_IS_ERR (0x00<<28)
92 #define SENSOR_INIT_IS_OK (0x01<<28)
99 //flash off in fixed time to prevent from too hot , zyc
101 struct soc_camera_device *icd;
102 struct hrtimer timer;
104 static enum hrtimer_restart flash_off_func(struct hrtimer *timer);
106 static struct flash_timer flash_off_timer;
107 //for user defined if user want to customize the series , zyc
108 #ifdef CONFIG_OV2659_USER_DEFINED_SERIES
109 #include "ov2659_user_series.c"
111 /* init 800*600 SVGA */
112 static struct reginfo sensor_init_data[] =
117 //{0x0100, 0x01}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array
284 static struct reginfo sensor_720p[]=
290 //{0x0100, 0x01 }, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array
453 static struct reginfo sensor_uxga[] =
502 //{0x3a00,OV2659ReadReg(0x3a00)&0xfb},
504 //{0x3406,OV2659ReadReg(0x3406)|0x01},
509 {0x5066,0x18}, // zenghaihui 20110920 16
554 {0x3003, 0x80},//10fps
583 static struct reginfo sensor_sxga[] =
588 static struct reginfo sensor_xga[] =
593 static struct reginfo sensor_svga[] =
595 {0x0100, 0x00}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
642 {0x0100, 0x01}, //software wake
647 static struct reginfo sensor_vga[] =
653 static struct reginfo sensor_cif[] =
659 static struct reginfo sensor_qvga[] =
665 static struct reginfo sensor_qcif[] =
672 static struct reginfo ov2655_qqvga[] =
716 static struct reginfo ov2655_Sharpness_auto[] =
721 static struct reginfo ov2655_Sharpness1[] =
727 static struct reginfo ov2655_Sharpness2[][3] =
734 static struct reginfo ov2655_Sharpness3[] =
740 static struct reginfo ov2655_Sharpness4[]=
747 static struct reginfo ov2655_Sharpness5[] =
755 static struct reginfo sensor_ClrFmt_YUYV[]=
761 static struct reginfo sensor_ClrFmt_UYVY[]=
767 #if CONFIG_SENSOR_WhiteBalance
768 static struct reginfo sensor_WhiteB_Auto[]=
770 {0x3406, 0x00}, //AWB auto, bit[1]:0,auto
773 /* Cloudy Colour Temperature : 6500K - 8000K */
774 static struct reginfo sensor_WhiteB_Cloudy[]=
785 /* ClearDay Colour Temperature : 5000K - 6500K */
786 static struct reginfo sensor_WhiteB_ClearDay[]=
798 /* Office Colour Temperature : 3500K - 5000K */
799 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
812 /* Home Colour Temperature : 2500K - 3500K */
813 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
825 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
826 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
830 #if CONFIG_SENSOR_Brightness
831 static struct reginfo sensor_Brightness0[]=
837 static struct reginfo sensor_Brightness1[]=
844 static struct reginfo sensor_Brightness2[]=
851 static struct reginfo sensor_Brightness3[]=
858 static struct reginfo sensor_Brightness4[]=
865 static struct reginfo sensor_Brightness5[]=
871 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
872 sensor_Brightness4, sensor_Brightness5,NULL,
877 #if CONFIG_SENSOR_Effect
878 static struct reginfo sensor_Effect_Normal[] =
884 static struct reginfo sensor_Effect_WandB[] =
890 static struct reginfo sensor_Effect_Sepia[] =
898 static struct reginfo sensor_Effect_Negative[] =
901 {0x507b, 0x40}, //bit[6] negative
904 static struct reginfo sensor_Effect_Bluish[] =
913 static struct reginfo sensor_Effect_Green[] =
921 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
922 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
925 #if CONFIG_SENSOR_Exposure
926 static struct reginfo sensor_Exposure0[]=
931 static struct reginfo sensor_Exposure1[]=
936 static struct reginfo sensor_Exposure2[]=
941 static struct reginfo sensor_Exposure3[]=
946 static struct reginfo sensor_Exposure4[]=
951 static struct reginfo sensor_Exposure5[]=
956 static struct reginfo sensor_Exposure6[]=
961 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
962 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
965 #if CONFIG_SENSOR_Saturation
966 static struct reginfo sensor_Saturation0[]=
971 static struct reginfo sensor_Saturation1[]=
976 static struct reginfo sensor_Saturation2[]=
980 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
983 #if CONFIG_SENSOR_Contrast
984 static struct reginfo sensor_Contrast0[]=
989 static struct reginfo sensor_Contrast1[]=
994 static struct reginfo sensor_Contrast2[]=
999 static struct reginfo sensor_Contrast3[]=
1004 static struct reginfo sensor_Contrast4[]=
1010 static struct reginfo sensor_Contrast5[]=
1015 static struct reginfo sensor_Contrast6[]=
1019 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1020 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1024 #if CONFIG_SENSOR_Mirror
1025 static struct reginfo sensor_MirrorOn[]=
1030 static struct reginfo sensor_MirrorOff[]=
1034 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1036 #if CONFIG_SENSOR_Flip
1037 static struct reginfo sensor_FlipOn[]=
1042 static struct reginfo sensor_FlipOff[]=
1046 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1049 #if CONFIG_SENSOR_Scene
1050 static struct reginfo sensor_SceneAuto[] =
1056 static struct reginfo sensor_SceneNight[] =
1069 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1072 #if CONFIG_SENSOR_DigitalZoom
1073 static struct reginfo sensor_Zoom0[] =
1078 static struct reginfo sensor_Zoom1[] =
1083 static struct reginfo sensor_Zoom2[] =
1089 static struct reginfo sensor_Zoom3[] =
1093 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1095 static const struct v4l2_querymenu sensor_menus[] =
1097 #if CONFIG_SENSOR_WhiteBalance
1098 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1099 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1100 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1103 #if CONFIG_SENSOR_Effect
1104 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1105 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1106 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1109 #if CONFIG_SENSOR_Scene
1110 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1113 #if CONFIG_SENSOR_Flash
1114 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1115 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1119 static struct v4l2_queryctrl sensor_controls[] =
1121 #if CONFIG_SENSOR_WhiteBalance
1123 .id = V4L2_CID_DO_WHITE_BALANCE,
1124 .type = V4L2_CTRL_TYPE_MENU,
1125 .name = "White Balance Control",
1133 #if CONFIG_SENSOR_Brightness
1135 .id = V4L2_CID_BRIGHTNESS,
1136 .type = V4L2_CTRL_TYPE_INTEGER,
1137 .name = "Brightness Control",
1145 #if CONFIG_SENSOR_Effect
1147 .id = V4L2_CID_EFFECT,
1148 .type = V4L2_CTRL_TYPE_MENU,
1149 .name = "Effect Control",
1157 #if CONFIG_SENSOR_Exposure
1159 .id = V4L2_CID_EXPOSURE,
1160 .type = V4L2_CTRL_TYPE_INTEGER,
1161 .name = "Exposure Control",
1169 #if CONFIG_SENSOR_Saturation
1171 .id = V4L2_CID_SATURATION,
1172 .type = V4L2_CTRL_TYPE_INTEGER,
1173 .name = "Saturation Control",
1181 #if CONFIG_SENSOR_Contrast
1183 .id = V4L2_CID_CONTRAST,
1184 .type = V4L2_CTRL_TYPE_INTEGER,
1185 .name = "Contrast Control",
1193 #if CONFIG_SENSOR_Mirror
1195 .id = V4L2_CID_HFLIP,
1196 .type = V4L2_CTRL_TYPE_BOOLEAN,
1197 .name = "Mirror Control",
1205 #if CONFIG_SENSOR_Flip
1207 .id = V4L2_CID_VFLIP,
1208 .type = V4L2_CTRL_TYPE_BOOLEAN,
1209 .name = "Flip Control",
1217 #if CONFIG_SENSOR_Scene
1219 .id = V4L2_CID_SCENE,
1220 .type = V4L2_CTRL_TYPE_MENU,
1221 .name = "Scene Control",
1229 #if CONFIG_SENSOR_DigitalZoom
1231 .id = V4L2_CID_ZOOM_RELATIVE,
1232 .type = V4L2_CTRL_TYPE_INTEGER,
1233 .name = "DigitalZoom Control",
1239 .id = V4L2_CID_ZOOM_ABSOLUTE,
1240 .type = V4L2_CTRL_TYPE_INTEGER,
1241 .name = "DigitalZoom Control",
1249 #if CONFIG_SENSOR_Focus
1251 .id = V4L2_CID_FOCUS_RELATIVE,
1252 .type = V4L2_CTRL_TYPE_INTEGER,
1253 .name = "Focus Control",
1259 .id = V4L2_CID_FOCUS_ABSOLUTE,
1260 .type = V4L2_CTRL_TYPE_INTEGER,
1261 .name = "Focus Control",
1265 .default_value = 125,
1269 #if CONFIG_SENSOR_Flash
1271 .id = V4L2_CID_FLASH,
1272 .type = V4L2_CTRL_TYPE_MENU,
1273 .name = "Flash Control",
1282 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1283 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1284 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1285 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1286 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1287 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1288 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1289 static int sensor_resume(struct soc_camera_device *icd);
1290 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1291 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1292 #if CONFIG_SENSOR_Effect
1293 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1295 #if CONFIG_SENSOR_WhiteBalance
1296 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1298 static int sensor_deactivate(struct i2c_client *client);
1300 static struct soc_camera_ops sensor_ops =
1302 .suspend = sensor_suspend,
1303 .resume = sensor_resume,
1304 .set_bus_param = sensor_set_bus_param,
1305 .query_bus_param = sensor_query_bus_param,
1306 .controls = sensor_controls,
1307 .menus = sensor_menus,
1308 .num_controls = ARRAY_SIZE(sensor_controls),
1309 .num_menus = ARRAY_SIZE(sensor_menus),
1312 /* only one fixed colorspace per pixelcode */
1313 struct sensor_datafmt {
1314 enum v4l2_mbus_pixelcode code;
1315 enum v4l2_colorspace colorspace;
1318 /* Find a data format by a pixel code in an array */
1319 static const struct sensor_datafmt *sensor_find_datafmt(
1320 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1324 for (i = 0; i < n; i++)
1325 if (fmt[i].code == code)
1331 static const struct sensor_datafmt sensor_colour_fmts[] = {
1332 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1333 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1336 typedef struct sensor_info_priv_s
1350 unsigned char mirror; /* HFLIP */
1351 unsigned char flip; /* VFLIP */
1352 unsigned int winseqe_cur_addr;
1353 struct sensor_datafmt fmt;
1354 unsigned int funmodule_state;
1355 } sensor_info_priv_t;
1357 struct sensor_parameter
1359 unsigned int PreviewDummyPixels;
1360 unsigned int CaptureDummyPixels;
1361 unsigned int preview_exposure;
1362 unsigned short int preview_line_width;
1363 unsigned short int preview_gain;
1365 unsigned short int PreviewPclk;
1366 unsigned short int CapturePclk;
1372 struct v4l2_subdev subdev;
1373 struct i2c_client *client;
1374 sensor_info_priv_t info_priv;
1375 struct sensor_parameter parameter;
1376 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1377 #if CONFIG_SENSOR_I2C_NOSCHED
1378 atomic_t tasklock_cnt;
1380 struct rk29camera_platform_data *sensor_io_request;
1381 struct rk29camera_gpio_res *sensor_gpio_res;
1385 static struct sensor* to_sensor(const struct i2c_client *client)
1387 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1390 static int sensor_task_lock(struct i2c_client *client, int lock)
1392 #if CONFIG_SENSOR_I2C_NOSCHED
1394 struct sensor *sensor = to_sensor(client);
1397 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1398 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1399 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1403 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1404 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1405 goto sensor_task_lock_err;
1410 atomic_add(1, &sensor->tasklock_cnt);
1412 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1413 atomic_sub(1, &sensor->tasklock_cnt);
1415 if (atomic_read(&sensor->tasklock_cnt) == 0)
1420 sensor_task_lock_err:
1428 /* sensor register write */
1429 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1433 struct i2c_msg msg[1];
1436 buf[1] = reg & 0xFF;
1439 msg->addr = client->addr;
1440 msg->flags = client->flags;
1442 msg->len = sizeof(buf);
1443 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1444 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1449 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1450 err = i2c_transfer(client->adapter, msg, 1);
1455 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1463 /* sensor register read */
1464 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1468 struct i2c_msg msg[2];
1471 buf[1] = reg & 0xFF;
1473 msg[0].addr = client->addr;
1474 msg[0].flags = client->flags;
1476 msg[0].len = sizeof(buf);
1477 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1478 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1480 msg[1].addr = client->addr;
1481 msg[1].flags = client->flags|I2C_M_RD;
1484 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1485 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1489 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1490 err = i2c_transfer(client->adapter, msg, 2);
1496 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1504 /* write a array of registers */
1505 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1509 #if CONFIG_SENSOR_I2C_RDWRCHK
1514 if (sensor_task_lock(client, 1) < 0)
1515 goto sensor_write_array_end;
1517 while (regarray[i].reg != 0)
1519 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1523 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1527 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1529 goto sensor_write_array_end;
1532 #if CONFIG_SENSOR_I2C_RDWRCHK
1533 sensor_read(client, regarray[i].reg, &valchk);
1534 if (valchk != regarray[i].val)
1535 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1541 sensor_write_array_end:
1542 sensor_task_lock(client,0);
1545 #if CONFIG_SENSOR_I2C_RDWRCHK
1546 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1554 while (regarray[i].reg != 0)
1556 sensor_read(client, regarray[i].reg, &valchk);
1557 if (valchk != regarray[i].val)
1558 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1566 static int sensor_parameter_record(struct i2c_client *client)
1568 u8 ret_l,ret_m,ret_h;
1570 struct sensor *sensor = to_sensor(client);
1572 sensor_read(client,0x3a00, &ret_l);
1573 sensor_write(client,0x3a00, ret_l&0xfb);
1575 sensor_write(client,0x3503,0x07); //stop AE/AG
1577 sensor_read(client,0x3500,&ret_h);
1578 sensor_read(client,0x3501, &ret_m);
1579 sensor_read(client,0x3502, &ret_l);
1583 sensor->parameter.preview_exposure = ((tp_h<<12) & 0xF000) | ((tp_m<<4) & 0x0FF0) | ((tp_l>>4) & 0x0F);
1585 //Read back AGC Gain for preview
1586 sensor_read(client,0x350b, &ret_l);
1587 sensor->parameter.preview_gain = ret_l;
1589 sensor->parameter.CapturePclk = 24000;
1590 sensor->parameter.PreviewPclk = 24000;
1591 sensor->parameter.PreviewDummyPixels = 0;
1592 sensor->parameter.CaptureDummyPixels = 0;
1593 SENSOR_DG(" %s Read 0x350b=0x%02x PreviewExposure:%d 0x3500=0x%02x 0x3501=0x%02x 0x3502=0x%02x \n",
1594 SENSOR_NAME_STRING(), ret_l,sensor->parameter.preview_exposure,tp_h, tp_m, tp_l);
1597 #define OV2659_FULL_PERIOD_PIXEL_NUMS (1940) // default pixel#(w/o dummy pixels) in UXGA mode
1598 #define OV2659_FULL_PERIOD_LINE_NUMS (1238) // default line#(w/o dummy lines) in UXGA mode
1599 #define OV2659_PV_PERIOD_PIXEL_NUMS (970) // default pixel#(w/o dummy pixels) in SVGA mode
1600 #define OV2659_PV_PERIOD_LINE_NUMS (618) // default line#(w/o dummy lines) in SVGA mode
1602 /* SENSOR EXPOSURE LINE LIMITATION */
1603 #define OV2659_FULL_EXPOSURE_LIMITATION (1236)
1604 #define OV2659_PV_EXPOSURE_LIMITATION (618)
1607 #define OV2659_IMAGE_SENSOR_FULL_WIDTH (1600)
1608 #define OV2659_IMAGE_SENSOR_FULL_HEIGHT (1200)
1610 #define OV2659_FULL_GRAB_WIDTH (OV2659_IMAGE_SENSOR_FULL_WIDTH - 16)
1611 #define OV2659_FULL_GRAB_HEIGHT (OV2659_IMAGE_SENSOR_FULL_HEIGHT - 12)
1613 static void OV2659SetDummy(struct i2c_client *client,unsigned int dummy_pixels, unsigned int dummy_lines)
1616 unsigned int temp_reg1, temp_reg2;
1617 unsigned int temp_reg;
1619 if (dummy_pixels > 0)
1621 sensor_read(client,0x380D,&val); // HTS[b7~b0]
1623 sensor_read(client,0x380C,&val); // HTS[b15~b8]
1625 temp_reg = (temp_reg1 & 0xFF) | (temp_reg2 << 8);
1627 temp_reg += dummy_pixels;
1629 sensor_write(client,0x380D,(temp_reg&0xFF)); //HTS[7:0]
1630 sensor_write(client,0x380C,((temp_reg&0xFF00)>>8)); //HTS[15:8]
1633 if (dummy_lines > 0)
1635 sensor_read(client,0x380F,&val); // VTS[b7~b0]
1637 sensor_read(client,0x380E,&val); // VTS[b15~b8]
1639 temp_reg = (temp_reg1 & 0xFF) | (temp_reg2 << 8);
1641 temp_reg += dummy_lines;
1643 sensor_write(client,0x380F,(temp_reg&0xFF)); //VTS[7:0]
1644 sensor_write(client,0x380E,((temp_reg&0xFF00)>>8)); //VTS[15:8]
1646 } /* OV2659_set_dummy */
1647 static void OV2659WriteShutter(struct i2c_client *client,bool is_preview, unsigned int shutter)
1649 unsigned int extra_exposure_lines = 0;
1658 if (shutter <= OV2659_PV_EXPOSURE_LIMITATION)
1660 extra_exposure_lines = 0;
1664 extra_exposure_lines=shutter - OV2659_PV_EXPOSURE_LIMITATION;
1670 if (shutter <= OV2659_FULL_EXPOSURE_LIMITATION)
1672 extra_exposure_lines = 0;
1676 extra_exposure_lines = shutter - OV2659_FULL_EXPOSURE_LIMITATION;
1683 sensor_write(client,0x3502, (shutter & 0x00FF)); //AEC[7:0]
1684 sensor_write(client,0x3501, ((shutter & 0x0FF00) >>8)); //AEC[15:8]
1685 sensor_write(client,0x3500, ((shutter & 0xFF0000) >> 16));
1687 if(extra_exposure_lines>0)
1689 // set extra exposure line [aec add vts]
1690 sensor_write(client,0x3507, extra_exposure_lines & 0xFF); // EXVTS[b7~b0]
1691 sensor_write(client,0x3506, (extra_exposure_lines & 0xFF00) >> 8); // EXVTS[b15~b8]
1695 // set extra exposure line [aec add vts]
1696 sensor_write(client,0x3507, 0x00); // EXVTS[b7~b0]
1697 sensor_write(client,0x3506, 0x00); // EXVTS[b15~b8]
1700 } /* OV2659_write_shutter */
1701 static int sensor_ae_transfer(struct i2c_client *client)
1703 unsigned int prev_line_len,cap_line_len,shutter;
1704 struct sensor *sensor = to_sensor(client);
1707 shutter = sensor->parameter.preview_exposure;
1709 OV2659SetDummy(client,600,0);
1711 prev_line_len = OV2659_PV_PERIOD_PIXEL_NUMS + sensor->parameter.PreviewDummyPixels;
1712 cap_line_len = OV2659_FULL_PERIOD_PIXEL_NUMS + sensor->parameter.CaptureDummyPixels;
1713 shutter = (shutter * sensor->parameter.CapturePclk) / sensor->parameter.PreviewPclk;
1714 shutter = (shutter * prev_line_len) / cap_line_len;
1717 OV2659WriteShutter(client,0,shutter);
1724 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1726 struct soc_camera_link *icl = to_soc_camera_link(icd);
1729 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1732 case Sensor_PowerDown:
1734 if (icl->powerdown) {
1735 ret = icl->powerdown(icd->pdev, on);
1736 if (ret == RK29_CAM_IO_SUCCESS) {
1740 icl->reset(icd->pdev);
1742 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1744 goto sensor_power_end;
1751 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1752 struct sensor *sensor = to_sensor(client);
1754 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1755 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1757 //flash off after 2 secs
1758 hrtimer_cancel(&(flash_off_timer.timer));
1759 hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
1766 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1774 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
1775 struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
1776 sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
1777 SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
1782 static s32 sensor_init_width = 800;
1783 static s32 sensor_init_height = 600;
1784 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);
1785 static enum v4l2_mbus_pixelcode sensor_init_pixelcode = V4L2_MBUS_FMT_YUYV8_2X8;
1786 static struct reginfo* sensor_init_data_p = sensor_init_data;
1787 static struct reginfo* sensor_init_winseq_p = sensor_svga;
1788 static struct reginfo* sensor_init_winseq_board = NULL;
1789 static struct reginfo* sensor_init_data_board = NULL;
1790 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1792 struct i2c_client *client = v4l2_get_subdevdata(sd);
1793 struct soc_camera_device *icd = client->dev.platform_data;
1794 struct sensor *sensor = to_sensor(client);
1795 const struct v4l2_queryctrl *qctrl;
1796 const struct sensor_datafmt *fmt;
1798 int ret,pid = 0,i = 0,j=0;
1799 //if val ==1,mean that sensor need to be reinit
1800 struct rk29camera_platform_data* tmp_plat_data =sensor->sensor_io_request;
1802 sensor_init_data_p = sensor_init_data;
1803 sensor_init_winseq_p = sensor_svga;
1804 sensor_init_width = 800;
1805 sensor_init_height = 600;
1807 if (tmp_plat_data != NULL) {
1808 for(i = 0;i < RK_CAM_NUM;i++){
1809 if ((tmp_plat_data->sensor_init_data[i])&& tmp_plat_data->info[i].dev_name &&
1810 (strcmp(tmp_plat_data->info[i].dev_name, dev_name(icd->pdev)) == 0)) {
1816 if(tmp_plat_data &&(i < RK_CAM_NUM) && tmp_plat_data->sensor_init_data[i]){
1817 //user has defined the init data
1819 int tmp_init_data_size = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data_size;
1820 if(tmp_init_data_size > 2){//init data is valid
1821 if((sizeof(struct reginfo) != sizeof(struct reginfo_t))){
1822 if(sensor_init_data_board) {
1823 vfree(sensor_init_data_board);
1824 sensor_init_data_board = NULL;
1826 sensor_init_data_board = (struct reginfo*)vmalloc(tmp_init_data_size);
1827 if(!sensor_init_data_board)
1828 SENSOR_TR("%s :vmalloc init data erro !",__FUNCTION__);
1829 for(j = 0;j< tmp_init_data_size;j++) {
1830 sensor_init_data_board[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].reg;
1831 sensor_init_data_board[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].val;
1833 sensor_init_data_p = sensor_init_data_board;
1835 sensor_init_data_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data);
1839 int tmp_winseq_size = tmp_plat_data->sensor_init_data[i]->rk_sensor_winseq_size;
1840 if(tmp_winseq_size > 2){
1841 if(sizeof(struct reginfo) != sizeof(struct reginfo_t)){
1842 if(sensor_init_winseq_board) {
1843 vfree(sensor_init_winseq_board);
1844 sensor_init_winseq_board = NULL;
1846 sensor_init_winseq_board = (struct reginfo*)vmalloc(tmp_winseq_size);
1847 if(!sensor_init_winseq_board)
1848 SENSOR_TR("%s :vmalloc erro !",__FUNCTION__);
1849 for(j = 0;j< tmp_winseq_size;j++){
1850 sensor_init_winseq_board[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].reg;
1851 sensor_init_winseq_board[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].val;
1853 sensor_init_winseq_p = sensor_init_winseq_board;
1855 sensor_init_winseq_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq);
1858 //init width,height,bus,pixelcode
1859 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width != INVALID_VALUE)
1860 sensor_init_width = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width;
1861 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height != INVALID_VALUE)
1862 sensor_init_height = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height;
1863 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param != INVALID_VALUE)
1864 sensor_init_busparam = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param;
1865 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode != INVALID_VALUE)
1866 sensor_init_pixelcode = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode;
1868 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1870 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1872 goto sensor_INIT_ERR;
1876 if (sensor_task_lock(client,1)<0)
1877 goto sensor_INIT_ERR;
1879 ret = sensor_write(client, 0x0103, 0x01);
1882 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1884 goto sensor_INIT_ERR;
1886 mdelay(5); //delay 5 microseconds
1888 /* check if it is an sensor sensor */
1889 ret = sensor_read(client, 0x300a, &value);
1891 SENSOR_TR("read chip id high byte failed\n");
1893 goto sensor_INIT_ERR;
1896 pid |= (value << 8);
1898 ret = sensor_read(client, 0x300b, &value);
1900 SENSOR_TR("read chip id low byte failed\n");
1902 goto sensor_INIT_ERR;
1905 pid |= (value & 0xff);
1906 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1907 if (pid == SENSOR_ID) {
1908 sensor->model = SENSOR_V4L2_IDENT;
1910 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1912 goto sensor_INIT_ERR;
1915 ret = sensor_write_array(client, sensor_init_data_p);
1918 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1919 goto sensor_INIT_ERR;
1921 sensor_task_lock(client,0);
1923 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1924 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1926 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1928 goto sensor_INIT_ERR;
1930 sensor->info_priv.fmt = *fmt;
1932 /* sensor sensor information for initialization */
1933 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1935 sensor->info_priv.whiteBalance = qctrl->default_value;
1936 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1938 sensor->info_priv.brightness = qctrl->default_value;
1939 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1941 sensor->info_priv.effect = qctrl->default_value;
1942 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1944 sensor->info_priv.exposure = qctrl->default_value;
1946 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1948 sensor->info_priv.saturation = qctrl->default_value;
1949 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1951 sensor->info_priv.contrast = qctrl->default_value;
1952 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1954 sensor->info_priv.mirror = qctrl->default_value;
1955 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1957 sensor->info_priv.flip = qctrl->default_value;
1958 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1960 sensor->info_priv.scene = qctrl->default_value;
1961 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1963 sensor->info_priv.digitalzoom = qctrl->default_value;
1965 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1966 #if CONFIG_SENSOR_Focus
1968 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1970 sensor->info_priv.focus = qctrl->default_value;
1973 #if CONFIG_SENSOR_Flash
1974 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1976 sensor->info_priv.flash = qctrl->default_value;
1977 flash_off_timer.icd = icd;
1978 flash_off_timer.timer.function = flash_off_func;
1981 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);
1982 sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
1985 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1986 sensor_task_lock(client,0);
1987 sensor_deactivate(client);
1991 static int sensor_deactivate(struct i2c_client *client)
1993 struct soc_camera_device *icd = client->dev.platform_data;
1995 struct sensor *sensor = to_sensor(client);
1996 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1998 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1999 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2000 sensor_task_lock(client, 1);
2001 sensor_read(client,0x3000,®_val);
2002 sensor_write(client, 0x3000, reg_val&0xfc);
2003 sensor_write(client, 0x3001, 0x00);
2004 sensor_read(client,0x3002,®_val);
2005 sensor_write(client, 0x3002, reg_val&0x1f);
2006 sensor_task_lock(client, 0);
2008 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2010 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2011 icd->user_width = SENSOR_INIT_WIDTH;
2012 icd->user_height = SENSOR_INIT_HEIGHT;
2013 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2018 static struct reginfo sensor_power_down_sequence[]=
2022 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2025 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2027 if (pm_msg.event == PM_EVENT_SUSPEND) {
2028 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2029 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2031 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2034 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2036 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2041 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2047 static int sensor_resume(struct soc_camera_device *icd)
2051 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2053 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2057 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2063 static int sensor_set_bus_param(struct soc_camera_device *icd,
2064 unsigned long flags)
2070 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2072 struct soc_camera_link *icl = to_soc_camera_link(icd);
2073 unsigned long flags = SENSOR_BUS_PARAM;
2075 return soc_camera_apply_sensor_flags(icl, flags);
2078 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2080 struct i2c_client *client = v4l2_get_subdevdata(sd);
2081 struct soc_camera_device *icd = client->dev.platform_data;
2082 struct sensor *sensor = to_sensor(client);
2084 mf->width = icd->user_width;
2085 mf->height = icd->user_height;
2086 mf->code = sensor->info_priv.fmt.code;
2087 mf->colorspace = sensor->info_priv.fmt.colorspace;
2088 mf->field = V4L2_FIELD_NONE;
2092 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2096 if ((mf->width == 1024) && (mf->height == 768)) {
2098 } else if ((mf->width == 1280) && (mf->height == 1024)) {
2100 } else if ((mf->width == 1600) && (mf->height == 1200)) {
2102 } else if ((mf->width == 2048) && (mf->height == 1536)) {
2104 } else if ((mf->width == 2592) && (mf->height == 1944)) {
2109 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2113 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2117 if ((mf->width == 1280) && (mf->height == 720)) {
2119 } else if ((mf->width == 1920) && (mf->height == 1080)) {
2124 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2127 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2129 struct i2c_client *client = v4l2_get_subdevdata(sd);
2130 const struct sensor_datafmt *fmt;
2131 struct sensor *sensor = to_sensor(client);
2132 const struct v4l2_queryctrl *qctrl;
2133 struct soc_camera_device *icd = client->dev.platform_data;
2134 struct reginfo *winseqe_set_addr=NULL;
2135 int ret=0, set_w,set_h;
2137 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2138 ARRAY_SIZE(sensor_colour_fmts));
2141 goto sensor_s_fmt_end;
2144 if (sensor->info_priv.fmt.code != mf->code) {
2147 case V4L2_MBUS_FMT_YUYV8_2X8:
2149 winseqe_set_addr = sensor_ClrFmt_YUYV;
2152 case V4L2_MBUS_FMT_UYVY8_2X8:
2154 winseqe_set_addr = sensor_ClrFmt_UYVY;
2160 if (winseqe_set_addr != NULL) {
2161 sensor_write_array(client, winseqe_set_addr);
2162 sensor->info_priv.fmt.code = mf->code;
2163 sensor->info_priv.fmt.colorspace= mf->colorspace;
2164 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2166 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2173 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2175 winseqe_set_addr = sensor_qcif;
2179 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2181 winseqe_set_addr = sensor_qvga;
2185 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2187 winseqe_set_addr = sensor_cif;
2191 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2193 winseqe_set_addr = sensor_vga;
2197 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2199 winseqe_set_addr = sensor_svga;
2203 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
2205 winseqe_set_addr = sensor_720p;
2209 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
2211 winseqe_set_addr = sensor_xga;
2215 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2217 winseqe_set_addr = sensor_sxga;
2221 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2223 winseqe_set_addr = sensor_uxga;
2229 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2230 set_w = SENSOR_INIT_WIDTH;
2231 set_h = SENSOR_INIT_HEIGHT;
2232 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2235 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
2236 #if CONFIG_SENSOR_Flash
2237 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2238 sensor_parameter_record(client);
2239 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2240 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2241 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2243 } else { /* ddl@rock-chips.com : Video */
2244 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2245 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2246 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2250 ret |= sensor_write_array(client, winseqe_set_addr);
2252 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2253 #if CONFIG_SENSOR_Flash
2254 if (sensor_fmt_capturechk(sd,mf) == true) {
2255 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2256 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2257 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2261 goto sensor_s_fmt_end;
2264 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
2266 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2267 sensor_ae_transfer(client);
2268 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2269 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2270 if (sensor->info_priv.whiteBalance != 0) {
2271 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2272 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2275 sensor->info_priv.snap2preview = true;
2276 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2277 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2278 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2279 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2280 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2281 sensor->info_priv.video2preview = true;
2282 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2283 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2284 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2285 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2286 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2288 sensor->info_priv.video2preview = false;
2289 sensor->info_priv.snap2preview = false;
2292 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2296 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2306 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2308 struct i2c_client *client = v4l2_get_subdevdata(sd);
2309 struct sensor *sensor = to_sensor(client);
2310 const struct sensor_datafmt *fmt;
2311 int ret = 0,set_w,set_h;
2313 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2314 ARRAY_SIZE(sensor_colour_fmts));
2316 fmt = &sensor->info_priv.fmt;
2317 mf->code = fmt->code;
2320 if (mf->height > SENSOR_MAX_HEIGHT)
2321 mf->height = SENSOR_MAX_HEIGHT;
2322 else if (mf->height < SENSOR_MIN_HEIGHT)
2323 mf->height = SENSOR_MIN_HEIGHT;
2325 if (mf->width > SENSOR_MAX_WIDTH)
2326 mf->width = SENSOR_MAX_WIDTH;
2327 else if (mf->width < SENSOR_MIN_WIDTH)
2328 mf->width = SENSOR_MIN_WIDTH;
2333 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2338 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2343 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2348 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2353 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2358 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
2363 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
2368 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2373 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2379 { /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2380 set_w = SENSOR_INIT_WIDTH;
2381 set_h = SENSOR_INIT_HEIGHT;
2386 mf->colorspace = fmt->colorspace;
2391 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2393 struct i2c_client *client = v4l2_get_subdevdata(sd);
2395 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2398 if (id->match.addr != client->addr)
2401 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2406 #if CONFIG_SENSOR_Brightness
2407 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2409 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2411 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2413 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2415 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2417 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2420 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2424 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2428 #if CONFIG_SENSOR_Effect
2429 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2431 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2433 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2435 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2437 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2439 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2442 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2446 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2450 #if CONFIG_SENSOR_Exposure
2451 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2453 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2455 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2457 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2459 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2461 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2464 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2468 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2472 #if CONFIG_SENSOR_Saturation
2473 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2475 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2477 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2479 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2481 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2483 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2486 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2490 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2494 #if CONFIG_SENSOR_Contrast
2495 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2497 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2499 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2501 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2503 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2505 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2508 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2512 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2516 #if CONFIG_SENSOR_Mirror
2517 static int sensor_mirror(struct i2c_client *client, int on)
2523 err = sensor_read(client, 0x3821, &val);
2526 err = sensor_write(client, 0x3821, val);
2529 err = sensor_read(client, 0x3821, &val);
2532 err = sensor_write(client, 0x3821, val);
2538 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2540 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2542 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2544 if (sensor_mirror(client,value) != 0)
2545 SENSOR_TR("%s(%d): sensor_mirror failed, value:0x%x",__FUNCTION__, __LINE__,value);
2547 SENSOR_DG("%s(%d): sensor_mirror success, value:0x%x",__FUNCTION__, __LINE__,value);
2550 SENSOR_TR("\n %s..%s value = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2554 #if CONFIG_SENSOR_Flip
2555 static int sensor_flip(struct i2c_client *client, int on)
2561 err = sensor_read(client, 0x3820, &val);
2564 err = sensor_write(client, 0x3820, val);
2567 err = sensor_read(client, 0x3820, &val);
2570 err = sensor_write(client, 0x3820, val);
2576 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2578 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2580 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2582 if (sensor_flip(client,value) != 0)
2583 SENSOR_TR("%s(%d): sensor_flip failed, value:0x%x",__FUNCTION__, __LINE__,value);
2585 SENSOR_DG("%s(%d): sensor_flip success, value:0x%x",__FUNCTION__, __LINE__,value);
2588 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2592 #if CONFIG_SENSOR_Scene
2593 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2595 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2597 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2599 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2601 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2603 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2606 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2610 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2614 #if CONFIG_SENSOR_WhiteBalance
2615 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2617 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2619 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2621 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2623 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2625 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2628 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2632 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2636 #if CONFIG_SENSOR_DigitalZoom
2637 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2639 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2640 struct sensor *sensor = to_sensor(client);
2641 const struct v4l2_queryctrl *qctrl_info;
2642 int digitalzoom_cur, digitalzoom_total;
2644 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2648 digitalzoom_cur = sensor->info_priv.digitalzoom;
2649 digitalzoom_total = qctrl_info->maximum;
2651 if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
2653 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2657 if ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2659 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2663 if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
2665 value = digitalzoom_total - digitalzoom_cur;
2668 if ((value < 0) && ((digitalzoom_cur + value) < 0))
2670 value = 0 - digitalzoom_cur;
2673 digitalzoom_cur += value;
2675 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2677 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2679 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2682 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2689 #if CONFIG_SENSOR_Flash
2690 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2692 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2693 if (value == 3) { /* ddl@rock-chips.com: torch */
2694 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2696 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2698 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2702 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2707 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2709 struct i2c_client *client = v4l2_get_subdevdata(sd);
2710 struct sensor *sensor = to_sensor(client);
2711 const struct v4l2_queryctrl *qctrl;
2713 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2717 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2723 case V4L2_CID_BRIGHTNESS:
2725 ctrl->value = sensor->info_priv.brightness;
2728 case V4L2_CID_SATURATION:
2730 ctrl->value = sensor->info_priv.saturation;
2733 case V4L2_CID_CONTRAST:
2735 ctrl->value = sensor->info_priv.contrast;
2738 case V4L2_CID_DO_WHITE_BALANCE:
2740 ctrl->value = sensor->info_priv.whiteBalance;
2743 case V4L2_CID_EXPOSURE:
2745 ctrl->value = sensor->info_priv.exposure;
2748 case V4L2_CID_HFLIP:
2750 ctrl->value = sensor->info_priv.mirror;
2753 case V4L2_CID_VFLIP:
2755 ctrl->value = sensor->info_priv.flip;
2766 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2768 struct i2c_client *client = v4l2_get_subdevdata(sd);
2769 struct sensor *sensor = to_sensor(client);
2770 struct soc_camera_device *icd = client->dev.platform_data;
2771 const struct v4l2_queryctrl *qctrl;
2774 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2778 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2784 #if CONFIG_SENSOR_Brightness
2785 case V4L2_CID_BRIGHTNESS:
2787 if (ctrl->value != sensor->info_priv.brightness)
2789 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2793 sensor->info_priv.brightness = ctrl->value;
2798 #if CONFIG_SENSOR_Exposure
2799 case V4L2_CID_EXPOSURE:
2801 if (ctrl->value != sensor->info_priv.exposure)
2803 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2807 sensor->info_priv.exposure = ctrl->value;
2812 #if CONFIG_SENSOR_Saturation
2813 case V4L2_CID_SATURATION:
2815 if (ctrl->value != sensor->info_priv.saturation)
2817 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2821 sensor->info_priv.saturation = ctrl->value;
2826 #if CONFIG_SENSOR_Contrast
2827 case V4L2_CID_CONTRAST:
2829 if (ctrl->value != sensor->info_priv.contrast)
2831 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2835 sensor->info_priv.contrast = ctrl->value;
2840 #if CONFIG_SENSOR_WhiteBalance
2841 case V4L2_CID_DO_WHITE_BALANCE:
2843 if (ctrl->value != sensor->info_priv.whiteBalance)
2845 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2849 sensor->info_priv.whiteBalance = ctrl->value;
2854 #if CONFIG_SENSOR_Mirror
2855 case V4L2_CID_HFLIP:
2857 if (ctrl->value != sensor->info_priv.mirror)
2859 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2861 sensor->info_priv.mirror = ctrl->value;
2866 #if CONFIG_SENSOR_Flip
2867 case V4L2_CID_VFLIP:
2869 if (ctrl->value != sensor->info_priv.flip)
2871 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2873 sensor->info_priv.flip = ctrl->value;
2884 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2886 const struct v4l2_queryctrl *qctrl;
2887 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2888 struct sensor *sensor = to_sensor(client);
2890 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2894 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2898 switch (ext_ctrl->id)
2900 case V4L2_CID_SCENE:
2902 ext_ctrl->value = sensor->info_priv.scene;
2905 case V4L2_CID_EFFECT:
2907 ext_ctrl->value = sensor->info_priv.effect;
2910 case V4L2_CID_ZOOM_ABSOLUTE:
2912 ext_ctrl->value = sensor->info_priv.digitalzoom;
2915 case V4L2_CID_ZOOM_RELATIVE:
2919 case V4L2_CID_FOCUS_ABSOLUTE:
2921 ext_ctrl->value = sensor->info_priv.focus;
2924 case V4L2_CID_FOCUS_RELATIVE:
2928 case V4L2_CID_FLASH:
2930 ext_ctrl->value = sensor->info_priv.flash;
2938 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2940 const struct v4l2_queryctrl *qctrl;
2941 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2942 struct sensor *sensor = to_sensor(client);
2945 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2949 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2954 switch (ext_ctrl->id)
2956 #if CONFIG_SENSOR_Scene
2957 case V4L2_CID_SCENE:
2959 if (ext_ctrl->value != sensor->info_priv.scene)
2961 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2963 sensor->info_priv.scene = ext_ctrl->value;
2968 #if CONFIG_SENSOR_Effect
2969 case V4L2_CID_EFFECT:
2971 if (ext_ctrl->value != sensor->info_priv.effect)
2973 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2975 sensor->info_priv.effect= ext_ctrl->value;
2980 #if CONFIG_SENSOR_DigitalZoom
2981 case V4L2_CID_ZOOM_ABSOLUTE:
2983 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2986 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2988 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2990 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2992 sensor->info_priv.digitalzoom += val_offset;
2994 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2999 case V4L2_CID_ZOOM_RELATIVE:
3001 if (ext_ctrl->value)
3003 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3005 sensor->info_priv.digitalzoom += ext_ctrl->value;
3007 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3012 #if CONFIG_SENSOR_Focus
3013 case V4L2_CID_FOCUS_ABSOLUTE:
3015 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3018 if (ext_ctrl->value != sensor->info_priv.focus)
3020 val_offset = ext_ctrl->value -sensor->info_priv.focus;
3022 sensor->info_priv.focus += val_offset;
3027 case V4L2_CID_FOCUS_RELATIVE:
3029 if (ext_ctrl->value)
3031 sensor->info_priv.focus += ext_ctrl->value;
3033 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
3038 #if CONFIG_SENSOR_Flash
3039 case V4L2_CID_FLASH:
3041 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3043 sensor->info_priv.flash = ext_ctrl->value;
3045 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3056 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3058 struct i2c_client *client = v4l2_get_subdevdata(sd);
3059 struct soc_camera_device *icd = client->dev.platform_data;
3060 int i, error_cnt=0, error_idx=-1;
3063 for (i=0; i<ext_ctrl->count; i++) {
3064 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3071 error_idx = ext_ctrl->count;
3073 if (error_idx != -1) {
3074 ext_ctrl->error_idx = error_idx;
3081 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3083 struct i2c_client *client = v4l2_get_subdevdata(sd);
3084 struct soc_camera_device *icd = client->dev.platform_data;
3085 int i, error_cnt=0, error_idx=-1;
3088 for (i=0; i<ext_ctrl->count; i++) {
3089 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3096 error_idx = ext_ctrl->count;
3098 if (error_idx != -1) {
3099 ext_ctrl->error_idx = error_idx;
3106 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3107 * this wasn't our capture interface, so, we wait for the right one */
3108 static int sensor_video_probe(struct soc_camera_device *icd,
3109 struct i2c_client *client)
3113 struct sensor *sensor = to_sensor(client);
3115 /* We must have a parent by now. And it cannot be a wrong one.
3116 * So this entire test is completely redundant. */
3117 if (!icd->dev.parent ||
3118 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3121 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3122 SENSOR_TR("power down %s failed\n",SENSOR_NAME_STRING());
3124 goto sensor_video_probe_err;
3128 ret = sensor_write(client, 0x0103, 0x01);
3130 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3132 goto sensor_video_probe_err;
3134 mdelay(5); //delay 5 microseconds
3136 /* check if it is an sensor sensor */
3137 ret = sensor_read(client, 0x300a, &value);
3139 SENSOR_TR("read chip id high byte failed\n");
3141 goto sensor_video_probe_err;
3144 pid |= (value << 8);
3146 ret = sensor_read(client, 0x300b, &value);
3148 SENSOR_TR("read chip id low byte failed\n");
3150 goto sensor_video_probe_err;
3153 pid |= (value & 0xff);
3154 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3155 if (pid == SENSOR_ID) {
3156 sensor->model = SENSOR_V4L2_IDENT;
3158 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3160 goto sensor_video_probe_err;
3165 sensor_video_probe_err:
3170 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3172 struct i2c_client *client = v4l2_get_subdevdata(sd);
3173 struct soc_camera_device *icd = client->dev.platform_data;
3174 struct sensor *sensor = to_sensor(client);
3176 #if CONFIG_SENSOR_Flash
3180 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3183 case RK29_CAM_SUBDEV_DEACTIVATE:
3185 sensor_deactivate(client);
3189 case RK29_CAM_SUBDEV_IOREQUEST:
3191 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3192 if (sensor->sensor_io_request != NULL) {
3194 for(j = 0;j < RK_CAM_NUM;j++){
3195 if (sensor->sensor_io_request->gpio_res[j].dev_name &&
3196 (strcmp(sensor->sensor_io_request->gpio_res[j].dev_name, dev_name(icd->pdev)) == 0)) {
3197 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[j];
3201 if(j == RK_CAM_NUM){
3202 SENSOR_TR("%s %s RK_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3204 goto sensor_ioctl_end;
3208 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3210 #if CONFIG_SENSOR_Flash
3211 if (sensor->sensor_gpio_res) {
3212 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3213 for (i = 0; i < icd->ops->num_controls; i++) {
3214 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3215 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3216 sensor_controls[i].id=0xffff;
3219 sensor->info_priv.flash = 0xff;
3220 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3221 }else{ //two cameras are the same,need to deal diffrently ,zyc
3222 for (i = 0; i < icd->ops->num_controls; i++) {
3223 if(0xffff == icd->ops->controls[i].id){
3224 sensor_controls[i].id=V4L2_CID_FLASH;
3234 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3242 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3243 enum v4l2_mbus_pixelcode *code)
3245 if (index >= ARRAY_SIZE(sensor_colour_fmts))
3248 *code = sensor_colour_fmts[index].code;
3251 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3252 .init = sensor_init,
3253 .g_ctrl = sensor_g_control,
3254 .s_ctrl = sensor_s_control,
3255 .g_ext_ctrls = sensor_g_ext_controls,
3256 .s_ext_ctrls = sensor_s_ext_controls,
3257 .g_chip_ident = sensor_g_chip_ident,
3258 .ioctl = sensor_ioctl,
3261 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3262 .s_mbus_fmt = sensor_s_fmt,
3263 .g_mbus_fmt = sensor_g_fmt,
3264 .try_mbus_fmt = sensor_try_fmt,
3265 .enum_mbus_fmt = sensor_enum_fmt,
3267 static struct v4l2_subdev_ops sensor_subdev_ops = {
3268 .core = &sensor_subdev_core_ops,
3269 .video = &sensor_subdev_video_ops,
3272 static int sensor_probe(struct i2c_client *client,
3273 const struct i2c_device_id *did)
3275 struct sensor *sensor;
3276 struct soc_camera_device *icd = client->dev.platform_data;
3277 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3278 struct soc_camera_link *icl;
3281 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3283 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3287 icl = to_soc_camera_link(icd);
3289 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3293 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3294 dev_warn(&adapter->dev,
3295 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3299 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3303 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3305 /* Second stage probe - when a capture adapter is there */
3306 icd->ops = &sensor_ops;
3308 sensor->info_priv.fmt = sensor_colour_fmts[0];
3310 #if CONFIG_SENSOR_I2C_NOSCHED
3311 atomic_set(&sensor->tasklock_cnt,0);
3314 ret = sensor_video_probe(icd, client);
3317 i2c_set_clientdata(client, NULL);
3321 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3322 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3326 static int sensor_remove(struct i2c_client *client)
3328 struct sensor *sensor = to_sensor(client);
3329 struct soc_camera_device *icd = client->dev.platform_data;
3332 i2c_set_clientdata(client, NULL);
3333 client->driver = NULL;
3339 static const struct i2c_device_id sensor_id[] = {
3340 {SENSOR_NAME_STRING(), 0 },
3343 MODULE_DEVICE_TABLE(i2c, sensor_id);
3345 static struct i2c_driver sensor_i2c_driver = {
3347 .name = SENSOR_NAME_STRING(),
3349 .probe = sensor_probe,
3350 .remove = sensor_remove,
3351 .id_table = sensor_id,
3354 static int __init sensor_mod_init(void)
3356 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3357 return i2c_add_driver(&sensor_i2c_driver);
3360 static void __exit sensor_mod_exit(void)
3362 i2c_del_driver(&sensor_i2c_driver);
3365 device_initcall_sync(sensor_mod_init);
3366 module_exit(sensor_mod_exit);
3368 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3369 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3370 MODULE_LICENSE("GPL");