2 * Driver for MT9P031 CMOS Image Sensor from Aptina
4 * Copyright (C) 2011, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
5 * Copyright (C) 2011, Javier Martin <javier.martin@vista-silicon.com>
6 * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
8 * Based on the MT9V032 driver and Bastian Hecht's code.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/gpio.h>
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/log2.h>
22 #include <linux/slab.h>
23 #include <linux/videodev2.h>
25 #include <media/mt9p031.h>
26 #include <media/v4l2-chip-ident.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-subdev.h>
31 #include "aptina-pll.h"
33 #define MT9P031_PIXEL_ARRAY_WIDTH 2752
34 #define MT9P031_PIXEL_ARRAY_HEIGHT 2004
36 #define MT9P031_CHIP_VERSION 0x00
37 #define MT9P031_CHIP_VERSION_VALUE 0x1801
38 #define MT9P031_ROW_START 0x01
39 #define MT9P031_ROW_START_MIN 0
40 #define MT9P031_ROW_START_MAX 2004
41 #define MT9P031_ROW_START_DEF 54
42 #define MT9P031_COLUMN_START 0x02
43 #define MT9P031_COLUMN_START_MIN 0
44 #define MT9P031_COLUMN_START_MAX 2750
45 #define MT9P031_COLUMN_START_DEF 16
46 #define MT9P031_WINDOW_HEIGHT 0x03
47 #define MT9P031_WINDOW_HEIGHT_MIN 2
48 #define MT9P031_WINDOW_HEIGHT_MAX 2006
49 #define MT9P031_WINDOW_HEIGHT_DEF 1944
50 #define MT9P031_WINDOW_WIDTH 0x04
51 #define MT9P031_WINDOW_WIDTH_MIN 2
52 #define MT9P031_WINDOW_WIDTH_MAX 2752
53 #define MT9P031_WINDOW_WIDTH_DEF 2592
54 #define MT9P031_HORIZONTAL_BLANK 0x05
55 #define MT9P031_HORIZONTAL_BLANK_MIN 0
56 #define MT9P031_HORIZONTAL_BLANK_MAX 4095
57 #define MT9P031_VERTICAL_BLANK 0x06
58 #define MT9P031_VERTICAL_BLANK_MIN 1
59 #define MT9P031_VERTICAL_BLANK_MAX 4096
60 #define MT9P031_VERTICAL_BLANK_DEF 26
61 #define MT9P031_OUTPUT_CONTROL 0x07
62 #define MT9P031_OUTPUT_CONTROL_CEN 2
63 #define MT9P031_OUTPUT_CONTROL_SYN 1
64 #define MT9P031_OUTPUT_CONTROL_DEF 0x1f82
65 #define MT9P031_SHUTTER_WIDTH_UPPER 0x08
66 #define MT9P031_SHUTTER_WIDTH_LOWER 0x09
67 #define MT9P031_SHUTTER_WIDTH_MIN 1
68 #define MT9P031_SHUTTER_WIDTH_MAX 1048575
69 #define MT9P031_SHUTTER_WIDTH_DEF 1943
70 #define MT9P031_PLL_CONTROL 0x10
71 #define MT9P031_PLL_CONTROL_PWROFF 0x0050
72 #define MT9P031_PLL_CONTROL_PWRON 0x0051
73 #define MT9P031_PLL_CONTROL_USEPLL 0x0052
74 #define MT9P031_PLL_CONFIG_1 0x11
75 #define MT9P031_PLL_CONFIG_2 0x12
76 #define MT9P031_PIXEL_CLOCK_CONTROL 0x0a
77 #define MT9P031_FRAME_RESTART 0x0b
78 #define MT9P031_SHUTTER_DELAY 0x0c
79 #define MT9P031_RST 0x0d
80 #define MT9P031_RST_ENABLE 1
81 #define MT9P031_RST_DISABLE 0
82 #define MT9P031_READ_MODE_1 0x1e
83 #define MT9P031_READ_MODE_2 0x20
84 #define MT9P031_READ_MODE_2_ROW_MIR (1 << 15)
85 #define MT9P031_READ_MODE_2_COL_MIR (1 << 14)
86 #define MT9P031_READ_MODE_2_ROW_BLC (1 << 6)
87 #define MT9P031_ROW_ADDRESS_MODE 0x22
88 #define MT9P031_COLUMN_ADDRESS_MODE 0x23
89 #define MT9P031_GLOBAL_GAIN 0x35
90 #define MT9P031_GLOBAL_GAIN_MIN 8
91 #define MT9P031_GLOBAL_GAIN_MAX 1024
92 #define MT9P031_GLOBAL_GAIN_DEF 8
93 #define MT9P031_GLOBAL_GAIN_MULT (1 << 6)
94 #define MT9P031_ROW_BLACK_TARGET 0x49
95 #define MT9P031_ROW_BLACK_DEF_OFFSET 0x4b
96 #define MT9P031_GREEN1_OFFSET 0x60
97 #define MT9P031_GREEN2_OFFSET 0x61
98 #define MT9P031_BLACK_LEVEL_CALIBRATION 0x62
99 #define MT9P031_BLC_MANUAL_BLC (1 << 0)
100 #define MT9P031_RED_OFFSET 0x63
101 #define MT9P031_BLUE_OFFSET 0x64
102 #define MT9P031_TEST_PATTERN 0xa0
103 #define MT9P031_TEST_PATTERN_SHIFT 3
104 #define MT9P031_TEST_PATTERN_ENABLE (1 << 0)
105 #define MT9P031_TEST_PATTERN_DISABLE (0 << 0)
106 #define MT9P031_TEST_PATTERN_GREEN 0xa1
107 #define MT9P031_TEST_PATTERN_RED 0xa2
108 #define MT9P031_TEST_PATTERN_BLUE 0xa3
112 MT9P031_MODEL_MONOCHROME
,
116 struct v4l2_subdev subdev
;
117 struct media_pad pad
;
118 struct v4l2_rect crop
; /* Sensor window */
119 struct v4l2_mbus_framefmt format
;
120 struct mt9p031_platform_data
*pdata
;
121 struct mutex power_lock
; /* lock to protect power_count */
124 enum mt9p031_model model
;
125 struct aptina_pll pll
;
128 struct v4l2_ctrl_handler ctrls
;
129 struct v4l2_ctrl
*blc_auto
;
130 struct v4l2_ctrl
*blc_offset
;
132 /* Registers cache */
137 static struct mt9p031
*to_mt9p031(struct v4l2_subdev
*sd
)
139 return container_of(sd
, struct mt9p031
, subdev
);
142 static int mt9p031_read(struct i2c_client
*client
, u8 reg
)
144 return i2c_smbus_read_word_swapped(client
, reg
);
147 static int mt9p031_write(struct i2c_client
*client
, u8 reg
, u16 data
)
149 return i2c_smbus_write_word_swapped(client
, reg
, data
);
152 static int mt9p031_set_output_control(struct mt9p031
*mt9p031
, u16 clear
,
155 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
156 u16 value
= (mt9p031
->output_control
& ~clear
) | set
;
159 ret
= mt9p031_write(client
, MT9P031_OUTPUT_CONTROL
, value
);
163 mt9p031
->output_control
= value
;
167 static int mt9p031_set_mode2(struct mt9p031
*mt9p031
, u16 clear
, u16 set
)
169 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
170 u16 value
= (mt9p031
->mode2
& ~clear
) | set
;
173 ret
= mt9p031_write(client
, MT9P031_READ_MODE_2
, value
);
177 mt9p031
->mode2
= value
;
181 static int mt9p031_reset(struct mt9p031
*mt9p031
)
183 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
186 /* Disable chip output, synchronous option update */
187 ret
= mt9p031_write(client
, MT9P031_RST
, MT9P031_RST_ENABLE
);
190 ret
= mt9p031_write(client
, MT9P031_RST
, MT9P031_RST_DISABLE
);
194 return mt9p031_set_output_control(mt9p031
, MT9P031_OUTPUT_CONTROL_CEN
,
198 static int mt9p031_pll_setup(struct mt9p031
*mt9p031
)
200 static const struct aptina_pll_limits limits
= {
201 .ext_clock_min
= 6000000,
202 .ext_clock_max
= 27000000,
203 .int_clock_min
= 2000000,
204 .int_clock_max
= 13500000,
205 .out_clock_min
= 180000000,
206 .out_clock_max
= 360000000,
207 .pix_clock_max
= 96000000,
216 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
217 struct mt9p031_platform_data
*pdata
= mt9p031
->pdata
;
219 mt9p031
->pll
.ext_clock
= pdata
->ext_freq
;
220 mt9p031
->pll
.pix_clock
= pdata
->target_freq
;
222 return aptina_pll_calculate(&client
->dev
, &limits
, &mt9p031
->pll
);
225 static int mt9p031_pll_enable(struct mt9p031
*mt9p031
)
227 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
230 ret
= mt9p031_write(client
, MT9P031_PLL_CONTROL
,
231 MT9P031_PLL_CONTROL_PWRON
);
235 ret
= mt9p031_write(client
, MT9P031_PLL_CONFIG_1
,
236 (mt9p031
->pll
.m
<< 8) | (mt9p031
->pll
.n
- 1));
240 ret
= mt9p031_write(client
, MT9P031_PLL_CONFIG_2
, mt9p031
->pll
.p1
- 1);
244 usleep_range(1000, 2000);
245 ret
= mt9p031_write(client
, MT9P031_PLL_CONTROL
,
246 MT9P031_PLL_CONTROL_PWRON
|
247 MT9P031_PLL_CONTROL_USEPLL
);
251 static inline int mt9p031_pll_disable(struct mt9p031
*mt9p031
)
253 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
255 return mt9p031_write(client
, MT9P031_PLL_CONTROL
,
256 MT9P031_PLL_CONTROL_PWROFF
);
259 static int mt9p031_power_on(struct mt9p031
*mt9p031
)
261 /* Ensure RESET_BAR is low */
262 if (mt9p031
->reset
!= -1) {
263 gpio_set_value(mt9p031
->reset
, 0);
264 usleep_range(1000, 2000);
268 if (mt9p031
->pdata
->set_xclk
)
269 mt9p031
->pdata
->set_xclk(&mt9p031
->subdev
,
270 mt9p031
->pdata
->ext_freq
);
272 /* Now RESET_BAR must be high */
273 if (mt9p031
->reset
!= -1) {
274 gpio_set_value(mt9p031
->reset
, 1);
275 usleep_range(1000, 2000);
281 static void mt9p031_power_off(struct mt9p031
*mt9p031
)
283 if (mt9p031
->reset
!= -1) {
284 gpio_set_value(mt9p031
->reset
, 0);
285 usleep_range(1000, 2000);
288 if (mt9p031
->pdata
->set_xclk
)
289 mt9p031
->pdata
->set_xclk(&mt9p031
->subdev
, 0);
292 static int __mt9p031_set_power(struct mt9p031
*mt9p031
, bool on
)
294 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
298 mt9p031_power_off(mt9p031
);
302 ret
= mt9p031_power_on(mt9p031
);
306 ret
= mt9p031_reset(mt9p031
);
308 dev_err(&client
->dev
, "Failed to reset the camera\n");
312 return v4l2_ctrl_handler_setup(&mt9p031
->ctrls
);
315 /* -----------------------------------------------------------------------------
316 * V4L2 subdev video operations
319 static int mt9p031_set_params(struct mt9p031
*mt9p031
)
321 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
322 struct v4l2_mbus_framefmt
*format
= &mt9p031
->format
;
323 const struct v4l2_rect
*crop
= &mt9p031
->crop
;
332 /* Windows position and size.
334 * TODO: Make sure the start coordinates and window size match the
335 * skipping, binning and mirroring (see description of registers 2 and 4
336 * in table 13, and Binning section on page 41).
338 ret
= mt9p031_write(client
, MT9P031_COLUMN_START
, crop
->left
);
341 ret
= mt9p031_write(client
, MT9P031_ROW_START
, crop
->top
);
344 ret
= mt9p031_write(client
, MT9P031_WINDOW_WIDTH
, crop
->width
- 1);
347 ret
= mt9p031_write(client
, MT9P031_WINDOW_HEIGHT
, crop
->height
- 1);
351 /* Row and column binning and skipping. Use the maximum binning value
352 * compatible with the skipping settings.
354 xskip
= DIV_ROUND_CLOSEST(crop
->width
, format
->width
);
355 yskip
= DIV_ROUND_CLOSEST(crop
->height
, format
->height
);
356 xbin
= 1 << (ffs(xskip
) - 1);
357 ybin
= 1 << (ffs(yskip
) - 1);
359 ret
= mt9p031_write(client
, MT9P031_COLUMN_ADDRESS_MODE
,
360 ((xbin
- 1) << 4) | (xskip
- 1));
363 ret
= mt9p031_write(client
, MT9P031_ROW_ADDRESS_MODE
,
364 ((ybin
- 1) << 4) | (yskip
- 1));
368 /* Blanking - use minimum value for horizontal blanking and default
369 * value for vertical blanking.
371 hblank
= 346 * ybin
+ 64 + (80 >> min_t(unsigned int, xbin
, 3));
372 vblank
= MT9P031_VERTICAL_BLANK_DEF
;
374 ret
= mt9p031_write(client
, MT9P031_HORIZONTAL_BLANK
, hblank
- 1);
377 ret
= mt9p031_write(client
, MT9P031_VERTICAL_BLANK
, vblank
- 1);
384 static int mt9p031_s_stream(struct v4l2_subdev
*subdev
, int enable
)
386 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
390 /* Stop sensor readout */
391 ret
= mt9p031_set_output_control(mt9p031
,
392 MT9P031_OUTPUT_CONTROL_CEN
, 0);
396 return mt9p031_pll_disable(mt9p031
);
399 ret
= mt9p031_set_params(mt9p031
);
403 /* Switch to master "normal" mode */
404 ret
= mt9p031_set_output_control(mt9p031
, 0,
405 MT9P031_OUTPUT_CONTROL_CEN
);
409 return mt9p031_pll_enable(mt9p031
);
412 static int mt9p031_enum_mbus_code(struct v4l2_subdev
*subdev
,
413 struct v4l2_subdev_fh
*fh
,
414 struct v4l2_subdev_mbus_code_enum
*code
)
416 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
418 if (code
->pad
|| code
->index
)
421 code
->code
= mt9p031
->format
.code
;
425 static int mt9p031_enum_frame_size(struct v4l2_subdev
*subdev
,
426 struct v4l2_subdev_fh
*fh
,
427 struct v4l2_subdev_frame_size_enum
*fse
)
429 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
431 if (fse
->index
>= 8 || fse
->code
!= mt9p031
->format
.code
)
434 fse
->min_width
= MT9P031_WINDOW_WIDTH_DEF
435 / min_t(unsigned int, 7, fse
->index
+ 1);
436 fse
->max_width
= fse
->min_width
;
437 fse
->min_height
= MT9P031_WINDOW_HEIGHT_DEF
/ (fse
->index
+ 1);
438 fse
->max_height
= fse
->min_height
;
443 static struct v4l2_mbus_framefmt
*
444 __mt9p031_get_pad_format(struct mt9p031
*mt9p031
, struct v4l2_subdev_fh
*fh
,
445 unsigned int pad
, u32 which
)
448 case V4L2_SUBDEV_FORMAT_TRY
:
449 return v4l2_subdev_get_try_format(fh
, pad
);
450 case V4L2_SUBDEV_FORMAT_ACTIVE
:
451 return &mt9p031
->format
;
457 static struct v4l2_rect
*
458 __mt9p031_get_pad_crop(struct mt9p031
*mt9p031
, struct v4l2_subdev_fh
*fh
,
459 unsigned int pad
, u32 which
)
462 case V4L2_SUBDEV_FORMAT_TRY
:
463 return v4l2_subdev_get_try_crop(fh
, pad
);
464 case V4L2_SUBDEV_FORMAT_ACTIVE
:
465 return &mt9p031
->crop
;
471 static int mt9p031_get_format(struct v4l2_subdev
*subdev
,
472 struct v4l2_subdev_fh
*fh
,
473 struct v4l2_subdev_format
*fmt
)
475 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
477 fmt
->format
= *__mt9p031_get_pad_format(mt9p031
, fh
, fmt
->pad
,
482 static int mt9p031_set_format(struct v4l2_subdev
*subdev
,
483 struct v4l2_subdev_fh
*fh
,
484 struct v4l2_subdev_format
*format
)
486 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
487 struct v4l2_mbus_framefmt
*__format
;
488 struct v4l2_rect
*__crop
;
494 __crop
= __mt9p031_get_pad_crop(mt9p031
, fh
, format
->pad
,
497 /* Clamp the width and height to avoid dividing by zero. */
498 width
= clamp_t(unsigned int, ALIGN(format
->format
.width
, 2),
499 max(__crop
->width
/ 7, MT9P031_WINDOW_WIDTH_MIN
),
501 height
= clamp_t(unsigned int, ALIGN(format
->format
.height
, 2),
502 max(__crop
->height
/ 8, MT9P031_WINDOW_HEIGHT_MIN
),
505 hratio
= DIV_ROUND_CLOSEST(__crop
->width
, width
);
506 vratio
= DIV_ROUND_CLOSEST(__crop
->height
, height
);
508 __format
= __mt9p031_get_pad_format(mt9p031
, fh
, format
->pad
,
510 __format
->width
= __crop
->width
/ hratio
;
511 __format
->height
= __crop
->height
/ vratio
;
513 format
->format
= *__format
;
518 static int mt9p031_get_crop(struct v4l2_subdev
*subdev
,
519 struct v4l2_subdev_fh
*fh
,
520 struct v4l2_subdev_crop
*crop
)
522 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
524 crop
->rect
= *__mt9p031_get_pad_crop(mt9p031
, fh
, crop
->pad
,
529 static int mt9p031_set_crop(struct v4l2_subdev
*subdev
,
530 struct v4l2_subdev_fh
*fh
,
531 struct v4l2_subdev_crop
*crop
)
533 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
534 struct v4l2_mbus_framefmt
*__format
;
535 struct v4l2_rect
*__crop
;
536 struct v4l2_rect rect
;
538 /* Clamp the crop rectangle boundaries and align them to a multiple of 2
539 * pixels to ensure a GRBG Bayer pattern.
541 rect
.left
= clamp(ALIGN(crop
->rect
.left
, 2), MT9P031_COLUMN_START_MIN
,
542 MT9P031_COLUMN_START_MAX
);
543 rect
.top
= clamp(ALIGN(crop
->rect
.top
, 2), MT9P031_ROW_START_MIN
,
544 MT9P031_ROW_START_MAX
);
545 rect
.width
= clamp(ALIGN(crop
->rect
.width
, 2),
546 MT9P031_WINDOW_WIDTH_MIN
,
547 MT9P031_WINDOW_WIDTH_MAX
);
548 rect
.height
= clamp(ALIGN(crop
->rect
.height
, 2),
549 MT9P031_WINDOW_HEIGHT_MIN
,
550 MT9P031_WINDOW_HEIGHT_MAX
);
552 rect
.width
= min(rect
.width
, MT9P031_PIXEL_ARRAY_WIDTH
- rect
.left
);
553 rect
.height
= min(rect
.height
, MT9P031_PIXEL_ARRAY_HEIGHT
- rect
.top
);
555 __crop
= __mt9p031_get_pad_crop(mt9p031
, fh
, crop
->pad
, crop
->which
);
557 if (rect
.width
!= __crop
->width
|| rect
.height
!= __crop
->height
) {
558 /* Reset the output image size if the crop rectangle size has
561 __format
= __mt9p031_get_pad_format(mt9p031
, fh
, crop
->pad
,
563 __format
->width
= rect
.width
;
564 __format
->height
= rect
.height
;
573 /* -----------------------------------------------------------------------------
574 * V4L2 subdev control operations
577 #define V4L2_CID_BLC_AUTO (V4L2_CID_USER_BASE | 0x1002)
578 #define V4L2_CID_BLC_TARGET_LEVEL (V4L2_CID_USER_BASE | 0x1003)
579 #define V4L2_CID_BLC_ANALOG_OFFSET (V4L2_CID_USER_BASE | 0x1004)
580 #define V4L2_CID_BLC_DIGITAL_OFFSET (V4L2_CID_USER_BASE | 0x1005)
582 static int mt9p031_s_ctrl(struct v4l2_ctrl
*ctrl
)
584 struct mt9p031
*mt9p031
=
585 container_of(ctrl
->handler
, struct mt9p031
, ctrls
);
586 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
591 case V4L2_CID_EXPOSURE
:
592 ret
= mt9p031_write(client
, MT9P031_SHUTTER_WIDTH_UPPER
,
593 (ctrl
->val
>> 16) & 0xffff);
597 return mt9p031_write(client
, MT9P031_SHUTTER_WIDTH_LOWER
,
601 /* Gain is controlled by 2 analog stages and a digital stage.
602 * Valid values for the 3 stages are
605 * ------------------------------------------
606 * First analog stage x1 x2 1
607 * Second analog stage x1 x4 0.125
608 * Digital stage x1 x16 0.125
610 * To minimize noise, the gain stages should be used in the
611 * second analog stage, first analog stage, digital stage order.
612 * Gain from a previous stage should be pushed to its maximum
613 * value before the next stage is used.
615 if (ctrl
->val
<= 32) {
617 } else if (ctrl
->val
<= 64) {
619 data
= (1 << 6) | (ctrl
->val
>> 1);
622 data
= ((ctrl
->val
- 64) << 5) | (1 << 6) | 32;
625 return mt9p031_write(client
, MT9P031_GLOBAL_GAIN
, data
);
629 return mt9p031_set_mode2(mt9p031
,
630 0, MT9P031_READ_MODE_2_COL_MIR
);
632 return mt9p031_set_mode2(mt9p031
,
633 MT9P031_READ_MODE_2_COL_MIR
, 0);
637 return mt9p031_set_mode2(mt9p031
,
638 0, MT9P031_READ_MODE_2_ROW_MIR
);
640 return mt9p031_set_mode2(mt9p031
,
641 MT9P031_READ_MODE_2_ROW_MIR
, 0);
643 case V4L2_CID_TEST_PATTERN
:
645 /* Restore the black level compensation settings. */
646 if (mt9p031
->blc_auto
->cur
.val
!= 0) {
647 ret
= mt9p031_s_ctrl(mt9p031
->blc_auto
);
651 if (mt9p031
->blc_offset
->cur
.val
!= 0) {
652 ret
= mt9p031_s_ctrl(mt9p031
->blc_offset
);
656 return mt9p031_write(client
, MT9P031_TEST_PATTERN
,
657 MT9P031_TEST_PATTERN_DISABLE
);
660 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_GREEN
, 0x05a0);
663 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_RED
, 0x0a50);
666 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_BLUE
, 0x0aa0);
670 /* Disable digital black level compensation when using a test
673 ret
= mt9p031_set_mode2(mt9p031
, MT9P031_READ_MODE_2_ROW_BLC
,
678 ret
= mt9p031_write(client
, MT9P031_ROW_BLACK_DEF_OFFSET
, 0);
682 return mt9p031_write(client
, MT9P031_TEST_PATTERN
,
683 ((ctrl
->val
- 1) << MT9P031_TEST_PATTERN_SHIFT
)
684 | MT9P031_TEST_PATTERN_ENABLE
);
686 case V4L2_CID_BLC_AUTO
:
687 ret
= mt9p031_set_mode2(mt9p031
,
688 ctrl
->val
? 0 : MT9P031_READ_MODE_2_ROW_BLC
,
689 ctrl
->val
? MT9P031_READ_MODE_2_ROW_BLC
: 0);
693 return mt9p031_write(client
, MT9P031_BLACK_LEVEL_CALIBRATION
,
694 ctrl
->val
? 0 : MT9P031_BLC_MANUAL_BLC
);
696 case V4L2_CID_BLC_TARGET_LEVEL
:
697 return mt9p031_write(client
, MT9P031_ROW_BLACK_TARGET
,
700 case V4L2_CID_BLC_ANALOG_OFFSET
:
701 data
= ctrl
->val
& ((1 << 9) - 1);
703 ret
= mt9p031_write(client
, MT9P031_GREEN1_OFFSET
, data
);
706 ret
= mt9p031_write(client
, MT9P031_GREEN2_OFFSET
, data
);
709 ret
= mt9p031_write(client
, MT9P031_RED_OFFSET
, data
);
712 return mt9p031_write(client
, MT9P031_BLUE_OFFSET
, data
);
714 case V4L2_CID_BLC_DIGITAL_OFFSET
:
715 return mt9p031_write(client
, MT9P031_ROW_BLACK_DEF_OFFSET
,
716 ctrl
->val
& ((1 << 12) - 1));
722 static struct v4l2_ctrl_ops mt9p031_ctrl_ops
= {
723 .s_ctrl
= mt9p031_s_ctrl
,
726 static const char * const mt9p031_test_pattern_menu
[] = {
729 "Horizontal Gradient",
732 "Classic Test Pattern",
734 "Monochrome Horizontal Bars",
735 "Monochrome Vertical Bars",
736 "Vertical Color Bars",
739 static const struct v4l2_ctrl_config mt9p031_ctrls
[] = {
741 .ops
= &mt9p031_ctrl_ops
,
742 .id
= V4L2_CID_BLC_AUTO
,
743 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
751 .ops
= &mt9p031_ctrl_ops
,
752 .id
= V4L2_CID_BLC_TARGET_LEVEL
,
753 .type
= V4L2_CTRL_TYPE_INTEGER
,
754 .name
= "BLC Target Level",
761 .ops
= &mt9p031_ctrl_ops
,
762 .id
= V4L2_CID_BLC_ANALOG_OFFSET
,
763 .type
= V4L2_CTRL_TYPE_INTEGER
,
764 .name
= "BLC Analog Offset",
771 .ops
= &mt9p031_ctrl_ops
,
772 .id
= V4L2_CID_BLC_DIGITAL_OFFSET
,
773 .type
= V4L2_CTRL_TYPE_INTEGER
,
774 .name
= "BLC Digital Offset",
783 /* -----------------------------------------------------------------------------
784 * V4L2 subdev core operations
787 static int mt9p031_set_power(struct v4l2_subdev
*subdev
, int on
)
789 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
792 mutex_lock(&mt9p031
->power_lock
);
794 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
795 * update the power state.
797 if (mt9p031
->power_count
== !on
) {
798 ret
= __mt9p031_set_power(mt9p031
, !!on
);
803 /* Update the power count. */
804 mt9p031
->power_count
+= on
? 1 : -1;
805 WARN_ON(mt9p031
->power_count
< 0);
808 mutex_unlock(&mt9p031
->power_lock
);
812 /* -----------------------------------------------------------------------------
813 * V4L2 subdev internal operations
816 static int mt9p031_registered(struct v4l2_subdev
*subdev
)
818 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
819 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
823 ret
= mt9p031_power_on(mt9p031
);
825 dev_err(&client
->dev
, "MT9P031 power up failed\n");
829 /* Read out the chip version register */
830 data
= mt9p031_read(client
, MT9P031_CHIP_VERSION
);
831 if (data
!= MT9P031_CHIP_VERSION_VALUE
) {
832 dev_err(&client
->dev
, "MT9P031 not detected, wrong version "
837 mt9p031_power_off(mt9p031
);
839 dev_info(&client
->dev
, "MT9P031 detected at address 0x%02x\n",
845 static int mt9p031_open(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
)
847 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
848 struct v4l2_mbus_framefmt
*format
;
849 struct v4l2_rect
*crop
;
851 crop
= v4l2_subdev_get_try_crop(fh
, 0);
852 crop
->left
= MT9P031_COLUMN_START_DEF
;
853 crop
->top
= MT9P031_ROW_START_DEF
;
854 crop
->width
= MT9P031_WINDOW_WIDTH_DEF
;
855 crop
->height
= MT9P031_WINDOW_HEIGHT_DEF
;
857 format
= v4l2_subdev_get_try_format(fh
, 0);
859 if (mt9p031
->model
== MT9P031_MODEL_MONOCHROME
)
860 format
->code
= V4L2_MBUS_FMT_Y12_1X12
;
862 format
->code
= V4L2_MBUS_FMT_SGRBG12_1X12
;
864 format
->width
= MT9P031_WINDOW_WIDTH_DEF
;
865 format
->height
= MT9P031_WINDOW_HEIGHT_DEF
;
866 format
->field
= V4L2_FIELD_NONE
;
867 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
869 return mt9p031_set_power(subdev
, 1);
872 static int mt9p031_close(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
)
874 return mt9p031_set_power(subdev
, 0);
877 static struct v4l2_subdev_core_ops mt9p031_subdev_core_ops
= {
878 .s_power
= mt9p031_set_power
,
881 static struct v4l2_subdev_video_ops mt9p031_subdev_video_ops
= {
882 .s_stream
= mt9p031_s_stream
,
885 static struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops
= {
886 .enum_mbus_code
= mt9p031_enum_mbus_code
,
887 .enum_frame_size
= mt9p031_enum_frame_size
,
888 .get_fmt
= mt9p031_get_format
,
889 .set_fmt
= mt9p031_set_format
,
890 .get_crop
= mt9p031_get_crop
,
891 .set_crop
= mt9p031_set_crop
,
894 static struct v4l2_subdev_ops mt9p031_subdev_ops
= {
895 .core
= &mt9p031_subdev_core_ops
,
896 .video
= &mt9p031_subdev_video_ops
,
897 .pad
= &mt9p031_subdev_pad_ops
,
900 static const struct v4l2_subdev_internal_ops mt9p031_subdev_internal_ops
= {
901 .registered
= mt9p031_registered
,
902 .open
= mt9p031_open
,
903 .close
= mt9p031_close
,
906 /* -----------------------------------------------------------------------------
907 * Driver initialization and probing
910 static int mt9p031_probe(struct i2c_client
*client
,
911 const struct i2c_device_id
*did
)
913 struct mt9p031_platform_data
*pdata
= client
->dev
.platform_data
;
914 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
915 struct mt9p031
*mt9p031
;
920 dev_err(&client
->dev
, "No platform data\n");
924 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
)) {
925 dev_warn(&client
->dev
,
926 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
930 mt9p031
= kzalloc(sizeof(*mt9p031
), GFP_KERNEL
);
934 mt9p031
->pdata
= pdata
;
935 mt9p031
->output_control
= MT9P031_OUTPUT_CONTROL_DEF
;
936 mt9p031
->mode2
= MT9P031_READ_MODE_2_ROW_BLC
;
937 mt9p031
->model
= did
->driver_data
;
940 v4l2_ctrl_handler_init(&mt9p031
->ctrls
, ARRAY_SIZE(mt9p031_ctrls
) + 6);
942 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
943 V4L2_CID_EXPOSURE
, MT9P031_SHUTTER_WIDTH_MIN
,
944 MT9P031_SHUTTER_WIDTH_MAX
, 1,
945 MT9P031_SHUTTER_WIDTH_DEF
);
946 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
947 V4L2_CID_GAIN
, MT9P031_GLOBAL_GAIN_MIN
,
948 MT9P031_GLOBAL_GAIN_MAX
, 1, MT9P031_GLOBAL_GAIN_DEF
);
949 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
950 V4L2_CID_HFLIP
, 0, 1, 1, 0);
951 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
952 V4L2_CID_VFLIP
, 0, 1, 1, 0);
953 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
954 V4L2_CID_PIXEL_RATE
, pdata
->target_freq
,
955 pdata
->target_freq
, 1, pdata
->target_freq
);
956 v4l2_ctrl_new_std_menu_items(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
957 V4L2_CID_TEST_PATTERN
,
958 ARRAY_SIZE(mt9p031_test_pattern_menu
) - 1, 0,
959 0, mt9p031_test_pattern_menu
);
961 for (i
= 0; i
< ARRAY_SIZE(mt9p031_ctrls
); ++i
)
962 v4l2_ctrl_new_custom(&mt9p031
->ctrls
, &mt9p031_ctrls
[i
], NULL
);
964 mt9p031
->subdev
.ctrl_handler
= &mt9p031
->ctrls
;
966 if (mt9p031
->ctrls
.error
) {
967 printk(KERN_INFO
"%s: control initialization error %d\n",
968 __func__
, mt9p031
->ctrls
.error
);
969 ret
= mt9p031
->ctrls
.error
;
973 mt9p031
->blc_auto
= v4l2_ctrl_find(&mt9p031
->ctrls
, V4L2_CID_BLC_AUTO
);
974 mt9p031
->blc_offset
= v4l2_ctrl_find(&mt9p031
->ctrls
,
975 V4L2_CID_BLC_DIGITAL_OFFSET
);
977 mutex_init(&mt9p031
->power_lock
);
978 v4l2_i2c_subdev_init(&mt9p031
->subdev
, client
, &mt9p031_subdev_ops
);
979 mt9p031
->subdev
.internal_ops
= &mt9p031_subdev_internal_ops
;
981 mt9p031
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
982 ret
= media_entity_init(&mt9p031
->subdev
.entity
, 1, &mt9p031
->pad
, 0);
986 mt9p031
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
988 mt9p031
->crop
.width
= MT9P031_WINDOW_WIDTH_DEF
;
989 mt9p031
->crop
.height
= MT9P031_WINDOW_HEIGHT_DEF
;
990 mt9p031
->crop
.left
= MT9P031_COLUMN_START_DEF
;
991 mt9p031
->crop
.top
= MT9P031_ROW_START_DEF
;
993 if (mt9p031
->model
== MT9P031_MODEL_MONOCHROME
)
994 mt9p031
->format
.code
= V4L2_MBUS_FMT_Y12_1X12
;
996 mt9p031
->format
.code
= V4L2_MBUS_FMT_SGRBG12_1X12
;
998 mt9p031
->format
.width
= MT9P031_WINDOW_WIDTH_DEF
;
999 mt9p031
->format
.height
= MT9P031_WINDOW_HEIGHT_DEF
;
1000 mt9p031
->format
.field
= V4L2_FIELD_NONE
;
1001 mt9p031
->format
.colorspace
= V4L2_COLORSPACE_SRGB
;
1003 if (pdata
->reset
!= -1) {
1004 ret
= gpio_request_one(pdata
->reset
, GPIOF_OUT_INIT_LOW
,
1009 mt9p031
->reset
= pdata
->reset
;
1012 ret
= mt9p031_pll_setup(mt9p031
);
1016 if (mt9p031
->reset
!= -1)
1017 gpio_free(mt9p031
->reset
);
1019 v4l2_ctrl_handler_free(&mt9p031
->ctrls
);
1020 media_entity_cleanup(&mt9p031
->subdev
.entity
);
1027 static int mt9p031_remove(struct i2c_client
*client
)
1029 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
1030 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
1032 v4l2_ctrl_handler_free(&mt9p031
->ctrls
);
1033 v4l2_device_unregister_subdev(subdev
);
1034 media_entity_cleanup(&subdev
->entity
);
1035 if (mt9p031
->reset
!= -1)
1036 gpio_free(mt9p031
->reset
);
1042 static const struct i2c_device_id mt9p031_id
[] = {
1043 { "mt9p031", MT9P031_MODEL_COLOR
},
1044 { "mt9p031m", MT9P031_MODEL_MONOCHROME
},
1047 MODULE_DEVICE_TABLE(i2c
, mt9p031_id
);
1049 static struct i2c_driver mt9p031_i2c_driver
= {
1053 .probe
= mt9p031_probe
,
1054 .remove
= mt9p031_remove
,
1055 .id_table
= mt9p031_id
,
1058 module_i2c_driver(mt9p031_i2c_driver
);
1060 MODULE_DESCRIPTION("Aptina MT9P031 Camera driver");
1061 MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
1062 MODULE_LICENSE("GPL v2");