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
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>
29 #include <media/i2c/mt9m032.h>
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
85 #define MT9M032_PLL_CONFIG1_PREDIV_MASK 0x3f
86 #define MT9M032_PLL_CONFIG1_MUL_SHIFT 8
87 #define MT9M032_READ_MODE1 0x1e
88 #define MT9M032_READ_MODE1_OUTPUT_BAD_FRAMES (1 << 13)
89 #define MT9M032_READ_MODE1_MAINTAIN_FRAME_RATE (1 << 12)
90 #define MT9M032_READ_MODE1_XOR_LINE_VALID (1 << 11)
91 #define MT9M032_READ_MODE1_CONT_LINE_VALID (1 << 10)
92 #define MT9M032_READ_MODE1_INVERT_TRIGGER (1 << 9)
93 #define MT9M032_READ_MODE1_SNAPSHOT (1 << 8)
94 #define MT9M032_READ_MODE1_GLOBAL_RESET (1 << 7)
95 #define MT9M032_READ_MODE1_BULB_EXPOSURE (1 << 6)
96 #define MT9M032_READ_MODE1_INVERT_STROBE (1 << 5)
97 #define MT9M032_READ_MODE1_STROBE_ENABLE (1 << 4)
98 #define MT9M032_READ_MODE1_STROBE_START_TRIG1 (0 << 2)
99 #define MT9M032_READ_MODE1_STROBE_START_EXP (1 << 2)
100 #define MT9M032_READ_MODE1_STROBE_START_SHUTTER (2 << 2)
101 #define MT9M032_READ_MODE1_STROBE_START_TRIG2 (3 << 2)
102 #define MT9M032_READ_MODE1_STROBE_END_TRIG1 (0 << 0)
103 #define MT9M032_READ_MODE1_STROBE_END_EXP (1 << 0)
104 #define MT9M032_READ_MODE1_STROBE_END_SHUTTER (2 << 0)
105 #define MT9M032_READ_MODE1_STROBE_END_TRIG2 (3 << 0)
106 #define MT9M032_READ_MODE2 0x20
107 #define MT9M032_READ_MODE2_VFLIP_SHIFT 15
108 #define MT9M032_READ_MODE2_HFLIP_SHIFT 14
109 #define MT9M032_READ_MODE2_ROW_BLC 0x40
110 #define MT9M032_GAIN_GREEN1 0x2b
111 #define MT9M032_GAIN_BLUE 0x2c
112 #define MT9M032_GAIN_RED 0x2d
113 #define MT9M032_GAIN_GREEN2 0x2e
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
122 #define MT9M032_FORMATTER1_PLL_P1_6 (1 << 8)
123 #define MT9M032_FORMATTER1_PARALLEL (1 << 12)
124 #define MT9M032_FORMATTER2 0x9f
125 #define MT9M032_FORMATTER2_DOUT_EN 0x1000
126 #define MT9M032_FORMATTER2_PIXCLK_EN 0x2000
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
139
140 struct 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
mt9m032_read(struct i2c_client * client,u8 reg)166 static int mt9m032_read(struct i2c_client *client, u8 reg)
167 {
168 return i2c_smbus_read_word_swapped(client, reg);
169 }
170
mt9m032_write(struct i2c_client * client,u8 reg,const u16 data)171 static 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
mt9m032_row_time(struct mt9m032 * sensor,unsigned int width)176 static 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
mt9m032_update_timing(struct mt9m032 * sensor,struct v4l2_fract * interval)187 static 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
mt9m032_update_geom_timing(struct mt9m032 * sensor)223 static 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
update_formatter2(struct mt9m032 * sensor,bool streaming)244 static 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
mt9m032_setup_pll(struct mt9m032 * sensor)257 static 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,
271 .p1_min = 6,
272 .p1_max = 7,
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;
278 u16 reg_val;
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,
291 (pll.m << MT9M032_PLL_CONFIG1_MUL_SHIFT) |
292 ((pll.n - 1) & MT9M032_PLL_CONFIG1_PREDIV_MASK));
293 if (!ret)
294 ret = mt9m032_write(client, MT9P031_PLL_CONTROL,
295 MT9P031_PLL_CONTROL_PWRON |
296 MT9P031_PLL_CONTROL_USEPLL);
297 if (!ret) /* more reserved, Continuous, Master Mode */
298 ret = mt9m032_write(client, MT9M032_READ_MODE1, 0x8000 |
299 MT9M032_READ_MODE1_STROBE_START_EXP |
300 MT9M032_READ_MODE1_STROBE_END_SHUTTER);
301 if (!ret) {
302 reg_val = (pll.p1 == 6 ? MT9M032_FORMATTER1_PLL_P1_6 : 0)
303 | MT9M032_FORMATTER1_PARALLEL | 0x001e; /* 14-bit */
304 ret = mt9m032_write(client, MT9M032_FORMATTER1, reg_val);
305 }
306
307 return ret;
308 }
309
310 /* -----------------------------------------------------------------------------
311 * Subdev pad operations
312 */
313
mt9m032_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)314 static int mt9m032_enum_mbus_code(struct v4l2_subdev *subdev,
315 struct v4l2_subdev_pad_config *cfg,
316 struct v4l2_subdev_mbus_code_enum *code)
317 {
318 if (code->index != 0)
319 return -EINVAL;
320
321 code->code = MEDIA_BUS_FMT_Y8_1X8;
322 return 0;
323 }
324
mt9m032_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)325 static int mt9m032_enum_frame_size(struct v4l2_subdev *subdev,
326 struct v4l2_subdev_pad_config *cfg,
327 struct v4l2_subdev_frame_size_enum *fse)
328 {
329 if (fse->index != 0 || fse->code != MEDIA_BUS_FMT_Y8_1X8)
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
343 * @cfg: v4l2_subdev_pad_config for getting the try crop rect from
344 * @which: select try or active crop rect
345 *
346 * Returns a pointer the current active or fh relative try crop rect
347 */
348 static struct v4l2_rect *
__mt9m032_get_pad_crop(struct mt9m032 * sensor,struct v4l2_subdev_pad_config * cfg,enum v4l2_subdev_format_whence which)349 __mt9m032_get_pad_crop(struct mt9m032 *sensor, struct v4l2_subdev_pad_config *cfg,
350 enum v4l2_subdev_format_whence which)
351 {
352 switch (which) {
353 case V4L2_SUBDEV_FORMAT_TRY:
354 return v4l2_subdev_get_try_crop(&sensor->subdev, cfg, 0);
355 case V4L2_SUBDEV_FORMAT_ACTIVE:
356 return &sensor->crop;
357 default:
358 return NULL;
359 }
360 }
361
362 /**
363 * __mt9m032_get_pad_format() - get format
364 * @sensor: pointer to the sensor struct
365 * @cfg: v4l2_subdev_pad_config for getting the try format from
366 * @which: select try or active format
367 *
368 * Returns a pointer the current active or fh relative try format
369 */
370 static struct v4l2_mbus_framefmt *
__mt9m032_get_pad_format(struct mt9m032 * sensor,struct v4l2_subdev_pad_config * cfg,enum v4l2_subdev_format_whence which)371 __mt9m032_get_pad_format(struct mt9m032 *sensor, struct v4l2_subdev_pad_config *cfg,
372 enum v4l2_subdev_format_whence which)
373 {
374 switch (which) {
375 case V4L2_SUBDEV_FORMAT_TRY:
376 return v4l2_subdev_get_try_format(&sensor->subdev, cfg, 0);
377 case V4L2_SUBDEV_FORMAT_ACTIVE:
378 return &sensor->format;
379 default:
380 return NULL;
381 }
382 }
383
mt9m032_get_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)384 static int mt9m032_get_pad_format(struct v4l2_subdev *subdev,
385 struct v4l2_subdev_pad_config *cfg,
386 struct v4l2_subdev_format *fmt)
387 {
388 struct mt9m032 *sensor = to_mt9m032(subdev);
389
390 mutex_lock(&sensor->lock);
391 fmt->format = *__mt9m032_get_pad_format(sensor, cfg, fmt->which);
392 mutex_unlock(&sensor->lock);
393
394 return 0;
395 }
396
mt9m032_set_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)397 static int mt9m032_set_pad_format(struct v4l2_subdev *subdev,
398 struct v4l2_subdev_pad_config *cfg,
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. */
412 fmt->format = *__mt9m032_get_pad_format(sensor, cfg, fmt->which);
413 ret = 0;
414
415 done:
416 mutex_unlock(&sensor->lock);
417 return ret;
418 }
419
mt9m032_get_pad_selection(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)420 static int mt9m032_get_pad_selection(struct v4l2_subdev *subdev,
421 struct v4l2_subdev_pad_config *cfg,
422 struct v4l2_subdev_selection *sel)
423 {
424 struct mt9m032 *sensor = to_mt9m032(subdev);
425
426 if (sel->target != V4L2_SEL_TGT_CROP)
427 return -EINVAL;
428
429 mutex_lock(&sensor->lock);
430 sel->r = *__mt9m032_get_pad_crop(sensor, cfg, sel->which);
431 mutex_unlock(&sensor->lock);
432
433 return 0;
434 }
435
mt9m032_set_pad_selection(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)436 static int mt9m032_set_pad_selection(struct v4l2_subdev *subdev,
437 struct v4l2_subdev_pad_config *cfg,
438 struct v4l2_subdev_selection *sel)
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
446 if (sel->target != V4L2_SEL_TGT_CROP)
447 return -EINVAL;
448
449 mutex_lock(&sensor->lock);
450
451 if (sensor->streaming && sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
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 */
459 rect.left = clamp(ALIGN(sel->r.left, 2), MT9M032_COLUMN_START_MIN,
460 MT9M032_COLUMN_START_MAX);
461 rect.top = clamp(ALIGN(sel->r.top, 2), MT9M032_ROW_START_MIN,
462 MT9M032_ROW_START_MAX);
463 rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
464 MT9M032_COLUMN_SIZE_MIN, MT9M032_COLUMN_SIZE_MAX);
465 rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
466 MT9M032_ROW_SIZE_MIN, MT9M032_ROW_SIZE_MAX);
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);
472
473 __crop = __mt9m032_get_pad_crop(sensor, cfg, sel->which);
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 */
479 format = __mt9m032_get_pad_format(sensor, cfg, sel->which);
480 format->width = rect.width;
481 format->height = rect.height;
482 }
483
484 *__crop = rect;
485 sel->r = rect;
486
487 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
488 ret = mt9m032_update_geom_timing(sensor);
489
490 done:
491 mutex_unlock(&sensor->lock);
492 return ret;
493 }
494
mt9m032_get_frame_interval(struct v4l2_subdev * subdev,struct v4l2_subdev_frame_interval * fi)495 static 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
mt9m032_set_frame_interval(struct v4l2_subdev * subdev,struct v4l2_subdev_frame_interval * fi)508 static 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
529 done:
530 mutex_unlock(&sensor->lock);
531 return ret;
532 }
533
mt9m032_s_stream(struct v4l2_subdev * subdev,int streaming)534 static 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
mt9m032_g_register(struct v4l2_subdev * sd,struct v4l2_dbg_register * reg)553 static 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
560 if (reg->reg > 0xff)
561 return -EINVAL;
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
mt9m032_s_register(struct v4l2_subdev * sd,const struct v4l2_dbg_register * reg)573 static int mt9m032_s_register(struct v4l2_subdev *sd,
574 const struct v4l2_dbg_register *reg)
575 {
576 struct mt9m032 *sensor = to_mt9m032(sd);
577 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
578
579 if (reg->reg > 0xff)
580 return -EINVAL;
581
582 return mt9m032_write(client, reg->reg, reg->val);
583 }
584 #endif
585
586 /* -----------------------------------------------------------------------------
587 * V4L2 subdev control operations
588 */
589
update_read_mode2(struct mt9m032 * sensor,bool vflip,bool hflip)590 static 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
mt9m032_set_gain(struct mt9m032 * sensor,s32 val)601 static 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
mt9m032_try_ctrl(struct v4l2_ctrl * ctrl)630 static 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
mt9m032_set_ctrl(struct v4l2_ctrl * ctrl)640 static 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
669 static const struct v4l2_ctrl_ops mt9m032_ctrl_ops = {
670 .s_ctrl = mt9m032_set_ctrl,
671 .try_ctrl = mt9m032_try_ctrl,
672 };
673
674 /* -------------------------------------------------------------------------- */
675
676 static const struct v4l2_subdev_core_ops mt9m032_core_ops = {
677 #ifdef CONFIG_VIDEO_ADV_DEBUG
678 .g_register = mt9m032_g_register,
679 .s_register = mt9m032_s_register,
680 #endif
681 };
682
683 static const struct v4l2_subdev_video_ops mt9m032_video_ops = {
684 .s_stream = mt9m032_s_stream,
685 .g_frame_interval = mt9m032_get_frame_interval,
686 .s_frame_interval = mt9m032_set_frame_interval,
687 };
688
689 static const struct v4l2_subdev_pad_ops mt9m032_pad_ops = {
690 .enum_mbus_code = mt9m032_enum_mbus_code,
691 .enum_frame_size = mt9m032_enum_frame_size,
692 .get_fmt = mt9m032_get_pad_format,
693 .set_fmt = mt9m032_set_pad_format,
694 .set_selection = mt9m032_set_pad_selection,
695 .get_selection = mt9m032_get_pad_selection,
696 };
697
698 static 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
mt9m032_probe(struct i2c_client * client,const struct i2c_device_id * devid)708 static int mt9m032_probe(struct i2c_client *client,
709 const struct i2c_device_id *devid)
710 {
711 struct mt9m032_platform_data *pdata = client->dev.platform_data;
712 struct i2c_adapter *adapter = client->adapter;
713 struct mt9m032 *sensor;
714 int chip_version;
715 int ret;
716
717 if (pdata == NULL) {
718 dev_err(&client->dev, "No platform data\n");
719 return -EINVAL;
720 }
721
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
731 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
732 if (sensor == NULL)
733 return -ENOMEM;
734
735 mutex_init(&sensor->lock);
736
737 sensor->pdata = pdata;
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;
763 sensor->format.code = MEDIA_BUS_FMT_Y8_1X8;
764 sensor->format.field = V4L2_FIELD_NONE;
765 sensor->format.colorspace = V4L2_COLORSPACE_SRGB;
766
767 v4l2_ctrl_handler_init(&sensor->ctrls, 5);
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);
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);
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->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
797 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
798 ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad);
799 if (ret < 0)
800 goto error_ctrl;
801
802 ret = mt9m032_write(client, MT9M032_RESET, 1); /* reset on */
803 if (ret < 0)
804 goto error_entity;
805 ret = mt9m032_write(client, MT9M032_RESET, 0); /* reset off */
806 if (ret < 0)
807 goto error_entity;
808
809 ret = mt9m032_setup_pll(sensor);
810 if (ret < 0)
811 goto error_entity;
812 usleep_range(10000, 11000);
813
814 ret = v4l2_ctrl_handler_setup(&sensor->ctrls);
815 if (ret < 0)
816 goto error_entity;
817
818 /* SIZE */
819 ret = mt9m032_update_geom_timing(sensor);
820 if (ret < 0)
821 goto error_entity;
822
823 ret = mt9m032_write(client, 0x41, 0x0000); /* reserved !!! */
824 if (ret < 0)
825 goto error_entity;
826 ret = mt9m032_write(client, 0x42, 0x0003); /* reserved !!! */
827 if (ret < 0)
828 goto error_entity;
829 ret = mt9m032_write(client, 0x43, 0x0003); /* reserved !!! */
830 if (ret < 0)
831 goto error_entity;
832 ret = mt9m032_write(client, 0x7f, 0x0000); /* reserved !!! */
833 if (ret < 0)
834 goto error_entity;
835 if (sensor->pdata->invert_pixclock) {
836 ret = mt9m032_write(client, MT9M032_PIX_CLK_CTRL,
837 MT9M032_PIX_CLK_CTRL_INV_PIXCLK);
838 if (ret < 0)
839 goto error_entity;
840 }
841
842 ret = mt9m032_write(client, MT9M032_RESTART, 1); /* Restart on */
843 if (ret < 0)
844 goto error_entity;
845 msleep(100);
846 ret = mt9m032_write(client, MT9M032_RESTART, 0); /* Restart off */
847 if (ret < 0)
848 goto error_entity;
849 msleep(100);
850 ret = update_formatter2(sensor, false);
851 if (ret < 0)
852 goto error_entity;
853
854 return ret;
855
856 error_entity:
857 media_entity_cleanup(&sensor->subdev.entity);
858 error_ctrl:
859 v4l2_ctrl_handler_free(&sensor->ctrls);
860 error_sensor:
861 mutex_destroy(&sensor->lock);
862 return ret;
863 }
864
mt9m032_remove(struct i2c_client * client)865 static int mt9m032_remove(struct i2c_client *client)
866 {
867 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
868 struct mt9m032 *sensor = to_mt9m032(subdev);
869
870 v4l2_device_unregister_subdev(subdev);
871 v4l2_ctrl_handler_free(&sensor->ctrls);
872 media_entity_cleanup(&subdev->entity);
873 mutex_destroy(&sensor->lock);
874 return 0;
875 }
876
877 static const struct i2c_device_id mt9m032_id_table[] = {
878 { MT9M032_NAME, 0 },
879 { }
880 };
881
882 MODULE_DEVICE_TABLE(i2c, mt9m032_id_table);
883
884 static struct i2c_driver mt9m032_i2c_driver = {
885 .driver = {
886 .name = MT9M032_NAME,
887 },
888 .probe = mt9m032_probe,
889 .remove = mt9m032_remove,
890 .id_table = mt9m032_id_table,
891 };
892
893 module_i2c_driver(mt9m032_i2c_driver);
894
895 MODULE_AUTHOR("Martin Hostettler <martin@neutronstar.dyndns.org>");
896 MODULE_DESCRIPTION("MT9M032 camera sensor driver");
897 MODULE_LICENSE("GPL v2");
898