blob: 2f40a35b3ddbbb9da094d56cff3ec1c6967e037f [file] [log] [blame]
Martin Hostettler251cb882011-12-17 07:10:55 -03001/*
2 * Driver for MT9M032 CMOS Image Sensor from Micron
3 *
4 * Copyright (C) 2010-2011 Lund Engineering
5 * Contact: Gil Lund <gwlund@lundeng.com>
6 * Author: Martin Hostettler <martin@neutronstar.dyndns.org>
7 *
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.
11 *
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.
16 *
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
20 * 02110-1301 USA
21 */
22
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>
32
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>
38
39#include "aptina-pll.h"
40
41/*
42 * width and height include active boundary and black parts
43 *
44 * column 0- 15 active boundary
45 * column 16-1455 image
46 * column 1456-1471 active boundary
47 * column 1472-1599 black
48 *
49 * row 0- 51 black
50 * row 53- 59 active boundary
51 * row 60-1139 image
52 * row 1140-1147 active boundary
53 * row 1148-1151 black
54 */
55
56#define MT9M032_PIXEL_ARRAY_WIDTH 1600
57#define MT9M032_PIXEL_ARRAY_HEIGHT 1152
58
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
Laurent Pincharta54b80b2012-09-25 10:46:34 -030090#define MT9M032_PLL_CONFIG1_PREDIV_MASK 0x3f
Martin Hostettler251cb882011-12-17 07:10:55 -030091#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
101
102/* write only */
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
Laurent Pincharta54b80b2012-09-25 10:46:34 -0300109#define MT9M032_FORMATTER1_PLL_P1_6 (1 << 8)
110#define MT9M032_FORMATTER1_PARALLEL (1 << 12)
Martin Hostettler251cb882011-12-17 07:10:55 -0300111#define MT9M032_FORMATTER2 0x9f
112#define MT9M032_FORMATTER2_DOUT_EN 0x1000
113#define MT9M032_FORMATTER2_PIXCLK_EN 0x2000
114
115/*
116 * The available MT9M032 datasheet is missing documentation for register 0x10
117 * MT9P031 seems to be close enough, so use constants from that datasheet for
118 * now.
119 * But keep the name MT9P031 to remind us, that this isn't really confirmed
120 * for this sensor.
121 */
122#define MT9P031_PLL_CONTROL 0x10
123#define MT9P031_PLL_CONTROL_PWROFF 0x0050
124#define MT9P031_PLL_CONTROL_PWRON 0x0051
125#define MT9P031_PLL_CONTROL_USEPLL 0x0052
Martin Hostettler251cb882011-12-17 07:10:55 -0300126
127struct mt9m032 {
128 struct v4l2_subdev subdev;
129 struct media_pad pad;
130 struct mt9m032_platform_data *pdata;
131
132 unsigned int pix_clock;
133
134 struct v4l2_ctrl_handler ctrls;
135 struct {
136 struct v4l2_ctrl *hflip;
137 struct v4l2_ctrl *vflip;
138 };
139
140 struct mutex lock; /* Protects streaming, format, interval and crop */
141
142 bool streaming;
143
144 struct v4l2_mbus_framefmt format;
145 struct v4l2_rect crop;
146 struct v4l2_fract frame_interval;
147};
148
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)
152
153static int mt9m032_read(struct i2c_client *client, u8 reg)
154{
155 return i2c_smbus_read_word_swapped(client, reg);
156}
157
158static int mt9m032_write(struct i2c_client *client, u8 reg, const u16 data)
159{
160 return i2c_smbus_write_word_swapped(client, reg, data);
161}
162
163static u32 mt9m032_row_time(struct mt9m032 *sensor, unsigned int width)
164{
165 unsigned int effective_width;
166 u32 ns;
167
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);
171 return ns;
172}
173
174static int mt9m032_update_timing(struct mt9m032 *sensor,
175 struct v4l2_fract *interval)
176{
177 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
178 struct v4l2_rect *crop = &sensor->crop;
179 unsigned int min_vblank;
180 unsigned int vblank;
181 u32 row_time;
182
183 if (!interval)
184 interval = &sensor->frame_interval;
185
186 row_time = mt9m032_row_time(sensor, crop->width);
187
188 vblank = div_u64(1000000000ULL * interval->numerator,
189 (u64)row_time * interval->denominator)
190 - crop->height;
191
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,
198 1000000000ULL);
199 vblank = div_u64(1000000000ULL * interval->numerator,
200 (u64)row_time * interval->denominator)
201 - crop->height;
202 }
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);
206
207 return mt9m032_write(client, MT9M032_VBLANK, vblank);
208}
209
210static int mt9m032_update_geom_timing(struct mt9m032 *sensor)
211{
212 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
213 int ret;
214
215 ret = mt9m032_write(client, MT9M032_COLUMN_SIZE,
216 sensor->crop.width - 1);
217 if (!ret)
218 ret = mt9m032_write(client, MT9M032_ROW_SIZE,
219 sensor->crop.height - 1);
220 if (!ret)
221 ret = mt9m032_write(client, MT9M032_COLUMN_START,
222 sensor->crop.left);
223 if (!ret)
224 ret = mt9m032_write(client, MT9M032_ROW_START,
225 sensor->crop.top);
226 if (!ret)
227 ret = mt9m032_update_timing(sensor, NULL);
228 return ret;
229}
230
231static int update_formatter2(struct mt9m032 *sensor, bool streaming)
232{
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 */
237
238 if (streaming)
239 reg_val |= MT9M032_FORMATTER2_PIXCLK_EN; /* pixclock enable */
240
241 return mt9m032_write(client, MT9M032_FORMATTER2, reg_val);
242}
243
244static int mt9m032_setup_pll(struct mt9m032 *sensor)
245{
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,
254 .n_min = 1,
255 .n_max = 64,
256 .m_min = 16,
257 .m_max = 255,
Laurent Pincharta54b80b2012-09-25 10:46:34 -0300258 .p1_min = 6,
259 .p1_max = 7,
Martin Hostettler251cb882011-12-17 07:10:55 -0300260 };
261
262 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
263 struct mt9m032_platform_data *pdata = sensor->pdata;
264 struct aptina_pll pll;
Laurent Pincharta54b80b2012-09-25 10:46:34 -0300265 u16 reg_val;
Martin Hostettler251cb882011-12-17 07:10:55 -0300266 int ret;
267
268 pll.ext_clock = pdata->ext_clock;
269 pll.pix_clock = pdata->pix_clock;
270
271 ret = aptina_pll_calculate(&client->dev, &limits, &pll);
272 if (ret < 0)
273 return ret;
274
275 sensor->pix_clock = pdata->pix_clock;
276
277 ret = mt9m032_write(client, MT9M032_PLL_CONFIG1,
Laurent Pincharta54b80b2012-09-25 10:46:34 -0300278 (pll.m << MT9M032_PLL_CONFIG1_MUL_SHIFT) |
279 ((pll.n - 1) & MT9M032_PLL_CONFIG1_PREDIV_MASK));
Martin Hostettler251cb882011-12-17 07:10:55 -0300280 if (!ret)
281 ret = mt9m032_write(client, MT9P031_PLL_CONTROL,
282 MT9P031_PLL_CONTROL_PWRON |
283 MT9P031_PLL_CONTROL_USEPLL);
284 if (!ret) /* more reserved, Continuous, Master Mode */
285 ret = mt9m032_write(client, MT9M032_READ_MODE1, 0x8006);
Laurent Pincharta54b80b2012-09-25 10:46:34 -0300286 if (!ret) {
287 reg_val = (pll.p1 == 6 ? MT9M032_FORMATTER1_PLL_P1_6 : 0)
288 | MT9M032_FORMATTER1_PARALLEL | 0x001e; /* 14-bit */
289 ret = mt9m032_write(client, MT9M032_FORMATTER1, reg_val);
290 }
Martin Hostettler251cb882011-12-17 07:10:55 -0300291
292 return ret;
293}
294
295/* -----------------------------------------------------------------------------
296 * Subdev pad operations
297 */
298
299static int mt9m032_enum_mbus_code(struct v4l2_subdev *subdev,
300 struct v4l2_subdev_fh *fh,
301 struct v4l2_subdev_mbus_code_enum *code)
302{
303 if (code->index != 0)
304 return -EINVAL;
305
306 code->code = V4L2_MBUS_FMT_Y8_1X8;
307 return 0;
308}
309
310static int mt9m032_enum_frame_size(struct v4l2_subdev *subdev,
311 struct v4l2_subdev_fh *fh,
312 struct v4l2_subdev_frame_size_enum *fse)
313{
314 if (fse->index != 0 || fse->code != V4L2_MBUS_FMT_Y8_1X8)
315 return -EINVAL;
316
317 fse->min_width = MT9M032_COLUMN_SIZE_DEF;
318 fse->max_width = MT9M032_COLUMN_SIZE_DEF;
319 fse->min_height = MT9M032_ROW_SIZE_DEF;
320 fse->max_height = MT9M032_ROW_SIZE_DEF;
321
322 return 0;
323}
324
325/**
326 * __mt9m032_get_pad_crop() - get crop rect
327 * @sensor: pointer to the sensor struct
328 * @fh: file handle for getting the try crop rect from
329 * @which: select try or active crop rect
330 *
331 * Returns a pointer the current active or fh relative try crop rect
332 */
333static struct v4l2_rect *
334__mt9m032_get_pad_crop(struct mt9m032 *sensor, struct v4l2_subdev_fh *fh,
335 enum v4l2_subdev_format_whence which)
336{
337 switch (which) {
338 case V4L2_SUBDEV_FORMAT_TRY:
339 return v4l2_subdev_get_try_crop(fh, 0);
340 case V4L2_SUBDEV_FORMAT_ACTIVE:
341 return &sensor->crop;
342 default:
343 return NULL;
344 }
345}
346
347/**
348 * __mt9m032_get_pad_format() - get format
349 * @sensor: pointer to the sensor struct
350 * @fh: file handle for getting the try format from
351 * @which: select try or active format
352 *
353 * Returns a pointer the current active or fh relative try format
354 */
355static struct v4l2_mbus_framefmt *
356__mt9m032_get_pad_format(struct mt9m032 *sensor, struct v4l2_subdev_fh *fh,
357 enum v4l2_subdev_format_whence which)
358{
359 switch (which) {
360 case V4L2_SUBDEV_FORMAT_TRY:
361 return v4l2_subdev_get_try_format(fh, 0);
362 case V4L2_SUBDEV_FORMAT_ACTIVE:
363 return &sensor->format;
364 default:
365 return NULL;
366 }
367}
368
369static int mt9m032_get_pad_format(struct v4l2_subdev *subdev,
370 struct v4l2_subdev_fh *fh,
371 struct v4l2_subdev_format *fmt)
372{
373 struct mt9m032 *sensor = to_mt9m032(subdev);
374
375 mutex_lock(&sensor->lock);
376 fmt->format = *__mt9m032_get_pad_format(sensor, fh, fmt->which);
377 mutex_unlock(&sensor->lock);
378
379 return 0;
380}
381
382static int mt9m032_set_pad_format(struct v4l2_subdev *subdev,
383 struct v4l2_subdev_fh *fh,
384 struct v4l2_subdev_format *fmt)
385{
386 struct mt9m032 *sensor = to_mt9m032(subdev);
387 int ret;
388
389 mutex_lock(&sensor->lock);
390
391 if (sensor->streaming && fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
392 ret = -EBUSY;
393 goto done;
394 }
395
396 /* Scaling is not supported, the format is thus fixed. */
Guennadi Liakhovetskib1e11792012-04-08 17:31:24 -0300397 fmt->format = *__mt9m032_get_pad_format(sensor, fh, fmt->which);
398 ret = 0;
Martin Hostettler251cb882011-12-17 07:10:55 -0300399
400done:
Guennadi Liakhovetskib1e11792012-04-08 17:31:24 -0300401 mutex_unlock(&sensor->lock);
Martin Hostettler251cb882011-12-17 07:10:55 -0300402 return ret;
403}
404
405static int mt9m032_get_pad_crop(struct v4l2_subdev *subdev,
406 struct v4l2_subdev_fh *fh,
407 struct v4l2_subdev_crop *crop)
408{
409 struct mt9m032 *sensor = to_mt9m032(subdev);
410
411 mutex_lock(&sensor->lock);
412 crop->rect = *__mt9m032_get_pad_crop(sensor, fh, crop->which);
413 mutex_unlock(&sensor->lock);
414
415 return 0;
416}
417
418static int mt9m032_set_pad_crop(struct v4l2_subdev *subdev,
419 struct v4l2_subdev_fh *fh,
420 struct v4l2_subdev_crop *crop)
421{
422 struct mt9m032 *sensor = to_mt9m032(subdev);
423 struct v4l2_mbus_framefmt *format;
424 struct v4l2_rect *__crop;
425 struct v4l2_rect rect;
426 int ret = 0;
427
428 mutex_lock(&sensor->lock);
429
430 if (sensor->streaming && crop->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
431 ret = -EBUSY;
432 goto done;
433 }
434
435 /* Clamp the crop rectangle boundaries and align them to a multiple of 2
436 * pixels to ensure a GRBG Bayer pattern.
437 */
438 rect.left = clamp(ALIGN(crop->rect.left, 2), MT9M032_COLUMN_START_MIN,
439 MT9M032_COLUMN_START_MAX);
440 rect.top = clamp(ALIGN(crop->rect.top, 2), MT9M032_ROW_START_MIN,
441 MT9M032_ROW_START_MAX);
442 rect.width = clamp(ALIGN(crop->rect.width, 2), MT9M032_COLUMN_SIZE_MIN,
443 MT9M032_COLUMN_SIZE_MAX);
444 rect.height = clamp(ALIGN(crop->rect.height, 2), MT9M032_ROW_SIZE_MIN,
445 MT9M032_ROW_SIZE_MAX);
446
447 rect.width = min(rect.width, MT9M032_PIXEL_ARRAY_WIDTH - rect.left);
448 rect.height = min(rect.height, MT9M032_PIXEL_ARRAY_HEIGHT - rect.top);
449
450 __crop = __mt9m032_get_pad_crop(sensor, fh, crop->which);
451
452 if (rect.width != __crop->width || rect.height != __crop->height) {
453 /* Reset the output image size if the crop rectangle size has
454 * been modified.
455 */
456 format = __mt9m032_get_pad_format(sensor, fh, crop->which);
457 format->width = rect.width;
458 format->height = rect.height;
459 }
460
461 *__crop = rect;
462 crop->rect = rect;
463
464 if (crop->which == V4L2_SUBDEV_FORMAT_ACTIVE)
465 ret = mt9m032_update_geom_timing(sensor);
466
467done:
468 mutex_unlock(&sensor->lock);
469 return ret;
470}
471
472static int mt9m032_get_frame_interval(struct v4l2_subdev *subdev,
473 struct v4l2_subdev_frame_interval *fi)
474{
475 struct mt9m032 *sensor = to_mt9m032(subdev);
476
477 mutex_lock(&sensor->lock);
478 memset(fi, 0, sizeof(*fi));
479 fi->interval = sensor->frame_interval;
480 mutex_unlock(&sensor->lock);
481
482 return 0;
483}
484
485static int mt9m032_set_frame_interval(struct v4l2_subdev *subdev,
486 struct v4l2_subdev_frame_interval *fi)
487{
488 struct mt9m032 *sensor = to_mt9m032(subdev);
489 int ret;
490
491 mutex_lock(&sensor->lock);
492
493 if (sensor->streaming) {
494 ret = -EBUSY;
495 goto done;
496 }
497
498 /* Avoid divisions by 0. */
499 if (fi->interval.denominator == 0)
500 fi->interval.denominator = 1;
501
502 ret = mt9m032_update_timing(sensor, &fi->interval);
503 if (!ret)
504 sensor->frame_interval = fi->interval;
505
506done:
507 mutex_unlock(&sensor->lock);
508 return ret;
509}
510
511static int mt9m032_s_stream(struct v4l2_subdev *subdev, int streaming)
512{
513 struct mt9m032 *sensor = to_mt9m032(subdev);
514 int ret;
515
516 mutex_lock(&sensor->lock);
517 ret = update_formatter2(sensor, streaming);
518 if (!ret)
519 sensor->streaming = streaming;
520 mutex_unlock(&sensor->lock);
521
522 return ret;
523}
524
525/* -----------------------------------------------------------------------------
526 * V4L2 subdev core operations
527 */
528
529#ifdef CONFIG_VIDEO_ADV_DEBUG
530static int mt9m032_g_register(struct v4l2_subdev *sd,
531 struct v4l2_dbg_register *reg)
532{
533 struct mt9m032 *sensor = to_mt9m032(sd);
534 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
535 int val;
536
537 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
538 return -EINVAL;
539 if (reg->match.addr != client->addr)
540 return -ENODEV;
541
542 val = mt9m032_read(client, reg->reg);
543 if (val < 0)
544 return -EIO;
545
546 reg->size = 2;
547 reg->val = val;
548
549 return 0;
550}
551
552static int mt9m032_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300553 const struct v4l2_dbg_register *reg)
Martin Hostettler251cb882011-12-17 07:10:55 -0300554{
555 struct mt9m032 *sensor = to_mt9m032(sd);
556 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
557
558 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
559 return -EINVAL;
560
561 if (reg->match.addr != client->addr)
562 return -ENODEV;
563
564 return mt9m032_write(client, reg->reg, reg->val);
565}
566#endif
567
568/* -----------------------------------------------------------------------------
569 * V4L2 subdev control operations
570 */
571
572static int update_read_mode2(struct mt9m032 *sensor, bool vflip, bool hflip)
573{
574 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
575 int reg_val = (vflip << MT9M032_READ_MODE2_VFLIP_SHIFT)
576 | (hflip << MT9M032_READ_MODE2_HFLIP_SHIFT)
577 | MT9M032_READ_MODE2_ROW_BLC
578 | 0x0007;
579
580 return mt9m032_write(client, MT9M032_READ_MODE2, reg_val);
581}
582
583static int mt9m032_set_gain(struct mt9m032 *sensor, s32 val)
584{
585 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
586 int digital_gain_val; /* in 1/8th (0..127) */
587 int analog_mul; /* 0 or 1 */
588 int analog_gain_val; /* in 1/16th. (0..63) */
589 u16 reg_val;
590
591 digital_gain_val = 51; /* from setup example */
592
593 if (val < 63) {
594 analog_mul = 0;
595 analog_gain_val = val;
596 } else {
597 analog_mul = 1;
598 analog_gain_val = val / 2;
599 }
600
601 /* a_gain = (1 + analog_mul) + (analog_gain_val + 1) / 16 */
602 /* overall_gain = a_gain * (1 + digital_gain_val / 8) */
603
604 reg_val = ((digital_gain_val & MT9M032_GAIN_DIGITAL_MASK)
605 << MT9M032_GAIN_DIGITAL_SHIFT)
606 | ((analog_mul & 1) << MT9M032_GAIN_AMUL_SHIFT)
607 | (analog_gain_val & MT9M032_GAIN_ANALOG_MASK);
608
609 return mt9m032_write(client, MT9M032_GAIN_ALL, reg_val);
610}
611
612static int mt9m032_try_ctrl(struct v4l2_ctrl *ctrl)
613{
614 if (ctrl->id == V4L2_CID_GAIN && ctrl->val >= 63) {
615 /* round because of multiplier used for values >= 63 */
616 ctrl->val &= ~1;
617 }
618
619 return 0;
620}
621
622static int mt9m032_set_ctrl(struct v4l2_ctrl *ctrl)
623{
624 struct mt9m032 *sensor =
625 container_of(ctrl->handler, struct mt9m032, ctrls);
626 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
627 int ret;
628
629 switch (ctrl->id) {
630 case V4L2_CID_GAIN:
631 return mt9m032_set_gain(sensor, ctrl->val);
632
633 case V4L2_CID_HFLIP:
634 /* case V4L2_CID_VFLIP: -- In the same cluster */
635 return update_read_mode2(sensor, sensor->vflip->val,
636 sensor->hflip->val);
637
638 case V4L2_CID_EXPOSURE:
639 ret = mt9m032_write(client, MT9M032_SHUTTER_WIDTH_HIGH,
640 (ctrl->val >> 16) & 0xffff);
641 if (ret < 0)
642 return ret;
643
644 return mt9m032_write(client, MT9M032_SHUTTER_WIDTH_LOW,
645 ctrl->val & 0xffff);
646 }
647
648 return 0;
649}
650
651static struct v4l2_ctrl_ops mt9m032_ctrl_ops = {
652 .s_ctrl = mt9m032_set_ctrl,
653 .try_ctrl = mt9m032_try_ctrl,
654};
655
656/* -------------------------------------------------------------------------- */
657
658static const struct v4l2_subdev_core_ops mt9m032_core_ops = {
659#ifdef CONFIG_VIDEO_ADV_DEBUG
660 .g_register = mt9m032_g_register,
661 .s_register = mt9m032_s_register,
662#endif
663};
664
665static const struct v4l2_subdev_video_ops mt9m032_video_ops = {
666 .s_stream = mt9m032_s_stream,
667 .g_frame_interval = mt9m032_get_frame_interval,
668 .s_frame_interval = mt9m032_set_frame_interval,
669};
670
671static const struct v4l2_subdev_pad_ops mt9m032_pad_ops = {
672 .enum_mbus_code = mt9m032_enum_mbus_code,
673 .enum_frame_size = mt9m032_enum_frame_size,
674 .get_fmt = mt9m032_get_pad_format,
675 .set_fmt = mt9m032_set_pad_format,
676 .set_crop = mt9m032_set_pad_crop,
677 .get_crop = mt9m032_get_pad_crop,
678};
679
680static const struct v4l2_subdev_ops mt9m032_ops = {
681 .core = &mt9m032_core_ops,
682 .video = &mt9m032_video_ops,
683 .pad = &mt9m032_pad_ops,
684};
685
686/* -----------------------------------------------------------------------------
687 * Driver initialization and probing
688 */
689
690static int mt9m032_probe(struct i2c_client *client,
691 const struct i2c_device_id *devid)
692{
Laurent Pinchart5472d3f2012-05-09 09:55:59 -0300693 struct mt9m032_platform_data *pdata = client->dev.platform_data;
Martin Hostettler251cb882011-12-17 07:10:55 -0300694 struct i2c_adapter *adapter = client->adapter;
695 struct mt9m032 *sensor;
696 int chip_version;
697 int ret;
698
Laurent Pinchart5472d3f2012-05-09 09:55:59 -0300699 if (pdata == NULL) {
700 dev_err(&client->dev, "No platform data\n");
701 return -EINVAL;
702 }
703
Martin Hostettler251cb882011-12-17 07:10:55 -0300704 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
705 dev_warn(&client->dev,
706 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
707 return -EIO;
708 }
709
710 if (!client->dev.platform_data)
711 return -ENODEV;
712
713 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
714 if (sensor == NULL)
715 return -ENOMEM;
716
717 mutex_init(&sensor->lock);
718
Laurent Pinchart5472d3f2012-05-09 09:55:59 -0300719 sensor->pdata = pdata;
Martin Hostettler251cb882011-12-17 07:10:55 -0300720
721 v4l2_i2c_subdev_init(&sensor->subdev, client, &mt9m032_ops);
722 sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
723
724 chip_version = mt9m032_read(client, MT9M032_CHIP_VERSION);
725 if (chip_version != MT9M032_CHIP_VERSION_VALUE) {
726 dev_err(&client->dev, "MT9M032 not detected, wrong version "
727 "0x%04x\n", chip_version);
728 ret = -ENODEV;
729 goto error_sensor;
730 }
731
732 dev_info(&client->dev, "MT9M032 detected at address 0x%02x\n",
733 client->addr);
734
735 sensor->frame_interval.numerator = 1;
736 sensor->frame_interval.denominator = 30;
737
738 sensor->crop.left = MT9M032_COLUMN_START_DEF;
739 sensor->crop.top = MT9M032_ROW_START_DEF;
740 sensor->crop.width = MT9M032_COLUMN_SIZE_DEF;
741 sensor->crop.height = MT9M032_ROW_SIZE_DEF;
742
743 sensor->format.width = sensor->crop.width;
744 sensor->format.height = sensor->crop.height;
745 sensor->format.code = V4L2_MBUS_FMT_Y8_1X8;
746 sensor->format.field = V4L2_FIELD_NONE;
747 sensor->format.colorspace = V4L2_COLORSPACE_SRGB;
748
Laurent Pinchart5472d3f2012-05-09 09:55:59 -0300749 v4l2_ctrl_handler_init(&sensor->ctrls, 5);
Martin Hostettler251cb882011-12-17 07:10:55 -0300750
751 v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
752 V4L2_CID_GAIN, 0, 127, 1, 64);
753
754 sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls,
755 &mt9m032_ctrl_ops,
756 V4L2_CID_HFLIP, 0, 1, 1, 0);
757 sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls,
758 &mt9m032_ctrl_ops,
759 V4L2_CID_VFLIP, 0, 1, 1, 0);
760
761 v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
762 V4L2_CID_EXPOSURE, MT9M032_SHUTTER_WIDTH_MIN,
763 MT9M032_SHUTTER_WIDTH_MAX, 1,
764 MT9M032_SHUTTER_WIDTH_DEF);
Laurent Pinchart5472d3f2012-05-09 09:55:59 -0300765 v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
766 V4L2_CID_PIXEL_RATE, pdata->pix_clock,
767 pdata->pix_clock, 1, pdata->pix_clock);
Martin Hostettler251cb882011-12-17 07:10:55 -0300768
769 if (sensor->ctrls.error) {
770 ret = sensor->ctrls.error;
771 dev_err(&client->dev, "control initialization error %d\n", ret);
772 goto error_ctrl;
773 }
774
775 v4l2_ctrl_cluster(2, &sensor->hflip);
776
777 sensor->subdev.ctrl_handler = &sensor->ctrls;
778 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
779 ret = media_entity_init(&sensor->subdev.entity, 1, &sensor->pad, 0);
780 if (ret < 0)
781 goto error_ctrl;
782
783 ret = mt9m032_write(client, MT9M032_RESET, 1); /* reset on */
784 if (ret < 0)
785 goto error_entity;
Julia Lawall612cd9e2012-08-18 17:25:56 -0300786 ret = mt9m032_write(client, MT9M032_RESET, 0); /* reset off */
Martin Hostettler251cb882011-12-17 07:10:55 -0300787 if (ret < 0)
788 goto error_entity;
789
790 ret = mt9m032_setup_pll(sensor);
791 if (ret < 0)
792 goto error_entity;
793 usleep_range(10000, 11000);
794
795 ret = v4l2_ctrl_handler_setup(&sensor->ctrls);
796 if (ret < 0)
797 goto error_entity;
798
799 /* SIZE */
800 ret = mt9m032_update_geom_timing(sensor);
801 if (ret < 0)
802 goto error_entity;
803
804 ret = mt9m032_write(client, 0x41, 0x0000); /* reserved !!! */
805 if (ret < 0)
806 goto error_entity;
807 ret = mt9m032_write(client, 0x42, 0x0003); /* reserved !!! */
808 if (ret < 0)
809 goto error_entity;
810 ret = mt9m032_write(client, 0x43, 0x0003); /* reserved !!! */
811 if (ret < 0)
812 goto error_entity;
813 ret = mt9m032_write(client, 0x7f, 0x0000); /* reserved !!! */
814 if (ret < 0)
815 goto error_entity;
816 if (sensor->pdata->invert_pixclock) {
817 ret = mt9m032_write(client, MT9M032_PIX_CLK_CTRL,
818 MT9M032_PIX_CLK_CTRL_INV_PIXCLK);
819 if (ret < 0)
820 goto error_entity;
821 }
822
823 ret = mt9m032_write(client, MT9M032_RESTART, 1); /* Restart on */
824 if (ret < 0)
825 goto error_entity;
826 msleep(100);
827 ret = mt9m032_write(client, MT9M032_RESTART, 0); /* Restart off */
828 if (ret < 0)
829 goto error_entity;
830 msleep(100);
831 ret = update_formatter2(sensor, false);
832 if (ret < 0)
833 goto error_entity;
834
835 return ret;
836
837error_entity:
838 media_entity_cleanup(&sensor->subdev.entity);
839error_ctrl:
840 v4l2_ctrl_handler_free(&sensor->ctrls);
841error_sensor:
842 mutex_destroy(&sensor->lock);
843 kfree(sensor);
844 return ret;
845}
846
847static int mt9m032_remove(struct i2c_client *client)
848{
849 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
850 struct mt9m032 *sensor = to_mt9m032(subdev);
851
Guennadi Liakhovetskib22b9f32012-04-18 05:00:52 -0300852 v4l2_device_unregister_subdev(subdev);
Martin Hostettler251cb882011-12-17 07:10:55 -0300853 v4l2_ctrl_handler_free(&sensor->ctrls);
Guennadi Liakhovetskib22b9f32012-04-18 05:00:52 -0300854 media_entity_cleanup(&subdev->entity);
Martin Hostettler251cb882011-12-17 07:10:55 -0300855 mutex_destroy(&sensor->lock);
856 kfree(sensor);
857 return 0;
858}
859
860static const struct i2c_device_id mt9m032_id_table[] = {
861 { MT9M032_NAME, 0 },
862 { }
863};
864
865MODULE_DEVICE_TABLE(i2c, mt9m032_id_table);
866
867static struct i2c_driver mt9m032_i2c_driver = {
868 .driver = {
869 .name = MT9M032_NAME,
870 },
871 .probe = mt9m032_probe,
872 .remove = mt9m032_remove,
873 .id_table = mt9m032_id_table,
874};
875
876module_i2c_driver(mt9m032_i2c_driver);
877
878MODULE_AUTHOR("Martin Hostettler <martin@neutronstar.dyndns.org>");
879MODULE_DESCRIPTION("MT9M032 camera sensor driver");
880MODULE_LICENSE("GPL v2");