27 #include <linux/bitops.h>
29 #include <linux/i2c.h>
30 #include <linux/slab.h>
32 #include <linux/module.h>
51 #define REG_CLKRC 0x11
60 #define REG_HSTRT 0x17
61 #define REG_HSTOP 0x18
62 #define REG_VSTRT 0x19
63 #define REG_VSTOP 0x1a
64 #define REG_PSHFT 0x1b
67 #define REG_HSYNS 0x1e
68 #define REG_HSYNE 0x1f
80 #define REG_FRARL 0x2b
87 #define REG_FRAJH 0x32
88 #define REG_FRAJL 0x33
90 #define REG_L1AEC 0x35
101 #define REG_SPCE 0x68
102 #define REG_ADCL 0x69
104 #define REG_RMCO 0x6c
105 #define REG_GMCO 0x6d
106 #define REG_BMCO 0x6e
110 #define OV6650_PIDH 0x66
111 #define OV6650_PIDL 0x50
112 #define OV6650_MIDH 0x7F
113 #define OV6650_MIDL 0xA2
115 #define DEF_GAIN 0x00
116 #define DEF_BLUE 0x80
120 #define SAT_MASK (0xf << SAT_SHIFT)
121 #define SET_SAT(x) (((x) << SAT_SHIFT) & SAT_MASK)
123 #define HUE_EN BIT(5)
124 #define HUE_MASK 0x1f
126 #define SET_HUE(x) (HUE_EN | ((x) & HUE_MASK))
128 #define DEF_AECH 0x4D
130 #define CLKRC_6MHz 0x00
131 #define CLKRC_12MHz 0x40
132 #define CLKRC_16MHz 0x80
133 #define CLKRC_24MHz 0xc0
134 #define CLKRC_DIV_MASK 0x3f
135 #define GET_CLKRC_DIV(x) (((x) & CLKRC_DIV_MASK) + 1)
137 #define COMA_RESET BIT(7)
138 #define COMA_QCIF BIT(5)
139 #define COMA_RAW_RGB BIT(4)
140 #define COMA_RGB BIT(3)
141 #define COMA_BW BIT(2)
142 #define COMA_WORD_SWAP BIT(1)
143 #define COMA_BYTE_SWAP BIT(0)
144 #define DEF_COMA 0x00
146 #define COMB_FLIP_V BIT(7)
147 #define COMB_FLIP_H BIT(5)
148 #define COMB_BAND_FILTER BIT(4)
149 #define COMB_AWB BIT(2)
150 #define COMB_AGC BIT(1)
151 #define COMB_AEC BIT(0)
152 #define DEF_COMB 0x5f
154 #define COML_ONE_CHANNEL BIT(7)
156 #define DEF_HSTRT 0x24
157 #define DEF_HSTOP 0xd4
158 #define DEF_VSTRT 0x04
159 #define DEF_VSTOP 0x94
161 #define COMF_HREF_LOW BIT(4)
163 #define COMJ_PCLK_RISING BIT(4)
164 #define COMJ_VSYNC_HIGH BIT(0)
167 #define W_QCIF (DEF_HSTOP - DEF_HSTRT)
168 #define W_CIF (W_QCIF << 1)
169 #define H_QCIF (DEF_VSTOP - DEF_VSTRT)
170 #define H_CIF (H_QCIF << 1)
172 #define FRAME_RATE_MAX 30
224 .addr = client->
addr,
243 dev_err(&client->
dev,
"Failed reading register 0x%02x!\n", reg);
248 static int ov6650_reg_write(
struct i2c_client *client,
u8 reg,
u8 val)
251 unsigned char data[2] = {
reg, val };
263 dev_err(&client->
dev,
"Failed writing register 0x%02x!\n", reg);
276 ret = ov6650_reg_read(client, reg, &val);
279 "[Read]-Modify-Write of register 0x%02x failed!\n",
287 ret = ov6650_reg_write(client, reg, val);
290 "Read-Modify-[Write] of register 0x%02x failed!\n",
312 struct i2c_client *client = v4l2_get_subdevdata(sd);
318 ret = ov6650_reg_read(client,
REG_GAIN, ®);
323 ret = ov6650_reg_read(client,
REG_BLUE, ®);
325 ret = ov6650_reg_read(client,
REG_RED, ®2);
332 ret = ov6650_reg_read(client,
REG_AECH, ®);
341 static int ov6550_s_ctrl(
struct v4l2_ctrl *ctrl)
345 struct i2c_client *client = v4l2_get_subdevdata(sd);
350 ret = ov6650_reg_rmw(client,
REG_COMB,
352 if (!ret && !ctrl->
val)
353 ret = ov6650_reg_write(client,
REG_GAIN, priv->
gain->val);
356 ret = ov6650_reg_rmw(client,
REG_COMB,
358 if (!ret && !ctrl->
val) {
359 ret = ov6650_reg_write(client,
REG_BLUE, priv->
blue->val);
361 ret = ov6650_reg_write(client,
REG_RED,
372 return ov6650_reg_write(client,
REG_BRT, ctrl->
val);
374 ret = ov6650_reg_rmw(client,
REG_COMB, ctrl->
val ==
377 ret = ov6650_reg_write(client,
REG_AECH,
381 return ov6650_reg_write(client,
REG_GAM1, ctrl->
val);
383 return ov6650_reg_rmw(client,
REG_COMB,
386 return ov6650_reg_rmw(client,
REG_COMB,
394 static int ov6650_g_chip_ident(
struct v4l2_subdev *sd,
403 #ifdef CONFIG_VIDEO_ADV_DEBUG
404 static int ov6650_get_register(
struct v4l2_subdev *sd,
407 struct i2c_client *client = v4l2_get_subdevdata(sd);
411 if (reg->
reg & ~0xff)
416 ret = ov6650_reg_read(client, reg->
reg, &val);
423 static int ov6650_set_register(
struct v4l2_subdev *sd,
426 struct i2c_client *client = v4l2_get_subdevdata(sd);
428 if (reg->
reg & ~0xff || reg->
val & ~0xff)
431 return ov6650_reg_write(client, reg->
reg, reg->
val);
435 static int ov6650_s_power(
struct v4l2_subdev *sd,
int on)
437 struct i2c_client *client = v4l2_get_subdevdata(sd);
440 return soc_camera_set_power(&client->
dev, icl, on);
445 struct i2c_client *client = v4l2_get_subdevdata(sd);
446 struct ov6650 *priv = to_ov6650(client);
456 struct i2c_client *client = v4l2_get_subdevdata(sd);
457 struct ov6650 *priv = to_ov6650(client);
468 soc_camera_limit_side(&rect.
left, &rect.
width,
470 soc_camera_limit_side(&rect.
top, &rect.
height,
476 ret = ov6650_reg_write(client,
REG_HSTOP,
481 ret = ov6650_reg_write(client,
REG_VSTRT, rect.
top >> 1);
485 ret = ov6650_reg_write(client,
REG_VSTOP,
511 struct v4l2_mbus_framefmt *mf)
513 struct i2c_client *client = v4l2_get_subdevdata(sd);
514 struct ov6650 *priv = to_ov6650(client);
518 mf->code = priv->
code;
527 return width > rect->
width >> 1 || height > rect->
height >> 1;
541 if (pclk_limit && pclk_limit < pclk)
544 return (pclk_max - 1) / pclk;
548 static int ov6650_s_fmt(
struct v4l2_subdev *sd,
struct v4l2_mbus_framefmt *mf)
550 struct i2c_client *client = v4l2_get_subdevdata(sd);
553 struct ov6650 *priv = to_ov6650(client);
554 bool half_scale = !is_unscaled_ok(mf->width, mf->height, &priv->
rect);
558 .left = priv->
rect.left + (priv->
rect.width >> 1) -
559 (mf->width >> (1 - half_scale)),
561 (mf->height >> (1 - half_scale)),
562 .width = mf->width << half_scale,
563 .height = mf->height << half_scale,
567 unsigned long mclk, pclk;
568 u8 coma_set = 0, coma_mask = 0, coml_set, coml_mask, clkrc;
574 dev_dbg(&client->
dev,
"pixel format GREY8_1X8\n");
579 dev_dbg(&client->
dev,
"pixel format YUYV8_2X8_LE\n");
584 dev_dbg(&client->
dev,
"pixel format YVYU8_2X8_LE (untested)\n");
589 dev_dbg(&client->
dev,
"pixel format YUYV8_2X8_BE\n");
599 dev_dbg(&client->
dev,
"pixel format YVYU8_2X8_BE (untested)\n");
609 dev_dbg(&client->
dev,
"pixel format SBGGR8_1X8 (untested)\n");
614 dev_err(&client->
dev,
"Pixel format not handled: 0x%x\n", code);
636 dev_dbg(&client->
dev,
"max resolution: QCIF\n");
640 dev_dbg(&client->
dev,
"max resolution: CIF\n");
646 if (sense->master_clock == 8000000) {
649 }
else if (sense->master_clock == 12000000) {
652 }
else if (sense->master_clock == 16000000) {
655 }
else if (sense->master_clock == 24000000) {
660 "unsupported input clock, check platform data\n");
663 mclk = sense->master_clock;
669 dev_dbg(&client->
dev,
"using default 24MHz input clock\n");
675 dev_dbg(&client->
dev,
"pixel clock divider: %ld.%ld\n",
676 mclk / pclk, 10 * mclk % pclk / pclk);
678 ret = ov6650_s_crop(sd, &a);
680 ret = ov6650_reg_rmw(client,
REG_COMA, coma_set, coma_mask);
682 ret = ov6650_reg_write(client,
REG_CLKRC, clkrc);
684 ret = ov6650_reg_rmw(client,
REG_COML, coml_set, coml_mask);
688 mf->width = priv->
rect.width >> half_scale;
689 mf->height = priv->
rect.height >> half_scale;
696 struct v4l2_mbus_framefmt *mf)
698 struct i2c_client *client = v4l2_get_subdevdata(sd);
699 struct ov6650 *priv = to_ov6650(client);
701 if (is_unscaled_ok(mf->width, mf->height, &priv->
rect))
703 &mf->height, 2,
H_CIF, 1, 0);
733 *code = ov6650_codes[
index];
739 struct i2c_client *client = v4l2_get_subdevdata(sd);
740 struct ov6650 *priv = to_ov6650(client);
746 memset(cp, 0,
sizeof(*cp));
752 dev_dbg(&client->
dev,
"Frame interval: %u/%u s\n",
760 struct i2c_client *client = v4l2_get_subdevdata(sd);
761 struct ov6650 *priv = to_ov6650(client);
787 priv->
tpf.numerator =
div;
802 static int ov6650_reset(
struct i2c_client *client)
811 "An error occurred while entering soft reset!\n");
817 static int ov6650_prog_dflt(
struct i2c_client *client)
823 ret = ov6650_reg_write(client,
REG_COMA, 0);
830 static int ov6650_video_probe(
struct i2c_client *client)
832 struct ov6650 *priv = to_ov6650(client);
833 u8 pidh, pidl, midh, midl;
836 ret = ov6650_s_power(&priv->
subdev, 1);
843 ret = ov6650_reg_read(client,
REG_PIDH, &pidh);
845 ret = ov6650_reg_read(client,
REG_PIDL, &pidl);
847 ret = ov6650_reg_read(client,
REG_MIDH, &midh);
849 ret = ov6650_reg_read(client,
REG_MIDL, &midl);
855 dev_err(&client->
dev,
"Product ID error 0x%02x:0x%02x\n",
862 "ov6650 Product ID 0x%02x:0x%02x Manufacturer ID 0x%02x:0x%02x\n",
863 pidh, pidl, midh, midl);
865 ret = ov6650_reset(client);
867 ret = ov6650_prog_dflt(client);
872 ov6650_s_power(&priv->
subdev, 0);
877 .g_volatile_ctrl = ov6550_g_volatile_ctrl,
878 .s_ctrl = ov6550_s_ctrl,
882 .g_chip_ident = ov6650_g_chip_ident,
883 #ifdef CONFIG_VIDEO_ADV_DEBUG
884 .g_register = ov6650_get_register,
885 .s_register = ov6650_set_register,
887 .s_power = ov6650_s_power,
891 static int ov6650_g_mbus_config(
struct v4l2_subdev *sd,
894 struct i2c_client *client = v4l2_get_subdevdata(sd);
909 static int ov6650_s_mbus_config(
struct v4l2_subdev *sd,
912 struct i2c_client *client = v4l2_get_subdevdata(sd);
940 .s_stream = ov6650_s_stream,
941 .g_mbus_fmt = ov6650_g_fmt,
942 .s_mbus_fmt = ov6650_s_fmt,
943 .try_mbus_fmt = ov6650_try_fmt,
944 .enum_mbus_fmt = ov6650_enum_fmt,
945 .cropcap = ov6650_cropcap,
946 .g_crop = ov6650_g_crop,
947 .s_crop = ov6650_s_crop,
948 .g_parm = ov6650_g_parm,
949 .s_parm = ov6650_s_parm,
950 .g_mbus_config = ov6650_g_mbus_config,
951 .s_mbus_config = ov6650_s_mbus_config,
955 .core = &ov6650_core_ops,
956 .video = &ov6650_video_ops,
962 static int ov6650_probe(
struct i2c_client *client,
970 dev_err(&client->
dev,
"Missing platform_data for driver\n");
977 "Failed to allocate memory for private data!\n");
1012 if (priv->
hdl.error) {
1013 int err = priv->
hdl.error;
1031 ret = ov6650_video_probe(client);
1040 static int ov6650_remove(
struct i2c_client *client)
1042 struct ov6650 *priv = to_ov6650(client);
1056 static struct i2c_driver ov6650_i2c_driver = {
1060 .probe = ov6650_probe,
1061 .remove = ov6650_remove,
1062 .id_table = ov6650_id,