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 if (pix
->height
< 32 + icd
->y_skip_top
)
284 pix
->height
= 32 + icd
->y_skip_top
;
285 if (pix
->height
> 1024 + icd
->y_skip_top
)
286 pix
->height
= 1024 + icd
->y_skip_top
;
289 if (pix
->width
> 1280)
291 pix
->width
&= ~0x01; /* has to be even, unsure why was ~3 */
296 static int mt9m001_get_chip_id(struct soc_camera_device
*icd
,
297 struct v4l2_dbg_chip_ident
*id
)
299 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
301 if (id
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
304 if (id
->match
.addr
!= mt9m001
->client
->addr
)
307 id
->ident
= mt9m001
->model
;
313 #ifdef CONFIG_VIDEO_ADV_DEBUG
314 static int mt9m001_get_register(struct soc_camera_device
*icd
,
315 struct v4l2_dbg_register
*reg
)
317 struct i2c_client
*client
= to_i2c_client(icd
->control
);
319 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0xff)
322 if (reg
->match
.addr
!= client
->addr
)
326 reg
->val
= reg_read(client
, reg
->reg
);
328 if (reg
->val
> 0xffff)
334 static int mt9m001_set_register(struct soc_camera_device
*icd
,
335 struct v4l2_dbg_register
*reg
)
337 struct i2c_client
*client
= to_i2c_client(icd
->control
);
339 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0xff)
342 if (reg
->match
.addr
!= client
->addr
)
345 if (reg_write(client
, reg
->reg
, reg
->val
) < 0)
352 static const struct v4l2_queryctrl mt9m001_controls
[] = {
354 .id
= V4L2_CID_VFLIP
,
355 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
356 .name
= "Flip Vertically",
363 .type
= V4L2_CTRL_TYPE_INTEGER
,
369 .flags
= V4L2_CTRL_FLAG_SLIDER
,
371 .id
= V4L2_CID_EXPOSURE
,
372 .type
= V4L2_CTRL_TYPE_INTEGER
,
377 .default_value
= 255,
378 .flags
= V4L2_CTRL_FLAG_SLIDER
,
380 .id
= V4L2_CID_EXPOSURE_AUTO
,
381 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
382 .name
= "Automatic Exposure",
390 static int mt9m001_video_probe(struct soc_camera_device
*);
391 static void mt9m001_video_remove(struct soc_camera_device
*);
392 static int mt9m001_get_control(struct soc_camera_device
*, struct v4l2_control
*);
393 static int mt9m001_set_control(struct soc_camera_device
*, struct v4l2_control
*);
395 static struct soc_camera_ops mt9m001_ops
= {
396 .owner
= THIS_MODULE
,
397 .probe
= mt9m001_video_probe
,
398 .remove
= mt9m001_video_remove
,
399 .init
= mt9m001_init
,
400 .release
= mt9m001_release
,
401 .start_capture
= mt9m001_start_capture
,
402 .stop_capture
= mt9m001_stop_capture
,
403 .set_crop
= mt9m001_set_crop
,
404 .set_fmt
= mt9m001_set_fmt
,
405 .try_fmt
= mt9m001_try_fmt
,
406 .set_bus_param
= mt9m001_set_bus_param
,
407 .query_bus_param
= mt9m001_query_bus_param
,
408 .controls
= mt9m001_controls
,
409 .num_controls
= ARRAY_SIZE(mt9m001_controls
),
410 .get_control
= mt9m001_get_control
,
411 .set_control
= mt9m001_set_control
,
412 .get_chip_id
= mt9m001_get_chip_id
,
413 #ifdef CONFIG_VIDEO_ADV_DEBUG
414 .get_register
= mt9m001_get_register
,
415 .set_register
= mt9m001_set_register
,
419 static int mt9m001_get_control(struct soc_camera_device
*icd
, struct v4l2_control
*ctrl
)
421 struct i2c_client
*client
= to_i2c_client(icd
->control
);
422 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
427 data
= reg_read(client
, MT9M001_READ_OPTIONS2
);
430 ctrl
->value
= !!(data
& 0x8000);
432 case V4L2_CID_EXPOSURE_AUTO
:
433 ctrl
->value
= mt9m001
->autoexposure
;
439 static int mt9m001_set_control(struct soc_camera_device
*icd
, struct v4l2_control
*ctrl
)
441 struct i2c_client
*client
= to_i2c_client(icd
->control
);
442 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
443 const struct v4l2_queryctrl
*qctrl
;
446 qctrl
= soc_camera_find_qctrl(&mt9m001_ops
, ctrl
->id
);
454 data
= reg_set(client
, MT9M001_READ_OPTIONS2
, 0x8000);
456 data
= reg_clear(client
, MT9M001_READ_OPTIONS2
, 0x8000);
461 if (ctrl
->value
> qctrl
->maximum
|| ctrl
->value
< qctrl
->minimum
)
463 /* See Datasheet Table 7, Gain settings. */
464 if (ctrl
->value
<= qctrl
->default_value
) {
465 /* Pack it into 0..1 step 0.125, register values 0..8 */
466 unsigned long range
= qctrl
->default_value
- qctrl
->minimum
;
467 data
= ((ctrl
->value
- qctrl
->minimum
) * 8 + range
/ 2) / range
;
469 dev_dbg(&icd
->dev
, "Setting gain %d\n", data
);
470 data
= reg_write(client
, MT9M001_GLOBAL_GAIN
, data
);
474 /* Pack it into 1.125..15 variable step, register values 9..67 */
475 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
476 unsigned long range
= qctrl
->maximum
- qctrl
->default_value
- 1;
477 unsigned long gain
= ((ctrl
->value
- qctrl
->default_value
- 1) *
478 111 + range
/ 2) / range
+ 9;
483 data
= ((gain
- 32) * 16 + 16) / 32 + 80;
485 data
= ((gain
- 64) * 7 + 28) / 56 + 96;
487 dev_dbg(&icd
->dev
, "Setting gain from %d to %d\n",
488 reg_read(client
, MT9M001_GLOBAL_GAIN
), data
);
489 data
= reg_write(client
, MT9M001_GLOBAL_GAIN
, data
);
495 icd
->gain
= ctrl
->value
;
497 case V4L2_CID_EXPOSURE
:
498 /* mt9m001 has maximum == default */
499 if (ctrl
->value
> qctrl
->maximum
|| ctrl
->value
< qctrl
->minimum
)
502 unsigned long range
= qctrl
->maximum
- qctrl
->minimum
;
503 unsigned long shutter
= ((ctrl
->value
- qctrl
->minimum
) * 1048 +
504 range
/ 2) / range
+ 1;
506 dev_dbg(&icd
->dev
, "Setting shutter width from %d to %lu\n",
507 reg_read(client
, MT9M001_SHUTTER_WIDTH
), shutter
);
508 if (reg_write(client
, MT9M001_SHUTTER_WIDTH
, shutter
) < 0)
510 icd
->exposure
= ctrl
->value
;
511 mt9m001
->autoexposure
= 0;
514 case V4L2_CID_EXPOSURE_AUTO
:
516 const u16 vblank
= 25;
517 if (reg_write(client
, MT9M001_SHUTTER_WIDTH
, icd
->height
+
518 icd
->y_skip_top
+ vblank
) < 0)
520 qctrl
= soc_camera_find_qctrl(icd
->ops
, V4L2_CID_EXPOSURE
);
521 icd
->exposure
= (524 + (icd
->height
+ icd
->y_skip_top
+ vblank
- 1) *
522 (qctrl
->maximum
- qctrl
->minimum
)) /
523 1048 + qctrl
->minimum
;
524 mt9m001
->autoexposure
= 1;
526 mt9m001
->autoexposure
= 0;
532 /* Interface active, can use i2c. If it fails, it can indeed mean, that
533 * this wasn't our capture interface, so, we wait for the right one */
534 static int mt9m001_video_probe(struct soc_camera_device
*icd
)
536 struct i2c_client
*client
= to_i2c_client(icd
->control
);
537 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
538 struct soc_camera_link
*icl
= client
->dev
.platform_data
;
543 /* We must have a parent by now. And it cannot be a wrong one.
544 * So this entire test is completely redundant. */
545 if (!icd
->dev
.parent
||
546 to_soc_camera_host(icd
->dev
.parent
)->nr
!= icd
->iface
)
549 /* Enable the chip */
550 data
= reg_write(client
, MT9M001_CHIP_ENABLE
, 1);
551 dev_dbg(&icd
->dev
, "write: %d\n", data
);
553 /* Read out the chip version register */
554 data
= reg_read(client
, MT9M001_CHIP_VERSION
);
556 /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
560 mt9m001
->model
= V4L2_IDENT_MT9M001C12ST
;
561 icd
->formats
= mt9m001_colour_formats
;
564 mt9m001
->model
= V4L2_IDENT_MT9M001C12STM
;
565 icd
->formats
= mt9m001_monochrome_formats
;
570 "No MT9M001 chip detected, register read %x\n", data
);
574 icd
->num_formats
= 0;
577 * This is a 10bit sensor, so by default we only allow 10bit.
578 * The platform may support different bus widths due to
579 * different routing of the data lines.
581 if (icl
->query_bus_param
)
582 flags
= icl
->query_bus_param(icl
);
584 flags
= SOCAM_DATAWIDTH_10
;
586 if (flags
& SOCAM_DATAWIDTH_10
)
591 if (flags
& SOCAM_DATAWIDTH_8
)
594 dev_info(&icd
->dev
, "Detected a MT9M001 chip ID %x (%s)\n", data
,
595 data
== 0x8431 ? "C12STM" : "C12ST");
597 /* Now that we know the model, we can start video */
598 ret
= soc_camera_video_start(icd
);
609 static void mt9m001_video_remove(struct soc_camera_device
*icd
)
611 struct mt9m001
*mt9m001
= container_of(icd
, struct mt9m001
, icd
);
612 struct soc_camera_link
*icl
= mt9m001
->client
->dev
.platform_data
;
614 dev_dbg(&icd
->dev
, "Video %x removed: %p, %p\n", mt9m001
->client
->addr
,
615 icd
->dev
.parent
, icd
->vdev
);
616 soc_camera_video_stop(icd
);
621 static int mt9m001_probe(struct i2c_client
*client
,
622 const struct i2c_device_id
*did
)
624 struct mt9m001
*mt9m001
;
625 struct soc_camera_device
*icd
;
626 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
627 struct soc_camera_link
*icl
= client
->dev
.platform_data
;
631 dev_err(&client
->dev
, "MT9M001 driver needs platform data\n");
635 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
)) {
636 dev_warn(&adapter
->dev
,
637 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
641 mt9m001
= kzalloc(sizeof(struct mt9m001
), GFP_KERNEL
);
645 mt9m001
->client
= client
;
646 i2c_set_clientdata(client
, mt9m001
);
648 /* Second stage probe - when a capture adapter is there */
650 icd
->ops
= &mt9m001_ops
;
651 icd
->control
= &client
->dev
;
657 icd
->width_max
= 1280;
658 icd
->height_min
= 32;
659 icd
->height_max
= 1024;
661 icd
->iface
= icl
->bus_id
;
662 /* Simulated autoexposure. If enabled, we calculate shutter width
663 * ourselves in the driver based on vertical blanking and frame width */
664 mt9m001
->autoexposure
= 1;
666 ret
= soc_camera_device_register(icd
);
677 static int mt9m001_remove(struct i2c_client
*client
)
679 struct mt9m001
*mt9m001
= i2c_get_clientdata(client
);
681 soc_camera_device_unregister(&mt9m001
->icd
);
687 static const struct i2c_device_id mt9m001_id
[] = {
691 MODULE_DEVICE_TABLE(i2c
, mt9m001_id
);
693 static struct i2c_driver mt9m001_i2c_driver
= {
697 .probe
= mt9m001_probe
,
698 .remove
= mt9m001_remove
,
699 .id_table
= mt9m001_id
,
702 static int __init
mt9m001_mod_init(void)
704 return i2c_add_driver(&mt9m001_i2c_driver
);
707 static void __exit
mt9m001_mod_exit(void)
709 i2c_del_driver(&mt9m001_i2c_driver
);
712 module_init(mt9m001_mod_init
);
713 module_exit(mt9m001_mod_exit
);
715 MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
716 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
717 MODULE_LICENSE("GPL");