1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for MT9M032 CMOS Image Sensor from Micron
5 * Copyright (C) 2010-2011 Lund Engineering
6 * Contact: Gil Lund <gwlund@lundeng.com>
7 * Author: Martin Hostettler <martin@neutronstar.dyndns.org>
10 #include <linux/delay.h>
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/math64.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/v4l2-mediabus.h>
20 #include <media/media-entity.h>
21 #include <media/i2c/mt9m032.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-subdev.h>
26 #include "aptina-pll.h"
29 * width and height include active boundary and black parts
31 * column 0- 15 active boundary
32 * column 16-1455 image
33 * column 1456-1471 active boundary
34 * column 1472-1599 black
37 * row 53- 59 active boundary
39 * row 1140-1147 active boundary
43 #define MT9M032_PIXEL_ARRAY_WIDTH 1600
44 #define MT9M032_PIXEL_ARRAY_HEIGHT 1152
46 #define MT9M032_CHIP_VERSION 0x00
47 #define MT9M032_CHIP_VERSION_VALUE 0x1402
48 #define MT9M032_ROW_START 0x01
49 #define MT9M032_ROW_START_MIN 0
50 #define MT9M032_ROW_START_MAX 1152
51 #define MT9M032_ROW_START_DEF 60
52 #define MT9M032_COLUMN_START 0x02
53 #define MT9M032_COLUMN_START_MIN 0
54 #define MT9M032_COLUMN_START_MAX 1600
55 #define MT9M032_COLUMN_START_DEF 16
56 #define MT9M032_ROW_SIZE 0x03
57 #define MT9M032_ROW_SIZE_MIN 32
58 #define MT9M032_ROW_SIZE_MAX 1152
59 #define MT9M032_ROW_SIZE_DEF 1080
60 #define MT9M032_COLUMN_SIZE 0x04
61 #define MT9M032_COLUMN_SIZE_MIN 32
62 #define MT9M032_COLUMN_SIZE_MAX 1600
63 #define MT9M032_COLUMN_SIZE_DEF 1440
64 #define MT9M032_HBLANK 0x05
65 #define MT9M032_VBLANK 0x06
66 #define MT9M032_VBLANK_MAX 0x7ff
67 #define MT9M032_SHUTTER_WIDTH_HIGH 0x08
68 #define MT9M032_SHUTTER_WIDTH_LOW 0x09
69 #define MT9M032_SHUTTER_WIDTH_MIN 1
70 #define MT9M032_SHUTTER_WIDTH_MAX 1048575
71 #define MT9M032_SHUTTER_WIDTH_DEF 1943
72 #define MT9M032_PIX_CLK_CTRL 0x0a
73 #define MT9M032_PIX_CLK_CTRL_INV_PIXCLK 0x8000
74 #define MT9M032_RESTART 0x0b
75 #define MT9M032_RESET 0x0d
76 #define MT9M032_PLL_CONFIG1 0x11
77 #define MT9M032_PLL_CONFIG1_PREDIV_MASK 0x3f
78 #define MT9M032_PLL_CONFIG1_MUL_SHIFT 8
79 #define MT9M032_READ_MODE1 0x1e
80 #define MT9M032_READ_MODE1_OUTPUT_BAD_FRAMES (1 << 13)
81 #define MT9M032_READ_MODE1_MAINTAIN_FRAME_RATE (1 << 12)
82 #define MT9M032_READ_MODE1_XOR_LINE_VALID (1 << 11)
83 #define MT9M032_READ_MODE1_CONT_LINE_VALID (1 << 10)
84 #define MT9M032_READ_MODE1_INVERT_TRIGGER (1 << 9)
85 #define MT9M032_READ_MODE1_SNAPSHOT (1 << 8)
86 #define MT9M032_READ_MODE1_GLOBAL_RESET (1 << 7)
87 #define MT9M032_READ_MODE1_BULB_EXPOSURE (1 << 6)
88 #define MT9M032_READ_MODE1_INVERT_STROBE (1 << 5)
89 #define MT9M032_READ_MODE1_STROBE_ENABLE (1 << 4)
90 #define MT9M032_READ_MODE1_STROBE_START_TRIG1 (0 << 2)
91 #define MT9M032_READ_MODE1_STROBE_START_EXP (1 << 2)
92 #define MT9M032_READ_MODE1_STROBE_START_SHUTTER (2 << 2)
93 #define MT9M032_READ_MODE1_STROBE_START_TRIG2 (3 << 2)
94 #define MT9M032_READ_MODE1_STROBE_END_TRIG1 (0 << 0)
95 #define MT9M032_READ_MODE1_STROBE_END_EXP (1 << 0)
96 #define MT9M032_READ_MODE1_STROBE_END_SHUTTER (2 << 0)
97 #define MT9M032_READ_MODE1_STROBE_END_TRIG2 (3 << 0)
98 #define MT9M032_READ_MODE2 0x20
99 #define MT9M032_READ_MODE2_VFLIP_SHIFT 15
100 #define MT9M032_READ_MODE2_HFLIP_SHIFT 14
101 #define MT9M032_READ_MODE2_ROW_BLC 0x40
102 #define MT9M032_GAIN_GREEN1 0x2b
103 #define MT9M032_GAIN_BLUE 0x2c
104 #define MT9M032_GAIN_RED 0x2d
105 #define MT9M032_GAIN_GREEN2 0x2e
108 #define MT9M032_GAIN_ALL 0x35
109 #define MT9M032_GAIN_DIGITAL_MASK 0x7f
110 #define MT9M032_GAIN_DIGITAL_SHIFT 8
111 #define MT9M032_GAIN_AMUL_SHIFT 6
112 #define MT9M032_GAIN_ANALOG_MASK 0x3f
113 #define MT9M032_FORMATTER1 0x9e
114 #define MT9M032_FORMATTER1_PLL_P1_6 (1 << 8)
115 #define MT9M032_FORMATTER1_PARALLEL (1 << 12)
116 #define MT9M032_FORMATTER2 0x9f
117 #define MT9M032_FORMATTER2_DOUT_EN 0x1000
118 #define MT9M032_FORMATTER2_PIXCLK_EN 0x2000
121 * The available MT9M032 datasheet is missing documentation for register 0x10
122 * MT9P031 seems to be close enough, so use constants from that datasheet for
124 * But keep the name MT9P031 to remind us, that this isn't really confirmed
127 #define MT9P031_PLL_CONTROL 0x10
128 #define MT9P031_PLL_CONTROL_PWROFF 0x0050
129 #define MT9P031_PLL_CONTROL_PWRON 0x0051
130 #define MT9P031_PLL_CONTROL_USEPLL 0x0052
133 struct v4l2_subdev subdev;
134 struct media_pad pad;
135 struct mt9m032_platform_data *pdata;
137 unsigned int pix_clock;
139 struct v4l2_ctrl_handler ctrls;
141 struct v4l2_ctrl *hflip;
142 struct v4l2_ctrl *vflip;
145 struct mutex lock; /* Protects streaming, format, interval and crop */
149 struct v4l2_mbus_framefmt format;
150 struct v4l2_rect crop;
151 struct v4l2_fract frame_interval;
154 #define to_mt9m032(sd) container_of(sd, struct mt9m032, subdev)
155 #define to_dev(sensor) \
156 (&((struct i2c_client *)v4l2_get_subdevdata(&(sensor)->subdev))->dev)
158 static int mt9m032_read(struct i2c_client *client, u8 reg)
160 return i2c_smbus_read_word_swapped(client, reg);
163 static int mt9m032_write(struct i2c_client *client, u8 reg, const u16 data)
165 return i2c_smbus_write_word_swapped(client, reg, data);
168 static u32 mt9m032_row_time(struct mt9m032 *sensor, unsigned int width)
170 unsigned int effective_width;
173 effective_width = width + 716; /* empirical value */
174 ns = div_u64(1000000000ULL * effective_width, sensor->pix_clock);
175 dev_dbg(to_dev(sensor), "MT9M032 line time: %u ns\n", ns);
179 static int mt9m032_update_timing(struct mt9m032 *sensor,
180 struct v4l2_fract *interval)
182 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
183 struct v4l2_rect *crop = &sensor->crop;
184 unsigned int min_vblank;
189 interval = &sensor->frame_interval;
191 row_time = mt9m032_row_time(sensor, crop->width);
193 vblank = div_u64(1000000000ULL * interval->numerator,
194 (u64)row_time * interval->denominator)
197 if (vblank > MT9M032_VBLANK_MAX) {
198 /* hardware limits to 11 bit values */
199 interval->denominator = 1000;
200 interval->numerator =
201 div_u64((crop->height + MT9M032_VBLANK_MAX) *
202 (u64)row_time * interval->denominator,
204 vblank = div_u64(1000000000ULL * interval->numerator,
205 (u64)row_time * interval->denominator)
208 /* enforce minimal 1.6ms blanking time. */
209 min_vblank = 1600000 / row_time;
210 vblank = clamp_t(unsigned int, vblank, min_vblank, MT9M032_VBLANK_MAX);
212 return mt9m032_write(client, MT9M032_VBLANK, vblank);
215 static int mt9m032_update_geom_timing(struct mt9m032 *sensor)
217 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
220 ret = mt9m032_write(client, MT9M032_COLUMN_SIZE,
221 sensor->crop.width - 1);
223 ret = mt9m032_write(client, MT9M032_ROW_SIZE,
224 sensor->crop.height - 1);
226 ret = mt9m032_write(client, MT9M032_COLUMN_START,
229 ret = mt9m032_write(client, MT9M032_ROW_START,
232 ret = mt9m032_update_timing(sensor, NULL);
236 static int update_formatter2(struct mt9m032 *sensor, bool streaming)
238 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
239 u16 reg_val = MT9M032_FORMATTER2_DOUT_EN
240 | 0x0070; /* parts reserved! */
241 /* possibly for changing to 14-bit mode */
244 reg_val |= MT9M032_FORMATTER2_PIXCLK_EN; /* pixclock enable */
246 return mt9m032_write(client, MT9M032_FORMATTER2, reg_val);
249 static int mt9m032_setup_pll(struct mt9m032 *sensor)
251 static const struct aptina_pll_limits limits = {
252 .ext_clock_min = 8000000,
253 .ext_clock_max = 16500000,
254 .int_clock_min = 2000000,
255 .int_clock_max = 24000000,
256 .out_clock_min = 322000000,
257 .out_clock_max = 693000000,
258 .pix_clock_max = 99000000,
267 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
268 struct mt9m032_platform_data *pdata = sensor->pdata;
269 struct aptina_pll pll;
273 pll.ext_clock = pdata->ext_clock;
274 pll.pix_clock = pdata->pix_clock;
276 ret = aptina_pll_calculate(&client->dev, &limits, &pll);
280 sensor->pix_clock = pdata->pix_clock;
282 ret = mt9m032_write(client, MT9M032_PLL_CONFIG1,
283 (pll.m << MT9M032_PLL_CONFIG1_MUL_SHIFT) |
284 ((pll.n - 1) & MT9M032_PLL_CONFIG1_PREDIV_MASK));
286 ret = mt9m032_write(client, MT9P031_PLL_CONTROL,
287 MT9P031_PLL_CONTROL_PWRON |
288 MT9P031_PLL_CONTROL_USEPLL);
289 if (!ret) /* more reserved, Continuous, Master Mode */
290 ret = mt9m032_write(client, MT9M032_READ_MODE1, 0x8000 |
291 MT9M032_READ_MODE1_STROBE_START_EXP |
292 MT9M032_READ_MODE1_STROBE_END_SHUTTER);
294 reg_val = (pll.p1 == 6 ? MT9M032_FORMATTER1_PLL_P1_6 : 0)
295 | MT9M032_FORMATTER1_PARALLEL | 0x001e; /* 14-bit */
296 ret = mt9m032_write(client, MT9M032_FORMATTER1, reg_val);
302 /* -----------------------------------------------------------------------------
303 * Subdev pad operations
306 static int mt9m032_enum_mbus_code(struct v4l2_subdev *subdev,
307 struct v4l2_subdev_state *sd_state,
308 struct v4l2_subdev_mbus_code_enum *code)
310 if (code->index != 0)
313 code->code = MEDIA_BUS_FMT_Y8_1X8;
317 static int mt9m032_enum_frame_size(struct v4l2_subdev *subdev,
318 struct v4l2_subdev_state *sd_state,
319 struct v4l2_subdev_frame_size_enum *fse)
321 if (fse->index != 0 || fse->code != MEDIA_BUS_FMT_Y8_1X8)
324 fse->min_width = MT9M032_COLUMN_SIZE_DEF;
325 fse->max_width = MT9M032_COLUMN_SIZE_DEF;
326 fse->min_height = MT9M032_ROW_SIZE_DEF;
327 fse->max_height = MT9M032_ROW_SIZE_DEF;
333 * __mt9m032_get_pad_crop() - get crop rect
334 * @sensor: pointer to the sensor struct
335 * @sd_state: v4l2_subdev_state for getting the try crop rect from
336 * @which: select try or active crop rect
338 * Returns a pointer the current active or fh relative try crop rect
340 static struct v4l2_rect *
341 __mt9m032_get_pad_crop(struct mt9m032 *sensor,
342 struct v4l2_subdev_state *sd_state,
343 enum v4l2_subdev_format_whence which)
346 case V4L2_SUBDEV_FORMAT_TRY:
347 return v4l2_subdev_get_try_crop(&sensor->subdev, sd_state, 0);
348 case V4L2_SUBDEV_FORMAT_ACTIVE:
349 return &sensor->crop;
356 * __mt9m032_get_pad_format() - get format
357 * @sensor: pointer to the sensor struct
358 * @sd_state: v4l2_subdev_state for getting the try format from
359 * @which: select try or active format
361 * Returns a pointer the current active or fh relative try format
363 static struct v4l2_mbus_framefmt *
364 __mt9m032_get_pad_format(struct mt9m032 *sensor,
365 struct v4l2_subdev_state *sd_state,
366 enum v4l2_subdev_format_whence which)
369 case V4L2_SUBDEV_FORMAT_TRY:
370 return v4l2_subdev_get_try_format(&sensor->subdev, sd_state,
372 case V4L2_SUBDEV_FORMAT_ACTIVE:
373 return &sensor->format;
379 static int mt9m032_get_pad_format(struct v4l2_subdev *subdev,
380 struct v4l2_subdev_state *sd_state,
381 struct v4l2_subdev_format *fmt)
383 struct mt9m032 *sensor = to_mt9m032(subdev);
385 mutex_lock(&sensor->lock);
386 fmt->format = *__mt9m032_get_pad_format(sensor, sd_state, fmt->which);
387 mutex_unlock(&sensor->lock);
392 static int mt9m032_set_pad_format(struct v4l2_subdev *subdev,
393 struct v4l2_subdev_state *sd_state,
394 struct v4l2_subdev_format *fmt)
396 struct mt9m032 *sensor = to_mt9m032(subdev);
399 mutex_lock(&sensor->lock);
401 if (sensor->streaming && fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
406 /* Scaling is not supported, the format is thus fixed. */
407 fmt->format = *__mt9m032_get_pad_format(sensor, sd_state, fmt->which);
411 mutex_unlock(&sensor->lock);
415 static int mt9m032_get_pad_selection(struct v4l2_subdev *subdev,
416 struct v4l2_subdev_state *sd_state,
417 struct v4l2_subdev_selection *sel)
419 struct mt9m032 *sensor = to_mt9m032(subdev);
421 if (sel->target != V4L2_SEL_TGT_CROP)
424 mutex_lock(&sensor->lock);
425 sel->r = *__mt9m032_get_pad_crop(sensor, sd_state, sel->which);
426 mutex_unlock(&sensor->lock);
431 static int mt9m032_set_pad_selection(struct v4l2_subdev *subdev,
432 struct v4l2_subdev_state *sd_state,
433 struct v4l2_subdev_selection *sel)
435 struct mt9m032 *sensor = to_mt9m032(subdev);
436 struct v4l2_mbus_framefmt *format;
437 struct v4l2_rect *__crop;
438 struct v4l2_rect rect;
441 if (sel->target != V4L2_SEL_TGT_CROP)
444 mutex_lock(&sensor->lock);
446 if (sensor->streaming && sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
451 /* Clamp the crop rectangle boundaries and align them to a multiple of 2
452 * pixels to ensure a GRBG Bayer pattern.
454 rect.left = clamp(ALIGN(sel->r.left, 2), MT9M032_COLUMN_START_MIN,
455 MT9M032_COLUMN_START_MAX);
456 rect.top = clamp(ALIGN(sel->r.top, 2), MT9M032_ROW_START_MIN,
457 MT9M032_ROW_START_MAX);
458 rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
459 MT9M032_COLUMN_SIZE_MIN, MT9M032_COLUMN_SIZE_MAX);
460 rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
461 MT9M032_ROW_SIZE_MIN, MT9M032_ROW_SIZE_MAX);
463 rect.width = min_t(unsigned int, rect.width,
464 MT9M032_PIXEL_ARRAY_WIDTH - rect.left);
465 rect.height = min_t(unsigned int, rect.height,
466 MT9M032_PIXEL_ARRAY_HEIGHT - rect.top);
468 __crop = __mt9m032_get_pad_crop(sensor, sd_state, sel->which);
470 if (rect.width != __crop->width || rect.height != __crop->height) {
471 /* Reset the output image size if the crop rectangle size has
474 format = __mt9m032_get_pad_format(sensor, sd_state,
476 format->width = rect.width;
477 format->height = rect.height;
483 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
484 ret = mt9m032_update_geom_timing(sensor);
487 mutex_unlock(&sensor->lock);
491 static int mt9m032_get_frame_interval(struct v4l2_subdev *subdev,
492 struct v4l2_subdev_frame_interval *fi)
494 struct mt9m032 *sensor = to_mt9m032(subdev);
496 mutex_lock(&sensor->lock);
497 memset(fi, 0, sizeof(*fi));
498 fi->interval = sensor->frame_interval;
499 mutex_unlock(&sensor->lock);
504 static int mt9m032_set_frame_interval(struct v4l2_subdev *subdev,
505 struct v4l2_subdev_frame_interval *fi)
507 struct mt9m032 *sensor = to_mt9m032(subdev);
510 mutex_lock(&sensor->lock);
512 if (sensor->streaming) {
517 /* Avoid divisions by 0. */
518 if (fi->interval.denominator == 0)
519 fi->interval.denominator = 1;
521 ret = mt9m032_update_timing(sensor, &fi->interval);
523 sensor->frame_interval = fi->interval;
526 mutex_unlock(&sensor->lock);
530 static int mt9m032_s_stream(struct v4l2_subdev *subdev, int streaming)
532 struct mt9m032 *sensor = to_mt9m032(subdev);
535 mutex_lock(&sensor->lock);
536 ret = update_formatter2(sensor, streaming);
538 sensor->streaming = streaming;
539 mutex_unlock(&sensor->lock);
544 /* -----------------------------------------------------------------------------
545 * V4L2 subdev core operations
548 #ifdef CONFIG_VIDEO_ADV_DEBUG
549 static int mt9m032_g_register(struct v4l2_subdev *sd,
550 struct v4l2_dbg_register *reg)
552 struct mt9m032 *sensor = to_mt9m032(sd);
553 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
559 val = mt9m032_read(client, reg->reg);
569 static int mt9m032_s_register(struct v4l2_subdev *sd,
570 const struct v4l2_dbg_register *reg)
572 struct mt9m032 *sensor = to_mt9m032(sd);
573 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
578 return mt9m032_write(client, reg->reg, reg->val);
582 /* -----------------------------------------------------------------------------
583 * V4L2 subdev control operations
586 static int update_read_mode2(struct mt9m032 *sensor, bool vflip, bool hflip)
588 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
589 int reg_val = (vflip << MT9M032_READ_MODE2_VFLIP_SHIFT)
590 | (hflip << MT9M032_READ_MODE2_HFLIP_SHIFT)
591 | MT9M032_READ_MODE2_ROW_BLC
594 return mt9m032_write(client, MT9M032_READ_MODE2, reg_val);
597 static int mt9m032_set_gain(struct mt9m032 *sensor, s32 val)
599 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
600 int digital_gain_val; /* in 1/8th (0..127) */
601 int analog_mul; /* 0 or 1 */
602 int analog_gain_val; /* in 1/16th. (0..63) */
605 digital_gain_val = 51; /* from setup example */
609 analog_gain_val = val;
612 analog_gain_val = val / 2;
615 /* a_gain = (1 + analog_mul) + (analog_gain_val + 1) / 16 */
616 /* overall_gain = a_gain * (1 + digital_gain_val / 8) */
618 reg_val = ((digital_gain_val & MT9M032_GAIN_DIGITAL_MASK)
619 << MT9M032_GAIN_DIGITAL_SHIFT)
620 | ((analog_mul & 1) << MT9M032_GAIN_AMUL_SHIFT)
621 | (analog_gain_val & MT9M032_GAIN_ANALOG_MASK);
623 return mt9m032_write(client, MT9M032_GAIN_ALL, reg_val);
626 static int mt9m032_try_ctrl(struct v4l2_ctrl *ctrl)
628 if (ctrl->id == V4L2_CID_GAIN && ctrl->val >= 63) {
629 /* round because of multiplier used for values >= 63 */
636 static int mt9m032_set_ctrl(struct v4l2_ctrl *ctrl)
638 struct mt9m032 *sensor =
639 container_of(ctrl->handler, struct mt9m032, ctrls);
640 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
645 return mt9m032_set_gain(sensor, ctrl->val);
648 /* case V4L2_CID_VFLIP: -- In the same cluster */
649 return update_read_mode2(sensor, sensor->vflip->val,
652 case V4L2_CID_EXPOSURE:
653 ret = mt9m032_write(client, MT9M032_SHUTTER_WIDTH_HIGH,
654 (ctrl->val >> 16) & 0xffff);
658 return mt9m032_write(client, MT9M032_SHUTTER_WIDTH_LOW,
665 static const struct v4l2_ctrl_ops mt9m032_ctrl_ops = {
666 .s_ctrl = mt9m032_set_ctrl,
667 .try_ctrl = mt9m032_try_ctrl,
670 /* -------------------------------------------------------------------------- */
672 static const struct v4l2_subdev_core_ops mt9m032_core_ops = {
673 #ifdef CONFIG_VIDEO_ADV_DEBUG
674 .g_register = mt9m032_g_register,
675 .s_register = mt9m032_s_register,
679 static const struct v4l2_subdev_video_ops mt9m032_video_ops = {
680 .s_stream = mt9m032_s_stream,
681 .g_frame_interval = mt9m032_get_frame_interval,
682 .s_frame_interval = mt9m032_set_frame_interval,
685 static const struct v4l2_subdev_pad_ops mt9m032_pad_ops = {
686 .enum_mbus_code = mt9m032_enum_mbus_code,
687 .enum_frame_size = mt9m032_enum_frame_size,
688 .get_fmt = mt9m032_get_pad_format,
689 .set_fmt = mt9m032_set_pad_format,
690 .set_selection = mt9m032_set_pad_selection,
691 .get_selection = mt9m032_get_pad_selection,
694 static const struct v4l2_subdev_ops mt9m032_ops = {
695 .core = &mt9m032_core_ops,
696 .video = &mt9m032_video_ops,
697 .pad = &mt9m032_pad_ops,
700 /* -----------------------------------------------------------------------------
701 * Driver initialization and probing
704 static int mt9m032_probe(struct i2c_client *client,
705 const struct i2c_device_id *devid)
707 struct mt9m032_platform_data *pdata = client->dev.platform_data;
708 struct i2c_adapter *adapter = client->adapter;
709 struct mt9m032 *sensor;
714 dev_err(&client->dev, "No platform data\n");
718 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
719 dev_warn(&client->dev,
720 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
724 if (!client->dev.platform_data)
727 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
731 mutex_init(&sensor->lock);
733 sensor->pdata = pdata;
735 v4l2_i2c_subdev_init(&sensor->subdev, client, &mt9m032_ops);
736 sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
738 chip_version = mt9m032_read(client, MT9M032_CHIP_VERSION);
739 if (chip_version != MT9M032_CHIP_VERSION_VALUE) {
740 dev_err(&client->dev, "MT9M032 not detected, wrong version "
741 "0x%04x\n", chip_version);
746 dev_info(&client->dev, "MT9M032 detected at address 0x%02x\n",
749 sensor->frame_interval.numerator = 1;
750 sensor->frame_interval.denominator = 30;
752 sensor->crop.left = MT9M032_COLUMN_START_DEF;
753 sensor->crop.top = MT9M032_ROW_START_DEF;
754 sensor->crop.width = MT9M032_COLUMN_SIZE_DEF;
755 sensor->crop.height = MT9M032_ROW_SIZE_DEF;
757 sensor->format.width = sensor->crop.width;
758 sensor->format.height = sensor->crop.height;
759 sensor->format.code = MEDIA_BUS_FMT_Y8_1X8;
760 sensor->format.field = V4L2_FIELD_NONE;
761 sensor->format.colorspace = V4L2_COLORSPACE_SRGB;
763 v4l2_ctrl_handler_init(&sensor->ctrls, 5);
765 v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
766 V4L2_CID_GAIN, 0, 127, 1, 64);
768 sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls,
770 V4L2_CID_HFLIP, 0, 1, 1, 0);
771 sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls,
773 V4L2_CID_VFLIP, 0, 1, 1, 0);
775 v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
776 V4L2_CID_EXPOSURE, MT9M032_SHUTTER_WIDTH_MIN,
777 MT9M032_SHUTTER_WIDTH_MAX, 1,
778 MT9M032_SHUTTER_WIDTH_DEF);
779 v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
780 V4L2_CID_PIXEL_RATE, pdata->pix_clock,
781 pdata->pix_clock, 1, pdata->pix_clock);
783 if (sensor->ctrls.error) {
784 ret = sensor->ctrls.error;
785 dev_err(&client->dev, "control initialization error %d\n", ret);
789 v4l2_ctrl_cluster(2, &sensor->hflip);
791 sensor->subdev.ctrl_handler = &sensor->ctrls;
792 sensor->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
793 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
794 ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad);
798 ret = mt9m032_write(client, MT9M032_RESET, 1); /* reset on */
801 ret = mt9m032_write(client, MT9M032_RESET, 0); /* reset off */
805 ret = mt9m032_setup_pll(sensor);
808 usleep_range(10000, 11000);
810 ret = v4l2_ctrl_handler_setup(&sensor->ctrls);
815 ret = mt9m032_update_geom_timing(sensor);
819 ret = mt9m032_write(client, 0x41, 0x0000); /* reserved !!! */
822 ret = mt9m032_write(client, 0x42, 0x0003); /* reserved !!! */
825 ret = mt9m032_write(client, 0x43, 0x0003); /* reserved !!! */
828 ret = mt9m032_write(client, 0x7f, 0x0000); /* reserved !!! */
831 if (sensor->pdata->invert_pixclock) {
832 ret = mt9m032_write(client, MT9M032_PIX_CLK_CTRL,
833 MT9M032_PIX_CLK_CTRL_INV_PIXCLK);
838 ret = mt9m032_write(client, MT9M032_RESTART, 1); /* Restart on */
842 ret = mt9m032_write(client, MT9M032_RESTART, 0); /* Restart off */
846 ret = update_formatter2(sensor, false);
853 media_entity_cleanup(&sensor->subdev.entity);
855 v4l2_ctrl_handler_free(&sensor->ctrls);
857 mutex_destroy(&sensor->lock);
861 static void mt9m032_remove(struct i2c_client *client)
863 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
864 struct mt9m032 *sensor = to_mt9m032(subdev);
866 v4l2_device_unregister_subdev(subdev);
867 v4l2_ctrl_handler_free(&sensor->ctrls);
868 media_entity_cleanup(&subdev->entity);
869 mutex_destroy(&sensor->lock);
872 static const struct i2c_device_id mt9m032_id_table[] = {
877 MODULE_DEVICE_TABLE(i2c, mt9m032_id_table);
879 static struct i2c_driver mt9m032_i2c_driver = {
881 .name = MT9M032_NAME,
883 .probe = mt9m032_probe,
884 .remove = mt9m032_remove,
885 .id_table = mt9m032_id_table,
888 module_i2c_driver(mt9m032_i2c_driver);
890 MODULE_AUTHOR("Martin Hostettler <martin@neutronstar.dyndns.org>");
891 MODULE_DESCRIPTION("MT9M032 camera sensor driver");
892 MODULE_LICENSE("GPL v2");