2 * Driver for MT9V022, MT9V024, MT9V032, and MT9V034 CMOS Image Sensors
4 * Copyright (C) 2010, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
6 * Based on the MT9M001 driver,
8 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
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/clk.h>
16 #include <linux/delay.h>
17 #include <linux/i2c.h>
18 #include <linux/log2.h>
19 #include <linux/mutex.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
22 #include <linux/videodev2.h>
23 #include <linux/v4l2-mediabus.h>
24 #include <linux/module.h>
26 #include <media/mt9v032.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-subdev.h>
31 /* The first four rows are black rows. The active area spans 753x481 pixels. */
32 #define MT9V032_PIXEL_ARRAY_HEIGHT 485
33 #define MT9V032_PIXEL_ARRAY_WIDTH 753
35 #define MT9V032_SYSCLK_FREQ_DEF 26600000
37 #define MT9V032_CHIP_VERSION 0x00
38 #define MT9V032_CHIP_ID_REV1 0x1311
39 #define MT9V032_CHIP_ID_REV3 0x1313
40 #define MT9V034_CHIP_ID_REV1 0X1324
41 #define MT9V032_COLUMN_START 0x01
42 #define MT9V032_COLUMN_START_MIN 1
43 #define MT9V032_COLUMN_START_DEF 1
44 #define MT9V032_COLUMN_START_MAX 752
45 #define MT9V032_ROW_START 0x02
46 #define MT9V032_ROW_START_MIN 4
47 #define MT9V032_ROW_START_DEF 5
48 #define MT9V032_ROW_START_MAX 482
49 #define MT9V032_WINDOW_HEIGHT 0x03
50 #define MT9V032_WINDOW_HEIGHT_MIN 1
51 #define MT9V032_WINDOW_HEIGHT_DEF 480
52 #define MT9V032_WINDOW_HEIGHT_MAX 480
53 #define MT9V032_WINDOW_WIDTH 0x04
54 #define MT9V032_WINDOW_WIDTH_MIN 1
55 #define MT9V032_WINDOW_WIDTH_DEF 752
56 #define MT9V032_WINDOW_WIDTH_MAX 752
57 #define MT9V032_HORIZONTAL_BLANKING 0x05
58 #define MT9V032_HORIZONTAL_BLANKING_MIN 43
59 #define MT9V034_HORIZONTAL_BLANKING_MIN 61
60 #define MT9V032_HORIZONTAL_BLANKING_DEF 94
61 #define MT9V032_HORIZONTAL_BLANKING_MAX 1023
62 #define MT9V032_VERTICAL_BLANKING 0x06
63 #define MT9V032_VERTICAL_BLANKING_MIN 4
64 #define MT9V034_VERTICAL_BLANKING_MIN 2
65 #define MT9V032_VERTICAL_BLANKING_DEF 45
66 #define MT9V032_VERTICAL_BLANKING_MAX 3000
67 #define MT9V034_VERTICAL_BLANKING_MAX 32288
68 #define MT9V032_CHIP_CONTROL 0x07
69 #define MT9V032_CHIP_CONTROL_MASTER_MODE (1 << 3)
70 #define MT9V032_CHIP_CONTROL_DOUT_ENABLE (1 << 7)
71 #define MT9V032_CHIP_CONTROL_SEQUENTIAL (1 << 8)
72 #define MT9V032_SHUTTER_WIDTH1 0x08
73 #define MT9V032_SHUTTER_WIDTH2 0x09
74 #define MT9V032_SHUTTER_WIDTH_CONTROL 0x0a
75 #define MT9V032_TOTAL_SHUTTER_WIDTH 0x0b
76 #define MT9V032_TOTAL_SHUTTER_WIDTH_MIN 1
77 #define MT9V034_TOTAL_SHUTTER_WIDTH_MIN 0
78 #define MT9V032_TOTAL_SHUTTER_WIDTH_DEF 480
79 #define MT9V032_TOTAL_SHUTTER_WIDTH_MAX 32767
80 #define MT9V034_TOTAL_SHUTTER_WIDTH_MAX 32765
81 #define MT9V032_RESET 0x0c
82 #define MT9V032_READ_MODE 0x0d
83 #define MT9V032_READ_MODE_ROW_BIN_MASK (3 << 0)
84 #define MT9V032_READ_MODE_ROW_BIN_SHIFT 0
85 #define MT9V032_READ_MODE_COLUMN_BIN_MASK (3 << 2)
86 #define MT9V032_READ_MODE_COLUMN_BIN_SHIFT 2
87 #define MT9V032_READ_MODE_ROW_FLIP (1 << 4)
88 #define MT9V032_READ_MODE_COLUMN_FLIP (1 << 5)
89 #define MT9V032_READ_MODE_DARK_COLUMNS (1 << 6)
90 #define MT9V032_READ_MODE_DARK_ROWS (1 << 7)
91 #define MT9V032_READ_MODE_RESERVED 0x0300
92 #define MT9V032_PIXEL_OPERATION_MODE 0x0f
93 #define MT9V034_PIXEL_OPERATION_MODE_HDR (1 << 0)
94 #define MT9V034_PIXEL_OPERATION_MODE_COLOR (1 << 1)
95 #define MT9V032_PIXEL_OPERATION_MODE_COLOR (1 << 2)
96 #define MT9V032_PIXEL_OPERATION_MODE_HDR (1 << 6)
97 #define MT9V032_ANALOG_GAIN 0x35
98 #define MT9V032_ANALOG_GAIN_MIN 16
99 #define MT9V032_ANALOG_GAIN_DEF 16
100 #define MT9V032_ANALOG_GAIN_MAX 64
101 #define MT9V032_MAX_ANALOG_GAIN 0x36
102 #define MT9V032_MAX_ANALOG_GAIN_MAX 127
103 #define MT9V032_FRAME_DARK_AVERAGE 0x42
104 #define MT9V032_DARK_AVG_THRESH 0x46
105 #define MT9V032_DARK_AVG_LOW_THRESH_MASK (255 << 0)
106 #define MT9V032_DARK_AVG_LOW_THRESH_SHIFT 0
107 #define MT9V032_DARK_AVG_HIGH_THRESH_MASK (255 << 8)
108 #define MT9V032_DARK_AVG_HIGH_THRESH_SHIFT 8
109 #define MT9V032_ROW_NOISE_CORR_CONTROL 0x70
110 #define MT9V034_ROW_NOISE_CORR_ENABLE (1 << 0)
111 #define MT9V034_ROW_NOISE_CORR_USE_BLK_AVG (1 << 1)
112 #define MT9V032_ROW_NOISE_CORR_ENABLE (1 << 5)
113 #define MT9V032_ROW_NOISE_CORR_USE_BLK_AVG (1 << 7)
114 #define MT9V032_PIXEL_CLOCK 0x74
115 #define MT9V034_PIXEL_CLOCK 0x72
116 #define MT9V032_PIXEL_CLOCK_INV_LINE (1 << 0)
117 #define MT9V032_PIXEL_CLOCK_INV_FRAME (1 << 1)
118 #define MT9V032_PIXEL_CLOCK_XOR_LINE (1 << 2)
119 #define MT9V032_PIXEL_CLOCK_CONT_LINE (1 << 3)
120 #define MT9V032_PIXEL_CLOCK_INV_PXL_CLK (1 << 4)
121 #define MT9V032_TEST_PATTERN 0x7f
122 #define MT9V032_TEST_PATTERN_DATA_MASK (1023 << 0)
123 #define MT9V032_TEST_PATTERN_DATA_SHIFT 0
124 #define MT9V032_TEST_PATTERN_USE_DATA (1 << 10)
125 #define MT9V032_TEST_PATTERN_GRAY_MASK (3 << 11)
126 #define MT9V032_TEST_PATTERN_GRAY_NONE (0 << 11)
127 #define MT9V032_TEST_PATTERN_GRAY_VERTICAL (1 << 11)
128 #define MT9V032_TEST_PATTERN_GRAY_HORIZONTAL (2 << 11)
129 #define MT9V032_TEST_PATTERN_GRAY_DIAGONAL (3 << 11)
130 #define MT9V032_TEST_PATTERN_ENABLE (1 << 13)
131 #define MT9V032_TEST_PATTERN_FLIP (1 << 14)
132 #define MT9V032_AEC_AGC_ENABLE 0xaf
133 #define MT9V032_AEC_ENABLE (1 << 0)
134 #define MT9V032_AGC_ENABLE (1 << 1)
135 #define MT9V032_THERMAL_INFO 0xc1
138 MT9V032_MODEL_V022_COLOR
, /* MT9V022IX7ATC */
139 MT9V032_MODEL_V022_MONO
, /* MT9V022IX7ATM */
140 MT9V032_MODEL_V024_COLOR
, /* MT9V024IA7XTC */
141 MT9V032_MODEL_V024_MONO
, /* MT9V024IA7XTM */
142 MT9V032_MODEL_V032_COLOR
, /* MT9V032C12STM */
143 MT9V032_MODEL_V032_MONO
, /* MT9V032C12STC */
144 MT9V032_MODEL_V034_COLOR
,
145 MT9V032_MODEL_V034_MONO
,
148 struct mt9v032_model_version
{
149 unsigned int version
;
153 struct mt9v032_model_data
{
154 unsigned int min_row_time
;
155 unsigned int min_hblank
;
156 unsigned int min_vblank
;
157 unsigned int max_vblank
;
158 unsigned int min_shutter
;
159 unsigned int max_shutter
;
160 unsigned int pclk_reg
;
163 struct mt9v032_model_info
{
164 const struct mt9v032_model_data
*data
;
168 static const struct mt9v032_model_version mt9v032_versions
[] = {
169 { MT9V032_CHIP_ID_REV1
, "MT9V022/MT9V032 rev1/2" },
170 { MT9V032_CHIP_ID_REV3
, "MT9V022/MT9V032 rev3" },
171 { MT9V034_CHIP_ID_REV1
, "MT9V024/MT9V034 rev1" },
174 static const struct mt9v032_model_data mt9v032_model_data
[] = {
176 /* MT9V022, MT9V032 revisions 1/2/3 */
178 .min_hblank
= MT9V032_HORIZONTAL_BLANKING_MIN
,
179 .min_vblank
= MT9V032_VERTICAL_BLANKING_MIN
,
180 .max_vblank
= MT9V032_VERTICAL_BLANKING_MAX
,
181 .min_shutter
= MT9V032_TOTAL_SHUTTER_WIDTH_MIN
,
182 .max_shutter
= MT9V032_TOTAL_SHUTTER_WIDTH_MAX
,
183 .pclk_reg
= MT9V032_PIXEL_CLOCK
,
185 /* MT9V024, MT9V034 */
187 .min_hblank
= MT9V034_HORIZONTAL_BLANKING_MIN
,
188 .min_vblank
= MT9V034_VERTICAL_BLANKING_MIN
,
189 .max_vblank
= MT9V034_VERTICAL_BLANKING_MAX
,
190 .min_shutter
= MT9V034_TOTAL_SHUTTER_WIDTH_MIN
,
191 .max_shutter
= MT9V034_TOTAL_SHUTTER_WIDTH_MAX
,
192 .pclk_reg
= MT9V034_PIXEL_CLOCK
,
196 static const struct mt9v032_model_info mt9v032_models
[] = {
197 [MT9V032_MODEL_V022_COLOR
] = {
198 .data
= &mt9v032_model_data
[0],
201 [MT9V032_MODEL_V022_MONO
] = {
202 .data
= &mt9v032_model_data
[0],
205 [MT9V032_MODEL_V024_COLOR
] = {
206 .data
= &mt9v032_model_data
[1],
209 [MT9V032_MODEL_V024_MONO
] = {
210 .data
= &mt9v032_model_data
[1],
213 [MT9V032_MODEL_V032_COLOR
] = {
214 .data
= &mt9v032_model_data
[0],
217 [MT9V032_MODEL_V032_MONO
] = {
218 .data
= &mt9v032_model_data
[0],
221 [MT9V032_MODEL_V034_COLOR
] = {
222 .data
= &mt9v032_model_data
[1],
225 [MT9V032_MODEL_V034_MONO
] = {
226 .data
= &mt9v032_model_data
[1],
232 struct v4l2_subdev subdev
;
233 struct media_pad pad
;
235 struct v4l2_mbus_framefmt format
;
236 struct v4l2_rect crop
;
240 struct v4l2_ctrl_handler ctrls
;
242 struct v4l2_ctrl
*link_freq
;
243 struct v4l2_ctrl
*pixel_rate
;
246 struct mutex power_lock
;
249 struct regmap
*regmap
;
252 struct mt9v032_platform_data
*pdata
;
253 const struct mt9v032_model_info
*model
;
254 const struct mt9v032_model_version
*version
;
260 struct v4l2_ctrl
*test_pattern
;
261 struct v4l2_ctrl
*test_pattern_color
;
265 static struct mt9v032
*to_mt9v032(struct v4l2_subdev
*sd
)
267 return container_of(sd
, struct mt9v032
, subdev
);
271 mt9v032_update_aec_agc(struct mt9v032
*mt9v032
, u16 which
, int enable
)
273 struct regmap
*map
= mt9v032
->regmap
;
274 u16 value
= mt9v032
->aec_agc
;
282 ret
= regmap_write(map
, MT9V032_AEC_AGC_ENABLE
, value
);
286 mt9v032
->aec_agc
= value
;
291 mt9v032_update_hblank(struct mt9v032
*mt9v032
)
293 struct v4l2_rect
*crop
= &mt9v032
->crop
;
294 unsigned int min_hblank
= mt9v032
->model
->data
->min_hblank
;
297 if (mt9v032
->version
->version
== MT9V034_CHIP_ID_REV1
)
298 min_hblank
+= (mt9v032
->hratio
- 1) * 10;
299 min_hblank
= max_t(int, mt9v032
->model
->data
->min_row_time
- crop
->width
,
301 hblank
= max_t(unsigned int, mt9v032
->hblank
, min_hblank
);
303 return regmap_write(mt9v032
->regmap
, MT9V032_HORIZONTAL_BLANKING
,
307 static int mt9v032_power_on(struct mt9v032
*mt9v032
)
309 struct regmap
*map
= mt9v032
->regmap
;
312 ret
= clk_set_rate(mt9v032
->clk
, mt9v032
->sysclk
);
316 ret
= clk_prepare_enable(mt9v032
->clk
);
322 /* Reset the chip and stop data read out */
323 ret
= regmap_write(map
, MT9V032_RESET
, 1);
327 ret
= regmap_write(map
, MT9V032_RESET
, 0);
331 return regmap_write(map
, MT9V032_CHIP_CONTROL
, 0);
334 static void mt9v032_power_off(struct mt9v032
*mt9v032
)
336 clk_disable_unprepare(mt9v032
->clk
);
339 static int __mt9v032_set_power(struct mt9v032
*mt9v032
, bool on
)
341 struct regmap
*map
= mt9v032
->regmap
;
345 mt9v032_power_off(mt9v032
);
349 ret
= mt9v032_power_on(mt9v032
);
353 /* Configure the pixel clock polarity */
354 if (mt9v032
->pdata
&& mt9v032
->pdata
->clk_pol
) {
355 ret
= regmap_write(map
, mt9v032
->model
->data
->pclk_reg
,
356 MT9V032_PIXEL_CLOCK_INV_PXL_CLK
);
361 /* Disable the noise correction algorithm and restore the controls. */
362 ret
= regmap_write(map
, MT9V032_ROW_NOISE_CORR_CONTROL
, 0);
366 return v4l2_ctrl_handler_setup(&mt9v032
->ctrls
);
369 /* -----------------------------------------------------------------------------
370 * V4L2 subdev video operations
373 static struct v4l2_mbus_framefmt
*
374 __mt9v032_get_pad_format(struct mt9v032
*mt9v032
, struct v4l2_subdev_fh
*fh
,
375 unsigned int pad
, enum v4l2_subdev_format_whence which
)
378 case V4L2_SUBDEV_FORMAT_TRY
:
379 return v4l2_subdev_get_try_format(fh
, pad
);
380 case V4L2_SUBDEV_FORMAT_ACTIVE
:
381 return &mt9v032
->format
;
387 static struct v4l2_rect
*
388 __mt9v032_get_pad_crop(struct mt9v032
*mt9v032
, struct v4l2_subdev_fh
*fh
,
389 unsigned int pad
, enum v4l2_subdev_format_whence which
)
392 case V4L2_SUBDEV_FORMAT_TRY
:
393 return v4l2_subdev_get_try_crop(fh
, pad
);
394 case V4L2_SUBDEV_FORMAT_ACTIVE
:
395 return &mt9v032
->crop
;
401 static int mt9v032_s_stream(struct v4l2_subdev
*subdev
, int enable
)
403 const u16 mode
= MT9V032_CHIP_CONTROL_MASTER_MODE
404 | MT9V032_CHIP_CONTROL_DOUT_ENABLE
405 | MT9V032_CHIP_CONTROL_SEQUENTIAL
;
406 struct mt9v032
*mt9v032
= to_mt9v032(subdev
);
407 struct v4l2_rect
*crop
= &mt9v032
->crop
;
408 struct regmap
*map
= mt9v032
->regmap
;
414 return regmap_update_bits(map
, MT9V032_CHIP_CONTROL
, mode
, 0);
416 /* Configure the window size and row/column bin */
417 hbin
= fls(mt9v032
->hratio
) - 1;
418 vbin
= fls(mt9v032
->vratio
) - 1;
419 ret
= regmap_update_bits(map
, MT9V032_READ_MODE
,
420 ~MT9V032_READ_MODE_RESERVED
,
421 hbin
<< MT9V032_READ_MODE_COLUMN_BIN_SHIFT
|
422 vbin
<< MT9V032_READ_MODE_ROW_BIN_SHIFT
);
426 ret
= regmap_write(map
, MT9V032_COLUMN_START
, crop
->left
);
430 ret
= regmap_write(map
, MT9V032_ROW_START
, crop
->top
);
434 ret
= regmap_write(map
, MT9V032_WINDOW_WIDTH
, crop
->width
);
438 ret
= regmap_write(map
, MT9V032_WINDOW_HEIGHT
, crop
->height
);
442 ret
= mt9v032_update_hblank(mt9v032
);
446 /* Switch to master "normal" mode */
447 return regmap_update_bits(map
, MT9V032_CHIP_CONTROL
, mode
, mode
);
450 static int mt9v032_enum_mbus_code(struct v4l2_subdev
*subdev
,
451 struct v4l2_subdev_fh
*fh
,
452 struct v4l2_subdev_mbus_code_enum
*code
)
457 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
461 static int mt9v032_enum_frame_size(struct v4l2_subdev
*subdev
,
462 struct v4l2_subdev_fh
*fh
,
463 struct v4l2_subdev_frame_size_enum
*fse
)
465 if (fse
->index
>= 3 || fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
468 fse
->min_width
= MT9V032_WINDOW_WIDTH_DEF
/ (1 << fse
->index
);
469 fse
->max_width
= fse
->min_width
;
470 fse
->min_height
= MT9V032_WINDOW_HEIGHT_DEF
/ (1 << fse
->index
);
471 fse
->max_height
= fse
->min_height
;
476 static int mt9v032_get_format(struct v4l2_subdev
*subdev
,
477 struct v4l2_subdev_fh
*fh
,
478 struct v4l2_subdev_format
*format
)
480 struct mt9v032
*mt9v032
= to_mt9v032(subdev
);
482 format
->format
= *__mt9v032_get_pad_format(mt9v032
, fh
, format
->pad
,
487 static void mt9v032_configure_pixel_rate(struct mt9v032
*mt9v032
)
489 struct i2c_client
*client
= v4l2_get_subdevdata(&mt9v032
->subdev
);
492 ret
= v4l2_ctrl_s_ctrl_int64(mt9v032
->pixel_rate
,
493 mt9v032
->sysclk
/ mt9v032
->hratio
);
495 dev_warn(&client
->dev
, "failed to set pixel rate (%d)\n", ret
);
498 static unsigned int mt9v032_calc_ratio(unsigned int input
, unsigned int output
)
500 /* Compute the power-of-two binning factor closest to the input size to
501 * output size ratio. Given that the output size is bounded by input/4
502 * and input, a generic implementation would be an ineffective luxury.
504 if (output
* 3 > input
* 2)
506 if (output
* 3 > input
)
511 static int mt9v032_set_format(struct v4l2_subdev
*subdev
,
512 struct v4l2_subdev_fh
*fh
,
513 struct v4l2_subdev_format
*format
)
515 struct mt9v032
*mt9v032
= to_mt9v032(subdev
);
516 struct v4l2_mbus_framefmt
*__format
;
517 struct v4l2_rect
*__crop
;
523 __crop
= __mt9v032_get_pad_crop(mt9v032
, fh
, format
->pad
,
526 /* Clamp the width and height to avoid dividing by zero. */
527 width
= clamp(ALIGN(format
->format
.width
, 2),
528 max_t(unsigned int, __crop
->width
/ 4,
529 MT9V032_WINDOW_WIDTH_MIN
),
531 height
= clamp(ALIGN(format
->format
.height
, 2),
532 max_t(unsigned int, __crop
->height
/ 4,
533 MT9V032_WINDOW_HEIGHT_MIN
),
536 hratio
= mt9v032_calc_ratio(__crop
->width
, width
);
537 vratio
= mt9v032_calc_ratio(__crop
->height
, height
);
539 __format
= __mt9v032_get_pad_format(mt9v032
, fh
, format
->pad
,
541 __format
->width
= __crop
->width
/ hratio
;
542 __format
->height
= __crop
->height
/ vratio
;
544 if (format
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
545 mt9v032
->hratio
= hratio
;
546 mt9v032
->vratio
= vratio
;
547 mt9v032_configure_pixel_rate(mt9v032
);
550 format
->format
= *__format
;
555 static int mt9v032_get_crop(struct v4l2_subdev
*subdev
,
556 struct v4l2_subdev_fh
*fh
,
557 struct v4l2_subdev_crop
*crop
)
559 struct mt9v032
*mt9v032
= to_mt9v032(subdev
);
561 crop
->rect
= *__mt9v032_get_pad_crop(mt9v032
, fh
, crop
->pad
,
566 static int mt9v032_set_crop(struct v4l2_subdev
*subdev
,
567 struct v4l2_subdev_fh
*fh
,
568 struct v4l2_subdev_crop
*crop
)
570 struct mt9v032
*mt9v032
= to_mt9v032(subdev
);
571 struct v4l2_mbus_framefmt
*__format
;
572 struct v4l2_rect
*__crop
;
573 struct v4l2_rect rect
;
575 /* Clamp the crop rectangle boundaries and align them to a non multiple
576 * of 2 pixels to ensure a GRBG Bayer pattern.
578 rect
.left
= clamp(ALIGN(crop
->rect
.left
+ 1, 2) - 1,
579 MT9V032_COLUMN_START_MIN
,
580 MT9V032_COLUMN_START_MAX
);
581 rect
.top
= clamp(ALIGN(crop
->rect
.top
+ 1, 2) - 1,
582 MT9V032_ROW_START_MIN
,
583 MT9V032_ROW_START_MAX
);
584 rect
.width
= clamp_t(unsigned int, ALIGN(crop
->rect
.width
, 2),
585 MT9V032_WINDOW_WIDTH_MIN
,
586 MT9V032_WINDOW_WIDTH_MAX
);
587 rect
.height
= clamp_t(unsigned int, ALIGN(crop
->rect
.height
, 2),
588 MT9V032_WINDOW_HEIGHT_MIN
,
589 MT9V032_WINDOW_HEIGHT_MAX
);
591 rect
.width
= min_t(unsigned int,
592 rect
.width
, MT9V032_PIXEL_ARRAY_WIDTH
- rect
.left
);
593 rect
.height
= min_t(unsigned int,
594 rect
.height
, MT9V032_PIXEL_ARRAY_HEIGHT
- rect
.top
);
596 __crop
= __mt9v032_get_pad_crop(mt9v032
, fh
, crop
->pad
, crop
->which
);
598 if (rect
.width
!= __crop
->width
|| rect
.height
!= __crop
->height
) {
599 /* Reset the output image size if the crop rectangle size has
602 __format
= __mt9v032_get_pad_format(mt9v032
, fh
, crop
->pad
,
604 __format
->width
= rect
.width
;
605 __format
->height
= rect
.height
;
606 if (crop
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
609 mt9v032_configure_pixel_rate(mt9v032
);
619 /* -----------------------------------------------------------------------------
620 * V4L2 subdev control operations
623 #define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001)
625 static int mt9v032_s_ctrl(struct v4l2_ctrl
*ctrl
)
627 struct mt9v032
*mt9v032
=
628 container_of(ctrl
->handler
, struct mt9v032
, ctrls
);
629 struct regmap
*map
= mt9v032
->regmap
;
634 case V4L2_CID_AUTOGAIN
:
635 return mt9v032_update_aec_agc(mt9v032
, MT9V032_AGC_ENABLE
,
639 return regmap_write(map
, MT9V032_ANALOG_GAIN
, ctrl
->val
);
641 case V4L2_CID_EXPOSURE_AUTO
:
642 return mt9v032_update_aec_agc(mt9v032
, MT9V032_AEC_ENABLE
,
645 case V4L2_CID_EXPOSURE
:
646 return regmap_write(map
, MT9V032_TOTAL_SHUTTER_WIDTH
,
649 case V4L2_CID_HBLANK
:
650 mt9v032
->hblank
= ctrl
->val
;
651 return mt9v032_update_hblank(mt9v032
);
653 case V4L2_CID_VBLANK
:
654 return regmap_write(map
, MT9V032_VERTICAL_BLANKING
,
657 case V4L2_CID_PIXEL_RATE
:
658 case V4L2_CID_LINK_FREQ
:
659 if (mt9v032
->link_freq
== NULL
)
662 freq
= mt9v032
->pdata
->link_freqs
[mt9v032
->link_freq
->val
];
663 *mt9v032
->pixel_rate
->p_new
.p_s64
= freq
;
664 mt9v032
->sysclk
= freq
;
667 case V4L2_CID_TEST_PATTERN
:
668 switch (mt9v032
->test_pattern
->val
) {
673 data
= MT9V032_TEST_PATTERN_GRAY_VERTICAL
674 | MT9V032_TEST_PATTERN_ENABLE
;
677 data
= MT9V032_TEST_PATTERN_GRAY_HORIZONTAL
678 | MT9V032_TEST_PATTERN_ENABLE
;
681 data
= MT9V032_TEST_PATTERN_GRAY_DIAGONAL
682 | MT9V032_TEST_PATTERN_ENABLE
;
685 data
= (mt9v032
->test_pattern_color
->val
<<
686 MT9V032_TEST_PATTERN_DATA_SHIFT
)
687 | MT9V032_TEST_PATTERN_USE_DATA
688 | MT9V032_TEST_PATTERN_ENABLE
689 | MT9V032_TEST_PATTERN_FLIP
;
692 return regmap_write(map
, MT9V032_TEST_PATTERN
, data
);
698 static struct v4l2_ctrl_ops mt9v032_ctrl_ops
= {
699 .s_ctrl
= mt9v032_s_ctrl
,
702 static const char * const mt9v032_test_pattern_menu
[] = {
704 "Gray Vertical Shade",
705 "Gray Horizontal Shade",
706 "Gray Diagonal Shade",
710 static const struct v4l2_ctrl_config mt9v032_test_pattern_color
= {
711 .ops
= &mt9v032_ctrl_ops
,
712 .id
= V4L2_CID_TEST_PATTERN_COLOR
,
713 .type
= V4L2_CTRL_TYPE_INTEGER
,
714 .name
= "Test Pattern Color",
722 /* -----------------------------------------------------------------------------
723 * V4L2 subdev core operations
726 static int mt9v032_set_power(struct v4l2_subdev
*subdev
, int on
)
728 struct mt9v032
*mt9v032
= to_mt9v032(subdev
);
731 mutex_lock(&mt9v032
->power_lock
);
733 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
734 * update the power state.
736 if (mt9v032
->power_count
== !on
) {
737 ret
= __mt9v032_set_power(mt9v032
, !!on
);
742 /* Update the power count. */
743 mt9v032
->power_count
+= on
? 1 : -1;
744 WARN_ON(mt9v032
->power_count
< 0);
747 mutex_unlock(&mt9v032
->power_lock
);
751 /* -----------------------------------------------------------------------------
752 * V4L2 subdev internal operations
755 static int mt9v032_registered(struct v4l2_subdev
*subdev
)
757 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
758 struct mt9v032
*mt9v032
= to_mt9v032(subdev
);
763 dev_info(&client
->dev
, "Probing MT9V032 at address 0x%02x\n",
766 ret
= mt9v032_power_on(mt9v032
);
768 dev_err(&client
->dev
, "MT9V032 power up failed\n");
772 /* Read and check the sensor version */
773 ret
= regmap_read(mt9v032
->regmap
, MT9V032_CHIP_VERSION
, &version
);
775 dev_err(&client
->dev
, "Failed reading chip version\n");
779 for (i
= 0; i
< ARRAY_SIZE(mt9v032_versions
); ++i
) {
780 if (mt9v032_versions
[i
].version
== version
) {
781 mt9v032
->version
= &mt9v032_versions
[i
];
786 if (mt9v032
->version
== NULL
) {
787 dev_err(&client
->dev
, "Unsupported chip version 0x%04x\n",
792 mt9v032_power_off(mt9v032
);
794 dev_info(&client
->dev
, "%s detected at address 0x%02x\n",
795 mt9v032
->version
->name
, client
->addr
);
797 mt9v032_configure_pixel_rate(mt9v032
);
802 static int mt9v032_open(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
)
804 struct mt9v032
*mt9v032
= to_mt9v032(subdev
);
805 struct v4l2_mbus_framefmt
*format
;
806 struct v4l2_rect
*crop
;
808 crop
= v4l2_subdev_get_try_crop(fh
, 0);
809 crop
->left
= MT9V032_COLUMN_START_DEF
;
810 crop
->top
= MT9V032_ROW_START_DEF
;
811 crop
->width
= MT9V032_WINDOW_WIDTH_DEF
;
812 crop
->height
= MT9V032_WINDOW_HEIGHT_DEF
;
814 format
= v4l2_subdev_get_try_format(fh
, 0);
816 if (mt9v032
->model
->color
)
817 format
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
819 format
->code
= MEDIA_BUS_FMT_Y10_1X10
;
821 format
->width
= MT9V032_WINDOW_WIDTH_DEF
;
822 format
->height
= MT9V032_WINDOW_HEIGHT_DEF
;
823 format
->field
= V4L2_FIELD_NONE
;
824 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
826 return mt9v032_set_power(subdev
, 1);
829 static int mt9v032_close(struct v4l2_subdev
*subdev
, struct v4l2_subdev_fh
*fh
)
831 return mt9v032_set_power(subdev
, 0);
834 static struct v4l2_subdev_core_ops mt9v032_subdev_core_ops
= {
835 .s_power
= mt9v032_set_power
,
838 static struct v4l2_subdev_video_ops mt9v032_subdev_video_ops
= {
839 .s_stream
= mt9v032_s_stream
,
842 static struct v4l2_subdev_pad_ops mt9v032_subdev_pad_ops
= {
843 .enum_mbus_code
= mt9v032_enum_mbus_code
,
844 .enum_frame_size
= mt9v032_enum_frame_size
,
845 .get_fmt
= mt9v032_get_format
,
846 .set_fmt
= mt9v032_set_format
,
847 .get_crop
= mt9v032_get_crop
,
848 .set_crop
= mt9v032_set_crop
,
851 static struct v4l2_subdev_ops mt9v032_subdev_ops
= {
852 .core
= &mt9v032_subdev_core_ops
,
853 .video
= &mt9v032_subdev_video_ops
,
854 .pad
= &mt9v032_subdev_pad_ops
,
857 static const struct v4l2_subdev_internal_ops mt9v032_subdev_internal_ops
= {
858 .registered
= mt9v032_registered
,
859 .open
= mt9v032_open
,
860 .close
= mt9v032_close
,
863 static const struct regmap_config mt9v032_regmap_config
= {
866 .max_register
= 0xff,
867 .cache_type
= REGCACHE_RBTREE
,
870 /* -----------------------------------------------------------------------------
871 * Driver initialization and probing
874 static int mt9v032_probe(struct i2c_client
*client
,
875 const struct i2c_device_id
*did
)
877 struct mt9v032_platform_data
*pdata
= client
->dev
.platform_data
;
878 struct mt9v032
*mt9v032
;
882 if (!i2c_check_functionality(client
->adapter
,
883 I2C_FUNC_SMBUS_WORD_DATA
)) {
884 dev_warn(&client
->adapter
->dev
,
885 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
889 mt9v032
= devm_kzalloc(&client
->dev
, sizeof(*mt9v032
), GFP_KERNEL
);
893 mt9v032
->regmap
= devm_regmap_init_i2c(client
, &mt9v032_regmap_config
);
894 if (IS_ERR(mt9v032
->regmap
))
895 return PTR_ERR(mt9v032
->regmap
);
897 mt9v032
->clk
= devm_clk_get(&client
->dev
, NULL
);
898 if (IS_ERR(mt9v032
->clk
))
899 return PTR_ERR(mt9v032
->clk
);
901 mutex_init(&mt9v032
->power_lock
);
902 mt9v032
->pdata
= pdata
;
903 mt9v032
->model
= (const void *)did
->driver_data
;
905 v4l2_ctrl_handler_init(&mt9v032
->ctrls
, 10);
907 v4l2_ctrl_new_std(&mt9v032
->ctrls
, &mt9v032_ctrl_ops
,
908 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
909 v4l2_ctrl_new_std(&mt9v032
->ctrls
, &mt9v032_ctrl_ops
,
910 V4L2_CID_GAIN
, MT9V032_ANALOG_GAIN_MIN
,
911 MT9V032_ANALOG_GAIN_MAX
, 1, MT9V032_ANALOG_GAIN_DEF
);
912 v4l2_ctrl_new_std_menu(&mt9v032
->ctrls
, &mt9v032_ctrl_ops
,
913 V4L2_CID_EXPOSURE_AUTO
, V4L2_EXPOSURE_MANUAL
, 0,
915 v4l2_ctrl_new_std(&mt9v032
->ctrls
, &mt9v032_ctrl_ops
,
916 V4L2_CID_EXPOSURE
, mt9v032
->model
->data
->min_shutter
,
917 mt9v032
->model
->data
->max_shutter
, 1,
918 MT9V032_TOTAL_SHUTTER_WIDTH_DEF
);
919 v4l2_ctrl_new_std(&mt9v032
->ctrls
, &mt9v032_ctrl_ops
,
920 V4L2_CID_HBLANK
, mt9v032
->model
->data
->min_hblank
,
921 MT9V032_HORIZONTAL_BLANKING_MAX
, 1,
922 MT9V032_HORIZONTAL_BLANKING_DEF
);
923 v4l2_ctrl_new_std(&mt9v032
->ctrls
, &mt9v032_ctrl_ops
,
924 V4L2_CID_VBLANK
, mt9v032
->model
->data
->min_vblank
,
925 mt9v032
->model
->data
->max_vblank
, 1,
926 MT9V032_VERTICAL_BLANKING_DEF
);
927 mt9v032
->test_pattern
= v4l2_ctrl_new_std_menu_items(&mt9v032
->ctrls
,
928 &mt9v032_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
929 ARRAY_SIZE(mt9v032_test_pattern_menu
) - 1, 0, 0,
930 mt9v032_test_pattern_menu
);
931 mt9v032
->test_pattern_color
= v4l2_ctrl_new_custom(&mt9v032
->ctrls
,
932 &mt9v032_test_pattern_color
, NULL
);
934 v4l2_ctrl_cluster(2, &mt9v032
->test_pattern
);
936 mt9v032
->pixel_rate
=
937 v4l2_ctrl_new_std(&mt9v032
->ctrls
, &mt9v032_ctrl_ops
,
938 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
940 if (pdata
&& pdata
->link_freqs
) {
941 unsigned int def
= 0;
943 for (i
= 0; pdata
->link_freqs
[i
]; ++i
) {
944 if (pdata
->link_freqs
[i
] == pdata
->link_def_freq
)
949 v4l2_ctrl_new_int_menu(&mt9v032
->ctrls
,
951 V4L2_CID_LINK_FREQ
, i
- 1, def
,
953 v4l2_ctrl_cluster(2, &mt9v032
->link_freq
);
957 mt9v032
->subdev
.ctrl_handler
= &mt9v032
->ctrls
;
959 if (mt9v032
->ctrls
.error
)
960 printk(KERN_INFO
"%s: control initialization error %d\n",
961 __func__
, mt9v032
->ctrls
.error
);
963 mt9v032
->crop
.left
= MT9V032_COLUMN_START_DEF
;
964 mt9v032
->crop
.top
= MT9V032_ROW_START_DEF
;
965 mt9v032
->crop
.width
= MT9V032_WINDOW_WIDTH_DEF
;
966 mt9v032
->crop
.height
= MT9V032_WINDOW_HEIGHT_DEF
;
968 if (mt9v032
->model
->color
)
969 mt9v032
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
971 mt9v032
->format
.code
= MEDIA_BUS_FMT_Y10_1X10
;
973 mt9v032
->format
.width
= MT9V032_WINDOW_WIDTH_DEF
;
974 mt9v032
->format
.height
= MT9V032_WINDOW_HEIGHT_DEF
;
975 mt9v032
->format
.field
= V4L2_FIELD_NONE
;
976 mt9v032
->format
.colorspace
= V4L2_COLORSPACE_SRGB
;
981 mt9v032
->aec_agc
= MT9V032_AEC_ENABLE
| MT9V032_AGC_ENABLE
;
982 mt9v032
->hblank
= MT9V032_HORIZONTAL_BLANKING_DEF
;
983 mt9v032
->sysclk
= MT9V032_SYSCLK_FREQ_DEF
;
985 v4l2_i2c_subdev_init(&mt9v032
->subdev
, client
, &mt9v032_subdev_ops
);
986 mt9v032
->subdev
.internal_ops
= &mt9v032_subdev_internal_ops
;
987 mt9v032
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
989 mt9v032
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
990 ret
= media_entity_init(&mt9v032
->subdev
.entity
, 1, &mt9v032
->pad
, 0);
994 mt9v032
->subdev
.dev
= &client
->dev
;
995 ret
= v4l2_async_register_subdev(&mt9v032
->subdev
);
1002 media_entity_cleanup(&mt9v032
->subdev
.entity
);
1003 v4l2_ctrl_handler_free(&mt9v032
->ctrls
);
1007 static int mt9v032_remove(struct i2c_client
*client
)
1009 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
1010 struct mt9v032
*mt9v032
= to_mt9v032(subdev
);
1012 v4l2_async_unregister_subdev(subdev
);
1013 v4l2_ctrl_handler_free(&mt9v032
->ctrls
);
1014 v4l2_device_unregister_subdev(subdev
);
1015 media_entity_cleanup(&subdev
->entity
);
1020 static const struct i2c_device_id mt9v032_id
[] = {
1021 { "mt9v022", (kernel_ulong_t
)&mt9v032_models
[MT9V032_MODEL_V022_COLOR
] },
1022 { "mt9v022m", (kernel_ulong_t
)&mt9v032_models
[MT9V032_MODEL_V022_MONO
] },
1023 { "mt9v024", (kernel_ulong_t
)&mt9v032_models
[MT9V032_MODEL_V024_COLOR
] },
1024 { "mt9v024m", (kernel_ulong_t
)&mt9v032_models
[MT9V032_MODEL_V024_MONO
] },
1025 { "mt9v032", (kernel_ulong_t
)&mt9v032_models
[MT9V032_MODEL_V032_COLOR
] },
1026 { "mt9v032m", (kernel_ulong_t
)&mt9v032_models
[MT9V032_MODEL_V032_MONO
] },
1027 { "mt9v034", (kernel_ulong_t
)&mt9v032_models
[MT9V032_MODEL_V034_COLOR
] },
1028 { "mt9v034m", (kernel_ulong_t
)&mt9v032_models
[MT9V032_MODEL_V034_MONO
] },
1031 MODULE_DEVICE_TABLE(i2c
, mt9v032_id
);
1033 static struct i2c_driver mt9v032_driver
= {
1037 .probe
= mt9v032_probe
,
1038 .remove
= mt9v032_remove
,
1039 .id_table
= mt9v032_id
,
1042 module_i2c_driver(mt9v032_driver
);
1044 MODULE_DESCRIPTION("Aptina MT9V032 Camera driver");
1045 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1046 MODULE_LICENSE("GPL");