2 * Driver for OV5642 CMOS Image Sensor from OmniVision
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/rk_camera.h>
23 #include <linux/vmalloc.h>
27 module_param(debug, int, S_IRUGO|S_IWUSR);
29 #define dprintk(level, fmt, arg...) do { \
31 printk(KERN_WARNING fmt , ## arg); } while (0)
33 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
34 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
36 #define _CONS(a,b) a##b
37 #define CONS(a,b) _CONS(a,b)
40 #define _STR(x) __STR(x)
41 #define STR(x) _STR(x)
43 #define MIN(x,y) ((x<y) ? x: y)
44 #define MAX(x,y) ((x>y) ? x: y)
46 /* Sensor Driver Configuration */
47 #define SENSOR_NAME RK_CAM_SENSOR_OV5642
48 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV5642
49 #define SENSOR_ID 0x5642
50 #define SENSOR_MIN_WIDTH 176
51 #define SENSOR_MIN_HEIGHT 144
52 #define SENSOR_MAX_WIDTH 2592
53 #define SENSOR_MAX_HEIGHT 1944
54 #define SENSOR_INIT_WIDTH sensor_init_width /* Sensor pixel size for sensor_init_data array */
55 #define SENSOR_INIT_HEIGHT sensor_init_height
56 #define SENSOR_INIT_WINSEQADR sensor_init_winseq_p
57 #define SENSOR_INIT_PIXFMT sensor_init_pixelcode
58 #define SENSOR_BUS_PARAM sensor_init_busparam
60 #define CONFIG_SENSOR_WhiteBalance 1
61 #define CONFIG_SENSOR_Brightness 0
62 #define CONFIG_SENSOR_Contrast 0
63 #define CONFIG_SENSOR_Saturation 0
64 #define CONFIG_SENSOR_Effect 1
65 #define CONFIG_SENSOR_Scene 1
66 #define CONFIG_SENSOR_DigitalZoom 0
67 #define CONFIG_SENSOR_Exposure 0
68 #define CONFIG_SENSOR_Flash 1
69 #define CONFIG_SENSOR_Mirror 0
70 #define CONFIG_SENSOR_Flip 0
71 #ifdef CONFIG_OV5642_AUTOFOCUS
72 #define CONFIG_SENSOR_Focus 1
73 #include "ov5642_af_firmware.c"
75 #define CONFIG_SENSOR_Focus 0
78 #define CONFIG_SENSOR_I2C_SPEED 250000 /* Hz */
79 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
80 #define CONFIG_SENSOR_I2C_NOSCHED 0
81 #define CONFIG_SENSOR_I2C_RDWRCHK 0
83 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
84 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
85 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
86 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
87 #define COLOR_TEMPERATURE_OFFICE_DN 3500
88 #define COLOR_TEMPERATURE_OFFICE_UP 5000
89 #define COLOR_TEMPERATURE_HOME_DN 2500
90 #define COLOR_TEMPERATURE_HOME_UP 3500
92 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
93 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
95 #define SENSOR_AF_IS_ERR (0x00<<0)
96 #define SENSOR_AF_IS_OK (0x01<<0)
97 #define SENSOR_INIT_IS_ERR (0x00<<28)
98 #define SENSOR_INIT_IS_OK (0x01<<28)
100 #if CONFIG_SENSOR_Focus
101 #define SENSOR_AF_MODE_INFINITY 0
102 #define SENSOR_AF_MODE_MACRO 1
103 #define SENSOR_AF_MODE_FIXED 2
104 #define SENSOR_AF_MODE_AUTO 3
105 #define SENSOR_AF_MODE_CONTINUOUS 4
106 #define SENSOR_AF_MODE_CLOSE 5
109 #if CONFIG_SENSOR_Focus
110 /* ov5642 VCM Command and Status Registers */
111 #define CMD_MAIN_Reg 0x3024
112 #define CMD_TAG_Reg 0x3025
113 #define CMD_PARA0_Reg 0x5085
114 #define CMD_PARA1_Reg 0x5084
115 #define CMD_PARA2_Reg 0x5083
116 #define CMD_PARA3_Reg 0x5082
117 #define STA_ZONE_Reg 0x3026
118 #define STA_FOCUS_Reg 0x3027
120 /* ov5642 VCM Command */
121 #define OverlayEn_Cmd 0x01
122 #define OverlayDis_Cmd 0x02
123 #define SingleFocus_Cmd 0x03
124 #define ConstFocus_Cmd 0x04
125 #define StepMode_Cmd 0x05
126 #define PauseFocus_Cmd 0x06
127 #define ReturnIdle_Cmd 0x08
128 #define SetZone_Cmd 0x10
129 #define UpdateZone_Cmd 0x12
130 #define SetMotor_Cmd 0x20
132 /* ov5642 Focus State */
133 #define S_FIRWRE 0xFF
134 #define S_STARTUP 0xfa
136 #define S_DRVICERR 0xee
138 #define S_FOCUSING 0x01
139 #define S_FOCUSED 0x02
140 #define S_CAPTURE 0x12
143 /* ov5642 Zone State */
144 #define Zone_Is_Focused(a, zone_val) (zone_val&(1<<(a-3)))
145 #define Zone_Get_ID(zone_val) (zone_val&0x03)
147 #define Zone_CenterMode 0x01
148 #define Zone_5xMode 0x02
149 #define Zone_5PlusMode 0x03
150 #define Zone_4fMode 0x04
152 #define ZoneSel_Auto 0x0b
153 #define ZoneSel_SemiAuto 0x0c
154 #define ZoneSel_Manual 0x0d
155 #define ZoneSel_Rotate 0x0e
157 /* ov5642 Step Focus Commands */
158 #define StepFocus_Near_Tag 0x01
159 #define StepFocus_Far_Tag 0x02
160 #define StepFocus_Furthest_Tag 0x03
161 #define StepFocus_Nearest_Tag 0x04
162 #define StepFocus_Spec_Tag 0x10
165 static s32 sensor_init_height = 0;
166 static unsigned long sensor_init_busparam = (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING|SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ);
167 static enum v4l2_mbus_pixelcode sensor_init_pixelcode = V4L2_MBUS_FMT_YUYV8_2X8;
168 static struct reginfo* sensor_init_data_p = NULL;
169 static struct reginfo* sensor_init_winseq_p = NULL;
170 static struct reginfo* sensor_init_winseq_board = NULL;
171 /* init 800X600 SVGA */
172 static struct reginfo sensor_init_data[] =
242 {0x4300 , 0x32}, //UYVY
845 /* 720p 15fps @ 1280x720 */
847 static struct reginfo sensor_720p[]=
850 {SEQUENCE_PROPERTY, SEQUENCE_INIT},
1311 {0x3011, 0x10 },//15fps 0x08 // 10fps: 0x05
1364 {0x4300, 0x32 }, // 0x30---> 0x32, set yuv
1369 //{0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
1450 //{0x3008,0x02}, //software wake
1456 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
1458 static struct reginfo sensor_1080p[]=
1461 {SEQUENCE_PROPERTY, SEQUENCE_INIT},
1474 { 0x3011, 0x05 }, //10 fps 0x05 //15fps 0x08
1922 { 0x3011, 0x07 }, //10 fps 0x05 //15fps 0x07
1930 { 0x3807, 0x39 },//0x3a
1968 { 0x4300, 0x32 }, // set yuv, 0x30--> 0x32
1973 //{0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
1988 {0x3622,0x00},//60},
1990 {0x3604,0x40},//48},
2019 { 0x3011, 0x07 }, //10 fps 0x05 //15fps 0x08
2022 {0x3a08,0x14}, //0x05
2023 {0x3a09,0xe0}, //0xf5
2024 {0x3a0a,0x11}, //0x04
2025 {0x3a0b,0x60}, //0xf6
2026 {0x3a0d,0x04}, //0x0b
2027 {0x3a0e,0x03}, //0x0d
2053 //{0x3008,0x02}, //software wake
2058 /* 2592X1944 QSXGA */
2059 static struct reginfo sensor_qsxga[] =
2061 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2129 {0x3008,0x02}, //software wake
2132 /* 2048*1536 QXGA */
2133 static struct reginfo sensor_qxga[] =
2135 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2229 {0x3008,0x02}, //software wake
2233 /* 1600X1200 UXGA */
2234 static struct reginfo sensor_uxga[] =
2236 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2330 {0x3008,0x02}, //software wake
2333 /* 1280X1024 SXGA */
2334 static struct reginfo sensor_sxga[] =
2339 static struct reginfo sensor_xga[] =
2341 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2435 {0x3008,0x02}, //software wake
2440 static struct reginfo sensor_svga[] =
2530 static struct reginfo sensor_vga[] =
2536 static struct reginfo sensor_cif[] =
2567 static struct reginfo sensor_qvga[] =
2573 static struct reginfo sensor_qcif[] =
2604 static struct reginfo ov2655_qqvga[] =
2648 static struct reginfo ov2655_Sharpness_auto[] =
2653 static struct reginfo ov2655_Sharpness1[] =
2659 static struct reginfo ov2655_Sharpness2[][3] =
2666 static struct reginfo ov2655_Sharpness3[] =
2672 static struct reginfo ov2655_Sharpness4[]=
2679 static struct reginfo ov2655_Sharpness5[] =
2687 static struct reginfo sensor_ClrFmt_YUYV[]=
2693 static struct reginfo sensor_ClrFmt_UYVY[]=
2700 #if CONFIG_SENSOR_WhiteBalance
2701 static struct reginfo sensor_WhiteB_Auto[]=
2723 /* Cloudy Colour Temperature : 6500K - 8000K */
2724 static struct reginfo sensor_WhiteB_Cloudy[]=
2735 /* ClearDay Colour Temperature : 5000K - 6500K */
2736 static struct reginfo sensor_WhiteB_ClearDay[]=
2748 /* Office Colour Temperature : 3500K - 5000K */
2749 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
2762 /* Home Colour Temperature : 2500K - 3500K */
2763 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
2775 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
2776 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
2780 #if CONFIG_SENSOR_Brightness
2781 static struct reginfo sensor_Brightness0[]=
2786 static struct reginfo sensor_Brightness1[]=
2791 static struct reginfo sensor_Brightness2[]=
2796 static struct reginfo sensor_Brightness3[]=
2801 static struct reginfo sensor_Brightness4[]=
2806 static struct reginfo sensor_Brightness5[]=
2810 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
2811 sensor_Brightness4, sensor_Brightness5,NULL,
2816 #if CONFIG_SENSOR_Effect
2817 static struct reginfo sensor_Effect_Normal[] =
2824 static struct reginfo sensor_Effect_WandB[] =
2833 static struct reginfo sensor_Effect_Sepia[] =
2842 static struct reginfo sensor_Effect_Negative[] =
2849 static struct reginfo sensor_Effect_Bluish[] =
2859 static struct reginfo sensor_Effect_Green[] =
2868 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
2869 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
2872 #if CONFIG_SENSOR_Exposure
2873 static struct reginfo sensor_Exposure0[]=
2878 static struct reginfo sensor_Exposure1[]=
2883 static struct reginfo sensor_Exposure2[]=
2888 static struct reginfo sensor_Exposure3[]=
2893 static struct reginfo sensor_Exposure4[]=
2898 static struct reginfo sensor_Exposure5[]=
2903 static struct reginfo sensor_Exposure6[]=
2908 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
2909 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
2912 #if CONFIG_SENSOR_Saturation
2913 static struct reginfo sensor_Saturation0[]=
2918 static struct reginfo sensor_Saturation1[]=
2923 static struct reginfo sensor_Saturation2[]=
2927 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
2930 #if CONFIG_SENSOR_Contrast
2931 static struct reginfo sensor_Contrast0[]=
2936 static struct reginfo sensor_Contrast1[]=
2941 static struct reginfo sensor_Contrast2[]=
2946 static struct reginfo sensor_Contrast3[]=
2951 static struct reginfo sensor_Contrast4[]=
2957 static struct reginfo sensor_Contrast5[]=
2962 static struct reginfo sensor_Contrast6[]=
2966 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
2967 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
2971 #if CONFIG_SENSOR_Mirror
2972 static struct reginfo sensor_MirrorOn[]=
2977 static struct reginfo sensor_MirrorOff[]=
2981 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
2983 #if CONFIG_SENSOR_Flip
2984 static struct reginfo sensor_FlipOn[]=
2989 static struct reginfo sensor_FlipOff[]=
2993 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
2996 #if CONFIG_SENSOR_Scene
2997 static struct reginfo sensor_SceneAuto[] =
3003 static struct reginfo sensor_SceneNight[] =
3006 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
3024 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
3027 #if CONFIG_SENSOR_DigitalZoom
3028 static struct reginfo sensor_Zoom0[] =
3033 static struct reginfo sensor_Zoom1[] =
3038 static struct reginfo sensor_Zoom2[] =
3044 static struct reginfo sensor_Zoom3[] =
3048 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
3050 static const struct v4l2_querymenu sensor_menus[] =
3052 #if CONFIG_SENSOR_WhiteBalance
3053 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
3054 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
3055 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
3058 #if CONFIG_SENSOR_Effect
3059 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
3060 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
3061 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
3064 #if CONFIG_SENSOR_Scene
3065 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
3068 #if CONFIG_SENSOR_Flash
3069 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
3070 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
3074 static const struct v4l2_queryctrl sensor_controls[] =
3076 #if CONFIG_SENSOR_WhiteBalance
3078 .id = V4L2_CID_DO_WHITE_BALANCE,
3079 .type = V4L2_CTRL_TYPE_MENU,
3080 .name = "White Balance Control",
3088 #if CONFIG_SENSOR_Brightness
3090 .id = V4L2_CID_BRIGHTNESS,
3091 .type = V4L2_CTRL_TYPE_INTEGER,
3092 .name = "Brightness Control",
3100 #if CONFIG_SENSOR_Effect
3102 .id = V4L2_CID_EFFECT,
3103 .type = V4L2_CTRL_TYPE_MENU,
3104 .name = "Effect Control",
3112 #if CONFIG_SENSOR_Exposure
3114 .id = V4L2_CID_EXPOSURE,
3115 .type = V4L2_CTRL_TYPE_INTEGER,
3116 .name = "Exposure Control",
3124 #if CONFIG_SENSOR_Saturation
3126 .id = V4L2_CID_SATURATION,
3127 .type = V4L2_CTRL_TYPE_INTEGER,
3128 .name = "Saturation Control",
3136 #if CONFIG_SENSOR_Contrast
3138 .id = V4L2_CID_CONTRAST,
3139 .type = V4L2_CTRL_TYPE_INTEGER,
3140 .name = "Contrast Control",
3148 #if CONFIG_SENSOR_Mirror
3150 .id = V4L2_CID_HFLIP,
3151 .type = V4L2_CTRL_TYPE_BOOLEAN,
3152 .name = "Mirror Control",
3160 #if CONFIG_SENSOR_Flip
3162 .id = V4L2_CID_VFLIP,
3163 .type = V4L2_CTRL_TYPE_BOOLEAN,
3164 .name = "Flip Control",
3172 #if CONFIG_SENSOR_Scene
3174 .id = V4L2_CID_SCENE,
3175 .type = V4L2_CTRL_TYPE_MENU,
3176 .name = "Scene Control",
3184 #if CONFIG_SENSOR_DigitalZoom
3186 .id = V4L2_CID_ZOOM_RELATIVE,
3187 .type = V4L2_CTRL_TYPE_INTEGER,
3188 .name = "DigitalZoom Control",
3194 .id = V4L2_CID_ZOOM_ABSOLUTE,
3195 .type = V4L2_CTRL_TYPE_INTEGER,
3196 .name = "DigitalZoom Control",
3204 #if CONFIG_SENSOR_Focus
3206 .id = V4L2_CID_FOCUS_RELATIVE,
3207 .type = V4L2_CTRL_TYPE_INTEGER,
3208 .name = "Focus Control",
3214 .id = V4L2_CID_FOCUS_ABSOLUTE,
3215 .type = V4L2_CTRL_TYPE_INTEGER,
3216 .name = "Focus Control",
3220 .default_value = 125,
3223 .id = V4L2_CID_FOCUS_AUTO,
3224 .type = V4L2_CTRL_TYPE_BOOLEAN,
3225 .name = "Focus Control",
3231 .id = V4L2_CID_FOCUS_CONTINUOUS,
3232 .type = V4L2_CTRL_TYPE_BOOLEAN,
3233 .name = "Focus Control",
3241 #if CONFIG_SENSOR_Flash
3243 .id = V4L2_CID_FLASH,
3244 .type = V4L2_CTRL_TYPE_MENU,
3245 .name = "Flash Control",
3254 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
3255 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
3256 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
3257 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
3258 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
3259 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
3260 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
3261 static int sensor_resume(struct soc_camera_device *icd);
3262 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
3263 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
3264 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
3265 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
3266 static int sensor_deactivate(struct i2c_client *client);
3267 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
3268 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
3270 static struct soc_camera_ops sensor_ops =
3272 .suspend = sensor_suspend,
3273 .resume = sensor_resume,
3274 .set_bus_param = sensor_set_bus_param,
3275 .query_bus_param = sensor_query_bus_param,
3276 .controls = sensor_controls,
3277 .menus = sensor_menus,
3278 .num_controls = ARRAY_SIZE(sensor_controls),
3279 .num_menus = ARRAY_SIZE(sensor_menus),
3282 /* only one fixed colorspace per pixelcode */
3283 struct sensor_datafmt {
3284 enum v4l2_mbus_pixelcode code;
3285 enum v4l2_colorspace colorspace;
3288 /* Find a data format by a pixel code in an array */
3289 static const struct sensor_datafmt *sensor_find_datafmt(
3290 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
3294 for (i = 0; i < n; i++)
3295 if (fmt[i].code == code)
3301 static const struct sensor_datafmt sensor_colour_fmts[] = {
3302 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
3303 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
3309 WqCmd_af_special_pos,
3313 WqCmd_af_update_zone
3315 enum sensor_wq_result
3323 struct i2c_client *client;
3324 struct delayed_work dwork;
3325 enum sensor_wq_cmd cmd;
3326 wait_queue_head_t done;
3327 enum sensor_wq_result result;
3331 typedef struct sensor_info_priv_s
3345 unsigned char mirror; /* HFLIP */
3346 unsigned char flip; /* VFLIP */
3349 struct reginfo *winseqe_cur_addr;
3350 struct sensor_datafmt fmt;
3351 unsigned int enable;
3352 unsigned int funmodule_state;
3353 } sensor_info_priv_t;
3357 struct sensor_parameter
3359 unsigned short int preview_maxlines;
3360 unsigned short int preview_exposure;
3361 unsigned short int preview_line_width;
3362 unsigned short int preview_gain;
3364 unsigned short int capture_framerate;
3365 unsigned short int preview_framerate;
3370 struct v4l2_subdev subdev;
3371 struct i2c_client *client;
3372 sensor_info_priv_t info_priv;
3373 struct sensor_parameter parameter;
3374 struct workqueue_struct *sensor_wq;
3375 struct mutex wq_lock;
3376 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
3377 #if CONFIG_SENSOR_I2C_NOSCHED
3378 atomic_t tasklock_cnt;
3380 struct rkcamera_platform_data *sensor_io_request;
3381 struct rkcamera_gpio_res *sensor_gpio_res;
3384 static struct sensor* to_sensor(const struct i2c_client *client)
3386 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
3389 static int sensor_task_lock(struct i2c_client *client, int lock)
3391 #if CONFIG_SENSOR_I2C_NOSCHED
3393 struct sensor *sensor = to_sensor(client);
3396 if (atomic_read(&sensor->tasklock_cnt) == 0) {
3397 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
3398 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
3402 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
3403 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
3404 goto sensor_task_lock_err;
3409 atomic_add(1, &sensor->tasklock_cnt);
3411 if (atomic_read(&sensor->tasklock_cnt) > 0) {
3412 atomic_sub(1, &sensor->tasklock_cnt);
3414 if (atomic_read(&sensor->tasklock_cnt) == 0)
3419 sensor_task_lock_err:
3427 /* sensor register write */
3428 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
3432 struct i2c_msg msg[1];
3435 buf[1] = reg & 0xFF;
3438 msg->addr = client->addr;
3439 msg->flags = client->flags;
3441 msg->len = sizeof(buf);
3442 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3443 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
3448 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
3449 err = i2c_transfer(client->adapter, msg, 1);
3454 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
3462 /* sensor register read */
3463 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
3467 struct i2c_msg msg[2];
3470 buf[1] = reg & 0xFF;
3472 msg[0].addr = client->addr;
3473 msg[0].flags = client->flags;
3475 msg[0].len = sizeof(buf);
3476 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3477 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
3479 msg[1].addr = client->addr;
3480 msg[1].flags = client->flags|I2C_M_RD;
3483 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3484 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
3488 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
3489 err = i2c_transfer(client->adapter, msg, 2);
3495 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
3503 /* write a array of registers */
3504 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
3508 #if CONFIG_SENSOR_Focus
3509 struct sensor *sensor = to_sensor(client);
3511 #if CONFIG_SENSOR_I2C_RDWRCHK
3516 if (sensor_task_lock(client, 1) < 0)
3517 goto sensor_write_array_end;
3518 while (regarray[i].reg != 0)
3520 #if CONFIG_SENSOR_Focus
3521 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
3522 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
3524 goto sensor_write_array_end;
3528 err = sensor_write(client, regarray[i].reg, regarray[i].val);
3532 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
3536 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
3538 goto sensor_write_array_end;
3541 #if CONFIG_SENSOR_I2C_RDWRCHK
3542 sensor_read(client, regarray[i].reg, &valchk);
3543 if (valchk != regarray[i].val)
3544 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
3551 #if CONFIG_SENSOR_Focus
3552 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
3553 || (regarray == sensor_init_data)) {
3554 sensor->info_priv.affm_reinit = 1;
3558 sensor_write_array_end:
3559 sensor_task_lock(client,0);
3562 #if CONFIG_SENSOR_I2C_RDWRCHK
3563 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
3571 while (regarray[i].reg != 0)
3573 sensor_read(client, regarray[i].reg, &valchk);
3574 if (valchk != regarray[i].val)
3575 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
3582 #if CONFIG_SENSOR_Focus
3589 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
3592 char read_tag=0xff,cnt;
3593 struct sensor *sensor = to_sensor(client);
3595 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3596 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3597 goto sensor_af_cmdset_err;
3601 if (cmdinfo->validate_bit & 0x80) {
3602 if (sensor_write(client, CMD_TAG_Reg, cmdinfo->cmd_tag)) {
3603 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
3604 goto sensor_af_cmdset_err;
3606 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
3608 for (i=0; i<4; i++) {
3609 if (cmdinfo->validate_bit & (1<<i)) {
3610 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
3611 SENSOR_TR("%s write CMD_PARA_Reg(main:0x%x para%d:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,i,cmdinfo->cmd_para[i]);
3612 goto sensor_af_cmdset_err;
3614 SENSOR_DG("%s write CMD_PARA_Reg(main:0x%x para%d:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,i,cmdinfo->cmd_para[i]);
3618 if (sensor_write(client, CMD_TAG_Reg, 0xff)) {
3619 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
3620 goto sensor_af_cmdset_err;
3622 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
3625 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
3626 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
3627 goto sensor_af_cmdset_err;
3634 if (sensor_read(client,CMD_TAG_Reg,&read_tag)){
3635 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
3638 } while((read_tag != 0x00)&& (cnt++<100));
3640 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
3642 sensor_af_cmdset_err:
3646 static int sensor_af_idlechk(struct i2c_client *client)
3650 struct sensor *sensor = to_sensor(client);
3652 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3653 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3655 goto sensor_af_idlechk_end;
3661 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3663 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3665 goto sensor_af_idlechk_end;
3668 if (state != S_IDLE) {
3669 sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
3673 } while((state != S_IDLE)&& (cnt<20));
3675 ret = (state == S_IDLE) ? 0 : -1;
3677 sensor_af_idlechk_end:
3681 static int sensor_af_single(struct i2c_client *client)
3685 struct sensor *sensor = to_sensor(client);
3687 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3688 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3690 goto sensor_af_single_end;
3693 if (sensor_af_idlechk(client))
3694 goto sensor_af_single_end;
3696 if (sensor_af_cmdset(client, SingleFocus_Cmd, NULL)) {
3697 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
3699 goto sensor_af_single_end;
3709 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3711 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3713 goto sensor_af_single_end;
3715 }while((state == S_FOCUSING) && (cnt<100));
3717 if (state != S_FOCUSED) {
3718 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
3720 goto sensor_af_single_end;
3722 sensor_af_single_end:
3726 static int sensor_af_const(struct i2c_client *client)
3729 struct sensor *sensor = to_sensor(client);
3731 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3732 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3734 goto sensor_af_const_end;
3737 if (sensor_af_idlechk(client))
3738 goto sensor_af_const_end;
3740 if (sensor_af_cmdset(client, ConstFocus_Cmd, NULL)) {
3741 SENSOR_TR("%s const focus mode set error!\n",SENSOR_NAME_STRING());
3743 goto sensor_af_const_end;
3745 sensor_af_const_end:
3748 static int sensor_af_pause2capture(struct i2c_client *client)
3752 struct sensor *sensor = to_sensor(client);
3754 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3755 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3757 goto sensor_af_pause_end;
3760 if (sensor_af_cmdset(client, PauseFocus_Cmd, NULL)) {
3761 SENSOR_TR("%s pause focus mode set error!\n",SENSOR_NAME_STRING());
3763 goto sensor_af_pause_end;
3773 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3775 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3777 goto sensor_af_pause_end;
3779 }while((state != S_CAPTURE) && (cnt<100));
3781 if (state != S_CAPTURE) {
3782 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
3784 goto sensor_af_pause_end;
3786 sensor_af_pause_end:
3789 static int sensor_af_zoneupdate(struct i2c_client *client)
3792 struct sensor *sensor = to_sensor(client);
3794 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3795 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3797 goto sensor_af_zoneupdate_end;
3800 if (sensor_af_idlechk(client))
3801 goto sensor_af_zoneupdate_end;
3803 if (sensor_af_cmdset(client, UpdateZone_Cmd, NULL)) {
3804 SENSOR_TR("%s update zone fail!\n",SENSOR_NAME_STRING());
3806 goto sensor_af_zoneupdate_end;
3809 sensor_af_zoneupdate_end:
3812 static int sensor_af_init(struct i2c_client *client)
3817 ret = sensor_write_array(client, sensor_af_firmware);
3819 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
3821 goto sensor_af_init_end;
3831 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3833 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3835 goto sensor_af_init_end;
3837 }while((state == S_STARTUP) && (cnt<100));
3839 if (state != S_IDLE) {
3840 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
3842 goto sensor_af_init_end;
3846 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
3850 static int sensor_af_downfirmware(struct i2c_client *client)
3852 struct sensor *sensor = to_sensor(client);
3853 struct af_cmdinfo cmdinfo;
3854 int ret=0, focus_pos = 0xfe;
3855 struct soc_camera_device *icd = client->dev.platform_data;
3856 struct v4l2_mbus_framefmt mf;
3858 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
3860 if (sensor_af_init(client)) {
3861 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
3864 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
3866 mf.width = icd->user_width;
3867 mf.height = icd->user_height;
3868 mf.code = sensor->info_priv.fmt.code;
3869 mf.colorspace = sensor->info_priv.fmt.colorspace;
3870 mf.field = V4L2_FIELD_NONE;
3871 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
3872 ret = sensor_af_const(client);
3874 switch (sensor->info_priv.auto_focus)
3876 case SENSOR_AF_MODE_INFINITY:
3880 case SENSOR_AF_MODE_MACRO:
3882 if (focus_pos != 0x00)
3885 sensor_af_idlechk(client);
3886 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
3887 cmdinfo.cmd_para[0] = focus_pos;
3888 cmdinfo.validate_bit = 0x81;
3889 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
3892 case SENSOR_AF_MODE_AUTO:
3894 ret = sensor_af_single(client);
3897 case SENSOR_AF_MODE_CONTINUOUS:
3899 ret = sensor_af_const(client);
3902 case SENSOR_AF_MODE_CLOSE:
3909 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
3910 goto sensor_af_downfirmware_end;
3914 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
3917 sensor_af_downfirmware_end:
3921 static void sensor_af_workqueue(struct work_struct *work)
3923 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
3924 struct i2c_client *client = sensor_work->client;
3925 struct sensor *sensor = to_sensor(client);
3926 struct af_cmdinfo cmdinfo;
3928 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
3930 mutex_lock(&sensor->wq_lock);
3931 switch (sensor_work->cmd)
3935 if (sensor_af_downfirmware(client) < 0) {
3936 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
3940 case WqCmd_af_single:
3942 if (sensor_af_single(client) < 0) {
3943 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
3944 sensor_work->result = WqRet_fail;
3946 sensor_work->result = WqRet_success;
3950 case WqCmd_af_special_pos:
3952 sensor_af_idlechk(client);
3954 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
3955 cmdinfo.cmd_para[0] = sensor_work->var;
3956 cmdinfo.validate_bit = 0x81;
3957 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
3958 sensor_work->result = WqRet_fail;
3960 sensor_work->result = WqRet_success;
3963 case WqCmd_af_near_pos:
3965 sensor_af_idlechk(client);
3966 cmdinfo.cmd_tag = StepFocus_Near_Tag;
3967 cmdinfo.validate_bit = 0x80;
3968 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
3969 sensor_work->result = WqRet_fail;
3971 sensor_work->result = WqRet_success;
3974 case WqCmd_af_far_pos:
3976 sensor_af_idlechk(client);
3977 cmdinfo.cmd_tag = StepFocus_Far_Tag;
3978 cmdinfo.validate_bit = 0x80;
3979 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
3980 sensor_work->result = WqRet_fail;
3982 sensor_work->result = WqRet_success;
3985 case WqCmd_af_continues:
3987 if (sensor_af_const(client) < 0)
3988 sensor_work->result = WqRet_fail;
3990 sensor_work->result = WqRet_success;
3993 case WqCmd_af_update_zone:
3995 if (sensor_af_zoneupdate(client) < 0)
3996 sensor_work->result = WqRet_fail;
3998 sensor_work->result = WqRet_success;
4002 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
4006 if (sensor_work->wait == false) {
4007 kfree((void*)sensor_work);
4009 wake_up(&sensor_work->done);
4011 mutex_unlock(&sensor->wq_lock);
4015 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait)
4017 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4018 struct sensor *sensor = to_sensor(client);
4019 struct sensor_work *wk;
4022 if (sensor->sensor_wq == NULL) {
4024 goto sensor_af_workqueue_set_end;
4027 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
4029 wk->client = client;
4030 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
4032 wk->result = WqRet_inval;
4035 init_waitqueue_head(&wk->done);
4037 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
4039 /* ddl@rock-chips.com:
4040 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
4041 * As a result any other ioctl calls will proceed very, very slowly since each call
4042 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
4043 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
4046 mutex_unlock(&icd->video_lock);
4047 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(2500)) == 0) {
4048 SENSOR_TR("%s %s cmd(%d) is timeout!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4052 mutex_lock(&icd->video_lock);
4056 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4059 sensor_af_workqueue_set_end:
4063 static int sensor_parameter_record(struct i2c_client *client)
4065 u8 ret_l,ret_m,ret_h;
4067 struct sensor *sensor = to_sensor(client);
4069 sensor_write(client,0x3503,0x07); //stop AE/AG
4070 sensor_write(client,0x3406,0x01); //stop AWB
4072 sensor_read(client,0x3500,&ret_h);
4073 sensor_read(client,0x3501, &ret_m);
4074 sensor_read(client,0x3502, &ret_l);
4078 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
4079 sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
4080 sensor_read(client,0x350c, &ret_h);
4081 sensor_read(client,0x350d, &ret_l);
4082 sensor->parameter.preview_line_width = ret_h & 0xff;
4083 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
4084 //Read back AGC Gain for preview
4085 sensor_read(client,0x350b, &tp_l);
4086 sensor->parameter.preview_gain = tp_l;
4088 sensor->parameter.capture_framerate = 900;
4089 sensor->parameter.preview_framerate = 1500;
4091 SENSOR_DG(" %s Read 0x350c = 0x%02x 0x350d = 0x%02x 0x350b=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_l, sensor->parameter.preview_gain);
4094 static int sensor_ae_transfer(struct i2c_client *client)
4099 u16 ulCapture_Exposure;
4100 u32 ulCapture_Exposure_Gain;
4104 u8 reg_l = 0,reg_h =0;
4105 u16 Preview_Maxlines;
4107 u32 Capture_MaxLines;
4108 struct sensor *sensor = to_sensor(client);
4110 Preview_Maxlines = sensor->parameter.preview_line_width;
4111 Gain = sensor->parameter.preview_gain;
4112 sensor_read(client,0x350c, ®_h);
4113 sensor_read(client,0x350d, ®_l);
4114 Capture_MaxLines = reg_h & 0xff;
4115 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
4118 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
4120 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
4123 if(Preview_Maxlines == 0)
4124 Preview_Maxlines = 1;
4126 ulCapture_Exposure =
4127 (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
4128 iCapture_Gain = (Gain & 0x0f) + 16;
4130 iCapture_Gain = iCapture_Gain << 1;
4133 iCapture_Gain = iCapture_Gain << 1;
4136 iCapture_Gain = iCapture_Gain << 1;
4139 iCapture_Gain = iCapture_Gain << 1;
4141 ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
4142 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
4143 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
4144 if (ulCapture_Exposure > Lines_10ms)
4146 //ulCapture_Exposure *= 1.7;
4147 ulCapture_Exposure /= Lines_10ms;
4148 ulCapture_Exposure *= Lines_10ms;
4151 ulCapture_Exposure = Capture_MaxLines;
4152 //ulCapture_Exposure_Gain *= 1.5;
4154 if(ulCapture_Exposure == 0)
4155 ulCapture_Exposure = 1;
4156 iCapture_Gain = (ulCapture_Exposure_Gain*2/ulCapture_Exposure + 1)/2;
4157 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
4158 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
4159 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
4162 if (iCapture_Gain > 31) {
4164 iCapture_Gain = iCapture_Gain >> 1;
4166 if (iCapture_Gain > 31) {
4168 iCapture_Gain = iCapture_Gain >> 1;
4170 if (iCapture_Gain > 31) {
4172 iCapture_Gain = iCapture_Gain >> 1;
4174 if (iCapture_Gain > 31) {
4176 iCapture_Gain = iCapture_Gain >> 1;
4178 if (iCapture_Gain > 16)
4179 Gain |= ((iCapture_Gain -16) & 0x0f);
4182 // write the gain and exposure to 0x350* registers
4183 //m_iWrite0x350b=Gain;
4184 sensor_write(client,0x350b, Gain);
4185 //m_iWrite0x3502=ExposureLow;
4186 sensor_write(client,0x3502, ExposureLow);
4187 //m_iWrite0x3501=ExposureMid;
4188 sensor_write(client,0x3501, ExposureMid);
4189 //m_iWrite0x3500=ExposureHigh;
4190 sensor_write(client,0x3500, ExposureHigh);
4191 // SendToFile("Gain = 0x%x\r\n", Gain);
4192 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
4193 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
4194 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
4195 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
4196 //camera_timed_wait(200);
4197 //linzhk camera_timed_wait(500);
4199 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
4203 static int sensor_ioctrl(struct soc_camera_device *icd,enum rksensor_power_cmd cmd, int on)
4205 struct soc_camera_link *icl = to_soc_camera_link(icd);
4208 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
4212 case Sensor_PowerDown:
4214 if (icl->powerdown) {
4215 ret = icl->powerdown(icd->pdev, on);
4216 if (ret == RK_CAM_IO_SUCCESS) {
4220 icl->reset(icd->pdev);
4222 } else if (ret == RK_CAM_EIO_REQUESTFAIL) {
4224 goto sensor_power_end;
4231 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4232 struct sensor *sensor = to_sensor(client);
4234 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
4235 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
4241 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
4249 static int sensor_init(struct v4l2_subdev *sd, u32 val)
4251 struct i2c_client *client = v4l2_get_subdevdata(sd);
4252 struct soc_camera_device *icd = client->dev.platform_data;
4253 struct sensor *sensor = to_sensor(client);
4254 const struct v4l2_queryctrl *qctrl;
4255 const struct sensor_datafmt *fmt;
4257 int ret,pid = 0,i = 0,j=0;
4258 struct rkcamera_platform_data* tmp_plat_data =(struct rkcamera_platform_data*)val;
4259 sensor_init_data_p = sensor_init_data;
4260 sensor_init_winseq_p = sensor_svga;
4261 sensor_init_width = 800;
4262 sensor_init_height = 600;
4263 if (tmp_plat_data != NULL) {
4264 for(i = 0;i < RK_CAM_NUM_PER_HOST;i++){
4265 if ((tmp_plat_data->sensor_init_data[i])&& tmp_plat_data->info[i].dev_name &&
4266 (strcmp(tmp_plat_data->info[i].dev_name, dev_name(icd->pdev)) == 0))
4270 if(tmp_plat_data && (i < RK_CAM_NUM_PER_HOST) && tmp_plat_data->sensor_init_data[i]){
4271 //user has defined the init data
4273 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data && (sizeof(struct reginfo) != sizeof(struct reginfo_t))){
4274 for(j = 0;j< sizeof(sensor_init_data)/sizeof(struct reginfo);j++){
4275 sensor_init_data[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].reg;
4276 sensor_init_data[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].val;
4278 sensor_init_data_p = sensor_init_data;
4280 else if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data){
4281 sensor_init_data_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data);
4284 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq && (sizeof(struct reginfo) != sizeof(struct reginfo_t))){
4285 int tmp_winseq_size = tmp_plat_data->sensor_init_data[i]->rk_sensor_winseq_size;
4286 if(sensor_init_winseq_board)
4288 vfree(sensor_init_winseq_board);
4289 sensor_init_winseq_board = NULL;
4291 sensor_init_winseq_board = (struct reginfo*)vmalloc(tmp_winseq_size);
4292 if(!sensor_init_winseq_board)
4293 SENSOR_TR("%s :vmalloc erro !",__FUNCTION__);
4294 for(j = 0;j< tmp_winseq_size;j++){
4295 sensor_init_winseq_board[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].reg;
4296 sensor_init_winseq_board[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].val;
4298 sensor_init_winseq_p = sensor_init_winseq_board;
4300 else if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq){
4301 sensor_init_winseq_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq);
4303 //init width,height,bus,pixelcode
4304 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width != INVALID_VALUE)
4305 sensor_init_width = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width;
4306 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height != INVALID_VALUE)
4307 sensor_init_height = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height;
4308 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param != INVALID_VALUE)
4309 sensor_init_busparam = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param;
4310 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode != INVALID_VALUE)
4311 sensor_init_pixelcode = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode;
4313 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
4315 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
4317 goto sensor_INIT_ERR;
4321 if (sensor_task_lock(client,1)<0)
4322 goto sensor_INIT_ERR;
4323 ret = sensor_write(client, 0x3008, 0x80);
4325 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
4327 goto sensor_INIT_ERR;
4330 mdelay(5); //delay 5 microseconds
4331 /* check if it is an sensor sensor */
4332 ret = sensor_read(client, 0x300a, &value);
4334 SENSOR_TR("read chip id high byte failed\n");
4336 goto sensor_INIT_ERR;
4339 pid |= (value << 8);
4341 ret = sensor_read(client, 0x300b, &value);
4343 SENSOR_TR("read chip id low byte failed\n");
4345 goto sensor_INIT_ERR;
4348 pid |= (value & 0xff);
4349 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
4351 if (pid == SENSOR_ID) {
4352 sensor->model = SENSOR_V4L2_IDENT;
4354 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
4356 goto sensor_INIT_ERR;
4359 ret = sensor_write_array(client, sensor_init_data_p);
4362 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
4363 goto sensor_INIT_ERR;
4365 sensor_task_lock(client,0);
4366 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
4367 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
4369 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
4371 goto sensor_INIT_ERR;
4373 sensor->info_priv.fmt = *fmt;
4375 /* sensor sensor information for initialization */
4376 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4378 sensor->info_priv.whiteBalance = qctrl->default_value;
4379 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
4381 sensor->info_priv.brightness = qctrl->default_value;
4382 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4384 sensor->info_priv.effect = qctrl->default_value;
4385 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
4387 sensor->info_priv.exposure = qctrl->default_value;
4389 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
4391 sensor->info_priv.saturation = qctrl->default_value;
4392 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
4394 sensor->info_priv.contrast = qctrl->default_value;
4395 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
4397 sensor->info_priv.mirror = qctrl->default_value;
4398 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
4400 sensor->info_priv.flip = qctrl->default_value;
4401 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
4403 sensor->info_priv.scene = qctrl->default_value;
4404 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
4406 sensor->info_priv.digitalzoom = qctrl->default_value;
4408 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
4409 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
4411 sensor->info_priv.focus = qctrl->default_value;
4413 #if CONFIG_SENSOR_Flash
4414 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
4416 sensor->info_priv.flash = qctrl->default_value;
4418 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);
4420 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
4424 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
4425 sensor_task_lock(client,0);
4426 sensor_deactivate(client);
4429 static int sensor_deactivate(struct i2c_client *client)
4431 struct soc_camera_device *icd = client->dev.platform_data;
4432 struct sensor *sensor = to_sensor(client);
4434 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
4436 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
4437 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
4438 sensor_task_lock(client, 1);
4439 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
4440 sensor_write(client, 0x3018, 0x03); // D5-D0
4441 sensor_write(client,0x3019,0x00); // STROBE,SDA
4442 sensor_task_lock(client, 0);
4444 sensor_ioctrl(icd, Sensor_PowerDown, 1);
4447 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
4448 icd->user_width = SENSOR_INIT_WIDTH;
4449 icd->user_height = SENSOR_INIT_HEIGHT;
4450 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
4453 static struct reginfo sensor_power_down_sequence[]=
4457 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
4460 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4462 if (pm_msg.event == PM_EVENT_SUSPEND) {
4463 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
4464 ret = sensor_write_array(client, sensor_power_down_sequence) ;
4466 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
4469 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
4471 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
4476 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
4483 static int sensor_resume(struct soc_camera_device *icd)
4487 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
4489 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
4493 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
4497 static int sensor_set_bus_param(struct soc_camera_device *icd,
4498 unsigned long flags)
4504 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
4506 struct soc_camera_link *icl = to_soc_camera_link(icd);
4507 unsigned long flags = SENSOR_BUS_PARAM;
4509 return soc_camera_apply_sensor_flags(icl, flags);
4511 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4513 struct i2c_client *client = v4l2_get_subdevdata(sd);
4514 struct soc_camera_device *icd = client->dev.platform_data;
4515 struct sensor *sensor = to_sensor(client);
4517 mf->width = icd->user_width;
4518 mf->height = icd->user_height;
4519 mf->code = sensor->info_priv.fmt.code;
4520 mf->colorspace = sensor->info_priv.fmt.colorspace;
4521 mf->field = V4L2_FIELD_NONE;
4525 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4529 if ((mf->width == 1024) && (mf->height == 768)) {
4531 } else if ((mf->width == 1280) && (mf->height == 1024)) {
4533 } else if ((mf->width == 1600) && (mf->height == 1200)) {
4535 } else if ((mf->width == 2048) && (mf->height == 1536)) {
4537 } else if ((mf->width == 2592) && (mf->height == 1944)) {
4542 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
4546 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4550 if ((mf->width == 1280) && (mf->height == 720)) {
4552 } else if ((mf->width == 1920) && (mf->height == 1080)) {
4557 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
4560 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4562 struct i2c_client *client = v4l2_get_subdevdata(sd);
4563 const struct sensor_datafmt *fmt;
4564 struct sensor *sensor = to_sensor(client);
4565 const struct v4l2_queryctrl *qctrl;
4566 struct soc_camera_device *icd = client->dev.platform_data;
4567 struct reginfo *winseqe_set_addr=NULL;
4568 int ret = 0, set_w,set_h;
4570 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
4571 ARRAY_SIZE(sensor_colour_fmts));
4574 goto sensor_s_fmt_end;
4577 if (sensor->info_priv.fmt.code != mf->code) {
4580 case V4L2_MBUS_FMT_YUYV8_2X8:
4582 winseqe_set_addr = sensor_ClrFmt_YUYV;
4585 case V4L2_MBUS_FMT_UYVY8_2X8:
4587 winseqe_set_addr = sensor_ClrFmt_UYVY;
4593 if (winseqe_set_addr != NULL) {
4594 sensor_write_array(client, winseqe_set_addr);
4595 sensor->info_priv.fmt.code = mf->code;
4596 sensor->info_priv.fmt.colorspace= mf->colorspace;
4597 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
4599 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
4606 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
4608 winseqe_set_addr = sensor_qcif;
4612 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
4614 winseqe_set_addr = sensor_qvga;
4618 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
4620 winseqe_set_addr = sensor_cif;
4624 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
4626 winseqe_set_addr = sensor_vga;
4630 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
4632 winseqe_set_addr = sensor_svga;
4636 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
4638 winseqe_set_addr = sensor_xga;
4642 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
4644 winseqe_set_addr = sensor_720p;
4648 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
4650 winseqe_set_addr = sensor_sxga;
4654 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
4656 winseqe_set_addr = sensor_uxga;
4660 else if (((set_w <= 1920) && (set_h <= 1080)) && sensor_1080p[0].reg)
4662 winseqe_set_addr = sensor_1080p;
4666 else if (((set_w <= 2048) && (set_h <= 1536)) && sensor_qxga[0].reg)
4668 winseqe_set_addr = sensor_qxga;
4672 else if (((set_w <= 2592) && (set_h <= 1944)) && sensor_qsxga[0].reg)
4674 winseqe_set_addr = sensor_qsxga;
4680 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
4681 set_w = SENSOR_INIT_WIDTH;
4682 set_h = SENSOR_INIT_HEIGHT;
4683 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
4686 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
4688 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
4689 sensor_parameter_record(client);
4690 #if CONFIG_SENSOR_Flash
4691 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4692 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
4693 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
4696 }else { /* ddl@rock-chips.com : Video */
4697 #if CONFIG_SENSOR_Flash
4698 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4699 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
4700 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
4705 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
4706 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
4707 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
4708 ret |= sensor_write_array(client,sensor_init_data);
4709 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
4713 ret |= sensor_write_array(client, winseqe_set_addr);
4715 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
4716 #if CONFIG_SENSOR_Flash
4717 if (sensor_fmt_capturechk(sd,mf) == true) {
4718 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4719 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
4720 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
4724 goto sensor_s_fmt_end;
4727 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
4729 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
4730 sensor_ae_transfer(client);
4731 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4732 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4733 if (sensor->info_priv.whiteBalance != 0) {
4734 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4735 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
4737 #if CONFIG_SENSOR_Focus
4738 if (sensor->info_priv.auto_focus != SENSOR_AF_MODE_INFINITY) {
4739 if (sensor_af_workqueue_set(icd, WqCmd_af_update_zone,0,true) == 0) {
4740 if ((sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) ||
4741 (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)) {
4743 sensor_af_workqueue_set(icd, WqCmd_af_single,0,false);
4748 sensor->info_priv.snap2preview = true;
4749 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
4750 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4751 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4752 #if CONFIG_SENSOR_Focus
4753 sensor->info_priv.affm_reinit = 1;
4755 sensor->info_priv.video2preview = true;
4756 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
4757 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4758 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4759 if (sensor->info_priv.snap2preview == true) {
4760 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4761 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
4763 #if CONFIG_SENSOR_Focus
4764 if (sensor->info_priv.auto_focus != SENSOR_AF_MODE_INFINITY) {
4765 /* ddl@rock-chips.com: The af operation is not necessary, if user don't care whether in focus after preview*/
4766 if (sensor_af_workqueue_set(icd, WqCmd_af_update_zone,0,true) == 0) {
4767 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS) {
4768 sensor_af_workqueue_set(icd, WqCmd_af_continues,0,false);
4769 } else if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
4771 sensor_af_workqueue_set(icd, WqCmd_af_single,0,false);
4776 msleep(600); /* ddl@rock-chips.com : whitebalance auto must delay */
4779 sensor->info_priv.video2preview = false;
4780 sensor->info_priv.snap2preview = false;
4785 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
4789 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
4798 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4800 struct i2c_client *client = v4l2_get_subdevdata(sd);
4801 struct sensor *sensor = to_sensor(client);
4802 const struct sensor_datafmt *fmt;
4805 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
4806 ARRAY_SIZE(sensor_colour_fmts));
4808 fmt = &sensor->info_priv.fmt;
4809 mf->code = fmt->code;
4812 if (mf->height > SENSOR_MAX_HEIGHT)
4813 mf->height = SENSOR_MAX_HEIGHT;
4814 else if (mf->height < SENSOR_MIN_HEIGHT)
4815 mf->height = SENSOR_MIN_HEIGHT;
4817 if (mf->width > SENSOR_MAX_WIDTH)
4818 mf->width = SENSOR_MAX_WIDTH;
4819 else if (mf->width < SENSOR_MIN_WIDTH)
4820 mf->width = SENSOR_MIN_WIDTH;
4822 mf->colorspace = fmt->colorspace;
4827 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
4829 struct i2c_client *client = v4l2_get_subdevdata(sd);
4831 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
4834 if (id->match.addr != client->addr)
4837 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
4842 #if CONFIG_SENSOR_Brightness
4843 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4845 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4847 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4849 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
4851 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
4853 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4856 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4860 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4864 #if CONFIG_SENSOR_Effect
4865 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4867 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4869 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4871 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
4873 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
4875 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4878 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4882 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4886 #if CONFIG_SENSOR_Exposure
4887 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4889 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4891 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4893 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
4895 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
4897 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4900 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4904 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4908 #if CONFIG_SENSOR_Saturation
4909 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4911 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4913 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4915 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
4917 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
4919 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4922 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4926 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4930 #if CONFIG_SENSOR_Contrast
4931 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4933 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4935 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4937 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
4939 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
4941 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4944 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4948 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4952 #if CONFIG_SENSOR_Mirror
4953 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4955 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4957 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4959 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
4961 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
4963 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4966 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4970 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4974 #if CONFIG_SENSOR_Flip
4975 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4977 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4979 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4981 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
4983 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
4985 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4988 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4992 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4996 #if CONFIG_SENSOR_Scene
4997 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4999 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5001 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
5003 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
5005 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
5007 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5010 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5014 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5018 #if CONFIG_SENSOR_WhiteBalance
5019 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5021 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5023 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
5025 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
5027 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
5029 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5032 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5036 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5040 #if CONFIG_SENSOR_DigitalZoom
5041 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
5043 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5044 struct sensor *sensor = to_sensor(client);
5045 const struct v4l2_queryctrl *qctrl_info;
5046 int digitalzoom_cur, digitalzoom_total;
5048 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
5052 digitalzoom_cur = sensor->info_priv.digitalzoom;
5053 digitalzoom_total = qctrl_info->maximum;
5055 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
5057 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
5061 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
5063 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
5067 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
5069 *value = digitalzoom_total - digitalzoom_cur;
5072 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
5074 *value = 0 - digitalzoom_cur;
5077 digitalzoom_cur += *value;
5079 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
5081 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
5083 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5086 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
5093 #if CONFIG_SENSOR_Focus
5094 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5096 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5097 struct sensor *sensor = to_sensor(client);
5098 const struct v4l2_queryctrl *qctrl_info;
5101 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
5105 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
5106 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
5107 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true);
5108 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
5111 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5115 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
5116 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
5121 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5123 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5124 struct sensor *sensor = to_sensor(client);
5125 const struct v4l2_queryctrl *qctrl_info;
5128 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
5132 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
5133 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
5135 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true);
5137 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true);
5139 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
5142 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5146 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
5147 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
5152 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5154 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5155 struct sensor *sensor = to_sensor(client);
5158 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
5161 case SENSOR_AF_MODE_AUTO:
5163 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true);
5167 case SENSOR_AF_MODE_MACRO:
5169 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
5173 case SENSOR_AF_MODE_INFINITY:
5175 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
5179 case SENSOR_AF_MODE_CONTINUOUS:
5181 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true);
5185 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5190 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
5193 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
5194 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
5201 #if CONFIG_SENSOR_Flash
5202 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5204 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
5205 if (value == 3) { /* ddl@rock-chips.com: torch */
5206 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
5208 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
5210 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5214 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5219 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
5221 struct i2c_client *client = v4l2_get_subdevdata(sd);
5222 struct sensor *sensor = to_sensor(client);
5223 const struct v4l2_queryctrl *qctrl;
5225 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
5229 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
5235 case V4L2_CID_BRIGHTNESS:
5237 ctrl->value = sensor->info_priv.brightness;
5240 case V4L2_CID_SATURATION:
5242 ctrl->value = sensor->info_priv.saturation;
5245 case V4L2_CID_CONTRAST:
5247 ctrl->value = sensor->info_priv.contrast;
5250 case V4L2_CID_DO_WHITE_BALANCE:
5252 ctrl->value = sensor->info_priv.whiteBalance;
5255 case V4L2_CID_EXPOSURE:
5257 ctrl->value = sensor->info_priv.exposure;
5260 case V4L2_CID_HFLIP:
5262 ctrl->value = sensor->info_priv.mirror;
5265 case V4L2_CID_VFLIP:
5267 ctrl->value = sensor->info_priv.flip;
5278 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
5280 struct i2c_client *client = v4l2_get_subdevdata(sd);
5281 struct sensor *sensor = to_sensor(client);
5282 struct soc_camera_device *icd = client->dev.platform_data;
5283 const struct v4l2_queryctrl *qctrl;
5286 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
5290 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
5296 #if CONFIG_SENSOR_Brightness
5297 case V4L2_CID_BRIGHTNESS:
5299 if (ctrl->value != sensor->info_priv.brightness)
5301 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
5305 sensor->info_priv.brightness = ctrl->value;
5310 #if CONFIG_SENSOR_Exposure
5311 case V4L2_CID_EXPOSURE:
5313 if (ctrl->value != sensor->info_priv.exposure)
5315 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
5319 sensor->info_priv.exposure = ctrl->value;
5324 #if CONFIG_SENSOR_Saturation
5325 case V4L2_CID_SATURATION:
5327 if (ctrl->value != sensor->info_priv.saturation)
5329 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
5333 sensor->info_priv.saturation = ctrl->value;
5338 #if CONFIG_SENSOR_Contrast
5339 case V4L2_CID_CONTRAST:
5341 if (ctrl->value != sensor->info_priv.contrast)
5343 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
5347 sensor->info_priv.contrast = ctrl->value;
5352 #if CONFIG_SENSOR_WhiteBalance
5353 case V4L2_CID_DO_WHITE_BALANCE:
5355 if (ctrl->value != sensor->info_priv.whiteBalance)
5357 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
5361 sensor->info_priv.whiteBalance = ctrl->value;
5366 #if CONFIG_SENSOR_Mirror
5367 case V4L2_CID_HFLIP:
5369 if (ctrl->value != sensor->info_priv.mirror)
5371 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
5373 sensor->info_priv.mirror = ctrl->value;
5378 #if CONFIG_SENSOR_Flip
5379 case V4L2_CID_VFLIP:
5381 if (ctrl->value != sensor->info_priv.flip)
5383 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
5385 sensor->info_priv.flip = ctrl->value;
5396 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
5398 const struct v4l2_queryctrl *qctrl;
5399 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5400 struct sensor *sensor = to_sensor(client);
5402 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
5406 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
5410 switch (ext_ctrl->id)
5412 case V4L2_CID_SCENE:
5414 ext_ctrl->value = sensor->info_priv.scene;
5417 case V4L2_CID_EFFECT:
5419 ext_ctrl->value = sensor->info_priv.effect;
5422 case V4L2_CID_ZOOM_ABSOLUTE:
5424 ext_ctrl->value = sensor->info_priv.digitalzoom;
5427 case V4L2_CID_ZOOM_RELATIVE:
5431 case V4L2_CID_FOCUS_ABSOLUTE:
5435 case V4L2_CID_FOCUS_RELATIVE:
5439 case V4L2_CID_FLASH:
5441 ext_ctrl->value = sensor->info_priv.flash;
5449 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
5451 const struct v4l2_queryctrl *qctrl;
5452 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5453 struct sensor *sensor = to_sensor(client);
5456 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
5460 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
5465 switch (ext_ctrl->id)
5467 #if CONFIG_SENSOR_Scene
5468 case V4L2_CID_SCENE:
5470 if (ext_ctrl->value != sensor->info_priv.scene)
5472 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
5474 sensor->info_priv.scene = ext_ctrl->value;
5479 #if CONFIG_SENSOR_Effect
5480 case V4L2_CID_EFFECT:
5482 if (ext_ctrl->value != sensor->info_priv.effect)
5484 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
5486 sensor->info_priv.effect= ext_ctrl->value;
5491 #if CONFIG_SENSOR_DigitalZoom
5492 case V4L2_CID_ZOOM_ABSOLUTE:
5494 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5497 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
5499 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
5501 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
5503 sensor->info_priv.digitalzoom += val_offset;
5505 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
5510 case V4L2_CID_ZOOM_RELATIVE:
5512 if (ext_ctrl->value)
5514 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
5516 sensor->info_priv.digitalzoom += ext_ctrl->value;
5518 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
5523 #if CONFIG_SENSOR_Focus
5524 case V4L2_CID_FOCUS_ABSOLUTE:
5526 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5529 ret = sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value);
5530 if ((ret == 0) || (0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK))) {
5531 if (ext_ctrl->value == qctrl->minimum) {
5532 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
5533 } else if (ext_ctrl->value == qctrl->maximum) {
5534 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
5536 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
5542 case V4L2_CID_FOCUS_RELATIVE:
5544 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5547 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
5550 case V4L2_CID_FOCUS_AUTO:
5552 if (ext_ctrl->value == 1) {
5553 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
5554 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
5555 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
5559 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
5560 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
5561 if (ext_ctrl->value == 0)
5562 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
5566 case V4L2_CID_FOCUS_CONTINUOUS:
5568 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
5569 if (ext_ctrl->value == 1) {
5570 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0) {
5571 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
5572 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
5576 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
5579 if (ext_ctrl->value == 0)
5580 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
5585 #if CONFIG_SENSOR_Flash
5586 case V4L2_CID_FLASH:
5588 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
5590 sensor->info_priv.flash = ext_ctrl->value;
5592 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
5603 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
5605 struct i2c_client *client = v4l2_get_subdevdata(sd);
5606 struct soc_camera_device *icd = client->dev.platform_data;
5607 int i, error_cnt=0, error_idx=-1;
5610 for (i=0; i<ext_ctrl->count; i++) {
5611 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
5618 error_idx = ext_ctrl->count;
5620 if (error_idx != -1) {
5621 ext_ctrl->error_idx = error_idx;
5628 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
5630 struct i2c_client *client = v4l2_get_subdevdata(sd);
5631 struct soc_camera_device *icd = client->dev.platform_data;
5632 int i, error_cnt=0, error_idx=-1;
5634 for (i=0; i<ext_ctrl->count; i++) {
5635 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
5642 error_idx = ext_ctrl->count;
5644 if (error_idx != -1) {
5645 ext_ctrl->error_idx = error_idx;
5652 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
5654 struct i2c_client *client = v4l2_get_subdevdata(sd);
5655 struct sensor *sensor = to_sensor(client);
5656 #if CONFIG_SENSOR_Focus
5657 struct soc_camera_device *icd = client->dev.platform_data;
5658 struct v4l2_mbus_framefmt mf;
5662 sensor->info_priv.enable = 1;
5663 #if CONFIG_SENSOR_Focus
5664 mf.width = icd->user_width;
5665 mf.height = icd->user_height;
5666 mf.code = sensor->info_priv.fmt.code;
5667 mf.colorspace = sensor->info_priv.fmt.colorspace;
5668 mf.field = V4L2_FIELD_NONE;
5669 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
5670 if (sensor_fmt_capturechk(sd, &mf) == false) {
5671 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
5672 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false);
5673 sensor->info_priv.affm_reinit = 0;
5677 } else if (enable == 0) {
5678 sensor->info_priv.enable = 0;
5679 #if CONFIG_SENSOR_Focus
5680 flush_workqueue(sensor->sensor_wq);
5686 /* Interface active, can use i2c. If it fails, it can indeed mean, that
5687 * this wasn't our capture interface, so, we wait for the right one */
5688 static int sensor_video_probe(struct soc_camera_device *icd,
5689 struct i2c_client *client)
5693 struct sensor *sensor = to_sensor(client);
5695 /* We must have a parent by now. And it cannot be a wrong one.
5696 * So this entire test is completely redundant. */
5697 if (!icd->dev.parent ||
5698 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
5701 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
5703 goto sensor_video_probe_err;
5706 ret = sensor_write(client, 0x3012, 0x80);
5708 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
5710 goto sensor_video_probe_err;
5712 mdelay(5); //delay 5 microseconds
5714 /* check if it is an sensor sensor */
5715 ret = sensor_read(client, 0x300a, &value);
5717 SENSOR_TR("read chip id high byte failed\n");
5719 goto sensor_video_probe_err;
5722 pid |= (value << 8);
5724 ret = sensor_read(client, 0x300b, &value);
5726 SENSOR_TR("read chip id low byte failed\n");
5728 goto sensor_video_probe_err;
5731 pid |= (value & 0xff);
5732 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
5733 if (pid == SENSOR_ID) {
5734 sensor->model = SENSOR_V4L2_IDENT;
5736 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
5738 goto sensor_video_probe_err;
5743 sensor_video_probe_err:
5747 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
5749 struct i2c_client *client = v4l2_get_subdevdata(sd);
5750 struct soc_camera_device *icd = client->dev.platform_data;
5751 struct sensor *sensor = to_sensor(client);
5754 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
5757 case RK_CAM_SUBDEV_DEACTIVATE:
5759 sensor_deactivate(client);
5762 case RK_CAM_SUBDEV_IOREQUEST:
5764 sensor->sensor_io_request = (struct rkcamera_platform_data*)arg;
5766 if (sensor->sensor_io_request != NULL) {
5767 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
5768 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
5769 sensor->sensor_gpio_res = (struct rkcamera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
5770 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
5771 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
5772 sensor->sensor_gpio_res = (struct rkcamera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
5775 SENSOR_TR("%s %s RK_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
5777 goto sensor_ioctl_end;
5780 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
5782 #if CONFIG_SENSOR_Flash
5783 if (sensor->sensor_gpio_res) {
5784 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
5785 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
5786 for (i = 0; i < icd->ops->num_controls; i++) {
5787 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
5788 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
5791 sensor->info_priv.flash = 0xff;
5792 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
5800 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
5809 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
5810 enum v4l2_mbus_pixelcode *code)
5812 if (index >= ARRAY_SIZE(sensor_colour_fmts))
5815 *code = sensor_colour_fmts[index].code;
5818 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
5819 .init = sensor_init,
5820 .g_ctrl = sensor_g_control,
5821 .s_ctrl = sensor_s_control,
5822 .g_ext_ctrls = sensor_g_ext_controls,
5823 .s_ext_ctrls = sensor_s_ext_controls,
5824 .g_chip_ident = sensor_g_chip_ident,
5825 .ioctl = sensor_ioctl,
5828 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
5829 .s_mbus_fmt = sensor_s_fmt,
5830 .g_mbus_fmt = sensor_g_fmt,
5831 .try_mbus_fmt = sensor_try_fmt,
5832 .enum_mbus_fmt = sensor_enum_fmt,
5833 .s_stream = sensor_s_stream,
5835 static struct v4l2_subdev_ops sensor_subdev_ops = {
5836 .core = &sensor_subdev_core_ops,
5837 .video = &sensor_subdev_video_ops,
5840 static int sensor_probe(struct i2c_client *client,
5841 const struct i2c_device_id *did)
5843 struct sensor *sensor;
5844 struct soc_camera_device *icd = client->dev.platform_data;
5845 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
5846 struct soc_camera_link *icl;
5849 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
5851 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
5855 icl = to_soc_camera_link(icd);
5857 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
5861 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
5862 dev_warn(&adapter->dev,
5863 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
5867 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
5871 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
5873 /* Second stage probe - when a capture adapter is there */
5874 icd->ops = &sensor_ops;
5875 sensor->info_priv.fmt = sensor_colour_fmts[0];
5876 #if CONFIG_SENSOR_I2C_NOSCHED
5877 atomic_set(&sensor->tasklock_cnt,0);
5880 ret = sensor_video_probe(icd, client);
5883 i2c_set_clientdata(client, NULL);
5887 #if CONFIG_SENSOR_Focus
5888 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
5889 if (sensor->sensor_wq == NULL)
5890 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
5891 mutex_init(&sensor->wq_lock);
5895 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
5899 static int sensor_remove(struct i2c_client *client)
5901 struct sensor *sensor = to_sensor(client);
5902 struct soc_camera_device *icd = client->dev.platform_data;
5904 #if CONFIG_SENSOR_Focus
5905 if (sensor->sensor_wq) {
5906 destroy_workqueue(sensor->sensor_wq);
5907 sensor->sensor_wq = NULL;
5912 i2c_set_clientdata(client, NULL);
5913 client->driver = NULL;
5919 static const struct i2c_device_id sensor_id[] = {
5920 {SENSOR_NAME_STRING(), 0 },
5923 MODULE_DEVICE_TABLE(i2c, sensor_id);
5925 static struct i2c_driver sensor_i2c_driver = {
5927 .name = SENSOR_NAME_STRING(),
5929 .probe = sensor_probe,
5930 .remove = sensor_remove,
5931 .id_table = sensor_id,
5934 static int __init sensor_mod_init(void)
5936 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
5937 return i2c_add_driver(&sensor_i2c_driver);
5940 static void __exit sensor_mod_exit(void)
5942 i2c_del_driver(&sensor_i2c_driver);
5945 device_initcall_sync(sensor_mod_init);
5946 module_exit(sensor_mod_exit);
5948 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
5949 MODULE_AUTHOR("ddl <kernel@rock-chips>");
5950 MODULE_LICENSE("GPL");