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/rk29_camera.h>
26 module_param(debug, int, S_IRUGO|S_IWUSR);
28 #define dprintk(level, fmt, arg...) do { \
30 printk(KERN_WARNING fmt , ## arg); } while (0)
32 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
33 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
42 #define MIN(x,y) ((x<y) ? x: y)
43 #define MAX(x,y) ((x>y) ? x: y)
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK29_CAM_SENSOR_OV5642
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV5642
48 #define SENSOR_ID 0x5642
49 #define SENSOR_MIN_WIDTH 176
50 #define SENSOR_MIN_HEIGHT 144
51 #define SENSOR_MAX_WIDTH 2592
52 #define SENSOR_MAX_HEIGHT 1944
53 #define SENSOR_INIT_WIDTH 800 /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT 600
55 #define SENSOR_INIT_WINSEQADR sensor_svga
56 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_UYVY8_2X8
58 #define CONFIG_SENSOR_WhiteBalance 1
59 #define CONFIG_SENSOR_Brightness 0
60 #define CONFIG_SENSOR_Contrast 0
61 #define CONFIG_SENSOR_Saturation 0
62 #define CONFIG_SENSOR_Effect 1
63 #define CONFIG_SENSOR_Scene 1
64 #define CONFIG_SENSOR_DigitalZoom 0
65 #define CONFIG_SENSOR_Exposure 0
66 #define CONFIG_SENSOR_Flash 1
67 #define CONFIG_SENSOR_Mirror 0
68 #define CONFIG_SENSOR_Flip 0
69 #ifdef CONFIG_OV5642_AUTOFOCUS
70 #define CONFIG_SENSOR_Focus 1
71 #include "ov5642_af_firmware.c"
73 #define CONFIG_SENSOR_Focus 0
76 #define CONFIG_SENSOR_I2C_SPEED 250000 /* Hz */
77 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
78 #define CONFIG_SENSOR_I2C_NOSCHED 0
79 #define CONFIG_SENSOR_I2C_RDWRCHK 0
82 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
83 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
84 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
86 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
87 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
88 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
89 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
90 #define COLOR_TEMPERATURE_OFFICE_DN 3500
91 #define COLOR_TEMPERATURE_OFFICE_UP 5000
92 #define COLOR_TEMPERATURE_HOME_DN 2500
93 #define COLOR_TEMPERATURE_HOME_UP 3500
95 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
96 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
98 #define SENSOR_AF_IS_ERR (0x00<<0)
99 #define SENSOR_AF_IS_OK (0x01<<0)
100 #define SENSOR_INIT_IS_ERR (0x00<<28)
101 #define SENSOR_INIT_IS_OK (0x01<<28)
103 #if CONFIG_SENSOR_Focus
104 #define SENSOR_AF_MODE_INFINITY 0
105 #define SENSOR_AF_MODE_MACRO 1
106 #define SENSOR_AF_MODE_FIXED 2
107 #define SENSOR_AF_MODE_AUTO 3
108 #define SENSOR_AF_MODE_CONTINUOUS 4
109 #define SENSOR_AF_MODE_CLOSE 5
112 #if CONFIG_SENSOR_Focus
113 /* ov5642 VCM Command and Status Registers */
114 #define CMD_MAIN_Reg 0x3024
115 #define CMD_TAG_Reg 0x3025
116 #define CMD_PARA0_Reg 0x5085
117 #define CMD_PARA1_Reg 0x5084
118 #define CMD_PARA2_Reg 0x5083
119 #define CMD_PARA3_Reg 0x5082
120 #define STA_ZONE_Reg 0x3026
121 #define STA_FOCUS_Reg 0x3027
123 /* ov5642 VCM Command */
124 #define OverlayEn_Cmd 0x01
125 #define OverlayDis_Cmd 0x02
126 #define SingleFocus_Cmd 0x03
127 #define ConstFocus_Cmd 0x04
128 #define StepMode_Cmd 0x05
129 #define PauseFocus_Cmd 0x06
130 #define ReturnIdle_Cmd 0x08
131 #define SetZone_Cmd 0x10
132 #define UpdateZone_Cmd 0x12
133 #define SetMotor_Cmd 0x20
135 /* ov5642 Focus State */
136 #define S_FIRWRE 0xFF
137 #define S_STARTUP 0xfa
139 #define S_DRVICERR 0xee
141 #define S_FOCUSING 0x01
142 #define S_FOCUSED 0x02
143 #define S_CAPTURE 0x12
146 /* ov5642 Zone State */
147 #define Zone_Is_Focused(a, zone_val) (zone_val&(1<<(a-3)))
148 #define Zone_Get_ID(zone_val) (zone_val&0x03)
150 #define Zone_CenterMode 0x01
151 #define Zone_5xMode 0x02
152 #define Zone_5PlusMode 0x03
153 #define Zone_4fMode 0x04
155 #define ZoneSel_Auto 0x0b
156 #define ZoneSel_SemiAuto 0x0c
157 #define ZoneSel_Manual 0x0d
158 #define ZoneSel_Rotate 0x0e
160 /* ov5642 Step Focus Commands */
161 #define StepFocus_Near_Tag 0x01
162 #define StepFocus_Far_Tag 0x02
163 #define StepFocus_Furthest_Tag 0x03
164 #define StepFocus_Nearest_Tag 0x04
165 #define StepFocus_Spec_Tag 0x10
168 /* init 800X600 SVGA */
169 static struct reginfo sensor_init_data[] =
239 {0x4300 , 0x32}, //UYVY
842 /* 720p 15fps @ 1280x720 */
844 static struct reginfo sensor_720p[]=
847 {SEQUENCE_PROPERTY, SEQUENCE_INIT},
1308 {0x3011, 0x10 },//15fps 0x08 // 10fps: 0x05
1361 {0x4300, 0x32 }, // 0x30---> 0x32, set yuv
1366 //{0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
1447 //{0x3008,0x02}, //software wake
1453 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
1455 static struct reginfo sensor_1080p[]=
1458 {SEQUENCE_PROPERTY, SEQUENCE_INIT},
1471 { 0x3011, 0x05 }, //10 fps 0x05 //15fps 0x08
1919 { 0x3011, 0x07 }, //10 fps 0x05 //15fps 0x07
1927 { 0x3807, 0x39 },//0x3a
1965 { 0x4300, 0x32 }, // set yuv, 0x30--> 0x32
1970 //{0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
1985 {0x3622,0x00},//60},
1987 {0x3604,0x40},//48},
2016 { 0x3011, 0x07 }, //10 fps 0x05 //15fps 0x08
2019 {0x3a08,0x14}, //0x05
2020 {0x3a09,0xe0}, //0xf5
2021 {0x3a0a,0x11}, //0x04
2022 {0x3a0b,0x60}, //0xf6
2023 {0x3a0d,0x04}, //0x0b
2024 {0x3a0e,0x03}, //0x0d
2050 //{0x3008,0x02}, //software wake
2055 /* 2592X1944 QSXGA */
2056 static struct reginfo sensor_qsxga[] =
2058 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2126 {0x3008,0x02}, //software wake
2129 /* 2048*1536 QXGA */
2130 static struct reginfo sensor_qxga[] =
2132 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2226 {0x3008,0x02}, //software wake
2230 /* 1600X1200 UXGA */
2231 static struct reginfo sensor_uxga[] =
2233 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2327 {0x3008,0x02}, //software wake
2330 /* 1280X1024 SXGA */
2331 static struct reginfo sensor_sxga[] =
2336 static struct reginfo sensor_xga[] =
2338 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2432 {0x3008,0x02}, //software wake
2437 static struct reginfo sensor_svga[] =
2527 static struct reginfo sensor_vga[] =
2533 static struct reginfo sensor_cif[] =
2564 static struct reginfo sensor_qvga[] =
2570 static struct reginfo sensor_qcif[] =
2601 static struct reginfo ov2655_qqvga[] =
2645 static struct reginfo ov2655_Sharpness_auto[] =
2650 static struct reginfo ov2655_Sharpness1[] =
2656 static struct reginfo ov2655_Sharpness2[][3] =
2663 static struct reginfo ov2655_Sharpness3[] =
2669 static struct reginfo ov2655_Sharpness4[]=
2676 static struct reginfo ov2655_Sharpness5[] =
2684 static struct reginfo sensor_ClrFmt_YUYV[]=
2690 static struct reginfo sensor_ClrFmt_UYVY[]=
2697 #if CONFIG_SENSOR_WhiteBalance
2698 static struct reginfo sensor_WhiteB_Auto[]=
2720 /* Cloudy Colour Temperature : 6500K - 8000K */
2721 static struct reginfo sensor_WhiteB_Cloudy[]=
2732 /* ClearDay Colour Temperature : 5000K - 6500K */
2733 static struct reginfo sensor_WhiteB_ClearDay[]=
2745 /* Office Colour Temperature : 3500K - 5000K */
2746 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
2759 /* Home Colour Temperature : 2500K - 3500K */
2760 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
2772 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
2773 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
2777 #if CONFIG_SENSOR_Brightness
2778 static struct reginfo sensor_Brightness0[]=
2783 static struct reginfo sensor_Brightness1[]=
2788 static struct reginfo sensor_Brightness2[]=
2793 static struct reginfo sensor_Brightness3[]=
2798 static struct reginfo sensor_Brightness4[]=
2803 static struct reginfo sensor_Brightness5[]=
2807 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
2808 sensor_Brightness4, sensor_Brightness5,NULL,
2813 #if CONFIG_SENSOR_Effect
2814 static struct reginfo sensor_Effect_Normal[] =
2821 static struct reginfo sensor_Effect_WandB[] =
2830 static struct reginfo sensor_Effect_Sepia[] =
2839 static struct reginfo sensor_Effect_Negative[] =
2846 static struct reginfo sensor_Effect_Bluish[] =
2856 static struct reginfo sensor_Effect_Green[] =
2865 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
2866 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
2869 #if CONFIG_SENSOR_Exposure
2870 static struct reginfo sensor_Exposure0[]=
2875 static struct reginfo sensor_Exposure1[]=
2880 static struct reginfo sensor_Exposure2[]=
2885 static struct reginfo sensor_Exposure3[]=
2890 static struct reginfo sensor_Exposure4[]=
2895 static struct reginfo sensor_Exposure5[]=
2900 static struct reginfo sensor_Exposure6[]=
2905 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
2906 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
2909 #if CONFIG_SENSOR_Saturation
2910 static struct reginfo sensor_Saturation0[]=
2915 static struct reginfo sensor_Saturation1[]=
2920 static struct reginfo sensor_Saturation2[]=
2924 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
2927 #if CONFIG_SENSOR_Contrast
2928 static struct reginfo sensor_Contrast0[]=
2933 static struct reginfo sensor_Contrast1[]=
2938 static struct reginfo sensor_Contrast2[]=
2943 static struct reginfo sensor_Contrast3[]=
2948 static struct reginfo sensor_Contrast4[]=
2954 static struct reginfo sensor_Contrast5[]=
2959 static struct reginfo sensor_Contrast6[]=
2963 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
2964 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
2968 #if CONFIG_SENSOR_Mirror
2969 static struct reginfo sensor_MirrorOn[]=
2974 static struct reginfo sensor_MirrorOff[]=
2978 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
2980 #if CONFIG_SENSOR_Flip
2981 static struct reginfo sensor_FlipOn[]=
2986 static struct reginfo sensor_FlipOff[]=
2990 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
2993 #if CONFIG_SENSOR_Scene
2994 static struct reginfo sensor_SceneAuto[] =
3000 static struct reginfo sensor_SceneNight[] =
3003 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
3021 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
3024 #if CONFIG_SENSOR_DigitalZoom
3025 static struct reginfo sensor_Zoom0[] =
3030 static struct reginfo sensor_Zoom1[] =
3035 static struct reginfo sensor_Zoom2[] =
3041 static struct reginfo sensor_Zoom3[] =
3045 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
3047 static const struct v4l2_querymenu sensor_menus[] =
3049 #if CONFIG_SENSOR_WhiteBalance
3050 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
3051 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
3052 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
3055 #if CONFIG_SENSOR_Effect
3056 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
3057 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
3058 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
3061 #if CONFIG_SENSOR_Scene
3062 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
3065 #if CONFIG_SENSOR_Flash
3066 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
3067 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
3071 static const struct v4l2_queryctrl sensor_controls[] =
3073 #if CONFIG_SENSOR_WhiteBalance
3075 .id = V4L2_CID_DO_WHITE_BALANCE,
3076 .type = V4L2_CTRL_TYPE_MENU,
3077 .name = "White Balance Control",
3085 #if CONFIG_SENSOR_Brightness
3087 .id = V4L2_CID_BRIGHTNESS,
3088 .type = V4L2_CTRL_TYPE_INTEGER,
3089 .name = "Brightness Control",
3097 #if CONFIG_SENSOR_Effect
3099 .id = V4L2_CID_EFFECT,
3100 .type = V4L2_CTRL_TYPE_MENU,
3101 .name = "Effect Control",
3109 #if CONFIG_SENSOR_Exposure
3111 .id = V4L2_CID_EXPOSURE,
3112 .type = V4L2_CTRL_TYPE_INTEGER,
3113 .name = "Exposure Control",
3121 #if CONFIG_SENSOR_Saturation
3123 .id = V4L2_CID_SATURATION,
3124 .type = V4L2_CTRL_TYPE_INTEGER,
3125 .name = "Saturation Control",
3133 #if CONFIG_SENSOR_Contrast
3135 .id = V4L2_CID_CONTRAST,
3136 .type = V4L2_CTRL_TYPE_INTEGER,
3137 .name = "Contrast Control",
3145 #if CONFIG_SENSOR_Mirror
3147 .id = V4L2_CID_HFLIP,
3148 .type = V4L2_CTRL_TYPE_BOOLEAN,
3149 .name = "Mirror Control",
3157 #if CONFIG_SENSOR_Flip
3159 .id = V4L2_CID_VFLIP,
3160 .type = V4L2_CTRL_TYPE_BOOLEAN,
3161 .name = "Flip Control",
3169 #if CONFIG_SENSOR_Scene
3171 .id = V4L2_CID_SCENE,
3172 .type = V4L2_CTRL_TYPE_MENU,
3173 .name = "Scene Control",
3181 #if CONFIG_SENSOR_DigitalZoom
3183 .id = V4L2_CID_ZOOM_RELATIVE,
3184 .type = V4L2_CTRL_TYPE_INTEGER,
3185 .name = "DigitalZoom Control",
3191 .id = V4L2_CID_ZOOM_ABSOLUTE,
3192 .type = V4L2_CTRL_TYPE_INTEGER,
3193 .name = "DigitalZoom Control",
3201 #if CONFIG_SENSOR_Focus
3203 .id = V4L2_CID_FOCUS_RELATIVE,
3204 .type = V4L2_CTRL_TYPE_INTEGER,
3205 .name = "Focus Control",
3211 .id = V4L2_CID_FOCUS_ABSOLUTE,
3212 .type = V4L2_CTRL_TYPE_INTEGER,
3213 .name = "Focus Control",
3217 .default_value = 125,
3220 .id = V4L2_CID_FOCUS_AUTO,
3221 .type = V4L2_CTRL_TYPE_BOOLEAN,
3222 .name = "Focus Control",
3228 .id = V4L2_CID_FOCUS_CONTINUOUS,
3229 .type = V4L2_CTRL_TYPE_BOOLEAN,
3230 .name = "Focus Control",
3238 #if CONFIG_SENSOR_Flash
3240 .id = V4L2_CID_FLASH,
3241 .type = V4L2_CTRL_TYPE_MENU,
3242 .name = "Flash Control",
3251 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
3252 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
3253 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
3254 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
3255 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
3256 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
3257 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
3258 static int sensor_resume(struct soc_camera_device *icd);
3259 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
3260 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
3261 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
3262 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
3263 static int sensor_deactivate(struct i2c_client *client);
3264 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
3265 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
3267 static struct soc_camera_ops sensor_ops =
3269 .suspend = sensor_suspend,
3270 .resume = sensor_resume,
3271 .set_bus_param = sensor_set_bus_param,
3272 .query_bus_param = sensor_query_bus_param,
3273 .controls = sensor_controls,
3274 .menus = sensor_menus,
3275 .num_controls = ARRAY_SIZE(sensor_controls),
3276 .num_menus = ARRAY_SIZE(sensor_menus),
3279 /* only one fixed colorspace per pixelcode */
3280 struct sensor_datafmt {
3281 enum v4l2_mbus_pixelcode code;
3282 enum v4l2_colorspace colorspace;
3285 /* Find a data format by a pixel code in an array */
3286 static const struct sensor_datafmt *sensor_find_datafmt(
3287 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
3291 for (i = 0; i < n; i++)
3292 if (fmt[i].code == code)
3298 static const struct sensor_datafmt sensor_colour_fmts[] = {
3299 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
3300 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
3303 enum sensor_work_state
3305 sensor_work_ready = 0,
3310 struct i2c_client *client;
3311 struct delayed_work dwork;
3312 enum sensor_work_state state;
3315 typedef struct sensor_info_priv_s
3329 unsigned char mirror; /* HFLIP */
3330 unsigned char flip; /* VFLIP */
3333 struct reginfo *winseqe_cur_addr;
3334 struct sensor_datafmt fmt;
3335 unsigned int enable;
3336 unsigned int funmodule_state;
3337 } sensor_info_priv_t;
3341 struct sensor_parameter
3343 unsigned short int preview_maxlines;
3344 unsigned short int preview_exposure;
3345 unsigned short int preview_line_width;
3346 unsigned short int preview_gain;
3348 unsigned short int capture_framerate;
3349 unsigned short int preview_framerate;
3354 struct v4l2_subdev subdev;
3355 struct i2c_client *client;
3356 sensor_info_priv_t info_priv;
3357 struct sensor_parameter parameter;
3358 struct workqueue_struct *sensor_wq;
3359 struct sensor_work sensor_wk;
3360 struct mutex wq_lock;
3361 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
3362 #if CONFIG_SENSOR_I2C_NOSCHED
3363 atomic_t tasklock_cnt;
3365 struct rk29camera_platform_data *sensor_io_request;
3366 struct rk29camera_gpio_res *sensor_gpio_res;
3369 static struct sensor* to_sensor(const struct i2c_client *client)
3371 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
3374 static int sensor_task_lock(struct i2c_client *client, int lock)
3376 #if CONFIG_SENSOR_I2C_NOSCHED
3378 struct sensor *sensor = to_sensor(client);
3381 if (atomic_read(&sensor->tasklock_cnt) == 0) {
3382 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
3383 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
3387 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
3388 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
3389 goto sensor_task_lock_err;
3394 atomic_add(1, &sensor->tasklock_cnt);
3396 if (atomic_read(&sensor->tasklock_cnt) > 0) {
3397 atomic_sub(1, &sensor->tasklock_cnt);
3399 if (atomic_read(&sensor->tasklock_cnt) == 0)
3404 sensor_task_lock_err:
3412 /* sensor register write */
3413 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
3417 struct i2c_msg msg[1];
3420 buf[1] = reg & 0xFF;
3423 msg->addr = client->addr;
3424 msg->flags = client->flags;
3426 msg->len = sizeof(buf);
3427 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3428 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
3433 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
3434 err = i2c_transfer(client->adapter, msg, 1);
3439 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
3447 /* sensor register read */
3448 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
3452 struct i2c_msg msg[2];
3455 buf[1] = reg & 0xFF;
3457 msg[0].addr = client->addr;
3458 msg[0].flags = client->flags;
3460 msg[0].len = sizeof(buf);
3461 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3462 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
3464 msg[1].addr = client->addr;
3465 msg[1].flags = client->flags|I2C_M_RD;
3468 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3469 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
3473 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
3474 err = i2c_transfer(client->adapter, msg, 2);
3480 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
3488 /* write a array of registers */
3489 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
3493 #if CONFIG_SENSOR_Focus
3494 struct sensor *sensor = to_sensor(client);
3496 #if CONFIG_SENSOR_I2C_RDWRCHK
3501 if (sensor_task_lock(client, 1) < 0)
3502 goto sensor_write_array_end;
3503 while (regarray[i].reg != 0)
3505 #if CONFIG_SENSOR_Focus
3506 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
3507 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
3509 goto sensor_write_array_end;
3513 err = sensor_write(client, regarray[i].reg, regarray[i].val);
3517 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
3521 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
3523 goto sensor_write_array_end;
3526 #if CONFIG_SENSOR_I2C_RDWRCHK
3527 sensor_read(client, regarray[i].reg, &valchk);
3528 if (valchk != regarray[i].val)
3529 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
3536 #if CONFIG_SENSOR_Focus
3537 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
3538 || (regarray == sensor_init_data)) {
3539 sensor->info_priv.affm_reinit = 1;
3543 sensor_write_array_end:
3544 sensor_task_lock(client,0);
3547 #if CONFIG_SENSOR_I2C_RDWRCHK
3548 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
3556 while (regarray[i].reg != 0)
3558 sensor_read(client, regarray[i].reg, &valchk);
3559 if (valchk != regarray[i].val)
3560 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
3567 #if CONFIG_SENSOR_Focus
3574 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
3577 char read_tag=0xff,cnt;
3578 struct sensor *sensor = to_sensor(client);
3580 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3581 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3582 goto sensor_af_cmdset_err;
3586 if (cmdinfo->validate_bit & 0x80) {
3587 if (sensor_write(client, CMD_TAG_Reg, cmdinfo->cmd_tag)) {
3588 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
3589 goto sensor_af_cmdset_err;
3591 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
3593 for (i=0; i<4; i++) {
3594 if (cmdinfo->validate_bit & (1<<i)) {
3595 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
3596 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]);
3597 goto sensor_af_cmdset_err;
3599 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]);
3603 if (sensor_write(client, CMD_TAG_Reg, 0xff)) {
3604 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
3605 goto sensor_af_cmdset_err;
3607 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
3610 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
3611 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
3612 goto sensor_af_cmdset_err;
3619 if (sensor_read(client,CMD_TAG_Reg,&read_tag)){
3620 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
3623 } while((read_tag != 0x00)&& (cnt++<100));
3625 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
3627 sensor_af_cmdset_err:
3631 static int sensor_af_idlechk(struct i2c_client *client)
3635 struct sensor *sensor = to_sensor(client);
3637 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3638 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3640 goto sensor_af_idlechk_end;
3646 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3648 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3650 goto sensor_af_idlechk_end;
3653 if (state != S_IDLE) {
3654 sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
3658 } while((state != S_IDLE)&& (cnt<20));
3660 ret = (state == S_IDLE) ? 0 : -1;
3662 sensor_af_idlechk_end:
3666 static int sensor_af_single(struct i2c_client *client)
3670 struct sensor *sensor = to_sensor(client);
3672 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3673 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3675 goto sensor_af_single_end;
3678 if (sensor_af_idlechk(client))
3679 goto sensor_af_single_end;
3681 if (sensor_af_cmdset(client, SingleFocus_Cmd, NULL)) {
3682 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
3684 goto sensor_af_single_end;
3694 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3696 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3698 goto sensor_af_single_end;
3700 }while((state == S_FOCUSING) && (cnt<100));
3702 if (state != S_FOCUSED) {
3703 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
3705 goto sensor_af_single_end;
3707 sensor_af_single_end:
3711 static int sensor_af_const(struct i2c_client *client)
3714 struct sensor *sensor = to_sensor(client);
3716 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3717 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3719 goto sensor_af_const_end;
3722 if (sensor_af_idlechk(client))
3723 goto sensor_af_const_end;
3725 if (sensor_af_cmdset(client, ConstFocus_Cmd, NULL)) {
3726 SENSOR_TR("%s const focus mode set error!\n",SENSOR_NAME_STRING());
3728 goto sensor_af_const_end;
3730 sensor_af_const_end:
3733 static int sensor_af_pause2capture(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_pause_end;
3745 if (sensor_af_cmdset(client, PauseFocus_Cmd, NULL)) {
3746 SENSOR_TR("%s pause focus mode set error!\n",SENSOR_NAME_STRING());
3748 goto sensor_af_pause_end;
3758 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3760 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3762 goto sensor_af_pause_end;
3764 }while((state != S_CAPTURE) && (cnt<100));
3766 if (state != S_CAPTURE) {
3767 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
3769 goto sensor_af_pause_end;
3771 sensor_af_pause_end:
3774 static int sensor_af_zoneupdate(struct i2c_client *client)
3777 struct sensor *sensor = to_sensor(client);
3779 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) == 0) {
3780 SENSOR_TR("%s %s cancel,because auto focus firmware is invalidate!",SENSOR_NAME_STRING(), __FUNCTION__);
3782 goto sensor_af_zoneupdate_end;
3785 if (sensor_af_idlechk(client))
3786 goto sensor_af_zoneupdate_end;
3788 if (sensor_af_cmdset(client, UpdateZone_Cmd, NULL)) {
3789 SENSOR_TR("%s update zone fail!\n",SENSOR_NAME_STRING());
3791 goto sensor_af_zoneupdate_end;
3794 sensor_af_zoneupdate_end:
3797 static int sensor_af_init(struct i2c_client *client)
3802 ret = sensor_write_array(client, sensor_af_firmware);
3804 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
3806 goto sensor_af_init_end;
3816 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3818 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3820 goto sensor_af_init_end;
3822 }while((state == S_STARTUP) && (cnt<100));
3824 if (state != S_IDLE) {
3825 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
3827 goto sensor_af_init_end;
3831 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
3835 static int sensor_af_wq_function(struct i2c_client *client)
3837 struct sensor *sensor = to_sensor(client);
3838 struct af_cmdinfo cmdinfo;
3839 int ret=0, focus_pos = 0xfe;
3840 struct soc_camera_device *icd = client->dev.platform_data;
3841 struct v4l2_mbus_framefmt mf;
3843 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
3845 mutex_lock(&sensor->wq_lock);
3846 if (sensor_af_init(client)) {
3847 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
3850 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
3852 mf.width = icd->user_width;
3853 mf.height = icd->user_height;
3854 mf.code = sensor->info_priv.fmt.code;
3855 mf.colorspace = sensor->info_priv.fmt.colorspace;
3856 mf.field = V4L2_FIELD_NONE;
3857 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
3858 ret = sensor_af_const(client);
3860 switch (sensor->info_priv.auto_focus)
3862 case SENSOR_AF_MODE_INFINITY:
3866 case SENSOR_AF_MODE_MACRO:
3868 if (focus_pos != 0x00)
3871 sensor_af_idlechk(client);
3872 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
3873 cmdinfo.cmd_para[0] = focus_pos;
3874 cmdinfo.validate_bit = 0x81;
3875 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
3878 case SENSOR_AF_MODE_AUTO:
3880 ret = sensor_af_single(client);
3883 case SENSOR_AF_MODE_CONTINUOUS:
3885 ret = sensor_af_const(client);
3888 case SENSOR_AF_MODE_CLOSE:
3895 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
3896 goto sensor_af_wq_function_end;
3900 SENSOR_DG("%s sensor_af_wq_function set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
3903 sensor_af_wq_function_end:
3904 sensor->sensor_wk.state = sensor_work_ready;
3905 mutex_unlock(&sensor->wq_lock);
3908 static void sensor_af_workqueue(struct work_struct *work)
3910 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
3911 struct i2c_client *client = sensor_work->client;
3913 if (sensor_af_wq_function(client) < 0) {
3914 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
3918 static int sensor_parameter_record(struct i2c_client *client)
3920 u8 ret_l,ret_m,ret_h;
3922 struct sensor *sensor = to_sensor(client);
3924 sensor_write(client,0x3503,0x07); //stop AE/AG
3925 sensor_write(client,0x3406,0x01); //stop AWB
3927 sensor_read(client,0x3500,&ret_h);
3928 sensor_read(client,0x3501, &ret_m);
3929 sensor_read(client,0x3502, &ret_l);
3933 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
3934 sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
3935 sensor_read(client,0x350c, &ret_h);
3936 sensor_read(client,0x350d, &ret_l);
3937 sensor->parameter.preview_line_width = ret_h & 0xff;
3938 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
3939 //Read back AGC Gain for preview
3940 sensor_read(client,0x350b, &tp_l);
3941 sensor->parameter.preview_gain = tp_l;
3943 sensor->parameter.capture_framerate = 900;
3944 sensor->parameter.preview_framerate = 1500;
3946 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);
3949 static int sensor_ae_transfer(struct i2c_client *client)
3954 u16 ulCapture_Exposure;
3955 u32 ulCapture_Exposure_Gain;
3959 u8 reg_l = 0,reg_h =0;
3960 u16 Preview_Maxlines;
3962 u32 Capture_MaxLines;
3963 struct sensor *sensor = to_sensor(client);
3965 Preview_Maxlines = sensor->parameter.preview_line_width;
3966 Gain = sensor->parameter.preview_gain;
3967 sensor_read(client,0x350c, ®_h);
3968 sensor_read(client,0x350d, ®_l);
3969 Capture_MaxLines = reg_h & 0xff;
3970 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
3973 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
3975 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
3978 if(Preview_Maxlines == 0)
3979 Preview_Maxlines = 1;
3981 ulCapture_Exposure =
3982 (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
3983 iCapture_Gain = (Gain & 0x0f) + 16;
3985 iCapture_Gain = iCapture_Gain << 1;
3988 iCapture_Gain = iCapture_Gain << 1;
3991 iCapture_Gain = iCapture_Gain << 1;
3994 iCapture_Gain = iCapture_Gain << 1;
3996 ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
3997 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
3998 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
3999 if (ulCapture_Exposure > Lines_10ms)
4001 //ulCapture_Exposure *= 1.7;
4002 ulCapture_Exposure /= Lines_10ms;
4003 ulCapture_Exposure *= Lines_10ms;
4006 ulCapture_Exposure = Capture_MaxLines;
4007 //ulCapture_Exposure_Gain *= 1.5;
4009 if(ulCapture_Exposure == 0)
4010 ulCapture_Exposure = 1;
4011 iCapture_Gain = (ulCapture_Exposure_Gain*2/ulCapture_Exposure + 1)/2;
4012 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
4013 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
4014 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
4017 if (iCapture_Gain > 31) {
4019 iCapture_Gain = iCapture_Gain >> 1;
4021 if (iCapture_Gain > 31) {
4023 iCapture_Gain = iCapture_Gain >> 1;
4025 if (iCapture_Gain > 31) {
4027 iCapture_Gain = iCapture_Gain >> 1;
4029 if (iCapture_Gain > 31) {
4031 iCapture_Gain = iCapture_Gain >> 1;
4033 if (iCapture_Gain > 16)
4034 Gain |= ((iCapture_Gain -16) & 0x0f);
4037 // write the gain and exposure to 0x350* registers
4038 //m_iWrite0x350b=Gain;
4039 sensor_write(client,0x350b, Gain);
4040 //m_iWrite0x3502=ExposureLow;
4041 sensor_write(client,0x3502, ExposureLow);
4042 //m_iWrite0x3501=ExposureMid;
4043 sensor_write(client,0x3501, ExposureMid);
4044 //m_iWrite0x3500=ExposureHigh;
4045 sensor_write(client,0x3500, ExposureHigh);
4046 // SendToFile("Gain = 0x%x\r\n", Gain);
4047 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
4048 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
4049 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
4050 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
4051 //camera_timed_wait(200);
4052 //linzhk camera_timed_wait(500);
4054 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
4058 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
4060 struct soc_camera_link *icl = to_soc_camera_link(icd);
4063 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
4067 case Sensor_PowerDown:
4069 if (icl->powerdown) {
4070 ret = icl->powerdown(icd->pdev, on);
4071 if (ret == RK29_CAM_IO_SUCCESS) {
4075 icl->reset(icd->pdev);
4077 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
4079 goto sensor_power_end;
4086 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4087 struct sensor *sensor = to_sensor(client);
4089 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
4090 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
4096 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
4104 static int sensor_init(struct v4l2_subdev *sd, u32 val)
4106 struct i2c_client *client = sd->priv;
4107 struct soc_camera_device *icd = client->dev.platform_data;
4108 struct sensor *sensor = to_sensor(client);
4109 const struct v4l2_queryctrl *qctrl;
4110 const struct sensor_datafmt *fmt;
4114 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
4116 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
4118 goto sensor_INIT_ERR;
4122 if (sensor_task_lock(client,1)<0)
4123 goto sensor_INIT_ERR;
4124 ret = sensor_write(client, 0x3008, 0x80);
4126 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
4128 goto sensor_INIT_ERR;
4131 mdelay(5); //delay 5 microseconds
4132 /* check if it is an sensor sensor */
4133 ret = sensor_read(client, 0x300a, &value);
4135 SENSOR_TR("read chip id high byte failed\n");
4137 goto sensor_INIT_ERR;
4140 pid |= (value << 8);
4142 ret = sensor_read(client, 0x300b, &value);
4144 SENSOR_TR("read chip id low byte failed\n");
4146 goto sensor_INIT_ERR;
4149 pid |= (value & 0xff);
4150 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
4152 if (pid == SENSOR_ID) {
4153 sensor->model = SENSOR_V4L2_IDENT;
4155 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
4157 goto sensor_INIT_ERR;
4160 ret = sensor_write_array(client, sensor_init_data);
4162 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
4163 goto sensor_INIT_ERR;
4165 sensor_task_lock(client,0);
4166 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
4167 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
4169 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
4171 goto sensor_INIT_ERR;
4173 sensor->info_priv.fmt = *fmt;
4175 /* sensor sensor information for initialization */
4176 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4178 sensor->info_priv.whiteBalance = qctrl->default_value;
4179 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
4181 sensor->info_priv.brightness = qctrl->default_value;
4182 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4184 sensor->info_priv.effect = qctrl->default_value;
4185 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
4187 sensor->info_priv.exposure = qctrl->default_value;
4189 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
4191 sensor->info_priv.saturation = qctrl->default_value;
4192 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
4194 sensor->info_priv.contrast = qctrl->default_value;
4195 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
4197 sensor->info_priv.mirror = qctrl->default_value;
4198 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
4200 sensor->info_priv.flip = qctrl->default_value;
4201 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
4203 sensor->info_priv.scene = qctrl->default_value;
4204 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
4206 sensor->info_priv.digitalzoom = qctrl->default_value;
4208 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
4209 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
4211 sensor->info_priv.focus = qctrl->default_value;
4213 #if CONFIG_SENSOR_Flash
4214 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
4216 sensor->info_priv.flash = qctrl->default_value;
4218 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);
4220 sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
4224 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
4225 sensor_task_lock(client,0);
4226 sensor_deactivate(client);
4229 static int sensor_deactivate(struct i2c_client *client)
4231 struct soc_camera_device *icd = client->dev.platform_data;
4232 struct sensor *sensor = to_sensor(client);
4234 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
4236 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
4237 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
4238 sensor_task_lock(client, 1);
4239 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
4240 sensor_write(client, 0x3018, 0x03); // D5-D0
4241 sensor_write(client,0x3019,0X00); // STROBE,SDA
4242 sensor_task_lock(client, 0);
4244 sensor_ioctrl(icd, Sensor_PowerDown, 1);
4247 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
4248 icd->user_width = SENSOR_INIT_WIDTH;
4249 icd->user_height = SENSOR_INIT_HEIGHT;
4250 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
4253 static struct reginfo sensor_power_down_sequence[]=
4257 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
4260 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4262 if (pm_msg.event == PM_EVENT_SUSPEND) {
4263 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
4264 ret = sensor_write_array(client, sensor_power_down_sequence) ;
4266 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
4269 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
4271 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
4276 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
4283 static int sensor_resume(struct soc_camera_device *icd)
4287 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
4289 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
4293 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
4297 static int sensor_set_bus_param(struct soc_camera_device *icd,
4298 unsigned long flags)
4304 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
4306 struct soc_camera_link *icl = to_soc_camera_link(icd);
4307 unsigned long flags = SENSOR_BUS_PARAM;
4309 return soc_camera_apply_sensor_flags(icl, flags);
4311 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4313 struct i2c_client *client = sd->priv;
4314 struct soc_camera_device *icd = client->dev.platform_data;
4315 struct sensor *sensor = to_sensor(client);
4317 mf->width = icd->user_width;
4318 mf->height = icd->user_height;
4319 mf->code = sensor->info_priv.fmt.code;
4320 mf->colorspace = sensor->info_priv.fmt.colorspace;
4321 mf->field = V4L2_FIELD_NONE;
4325 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4329 if ((mf->width == 1024) && (mf->height == 768)) {
4331 } else if ((mf->width == 1280) && (mf->height == 1024)) {
4333 } else if ((mf->width == 1600) && (mf->height == 1200)) {
4335 } else if ((mf->width == 2048) && (mf->height == 1536)) {
4337 } else if ((mf->width == 2592) && (mf->height == 1944)) {
4342 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
4346 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4350 if ((mf->width == 1280) && (mf->height == 720)) {
4352 } else if ((mf->width == 1920) && (mf->height == 1080)) {
4357 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
4360 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4362 struct i2c_client *client = sd->priv;
4363 const struct sensor_datafmt *fmt;
4364 struct sensor *sensor = to_sensor(client);
4365 const struct v4l2_queryctrl *qctrl;
4366 struct soc_camera_device *icd = client->dev.platform_data;
4367 struct reginfo *winseqe_set_addr=NULL;
4368 int ret = 0, set_w,set_h;
4370 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
4371 ARRAY_SIZE(sensor_colour_fmts));
4374 goto sensor_s_fmt_end;
4377 if (sensor->info_priv.fmt.code != mf->code) {
4380 case V4L2_MBUS_FMT_YUYV8_2X8:
4382 winseqe_set_addr = sensor_ClrFmt_YUYV;
4385 case V4L2_MBUS_FMT_UYVY8_2X8:
4387 winseqe_set_addr = sensor_ClrFmt_UYVY;
4393 if (winseqe_set_addr != NULL) {
4394 sensor_write_array(client, winseqe_set_addr);
4395 sensor->info_priv.fmt.code = mf->code;
4396 sensor->info_priv.fmt.colorspace= mf->colorspace;
4397 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
4399 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
4406 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
4408 winseqe_set_addr = sensor_qcif;
4412 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
4414 winseqe_set_addr = sensor_qvga;
4418 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
4420 winseqe_set_addr = sensor_cif;
4424 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
4426 winseqe_set_addr = sensor_vga;
4430 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
4432 winseqe_set_addr = sensor_svga;
4436 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
4438 winseqe_set_addr = sensor_xga;
4442 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
4444 winseqe_set_addr = sensor_720p;
4448 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
4450 winseqe_set_addr = sensor_sxga;
4454 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
4456 winseqe_set_addr = sensor_uxga;
4460 else if (((set_w <= 1920) && (set_h <= 1080)) && sensor_1080p[0].reg)
4462 winseqe_set_addr = sensor_1080p;
4466 else if (((set_w <= 2048) && (set_h <= 1536)) && sensor_qxga[0].reg)
4468 winseqe_set_addr = sensor_qxga;
4472 else if (((set_w <= 2592) && (set_h <= 1944)) && sensor_qsxga[0].reg)
4474 winseqe_set_addr = sensor_qsxga;
4480 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
4481 set_w = SENSOR_INIT_WIDTH;
4482 set_h = SENSOR_INIT_HEIGHT;
4483 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
4486 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
4488 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
4489 sensor_parameter_record(client);
4490 #if CONFIG_SENSOR_Flash
4491 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4492 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
4493 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
4496 }else { /* ddl@rock-chips.com : Video */
4497 #if CONFIG_SENSOR_Flash
4498 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4499 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
4500 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
4505 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
4506 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
4507 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
4508 ret |= sensor_write_array(client,sensor_init_data);
4509 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
4513 ret |= sensor_write_array(client, winseqe_set_addr);
4515 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
4516 #if CONFIG_SENSOR_Flash
4517 if (sensor_fmt_capturechk(sd,mf) == true) {
4518 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4519 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
4520 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
4524 goto sensor_s_fmt_end;
4527 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
4529 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
4530 sensor_ae_transfer(client);
4531 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4532 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4533 if (sensor->info_priv.whiteBalance != 0) {
4534 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4535 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
4537 #if CONFIG_SENSOR_Focus
4538 if (sensor->info_priv.auto_focus != SENSOR_AF_MODE_INFINITY) {
4539 if (sensor_af_zoneupdate(client) == 0) {
4540 if ((sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) ||
4541 (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)) {
4543 sensor_af_single(client);
4548 sensor->info_priv.snap2preview = true;
4549 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
4550 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4551 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4552 #if CONFIG_SENSOR_Focus
4553 sensor->info_priv.affm_reinit = 1;
4555 sensor->info_priv.video2preview = true;
4556 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
4557 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4558 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4559 if (sensor->info_priv.snap2preview == true) {
4560 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4561 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
4563 #if CONFIG_SENSOR_Focus
4564 if (sensor->info_priv.auto_focus != SENSOR_AF_MODE_INFINITY) {
4565 if (sensor_af_zoneupdate(client) == 0) {
4566 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS) {
4567 sensor_af_const(client);
4568 } else if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
4570 sensor_af_single(client);
4574 msleep(600); /* ddl@rock-chips.com : whitebalance auto must delay */
4577 sensor->info_priv.video2preview = false;
4578 sensor->info_priv.snap2preview = false;
4583 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
4587 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
4596 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4598 struct i2c_client *client = sd->priv;
4599 struct sensor *sensor = to_sensor(client);
4600 const struct sensor_datafmt *fmt;
4603 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
4604 ARRAY_SIZE(sensor_colour_fmts));
4606 fmt = &sensor->info_priv.fmt;
4607 mf->code = fmt->code;
4610 if (mf->height > SENSOR_MAX_HEIGHT)
4611 mf->height = SENSOR_MAX_HEIGHT;
4612 else if (mf->height < SENSOR_MIN_HEIGHT)
4613 mf->height = SENSOR_MIN_HEIGHT;
4615 if (mf->width > SENSOR_MAX_WIDTH)
4616 mf->width = SENSOR_MAX_WIDTH;
4617 else if (mf->width < SENSOR_MIN_WIDTH)
4618 mf->width = SENSOR_MIN_WIDTH;
4620 mf->colorspace = fmt->colorspace;
4625 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
4627 struct i2c_client *client = sd->priv;
4629 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
4632 if (id->match.addr != client->addr)
4635 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
4640 #if CONFIG_SENSOR_Brightness
4641 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4643 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4645 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4647 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
4649 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
4651 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4654 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4658 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4662 #if CONFIG_SENSOR_Effect
4663 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4665 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4667 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4669 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
4671 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
4673 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4676 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4680 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4684 #if CONFIG_SENSOR_Exposure
4685 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4687 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4689 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4691 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
4693 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
4695 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4698 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4702 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4706 #if CONFIG_SENSOR_Saturation
4707 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4709 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4711 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4713 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
4715 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
4717 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4720 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4724 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4728 #if CONFIG_SENSOR_Contrast
4729 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4731 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4733 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4735 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
4737 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
4739 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4742 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4746 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4750 #if CONFIG_SENSOR_Mirror
4751 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4753 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4755 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4757 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
4759 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
4761 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4764 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4768 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4772 #if CONFIG_SENSOR_Flip
4773 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4775 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4777 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4779 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
4781 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
4783 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4786 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4790 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4794 #if CONFIG_SENSOR_Scene
4795 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4797 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4799 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4801 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
4803 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
4805 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4808 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4812 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4816 #if CONFIG_SENSOR_WhiteBalance
4817 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4819 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4821 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4823 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
4825 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
4827 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4830 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4834 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4838 #if CONFIG_SENSOR_DigitalZoom
4839 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
4841 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4842 struct sensor *sensor = to_sensor(client);
4843 const struct v4l2_queryctrl *qctrl_info;
4844 int digitalzoom_cur, digitalzoom_total;
4846 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
4850 digitalzoom_cur = sensor->info_priv.digitalzoom;
4851 digitalzoom_total = qctrl_info->maximum;
4853 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
4855 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
4859 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
4861 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
4865 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
4867 *value = digitalzoom_total - digitalzoom_cur;
4870 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
4872 *value = 0 - digitalzoom_cur;
4875 digitalzoom_cur += *value;
4877 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
4879 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
4881 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4884 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
4891 #if CONFIG_SENSOR_Focus
4892 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4894 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4895 struct sensor *sensor = to_sensor(client);
4896 const struct v4l2_queryctrl *qctrl_info;
4897 struct af_cmdinfo cmdinfo;
4900 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
4904 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
4905 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
4907 if (sensor_af_idlechk(client))
4908 goto sensor_set_focus_absolute_end;
4910 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
4911 cmdinfo.cmd_para[0] = value;
4912 cmdinfo.validate_bit = 0x81;
4913 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
4914 //ret |= sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
4915 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
4918 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4922 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
4923 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
4926 sensor_set_focus_absolute_end:
4929 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4931 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4932 struct sensor *sensor = to_sensor(client);
4933 const struct v4l2_queryctrl *qctrl_info;
4934 struct af_cmdinfo cmdinfo;
4937 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
4941 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
4942 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
4944 if (sensor_af_idlechk(client))
4945 goto sensor_set_focus_relative_end;
4948 cmdinfo.cmd_tag = StepFocus_Near_Tag;
4949 } else if (value < 0) {
4950 cmdinfo.cmd_tag = StepFocus_Far_Tag;
4952 cmdinfo.validate_bit = 0x80;
4953 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
4955 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
4958 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4962 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
4963 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
4965 sensor_set_focus_relative_end:
4969 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4971 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4972 struct sensor *sensor = to_sensor(client);
4975 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
4978 case SENSOR_AF_MODE_AUTO:
4980 ret = sensor_af_single(client);
4984 case SENSOR_AF_MODE_MACRO:
4986 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
4990 case SENSOR_AF_MODE_INFINITY:
4992 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
4996 case SENSOR_AF_MODE_CONTINUOUS:
4998 ret = sensor_af_const(client);
5002 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5007 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
5010 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
5011 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
5018 #if CONFIG_SENSOR_Flash
5019 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5021 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
5022 if (value == 3) { /* ddl@rock-chips.com: torch */
5023 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
5025 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
5027 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5031 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5036 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
5038 struct i2c_client *client = sd->priv;
5039 struct sensor *sensor = to_sensor(client);
5040 const struct v4l2_queryctrl *qctrl;
5042 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
5046 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
5052 case V4L2_CID_BRIGHTNESS:
5054 ctrl->value = sensor->info_priv.brightness;
5057 case V4L2_CID_SATURATION:
5059 ctrl->value = sensor->info_priv.saturation;
5062 case V4L2_CID_CONTRAST:
5064 ctrl->value = sensor->info_priv.contrast;
5067 case V4L2_CID_DO_WHITE_BALANCE:
5069 ctrl->value = sensor->info_priv.whiteBalance;
5072 case V4L2_CID_EXPOSURE:
5074 ctrl->value = sensor->info_priv.exposure;
5077 case V4L2_CID_HFLIP:
5079 ctrl->value = sensor->info_priv.mirror;
5082 case V4L2_CID_VFLIP:
5084 ctrl->value = sensor->info_priv.flip;
5095 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
5097 struct i2c_client *client = sd->priv;
5098 struct sensor *sensor = to_sensor(client);
5099 struct soc_camera_device *icd = client->dev.platform_data;
5100 const struct v4l2_queryctrl *qctrl;
5103 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
5107 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
5113 #if CONFIG_SENSOR_Brightness
5114 case V4L2_CID_BRIGHTNESS:
5116 if (ctrl->value != sensor->info_priv.brightness)
5118 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
5122 sensor->info_priv.brightness = ctrl->value;
5127 #if CONFIG_SENSOR_Exposure
5128 case V4L2_CID_EXPOSURE:
5130 if (ctrl->value != sensor->info_priv.exposure)
5132 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
5136 sensor->info_priv.exposure = ctrl->value;
5141 #if CONFIG_SENSOR_Saturation
5142 case V4L2_CID_SATURATION:
5144 if (ctrl->value != sensor->info_priv.saturation)
5146 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
5150 sensor->info_priv.saturation = ctrl->value;
5155 #if CONFIG_SENSOR_Contrast
5156 case V4L2_CID_CONTRAST:
5158 if (ctrl->value != sensor->info_priv.contrast)
5160 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
5164 sensor->info_priv.contrast = ctrl->value;
5169 #if CONFIG_SENSOR_WhiteBalance
5170 case V4L2_CID_DO_WHITE_BALANCE:
5172 if (ctrl->value != sensor->info_priv.whiteBalance)
5174 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
5178 sensor->info_priv.whiteBalance = ctrl->value;
5183 #if CONFIG_SENSOR_Mirror
5184 case V4L2_CID_HFLIP:
5186 if (ctrl->value != sensor->info_priv.mirror)
5188 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
5190 sensor->info_priv.mirror = ctrl->value;
5195 #if CONFIG_SENSOR_Flip
5196 case V4L2_CID_VFLIP:
5198 if (ctrl->value != sensor->info_priv.flip)
5200 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
5202 sensor->info_priv.flip = ctrl->value;
5213 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
5215 const struct v4l2_queryctrl *qctrl;
5216 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5217 struct sensor *sensor = to_sensor(client);
5219 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
5223 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
5227 switch (ext_ctrl->id)
5229 case V4L2_CID_SCENE:
5231 ext_ctrl->value = sensor->info_priv.scene;
5234 case V4L2_CID_EFFECT:
5236 ext_ctrl->value = sensor->info_priv.effect;
5239 case V4L2_CID_ZOOM_ABSOLUTE:
5241 ext_ctrl->value = sensor->info_priv.digitalzoom;
5244 case V4L2_CID_ZOOM_RELATIVE:
5248 case V4L2_CID_FOCUS_ABSOLUTE:
5252 case V4L2_CID_FOCUS_RELATIVE:
5256 case V4L2_CID_FLASH:
5258 ext_ctrl->value = sensor->info_priv.flash;
5266 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
5268 const struct v4l2_queryctrl *qctrl;
5269 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5270 struct sensor *sensor = to_sensor(client);
5273 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
5277 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
5282 switch (ext_ctrl->id)
5284 #if CONFIG_SENSOR_Scene
5285 case V4L2_CID_SCENE:
5287 if (ext_ctrl->value != sensor->info_priv.scene)
5289 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
5291 sensor->info_priv.scene = ext_ctrl->value;
5296 #if CONFIG_SENSOR_Effect
5297 case V4L2_CID_EFFECT:
5299 if (ext_ctrl->value != sensor->info_priv.effect)
5301 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
5303 sensor->info_priv.effect= ext_ctrl->value;
5308 #if CONFIG_SENSOR_DigitalZoom
5309 case V4L2_CID_ZOOM_ABSOLUTE:
5311 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5314 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
5316 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
5318 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
5320 sensor->info_priv.digitalzoom += val_offset;
5322 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
5327 case V4L2_CID_ZOOM_RELATIVE:
5329 if (ext_ctrl->value)
5331 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
5333 sensor->info_priv.digitalzoom += ext_ctrl->value;
5335 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
5340 #if CONFIG_SENSOR_Focus
5341 case V4L2_CID_FOCUS_ABSOLUTE:
5343 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5346 ret = sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value);
5347 if ((ret == 0) || (0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK))) {
5348 if (ext_ctrl->value == qctrl->minimum) {
5349 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
5350 } else if (ext_ctrl->value == qctrl->maximum) {
5351 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
5353 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
5359 case V4L2_CID_FOCUS_RELATIVE:
5361 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5364 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
5367 case V4L2_CID_FOCUS_AUTO:
5369 if (ext_ctrl->value == 1) {
5370 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
5371 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
5372 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
5376 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
5377 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
5378 if (ext_ctrl->value == 0)
5379 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
5383 case V4L2_CID_FOCUS_CONTINUOUS:
5385 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
5386 if (ext_ctrl->value == 1) {
5387 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0) {
5388 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
5389 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
5393 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
5396 if (ext_ctrl->value == 0)
5397 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
5402 #if CONFIG_SENSOR_Flash
5403 case V4L2_CID_FLASH:
5405 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
5407 sensor->info_priv.flash = ext_ctrl->value;
5409 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
5420 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
5422 struct i2c_client *client = sd->priv;
5423 struct soc_camera_device *icd = client->dev.platform_data;
5424 int i, error_cnt=0, error_idx=-1;
5427 for (i=0; i<ext_ctrl->count; i++) {
5428 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
5435 error_idx = ext_ctrl->count;
5437 if (error_idx != -1) {
5438 ext_ctrl->error_idx = error_idx;
5445 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
5447 struct i2c_client *client = sd->priv;
5448 struct soc_camera_device *icd = client->dev.platform_data;
5449 int i, error_cnt=0, error_idx=-1;
5451 for (i=0; i<ext_ctrl->count; i++) {
5452 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
5459 error_idx = ext_ctrl->count;
5461 if (error_idx != -1) {
5462 ext_ctrl->error_idx = error_idx;
5469 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
5471 struct i2c_client *client = sd->priv;
5472 struct sensor *sensor = to_sensor(client);
5473 #if CONFIG_SENSOR_Focus
5474 struct soc_camera_device *icd = client->dev.platform_data;
5475 struct v4l2_mbus_framefmt mf;
5479 sensor->info_priv.enable = 1;
5480 #if CONFIG_SENSOR_Focus
5481 mf.width = icd->user_width;
5482 mf.height = icd->user_height;
5483 mf.code = sensor->info_priv.fmt.code;
5484 mf.colorspace = sensor->info_priv.fmt.colorspace;
5485 mf.field = V4L2_FIELD_NONE;
5486 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
5487 if (sensor_fmt_capturechk(sd, &mf) == false) {
5488 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
5489 if (sensor->sensor_wq != NULL) {
5490 mutex_lock(&sensor->wq_lock);
5491 if (sensor->sensor_wk.state == sensor_working) {
5492 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
5493 mutex_unlock(&sensor->wq_lock);
5494 goto sensor_s_stream_end;
5496 sensor->sensor_wk.state = sensor_working;
5497 mutex_unlock(&sensor->wq_lock);
5498 sensor->sensor_wk.client = client;
5499 INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
5500 queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
5502 sensor->info_priv.affm_reinit = 0;
5506 } else if (enable == 0) {
5507 sensor->info_priv.enable = 0;
5508 #if CONFIG_SENSOR_Focus
5509 flush_work(&(sensor->sensor_wk.dwork.work));
5510 mutex_lock(&sensor->wq_lock);
5511 sensor->sensor_wk.state = sensor_work_ready;
5512 mutex_unlock(&sensor->wq_lock);
5516 sensor_s_stream_end:
5520 /* Interface active, can use i2c. If it fails, it can indeed mean, that
5521 * this wasn't our capture interface, so, we wait for the right one */
5522 static int sensor_video_probe(struct soc_camera_device *icd,
5523 struct i2c_client *client)
5527 struct sensor *sensor = to_sensor(client);
5529 /* We must have a parent by now. And it cannot be a wrong one.
5530 * So this entire test is completely redundant. */
5531 if (!icd->dev.parent ||
5532 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
5535 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
5537 goto sensor_video_probe_err;
5540 ret = sensor_write(client, 0x3012, 0x80);
5542 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
5544 goto sensor_video_probe_err;
5546 mdelay(5); //delay 5 microseconds
5548 /* check if it is an sensor sensor */
5549 ret = sensor_read(client, 0x300a, &value);
5551 SENSOR_TR("read chip id high byte failed\n");
5553 goto sensor_video_probe_err;
5556 pid |= (value << 8);
5558 ret = sensor_read(client, 0x300b, &value);
5560 SENSOR_TR("read chip id low byte failed\n");
5562 goto sensor_video_probe_err;
5565 pid |= (value & 0xff);
5566 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
5567 if (pid == SENSOR_ID) {
5568 sensor->model = SENSOR_V4L2_IDENT;
5570 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
5572 goto sensor_video_probe_err;
5577 sensor_video_probe_err:
5581 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
5583 struct i2c_client *client = sd->priv;
5584 struct soc_camera_device *icd = client->dev.platform_data;
5585 struct sensor *sensor = to_sensor(client);
5588 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
5591 case RK29_CAM_SUBDEV_DEACTIVATE:
5593 sensor_deactivate(client);
5596 case RK29_CAM_SUBDEV_IOREQUEST:
5598 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
5600 if (sensor->sensor_io_request != NULL) {
5601 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
5602 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
5603 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
5604 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
5605 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
5606 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
5609 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
5611 goto sensor_ioctl_end;
5614 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
5616 #if CONFIG_SENSOR_Flash
5617 if (sensor->sensor_gpio_res) {
5618 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
5619 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
5620 for (i = 0; i < icd->ops->num_controls; i++) {
5621 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
5622 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
5625 sensor->info_priv.flash = 0xff;
5626 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
5634 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
5643 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
5644 enum v4l2_mbus_pixelcode *code)
5646 if (index >= ARRAY_SIZE(sensor_colour_fmts))
5649 *code = sensor_colour_fmts[index].code;
5652 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
5653 .init = sensor_init,
5654 .g_ctrl = sensor_g_control,
5655 .s_ctrl = sensor_s_control,
5656 .g_ext_ctrls = sensor_g_ext_controls,
5657 .s_ext_ctrls = sensor_s_ext_controls,
5658 .g_chip_ident = sensor_g_chip_ident,
5659 .ioctl = sensor_ioctl,
5662 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
5663 .s_mbus_fmt = sensor_s_fmt,
5664 .g_mbus_fmt = sensor_g_fmt,
5665 .try_mbus_fmt = sensor_try_fmt,
5666 .enum_mbus_fmt = sensor_enum_fmt,
5667 .s_stream = sensor_s_stream,
5669 static struct v4l2_subdev_ops sensor_subdev_ops = {
5670 .core = &sensor_subdev_core_ops,
5671 .video = &sensor_subdev_video_ops,
5674 static int sensor_probe(struct i2c_client *client,
5675 const struct i2c_device_id *did)
5677 struct sensor *sensor;
5678 struct soc_camera_device *icd = client->dev.platform_data;
5679 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
5680 struct soc_camera_link *icl;
5683 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
5685 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
5689 icl = to_soc_camera_link(icd);
5691 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
5695 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
5696 dev_warn(&adapter->dev,
5697 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
5701 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
5705 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
5707 /* Second stage probe - when a capture adapter is there */
5708 icd->ops = &sensor_ops;
5709 sensor->info_priv.fmt = sensor_colour_fmts[0];
5710 #if CONFIG_SENSOR_I2C_NOSCHED
5711 atomic_set(&sensor->tasklock_cnt,0);
5714 ret = sensor_video_probe(icd, client);
5717 i2c_set_clientdata(client, NULL);
5721 #if CONFIG_SENSOR_Focus
5722 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
5723 if (sensor->sensor_wq == NULL)
5724 SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
5725 mutex_init(&sensor->wq_lock);
5726 sensor->sensor_wk.state = sensor_work_ready;
5730 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
5734 static int sensor_remove(struct i2c_client *client)
5736 struct sensor *sensor = to_sensor(client);
5737 struct soc_camera_device *icd = client->dev.platform_data;
5739 #if CONFIG_SENSOR_Focus
5740 if (sensor->sensor_wq) {
5741 destroy_workqueue(sensor->sensor_wq);
5742 sensor->sensor_wq = NULL;
5747 i2c_set_clientdata(client, NULL);
5748 client->driver = NULL;
5754 static const struct i2c_device_id sensor_id[] = {
5755 {SENSOR_NAME_STRING(), 0 },
5758 MODULE_DEVICE_TABLE(i2c, sensor_id);
5760 static struct i2c_driver sensor_i2c_driver = {
5762 .name = SENSOR_NAME_STRING(),
5764 .probe = sensor_probe,
5765 .remove = sensor_remove,
5766 .id_table = sensor_id,
5769 static int __init sensor_mod_init(void)
5771 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
5772 return i2c_add_driver(&sensor_i2c_driver);
5775 static void __exit sensor_mod_exit(void)
5777 i2c_del_driver(&sensor_i2c_driver);
5780 device_initcall_sync(sensor_mod_init);
5781 module_exit(sensor_mod_exit);
5783 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
5784 MODULE_AUTHOR("ddl <kernel@rock-chips>");
5785 MODULE_LICENSE("GPL");