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/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/slab.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/vmalloc.h>
31 #include <media/soc_camera.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-dev.h>
35 #include <media/videobuf-core.h>
36 #include <media/soc_mediabus.h>
38 /* Default to VGA resolution */
39 #define DEFAULT_WIDTH 640
40 #define DEFAULT_HEIGHT 480
42 static LIST_HEAD(hosts);
43 static LIST_HEAD(devices);
44 static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
46 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
47 struct soc_camera_device *icd, unsigned int fourcc)
51 for (i = 0; i < icd->num_user_formats; i++)
52 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
53 return icd->user_formats + i;
56 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
59 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
60 * @icl: camera platform parameters
61 * @flags: flags to be inverted according to platform configuration
62 * @return: resulting flags
64 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
69 /* If only one of the two polarities is supported, switch to the opposite */
70 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
71 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
72 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
73 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
76 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
77 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
78 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
79 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
82 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
83 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
84 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
85 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
90 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
92 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
93 struct v4l2_format *f)
95 struct soc_camera_file *icf = file->private_data;
96 struct soc_camera_device *icd = icf->icd;
97 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
99 WARN_ON(priv != file->private_data);
101 /* limit format to hardware capabilities */
102 return ici->ops->try_fmt(icd, f);
105 static int soc_camera_enum_input(struct file *file, void *priv,
106 struct v4l2_input *inp)
108 struct soc_camera_file *icf = file->private_data;
109 struct soc_camera_device *icd = icf->icd;
115 if (icd->ops->enum_input)
116 ret = icd->ops->enum_input(icd, inp);
118 /* default is camera */
119 inp->type = V4L2_INPUT_TYPE_CAMERA;
120 inp->std = V4L2_STD_UNKNOWN;
121 strcpy(inp->name, "Camera");
127 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
134 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
142 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
144 struct soc_camera_file *icf = file->private_data;
145 struct soc_camera_device *icd = icf->icd;
146 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
148 return v4l2_subdev_call(sd, core, s_std, *a);
151 static int soc_camera_reqbufs(struct file *file, void *priv,
152 struct v4l2_requestbuffers *p)
155 struct soc_camera_file *icf = file->private_data;
156 struct soc_camera_device *icd = icf->icd;
157 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
159 WARN_ON(priv != file->private_data);
161 ret = videobuf_reqbufs(&icf->vb_vidq, p);
165 return ici->ops->reqbufs(icf, p);
168 static int soc_camera_querybuf(struct file *file, void *priv,
169 struct v4l2_buffer *p)
171 struct soc_camera_file *icf = file->private_data;
173 WARN_ON(priv != file->private_data);
175 return videobuf_querybuf(&icf->vb_vidq, p);
178 static int soc_camera_qbuf(struct file *file, void *priv,
179 struct v4l2_buffer *p)
181 struct soc_camera_file *icf = file->private_data;
183 WARN_ON(priv != file->private_data);
185 return videobuf_qbuf(&icf->vb_vidq, p);
188 static int soc_camera_dqbuf(struct file *file, void *priv,
189 struct v4l2_buffer *p)
191 struct soc_camera_file *icf = file->private_data;
193 WARN_ON(priv != file->private_data);
195 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
198 /* Always entered with .video_lock held */
199 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
201 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
202 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
203 unsigned int i, fmts = 0, raw_fmts = 0;
205 enum v4l2_mbus_pixelcode code;
207 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
210 if (!ici->ops->get_formats)
212 * Fallback mode - the host will have to serve all
213 * sensor-provided formats one-to-one to the user
218 * First pass - only count formats this host-sensor
219 * configuration can provide
221 for (i = 0; i < raw_fmts; i++) {
222 ret = ici->ops->get_formats(icd, i, NULL);
232 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
233 if (!icd->user_formats)
236 icd->num_user_formats = fmts;
238 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
240 /* Second pass - actually fill data formats */
242 for (i = 0; i < raw_fmts; i++)
243 if (!ici->ops->get_formats) {
244 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
245 icd->user_formats[i].host_fmt =
246 soc_mbus_get_fmtdesc(code);
247 icd->user_formats[i].code = code;
249 ret = ici->ops->get_formats(icd, i,
250 &icd->user_formats[fmts]);
256 icd->current_fmt = &icd->user_formats[0];
261 icd->num_user_formats = 0;
262 vfree(icd->user_formats);
266 /* Always entered with .video_lock held */
267 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
269 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
271 if (ici->ops->put_formats)
272 ici->ops->put_formats(icd);
273 icd->current_fmt = NULL;
274 icd->num_user_formats = 0;
275 vfree(icd->user_formats);
276 icd->user_formats = NULL;
279 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
282 /* Called with .vb_lock held, or from the first open(2), see comment there */
283 static int soc_camera_set_fmt(struct soc_camera_file *icf,
284 struct v4l2_format *f)
286 struct soc_camera_device *icd = icf->icd;
287 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
288 struct v4l2_pix_format *pix = &f->fmt.pix;
291 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
292 pixfmtstr(pix->pixelformat), pix->width, pix->height);
294 /* We always call try_fmt() before set_fmt() or set_crop() */
295 ret = ici->ops->try_fmt(icd, f);
299 ret = ici->ops->set_fmt(icd, f);
302 } else if (!icd->current_fmt ||
303 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
305 "Host driver hasn't set up current format correctly!\n");
309 icd->user_width = pix->width;
310 icd->user_height = pix->height;
311 icd->colorspace = pix->colorspace;
313 icd->field = pix->field;
315 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
316 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
319 dev_dbg(&icd->dev, "set width: %d height: %d\n",
320 icd->user_width, icd->user_height);
322 /* set physical bus parameters */
323 return ici->ops->set_bus_param(icd, pix->pixelformat);
326 static int soc_camera_open(struct file *file)
328 struct video_device *vdev = video_devdata(file);
329 struct soc_camera_device *icd = container_of(vdev->parent,
330 struct soc_camera_device,
332 struct soc_camera_link *icl = to_soc_camera_link(icd);
333 struct soc_camera_host *ici;
334 struct soc_camera_file *icf;
338 /* No device driver attached */
341 ici = to_soc_camera_host(icd->dev.parent);
343 icf = vmalloc(sizeof(*icf));
347 if (!try_module_get(ici->ops->owner)) {
348 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
354 * Protect against icd->ops->remove() until we module_get() both
357 mutex_lock(&icd->video_lock);
362 /* Now we really have to activate the camera */
363 if (icd->use_count == 1) {
364 /* Restore parameters before the last close() per V4L2 API */
365 struct v4l2_format f = {
366 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
368 .width = icd->user_width,
369 .height = icd->user_height,
371 .colorspace = icd->colorspace,
373 icd->current_fmt->host_fmt->fourcc,
376 /* ddl@rock-chips.com : accelerate device open */
377 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
379 ret = icl->power(icd->pdev, 1);
384 /* The camera could have been already on, try to reset */
386 icl->reset(icd->pdev);
389 ret = ici->ops->add(icd);
391 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
395 pm_runtime_enable(&icd->vdev->dev);
396 ret = pm_runtime_resume(&icd->vdev->dev);
397 if (ret < 0 && ret != -ENOSYS)
400 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
402 * Try to configure with default parameters. Notice: this is the
403 * very first open, so, we cannot race against other calls,
404 * apart from someone else calling open() simultaneously, but
405 * .video_lock is protecting us against it.
407 ret = soc_camera_set_fmt(icf, &f);
413 file->private_data = icf;
414 dev_dbg(&icd->dev, "camera device open\n");
416 ici->ops->init_videobuf(&icf->vb_vidq, icd);
418 mutex_unlock(&icd->video_lock);
423 * First four errors are entered with the .video_lock held
427 pm_runtime_disable(&icd->vdev->dev);
429 ici->ops->remove(icd);
432 icl->power(icd->pdev, 0);
435 mutex_unlock(&icd->video_lock);
436 module_put(ici->ops->owner);
442 static int soc_camera_close(struct file *file)
444 struct soc_camera_file *icf = file->private_data;
445 struct soc_camera_device *icd = icf->icd;
446 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
448 mutex_lock(&icd->video_lock);
450 if (!icd->use_count) {
451 struct soc_camera_link *icl = to_soc_camera_link(icd);
453 pm_runtime_suspend(&icd->vdev->dev);
454 pm_runtime_disable(&icd->vdev->dev);
456 ici->ops->remove(icd);
458 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
460 icl->power(icd->pdev, 0);
464 mutex_unlock(&icd->video_lock);
466 module_put(ici->ops->owner);
470 dev_dbg(&icd->dev, "camera device close\n");
475 static ssize_t soc_camera_read(struct file *file, char __user *buf,
476 size_t count, loff_t *ppos)
478 struct soc_camera_file *icf = file->private_data;
479 struct soc_camera_device *icd = icf->icd;
482 dev_err(&icd->dev, "camera device read not implemented\n");
487 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
489 struct soc_camera_file *icf = file->private_data;
490 struct soc_camera_device *icd = icf->icd;
493 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
495 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
497 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
498 (unsigned long)vma->vm_start,
499 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
505 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
507 struct soc_camera_file *icf = file->private_data;
508 struct soc_camera_device *icd = icf->icd;
509 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
511 if (list_empty(&icf->vb_vidq.stream)) {
512 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
516 return ici->ops->poll(file, pt);
519 static struct v4l2_file_operations soc_camera_fops = {
520 .owner = THIS_MODULE,
521 .open = soc_camera_open,
522 .release = soc_camera_close,
523 .ioctl = video_ioctl2,
524 .read = soc_camera_read,
525 .mmap = soc_camera_mmap,
526 .poll = soc_camera_poll,
529 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
530 struct v4l2_format *f)
532 struct soc_camera_file *icf = file->private_data;
533 struct soc_camera_device *icd = icf->icd;
536 WARN_ON(priv != file->private_data);
538 mutex_lock(&icf->vb_vidq.vb_lock);
541 if (icf->vb_vidq.bufs[0]) {
542 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
548 /* ddl@rock-chips.com :
549 Judge queue initialised by Judge icf->vb_vidq.bufs[0] whether is NULL , it is error. */
552 while (icf->vb_vidq.bufs[i] && (i<VIDEO_MAX_FRAME)) {
553 if (icf->vb_vidq.bufs[i]->state != VIDEOBUF_NEEDS_INIT) {
554 dev_err(&icd->dev, "S_FMT denied: queue initialised, icf->vb_vidq.bufs[%d]->state:0x%x\n",i,icf->vb_vidq.bufs[i]->state);
563 ret = soc_camera_set_fmt(icf, f);
566 mutex_unlock(&icf->vb_vidq.vb_lock);
571 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
572 struct v4l2_fmtdesc *f)
574 struct soc_camera_file *icf = file->private_data;
575 struct soc_camera_device *icd = icf->icd;
576 const struct soc_mbus_pixelfmt *format;
578 WARN_ON(priv != file->private_data);
580 if (f->index >= icd->num_user_formats)
583 format = icd->user_formats[f->index].host_fmt;
586 strlcpy(f->description, format->name, sizeof(f->description));
587 f->pixelformat = format->fourcc;
591 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
592 struct v4l2_format *f)
594 struct soc_camera_file *icf = file->private_data;
595 struct soc_camera_device *icd = icf->icd;
596 struct v4l2_pix_format *pix = &f->fmt.pix;
598 WARN_ON(priv != file->private_data);
600 pix->width = icd->user_width;
601 pix->height = icd->user_height;
602 pix->field = icf->vb_vidq.field;
603 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
604 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
605 icd->current_fmt->host_fmt);
606 pix->colorspace = icd->colorspace;
607 if (pix->bytesperline < 0)
608 return pix->bytesperline;
609 pix->sizeimage = pix->height * pix->bytesperline;
610 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
611 icd->current_fmt->host_fmt->fourcc);
615 static int soc_camera_querycap(struct file *file, void *priv,
616 struct v4l2_capability *cap)
618 struct soc_camera_file *icf = file->private_data;
619 struct soc_camera_device *icd = icf->icd;
620 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
622 WARN_ON(priv != file->private_data);
624 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
625 return ici->ops->querycap(ici, cap);
628 static int soc_camera_streamon(struct file *file, void *priv,
629 enum v4l2_buf_type i)
631 struct soc_camera_file *icf = file->private_data;
632 struct soc_camera_device *icd = icf->icd;
633 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
634 struct soc_camera_host *ici =
635 to_soc_camera_host(icd->dev.parent);
638 WARN_ON(priv != file->private_data);
640 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
643 mutex_lock(&icd->video_lock);
645 v4l2_subdev_call(sd, video, s_stream, 1);
646 if (ici->ops->s_stream)
647 ici->ops->s_stream(icd, 1); /* ddl@rock-chips.com : Add stream control for host */
648 /* This calls buf_queue from host driver's videobuf_queue_ops */
649 ret = videobuf_streamon(&icf->vb_vidq);
651 mutex_unlock(&icd->video_lock);
656 static int soc_camera_streamoff(struct file *file, void *priv,
657 enum v4l2_buf_type i)
659 struct soc_camera_file *icf = file->private_data;
660 struct soc_camera_device *icd = icf->icd;
661 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
662 struct soc_camera_host *ici =
663 to_soc_camera_host(icd->dev.parent);
665 WARN_ON(priv != file->private_data);
667 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
670 mutex_lock(&icd->video_lock);
673 * This calls buf_release from host driver's videobuf_queue_ops for all
674 * remaining buffers. When the last buffer is freed, stop capture
676 videobuf_streamoff(&icf->vb_vidq);
678 v4l2_subdev_call(sd, video, s_stream, 0);
679 if (ici->ops->s_stream)
680 ici->ops->s_stream(icd, 0); /* ddl@rock-chips.com : Add stream control for host */
682 videobuf_mmap_free(&icf->vb_vidq); /* ddl@rock-chips.com : free video buf */
683 mutex_unlock(&icd->video_lock);
688 static int soc_camera_queryctrl(struct file *file, void *priv,
689 struct v4l2_queryctrl *qc)
691 struct soc_camera_file *icf = file->private_data;
692 struct soc_camera_device *icd = icf->icd;
693 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
696 WARN_ON(priv != file->private_data);
701 /* First check host controls */
702 for (i = 0; i < ici->ops->num_controls; i++)
703 if (qc->id == ici->ops->controls[i].id) {
704 memcpy(qc, &(ici->ops->controls[i]),
709 /* Then device controls */
710 for (i = 0; i < icd->ops->num_controls; i++)
711 if (qc->id == icd->ops->controls[i].id) {
712 memcpy(qc, &(icd->ops->controls[i]),
720 /* ddl@rock-chips.com : Add ioctrl -VIDIOC_QUERYMENU */
721 static int soc_camera_querymenu(struct file *file, void *priv,
722 struct v4l2_querymenu *qm)
724 struct soc_camera_file *icf = file->private_data;
725 struct soc_camera_device *icd = icf->icd;
726 struct v4l2_queryctrl qctrl;
731 if (soc_camera_queryctrl(file,priv, &qctrl) == 0) {
732 for (i = 0; i < icd->ops->num_menus; i++) {
733 if (qm->id == icd->ops->menus[i].id) {
734 for (j=0; j<=(qctrl.maximum - qctrl.minimum); j++) {
736 if (qm->index == icd->ops->menus[i].index) {
737 snprintf(qm->name, sizeof(qm->name), icd->ops->menus[i].name);
743 if ( i >= icd->ops->num_menus)
754 static int soc_camera_g_ctrl(struct file *file, void *priv,
755 struct v4l2_control *ctrl)
757 struct soc_camera_file *icf = file->private_data;
758 struct soc_camera_device *icd = icf->icd;
759 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
760 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
763 WARN_ON(priv != file->private_data);
765 if (ici->ops->get_ctrl) {
766 ret = ici->ops->get_ctrl(icd, ctrl);
767 if (ret != -ENOIOCTLCMD)
771 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
774 static int soc_camera_s_ctrl(struct file *file, void *priv,
775 struct v4l2_control *ctrl)
777 struct soc_camera_file *icf = file->private_data;
778 struct soc_camera_device *icd = icf->icd;
779 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
780 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
783 WARN_ON(priv != file->private_data);
785 if (ici->ops->set_ctrl) {
786 ret = ici->ops->set_ctrl(icd, ctrl);
787 if (ret != -ENOIOCTLCMD)
791 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
795 /* ddl@rock-chips.com : Add ioctrl -VIDIOC_XXX_ext_ctrl for soc-camera */
796 static int soc_camera_try_ext_ctrl(struct file *file, void *priv,
797 struct v4l2_ext_controls *ctrl)
799 struct soc_camera_file *icf = file->private_data;
800 struct soc_camera_device *icd = icf->icd;
801 const struct v4l2_queryctrl *qctrl;
804 WARN_ON(priv != file->private_data);
806 if (ctrl->ctrl_class != V4L2_CTRL_CLASS_CAMERA)
809 for (i=0; i<ctrl->count; i++) {
810 qctrl = soc_camera_find_qctrl(icd->ops, ctrl->controls[i].id);
814 if ((ctrl->controls[i].value < qctrl->minimum) ||(ctrl->controls[i].value > qctrl->minimum))
820 /* ddl@rock-chips.com : Add ioctrl -VIDIOC_XXX_ext_ctrl for soc-camera */
821 static int soc_camera_g_ext_ctrl(struct file *file, void *priv,
822 struct v4l2_ext_controls *ctrl)
824 struct soc_camera_file *icf = file->private_data;
825 struct soc_camera_device *icd = icf->icd;
826 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
828 WARN_ON(priv != file->private_data);
830 if (ctrl->ctrl_class != V4L2_CTRL_CLASS_CAMERA)
833 return v4l2_subdev_call(sd, core, g_ext_ctrls, ctrl);
835 /* ddl@rock-chips.com : Add ioctrl -VIDIOC_XXX_ext_ctrl for soc-camera */
836 static int soc_camera_s_ext_ctrl(struct file *file, void *priv,
837 struct v4l2_ext_controls *ctrl)
839 struct soc_camera_file *icf = file->private_data;
840 struct soc_camera_device *icd = icf->icd;
841 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
843 WARN_ON(priv != file->private_data);
845 if (ctrl->ctrl_class != V4L2_CTRL_CLASS_CAMERA)
848 return v4l2_subdev_call(sd, core, s_ext_ctrls, ctrl);
852 static int soc_camera_cropcap(struct file *file, void *fh,
853 struct v4l2_cropcap *a)
855 struct soc_camera_file *icf = file->private_data;
856 struct soc_camera_device *icd = icf->icd;
857 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
859 return ici->ops->cropcap(icd, a);
862 static int soc_camera_g_crop(struct file *file, void *fh,
865 struct soc_camera_file *icf = file->private_data;
866 struct soc_camera_device *icd = icf->icd;
867 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
870 mutex_lock(&icf->vb_vidq.vb_lock);
871 ret = ici->ops->get_crop(icd, a);
872 mutex_unlock(&icf->vb_vidq.vb_lock);
878 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
879 * argument with the actual geometry, instead, the user shall use G_CROP to
882 static int soc_camera_s_crop(struct file *file, void *fh,
885 struct soc_camera_file *icf = file->private_data;
886 struct soc_camera_device *icd = icf->icd;
887 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
888 struct v4l2_rect *rect = &a->c;
889 struct v4l2_crop current_crop;
892 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
895 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
896 rect->width, rect->height, rect->left, rect->top);
898 /* Cropping is allowed during a running capture, guard consistency */
899 mutex_lock(&icf->vb_vidq.vb_lock);
901 /* If get_crop fails, we'll let host and / or client drivers decide */
902 ret = ici->ops->get_crop(icd, ¤t_crop);
904 /* Prohibit window size change with initialised buffers */
907 "S_CROP denied: getting current crop failed\n");
908 } else if (icf->vb_vidq.bufs[0] &&
909 (a->c.width != current_crop.c.width ||
910 a->c.height != current_crop.c.height)) {
912 "S_CROP denied: queue initialised and sizes differ\n");
915 ret = ici->ops->set_crop(icd, a);
918 mutex_unlock(&icf->vb_vidq.vb_lock);
923 static int soc_camera_g_parm(struct file *file, void *fh,
924 struct v4l2_streamparm *a)
926 struct soc_camera_file *icf = file->private_data;
927 struct soc_camera_device *icd = icf->icd;
928 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
930 if (ici->ops->get_parm)
931 return ici->ops->get_parm(icd, a);
936 static int soc_camera_s_parm(struct file *file, void *fh,
937 struct v4l2_streamparm *a)
939 struct soc_camera_file *icf = file->private_data;
940 struct soc_camera_device *icd = icf->icd;
941 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
943 if (ici->ops->set_parm)
944 return ici->ops->set_parm(icd, a);
949 static int soc_camera_g_chip_ident(struct file *file, void *fh,
950 struct v4l2_dbg_chip_ident *id)
952 struct soc_camera_file *icf = file->private_data;
953 struct soc_camera_device *icd = icf->icd;
954 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
956 return v4l2_subdev_call(sd, core, g_chip_ident, id);
959 #ifdef CONFIG_VIDEO_ADV_DEBUG
960 static int soc_camera_g_register(struct file *file, void *fh,
961 struct v4l2_dbg_register *reg)
963 struct soc_camera_file *icf = file->private_data;
964 struct soc_camera_device *icd = icf->icd;
965 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
967 return v4l2_subdev_call(sd, core, g_register, reg);
970 static int soc_camera_s_register(struct file *file, void *fh,
971 struct v4l2_dbg_register *reg)
973 struct soc_camera_file *icf = file->private_data;
974 struct soc_camera_device *icd = icf->icd;
975 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
977 return v4l2_subdev_call(sd, core, s_register, reg);
981 /* So far this function cannot fail */
982 static void scan_add_host(struct soc_camera_host *ici)
984 struct soc_camera_device *icd;
986 mutex_lock(&list_lock);
988 list_for_each_entry(icd, &devices, list) {
989 if (icd->iface == ici->nr) {
991 icd->dev.parent = ici->v4l2_dev.dev;
992 dev_set_name(&icd->dev, "%u-%u", icd->iface,
994 ret = device_register(&icd->dev);
996 icd->dev.parent = NULL;
998 "Cannot register device: %d\n", ret);
1003 mutex_unlock(&list_lock);
1006 #ifdef CONFIG_I2C_BOARDINFO
1007 static int soc_camera_init_i2c(struct soc_camera_device *icd,
1008 struct soc_camera_link *icl)
1010 struct i2c_client *client;
1011 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1012 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
1013 struct v4l2_subdev *subdev;
1016 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
1017 icl->i2c_adapter_id);
1021 icl->board_info->platform_data = icd;
1023 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1024 icl->module_name, icl->board_info, NULL);
1028 client = subdev->priv;
1030 /* Use to_i2c_client(dev) to recover the i2c client */
1031 dev_set_drvdata(&icd->dev, &client->dev);
1035 i2c_put_adapter(adap);
1040 static void soc_camera_free_i2c(struct soc_camera_device *icd)
1042 struct i2c_client *client =
1043 to_i2c_client(to_soc_camera_control(icd));
1044 dev_set_drvdata(&icd->dev, NULL);
1045 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1046 i2c_unregister_device(client);
1047 i2c_put_adapter(client->adapter);
1050 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
1051 #define soc_camera_free_i2c(icd) do {} while (0)
1054 static int soc_camera_video_start(struct soc_camera_device *icd);
1055 static int video_dev_create(struct soc_camera_device *icd);
1056 /* Called during host-driver probe */
1057 static int soc_camera_probe(struct device *dev)
1059 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1060 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
1061 struct soc_camera_link *icl = to_soc_camera_link(icd);
1062 struct device *control = NULL;
1063 struct v4l2_subdev *sd;
1064 struct v4l2_mbus_framefmt mf;
1067 dev_info(dev, "Probing %s\n", dev_name(dev));
1070 ret = icl->power(icd->pdev, 1);
1073 "Platform failed to power-on the camera.\n");
1078 /* The camera could have been already on, try to reset */
1080 icl->reset(icd->pdev);
1082 ret = ici->ops->add(icd);
1086 /* Must have icd->vdev before registering the device */
1087 ret = video_dev_create(icd);
1091 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1092 if (icl->board_info) {
1093 ret = soc_camera_init_i2c(icd, icl);
1096 } else if (!icl->add_device || !icl->del_device) {
1100 if (icl->module_name)
1101 ret = request_module(icl->module_name);
1103 ret = icl->add_device(icl, &icd->dev);
1108 * FIXME: this is racy, have to use driver-binding notification,
1109 * when it is available
1111 control = to_soc_camera_control(icd);
1112 if (!control || !control->driver || !dev_get_drvdata(control) ||
1113 !try_module_get(control->driver->owner)) {
1114 icl->del_device(icl);
1119 /* At this point client .probe() should have run already */
1120 ret = soc_camera_init_user_formats(icd);
1124 icd->field = V4L2_FIELD_ANY;
1126 /* ..._video_start() will create a device node, so we have to protect */
1127 mutex_lock(&icd->video_lock);
1129 ret = soc_camera_video_start(icd);
1133 /* Try to improve our guess of a reasonable window format */
1134 sd = soc_camera_to_subdev(icd);
1135 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1136 icd->user_width = mf.width;
1137 icd->user_height = mf.height;
1138 icd->colorspace = mf.colorspace;
1139 icd->field = mf.field;
1142 /* Do we have to sysfs_remove_link() before device_unregister()? */
1143 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
1145 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1147 ici->ops->remove(icd);
1150 icl->power(icd->pdev, 0);
1152 mutex_unlock(&icd->video_lock);
1157 mutex_unlock(&icd->video_lock);
1158 soc_camera_free_user_formats(icd);
1160 if (icl->board_info) {
1161 soc_camera_free_i2c(icd);
1163 icl->del_device(icl);
1164 module_put(control->driver->owner);
1168 video_device_release(icd->vdev);
1170 ici->ops->remove(icd);
1173 icl->power(icd->pdev, 0);
1179 * This is called on device_unregister, which only means we have to disconnect
1180 * from the host, but not remove ourselves from the device list
1182 static int soc_camera_remove(struct device *dev)
1184 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1185 struct soc_camera_link *icl = to_soc_camera_link(icd);
1186 struct video_device *vdev = icd->vdev;
1188 BUG_ON(!dev->parent);
1191 mutex_lock(&icd->video_lock);
1192 video_unregister_device(vdev);
1194 mutex_unlock(&icd->video_lock);
1197 if (icl->board_info) {
1198 soc_camera_free_i2c(icd);
1200 struct device_driver *drv = to_soc_camera_control(icd) ?
1201 to_soc_camera_control(icd)->driver : NULL;
1203 icl->del_device(icl);
1204 module_put(drv->owner);
1207 soc_camera_free_user_formats(icd);
1212 static int soc_camera_suspend(struct device *dev, pm_message_t state)
1214 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1215 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1218 if (ici->ops->suspend)
1219 ret = ici->ops->suspend(icd, state);
1224 static int soc_camera_resume(struct device *dev)
1226 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1227 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1230 if (ici->ops->resume)
1231 ret = ici->ops->resume(icd);
1236 struct bus_type soc_camera_bus_type = {
1237 .name = "soc-camera",
1238 .probe = soc_camera_probe,
1239 .remove = soc_camera_remove,
1240 .suspend = soc_camera_suspend,
1241 .resume = soc_camera_resume,
1243 EXPORT_SYMBOL_GPL(soc_camera_bus_type);
1245 static struct device_driver ic_drv = {
1247 .bus = &soc_camera_bus_type,
1248 .owner = THIS_MODULE,
1251 static void dummy_release(struct device *dev)
1255 static int default_cropcap(struct soc_camera_device *icd,
1256 struct v4l2_cropcap *a)
1258 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1259 return v4l2_subdev_call(sd, video, cropcap, a);
1262 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1264 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1265 return v4l2_subdev_call(sd, video, g_crop, a);
1268 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1270 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1271 return v4l2_subdev_call(sd, video, s_crop, a);
1274 static void soc_camera_device_init(struct device *dev, void *pdata)
1276 dev->platform_data = pdata;
1277 dev->bus = &soc_camera_bus_type;
1278 dev->release = dummy_release;
1281 int soc_camera_host_register(struct soc_camera_host *ici)
1283 struct soc_camera_host *ix;
1286 if (!ici || !ici->ops ||
1287 !ici->ops->try_fmt ||
1288 !ici->ops->set_fmt ||
1289 !ici->ops->set_bus_param ||
1290 !ici->ops->querycap ||
1291 !ici->ops->init_videobuf ||
1292 !ici->ops->reqbufs ||
1294 !ici->ops->remove ||
1299 if (!ici->ops->set_crop)
1300 ici->ops->set_crop = default_s_crop;
1301 if (!ici->ops->get_crop)
1302 ici->ops->get_crop = default_g_crop;
1303 if (!ici->ops->cropcap)
1304 ici->ops->cropcap = default_cropcap;
1306 mutex_lock(&list_lock);
1307 list_for_each_entry(ix, &hosts, list) {
1308 if (ix->nr == ici->nr) {
1314 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1318 list_add_tail(&ici->list, &hosts);
1319 mutex_unlock(&list_lock);
1326 mutex_unlock(&list_lock);
1329 EXPORT_SYMBOL(soc_camera_host_register);
1331 /* Unregister all clients! */
1332 void soc_camera_host_unregister(struct soc_camera_host *ici)
1334 struct soc_camera_device *icd;
1336 mutex_lock(&list_lock);
1338 list_del(&ici->list);
1340 list_for_each_entry(icd, &devices, list) {
1341 if (icd->iface == ici->nr) {
1342 void *pdata = icd->dev.platform_data;
1343 /* The bus->remove will be called */
1344 device_unregister(&icd->dev);
1346 * Not before device_unregister(), .remove
1347 * needs parent to call ici->ops->remove().
1348 * If the host module is loaded again, device_register()
1349 * would complain "already initialised," since 2.6.32
1350 * this is also needed to prevent use-after-free of the
1351 * device private data.
1353 memset(&icd->dev, 0, sizeof(icd->dev));
1354 soc_camera_device_init(&icd->dev, pdata);
1358 mutex_unlock(&list_lock);
1360 v4l2_device_unregister(&ici->v4l2_dev);
1362 EXPORT_SYMBOL(soc_camera_host_unregister);
1364 /* Image capture device */
1365 static int soc_camera_device_register(struct soc_camera_device *icd)
1367 struct soc_camera_device *ix;
1370 for (i = 0; i < 256 && num < 0; i++) {
1372 /* Check if this index is available on this interface */
1373 list_for_each_entry(ix, &devices, list) {
1374 if (ix->iface == icd->iface && ix->devnum == i) {
1383 * ok, we have 256 cameras on this host...
1384 * man, stay reasonable...
1390 icd->host_priv = NULL;
1391 mutex_init(&icd->video_lock);
1393 list_add_tail(&icd->list, &devices);
1398 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1400 list_del(&icd->list);
1403 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1404 .vidioc_querycap = soc_camera_querycap,
1405 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1406 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1407 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1408 .vidioc_enum_input = soc_camera_enum_input,
1409 .vidioc_g_input = soc_camera_g_input,
1410 .vidioc_s_input = soc_camera_s_input,
1411 .vidioc_s_std = soc_camera_s_std,
1412 .vidioc_reqbufs = soc_camera_reqbufs,
1413 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1414 .vidioc_querybuf = soc_camera_querybuf,
1415 .vidioc_qbuf = soc_camera_qbuf,
1416 .vidioc_dqbuf = soc_camera_dqbuf,
1417 .vidioc_streamon = soc_camera_streamon,
1418 .vidioc_streamoff = soc_camera_streamoff,
1419 .vidioc_queryctrl = soc_camera_queryctrl,
1420 .vidioc_querymenu = soc_camera_querymenu, /* ddl@rock-chips.com: Add ioctrl - vidioc_querymenu for soc-camera */
1421 .vidioc_g_ctrl = soc_camera_g_ctrl,
1422 .vidioc_s_ctrl = soc_camera_s_ctrl,
1423 .vidioc_g_ext_ctrls = soc_camera_g_ext_ctrl, /* ddl@rock-chips.com: Add ioctrl - vidioc_g_ext_ctrls for soc-camera */
1424 .vidioc_s_ext_ctrls = soc_camera_s_ext_ctrl, /* ddl@rock-chips.com: Add ioctrl - vidioc_s_ext_ctrls for soc-camera */
1425 .vidioc_try_ext_ctrls = soc_camera_try_ext_ctrl, /* ddl@rock-chips.com: Add ioctrl - vidioc_try_ext_ctrls for soc-camera */
1427 .vidioc_cropcap = soc_camera_cropcap,
1428 .vidioc_g_crop = soc_camera_g_crop,
1429 .vidioc_s_crop = soc_camera_s_crop,
1430 .vidioc_g_parm = soc_camera_g_parm,
1431 .vidioc_s_parm = soc_camera_s_parm,
1432 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1433 #ifdef CONFIG_VIDEO_ADV_DEBUG
1434 .vidioc_g_register = soc_camera_g_register,
1435 .vidioc_s_register = soc_camera_s_register,
1439 static int video_dev_create(struct soc_camera_device *icd)
1441 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1442 struct video_device *vdev = video_device_alloc();
1447 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1449 vdev->parent = &icd->dev;
1450 vdev->current_norm = V4L2_STD_UNKNOWN;
1451 vdev->fops = &soc_camera_fops;
1452 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1453 vdev->release = video_device_release;
1454 vdev->tvnorms = V4L2_STD_UNKNOWN;
1462 * Called from soc_camera_probe() above (with .video_lock held???)
1464 static int soc_camera_video_start(struct soc_camera_device *icd)
1466 struct device_type *type = icd->vdev->dev.type;
1469 if (!icd->dev.parent)
1473 !icd->ops->query_bus_param ||
1474 !icd->ops->set_bus_param)
1477 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1479 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1483 /* Restore device type, possibly set by the subdevice driver */
1484 icd->vdev->dev.type = type;
1489 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1491 struct soc_camera_link *icl = pdev->dev.platform_data;
1492 struct soc_camera_device *icd;
1498 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1502 icd->iface = icl->bus_id;
1503 icd->pdev = &pdev->dev;
1504 platform_set_drvdata(pdev, icd);
1506 ret = soc_camera_device_register(icd);
1510 soc_camera_device_init(&icd->dev, icl);
1512 icd->user_width = DEFAULT_WIDTH;
1513 icd->user_height = DEFAULT_HEIGHT;
1524 * Only called on rmmod for each platform device, since they are not
1525 * hot-pluggable. Now we know, that all our users - hosts and devices have
1526 * been unloaded already
1528 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1530 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1535 soc_camera_device_unregister(icd);
1542 static struct platform_driver __refdata soc_camera_pdrv = {
1543 .remove = __devexit_p(soc_camera_pdrv_remove),
1545 .name = "soc-camera-pdrv",
1546 .owner = THIS_MODULE,
1550 static int __init soc_camera_init(void)
1552 int ret = bus_register(&soc_camera_bus_type);
1555 ret = driver_register(&ic_drv);
1559 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1566 driver_unregister(&ic_drv);
1568 bus_unregister(&soc_camera_bus_type);
1572 static void __exit soc_camera_exit(void)
1574 platform_driver_unregister(&soc_camera_pdrv);
1575 driver_unregister(&ic_drv);
1576 bus_unregister(&soc_camera_bus_type);
1579 module_init(soc_camera_init);
1580 module_exit(soc_camera_exit);
1582 MODULE_DESCRIPTION("Image capture bus driver");
1583 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1584 MODULE_LICENSE("GPL");
1585 MODULE_ALIAS("platform:soc-camera-pdrv");