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