2 * mt9v011 -Micron 1/4-Inch VGA Digital Image Sensor
4 * Copyright (c) 2009 Mauro Carvalho Chehab (mchehab@redhat.com)
5 * This code is placed under the terms of the GNU General Public License v2
9 #include <linux/slab.h>
10 #include <linux/videodev2.h>
11 #include <linux/delay.h>
12 #include <asm/div64.h>
13 #include <media/v4l2-device.h>
14 #include <media/v4l2-chip-ident.h>
17 MODULE_DESCRIPTION("Micron mt9v011 sensor driver");
18 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
19 MODULE_LICENSE("GPL");
23 module_param(debug
, int, 0);
24 MODULE_PARM_DESC(debug
, "Debug level (0-2)");
26 /* supported controls */
27 static struct v4l2_queryctrl mt9v011_qctrl
[] = {
30 .type
= V4L2_CTRL_TYPE_INTEGER
,
33 .maximum
= (1 << 10) - 1,
35 .default_value
= 0x0020,
38 .id
= V4L2_CID_RED_BALANCE
,
39 .type
= V4L2_CTRL_TYPE_INTEGER
,
40 .name
= "Red Balance",
42 .maximum
= (1 << 9) - 1,
47 .id
= V4L2_CID_BLUE_BALANCE
,
48 .type
= V4L2_CTRL_TYPE_INTEGER
,
49 .name
= "Blue Balance",
51 .maximum
= (1 << 9) - 1,
57 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
66 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
78 struct v4l2_subdev sd
;
79 unsigned width
, height
;
84 u16 global_gain
, red_bal
, blue_bal
;
87 static inline struct mt9v011
*to_mt9v011(struct v4l2_subdev
*sd
)
89 return container_of(sd
, struct mt9v011
, sd
);
92 static int mt9v011_read(struct v4l2_subdev
*sd
, unsigned char addr
)
94 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
98 rc
= i2c_master_send(c
, &addr
, 1);
100 v4l2_dbg(0, debug
, sd
,
101 "i2c i/o error: rc == %d (should be 1)\n", rc
);
105 rc
= i2c_master_recv(c
, (char *)&buffer
, 2);
107 v4l2_dbg(0, debug
, sd
,
108 "i2c i/o error: rc == %d (should be 2)\n", rc
);
110 val
= be16_to_cpu(buffer
);
112 v4l2_dbg(2, debug
, sd
, "mt9v011: read 0x%02x = 0x%04x\n", addr
, val
);
117 static void mt9v011_write(struct v4l2_subdev
*sd
, unsigned char addr
,
120 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
121 unsigned char buffer
[3];
125 buffer
[1] = value
>> 8;
126 buffer
[2] = value
& 0xff;
128 v4l2_dbg(2, debug
, sd
,
129 "mt9v011: writing 0x%02x 0x%04x\n", buffer
[0], value
);
130 rc
= i2c_master_send(c
, buffer
, 3);
132 v4l2_dbg(0, debug
, sd
,
133 "i2c i/o error: rc == %d (should be 3)\n", rc
);
137 struct i2c_reg_value
{
143 * Values used at the original driver
144 * Some values are marked as Reserved at the datasheet
146 static const struct i2c_reg_value mt9v011_init_default
[] = {
147 { R0D_MT9V011_RESET
, 0x0001 },
148 { R0D_MT9V011_RESET
, 0x0000 },
150 { R0C_MT9V011_SHUTTER_DELAY
, 0x0000 },
151 { R09_MT9V011_SHUTTER_WIDTH
, 0x1fc },
153 { R0A_MT9V011_CLK_SPEED
, 0x0000 },
154 { R1E_MT9V011_DIGITAL_ZOOM
, 0x0000 },
156 { R07_MT9V011_OUT_CTRL
, 0x0002 }, /* chip enable */
159 static void set_balance(struct v4l2_subdev
*sd
)
161 struct mt9v011
*core
= to_mt9v011(sd
);
162 u16 green1_gain
, green2_gain
, blue_gain
, red_gain
;
164 green1_gain
= core
->global_gain
;
165 green2_gain
= core
->global_gain
;
167 blue_gain
= core
->global_gain
+
168 core
->global_gain
* core
->blue_bal
/ (1 << 9);
170 red_gain
= core
->global_gain
+
171 core
->global_gain
* core
->blue_bal
/ (1 << 9);
173 mt9v011_write(sd
, R2B_MT9V011_GREEN_1_GAIN
, green1_gain
);
174 mt9v011_write(sd
, R2E_MT9V011_GREEN_2_GAIN
, green1_gain
);
175 mt9v011_write(sd
, R2C_MT9V011_BLUE_GAIN
, blue_gain
);
176 mt9v011_write(sd
, R2D_MT9V011_RED_GAIN
, red_gain
);
179 static void calc_fps(struct v4l2_subdev
*sd
, u32
*numerator
, u32
*denominator
)
181 struct mt9v011
*core
= to_mt9v011(sd
);
182 unsigned height
, width
, hblank
, vblank
, speed
;
183 unsigned row_time
, t_time
;
187 height
= mt9v011_read(sd
, R03_MT9V011_HEIGHT
);
188 width
= mt9v011_read(sd
, R04_MT9V011_WIDTH
);
189 hblank
= mt9v011_read(sd
, R05_MT9V011_HBLANK
);
190 vblank
= mt9v011_read(sd
, R06_MT9V011_VBLANK
);
191 speed
= mt9v011_read(sd
, R0A_MT9V011_CLK_SPEED
);
193 row_time
= (width
+ 113 + hblank
) * (speed
+ 2);
194 t_time
= row_time
* (height
+ vblank
+ 1);
196 frames_per_ms
= core
->xtal
* 1000l;
197 do_div(frames_per_ms
, t_time
);
200 v4l2_dbg(1, debug
, sd
, "Programmed to %u.%03u fps (%d pixel clcks)\n",
201 tmp
/ 1000, tmp
% 1000, t_time
);
203 if (numerator
&& denominator
) {
205 *denominator
= (u32
)frames_per_ms
;
209 static u16
calc_speed(struct v4l2_subdev
*sd
, u32 numerator
, u32 denominator
)
211 struct mt9v011
*core
= to_mt9v011(sd
);
212 unsigned height
, width
, hblank
, vblank
;
213 unsigned row_time
, line_time
;
216 /* Avoid bogus calculus */
217 if (!numerator
|| !denominator
)
220 height
= mt9v011_read(sd
, R03_MT9V011_HEIGHT
);
221 width
= mt9v011_read(sd
, R04_MT9V011_WIDTH
);
222 hblank
= mt9v011_read(sd
, R05_MT9V011_HBLANK
);
223 vblank
= mt9v011_read(sd
, R06_MT9V011_VBLANK
);
225 row_time
= width
+ 113 + hblank
;
226 line_time
= height
+ vblank
+ 1;
228 t_time
= core
->xtal
* ((u64
)numerator
);
229 /* round to the closest value */
230 t_time
+= denominator
/ 2;
231 do_div(t_time
, denominator
);
234 do_div(speed
, row_time
* line_time
);
236 /* Avoid having a negative value for speed */
242 /* Avoid speed overflow */
249 static void set_res(struct v4l2_subdev
*sd
)
251 struct mt9v011
*core
= to_mt9v011(sd
);
252 unsigned vstart
, hstart
;
255 * The mt9v011 doesn't have scaling. So, in order to select the desired
256 * resolution, we're cropping at the middle of the sensor.
257 * hblank and vblank should be adjusted, in order to warrant that
258 * we'll preserve the line timings for 30 fps, no matter what resolution
260 * NOTE: datasheet says that width (and height) should be filled with
261 * width-1. However, this doesn't work, since one pixel per line will
265 hstart
= 14 + (640 - core
->width
) / 2;
266 mt9v011_write(sd
, R02_MT9V011_COLSTART
, hstart
);
267 mt9v011_write(sd
, R04_MT9V011_WIDTH
, core
->width
);
268 mt9v011_write(sd
, R05_MT9V011_HBLANK
, 771 - core
->width
);
270 vstart
= 8 + (480 - core
->height
) / 2;
271 mt9v011_write(sd
, R01_MT9V011_ROWSTART
, vstart
);
272 mt9v011_write(sd
, R03_MT9V011_HEIGHT
, core
->height
);
273 mt9v011_write(sd
, R06_MT9V011_VBLANK
, 508 - core
->height
);
275 calc_fps(sd
, NULL
, NULL
);
278 static void set_read_mode(struct v4l2_subdev
*sd
)
280 struct mt9v011
*core
= to_mt9v011(sd
);
281 unsigned mode
= 0x1000;
289 mt9v011_write(sd
, R20_MT9V011_READ_MODE
, mode
);
292 static int mt9v011_reset(struct v4l2_subdev
*sd
, u32 val
)
296 for (i
= 0; i
< ARRAY_SIZE(mt9v011_init_default
); i
++)
297 mt9v011_write(sd
, mt9v011_init_default
[i
].reg
,
298 mt9v011_init_default
[i
].value
);
307 static int mt9v011_g_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
309 struct mt9v011
*core
= to_mt9v011(sd
);
311 v4l2_dbg(1, debug
, sd
, "g_ctrl called\n");
315 ctrl
->value
= core
->global_gain
;
317 case V4L2_CID_RED_BALANCE
:
318 ctrl
->value
= core
->red_bal
;
320 case V4L2_CID_BLUE_BALANCE
:
321 ctrl
->value
= core
->blue_bal
;
324 ctrl
->value
= core
->hflip
? 1 : 0;
327 ctrl
->value
= core
->vflip
? 1 : 0;
333 static int mt9v011_queryctrl(struct v4l2_subdev
*sd
, struct v4l2_queryctrl
*qc
)
337 v4l2_dbg(1, debug
, sd
, "queryctrl called\n");
339 for (i
= 0; i
< ARRAY_SIZE(mt9v011_qctrl
); i
++)
340 if (qc
->id
&& qc
->id
== mt9v011_qctrl
[i
].id
) {
341 memcpy(qc
, &(mt9v011_qctrl
[i
]),
350 static int mt9v011_s_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
352 struct mt9v011
*core
= to_mt9v011(sd
);
354 n
= ARRAY_SIZE(mt9v011_qctrl
);
356 for (i
= 0; i
< n
; i
++) {
357 if (ctrl
->id
!= mt9v011_qctrl
[i
].id
)
359 if (ctrl
->value
< mt9v011_qctrl
[i
].minimum
||
360 ctrl
->value
> mt9v011_qctrl
[i
].maximum
)
362 v4l2_dbg(1, debug
, sd
, "s_ctrl: id=%d, value=%d\n",
363 ctrl
->id
, ctrl
->value
);
369 core
->global_gain
= ctrl
->value
;
371 case V4L2_CID_RED_BALANCE
:
372 core
->red_bal
= ctrl
->value
;
374 case V4L2_CID_BLUE_BALANCE
:
375 core
->blue_bal
= ctrl
->value
;
378 core
->hflip
= ctrl
->value
;
382 core
->vflip
= ctrl
->value
;
394 static int mt9v011_enum_mbus_fmt(struct v4l2_subdev
*sd
, unsigned index
,
395 enum v4l2_mbus_pixelcode
*code
)
400 *code
= V4L2_MBUS_FMT_SGRBG8_1X8
;
404 static int mt9v011_try_mbus_fmt(struct v4l2_subdev
*sd
, struct v4l2_mbus_framefmt
*fmt
)
406 if (fmt
->code
!= V4L2_MBUS_FMT_SGRBG8_1X8
)
409 v4l_bound_align_image(&fmt
->width
, 48, 639, 1,
410 &fmt
->height
, 32, 480, 1, 0);
411 fmt
->field
= V4L2_FIELD_NONE
;
412 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
417 static int mt9v011_g_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*parms
)
419 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
421 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
424 memset(cp
, 0, sizeof(struct v4l2_captureparm
));
425 cp
->capability
= V4L2_CAP_TIMEPERFRAME
;
427 &cp
->timeperframe
.numerator
,
428 &cp
->timeperframe
.denominator
);
433 static int mt9v011_s_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*parms
)
435 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
436 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
439 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
441 if (cp
->extendedmode
!= 0)
444 speed
= calc_speed(sd
, tpf
->numerator
, tpf
->denominator
);
446 mt9v011_write(sd
, R0A_MT9V011_CLK_SPEED
, speed
);
447 v4l2_dbg(1, debug
, sd
, "Setting speed to %d\n", speed
);
449 /* Recalculate and update fps info */
450 calc_fps(sd
, &tpf
->numerator
, &tpf
->denominator
);
455 static int mt9v011_s_mbus_fmt(struct v4l2_subdev
*sd
, struct v4l2_mbus_framefmt
*fmt
)
457 struct mt9v011
*core
= to_mt9v011(sd
);
460 rc
= mt9v011_try_mbus_fmt(sd
, fmt
);
464 core
->width
= fmt
->width
;
465 core
->height
= fmt
->height
;
472 static int mt9v011_s_config(struct v4l2_subdev
*sd
, int dumb
, void *data
)
474 struct mt9v011
*core
= to_mt9v011(sd
);
475 unsigned *xtal
= data
;
477 v4l2_dbg(1, debug
, sd
, "s_config called\n");
481 v4l2_dbg(1, debug
, sd
, "xtal set to %d.%03d MHz\n",
482 *xtal
/ 1000000, (*xtal
/ 1000) % 1000);
489 #ifdef CONFIG_VIDEO_ADV_DEBUG
490 static int mt9v011_g_register(struct v4l2_subdev
*sd
,
491 struct v4l2_dbg_register
*reg
)
493 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
495 if (!v4l2_chip_match_i2c_client(client
, ®
->match
))
497 if (!capable(CAP_SYS_ADMIN
))
500 reg
->val
= mt9v011_read(sd
, reg
->reg
& 0xff);
506 static int mt9v011_s_register(struct v4l2_subdev
*sd
,
507 struct v4l2_dbg_register
*reg
)
509 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
511 if (!v4l2_chip_match_i2c_client(client
, ®
->match
))
513 if (!capable(CAP_SYS_ADMIN
))
516 mt9v011_write(sd
, reg
->reg
& 0xff, reg
->val
& 0xffff);
522 static int mt9v011_g_chip_ident(struct v4l2_subdev
*sd
,
523 struct v4l2_dbg_chip_ident
*chip
)
526 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
528 version
= mt9v011_read(sd
, R00_MT9V011_CHIP_VERSION
);
530 return v4l2_chip_ident_i2c_client(client
, chip
, V4L2_IDENT_MT9V011
,
534 static const struct v4l2_subdev_core_ops mt9v011_core_ops
= {
535 .queryctrl
= mt9v011_queryctrl
,
536 .g_ctrl
= mt9v011_g_ctrl
,
537 .s_ctrl
= mt9v011_s_ctrl
,
538 .reset
= mt9v011_reset
,
539 .s_config
= mt9v011_s_config
,
540 .g_chip_ident
= mt9v011_g_chip_ident
,
541 #ifdef CONFIG_VIDEO_ADV_DEBUG
542 .g_register
= mt9v011_g_register
,
543 .s_register
= mt9v011_s_register
,
547 static const struct v4l2_subdev_video_ops mt9v011_video_ops
= {
548 .enum_mbus_fmt
= mt9v011_enum_mbus_fmt
,
549 .try_mbus_fmt
= mt9v011_try_mbus_fmt
,
550 .s_mbus_fmt
= mt9v011_s_mbus_fmt
,
551 .g_parm
= mt9v011_g_parm
,
552 .s_parm
= mt9v011_s_parm
,
555 static const struct v4l2_subdev_ops mt9v011_ops
= {
556 .core
= &mt9v011_core_ops
,
557 .video
= &mt9v011_video_ops
,
561 /****************************************************************************
563 ****************************************************************************/
565 static int mt9v011_probe(struct i2c_client
*c
,
566 const struct i2c_device_id
*id
)
569 struct mt9v011
*core
;
570 struct v4l2_subdev
*sd
;
572 /* Check if the adapter supports the needed features */
573 if (!i2c_check_functionality(c
->adapter
,
574 I2C_FUNC_SMBUS_READ_BYTE
| I2C_FUNC_SMBUS_WRITE_BYTE_DATA
))
577 core
= kzalloc(sizeof(struct mt9v011
), GFP_KERNEL
);
582 v4l2_i2c_subdev_init(sd
, c
, &mt9v011_ops
);
584 /* Check if the sensor is really a MT9V011 */
585 version
= mt9v011_read(sd
, R00_MT9V011_CHIP_VERSION
);
586 if ((version
!= MT9V011_VERSION
) &&
587 (version
!= MT9V011_REV_B_VERSION
)) {
588 v4l2_info(sd
, "*** unknown micron chip detected (0x%04x).\n",
594 core
->global_gain
= 0x0024;
597 core
->xtal
= 27000000; /* Hz */
599 v4l_info(c
, "chip found @ 0x%02x (%s - chip version 0x%04x)\n",
600 c
->addr
<< 1, c
->adapter
->name
, version
);
605 static int mt9v011_remove(struct i2c_client
*c
)
607 struct v4l2_subdev
*sd
= i2c_get_clientdata(c
);
609 v4l2_dbg(1, debug
, sd
,
610 "mt9v011.c: removing mt9v011 adapter on address 0x%x\n",
613 v4l2_device_unregister_subdev(sd
);
614 kfree(to_mt9v011(sd
));
618 /* ----------------------------------------------------------------------- */
620 static const struct i2c_device_id mt9v011_id
[] = {
624 MODULE_DEVICE_TABLE(i2c
, mt9v011_id
);
626 static struct i2c_driver mt9v011_driver
= {
628 .owner
= THIS_MODULE
,
631 .probe
= mt9v011_probe
,
632 .remove
= mt9v011_remove
,
633 .id_table
= mt9v011_id
,
636 static __init
int init_mt9v011(void)
638 return i2c_add_driver(&mt9v011_driver
);
641 static __exit
void exit_mt9v011(void)
643 i2c_del_driver(&mt9v011_driver
);
646 module_init(init_mt9v011
);
647 module_exit(exit_mt9v011
);