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