blob: 412b399baca40c58ce13729b922b250b6ba4ab07 [file] [log] [blame]
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -03001/*
2 * Driver for MT9V022 CMOS Image Sensor from Micron
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/videodev2.h>
12#include <linux/slab.h>
13#include <linux/i2c.h>
14#include <linux/delay.h>
15#include <linux/log2.h>
16
17#include <media/v4l2-common.h>
18#include <media/v4l2-chip-ident.h>
19#include <media/soc_camera.h>
20
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030021/* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
22 * The platform has to define i2c_board_info
23 * and call i2c_register_board_info() */
24
25static char *sensor_type;
26module_param(sensor_type, charp, S_IRUGO);
Niels de Vos61a2d072008-07-31 00:07:23 -070027MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030028
29/* mt9v022 selected register addresses */
30#define MT9V022_CHIP_VERSION 0x00
31#define MT9V022_COLUMN_START 0x01
32#define MT9V022_ROW_START 0x02
33#define MT9V022_WINDOW_HEIGHT 0x03
34#define MT9V022_WINDOW_WIDTH 0x04
35#define MT9V022_HORIZONTAL_BLANKING 0x05
36#define MT9V022_VERTICAL_BLANKING 0x06
37#define MT9V022_CHIP_CONTROL 0x07
38#define MT9V022_SHUTTER_WIDTH1 0x08
39#define MT9V022_SHUTTER_WIDTH2 0x09
40#define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
41#define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
42#define MT9V022_RESET 0x0c
43#define MT9V022_READ_MODE 0x0d
44#define MT9V022_MONITOR_MODE 0x0e
45#define MT9V022_PIXEL_OPERATION_MODE 0x0f
46#define MT9V022_LED_OUT_CONTROL 0x1b
47#define MT9V022_ADC_MODE_CONTROL 0x1c
48#define MT9V022_ANALOG_GAIN 0x34
49#define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
50#define MT9V022_PIXCLK_FV_LV 0x74
51#define MT9V022_DIGITAL_TEST_PATTERN 0x7f
52#define MT9V022_AEC_AGC_ENABLE 0xAF
53#define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
54
55/* Progressive scan, master, defaults */
56#define MT9V022_CHIP_CONTROL_DEFAULT 0x188
57
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030058static const struct soc_camera_data_format mt9v022_colour_formats[] = {
59 /* Order important: first natively supported,
60 * second supported with a GPIO extender */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030061 {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030062 .name = "Bayer (sRGB) 10 bit",
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030063 .depth = 10,
64 .fourcc = V4L2_PIX_FMT_SBGGR16,
65 .colorspace = V4L2_COLORSPACE_SRGB,
66 }, {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030067 .name = "Bayer (sRGB) 8 bit",
68 .depth = 8,
69 .fourcc = V4L2_PIX_FMT_SBGGR8,
70 .colorspace = V4L2_COLORSPACE_SRGB,
71 }
72};
73
74static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
75 /* Order important - see above */
76 {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030077 .name = "Monochrome 10 bit",
78 .depth = 10,
79 .fourcc = V4L2_PIX_FMT_Y16,
80 }, {
81 .name = "Monochrome 8 bit",
82 .depth = 8,
83 .fourcc = V4L2_PIX_FMT_GREY,
84 },
85};
86
87struct mt9v022 {
88 struct i2c_client *client;
89 struct soc_camera_device icd;
Guennadi Liakhovetski7fb0fd02008-05-05 14:12:30 -030090 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030091 u16 chip_control;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030092};
93
94static int reg_read(struct soc_camera_device *icd, const u8 reg)
95{
96 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
97 struct i2c_client *client = mt9v022->client;
98 s32 data = i2c_smbus_read_word_data(client, reg);
99 return data < 0 ? data : swab16(data);
100}
101
102static int reg_write(struct soc_camera_device *icd, const u8 reg,
103 const u16 data)
104{
105 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
106 return i2c_smbus_write_word_data(mt9v022->client, reg, swab16(data));
107}
108
109static int reg_set(struct soc_camera_device *icd, const u8 reg,
110 const u16 data)
111{
112 int ret;
113
114 ret = reg_read(icd, reg);
115 if (ret < 0)
116 return ret;
117 return reg_write(icd, reg, ret | data);
118}
119
120static int reg_clear(struct soc_camera_device *icd, const u8 reg,
121 const u16 data)
122{
123 int ret;
124
125 ret = reg_read(icd, reg);
126 if (ret < 0)
127 return ret;
128 return reg_write(icd, reg, ret & ~data);
129}
130
131static int mt9v022_init(struct soc_camera_device *icd)
132{
133 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300134 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300135 int ret;
136
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300137 if (icl->power) {
138 ret = icl->power(&mt9v022->client->dev, 1);
139 if (ret < 0) {
140 dev_err(icd->vdev->parent,
141 "Platform failed to power-on the camera.\n");
142 return ret;
143 }
144 }
145
146 /*
147 * The camera could have been already on, we hard-reset it additionally,
148 * if available. Soft reset is done in video_probe().
149 */
150 if (icl->reset)
151 icl->reset(&mt9v022->client->dev);
152
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300153 /* Almost the default mode: master, parallel, simultaneous, and an
154 * undocumented bit 0x200, which is present in table 7, but not in 8,
155 * plus snapshot mode to disable scan for now */
156 mt9v022->chip_control |= 0x10;
157 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300158 if (!ret)
159 ret = reg_write(icd, MT9V022_READ_MODE, 0x300);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300160
161 /* All defaults */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300162 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300163 /* AEC, AGC on */
164 ret = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x3);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300165 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300166 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300167 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300168 /* default - auto */
169 ret = reg_clear(icd, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300170 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300171 ret = reg_write(icd, MT9V022_DIGITAL_TEST_PATTERN, 0);
172
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300173 return ret;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300174}
175
176static int mt9v022_release(struct soc_camera_device *icd)
177{
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300178 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
179 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
180
181 if (icl->power)
182 icl->power(&mt9v022->client->dev, 0);
183
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300184 return 0;
185}
186
187static int mt9v022_start_capture(struct soc_camera_device *icd)
188{
189 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
190 /* Switch to master "normal" mode */
191 mt9v022->chip_control &= ~0x10;
192 if (reg_write(icd, MT9V022_CHIP_CONTROL,
193 mt9v022->chip_control) < 0)
194 return -EIO;
195 return 0;
196}
197
198static int mt9v022_stop_capture(struct soc_camera_device *icd)
199{
200 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
201 /* Switch to snapshot mode */
202 mt9v022->chip_control |= 0x10;
203 if (reg_write(icd, MT9V022_CHIP_CONTROL,
204 mt9v022->chip_control) < 0)
205 return -EIO;
206 return 0;
207}
208
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300209static int mt9v022_set_bus_param(struct soc_camera_device *icd,
210 unsigned long flags)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300211{
212 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300213 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300214 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300215 int ret;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300216 u16 pixclk = 0;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300217
218 /* Only one width bit may be set */
219 if (!is_power_of_2(width_flag))
220 return -EINVAL;
221
Sascha Hauere958e272009-03-13 06:08:20 -0300222 if (icl->set_bus_param) {
223 ret = icl->set_bus_param(icl, width_flag);
224 if (ret)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300225 return ret;
Sascha Hauere958e272009-03-13 06:08:20 -0300226 } else {
227 /*
228 * Without board specific bus width settings we only support the
229 * sensors native bus width
230 */
231 if (width_flag != SOCAM_DATAWIDTH_10)
232 return -EINVAL;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300233 }
234
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300235 flags = soc_camera_apply_sensor_flags(icl, flags);
236
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300237 if (flags & SOCAM_PCLK_SAMPLE_RISING)
238 pixclk |= 0x10;
239
240 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
241 pixclk |= 0x1;
242
243 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
244 pixclk |= 0x2;
245
246 ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk);
247 if (ret < 0)
248 return ret;
249
250 if (!(flags & SOCAM_MASTER))
251 mt9v022->chip_control &= ~0x8;
252
253 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
254 if (ret < 0)
255 return ret;
256
257 dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
258 pixclk, mt9v022->chip_control);
259
260 return 0;
261}
262
263static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
264{
265 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
Sascha Hauere958e272009-03-13 06:08:20 -0300266 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
267 unsigned int width_flag;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300268
Sascha Hauere958e272009-03-13 06:08:20 -0300269 if (icl->query_bus_param)
270 width_flag = icl->query_bus_param(icl) &
271 SOCAM_DATAWIDTH_MASK;
272 else
273 width_flag = SOCAM_DATAWIDTH_10;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300274
275 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
276 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
277 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
Guennadi Liakhovetski2d9329f2009-02-23 12:12:58 -0300278 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE |
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300279 width_flag;
280}
281
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300282static int mt9v022_set_crop(struct soc_camera_device *icd,
283 struct v4l2_rect *rect)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300284{
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300285 int ret;
286
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300287 /* Like in example app. Contradicts the datasheet though */
288 ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
289 if (ret >= 0) {
290 if (ret & 1) /* Autoexposure */
291 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
292 rect->height + icd->y_skip_top + 43);
293 else
294 ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
295 rect->height + icd->y_skip_top + 43);
296 }
297 /* Setup frame format: defaults apart from width and height */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300298 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300299 ret = reg_write(icd, MT9V022_COLUMN_START, rect->left);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300300 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300301 ret = reg_write(icd, MT9V022_ROW_START, rect->top);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300302 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300303 /* Default 94, Phytec driver says:
304 * "width + horizontal blank >= 660" */
305 ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING,
306 rect->width > 660 - 43 ? 43 :
307 660 - rect->width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300308 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300309 ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300310 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300311 ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300312 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300313 ret = reg_write(icd, MT9V022_WINDOW_HEIGHT,
314 rect->height + icd->y_skip_top);
315
316 if (ret < 0)
317 return ret;
318
319 dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
320
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300321 return 0;
322}
323
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300324static int mt9v022_set_fmt(struct soc_camera_device *icd,
325 struct v4l2_format *f)
326{
327 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
328 struct v4l2_pix_format *pix = &f->fmt.pix;
329 struct v4l2_rect rect = {
330 .left = icd->x_current,
331 .top = icd->y_current,
332 .width = pix->width,
333 .height = pix->height,
334 };
335
336 /* The caller provides a supported format, as verified per call to
337 * icd->try_fmt(), datawidth is from our supported format list */
338 switch (pix->pixelformat) {
339 case V4L2_PIX_FMT_GREY:
340 case V4L2_PIX_FMT_Y16:
341 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
342 return -EINVAL;
343 break;
344 case V4L2_PIX_FMT_SBGGR8:
345 case V4L2_PIX_FMT_SBGGR16:
346 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
347 return -EINVAL;
348 break;
349 case 0:
350 /* No format change, only geometry */
351 break;
352 default:
353 return -EINVAL;
354 }
355
356 /* No support for scaling on this camera, just crop. */
357 return mt9v022_set_crop(icd, &rect);
358}
359
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300360static int mt9v022_try_fmt(struct soc_camera_device *icd,
361 struct v4l2_format *f)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300362{
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300363 struct v4l2_pix_format *pix = &f->fmt.pix;
364
365 if (pix->height < 32 + icd->y_skip_top)
366 pix->height = 32 + icd->y_skip_top;
367 if (pix->height > 480 + icd->y_skip_top)
368 pix->height = 480 + icd->y_skip_top;
369 if (pix->width < 48)
370 pix->width = 48;
371 if (pix->width > 752)
372 pix->width = 752;
373 pix->width &= ~0x03; /* ? */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300374
375 return 0;
376}
377
378static int mt9v022_get_chip_id(struct soc_camera_device *icd,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300379 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300380{
381 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
382
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300383 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300384 return -EINVAL;
385
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300386 if (id->match.addr != mt9v022->client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300387 return -ENODEV;
388
389 id->ident = mt9v022->model;
390 id->revision = 0;
391
392 return 0;
393}
394
395#ifdef CONFIG_VIDEO_ADV_DEBUG
396static int mt9v022_get_register(struct soc_camera_device *icd,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300397 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300398{
399 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
400
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300401 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300402 return -EINVAL;
403
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300404 if (reg->match.addr != mt9v022->client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300405 return -ENODEV;
406
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300407 reg->size = 2;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300408 reg->val = reg_read(icd, reg->reg);
409
410 if (reg->val > 0xffff)
411 return -EIO;
412
413 return 0;
414}
415
416static int mt9v022_set_register(struct soc_camera_device *icd,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300417 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300418{
419 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
420
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300421 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300422 return -EINVAL;
423
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300424 if (reg->match.addr != mt9v022->client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300425 return -ENODEV;
426
427 if (reg_write(icd, reg->reg, reg->val) < 0)
428 return -EIO;
429
430 return 0;
431}
432#endif
433
Adrian Bunk4407a462008-04-28 17:13:51 -0300434static const struct v4l2_queryctrl mt9v022_controls[] = {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300435 {
436 .id = V4L2_CID_VFLIP,
437 .type = V4L2_CTRL_TYPE_BOOLEAN,
438 .name = "Flip Vertically",
439 .minimum = 0,
440 .maximum = 1,
441 .step = 1,
442 .default_value = 0,
443 }, {
444 .id = V4L2_CID_HFLIP,
445 .type = V4L2_CTRL_TYPE_BOOLEAN,
446 .name = "Flip Horizontally",
447 .minimum = 0,
448 .maximum = 1,
449 .step = 1,
450 .default_value = 0,
451 }, {
452 .id = V4L2_CID_GAIN,
453 .type = V4L2_CTRL_TYPE_INTEGER,
454 .name = "Analog Gain",
455 .minimum = 64,
456 .maximum = 127,
457 .step = 1,
458 .default_value = 64,
459 .flags = V4L2_CTRL_FLAG_SLIDER,
460 }, {
461 .id = V4L2_CID_EXPOSURE,
462 .type = V4L2_CTRL_TYPE_INTEGER,
463 .name = "Exposure",
464 .minimum = 1,
465 .maximum = 255,
466 .step = 1,
467 .default_value = 255,
468 .flags = V4L2_CTRL_FLAG_SLIDER,
469 }, {
470 .id = V4L2_CID_AUTOGAIN,
471 .type = V4L2_CTRL_TYPE_BOOLEAN,
472 .name = "Automatic Gain",
473 .minimum = 0,
474 .maximum = 1,
475 .step = 1,
476 .default_value = 1,
477 }, {
478 .id = V4L2_CID_EXPOSURE_AUTO,
479 .type = V4L2_CTRL_TYPE_BOOLEAN,
480 .name = "Automatic Exposure",
481 .minimum = 0,
482 .maximum = 1,
483 .step = 1,
484 .default_value = 1,
485 }
486};
487
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300488static int mt9v022_video_probe(struct soc_camera_device *);
489static void mt9v022_video_remove(struct soc_camera_device *);
490static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
491static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300492
493static struct soc_camera_ops mt9v022_ops = {
494 .owner = THIS_MODULE,
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300495 .probe = mt9v022_video_probe,
496 .remove = mt9v022_video_remove,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300497 .init = mt9v022_init,
498 .release = mt9v022_release,
499 .start_capture = mt9v022_start_capture,
500 .stop_capture = mt9v022_stop_capture,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300501 .set_crop = mt9v022_set_crop,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300502 .set_fmt = mt9v022_set_fmt,
503 .try_fmt = mt9v022_try_fmt,
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300504 .set_bus_param = mt9v022_set_bus_param,
505 .query_bus_param = mt9v022_query_bus_param,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300506 .controls = mt9v022_controls,
507 .num_controls = ARRAY_SIZE(mt9v022_controls),
508 .get_control = mt9v022_get_control,
509 .set_control = mt9v022_set_control,
510 .get_chip_id = mt9v022_get_chip_id,
511#ifdef CONFIG_VIDEO_ADV_DEBUG
512 .get_register = mt9v022_get_register,
513 .set_register = mt9v022_set_register,
514#endif
515};
516
517static int mt9v022_get_control(struct soc_camera_device *icd,
518 struct v4l2_control *ctrl)
519{
520 int data;
521
522 switch (ctrl->id) {
523 case V4L2_CID_VFLIP:
524 data = reg_read(icd, MT9V022_READ_MODE);
525 if (data < 0)
526 return -EIO;
527 ctrl->value = !!(data & 0x10);
528 break;
529 case V4L2_CID_HFLIP:
530 data = reg_read(icd, MT9V022_READ_MODE);
531 if (data < 0)
532 return -EIO;
533 ctrl->value = !!(data & 0x20);
534 break;
535 case V4L2_CID_EXPOSURE_AUTO:
536 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
537 if (data < 0)
538 return -EIO;
539 ctrl->value = !!(data & 0x1);
540 break;
541 case V4L2_CID_AUTOGAIN:
542 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
543 if (data < 0)
544 return -EIO;
545 ctrl->value = !!(data & 0x2);
546 break;
547 }
548 return 0;
549}
550
551static int mt9v022_set_control(struct soc_camera_device *icd,
552 struct v4l2_control *ctrl)
553{
554 int data;
555 const struct v4l2_queryctrl *qctrl;
556
557 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
558
559 if (!qctrl)
560 return -EINVAL;
561
562 switch (ctrl->id) {
563 case V4L2_CID_VFLIP:
564 if (ctrl->value)
565 data = reg_set(icd, MT9V022_READ_MODE, 0x10);
566 else
567 data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
568 if (data < 0)
569 return -EIO;
570 break;
571 case V4L2_CID_HFLIP:
572 if (ctrl->value)
573 data = reg_set(icd, MT9V022_READ_MODE, 0x20);
574 else
575 data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
576 if (data < 0)
577 return -EIO;
578 break;
579 case V4L2_CID_GAIN:
580 /* mt9v022 has minimum == default */
581 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
582 return -EINVAL;
583 else {
584 unsigned long range = qctrl->maximum - qctrl->minimum;
585 /* Datasheet says 16 to 64. autogain only works properly
586 * after setting gain to maximum 14. Larger values
587 * produce "white fly" noise effect. On the whole,
588 * manually setting analog gain does no good. */
589 unsigned long gain = ((ctrl->value - qctrl->minimum) *
590 10 + range / 2) / range + 4;
591 if (gain >= 32)
592 gain &= ~1;
593 /* The user wants to set gain manually, hope, she
594 * knows, what she's doing... Switch AGC off. */
595
596 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
597 return -EIO;
598
599 dev_info(&icd->dev, "Setting gain from %d to %lu\n",
600 reg_read(icd, MT9V022_ANALOG_GAIN), gain);
601 if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
602 return -EIO;
603 icd->gain = ctrl->value;
604 }
605 break;
606 case V4L2_CID_EXPOSURE:
607 /* mt9v022 has maximum == default */
608 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
609 return -EINVAL;
610 else {
611 unsigned long range = qctrl->maximum - qctrl->minimum;
612 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
613 479 + range / 2) / range + 1;
614 /* The user wants to set shutter width manually, hope,
615 * she knows, what she's doing... Switch AEC off. */
616
617 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
618 return -EIO;
619
620 dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
621 reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
622 shutter);
623 if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
624 shutter) < 0)
625 return -EIO;
626 icd->exposure = ctrl->value;
627 }
628 break;
629 case V4L2_CID_AUTOGAIN:
630 if (ctrl->value)
631 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
632 else
633 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
634 if (data < 0)
635 return -EIO;
636 break;
637 case V4L2_CID_EXPOSURE_AUTO:
638 if (ctrl->value)
639 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
640 else
641 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
642 if (data < 0)
643 return -EIO;
644 break;
645 }
646 return 0;
647}
648
649/* Interface active, can use i2c. If it fails, it can indeed mean, that
650 * this wasn't our capture interface, so, we wait for the right one */
651static int mt9v022_video_probe(struct soc_camera_device *icd)
652{
653 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
Guennadi Liakhovetski297a7ef2008-12-17 14:05:38 -0300654 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300655 s32 data;
656 int ret;
Sascha Hauere958e272009-03-13 06:08:20 -0300657 unsigned long flags;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300658
659 if (!icd->dev.parent ||
660 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
661 return -ENODEV;
662
663 /* Read out the chip version register */
664 data = reg_read(icd, MT9V022_CHIP_VERSION);
665
666 /* must be 0x1311 or 0x1313 */
667 if (data != 0x1311 && data != 0x1313) {
668 ret = -ENODEV;
669 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
670 data);
671 goto ei2c;
672 }
673
674 /* Soft reset */
675 ret = reg_write(icd, MT9V022_RESET, 1);
676 if (ret < 0)
677 goto ei2c;
678 /* 15 clock cycles */
679 udelay(200);
680 if (reg_read(icd, MT9V022_RESET)) {
681 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
682 goto ei2c;
683 }
684
685 /* Set monochrome or colour sensor type */
686 if (sensor_type && (!strcmp("colour", sensor_type) ||
687 !strcmp("color", sensor_type))) {
688 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
689 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300690 icd->formats = mt9v022_colour_formats;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300691 } else {
692 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
693 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300694 icd->formats = mt9v022_monochrome_formats;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300695 }
696
Sascha Hauere958e272009-03-13 06:08:20 -0300697 if (ret < 0)
698 goto eisis;
699
700 icd->num_formats = 0;
701
702 /*
703 * This is a 10bit sensor, so by default we only allow 10bit.
704 * The platform may support different bus widths due to
705 * different routing of the data lines.
706 */
707 if (icl->query_bus_param)
708 flags = icl->query_bus_param(icl);
709 else
710 flags = SOCAM_DATAWIDTH_10;
711
712 if (flags & SOCAM_DATAWIDTH_10)
713 icd->num_formats++;
714 else
715 icd->formats++;
716
717 if (flags & SOCAM_DATAWIDTH_8)
718 icd->num_formats++;
719
720 ret = soc_camera_video_start(icd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300721 if (ret < 0)
722 goto eisis;
723
724 dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
725 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
726 "monochrome" : "colour");
727
728 return 0;
729
730eisis:
731ei2c:
732 return ret;
733}
734
735static void mt9v022_video_remove(struct soc_camera_device *icd)
736{
737 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
Guennadi Liakhovetski594bb462009-04-24 12:53:51 -0300738 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300739
740 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
Guennadi Liakhovetskia85bdac2008-12-18 12:54:33 -0300741 icd->dev.parent, icd->vdev);
742 soc_camera_video_stop(icd);
Guennadi Liakhovetski594bb462009-04-24 12:53:51 -0300743 if (icl->free_bus)
744 icl->free_bus(icl);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300745}
746
Jean Delvared2653e92008-04-29 23:11:39 +0200747static int mt9v022_probe(struct i2c_client *client,
748 const struct i2c_device_id *did)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300749{
750 struct mt9v022 *mt9v022;
751 struct soc_camera_device *icd;
752 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
753 struct soc_camera_link *icl = client->dev.platform_data;
754 int ret;
755
756 if (!icl) {
757 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
758 return -EINVAL;
759 }
760
761 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
762 dev_warn(&adapter->dev,
763 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
764 return -EIO;
765 }
766
767 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
768 if (!mt9v022)
769 return -ENOMEM;
770
771 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
772 mt9v022->client = client;
773 i2c_set_clientdata(client, mt9v022);
774
775 icd = &mt9v022->icd;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300776 icd->ops = &mt9v022_ops;
777 icd->control = &client->dev;
778 icd->x_min = 1;
779 icd->y_min = 4;
780 icd->x_current = 1;
781 icd->y_current = 4;
782 icd->width_min = 48;
783 icd->width_max = 752;
784 icd->height_min = 32;
785 icd->height_max = 480;
786 icd->y_skip_top = 1;
787 icd->iface = icl->bus_id;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300788
789 ret = soc_camera_device_register(icd);
790 if (ret)
791 goto eisdr;
792
793 return 0;
794
795eisdr:
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300796 kfree(mt9v022);
797 return ret;
798}
799
800static int mt9v022_remove(struct i2c_client *client)
801{
802 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
803
804 soc_camera_device_unregister(&mt9v022->icd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300805 kfree(mt9v022);
806
807 return 0;
808}
Jean Delvare3760f732008-04-29 23:11:40 +0200809static const struct i2c_device_id mt9v022_id[] = {
810 { "mt9v022", 0 },
811 { }
812};
813MODULE_DEVICE_TABLE(i2c, mt9v022_id);
814
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300815static struct i2c_driver mt9v022_i2c_driver = {
816 .driver = {
817 .name = "mt9v022",
818 },
819 .probe = mt9v022_probe,
820 .remove = mt9v022_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200821 .id_table = mt9v022_id,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300822};
823
824static int __init mt9v022_mod_init(void)
825{
826 return i2c_add_driver(&mt9v022_i2c_driver);
827}
828
829static void __exit mt9v022_mod_exit(void)
830{
831 i2c_del_driver(&mt9v022_i2c_driver);
832}
833
834module_init(mt9v022_mod_init);
835module_exit(mt9v022_mod_exit);
836
837MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
838MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
839MODULE_LICENSE("GPL");