Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / i2c / mt9m032.c
blob6a9e068462fd9484be7a69135af9ed8510311d63
1 /*
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.
18 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/math64.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/v4l2-mediabus.h>
28 #include <media/media-entity.h>
29 #include <media/i2c/mt9m032.h>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-subdev.h>
34 #include "aptina-pll.h"
37 * width and height include active boundary and black parts
39 * column 0- 15 active boundary
40 * column 16-1455 image
41 * column 1456-1471 active boundary
42 * column 1472-1599 black
44 * row 0- 51 black
45 * row 53- 59 active boundary
46 * row 60-1139 image
47 * row 1140-1147 active boundary
48 * row 1148-1151 black
51 #define MT9M032_PIXEL_ARRAY_WIDTH 1600
52 #define MT9M032_PIXEL_ARRAY_HEIGHT 1152
54 #define MT9M032_CHIP_VERSION 0x00
55 #define MT9M032_CHIP_VERSION_VALUE 0x1402
56 #define MT9M032_ROW_START 0x01
57 #define MT9M032_ROW_START_MIN 0
58 #define MT9M032_ROW_START_MAX 1152
59 #define MT9M032_ROW_START_DEF 60
60 #define MT9M032_COLUMN_START 0x02
61 #define MT9M032_COLUMN_START_MIN 0
62 #define MT9M032_COLUMN_START_MAX 1600
63 #define MT9M032_COLUMN_START_DEF 16
64 #define MT9M032_ROW_SIZE 0x03
65 #define MT9M032_ROW_SIZE_MIN 32
66 #define MT9M032_ROW_SIZE_MAX 1152
67 #define MT9M032_ROW_SIZE_DEF 1080
68 #define MT9M032_COLUMN_SIZE 0x04
69 #define MT9M032_COLUMN_SIZE_MIN 32
70 #define MT9M032_COLUMN_SIZE_MAX 1600
71 #define MT9M032_COLUMN_SIZE_DEF 1440
72 #define MT9M032_HBLANK 0x05
73 #define MT9M032_VBLANK 0x06
74 #define MT9M032_VBLANK_MAX 0x7ff
75 #define MT9M032_SHUTTER_WIDTH_HIGH 0x08
76 #define MT9M032_SHUTTER_WIDTH_LOW 0x09
77 #define MT9M032_SHUTTER_WIDTH_MIN 1
78 #define MT9M032_SHUTTER_WIDTH_MAX 1048575
79 #define MT9M032_SHUTTER_WIDTH_DEF 1943
80 #define MT9M032_PIX_CLK_CTRL 0x0a
81 #define MT9M032_PIX_CLK_CTRL_INV_PIXCLK 0x8000
82 #define MT9M032_RESTART 0x0b
83 #define MT9M032_RESET 0x0d
84 #define MT9M032_PLL_CONFIG1 0x11
85 #define MT9M032_PLL_CONFIG1_PREDIV_MASK 0x3f
86 #define MT9M032_PLL_CONFIG1_MUL_SHIFT 8
87 #define MT9M032_READ_MODE1 0x1e
88 #define MT9M032_READ_MODE1_OUTPUT_BAD_FRAMES (1 << 13)
89 #define MT9M032_READ_MODE1_MAINTAIN_FRAME_RATE (1 << 12)
90 #define MT9M032_READ_MODE1_XOR_LINE_VALID (1 << 11)
91 #define MT9M032_READ_MODE1_CONT_LINE_VALID (1 << 10)
92 #define MT9M032_READ_MODE1_INVERT_TRIGGER (1 << 9)
93 #define MT9M032_READ_MODE1_SNAPSHOT (1 << 8)
94 #define MT9M032_READ_MODE1_GLOBAL_RESET (1 << 7)
95 #define MT9M032_READ_MODE1_BULB_EXPOSURE (1 << 6)
96 #define MT9M032_READ_MODE1_INVERT_STROBE (1 << 5)
97 #define MT9M032_READ_MODE1_STROBE_ENABLE (1 << 4)
98 #define MT9M032_READ_MODE1_STROBE_START_TRIG1 (0 << 2)
99 #define MT9M032_READ_MODE1_STROBE_START_EXP (1 << 2)
100 #define MT9M032_READ_MODE1_STROBE_START_SHUTTER (2 << 2)
101 #define MT9M032_READ_MODE1_STROBE_START_TRIG2 (3 << 2)
102 #define MT9M032_READ_MODE1_STROBE_END_TRIG1 (0 << 0)
103 #define MT9M032_READ_MODE1_STROBE_END_EXP (1 << 0)
104 #define MT9M032_READ_MODE1_STROBE_END_SHUTTER (2 << 0)
105 #define MT9M032_READ_MODE1_STROBE_END_TRIG2 (3 << 0)
106 #define MT9M032_READ_MODE2 0x20
107 #define MT9M032_READ_MODE2_VFLIP_SHIFT 15
108 #define MT9M032_READ_MODE2_HFLIP_SHIFT 14
109 #define MT9M032_READ_MODE2_ROW_BLC 0x40
110 #define MT9M032_GAIN_GREEN1 0x2b
111 #define MT9M032_GAIN_BLUE 0x2c
112 #define MT9M032_GAIN_RED 0x2d
113 #define MT9M032_GAIN_GREEN2 0x2e
115 /* write only */
116 #define MT9M032_GAIN_ALL 0x35
117 #define MT9M032_GAIN_DIGITAL_MASK 0x7f
118 #define MT9M032_GAIN_DIGITAL_SHIFT 8
119 #define MT9M032_GAIN_AMUL_SHIFT 6
120 #define MT9M032_GAIN_ANALOG_MASK 0x3f
121 #define MT9M032_FORMATTER1 0x9e
122 #define MT9M032_FORMATTER1_PLL_P1_6 (1 << 8)
123 #define MT9M032_FORMATTER1_PARALLEL (1 << 12)
124 #define MT9M032_FORMATTER2 0x9f
125 #define MT9M032_FORMATTER2_DOUT_EN 0x1000
126 #define MT9M032_FORMATTER2_PIXCLK_EN 0x2000
129 * The available MT9M032 datasheet is missing documentation for register 0x10
130 * MT9P031 seems to be close enough, so use constants from that datasheet for
131 * now.
132 * But keep the name MT9P031 to remind us, that this isn't really confirmed
133 * for this sensor.
135 #define MT9P031_PLL_CONTROL 0x10
136 #define MT9P031_PLL_CONTROL_PWROFF 0x0050
137 #define MT9P031_PLL_CONTROL_PWRON 0x0051
138 #define MT9P031_PLL_CONTROL_USEPLL 0x0052
140 struct mt9m032 {
141 struct v4l2_subdev subdev;
142 struct media_pad pad;
143 struct mt9m032_platform_data *pdata;
145 unsigned int pix_clock;
147 struct v4l2_ctrl_handler ctrls;
148 struct {
149 struct v4l2_ctrl *hflip;
150 struct v4l2_ctrl *vflip;
153 struct mutex lock; /* Protects streaming, format, interval and crop */
155 bool streaming;
157 struct v4l2_mbus_framefmt format;
158 struct v4l2_rect crop;
159 struct v4l2_fract frame_interval;
162 #define to_mt9m032(sd) container_of(sd, struct mt9m032, subdev)
163 #define to_dev(sensor) \
164 (&((struct i2c_client *)v4l2_get_subdevdata(&(sensor)->subdev))->dev)
166 static int mt9m032_read(struct i2c_client *client, u8 reg)
168 return i2c_smbus_read_word_swapped(client, reg);
171 static int mt9m032_write(struct i2c_client *client, u8 reg, const u16 data)
173 return i2c_smbus_write_word_swapped(client, reg, data);
176 static u32 mt9m032_row_time(struct mt9m032 *sensor, unsigned int width)
178 unsigned int effective_width;
179 u32 ns;
181 effective_width = width + 716; /* empirical value */
182 ns = div_u64(1000000000ULL * effective_width, sensor->pix_clock);
183 dev_dbg(to_dev(sensor), "MT9M032 line time: %u ns\n", ns);
184 return ns;
187 static int mt9m032_update_timing(struct mt9m032 *sensor,
188 struct v4l2_fract *interval)
190 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
191 struct v4l2_rect *crop = &sensor->crop;
192 unsigned int min_vblank;
193 unsigned int vblank;
194 u32 row_time;
196 if (!interval)
197 interval = &sensor->frame_interval;
199 row_time = mt9m032_row_time(sensor, crop->width);
201 vblank = div_u64(1000000000ULL * interval->numerator,
202 (u64)row_time * interval->denominator)
203 - crop->height;
205 if (vblank > MT9M032_VBLANK_MAX) {
206 /* hardware limits to 11 bit values */
207 interval->denominator = 1000;
208 interval->numerator =
209 div_u64((crop->height + MT9M032_VBLANK_MAX) *
210 (u64)row_time * interval->denominator,
211 1000000000ULL);
212 vblank = div_u64(1000000000ULL * interval->numerator,
213 (u64)row_time * interval->denominator)
214 - crop->height;
216 /* enforce minimal 1.6ms blanking time. */
217 min_vblank = 1600000 / row_time;
218 vblank = clamp_t(unsigned int, vblank, min_vblank, MT9M032_VBLANK_MAX);
220 return mt9m032_write(client, MT9M032_VBLANK, vblank);
223 static int mt9m032_update_geom_timing(struct mt9m032 *sensor)
225 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
226 int ret;
228 ret = mt9m032_write(client, MT9M032_COLUMN_SIZE,
229 sensor->crop.width - 1);
230 if (!ret)
231 ret = mt9m032_write(client, MT9M032_ROW_SIZE,
232 sensor->crop.height - 1);
233 if (!ret)
234 ret = mt9m032_write(client, MT9M032_COLUMN_START,
235 sensor->crop.left);
236 if (!ret)
237 ret = mt9m032_write(client, MT9M032_ROW_START,
238 sensor->crop.top);
239 if (!ret)
240 ret = mt9m032_update_timing(sensor, NULL);
241 return ret;
244 static int update_formatter2(struct mt9m032 *sensor, bool streaming)
246 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
247 u16 reg_val = MT9M032_FORMATTER2_DOUT_EN
248 | 0x0070; /* parts reserved! */
249 /* possibly for changing to 14-bit mode */
251 if (streaming)
252 reg_val |= MT9M032_FORMATTER2_PIXCLK_EN; /* pixclock enable */
254 return mt9m032_write(client, MT9M032_FORMATTER2, reg_val);
257 static int mt9m032_setup_pll(struct mt9m032 *sensor)
259 static const struct aptina_pll_limits limits = {
260 .ext_clock_min = 8000000,
261 .ext_clock_max = 16500000,
262 .int_clock_min = 2000000,
263 .int_clock_max = 24000000,
264 .out_clock_min = 322000000,
265 .out_clock_max = 693000000,
266 .pix_clock_max = 99000000,
267 .n_min = 1,
268 .n_max = 64,
269 .m_min = 16,
270 .m_max = 255,
271 .p1_min = 6,
272 .p1_max = 7,
275 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
276 struct mt9m032_platform_data *pdata = sensor->pdata;
277 struct aptina_pll pll;
278 u16 reg_val;
279 int ret;
281 pll.ext_clock = pdata->ext_clock;
282 pll.pix_clock = pdata->pix_clock;
284 ret = aptina_pll_calculate(&client->dev, &limits, &pll);
285 if (ret < 0)
286 return ret;
288 sensor->pix_clock = pdata->pix_clock;
290 ret = mt9m032_write(client, MT9M032_PLL_CONFIG1,
291 (pll.m << MT9M032_PLL_CONFIG1_MUL_SHIFT) |
292 ((pll.n - 1) & MT9M032_PLL_CONFIG1_PREDIV_MASK));
293 if (!ret)
294 ret = mt9m032_write(client, MT9P031_PLL_CONTROL,
295 MT9P031_PLL_CONTROL_PWRON |
296 MT9P031_PLL_CONTROL_USEPLL);
297 if (!ret) /* more reserved, Continuous, Master Mode */
298 ret = mt9m032_write(client, MT9M032_READ_MODE1, 0x8000 |
299 MT9M032_READ_MODE1_STROBE_START_EXP |
300 MT9M032_READ_MODE1_STROBE_END_SHUTTER);
301 if (!ret) {
302 reg_val = (pll.p1 == 6 ? MT9M032_FORMATTER1_PLL_P1_6 : 0)
303 | MT9M032_FORMATTER1_PARALLEL | 0x001e; /* 14-bit */
304 ret = mt9m032_write(client, MT9M032_FORMATTER1, reg_val);
307 return ret;
310 /* -----------------------------------------------------------------------------
311 * Subdev pad operations
314 static int mt9m032_enum_mbus_code(struct v4l2_subdev *subdev,
315 struct v4l2_subdev_pad_config *cfg,
316 struct v4l2_subdev_mbus_code_enum *code)
318 if (code->index != 0)
319 return -EINVAL;
321 code->code = MEDIA_BUS_FMT_Y8_1X8;
322 return 0;
325 static int mt9m032_enum_frame_size(struct v4l2_subdev *subdev,
326 struct v4l2_subdev_pad_config *cfg,
327 struct v4l2_subdev_frame_size_enum *fse)
329 if (fse->index != 0 || fse->code != MEDIA_BUS_FMT_Y8_1X8)
330 return -EINVAL;
332 fse->min_width = MT9M032_COLUMN_SIZE_DEF;
333 fse->max_width = MT9M032_COLUMN_SIZE_DEF;
334 fse->min_height = MT9M032_ROW_SIZE_DEF;
335 fse->max_height = MT9M032_ROW_SIZE_DEF;
337 return 0;
341 * __mt9m032_get_pad_crop() - get crop rect
342 * @sensor: pointer to the sensor struct
343 * @cfg: v4l2_subdev_pad_config for getting the try crop rect from
344 * @which: select try or active crop rect
346 * Returns a pointer the current active or fh relative try crop rect
348 static struct v4l2_rect *
349 __mt9m032_get_pad_crop(struct mt9m032 *sensor, struct v4l2_subdev_pad_config *cfg,
350 enum v4l2_subdev_format_whence which)
352 switch (which) {
353 case V4L2_SUBDEV_FORMAT_TRY:
354 return v4l2_subdev_get_try_crop(&sensor->subdev, cfg, 0);
355 case V4L2_SUBDEV_FORMAT_ACTIVE:
356 return &sensor->crop;
357 default:
358 return NULL;
363 * __mt9m032_get_pad_format() - get format
364 * @sensor: pointer to the sensor struct
365 * @cfg: v4l2_subdev_pad_config for getting the try format from
366 * @which: select try or active format
368 * Returns a pointer the current active or fh relative try format
370 static struct v4l2_mbus_framefmt *
371 __mt9m032_get_pad_format(struct mt9m032 *sensor, struct v4l2_subdev_pad_config *cfg,
372 enum v4l2_subdev_format_whence which)
374 switch (which) {
375 case V4L2_SUBDEV_FORMAT_TRY:
376 return v4l2_subdev_get_try_format(&sensor->subdev, cfg, 0);
377 case V4L2_SUBDEV_FORMAT_ACTIVE:
378 return &sensor->format;
379 default:
380 return NULL;
384 static int mt9m032_get_pad_format(struct v4l2_subdev *subdev,
385 struct v4l2_subdev_pad_config *cfg,
386 struct v4l2_subdev_format *fmt)
388 struct mt9m032 *sensor = to_mt9m032(subdev);
390 mutex_lock(&sensor->lock);
391 fmt->format = *__mt9m032_get_pad_format(sensor, cfg, fmt->which);
392 mutex_unlock(&sensor->lock);
394 return 0;
397 static int mt9m032_set_pad_format(struct v4l2_subdev *subdev,
398 struct v4l2_subdev_pad_config *cfg,
399 struct v4l2_subdev_format *fmt)
401 struct mt9m032 *sensor = to_mt9m032(subdev);
402 int ret;
404 mutex_lock(&sensor->lock);
406 if (sensor->streaming && fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
407 ret = -EBUSY;
408 goto done;
411 /* Scaling is not supported, the format is thus fixed. */
412 fmt->format = *__mt9m032_get_pad_format(sensor, cfg, fmt->which);
413 ret = 0;
415 done:
416 mutex_unlock(&sensor->lock);
417 return ret;
420 static int mt9m032_get_pad_selection(struct v4l2_subdev *subdev,
421 struct v4l2_subdev_pad_config *cfg,
422 struct v4l2_subdev_selection *sel)
424 struct mt9m032 *sensor = to_mt9m032(subdev);
426 if (sel->target != V4L2_SEL_TGT_CROP)
427 return -EINVAL;
429 mutex_lock(&sensor->lock);
430 sel->r = *__mt9m032_get_pad_crop(sensor, cfg, sel->which);
431 mutex_unlock(&sensor->lock);
433 return 0;
436 static int mt9m032_set_pad_selection(struct v4l2_subdev *subdev,
437 struct v4l2_subdev_pad_config *cfg,
438 struct v4l2_subdev_selection *sel)
440 struct mt9m032 *sensor = to_mt9m032(subdev);
441 struct v4l2_mbus_framefmt *format;
442 struct v4l2_rect *__crop;
443 struct v4l2_rect rect;
444 int ret = 0;
446 if (sel->target != V4L2_SEL_TGT_CROP)
447 return -EINVAL;
449 mutex_lock(&sensor->lock);
451 if (sensor->streaming && sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
452 ret = -EBUSY;
453 goto done;
456 /* Clamp the crop rectangle boundaries and align them to a multiple of 2
457 * pixels to ensure a GRBG Bayer pattern.
459 rect.left = clamp(ALIGN(sel->r.left, 2), MT9M032_COLUMN_START_MIN,
460 MT9M032_COLUMN_START_MAX);
461 rect.top = clamp(ALIGN(sel->r.top, 2), MT9M032_ROW_START_MIN,
462 MT9M032_ROW_START_MAX);
463 rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
464 MT9M032_COLUMN_SIZE_MIN, MT9M032_COLUMN_SIZE_MAX);
465 rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
466 MT9M032_ROW_SIZE_MIN, MT9M032_ROW_SIZE_MAX);
468 rect.width = min_t(unsigned int, rect.width,
469 MT9M032_PIXEL_ARRAY_WIDTH - rect.left);
470 rect.height = min_t(unsigned int, rect.height,
471 MT9M032_PIXEL_ARRAY_HEIGHT - rect.top);
473 __crop = __mt9m032_get_pad_crop(sensor, cfg, sel->which);
475 if (rect.width != __crop->width || rect.height != __crop->height) {
476 /* Reset the output image size if the crop rectangle size has
477 * been modified.
479 format = __mt9m032_get_pad_format(sensor, cfg, sel->which);
480 format->width = rect.width;
481 format->height = rect.height;
484 *__crop = rect;
485 sel->r = rect;
487 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
488 ret = mt9m032_update_geom_timing(sensor);
490 done:
491 mutex_unlock(&sensor->lock);
492 return ret;
495 static int mt9m032_get_frame_interval(struct v4l2_subdev *subdev,
496 struct v4l2_subdev_frame_interval *fi)
498 struct mt9m032 *sensor = to_mt9m032(subdev);
500 mutex_lock(&sensor->lock);
501 memset(fi, 0, sizeof(*fi));
502 fi->interval = sensor->frame_interval;
503 mutex_unlock(&sensor->lock);
505 return 0;
508 static int mt9m032_set_frame_interval(struct v4l2_subdev *subdev,
509 struct v4l2_subdev_frame_interval *fi)
511 struct mt9m032 *sensor = to_mt9m032(subdev);
512 int ret;
514 mutex_lock(&sensor->lock);
516 if (sensor->streaming) {
517 ret = -EBUSY;
518 goto done;
521 /* Avoid divisions by 0. */
522 if (fi->interval.denominator == 0)
523 fi->interval.denominator = 1;
525 ret = mt9m032_update_timing(sensor, &fi->interval);
526 if (!ret)
527 sensor->frame_interval = fi->interval;
529 done:
530 mutex_unlock(&sensor->lock);
531 return ret;
534 static int mt9m032_s_stream(struct v4l2_subdev *subdev, int streaming)
536 struct mt9m032 *sensor = to_mt9m032(subdev);
537 int ret;
539 mutex_lock(&sensor->lock);
540 ret = update_formatter2(sensor, streaming);
541 if (!ret)
542 sensor->streaming = streaming;
543 mutex_unlock(&sensor->lock);
545 return ret;
548 /* -----------------------------------------------------------------------------
549 * V4L2 subdev core operations
552 #ifdef CONFIG_VIDEO_ADV_DEBUG
553 static int mt9m032_g_register(struct v4l2_subdev *sd,
554 struct v4l2_dbg_register *reg)
556 struct mt9m032 *sensor = to_mt9m032(sd);
557 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
558 int val;
560 if (reg->reg > 0xff)
561 return -EINVAL;
563 val = mt9m032_read(client, reg->reg);
564 if (val < 0)
565 return -EIO;
567 reg->size = 2;
568 reg->val = val;
570 return 0;
573 static int mt9m032_s_register(struct v4l2_subdev *sd,
574 const struct v4l2_dbg_register *reg)
576 struct mt9m032 *sensor = to_mt9m032(sd);
577 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
579 if (reg->reg > 0xff)
580 return -EINVAL;
582 return mt9m032_write(client, reg->reg, reg->val);
584 #endif
586 /* -----------------------------------------------------------------------------
587 * V4L2 subdev control operations
590 static int update_read_mode2(struct mt9m032 *sensor, bool vflip, bool hflip)
592 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
593 int reg_val = (vflip << MT9M032_READ_MODE2_VFLIP_SHIFT)
594 | (hflip << MT9M032_READ_MODE2_HFLIP_SHIFT)
595 | MT9M032_READ_MODE2_ROW_BLC
596 | 0x0007;
598 return mt9m032_write(client, MT9M032_READ_MODE2, reg_val);
601 static int mt9m032_set_gain(struct mt9m032 *sensor, s32 val)
603 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
604 int digital_gain_val; /* in 1/8th (0..127) */
605 int analog_mul; /* 0 or 1 */
606 int analog_gain_val; /* in 1/16th. (0..63) */
607 u16 reg_val;
609 digital_gain_val = 51; /* from setup example */
611 if (val < 63) {
612 analog_mul = 0;
613 analog_gain_val = val;
614 } else {
615 analog_mul = 1;
616 analog_gain_val = val / 2;
619 /* a_gain = (1 + analog_mul) + (analog_gain_val + 1) / 16 */
620 /* overall_gain = a_gain * (1 + digital_gain_val / 8) */
622 reg_val = ((digital_gain_val & MT9M032_GAIN_DIGITAL_MASK)
623 << MT9M032_GAIN_DIGITAL_SHIFT)
624 | ((analog_mul & 1) << MT9M032_GAIN_AMUL_SHIFT)
625 | (analog_gain_val & MT9M032_GAIN_ANALOG_MASK);
627 return mt9m032_write(client, MT9M032_GAIN_ALL, reg_val);
630 static int mt9m032_try_ctrl(struct v4l2_ctrl *ctrl)
632 if (ctrl->id == V4L2_CID_GAIN && ctrl->val >= 63) {
633 /* round because of multiplier used for values >= 63 */
634 ctrl->val &= ~1;
637 return 0;
640 static int mt9m032_set_ctrl(struct v4l2_ctrl *ctrl)
642 struct mt9m032 *sensor =
643 container_of(ctrl->handler, struct mt9m032, ctrls);
644 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
645 int ret;
647 switch (ctrl->id) {
648 case V4L2_CID_GAIN:
649 return mt9m032_set_gain(sensor, ctrl->val);
651 case V4L2_CID_HFLIP:
652 /* case V4L2_CID_VFLIP: -- In the same cluster */
653 return update_read_mode2(sensor, sensor->vflip->val,
654 sensor->hflip->val);
656 case V4L2_CID_EXPOSURE:
657 ret = mt9m032_write(client, MT9M032_SHUTTER_WIDTH_HIGH,
658 (ctrl->val >> 16) & 0xffff);
659 if (ret < 0)
660 return ret;
662 return mt9m032_write(client, MT9M032_SHUTTER_WIDTH_LOW,
663 ctrl->val & 0xffff);
666 return 0;
669 static const struct v4l2_ctrl_ops mt9m032_ctrl_ops = {
670 .s_ctrl = mt9m032_set_ctrl,
671 .try_ctrl = mt9m032_try_ctrl,
674 /* -------------------------------------------------------------------------- */
676 static const struct v4l2_subdev_core_ops mt9m032_core_ops = {
677 #ifdef CONFIG_VIDEO_ADV_DEBUG
678 .g_register = mt9m032_g_register,
679 .s_register = mt9m032_s_register,
680 #endif
683 static const struct v4l2_subdev_video_ops mt9m032_video_ops = {
684 .s_stream = mt9m032_s_stream,
685 .g_frame_interval = mt9m032_get_frame_interval,
686 .s_frame_interval = mt9m032_set_frame_interval,
689 static const struct v4l2_subdev_pad_ops mt9m032_pad_ops = {
690 .enum_mbus_code = mt9m032_enum_mbus_code,
691 .enum_frame_size = mt9m032_enum_frame_size,
692 .get_fmt = mt9m032_get_pad_format,
693 .set_fmt = mt9m032_set_pad_format,
694 .set_selection = mt9m032_set_pad_selection,
695 .get_selection = mt9m032_get_pad_selection,
698 static const struct v4l2_subdev_ops mt9m032_ops = {
699 .core = &mt9m032_core_ops,
700 .video = &mt9m032_video_ops,
701 .pad = &mt9m032_pad_ops,
704 /* -----------------------------------------------------------------------------
705 * Driver initialization and probing
708 static int mt9m032_probe(struct i2c_client *client,
709 const struct i2c_device_id *devid)
711 struct mt9m032_platform_data *pdata = client->dev.platform_data;
712 struct i2c_adapter *adapter = client->adapter;
713 struct mt9m032 *sensor;
714 int chip_version;
715 int ret;
717 if (pdata == NULL) {
718 dev_err(&client->dev, "No platform data\n");
719 return -EINVAL;
722 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
723 dev_warn(&client->dev,
724 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
725 return -EIO;
728 if (!client->dev.platform_data)
729 return -ENODEV;
731 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
732 if (sensor == NULL)
733 return -ENOMEM;
735 mutex_init(&sensor->lock);
737 sensor->pdata = pdata;
739 v4l2_i2c_subdev_init(&sensor->subdev, client, &mt9m032_ops);
740 sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
742 chip_version = mt9m032_read(client, MT9M032_CHIP_VERSION);
743 if (chip_version != MT9M032_CHIP_VERSION_VALUE) {
744 dev_err(&client->dev, "MT9M032 not detected, wrong version "
745 "0x%04x\n", chip_version);
746 ret = -ENODEV;
747 goto error_sensor;
750 dev_info(&client->dev, "MT9M032 detected at address 0x%02x\n",
751 client->addr);
753 sensor->frame_interval.numerator = 1;
754 sensor->frame_interval.denominator = 30;
756 sensor->crop.left = MT9M032_COLUMN_START_DEF;
757 sensor->crop.top = MT9M032_ROW_START_DEF;
758 sensor->crop.width = MT9M032_COLUMN_SIZE_DEF;
759 sensor->crop.height = MT9M032_ROW_SIZE_DEF;
761 sensor->format.width = sensor->crop.width;
762 sensor->format.height = sensor->crop.height;
763 sensor->format.code = MEDIA_BUS_FMT_Y8_1X8;
764 sensor->format.field = V4L2_FIELD_NONE;
765 sensor->format.colorspace = V4L2_COLORSPACE_SRGB;
767 v4l2_ctrl_handler_init(&sensor->ctrls, 5);
769 v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
770 V4L2_CID_GAIN, 0, 127, 1, 64);
772 sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls,
773 &mt9m032_ctrl_ops,
774 V4L2_CID_HFLIP, 0, 1, 1, 0);
775 sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls,
776 &mt9m032_ctrl_ops,
777 V4L2_CID_VFLIP, 0, 1, 1, 0);
779 v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
780 V4L2_CID_EXPOSURE, MT9M032_SHUTTER_WIDTH_MIN,
781 MT9M032_SHUTTER_WIDTH_MAX, 1,
782 MT9M032_SHUTTER_WIDTH_DEF);
783 v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
784 V4L2_CID_PIXEL_RATE, pdata->pix_clock,
785 pdata->pix_clock, 1, pdata->pix_clock);
787 if (sensor->ctrls.error) {
788 ret = sensor->ctrls.error;
789 dev_err(&client->dev, "control initialization error %d\n", ret);
790 goto error_ctrl;
793 v4l2_ctrl_cluster(2, &sensor->hflip);
795 sensor->subdev.ctrl_handler = &sensor->ctrls;
796 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
797 ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad);
798 if (ret < 0)
799 goto error_ctrl;
801 ret = mt9m032_write(client, MT9M032_RESET, 1); /* reset on */
802 if (ret < 0)
803 goto error_entity;
804 ret = mt9m032_write(client, MT9M032_RESET, 0); /* reset off */
805 if (ret < 0)
806 goto error_entity;
808 ret = mt9m032_setup_pll(sensor);
809 if (ret < 0)
810 goto error_entity;
811 usleep_range(10000, 11000);
813 ret = v4l2_ctrl_handler_setup(&sensor->ctrls);
814 if (ret < 0)
815 goto error_entity;
817 /* SIZE */
818 ret = mt9m032_update_geom_timing(sensor);
819 if (ret < 0)
820 goto error_entity;
822 ret = mt9m032_write(client, 0x41, 0x0000); /* reserved !!! */
823 if (ret < 0)
824 goto error_entity;
825 ret = mt9m032_write(client, 0x42, 0x0003); /* reserved !!! */
826 if (ret < 0)
827 goto error_entity;
828 ret = mt9m032_write(client, 0x43, 0x0003); /* reserved !!! */
829 if (ret < 0)
830 goto error_entity;
831 ret = mt9m032_write(client, 0x7f, 0x0000); /* reserved !!! */
832 if (ret < 0)
833 goto error_entity;
834 if (sensor->pdata->invert_pixclock) {
835 ret = mt9m032_write(client, MT9M032_PIX_CLK_CTRL,
836 MT9M032_PIX_CLK_CTRL_INV_PIXCLK);
837 if (ret < 0)
838 goto error_entity;
841 ret = mt9m032_write(client, MT9M032_RESTART, 1); /* Restart on */
842 if (ret < 0)
843 goto error_entity;
844 msleep(100);
845 ret = mt9m032_write(client, MT9M032_RESTART, 0); /* Restart off */
846 if (ret < 0)
847 goto error_entity;
848 msleep(100);
849 ret = update_formatter2(sensor, false);
850 if (ret < 0)
851 goto error_entity;
853 return ret;
855 error_entity:
856 media_entity_cleanup(&sensor->subdev.entity);
857 error_ctrl:
858 v4l2_ctrl_handler_free(&sensor->ctrls);
859 error_sensor:
860 mutex_destroy(&sensor->lock);
861 return ret;
864 static int mt9m032_remove(struct i2c_client *client)
866 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
867 struct mt9m032 *sensor = to_mt9m032(subdev);
869 v4l2_device_unregister_subdev(subdev);
870 v4l2_ctrl_handler_free(&sensor->ctrls);
871 media_entity_cleanup(&subdev->entity);
872 mutex_destroy(&sensor->lock);
873 return 0;
876 static const struct i2c_device_id mt9m032_id_table[] = {
877 { MT9M032_NAME, 0 },
881 MODULE_DEVICE_TABLE(i2c, mt9m032_id_table);
883 static struct i2c_driver mt9m032_i2c_driver = {
884 .driver = {
885 .name = MT9M032_NAME,
887 .probe = mt9m032_probe,
888 .remove = mt9m032_remove,
889 .id_table = mt9m032_id_table,
892 module_i2c_driver(mt9m032_i2c_driver);
894 MODULE_AUTHOR("Martin Hostettler <martin@neutronstar.dyndns.org>");
895 MODULE_DESCRIPTION("MT9M032 camera sensor driver");
896 MODULE_LICENSE("GPL v2");