1 // SPDX-License-Identifier: GPL-2.0
3 * camera image capture (abstract) bus driver
5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
7 * This driver provides an interface between platform-specific camera
8 * buses and camera devices. It should be used if the camera is
9 * connected not over a "proper" bus like PCI or USB, but over a
10 * special bus, like, for example, the Quick Capture interface on PXA270
11 * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
12 * It can handle multiple cameras and / or multiple buses, which can
13 * be used, e.g., in stereo-vision applications.
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/of_graph.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
29 #include <media/soc_camera.h>
30 #include <media/drv-intf/soc_mediabus.h>
31 #include <media/v4l2-async.h>
32 #include <media/v4l2-clk.h>
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-dev.h>
36 #include <media/v4l2-fwnode.h>
37 #include <media/videobuf2-v4l2.h>
39 /* Default to VGA resolution */
40 #define DEFAULT_WIDTH 640
41 #define DEFAULT_HEIGHT 480
43 #define MAP_MAX_NUM 32
44 static DECLARE_BITMAP(device_map, MAP_MAX_NUM);
45 static LIST_HEAD(hosts);
46 static LIST_HEAD(devices);
48 * Protects lists and bitmaps of hosts and devices.
49 * Lock nesting: Ok to take ->host_lock under list_lock.
51 static DEFINE_MUTEX(list_lock);
53 struct soc_camera_async_client {
54 struct v4l2_async_subdev *sensor;
55 struct v4l2_async_notifier notifier;
56 struct platform_device *pdev;
57 struct list_head list; /* needed for clean up */
60 static int soc_camera_video_start(struct soc_camera_device *icd);
61 static int video_dev_create(struct soc_camera_device *icd);
63 int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
69 if (clk && (!ssdd->unbalanced_power ||
70 !test_and_set_bit(0, &ssdd->clock_state))) {
71 ret = v4l2_clk_enable(clk);
73 dev_err(dev, "Cannot enable clock: %d\n", ret);
81 ret = regulator_bulk_enable(ssdd->sd_pdata.num_regulators,
82 ssdd->sd_pdata.regulators);
84 dev_err(dev, "Cannot enable regulators\n");
89 ret = ssdd->power(dev, 1);
92 "Platform failed to power-on the camera.\n");
100 regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
101 ssdd->sd_pdata.regulators);
104 v4l2_clk_disable(clk);
108 EXPORT_SYMBOL(soc_camera_power_on);
110 int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
111 struct v4l2_clk *clk)
117 err = ssdd->power(dev, 0);
120 "Platform failed to power-off the camera.\n");
125 err = regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
126 ssdd->sd_pdata.regulators);
128 dev_err(dev, "Cannot disable regulators\n");
132 if (clk && (!ssdd->unbalanced_power || test_and_clear_bit(0, &ssdd->clock_state)))
133 v4l2_clk_disable(clk);
137 EXPORT_SYMBOL(soc_camera_power_off);
139 int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd)
141 /* Should not have any effect in synchronous case */
142 return devm_regulator_bulk_get(dev, ssdd->sd_pdata.num_regulators,
143 ssdd->sd_pdata.regulators);
145 EXPORT_SYMBOL(soc_camera_power_init);
147 static int __soc_camera_power_on(struct soc_camera_device *icd)
149 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
152 ret = v4l2_subdev_call(sd, core, s_power, 1);
153 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
159 static int __soc_camera_power_off(struct soc_camera_device *icd)
161 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
164 ret = v4l2_subdev_call(sd, core, s_power, 0);
165 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
171 static int soc_camera_clock_start(struct soc_camera_host *ici)
175 if (!ici->ops->clock_start)
178 mutex_lock(&ici->clk_lock);
179 ret = ici->ops->clock_start(ici);
180 mutex_unlock(&ici->clk_lock);
185 static void soc_camera_clock_stop(struct soc_camera_host *ici)
187 if (!ici->ops->clock_stop)
190 mutex_lock(&ici->clk_lock);
191 ici->ops->clock_stop(ici);
192 mutex_unlock(&ici->clk_lock);
195 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
196 struct soc_camera_device *icd, unsigned int fourcc)
200 for (i = 0; i < icd->num_user_formats; i++)
201 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
202 return icd->user_formats + i;
205 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
208 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
209 * @ssdd: camera platform parameters
210 * @cfg: media bus configuration
211 * @return: resulting flags
213 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
214 const struct v4l2_mbus_config *cfg)
216 unsigned long f, flags = cfg->flags;
218 /* If only one of the two polarities is supported, switch to the opposite */
219 if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
220 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
221 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
222 flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
225 if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
226 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
227 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
228 flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
231 if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
232 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
233 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
234 flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
239 EXPORT_SYMBOL(soc_camera_apply_board_flags);
241 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
244 static int soc_camera_try_fmt(struct soc_camera_device *icd,
245 struct v4l2_format *f)
247 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
248 const struct soc_camera_format_xlate *xlate;
249 struct v4l2_pix_format *pix = &f->fmt.pix;
252 dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
253 pixfmtstr(pix->pixelformat), pix->width, pix->height);
255 if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
256 !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
257 pix->bytesperline = 0;
261 ret = ici->ops->try_fmt(icd, f);
265 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
269 ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
273 pix->bytesperline = max_t(u32, pix->bytesperline, ret);
275 ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
280 pix->sizeimage = max_t(u32, pix->sizeimage, ret);
285 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
286 struct v4l2_format *f)
288 struct soc_camera_device *icd = file->private_data;
290 WARN_ON(priv != file->private_data);
292 /* Only single-plane capture is supported so far */
293 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
296 /* limit format to hardware capabilities */
297 return soc_camera_try_fmt(icd, f);
300 static int soc_camera_enum_input(struct file *file, void *priv,
301 struct v4l2_input *inp)
303 struct soc_camera_device *icd = file->private_data;
308 /* default is camera */
309 inp->type = V4L2_INPUT_TYPE_CAMERA;
310 inp->std = icd->vdev->tvnorms;
311 strscpy(inp->name, "Camera", sizeof(inp->name));
316 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
323 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
331 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
333 struct soc_camera_device *icd = file->private_data;
334 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
336 return v4l2_subdev_call(sd, video, s_std, a);
339 static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
341 struct soc_camera_device *icd = file->private_data;
342 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
344 return v4l2_subdev_call(sd, video, g_std, a);
347 static int soc_camera_enum_framesizes(struct file *file, void *fh,
348 struct v4l2_frmsizeenum *fsize)
350 struct soc_camera_device *icd = file->private_data;
351 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
353 return ici->ops->enum_framesizes(icd, fsize);
356 static int soc_camera_reqbufs(struct file *file, void *priv,
357 struct v4l2_requestbuffers *p)
360 struct soc_camera_device *icd = file->private_data;
362 WARN_ON(priv != file->private_data);
364 if (icd->streamer && icd->streamer != file)
367 ret = vb2_reqbufs(&icd->vb2_vidq, p);
369 icd->streamer = p->count ? file : NULL;
373 static int soc_camera_querybuf(struct file *file, void *priv,
374 struct v4l2_buffer *p)
376 struct soc_camera_device *icd = file->private_data;
378 WARN_ON(priv != file->private_data);
380 return vb2_querybuf(&icd->vb2_vidq, p);
383 static int soc_camera_qbuf(struct file *file, void *priv,
384 struct v4l2_buffer *p)
386 struct soc_camera_device *icd = file->private_data;
388 WARN_ON(priv != file->private_data);
390 if (icd->streamer != file)
393 return vb2_qbuf(&icd->vb2_vidq, NULL, p);
396 static int soc_camera_dqbuf(struct file *file, void *priv,
397 struct v4l2_buffer *p)
399 struct soc_camera_device *icd = file->private_data;
401 WARN_ON(priv != file->private_data);
403 if (icd->streamer != file)
406 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
409 static int soc_camera_create_bufs(struct file *file, void *priv,
410 struct v4l2_create_buffers *create)
412 struct soc_camera_device *icd = file->private_data;
415 if (icd->streamer && icd->streamer != file)
418 ret = vb2_create_bufs(&icd->vb2_vidq, create);
420 icd->streamer = file;
424 static int soc_camera_prepare_buf(struct file *file, void *priv,
425 struct v4l2_buffer *b)
427 struct soc_camera_device *icd = file->private_data;
429 return vb2_prepare_buf(&icd->vb2_vidq, NULL, b);
432 static int soc_camera_expbuf(struct file *file, void *priv,
433 struct v4l2_exportbuffer *p)
435 struct soc_camera_device *icd = file->private_data;
437 if (icd->streamer && icd->streamer != file)
439 return vb2_expbuf(&icd->vb2_vidq, p);
442 /* Always entered with .host_lock held */
443 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
445 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
446 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
447 unsigned int i, fmts = 0, raw_fmts = 0;
449 struct v4l2_subdev_mbus_code_enum code = {
450 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
453 while (!v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code)) {
458 if (!ici->ops->get_formats)
460 * Fallback mode - the host will have to serve all
461 * sensor-provided formats one-to-one to the user
466 * First pass - only count formats this host-sensor
467 * configuration can provide
469 for (i = 0; i < raw_fmts; i++) {
470 ret = ici->ops->get_formats(icd, i, NULL);
480 vmalloc(array_size(fmts,
481 sizeof(struct soc_camera_format_xlate)));
482 if (!icd->user_formats)
485 dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
487 /* Second pass - actually fill data formats */
489 for (i = 0; i < raw_fmts; i++)
490 if (!ici->ops->get_formats) {
492 v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
493 icd->user_formats[fmts].host_fmt =
494 soc_mbus_get_fmtdesc(code.code);
495 if (icd->user_formats[fmts].host_fmt)
496 icd->user_formats[fmts++].code = code.code;
498 ret = ici->ops->get_formats(icd, i,
499 &icd->user_formats[fmts]);
505 icd->num_user_formats = fmts;
506 icd->current_fmt = &icd->user_formats[0];
511 vfree(icd->user_formats);
515 /* Always entered with .host_lock held */
516 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
518 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
520 if (ici->ops->put_formats)
521 ici->ops->put_formats(icd);
522 icd->current_fmt = NULL;
523 icd->num_user_formats = 0;
524 vfree(icd->user_formats);
525 icd->user_formats = NULL;
528 /* Called with .vb_lock held, or from the first open(2), see comment there */
529 static int soc_camera_set_fmt(struct soc_camera_device *icd,
530 struct v4l2_format *f)
532 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
533 struct v4l2_pix_format *pix = &f->fmt.pix;
536 dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
537 pixfmtstr(pix->pixelformat), pix->width, pix->height);
539 /* We always call try_fmt() before set_fmt() or set_selection() */
540 ret = soc_camera_try_fmt(icd, f);
544 ret = ici->ops->set_fmt(icd, f);
547 } else if (!icd->current_fmt ||
548 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
550 "Host driver hasn't set up current format correctly!\n");
554 icd->user_width = pix->width;
555 icd->user_height = pix->height;
556 icd->bytesperline = pix->bytesperline;
557 icd->sizeimage = pix->sizeimage;
558 icd->colorspace = pix->colorspace;
559 icd->field = pix->field;
561 dev_dbg(icd->pdev, "set width: %d height: %d\n",
562 icd->user_width, icd->user_height);
564 /* set physical bus parameters */
565 return ici->ops->set_bus_param(icd);
568 static int soc_camera_add_device(struct soc_camera_device *icd)
570 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
577 ret = soc_camera_clock_start(ici);
583 ret = ici->ops->add(icd);
594 soc_camera_clock_stop(ici);
598 static void soc_camera_remove_device(struct soc_camera_device *icd)
600 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
602 if (WARN_ON(icd != ici->icd))
605 if (ici->ops->remove)
606 ici->ops->remove(icd);
608 soc_camera_clock_stop(ici);
612 static int soc_camera_open(struct file *file)
614 struct video_device *vdev = video_devdata(file);
615 struct soc_camera_device *icd;
616 struct soc_camera_host *ici;
620 * Don't mess with the host during probe: wait until the loop in
621 * scan_add_host() completes. Also protect against a race with
622 * soc_camera_host_unregister().
624 if (mutex_lock_interruptible(&list_lock))
627 if (!vdev || !video_is_registered(vdev)) {
628 mutex_unlock(&list_lock);
632 icd = video_get_drvdata(vdev);
633 ici = to_soc_camera_host(icd->parent);
635 ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
636 mutex_unlock(&list_lock);
639 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
643 if (!to_soc_camera_control(icd)) {
644 /* No device driver attached */
649 if (mutex_lock_interruptible(&ici->host_lock)) {
655 /* Now we really have to activate the camera */
656 if (icd->use_count == 1) {
657 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
658 /* Restore parameters before the last close() per V4L2 API */
659 struct v4l2_format f = {
660 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
662 .width = icd->user_width,
663 .height = icd->user_height,
665 .colorspace = icd->colorspace,
667 icd->current_fmt->host_fmt->fourcc,
671 /* The camera could have been already on, try to reset */
672 if (sdesc->subdev_desc.reset)
674 sdesc->subdev_desc.reset(icd->control);
676 ret = soc_camera_add_device(icd);
678 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
682 ret = __soc_camera_power_on(icd);
686 pm_runtime_enable(&icd->vdev->dev);
687 ret = pm_runtime_resume(&icd->vdev->dev);
688 if (ret < 0 && ret != -ENOSYS)
692 * Try to configure with default parameters. Notice: this is the
693 * very first open, so, we cannot race against other calls,
694 * apart from someone else calling open() simultaneously, but
695 * .host_lock is protecting us against it.
697 ret = soc_camera_set_fmt(icd, &f);
701 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
704 v4l2_ctrl_handler_setup(&icd->ctrl_handler);
706 mutex_unlock(&ici->host_lock);
708 file->private_data = icd;
709 dev_dbg(icd->pdev, "camera device open\n");
714 * All errors are entered with the .host_lock held, first four also
715 * with use_count == 1
719 pm_runtime_disable(&icd->vdev->dev);
721 __soc_camera_power_off(icd);
723 soc_camera_remove_device(icd);
726 mutex_unlock(&ici->host_lock);
729 module_put(ici->ops->owner);
734 static int soc_camera_close(struct file *file)
736 struct soc_camera_device *icd = file->private_data;
737 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
739 mutex_lock(&ici->host_lock);
740 if (icd->streamer == file) {
741 if (ici->ops->init_videobuf2)
742 vb2_queue_release(&icd->vb2_vidq);
743 icd->streamer = NULL;
746 if (!icd->use_count) {
747 pm_runtime_suspend(&icd->vdev->dev);
748 pm_runtime_disable(&icd->vdev->dev);
750 __soc_camera_power_off(icd);
752 soc_camera_remove_device(icd);
755 mutex_unlock(&ici->host_lock);
757 module_put(ici->ops->owner);
759 dev_dbg(icd->pdev, "camera device close\n");
764 static ssize_t soc_camera_read(struct file *file, char __user *buf,
765 size_t count, loff_t *ppos)
767 struct soc_camera_device *icd = file->private_data;
768 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
770 dev_dbg(icd->pdev, "read called, buf %p\n", buf);
772 if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
773 return vb2_read(&icd->vb2_vidq, buf, count, ppos,
774 file->f_flags & O_NONBLOCK);
776 dev_err(icd->pdev, "camera device read not implemented\n");
781 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
783 struct soc_camera_device *icd = file->private_data;
784 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
787 dev_dbg(icd->pdev, "mmap called, vma=%p\n", vma);
789 if (icd->streamer != file)
792 if (mutex_lock_interruptible(&ici->host_lock))
794 err = vb2_mmap(&icd->vb2_vidq, vma);
795 mutex_unlock(&ici->host_lock);
797 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
798 (unsigned long)vma->vm_start,
799 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
805 static __poll_t soc_camera_poll(struct file *file, poll_table *pt)
807 struct soc_camera_device *icd = file->private_data;
808 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
809 __poll_t res = EPOLLERR;
811 if (icd->streamer != file)
814 mutex_lock(&ici->host_lock);
815 res = ici->ops->poll(file, pt);
816 mutex_unlock(&ici->host_lock);
820 static const struct v4l2_file_operations soc_camera_fops = {
821 .owner = THIS_MODULE,
822 .open = soc_camera_open,
823 .release = soc_camera_close,
824 .unlocked_ioctl = video_ioctl2,
825 .read = soc_camera_read,
826 .mmap = soc_camera_mmap,
827 .poll = soc_camera_poll,
830 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
831 struct v4l2_format *f)
833 struct soc_camera_device *icd = file->private_data;
836 WARN_ON(priv != file->private_data);
838 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
839 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
843 if (icd->streamer && icd->streamer != file)
846 if (vb2_is_streaming(&icd->vb2_vidq)) {
847 dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
851 ret = soc_camera_set_fmt(icd, f);
853 if (!ret && !icd->streamer)
854 icd->streamer = file;
859 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
860 struct v4l2_fmtdesc *f)
862 struct soc_camera_device *icd = file->private_data;
863 const struct soc_mbus_pixelfmt *format;
865 WARN_ON(priv != file->private_data);
867 if (f->index >= icd->num_user_formats)
870 format = icd->user_formats[f->index].host_fmt;
873 strscpy(f->description, format->name, sizeof(f->description));
874 f->pixelformat = format->fourcc;
878 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
879 struct v4l2_format *f)
881 struct soc_camera_device *icd = file->private_data;
882 struct v4l2_pix_format *pix = &f->fmt.pix;
884 WARN_ON(priv != file->private_data);
886 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
889 pix->width = icd->user_width;
890 pix->height = icd->user_height;
891 pix->bytesperline = icd->bytesperline;
892 pix->sizeimage = icd->sizeimage;
893 pix->field = icd->field;
894 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
895 pix->colorspace = icd->colorspace;
896 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
897 icd->current_fmt->host_fmt->fourcc);
901 static int soc_camera_querycap(struct file *file, void *priv,
902 struct v4l2_capability *cap)
904 struct soc_camera_device *icd = file->private_data;
905 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
907 WARN_ON(priv != file->private_data);
909 strscpy(cap->driver, ici->drv_name, sizeof(cap->driver));
910 return ici->ops->querycap(ici, cap);
913 static int soc_camera_streamon(struct file *file, void *priv,
914 enum v4l2_buf_type i)
916 struct soc_camera_device *icd = file->private_data;
917 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
920 WARN_ON(priv != file->private_data);
922 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
925 if (icd->streamer != file)
928 /* This calls buf_queue from host driver's videobuf2_queue_ops */
929 ret = vb2_streamon(&icd->vb2_vidq, i);
931 v4l2_subdev_call(sd, video, s_stream, 1);
936 static int soc_camera_streamoff(struct file *file, void *priv,
937 enum v4l2_buf_type i)
939 struct soc_camera_device *icd = file->private_data;
940 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
943 WARN_ON(priv != file->private_data);
945 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
948 if (icd->streamer != file)
952 * This calls buf_release from host driver's videobuf2_queue_ops for all
953 * remaining buffers. When the last buffer is freed, stop capture
955 ret = vb2_streamoff(&icd->vb2_vidq, i);
957 v4l2_subdev_call(sd, video, s_stream, 0);
962 static int soc_camera_g_selection(struct file *file, void *fh,
963 struct v4l2_selection *s)
965 struct soc_camera_device *icd = file->private_data;
966 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
968 /* With a wrong type no need to try to fall back to cropping */
969 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
972 return ici->ops->get_selection(icd, s);
975 static int soc_camera_s_selection(struct file *file, void *fh,
976 struct v4l2_selection *s)
978 struct soc_camera_device *icd = file->private_data;
979 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
982 /* In all these cases cropping emulation will not help */
983 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
984 (s->target != V4L2_SEL_TGT_COMPOSE &&
985 s->target != V4L2_SEL_TGT_CROP))
988 if (s->target == V4L2_SEL_TGT_COMPOSE) {
989 /* No output size change during a running capture! */
990 if (vb2_is_streaming(&icd->vb2_vidq) &&
991 (icd->user_width != s->r.width ||
992 icd->user_height != s->r.height))
996 * Only one user is allowed to change the output format, touch
997 * buffers, start / stop streaming, poll for data
999 if (icd->streamer && icd->streamer != file)
1003 if (s->target == V4L2_SEL_TGT_CROP &&
1004 vb2_is_streaming(&icd->vb2_vidq) &&
1005 ici->ops->set_liveselection)
1006 ret = ici->ops->set_liveselection(icd, s);
1008 ret = ici->ops->set_selection(icd, s);
1010 s->target == V4L2_SEL_TGT_COMPOSE) {
1011 icd->user_width = s->r.width;
1012 icd->user_height = s->r.height;
1014 icd->streamer = file;
1020 static int soc_camera_g_parm(struct file *file, void *fh,
1021 struct v4l2_streamparm *a)
1023 struct soc_camera_device *icd = file->private_data;
1024 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1026 if (ici->ops->get_parm)
1027 return ici->ops->get_parm(icd, a);
1029 return -ENOIOCTLCMD;
1032 static int soc_camera_s_parm(struct file *file, void *fh,
1033 struct v4l2_streamparm *a)
1035 struct soc_camera_device *icd = file->private_data;
1036 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1038 if (ici->ops->set_parm)
1039 return ici->ops->set_parm(icd, a);
1041 return -ENOIOCTLCMD;
1044 static int soc_camera_probe(struct soc_camera_host *ici,
1045 struct soc_camera_device *icd);
1047 /* So far this function cannot fail */
1048 static void scan_add_host(struct soc_camera_host *ici)
1050 struct soc_camera_device *icd;
1052 mutex_lock(&list_lock);
1054 list_for_each_entry(icd, &devices, list)
1055 if (icd->iface == ici->nr) {
1056 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1057 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1059 /* The camera could have been already on, try to reset */
1062 ssdd->reset(icd->control);
1064 icd->parent = ici->v4l2_dev.dev;
1067 soc_camera_probe(ici, icd);
1070 mutex_unlock(&list_lock);
1074 * It is invalid to call v4l2_clk_enable() after a successful probing
1075 * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
1077 static int soc_camera_clk_enable(struct v4l2_clk *clk)
1079 struct soc_camera_device *icd = clk->priv;
1080 struct soc_camera_host *ici;
1082 if (!icd || !icd->parent)
1085 ici = to_soc_camera_host(icd->parent);
1087 if (!try_module_get(ici->ops->owner))
1091 * If a different client is currently being probed, the host will tell
1094 return soc_camera_clock_start(ici);
1097 static void soc_camera_clk_disable(struct v4l2_clk *clk)
1099 struct soc_camera_device *icd = clk->priv;
1100 struct soc_camera_host *ici;
1102 if (!icd || !icd->parent)
1105 ici = to_soc_camera_host(icd->parent);
1107 soc_camera_clock_stop(ici);
1109 module_put(ici->ops->owner);
1113 * Eventually, it would be more logical to make the respective host the clock
1114 * owner, but then we would have to copy this struct for each ici. Besides, it
1115 * would introduce the circular dependency problem, unless we port all client
1116 * drivers to release the clock, when not in use.
1118 static const struct v4l2_clk_ops soc_camera_clk_ops = {
1119 .owner = THIS_MODULE,
1120 .enable = soc_camera_clk_enable,
1121 .disable = soc_camera_clk_disable,
1124 static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
1125 struct soc_camera_async_client *sasc)
1127 struct platform_device *pdev;
1130 mutex_lock(&list_lock);
1131 i = find_first_zero_bit(device_map, MAP_MAX_NUM);
1132 if (i < MAP_MAX_NUM)
1133 set_bit(i, device_map);
1134 mutex_unlock(&list_lock);
1135 if (i >= MAP_MAX_NUM)
1138 pdev = platform_device_alloc("soc-camera-pdrv", i);
1142 ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
1144 platform_device_put(pdev);
1153 static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
1155 struct platform_device *pdev = sasc->pdev;
1158 ret = platform_device_add(pdev);
1159 if (ret < 0 || !pdev->dev.driver)
1162 return platform_get_drvdata(pdev);
1165 /* Locking: called with .host_lock held */
1166 static int soc_camera_probe_finish(struct soc_camera_device *icd)
1168 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1169 struct v4l2_subdev_format fmt = {
1170 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1172 struct v4l2_mbus_framefmt *mf = &fmt.format;
1175 sd->grp_id = soc_camera_grp_id(icd);
1176 v4l2_set_subdev_hostdata(sd, icd);
1178 v4l2_subdev_call(sd, video, g_tvnorms, &icd->vdev->tvnorms);
1180 ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler,
1185 ret = soc_camera_add_device(icd);
1187 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
1191 /* At this point client .probe() should have run already */
1192 ret = soc_camera_init_user_formats(icd);
1196 icd->field = V4L2_FIELD_ANY;
1198 ret = soc_camera_video_start(icd);
1202 /* Try to improve our guess of a reasonable window format */
1203 if (!v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt)) {
1204 icd->user_width = mf->width;
1205 icd->user_height = mf->height;
1206 icd->colorspace = mf->colorspace;
1207 icd->field = mf->field;
1209 soc_camera_remove_device(icd);
1214 soc_camera_free_user_formats(icd);
1216 soc_camera_remove_device(icd);
1221 #ifdef CONFIG_I2C_BOARDINFO
1222 static int soc_camera_i2c_init(struct soc_camera_device *icd,
1223 struct soc_camera_desc *sdesc)
1225 struct soc_camera_subdev_desc *ssdd;
1226 struct i2c_client *client;
1227 struct soc_camera_host *ici;
1228 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1229 struct i2c_adapter *adap;
1230 struct v4l2_subdev *subdev;
1231 char clk_name[V4L2_CLK_NAME_SIZE];
1234 /* First find out how we link the main client */
1236 /* Async non-OF probing handled by the subdevice list */
1237 return -EPROBE_DEFER;
1240 ici = to_soc_camera_host(icd->parent);
1241 adap = i2c_get_adapter(shd->i2c_adapter_id);
1243 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1244 shd->i2c_adapter_id);
1248 ssdd = kmemdup(&sdesc->subdev_desc, sizeof(*ssdd), GFP_KERNEL);
1254 * In synchronous case we request regulators ourselves in
1255 * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
1256 * to allocate them again.
1258 ssdd->sd_pdata.num_regulators = 0;
1259 ssdd->sd_pdata.regulators = NULL;
1260 shd->board_info->platform_data = ssdd;
1262 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1263 shd->i2c_adapter_id, shd->board_info->addr);
1265 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1266 if (IS_ERR(icd->clk)) {
1267 ret = PTR_ERR(icd->clk);
1271 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1272 shd->board_info, NULL);
1278 client = v4l2_get_subdevdata(subdev);
1280 /* Use to_i2c_client(dev) to recover the i2c client */
1281 icd->control = &client->dev;
1285 v4l2_clk_unregister(icd->clk);
1290 i2c_put_adapter(adap);
1294 static void soc_camera_i2c_free(struct soc_camera_device *icd)
1296 struct i2c_client *client =
1297 to_i2c_client(to_soc_camera_control(icd));
1298 struct i2c_adapter *adap;
1299 struct soc_camera_subdev_desc *ssdd;
1301 icd->control = NULL;
1305 adap = client->adapter;
1306 ssdd = client->dev.platform_data;
1307 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1308 i2c_unregister_device(client);
1309 i2c_put_adapter(adap);
1311 v4l2_clk_unregister(icd->clk);
1316 * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
1317 * internal global mutex, therefore cannot race against other asynchronous
1318 * events. Until notifier->complete() (soc_camera_async_complete()) is called,
1319 * the video device node is not registered and no V4L fops can occur. Unloading
1320 * of the host driver also calls a v4l2-async function, so also there we're
1323 static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
1324 struct v4l2_subdev *sd,
1325 struct v4l2_async_subdev *asd)
1327 struct soc_camera_async_client *sasc = container_of(notifier,
1328 struct soc_camera_async_client, notifier);
1329 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1331 if (asd == sasc->sensor && !WARN_ON(icd->control)) {
1332 struct i2c_client *client = v4l2_get_subdevdata(sd);
1335 * Only now we get subdevice-specific information like
1336 * regulators, flags, callbacks, etc.
1339 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1340 struct soc_camera_subdev_desc *ssdd =
1341 soc_camera_i2c_to_desc(client);
1343 memcpy(&sdesc->subdev_desc, ssdd,
1344 sizeof(sdesc->subdev_desc));
1346 ssdd->reset(&client->dev);
1349 icd->control = &client->dev;
1356 static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
1357 struct v4l2_subdev *sd,
1358 struct v4l2_async_subdev *asd)
1360 struct soc_camera_async_client *sasc = container_of(notifier,
1361 struct soc_camera_async_client, notifier);
1362 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1364 icd->control = NULL;
1367 v4l2_clk_unregister(icd->clk);
1372 static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
1374 struct soc_camera_async_client *sasc = container_of(notifier,
1375 struct soc_camera_async_client, notifier);
1376 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1378 if (to_soc_camera_control(icd)) {
1379 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1382 mutex_lock(&list_lock);
1383 ret = soc_camera_probe(ici, icd);
1384 mutex_unlock(&list_lock);
1392 static const struct v4l2_async_notifier_operations soc_camera_async_ops = {
1393 .bound = soc_camera_async_bound,
1394 .unbind = soc_camera_async_unbind,
1395 .complete = soc_camera_async_complete,
1398 static int scan_async_group(struct soc_camera_host *ici,
1399 struct v4l2_async_subdev **asd, unsigned int size)
1401 struct soc_camera_async_subdev *sasd;
1402 struct soc_camera_async_client *sasc;
1403 struct soc_camera_device *icd;
1404 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1405 char clk_name[V4L2_CLK_NAME_SIZE];
1409 /* First look for a sensor */
1410 for (i = 0; i < size; i++) {
1411 sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
1412 if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
1416 if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
1418 dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
1422 /* Or shall this be managed by the soc-camera device? */
1423 sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
1427 /* HACK: just need a != NULL */
1428 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1430 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1434 sasc->sensor = &sasd->asd;
1436 icd = soc_camera_add_pdev(sasc);
1442 v4l2_async_notifier_init(&sasc->notifier);
1444 for (i = 0; i < size; i++) {
1445 ret = v4l2_async_notifier_add_subdev(&sasc->notifier, asd[i]);
1450 sasc->notifier.ops = &soc_camera_async_ops;
1453 icd->parent = ici->v4l2_dev.dev;
1455 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1456 sasd->asd.match.i2c.adapter_id,
1457 sasd->asd.match.i2c.address);
1459 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1460 if (IS_ERR(icd->clk)) {
1461 ret = PTR_ERR(icd->clk);
1465 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1469 v4l2_clk_unregister(icd->clk);
1473 v4l2_async_notifier_cleanup(&sasc->notifier);
1474 platform_device_del(sasc->pdev);
1476 platform_device_put(sasc->pdev);
1478 devm_kfree(ici->v4l2_dev.dev, sasc);
1479 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1484 static void scan_async_host(struct soc_camera_host *ici)
1486 struct v4l2_async_subdev **asd;
1489 for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
1490 scan_async_group(ici, asd, ici->asd_sizes[j]);
1491 asd += ici->asd_sizes[j];
1495 #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
1496 #define soc_camera_i2c_free(icd) do {} while (0)
1497 #define scan_async_host(ici) do {} while (0)
1502 struct soc_of_info {
1503 struct soc_camera_async_subdev sasd;
1504 struct soc_camera_async_client sasc;
1505 struct v4l2_async_subdev *subdev;
1508 static int soc_of_bind(struct soc_camera_host *ici,
1509 struct device_node *ep,
1510 struct device_node *remote)
1512 struct soc_camera_device *icd;
1513 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1514 struct soc_camera_async_client *sasc;
1515 struct soc_of_info *info;
1516 struct i2c_client *client;
1517 char clk_name[V4L2_CLK_NAME_SIZE];
1520 /* allocate a new subdev and add match info to it */
1521 info = devm_kzalloc(ici->v4l2_dev.dev, sizeof(struct soc_of_info),
1526 info->sasd.asd.match.fwnode = of_fwnode_handle(remote);
1527 info->sasd.asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
1528 info->subdev = &info->sasd.asd;
1530 /* Or shall this be managed by the soc-camera device? */
1533 /* HACK: just need a != NULL */
1534 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1536 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1540 sasc->sensor = &info->sasd.asd;
1542 icd = soc_camera_add_pdev(sasc);
1548 v4l2_async_notifier_init(&sasc->notifier);
1550 ret = v4l2_async_notifier_add_subdev(&sasc->notifier, info->subdev);
1552 of_node_put(remote);
1556 sasc->notifier.ops = &soc_camera_async_ops;
1559 icd->parent = ici->v4l2_dev.dev;
1561 client = of_find_i2c_device_by_node(remote);
1564 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1565 client->adapter->nr, client->addr);
1567 v4l2_clk_name_of(clk_name, sizeof(clk_name), remote);
1569 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1570 if (IS_ERR(icd->clk)) {
1571 ret = PTR_ERR(icd->clk);
1575 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1579 v4l2_clk_unregister(icd->clk);
1583 v4l2_async_notifier_cleanup(&sasc->notifier);
1584 platform_device_del(sasc->pdev);
1586 platform_device_put(sasc->pdev);
1588 devm_kfree(ici->v4l2_dev.dev, info);
1589 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1594 static void scan_of_host(struct soc_camera_host *ici)
1596 struct device *dev = ici->v4l2_dev.dev;
1597 struct device_node *np = dev->of_node;
1598 struct device_node *epn = NULL, *rem;
1601 for (i = 0; ; i++) {
1602 epn = of_graph_get_next_endpoint(np, epn);
1606 rem = of_graph_get_remote_port_parent(epn);
1608 dev_notice(dev, "no remote for %pOF\n", epn);
1612 /* so we now have a remote node to connect */
1614 soc_of_bind(ici, epn, rem);
1617 dev_err(dev, "multiple subdevices aren't supported yet!\n");
1626 static inline void scan_of_host(struct soc_camera_host *ici) { }
1629 /* Called during host-driver probe */
1630 static int soc_camera_probe(struct soc_camera_host *ici,
1631 struct soc_camera_device *icd)
1633 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1634 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1635 struct device *control = NULL;
1638 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1641 * Currently the subdev with the largest number of controls (13) is
1642 * ov6550. So let's pick 16 as a hint for the control handler. Note
1643 * that this is a hint only: too large and you waste some memory, too
1644 * small and there is a (very) small performance hit when looking up
1645 * controls in the internal hash.
1647 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1651 /* Must have icd->vdev before registering the device */
1652 ret = video_dev_create(icd);
1657 * ..._video_start() will create a device node, video_register_device()
1658 * itself is protected against concurrent open() calls, but we also have
1659 * to protect our data also during client probing.
1662 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1663 if (shd->board_info) {
1664 ret = soc_camera_i2c_init(icd, sdesc);
1665 if (ret < 0 && ret != -EPROBE_DEFER)
1667 } else if (!shd->add_device || !shd->del_device) {
1671 ret = soc_camera_clock_start(ici);
1675 if (shd->module_name)
1676 ret = request_module(shd->module_name);
1678 ret = shd->add_device(icd);
1683 * FIXME: this is racy, have to use driver-binding notification,
1684 * when it is available
1686 control = to_soc_camera_control(icd);
1687 if (!control || !control->driver || !dev_get_drvdata(control) ||
1688 !try_module_get(control->driver->owner)) {
1689 shd->del_device(icd);
1695 mutex_lock(&ici->host_lock);
1696 ret = soc_camera_probe_finish(icd);
1697 mutex_unlock(&ici->host_lock);
1704 if (shd->board_info) {
1705 soc_camera_i2c_free(icd);
1707 shd->del_device(icd);
1708 module_put(control->driver->owner);
1711 soc_camera_clock_stop(ici);
1715 video_device_release(icd->vdev);
1719 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1724 * This is called on device_unregister, which only means we have to disconnect
1725 * from the host, but not remove ourselves from the device list. With
1726 * asynchronous client probing this can also be called without
1727 * soc_camera_probe_finish() having run. Careful with clean up.
1729 static int soc_camera_remove(struct soc_camera_device *icd)
1731 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1732 struct video_device *vdev = icd->vdev;
1734 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1736 video_unregister_device(vdev);
1740 if (sdesc->host_desc.board_info) {
1741 soc_camera_i2c_free(icd);
1743 struct device *dev = to_soc_camera_control(icd);
1744 struct device_driver *drv = dev ? dev->driver : NULL;
1746 sdesc->host_desc.del_device(icd);
1747 module_put(drv->owner);
1751 if (icd->num_user_formats)
1752 soc_camera_free_user_formats(icd);
1755 /* For the synchronous case */
1756 v4l2_clk_unregister(icd->clk);
1761 platform_device_unregister(icd->sasc->pdev);
1766 static int default_g_selection(struct soc_camera_device *icd,
1767 struct v4l2_selection *sel)
1769 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1770 struct v4l2_subdev_selection sdsel = {
1771 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1772 .target = sel->target,
1776 ret = v4l2_subdev_call(sd, pad, get_selection, NULL, &sdsel);
1783 static int default_s_selection(struct soc_camera_device *icd,
1784 struct v4l2_selection *sel)
1786 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1787 struct v4l2_subdev_selection sdsel = {
1788 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1789 .target = sel->target,
1790 .flags = sel->flags,
1795 ret = v4l2_subdev_call(sd, pad, set_selection, NULL, &sdsel);
1802 static int default_g_parm(struct soc_camera_device *icd,
1803 struct v4l2_streamparm *a)
1805 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1807 return v4l2_g_parm_cap(icd->vdev, sd, a);
1810 static int default_s_parm(struct soc_camera_device *icd,
1811 struct v4l2_streamparm *a)
1813 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1815 return v4l2_s_parm_cap(icd->vdev, sd, a);
1818 static int default_enum_framesizes(struct soc_camera_device *icd,
1819 struct v4l2_frmsizeenum *fsize)
1822 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1823 const struct soc_camera_format_xlate *xlate;
1824 struct v4l2_subdev_frame_size_enum fse = {
1825 .index = fsize->index,
1826 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1829 xlate = soc_camera_xlate_by_fourcc(icd, fsize->pixel_format);
1832 fse.code = xlate->code;
1834 ret = v4l2_subdev_call(sd, pad, enum_frame_size, NULL, &fse);
1838 if (fse.min_width == fse.max_width &&
1839 fse.min_height == fse.max_height) {
1840 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1841 fsize->discrete.width = fse.min_width;
1842 fsize->discrete.height = fse.min_height;
1845 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1846 fsize->stepwise.min_width = fse.min_width;
1847 fsize->stepwise.max_width = fse.max_width;
1848 fsize->stepwise.min_height = fse.min_height;
1849 fsize->stepwise.max_height = fse.max_height;
1850 fsize->stepwise.step_width = 1;
1851 fsize->stepwise.step_height = 1;
1855 int soc_camera_host_register(struct soc_camera_host *ici)
1857 struct soc_camera_host *ix;
1860 if (!ici || !ici->ops ||
1861 !ici->ops->try_fmt ||
1862 !ici->ops->set_fmt ||
1863 !ici->ops->set_bus_param ||
1864 !ici->ops->querycap ||
1865 !ici->ops->init_videobuf2 ||
1870 if (!ici->ops->set_selection)
1871 ici->ops->set_selection = default_s_selection;
1872 if (!ici->ops->get_selection)
1873 ici->ops->get_selection = default_g_selection;
1874 if (!ici->ops->set_parm)
1875 ici->ops->set_parm = default_s_parm;
1876 if (!ici->ops->get_parm)
1877 ici->ops->get_parm = default_g_parm;
1878 if (!ici->ops->enum_framesizes)
1879 ici->ops->enum_framesizes = default_enum_framesizes;
1881 mutex_lock(&list_lock);
1882 list_for_each_entry(ix, &hosts, list) {
1883 if (ix->nr == ici->nr) {
1889 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1893 list_add_tail(&ici->list, &hosts);
1894 mutex_unlock(&list_lock);
1896 mutex_init(&ici->host_lock);
1897 mutex_init(&ici->clk_lock);
1899 if (ici->v4l2_dev.dev->of_node)
1901 else if (ici->asd_sizes)
1903 * No OF, host with a list of subdevices. Don't try to mix
1904 * modes by initialising some groups statically and some
1907 scan_async_host(ici);
1909 /* Legacy: static platform devices from board data */
1915 mutex_unlock(&list_lock);
1918 EXPORT_SYMBOL(soc_camera_host_register);
1920 /* Unregister all clients! */
1921 void soc_camera_host_unregister(struct soc_camera_host *ici)
1923 struct soc_camera_device *icd, *tmp;
1924 struct soc_camera_async_client *sasc;
1925 LIST_HEAD(notifiers);
1927 mutex_lock(&list_lock);
1928 list_del(&ici->list);
1929 list_for_each_entry(icd, &devices, list)
1930 if (icd->iface == ici->nr && icd->sasc) {
1931 /* as long as we hold the device, sasc won't be freed */
1932 get_device(icd->pdev);
1933 list_add(&icd->sasc->list, ¬ifiers);
1935 mutex_unlock(&list_lock);
1937 list_for_each_entry(sasc, ¬ifiers, list) {
1938 /* Must call unlocked to avoid AB-BA dead-lock */
1939 v4l2_async_notifier_unregister(&sasc->notifier);
1940 v4l2_async_notifier_cleanup(&sasc->notifier);
1941 put_device(&sasc->pdev->dev);
1944 mutex_lock(&list_lock);
1946 list_for_each_entry_safe(icd, tmp, &devices, list)
1947 if (icd->iface == ici->nr)
1948 soc_camera_remove(icd);
1950 mutex_unlock(&list_lock);
1952 v4l2_device_unregister(&ici->v4l2_dev);
1954 EXPORT_SYMBOL(soc_camera_host_unregister);
1956 /* Image capture device */
1957 static int soc_camera_device_register(struct soc_camera_device *icd)
1959 struct soc_camera_device *ix;
1962 mutex_lock(&list_lock);
1963 for (i = 0; i < 256 && num < 0; i++) {
1965 /* Check if this index is available on this interface */
1966 list_for_each_entry(ix, &devices, list) {
1967 if (ix->iface == icd->iface && ix->devnum == i) {
1976 * ok, we have 256 cameras on this host...
1977 * man, stay reasonable...
1979 mutex_unlock(&list_lock);
1985 icd->host_priv = NULL;
1988 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
1991 i = to_platform_device(icd->pdev)->id;
1993 /* One static (legacy) soc-camera platform device */
1995 if (i >= MAP_MAX_NUM) {
1996 mutex_unlock(&list_lock);
1999 set_bit(i, device_map);
2000 list_add_tail(&icd->list, &devices);
2001 mutex_unlock(&list_lock);
2006 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
2007 .vidioc_querycap = soc_camera_querycap,
2008 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
2009 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
2010 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
2011 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
2012 .vidioc_enum_input = soc_camera_enum_input,
2013 .vidioc_g_input = soc_camera_g_input,
2014 .vidioc_s_input = soc_camera_s_input,
2015 .vidioc_s_std = soc_camera_s_std,
2016 .vidioc_g_std = soc_camera_g_std,
2017 .vidioc_enum_framesizes = soc_camera_enum_framesizes,
2018 .vidioc_reqbufs = soc_camera_reqbufs,
2019 .vidioc_querybuf = soc_camera_querybuf,
2020 .vidioc_qbuf = soc_camera_qbuf,
2021 .vidioc_dqbuf = soc_camera_dqbuf,
2022 .vidioc_create_bufs = soc_camera_create_bufs,
2023 .vidioc_prepare_buf = soc_camera_prepare_buf,
2024 .vidioc_expbuf = soc_camera_expbuf,
2025 .vidioc_streamon = soc_camera_streamon,
2026 .vidioc_streamoff = soc_camera_streamoff,
2027 .vidioc_g_selection = soc_camera_g_selection,
2028 .vidioc_s_selection = soc_camera_s_selection,
2029 .vidioc_g_parm = soc_camera_g_parm,
2030 .vidioc_s_parm = soc_camera_s_parm,
2033 static int video_dev_create(struct soc_camera_device *icd)
2035 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2036 struct video_device *vdev = video_device_alloc();
2041 strscpy(vdev->name, ici->drv_name, sizeof(vdev->name));
2043 vdev->v4l2_dev = &ici->v4l2_dev;
2044 vdev->fops = &soc_camera_fops;
2045 vdev->ioctl_ops = &soc_camera_ioctl_ops;
2046 vdev->release = video_device_release;
2047 vdev->ctrl_handler = &icd->ctrl_handler;
2048 vdev->lock = &ici->host_lock;
2056 * Called from soc_camera_probe() above with .host_lock held
2058 static int soc_camera_video_start(struct soc_camera_device *icd)
2060 const struct device_type *type = icd->vdev->dev.type;
2066 video_set_drvdata(icd->vdev, icd);
2067 if (icd->vdev->tvnorms == 0) {
2068 /* disable the STD API if there are no tvnorms defined */
2069 v4l2_disable_ioctl(icd->vdev, VIDIOC_G_STD);
2070 v4l2_disable_ioctl(icd->vdev, VIDIOC_S_STD);
2071 v4l2_disable_ioctl(icd->vdev, VIDIOC_ENUMSTD);
2073 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
2075 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
2079 /* Restore device type, possibly set by the subdevice driver */
2080 icd->vdev->dev.type = type;
2085 static int soc_camera_pdrv_probe(struct platform_device *pdev)
2087 struct soc_camera_desc *sdesc = pdev->dev.platform_data;
2088 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
2089 struct soc_camera_device *icd;
2095 icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
2100 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
2101 * regulator allocation is a dummy. They are actually requested by the
2102 * subdevice driver, using soc_camera_power_init(). Also note, that in
2103 * that case regulators are attached to the I2C device and not to the
2104 * camera platform device.
2106 ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators,
2107 ssdd->sd_pdata.regulators);
2111 icd->iface = sdesc->host_desc.bus_id;
2113 icd->pdev = &pdev->dev;
2114 platform_set_drvdata(pdev, icd);
2116 icd->user_width = DEFAULT_WIDTH;
2117 icd->user_height = DEFAULT_HEIGHT;
2119 return soc_camera_device_register(icd);
2123 * Only called on rmmod for each platform device, since they are not
2124 * hot-pluggable. Now we know, that all our users - hosts and devices have
2125 * been unloaded already
2127 static int soc_camera_pdrv_remove(struct platform_device *pdev)
2129 struct soc_camera_device *icd = platform_get_drvdata(pdev);
2140 * In synchronous mode with static platform devices this is called in a
2141 * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
2142 * no need to lock. In asynchronous case the caller -
2143 * soc_camera_host_unregister() - already holds the lock
2145 if (test_bit(i, device_map)) {
2146 clear_bit(i, device_map);
2147 list_del(&icd->list);
2153 static struct platform_driver __refdata soc_camera_pdrv = {
2154 .probe = soc_camera_pdrv_probe,
2155 .remove = soc_camera_pdrv_remove,
2157 .name = "soc-camera-pdrv",
2161 module_platform_driver(soc_camera_pdrv);
2163 MODULE_DESCRIPTION("Image capture bus driver");
2164 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2165 MODULE_LICENSE("GPL");
2166 MODULE_ALIAS("platform:soc-camera-pdrv");