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 <linux/module.h>
13 #include <asm/div64.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-chip-ident.h>
16 #include <media/v4l2-ctrls.h>
17 #include <media/mt9v011.h>
19 MODULE_DESCRIPTION("Micron mt9v011 sensor driver");
20 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
21 MODULE_LICENSE("GPL");
24 module_param(debug
, int, 0);
25 MODULE_PARM_DESC(debug
, "Debug level (0-2)");
27 #define R00_MT9V011_CHIP_VERSION 0x00
28 #define R01_MT9V011_ROWSTART 0x01
29 #define R02_MT9V011_COLSTART 0x02
30 #define R03_MT9V011_HEIGHT 0x03
31 #define R04_MT9V011_WIDTH 0x04
32 #define R05_MT9V011_HBLANK 0x05
33 #define R06_MT9V011_VBLANK 0x06
34 #define R07_MT9V011_OUT_CTRL 0x07
35 #define R09_MT9V011_SHUTTER_WIDTH 0x09
36 #define R0A_MT9V011_CLK_SPEED 0x0a
37 #define R0B_MT9V011_RESTART 0x0b
38 #define R0C_MT9V011_SHUTTER_DELAY 0x0c
39 #define R0D_MT9V011_RESET 0x0d
40 #define R1E_MT9V011_DIGITAL_ZOOM 0x1e
41 #define R20_MT9V011_READ_MODE 0x20
42 #define R2B_MT9V011_GREEN_1_GAIN 0x2b
43 #define R2C_MT9V011_BLUE_GAIN 0x2c
44 #define R2D_MT9V011_RED_GAIN 0x2d
45 #define R2E_MT9V011_GREEN_2_GAIN 0x2e
46 #define R35_MT9V011_GLOBAL_GAIN 0x35
47 #define RF1_MT9V011_CHIP_ENABLE 0xf1
49 #define MT9V011_VERSION 0x8232
50 #define MT9V011_REV_B_VERSION 0x8243
53 struct v4l2_subdev sd
;
54 struct v4l2_ctrl_handler ctrls
;
55 unsigned width
, height
;
60 u16 global_gain
, exposure
;
61 s16 red_bal
, blue_bal
;
64 static inline struct mt9v011
*to_mt9v011(struct v4l2_subdev
*sd
)
66 return container_of(sd
, struct mt9v011
, sd
);
69 static int mt9v011_read(struct v4l2_subdev
*sd
, unsigned char addr
)
71 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
75 rc
= i2c_master_send(c
, &addr
, 1);
77 v4l2_dbg(0, debug
, sd
,
78 "i2c i/o error: rc == %d (should be 1)\n", rc
);
82 rc
= i2c_master_recv(c
, (char *)&buffer
, 2);
84 v4l2_dbg(0, debug
, sd
,
85 "i2c i/o error: rc == %d (should be 2)\n", rc
);
87 val
= be16_to_cpu(buffer
);
89 v4l2_dbg(2, debug
, sd
, "mt9v011: read 0x%02x = 0x%04x\n", addr
, val
);
94 static void mt9v011_write(struct v4l2_subdev
*sd
, unsigned char addr
,
97 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
98 unsigned char buffer
[3];
102 buffer
[1] = value
>> 8;
103 buffer
[2] = value
& 0xff;
105 v4l2_dbg(2, debug
, sd
,
106 "mt9v011: writing 0x%02x 0x%04x\n", buffer
[0], value
);
107 rc
= i2c_master_send(c
, buffer
, 3);
109 v4l2_dbg(0, debug
, sd
,
110 "i2c i/o error: rc == %d (should be 3)\n", rc
);
114 struct i2c_reg_value
{
120 * Values used at the original driver
121 * Some values are marked as Reserved at the datasheet
123 static const struct i2c_reg_value mt9v011_init_default
[] = {
124 { R0D_MT9V011_RESET
, 0x0001 },
125 { R0D_MT9V011_RESET
, 0x0000 },
127 { R0C_MT9V011_SHUTTER_DELAY
, 0x0000 },
128 { R09_MT9V011_SHUTTER_WIDTH
, 0x1fc },
130 { R0A_MT9V011_CLK_SPEED
, 0x0000 },
131 { R1E_MT9V011_DIGITAL_ZOOM
, 0x0000 },
133 { R07_MT9V011_OUT_CTRL
, 0x0002 }, /* chip enable */
137 static u16
calc_mt9v011_gain(s16 lineargain
)
147 /* recommended minimum */
148 lineargain
+= 0x0020;
150 if (lineargain
> 2047)
153 if (lineargain
> 1023) {
156 analoginit
= lineargain
/ 16;
157 } else if (lineargain
> 511) {
160 analoginit
= lineargain
/ 8;
161 } else if (lineargain
> 255) {
163 analoginit
= lineargain
/ 4;
164 } else if (lineargain
> 127) {
166 analoginit
= lineargain
/ 2;
168 analoginit
= lineargain
;
170 return analoginit
+ (analogmult
<< 7) + (digitalgain
<< 9);
174 static void set_balance(struct v4l2_subdev
*sd
)
176 struct mt9v011
*core
= to_mt9v011(sd
);
177 u16 green_gain
, blue_gain
, red_gain
;
181 exposure
= core
->exposure
;
183 green_gain
= calc_mt9v011_gain(core
->global_gain
);
185 bal
= core
->global_gain
;
186 bal
+= (core
->blue_bal
* core
->global_gain
/ (1 << 7));
187 blue_gain
= calc_mt9v011_gain(bal
);
189 bal
= core
->global_gain
;
190 bal
+= (core
->red_bal
* core
->global_gain
/ (1 << 7));
191 red_gain
= calc_mt9v011_gain(bal
);
193 mt9v011_write(sd
, R2B_MT9V011_GREEN_1_GAIN
, green_gain
);
194 mt9v011_write(sd
, R2E_MT9V011_GREEN_2_GAIN
, green_gain
);
195 mt9v011_write(sd
, R2C_MT9V011_BLUE_GAIN
, blue_gain
);
196 mt9v011_write(sd
, R2D_MT9V011_RED_GAIN
, red_gain
);
197 mt9v011_write(sd
, R09_MT9V011_SHUTTER_WIDTH
, exposure
);
200 static void calc_fps(struct v4l2_subdev
*sd
, u32
*numerator
, u32
*denominator
)
202 struct mt9v011
*core
= to_mt9v011(sd
);
203 unsigned height
, width
, hblank
, vblank
, speed
;
204 unsigned row_time
, t_time
;
208 height
= mt9v011_read(sd
, R03_MT9V011_HEIGHT
);
209 width
= mt9v011_read(sd
, R04_MT9V011_WIDTH
);
210 hblank
= mt9v011_read(sd
, R05_MT9V011_HBLANK
);
211 vblank
= mt9v011_read(sd
, R06_MT9V011_VBLANK
);
212 speed
= mt9v011_read(sd
, R0A_MT9V011_CLK_SPEED
);
214 row_time
= (width
+ 113 + hblank
) * (speed
+ 2);
215 t_time
= row_time
* (height
+ vblank
+ 1);
217 frames_per_ms
= core
->xtal
* 1000l;
218 do_div(frames_per_ms
, t_time
);
221 v4l2_dbg(1, debug
, sd
, "Programmed to %u.%03u fps (%d pixel clcks)\n",
222 tmp
/ 1000, tmp
% 1000, t_time
);
224 if (numerator
&& denominator
) {
226 *denominator
= (u32
)frames_per_ms
;
230 static u16
calc_speed(struct v4l2_subdev
*sd
, u32 numerator
, u32 denominator
)
232 struct mt9v011
*core
= to_mt9v011(sd
);
233 unsigned height
, width
, hblank
, vblank
;
234 unsigned row_time
, line_time
;
237 /* Avoid bogus calculus */
238 if (!numerator
|| !denominator
)
241 height
= mt9v011_read(sd
, R03_MT9V011_HEIGHT
);
242 width
= mt9v011_read(sd
, R04_MT9V011_WIDTH
);
243 hblank
= mt9v011_read(sd
, R05_MT9V011_HBLANK
);
244 vblank
= mt9v011_read(sd
, R06_MT9V011_VBLANK
);
246 row_time
= width
+ 113 + hblank
;
247 line_time
= height
+ vblank
+ 1;
249 t_time
= core
->xtal
* ((u64
)numerator
);
250 /* round to the closest value */
251 t_time
+= denominator
/ 2;
252 do_div(t_time
, denominator
);
255 do_div(speed
, row_time
* line_time
);
257 /* Avoid having a negative value for speed */
263 /* Avoid speed overflow */
270 static void set_res(struct v4l2_subdev
*sd
)
272 struct mt9v011
*core
= to_mt9v011(sd
);
273 unsigned vstart
, hstart
;
276 * The mt9v011 doesn't have scaling. So, in order to select the desired
277 * resolution, we're cropping at the middle of the sensor.
278 * hblank and vblank should be adjusted, in order to warrant that
279 * we'll preserve the line timings for 30 fps, no matter what resolution
281 * NOTE: datasheet says that width (and height) should be filled with
282 * width-1. However, this doesn't work, since one pixel per line will
286 hstart
= 20 + (640 - core
->width
) / 2;
287 mt9v011_write(sd
, R02_MT9V011_COLSTART
, hstart
);
288 mt9v011_write(sd
, R04_MT9V011_WIDTH
, core
->width
);
289 mt9v011_write(sd
, R05_MT9V011_HBLANK
, 771 - core
->width
);
291 vstart
= 8 + (480 - core
->height
) / 2;
292 mt9v011_write(sd
, R01_MT9V011_ROWSTART
, vstart
);
293 mt9v011_write(sd
, R03_MT9V011_HEIGHT
, core
->height
);
294 mt9v011_write(sd
, R06_MT9V011_VBLANK
, 508 - core
->height
);
296 calc_fps(sd
, NULL
, NULL
);
299 static void set_read_mode(struct v4l2_subdev
*sd
)
301 struct mt9v011
*core
= to_mt9v011(sd
);
302 unsigned mode
= 0x1000;
310 mt9v011_write(sd
, R20_MT9V011_READ_MODE
, mode
);
313 static int mt9v011_reset(struct v4l2_subdev
*sd
, u32 val
)
317 for (i
= 0; i
< ARRAY_SIZE(mt9v011_init_default
); i
++)
318 mt9v011_write(sd
, mt9v011_init_default
[i
].reg
,
319 mt9v011_init_default
[i
].value
);
328 static int mt9v011_enum_mbus_fmt(struct v4l2_subdev
*sd
, unsigned index
,
329 enum v4l2_mbus_pixelcode
*code
)
334 *code
= V4L2_MBUS_FMT_SGRBG8_1X8
;
338 static int mt9v011_try_mbus_fmt(struct v4l2_subdev
*sd
, struct v4l2_mbus_framefmt
*fmt
)
340 if (fmt
->code
!= V4L2_MBUS_FMT_SGRBG8_1X8
)
343 v4l_bound_align_image(&fmt
->width
, 48, 639, 1,
344 &fmt
->height
, 32, 480, 1, 0);
345 fmt
->field
= V4L2_FIELD_NONE
;
346 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
351 static int mt9v011_g_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*parms
)
353 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
355 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
358 memset(cp
, 0, sizeof(struct v4l2_captureparm
));
359 cp
->capability
= V4L2_CAP_TIMEPERFRAME
;
361 &cp
->timeperframe
.numerator
,
362 &cp
->timeperframe
.denominator
);
367 static int mt9v011_s_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*parms
)
369 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
370 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
373 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
375 if (cp
->extendedmode
!= 0)
378 speed
= calc_speed(sd
, tpf
->numerator
, tpf
->denominator
);
380 mt9v011_write(sd
, R0A_MT9V011_CLK_SPEED
, speed
);
381 v4l2_dbg(1, debug
, sd
, "Setting speed to %d\n", speed
);
383 /* Recalculate and update fps info */
384 calc_fps(sd
, &tpf
->numerator
, &tpf
->denominator
);
389 static int mt9v011_s_mbus_fmt(struct v4l2_subdev
*sd
, struct v4l2_mbus_framefmt
*fmt
)
391 struct mt9v011
*core
= to_mt9v011(sd
);
394 rc
= mt9v011_try_mbus_fmt(sd
, fmt
);
398 core
->width
= fmt
->width
;
399 core
->height
= fmt
->height
;
406 #ifdef CONFIG_VIDEO_ADV_DEBUG
407 static int mt9v011_g_register(struct v4l2_subdev
*sd
,
408 struct v4l2_dbg_register
*reg
)
410 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
412 if (!v4l2_chip_match_i2c_client(client
, ®
->match
))
414 if (!capable(CAP_SYS_ADMIN
))
417 reg
->val
= mt9v011_read(sd
, reg
->reg
& 0xff);
423 static int mt9v011_s_register(struct v4l2_subdev
*sd
,
424 const struct v4l2_dbg_register
*reg
)
426 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
428 if (!v4l2_chip_match_i2c_client(client
, ®
->match
))
430 if (!capable(CAP_SYS_ADMIN
))
433 mt9v011_write(sd
, reg
->reg
& 0xff, reg
->val
& 0xffff);
439 static int mt9v011_g_chip_ident(struct v4l2_subdev
*sd
,
440 struct v4l2_dbg_chip_ident
*chip
)
443 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
445 version
= mt9v011_read(sd
, R00_MT9V011_CHIP_VERSION
);
447 return v4l2_chip_ident_i2c_client(client
, chip
, V4L2_IDENT_MT9V011
,
451 static int mt9v011_s_ctrl(struct v4l2_ctrl
*ctrl
)
453 struct mt9v011
*core
=
454 container_of(ctrl
->handler
, struct mt9v011
, ctrls
);
455 struct v4l2_subdev
*sd
= &core
->sd
;
459 core
->global_gain
= ctrl
->val
;
461 case V4L2_CID_EXPOSURE
:
462 core
->exposure
= ctrl
->val
;
464 case V4L2_CID_RED_BALANCE
:
465 core
->red_bal
= ctrl
->val
;
467 case V4L2_CID_BLUE_BALANCE
:
468 core
->blue_bal
= ctrl
->val
;
471 core
->hflip
= ctrl
->val
;
475 core
->vflip
= ctrl
->val
;
486 static struct v4l2_ctrl_ops mt9v011_ctrl_ops
= {
487 .s_ctrl
= mt9v011_s_ctrl
,
490 static const struct v4l2_subdev_core_ops mt9v011_core_ops
= {
491 .reset
= mt9v011_reset
,
492 .g_chip_ident
= mt9v011_g_chip_ident
,
493 #ifdef CONFIG_VIDEO_ADV_DEBUG
494 .g_register
= mt9v011_g_register
,
495 .s_register
= mt9v011_s_register
,
499 static const struct v4l2_subdev_video_ops mt9v011_video_ops
= {
500 .enum_mbus_fmt
= mt9v011_enum_mbus_fmt
,
501 .try_mbus_fmt
= mt9v011_try_mbus_fmt
,
502 .s_mbus_fmt
= mt9v011_s_mbus_fmt
,
503 .g_parm
= mt9v011_g_parm
,
504 .s_parm
= mt9v011_s_parm
,
507 static const struct v4l2_subdev_ops mt9v011_ops
= {
508 .core
= &mt9v011_core_ops
,
509 .video
= &mt9v011_video_ops
,
513 /****************************************************************************
515 ****************************************************************************/
517 static int mt9v011_probe(struct i2c_client
*c
,
518 const struct i2c_device_id
*id
)
521 struct mt9v011
*core
;
522 struct v4l2_subdev
*sd
;
524 /* Check if the adapter supports the needed features */
525 if (!i2c_check_functionality(c
->adapter
,
526 I2C_FUNC_SMBUS_READ_BYTE
| I2C_FUNC_SMBUS_WRITE_BYTE_DATA
))
529 core
= kzalloc(sizeof(struct mt9v011
), GFP_KERNEL
);
534 v4l2_i2c_subdev_init(sd
, c
, &mt9v011_ops
);
536 /* Check if the sensor is really a MT9V011 */
537 version
= mt9v011_read(sd
, R00_MT9V011_CHIP_VERSION
);
538 if ((version
!= MT9V011_VERSION
) &&
539 (version
!= MT9V011_REV_B_VERSION
)) {
540 v4l2_info(sd
, "*** unknown micron chip detected (0x%04x).\n",
546 v4l2_ctrl_handler_init(&core
->ctrls
, 5);
547 v4l2_ctrl_new_std(&core
->ctrls
, &mt9v011_ctrl_ops
,
548 V4L2_CID_GAIN
, 0, (1 << 12) - 1 - 0x20, 1, 0x20);
549 v4l2_ctrl_new_std(&core
->ctrls
, &mt9v011_ctrl_ops
,
550 V4L2_CID_EXPOSURE
, 0, 2047, 1, 0x01fc);
551 v4l2_ctrl_new_std(&core
->ctrls
, &mt9v011_ctrl_ops
,
552 V4L2_CID_RED_BALANCE
, -(1 << 9), (1 << 9) - 1, 1, 0);
553 v4l2_ctrl_new_std(&core
->ctrls
, &mt9v011_ctrl_ops
,
554 V4L2_CID_BLUE_BALANCE
, -(1 << 9), (1 << 9) - 1, 1, 0);
555 v4l2_ctrl_new_std(&core
->ctrls
, &mt9v011_ctrl_ops
,
556 V4L2_CID_HFLIP
, 0, 1, 1, 0);
557 v4l2_ctrl_new_std(&core
->ctrls
, &mt9v011_ctrl_ops
,
558 V4L2_CID_VFLIP
, 0, 1, 1, 0);
560 if (core
->ctrls
.error
) {
561 int ret
= core
->ctrls
.error
;
563 v4l2_err(sd
, "control initialization error %d\n", ret
);
564 v4l2_ctrl_handler_free(&core
->ctrls
);
568 core
->sd
.ctrl_handler
= &core
->ctrls
;
570 core
->global_gain
= 0x0024;
571 core
->exposure
= 0x01fc;
574 core
->xtal
= 27000000; /* Hz */
576 if (c
->dev
.platform_data
) {
577 struct mt9v011_platform_data
*pdata
= c
->dev
.platform_data
;
579 core
->xtal
= pdata
->xtal
;
580 v4l2_dbg(1, debug
, sd
, "xtal set to %d.%03d MHz\n",
581 core
->xtal
/ 1000000, (core
->xtal
/ 1000) % 1000);
584 v4l_info(c
, "chip found @ 0x%02x (%s - chip version 0x%04x)\n",
585 c
->addr
<< 1, c
->adapter
->name
, version
);
590 static int mt9v011_remove(struct i2c_client
*c
)
592 struct v4l2_subdev
*sd
= i2c_get_clientdata(c
);
593 struct mt9v011
*core
= to_mt9v011(sd
);
595 v4l2_dbg(1, debug
, sd
,
596 "mt9v011.c: removing mt9v011 adapter on address 0x%x\n",
599 v4l2_device_unregister_subdev(sd
);
600 v4l2_ctrl_handler_free(&core
->ctrls
);
601 kfree(to_mt9v011(sd
));
605 /* ----------------------------------------------------------------------- */
607 static const struct i2c_device_id mt9v011_id
[] = {
611 MODULE_DEVICE_TABLE(i2c
, mt9v011_id
);
613 static struct i2c_driver mt9v011_driver
= {
615 .owner
= THIS_MODULE
,
618 .probe
= mt9v011_probe
,
619 .remove
= mt9v011_remove
,
620 .id_table
= mt9v011_id
,
623 module_i2c_driver(mt9v011_driver
);