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)
97 /* init 640X480 VGA */
98 static struct reginfo sensor_init_data[] =
152 {0xb9, 0x00}, //0x07//0x27
225 static struct reginfo sensor_720p[]=
232 static struct reginfo sensor_uxga[] =
238 static struct reginfo sensor_sxga[] =
243 static struct reginfo sensor_xga[] =
248 static struct reginfo sensor_svga[] =
254 static struct reginfo sensor_vga[] =
274 static struct reginfo sensor_cif[] =
280 static struct reginfo sensor_qvga[] =
300 static struct reginfo sensor_qcif[] =
320 static struct reginfo ov2655_qqvga[] =
327 static struct reginfo ov2655_Sharpness_auto[] =
332 static struct reginfo ov2655_Sharpness1[] =
338 static struct reginfo ov2655_Sharpness2[][3] =
345 static struct reginfo ov2655_Sharpness3[] =
351 static struct reginfo ov2655_Sharpness4[]=
358 static struct reginfo ov2655_Sharpness5[] =
366 static struct reginfo sensor_ClrFmt_YUYV[]=
371 static struct reginfo sensor_ClrFmt_UYVY[]=
377 #if CONFIG_SENSOR_WhiteBalance
378 static struct reginfo sensor_WhiteB_Auto[]=
383 /* Cloudy Colour Temperature : 6500K - 8000K */
384 static struct reginfo sensor_WhiteB_Cloudy[]=
392 /* ClearDay Colour Temperature : 5000K - 6500K */
393 static struct reginfo sensor_WhiteB_ClearDay[]=
401 /* Office Colour Temperature : 3500K - 5000K */
402 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
411 /* Home Colour Temperature : 2500K - 3500K */
412 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
420 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
421 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
425 #if CONFIG_SENSOR_Brightness
426 static struct reginfo sensor_Brightness0[]=
432 static struct reginfo sensor_Brightness1[]=
439 static struct reginfo sensor_Brightness2[]=
446 static struct reginfo sensor_Brightness3[]=
453 static struct reginfo sensor_Brightness4[]=
460 static struct reginfo sensor_Brightness5[]=
466 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
467 sensor_Brightness4, sensor_Brightness5,NULL,
472 #if CONFIG_SENSOR_Effect
473 static struct reginfo sensor_Effect_Normal[] =
479 static struct reginfo sensor_Effect_WandB[] =
487 static struct reginfo sensor_Effect_Sepia[] =
495 static struct reginfo sensor_Effect_Negative[] =
501 static struct reginfo sensor_Effect_Bluish[] =
510 static struct reginfo sensor_Effect_Green[] =
518 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
519 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
522 #if CONFIG_SENSOR_Exposure
523 static struct reginfo sensor_Exposure0[]=
528 static struct reginfo sensor_Exposure1[]=
533 static struct reginfo sensor_Exposure2[]=
538 static struct reginfo sensor_Exposure3[]=
543 static struct reginfo sensor_Exposure4[]=
548 static struct reginfo sensor_Exposure5[]=
553 static struct reginfo sensor_Exposure6[]=
558 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
559 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
562 #if CONFIG_SENSOR_Saturation
563 static struct reginfo sensor_Saturation0[]=
568 static struct reginfo sensor_Saturation1[]=
573 static struct reginfo sensor_Saturation2[]=
577 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
580 #if CONFIG_SENSOR_Contrast
581 static struct reginfo sensor_Contrast0[]=
586 static struct reginfo sensor_Contrast1[]=
591 static struct reginfo sensor_Contrast2[]=
596 static struct reginfo sensor_Contrast3[]=
601 static struct reginfo sensor_Contrast4[]=
607 static struct reginfo sensor_Contrast5[]=
612 static struct reginfo sensor_Contrast6[]=
616 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
617 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
621 #if CONFIG_SENSOR_Mirror
622 static struct reginfo sensor_MirrorOn[]=
627 static struct reginfo sensor_MirrorOff[]=
631 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
633 #if CONFIG_SENSOR_Flip
634 static struct reginfo sensor_FlipOn[]=
639 static struct reginfo sensor_FlipOff[]=
643 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
646 #if CONFIG_SENSOR_Scene
647 static struct reginfo sensor_SceneAuto[] =
652 static struct reginfo sensor_SceneNight[] =
656 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
659 #if CONFIG_SENSOR_DigitalZoom
660 static struct reginfo sensor_Zoom0[] =
665 static struct reginfo sensor_Zoom1[] =
670 static struct reginfo sensor_Zoom2[] =
676 static struct reginfo sensor_Zoom3[] =
680 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
682 static const struct v4l2_querymenu sensor_menus[] =
684 #if CONFIG_SENSOR_WhiteBalance
685 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
686 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
687 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
690 #if CONFIG_SENSOR_Effect
691 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
692 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
693 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
696 #if CONFIG_SENSOR_Scene
697 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
700 #if CONFIG_SENSOR_Flash
701 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
702 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
706 static const struct v4l2_queryctrl sensor_controls[] =
708 #if CONFIG_SENSOR_WhiteBalance
710 .id = V4L2_CID_DO_WHITE_BALANCE,
711 .type = V4L2_CTRL_TYPE_MENU,
712 .name = "White Balance Control",
720 #if CONFIG_SENSOR_Brightness
722 .id = V4L2_CID_BRIGHTNESS,
723 .type = V4L2_CTRL_TYPE_INTEGER,
724 .name = "Brightness Control",
732 #if CONFIG_SENSOR_Effect
734 .id = V4L2_CID_EFFECT,
735 .type = V4L2_CTRL_TYPE_MENU,
736 .name = "Effect Control",
744 #if CONFIG_SENSOR_Exposure
746 .id = V4L2_CID_EXPOSURE,
747 .type = V4L2_CTRL_TYPE_INTEGER,
748 .name = "Exposure Control",
756 #if CONFIG_SENSOR_Saturation
758 .id = V4L2_CID_SATURATION,
759 .type = V4L2_CTRL_TYPE_INTEGER,
760 .name = "Saturation Control",
768 #if CONFIG_SENSOR_Contrast
770 .id = V4L2_CID_CONTRAST,
771 .type = V4L2_CTRL_TYPE_INTEGER,
772 .name = "Contrast Control",
780 #if CONFIG_SENSOR_Mirror
782 .id = V4L2_CID_HFLIP,
783 .type = V4L2_CTRL_TYPE_BOOLEAN,
784 .name = "Mirror Control",
792 #if CONFIG_SENSOR_Flip
794 .id = V4L2_CID_VFLIP,
795 .type = V4L2_CTRL_TYPE_BOOLEAN,
796 .name = "Flip Control",
804 #if CONFIG_SENSOR_Scene
806 .id = V4L2_CID_SCENE,
807 .type = V4L2_CTRL_TYPE_MENU,
808 .name = "Scene Control",
816 #if CONFIG_SENSOR_DigitalZoom
818 .id = V4L2_CID_ZOOM_RELATIVE,
819 .type = V4L2_CTRL_TYPE_INTEGER,
820 .name = "DigitalZoom Control",
826 .id = V4L2_CID_ZOOM_ABSOLUTE,
827 .type = V4L2_CTRL_TYPE_INTEGER,
828 .name = "DigitalZoom Control",
836 #if CONFIG_SENSOR_Focus
838 .id = V4L2_CID_FOCUS_RELATIVE,
839 .type = V4L2_CTRL_TYPE_INTEGER,
840 .name = "Focus Control",
846 .id = V4L2_CID_FOCUS_ABSOLUTE,
847 .type = V4L2_CTRL_TYPE_INTEGER,
848 .name = "Focus Control",
852 .default_value = 125,
856 #if CONFIG_SENSOR_Flash
858 .id = V4L2_CID_FLASH,
859 .type = V4L2_CTRL_TYPE_MENU,
860 .name = "Flash Control",
869 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
870 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
871 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
872 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
873 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
874 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
875 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
876 static int sensor_resume(struct soc_camera_device *icd);
877 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
878 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
879 #if CONFIG_SENSOR_Effect
880 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
882 #if CONFIG_SENSOR_WhiteBalance
883 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
885 static int sensor_deactivate(struct i2c_client *client);
887 static struct soc_camera_ops sensor_ops =
889 .suspend = sensor_suspend,
890 .resume = sensor_resume,
891 .set_bus_param = sensor_set_bus_param,
892 .query_bus_param = sensor_query_bus_param,
893 .controls = sensor_controls,
894 .menus = sensor_menus,
895 .num_controls = ARRAY_SIZE(sensor_controls),
896 .num_menus = ARRAY_SIZE(sensor_menus),
899 /* only one fixed colorspace per pixelcode */
900 struct sensor_datafmt {
901 enum v4l2_mbus_pixelcode code;
902 enum v4l2_colorspace colorspace;
905 /* Find a data format by a pixel code in an array */
906 static const struct sensor_datafmt *sensor_find_datafmt(
907 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
911 for (i = 0; i < n; i++)
912 if (fmt[i].code == code)
918 static const struct sensor_datafmt sensor_colour_fmts[] = {
919 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
920 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
923 typedef struct sensor_info_priv_s
937 unsigned char mirror; /* HFLIP */
938 unsigned char flip; /* VFLIP */
939 unsigned int winseqe_cur_addr;
940 struct sensor_datafmt fmt;
941 unsigned int funmodule_state;
942 } sensor_info_priv_t;
946 struct v4l2_subdev subdev;
947 struct i2c_client *client;
948 sensor_info_priv_t info_priv;
949 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
950 #if CONFIG_SENSOR_I2C_NOSCHED
951 atomic_t tasklock_cnt;
953 struct rk29camera_platform_data *sensor_io_request;
954 struct rk29camera_gpio_res *sensor_gpio_res;
958 static struct sensor* to_sensor(const struct i2c_client *client)
960 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
963 static int sensor_task_lock(struct i2c_client *client, int lock)
965 #if CONFIG_SENSOR_I2C_NOSCHED
967 struct sensor *sensor = to_sensor(client);
970 if (atomic_read(&sensor->tasklock_cnt) == 0) {
971 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
972 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
976 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
977 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
978 goto sensor_task_lock_err;
983 atomic_add(1, &sensor->tasklock_cnt);
985 if (atomic_read(&sensor->tasklock_cnt) > 0) {
986 atomic_sub(1, &sensor->tasklock_cnt);
988 if (atomic_read(&sensor->tasklock_cnt) == 0)
993 sensor_task_lock_err:
1001 /* sensor register write */
1002 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1006 struct i2c_msg msg[1];
1008 buf[0] = reg & 0xFF;
1011 msg->addr = client->addr;
1012 msg->flags = client->flags;
1014 msg->len = sizeof(buf);
1015 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1016 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1021 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1022 err = i2c_transfer(client->adapter, msg, 1);
1027 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1035 /* sensor register read */
1036 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1040 struct i2c_msg msg[2];
1044 msg[0].addr = client->addr;
1045 msg[0].flags = client->flags;
1047 msg[0].len = sizeof(buf);
1048 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1049 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1051 msg[1].addr = client->addr;
1052 msg[1].flags = client->flags|I2C_M_RD;
1055 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1056 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1060 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1061 err = i2c_transfer(client->adapter, msg, 2);
1067 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1075 /* write a array of registers */
1076 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1080 #if CONFIG_SENSOR_I2C_RDWRCHK
1085 if (sensor_task_lock(client, 1) < 0)
1086 goto sensor_write_array_end;
1088 while (regarray[i].reg != 0)
1090 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1094 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1098 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1100 goto sensor_write_array_end;
1103 #if CONFIG_SENSOR_I2C_RDWRCHK
1104 sensor_read(client, regarray[i].reg, &valchk);
1105 if (valchk != regarray[i].val)
1106 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1112 sensor_write_array_end:
1113 sensor_task_lock(client,0);
1116 #if CONFIG_SENSOR_I2C_RDWRCHK
1117 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1125 while (regarray[i].reg != 0)
1127 sensor_read(client, regarray[i].reg, &valchk);
1128 if (valchk != regarray[i].val)
1129 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1136 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1138 struct soc_camera_link *icl = to_soc_camera_link(icd);
1141 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1144 case Sensor_PowerDown:
1146 if (icl->powerdown) {
1149 ret = icl->powerdown(icd->pdev, on);
1150 if (ret == RK29_CAM_IO_SUCCESS) {
1154 icl->reset(icd->pdev);
1156 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1158 goto sensor_power_end;
1165 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1166 struct sensor *sensor = to_sensor(client);
1168 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1169 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1175 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1182 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1184 struct i2c_client *client = v4l2_get_subdevdata(sd);
1185 struct soc_camera_device *icd = client->dev.platform_data;
1186 struct sensor *sensor = to_sensor(client);
1187 const struct v4l2_queryctrl *qctrl;
1188 const struct sensor_datafmt *fmt;
1191 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1193 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1195 goto sensor_INIT_ERR;
1199 if (sensor_task_lock(client,1)<0)
1200 goto sensor_INIT_ERR;
1201 ret = sensor_write(client, 0x12, 0x80);
1204 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1206 goto sensor_INIT_ERR;
1208 mdelay(5); //delay 5 microseconds
1210 ret = sensor_write_array(client, sensor_init_data);
1213 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1214 goto sensor_INIT_ERR;
1216 sensor_task_lock(client,0);
1218 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1219 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1221 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1223 goto sensor_INIT_ERR;
1225 sensor->info_priv.fmt = *fmt;
1227 /* sensor sensor information for initialization */
1228 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1230 sensor->info_priv.whiteBalance = qctrl->default_value;
1231 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1233 sensor->info_priv.brightness = qctrl->default_value;
1234 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1236 sensor->info_priv.effect = qctrl->default_value;
1237 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1239 sensor->info_priv.exposure = qctrl->default_value;
1241 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1243 sensor->info_priv.saturation = qctrl->default_value;
1244 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1246 sensor->info_priv.contrast = qctrl->default_value;
1247 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1249 sensor->info_priv.mirror = qctrl->default_value;
1250 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1252 sensor->info_priv.flip = qctrl->default_value;
1253 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1255 sensor->info_priv.scene = qctrl->default_value;
1256 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1258 sensor->info_priv.digitalzoom = qctrl->default_value;
1260 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1261 #if CONFIG_SENSOR_Focus
1263 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1265 sensor->info_priv.focus = qctrl->default_value;
1268 #if CONFIG_SENSOR_Flash
1269 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1271 sensor->info_priv.flash = qctrl->default_value;
1274 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);
1278 sensor_task_lock(client,0);
1279 sensor_deactivate(client);
1283 static int sensor_deactivate(struct i2c_client *client)
1285 struct soc_camera_device *icd = client->dev.platform_data;
1288 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1290 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1291 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1293 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1294 icd->user_width = SENSOR_INIT_WIDTH;
1295 icd->user_height = SENSOR_INIT_HEIGHT;
1299 static struct reginfo sensor_power_down_sequence[]=
1303 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1306 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1308 if (pm_msg.event == PM_EVENT_SUSPEND) {
1309 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1310 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1312 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1315 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1317 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1322 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1328 static int sensor_resume(struct soc_camera_device *icd)
1332 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1334 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1338 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1344 static int sensor_set_bus_param(struct soc_camera_device *icd,
1345 unsigned long flags)
1351 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1353 struct soc_camera_link *icl = to_soc_camera_link(icd);
1354 unsigned long flags = SENSOR_BUS_PARAM;
1356 return soc_camera_apply_sensor_flags(icl, flags);
1359 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1361 struct i2c_client *client = v4l2_get_subdevdata(sd);
1362 struct soc_camera_device *icd = client->dev.platform_data;
1363 struct sensor *sensor = to_sensor(client);
1365 mf->width = icd->user_width;
1366 mf->height = icd->user_height;
1367 mf->code = sensor->info_priv.fmt.code;
1368 mf->colorspace = sensor->info_priv.fmt.colorspace;
1369 mf->field = V4L2_FIELD_NONE;
1373 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1377 if ((mf->width == 1024) && (mf->height == 768)) {
1379 } else if ((mf->width == 1280) && (mf->height == 1024)) {
1381 } else if ((mf->width == 1600) && (mf->height == 1200)) {
1383 } else if ((mf->width == 2048) && (mf->height == 1536)) {
1385 } else if ((mf->width == 2592) && (mf->height == 1944)) {
1390 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1394 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1398 if ((mf->width == 1280) && (mf->height == 720)) {
1400 } else if ((mf->width == 1920) && (mf->height == 1080)) {
1405 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1408 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1410 struct i2c_client *client = v4l2_get_subdevdata(sd);
1411 const struct sensor_datafmt *fmt;
1412 struct sensor *sensor = to_sensor(client);
1413 const struct v4l2_queryctrl *qctrl;
1414 struct soc_camera_device *icd = client->dev.platform_data;
1415 struct reginfo *winseqe_set_addr=NULL;
1417 int ret=0, set_w,set_h;
1419 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1420 ARRAY_SIZE(sensor_colour_fmts));
1423 goto sensor_s_fmt_end;
1426 if (sensor->info_priv.fmt.code != mf->code) {
1429 case V4L2_MBUS_FMT_YUYV8_2X8:
1431 //winseqe_set_addr = sensor_ClrFmt_YUYV;
1432 sensor_read(client, 0x3a, &readval);
1433 sensor_write(client,0x3a, readval&0xf7);
1434 sensor_read(client,0x3d,&readval);
1435 sensor_write(client,0x3d,readval&0xfe);
1438 case V4L2_MBUS_FMT_UYVY8_2X8:
1440 //winseqe_set_addr = sensor_ClrFmt_UYVY;
1441 sensor_read(client, 0x3a, &readval);
1442 sensor_write(client,0x3a, readval|0x08);
1443 sensor_read(client,0x3d,&readval);
1444 sensor_write(client,0x3d,readval&0xfe);
1450 if (winseqe_set_addr != NULL) {
1451 sensor_write_array(client, winseqe_set_addr);
1452 sensor->info_priv.fmt.code = mf->code;
1453 sensor->info_priv.fmt.colorspace= mf->colorspace;
1454 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1456 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1463 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1465 winseqe_set_addr = sensor_qcif;
1469 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1471 winseqe_set_addr = sensor_qvga;
1475 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1477 winseqe_set_addr = sensor_cif;
1481 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1483 winseqe_set_addr = sensor_vga;
1487 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1489 winseqe_set_addr = sensor_svga;
1493 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
1495 winseqe_set_addr = sensor_720p;
1499 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
1501 winseqe_set_addr = sensor_xga;
1505 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1507 winseqe_set_addr = sensor_vga; //sensor_sxga;
1511 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1513 winseqe_set_addr = sensor_uxga;
1519 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1520 set_w = SENSOR_INIT_WIDTH;
1521 set_h = SENSOR_INIT_HEIGHT;
1522 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1525 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1526 #if CONFIG_SENSOR_Flash
1527 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
1528 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1529 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1530 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1532 } else { /* ddl@rock-chips.com : Video */
1533 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1534 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1535 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1539 ret |= sensor_write_array(client, winseqe_set_addr);
1541 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1542 #if CONFIG_SENSOR_Flash
1543 if (sensor_fmt_capturechk(sd,mf) == true) {
1544 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1545 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1546 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1550 goto sensor_s_fmt_end;
1553 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1555 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
1556 #if CONFIG_SENSOR_Effect
1557 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1558 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1560 #if CONFIG_SENSOR_WhiteBalance
1561 if (sensor->info_priv.whiteBalance != 0) {
1562 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1563 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1566 sensor->info_priv.snap2preview = true;
1567 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
1568 #if CONFIG_SENSOR_Effect
1569 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1570 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1572 #if CONFIG_SENSOR_WhiteBalance
1573 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1574 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1576 sensor->info_priv.video2preview = true;
1577 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1578 #if CONFIG_SENSOR_Effect
1579 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1580 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1582 #if CONFIG_SENSOR_WhiteBalance
1583 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1584 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1586 sensor->info_priv.video2preview = false;
1587 sensor->info_priv.snap2preview = false;
1590 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1594 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1604 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1606 struct i2c_client *client = v4l2_get_subdevdata(sd);
1607 struct sensor *sensor = to_sensor(client);
1608 const struct sensor_datafmt *fmt;
1611 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1612 ARRAY_SIZE(sensor_colour_fmts));
1614 fmt = &sensor->info_priv.fmt;
1615 mf->code = fmt->code;
1618 if (mf->height > SENSOR_MAX_HEIGHT)
1619 mf->height = SENSOR_MAX_HEIGHT;
1620 else if (mf->height < SENSOR_MIN_HEIGHT)
1621 mf->height = SENSOR_MIN_HEIGHT;
1623 if (mf->width > SENSOR_MAX_WIDTH)
1624 mf->width = SENSOR_MAX_WIDTH;
1625 else if (mf->width < SENSOR_MIN_WIDTH)
1626 mf->width = SENSOR_MIN_WIDTH;
1628 mf->colorspace = fmt->colorspace;
1633 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1635 struct i2c_client *client = v4l2_get_subdevdata(sd);
1637 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1640 if (id->match.addr != client->addr)
1643 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
1648 #if CONFIG_SENSOR_Brightness
1649 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1651 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1653 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1655 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1657 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1659 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1662 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1666 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1670 #if CONFIG_SENSOR_Effect
1671 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1673 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1675 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1677 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1679 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1681 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1684 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1688 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1692 #if CONFIG_SENSOR_Exposure
1693 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1695 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1697 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1699 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1701 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1703 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1706 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1710 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1714 #if CONFIG_SENSOR_Saturation
1715 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1717 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1719 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1721 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1723 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1725 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1728 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1732 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1736 #if CONFIG_SENSOR_Contrast
1737 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1739 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1741 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1743 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1745 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1747 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1750 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1754 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1758 #if CONFIG_SENSOR_Mirror
1759 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1761 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1763 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1765 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1767 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1769 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1772 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1776 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1780 #if CONFIG_SENSOR_Flip
1781 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1783 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1785 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1787 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1789 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1791 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1794 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1798 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1802 #if CONFIG_SENSOR_Scene
1803 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1805 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1807 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1809 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1811 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1813 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1816 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1820 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1824 #if CONFIG_SENSOR_WhiteBalance
1825 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1827 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1829 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1831 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
1833 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
1835 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1838 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1842 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1846 #if CONFIG_SENSOR_DigitalZoom
1847 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
1849 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1850 struct sensor *sensor = to_sensor(client);
1851 const struct v4l2_queryctrl *qctrl_info;
1852 int digitalzoom_cur, digitalzoom_total;
1854 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1858 digitalzoom_cur = sensor->info_priv.digitalzoom;
1859 digitalzoom_total = qctrl_info->maximum;
1861 if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
1863 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1867 if ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
1869 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1873 if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
1875 value = digitalzoom_total - digitalzoom_cur;
1878 if ((value < 0) && ((digitalzoom_cur + value) < 0))
1880 value = 0 - digitalzoom_cur;
1883 digitalzoom_cur += value;
1885 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
1887 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
1889 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1892 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1899 #if CONFIG_SENSOR_Flash
1900 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1902 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
1903 if (value == 3) { /* ddl@rock-chips.com: torch */
1904 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
1906 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1908 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1912 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1917 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1919 struct i2c_client *client = v4l2_get_subdevdata(sd);
1920 struct sensor *sensor = to_sensor(client);
1921 const struct v4l2_queryctrl *qctrl;
1923 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
1927 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
1933 case V4L2_CID_BRIGHTNESS:
1935 ctrl->value = sensor->info_priv.brightness;
1938 case V4L2_CID_SATURATION:
1940 ctrl->value = sensor->info_priv.saturation;
1943 case V4L2_CID_CONTRAST:
1945 ctrl->value = sensor->info_priv.contrast;
1948 case V4L2_CID_DO_WHITE_BALANCE:
1950 ctrl->value = sensor->info_priv.whiteBalance;
1953 case V4L2_CID_EXPOSURE:
1955 ctrl->value = sensor->info_priv.exposure;
1958 case V4L2_CID_HFLIP:
1960 ctrl->value = sensor->info_priv.mirror;
1963 case V4L2_CID_VFLIP:
1965 ctrl->value = sensor->info_priv.flip;
1976 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1978 struct i2c_client *client = v4l2_get_subdevdata(sd);
1979 struct sensor *sensor = to_sensor(client);
1980 struct soc_camera_device *icd = client->dev.platform_data;
1981 const struct v4l2_queryctrl *qctrl;
1984 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
1988 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
1994 #if CONFIG_SENSOR_Brightness
1995 case V4L2_CID_BRIGHTNESS:
1997 if (ctrl->value != sensor->info_priv.brightness)
1999 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2003 sensor->info_priv.brightness = ctrl->value;
2008 #if CONFIG_SENSOR_Exposure
2009 case V4L2_CID_EXPOSURE:
2011 if (ctrl->value != sensor->info_priv.exposure)
2013 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2017 sensor->info_priv.exposure = ctrl->value;
2022 #if CONFIG_SENSOR_Saturation
2023 case V4L2_CID_SATURATION:
2025 if (ctrl->value != sensor->info_priv.saturation)
2027 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2031 sensor->info_priv.saturation = ctrl->value;
2036 #if CONFIG_SENSOR_Contrast
2037 case V4L2_CID_CONTRAST:
2039 if (ctrl->value != sensor->info_priv.contrast)
2041 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2045 sensor->info_priv.contrast = ctrl->value;
2050 #if CONFIG_SENSOR_WhiteBalance
2051 case V4L2_CID_DO_WHITE_BALANCE:
2053 if (ctrl->value != sensor->info_priv.whiteBalance)
2055 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2059 sensor->info_priv.whiteBalance = ctrl->value;
2064 #if CONFIG_SENSOR_Mirror
2065 case V4L2_CID_HFLIP:
2067 if (ctrl->value != sensor->info_priv.mirror)
2069 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2071 sensor->info_priv.mirror = ctrl->value;
2076 #if CONFIG_SENSOR_Flip
2077 case V4L2_CID_VFLIP:
2079 if (ctrl->value != sensor->info_priv.flip)
2081 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2083 sensor->info_priv.flip = ctrl->value;
2094 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2096 const struct v4l2_queryctrl *qctrl;
2097 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2098 struct sensor *sensor = to_sensor(client);
2100 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2104 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2108 switch (ext_ctrl->id)
2110 case V4L2_CID_SCENE:
2112 ext_ctrl->value = sensor->info_priv.scene;
2115 case V4L2_CID_EFFECT:
2117 ext_ctrl->value = sensor->info_priv.effect;
2120 case V4L2_CID_ZOOM_ABSOLUTE:
2122 ext_ctrl->value = sensor->info_priv.digitalzoom;
2125 case V4L2_CID_ZOOM_RELATIVE:
2129 case V4L2_CID_FOCUS_ABSOLUTE:
2131 ext_ctrl->value = sensor->info_priv.focus;
2134 case V4L2_CID_FOCUS_RELATIVE:
2138 case V4L2_CID_FLASH:
2140 ext_ctrl->value = sensor->info_priv.flash;
2148 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2150 const struct v4l2_queryctrl *qctrl;
2151 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2152 struct sensor *sensor = to_sensor(client);
2155 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2159 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2164 switch (ext_ctrl->id)
2166 #if CONFIG_SENSOR_Scene
2167 case V4L2_CID_SCENE:
2169 if (ext_ctrl->value != sensor->info_priv.scene)
2171 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2173 sensor->info_priv.scene = ext_ctrl->value;
2178 #if CONFIG_SENSOR_Effect
2179 case V4L2_CID_EFFECT:
2181 if (ext_ctrl->value != sensor->info_priv.effect)
2183 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2185 sensor->info_priv.effect= ext_ctrl->value;
2190 #if CONFIG_SENSOR_DigitalZoom
2191 case V4L2_CID_ZOOM_ABSOLUTE:
2193 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2196 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2198 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2200 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2202 sensor->info_priv.digitalzoom += val_offset;
2204 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2209 case V4L2_CID_ZOOM_RELATIVE:
2211 if (ext_ctrl->value)
2213 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2215 sensor->info_priv.digitalzoom += ext_ctrl->value;
2217 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2222 #if CONFIG_SENSOR_Focus
2223 case V4L2_CID_FOCUS_ABSOLUTE:
2225 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2228 if (ext_ctrl->value != sensor->info_priv.focus)
2230 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2232 sensor->info_priv.focus += val_offset;
2237 case V4L2_CID_FOCUS_RELATIVE:
2239 if (ext_ctrl->value)
2241 sensor->info_priv.focus += ext_ctrl->value;
2243 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2248 #if CONFIG_SENSOR_Flash
2249 case V4L2_CID_FLASH:
2251 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2253 sensor->info_priv.flash = ext_ctrl->value;
2255 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2266 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2268 struct i2c_client *client = v4l2_get_subdevdata(sd);
2269 struct soc_camera_device *icd = client->dev.platform_data;
2270 int i, error_cnt=0, error_idx=-1;
2273 for (i=0; i<ext_ctrl->count; i++) {
2274 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2281 error_idx = ext_ctrl->count;
2283 if (error_idx != -1) {
2284 ext_ctrl->error_idx = error_idx;
2291 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2293 struct i2c_client *client = v4l2_get_subdevdata(sd);
2294 struct soc_camera_device *icd = client->dev.platform_data;
2295 int i, error_cnt=0, error_idx=-1;
2298 for (i=0; i<ext_ctrl->count; i++) {
2299 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2306 error_idx = ext_ctrl->count;
2308 if (error_idx != -1) {
2309 ext_ctrl->error_idx = error_idx;
2316 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2317 * this wasn't our capture interface, so, we wait for the right one */
2318 static int sensor_video_probe(struct soc_camera_device *icd,
2319 struct i2c_client *client)
2323 struct sensor *sensor = to_sensor(client);
2325 /* We must have a parent by now. And it cannot be a wrong one.
2326 * So this entire test is completely redundant. */
2327 if (!icd->dev.parent ||
2328 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2331 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2333 goto sensor_video_probe_err;
2336 ret = sensor_write(client, 0x12, 0x80);
2339 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2342 mdelay(50); //delay 5 microseconds
2344 /* check if it is an sensor sensor */
2345 ret = sensor_read(client, 0x0a, &pid);
2347 SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2349 goto sensor_video_probe_err;
2352 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2353 if (pid == SENSOR_ID) {
2354 sensor->model = SENSOR_V4L2_IDENT;
2356 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2358 goto sensor_video_probe_err;
2363 sensor_video_probe_err:
2368 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2370 struct i2c_client *client = v4l2_get_subdevdata(sd);
2371 struct soc_camera_device *icd = client->dev.platform_data;
2372 struct sensor *sensor = to_sensor(client);
2374 #if CONFIG_SENSOR_Flash
2378 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2381 case RK29_CAM_SUBDEV_DEACTIVATE:
2383 sensor_deactivate(client);
2387 case RK29_CAM_SUBDEV_IOREQUEST:
2389 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2390 if (sensor->sensor_io_request != NULL) {
2391 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2392 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2393 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2394 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2395 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2396 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2399 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2401 goto sensor_ioctl_end;
2403 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2405 #if CONFIG_SENSOR_Flash
2406 if (sensor->sensor_gpio_res) {
2407 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2408 for (i = 0; i < icd->ops->num_controls; i++) {
2409 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2410 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2413 sensor->info_priv.flash = 0xff;
2414 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2422 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2430 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2431 enum v4l2_mbus_pixelcode *code)
2433 if (index >= ARRAY_SIZE(sensor_colour_fmts))
2436 *code = sensor_colour_fmts[index].code;
2439 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2440 .init = sensor_init,
2441 .g_ctrl = sensor_g_control,
2442 .s_ctrl = sensor_s_control,
2443 .g_ext_ctrls = sensor_g_ext_controls,
2444 .s_ext_ctrls = sensor_s_ext_controls,
2445 .g_chip_ident = sensor_g_chip_ident,
2446 .ioctl = sensor_ioctl,
2449 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2450 .s_mbus_fmt = sensor_s_fmt,
2451 .g_mbus_fmt = sensor_g_fmt,
2452 .try_mbus_fmt = sensor_try_fmt,
2453 .enum_mbus_fmt = sensor_enum_fmt,
2455 static struct v4l2_subdev_ops sensor_subdev_ops = {
2456 .core = &sensor_subdev_core_ops,
2457 .video = &sensor_subdev_video_ops,
2460 static int sensor_probe(struct i2c_client *client,
2461 const struct i2c_device_id *did)
2463 struct sensor *sensor;
2464 struct soc_camera_device *icd = client->dev.platform_data;
2465 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2466 struct soc_camera_link *icl;
2469 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2471 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2475 icl = to_soc_camera_link(icd);
2477 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2481 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2482 dev_warn(&adapter->dev,
2483 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2487 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2491 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2493 /* Second stage probe - when a capture adapter is there */
2494 icd->ops = &sensor_ops;
2496 sensor->info_priv.fmt = sensor_colour_fmts[0];
2498 #if CONFIG_SENSOR_I2C_NOSCHED
2499 atomic_set(&sensor->tasklock_cnt,0);
2502 ret = sensor_video_probe(icd, client);
2505 i2c_set_clientdata(client, NULL);
2509 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2513 static int sensor_remove(struct i2c_client *client)
2515 struct sensor *sensor = to_sensor(client);
2516 struct soc_camera_device *icd = client->dev.platform_data;
2519 i2c_set_clientdata(client, NULL);
2520 client->driver = NULL;
2526 static const struct i2c_device_id sensor_id[] = {
2527 {SENSOR_NAME_STRING(), 0 },
2530 MODULE_DEVICE_TABLE(i2c, sensor_id);
2532 static struct i2c_driver sensor_i2c_driver = {
2534 .name = SENSOR_NAME_STRING(),
2536 .probe = sensor_probe,
2537 .remove = sensor_remove,
2538 .id_table = sensor_id,
2541 static int __init sensor_mod_init(void)
2543 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2544 return i2c_add_driver(&sensor_i2c_driver);
2547 static void __exit sensor_mod_exit(void)
2549 i2c_del_driver(&sensor_i2c_driver);
2552 device_initcall_sync(sensor_mod_init);
2553 module_exit(sensor_mod_exit);
2555 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2556 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2557 MODULE_LICENSE("GPL");