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>
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_OV7690
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV7690
48 #define SENSOR_ID 0x76
49 #define SENSOR_MIN_WIDTH 176
50 #define SENSOR_MIN_HEIGHT 144
51 #define SENSOR_MAX_WIDTH 640
52 #define SENSOR_MAX_HEIGHT 480
53 #define SENSOR_INIT_WIDTH 640 /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT 480
55 #define SENSOR_INIT_WINSEQADR sensor_vga
56 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_YUYV8_2X8
58 #define CONFIG_SENSOR_WhiteBalance 1
59 #define CONFIG_SENSOR_Brightness 0
60 #define CONFIG_SENSOR_Contrast 0
61 #define CONFIG_SENSOR_Saturation 0
62 #define CONFIG_SENSOR_Effect 1
63 #define CONFIG_SENSOR_Scene 0
64 #define CONFIG_SENSOR_DigitalZoom 0
65 #define CONFIG_SENSOR_Focus 0
66 #define CONFIG_SENSOR_Exposure 0
67 #define CONFIG_SENSOR_Flash 1
68 #define CONFIG_SENSOR_Mirror 0
69 #define CONFIG_SENSOR_Flip 0
71 #define CONFIG_SENSOR_I2C_SPEED 250000 /* Hz */
72 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
73 #define CONFIG_SENSOR_I2C_NOSCHED 0
74 #define CONFIG_SENSOR_I2C_RDWRCHK 0
76 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING|\
77 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
78 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
80 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
81 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
82 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
83 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
84 #define COLOR_TEMPERATURE_OFFICE_DN 3500
85 #define COLOR_TEMPERATURE_OFFICE_UP 5000
86 #define COLOR_TEMPERATURE_HOME_DN 2500
87 #define COLOR_TEMPERATURE_HOME_UP 3500
89 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
90 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
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 #if CONFIG_OV7690_USER_DEFINED_SERIES
109 #include "ov7690_user_series.c"
112 /* init 640X480 VGA */
113 static struct reginfo sensor_init_data[] =
167 {0xb9, 0x00}, //0x07//0x27
240 static struct reginfo sensor_720p[]=
247 static struct reginfo sensor_uxga[] =
253 static struct reginfo sensor_sxga[] =
258 static struct reginfo sensor_xga[] =
263 static struct reginfo sensor_svga[] =
269 static struct reginfo sensor_vga[] =
289 static struct reginfo sensor_cif[] =
295 static struct reginfo sensor_qvga[] =
315 static struct reginfo sensor_qcif[] =
336 static struct reginfo ov2655_qqvga[] =
343 static struct reginfo ov2655_Sharpness_auto[] =
348 static struct reginfo ov2655_Sharpness1[] =
354 static struct reginfo ov2655_Sharpness2[][3] =
361 static struct reginfo ov2655_Sharpness3[] =
367 static struct reginfo ov2655_Sharpness4[]=
374 static struct reginfo ov2655_Sharpness5[] =
382 static struct reginfo sensor_ClrFmt_YUYV[]=
387 static struct reginfo sensor_ClrFmt_UYVY[]=
393 #if CONFIG_SENSOR_WhiteBalance
394 static struct reginfo sensor_WhiteB_Auto[]=
399 /* Cloudy Colour Temperature : 6500K - 8000K */
400 static struct reginfo sensor_WhiteB_Cloudy[]=
408 /* ClearDay Colour Temperature : 5000K - 6500K */
409 static struct reginfo sensor_WhiteB_ClearDay[]=
417 /* Office Colour Temperature : 3500K - 5000K */
418 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
427 /* Home Colour Temperature : 2500K - 3500K */
428 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
436 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
437 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
441 #if CONFIG_SENSOR_Brightness
442 static struct reginfo sensor_Brightness0[]=
448 static struct reginfo sensor_Brightness1[]=
455 static struct reginfo sensor_Brightness2[]=
462 static struct reginfo sensor_Brightness3[]=
469 static struct reginfo sensor_Brightness4[]=
476 static struct reginfo sensor_Brightness5[]=
482 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
483 sensor_Brightness4, sensor_Brightness5,NULL,
488 #if CONFIG_SENSOR_Effect
489 static struct reginfo sensor_Effect_Normal[] =
495 static struct reginfo sensor_Effect_WandB[] =
503 static struct reginfo sensor_Effect_Sepia[] =
511 static struct reginfo sensor_Effect_Negative[] =
517 static struct reginfo sensor_Effect_Bluish[] =
526 static struct reginfo sensor_Effect_Green[] =
534 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
535 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
538 #if CONFIG_SENSOR_Exposure
539 static struct reginfo sensor_Exposure0[]=
544 static struct reginfo sensor_Exposure1[]=
549 static struct reginfo sensor_Exposure2[]=
554 static struct reginfo sensor_Exposure3[]=
559 static struct reginfo sensor_Exposure4[]=
564 static struct reginfo sensor_Exposure5[]=
569 static struct reginfo sensor_Exposure6[]=
574 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
575 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
578 #if CONFIG_SENSOR_Saturation
579 static struct reginfo sensor_Saturation0[]=
584 static struct reginfo sensor_Saturation1[]=
589 static struct reginfo sensor_Saturation2[]=
593 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
596 #if CONFIG_SENSOR_Contrast
597 static struct reginfo sensor_Contrast0[]=
602 static struct reginfo sensor_Contrast1[]=
607 static struct reginfo sensor_Contrast2[]=
612 static struct reginfo sensor_Contrast3[]=
617 static struct reginfo sensor_Contrast4[]=
623 static struct reginfo sensor_Contrast5[]=
628 static struct reginfo sensor_Contrast6[]=
632 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
633 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
637 #if CONFIG_SENSOR_Mirror
638 static struct reginfo sensor_MirrorOn[]=
643 static struct reginfo sensor_MirrorOff[]=
647 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
649 #if CONFIG_SENSOR_Flip
650 static struct reginfo sensor_FlipOn[]=
655 static struct reginfo sensor_FlipOff[]=
659 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
662 #if CONFIG_SENSOR_Scene
663 static struct reginfo sensor_SceneAuto[] =
668 static struct reginfo sensor_SceneNight[] =
672 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
675 #if CONFIG_SENSOR_DigitalZoom
676 static struct reginfo sensor_Zoom0[] =
681 static struct reginfo sensor_Zoom1[] =
686 static struct reginfo sensor_Zoom2[] =
692 static struct reginfo sensor_Zoom3[] =
696 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
698 static const struct v4l2_querymenu sensor_menus[] =
700 #if CONFIG_SENSOR_WhiteBalance
701 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
702 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
703 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
706 #if CONFIG_SENSOR_Effect
707 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
708 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
709 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
712 #if CONFIG_SENSOR_Scene
713 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
716 #if CONFIG_SENSOR_Flash
717 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
718 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
722 static struct v4l2_queryctrl sensor_controls[] =
724 #if CONFIG_SENSOR_WhiteBalance
726 .id = V4L2_CID_DO_WHITE_BALANCE,
727 .type = V4L2_CTRL_TYPE_MENU,
728 .name = "White Balance Control",
736 #if CONFIG_SENSOR_Brightness
738 .id = V4L2_CID_BRIGHTNESS,
739 .type = V4L2_CTRL_TYPE_INTEGER,
740 .name = "Brightness Control",
748 #if CONFIG_SENSOR_Effect
750 .id = V4L2_CID_EFFECT,
751 .type = V4L2_CTRL_TYPE_MENU,
752 .name = "Effect Control",
760 #if CONFIG_SENSOR_Exposure
762 .id = V4L2_CID_EXPOSURE,
763 .type = V4L2_CTRL_TYPE_INTEGER,
764 .name = "Exposure Control",
772 #if CONFIG_SENSOR_Saturation
774 .id = V4L2_CID_SATURATION,
775 .type = V4L2_CTRL_TYPE_INTEGER,
776 .name = "Saturation Control",
784 #if CONFIG_SENSOR_Contrast
786 .id = V4L2_CID_CONTRAST,
787 .type = V4L2_CTRL_TYPE_INTEGER,
788 .name = "Contrast Control",
796 #if CONFIG_SENSOR_Mirror
798 .id = V4L2_CID_HFLIP,
799 .type = V4L2_CTRL_TYPE_BOOLEAN,
800 .name = "Mirror Control",
808 #if CONFIG_SENSOR_Flip
810 .id = V4L2_CID_VFLIP,
811 .type = V4L2_CTRL_TYPE_BOOLEAN,
812 .name = "Flip Control",
820 #if CONFIG_SENSOR_Scene
822 .id = V4L2_CID_SCENE,
823 .type = V4L2_CTRL_TYPE_MENU,
824 .name = "Scene Control",
832 #if CONFIG_SENSOR_DigitalZoom
834 .id = V4L2_CID_ZOOM_RELATIVE,
835 .type = V4L2_CTRL_TYPE_INTEGER,
836 .name = "DigitalZoom Control",
842 .id = V4L2_CID_ZOOM_ABSOLUTE,
843 .type = V4L2_CTRL_TYPE_INTEGER,
844 .name = "DigitalZoom Control",
852 #if CONFIG_SENSOR_Focus
854 .id = V4L2_CID_FOCUS_RELATIVE,
855 .type = V4L2_CTRL_TYPE_INTEGER,
856 .name = "Focus Control",
862 .id = V4L2_CID_FOCUS_ABSOLUTE,
863 .type = V4L2_CTRL_TYPE_INTEGER,
864 .name = "Focus Control",
868 .default_value = 125,
872 #if CONFIG_SENSOR_Flash
874 .id = V4L2_CID_FLASH,
875 .type = V4L2_CTRL_TYPE_MENU,
876 .name = "Flash Control",
885 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
886 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
887 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
888 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
889 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
890 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
891 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
892 static int sensor_resume(struct soc_camera_device *icd);
893 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
894 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
895 #if CONFIG_SENSOR_Effect
896 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
898 #if CONFIG_SENSOR_WhiteBalance
899 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
901 static int sensor_deactivate(struct i2c_client *client);
903 static struct soc_camera_ops sensor_ops =
905 .suspend = sensor_suspend,
906 .resume = sensor_resume,
907 .set_bus_param = sensor_set_bus_param,
908 .query_bus_param = sensor_query_bus_param,
909 .controls = sensor_controls,
910 .menus = sensor_menus,
911 .num_controls = ARRAY_SIZE(sensor_controls),
912 .num_menus = ARRAY_SIZE(sensor_menus),
915 /* only one fixed colorspace per pixelcode */
916 struct sensor_datafmt {
917 enum v4l2_mbus_pixelcode code;
918 enum v4l2_colorspace colorspace;
921 /* Find a data format by a pixel code in an array */
922 static const struct sensor_datafmt *sensor_find_datafmt(
923 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
927 for (i = 0; i < n; i++)
928 if (fmt[i].code == code)
934 static const struct sensor_datafmt sensor_colour_fmts[] = {
935 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
936 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
939 typedef struct sensor_info_priv_s
953 unsigned char mirror; /* HFLIP */
954 unsigned char flip; /* VFLIP */
955 unsigned int winseqe_cur_addr;
956 struct sensor_datafmt fmt;
957 unsigned int funmodule_state;
958 } sensor_info_priv_t;
962 struct v4l2_subdev subdev;
963 struct i2c_client *client;
964 sensor_info_priv_t info_priv;
965 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
966 #if CONFIG_SENSOR_I2C_NOSCHED
967 atomic_t tasklock_cnt;
969 struct rk29camera_platform_data *sensor_io_request;
970 struct rk29camera_gpio_res *sensor_gpio_res;
974 static struct sensor* to_sensor(const struct i2c_client *client)
976 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
979 static int sensor_task_lock(struct i2c_client *client, int lock)
981 #if CONFIG_SENSOR_I2C_NOSCHED
983 struct sensor *sensor = to_sensor(client);
986 if (atomic_read(&sensor->tasklock_cnt) == 0) {
987 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
988 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
992 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
993 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
994 goto sensor_task_lock_err;
999 atomic_add(1, &sensor->tasklock_cnt);
1001 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1002 atomic_sub(1, &sensor->tasklock_cnt);
1004 if (atomic_read(&sensor->tasklock_cnt) == 0)
1009 sensor_task_lock_err:
1017 /* sensor register write */
1018 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1022 struct i2c_msg msg[1];
1024 buf[0] = reg & 0xFF;
1027 msg->addr = client->addr;
1028 msg->flags = client->flags;
1030 msg->len = sizeof(buf);
1031 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1032 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1037 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1038 err = i2c_transfer(client->adapter, msg, 1);
1043 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1051 /* sensor register read */
1052 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1056 struct i2c_msg msg[2];
1060 msg[0].addr = client->addr;
1061 msg[0].flags = client->flags;
1063 msg[0].len = sizeof(buf);
1064 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1065 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1067 msg[1].addr = client->addr;
1068 msg[1].flags = client->flags|I2C_M_RD;
1071 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1072 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1076 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1077 err = i2c_transfer(client->adapter, msg, 2);
1083 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1091 /* write a array of registers */
1092 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1096 #if CONFIG_SENSOR_I2C_RDWRCHK
1101 if (sensor_task_lock(client, 1) < 0)
1102 goto sensor_write_array_end;
1104 while (regarray[i].reg != 0)
1106 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1110 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1114 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1116 goto sensor_write_array_end;
1119 #if CONFIG_SENSOR_I2C_RDWRCHK
1120 sensor_read(client, regarray[i].reg, &valchk);
1121 if (valchk != regarray[i].val)
1122 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1128 sensor_write_array_end:
1129 sensor_task_lock(client,0);
1132 #if CONFIG_SENSOR_I2C_RDWRCHK
1133 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1141 while (regarray[i].reg != 0)
1143 sensor_read(client, regarray[i].reg, &valchk);
1144 if (valchk != regarray[i].val)
1145 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1152 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1154 struct soc_camera_link *icl = to_soc_camera_link(icd);
1157 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1160 case Sensor_PowerDown:
1162 if (icl->powerdown) {
1165 ret = icl->powerdown(icd->pdev, on);
1166 if (ret == RK29_CAM_IO_SUCCESS) {
1170 icl->reset(icd->pdev);
1172 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1174 goto sensor_power_end;
1181 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1182 struct sensor *sensor = to_sensor(client);
1184 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1185 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1187 //flash off after 2 secs
1188 hrtimer_cancel(&(flash_off_timer.timer));
1189 hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
1196 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1204 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
1205 struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
1206 sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
1207 SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
1212 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1214 struct i2c_client *client = v4l2_get_subdevdata(sd);
1215 struct soc_camera_device *icd = client->dev.platform_data;
1216 struct sensor *sensor = to_sensor(client);
1217 const struct v4l2_queryctrl *qctrl;
1218 const struct sensor_datafmt *fmt;
1221 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1223 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1225 goto sensor_INIT_ERR;
1229 if (sensor_task_lock(client,1)<0)
1230 goto sensor_INIT_ERR;
1231 ret = sensor_write(client, 0x12, 0x80);
1234 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1236 goto sensor_INIT_ERR;
1238 mdelay(5); //delay 5 microseconds
1240 ret = sensor_write_array(client, sensor_init_data);
1243 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1244 goto sensor_INIT_ERR;
1246 sensor_task_lock(client,0);
1248 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1249 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1251 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1253 goto sensor_INIT_ERR;
1255 sensor->info_priv.fmt = *fmt;
1257 /* sensor sensor information for initialization */
1258 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1260 sensor->info_priv.whiteBalance = qctrl->default_value;
1261 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1263 sensor->info_priv.brightness = qctrl->default_value;
1264 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1266 sensor->info_priv.effect = qctrl->default_value;
1267 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1269 sensor->info_priv.exposure = qctrl->default_value;
1271 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1273 sensor->info_priv.saturation = qctrl->default_value;
1274 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1276 sensor->info_priv.contrast = qctrl->default_value;
1277 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1279 sensor->info_priv.mirror = qctrl->default_value;
1280 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1282 sensor->info_priv.flip = qctrl->default_value;
1283 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1285 sensor->info_priv.scene = qctrl->default_value;
1286 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1288 sensor->info_priv.digitalzoom = qctrl->default_value;
1290 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1291 #if CONFIG_SENSOR_Focus
1293 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1295 sensor->info_priv.focus = qctrl->default_value;
1298 #if CONFIG_SENSOR_Flash
1299 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1301 sensor->info_priv.flash = qctrl->default_value;
1302 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1303 flash_off_timer.icd = icd;
1304 flash_off_timer.timer.function = flash_off_func;
1307 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);
1311 sensor_task_lock(client,0);
1312 sensor_deactivate(client);
1316 static int sensor_deactivate(struct i2c_client *client)
1318 struct soc_camera_device *icd = client->dev.platform_data;
1321 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1323 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1324 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1325 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1326 icd->user_width = SENSOR_INIT_WIDTH;
1327 icd->user_height = SENSOR_INIT_HEIGHT;
1331 static struct reginfo sensor_power_down_sequence[]=
1335 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1338 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1340 if (pm_msg.event == PM_EVENT_SUSPEND) {
1341 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1342 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1344 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1347 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1349 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1354 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1360 static int sensor_resume(struct soc_camera_device *icd)
1364 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1366 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1370 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1376 static int sensor_set_bus_param(struct soc_camera_device *icd,
1377 unsigned long flags)
1383 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1385 struct soc_camera_link *icl = to_soc_camera_link(icd);
1386 unsigned long flags = SENSOR_BUS_PARAM;
1388 return soc_camera_apply_sensor_flags(icl, flags);
1391 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1393 struct i2c_client *client = v4l2_get_subdevdata(sd);
1394 struct soc_camera_device *icd = client->dev.platform_data;
1395 struct sensor *sensor = to_sensor(client);
1397 mf->width = icd->user_width;
1398 mf->height = icd->user_height;
1399 mf->code = sensor->info_priv.fmt.code;
1400 mf->colorspace = sensor->info_priv.fmt.colorspace;
1401 mf->field = V4L2_FIELD_NONE;
1405 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1409 if ((mf->width == 1024) && (mf->height == 768)) {
1411 } else if ((mf->width == 1280) && (mf->height == 1024)) {
1413 } else if ((mf->width == 1600) && (mf->height == 1200)) {
1415 } else if ((mf->width == 2048) && (mf->height == 1536)) {
1417 } else if ((mf->width == 2592) && (mf->height == 1944)) {
1422 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1426 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1430 if ((mf->width == 1280) && (mf->height == 720)) {
1432 } else if ((mf->width == 1920) && (mf->height == 1080)) {
1437 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1440 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1442 struct i2c_client *client = v4l2_get_subdevdata(sd);
1443 const struct sensor_datafmt *fmt;
1444 struct sensor *sensor = to_sensor(client);
1445 const struct v4l2_queryctrl *qctrl;
1446 struct soc_camera_device *icd = client->dev.platform_data;
1447 struct reginfo *winseqe_set_addr=NULL;
1449 int ret=0, set_w,set_h;
1451 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1452 ARRAY_SIZE(sensor_colour_fmts));
1455 goto sensor_s_fmt_end;
1458 if (sensor->info_priv.fmt.code != mf->code) {
1461 case V4L2_MBUS_FMT_YUYV8_2X8:
1463 //winseqe_set_addr = sensor_ClrFmt_YUYV;
1464 sensor_read(client, 0x3a, &readval);
1465 sensor_write(client,0x3a, readval&0xf7);
1466 sensor_read(client,0x3d,&readval);
1467 sensor_write(client,0x3d,readval&0xfe);
1470 case V4L2_MBUS_FMT_UYVY8_2X8:
1472 //winseqe_set_addr = sensor_ClrFmt_UYVY;
1473 sensor_read(client, 0x3a, &readval);
1474 sensor_write(client,0x3a, readval|0x08);
1475 sensor_read(client,0x3d,&readval);
1476 sensor_write(client,0x3d,readval&0xfe);
1482 if (winseqe_set_addr != NULL) {
1483 sensor_write_array(client, winseqe_set_addr);
1484 sensor->info_priv.fmt.code = mf->code;
1485 sensor->info_priv.fmt.colorspace= mf->colorspace;
1486 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1488 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1495 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1497 winseqe_set_addr = sensor_qcif;
1501 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1503 winseqe_set_addr = sensor_qvga;
1507 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1509 winseqe_set_addr = sensor_cif;
1513 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1515 winseqe_set_addr = sensor_vga;
1519 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1521 winseqe_set_addr = sensor_svga;
1525 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
1527 winseqe_set_addr = sensor_720p;
1531 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
1533 winseqe_set_addr = sensor_xga;
1537 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1539 winseqe_set_addr = sensor_vga; //sensor_sxga;
1543 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1545 winseqe_set_addr = sensor_uxga;
1551 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1552 set_w = SENSOR_INIT_WIDTH;
1553 set_h = SENSOR_INIT_HEIGHT;
1554 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1557 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1558 #if CONFIG_SENSOR_Flash
1559 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
1560 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1561 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1562 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1564 } else { /* ddl@rock-chips.com : Video */
1565 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1566 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1567 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1571 ret |= sensor_write_array(client, winseqe_set_addr);
1573 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1574 #if CONFIG_SENSOR_Flash
1575 if (sensor_fmt_capturechk(sd,mf) == true) {
1576 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1577 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1578 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1582 goto sensor_s_fmt_end;
1585 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1587 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
1588 #if CONFIG_SENSOR_Effect
1589 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1590 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1592 #if CONFIG_SENSOR_WhiteBalance
1593 if (sensor->info_priv.whiteBalance != 0) {
1594 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1595 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1598 sensor->info_priv.snap2preview = true;
1599 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
1600 #if CONFIG_SENSOR_Effect
1601 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1602 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1604 #if CONFIG_SENSOR_WhiteBalance
1605 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1606 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1608 sensor->info_priv.video2preview = true;
1609 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1610 #if CONFIG_SENSOR_Effect
1611 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1612 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1614 #if CONFIG_SENSOR_WhiteBalance
1615 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1616 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1618 sensor->info_priv.video2preview = false;
1619 sensor->info_priv.snap2preview = false;
1622 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1626 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1636 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1638 struct i2c_client *client = v4l2_get_subdevdata(sd);
1639 struct sensor *sensor = to_sensor(client);
1640 const struct sensor_datafmt *fmt;
1643 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1644 ARRAY_SIZE(sensor_colour_fmts));
1646 fmt = &sensor->info_priv.fmt;
1647 mf->code = fmt->code;
1650 if (mf->height > SENSOR_MAX_HEIGHT)
1651 mf->height = SENSOR_MAX_HEIGHT;
1652 else if (mf->height < SENSOR_MIN_HEIGHT)
1653 mf->height = SENSOR_MIN_HEIGHT;
1655 if (mf->width > SENSOR_MAX_WIDTH)
1656 mf->width = SENSOR_MAX_WIDTH;
1657 else if (mf->width < SENSOR_MIN_WIDTH)
1658 mf->width = SENSOR_MIN_WIDTH;
1660 mf->colorspace = fmt->colorspace;
1665 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1667 struct i2c_client *client = v4l2_get_subdevdata(sd);
1669 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1672 if (id->match.addr != client->addr)
1675 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
1680 #if CONFIG_SENSOR_Brightness
1681 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1683 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1685 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1687 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1689 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1691 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1694 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1698 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1702 #if CONFIG_SENSOR_Effect
1703 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1705 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1707 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1709 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1711 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1713 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1716 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1720 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1724 #if CONFIG_SENSOR_Exposure
1725 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1727 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1729 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1731 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1733 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1735 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1738 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1742 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1746 #if CONFIG_SENSOR_Saturation
1747 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1749 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1751 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1753 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1755 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1757 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1760 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1764 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1768 #if CONFIG_SENSOR_Contrast
1769 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1771 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1773 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1775 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1777 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1779 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1782 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1786 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1790 #if CONFIG_SENSOR_Mirror
1791 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1793 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1795 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1797 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1799 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1801 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1804 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1808 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1812 #if CONFIG_SENSOR_Flip
1813 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1815 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1817 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1819 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1821 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1823 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1826 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1830 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1834 #if CONFIG_SENSOR_Scene
1835 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1837 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1839 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1841 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1843 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1845 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1848 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1852 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1856 #if CONFIG_SENSOR_WhiteBalance
1857 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1859 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1861 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1863 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
1865 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
1867 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1870 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1874 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1878 #if CONFIG_SENSOR_DigitalZoom
1879 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
1881 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1882 struct sensor *sensor = to_sensor(client);
1883 const struct v4l2_queryctrl *qctrl_info;
1884 int digitalzoom_cur, digitalzoom_total;
1886 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1890 digitalzoom_cur = sensor->info_priv.digitalzoom;
1891 digitalzoom_total = qctrl_info->maximum;
1893 if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
1895 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1899 if ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
1901 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1905 if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
1907 value = digitalzoom_total - digitalzoom_cur;
1910 if ((value < 0) && ((digitalzoom_cur + value) < 0))
1912 value = 0 - digitalzoom_cur;
1915 digitalzoom_cur += value;
1917 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
1919 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
1921 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1924 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1931 #if CONFIG_SENSOR_Flash
1932 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1934 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
1935 if (value == 3) { /* ddl@rock-chips.com: torch */
1936 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
1938 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1940 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1944 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1949 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1951 struct i2c_client *client = v4l2_get_subdevdata(sd);
1952 struct sensor *sensor = to_sensor(client);
1953 const struct v4l2_queryctrl *qctrl;
1955 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
1959 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
1965 case V4L2_CID_BRIGHTNESS:
1967 ctrl->value = sensor->info_priv.brightness;
1970 case V4L2_CID_SATURATION:
1972 ctrl->value = sensor->info_priv.saturation;
1975 case V4L2_CID_CONTRAST:
1977 ctrl->value = sensor->info_priv.contrast;
1980 case V4L2_CID_DO_WHITE_BALANCE:
1982 ctrl->value = sensor->info_priv.whiteBalance;
1985 case V4L2_CID_EXPOSURE:
1987 ctrl->value = sensor->info_priv.exposure;
1990 case V4L2_CID_HFLIP:
1992 ctrl->value = sensor->info_priv.mirror;
1995 case V4L2_CID_VFLIP:
1997 ctrl->value = sensor->info_priv.flip;
2008 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2010 struct i2c_client *client = v4l2_get_subdevdata(sd);
2011 struct sensor *sensor = to_sensor(client);
2012 struct soc_camera_device *icd = client->dev.platform_data;
2013 const struct v4l2_queryctrl *qctrl;
2016 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2020 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2026 #if CONFIG_SENSOR_Brightness
2027 case V4L2_CID_BRIGHTNESS:
2029 if (ctrl->value != sensor->info_priv.brightness)
2031 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2035 sensor->info_priv.brightness = ctrl->value;
2040 #if CONFIG_SENSOR_Exposure
2041 case V4L2_CID_EXPOSURE:
2043 if (ctrl->value != sensor->info_priv.exposure)
2045 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2049 sensor->info_priv.exposure = ctrl->value;
2054 #if CONFIG_SENSOR_Saturation
2055 case V4L2_CID_SATURATION:
2057 if (ctrl->value != sensor->info_priv.saturation)
2059 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2063 sensor->info_priv.saturation = ctrl->value;
2068 #if CONFIG_SENSOR_Contrast
2069 case V4L2_CID_CONTRAST:
2071 if (ctrl->value != sensor->info_priv.contrast)
2073 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2077 sensor->info_priv.contrast = ctrl->value;
2082 #if CONFIG_SENSOR_WhiteBalance
2083 case V4L2_CID_DO_WHITE_BALANCE:
2085 if (ctrl->value != sensor->info_priv.whiteBalance)
2087 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2091 sensor->info_priv.whiteBalance = ctrl->value;
2096 #if CONFIG_SENSOR_Mirror
2097 case V4L2_CID_HFLIP:
2099 if (ctrl->value != sensor->info_priv.mirror)
2101 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2103 sensor->info_priv.mirror = ctrl->value;
2108 #if CONFIG_SENSOR_Flip
2109 case V4L2_CID_VFLIP:
2111 if (ctrl->value != sensor->info_priv.flip)
2113 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2115 sensor->info_priv.flip = ctrl->value;
2126 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2128 const struct v4l2_queryctrl *qctrl;
2129 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2130 struct sensor *sensor = to_sensor(client);
2132 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2136 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2140 switch (ext_ctrl->id)
2142 case V4L2_CID_SCENE:
2144 ext_ctrl->value = sensor->info_priv.scene;
2147 case V4L2_CID_EFFECT:
2149 ext_ctrl->value = sensor->info_priv.effect;
2152 case V4L2_CID_ZOOM_ABSOLUTE:
2154 ext_ctrl->value = sensor->info_priv.digitalzoom;
2157 case V4L2_CID_ZOOM_RELATIVE:
2161 case V4L2_CID_FOCUS_ABSOLUTE:
2163 ext_ctrl->value = sensor->info_priv.focus;
2166 case V4L2_CID_FOCUS_RELATIVE:
2170 case V4L2_CID_FLASH:
2172 ext_ctrl->value = sensor->info_priv.flash;
2180 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2182 const struct v4l2_queryctrl *qctrl;
2183 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2184 struct sensor *sensor = to_sensor(client);
2187 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2191 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2196 switch (ext_ctrl->id)
2198 #if CONFIG_SENSOR_Scene
2199 case V4L2_CID_SCENE:
2201 if (ext_ctrl->value != sensor->info_priv.scene)
2203 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2205 sensor->info_priv.scene = ext_ctrl->value;
2210 #if CONFIG_SENSOR_Effect
2211 case V4L2_CID_EFFECT:
2213 if (ext_ctrl->value != sensor->info_priv.effect)
2215 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2217 sensor->info_priv.effect= ext_ctrl->value;
2222 #if CONFIG_SENSOR_DigitalZoom
2223 case V4L2_CID_ZOOM_ABSOLUTE:
2225 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2228 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2230 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2232 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2234 sensor->info_priv.digitalzoom += val_offset;
2236 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2241 case V4L2_CID_ZOOM_RELATIVE:
2243 if (ext_ctrl->value)
2245 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2247 sensor->info_priv.digitalzoom += ext_ctrl->value;
2249 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2254 #if CONFIG_SENSOR_Focus
2255 case V4L2_CID_FOCUS_ABSOLUTE:
2257 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2260 if (ext_ctrl->value != sensor->info_priv.focus)
2262 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2264 sensor->info_priv.focus += val_offset;
2269 case V4L2_CID_FOCUS_RELATIVE:
2271 if (ext_ctrl->value)
2273 sensor->info_priv.focus += ext_ctrl->value;
2275 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2280 #if CONFIG_SENSOR_Flash
2281 case V4L2_CID_FLASH:
2283 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2285 sensor->info_priv.flash = ext_ctrl->value;
2287 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2298 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2300 struct i2c_client *client = v4l2_get_subdevdata(sd);
2301 struct soc_camera_device *icd = client->dev.platform_data;
2302 int i, error_cnt=0, error_idx=-1;
2305 for (i=0; i<ext_ctrl->count; i++) {
2306 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2313 error_idx = ext_ctrl->count;
2315 if (error_idx != -1) {
2316 ext_ctrl->error_idx = error_idx;
2323 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2325 struct i2c_client *client = v4l2_get_subdevdata(sd);
2326 struct soc_camera_device *icd = client->dev.platform_data;
2327 int i, error_cnt=0, error_idx=-1;
2330 for (i=0; i<ext_ctrl->count; i++) {
2331 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2338 error_idx = ext_ctrl->count;
2340 if (error_idx != -1) {
2341 ext_ctrl->error_idx = error_idx;
2348 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2349 * this wasn't our capture interface, so, we wait for the right one */
2350 static int sensor_video_probe(struct soc_camera_device *icd,
2351 struct i2c_client *client)
2355 struct sensor *sensor = to_sensor(client);
2357 /* We must have a parent by now. And it cannot be a wrong one.
2358 * So this entire test is completely redundant. */
2359 if (!icd->dev.parent ||
2360 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2363 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2365 goto sensor_video_probe_err;
2368 ret = sensor_write(client, 0x12, 0x80);
2371 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2374 mdelay(50); //delay 5 microseconds
2376 /* check if it is an sensor sensor */
2377 ret = sensor_read(client, 0x0a, &pid);
2379 SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2381 goto sensor_video_probe_err;
2384 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2385 if (pid == SENSOR_ID) {
2386 sensor->model = SENSOR_V4L2_IDENT;
2388 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2390 goto sensor_video_probe_err;
2395 sensor_video_probe_err:
2400 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2402 struct i2c_client *client = v4l2_get_subdevdata(sd);
2403 struct soc_camera_device *icd = client->dev.platform_data;
2404 struct sensor *sensor = to_sensor(client);
2406 #if CONFIG_SENSOR_Flash
2410 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2413 case RK29_CAM_SUBDEV_DEACTIVATE:
2415 sensor_deactivate(client);
2419 case RK29_CAM_SUBDEV_IOREQUEST:
2421 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2422 if (sensor->sensor_io_request != NULL) {
2423 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2424 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2425 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2426 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2427 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2428 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2431 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2433 goto sensor_ioctl_end;
2435 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2437 #if CONFIG_SENSOR_Flash
2438 if (sensor->sensor_gpio_res) {
2439 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2440 for (i = 0; i < icd->ops->num_controls; i++) {
2441 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2442 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2443 sensor_controls[i].id=0xffff;
2446 sensor->info_priv.flash = 0xff;
2447 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2448 }else{ //two cameras are the same,need to deal diffrently ,zyc
2449 for (i = 0; i < icd->ops->num_controls; i++) {
2450 if(0xffff == icd->ops->controls[i].id){
2451 sensor_controls[i].id=V4L2_CID_FLASH;
2461 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2469 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2470 enum v4l2_mbus_pixelcode *code)
2472 if (index >= ARRAY_SIZE(sensor_colour_fmts))
2475 *code = sensor_colour_fmts[index].code;
2478 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2479 .init = sensor_init,
2480 .g_ctrl = sensor_g_control,
2481 .s_ctrl = sensor_s_control,
2482 .g_ext_ctrls = sensor_g_ext_controls,
2483 .s_ext_ctrls = sensor_s_ext_controls,
2484 .g_chip_ident = sensor_g_chip_ident,
2485 .ioctl = sensor_ioctl,
2488 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2489 .s_mbus_fmt = sensor_s_fmt,
2490 .g_mbus_fmt = sensor_g_fmt,
2491 .try_mbus_fmt = sensor_try_fmt,
2492 .enum_mbus_fmt = sensor_enum_fmt,
2494 static struct v4l2_subdev_ops sensor_subdev_ops = {
2495 .core = &sensor_subdev_core_ops,
2496 .video = &sensor_subdev_video_ops,
2499 static int sensor_probe(struct i2c_client *client,
2500 const struct i2c_device_id *did)
2502 struct sensor *sensor;
2503 struct soc_camera_device *icd = client->dev.platform_data;
2504 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2505 struct soc_camera_link *icl;
2508 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2510 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2514 icl = to_soc_camera_link(icd);
2516 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2520 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2521 dev_warn(&adapter->dev,
2522 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2526 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2530 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2532 /* Second stage probe - when a capture adapter is there */
2533 icd->ops = &sensor_ops;
2535 sensor->info_priv.fmt = sensor_colour_fmts[0];
2537 #if CONFIG_SENSOR_I2C_NOSCHED
2538 atomic_set(&sensor->tasklock_cnt,0);
2541 ret = sensor_video_probe(icd, client);
2544 i2c_set_clientdata(client, NULL);
2548 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2552 static int sensor_remove(struct i2c_client *client)
2554 struct sensor *sensor = to_sensor(client);
2555 struct soc_camera_device *icd = client->dev.platform_data;
2558 i2c_set_clientdata(client, NULL);
2559 client->driver = NULL;
2565 static const struct i2c_device_id sensor_id[] = {
2566 {SENSOR_NAME_STRING(), 0 },
2569 MODULE_DEVICE_TABLE(i2c, sensor_id);
2571 static struct i2c_driver sensor_i2c_driver = {
2573 .name = SENSOR_NAME_STRING(),
2575 .probe = sensor_probe,
2576 .remove = sensor_remove,
2577 .id_table = sensor_id,
2580 static int __init sensor_mod_init(void)
2582 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2583 return i2c_add_driver(&sensor_i2c_driver);
2586 static void __exit sensor_mod_exit(void)
2588 i2c_del_driver(&sensor_i2c_driver);
2591 device_initcall_sync(sensor_mod_init);
2592 module_exit(sensor_mod_exit);
2594 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2595 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2596 MODULE_LICENSE("GPL");