2 * Driver for MT9M001 CMOS Image Sensor from Micron
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
16 #include <media/v4l2-common.h>
17 #include <media/v4l2-chip-ident.h>
18 #include <media/soc_camera.h>
20 /* mt9m001 i2c address 0x5d
21 * The platform has to define i2c_board_info
22 * and call i2c_register_board_info() */
24 /* mt9m001 selected register addresses */
25 #define MT9M001_CHIP_VERSION 0x00
26 #define MT9M001_ROW_START 0x01
27 #define MT9M001_COLUMN_START 0x02
28 #define MT9M001_WINDOW_HEIGHT 0x03
29 #define MT9M001_WINDOW_WIDTH 0x04
30 #define MT9M001_HORIZONTAL_BLANKING 0x05
31 #define MT9M001_VERTICAL_BLANKING 0x06
32 #define MT9M001_OUTPUT_CONTROL 0x07
33 #define MT9M001_SHUTTER_WIDTH 0x09
34 #define MT9M001_FRAME_RESTART 0x0b
35 #define MT9M001_SHUTTER_DELAY 0x0c
36 #define MT9M001_RESET 0x0d
37 #define MT9M001_READ_OPTIONS1 0x1e
38 #define MT9M001_READ_OPTIONS2 0x20
39 #define MT9M001_GLOBAL_GAIN 0x35
40 #define MT9M001_CHIP_ENABLE 0xF1
42 static const struct soc_camera_data_format mt9m001_colour_formats
[] = {
43 /* Order important: first natively supported,
44 * second supported with a GPIO extender */
46 .name
= "Bayer (sRGB) 10 bit",
48 .fourcc
= V4L2_PIX_FMT_SBGGR16
,
49 .colorspace
= V4L2_COLORSPACE_SRGB
,
51 .name
= "Bayer (sRGB) 8 bit",
53 .fourcc
= V4L2_PIX_FMT_SBGGR8
,
54 .colorspace
= V4L2_COLORSPACE_SRGB
,
58 static const struct soc_camera_data_format mt9m001_monochrome_formats
[] = {
59 /* Order important - see above */
61 .name
= "Monochrome 10 bit",
63 .fourcc
= V4L2_PIX_FMT_Y16
,
65 .name
= "Monochrome 8 bit",
67 .fourcc
= V4L2_PIX_FMT_GREY
,
72 struct i2c_client
*client
;
73 struct soc_camera_device icd
;
74 int model
; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
75 unsigned char autoexposure
;
78 static int reg_read(struct i2c_client
*client
, const u8 reg
)
80 s32 data
= i2c_smbus_read_word_data(client
, reg
);
81 return data
< 0 ? data
: swab16(data
);
84 static int reg_write(struct i2c_client
*client
, const u8 reg
,
87 return i2c_smbus_write_word_data(client
, reg
, swab16(data
));
90 static int reg_set(struct i2c_client
*client
, const u8 reg
,
95 ret
= reg_read(client
, reg
);
98 return reg_write(client
, reg
, ret
| data
);
101 static int reg_clear(struct i2c_client
*client
, const u8 reg
,
106 ret
= reg_read(client
, reg
);
109 return reg_write(client
, reg
, ret
& ~data
);
112 static int mt9m001_init(struct soc_camera_device
*icd
)
114 struct i2c_client
*client
= to_i2c_client(icd
->control
);
115 struct soc_camera_link
*icl
= client
->dev
.platform_data
;
118 dev_dbg(icd
->vdev
->parent
, "%s\n", __func__
);
121 ret
= icl
->power(&client
->dev
, 1);
123 dev_err(icd
->vdev
->parent
,
124 "Platform failed to power-on the camera.\n");
129 /* The camera could have been already on, we reset it additionally */
131 ret
= icl
->reset(&client
->dev
);
136 /* Either no platform reset, or platform reset failed */
137 ret
= reg_write(client
, MT9M001_RESET
, 1);
139 ret
= reg_write(client
, MT9M001_RESET
, 0);
141 /* Disable chip, synchronous option update */
143 ret
= reg_write(client
, MT9M001_OUTPUT_CONTROL
, 0);
148 static int mt9m001_release(struct soc_camera_device
*icd
)
150 struct i2c_client
*client
= to_i2c_client(icd
->control
);
151 struct soc_camera_link
*icl
= client
->dev
.platform_data
;
153 /* Disable the chip */
154 reg_write(client
, MT9M001_OUTPUT_CONTROL
, 0);
157 icl
->power(&client
->dev
, 0);
162 static int mt9m001_start_capture(struct soc_camera_device
*icd
)
164 struct i2c_client
*client
= to_i2c_client(icd
->control
);
166 /* Switch to master "normal" mode */
167 if (reg_write(client
, MT9M001_OUTPUT_CONTROL
, 2) < 0)
172 static int mt9m001_stop_capture(struct soc_camera_device
*icd
)
174 struct i2c_client
*client
= to_i2c_client(icd
->control
);
176 /* Stop sensor readout */
177 if (reg_write(client
, MT9M001_OUTPUT_CONTROL
, 0) < 0)
182 static int mt9m001_set_bus_param(struct soc_camera_device
*icd
,
185 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
186 struct soc_camera_link
*icl
= mt9m001
->client
->dev
.platform_data
;
187 unsigned long width_flag
= flags
& SOCAM_DATAWIDTH_MASK
;
189 /* Only one width bit may be set */
190 if (!is_power_of_2(width_flag
))
193 if (icl
->set_bus_param
)
194 return icl
->set_bus_param(icl
, width_flag
);
197 * Without board specific bus width settings we only support the
198 * sensors native bus width
200 if (width_flag
== SOCAM_DATAWIDTH_10
)
206 static unsigned long mt9m001_query_bus_param(struct soc_camera_device
*icd
)
208 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
209 struct soc_camera_link
*icl
= mt9m001
->client
->dev
.platform_data
;
210 /* MT9M001 has all capture_format parameters fixed */
211 unsigned long flags
= SOCAM_PCLK_SAMPLE_FALLING
|
212 SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_HIGH
|
213 SOCAM_DATA_ACTIVE_HIGH
| SOCAM_MASTER
;
215 if (icl
->query_bus_param
)
216 flags
|= icl
->query_bus_param(icl
) & SOCAM_DATAWIDTH_MASK
;
218 flags
|= SOCAM_DATAWIDTH_10
;
220 return soc_camera_apply_sensor_flags(icl
, flags
);
223 static int mt9m001_set_crop(struct soc_camera_device
*icd
,
224 struct v4l2_rect
*rect
)
226 struct i2c_client
*client
= to_i2c_client(icd
->control
);
227 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
229 const u16 hblank
= 9, vblank
= 25;
231 /* Blanking and start values - default... */
232 ret
= reg_write(client
, MT9M001_HORIZONTAL_BLANKING
, hblank
);
234 ret
= reg_write(client
, MT9M001_VERTICAL_BLANKING
, vblank
);
236 /* The caller provides a supported format, as verified per
237 * call to icd->try_fmt() */
239 ret
= reg_write(client
, MT9M001_COLUMN_START
, rect
->left
);
241 ret
= reg_write(client
, MT9M001_ROW_START
, rect
->top
);
243 ret
= reg_write(client
, MT9M001_WINDOW_WIDTH
, rect
->width
- 1);
245 ret
= reg_write(client
, MT9M001_WINDOW_HEIGHT
,
246 rect
->height
+ icd
->y_skip_top
- 1);
247 if (!ret
&& mt9m001
->autoexposure
) {
248 ret
= reg_write(client
, MT9M001_SHUTTER_WIDTH
,
249 rect
->height
+ icd
->y_skip_top
+ vblank
);
251 const struct v4l2_queryctrl
*qctrl
=
252 soc_camera_find_qctrl(icd
->ops
,
254 icd
->exposure
= (524 + (rect
->height
+ icd
->y_skip_top
+
256 (qctrl
->maximum
- qctrl
->minimum
)) /
257 1048 + qctrl
->minimum
;
264 static int mt9m001_set_fmt(struct soc_camera_device
*icd
,
265 struct v4l2_format
*f
)
267 struct v4l2_rect rect
= {
268 .left
= icd
->x_current
,
269 .top
= icd
->y_current
,
270 .width
= f
->fmt
.pix
.width
,
271 .height
= f
->fmt
.pix
.height
,
274 /* No support for scaling so far, just crop. TODO: use skipping */
275 return mt9m001_set_crop(icd
, &rect
);
278 static int mt9m001_try_fmt(struct soc_camera_device
*icd
,
279 struct v4l2_format
*f
)
281 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
283 v4l_bound_align_image(&pix
->width
, 48, 1280, 1,
284 &pix
->height
, 32 + icd
->y_skip_top
,
285 1024 + icd
->y_skip_top
, 0, 0);
290 static int mt9m001_get_chip_id(struct soc_camera_device
*icd
,
291 struct v4l2_dbg_chip_ident
*id
)
293 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
295 if (id
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
298 if (id
->match
.addr
!= mt9m001
->client
->addr
)
301 id
->ident
= mt9m001
->model
;
307 #ifdef CONFIG_VIDEO_ADV_DEBUG
308 static int mt9m001_get_register(struct soc_camera_device
*icd
,
309 struct v4l2_dbg_register
*reg
)
311 struct i2c_client
*client
= to_i2c_client(icd
->control
);
313 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0xff)
316 if (reg
->match
.addr
!= client
->addr
)
320 reg
->val
= reg_read(client
, reg
->reg
);
322 if (reg
->val
> 0xffff)
328 static int mt9m001_set_register(struct soc_camera_device
*icd
,
329 struct v4l2_dbg_register
*reg
)
331 struct i2c_client
*client
= to_i2c_client(icd
->control
);
333 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0xff)
336 if (reg
->match
.addr
!= client
->addr
)
339 if (reg_write(client
, reg
->reg
, reg
->val
) < 0)
346 static const struct v4l2_queryctrl mt9m001_controls
[] = {
348 .id
= V4L2_CID_VFLIP
,
349 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
350 .name
= "Flip Vertically",
357 .type
= V4L2_CTRL_TYPE_INTEGER
,
363 .flags
= V4L2_CTRL_FLAG_SLIDER
,
365 .id
= V4L2_CID_EXPOSURE
,
366 .type
= V4L2_CTRL_TYPE_INTEGER
,
371 .default_value
= 255,
372 .flags
= V4L2_CTRL_FLAG_SLIDER
,
374 .id
= V4L2_CID_EXPOSURE_AUTO
,
375 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
376 .name
= "Automatic Exposure",
384 static int mt9m001_video_probe(struct soc_camera_device
*);
385 static void mt9m001_video_remove(struct soc_camera_device
*);
386 static int mt9m001_get_control(struct soc_camera_device
*, struct v4l2_control
*);
387 static int mt9m001_set_control(struct soc_camera_device
*, struct v4l2_control
*);
389 static struct soc_camera_ops mt9m001_ops
= {
390 .owner
= THIS_MODULE
,
391 .probe
= mt9m001_video_probe
,
392 .remove
= mt9m001_video_remove
,
393 .init
= mt9m001_init
,
394 .release
= mt9m001_release
,
395 .start_capture
= mt9m001_start_capture
,
396 .stop_capture
= mt9m001_stop_capture
,
397 .set_crop
= mt9m001_set_crop
,
398 .set_fmt
= mt9m001_set_fmt
,
399 .try_fmt
= mt9m001_try_fmt
,
400 .set_bus_param
= mt9m001_set_bus_param
,
401 .query_bus_param
= mt9m001_query_bus_param
,
402 .controls
= mt9m001_controls
,
403 .num_controls
= ARRAY_SIZE(mt9m001_controls
),
404 .get_control
= mt9m001_get_control
,
405 .set_control
= mt9m001_set_control
,
406 .get_chip_id
= mt9m001_get_chip_id
,
407 #ifdef CONFIG_VIDEO_ADV_DEBUG
408 .get_register
= mt9m001_get_register
,
409 .set_register
= mt9m001_set_register
,
413 static int mt9m001_get_control(struct soc_camera_device
*icd
, struct v4l2_control
*ctrl
)
415 struct i2c_client
*client
= to_i2c_client(icd
->control
);
416 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
421 data
= reg_read(client
, MT9M001_READ_OPTIONS2
);
424 ctrl
->value
= !!(data
& 0x8000);
426 case V4L2_CID_EXPOSURE_AUTO
:
427 ctrl
->value
= mt9m001
->autoexposure
;
433 static int mt9m001_set_control(struct soc_camera_device
*icd
, struct v4l2_control
*ctrl
)
435 struct i2c_client
*client
= to_i2c_client(icd
->control
);
436 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
437 const struct v4l2_queryctrl
*qctrl
;
440 qctrl
= soc_camera_find_qctrl(&mt9m001_ops
, ctrl
->id
);
448 data
= reg_set(client
, MT9M001_READ_OPTIONS2
, 0x8000);
450 data
= reg_clear(client
, MT9M001_READ_OPTIONS2
, 0x8000);
455 if (ctrl
->value
> qctrl
->maximum
|| ctrl
->value
< qctrl
->minimum
)
457 /* See Datasheet Table 7, Gain settings. */
458 if (ctrl
->value
<= qctrl
->default_value
) {
459 /* Pack it into 0..1 step 0.125, register values 0..8 */
460 unsigned long range
= qctrl
->default_value
- qctrl
->minimum
;
461 data
= ((ctrl
->value
- qctrl
->minimum
) * 8 + range
/ 2) / range
;
463 dev_dbg(&icd
->dev
, "Setting gain %d\n", data
);
464 data
= reg_write(client
, MT9M001_GLOBAL_GAIN
, data
);
468 /* Pack it into 1.125..15 variable step, register values 9..67 */
469 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
470 unsigned long range
= qctrl
->maximum
- qctrl
->default_value
- 1;
471 unsigned long gain
= ((ctrl
->value
- qctrl
->default_value
- 1) *
472 111 + range
/ 2) / range
+ 9;
477 data
= ((gain
- 32) * 16 + 16) / 32 + 80;
479 data
= ((gain
- 64) * 7 + 28) / 56 + 96;
481 dev_dbg(&icd
->dev
, "Setting gain from %d to %d\n",
482 reg_read(client
, MT9M001_GLOBAL_GAIN
), data
);
483 data
= reg_write(client
, MT9M001_GLOBAL_GAIN
, data
);
489 icd
->gain
= ctrl
->value
;
491 case V4L2_CID_EXPOSURE
:
492 /* mt9m001 has maximum == default */
493 if (ctrl
->value
> qctrl
->maximum
|| ctrl
->value
< qctrl
->minimum
)
496 unsigned long range
= qctrl
->maximum
- qctrl
->minimum
;
497 unsigned long shutter
= ((ctrl
->value
- qctrl
->minimum
) * 1048 +
498 range
/ 2) / range
+ 1;
500 dev_dbg(&icd
->dev
, "Setting shutter width from %d to %lu\n",
501 reg_read(client
, MT9M001_SHUTTER_WIDTH
), shutter
);
502 if (reg_write(client
, MT9M001_SHUTTER_WIDTH
, shutter
) < 0)
504 icd
->exposure
= ctrl
->value
;
505 mt9m001
->autoexposure
= 0;
508 case V4L2_CID_EXPOSURE_AUTO
:
510 const u16 vblank
= 25;
511 if (reg_write(client
, MT9M001_SHUTTER_WIDTH
, icd
->height
+
512 icd
->y_skip_top
+ vblank
) < 0)
514 qctrl
= soc_camera_find_qctrl(icd
->ops
, V4L2_CID_EXPOSURE
);
515 icd
->exposure
= (524 + (icd
->height
+ icd
->y_skip_top
+ vblank
- 1) *
516 (qctrl
->maximum
- qctrl
->minimum
)) /
517 1048 + qctrl
->minimum
;
518 mt9m001
->autoexposure
= 1;
520 mt9m001
->autoexposure
= 0;
526 /* Interface active, can use i2c. If it fails, it can indeed mean, that
527 * this wasn't our capture interface, so, we wait for the right one */
528 static int mt9m001_video_probe(struct soc_camera_device
*icd
)
530 struct i2c_client
*client
= to_i2c_client(icd
->control
);
531 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
532 struct soc_camera_link
*icl
= client
->dev
.platform_data
;
537 /* We must have a parent by now. And it cannot be a wrong one.
538 * So this entire test is completely redundant. */
539 if (!icd
->dev
.parent
||
540 to_soc_camera_host(icd
->dev
.parent
)->nr
!= icd
->iface
)
543 /* Enable the chip */
544 data
= reg_write(client
, MT9M001_CHIP_ENABLE
, 1);
545 dev_dbg(&icd
->dev
, "write: %d\n", data
);
547 /* Read out the chip version register */
548 data
= reg_read(client
, MT9M001_CHIP_VERSION
);
550 /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
554 mt9m001
->model
= V4L2_IDENT_MT9M001C12ST
;
555 icd
->formats
= mt9m001_colour_formats
;
558 mt9m001
->model
= V4L2_IDENT_MT9M001C12STM
;
559 icd
->formats
= mt9m001_monochrome_formats
;
564 "No MT9M001 chip detected, register read %x\n", data
);
568 icd
->num_formats
= 0;
571 * This is a 10bit sensor, so by default we only allow 10bit.
572 * The platform may support different bus widths due to
573 * different routing of the data lines.
575 if (icl
->query_bus_param
)
576 flags
= icl
->query_bus_param(icl
);
578 flags
= SOCAM_DATAWIDTH_10
;
580 if (flags
& SOCAM_DATAWIDTH_10
)
585 if (flags
& SOCAM_DATAWIDTH_8
)
588 dev_info(&icd
->dev
, "Detected a MT9M001 chip ID %x (%s)\n", data
,
589 data
== 0x8431 ? "C12STM" : "C12ST");
591 /* Now that we know the model, we can start video */
592 ret
= soc_camera_video_start(icd
);
603 static void mt9m001_video_remove(struct soc_camera_device
*icd
)
605 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
606 struct soc_camera_link
*icl
= mt9m001
->client
->dev
.platform_data
;
608 dev_dbg(&icd
->dev
, "Video %x removed: %p, %p\n", mt9m001
->client
->addr
,
609 icd
->dev
.parent
, icd
->vdev
);
610 soc_camera_video_stop(icd
);
615 static int mt9m001_probe(struct i2c_client
*client
,
616 const struct i2c_device_id
*did
)
618 struct mt9m001
*mt9m001
;
619 struct soc_camera_device
*icd
;
620 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
621 struct soc_camera_link
*icl
= client
->dev
.platform_data
;
625 dev_err(&client
->dev
, "MT9M001 driver needs platform data\n");
629 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
)) {
630 dev_warn(&adapter
->dev
,
631 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
635 mt9m001
= kzalloc(sizeof(struct mt9m001
), GFP_KERNEL
);
639 mt9m001
->client
= client
;
640 i2c_set_clientdata(client
, mt9m001
);
642 /* Second stage probe - when a capture adapter is there */
644 icd
->ops
= &mt9m001_ops
;
645 icd
->control
= &client
->dev
;
651 icd
->width_max
= 1280;
652 icd
->height_min
= 32;
653 icd
->height_max
= 1024;
655 icd
->iface
= icl
->bus_id
;
656 /* Simulated autoexposure. If enabled, we calculate shutter width
657 * ourselves in the driver based on vertical blanking and frame width */
658 mt9m001
->autoexposure
= 1;
660 ret
= soc_camera_device_register(icd
);
671 static int mt9m001_remove(struct i2c_client
*client
)
673 struct mt9m001
*mt9m001
= i2c_get_clientdata(client
);
675 soc_camera_device_unregister(&mt9m001
->icd
);
681 static const struct i2c_device_id mt9m001_id
[] = {
685 MODULE_DEVICE_TABLE(i2c
, mt9m001_id
);
687 static struct i2c_driver mt9m001_i2c_driver
= {
691 .probe
= mt9m001_probe
,
692 .remove
= mt9m001_remove
,
693 .id_table
= mt9m001_id
,
696 static int __init
mt9m001_mod_init(void)
698 return i2c_add_driver(&mt9m001_i2c_driver
);
701 static void __exit
mt9m001_mod_exit(void)
703 i2c_del_driver(&mt9m001_i2c_driver
);
706 module_init(mt9m001_mod_init
);
707 module_exit(mt9m001_mod_exit
);
709 MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
710 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
711 MODULE_LICENSE("GPL");