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 #include "aptina-pll.h"
32 #define MT9P031_PIXEL_ARRAY_WIDTH 2752
33 #define MT9P031_PIXEL_ARRAY_HEIGHT 2004
35 #define MT9P031_CHIP_VERSION 0x00
36 #define MT9P031_CHIP_VERSION_VALUE 0x1801
37 #define MT9P031_ROW_START 0x01
38 #define MT9P031_ROW_START_MIN 0
39 #define MT9P031_ROW_START_MAX 2004
40 #define MT9P031_ROW_START_DEF 54
41 #define MT9P031_COLUMN_START 0x02
42 #define MT9P031_COLUMN_START_MIN 0
43 #define MT9P031_COLUMN_START_MAX 2750
44 #define MT9P031_COLUMN_START_DEF 16
45 #define MT9P031_WINDOW_HEIGHT 0x03
46 #define MT9P031_WINDOW_HEIGHT_MIN 2
47 #define MT9P031_WINDOW_HEIGHT_MAX 2006
48 #define MT9P031_WINDOW_HEIGHT_DEF 1944
49 #define MT9P031_WINDOW_WIDTH 0x04
50 #define MT9P031_WINDOW_WIDTH_MIN 2
51 #define MT9P031_WINDOW_WIDTH_MAX 2752
52 #define MT9P031_WINDOW_WIDTH_DEF 2592
53 #define MT9P031_HORIZONTAL_BLANK 0x05
54 #define MT9P031_HORIZONTAL_BLANK_MIN 0
55 #define MT9P031_HORIZONTAL_BLANK_MAX 4095
56 #define MT9P031_VERTICAL_BLANK 0x06
57 #define MT9P031_VERTICAL_BLANK_MIN 0
58 #define MT9P031_VERTICAL_BLANK_MAX 4095
59 #define MT9P031_VERTICAL_BLANK_DEF 25
60 #define MT9P031_OUTPUT_CONTROL 0x07
61 #define MT9P031_OUTPUT_CONTROL_CEN 2
62 #define MT9P031_OUTPUT_CONTROL_SYN 1
63 #define MT9P031_OUTPUT_CONTROL_DEF 0x1f82
64 #define MT9P031_SHUTTER_WIDTH_UPPER 0x08
65 #define MT9P031_SHUTTER_WIDTH_LOWER 0x09
66 #define MT9P031_SHUTTER_WIDTH_MIN 1
67 #define MT9P031_SHUTTER_WIDTH_MAX 1048575
68 #define MT9P031_SHUTTER_WIDTH_DEF 1943
69 #define MT9P031_PLL_CONTROL 0x10
70 #define MT9P031_PLL_CONTROL_PWROFF 0x0050
71 #define MT9P031_PLL_CONTROL_PWRON 0x0051
72 #define MT9P031_PLL_CONTROL_USEPLL 0x0052
73 #define MT9P031_PLL_CONFIG_1 0x11
74 #define MT9P031_PLL_CONFIG_2 0x12
75 #define MT9P031_PIXEL_CLOCK_CONTROL 0x0a
76 #define MT9P031_FRAME_RESTART 0x0b
77 #define MT9P031_SHUTTER_DELAY 0x0c
78 #define MT9P031_RST 0x0d
79 #define MT9P031_RST_ENABLE 1
80 #define MT9P031_RST_DISABLE 0
81 #define MT9P031_READ_MODE_1 0x1e
82 #define MT9P031_READ_MODE_2 0x20
83 #define MT9P031_READ_MODE_2_ROW_MIR (1 << 15)
84 #define MT9P031_READ_MODE_2_COL_MIR (1 << 14)
85 #define MT9P031_READ_MODE_2_ROW_BLC (1 << 6)
86 #define MT9P031_ROW_ADDRESS_MODE 0x22
87 #define MT9P031_COLUMN_ADDRESS_MODE 0x23
88 #define MT9P031_GLOBAL_GAIN 0x35
89 #define MT9P031_GLOBAL_GAIN_MIN 8
90 #define MT9P031_GLOBAL_GAIN_MAX 1024
91 #define MT9P031_GLOBAL_GAIN_DEF 8
92 #define MT9P031_GLOBAL_GAIN_MULT (1 << 6)
93 #define MT9P031_ROW_BLACK_DEF_OFFSET 0x4b
94 #define MT9P031_TEST_PATTERN 0xa0
95 #define MT9P031_TEST_PATTERN_SHIFT 3
96 #define MT9P031_TEST_PATTERN_ENABLE (1 << 0)
97 #define MT9P031_TEST_PATTERN_DISABLE (0 << 0)
98 #define MT9P031_TEST_PATTERN_GREEN 0xa1
99 #define MT9P031_TEST_PATTERN_RED 0xa2
100 #define MT9P031_TEST_PATTERN_BLUE 0xa3
103 struct v4l2_subdev subdev
;
104 struct media_pad pad
;
105 struct v4l2_rect crop
; /* Sensor window */
106 struct v4l2_mbus_framefmt format
;
107 struct v4l2_ctrl_handler ctrls
;
108 struct mt9p031_platform_data
*pdata
;
109 struct mutex power_lock
; /* lock to protect power_count */
112 struct aptina_pll pll
;
114 /* Registers cache */
119 static struct mt9p031
*to_mt9p031(struct v4l2_subdev
*sd
)
121 return container_of(sd
, struct mt9p031
, subdev
);
124 static int mt9p031_read(struct i2c_client
*client
, u8 reg
)
126 return i2c_smbus_read_word_swapped(client
, reg
);
129 static int mt9p031_write(struct i2c_client
*client
, u8 reg
, u16 data
)
131 return i2c_smbus_write_word_swapped(client
, reg
, data
);
134 static int mt9p031_set_output_control(struct mt9p031
*mt9p031
, u16 clear
,
137 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
138 u16 value
= (mt9p031
->output_control
& ~clear
) | set
;
141 ret
= mt9p031_write(client
, MT9P031_OUTPUT_CONTROL
, value
);
145 mt9p031
->output_control
= value
;
149 static int mt9p031_set_mode2(struct mt9p031
*mt9p031
, u16 clear
, u16 set
)
151 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
152 u16 value
= (mt9p031
->mode2
& ~clear
) | set
;
155 ret
= mt9p031_write(client
, MT9P031_READ_MODE_2
, value
);
159 mt9p031
->mode2
= value
;
163 static int mt9p031_reset(struct mt9p031
*mt9p031
)
165 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
168 /* Disable chip output, synchronous option update */
169 ret
= mt9p031_write(client
, MT9P031_RST
, MT9P031_RST_ENABLE
);
172 ret
= mt9p031_write(client
, MT9P031_RST
, MT9P031_RST_DISABLE
);
176 return mt9p031_set_output_control(mt9p031
, MT9P031_OUTPUT_CONTROL_CEN
,
180 static int mt9p031_pll_setup(struct mt9p031
*mt9p031
)
182 static const struct aptina_pll_limits limits
= {
183 .ext_clock_min
= 6000000,
184 .ext_clock_max
= 27000000,
185 .int_clock_min
= 2000000,
186 .int_clock_max
= 13500000,
187 .out_clock_min
= 180000000,
188 .out_clock_max
= 360000000,
189 .pix_clock_max
= 96000000,
198 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
199 struct mt9p031_platform_data
*pdata
= mt9p031
->pdata
;
201 mt9p031
->pll
.ext_clock
= pdata
->ext_freq
;
202 mt9p031
->pll
.pix_clock
= pdata
->target_freq
;
204 return aptina_pll_calculate(&client
->dev
, &limits
, &mt9p031
->pll
);
207 static int mt9p031_pll_enable(struct mt9p031
*mt9p031
)
209 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
212 ret
= mt9p031_write(client
, MT9P031_PLL_CONTROL
,
213 MT9P031_PLL_CONTROL_PWRON
);
217 ret
= mt9p031_write(client
, MT9P031_PLL_CONFIG_1
,
218 (mt9p031
->pll
.m
<< 8) | (mt9p031
->pll
.n
- 1));
222 ret
= mt9p031_write(client
, MT9P031_PLL_CONFIG_2
, mt9p031
->pll
.p1
- 1);
226 usleep_range(1000, 2000);
227 ret
= mt9p031_write(client
, MT9P031_PLL_CONTROL
,
228 MT9P031_PLL_CONTROL_PWRON
|
229 MT9P031_PLL_CONTROL_USEPLL
);
233 static inline int mt9p031_pll_disable(struct mt9p031
*mt9p031
)
235 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
237 return mt9p031_write(client
, MT9P031_PLL_CONTROL
,
238 MT9P031_PLL_CONTROL_PWROFF
);
241 static int mt9p031_power_on(struct mt9p031
*mt9p031
)
243 /* Ensure RESET_BAR is low */
244 if (mt9p031
->pdata
->reset
) {
245 mt9p031
->pdata
->reset(&mt9p031
->subdev
, 1);
246 usleep_range(1000, 2000);
250 if (mt9p031
->pdata
->set_xclk
)
251 mt9p031
->pdata
->set_xclk(&mt9p031
->subdev
,
252 mt9p031
->pdata
->ext_freq
);
254 /* Now RESET_BAR must be high */
255 if (mt9p031
->pdata
->reset
) {
256 mt9p031
->pdata
->reset(&mt9p031
->subdev
, 0);
257 usleep_range(1000, 2000);
263 static void mt9p031_power_off(struct mt9p031
*mt9p031
)
265 if (mt9p031
->pdata
->reset
) {
266 mt9p031
->pdata
->reset(&mt9p031
->subdev
, 1);
267 usleep_range(1000, 2000);
270 if (mt9p031
->pdata
->set_xclk
)
271 mt9p031
->pdata
->set_xclk(&mt9p031
->subdev
, 0);
274 static int __mt9p031_set_power(struct mt9p031
*mt9p031
, bool on
)
276 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
280 mt9p031_power_off(mt9p031
);
284 ret
= mt9p031_power_on(mt9p031
);
288 ret
= mt9p031_reset(mt9p031
);
290 dev_err(&client
->dev
, "Failed to reset the camera\n");
294 return v4l2_ctrl_handler_setup(&mt9p031
->ctrls
);
297 /* -----------------------------------------------------------------------------
298 * V4L2 subdev video operations
301 static int mt9p031_set_params(struct mt9p031
*mt9p031
)
303 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
304 struct v4l2_mbus_framefmt
*format
= &mt9p031
->format
;
305 const struct v4l2_rect
*crop
= &mt9p031
->crop
;
314 /* Windows position and size.
316 * TODO: Make sure the start coordinates and window size match the
317 * skipping, binning and mirroring (see description of registers 2 and 4
318 * in table 13, and Binning section on page 41).
320 ret
= mt9p031_write(client
, MT9P031_COLUMN_START
, crop
->left
);
323 ret
= mt9p031_write(client
, MT9P031_ROW_START
, crop
->top
);
326 ret
= mt9p031_write(client
, MT9P031_WINDOW_WIDTH
, crop
->width
- 1);
329 ret
= mt9p031_write(client
, MT9P031_WINDOW_HEIGHT
, crop
->height
- 1);
333 /* Row and column binning and skipping. Use the maximum binning value
334 * compatible with the skipping settings.
336 xskip
= DIV_ROUND_CLOSEST(crop
->width
, format
->width
);
337 yskip
= DIV_ROUND_CLOSEST(crop
->height
, format
->height
);
338 xbin
= 1 << (ffs(xskip
) - 1);
339 ybin
= 1 << (ffs(yskip
) - 1);
341 ret
= mt9p031_write(client
, MT9P031_COLUMN_ADDRESS_MODE
,
342 ((xbin
- 1) << 4) | (xskip
- 1));
345 ret
= mt9p031_write(client
, MT9P031_ROW_ADDRESS_MODE
,
346 ((ybin
- 1) << 4) | (yskip
- 1));
350 /* Blanking - use minimum value for horizontal blanking and default
351 * value for vertical blanking.
353 hblank
= 346 * ybin
+ 64 + (80 >> max_t(unsigned int, xbin
, 3));
354 vblank
= MT9P031_VERTICAL_BLANK_DEF
;
356 ret
= mt9p031_write(client
, MT9P031_HORIZONTAL_BLANK
, hblank
);
359 ret
= mt9p031_write(client
, MT9P031_VERTICAL_BLANK
, vblank
);
366 static int mt9p031_s_stream(struct v4l2_subdev
*subdev
, int enable
)
368 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
372 /* Stop sensor readout */
373 ret
= mt9p031_set_output_control(mt9p031
,
374 MT9P031_OUTPUT_CONTROL_CEN
, 0);
378 return mt9p031_pll_disable(mt9p031
);
381 ret
= mt9p031_set_params(mt9p031
);
385 /* Switch to master "normal" mode */
386 ret
= mt9p031_set_output_control(mt9p031
, 0,
387 MT9P031_OUTPUT_CONTROL_CEN
);
391 return mt9p031_pll_enable(mt9p031
);
394 static int mt9p031_enum_mbus_code(struct v4l2_subdev
*subdev
,
395 struct v4l2_subdev_fh
*fh
,
396 struct v4l2_subdev_mbus_code_enum
*code
)
398 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
400 if (code
->pad
|| code
->index
)
403 code
->code
= mt9p031
->format
.code
;
407 static int mt9p031_enum_frame_size(struct v4l2_subdev
*subdev
,
408 struct v4l2_subdev_fh
*fh
,
409 struct v4l2_subdev_frame_size_enum
*fse
)
411 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
413 if (fse
->index
>= 8 || fse
->code
!= mt9p031
->format
.code
)
416 fse
->min_width
= MT9P031_WINDOW_WIDTH_DEF
417 / min_t(unsigned int, 7, fse
->index
+ 1);
418 fse
->max_width
= fse
->min_width
;
419 fse
->min_height
= MT9P031_WINDOW_HEIGHT_DEF
/ (fse
->index
+ 1);
420 fse
->max_height
= fse
->min_height
;
425 static struct v4l2_mbus_framefmt
*
426 __mt9p031_get_pad_format(struct mt9p031
*mt9p031
, struct v4l2_subdev_fh
*fh
,
427 unsigned int pad
, u32 which
)
430 case V4L2_SUBDEV_FORMAT_TRY
:
431 return v4l2_subdev_get_try_format(fh
, pad
);
432 case V4L2_SUBDEV_FORMAT_ACTIVE
:
433 return &mt9p031
->format
;
439 static struct v4l2_rect
*
440 __mt9p031_get_pad_crop(struct mt9p031
*mt9p031
, struct v4l2_subdev_fh
*fh
,
441 unsigned int pad
, u32 which
)
444 case V4L2_SUBDEV_FORMAT_TRY
:
445 return v4l2_subdev_get_try_crop(fh
, pad
);
446 case V4L2_SUBDEV_FORMAT_ACTIVE
:
447 return &mt9p031
->crop
;
453 static int mt9p031_get_format(struct v4l2_subdev
*subdev
,
454 struct v4l2_subdev_fh
*fh
,
455 struct v4l2_subdev_format
*fmt
)
457 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
459 fmt
->format
= *__mt9p031_get_pad_format(mt9p031
, fh
, fmt
->pad
,
464 static int mt9p031_set_format(struct v4l2_subdev
*subdev
,
465 struct v4l2_subdev_fh
*fh
,
466 struct v4l2_subdev_format
*format
)
468 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
469 struct v4l2_mbus_framefmt
*__format
;
470 struct v4l2_rect
*__crop
;
476 __crop
= __mt9p031_get_pad_crop(mt9p031
, fh
, format
->pad
,
479 /* Clamp the width and height to avoid dividing by zero. */
480 width
= clamp_t(unsigned int, ALIGN(format
->format
.width
, 2),
481 max(__crop
->width
/ 7, MT9P031_WINDOW_WIDTH_MIN
),
483 height
= clamp_t(unsigned int, ALIGN(format
->format
.height
, 2),
484 max(__crop
->height
/ 8, MT9P031_WINDOW_HEIGHT_MIN
),
487 hratio
= DIV_ROUND_CLOSEST(__crop
->width
, width
);
488 vratio
= DIV_ROUND_CLOSEST(__crop
->height
, height
);
490 __format
= __mt9p031_get_pad_format(mt9p031
, fh
, format
->pad
,
492 __format
->width
= __crop
->width
/ hratio
;
493 __format
->height
= __crop
->height
/ vratio
;
495 format
->format
= *__format
;
500 static int mt9p031_get_crop(struct v4l2_subdev
*subdev
,
501 struct v4l2_subdev_fh
*fh
,
502 struct v4l2_subdev_crop
*crop
)
504 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
506 crop
->rect
= *__mt9p031_get_pad_crop(mt9p031
, fh
, crop
->pad
,
511 static int mt9p031_set_crop(struct v4l2_subdev
*subdev
,
512 struct v4l2_subdev_fh
*fh
,
513 struct v4l2_subdev_crop
*crop
)
515 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
516 struct v4l2_mbus_framefmt
*__format
;
517 struct v4l2_rect
*__crop
;
518 struct v4l2_rect rect
;
520 /* Clamp the crop rectangle boundaries and align them to a multiple of 2
521 * pixels to ensure a GRBG Bayer pattern.
523 rect
.left
= clamp(ALIGN(crop
->rect
.left
, 2), MT9P031_COLUMN_START_MIN
,
524 MT9P031_COLUMN_START_MAX
);
525 rect
.top
= clamp(ALIGN(crop
->rect
.top
, 2), MT9P031_ROW_START_MIN
,
526 MT9P031_ROW_START_MAX
);
527 rect
.width
= clamp(ALIGN(crop
->rect
.width
, 2),
528 MT9P031_WINDOW_WIDTH_MIN
,
529 MT9P031_WINDOW_WIDTH_MAX
);
530 rect
.height
= clamp(ALIGN(crop
->rect
.height
, 2),
531 MT9P031_WINDOW_HEIGHT_MIN
,
532 MT9P031_WINDOW_HEIGHT_MAX
);
534 rect
.width
= min(rect
.width
, MT9P031_PIXEL_ARRAY_WIDTH
- rect
.left
);
535 rect
.height
= min(rect
.height
, MT9P031_PIXEL_ARRAY_HEIGHT
- rect
.top
);
537 __crop
= __mt9p031_get_pad_crop(mt9p031
, fh
, crop
->pad
, crop
->which
);
539 if (rect
.width
!= __crop
->width
|| rect
.height
!= __crop
->height
) {
540 /* Reset the output image size if the crop rectangle size has
543 __format
= __mt9p031_get_pad_format(mt9p031
, fh
, crop
->pad
,
545 __format
->width
= rect
.width
;
546 __format
->height
= rect
.height
;
555 /* -----------------------------------------------------------------------------
556 * V4L2 subdev control operations
559 #define V4L2_CID_TEST_PATTERN (V4L2_CID_USER_BASE | 0x1001)
561 static int mt9p031_s_ctrl(struct v4l2_ctrl
*ctrl
)
563 struct mt9p031
*mt9p031
=
564 container_of(ctrl
->handler
, struct mt9p031
, ctrls
);
565 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9p031
->subdev
);
570 case V4L2_CID_EXPOSURE
:
571 ret
= mt9p031_write(client
, MT9P031_SHUTTER_WIDTH_UPPER
,
572 (ctrl
->val
>> 16) & 0xffff);
576 return mt9p031_write(client
, MT9P031_SHUTTER_WIDTH_LOWER
,
580 /* Gain is controlled by 2 analog stages and a digital stage.
581 * Valid values for the 3 stages are
584 * ------------------------------------------
585 * First analog stage x1 x2 1
586 * Second analog stage x1 x4 0.125
587 * Digital stage x1 x16 0.125
589 * To minimize noise, the gain stages should be used in the
590 * second analog stage, first analog stage, digital stage order.
591 * Gain from a previous stage should be pushed to its maximum
592 * value before the next stage is used.
594 if (ctrl
->val
<= 32) {
596 } else if (ctrl
->val
<= 64) {
598 data
= (1 << 6) | (ctrl
->val
>> 1);
601 data
= ((ctrl
->val
- 64) << 5) | (1 << 6) | 32;
604 return mt9p031_write(client
, MT9P031_GLOBAL_GAIN
, data
);
608 return mt9p031_set_mode2(mt9p031
,
609 0, MT9P031_READ_MODE_2_COL_MIR
);
611 return mt9p031_set_mode2(mt9p031
,
612 MT9P031_READ_MODE_2_COL_MIR
, 0);
616 return mt9p031_set_mode2(mt9p031
,
617 0, MT9P031_READ_MODE_2_ROW_MIR
);
619 return mt9p031_set_mode2(mt9p031
,
620 MT9P031_READ_MODE_2_ROW_MIR
, 0);
622 case V4L2_CID_TEST_PATTERN
:
624 ret
= mt9p031_set_mode2(mt9p031
,
625 0, MT9P031_READ_MODE_2_ROW_BLC
);
629 return mt9p031_write(client
, MT9P031_TEST_PATTERN
,
630 MT9P031_TEST_PATTERN_DISABLE
);
633 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_GREEN
, 0x05a0);
636 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_RED
, 0x0a50);
639 ret
= mt9p031_write(client
, MT9P031_TEST_PATTERN_BLUE
, 0x0aa0);
643 ret
= mt9p031_set_mode2(mt9p031
, MT9P031_READ_MODE_2_ROW_BLC
,
647 ret
= mt9p031_write(client
, MT9P031_ROW_BLACK_DEF_OFFSET
, 0);
651 return mt9p031_write(client
, MT9P031_TEST_PATTERN
,
652 ((ctrl
->val
- 1) << MT9P031_TEST_PATTERN_SHIFT
)
653 | MT9P031_TEST_PATTERN_ENABLE
);
658 static struct v4l2_ctrl_ops mt9p031_ctrl_ops
= {
659 .s_ctrl
= mt9p031_s_ctrl
,
662 static const char * const mt9p031_test_pattern_menu
[] = {
665 "Horizontal Gradient",
668 "Classic Test Pattern",
670 "Monochrome Horizontal Bars",
671 "Monochrome Vertical Bars",
672 "Vertical Color Bars",
675 static const struct v4l2_ctrl_config mt9p031_ctrls
[] = {
677 .ops
= &mt9p031_ctrl_ops
,
678 .id
= V4L2_CID_TEST_PATTERN
,
679 .type
= V4L2_CTRL_TYPE_MENU
,
680 .name
= "Test Pattern",
682 .max
= ARRAY_SIZE(mt9p031_test_pattern_menu
) - 1,
687 .qmenu
= mt9p031_test_pattern_menu
,
691 /* -----------------------------------------------------------------------------
692 * V4L2 subdev core operations
695 static int mt9p031_set_power(struct v4l2_subdev
*subdev
, int on
)
697 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
700 mutex_lock(&mt9p031
->power_lock
);
702 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
703 * update the power state.
705 if (mt9p031
->power_count
== !on
) {
706 ret
= __mt9p031_set_power(mt9p031
, !!on
);
711 /* Update the power count. */
712 mt9p031
->power_count
+= on
? 1 : -1;
713 WARN_ON(mt9p031
->power_count
< 0);
716 mutex_unlock(&mt9p031
->power_lock
);
720 /* -----------------------------------------------------------------------------
721 * V4L2 subdev internal operations
724 static int mt9p031_registered(struct v4l2_subdev
*subdev
)
726 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
727 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
731 ret
= mt9p031_power_on(mt9p031
);
733 dev_err(&client
->dev
, "MT9P031 power up failed\n");
737 /* Read out the chip version register */
738 data
= mt9p031_read(client
, MT9P031_CHIP_VERSION
);
739 if (data
!= MT9P031_CHIP_VERSION_VALUE
) {
740 dev_err(&client
->dev
, "MT9P031 not detected, wrong version "
745 mt9p031_power_off(mt9p031
);
747 dev_info(&client
->dev
, "MT9P031 detected at address 0x%02x\n",
753 static int mt9p031_open(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
)
755 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
756 struct v4l2_mbus_framefmt
*format
;
757 struct v4l2_rect
*crop
;
759 crop
= v4l2_subdev_get_try_crop(fh
, 0);
760 crop
->left
= MT9P031_COLUMN_START_DEF
;
761 crop
->top
= MT9P031_ROW_START_DEF
;
762 crop
->width
= MT9P031_WINDOW_WIDTH_DEF
;
763 crop
->height
= MT9P031_WINDOW_HEIGHT_DEF
;
765 format
= v4l2_subdev_get_try_format(fh
, 0);
767 if (mt9p031
->pdata
->version
== MT9P031_MONOCHROME_VERSION
)
768 format
->code
= V4L2_MBUS_FMT_Y12_1X12
;
770 format
->code
= V4L2_MBUS_FMT_SGRBG12_1X12
;
772 format
->width
= MT9P031_WINDOW_WIDTH_DEF
;
773 format
->height
= MT9P031_WINDOW_HEIGHT_DEF
;
774 format
->field
= V4L2_FIELD_NONE
;
775 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
777 return mt9p031_set_power(subdev
, 1);
780 static int mt9p031_close(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
)
782 return mt9p031_set_power(subdev
, 0);
785 static struct v4l2_subdev_core_ops mt9p031_subdev_core_ops
= {
786 .s_power
= mt9p031_set_power
,
789 static struct v4l2_subdev_video_ops mt9p031_subdev_video_ops
= {
790 .s_stream
= mt9p031_s_stream
,
793 static struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops
= {
794 .enum_mbus_code
= mt9p031_enum_mbus_code
,
795 .enum_frame_size
= mt9p031_enum_frame_size
,
796 .get_fmt
= mt9p031_get_format
,
797 .set_fmt
= mt9p031_set_format
,
798 .get_crop
= mt9p031_get_crop
,
799 .set_crop
= mt9p031_set_crop
,
802 static struct v4l2_subdev_ops mt9p031_subdev_ops
= {
803 .core
= &mt9p031_subdev_core_ops
,
804 .video
= &mt9p031_subdev_video_ops
,
805 .pad
= &mt9p031_subdev_pad_ops
,
808 static const struct v4l2_subdev_internal_ops mt9p031_subdev_internal_ops
= {
809 .registered
= mt9p031_registered
,
810 .open
= mt9p031_open
,
811 .close
= mt9p031_close
,
814 /* -----------------------------------------------------------------------------
815 * Driver initialization and probing
818 static int mt9p031_probe(struct i2c_client
*client
,
819 const struct i2c_device_id
*did
)
821 struct mt9p031_platform_data
*pdata
= client
->dev
.platform_data
;
822 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
823 struct mt9p031
*mt9p031
;
828 dev_err(&client
->dev
, "No platform data\n");
832 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
)) {
833 dev_warn(&client
->dev
,
834 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
838 mt9p031
= kzalloc(sizeof(*mt9p031
), GFP_KERNEL
);
842 mt9p031
->pdata
= pdata
;
843 mt9p031
->output_control
= MT9P031_OUTPUT_CONTROL_DEF
;
844 mt9p031
->mode2
= MT9P031_READ_MODE_2_ROW_BLC
;
846 v4l2_ctrl_handler_init(&mt9p031
->ctrls
, ARRAY_SIZE(mt9p031_ctrls
) + 4);
848 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
849 V4L2_CID_EXPOSURE
, MT9P031_SHUTTER_WIDTH_MIN
,
850 MT9P031_SHUTTER_WIDTH_MAX
, 1,
851 MT9P031_SHUTTER_WIDTH_DEF
);
852 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
853 V4L2_CID_GAIN
, MT9P031_GLOBAL_GAIN_MIN
,
854 MT9P031_GLOBAL_GAIN_MAX
, 1, MT9P031_GLOBAL_GAIN_DEF
);
855 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
856 V4L2_CID_HFLIP
, 0, 1, 1, 0);
857 v4l2_ctrl_new_std(&mt9p031
->ctrls
, &mt9p031_ctrl_ops
,
858 V4L2_CID_VFLIP
, 0, 1, 1, 0);
860 for (i
= 0; i
< ARRAY_SIZE(mt9p031_ctrls
); ++i
)
861 v4l2_ctrl_new_custom(&mt9p031
->ctrls
, &mt9p031_ctrls
[i
], NULL
);
863 mt9p031
->subdev
.ctrl_handler
= &mt9p031
->ctrls
;
865 if (mt9p031
->ctrls
.error
)
866 printk(KERN_INFO
"%s: control initialization error %d\n",
867 __func__
, mt9p031
->ctrls
.error
);
869 mutex_init(&mt9p031
->power_lock
);
870 v4l2_i2c_subdev_init(&mt9p031
->subdev
, client
, &mt9p031_subdev_ops
);
871 mt9p031
->subdev
.internal_ops
= &mt9p031_subdev_internal_ops
;
873 mt9p031
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
874 ret
= media_entity_init(&mt9p031
->subdev
.entity
, 1, &mt9p031
->pad
, 0);
878 mt9p031
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
880 mt9p031
->crop
.width
= MT9P031_WINDOW_WIDTH_DEF
;
881 mt9p031
->crop
.height
= MT9P031_WINDOW_HEIGHT_DEF
;
882 mt9p031
->crop
.left
= MT9P031_COLUMN_START_DEF
;
883 mt9p031
->crop
.top
= MT9P031_ROW_START_DEF
;
885 if (mt9p031
->pdata
->version
== MT9P031_MONOCHROME_VERSION
)
886 mt9p031
->format
.code
= V4L2_MBUS_FMT_Y12_1X12
;
888 mt9p031
->format
.code
= V4L2_MBUS_FMT_SGRBG12_1X12
;
890 mt9p031
->format
.width
= MT9P031_WINDOW_WIDTH_DEF
;
891 mt9p031
->format
.height
= MT9P031_WINDOW_HEIGHT_DEF
;
892 mt9p031
->format
.field
= V4L2_FIELD_NONE
;
893 mt9p031
->format
.colorspace
= V4L2_COLORSPACE_SRGB
;
895 ret
= mt9p031_pll_setup(mt9p031
);
899 v4l2_ctrl_handler_free(&mt9p031
->ctrls
);
900 media_entity_cleanup(&mt9p031
->subdev
.entity
);
907 static int mt9p031_remove(struct i2c_client
*client
)
909 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
910 struct mt9p031
*mt9p031
= to_mt9p031(subdev
);
912 v4l2_ctrl_handler_free(&mt9p031
->ctrls
);
913 v4l2_device_unregister_subdev(subdev
);
914 media_entity_cleanup(&subdev
->entity
);
920 static const struct i2c_device_id mt9p031_id
[] = {
924 MODULE_DEVICE_TABLE(i2c
, mt9p031_id
);
926 static struct i2c_driver mt9p031_i2c_driver
= {
930 .probe
= mt9p031_probe
,
931 .remove
= mt9p031_remove
,
932 .id_table
= mt9p031_id
,
935 module_i2c_driver(mt9p031_i2c_driver
);
937 MODULE_DESCRIPTION("Aptina MT9P031 Camera driver");
938 MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
939 MODULE_LICENSE("GPL v2");