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/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <linux/videodev2.h>
26 #include <media/mt9p031.h>
27 #include <media/v4l2-chip-ident.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-subdev.h>
32 #include "aptina-pll.h"
34 #define MT9P031_PIXEL_ARRAY_WIDTH 2752
35 #define MT9P031_PIXEL_ARRAY_HEIGHT 2004
37 #define MT9P031_CHIP_VERSION 0x00
38 #define MT9P031_CHIP_VERSION_VALUE 0x1801
39 #define MT9P031_ROW_START 0x01
40 #define MT9P031_ROW_START_MIN 0
41 #define MT9P031_ROW_START_MAX 2004
42 #define MT9P031_ROW_START_DEF 54
43 #define MT9P031_COLUMN_START 0x02
44 #define MT9P031_COLUMN_START_MIN 0
45 #define MT9P031_COLUMN_START_MAX 2750
46 #define MT9P031_COLUMN_START_DEF 16
47 #define MT9P031_WINDOW_HEIGHT 0x03
48 #define MT9P031_WINDOW_HEIGHT_MIN 2
49 #define MT9P031_WINDOW_HEIGHT_MAX 2006
50 #define MT9P031_WINDOW_HEIGHT_DEF 1944
51 #define MT9P031_WINDOW_WIDTH 0x04
52 #define MT9P031_WINDOW_WIDTH_MIN 2
53 #define MT9P031_WINDOW_WIDTH_MAX 2752
54 #define MT9P031_WINDOW_WIDTH_DEF 2592
55 #define MT9P031_HORIZONTAL_BLANK 0x05
56 #define MT9P031_HORIZONTAL_BLANK_MIN 0
57 #define MT9P031_HORIZONTAL_BLANK_MAX 4095
58 #define MT9P031_VERTICAL_BLANK 0x06
59 #define MT9P031_VERTICAL_BLANK_MIN 1
60 #define MT9P031_VERTICAL_BLANK_MAX 4096
61 #define MT9P031_VERTICAL_BLANK_DEF 26
62 #define MT9P031_OUTPUT_CONTROL 0x07
63 #define MT9P031_OUTPUT_CONTROL_CEN 2
64 #define MT9P031_OUTPUT_CONTROL_SYN 1
65 #define MT9P031_OUTPUT_CONTROL_DEF 0x1f82
66 #define MT9P031_SHUTTER_WIDTH_UPPER 0x08
67 #define MT9P031_SHUTTER_WIDTH_LOWER 0x09
68 #define MT9P031_SHUTTER_WIDTH_MIN 1
69 #define MT9P031_SHUTTER_WIDTH_MAX 1048575
70 #define MT9P031_SHUTTER_WIDTH_DEF 1943
71 #define MT9P031_PLL_CONTROL 0x10
72 #define MT9P031_PLL_CONTROL_PWROFF 0x0050
73 #define MT9P031_PLL_CONTROL_PWRON 0x0051
74 #define MT9P031_PLL_CONTROL_USEPLL 0x0052
75 #define MT9P031_PLL_CONFIG_1 0x11
76 #define MT9P031_PLL_CONFIG_2 0x12
77 #define MT9P031_PIXEL_CLOCK_CONTROL 0x0a
78 #define MT9P031_FRAME_RESTART 0x0b
79 #define MT9P031_SHUTTER_DELAY 0x0c
80 #define MT9P031_RST 0x0d
81 #define MT9P031_RST_ENABLE 1
82 #define MT9P031_RST_DISABLE 0
83 #define MT9P031_READ_MODE_1 0x1e
84 #define MT9P031_READ_MODE_2 0x20
85 #define MT9P031_READ_MODE_2_ROW_MIR (1 << 15)
86 #define MT9P031_READ_MODE_2_COL_MIR (1 << 14)
87 #define MT9P031_READ_MODE_2_ROW_BLC (1 << 6)
88 #define MT9P031_ROW_ADDRESS_MODE 0x22
89 #define MT9P031_COLUMN_ADDRESS_MODE 0x23
90 #define MT9P031_GLOBAL_GAIN 0x35
91 #define MT9P031_GLOBAL_GAIN_MIN 8
92 #define MT9P031_GLOBAL_GAIN_MAX 1024
93 #define MT9P031_GLOBAL_GAIN_DEF 8
94 #define MT9P031_GLOBAL_GAIN_MULT (1 << 6)
95 #define MT9P031_ROW_BLACK_TARGET 0x49
96 #define MT9P031_ROW_BLACK_DEF_OFFSET 0x4b
97 #define MT9P031_GREEN1_OFFSET 0x60
98 #define MT9P031_GREEN2_OFFSET 0x61
99 #define MT9P031_BLACK_LEVEL_CALIBRATION 0x62
100 #define MT9P031_BLC_MANUAL_BLC (1 << 0)
101 #define MT9P031_RED_OFFSET 0x63
102 #define MT9P031_BLUE_OFFSET 0x64
103 #define MT9P031_TEST_PATTERN 0xa0
104 #define MT9P031_TEST_PATTERN_SHIFT 3
105 #define MT9P031_TEST_PATTERN_ENABLE (1 << 0)
106 #define MT9P031_TEST_PATTERN_DISABLE (0 << 0)
107 #define MT9P031_TEST_PATTERN_GREEN 0xa1
108 #define MT9P031_TEST_PATTERN_RED 0xa2
109 #define MT9P031_TEST_PATTERN_BLUE 0xa3
113 MT9P031_MODEL_MONOCHROME
,
117 struct v4l2_subdev subdev
;
118 struct media_pad pad
;
119 struct v4l2_rect crop
; /* Sensor window */
120 struct v4l2_mbus_framefmt format
;
121 struct mt9p031_platform_data
*pdata
;
122 struct mutex power_lock
; /* lock to protect power_count */
125 struct regulator
*vaa
;
126 struct regulator
*vdd
;
127 struct regulator
*vdd_io
;
129 enum mt9p031_model model
;
130 struct aptina_pll pll
;
133 struct v4l2_ctrl_handler ctrls
;
134 struct v4l2_ctrl
*blc_auto
;
135 struct v4l2_ctrl
*blc_offset
;
137 /* Registers cache */
142 static struct mt9p031
*to_mt9p031(struct v4l2_subdev
*sd
)
144 return container_of(sd
, struct mt9p031
, subdev
);
147 static int mt9p031_read(struct i2c_client
*client
, u8 reg
)
149 return i2c_smbus_read_word_swapped(client
, reg
);
152 static int mt9p031_write(struct i2c_client
*client
, u8 reg
, u16 data
)
154 return i2c_smbus_write_word_swapped(client
, reg
, data
);
157 static int mt9p031_set_output_control(struct mt9p031
*mt9p031
, u16 clear
,
160 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
161 u16 value
= (mt9p031
->output_control
& ~clear
) | set
;
164 ret
= mt9p031_write(client
, MT9P031_OUTPUT_CONTROL
, value
);
168 mt9p031
->output_control
= value
;
172 static int mt9p031_set_mode2(struct mt9p031
*mt9p031
, u16 clear
, u16 set
)
174 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
175 u16 value
= (mt9p031
->mode2
& ~clear
) | set
;
178 ret
= mt9p031_write(client
, MT9P031_READ_MODE_2
, value
);
182 mt9p031
->mode2
= value
;
186 static int mt9p031_reset(struct mt9p031
*mt9p031
)
188 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
191 /* Disable chip output, synchronous option update */
192 ret
= mt9p031_write(client
, MT9P031_RST
, MT9P031_RST_ENABLE
);
195 ret
= mt9p031_write(client
, MT9P031_RST
, MT9P031_RST_DISABLE
);
199 return mt9p031_set_output_control(mt9p031
, MT9P031_OUTPUT_CONTROL_CEN
,
203 static int mt9p031_pll_setup(struct mt9p031
*mt9p031
)
205 static const struct aptina_pll_limits limits
= {
206 .ext_clock_min
= 6000000,
207 .ext_clock_max
= 27000000,
208 .int_clock_min
= 2000000,
209 .int_clock_max
= 13500000,
210 .out_clock_min
= 180000000,
211 .out_clock_max
= 360000000,
212 .pix_clock_max
= 96000000,
221 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
222 struct mt9p031_platform_data
*pdata
= mt9p031
->pdata
;
224 mt9p031
->pll
.ext_clock
= pdata
->ext_freq
;
225 mt9p031
->pll
.pix_clock
= pdata
->target_freq
;
227 return aptina_pll_calculate(&client
->dev
, &limits
, &mt9p031
->pll
);
230 static int mt9p031_pll_enable(struct mt9p031
*mt9p031
)
232 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
235 ret
= mt9p031_write(client
, MT9P031_PLL_CONTROL
,
236 MT9P031_PLL_CONTROL_PWRON
);
240 ret
= mt9p031_write(client
, MT9P031_PLL_CONFIG_1
,
241 (mt9p031
->pll
.m
<< 8) | (mt9p031
->pll
.n
- 1));
245 ret
= mt9p031_write(client
, MT9P031_PLL_CONFIG_2
, mt9p031
->pll
.p1
- 1);
249 usleep_range(1000, 2000);
250 ret
= mt9p031_write(client
, MT9P031_PLL_CONTROL
,
251 MT9P031_PLL_CONTROL_PWRON
|
252 MT9P031_PLL_CONTROL_USEPLL
);
256 static inline int mt9p031_pll_disable(struct mt9p031
*mt9p031
)
258 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
260 return mt9p031_write(client
, MT9P031_PLL_CONTROL
,
261 MT9P031_PLL_CONTROL_PWROFF
);
264 static int mt9p031_power_on(struct mt9p031
*mt9p031
)
266 /* Ensure RESET_BAR is low */
267 if (mt9p031
->reset
!= -1) {
268 gpio_set_value(mt9p031
->reset
, 0);
269 usleep_range(1000, 2000);
272 /* Bring up the supplies */
273 regulator_enable(mt9p031
->vdd
);
274 regulator_enable(mt9p031
->vdd_io
);
275 regulator_enable(mt9p031
->vaa
);
278 if (mt9p031
->pdata
->set_xclk
)
279 mt9p031
->pdata
->set_xclk(&mt9p031
->subdev
,
280 mt9p031
->pdata
->ext_freq
);
282 /* Now RESET_BAR must be high */
283 if (mt9p031
->reset
!= -1) {
284 gpio_set_value(mt9p031
->reset
, 1);
285 usleep_range(1000, 2000);
291 static void mt9p031_power_off(struct mt9p031
*mt9p031
)
293 if (mt9p031
->reset
!= -1) {
294 gpio_set_value(mt9p031
->reset
, 0);
295 usleep_range(1000, 2000);
298 regulator_disable(mt9p031
->vaa
);
299 regulator_disable(mt9p031
->vdd_io
);
300 regulator_disable(mt9p031
->vdd
);
302 if (mt9p031
->pdata
->set_xclk
)
303 mt9p031
->pdata
->set_xclk(&mt9p031
->subdev
, 0);
306 static int __mt9p031_set_power(struct mt9p031
*mt9p031
, bool on
)
308 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
312 mt9p031_power_off(mt9p031
);
316 ret
= mt9p031_power_on(mt9p031
);
320 ret
= mt9p031_reset(mt9p031
);
322 dev_err(&client
->dev
, "Failed to reset the camera\n");
326 return v4l2_ctrl_handler_setup(&mt9p031
->ctrls
);
329 /* -----------------------------------------------------------------------------
330 * V4L2 subdev video operations
333 static int mt9p031_set_params(struct mt9p031
*mt9p031
)
335 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
336 struct v4l2_mbus_framefmt
*format
= &mt9p031
->format
;
337 const struct v4l2_rect
*crop
= &mt9p031
->crop
;
346 /* Windows position and size.
348 * TODO: Make sure the start coordinates and window size match the
349 * skipping, binning and mirroring (see description of registers 2 and 4
350 * in table 13, and Binning section on page 41).
352 ret
= mt9p031_write(client
, MT9P031_COLUMN_START
, crop
->left
);
355 ret
= mt9p031_write(client
, MT9P031_ROW_START
, crop
->top
);
358 ret
= mt9p031_write(client
, MT9P031_WINDOW_WIDTH
, crop
->width
- 1);
361 ret
= mt9p031_write(client
, MT9P031_WINDOW_HEIGHT
, crop
->height
- 1);
365 /* Row and column binning and skipping. Use the maximum binning value
366 * compatible with the skipping settings.
368 xskip
= DIV_ROUND_CLOSEST(crop
->width
, format
->width
);
369 yskip
= DIV_ROUND_CLOSEST(crop
->height
, format
->height
);
370 xbin
= 1 << (ffs(xskip
) - 1);
371 ybin
= 1 << (ffs(yskip
) - 1);
373 ret
= mt9p031_write(client
, MT9P031_COLUMN_ADDRESS_MODE
,
374 ((xbin
- 1) << 4) | (xskip
- 1));
377 ret
= mt9p031_write(client
, MT9P031_ROW_ADDRESS_MODE
,
378 ((ybin
- 1) << 4) | (yskip
- 1));
382 /* Blanking - use minimum value for horizontal blanking and default
383 * value for vertical blanking.
385 hblank
= 346 * ybin
+ 64 + (80 >> min_t(unsigned int, xbin
, 3));
386 vblank
= MT9P031_VERTICAL_BLANK_DEF
;
388 ret
= mt9p031_write(client
, MT9P031_HORIZONTAL_BLANK
, hblank
- 1);
391 ret
= mt9p031_write(client
, MT9P031_VERTICAL_BLANK
, vblank
- 1);
398 static int mt9p031_s_stream(struct v4l2_subdev
*subdev
, int enable
)
400 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
404 /* Stop sensor readout */
405 ret
= mt9p031_set_output_control(mt9p031
,
406 MT9P031_OUTPUT_CONTROL_CEN
, 0);
410 return mt9p031_pll_disable(mt9p031
);
413 ret
= mt9p031_set_params(mt9p031
);
417 /* Switch to master "normal" mode */
418 ret
= mt9p031_set_output_control(mt9p031
, 0,
419 MT9P031_OUTPUT_CONTROL_CEN
);
423 return mt9p031_pll_enable(mt9p031
);
426 static int mt9p031_enum_mbus_code(struct v4l2_subdev
*subdev
,
427 struct v4l2_subdev_fh
*fh
,
428 struct v4l2_subdev_mbus_code_enum
*code
)
430 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
432 if (code
->pad
|| code
->index
)
435 code
->code
= mt9p031
->format
.code
;
439 static int mt9p031_enum_frame_size(struct v4l2_subdev
*subdev
,
440 struct v4l2_subdev_fh
*fh
,
441 struct v4l2_subdev_frame_size_enum
*fse
)
443 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
445 if (fse
->index
>= 8 || fse
->code
!= mt9p031
->format
.code
)
448 fse
->min_width
= MT9P031_WINDOW_WIDTH_DEF
449 / min_t(unsigned int, 7, fse
->index
+ 1);
450 fse
->max_width
= fse
->min_width
;
451 fse
->min_height
= MT9P031_WINDOW_HEIGHT_DEF
/ (fse
->index
+ 1);
452 fse
->max_height
= fse
->min_height
;
457 static struct v4l2_mbus_framefmt
*
458 __mt9p031_get_pad_format(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_format(fh
, pad
);
464 case V4L2_SUBDEV_FORMAT_ACTIVE
:
465 return &mt9p031
->format
;
471 static struct v4l2_rect
*
472 __mt9p031_get_pad_crop(struct mt9p031
*mt9p031
, struct v4l2_subdev_fh
*fh
,
473 unsigned int pad
, u32 which
)
476 case V4L2_SUBDEV_FORMAT_TRY
:
477 return v4l2_subdev_get_try_crop(fh
, pad
);
478 case V4L2_SUBDEV_FORMAT_ACTIVE
:
479 return &mt9p031
->crop
;
485 static int mt9p031_get_format(struct v4l2_subdev
*subdev
,
486 struct v4l2_subdev_fh
*fh
,
487 struct v4l2_subdev_format
*fmt
)
489 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
491 fmt
->format
= *__mt9p031_get_pad_format(mt9p031
, fh
, fmt
->pad
,
496 static int mt9p031_set_format(struct v4l2_subdev
*subdev
,
497 struct v4l2_subdev_fh
*fh
,
498 struct v4l2_subdev_format
*format
)
500 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
501 struct v4l2_mbus_framefmt
*__format
;
502 struct v4l2_rect
*__crop
;
508 __crop
= __mt9p031_get_pad_crop(mt9p031
, fh
, format
->pad
,
511 /* Clamp the width and height to avoid dividing by zero. */
512 width
= clamp_t(unsigned int, ALIGN(format
->format
.width
, 2),
513 max(__crop
->width
/ 7, MT9P031_WINDOW_WIDTH_MIN
),
515 height
= clamp_t(unsigned int, ALIGN(format
->format
.height
, 2),
516 max(__crop
->height
/ 8, MT9P031_WINDOW_HEIGHT_MIN
),
519 hratio
= DIV_ROUND_CLOSEST(__crop
->width
, width
);
520 vratio
= DIV_ROUND_CLOSEST(__crop
->height
, height
);
522 __format
= __mt9p031_get_pad_format(mt9p031
, fh
, format
->pad
,
524 __format
->width
= __crop
->width
/ hratio
;
525 __format
->height
= __crop
->height
/ vratio
;
527 format
->format
= *__format
;
532 static int mt9p031_get_crop(struct v4l2_subdev
*subdev
,
533 struct v4l2_subdev_fh
*fh
,
534 struct v4l2_subdev_crop
*crop
)
536 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
538 crop
->rect
= *__mt9p031_get_pad_crop(mt9p031
, fh
, crop
->pad
,
543 static int mt9p031_set_crop(struct v4l2_subdev
*subdev
,
544 struct v4l2_subdev_fh
*fh
,
545 struct v4l2_subdev_crop
*crop
)
547 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
548 struct v4l2_mbus_framefmt
*__format
;
549 struct v4l2_rect
*__crop
;
550 struct v4l2_rect rect
;
552 /* Clamp the crop rectangle boundaries and align them to a multiple of 2
553 * pixels to ensure a GRBG Bayer pattern.
555 rect
.left
= clamp(ALIGN(crop
->rect
.left
, 2), MT9P031_COLUMN_START_MIN
,
556 MT9P031_COLUMN_START_MAX
);
557 rect
.top
= clamp(ALIGN(crop
->rect
.top
, 2), MT9P031_ROW_START_MIN
,
558 MT9P031_ROW_START_MAX
);
559 rect
.width
= clamp(ALIGN(crop
->rect
.width
, 2),
560 MT9P031_WINDOW_WIDTH_MIN
,
561 MT9P031_WINDOW_WIDTH_MAX
);
562 rect
.height
= clamp(ALIGN(crop
->rect
.height
, 2),
563 MT9P031_WINDOW_HEIGHT_MIN
,
564 MT9P031_WINDOW_HEIGHT_MAX
);
566 rect
.width
= min(rect
.width
, MT9P031_PIXEL_ARRAY_WIDTH
- rect
.left
);
567 rect
.height
= min(rect
.height
, MT9P031_PIXEL_ARRAY_HEIGHT
- rect
.top
);
569 __crop
= __mt9p031_get_pad_crop(mt9p031
, fh
, crop
->pad
, crop
->which
);
571 if (rect
.width
!= __crop
->width
|| rect
.height
!= __crop
->height
) {
572 /* Reset the output image size if the crop rectangle size has
575 __format
= __mt9p031_get_pad_format(mt9p031
, fh
, crop
->pad
,
577 __format
->width
= rect
.width
;
578 __format
->height
= rect
.height
;
587 /* -----------------------------------------------------------------------------
588 * V4L2 subdev control operations
591 #define V4L2_CID_BLC_AUTO (V4L2_CID_USER_BASE | 0x1002)
592 #define V4L2_CID_BLC_TARGET_LEVEL (V4L2_CID_USER_BASE | 0x1003)
593 #define V4L2_CID_BLC_ANALOG_OFFSET (V4L2_CID_USER_BASE | 0x1004)
594 #define V4L2_CID_BLC_DIGITAL_OFFSET (V4L2_CID_USER_BASE | 0x1005)
596 static int mt9p031_s_ctrl(struct v4l2_ctrl
*ctrl
)
598 struct mt9p031
*mt9p031
=
599 container_of(ctrl
->handler
, struct mt9p031
, ctrls
);
600 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
605 case V4L2_CID_EXPOSURE
:
606 ret
= mt9p031_write(client
, MT9P031_SHUTTER_WIDTH_UPPER
,
607 (ctrl
->val
>> 16) & 0xffff);
611 return mt9p031_write(client
, MT9P031_SHUTTER_WIDTH_LOWER
,
615 /* Gain is controlled by 2 analog stages and a digital stage.
616 * Valid values for the 3 stages are
619 * ------------------------------------------
620 * First analog stage x1 x2 1
621 * Second analog stage x1 x4 0.125
622 * Digital stage x1 x16 0.125
624 * To minimize noise, the gain stages should be used in the
625 * second analog stage, first analog stage, digital stage order.
626 * Gain from a previous stage should be pushed to its maximum
627 * value before the next stage is used.
629 if (ctrl
->val
<= 32) {
631 } else if (ctrl
->val
<= 64) {
633 data
= (1 << 6) | (ctrl
->val
>> 1);
636 data
= ((ctrl
->val
- 64) << 5) | (1 << 6) | 32;
639 return mt9p031_write(client
, MT9P031_GLOBAL_GAIN
, data
);
643 return mt9p031_set_mode2(mt9p031
,
644 0, MT9P031_READ_MODE_2_COL_MIR
);
646 return mt9p031_set_mode2(mt9p031
,
647 MT9P031_READ_MODE_2_COL_MIR
, 0);
651 return mt9p031_set_mode2(mt9p031
,
652 0, MT9P031_READ_MODE_2_ROW_MIR
);
654 return mt9p031_set_mode2(mt9p031
,
655 MT9P031_READ_MODE_2_ROW_MIR
, 0);
657 case V4L2_CID_TEST_PATTERN
:
659 /* Restore the black level compensation settings. */
660 if (mt9p031
->blc_auto
->cur
.val
!= 0) {
661 ret
= mt9p031_s_ctrl(mt9p031
->blc_auto
);
665 if (mt9p031
->blc_offset
->cur
.val
!= 0) {
666 ret
= mt9p031_s_ctrl(mt9p031
->blc_offset
);
670 return mt9p031_write(client
, MT9P031_TEST_PATTERN
,
671 MT9P031_TEST_PATTERN_DISABLE
);
674 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_GREEN
, 0x05a0);
677 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_RED
, 0x0a50);
680 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_BLUE
, 0x0aa0);
684 /* Disable digital black level compensation when using a test
687 ret
= mt9p031_set_mode2(mt9p031
, MT9P031_READ_MODE_2_ROW_BLC
,
692 ret
= mt9p031_write(client
, MT9P031_ROW_BLACK_DEF_OFFSET
, 0);
696 return mt9p031_write(client
, MT9P031_TEST_PATTERN
,
697 ((ctrl
->val
- 1) << MT9P031_TEST_PATTERN_SHIFT
)
698 | MT9P031_TEST_PATTERN_ENABLE
);
700 case V4L2_CID_BLC_AUTO
:
701 ret
= mt9p031_set_mode2(mt9p031
,
702 ctrl
->val
? 0 : MT9P031_READ_MODE_2_ROW_BLC
,
703 ctrl
->val
? MT9P031_READ_MODE_2_ROW_BLC
: 0);
707 return mt9p031_write(client
, MT9P031_BLACK_LEVEL_CALIBRATION
,
708 ctrl
->val
? 0 : MT9P031_BLC_MANUAL_BLC
);
710 case V4L2_CID_BLC_TARGET_LEVEL
:
711 return mt9p031_write(client
, MT9P031_ROW_BLACK_TARGET
,
714 case V4L2_CID_BLC_ANALOG_OFFSET
:
715 data
= ctrl
->val
& ((1 << 9) - 1);
717 ret
= mt9p031_write(client
, MT9P031_GREEN1_OFFSET
, data
);
720 ret
= mt9p031_write(client
, MT9P031_GREEN2_OFFSET
, data
);
723 ret
= mt9p031_write(client
, MT9P031_RED_OFFSET
, data
);
726 return mt9p031_write(client
, MT9P031_BLUE_OFFSET
, data
);
728 case V4L2_CID_BLC_DIGITAL_OFFSET
:
729 return mt9p031_write(client
, MT9P031_ROW_BLACK_DEF_OFFSET
,
730 ctrl
->val
& ((1 << 12) - 1));
736 static struct v4l2_ctrl_ops mt9p031_ctrl_ops
= {
737 .s_ctrl
= mt9p031_s_ctrl
,
740 static const char * const mt9p031_test_pattern_menu
[] = {
743 "Horizontal Gradient",
746 "Classic Test Pattern",
748 "Monochrome Horizontal Bars",
749 "Monochrome Vertical Bars",
750 "Vertical Color Bars",
753 static const struct v4l2_ctrl_config mt9p031_ctrls
[] = {
755 .ops
= &mt9p031_ctrl_ops
,
756 .id
= V4L2_CID_BLC_AUTO
,
757 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
765 .ops
= &mt9p031_ctrl_ops
,
766 .id
= V4L2_CID_BLC_TARGET_LEVEL
,
767 .type
= V4L2_CTRL_TYPE_INTEGER
,
768 .name
= "BLC Target Level",
775 .ops
= &mt9p031_ctrl_ops
,
776 .id
= V4L2_CID_BLC_ANALOG_OFFSET
,
777 .type
= V4L2_CTRL_TYPE_INTEGER
,
778 .name
= "BLC Analog Offset",
785 .ops
= &mt9p031_ctrl_ops
,
786 .id
= V4L2_CID_BLC_DIGITAL_OFFSET
,
787 .type
= V4L2_CTRL_TYPE_INTEGER
,
788 .name
= "BLC Digital Offset",
797 /* -----------------------------------------------------------------------------
798 * V4L2 subdev core operations
801 static int mt9p031_set_power(struct v4l2_subdev
*subdev
, int on
)
803 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
806 mutex_lock(&mt9p031
->power_lock
);
808 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
809 * update the power state.
811 if (mt9p031
->power_count
== !on
) {
812 ret
= __mt9p031_set_power(mt9p031
, !!on
);
817 /* Update the power count. */
818 mt9p031
->power_count
+= on
? 1 : -1;
819 WARN_ON(mt9p031
->power_count
< 0);
822 mutex_unlock(&mt9p031
->power_lock
);
826 /* -----------------------------------------------------------------------------
827 * V4L2 subdev internal operations
830 static int mt9p031_registered(struct v4l2_subdev
*subdev
)
832 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
833 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
837 ret
= mt9p031_power_on(mt9p031
);
839 dev_err(&client
->dev
, "MT9P031 power up failed\n");
843 /* Read out the chip version register */
844 data
= mt9p031_read(client
, MT9P031_CHIP_VERSION
);
845 if (data
!= MT9P031_CHIP_VERSION_VALUE
) {
846 dev_err(&client
->dev
, "MT9P031 not detected, wrong version "
851 mt9p031_power_off(mt9p031
);
853 dev_info(&client
->dev
, "MT9P031 detected at address 0x%02x\n",
859 static int mt9p031_open(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
)
861 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
862 struct v4l2_mbus_framefmt
*format
;
863 struct v4l2_rect
*crop
;
865 crop
= v4l2_subdev_get_try_crop(fh
, 0);
866 crop
->left
= MT9P031_COLUMN_START_DEF
;
867 crop
->top
= MT9P031_ROW_START_DEF
;
868 crop
->width
= MT9P031_WINDOW_WIDTH_DEF
;
869 crop
->height
= MT9P031_WINDOW_HEIGHT_DEF
;
871 format
= v4l2_subdev_get_try_format(fh
, 0);
873 if (mt9p031
->model
== MT9P031_MODEL_MONOCHROME
)
874 format
->code
= V4L2_MBUS_FMT_Y12_1X12
;
876 format
->code
= V4L2_MBUS_FMT_SGRBG12_1X12
;
878 format
->width
= MT9P031_WINDOW_WIDTH_DEF
;
879 format
->height
= MT9P031_WINDOW_HEIGHT_DEF
;
880 format
->field
= V4L2_FIELD_NONE
;
881 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
883 return mt9p031_set_power(subdev
, 1);
886 static int mt9p031_close(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
)
888 return mt9p031_set_power(subdev
, 0);
891 static struct v4l2_subdev_core_ops mt9p031_subdev_core_ops
= {
892 .s_power
= mt9p031_set_power
,
895 static struct v4l2_subdev_video_ops mt9p031_subdev_video_ops
= {
896 .s_stream
= mt9p031_s_stream
,
899 static struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops
= {
900 .enum_mbus_code
= mt9p031_enum_mbus_code
,
901 .enum_frame_size
= mt9p031_enum_frame_size
,
902 .get_fmt
= mt9p031_get_format
,
903 .set_fmt
= mt9p031_set_format
,
904 .get_crop
= mt9p031_get_crop
,
905 .set_crop
= mt9p031_set_crop
,
908 static struct v4l2_subdev_ops mt9p031_subdev_ops
= {
909 .core
= &mt9p031_subdev_core_ops
,
910 .video
= &mt9p031_subdev_video_ops
,
911 .pad
= &mt9p031_subdev_pad_ops
,
914 static const struct v4l2_subdev_internal_ops mt9p031_subdev_internal_ops
= {
915 .registered
= mt9p031_registered
,
916 .open
= mt9p031_open
,
917 .close
= mt9p031_close
,
920 /* -----------------------------------------------------------------------------
921 * Driver initialization and probing
924 static int mt9p031_probe(struct i2c_client
*client
,
925 const struct i2c_device_id
*did
)
927 struct mt9p031_platform_data
*pdata
= client
->dev
.platform_data
;
928 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
929 struct mt9p031
*mt9p031
;
934 dev_err(&client
->dev
, "No platform data\n");
938 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
)) {
939 dev_warn(&client
->dev
,
940 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
944 mt9p031
= devm_kzalloc(&client
->dev
, sizeof(*mt9p031
), GFP_KERNEL
);
948 mt9p031
->pdata
= pdata
;
949 mt9p031
->output_control
= MT9P031_OUTPUT_CONTROL_DEF
;
950 mt9p031
->mode2
= MT9P031_READ_MODE_2_ROW_BLC
;
951 mt9p031
->model
= did
->driver_data
;
954 mt9p031
->vaa
= devm_regulator_get(&client
->dev
, "vaa");
955 mt9p031
->vdd
= devm_regulator_get(&client
->dev
, "vdd");
956 mt9p031
->vdd_io
= devm_regulator_get(&client
->dev
, "vdd_io");
958 if (IS_ERR(mt9p031
->vaa
) || IS_ERR(mt9p031
->vdd
) ||
959 IS_ERR(mt9p031
->vdd_io
)) {
960 dev_err(&client
->dev
, "Unable to get regulators\n");
964 v4l2_ctrl_handler_init(&mt9p031
->ctrls
, ARRAY_SIZE(mt9p031_ctrls
) + 6);
966 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
967 V4L2_CID_EXPOSURE
, MT9P031_SHUTTER_WIDTH_MIN
,
968 MT9P031_SHUTTER_WIDTH_MAX
, 1,
969 MT9P031_SHUTTER_WIDTH_DEF
);
970 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
971 V4L2_CID_GAIN
, MT9P031_GLOBAL_GAIN_MIN
,
972 MT9P031_GLOBAL_GAIN_MAX
, 1, MT9P031_GLOBAL_GAIN_DEF
);
973 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
974 V4L2_CID_HFLIP
, 0, 1, 1, 0);
975 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
976 V4L2_CID_VFLIP
, 0, 1, 1, 0);
977 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
978 V4L2_CID_PIXEL_RATE
, pdata
->target_freq
,
979 pdata
->target_freq
, 1, pdata
->target_freq
);
980 v4l2_ctrl_new_std_menu_items(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
981 V4L2_CID_TEST_PATTERN
,
982 ARRAY_SIZE(mt9p031_test_pattern_menu
) - 1, 0,
983 0, mt9p031_test_pattern_menu
);
985 for (i
= 0; i
< ARRAY_SIZE(mt9p031_ctrls
); ++i
)
986 v4l2_ctrl_new_custom(&mt9p031
->ctrls
, &mt9p031_ctrls
[i
], NULL
);
988 mt9p031
->subdev
.ctrl_handler
= &mt9p031
->ctrls
;
990 if (mt9p031
->ctrls
.error
) {
991 printk(KERN_INFO
"%s: control initialization error %d\n",
992 __func__
, mt9p031
->ctrls
.error
);
993 ret
= mt9p031
->ctrls
.error
;
997 mt9p031
->blc_auto
= v4l2_ctrl_find(&mt9p031
->ctrls
, V4L2_CID_BLC_AUTO
);
998 mt9p031
->blc_offset
= v4l2_ctrl_find(&mt9p031
->ctrls
,
999 V4L2_CID_BLC_DIGITAL_OFFSET
);
1001 mutex_init(&mt9p031
->power_lock
);
1002 v4l2_i2c_subdev_init(&mt9p031
->subdev
, client
, &mt9p031_subdev_ops
);
1003 mt9p031
->subdev
.internal_ops
= &mt9p031_subdev_internal_ops
;
1005 mt9p031
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1006 ret
= media_entity_init(&mt9p031
->subdev
.entity
, 1, &mt9p031
->pad
, 0);
1010 mt9p031
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1012 mt9p031
->crop
.width
= MT9P031_WINDOW_WIDTH_DEF
;
1013 mt9p031
->crop
.height
= MT9P031_WINDOW_HEIGHT_DEF
;
1014 mt9p031
->crop
.left
= MT9P031_COLUMN_START_DEF
;
1015 mt9p031
->crop
.top
= MT9P031_ROW_START_DEF
;
1017 if (mt9p031
->model
== MT9P031_MODEL_MONOCHROME
)
1018 mt9p031
->format
.code
= V4L2_MBUS_FMT_Y12_1X12
;
1020 mt9p031
->format
.code
= V4L2_MBUS_FMT_SGRBG12_1X12
;
1022 mt9p031
->format
.width
= MT9P031_WINDOW_WIDTH_DEF
;
1023 mt9p031
->format
.height
= MT9P031_WINDOW_HEIGHT_DEF
;
1024 mt9p031
->format
.field
= V4L2_FIELD_NONE
;
1025 mt9p031
->format
.colorspace
= V4L2_COLORSPACE_SRGB
;
1027 if (pdata
->reset
!= -1) {
1028 ret
= devm_gpio_request_one(&client
->dev
, pdata
->reset
,
1029 GPIOF_OUT_INIT_LOW
, "mt9p031_rst");
1033 mt9p031
->reset
= pdata
->reset
;
1036 ret
= mt9p031_pll_setup(mt9p031
);
1040 v4l2_ctrl_handler_free(&mt9p031
->ctrls
);
1041 media_entity_cleanup(&mt9p031
->subdev
.entity
);
1047 static int mt9p031_remove(struct i2c_client
*client
)
1049 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
1050 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
1052 v4l2_ctrl_handler_free(&mt9p031
->ctrls
);
1053 v4l2_device_unregister_subdev(subdev
);
1054 media_entity_cleanup(&subdev
->entity
);
1059 static const struct i2c_device_id mt9p031_id
[] = {
1060 { "mt9p031", MT9P031_MODEL_COLOR
},
1061 { "mt9p031m", MT9P031_MODEL_MONOCHROME
},
1064 MODULE_DEVICE_TABLE(i2c
, mt9p031_id
);
1066 static struct i2c_driver mt9p031_i2c_driver
= {
1070 .probe
= mt9p031_probe
,
1071 .remove
= mt9p031_remove
,
1072 .id_table
= mt9p031_id
,
1075 module_i2c_driver(mt9p031_i2c_driver
);
1077 MODULE_DESCRIPTION("Aptina MT9P031 Camera driver");
1078 MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
1079 MODULE_LICENSE("GPL v2");