]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/i2c/mt9m032.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / drivers / media / i2c / mt9m032.c
CommitLineData
251cb88e
MH
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.
251cb88e
MH
16 */
17
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>
27
28#include <media/media-entity.h>
b5dcee22 29#include <media/i2c/mt9m032.h>
251cb88e
MH
30#include <media/v4l2-ctrls.h>
31#include <media/v4l2-device.h>
32#include <media/v4l2-subdev.h>
33
34#include "aptina-pll.h"
35
36/*
37 * width and height include active boundary and black parts
38 *
39 * column 0- 15 active boundary
40 * column 16-1455 image
41 * column 1456-1471 active boundary
42 * column 1472-1599 black
43 *
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
49 */
50
51#define MT9M032_PIXEL_ARRAY_WIDTH 1600
52#define MT9M032_PIXEL_ARRAY_HEIGHT 1152
53
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
a54b80b1 85#define MT9M032_PLL_CONFIG1_PREDIV_MASK 0x3f
251cb88e
MH
86#define MT9M032_PLL_CONFIG1_MUL_SHIFT 8
87#define MT9M032_READ_MODE1 0x1e
73254c30
LP
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)
251cb88e
MH
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
114
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
a54b80b1
LP
122#define MT9M032_FORMATTER1_PLL_P1_6 (1 << 8)
123#define MT9M032_FORMATTER1_PARALLEL (1 << 12)
251cb88e
MH
124#define MT9M032_FORMATTER2 0x9f
125#define MT9M032_FORMATTER2_DOUT_EN 0x1000
126#define MT9M032_FORMATTER2_PIXCLK_EN 0x2000
127
128/*
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.
134 */
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
251cb88e
MH
139
140struct mt9m032 {
141 struct v4l2_subdev subdev;
142 struct media_pad pad;
143 struct mt9m032_platform_data *pdata;
144
145 unsigned int pix_clock;
146
147 struct v4l2_ctrl_handler ctrls;
148 struct {
149 struct v4l2_ctrl *hflip;
150 struct v4l2_ctrl *vflip;
151 };
152
153 struct mutex lock; /* Protects streaming, format, interval and crop */
154
155 bool streaming;
156
157 struct v4l2_mbus_framefmt format;
158 struct v4l2_rect crop;
159 struct v4l2_fract frame_interval;
160};
161
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)
165
166static int mt9m032_read(struct i2c_client *client, u8 reg)
167{
168 return i2c_smbus_read_word_swapped(client, reg);
169}
170
171static int mt9m032_write(struct i2c_client *client, u8 reg, const u16 data)
172{
173 return i2c_smbus_write_word_swapped(client, reg, data);
174}
175
176static u32 mt9m032_row_time(struct mt9m032 *sensor, unsigned int width)
177{
178 unsigned int effective_width;
179 u32 ns;
180
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;
185}
186
187static int mt9m032_update_timing(struct mt9m032 *sensor,
188 struct v4l2_fract *interval)
189{
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;
195
196 if (!interval)
197 interval = &sensor->frame_interval;
198
199 row_time = mt9m032_row_time(sensor, crop->width);
200
201 vblank = div_u64(1000000000ULL * interval->numerator,
202 (u64)row_time * interval->denominator)
203 - crop->height;
204
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;
215 }
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);
219
220 return mt9m032_write(client, MT9M032_VBLANK, vblank);
221}
222
223static int mt9m032_update_geom_timing(struct mt9m032 *sensor)
224{
225 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
226 int ret;
227
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;
242}
243
244static int update_formatter2(struct mt9m032 *sensor, bool streaming)
245{
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 */
250
251 if (streaming)
252 reg_val |= MT9M032_FORMATTER2_PIXCLK_EN; /* pixclock enable */
253
254 return mt9m032_write(client, MT9M032_FORMATTER2, reg_val);
255}
256
257static int mt9m032_setup_pll(struct mt9m032 *sensor)
258{
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,
a54b80b1
LP
271 .p1_min = 6,
272 .p1_max = 7,
251cb88e
MH
273 };
274
275 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
276 struct mt9m032_platform_data *pdata = sensor->pdata;
277 struct aptina_pll pll;
a54b80b1 278 u16 reg_val;
251cb88e
MH
279 int ret;
280
281 pll.ext_clock = pdata->ext_clock;
282 pll.pix_clock = pdata->pix_clock;
283
284 ret = aptina_pll_calculate(&client->dev, &limits, &pll);
285 if (ret < 0)
286 return ret;
287
288 sensor->pix_clock = pdata->pix_clock;
289
290 ret = mt9m032_write(client, MT9M032_PLL_CONFIG1,
a54b80b1
LP
291 (pll.m << MT9M032_PLL_CONFIG1_MUL_SHIFT) |
292 ((pll.n - 1) & MT9M032_PLL_CONFIG1_PREDIV_MASK));
251cb88e
MH
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 */
73254c30
LP
298 ret = mt9m032_write(client, MT9M032_READ_MODE1, 0x8000 |
299 MT9M032_READ_MODE1_STROBE_START_EXP |
300 MT9M032_READ_MODE1_STROBE_END_SHUTTER);
a54b80b1
LP
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);
305 }
251cb88e
MH
306
307 return ret;
308}
309
310/* -----------------------------------------------------------------------------
311 * Subdev pad operations
312 */
313
314static int mt9m032_enum_mbus_code(struct v4l2_subdev *subdev,
f7234138 315 struct v4l2_subdev_pad_config *cfg,
251cb88e
MH
316 struct v4l2_subdev_mbus_code_enum *code)
317{
318 if (code->index != 0)
319 return -EINVAL;
320
f5fe58fd 321 code->code = MEDIA_BUS_FMT_Y8_1X8;
251cb88e
MH
322 return 0;
323}
324
325static int mt9m032_enum_frame_size(struct v4l2_subdev *subdev,
f7234138 326 struct v4l2_subdev_pad_config *cfg,
251cb88e
MH
327 struct v4l2_subdev_frame_size_enum *fse)
328{
f5fe58fd 329 if (fse->index != 0 || fse->code != MEDIA_BUS_FMT_Y8_1X8)
251cb88e
MH
330 return -EINVAL;
331
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;
336
337 return 0;
338}
339
340/**
341 * __mt9m032_get_pad_crop() - get crop rect
342 * @sensor: pointer to the sensor struct
f7234138 343 * @cfg: v4l2_subdev_pad_config for getting the try crop rect from
251cb88e
MH
344 * @which: select try or active crop rect
345 *
346 * Returns a pointer the current active or fh relative try crop rect
347 */
348static struct v4l2_rect *
f7234138 349__mt9m032_get_pad_crop(struct mt9m032 *sensor, struct v4l2_subdev_pad_config *cfg,
251cb88e
MH
350 enum v4l2_subdev_format_whence which)
351{
352 switch (which) {
353 case V4L2_SUBDEV_FORMAT_TRY:
f7234138 354 return v4l2_subdev_get_try_crop(&sensor->subdev, cfg, 0);
251cb88e
MH
355 case V4L2_SUBDEV_FORMAT_ACTIVE:
356 return &sensor->crop;
357 default:
358 return NULL;
359 }
360}
361
362/**
363 * __mt9m032_get_pad_format() - get format
364 * @sensor: pointer to the sensor struct
f7234138 365 * @cfg: v4l2_subdev_pad_config for getting the try format from
251cb88e
MH
366 * @which: select try or active format
367 *
368 * Returns a pointer the current active or fh relative try format
369 */
370static struct v4l2_mbus_framefmt *
f7234138 371__mt9m032_get_pad_format(struct mt9m032 *sensor, struct v4l2_subdev_pad_config *cfg,
251cb88e
MH
372 enum v4l2_subdev_format_whence which)
373{
374 switch (which) {
375 case V4L2_SUBDEV_FORMAT_TRY:
f7234138 376 return v4l2_subdev_get_try_format(&sensor->subdev, cfg, 0);
251cb88e
MH
377 case V4L2_SUBDEV_FORMAT_ACTIVE:
378 return &sensor->format;
379 default:
380 return NULL;
381 }
382}
383
384static int mt9m032_get_pad_format(struct v4l2_subdev *subdev,
f7234138 385 struct v4l2_subdev_pad_config *cfg,
251cb88e
MH
386 struct v4l2_subdev_format *fmt)
387{
388 struct mt9m032 *sensor = to_mt9m032(subdev);
389
390 mutex_lock(&sensor->lock);
f7234138 391 fmt->format = *__mt9m032_get_pad_format(sensor, cfg, fmt->which);
251cb88e
MH
392 mutex_unlock(&sensor->lock);
393
394 return 0;
395}
396
397static int mt9m032_set_pad_format(struct v4l2_subdev *subdev,
f7234138 398 struct v4l2_subdev_pad_config *cfg,
251cb88e
MH
399 struct v4l2_subdev_format *fmt)
400{
401 struct mt9m032 *sensor = to_mt9m032(subdev);
402 int ret;
403
404 mutex_lock(&sensor->lock);
405
406 if (sensor->streaming && fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
407 ret = -EBUSY;
408 goto done;
409 }
410
411 /* Scaling is not supported, the format is thus fixed. */
f7234138 412 fmt->format = *__mt9m032_get_pad_format(sensor, cfg, fmt->which);
b1e1179c 413 ret = 0;
251cb88e
MH
414
415done:
b1e1179c 416 mutex_unlock(&sensor->lock);
251cb88e
MH
417 return ret;
418}
419
1a023feb 420static int mt9m032_get_pad_selection(struct v4l2_subdev *subdev,
f7234138 421 struct v4l2_subdev_pad_config *cfg,
1a023feb 422 struct v4l2_subdev_selection *sel)
251cb88e
MH
423{
424 struct mt9m032 *sensor = to_mt9m032(subdev);
425
1a023feb
HV
426 if (sel->target != V4L2_SEL_TGT_CROP)
427 return -EINVAL;
428
251cb88e 429 mutex_lock(&sensor->lock);
f7234138 430 sel->r = *__mt9m032_get_pad_crop(sensor, cfg, sel->which);
251cb88e
MH
431 mutex_unlock(&sensor->lock);
432
433 return 0;
434}
435
1a023feb 436static int mt9m032_set_pad_selection(struct v4l2_subdev *subdev,
f7234138 437 struct v4l2_subdev_pad_config *cfg,
1a023feb 438 struct v4l2_subdev_selection *sel)
251cb88e
MH
439{
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;
445
1a023feb
HV
446 if (sel->target != V4L2_SEL_TGT_CROP)
447 return -EINVAL;
448
251cb88e
MH
449 mutex_lock(&sensor->lock);
450
1a023feb 451 if (sensor->streaming && sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
251cb88e
MH
452 ret = -EBUSY;
453 goto done;
454 }
455
456 /* Clamp the crop rectangle boundaries and align them to a multiple of 2
457 * pixels to ensure a GRBG Bayer pattern.
458 */
1a023feb 459 rect.left = clamp(ALIGN(sel->r.left, 2), MT9M032_COLUMN_START_MIN,
251cb88e 460 MT9M032_COLUMN_START_MAX);
1a023feb 461 rect.top = clamp(ALIGN(sel->r.top, 2), MT9M032_ROW_START_MIN,
251cb88e 462 MT9M032_ROW_START_MAX);
1a023feb 463 rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
f90580ca 464 MT9M032_COLUMN_SIZE_MIN, MT9M032_COLUMN_SIZE_MAX);
1a023feb 465 rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
f90580ca
RRD
466 MT9M032_ROW_SIZE_MIN, MT9M032_ROW_SIZE_MAX);
467
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);
251cb88e 472
f7234138 473 __crop = __mt9m032_get_pad_crop(sensor, cfg, sel->which);
251cb88e
MH
474
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.
478 */
f7234138 479 format = __mt9m032_get_pad_format(sensor, cfg, sel->which);
251cb88e
MH
480 format->width = rect.width;
481 format->height = rect.height;
482 }
483
484 *__crop = rect;
1a023feb 485 sel->r = rect;
251cb88e 486
1a023feb 487 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
251cb88e
MH
488 ret = mt9m032_update_geom_timing(sensor);
489
490done:
491 mutex_unlock(&sensor->lock);
492 return ret;
493}
494
495static int mt9m032_get_frame_interval(struct v4l2_subdev *subdev,
496 struct v4l2_subdev_frame_interval *fi)
497{
498 struct mt9m032 *sensor = to_mt9m032(subdev);
499
500 mutex_lock(&sensor->lock);
501 memset(fi, 0, sizeof(*fi));
502 fi->interval = sensor->frame_interval;
503 mutex_unlock(&sensor->lock);
504
505 return 0;
506}
507
508static int mt9m032_set_frame_interval(struct v4l2_subdev *subdev,
509 struct v4l2_subdev_frame_interval *fi)
510{
511 struct mt9m032 *sensor = to_mt9m032(subdev);
512 int ret;
513
514 mutex_lock(&sensor->lock);
515
516 if (sensor->streaming) {
517 ret = -EBUSY;
518 goto done;
519 }
520
521 /* Avoid divisions by 0. */
522 if (fi->interval.denominator == 0)
523 fi->interval.denominator = 1;
524
525 ret = mt9m032_update_timing(sensor, &fi->interval);
526 if (!ret)
527 sensor->frame_interval = fi->interval;
528
529done:
530 mutex_unlock(&sensor->lock);
531 return ret;
532}
533
534static int mt9m032_s_stream(struct v4l2_subdev *subdev, int streaming)
535{
536 struct mt9m032 *sensor = to_mt9m032(subdev);
537 int ret;
538
539 mutex_lock(&sensor->lock);
540 ret = update_formatter2(sensor, streaming);
541 if (!ret)
542 sensor->streaming = streaming;
543 mutex_unlock(&sensor->lock);
544
545 return ret;
546}
547
548/* -----------------------------------------------------------------------------
549 * V4L2 subdev core operations
550 */
551
552#ifdef CONFIG_VIDEO_ADV_DEBUG
553static int mt9m032_g_register(struct v4l2_subdev *sd,
554 struct v4l2_dbg_register *reg)
555{
556 struct mt9m032 *sensor = to_mt9m032(sd);
557 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
558 int val;
559
e1277110 560 if (reg->reg > 0xff)
251cb88e 561 return -EINVAL;
251cb88e
MH
562
563 val = mt9m032_read(client, reg->reg);
564 if (val < 0)
565 return -EIO;
566
567 reg->size = 2;
568 reg->val = val;
569
570 return 0;
571}
572
573static int mt9m032_s_register(struct v4l2_subdev *sd,
977ba3b1 574 const struct v4l2_dbg_register *reg)
251cb88e
MH
575{
576 struct mt9m032 *sensor = to_mt9m032(sd);
577 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
578
e1277110 579 if (reg->reg > 0xff)
251cb88e
MH
580 return -EINVAL;
581
251cb88e
MH
582 return mt9m032_write(client, reg->reg, reg->val);
583}
584#endif
585
586/* -----------------------------------------------------------------------------
587 * V4L2 subdev control operations
588 */
589
590static int update_read_mode2(struct mt9m032 *sensor, bool vflip, bool hflip)
591{
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;
597
598 return mt9m032_write(client, MT9M032_READ_MODE2, reg_val);
599}
600
601static int mt9m032_set_gain(struct mt9m032 *sensor, s32 val)
602{
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;
608
609 digital_gain_val = 51; /* from setup example */
610
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;
617 }
618
619 /* a_gain = (1 + analog_mul) + (analog_gain_val + 1) / 16 */
620 /* overall_gain = a_gain * (1 + digital_gain_val / 8) */
621
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);
626
627 return mt9m032_write(client, MT9M032_GAIN_ALL, reg_val);
628}
629
630static int mt9m032_try_ctrl(struct v4l2_ctrl *ctrl)
631{
632 if (ctrl->id == V4L2_CID_GAIN && ctrl->val >= 63) {
633 /* round because of multiplier used for values >= 63 */
634 ctrl->val &= ~1;
635 }
636
637 return 0;
638}
639
640static int mt9m032_set_ctrl(struct v4l2_ctrl *ctrl)
641{
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;
646
647 switch (ctrl->id) {
648 case V4L2_CID_GAIN:
649 return mt9m032_set_gain(sensor, ctrl->val);
650
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);
655
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;
661
662 return mt9m032_write(client, MT9M032_SHUTTER_WIDTH_LOW,
663 ctrl->val & 0xffff);
664 }
665
666 return 0;
667}
668
217bdb07 669static const struct v4l2_ctrl_ops mt9m032_ctrl_ops = {
251cb88e
MH
670 .s_ctrl = mt9m032_set_ctrl,
671 .try_ctrl = mt9m032_try_ctrl,
672};
673
674/* -------------------------------------------------------------------------- */
675
676static 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
681};
682
683static 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,
687};
688
689static 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,
1a023feb
HV
694 .set_selection = mt9m032_set_pad_selection,
695 .get_selection = mt9m032_get_pad_selection,
251cb88e
MH
696};
697
698static const struct v4l2_subdev_ops mt9m032_ops = {
699 .core = &mt9m032_core_ops,
700 .video = &mt9m032_video_ops,
701 .pad = &mt9m032_pad_ops,
702};
703
704/* -----------------------------------------------------------------------------
705 * Driver initialization and probing
706 */
707
708static int mt9m032_probe(struct i2c_client *client,
709 const struct i2c_device_id *devid)
710{
5472d3f1 711 struct mt9m032_platform_data *pdata = client->dev.platform_data;
251cb88e
MH
712 struct i2c_adapter *adapter = client->adapter;
713 struct mt9m032 *sensor;
714 int chip_version;
715 int ret;
716
5472d3f1
LP
717 if (pdata == NULL) {
718 dev_err(&client->dev, "No platform data\n");
719 return -EINVAL;
720 }
721
251cb88e
MH
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;
726 }
727
728 if (!client->dev.platform_data)
729 return -ENODEV;
730
c02b211d 731 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
251cb88e
MH
732 if (sensor == NULL)
733 return -ENOMEM;
734
735 mutex_init(&sensor->lock);
736
5472d3f1 737 sensor->pdata = pdata;
251cb88e
MH
738
739 v4l2_i2c_subdev_init(&sensor->subdev, client, &mt9m032_ops);
740 sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
741
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;
748 }
749
750 dev_info(&client->dev, "MT9M032 detected at address 0x%02x\n",
751 client->addr);
752
753 sensor->frame_interval.numerator = 1;
754 sensor->frame_interval.denominator = 30;
755
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;
760
761 sensor->format.width = sensor->crop.width;
762 sensor->format.height = sensor->crop.height;
f5fe58fd 763 sensor->format.code = MEDIA_BUS_FMT_Y8_1X8;
251cb88e
MH
764 sensor->format.field = V4L2_FIELD_NONE;
765 sensor->format.colorspace = V4L2_COLORSPACE_SRGB;
766
5472d3f1 767 v4l2_ctrl_handler_init(&sensor->ctrls, 5);
251cb88e
MH
768
769 v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
770 V4L2_CID_GAIN, 0, 127, 1, 64);
771
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);
778
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);
5472d3f1
LP
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);
251cb88e
MH
786
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;
791 }
792
793 v4l2_ctrl_cluster(2, &sensor->hflip);
794
795 sensor->subdev.ctrl_handler = &sensor->ctrls;
796 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
ab22e77c 797 ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad);
251cb88e
MH
798 if (ret < 0)
799 goto error_ctrl;
800
801 ret = mt9m032_write(client, MT9M032_RESET, 1); /* reset on */
802 if (ret < 0)
803 goto error_entity;
612cd9e8 804 ret = mt9m032_write(client, MT9M032_RESET, 0); /* reset off */
251cb88e
MH
805 if (ret < 0)
806 goto error_entity;
807
808 ret = mt9m032_setup_pll(sensor);
809 if (ret < 0)
810 goto error_entity;
811 usleep_range(10000, 11000);
812
813 ret = v4l2_ctrl_handler_setup(&sensor->ctrls);
814 if (ret < 0)
815 goto error_entity;
816
817 /* SIZE */
818 ret = mt9m032_update_geom_timing(sensor);
819 if (ret < 0)
820 goto error_entity;
821
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;
839 }
840
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;
852
853 return ret;
854
855error_entity:
856 media_entity_cleanup(&sensor->subdev.entity);
857error_ctrl:
858 v4l2_ctrl_handler_free(&sensor->ctrls);
859error_sensor:
860 mutex_destroy(&sensor->lock);
251cb88e
MH
861 return ret;
862}
863
864static int mt9m032_remove(struct i2c_client *client)
865{
866 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
867 struct mt9m032 *sensor = to_mt9m032(subdev);
868
b22b9f32 869 v4l2_device_unregister_subdev(subdev);
251cb88e 870 v4l2_ctrl_handler_free(&sensor->ctrls);
b22b9f32 871 media_entity_cleanup(&subdev->entity);
251cb88e 872 mutex_destroy(&sensor->lock);
251cb88e
MH
873 return 0;
874}
875
876static const struct i2c_device_id mt9m032_id_table[] = {
877 { MT9M032_NAME, 0 },
878 { }
879};
880
881MODULE_DEVICE_TABLE(i2c, mt9m032_id_table);
882
883static struct i2c_driver mt9m032_i2c_driver = {
884 .driver = {
885 .name = MT9M032_NAME,
886 },
887 .probe = mt9m032_probe,
888 .remove = mt9m032_remove,
889 .id_table = mt9m032_id_table,
890};
891
892module_i2c_driver(mt9m032_i2c_driver);
893
894MODULE_AUTHOR("Martin Hostettler <martin@neutronstar.dyndns.org>");
895MODULE_DESCRIPTION("MT9M032 camera sensor driver");
896MODULE_LICENSE("GPL v2");