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/module.h>
18 #include <linux/i2c.h>
19 #include <linux/log2.h>
21 #include <linux/slab.h>
22 #include <linux/videodev2.h>
24 #include <media/mt9p031.h>
25 #include <media/v4l2-chip-ident.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-subdev.h>
30 #define MT9P031_PIXEL_ARRAY_WIDTH 2752
31 #define MT9P031_PIXEL_ARRAY_HEIGHT 2004
33 #define MT9P031_CHIP_VERSION 0x00
34 #define MT9P031_CHIP_VERSION_VALUE 0x1801
35 #define MT9P031_ROW_START 0x01
36 #define MT9P031_ROW_START_MIN 0
37 #define MT9P031_ROW_START_MAX 2004
38 #define MT9P031_ROW_START_DEF 54
39 #define MT9P031_COLUMN_START 0x02
40 #define MT9P031_COLUMN_START_MIN 0
41 #define MT9P031_COLUMN_START_MAX 2750
42 #define MT9P031_COLUMN_START_DEF 16
43 #define MT9P031_WINDOW_HEIGHT 0x03
44 #define MT9P031_WINDOW_HEIGHT_MIN 2
45 #define MT9P031_WINDOW_HEIGHT_MAX 2006
46 #define MT9P031_WINDOW_HEIGHT_DEF 1944
47 #define MT9P031_WINDOW_WIDTH 0x04
48 #define MT9P031_WINDOW_WIDTH_MIN 2
49 #define MT9P031_WINDOW_WIDTH_MAX 2752
50 #define MT9P031_WINDOW_WIDTH_DEF 2592
51 #define MT9P031_HORIZONTAL_BLANK 0x05
52 #define MT9P031_HORIZONTAL_BLANK_MIN 0
53 #define MT9P031_HORIZONTAL_BLANK_MAX 4095
54 #define MT9P031_VERTICAL_BLANK 0x06
55 #define MT9P031_VERTICAL_BLANK_MIN 0
56 #define MT9P031_VERTICAL_BLANK_MAX 4095
57 #define MT9P031_VERTICAL_BLANK_DEF 25
58 #define MT9P031_OUTPUT_CONTROL 0x07
59 #define MT9P031_OUTPUT_CONTROL_CEN 2
60 #define MT9P031_OUTPUT_CONTROL_SYN 1
61 #define MT9P031_OUTPUT_CONTROL_DEF 0x1f82
62 #define MT9P031_SHUTTER_WIDTH_UPPER 0x08
63 #define MT9P031_SHUTTER_WIDTH_LOWER 0x09
64 #define MT9P031_SHUTTER_WIDTH_MIN 1
65 #define MT9P031_SHUTTER_WIDTH_MAX 1048575
66 #define MT9P031_SHUTTER_WIDTH_DEF 1943
67 #define MT9P031_PLL_CONTROL 0x10
68 #define MT9P031_PLL_CONTROL_PWROFF 0x0050
69 #define MT9P031_PLL_CONTROL_PWRON 0x0051
70 #define MT9P031_PLL_CONTROL_USEPLL 0x0052
71 #define MT9P031_PLL_CONFIG_1 0x11
72 #define MT9P031_PLL_CONFIG_2 0x12
73 #define MT9P031_PIXEL_CLOCK_CONTROL 0x0a
74 #define MT9P031_FRAME_RESTART 0x0b
75 #define MT9P031_SHUTTER_DELAY 0x0c
76 #define MT9P031_RST 0x0d
77 #define MT9P031_RST_ENABLE 1
78 #define MT9P031_RST_DISABLE 0
79 #define MT9P031_READ_MODE_1 0x1e
80 #define MT9P031_READ_MODE_2 0x20
81 #define MT9P031_READ_MODE_2_ROW_MIR (1 << 15)
82 #define MT9P031_READ_MODE_2_COL_MIR (1 << 14)
83 #define MT9P031_READ_MODE_2_ROW_BLC (1 << 6)
84 #define MT9P031_ROW_ADDRESS_MODE 0x22
85 #define MT9P031_COLUMN_ADDRESS_MODE 0x23
86 #define MT9P031_GLOBAL_GAIN 0x35
87 #define MT9P031_GLOBAL_GAIN_MIN 8
88 #define MT9P031_GLOBAL_GAIN_MAX 1024
89 #define MT9P031_GLOBAL_GAIN_DEF 8
90 #define MT9P031_GLOBAL_GAIN_MULT (1 << 6)
91 #define MT9P031_ROW_BLACK_DEF_OFFSET 0x4b
92 #define MT9P031_TEST_PATTERN 0xa0
93 #define MT9P031_TEST_PATTERN_SHIFT 3
94 #define MT9P031_TEST_PATTERN_ENABLE (1 << 0)
95 #define MT9P031_TEST_PATTERN_DISABLE (0 << 0)
96 #define MT9P031_TEST_PATTERN_GREEN 0xa1
97 #define MT9P031_TEST_PATTERN_RED 0xa2
98 #define MT9P031_TEST_PATTERN_BLUE 0xa3
100 struct mt9p031_pll_divs
{
109 struct v4l2_subdev subdev
;
110 struct media_pad pad
;
111 struct v4l2_rect crop
; /* Sensor window */
112 struct v4l2_mbus_framefmt format
;
113 struct v4l2_ctrl_handler ctrls
;
114 struct mt9p031_platform_data
*pdata
;
115 struct mutex power_lock
; /* lock to protect power_count */
118 const struct mt9p031_pll_divs
*pll
;
120 /* Registers cache */
125 static struct mt9p031
*to_mt9p031(struct v4l2_subdev
*sd
)
127 return container_of(sd
, struct mt9p031
, subdev
);
130 static int mt9p031_read(struct i2c_client
*client
, u8 reg
)
132 return i2c_smbus_read_word_swapped(client
, reg
);
135 static int mt9p031_write(struct i2c_client
*client
, u8 reg
, u16 data
)
137 return i2c_smbus_write_word_swapped(client
, reg
, data
);
140 static int mt9p031_set_output_control(struct mt9p031
*mt9p031
, u16 clear
,
143 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
144 u16 value
= (mt9p031
->output_control
& ~clear
) | set
;
147 ret
= mt9p031_write(client
, MT9P031_OUTPUT_CONTROL
, value
);
151 mt9p031
->output_control
= value
;
155 static int mt9p031_set_mode2(struct mt9p031
*mt9p031
, u16 clear
, u16 set
)
157 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
158 u16 value
= (mt9p031
->mode2
& ~clear
) | set
;
161 ret
= mt9p031_write(client
, MT9P031_READ_MODE_2
, value
);
165 mt9p031
->mode2
= value
;
169 static int mt9p031_reset(struct mt9p031
*mt9p031
)
171 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
174 /* Disable chip output, synchronous option update */
175 ret
= mt9p031_write(client
, MT9P031_RST
, MT9P031_RST_ENABLE
);
178 ret
= mt9p031_write(client
, MT9P031_RST
, MT9P031_RST_DISABLE
);
182 return mt9p031_set_output_control(mt9p031
, MT9P031_OUTPUT_CONTROL_CEN
,
187 * This static table uses ext_freq and vdd_io values to select suitable
188 * PLL dividers m, n and p1 which have been calculated as specifiec in p36
189 * of Aptina's mt9p031 datasheet. New values should be added here.
191 static const struct mt9p031_pll_divs mt9p031_divs
[] = {
192 /* ext_freq target_freq m n p1 */
193 {21000000, 48000000, 26, 2, 6}
196 static int mt9p031_pll_get_divs(struct mt9p031
*mt9p031
)
198 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
201 for (i
= 0; i
< ARRAY_SIZE(mt9p031_divs
); i
++) {
202 if (mt9p031_divs
[i
].ext_freq
== mt9p031
->pdata
->ext_freq
&&
203 mt9p031_divs
[i
].target_freq
== mt9p031
->pdata
->target_freq
) {
204 mt9p031
->pll
= &mt9p031_divs
[i
];
209 dev_err(&client
->dev
, "Couldn't find PLL dividers for ext_freq = %d, "
210 "target_freq = %d\n", mt9p031
->pdata
->ext_freq
,
211 mt9p031
->pdata
->target_freq
);
215 static int mt9p031_pll_enable(struct mt9p031
*mt9p031
)
217 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
220 ret
= mt9p031_write(client
, MT9P031_PLL_CONTROL
,
221 MT9P031_PLL_CONTROL_PWRON
);
225 ret
= mt9p031_write(client
, MT9P031_PLL_CONFIG_1
,
226 (mt9p031
->pll
->m
<< 8) | (mt9p031
->pll
->n
- 1));
230 ret
= mt9p031_write(client
, MT9P031_PLL_CONFIG_2
, mt9p031
->pll
->p1
- 1);
234 usleep_range(1000, 2000);
235 ret
= mt9p031_write(client
, MT9P031_PLL_CONTROL
,
236 MT9P031_PLL_CONTROL_PWRON
|
237 MT9P031_PLL_CONTROL_USEPLL
);
241 static inline int mt9p031_pll_disable(struct mt9p031
*mt9p031
)
243 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
245 return mt9p031_write(client
, MT9P031_PLL_CONTROL
,
246 MT9P031_PLL_CONTROL_PWROFF
);
249 static int mt9p031_power_on(struct mt9p031
*mt9p031
)
251 /* Ensure RESET_BAR is low */
252 if (mt9p031
->pdata
->reset
) {
253 mt9p031
->pdata
->reset(&mt9p031
->subdev
, 1);
254 usleep_range(1000, 2000);
258 if (mt9p031
->pdata
->set_xclk
)
259 mt9p031
->pdata
->set_xclk(&mt9p031
->subdev
,
260 mt9p031
->pdata
->ext_freq
);
262 /* Now RESET_BAR must be high */
263 if (mt9p031
->pdata
->reset
) {
264 mt9p031
->pdata
->reset(&mt9p031
->subdev
, 0);
265 usleep_range(1000, 2000);
271 static void mt9p031_power_off(struct mt9p031
*mt9p031
)
273 if (mt9p031
->pdata
->reset
) {
274 mt9p031
->pdata
->reset(&mt9p031
->subdev
, 1);
275 usleep_range(1000, 2000);
278 if (mt9p031
->pdata
->set_xclk
)
279 mt9p031
->pdata
->set_xclk(&mt9p031
->subdev
, 0);
282 static int __mt9p031_set_power(struct mt9p031
*mt9p031
, bool on
)
284 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
288 mt9p031_power_off(mt9p031
);
292 ret
= mt9p031_power_on(mt9p031
);
296 ret
= mt9p031_reset(mt9p031
);
298 dev_err(&client
->dev
, "Failed to reset the camera\n");
302 return v4l2_ctrl_handler_setup(&mt9p031
->ctrls
);
305 /* -----------------------------------------------------------------------------
306 * V4L2 subdev video operations
309 static int mt9p031_set_params(struct mt9p031
*mt9p031
)
311 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
312 struct v4l2_mbus_framefmt
*format
= &mt9p031
->format
;
313 const struct v4l2_rect
*crop
= &mt9p031
->crop
;
322 /* Windows position and size.
324 * TODO: Make sure the start coordinates and window size match the
325 * skipping, binning and mirroring (see description of registers 2 and 4
326 * in table 13, and Binning section on page 41).
328 ret
= mt9p031_write(client
, MT9P031_COLUMN_START
, crop
->left
);
331 ret
= mt9p031_write(client
, MT9P031_ROW_START
, crop
->top
);
334 ret
= mt9p031_write(client
, MT9P031_WINDOW_WIDTH
, crop
->width
- 1);
337 ret
= mt9p031_write(client
, MT9P031_WINDOW_HEIGHT
, crop
->height
- 1);
341 /* Row and column binning and skipping. Use the maximum binning value
342 * compatible with the skipping settings.
344 xskip
= DIV_ROUND_CLOSEST(crop
->width
, format
->width
);
345 yskip
= DIV_ROUND_CLOSEST(crop
->height
, format
->height
);
346 xbin
= 1 << (ffs(xskip
) - 1);
347 ybin
= 1 << (ffs(yskip
) - 1);
349 ret
= mt9p031_write(client
, MT9P031_COLUMN_ADDRESS_MODE
,
350 ((xbin
- 1) << 4) | (xskip
- 1));
353 ret
= mt9p031_write(client
, MT9P031_ROW_ADDRESS_MODE
,
354 ((ybin
- 1) << 4) | (yskip
- 1));
358 /* Blanking - use minimum value for horizontal blanking and default
359 * value for vertical blanking.
361 hblank
= 346 * ybin
+ 64 + (80 >> max_t(unsigned int, xbin
, 3));
362 vblank
= MT9P031_VERTICAL_BLANK_DEF
;
364 ret
= mt9p031_write(client
, MT9P031_HORIZONTAL_BLANK
, hblank
);
367 ret
= mt9p031_write(client
, MT9P031_VERTICAL_BLANK
, vblank
);
374 static int mt9p031_s_stream(struct v4l2_subdev
*subdev
, int enable
)
376 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
380 /* Stop sensor readout */
381 ret
= mt9p031_set_output_control(mt9p031
,
382 MT9P031_OUTPUT_CONTROL_CEN
, 0);
386 return mt9p031_pll_disable(mt9p031
);
389 ret
= mt9p031_set_params(mt9p031
);
393 /* Switch to master "normal" mode */
394 ret
= mt9p031_set_output_control(mt9p031
, 0,
395 MT9P031_OUTPUT_CONTROL_CEN
);
399 return mt9p031_pll_enable(mt9p031
);
402 static int mt9p031_enum_mbus_code(struct v4l2_subdev
*subdev
,
403 struct v4l2_subdev_fh
*fh
,
404 struct v4l2_subdev_mbus_code_enum
*code
)
406 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
408 if (code
->pad
|| code
->index
)
411 code
->code
= mt9p031
->format
.code
;
415 static int mt9p031_enum_frame_size(struct v4l2_subdev
*subdev
,
416 struct v4l2_subdev_fh
*fh
,
417 struct v4l2_subdev_frame_size_enum
*fse
)
419 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
421 if (fse
->index
>= 8 || fse
->code
!= mt9p031
->format
.code
)
424 fse
->min_width
= MT9P031_WINDOW_WIDTH_DEF
425 / min_t(unsigned int, 7, fse
->index
+ 1);
426 fse
->max_width
= fse
->min_width
;
427 fse
->min_height
= MT9P031_WINDOW_HEIGHT_DEF
/ (fse
->index
+ 1);
428 fse
->max_height
= fse
->min_height
;
433 static struct v4l2_mbus_framefmt
*
434 __mt9p031_get_pad_format(struct mt9p031
*mt9p031
, struct v4l2_subdev_fh
*fh
,
435 unsigned int pad
, u32 which
)
438 case V4L2_SUBDEV_FORMAT_TRY
:
439 return v4l2_subdev_get_try_format(fh
, pad
);
440 case V4L2_SUBDEV_FORMAT_ACTIVE
:
441 return &mt9p031
->format
;
447 static struct v4l2_rect
*
448 __mt9p031_get_pad_crop(struct mt9p031
*mt9p031
, struct v4l2_subdev_fh
*fh
,
449 unsigned int pad
, u32 which
)
452 case V4L2_SUBDEV_FORMAT_TRY
:
453 return v4l2_subdev_get_try_crop(fh
, pad
);
454 case V4L2_SUBDEV_FORMAT_ACTIVE
:
455 return &mt9p031
->crop
;
461 static int mt9p031_get_format(struct v4l2_subdev
*subdev
,
462 struct v4l2_subdev_fh
*fh
,
463 struct v4l2_subdev_format
*fmt
)
465 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
467 fmt
->format
= *__mt9p031_get_pad_format(mt9p031
, fh
, fmt
->pad
,
472 static int mt9p031_set_format(struct v4l2_subdev
*subdev
,
473 struct v4l2_subdev_fh
*fh
,
474 struct v4l2_subdev_format
*format
)
476 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
477 struct v4l2_mbus_framefmt
*__format
;
478 struct v4l2_rect
*__crop
;
484 __crop
= __mt9p031_get_pad_crop(mt9p031
, fh
, format
->pad
,
487 /* Clamp the width and height to avoid dividing by zero. */
488 width
= clamp_t(unsigned int, ALIGN(format
->format
.width
, 2),
489 max(__crop
->width
/ 7, MT9P031_WINDOW_WIDTH_MIN
),
491 height
= clamp_t(unsigned int, ALIGN(format
->format
.height
, 2),
492 max(__crop
->height
/ 8, MT9P031_WINDOW_HEIGHT_MIN
),
495 hratio
= DIV_ROUND_CLOSEST(__crop
->width
, width
);
496 vratio
= DIV_ROUND_CLOSEST(__crop
->height
, height
);
498 __format
= __mt9p031_get_pad_format(mt9p031
, fh
, format
->pad
,
500 __format
->width
= __crop
->width
/ hratio
;
501 __format
->height
= __crop
->height
/ vratio
;
503 format
->format
= *__format
;
508 static int mt9p031_get_crop(struct v4l2_subdev
*subdev
,
509 struct v4l2_subdev_fh
*fh
,
510 struct v4l2_subdev_crop
*crop
)
512 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
514 crop
->rect
= *__mt9p031_get_pad_crop(mt9p031
, fh
, crop
->pad
,
519 static int mt9p031_set_crop(struct v4l2_subdev
*subdev
,
520 struct v4l2_subdev_fh
*fh
,
521 struct v4l2_subdev_crop
*crop
)
523 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
524 struct v4l2_mbus_framefmt
*__format
;
525 struct v4l2_rect
*__crop
;
526 struct v4l2_rect rect
;
528 /* Clamp the crop rectangle boundaries and align them to a multiple of 2
529 * pixels to ensure a GRBG Bayer pattern.
531 rect
.left
= clamp(ALIGN(crop
->rect
.left
, 2), MT9P031_COLUMN_START_MIN
,
532 MT9P031_COLUMN_START_MAX
);
533 rect
.top
= clamp(ALIGN(crop
->rect
.top
, 2), MT9P031_ROW_START_MIN
,
534 MT9P031_ROW_START_MAX
);
535 rect
.width
= clamp(ALIGN(crop
->rect
.width
, 2),
536 MT9P031_WINDOW_WIDTH_MIN
,
537 MT9P031_WINDOW_WIDTH_MAX
);
538 rect
.height
= clamp(ALIGN(crop
->rect
.height
, 2),
539 MT9P031_WINDOW_HEIGHT_MIN
,
540 MT9P031_WINDOW_HEIGHT_MAX
);
542 rect
.width
= min(rect
.width
, MT9P031_PIXEL_ARRAY_WIDTH
- rect
.left
);
543 rect
.height
= min(rect
.height
, MT9P031_PIXEL_ARRAY_HEIGHT
- rect
.top
);
545 __crop
= __mt9p031_get_pad_crop(mt9p031
, fh
, crop
->pad
, crop
->which
);
547 if (rect
.width
!= __crop
->width
|| rect
.height
!= __crop
->height
) {
548 /* Reset the output image size if the crop rectangle size has
551 __format
= __mt9p031_get_pad_format(mt9p031
, fh
, crop
->pad
,
553 __format
->width
= rect
.width
;
554 __format
->height
= rect
.height
;
563 /* -----------------------------------------------------------------------------
564 * V4L2 subdev control operations
567 #define V4L2_CID_TEST_PATTERN (V4L2_CID_USER_BASE | 0x1001)
569 static int mt9p031_s_ctrl(struct v4l2_ctrl
*ctrl
)
571 struct mt9p031
*mt9p031
=
572 container_of(ctrl
->handler
, struct mt9p031
, ctrls
);
573 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
578 case V4L2_CID_EXPOSURE
:
579 ret
= mt9p031_write(client
, MT9P031_SHUTTER_WIDTH_UPPER
,
580 (ctrl
->val
>> 16) & 0xffff);
584 return mt9p031_write(client
, MT9P031_SHUTTER_WIDTH_LOWER
,
588 /* Gain is controlled by 2 analog stages and a digital stage.
589 * Valid values for the 3 stages are
592 * ------------------------------------------
593 * First analog stage x1 x2 1
594 * Second analog stage x1 x4 0.125
595 * Digital stage x1 x16 0.125
597 * To minimize noise, the gain stages should be used in the
598 * second analog stage, first analog stage, digital stage order.
599 * Gain from a previous stage should be pushed to its maximum
600 * value before the next stage is used.
602 if (ctrl
->val
<= 32) {
604 } else if (ctrl
->val
<= 64) {
606 data
= (1 << 6) | (ctrl
->val
>> 1);
609 data
= ((ctrl
->val
- 64) << 5) | (1 << 6) | 32;
612 return mt9p031_write(client
, MT9P031_GLOBAL_GAIN
, data
);
616 return mt9p031_set_mode2(mt9p031
,
617 0, MT9P031_READ_MODE_2_COL_MIR
);
619 return mt9p031_set_mode2(mt9p031
,
620 MT9P031_READ_MODE_2_COL_MIR
, 0);
624 return mt9p031_set_mode2(mt9p031
,
625 0, MT9P031_READ_MODE_2_ROW_MIR
);
627 return mt9p031_set_mode2(mt9p031
,
628 MT9P031_READ_MODE_2_ROW_MIR
, 0);
630 case V4L2_CID_TEST_PATTERN
:
632 ret
= mt9p031_set_mode2(mt9p031
,
633 0, MT9P031_READ_MODE_2_ROW_BLC
);
637 return mt9p031_write(client
, MT9P031_TEST_PATTERN
,
638 MT9P031_TEST_PATTERN_DISABLE
);
641 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_GREEN
, 0x05a0);
644 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_RED
, 0x0a50);
647 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_BLUE
, 0x0aa0);
651 ret
= mt9p031_set_mode2(mt9p031
, MT9P031_READ_MODE_2_ROW_BLC
,
655 ret
= mt9p031_write(client
, MT9P031_ROW_BLACK_DEF_OFFSET
, 0);
659 return mt9p031_write(client
, MT9P031_TEST_PATTERN
,
660 ((ctrl
->val
- 1) << MT9P031_TEST_PATTERN_SHIFT
)
661 | MT9P031_TEST_PATTERN_ENABLE
);
666 static struct v4l2_ctrl_ops mt9p031_ctrl_ops
= {
667 .s_ctrl
= mt9p031_s_ctrl
,
670 static const char * const mt9p031_test_pattern_menu
[] = {
673 "Horizontal Gradient",
676 "Classic Test Pattern",
678 "Monochrome Horizontal Bars",
679 "Monochrome Vertical Bars",
680 "Vertical Color Bars",
683 static const struct v4l2_ctrl_config mt9p031_ctrls
[] = {
685 .ops
= &mt9p031_ctrl_ops
,
686 .id
= V4L2_CID_TEST_PATTERN
,
687 .type
= V4L2_CTRL_TYPE_MENU
,
688 .name
= "Test Pattern",
690 .max
= ARRAY_SIZE(mt9p031_test_pattern_menu
) - 1,
695 .qmenu
= mt9p031_test_pattern_menu
,
699 /* -----------------------------------------------------------------------------
700 * V4L2 subdev core operations
703 static int mt9p031_set_power(struct v4l2_subdev
*subdev
, int on
)
705 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
708 mutex_lock(&mt9p031
->power_lock
);
710 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
711 * update the power state.
713 if (mt9p031
->power_count
== !on
) {
714 ret
= __mt9p031_set_power(mt9p031
, !!on
);
719 /* Update the power count. */
720 mt9p031
->power_count
+= on
? 1 : -1;
721 WARN_ON(mt9p031
->power_count
< 0);
724 mutex_unlock(&mt9p031
->power_lock
);
728 /* -----------------------------------------------------------------------------
729 * V4L2 subdev internal operations
732 static int mt9p031_registered(struct v4l2_subdev
*subdev
)
734 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
735 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
739 ret
= mt9p031_power_on(mt9p031
);
741 dev_err(&client
->dev
, "MT9P031 power up failed\n");
745 /* Read out the chip version register */
746 data
= mt9p031_read(client
, MT9P031_CHIP_VERSION
);
747 if (data
!= MT9P031_CHIP_VERSION_VALUE
) {
748 dev_err(&client
->dev
, "MT9P031 not detected, wrong version "
753 mt9p031_power_off(mt9p031
);
755 dev_info(&client
->dev
, "MT9P031 detected at address 0x%02x\n",
761 static int mt9p031_open(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
)
763 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
764 struct v4l2_mbus_framefmt
*format
;
765 struct v4l2_rect
*crop
;
767 crop
= v4l2_subdev_get_try_crop(fh
, 0);
768 crop
->left
= MT9P031_COLUMN_START_DEF
;
769 crop
->top
= MT9P031_ROW_START_DEF
;
770 crop
->width
= MT9P031_WINDOW_WIDTH_DEF
;
771 crop
->height
= MT9P031_WINDOW_HEIGHT_DEF
;
773 format
= v4l2_subdev_get_try_format(fh
, 0);
775 if (mt9p031
->pdata
->version
== MT9P031_MONOCHROME_VERSION
)
776 format
->code
= V4L2_MBUS_FMT_Y12_1X12
;
778 format
->code
= V4L2_MBUS_FMT_SGRBG12_1X12
;
780 format
->width
= MT9P031_WINDOW_WIDTH_DEF
;
781 format
->height
= MT9P031_WINDOW_HEIGHT_DEF
;
782 format
->field
= V4L2_FIELD_NONE
;
783 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
785 return mt9p031_set_power(subdev
, 1);
788 static int mt9p031_close(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
)
790 return mt9p031_set_power(subdev
, 0);
793 static struct v4l2_subdev_core_ops mt9p031_subdev_core_ops
= {
794 .s_power
= mt9p031_set_power
,
797 static struct v4l2_subdev_video_ops mt9p031_subdev_video_ops
= {
798 .s_stream
= mt9p031_s_stream
,
801 static struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops
= {
802 .enum_mbus_code
= mt9p031_enum_mbus_code
,
803 .enum_frame_size
= mt9p031_enum_frame_size
,
804 .get_fmt
= mt9p031_get_format
,
805 .set_fmt
= mt9p031_set_format
,
806 .get_crop
= mt9p031_get_crop
,
807 .set_crop
= mt9p031_set_crop
,
810 static struct v4l2_subdev_ops mt9p031_subdev_ops
= {
811 .core
= &mt9p031_subdev_core_ops
,
812 .video
= &mt9p031_subdev_video_ops
,
813 .pad
= &mt9p031_subdev_pad_ops
,
816 static const struct v4l2_subdev_internal_ops mt9p031_subdev_internal_ops
= {
817 .registered
= mt9p031_registered
,
818 .open
= mt9p031_open
,
819 .close
= mt9p031_close
,
822 /* -----------------------------------------------------------------------------
823 * Driver initialization and probing
826 static int mt9p031_probe(struct i2c_client
*client
,
827 const struct i2c_device_id
*did
)
829 struct mt9p031_platform_data
*pdata
= client
->dev
.platform_data
;
830 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
831 struct mt9p031
*mt9p031
;
836 dev_err(&client
->dev
, "No platform data\n");
840 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
)) {
841 dev_warn(&client
->dev
,
842 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
846 mt9p031
= kzalloc(sizeof(*mt9p031
), GFP_KERNEL
);
850 mt9p031
->pdata
= pdata
;
851 mt9p031
->output_control
= MT9P031_OUTPUT_CONTROL_DEF
;
852 mt9p031
->mode2
= MT9P031_READ_MODE_2_ROW_BLC
;
854 v4l2_ctrl_handler_init(&mt9p031
->ctrls
, ARRAY_SIZE(mt9p031_ctrls
) + 4);
856 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
857 V4L2_CID_EXPOSURE
, MT9P031_SHUTTER_WIDTH_MIN
,
858 MT9P031_SHUTTER_WIDTH_MAX
, 1,
859 MT9P031_SHUTTER_WIDTH_DEF
);
860 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
861 V4L2_CID_GAIN
, MT9P031_GLOBAL_GAIN_MIN
,
862 MT9P031_GLOBAL_GAIN_MAX
, 1, MT9P031_GLOBAL_GAIN_DEF
);
863 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
864 V4L2_CID_HFLIP
, 0, 1, 1, 0);
865 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
866 V4L2_CID_VFLIP
, 0, 1, 1, 0);
868 for (i
= 0; i
< ARRAY_SIZE(mt9p031_ctrls
); ++i
)
869 v4l2_ctrl_new_custom(&mt9p031
->ctrls
, &mt9p031_ctrls
[i
], NULL
);
871 mt9p031
->subdev
.ctrl_handler
= &mt9p031
->ctrls
;
873 if (mt9p031
->ctrls
.error
)
874 printk(KERN_INFO
"%s: control initialization error %d\n",
875 __func__
, mt9p031
->ctrls
.error
);
877 mutex_init(&mt9p031
->power_lock
);
878 v4l2_i2c_subdev_init(&mt9p031
->subdev
, client
, &mt9p031_subdev_ops
);
879 mt9p031
->subdev
.internal_ops
= &mt9p031_subdev_internal_ops
;
881 mt9p031
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
882 ret
= media_entity_init(&mt9p031
->subdev
.entity
, 1, &mt9p031
->pad
, 0);
886 mt9p031
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
888 mt9p031
->crop
.width
= MT9P031_WINDOW_WIDTH_DEF
;
889 mt9p031
->crop
.height
= MT9P031_WINDOW_HEIGHT_DEF
;
890 mt9p031
->crop
.left
= MT9P031_COLUMN_START_DEF
;
891 mt9p031
->crop
.top
= MT9P031_ROW_START_DEF
;
893 if (mt9p031
->pdata
->version
== MT9P031_MONOCHROME_VERSION
)
894 mt9p031
->format
.code
= V4L2_MBUS_FMT_Y12_1X12
;
896 mt9p031
->format
.code
= V4L2_MBUS_FMT_SGRBG12_1X12
;
898 mt9p031
->format
.width
= MT9P031_WINDOW_WIDTH_DEF
;
899 mt9p031
->format
.height
= MT9P031_WINDOW_HEIGHT_DEF
;
900 mt9p031
->format
.field
= V4L2_FIELD_NONE
;
901 mt9p031
->format
.colorspace
= V4L2_COLORSPACE_SRGB
;
903 ret
= mt9p031_pll_get_divs(mt9p031
);
907 v4l2_ctrl_handler_free(&mt9p031
->ctrls
);
908 media_entity_cleanup(&mt9p031
->subdev
.entity
);
915 static int mt9p031_remove(struct i2c_client
*client
)
917 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
918 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
920 v4l2_ctrl_handler_free(&mt9p031
->ctrls
);
921 v4l2_device_unregister_subdev(subdev
);
922 media_entity_cleanup(&subdev
->entity
);
928 static const struct i2c_device_id mt9p031_id
[] = {
932 MODULE_DEVICE_TABLE(i2c
, mt9p031_id
);
934 static struct i2c_driver mt9p031_i2c_driver
= {
938 .probe
= mt9p031_probe
,
939 .remove
= mt9p031_remove
,
940 .id_table
= mt9p031_id
,
943 module_i2c_driver(mt9p031_i2c_driver
);
945 MODULE_DESCRIPTION("Aptina MT9P031 Camera driver");
946 MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
947 MODULE_LICENSE("GPL v2");