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)
158 static int soc_camera_reqbufs(struct file *file, void *priv,
159 struct v4l2_requestbuffers *p)
162 struct soc_camera_file *icf = file->private_data;
163 struct soc_camera_device *icd = icf->icd;
164 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
166 WARN_ON(priv != file->private_data);
168 dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
170 ret = videobuf_reqbufs(&icf->vb_vidq, p);
174 return ici->ops->reqbufs(icf, p);
177 static int soc_camera_querybuf(struct file *file, void *priv,
178 struct v4l2_buffer *p)
180 struct soc_camera_file *icf = file->private_data;
182 WARN_ON(priv != file->private_data);
184 return videobuf_querybuf(&icf->vb_vidq, p);
187 static int soc_camera_qbuf(struct file *file, void *priv,
188 struct v4l2_buffer *p)
190 struct soc_camera_file *icf = file->private_data;
192 WARN_ON(priv != file->private_data);
194 return videobuf_qbuf(&icf->vb_vidq, p);
197 static int soc_camera_dqbuf(struct file *file, void *priv,
198 struct v4l2_buffer *p)
200 struct soc_camera_file *icf = file->private_data;
202 WARN_ON(priv != file->private_data);
204 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
207 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
209 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
212 if (!ici->ops->get_formats)
214 * Fallback mode - the host will have to serve all
215 * sensor-provided formats one-to-one to the user
217 fmts = icd->num_formats;
220 * First pass - only count formats this host-sensor
221 * configuration can provide
223 for (i = 0; i < icd->num_formats; i++)
224 fmts += ici->ops->get_formats(icd, i, NULL);
230 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
231 if (!icd->user_formats)
234 icd->num_user_formats = fmts;
237 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
239 /* Second pass - actually fill data formats */
240 for (i = 0; i < icd->num_formats; i++)
241 if (!ici->ops->get_formats) {
242 icd->user_formats[i].host_fmt = icd->formats + i;
243 icd->user_formats[i].cam_fmt = icd->formats + i;
244 icd->user_formats[i].buswidth = icd->formats[i].depth;
246 fmts += ici->ops->get_formats(icd, i,
247 &icd->user_formats[fmts]);
250 icd->current_fmt = icd->user_formats[0].host_fmt;
255 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
257 vfree(icd->user_formats);
260 static int soc_camera_open(struct inode *inode, struct file *file)
262 struct video_device *vdev;
263 struct soc_camera_device *icd;
264 struct soc_camera_host *ici;
265 struct soc_camera_file *icf;
268 icf = vmalloc(sizeof(*icf));
273 * It is safe to dereference these pointers now as long as a user has
274 * the video device open - we are protected by the held cdev reference.
277 vdev = video_devdata(file);
278 icd = container_of(vdev->parent, struct soc_camera_device, dev);
279 ici = to_soc_camera_host(icd->dev.parent);
281 if (!try_module_get(icd->ops->owner)) {
282 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
287 if (!try_module_get(ici->ops->owner)) {
288 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
293 /* Protect against icd->remove() until we module_get() both drivers. */
294 mutex_lock(&icd->video_lock);
299 /* Now we really have to activate the camera */
300 if (icd->use_count == 1) {
301 ret = soc_camera_init_user_formats(icd);
304 ret = ici->ops->add(icd);
306 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
311 mutex_unlock(&icd->video_lock);
313 file->private_data = icf;
314 dev_dbg(&icd->dev, "camera device open\n");
316 ici->ops->init_videobuf(&icf->vb_vidq, icd);
320 /* First two errors are entered with the .video_lock held */
322 soc_camera_free_user_formats(icd);
325 mutex_unlock(&icd->video_lock);
326 module_put(ici->ops->owner);
328 module_put(icd->ops->owner);
334 static int soc_camera_close(struct inode *inode, struct file *file)
336 struct soc_camera_file *icf = file->private_data;
337 struct soc_camera_device *icd = icf->icd;
338 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
339 struct video_device *vdev = icd->vdev;
341 mutex_lock(&icd->video_lock);
343 if (!icd->use_count) {
344 ici->ops->remove(icd);
345 soc_camera_free_user_formats(icd);
347 mutex_unlock(&icd->video_lock);
349 module_put(icd->ops->owner);
350 module_put(ici->ops->owner);
354 dev_dbg(vdev->parent, "camera device close\n");
359 static ssize_t soc_camera_read(struct file *file, char __user *buf,
360 size_t count, loff_t *ppos)
362 struct soc_camera_file *icf = file->private_data;
363 struct soc_camera_device *icd = icf->icd;
364 struct video_device *vdev = icd->vdev;
367 dev_err(vdev->parent, "camera device read not implemented\n");
372 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
374 struct soc_camera_file *icf = file->private_data;
375 struct soc_camera_device *icd = icf->icd;
378 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
380 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
382 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
383 (unsigned long)vma->vm_start,
384 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
390 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
392 struct soc_camera_file *icf = file->private_data;
393 struct soc_camera_device *icd = icf->icd;
394 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
396 if (list_empty(&icf->vb_vidq.stream)) {
397 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
401 return ici->ops->poll(file, pt);
404 static struct file_operations soc_camera_fops = {
405 .owner = THIS_MODULE,
406 .open = soc_camera_open,
407 .release = soc_camera_close,
408 .ioctl = video_ioctl2,
409 .read = soc_camera_read,
410 .mmap = soc_camera_mmap,
411 .poll = soc_camera_poll,
415 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
416 struct v4l2_format *f)
418 struct soc_camera_file *icf = file->private_data;
419 struct soc_camera_device *icd = icf->icd;
420 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
421 struct v4l2_pix_format *pix = &f->fmt.pix;
422 __u32 pixfmt = pix->pixelformat;
424 struct v4l2_rect rect;
426 WARN_ON(priv != file->private_data);
428 ret = soc_camera_try_fmt_vid_cap(file, priv, f);
432 mutex_lock(&icf->vb_vidq.vb_lock);
434 if (videobuf_queue_is_busy(&icf->vb_vidq)) {
435 dev_err(&icd->dev, "S_FMT denied: queue busy\n");
440 rect.left = icd->x_current;
441 rect.top = icd->y_current;
442 rect.width = pix->width;
443 rect.height = pix->height;
444 ret = ici->ops->set_fmt(icd, pix->pixelformat, &rect);
447 } else if (!icd->current_fmt ||
448 icd->current_fmt->fourcc != pixfmt) {
450 "Host driver hasn't set up current format correctly!\n");
455 icd->width = rect.width;
456 icd->height = rect.height;
457 icf->vb_vidq.field = pix->field;
458 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
459 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
462 dev_dbg(&icd->dev, "set width: %d height: %d\n",
463 icd->width, icd->height);
465 /* set physical bus parameters */
466 ret = ici->ops->set_bus_param(icd, pixfmt);
469 mutex_unlock(&icf->vb_vidq.vb_lock);
474 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
475 struct v4l2_fmtdesc *f)
477 struct soc_camera_file *icf = file->private_data;
478 struct soc_camera_device *icd = icf->icd;
479 const struct soc_camera_data_format *format;
481 WARN_ON(priv != file->private_data);
483 if (f->index >= icd->num_user_formats)
486 format = icd->user_formats[f->index].host_fmt;
488 strlcpy(f->description, format->name, sizeof(f->description));
489 f->pixelformat = format->fourcc;
493 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
494 struct v4l2_format *f)
496 struct soc_camera_file *icf = file->private_data;
497 struct soc_camera_device *icd = icf->icd;
498 struct v4l2_pix_format *pix = &f->fmt.pix;
500 WARN_ON(priv != file->private_data);
502 pix->width = icd->width;
503 pix->height = icd->height;
504 pix->field = icf->vb_vidq.field;
505 pix->pixelformat = icd->current_fmt->fourcc;
506 pix->bytesperline = pix->width *
507 DIV_ROUND_UP(icd->current_fmt->depth, 8);
508 pix->sizeimage = pix->height * pix->bytesperline;
509 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
510 icd->current_fmt->fourcc);
514 static int soc_camera_querycap(struct file *file, void *priv,
515 struct v4l2_capability *cap)
517 struct soc_camera_file *icf = file->private_data;
518 struct soc_camera_device *icd = icf->icd;
519 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
521 WARN_ON(priv != file->private_data);
523 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
524 return ici->ops->querycap(ici, cap);
527 static int soc_camera_streamon(struct file *file, void *priv,
528 enum v4l2_buf_type i)
530 struct soc_camera_file *icf = file->private_data;
531 struct soc_camera_device *icd = icf->icd;
534 WARN_ON(priv != file->private_data);
536 dev_dbg(&icd->dev, "%s\n", __func__);
538 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
541 mutex_lock(&icd->video_lock);
543 icd->ops->start_capture(icd);
545 /* This calls buf_queue from host driver's videobuf_queue_ops */
546 ret = videobuf_streamon(&icf->vb_vidq);
548 mutex_unlock(&icd->video_lock);
553 static int soc_camera_streamoff(struct file *file, void *priv,
554 enum v4l2_buf_type i)
556 struct soc_camera_file *icf = file->private_data;
557 struct soc_camera_device *icd = icf->icd;
559 WARN_ON(priv != file->private_data);
561 dev_dbg(&icd->dev, "%s\n", __func__);
563 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
566 mutex_lock(&icd->video_lock);
568 /* This calls buf_release from host driver's videobuf_queue_ops for all
569 * remaining buffers. When the last buffer is freed, stop capture */
570 videobuf_streamoff(&icf->vb_vidq);
572 icd->ops->stop_capture(icd);
574 mutex_unlock(&icd->video_lock);
579 static int soc_camera_queryctrl(struct file *file, void *priv,
580 struct v4l2_queryctrl *qc)
582 struct soc_camera_file *icf = file->private_data;
583 struct soc_camera_device *icd = icf->icd;
586 WARN_ON(priv != file->private_data);
591 for (i = 0; i < icd->ops->num_controls; i++)
592 if (qc->id == icd->ops->controls[i].id) {
593 memcpy(qc, &(icd->ops->controls[i]),
601 static int soc_camera_g_ctrl(struct file *file, void *priv,
602 struct v4l2_control *ctrl)
604 struct soc_camera_file *icf = file->private_data;
605 struct soc_camera_device *icd = icf->icd;
607 WARN_ON(priv != file->private_data);
611 if (icd->gain == (unsigned short)~0)
613 ctrl->value = icd->gain;
615 case V4L2_CID_EXPOSURE:
616 if (icd->exposure == (unsigned short)~0)
618 ctrl->value = icd->exposure;
622 if (icd->ops->get_control)
623 return icd->ops->get_control(icd, ctrl);
627 static int soc_camera_s_ctrl(struct file *file, void *priv,
628 struct v4l2_control *ctrl)
630 struct soc_camera_file *icf = file->private_data;
631 struct soc_camera_device *icd = icf->icd;
633 WARN_ON(priv != file->private_data);
635 if (icd->ops->set_control)
636 return icd->ops->set_control(icd, ctrl);
640 static int soc_camera_cropcap(struct file *file, void *fh,
641 struct v4l2_cropcap *a)
643 struct soc_camera_file *icf = file->private_data;
644 struct soc_camera_device *icd = icf->icd;
646 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
647 a->bounds.left = icd->x_min;
648 a->bounds.top = icd->y_min;
649 a->bounds.width = icd->width_max;
650 a->bounds.height = icd->height_max;
651 a->defrect.left = icd->x_min;
652 a->defrect.top = icd->y_min;
653 a->defrect.width = 640;
654 a->defrect.height = 480;
655 a->pixelaspect.numerator = 1;
656 a->pixelaspect.denominator = 1;
661 static int soc_camera_g_crop(struct file *file, void *fh,
664 struct soc_camera_file *icf = file->private_data;
665 struct soc_camera_device *icd = icf->icd;
667 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
668 a->c.left = icd->x_current;
669 a->c.top = icd->y_current;
670 a->c.width = icd->width;
671 a->c.height = icd->height;
676 static int soc_camera_s_crop(struct file *file, void *fh,
679 struct soc_camera_file *icf = file->private_data;
680 struct soc_camera_device *icd = icf->icd;
681 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
684 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
687 /* Cropping is allowed during a running capture, guard consistency */
688 mutex_lock(&icf->vb_vidq.vb_lock);
690 ret = ici->ops->set_fmt(icd, 0, &a->c);
692 icd->width = a->c.width;
693 icd->height = a->c.height;
694 icd->x_current = a->c.left;
695 icd->y_current = a->c.top;
698 mutex_unlock(&icf->vb_vidq.vb_lock);
703 static int soc_camera_g_chip_ident(struct file *file, void *fh,
704 struct v4l2_chip_ident *id)
706 struct soc_camera_file *icf = file->private_data;
707 struct soc_camera_device *icd = icf->icd;
709 if (!icd->ops->get_chip_id)
712 return icd->ops->get_chip_id(icd, id);
715 #ifdef CONFIG_VIDEO_ADV_DEBUG
716 static int soc_camera_g_register(struct file *file, void *fh,
717 struct v4l2_register *reg)
719 struct soc_camera_file *icf = file->private_data;
720 struct soc_camera_device *icd = icf->icd;
722 if (!icd->ops->get_register)
725 return icd->ops->get_register(icd, reg);
728 static int soc_camera_s_register(struct file *file, void *fh,
729 struct v4l2_register *reg)
731 struct soc_camera_file *icf = file->private_data;
732 struct soc_camera_device *icd = icf->icd;
734 if (!icd->ops->set_register)
737 return icd->ops->set_register(icd, reg);
741 static int device_register_link(struct soc_camera_device *icd)
743 int ret = device_register(&icd->dev);
746 /* Prevent calling device_unregister() */
747 icd->dev.parent = NULL;
748 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
749 /* Even if probe() was unsuccessful for all registered drivers,
750 * device_register() returns 0, and we add the link, just to
751 * document this camera's control device */
752 } else if (icd->control)
753 /* Have to sysfs_remove_link() before device_unregister()? */
754 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
757 "Failed creating the control symlink\n");
761 /* So far this function cannot fail */
762 static void scan_add_host(struct soc_camera_host *ici)
764 struct soc_camera_device *icd;
766 mutex_lock(&list_lock);
768 list_for_each_entry(icd, &devices, list) {
769 if (icd->iface == ici->nr) {
770 icd->dev.parent = &ici->dev;
771 device_register_link(icd);
775 mutex_unlock(&list_lock);
778 /* return: 0 if no match found or a match found and
779 * device_register() successful, error code otherwise */
780 static int scan_add_device(struct soc_camera_device *icd)
782 struct soc_camera_host *ici;
785 mutex_lock(&list_lock);
787 list_add_tail(&icd->list, &devices);
789 /* Watch out for class_for_each_device / class_find_device API by
790 * Dave Young <hidave.darkstar@gmail.com> */
791 list_for_each_entry(ici, &hosts, list) {
792 if (icd->iface == ici->nr) {
794 icd->dev.parent = &ici->dev;
799 mutex_unlock(&list_lock);
802 ret = device_register_link(icd);
807 static int soc_camera_probe(struct device *dev)
809 struct soc_camera_device *icd = to_soc_camera_dev(dev);
810 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
814 * Possible race scenario:
815 * modprobe <camera-host-driver> triggers __func__
816 * at this moment respective <camera-sensor-driver> gets rmmod'ed
817 * to protect take module references.
820 if (!try_module_get(icd->ops->owner)) {
821 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
826 if (!try_module_get(ici->ops->owner)) {
827 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
832 mutex_lock(&icd->video_lock);
834 /* We only call ->add() here to activate and probe the camera.
835 * We shall ->remove() and deactivate it immediately afterwards. */
836 ret = ici->ops->add(icd);
840 ret = icd->ops->probe(icd);
842 const struct v4l2_queryctrl *qctrl;
844 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
845 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
846 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
847 icd->exposure = qctrl ? qctrl->default_value :
850 ici->ops->remove(icd);
853 mutex_unlock(&icd->video_lock);
854 module_put(ici->ops->owner);
856 module_put(icd->ops->owner);
861 /* This is called on device_unregister, which only means we have to disconnect
862 * from the host, but not remove ourselves from the device list */
863 static int soc_camera_remove(struct device *dev)
865 struct soc_camera_device *icd = to_soc_camera_dev(dev);
867 if (icd->ops->remove)
868 icd->ops->remove(icd);
873 static int soc_camera_suspend(struct device *dev, pm_message_t state)
875 struct soc_camera_device *icd = to_soc_camera_dev(dev);
876 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
879 if (ici->ops->suspend)
880 ret = ici->ops->suspend(icd, state);
885 static int soc_camera_resume(struct device *dev)
887 struct soc_camera_device *icd = to_soc_camera_dev(dev);
888 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
891 if (ici->ops->resume)
892 ret = ici->ops->resume(icd);
897 static struct bus_type soc_camera_bus_type = {
898 .name = "soc-camera",
899 .probe = soc_camera_probe,
900 .remove = soc_camera_remove,
901 .suspend = soc_camera_suspend,
902 .resume = soc_camera_resume,
905 static struct device_driver ic_drv = {
907 .bus = &soc_camera_bus_type,
908 .owner = THIS_MODULE,
911 static void dummy_release(struct device *dev)
915 int soc_camera_host_register(struct soc_camera_host *ici)
918 struct soc_camera_host *ix;
920 if (!ici || !ici->ops ||
921 !ici->ops->try_fmt ||
922 !ici->ops->set_fmt ||
923 !ici->ops->set_bus_param ||
924 !ici->ops->querycap ||
925 !ici->ops->init_videobuf ||
926 !ici->ops->reqbufs ||
932 /* Number might be equal to the platform device ID */
933 dev_set_name(&ici->dev, "camera_host%d", ici->nr);
935 mutex_lock(&list_lock);
936 list_for_each_entry(ix, &hosts, list) {
937 if (ix->nr == ici->nr) {
938 mutex_unlock(&list_lock);
943 list_add_tail(&ici->list, &hosts);
944 mutex_unlock(&list_lock);
946 ici->dev.release = dummy_release;
948 ret = device_register(&ici->dev);
958 mutex_lock(&list_lock);
959 list_del(&ici->list);
960 mutex_unlock(&list_lock);
964 EXPORT_SYMBOL(soc_camera_host_register);
966 /* Unregister all clients! */
967 void soc_camera_host_unregister(struct soc_camera_host *ici)
969 struct soc_camera_device *icd;
971 mutex_lock(&list_lock);
973 list_del(&ici->list);
975 list_for_each_entry(icd, &devices, list) {
976 if (icd->dev.parent == &ici->dev) {
977 device_unregister(&icd->dev);
978 /* Not before device_unregister(), .remove
979 * needs parent to call ici->ops->remove() */
980 icd->dev.parent = NULL;
981 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
985 mutex_unlock(&list_lock);
987 device_unregister(&ici->dev);
989 EXPORT_SYMBOL(soc_camera_host_unregister);
991 /* Image capture device */
992 int soc_camera_device_register(struct soc_camera_device *icd)
994 struct soc_camera_device *ix;
997 if (!icd || !icd->ops ||
1000 !icd->ops->release ||
1001 !icd->ops->start_capture ||
1002 !icd->ops->stop_capture ||
1003 !icd->ops->set_fmt ||
1004 !icd->ops->try_fmt ||
1005 !icd->ops->query_bus_param ||
1006 !icd->ops->set_bus_param)
1009 for (i = 0; i < 256 && num < 0; i++) {
1011 list_for_each_entry(ix, &devices, list) {
1012 if (ix->iface == icd->iface && ix->devnum == i) {
1020 /* ok, we have 256 cameras on this host...
1021 * man, stay reasonable... */
1025 icd->dev.bus = &soc_camera_bus_type;
1026 dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
1028 icd->dev.release = dummy_release;
1030 icd->host_priv = NULL;
1031 mutex_init(&icd->video_lock);
1033 return scan_add_device(icd);
1035 EXPORT_SYMBOL(soc_camera_device_register);
1037 void soc_camera_device_unregister(struct soc_camera_device *icd)
1039 mutex_lock(&list_lock);
1040 list_del(&icd->list);
1042 /* The bus->remove will be eventually called */
1043 if (icd->dev.parent)
1044 device_unregister(&icd->dev);
1045 mutex_unlock(&list_lock);
1047 EXPORT_SYMBOL(soc_camera_device_unregister);
1049 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1050 .vidioc_querycap = soc_camera_querycap,
1051 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1052 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1053 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1054 .vidioc_enum_input = soc_camera_enum_input,
1055 .vidioc_g_input = soc_camera_g_input,
1056 .vidioc_s_input = soc_camera_s_input,
1057 .vidioc_s_std = soc_camera_s_std,
1058 .vidioc_reqbufs = soc_camera_reqbufs,
1059 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1060 .vidioc_querybuf = soc_camera_querybuf,
1061 .vidioc_qbuf = soc_camera_qbuf,
1062 .vidioc_dqbuf = soc_camera_dqbuf,
1063 .vidioc_streamon = soc_camera_streamon,
1064 .vidioc_streamoff = soc_camera_streamoff,
1065 .vidioc_queryctrl = soc_camera_queryctrl,
1066 .vidioc_g_ctrl = soc_camera_g_ctrl,
1067 .vidioc_s_ctrl = soc_camera_s_ctrl,
1068 .vidioc_cropcap = soc_camera_cropcap,
1069 .vidioc_g_crop = soc_camera_g_crop,
1070 .vidioc_s_crop = soc_camera_s_crop,
1071 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1072 #ifdef CONFIG_VIDEO_ADV_DEBUG
1073 .vidioc_g_register = soc_camera_g_register,
1074 .vidioc_s_register = soc_camera_s_register,
1079 * Usually called from the struct soc_camera_ops .probe() method, i.e., from
1080 * soc_camera_probe() above with .video_lock held
1082 int soc_camera_video_start(struct soc_camera_device *icd)
1084 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1086 struct video_device *vdev;
1088 if (!icd->dev.parent)
1091 vdev = video_device_alloc();
1094 dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
1096 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1098 vdev->parent = &icd->dev;
1099 vdev->current_norm = V4L2_STD_UNKNOWN;
1100 vdev->fops = &soc_camera_fops;
1101 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1102 vdev->release = video_device_release;
1104 vdev->tvnorms = V4L2_STD_UNKNOWN,
1106 err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
1108 dev_err(vdev->parent, "video_register_device failed\n");
1116 video_device_release(vdev);
1120 EXPORT_SYMBOL(soc_camera_video_start);
1122 void soc_camera_video_stop(struct soc_camera_device *icd)
1124 struct video_device *vdev = icd->vdev;
1126 dev_dbg(&icd->dev, "%s\n", __func__);
1128 if (!icd->dev.parent || !vdev)
1131 mutex_lock(&icd->video_lock);
1132 video_unregister_device(vdev);
1134 mutex_unlock(&icd->video_lock);
1136 EXPORT_SYMBOL(soc_camera_video_stop);
1138 static int __init soc_camera_init(void)
1140 int ret = bus_register(&soc_camera_bus_type);
1143 ret = driver_register(&ic_drv);
1150 bus_unregister(&soc_camera_bus_type);
1154 static void __exit soc_camera_exit(void)
1156 driver_unregister(&ic_drv);
1157 bus_unregister(&soc_camera_bus_type);
1160 module_init(soc_camera_init);
1161 module_exit(soc_camera_exit);
1163 MODULE_DESCRIPTION("Image capture bus driver");
1164 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1165 MODULE_LICENSE("GPL");