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(0, 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_PIX_FMT_UYVY
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)
101 #if CONFIG_SENSOR_Focus
102 #define SENSOR_AF_MODE_INFINITY 0
103 #define SENSOR_AF_MODE_MACRO 1
104 #define SENSOR_AF_MODE_FIXED 2
105 #define SENSOR_AF_MODE_AUTO 3
106 #define SENSOR_AF_MODE_CONTINUOUS 4
107 #define SENSOR_AF_MODE_CLOSE 5
110 #if CONFIG_SENSOR_Focus
111 /* ov5642 VCM Command and Status Registers */
112 #define CMD_MAIN_Reg 0x3024
113 #define CMD_TAG_Reg 0x3025
114 #define CMD_PARA0_Reg 0x5085
115 #define CMD_PARA1_Reg 0x5084
116 #define CMD_PARA2_Reg 0x5083
117 #define CMD_PARA3_Reg 0x5082
118 #define STA_ZONE_Reg 0x3026
119 #define STA_FOCUS_Reg 0x3027
121 /* ov5642 VCM Command */
122 #define OverlayEn_Cmd 0x01
123 #define OverlayDis_Cmd 0x02
124 #define SingleFocus_Cmd 0x03
125 #define ConstFocus_Cmd 0x04
126 #define StepMode_Cmd 0x05
127 #define PauseFocus_Cmd 0x06
128 #define ReturnIdle_Cmd 0x08
129 #define SetZone_Cmd 0x10
130 #define UpdateZone_Cmd 0x12
131 #define SetMotor_Cmd 0x20
133 /* ov5642 Focus State */
134 #define S_FIRWRE 0xFF
135 #define S_STARTUP 0xfa
137 #define S_DRVICERR 0xee
139 #define S_FOCUSING 0x01
140 #define S_FOCUSED 0x02
141 #define S_CAPTURE 0x12
144 /* ov5642 Zone State */
145 #define Zone_Is_Focused(a, zone_val) (zone_val&(1<<(a-3)))
146 #define Zone_Get_ID(zone_val) (zone_val&0x03)
148 #define Zone_CenterMode 0x01
149 #define Zone_5xMode 0x02
150 #define Zone_5PlusMode 0x03
151 #define Zone_4fMode 0x04
153 #define ZoneSel_Auto 0x0b
154 #define ZoneSel_SemiAuto 0x0c
155 #define ZoneSel_Manual 0x0d
156 #define ZoneSel_Rotate 0x0e
158 /* ov5642 Step Focus Commands */
159 #define StepFocus_Near_Tag 0x01
160 #define StepFocus_Far_Tag 0x02
161 #define StepFocus_Furthest_Tag 0x03
162 #define StepFocus_Nearest_Tag 0x04
163 #define StepFocus_Spec_Tag 0x10
166 /* init 800X600 SVGA */
167 static struct reginfo sensor_init_data[] =
237 {0x4300 , 0x32}, //UYVY
840 /* 720p 15fps @ 1280x720 */
842 static struct reginfo sensor_720p[]=
845 {SEQUENCE_PROPERTY, SEQUENCE_INIT},
1306 {0x3011, 0x10 },//15fps 0x08 // 10fps: 0x05
1359 {0x4300, 0x32 }, // 0x30---> 0x32, set yuv
1364 //{0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
1445 //{0x3008,0x02}, //software wake
1451 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
1453 static struct reginfo sensor_1080p[]=
1456 {SEQUENCE_PROPERTY, SEQUENCE_INIT},
1469 { 0x3011, 0x05 }, //10 fps 0x05 //15fps 0x08
1917 { 0x3011, 0x07 }, //10 fps 0x05 //15fps 0x07
1925 { 0x3807, 0x39 },//0x3a
1963 { 0x4300, 0x32 }, // set yuv, 0x30--> 0x32
1968 //{0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
1983 {0x3622,0x00},//60},
1985 {0x3604,0x40},//48},
2014 { 0x3011, 0x07 }, //10 fps 0x05 //15fps 0x08
2017 {0x3a08,0x14}, //0x05
2018 {0x3a09,0xe0}, //0xf5
2019 {0x3a0a,0x11}, //0x04
2020 {0x3a0b,0x60}, //0xf6
2021 {0x3a0d,0x04}, //0x0b
2022 {0x3a0e,0x03}, //0x0d
2048 //{0x3008,0x02}, //software wake
2053 /* 2592X1944 QSXGA */
2054 static struct reginfo sensor_qsxga[] =
2056 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2124 {0x3008,0x02}, //software wake
2127 /* 2048*1536 QXGA */
2128 static struct reginfo sensor_qxga[] =
2130 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2224 {0x3008,0x02}, //software wake
2228 /* 1600X1200 UXGA */
2229 static struct reginfo sensor_uxga[] =
2231 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2325 {0x3008,0x02}, //software wake
2328 /* 1280X1024 SXGA */
2329 static struct reginfo sensor_sxga[] =
2334 static struct reginfo sensor_xga[] =
2336 {0x3008,0x42}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
2430 {0x3008,0x02}, //software wake
2435 static struct reginfo sensor_svga[] =
2524 static struct reginfo sensor_vga[] =
2530 static struct reginfo sensor_cif[] =
2561 static struct reginfo sensor_qvga[] =
2567 static struct reginfo sensor_qcif[] =
2598 static struct reginfo ov2655_qqvga[] =
2642 static struct reginfo ov2655_Sharpness_auto[] =
2647 static struct reginfo ov2655_Sharpness1[] =
2653 static struct reginfo ov2655_Sharpness2[][3] =
2660 static struct reginfo ov2655_Sharpness3[] =
2666 static struct reginfo ov2655_Sharpness4[]=
2673 static struct reginfo ov2655_Sharpness5[] =
2681 static struct reginfo sensor_ClrFmt_YUYV[]=
2687 static struct reginfo sensor_ClrFmt_UYVY[]=
2694 #if CONFIG_SENSOR_WhiteBalance
2695 static struct reginfo sensor_WhiteB_Auto[]=
2703 /* Cloudy Colour Temperature : 6500K - 8000K */
2704 static struct reginfo sensor_WhiteB_Cloudy[]=
2715 /* ClearDay Colour Temperature : 5000K - 6500K */
2716 static struct reginfo sensor_WhiteB_ClearDay[]=
2728 /* Office Colour Temperature : 3500K - 5000K */
2729 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
2742 /* Home Colour Temperature : 2500K - 3500K */
2743 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
2755 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
2756 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
2760 #if CONFIG_SENSOR_Brightness
2761 static struct reginfo sensor_Brightness0[]=
2766 static struct reginfo sensor_Brightness1[]=
2771 static struct reginfo sensor_Brightness2[]=
2776 static struct reginfo sensor_Brightness3[]=
2781 static struct reginfo sensor_Brightness4[]=
2786 static struct reginfo sensor_Brightness5[]=
2790 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
2791 sensor_Brightness4, sensor_Brightness5,NULL,
2796 #if CONFIG_SENSOR_Effect
2797 static struct reginfo sensor_Effect_Normal[] =
2804 static struct reginfo sensor_Effect_WandB[] =
2813 static struct reginfo sensor_Effect_Sepia[] =
2822 static struct reginfo sensor_Effect_Negative[] =
2829 static struct reginfo sensor_Effect_Bluish[] =
2839 static struct reginfo sensor_Effect_Green[] =
2848 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
2849 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
2852 #if CONFIG_SENSOR_Exposure
2853 static struct reginfo sensor_Exposure0[]=
2858 static struct reginfo sensor_Exposure1[]=
2863 static struct reginfo sensor_Exposure2[]=
2868 static struct reginfo sensor_Exposure3[]=
2873 static struct reginfo sensor_Exposure4[]=
2878 static struct reginfo sensor_Exposure5[]=
2883 static struct reginfo sensor_Exposure6[]=
2888 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
2889 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
2892 #if CONFIG_SENSOR_Saturation
2893 static struct reginfo sensor_Saturation0[]=
2898 static struct reginfo sensor_Saturation1[]=
2903 static struct reginfo sensor_Saturation2[]=
2907 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
2910 #if CONFIG_SENSOR_Contrast
2911 static struct reginfo sensor_Contrast0[]=
2916 static struct reginfo sensor_Contrast1[]=
2921 static struct reginfo sensor_Contrast2[]=
2926 static struct reginfo sensor_Contrast3[]=
2931 static struct reginfo sensor_Contrast4[]=
2937 static struct reginfo sensor_Contrast5[]=
2942 static struct reginfo sensor_Contrast6[]=
2946 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
2947 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
2951 #if CONFIG_SENSOR_Mirror
2952 static struct reginfo sensor_MirrorOn[]=
2957 static struct reginfo sensor_MirrorOff[]=
2961 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
2963 #if CONFIG_SENSOR_Flip
2964 static struct reginfo sensor_FlipOn[]=
2969 static struct reginfo sensor_FlipOff[]=
2973 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
2976 #if CONFIG_SENSOR_Scene
2977 static struct reginfo sensor_SceneAuto[] =
2983 static struct reginfo sensor_SceneNight[] =
2986 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
3004 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
3007 #if CONFIG_SENSOR_DigitalZoom
3008 static struct reginfo sensor_Zoom0[] =
3013 static struct reginfo sensor_Zoom1[] =
3018 static struct reginfo sensor_Zoom2[] =
3024 static struct reginfo sensor_Zoom3[] =
3028 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
3030 static const struct v4l2_querymenu sensor_menus[] =
3032 #if CONFIG_SENSOR_WhiteBalance
3033 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
3034 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
3035 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
3038 #if CONFIG_SENSOR_Effect
3039 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
3040 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
3041 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
3044 #if CONFIG_SENSOR_Scene
3045 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
3048 #if CONFIG_SENSOR_Flash
3049 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
3050 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
3054 static const struct v4l2_queryctrl sensor_controls[] =
3056 #if CONFIG_SENSOR_WhiteBalance
3058 .id = V4L2_CID_DO_WHITE_BALANCE,
3059 .type = V4L2_CTRL_TYPE_MENU,
3060 .name = "White Balance Control",
3068 #if CONFIG_SENSOR_Brightness
3070 .id = V4L2_CID_BRIGHTNESS,
3071 .type = V4L2_CTRL_TYPE_INTEGER,
3072 .name = "Brightness Control",
3080 #if CONFIG_SENSOR_Effect
3082 .id = V4L2_CID_EFFECT,
3083 .type = V4L2_CTRL_TYPE_MENU,
3084 .name = "Effect Control",
3092 #if CONFIG_SENSOR_Exposure
3094 .id = V4L2_CID_EXPOSURE,
3095 .type = V4L2_CTRL_TYPE_INTEGER,
3096 .name = "Exposure Control",
3104 #if CONFIG_SENSOR_Saturation
3106 .id = V4L2_CID_SATURATION,
3107 .type = V4L2_CTRL_TYPE_INTEGER,
3108 .name = "Saturation Control",
3116 #if CONFIG_SENSOR_Contrast
3118 .id = V4L2_CID_CONTRAST,
3119 .type = V4L2_CTRL_TYPE_INTEGER,
3120 .name = "Contrast Control",
3128 #if CONFIG_SENSOR_Mirror
3130 .id = V4L2_CID_HFLIP,
3131 .type = V4L2_CTRL_TYPE_BOOLEAN,
3132 .name = "Mirror Control",
3140 #if CONFIG_SENSOR_Flip
3142 .id = V4L2_CID_VFLIP,
3143 .type = V4L2_CTRL_TYPE_BOOLEAN,
3144 .name = "Flip Control",
3152 #if CONFIG_SENSOR_Scene
3154 .id = V4L2_CID_SCENE,
3155 .type = V4L2_CTRL_TYPE_MENU,
3156 .name = "Scene Control",
3164 #if CONFIG_SENSOR_DigitalZoom
3166 .id = V4L2_CID_ZOOM_RELATIVE,
3167 .type = V4L2_CTRL_TYPE_INTEGER,
3168 .name = "DigitalZoom Control",
3174 .id = V4L2_CID_ZOOM_ABSOLUTE,
3175 .type = V4L2_CTRL_TYPE_INTEGER,
3176 .name = "DigitalZoom Control",
3184 #if CONFIG_SENSOR_Focus
3186 .id = V4L2_CID_FOCUS_RELATIVE,
3187 .type = V4L2_CTRL_TYPE_INTEGER,
3188 .name = "Focus Control",
3194 .id = V4L2_CID_FOCUS_ABSOLUTE,
3195 .type = V4L2_CTRL_TYPE_INTEGER,
3196 .name = "Focus Control",
3200 .default_value = 125,
3203 .id = V4L2_CID_FOCUS_AUTO,
3204 .type = V4L2_CTRL_TYPE_BOOLEAN,
3205 .name = "Focus Control",
3211 .id = V4L2_CID_FOCUS_CONTINUOUS,
3212 .type = V4L2_CTRL_TYPE_BOOLEAN,
3213 .name = "Focus Control",
3221 #if CONFIG_SENSOR_Flash
3223 .id = V4L2_CID_FLASH,
3224 .type = V4L2_CTRL_TYPE_MENU,
3225 .name = "Flash Control",
3234 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
3235 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
3236 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
3237 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
3238 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
3239 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
3240 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
3241 static int sensor_resume(struct soc_camera_device *icd);
3242 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
3243 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
3244 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
3245 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
3246 static int sensor_deactivate(struct i2c_client *client);
3248 static struct soc_camera_ops sensor_ops =
3250 .suspend = sensor_suspend,
3251 .resume = sensor_resume,
3252 .set_bus_param = sensor_set_bus_param,
3253 .query_bus_param = sensor_query_bus_param,
3254 .controls = sensor_controls,
3255 .menus = sensor_menus,
3256 .num_controls = ARRAY_SIZE(sensor_controls),
3257 .num_menus = ARRAY_SIZE(sensor_menus),
3260 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
3261 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
3262 .colorspace = _colorspace }
3264 #define JPG_FMT(_name, _depth, _fourcc) \
3265 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
3267 static const struct soc_camera_data_format sensor_colour_formats[] = {
3268 JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
3269 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
3271 enum sensor_work_state
3273 sensor_work_ready = 0,
3278 struct i2c_client *client;
3279 struct delayed_work dwork;
3280 enum sensor_work_state state;
3283 typedef struct sensor_info_priv_s
3297 unsigned char mirror; /* HFLIP */
3298 unsigned char flip; /* VFLIP */
3301 struct reginfo *winseqe_cur_addr;
3302 unsigned int pixfmt;
3303 unsigned int enable;
3304 unsigned int funmodule_state;
3305 } sensor_info_priv_t;
3309 struct sensor_parameter
3311 unsigned short int preview_maxlines;
3312 unsigned short int preview_exposure;
3313 unsigned short int preview_line_width;
3314 unsigned short int preview_gain;
3316 unsigned short int capture_framerate;
3317 unsigned short int preview_framerate;
3322 struct v4l2_subdev subdev;
3323 struct i2c_client *client;
3324 sensor_info_priv_t info_priv;
3325 struct sensor_parameter parameter;
3326 struct workqueue_struct *sensor_wq;
3327 struct sensor_work sensor_wk;
3328 struct mutex wq_lock;
3329 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
3330 #if CONFIG_SENSOR_I2C_NOSCHED
3331 atomic_t tasklock_cnt;
3333 struct rk29camera_platform_data *sensor_io_request;
3334 struct rk29camera_gpio_res *sensor_gpio_res;
3337 static struct sensor* to_sensor(const struct i2c_client *client)
3339 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
3342 static int sensor_task_lock(struct i2c_client *client, int lock)
3344 #if CONFIG_SENSOR_I2C_NOSCHED
3346 struct sensor *sensor = to_sensor(client);
3349 if (atomic_read(&sensor->tasklock_cnt) == 0) {
3350 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
3351 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
3355 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
3356 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
3357 goto sensor_task_lock_err;
3362 atomic_add(1, &sensor->tasklock_cnt);
3364 if (atomic_read(&sensor->tasklock_cnt) > 0) {
3365 atomic_sub(1, &sensor->tasklock_cnt);
3367 if (atomic_read(&sensor->tasklock_cnt) == 0)
3372 sensor_task_lock_err:
3380 /* sensor register write */
3381 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
3385 struct i2c_msg msg[1];
3388 buf[1] = reg & 0xFF;
3391 msg->addr = client->addr;
3392 msg->flags = client->flags;
3394 msg->len = sizeof(buf);
3395 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3396 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
3401 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
3402 err = i2c_transfer(client->adapter, msg, 1);
3407 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
3415 /* sensor register read */
3416 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
3420 struct i2c_msg msg[2];
3423 buf[1] = reg & 0xFF;
3425 msg[0].addr = client->addr;
3426 msg[0].flags = client->flags;
3428 msg[0].len = sizeof(buf);
3429 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3430 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
3432 msg[1].addr = client->addr;
3433 msg[1].flags = client->flags|I2C_M_RD;
3436 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3437 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
3441 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
3442 err = i2c_transfer(client->adapter, msg, 2);
3448 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
3456 /* write a array of registers */
3457 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
3461 #if CONFIG_SENSOR_Focus
3462 struct sensor *sensor = to_sensor(client);
3464 #if CONFIG_SENSOR_I2C_RDWRCHK
3469 if (sensor_task_lock(client, 1) < 0)
3470 goto sensor_write_array_end;
3471 while (regarray[i].reg != 0)
3473 #if CONFIG_SENSOR_Focus
3474 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
3475 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
3477 goto sensor_write_array_end;
3481 err = sensor_write(client, regarray[i].reg, regarray[i].val);
3485 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
3489 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
3491 goto sensor_write_array_end;
3494 #if CONFIG_SENSOR_I2C_RDWRCHK
3495 sensor_read(client, regarray[i].reg, &valchk);
3496 if (valchk != regarray[i].val)
3497 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
3504 #if CONFIG_SENSOR_Focus
3505 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
3506 || (regarray == sensor_init_data)) {
3507 sensor->info_priv.affm_reinit = 1;
3511 sensor_write_array_end:
3512 sensor_task_lock(client,0);
3515 #if CONFIG_SENSOR_I2C_RDWRCHK
3516 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
3524 while (regarray[i].reg != 0)
3526 sensor_read(client, regarray[i].reg, &valchk);
3527 if (valchk != regarray[i].val)
3528 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
3535 #if CONFIG_SENSOR_Focus
3542 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
3545 char read_tag=0xff,cnt;
3548 if (cmdinfo->validate_bit & 0x80) {
3549 if (sensor_write(client, CMD_TAG_Reg, cmdinfo->cmd_tag)) {
3550 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
3551 goto sensor_af_cmdset_err;
3553 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
3555 for (i=0; i<4; i++) {
3556 if (cmdinfo->validate_bit & (1<<i)) {
3557 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
3558 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]);
3559 goto sensor_af_cmdset_err;
3561 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]);
3565 if (sensor_write(client, CMD_TAG_Reg, 0xff)) {
3566 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
3567 goto sensor_af_cmdset_err;
3569 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
3572 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
3573 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
3574 goto sensor_af_cmdset_err;
3581 if (sensor_read(client,CMD_TAG_Reg,&read_tag)){
3582 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
3585 } while((read_tag != 0x00)&& (cnt++<100));
3587 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
3589 sensor_af_cmdset_err:
3593 static int sensor_af_idlechk(struct i2c_client *client)
3601 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3603 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3605 goto sensor_af_idlechk_end;
3608 if (state != S_IDLE) {
3609 sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
3613 } while((state != S_IDLE)&& (cnt<100));
3615 ret = (state == S_IDLE) ? 0 : -1;
3617 sensor_af_idlechk_end:
3621 static int sensor_af_single(struct i2c_client *client)
3626 if (sensor_af_idlechk(client))
3627 goto sensor_af_single_end;
3629 if (sensor_af_cmdset(client, SingleFocus_Cmd, NULL)) {
3630 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
3632 goto sensor_af_single_end;
3642 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3644 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3646 goto sensor_af_single_end;
3648 }while((state == S_FOCUSING) && (cnt<100));
3650 if (state != S_FOCUSED) {
3651 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
3653 goto sensor_af_single_end;
3656 sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
3657 sensor_af_single_end:
3661 static int sensor_af_const(struct i2c_client *client)
3665 if (sensor_af_idlechk(client))
3666 goto sensor_af_const_end;
3668 if (sensor_af_cmdset(client, ConstFocus_Cmd, NULL)) {
3669 SENSOR_TR("%s const focus mode set error!\n",SENSOR_NAME_STRING());
3671 goto sensor_af_const_end;
3673 sensor_af_const_end:
3676 static int sensor_af_pause2capture(struct i2c_client *client)
3681 if (sensor_af_cmdset(client, PauseFocus_Cmd, NULL)) {
3682 SENSOR_TR("%s pause focus mode set error!\n",SENSOR_NAME_STRING());
3684 goto sensor_af_pause_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_pause_end;
3700 }while((state != S_CAPTURE) && (cnt<100));
3702 if (state != S_CAPTURE) {
3703 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
3705 goto sensor_af_pause_end;
3707 sensor_af_pause_end:
3710 static int sensor_af_zoneupdate(struct i2c_client *client)
3714 if (sensor_af_idlechk(client))
3715 goto sensor_af_zoneupdate_end;
3717 if (sensor_af_cmdset(client, UpdateZone_Cmd, NULL)) {
3718 SENSOR_TR("%s update zone fail!\n",SENSOR_NAME_STRING());
3720 goto sensor_af_zoneupdate_end;
3723 sensor_af_zoneupdate_end:
3726 static int sensor_af_init(struct i2c_client *client)
3731 ret = sensor_write_array(client, sensor_af_firmware);
3733 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
3735 goto sensor_af_init_end;
3745 ret = sensor_read(client, STA_FOCUS_Reg, &state);
3747 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
3749 goto sensor_af_init_end;
3751 }while((state == S_STARTUP) && (cnt<100));
3753 if (state != S_IDLE) {
3754 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
3756 goto sensor_af_init_end;
3760 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
3764 static int sensor_af_wq_function(struct i2c_client *client)
3766 struct sensor *sensor = to_sensor(client);
3767 struct af_cmdinfo cmdinfo;
3768 int ret=0, focus_pos = 0xfe;
3770 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
3772 mutex_lock(&sensor->wq_lock);
3773 if (sensor_af_init(client)) {
3774 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
3777 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
3779 switch (sensor->info_priv.auto_focus)
3781 case SENSOR_AF_MODE_INFINITY:
3785 case SENSOR_AF_MODE_MACRO:
3787 if (focus_pos != 0x00)
3790 sensor_af_idlechk(client);
3791 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
3792 cmdinfo.cmd_para[0] = focus_pos;
3793 cmdinfo.validate_bit = 0x81;
3794 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
3797 case SENSOR_AF_MODE_AUTO:
3799 ret = sensor_af_single(client);
3802 case SENSOR_AF_MODE_CONTINUOUS:
3804 ret = sensor_af_const(client);
3807 case SENSOR_AF_MODE_CLOSE:
3814 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
3815 goto sensor_af_wq_function_end;
3819 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);
3822 sensor_af_wq_function_end:
3823 sensor->sensor_wk.state = sensor_work_ready;
3824 mutex_unlock(&sensor->wq_lock);
3827 static void sensor_af_workqueue(struct work_struct *work)
3829 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
3830 struct i2c_client *client = sensor_work->client;
3832 if (sensor_af_wq_function(client) < 0) {
3833 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
3837 static int sensor_parameter_record(struct i2c_client *client)
3839 u8 ret_l,ret_m,ret_h;
3841 struct sensor *sensor = to_sensor(client);
3843 sensor_write(client,0x3503,0x07); //stop AE/AG
3844 sensor_write(client,0x3406,0x01); //stop AWB
3846 sensor_read(client,0x3500,&ret_h);
3847 sensor_read(client,0x3501, &ret_m);
3848 sensor_read(client,0x3502, &ret_l);
3852 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
3853 sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
3854 sensor_read(client,0x350c, &ret_h);
3855 sensor_read(client,0x350d, &ret_l);
3856 sensor->parameter.preview_line_width = ret_h & 0xff;
3857 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
3858 //Read back AGC Gain for preview
3859 sensor_read(client,0x350b, &tp_l);
3860 sensor->parameter.preview_gain = tp_l;
3862 sensor->parameter.capture_framerate = 900;
3863 sensor->parameter.preview_framerate = 1500;
3865 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);
3868 static int sensor_ae_transfer(struct i2c_client *client)
3873 u16 ulCapture_Exposure;
3874 u32 ulCapture_Exposure_Gain;
3878 u8 reg_l = 0,reg_h =0;
3879 u16 Preview_Maxlines;
3881 u32 Capture_MaxLines;
3882 struct sensor *sensor = to_sensor(client);
3884 Preview_Maxlines = sensor->parameter.preview_line_width;
3885 Gain = sensor->parameter.preview_gain;
3886 sensor_read(client,0x350c, ®_h);
3887 sensor_read(client,0x350d, ®_l);
3888 Capture_MaxLines = reg_h & 0xff;
3889 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
3892 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
3894 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
3897 if(Preview_Maxlines == 0)
3898 Preview_Maxlines = 1;
3900 ulCapture_Exposure =
3901 (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
3902 iCapture_Gain = (Gain & 0x0f) + 16;
3904 iCapture_Gain = iCapture_Gain << 1;
3907 iCapture_Gain = iCapture_Gain << 1;
3910 iCapture_Gain = iCapture_Gain << 1;
3913 iCapture_Gain = iCapture_Gain << 1;
3915 ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
3916 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
3917 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
3918 if (ulCapture_Exposure > Lines_10ms)
3920 //ulCapture_Exposure *= 1.7;
3921 ulCapture_Exposure /= Lines_10ms;
3922 ulCapture_Exposure *= Lines_10ms;
3925 ulCapture_Exposure = Capture_MaxLines;
3926 //ulCapture_Exposure_Gain *= 1.5;
3928 if(ulCapture_Exposure == 0)
3929 ulCapture_Exposure = 1;
3930 iCapture_Gain = (ulCapture_Exposure_Gain*2/ulCapture_Exposure + 1)/2;
3931 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
3932 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
3933 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
3936 if (iCapture_Gain > 31) {
3938 iCapture_Gain = iCapture_Gain >> 1;
3940 if (iCapture_Gain > 31) {
3942 iCapture_Gain = iCapture_Gain >> 1;
3944 if (iCapture_Gain > 31) {
3946 iCapture_Gain = iCapture_Gain >> 1;
3948 if (iCapture_Gain > 31) {
3950 iCapture_Gain = iCapture_Gain >> 1;
3952 if (iCapture_Gain > 16)
3953 Gain |= ((iCapture_Gain -16) & 0x0f);
3956 // write the gain and exposure to 0x350* registers
3957 //m_iWrite0x350b=Gain;
3958 sensor_write(client,0x350b, Gain);
3959 //m_iWrite0x3502=ExposureLow;
3960 sensor_write(client,0x3502, ExposureLow);
3961 //m_iWrite0x3501=ExposureMid;
3962 sensor_write(client,0x3501, ExposureMid);
3963 //m_iWrite0x3500=ExposureHigh;
3964 sensor_write(client,0x3500, ExposureHigh);
3965 // SendToFile("Gain = 0x%x\r\n", Gain);
3966 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
3967 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
3968 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
3969 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
3970 //camera_timed_wait(200);
3971 //linzhk camera_timed_wait(500);
3973 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
3977 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
3979 struct soc_camera_link *icl = to_soc_camera_link(icd);
3982 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
3986 case Sensor_PowerDown:
3988 if (icl->powerdown) {
3989 ret = icl->powerdown(icd->pdev, on);
3990 if (ret == RK29_CAM_IO_SUCCESS) {
3994 icl->reset(icd->pdev);
3996 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
3998 goto sensor_power_end;
4005 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4006 struct sensor *sensor = to_sensor(client);
4008 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
4009 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
4015 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
4023 static int sensor_init(struct v4l2_subdev *sd, u32 val)
4025 struct i2c_client *client = sd->priv;
4026 struct soc_camera_device *icd = client->dev.platform_data;
4027 struct sensor *sensor = to_sensor(client);
4028 const struct v4l2_queryctrl *qctrl;
4032 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
4034 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
4036 goto sensor_INIT_ERR;
4040 if (sensor_task_lock(client,1)<0)
4041 goto sensor_INIT_ERR;
4042 ret = sensor_write(client, 0x3008, 0x80);
4044 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
4046 goto sensor_INIT_ERR;
4049 mdelay(5); //delay 5 microseconds
4050 /* check if it is an sensor sensor */
4051 ret = sensor_read(client, 0x300a, &value);
4053 SENSOR_TR("read chip id high byte failed\n");
4055 goto sensor_INIT_ERR;
4058 pid |= (value << 8);
4060 ret = sensor_read(client, 0x300b, &value);
4062 SENSOR_TR("read chip id low byte failed\n");
4064 goto sensor_INIT_ERR;
4067 pid |= (value & 0xff);
4068 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
4070 if (pid == SENSOR_ID) {
4071 sensor->model = SENSOR_V4L2_IDENT;
4073 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
4075 goto sensor_INIT_ERR;
4078 ret = sensor_write_array(client, sensor_init_data);
4081 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
4082 goto sensor_INIT_ERR;
4084 sensor_task_lock(client,0);
4085 //icd->user_width = SENSOR_INIT_WIDTH;
4086 //icd->user_height = SENSOR_INIT_HEIGHT;
4087 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
4088 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
4090 /* sensor sensor information for initialization */
4091 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4093 sensor->info_priv.whiteBalance = qctrl->default_value;
4094 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
4096 sensor->info_priv.brightness = qctrl->default_value;
4097 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4099 sensor->info_priv.effect = qctrl->default_value;
4100 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
4102 sensor->info_priv.exposure = qctrl->default_value;
4104 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
4106 sensor->info_priv.saturation = qctrl->default_value;
4107 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
4109 sensor->info_priv.contrast = qctrl->default_value;
4110 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
4112 sensor->info_priv.mirror = qctrl->default_value;
4113 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
4115 sensor->info_priv.flip = qctrl->default_value;
4116 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
4118 sensor->info_priv.scene = qctrl->default_value;
4119 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
4121 sensor->info_priv.digitalzoom = qctrl->default_value;
4123 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
4124 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
4126 sensor->info_priv.focus = qctrl->default_value;
4128 #if CONFIG_SENSOR_Flash
4129 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
4131 sensor->info_priv.flash = qctrl->default_value;
4133 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);
4137 sensor_task_lock(client,0);
4138 sensor_deactivate(client);
4141 static int sensor_deactivate(struct i2c_client *client)
4143 struct soc_camera_device *icd = client->dev.platform_data;
4145 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
4147 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
4148 sensor_task_lock(client, 1);
4149 sensor_write(client, 0x3017, 0x00); // FREX,VSYNC,HREF,PCLK,D9-D6
4150 sensor_write(client, 0x3018, 0x03); // D5-D0
4151 sensor_write(client,0x3019,0X00); // STROBE,SDA
4152 sensor_task_lock(client, 0);
4153 sensor_ioctrl(icd, Sensor_PowerDown, 1);
4154 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
4155 icd->user_width = SENSOR_INIT_WIDTH;
4156 icd->user_height = SENSOR_INIT_HEIGHT;
4160 static struct reginfo sensor_power_down_sequence[]=
4164 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
4167 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4169 if (pm_msg.event == PM_EVENT_SUSPEND) {
4170 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
4171 ret = sensor_write_array(client, sensor_power_down_sequence) ;
4173 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
4176 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
4178 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
4183 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
4190 static int sensor_resume(struct soc_camera_device *icd)
4194 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
4196 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
4200 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
4204 static int sensor_set_bus_param(struct soc_camera_device *icd,
4205 unsigned long flags)
4211 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
4213 struct soc_camera_link *icl = to_soc_camera_link(icd);
4214 unsigned long flags = SENSOR_BUS_PARAM;
4216 return soc_camera_apply_sensor_flags(icl, flags);
4219 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
4221 struct i2c_client *client = sd->priv;
4222 struct soc_camera_device *icd = client->dev.platform_data;
4223 struct sensor *sensor = to_sensor(client);
4224 struct v4l2_pix_format *pix = &f->fmt.pix;
4226 pix->width = icd->user_width;
4227 pix->height = icd->user_height;
4228 pix->pixelformat = sensor->info_priv.pixfmt;
4229 pix->field = V4L2_FIELD_NONE;
4230 pix->colorspace = V4L2_COLORSPACE_JPEG;
4234 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
4238 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
4240 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
4242 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
4244 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
4246 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
4251 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
4255 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
4259 if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
4261 } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
4266 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
4269 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
4271 struct i2c_client *client = sd->priv;
4272 struct sensor *sensor = to_sensor(client);
4273 struct v4l2_pix_format *pix = &f->fmt.pix;
4274 const struct v4l2_queryctrl *qctrl;
4275 struct soc_camera_device *icd = client->dev.platform_data;
4276 struct reginfo *winseqe_set_addr=NULL;
4277 int ret = 0, set_w,set_h;
4279 if (sensor->info_priv.pixfmt != pix->pixelformat) {
4280 switch (pix->pixelformat)
4282 case V4L2_PIX_FMT_YUYV:
4284 winseqe_set_addr = sensor_ClrFmt_YUYV;
4287 case V4L2_PIX_FMT_UYVY:
4289 winseqe_set_addr = sensor_ClrFmt_UYVY;
4295 if (winseqe_set_addr != NULL) {
4296 sensor_write_array(client, winseqe_set_addr);
4297 sensor->info_priv.pixfmt = pix->pixelformat;
4299 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
4301 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
4306 set_h = pix->height;
4308 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
4310 winseqe_set_addr = sensor_qcif;
4314 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
4316 winseqe_set_addr = sensor_qvga;
4320 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
4322 winseqe_set_addr = sensor_cif;
4326 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
4328 winseqe_set_addr = sensor_vga;
4332 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
4334 winseqe_set_addr = sensor_svga;
4338 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
4340 winseqe_set_addr = sensor_xga;
4344 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
4346 winseqe_set_addr = sensor_720p;
4350 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
4352 winseqe_set_addr = sensor_sxga;
4356 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
4358 winseqe_set_addr = sensor_uxga;
4362 else if (((set_w <= 1920) && (set_h <= 1080)) && sensor_1080p[0].reg)
4364 winseqe_set_addr = sensor_1080p;
4368 else if (((set_w <= 2048) && (set_h <= 1536)) && sensor_qxga[0].reg)
4370 winseqe_set_addr = sensor_qxga;
4374 else if (((set_w <= 2592) && (set_h <= 1944)) && sensor_qsxga[0].reg)
4376 winseqe_set_addr = sensor_qsxga;
4382 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
4383 set_w = SENSOR_INIT_WIDTH;
4384 set_h = SENSOR_INIT_HEIGHT;
4386 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
4389 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
4391 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
4392 sensor_parameter_record(client);
4393 #if CONFIG_SENSOR_Flash
4394 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4395 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
4396 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
4399 }else { /* ddl@rock-chips.com : Video */
4400 #if CONFIG_SENSOR_Flash
4401 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4402 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
4403 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
4408 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
4409 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
4410 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
4411 ret |= sensor_write_array(client,sensor_init_data);
4412 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
4416 ret |= sensor_write_array(client, winseqe_set_addr);
4418 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
4419 #if CONFIG_SENSOR_Flash
4420 if (sensor_fmt_capturechk(sd,f) == true) {
4421 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4422 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
4423 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
4427 goto sensor_s_fmt_end;
4430 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
4432 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
4433 sensor_ae_transfer(client);
4434 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4435 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4436 if (sensor->info_priv.whiteBalance != 0) {
4437 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4438 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
4440 #if CONFIG_SENSOR_Focus
4441 sensor_af_zoneupdate(client);
4442 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS) {
4443 sensor_af_const(client);
4446 sensor->info_priv.snap2preview = true;
4447 } else if (sensor_fmt_videochk(sd,f) == true) { /* ddl@rock-chips.com : Video */
4448 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4449 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4451 sensor->info_priv.video2preview = true;
4452 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
4453 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4454 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4455 if (sensor->info_priv.snap2preview == true) {
4456 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4457 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
4459 #if CONFIG_SENSOR_Focus
4460 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
4461 sensor_af_const(client);
4464 sensor->info_priv.video2preview = false;
4465 sensor->info_priv.snap2preview = false;
4468 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
4472 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
4476 pix->height = set_h;
4481 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
4483 struct v4l2_pix_format *pix = &f->fmt.pix;
4484 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
4485 pix->pixelformat == V4L2_PIX_FMT_YUYV;
4488 * With Bayer format enforce even side lengths, but let the user play
4489 * with the starting pixel
4492 if (pix->height > SENSOR_MAX_HEIGHT)
4493 pix->height = SENSOR_MAX_HEIGHT;
4494 else if (pix->height < SENSOR_MIN_HEIGHT)
4495 pix->height = SENSOR_MIN_HEIGHT;
4497 pix->height = ALIGN(pix->height, 2);
4499 if (pix->width > SENSOR_MAX_WIDTH)
4500 pix->width = SENSOR_MAX_WIDTH;
4501 else if (pix->width < SENSOR_MIN_WIDTH)
4502 pix->width = SENSOR_MIN_WIDTH;
4504 pix->width = ALIGN(pix->width, 2);
4509 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
4511 struct i2c_client *client = sd->priv;
4513 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
4516 if (id->match.addr != client->addr)
4519 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
4524 #if CONFIG_SENSOR_Brightness
4525 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4527 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4529 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4531 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
4533 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
4535 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4538 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4542 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4546 #if CONFIG_SENSOR_Effect
4547 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4549 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4551 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4553 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
4555 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
4557 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4560 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4564 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4568 #if CONFIG_SENSOR_Exposure
4569 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4571 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4573 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4575 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
4577 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
4579 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4582 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4586 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4590 #if CONFIG_SENSOR_Saturation
4591 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4593 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4595 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4597 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
4599 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
4601 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4604 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4608 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4612 #if CONFIG_SENSOR_Contrast
4613 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4615 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4617 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4619 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
4621 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
4623 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4626 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4630 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4634 #if CONFIG_SENSOR_Mirror
4635 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4637 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4639 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4641 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
4643 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
4645 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4648 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4652 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4656 #if CONFIG_SENSOR_Flip
4657 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4659 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4661 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4663 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
4665 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
4667 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4670 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4674 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4678 #if CONFIG_SENSOR_Scene
4679 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4681 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4683 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4685 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
4687 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
4689 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4692 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4696 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4700 #if CONFIG_SENSOR_WhiteBalance
4701 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4703 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4705 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4707 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
4709 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
4711 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4714 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4718 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4722 #if CONFIG_SENSOR_DigitalZoom
4723 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
4725 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4726 struct sensor *sensor = to_sensor(client);
4727 const struct v4l2_queryctrl *qctrl_info;
4728 int digitalzoom_cur, digitalzoom_total;
4730 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
4734 digitalzoom_cur = sensor->info_priv.digitalzoom;
4735 digitalzoom_total = qctrl_info->maximum;
4737 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
4739 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
4743 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
4745 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
4749 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
4751 *value = digitalzoom_total - digitalzoom_cur;
4754 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
4756 *value = 0 - digitalzoom_cur;
4759 digitalzoom_cur += *value;
4761 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
4763 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
4765 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4768 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
4775 #if CONFIG_SENSOR_Focus
4776 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4778 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4779 struct sensor *sensor = to_sensor(client);
4780 const struct v4l2_queryctrl *qctrl_info;
4781 struct af_cmdinfo cmdinfo;
4784 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
4788 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
4789 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
4791 if (sensor_af_idlechk(client))
4792 goto sensor_set_focus_absolute_end;
4794 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
4795 cmdinfo.cmd_para[0] = value;
4796 cmdinfo.validate_bit = 0x81;
4797 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
4798 //ret |= sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
4799 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
4802 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4806 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
4807 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
4810 sensor_set_focus_absolute_end:
4813 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4815 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4816 struct sensor *sensor = to_sensor(client);
4817 const struct v4l2_queryctrl *qctrl_info;
4818 struct af_cmdinfo cmdinfo;
4821 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
4825 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
4826 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
4828 if (sensor_af_idlechk(client))
4829 goto sensor_set_focus_relative_end;
4832 cmdinfo.cmd_tag = StepFocus_Near_Tag;
4833 } else if (value < 0) {
4834 cmdinfo.cmd_tag = StepFocus_Far_Tag;
4836 cmdinfo.validate_bit = 0x80;
4837 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
4839 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
4842 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4846 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
4847 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
4849 sensor_set_focus_relative_end:
4853 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4855 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4856 struct sensor *sensor = to_sensor(client);
4859 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
4862 case SENSOR_AF_MODE_AUTO:
4864 ret = sensor_af_single(client);
4868 case SENSOR_AF_MODE_MACRO:
4870 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
4874 case SENSOR_AF_MODE_INFINITY:
4876 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
4880 case SENSOR_AF_MODE_CONTINUOUS:
4882 ret = sensor_af_const(client);
4886 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4891 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
4894 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
4895 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
4902 #if CONFIG_SENSOR_Flash
4903 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4905 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
4906 if (value == 3) { /* ddl@rock-chips.com: torch */
4907 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
4909 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
4911 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4915 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4920 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
4922 struct i2c_client *client = sd->priv;
4923 struct sensor *sensor = to_sensor(client);
4924 const struct v4l2_queryctrl *qctrl;
4926 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
4930 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
4936 case V4L2_CID_BRIGHTNESS:
4938 ctrl->value = sensor->info_priv.brightness;
4941 case V4L2_CID_SATURATION:
4943 ctrl->value = sensor->info_priv.saturation;
4946 case V4L2_CID_CONTRAST:
4948 ctrl->value = sensor->info_priv.contrast;
4951 case V4L2_CID_DO_WHITE_BALANCE:
4953 ctrl->value = sensor->info_priv.whiteBalance;
4956 case V4L2_CID_EXPOSURE:
4958 ctrl->value = sensor->info_priv.exposure;
4961 case V4L2_CID_HFLIP:
4963 ctrl->value = sensor->info_priv.mirror;
4966 case V4L2_CID_VFLIP:
4968 ctrl->value = sensor->info_priv.flip;
4979 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
4981 struct i2c_client *client = sd->priv;
4982 struct sensor *sensor = to_sensor(client);
4983 struct soc_camera_device *icd = client->dev.platform_data;
4984 const struct v4l2_queryctrl *qctrl;
4987 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
4991 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
4997 #if CONFIG_SENSOR_Brightness
4998 case V4L2_CID_BRIGHTNESS:
5000 if (ctrl->value != sensor->info_priv.brightness)
5002 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
5006 sensor->info_priv.brightness = ctrl->value;
5011 #if CONFIG_SENSOR_Exposure
5012 case V4L2_CID_EXPOSURE:
5014 if (ctrl->value != sensor->info_priv.exposure)
5016 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
5020 sensor->info_priv.exposure = ctrl->value;
5025 #if CONFIG_SENSOR_Saturation
5026 case V4L2_CID_SATURATION:
5028 if (ctrl->value != sensor->info_priv.saturation)
5030 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
5034 sensor->info_priv.saturation = ctrl->value;
5039 #if CONFIG_SENSOR_Contrast
5040 case V4L2_CID_CONTRAST:
5042 if (ctrl->value != sensor->info_priv.contrast)
5044 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
5048 sensor->info_priv.contrast = ctrl->value;
5053 #if CONFIG_SENSOR_WhiteBalance
5054 case V4L2_CID_DO_WHITE_BALANCE:
5056 if (ctrl->value != sensor->info_priv.whiteBalance)
5058 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
5062 sensor->info_priv.whiteBalance = ctrl->value;
5067 #if CONFIG_SENSOR_Mirror
5068 case V4L2_CID_HFLIP:
5070 if (ctrl->value != sensor->info_priv.mirror)
5072 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
5074 sensor->info_priv.mirror = ctrl->value;
5079 #if CONFIG_SENSOR_Flip
5080 case V4L2_CID_VFLIP:
5082 if (ctrl->value != sensor->info_priv.flip)
5084 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
5086 sensor->info_priv.flip = ctrl->value;
5097 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
5099 const struct v4l2_queryctrl *qctrl;
5100 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5101 struct sensor *sensor = to_sensor(client);
5103 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
5107 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
5111 switch (ext_ctrl->id)
5113 case V4L2_CID_SCENE:
5115 ext_ctrl->value = sensor->info_priv.scene;
5118 case V4L2_CID_EFFECT:
5120 ext_ctrl->value = sensor->info_priv.effect;
5123 case V4L2_CID_ZOOM_ABSOLUTE:
5125 ext_ctrl->value = sensor->info_priv.digitalzoom;
5128 case V4L2_CID_ZOOM_RELATIVE:
5132 case V4L2_CID_FOCUS_ABSOLUTE:
5136 case V4L2_CID_FOCUS_RELATIVE:
5140 case V4L2_CID_FLASH:
5142 ext_ctrl->value = sensor->info_priv.flash;
5150 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
5152 const struct v4l2_queryctrl *qctrl;
5153 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5154 struct sensor *sensor = to_sensor(client);
5157 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
5161 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
5166 switch (ext_ctrl->id)
5168 #if CONFIG_SENSOR_Scene
5169 case V4L2_CID_SCENE:
5171 if (ext_ctrl->value != sensor->info_priv.scene)
5173 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
5175 sensor->info_priv.scene = ext_ctrl->value;
5180 #if CONFIG_SENSOR_Effect
5181 case V4L2_CID_EFFECT:
5183 if (ext_ctrl->value != sensor->info_priv.effect)
5185 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
5187 sensor->info_priv.effect= ext_ctrl->value;
5192 #if CONFIG_SENSOR_DigitalZoom
5193 case V4L2_CID_ZOOM_ABSOLUTE:
5195 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5198 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
5200 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
5202 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
5204 sensor->info_priv.digitalzoom += val_offset;
5206 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
5211 case V4L2_CID_ZOOM_RELATIVE:
5213 if (ext_ctrl->value)
5215 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
5217 sensor->info_priv.digitalzoom += ext_ctrl->value;
5219 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
5224 #if CONFIG_SENSOR_Focus
5225 case V4L2_CID_FOCUS_ABSOLUTE:
5227 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5230 if (sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value) == 0) {
5231 if (ext_ctrl->value == qctrl->minimum) {
5232 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
5233 } else if (ext_ctrl->value == qctrl->maximum) {
5234 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
5236 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
5242 case V4L2_CID_FOCUS_RELATIVE:
5244 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5247 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
5250 case V4L2_CID_FOCUS_AUTO:
5252 if (ext_ctrl->value == 1) {
5253 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
5254 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
5255 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
5259 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
5260 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
5261 if (ext_ctrl->value == 0)
5262 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
5266 case V4L2_CID_FOCUS_CONTINUOUS:
5268 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
5269 if (ext_ctrl->value == 1) {
5270 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0) {
5271 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
5272 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
5276 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
5279 if (ext_ctrl->value == 0)
5280 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
5285 #if CONFIG_SENSOR_Flash
5286 case V4L2_CID_FLASH:
5288 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
5290 sensor->info_priv.flash = ext_ctrl->value;
5292 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
5303 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
5305 struct i2c_client *client = sd->priv;
5306 struct soc_camera_device *icd = client->dev.platform_data;
5307 int i, error_cnt=0, error_idx=-1;
5310 for (i=0; i<ext_ctrl->count; i++) {
5311 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
5318 error_idx = ext_ctrl->count;
5320 if (error_idx != -1) {
5321 ext_ctrl->error_idx = error_idx;
5328 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
5330 struct i2c_client *client = sd->priv;
5331 struct soc_camera_device *icd = client->dev.platform_data;
5332 int i, error_cnt=0, error_idx=-1;
5334 for (i=0; i<ext_ctrl->count; i++) {
5335 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
5342 error_idx = ext_ctrl->count;
5344 if (error_idx != -1) {
5345 ext_ctrl->error_idx = error_idx;
5352 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
5354 struct i2c_client *client = sd->priv;
5355 struct sensor *sensor = to_sensor(client);
5356 struct soc_camera_device *icd = client->dev.platform_data;
5357 struct v4l2_format fmt;
5360 sensor->info_priv.enable = 1;
5361 #if CONFIG_SENSOR_Focus
5362 fmt.fmt.pix.width = icd->user_width;
5363 fmt.fmt.pix.height = icd->user_height;
5364 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
5365 if (sensor_fmt_capturechk(sd, &fmt) == false) {
5366 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
5367 if (sensor->sensor_wq != NULL) {
5368 mutex_lock(&sensor->wq_lock);
5369 if (sensor->sensor_wk.state == sensor_working) {
5370 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
5371 mutex_unlock(&sensor->wq_lock);
5372 goto sensor_s_stream_end;
5374 sensor->sensor_wk.state = sensor_working;
5375 mutex_unlock(&sensor->wq_lock);
5376 sensor->sensor_wk.client = client;
5377 INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
5378 queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
5380 sensor->info_priv.affm_reinit = 0;
5384 } else if (enable == 0) {
5385 sensor->info_priv.enable = 0;
5386 #if CONFIG_SENSOR_Focus
5387 flush_work(&(sensor->sensor_wk.dwork.work));
5388 mutex_lock(&sensor->wq_lock);
5389 sensor->sensor_wk.state = sensor_work_ready;
5390 mutex_unlock(&sensor->wq_lock);
5394 sensor_s_stream_end:
5398 /* Interface active, can use i2c. If it fails, it can indeed mean, that
5399 * this wasn't our capture interface, so, we wait for the right one */
5400 static int sensor_video_probe(struct soc_camera_device *icd,
5401 struct i2c_client *client)
5405 struct sensor *sensor = to_sensor(client);
5407 /* We must have a parent by now. And it cannot be a wrong one.
5408 * So this entire test is completely redundant. */
5409 if (!icd->dev.parent ||
5410 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
5413 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
5415 goto sensor_video_probe_err;
5418 ret = sensor_write(client, 0x3012, 0x80);
5420 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
5422 goto sensor_video_probe_err;
5424 mdelay(5); //delay 5 microseconds
5426 /* check if it is an sensor sensor */
5427 ret = sensor_read(client, 0x300a, &value);
5429 SENSOR_TR("read chip id high byte failed\n");
5431 goto sensor_video_probe_err;
5434 pid |= (value << 8);
5436 ret = sensor_read(client, 0x300b, &value);
5438 SENSOR_TR("read chip id low byte failed\n");
5440 goto sensor_video_probe_err;
5443 pid |= (value & 0xff);
5444 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
5445 if (pid == SENSOR_ID) {
5446 sensor->model = SENSOR_V4L2_IDENT;
5448 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
5450 goto sensor_video_probe_err;
5453 icd->formats = sensor_colour_formats;
5454 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
5458 sensor_video_probe_err:
5462 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
5464 struct i2c_client *client = sd->priv;
5465 struct soc_camera_device *icd = client->dev.platform_data;
5466 struct sensor *sensor = to_sensor(client);
5469 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
5472 case RK29_CAM_SUBDEV_DEACTIVATE:
5474 sensor_deactivate(client);
5477 case RK29_CAM_SUBDEV_IOREQUEST:
5479 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
5481 if (sensor->sensor_io_request != NULL) {
5482 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
5483 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
5484 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
5485 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
5486 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
5487 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
5490 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
5492 goto sensor_ioctl_end;
5495 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
5497 #if CONFIG_SENSOR_Flash
5498 if (sensor->sensor_gpio_res) {
5499 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
5500 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
5501 for (i = 0; i < icd->ops->num_controls; i++) {
5502 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
5503 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
5506 sensor->info_priv.flash = 0xff;
5507 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
5515 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
5525 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
5526 .init = sensor_init,
5527 .g_ctrl = sensor_g_control,
5528 .s_ctrl = sensor_s_control,
5529 .g_ext_ctrls = sensor_g_ext_controls,
5530 .s_ext_ctrls = sensor_s_ext_controls,
5531 .g_chip_ident = sensor_g_chip_ident,
5532 .ioctl = sensor_ioctl,
5535 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
5536 .s_fmt = sensor_s_fmt,
5537 .g_fmt = sensor_g_fmt,
5538 .try_fmt = sensor_try_fmt,
5539 .s_stream = sensor_s_stream,
5542 static struct v4l2_subdev_ops sensor_subdev_ops = {
5543 .core = &sensor_subdev_core_ops,
5544 .video = &sensor_subdev_video_ops,
5547 static int sensor_probe(struct i2c_client *client,
5548 const struct i2c_device_id *did)
5550 struct sensor *sensor;
5551 struct soc_camera_device *icd = client->dev.platform_data;
5552 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
5553 struct soc_camera_link *icl;
5556 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
5558 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
5562 icl = to_soc_camera_link(icd);
5564 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
5568 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
5569 dev_warn(&adapter->dev,
5570 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
5574 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
5578 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
5580 /* Second stage probe - when a capture adapter is there */
5581 icd->ops = &sensor_ops;
5582 icd->y_skip_top = 0;
5583 #if CONFIG_SENSOR_I2C_NOSCHED
5584 atomic_set(&sensor->tasklock_cnt,0);
5587 ret = sensor_video_probe(icd, client);
5590 i2c_set_clientdata(client, NULL);
5594 #if CONFIG_SENSOR_Focus
5595 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
5596 if (sensor->sensor_wq == NULL)
5597 SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
5598 mutex_init(&sensor->wq_lock);
5599 sensor->sensor_wk.state = sensor_work_ready;
5603 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
5607 static int sensor_remove(struct i2c_client *client)
5609 struct sensor *sensor = to_sensor(client);
5610 struct soc_camera_device *icd = client->dev.platform_data;
5612 #if CONFIG_SENSOR_Focus
5613 if (sensor->sensor_wq) {
5614 destroy_workqueue(sensor->sensor_wq);
5615 sensor->sensor_wq = NULL;
5620 i2c_set_clientdata(client, NULL);
5621 client->driver = NULL;
5627 static const struct i2c_device_id sensor_id[] = {
5628 {SENSOR_NAME_STRING(), 0 },
5631 MODULE_DEVICE_TABLE(i2c, sensor_id);
5633 static struct i2c_driver sensor_i2c_driver = {
5635 .name = SENSOR_NAME_STRING(),
5637 .probe = sensor_probe,
5638 .remove = sensor_remove,
5639 .id_table = sensor_id,
5642 static int __init sensor_mod_init(void)
5644 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
5645 return i2c_add_driver(&sensor_i2c_driver);
5648 static void __exit sensor_mod_exit(void)
5650 i2c_del_driver(&sensor_i2c_driver);
5653 device_initcall_sync(sensor_mod_init);
5654 module_exit(sensor_mod_exit);
5656 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
5657 MODULE_AUTHOR("ddl <kernel@rock-chips>");
5658 MODULE_LICENSE("GPL");