2 * Driver for MT9V022 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/delay.h>
15 #include <linux/log2.h>
16 #include <linux/module.h>
18 #include <media/v4l2-subdev.h>
19 #include <media/v4l2-chip-ident.h>
20 #include <media/soc_camera.h>
23 * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
24 * The platform has to define ctruct i2c_board_info objects and link to them
25 * from struct soc_camera_link
28 static char *sensor_type
;
29 module_param(sensor_type
, charp
, S_IRUGO
);
30 MODULE_PARM_DESC(sensor_type
, "Sensor type: \"colour\" or \"monochrome\"");
32 /* mt9v022 selected register addresses */
33 #define MT9V022_CHIP_VERSION 0x00
34 #define MT9V022_COLUMN_START 0x01
35 #define MT9V022_ROW_START 0x02
36 #define MT9V022_WINDOW_HEIGHT 0x03
37 #define MT9V022_WINDOW_WIDTH 0x04
38 #define MT9V022_HORIZONTAL_BLANKING 0x05
39 #define MT9V022_VERTICAL_BLANKING 0x06
40 #define MT9V022_CHIP_CONTROL 0x07
41 #define MT9V022_SHUTTER_WIDTH1 0x08
42 #define MT9V022_SHUTTER_WIDTH2 0x09
43 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
44 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
45 #define MT9V022_RESET 0x0c
46 #define MT9V022_READ_MODE 0x0d
47 #define MT9V022_MONITOR_MODE 0x0e
48 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
49 #define MT9V022_LED_OUT_CONTROL 0x1b
50 #define MT9V022_ADC_MODE_CONTROL 0x1c
51 #define MT9V022_ANALOG_GAIN 0x35
52 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
53 #define MT9V022_PIXCLK_FV_LV 0x74
54 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
55 #define MT9V022_AEC_AGC_ENABLE 0xAF
56 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
58 /* Progressive scan, master, defaults */
59 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
61 #define MT9V022_MAX_WIDTH 752
62 #define MT9V022_MAX_HEIGHT 480
63 #define MT9V022_MIN_WIDTH 48
64 #define MT9V022_MIN_HEIGHT 32
65 #define MT9V022_COLUMN_SKIP 1
66 #define MT9V022_ROW_SKIP 4
68 /* MT9V022 has only one fixed colorspace per pixelcode */
69 struct mt9v022_datafmt
{
70 enum v4l2_mbus_pixelcode code
;
71 enum v4l2_colorspace colorspace
;
74 /* Find a data format by a pixel code in an array */
75 static const struct mt9v022_datafmt
*mt9v022_find_datafmt(
76 enum v4l2_mbus_pixelcode code
, const struct mt9v022_datafmt
*fmt
,
80 for (i
= 0; i
< n
; i
++)
81 if (fmt
[i
].code
== code
)
87 static const struct mt9v022_datafmt mt9v022_colour_fmts
[] = {
89 * Order important: first natively supported,
90 * second supported with a GPIO extender
92 {V4L2_MBUS_FMT_SBGGR10_1X10
, V4L2_COLORSPACE_SRGB
},
93 {V4L2_MBUS_FMT_SBGGR8_1X8
, V4L2_COLORSPACE_SRGB
},
96 static const struct mt9v022_datafmt mt9v022_monochrome_fmts
[] = {
97 /* Order important - see above */
98 {V4L2_MBUS_FMT_Y10_1X10
, V4L2_COLORSPACE_JPEG
},
99 {V4L2_MBUS_FMT_Y8_1X8
, V4L2_COLORSPACE_JPEG
},
103 struct v4l2_subdev subdev
;
104 struct v4l2_rect rect
; /* Sensor window */
105 const struct mt9v022_datafmt
*fmt
;
106 const struct mt9v022_datafmt
*fmts
;
108 int model
; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
110 unsigned short y_skip_top
; /* Lines to skip at the top */
113 static struct mt9v022
*to_mt9v022(const struct i2c_client
*client
)
115 return container_of(i2c_get_clientdata(client
), struct mt9v022
, subdev
);
118 static int reg_read(struct i2c_client
*client
, const u8 reg
)
120 s32 data
= i2c_smbus_read_word_data(client
, reg
);
121 return data
< 0 ? data
: swab16(data
);
124 static int reg_write(struct i2c_client
*client
, const u8 reg
,
127 return i2c_smbus_write_word_data(client
, reg
, swab16(data
));
130 static int reg_set(struct i2c_client
*client
, const u8 reg
,
135 ret
= reg_read(client
, reg
);
138 return reg_write(client
, reg
, ret
| data
);
141 static int reg_clear(struct i2c_client
*client
, const u8 reg
,
146 ret
= reg_read(client
, reg
);
149 return reg_write(client
, reg
, ret
& ~data
);
152 static int mt9v022_init(struct i2c_client
*client
)
154 struct mt9v022
*mt9v022
= to_mt9v022(client
);
158 * Almost the default mode: master, parallel, simultaneous, and an
159 * undocumented bit 0x200, which is present in table 7, but not in 8,
160 * plus snapshot mode to disable scan for now
162 mt9v022
->chip_control
|= 0x10;
163 ret
= reg_write(client
, MT9V022_CHIP_CONTROL
, mt9v022
->chip_control
);
165 ret
= reg_write(client
, MT9V022_READ_MODE
, 0x300);
170 ret
= reg_set(client
, MT9V022_AEC_AGC_ENABLE
, 0x3);
172 ret
= reg_write(client
, MT9V022_ANALOG_GAIN
, 16);
174 ret
= reg_write(client
, MT9V022_TOTAL_SHUTTER_WIDTH
, 480);
176 ret
= reg_write(client
, MT9V022_MAX_TOTAL_SHUTTER_WIDTH
, 480);
179 ret
= reg_clear(client
, MT9V022_BLACK_LEVEL_CALIB_CTRL
, 1);
181 ret
= reg_write(client
, MT9V022_DIGITAL_TEST_PATTERN
, 0);
186 static int mt9v022_s_stream(struct v4l2_subdev
*sd
, int enable
)
188 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
189 struct mt9v022
*mt9v022
= to_mt9v022(client
);
192 /* Switch to master "normal" mode */
193 mt9v022
->chip_control
&= ~0x10;
195 /* Switch to snapshot mode */
196 mt9v022
->chip_control
|= 0x10;
198 if (reg_write(client
, MT9V022_CHIP_CONTROL
, mt9v022
->chip_control
) < 0)
203 static int mt9v022_set_bus_param(struct soc_camera_device
*icd
,
206 struct i2c_client
*client
= to_i2c_client(to_soc_camera_control(icd
));
207 struct mt9v022
*mt9v022
= to_mt9v022(client
);
208 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
209 unsigned int width_flag
= flags
& SOCAM_DATAWIDTH_MASK
;
213 /* Only one width bit may be set */
214 if (!is_power_of_2(width_flag
))
217 if (icl
->set_bus_param
) {
218 ret
= icl
->set_bus_param(icl
, width_flag
);
223 * Without board specific bus width settings we only support the
224 * sensors native bus width
226 if (width_flag
!= SOCAM_DATAWIDTH_10
)
230 flags
= soc_camera_apply_sensor_flags(icl
, flags
);
232 if (flags
& SOCAM_PCLK_SAMPLE_FALLING
)
235 if (!(flags
& SOCAM_HSYNC_ACTIVE_HIGH
))
238 if (!(flags
& SOCAM_VSYNC_ACTIVE_HIGH
))
241 ret
= reg_write(client
, MT9V022_PIXCLK_FV_LV
, pixclk
);
245 if (!(flags
& SOCAM_MASTER
))
246 mt9v022
->chip_control
&= ~0x8;
248 ret
= reg_write(client
, MT9V022_CHIP_CONTROL
, mt9v022
->chip_control
);
252 dev_dbg(&client
->dev
, "Calculated pixclk 0x%x, chip control 0x%x\n",
253 pixclk
, mt9v022
->chip_control
);
258 static unsigned long mt9v022_query_bus_param(struct soc_camera_device
*icd
)
260 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
261 unsigned int flags
= SOCAM_MASTER
| SOCAM_SLAVE
|
262 SOCAM_PCLK_SAMPLE_RISING
| SOCAM_PCLK_SAMPLE_FALLING
|
263 SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_HSYNC_ACTIVE_LOW
|
264 SOCAM_VSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_LOW
|
265 SOCAM_DATA_ACTIVE_HIGH
;
267 if (icl
->query_bus_param
)
268 flags
|= icl
->query_bus_param(icl
) & SOCAM_DATAWIDTH_MASK
;
270 flags
|= SOCAM_DATAWIDTH_10
;
272 return soc_camera_apply_sensor_flags(icl
, flags
);
275 static int mt9v022_s_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
277 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
278 struct mt9v022
*mt9v022
= to_mt9v022(client
);
279 struct v4l2_rect rect
= a
->c
;
282 /* Bayer format - even size lengths */
283 if (mt9v022
->fmts
== mt9v022_colour_fmts
) {
284 rect
.width
= ALIGN(rect
.width
, 2);
285 rect
.height
= ALIGN(rect
.height
, 2);
286 /* Let the user play with the starting pixel */
289 soc_camera_limit_side(&rect
.left
, &rect
.width
,
290 MT9V022_COLUMN_SKIP
, MT9V022_MIN_WIDTH
, MT9V022_MAX_WIDTH
);
292 soc_camera_limit_side(&rect
.top
, &rect
.height
,
293 MT9V022_ROW_SKIP
, MT9V022_MIN_HEIGHT
, MT9V022_MAX_HEIGHT
);
295 /* Like in example app. Contradicts the datasheet though */
296 ret
= reg_read(client
, MT9V022_AEC_AGC_ENABLE
);
298 if (ret
& 1) /* Autoexposure */
299 ret
= reg_write(client
, MT9V022_MAX_TOTAL_SHUTTER_WIDTH
,
300 rect
.height
+ mt9v022
->y_skip_top
+ 43);
302 ret
= reg_write(client
, MT9V022_TOTAL_SHUTTER_WIDTH
,
303 rect
.height
+ mt9v022
->y_skip_top
+ 43);
305 /* Setup frame format: defaults apart from width and height */
307 ret
= reg_write(client
, MT9V022_COLUMN_START
, rect
.left
);
309 ret
= reg_write(client
, MT9V022_ROW_START
, rect
.top
);
312 * Default 94, Phytec driver says:
313 * "width + horizontal blank >= 660"
315 ret
= reg_write(client
, MT9V022_HORIZONTAL_BLANKING
,
316 rect
.width
> 660 - 43 ? 43 :
319 ret
= reg_write(client
, MT9V022_VERTICAL_BLANKING
, 45);
321 ret
= reg_write(client
, MT9V022_WINDOW_WIDTH
, rect
.width
);
323 ret
= reg_write(client
, MT9V022_WINDOW_HEIGHT
,
324 rect
.height
+ mt9v022
->y_skip_top
);
329 dev_dbg(&client
->dev
, "Frame %dx%d pixel\n", rect
.width
, rect
.height
);
331 mt9v022
->rect
= rect
;
336 static int mt9v022_g_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
338 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
339 struct mt9v022
*mt9v022
= to_mt9v022(client
);
341 a
->c
= mt9v022
->rect
;
342 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
347 static int mt9v022_cropcap(struct v4l2_subdev
*sd
, struct v4l2_cropcap
*a
)
349 a
->bounds
.left
= MT9V022_COLUMN_SKIP
;
350 a
->bounds
.top
= MT9V022_ROW_SKIP
;
351 a
->bounds
.width
= MT9V022_MAX_WIDTH
;
352 a
->bounds
.height
= MT9V022_MAX_HEIGHT
;
353 a
->defrect
= a
->bounds
;
354 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
355 a
->pixelaspect
.numerator
= 1;
356 a
->pixelaspect
.denominator
= 1;
361 static int mt9v022_g_fmt(struct v4l2_subdev
*sd
,
362 struct v4l2_mbus_framefmt
*mf
)
364 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
365 struct mt9v022
*mt9v022
= to_mt9v022(client
);
367 mf
->width
= mt9v022
->rect
.width
;
368 mf
->height
= mt9v022
->rect
.height
;
369 mf
->code
= mt9v022
->fmt
->code
;
370 mf
->colorspace
= mt9v022
->fmt
->colorspace
;
371 mf
->field
= V4L2_FIELD_NONE
;
376 static int mt9v022_s_fmt(struct v4l2_subdev
*sd
,
377 struct v4l2_mbus_framefmt
*mf
)
379 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
380 struct mt9v022
*mt9v022
= to_mt9v022(client
);
381 struct v4l2_crop a
= {
383 .left
= mt9v022
->rect
.left
,
384 .top
= mt9v022
->rect
.top
,
386 .height
= mf
->height
,
392 * The caller provides a supported format, as verified per call to
393 * icd->try_fmt(), datawidth is from our supported format list
396 case V4L2_MBUS_FMT_Y8_1X8
:
397 case V4L2_MBUS_FMT_Y10_1X10
:
398 if (mt9v022
->model
!= V4L2_IDENT_MT9V022IX7ATM
)
401 case V4L2_MBUS_FMT_SBGGR8_1X8
:
402 case V4L2_MBUS_FMT_SBGGR10_1X10
:
403 if (mt9v022
->model
!= V4L2_IDENT_MT9V022IX7ATC
)
410 /* No support for scaling on this camera, just crop. */
411 ret
= mt9v022_s_crop(sd
, &a
);
413 mf
->width
= mt9v022
->rect
.width
;
414 mf
->height
= mt9v022
->rect
.height
;
415 mt9v022
->fmt
= mt9v022_find_datafmt(mf
->code
,
416 mt9v022
->fmts
, mt9v022
->num_fmts
);
417 mf
->colorspace
= mt9v022
->fmt
->colorspace
;
423 static int mt9v022_try_fmt(struct v4l2_subdev
*sd
,
424 struct v4l2_mbus_framefmt
*mf
)
426 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
427 struct mt9v022
*mt9v022
= to_mt9v022(client
);
428 const struct mt9v022_datafmt
*fmt
;
429 int align
= mf
->code
== V4L2_MBUS_FMT_SBGGR8_1X8
||
430 mf
->code
== V4L2_MBUS_FMT_SBGGR10_1X10
;
432 v4l_bound_align_image(&mf
->width
, MT9V022_MIN_WIDTH
,
433 MT9V022_MAX_WIDTH
, align
,
434 &mf
->height
, MT9V022_MIN_HEIGHT
+ mt9v022
->y_skip_top
,
435 MT9V022_MAX_HEIGHT
+ mt9v022
->y_skip_top
, align
, 0);
437 fmt
= mt9v022_find_datafmt(mf
->code
, mt9v022
->fmts
,
441 mf
->code
= fmt
->code
;
444 mf
->colorspace
= fmt
->colorspace
;
449 static int mt9v022_g_chip_ident(struct v4l2_subdev
*sd
,
450 struct v4l2_dbg_chip_ident
*id
)
452 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
453 struct mt9v022
*mt9v022
= to_mt9v022(client
);
455 if (id
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
458 if (id
->match
.addr
!= client
->addr
)
461 id
->ident
= mt9v022
->model
;
467 #ifdef CONFIG_VIDEO_ADV_DEBUG
468 static int mt9v022_g_register(struct v4l2_subdev
*sd
,
469 struct v4l2_dbg_register
*reg
)
471 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
473 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0xff)
476 if (reg
->match
.addr
!= client
->addr
)
480 reg
->val
= reg_read(client
, reg
->reg
);
482 if (reg
->val
> 0xffff)
488 static int mt9v022_s_register(struct v4l2_subdev
*sd
,
489 struct v4l2_dbg_register
*reg
)
491 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
493 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0xff)
496 if (reg
->match
.addr
!= client
->addr
)
499 if (reg_write(client
, reg
->reg
, reg
->val
) < 0)
506 static const struct v4l2_queryctrl mt9v022_controls
[] = {
508 .id
= V4L2_CID_VFLIP
,
509 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
510 .name
= "Flip Vertically",
516 .id
= V4L2_CID_HFLIP
,
517 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
518 .name
= "Flip Horizontally",
525 .type
= V4L2_CTRL_TYPE_INTEGER
,
526 .name
= "Analog Gain",
531 .flags
= V4L2_CTRL_FLAG_SLIDER
,
533 .id
= V4L2_CID_EXPOSURE
,
534 .type
= V4L2_CTRL_TYPE_INTEGER
,
539 .default_value
= 255,
540 .flags
= V4L2_CTRL_FLAG_SLIDER
,
542 .id
= V4L2_CID_AUTOGAIN
,
543 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
544 .name
= "Automatic Gain",
550 .id
= V4L2_CID_EXPOSURE_AUTO
,
551 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
552 .name
= "Automatic Exposure",
560 static struct soc_camera_ops mt9v022_ops
= {
561 .set_bus_param
= mt9v022_set_bus_param
,
562 .query_bus_param
= mt9v022_query_bus_param
,
563 .controls
= mt9v022_controls
,
564 .num_controls
= ARRAY_SIZE(mt9v022_controls
),
567 static int mt9v022_g_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
569 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
570 const struct v4l2_queryctrl
*qctrl
;
574 qctrl
= soc_camera_find_qctrl(&mt9v022_ops
, ctrl
->id
);
578 data
= reg_read(client
, MT9V022_READ_MODE
);
581 ctrl
->value
= !!(data
& 0x10);
584 data
= reg_read(client
, MT9V022_READ_MODE
);
587 ctrl
->value
= !!(data
& 0x20);
589 case V4L2_CID_EXPOSURE_AUTO
:
590 data
= reg_read(client
, MT9V022_AEC_AGC_ENABLE
);
593 ctrl
->value
= !!(data
& 0x1);
595 case V4L2_CID_AUTOGAIN
:
596 data
= reg_read(client
, MT9V022_AEC_AGC_ENABLE
);
599 ctrl
->value
= !!(data
& 0x2);
602 data
= reg_read(client
, MT9V022_ANALOG_GAIN
);
606 range
= qctrl
->maximum
- qctrl
->minimum
;
607 ctrl
->value
= ((data
- 16) * range
+ 24) / 48 + qctrl
->minimum
;
610 case V4L2_CID_EXPOSURE
:
611 data
= reg_read(client
, MT9V022_TOTAL_SHUTTER_WIDTH
);
615 range
= qctrl
->maximum
- qctrl
->minimum
;
616 ctrl
->value
= ((data
- 1) * range
+ 239) / 479 + qctrl
->minimum
;
623 static int mt9v022_s_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
626 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
627 const struct v4l2_queryctrl
*qctrl
;
629 qctrl
= soc_camera_find_qctrl(&mt9v022_ops
, ctrl
->id
);
636 data
= reg_set(client
, MT9V022_READ_MODE
, 0x10);
638 data
= reg_clear(client
, MT9V022_READ_MODE
, 0x10);
644 data
= reg_set(client
, MT9V022_READ_MODE
, 0x20);
646 data
= reg_clear(client
, MT9V022_READ_MODE
, 0x20);
651 /* mt9v022 has minimum == default */
652 if (ctrl
->value
> qctrl
->maximum
|| ctrl
->value
< qctrl
->minimum
)
655 unsigned long range
= qctrl
->maximum
- qctrl
->minimum
;
656 /* Valid values 16 to 64, 32 to 64 must be even. */
657 unsigned long gain
= ((ctrl
->value
- qctrl
->minimum
) *
658 48 + range
/ 2) / range
+ 16;
662 * The user wants to set gain manually, hope, she
663 * knows, what she's doing... Switch AGC off.
666 if (reg_clear(client
, MT9V022_AEC_AGC_ENABLE
, 0x2) < 0)
669 dev_dbg(&client
->dev
, "Setting gain from %d to %lu\n",
670 reg_read(client
, MT9V022_ANALOG_GAIN
), gain
);
671 if (reg_write(client
, MT9V022_ANALOG_GAIN
, gain
) < 0)
675 case V4L2_CID_EXPOSURE
:
676 /* mt9v022 has maximum == default */
677 if (ctrl
->value
> qctrl
->maximum
|| ctrl
->value
< qctrl
->minimum
)
680 unsigned long range
= qctrl
->maximum
- qctrl
->minimum
;
681 unsigned long shutter
= ((ctrl
->value
- qctrl
->minimum
) *
682 479 + range
/ 2) / range
+ 1;
684 * The user wants to set shutter width manually, hope,
685 * she knows, what she's doing... Switch AEC off.
688 if (reg_clear(client
, MT9V022_AEC_AGC_ENABLE
, 0x1) < 0)
691 dev_dbg(&client
->dev
, "Shutter width from %d to %lu\n",
692 reg_read(client
, MT9V022_TOTAL_SHUTTER_WIDTH
),
694 if (reg_write(client
, MT9V022_TOTAL_SHUTTER_WIDTH
,
699 case V4L2_CID_AUTOGAIN
:
701 data
= reg_set(client
, MT9V022_AEC_AGC_ENABLE
, 0x2);
703 data
= reg_clear(client
, MT9V022_AEC_AGC_ENABLE
, 0x2);
707 case V4L2_CID_EXPOSURE_AUTO
:
709 data
= reg_set(client
, MT9V022_AEC_AGC_ENABLE
, 0x1);
711 data
= reg_clear(client
, MT9V022_AEC_AGC_ENABLE
, 0x1);
720 * Interface active, can use i2c. If it fails, it can indeed mean, that
721 * this wasn't our capture interface, so, we wait for the right one
723 static int mt9v022_video_probe(struct soc_camera_device
*icd
,
724 struct i2c_client
*client
)
726 struct mt9v022
*mt9v022
= to_mt9v022(client
);
727 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
732 /* We must have a parent by now. And it cannot be a wrong one. */
733 BUG_ON(!icd
->parent
||
734 to_soc_camera_host(icd
->parent
)->nr
!= icd
->iface
);
736 /* Read out the chip version register */
737 data
= reg_read(client
, MT9V022_CHIP_VERSION
);
739 /* must be 0x1311 or 0x1313 */
740 if (data
!= 0x1311 && data
!= 0x1313) {
742 dev_info(&client
->dev
, "No MT9V022 found, ID register 0x%x\n",
748 ret
= reg_write(client
, MT9V022_RESET
, 1);
751 /* 15 clock cycles */
753 if (reg_read(client
, MT9V022_RESET
)) {
754 dev_err(&client
->dev
, "Resetting MT9V022 failed!\n");
760 /* Set monochrome or colour sensor type */
761 if (sensor_type
&& (!strcmp("colour", sensor_type
) ||
762 !strcmp("color", sensor_type
))) {
763 ret
= reg_write(client
, MT9V022_PIXEL_OPERATION_MODE
, 4 | 0x11);
764 mt9v022
->model
= V4L2_IDENT_MT9V022IX7ATC
;
765 mt9v022
->fmts
= mt9v022_colour_fmts
;
767 ret
= reg_write(client
, MT9V022_PIXEL_OPERATION_MODE
, 0x11);
768 mt9v022
->model
= V4L2_IDENT_MT9V022IX7ATM
;
769 mt9v022
->fmts
= mt9v022_monochrome_fmts
;
775 mt9v022
->num_fmts
= 0;
778 * This is a 10bit sensor, so by default we only allow 10bit.
779 * The platform may support different bus widths due to
780 * different routing of the data lines.
782 if (icl
->query_bus_param
)
783 flags
= icl
->query_bus_param(icl
);
785 flags
= SOCAM_DATAWIDTH_10
;
787 if (flags
& SOCAM_DATAWIDTH_10
)
792 if (flags
& SOCAM_DATAWIDTH_8
)
795 mt9v022
->fmt
= &mt9v022
->fmts
[0];
797 dev_info(&client
->dev
, "Detected a MT9V022 chip ID %x, %s sensor\n",
798 data
, mt9v022
->model
== V4L2_IDENT_MT9V022IX7ATM
?
799 "monochrome" : "colour");
801 ret
= mt9v022_init(client
);
803 dev_err(&client
->dev
, "Failed to initialise the camera\n");
809 static void mt9v022_video_remove(struct soc_camera_device
*icd
)
811 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
813 dev_dbg(icd
->pdev
, "Video removed: %p, %p\n",
814 icd
->parent
, icd
->vdev
);
819 static int mt9v022_g_skip_top_lines(struct v4l2_subdev
*sd
, u32
*lines
)
821 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
822 struct mt9v022
*mt9v022
= to_mt9v022(client
);
824 *lines
= mt9v022
->y_skip_top
;
829 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops
= {
830 .g_ctrl
= mt9v022_g_ctrl
,
831 .s_ctrl
= mt9v022_s_ctrl
,
832 .g_chip_ident
= mt9v022_g_chip_ident
,
833 #ifdef CONFIG_VIDEO_ADV_DEBUG
834 .g_register
= mt9v022_g_register
,
835 .s_register
= mt9v022_s_register
,
839 static int mt9v022_enum_fmt(struct v4l2_subdev
*sd
, unsigned int index
,
840 enum v4l2_mbus_pixelcode
*code
)
842 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
843 struct mt9v022
*mt9v022
= to_mt9v022(client
);
845 if (index
>= mt9v022
->num_fmts
)
848 *code
= mt9v022
->fmts
[index
].code
;
852 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops
= {
853 .s_stream
= mt9v022_s_stream
,
854 .s_mbus_fmt
= mt9v022_s_fmt
,
855 .g_mbus_fmt
= mt9v022_g_fmt
,
856 .try_mbus_fmt
= mt9v022_try_fmt
,
857 .s_crop
= mt9v022_s_crop
,
858 .g_crop
= mt9v022_g_crop
,
859 .cropcap
= mt9v022_cropcap
,
860 .enum_mbus_fmt
= mt9v022_enum_fmt
,
863 static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops
= {
864 .g_skip_top_lines
= mt9v022_g_skip_top_lines
,
867 static struct v4l2_subdev_ops mt9v022_subdev_ops
= {
868 .core
= &mt9v022_subdev_core_ops
,
869 .video
= &mt9v022_subdev_video_ops
,
870 .sensor
= &mt9v022_subdev_sensor_ops
,
873 static int mt9v022_probe(struct i2c_client
*client
,
874 const struct i2c_device_id
*did
)
876 struct mt9v022
*mt9v022
;
877 struct soc_camera_device
*icd
= client
->dev
.platform_data
;
878 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
879 struct soc_camera_link
*icl
;
883 dev_err(&client
->dev
, "MT9V022: missing soc-camera data!\n");
887 icl
= to_soc_camera_link(icd
);
889 dev_err(&client
->dev
, "MT9V022 driver needs platform data\n");
893 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
)) {
894 dev_warn(&adapter
->dev
,
895 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
899 mt9v022
= kzalloc(sizeof(struct mt9v022
), GFP_KERNEL
);
903 v4l2_i2c_subdev_init(&mt9v022
->subdev
, client
, &mt9v022_subdev_ops
);
905 mt9v022
->chip_control
= MT9V022_CHIP_CONTROL_DEFAULT
;
907 icd
->ops
= &mt9v022_ops
;
909 * MT9V022 _really_ corrupts the first read out line.
910 * TODO: verify on i.MX31
912 mt9v022
->y_skip_top
= 1;
913 mt9v022
->rect
.left
= MT9V022_COLUMN_SKIP
;
914 mt9v022
->rect
.top
= MT9V022_ROW_SKIP
;
915 mt9v022
->rect
.width
= MT9V022_MAX_WIDTH
;
916 mt9v022
->rect
.height
= MT9V022_MAX_HEIGHT
;
918 ret
= mt9v022_video_probe(icd
, client
);
927 static int mt9v022_remove(struct i2c_client
*client
)
929 struct mt9v022
*mt9v022
= to_mt9v022(client
);
930 struct soc_camera_device
*icd
= client
->dev
.platform_data
;
933 mt9v022_video_remove(icd
);
938 static const struct i2c_device_id mt9v022_id
[] = {
942 MODULE_DEVICE_TABLE(i2c
, mt9v022_id
);
944 static struct i2c_driver mt9v022_i2c_driver
= {
948 .probe
= mt9v022_probe
,
949 .remove
= mt9v022_remove
,
950 .id_table
= mt9v022_id
,
953 static int __init
mt9v022_mod_init(void)
955 return i2c_add_driver(&mt9v022_i2c_driver
);
958 static void __exit
mt9v022_mod_exit(void)
960 i2c_del_driver(&mt9v022_i2c_driver
);
963 module_init(mt9v022_mod_init
);
964 module_exit(mt9v022_mod_exit
);
966 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
967 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
968 MODULE_LICENSE("GPL");