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 #ifdef 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 flash_off_timer.icd = icd;
1303 flash_off_timer.timer.function = flash_off_func;
1306 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);
1310 sensor_task_lock(client,0);
1311 sensor_deactivate(client);
1315 static int sensor_deactivate(struct i2c_client *client)
1317 struct soc_camera_device *icd = client->dev.platform_data;
1320 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1322 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1323 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1324 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1325 icd->user_width = SENSOR_INIT_WIDTH;
1326 icd->user_height = SENSOR_INIT_HEIGHT;
1330 static struct reginfo sensor_power_down_sequence[]=
1334 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1337 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1339 if (pm_msg.event == PM_EVENT_SUSPEND) {
1340 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1341 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1343 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1346 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1348 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1353 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1359 static int sensor_resume(struct soc_camera_device *icd)
1363 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1365 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1369 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1375 static int sensor_set_bus_param(struct soc_camera_device *icd,
1376 unsigned long flags)
1382 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1384 struct soc_camera_link *icl = to_soc_camera_link(icd);
1385 unsigned long flags = SENSOR_BUS_PARAM;
1387 return soc_camera_apply_sensor_flags(icl, flags);
1390 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1392 struct i2c_client *client = v4l2_get_subdevdata(sd);
1393 struct soc_camera_device *icd = client->dev.platform_data;
1394 struct sensor *sensor = to_sensor(client);
1396 mf->width = icd->user_width;
1397 mf->height = icd->user_height;
1398 mf->code = sensor->info_priv.fmt.code;
1399 mf->colorspace = sensor->info_priv.fmt.colorspace;
1400 mf->field = V4L2_FIELD_NONE;
1404 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1408 if ((mf->width == 1024) && (mf->height == 768)) {
1410 } else if ((mf->width == 1280) && (mf->height == 1024)) {
1412 } else if ((mf->width == 1600) && (mf->height == 1200)) {
1414 } else if ((mf->width == 2048) && (mf->height == 1536)) {
1416 } else if ((mf->width == 2592) && (mf->height == 1944)) {
1421 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1425 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1429 if ((mf->width == 1280) && (mf->height == 720)) {
1431 } else if ((mf->width == 1920) && (mf->height == 1080)) {
1436 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1439 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1441 struct i2c_client *client = v4l2_get_subdevdata(sd);
1442 const struct sensor_datafmt *fmt;
1443 struct sensor *sensor = to_sensor(client);
1444 const struct v4l2_queryctrl *qctrl;
1445 struct soc_camera_device *icd = client->dev.platform_data;
1446 struct reginfo *winseqe_set_addr=NULL;
1448 int ret=0, set_w,set_h;
1450 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1451 ARRAY_SIZE(sensor_colour_fmts));
1454 goto sensor_s_fmt_end;
1457 if (sensor->info_priv.fmt.code != mf->code) {
1460 case V4L2_MBUS_FMT_YUYV8_2X8:
1462 //winseqe_set_addr = sensor_ClrFmt_YUYV;
1463 sensor_read(client, 0x3a, &readval);
1464 sensor_write(client,0x3a, readval&0xf7);
1465 sensor_read(client,0x3d,&readval);
1466 sensor_write(client,0x3d,readval&0xfe);
1469 case V4L2_MBUS_FMT_UYVY8_2X8:
1471 //winseqe_set_addr = sensor_ClrFmt_UYVY;
1472 sensor_read(client, 0x3a, &readval);
1473 sensor_write(client,0x3a, readval|0x08);
1474 sensor_read(client,0x3d,&readval);
1475 sensor_write(client,0x3d,readval&0xfe);
1481 if (winseqe_set_addr != NULL) {
1482 sensor_write_array(client, winseqe_set_addr);
1483 sensor->info_priv.fmt.code = mf->code;
1484 sensor->info_priv.fmt.colorspace= mf->colorspace;
1485 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1487 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1494 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1496 winseqe_set_addr = sensor_qcif;
1500 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1502 winseqe_set_addr = sensor_qvga;
1506 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1508 winseqe_set_addr = sensor_cif;
1512 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1514 winseqe_set_addr = sensor_vga;
1518 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1520 winseqe_set_addr = sensor_svga;
1524 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
1526 winseqe_set_addr = sensor_720p;
1530 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
1532 winseqe_set_addr = sensor_xga;
1536 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1538 winseqe_set_addr = sensor_vga; //sensor_sxga;
1542 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1544 winseqe_set_addr = sensor_uxga;
1550 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1551 set_w = SENSOR_INIT_WIDTH;
1552 set_h = SENSOR_INIT_HEIGHT;
1553 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1556 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1557 #if CONFIG_SENSOR_Flash
1558 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
1559 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1560 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1561 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1563 } else { /* ddl@rock-chips.com : Video */
1564 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1565 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1566 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1570 ret |= sensor_write_array(client, winseqe_set_addr);
1572 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1573 #if CONFIG_SENSOR_Flash
1574 if (sensor_fmt_capturechk(sd,mf) == true) {
1575 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1576 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1577 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1581 goto sensor_s_fmt_end;
1584 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1586 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
1587 #if CONFIG_SENSOR_Effect
1588 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1589 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1591 #if CONFIG_SENSOR_WhiteBalance
1592 if (sensor->info_priv.whiteBalance != 0) {
1593 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1594 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1597 sensor->info_priv.snap2preview = true;
1598 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
1599 #if CONFIG_SENSOR_Effect
1600 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1601 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1603 #if CONFIG_SENSOR_WhiteBalance
1604 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1605 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1607 sensor->info_priv.video2preview = true;
1608 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1609 #if CONFIG_SENSOR_Effect
1610 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1611 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1613 #if CONFIG_SENSOR_WhiteBalance
1614 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1615 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1617 sensor->info_priv.video2preview = false;
1618 sensor->info_priv.snap2preview = false;
1621 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1625 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1635 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1637 struct i2c_client *client = v4l2_get_subdevdata(sd);
1638 struct sensor *sensor = to_sensor(client);
1639 const struct sensor_datafmt *fmt;
1642 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1643 ARRAY_SIZE(sensor_colour_fmts));
1645 fmt = &sensor->info_priv.fmt;
1646 mf->code = fmt->code;
1649 if (mf->height > SENSOR_MAX_HEIGHT)
1650 mf->height = SENSOR_MAX_HEIGHT;
1651 else if (mf->height < SENSOR_MIN_HEIGHT)
1652 mf->height = SENSOR_MIN_HEIGHT;
1654 if (mf->width > SENSOR_MAX_WIDTH)
1655 mf->width = SENSOR_MAX_WIDTH;
1656 else if (mf->width < SENSOR_MIN_WIDTH)
1657 mf->width = SENSOR_MIN_WIDTH;
1659 mf->colorspace = fmt->colorspace;
1664 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1666 struct i2c_client *client = v4l2_get_subdevdata(sd);
1668 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1671 if (id->match.addr != client->addr)
1674 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
1679 #if CONFIG_SENSOR_Brightness
1680 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1682 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1684 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1686 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1688 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1690 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1693 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1697 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1701 #if CONFIG_SENSOR_Effect
1702 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1704 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1706 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1708 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1710 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1712 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1715 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1719 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1723 #if CONFIG_SENSOR_Exposure
1724 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1726 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1728 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1730 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1732 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1734 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1737 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1741 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1745 #if CONFIG_SENSOR_Saturation
1746 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1748 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1750 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1752 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1754 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1756 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1759 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1763 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1767 #if CONFIG_SENSOR_Contrast
1768 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1770 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1772 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1774 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1776 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1778 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1781 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1785 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1789 #if CONFIG_SENSOR_Mirror
1790 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1792 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1794 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1796 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1798 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1800 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1803 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1807 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1811 #if CONFIG_SENSOR_Flip
1812 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1814 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1816 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1818 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1820 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1822 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1825 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1829 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1833 #if CONFIG_SENSOR_Scene
1834 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1836 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1838 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1840 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1842 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1844 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1847 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1851 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1855 #if CONFIG_SENSOR_WhiteBalance
1856 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1858 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1860 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1862 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
1864 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
1866 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1869 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1873 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1877 #if CONFIG_SENSOR_DigitalZoom
1878 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
1880 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1881 struct sensor *sensor = to_sensor(client);
1882 const struct v4l2_queryctrl *qctrl_info;
1883 int digitalzoom_cur, digitalzoom_total;
1885 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1889 digitalzoom_cur = sensor->info_priv.digitalzoom;
1890 digitalzoom_total = qctrl_info->maximum;
1892 if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
1894 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1898 if ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
1900 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1904 if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
1906 value = digitalzoom_total - digitalzoom_cur;
1909 if ((value < 0) && ((digitalzoom_cur + value) < 0))
1911 value = 0 - digitalzoom_cur;
1914 digitalzoom_cur += value;
1916 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
1918 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
1920 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1923 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1930 #if CONFIG_SENSOR_Flash
1931 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1933 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
1934 if (value == 3) { /* ddl@rock-chips.com: torch */
1935 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
1937 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1939 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1943 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1948 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1950 struct i2c_client *client = v4l2_get_subdevdata(sd);
1951 struct sensor *sensor = to_sensor(client);
1952 const struct v4l2_queryctrl *qctrl;
1954 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
1958 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
1964 case V4L2_CID_BRIGHTNESS:
1966 ctrl->value = sensor->info_priv.brightness;
1969 case V4L2_CID_SATURATION:
1971 ctrl->value = sensor->info_priv.saturation;
1974 case V4L2_CID_CONTRAST:
1976 ctrl->value = sensor->info_priv.contrast;
1979 case V4L2_CID_DO_WHITE_BALANCE:
1981 ctrl->value = sensor->info_priv.whiteBalance;
1984 case V4L2_CID_EXPOSURE:
1986 ctrl->value = sensor->info_priv.exposure;
1989 case V4L2_CID_HFLIP:
1991 ctrl->value = sensor->info_priv.mirror;
1994 case V4L2_CID_VFLIP:
1996 ctrl->value = sensor->info_priv.flip;
2007 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2009 struct i2c_client *client = v4l2_get_subdevdata(sd);
2010 struct sensor *sensor = to_sensor(client);
2011 struct soc_camera_device *icd = client->dev.platform_data;
2012 const struct v4l2_queryctrl *qctrl;
2015 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2019 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2025 #if CONFIG_SENSOR_Brightness
2026 case V4L2_CID_BRIGHTNESS:
2028 if (ctrl->value != sensor->info_priv.brightness)
2030 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2034 sensor->info_priv.brightness = ctrl->value;
2039 #if CONFIG_SENSOR_Exposure
2040 case V4L2_CID_EXPOSURE:
2042 if (ctrl->value != sensor->info_priv.exposure)
2044 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2048 sensor->info_priv.exposure = ctrl->value;
2053 #if CONFIG_SENSOR_Saturation
2054 case V4L2_CID_SATURATION:
2056 if (ctrl->value != sensor->info_priv.saturation)
2058 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2062 sensor->info_priv.saturation = ctrl->value;
2067 #if CONFIG_SENSOR_Contrast
2068 case V4L2_CID_CONTRAST:
2070 if (ctrl->value != sensor->info_priv.contrast)
2072 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2076 sensor->info_priv.contrast = ctrl->value;
2081 #if CONFIG_SENSOR_WhiteBalance
2082 case V4L2_CID_DO_WHITE_BALANCE:
2084 if (ctrl->value != sensor->info_priv.whiteBalance)
2086 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2090 sensor->info_priv.whiteBalance = ctrl->value;
2095 #if CONFIG_SENSOR_Mirror
2096 case V4L2_CID_HFLIP:
2098 if (ctrl->value != sensor->info_priv.mirror)
2100 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2102 sensor->info_priv.mirror = ctrl->value;
2107 #if CONFIG_SENSOR_Flip
2108 case V4L2_CID_VFLIP:
2110 if (ctrl->value != sensor->info_priv.flip)
2112 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2114 sensor->info_priv.flip = ctrl->value;
2125 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2127 const struct v4l2_queryctrl *qctrl;
2128 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2129 struct sensor *sensor = to_sensor(client);
2131 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2135 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2139 switch (ext_ctrl->id)
2141 case V4L2_CID_SCENE:
2143 ext_ctrl->value = sensor->info_priv.scene;
2146 case V4L2_CID_EFFECT:
2148 ext_ctrl->value = sensor->info_priv.effect;
2151 case V4L2_CID_ZOOM_ABSOLUTE:
2153 ext_ctrl->value = sensor->info_priv.digitalzoom;
2156 case V4L2_CID_ZOOM_RELATIVE:
2160 case V4L2_CID_FOCUS_ABSOLUTE:
2162 ext_ctrl->value = sensor->info_priv.focus;
2165 case V4L2_CID_FOCUS_RELATIVE:
2169 case V4L2_CID_FLASH:
2171 ext_ctrl->value = sensor->info_priv.flash;
2179 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2181 const struct v4l2_queryctrl *qctrl;
2182 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2183 struct sensor *sensor = to_sensor(client);
2186 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2190 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2195 switch (ext_ctrl->id)
2197 #if CONFIG_SENSOR_Scene
2198 case V4L2_CID_SCENE:
2200 if (ext_ctrl->value != sensor->info_priv.scene)
2202 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2204 sensor->info_priv.scene = ext_ctrl->value;
2209 #if CONFIG_SENSOR_Effect
2210 case V4L2_CID_EFFECT:
2212 if (ext_ctrl->value != sensor->info_priv.effect)
2214 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2216 sensor->info_priv.effect= ext_ctrl->value;
2221 #if CONFIG_SENSOR_DigitalZoom
2222 case V4L2_CID_ZOOM_ABSOLUTE:
2224 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2227 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2229 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2231 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2233 sensor->info_priv.digitalzoom += val_offset;
2235 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2240 case V4L2_CID_ZOOM_RELATIVE:
2242 if (ext_ctrl->value)
2244 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2246 sensor->info_priv.digitalzoom += ext_ctrl->value;
2248 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2253 #if CONFIG_SENSOR_Focus
2254 case V4L2_CID_FOCUS_ABSOLUTE:
2256 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2259 if (ext_ctrl->value != sensor->info_priv.focus)
2261 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2263 sensor->info_priv.focus += val_offset;
2268 case V4L2_CID_FOCUS_RELATIVE:
2270 if (ext_ctrl->value)
2272 sensor->info_priv.focus += ext_ctrl->value;
2274 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2279 #if CONFIG_SENSOR_Flash
2280 case V4L2_CID_FLASH:
2282 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2284 sensor->info_priv.flash = ext_ctrl->value;
2286 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2297 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2299 struct i2c_client *client = v4l2_get_subdevdata(sd);
2300 struct soc_camera_device *icd = client->dev.platform_data;
2301 int i, error_cnt=0, error_idx=-1;
2304 for (i=0; i<ext_ctrl->count; i++) {
2305 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2312 error_idx = ext_ctrl->count;
2314 if (error_idx != -1) {
2315 ext_ctrl->error_idx = error_idx;
2322 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2324 struct i2c_client *client = v4l2_get_subdevdata(sd);
2325 struct soc_camera_device *icd = client->dev.platform_data;
2326 int i, error_cnt=0, error_idx=-1;
2329 for (i=0; i<ext_ctrl->count; i++) {
2330 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2337 error_idx = ext_ctrl->count;
2339 if (error_idx != -1) {
2340 ext_ctrl->error_idx = error_idx;
2347 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2348 * this wasn't our capture interface, so, we wait for the right one */
2349 static int sensor_video_probe(struct soc_camera_device *icd,
2350 struct i2c_client *client)
2354 struct sensor *sensor = to_sensor(client);
2356 /* We must have a parent by now. And it cannot be a wrong one.
2357 * So this entire test is completely redundant. */
2358 if (!icd->dev.parent ||
2359 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2362 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2364 goto sensor_video_probe_err;
2367 ret = sensor_write(client, 0x12, 0x80);
2370 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2373 mdelay(50); //delay 5 microseconds
2375 /* check if it is an sensor sensor */
2376 ret = sensor_read(client, 0x0a, &pid);
2378 SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2380 goto sensor_video_probe_err;
2383 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2384 if (pid == SENSOR_ID) {
2385 sensor->model = SENSOR_V4L2_IDENT;
2387 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2389 goto sensor_video_probe_err;
2394 sensor_video_probe_err:
2399 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2401 struct i2c_client *client = v4l2_get_subdevdata(sd);
2402 struct soc_camera_device *icd = client->dev.platform_data;
2403 struct sensor *sensor = to_sensor(client);
2405 #if CONFIG_SENSOR_Flash
2409 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2412 case RK29_CAM_SUBDEV_DEACTIVATE:
2414 sensor_deactivate(client);
2418 case RK29_CAM_SUBDEV_IOREQUEST:
2420 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2421 if (sensor->sensor_io_request != NULL) {
2422 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2423 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2424 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2425 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2426 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2427 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2430 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2432 goto sensor_ioctl_end;
2434 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2436 #if CONFIG_SENSOR_Flash
2437 if (sensor->sensor_gpio_res) {
2438 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2439 for (i = 0; i < icd->ops->num_controls; i++) {
2440 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2441 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2442 sensor_controls[i].id=0xffff;
2445 sensor->info_priv.flash = 0xff;
2446 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2447 }else{ //two cameras are the same,need to deal diffrently ,zyc
2448 for (i = 0; i < icd->ops->num_controls; i++) {
2449 if(0xffff == icd->ops->controls[i].id){
2450 sensor_controls[i].id=V4L2_CID_FLASH;
2460 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2468 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2469 enum v4l2_mbus_pixelcode *code)
2471 if (index >= ARRAY_SIZE(sensor_colour_fmts))
2474 *code = sensor_colour_fmts[index].code;
2477 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2478 .init = sensor_init,
2479 .g_ctrl = sensor_g_control,
2480 .s_ctrl = sensor_s_control,
2481 .g_ext_ctrls = sensor_g_ext_controls,
2482 .s_ext_ctrls = sensor_s_ext_controls,
2483 .g_chip_ident = sensor_g_chip_ident,
2484 .ioctl = sensor_ioctl,
2487 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2488 .s_mbus_fmt = sensor_s_fmt,
2489 .g_mbus_fmt = sensor_g_fmt,
2490 .try_mbus_fmt = sensor_try_fmt,
2491 .enum_mbus_fmt = sensor_enum_fmt,
2493 static struct v4l2_subdev_ops sensor_subdev_ops = {
2494 .core = &sensor_subdev_core_ops,
2495 .video = &sensor_subdev_video_ops,
2498 static int sensor_probe(struct i2c_client *client,
2499 const struct i2c_device_id *did)
2501 struct sensor *sensor;
2502 struct soc_camera_device *icd = client->dev.platform_data;
2503 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2504 struct soc_camera_link *icl;
2507 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2509 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2513 icl = to_soc_camera_link(icd);
2515 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2519 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2520 dev_warn(&adapter->dev,
2521 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2525 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2529 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2531 /* Second stage probe - when a capture adapter is there */
2532 icd->ops = &sensor_ops;
2534 sensor->info_priv.fmt = sensor_colour_fmts[0];
2536 #if CONFIG_SENSOR_I2C_NOSCHED
2537 atomic_set(&sensor->tasklock_cnt,0);
2540 ret = sensor_video_probe(icd, client);
2543 i2c_set_clientdata(client, NULL);
2547 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
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");