2 #include "generic_sensor.h"
3 #include <linux/moduleparam.h>
6 *v0.0.1: this driver is compatible with generic_sensor
8 * add sensor_focus_af_const_pause_usr_cb;
10 static int version = KERNEL_VERSION(0,1,3);
11 module_param(version, int, S_IRUGO);
14 module_param(debug, int, S_IRUGO|S_IWUSR);
16 #define dprintk(level, fmt, arg...) do { \
18 printk(KERN_WARNING fmt , ## arg); } while (0)
19 #define debug_printk(format, ...) dprintk(1, format, ## __VA_ARGS__)
20 /* Sensor Driver Configuration Begin */
21 #define SENSOR_NAME RK29_CAM_SENSOR_OV2659
22 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV2659
23 #define SENSOR_ID 0x2656
24 #define SENSOR_BUS_PARAM (V4L2_MBUS_MASTER |\
25 V4L2_MBUS_PCLK_SAMPLE_RISING|V4L2_MBUS_HSYNC_ACTIVE_HIGH| V4L2_MBUS_VSYNC_ACTIVE_LOW|\
26 V4L2_MBUS_DATA_ACTIVE_HIGH |SOCAM_MCLK_24MHZ)
27 #define SENSOR_PREVIEW_W 800
28 #define SENSOR_PREVIEW_H 600
29 #define SENSOR_PREVIEW_FPS 15000 // 15fps
30 #define SENSOR_FULLRES_L_FPS 7500 // 7.5fps
31 #define SENSOR_FULLRES_H_FPS 7500 // 7.5fps
32 #define SENSOR_720P_FPS 0
33 #define SENSOR_1080P_FPS 0
35 #define SENSOR_REGISTER_LEN 2 // sensor register address bytes
36 #define SENSOR_VALUE_LEN 1 // sensor register value bytes
38 static unsigned int SensorConfiguration = (CFG_WhiteBalance|CFG_Effect|CFG_Scene);
39 static unsigned int SensorChipID[] = {SENSOR_ID};
40 /* Sensor Driver Configuration End */
43 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
44 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
46 #define SensorRegVal(a,b) CONS4(SensorReg,SENSOR_REGISTER_LEN,Val,SENSOR_VALUE_LEN)(a,b)
47 #define sensor_write(client,reg,v) CONS4(sensor_write_reg,SENSOR_REGISTER_LEN,val,SENSOR_VALUE_LEN)(client,(reg),(v))
48 #define sensor_read(client,reg,v) CONS4(sensor_read_reg,SENSOR_REGISTER_LEN,val,SENSOR_VALUE_LEN)(client,(reg),(v))
49 #define sensor_write_array generic_sensor_write_array
51 struct sensor_parameter
53 unsigned int PreviewDummyPixels;
54 unsigned int CaptureDummyPixels;
55 unsigned int preview_exposure;
56 unsigned short int preview_line_width;
57 unsigned short int preview_gain;
59 unsigned short int PreviewPclk;
60 unsigned short int CapturePclk;
64 struct specific_sensor{
65 struct generic_sensor common_sensor;
66 //define user data below
67 struct sensor_parameter parameter;
72 * The follow setting need been filled.
75 * sensor_init_data : Sensor initial setting;
76 * sensor_fullres_lowfps_data : Sensor full resolution setting with best auality, recommand for video;
77 * sensor_preview_data : Sensor preview resolution setting, recommand it is vga or svga;
78 * sensor_softreset_data : Sensor software reset register;
79 * sensor_check_id_data : Sensir chip id register;
82 * sensor_fullres_highfps_data: Sensor full resolution setting with high framerate, recommand for video;
83 * sensor_720p: Sensor 720p setting, it is for video;
84 * sensor_1080p: Sensor 1080p setting, it is for video;
87 * The SensorEnd which is the setting end flag must be filled int the last of each setting;
90 /* Sensor initial setting */
91 static struct rk_sensor_reg sensor_init_data[] ={
95 //{0x0100, 0x01}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array
259 /* Senor full resolution setting: recommand for capture */
260 static struct rk_sensor_reg sensor_fullres_lowfps_data[] ={
266 {0x5066,0x18}, // zenghaihui 20110920 16
311 {0x3003, 0x80},//10fps
337 /* Senor full resolution setting: recommand for video */
338 static struct rk_sensor_reg sensor_fullres_highfps_data[] ={
341 /* Preview resolution setting*/
342 static struct rk_sensor_reg sensor_preview_data[] =
344 {0x0100, 0x00}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
391 {0x0100, 0x01}, //software wake
396 static struct rk_sensor_reg sensor_720p[]={
401 static struct rk_sensor_reg sensor_1080p[]={
406 static struct rk_sensor_reg sensor_softreset_data[]={
407 SensorRegVal(0x0103,0x01),
411 static struct rk_sensor_reg sensor_check_id_data[]={
412 SensorRegVal(0x300a,0),
413 SensorRegVal(0x300b,0),
417 * The following setting must been filled, if the function is turn on by CONFIG_SENSOR_xxxx
419 static struct rk_sensor_reg sensor_WhiteB_Auto[]=
421 {0x3406, 0x00}, //AWB auto, bit[1]:0,auto
424 /* Cloudy Colour Temperature : 6500K - 8000K */
425 static struct rk_sensor_reg sensor_WhiteB_Cloudy[]=
436 /* ClearDay Colour Temperature : 5000K - 6500K */
437 static struct rk_sensor_reg sensor_WhiteB_ClearDay[]=
449 /* Office Colour Temperature : 3500K - 5000K */
450 static struct rk_sensor_reg sensor_WhiteB_TungstenLamp1[]=
463 /* Home Colour Temperature : 2500K - 3500K */
464 static struct rk_sensor_reg sensor_WhiteB_TungstenLamp2[]=
476 static struct rk_sensor_reg *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
477 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
480 static struct rk_sensor_reg sensor_Brightness0[]=
486 static struct rk_sensor_reg sensor_Brightness1[]=
493 static struct rk_sensor_reg sensor_Brightness2[]=
500 static struct rk_sensor_reg sensor_Brightness3[]=
507 static struct rk_sensor_reg sensor_Brightness4[]=
514 static struct rk_sensor_reg sensor_Brightness5[]=
520 static struct rk_sensor_reg *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
521 sensor_Brightness4, sensor_Brightness5,NULL,
524 static struct rk_sensor_reg sensor_Effect_Normal[] =
530 static struct rk_sensor_reg sensor_Effect_WandB[] =
536 static struct rk_sensor_reg sensor_Effect_Sepia[] =
544 static struct rk_sensor_reg sensor_Effect_Negative[] =
547 {0x507b, 0x40}, //bit[6] negative
550 static struct rk_sensor_reg sensor_Effect_Bluish[] =
559 static struct rk_sensor_reg sensor_Effect_Green[] =
567 static struct rk_sensor_reg *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
568 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
571 static struct rk_sensor_reg sensor_Exposure0[]=
576 static struct rk_sensor_reg sensor_Exposure1[]=
581 static struct rk_sensor_reg sensor_Exposure2[]=
586 static struct rk_sensor_reg sensor_Exposure3[]=
591 static struct rk_sensor_reg sensor_Exposure4[]=
596 static struct rk_sensor_reg sensor_Exposure5[]=
601 static struct rk_sensor_reg sensor_Exposure6[]=
606 static struct rk_sensor_reg *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
607 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
610 static struct rk_sensor_reg sensor_Saturation0[]=
615 static struct rk_sensor_reg sensor_Saturation1[]=
620 static struct rk_sensor_reg sensor_Saturation2[]=
624 static struct rk_sensor_reg *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
626 static struct rk_sensor_reg sensor_Contrast0[]=
631 static struct rk_sensor_reg sensor_Contrast1[]=
636 static struct rk_sensor_reg sensor_Contrast2[]=
641 static struct rk_sensor_reg sensor_Contrast3[]=
646 static struct rk_sensor_reg sensor_Contrast4[]=
652 static struct rk_sensor_reg sensor_Contrast5[]=
657 static struct rk_sensor_reg sensor_Contrast6[]=
661 static struct rk_sensor_reg *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
662 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
664 static struct rk_sensor_reg sensor_SceneAuto[] =
670 static struct rk_sensor_reg sensor_SceneNight[] =
683 static struct rk_sensor_reg *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
685 static struct rk_sensor_reg sensor_Zoom0[] =
690 static struct rk_sensor_reg sensor_Zoom1[] =
695 static struct rk_sensor_reg sensor_Zoom2[] =
701 static struct rk_sensor_reg sensor_Zoom3[] =
705 static struct rk_sensor_reg *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
708 * User could be add v4l2_querymenu in sensor_controls by new_usr_v4l2menu
710 static struct v4l2_querymenu sensor_menus[] =
714 * User could be add v4l2_queryctrl in sensor_controls by new_user_v4l2ctrl
716 static struct sensor_v4l2ctrl_usr_s sensor_controls[] =
720 //MUST define the current used format as the first item
721 static struct rk_sensor_datafmt sensor_colour_fmts[] = {
722 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
723 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
725 //static struct soc_camera_ops sensor_ops;//yzm
729 **********************************************************
730 * Following is local code:
732 * Please codeing your program here
733 **********************************************************
735 static int sensor_parameter_record(struct i2c_client *client)
737 u8 ret_l,ret_m,ret_h;
740 struct generic_sensor *sensor = to_generic_sensor(client);
741 struct specific_sensor *spsensor = to_specific_sensor(sensor);
743 sensor_read(client,0x3a00, &ret_l);
744 sensor_write(client,0x3a00, ret_l&0xfb);
746 sensor_write(client,0x3503,0x07); //stop AE/AG
748 sensor_read(client,0x3500,&ret_h);
749 sensor_read(client,0x3501, &ret_m);
750 sensor_read(client,0x3502, &ret_l);
754 spsensor->parameter.preview_exposure = ((tp_h<<12) & 0xF000) | ((tp_m<<4) & 0x0FF0) | ((tp_l>>4) & 0x0F);
756 //Read back AGC Gain for preview
757 sensor_read(client,0x350b, &ret_l);
758 spsensor->parameter.preview_gain = ret_l;
760 spsensor->parameter.CapturePclk = 24000;
761 spsensor->parameter.PreviewPclk = 24000;
762 spsensor->parameter.PreviewDummyPixels = 0;
763 spsensor->parameter.CaptureDummyPixels = 0;
764 SENSOR_DG("Read 0x350b=0x%02x PreviewExposure:%d 0x3500=0x%02x 0x3501=0x%02x 0x3502=0x%02x",
765 ret_l,spsensor->parameter.preview_exposure,tp_h, tp_m, tp_l);
768 #define OV2659_FULL_PERIOD_PIXEL_NUMS (1940) // default pixel#(w/o dummy pixels) in UXGA mode
769 #define OV2659_FULL_PERIOD_LINE_NUMS (1238) // default line#(w/o dummy lines) in UXGA mode
770 #define OV2659_PV_PERIOD_PIXEL_NUMS (970) // default pixel#(w/o dummy pixels) in SVGA mode
771 #define OV2659_PV_PERIOD_LINE_NUMS (618) // default line#(w/o dummy lines) in SVGA mode
773 /* SENSOR EXPOSURE LINE LIMITATION */
774 #define OV2659_FULL_EXPOSURE_LIMITATION (1236)
775 #define OV2659_PV_EXPOSURE_LIMITATION (618)
778 #define OV2659_IMAGE_SENSOR_FULL_WIDTH (1600)
779 #define OV2659_IMAGE_SENSOR_FULL_HEIGHT (1200)
781 #define OV2659_FULL_GRAB_WIDTH (OV2659_IMAGE_SENSOR_FULL_WIDTH - 16)
782 #define OV2659_FULL_GRAB_HEIGHT (OV2659_IMAGE_SENSOR_FULL_HEIGHT - 12)
783 static void OV2659SetDummy(struct i2c_client *client,unsigned int dummy_pixels, unsigned int dummy_lines)
786 unsigned int temp_reg1, temp_reg2;
787 unsigned int temp_reg;
789 if (dummy_pixels > 0)
791 sensor_read(client,0x380D,&val); // HTS[b7~b0]
793 sensor_read(client,0x380C,&val); // HTS[b15~b8]
795 temp_reg = (temp_reg1 & 0xFF) | (temp_reg2 << 8);
797 temp_reg += dummy_pixels;
799 sensor_write(client,0x380D,(temp_reg&0xFF)); //HTS[7:0]
800 sensor_write(client,0x380C,((temp_reg&0xFF00)>>8)); //HTS[15:8]
805 sensor_read(client,0x380F,&val); // VTS[b7~b0]
807 sensor_read(client,0x380E,&val); // VTS[b15~b8]
809 temp_reg = (temp_reg1 & 0xFF) | (temp_reg2 << 8);
811 temp_reg += dummy_lines;
813 sensor_write(client,0x380F,(temp_reg&0xFF)); //VTS[7:0]
814 sensor_write(client,0x380E,((temp_reg&0xFF00)>>8)); //VTS[15:8]
816 } /* OV2659_set_dummy */
818 static void OV2659WriteShutter(struct i2c_client *client,bool is_preview, unsigned int shutter)
820 unsigned int extra_exposure_lines = 0;
829 if (shutter <= OV2659_PV_EXPOSURE_LIMITATION)
831 extra_exposure_lines = 0;
835 extra_exposure_lines=shutter - OV2659_PV_EXPOSURE_LIMITATION;
841 if (shutter <= OV2659_FULL_EXPOSURE_LIMITATION)
843 extra_exposure_lines = 0;
847 extra_exposure_lines = shutter - OV2659_FULL_EXPOSURE_LIMITATION;
854 sensor_write(client,0x3502, (shutter & 0x00FF)); //AEC[7:0]
855 sensor_write(client,0x3501, ((shutter & 0x0FF00) >>8)); //AEC[15:8]
856 sensor_write(client,0x3500, ((shutter & 0xFF0000) >> 16));
858 if(extra_exposure_lines>0)
860 // set extra exposure line [aec add vts]
861 sensor_write(client,0x3507, extra_exposure_lines & 0xFF); // EXVTS[b7~b0]
862 sensor_write(client,0x3506, (extra_exposure_lines & 0xFF00) >> 8); // EXVTS[b15~b8]
866 // set extra exposure line [aec add vts]
867 sensor_write(client,0x3507, 0x00); // EXVTS[b7~b0]
868 sensor_write(client,0x3506, 0x00); // EXVTS[b15~b8]
871 } /* OV2659_write_shutter */
872 static int sensor_ae_transfer(struct i2c_client *client)
874 unsigned int prev_line_len,cap_line_len,shutter;
875 struct generic_sensor *sensor = to_generic_sensor(client);
876 struct specific_sensor *spsensor = to_specific_sensor(sensor);
879 shutter = spsensor->parameter.preview_exposure;
881 OV2659SetDummy(client,600,0);
883 prev_line_len = OV2659_PV_PERIOD_PIXEL_NUMS + spsensor->parameter.PreviewDummyPixels;
884 cap_line_len = OV2659_FULL_PERIOD_PIXEL_NUMS + spsensor->parameter.CaptureDummyPixels;
885 shutter = (shutter * spsensor->parameter.CapturePclk) / spsensor->parameter.PreviewPclk;
886 shutter = (shutter * prev_line_len) / cap_line_len;
889 OV2659WriteShutter(client,0,shutter);
895 **********************************************************
896 * Following is callback
897 * If necessary, you could coding these callback
898 **********************************************************
901 * the function is called in open sensor
903 static int sensor_activate_cb(struct i2c_client *client)
907 SENSOR_DG("%s",__FUNCTION__);
909 sensor_read(client,0x3000,®_val);
910 sensor_write(client, 0x3000, reg_val|0x03);
911 sensor_write(client, 0x3001, 0xff);
912 sensor_read(client,0x3002,®_val);
913 sensor_write(client, 0x3002, reg_val|0xe0);
918 * the function is called in close sensor
920 static int sensor_deactivate_cb(struct i2c_client *client)
923 struct generic_sensor *sensor = to_generic_sensor(client);
925 SENSOR_DG("%s",__FUNCTION__);
927 /* ddl@rock-chips.com : all sensor output pin must switch into Hi-Z */
928 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
929 sensor_read(client,0x3000,®_val);
930 sensor_write(client, 0x3000, reg_val&0xfc);
931 sensor_write(client, 0x3001, 0x00);
932 sensor_read(client,0x3002,®_val);
933 sensor_write(client, 0x3002, reg_val&0x1f);
939 * the function is called before sensor register setting in VIDIOC_S_FMT
941 static int sensor_s_fmt_cb_th(struct i2c_client *client,struct v4l2_mbus_framefmt *mf, bool capture)
944 sensor_parameter_record(client);
950 * the function is called after sensor register setting finished in VIDIOC_S_FMT
952 static int sensor_s_fmt_cb_bh (struct i2c_client *client,struct v4l2_mbus_framefmt *mf, bool capture)
955 sensor_ae_transfer(client);
959 static int sensor_try_fmt_cb_th(struct i2c_client *client,struct v4l2_mbus_framefmt *mf)
964 static int sensor_softrest_usr_cb(struct i2c_client *client,struct rk_sensor_reg *series)
969 static int sensor_check_id_usr_cb(struct i2c_client *client,struct rk_sensor_reg *series)
974 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
976 //struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
978 if (pm_msg.event == PM_EVENT_SUSPEND) {
979 SENSOR_DG("Suspend");
982 SENSOR_TR("pm_msg.event(0x%x) != PM_EVENT_SUSPEND\n",pm_msg.event);
988 static int sensor_resume(struct soc_camera_device *icd)
996 static int sensor_mirror_cb (struct i2c_client *client, int mirror)
1001 SENSOR_DG("mirror: %d",mirror);
1003 err = sensor_read(client, 0x3821, &val);
1006 err = sensor_write(client, 0x3821, val);
1009 err = sensor_read(client, 0x3821, &val);
1012 err = sensor_write(client, 0x3821, val);
1019 * the function is v4l2 control V4L2_CID_HFLIP callback
1021 static int sensor_v4l2ctrl_mirror_cb(struct soc_camera_device *icd, struct sensor_v4l2ctrl_info_s *ctrl_info,
1022 struct v4l2_ext_control *ext_ctrl)
1024 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1026 if (sensor_mirror_cb(client,ext_ctrl->value) != 0)
1027 SENSOR_TR("sensor_mirror failed, value:0x%x",ext_ctrl->value);
1029 SENSOR_DG("sensor_mirror success, value:0x%x",ext_ctrl->value);
1033 static int sensor_flip_cb(struct i2c_client *client, int flip)
1038 SENSOR_DG("flip: %d",flip);
1040 err = sensor_read(client, 0x3820, &val);
1043 err = sensor_write(client, 0x3820, val);
1046 err = sensor_read(client, 0x3820, &val);
1049 err = sensor_write(client, 0x3820, val);
1056 * the function is v4l2 control V4L2_CID_VFLIP callback
1058 static int sensor_v4l2ctrl_flip_cb(struct soc_camera_device *icd, struct sensor_v4l2ctrl_info_s *ctrl_info,
1059 struct v4l2_ext_control *ext_ctrl)
1061 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1063 if (sensor_flip_cb(client,ext_ctrl->value) != 0)
1064 SENSOR_TR("sensor_flip failed, value:0x%x",ext_ctrl->value);
1066 SENSOR_DG("sensor_flip success, value:0x%x",ext_ctrl->value);
1070 * the functions are focus callbacks
1072 static int sensor_focus_init_usr_cb(struct i2c_client *client){
1076 static int sensor_focus_af_single_usr_cb(struct i2c_client *client){
1080 static int sensor_focus_af_near_usr_cb(struct i2c_client *client){
1084 static int sensor_focus_af_far_usr_cb(struct i2c_client *client){
1088 static int sensor_focus_af_specialpos_usr_cb(struct i2c_client *client,int pos){
1092 static int sensor_focus_af_const_usr_cb(struct i2c_client *client){
1095 static int sensor_focus_af_const_pause_usr_cb(struct i2c_client *client)
1099 static int sensor_focus_af_close_usr_cb(struct i2c_client *client){
1103 static int sensor_focus_af_zoneupdate_usr_cb(struct i2c_client *client, int *zone_tm_pos)
1109 face defect call back
1111 static int sensor_face_detect_usr_cb(struct i2c_client *client,int on){
1116 * The function can been run in sensor_init_parametres which run in sensor_probe, so user can do some
1117 * initialization in the function.
1119 static void sensor_init_parameters_user(struct specific_sensor* spsensor,struct soc_camera_device *icd)
1121 debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()\n", __FILE__, __LINE__,__FUNCTION__);
1127 * It is not allowed to modify the following code
1130 sensor_init_parameters_default_code();
1132 sensor_v4l2_struct_initialization();
1134 sensor_probe_default_code();
1136 sensor_remove_default_code();
1138 sensor_driver_default_module_code();