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 <mach/rk29_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(0, 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_GC0309
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_GC0309
48 #define SENSOR_ID 0xa0
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_PIX_FMT_UYVY
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 1
64 #define CONFIG_SENSOR_DigitalZoom 0
65 #define CONFIG_SENSOR_Focus 0
66 #define CONFIG_SENSOR_Exposure 0
67 #define CONFIG_SENSOR_Flash 0
68 #define CONFIG_SENSOR_Mirror 0
69 #define CONFIG_SENSOR_Flip 0
71 #define CONFIG_SENSOR_I2C_SPEED 100000 /* 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_HIGH |\
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 /* init SVGA preview */
100 static struct reginfo sensor_init_data[] =
102 /*init registers code.*/
104 {0xfe,0x80}, // soft reset
106 // GC0309_SET_PAGE0; // set page0
109 {0xd2,0x10}, // close AEC
110 {0x22,0x55}, // close AWB
178 /////////////////////////////////////////////////////////////////////
179 /////////////////////////// grab_t ////////////////////////////////
180 /////////////////////////////////////////////////////////////////////
200 /////////////////////////////////////////////////////////////////////
201 /////////////////////////// LSC_t ////////////////////////////////
202 /////////////////////////////////////////////////////////////////////
214 /////////////////////////////////////////////////////////////////////
215 /////////////////////////// DNDD_t ///////////////////////////////
216 /////////////////////////////////////////////////////////////////////
225 /////////////////////////////////////////////////////////////////////
226 /////////////////////////// asde_t ///////////////////////////////
227 /////////////////////////////////////////////////////////////////////
237 /////////////////////////////////////////////////////////////////////
238 /////////////////////////// eeintp_t///////////////////////////////
243 static struct reginfo sensor_vga[] =
245 {0x45 , 0x0f}, //output enable
250 static struct reginfo sensor_cif[] =
254 static struct reginfo sensor_qvga[] =
258 static struct reginfo sensor_qcif[] =
262 static struct reginfo sensor_ClrFmt_YUYV[]=
268 static struct reginfo sensor_ClrFmt_UYVY[]=
274 #if CONFIG_SENSOR_WhiteBalance
275 static struct reginfo sensor_WhiteB_Auto[]=
280 /* Cloudy Colour Temperature : 6500K - 8000K */
281 static struct reginfo sensor_WhiteB_Cloudy[]=
286 /* ClearDay Colour Temperature : 5000K - 6500K */
287 static struct reginfo sensor_WhiteB_ClearDay[]=
293 /* Office Colour Temperature : 3500K - 5000K */
294 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
301 /* Home Colour Temperature : 2500K - 3500K */
302 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
308 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
309 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
313 #if CONFIG_SENSOR_Brightness
314 static struct reginfo sensor_Brightness0[]=
321 static struct reginfo sensor_Brightness1[]=
328 static struct reginfo sensor_Brightness2[]=
335 static struct reginfo sensor_Brightness3[]=
342 static struct reginfo sensor_Brightness4[]=
349 static struct reginfo sensor_Brightness5[]=
355 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
356 sensor_Brightness4, sensor_Brightness5,NULL,
361 #if CONFIG_SENSOR_Effect
362 static struct reginfo sensor_Effect_Normal[] =
368 static struct reginfo sensor_Effect_WandB[] =
374 static struct reginfo sensor_Effect_Sepia[] =
380 static struct reginfo sensor_Effect_Negative[] =
386 static struct reginfo sensor_Effect_Bluish[] =
393 static struct reginfo sensor_Effect_Green[] =
399 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
400 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
403 #if CONFIG_SENSOR_Exposure
404 static struct reginfo sensor_Exposure0[]=
410 static struct reginfo sensor_Exposure1[]=
417 static struct reginfo sensor_Exposure2[]=
424 static struct reginfo sensor_Exposure3[]=
431 static struct reginfo sensor_Exposure4[]=
438 static struct reginfo sensor_Exposure5[]=
445 static struct reginfo sensor_Exposure6[]=
452 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
453 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
456 #if CONFIG_SENSOR_Saturation
457 static struct reginfo sensor_Saturation0[]=
463 static struct reginfo sensor_Saturation1[]=
469 static struct reginfo sensor_Saturation2[]=
474 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
477 #if CONFIG_SENSOR_Contrast
478 static struct reginfo sensor_Contrast0[]=
485 static struct reginfo sensor_Contrast1[]=
492 static struct reginfo sensor_Contrast2[]=
499 static struct reginfo sensor_Contrast3[]=
506 static struct reginfo sensor_Contrast4[]=
514 static struct reginfo sensor_Contrast5[]=
521 static struct reginfo sensor_Contrast6[]=
527 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
528 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
532 #if CONFIG_SENSOR_Mirror
533 static struct reginfo sensor_MirrorOn[]=
539 static struct reginfo sensor_MirrorOff[]=
544 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
546 #if CONFIG_SENSOR_Flip
547 static struct reginfo sensor_FlipOn[]=
553 static struct reginfo sensor_FlipOff[]=
558 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
561 #if CONFIG_SENSOR_Scene
562 static struct reginfo sensor_SceneAuto[] =
564 #if 0 /* ddl@rock-chips.com : */
576 static struct reginfo sensor_SceneNight[] =
579 //30fps ~ 5fps night mode for 60/50Hz light environment, 24Mhz clock input,36Mzh pclk
583 //15fps ~ 5fps night mode for 60/50Hz light environment, 24Mhz clock input,18Mhz pclk
599 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
602 #if CONFIG_SENSOR_DigitalZoom
603 static struct reginfo sensor_Zoom0[] =
608 static struct reginfo sensor_Zoom1[] =
613 static struct reginfo sensor_Zoom2[] =
619 static struct reginfo sensor_Zoom3[] =
623 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
625 static const struct v4l2_querymenu sensor_menus[] =
627 #if CONFIG_SENSOR_WhiteBalance
628 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
629 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
630 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
633 #if CONFIG_SENSOR_Effect
634 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
635 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
636 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
639 #if CONFIG_SENSOR_Scene
640 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
643 #if CONFIG_SENSOR_Flash
644 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
645 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
649 static const struct v4l2_queryctrl sensor_controls[] =
651 #if CONFIG_SENSOR_WhiteBalance
653 .id = V4L2_CID_DO_WHITE_BALANCE,
654 .type = V4L2_CTRL_TYPE_MENU,
655 .name = "White Balance Control",
663 #if CONFIG_SENSOR_Brightness
665 .id = V4L2_CID_BRIGHTNESS,
666 .type = V4L2_CTRL_TYPE_INTEGER,
667 .name = "Brightness Control",
675 #if CONFIG_SENSOR_Effect
677 .id = V4L2_CID_EFFECT,
678 .type = V4L2_CTRL_TYPE_MENU,
679 .name = "Effect Control",
687 #if CONFIG_SENSOR_Exposure
689 .id = V4L2_CID_EXPOSURE,
690 .type = V4L2_CTRL_TYPE_INTEGER,
691 .name = "Exposure Control",
699 #if CONFIG_SENSOR_Saturation
701 .id = V4L2_CID_SATURATION,
702 .type = V4L2_CTRL_TYPE_INTEGER,
703 .name = "Saturation Control",
711 #if CONFIG_SENSOR_Contrast
713 .id = V4L2_CID_CONTRAST,
714 .type = V4L2_CTRL_TYPE_INTEGER,
715 .name = "Contrast Control",
723 #if CONFIG_SENSOR_Mirror
725 .id = V4L2_CID_HFLIP,
726 .type = V4L2_CTRL_TYPE_BOOLEAN,
727 .name = "Mirror Control",
735 #if CONFIG_SENSOR_Flip
737 .id = V4L2_CID_VFLIP,
738 .type = V4L2_CTRL_TYPE_BOOLEAN,
739 .name = "Flip Control",
747 #if CONFIG_SENSOR_Scene
749 .id = V4L2_CID_SCENE,
750 .type = V4L2_CTRL_TYPE_MENU,
751 .name = "Scene Control",
759 #if CONFIG_SENSOR_DigitalZoom
761 .id = V4L2_CID_ZOOM_RELATIVE,
762 .type = V4L2_CTRL_TYPE_INTEGER,
763 .name = "DigitalZoom Control",
769 .id = V4L2_CID_ZOOM_ABSOLUTE,
770 .type = V4L2_CTRL_TYPE_INTEGER,
771 .name = "DigitalZoom Control",
779 #if CONFIG_SENSOR_Focus
781 .id = V4L2_CID_FOCUS_RELATIVE,
782 .type = V4L2_CTRL_TYPE_INTEGER,
783 .name = "Focus Control",
789 .id = V4L2_CID_FOCUS_ABSOLUTE,
790 .type = V4L2_CTRL_TYPE_INTEGER,
791 .name = "Focus Control",
795 .default_value = 125,
799 #if CONFIG_SENSOR_Flash
801 .id = V4L2_CID_FLASH,
802 .type = V4L2_CTRL_TYPE_MENU,
803 .name = "Flash Control",
812 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
813 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
814 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
815 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
816 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
817 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
818 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
819 static int sensor_resume(struct soc_camera_device *icd);
820 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
821 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
822 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
823 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
824 static int sensor_deactivate(struct i2c_client *client);
826 static struct soc_camera_ops sensor_ops =
828 .suspend = sensor_suspend,
829 .resume = sensor_resume,
830 .set_bus_param = sensor_set_bus_param,
831 .query_bus_param = sensor_query_bus_param,
832 .controls = sensor_controls,
833 .menus = sensor_menus,
834 .num_controls = ARRAY_SIZE(sensor_controls),
835 .num_menus = ARRAY_SIZE(sensor_menus),
838 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
839 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
840 .colorspace = _colorspace }
842 #define JPG_FMT(_name, _depth, _fourcc) \
843 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
845 static const struct soc_camera_data_format sensor_colour_formats[] = {
846 JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
847 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
850 typedef struct sensor_info_priv_s
864 unsigned char mirror; /* HFLIP */
865 unsigned char flip; /* VFLIP */
866 unsigned int winseqe_cur_addr;
869 } sensor_info_priv_t;
873 struct v4l2_subdev subdev;
874 struct i2c_client *client;
875 sensor_info_priv_t info_priv;
876 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
877 #if CONFIG_SENSOR_I2C_NOSCHED
878 atomic_t tasklock_cnt;
880 struct rk29camera_platform_data *sensor_io_request;
881 struct rk29camera_gpio_res *sensor_gpio_res;
884 static struct sensor* to_sensor(const struct i2c_client *client)
886 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
889 static int sensor_task_lock(struct i2c_client *client, int lock)
891 #if CONFIG_SENSOR_I2C_NOSCHED
893 struct sensor *sensor = to_sensor(client);
896 if (atomic_read(&sensor->tasklock_cnt) == 0) {
897 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
898 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
902 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
903 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
904 goto sensor_task_lock_err;
909 atomic_add(1, &sensor->tasklock_cnt);
911 if (atomic_read(&sensor->tasklock_cnt) > 0) {
912 atomic_sub(1, &sensor->tasklock_cnt);
914 if (atomic_read(&sensor->tasklock_cnt) == 0)
920 sensor_task_lock_err:
925 /* sensor register */
926 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
930 ret = i2c_master_reg8_recv(client, reg, val, 1, CONFIG_SENSOR_I2C_SPEED);
932 return (ret > 0)? 0 : ret;
935 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
939 ret = i2c_master_reg8_send(client, reg, &val, 1, CONFIG_SENSOR_I2C_SPEED);
941 return (ret > 0)? 0 : ret;
944 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
948 struct i2c_msg msg[1];
956 msg->addr = client->addr;
957 msg->flags = client->flags;
959 msg->len = sizeof(buf);
960 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
961 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
966 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
967 err = i2c_transfer(client->adapter, msg, 1);
972 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
980 /* sensor register read */
981 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
985 struct i2c_msg msg[2];
989 msg[0].addr = client->addr;
990 msg[0].flags = client->flags;
992 msg[0].len = sizeof(buf);
993 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
994 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
996 msg[1].addr = client->addr;
997 msg[1].flags = client->flags|I2C_M_RD;
1000 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1001 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1005 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1006 err = i2c_transfer(client->adapter, msg, 2);
1012 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1022 /* write a array of registers */
1023 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1031 if (sensor_task_lock(client, 1) < 0)
1032 goto sensor_write_array_end;
1033 while (regarray[i].reg != 0)
1035 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1039 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1043 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1045 goto sensor_write_array_end;
1048 #if CONFIG_SENSOR_I2C_RDWRCHK
1050 sensor_read(client, regarray[i].reg, &valchk);
1051 if (valchk != regarray[i].val)
1052 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1058 sensor_write_array_end:
1059 sensor_task_lock(client,0);
1062 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1070 while (regarray[i].reg != 0)
1072 sensor_read(client, regarray[i].reg, &valchk);
1073 if (valchk != regarray[i].val)
1074 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1080 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1082 struct soc_camera_link *icl = to_soc_camera_link(icd);
1085 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1088 case Sensor_PowerDown:
1090 if (icl->powerdown) {
1091 ret = icl->powerdown(icd->pdev, on);
1092 if (ret == RK29_CAM_IO_SUCCESS) {
1096 icl->reset(icd->pdev);
1098 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1100 goto sensor_power_end;
1107 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1108 struct sensor *sensor = to_sensor(client);
1110 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1111 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1117 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1124 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1126 struct i2c_client *client = sd->priv;
1127 struct soc_camera_device *icd = client->dev.platform_data;
1128 struct sensor *sensor = to_sensor(client);
1129 const struct v4l2_queryctrl *qctrl;
1133 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1135 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1137 goto sensor_INIT_ERR;
1141 if (sensor_task_lock(client,1)<0)
1142 goto sensor_INIT_ERR;
1143 ret = sensor_write(client, 0xfe, 0x80);
1146 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1148 goto sensor_INIT_ERR;
1151 mdelay(5); //delay 5 microseconds
1152 /* check if it is an sensor sensor */
1153 ret = sensor_read(client, 0x00, &value);
1155 SENSOR_TR("read chip id high byte failed\n");
1157 goto sensor_INIT_ERR;
1160 if (value == SENSOR_ID) {
1161 sensor->model = SENSOR_V4L2_IDENT;
1163 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), value);
1165 goto sensor_INIT_ERR;
1168 ret = sensor_write_array(client, sensor_init_data);
1171 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1172 goto sensor_INIT_ERR;
1174 sensor_task_lock(client,0);
1175 //icd->user_width = SENSOR_INIT_WIDTH;
1176 //icd->user_height = SENSOR_INIT_HEIGHT;
1177 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1178 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
1180 /* sensor sensor information for initialization */
1181 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1183 sensor->info_priv.whiteBalance = qctrl->default_value;
1184 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1186 sensor->info_priv.brightness = qctrl->default_value;
1187 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1189 sensor->info_priv.effect = qctrl->default_value;
1190 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1192 sensor->info_priv.exposure = qctrl->default_value;
1194 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1196 sensor->info_priv.saturation = qctrl->default_value;
1197 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1199 sensor->info_priv.contrast = qctrl->default_value;
1200 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1202 sensor->info_priv.mirror = qctrl->default_value;
1203 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1205 sensor->info_priv.flip = qctrl->default_value;
1206 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1208 sensor->info_priv.scene = qctrl->default_value;
1209 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1211 sensor->info_priv.digitalzoom = qctrl->default_value;
1213 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1214 #if CONFIG_SENSOR_Focus
1216 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1218 sensor->info_priv.focus = qctrl->default_value;
1221 #if CONFIG_SENSOR_Flash
1222 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1224 sensor->info_priv.flash = qctrl->default_value;
1227 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);
1231 sensor_task_lock(client,0);
1232 sensor_deactivate(client);
1236 static int sensor_deactivate(struct i2c_client *client)
1238 struct soc_camera_device *icd = client->dev.platform_data;
1240 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1242 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1243 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1245 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1246 icd->user_width = SENSOR_INIT_WIDTH;
1247 icd->user_height = SENSOR_INIT_HEIGHT;
1252 static struct reginfo sensor_power_down_sequence[]=
1256 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1259 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1261 if (pm_msg.event == PM_EVENT_SUSPEND) {
1262 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1263 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1265 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1268 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1270 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1275 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1281 static int sensor_resume(struct soc_camera_device *icd)
1285 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1287 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1291 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1297 static int sensor_set_bus_param(struct soc_camera_device *icd,
1298 unsigned long flags)
1304 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1306 struct soc_camera_link *icl = to_soc_camera_link(icd);
1307 unsigned long flags = SENSOR_BUS_PARAM;
1309 return soc_camera_apply_sensor_flags(icl, flags);
1312 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1314 struct i2c_client *client = sd->priv;
1315 struct soc_camera_device *icd = client->dev.platform_data;
1316 struct sensor *sensor = to_sensor(client);
1317 struct v4l2_pix_format *pix = &f->fmt.pix;
1319 pix->width = icd->user_width;
1320 pix->height = icd->user_height;
1321 pix->pixelformat = sensor->info_priv.pixfmt;
1322 pix->field = V4L2_FIELD_NONE;
1323 pix->colorspace = V4L2_COLORSPACE_JPEG;
1327 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
1331 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
1333 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
1335 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
1337 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
1339 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
1344 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1348 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
1352 if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
1354 } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
1359 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1362 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1364 struct i2c_client *client = sd->priv;
1365 struct sensor *sensor = to_sensor(client);
1366 struct v4l2_pix_format *pix = &f->fmt.pix;
1367 const struct v4l2_queryctrl *qctrl;
1368 struct soc_camera_device *icd = client->dev.platform_data;
1369 struct reginfo *winseqe_set_addr=NULL;
1370 int ret=0, set_w,set_h;
1372 SENSOR_TR("sensor_s_fmt\n");
1373 if (sensor->info_priv.pixfmt != pix->pixelformat) {
1374 switch (pix->pixelformat)
1376 case V4L2_PIX_FMT_YUYV:
1378 winseqe_set_addr = sensor_ClrFmt_YUYV;
1381 case V4L2_PIX_FMT_UYVY:
1383 winseqe_set_addr = sensor_ClrFmt_UYVY;
1389 if (winseqe_set_addr != NULL) {
1390 sensor_write_array(client, winseqe_set_addr);
1391 sensor->info_priv.pixfmt = pix->pixelformat;
1393 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1395 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1400 set_h = pix->height;
1402 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1404 winseqe_set_addr = sensor_qcif;
1408 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1410 winseqe_set_addr = sensor_qvga;
1414 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1416 winseqe_set_addr = sensor_cif;
1420 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1422 winseqe_set_addr = sensor_vga;
1429 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1430 set_w = SENSOR_INIT_WIDTH;
1431 set_h = SENSOR_INIT_HEIGHT;
1433 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
1436 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1437 #if CONFIG_SENSOR_Flash
1438 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
1439 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1440 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1441 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1443 } else { /* ddl@rock-chips.com : Video */
1444 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1445 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1446 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1450 ret |= sensor_write_array(client, winseqe_set_addr);
1452 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1453 #if CONFIG_SENSOR_Flash
1454 if (sensor_fmt_capturechk(sd,f) == true) {
1455 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1456 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1457 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1461 goto sensor_s_fmt_end;
1464 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1466 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
1467 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1468 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1469 if (sensor->info_priv.whiteBalance != 0) {
1470 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1471 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1473 sensor->info_priv.snap2preview = true;
1474 } else if (sensor_fmt_videochk(sd,f) == true) { /* ddl@rock-chips.com : Video */
1475 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1476 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1477 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1478 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1479 sensor->info_priv.video2preview = true;
1480 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1481 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1482 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1483 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1484 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1485 sensor->info_priv.video2preview = false;
1486 sensor->info_priv.snap2preview = false;
1488 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1492 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1496 pix->height = set_h;
1502 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1504 struct v4l2_pix_format *pix = &f->fmt.pix;
1505 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
1506 pix->pixelformat == V4L2_PIX_FMT_YUYV;
1509 * With Bayer format enforce even side lengths, but let the user play
1510 * with the starting pixel
1513 if (pix->height > SENSOR_MAX_HEIGHT)
1514 pix->height = SENSOR_MAX_HEIGHT;
1515 else if (pix->height < SENSOR_MIN_HEIGHT)
1516 pix->height = SENSOR_MIN_HEIGHT;
1518 pix->height = ALIGN(pix->height, 2);
1520 if (pix->width > SENSOR_MAX_WIDTH)
1521 pix->width = SENSOR_MAX_WIDTH;
1522 else if (pix->width < SENSOR_MIN_WIDTH)
1523 pix->width = SENSOR_MIN_WIDTH;
1525 pix->width = ALIGN(pix->width, 2);
1530 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1532 struct i2c_client *client = sd->priv;
1534 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1537 if (id->match.addr != client->addr)
1540 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
1545 #if CONFIG_SENSOR_Brightness
1546 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1548 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1550 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1552 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1554 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1556 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1559 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1563 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1567 #if CONFIG_SENSOR_Effect
1568 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1570 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1572 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1574 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1576 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1578 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1581 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1585 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1589 #if CONFIG_SENSOR_Exposure
1590 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1592 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1594 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1596 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1598 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1600 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1603 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1607 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1611 #if CONFIG_SENSOR_Saturation
1612 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1614 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1616 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1618 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1620 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1622 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1625 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1629 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1633 #if CONFIG_SENSOR_Contrast
1634 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1636 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1638 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1640 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1642 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1644 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1647 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1651 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1655 #if CONFIG_SENSOR_Mirror
1656 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1658 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1660 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1662 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1664 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1666 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1669 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1673 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1677 #if CONFIG_SENSOR_Flip
1678 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1680 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1682 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1684 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1686 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1688 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1691 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1695 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1699 #if CONFIG_SENSOR_Scene
1700 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1702 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1704 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1706 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1708 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1710 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1713 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1717 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1721 #if CONFIG_SENSOR_WhiteBalance
1722 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1724 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1726 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1728 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
1730 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
1732 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1735 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1739 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1743 #if CONFIG_SENSOR_DigitalZoom
1744 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
1746 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1747 struct sensor *sensor = to_sensor(client);
1748 const struct v4l2_queryctrl *qctrl_info;
1749 int digitalzoom_cur, digitalzoom_total;
1751 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1755 digitalzoom_cur = sensor->info_priv.digitalzoom;
1756 digitalzoom_total = qctrl_info->maximum;
1758 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
1760 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1764 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
1766 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1770 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
1772 *value = digitalzoom_total - digitalzoom_cur;
1775 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
1777 *value = 0 - digitalzoom_cur;
1780 digitalzoom_cur += *value;
1782 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
1784 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
1786 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1789 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
1796 #if CONFIG_SENSOR_Flash
1797 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1799 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
1800 if (value == 3) { /* ddl@rock-chips.com: torch */
1801 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
1803 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1805 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1809 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1814 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1816 struct i2c_client *client = sd->priv;
1817 struct sensor *sensor = to_sensor(client);
1818 const struct v4l2_queryctrl *qctrl;
1820 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
1824 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
1830 case V4L2_CID_BRIGHTNESS:
1832 ctrl->value = sensor->info_priv.brightness;
1835 case V4L2_CID_SATURATION:
1837 ctrl->value = sensor->info_priv.saturation;
1840 case V4L2_CID_CONTRAST:
1842 ctrl->value = sensor->info_priv.contrast;
1845 case V4L2_CID_DO_WHITE_BALANCE:
1847 ctrl->value = sensor->info_priv.whiteBalance;
1850 case V4L2_CID_EXPOSURE:
1852 ctrl->value = sensor->info_priv.exposure;
1855 case V4L2_CID_HFLIP:
1857 ctrl->value = sensor->info_priv.mirror;
1860 case V4L2_CID_VFLIP:
1862 ctrl->value = sensor->info_priv.flip;
1873 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1875 struct i2c_client *client = sd->priv;
1876 struct sensor *sensor = to_sensor(client);
1877 struct soc_camera_device *icd = client->dev.platform_data;
1878 const struct v4l2_queryctrl *qctrl;
1881 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
1885 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
1891 #if CONFIG_SENSOR_Brightness
1892 case V4L2_CID_BRIGHTNESS:
1894 if (ctrl->value != sensor->info_priv.brightness)
1896 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
1900 sensor->info_priv.brightness = ctrl->value;
1905 #if CONFIG_SENSOR_Exposure
1906 case V4L2_CID_EXPOSURE:
1908 if (ctrl->value != sensor->info_priv.exposure)
1910 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
1914 sensor->info_priv.exposure = ctrl->value;
1919 #if CONFIG_SENSOR_Saturation
1920 case V4L2_CID_SATURATION:
1922 if (ctrl->value != sensor->info_priv.saturation)
1924 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
1928 sensor->info_priv.saturation = ctrl->value;
1933 #if CONFIG_SENSOR_Contrast
1934 case V4L2_CID_CONTRAST:
1936 if (ctrl->value != sensor->info_priv.contrast)
1938 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
1942 sensor->info_priv.contrast = ctrl->value;
1947 #if CONFIG_SENSOR_WhiteBalance
1948 case V4L2_CID_DO_WHITE_BALANCE:
1950 if (ctrl->value != sensor->info_priv.whiteBalance)
1952 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
1956 sensor->info_priv.whiteBalance = ctrl->value;
1961 #if CONFIG_SENSOR_Mirror
1962 case V4L2_CID_HFLIP:
1964 if (ctrl->value != sensor->info_priv.mirror)
1966 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
1968 sensor->info_priv.mirror = ctrl->value;
1973 #if CONFIG_SENSOR_Flip
1974 case V4L2_CID_VFLIP:
1976 if (ctrl->value != sensor->info_priv.flip)
1978 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
1980 sensor->info_priv.flip = ctrl->value;
1991 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
1993 const struct v4l2_queryctrl *qctrl;
1994 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1995 struct sensor *sensor = to_sensor(client);
1997 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2001 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2005 switch (ext_ctrl->id)
2007 case V4L2_CID_SCENE:
2009 ext_ctrl->value = sensor->info_priv.scene;
2012 case V4L2_CID_EFFECT:
2014 ext_ctrl->value = sensor->info_priv.effect;
2017 case V4L2_CID_ZOOM_ABSOLUTE:
2019 ext_ctrl->value = sensor->info_priv.digitalzoom;
2022 case V4L2_CID_ZOOM_RELATIVE:
2026 case V4L2_CID_FOCUS_ABSOLUTE:
2028 ext_ctrl->value = sensor->info_priv.focus;
2031 case V4L2_CID_FOCUS_RELATIVE:
2035 case V4L2_CID_FLASH:
2037 ext_ctrl->value = sensor->info_priv.flash;
2045 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2047 const struct v4l2_queryctrl *qctrl;
2048 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2049 struct sensor *sensor = to_sensor(client);
2052 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2056 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2061 switch (ext_ctrl->id)
2063 #if CONFIG_SENSOR_Scene
2064 case V4L2_CID_SCENE:
2066 if (ext_ctrl->value != sensor->info_priv.scene)
2068 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2070 sensor->info_priv.scene = ext_ctrl->value;
2075 #if CONFIG_SENSOR_Effect
2076 case V4L2_CID_EFFECT:
2078 if (ext_ctrl->value != sensor->info_priv.effect)
2080 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2082 sensor->info_priv.effect= ext_ctrl->value;
2087 #if CONFIG_SENSOR_DigitalZoom
2088 case V4L2_CID_ZOOM_ABSOLUTE:
2090 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2093 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2095 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2097 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2099 sensor->info_priv.digitalzoom += val_offset;
2101 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2106 case V4L2_CID_ZOOM_RELATIVE:
2108 if (ext_ctrl->value)
2110 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2112 sensor->info_priv.digitalzoom += ext_ctrl->value;
2114 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2119 #if CONFIG_SENSOR_Focus
2120 case V4L2_CID_FOCUS_ABSOLUTE:
2122 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2125 if (ext_ctrl->value != sensor->info_priv.focus)
2127 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2129 sensor->info_priv.focus += val_offset;
2134 case V4L2_CID_FOCUS_RELATIVE:
2136 if (ext_ctrl->value)
2138 sensor->info_priv.focus += ext_ctrl->value;
2140 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2145 #if CONFIG_SENSOR_Flash
2146 case V4L2_CID_FLASH:
2148 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2150 sensor->info_priv.flash = ext_ctrl->value;
2152 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2163 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2165 struct i2c_client *client = sd->priv;
2166 struct soc_camera_device *icd = client->dev.platform_data;
2167 int i, error_cnt=0, error_idx=-1;
2170 for (i=0; i<ext_ctrl->count; i++) {
2171 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2178 error_idx = ext_ctrl->count;
2180 if (error_idx != -1) {
2181 ext_ctrl->error_idx = error_idx;
2188 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2190 struct i2c_client *client = sd->priv;
2191 struct soc_camera_device *icd = client->dev.platform_data;
2192 int i, error_cnt=0, error_idx=-1;
2195 for (i=0; i<ext_ctrl->count; i++) {
2196 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2203 error_idx = ext_ctrl->count;
2205 if (error_idx != -1) {
2206 ext_ctrl->error_idx = error_idx;
2213 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2214 * this wasn't our capture interface, so, we wait for the right one */
2215 static int sensor_video_probe(struct soc_camera_device *icd,
2216 struct i2c_client *client)
2220 struct sensor *sensor = to_sensor(client);
2222 /* We must have a parent by now. And it cannot be a wrong one.
2223 * So this entire test is completely redundant. */
2224 if (!icd->dev.parent ||
2225 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2228 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2230 goto sensor_video_probe_err;
2234 ret = sensor_write(client, 0xfe, 0x80);
2237 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2240 mdelay(5); //delay 5 microseconds
2242 /* check if it is an sensor sensor */
2243 ret = sensor_read(client, 0x00, &value);
2245 SENSOR_TR("read chip id high byte failed\n");
2247 goto sensor_video_probe_err;
2250 if (value == SENSOR_ID) {
2251 sensor->model = SENSOR_V4L2_IDENT;
2252 SENSOR_TR("chip id:0x%x\n",value);
2254 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), value);
2256 goto sensor_video_probe_err;
2259 icd->formats = sensor_colour_formats;
2260 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
2264 sensor_video_probe_err:
2268 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2270 struct i2c_client *client = sd->priv;
2271 struct soc_camera_device *icd = client->dev.platform_data;
2272 struct sensor *sensor = to_sensor(client);
2274 #if CONFIG_SENSOR_Flash
2278 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2281 case RK29_CAM_SUBDEV_DEACTIVATE:
2283 sensor_deactivate(client);
2287 case RK29_CAM_SUBDEV_IOREQUEST:
2289 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2290 if (sensor->sensor_io_request != NULL) {
2291 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2292 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2293 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2294 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2295 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2296 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2299 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2301 goto sensor_ioctl_end;
2303 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2305 #if CONFIG_SENSOR_Flash
2306 if (sensor->sensor_gpio_res) {
2307 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2308 for (i = 0; i < icd->ops->num_controls; i++) {
2309 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2310 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2313 sensor->info_priv.flash = 0xff;
2314 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2322 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2330 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2331 .init = sensor_init,
2332 .g_ctrl = sensor_g_control,
2333 .s_ctrl = sensor_s_control,
2334 .g_ext_ctrls = sensor_g_ext_controls,
2335 .s_ext_ctrls = sensor_s_ext_controls,
2336 .g_chip_ident = sensor_g_chip_ident,
2337 .ioctl = sensor_ioctl,
2340 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2341 .s_fmt = sensor_s_fmt,
2342 .g_fmt = sensor_g_fmt,
2343 .try_fmt = sensor_try_fmt,
2346 static struct v4l2_subdev_ops sensor_subdev_ops = {
2347 .core = &sensor_subdev_core_ops,
2348 .video = &sensor_subdev_video_ops,
2351 static int sensor_probe(struct i2c_client *client,
2352 const struct i2c_device_id *did)
2354 struct sensor *sensor;
2355 struct soc_camera_device *icd = client->dev.platform_data;
2356 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2357 struct soc_camera_link *icl;
2360 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2362 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2366 icl = to_soc_camera_link(icd);
2368 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2372 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2373 dev_warn(&adapter->dev,
2374 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2378 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2382 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2384 /* Second stage probe - when a capture adapter is there */
2385 icd->ops = &sensor_ops;
2386 icd->y_skip_top = 0;
2387 #if CONFIG_SENSOR_I2C_NOSCHED
2388 atomic_set(&sensor->tasklock_cnt,0);
2391 ret = sensor_video_probe(icd, client);
2394 i2c_set_clientdata(client, NULL);
2398 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2402 static int sensor_remove(struct i2c_client *client)
2404 struct sensor *sensor = to_sensor(client);
2405 struct soc_camera_device *icd = client->dev.platform_data;
2408 i2c_set_clientdata(client, NULL);
2409 client->driver = NULL;
2415 static const struct i2c_device_id sensor_id[] = {
2416 {SENSOR_NAME_STRING(), 0 },
2419 MODULE_DEVICE_TABLE(i2c, sensor_id);
2421 static struct i2c_driver sensor_i2c_driver = {
2423 .name = SENSOR_NAME_STRING(),
2425 .probe = sensor_probe,
2426 .remove = sensor_remove,
2427 .id_table = sensor_id,
2430 static int __init sensor_mod_init(void)
2432 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2433 return i2c_add_driver(&sensor_i2c_driver);
2436 static void __exit sensor_mod_exit(void)
2438 i2c_del_driver(&sensor_i2c_driver);
2441 device_initcall_sync(sensor_mod_init);
2442 module_exit(sensor_mod_exit);
2444 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2445 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2446 MODULE_LICENSE("GPL");