9 #include <linux/slab.h>
10 #include <linux/videodev2.h>
12 #include <linux/module.h>
13 #include <asm/div64.h>
26 #define R00_MT9V011_CHIP_VERSION 0x00
27 #define R01_MT9V011_ROWSTART 0x01
28 #define R02_MT9V011_COLSTART 0x02
29 #define R03_MT9V011_HEIGHT 0x03
30 #define R04_MT9V011_WIDTH 0x04
31 #define R05_MT9V011_HBLANK 0x05
32 #define R06_MT9V011_VBLANK 0x06
33 #define R07_MT9V011_OUT_CTRL 0x07
34 #define R09_MT9V011_SHUTTER_WIDTH 0x09
35 #define R0A_MT9V011_CLK_SPEED 0x0a
36 #define R0B_MT9V011_RESTART 0x0b
37 #define R0C_MT9V011_SHUTTER_DELAY 0x0c
38 #define R0D_MT9V011_RESET 0x0d
39 #define R1E_MT9V011_DIGITAL_ZOOM 0x1e
40 #define R20_MT9V011_READ_MODE 0x20
41 #define R2B_MT9V011_GREEN_1_GAIN 0x2b
42 #define R2C_MT9V011_BLUE_GAIN 0x2c
43 #define R2D_MT9V011_RED_GAIN 0x2d
44 #define R2E_MT9V011_GREEN_2_GAIN 0x2e
45 #define R35_MT9V011_GLOBAL_GAIN 0x35
46 #define RF1_MT9V011_CHIP_ENABLE 0xf1
48 #define MT9V011_VERSION 0x8232
49 #define MT9V011_REV_B_VERSION 0x8243
58 .maximum = (1 << 12) - 1 - 0x0020,
69 .default_value = 0x01fc,
74 .name =
"Red Balance",
76 .maximum = (1 << 9) - 1,
83 .name =
"Blue Balance",
85 .maximum = (1 << 9) - 1,
136 "i2c i/o error: rc == %d (should be 1)\n", rc);
143 "i2c i/o error: rc == %d (should be 2)\n", rc);
147 v4l2_dbg(2,
debug, sd,
"mt9v011: read 0x%02x = 0x%04x\n", addr, val);
152 static void mt9v011_write(
struct v4l2_subdev *sd,
unsigned char addr,
155 struct i2c_client *c = v4l2_get_subdevdata(sd);
156 unsigned char buffer[3];
160 buffer[1] = value >> 8;
161 buffer[2] = value & 0xff;
164 "mt9v011: writing 0x%02x 0x%04x\n", buffer[0], value);
168 "i2c i/o error: rc == %d (should be 3)\n", rc);
195 static u16 calc_mt9v011_gain(
s16 lineargain)
206 lineargain += 0x0020;
208 if (lineargain > 2047)
211 if (lineargain > 1023) {
214 analoginit = lineargain / 16;
215 }
else if (lineargain > 511) {
218 analoginit = lineargain / 8;
219 }
else if (lineargain > 255) {
221 analoginit = lineargain / 4;
222 }
else if (lineargain > 127) {
224 analoginit = lineargain / 2;
226 analoginit = lineargain;
228 return analoginit + (analogmult << 7) + (digitalgain << 9);
235 u16 green_gain, blue_gain, red_gain;
245 blue_gain = calc_mt9v011_gain(bal);
249 red_gain = calc_mt9v011_gain(bal);
262 unsigned row_time, t_time;
272 row_time = (width + 113 + hblank) * (speed + 2);
273 t_time = row_time * (height + vblank + 1);
275 frames_per_ms = core->
xtal * 1000
l;
276 do_div(frames_per_ms, t_time);
279 v4l2_dbg(1,
debug, sd,
"Programmed to %u.%03u fps (%d pixel clcks)\n",
280 tmp / 1000, tmp % 1000, t_time);
282 if (numerator && denominator) {
284 *denominator = (
u32)frames_per_ms;
290 struct mt9v011 *core = to_mt9v011(sd);
292 unsigned row_time, line_time;
296 if (!numerator || !denominator)
304 row_time = width + 113 + hblank;
305 line_time = height + vblank + 1;
307 t_time = core->
xtal * ((
u64)numerator);
309 t_time += denominator / 2;
310 do_div(t_time, denominator);
313 do_div(speed, row_time * line_time);
330 struct mt9v011 *core = to_mt9v011(sd);
344 hstart = 20 + (640 - core->
width) / 2;
349 vstart = 8 + (480 - core->
height) / 2;
359 struct mt9v011 *core = to_mt9v011(sd);
360 unsigned mode = 0x1000;
375 for (i = 0; i <
ARRAY_SIZE(mt9v011_init_default); i++)
376 mt9v011_write(sd, mt9v011_init_default[i].
reg,
377 mt9v011_init_default[i].value);
388 struct mt9v011 *core = to_mt9v011(sd);
421 for (i = 0; i <
ARRAY_SIZE(mt9v011_qctrl); i++)
422 if (qc->
id && qc->
id == mt9v011_qctrl[i].
id) {
423 memcpy(qc, &(mt9v011_qctrl[i]),
434 struct mt9v011 *core = to_mt9v011(sd);
438 for (i = 0; i <
n; i++) {
439 if (ctrl->
id != mt9v011_qctrl[i].
id)
489 static int mt9v011_try_mbus_fmt(
struct v4l2_subdev *sd,
struct v4l2_mbus_framefmt *
fmt)
495 &fmt->height, 32, 480, 1, 0);
540 static int mt9v011_s_mbus_fmt(
struct v4l2_subdev *sd,
struct v4l2_mbus_framefmt *fmt)
542 struct mt9v011 *core = to_mt9v011(sd);
545 rc = mt9v011_try_mbus_fmt(sd, fmt);
549 core->
width = fmt->width;
550 core->
height = fmt->height;
557 #ifdef CONFIG_VIDEO_ADV_DEBUG
558 static int mt9v011_g_register(
struct v4l2_subdev *sd,
568 reg->
val = mt9v011_read(sd, reg->
reg & 0xff);
574 static int mt9v011_s_register(
struct v4l2_subdev *sd,
577 struct i2c_client *client = v4l2_get_subdevdata(sd);
584 mt9v011_write(sd, reg->
reg & 0xff, reg->
val & 0xffff);
590 static int mt9v011_g_chip_ident(
struct v4l2_subdev *sd,
594 struct i2c_client *client = v4l2_get_subdevdata(sd);
603 .queryctrl = mt9v011_queryctrl,
604 .g_ctrl = mt9v011_g_ctrl,
605 .s_ctrl = mt9v011_s_ctrl,
606 .reset = mt9v011_reset,
607 .g_chip_ident = mt9v011_g_chip_ident,
608 #ifdef CONFIG_VIDEO_ADV_DEBUG
609 .g_register = mt9v011_g_register,
610 .s_register = mt9v011_s_register,
615 .enum_mbus_fmt = mt9v011_enum_mbus_fmt,
616 .try_mbus_fmt = mt9v011_try_mbus_fmt,
617 .s_mbus_fmt = mt9v011_s_mbus_fmt,
618 .g_parm = mt9v011_g_parm,
619 .s_parm = mt9v011_s_parm,
623 .core = &mt9v011_core_ops,
624 .video = &mt9v011_video_ops,
632 static int mt9v011_probe(
struct i2c_client *c,
640 if (!i2c_check_functionality(c->
adapter,
655 v4l2_info(sd,
"*** unknown micron chip detected (0x%04x).\n",
665 core->
xtal = 27000000;
667 if (c->
dev.platform_data) {
672 core->
xtal / 1000000, (core->
xtal / 1000) % 1000);
675 v4l_info(c,
"chip found @ 0x%02x (%s - chip version 0x%04x)\n",
681 static int mt9v011_remove(
struct i2c_client *c)
686 "mt9v011.c: removing mt9v011 adapter on address 0x%x\n",
690 kfree(to_mt9v011(sd));
707 .probe = mt9v011_probe,
708 .remove = mt9v011_remove,
709 .id_table = mt9v011_id,