2 * Driver for MT9M032 CMOS Image Sensor from Micron
4 * Copyright (C) 2010-2011 Lund Engineering
5 * Contact: Gil Lund <gwlund@lundeng.com>
6 * Author: Martin Hostettler <martin@neutronstar.dyndns.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 #include <linux/delay.h>
24 #include <linux/i2c.h>
25 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/math64.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/slab.h>
31 #include <linux/v4l2-mediabus.h>
33 #include <media/media-entity.h>
34 #include <media/mt9m032.h>
35 #include <media/v4l2-ctrls.h>
36 #include <media/v4l2-device.h>
37 #include <media/v4l2-subdev.h>
39 #include "aptina-pll.h"
42 * width and height include active boundary and black parts
44 * column 0- 15 active boundary
45 * column 16-1455 image
46 * column 1456-1471 active boundary
47 * column 1472-1599 black
50 * row 53- 59 active boundary
52 * row 1140-1147 active boundary
56 #define MT9M032_PIXEL_ARRAY_WIDTH 1600
57 #define MT9M032_PIXEL_ARRAY_HEIGHT 1152
59 #define MT9M032_CHIP_VERSION 0x00
60 #define MT9M032_CHIP_VERSION_VALUE 0x1402
61 #define MT9M032_ROW_START 0x01
62 #define MT9M032_ROW_START_MIN 0
63 #define MT9M032_ROW_START_MAX 1152
64 #define MT9M032_ROW_START_DEF 60
65 #define MT9M032_COLUMN_START 0x02
66 #define MT9M032_COLUMN_START_MIN 0
67 #define MT9M032_COLUMN_START_MAX 1600
68 #define MT9M032_COLUMN_START_DEF 16
69 #define MT9M032_ROW_SIZE 0x03
70 #define MT9M032_ROW_SIZE_MIN 32
71 #define MT9M032_ROW_SIZE_MAX 1152
72 #define MT9M032_ROW_SIZE_DEF 1080
73 #define MT9M032_COLUMN_SIZE 0x04
74 #define MT9M032_COLUMN_SIZE_MIN 32
75 #define MT9M032_COLUMN_SIZE_MAX 1600
76 #define MT9M032_COLUMN_SIZE_DEF 1440
77 #define MT9M032_HBLANK 0x05
78 #define MT9M032_VBLANK 0x06
79 #define MT9M032_VBLANK_MAX 0x7ff
80 #define MT9M032_SHUTTER_WIDTH_HIGH 0x08
81 #define MT9M032_SHUTTER_WIDTH_LOW 0x09
82 #define MT9M032_SHUTTER_WIDTH_MIN 1
83 #define MT9M032_SHUTTER_WIDTH_MAX 1048575
84 #define MT9M032_SHUTTER_WIDTH_DEF 1943
85 #define MT9M032_PIX_CLK_CTRL 0x0a
86 #define MT9M032_PIX_CLK_CTRL_INV_PIXCLK 0x8000
87 #define MT9M032_RESTART 0x0b
88 #define MT9M032_RESET 0x0d
89 #define MT9M032_PLL_CONFIG1 0x11
90 #define MT9M032_PLL_CONFIG1_OUTDIV_MASK 0x3f
91 #define MT9M032_PLL_CONFIG1_MUL_SHIFT 8
92 #define MT9M032_READ_MODE1 0x1e
93 #define MT9M032_READ_MODE2 0x20
94 #define MT9M032_READ_MODE2_VFLIP_SHIFT 15
95 #define MT9M032_READ_MODE2_HFLIP_SHIFT 14
96 #define MT9M032_READ_MODE2_ROW_BLC 0x40
97 #define MT9M032_GAIN_GREEN1 0x2b
98 #define MT9M032_GAIN_BLUE 0x2c
99 #define MT9M032_GAIN_RED 0x2d
100 #define MT9M032_GAIN_GREEN2 0x2e
103 #define MT9M032_GAIN_ALL 0x35
104 #define MT9M032_GAIN_DIGITAL_MASK 0x7f
105 #define MT9M032_GAIN_DIGITAL_SHIFT 8
106 #define MT9M032_GAIN_AMUL_SHIFT 6
107 #define MT9M032_GAIN_ANALOG_MASK 0x3f
108 #define MT9M032_FORMATTER1 0x9e
109 #define MT9M032_FORMATTER2 0x9f
110 #define MT9M032_FORMATTER2_DOUT_EN 0x1000
111 #define MT9M032_FORMATTER2_PIXCLK_EN 0x2000
114 * The available MT9M032 datasheet is missing documentation for register 0x10
115 * MT9P031 seems to be close enough, so use constants from that datasheet for
117 * But keep the name MT9P031 to remind us, that this isn't really confirmed
120 #define MT9P031_PLL_CONTROL 0x10
121 #define MT9P031_PLL_CONTROL_PWROFF 0x0050
122 #define MT9P031_PLL_CONTROL_PWRON 0x0051
123 #define MT9P031_PLL_CONTROL_USEPLL 0x0052
124 #define MT9P031_PLL_CONFIG2 0x11
125 #define MT9P031_PLL_CONFIG2_P1_DIV_MASK 0x1f
128 struct v4l2_subdev subdev
;
129 struct media_pad pad
;
130 struct mt9m032_platform_data
*pdata
;
132 unsigned int pix_clock
;
134 struct v4l2_ctrl_handler ctrls
;
136 struct v4l2_ctrl
*hflip
;
137 struct v4l2_ctrl
*vflip
;
140 struct mutex lock
; /* Protects streaming, format, interval and crop */
144 struct v4l2_mbus_framefmt format
;
145 struct v4l2_rect crop
;
146 struct v4l2_fract frame_interval
;
149 #define to_mt9m032(sd) container_of(sd, struct mt9m032, subdev)
150 #define to_dev(sensor) \
151 (&((struct i2c_client *)v4l2_get_subdevdata(&(sensor)->subdev))->dev)
153 static int mt9m032_read(struct i2c_client
*client
, u8 reg
)
155 return i2c_smbus_read_word_swapped(client
, reg
);
158 static int mt9m032_write(struct i2c_client
*client
, u8 reg
, const u16 data
)
160 return i2c_smbus_write_word_swapped(client
, reg
, data
);
163 static u32
mt9m032_row_time(struct mt9m032
*sensor
, unsigned int width
)
165 unsigned int effective_width
;
168 effective_width
= width
+ 716; /* empirical value */
169 ns
= div_u64(1000000000ULL * effective_width
, sensor
->pix_clock
);
170 dev_dbg(to_dev(sensor
), "MT9M032 line time: %u ns\n", ns
);
174 static int mt9m032_update_timing(struct mt9m032
*sensor
,
175 struct v4l2_fract
*interval
)
177 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
178 struct v4l2_rect
*crop
= &sensor
->crop
;
179 unsigned int min_vblank
;
184 interval
= &sensor
->frame_interval
;
186 row_time
= mt9m032_row_time(sensor
, crop
->width
);
188 vblank
= div_u64(1000000000ULL * interval
->numerator
,
189 (u64
)row_time
* interval
->denominator
)
192 if (vblank
> MT9M032_VBLANK_MAX
) {
193 /* hardware limits to 11 bit values */
194 interval
->denominator
= 1000;
195 interval
->numerator
=
196 div_u64((crop
->height
+ MT9M032_VBLANK_MAX
) *
197 (u64
)row_time
* interval
->denominator
,
199 vblank
= div_u64(1000000000ULL * interval
->numerator
,
200 (u64
)row_time
* interval
->denominator
)
203 /* enforce minimal 1.6ms blanking time. */
204 min_vblank
= 1600000 / row_time
;
205 vblank
= clamp_t(unsigned int, vblank
, min_vblank
, MT9M032_VBLANK_MAX
);
207 return mt9m032_write(client
, MT9M032_VBLANK
, vblank
);
210 static int mt9m032_update_geom_timing(struct mt9m032
*sensor
)
212 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
215 ret
= mt9m032_write(client
, MT9M032_COLUMN_SIZE
,
216 sensor
->crop
.width
- 1);
218 ret
= mt9m032_write(client
, MT9M032_ROW_SIZE
,
219 sensor
->crop
.height
- 1);
221 ret
= mt9m032_write(client
, MT9M032_COLUMN_START
,
224 ret
= mt9m032_write(client
, MT9M032_ROW_START
,
227 ret
= mt9m032_update_timing(sensor
, NULL
);
231 static int update_formatter2(struct mt9m032
*sensor
, bool streaming
)
233 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
234 u16 reg_val
= MT9M032_FORMATTER2_DOUT_EN
235 | 0x0070; /* parts reserved! */
236 /* possibly for changing to 14-bit mode */
239 reg_val
|= MT9M032_FORMATTER2_PIXCLK_EN
; /* pixclock enable */
241 return mt9m032_write(client
, MT9M032_FORMATTER2
, reg_val
);
244 static int mt9m032_setup_pll(struct mt9m032
*sensor
)
246 static const struct aptina_pll_limits limits
= {
247 .ext_clock_min
= 8000000,
248 .ext_clock_max
= 16500000,
249 .int_clock_min
= 2000000,
250 .int_clock_max
= 24000000,
251 .out_clock_min
= 322000000,
252 .out_clock_max
= 693000000,
253 .pix_clock_max
= 99000000,
262 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
263 struct mt9m032_platform_data
*pdata
= sensor
->pdata
;
264 struct aptina_pll pll
;
267 pll
.ext_clock
= pdata
->ext_clock
;
268 pll
.pix_clock
= pdata
->pix_clock
;
270 ret
= aptina_pll_calculate(&client
->dev
, &limits
, &pll
);
274 sensor
->pix_clock
= pdata
->pix_clock
;
276 ret
= mt9m032_write(client
, MT9M032_PLL_CONFIG1
,
277 (pll
.m
<< MT9M032_PLL_CONFIG1_MUL_SHIFT
)
280 ret
= mt9m032_write(client
, MT9P031_PLL_CONFIG2
, pll
.n
- 1);
282 ret
= mt9m032_write(client
, MT9P031_PLL_CONTROL
,
283 MT9P031_PLL_CONTROL_PWRON
|
284 MT9P031_PLL_CONTROL_USEPLL
);
285 if (!ret
) /* more reserved, Continuous, Master Mode */
286 ret
= mt9m032_write(client
, MT9M032_READ_MODE1
, 0x8006);
287 if (!ret
) /* Set 14-bit mode, select 7 divider */
288 ret
= mt9m032_write(client
, MT9M032_FORMATTER1
, 0x111e);
293 /* -----------------------------------------------------------------------------
294 * Subdev pad operations
297 static int mt9m032_enum_mbus_code(struct v4l2_subdev
*subdev
,
298 struct v4l2_subdev_fh
*fh
,
299 struct v4l2_subdev_mbus_code_enum
*code
)
301 if (code
->index
!= 0)
304 code
->code
= V4L2_MBUS_FMT_Y8_1X8
;
308 static int mt9m032_enum_frame_size(struct v4l2_subdev
*subdev
,
309 struct v4l2_subdev_fh
*fh
,
310 struct v4l2_subdev_frame_size_enum
*fse
)
312 if (fse
->index
!= 0 || fse
->code
!= V4L2_MBUS_FMT_Y8_1X8
)
315 fse
->min_width
= MT9M032_COLUMN_SIZE_DEF
;
316 fse
->max_width
= MT9M032_COLUMN_SIZE_DEF
;
317 fse
->min_height
= MT9M032_ROW_SIZE_DEF
;
318 fse
->max_height
= MT9M032_ROW_SIZE_DEF
;
324 * __mt9m032_get_pad_crop() - get crop rect
325 * @sensor: pointer to the sensor struct
326 * @fh: file handle for getting the try crop rect from
327 * @which: select try or active crop rect
329 * Returns a pointer the current active or fh relative try crop rect
331 static struct v4l2_rect
*
332 __mt9m032_get_pad_crop(struct mt9m032
*sensor
, struct v4l2_subdev_fh
*fh
,
333 enum v4l2_subdev_format_whence which
)
336 case V4L2_SUBDEV_FORMAT_TRY
:
337 return v4l2_subdev_get_try_crop(fh
, 0);
338 case V4L2_SUBDEV_FORMAT_ACTIVE
:
339 return &sensor
->crop
;
346 * __mt9m032_get_pad_format() - get format
347 * @sensor: pointer to the sensor struct
348 * @fh: file handle for getting the try format from
349 * @which: select try or active format
351 * Returns a pointer the current active or fh relative try format
353 static struct v4l2_mbus_framefmt
*
354 __mt9m032_get_pad_format(struct mt9m032
*sensor
, struct v4l2_subdev_fh
*fh
,
355 enum v4l2_subdev_format_whence which
)
358 case V4L2_SUBDEV_FORMAT_TRY
:
359 return v4l2_subdev_get_try_format(fh
, 0);
360 case V4L2_SUBDEV_FORMAT_ACTIVE
:
361 return &sensor
->format
;
367 static int mt9m032_get_pad_format(struct v4l2_subdev
*subdev
,
368 struct v4l2_subdev_fh
*fh
,
369 struct v4l2_subdev_format
*fmt
)
371 struct mt9m032
*sensor
= to_mt9m032(subdev
);
373 mutex_lock(&sensor
->lock
);
374 fmt
->format
= *__mt9m032_get_pad_format(sensor
, fh
, fmt
->which
);
375 mutex_unlock(&sensor
->lock
);
380 static int mt9m032_set_pad_format(struct v4l2_subdev
*subdev
,
381 struct v4l2_subdev_fh
*fh
,
382 struct v4l2_subdev_format
*fmt
)
384 struct mt9m032
*sensor
= to_mt9m032(subdev
);
387 mutex_lock(&sensor
->lock
);
389 if (sensor
->streaming
&& fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
394 /* Scaling is not supported, the format is thus fixed. */
395 fmt
->format
= *__mt9m032_get_pad_format(sensor
, fh
, fmt
->which
);
399 mutex_unlock(&sensor
->lock
);
403 static int mt9m032_get_pad_crop(struct v4l2_subdev
*subdev
,
404 struct v4l2_subdev_fh
*fh
,
405 struct v4l2_subdev_crop
*crop
)
407 struct mt9m032
*sensor
= to_mt9m032(subdev
);
409 mutex_lock(&sensor
->lock
);
410 crop
->rect
= *__mt9m032_get_pad_crop(sensor
, fh
, crop
->which
);
411 mutex_unlock(&sensor
->lock
);
416 static int mt9m032_set_pad_crop(struct v4l2_subdev
*subdev
,
417 struct v4l2_subdev_fh
*fh
,
418 struct v4l2_subdev_crop
*crop
)
420 struct mt9m032
*sensor
= to_mt9m032(subdev
);
421 struct v4l2_mbus_framefmt
*format
;
422 struct v4l2_rect
*__crop
;
423 struct v4l2_rect rect
;
426 mutex_lock(&sensor
->lock
);
428 if (sensor
->streaming
&& crop
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
433 /* Clamp the crop rectangle boundaries and align them to a multiple of 2
434 * pixels to ensure a GRBG Bayer pattern.
436 rect
.left
= clamp(ALIGN(crop
->rect
.left
, 2), MT9M032_COLUMN_START_MIN
,
437 MT9M032_COLUMN_START_MAX
);
438 rect
.top
= clamp(ALIGN(crop
->rect
.top
, 2), MT9M032_ROW_START_MIN
,
439 MT9M032_ROW_START_MAX
);
440 rect
.width
= clamp(ALIGN(crop
->rect
.width
, 2), MT9M032_COLUMN_SIZE_MIN
,
441 MT9M032_COLUMN_SIZE_MAX
);
442 rect
.height
= clamp(ALIGN(crop
->rect
.height
, 2), MT9M032_ROW_SIZE_MIN
,
443 MT9M032_ROW_SIZE_MAX
);
445 rect
.width
= min(rect
.width
, MT9M032_PIXEL_ARRAY_WIDTH
- rect
.left
);
446 rect
.height
= min(rect
.height
, MT9M032_PIXEL_ARRAY_HEIGHT
- rect
.top
);
448 __crop
= __mt9m032_get_pad_crop(sensor
, fh
, crop
->which
);
450 if (rect
.width
!= __crop
->width
|| rect
.height
!= __crop
->height
) {
451 /* Reset the output image size if the crop rectangle size has
454 format
= __mt9m032_get_pad_format(sensor
, fh
, crop
->which
);
455 format
->width
= rect
.width
;
456 format
->height
= rect
.height
;
462 if (crop
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
463 ret
= mt9m032_update_geom_timing(sensor
);
466 mutex_unlock(&sensor
->lock
);
470 static int mt9m032_get_frame_interval(struct v4l2_subdev
*subdev
,
471 struct v4l2_subdev_frame_interval
*fi
)
473 struct mt9m032
*sensor
= to_mt9m032(subdev
);
475 mutex_lock(&sensor
->lock
);
476 memset(fi
, 0, sizeof(*fi
));
477 fi
->interval
= sensor
->frame_interval
;
478 mutex_unlock(&sensor
->lock
);
483 static int mt9m032_set_frame_interval(struct v4l2_subdev
*subdev
,
484 struct v4l2_subdev_frame_interval
*fi
)
486 struct mt9m032
*sensor
= to_mt9m032(subdev
);
489 mutex_lock(&sensor
->lock
);
491 if (sensor
->streaming
) {
496 /* Avoid divisions by 0. */
497 if (fi
->interval
.denominator
== 0)
498 fi
->interval
.denominator
= 1;
500 ret
= mt9m032_update_timing(sensor
, &fi
->interval
);
502 sensor
->frame_interval
= fi
->interval
;
505 mutex_unlock(&sensor
->lock
);
509 static int mt9m032_s_stream(struct v4l2_subdev
*subdev
, int streaming
)
511 struct mt9m032
*sensor
= to_mt9m032(subdev
);
514 mutex_lock(&sensor
->lock
);
515 ret
= update_formatter2(sensor
, streaming
);
517 sensor
->streaming
= streaming
;
518 mutex_unlock(&sensor
->lock
);
523 /* -----------------------------------------------------------------------------
524 * V4L2 subdev core operations
527 #ifdef CONFIG_VIDEO_ADV_DEBUG
528 static int mt9m032_g_register(struct v4l2_subdev
*sd
,
529 struct v4l2_dbg_register
*reg
)
531 struct mt9m032
*sensor
= to_mt9m032(sd
);
532 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
535 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0xff)
537 if (reg
->match
.addr
!= client
->addr
)
540 val
= mt9m032_read(client
, reg
->reg
);
550 static int mt9m032_s_register(struct v4l2_subdev
*sd
,
551 struct v4l2_dbg_register
*reg
)
553 struct mt9m032
*sensor
= to_mt9m032(sd
);
554 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
556 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0xff)
559 if (reg
->match
.addr
!= client
->addr
)
562 return mt9m032_write(client
, reg
->reg
, reg
->val
);
566 /* -----------------------------------------------------------------------------
567 * V4L2 subdev control operations
570 static int update_read_mode2(struct mt9m032
*sensor
, bool vflip
, bool hflip
)
572 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
573 int reg_val
= (vflip
<< MT9M032_READ_MODE2_VFLIP_SHIFT
)
574 | (hflip
<< MT9M032_READ_MODE2_HFLIP_SHIFT
)
575 | MT9M032_READ_MODE2_ROW_BLC
578 return mt9m032_write(client
, MT9M032_READ_MODE2
, reg_val
);
581 static int mt9m032_set_gain(struct mt9m032
*sensor
, s32 val
)
583 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
584 int digital_gain_val
; /* in 1/8th (0..127) */
585 int analog_mul
; /* 0 or 1 */
586 int analog_gain_val
; /* in 1/16th. (0..63) */
589 digital_gain_val
= 51; /* from setup example */
593 analog_gain_val
= val
;
596 analog_gain_val
= val
/ 2;
599 /* a_gain = (1 + analog_mul) + (analog_gain_val + 1) / 16 */
600 /* overall_gain = a_gain * (1 + digital_gain_val / 8) */
602 reg_val
= ((digital_gain_val
& MT9M032_GAIN_DIGITAL_MASK
)
603 << MT9M032_GAIN_DIGITAL_SHIFT
)
604 | ((analog_mul
& 1) << MT9M032_GAIN_AMUL_SHIFT
)
605 | (analog_gain_val
& MT9M032_GAIN_ANALOG_MASK
);
607 return mt9m032_write(client
, MT9M032_GAIN_ALL
, reg_val
);
610 static int mt9m032_try_ctrl(struct v4l2_ctrl
*ctrl
)
612 if (ctrl
->id
== V4L2_CID_GAIN
&& ctrl
->val
>= 63) {
613 /* round because of multiplier used for values >= 63 */
620 static int mt9m032_set_ctrl(struct v4l2_ctrl
*ctrl
)
622 struct mt9m032
*sensor
=
623 container_of(ctrl
->handler
, struct mt9m032
, ctrls
);
624 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->subdev
);
629 return mt9m032_set_gain(sensor
, ctrl
->val
);
632 /* case V4L2_CID_VFLIP: -- In the same cluster */
633 return update_read_mode2(sensor
, sensor
->vflip
->val
,
636 case V4L2_CID_EXPOSURE
:
637 ret
= mt9m032_write(client
, MT9M032_SHUTTER_WIDTH_HIGH
,
638 (ctrl
->val
>> 16) & 0xffff);
642 return mt9m032_write(client
, MT9M032_SHUTTER_WIDTH_LOW
,
649 static struct v4l2_ctrl_ops mt9m032_ctrl_ops
= {
650 .s_ctrl
= mt9m032_set_ctrl
,
651 .try_ctrl
= mt9m032_try_ctrl
,
654 /* -------------------------------------------------------------------------- */
656 static const struct v4l2_subdev_core_ops mt9m032_core_ops
= {
657 #ifdef CONFIG_VIDEO_ADV_DEBUG
658 .g_register
= mt9m032_g_register
,
659 .s_register
= mt9m032_s_register
,
663 static const struct v4l2_subdev_video_ops mt9m032_video_ops
= {
664 .s_stream
= mt9m032_s_stream
,
665 .g_frame_interval
= mt9m032_get_frame_interval
,
666 .s_frame_interval
= mt9m032_set_frame_interval
,
669 static const struct v4l2_subdev_pad_ops mt9m032_pad_ops
= {
670 .enum_mbus_code
= mt9m032_enum_mbus_code
,
671 .enum_frame_size
= mt9m032_enum_frame_size
,
672 .get_fmt
= mt9m032_get_pad_format
,
673 .set_fmt
= mt9m032_set_pad_format
,
674 .set_crop
= mt9m032_set_pad_crop
,
675 .get_crop
= mt9m032_get_pad_crop
,
678 static const struct v4l2_subdev_ops mt9m032_ops
= {
679 .core
= &mt9m032_core_ops
,
680 .video
= &mt9m032_video_ops
,
681 .pad
= &mt9m032_pad_ops
,
684 /* -----------------------------------------------------------------------------
685 * Driver initialization and probing
688 static int mt9m032_probe(struct i2c_client
*client
,
689 const struct i2c_device_id
*devid
)
691 struct mt9m032_platform_data
*pdata
= client
->dev
.platform_data
;
692 struct i2c_adapter
*adapter
= client
->adapter
;
693 struct mt9m032
*sensor
;
698 dev_err(&client
->dev
, "No platform data\n");
702 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
)) {
703 dev_warn(&client
->dev
,
704 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
708 if (!client
->dev
.platform_data
)
711 sensor
= kzalloc(sizeof(*sensor
), GFP_KERNEL
);
715 mutex_init(&sensor
->lock
);
717 sensor
->pdata
= pdata
;
719 v4l2_i2c_subdev_init(&sensor
->subdev
, client
, &mt9m032_ops
);
720 sensor
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
722 chip_version
= mt9m032_read(client
, MT9M032_CHIP_VERSION
);
723 if (chip_version
!= MT9M032_CHIP_VERSION_VALUE
) {
724 dev_err(&client
->dev
, "MT9M032 not detected, wrong version "
725 "0x%04x\n", chip_version
);
730 dev_info(&client
->dev
, "MT9M032 detected at address 0x%02x\n",
733 sensor
->frame_interval
.numerator
= 1;
734 sensor
->frame_interval
.denominator
= 30;
736 sensor
->crop
.left
= MT9M032_COLUMN_START_DEF
;
737 sensor
->crop
.top
= MT9M032_ROW_START_DEF
;
738 sensor
->crop
.width
= MT9M032_COLUMN_SIZE_DEF
;
739 sensor
->crop
.height
= MT9M032_ROW_SIZE_DEF
;
741 sensor
->format
.width
= sensor
->crop
.width
;
742 sensor
->format
.height
= sensor
->crop
.height
;
743 sensor
->format
.code
= V4L2_MBUS_FMT_Y8_1X8
;
744 sensor
->format
.field
= V4L2_FIELD_NONE
;
745 sensor
->format
.colorspace
= V4L2_COLORSPACE_SRGB
;
747 v4l2_ctrl_handler_init(&sensor
->ctrls
, 5);
749 v4l2_ctrl_new_std(&sensor
->ctrls
, &mt9m032_ctrl_ops
,
750 V4L2_CID_GAIN
, 0, 127, 1, 64);
752 sensor
->hflip
= v4l2_ctrl_new_std(&sensor
->ctrls
,
754 V4L2_CID_HFLIP
, 0, 1, 1, 0);
755 sensor
->vflip
= v4l2_ctrl_new_std(&sensor
->ctrls
,
757 V4L2_CID_VFLIP
, 0, 1, 1, 0);
759 v4l2_ctrl_new_std(&sensor
->ctrls
, &mt9m032_ctrl_ops
,
760 V4L2_CID_EXPOSURE
, MT9M032_SHUTTER_WIDTH_MIN
,
761 MT9M032_SHUTTER_WIDTH_MAX
, 1,
762 MT9M032_SHUTTER_WIDTH_DEF
);
763 v4l2_ctrl_new_std(&sensor
->ctrls
, &mt9m032_ctrl_ops
,
764 V4L2_CID_PIXEL_RATE
, pdata
->pix_clock
,
765 pdata
->pix_clock
, 1, pdata
->pix_clock
);
767 if (sensor
->ctrls
.error
) {
768 ret
= sensor
->ctrls
.error
;
769 dev_err(&client
->dev
, "control initialization error %d\n", ret
);
773 v4l2_ctrl_cluster(2, &sensor
->hflip
);
775 sensor
->subdev
.ctrl_handler
= &sensor
->ctrls
;
776 sensor
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
777 ret
= media_entity_init(&sensor
->subdev
.entity
, 1, &sensor
->pad
, 0);
781 ret
= mt9m032_write(client
, MT9M032_RESET
, 1); /* reset on */
784 ret
= mt9m032_write(client
, MT9M032_RESET
, 0); /* reset off */
788 ret
= mt9m032_setup_pll(sensor
);
791 usleep_range(10000, 11000);
793 ret
= v4l2_ctrl_handler_setup(&sensor
->ctrls
);
798 ret
= mt9m032_update_geom_timing(sensor
);
802 ret
= mt9m032_write(client
, 0x41, 0x0000); /* reserved !!! */
805 ret
= mt9m032_write(client
, 0x42, 0x0003); /* reserved !!! */
808 ret
= mt9m032_write(client
, 0x43, 0x0003); /* reserved !!! */
811 ret
= mt9m032_write(client
, 0x7f, 0x0000); /* reserved !!! */
814 if (sensor
->pdata
->invert_pixclock
) {
815 ret
= mt9m032_write(client
, MT9M032_PIX_CLK_CTRL
,
816 MT9M032_PIX_CLK_CTRL_INV_PIXCLK
);
821 ret
= mt9m032_write(client
, MT9M032_RESTART
, 1); /* Restart on */
825 ret
= mt9m032_write(client
, MT9M032_RESTART
, 0); /* Restart off */
829 ret
= update_formatter2(sensor
, false);
836 media_entity_cleanup(&sensor
->subdev
.entity
);
838 v4l2_ctrl_handler_free(&sensor
->ctrls
);
840 mutex_destroy(&sensor
->lock
);
845 static int mt9m032_remove(struct i2c_client
*client
)
847 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
848 struct mt9m032
*sensor
= to_mt9m032(subdev
);
850 v4l2_device_unregister_subdev(subdev
);
851 v4l2_ctrl_handler_free(&sensor
->ctrls
);
852 media_entity_cleanup(&subdev
->entity
);
853 mutex_destroy(&sensor
->lock
);
858 static const struct i2c_device_id mt9m032_id_table
[] = {
863 MODULE_DEVICE_TABLE(i2c
, mt9m032_id_table
);
865 static struct i2c_driver mt9m032_i2c_driver
= {
867 .name
= MT9M032_NAME
,
869 .probe
= mt9m032_probe
,
870 .remove
= mt9m032_remove
,
871 .id_table
= mt9m032_id_table
,
874 module_i2c_driver(mt9m032_i2c_driver
);
876 MODULE_AUTHOR("Martin Hostettler <martin@neutronstar.dyndns.org>");
877 MODULE_DESCRIPTION("MT9M032 camera sensor driver");
878 MODULE_LICENSE("GPL v2");