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;
872 f->pixelformat = format->fourcc;
876 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
877 struct v4l2_format *f)
879 struct soc_camera_device *icd = file->private_data;
880 struct v4l2_pix_format *pix = &f->fmt.pix;
882 WARN_ON(priv != file->private_data);
884 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
887 pix->width = icd->user_width;
888 pix->height = icd->user_height;
889 pix->bytesperline = icd->bytesperline;
890 pix->sizeimage = icd->sizeimage;
891 pix->field = icd->field;
892 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
893 pix->colorspace = icd->colorspace;
894 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
895 icd->current_fmt->host_fmt->fourcc);
899 static int soc_camera_querycap(struct file *file, void *priv,
900 struct v4l2_capability *cap)
902 struct soc_camera_device *icd = file->private_data;
903 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
905 WARN_ON(priv != file->private_data);
907 strscpy(cap->driver, ici->drv_name, sizeof(cap->driver));
908 return ici->ops->querycap(ici, cap);
911 static int soc_camera_streamon(struct file *file, void *priv,
912 enum v4l2_buf_type i)
914 struct soc_camera_device *icd = file->private_data;
915 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
918 WARN_ON(priv != file->private_data);
920 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
923 if (icd->streamer != file)
926 /* This calls buf_queue from host driver's videobuf2_queue_ops */
927 ret = vb2_streamon(&icd->vb2_vidq, i);
929 v4l2_subdev_call(sd, video, s_stream, 1);
934 static int soc_camera_streamoff(struct file *file, void *priv,
935 enum v4l2_buf_type i)
937 struct soc_camera_device *icd = file->private_data;
938 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
941 WARN_ON(priv != file->private_data);
943 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
946 if (icd->streamer != file)
950 * This calls buf_release from host driver's videobuf2_queue_ops for all
951 * remaining buffers. When the last buffer is freed, stop capture
953 ret = vb2_streamoff(&icd->vb2_vidq, i);
955 v4l2_subdev_call(sd, video, s_stream, 0);
960 static int soc_camera_g_selection(struct file *file, void *fh,
961 struct v4l2_selection *s)
963 struct soc_camera_device *icd = file->private_data;
964 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
966 /* With a wrong type no need to try to fall back to cropping */
967 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
970 return ici->ops->get_selection(icd, s);
973 static int soc_camera_s_selection(struct file *file, void *fh,
974 struct v4l2_selection *s)
976 struct soc_camera_device *icd = file->private_data;
977 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
980 /* In all these cases cropping emulation will not help */
981 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
982 (s->target != V4L2_SEL_TGT_COMPOSE &&
983 s->target != V4L2_SEL_TGT_CROP))
986 if (s->target == V4L2_SEL_TGT_COMPOSE) {
987 /* No output size change during a running capture! */
988 if (vb2_is_streaming(&icd->vb2_vidq) &&
989 (icd->user_width != s->r.width ||
990 icd->user_height != s->r.height))
994 * Only one user is allowed to change the output format, touch
995 * buffers, start / stop streaming, poll for data
997 if (icd->streamer && icd->streamer != file)
1001 if (s->target == V4L2_SEL_TGT_CROP &&
1002 vb2_is_streaming(&icd->vb2_vidq) &&
1003 ici->ops->set_liveselection)
1004 ret = ici->ops->set_liveselection(icd, s);
1006 ret = ici->ops->set_selection(icd, s);
1008 s->target == V4L2_SEL_TGT_COMPOSE) {
1009 icd->user_width = s->r.width;
1010 icd->user_height = s->r.height;
1012 icd->streamer = file;
1018 static int soc_camera_g_parm(struct file *file, void *fh,
1019 struct v4l2_streamparm *a)
1021 struct soc_camera_device *icd = file->private_data;
1022 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1024 if (ici->ops->get_parm)
1025 return ici->ops->get_parm(icd, a);
1027 return -ENOIOCTLCMD;
1030 static int soc_camera_s_parm(struct file *file, void *fh,
1031 struct v4l2_streamparm *a)
1033 struct soc_camera_device *icd = file->private_data;
1034 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1036 if (ici->ops->set_parm)
1037 return ici->ops->set_parm(icd, a);
1039 return -ENOIOCTLCMD;
1042 static int soc_camera_probe(struct soc_camera_host *ici,
1043 struct soc_camera_device *icd);
1045 /* So far this function cannot fail */
1046 static void scan_add_host(struct soc_camera_host *ici)
1048 struct soc_camera_device *icd;
1050 mutex_lock(&list_lock);
1052 list_for_each_entry(icd, &devices, list)
1053 if (icd->iface == ici->nr) {
1054 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1055 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1057 /* The camera could have been already on, try to reset */
1060 ssdd->reset(icd->control);
1062 icd->parent = ici->v4l2_dev.dev;
1065 soc_camera_probe(ici, icd);
1068 mutex_unlock(&list_lock);
1072 * It is invalid to call v4l2_clk_enable() after a successful probing
1073 * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
1075 static int soc_camera_clk_enable(struct v4l2_clk *clk)
1077 struct soc_camera_device *icd = clk->priv;
1078 struct soc_camera_host *ici;
1080 if (!icd || !icd->parent)
1083 ici = to_soc_camera_host(icd->parent);
1085 if (!try_module_get(ici->ops->owner))
1089 * If a different client is currently being probed, the host will tell
1092 return soc_camera_clock_start(ici);
1095 static void soc_camera_clk_disable(struct v4l2_clk *clk)
1097 struct soc_camera_device *icd = clk->priv;
1098 struct soc_camera_host *ici;
1100 if (!icd || !icd->parent)
1103 ici = to_soc_camera_host(icd->parent);
1105 soc_camera_clock_stop(ici);
1107 module_put(ici->ops->owner);
1111 * Eventually, it would be more logical to make the respective host the clock
1112 * owner, but then we would have to copy this struct for each ici. Besides, it
1113 * would introduce the circular dependency problem, unless we port all client
1114 * drivers to release the clock, when not in use.
1116 static const struct v4l2_clk_ops soc_camera_clk_ops = {
1117 .owner = THIS_MODULE,
1118 .enable = soc_camera_clk_enable,
1119 .disable = soc_camera_clk_disable,
1122 static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
1123 struct soc_camera_async_client *sasc)
1125 struct platform_device *pdev;
1128 mutex_lock(&list_lock);
1129 i = find_first_zero_bit(device_map, MAP_MAX_NUM);
1130 if (i < MAP_MAX_NUM)
1131 set_bit(i, device_map);
1132 mutex_unlock(&list_lock);
1133 if (i >= MAP_MAX_NUM)
1136 pdev = platform_device_alloc("soc-camera-pdrv", i);
1140 ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
1142 platform_device_put(pdev);
1151 static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
1153 struct platform_device *pdev = sasc->pdev;
1156 ret = platform_device_add(pdev);
1157 if (ret < 0 || !pdev->dev.driver)
1160 return platform_get_drvdata(pdev);
1163 /* Locking: called with .host_lock held */
1164 static int soc_camera_probe_finish(struct soc_camera_device *icd)
1166 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1167 struct v4l2_subdev_format fmt = {
1168 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1170 struct v4l2_mbus_framefmt *mf = &fmt.format;
1173 sd->grp_id = soc_camera_grp_id(icd);
1174 v4l2_set_subdev_hostdata(sd, icd);
1176 v4l2_subdev_call(sd, video, g_tvnorms, &icd->vdev->tvnorms);
1178 ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler,
1183 ret = soc_camera_add_device(icd);
1185 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
1189 /* At this point client .probe() should have run already */
1190 ret = soc_camera_init_user_formats(icd);
1194 icd->field = V4L2_FIELD_ANY;
1196 ret = soc_camera_video_start(icd);
1200 /* Try to improve our guess of a reasonable window format */
1201 if (!v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt)) {
1202 icd->user_width = mf->width;
1203 icd->user_height = mf->height;
1204 icd->colorspace = mf->colorspace;
1205 icd->field = mf->field;
1207 soc_camera_remove_device(icd);
1212 soc_camera_free_user_formats(icd);
1214 soc_camera_remove_device(icd);
1219 #ifdef CONFIG_I2C_BOARDINFO
1220 static int soc_camera_i2c_init(struct soc_camera_device *icd,
1221 struct soc_camera_desc *sdesc)
1223 struct soc_camera_subdev_desc *ssdd;
1224 struct i2c_client *client;
1225 struct soc_camera_host *ici;
1226 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1227 struct i2c_adapter *adap;
1228 struct v4l2_subdev *subdev;
1229 char clk_name[V4L2_CLK_NAME_SIZE];
1232 /* First find out how we link the main client */
1234 /* Async non-OF probing handled by the subdevice list */
1235 return -EPROBE_DEFER;
1238 ici = to_soc_camera_host(icd->parent);
1239 adap = i2c_get_adapter(shd->i2c_adapter_id);
1241 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1242 shd->i2c_adapter_id);
1246 ssdd = kmemdup(&sdesc->subdev_desc, sizeof(*ssdd), GFP_KERNEL);
1252 * In synchronous case we request regulators ourselves in
1253 * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
1254 * to allocate them again.
1256 ssdd->sd_pdata.num_regulators = 0;
1257 ssdd->sd_pdata.regulators = NULL;
1258 shd->board_info->platform_data = ssdd;
1260 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1261 shd->i2c_adapter_id, shd->board_info->addr);
1263 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1264 if (IS_ERR(icd->clk)) {
1265 ret = PTR_ERR(icd->clk);
1269 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1270 shd->board_info, NULL);
1276 client = v4l2_get_subdevdata(subdev);
1278 /* Use to_i2c_client(dev) to recover the i2c client */
1279 icd->control = &client->dev;
1283 v4l2_clk_unregister(icd->clk);
1288 i2c_put_adapter(adap);
1292 static void soc_camera_i2c_free(struct soc_camera_device *icd)
1294 struct i2c_client *client =
1295 to_i2c_client(to_soc_camera_control(icd));
1296 struct i2c_adapter *adap;
1297 struct soc_camera_subdev_desc *ssdd;
1299 icd->control = NULL;
1303 adap = client->adapter;
1304 ssdd = client->dev.platform_data;
1305 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1306 i2c_unregister_device(client);
1307 i2c_put_adapter(adap);
1309 v4l2_clk_unregister(icd->clk);
1314 * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
1315 * internal global mutex, therefore cannot race against other asynchronous
1316 * events. Until notifier->complete() (soc_camera_async_complete()) is called,
1317 * the video device node is not registered and no V4L fops can occur. Unloading
1318 * of the host driver also calls a v4l2-async function, so also there we're
1321 static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
1322 struct v4l2_subdev *sd,
1323 struct v4l2_async_subdev *asd)
1325 struct soc_camera_async_client *sasc = container_of(notifier,
1326 struct soc_camera_async_client, notifier);
1327 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1329 if (asd == sasc->sensor && !WARN_ON(icd->control)) {
1330 struct i2c_client *client = v4l2_get_subdevdata(sd);
1333 * Only now we get subdevice-specific information like
1334 * regulators, flags, callbacks, etc.
1337 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1338 struct soc_camera_subdev_desc *ssdd =
1339 soc_camera_i2c_to_desc(client);
1341 memcpy(&sdesc->subdev_desc, ssdd,
1342 sizeof(sdesc->subdev_desc));
1344 ssdd->reset(&client->dev);
1347 icd->control = &client->dev;
1354 static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
1355 struct v4l2_subdev *sd,
1356 struct v4l2_async_subdev *asd)
1358 struct soc_camera_async_client *sasc = container_of(notifier,
1359 struct soc_camera_async_client, notifier);
1360 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1362 icd->control = NULL;
1365 v4l2_clk_unregister(icd->clk);
1370 static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
1372 struct soc_camera_async_client *sasc = container_of(notifier,
1373 struct soc_camera_async_client, notifier);
1374 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1376 if (to_soc_camera_control(icd)) {
1377 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1380 mutex_lock(&list_lock);
1381 ret = soc_camera_probe(ici, icd);
1382 mutex_unlock(&list_lock);
1390 static const struct v4l2_async_notifier_operations soc_camera_async_ops = {
1391 .bound = soc_camera_async_bound,
1392 .unbind = soc_camera_async_unbind,
1393 .complete = soc_camera_async_complete,
1396 static int scan_async_group(struct soc_camera_host *ici,
1397 struct v4l2_async_subdev **asd, unsigned int size)
1399 struct soc_camera_async_subdev *sasd;
1400 struct soc_camera_async_client *sasc;
1401 struct soc_camera_device *icd;
1402 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1403 char clk_name[V4L2_CLK_NAME_SIZE];
1407 /* First look for a sensor */
1408 for (i = 0; i < size; i++) {
1409 sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
1410 if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
1414 if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
1416 dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
1420 /* Or shall this be managed by the soc-camera device? */
1421 sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
1425 /* HACK: just need a != NULL */
1426 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1428 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1432 sasc->sensor = &sasd->asd;
1434 icd = soc_camera_add_pdev(sasc);
1440 v4l2_async_notifier_init(&sasc->notifier);
1442 for (i = 0; i < size; i++) {
1443 ret = v4l2_async_notifier_add_subdev(&sasc->notifier, asd[i]);
1448 sasc->notifier.ops = &soc_camera_async_ops;
1451 icd->parent = ici->v4l2_dev.dev;
1453 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1454 sasd->asd.match.i2c.adapter_id,
1455 sasd->asd.match.i2c.address);
1457 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1458 if (IS_ERR(icd->clk)) {
1459 ret = PTR_ERR(icd->clk);
1463 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1467 v4l2_clk_unregister(icd->clk);
1471 v4l2_async_notifier_cleanup(&sasc->notifier);
1472 platform_device_del(sasc->pdev);
1474 platform_device_put(sasc->pdev);
1476 devm_kfree(ici->v4l2_dev.dev, sasc);
1477 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1482 static void scan_async_host(struct soc_camera_host *ici)
1484 struct v4l2_async_subdev **asd;
1487 for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
1488 scan_async_group(ici, asd, ici->asd_sizes[j]);
1489 asd += ici->asd_sizes[j];
1493 #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
1494 #define soc_camera_i2c_free(icd) do {} while (0)
1495 #define scan_async_host(ici) do {} while (0)
1500 struct soc_of_info {
1501 struct soc_camera_async_subdev sasd;
1502 struct soc_camera_async_client sasc;
1503 struct v4l2_async_subdev *subdev;
1506 static int soc_of_bind(struct soc_camera_host *ici,
1507 struct device_node *ep,
1508 struct device_node *remote)
1510 struct soc_camera_device *icd;
1511 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1512 struct soc_camera_async_client *sasc;
1513 struct soc_of_info *info;
1514 struct i2c_client *client;
1515 char clk_name[V4L2_CLK_NAME_SIZE];
1518 /* allocate a new subdev and add match info to it */
1519 info = devm_kzalloc(ici->v4l2_dev.dev, sizeof(struct soc_of_info),
1524 info->sasd.asd.match.fwnode = of_fwnode_handle(remote);
1525 info->sasd.asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
1526 info->subdev = &info->sasd.asd;
1528 /* Or shall this be managed by the soc-camera device? */
1531 /* HACK: just need a != NULL */
1532 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1534 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1538 sasc->sensor = &info->sasd.asd;
1540 icd = soc_camera_add_pdev(sasc);
1546 v4l2_async_notifier_init(&sasc->notifier);
1548 ret = v4l2_async_notifier_add_subdev(&sasc->notifier, info->subdev);
1550 of_node_put(remote);
1554 sasc->notifier.ops = &soc_camera_async_ops;
1557 icd->parent = ici->v4l2_dev.dev;
1559 client = of_find_i2c_device_by_node(remote);
1562 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1563 client->adapter->nr, client->addr);
1565 v4l2_clk_name_of(clk_name, sizeof(clk_name), remote);
1567 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1568 if (IS_ERR(icd->clk)) {
1569 ret = PTR_ERR(icd->clk);
1573 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1577 v4l2_clk_unregister(icd->clk);
1581 v4l2_async_notifier_cleanup(&sasc->notifier);
1582 platform_device_del(sasc->pdev);
1584 platform_device_put(sasc->pdev);
1586 devm_kfree(ici->v4l2_dev.dev, info);
1587 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1592 static void scan_of_host(struct soc_camera_host *ici)
1594 struct device *dev = ici->v4l2_dev.dev;
1595 struct device_node *np = dev->of_node;
1596 struct device_node *epn = NULL, *rem;
1599 for (i = 0; ; i++) {
1600 epn = of_graph_get_next_endpoint(np, epn);
1604 rem = of_graph_get_remote_port_parent(epn);
1606 dev_notice(dev, "no remote for %pOF\n", epn);
1610 /* so we now have a remote node to connect */
1612 soc_of_bind(ici, epn, rem);
1615 dev_err(dev, "multiple subdevices aren't supported yet!\n");
1624 static inline void scan_of_host(struct soc_camera_host *ici) { }
1627 /* Called during host-driver probe */
1628 static int soc_camera_probe(struct soc_camera_host *ici,
1629 struct soc_camera_device *icd)
1631 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1632 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1633 struct device *control = NULL;
1636 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1639 * Currently the subdev with the largest number of controls (13) is
1640 * ov6550. So let's pick 16 as a hint for the control handler. Note
1641 * that this is a hint only: too large and you waste some memory, too
1642 * small and there is a (very) small performance hit when looking up
1643 * controls in the internal hash.
1645 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1649 /* Must have icd->vdev before registering the device */
1650 ret = video_dev_create(icd);
1655 * ..._video_start() will create a device node, video_register_device()
1656 * itself is protected against concurrent open() calls, but we also have
1657 * to protect our data also during client probing.
1660 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1661 if (shd->board_info) {
1662 ret = soc_camera_i2c_init(icd, sdesc);
1663 if (ret < 0 && ret != -EPROBE_DEFER)
1665 } else if (!shd->add_device || !shd->del_device) {
1669 ret = soc_camera_clock_start(ici);
1673 if (shd->module_name)
1674 ret = request_module(shd->module_name);
1676 ret = shd->add_device(icd);
1681 * FIXME: this is racy, have to use driver-binding notification,
1682 * when it is available
1684 control = to_soc_camera_control(icd);
1685 if (!control || !control->driver || !dev_get_drvdata(control) ||
1686 !try_module_get(control->driver->owner)) {
1687 shd->del_device(icd);
1693 mutex_lock(&ici->host_lock);
1694 ret = soc_camera_probe_finish(icd);
1695 mutex_unlock(&ici->host_lock);
1702 if (shd->board_info) {
1703 soc_camera_i2c_free(icd);
1705 shd->del_device(icd);
1706 module_put(control->driver->owner);
1709 soc_camera_clock_stop(ici);
1713 video_device_release(icd->vdev);
1717 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1722 * This is called on device_unregister, which only means we have to disconnect
1723 * from the host, but not remove ourselves from the device list. With
1724 * asynchronous client probing this can also be called without
1725 * soc_camera_probe_finish() having run. Careful with clean up.
1727 static int soc_camera_remove(struct soc_camera_device *icd)
1729 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1730 struct video_device *vdev = icd->vdev;
1732 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1734 video_unregister_device(vdev);
1738 if (sdesc->host_desc.board_info) {
1739 soc_camera_i2c_free(icd);
1741 struct device *dev = to_soc_camera_control(icd);
1742 struct device_driver *drv = dev ? dev->driver : NULL;
1744 sdesc->host_desc.del_device(icd);
1745 module_put(drv->owner);
1749 if (icd->num_user_formats)
1750 soc_camera_free_user_formats(icd);
1753 /* For the synchronous case */
1754 v4l2_clk_unregister(icd->clk);
1759 platform_device_unregister(icd->sasc->pdev);
1764 static int default_g_selection(struct soc_camera_device *icd,
1765 struct v4l2_selection *sel)
1767 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1768 struct v4l2_subdev_selection sdsel = {
1769 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1770 .target = sel->target,
1774 ret = v4l2_subdev_call(sd, pad, get_selection, NULL, &sdsel);
1781 static int default_s_selection(struct soc_camera_device *icd,
1782 struct v4l2_selection *sel)
1784 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1785 struct v4l2_subdev_selection sdsel = {
1786 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1787 .target = sel->target,
1788 .flags = sel->flags,
1793 ret = v4l2_subdev_call(sd, pad, set_selection, NULL, &sdsel);
1800 static int default_g_parm(struct soc_camera_device *icd,
1801 struct v4l2_streamparm *a)
1803 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1805 return v4l2_g_parm_cap(icd->vdev, sd, a);
1808 static int default_s_parm(struct soc_camera_device *icd,
1809 struct v4l2_streamparm *a)
1811 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1813 return v4l2_s_parm_cap(icd->vdev, sd, a);
1816 static int default_enum_framesizes(struct soc_camera_device *icd,
1817 struct v4l2_frmsizeenum *fsize)
1820 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1821 const struct soc_camera_format_xlate *xlate;
1822 struct v4l2_subdev_frame_size_enum fse = {
1823 .index = fsize->index,
1824 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1827 xlate = soc_camera_xlate_by_fourcc(icd, fsize->pixel_format);
1830 fse.code = xlate->code;
1832 ret = v4l2_subdev_call(sd, pad, enum_frame_size, NULL, &fse);
1836 if (fse.min_width == fse.max_width &&
1837 fse.min_height == fse.max_height) {
1838 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1839 fsize->discrete.width = fse.min_width;
1840 fsize->discrete.height = fse.min_height;
1843 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1844 fsize->stepwise.min_width = fse.min_width;
1845 fsize->stepwise.max_width = fse.max_width;
1846 fsize->stepwise.min_height = fse.min_height;
1847 fsize->stepwise.max_height = fse.max_height;
1848 fsize->stepwise.step_width = 1;
1849 fsize->stepwise.step_height = 1;
1853 int soc_camera_host_register(struct soc_camera_host *ici)
1855 struct soc_camera_host *ix;
1858 if (!ici || !ici->ops ||
1859 !ici->ops->try_fmt ||
1860 !ici->ops->set_fmt ||
1861 !ici->ops->set_bus_param ||
1862 !ici->ops->querycap ||
1863 !ici->ops->init_videobuf2 ||
1868 if (!ici->ops->set_selection)
1869 ici->ops->set_selection = default_s_selection;
1870 if (!ici->ops->get_selection)
1871 ici->ops->get_selection = default_g_selection;
1872 if (!ici->ops->set_parm)
1873 ici->ops->set_parm = default_s_parm;
1874 if (!ici->ops->get_parm)
1875 ici->ops->get_parm = default_g_parm;
1876 if (!ici->ops->enum_framesizes)
1877 ici->ops->enum_framesizes = default_enum_framesizes;
1879 mutex_lock(&list_lock);
1880 list_for_each_entry(ix, &hosts, list) {
1881 if (ix->nr == ici->nr) {
1887 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1891 list_add_tail(&ici->list, &hosts);
1892 mutex_unlock(&list_lock);
1894 mutex_init(&ici->host_lock);
1895 mutex_init(&ici->clk_lock);
1897 if (ici->v4l2_dev.dev->of_node)
1899 else if (ici->asd_sizes)
1901 * No OF, host with a list of subdevices. Don't try to mix
1902 * modes by initialising some groups statically and some
1905 scan_async_host(ici);
1907 /* Legacy: static platform devices from board data */
1913 mutex_unlock(&list_lock);
1916 EXPORT_SYMBOL(soc_camera_host_register);
1918 /* Unregister all clients! */
1919 void soc_camera_host_unregister(struct soc_camera_host *ici)
1921 struct soc_camera_device *icd, *tmp;
1922 struct soc_camera_async_client *sasc;
1923 LIST_HEAD(notifiers);
1925 mutex_lock(&list_lock);
1926 list_del(&ici->list);
1927 list_for_each_entry(icd, &devices, list)
1928 if (icd->iface == ici->nr && icd->sasc) {
1929 /* as long as we hold the device, sasc won't be freed */
1930 get_device(icd->pdev);
1931 list_add(&icd->sasc->list, ¬ifiers);
1933 mutex_unlock(&list_lock);
1935 list_for_each_entry(sasc, ¬ifiers, list) {
1936 /* Must call unlocked to avoid AB-BA dead-lock */
1937 v4l2_async_notifier_unregister(&sasc->notifier);
1938 v4l2_async_notifier_cleanup(&sasc->notifier);
1939 put_device(&sasc->pdev->dev);
1942 mutex_lock(&list_lock);
1944 list_for_each_entry_safe(icd, tmp, &devices, list)
1945 if (icd->iface == ici->nr)
1946 soc_camera_remove(icd);
1948 mutex_unlock(&list_lock);
1950 v4l2_device_unregister(&ici->v4l2_dev);
1952 EXPORT_SYMBOL(soc_camera_host_unregister);
1954 /* Image capture device */
1955 static int soc_camera_device_register(struct soc_camera_device *icd)
1957 struct soc_camera_device *ix;
1960 mutex_lock(&list_lock);
1961 for (i = 0; i < 256 && num < 0; i++) {
1963 /* Check if this index is available on this interface */
1964 list_for_each_entry(ix, &devices, list) {
1965 if (ix->iface == icd->iface && ix->devnum == i) {
1974 * ok, we have 256 cameras on this host...
1975 * man, stay reasonable...
1977 mutex_unlock(&list_lock);
1983 icd->host_priv = NULL;
1986 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
1989 i = to_platform_device(icd->pdev)->id;
1991 /* One static (legacy) soc-camera platform device */
1993 if (i >= MAP_MAX_NUM) {
1994 mutex_unlock(&list_lock);
1997 set_bit(i, device_map);
1998 list_add_tail(&icd->list, &devices);
1999 mutex_unlock(&list_lock);
2004 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
2005 .vidioc_querycap = soc_camera_querycap,
2006 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
2007 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
2008 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
2009 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
2010 .vidioc_enum_input = soc_camera_enum_input,
2011 .vidioc_g_input = soc_camera_g_input,
2012 .vidioc_s_input = soc_camera_s_input,
2013 .vidioc_s_std = soc_camera_s_std,
2014 .vidioc_g_std = soc_camera_g_std,
2015 .vidioc_enum_framesizes = soc_camera_enum_framesizes,
2016 .vidioc_reqbufs = soc_camera_reqbufs,
2017 .vidioc_querybuf = soc_camera_querybuf,
2018 .vidioc_qbuf = soc_camera_qbuf,
2019 .vidioc_dqbuf = soc_camera_dqbuf,
2020 .vidioc_create_bufs = soc_camera_create_bufs,
2021 .vidioc_prepare_buf = soc_camera_prepare_buf,
2022 .vidioc_expbuf = soc_camera_expbuf,
2023 .vidioc_streamon = soc_camera_streamon,
2024 .vidioc_streamoff = soc_camera_streamoff,
2025 .vidioc_g_selection = soc_camera_g_selection,
2026 .vidioc_s_selection = soc_camera_s_selection,
2027 .vidioc_g_parm = soc_camera_g_parm,
2028 .vidioc_s_parm = soc_camera_s_parm,
2031 static int video_dev_create(struct soc_camera_device *icd)
2033 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2034 struct video_device *vdev = video_device_alloc();
2039 strscpy(vdev->name, ici->drv_name, sizeof(vdev->name));
2041 vdev->v4l2_dev = &ici->v4l2_dev;
2042 vdev->fops = &soc_camera_fops;
2043 vdev->ioctl_ops = &soc_camera_ioctl_ops;
2044 vdev->release = video_device_release;
2045 vdev->ctrl_handler = &icd->ctrl_handler;
2046 vdev->lock = &ici->host_lock;
2054 * Called from soc_camera_probe() above with .host_lock held
2056 static int soc_camera_video_start(struct soc_camera_device *icd)
2058 const struct device_type *type = icd->vdev->dev.type;
2064 video_set_drvdata(icd->vdev, icd);
2065 if (icd->vdev->tvnorms == 0) {
2066 /* disable the STD API if there are no tvnorms defined */
2067 v4l2_disable_ioctl(icd->vdev, VIDIOC_G_STD);
2068 v4l2_disable_ioctl(icd->vdev, VIDIOC_S_STD);
2069 v4l2_disable_ioctl(icd->vdev, VIDIOC_ENUMSTD);
2071 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
2073 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
2077 /* Restore device type, possibly set by the subdevice driver */
2078 icd->vdev->dev.type = type;
2083 static int soc_camera_pdrv_probe(struct platform_device *pdev)
2085 struct soc_camera_desc *sdesc = pdev->dev.platform_data;
2086 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
2087 struct soc_camera_device *icd;
2093 icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
2098 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
2099 * regulator allocation is a dummy. They are actually requested by the
2100 * subdevice driver, using soc_camera_power_init(). Also note, that in
2101 * that case regulators are attached to the I2C device and not to the
2102 * camera platform device.
2104 ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators,
2105 ssdd->sd_pdata.regulators);
2109 icd->iface = sdesc->host_desc.bus_id;
2111 icd->pdev = &pdev->dev;
2112 platform_set_drvdata(pdev, icd);
2114 icd->user_width = DEFAULT_WIDTH;
2115 icd->user_height = DEFAULT_HEIGHT;
2117 return soc_camera_device_register(icd);
2121 * Only called on rmmod for each platform device, since they are not
2122 * hot-pluggable. Now we know, that all our users - hosts and devices have
2123 * been unloaded already
2125 static int soc_camera_pdrv_remove(struct platform_device *pdev)
2127 struct soc_camera_device *icd = platform_get_drvdata(pdev);
2138 * In synchronous mode with static platform devices this is called in a
2139 * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
2140 * no need to lock. In asynchronous case the caller -
2141 * soc_camera_host_unregister() - already holds the lock
2143 if (test_bit(i, device_map)) {
2144 clear_bit(i, device_map);
2145 list_del(&icd->list);
2151 static struct platform_driver __refdata soc_camera_pdrv = {
2152 .probe = soc_camera_pdrv_probe,
2153 .remove = soc_camera_pdrv_remove,
2155 .name = "soc-camera-pdrv",
2159 module_platform_driver(soc_camera_pdrv);
2161 MODULE_DESCRIPTION("Image capture bus driver");
2162 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2163 MODULE_LICENSE("GPL");
2164 MODULE_ALIAS("platform:soc-camera-pdrv");