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/regulator/consumer.h>
28 #include <linux/slab.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/vmalloc.h>
32 #include <media/soc_camera.h>
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-dev.h>
36 #include <media/videobuf-core.h>
37 #include <media/videobuf2-core.h>
38 #include <media/soc_mediabus.h>
40 /* Default to VGA resolution */
41 #define DEFAULT_WIDTH 640
42 #define DEFAULT_HEIGHT 480
44 #define is_streaming(ici, icd) \
45 (((ici)->ops->init_videobuf) ? \
46 (icd)->vb_vidq.streaming : \
47 vb2_is_streaming(&(icd)->vb2_vidq))
49 static LIST_HEAD(hosts);
50 static LIST_HEAD(devices);
51 static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
53 static int soc_camera_power_set(struct soc_camera_device *icd,
54 struct soc_camera_link *icl,
60 ret = regulator_bulk_enable(icl->num_regulators,
63 dev_err(icd->pdev, "Cannot enable regulators\n");
68 ret = icl->power(icd->pdev, power_on);
71 "Platform failed to power-on the camera.\n");
73 regulator_bulk_disable(icl->num_regulators,
80 ret = icl->power(icd->pdev, 0);
83 "Platform failed to power-off the camera.\n");
87 ret = regulator_bulk_disable(icl->num_regulators,
90 dev_err(icd->pdev, "Cannot disable regulators\n");
98 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
99 struct soc_camera_device *icd, unsigned int fourcc)
103 for (i = 0; i < icd->num_user_formats; i++)
104 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
105 return icd->user_formats + i;
108 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
111 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
112 * @icl: camera platform parameters
113 * @cfg: media bus configuration
114 * @return: resulting flags
116 unsigned long soc_camera_apply_board_flags(struct soc_camera_link *icl,
117 const struct v4l2_mbus_config *cfg)
119 unsigned long f, flags = cfg->flags;
121 /* If only one of the two polarities is supported, switch to the opposite */
122 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
123 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
124 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
125 flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
128 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
129 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
130 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
131 flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
134 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
135 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
136 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
137 flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
142 EXPORT_SYMBOL(soc_camera_apply_board_flags);
145 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
146 * @icl: camera platform parameters
147 * @flags: flags to be inverted according to platform configuration
148 * @return: resulting flags
150 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
155 /* If only one of the two polarities is supported, switch to the opposite */
156 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
157 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
158 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
159 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
162 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
163 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
164 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
165 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
168 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
169 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
170 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
171 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
176 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
178 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
181 static int soc_camera_try_fmt(struct soc_camera_device *icd,
182 struct v4l2_format *f)
184 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
185 struct v4l2_pix_format *pix = &f->fmt.pix;
188 dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
189 pixfmtstr(pix->pixelformat), pix->width, pix->height);
191 pix->bytesperline = 0;
194 ret = ici->ops->try_fmt(icd, f);
198 if (!pix->sizeimage) {
199 if (!pix->bytesperline) {
200 const struct soc_camera_format_xlate *xlate;
202 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
206 ret = soc_mbus_bytes_per_line(pix->width,
209 pix->bytesperline = ret;
211 if (pix->bytesperline)
212 pix->sizeimage = pix->bytesperline * pix->height;
218 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
219 struct v4l2_format *f)
221 struct soc_camera_device *icd = file->private_data;
223 WARN_ON(priv != file->private_data);
225 /* Only single-plane capture is supported so far */
226 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
229 /* limit format to hardware capabilities */
230 return soc_camera_try_fmt(icd, f);
233 static int soc_camera_enum_input(struct file *file, void *priv,
234 struct v4l2_input *inp)
239 /* default is camera */
240 inp->type = V4L2_INPUT_TYPE_CAMERA;
241 inp->std = V4L2_STD_UNKNOWN;
242 strcpy(inp->name, "Camera");
247 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
254 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
262 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
264 struct soc_camera_device *icd = file->private_data;
265 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
267 return v4l2_subdev_call(sd, core, s_std, *a);
270 static int soc_camera_enum_fsizes(struct file *file, void *fh,
271 struct v4l2_frmsizeenum *fsize)
273 struct soc_camera_device *icd = file->private_data;
274 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
276 return ici->ops->enum_fsizes(icd, fsize);
279 static int soc_camera_reqbufs(struct file *file, void *priv,
280 struct v4l2_requestbuffers *p)
283 struct soc_camera_device *icd = file->private_data;
284 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
286 WARN_ON(priv != file->private_data);
288 if (icd->streamer && icd->streamer != file)
291 if (ici->ops->init_videobuf) {
292 ret = videobuf_reqbufs(&icd->vb_vidq, p);
296 ret = ici->ops->reqbufs(icd, p);
298 ret = vb2_reqbufs(&icd->vb2_vidq, p);
301 if (!ret && !icd->streamer)
302 icd->streamer = file;
307 static int soc_camera_querybuf(struct file *file, void *priv,
308 struct v4l2_buffer *p)
310 struct soc_camera_device *icd = file->private_data;
311 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
313 WARN_ON(priv != file->private_data);
315 if (ici->ops->init_videobuf)
316 return videobuf_querybuf(&icd->vb_vidq, p);
318 return vb2_querybuf(&icd->vb2_vidq, p);
321 static int soc_camera_qbuf(struct file *file, void *priv,
322 struct v4l2_buffer *p)
324 struct soc_camera_device *icd = file->private_data;
325 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
327 WARN_ON(priv != file->private_data);
329 if (icd->streamer != file)
332 if (ici->ops->init_videobuf)
333 return videobuf_qbuf(&icd->vb_vidq, p);
335 return vb2_qbuf(&icd->vb2_vidq, p);
338 static int soc_camera_dqbuf(struct file *file, void *priv,
339 struct v4l2_buffer *p)
341 struct soc_camera_device *icd = file->private_data;
342 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
344 WARN_ON(priv != file->private_data);
346 if (icd->streamer != file)
349 if (ici->ops->init_videobuf)
350 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
352 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
355 /* Always entered with .video_lock held */
356 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
358 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
359 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
360 unsigned int i, fmts = 0, raw_fmts = 0;
362 enum v4l2_mbus_pixelcode code;
364 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
367 if (!ici->ops->get_formats)
369 * Fallback mode - the host will have to serve all
370 * sensor-provided formats one-to-one to the user
375 * First pass - only count formats this host-sensor
376 * configuration can provide
378 for (i = 0; i < raw_fmts; i++) {
379 ret = ici->ops->get_formats(icd, i, NULL);
389 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
390 if (!icd->user_formats)
393 dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
395 /* Second pass - actually fill data formats */
397 for (i = 0; i < raw_fmts; i++)
398 if (!ici->ops->get_formats) {
399 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
400 icd->user_formats[fmts].host_fmt =
401 soc_mbus_get_fmtdesc(code);
402 if (icd->user_formats[fmts].host_fmt)
403 icd->user_formats[fmts++].code = code;
405 ret = ici->ops->get_formats(icd, i,
406 &icd->user_formats[fmts]);
412 icd->num_user_formats = fmts;
413 icd->current_fmt = &icd->user_formats[0];
418 vfree(icd->user_formats);
422 /* Always entered with .video_lock held */
423 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
425 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
427 if (ici->ops->put_formats)
428 ici->ops->put_formats(icd);
429 icd->current_fmt = NULL;
430 icd->num_user_formats = 0;
431 vfree(icd->user_formats);
432 icd->user_formats = NULL;
435 /* Called with .vb_lock held, or from the first open(2), see comment there */
436 static int soc_camera_set_fmt(struct soc_camera_device *icd,
437 struct v4l2_format *f)
439 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
440 struct v4l2_pix_format *pix = &f->fmt.pix;
443 dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
444 pixfmtstr(pix->pixelformat), pix->width, pix->height);
446 /* We always call try_fmt() before set_fmt() or set_crop() */
447 ret = soc_camera_try_fmt(icd, f);
451 ret = ici->ops->set_fmt(icd, f);
454 } else if (!icd->current_fmt ||
455 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
457 "Host driver hasn't set up current format correctly!\n");
461 icd->user_width = pix->width;
462 icd->user_height = pix->height;
463 icd->bytesperline = pix->bytesperline;
464 icd->sizeimage = pix->sizeimage;
465 icd->colorspace = pix->colorspace;
466 icd->field = pix->field;
467 if (ici->ops->init_videobuf)
468 icd->vb_vidq.field = pix->field;
470 dev_dbg(icd->pdev, "set width: %d height: %d\n",
471 icd->user_width, icd->user_height);
473 /* set physical bus parameters */
474 return ici->ops->set_bus_param(icd, pix->pixelformat);
477 static int soc_camera_open(struct file *file)
479 struct video_device *vdev = video_devdata(file);
480 struct soc_camera_device *icd = dev_get_drvdata(vdev->parent);
481 struct soc_camera_link *icl = to_soc_camera_link(icd);
482 struct soc_camera_host *ici;
485 if (!to_soc_camera_control(icd))
486 /* No device driver attached */
489 ici = to_soc_camera_host(icd->parent);
491 if (!try_module_get(ici->ops->owner)) {
492 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
498 /* Now we really have to activate the camera */
499 if (icd->use_count == 1) {
500 /* Restore parameters before the last close() per V4L2 API */
501 struct v4l2_format f = {
502 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
504 .width = icd->user_width,
505 .height = icd->user_height,
507 .colorspace = icd->colorspace,
509 icd->current_fmt->host_fmt->fourcc,
513 ret = soc_camera_power_set(icd, icl, 1);
517 /* The camera could have been already on, try to reset */
519 icl->reset(icd->pdev);
521 ret = ici->ops->add(icd);
523 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
527 pm_runtime_enable(&icd->vdev->dev);
528 ret = pm_runtime_resume(&icd->vdev->dev);
529 if (ret < 0 && ret != -ENOSYS)
533 * Try to configure with default parameters. Notice: this is the
534 * very first open, so, we cannot race against other calls,
535 * apart from someone else calling open() simultaneously, but
536 * .video_lock is protecting us against it.
538 ret = soc_camera_set_fmt(icd, &f);
542 if (ici->ops->init_videobuf) {
543 ici->ops->init_videobuf(&icd->vb_vidq, icd);
545 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
551 file->private_data = icd;
552 dev_dbg(icd->pdev, "camera device open\n");
557 * First four errors are entered with the .video_lock held
562 pm_runtime_disable(&icd->vdev->dev);
564 ici->ops->remove(icd);
566 soc_camera_power_set(icd, icl, 0);
569 module_put(ici->ops->owner);
574 static int soc_camera_close(struct file *file)
576 struct soc_camera_device *icd = file->private_data;
577 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
580 if (!icd->use_count) {
581 struct soc_camera_link *icl = to_soc_camera_link(icd);
583 pm_runtime_suspend(&icd->vdev->dev);
584 pm_runtime_disable(&icd->vdev->dev);
586 ici->ops->remove(icd);
587 if (ici->ops->init_videobuf2)
588 vb2_queue_release(&icd->vb2_vidq);
590 soc_camera_power_set(icd, icl, 0);
593 if (icd->streamer == file)
594 icd->streamer = NULL;
596 module_put(ici->ops->owner);
598 dev_dbg(icd->pdev, "camera device close\n");
603 static ssize_t soc_camera_read(struct file *file, char __user *buf,
604 size_t count, loff_t *ppos)
606 struct soc_camera_device *icd = file->private_data;
609 dev_err(icd->pdev, "camera device read not implemented\n");
614 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
616 struct soc_camera_device *icd = file->private_data;
617 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
620 dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
622 if (icd->streamer != file)
625 if (ici->ops->init_videobuf)
626 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
628 err = vb2_mmap(&icd->vb2_vidq, vma);
630 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
631 (unsigned long)vma->vm_start,
632 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
638 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
640 struct soc_camera_device *icd = file->private_data;
641 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
643 if (icd->streamer != file)
646 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
647 dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
651 return ici->ops->poll(file, pt);
654 void soc_camera_lock(struct vb2_queue *vq)
656 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
657 mutex_lock(&icd->video_lock);
659 EXPORT_SYMBOL(soc_camera_lock);
661 void soc_camera_unlock(struct vb2_queue *vq)
663 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
664 mutex_unlock(&icd->video_lock);
666 EXPORT_SYMBOL(soc_camera_unlock);
668 static struct v4l2_file_operations soc_camera_fops = {
669 .owner = THIS_MODULE,
670 .open = soc_camera_open,
671 .release = soc_camera_close,
672 .unlocked_ioctl = video_ioctl2,
673 .read = soc_camera_read,
674 .mmap = soc_camera_mmap,
675 .poll = soc_camera_poll,
678 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
679 struct v4l2_format *f)
681 struct soc_camera_device *icd = file->private_data;
684 WARN_ON(priv != file->private_data);
686 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
687 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
691 if (icd->streamer && icd->streamer != file)
694 if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
695 dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
699 ret = soc_camera_set_fmt(icd, f);
701 if (!ret && !icd->streamer)
702 icd->streamer = file;
707 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
708 struct v4l2_fmtdesc *f)
710 struct soc_camera_device *icd = file->private_data;
711 const struct soc_mbus_pixelfmt *format;
713 WARN_ON(priv != file->private_data);
715 if (f->index >= icd->num_user_formats)
718 format = icd->user_formats[f->index].host_fmt;
721 strlcpy(f->description, format->name, sizeof(f->description));
722 f->pixelformat = format->fourcc;
726 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
727 struct v4l2_format *f)
729 struct soc_camera_device *icd = file->private_data;
730 struct v4l2_pix_format *pix = &f->fmt.pix;
732 WARN_ON(priv != file->private_data);
734 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
737 pix->width = icd->user_width;
738 pix->height = icd->user_height;
739 pix->bytesperline = icd->bytesperline;
740 pix->sizeimage = icd->sizeimage;
741 pix->field = icd->field;
742 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
743 pix->colorspace = icd->colorspace;
744 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
745 icd->current_fmt->host_fmt->fourcc);
749 static int soc_camera_querycap(struct file *file, void *priv,
750 struct v4l2_capability *cap)
752 struct soc_camera_device *icd = file->private_data;
753 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
755 WARN_ON(priv != file->private_data);
757 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
758 return ici->ops->querycap(ici, cap);
761 static int soc_camera_streamon(struct file *file, void *priv,
762 enum v4l2_buf_type i)
764 struct soc_camera_device *icd = file->private_data;
765 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
766 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
769 WARN_ON(priv != file->private_data);
771 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
774 if (icd->streamer != file)
777 /* This calls buf_queue from host driver's videobuf_queue_ops */
778 if (ici->ops->init_videobuf)
779 ret = videobuf_streamon(&icd->vb_vidq);
781 ret = vb2_streamon(&icd->vb2_vidq, i);
784 v4l2_subdev_call(sd, video, s_stream, 1);
789 static int soc_camera_streamoff(struct file *file, void *priv,
790 enum v4l2_buf_type i)
792 struct soc_camera_device *icd = file->private_data;
793 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
794 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
796 WARN_ON(priv != file->private_data);
798 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
801 if (icd->streamer != file)
805 * This calls buf_release from host driver's videobuf_queue_ops for all
806 * remaining buffers. When the last buffer is freed, stop capture
808 if (ici->ops->init_videobuf)
809 videobuf_streamoff(&icd->vb_vidq);
811 vb2_streamoff(&icd->vb2_vidq, i);
813 v4l2_subdev_call(sd, video, s_stream, 0);
818 static int soc_camera_queryctrl(struct file *file, void *priv,
819 struct v4l2_queryctrl *qc)
821 struct soc_camera_device *icd = file->private_data;
822 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
825 WARN_ON(priv != file->private_data);
830 /* First check host controls */
831 for (i = 0; i < ici->ops->num_controls; i++)
832 if (qc->id == ici->ops->controls[i].id) {
833 memcpy(qc, &(ici->ops->controls[i]),
841 /* Then device controls */
842 for (i = 0; i < icd->ops->num_controls; i++)
843 if (qc->id == icd->ops->controls[i].id) {
844 memcpy(qc, &(icd->ops->controls[i]),
852 static int soc_camera_g_ctrl(struct file *file, void *priv,
853 struct v4l2_control *ctrl)
855 struct soc_camera_device *icd = file->private_data;
856 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
857 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
860 WARN_ON(priv != file->private_data);
862 if (ici->ops->get_ctrl) {
863 ret = ici->ops->get_ctrl(icd, ctrl);
864 if (ret != -ENOIOCTLCMD)
868 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
871 static int soc_camera_s_ctrl(struct file *file, void *priv,
872 struct v4l2_control *ctrl)
874 struct soc_camera_device *icd = file->private_data;
875 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
876 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
879 WARN_ON(priv != file->private_data);
881 if (ici->ops->set_ctrl) {
882 ret = ici->ops->set_ctrl(icd, ctrl);
883 if (ret != -ENOIOCTLCMD)
887 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
890 static int soc_camera_cropcap(struct file *file, void *fh,
891 struct v4l2_cropcap *a)
893 struct soc_camera_device *icd = file->private_data;
894 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
896 return ici->ops->cropcap(icd, a);
899 static int soc_camera_g_crop(struct file *file, void *fh,
902 struct soc_camera_device *icd = file->private_data;
903 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
906 ret = ici->ops->get_crop(icd, a);
912 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
913 * argument with the actual geometry, instead, the user shall use G_CROP to
916 static int soc_camera_s_crop(struct file *file, void *fh,
919 struct soc_camera_device *icd = file->private_data;
920 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
921 struct v4l2_rect *rect = &a->c;
922 struct v4l2_crop current_crop;
925 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
928 dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
929 rect->width, rect->height, rect->left, rect->top);
931 /* If get_crop fails, we'll let host and / or client drivers decide */
932 ret = ici->ops->get_crop(icd, ¤t_crop);
934 /* Prohibit window size change with initialised buffers */
937 "S_CROP denied: getting current crop failed\n");
938 } else if ((a->c.width == current_crop.c.width &&
939 a->c.height == current_crop.c.height) ||
940 !is_streaming(ici, icd)) {
941 /* same size or not streaming - use .set_crop() */
942 ret = ici->ops->set_crop(icd, a);
943 } else if (ici->ops->set_livecrop) {
944 ret = ici->ops->set_livecrop(icd, a);
947 "S_CROP denied: queue initialised and sizes differ\n");
954 static int soc_camera_g_parm(struct file *file, void *fh,
955 struct v4l2_streamparm *a)
957 struct soc_camera_device *icd = file->private_data;
958 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
960 if (ici->ops->get_parm)
961 return ici->ops->get_parm(icd, a);
966 static int soc_camera_s_parm(struct file *file, void *fh,
967 struct v4l2_streamparm *a)
969 struct soc_camera_device *icd = file->private_data;
970 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
972 if (ici->ops->set_parm)
973 return ici->ops->set_parm(icd, a);
978 static int soc_camera_g_chip_ident(struct file *file, void *fh,
979 struct v4l2_dbg_chip_ident *id)
981 struct soc_camera_device *icd = file->private_data;
982 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
984 return v4l2_subdev_call(sd, core, g_chip_ident, id);
987 #ifdef CONFIG_VIDEO_ADV_DEBUG
988 static int soc_camera_g_register(struct file *file, void *fh,
989 struct v4l2_dbg_register *reg)
991 struct soc_camera_device *icd = file->private_data;
992 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
994 return v4l2_subdev_call(sd, core, g_register, reg);
997 static int soc_camera_s_register(struct file *file, void *fh,
998 struct v4l2_dbg_register *reg)
1000 struct soc_camera_device *icd = file->private_data;
1001 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1003 return v4l2_subdev_call(sd, core, s_register, reg);
1007 static int soc_camera_probe(struct soc_camera_device *icd);
1009 /* So far this function cannot fail */
1010 static void scan_add_host(struct soc_camera_host *ici)
1012 struct soc_camera_device *icd;
1014 mutex_lock(&list_lock);
1016 list_for_each_entry(icd, &devices, list) {
1017 if (icd->iface == ici->nr) {
1020 icd->parent = ici->v4l2_dev.dev;
1021 ret = soc_camera_probe(icd);
1025 mutex_unlock(&list_lock);
1028 #ifdef CONFIG_I2C_BOARDINFO
1029 static int soc_camera_init_i2c(struct soc_camera_device *icd,
1030 struct soc_camera_link *icl)
1032 struct i2c_client *client;
1033 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1034 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
1035 struct v4l2_subdev *subdev;
1038 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1039 icl->i2c_adapter_id);
1043 icl->board_info->platform_data = icd;
1045 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1046 icl->board_info, NULL);
1050 client = v4l2_get_subdevdata(subdev);
1052 /* Use to_i2c_client(dev) to recover the i2c client */
1053 icd->control = &client->dev;
1057 i2c_put_adapter(adap);
1062 static void soc_camera_free_i2c(struct soc_camera_device *icd)
1064 struct i2c_client *client =
1065 to_i2c_client(to_soc_camera_control(icd));
1066 struct i2c_adapter *adap = client->adapter;
1068 icd->control = NULL;
1069 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1070 i2c_unregister_device(client);
1071 i2c_put_adapter(adap);
1074 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
1075 #define soc_camera_free_i2c(icd) do {} while (0)
1078 static int soc_camera_video_start(struct soc_camera_device *icd);
1079 static int video_dev_create(struct soc_camera_device *icd);
1080 /* Called during host-driver probe */
1081 static int soc_camera_probe(struct soc_camera_device *icd)
1083 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1084 struct soc_camera_link *icl = to_soc_camera_link(icd);
1085 struct device *control = NULL;
1086 struct v4l2_subdev *sd;
1087 struct v4l2_mbus_framefmt mf;
1090 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1092 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
1097 ret = soc_camera_power_set(icd, icl, 1);
1101 /* The camera could have been already on, try to reset */
1103 icl->reset(icd->pdev);
1105 ret = ici->ops->add(icd);
1109 /* Must have icd->vdev before registering the device */
1110 ret = video_dev_create(icd);
1114 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1115 if (icl->board_info) {
1116 ret = soc_camera_init_i2c(icd, icl);
1119 } else if (!icl->add_device || !icl->del_device) {
1123 if (icl->module_name)
1124 ret = request_module(icl->module_name);
1126 ret = icl->add_device(icd);
1131 * FIXME: this is racy, have to use driver-binding notification,
1132 * when it is available
1134 control = to_soc_camera_control(icd);
1135 if (!control || !control->driver || !dev_get_drvdata(control) ||
1136 !try_module_get(control->driver->owner)) {
1137 icl->del_device(icd);
1142 sd = soc_camera_to_subdev(icd);
1143 sd->grp_id = (long)icd;
1145 /* At this point client .probe() should have run already */
1146 ret = soc_camera_init_user_formats(icd);
1150 icd->field = V4L2_FIELD_ANY;
1153 * ..._video_start() will create a device node, video_register_device()
1154 * itself is protected against concurrent open() calls, but we also have
1155 * to protect our data.
1157 mutex_lock(&icd->video_lock);
1159 ret = soc_camera_video_start(icd);
1163 /* Try to improve our guess of a reasonable window format */
1164 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1165 icd->user_width = mf.width;
1166 icd->user_height = mf.height;
1167 icd->colorspace = mf.colorspace;
1168 icd->field = mf.field;
1171 ici->ops->remove(icd);
1173 soc_camera_power_set(icd, icl, 0);
1175 mutex_unlock(&icd->video_lock);
1180 mutex_unlock(&icd->video_lock);
1181 soc_camera_free_user_formats(icd);
1183 if (icl->board_info) {
1184 soc_camera_free_i2c(icd);
1186 icl->del_device(icd);
1187 module_put(control->driver->owner);
1191 video_device_release(icd->vdev);
1193 ici->ops->remove(icd);
1195 soc_camera_power_set(icd, icl, 0);
1197 regulator_bulk_free(icl->num_regulators, icl->regulators);
1203 * This is called on device_unregister, which only means we have to disconnect
1204 * from the host, but not remove ourselves from the device list
1206 static int soc_camera_remove(struct soc_camera_device *icd)
1208 struct soc_camera_link *icl = to_soc_camera_link(icd);
1209 struct video_device *vdev = icd->vdev;
1211 BUG_ON(!icd->parent);
1214 video_unregister_device(vdev);
1218 if (icl->board_info) {
1219 soc_camera_free_i2c(icd);
1221 struct device_driver *drv = to_soc_camera_control(icd)->driver;
1223 icl->del_device(icd);
1224 module_put(drv->owner);
1227 soc_camera_free_user_formats(icd);
1229 regulator_bulk_free(icl->num_regulators, icl->regulators);
1234 static int default_cropcap(struct soc_camera_device *icd,
1235 struct v4l2_cropcap *a)
1237 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1238 return v4l2_subdev_call(sd, video, cropcap, a);
1241 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1243 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1244 return v4l2_subdev_call(sd, video, g_crop, a);
1247 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1249 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1250 return v4l2_subdev_call(sd, video, s_crop, a);
1253 static int default_g_parm(struct soc_camera_device *icd,
1254 struct v4l2_streamparm *parm)
1256 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1257 return v4l2_subdev_call(sd, video, g_parm, parm);
1260 static int default_s_parm(struct soc_camera_device *icd,
1261 struct v4l2_streamparm *parm)
1263 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1264 return v4l2_subdev_call(sd, video, s_parm, parm);
1267 static int default_enum_fsizes(struct soc_camera_device *icd,
1268 struct v4l2_frmsizeenum *fsize)
1271 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1272 const struct soc_camera_format_xlate *xlate;
1273 __u32 pixfmt = fsize->pixel_format;
1274 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1276 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1279 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1280 fsize_mbus.pixel_format = xlate->code;
1282 ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
1286 *fsize = fsize_mbus;
1287 fsize->pixel_format = pixfmt;
1292 int soc_camera_host_register(struct soc_camera_host *ici)
1294 struct soc_camera_host *ix;
1297 if (!ici || !ici->ops ||
1298 !ici->ops->try_fmt ||
1299 !ici->ops->set_fmt ||
1300 !ici->ops->set_bus_param ||
1301 !ici->ops->querycap ||
1302 ((!ici->ops->init_videobuf ||
1303 !ici->ops->reqbufs) &&
1304 !ici->ops->init_videobuf2) ||
1306 !ici->ops->remove ||
1311 if (!ici->ops->set_crop)
1312 ici->ops->set_crop = default_s_crop;
1313 if (!ici->ops->get_crop)
1314 ici->ops->get_crop = default_g_crop;
1315 if (!ici->ops->cropcap)
1316 ici->ops->cropcap = default_cropcap;
1317 if (!ici->ops->set_parm)
1318 ici->ops->set_parm = default_s_parm;
1319 if (!ici->ops->get_parm)
1320 ici->ops->get_parm = default_g_parm;
1321 if (!ici->ops->enum_fsizes)
1322 ici->ops->enum_fsizes = default_enum_fsizes;
1324 mutex_lock(&list_lock);
1325 list_for_each_entry(ix, &hosts, list) {
1326 if (ix->nr == ici->nr) {
1332 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1336 list_add_tail(&ici->list, &hosts);
1337 mutex_unlock(&list_lock);
1344 mutex_unlock(&list_lock);
1347 EXPORT_SYMBOL(soc_camera_host_register);
1349 /* Unregister all clients! */
1350 void soc_camera_host_unregister(struct soc_camera_host *ici)
1352 struct soc_camera_device *icd;
1354 mutex_lock(&list_lock);
1356 list_del(&ici->list);
1357 list_for_each_entry(icd, &devices, list)
1358 if (icd->iface == ici->nr && to_soc_camera_control(icd))
1359 soc_camera_remove(icd);
1361 mutex_unlock(&list_lock);
1363 v4l2_device_unregister(&ici->v4l2_dev);
1365 EXPORT_SYMBOL(soc_camera_host_unregister);
1367 /* Image capture device */
1368 static int soc_camera_device_register(struct soc_camera_device *icd)
1370 struct soc_camera_device *ix;
1373 for (i = 0; i < 256 && num < 0; i++) {
1375 /* Check if this index is available on this interface */
1376 list_for_each_entry(ix, &devices, list) {
1377 if (ix->iface == icd->iface && ix->devnum == i) {
1386 * ok, we have 256 cameras on this host...
1387 * man, stay reasonable...
1393 icd->host_priv = NULL;
1394 mutex_init(&icd->video_lock);
1396 list_add_tail(&icd->list, &devices);
1401 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1402 .vidioc_querycap = soc_camera_querycap,
1403 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1404 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1405 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1406 .vidioc_enum_input = soc_camera_enum_input,
1407 .vidioc_g_input = soc_camera_g_input,
1408 .vidioc_s_input = soc_camera_s_input,
1409 .vidioc_s_std = soc_camera_s_std,
1410 .vidioc_enum_framesizes = soc_camera_enum_fsizes,
1411 .vidioc_reqbufs = soc_camera_reqbufs,
1412 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1413 .vidioc_querybuf = soc_camera_querybuf,
1414 .vidioc_qbuf = soc_camera_qbuf,
1415 .vidioc_dqbuf = soc_camera_dqbuf,
1416 .vidioc_streamon = soc_camera_streamon,
1417 .vidioc_streamoff = soc_camera_streamoff,
1418 .vidioc_queryctrl = soc_camera_queryctrl,
1419 .vidioc_g_ctrl = soc_camera_g_ctrl,
1420 .vidioc_s_ctrl = soc_camera_s_ctrl,
1421 .vidioc_cropcap = soc_camera_cropcap,
1422 .vidioc_g_crop = soc_camera_g_crop,
1423 .vidioc_s_crop = soc_camera_s_crop,
1424 .vidioc_g_parm = soc_camera_g_parm,
1425 .vidioc_s_parm = soc_camera_s_parm,
1426 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1427 #ifdef CONFIG_VIDEO_ADV_DEBUG
1428 .vidioc_g_register = soc_camera_g_register,
1429 .vidioc_s_register = soc_camera_s_register,
1433 static int video_dev_create(struct soc_camera_device *icd)
1435 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1436 struct video_device *vdev = video_device_alloc();
1441 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1443 vdev->parent = icd->pdev;
1444 vdev->current_norm = V4L2_STD_UNKNOWN;
1445 vdev->fops = &soc_camera_fops;
1446 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1447 vdev->release = video_device_release;
1448 vdev->tvnorms = V4L2_STD_UNKNOWN;
1449 vdev->lock = &icd->video_lock;
1457 * Called from soc_camera_probe() above (with .video_lock held???)
1459 static int soc_camera_video_start(struct soc_camera_device *icd)
1461 const struct device_type *type = icd->vdev->dev.type;
1467 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1469 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
1473 /* Restore device type, possibly set by the subdevice driver */
1474 icd->vdev->dev.type = type;
1479 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1481 struct soc_camera_link *icl = pdev->dev.platform_data;
1482 struct soc_camera_device *icd;
1488 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1492 icd->iface = icl->bus_id;
1494 icd->pdev = &pdev->dev;
1495 platform_set_drvdata(pdev, icd);
1497 ret = soc_camera_device_register(icd);
1501 icd->user_width = DEFAULT_WIDTH;
1502 icd->user_height = DEFAULT_HEIGHT;
1513 * Only called on rmmod for each platform device, since they are not
1514 * hot-pluggable. Now we know, that all our users - hosts and devices have
1515 * been unloaded already
1517 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1519 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1524 list_del(&icd->list);
1531 static struct platform_driver __refdata soc_camera_pdrv = {
1532 .remove = __devexit_p(soc_camera_pdrv_remove),
1534 .name = "soc-camera-pdrv",
1535 .owner = THIS_MODULE,
1539 static int __init soc_camera_init(void)
1541 return platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1544 static void __exit soc_camera_exit(void)
1546 platform_driver_unregister(&soc_camera_pdrv);
1549 module_init(soc_camera_init);
1550 module_exit(soc_camera_exit);
1552 MODULE_DESCRIPTION("Image capture bus driver");
1553 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1554 MODULE_LICENSE("GPL");
1555 MODULE_ALIAS("platform:soc-camera-pdrv");