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)
128 struct soc_camera_file *icf = file->private_data;
129 struct soc_camera_device *icd = icf->icd;
135 if (icd->ops->enum_input)
136 ret = icd->ops->enum_input(icd, inp);
138 /* default is camera */
139 inp->type = V4L2_INPUT_TYPE_CAMERA;
140 inp->std = V4L2_STD_UNKNOWN;
141 strcpy(inp->name, "Camera");
147 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
154 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
162 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
164 struct soc_camera_file *icf = file->private_data;
165 struct soc_camera_device *icd = icf->icd;
168 if (icd->ops->set_std)
169 ret = icd->ops->set_std(icd, a);
174 static int soc_camera_reqbufs(struct file *file, void *priv,
175 struct v4l2_requestbuffers *p)
178 struct soc_camera_file *icf = file->private_data;
179 struct soc_camera_device *icd = icf->icd;
180 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
182 WARN_ON(priv != file->private_data);
184 dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
186 ret = videobuf_reqbufs(&icf->vb_vidq, p);
190 return ici->ops->reqbufs(icf, p);
193 static int soc_camera_querybuf(struct file *file, void *priv,
194 struct v4l2_buffer *p)
196 struct soc_camera_file *icf = file->private_data;
198 WARN_ON(priv != file->private_data);
200 return videobuf_querybuf(&icf->vb_vidq, p);
203 static int soc_camera_qbuf(struct file *file, void *priv,
204 struct v4l2_buffer *p)
206 struct soc_camera_file *icf = file->private_data;
208 WARN_ON(priv != file->private_data);
210 return videobuf_qbuf(&icf->vb_vidq, p);
213 static int soc_camera_dqbuf(struct file *file, void *priv,
214 struct v4l2_buffer *p)
216 struct soc_camera_file *icf = file->private_data;
218 WARN_ON(priv != file->private_data);
220 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
223 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
225 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
228 if (!ici->ops->get_formats)
230 * Fallback mode - the host will have to serve all
231 * sensor-provided formats one-to-one to the user
233 fmts = icd->num_formats;
236 * First pass - only count formats this host-sensor
237 * configuration can provide
239 for (i = 0; i < icd->num_formats; i++)
240 fmts += ici->ops->get_formats(icd, i, NULL);
246 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
247 if (!icd->user_formats)
250 icd->num_user_formats = fmts;
253 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
255 /* Second pass - actually fill data formats */
256 for (i = 0; i < icd->num_formats; i++)
257 if (!ici->ops->get_formats) {
258 icd->user_formats[i].host_fmt = icd->formats + i;
259 icd->user_formats[i].cam_fmt = icd->formats + i;
260 icd->user_formats[i].buswidth = icd->formats[i].depth;
262 fmts += ici->ops->get_formats(icd, i,
263 &icd->user_formats[fmts]);
266 icd->current_fmt = icd->user_formats[0].host_fmt;
271 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
273 vfree(icd->user_formats);
276 static int soc_camera_open(struct inode *inode, struct file *file)
278 struct video_device *vdev;
279 struct soc_camera_device *icd;
280 struct soc_camera_host *ici;
281 struct soc_camera_file *icf;
284 icf = vmalloc(sizeof(*icf));
289 * It is safe to dereference these pointers now as long as a user has
290 * the video device open - we are protected by the held cdev reference.
293 vdev = video_devdata(file);
294 icd = container_of(vdev->parent, struct soc_camera_device, dev);
295 ici = to_soc_camera_host(icd->dev.parent);
297 if (!try_module_get(icd->ops->owner)) {
298 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
303 if (!try_module_get(ici->ops->owner)) {
304 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
309 /* Protect against icd->remove() until we module_get() both drivers. */
310 mutex_lock(&icd->video_lock);
315 /* Now we really have to activate the camera */
316 if (icd->use_count == 1) {
317 ret = soc_camera_init_user_formats(icd);
320 ret = ici->ops->add(icd);
322 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
327 mutex_unlock(&icd->video_lock);
329 file->private_data = icf;
330 dev_dbg(&icd->dev, "camera device open\n");
332 ici->ops->init_videobuf(&icf->vb_vidq, icd);
336 /* First two errors are entered with the .video_lock held */
338 soc_camera_free_user_formats(icd);
341 mutex_unlock(&icd->video_lock);
342 module_put(ici->ops->owner);
344 module_put(icd->ops->owner);
350 static int soc_camera_close(struct inode *inode, struct file *file)
352 struct soc_camera_file *icf = file->private_data;
353 struct soc_camera_device *icd = icf->icd;
354 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
355 struct video_device *vdev = icd->vdev;
357 mutex_lock(&icd->video_lock);
359 if (!icd->use_count) {
360 ici->ops->remove(icd);
361 soc_camera_free_user_formats(icd);
363 mutex_unlock(&icd->video_lock);
365 module_put(icd->ops->owner);
366 module_put(ici->ops->owner);
370 dev_dbg(vdev->parent, "camera device close\n");
375 static ssize_t soc_camera_read(struct file *file, char __user *buf,
376 size_t count, loff_t *ppos)
378 struct soc_camera_file *icf = file->private_data;
379 struct soc_camera_device *icd = icf->icd;
380 struct video_device *vdev = icd->vdev;
383 dev_err(vdev->parent, "camera device read not implemented\n");
388 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
390 struct soc_camera_file *icf = file->private_data;
391 struct soc_camera_device *icd = icf->icd;
394 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
396 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
398 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
399 (unsigned long)vma->vm_start,
400 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
406 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
408 struct soc_camera_file *icf = file->private_data;
409 struct soc_camera_device *icd = icf->icd;
410 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
412 if (list_empty(&icf->vb_vidq.stream)) {
413 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
417 return ici->ops->poll(file, pt);
420 static struct file_operations soc_camera_fops = {
421 .owner = THIS_MODULE,
422 .open = soc_camera_open,
423 .release = soc_camera_close,
424 .ioctl = video_ioctl2,
425 .read = soc_camera_read,
426 .mmap = soc_camera_mmap,
427 .poll = soc_camera_poll,
431 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
432 struct v4l2_format *f)
434 struct soc_camera_file *icf = file->private_data;
435 struct soc_camera_device *icd = icf->icd;
436 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
437 struct v4l2_pix_format *pix = &f->fmt.pix;
438 __u32 pixfmt = pix->pixelformat;
440 struct v4l2_rect rect;
442 WARN_ON(priv != file->private_data);
444 ret = soc_camera_try_fmt_vid_cap(file, priv, f);
448 mutex_lock(&icf->vb_vidq.vb_lock);
450 if (videobuf_queue_is_busy(&icf->vb_vidq)) {
451 dev_err(&icd->dev, "S_FMT denied: queue busy\n");
456 rect.left = icd->x_current;
457 rect.top = icd->y_current;
458 rect.width = pix->width;
459 rect.height = pix->height;
460 ret = ici->ops->set_fmt(icd, pix->pixelformat, &rect);
463 } else if (!icd->current_fmt ||
464 icd->current_fmt->fourcc != pixfmt) {
466 "Host driver hasn't set up current format correctly!\n");
471 icd->width = rect.width;
472 icd->height = rect.height;
473 icf->vb_vidq.field = pix->field;
474 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
475 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
478 dev_dbg(&icd->dev, "set width: %d height: %d\n",
479 icd->width, icd->height);
481 /* set physical bus parameters */
482 ret = ici->ops->set_bus_param(icd, pixfmt);
485 mutex_unlock(&icf->vb_vidq.vb_lock);
490 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
491 struct v4l2_fmtdesc *f)
493 struct soc_camera_file *icf = file->private_data;
494 struct soc_camera_device *icd = icf->icd;
495 const struct soc_camera_data_format *format;
497 WARN_ON(priv != file->private_data);
499 if (f->index >= icd->num_user_formats)
502 format = icd->user_formats[f->index].host_fmt;
504 strlcpy(f->description, format->name, sizeof(f->description));
505 f->pixelformat = format->fourcc;
509 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
510 struct v4l2_format *f)
512 struct soc_camera_file *icf = file->private_data;
513 struct soc_camera_device *icd = icf->icd;
514 struct v4l2_pix_format *pix = &f->fmt.pix;
516 WARN_ON(priv != file->private_data);
518 pix->width = icd->width;
519 pix->height = icd->height;
520 pix->field = icf->vb_vidq.field;
521 pix->pixelformat = icd->current_fmt->fourcc;
522 pix->bytesperline = pix->width *
523 DIV_ROUND_UP(icd->current_fmt->depth, 8);
524 pix->sizeimage = pix->height * pix->bytesperline;
525 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
526 icd->current_fmt->fourcc);
530 static int soc_camera_querycap(struct file *file, void *priv,
531 struct v4l2_capability *cap)
533 struct soc_camera_file *icf = file->private_data;
534 struct soc_camera_device *icd = icf->icd;
535 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
537 WARN_ON(priv != file->private_data);
539 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
540 return ici->ops->querycap(ici, cap);
543 static int soc_camera_streamon(struct file *file, void *priv,
544 enum v4l2_buf_type i)
546 struct soc_camera_file *icf = file->private_data;
547 struct soc_camera_device *icd = icf->icd;
550 WARN_ON(priv != file->private_data);
552 dev_dbg(&icd->dev, "%s\n", __func__);
554 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
557 mutex_lock(&icd->video_lock);
559 icd->ops->start_capture(icd);
561 /* This calls buf_queue from host driver's videobuf_queue_ops */
562 ret = videobuf_streamon(&icf->vb_vidq);
564 mutex_unlock(&icd->video_lock);
569 static int soc_camera_streamoff(struct file *file, void *priv,
570 enum v4l2_buf_type i)
572 struct soc_camera_file *icf = file->private_data;
573 struct soc_camera_device *icd = icf->icd;
575 WARN_ON(priv != file->private_data);
577 dev_dbg(&icd->dev, "%s\n", __func__);
579 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
582 mutex_lock(&icd->video_lock);
584 /* This calls buf_release from host driver's videobuf_queue_ops for all
585 * remaining buffers. When the last buffer is freed, stop capture */
586 videobuf_streamoff(&icf->vb_vidq);
588 icd->ops->stop_capture(icd);
590 mutex_unlock(&icd->video_lock);
595 static int soc_camera_queryctrl(struct file *file, void *priv,
596 struct v4l2_queryctrl *qc)
598 struct soc_camera_file *icf = file->private_data;
599 struct soc_camera_device *icd = icf->icd;
602 WARN_ON(priv != file->private_data);
607 for (i = 0; i < icd->ops->num_controls; i++)
608 if (qc->id == icd->ops->controls[i].id) {
609 memcpy(qc, &(icd->ops->controls[i]),
617 static int soc_camera_g_ctrl(struct file *file, void *priv,
618 struct v4l2_control *ctrl)
620 struct soc_camera_file *icf = file->private_data;
621 struct soc_camera_device *icd = icf->icd;
623 WARN_ON(priv != file->private_data);
627 if (icd->gain == (unsigned short)~0)
629 ctrl->value = icd->gain;
631 case V4L2_CID_EXPOSURE:
632 if (icd->exposure == (unsigned short)~0)
634 ctrl->value = icd->exposure;
638 if (icd->ops->get_control)
639 return icd->ops->get_control(icd, ctrl);
643 static int soc_camera_s_ctrl(struct file *file, void *priv,
644 struct v4l2_control *ctrl)
646 struct soc_camera_file *icf = file->private_data;
647 struct soc_camera_device *icd = icf->icd;
649 WARN_ON(priv != file->private_data);
651 if (icd->ops->set_control)
652 return icd->ops->set_control(icd, ctrl);
656 static int soc_camera_cropcap(struct file *file, void *fh,
657 struct v4l2_cropcap *a)
659 struct soc_camera_file *icf = file->private_data;
660 struct soc_camera_device *icd = icf->icd;
662 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
663 a->bounds.left = icd->x_min;
664 a->bounds.top = icd->y_min;
665 a->bounds.width = icd->width_max;
666 a->bounds.height = icd->height_max;
667 a->defrect.left = icd->x_min;
668 a->defrect.top = icd->y_min;
669 a->defrect.width = 640;
670 a->defrect.height = 480;
671 a->pixelaspect.numerator = 1;
672 a->pixelaspect.denominator = 1;
677 static int soc_camera_g_crop(struct file *file, void *fh,
680 struct soc_camera_file *icf = file->private_data;
681 struct soc_camera_device *icd = icf->icd;
683 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
684 a->c.left = icd->x_current;
685 a->c.top = icd->y_current;
686 a->c.width = icd->width;
687 a->c.height = icd->height;
692 static int soc_camera_s_crop(struct file *file, void *fh,
695 struct soc_camera_file *icf = file->private_data;
696 struct soc_camera_device *icd = icf->icd;
697 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
700 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
703 /* Cropping is allowed during a running capture, guard consistency */
704 mutex_lock(&icf->vb_vidq.vb_lock);
706 ret = ici->ops->set_fmt(icd, 0, &a->c);
708 icd->width = a->c.width;
709 icd->height = a->c.height;
710 icd->x_current = a->c.left;
711 icd->y_current = a->c.top;
714 mutex_unlock(&icf->vb_vidq.vb_lock);
719 static int soc_camera_g_chip_ident(struct file *file, void *fh,
720 struct v4l2_chip_ident *id)
722 struct soc_camera_file *icf = file->private_data;
723 struct soc_camera_device *icd = icf->icd;
725 if (!icd->ops->get_chip_id)
728 return icd->ops->get_chip_id(icd, id);
731 #ifdef CONFIG_VIDEO_ADV_DEBUG
732 static int soc_camera_g_register(struct file *file, void *fh,
733 struct v4l2_register *reg)
735 struct soc_camera_file *icf = file->private_data;
736 struct soc_camera_device *icd = icf->icd;
738 if (!icd->ops->get_register)
741 return icd->ops->get_register(icd, reg);
744 static int soc_camera_s_register(struct file *file, void *fh,
745 struct v4l2_register *reg)
747 struct soc_camera_file *icf = file->private_data;
748 struct soc_camera_device *icd = icf->icd;
750 if (!icd->ops->set_register)
753 return icd->ops->set_register(icd, reg);
757 static int device_register_link(struct soc_camera_device *icd)
759 int ret = device_register(&icd->dev);
762 /* Prevent calling device_unregister() */
763 icd->dev.parent = NULL;
764 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
765 /* Even if probe() was unsuccessful for all registered drivers,
766 * device_register() returns 0, and we add the link, just to
767 * document this camera's control device */
768 } else if (icd->control)
769 /* Have to sysfs_remove_link() before device_unregister()? */
770 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
773 "Failed creating the control symlink\n");
777 /* So far this function cannot fail */
778 static void scan_add_host(struct soc_camera_host *ici)
780 struct soc_camera_device *icd;
782 mutex_lock(&list_lock);
784 list_for_each_entry(icd, &devices, list) {
785 if (icd->iface == ici->nr) {
786 icd->dev.parent = &ici->dev;
787 device_register_link(icd);
791 mutex_unlock(&list_lock);
794 /* return: 0 if no match found or a match found and
795 * device_register() successful, error code otherwise */
796 static int scan_add_device(struct soc_camera_device *icd)
798 struct soc_camera_host *ici;
801 mutex_lock(&list_lock);
803 list_add_tail(&icd->list, &devices);
805 /* Watch out for class_for_each_device / class_find_device API by
806 * Dave Young <hidave.darkstar@gmail.com> */
807 list_for_each_entry(ici, &hosts, list) {
808 if (icd->iface == ici->nr) {
810 icd->dev.parent = &ici->dev;
815 mutex_unlock(&list_lock);
818 ret = device_register_link(icd);
823 static int soc_camera_probe(struct device *dev)
825 struct soc_camera_device *icd = to_soc_camera_dev(dev);
826 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
830 * Possible race scenario:
831 * modprobe <camera-host-driver> triggers __func__
832 * at this moment respective <camera-sensor-driver> gets rmmod'ed
833 * to protect take module references.
836 if (!try_module_get(icd->ops->owner)) {
837 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
842 if (!try_module_get(ici->ops->owner)) {
843 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
848 mutex_lock(&icd->video_lock);
850 /* We only call ->add() here to activate and probe the camera.
851 * We shall ->remove() and deactivate it immediately afterwards. */
852 ret = ici->ops->add(icd);
856 ret = icd->ops->probe(icd);
858 const struct v4l2_queryctrl *qctrl;
860 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
861 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
862 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
863 icd->exposure = qctrl ? qctrl->default_value :
866 ici->ops->remove(icd);
869 mutex_unlock(&icd->video_lock);
870 module_put(ici->ops->owner);
872 module_put(icd->ops->owner);
877 /* This is called on device_unregister, which only means we have to disconnect
878 * from the host, but not remove ourselves from the device list */
879 static int soc_camera_remove(struct device *dev)
881 struct soc_camera_device *icd = to_soc_camera_dev(dev);
883 if (icd->ops->remove)
884 icd->ops->remove(icd);
889 static int soc_camera_suspend(struct device *dev, pm_message_t state)
891 struct soc_camera_device *icd = to_soc_camera_dev(dev);
892 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
895 if (ici->ops->suspend)
896 ret = ici->ops->suspend(icd, state);
901 static int soc_camera_resume(struct device *dev)
903 struct soc_camera_device *icd = to_soc_camera_dev(dev);
904 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
907 if (ici->ops->resume)
908 ret = ici->ops->resume(icd);
913 static struct bus_type soc_camera_bus_type = {
914 .name = "soc-camera",
915 .probe = soc_camera_probe,
916 .remove = soc_camera_remove,
917 .suspend = soc_camera_suspend,
918 .resume = soc_camera_resume,
921 static struct device_driver ic_drv = {
923 .bus = &soc_camera_bus_type,
924 .owner = THIS_MODULE,
927 static void dummy_release(struct device *dev)
931 int soc_camera_host_register(struct soc_camera_host *ici)
934 struct soc_camera_host *ix;
936 if (!ici || !ici->ops ||
937 !ici->ops->try_fmt ||
938 !ici->ops->set_fmt ||
939 !ici->ops->set_bus_param ||
940 !ici->ops->querycap ||
941 !ici->ops->init_videobuf ||
942 !ici->ops->reqbufs ||
948 /* Number might be equal to the platform device ID */
949 dev_set_name(&ici->dev, "camera_host%d", ici->nr);
951 mutex_lock(&list_lock);
952 list_for_each_entry(ix, &hosts, list) {
953 if (ix->nr == ici->nr) {
954 mutex_unlock(&list_lock);
959 list_add_tail(&ici->list, &hosts);
960 mutex_unlock(&list_lock);
962 ici->dev.release = dummy_release;
964 ret = device_register(&ici->dev);
974 mutex_lock(&list_lock);
975 list_del(&ici->list);
976 mutex_unlock(&list_lock);
980 EXPORT_SYMBOL(soc_camera_host_register);
982 /* Unregister all clients! */
983 void soc_camera_host_unregister(struct soc_camera_host *ici)
985 struct soc_camera_device *icd;
987 mutex_lock(&list_lock);
989 list_del(&ici->list);
991 list_for_each_entry(icd, &devices, list) {
992 if (icd->dev.parent == &ici->dev) {
993 device_unregister(&icd->dev);
994 /* Not before device_unregister(), .remove
995 * needs parent to call ici->ops->remove() */
996 icd->dev.parent = NULL;
997 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
1001 mutex_unlock(&list_lock);
1003 device_unregister(&ici->dev);
1005 EXPORT_SYMBOL(soc_camera_host_unregister);
1007 /* Image capture device */
1008 int soc_camera_device_register(struct soc_camera_device *icd)
1010 struct soc_camera_device *ix;
1013 if (!icd || !icd->ops ||
1016 !icd->ops->release ||
1017 !icd->ops->start_capture ||
1018 !icd->ops->stop_capture ||
1019 !icd->ops->set_fmt ||
1020 !icd->ops->try_fmt ||
1021 !icd->ops->query_bus_param ||
1022 !icd->ops->set_bus_param)
1025 for (i = 0; i < 256 && num < 0; i++) {
1027 list_for_each_entry(ix, &devices, list) {
1028 if (ix->iface == icd->iface && ix->devnum == i) {
1036 /* ok, we have 256 cameras on this host...
1037 * man, stay reasonable... */
1041 icd->dev.bus = &soc_camera_bus_type;
1042 dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
1044 icd->dev.release = dummy_release;
1046 icd->host_priv = NULL;
1047 mutex_init(&icd->video_lock);
1049 return scan_add_device(icd);
1051 EXPORT_SYMBOL(soc_camera_device_register);
1053 void soc_camera_device_unregister(struct soc_camera_device *icd)
1055 mutex_lock(&list_lock);
1056 list_del(&icd->list);
1058 /* The bus->remove will be eventually called */
1059 if (icd->dev.parent)
1060 device_unregister(&icd->dev);
1061 mutex_unlock(&list_lock);
1063 EXPORT_SYMBOL(soc_camera_device_unregister);
1065 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1066 .vidioc_querycap = soc_camera_querycap,
1067 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1068 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1069 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1070 .vidioc_enum_input = soc_camera_enum_input,
1071 .vidioc_g_input = soc_camera_g_input,
1072 .vidioc_s_input = soc_camera_s_input,
1073 .vidioc_s_std = soc_camera_s_std,
1074 .vidioc_reqbufs = soc_camera_reqbufs,
1075 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1076 .vidioc_querybuf = soc_camera_querybuf,
1077 .vidioc_qbuf = soc_camera_qbuf,
1078 .vidioc_dqbuf = soc_camera_dqbuf,
1079 .vidioc_streamon = soc_camera_streamon,
1080 .vidioc_streamoff = soc_camera_streamoff,
1081 .vidioc_queryctrl = soc_camera_queryctrl,
1082 .vidioc_g_ctrl = soc_camera_g_ctrl,
1083 .vidioc_s_ctrl = soc_camera_s_ctrl,
1084 .vidioc_cropcap = soc_camera_cropcap,
1085 .vidioc_g_crop = soc_camera_g_crop,
1086 .vidioc_s_crop = soc_camera_s_crop,
1087 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1088 #ifdef CONFIG_VIDEO_ADV_DEBUG
1089 .vidioc_g_register = soc_camera_g_register,
1090 .vidioc_s_register = soc_camera_s_register,
1095 * Usually called from the struct soc_camera_ops .probe() method, i.e., from
1096 * soc_camera_probe() above with .video_lock held
1098 int soc_camera_video_start(struct soc_camera_device *icd)
1100 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1102 struct video_device *vdev;
1104 if (!icd->dev.parent)
1107 vdev = video_device_alloc();
1110 dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
1112 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1114 vdev->parent = &icd->dev;
1115 vdev->current_norm = V4L2_STD_UNKNOWN;
1116 vdev->fops = &soc_camera_fops;
1117 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1118 vdev->release = video_device_release;
1120 vdev->tvnorms = V4L2_STD_UNKNOWN,
1122 err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
1124 dev_err(vdev->parent, "video_register_device failed\n");
1132 video_device_release(vdev);
1136 EXPORT_SYMBOL(soc_camera_video_start);
1138 void soc_camera_video_stop(struct soc_camera_device *icd)
1140 struct video_device *vdev = icd->vdev;
1142 dev_dbg(&icd->dev, "%s\n", __func__);
1144 if (!icd->dev.parent || !vdev)
1147 mutex_lock(&icd->video_lock);
1148 video_unregister_device(vdev);
1150 mutex_unlock(&icd->video_lock);
1152 EXPORT_SYMBOL(soc_camera_video_stop);
1154 static int __init soc_camera_init(void)
1156 int ret = bus_register(&soc_camera_bus_type);
1159 ret = driver_register(&ic_drv);
1166 bus_unregister(&soc_camera_bus_type);
1170 static void __exit soc_camera_exit(void)
1172 driver_unregister(&ic_drv);
1173 bus_unregister(&soc_camera_bus_type);
1176 module_init(soc_camera_init);
1177 module_exit(soc_camera_exit);
1179 MODULE_DESCRIPTION("Image capture bus driver");
1180 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1181 MODULE_LICENSE("GPL");