Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
[pandora-kernel.git] / drivers / media / video / soc_camera.c
1 /*
2  * camera image capture (abstract) bus driver
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This driver provides an interface between platform-specific camera
7  * busses and camera devices. It should be used if the camera is
8  * connected not over a "proper" bus like PCI or USB, but over a
9  * special bus, like, for example, the Quick Capture interface on PXA270
10  * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
11  * It can handle multiple cameras and / or multiple busses, which can
12  * be used, e.g., in stereo-vision applications.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  */
18
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/slab.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/vmalloc.h>
31
32 #include <media/soc_camera.h>
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-dev.h>
36 #include <media/videobuf-core.h>
37 #include <media/soc_mediabus.h>
38
39 /* Default to VGA resolution */
40 #define DEFAULT_WIDTH   640
41 #define DEFAULT_HEIGHT  480
42
43 static LIST_HEAD(hosts);
44 static LIST_HEAD(devices);
45 static DEFINE_MUTEX(list_lock);         /* Protects the list of hosts */
46
47 static int soc_camera_power_set(struct soc_camera_device *icd,
48                                 struct soc_camera_link *icl,
49                                 int power_on)
50 {
51         int ret;
52
53         if (power_on) {
54                 ret = regulator_bulk_enable(icl->num_regulators,
55                                             icl->regulators);
56                 if (ret < 0) {
57                         dev_err(&icd->dev, "Cannot enable regulators\n");
58                         return ret;
59                 }
60
61                 if (icl->power)
62                         ret = icl->power(icd->pdev, power_on);
63                 if (ret < 0) {
64                         dev_err(&icd->dev,
65                                 "Platform failed to power-on the camera.\n");
66
67                         regulator_bulk_disable(icl->num_regulators,
68                                                icl->regulators);
69                         return ret;
70                 }
71         } else {
72                 ret = 0;
73                 if (icl->power)
74                         ret = icl->power(icd->pdev, 0);
75                 if (ret < 0) {
76                         dev_err(&icd->dev,
77                                 "Platform failed to power-off the camera.\n");
78                         return ret;
79                 }
80
81                 ret = regulator_bulk_disable(icl->num_regulators,
82                                              icl->regulators);
83                 if (ret < 0) {
84                         dev_err(&icd->dev, "Cannot disable regulators\n");
85                         return ret;
86                 }
87         }
88
89         return 0;
90 }
91
92 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
93         struct soc_camera_device *icd, unsigned int fourcc)
94 {
95         unsigned int i;
96
97         for (i = 0; i < icd->num_user_formats; i++)
98                 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
99                         return icd->user_formats + i;
100         return NULL;
101 }
102 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
103
104 /**
105  * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
106  * @icl:        camera platform parameters
107  * @flags:      flags to be inverted according to platform configuration
108  * @return:     resulting flags
109  */
110 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
111                                             unsigned long flags)
112 {
113         unsigned long f;
114
115         /* If only one of the two polarities is supported, switch to the opposite */
116         if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
117                 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
118                 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
119                         flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
120         }
121
122         if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
123                 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
124                 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
125                         flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
126         }
127
128         if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
129                 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
130                 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
131                         flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
132         }
133
134         return flags;
135 }
136 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
137
138 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
139                                       struct v4l2_format *f)
140 {
141         struct soc_camera_device *icd = file->private_data;
142         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
143
144         WARN_ON(priv != file->private_data);
145
146         /* limit format to hardware capabilities */
147         return ici->ops->try_fmt(icd, f);
148 }
149
150 static int soc_camera_enum_input(struct file *file, void *priv,
151                                  struct v4l2_input *inp)
152 {
153         struct soc_camera_device *icd = file->private_data;
154         int ret = 0;
155
156         if (inp->index != 0)
157                 return -EINVAL;
158
159         if (icd->ops->enum_input)
160                 ret = icd->ops->enum_input(icd, inp);
161         else {
162                 /* default is camera */
163                 inp->type = V4L2_INPUT_TYPE_CAMERA;
164                 inp->std  = V4L2_STD_UNKNOWN;
165                 strcpy(inp->name, "Camera");
166         }
167
168         return ret;
169 }
170
171 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
172 {
173         *i = 0;
174
175         return 0;
176 }
177
178 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
179 {
180         if (i > 0)
181                 return -EINVAL;
182
183         return 0;
184 }
185
186 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
187 {
188         struct soc_camera_device *icd = file->private_data;
189         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
190
191         return v4l2_subdev_call(sd, core, s_std, *a);
192 }
193
194 static int soc_camera_reqbufs(struct file *file, void *priv,
195                               struct v4l2_requestbuffers *p)
196 {
197         int ret;
198         struct soc_camera_device *icd = file->private_data;
199         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
200
201         WARN_ON(priv != file->private_data);
202
203         if (icd->streamer && icd->streamer != file)
204                 return -EBUSY;
205
206         ret = videobuf_reqbufs(&icd->vb_vidq, p);
207         if (ret < 0)
208                 return ret;
209
210         ret = ici->ops->reqbufs(icd, p);
211         if (!ret && !icd->streamer)
212                 icd->streamer = file;
213
214         return ret;
215 }
216
217 static int soc_camera_querybuf(struct file *file, void *priv,
218                                struct v4l2_buffer *p)
219 {
220         struct soc_camera_device *icd = file->private_data;
221
222         WARN_ON(priv != file->private_data);
223
224         return videobuf_querybuf(&icd->vb_vidq, p);
225 }
226
227 static int soc_camera_qbuf(struct file *file, void *priv,
228                            struct v4l2_buffer *p)
229 {
230         struct soc_camera_device *icd = file->private_data;
231
232         WARN_ON(priv != file->private_data);
233
234         if (icd->streamer != file)
235                 return -EBUSY;
236
237         return videobuf_qbuf(&icd->vb_vidq, p);
238 }
239
240 static int soc_camera_dqbuf(struct file *file, void *priv,
241                             struct v4l2_buffer *p)
242 {
243         struct soc_camera_device *icd = file->private_data;
244
245         WARN_ON(priv != file->private_data);
246
247         if (icd->streamer != file)
248                 return -EBUSY;
249
250         return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
251 }
252
253 /* Always entered with .video_lock held */
254 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
255 {
256         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
257         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
258         unsigned int i, fmts = 0, raw_fmts = 0;
259         int ret;
260         enum v4l2_mbus_pixelcode code;
261
262         while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
263                 raw_fmts++;
264
265         if (!ici->ops->get_formats)
266                 /*
267                  * Fallback mode - the host will have to serve all
268                  * sensor-provided formats one-to-one to the user
269                  */
270                 fmts = raw_fmts;
271         else
272                 /*
273                  * First pass - only count formats this host-sensor
274                  * configuration can provide
275                  */
276                 for (i = 0; i < raw_fmts; i++) {
277                         ret = ici->ops->get_formats(icd, i, NULL);
278                         if (ret < 0)
279                                 return ret;
280                         fmts += ret;
281                 }
282
283         if (!fmts)
284                 return -ENXIO;
285
286         icd->user_formats =
287                 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
288         if (!icd->user_formats)
289                 return -ENOMEM;
290
291         icd->num_user_formats = fmts;
292
293         dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
294
295         /* Second pass - actually fill data formats */
296         fmts = 0;
297         for (i = 0; i < raw_fmts; i++)
298                 if (!ici->ops->get_formats) {
299                         v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
300                         icd->user_formats[i].host_fmt =
301                                 soc_mbus_get_fmtdesc(code);
302                         icd->user_formats[i].code = code;
303                 } else {
304                         ret = ici->ops->get_formats(icd, i,
305                                                     &icd->user_formats[fmts]);
306                         if (ret < 0)
307                                 goto egfmt;
308                         fmts += ret;
309                 }
310
311         icd->current_fmt = &icd->user_formats[0];
312
313         return 0;
314
315 egfmt:
316         icd->num_user_formats = 0;
317         vfree(icd->user_formats);
318         return ret;
319 }
320
321 /* Always entered with .video_lock held */
322 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
323 {
324         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
325
326         if (ici->ops->put_formats)
327                 ici->ops->put_formats(icd);
328         icd->current_fmt = NULL;
329         icd->num_user_formats = 0;
330         vfree(icd->user_formats);
331         icd->user_formats = NULL;
332 }
333
334 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
335         ((x) >> 24) & 0xff
336
337 /* Called with .vb_lock held, or from the first open(2), see comment there */
338 static int soc_camera_set_fmt(struct soc_camera_device *icd,
339                               struct v4l2_format *f)
340 {
341         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
342         struct v4l2_pix_format *pix = &f->fmt.pix;
343         int ret;
344
345         dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
346                 pixfmtstr(pix->pixelformat), pix->width, pix->height);
347
348         /* We always call try_fmt() before set_fmt() or set_crop() */
349         ret = ici->ops->try_fmt(icd, f);
350         if (ret < 0)
351                 return ret;
352
353         ret = ici->ops->set_fmt(icd, f);
354         if (ret < 0) {
355                 return ret;
356         } else if (!icd->current_fmt ||
357                    icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
358                 dev_err(&icd->dev,
359                         "Host driver hasn't set up current format correctly!\n");
360                 return -EINVAL;
361         }
362
363         icd->user_width         = pix->width;
364         icd->user_height        = pix->height;
365         icd->colorspace         = pix->colorspace;
366         icd->vb_vidq.field      =
367                 icd->field      = pix->field;
368
369         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
370                 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
371                          f->type);
372
373         dev_dbg(&icd->dev, "set width: %d height: %d\n",
374                 icd->user_width, icd->user_height);
375
376         /* set physical bus parameters */
377         return ici->ops->set_bus_param(icd, pix->pixelformat);
378 }
379
380 static int soc_camera_open(struct file *file)
381 {
382         struct video_device *vdev = video_devdata(file);
383         struct soc_camera_device *icd = container_of(vdev->parent,
384                                                      struct soc_camera_device,
385                                                      dev);
386         struct soc_camera_link *icl = to_soc_camera_link(icd);
387         struct soc_camera_host *ici;
388         int ret;
389
390         if (!icd->ops)
391                 /* No device driver attached */
392                 return -ENODEV;
393
394         ici = to_soc_camera_host(icd->dev.parent);
395
396         if (!try_module_get(ici->ops->owner)) {
397                 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
398                 return -EINVAL;
399         }
400
401         icd->use_count++;
402
403         /* Now we really have to activate the camera */
404         if (icd->use_count == 1) {
405                 /* Restore parameters before the last close() per V4L2 API */
406                 struct v4l2_format f = {
407                         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
408                         .fmt.pix = {
409                                 .width          = icd->user_width,
410                                 .height         = icd->user_height,
411                                 .field          = icd->field,
412                                 .colorspace     = icd->colorspace,
413                                 .pixelformat    =
414                                         icd->current_fmt->host_fmt->fourcc,
415                         },
416                 };
417
418                 ret = soc_camera_power_set(icd, icl, 1);
419                 if (ret < 0)
420                         goto epower;
421
422                 /* The camera could have been already on, try to reset */
423                 if (icl->reset)
424                         icl->reset(icd->pdev);
425
426                 ret = ici->ops->add(icd);
427                 if (ret < 0) {
428                         dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
429                         goto eiciadd;
430                 }
431
432                 pm_runtime_enable(&icd->vdev->dev);
433                 ret = pm_runtime_resume(&icd->vdev->dev);
434                 if (ret < 0 && ret != -ENOSYS)
435                         goto eresume;
436
437                 /*
438                  * Try to configure with default parameters. Notice: this is the
439                  * very first open, so, we cannot race against other calls,
440                  * apart from someone else calling open() simultaneously, but
441                  * .video_lock is protecting us against it.
442                  */
443                 ret = soc_camera_set_fmt(icd, &f);
444                 if (ret < 0)
445                         goto esfmt;
446
447                 ici->ops->init_videobuf(&icd->vb_vidq, icd);
448         }
449
450         file->private_data = icd;
451         dev_dbg(&icd->dev, "camera device open\n");
452
453         return 0;
454
455         /*
456          * First four errors are entered with the .video_lock held
457          * and use_count == 1
458          */
459 esfmt:
460         pm_runtime_disable(&icd->vdev->dev);
461 eresume:
462         ici->ops->remove(icd);
463 eiciadd:
464         soc_camera_power_set(icd, icl, 0);
465 epower:
466         icd->use_count--;
467         module_put(ici->ops->owner);
468
469         return ret;
470 }
471
472 static int soc_camera_close(struct file *file)
473 {
474         struct soc_camera_device *icd = file->private_data;
475         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
476
477         icd->use_count--;
478         if (!icd->use_count) {
479                 struct soc_camera_link *icl = to_soc_camera_link(icd);
480
481                 pm_runtime_suspend(&icd->vdev->dev);
482                 pm_runtime_disable(&icd->vdev->dev);
483
484                 ici->ops->remove(icd);
485
486                 soc_camera_power_set(icd, icl, 0);
487         }
488
489         if (icd->streamer == file)
490                 icd->streamer = NULL;
491
492         module_put(ici->ops->owner);
493
494         dev_dbg(&icd->dev, "camera device close\n");
495
496         return 0;
497 }
498
499 static ssize_t soc_camera_read(struct file *file, char __user *buf,
500                                size_t count, loff_t *ppos)
501 {
502         struct soc_camera_device *icd = file->private_data;
503         int err = -EINVAL;
504
505         dev_err(&icd->dev, "camera device read not implemented\n");
506
507         return err;
508 }
509
510 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
511 {
512         struct soc_camera_device *icd = file->private_data;
513         int err;
514
515         dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
516
517         if (icd->streamer != file)
518                 return -EBUSY;
519
520         err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
521
522         dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
523                 (unsigned long)vma->vm_start,
524                 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
525                 err);
526
527         return err;
528 }
529
530 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
531 {
532         struct soc_camera_device *icd = file->private_data;
533         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
534
535         if (icd->streamer != file)
536                 return -EBUSY;
537
538         if (list_empty(&icd->vb_vidq.stream)) {
539                 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
540                 return POLLERR;
541         }
542
543         return ici->ops->poll(file, pt);
544 }
545
546 static struct v4l2_file_operations soc_camera_fops = {
547         .owner          = THIS_MODULE,
548         .open           = soc_camera_open,
549         .release        = soc_camera_close,
550         .unlocked_ioctl = video_ioctl2,
551         .read           = soc_camera_read,
552         .mmap           = soc_camera_mmap,
553         .poll           = soc_camera_poll,
554 };
555
556 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
557                                     struct v4l2_format *f)
558 {
559         struct soc_camera_device *icd = file->private_data;
560         int ret;
561
562         WARN_ON(priv != file->private_data);
563
564         if (icd->streamer && icd->streamer != file)
565                 return -EBUSY;
566
567         if (icd->vb_vidq.bufs[0]) {
568                 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
569                 return -EBUSY;
570         }
571
572         ret = soc_camera_set_fmt(icd, f);
573
574         if (!ret && !icd->streamer)
575                 icd->streamer = file;
576
577         return ret;
578 }
579
580 static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
581                                        struct v4l2_fmtdesc *f)
582 {
583         struct soc_camera_device *icd = file->private_data;
584         const struct soc_mbus_pixelfmt *format;
585
586         WARN_ON(priv != file->private_data);
587
588         if (f->index >= icd->num_user_formats)
589                 return -EINVAL;
590
591         format = icd->user_formats[f->index].host_fmt;
592
593         if (format->name)
594                 strlcpy(f->description, format->name, sizeof(f->description));
595         f->pixelformat = format->fourcc;
596         return 0;
597 }
598
599 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
600                                     struct v4l2_format *f)
601 {
602         struct soc_camera_device *icd = file->private_data;
603         struct v4l2_pix_format *pix = &f->fmt.pix;
604
605         WARN_ON(priv != file->private_data);
606
607         pix->width              = icd->user_width;
608         pix->height             = icd->user_height;
609         pix->field              = icd->vb_vidq.field;
610         pix->pixelformat        = icd->current_fmt->host_fmt->fourcc;
611         pix->bytesperline       = soc_mbus_bytes_per_line(pix->width,
612                                                 icd->current_fmt->host_fmt);
613         pix->colorspace         = icd->colorspace;
614         if (pix->bytesperline < 0)
615                 return pix->bytesperline;
616         pix->sizeimage          = pix->height * pix->bytesperline;
617         dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
618                 icd->current_fmt->host_fmt->fourcc);
619         return 0;
620 }
621
622 static int soc_camera_querycap(struct file *file, void  *priv,
623                                struct v4l2_capability *cap)
624 {
625         struct soc_camera_device *icd = file->private_data;
626         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
627
628         WARN_ON(priv != file->private_data);
629
630         strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
631         return ici->ops->querycap(ici, cap);
632 }
633
634 static int soc_camera_streamon(struct file *file, void *priv,
635                                enum v4l2_buf_type i)
636 {
637         struct soc_camera_device *icd = file->private_data;
638         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
639         int ret;
640
641         WARN_ON(priv != file->private_data);
642
643         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
644                 return -EINVAL;
645
646         if (icd->streamer != file)
647                 return -EBUSY;
648
649         v4l2_subdev_call(sd, video, s_stream, 1);
650
651         /* This calls buf_queue from host driver's videobuf_queue_ops */
652         ret = videobuf_streamon(&icd->vb_vidq);
653
654         return ret;
655 }
656
657 static int soc_camera_streamoff(struct file *file, void *priv,
658                                 enum v4l2_buf_type i)
659 {
660         struct soc_camera_device *icd = file->private_data;
661         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
662
663         WARN_ON(priv != file->private_data);
664
665         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
666                 return -EINVAL;
667
668         if (icd->streamer != file)
669                 return -EBUSY;
670
671         /*
672          * This calls buf_release from host driver's videobuf_queue_ops for all
673          * remaining buffers. When the last buffer is freed, stop capture
674          */
675         videobuf_streamoff(&icd->vb_vidq);
676
677         v4l2_subdev_call(sd, video, s_stream, 0);
678
679         return 0;
680 }
681
682 static int soc_camera_queryctrl(struct file *file, void *priv,
683                                 struct v4l2_queryctrl *qc)
684 {
685         struct soc_camera_device *icd = file->private_data;
686         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
687         int i;
688
689         WARN_ON(priv != file->private_data);
690
691         if (!qc->id)
692                 return -EINVAL;
693
694         /* First check host controls */
695         for (i = 0; i < ici->ops->num_controls; i++)
696                 if (qc->id == ici->ops->controls[i].id) {
697                         memcpy(qc, &(ici->ops->controls[i]),
698                                 sizeof(*qc));
699                         return 0;
700                 }
701
702         /* Then device controls */
703         for (i = 0; i < icd->ops->num_controls; i++)
704                 if (qc->id == icd->ops->controls[i].id) {
705                         memcpy(qc, &(icd->ops->controls[i]),
706                                 sizeof(*qc));
707                         return 0;
708                 }
709
710         return -EINVAL;
711 }
712
713 static int soc_camera_g_ctrl(struct file *file, void *priv,
714                              struct v4l2_control *ctrl)
715 {
716         struct soc_camera_device *icd = file->private_data;
717         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
718         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
719         int ret;
720
721         WARN_ON(priv != file->private_data);
722
723         if (ici->ops->get_ctrl) {
724                 ret = ici->ops->get_ctrl(icd, ctrl);
725                 if (ret != -ENOIOCTLCMD)
726                         return ret;
727         }
728
729         return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
730 }
731
732 static int soc_camera_s_ctrl(struct file *file, void *priv,
733                              struct v4l2_control *ctrl)
734 {
735         struct soc_camera_device *icd = file->private_data;
736         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
737         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
738         int ret;
739
740         WARN_ON(priv != file->private_data);
741
742         if (ici->ops->set_ctrl) {
743                 ret = ici->ops->set_ctrl(icd, ctrl);
744                 if (ret != -ENOIOCTLCMD)
745                         return ret;
746         }
747
748         return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
749 }
750
751 static int soc_camera_cropcap(struct file *file, void *fh,
752                               struct v4l2_cropcap *a)
753 {
754         struct soc_camera_device *icd = file->private_data;
755         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
756
757         return ici->ops->cropcap(icd, a);
758 }
759
760 static int soc_camera_g_crop(struct file *file, void *fh,
761                              struct v4l2_crop *a)
762 {
763         struct soc_camera_device *icd = file->private_data;
764         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
765         int ret;
766
767         ret = ici->ops->get_crop(icd, a);
768
769         return ret;
770 }
771
772 /*
773  * According to the V4L2 API, drivers shall not update the struct v4l2_crop
774  * argument with the actual geometry, instead, the user shall use G_CROP to
775  * retrieve it.
776  */
777 static int soc_camera_s_crop(struct file *file, void *fh,
778                              struct v4l2_crop *a)
779 {
780         struct soc_camera_device *icd = file->private_data;
781         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
782         struct v4l2_rect *rect = &a->c;
783         struct v4l2_crop current_crop;
784         int ret;
785
786         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
787                 return -EINVAL;
788
789         dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
790                 rect->width, rect->height, rect->left, rect->top);
791
792         /* If get_crop fails, we'll let host and / or client drivers decide */
793         ret = ici->ops->get_crop(icd, &current_crop);
794
795         /* Prohibit window size change with initialised buffers */
796         if (ret < 0) {
797                 dev_err(&icd->dev,
798                         "S_CROP denied: getting current crop failed\n");
799         } else if (icd->vb_vidq.bufs[0] &&
800                    (a->c.width != current_crop.c.width ||
801                     a->c.height != current_crop.c.height)) {
802                 dev_err(&icd->dev,
803                         "S_CROP denied: queue initialised and sizes differ\n");
804                 ret = -EBUSY;
805         } else {
806                 ret = ici->ops->set_crop(icd, a);
807         }
808
809         return ret;
810 }
811
812 static int soc_camera_g_parm(struct file *file, void *fh,
813                              struct v4l2_streamparm *a)
814 {
815         struct soc_camera_device *icd = file->private_data;
816         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
817
818         if (ici->ops->get_parm)
819                 return ici->ops->get_parm(icd, a);
820
821         return -ENOIOCTLCMD;
822 }
823
824 static int soc_camera_s_parm(struct file *file, void *fh,
825                              struct v4l2_streamparm *a)
826 {
827         struct soc_camera_device *icd = file->private_data;
828         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
829
830         if (ici->ops->set_parm)
831                 return ici->ops->set_parm(icd, a);
832
833         return -ENOIOCTLCMD;
834 }
835
836 static int soc_camera_g_chip_ident(struct file *file, void *fh,
837                                    struct v4l2_dbg_chip_ident *id)
838 {
839         struct soc_camera_device *icd = file->private_data;
840         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
841
842         return v4l2_subdev_call(sd, core, g_chip_ident, id);
843 }
844
845 #ifdef CONFIG_VIDEO_ADV_DEBUG
846 static int soc_camera_g_register(struct file *file, void *fh,
847                                  struct v4l2_dbg_register *reg)
848 {
849         struct soc_camera_device *icd = file->private_data;
850         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
851
852         return v4l2_subdev_call(sd, core, g_register, reg);
853 }
854
855 static int soc_camera_s_register(struct file *file, void *fh,
856                                  struct v4l2_dbg_register *reg)
857 {
858         struct soc_camera_device *icd = file->private_data;
859         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
860
861         return v4l2_subdev_call(sd, core, s_register, reg);
862 }
863 #endif
864
865 /* So far this function cannot fail */
866 static void scan_add_host(struct soc_camera_host *ici)
867 {
868         struct soc_camera_device *icd;
869
870         mutex_lock(&list_lock);
871
872         list_for_each_entry(icd, &devices, list) {
873                 if (icd->iface == ici->nr) {
874                         int ret;
875                         icd->dev.parent = ici->v4l2_dev.dev;
876                         dev_set_name(&icd->dev, "%u-%u", icd->iface,
877                                      icd->devnum);
878                         ret = device_register(&icd->dev);
879                         if (ret < 0) {
880                                 icd->dev.parent = NULL;
881                                 dev_err(&icd->dev,
882                                         "Cannot register device: %d\n", ret);
883                         }
884                 }
885         }
886
887         mutex_unlock(&list_lock);
888 }
889
890 #ifdef CONFIG_I2C_BOARDINFO
891 static int soc_camera_init_i2c(struct soc_camera_device *icd,
892                                struct soc_camera_link *icl)
893 {
894         struct i2c_client *client;
895         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
896         struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
897         struct v4l2_subdev *subdev;
898
899         if (!adap) {
900                 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
901                         icl->i2c_adapter_id);
902                 goto ei2cga;
903         }
904
905         icl->board_info->platform_data = icd;
906
907         subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
908                                 icl->board_info, NULL);
909         if (!subdev)
910                 goto ei2cnd;
911
912         client = v4l2_get_subdevdata(subdev);
913
914         /* Use to_i2c_client(dev) to recover the i2c client */
915         dev_set_drvdata(&icd->dev, &client->dev);
916
917         return 0;
918 ei2cnd:
919         i2c_put_adapter(adap);
920 ei2cga:
921         return -ENODEV;
922 }
923
924 static void soc_camera_free_i2c(struct soc_camera_device *icd)
925 {
926         struct i2c_client *client =
927                 to_i2c_client(to_soc_camera_control(icd));
928         dev_set_drvdata(&icd->dev, NULL);
929         v4l2_device_unregister_subdev(i2c_get_clientdata(client));
930         i2c_unregister_device(client);
931         i2c_put_adapter(client->adapter);
932 }
933 #else
934 #define soc_camera_init_i2c(icd, icl)   (-ENODEV)
935 #define soc_camera_free_i2c(icd)        do {} while (0)
936 #endif
937
938 static int soc_camera_video_start(struct soc_camera_device *icd);
939 static int video_dev_create(struct soc_camera_device *icd);
940 /* Called during host-driver probe */
941 static int soc_camera_probe(struct device *dev)
942 {
943         struct soc_camera_device *icd = to_soc_camera_dev(dev);
944         struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
945         struct soc_camera_link *icl = to_soc_camera_link(icd);
946         struct device *control = NULL;
947         struct v4l2_subdev *sd;
948         struct v4l2_mbus_framefmt mf;
949         int ret;
950
951         dev_info(dev, "Probing %s\n", dev_name(dev));
952
953         ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
954                                  icl->regulators);
955         if (ret < 0)
956                 goto ereg;
957
958         ret = soc_camera_power_set(icd, icl, 1);
959         if (ret < 0)
960                 goto epower;
961
962         /* The camera could have been already on, try to reset */
963         if (icl->reset)
964                 icl->reset(icd->pdev);
965
966         ret = ici->ops->add(icd);
967         if (ret < 0)
968                 goto eadd;
969
970         /* Must have icd->vdev before registering the device */
971         ret = video_dev_create(icd);
972         if (ret < 0)
973                 goto evdc;
974
975         /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
976         if (icl->board_info) {
977                 ret = soc_camera_init_i2c(icd, icl);
978                 if (ret < 0)
979                         goto eadddev;
980         } else if (!icl->add_device || !icl->del_device) {
981                 ret = -EINVAL;
982                 goto eadddev;
983         } else {
984                 if (icl->module_name)
985                         ret = request_module(icl->module_name);
986
987                 ret = icl->add_device(icl, &icd->dev);
988                 if (ret < 0)
989                         goto eadddev;
990
991                 /*
992                  * FIXME: this is racy, have to use driver-binding notification,
993                  * when it is available
994                  */
995                 control = to_soc_camera_control(icd);
996                 if (!control || !control->driver || !dev_get_drvdata(control) ||
997                     !try_module_get(control->driver->owner)) {
998                         icl->del_device(icl);
999                         goto enodrv;
1000                 }
1001         }
1002
1003         /* At this point client .probe() should have run already */
1004         ret = soc_camera_init_user_formats(icd);
1005         if (ret < 0)
1006                 goto eiufmt;
1007
1008         icd->field = V4L2_FIELD_ANY;
1009
1010         icd->vdev->lock = &icd->video_lock;
1011
1012         /*
1013          * ..._video_start() will create a device node, video_register_device()
1014          * itself is protected against concurrent open() calls, but we also have
1015          * to protect our data.
1016          */
1017         mutex_lock(&icd->video_lock);
1018
1019         ret = soc_camera_video_start(icd);
1020         if (ret < 0)
1021                 goto evidstart;
1022
1023         /* Try to improve our guess of a reasonable window format */
1024         sd = soc_camera_to_subdev(icd);
1025         if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1026                 icd->user_width         = mf.width;
1027                 icd->user_height        = mf.height;
1028                 icd->colorspace         = mf.colorspace;
1029                 icd->field              = mf.field;
1030         }
1031
1032         /* Do we have to sysfs_remove_link() before device_unregister()? */
1033         if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
1034                               "control"))
1035                 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1036
1037         ici->ops->remove(icd);
1038
1039         soc_camera_power_set(icd, icl, 0);
1040
1041         mutex_unlock(&icd->video_lock);
1042
1043         return 0;
1044
1045 evidstart:
1046         mutex_unlock(&icd->video_lock);
1047         soc_camera_free_user_formats(icd);
1048 eiufmt:
1049         if (icl->board_info) {
1050                 soc_camera_free_i2c(icd);
1051         } else {
1052                 icl->del_device(icl);
1053                 module_put(control->driver->owner);
1054         }
1055 enodrv:
1056 eadddev:
1057         video_device_release(icd->vdev);
1058 evdc:
1059         ici->ops->remove(icd);
1060 eadd:
1061         soc_camera_power_set(icd, icl, 0);
1062 epower:
1063         regulator_bulk_free(icl->num_regulators, icl->regulators);
1064 ereg:
1065         return ret;
1066 }
1067
1068 /*
1069  * This is called on device_unregister, which only means we have to disconnect
1070  * from the host, but not remove ourselves from the device list
1071  */
1072 static int soc_camera_remove(struct device *dev)
1073 {
1074         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1075         struct soc_camera_link *icl = to_soc_camera_link(icd);
1076         struct video_device *vdev = icd->vdev;
1077
1078         BUG_ON(!dev->parent);
1079
1080         if (vdev) {
1081                 video_unregister_device(vdev);
1082                 icd->vdev = NULL;
1083         }
1084
1085         if (icl->board_info) {
1086                 soc_camera_free_i2c(icd);
1087         } else {
1088                 struct device_driver *drv = to_soc_camera_control(icd) ?
1089                         to_soc_camera_control(icd)->driver : NULL;
1090                 if (drv) {
1091                         icl->del_device(icl);
1092                         module_put(drv->owner);
1093                 }
1094         }
1095         soc_camera_free_user_formats(icd);
1096
1097         regulator_bulk_free(icl->num_regulators, icl->regulators);
1098
1099         return 0;
1100 }
1101
1102 static int soc_camera_suspend(struct device *dev, pm_message_t state)
1103 {
1104         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1105         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1106         int ret = 0;
1107
1108         if (ici->ops->suspend)
1109                 ret = ici->ops->suspend(icd, state);
1110
1111         return ret;
1112 }
1113
1114 static int soc_camera_resume(struct device *dev)
1115 {
1116         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1117         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1118         int ret = 0;
1119
1120         if (ici->ops->resume)
1121                 ret = ici->ops->resume(icd);
1122
1123         return ret;
1124 }
1125
1126 struct bus_type soc_camera_bus_type = {
1127         .name           = "soc-camera",
1128         .probe          = soc_camera_probe,
1129         .remove         = soc_camera_remove,
1130         .suspend        = soc_camera_suspend,
1131         .resume         = soc_camera_resume,
1132 };
1133 EXPORT_SYMBOL_GPL(soc_camera_bus_type);
1134
1135 static struct device_driver ic_drv = {
1136         .name   = "camera",
1137         .bus    = &soc_camera_bus_type,
1138         .owner  = THIS_MODULE,
1139 };
1140
1141 static void dummy_release(struct device *dev)
1142 {
1143 }
1144
1145 static int default_cropcap(struct soc_camera_device *icd,
1146                            struct v4l2_cropcap *a)
1147 {
1148         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1149         return v4l2_subdev_call(sd, video, cropcap, a);
1150 }
1151
1152 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1153 {
1154         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1155         return v4l2_subdev_call(sd, video, g_crop, a);
1156 }
1157
1158 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1159 {
1160         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1161         return v4l2_subdev_call(sd, video, s_crop, a);
1162 }
1163
1164 static int default_g_parm(struct soc_camera_device *icd,
1165                           struct v4l2_streamparm *parm)
1166 {
1167         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1168         return v4l2_subdev_call(sd, video, g_parm, parm);
1169 }
1170
1171 static int default_s_parm(struct soc_camera_device *icd,
1172                           struct v4l2_streamparm *parm)
1173 {
1174         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1175         return v4l2_subdev_call(sd, video, s_parm, parm);
1176 }
1177
1178 static void soc_camera_device_init(struct device *dev, void *pdata)
1179 {
1180         dev->platform_data      = pdata;
1181         dev->bus                = &soc_camera_bus_type;
1182         dev->release            = dummy_release;
1183 }
1184
1185 int soc_camera_host_register(struct soc_camera_host *ici)
1186 {
1187         struct soc_camera_host *ix;
1188         int ret;
1189
1190         if (!ici || !ici->ops ||
1191             !ici->ops->try_fmt ||
1192             !ici->ops->set_fmt ||
1193             !ici->ops->set_bus_param ||
1194             !ici->ops->querycap ||
1195             !ici->ops->init_videobuf ||
1196             !ici->ops->reqbufs ||
1197             !ici->ops->add ||
1198             !ici->ops->remove ||
1199             !ici->ops->poll ||
1200             !ici->v4l2_dev.dev)
1201                 return -EINVAL;
1202
1203         if (!ici->ops->set_crop)
1204                 ici->ops->set_crop = default_s_crop;
1205         if (!ici->ops->get_crop)
1206                 ici->ops->get_crop = default_g_crop;
1207         if (!ici->ops->cropcap)
1208                 ici->ops->cropcap = default_cropcap;
1209         if (!ici->ops->set_parm)
1210                 ici->ops->set_parm = default_s_parm;
1211         if (!ici->ops->get_parm)
1212                 ici->ops->get_parm = default_g_parm;
1213
1214         mutex_lock(&list_lock);
1215         list_for_each_entry(ix, &hosts, list) {
1216                 if (ix->nr == ici->nr) {
1217                         ret = -EBUSY;
1218                         goto edevreg;
1219                 }
1220         }
1221
1222         ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1223         if (ret < 0)
1224                 goto edevreg;
1225
1226         list_add_tail(&ici->list, &hosts);
1227         mutex_unlock(&list_lock);
1228
1229         scan_add_host(ici);
1230
1231         return 0;
1232
1233 edevreg:
1234         mutex_unlock(&list_lock);
1235         return ret;
1236 }
1237 EXPORT_SYMBOL(soc_camera_host_register);
1238
1239 /* Unregister all clients! */
1240 void soc_camera_host_unregister(struct soc_camera_host *ici)
1241 {
1242         struct soc_camera_device *icd;
1243
1244         mutex_lock(&list_lock);
1245
1246         list_del(&ici->list);
1247
1248         list_for_each_entry(icd, &devices, list) {
1249                 if (icd->iface == ici->nr) {
1250                         void *pdata = icd->dev.platform_data;
1251                         /* The bus->remove will be called */
1252                         device_unregister(&icd->dev);
1253                         /*
1254                          * Not before device_unregister(), .remove
1255                          * needs parent to call ici->ops->remove().
1256                          * If the host module is loaded again, device_register()
1257                          * would complain "already initialised," since 2.6.32
1258                          * this is also needed to prevent use-after-free of the
1259                          * device private data.
1260                          */
1261                         memset(&icd->dev, 0, sizeof(icd->dev));
1262                         soc_camera_device_init(&icd->dev, pdata);
1263                 }
1264         }
1265
1266         mutex_unlock(&list_lock);
1267
1268         v4l2_device_unregister(&ici->v4l2_dev);
1269 }
1270 EXPORT_SYMBOL(soc_camera_host_unregister);
1271
1272 /* Image capture device */
1273 static int soc_camera_device_register(struct soc_camera_device *icd)
1274 {
1275         struct soc_camera_device *ix;
1276         int num = -1, i;
1277
1278         for (i = 0; i < 256 && num < 0; i++) {
1279                 num = i;
1280                 /* Check if this index is available on this interface */
1281                 list_for_each_entry(ix, &devices, list) {
1282                         if (ix->iface == icd->iface && ix->devnum == i) {
1283                                 num = -1;
1284                                 break;
1285                         }
1286                 }
1287         }
1288
1289         if (num < 0)
1290                 /*
1291                  * ok, we have 256 cameras on this host...
1292                  * man, stay reasonable...
1293                  */
1294                 return -ENOMEM;
1295
1296         icd->devnum             = num;
1297         icd->use_count          = 0;
1298         icd->host_priv          = NULL;
1299         mutex_init(&icd->video_lock);
1300
1301         list_add_tail(&icd->list, &devices);
1302
1303         return 0;
1304 }
1305
1306 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1307 {
1308         list_del(&icd->list);
1309 }
1310
1311 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1312         .vidioc_querycap         = soc_camera_querycap,
1313         .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
1314         .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1315         .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1316         .vidioc_enum_input       = soc_camera_enum_input,
1317         .vidioc_g_input          = soc_camera_g_input,
1318         .vidioc_s_input          = soc_camera_s_input,
1319         .vidioc_s_std            = soc_camera_s_std,
1320         .vidioc_reqbufs          = soc_camera_reqbufs,
1321         .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1322         .vidioc_querybuf         = soc_camera_querybuf,
1323         .vidioc_qbuf             = soc_camera_qbuf,
1324         .vidioc_dqbuf            = soc_camera_dqbuf,
1325         .vidioc_streamon         = soc_camera_streamon,
1326         .vidioc_streamoff        = soc_camera_streamoff,
1327         .vidioc_queryctrl        = soc_camera_queryctrl,
1328         .vidioc_g_ctrl           = soc_camera_g_ctrl,
1329         .vidioc_s_ctrl           = soc_camera_s_ctrl,
1330         .vidioc_cropcap          = soc_camera_cropcap,
1331         .vidioc_g_crop           = soc_camera_g_crop,
1332         .vidioc_s_crop           = soc_camera_s_crop,
1333         .vidioc_g_parm           = soc_camera_g_parm,
1334         .vidioc_s_parm           = soc_camera_s_parm,
1335         .vidioc_g_chip_ident     = soc_camera_g_chip_ident,
1336 #ifdef CONFIG_VIDEO_ADV_DEBUG
1337         .vidioc_g_register       = soc_camera_g_register,
1338         .vidioc_s_register       = soc_camera_s_register,
1339 #endif
1340 };
1341
1342 static int video_dev_create(struct soc_camera_device *icd)
1343 {
1344         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1345         struct video_device *vdev = video_device_alloc();
1346
1347         if (!vdev)
1348                 return -ENOMEM;
1349
1350         strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1351
1352         vdev->parent            = &icd->dev;
1353         vdev->current_norm      = V4L2_STD_UNKNOWN;
1354         vdev->fops              = &soc_camera_fops;
1355         vdev->ioctl_ops         = &soc_camera_ioctl_ops;
1356         vdev->release           = video_device_release;
1357         vdev->tvnorms           = V4L2_STD_UNKNOWN;
1358
1359         icd->vdev = vdev;
1360
1361         return 0;
1362 }
1363
1364 /*
1365  * Called from soc_camera_probe() above (with .video_lock held???)
1366  */
1367 static int soc_camera_video_start(struct soc_camera_device *icd)
1368 {
1369         struct device_type *type = icd->vdev->dev.type;
1370         int ret;
1371
1372         if (!icd->dev.parent)
1373                 return -ENODEV;
1374
1375         if (!icd->ops ||
1376             !icd->ops->query_bus_param ||
1377             !icd->ops->set_bus_param)
1378                 return -EINVAL;
1379
1380         ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1381         if (ret < 0) {
1382                 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1383                 return ret;
1384         }
1385
1386         /* Restore device type, possibly set by the subdevice driver */
1387         icd->vdev->dev.type = type;
1388
1389         return 0;
1390 }
1391
1392 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1393 {
1394         struct soc_camera_link *icl = pdev->dev.platform_data;
1395         struct soc_camera_device *icd;
1396         int ret;
1397
1398         if (!icl)
1399                 return -EINVAL;
1400
1401         icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1402         if (!icd)
1403                 return -ENOMEM;
1404
1405         icd->iface = icl->bus_id;
1406         icd->pdev = &pdev->dev;
1407         platform_set_drvdata(pdev, icd);
1408
1409         ret = soc_camera_device_register(icd);
1410         if (ret < 0)
1411                 goto escdevreg;
1412
1413         soc_camera_device_init(&icd->dev, icl);
1414
1415         icd->user_width         = DEFAULT_WIDTH;
1416         icd->user_height        = DEFAULT_HEIGHT;
1417
1418         return 0;
1419
1420 escdevreg:
1421         kfree(icd);
1422
1423         return ret;
1424 }
1425
1426 /*
1427  * Only called on rmmod for each platform device, since they are not
1428  * hot-pluggable. Now we know, that all our users - hosts and devices have
1429  * been unloaded already
1430  */
1431 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1432 {
1433         struct soc_camera_device *icd = platform_get_drvdata(pdev);
1434
1435         if (!icd)
1436                 return -EINVAL;
1437
1438         soc_camera_device_unregister(icd);
1439
1440         kfree(icd);
1441
1442         return 0;
1443 }
1444
1445 static struct platform_driver __refdata soc_camera_pdrv = {
1446         .remove  = __devexit_p(soc_camera_pdrv_remove),
1447         .driver  = {
1448                 .name   = "soc-camera-pdrv",
1449                 .owner  = THIS_MODULE,
1450         },
1451 };
1452
1453 static int __init soc_camera_init(void)
1454 {
1455         int ret = bus_register(&soc_camera_bus_type);
1456         if (ret)
1457                 return ret;
1458         ret = driver_register(&ic_drv);
1459         if (ret)
1460                 goto edrvr;
1461
1462         ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1463         if (ret)
1464                 goto epdr;
1465
1466         return 0;
1467
1468 epdr:
1469         driver_unregister(&ic_drv);
1470 edrvr:
1471         bus_unregister(&soc_camera_bus_type);
1472         return ret;
1473 }
1474
1475 static void __exit soc_camera_exit(void)
1476 {
1477         platform_driver_unregister(&soc_camera_pdrv);
1478         driver_unregister(&ic_drv);
1479         bus_unregister(&soc_camera_bus_type);
1480 }
1481
1482 module_init(soc_camera_init);
1483 module_exit(soc_camera_exit);
1484
1485 MODULE_DESCRIPTION("Image capture bus driver");
1486 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1487 MODULE_LICENSE("GPL");
1488 MODULE_ALIAS("platform:soc-camera-pdrv");