[go: nahoru, domu]

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/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_fh *fh,
321				  struct v4l2_subdev_mbus_code_enum *code)
322{
323	if (code->index != 0)
324		return -EINVAL;
325
326	code->code = V4L2_MBUS_FMT_Y8_1X8;
327	return 0;
328}
329
330static int mt9m032_enum_frame_size(struct v4l2_subdev *subdev,
331				   struct v4l2_subdev_fh *fh,
332				   struct v4l2_subdev_frame_size_enum *fse)
333{
334	if (fse->index != 0 || fse->code != V4L2_MBUS_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 * @fh: file handle 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_fh *fh,
355		       enum v4l2_subdev_format_whence which)
356{
357	switch (which) {
358	case V4L2_SUBDEV_FORMAT_TRY:
359		return v4l2_subdev_get_try_crop(fh, 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 * @fh: file handle 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_fh *fh,
377			 enum v4l2_subdev_format_whence which)
378{
379	switch (which) {
380	case V4L2_SUBDEV_FORMAT_TRY:
381		return v4l2_subdev_get_try_format(fh, 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_fh *fh,
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, fh, 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_fh *fh,
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, fh, fmt->which);
418	ret = 0;
419
420done:
421	mutex_unlock(&sensor->lock);
422	return ret;
423}
424
425static int mt9m032_get_pad_crop(struct v4l2_subdev *subdev,
426				struct v4l2_subdev_fh *fh,
427				struct v4l2_subdev_crop *crop)
428{
429	struct mt9m032 *sensor = to_mt9m032(subdev);
430
431	mutex_lock(&sensor->lock);
432	crop->rect = *__mt9m032_get_pad_crop(sensor, fh, crop->which);
433	mutex_unlock(&sensor->lock);
434
435	return 0;
436}
437
438static int mt9m032_set_pad_crop(struct v4l2_subdev *subdev,
439				struct v4l2_subdev_fh *fh,
440				struct v4l2_subdev_crop *crop)
441{
442	struct mt9m032 *sensor = to_mt9m032(subdev);
443	struct v4l2_mbus_framefmt *format;
444	struct v4l2_rect *__crop;
445	struct v4l2_rect rect;
446	int ret = 0;
447
448	mutex_lock(&sensor->lock);
449
450	if (sensor->streaming && crop->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
451		ret = -EBUSY;
452		goto done;
453	}
454
455	/* Clamp the crop rectangle boundaries and align them to a multiple of 2
456	 * pixels to ensure a GRBG Bayer pattern.
457	 */
458	rect.left = clamp(ALIGN(crop->rect.left, 2), MT9M032_COLUMN_START_MIN,
459			  MT9M032_COLUMN_START_MAX);
460	rect.top = clamp(ALIGN(crop->rect.top, 2), MT9M032_ROW_START_MIN,
461			 MT9M032_ROW_START_MAX);
462	rect.width = clamp_t(unsigned int, ALIGN(crop->rect.width, 2),
463			     MT9M032_COLUMN_SIZE_MIN, MT9M032_COLUMN_SIZE_MAX);
464	rect.height = clamp_t(unsigned int, ALIGN(crop->rect.height, 2),
465			      MT9M032_ROW_SIZE_MIN, MT9M032_ROW_SIZE_MAX);
466
467	rect.width = min_t(unsigned int, rect.width,
468			   MT9M032_PIXEL_ARRAY_WIDTH - rect.left);
469	rect.height = min_t(unsigned int, rect.height,
470			    MT9M032_PIXEL_ARRAY_HEIGHT - rect.top);
471
472	__crop = __mt9m032_get_pad_crop(sensor, fh, crop->which);
473
474	if (rect.width != __crop->width || rect.height != __crop->height) {
475		/* Reset the output image size if the crop rectangle size has
476		 * been modified.
477		 */
478		format = __mt9m032_get_pad_format(sensor, fh, crop->which);
479		format->width = rect.width;
480		format->height = rect.height;
481	}
482
483	*__crop = rect;
484	crop->rect = rect;
485
486	if (crop->which == V4L2_SUBDEV_FORMAT_ACTIVE)
487		ret = mt9m032_update_geom_timing(sensor);
488
489done:
490	mutex_unlock(&sensor->lock);
491	return ret;
492}
493
494static int mt9m032_get_frame_interval(struct v4l2_subdev *subdev,
495				      struct v4l2_subdev_frame_interval *fi)
496{
497	struct mt9m032 *sensor = to_mt9m032(subdev);
498
499	mutex_lock(&sensor->lock);
500	memset(fi, 0, sizeof(*fi));
501	fi->interval = sensor->frame_interval;
502	mutex_unlock(&sensor->lock);
503
504	return 0;
505}
506
507static int mt9m032_set_frame_interval(struct v4l2_subdev *subdev,
508				      struct v4l2_subdev_frame_interval *fi)
509{
510	struct mt9m032 *sensor = to_mt9m032(subdev);
511	int ret;
512
513	mutex_lock(&sensor->lock);
514
515	if (sensor->streaming) {
516		ret = -EBUSY;
517		goto done;
518	}
519
520	/* Avoid divisions by 0. */
521	if (fi->interval.denominator == 0)
522		fi->interval.denominator = 1;
523
524	ret = mt9m032_update_timing(sensor, &fi->interval);
525	if (!ret)
526		sensor->frame_interval = fi->interval;
527
528done:
529	mutex_unlock(&sensor->lock);
530	return ret;
531}
532
533static int mt9m032_s_stream(struct v4l2_subdev *subdev, int streaming)
534{
535	struct mt9m032 *sensor = to_mt9m032(subdev);
536	int ret;
537
538	mutex_lock(&sensor->lock);
539	ret = update_formatter2(sensor, streaming);
540	if (!ret)
541		sensor->streaming = streaming;
542	mutex_unlock(&sensor->lock);
543
544	return ret;
545}
546
547/* -----------------------------------------------------------------------------
548 * V4L2 subdev core operations
549 */
550
551#ifdef CONFIG_VIDEO_ADV_DEBUG
552static int mt9m032_g_register(struct v4l2_subdev *sd,
553			      struct v4l2_dbg_register *reg)
554{
555	struct mt9m032 *sensor = to_mt9m032(sd);
556	struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
557	int val;
558
559	if (reg->reg > 0xff)
560		return -EINVAL;
561
562	val = mt9m032_read(client, reg->reg);
563	if (val < 0)
564		return -EIO;
565
566	reg->size = 2;
567	reg->val = val;
568
569	return 0;
570}
571
572static int mt9m032_s_register(struct v4l2_subdev *sd,
573			      const struct v4l2_dbg_register *reg)
574{
575	struct mt9m032 *sensor = to_mt9m032(sd);
576	struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
577
578	if (reg->reg > 0xff)
579		return -EINVAL;
580
581	return mt9m032_write(client, reg->reg, reg->val);
582}
583#endif
584
585/* -----------------------------------------------------------------------------
586 * V4L2 subdev control operations
587 */
588
589static int update_read_mode2(struct mt9m032 *sensor, bool vflip, bool hflip)
590{
591	struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
592	int reg_val = (vflip << MT9M032_READ_MODE2_VFLIP_SHIFT)
593		    | (hflip << MT9M032_READ_MODE2_HFLIP_SHIFT)
594		    | MT9M032_READ_MODE2_ROW_BLC
595		    | 0x0007;
596
597	return mt9m032_write(client, MT9M032_READ_MODE2, reg_val);
598}
599
600static int mt9m032_set_gain(struct mt9m032 *sensor, s32 val)
601{
602	struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
603	int digital_gain_val;	/* in 1/8th (0..127) */
604	int analog_mul;		/* 0 or 1 */
605	int analog_gain_val;	/* in 1/16th. (0..63) */
606	u16 reg_val;
607
608	digital_gain_val = 51; /* from setup example */
609
610	if (val < 63) {
611		analog_mul = 0;
612		analog_gain_val = val;
613	} else {
614		analog_mul = 1;
615		analog_gain_val = val / 2;
616	}
617
618	/* a_gain = (1 + analog_mul) + (analog_gain_val + 1) / 16 */
619	/* overall_gain = a_gain * (1 + digital_gain_val / 8) */
620
621	reg_val = ((digital_gain_val & MT9M032_GAIN_DIGITAL_MASK)
622		   << MT9M032_GAIN_DIGITAL_SHIFT)
623		| ((analog_mul & 1) << MT9M032_GAIN_AMUL_SHIFT)
624		| (analog_gain_val & MT9M032_GAIN_ANALOG_MASK);
625
626	return mt9m032_write(client, MT9M032_GAIN_ALL, reg_val);
627}
628
629static int mt9m032_try_ctrl(struct v4l2_ctrl *ctrl)
630{
631	if (ctrl->id == V4L2_CID_GAIN && ctrl->val >= 63) {
632		/* round because of multiplier used for values >= 63 */
633		ctrl->val &= ~1;
634	}
635
636	return 0;
637}
638
639static int mt9m032_set_ctrl(struct v4l2_ctrl *ctrl)
640{
641	struct mt9m032 *sensor =
642		container_of(ctrl->handler, struct mt9m032, ctrls);
643	struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
644	int ret;
645
646	switch (ctrl->id) {
647	case V4L2_CID_GAIN:
648		return mt9m032_set_gain(sensor, ctrl->val);
649
650	case V4L2_CID_HFLIP:
651	/* case V4L2_CID_VFLIP: -- In the same cluster */
652		return update_read_mode2(sensor, sensor->vflip->val,
653					 sensor->hflip->val);
654
655	case V4L2_CID_EXPOSURE:
656		ret = mt9m032_write(client, MT9M032_SHUTTER_WIDTH_HIGH,
657				    (ctrl->val >> 16) & 0xffff);
658		if (ret < 0)
659			return ret;
660
661		return mt9m032_write(client, MT9M032_SHUTTER_WIDTH_LOW,
662				     ctrl->val & 0xffff);
663	}
664
665	return 0;
666}
667
668static struct v4l2_ctrl_ops mt9m032_ctrl_ops = {
669	.s_ctrl = mt9m032_set_ctrl,
670	.try_ctrl = mt9m032_try_ctrl,
671};
672
673/* -------------------------------------------------------------------------- */
674
675static const struct v4l2_subdev_core_ops mt9m032_core_ops = {
676#ifdef CONFIG_VIDEO_ADV_DEBUG
677	.g_register = mt9m032_g_register,
678	.s_register = mt9m032_s_register,
679#endif
680};
681
682static const struct v4l2_subdev_video_ops mt9m032_video_ops = {
683	.s_stream = mt9m032_s_stream,
684	.g_frame_interval = mt9m032_get_frame_interval,
685	.s_frame_interval = mt9m032_set_frame_interval,
686};
687
688static const struct v4l2_subdev_pad_ops mt9m032_pad_ops = {
689	.enum_mbus_code = mt9m032_enum_mbus_code,
690	.enum_frame_size = mt9m032_enum_frame_size,
691	.get_fmt = mt9m032_get_pad_format,
692	.set_fmt = mt9m032_set_pad_format,
693	.set_crop = mt9m032_set_pad_crop,
694	.get_crop = mt9m032_get_pad_crop,
695};
696
697static const struct v4l2_subdev_ops mt9m032_ops = {
698	.core = &mt9m032_core_ops,
699	.video = &mt9m032_video_ops,
700	.pad = &mt9m032_pad_ops,
701};
702
703/* -----------------------------------------------------------------------------
704 * Driver initialization and probing
705 */
706
707static int mt9m032_probe(struct i2c_client *client,
708			 const struct i2c_device_id *devid)
709{
710	struct mt9m032_platform_data *pdata = client->dev.platform_data;
711	struct i2c_adapter *adapter = client->adapter;
712	struct mt9m032 *sensor;
713	int chip_version;
714	int ret;
715
716	if (pdata == NULL) {
717		dev_err(&client->dev, "No platform data\n");
718		return -EINVAL;
719	}
720
721	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
722		dev_warn(&client->dev,
723			 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
724		return -EIO;
725	}
726
727	if (!client->dev.platform_data)
728		return -ENODEV;
729
730	sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
731	if (sensor == NULL)
732		return -ENOMEM;
733
734	mutex_init(&sensor->lock);
735
736	sensor->pdata = pdata;
737
738	v4l2_i2c_subdev_init(&sensor->subdev, client, &mt9m032_ops);
739	sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
740
741	chip_version = mt9m032_read(client, MT9M032_CHIP_VERSION);
742	if (chip_version != MT9M032_CHIP_VERSION_VALUE) {
743		dev_err(&client->dev, "MT9M032 not detected, wrong version "
744			"0x%04x\n", chip_version);
745		ret = -ENODEV;
746		goto error_sensor;
747	}
748
749	dev_info(&client->dev, "MT9M032 detected at address 0x%02x\n",
750		 client->addr);
751
752	sensor->frame_interval.numerator = 1;
753	sensor->frame_interval.denominator = 30;
754
755	sensor->crop.left = MT9M032_COLUMN_START_DEF;
756	sensor->crop.top = MT9M032_ROW_START_DEF;
757	sensor->crop.width = MT9M032_COLUMN_SIZE_DEF;
758	sensor->crop.height = MT9M032_ROW_SIZE_DEF;
759
760	sensor->format.width = sensor->crop.width;
761	sensor->format.height = sensor->crop.height;
762	sensor->format.code = V4L2_MBUS_FMT_Y8_1X8;
763	sensor->format.field = V4L2_FIELD_NONE;
764	sensor->format.colorspace = V4L2_COLORSPACE_SRGB;
765
766	v4l2_ctrl_handler_init(&sensor->ctrls, 5);
767
768	v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
769			  V4L2_CID_GAIN, 0, 127, 1, 64);
770
771	sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls,
772					  &mt9m032_ctrl_ops,
773					  V4L2_CID_HFLIP, 0, 1, 1, 0);
774	sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls,
775					  &mt9m032_ctrl_ops,
776					  V4L2_CID_VFLIP, 0, 1, 1, 0);
777
778	v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
779			  V4L2_CID_EXPOSURE, MT9M032_SHUTTER_WIDTH_MIN,
780			  MT9M032_SHUTTER_WIDTH_MAX, 1,
781			  MT9M032_SHUTTER_WIDTH_DEF);
782	v4l2_ctrl_new_std(&sensor->ctrls, &mt9m032_ctrl_ops,
783			  V4L2_CID_PIXEL_RATE, pdata->pix_clock,
784			  pdata->pix_clock, 1, pdata->pix_clock);
785
786	if (sensor->ctrls.error) {
787		ret = sensor->ctrls.error;
788		dev_err(&client->dev, "control initialization error %d\n", ret);
789		goto error_ctrl;
790	}
791
792	v4l2_ctrl_cluster(2, &sensor->hflip);
793
794	sensor->subdev.ctrl_handler = &sensor->ctrls;
795	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
796	ret = media_entity_init(&sensor->subdev.entity, 1, &sensor->pad, 0);
797	if (ret < 0)
798		goto error_ctrl;
799
800	ret = mt9m032_write(client, MT9M032_RESET, 1);	/* reset on */
801	if (ret < 0)
802		goto error_entity;
803	ret = mt9m032_write(client, MT9M032_RESET, 0);	/* reset off */
804	if (ret < 0)
805		goto error_entity;
806
807	ret = mt9m032_setup_pll(sensor);
808	if (ret < 0)
809		goto error_entity;
810	usleep_range(10000, 11000);
811
812	ret = v4l2_ctrl_handler_setup(&sensor->ctrls);
813	if (ret < 0)
814		goto error_entity;
815
816	/* SIZE */
817	ret = mt9m032_update_geom_timing(sensor);
818	if (ret < 0)
819		goto error_entity;
820
821	ret = mt9m032_write(client, 0x41, 0x0000);	/* reserved !!! */
822	if (ret < 0)
823		goto error_entity;
824	ret = mt9m032_write(client, 0x42, 0x0003);	/* reserved !!! */
825	if (ret < 0)
826		goto error_entity;
827	ret = mt9m032_write(client, 0x43, 0x0003);	/* reserved !!! */
828	if (ret < 0)
829		goto error_entity;
830	ret = mt9m032_write(client, 0x7f, 0x0000);	/* reserved !!! */
831	if (ret < 0)
832		goto error_entity;
833	if (sensor->pdata->invert_pixclock) {
834		ret = mt9m032_write(client, MT9M032_PIX_CLK_CTRL,
835				    MT9M032_PIX_CLK_CTRL_INV_PIXCLK);
836		if (ret < 0)
837			goto error_entity;
838	}
839
840	ret = mt9m032_write(client, MT9M032_RESTART, 1); /* Restart on */
841	if (ret < 0)
842		goto error_entity;
843	msleep(100);
844	ret = mt9m032_write(client, MT9M032_RESTART, 0); /* Restart off */
845	if (ret < 0)
846		goto error_entity;
847	msleep(100);
848	ret = update_formatter2(sensor, false);
849	if (ret < 0)
850		goto error_entity;
851
852	return ret;
853
854error_entity:
855	media_entity_cleanup(&sensor->subdev.entity);
856error_ctrl:
857	v4l2_ctrl_handler_free(&sensor->ctrls);
858error_sensor:
859	mutex_destroy(&sensor->lock);
860	return ret;
861}
862
863static int mt9m032_remove(struct i2c_client *client)
864{
865	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
866	struct mt9m032 *sensor = to_mt9m032(subdev);
867
868	v4l2_device_unregister_subdev(subdev);
869	v4l2_ctrl_handler_free(&sensor->ctrls);
870	media_entity_cleanup(&subdev->entity);
871	mutex_destroy(&sensor->lock);
872	return 0;
873}
874
875static const struct i2c_device_id mt9m032_id_table[] = {
876	{ MT9M032_NAME, 0 },
877	{ }
878};
879
880MODULE_DEVICE_TABLE(i2c, mt9m032_id_table);
881
882static struct i2c_driver mt9m032_i2c_driver = {
883	.driver = {
884		.name = MT9M032_NAME,
885	},
886	.probe = mt9m032_probe,
887	.remove = mt9m032_remove,
888	.id_table = mt9m032_id_table,
889};
890
891module_i2c_driver(mt9m032_i2c_driver);
892
893MODULE_AUTHOR("Martin Hostettler <martin@neutronstar.dyndns.org>");
894MODULE_DESCRIPTION("MT9M032 camera sensor driver");
895MODULE_LICENSE("GPL v2");
896