V4L/DVB (12506): soc-camera: convert to platform device
[safe/jmp/linux-2.6] / 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/vmalloc.h>
28
29 #include <media/soc_camera.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-dev.h>
33 #include <media/videobuf-core.h>
34
35 /* Default to VGA resolution */
36 #define DEFAULT_WIDTH   640
37 #define DEFAULT_HEIGHT  480
38
39 static LIST_HEAD(hosts);
40 static LIST_HEAD(devices);
41 static DEFINE_MUTEX(list_lock);         /* Protects the list of hosts */
42
43 const struct soc_camera_data_format *soc_camera_format_by_fourcc(
44         struct soc_camera_device *icd, unsigned int fourcc)
45 {
46         unsigned int i;
47
48         for (i = 0; i < icd->num_formats; i++)
49                 if (icd->formats[i].fourcc == fourcc)
50                         return icd->formats + i;
51         return NULL;
52 }
53 EXPORT_SYMBOL(soc_camera_format_by_fourcc);
54
55 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
56         struct soc_camera_device *icd, unsigned int fourcc)
57 {
58         unsigned int i;
59
60         for (i = 0; i < icd->num_user_formats; i++)
61                 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
62                         return icd->user_formats + i;
63         return NULL;
64 }
65 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
66
67 /**
68  * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
69  * @icl:        camera platform parameters
70  * @flags:      flags to be inverted according to platform configuration
71  * @return:     resulting flags
72  */
73 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
74                                             unsigned long flags)
75 {
76         unsigned long f;
77
78         /* If only one of the two polarities is supported, switch to the opposite */
79         if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
80                 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
81                 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
82                         flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
83         }
84
85         if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
86                 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
87                 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
88                         flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
89         }
90
91         if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
92                 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
93                 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
94                         flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
95         }
96
97         return flags;
98 }
99 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
100
101 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
102                                       struct v4l2_format *f)
103 {
104         struct soc_camera_file *icf = file->private_data;
105         struct soc_camera_device *icd = icf->icd;
106         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
107
108         WARN_ON(priv != file->private_data);
109
110         /* limit format to hardware capabilities */
111         return ici->ops->try_fmt(icd, f);
112 }
113
114 static int soc_camera_enum_input(struct file *file, void *priv,
115                                  struct v4l2_input *inp)
116 {
117         struct soc_camera_file *icf = file->private_data;
118         struct soc_camera_device *icd = icf->icd;
119         int ret = 0;
120
121         if (inp->index != 0)
122                 return -EINVAL;
123
124         if (icd->ops->enum_input)
125                 ret = icd->ops->enum_input(icd, inp);
126         else {
127                 /* default is camera */
128                 inp->type = V4L2_INPUT_TYPE_CAMERA;
129                 inp->std  = V4L2_STD_UNKNOWN;
130                 strcpy(inp->name, "Camera");
131         }
132
133         return ret;
134 }
135
136 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
137 {
138         *i = 0;
139
140         return 0;
141 }
142
143 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
144 {
145         if (i > 0)
146                 return -EINVAL;
147
148         return 0;
149 }
150
151 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
152 {
153         struct soc_camera_file *icf = file->private_data;
154         struct soc_camera_device *icd = icf->icd;
155         int ret = 0;
156
157         if (icd->ops->set_std)
158                 ret = icd->ops->set_std(icd, a);
159
160         return ret;
161 }
162
163 static int soc_camera_reqbufs(struct file *file, void *priv,
164                               struct v4l2_requestbuffers *p)
165 {
166         int ret;
167         struct soc_camera_file *icf = file->private_data;
168         struct soc_camera_device *icd = icf->icd;
169         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
170
171         WARN_ON(priv != file->private_data);
172
173         dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
174
175         ret = videobuf_reqbufs(&icf->vb_vidq, p);
176         if (ret < 0)
177                 return ret;
178
179         return ici->ops->reqbufs(icf, p);
180 }
181
182 static int soc_camera_querybuf(struct file *file, void *priv,
183                                struct v4l2_buffer *p)
184 {
185         struct soc_camera_file *icf = file->private_data;
186
187         WARN_ON(priv != file->private_data);
188
189         return videobuf_querybuf(&icf->vb_vidq, p);
190 }
191
192 static int soc_camera_qbuf(struct file *file, void *priv,
193                            struct v4l2_buffer *p)
194 {
195         struct soc_camera_file *icf = file->private_data;
196
197         WARN_ON(priv != file->private_data);
198
199         return videobuf_qbuf(&icf->vb_vidq, p);
200 }
201
202 static int soc_camera_dqbuf(struct file *file, void *priv,
203                             struct v4l2_buffer *p)
204 {
205         struct soc_camera_file *icf = file->private_data;
206
207         WARN_ON(priv != file->private_data);
208
209         return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
210 }
211
212 /* Always entered with .video_lock held */
213 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
214 {
215         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
216         int i, fmts = 0;
217
218         if (!ici->ops->get_formats)
219                 /*
220                  * Fallback mode - the host will have to serve all
221                  * sensor-provided formats one-to-one to the user
222                  */
223                 fmts = icd->num_formats;
224         else
225                 /*
226                  * First pass - only count formats this host-sensor
227                  * configuration can provide
228                  */
229                 for (i = 0; i < icd->num_formats; i++)
230                         fmts += ici->ops->get_formats(icd, i, NULL);
231
232         if (!fmts)
233                 return -ENXIO;
234
235         icd->user_formats =
236                 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
237         if (!icd->user_formats)
238                 return -ENOMEM;
239
240         icd->num_user_formats = fmts;
241
242         dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
243
244         /* Second pass - actually fill data formats */
245         fmts = 0;
246         for (i = 0; i < icd->num_formats; i++)
247                 if (!ici->ops->get_formats) {
248                         icd->user_formats[i].host_fmt = icd->formats + i;
249                         icd->user_formats[i].cam_fmt = icd->formats + i;
250                         icd->user_formats[i].buswidth = icd->formats[i].depth;
251                 } else {
252                         fmts += ici->ops->get_formats(icd, i,
253                                                       &icd->user_formats[fmts]);
254                 }
255
256         icd->current_fmt = icd->user_formats[0].host_fmt;
257
258         return 0;
259 }
260
261 /* Always entered with .video_lock held */
262 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
263 {
264         icd->current_fmt = NULL;
265         vfree(icd->user_formats);
266         icd->user_formats = NULL;
267 }
268
269 /* Called with .vb_lock held */
270 static int soc_camera_set_fmt(struct soc_camera_file *icf,
271                               struct v4l2_format *f)
272 {
273         struct soc_camera_device *icd = icf->icd;
274         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
275         struct v4l2_pix_format *pix = &f->fmt.pix;
276         int ret;
277
278         /* We always call try_fmt() before set_fmt() or set_crop() */
279         ret = ici->ops->try_fmt(icd, f);
280         if (ret < 0)
281                 return ret;
282
283         ret = ici->ops->set_fmt(icd, f);
284         if (ret < 0) {
285                 return ret;
286         } else if (!icd->current_fmt ||
287                    icd->current_fmt->fourcc != pix->pixelformat) {
288                 dev_err(ici->dev,
289                         "Host driver hasn't set up current format correctly!\n");
290                 return -EINVAL;
291         }
292
293         icd->width              = pix->width;
294         icd->height             = pix->height;
295         icf->vb_vidq.field      =
296                 icd->field      = pix->field;
297
298         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
299                 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
300                          f->type);
301
302         dev_dbg(&icd->dev, "set width: %d height: %d\n",
303                 icd->width, icd->height);
304
305         /* set physical bus parameters */
306         return ici->ops->set_bus_param(icd, pix->pixelformat);
307 }
308
309 static int soc_camera_open(struct file *file)
310 {
311         struct video_device *vdev;
312         struct soc_camera_device *icd;
313         struct soc_camera_host *ici;
314         struct soc_camera_file *icf;
315         int ret;
316
317         /*
318          * It is safe to dereference these pointers now as long as a user has
319          * the video device open - we are protected by the held cdev reference.
320          */
321
322         vdev = video_devdata(file);
323         icd = container_of(vdev->parent, struct soc_camera_device, dev);
324
325         if (!icd->ops)
326                 /* No device driver attached */
327                 return -ENODEV;
328
329         ici = to_soc_camera_host(icd->dev.parent);
330
331         icf = vmalloc(sizeof(*icf));
332         if (!icf)
333                 return -ENOMEM;
334
335         if (!try_module_get(icd->ops->owner)) {
336                 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
337                 ret = -EINVAL;
338                 goto emgd;
339         }
340
341         if (!try_module_get(ici->ops->owner)) {
342                 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
343                 ret = -EINVAL;
344                 goto emgi;
345         }
346
347         /* Protect against icd->ops->remove() until we module_get() both drivers. */
348         mutex_lock(&icd->video_lock);
349
350         icf->icd = icd;
351         icd->use_count++;
352
353         /* Now we really have to activate the camera */
354         if (icd->use_count == 1) {
355                 /* Restore parameters before the last close() per V4L2 API */
356                 struct v4l2_format f = {
357                         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
358                         .fmt.pix = {
359                                 .width          = icd->width,
360                                 .height         = icd->height,
361                                 .field          = icd->field,
362                         },
363                 };
364
365                 ret = soc_camera_init_user_formats(icd);
366                 if (ret < 0)
367                         goto eiufmt;
368
369                 dev_dbg(&icd->dev, "Using fmt %x\n", icd->current_fmt->fourcc);
370
371                 f.fmt.pix.pixelformat   = icd->current_fmt->fourcc;
372                 f.fmt.pix.colorspace    = icd->current_fmt->colorspace;
373
374                 ret = ici->ops->add(icd);
375                 if (ret < 0) {
376                         dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
377                         goto eiciadd;
378                 }
379
380                 /* Try to configure with default parameters */
381                 ret = soc_camera_set_fmt(icf, &f);
382                 if (ret < 0)
383                         goto esfmt;
384         }
385
386         mutex_unlock(&icd->video_lock);
387
388         file->private_data = icf;
389         dev_dbg(&icd->dev, "camera device open\n");
390
391         ici->ops->init_videobuf(&icf->vb_vidq, icd);
392
393         return 0;
394
395         /*
396          * First three errors are entered with the .video_lock held
397          * and use_count == 1
398          */
399 esfmt:
400         ici->ops->remove(icd);
401 eiciadd:
402         soc_camera_free_user_formats(icd);
403 eiufmt:
404         icd->use_count--;
405         mutex_unlock(&icd->video_lock);
406         module_put(ici->ops->owner);
407 emgi:
408         module_put(icd->ops->owner);
409 emgd:
410         vfree(icf);
411         return ret;
412 }
413
414 static int soc_camera_close(struct file *file)
415 {
416         struct soc_camera_file *icf = file->private_data;
417         struct soc_camera_device *icd = icf->icd;
418         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
419         struct video_device *vdev = icd->vdev;
420
421         mutex_lock(&icd->video_lock);
422         icd->use_count--;
423         if (!icd->use_count) {
424                 ici->ops->remove(icd);
425                 soc_camera_free_user_formats(icd);
426         }
427
428         mutex_unlock(&icd->video_lock);
429
430         module_put(icd->ops->owner);
431         module_put(ici->ops->owner);
432
433         vfree(icf);
434
435         dev_dbg(vdev->parent, "camera device close\n");
436
437         return 0;
438 }
439
440 static ssize_t soc_camera_read(struct file *file, char __user *buf,
441                                size_t count, loff_t *ppos)
442 {
443         struct soc_camera_file *icf = file->private_data;
444         struct soc_camera_device *icd = icf->icd;
445         struct video_device *vdev = icd->vdev;
446         int err = -EINVAL;
447
448         dev_err(vdev->parent, "camera device read not implemented\n");
449
450         return err;
451 }
452
453 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
454 {
455         struct soc_camera_file *icf = file->private_data;
456         struct soc_camera_device *icd = icf->icd;
457         int err;
458
459         dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
460
461         err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
462
463         dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
464                 (unsigned long)vma->vm_start,
465                 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
466                 err);
467
468         return err;
469 }
470
471 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
472 {
473         struct soc_camera_file *icf = file->private_data;
474         struct soc_camera_device *icd = icf->icd;
475         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
476
477         if (list_empty(&icf->vb_vidq.stream)) {
478                 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
479                 return POLLERR;
480         }
481
482         return ici->ops->poll(file, pt);
483 }
484
485 static struct v4l2_file_operations soc_camera_fops = {
486         .owner          = THIS_MODULE,
487         .open           = soc_camera_open,
488         .release        = soc_camera_close,
489         .ioctl          = video_ioctl2,
490         .read           = soc_camera_read,
491         .mmap           = soc_camera_mmap,
492         .poll           = soc_camera_poll,
493 };
494
495 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
496                                     struct v4l2_format *f)
497 {
498         struct soc_camera_file *icf = file->private_data;
499         struct soc_camera_device *icd = icf->icd;
500         int ret;
501
502         WARN_ON(priv != file->private_data);
503
504         mutex_lock(&icf->vb_vidq.vb_lock);
505
506         if (videobuf_queue_is_busy(&icf->vb_vidq)) {
507                 dev_err(&icd->dev, "S_FMT denied: queue busy\n");
508                 ret = -EBUSY;
509                 goto unlock;
510         }
511
512         ret = soc_camera_set_fmt(icf, f);
513
514 unlock:
515         mutex_unlock(&icf->vb_vidq.vb_lock);
516
517         return ret;
518 }
519
520 static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
521                                        struct v4l2_fmtdesc *f)
522 {
523         struct soc_camera_file *icf = file->private_data;
524         struct soc_camera_device *icd = icf->icd;
525         const struct soc_camera_data_format *format;
526
527         WARN_ON(priv != file->private_data);
528
529         if (f->index >= icd->num_user_formats)
530                 return -EINVAL;
531
532         format = icd->user_formats[f->index].host_fmt;
533
534         strlcpy(f->description, format->name, sizeof(f->description));
535         f->pixelformat = format->fourcc;
536         return 0;
537 }
538
539 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
540                                     struct v4l2_format *f)
541 {
542         struct soc_camera_file *icf = file->private_data;
543         struct soc_camera_device *icd = icf->icd;
544         struct v4l2_pix_format *pix = &f->fmt.pix;
545
546         WARN_ON(priv != file->private_data);
547
548         pix->width              = icd->width;
549         pix->height             = icd->height;
550         pix->field              = icf->vb_vidq.field;
551         pix->pixelformat        = icd->current_fmt->fourcc;
552         pix->bytesperline       = pix->width *
553                 DIV_ROUND_UP(icd->current_fmt->depth, 8);
554         pix->sizeimage          = pix->height * pix->bytesperline;
555         dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
556                 icd->current_fmt->fourcc);
557         return 0;
558 }
559
560 static int soc_camera_querycap(struct file *file, void  *priv,
561                                struct v4l2_capability *cap)
562 {
563         struct soc_camera_file *icf = file->private_data;
564         struct soc_camera_device *icd = icf->icd;
565         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
566
567         WARN_ON(priv != file->private_data);
568
569         strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
570         return ici->ops->querycap(ici, cap);
571 }
572
573 static int soc_camera_streamon(struct file *file, void *priv,
574                                enum v4l2_buf_type i)
575 {
576         struct soc_camera_file *icf = file->private_data;
577         struct soc_camera_device *icd = icf->icd;
578         int ret;
579
580         WARN_ON(priv != file->private_data);
581
582         dev_dbg(&icd->dev, "%s\n", __func__);
583
584         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
585                 return -EINVAL;
586
587         mutex_lock(&icd->video_lock);
588
589         icd->ops->start_capture(icd);
590
591         /* This calls buf_queue from host driver's videobuf_queue_ops */
592         ret = videobuf_streamon(&icf->vb_vidq);
593
594         mutex_unlock(&icd->video_lock);
595
596         return ret;
597 }
598
599 static int soc_camera_streamoff(struct file *file, void *priv,
600                                 enum v4l2_buf_type i)
601 {
602         struct soc_camera_file *icf = file->private_data;
603         struct soc_camera_device *icd = icf->icd;
604
605         WARN_ON(priv != file->private_data);
606
607         dev_dbg(&icd->dev, "%s\n", __func__);
608
609         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
610                 return -EINVAL;
611
612         mutex_lock(&icd->video_lock);
613
614         /* This calls buf_release from host driver's videobuf_queue_ops for all
615          * remaining buffers. When the last buffer is freed, stop capture */
616         videobuf_streamoff(&icf->vb_vidq);
617
618         icd->ops->stop_capture(icd);
619
620         mutex_unlock(&icd->video_lock);
621
622         return 0;
623 }
624
625 static int soc_camera_queryctrl(struct file *file, void *priv,
626                                 struct v4l2_queryctrl *qc)
627 {
628         struct soc_camera_file *icf = file->private_data;
629         struct soc_camera_device *icd = icf->icd;
630         int i;
631
632         WARN_ON(priv != file->private_data);
633
634         if (!qc->id)
635                 return -EINVAL;
636
637         for (i = 0; i < icd->ops->num_controls; i++)
638                 if (qc->id == icd->ops->controls[i].id) {
639                         memcpy(qc, &(icd->ops->controls[i]),
640                                 sizeof(*qc));
641                         return 0;
642                 }
643
644         return -EINVAL;
645 }
646
647 static int soc_camera_g_ctrl(struct file *file, void *priv,
648                              struct v4l2_control *ctrl)
649 {
650         struct soc_camera_file *icf = file->private_data;
651         struct soc_camera_device *icd = icf->icd;
652
653         WARN_ON(priv != file->private_data);
654
655         switch (ctrl->id) {
656         case V4L2_CID_GAIN:
657                 if (icd->gain == (unsigned short)~0)
658                         return -EINVAL;
659                 ctrl->value = icd->gain;
660                 return 0;
661         case V4L2_CID_EXPOSURE:
662                 if (icd->exposure == (unsigned short)~0)
663                         return -EINVAL;
664                 ctrl->value = icd->exposure;
665                 return 0;
666         }
667
668         if (icd->ops->get_control)
669                 return icd->ops->get_control(icd, ctrl);
670         return -EINVAL;
671 }
672
673 static int soc_camera_s_ctrl(struct file *file, void *priv,
674                              struct v4l2_control *ctrl)
675 {
676         struct soc_camera_file *icf = file->private_data;
677         struct soc_camera_device *icd = icf->icd;
678
679         WARN_ON(priv != file->private_data);
680
681         if (icd->ops->set_control)
682                 return icd->ops->set_control(icd, ctrl);
683         return -EINVAL;
684 }
685
686 static int soc_camera_cropcap(struct file *file, void *fh,
687                               struct v4l2_cropcap *a)
688 {
689         struct soc_camera_file *icf = file->private_data;
690         struct soc_camera_device *icd = icf->icd;
691
692         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
693         a->bounds.left                  = icd->x_min;
694         a->bounds.top                   = icd->y_min;
695         a->bounds.width                 = icd->width_max;
696         a->bounds.height                = icd->height_max;
697         a->defrect.left                 = icd->x_min;
698         a->defrect.top                  = icd->y_min;
699         a->defrect.width                = DEFAULT_WIDTH;
700         a->defrect.height               = DEFAULT_HEIGHT;
701         a->pixelaspect.numerator        = 1;
702         a->pixelaspect.denominator      = 1;
703
704         return 0;
705 }
706
707 static int soc_camera_g_crop(struct file *file, void *fh,
708                              struct v4l2_crop *a)
709 {
710         struct soc_camera_file *icf = file->private_data;
711         struct soc_camera_device *icd = icf->icd;
712
713         a->type         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
714         a->c.left       = icd->x_current;
715         a->c.top        = icd->y_current;
716         a->c.width      = icd->width;
717         a->c.height     = icd->height;
718
719         return 0;
720 }
721
722 static int soc_camera_s_crop(struct file *file, void *fh,
723                              struct v4l2_crop *a)
724 {
725         struct soc_camera_file *icf = file->private_data;
726         struct soc_camera_device *icd = icf->icd;
727         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
728         int ret;
729
730         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
731                 return -EINVAL;
732
733         /* Cropping is allowed during a running capture, guard consistency */
734         mutex_lock(&icf->vb_vidq.vb_lock);
735
736         ret = ici->ops->set_crop(icd, &a->c);
737         if (!ret) {
738                 icd->width      = a->c.width;
739                 icd->height     = a->c.height;
740                 icd->x_current  = a->c.left;
741                 icd->y_current  = a->c.top;
742         }
743
744         mutex_unlock(&icf->vb_vidq.vb_lock);
745
746         return ret;
747 }
748
749 static int soc_camera_g_chip_ident(struct file *file, void *fh,
750                                    struct v4l2_dbg_chip_ident *id)
751 {
752         struct soc_camera_file *icf = file->private_data;
753         struct soc_camera_device *icd = icf->icd;
754
755         if (!icd->ops->get_chip_id)
756                 return -EINVAL;
757
758         return icd->ops->get_chip_id(icd, id);
759 }
760
761 #ifdef CONFIG_VIDEO_ADV_DEBUG
762 static int soc_camera_g_register(struct file *file, void *fh,
763                                  struct v4l2_dbg_register *reg)
764 {
765         struct soc_camera_file *icf = file->private_data;
766         struct soc_camera_device *icd = icf->icd;
767
768         if (!icd->ops->get_register)
769                 return -EINVAL;
770
771         return icd->ops->get_register(icd, reg);
772 }
773
774 static int soc_camera_s_register(struct file *file, void *fh,
775                                  struct v4l2_dbg_register *reg)
776 {
777         struct soc_camera_file *icf = file->private_data;
778         struct soc_camera_device *icd = icf->icd;
779
780         if (!icd->ops->set_register)
781                 return -EINVAL;
782
783         return icd->ops->set_register(icd, reg);
784 }
785 #endif
786
787 /* So far this function cannot fail */
788 static void scan_add_host(struct soc_camera_host *ici)
789 {
790         struct soc_camera_device *icd;
791
792         mutex_lock(&list_lock);
793
794         list_for_each_entry(icd, &devices, list) {
795                 if (icd->iface == ici->nr) {
796                         int ret;
797                         icd->dev.parent = ici->dev;
798                         dev_set_name(&icd->dev, "%u-%u", icd->iface,
799                                      icd->devnum);
800                         ret = device_register(&icd->dev);
801                         if (ret < 0) {
802                                 icd->dev.parent = NULL;
803                                 dev_err(&icd->dev,
804                                         "Cannot register device: %d\n", ret);
805                         }
806                 }
807         }
808
809         mutex_unlock(&list_lock);
810 }
811
812 #ifdef CONFIG_I2C_BOARDINFO
813 static int soc_camera_init_i2c(struct soc_camera_device *icd,
814                                struct soc_camera_link *icl)
815 {
816         struct i2c_client *client;
817         struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
818         int ret;
819
820         if (!adap) {
821                 ret = -ENODEV;
822                 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
823                         icl->i2c_adapter_id);
824                 goto ei2cga;
825         }
826
827         icl->board_info->platform_data = icd;
828
829         client = i2c_new_device(adap, icl->board_info);
830         if (!client) {
831                 ret = -ENOMEM;
832                 goto ei2cnd;
833         }
834
835         /*
836          * We set icd drvdata at two locations - here and in
837          * soc_camera_video_start(). Depending on the module loading /
838          * initialisation order one of these locations will be entered first
839          */
840         /* Use to_i2c_client(dev) to recover the i2c client */
841         dev_set_drvdata(&icd->dev, &client->dev);
842
843         return 0;
844 ei2cnd:
845         i2c_put_adapter(adap);
846 ei2cga:
847         return ret;
848 }
849
850 static void soc_camera_free_i2c(struct soc_camera_device *icd)
851 {
852         struct i2c_client *client =
853                 to_i2c_client(to_soc_camera_control(icd));
854         dev_set_drvdata(&icd->dev, NULL);
855         i2c_unregister_device(client);
856         i2c_put_adapter(client->adapter);
857 }
858 #else
859 #define soc_camera_init_i2c(icd, icl)   (-ENODEV)
860 #define soc_camera_free_i2c(icd)        do {} while (0)
861 #endif
862
863 static int video_dev_create(struct soc_camera_device *icd);
864 /* Called during host-driver probe */
865 static int soc_camera_probe(struct device *dev)
866 {
867         struct soc_camera_device *icd = to_soc_camera_dev(dev);
868         struct soc_camera_link *icl = to_soc_camera_link(icd);
869         int ret;
870
871         dev_info(dev, "Probing %s\n", dev_name(dev));
872
873         ret = video_dev_create(icd);
874         if (ret < 0)
875                 goto evdc;
876
877         /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
878         if (icl->board_info) {
879                 ret = soc_camera_init_i2c(icd, icl);
880                 if (ret < 0)
881                         goto eadddev;
882         } else if (!icl->add_device || !icl->del_device) {
883                 ret = -EINVAL;
884                 goto eadddev;
885         } else {
886                 ret = icl->add_device(icl, &icd->dev);
887                 if (ret < 0)
888                         goto eadddev;
889         }
890
891         ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, icd->vdev->minor);
892         if (ret < 0) {
893                 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
894                 goto evidregd;
895         }
896
897         /* Do we have to sysfs_remove_link() before device_unregister()? */
898         if (to_soc_camera_control(icd) &&
899             sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
900                               "control"))
901                 dev_warn(&icd->dev, "Failed creating the control symlink\n");
902
903
904         return 0;
905
906 evidregd:
907         if (icl->board_info)
908                 soc_camera_free_i2c(icd);
909         else
910                 icl->del_device(icl);
911 eadddev:
912         video_device_release(icd->vdev);
913 evdc:
914         return ret;
915 }
916
917 /* This is called on device_unregister, which only means we have to disconnect
918  * from the host, but not remove ourselves from the device list */
919 static int soc_camera_remove(struct device *dev)
920 {
921         struct soc_camera_device *icd = to_soc_camera_dev(dev);
922         struct soc_camera_link *icl = to_soc_camera_link(icd);
923         struct video_device *vdev = icd->vdev;
924
925         BUG_ON(!dev->parent);
926
927         if (vdev) {
928                 mutex_lock(&icd->video_lock);
929                 video_unregister_device(vdev);
930                 icd->vdev = NULL;
931                 mutex_unlock(&icd->video_lock);
932         }
933
934         if (icl->board_info)
935                 soc_camera_free_i2c(icd);
936         else
937                 icl->del_device(icl);
938
939         return 0;
940 }
941
942 static int soc_camera_suspend(struct device *dev, pm_message_t state)
943 {
944         struct soc_camera_device *icd = to_soc_camera_dev(dev);
945         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
946         int ret = 0;
947
948         if (ici->ops->suspend)
949                 ret = ici->ops->suspend(icd, state);
950
951         return ret;
952 }
953
954 static int soc_camera_resume(struct device *dev)
955 {
956         struct soc_camera_device *icd = to_soc_camera_dev(dev);
957         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
958         int ret = 0;
959
960         if (ici->ops->resume)
961                 ret = ici->ops->resume(icd);
962
963         return ret;
964 }
965
966 static struct bus_type soc_camera_bus_type = {
967         .name           = "soc-camera",
968         .probe          = soc_camera_probe,
969         .remove         = soc_camera_remove,
970         .suspend        = soc_camera_suspend,
971         .resume         = soc_camera_resume,
972 };
973
974 static struct device_driver ic_drv = {
975         .name   = "camera",
976         .bus    = &soc_camera_bus_type,
977         .owner  = THIS_MODULE,
978 };
979
980 static void dummy_release(struct device *dev)
981 {
982 }
983
984 int soc_camera_host_register(struct soc_camera_host *ici)
985 {
986         struct soc_camera_host *ix;
987
988         if (!ici || !ici->ops ||
989             !ici->ops->try_fmt ||
990             !ici->ops->set_fmt ||
991             !ici->ops->set_crop ||
992             !ici->ops->set_bus_param ||
993             !ici->ops->querycap ||
994             !ici->ops->init_videobuf ||
995             !ici->ops->reqbufs ||
996             !ici->ops->add ||
997             !ici->ops->remove ||
998             !ici->ops->poll ||
999             !ici->dev)
1000                 return -EINVAL;
1001
1002         mutex_lock(&list_lock);
1003         list_for_each_entry(ix, &hosts, list) {
1004                 if (ix->nr == ici->nr) {
1005                         mutex_unlock(&list_lock);
1006                         return -EBUSY;
1007                 }
1008         }
1009
1010         dev_set_drvdata(ici->dev, ici);
1011
1012         list_add_tail(&ici->list, &hosts);
1013         mutex_unlock(&list_lock);
1014
1015         scan_add_host(ici);
1016
1017         return 0;
1018 }
1019 EXPORT_SYMBOL(soc_camera_host_register);
1020
1021 /* Unregister all clients! */
1022 void soc_camera_host_unregister(struct soc_camera_host *ici)
1023 {
1024         struct soc_camera_device *icd;
1025
1026         mutex_lock(&list_lock);
1027
1028         list_del(&ici->list);
1029
1030         list_for_each_entry(icd, &devices, list) {
1031                 if (icd->dev.parent == ici->dev) {
1032                         /* The bus->remove will be called */
1033                         device_unregister(&icd->dev);
1034                         /* Not before device_unregister(), .remove
1035                          * needs parent to call ici->ops->remove() */
1036                         icd->dev.parent = NULL;
1037
1038                         /* If the host module is loaded again, device_register()
1039                          * would complain "already initialised" */
1040                         memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
1041                 }
1042         }
1043
1044         mutex_unlock(&list_lock);
1045
1046         dev_set_drvdata(ici->dev, NULL);
1047 }
1048 EXPORT_SYMBOL(soc_camera_host_unregister);
1049
1050 /* Image capture device */
1051 static int soc_camera_device_register(struct soc_camera_device *icd)
1052 {
1053         struct soc_camera_device *ix;
1054         int num = -1, i;
1055
1056         for (i = 0; i < 256 && num < 0; i++) {
1057                 num = i;
1058                 /* Check if this index is available on this interface */
1059                 list_for_each_entry(ix, &devices, list) {
1060                         if (ix->iface == icd->iface && ix->devnum == i) {
1061                                 num = -1;
1062                                 break;
1063                         }
1064                 }
1065         }
1066
1067         if (num < 0)
1068                 /* ok, we have 256 cameras on this host...
1069                  * man, stay reasonable... */
1070                 return -ENOMEM;
1071
1072         icd->devnum = num;
1073         icd->dev.bus = &soc_camera_bus_type;
1074
1075         icd->dev.release        = dummy_release;
1076         icd->use_count          = 0;
1077         icd->host_priv          = NULL;
1078         mutex_init(&icd->video_lock);
1079
1080         list_add_tail(&icd->list, &devices);
1081
1082         return 0;
1083 }
1084
1085 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1086 {
1087         list_del(&icd->list);
1088 }
1089
1090 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1091         .vidioc_querycap         = soc_camera_querycap,
1092         .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
1093         .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1094         .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1095         .vidioc_enum_input       = soc_camera_enum_input,
1096         .vidioc_g_input          = soc_camera_g_input,
1097         .vidioc_s_input          = soc_camera_s_input,
1098         .vidioc_s_std            = soc_camera_s_std,
1099         .vidioc_reqbufs          = soc_camera_reqbufs,
1100         .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1101         .vidioc_querybuf         = soc_camera_querybuf,
1102         .vidioc_qbuf             = soc_camera_qbuf,
1103         .vidioc_dqbuf            = soc_camera_dqbuf,
1104         .vidioc_streamon         = soc_camera_streamon,
1105         .vidioc_streamoff        = soc_camera_streamoff,
1106         .vidioc_queryctrl        = soc_camera_queryctrl,
1107         .vidioc_g_ctrl           = soc_camera_g_ctrl,
1108         .vidioc_s_ctrl           = soc_camera_s_ctrl,
1109         .vidioc_cropcap          = soc_camera_cropcap,
1110         .vidioc_g_crop           = soc_camera_g_crop,
1111         .vidioc_s_crop           = soc_camera_s_crop,
1112         .vidioc_g_chip_ident     = soc_camera_g_chip_ident,
1113 #ifdef CONFIG_VIDEO_ADV_DEBUG
1114         .vidioc_g_register       = soc_camera_g_register,
1115         .vidioc_s_register       = soc_camera_s_register,
1116 #endif
1117 };
1118
1119 static int video_dev_create(struct soc_camera_device *icd)
1120 {
1121         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1122         struct video_device *vdev = video_device_alloc();
1123
1124         if (!vdev)
1125                 return -ENOMEM;
1126         dev_dbg(ici->dev, "Allocated video_device %p\n", vdev);
1127
1128         strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1129
1130         vdev->parent            = &icd->dev;
1131         vdev->current_norm      = V4L2_STD_UNKNOWN;
1132         vdev->fops              = &soc_camera_fops;
1133         vdev->ioctl_ops         = &soc_camera_ioctl_ops;
1134         vdev->release           = video_device_release;
1135         vdev->minor             = -1;
1136         vdev->tvnorms           = V4L2_STD_UNKNOWN;
1137
1138         icd->vdev = vdev;
1139
1140         return 0;
1141 }
1142
1143 /*
1144  * Usually called from the struct soc_camera_ops .probe() method, i.e., from
1145  * soc_camera_probe() above with .video_lock held
1146  */
1147 int soc_camera_video_start(struct soc_camera_device *icd, struct device *dev)
1148 {
1149         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1150         const struct v4l2_queryctrl *qctrl;
1151
1152         if (!icd->dev.parent)
1153                 return -ENODEV;
1154
1155         if (!icd->ops ||
1156             !icd->ops->init ||
1157             !icd->ops->release ||
1158             !icd->ops->start_capture ||
1159             !icd->ops->stop_capture ||
1160             !icd->ops->set_fmt ||
1161             !icd->ops->try_fmt ||
1162             !icd->ops->query_bus_param ||
1163             !icd->ops->set_bus_param)
1164                 return -EINVAL;
1165
1166         /* See comment in soc_camera_probe() */
1167         dev_set_drvdata(&icd->dev, dev);
1168
1169         qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
1170         icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
1171         qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
1172         icd->exposure = qctrl ? qctrl->default_value : (unsigned short)~0;
1173
1174         return ici->ops->add(icd);
1175 }
1176 EXPORT_SYMBOL(soc_camera_video_start);
1177
1178 /* Called from client .remove() methods with .video_lock held */
1179 void soc_camera_video_stop(struct soc_camera_device *icd)
1180 {
1181         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1182
1183         dev_dbg(&icd->dev, "%s\n", __func__);
1184
1185         ici->ops->remove(icd);
1186 }
1187 EXPORT_SYMBOL(soc_camera_video_stop);
1188
1189 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1190 {
1191         struct soc_camera_link *icl = pdev->dev.platform_data;
1192         struct soc_camera_device *icd;
1193         int ret;
1194
1195         if (!icl)
1196                 return -EINVAL;
1197
1198         icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1199         if (!icd)
1200                 return -ENOMEM;
1201
1202         icd->iface = icl->bus_id;
1203         platform_set_drvdata(pdev, icd);
1204         icd->dev.platform_data = icl;
1205
1206         ret = soc_camera_device_register(icd);
1207         if (ret < 0)
1208                 goto escdevreg;
1209
1210         return 0;
1211
1212 escdevreg:
1213         kfree(icd);
1214
1215         return ret;
1216 }
1217
1218 /* Only called on rmmod for each platform device, since they are not
1219  * hot-pluggable. Now we know, that all our users - hosts and devices have
1220  * been unloaded already */
1221 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1222 {
1223         struct soc_camera_device *icd = platform_get_drvdata(pdev);
1224
1225         if (!icd)
1226                 return -EINVAL;
1227
1228         soc_camera_device_unregister(icd);
1229
1230         kfree(icd);
1231
1232         return 0;
1233 }
1234
1235 static struct platform_driver __refdata soc_camera_pdrv = {
1236         .remove  = __devexit_p(soc_camera_pdrv_remove),
1237         .driver  = {
1238                 .name   = "soc-camera-pdrv",
1239                 .owner  = THIS_MODULE,
1240         },
1241 };
1242
1243 static int __init soc_camera_init(void)
1244 {
1245         int ret = bus_register(&soc_camera_bus_type);
1246         if (ret)
1247                 return ret;
1248         ret = driver_register(&ic_drv);
1249         if (ret)
1250                 goto edrvr;
1251
1252         ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1253         if (ret)
1254                 goto epdr;
1255
1256         return 0;
1257
1258 epdr:
1259         driver_unregister(&ic_drv);
1260 edrvr:
1261         bus_unregister(&soc_camera_bus_type);
1262         return ret;
1263 }
1264
1265 static void __exit soc_camera_exit(void)
1266 {
1267         platform_driver_unregister(&soc_camera_pdrv);
1268         driver_unregister(&ic_drv);
1269         bus_unregister(&soc_camera_bus_type);
1270 }
1271
1272 module_init(soc_camera_init);
1273 module_exit(soc_camera_exit);
1274
1275 MODULE_DESCRIPTION("Image capture bus driver");
1276 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1277 MODULE_LICENSE("GPL");
1278 MODULE_ALIAS("platform:soc-camera-pdrv");