2 * Driver for HM5065 CMOS Image Sensor from Himax
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/miscdevice.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/soc_camera.h>
22 #include <plat/rk_camera.h>
24 //#include <linux/wine_sysinf.h>
31 module_param(debug, int, S_IRUGO|S_IWUSR);
33 #define dprintk(level, fmt, arg...) do { \
35 printk(KERN_WARNING fmt , ## arg); } while (0)
37 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
38 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
40 #define _CONS(a,b) a##b
41 #define CONS(a,b) _CONS(a,b)
44 #define _STR(x) __STR(x)
45 #define STR(x) _STR(x)
47 #define MIN(x,y) ((x<y) ? x: y)
48 #define MAX(x,y) ((x>y) ? x: y)
50 /* Sensor Driver Configuration */
51 #define SENSOR_NAME RK29_CAM_SENSOR_HM5065
52 #define SENSOR_V4L2_IDENT V4L2_IDENT_HM5065
53 #define SENSOR_ID 0x039E
54 #define SENSOR_MIN_WIDTH 176
55 #define SENSOR_MIN_HEIGHT 144
56 #define SENSOR_MAX_WIDTH 2592
57 #define SENSOR_MAX_HEIGHT 1944
58 #define SENSOR_INIT_WIDTH 800 //640/* Sensor pixel size for sensor_init_data array */
59 #define SENSOR_INIT_HEIGHT 600 //480
60 #define SENSOR_INIT_WINSEQADR sensor_svga
61 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_YUYV8_2X8
63 #define CONFIG_SENSOR_WhiteBalance 1
64 #define CONFIG_SENSOR_Brightness 1
65 #define CONFIG_SENSOR_Contrast 1
66 #define CONFIG_SENSOR_Saturation 1
67 #define CONFIG_SENSOR_Effect 1
68 #define CONFIG_SENSOR_Scene 1
69 #define CONFIG_SENSOR_DigitalZoom 0
70 #define CONFIG_SENSOR_Exposure 0
71 #define CONFIG_SENSOR_Flash 1
72 #define CONFIG_SENSOR_Mirror 0
73 #define CONFIG_SENSOR_Flip 0
74 #define CONFIG_SENSOR_Focus 1
76 #define CONFIG_SENSOR_I2C_SPEED 100000 /* 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
81 #define CONFIG_SENSOR_WRITE_REGS 1
82 #define WRITE_REGS_NUM 3
84 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
85 SOCAM_HSYNC_ACTIVE_HIGH| SOCAM_VSYNC_ACTIVE_LOW |\
86 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
88 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
89 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
90 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
91 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
92 #define COLOR_TEMPERATURE_OFFICE_DN 3500
93 #define COLOR_TEMPERATURE_OFFICE_UP 5000
94 #define COLOR_TEMPERATURE_HOME_DN 2500
95 #define COLOR_TEMPERATURE_HOME_UP 3500
97 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
98 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
100 #define SENSOR_AF_IS_ERR (0x00<<0)
101 #define SENSOR_AF_IS_OK (0x01<<0)
102 #define SENSOR_INIT_IS_ERR (0x00<<28)
103 #define SENSOR_INIT_IS_OK (0x01<<28)
105 #if CONFIG_SENSOR_Focus
106 /*#define SENSOR_AF_MODE_INFINITY 0
107 #define SENSOR_AF_MODE_MACRO 1
108 #define SENSOR_AF_MODE_FIXED 2
109 #define SENSOR_AF_MODE_AUTO 3
110 #define SENSOR_AF_MODE_CONTINUOUS 4
111 #define SENSOR_AF_MODE_CLOSE 5*/
112 #define SENSOR_AF_MODE_AUTO 0
113 #define SENSOR_AF_MODE_CLOSE 1
114 #define SENSOR_AF_MODE_CONTINUOUS 2
117 #if CONFIG_SENSOR_Focus
118 /* hm5065 VCM Command and Status Registers */
119 #define AF_TARGETPOS_REG_H 0x0700
120 #define AF_TARGETPOS_REG_L 0x0701
122 #define AF_MODES_REG 0x070A
123 #define AF_AUTOCMDS_REG 0x070b
124 #define AF_LENSPOS_REG_H 0x06F0
125 #define AF_LENSPOS_REG_L 0x06F1
126 #define AF_MANUCMDS_REG 0x070c
127 #define AF_FINISHCHK_REG 0x07ae //[0]: false [1] : true
129 #define FACE_LC 0x0714 //[0] : focus , [1] : AE Â
\93\84Ó
130 #define FACE_START_XH 0x0715
131 #define FACE_START_XL 0x0716
132 #define FACE_SIZE_XH 0x0717
133 #define FACE_SIZE_XL 0x0718
134 #define FACE_START_YH 0x0719
135 #define FACE_START_YL 0x071A
136 #define FACE_SIZE_YH 0x071B
137 #define FACE_SIZE_YL 0x071C
140 #define AF_ZONE0_WEIGHT 0x0808
141 #define AF_ZONE1_WEIGHT 0x0809
142 #define AF_ZONE2_WEIGHT 0x080A
143 #define AF_ZONE3_WEIGHT 0x080B
144 #define AF_ZONE4_WEIGHT 0x080C
145 #define AF_ZONE5_WEIGHT 0x080D
146 #define AF_ZONE6_WEIGHT 0x080E
150 //flash off in fixed time to prevent from too hot , zyc
152 struct soc_camera_device *icd;
153 struct hrtimer timer;
155 static enum hrtimer_restart flash_off_func(struct hrtimer *timer);
157 static struct flash_timer flash_off_timer;
158 //for user defined if user want to customize the series , zyc
159 #ifdef CONFIG_HM5065_USER_DEFINED_SERIES
160 #include "hm5065_user_series.c"
164 static struct reginfo sensor_init_data[] =
167 {0xffff,0x01}, // 2013-03-01 Start new patch
1681 {0xffff,0x00}, // 2013-03-01 end
1682 {SEQUENCE_END, 0x00}
1685 static struct reginfo sensor_init_data1[] =
1716 {0x0041,0x00},//00:5M 03:SVGA
1814 #if 0 // 03-06 old LSC
2076 #if 1 //new LSC start - 0306
2337 #endif //new LSC end
2428 {0x0081,0x55}, //6E->55
2440 {SEQUENCE_END, 0x00}
2443 static struct reginfo sensor_init_data2[] =
2470 {0x0756,0x00},//03->00
2494 #endif // old af @ vcm end -03-06
2495 #if 1 //03-06 new af @ vcm
2554 {0x0040,0x01}, //AB2
2555 {0x0041,0x03}, //SVGA mode
2559 {SEQUENCE_END, 0x00}
2560 }; //sensor_init_data2 end
2564 /* 720p 15fps @ 1280x720 */
2566 static struct reginfo sensor_720p[]=
2569 {SEQUENCE_END, 0x00}
2572 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
2574 static struct reginfo sensor_1080p[]=
2577 {SEQUENCE_END, 0x00}
2580 /* 2592X1944 QSXGA */
2581 static struct reginfo sensor_qsxga[] =
2596 {SEQUENCE_END, 0x00}
2598 /* 2048*1536 QXGA */
2599 static struct reginfo sensor_qxga[] =
2601 {SEQUENCE_END, 0x00}
2604 /* 1600X1200 UXGA */
2605 static struct reginfo sensor_uxga[] =
2618 {SEQUENCE_END, 0x00}
2621 /* 1280X1024 SXGA */
2622 static struct reginfo sensor_sxga[] =
2635 {SEQUENCE_END, 0x00}
2638 static struct reginfo sensor_xga[] =
2655 {SEQUENCE_END, 0x00}
2658 static struct reginfo sensor_svga[] =
2676 {SEQUENCE_END, 0x00}
2680 static struct reginfo sensor_vga[] =
2692 {SEQUENCE_END, 0x00}
2695 static struct reginfo sensor_cif[] =
2698 {SEQUENCE_END, 0x00}
2702 static struct reginfo sensor_qvga[] =
2705 {SEQUENCE_END, 0x00}
2709 static struct reginfo sensor_qcif[] =
2712 {SEQUENCE_END, 0x00}
2715 static struct reginfo sensor_ClrFmt_YUYV[]=
2718 {SEQUENCE_END, 0x00}
2721 static struct reginfo sensor_ClrFmt_UYVY[]=
2724 {SEQUENCE_END, 0x00}
2728 #if CONFIG_SENSOR_WhiteBalance
2729 static struct reginfo sensor_WhiteB_Auto[]=
2732 {SEQUENCE_END, 0x00}
2734 /* Cloudy Colour Temperature : 6500K - 8000K */
2735 static struct reginfo sensor_WhiteB_Cloudy[]=
2738 {SEQUENCE_END, 0x00}
2740 /* ClearDay Colour Temperature : 5000K - 6500K */
2741 static struct reginfo sensor_WhiteB_ClearDay[]=
2744 {SEQUENCE_END, 0x00}
2746 /* Office Colour Temperature : 3500K - 5000K */
2747 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
2750 {SEQUENCE_END, 0x00}
2752 /* Home Colour Temperature : 2500K - 3500K */
2753 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
2756 {SEQUENCE_END, 0x00}
2758 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
2759 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
2763 #if CONFIG_SENSOR_Brightness
2764 static struct reginfo sensor_Brightness0[]=
2767 {SEQUENCE_END, 0x00}
2769 static struct reginfo sensor_Brightness1[]=
2772 {SEQUENCE_END, 0x00}
2775 static struct reginfo sensor_Brightness2[]=
2778 {SEQUENCE_END, 0x00}
2780 static struct reginfo sensor_Brightness3[]=
2783 {SEQUENCE_END, 0x00}
2785 static struct reginfo sensor_Brightness4[]=
2788 {SEQUENCE_END, 0x00}
2791 static struct reginfo sensor_Brightness5[]=
2794 {SEQUENCE_END, 0x00}
2796 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
2797 sensor_Brightness4, sensor_Brightness5,NULL,
2802 #if CONFIG_SENSOR_Effect
2803 static struct reginfo sensor_Effect_Normal[] =
2809 {SEQUENCE_END, 0x00}
2811 static struct reginfo sensor_Effect_WandB[] =
2817 {SEQUENCE_END, 0x00}
2819 static struct reginfo sensor_Effect_Sepia[] =
2825 {SEQUENCE_END, 0x00}
2828 static struct reginfo sensor_Effect_Negative[] =
2835 {SEQUENCE_END, 0x00}
2836 };static struct reginfo sensor_Effect_Bluish[] =
2843 {SEQUENCE_END, 0x00}
2846 static struct reginfo sensor_Effect_Green[] =
2853 {SEQUENCE_END, 0x00}
2855 /*static struct reginfo sensor_Effect_Reddish[] =
2859 {SEQUENCE_END, 0x00}
2862 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
2863 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
2866 #if CONFIG_SENSOR_Exposure
2867 static struct reginfo sensor_Exposure0[]=
2869 {SEQUENCE_END, 0x00}
2871 static struct reginfo sensor_Exposure1[]=
2873 {SEQUENCE_END, 0x00}
2875 static struct reginfo sensor_Exposure2[]=
2877 {SEQUENCE_END, 0x00}
2880 static struct reginfo sensor_Exposure3[]=
2882 {SEQUENCE_END, 0x00}
2884 static struct reginfo sensor_Exposure4[]=
2886 {SEQUENCE_END, 0x00}
2888 static struct reginfo sensor_Exposure5[]=
2890 {SEQUENCE_END, 0x00}
2892 static struct reginfo sensor_Exposure6[]=
2894 {SEQUENCE_END, 0x00}
2896 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
2897 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
2900 #if CONFIG_SENSOR_Saturation
2901 static struct reginfo sensor_Saturation0[]=
2904 {SEQUENCE_END, 0x00}
2907 static struct reginfo sensor_Saturation1[]=
2910 {SEQUENCE_END, 0x00}
2913 static struct reginfo sensor_Saturation2[]=
2916 {SEQUENCE_END, 0x00}
2918 static struct reginfo sensor_Saturation3[]=
2921 {SEQUENCE_END, 0x00}
2924 static struct reginfo sensor_Saturation4[]=
2927 {SEQUENCE_END, 0x00}
2929 static struct reginfo sensor_Saturation5[]=
2932 {SEQUENCE_END, 0x00}
2935 static struct reginfo sensor_Saturation6[]=
2938 {SEQUENCE_END, 0x00}
2940 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2,sensor_Saturation3, sensor_Saturation4, sensor_Saturation5,sensor_Saturation6, NULL,};
2943 #if CONFIG_SENSOR_Contrast
2944 static struct reginfo sensor_Contrast0[]=
2947 {SEQUENCE_END, 0x00}
2950 static struct reginfo sensor_Contrast1[]=
2953 {SEQUENCE_END, 0x00}
2955 static struct reginfo sensor_Contrast2[]=
2958 {SEQUENCE_END, 0x00}
2961 static struct reginfo sensor_Contrast3[]=
2964 {SEQUENCE_END, 0x00}
2967 static struct reginfo sensor_Contrast4[]=
2970 {SEQUENCE_END, 0x00}
2974 static struct reginfo sensor_Contrast5[]=
2977 {SEQUENCE_END, 0x00}
2980 static struct reginfo sensor_Contrast6[]=
2983 {SEQUENCE_END, 0x00}
2985 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
2986 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
2990 #if CONFIG_SENSOR_Mirror
2991 static struct reginfo sensor_MirrorOn[]=
2993 {SEQUENCE_END, 0x00}
2995 static struct reginfo sensor_MirrorOff[]=
2997 {SEQUENCE_END, 0x00}
2999 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
3001 #if CONFIG_SENSOR_Flip
3002 static struct reginfo sensor_FlipOn[]=
3004 {SEQUENCE_END, 0x00}
3007 static struct reginfo sensor_FlipOff[]=
3009 {SEQUENCE_END, 0x00}
3011 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
3014 #if CONFIG_SENSOR_Scene
3015 static struct reginfo sensor_SceneAuto[] =
3018 {SEQUENCE_END, 0x00}
3020 static struct reginfo sensor_SceneNight[] =
3022 //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
3024 {SEQUENCE_END, 0x00}
3026 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
3029 #if CONFIG_SENSOR_DigitalZoom
3030 static struct reginfo sensor_Zoom0[] =
3032 {SEQUENCE_END, 0x00}
3034 static struct reginfo sensor_Zoom1[] =
3036 {SEQUENCE_END, 0x00}
3039 static struct reginfo sensor_Zoom2[] =
3041 {SEQUENCE_END, 0x00}
3045 static struct reginfo sensor_Zoom3[] =
3047 {SEQUENCE_END, 0x00}
3049 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
3051 static const struct v4l2_querymenu sensor_menus[] =
3053 #if CONFIG_SENSOR_WhiteBalance
3054 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
3055 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
3056 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
3059 #if CONFIG_SENSOR_Effect
3060 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
3061 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
3062 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
3065 #if CONFIG_SENSOR_Scene
3066 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
3069 #if CONFIG_SENSOR_Flash
3070 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
3071 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
3075 static struct v4l2_queryctrl sensor_controls[] =
3077 #if CONFIG_SENSOR_WhiteBalance
3079 .id = V4L2_CID_DO_WHITE_BALANCE,
3080 .type = V4L2_CTRL_TYPE_MENU,
3081 .name = "White Balance Control",
3089 #if CONFIG_SENSOR_Brightness
3091 .id = V4L2_CID_BRIGHTNESS,
3092 .type = V4L2_CTRL_TYPE_INTEGER,
3093 .name = "Brightness Control",
3101 #if CONFIG_SENSOR_Effect
3103 .id = V4L2_CID_EFFECT,
3104 .type = V4L2_CTRL_TYPE_MENU,
3105 .name = "Effect Control",
3113 #if CONFIG_SENSOR_Exposure
3115 .id = V4L2_CID_EXPOSURE,
3116 .type = V4L2_CTRL_TYPE_INTEGER,
3117 .name = "Exposure Control",
3125 #if CONFIG_SENSOR_Saturation
3127 .id = V4L2_CID_SATURATION,
3128 .type = V4L2_CTRL_TYPE_INTEGER,
3129 .name = "Saturation Control",
3137 #if CONFIG_SENSOR_Contrast
3139 .id = V4L2_CID_CONTRAST,
3140 .type = V4L2_CTRL_TYPE_INTEGER,
3141 .name = "Contrast Control",
3149 #if CONFIG_SENSOR_Mirror
3151 .id = V4L2_CID_HFLIP,
3152 .type = V4L2_CTRL_TYPE_BOOLEAN,
3153 .name = "Mirror Control",
3161 #if CONFIG_SENSOR_Flip
3163 .id = V4L2_CID_VFLIP,
3164 .type = V4L2_CTRL_TYPE_BOOLEAN,
3165 .name = "Flip Control",
3173 #if CONFIG_SENSOR_Scene
3175 .id = V4L2_CID_SCENE,
3176 .type = V4L2_CTRL_TYPE_MENU,
3177 .name = "Scene Control",
3185 #if CONFIG_SENSOR_DigitalZoom
3187 .id = V4L2_CID_ZOOM_RELATIVE,
3188 .type = V4L2_CTRL_TYPE_INTEGER,
3189 .name = "DigitalZoom Control",
3195 .id = V4L2_CID_ZOOM_ABSOLUTE,
3196 .type = V4L2_CTRL_TYPE_INTEGER,
3197 .name = "DigitalZoom Control",
3205 #if CONFIG_SENSOR_Focus
3207 .id = V4L2_CID_FOCUS_RELATIVE,
3208 .type = V4L2_CTRL_TYPE_INTEGER,
3209 .name = "Focus Control",
3215 .id = V4L2_CID_FOCUS_ABSOLUTE,
3216 .type = V4L2_CTRL_TYPE_INTEGER,
3217 .name = "Focus Control",
3221 .default_value = 125,
3224 .id = V4L2_CID_FOCUSZONE,
3225 .type = V4L2_CTRL_TYPE_INTEGER,
3226 .name = "FocusZone Control",
3232 .id = V4L2_CID_FOCUS_AUTO,
3233 .type = V4L2_CTRL_TYPE_BOOLEAN,
3234 .name = "Focus Control",
3240 .id = V4L2_CID_FOCUS_CONTINUOUS,
3241 .type = V4L2_CTRL_TYPE_BOOLEAN,
3242 .name = "Focus Control",
3250 #if CONFIG_SENSOR_Flash
3252 .id = V4L2_CID_FLASH,
3253 .type = V4L2_CTRL_TYPE_MENU,
3254 .name = "Flash Control",
3263 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
3264 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
3265 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
3266 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
3267 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
3268 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
3269 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
3270 static int sensor_resume(struct soc_camera_device *icd);
3271 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
3272 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
3273 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
3274 #if CONFIG_SENSOR_WhiteBalance
3275 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
3277 static int sensor_deactivate(struct i2c_client *client);
3278 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
3279 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
3281 static struct soc_camera_ops sensor_ops =
3283 .suspend = sensor_suspend,
3284 .resume = sensor_resume,
3285 .set_bus_param = sensor_set_bus_param,
3286 .query_bus_param = sensor_query_bus_param,
3287 .controls = sensor_controls,
3288 .menus = sensor_menus,
3289 .num_controls = ARRAY_SIZE(sensor_controls),
3290 .num_menus = ARRAY_SIZE(sensor_menus),
3292 /* only one fixed colorspace per pixelcode */
3293 struct sensor_datafmt {
3294 enum v4l2_mbus_pixelcode code;
3295 enum v4l2_colorspace colorspace;
3298 /* Find a data format by a pixel code in an array */
3299 static const struct sensor_datafmt *sensor_find_datafmt(
3300 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
3304 for (i = 0; i < n; i++)
3305 if (fmt[i].code == code)
3311 static const struct sensor_datafmt sensor_colour_fmts[] = {
3312 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
3313 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
3319 WqCmd_af_special_pos,
3323 WqCmd_af_continues_pause,
3324 WqCmd_af_return_idle,
3326 enum sensor_wq_result
3334 struct i2c_client *client;
3335 struct delayed_work dwork;
3336 enum sensor_wq_cmd cmd;
3337 wait_queue_head_t done;
3338 enum sensor_wq_result result;
3344 typedef struct sensor_info_priv_s
3360 unsigned char mirror; /* HFLIP */
3361 unsigned char flip; /* VFLIP */
3362 struct reginfo *winseqe_cur_addr;
3363 struct sensor_datafmt fmt;
3364 unsigned int enable;
3365 unsigned int funmodule_state;
3366 } sensor_info_priv_t;
3370 struct sensor_parameter
3377 struct v4l2_subdev subdev;
3378 struct i2c_client *client;
3379 sensor_info_priv_t info_priv;
3380 struct sensor_parameter parameter;
3381 struct workqueue_struct *sensor_wq;
3382 struct mutex wq_lock;
3383 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
3384 #if CONFIG_SENSOR_I2C_NOSCHED
3385 atomic_t tasklock_cnt;
3387 struct rk29camera_platform_data *sensor_io_request;
3388 struct rk29camera_gpio_res *sensor_gpio_res;
3391 static struct sensor* to_sensor(const struct i2c_client *client)
3393 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
3396 static int sensor_task_lock(struct i2c_client *client, int lock)
3398 #if CONFIG_SENSOR_I2C_NOSCHED
3400 struct sensor *sensor = to_sensor(client);
3403 if (atomic_read(&sensor->tasklock_cnt) == 0) {
3404 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
3405 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
3409 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
3410 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
3411 goto sensor_task_lock_err;
3416 atomic_add(1, &sensor->tasklock_cnt);
3418 if (atomic_read(&sensor->tasklock_cnt) > 0) {
3419 atomic_sub(1, &sensor->tasklock_cnt);
3421 if (atomic_read(&sensor->tasklock_cnt) == 0)
3426 sensor_task_lock_err:
3435 #if CONFIG_SENSOR_WRITE_REGS
3436 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
3439 struct i2c_msg msg[1];
3442 msg->addr = client->addr;
3443 msg->flags = client->flags;
3444 msg->buf = reg_info;
3445 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3446 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
3452 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
3453 err = i2c_transfer(client->adapter, msg, 1);
3457 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
3471 /* sensor register write */
3472 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
3476 struct i2c_msg msg[1];
3479 buf[1] = reg & 0xFF;
3482 msg->addr = client->addr;
3483 msg->flags = client->flags;
3485 msg->len = sizeof(buf);
3486 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3487 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
3492 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
3493 err = i2c_transfer(client->adapter, msg, 1);
3498 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
3506 /* sensor register read */
3507 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
3511 struct i2c_msg msg[2];
3514 buf[1] = reg & 0xFF;
3516 msg[0].addr = client->addr;
3517 msg[0].flags = client->flags;
3519 msg[0].len = sizeof(buf);
3520 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3521 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
3523 msg[1].addr = client->addr;
3524 msg[1].flags = client->flags|I2C_M_RD;
3527 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
3528 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
3532 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
3533 err = i2c_transfer(client->adapter, msg, 2);
3539 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
3547 /* write a array of registers */
3548 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
3552 #if CONFIG_SENSOR_Focus
3553 struct sensor *sensor = to_sensor(client);
3555 #if CONFIG_SENSOR_I2C_RDWRCHK
3558 #if CONFIG_SENSOR_WRITE_REGS
3565 if (sensor_task_lock(client, 1) < 0)
3566 goto sensor_write_array_end;
3567 while (regarray[i].reg != SEQUENCE_END) {
3570 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
3571 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
3573 goto sensor_write_array_end;
3577 #if CONFIG_SENSOR_WRITE_REGS
3581 reg_length = 0x0001;
3583 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
3586 if(reg_num >= WRITE_REGS_NUM)
3593 size_num = reg_num + 1;
3595 ptemp = phead = (u8*)kmalloc((size_num+10)*sizeof(u8),GFP_KERNEL);
3597 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
3599 err = sensor_write(client, regarray[i].reg, regarray[i].val);
3601 *phead = regarray[j].reg >> 8;
3602 *(ptemp+1) = regarray[j].reg & 0xFF;
3604 for( ; reg_num > 0; reg_num --, j++) {
3605 *ptemp ++ = regarray[j].val;
3609 err = sensor_write_regs(client, ptemp,size_num);
3613 err = sensor_write(client, regarray[i].reg, regarray[i].val);
3616 err = sensor_write(client, regarray[i].reg, regarray[i].val);
3621 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
3625 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
3627 goto sensor_write_array_end;
3630 #if CONFIG_SENSOR_I2C_RDWRCHK
3631 sensor_read(client, regarray[i].reg, &valchk);
3632 if (valchk != regarray[i].val)
3633 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
3639 #if CONFIG_SENSOR_Focus
3640 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
3641 || (regarray == sensor_init_data)) {
3642 sensor->info_priv.affm_reinit = 1;
3646 sensor_write_array_end:
3647 sensor_task_lock(client,0);
3650 #if CONFIG_SENSOR_I2C_RDWRCHK
3651 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
3659 while (regarray[i].reg != 0)
3661 sensor_read(client, regarray[i].reg, &valchk);
3662 if (valchk != regarray[i].val)
3663 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
3670 #if CONFIG_SENSOR_Focus
3677 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
3681 sensor_af_cmdset_err:
3685 static int sensor_af_idlechk(struct i2c_client *client)
3689 sensor_write(client,0x070A, 0x00);
3690 sensor_write(client,0x0700, 0x03);
3691 sensor_write(client,0x0701, 0xFF);
3692 sensor_write(client,0x070C, 0x00);
3693 sensor_write(client,0x070C, 0x07);
3695 sensor_af_idlechk_end:
3698 static int sensor_af_touch_zone(struct i2c_client *client, int *zone_pos)
3704 *(zone_pos+1) += 1000;
3705 *(zone_pos+2) += 1000;
3706 *(zone_pos+3) += 1000;
3708 xstart = ((*zone_pos + *(zone_pos+2))>>1)*800/2000;
3709 ystart = ((*(zone_pos+1) + *(zone_pos+3))>>1)*600/2000;
3723 ret |= sensor_write(client, AF_ZONE0_WEIGHT,0x01);
3724 ret |= sensor_write(client, AF_ZONE1_WEIGHT,0x00);
3725 ret |= sensor_write(client, AF_ZONE2_WEIGHT,0x00);
3726 ret |= sensor_write(client, AF_ZONE3_WEIGHT,0x00);
3727 ret |= sensor_write(client, AF_ZONE4_WEIGHT,0x00);
3728 ret |= sensor_write(client, AF_ZONE5_WEIGHT,0x00);
3729 ret |= sensor_write(client, AF_ZONE6_WEIGHT,0x00);
3730 ret |= sensor_write(client, FACE_LC,0x03);
3732 ret |= sensor_write(client, FACE_START_XH, ((xstart&0xff00)>>8));
3733 ret |= sensor_write(client, FACE_START_XL, xstart&0xff);
3734 ret |= sensor_write(client, FACE_START_YH, ((ystart&0xff00)>>8));
3735 ret |= sensor_write(client, FACE_START_YL, ystart&0xff);
3737 ret |= sensor_write(client, FACE_SIZE_XH, 0x01);
3738 ret |= sensor_write(client, FACE_SIZE_XL, 0x40);
3739 ret |= sensor_write(client, FACE_SIZE_YH, 0x01);
3740 ret |= sensor_write(client, FACE_SIZE_YL, 0x40);
3742 SENSOR_DG("%s touch focus zone at (%d,%d)\n",SENSOR_NAME_STRING(), xstart,ystart);
3747 static int sensor_af_single(struct i2c_client *client)
3751 struct sensor *sensor = to_sensor(client);
3753 ret |= sensor_write(client,AF_MODES_REG,0x03);
3754 ret |= sensor_write(client,AF_AUTOCMDS_REG,0x01);
3756 ret |= sensor_write(client,AF_AUTOCMDS_REG,0x02);
3760 sensor_read(client,AF_FINISHCHK_REG,&state);
3762 }while ((state != 1) && (cnt<100));
3765 sensor_read(client, AF_LENSPOS_REG_H,&sensor->parameter.af_pos[0]);
3766 sensor_read(client, AF_LENSPOS_REG_L,&sensor->parameter.af_pos[1]);
3769 SENSOR_DG("%s single focus, state: %d cnt: %d\n",SENSOR_NAME_STRING(),state,cnt);
3770 sensor_af_single_end:
3774 static int sensor_af_const(struct i2c_client *client)
3778 ret = sensor_write(client, AF_MODES_REG, 0x01);
3780 SENSOR_DG("%s const focus\n",SENSOR_NAME_STRING());
3783 static int sensor_af_const_pause (struct i2c_client *client)
3787 struct sensor *sensor = to_sensor(client);
3789 sensor_read(client, 0x07ae, &status);
3792 sensor_read(client, AF_LENSPOS_REG_H,&sensor->parameter.af_pos[0]);
3793 sensor_read(client, AF_LENSPOS_REG_L,&sensor->parameter.af_pos[1]);
3795 sensor_af_single(client);
3798 SENSOR_DG("%s const focus pause\n",SENSOR_NAME_STRING());
3801 static int sensor_af_special_pos (struct i2c_client *client)
3803 struct sensor *sensor = to_sensor(client);
3806 sensor_write(client,0x070A, 0x00);
3807 sensor_write(client,0x0734, sensor->parameter.af_pos[0]& 0xFF);
3808 sensor_write(client,0x0735, sensor->parameter.af_pos[1] & 0xFF);
3809 sensor_write(client,0x070C, 0x00);
3811 sensor_write(client,0x070C, 0x05);
3815 static int sensor_af_init(struct i2c_client *client)
3821 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
3825 static int sensor_af_downfirmware(struct i2c_client *client)
3827 struct sensor *sensor = to_sensor(client);
3829 struct soc_camera_device *icd = client->dev.platform_data;
3830 struct v4l2_mbus_framefmt mf;
3832 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
3834 if (sensor_af_init(client)) {
3835 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
3838 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
3840 mf.width = icd->user_width;
3841 mf.height = icd->user_height;
3842 mf.code = sensor->info_priv.fmt.code;
3843 mf.colorspace = sensor->info_priv.fmt.colorspace;
3844 mf.field = V4L2_FIELD_NONE;
3846 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
3847 ret = sensor_af_const(client);
3849 switch (sensor->info_priv.auto_focus)
3851 case SENSOR_AF_MODE_AUTO:
3853 ret = sensor_af_single(client);
3856 case SENSOR_AF_MODE_CLOSE:
3861 case SENSOR_AF_MODE_CONTINUOUS:
3863 ret = sensor_af_const(client);
3868 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
3869 goto sensor_af_downfirmware_end;
3873 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
3876 sensor_af_downfirmware_end:
3880 static void sensor_af_workqueue(struct work_struct *work)
3882 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
3883 struct i2c_client *client = sensor_work->client;
3884 struct sensor *sensor = to_sensor(client);
3885 struct af_cmdinfo cmdinfo;
3887 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
3889 mutex_lock(&sensor->wq_lock);
3891 switch (sensor_work->cmd)
3895 if (sensor_af_downfirmware(client) < 0) {
3896 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
3900 case WqCmd_af_single:
3902 sensor_af_touch_zone(client,sensor_work->zone_pos);
3904 if (sensor_af_single(client) < 0) {
3905 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
3906 sensor_work->result = WqRet_fail;
3908 sensor_work->result = WqRet_success;
3913 case WqCmd_af_special_pos:
3915 if (sensor_af_special_pos(client)< 0)
3916 sensor_work->result = WqRet_fail;
3918 sensor_work->result = WqRet_success;
3922 case WqCmd_af_near_pos:
3924 sensor_af_idlechk(client);
3925 cmdinfo.cmd_tag = StepFocus_Near_Tag;
3926 cmdinfo.validate_bit = 0x80;
3927 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
3928 sensor_work->result = WqRet_fail;
3930 sensor_work->result = WqRet_success;
3933 case WqCmd_af_far_pos:
3935 sensor_af_idlechk(client);
3936 cmdinfo.cmd_tag = StepFocus_Far_Tag;
3937 cmdinfo.validate_bit = 0x80;
3938 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
3939 sensor_work->result = WqRet_fail;
3941 sensor_work->result = WqRet_success;
3945 case WqCmd_af_continues:
3947 if (sensor_af_const(client) < 0)
3948 sensor_work->result = WqRet_fail;
3950 sensor_work->result = WqRet_success;
3953 case WqCmd_af_continues_pause:
3955 if (sensor_af_const_pause(client) < 0)
3956 sensor_work->result = WqRet_fail;
3958 sensor_work->result = WqRet_success;
3961 case WqCmd_af_return_idle:
3963 if (sensor_af_idlechk(client) < 0)
3964 sensor_work->result = WqRet_fail;
3966 sensor_work->result = WqRet_success;
3970 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
3974 if (sensor_work->wait == false) {
3975 kfree((void*)sensor_work);
3977 wake_up(&sensor_work->done);
3979 mutex_unlock(&sensor->wq_lock);
3983 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait, int *zone_pos)
3985 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3986 struct sensor *sensor = to_sensor(client);
3987 struct sensor_work *wk;
3990 if (sensor->sensor_wq == NULL) {
3992 goto sensor_af_workqueue_set_end;
3995 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) != SENSOR_AF_IS_OK) {
3996 if (cmd != WqCmd_af_init) {
3997 SENSOR_TR("%s %s cmd(%d) ingore,because af module isn't ready!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3999 goto sensor_af_workqueue_set_end;
4003 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
4005 wk->client = client;
4006 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
4008 wk->result = WqRet_inval;
4011 init_waitqueue_head(&wk->done);
4014 if (*zone_pos || *(zone_pos+1) || *(zone_pos+2) || *(zone_pos+3)) {
4015 memcpy(wk->zone_pos,zone_pos, sizeof(wk->zone_pos));
4019 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
4021 /* ddl@rock-chips.com:
4022 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
4023 * As a result any other ioctl calls will proceed very, very slowly since each call
4024 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
4025 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
4028 mutex_unlock(&icd->video_lock);
4029 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(2500)) == 0) {
4030 SENSOR_TR("%s %s cmd(%d) is timeout!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4034 mutex_lock(&icd->video_lock);
4038 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4041 sensor_af_workqueue_set_end:
4045 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
4047 struct soc_camera_link *icl = to_soc_camera_link(icd);
4050 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
4055 case Sensor_PowerDown:
4057 if (icl->powerdown) {
4058 ret = icl->powerdown(icd->pdev, on);
4059 //printk("=============================<<<<222RK30_PIN1_PB7's value is %d\n",gpio_get_value(RK30_PIN1_PB7));
4060 if (ret == RK29_CAM_IO_SUCCESS) {
4064 icl->reset(icd->pdev);
4067 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
4069 goto sensor_power_end;
4077 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4078 struct sensor *sensor = to_sensor(client);
4080 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
4081 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
4083 //flash off after 2 secs
4084 hrtimer_cancel(&(flash_off_timer.timer));
4085 hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
4092 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
4101 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
4102 struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
4103 sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
4104 SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
4109 static int sensor_init(struct v4l2_subdev *sd, u32 val)
4111 struct i2c_client *client = v4l2_get_subdevdata(sd);
4112 struct soc_camera_device *icd = client->dev.platform_data;
4113 struct sensor *sensor = to_sensor(client);
4114 const struct v4l2_queryctrl *qctrl;
4115 const struct sensor_datafmt *fmt;
4119 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
4121 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
4122 //if (sensor_ioctrl(icd, Sensor_PowerDown, 1) < 0) {
4124 goto sensor_INIT_ERR;
4128 if (sensor_task_lock(client,1)<0)
4129 goto sensor_INIT_ERR;
4131 ret = sensor_write(client, 0x3008, 0x80);
4133 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
4135 goto sensor_INIT_ERR;
4138 mdelay(5); //delay 5 microseconds
4139 /* check if it is an sensor sensor */
4141 ret = sensor_read(client, 0x0000, &value);
4143 SENSOR_TR("read chip id high byte failed\n");
4145 goto sensor_INIT_ERR;
4147 printk("jzw 5065:value=0x%02x\n",value);
4148 pid |= (value << 8);
4150 ret = sensor_read(client, 0x0001, &value);
4152 SENSOR_TR("read chip id low byte failed\n");
4154 goto sensor_INIT_ERR;
4156 printk("jzw 506522:value=0x%02x\n",value);
4157 pid |= (value & 0xff);
4158 SENSOR_DG("\n %s pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
4160 if (pid == SENSOR_ID) {
4161 sensor->model = SENSOR_V4L2_IDENT;
4163 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
4165 goto sensor_INIT_ERR;
4168 // 写两次init_date,尽量避免打开摄像头绿屏的问题,Edit by zhansb@20120524
4169 SENSOR_DG("write 1 time\n");
4170 ret = sensor_write_array(client, sensor_init_data);
4172 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
4173 goto sensor_INIT_ERR;
4176 ret = sensor_write_array(client, sensor_init_data1);
4178 SENSOR_TR("error: %s initial1 failed\n",SENSOR_NAME_STRING());
4179 goto sensor_INIT_ERR;
4182 ret = sensor_write_array(client, sensor_init_data2);
4184 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
4185 goto sensor_INIT_ERR;
4188 // sensor_write(client, 0x070a, 0x01);
4189 ret = sensor_read(client, 0x0010, &value);
4192 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
4193 goto sensor_INIT_ERR;
4195 printk("<<<<addr 0010 's value is %d\n",value);
4199 sensor_task_lock(client,0);
4200 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
4201 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
4203 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
4205 goto sensor_INIT_ERR;
4207 sensor->info_priv.fmt = *fmt;
4210 /* sensor sensor information for initialization */
4211 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4213 sensor->info_priv.whiteBalance = qctrl->default_value;
4215 // 解决进摄像头后第一次拍的照片跟后续的拍的照片白平衡相差较大的问题,add by zhansb@120614
4216 #if CONFIG_SENSOR_WhiteBalance
4217 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
4219 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
4221 sensor->info_priv.brightness = qctrl->default_value;
4222 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4224 sensor->info_priv.effect = qctrl->default_value;
4225 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
4227 sensor->info_priv.exposure = qctrl->default_value;
4229 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
4231 sensor->info_priv.saturation = qctrl->default_value;
4232 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
4234 sensor->info_priv.contrast = qctrl->default_value;
4235 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
4237 sensor->info_priv.mirror = qctrl->default_value;
4238 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
4240 sensor->info_priv.flip = qctrl->default_value;
4241 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
4243 sensor->info_priv.scene = qctrl->default_value;
4244 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
4246 sensor->info_priv.digitalzoom = qctrl->default_value;
4248 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
4249 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
4251 sensor->info_priv.focus = qctrl->default_value;
4253 #if CONFIG_SENSOR_Flash
4254 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
4256 sensor->info_priv.flash = qctrl->default_value;
4257 flash_off_timer.icd = icd;
4258 flash_off_timer.timer.function = flash_off_func;
4260 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);
4262 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
4266 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
4267 sensor_task_lock(client,0);
4268 sensor_deactivate(client);
4269 printk("init error============================================================\n");
4272 static int sensor_deactivate(struct i2c_client *client)
4274 struct soc_camera_device *icd = client->dev.platform_data;
4275 struct sensor *sensor = to_sensor(client);
4277 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
4279 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
4280 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
4284 sensor_ioctrl(icd, Sensor_PowerDown, 1);
4288 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
4289 icd->user_width = SENSOR_INIT_WIDTH;
4290 icd->user_height = SENSOR_INIT_HEIGHT;
4291 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
4295 static struct reginfo sensor_power_down_sequence[]=
4300 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
4303 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4305 if (pm_msg.event == PM_EVENT_SUSPEND) {
4306 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
4307 ret = sensor_write_array(client, sensor_power_down_sequence) ;
4309 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
4313 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
4315 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
4321 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
4328 static int sensor_resume(struct soc_camera_device *icd)
4332 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
4334 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
4338 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
4342 static int sensor_set_bus_param(struct soc_camera_device *icd,
4343 unsigned long flags)
4349 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
4351 struct soc_camera_link *icl = to_soc_camera_link(icd);
4352 unsigned long flags = SENSOR_BUS_PARAM;
4354 return soc_camera_apply_sensor_flags(icl, flags);
4357 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4359 struct i2c_client *client = v4l2_get_subdevdata(sd);
4360 struct soc_camera_device *icd = client->dev.platform_data;
4361 struct sensor *sensor = to_sensor(client);
4363 mf->width = icd->user_width;
4364 mf->height = icd->user_height;
4365 mf->code = sensor->info_priv.fmt.code;
4366 mf->colorspace = sensor->info_priv.fmt.colorspace;
4367 mf->field = V4L2_FIELD_NONE;
4371 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4375 if ((mf->width == 1024) && (mf->height == 768)) {
4377 } else if ((mf->width == 1280) && (mf->height == 1024)) {
4379 } else if ((mf->width == 1600) && (mf->height == 1200)) {
4381 } else if ((mf->width == 2048) && (mf->height == 1536)) {
4383 } else if ((mf->width == 2592) && (mf->height == 1944)) {
4388 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
4392 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4396 if ((mf->width == 1280) && (mf->height == 720)) {
4398 } else if ((mf->width == 1920) && (mf->height == 1080)) {
4403 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
4406 static int sensor_s_fmt(struct v4l2_subdev *sd,struct v4l2_mbus_framefmt *mf)
4408 char value,af_status=0,af_pos_h=0,af_pos_l=0;
4409 struct i2c_client *client = v4l2_get_subdevdata(sd);
4410 const struct sensor_datafmt *fmt;
4411 struct sensor *sensor = to_sensor(client);
4412 const struct v4l2_queryctrl *qctrl;
4413 struct soc_camera_device *icd = client->dev.platform_data;
4414 struct reginfo *winseqe_set_addr=NULL;
4415 int ret = 0, set_w,set_h;
4419 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
4420 ARRAY_SIZE(sensor_colour_fmts));
4423 goto sensor_s_fmt_end;
4426 if (sensor->info_priv.fmt.code != mf->code) {
4429 case V4L2_MBUS_FMT_YUYV8_2X8:
4431 winseqe_set_addr = sensor_ClrFmt_YUYV;
4434 case V4L2_MBUS_FMT_UYVY8_2X8:
4436 winseqe_set_addr = sensor_ClrFmt_UYVY;
4442 if (winseqe_set_addr != NULL) {
4443 sensor_write_array(client, winseqe_set_addr);
4444 sensor->info_priv.fmt.code = mf->code;
4445 sensor->info_priv.fmt.colorspace= mf->colorspace;
4446 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
4448 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
4455 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
4457 winseqe_set_addr = sensor_qcif;
4461 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
4463 winseqe_set_addr = sensor_qvga;
4467 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
4469 winseqe_set_addr = sensor_cif;
4473 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
4475 winseqe_set_addr = sensor_vga;
4479 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
4481 winseqe_set_addr = sensor_svga;
4485 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
4487 winseqe_set_addr = sensor_xga;
4491 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
4493 winseqe_set_addr = sensor_720p;
4497 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
4499 winseqe_set_addr = sensor_sxga;
4503 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
4505 winseqe_set_addr = sensor_uxga;
4509 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
4511 winseqe_set_addr = sensor_1080p;
4515 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
4517 winseqe_set_addr = sensor_qxga;
4521 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
4524 winseqe_set_addr = sensor_qsxga;
4530 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
4531 set_w = SENSOR_INIT_WIDTH;
4532 set_h = SENSOR_INIT_HEIGHT;
4533 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
4536 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
4538 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
4540 #if CONFIG_SENSOR_Flash
4541 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4542 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
4543 printk("%s flash on in capture!\n", SENSOR_NAME_STRING());
4546 }else { /* ddl@rock-chips.com : Video */
4547 #if CONFIG_SENSOR_Flash
4548 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4549 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
4550 printk("%s flash off in preivew!\n", SENSOR_NAME_STRING());
4555 #if CONFIG_SENSOR_Focus
4556 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS) {
4557 sensor_af_workqueue_set(icd,WqCmd_af_continues_pause,0,true,NULL);
4561 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
4562 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
4563 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
4564 ret |= sensor_write_array(client,sensor_init_data);
4565 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
4569 ret |= sensor_write_array(client, winseqe_set_addr);
4571 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
4572 #if CONFIG_SENSOR_Flash
4573 if (sensor_fmt_capturechk(sd,mf) == true) {
4574 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
4575 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
4576 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
4580 goto sensor_s_fmt_end;
4582 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
4583 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
4584 #if CONFIG_SENSOR_Focus
4585 sensor_af_workqueue_set(icd,WqCmd_af_special_pos,0,true,NULL);
4587 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4588 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4589 if (sensor->info_priv.whiteBalance != 0) {
4590 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4591 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
4593 sensor->info_priv.snap2preview = true;
4594 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
4595 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4596 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4598 sensor->info_priv.video2preview = true;
4600 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
4601 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
4602 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
4604 if (sensor->info_priv.snap2preview == true) {
4605 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
4606 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
4608 #if CONFIG_SENSOR_Focus
4609 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
4610 sensor_af_workqueue_set(icd,WqCmd_af_return_idle,0,false,NULL);
4611 } else if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS) {
4612 sensor_af_workqueue_set(icd,WqCmd_af_continues,0,false,NULL);
4617 sensor->info_priv.video2preview = false;
4618 sensor->info_priv.snap2preview = false;
4620 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
4623 { // preview size @ video preview
4624 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
4633 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
4635 struct i2c_client *client = v4l2_get_subdevdata(sd);
4636 struct sensor *sensor = to_sensor(client);
4637 const struct sensor_datafmt *fmt;
4638 int ret = 0,set_w,set_h;
4640 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
4641 ARRAY_SIZE(sensor_colour_fmts));
4643 fmt = &sensor->info_priv.fmt;
4644 mf->code = fmt->code;
4647 if (mf->height > SENSOR_MAX_HEIGHT)
4648 mf->height = SENSOR_MAX_HEIGHT;
4649 else if (mf->height < SENSOR_MIN_HEIGHT)
4650 mf->height = SENSOR_MIN_HEIGHT;
4652 if (mf->width > SENSOR_MAX_WIDTH)
4653 mf->width = SENSOR_MAX_WIDTH;
4654 else if (mf->width < SENSOR_MIN_WIDTH)
4655 mf->width = SENSOR_MIN_WIDTH;
4660 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
4665 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
4670 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
4675 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
4680 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
4685 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
4690 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
4695 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
4700 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
4705 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
4710 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
4715 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
4722 set_w = SENSOR_INIT_WIDTH;
4723 set_h = SENSOR_INIT_HEIGHT;
4729 mf->colorspace = fmt->colorspace;
4734 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
4736 struct i2c_client *client = v4l2_get_subdevdata(sd);
4738 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
4741 if (id->match.addr != client->addr)
4744 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
4749 #if CONFIG_SENSOR_Brightness
4750 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4752 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4754 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4756 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
4758 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
4760 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4763 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4767 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4771 #if CONFIG_SENSOR_Effect
4772 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4774 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4776 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4778 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
4780 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
4782 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4785 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4789 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4793 #if CONFIG_SENSOR_Exposure
4794 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4796 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4798 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4800 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
4802 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
4804 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4807 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4811 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4815 #if CONFIG_SENSOR_Saturation
4816 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4818 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4820 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4822 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
4824 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
4826 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4829 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4833 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4837 #if CONFIG_SENSOR_Contrast
4838 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4840 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4842 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4844 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
4846 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
4848 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4851 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4855 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4859 #if CONFIG_SENSOR_Mirror
4860 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4862 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4864 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4866 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
4868 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
4870 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4873 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4877 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4881 #if CONFIG_SENSOR_Flip
4882 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4884 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4886 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4888 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
4890 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
4892 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4895 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4899 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4903 #if CONFIG_SENSOR_Scene
4904 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4906 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4908 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4910 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
4912 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
4914 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4917 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4921 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4925 #if CONFIG_SENSOR_WhiteBalance
4926 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
4928 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4930 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
4932 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
4934 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
4936 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4939 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
4943 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
4947 #if CONFIG_SENSOR_DigitalZoom
4948 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
4950 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4951 struct sensor *sensor = to_sensor(client);
4952 const struct v4l2_queryctrl *qctrl_info;
4953 int digitalzoom_cur, digitalzoom_total;
4955 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
4959 digitalzoom_cur = sensor->info_priv.digitalzoom;
4960 digitalzoom_total = qctrl_info->maximum;
4962 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
4964 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
4968 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
4970 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
4974 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
4976 *value = digitalzoom_total - digitalzoom_cur;
4979 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
4981 *value = 0 - digitalzoom_cur;
4984 digitalzoom_cur += *value;
4986 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
4988 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
4990 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
4993 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
5000 #if CONFIG_SENSOR_Focus
5001 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5003 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5004 struct sensor *sensor = to_sensor(client);
5005 const struct v4l2_queryctrl *qctrl_info;
5008 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
5012 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
5013 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
5014 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true,NULL);
5015 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
5018 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5022 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
5023 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
5028 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5030 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5031 struct sensor *sensor = to_sensor(client);
5032 const struct v4l2_queryctrl *qctrl_info;
5035 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
5039 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
5040 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
5042 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true,NULL);
5044 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true,NULL);
5046 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
5049 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5053 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
5054 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
5059 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value, int *zone_pos)
5061 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5062 struct sensor *sensor = to_sensor(client);
5065 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
5068 case SENSOR_AF_MODE_AUTO:
5070 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true, zone_pos);
5074 /*case SENSOR_AF_MODE_MACRO:
5076 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
5080 case SENSOR_AF_MODE_INFINITY:
5082 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
5086 case SENSOR_AF_MODE_CONTINUOUS:
5088 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true,NULL);
5092 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5097 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
5100 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
5101 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
5107 #if CONFIG_SENSOR_Flash
5108 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
5110 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
5111 if (value == 3) { /* ddl@rock-chips.com: torch */
5112 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
5114 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
5116 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
5120 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
5124 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
5126 struct i2c_client *client = v4l2_get_subdevdata(sd);
5127 struct sensor *sensor = to_sensor(client);
5128 const struct v4l2_queryctrl *qctrl;
5130 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
5134 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
5140 case V4L2_CID_BRIGHTNESS:
5142 ctrl->value = sensor->info_priv.brightness;
5145 case V4L2_CID_SATURATION:
5147 ctrl->value = sensor->info_priv.saturation;
5150 case V4L2_CID_CONTRAST:
5152 ctrl->value = sensor->info_priv.contrast;
5155 case V4L2_CID_DO_WHITE_BALANCE:
5157 ctrl->value = sensor->info_priv.whiteBalance;
5160 case V4L2_CID_EXPOSURE:
5162 ctrl->value = sensor->info_priv.exposure;
5165 case V4L2_CID_HFLIP:
5167 ctrl->value = sensor->info_priv.mirror;
5170 case V4L2_CID_VFLIP:
5172 ctrl->value = sensor->info_priv.flip;
5183 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
5185 struct i2c_client *client = v4l2_get_subdevdata(sd);
5186 struct sensor *sensor = to_sensor(client);
5187 struct soc_camera_device *icd = client->dev.platform_data;
5188 const struct v4l2_queryctrl *qctrl;
5191 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
5195 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
5201 #if CONFIG_SENSOR_Brightness
5202 case V4L2_CID_BRIGHTNESS:
5204 if (ctrl->value != sensor->info_priv.brightness)
5206 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
5210 sensor->info_priv.brightness = ctrl->value;
5215 #if CONFIG_SENSOR_Exposure
5216 case V4L2_CID_EXPOSURE:
5218 if (ctrl->value != sensor->info_priv.exposure)
5220 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
5224 sensor->info_priv.exposure = ctrl->value;
5229 #if CONFIG_SENSOR_Saturation
5230 case V4L2_CID_SATURATION:
5232 if (ctrl->value != sensor->info_priv.saturation)
5234 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
5238 sensor->info_priv.saturation = ctrl->value;
5243 #if CONFIG_SENSOR_Contrast
5244 case V4L2_CID_CONTRAST:
5246 if (ctrl->value != sensor->info_priv.contrast)
5248 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
5252 sensor->info_priv.contrast = ctrl->value;
5257 #if CONFIG_SENSOR_WhiteBalance
5258 case V4L2_CID_DO_WHITE_BALANCE:
5260 if (ctrl->value != sensor->info_priv.whiteBalance)
5262 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
5266 sensor->info_priv.whiteBalance = ctrl->value;
5271 #if CONFIG_SENSOR_Mirror
5272 case V4L2_CID_HFLIP:
5274 if (ctrl->value != sensor->info_priv.mirror)
5276 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
5278 sensor->info_priv.mirror = ctrl->value;
5283 #if CONFIG_SENSOR_Flip
5284 case V4L2_CID_VFLIP:
5286 if (ctrl->value != sensor->info_priv.flip)
5288 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
5290 sensor->info_priv.flip = ctrl->value;
5301 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
5303 const struct v4l2_queryctrl *qctrl;
5304 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5305 struct sensor *sensor = to_sensor(client);
5307 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
5311 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
5315 switch (ext_ctrl->id)
5317 case V4L2_CID_SCENE:
5319 ext_ctrl->value = sensor->info_priv.scene;
5322 case V4L2_CID_EFFECT:
5324 ext_ctrl->value = sensor->info_priv.effect;
5327 case V4L2_CID_ZOOM_ABSOLUTE:
5329 ext_ctrl->value = sensor->info_priv.digitalzoom;
5332 case V4L2_CID_ZOOM_RELATIVE:
5336 case V4L2_CID_FOCUS_ABSOLUTE:
5340 case V4L2_CID_FOCUS_RELATIVE:
5344 case V4L2_CID_FLASH:
5346 ext_ctrl->value = sensor->info_priv.flash;
5354 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
5356 const struct v4l2_queryctrl *qctrl;
5357 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
5358 struct sensor *sensor = to_sensor(client);
5361 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
5365 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
5370 switch (ext_ctrl->id)
5372 #if CONFIG_SENSOR_Scene
5373 case V4L2_CID_SCENE:
5375 if (ext_ctrl->value != sensor->info_priv.scene)
5377 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
5379 sensor->info_priv.scene = ext_ctrl->value;
5384 #if CONFIG_SENSOR_Effect
5385 case V4L2_CID_EFFECT:
5387 if (ext_ctrl->value != sensor->info_priv.effect)
5389 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
5391 sensor->info_priv.effect= ext_ctrl->value;
5396 #if CONFIG_SENSOR_DigitalZoom
5397 case V4L2_CID_ZOOM_ABSOLUTE:
5399 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5402 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
5404 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
5406 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
5408 sensor->info_priv.digitalzoom += val_offset;
5410 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
5415 case V4L2_CID_ZOOM_RELATIVE:
5417 if (ext_ctrl->value)
5419 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
5421 sensor->info_priv.digitalzoom += ext_ctrl->value;
5423 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
5428 #if CONFIG_SENSOR_Focus
5429 case V4L2_CID_FOCUS_ABSOLUTE:
5431 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5436 case V4L2_CID_FOCUS_RELATIVE:
5438 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
5441 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
5444 case V4L2_CID_FOCUS_AUTO:
5446 printk("AF: %d\n",ext_ctrl->value);
5447 if (ext_ctrl->value) {
5448 if ((ext_ctrl->value==1) || (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus)) {
5449 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO,ext_ctrl->rect) != 0) {
5450 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
5451 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
5456 if (ext_ctrl->value == 1)
5457 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
5458 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
5459 if (ext_ctrl->value == 0)
5460 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
5464 case V4L2_CID_FOCUS_CONTINUOUS:
5466 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
5467 if (ext_ctrl->value == 1) {
5468 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS,NULL) != 0) {
5469 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
5470 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
5474 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
5477 if (ext_ctrl->value == 0)
5478 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
5483 #if CONFIG_SENSOR_Flash
5484 case V4L2_CID_FLASH:
5486 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
5488 sensor->info_priv.flash = ext_ctrl->value;
5490 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
5501 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
5503 struct i2c_client *client = v4l2_get_subdevdata(sd);
5504 struct soc_camera_device *icd = client->dev.platform_data;
5505 int i, error_cnt=0, error_idx=-1;
5508 for (i=0; i<ext_ctrl->count; i++) {
5509 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
5516 error_idx = ext_ctrl->count;
5518 if (error_idx != -1) {
5519 ext_ctrl->error_idx = error_idx;
5526 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
5528 struct i2c_client *client = v4l2_get_subdevdata(sd);
5529 struct soc_camera_device *icd = client->dev.platform_data;
5530 int i, error_cnt=0, error_idx=-1;
5532 for (i=0; i<ext_ctrl->count; i++) {
5533 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
5540 error_idx = ext_ctrl->count;
5542 if (error_idx != -1) {
5543 ext_ctrl->error_idx = error_idx;
5550 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
5552 struct i2c_client *client = v4l2_get_subdevdata(sd);
5553 struct sensor *sensor = to_sensor(client);
5554 #if CONFIG_SENSOR_Focus
5555 struct soc_camera_device *icd = client->dev.platform_data;
5556 struct v4l2_mbus_framefmt mf;
5560 sensor->info_priv.enable = 1;
5561 #if CONFIG_SENSOR_Focus
5562 mf.width = icd->user_width;
5563 mf.height = icd->user_height;
5564 mf.code = sensor->info_priv.fmt.code;
5565 mf.colorspace = sensor->info_priv.fmt.colorspace;
5566 mf.field = V4L2_FIELD_NONE;
5567 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
5568 if (sensor_fmt_capturechk(sd, &mf) == false) {
5569 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
5570 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false,NULL);
5571 sensor->info_priv.affm_reinit = 0;
5575 } else if (enable == 0) {
5576 sensor->info_priv.enable = 0;
5577 #if CONFIG_SENSOR_Focus
5578 flush_workqueue(sensor->sensor_wq);
5584 /* Interface active, can use i2c. If it fails, it can indeed mean, that
5585 * this wasn't our capture interface, so, we wait for the right one */
5586 static int sensor_video_probe(struct soc_camera_device *icd,
5587 struct i2c_client *client)
5591 struct sensor *sensor = to_sensor(client);
5593 /* We must have a parent by now. And it cannot be a wrong one.
5594 * So this entire test is completely redundant. */
5595 if (!icd->dev.parent ||
5596 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
5599 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
5601 goto sensor_video_probe_err;
5606 ret = sensor_write(client, 0x3008, 0x80);
5608 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
5610 goto sensor_video_probe_err;
5612 mdelay(5); //delay 5 microseconds
5614 printk("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
5615 mdelay(5000); //jzw modify
5618 /* check if it is an sensor sensor */
5619 ret = sensor_read(client, 0x0000, &value);
5621 SENSOR_TR("read chip id high byte failed\n");
5623 goto sensor_video_probe_err;
5625 printk("jzw:%s:0x%02x\n",__FUNCTION__,value);
5626 pid |= (value << 8);
5628 ret = sensor_read(client, 0x0001, &value);
5630 SENSOR_TR("read chip id low byte failed\n");
5632 goto sensor_video_probe_err;
5634 printk("jzw:%s:0x%02x\n",__FUNCTION__,value);
5635 pid |= (value & 0xff);
5636 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
5637 if (pid == SENSOR_ID) {
5638 sensor->model = SENSOR_V4L2_IDENT;
5640 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
5642 goto sensor_video_probe_err;
5647 sensor_video_probe_err:
5650 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
5652 struct i2c_client *client = v4l2_get_subdevdata(sd);
5653 struct soc_camera_device *icd = client->dev.platform_data;
5654 struct sensor *sensor = to_sensor(client);
5657 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
5660 case RK29_CAM_SUBDEV_DEACTIVATE:
5662 sensor_deactivate(client);
5665 case RK29_CAM_SUBDEV_IOREQUEST:
5667 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
5668 if (sensor->sensor_io_request != NULL) {
5669 sensor->sensor_gpio_res = NULL;
5670 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
5671 if (sensor->sensor_io_request->gpio_res[i].dev_name &&
5672 (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
5673 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
5676 if (sensor->sensor_gpio_res == NULL) {
5677 SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
5679 goto sensor_ioctl_end;
5682 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
5684 goto sensor_ioctl_end;
5686 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
5688 #if CONFIG_SENSOR_Flash
5689 if (sensor->sensor_gpio_res) {
5690 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
5691 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
5692 for (i = 0; i < icd->ops->num_controls; i++) {
5693 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
5694 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
5695 sensor_controls[i].id=0xffff;
5698 sensor->info_priv.flash = 0xff;
5699 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
5700 }else{ //two cameras are the same,need to deal diffrently ,zyc
5701 for (i = 0; i < icd->ops->num_controls; i++) {
5702 if(0xffff == icd->ops->controls[i].id){
5703 sensor_controls[i].id=V4L2_CID_FLASH;
5713 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
5722 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
5723 enum v4l2_mbus_pixelcode *code)
5725 if (index >= ARRAY_SIZE(sensor_colour_fmts))
5728 *code = sensor_colour_fmts[index].code;
5731 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
5732 .init = sensor_init,
5733 .g_ctrl = sensor_g_control,
5734 .s_ctrl = sensor_s_control,
5735 .g_ext_ctrls = sensor_g_ext_controls,
5736 .s_ext_ctrls = sensor_s_ext_controls,
5737 .g_chip_ident = sensor_g_chip_ident,
5738 .ioctl = sensor_ioctl,
5741 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
5742 .s_mbus_fmt = sensor_s_fmt,
5743 .g_mbus_fmt = sensor_g_fmt,
5744 .try_mbus_fmt = sensor_try_fmt,
5745 .enum_mbus_fmt = sensor_enum_fmt,
5746 .s_stream = sensor_s_stream,
5749 static struct v4l2_subdev_ops sensor_subdev_ops = {
5750 .core = &sensor_subdev_core_ops,
5751 .video = &sensor_subdev_video_ops,
5754 static int sensor_probe(struct i2c_client *client,
5755 const struct i2c_device_id *did)
5757 struct sensor *sensor;
5758 struct soc_camera_device *icd = client->dev.platform_data;
5759 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
5760 struct soc_camera_link *icl;
5763 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
5765 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
5769 icl = to_soc_camera_link(icd);
5771 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
5775 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
5776 dev_warn(&adapter->dev,
5777 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
5781 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
5785 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
5787 /* Second stage probe - when a capture adapter is there */
5788 icd->ops = &sensor_ops;
5789 sensor->info_priv.fmt = sensor_colour_fmts[0];
5790 #if CONFIG_SENSOR_I2C_NOSCHED
5791 atomic_set(&sensor->tasklock_cnt,0);
5794 ret = sensor_video_probe(icd, client);
5797 i2c_set_clientdata(client, NULL);
5801 #if CONFIG_SENSOR_Focus
5802 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
5803 if (sensor->sensor_wq == NULL)
5804 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
5805 mutex_init(&sensor->wq_lock);
5808 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5809 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
5813 static int sensor_remove(struct i2c_client *client)
5815 struct sensor *sensor = to_sensor(client);
5816 struct soc_camera_device *icd = client->dev.platform_data;
5818 #if CONFIG_SENSOR_Focus
5819 if (sensor->sensor_wq) {
5820 destroy_workqueue(sensor->sensor_wq);
5821 sensor->sensor_wq = NULL;
5826 i2c_set_clientdata(client, NULL);
5827 client->driver = NULL;
5833 static const struct i2c_device_id sensor_id[] = {
5834 {SENSOR_NAME_STRING(), 0 },
5837 MODULE_DEVICE_TABLE(i2c, sensor_id);
5839 static struct i2c_driver sensor_i2c_driver = {
5841 .name = SENSOR_NAME_STRING(),
5843 .probe = sensor_probe,
5844 .remove = sensor_remove,
5845 .id_table = sensor_id,
5848 static int __init sensor_mod_init(void)
5850 // if (!tcsi_get_value(WESI_CAMERA_HM5065)) //add by caixx
5853 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
5854 return i2c_add_driver(&sensor_i2c_driver);
5857 static void __exit sensor_mod_exit(void)
5859 //if (!tcsi_get_value(WESI_CAMERA_HM5065)) //add by caixx
5862 i2c_del_driver(&sensor_i2c_driver);
5865 device_initcall_sync(sensor_mod_init);
5866 module_exit(sensor_mod_exit);
5868 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
5869 MODULE_AUTHOR("ddl <kernel@rock-chips>");
5870 MODULE_LICENSE("GPL");