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 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
140 struct v4l2_format *f)
142 struct soc_camera_device *icd = file->private_data;
143 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
145 WARN_ON(priv != file->private_data);
147 /* Only single-plane capture is supported so far */
148 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
151 /* limit format to hardware capabilities */
152 return ici->ops->try_fmt(icd, f);
155 static int soc_camera_enum_input(struct file *file, void *priv,
156 struct v4l2_input *inp)
158 struct soc_camera_device *icd = file->private_data;
164 if (icd->ops->enum_input)
165 ret = icd->ops->enum_input(icd, inp);
167 /* default is camera */
168 inp->type = V4L2_INPUT_TYPE_CAMERA;
169 inp->std = V4L2_STD_UNKNOWN;
170 strcpy(inp->name, "Camera");
176 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
183 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
191 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
193 struct soc_camera_device *icd = file->private_data;
194 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
196 return v4l2_subdev_call(sd, core, s_std, *a);
199 static int soc_camera_enum_fsizes(struct file *file, void *fh,
200 struct v4l2_frmsizeenum *fsize)
202 struct soc_camera_device *icd = file->private_data;
203 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
205 return ici->ops->enum_fsizes(icd, fsize);
208 static int soc_camera_reqbufs(struct file *file, void *priv,
209 struct v4l2_requestbuffers *p)
212 struct soc_camera_device *icd = file->private_data;
213 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
215 WARN_ON(priv != file->private_data);
217 if (icd->streamer && icd->streamer != file)
220 if (ici->ops->init_videobuf) {
221 ret = videobuf_reqbufs(&icd->vb_vidq, p);
225 ret = ici->ops->reqbufs(icd, p);
227 ret = vb2_reqbufs(&icd->vb2_vidq, p);
230 if (!ret && !icd->streamer)
231 icd->streamer = file;
236 static int soc_camera_querybuf(struct file *file, void *priv,
237 struct v4l2_buffer *p)
239 struct soc_camera_device *icd = file->private_data;
240 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
242 WARN_ON(priv != file->private_data);
244 if (ici->ops->init_videobuf)
245 return videobuf_querybuf(&icd->vb_vidq, p);
247 return vb2_querybuf(&icd->vb2_vidq, p);
250 static int soc_camera_qbuf(struct file *file, void *priv,
251 struct v4l2_buffer *p)
253 struct soc_camera_device *icd = file->private_data;
254 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
256 WARN_ON(priv != file->private_data);
258 if (icd->streamer != file)
261 if (ici->ops->init_videobuf)
262 return videobuf_qbuf(&icd->vb_vidq, p);
264 return vb2_qbuf(&icd->vb2_vidq, p);
267 static int soc_camera_dqbuf(struct file *file, void *priv,
268 struct v4l2_buffer *p)
270 struct soc_camera_device *icd = file->private_data;
271 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
273 WARN_ON(priv != file->private_data);
275 if (icd->streamer != file)
278 if (ici->ops->init_videobuf)
279 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
281 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
284 /* Always entered with .video_lock held */
285 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
287 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
288 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
289 unsigned int i, fmts = 0, raw_fmts = 0;
291 enum v4l2_mbus_pixelcode code;
293 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
296 if (!ici->ops->get_formats)
298 * Fallback mode - the host will have to serve all
299 * sensor-provided formats one-to-one to the user
304 * First pass - only count formats this host-sensor
305 * configuration can provide
307 for (i = 0; i < raw_fmts; i++) {
308 ret = ici->ops->get_formats(icd, i, NULL);
318 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
319 if (!icd->user_formats)
322 icd->num_user_formats = fmts;
324 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
326 /* Second pass - actually fill data formats */
328 for (i = 0; i < raw_fmts; i++)
329 if (!ici->ops->get_formats) {
330 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
331 icd->user_formats[i].host_fmt =
332 soc_mbus_get_fmtdesc(code);
333 icd->user_formats[i].code = code;
335 ret = ici->ops->get_formats(icd, i,
336 &icd->user_formats[fmts]);
342 icd->current_fmt = &icd->user_formats[0];
347 icd->num_user_formats = 0;
348 vfree(icd->user_formats);
352 /* Always entered with .video_lock held */
353 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
355 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
357 if (ici->ops->put_formats)
358 ici->ops->put_formats(icd);
359 icd->current_fmt = NULL;
360 icd->num_user_formats = 0;
361 vfree(icd->user_formats);
362 icd->user_formats = NULL;
365 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
368 /* Called with .vb_lock held, or from the first open(2), see comment there */
369 static int soc_camera_set_fmt(struct soc_camera_device *icd,
370 struct v4l2_format *f)
372 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
373 struct v4l2_pix_format *pix = &f->fmt.pix;
376 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
377 pixfmtstr(pix->pixelformat), pix->width, pix->height);
379 /* We always call try_fmt() before set_fmt() or set_crop() */
380 ret = ici->ops->try_fmt(icd, f);
384 ret = ici->ops->set_fmt(icd, f);
387 } else if (!icd->current_fmt ||
388 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
390 "Host driver hasn't set up current format correctly!\n");
394 icd->user_width = pix->width;
395 icd->user_height = pix->height;
396 icd->bytesperline = pix->bytesperline;
397 icd->sizeimage = pix->sizeimage;
398 icd->colorspace = pix->colorspace;
399 icd->field = pix->field;
400 if (ici->ops->init_videobuf)
401 icd->vb_vidq.field = pix->field;
403 dev_dbg(&icd->dev, "set width: %d height: %d\n",
404 icd->user_width, icd->user_height);
406 /* set physical bus parameters */
407 return ici->ops->set_bus_param(icd, pix->pixelformat);
410 static int soc_camera_open(struct file *file)
412 struct video_device *vdev = video_devdata(file);
413 struct soc_camera_device *icd = container_of(vdev->parent,
414 struct soc_camera_device,
416 struct soc_camera_link *icl = to_soc_camera_link(icd);
417 struct soc_camera_host *ici;
421 /* No device driver attached */
424 ici = to_soc_camera_host(icd->dev.parent);
426 if (!try_module_get(ici->ops->owner)) {
427 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
433 /* Now we really have to activate the camera */
434 if (icd->use_count == 1) {
435 /* Restore parameters before the last close() per V4L2 API */
436 struct v4l2_format f = {
437 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
439 .width = icd->user_width,
440 .height = icd->user_height,
442 .colorspace = icd->colorspace,
444 icd->current_fmt->host_fmt->fourcc,
448 ret = soc_camera_power_set(icd, icl, 1);
452 /* The camera could have been already on, try to reset */
454 icl->reset(icd->pdev);
456 ret = ici->ops->add(icd);
458 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
462 pm_runtime_enable(&icd->vdev->dev);
463 ret = pm_runtime_resume(&icd->vdev->dev);
464 if (ret < 0 && ret != -ENOSYS)
468 * Try to configure with default parameters. Notice: this is the
469 * very first open, so, we cannot race against other calls,
470 * apart from someone else calling open() simultaneously, but
471 * .video_lock is protecting us against it.
473 ret = soc_camera_set_fmt(icd, &f);
477 if (ici->ops->init_videobuf) {
478 ici->ops->init_videobuf(&icd->vb_vidq, icd);
480 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
486 file->private_data = icd;
487 dev_dbg(&icd->dev, "camera device open\n");
492 * First four errors are entered with the .video_lock held
497 pm_runtime_disable(&icd->vdev->dev);
499 ici->ops->remove(icd);
501 soc_camera_power_set(icd, icl, 0);
504 module_put(ici->ops->owner);
509 static int soc_camera_close(struct file *file)
511 struct soc_camera_device *icd = file->private_data;
512 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
515 if (!icd->use_count) {
516 struct soc_camera_link *icl = to_soc_camera_link(icd);
518 pm_runtime_suspend(&icd->vdev->dev);
519 pm_runtime_disable(&icd->vdev->dev);
521 ici->ops->remove(icd);
522 if (ici->ops->init_videobuf2)
523 vb2_queue_release(&icd->vb2_vidq);
525 soc_camera_power_set(icd, icl, 0);
528 if (icd->streamer == file)
529 icd->streamer = NULL;
531 module_put(ici->ops->owner);
533 dev_dbg(&icd->dev, "camera device close\n");
538 static ssize_t soc_camera_read(struct file *file, char __user *buf,
539 size_t count, loff_t *ppos)
541 struct soc_camera_device *icd = file->private_data;
544 dev_err(&icd->dev, "camera device read not implemented\n");
549 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
551 struct soc_camera_device *icd = file->private_data;
552 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
555 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
557 if (icd->streamer != file)
560 if (ici->ops->init_videobuf)
561 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
563 err = vb2_mmap(&icd->vb2_vidq, vma);
565 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
566 (unsigned long)vma->vm_start,
567 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
573 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
575 struct soc_camera_device *icd = file->private_data;
576 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
578 if (icd->streamer != file)
581 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
582 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
586 return ici->ops->poll(file, pt);
589 void soc_camera_lock(struct vb2_queue *vq)
591 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
592 mutex_lock(&icd->video_lock);
594 EXPORT_SYMBOL(soc_camera_lock);
596 void soc_camera_unlock(struct vb2_queue *vq)
598 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
599 mutex_unlock(&icd->video_lock);
601 EXPORT_SYMBOL(soc_camera_unlock);
603 static struct v4l2_file_operations soc_camera_fops = {
604 .owner = THIS_MODULE,
605 .open = soc_camera_open,
606 .release = soc_camera_close,
607 .unlocked_ioctl = video_ioctl2,
608 .read = soc_camera_read,
609 .mmap = soc_camera_mmap,
610 .poll = soc_camera_poll,
613 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
614 struct v4l2_format *f)
616 struct soc_camera_device *icd = file->private_data;
619 WARN_ON(priv != file->private_data);
621 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
622 dev_warn(&icd->dev, "Wrong buf-type %d\n", f->type);
626 if (icd->streamer && icd->streamer != file)
629 if (icd->vb_vidq.bufs[0]) {
630 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
634 ret = soc_camera_set_fmt(icd, f);
636 if (!ret && !icd->streamer)
637 icd->streamer = file;
642 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
643 struct v4l2_fmtdesc *f)
645 struct soc_camera_device *icd = file->private_data;
646 const struct soc_mbus_pixelfmt *format;
648 WARN_ON(priv != file->private_data);
650 if (f->index >= icd->num_user_formats)
653 format = icd->user_formats[f->index].host_fmt;
656 strlcpy(f->description, format->name, sizeof(f->description));
657 f->pixelformat = format->fourcc;
661 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
662 struct v4l2_format *f)
664 struct soc_camera_device *icd = file->private_data;
665 struct v4l2_pix_format *pix = &f->fmt.pix;
667 WARN_ON(priv != file->private_data);
669 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
672 pix->width = icd->user_width;
673 pix->height = icd->user_height;
674 pix->bytesperline = icd->bytesperline;
675 pix->sizeimage = icd->sizeimage;
676 pix->field = icd->field;
677 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
678 pix->colorspace = icd->colorspace;
679 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
680 icd->current_fmt->host_fmt->fourcc);
684 static int soc_camera_querycap(struct file *file, void *priv,
685 struct v4l2_capability *cap)
687 struct soc_camera_device *icd = file->private_data;
688 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
690 WARN_ON(priv != file->private_data);
692 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
693 return ici->ops->querycap(ici, cap);
696 static int soc_camera_streamon(struct file *file, void *priv,
697 enum v4l2_buf_type i)
699 struct soc_camera_device *icd = file->private_data;
700 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
701 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
704 WARN_ON(priv != file->private_data);
706 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
709 if (icd->streamer != file)
712 /* This calls buf_queue from host driver's videobuf_queue_ops */
713 if (ici->ops->init_videobuf)
714 ret = videobuf_streamon(&icd->vb_vidq);
716 ret = vb2_streamon(&icd->vb2_vidq, i);
719 v4l2_subdev_call(sd, video, s_stream, 1);
724 static int soc_camera_streamoff(struct file *file, void *priv,
725 enum v4l2_buf_type i)
727 struct soc_camera_device *icd = file->private_data;
728 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
729 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
731 WARN_ON(priv != file->private_data);
733 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
736 if (icd->streamer != file)
740 * This calls buf_release from host driver's videobuf_queue_ops for all
741 * remaining buffers. When the last buffer is freed, stop capture
743 if (ici->ops->init_videobuf)
744 videobuf_streamoff(&icd->vb_vidq);
746 vb2_streamoff(&icd->vb2_vidq, i);
748 v4l2_subdev_call(sd, video, s_stream, 0);
753 static int soc_camera_queryctrl(struct file *file, void *priv,
754 struct v4l2_queryctrl *qc)
756 struct soc_camera_device *icd = file->private_data;
757 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
760 WARN_ON(priv != file->private_data);
765 /* First check host controls */
766 for (i = 0; i < ici->ops->num_controls; i++)
767 if (qc->id == ici->ops->controls[i].id) {
768 memcpy(qc, &(ici->ops->controls[i]),
773 /* Then device controls */
774 for (i = 0; i < icd->ops->num_controls; i++)
775 if (qc->id == icd->ops->controls[i].id) {
776 memcpy(qc, &(icd->ops->controls[i]),
784 static int soc_camera_g_ctrl(struct file *file, void *priv,
785 struct v4l2_control *ctrl)
787 struct soc_camera_device *icd = file->private_data;
788 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
789 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
792 WARN_ON(priv != file->private_data);
794 if (ici->ops->get_ctrl) {
795 ret = ici->ops->get_ctrl(icd, ctrl);
796 if (ret != -ENOIOCTLCMD)
800 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
803 static int soc_camera_s_ctrl(struct file *file, void *priv,
804 struct v4l2_control *ctrl)
806 struct soc_camera_device *icd = file->private_data;
807 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
808 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
811 WARN_ON(priv != file->private_data);
813 if (ici->ops->set_ctrl) {
814 ret = ici->ops->set_ctrl(icd, ctrl);
815 if (ret != -ENOIOCTLCMD)
819 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
822 static int soc_camera_cropcap(struct file *file, void *fh,
823 struct v4l2_cropcap *a)
825 struct soc_camera_device *icd = file->private_data;
826 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
828 return ici->ops->cropcap(icd, a);
831 static int soc_camera_g_crop(struct file *file, void *fh,
834 struct soc_camera_device *icd = file->private_data;
835 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
838 ret = ici->ops->get_crop(icd, a);
844 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
845 * argument with the actual geometry, instead, the user shall use G_CROP to
848 static int soc_camera_s_crop(struct file *file, void *fh,
851 struct soc_camera_device *icd = file->private_data;
852 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
853 struct v4l2_rect *rect = &a->c;
854 struct v4l2_crop current_crop;
857 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
860 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
861 rect->width, rect->height, rect->left, rect->top);
863 /* If get_crop fails, we'll let host and / or client drivers decide */
864 ret = ici->ops->get_crop(icd, ¤t_crop);
866 /* Prohibit window size change with initialised buffers */
869 "S_CROP denied: getting current crop failed\n");
870 } else if (icd->vb_vidq.bufs[0] &&
871 (a->c.width != current_crop.c.width ||
872 a->c.height != current_crop.c.height)) {
874 "S_CROP denied: queue initialised and sizes differ\n");
877 ret = ici->ops->set_crop(icd, a);
883 static int soc_camera_g_parm(struct file *file, void *fh,
884 struct v4l2_streamparm *a)
886 struct soc_camera_device *icd = file->private_data;
887 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
889 if (ici->ops->get_parm)
890 return ici->ops->get_parm(icd, a);
895 static int soc_camera_s_parm(struct file *file, void *fh,
896 struct v4l2_streamparm *a)
898 struct soc_camera_device *icd = file->private_data;
899 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
901 if (ici->ops->set_parm)
902 return ici->ops->set_parm(icd, a);
907 static int soc_camera_g_chip_ident(struct file *file, void *fh,
908 struct v4l2_dbg_chip_ident *id)
910 struct soc_camera_device *icd = file->private_data;
911 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
913 return v4l2_subdev_call(sd, core, g_chip_ident, id);
916 #ifdef CONFIG_VIDEO_ADV_DEBUG
917 static int soc_camera_g_register(struct file *file, void *fh,
918 struct v4l2_dbg_register *reg)
920 struct soc_camera_device *icd = file->private_data;
921 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
923 return v4l2_subdev_call(sd, core, g_register, reg);
926 static int soc_camera_s_register(struct file *file, void *fh,
927 struct v4l2_dbg_register *reg)
929 struct soc_camera_device *icd = file->private_data;
930 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
932 return v4l2_subdev_call(sd, core, s_register, reg);
936 /* So far this function cannot fail */
937 static void scan_add_host(struct soc_camera_host *ici)
939 struct soc_camera_device *icd;
941 mutex_lock(&list_lock);
943 list_for_each_entry(icd, &devices, list) {
944 if (icd->iface == ici->nr) {
946 icd->dev.parent = ici->v4l2_dev.dev;
947 dev_set_name(&icd->dev, "%u-%u", icd->iface,
949 ret = device_register(&icd->dev);
951 icd->dev.parent = NULL;
953 "Cannot register device: %d\n", ret);
958 mutex_unlock(&list_lock);
961 #ifdef CONFIG_I2C_BOARDINFO
962 static int soc_camera_init_i2c(struct soc_camera_device *icd,
963 struct soc_camera_link *icl)
965 struct i2c_client *client;
966 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
967 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
968 struct v4l2_subdev *subdev;
971 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
972 icl->i2c_adapter_id);
976 icl->board_info->platform_data = icd;
978 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
979 icl->board_info, NULL);
983 client = v4l2_get_subdevdata(subdev);
985 /* Use to_i2c_client(dev) to recover the i2c client */
986 dev_set_drvdata(&icd->dev, &client->dev);
990 i2c_put_adapter(adap);
995 static void soc_camera_free_i2c(struct soc_camera_device *icd)
997 struct i2c_client *client =
998 to_i2c_client(to_soc_camera_control(icd));
999 struct i2c_adapter *adap = client->adapter;
1000 dev_set_drvdata(&icd->dev, NULL);
1001 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1002 i2c_unregister_device(client);
1003 i2c_put_adapter(adap);
1006 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
1007 #define soc_camera_free_i2c(icd) do {} while (0)
1010 static int soc_camera_video_start(struct soc_camera_device *icd);
1011 static int video_dev_create(struct soc_camera_device *icd);
1012 /* Called during host-driver probe */
1013 static int soc_camera_probe(struct device *dev)
1015 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1016 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
1017 struct soc_camera_link *icl = to_soc_camera_link(icd);
1018 struct device *control = NULL;
1019 struct v4l2_subdev *sd;
1020 struct v4l2_mbus_framefmt mf;
1023 dev_info(dev, "Probing %s\n", dev_name(dev));
1025 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
1030 ret = soc_camera_power_set(icd, icl, 1);
1034 /* The camera could have been already on, try to reset */
1036 icl->reset(icd->pdev);
1038 ret = ici->ops->add(icd);
1042 /* Must have icd->vdev before registering the device */
1043 ret = video_dev_create(icd);
1047 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1048 if (icl->board_info) {
1049 ret = soc_camera_init_i2c(icd, icl);
1052 } else if (!icl->add_device || !icl->del_device) {
1056 if (icl->module_name)
1057 ret = request_module(icl->module_name);
1059 ret = icl->add_device(icl, &icd->dev);
1064 * FIXME: this is racy, have to use driver-binding notification,
1065 * when it is available
1067 control = to_soc_camera_control(icd);
1068 if (!control || !control->driver || !dev_get_drvdata(control) ||
1069 !try_module_get(control->driver->owner)) {
1070 icl->del_device(icl);
1075 sd = soc_camera_to_subdev(icd);
1076 sd->grp_id = (long)icd;
1078 /* At this point client .probe() should have run already */
1079 ret = soc_camera_init_user_formats(icd);
1083 icd->field = V4L2_FIELD_ANY;
1085 icd->vdev->lock = &icd->video_lock;
1088 * ..._video_start() will create a device node, video_register_device()
1089 * itself is protected against concurrent open() calls, but we also have
1090 * to protect our data.
1092 mutex_lock(&icd->video_lock);
1094 ret = soc_camera_video_start(icd);
1098 /* Try to improve our guess of a reasonable window format */
1099 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1100 icd->user_width = mf.width;
1101 icd->user_height = mf.height;
1102 icd->colorspace = mf.colorspace;
1103 icd->field = mf.field;
1106 /* Do we have to sysfs_remove_link() before device_unregister()? */
1107 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
1109 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1111 ici->ops->remove(icd);
1113 soc_camera_power_set(icd, icl, 0);
1115 mutex_unlock(&icd->video_lock);
1120 mutex_unlock(&icd->video_lock);
1121 soc_camera_free_user_formats(icd);
1123 if (icl->board_info) {
1124 soc_camera_free_i2c(icd);
1126 icl->del_device(icl);
1127 module_put(control->driver->owner);
1131 video_device_release(icd->vdev);
1133 ici->ops->remove(icd);
1135 soc_camera_power_set(icd, icl, 0);
1137 regulator_bulk_free(icl->num_regulators, icl->regulators);
1143 * This is called on device_unregister, which only means we have to disconnect
1144 * from the host, but not remove ourselves from the device list
1146 static int soc_camera_remove(struct device *dev)
1148 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1149 struct soc_camera_link *icl = to_soc_camera_link(icd);
1150 struct video_device *vdev = icd->vdev;
1152 BUG_ON(!dev->parent);
1155 video_unregister_device(vdev);
1159 if (icl->board_info) {
1160 soc_camera_free_i2c(icd);
1162 struct device_driver *drv = to_soc_camera_control(icd) ?
1163 to_soc_camera_control(icd)->driver : NULL;
1165 icl->del_device(icl);
1166 module_put(drv->owner);
1169 soc_camera_free_user_formats(icd);
1171 regulator_bulk_free(icl->num_regulators, icl->regulators);
1176 static int soc_camera_suspend(struct device *dev, pm_message_t state)
1178 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1179 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1182 if (ici->ops->suspend)
1183 ret = ici->ops->suspend(icd, state);
1188 static int soc_camera_resume(struct device *dev)
1190 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1191 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1194 if (ici->ops->resume)
1195 ret = ici->ops->resume(icd);
1200 struct bus_type soc_camera_bus_type = {
1201 .name = "soc-camera",
1202 .probe = soc_camera_probe,
1203 .remove = soc_camera_remove,
1204 .suspend = soc_camera_suspend,
1205 .resume = soc_camera_resume,
1207 EXPORT_SYMBOL_GPL(soc_camera_bus_type);
1209 static struct device_driver ic_drv = {
1211 .bus = &soc_camera_bus_type,
1212 .owner = THIS_MODULE,
1215 static void dummy_release(struct device *dev)
1219 static int default_cropcap(struct soc_camera_device *icd,
1220 struct v4l2_cropcap *a)
1222 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1223 return v4l2_subdev_call(sd, video, cropcap, a);
1226 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1228 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1229 return v4l2_subdev_call(sd, video, g_crop, a);
1232 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1234 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1235 return v4l2_subdev_call(sd, video, s_crop, a);
1238 static int default_g_parm(struct soc_camera_device *icd,
1239 struct v4l2_streamparm *parm)
1241 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1242 return v4l2_subdev_call(sd, video, g_parm, parm);
1245 static int default_s_parm(struct soc_camera_device *icd,
1246 struct v4l2_streamparm *parm)
1248 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1249 return v4l2_subdev_call(sd, video, s_parm, parm);
1252 static int default_enum_fsizes(struct soc_camera_device *icd,
1253 struct v4l2_frmsizeenum *fsize)
1256 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1257 const struct soc_camera_format_xlate *xlate;
1258 __u32 pixfmt = fsize->pixel_format;
1259 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1261 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1264 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1265 fsize_mbus.pixel_format = xlate->code;
1267 ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
1271 *fsize = fsize_mbus;
1272 fsize->pixel_format = pixfmt;
1277 static void soc_camera_device_init(struct device *dev, void *pdata)
1279 dev->platform_data = pdata;
1280 dev->bus = &soc_camera_bus_type;
1281 dev->release = dummy_release;
1284 int soc_camera_host_register(struct soc_camera_host *ici)
1286 struct soc_camera_host *ix;
1289 if (!ici || !ici->ops ||
1290 !ici->ops->try_fmt ||
1291 !ici->ops->set_fmt ||
1292 !ici->ops->set_bus_param ||
1293 !ici->ops->querycap ||
1294 ((!ici->ops->init_videobuf ||
1295 !ici->ops->reqbufs) &&
1296 !ici->ops->init_videobuf2) ||
1298 !ici->ops->remove ||
1303 if (!ici->ops->set_crop)
1304 ici->ops->set_crop = default_s_crop;
1305 if (!ici->ops->get_crop)
1306 ici->ops->get_crop = default_g_crop;
1307 if (!ici->ops->cropcap)
1308 ici->ops->cropcap = default_cropcap;
1309 if (!ici->ops->set_parm)
1310 ici->ops->set_parm = default_s_parm;
1311 if (!ici->ops->get_parm)
1312 ici->ops->get_parm = default_g_parm;
1313 if (!ici->ops->enum_fsizes)
1314 ici->ops->enum_fsizes = default_enum_fsizes;
1316 mutex_lock(&list_lock);
1317 list_for_each_entry(ix, &hosts, list) {
1318 if (ix->nr == ici->nr) {
1324 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1328 list_add_tail(&ici->list, &hosts);
1329 mutex_unlock(&list_lock);
1336 mutex_unlock(&list_lock);
1339 EXPORT_SYMBOL(soc_camera_host_register);
1341 /* Unregister all clients! */
1342 void soc_camera_host_unregister(struct soc_camera_host *ici)
1344 struct soc_camera_device *icd;
1346 mutex_lock(&list_lock);
1348 list_del(&ici->list);
1350 list_for_each_entry(icd, &devices, list) {
1351 if (icd->iface == ici->nr) {
1352 void *pdata = icd->dev.platform_data;
1353 /* The bus->remove will be called */
1354 device_unregister(&icd->dev);
1356 * Not before device_unregister(), .remove
1357 * needs parent to call ici->ops->remove().
1358 * If the host module is loaded again, device_register()
1359 * would complain "already initialised," since 2.6.32
1360 * this is also needed to prevent use-after-free of the
1361 * device private data.
1363 memset(&icd->dev, 0, sizeof(icd->dev));
1364 soc_camera_device_init(&icd->dev, pdata);
1368 mutex_unlock(&list_lock);
1370 v4l2_device_unregister(&ici->v4l2_dev);
1372 EXPORT_SYMBOL(soc_camera_host_unregister);
1374 /* Image capture device */
1375 static int soc_camera_device_register(struct soc_camera_device *icd)
1377 struct soc_camera_device *ix;
1380 for (i = 0; i < 256 && num < 0; i++) {
1382 /* Check if this index is available on this interface */
1383 list_for_each_entry(ix, &devices, list) {
1384 if (ix->iface == icd->iface && ix->devnum == i) {
1393 * ok, we have 256 cameras on this host...
1394 * man, stay reasonable...
1400 icd->host_priv = NULL;
1401 mutex_init(&icd->video_lock);
1403 list_add_tail(&icd->list, &devices);
1408 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1410 list_del(&icd->list);
1413 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1414 .vidioc_querycap = soc_camera_querycap,
1415 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1416 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1417 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1418 .vidioc_enum_input = soc_camera_enum_input,
1419 .vidioc_g_input = soc_camera_g_input,
1420 .vidioc_s_input = soc_camera_s_input,
1421 .vidioc_s_std = soc_camera_s_std,
1422 .vidioc_enum_framesizes = soc_camera_enum_fsizes,
1423 .vidioc_reqbufs = soc_camera_reqbufs,
1424 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1425 .vidioc_querybuf = soc_camera_querybuf,
1426 .vidioc_qbuf = soc_camera_qbuf,
1427 .vidioc_dqbuf = soc_camera_dqbuf,
1428 .vidioc_streamon = soc_camera_streamon,
1429 .vidioc_streamoff = soc_camera_streamoff,
1430 .vidioc_queryctrl = soc_camera_queryctrl,
1431 .vidioc_g_ctrl = soc_camera_g_ctrl,
1432 .vidioc_s_ctrl = soc_camera_s_ctrl,
1433 .vidioc_cropcap = soc_camera_cropcap,
1434 .vidioc_g_crop = soc_camera_g_crop,
1435 .vidioc_s_crop = soc_camera_s_crop,
1436 .vidioc_g_parm = soc_camera_g_parm,
1437 .vidioc_s_parm = soc_camera_s_parm,
1438 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1439 #ifdef CONFIG_VIDEO_ADV_DEBUG
1440 .vidioc_g_register = soc_camera_g_register,
1441 .vidioc_s_register = soc_camera_s_register,
1445 static int video_dev_create(struct soc_camera_device *icd)
1447 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1448 struct video_device *vdev = video_device_alloc();
1453 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1455 vdev->parent = &icd->dev;
1456 vdev->current_norm = V4L2_STD_UNKNOWN;
1457 vdev->fops = &soc_camera_fops;
1458 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1459 vdev->release = video_device_release;
1460 vdev->tvnorms = V4L2_STD_UNKNOWN;
1468 * Called from soc_camera_probe() above (with .video_lock held???)
1470 static int soc_camera_video_start(struct soc_camera_device *icd)
1472 struct device_type *type = icd->vdev->dev.type;
1475 if (!icd->dev.parent)
1479 !icd->ops->query_bus_param ||
1480 !icd->ops->set_bus_param)
1483 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1485 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1489 /* Restore device type, possibly set by the subdevice driver */
1490 icd->vdev->dev.type = type;
1495 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1497 struct soc_camera_link *icl = pdev->dev.platform_data;
1498 struct soc_camera_device *icd;
1504 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1508 icd->iface = icl->bus_id;
1509 icd->pdev = &pdev->dev;
1510 platform_set_drvdata(pdev, icd);
1512 ret = soc_camera_device_register(icd);
1516 soc_camera_device_init(&icd->dev, icl);
1518 icd->user_width = DEFAULT_WIDTH;
1519 icd->user_height = DEFAULT_HEIGHT;
1530 * Only called on rmmod for each platform device, since they are not
1531 * hot-pluggable. Now we know, that all our users - hosts and devices have
1532 * been unloaded already
1534 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1536 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1541 soc_camera_device_unregister(icd);
1548 static struct platform_driver __refdata soc_camera_pdrv = {
1549 .remove = __devexit_p(soc_camera_pdrv_remove),
1551 .name = "soc-camera-pdrv",
1552 .owner = THIS_MODULE,
1556 static int __init soc_camera_init(void)
1558 int ret = bus_register(&soc_camera_bus_type);
1561 ret = driver_register(&ic_drv);
1565 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1572 driver_unregister(&ic_drv);
1574 bus_unregister(&soc_camera_bus_type);
1578 static void __exit soc_camera_exit(void)
1580 platform_driver_unregister(&soc_camera_pdrv);
1581 driver_unregister(&ic_drv);
1582 bus_unregister(&soc_camera_bus_type);
1585 module_init(soc_camera_init);
1586 module_exit(soc_camera_exit);
1588 MODULE_DESCRIPTION("Image capture bus driver");
1589 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1590 MODULE_LICENSE("GPL");
1591 MODULE_ALIAS("platform:soc-camera-pdrv");