2 * camera image capture (abstract) bus driver
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
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.
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.
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/list.h>
23 #include <linux/err.h>
24 #include <linux/mutex.h>
25 #include <linux/vmalloc.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-dev.h>
30 #include <media/videobuf-core.h>
31 #include <media/soc_camera.h>
33 static LIST_HEAD(hosts);
34 static LIST_HEAD(devices);
35 static DEFINE_MUTEX(list_lock);
37 const struct soc_camera_data_format *soc_camera_format_by_fourcc(
38 struct soc_camera_device *icd, unsigned int fourcc)
42 for (i = 0; i < icd->num_formats; i++)
43 if (icd->formats[i].fourcc == fourcc)
44 return icd->formats + i;
47 EXPORT_SYMBOL(soc_camera_format_by_fourcc);
49 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
50 struct soc_camera_device *icd, unsigned int fourcc)
54 for (i = 0; i < icd->num_user_formats; i++)
55 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
56 return icd->user_formats + i;
59 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
62 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
63 * @icl: camera platform parameters
64 * @flags: flags to be inverted according to platform configuration
65 * @return: resulting flags
67 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
72 /* If only one of the two polarities is supported, switch to the opposite */
73 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
74 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
75 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
76 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
79 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
80 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
81 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
82 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
85 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
86 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
87 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
88 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
93 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
95 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
96 struct v4l2_format *f)
98 struct soc_camera_file *icf = file->private_data;
99 struct soc_camera_device *icd = icf->icd;
100 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
101 enum v4l2_field field;
104 WARN_ON(priv != file->private_data);
107 * TODO: this might also have to migrate to host-drivers, if anyone
108 * wishes to support other fields
110 field = f->fmt.pix.field;
112 if (field == V4L2_FIELD_ANY) {
113 f->fmt.pix.field = V4L2_FIELD_NONE;
114 } else if (field != V4L2_FIELD_NONE) {
115 dev_err(&icd->dev, "Field type invalid.\n");
119 /* limit format to hardware capabilities */
120 ret = ici->ops->try_fmt(icd, f);
125 static int soc_camera_enum_input(struct file *file, void *priv,
126 struct v4l2_input *inp)
131 inp->type = V4L2_INPUT_TYPE_CAMERA;
132 inp->std = V4L2_STD_UNKNOWN;
133 strcpy(inp->name, "Camera");
138 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
145 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
153 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
155 struct soc_camera_file *icf = file->private_data;
156 struct soc_camera_device *icd = icf->icd;
159 if (icd->ops->set_std)
160 ret = icd->ops->set_std(icd, a);
165 static int soc_camera_reqbufs(struct file *file, void *priv,
166 struct v4l2_requestbuffers *p)
169 struct soc_camera_file *icf = file->private_data;
170 struct soc_camera_device *icd = icf->icd;
171 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
173 WARN_ON(priv != file->private_data);
175 dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
177 ret = videobuf_reqbufs(&icf->vb_vidq, p);
181 return ici->ops->reqbufs(icf, p);
184 static int soc_camera_querybuf(struct file *file, void *priv,
185 struct v4l2_buffer *p)
187 struct soc_camera_file *icf = file->private_data;
189 WARN_ON(priv != file->private_data);
191 return videobuf_querybuf(&icf->vb_vidq, p);
194 static int soc_camera_qbuf(struct file *file, void *priv,
195 struct v4l2_buffer *p)
197 struct soc_camera_file *icf = file->private_data;
199 WARN_ON(priv != file->private_data);
201 return videobuf_qbuf(&icf->vb_vidq, p);
204 static int soc_camera_dqbuf(struct file *file, void *priv,
205 struct v4l2_buffer *p)
207 struct soc_camera_file *icf = file->private_data;
209 WARN_ON(priv != file->private_data);
211 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
214 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
216 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
219 if (!ici->ops->get_formats)
221 * Fallback mode - the host will have to serve all
222 * sensor-provided formats one-to-one to the user
224 fmts = icd->num_formats;
227 * First pass - only count formats this host-sensor
228 * configuration can provide
230 for (i = 0; i < icd->num_formats; i++)
231 fmts += ici->ops->get_formats(icd, i, NULL);
237 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
238 if (!icd->user_formats)
241 icd->num_user_formats = fmts;
244 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
246 /* Second pass - actually fill data formats */
247 for (i = 0; i < icd->num_formats; i++)
248 if (!ici->ops->get_formats) {
249 icd->user_formats[i].host_fmt = icd->formats + i;
250 icd->user_formats[i].cam_fmt = icd->formats + i;
251 icd->user_formats[i].buswidth = icd->formats[i].depth;
253 fmts += ici->ops->get_formats(icd, i,
254 &icd->user_formats[fmts]);
257 icd->current_fmt = icd->user_formats[0].host_fmt;
262 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
264 vfree(icd->user_formats);
267 static int soc_camera_open(struct inode *inode, struct file *file)
269 struct video_device *vdev;
270 struct soc_camera_device *icd;
271 struct soc_camera_host *ici;
272 struct soc_camera_file *icf;
275 icf = vmalloc(sizeof(*icf));
280 * It is safe to dereference these pointers now as long as a user has
281 * the video device open - we are protected by the held cdev reference.
284 vdev = video_devdata(file);
285 icd = container_of(vdev->parent, struct soc_camera_device, dev);
286 ici = to_soc_camera_host(icd->dev.parent);
288 if (!try_module_get(icd->ops->owner)) {
289 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
294 if (!try_module_get(ici->ops->owner)) {
295 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
300 /* Protect against icd->remove() until we module_get() both drivers. */
301 mutex_lock(&icd->video_lock);
306 /* Now we really have to activate the camera */
307 if (icd->use_count == 1) {
308 ret = soc_camera_init_user_formats(icd);
311 ret = ici->ops->add(icd);
313 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
318 mutex_unlock(&icd->video_lock);
320 file->private_data = icf;
321 dev_dbg(&icd->dev, "camera device open\n");
323 ici->ops->init_videobuf(&icf->vb_vidq, icd);
327 /* First two errors are entered with the .video_lock held */
329 soc_camera_free_user_formats(icd);
332 mutex_unlock(&icd->video_lock);
333 module_put(ici->ops->owner);
335 module_put(icd->ops->owner);
341 static int soc_camera_close(struct inode *inode, struct file *file)
343 struct soc_camera_file *icf = file->private_data;
344 struct soc_camera_device *icd = icf->icd;
345 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
346 struct video_device *vdev = icd->vdev;
348 mutex_lock(&icd->video_lock);
350 if (!icd->use_count) {
351 ici->ops->remove(icd);
352 soc_camera_free_user_formats(icd);
354 mutex_unlock(&icd->video_lock);
356 module_put(icd->ops->owner);
357 module_put(ici->ops->owner);
361 dev_dbg(vdev->parent, "camera device close\n");
366 static ssize_t soc_camera_read(struct file *file, char __user *buf,
367 size_t count, loff_t *ppos)
369 struct soc_camera_file *icf = file->private_data;
370 struct soc_camera_device *icd = icf->icd;
371 struct video_device *vdev = icd->vdev;
374 dev_err(vdev->parent, "camera device read not implemented\n");
379 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
381 struct soc_camera_file *icf = file->private_data;
382 struct soc_camera_device *icd = icf->icd;
385 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
387 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
389 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
390 (unsigned long)vma->vm_start,
391 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
397 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
399 struct soc_camera_file *icf = file->private_data;
400 struct soc_camera_device *icd = icf->icd;
401 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
403 if (list_empty(&icf->vb_vidq.stream)) {
404 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
408 return ici->ops->poll(file, pt);
411 static struct file_operations soc_camera_fops = {
412 .owner = THIS_MODULE,
413 .open = soc_camera_open,
414 .release = soc_camera_close,
415 .ioctl = video_ioctl2,
416 .read = soc_camera_read,
417 .mmap = soc_camera_mmap,
418 .poll = soc_camera_poll,
422 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
423 struct v4l2_format *f)
425 struct soc_camera_file *icf = file->private_data;
426 struct soc_camera_device *icd = icf->icd;
427 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
428 struct v4l2_pix_format *pix = &f->fmt.pix;
429 __u32 pixfmt = pix->pixelformat;
431 struct v4l2_rect rect;
433 WARN_ON(priv != file->private_data);
435 ret = soc_camera_try_fmt_vid_cap(file, priv, f);
439 mutex_lock(&icf->vb_vidq.vb_lock);
441 if (videobuf_queue_is_busy(&icf->vb_vidq)) {
442 dev_err(&icd->dev, "S_FMT denied: queue busy\n");
447 rect.left = icd->x_current;
448 rect.top = icd->y_current;
449 rect.width = pix->width;
450 rect.height = pix->height;
451 ret = ici->ops->set_fmt(icd, pix->pixelformat, &rect);
454 } else if (!icd->current_fmt ||
455 icd->current_fmt->fourcc != pixfmt) {
457 "Host driver hasn't set up current format correctly!\n");
462 icd->width = rect.width;
463 icd->height = rect.height;
464 icf->vb_vidq.field = pix->field;
465 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
466 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
469 dev_dbg(&icd->dev, "set width: %d height: %d\n",
470 icd->width, icd->height);
472 /* set physical bus parameters */
473 ret = ici->ops->set_bus_param(icd, pixfmt);
476 mutex_unlock(&icf->vb_vidq.vb_lock);
481 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
482 struct v4l2_fmtdesc *f)
484 struct soc_camera_file *icf = file->private_data;
485 struct soc_camera_device *icd = icf->icd;
486 const struct soc_camera_data_format *format;
488 WARN_ON(priv != file->private_data);
490 if (f->index >= icd->num_user_formats)
493 format = icd->user_formats[f->index].host_fmt;
495 strlcpy(f->description, format->name, sizeof(f->description));
496 f->pixelformat = format->fourcc;
500 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
501 struct v4l2_format *f)
503 struct soc_camera_file *icf = file->private_data;
504 struct soc_camera_device *icd = icf->icd;
505 struct v4l2_pix_format *pix = &f->fmt.pix;
507 WARN_ON(priv != file->private_data);
509 pix->width = icd->width;
510 pix->height = icd->height;
511 pix->field = icf->vb_vidq.field;
512 pix->pixelformat = icd->current_fmt->fourcc;
513 pix->bytesperline = pix->width *
514 DIV_ROUND_UP(icd->current_fmt->depth, 8);
515 pix->sizeimage = pix->height * pix->bytesperline;
516 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
517 icd->current_fmt->fourcc);
521 static int soc_camera_querycap(struct file *file, void *priv,
522 struct v4l2_capability *cap)
524 struct soc_camera_file *icf = file->private_data;
525 struct soc_camera_device *icd = icf->icd;
526 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
528 WARN_ON(priv != file->private_data);
530 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
531 return ici->ops->querycap(ici, cap);
534 static int soc_camera_streamon(struct file *file, void *priv,
535 enum v4l2_buf_type i)
537 struct soc_camera_file *icf = file->private_data;
538 struct soc_camera_device *icd = icf->icd;
541 WARN_ON(priv != file->private_data);
543 dev_dbg(&icd->dev, "%s\n", __func__);
545 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
548 mutex_lock(&icd->video_lock);
550 icd->ops->start_capture(icd);
552 /* This calls buf_queue from host driver's videobuf_queue_ops */
553 ret = videobuf_streamon(&icf->vb_vidq);
555 mutex_unlock(&icd->video_lock);
560 static int soc_camera_streamoff(struct file *file, void *priv,
561 enum v4l2_buf_type i)
563 struct soc_camera_file *icf = file->private_data;
564 struct soc_camera_device *icd = icf->icd;
566 WARN_ON(priv != file->private_data);
568 dev_dbg(&icd->dev, "%s\n", __func__);
570 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
573 mutex_lock(&icd->video_lock);
575 /* This calls buf_release from host driver's videobuf_queue_ops for all
576 * remaining buffers. When the last buffer is freed, stop capture */
577 videobuf_streamoff(&icf->vb_vidq);
579 icd->ops->stop_capture(icd);
581 mutex_unlock(&icd->video_lock);
586 static int soc_camera_queryctrl(struct file *file, void *priv,
587 struct v4l2_queryctrl *qc)
589 struct soc_camera_file *icf = file->private_data;
590 struct soc_camera_device *icd = icf->icd;
593 WARN_ON(priv != file->private_data);
598 for (i = 0; i < icd->ops->num_controls; i++)
599 if (qc->id == icd->ops->controls[i].id) {
600 memcpy(qc, &(icd->ops->controls[i]),
608 static int soc_camera_g_ctrl(struct file *file, void *priv,
609 struct v4l2_control *ctrl)
611 struct soc_camera_file *icf = file->private_data;
612 struct soc_camera_device *icd = icf->icd;
614 WARN_ON(priv != file->private_data);
618 if (icd->gain == (unsigned short)~0)
620 ctrl->value = icd->gain;
622 case V4L2_CID_EXPOSURE:
623 if (icd->exposure == (unsigned short)~0)
625 ctrl->value = icd->exposure;
629 if (icd->ops->get_control)
630 return icd->ops->get_control(icd, ctrl);
634 static int soc_camera_s_ctrl(struct file *file, void *priv,
635 struct v4l2_control *ctrl)
637 struct soc_camera_file *icf = file->private_data;
638 struct soc_camera_device *icd = icf->icd;
640 WARN_ON(priv != file->private_data);
642 if (icd->ops->set_control)
643 return icd->ops->set_control(icd, ctrl);
647 static int soc_camera_cropcap(struct file *file, void *fh,
648 struct v4l2_cropcap *a)
650 struct soc_camera_file *icf = file->private_data;
651 struct soc_camera_device *icd = icf->icd;
653 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
654 a->bounds.left = icd->x_min;
655 a->bounds.top = icd->y_min;
656 a->bounds.width = icd->width_max;
657 a->bounds.height = icd->height_max;
658 a->defrect.left = icd->x_min;
659 a->defrect.top = icd->y_min;
660 a->defrect.width = 640;
661 a->defrect.height = 480;
662 a->pixelaspect.numerator = 1;
663 a->pixelaspect.denominator = 1;
668 static int soc_camera_g_crop(struct file *file, void *fh,
671 struct soc_camera_file *icf = file->private_data;
672 struct soc_camera_device *icd = icf->icd;
674 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
675 a->c.left = icd->x_current;
676 a->c.top = icd->y_current;
677 a->c.width = icd->width;
678 a->c.height = icd->height;
683 static int soc_camera_s_crop(struct file *file, void *fh,
686 struct soc_camera_file *icf = file->private_data;
687 struct soc_camera_device *icd = icf->icd;
688 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
691 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
694 /* Cropping is allowed during a running capture, guard consistency */
695 mutex_lock(&icf->vb_vidq.vb_lock);
697 ret = ici->ops->set_fmt(icd, 0, &a->c);
699 icd->width = a->c.width;
700 icd->height = a->c.height;
701 icd->x_current = a->c.left;
702 icd->y_current = a->c.top;
705 mutex_unlock(&icf->vb_vidq.vb_lock);
710 static int soc_camera_g_chip_ident(struct file *file, void *fh,
711 struct v4l2_chip_ident *id)
713 struct soc_camera_file *icf = file->private_data;
714 struct soc_camera_device *icd = icf->icd;
716 if (!icd->ops->get_chip_id)
719 return icd->ops->get_chip_id(icd, id);
722 #ifdef CONFIG_VIDEO_ADV_DEBUG
723 static int soc_camera_g_register(struct file *file, void *fh,
724 struct v4l2_register *reg)
726 struct soc_camera_file *icf = file->private_data;
727 struct soc_camera_device *icd = icf->icd;
729 if (!icd->ops->get_register)
732 return icd->ops->get_register(icd, reg);
735 static int soc_camera_s_register(struct file *file, void *fh,
736 struct v4l2_register *reg)
738 struct soc_camera_file *icf = file->private_data;
739 struct soc_camera_device *icd = icf->icd;
741 if (!icd->ops->set_register)
744 return icd->ops->set_register(icd, reg);
748 static int device_register_link(struct soc_camera_device *icd)
750 int ret = device_register(&icd->dev);
753 /* Prevent calling device_unregister() */
754 icd->dev.parent = NULL;
755 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
756 /* Even if probe() was unsuccessful for all registered drivers,
757 * device_register() returns 0, and we add the link, just to
758 * document this camera's control device */
759 } else if (icd->control)
760 /* Have to sysfs_remove_link() before device_unregister()? */
761 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
764 "Failed creating the control symlink\n");
768 /* So far this function cannot fail */
769 static void scan_add_host(struct soc_camera_host *ici)
771 struct soc_camera_device *icd;
773 mutex_lock(&list_lock);
775 list_for_each_entry(icd, &devices, list) {
776 if (icd->iface == ici->nr) {
777 icd->dev.parent = &ici->dev;
778 device_register_link(icd);
782 mutex_unlock(&list_lock);
785 /* return: 0 if no match found or a match found and
786 * device_register() successful, error code otherwise */
787 static int scan_add_device(struct soc_camera_device *icd)
789 struct soc_camera_host *ici;
792 mutex_lock(&list_lock);
794 list_add_tail(&icd->list, &devices);
796 /* Watch out for class_for_each_device / class_find_device API by
797 * Dave Young <hidave.darkstar@gmail.com> */
798 list_for_each_entry(ici, &hosts, list) {
799 if (icd->iface == ici->nr) {
801 icd->dev.parent = &ici->dev;
806 mutex_unlock(&list_lock);
809 ret = device_register_link(icd);
814 static int soc_camera_probe(struct device *dev)
816 struct soc_camera_device *icd = to_soc_camera_dev(dev);
817 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
821 * Possible race scenario:
822 * modprobe <camera-host-driver> triggers __func__
823 * at this moment respective <camera-sensor-driver> gets rmmod'ed
824 * to protect take module references.
827 if (!try_module_get(icd->ops->owner)) {
828 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
833 if (!try_module_get(ici->ops->owner)) {
834 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
839 mutex_lock(&icd->video_lock);
841 /* We only call ->add() here to activate and probe the camera.
842 * We shall ->remove() and deactivate it immediately afterwards. */
843 ret = ici->ops->add(icd);
847 ret = icd->ops->probe(icd);
849 const struct v4l2_queryctrl *qctrl;
851 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
852 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
853 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
854 icd->exposure = qctrl ? qctrl->default_value :
857 ici->ops->remove(icd);
860 mutex_unlock(&icd->video_lock);
861 module_put(ici->ops->owner);
863 module_put(icd->ops->owner);
868 /* This is called on device_unregister, which only means we have to disconnect
869 * from the host, but not remove ourselves from the device list */
870 static int soc_camera_remove(struct device *dev)
872 struct soc_camera_device *icd = to_soc_camera_dev(dev);
874 if (icd->ops->remove)
875 icd->ops->remove(icd);
880 static int soc_camera_suspend(struct device *dev, pm_message_t state)
882 struct soc_camera_device *icd = to_soc_camera_dev(dev);
883 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
886 if (ici->ops->suspend)
887 ret = ici->ops->suspend(icd, state);
892 static int soc_camera_resume(struct device *dev)
894 struct soc_camera_device *icd = to_soc_camera_dev(dev);
895 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
898 if (ici->ops->resume)
899 ret = ici->ops->resume(icd);
904 static struct bus_type soc_camera_bus_type = {
905 .name = "soc-camera",
906 .probe = soc_camera_probe,
907 .remove = soc_camera_remove,
908 .suspend = soc_camera_suspend,
909 .resume = soc_camera_resume,
912 static struct device_driver ic_drv = {
914 .bus = &soc_camera_bus_type,
915 .owner = THIS_MODULE,
918 static void dummy_release(struct device *dev)
922 int soc_camera_host_register(struct soc_camera_host *ici)
925 struct soc_camera_host *ix;
927 if (!ici || !ici->ops ||
928 !ici->ops->try_fmt ||
929 !ici->ops->set_fmt ||
930 !ici->ops->set_bus_param ||
931 !ici->ops->querycap ||
932 !ici->ops->init_videobuf ||
933 !ici->ops->reqbufs ||
939 /* Number might be equal to the platform device ID */
940 dev_set_name(&ici->dev, "camera_host%d", ici->nr);
942 mutex_lock(&list_lock);
943 list_for_each_entry(ix, &hosts, list) {
944 if (ix->nr == ici->nr) {
945 mutex_unlock(&list_lock);
950 list_add_tail(&ici->list, &hosts);
951 mutex_unlock(&list_lock);
953 ici->dev.release = dummy_release;
955 ret = device_register(&ici->dev);
965 mutex_lock(&list_lock);
966 list_del(&ici->list);
967 mutex_unlock(&list_lock);
971 EXPORT_SYMBOL(soc_camera_host_register);
973 /* Unregister all clients! */
974 void soc_camera_host_unregister(struct soc_camera_host *ici)
976 struct soc_camera_device *icd;
978 mutex_lock(&list_lock);
980 list_del(&ici->list);
982 list_for_each_entry(icd, &devices, list) {
983 if (icd->dev.parent == &ici->dev) {
984 device_unregister(&icd->dev);
985 /* Not before device_unregister(), .remove
986 * needs parent to call ici->ops->remove() */
987 icd->dev.parent = NULL;
988 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
992 mutex_unlock(&list_lock);
994 device_unregister(&ici->dev);
996 EXPORT_SYMBOL(soc_camera_host_unregister);
998 /* Image capture device */
999 int soc_camera_device_register(struct soc_camera_device *icd)
1001 struct soc_camera_device *ix;
1004 if (!icd || !icd->ops ||
1007 !icd->ops->release ||
1008 !icd->ops->start_capture ||
1009 !icd->ops->stop_capture ||
1010 !icd->ops->set_fmt ||
1011 !icd->ops->try_fmt ||
1012 !icd->ops->query_bus_param ||
1013 !icd->ops->set_bus_param)
1016 for (i = 0; i < 256 && num < 0; i++) {
1018 list_for_each_entry(ix, &devices, list) {
1019 if (ix->iface == icd->iface && ix->devnum == i) {
1027 /* ok, we have 256 cameras on this host...
1028 * man, stay reasonable... */
1032 icd->dev.bus = &soc_camera_bus_type;
1033 dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
1035 icd->dev.release = dummy_release;
1037 icd->host_priv = NULL;
1038 mutex_init(&icd->video_lock);
1040 return scan_add_device(icd);
1042 EXPORT_SYMBOL(soc_camera_device_register);
1044 void soc_camera_device_unregister(struct soc_camera_device *icd)
1046 mutex_lock(&list_lock);
1047 list_del(&icd->list);
1049 /* The bus->remove will be eventually called */
1050 if (icd->dev.parent)
1051 device_unregister(&icd->dev);
1052 mutex_unlock(&list_lock);
1054 EXPORT_SYMBOL(soc_camera_device_unregister);
1056 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1057 .vidioc_querycap = soc_camera_querycap,
1058 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1059 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1060 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1061 .vidioc_enum_input = soc_camera_enum_input,
1062 .vidioc_g_input = soc_camera_g_input,
1063 .vidioc_s_input = soc_camera_s_input,
1064 .vidioc_s_std = soc_camera_s_std,
1065 .vidioc_reqbufs = soc_camera_reqbufs,
1066 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1067 .vidioc_querybuf = soc_camera_querybuf,
1068 .vidioc_qbuf = soc_camera_qbuf,
1069 .vidioc_dqbuf = soc_camera_dqbuf,
1070 .vidioc_streamon = soc_camera_streamon,
1071 .vidioc_streamoff = soc_camera_streamoff,
1072 .vidioc_queryctrl = soc_camera_queryctrl,
1073 .vidioc_g_ctrl = soc_camera_g_ctrl,
1074 .vidioc_s_ctrl = soc_camera_s_ctrl,
1075 .vidioc_cropcap = soc_camera_cropcap,
1076 .vidioc_g_crop = soc_camera_g_crop,
1077 .vidioc_s_crop = soc_camera_s_crop,
1078 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1079 #ifdef CONFIG_VIDEO_ADV_DEBUG
1080 .vidioc_g_register = soc_camera_g_register,
1081 .vidioc_s_register = soc_camera_s_register,
1086 * Usually called from the struct soc_camera_ops .probe() method, i.e., from
1087 * soc_camera_probe() above with .video_lock held
1089 int soc_camera_video_start(struct soc_camera_device *icd)
1091 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1093 struct video_device *vdev;
1095 if (!icd->dev.parent)
1098 vdev = video_device_alloc();
1101 dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
1103 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1105 vdev->parent = &icd->dev;
1106 vdev->current_norm = V4L2_STD_UNKNOWN;
1107 vdev->fops = &soc_camera_fops;
1108 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1109 vdev->release = video_device_release;
1111 vdev->tvnorms = V4L2_STD_UNKNOWN,
1113 err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
1115 dev_err(vdev->parent, "video_register_device failed\n");
1123 video_device_release(vdev);
1127 EXPORT_SYMBOL(soc_camera_video_start);
1129 void soc_camera_video_stop(struct soc_camera_device *icd)
1131 struct video_device *vdev = icd->vdev;
1133 dev_dbg(&icd->dev, "%s\n", __func__);
1135 if (!icd->dev.parent || !vdev)
1138 mutex_lock(&icd->video_lock);
1139 video_unregister_device(vdev);
1141 mutex_unlock(&icd->video_lock);
1143 EXPORT_SYMBOL(soc_camera_video_stop);
1145 static int __init soc_camera_init(void)
1147 int ret = bus_register(&soc_camera_bus_type);
1150 ret = driver_register(&ic_drv);
1157 bus_unregister(&soc_camera_bus_type);
1161 static void __exit soc_camera_exit(void)
1163 driver_unregister(&ic_drv);
1164 bus_unregister(&soc_camera_bus_type);
1167 module_init(soc_camera_init);
1168 module_exit(soc_camera_exit);
1170 MODULE_DESCRIPTION("Image capture bus driver");
1171 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1172 MODULE_LICENSE("GPL");