V4L/DVB (11742): TI THS7303 video amplifier driver code
[pandora-kernel.git] / drivers / media / video / mt9v022.c
1 /*
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
21 /* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
22  * The platform has to define i2c_board_info
23  * and call i2c_register_board_info() */
24
25 static char *sensor_type;
26 module_param(sensor_type, charp, S_IRUGO);
27 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
28
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
58 static const struct soc_camera_data_format mt9v022_colour_formats[] = {
59         /* Order important: first natively supported,
60          * second supported with a GPIO extender */
61         {
62                 .name           = "Bayer (sRGB) 10 bit",
63                 .depth          = 10,
64                 .fourcc         = V4L2_PIX_FMT_SBGGR16,
65                 .colorspace     = V4L2_COLORSPACE_SRGB,
66         }, {
67                 .name           = "Bayer (sRGB) 8 bit",
68                 .depth          = 8,
69                 .fourcc         = V4L2_PIX_FMT_SBGGR8,
70                 .colorspace     = V4L2_COLORSPACE_SRGB,
71         }
72 };
73
74 static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
75         /* Order important - see above */
76         {
77                 .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
87 struct mt9v022 {
88         struct i2c_client *client;
89         struct soc_camera_device icd;
90         int model;      /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
91         u16 chip_control;
92 };
93
94 static int reg_read(struct i2c_client *client, const u8 reg)
95 {
96         s32 data = i2c_smbus_read_word_data(client, reg);
97         return data < 0 ? data : swab16(data);
98 }
99
100 static int reg_write(struct i2c_client *client, const u8 reg,
101                      const u16 data)
102 {
103         return i2c_smbus_write_word_data(client, reg, swab16(data));
104 }
105
106 static int reg_set(struct i2c_client *client, const u8 reg,
107                    const u16 data)
108 {
109         int ret;
110
111         ret = reg_read(client, reg);
112         if (ret < 0)
113                 return ret;
114         return reg_write(client, reg, ret | data);
115 }
116
117 static int reg_clear(struct i2c_client *client, const u8 reg,
118                      const u16 data)
119 {
120         int ret;
121
122         ret = reg_read(client, reg);
123         if (ret < 0)
124                 return ret;
125         return reg_write(client, reg, ret & ~data);
126 }
127
128 static int mt9v022_init(struct soc_camera_device *icd)
129 {
130         struct i2c_client *client = to_i2c_client(icd->control);
131         struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
132         struct soc_camera_link *icl = client->dev.platform_data;
133         int ret;
134
135         if (icl->power) {
136                 ret = icl->power(&client->dev, 1);
137                 if (ret < 0) {
138                         dev_err(icd->vdev->parent,
139                                 "Platform failed to power-on the camera.\n");
140                         return ret;
141                 }
142         }
143
144         /*
145          * The camera could have been already on, we hard-reset it additionally,
146          * if available. Soft reset is done in video_probe().
147          */
148         if (icl->reset)
149                 icl->reset(&client->dev);
150
151         /* Almost the default mode: master, parallel, simultaneous, and an
152          * undocumented bit 0x200, which is present in table 7, but not in 8,
153          * plus snapshot mode to disable scan for now */
154         mt9v022->chip_control |= 0x10;
155         ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
156         if (!ret)
157                 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
158
159         /* All defaults */
160         if (!ret)
161                 /* AEC, AGC on */
162                 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
163         if (!ret)
164                 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
165         if (!ret)
166                 /* default - auto */
167                 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
168         if (!ret)
169                 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
170
171         return ret;
172 }
173
174 static int mt9v022_release(struct soc_camera_device *icd)
175 {
176         struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
177         struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
178
179         if (icl->power)
180                 icl->power(&mt9v022->client->dev, 0);
181
182         return 0;
183 }
184
185 static int mt9v022_start_capture(struct soc_camera_device *icd)
186 {
187         struct i2c_client *client = to_i2c_client(icd->control);
188         struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
189         /* Switch to master "normal" mode */
190         mt9v022->chip_control &= ~0x10;
191         if (reg_write(client, MT9V022_CHIP_CONTROL,
192                       mt9v022->chip_control) < 0)
193                 return -EIO;
194         return 0;
195 }
196
197 static int mt9v022_stop_capture(struct soc_camera_device *icd)
198 {
199         struct i2c_client *client = to_i2c_client(icd->control);
200         struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
201         /* Switch to snapshot mode */
202         mt9v022->chip_control |= 0x10;
203         if (reg_write(client, MT9V022_CHIP_CONTROL,
204                       mt9v022->chip_control) < 0)
205                 return -EIO;
206         return 0;
207 }
208
209 static int mt9v022_set_bus_param(struct soc_camera_device *icd,
210                                  unsigned long flags)
211 {
212         struct i2c_client *client = to_i2c_client(icd->control);
213         struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
214         struct soc_camera_link *icl = client->dev.platform_data;
215         unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
216         int ret;
217         u16 pixclk = 0;
218
219         /* Only one width bit may be set */
220         if (!is_power_of_2(width_flag))
221                 return -EINVAL;
222
223         if (icl->set_bus_param) {
224                 ret = icl->set_bus_param(icl, width_flag);
225                 if (ret)
226                         return ret;
227         } else {
228                 /*
229                  * Without board specific bus width settings we only support the
230                  * sensors native bus width
231                  */
232                 if (width_flag != SOCAM_DATAWIDTH_10)
233                         return -EINVAL;
234         }
235
236         flags = soc_camera_apply_sensor_flags(icl, flags);
237
238         if (flags & SOCAM_PCLK_SAMPLE_RISING)
239                 pixclk |= 0x10;
240
241         if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
242                 pixclk |= 0x1;
243
244         if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
245                 pixclk |= 0x2;
246
247         ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
248         if (ret < 0)
249                 return ret;
250
251         if (!(flags & SOCAM_MASTER))
252                 mt9v022->chip_control &= ~0x8;
253
254         ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
255         if (ret < 0)
256                 return ret;
257
258         dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
259                 pixclk, mt9v022->chip_control);
260
261         return 0;
262 }
263
264 static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
265 {
266         struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
267         struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
268         unsigned int width_flag;
269
270         if (icl->query_bus_param)
271                 width_flag = icl->query_bus_param(icl) &
272                         SOCAM_DATAWIDTH_MASK;
273         else
274                 width_flag = SOCAM_DATAWIDTH_10;
275
276         return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
277                 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
278                 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
279                 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE |
280                 width_flag;
281 }
282
283 static int mt9v022_set_crop(struct soc_camera_device *icd,
284                             struct v4l2_rect *rect)
285 {
286         struct i2c_client *client = to_i2c_client(icd->control);
287         int ret;
288
289         /* Like in example app. Contradicts the datasheet though */
290         ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
291         if (ret >= 0) {
292                 if (ret & 1) /* Autoexposure */
293                         ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
294                                         rect->height + icd->y_skip_top + 43);
295                 else
296                         ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
297                                         rect->height + icd->y_skip_top + 43);
298         }
299         /* Setup frame format: defaults apart from width and height */
300         if (!ret)
301                 ret = reg_write(client, MT9V022_COLUMN_START, rect->left);
302         if (!ret)
303                 ret = reg_write(client, MT9V022_ROW_START, rect->top);
304         if (!ret)
305                 /* Default 94, Phytec driver says:
306                  * "width + horizontal blank >= 660" */
307                 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
308                                 rect->width > 660 - 43 ? 43 :
309                                 660 - rect->width);
310         if (!ret)
311                 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
312         if (!ret)
313                 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect->width);
314         if (!ret)
315                 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
316                                 rect->height + icd->y_skip_top);
317
318         if (ret < 0)
319                 return ret;
320
321         dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
322
323         return 0;
324 }
325
326 static int mt9v022_set_fmt(struct soc_camera_device *icd,
327                            struct v4l2_format *f)
328 {
329         struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
330         struct v4l2_pix_format *pix = &f->fmt.pix;
331         struct v4l2_rect rect = {
332                 .left   = icd->x_current,
333                 .top    = icd->y_current,
334                 .width  = pix->width,
335                 .height = pix->height,
336         };
337
338         /* The caller provides a supported format, as verified per call to
339          * icd->try_fmt(), datawidth is from our supported format list */
340         switch (pix->pixelformat) {
341         case V4L2_PIX_FMT_GREY:
342         case V4L2_PIX_FMT_Y16:
343                 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
344                         return -EINVAL;
345                 break;
346         case V4L2_PIX_FMT_SBGGR8:
347         case V4L2_PIX_FMT_SBGGR16:
348                 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
349                         return -EINVAL;
350                 break;
351         case 0:
352                 /* No format change, only geometry */
353                 break;
354         default:
355                 return -EINVAL;
356         }
357
358         /* No support for scaling on this camera, just crop. */
359         return mt9v022_set_crop(icd, &rect);
360 }
361
362 static int mt9v022_try_fmt(struct soc_camera_device *icd,
363                            struct v4l2_format *f)
364 {
365         struct v4l2_pix_format *pix = &f->fmt.pix;
366
367         if (pix->height < 32 + icd->y_skip_top)
368                 pix->height = 32 + icd->y_skip_top;
369         if (pix->height > 480 + icd->y_skip_top)
370                 pix->height = 480 + icd->y_skip_top;
371         if (pix->width < 48)
372                 pix->width = 48;
373         if (pix->width > 752)
374                 pix->width = 752;
375         pix->width &= ~0x03; /* ? */
376
377         return 0;
378 }
379
380 static int mt9v022_get_chip_id(struct soc_camera_device *icd,
381                                struct v4l2_dbg_chip_ident *id)
382 {
383         struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
384
385         if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
386                 return -EINVAL;
387
388         if (id->match.addr != mt9v022->client->addr)
389                 return -ENODEV;
390
391         id->ident       = mt9v022->model;
392         id->revision    = 0;
393
394         return 0;
395 }
396
397 #ifdef CONFIG_VIDEO_ADV_DEBUG
398 static int mt9v022_get_register(struct soc_camera_device *icd,
399                                 struct v4l2_dbg_register *reg)
400 {
401         struct i2c_client *client = to_i2c_client(icd->control);
402
403         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
404                 return -EINVAL;
405
406         if (reg->match.addr != client->addr)
407                 return -ENODEV;
408
409         reg->size = 2;
410         reg->val = reg_read(client, reg->reg);
411
412         if (reg->val > 0xffff)
413                 return -EIO;
414
415         return 0;
416 }
417
418 static int mt9v022_set_register(struct soc_camera_device *icd,
419                                 struct v4l2_dbg_register *reg)
420 {
421         struct i2c_client *client = to_i2c_client(icd->control);
422
423         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
424                 return -EINVAL;
425
426         if (reg->match.addr != client->addr)
427                 return -ENODEV;
428
429         if (reg_write(client, reg->reg, reg->val) < 0)
430                 return -EIO;
431
432         return 0;
433 }
434 #endif
435
436 static const struct v4l2_queryctrl mt9v022_controls[] = {
437         {
438                 .id             = V4L2_CID_VFLIP,
439                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
440                 .name           = "Flip Vertically",
441                 .minimum        = 0,
442                 .maximum        = 1,
443                 .step           = 1,
444                 .default_value  = 0,
445         }, {
446                 .id             = V4L2_CID_HFLIP,
447                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
448                 .name           = "Flip Horizontally",
449                 .minimum        = 0,
450                 .maximum        = 1,
451                 .step           = 1,
452                 .default_value  = 0,
453         }, {
454                 .id             = V4L2_CID_GAIN,
455                 .type           = V4L2_CTRL_TYPE_INTEGER,
456                 .name           = "Analog Gain",
457                 .minimum        = 64,
458                 .maximum        = 127,
459                 .step           = 1,
460                 .default_value  = 64,
461                 .flags          = V4L2_CTRL_FLAG_SLIDER,
462         }, {
463                 .id             = V4L2_CID_EXPOSURE,
464                 .type           = V4L2_CTRL_TYPE_INTEGER,
465                 .name           = "Exposure",
466                 .minimum        = 1,
467                 .maximum        = 255,
468                 .step           = 1,
469                 .default_value  = 255,
470                 .flags          = V4L2_CTRL_FLAG_SLIDER,
471         }, {
472                 .id             = V4L2_CID_AUTOGAIN,
473                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
474                 .name           = "Automatic Gain",
475                 .minimum        = 0,
476                 .maximum        = 1,
477                 .step           = 1,
478                 .default_value  = 1,
479         }, {
480                 .id             = V4L2_CID_EXPOSURE_AUTO,
481                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
482                 .name           = "Automatic Exposure",
483                 .minimum        = 0,
484                 .maximum        = 1,
485                 .step           = 1,
486                 .default_value  = 1,
487         }
488 };
489
490 static int mt9v022_video_probe(struct soc_camera_device *);
491 static void mt9v022_video_remove(struct soc_camera_device *);
492 static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
493 static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
494
495 static struct soc_camera_ops mt9v022_ops = {
496         .owner                  = THIS_MODULE,
497         .probe                  = mt9v022_video_probe,
498         .remove                 = mt9v022_video_remove,
499         .init                   = mt9v022_init,
500         .release                = mt9v022_release,
501         .start_capture          = mt9v022_start_capture,
502         .stop_capture           = mt9v022_stop_capture,
503         .set_crop               = mt9v022_set_crop,
504         .set_fmt                = mt9v022_set_fmt,
505         .try_fmt                = mt9v022_try_fmt,
506         .set_bus_param          = mt9v022_set_bus_param,
507         .query_bus_param        = mt9v022_query_bus_param,
508         .controls               = mt9v022_controls,
509         .num_controls           = ARRAY_SIZE(mt9v022_controls),
510         .get_control            = mt9v022_get_control,
511         .set_control            = mt9v022_set_control,
512         .get_chip_id            = mt9v022_get_chip_id,
513 #ifdef CONFIG_VIDEO_ADV_DEBUG
514         .get_register           = mt9v022_get_register,
515         .set_register           = mt9v022_set_register,
516 #endif
517 };
518
519 static int mt9v022_get_control(struct soc_camera_device *icd,
520                                struct v4l2_control *ctrl)
521 {
522         struct i2c_client *client = to_i2c_client(icd->control);
523         int data;
524
525         switch (ctrl->id) {
526         case V4L2_CID_VFLIP:
527                 data = reg_read(client, MT9V022_READ_MODE);
528                 if (data < 0)
529                         return -EIO;
530                 ctrl->value = !!(data & 0x10);
531                 break;
532         case V4L2_CID_HFLIP:
533                 data = reg_read(client, MT9V022_READ_MODE);
534                 if (data < 0)
535                         return -EIO;
536                 ctrl->value = !!(data & 0x20);
537                 break;
538         case V4L2_CID_EXPOSURE_AUTO:
539                 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
540                 if (data < 0)
541                         return -EIO;
542                 ctrl->value = !!(data & 0x1);
543                 break;
544         case V4L2_CID_AUTOGAIN:
545                 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
546                 if (data < 0)
547                         return -EIO;
548                 ctrl->value = !!(data & 0x2);
549                 break;
550         }
551         return 0;
552 }
553
554 static int mt9v022_set_control(struct soc_camera_device *icd,
555                                struct v4l2_control *ctrl)
556 {
557         int data;
558         struct i2c_client *client = to_i2c_client(icd->control);
559         const struct v4l2_queryctrl *qctrl;
560
561         qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
562
563         if (!qctrl)
564                 return -EINVAL;
565
566         switch (ctrl->id) {
567         case V4L2_CID_VFLIP:
568                 if (ctrl->value)
569                         data = reg_set(client, MT9V022_READ_MODE, 0x10);
570                 else
571                         data = reg_clear(client, MT9V022_READ_MODE, 0x10);
572                 if (data < 0)
573                         return -EIO;
574                 break;
575         case V4L2_CID_HFLIP:
576                 if (ctrl->value)
577                         data = reg_set(client, MT9V022_READ_MODE, 0x20);
578                 else
579                         data = reg_clear(client, MT9V022_READ_MODE, 0x20);
580                 if (data < 0)
581                         return -EIO;
582                 break;
583         case V4L2_CID_GAIN:
584                 /* mt9v022 has minimum == default */
585                 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
586                         return -EINVAL;
587                 else {
588                         unsigned long range = qctrl->maximum - qctrl->minimum;
589                         /* Datasheet says 16 to 64. autogain only works properly
590                          * after setting gain to maximum 14. Larger values
591                          * produce "white fly" noise effect. On the whole,
592                          * manually setting analog gain does no good. */
593                         unsigned long gain = ((ctrl->value - qctrl->minimum) *
594                                               10 + range / 2) / range + 4;
595                         if (gain >= 32)
596                                 gain &= ~1;
597                         /* The user wants to set gain manually, hope, she
598                          * knows, what she's doing... Switch AGC off. */
599
600                         if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
601                                 return -EIO;
602
603                         dev_info(&icd->dev, "Setting gain from %d to %lu\n",
604                                  reg_read(client, MT9V022_ANALOG_GAIN), gain);
605                         if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
606                                 return -EIO;
607                         icd->gain = ctrl->value;
608                 }
609                 break;
610         case V4L2_CID_EXPOSURE:
611                 /* mt9v022 has maximum == default */
612                 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
613                         return -EINVAL;
614                 else {
615                         unsigned long range = qctrl->maximum - qctrl->minimum;
616                         unsigned long shutter = ((ctrl->value - qctrl->minimum) *
617                                                  479 + range / 2) / range + 1;
618                         /* The user wants to set shutter width manually, hope,
619                          * she knows, what she's doing... Switch AEC off. */
620
621                         if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
622                                 return -EIO;
623
624                         dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
625                                 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
626                                 shutter);
627                         if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
628                                       shutter) < 0)
629                                 return -EIO;
630                         icd->exposure = ctrl->value;
631                 }
632                 break;
633         case V4L2_CID_AUTOGAIN:
634                 if (ctrl->value)
635                         data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2);
636                 else
637                         data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2);
638                 if (data < 0)
639                         return -EIO;
640                 break;
641         case V4L2_CID_EXPOSURE_AUTO:
642                 if (ctrl->value)
643                         data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
644                 else
645                         data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
646                 if (data < 0)
647                         return -EIO;
648                 break;
649         }
650         return 0;
651 }
652
653 /* Interface active, can use i2c. If it fails, it can indeed mean, that
654  * this wasn't our capture interface, so, we wait for the right one */
655 static int mt9v022_video_probe(struct soc_camera_device *icd)
656 {
657         struct i2c_client *client = to_i2c_client(icd->control);
658         struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
659         struct soc_camera_link *icl = client->dev.platform_data;
660         s32 data;
661         int ret;
662         unsigned long flags;
663
664         if (!icd->dev.parent ||
665             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
666                 return -ENODEV;
667
668         /* Read out the chip version register */
669         data = reg_read(client, MT9V022_CHIP_VERSION);
670
671         /* must be 0x1311 or 0x1313 */
672         if (data != 0x1311 && data != 0x1313) {
673                 ret = -ENODEV;
674                 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
675                          data);
676                 goto ei2c;
677         }
678
679         /* Soft reset */
680         ret = reg_write(client, MT9V022_RESET, 1);
681         if (ret < 0)
682                 goto ei2c;
683         /* 15 clock cycles */
684         udelay(200);
685         if (reg_read(client, MT9V022_RESET)) {
686                 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
687                 goto ei2c;
688         }
689
690         /* Set monochrome or colour sensor type */
691         if (sensor_type && (!strcmp("colour", sensor_type) ||
692                             !strcmp("color", sensor_type))) {
693                 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
694                 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
695                 icd->formats = mt9v022_colour_formats;
696         } else {
697                 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
698                 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
699                 icd->formats = mt9v022_monochrome_formats;
700         }
701
702         if (ret < 0)
703                 goto eisis;
704
705         icd->num_formats = 0;
706
707         /*
708          * This is a 10bit sensor, so by default we only allow 10bit.
709          * The platform may support different bus widths due to
710          * different routing of the data lines.
711          */
712         if (icl->query_bus_param)
713                 flags = icl->query_bus_param(icl);
714         else
715                 flags = SOCAM_DATAWIDTH_10;
716
717         if (flags & SOCAM_DATAWIDTH_10)
718                 icd->num_formats++;
719         else
720                 icd->formats++;
721
722         if (flags & SOCAM_DATAWIDTH_8)
723                 icd->num_formats++;
724
725         ret = soc_camera_video_start(icd);
726         if (ret < 0)
727                 goto eisis;
728
729         dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
730                  data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
731                  "monochrome" : "colour");
732
733         return 0;
734
735 eisis:
736 ei2c:
737         return ret;
738 }
739
740 static void mt9v022_video_remove(struct soc_camera_device *icd)
741 {
742         struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
743         struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
744
745         dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
746                 icd->dev.parent, icd->vdev);
747         soc_camera_video_stop(icd);
748         if (icl->free_bus)
749                 icl->free_bus(icl);
750 }
751
752 static int mt9v022_probe(struct i2c_client *client,
753                          const struct i2c_device_id *did)
754 {
755         struct mt9v022 *mt9v022;
756         struct soc_camera_device *icd;
757         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
758         struct soc_camera_link *icl = client->dev.platform_data;
759         int ret;
760
761         if (!icl) {
762                 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
763                 return -EINVAL;
764         }
765
766         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
767                 dev_warn(&adapter->dev,
768                          "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
769                 return -EIO;
770         }
771
772         mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
773         if (!mt9v022)
774                 return -ENOMEM;
775
776         mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
777         mt9v022->client = client;
778         i2c_set_clientdata(client, mt9v022);
779
780         icd = &mt9v022->icd;
781         icd->ops        = &mt9v022_ops;
782         icd->control    = &client->dev;
783         icd->x_min      = 1;
784         icd->y_min      = 4;
785         icd->x_current  = 1;
786         icd->y_current  = 4;
787         icd->width_min  = 48;
788         icd->width_max  = 752;
789         icd->height_min = 32;
790         icd->height_max = 480;
791         icd->y_skip_top = 1;
792         icd->iface      = icl->bus_id;
793
794         ret = soc_camera_device_register(icd);
795         if (ret)
796                 goto eisdr;
797
798         return 0;
799
800 eisdr:
801         kfree(mt9v022);
802         return ret;
803 }
804
805 static int mt9v022_remove(struct i2c_client *client)
806 {
807         struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
808
809         soc_camera_device_unregister(&mt9v022->icd);
810         kfree(mt9v022);
811
812         return 0;
813 }
814 static const struct i2c_device_id mt9v022_id[] = {
815         { "mt9v022", 0 },
816         { }
817 };
818 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
819
820 static struct i2c_driver mt9v022_i2c_driver = {
821         .driver = {
822                 .name = "mt9v022",
823         },
824         .probe          = mt9v022_probe,
825         .remove         = mt9v022_remove,
826         .id_table       = mt9v022_id,
827 };
828
829 static int __init mt9v022_mod_init(void)
830 {
831         return i2c_add_driver(&mt9v022_i2c_driver);
832 }
833
834 static void __exit mt9v022_mod_exit(void)
835 {
836         i2c_del_driver(&mt9v022_i2c_driver);
837 }
838
839 module_init(mt9v022_mod_init);
840 module_exit(mt9v022_mod_exit);
841
842 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
843 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
844 MODULE_LICENSE("GPL");