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