i2c: Add support for device alias names
[safe/jmp/linux-2.6] / drivers / media / video / mt9m001.c
1 /*
2  * Driver for MT9M001 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/log2.h>
15
16 #include <media/v4l2-common.h>
17 #include <media/v4l2-chip-ident.h>
18 #include <media/soc_camera.h>
19
20 #ifdef CONFIG_MT9M001_PCA9536_SWITCH
21 #include <asm/gpio.h>
22 #endif
23
24 /* mt9m001 i2c address 0x5d
25  * The platform has to define i2c_board_info
26  * and call i2c_register_board_info() */
27
28 /* mt9m001 selected register addresses */
29 #define MT9M001_CHIP_VERSION            0x00
30 #define MT9M001_ROW_START               0x01
31 #define MT9M001_COLUMN_START            0x02
32 #define MT9M001_WINDOW_HEIGHT           0x03
33 #define MT9M001_WINDOW_WIDTH            0x04
34 #define MT9M001_HORIZONTAL_BLANKING     0x05
35 #define MT9M001_VERTICAL_BLANKING       0x06
36 #define MT9M001_OUTPUT_CONTROL          0x07
37 #define MT9M001_SHUTTER_WIDTH           0x09
38 #define MT9M001_FRAME_RESTART           0x0b
39 #define MT9M001_SHUTTER_DELAY           0x0c
40 #define MT9M001_RESET                   0x0d
41 #define MT9M001_READ_OPTIONS1           0x1e
42 #define MT9M001_READ_OPTIONS2           0x20
43 #define MT9M001_GLOBAL_GAIN             0x35
44 #define MT9M001_CHIP_ENABLE             0xF1
45
46 static const struct soc_camera_data_format mt9m001_colour_formats[] = {
47         /* Order important: first natively supported,
48          * second supported with a GPIO extender */
49         {
50                 .name           = "Bayer (sRGB) 10 bit",
51                 .depth          = 10,
52                 .fourcc         = V4L2_PIX_FMT_SBGGR16,
53                 .colorspace     = V4L2_COLORSPACE_SRGB,
54         }, {
55                 .name           = "Bayer (sRGB) 8 bit",
56                 .depth          = 8,
57                 .fourcc         = V4L2_PIX_FMT_SBGGR8,
58                 .colorspace     = V4L2_COLORSPACE_SRGB,
59         }
60 };
61
62 static const struct soc_camera_data_format mt9m001_monochrome_formats[] = {
63         /* Order important - see above */
64         {
65                 .name           = "Monochrome 10 bit",
66                 .depth          = 10,
67                 .fourcc         = V4L2_PIX_FMT_Y16,
68         }, {
69                 .name           = "Monochrome 8 bit",
70                 .depth          = 8,
71                 .fourcc         = V4L2_PIX_FMT_GREY,
72         },
73 };
74
75 struct mt9m001 {
76         struct i2c_client *client;
77         struct soc_camera_device icd;
78         int model;      /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
79         int switch_gpio;
80         unsigned char autoexposure;
81         unsigned char datawidth;
82 };
83
84 static int reg_read(struct soc_camera_device *icd, const u8 reg)
85 {
86         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
87         struct i2c_client *client = mt9m001->client;
88         s32 data = i2c_smbus_read_word_data(client, reg);
89         return data < 0 ? data : swab16(data);
90 }
91
92 static int reg_write(struct soc_camera_device *icd, const u8 reg,
93                      const u16 data)
94 {
95         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
96         return i2c_smbus_write_word_data(mt9m001->client, reg, swab16(data));
97 }
98
99 static int reg_set(struct soc_camera_device *icd, const u8 reg,
100                    const u16 data)
101 {
102         int ret;
103
104         ret = reg_read(icd, reg);
105         if (ret < 0)
106                 return ret;
107         return reg_write(icd, reg, ret | data);
108 }
109
110 static int reg_clear(struct soc_camera_device *icd, const u8 reg,
111                      const u16 data)
112 {
113         int ret;
114
115         ret = reg_read(icd, reg);
116         if (ret < 0)
117                 return ret;
118         return reg_write(icd, reg, ret & ~data);
119 }
120
121 static int mt9m001_init(struct soc_camera_device *icd)
122 {
123         int ret;
124
125         /* Disable chip, synchronous option update */
126         dev_dbg(icd->vdev->dev, "%s\n", __func__);
127
128         ret = reg_write(icd, MT9M001_RESET, 1);
129         if (ret >= 0)
130                 ret = reg_write(icd, MT9M001_RESET, 0);
131         if (ret >= 0)
132                 ret = reg_write(icd, MT9M001_OUTPUT_CONTROL, 0);
133
134         return ret >= 0 ? 0 : -EIO;
135 }
136
137 static int mt9m001_release(struct soc_camera_device *icd)
138 {
139         /* Disable the chip */
140         reg_write(icd, MT9M001_OUTPUT_CONTROL, 0);
141         return 0;
142 }
143
144 static int mt9m001_start_capture(struct soc_camera_device *icd)
145 {
146         /* Switch to master "normal" mode */
147         if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 2) < 0)
148                 return -EIO;
149         return 0;
150 }
151
152 static int mt9m001_stop_capture(struct soc_camera_device *icd)
153 {
154         /* Stop sensor readout */
155         if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 0) < 0)
156                 return -EIO;
157         return 0;
158 }
159
160 static int bus_switch_request(struct mt9m001 *mt9m001,
161                               struct soc_camera_link *icl)
162 {
163 #ifdef CONFIG_MT9M001_PCA9536_SWITCH
164         int ret;
165         unsigned int gpio = icl->gpio;
166
167         if (gpio_is_valid(gpio)) {
168                 /* We have a data bus switch. */
169                 ret = gpio_request(gpio, "mt9m001");
170                 if (ret < 0) {
171                         dev_err(&mt9m001->client->dev, "Cannot get GPIO %u\n",
172                                 gpio);
173                         return ret;
174                 }
175
176                 ret = gpio_direction_output(gpio, 0);
177                 if (ret < 0) {
178                         dev_err(&mt9m001->client->dev,
179                                 "Cannot set GPIO %u to output\n", gpio);
180                         gpio_free(gpio);
181                         return ret;
182                 }
183         }
184
185         mt9m001->switch_gpio = gpio;
186 #else
187         mt9m001->switch_gpio = -EINVAL;
188 #endif
189         return 0;
190 }
191
192 static void bus_switch_release(struct mt9m001 *mt9m001)
193 {
194 #ifdef CONFIG_MT9M001_PCA9536_SWITCH
195         if (gpio_is_valid(mt9m001->switch_gpio))
196                 gpio_free(mt9m001->switch_gpio);
197 #endif
198 }
199
200 static int bus_switch_act(struct mt9m001 *mt9m001, int go8bit)
201 {
202 #ifdef CONFIG_MT9M001_PCA9536_SWITCH
203         if (!gpio_is_valid(mt9m001->switch_gpio))
204                 return -ENODEV;
205
206         gpio_set_value_cansleep(mt9m001->switch_gpio, go8bit);
207         return 0;
208 #else
209         return -ENODEV;
210 #endif
211 }
212
213 static int bus_switch_possible(struct mt9m001 *mt9m001)
214 {
215 #ifdef CONFIG_MT9M001_PCA9536_SWITCH
216         return gpio_is_valid(mt9m001->switch_gpio);
217 #else
218         return 0;
219 #endif
220 }
221
222 static int mt9m001_set_bus_param(struct soc_camera_device *icd,
223                                  unsigned long flags)
224 {
225         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
226         unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
227         int ret;
228
229         /* Flags validity verified in test_bus_param */
230
231         if ((mt9m001->datawidth != 10 && (width_flag == SOCAM_DATAWIDTH_10)) ||
232             (mt9m001->datawidth != 9  && (width_flag == SOCAM_DATAWIDTH_9)) ||
233             (mt9m001->datawidth != 8  && (width_flag == SOCAM_DATAWIDTH_8))) {
234                 /* Well, we actually only can do 10 or 8 bits... */
235                 if (width_flag == SOCAM_DATAWIDTH_9)
236                         return -EINVAL;
237                 ret = bus_switch_act(mt9m001,
238                                      width_flag == SOCAM_DATAWIDTH_8);
239                 if (ret < 0)
240                         return ret;
241
242                 mt9m001->datawidth = width_flag == SOCAM_DATAWIDTH_8 ? 8 : 10;
243         }
244
245         return 0;
246 }
247
248 static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
249 {
250         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
251         unsigned int width_flag = SOCAM_DATAWIDTH_10;
252
253         if (bus_switch_possible(mt9m001))
254                 width_flag |= SOCAM_DATAWIDTH_8;
255
256         /* MT9M001 has all capture_format parameters fixed */
257         return SOCAM_PCLK_SAMPLE_RISING |
258                 SOCAM_HSYNC_ACTIVE_HIGH |
259                 SOCAM_VSYNC_ACTIVE_HIGH |
260                 SOCAM_MASTER |
261                 width_flag;
262 }
263
264 static int mt9m001_set_fmt_cap(struct soc_camera_device *icd,
265                 __u32 pixfmt, struct v4l2_rect *rect)
266 {
267         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
268         int ret;
269         const u16 hblank = 9, vblank = 25;
270
271         /* Blanking and start values - default... */
272         ret = reg_write(icd, MT9M001_HORIZONTAL_BLANKING, hblank);
273         if (ret >= 0)
274                 ret = reg_write(icd, MT9M001_VERTICAL_BLANKING, vblank);
275
276         /* The caller provides a supported format, as verified per
277          * call to icd->try_fmt_cap() */
278         if (ret >= 0)
279                 ret = reg_write(icd, MT9M001_COLUMN_START, rect->left);
280         if (ret >= 0)
281                 ret = reg_write(icd, MT9M001_ROW_START, rect->top);
282         if (ret >= 0)
283                 ret = reg_write(icd, MT9M001_WINDOW_WIDTH, rect->width - 1);
284         if (ret >= 0)
285                 ret = reg_write(icd, MT9M001_WINDOW_HEIGHT,
286                                 rect->height + icd->y_skip_top - 1);
287         if (ret >= 0 && mt9m001->autoexposure) {
288                 ret = reg_write(icd, MT9M001_SHUTTER_WIDTH,
289                                 rect->height + icd->y_skip_top + vblank);
290                 if (ret >= 0) {
291                         const struct v4l2_queryctrl *qctrl =
292                                 soc_camera_find_qctrl(icd->ops,
293                                                       V4L2_CID_EXPOSURE);
294                         icd->exposure = (524 + (rect->height + icd->y_skip_top +
295                                                 vblank - 1) *
296                                          (qctrl->maximum - qctrl->minimum)) /
297                                 1048 + qctrl->minimum;
298                 }
299         }
300
301         return ret < 0 ? ret : 0;
302 }
303
304 static int mt9m001_try_fmt_cap(struct soc_camera_device *icd,
305                                struct v4l2_format *f)
306 {
307         if (f->fmt.pix.height < 32 + icd->y_skip_top)
308                 f->fmt.pix.height = 32 + icd->y_skip_top;
309         if (f->fmt.pix.height > 1024 + icd->y_skip_top)
310                 f->fmt.pix.height = 1024 + icd->y_skip_top;
311         if (f->fmt.pix.width < 48)
312                 f->fmt.pix.width = 48;
313         if (f->fmt.pix.width > 1280)
314                 f->fmt.pix.width = 1280;
315         f->fmt.pix.width &= ~0x01; /* has to be even, unsure why was ~3 */
316
317         return 0;
318 }
319
320 static int mt9m001_get_chip_id(struct soc_camera_device *icd,
321                                struct v4l2_chip_ident *id)
322 {
323         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
324
325         if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR)
326                 return -EINVAL;
327
328         if (id->match_chip != mt9m001->client->addr)
329                 return -ENODEV;
330
331         id->ident       = mt9m001->model;
332         id->revision    = 0;
333
334         return 0;
335 }
336
337 #ifdef CONFIG_VIDEO_ADV_DEBUG
338 static int mt9m001_get_register(struct soc_camera_device *icd,
339                                 struct v4l2_register *reg)
340 {
341         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
342
343         if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
344                 return -EINVAL;
345
346         if (reg->match_chip != mt9m001->client->addr)
347                 return -ENODEV;
348
349         reg->val = reg_read(icd, reg->reg);
350
351         if (reg->val > 0xffff)
352                 return -EIO;
353
354         return 0;
355 }
356
357 static int mt9m001_set_register(struct soc_camera_device *icd,
358                                 struct v4l2_register *reg)
359 {
360         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
361
362         if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
363                 return -EINVAL;
364
365         if (reg->match_chip != mt9m001->client->addr)
366                 return -ENODEV;
367
368         if (reg_write(icd, reg->reg, reg->val) < 0)
369                 return -EIO;
370
371         return 0;
372 }
373 #endif
374
375 const struct v4l2_queryctrl mt9m001_controls[] = {
376         {
377                 .id             = V4L2_CID_VFLIP,
378                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
379                 .name           = "Flip Vertically",
380                 .minimum        = 0,
381                 .maximum        = 1,
382                 .step           = 1,
383                 .default_value  = 0,
384         }, {
385                 .id             = V4L2_CID_GAIN,
386                 .type           = V4L2_CTRL_TYPE_INTEGER,
387                 .name           = "Gain",
388                 .minimum        = 0,
389                 .maximum        = 127,
390                 .step           = 1,
391                 .default_value  = 64,
392                 .flags          = V4L2_CTRL_FLAG_SLIDER,
393         }, {
394                 .id             = V4L2_CID_EXPOSURE,
395                 .type           = V4L2_CTRL_TYPE_INTEGER,
396                 .name           = "Exposure",
397                 .minimum        = 1,
398                 .maximum        = 255,
399                 .step           = 1,
400                 .default_value  = 255,
401                 .flags          = V4L2_CTRL_FLAG_SLIDER,
402         }, {
403                 .id             = V4L2_CID_EXPOSURE_AUTO,
404                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
405                 .name           = "Automatic Exposure",
406                 .minimum        = 0,
407                 .maximum        = 1,
408                 .step           = 1,
409                 .default_value  = 1,
410         }
411 };
412
413 static int mt9m001_video_probe(struct soc_camera_device *);
414 static void mt9m001_video_remove(struct soc_camera_device *);
415 static int mt9m001_get_control(struct soc_camera_device *, struct v4l2_control *);
416 static int mt9m001_set_control(struct soc_camera_device *, struct v4l2_control *);
417
418 static struct soc_camera_ops mt9m001_ops = {
419         .owner                  = THIS_MODULE,
420         .probe                  = mt9m001_video_probe,
421         .remove                 = mt9m001_video_remove,
422         .init                   = mt9m001_init,
423         .release                = mt9m001_release,
424         .start_capture          = mt9m001_start_capture,
425         .stop_capture           = mt9m001_stop_capture,
426         .set_fmt_cap            = mt9m001_set_fmt_cap,
427         .try_fmt_cap            = mt9m001_try_fmt_cap,
428         .set_bus_param          = mt9m001_set_bus_param,
429         .query_bus_param        = mt9m001_query_bus_param,
430         .controls               = mt9m001_controls,
431         .num_controls           = ARRAY_SIZE(mt9m001_controls),
432         .get_control            = mt9m001_get_control,
433         .set_control            = mt9m001_set_control,
434         .get_chip_id            = mt9m001_get_chip_id,
435 #ifdef CONFIG_VIDEO_ADV_DEBUG
436         .get_register           = mt9m001_get_register,
437         .set_register           = mt9m001_set_register,
438 #endif
439 };
440
441 static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
442 {
443         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
444         int data;
445
446         switch (ctrl->id) {
447         case V4L2_CID_VFLIP:
448                 data = reg_read(icd, MT9M001_READ_OPTIONS2);
449                 if (data < 0)
450                         return -EIO;
451                 ctrl->value = !!(data & 0x8000);
452                 break;
453         case V4L2_CID_EXPOSURE_AUTO:
454                 ctrl->value = mt9m001->autoexposure;
455                 break;
456         }
457         return 0;
458 }
459
460 static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
461 {
462         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
463         const struct v4l2_queryctrl *qctrl;
464         int data;
465
466         qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
467
468         if (!qctrl)
469                 return -EINVAL;
470
471         switch (ctrl->id) {
472         case V4L2_CID_VFLIP:
473                 if (ctrl->value)
474                         data = reg_set(icd, MT9M001_READ_OPTIONS2, 0x8000);
475                 else
476                         data = reg_clear(icd, MT9M001_READ_OPTIONS2, 0x8000);
477                 if (data < 0)
478                         return -EIO;
479                 break;
480         case V4L2_CID_GAIN:
481                 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
482                         return -EINVAL;
483                 /* See Datasheet Table 7, Gain settings. */
484                 if (ctrl->value <= qctrl->default_value) {
485                         /* Pack it into 0..1 step 0.125, register values 0..8 */
486                         unsigned long range = qctrl->default_value - qctrl->minimum;
487                         data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
488
489                         dev_dbg(&icd->dev, "Setting gain %d\n", data);
490                         data = reg_write(icd, MT9M001_GLOBAL_GAIN, data);
491                         if (data < 0)
492                                 return -EIO;
493                 } else {
494                         /* Pack it into 1.125..15 variable step, register values 9..67 */
495                         /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
496                         unsigned long range = qctrl->maximum - qctrl->default_value - 1;
497                         unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
498                                                111 + range / 2) / range + 9;
499
500                         if (gain <= 32)
501                                 data = gain;
502                         else if (gain <= 64)
503                                 data = ((gain - 32) * 16 + 16) / 32 + 80;
504                         else
505                                 data = ((gain - 64) * 7 + 28) / 56 + 96;
506
507                         dev_dbg(&icd->dev, "Setting gain from %d to %d\n",
508                                  reg_read(icd, MT9M001_GLOBAL_GAIN), data);
509                         data = reg_write(icd, MT9M001_GLOBAL_GAIN, data);
510                         if (data < 0)
511                                 return -EIO;
512                 }
513
514                 /* Success */
515                 icd->gain = ctrl->value;
516                 break;
517         case V4L2_CID_EXPOSURE:
518                 /* mt9m001 has maximum == default */
519                 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
520                         return -EINVAL;
521                 else {
522                         unsigned long range = qctrl->maximum - qctrl->minimum;
523                         unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
524                                                  range / 2) / range + 1;
525
526                         dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n",
527                                  reg_read(icd, MT9M001_SHUTTER_WIDTH), shutter);
528                         if (reg_write(icd, MT9M001_SHUTTER_WIDTH, shutter) < 0)
529                                 return -EIO;
530                         icd->exposure = ctrl->value;
531                         mt9m001->autoexposure = 0;
532                 }
533                 break;
534         case V4L2_CID_EXPOSURE_AUTO:
535                 if (ctrl->value) {
536                         const u16 vblank = 25;
537                         if (reg_write(icd, MT9M001_SHUTTER_WIDTH, icd->height +
538                                       icd->y_skip_top + vblank) < 0)
539                                 return -EIO;
540                         qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
541                         icd->exposure = (524 + (icd->height + icd->y_skip_top + vblank - 1) *
542                                          (qctrl->maximum - qctrl->minimum)) /
543                                 1048 + qctrl->minimum;
544                         mt9m001->autoexposure = 1;
545                 } else
546                         mt9m001->autoexposure = 0;
547                 break;
548         }
549         return 0;
550 }
551
552 /* Interface active, can use i2c. If it fails, it can indeed mean, that
553  * this wasn't our capture interface, so, we wait for the right one */
554 static int mt9m001_video_probe(struct soc_camera_device *icd)
555 {
556         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
557         s32 data;
558         int ret;
559
560         /* We must have a parent by now. And it cannot be a wrong one.
561          * So this entire test is completely redundant. */
562         if (!icd->dev.parent ||
563             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
564                 return -ENODEV;
565
566         /* Enable the chip */
567         data = reg_write(&mt9m001->icd, MT9M001_CHIP_ENABLE, 1);
568         dev_dbg(&icd->dev, "write: %d\n", data);
569
570         /* Read out the chip version register */
571         data = reg_read(icd, MT9M001_CHIP_VERSION);
572
573         /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
574         switch (data) {
575         case 0x8411:
576         case 0x8421:
577                 mt9m001->model = V4L2_IDENT_MT9M001C12ST;
578                 icd->formats = mt9m001_colour_formats;
579                 if (mt9m001->client->dev.platform_data)
580                         icd->num_formats = ARRAY_SIZE(mt9m001_colour_formats);
581                 else
582                         icd->num_formats = 1;
583                 break;
584         case 0x8431:
585                 mt9m001->model = V4L2_IDENT_MT9M001C12STM;
586                 icd->formats = mt9m001_monochrome_formats;
587                 if (mt9m001->client->dev.platform_data)
588                         icd->num_formats = ARRAY_SIZE(mt9m001_monochrome_formats);
589                 else
590                         icd->num_formats = 1;
591                 break;
592         default:
593                 ret = -ENODEV;
594                 dev_err(&icd->dev,
595                         "No MT9M001 chip detected, register read %x\n", data);
596                 goto ei2c;
597         }
598
599         dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
600                  data == 0x8431 ? "C12STM" : "C12ST");
601
602         /* Now that we know the model, we can start video */
603         ret = soc_camera_video_start(icd);
604         if (ret)
605                 goto eisis;
606
607         return 0;
608
609 eisis:
610 ei2c:
611         return ret;
612 }
613
614 static void mt9m001_video_remove(struct soc_camera_device *icd)
615 {
616         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
617
618         dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m001->client->addr,
619                 mt9m001->icd.dev.parent, mt9m001->icd.vdev);
620         soc_camera_video_stop(&mt9m001->icd);
621 }
622
623 static int mt9m001_probe(struct i2c_client *client,
624                          const struct i2c_device_id *did)
625 {
626         struct mt9m001 *mt9m001;
627         struct soc_camera_device *icd;
628         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
629         struct soc_camera_link *icl = client->dev.platform_data;
630         int ret;
631
632         if (!icl) {
633                 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
634                 return -EINVAL;
635         }
636
637         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
638                 dev_warn(&adapter->dev,
639                          "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
640                 return -EIO;
641         }
642
643         mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
644         if (!mt9m001)
645                 return -ENOMEM;
646
647         mt9m001->client = client;
648         i2c_set_clientdata(client, mt9m001);
649
650         /* Second stage probe - when a capture adapter is there */
651         icd = &mt9m001->icd;
652         icd->ops        = &mt9m001_ops;
653         icd->control    = &client->dev;
654         icd->x_min      = 20;
655         icd->y_min      = 12;
656         icd->x_current  = 20;
657         icd->y_current  = 12;
658         icd->width_min  = 48;
659         icd->width_max  = 1280;
660         icd->height_min = 32;
661         icd->height_max = 1024;
662         icd->y_skip_top = 1;
663         icd->iface      = icl->bus_id;
664         /* Default datawidth - this is the only width this camera (normally)
665          * supports. It is only with extra logic that it can support
666          * other widths. Therefore it seems to be a sensible default. */
667         mt9m001->datawidth = 10;
668         /* Simulated autoexposure. If enabled, we calculate shutter width
669          * ourselves in the driver based on vertical blanking and frame width */
670         mt9m001->autoexposure = 1;
671
672         ret = bus_switch_request(mt9m001, icl);
673         if (ret)
674                 goto eswinit;
675
676         ret = soc_camera_device_register(icd);
677         if (ret)
678                 goto eisdr;
679
680         return 0;
681
682 eisdr:
683         bus_switch_release(mt9m001);
684 eswinit:
685         kfree(mt9m001);
686         return ret;
687 }
688
689 static int mt9m001_remove(struct i2c_client *client)
690 {
691         struct mt9m001 *mt9m001 = i2c_get_clientdata(client);
692
693         soc_camera_device_unregister(&mt9m001->icd);
694         bus_switch_release(mt9m001);
695         kfree(mt9m001);
696
697         return 0;
698 }
699
700 static struct i2c_driver mt9m001_i2c_driver = {
701         .driver = {
702                 .name = "mt9m001",
703         },
704         .probe          = mt9m001_probe,
705         .remove         = mt9m001_remove,
706 };
707
708 static int __init mt9m001_mod_init(void)
709 {
710         return i2c_add_driver(&mt9m001_i2c_driver);
711 }
712
713 static void __exit mt9m001_mod_exit(void)
714 {
715         i2c_del_driver(&mt9m001_i2c_driver);
716 }
717
718 module_init(mt9m001_mod_init);
719 module_exit(mt9m001_mod_exit);
720
721 MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
722 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
723 MODULE_LICENSE("GPL");