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 static LIST_HEAD(hosts);
45 static LIST_HEAD(devices);
46 static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
48 static int soc_camera_power_set(struct soc_camera_device *icd,
49 struct soc_camera_link *icl,
55 ret = regulator_bulk_enable(icl->num_regulators,
58 dev_err(&icd->dev, "Cannot enable regulators\n");
63 ret = icl->power(icd->pdev, power_on);
66 "Platform failed to power-on the camera.\n");
68 regulator_bulk_disable(icl->num_regulators,
75 ret = icl->power(icd->pdev, 0);
78 "Platform failed to power-off the camera.\n");
82 ret = regulator_bulk_disable(icl->num_regulators,
85 dev_err(&icd->dev, "Cannot disable regulators\n");
93 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
94 struct soc_camera_device *icd, unsigned int fourcc)
98 for (i = 0; i < icd->num_user_formats; i++)
99 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
100 return icd->user_formats + i;
103 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
106 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
107 * @icl: camera platform parameters
108 * @flags: flags to be inverted according to platform configuration
109 * @return: resulting flags
111 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
116 /* If only one of the two polarities is supported, switch to the opposite */
117 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
118 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
119 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
120 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
123 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
124 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
125 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
126 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
129 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
130 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
131 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
132 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
137 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
139 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
142 static int soc_camera_try_fmt(struct soc_camera_device *icd,
143 struct v4l2_format *f)
145 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
146 struct v4l2_pix_format *pix = &f->fmt.pix;
149 dev_dbg(&icd->dev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
150 pixfmtstr(pix->pixelformat), pix->width, pix->height);
152 pix->bytesperline = 0;
155 ret = ici->ops->try_fmt(icd, f);
159 if (!pix->sizeimage) {
160 if (!pix->bytesperline) {
161 const struct soc_camera_format_xlate *xlate;
163 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
167 ret = soc_mbus_bytes_per_line(pix->width,
170 pix->bytesperline = ret;
172 if (pix->bytesperline)
173 pix->sizeimage = pix->bytesperline * pix->height;
179 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
180 struct v4l2_format *f)
182 struct soc_camera_device *icd = file->private_data;
184 WARN_ON(priv != file->private_data);
186 /* Only single-plane capture is supported so far */
187 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
190 /* limit format to hardware capabilities */
191 return soc_camera_try_fmt(icd, f);
194 static int soc_camera_enum_input(struct file *file, void *priv,
195 struct v4l2_input *inp)
197 struct soc_camera_device *icd = file->private_data;
203 if (icd->ops->enum_input)
204 ret = icd->ops->enum_input(icd, inp);
206 /* default is camera */
207 inp->type = V4L2_INPUT_TYPE_CAMERA;
208 inp->std = V4L2_STD_UNKNOWN;
209 strcpy(inp->name, "Camera");
215 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
222 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
230 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
232 struct soc_camera_device *icd = file->private_data;
233 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
235 return v4l2_subdev_call(sd, core, s_std, *a);
238 static int soc_camera_enum_fsizes(struct file *file, void *fh,
239 struct v4l2_frmsizeenum *fsize)
241 struct soc_camera_device *icd = file->private_data;
242 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
244 return ici->ops->enum_fsizes(icd, fsize);
247 static int soc_camera_reqbufs(struct file *file, void *priv,
248 struct v4l2_requestbuffers *p)
251 struct soc_camera_device *icd = file->private_data;
252 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
254 WARN_ON(priv != file->private_data);
256 if (icd->streamer && icd->streamer != file)
259 if (ici->ops->init_videobuf) {
260 ret = videobuf_reqbufs(&icd->vb_vidq, p);
264 ret = ici->ops->reqbufs(icd, p);
266 ret = vb2_reqbufs(&icd->vb2_vidq, p);
269 if (!ret && !icd->streamer)
270 icd->streamer = file;
275 static int soc_camera_querybuf(struct file *file, void *priv,
276 struct v4l2_buffer *p)
278 struct soc_camera_device *icd = file->private_data;
279 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
281 WARN_ON(priv != file->private_data);
283 if (ici->ops->init_videobuf)
284 return videobuf_querybuf(&icd->vb_vidq, p);
286 return vb2_querybuf(&icd->vb2_vidq, p);
289 static int soc_camera_qbuf(struct file *file, void *priv,
290 struct v4l2_buffer *p)
292 struct soc_camera_device *icd = file->private_data;
293 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
295 WARN_ON(priv != file->private_data);
297 if (icd->streamer != file)
300 if (ici->ops->init_videobuf)
301 return videobuf_qbuf(&icd->vb_vidq, p);
303 return vb2_qbuf(&icd->vb2_vidq, p);
306 static int soc_camera_dqbuf(struct file *file, void *priv,
307 struct v4l2_buffer *p)
309 struct soc_camera_device *icd = file->private_data;
310 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
312 WARN_ON(priv != file->private_data);
314 if (icd->streamer != file)
317 if (ici->ops->init_videobuf)
318 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
320 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
323 /* Always entered with .video_lock held */
324 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
326 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
327 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
328 unsigned int i, fmts = 0, raw_fmts = 0;
330 enum v4l2_mbus_pixelcode code;
332 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
335 if (!ici->ops->get_formats)
337 * Fallback mode - the host will have to serve all
338 * sensor-provided formats one-to-one to the user
343 * First pass - only count formats this host-sensor
344 * configuration can provide
346 for (i = 0; i < raw_fmts; i++) {
347 ret = ici->ops->get_formats(icd, i, NULL);
357 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
358 if (!icd->user_formats)
361 icd->num_user_formats = fmts;
363 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
365 /* Second pass - actually fill data formats */
367 for (i = 0; i < raw_fmts; i++)
368 if (!ici->ops->get_formats) {
369 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
370 icd->user_formats[i].host_fmt =
371 soc_mbus_get_fmtdesc(code);
372 icd->user_formats[i].code = code;
374 ret = ici->ops->get_formats(icd, i,
375 &icd->user_formats[fmts]);
381 icd->current_fmt = &icd->user_formats[0];
386 icd->num_user_formats = 0;
387 vfree(icd->user_formats);
391 /* Always entered with .video_lock held */
392 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
394 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
396 if (ici->ops->put_formats)
397 ici->ops->put_formats(icd);
398 icd->current_fmt = NULL;
399 icd->num_user_formats = 0;
400 vfree(icd->user_formats);
401 icd->user_formats = NULL;
404 /* Called with .vb_lock held, or from the first open(2), see comment there */
405 static int soc_camera_set_fmt(struct soc_camera_device *icd,
406 struct v4l2_format *f)
408 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
409 struct v4l2_pix_format *pix = &f->fmt.pix;
412 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
413 pixfmtstr(pix->pixelformat), pix->width, pix->height);
415 /* We always call try_fmt() before set_fmt() or set_crop() */
416 ret = soc_camera_try_fmt(icd, f);
420 ret = ici->ops->set_fmt(icd, f);
423 } else if (!icd->current_fmt ||
424 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
426 "Host driver hasn't set up current format correctly!\n");
430 icd->user_width = pix->width;
431 icd->user_height = pix->height;
432 icd->bytesperline = pix->bytesperline;
433 icd->sizeimage = pix->sizeimage;
434 icd->colorspace = pix->colorspace;
435 icd->field = pix->field;
436 if (ici->ops->init_videobuf)
437 icd->vb_vidq.field = pix->field;
439 dev_dbg(&icd->dev, "set width: %d height: %d\n",
440 icd->user_width, icd->user_height);
442 /* set physical bus parameters */
443 return ici->ops->set_bus_param(icd, pix->pixelformat);
446 static int soc_camera_open(struct file *file)
448 struct video_device *vdev = video_devdata(file);
449 struct soc_camera_device *icd = container_of(vdev->parent,
450 struct soc_camera_device,
452 struct soc_camera_link *icl = to_soc_camera_link(icd);
453 struct soc_camera_host *ici;
457 /* No device driver attached */
460 ici = to_soc_camera_host(icd->dev.parent);
462 if (!try_module_get(ici->ops->owner)) {
463 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
469 /* Now we really have to activate the camera */
470 if (icd->use_count == 1) {
471 /* Restore parameters before the last close() per V4L2 API */
472 struct v4l2_format f = {
473 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
475 .width = icd->user_width,
476 .height = icd->user_height,
478 .colorspace = icd->colorspace,
480 icd->current_fmt->host_fmt->fourcc,
484 ret = soc_camera_power_set(icd, icl, 1);
488 /* The camera could have been already on, try to reset */
490 icl->reset(icd->pdev);
492 ret = ici->ops->add(icd);
494 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
498 pm_runtime_enable(&icd->vdev->dev);
499 ret = pm_runtime_resume(&icd->vdev->dev);
500 if (ret < 0 && ret != -ENOSYS)
504 * Try to configure with default parameters. Notice: this is the
505 * very first open, so, we cannot race against other calls,
506 * apart from someone else calling open() simultaneously, but
507 * .video_lock is protecting us against it.
509 ret = soc_camera_set_fmt(icd, &f);
513 if (ici->ops->init_videobuf) {
514 ici->ops->init_videobuf(&icd->vb_vidq, icd);
516 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
522 file->private_data = icd;
523 dev_dbg(&icd->dev, "camera device open\n");
528 * First four errors are entered with the .video_lock held
533 pm_runtime_disable(&icd->vdev->dev);
535 ici->ops->remove(icd);
537 soc_camera_power_set(icd, icl, 0);
540 module_put(ici->ops->owner);
545 static int soc_camera_close(struct file *file)
547 struct soc_camera_device *icd = file->private_data;
548 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
551 if (!icd->use_count) {
552 struct soc_camera_link *icl = to_soc_camera_link(icd);
554 pm_runtime_suspend(&icd->vdev->dev);
555 pm_runtime_disable(&icd->vdev->dev);
557 ici->ops->remove(icd);
558 if (ici->ops->init_videobuf2)
559 vb2_queue_release(&icd->vb2_vidq);
561 soc_camera_power_set(icd, icl, 0);
564 if (icd->streamer == file)
565 icd->streamer = NULL;
567 module_put(ici->ops->owner);
569 dev_dbg(&icd->dev, "camera device close\n");
574 static ssize_t soc_camera_read(struct file *file, char __user *buf,
575 size_t count, loff_t *ppos)
577 struct soc_camera_device *icd = file->private_data;
580 dev_err(&icd->dev, "camera device read not implemented\n");
585 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
587 struct soc_camera_device *icd = file->private_data;
588 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
591 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
593 if (icd->streamer != file)
596 if (ici->ops->init_videobuf)
597 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
599 err = vb2_mmap(&icd->vb2_vidq, vma);
601 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
602 (unsigned long)vma->vm_start,
603 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
609 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
611 struct soc_camera_device *icd = file->private_data;
612 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
614 if (icd->streamer != file)
617 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
618 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
622 return ici->ops->poll(file, pt);
625 void soc_camera_lock(struct vb2_queue *vq)
627 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
628 mutex_lock(&icd->video_lock);
630 EXPORT_SYMBOL(soc_camera_lock);
632 void soc_camera_unlock(struct vb2_queue *vq)
634 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
635 mutex_unlock(&icd->video_lock);
637 EXPORT_SYMBOL(soc_camera_unlock);
639 static struct v4l2_file_operations soc_camera_fops = {
640 .owner = THIS_MODULE,
641 .open = soc_camera_open,
642 .release = soc_camera_close,
643 .unlocked_ioctl = video_ioctl2,
644 .read = soc_camera_read,
645 .mmap = soc_camera_mmap,
646 .poll = soc_camera_poll,
649 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
650 struct v4l2_format *f)
652 struct soc_camera_device *icd = file->private_data;
655 WARN_ON(priv != file->private_data);
657 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
658 dev_warn(&icd->dev, "Wrong buf-type %d\n", f->type);
662 if (icd->streamer && icd->streamer != file)
665 if (icd->vb_vidq.bufs[0]) {
666 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
670 ret = soc_camera_set_fmt(icd, f);
672 if (!ret && !icd->streamer)
673 icd->streamer = file;
678 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
679 struct v4l2_fmtdesc *f)
681 struct soc_camera_device *icd = file->private_data;
682 const struct soc_mbus_pixelfmt *format;
684 WARN_ON(priv != file->private_data);
686 if (f->index >= icd->num_user_formats)
689 format = icd->user_formats[f->index].host_fmt;
692 strlcpy(f->description, format->name, sizeof(f->description));
693 f->pixelformat = format->fourcc;
697 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
698 struct v4l2_format *f)
700 struct soc_camera_device *icd = file->private_data;
701 struct v4l2_pix_format *pix = &f->fmt.pix;
703 WARN_ON(priv != file->private_data);
705 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
708 pix->width = icd->user_width;
709 pix->height = icd->user_height;
710 pix->bytesperline = icd->bytesperline;
711 pix->sizeimage = icd->sizeimage;
712 pix->field = icd->field;
713 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
714 pix->colorspace = icd->colorspace;
715 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
716 icd->current_fmt->host_fmt->fourcc);
720 static int soc_camera_querycap(struct file *file, void *priv,
721 struct v4l2_capability *cap)
723 struct soc_camera_device *icd = file->private_data;
724 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
726 WARN_ON(priv != file->private_data);
728 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
729 return ici->ops->querycap(ici, cap);
732 static int soc_camera_streamon(struct file *file, void *priv,
733 enum v4l2_buf_type i)
735 struct soc_camera_device *icd = file->private_data;
736 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
737 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
740 WARN_ON(priv != file->private_data);
742 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
745 if (icd->streamer != file)
748 /* This calls buf_queue from host driver's videobuf_queue_ops */
749 if (ici->ops->init_videobuf)
750 ret = videobuf_streamon(&icd->vb_vidq);
752 ret = vb2_streamon(&icd->vb2_vidq, i);
755 v4l2_subdev_call(sd, video, s_stream, 1);
760 static int soc_camera_streamoff(struct file *file, void *priv,
761 enum v4l2_buf_type i)
763 struct soc_camera_device *icd = file->private_data;
764 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
765 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
767 WARN_ON(priv != file->private_data);
769 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
772 if (icd->streamer != file)
776 * This calls buf_release from host driver's videobuf_queue_ops for all
777 * remaining buffers. When the last buffer is freed, stop capture
779 if (ici->ops->init_videobuf)
780 videobuf_streamoff(&icd->vb_vidq);
782 vb2_streamoff(&icd->vb2_vidq, i);
784 v4l2_subdev_call(sd, video, s_stream, 0);
789 static int soc_camera_queryctrl(struct file *file, void *priv,
790 struct v4l2_queryctrl *qc)
792 struct soc_camera_device *icd = file->private_data;
793 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
796 WARN_ON(priv != file->private_data);
801 /* First check host controls */
802 for (i = 0; i < ici->ops->num_controls; i++)
803 if (qc->id == ici->ops->controls[i].id) {
804 memcpy(qc, &(ici->ops->controls[i]),
809 /* Then device controls */
810 for (i = 0; i < icd->ops->num_controls; i++)
811 if (qc->id == icd->ops->controls[i].id) {
812 memcpy(qc, &(icd->ops->controls[i]),
820 static int soc_camera_g_ctrl(struct file *file, void *priv,
821 struct v4l2_control *ctrl)
823 struct soc_camera_device *icd = file->private_data;
824 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
825 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
828 WARN_ON(priv != file->private_data);
830 if (ici->ops->get_ctrl) {
831 ret = ici->ops->get_ctrl(icd, ctrl);
832 if (ret != -ENOIOCTLCMD)
836 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
839 static int soc_camera_s_ctrl(struct file *file, void *priv,
840 struct v4l2_control *ctrl)
842 struct soc_camera_device *icd = file->private_data;
843 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
844 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
847 WARN_ON(priv != file->private_data);
849 if (ici->ops->set_ctrl) {
850 ret = ici->ops->set_ctrl(icd, ctrl);
851 if (ret != -ENOIOCTLCMD)
855 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
858 static int soc_camera_cropcap(struct file *file, void *fh,
859 struct v4l2_cropcap *a)
861 struct soc_camera_device *icd = file->private_data;
862 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
864 return ici->ops->cropcap(icd, a);
867 static int soc_camera_g_crop(struct file *file, void *fh,
870 struct soc_camera_device *icd = file->private_data;
871 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
874 ret = ici->ops->get_crop(icd, a);
880 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
881 * argument with the actual geometry, instead, the user shall use G_CROP to
884 static int soc_camera_s_crop(struct file *file, void *fh,
887 struct soc_camera_device *icd = file->private_data;
888 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
889 struct v4l2_rect *rect = &a->c;
890 struct v4l2_crop current_crop;
893 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
896 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
897 rect->width, rect->height, rect->left, rect->top);
899 /* If get_crop fails, we'll let host and / or client drivers decide */
900 ret = ici->ops->get_crop(icd, ¤t_crop);
902 /* Prohibit window size change with initialised buffers */
905 "S_CROP denied: getting current crop failed\n");
906 } else if (icd->vb_vidq.bufs[0] &&
907 (a->c.width != current_crop.c.width ||
908 a->c.height != current_crop.c.height)) {
910 "S_CROP denied: queue initialised and sizes differ\n");
913 ret = ici->ops->set_crop(icd, a);
919 static int soc_camera_g_parm(struct file *file, void *fh,
920 struct v4l2_streamparm *a)
922 struct soc_camera_device *icd = file->private_data;
923 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
925 if (ici->ops->get_parm)
926 return ici->ops->get_parm(icd, a);
931 static int soc_camera_s_parm(struct file *file, void *fh,
932 struct v4l2_streamparm *a)
934 struct soc_camera_device *icd = file->private_data;
935 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
937 if (ici->ops->set_parm)
938 return ici->ops->set_parm(icd, a);
943 static int soc_camera_g_chip_ident(struct file *file, void *fh,
944 struct v4l2_dbg_chip_ident *id)
946 struct soc_camera_device *icd = file->private_data;
947 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
949 return v4l2_subdev_call(sd, core, g_chip_ident, id);
952 #ifdef CONFIG_VIDEO_ADV_DEBUG
953 static int soc_camera_g_register(struct file *file, void *fh,
954 struct v4l2_dbg_register *reg)
956 struct soc_camera_device *icd = file->private_data;
957 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
959 return v4l2_subdev_call(sd, core, g_register, reg);
962 static int soc_camera_s_register(struct file *file, void *fh,
963 struct v4l2_dbg_register *reg)
965 struct soc_camera_device *icd = file->private_data;
966 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
968 return v4l2_subdev_call(sd, core, s_register, reg);
972 /* So far this function cannot fail */
973 static void scan_add_host(struct soc_camera_host *ici)
975 struct soc_camera_device *icd;
977 mutex_lock(&list_lock);
979 list_for_each_entry(icd, &devices, list) {
980 if (icd->iface == ici->nr) {
982 icd->dev.parent = ici->v4l2_dev.dev;
983 dev_set_name(&icd->dev, "%u-%u", icd->iface,
985 ret = device_register(&icd->dev);
987 icd->dev.parent = NULL;
989 "Cannot register device: %d\n", ret);
994 mutex_unlock(&list_lock);
997 #ifdef CONFIG_I2C_BOARDINFO
998 static int soc_camera_init_i2c(struct soc_camera_device *icd,
999 struct soc_camera_link *icl)
1001 struct i2c_client *client;
1002 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1003 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
1004 struct v4l2_subdev *subdev;
1007 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
1008 icl->i2c_adapter_id);
1012 icl->board_info->platform_data = icd;
1014 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1015 icl->board_info, NULL);
1019 client = v4l2_get_subdevdata(subdev);
1021 /* Use to_i2c_client(dev) to recover the i2c client */
1022 dev_set_drvdata(&icd->dev, &client->dev);
1026 i2c_put_adapter(adap);
1031 static void soc_camera_free_i2c(struct soc_camera_device *icd)
1033 struct i2c_client *client =
1034 to_i2c_client(to_soc_camera_control(icd));
1035 struct i2c_adapter *adap = client->adapter;
1036 dev_set_drvdata(&icd->dev, NULL);
1037 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1038 i2c_unregister_device(client);
1039 i2c_put_adapter(adap);
1042 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
1043 #define soc_camera_free_i2c(icd) do {} while (0)
1046 static int soc_camera_video_start(struct soc_camera_device *icd);
1047 static int video_dev_create(struct soc_camera_device *icd);
1048 /* Called during host-driver probe */
1049 static int soc_camera_probe(struct device *dev)
1051 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1052 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
1053 struct soc_camera_link *icl = to_soc_camera_link(icd);
1054 struct device *control = NULL;
1055 struct v4l2_subdev *sd;
1056 struct v4l2_mbus_framefmt mf;
1059 dev_info(dev, "Probing %s\n", dev_name(dev));
1061 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
1066 ret = soc_camera_power_set(icd, icl, 1);
1070 /* The camera could have been already on, try to reset */
1072 icl->reset(icd->pdev);
1074 ret = ici->ops->add(icd);
1078 /* Must have icd->vdev before registering the device */
1079 ret = video_dev_create(icd);
1083 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1084 if (icl->board_info) {
1085 ret = soc_camera_init_i2c(icd, icl);
1088 } else if (!icl->add_device || !icl->del_device) {
1092 if (icl->module_name)
1093 ret = request_module(icl->module_name);
1095 ret = icl->add_device(icl, &icd->dev);
1100 * FIXME: this is racy, have to use driver-binding notification,
1101 * when it is available
1103 control = to_soc_camera_control(icd);
1104 if (!control || !control->driver || !dev_get_drvdata(control) ||
1105 !try_module_get(control->driver->owner)) {
1106 icl->del_device(icl);
1111 sd = soc_camera_to_subdev(icd);
1112 sd->grp_id = (long)icd;
1114 /* At this point client .probe() should have run already */
1115 ret = soc_camera_init_user_formats(icd);
1119 icd->field = V4L2_FIELD_ANY;
1121 icd->vdev->lock = &icd->video_lock;
1124 * ..._video_start() will create a device node, video_register_device()
1125 * itself is protected against concurrent open() calls, but we also have
1126 * to protect our data.
1128 mutex_lock(&icd->video_lock);
1130 ret = soc_camera_video_start(icd);
1134 /* Try to improve our guess of a reasonable window format */
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);
1149 soc_camera_power_set(icd, icl, 0);
1151 mutex_unlock(&icd->video_lock);
1156 mutex_unlock(&icd->video_lock);
1157 soc_camera_free_user_formats(icd);
1159 if (icl->board_info) {
1160 soc_camera_free_i2c(icd);
1162 icl->del_device(icl);
1163 module_put(control->driver->owner);
1167 video_device_release(icd->vdev);
1169 ici->ops->remove(icd);
1171 soc_camera_power_set(icd, icl, 0);
1173 regulator_bulk_free(icl->num_regulators, icl->regulators);
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 video_unregister_device(vdev);
1195 if (icl->board_info) {
1196 soc_camera_free_i2c(icd);
1198 struct device_driver *drv = to_soc_camera_control(icd) ?
1199 to_soc_camera_control(icd)->driver : NULL;
1201 icl->del_device(icl);
1202 module_put(drv->owner);
1205 soc_camera_free_user_formats(icd);
1207 regulator_bulk_free(icl->num_regulators, icl->regulators);
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 int default_g_parm(struct soc_camera_device *icd,
1275 struct v4l2_streamparm *parm)
1277 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1278 return v4l2_subdev_call(sd, video, g_parm, parm);
1281 static int default_s_parm(struct soc_camera_device *icd,
1282 struct v4l2_streamparm *parm)
1284 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1285 return v4l2_subdev_call(sd, video, s_parm, parm);
1288 static int default_enum_fsizes(struct soc_camera_device *icd,
1289 struct v4l2_frmsizeenum *fsize)
1292 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1293 const struct soc_camera_format_xlate *xlate;
1294 __u32 pixfmt = fsize->pixel_format;
1295 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1297 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1300 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1301 fsize_mbus.pixel_format = xlate->code;
1303 ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
1307 *fsize = fsize_mbus;
1308 fsize->pixel_format = pixfmt;
1313 static void soc_camera_device_init(struct device *dev, void *pdata)
1315 dev->platform_data = pdata;
1316 dev->bus = &soc_camera_bus_type;
1317 dev->release = dummy_release;
1320 int soc_camera_host_register(struct soc_camera_host *ici)
1322 struct soc_camera_host *ix;
1325 if (!ici || !ici->ops ||
1326 !ici->ops->try_fmt ||
1327 !ici->ops->set_fmt ||
1328 !ici->ops->set_bus_param ||
1329 !ici->ops->querycap ||
1330 ((!ici->ops->init_videobuf ||
1331 !ici->ops->reqbufs) &&
1332 !ici->ops->init_videobuf2) ||
1334 !ici->ops->remove ||
1339 if (!ici->ops->set_crop)
1340 ici->ops->set_crop = default_s_crop;
1341 if (!ici->ops->get_crop)
1342 ici->ops->get_crop = default_g_crop;
1343 if (!ici->ops->cropcap)
1344 ici->ops->cropcap = default_cropcap;
1345 if (!ici->ops->set_parm)
1346 ici->ops->set_parm = default_s_parm;
1347 if (!ici->ops->get_parm)
1348 ici->ops->get_parm = default_g_parm;
1349 if (!ici->ops->enum_fsizes)
1350 ici->ops->enum_fsizes = default_enum_fsizes;
1352 mutex_lock(&list_lock);
1353 list_for_each_entry(ix, &hosts, list) {
1354 if (ix->nr == ici->nr) {
1360 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1364 list_add_tail(&ici->list, &hosts);
1365 mutex_unlock(&list_lock);
1372 mutex_unlock(&list_lock);
1375 EXPORT_SYMBOL(soc_camera_host_register);
1377 /* Unregister all clients! */
1378 void soc_camera_host_unregister(struct soc_camera_host *ici)
1380 struct soc_camera_device *icd;
1382 mutex_lock(&list_lock);
1384 list_del(&ici->list);
1386 list_for_each_entry(icd, &devices, list) {
1387 if (icd->iface == ici->nr) {
1388 void *pdata = icd->dev.platform_data;
1389 /* The bus->remove will be called */
1390 device_unregister(&icd->dev);
1392 * Not before device_unregister(), .remove
1393 * needs parent to call ici->ops->remove().
1394 * If the host module is loaded again, device_register()
1395 * would complain "already initialised," since 2.6.32
1396 * this is also needed to prevent use-after-free of the
1397 * device private data.
1399 memset(&icd->dev, 0, sizeof(icd->dev));
1400 soc_camera_device_init(&icd->dev, pdata);
1404 mutex_unlock(&list_lock);
1406 v4l2_device_unregister(&ici->v4l2_dev);
1408 EXPORT_SYMBOL(soc_camera_host_unregister);
1410 /* Image capture device */
1411 static int soc_camera_device_register(struct soc_camera_device *icd)
1413 struct soc_camera_device *ix;
1416 for (i = 0; i < 256 && num < 0; i++) {
1418 /* Check if this index is available on this interface */
1419 list_for_each_entry(ix, &devices, list) {
1420 if (ix->iface == icd->iface && ix->devnum == i) {
1429 * ok, we have 256 cameras on this host...
1430 * man, stay reasonable...
1436 icd->host_priv = NULL;
1437 mutex_init(&icd->video_lock);
1439 list_add_tail(&icd->list, &devices);
1444 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1446 list_del(&icd->list);
1449 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1450 .vidioc_querycap = soc_camera_querycap,
1451 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1452 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1453 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1454 .vidioc_enum_input = soc_camera_enum_input,
1455 .vidioc_g_input = soc_camera_g_input,
1456 .vidioc_s_input = soc_camera_s_input,
1457 .vidioc_s_std = soc_camera_s_std,
1458 .vidioc_enum_framesizes = soc_camera_enum_fsizes,
1459 .vidioc_reqbufs = soc_camera_reqbufs,
1460 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1461 .vidioc_querybuf = soc_camera_querybuf,
1462 .vidioc_qbuf = soc_camera_qbuf,
1463 .vidioc_dqbuf = soc_camera_dqbuf,
1464 .vidioc_streamon = soc_camera_streamon,
1465 .vidioc_streamoff = soc_camera_streamoff,
1466 .vidioc_queryctrl = soc_camera_queryctrl,
1467 .vidioc_g_ctrl = soc_camera_g_ctrl,
1468 .vidioc_s_ctrl = soc_camera_s_ctrl,
1469 .vidioc_cropcap = soc_camera_cropcap,
1470 .vidioc_g_crop = soc_camera_g_crop,
1471 .vidioc_s_crop = soc_camera_s_crop,
1472 .vidioc_g_parm = soc_camera_g_parm,
1473 .vidioc_s_parm = soc_camera_s_parm,
1474 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1475 #ifdef CONFIG_VIDEO_ADV_DEBUG
1476 .vidioc_g_register = soc_camera_g_register,
1477 .vidioc_s_register = soc_camera_s_register,
1481 static int video_dev_create(struct soc_camera_device *icd)
1483 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1484 struct video_device *vdev = video_device_alloc();
1489 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1491 vdev->parent = &icd->dev;
1492 vdev->current_norm = V4L2_STD_UNKNOWN;
1493 vdev->fops = &soc_camera_fops;
1494 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1495 vdev->release = video_device_release;
1496 vdev->tvnorms = V4L2_STD_UNKNOWN;
1504 * Called from soc_camera_probe() above (with .video_lock held???)
1506 static int soc_camera_video_start(struct soc_camera_device *icd)
1508 struct device_type *type = icd->vdev->dev.type;
1511 if (!icd->dev.parent)
1515 !icd->ops->query_bus_param ||
1516 !icd->ops->set_bus_param)
1519 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1521 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1525 /* Restore device type, possibly set by the subdevice driver */
1526 icd->vdev->dev.type = type;
1531 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1533 struct soc_camera_link *icl = pdev->dev.platform_data;
1534 struct soc_camera_device *icd;
1540 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1544 icd->iface = icl->bus_id;
1545 icd->pdev = &pdev->dev;
1546 platform_set_drvdata(pdev, icd);
1548 ret = soc_camera_device_register(icd);
1552 soc_camera_device_init(&icd->dev, icl);
1554 icd->user_width = DEFAULT_WIDTH;
1555 icd->user_height = DEFAULT_HEIGHT;
1566 * Only called on rmmod for each platform device, since they are not
1567 * hot-pluggable. Now we know, that all our users - hosts and devices have
1568 * been unloaded already
1570 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1572 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1577 soc_camera_device_unregister(icd);
1584 static struct platform_driver __refdata soc_camera_pdrv = {
1585 .remove = __devexit_p(soc_camera_pdrv_remove),
1587 .name = "soc-camera-pdrv",
1588 .owner = THIS_MODULE,
1592 static int __init soc_camera_init(void)
1594 int ret = bus_register(&soc_camera_bus_type);
1597 ret = driver_register(&ic_drv);
1601 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1608 driver_unregister(&ic_drv);
1610 bus_unregister(&soc_camera_bus_type);
1614 static void __exit soc_camera_exit(void)
1616 platform_driver_unregister(&soc_camera_pdrv);
1617 driver_unregister(&ic_drv);
1618 bus_unregister(&soc_camera_bus_type);
1621 module_init(soc_camera_init);
1622 module_exit(soc_camera_exit);
1624 MODULE_DESCRIPTION("Image capture bus driver");
1625 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1626 MODULE_LICENSE("GPL");
1627 MODULE_ALIAS("platform:soc-camera-pdrv");