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