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/soc_mediabus.h>
39 /* Default to VGA resolution */
40 #define DEFAULT_WIDTH 640
41 #define DEFAULT_HEIGHT 480
43 static LIST_HEAD(hosts);
44 static LIST_HEAD(devices);
45 static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
47 static int soc_camera_power_set(struct soc_camera_device *icd,
48 struct soc_camera_link *icl,
54 ret = regulator_bulk_enable(icl->num_regulators,
57 dev_err(&icd->dev, "Cannot enable regulators\n");
62 ret = icl->power(icd->pdev, power_on);
65 "Platform failed to power-on the camera.\n");
67 regulator_bulk_disable(icl->num_regulators,
74 ret = icl->power(icd->pdev, 0);
77 "Platform failed to power-off the camera.\n");
81 ret = regulator_bulk_disable(icl->num_regulators,
84 dev_err(&icd->dev, "Cannot disable regulators\n");
92 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
93 struct soc_camera_device *icd, unsigned int fourcc)
97 for (i = 0; i < icd->num_user_formats; i++)
98 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
99 return icd->user_formats + i;
102 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
105 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
106 * @icl: camera platform parameters
107 * @flags: flags to be inverted according to platform configuration
108 * @return: resulting flags
110 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
115 /* If only one of the two polarities is supported, switch to the opposite */
116 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
117 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
118 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
119 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
122 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
123 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
124 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
125 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
128 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
129 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
130 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
131 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
136 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
138 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
139 struct v4l2_format *f)
141 struct soc_camera_device *icd = file->private_data;
142 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
144 WARN_ON(priv != file->private_data);
146 /* limit format to hardware capabilities */
147 return ici->ops->try_fmt(icd, f);
150 static int soc_camera_enum_input(struct file *file, void *priv,
151 struct v4l2_input *inp)
153 struct soc_camera_device *icd = file->private_data;
159 if (icd->ops->enum_input)
160 ret = icd->ops->enum_input(icd, inp);
162 /* default is camera */
163 inp->type = V4L2_INPUT_TYPE_CAMERA;
164 inp->std = V4L2_STD_UNKNOWN;
165 strcpy(inp->name, "Camera");
171 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
178 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
186 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
188 struct soc_camera_device *icd = file->private_data;
189 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
191 return v4l2_subdev_call(sd, core, s_std, *a);
194 static int soc_camera_enum_fsizes(struct file *file, void *fh,
195 struct v4l2_frmsizeenum *fsize)
197 struct soc_camera_device *icd = file->private_data;
198 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
200 return ici->ops->enum_fsizes(icd, fsize);
203 static int soc_camera_reqbufs(struct file *file, void *priv,
204 struct v4l2_requestbuffers *p)
207 struct soc_camera_device *icd = file->private_data;
208 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
210 WARN_ON(priv != file->private_data);
212 if (icd->streamer && icd->streamer != file)
215 ret = videobuf_reqbufs(&icd->vb_vidq, p);
219 ret = ici->ops->reqbufs(icd, p);
220 if (!ret && !icd->streamer)
221 icd->streamer = file;
226 static int soc_camera_querybuf(struct file *file, void *priv,
227 struct v4l2_buffer *p)
229 struct soc_camera_device *icd = file->private_data;
231 WARN_ON(priv != file->private_data);
233 return videobuf_querybuf(&icd->vb_vidq, p);
236 static int soc_camera_qbuf(struct file *file, void *priv,
237 struct v4l2_buffer *p)
239 struct soc_camera_device *icd = file->private_data;
241 WARN_ON(priv != file->private_data);
243 if (icd->streamer != file)
246 return videobuf_qbuf(&icd->vb_vidq, p);
249 static int soc_camera_dqbuf(struct file *file, void *priv,
250 struct v4l2_buffer *p)
252 struct soc_camera_device *icd = file->private_data;
254 WARN_ON(priv != file->private_data);
256 if (icd->streamer != file)
259 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
262 /* Always entered with .video_lock held */
263 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
265 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
266 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
267 unsigned int i, fmts = 0, raw_fmts = 0;
269 enum v4l2_mbus_pixelcode code;
271 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
274 if (!ici->ops->get_formats)
276 * Fallback mode - the host will have to serve all
277 * sensor-provided formats one-to-one to the user
282 * First pass - only count formats this host-sensor
283 * configuration can provide
285 for (i = 0; i < raw_fmts; i++) {
286 ret = ici->ops->get_formats(icd, i, NULL);
296 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
297 if (!icd->user_formats)
300 icd->num_user_formats = fmts;
302 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
304 /* Second pass - actually fill data formats */
306 for (i = 0; i < raw_fmts; i++)
307 if (!ici->ops->get_formats) {
308 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
309 icd->user_formats[i].host_fmt =
310 soc_mbus_get_fmtdesc(code);
311 icd->user_formats[i].code = code;
313 ret = ici->ops->get_formats(icd, i,
314 &icd->user_formats[fmts]);
320 icd->current_fmt = &icd->user_formats[0];
325 icd->num_user_formats = 0;
326 vfree(icd->user_formats);
330 /* Always entered with .video_lock held */
331 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
333 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
335 if (ici->ops->put_formats)
336 ici->ops->put_formats(icd);
337 icd->current_fmt = NULL;
338 icd->num_user_formats = 0;
339 vfree(icd->user_formats);
340 icd->user_formats = NULL;
343 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
346 /* Called with .vb_lock held, or from the first open(2), see comment there */
347 static int soc_camera_set_fmt(struct soc_camera_device *icd,
348 struct v4l2_format *f)
350 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
351 struct v4l2_pix_format *pix = &f->fmt.pix;
354 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
355 pixfmtstr(pix->pixelformat), pix->width, pix->height);
357 /* We always call try_fmt() before set_fmt() or set_crop() */
358 ret = ici->ops->try_fmt(icd, f);
362 ret = ici->ops->set_fmt(icd, f);
365 } else if (!icd->current_fmt ||
366 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
368 "Host driver hasn't set up current format correctly!\n");
372 icd->user_width = pix->width;
373 icd->user_height = pix->height;
374 icd->colorspace = pix->colorspace;
376 icd->field = pix->field;
378 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
379 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
382 dev_dbg(&icd->dev, "set width: %d height: %d\n",
383 icd->user_width, icd->user_height);
385 /* set physical bus parameters */
386 return ici->ops->set_bus_param(icd, pix->pixelformat);
389 static int soc_camera_open(struct file *file)
391 struct video_device *vdev = video_devdata(file);
392 struct soc_camera_device *icd = container_of(vdev->parent,
393 struct soc_camera_device,
395 struct soc_camera_link *icl = to_soc_camera_link(icd);
396 struct soc_camera_host *ici;
400 /* No device driver attached */
403 ici = to_soc_camera_host(icd->dev.parent);
405 if (!try_module_get(ici->ops->owner)) {
406 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
412 /* Now we really have to activate the camera */
413 if (icd->use_count == 1) {
414 /* Restore parameters before the last close() per V4L2 API */
415 struct v4l2_format f = {
416 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
418 .width = icd->user_width,
419 .height = icd->user_height,
421 .colorspace = icd->colorspace,
423 icd->current_fmt->host_fmt->fourcc,
427 ret = soc_camera_power_set(icd, icl, 1);
431 /* The camera could have been already on, try to reset */
433 icl->reset(icd->pdev);
435 ret = ici->ops->add(icd);
437 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
441 pm_runtime_enable(&icd->vdev->dev);
442 ret = pm_runtime_resume(&icd->vdev->dev);
443 if (ret < 0 && ret != -ENOSYS)
447 * Try to configure with default parameters. Notice: this is the
448 * very first open, so, we cannot race against other calls,
449 * apart from someone else calling open() simultaneously, but
450 * .video_lock is protecting us against it.
452 ret = soc_camera_set_fmt(icd, &f);
456 ici->ops->init_videobuf(&icd->vb_vidq, icd);
459 file->private_data = icd;
460 dev_dbg(&icd->dev, "camera device open\n");
465 * First four errors are entered with the .video_lock held
469 pm_runtime_disable(&icd->vdev->dev);
471 ici->ops->remove(icd);
473 soc_camera_power_set(icd, icl, 0);
476 module_put(ici->ops->owner);
481 static int soc_camera_close(struct file *file)
483 struct soc_camera_device *icd = file->private_data;
484 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
487 if (!icd->use_count) {
488 struct soc_camera_link *icl = to_soc_camera_link(icd);
490 pm_runtime_suspend(&icd->vdev->dev);
491 pm_runtime_disable(&icd->vdev->dev);
493 ici->ops->remove(icd);
495 soc_camera_power_set(icd, icl, 0);
498 if (icd->streamer == file)
499 icd->streamer = NULL;
501 module_put(ici->ops->owner);
503 dev_dbg(&icd->dev, "camera device close\n");
508 static ssize_t soc_camera_read(struct file *file, char __user *buf,
509 size_t count, loff_t *ppos)
511 struct soc_camera_device *icd = file->private_data;
514 dev_err(&icd->dev, "camera device read not implemented\n");
519 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
521 struct soc_camera_device *icd = file->private_data;
524 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
526 if (icd->streamer != file)
529 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
531 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
532 (unsigned long)vma->vm_start,
533 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
539 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
541 struct soc_camera_device *icd = file->private_data;
542 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
544 if (icd->streamer != file)
547 if (list_empty(&icd->vb_vidq.stream)) {
548 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
552 return ici->ops->poll(file, pt);
555 static struct v4l2_file_operations soc_camera_fops = {
556 .owner = THIS_MODULE,
557 .open = soc_camera_open,
558 .release = soc_camera_close,
559 .unlocked_ioctl = video_ioctl2,
560 .read = soc_camera_read,
561 .mmap = soc_camera_mmap,
562 .poll = soc_camera_poll,
565 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
566 struct v4l2_format *f)
568 struct soc_camera_device *icd = file->private_data;
571 WARN_ON(priv != file->private_data);
573 if (icd->streamer && icd->streamer != file)
576 if (icd->vb_vidq.bufs[0]) {
577 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
581 ret = soc_camera_set_fmt(icd, f);
583 if (!ret && !icd->streamer)
584 icd->streamer = file;
589 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
590 struct v4l2_fmtdesc *f)
592 struct soc_camera_device *icd = file->private_data;
593 const struct soc_mbus_pixelfmt *format;
595 WARN_ON(priv != file->private_data);
597 if (f->index >= icd->num_user_formats)
600 format = icd->user_formats[f->index].host_fmt;
603 strlcpy(f->description, format->name, sizeof(f->description));
604 f->pixelformat = format->fourcc;
608 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
609 struct v4l2_format *f)
611 struct soc_camera_device *icd = file->private_data;
612 struct v4l2_pix_format *pix = &f->fmt.pix;
614 WARN_ON(priv != file->private_data);
616 pix->width = icd->user_width;
617 pix->height = icd->user_height;
618 pix->field = icd->vb_vidq.field;
619 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
620 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
621 icd->current_fmt->host_fmt);
622 pix->colorspace = icd->colorspace;
623 if (pix->bytesperline < 0)
624 return pix->bytesperline;
625 pix->sizeimage = pix->height * pix->bytesperline;
626 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
627 icd->current_fmt->host_fmt->fourcc);
631 static int soc_camera_querycap(struct file *file, void *priv,
632 struct v4l2_capability *cap)
634 struct soc_camera_device *icd = file->private_data;
635 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
637 WARN_ON(priv != file->private_data);
639 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
640 return ici->ops->querycap(ici, cap);
643 static int soc_camera_streamon(struct file *file, void *priv,
644 enum v4l2_buf_type i)
646 struct soc_camera_device *icd = file->private_data;
647 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
650 WARN_ON(priv != file->private_data);
652 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
655 if (icd->streamer != file)
658 /* This calls buf_queue from host driver's videobuf_queue_ops */
659 ret = videobuf_streamon(&icd->vb_vidq);
661 v4l2_subdev_call(sd, video, s_stream, 1);
666 static int soc_camera_streamoff(struct file *file, void *priv,
667 enum v4l2_buf_type i)
669 struct soc_camera_device *icd = file->private_data;
670 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
672 WARN_ON(priv != file->private_data);
674 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
677 if (icd->streamer != file)
681 * This calls buf_release from host driver's videobuf_queue_ops for all
682 * remaining buffers. When the last buffer is freed, stop capture
684 videobuf_streamoff(&icd->vb_vidq);
686 v4l2_subdev_call(sd, video, s_stream, 0);
691 static int soc_camera_queryctrl(struct file *file, void *priv,
692 struct v4l2_queryctrl *qc)
694 struct soc_camera_device *icd = file->private_data;
695 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
698 WARN_ON(priv != file->private_data);
703 /* First check host controls */
704 for (i = 0; i < ici->ops->num_controls; i++)
705 if (qc->id == ici->ops->controls[i].id) {
706 memcpy(qc, &(ici->ops->controls[i]),
711 /* Then device controls */
712 for (i = 0; i < icd->ops->num_controls; i++)
713 if (qc->id == icd->ops->controls[i].id) {
714 memcpy(qc, &(icd->ops->controls[i]),
722 static int soc_camera_g_ctrl(struct file *file, void *priv,
723 struct v4l2_control *ctrl)
725 struct soc_camera_device *icd = file->private_data;
726 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
727 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
730 WARN_ON(priv != file->private_data);
732 if (ici->ops->get_ctrl) {
733 ret = ici->ops->get_ctrl(icd, ctrl);
734 if (ret != -ENOIOCTLCMD)
738 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
741 static int soc_camera_s_ctrl(struct file *file, void *priv,
742 struct v4l2_control *ctrl)
744 struct soc_camera_device *icd = file->private_data;
745 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
746 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
749 WARN_ON(priv != file->private_data);
751 if (ici->ops->set_ctrl) {
752 ret = ici->ops->set_ctrl(icd, ctrl);
753 if (ret != -ENOIOCTLCMD)
757 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
760 static int soc_camera_cropcap(struct file *file, void *fh,
761 struct v4l2_cropcap *a)
763 struct soc_camera_device *icd = file->private_data;
764 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
766 return ici->ops->cropcap(icd, a);
769 static int soc_camera_g_crop(struct file *file, void *fh,
772 struct soc_camera_device *icd = file->private_data;
773 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
776 ret = ici->ops->get_crop(icd, a);
782 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
783 * argument with the actual geometry, instead, the user shall use G_CROP to
786 static int soc_camera_s_crop(struct file *file, void *fh,
789 struct soc_camera_device *icd = file->private_data;
790 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
791 struct v4l2_rect *rect = &a->c;
792 struct v4l2_crop current_crop;
795 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
798 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
799 rect->width, rect->height, rect->left, rect->top);
801 /* If get_crop fails, we'll let host and / or client drivers decide */
802 ret = ici->ops->get_crop(icd, ¤t_crop);
804 /* Prohibit window size change with initialised buffers */
807 "S_CROP denied: getting current crop failed\n");
808 } else if (icd->vb_vidq.bufs[0] &&
809 (a->c.width != current_crop.c.width ||
810 a->c.height != current_crop.c.height)) {
812 "S_CROP denied: queue initialised and sizes differ\n");
815 ret = ici->ops->set_crop(icd, a);
821 static int soc_camera_g_parm(struct file *file, void *fh,
822 struct v4l2_streamparm *a)
824 struct soc_camera_device *icd = file->private_data;
825 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
827 if (ici->ops->get_parm)
828 return ici->ops->get_parm(icd, a);
833 static int soc_camera_s_parm(struct file *file, void *fh,
834 struct v4l2_streamparm *a)
836 struct soc_camera_device *icd = file->private_data;
837 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
839 if (ici->ops->set_parm)
840 return ici->ops->set_parm(icd, a);
845 static int soc_camera_g_chip_ident(struct file *file, void *fh,
846 struct v4l2_dbg_chip_ident *id)
848 struct soc_camera_device *icd = file->private_data;
849 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
851 return v4l2_subdev_call(sd, core, g_chip_ident, id);
854 #ifdef CONFIG_VIDEO_ADV_DEBUG
855 static int soc_camera_g_register(struct file *file, void *fh,
856 struct v4l2_dbg_register *reg)
858 struct soc_camera_device *icd = file->private_data;
859 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
861 return v4l2_subdev_call(sd, core, g_register, reg);
864 static int soc_camera_s_register(struct file *file, void *fh,
865 struct v4l2_dbg_register *reg)
867 struct soc_camera_device *icd = file->private_data;
868 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
870 return v4l2_subdev_call(sd, core, s_register, reg);
874 /* So far this function cannot fail */
875 static void scan_add_host(struct soc_camera_host *ici)
877 struct soc_camera_device *icd;
879 mutex_lock(&list_lock);
881 list_for_each_entry(icd, &devices, list) {
882 if (icd->iface == ici->nr) {
884 icd->dev.parent = ici->v4l2_dev.dev;
885 dev_set_name(&icd->dev, "%u-%u", icd->iface,
887 ret = device_register(&icd->dev);
889 icd->dev.parent = NULL;
891 "Cannot register device: %d\n", ret);
896 mutex_unlock(&list_lock);
899 #ifdef CONFIG_I2C_BOARDINFO
900 static int soc_camera_init_i2c(struct soc_camera_device *icd,
901 struct soc_camera_link *icl)
903 struct i2c_client *client;
904 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
905 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
906 struct v4l2_subdev *subdev;
909 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
910 icl->i2c_adapter_id);
914 icl->board_info->platform_data = icd;
916 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
917 icl->board_info, NULL);
921 client = v4l2_get_subdevdata(subdev);
923 /* Use to_i2c_client(dev) to recover the i2c client */
924 dev_set_drvdata(&icd->dev, &client->dev);
928 i2c_put_adapter(adap);
933 static void soc_camera_free_i2c(struct soc_camera_device *icd)
935 struct i2c_client *client =
936 to_i2c_client(to_soc_camera_control(icd));
937 dev_set_drvdata(&icd->dev, NULL);
938 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
939 i2c_unregister_device(client);
940 i2c_put_adapter(client->adapter);
943 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
944 #define soc_camera_free_i2c(icd) do {} while (0)
947 static int soc_camera_video_start(struct soc_camera_device *icd);
948 static int video_dev_create(struct soc_camera_device *icd);
949 /* Called during host-driver probe */
950 static int soc_camera_probe(struct device *dev)
952 struct soc_camera_device *icd = to_soc_camera_dev(dev);
953 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
954 struct soc_camera_link *icl = to_soc_camera_link(icd);
955 struct device *control = NULL;
956 struct v4l2_subdev *sd;
957 struct v4l2_mbus_framefmt mf;
960 dev_info(dev, "Probing %s\n", dev_name(dev));
962 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
967 ret = soc_camera_power_set(icd, icl, 1);
971 /* The camera could have been already on, try to reset */
973 icl->reset(icd->pdev);
975 ret = ici->ops->add(icd);
979 /* Must have icd->vdev before registering the device */
980 ret = video_dev_create(icd);
984 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
985 if (icl->board_info) {
986 ret = soc_camera_init_i2c(icd, icl);
989 } else if (!icl->add_device || !icl->del_device) {
993 if (icl->module_name)
994 ret = request_module(icl->module_name);
996 ret = icl->add_device(icl, &icd->dev);
1001 * FIXME: this is racy, have to use driver-binding notification,
1002 * when it is available
1004 control = to_soc_camera_control(icd);
1005 if (!control || !control->driver || !dev_get_drvdata(control) ||
1006 !try_module_get(control->driver->owner)) {
1007 icl->del_device(icl);
1012 /* At this point client .probe() should have run already */
1013 ret = soc_camera_init_user_formats(icd);
1017 icd->field = V4L2_FIELD_ANY;
1019 icd->vdev->lock = &icd->video_lock;
1022 * ..._video_start() will create a device node, video_register_device()
1023 * itself is protected against concurrent open() calls, but we also have
1024 * to protect our data.
1026 mutex_lock(&icd->video_lock);
1028 ret = soc_camera_video_start(icd);
1032 /* Try to improve our guess of a reasonable window format */
1033 sd = soc_camera_to_subdev(icd);
1034 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1035 icd->user_width = mf.width;
1036 icd->user_height = mf.height;
1037 icd->colorspace = mf.colorspace;
1038 icd->field = mf.field;
1041 /* Do we have to sysfs_remove_link() before device_unregister()? */
1042 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
1044 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1046 ici->ops->remove(icd);
1048 soc_camera_power_set(icd, icl, 0);
1050 mutex_unlock(&icd->video_lock);
1055 mutex_unlock(&icd->video_lock);
1056 soc_camera_free_user_formats(icd);
1058 if (icl->board_info) {
1059 soc_camera_free_i2c(icd);
1061 icl->del_device(icl);
1062 module_put(control->driver->owner);
1066 video_device_release(icd->vdev);
1068 ici->ops->remove(icd);
1070 soc_camera_power_set(icd, icl, 0);
1072 regulator_bulk_free(icl->num_regulators, icl->regulators);
1078 * This is called on device_unregister, which only means we have to disconnect
1079 * from the host, but not remove ourselves from the device list
1081 static int soc_camera_remove(struct device *dev)
1083 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1084 struct soc_camera_link *icl = to_soc_camera_link(icd);
1085 struct video_device *vdev = icd->vdev;
1087 BUG_ON(!dev->parent);
1090 video_unregister_device(vdev);
1094 if (icl->board_info) {
1095 soc_camera_free_i2c(icd);
1097 struct device_driver *drv = to_soc_camera_control(icd) ?
1098 to_soc_camera_control(icd)->driver : NULL;
1100 icl->del_device(icl);
1101 module_put(drv->owner);
1104 soc_camera_free_user_formats(icd);
1106 regulator_bulk_free(icl->num_regulators, icl->regulators);
1111 static int soc_camera_suspend(struct device *dev, pm_message_t state)
1113 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1114 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1117 if (ici->ops->suspend)
1118 ret = ici->ops->suspend(icd, state);
1123 static int soc_camera_resume(struct device *dev)
1125 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1126 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1129 if (ici->ops->resume)
1130 ret = ici->ops->resume(icd);
1135 struct bus_type soc_camera_bus_type = {
1136 .name = "soc-camera",
1137 .probe = soc_camera_probe,
1138 .remove = soc_camera_remove,
1139 .suspend = soc_camera_suspend,
1140 .resume = soc_camera_resume,
1142 EXPORT_SYMBOL_GPL(soc_camera_bus_type);
1144 static struct device_driver ic_drv = {
1146 .bus = &soc_camera_bus_type,
1147 .owner = THIS_MODULE,
1150 static void dummy_release(struct device *dev)
1154 static int default_cropcap(struct soc_camera_device *icd,
1155 struct v4l2_cropcap *a)
1157 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1158 return v4l2_subdev_call(sd, video, cropcap, a);
1161 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1163 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1164 return v4l2_subdev_call(sd, video, g_crop, a);
1167 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1169 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1170 return v4l2_subdev_call(sd, video, s_crop, a);
1173 static int default_g_parm(struct soc_camera_device *icd,
1174 struct v4l2_streamparm *parm)
1176 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1177 return v4l2_subdev_call(sd, video, g_parm, parm);
1180 static int default_s_parm(struct soc_camera_device *icd,
1181 struct v4l2_streamparm *parm)
1183 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1184 return v4l2_subdev_call(sd, video, s_parm, parm);
1187 static int default_enum_fsizes(struct soc_camera_device *icd,
1188 struct v4l2_frmsizeenum *fsize)
1191 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1192 const struct soc_camera_format_xlate *xlate;
1193 __u32 pixfmt = fsize->pixel_format;
1194 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1196 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1199 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1200 fsize_mbus.pixel_format = xlate->code;
1202 ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
1206 *fsize = fsize_mbus;
1207 fsize->pixel_format = pixfmt;
1212 static void soc_camera_device_init(struct device *dev, void *pdata)
1214 dev->platform_data = pdata;
1215 dev->bus = &soc_camera_bus_type;
1216 dev->release = dummy_release;
1219 int soc_camera_host_register(struct soc_camera_host *ici)
1221 struct soc_camera_host *ix;
1224 if (!ici || !ici->ops ||
1225 !ici->ops->try_fmt ||
1226 !ici->ops->set_fmt ||
1227 !ici->ops->set_bus_param ||
1228 !ici->ops->querycap ||
1229 !ici->ops->init_videobuf ||
1230 !ici->ops->reqbufs ||
1232 !ici->ops->remove ||
1237 if (!ici->ops->set_crop)
1238 ici->ops->set_crop = default_s_crop;
1239 if (!ici->ops->get_crop)
1240 ici->ops->get_crop = default_g_crop;
1241 if (!ici->ops->cropcap)
1242 ici->ops->cropcap = default_cropcap;
1243 if (!ici->ops->set_parm)
1244 ici->ops->set_parm = default_s_parm;
1245 if (!ici->ops->get_parm)
1246 ici->ops->get_parm = default_g_parm;
1247 if (!ici->ops->enum_fsizes)
1248 ici->ops->enum_fsizes = default_enum_fsizes;
1250 mutex_lock(&list_lock);
1251 list_for_each_entry(ix, &hosts, list) {
1252 if (ix->nr == ici->nr) {
1258 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1262 list_add_tail(&ici->list, &hosts);
1263 mutex_unlock(&list_lock);
1270 mutex_unlock(&list_lock);
1273 EXPORT_SYMBOL(soc_camera_host_register);
1275 /* Unregister all clients! */
1276 void soc_camera_host_unregister(struct soc_camera_host *ici)
1278 struct soc_camera_device *icd;
1280 mutex_lock(&list_lock);
1282 list_del(&ici->list);
1284 list_for_each_entry(icd, &devices, list) {
1285 if (icd->iface == ici->nr) {
1286 void *pdata = icd->dev.platform_data;
1287 /* The bus->remove will be called */
1288 device_unregister(&icd->dev);
1290 * Not before device_unregister(), .remove
1291 * needs parent to call ici->ops->remove().
1292 * If the host module is loaded again, device_register()
1293 * would complain "already initialised," since 2.6.32
1294 * this is also needed to prevent use-after-free of the
1295 * device private data.
1297 memset(&icd->dev, 0, sizeof(icd->dev));
1298 soc_camera_device_init(&icd->dev, pdata);
1302 mutex_unlock(&list_lock);
1304 v4l2_device_unregister(&ici->v4l2_dev);
1306 EXPORT_SYMBOL(soc_camera_host_unregister);
1308 /* Image capture device */
1309 static int soc_camera_device_register(struct soc_camera_device *icd)
1311 struct soc_camera_device *ix;
1314 for (i = 0; i < 256 && num < 0; i++) {
1316 /* Check if this index is available on this interface */
1317 list_for_each_entry(ix, &devices, list) {
1318 if (ix->iface == icd->iface && ix->devnum == i) {
1327 * ok, we have 256 cameras on this host...
1328 * man, stay reasonable...
1334 icd->host_priv = NULL;
1335 mutex_init(&icd->video_lock);
1337 list_add_tail(&icd->list, &devices);
1342 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1344 list_del(&icd->list);
1347 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1348 .vidioc_querycap = soc_camera_querycap,
1349 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1350 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1351 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1352 .vidioc_enum_input = soc_camera_enum_input,
1353 .vidioc_g_input = soc_camera_g_input,
1354 .vidioc_s_input = soc_camera_s_input,
1355 .vidioc_s_std = soc_camera_s_std,
1356 .vidioc_enum_framesizes = soc_camera_enum_fsizes,
1357 .vidioc_reqbufs = soc_camera_reqbufs,
1358 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1359 .vidioc_querybuf = soc_camera_querybuf,
1360 .vidioc_qbuf = soc_camera_qbuf,
1361 .vidioc_dqbuf = soc_camera_dqbuf,
1362 .vidioc_streamon = soc_camera_streamon,
1363 .vidioc_streamoff = soc_camera_streamoff,
1364 .vidioc_queryctrl = soc_camera_queryctrl,
1365 .vidioc_g_ctrl = soc_camera_g_ctrl,
1366 .vidioc_s_ctrl = soc_camera_s_ctrl,
1367 .vidioc_cropcap = soc_camera_cropcap,
1368 .vidioc_g_crop = soc_camera_g_crop,
1369 .vidioc_s_crop = soc_camera_s_crop,
1370 .vidioc_g_parm = soc_camera_g_parm,
1371 .vidioc_s_parm = soc_camera_s_parm,
1372 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1373 #ifdef CONFIG_VIDEO_ADV_DEBUG
1374 .vidioc_g_register = soc_camera_g_register,
1375 .vidioc_s_register = soc_camera_s_register,
1379 static int video_dev_create(struct soc_camera_device *icd)
1381 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1382 struct video_device *vdev = video_device_alloc();
1387 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1389 vdev->parent = &icd->dev;
1390 vdev->current_norm = V4L2_STD_UNKNOWN;
1391 vdev->fops = &soc_camera_fops;
1392 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1393 vdev->release = video_device_release;
1394 vdev->tvnorms = V4L2_STD_UNKNOWN;
1402 * Called from soc_camera_probe() above (with .video_lock held???)
1404 static int soc_camera_video_start(struct soc_camera_device *icd)
1406 struct device_type *type = icd->vdev->dev.type;
1409 if (!icd->dev.parent)
1413 !icd->ops->query_bus_param ||
1414 !icd->ops->set_bus_param)
1417 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1419 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1423 /* Restore device type, possibly set by the subdevice driver */
1424 icd->vdev->dev.type = type;
1429 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1431 struct soc_camera_link *icl = pdev->dev.platform_data;
1432 struct soc_camera_device *icd;
1438 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1442 icd->iface = icl->bus_id;
1443 icd->pdev = &pdev->dev;
1444 platform_set_drvdata(pdev, icd);
1446 ret = soc_camera_device_register(icd);
1450 soc_camera_device_init(&icd->dev, icl);
1452 icd->user_width = DEFAULT_WIDTH;
1453 icd->user_height = DEFAULT_HEIGHT;
1464 * Only called on rmmod for each platform device, since they are not
1465 * hot-pluggable. Now we know, that all our users - hosts and devices have
1466 * been unloaded already
1468 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1470 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1475 soc_camera_device_unregister(icd);
1482 static struct platform_driver __refdata soc_camera_pdrv = {
1483 .remove = __devexit_p(soc_camera_pdrv_remove),
1485 .name = "soc-camera-pdrv",
1486 .owner = THIS_MODULE,
1490 static int __init soc_camera_init(void)
1492 int ret = bus_register(&soc_camera_bus_type);
1495 ret = driver_register(&ic_drv);
1499 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1506 driver_unregister(&ic_drv);
1508 bus_unregister(&soc_camera_bus_type);
1512 static void __exit soc_camera_exit(void)
1514 platform_driver_unregister(&soc_camera_pdrv);
1515 driver_unregister(&ic_drv);
1516 bus_unregister(&soc_camera_bus_type);
1519 module_init(soc_camera_init);
1520 module_exit(soc_camera_exit);
1522 MODULE_DESCRIPTION("Image capture bus driver");
1523 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1524 MODULE_LICENSE("GPL");
1525 MODULE_ALIAS("platform:soc-camera-pdrv");