17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
22 #include <linux/videodev2.h>
29 #define VAL_SET(x, mask, rshift, lshift) \
30 ((((x) >> rshift) & mask) << lshift)
36 #define R_BYPASS_DSP_BYPAS 0x01
37 #define R_BYPASS_USE_DSP 0x00
40 #define CTRLI_LP_DP 0x80
41 #define CTRLI_ROUND 0x40
42 #define CTRLI_V_DIV_SET(x) VAL_SET(x, 0x3, 0, 3)
43 #define CTRLI_H_DIV_SET(x) VAL_SET(x, 0x3, 0, 0)
45 #define HSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
47 #define VSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
49 #define XOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
51 #define YOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
53 #define VHYX_VSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 7)
54 #define VHYX_HSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 3)
55 #define VHYX_YOFF_SET(x) VAL_SET(x, 0x3, 8, 4)
56 #define VHYX_XOFF_SET(x) VAL_SET(x, 0x3, 8, 0)
59 #define TEST_HSIZE_SET(x) VAL_SET(x, 0x1, (9+2), 7)
61 #define ZMOW_OUTW_SET(x) VAL_SET(x, 0xFF, 2, 0)
63 #define ZMOH_OUTH_SET(x) VAL_SET(x, 0xFF, 2, 0)
65 #define ZMHH_ZSPEED_SET(x) VAL_SET(x, 0x0F, 0, 4)
66 #define ZMHH_OUTH_SET(x) VAL_SET(x, 0x1, (8+2), 2)
67 #define ZMHH_OUTW_SET(x) VAL_SET(x, 0x3, (8+2), 0)
71 #define CTRL2_DCW_EN 0x20
72 #define CTRL2_SDE_EN 0x10
73 #define CTRL2_UV_ADJ_EN 0x08
74 #define CTRL2_UV_AVG_EN 0x04
75 #define CTRL2_CMX_EN 0x01
77 #define CTRL3_BPC_EN 0x80
78 #define CTRL3_WPC_EN 0x40
80 #define SIZEL_HSIZE8_11_SET(x) VAL_SET(x, 0x1, 11, 6)
81 #define SIZEL_HSIZE8_SET(x) VAL_SET(x, 0x7, 0, 3)
82 #define SIZEL_VSIZE8_SET(x) VAL_SET(x, 0x7, 0, 0)
84 #define HSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
86 #define VSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
88 #define CTRL0_AEC_EN 0x80
89 #define CTRL0_AEC_SEL 0x40
90 #define CTRL0_STAT_SEL 0x20
91 #define CTRL0_VFIRST 0x10
92 #define CTRL0_YUV422 0x08
93 #define CTRL0_YUV_EN 0x04
94 #define CTRL0_RGB_EN 0x02
95 #define CTRL0_RAW_EN 0x01
97 #define CTRL1_CIP 0x80
98 #define CTRL1_DMY 0x40
99 #define CTRL1_RAW_GMA 0x20
100 #define CTRL1_DG 0x10
101 #define CTRL1_AWB 0x08
102 #define CTRL1_AWB_GAIN 0x04
103 #define CTRL1_LENC 0x02
104 #define CTRL1_PRE 0x01
105 #define R_DVP_SP 0xD3
106 #define R_DVP_SP_AUTO_MODE 0x80
107 #define R_DVP_SP_DVP_MASK 0x3F
109 #define IMAGE_MODE 0xDA
110 #define IMAGE_MODE_Y8_DVP_EN 0x40
111 #define IMAGE_MODE_JPEG_EN 0x10
112 #define IMAGE_MODE_YUV422 0x00
113 #define IMAGE_MODE_RAW10 0x04
114 #define IMAGE_MODE_RGB565 0x08
115 #define IMAGE_MODE_HREF_VSYNC 0x02
117 #define IMAGE_MODE_LBYTE_FIRST 0x01
123 #define RESET_MICROC 0x40
124 #define RESET_SCCB 0x20
125 #define RESET_JPEG 0x10
126 #define RESET_DVP 0x04
127 #define RESET_IPU 0x02
128 #define RESET_CIF 0x01
130 #define REGED_CLK_OUT_DIS 0x10
134 #define SS_CTRL_ADD_AUTO_INC 0x20
135 #define SS_CTRL_EN 0x08
136 #define SS_CTRL_DELAY_CLK 0x04
137 #define SS_CTRL_ACC_EN 0x02
138 #define SS_CTRL_SEN_PASS_THR 0x01
140 #define MC_BIST_RESET 0x80
141 #define MC_BIST_BOOT_ROM_SEL 0x40
142 #define MC_BIST_12KB_SEL 0x20
143 #define MC_BIST_12KB_MASK 0x30
144 #define MC_BIST_512KB_SEL 0x08
145 #define MC_BIST_512KB_MASK 0x0C
146 #define MC_BIST_BUSY_BIT_R 0x02
147 #define MC_BIST_MC_RES_ONE_SH_W 0x02
148 #define MC_BIST_LAUNCH 0x01
149 #define BANK_SEL 0xFF
150 #define BANK_SEL_DSP 0x00
151 #define BANK_SEL_SENS 0x01
159 #define COM1_1_DUMMY_FR 0x40
160 #define COM1_3_DUMMY_FR 0x80
161 #define COM1_7_DUMMY_FR 0xC0
162 #define COM1_VWIN_LSB_UXGA 0x0F
163 #define COM1_VWIN_LSB_SVGA 0x0A
164 #define COM1_VWIN_LSB_CIF 0x06
166 #define REG04_DEF 0x20
167 #define REG04_HFLIP_IMG 0x80
168 #define REG04_VFLIP_IMG 0x40
169 #define REG04_VREF_EN 0x10
170 #define REG04_HREF_EN 0x08
171 #define REG04_AEC_SET(x) VAL_SET(x, 0x3, 0, 0)
174 #define COM2_SOFT_SLEEP_MODE 0x10
176 #define COM2_OCAP_Nx_SET(N) (((N) - 1) & 0x03)
180 #define COM3_BAND_50H 0x04
181 #define COM3_BAND_AUTO 0x02
182 #define COM3_SING_FR_SNAPSH 0x01
186 #define CLKRC_EN 0x80
187 #define CLKRC_DIV_SET(x) (((x) - 1) & 0x1F)
189 #define COM7_SRST 0x80
192 #define COM7_RES_UXGA 0x00
193 #define COM7_RES_SVGA 0x40
194 #define COM7_RES_CIF 0x20
195 #define COM7_ZOOM_EN 0x04
196 #define COM7_COLOR_BAR_TEST 0x02
198 #define COM8_DEF 0xC0
199 #define COM8_BNDF_EN 0x20
200 #define COM8_AGC_EN 0x04
201 #define COM8_AEC_EN 0x01
204 #define COM9_AGC_GAIN_2x 0x00
205 #define COM9_AGC_GAIN_4x 0x20
206 #define COM9_AGC_GAIN_8x 0x40
207 #define COM9_AGC_GAIN_16x 0x60
208 #define COM9_AGC_GAIN_32x 0x80
209 #define COM9_AGC_GAIN_64x 0xA0
210 #define COM9_AGC_GAIN_128x 0xC0
212 #define COM10_PCLK_HREF 0x20
213 #define COM10_PCLK_RISE 0x10
217 #define COM10_HREF_INV 0x08
219 #define COM10_VSINC_INV 0x02
229 #define VV_HIGH_TH_SET(x) VAL_SET(x, 0xF, 0, 4)
230 #define VV_LOW_TH_SET(x) VAL_SET(x, 0xF, 0, 0)
237 #define REG32_PCLK_DIV_2 0x80
238 #define REG32_PCLK_DIV_4 0xC0
253 #define HISTO_LOW 0x61
254 #define HISTO_HIGH 0x62
259 #define MANUFACTURER_ID 0x7FA2
260 #define PID_OV2640 0x2642
261 #define VERSION(pid, ver) ((pid << 8) | (ver & 0xFF))
314 #define ENDMARKER { 0xff, 0xff }
316 static const struct regval_list ov2640_init_regs[] = {
495 static const struct regval_list ov2640_size_change_preamble_regs[] = {
512 #define PER_SIZE_REG_SEQ(x, y, v_div, h_div, pclk_div) \
513 { CTRLI, CTRLI_LP_DP | CTRLI_V_DIV_SET(v_div) | \
514 CTRLI_H_DIV_SET(h_div)}, \
515 { ZMOW, ZMOW_OUTW_SET(x) }, \
516 { ZMOH, ZMOH_OUTH_SET(y) }, \
517 { ZMHH, ZMHH_OUTW_SET(x) | ZMHH_OUTH_SET(y) }, \
518 { R_DVP_SP, pclk_div }, \
521 static const struct regval_list ov2640_qcif_regs[] = {
526 static const struct regval_list ov2640_qvga_regs[] = {
531 static const struct regval_list ov2640_cif_regs[] = {
536 static const struct regval_list ov2640_vga_regs[] = {
541 static const struct regval_list ov2640_svga_regs[] = {
546 static const struct regval_list ov2640_xga_regs[] = {
559 static const struct regval_list ov2640_uxga_regs[] = {
566 #define OV2640_SIZE(n, w, h, r) \
567 {.name = n, .width = w , .height = h, .regs = r }
583 static const struct regval_list ov2640_format_change_preamble_regs[] = {
589 static const struct regval_list ov2640_yuv422_regs[] = {
599 static const struct regval_list ov2640_rgb565_regs[] = {
621 static int ov2640_write_array(
struct i2c_client *client,
626 while ((vals->
reg_num != 0xff) || (vals->
value != 0xff)) {
639 static int ov2640_mask_set(
struct i2c_client *client,
649 dev_vdbg(&client->
dev,
"masks: 0x%02x, 0x%02x", reg, val);
654 static int ov2640_reset(
struct i2c_client *client)
663 ret = ov2640_write_array(client, reset_seq);
669 dev_dbg(&client->
dev,
"%s: (ret %d)", __func__, ret);
685 struct i2c_client *client = v4l2_get_subdevdata(sd);
705 static int ov2640_g_chip_ident(
struct v4l2_subdev *sd,
708 struct i2c_client *client = v4l2_get_subdevdata(sd);
711 id->ident = priv->
model;
717 #ifdef CONFIG_VIDEO_ADV_DEBUG
718 static int ov2640_g_register(
struct v4l2_subdev *sd,
721 struct i2c_client *client = v4l2_get_subdevdata(sd);
737 static int ov2640_s_register(
struct v4l2_subdev *sd,
740 struct i2c_client *client = v4l2_get_subdevdata(sd);
742 if (reg->
reg > 0xff ||
750 static int ov2640_s_power(
struct v4l2_subdev *sd,
int on)
752 struct i2c_client *client = v4l2_get_subdevdata(sd);
755 return soc_camera_set_power(&client->
dev, icl, on);
761 int i, default_size =
ARRAY_SIZE(ov2640_supported_win_sizes) - 1;
763 for (i = 0; i <
ARRAY_SIZE(ov2640_supported_win_sizes); i++) {
764 if (ov2640_supported_win_sizes[i].width >= *width &&
765 ov2640_supported_win_sizes[i].height >= *height) {
766 *width = ov2640_supported_win_sizes[
i].
width;
767 *height = ov2640_supported_win_sizes[
i].
height;
768 return &ov2640_supported_win_sizes[
i];
772 *width = ov2640_supported_win_sizes[default_size].
width;
773 *height = ov2640_supported_win_sizes[default_size].
height;
774 return &ov2640_supported_win_sizes[default_size];
777 static int ov2640_set_params(
struct i2c_client *client,
u32 *width,
u32 *height,
785 priv->
win = ov2640_select_win(width, height);
791 dev_dbg(&client->
dev,
"%s: Selected cfmt RGB565", __func__);
792 selected_cfmt_regs = ov2640_rgb565_regs;
796 dev_dbg(&client->
dev,
"%s: Selected cfmt YUV422", __func__);
797 selected_cfmt_regs = ov2640_yuv422_regs;
801 ov2640_reset(client);
804 dev_dbg(&client->
dev,
"%s: Init default", __func__);
805 ret = ov2640_write_array(client, ov2640_init_regs);
810 dev_dbg(&client->
dev,
"%s: Set size to %s", __func__, priv->
win->name);
811 ret = ov2640_write_array(client, ov2640_size_change_preamble_regs);
816 ret = ov2640_write_array(client, priv->
win->regs);
821 dev_dbg(&client->
dev,
"%s: Set cfmt", __func__);
822 ret = ov2640_write_array(client, ov2640_format_change_preamble_regs);
827 ret = ov2640_write_array(client, selected_cfmt_regs);
832 *width = priv->
win->width;
833 *height = priv->
win->height;
838 dev_err(&client->
dev,
"%s: Error %d", __func__, ret);
839 ov2640_reset(client);
846 struct v4l2_mbus_framefmt *mf)
848 struct i2c_client *client = v4l2_get_subdevdata(sd);
853 priv->
win = ov2640_select_win(&width, &height);
857 mf->width = priv->
win->width;
858 mf->height = priv->
win->height;
875 struct v4l2_mbus_framefmt *mf)
877 struct i2c_client *client = v4l2_get_subdevdata(sd);
891 ret = ov2640_set_params(client, &mf->width, &mf->height, mf->code);
897 struct v4l2_mbus_framefmt *mf)
904 win = ov2640_select_win(&mf->width, &mf->height);
927 *code = ov2640_codes[
index];
956 static int ov2640_video_probe(
struct i2c_client *client)
963 ret = ov2640_s_power(&priv->
subdev, 1);
983 "Product ID error %x:%x\n", pid, ver);
989 "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
990 devname, pid, ver, midh, midl);
995 ov2640_s_power(&priv->
subdev, 0);
1000 .s_ctrl = ov2640_s_ctrl,
1004 .g_chip_ident = ov2640_g_chip_ident,
1005 #ifdef CONFIG_VIDEO_ADV_DEBUG
1006 .g_register = ov2640_g_register,
1007 .s_register = ov2640_s_register,
1009 .s_power = ov2640_s_power,
1012 static int ov2640_g_mbus_config(
struct v4l2_subdev *sd,
1015 struct i2c_client *client = v4l2_get_subdevdata(sd);
1028 .s_stream = ov2640_s_stream,
1029 .g_mbus_fmt = ov2640_g_fmt,
1030 .s_mbus_fmt = ov2640_s_fmt,
1031 .try_mbus_fmt = ov2640_try_fmt,
1032 .cropcap = ov2640_cropcap,
1033 .g_crop = ov2640_g_crop,
1034 .enum_mbus_fmt = ov2640_enum_fmt,
1035 .g_mbus_config = ov2640_g_mbus_config,
1039 .core = &ov2640_subdev_core_ops,
1040 .video = &ov2640_subdev_video_ops,
1046 static int ov2640_probe(
struct i2c_client *client,
1056 "OV2640: Missing platform_data for driver\n");
1062 "OV2640: I2C-Adapter doesn't support SMBUS\n");
1069 "Failed to allocate memory for private data!\n");
1080 if (priv->
hdl.error) {
1081 int err = priv->
hdl.error;
1087 ret = ov2640_video_probe(client);
1098 static int ov2640_remove(
struct i2c_client *client)
1114 static struct i2c_driver ov2640_i2c_driver = {
1118 .probe = ov2640_probe,
1119 .remove = ov2640_remove,
1120 .id_table = ov2640_id,