/* * A V4L2 driver for OV5648 cameras. * */ #include #include #include #include #include #include #include #include #include #include #include #include "camera.h" #include "sensor_helper.h" MODULE_AUTHOR("lwj"); MODULE_DESCRIPTION("A low-level driver for OV5648 sensors"); MODULE_LICENSE("GPL"); //for internel driver debug #define DEV_DBG_EN 0 #if(DEV_DBG_EN == 1) #define vfe_dev_dbg(x,arg...) printk("[OV5648]"x,##arg) #else #define vfe_dev_dbg(x,arg...) #endif #define vfe_dev_err(x,arg...) printk("[OV5648]"x,##arg) #define vfe_dev_print(x,arg...) printk("[OV5648]"x,##arg) #define LOG_ERR_RET(x) { \ int ret; \ ret = x; \ if(ret < 0) {\ vfe_dev_err("error at %s\n",__func__); \ return ret; \ } \ } //define module timing #define MCLK (24*1000*1000) #define VREF_POL V4L2_MBUS_VSYNC_ACTIVE_HIGH #define HREF_POL V4L2_MBUS_HSYNC_ACTIVE_HIGH #define CLK_POL V4L2_MBUS_PCLK_SAMPLE_RISING #define V4L2_IDENT_SENSOR 0x5648 /* * Our nominal (default) frame rate. */ #define SENSOR_FRAME_RATE 30 /* * The ov5648 sits on i2c with ID 0x6c */ #define I2C_ADDR 0x6c #define SENSOR_NAME "ov5648" //static struct delayed_work sensor_s_ae_ratio_work; static struct v4l2_subdev *glb_sd; /* * Information we maintain about a known sensor. */ struct sensor_format_struct; /* coming later */ struct cfg_array { /* coming later */ struct regval_list * regs; int size; }; static inline struct sensor_info *to_state(struct v4l2_subdev *sd) { return container_of(sd, struct sensor_info, sd); } /* * The default register settings * */ static struct regval_list sensor_default_regs[] = { //2lane initial // Slave_ID=0x6c// {0x0100, 0x00}, // Software Standy {0x0103, 0x01}, // Software Reset // delay(5ms) {REG_DLY,0x05}, //must delay {0x3001, 0x00}, // D[7:0] set to input {0x3002, 0x00}, // D[11:8] set to input {0x3011, 0x02}, // Drive strength 2x {0x3013, 0x08}, //extend dvdd {0x3018, 0x4c}, // MIPI 2 lane {0x3022, 0x00}, {0x3034, 0x1a}, // 10-bit mode {0x3035, 0x21}, // PLL {0x3036, 0x69}, // PLL {0x3037, 0x03}, // PLL {0x3038, 0x00}, // PLL {0x3039, 0x00}, // PLL {0x303a, 0x00}, // PLLS {0x303b, 0x19}, // PLLS {0x303c, 0x11}, // PLLS {0x303d, 0x30}, // PLLS {0x3105, 0x11}, {0x3106, 0x05}, // PLL {REG_DLY,0x05}, {0x3013, 0x08}, //disable internal regulator {0x3304, 0x28}, {0x3305, 0x41}, {0x3306, 0x30}, {0x3308, 0x00}, {0x3309, 0xc8}, {0x330a, 0x01}, {0x330b, 0x90}, {0x330c, 0x02}, {0x330d, 0x58}, {0x330e, 0x03}, {0x330f, 0x20}, {0x3300, 0x00}, {0x3500, 0x00}, // exposure [19:16] {0x3501, 0x3d}, // exposure [15:8] {0x3502, 0x00}, // exposure [7:0], exposure = 0x3d0 = 976 {0x3503, 0x07}, // gain has no delay, manual agc/aec {0x350a, 0x00}, // gain[9:8] {0x350b, 0x40}, // gain[7:0], gain = 4x {0x3601, 0x33}, // analog control {0x3602, 0x00}, // analog control {0x3611, 0x0e}, // analog control {0x3612, 0x2b}, // analog control {0x3614, 0x50}, // analog control {0x3620, 0x33}, // analog control {0x3622, 0x00}, // analog control {0x3630, 0xad}, // analog control {0x3631, 0x00}, // analog control {0x3632, 0x94}, // analog control {0x3633, 0x17}, // analog control {0x3634, 0x14}, // analog control {0x3704, 0xc0}, // analog control {0x3705, 0x2a}, // analog control {0x3708, 0x66}, // analog control {0x3709, 0x52}, // analog control {0x370b, 0x23}, // analog control {0x370c, 0xc3}, // analog control {0x370d, 0x00}, // analog control {0x370e, 0x00}, // analog control {0x371c, 0x07}, // analog control {0x3739, 0xd2}, // analog control {0x373c, 0x00}, {0x3800, 0x00}, // xstart = 0 {0x3801, 0x00}, // xstart {0x3802, 0x00}, // ystart = 0 {0x3803, 0x00}, // ystart {0x3804, 0x0a}, // xend = 2623 {0x3805, 0x3f}, // yend {0x3806, 0x07}, // yend = 1955 {0x3807, 0xa3}, // yend {0x3808, 0x05}, // x output size = 1296 {0x3809, 0x10}, // x output size {0x380a, 0x03}, // y output size = 972 {0x380b, 0xcc}, // y output size {0x380c, 0x0b}, // hts = 2816 {0x380d, 0x00}, // hts {0x380e, 0x03}, // vts = 992 {0x380f, 0xe0}, // vts {0x3810, 0x00}, // isp x win = 8 {0x3811, 0x08}, // isp x win {0x3812, 0x00}, // isp y win = 4 {0x3813, 0x04}, // isp y win {0x3814, 0x31}, // x inc {0x3815, 0x31}, // y inc {0x3817, 0x00}, // hsync start {0x3820, 0x08}, // flip off, v bin off {0x3821, 0x07}, // mirror on, h bin on {0x3826, 0x03}, {0x3829, 0x00}, {0x382b, 0x0b}, {0x3830, 0x00}, {0x3836, 0x00}, {0x3837, 0x00}, {0x3838, 0x00}, {0x3839, 0x04}, {0x383a, 0x00}, {0x383b, 0x01}, {0x3b00, 0x00}, // strobe off {0x3b02, 0x08}, // shutter delay {0x3b03, 0x00}, // shutter delay {0x3b04, 0x04}, // frex_exp {0x3b05, 0x00}, // frex_exp {0x3b06, 0x04}, {0x3b07, 0x08}, // frex inv {0x3b08, 0x00}, // frex exp req {0x3b09, 0x02}, // frex end option {0x3b0a, 0x04}, // frex rst length {0x3b0b, 0x00}, // frex strobe width {0x3b0c, 0x3d}, // frex strobe width {0x3f01, 0x0d}, {0x3f0f, 0xf5}, {0x4000, 0x89}, // blc enable {0x4001, 0x02}, // blc start line {0x4002, 0x45}, // blc auto, reset frame number = 5 {0x4004, 0x02}, // black line number {0x4005, 0x18}, // blc normal freeze {0x4006, 0x08}, {0x4007, 0x10}, {0x4008, 0x00}, {0x4300, 0xf8}, {0x4303, 0xff}, {0x4304, 0x00}, {0x4307, 0xff}, {0x4520, 0x00}, {0x4521, 0x00}, {0x4511, 0x22}, {0x4800, 0x14}, // MIPI line sync enable {0x481f, 0x3c}, // MIPI clk prepare min {0x4826, 0x00}, // MIPI hs prepare min {0x4837, 0x18}, // MIPI global timing {0x4b00, 0x06}, {0x4b01, 0x0a}, {0x5000, 0xff}, // bpc on, wpc on {0x5001, 0x00}, // awb disable {0x5002, 0x41}, // win enable, awb gain enable {0x5003, 0x0a}, // buf en, bin auto en {0x5004, 0x00}, // size man off {0x5043, 0x00}, {0x5013, 0x00}, {0x501f, 0x03}, // ISP output data {0x503d, 0x00}, // test pattern off {0x5180, 0x08}, // manual wb gain on {0x5a00, 0x08}, {0x5b00, 0x01}, {0x5b01, 0x40}, {0x5b02, 0x00}, {0x5b03, 0xf0}, {0x301a, 0xf0}, {0x0100, 0x01}, // wake up from software sleep {0x4837, 0x17}, // MIPI global timing };; //for capture static struct regval_list sensor_qsxga_regs[] = { // 2592x1944 15fps 2 lane MIPI 420Mbps/lane {0x0100, 0x00}, {0x3501, 0x7b}, // exposure {0x2502, 0x00}, // exposure {0x3708, 0x63}, {0x3709, 0x12}, {0x370c, 0xc0}, {0x3800, 0x00}, // xstart = 0 {0x3801, 0x00}, // xstart {0x3802, 0x00}, // ystart = 0 {0x3803, 0x00}, // ystart {0x3804, 0x0a}, // xend = 2623 {0x3805, 0x3f}, // xend {0x3806, 0x07}, // yend = 1955 {0x3807, 0xa3}, // yend {0x3808, 0x0a}, // x output size = 2592 {0x3809, 0x20}, // x output size {0x380a, 0x07}, // y output size = 1944 {0x380b, 0x98}, // y output size {0x380c, 0x0b}, // hts = 2816 {0x380d, 0x00}, // hts {0x380e, 0x07}, // vts = 1984 {0x380f, 0xc0}, // vts {0x3810, 0x00}, // isp x win = 16 {0x3811, 0x10}, // isp x win {0x3812, 0x00}, // isp y win = 6 {0x3813, 0x06}, // isp y win {0x3814, 0x11}, // x inc {0x3815, 0x11}, // y inc {0x3817, 0x00}, // hsync start {0x3820, 0x40}, // flip off, v bin off {0x3821, 0x06}, // mirror on, v bin off {0x4004, 0x04}, // black line number {0x4005, 0x1a}, // blc always update {0x350b, 0x40}, // gain = 4x {0x4837, 0x17}, // MIPI global timing {0x0100, 0x01}, }; //for video static struct regval_list sensor_1080p_regs[] = { // 2592x1944 15fps 2 lane MIPI 420Mbps/lane {0x0100, 0x00}, {0x3501, 0x7b}, // exposure {0x2502, 0x00}, // exposure {0x3708, 0x63}, {0x3709, 0x12}, {0x370c, 0xc0}, {0x3800, 0x00}, // xstart = 0 {0x3801, 0x00}, // xstart {0x3802, 0x00}, // ystart = 0 {0x3803, 0x00}, // ystart {0x3804, 0x0a}, // xend = 2623 {0x3805, 0x3f}, // xend {0x3806, 0x07}, // yend = 1955 {0x3807, 0xa3}, // yend {0x3808, 0x0a}, // x output size = 2592 {0x3809, 0x20}, // x output size {0x380a, 0x07}, // y output size = 1944 {0x380b, 0x98}, // y output size {0x380c, 0x0b}, // hts = 2816 {0x380d, 0x00}, // hts {0x380e, 0x07}, // vts = 1984 {0x380f, 0xc0}, // vts {0x3810, 0x00}, // isp x win = 16 {0x3811, 0x10}, // isp x win {0x3812, 0x00}, // isp y win = 6 {0x3813, 0x06}, // isp y win {0x3814, 0x11}, // x inc {0x3815, 0x11}, // y inc {0x3817, 0x00}, // hsync start {0x3820, 0x40}, // flip off, v bin off {0x3821, 0x06}, // mirror on, v bin off {0x4004, 0x04}, // black line number {0x4005, 0x1a}, // blc always update {0x350b, 0x40}, // gain = 4x {0x4837, 0x17}, // MIPI global timing {0x0100, 0x01}, }; static struct regval_list sensor_sxga_regs[] = { // 1296x972 30fps 2 lane MIPI 420Mbps/lane {0x0100, 0x00}, {0x3501, 0x3d}, // exposure {0x3502, 0x00}, // exposure {0x3708, 0x66}, {0x3709, 0x52}, {0x370c, 0xcf}, {0x3800, 0x00}, // xstart = 0 {0x3801, 0x00}, // x start {0x3802, 0x00}, // y start = 0 {0x3803, 0x00}, // y start {0x3804, 0x0a}, // xend = 2623 {0x3805, 0x3f}, // xend {0x3806, 0x07}, // yend = 1955 {0x3807, 0xa3}, // yend {0x3808, 0x05}, // x output size = 1296 {0x3809, 0x10}, // x output size {0x380a, 0x03}, // y output size = 972 {0x380b, 0xcc}, // y output size {0x380c, 0x0b}, // preview_HTS = 2816 {0x380d, 0x00}, // {0x380e, 0x03}, // preview_VTS = 992 {0x380f, 0xe0}, // {0x3810, 0x00}, // isp x win = 8 {0x3811, 0x08}, // isp x win {0x3812, 0x00}, // isp y win = 4 {0x3813, 0x04}, // isp y win {0x3814, 0x31}, // x inc {0x3815, 0x31}, // y inc {0x3817, 0x00}, // hsync start {0x3820, 0x08}, // flip off, v bin off {0x3821, 0x07}, // mirror on, h bin on {0x4004, 0x02}, // black line number {0x4005, 0x18}, // blc level trigger {0x350b, 0x80}, // gain = 8x {0x4837, 0x17}, // MIPI global timing {0x0100, 0x01}, }; static struct regval_list sensor_720p_regs[] = { // 1280x720 30fps 2 lane MIPI 420Mbps/lane {0x0100, 0x00}, {0x3501, 0x2d}, // exposure {0x3502, 0xc0}, // exposure {0x3708, 0x66}, {0x3709, 0x52}, {0x370c, 0xcf}, {0x3800, 0x00}, // xstart = 16 {0x3801, 0x10}, // xstart {0x3802, 0x00}, // ystart = 254 {0x3803, 0xfe}, // ystart {0x3804, 0x0a}, // xend = 2607 {0x3805, 0x2f}, // xend {0x3806, 0x06}, // yend = 1701 {0x3807, 0xa5}, // yend {0x3808, 0x05}, // x output size = 12280 {0x3809, 0x00}, // x output size {0x380a, 0x02}, // y output size = 720 {0x380b, 0xd0}, // y output size {0x380c, 0x0e}, // preview_HTS = 3780; {0x380d, 0xc4}, // {0x380e, 0x02}, // preview_VTS = 742; {0x380f, 0xe6}, // {0x3810, 0x00}, // isp x win = 8 {0x3811, 0x08}, // isp x win {0x3812, 0x00}, // isp y win = 2 {0x3813, 0x02}, // isp y win {0x3814, 0x31}, // x inc {0x3815, 0x31}, // y inc {0x3817, 0x00}, // hsync start {0x3820, 0x08}, // flip off, v bin off {0x3821, 0x07}, // mirror on, h bin on {0x4004, 0x02}, // number of black line {0x4005, 0x18}, // blc level trigger {0x3b0b, 0x80}, // gain = 8x {0x4837, 0x17}, // MIPI global timing {0x0100, 0x01}, }; //static struct regval_list sensor_vga_regs[] = { //VGA: 640*480 //}; /* * Here we'll try to encapsulate the changes for just the output * video format. * */ static struct regval_list sensor_fmt_raw[] = { }; static int sensor_g_exp(struct v4l2_subdev *sd, __s32 *value) { struct sensor_info *info = to_state(sd); *value = info->exp; vfe_dev_dbg("sensor_get_exposure = %d\n", info->exp); return 0; } static int ov5648_sensor_vts = 0; static int sensor_s_exp_gain(struct v4l2_subdev *sd, struct sensor_exp_gain *exp_gain) { int exp_val, gain_val,shutter,frame_length; unsigned char explow=0,expmid=0,exphigh=0; unsigned char gainlow=0,gainhigh=0; struct sensor_info *info = to_state(sd); exp_val = exp_gain->exp_val; gain_val = exp_gain->gain_val; if(gain_val<1*16) gain_val=16; if(gain_val>64*16-1) gain_val=64*16-1; if(exp_val>0xfffff) exp_val=0xfffff; gainlow=(unsigned char)(gain_val&0xff); gainhigh=(unsigned char)((gain_val>>8)&0x3); exphigh = (unsigned char) ( (0x0f0000&exp_val)>>16); expmid = (unsigned char) ( (0x00ff00&exp_val)>>8); explow = (unsigned char) ( (0x0000ff&exp_val) ); shutter = exp_val/16; if(shutter > ov5648_sensor_vts- 4) frame_length = shutter + 4; else frame_length = ov5648_sensor_vts; //printk("exp_val = %d,gain_val = %d\n",exp_val,gain_val); sensor_write(sd, 0x3208, 0x00);//enter group write sensor_write(sd, 0x3503, 0x07); sensor_write(sd, 0x380f, (frame_length & 0xff)); sensor_write(sd, 0x380e, (frame_length >> 8)); sensor_write(sd, 0x350b, gainlow); sensor_write(sd, 0x350a, gainhigh); sensor_write(sd, 0x3502, explow); sensor_write(sd, 0x3501, expmid); sensor_write(sd, 0x3500, exphigh); sensor_write(sd, 0x3208, 0x10);//end group write sensor_write(sd, 0x3208, 0xa0);//init group write info->exp = exp_val; info->gain = gain_val; return 0; } static int sensor_s_exp(struct v4l2_subdev *sd, unsigned int exp_val) { unsigned char explow,expmid,exphigh; struct sensor_info *info = to_state(sd); if(exp_val>0xfffff) exp_val=0xfffff; // if(info->exp == exp_val && exp_val <= (1968)*16) // return 0; exphigh = (unsigned char) ( (0x0f0000&exp_val)>>16); expmid = (unsigned char) ( (0x00ff00&exp_val)>>8); explow = (unsigned char) ( (0x0000ff&exp_val) ); sensor_write(sd, 0x3208, 0x00);//enter group write sensor_write(sd, 0x3502, explow); sensor_write(sd, 0x3501, expmid); sensor_write(sd, 0x3500, exphigh); info->exp = exp_val; return 0; } static int sensor_g_gain(struct v4l2_subdev *sd, __s32 *value) { struct sensor_info *info = to_state(sd); *value = info->gain; vfe_dev_dbg("sensor_get_gain = %d\n", info->gain); return 0; } static int sensor_s_gain(struct v4l2_subdev *sd, int gain_val) { struct sensor_info *info = to_state(sd); unsigned char gainlow=0; unsigned char gainhigh=0; if(gain_val<1*16) gain_val=16; if(gain_val>64*16-1) gain_val=64*16-1; gainlow=(unsigned char)(gain_val&0xff); gainhigh=(unsigned char)((gain_val>>8)&0x3); sensor_write(sd, 0x3503, 0x17); sensor_write(sd, 0x350b, gainlow); sensor_write(sd, 0x350a, gainhigh); sensor_write(sd, 0x3208, 0x10);//end group write sensor_write(sd, 0x3208, 0xa0);//init group write printk("5648 sensor_set_gain = %d, Done!\n", gain_val); info->gain = gain_val; return 0; } static int sensor_s_sw_stby(struct v4l2_subdev *sd, int on_off) { int ret; data_type rdval; ret=sensor_read(sd, 0x0100, &rdval); if(ret!=0) return ret; if(on_off==CSI_GPIO_LOW)//sw stby on { ret=sensor_write(sd, 0x0100, rdval&0xfe); } else//sw stby off { ret=sensor_write(sd, 0x0100, rdval|0x01); } return ret; } /* * Stuff that knows about the sensor. */ static int sensor_power(struct v4l2_subdev *sd, int on) { int ret; ret = 0; switch(on) { case CSI_SUBDEV_STBY_ON: vfe_dev_dbg("CSI_SUBDEV_STBY_ON!\n"); ret = sensor_s_sw_stby(sd, CSI_GPIO_LOW); if(ret < 0) vfe_dev_err("soft stby falied!\n"); mdelay(10); cci_lock(sd); vfe_gpio_write(sd,PWDN,CSI_GPIO_LOW); cci_unlock(sd); vfe_set_mclk(sd,OFF); break; case CSI_SUBDEV_STBY_OFF: vfe_dev_dbg("CSI_SUBDEV_STBY_OFF!\n"); cci_lock(sd); vfe_set_mclk_freq(sd,MCLK); vfe_set_mclk(sd,ON); mdelay(10); vfe_gpio_write(sd,PWDN,CSI_GPIO_HIGH); mdelay(10); cci_unlock(sd); break; case CSI_SUBDEV_PWR_ON: vfe_dev_dbg("CSI_SUBDEV_PWR_ON!\n"); cci_lock(sd); vfe_gpio_set_status(sd,PWDN,1);//set the gpio to output vfe_gpio_set_status(sd,RESET,1);//set the gpio to output vfe_gpio_write(sd,PWDN,CSI_GPIO_LOW); vfe_gpio_write(sd,RESET,CSI_GPIO_LOW); mdelay(1); vfe_gpio_write(sd,POWER_EN,CSI_GPIO_HIGH); vfe_set_pmu_channel(sd,IOVDD,ON); vfe_set_pmu_channel(sd,AFVDD,ON); vfe_set_pmu_channel(sd,AVDD,ON); vfe_set_pmu_channel(sd,DVDD,ON); mdelay(10); vfe_gpio_write(sd,PWDN,CSI_GPIO_HIGH); mdelay(5); vfe_gpio_write(sd,RESET,CSI_GPIO_HIGH); mdelay(10); vfe_set_mclk_freq(sd,MCLK); vfe_set_mclk(sd,ON); mdelay(10); cci_unlock(sd); break; case CSI_SUBDEV_PWR_OFF: vfe_dev_dbg("CSI_SUBDEV_PWR_OFF!\n"); cci_lock(sd); mdelay(5); vfe_gpio_write(sd,PWDN,CSI_GPIO_LOW); vfe_gpio_write(sd,RESET,CSI_GPIO_LOW); vfe_gpio_write(sd,POWER_EN,CSI_GPIO_LOW); vfe_set_pmu_channel(sd,DVDD,OFF); mdelay(1); vfe_set_mclk(sd,OFF); vfe_set_pmu_channel(sd,AVDD,OFF); vfe_set_pmu_channel(sd,IOVDD,OFF); vfe_set_pmu_channel(sd,AFVDD,OFF); vfe_gpio_set_status(sd,RESET,0);//set the gpio to input vfe_gpio_set_status(sd,PWDN,0);//set the gpio to input cci_unlock(sd); break; default: return -EINVAL; } return 0; } static int sensor_reset(struct v4l2_subdev *sd, u32 val) { switch(val) { case 0: vfe_gpio_write(sd,RESET,CSI_GPIO_HIGH); usleep_range(10000,12000); break; case 1: vfe_gpio_write(sd,RESET,CSI_GPIO_LOW); usleep_range(10000,12000); break; default: return -EINVAL; } return 0; } static int sensor_detect(struct v4l2_subdev *sd) { data_type rdval; LOG_ERR_RET(sensor_read(sd, 0x300a, &rdval)) if(rdval != 0x56) return -ENODEV; LOG_ERR_RET(sensor_read(sd, 0x300b, &rdval)) if(rdval != 0x48) return -ENODEV; vfe_dev_dbg("!!!!!!!!!!!!sensor_detect\t%d\t\n", rdval); return 0; } static int sensor_init(struct v4l2_subdev *sd, u32 val) { int ret; struct sensor_info *info = to_state(sd); vfe_dev_dbg("sensor_init\n"); /*Make sure it is a target sensor*/ ret = sensor_detect(sd); if (ret) { vfe_dev_err("chip found is not an target chip.\n"); return ret; } vfe_get_standby_mode(sd,&info->stby_mode); if((info->stby_mode == HW_STBY || info->stby_mode == SW_STBY) \ && info->init_first_flag == 0) { vfe_dev_print("stby_mode and init_first_flag = 0\n"); return 0; } info->focus_status = 0; info->low_speed = 0; info->width = 0; info->height = 0; info->hflip = 0; info->vflip = 0; info->gain = 0; info->tpf.numerator = 1; info->tpf.denominator = 15; /* 30fps */ ret = sensor_write_array(sd, sensor_default_regs, ARRAY_SIZE(sensor_default_regs)); if(ret < 0) { vfe_dev_err("write sensor_default_regs error\n"); return ret; } if(info->stby_mode == 0) info->init_first_flag = 0; info->preview_first_flag = 1; return 0; } static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) { int ret=0; struct sensor_info *info = to_state(sd); switch(cmd) { case GET_CURRENT_WIN_CFG: if(info->current_wins != NULL) { memcpy( arg, info->current_wins, sizeof(struct sensor_win_size) ); ret=0; } else { vfe_dev_err("empty wins!\n"); ret=-1; } break; case SET_FPS: break; case ISP_SET_EXP_GAIN: sensor_s_exp_gain(sd, (struct sensor_exp_gain *)arg); break; default: return -EINVAL; } return ret; } /* * Store information about the video data format. */ static struct sensor_format_struct { __u8 *desc; //__u32 pixelformat; enum v4l2_mbus_pixelcode mbus_code; struct regval_list *regs; int regs_size; int bpp; /* Bytes per pixel */ }sensor_formats[] = { { .desc = "Raw RGB Bayer", .mbus_code = V4L2_MBUS_FMT_SBGGR10_10X1,//V4L2_MBUS_FMT_SGRBG10_10X1, .regs = sensor_fmt_raw, .regs_size = ARRAY_SIZE(sensor_fmt_raw), .bpp = 1 }, }; #define N_FMTS ARRAY_SIZE(sensor_formats) /* * Then there is the issue of window sizes. Try to capture the info here. */ static struct sensor_win_size sensor_win_sizes[] = { /* quxga: 2592*1936 */ { .width = QSXGA_WIDTH, .height = QSXGA_HEIGHT, .hoffset = 0, .voffset = 4, .hts = 2816, .vts = 1984, .pclk = 84*1000*1000, .mipi_bps = 420*1000*1000, .fps_fixed = 2, .bin_factor = 1, .intg_min = 1<<4, .intg_max = (1984-4)<<4, .gain_min = 1<<4, .gain_max = (12<<4)-2, .regs = sensor_qsxga_regs, .regs_size = ARRAY_SIZE(sensor_qsxga_regs), .set_size = NULL, }, /* 1080P */ { .width = HD1080_WIDTH, .height = HD1080_HEIGHT, .hoffset = 336, //(2592-1920)/2, .voffset = 432, //(1944-1080)/2, .hts = 2816, .vts = 1984, .pclk = 84*1000*1000, .mipi_bps = 420*1000*1000, .fps_fixed = 2, .bin_factor = 1, .intg_min = 1<<4, .intg_max = (1984-4)<<4, .gain_min = 1<<4, .gain_max = 10<<4, .regs = sensor_1080p_regs,// .regs_size = ARRAY_SIZE(sensor_1080p_regs),// .set_size = NULL, }, /* SXGA */ { .width = SXGA_WIDTH, .height = SXGA_HEIGHT, .hoffset = 8, //(1296-1280)/2, .voffset = 6, //(972-960)/2, .hts = 2816, .vts = 992, .pclk = 84*1000*1000, .mipi_bps = 420*1000*1000, .fps_fixed = 1, .bin_factor = 1, .intg_min = 1, .intg_max = (992-4)<<4, .gain_min = 1<<4, .gain_max = 10<<4, .regs = sensor_sxga_regs, .regs_size = ARRAY_SIZE(sensor_sxga_regs), .set_size = NULL, }, /* 720p */ { .width = HD720_WIDTH, .height = HD720_HEIGHT, .hoffset = 0, .voffset = 0, .hts = 3780, .vts = 742, .pclk = 84*1000*1000, .mipi_bps = 420*1000*1000, .fps_fixed = 1, .bin_factor = 1, .intg_min = 16, .intg_max = (742-4)<<4, .gain_min = 1<<4, .gain_max = 10<<4, .regs = sensor_720p_regs,// .regs_size = ARRAY_SIZE(sensor_720p_regs),// .set_size = NULL, }, /* VGA */ { .width = VGA_WIDTH, .height = VGA_HEIGHT, .hoffset = 8, //(1296-1280)/2, .voffset = 6, //(972-960)/2, .hts = 2816, .vts = 992, .pclk = 84*1000*1000, .mipi_bps = 420*1000*1000, .fps_fixed = 1, .bin_factor = 1, .intg_min = 1, .intg_max = (992-4)<<4, .gain_min = 1<<4, .gain_max = 10<<4, .width_input = SXGA_WIDTH, .height_input = SXGA_HEIGHT, .regs = sensor_sxga_regs, .regs_size = ARRAY_SIZE(sensor_sxga_regs), .set_size = NULL, }, }; #define N_WIN_SIZES (ARRAY_SIZE(sensor_win_sizes)) static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned index, enum v4l2_mbus_pixelcode *code) { if (index >= N_FMTS) return -EINVAL; *code = sensor_formats[index].mbus_code; return 0; } static int sensor_enum_size(struct v4l2_subdev *sd, struct v4l2_frmsizeenum *fsize) { if(fsize->index > N_WIN_SIZES-1) return -EINVAL; fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; fsize->discrete.width = sensor_win_sizes[fsize->index].width; fsize->discrete.height = sensor_win_sizes[fsize->index].height; return 0; } static int sensor_try_fmt_internal(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt, struct sensor_format_struct **ret_fmt, struct sensor_win_size **ret_wsize) { int index; struct sensor_win_size *wsize; struct sensor_info *info = to_state(sd); for (index = 0; index < N_FMTS; index++) if (sensor_formats[index].mbus_code == fmt->code) break; if (index >= N_FMTS) return -EINVAL; if (ret_fmt != NULL) *ret_fmt = sensor_formats + index; /* * Fields: the sensor devices claim to be progressive. */ fmt->field = V4L2_FIELD_NONE; /* * Round requested image size down to the nearest * we support, but not below the smallest. */ for (wsize = sensor_win_sizes; wsize < sensor_win_sizes + N_WIN_SIZES; wsize++) if (fmt->width >= wsize->width && fmt->height >= wsize->height) break; if (wsize >= sensor_win_sizes + N_WIN_SIZES) wsize--; /* Take the smallest one */ if (ret_wsize != NULL) *ret_wsize = wsize; /* * Note the size we'll actually handle. */ fmt->width = wsize->width; fmt->height = wsize->height; info->current_wins = wsize; return 0; } static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt) { return sensor_try_fmt_internal(sd, fmt, NULL, NULL); } static int sensor_g_mbus_config(struct v4l2_subdev *sd, struct v4l2_mbus_config *cfg) { cfg->type = V4L2_MBUS_CSI2; cfg->flags = 0|V4L2_MBUS_CSI2_2_LANE|V4L2_MBUS_CSI2_CHANNEL_0; return 0; } /* * Set a format. */ static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt) { int ret; struct sensor_format_struct *sensor_fmt; struct sensor_win_size *wsize; struct sensor_info *info = to_state(sd); vfe_dev_dbg("sensor_s_fmt\n"); //sensor_write_array(sd, sensor_oe_disable_regs, ARRAY_SIZE(sensor_oe_disable_regs)); ret = sensor_try_fmt_internal(sd, fmt, &sensor_fmt, &wsize); if (ret) return ret; if(info->capture_mode == V4L2_MODE_VIDEO) { //video } else if(info->capture_mode == V4L2_MODE_IMAGE) { //image } sensor_write_array(sd, sensor_fmt->regs, sensor_fmt->regs_size); ret = 0; if (wsize->regs) LOG_ERR_RET(sensor_write_array(sd, wsize->regs, wsize->regs_size)) if (wsize->set_size) LOG_ERR_RET(wsize->set_size(sd)) info->fmt = sensor_fmt; info->width = wsize->width; info->height = wsize->height; ov5648_sensor_vts = wsize->vts; //vfe_dev_print("s_fmt = %d, width = %d, height = %d\n",sensor_fmt,wsize->width,wsize->height); if(info->capture_mode == V4L2_MODE_VIDEO) { //video } else { //capture image } //sensor_write_array(sd, sensor_oe_enable_regs, ARRAY_SIZE(sensor_oe_enable_regs)); printk("s_fmt end\n"); return 0; } /* * Implement G/S_PARM. There is a "high quality" mode we could try * to do someday; for now, we just do the frame rate tweak. */ static int sensor_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms) { struct v4l2_captureparm *cp = &parms->parm.capture; struct sensor_info *info = to_state(sd); if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; memset(cp, 0, sizeof(struct v4l2_captureparm)); cp->capability = V4L2_CAP_TIMEPERFRAME; cp->capturemode = info->capture_mode; return 0; } static int sensor_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms) { struct v4l2_captureparm *cp = &parms->parm.capture; struct sensor_info *info = to_state(sd); vfe_dev_dbg("sensor_s_parm\n"); if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; if (info->tpf.numerator == 0) return -EINVAL; info->capture_mode = cp->capturemode; return 0; } static int sensor_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc) { /* Fill in min, max, step and default value for these controls. */ /* see include/linux/videodev2.h for details */ switch (qc->id) { case V4L2_CID_GAIN: return v4l2_ctrl_query_fill(qc, 1*16, 32*16, 1, 16); case V4L2_CID_EXPOSURE: return v4l2_ctrl_query_fill(qc, 0, 65535*16, 1, 0); } return -EINVAL; } static int sensor_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) { switch (ctrl->id) { case V4L2_CID_GAIN: return sensor_g_gain(sd, &ctrl->value); case V4L2_CID_EXPOSURE: return sensor_g_exp(sd, &ctrl->value); } return -EINVAL; } static int sensor_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) { struct v4l2_queryctrl qc; int ret; qc.id = ctrl->id; ret = sensor_queryctrl(sd, &qc); if (ret < 0) { return ret; } if (ctrl->value < qc.minimum || ctrl->value > qc.maximum) { vfe_dev_err("max gain qurery is %d,min gain qurey is %d\n",qc.maximum,qc.minimum); return -ERANGE; } switch (ctrl->id) { case V4L2_CID_GAIN: return sensor_s_gain(sd, ctrl->value); case V4L2_CID_EXPOSURE: return sensor_s_exp(sd, ctrl->value); } return -EINVAL; } static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip) { struct i2c_client *client = v4l2_get_subdevdata(sd); return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SENSOR, 0); } /* ----------------------------------------------------------------------- */ static const struct v4l2_subdev_core_ops sensor_core_ops = { .g_chip_ident = sensor_g_chip_ident, .g_ctrl = sensor_g_ctrl, .s_ctrl = sensor_s_ctrl, .queryctrl = sensor_queryctrl, .reset = sensor_reset, .init = sensor_init, .s_power = sensor_power, .ioctl = sensor_ioctl, }; static const struct v4l2_subdev_video_ops sensor_video_ops = { .enum_mbus_fmt = sensor_enum_fmt, .enum_framesizes = sensor_enum_size, .try_mbus_fmt = sensor_try_fmt, .s_mbus_fmt = sensor_s_fmt, .s_parm = sensor_s_parm, .g_parm = sensor_g_parm, .g_mbus_config = sensor_g_mbus_config, }; static const struct v4l2_subdev_ops sensor_ops = { .core = &sensor_core_ops, .video = &sensor_video_ops, }; /* ----------------------------------------------------------------------- */ static struct cci_driver cci_drv = { .name = SENSOR_NAME, .addr_width = CCI_BITS_16, .data_width = CCI_BITS_8, }; static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *id) { struct v4l2_subdev *sd; struct sensor_info *info; info = kzalloc(sizeof(struct sensor_info), GFP_KERNEL); if (info == NULL) return -ENOMEM; sd = &info->sd; glb_sd = sd; cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv); info->fmt = &sensor_formats[0]; info->af_first_flag = 1; info->init_first_flag = 1; return 0; } static int sensor_remove(struct i2c_client *client) { struct v4l2_subdev *sd; sd = cci_dev_remove_helper(client, &cci_drv); kfree(to_state(sd)); return 0; } static const struct i2c_device_id sensor_id[] = { {SENSOR_NAME, 0 }, { } }; MODULE_DEVICE_TABLE(i2c, sensor_id); static struct i2c_driver sensor_driver = { .driver = { .owner = THIS_MODULE, .name = SENSOR_NAME, }, .probe = sensor_probe, .remove = sensor_remove, .id_table = sensor_id, }; static __init int init_sensor(void) { return cci_dev_init_helper(&sensor_driver); } static __exit void exit_sensor(void) { cci_dev_exit_helper(&sensor_driver); } module_init(init_sensor); module_exit(exit_sensor);