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 <plat/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 RK29_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
166 //flash off in fixed time to prevent from too hot , zyc
168 struct soc_camera_device *icd;
169 struct hrtimer timer;
171 static enum hrtimer_restart flash_off_func(struct hrtimer *timer);
173 static struct flash_timer flash_off_timer;
174 //for user defined if user want to customize the series , zyc
175 #if CONFIG_OV5642_USER_DEFINED_SERIES
176 #include "ov5642_user_series.c"
178 /* init 800X600 SVGA */
179 static struct reginfo sensor_init_data[] =
249 {0x4300 , 0x32}, //UYVY
852 /* 720p 15fps @ 1280x720 */
854 static struct reginfo sensor_720p[]=
857 {SEQUENCE_PROPERTY, SEQUENCE_INIT},
1318 {0x3011, 0x10 },//15fps 0x08 // 10fps: 0x05
1371 {0x4300, 0x32 }, // 0x30---> 0x32, set yuv
1376 //{0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
1457 //{0x3008,0x02}, //software wake
1463 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
1465 static struct reginfo sensor_1080p[]=
1468 {SEQUENCE_PROPERTY, SEQUENCE_INIT},
1481 { 0x3011, 0x05 }, //10 fps 0x05 //15fps 0x08
1929 { 0x3011, 0x07 }, //10 fps 0x05 //15fps 0x07
1937 { 0x3807, 0x39 },//0x3a
1975 { 0x4300, 0x32 }, // set yuv, 0x30--> 0x32
1980 //{0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
1995 {0x3622,0x00},//60},
1997 {0x3604,0x40},//48},
2026 { 0x3011, 0x07 }, //10 fps 0x05 //15fps 0x08
2029 {0x3a08,0x14}, //0x05
2030 {0x3a09,0xe0}, //0xf5
2031 {0x3a0a,0x11}, //0x04
2032 {0x3a0b,0x60}, //0xf6
2033 {0x3a0d,0x04}, //0x0b
2034 {0x3a0e,0x03}, //0x0d
2060 //{0x3008,0x02}, //software wake
2065 /* 2592X1944 QSXGA */
2066 static struct reginfo sensor_qsxga[] =
2068 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2136 {0x3008,0x02}, //software wake
2139 /* 2048*1536 QXGA */
2140 static struct reginfo sensor_qxga[] =
2142 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2236 {0x3008,0x02}, //software wake
2240 /* 1600X1200 UXGA */
2241 static struct reginfo sensor_uxga[] =
2243 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2337 {0x3008,0x02}, //software wake
2340 /* 1280X1024 SXGA */
2341 static struct reginfo sensor_sxga[] =
2346 static struct reginfo sensor_xga[] =
2348 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2442 {0x3008,0x02}, //software wake
2447 static struct reginfo sensor_svga[] =
2537 static struct reginfo sensor_vga[] =
2543 static struct reginfo sensor_cif[] =
2574 static struct reginfo sensor_qvga[] =
2580 static struct reginfo sensor_qcif[] =
2612 static struct reginfo ov2655_qqvga[] =
2656 static struct reginfo ov2655_Sharpness_auto[] =
2661 static struct reginfo ov2655_Sharpness1[] =
2667 static struct reginfo ov2655_Sharpness2[][3] =
2674 static struct reginfo ov2655_Sharpness3[] =
2680 static struct reginfo ov2655_Sharpness4[]=
2687 static struct reginfo ov2655_Sharpness5[] =
2695 static struct reginfo sensor_ClrFmt_YUYV[]=
2701 static struct reginfo sensor_ClrFmt_UYVY[]=
2708 #if CONFIG_SENSOR_WhiteBalance
2709 static struct reginfo sensor_WhiteB_Auto[]=
2731 /* Cloudy Colour Temperature : 6500K - 8000K */
2732 static struct reginfo sensor_WhiteB_Cloudy[]=
2743 /* ClearDay Colour Temperature : 5000K - 6500K */
2744 static struct reginfo sensor_WhiteB_ClearDay[]=
2756 /* Office Colour Temperature : 3500K - 5000K */
2757 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
2770 /* Home Colour Temperature : 2500K - 3500K */
2771 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
2783 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
2784 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
2788 #if CONFIG_SENSOR_Brightness
2789 static struct reginfo sensor_Brightness0[]=
2794 static struct reginfo sensor_Brightness1[]=
2799 static struct reginfo sensor_Brightness2[]=
2804 static struct reginfo sensor_Brightness3[]=
2809 static struct reginfo sensor_Brightness4[]=
2814 static struct reginfo sensor_Brightness5[]=
2818 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
2819 sensor_Brightness4, sensor_Brightness5,NULL,
2824 #if CONFIG_SENSOR_Effect
2825 static struct reginfo sensor_Effect_Normal[] =
2832 static struct reginfo sensor_Effect_WandB[] =
2841 static struct reginfo sensor_Effect_Sepia[] =
2850 static struct reginfo sensor_Effect_Negative[] =
2857 static struct reginfo sensor_Effect_Bluish[] =
2867 static struct reginfo sensor_Effect_Green[] =
2876 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
2877 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
2880 #if CONFIG_SENSOR_Exposure
2881 static struct reginfo sensor_Exposure0[]=
2886 static struct reginfo sensor_Exposure1[]=
2891 static struct reginfo sensor_Exposure2[]=
2896 static struct reginfo sensor_Exposure3[]=
2901 static struct reginfo sensor_Exposure4[]=
2906 static struct reginfo sensor_Exposure5[]=
2911 static struct reginfo sensor_Exposure6[]=
2916 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
2917 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
2920 #if CONFIG_SENSOR_Saturation
2921 static struct reginfo sensor_Saturation0[]=
2926 static struct reginfo sensor_Saturation1[]=
2931 static struct reginfo sensor_Saturation2[]=
2935 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
2938 #if CONFIG_SENSOR_Contrast
2939 static struct reginfo sensor_Contrast0[]=
2944 static struct reginfo sensor_Contrast1[]=
2949 static struct reginfo sensor_Contrast2[]=
2954 static struct reginfo sensor_Contrast3[]=
2959 static struct reginfo sensor_Contrast4[]=
2965 static struct reginfo sensor_Contrast5[]=
2970 static struct reginfo sensor_Contrast6[]=
2974 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
2975 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
2979 #if CONFIG_SENSOR_Mirror
2980 static struct reginfo sensor_MirrorOn[]=
2985 static struct reginfo sensor_MirrorOff[]=
2989 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
2991 #if CONFIG_SENSOR_Flip
2992 static struct reginfo sensor_FlipOn[]=
2997 static struct reginfo sensor_FlipOff[]=
3001 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
3004 #if CONFIG_SENSOR_Scene
3005 static struct reginfo sensor_SceneAuto[] =
3011 static struct reginfo sensor_SceneNight[] =
3014 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
3032 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
3035 #if CONFIG_SENSOR_DigitalZoom
3036 static struct reginfo sensor_Zoom0[] =
3041 static struct reginfo sensor_Zoom1[] =
3046 static struct reginfo sensor_Zoom2[] =
3052 static struct reginfo sensor_Zoom3[] =
3056 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
3058 static const struct v4l2_querymenu sensor_menus[] =
3060 #if CONFIG_SENSOR_WhiteBalance
3061 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
3062 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
3063 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
3066 #if CONFIG_SENSOR_Effect
3067 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
3068 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
3069 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
3072 #if CONFIG_SENSOR_Scene
3073 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
3076 #if CONFIG_SENSOR_Flash
3077 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
3078 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
3082 static struct v4l2_queryctrl sensor_controls[] =
3084 #if CONFIG_SENSOR_WhiteBalance
3086 .id = V4L2_CID_DO_WHITE_BALANCE,
3087 .type = V4L2_CTRL_TYPE_MENU,
3088 .name = "White Balance Control",
3096 #if CONFIG_SENSOR_Brightness
3098 .id = V4L2_CID_BRIGHTNESS,
3099 .type = V4L2_CTRL_TYPE_INTEGER,
3100 .name = "Brightness Control",
3108 #if CONFIG_SENSOR_Effect
3110 .id = V4L2_CID_EFFECT,
3111 .type = V4L2_CTRL_TYPE_MENU,
3112 .name = "Effect Control",
3120 #if CONFIG_SENSOR_Exposure
3122 .id = V4L2_CID_EXPOSURE,
3123 .type = V4L2_CTRL_TYPE_INTEGER,
3124 .name = "Exposure Control",
3132 #if CONFIG_SENSOR_Saturation
3134 .id = V4L2_CID_SATURATION,
3135 .type = V4L2_CTRL_TYPE_INTEGER,
3136 .name = "Saturation Control",
3144 #if CONFIG_SENSOR_Contrast
3146 .id = V4L2_CID_CONTRAST,
3147 .type = V4L2_CTRL_TYPE_INTEGER,
3148 .name = "Contrast Control",
3156 #if CONFIG_SENSOR_Mirror
3158 .id = V4L2_CID_HFLIP,
3159 .type = V4L2_CTRL_TYPE_BOOLEAN,
3160 .name = "Mirror Control",
3168 #if CONFIG_SENSOR_Flip
3170 .id = V4L2_CID_VFLIP,
3171 .type = V4L2_CTRL_TYPE_BOOLEAN,
3172 .name = "Flip Control",
3180 #if CONFIG_SENSOR_Scene
3182 .id = V4L2_CID_SCENE,
3183 .type = V4L2_CTRL_TYPE_MENU,
3184 .name = "Scene Control",
3192 #if CONFIG_SENSOR_DigitalZoom
3194 .id = V4L2_CID_ZOOM_RELATIVE,
3195 .type = V4L2_CTRL_TYPE_INTEGER,
3196 .name = "DigitalZoom Control",
3202 .id = V4L2_CID_ZOOM_ABSOLUTE,
3203 .type = V4L2_CTRL_TYPE_INTEGER,
3204 .name = "DigitalZoom Control",
3212 #if CONFIG_SENSOR_Focus
3214 .id = V4L2_CID_FOCUS_RELATIVE,
3215 .type = V4L2_CTRL_TYPE_INTEGER,
3216 .name = "Focus Control",
3222 .id = V4L2_CID_FOCUS_ABSOLUTE,
3223 .type = V4L2_CTRL_TYPE_INTEGER,
3224 .name = "Focus Control",
3228 .default_value = 125,
3231 .id = V4L2_CID_FOCUS_AUTO,
3232 .type = V4L2_CTRL_TYPE_BOOLEAN,
3233 .name = "Focus Control",
3239 .id = V4L2_CID_FOCUS_CONTINUOUS,
3240 .type = V4L2_CTRL_TYPE_BOOLEAN,
3241 .name = "Focus Control",
3249 #if CONFIG_SENSOR_Flash
3251 .id = V4L2_CID_FLASH,
3252 .type = V4L2_CTRL_TYPE_MENU,
3253 .name = "Flash Control",
3262 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
3263 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
3264 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
3265 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
3266 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
3267 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
3268 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
3269 static int sensor_resume(struct soc_camera_device *icd);
3270 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
3271 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
3272 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
3273 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
3274 static int sensor_deactivate(struct i2c_client *client);
3275 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
3276 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
3278 static struct soc_camera_ops sensor_ops =
3280 .suspend = sensor_suspend,
3281 .resume = sensor_resume,
3282 .set_bus_param = sensor_set_bus_param,
3283 .query_bus_param = sensor_query_bus_param,
3284 .controls = sensor_controls,
3285 .menus = sensor_menus,
3286 .num_controls = ARRAY_SIZE(sensor_controls),
3287 .num_menus = ARRAY_SIZE(sensor_menus),
3290 /* only one fixed colorspace per pixelcode */
3291 struct sensor_datafmt {
3292 enum v4l2_mbus_pixelcode code;
3293 enum v4l2_colorspace colorspace;
3296 /* Find a data format by a pixel code in an array */
3297 static const struct sensor_datafmt *sensor_find_datafmt(
3298 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
3302 for (i = 0; i < n; i++)
3303 if (fmt[i].code == code)
3309 static const struct sensor_datafmt sensor_colour_fmts[] = {
3310 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
3311 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
3317 WqCmd_af_special_pos,
3321 WqCmd_af_update_zone
3323 enum sensor_wq_result
3331 struct i2c_client *client;
3332 struct delayed_work dwork;
3333 enum sensor_wq_cmd cmd;
3334 wait_queue_head_t done;
3335 enum sensor_wq_result result;
3339 typedef struct sensor_info_priv_s
3353 unsigned char mirror; /* HFLIP */
3354 unsigned char flip; /* VFLIP */
3357 struct reginfo *winseqe_cur_addr;
3358 struct sensor_datafmt fmt;
3359 unsigned int enable;
3360 unsigned int funmodule_state;
3361 } sensor_info_priv_t;
3365 struct sensor_parameter
3367 unsigned short int preview_maxlines;
3368 unsigned short int preview_exposure;
3369 unsigned short int preview_line_width;
3370 unsigned short int preview_gain;
3372 unsigned short int capture_framerate;
3373 unsigned short int preview_framerate;
3378 struct v4l2_subdev subdev;
3379 struct i2c_client *client;
3380 sensor_info_priv_t info_priv;
3381 struct sensor_parameter parameter;
3382 struct workqueue_struct *sensor_wq;
3383 struct mutex wq_lock;
3384 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
3385 #if CONFIG_SENSOR_I2C_NOSCHED
3386 atomic_t tasklock_cnt;
3388 struct rk29camera_platform_data *sensor_io_request;
3389 struct rk29camera_gpio_res *sensor_gpio_res;
3392 static struct sensor* to_sensor(const struct i2c_client *client)
3394 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
3397 static int sensor_task_lock(struct i2c_client *client, int lock)
3399 #if CONFIG_SENSOR_I2C_NOSCHED
3401 struct sensor *sensor = to_sensor(client);
3404 if (atomic_read(&sensor->tasklock_cnt) == 0) {
3405 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
3406 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
3410 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
3411 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
3412 goto sensor_task_lock_err;
3417 atomic_add(1, &sensor->tasklock_cnt);
3419 if (atomic_read(&sensor->tasklock_cnt) > 0) {
3420 atomic_sub(1, &sensor->tasklock_cnt);
3422 if (atomic_read(&sensor->tasklock_cnt) == 0)
3427 sensor_task_lock_err:
3435 /* sensor register write */
3436 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
3440 struct i2c_msg msg[1];
3443 buf[1] = reg & 0xFF;
3446 msg->addr = client->addr;
3447 msg->flags = client->flags;
3449 msg->len = sizeof(buf);
3450 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3451 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
3456 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
3457 err = i2c_transfer(client->adapter, msg, 1);
3462 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
3470 /* sensor register read */
3471 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
3475 struct i2c_msg msg[2];
3478 buf[1] = reg & 0xFF;
3480 msg[0].addr = client->addr;
3481 msg[0].flags = client->flags;
3483 msg[0].len = sizeof(buf);
3484 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3485 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
3487 msg[1].addr = client->addr;
3488 msg[1].flags = client->flags|I2C_M_RD;
3491 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3492 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
3496 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
3497 err = i2c_transfer(client->adapter, msg, 2);
3503 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
3511 /* write a array of registers */
3512 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
3516 #if CONFIG_SENSOR_Focus
3517 struct sensor *sensor = to_sensor(client);
3519 #if CONFIG_SENSOR_I2C_RDWRCHK
3524 if (sensor_task_lock(client, 1) < 0)
3525 goto sensor_write_array_end;
3526 while (regarray[i].reg != 0)
3528 #if CONFIG_SENSOR_Focus
3529 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
3530 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
3532 goto sensor_write_array_end;
3536 err = sensor_write(client, regarray[i].reg, regarray[i].val);
3540 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
3544 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
3546 goto sensor_write_array_end;
3549 #if CONFIG_SENSOR_I2C_RDWRCHK
3550 sensor_read(client, regarray[i].reg, &valchk);
3551 if (valchk != regarray[i].val)
3552 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
3559 #if CONFIG_SENSOR_Focus
3560 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
3561 || (regarray == sensor_init_data)) {
3562 sensor->info_priv.affm_reinit = 1;
3566 sensor_write_array_end:
3567 sensor_task_lock(client,0);
3570 #if CONFIG_SENSOR_I2C_RDWRCHK
3571 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
3579 while (regarray[i].reg != 0)
3581 sensor_read(client, regarray[i].reg, &valchk);
3582 if (valchk != regarray[i].val)
3583 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
3590 #if CONFIG_SENSOR_Focus
3597 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
3600 char read_tag=0xff,cnt;
3601 struct sensor *sensor = to_sensor(client);
3603 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3604 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3605 goto sensor_af_cmdset_err;
3609 if (cmdinfo->validate_bit & 0x80) {
3610 if (sensor_write(client, CMD_TAG_Reg, cmdinfo->cmd_tag)) {
3611 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
3612 goto sensor_af_cmdset_err;
3614 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
3616 for (i=0; i<4; i++) {
3617 if (cmdinfo->validate_bit & (1<<i)) {
3618 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
3619 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]);
3620 goto sensor_af_cmdset_err;
3622 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]);
3626 if (sensor_write(client, CMD_TAG_Reg, 0xff)) {
3627 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
3628 goto sensor_af_cmdset_err;
3630 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
3633 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
3634 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
3635 goto sensor_af_cmdset_err;
3642 if (sensor_read(client,CMD_TAG_Reg,&read_tag)){
3643 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
3646 } while((read_tag != 0x00)&& (cnt++<100));
3648 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
3650 sensor_af_cmdset_err:
3654 static int sensor_af_idlechk(struct i2c_client *client)
3658 struct sensor *sensor = to_sensor(client);
3660 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3661 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3663 goto sensor_af_idlechk_end;
3669 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3671 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3673 goto sensor_af_idlechk_end;
3676 if (state != S_IDLE) {
3677 sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
3681 } while((state != S_IDLE)&& (cnt<20));
3683 ret = (state == S_IDLE) ? 0 : -1;
3685 sensor_af_idlechk_end:
3689 static int sensor_af_single(struct i2c_client *client)
3693 struct sensor *sensor = to_sensor(client);
3695 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3696 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3698 goto sensor_af_single_end;
3701 if (sensor_af_idlechk(client))
3702 goto sensor_af_single_end;
3704 if (sensor_af_cmdset(client, SingleFocus_Cmd, NULL)) {
3705 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
3707 goto sensor_af_single_end;
3717 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3719 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3721 goto sensor_af_single_end;
3723 }while((state == S_FOCUSING) && (cnt<100));
3725 if (state != S_FOCUSED) {
3726 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
3728 goto sensor_af_single_end;
3730 sensor_af_single_end:
3734 static int sensor_af_const(struct i2c_client *client)
3737 struct sensor *sensor = to_sensor(client);
3739 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3740 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3742 goto sensor_af_const_end;
3745 if (sensor_af_idlechk(client))
3746 goto sensor_af_const_end;
3748 if (sensor_af_cmdset(client, ConstFocus_Cmd, NULL)) {
3749 SENSOR_TR("%s const focus mode set error!\n",SENSOR_NAME_STRING());
3751 goto sensor_af_const_end;
3753 sensor_af_const_end:
3756 static int sensor_af_pause2capture(struct i2c_client *client)
3760 struct sensor *sensor = to_sensor(client);
3762 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3763 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3765 goto sensor_af_pause_end;
3768 if (sensor_af_cmdset(client, PauseFocus_Cmd, NULL)) {
3769 SENSOR_TR("%s pause focus mode set error!\n",SENSOR_NAME_STRING());
3771 goto sensor_af_pause_end;
3781 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3783 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3785 goto sensor_af_pause_end;
3787 }while((state != S_CAPTURE) && (cnt<100));
3789 if (state != S_CAPTURE) {
3790 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
3792 goto sensor_af_pause_end;
3794 sensor_af_pause_end:
3797 static int sensor_af_zoneupdate(struct i2c_client *client)
3800 struct sensor *sensor = to_sensor(client);
3802 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3803 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3805 goto sensor_af_zoneupdate_end;
3808 if (sensor_af_idlechk(client))
3809 goto sensor_af_zoneupdate_end;
3811 if (sensor_af_cmdset(client, UpdateZone_Cmd, NULL)) {
3812 SENSOR_TR("%s update zone fail!\n",SENSOR_NAME_STRING());
3814 goto sensor_af_zoneupdate_end;
3817 sensor_af_zoneupdate_end:
3820 static int sensor_af_init(struct i2c_client *client)
3825 ret = sensor_write_array(client, sensor_af_firmware);
3827 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
3829 goto sensor_af_init_end;
3839 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3841 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3843 goto sensor_af_init_end;
3845 }while((state == S_STARTUP) && (cnt<100));
3847 if (state != S_IDLE) {
3848 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
3850 goto sensor_af_init_end;
3854 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
3858 static int sensor_af_downfirmware(struct i2c_client *client)
3860 struct sensor *sensor = to_sensor(client);
3861 struct af_cmdinfo cmdinfo;
3862 int ret=0, focus_pos = 0xfe;
3863 struct soc_camera_device *icd = client->dev.platform_data;
3864 struct v4l2_mbus_framefmt mf;
3866 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
3868 if (sensor_af_init(client)) {
3869 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
3872 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
3874 mf.width = icd->user_width;
3875 mf.height = icd->user_height;
3876 mf.code = sensor->info_priv.fmt.code;
3877 mf.colorspace = sensor->info_priv.fmt.colorspace;
3878 mf.field = V4L2_FIELD_NONE;
3879 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
3880 ret = sensor_af_const(client);
3882 switch (sensor->info_priv.auto_focus)
3884 case SENSOR_AF_MODE_INFINITY:
3888 case SENSOR_AF_MODE_MACRO:
3890 if (focus_pos != 0x00)
3893 sensor_af_idlechk(client);
3894 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
3895 cmdinfo.cmd_para[0] = focus_pos;
3896 cmdinfo.validate_bit = 0x81;
3897 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
3900 case SENSOR_AF_MODE_AUTO:
3902 ret = sensor_af_single(client);
3905 case SENSOR_AF_MODE_CONTINUOUS:
3907 ret = sensor_af_const(client);
3910 case SENSOR_AF_MODE_CLOSE:
3917 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
3918 goto sensor_af_downfirmware_end;
3922 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
3925 sensor_af_downfirmware_end:
3929 static void sensor_af_workqueue(struct work_struct *work)
3931 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
3932 struct i2c_client *client = sensor_work->client;
3933 struct sensor *sensor = to_sensor(client);
3934 struct af_cmdinfo cmdinfo;
3936 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
3938 mutex_lock(&sensor->wq_lock);
3940 switch (sensor_work->cmd)
3944 if (sensor_af_downfirmware(client) < 0) {
3945 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
3949 case WqCmd_af_single:
3951 if (sensor_af_single(client) < 0) {
3952 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
3953 sensor_work->result = WqRet_fail;
3955 sensor_work->result = WqRet_success;
3959 case WqCmd_af_special_pos:
3961 sensor_af_idlechk(client);
3963 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
3964 cmdinfo.cmd_para[0] = sensor_work->var;
3965 cmdinfo.validate_bit = 0x81;
3966 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
3967 sensor_work->result = WqRet_fail;
3969 sensor_work->result = WqRet_success;
3972 case WqCmd_af_near_pos:
3974 sensor_af_idlechk(client);
3975 cmdinfo.cmd_tag = StepFocus_Near_Tag;
3976 cmdinfo.validate_bit = 0x80;
3977 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
3978 sensor_work->result = WqRet_fail;
3980 sensor_work->result = WqRet_success;
3983 case WqCmd_af_far_pos:
3985 sensor_af_idlechk(client);
3986 cmdinfo.cmd_tag = StepFocus_Far_Tag;
3987 cmdinfo.validate_bit = 0x80;
3988 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
3989 sensor_work->result = WqRet_fail;
3991 sensor_work->result = WqRet_success;
3994 case WqCmd_af_continues:
3996 if (sensor_af_const(client) < 0)
3997 sensor_work->result = WqRet_fail;
3999 sensor_work->result = WqRet_success;
4002 case WqCmd_af_update_zone:
4004 if (sensor_af_zoneupdate(client) < 0)
4005 sensor_work->result = WqRet_fail;
4007 sensor_work->result = WqRet_success;
4011 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
4015 if (sensor_work->wait == false) {
4016 kfree((void*)sensor_work);
4018 wake_up(&sensor_work->done);
4020 mutex_unlock(&sensor->wq_lock);
4024 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait)
4026 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4027 struct sensor *sensor = to_sensor(client);
4028 struct sensor_work *wk;
4031 if (sensor->sensor_wq == NULL) {
4033 goto sensor_af_workqueue_set_end;
4036 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) != SENSOR_AF_IS_OK) {
4037 if (cmd != WqCmd_af_init) {
4038 SENSOR_TR("%s %s cmd(%d) ingore,because af module isn't ready!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4040 goto sensor_af_workqueue_set_end;
4044 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
4046 wk->client = client;
4047 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
4049 wk->result = WqRet_inval;
4052 init_waitqueue_head(&wk->done);
4054 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
4056 /* ddl@rock-chips.com:
4057 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
4058 * As a result any other ioctl calls will proceed very, very slowly since each call
4059 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
4060 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
4063 mutex_unlock(&icd->video_lock);
4064 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(2500)) == 0) {
4065 SENSOR_TR("%s %s cmd(%d) is timeout!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4069 mutex_lock(&icd->video_lock);
4073 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4076 sensor_af_workqueue_set_end:
4080 static int sensor_parameter_record(struct i2c_client *client)
4082 u8 ret_l,ret_m,ret_h;
4084 struct sensor *sensor = to_sensor(client);
4086 sensor_write(client,0x3503,0x07); //stop AE/AG
4087 sensor_write(client,0x3406,0x01); //stop AWB
4089 sensor_read(client,0x3500,&ret_h);
4090 sensor_read(client,0x3501, &ret_m);
4091 sensor_read(client,0x3502, &ret_l);
4095 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
4096 sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
4097 sensor_read(client,0x350c, &ret_h);
4098 sensor_read(client,0x350d, &ret_l);
4099 sensor->parameter.preview_line_width = ret_h & 0xff;
4100 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
4101 //Read back AGC Gain for preview
4102 sensor_read(client,0x350b, &tp_l);
4103 sensor->parameter.preview_gain = tp_l;
4105 sensor->parameter.capture_framerate = 900;
4106 sensor->parameter.preview_framerate = 1500;
4108 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);
4111 static int sensor_ae_transfer(struct i2c_client *client)
4116 u16 ulCapture_Exposure;
4117 u32 ulCapture_Exposure_Gain;
4121 u8 reg_l = 0,reg_h =0;
4122 u16 Preview_Maxlines;
4124 u32 Capture_MaxLines;
4125 struct sensor *sensor = to_sensor(client);
4127 Preview_Maxlines = sensor->parameter.preview_line_width;
4128 Gain = sensor->parameter.preview_gain;
4129 sensor_read(client,0x350c, ®_h);
4130 sensor_read(client,0x350d, ®_l);
4131 Capture_MaxLines = reg_h & 0xff;
4132 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
4135 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
4137 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
4140 if(Preview_Maxlines == 0)
4141 Preview_Maxlines = 1;
4143 ulCapture_Exposure =
4144 (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
4145 iCapture_Gain = (Gain & 0x0f) + 16;
4147 iCapture_Gain = iCapture_Gain << 1;
4150 iCapture_Gain = iCapture_Gain << 1;
4153 iCapture_Gain = iCapture_Gain << 1;
4156 iCapture_Gain = iCapture_Gain << 1;
4158 ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
4159 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
4160 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
4161 if (ulCapture_Exposure > Lines_10ms)
4163 //ulCapture_Exposure *= 1.7;
4164 ulCapture_Exposure /= Lines_10ms;
4165 ulCapture_Exposure *= Lines_10ms;
4168 ulCapture_Exposure = Capture_MaxLines;
4169 //ulCapture_Exposure_Gain *= 1.5;
4171 if(ulCapture_Exposure == 0)
4172 ulCapture_Exposure = 1;
4173 iCapture_Gain = (ulCapture_Exposure_Gain*2/ulCapture_Exposure + 1)/2;
4174 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
4175 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
4176 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
4179 if (iCapture_Gain > 31) {
4181 iCapture_Gain = iCapture_Gain >> 1;
4183 if (iCapture_Gain > 31) {
4185 iCapture_Gain = iCapture_Gain >> 1;
4187 if (iCapture_Gain > 31) {
4189 iCapture_Gain = iCapture_Gain >> 1;
4191 if (iCapture_Gain > 31) {
4193 iCapture_Gain = iCapture_Gain >> 1;
4195 if (iCapture_Gain > 16)
4196 Gain |= ((iCapture_Gain -16) & 0x0f);
4199 // write the gain and exposure to 0x350* registers
4200 //m_iWrite0x350b=Gain;
4201 sensor_write(client,0x350b, Gain);
4202 //m_iWrite0x3502=ExposureLow;
4203 sensor_write(client,0x3502, ExposureLow);
4204 //m_iWrite0x3501=ExposureMid;
4205 sensor_write(client,0x3501, ExposureMid);
4206 //m_iWrite0x3500=ExposureHigh;
4207 sensor_write(client,0x3500, ExposureHigh);
4208 // SendToFile("Gain = 0x%x\r\n", Gain);
4209 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
4210 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
4211 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
4212 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
4213 //camera_timed_wait(200);
4214 //linzhk camera_timed_wait(500);
4216 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
4220 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
4222 struct soc_camera_link *icl = to_soc_camera_link(icd);
4225 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
4229 case Sensor_PowerDown:
4231 if (icl->powerdown) {
4232 ret = icl->powerdown(icd->pdev, on);
4233 if (ret == RK29_CAM_IO_SUCCESS) {
4237 icl->reset(icd->pdev);
4239 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
4241 goto sensor_power_end;
4248 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4249 struct sensor *sensor = to_sensor(client);
4251 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
4252 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
4254 //flash off after 2 secs
4255 hrtimer_cancel(&(flash_off_timer.timer));
4256 hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
4263 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
4272 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
4273 struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
4274 sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
4275 SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
4280 static s32 sensor_init_width = 800;
4281 static s32 sensor_init_height = 600;
4282 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);
4283 static enum v4l2_mbus_pixelcode sensor_init_pixelcode = V4L2_MBUS_FMT_YUYV8_2X8;
4284 static struct reginfo* sensor_init_data_p = NULL;
4285 static struct reginfo* sensor_init_winseq_p = NULL;
4286 static struct reginfo* sensor_init_winseq_board = NULL;
4287 static struct reginfo* sensor_init_data_board = NULL;
4288 static int sensor_init(struct v4l2_subdev *sd, u32 val)
4290 struct i2c_client *client = v4l2_get_subdevdata(sd);
4291 struct soc_camera_device *icd = client->dev.platform_data;
4292 struct sensor *sensor = to_sensor(client);
4293 const struct v4l2_queryctrl *qctrl;
4294 const struct sensor_datafmt *fmt;
4296 int ret,pid = 0,i = 0,j=0;
4297 struct rk29camera_platform_data* tmp_plat_data =sensor->sensor_io_request;
4299 sensor_init_data_p = sensor_init_data;
4300 sensor_init_winseq_p = sensor_svga;
4301 sensor_init_width = 800;
4302 sensor_init_height = 600;
4303 if (tmp_plat_data != NULL) {
4304 for(i = 0;i < RK_CAM_NUM;i++){
4305 if ((tmp_plat_data->sensor_init_data[i])&& tmp_plat_data->info[i].dev_name &&
4306 (strcmp(tmp_plat_data->info[i].dev_name, dev_name(icd->pdev)) == 0)) {
4311 if(tmp_plat_data &&(i < RK_CAM_NUM) && tmp_plat_data->sensor_init_data[i]){
4312 //user has defined the init data
4314 int tmp_init_data_size = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data_size;
4315 if(tmp_init_data_size > 2){//init data is valid
4316 if((sizeof(struct reginfo) != sizeof(struct reginfo_t))){
4317 if(sensor_init_data_board) {
4318 vfree(sensor_init_data_board);
4319 sensor_init_data_board = NULL;
4321 sensor_init_data_board = (struct reginfo*)vmalloc(tmp_init_data_size);
4322 if(!sensor_init_data_board)
4323 SENSOR_TR("%s :vmalloc init data erro !",__FUNCTION__);
4324 for(j = 0;j< tmp_init_data_size;j++) {
4325 sensor_init_data_board[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].reg;
4326 sensor_init_data_board[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].val;
4328 sensor_init_data_p = sensor_init_data_board;
4330 sensor_init_data_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data);
4334 int tmp_winseq_size = tmp_plat_data->sensor_init_data[i]->rk_sensor_winseq_size;
4335 if(tmp_winseq_size > 2){
4336 if(sizeof(struct reginfo) != sizeof(struct reginfo_t)){
4337 if(sensor_init_winseq_board) {
4338 vfree(sensor_init_winseq_board);
4339 sensor_init_winseq_board = NULL;
4341 sensor_init_winseq_board = (struct reginfo*)vmalloc(tmp_winseq_size);
4342 if(!sensor_init_winseq_board)
4343 SENSOR_TR("%s :vmalloc erro !",__FUNCTION__);
4344 for(j = 0;j< tmp_winseq_size;j++){
4345 sensor_init_winseq_board[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].reg;
4346 sensor_init_winseq_board[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].val;
4348 sensor_init_winseq_p = sensor_init_winseq_board;
4350 sensor_init_winseq_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq);
4353 //init width,height,bus,pixelcode
4354 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width != INVALID_VALUE)
4355 sensor_init_width = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width;
4356 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height != INVALID_VALUE)
4357 sensor_init_height = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height;
4358 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param != INVALID_VALUE)
4359 sensor_init_busparam = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param;
4360 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode != INVALID_VALUE)
4361 sensor_init_pixelcode = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode;
4363 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
4365 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
4367 goto sensor_INIT_ERR;
4371 if (sensor_task_lock(client,1)<0)
4372 goto sensor_INIT_ERR;
4373 ret = sensor_write(client, 0x3008, 0x80);
4375 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
4377 goto sensor_INIT_ERR;
4380 mdelay(5); //delay 5 microseconds
4381 /* check if it is an sensor sensor */
4382 ret = sensor_read(client, 0x300a, &value);
4384 SENSOR_TR("read chip id high byte failed\n");
4386 goto sensor_INIT_ERR;
4389 pid |= (value << 8);
4391 ret = sensor_read(client, 0x300b, &value);
4393 SENSOR_TR("read chip id low byte failed\n");
4395 goto sensor_INIT_ERR;
4398 pid |= (value & 0xff);
4399 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
4401 if (pid == SENSOR_ID) {
4402 sensor->model = SENSOR_V4L2_IDENT;
4404 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
4406 goto sensor_INIT_ERR;
4409 ret = sensor_write_array(client, sensor_init_data_p);
4411 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
4412 goto sensor_INIT_ERR;
4414 sensor_task_lock(client,0);
4415 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
4416 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
4418 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
4420 goto sensor_INIT_ERR;
4422 sensor->info_priv.fmt = *fmt;
4424 /* sensor sensor information for initialization */
4425 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4427 sensor->info_priv.whiteBalance = qctrl->default_value;
4428 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
4430 sensor->info_priv.brightness = qctrl->default_value;
4431 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4433 sensor->info_priv.effect = qctrl->default_value;
4434 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
4436 sensor->info_priv.exposure = qctrl->default_value;
4438 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
4440 sensor->info_priv.saturation = qctrl->default_value;
4441 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
4443 sensor->info_priv.contrast = qctrl->default_value;
4444 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
4446 sensor->info_priv.mirror = qctrl->default_value;
4447 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
4449 sensor->info_priv.flip = qctrl->default_value;
4450 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
4452 sensor->info_priv.scene = qctrl->default_value;
4453 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
4455 sensor->info_priv.digitalzoom = qctrl->default_value;
4457 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
4458 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
4460 sensor->info_priv.focus = qctrl->default_value;
4462 #if CONFIG_SENSOR_Flash
4463 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
4465 sensor->info_priv.flash = qctrl->default_value;
4466 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4467 flash_off_timer.icd = icd;
4468 flash_off_timer.timer.function = flash_off_func;
4470 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);
4472 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
4476 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
4477 sensor_task_lock(client,0);
4478 sensor_deactivate(client);
4481 static int sensor_deactivate(struct i2c_client *client)
4483 struct soc_camera_device *icd = client->dev.platform_data;
4484 struct sensor *sensor = to_sensor(client);
4486 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
4488 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
4489 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
4490 sensor_task_lock(client, 1);
4491 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
4492 sensor_write(client, 0x3018, 0x03); // D5-D0
4493 sensor_write(client,0x3019,0x00); // STROBE,SDA
4494 sensor_task_lock(client, 0);
4496 sensor_ioctrl(icd, Sensor_PowerDown, 1);
4498 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
4499 icd->user_width = SENSOR_INIT_WIDTH;
4500 icd->user_height = SENSOR_INIT_HEIGHT;
4501 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
4504 static struct reginfo sensor_power_down_sequence[]=
4508 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
4511 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4513 if (pm_msg.event == PM_EVENT_SUSPEND) {
4514 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
4515 ret = sensor_write_array(client, sensor_power_down_sequence) ;
4517 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
4520 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
4522 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
4527 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
4534 static int sensor_resume(struct soc_camera_device *icd)
4538 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
4540 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
4544 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
4548 static int sensor_set_bus_param(struct soc_camera_device *icd,
4549 unsigned long flags)
4555 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
4557 struct soc_camera_link *icl = to_soc_camera_link(icd);
4558 unsigned long flags = SENSOR_BUS_PARAM;
4560 return soc_camera_apply_sensor_flags(icl, flags);
4562 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4564 struct i2c_client *client = v4l2_get_subdevdata(sd);
4565 struct soc_camera_device *icd = client->dev.platform_data;
4566 struct sensor *sensor = to_sensor(client);
4568 mf->width = icd->user_width;
4569 mf->height = icd->user_height;
4570 mf->code = sensor->info_priv.fmt.code;
4571 mf->colorspace = sensor->info_priv.fmt.colorspace;
4572 mf->field = V4L2_FIELD_NONE;
4576 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4580 if ((mf->width == 1024) && (mf->height == 768)) {
4582 } else if ((mf->width == 1280) && (mf->height == 1024)) {
4584 } else if ((mf->width == 1600) && (mf->height == 1200)) {
4586 } else if ((mf->width == 2048) && (mf->height == 1536)) {
4588 } else if ((mf->width == 2592) && (mf->height == 1944)) {
4593 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
4597 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4601 if ((mf->width == 1280) && (mf->height == 720)) {
4603 } else if ((mf->width == 1920) && (mf->height == 1080)) {
4608 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
4611 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4613 struct i2c_client *client = v4l2_get_subdevdata(sd);
4614 const struct sensor_datafmt *fmt;
4615 struct sensor *sensor = to_sensor(client);
4616 const struct v4l2_queryctrl *qctrl;
4617 struct soc_camera_device *icd = client->dev.platform_data;
4618 struct reginfo *winseqe_set_addr=NULL;
4619 int ret = 0, set_w,set_h;
4621 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
4622 ARRAY_SIZE(sensor_colour_fmts));
4625 goto sensor_s_fmt_end;
4628 if (sensor->info_priv.fmt.code != mf->code) {
4631 case V4L2_MBUS_FMT_YUYV8_2X8:
4633 winseqe_set_addr = sensor_ClrFmt_YUYV;
4636 case V4L2_MBUS_FMT_UYVY8_2X8:
4638 winseqe_set_addr = sensor_ClrFmt_UYVY;
4644 if (winseqe_set_addr != NULL) {
4645 sensor_write_array(client, winseqe_set_addr);
4646 sensor->info_priv.fmt.code = mf->code;
4647 sensor->info_priv.fmt.colorspace= mf->colorspace;
4648 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
4650 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
4657 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
4659 winseqe_set_addr = sensor_qcif;
4663 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
4665 winseqe_set_addr = sensor_qvga;
4669 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
4671 winseqe_set_addr = sensor_cif;
4675 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
4677 winseqe_set_addr = sensor_vga;
4681 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
4683 winseqe_set_addr = sensor_svga;
4687 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
4689 winseqe_set_addr = sensor_xga;
4693 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
4695 winseqe_set_addr = sensor_720p;
4699 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
4701 winseqe_set_addr = sensor_sxga;
4705 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
4707 winseqe_set_addr = sensor_uxga;
4711 else if (((set_w <= 1920) && (set_h <= 1080)) && sensor_1080p[0].reg)
4713 winseqe_set_addr = sensor_1080p;
4717 else if (((set_w <= 2048) && (set_h <= 1536)) && sensor_qxga[0].reg)
4719 winseqe_set_addr = sensor_qxga;
4723 else if (((set_w <= 2592) && (set_h <= 1944)) && sensor_qsxga[0].reg)
4725 winseqe_set_addr = sensor_qsxga;
4731 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
4732 set_w = SENSOR_INIT_WIDTH;
4733 set_h = SENSOR_INIT_HEIGHT;
4734 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
4737 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
4739 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
4740 sensor_parameter_record(client);
4741 #if CONFIG_SENSOR_Flash
4742 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4743 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
4744 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
4747 }else { /* ddl@rock-chips.com : Video */
4748 #if CONFIG_SENSOR_Flash
4749 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4750 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
4751 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
4756 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
4757 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
4758 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
4759 ret |= sensor_write_array(client,sensor_init_data);
4760 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
4764 ret |= sensor_write_array(client, winseqe_set_addr);
4766 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
4767 #if CONFIG_SENSOR_Flash
4768 if (sensor_fmt_capturechk(sd,mf) == true) {
4769 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4770 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
4771 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
4775 goto sensor_s_fmt_end;
4778 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
4780 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
4781 sensor_ae_transfer(client);
4782 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4783 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4784 if (sensor->info_priv.whiteBalance != 0) {
4785 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4786 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
4788 #if CONFIG_SENSOR_Focus
4789 if (sensor->info_priv.auto_focus != SENSOR_AF_MODE_INFINITY) {
4790 if (sensor_af_workqueue_set(icd, WqCmd_af_update_zone,0,true) == 0) {
4791 if ((sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) ||
4792 (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)) {
4794 sensor_af_workqueue_set(icd, WqCmd_af_single,0,false);
4799 sensor->info_priv.snap2preview = true;
4800 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
4801 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4802 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4803 #if CONFIG_SENSOR_Focus
4804 sensor->info_priv.affm_reinit = 1;
4806 sensor->info_priv.video2preview = true;
4807 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
4808 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4809 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4810 if (sensor->info_priv.snap2preview == true) {
4811 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4812 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
4814 #if CONFIG_SENSOR_Focus
4815 if (sensor->info_priv.auto_focus != SENSOR_AF_MODE_INFINITY) {
4816 /* ddl@rock-chips.com: The af operation is not necessary, if user don't care whether in focus after preview*/
4817 if (sensor_af_workqueue_set(icd, WqCmd_af_update_zone,0,true) == 0) {
4818 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS) {
4819 sensor_af_workqueue_set(icd, WqCmd_af_continues,0,false);
4820 } else if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
4822 sensor_af_workqueue_set(icd, WqCmd_af_single,0,false);
4827 msleep(600); /* ddl@rock-chips.com : whitebalance auto must delay */
4830 sensor->info_priv.video2preview = false;
4831 sensor->info_priv.snap2preview = false;
4836 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
4840 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
4849 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4851 struct i2c_client *client = v4l2_get_subdevdata(sd);
4852 struct sensor *sensor = to_sensor(client);
4853 const struct sensor_datafmt *fmt;
4854 int ret = 0,set_w,set_h;
4856 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
4857 ARRAY_SIZE(sensor_colour_fmts));
4859 fmt = &sensor->info_priv.fmt;
4860 mf->code = fmt->code;
4863 if (mf->height > SENSOR_MAX_HEIGHT)
4864 mf->height = SENSOR_MAX_HEIGHT;
4865 else if (mf->height < SENSOR_MIN_HEIGHT)
4866 mf->height = SENSOR_MIN_HEIGHT;
4868 if (mf->width > SENSOR_MAX_WIDTH)
4869 mf->width = SENSOR_MAX_WIDTH;
4870 else if (mf->width < SENSOR_MIN_WIDTH)
4871 mf->width = SENSOR_MIN_WIDTH;
4876 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
4881 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
4886 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
4891 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
4896 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
4901 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
4906 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
4911 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
4916 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
4921 else if (((set_w <= 1920) && (set_h <= 1080)) && sensor_1080p[0].reg)
4926 else if (((set_w <= 2048) && (set_h <= 1536)) && sensor_qxga[0].reg)
4931 else if (((set_w <= 2592) && (set_h <= 1944)) && sensor_qsxga[0].reg)
4938 set_w = SENSOR_INIT_WIDTH;
4939 set_h = SENSOR_INIT_HEIGHT;
4945 mf->colorspace = fmt->colorspace;
4950 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
4952 struct i2c_client *client = v4l2_get_subdevdata(sd);
4954 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
4957 if (id->match.addr != client->addr)
4960 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
4965 #if CONFIG_SENSOR_Brightness
4966 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4968 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4970 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4972 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
4974 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
4976 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4979 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4983 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4987 #if CONFIG_SENSOR_Effect
4988 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4990 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4992 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4994 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
4996 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
4998 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5001 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5005 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5009 #if CONFIG_SENSOR_Exposure
5010 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5012 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5014 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
5016 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
5018 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
5020 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5023 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5027 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5031 #if CONFIG_SENSOR_Saturation
5032 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5034 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5036 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
5038 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
5040 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
5042 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5045 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5049 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5053 #if CONFIG_SENSOR_Contrast
5054 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5056 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5058 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
5060 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
5062 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
5064 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5067 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5071 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5075 #if CONFIG_SENSOR_Mirror
5076 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5078 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5080 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
5082 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
5084 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
5086 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5089 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5093 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5097 #if CONFIG_SENSOR_Flip
5098 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5100 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5102 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
5104 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
5106 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
5108 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5111 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5115 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5119 #if CONFIG_SENSOR_Scene
5120 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5122 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5124 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
5126 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
5128 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
5130 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5133 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5137 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5141 #if CONFIG_SENSOR_WhiteBalance
5142 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5144 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5146 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
5148 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
5150 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
5152 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5155 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5159 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5163 #if CONFIG_SENSOR_DigitalZoom
5164 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
5166 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5167 struct sensor *sensor = to_sensor(client);
5168 const struct v4l2_queryctrl *qctrl_info;
5169 int digitalzoom_cur, digitalzoom_total;
5171 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
5175 digitalzoom_cur = sensor->info_priv.digitalzoom;
5176 digitalzoom_total = qctrl_info->maximum;
5178 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
5180 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
5184 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
5186 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
5190 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
5192 *value = digitalzoom_total - digitalzoom_cur;
5195 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
5197 *value = 0 - digitalzoom_cur;
5200 digitalzoom_cur += *value;
5202 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
5204 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
5206 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
5209 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
5216 #if CONFIG_SENSOR_Focus
5217 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5219 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5220 struct sensor *sensor = to_sensor(client);
5221 const struct v4l2_queryctrl *qctrl_info;
5224 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
5228 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
5229 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
5230 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true);
5231 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
5234 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5238 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
5239 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
5244 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5246 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5247 struct sensor *sensor = to_sensor(client);
5248 const struct v4l2_queryctrl *qctrl_info;
5251 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
5255 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
5256 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
5258 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true);
5260 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true);
5262 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
5265 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5269 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
5270 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
5275 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5277 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5278 struct sensor *sensor = to_sensor(client);
5281 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
5284 case SENSOR_AF_MODE_AUTO:
5286 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true);
5290 case SENSOR_AF_MODE_MACRO:
5292 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
5296 case SENSOR_AF_MODE_INFINITY:
5298 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
5302 case SENSOR_AF_MODE_CONTINUOUS:
5304 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true);
5308 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5313 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
5316 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
5317 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
5324 #if CONFIG_SENSOR_Flash
5325 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5327 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
5328 if (value == 3) { /* ddl@rock-chips.com: torch */
5329 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
5331 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
5333 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5337 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5342 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
5344 struct i2c_client *client = v4l2_get_subdevdata(sd);
5345 struct sensor *sensor = to_sensor(client);
5346 const struct v4l2_queryctrl *qctrl;
5348 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
5352 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
5358 case V4L2_CID_BRIGHTNESS:
5360 ctrl->value = sensor->info_priv.brightness;
5363 case V4L2_CID_SATURATION:
5365 ctrl->value = sensor->info_priv.saturation;
5368 case V4L2_CID_CONTRAST:
5370 ctrl->value = sensor->info_priv.contrast;
5373 case V4L2_CID_DO_WHITE_BALANCE:
5375 ctrl->value = sensor->info_priv.whiteBalance;
5378 case V4L2_CID_EXPOSURE:
5380 ctrl->value = sensor->info_priv.exposure;
5383 case V4L2_CID_HFLIP:
5385 ctrl->value = sensor->info_priv.mirror;
5388 case V4L2_CID_VFLIP:
5390 ctrl->value = sensor->info_priv.flip;
5401 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
5403 struct i2c_client *client = v4l2_get_subdevdata(sd);
5404 struct sensor *sensor = to_sensor(client);
5405 struct soc_camera_device *icd = client->dev.platform_data;
5406 const struct v4l2_queryctrl *qctrl;
5409 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
5413 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
5419 #if CONFIG_SENSOR_Brightness
5420 case V4L2_CID_BRIGHTNESS:
5422 if (ctrl->value != sensor->info_priv.brightness)
5424 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
5428 sensor->info_priv.brightness = ctrl->value;
5433 #if CONFIG_SENSOR_Exposure
5434 case V4L2_CID_EXPOSURE:
5436 if (ctrl->value != sensor->info_priv.exposure)
5438 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
5442 sensor->info_priv.exposure = ctrl->value;
5447 #if CONFIG_SENSOR_Saturation
5448 case V4L2_CID_SATURATION:
5450 if (ctrl->value != sensor->info_priv.saturation)
5452 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
5456 sensor->info_priv.saturation = ctrl->value;
5461 #if CONFIG_SENSOR_Contrast
5462 case V4L2_CID_CONTRAST:
5464 if (ctrl->value != sensor->info_priv.contrast)
5466 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
5470 sensor->info_priv.contrast = ctrl->value;
5475 #if CONFIG_SENSOR_WhiteBalance
5476 case V4L2_CID_DO_WHITE_BALANCE:
5478 if (ctrl->value != sensor->info_priv.whiteBalance)
5480 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
5484 sensor->info_priv.whiteBalance = ctrl->value;
5489 #if CONFIG_SENSOR_Mirror
5490 case V4L2_CID_HFLIP:
5492 if (ctrl->value != sensor->info_priv.mirror)
5494 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
5496 sensor->info_priv.mirror = ctrl->value;
5501 #if CONFIG_SENSOR_Flip
5502 case V4L2_CID_VFLIP:
5504 if (ctrl->value != sensor->info_priv.flip)
5506 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
5508 sensor->info_priv.flip = ctrl->value;
5519 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
5521 const struct v4l2_queryctrl *qctrl;
5522 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5523 struct sensor *sensor = to_sensor(client);
5525 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
5529 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
5533 switch (ext_ctrl->id)
5535 case V4L2_CID_SCENE:
5537 ext_ctrl->value = sensor->info_priv.scene;
5540 case V4L2_CID_EFFECT:
5542 ext_ctrl->value = sensor->info_priv.effect;
5545 case V4L2_CID_ZOOM_ABSOLUTE:
5547 ext_ctrl->value = sensor->info_priv.digitalzoom;
5550 case V4L2_CID_ZOOM_RELATIVE:
5554 case V4L2_CID_FOCUS_ABSOLUTE:
5558 case V4L2_CID_FOCUS_RELATIVE:
5562 case V4L2_CID_FLASH:
5564 ext_ctrl->value = sensor->info_priv.flash;
5572 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
5574 const struct v4l2_queryctrl *qctrl;
5575 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5576 struct sensor *sensor = to_sensor(client);
5579 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
5583 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
5588 switch (ext_ctrl->id)
5590 #if CONFIG_SENSOR_Scene
5591 case V4L2_CID_SCENE:
5593 if (ext_ctrl->value != sensor->info_priv.scene)
5595 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
5597 sensor->info_priv.scene = ext_ctrl->value;
5602 #if CONFIG_SENSOR_Effect
5603 case V4L2_CID_EFFECT:
5605 if (ext_ctrl->value != sensor->info_priv.effect)
5607 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
5609 sensor->info_priv.effect= ext_ctrl->value;
5614 #if CONFIG_SENSOR_DigitalZoom
5615 case V4L2_CID_ZOOM_ABSOLUTE:
5617 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5620 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
5622 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
5624 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
5626 sensor->info_priv.digitalzoom += val_offset;
5628 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
5633 case V4L2_CID_ZOOM_RELATIVE:
5635 if (ext_ctrl->value)
5637 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
5639 sensor->info_priv.digitalzoom += ext_ctrl->value;
5641 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
5646 #if CONFIG_SENSOR_Focus
5647 case V4L2_CID_FOCUS_ABSOLUTE:
5649 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5652 ret = sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value);
5653 if ((ret == 0) || (0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK))) {
5654 if (ext_ctrl->value == qctrl->minimum) {
5655 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
5656 } else if (ext_ctrl->value == qctrl->maximum) {
5657 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
5659 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
5665 case V4L2_CID_FOCUS_RELATIVE:
5667 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5670 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
5673 case V4L2_CID_FOCUS_AUTO:
5675 if (ext_ctrl->value == 1) {
5676 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
5677 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
5678 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
5682 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
5683 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
5684 if (ext_ctrl->value == 0)
5685 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
5689 case V4L2_CID_FOCUS_CONTINUOUS:
5691 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
5692 if (ext_ctrl->value == 1) {
5693 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0) {
5694 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
5695 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
5699 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
5702 if (ext_ctrl->value == 0)
5703 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
5708 #if CONFIG_SENSOR_Flash
5709 case V4L2_CID_FLASH:
5711 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
5713 sensor->info_priv.flash = ext_ctrl->value;
5715 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
5726 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
5728 struct i2c_client *client = v4l2_get_subdevdata(sd);
5729 struct soc_camera_device *icd = client->dev.platform_data;
5730 int i, error_cnt=0, error_idx=-1;
5733 for (i=0; i<ext_ctrl->count; i++) {
5734 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
5741 error_idx = ext_ctrl->count;
5743 if (error_idx != -1) {
5744 ext_ctrl->error_idx = error_idx;
5751 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
5753 struct i2c_client *client = v4l2_get_subdevdata(sd);
5754 struct soc_camera_device *icd = client->dev.platform_data;
5755 int i, error_cnt=0, error_idx=-1;
5757 for (i=0; i<ext_ctrl->count; i++) {
5758 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
5765 error_idx = ext_ctrl->count;
5767 if (error_idx != -1) {
5768 ext_ctrl->error_idx = error_idx;
5775 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
5777 struct i2c_client *client = v4l2_get_subdevdata(sd);
5778 struct sensor *sensor = to_sensor(client);
5779 #if CONFIG_SENSOR_Focus
5780 struct soc_camera_device *icd = client->dev.platform_data;
5781 struct v4l2_mbus_framefmt mf;
5785 sensor->info_priv.enable = 1;
5786 #if CONFIG_SENSOR_Focus
5787 mf.width = icd->user_width;
5788 mf.height = icd->user_height;
5789 mf.code = sensor->info_priv.fmt.code;
5790 mf.colorspace = sensor->info_priv.fmt.colorspace;
5791 mf.field = V4L2_FIELD_NONE;
5792 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
5793 if (sensor_fmt_capturechk(sd, &mf) == false) {
5794 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
5795 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false);
5796 sensor->info_priv.affm_reinit = 0;
5800 } else if (enable == 0) {
5801 sensor->info_priv.enable = 0;
5802 #if CONFIG_SENSOR_Focus
5803 flush_workqueue(sensor->sensor_wq);
5809 /* Interface active, can use i2c. If it fails, it can indeed mean, that
5810 * this wasn't our capture interface, so, we wait for the right one */
5811 static int sensor_video_probe(struct soc_camera_device *icd,
5812 struct i2c_client *client)
5816 struct sensor *sensor = to_sensor(client);
5818 /* We must have a parent by now. And it cannot be a wrong one.
5819 * So this entire test is completely redundant. */
5820 if (!icd->dev.parent ||
5821 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
5824 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
5826 goto sensor_video_probe_err;
5829 ret = sensor_write(client, 0x3012, 0x80);
5831 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
5833 goto sensor_video_probe_err;
5835 mdelay(5); //delay 5 microseconds
5837 /* check if it is an sensor sensor */
5838 ret = sensor_read(client, 0x300a, &value);
5840 SENSOR_TR("read chip id high byte failed\n");
5842 goto sensor_video_probe_err;
5845 pid |= (value << 8);
5847 ret = sensor_read(client, 0x300b, &value);
5849 SENSOR_TR("read chip id low byte failed\n");
5851 goto sensor_video_probe_err;
5854 pid |= (value & 0xff);
5855 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
5856 if (pid == SENSOR_ID) {
5857 sensor->model = SENSOR_V4L2_IDENT;
5859 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
5861 goto sensor_video_probe_err;
5866 sensor_video_probe_err:
5870 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
5872 struct i2c_client *client = v4l2_get_subdevdata(sd);
5873 struct soc_camera_device *icd = client->dev.platform_data;
5874 struct sensor *sensor = to_sensor(client);
5877 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
5880 case RK29_CAM_SUBDEV_DEACTIVATE:
5882 sensor_deactivate(client);
5885 case RK29_CAM_SUBDEV_IOREQUEST:
5887 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
5889 if (sensor->sensor_io_request != NULL) {
5891 for(j = 0;j < RK_CAM_NUM;j++){
5892 if (sensor->sensor_io_request->gpio_res[j].dev_name &&
5893 (strcmp(sensor->sensor_io_request->gpio_res[j].dev_name, dev_name(icd->pdev)) == 0)) {
5894 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[j];
5898 if(j == RK_CAM_NUM){
5899 SENSOR_TR("%s %s RK_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
5901 goto sensor_ioctl_end;
5905 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
5907 #if CONFIG_SENSOR_Flash
5908 if (sensor->sensor_gpio_res) {
5909 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
5910 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
5911 for (i = 0; i < icd->ops->num_controls; i++) {
5912 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
5913 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
5914 sensor_controls[i].id=0xffff;
5917 sensor->info_priv.flash = 0xff;
5918 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
5919 }else{ //two cameras are the same,need to deal diffrently ,zycc
5920 for (i = 0; i < icd->ops->num_controls; i++) {
5921 if(0xffff == icd->ops->controls[i].id){
5922 sensor_controls[i].id=V4L2_CID_FLASH;
5932 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
5941 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
5942 enum v4l2_mbus_pixelcode *code)
5944 if (index >= ARRAY_SIZE(sensor_colour_fmts))
5947 *code = sensor_colour_fmts[index].code;
5950 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
5951 .init = sensor_init,
5952 .g_ctrl = sensor_g_control,
5953 .s_ctrl = sensor_s_control,
5954 .g_ext_ctrls = sensor_g_ext_controls,
5955 .s_ext_ctrls = sensor_s_ext_controls,
5956 .g_chip_ident = sensor_g_chip_ident,
5957 .ioctl = sensor_ioctl,
5960 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
5961 .s_mbus_fmt = sensor_s_fmt,
5962 .g_mbus_fmt = sensor_g_fmt,
5963 .try_mbus_fmt = sensor_try_fmt,
5964 .enum_mbus_fmt = sensor_enum_fmt,
5965 .s_stream = sensor_s_stream,
5967 static struct v4l2_subdev_ops sensor_subdev_ops = {
5968 .core = &sensor_subdev_core_ops,
5969 .video = &sensor_subdev_video_ops,
5972 static int sensor_probe(struct i2c_client *client,
5973 const struct i2c_device_id *did)
5975 struct sensor *sensor;
5976 struct soc_camera_device *icd = client->dev.platform_data;
5977 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
5978 struct soc_camera_link *icl;
5981 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
5983 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
5987 icl = to_soc_camera_link(icd);
5989 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
5993 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
5994 dev_warn(&adapter->dev,
5995 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
5999 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
6003 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
6005 /* Second stage probe - when a capture adapter is there */
6006 icd->ops = &sensor_ops;
6007 sensor->info_priv.fmt = sensor_colour_fmts[0];
6008 #if CONFIG_SENSOR_I2C_NOSCHED
6009 atomic_set(&sensor->tasklock_cnt,0);
6012 ret = sensor_video_probe(icd, client);
6015 i2c_set_clientdata(client, NULL);
6019 #if CONFIG_SENSOR_Focus
6020 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
6021 if (sensor->sensor_wq == NULL)
6022 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
6023 mutex_init(&sensor->wq_lock);
6027 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
6031 static int sensor_remove(struct i2c_client *client)
6033 struct sensor *sensor = to_sensor(client);
6034 struct soc_camera_device *icd = client->dev.platform_data;
6036 #if CONFIG_SENSOR_Focus
6037 if (sensor->sensor_wq) {
6038 destroy_workqueue(sensor->sensor_wq);
6039 sensor->sensor_wq = NULL;
6044 i2c_set_clientdata(client, NULL);
6045 client->driver = NULL;
6051 static const struct i2c_device_id sensor_id[] = {
6052 {SENSOR_NAME_STRING(), 0 },
6055 MODULE_DEVICE_TABLE(i2c, sensor_id);
6057 static struct i2c_driver sensor_i2c_driver = {
6059 .name = SENSOR_NAME_STRING(),
6061 .probe = sensor_probe,
6062 .remove = sensor_remove,
6063 .id_table = sensor_id,
6066 static int __init sensor_mod_init(void)
6068 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
6069 return i2c_add_driver(&sensor_i2c_driver);
6072 static void __exit sensor_mod_exit(void)
6074 i2c_del_driver(&sensor_i2c_driver);
6077 device_initcall_sync(sensor_mod_init);
6078 module_exit(sensor_mod_exit);
6080 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
6081 MODULE_AUTHOR("ddl <kernel@rock-chips>");
6082 MODULE_LICENSE("GPL");