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/soc_camera.h>
19 #include <media/soc_mediabus.h>
20 #include <media/v4l2-subdev.h>
21 #include <media/v4l2-chip-ident.h>
22 #include <media/v4l2-ctrls.h>
25 * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
26 * The platform has to define ctruct i2c_board_info objects and link to them
27 * from struct soc_camera_link
30 static char *sensor_type
;
31 module_param(sensor_type
, charp
, S_IRUGO
);
32 MODULE_PARM_DESC(sensor_type
, "Sensor type: \"colour\" or \"monochrome\"");
34 /* mt9v022 selected register addresses */
35 #define MT9V022_CHIP_VERSION 0x00
36 #define MT9V022_COLUMN_START 0x01
37 #define MT9V022_ROW_START 0x02
38 #define MT9V022_WINDOW_HEIGHT 0x03
39 #define MT9V022_WINDOW_WIDTH 0x04
40 #define MT9V022_HORIZONTAL_BLANKING 0x05
41 #define MT9V022_VERTICAL_BLANKING 0x06
42 #define MT9V022_CHIP_CONTROL 0x07
43 #define MT9V022_SHUTTER_WIDTH1 0x08
44 #define MT9V022_SHUTTER_WIDTH2 0x09
45 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
46 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
47 #define MT9V022_RESET 0x0c
48 #define MT9V022_READ_MODE 0x0d
49 #define MT9V022_MONITOR_MODE 0x0e
50 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
51 #define MT9V022_LED_OUT_CONTROL 0x1b
52 #define MT9V022_ADC_MODE_CONTROL 0x1c
53 #define MT9V022_ANALOG_GAIN 0x35
54 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
55 #define MT9V022_PIXCLK_FV_LV 0x74
56 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
57 #define MT9V022_AEC_AGC_ENABLE 0xAF
58 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
60 /* Progressive scan, master, defaults */
61 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
63 #define MT9V022_MAX_WIDTH 752
64 #define MT9V022_MAX_HEIGHT 480
65 #define MT9V022_MIN_WIDTH 48
66 #define MT9V022_MIN_HEIGHT 32
67 #define MT9V022_COLUMN_SKIP 1
68 #define MT9V022_ROW_SKIP 4
70 /* MT9V022 has only one fixed colorspace per pixelcode */
71 struct mt9v022_datafmt
{
72 enum v4l2_mbus_pixelcode code
;
73 enum v4l2_colorspace colorspace
;
76 /* Find a data format by a pixel code in an array */
77 static const struct mt9v022_datafmt
*mt9v022_find_datafmt(
78 enum v4l2_mbus_pixelcode code
, const struct mt9v022_datafmt
*fmt
,
82 for (i
= 0; i
< n
; i
++)
83 if (fmt
[i
].code
== code
)
89 static const struct mt9v022_datafmt mt9v022_colour_fmts
[] = {
91 * Order important: first natively supported,
92 * second supported with a GPIO extender
94 {V4L2_MBUS_FMT_SBGGR10_1X10
, V4L2_COLORSPACE_SRGB
},
95 {V4L2_MBUS_FMT_SBGGR8_1X8
, V4L2_COLORSPACE_SRGB
},
98 static const struct mt9v022_datafmt mt9v022_monochrome_fmts
[] = {
99 /* Order important - see above */
100 {V4L2_MBUS_FMT_Y10_1X10
, V4L2_COLORSPACE_JPEG
},
101 {V4L2_MBUS_FMT_Y8_1X8
, V4L2_COLORSPACE_JPEG
},
105 struct v4l2_subdev subdev
;
106 struct v4l2_ctrl_handler hdl
;
108 /* exposure/auto-exposure cluster */
109 struct v4l2_ctrl
*autoexposure
;
110 struct v4l2_ctrl
*exposure
;
113 /* gain/auto-gain cluster */
114 struct v4l2_ctrl
*autogain
;
115 struct v4l2_ctrl
*gain
;
117 struct v4l2_rect rect
; /* Sensor window */
118 const struct mt9v022_datafmt
*fmt
;
119 const struct mt9v022_datafmt
*fmts
;
121 int model
; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
123 unsigned short y_skip_top
; /* Lines to skip at the top */
126 static struct mt9v022
*to_mt9v022(const struct i2c_client
*client
)
128 return container_of(i2c_get_clientdata(client
), struct mt9v022
, subdev
);
131 static int reg_read(struct i2c_client
*client
, const u8 reg
)
133 return i2c_smbus_read_word_swapped(client
, reg
);
136 static int reg_write(struct i2c_client
*client
, const u8 reg
,
139 return i2c_smbus_write_word_swapped(client
, reg
, data
);
142 static int reg_set(struct i2c_client
*client
, const u8 reg
,
147 ret
= reg_read(client
, reg
);
150 return reg_write(client
, reg
, ret
| data
);
153 static int reg_clear(struct i2c_client
*client
, const u8 reg
,
158 ret
= reg_read(client
, reg
);
161 return reg_write(client
, reg
, ret
& ~data
);
164 static int mt9v022_init(struct i2c_client
*client
)
166 struct mt9v022
*mt9v022
= to_mt9v022(client
);
170 * Almost the default mode: master, parallel, simultaneous, and an
171 * undocumented bit 0x200, which is present in table 7, but not in 8,
172 * plus snapshot mode to disable scan for now
174 mt9v022
->chip_control
|= 0x10;
175 ret
= reg_write(client
, MT9V022_CHIP_CONTROL
, mt9v022
->chip_control
);
177 ret
= reg_write(client
, MT9V022_READ_MODE
, 0x300);
182 ret
= reg_set(client
, MT9V022_AEC_AGC_ENABLE
, 0x3);
184 ret
= reg_write(client
, MT9V022_ANALOG_GAIN
, 16);
186 ret
= reg_write(client
, MT9V022_TOTAL_SHUTTER_WIDTH
, 480);
188 ret
= reg_write(client
, MT9V022_MAX_TOTAL_SHUTTER_WIDTH
, 480);
191 ret
= reg_clear(client
, MT9V022_BLACK_LEVEL_CALIB_CTRL
, 1);
193 ret
= reg_write(client
, MT9V022_DIGITAL_TEST_PATTERN
, 0);
195 return v4l2_ctrl_handler_setup(&mt9v022
->hdl
);
200 static int mt9v022_s_stream(struct v4l2_subdev
*sd
, int enable
)
202 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
203 struct mt9v022
*mt9v022
= to_mt9v022(client
);
206 /* Switch to master "normal" mode */
207 mt9v022
->chip_control
&= ~0x10;
209 /* Switch to snapshot mode */
210 mt9v022
->chip_control
|= 0x10;
212 if (reg_write(client
, MT9V022_CHIP_CONTROL
, mt9v022
->chip_control
) < 0)
217 static int mt9v022_s_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
219 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
220 struct mt9v022
*mt9v022
= to_mt9v022(client
);
221 struct v4l2_rect rect
= a
->c
;
224 /* Bayer format - even size lengths */
225 if (mt9v022
->fmts
== mt9v022_colour_fmts
) {
226 rect
.width
= ALIGN(rect
.width
, 2);
227 rect
.height
= ALIGN(rect
.height
, 2);
228 /* Let the user play with the starting pixel */
231 soc_camera_limit_side(&rect
.left
, &rect
.width
,
232 MT9V022_COLUMN_SKIP
, MT9V022_MIN_WIDTH
, MT9V022_MAX_WIDTH
);
234 soc_camera_limit_side(&rect
.top
, &rect
.height
,
235 MT9V022_ROW_SKIP
, MT9V022_MIN_HEIGHT
, MT9V022_MAX_HEIGHT
);
237 /* Like in example app. Contradicts the datasheet though */
238 ret
= reg_read(client
, MT9V022_AEC_AGC_ENABLE
);
240 if (ret
& 1) /* Autoexposure */
241 ret
= reg_write(client
, MT9V022_MAX_TOTAL_SHUTTER_WIDTH
,
242 rect
.height
+ mt9v022
->y_skip_top
+ 43);
244 ret
= reg_write(client
, MT9V022_TOTAL_SHUTTER_WIDTH
,
245 rect
.height
+ mt9v022
->y_skip_top
+ 43);
247 /* Setup frame format: defaults apart from width and height */
249 ret
= reg_write(client
, MT9V022_COLUMN_START
, rect
.left
);
251 ret
= reg_write(client
, MT9V022_ROW_START
, rect
.top
);
254 * Default 94, Phytec driver says:
255 * "width + horizontal blank >= 660"
257 ret
= reg_write(client
, MT9V022_HORIZONTAL_BLANKING
,
258 rect
.width
> 660 - 43 ? 43 :
261 ret
= reg_write(client
, MT9V022_VERTICAL_BLANKING
, 45);
263 ret
= reg_write(client
, MT9V022_WINDOW_WIDTH
, rect
.width
);
265 ret
= reg_write(client
, MT9V022_WINDOW_HEIGHT
,
266 rect
.height
+ mt9v022
->y_skip_top
);
271 dev_dbg(&client
->dev
, "Frame %dx%d pixel\n", rect
.width
, rect
.height
);
273 mt9v022
->rect
= rect
;
278 static int mt9v022_g_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
280 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
281 struct mt9v022
*mt9v022
= to_mt9v022(client
);
283 a
->c
= mt9v022
->rect
;
284 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
289 static int mt9v022_cropcap(struct v4l2_subdev
*sd
, struct v4l2_cropcap
*a
)
291 a
->bounds
.left
= MT9V022_COLUMN_SKIP
;
292 a
->bounds
.top
= MT9V022_ROW_SKIP
;
293 a
->bounds
.width
= MT9V022_MAX_WIDTH
;
294 a
->bounds
.height
= MT9V022_MAX_HEIGHT
;
295 a
->defrect
= a
->bounds
;
296 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
297 a
->pixelaspect
.numerator
= 1;
298 a
->pixelaspect
.denominator
= 1;
303 static int mt9v022_g_fmt(struct v4l2_subdev
*sd
,
304 struct v4l2_mbus_framefmt
*mf
)
306 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
307 struct mt9v022
*mt9v022
= to_mt9v022(client
);
309 mf
->width
= mt9v022
->rect
.width
;
310 mf
->height
= mt9v022
->rect
.height
;
311 mf
->code
= mt9v022
->fmt
->code
;
312 mf
->colorspace
= mt9v022
->fmt
->colorspace
;
313 mf
->field
= V4L2_FIELD_NONE
;
318 static int mt9v022_s_fmt(struct v4l2_subdev
*sd
,
319 struct v4l2_mbus_framefmt
*mf
)
321 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
322 struct mt9v022
*mt9v022
= to_mt9v022(client
);
323 struct v4l2_crop a
= {
325 .left
= mt9v022
->rect
.left
,
326 .top
= mt9v022
->rect
.top
,
328 .height
= mf
->height
,
334 * The caller provides a supported format, as verified per call to
335 * .try_mbus_fmt(), datawidth is from our supported format list
338 case V4L2_MBUS_FMT_Y8_1X8
:
339 case V4L2_MBUS_FMT_Y10_1X10
:
340 if (mt9v022
->model
!= V4L2_IDENT_MT9V022IX7ATM
)
343 case V4L2_MBUS_FMT_SBGGR8_1X8
:
344 case V4L2_MBUS_FMT_SBGGR10_1X10
:
345 if (mt9v022
->model
!= V4L2_IDENT_MT9V022IX7ATC
)
352 /* No support for scaling on this camera, just crop. */
353 ret
= mt9v022_s_crop(sd
, &a
);
355 mf
->width
= mt9v022
->rect
.width
;
356 mf
->height
= mt9v022
->rect
.height
;
357 mt9v022
->fmt
= mt9v022_find_datafmt(mf
->code
,
358 mt9v022
->fmts
, mt9v022
->num_fmts
);
359 mf
->colorspace
= mt9v022
->fmt
->colorspace
;
365 static int mt9v022_try_fmt(struct v4l2_subdev
*sd
,
366 struct v4l2_mbus_framefmt
*mf
)
368 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
369 struct mt9v022
*mt9v022
= to_mt9v022(client
);
370 const struct mt9v022_datafmt
*fmt
;
371 int align
= mf
->code
== V4L2_MBUS_FMT_SBGGR8_1X8
||
372 mf
->code
== V4L2_MBUS_FMT_SBGGR10_1X10
;
374 v4l_bound_align_image(&mf
->width
, MT9V022_MIN_WIDTH
,
375 MT9V022_MAX_WIDTH
, align
,
376 &mf
->height
, MT9V022_MIN_HEIGHT
+ mt9v022
->y_skip_top
,
377 MT9V022_MAX_HEIGHT
+ mt9v022
->y_skip_top
, align
, 0);
379 fmt
= mt9v022_find_datafmt(mf
->code
, mt9v022
->fmts
,
383 mf
->code
= fmt
->code
;
386 mf
->colorspace
= fmt
->colorspace
;
391 static int mt9v022_g_chip_ident(struct v4l2_subdev
*sd
,
392 struct v4l2_dbg_chip_ident
*id
)
394 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
395 struct mt9v022
*mt9v022
= to_mt9v022(client
);
397 if (id
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
400 if (id
->match
.addr
!= client
->addr
)
403 id
->ident
= mt9v022
->model
;
409 #ifdef CONFIG_VIDEO_ADV_DEBUG
410 static int mt9v022_g_register(struct v4l2_subdev
*sd
,
411 struct v4l2_dbg_register
*reg
)
413 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
415 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0xff)
418 if (reg
->match
.addr
!= client
->addr
)
422 reg
->val
= reg_read(client
, reg
->reg
);
424 if (reg
->val
> 0xffff)
430 static int mt9v022_s_register(struct v4l2_subdev
*sd
,
431 struct v4l2_dbg_register
*reg
)
433 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
435 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0xff)
438 if (reg
->match
.addr
!= client
->addr
)
441 if (reg_write(client
, reg
->reg
, reg
->val
) < 0)
448 static int mt9v022_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
450 struct mt9v022
*mt9v022
= container_of(ctrl
->handler
,
451 struct mt9v022
, hdl
);
452 struct v4l2_subdev
*sd
= &mt9v022
->subdev
;
453 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
454 struct v4l2_ctrl
*gain
= mt9v022
->gain
;
455 struct v4l2_ctrl
*exp
= mt9v022
->exposure
;
460 case V4L2_CID_AUTOGAIN
:
461 data
= reg_read(client
, MT9V022_ANALOG_GAIN
);
465 range
= gain
->maximum
- gain
->minimum
;
466 gain
->val
= ((data
- 16) * range
+ 24) / 48 + gain
->minimum
;
468 case V4L2_CID_EXPOSURE_AUTO
:
469 data
= reg_read(client
, MT9V022_TOTAL_SHUTTER_WIDTH
);
473 range
= exp
->maximum
- exp
->minimum
;
474 exp
->val
= ((data
- 1) * range
+ 239) / 479 + exp
->minimum
;
480 static int mt9v022_s_ctrl(struct v4l2_ctrl
*ctrl
)
482 struct mt9v022
*mt9v022
= container_of(ctrl
->handler
,
483 struct mt9v022
, hdl
);
484 struct v4l2_subdev
*sd
= &mt9v022
->subdev
;
485 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
491 data
= reg_set(client
, MT9V022_READ_MODE
, 0x10);
493 data
= reg_clear(client
, MT9V022_READ_MODE
, 0x10);
499 data
= reg_set(client
, MT9V022_READ_MODE
, 0x20);
501 data
= reg_clear(client
, MT9V022_READ_MODE
, 0x20);
505 case V4L2_CID_AUTOGAIN
:
507 if (reg_set(client
, MT9V022_AEC_AGC_ENABLE
, 0x2) < 0)
510 struct v4l2_ctrl
*gain
= mt9v022
->gain
;
511 /* mt9v022 has minimum == default */
512 unsigned long range
= gain
->maximum
- gain
->minimum
;
513 /* Valid values 16 to 64, 32 to 64 must be even. */
514 unsigned long gain_val
= ((gain
->val
- gain
->minimum
) *
515 48 + range
/ 2) / range
+ 16;
521 * The user wants to set gain manually, hope, she
522 * knows, what she's doing... Switch AGC off.
524 if (reg_clear(client
, MT9V022_AEC_AGC_ENABLE
, 0x2) < 0)
527 dev_dbg(&client
->dev
, "Setting gain from %d to %lu\n",
528 reg_read(client
, MT9V022_ANALOG_GAIN
), gain_val
);
529 if (reg_write(client
, MT9V022_ANALOG_GAIN
, gain_val
) < 0)
533 case V4L2_CID_EXPOSURE_AUTO
:
534 if (ctrl
->val
== V4L2_EXPOSURE_AUTO
) {
535 data
= reg_set(client
, MT9V022_AEC_AGC_ENABLE
, 0x1);
537 struct v4l2_ctrl
*exp
= mt9v022
->exposure
;
538 unsigned long range
= exp
->maximum
- exp
->minimum
;
539 unsigned long shutter
= ((exp
->val
- exp
->minimum
) *
540 479 + range
/ 2) / range
+ 1;
543 * The user wants to set shutter width manually, hope,
544 * she knows, what she's doing... Switch AEC off.
546 data
= reg_clear(client
, MT9V022_AEC_AGC_ENABLE
, 0x1);
549 dev_dbg(&client
->dev
, "Shutter width from %d to %lu\n",
550 reg_read(client
, MT9V022_TOTAL_SHUTTER_WIDTH
),
552 if (reg_write(client
, MT9V022_TOTAL_SHUTTER_WIDTH
,
562 * Interface active, can use i2c. If it fails, it can indeed mean, that
563 * this wasn't our capture interface, so, we wait for the right one
565 static int mt9v022_video_probe(struct i2c_client
*client
)
567 struct mt9v022
*mt9v022
= to_mt9v022(client
);
568 struct soc_camera_link
*icl
= soc_camera_i2c_to_link(client
);
573 /* Read out the chip version register */
574 data
= reg_read(client
, MT9V022_CHIP_VERSION
);
576 /* must be 0x1311 or 0x1313 */
577 if (data
!= 0x1311 && data
!= 0x1313) {
579 dev_info(&client
->dev
, "No MT9V022 found, ID register 0x%x\n",
585 ret
= reg_write(client
, MT9V022_RESET
, 1);
588 /* 15 clock cycles */
590 if (reg_read(client
, MT9V022_RESET
)) {
591 dev_err(&client
->dev
, "Resetting MT9V022 failed!\n");
597 /* Set monochrome or colour sensor type */
598 if (sensor_type
&& (!strcmp("colour", sensor_type
) ||
599 !strcmp("color", sensor_type
))) {
600 ret
= reg_write(client
, MT9V022_PIXEL_OPERATION_MODE
, 4 | 0x11);
601 mt9v022
->model
= V4L2_IDENT_MT9V022IX7ATC
;
602 mt9v022
->fmts
= mt9v022_colour_fmts
;
604 ret
= reg_write(client
, MT9V022_PIXEL_OPERATION_MODE
, 0x11);
605 mt9v022
->model
= V4L2_IDENT_MT9V022IX7ATM
;
606 mt9v022
->fmts
= mt9v022_monochrome_fmts
;
612 mt9v022
->num_fmts
= 0;
615 * This is a 10bit sensor, so by default we only allow 10bit.
616 * The platform may support different bus widths due to
617 * different routing of the data lines.
619 if (icl
->query_bus_param
)
620 flags
= icl
->query_bus_param(icl
);
622 flags
= SOCAM_DATAWIDTH_10
;
624 if (flags
& SOCAM_DATAWIDTH_10
)
629 if (flags
& SOCAM_DATAWIDTH_8
)
632 mt9v022
->fmt
= &mt9v022
->fmts
[0];
634 dev_info(&client
->dev
, "Detected a MT9V022 chip ID %x, %s sensor\n",
635 data
, mt9v022
->model
== V4L2_IDENT_MT9V022IX7ATM
?
636 "monochrome" : "colour");
638 ret
= mt9v022_init(client
);
640 dev_err(&client
->dev
, "Failed to initialise the camera\n");
646 static int mt9v022_g_skip_top_lines(struct v4l2_subdev
*sd
, u32
*lines
)
648 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
649 struct mt9v022
*mt9v022
= to_mt9v022(client
);
651 *lines
= mt9v022
->y_skip_top
;
656 static const struct v4l2_ctrl_ops mt9v022_ctrl_ops
= {
657 .g_volatile_ctrl
= mt9v022_g_volatile_ctrl
,
658 .s_ctrl
= mt9v022_s_ctrl
,
661 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops
= {
662 .g_chip_ident
= mt9v022_g_chip_ident
,
663 #ifdef CONFIG_VIDEO_ADV_DEBUG
664 .g_register
= mt9v022_g_register
,
665 .s_register
= mt9v022_s_register
,
669 static int mt9v022_enum_fmt(struct v4l2_subdev
*sd
, unsigned int index
,
670 enum v4l2_mbus_pixelcode
*code
)
672 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
673 struct mt9v022
*mt9v022
= to_mt9v022(client
);
675 if (index
>= mt9v022
->num_fmts
)
678 *code
= mt9v022
->fmts
[index
].code
;
682 static int mt9v022_g_mbus_config(struct v4l2_subdev
*sd
,
683 struct v4l2_mbus_config
*cfg
)
685 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
686 struct soc_camera_link
*icl
= soc_camera_i2c_to_link(client
);
688 cfg
->flags
= V4L2_MBUS_MASTER
| V4L2_MBUS_SLAVE
|
689 V4L2_MBUS_PCLK_SAMPLE_RISING
| V4L2_MBUS_PCLK_SAMPLE_FALLING
|
690 V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_HSYNC_ACTIVE_LOW
|
691 V4L2_MBUS_VSYNC_ACTIVE_HIGH
| V4L2_MBUS_VSYNC_ACTIVE_LOW
|
692 V4L2_MBUS_DATA_ACTIVE_HIGH
;
693 cfg
->type
= V4L2_MBUS_PARALLEL
;
694 cfg
->flags
= soc_camera_apply_board_flags(icl
, cfg
);
699 static int mt9v022_s_mbus_config(struct v4l2_subdev
*sd
,
700 const struct v4l2_mbus_config
*cfg
)
702 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
703 struct soc_camera_link
*icl
= soc_camera_i2c_to_link(client
);
704 struct mt9v022
*mt9v022
= to_mt9v022(client
);
705 unsigned long flags
= soc_camera_apply_board_flags(icl
, cfg
);
706 unsigned int bps
= soc_mbus_get_fmtdesc(mt9v022
->fmt
->code
)->bits_per_sample
;
710 if (icl
->set_bus_param
) {
711 ret
= icl
->set_bus_param(icl
, 1 << (bps
- 1));
714 } else if (bps
!= 10) {
716 * Without board specific bus width settings we only support the
717 * sensors native bus width
722 if (flags
& V4L2_MBUS_PCLK_SAMPLE_FALLING
)
725 if (!(flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
))
728 if (!(flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
))
731 ret
= reg_write(client
, MT9V022_PIXCLK_FV_LV
, pixclk
);
735 if (!(flags
& V4L2_MBUS_MASTER
))
736 mt9v022
->chip_control
&= ~0x8;
738 ret
= reg_write(client
, MT9V022_CHIP_CONTROL
, mt9v022
->chip_control
);
742 dev_dbg(&client
->dev
, "Calculated pixclk 0x%x, chip control 0x%x\n",
743 pixclk
, mt9v022
->chip_control
);
748 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops
= {
749 .s_stream
= mt9v022_s_stream
,
750 .s_mbus_fmt
= mt9v022_s_fmt
,
751 .g_mbus_fmt
= mt9v022_g_fmt
,
752 .try_mbus_fmt
= mt9v022_try_fmt
,
753 .s_crop
= mt9v022_s_crop
,
754 .g_crop
= mt9v022_g_crop
,
755 .cropcap
= mt9v022_cropcap
,
756 .enum_mbus_fmt
= mt9v022_enum_fmt
,
757 .g_mbus_config
= mt9v022_g_mbus_config
,
758 .s_mbus_config
= mt9v022_s_mbus_config
,
761 static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops
= {
762 .g_skip_top_lines
= mt9v022_g_skip_top_lines
,
765 static struct v4l2_subdev_ops mt9v022_subdev_ops
= {
766 .core
= &mt9v022_subdev_core_ops
,
767 .video
= &mt9v022_subdev_video_ops
,
768 .sensor
= &mt9v022_subdev_sensor_ops
,
771 static int mt9v022_probe(struct i2c_client
*client
,
772 const struct i2c_device_id
*did
)
774 struct mt9v022
*mt9v022
;
775 struct soc_camera_link
*icl
= soc_camera_i2c_to_link(client
);
776 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
780 dev_err(&client
->dev
, "MT9V022 driver needs platform data\n");
784 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
)) {
785 dev_warn(&adapter
->dev
,
786 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
790 mt9v022
= kzalloc(sizeof(struct mt9v022
), GFP_KERNEL
);
794 v4l2_i2c_subdev_init(&mt9v022
->subdev
, client
, &mt9v022_subdev_ops
);
795 v4l2_ctrl_handler_init(&mt9v022
->hdl
, 6);
796 v4l2_ctrl_new_std(&mt9v022
->hdl
, &mt9v022_ctrl_ops
,
797 V4L2_CID_VFLIP
, 0, 1, 1, 0);
798 v4l2_ctrl_new_std(&mt9v022
->hdl
, &mt9v022_ctrl_ops
,
799 V4L2_CID_HFLIP
, 0, 1, 1, 0);
800 mt9v022
->autogain
= v4l2_ctrl_new_std(&mt9v022
->hdl
, &mt9v022_ctrl_ops
,
801 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
802 mt9v022
->gain
= v4l2_ctrl_new_std(&mt9v022
->hdl
, &mt9v022_ctrl_ops
,
803 V4L2_CID_GAIN
, 0, 127, 1, 64);
806 * Simulated autoexposure. If enabled, we calculate shutter width
807 * ourselves in the driver based on vertical blanking and frame width
809 mt9v022
->autoexposure
= v4l2_ctrl_new_std_menu(&mt9v022
->hdl
,
810 &mt9v022_ctrl_ops
, V4L2_CID_EXPOSURE_AUTO
, 1, 0,
812 mt9v022
->exposure
= v4l2_ctrl_new_std(&mt9v022
->hdl
, &mt9v022_ctrl_ops
,
813 V4L2_CID_EXPOSURE
, 1, 255, 1, 255);
815 mt9v022
->subdev
.ctrl_handler
= &mt9v022
->hdl
;
816 if (mt9v022
->hdl
.error
) {
817 int err
= mt9v022
->hdl
.error
;
822 v4l2_ctrl_auto_cluster(2, &mt9v022
->autoexposure
,
823 V4L2_EXPOSURE_MANUAL
, true);
824 v4l2_ctrl_auto_cluster(2, &mt9v022
->autogain
, 0, true);
826 mt9v022
->chip_control
= MT9V022_CHIP_CONTROL_DEFAULT
;
829 * MT9V022 _really_ corrupts the first read out line.
830 * TODO: verify on i.MX31
832 mt9v022
->y_skip_top
= 1;
833 mt9v022
->rect
.left
= MT9V022_COLUMN_SKIP
;
834 mt9v022
->rect
.top
= MT9V022_ROW_SKIP
;
835 mt9v022
->rect
.width
= MT9V022_MAX_WIDTH
;
836 mt9v022
->rect
.height
= MT9V022_MAX_HEIGHT
;
838 ret
= mt9v022_video_probe(client
);
840 v4l2_ctrl_handler_free(&mt9v022
->hdl
);
847 static int mt9v022_remove(struct i2c_client
*client
)
849 struct mt9v022
*mt9v022
= to_mt9v022(client
);
850 struct soc_camera_link
*icl
= soc_camera_i2c_to_link(client
);
852 v4l2_device_unregister_subdev(&mt9v022
->subdev
);
855 v4l2_ctrl_handler_free(&mt9v022
->hdl
);
860 static const struct i2c_device_id mt9v022_id
[] = {
864 MODULE_DEVICE_TABLE(i2c
, mt9v022_id
);
866 static struct i2c_driver mt9v022_i2c_driver
= {
870 .probe
= mt9v022_probe
,
871 .remove
= mt9v022_remove
,
872 .id_table
= mt9v022_id
,
875 static int __init
mt9v022_mod_init(void)
877 return i2c_add_driver(&mt9v022_i2c_driver
);
880 static void __exit
mt9v022_mod_exit(void)
882 i2c_del_driver(&mt9v022_i2c_driver
);
885 module_init(mt9v022_mod_init
);
886 module_exit(mt9v022_mod_exit
);
888 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
889 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
890 MODULE_LICENSE("GPL");