2 * camera image capture (abstract) bus driver
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 * This driver provides an interface between platform-specific camera
7 * busses and camera devices. It should be used if the camera is
8 * connected not over a "proper" bus like PCI or USB, but over a
9 * special bus, like, for example, the Quick Capture interface on PXA270
10 * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
11 * It can handle multiple cameras and / or multiple busses, which can
12 * be used, e.g., in stereo-vision applications.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/of_graph.h>
27 #include <linux/platform_device.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/slab.h>
31 #include <linux/vmalloc.h>
33 #include <media/soc_camera.h>
34 #include <media/drv-intf/soc_mediabus.h>
35 #include <media/v4l2-async.h>
36 #include <media/v4l2-clk.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-dev.h>
40 #include <media/v4l2-fwnode.h>
41 #include <media/videobuf2-v4l2.h>
43 /* Default to VGA resolution */
44 #define DEFAULT_WIDTH 640
45 #define DEFAULT_HEIGHT 480
47 #define MAP_MAX_NUM 32
48 static DECLARE_BITMAP(device_map, MAP_MAX_NUM);
49 static LIST_HEAD(hosts);
50 static LIST_HEAD(devices);
52 * Protects lists and bitmaps of hosts and devices.
53 * Lock nesting: Ok to take ->host_lock under list_lock.
55 static DEFINE_MUTEX(list_lock);
57 struct soc_camera_async_client {
58 struct v4l2_async_subdev *sensor;
59 struct v4l2_async_notifier notifier;
60 struct platform_device *pdev;
61 struct list_head list; /* needed for clean up */
64 static int soc_camera_video_start(struct soc_camera_device *icd);
65 static int video_dev_create(struct soc_camera_device *icd);
67 int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
73 if (clk && (!ssdd->unbalanced_power ||
74 !test_and_set_bit(0, &ssdd->clock_state))) {
75 ret = v4l2_clk_enable(clk);
77 dev_err(dev, "Cannot enable clock: %d\n", ret);
85 ret = regulator_bulk_enable(ssdd->sd_pdata.num_regulators,
86 ssdd->sd_pdata.regulators);
88 dev_err(dev, "Cannot enable regulators\n");
93 ret = ssdd->power(dev, 1);
96 "Platform failed to power-on the camera.\n");
104 regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
105 ssdd->sd_pdata.regulators);
108 v4l2_clk_disable(clk);
112 EXPORT_SYMBOL(soc_camera_power_on);
114 int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
115 struct v4l2_clk *clk)
121 err = ssdd->power(dev, 0);
124 "Platform failed to power-off the camera.\n");
129 err = regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
130 ssdd->sd_pdata.regulators);
132 dev_err(dev, "Cannot disable regulators\n");
136 if (clk && (!ssdd->unbalanced_power || test_and_clear_bit(0, &ssdd->clock_state)))
137 v4l2_clk_disable(clk);
141 EXPORT_SYMBOL(soc_camera_power_off);
143 int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd)
145 /* Should not have any effect in synchronous case */
146 return devm_regulator_bulk_get(dev, ssdd->sd_pdata.num_regulators,
147 ssdd->sd_pdata.regulators);
149 EXPORT_SYMBOL(soc_camera_power_init);
151 static int __soc_camera_power_on(struct soc_camera_device *icd)
153 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
156 ret = v4l2_subdev_call(sd, core, s_power, 1);
157 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
163 static int __soc_camera_power_off(struct soc_camera_device *icd)
165 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
168 ret = v4l2_subdev_call(sd, core, s_power, 0);
169 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
175 static int soc_camera_clock_start(struct soc_camera_host *ici)
179 if (!ici->ops->clock_start)
182 mutex_lock(&ici->clk_lock);
183 ret = ici->ops->clock_start(ici);
184 mutex_unlock(&ici->clk_lock);
189 static void soc_camera_clock_stop(struct soc_camera_host *ici)
191 if (!ici->ops->clock_stop)
194 mutex_lock(&ici->clk_lock);
195 ici->ops->clock_stop(ici);
196 mutex_unlock(&ici->clk_lock);
199 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
200 struct soc_camera_device *icd, unsigned int fourcc)
204 for (i = 0; i < icd->num_user_formats; i++)
205 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
206 return icd->user_formats + i;
209 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
212 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
213 * @ssdd: camera platform parameters
214 * @cfg: media bus configuration
215 * @return: resulting flags
217 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
218 const struct v4l2_mbus_config *cfg)
220 unsigned long f, flags = cfg->flags;
222 /* If only one of the two polarities is supported, switch to the opposite */
223 if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
224 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
225 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
226 flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
229 if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
230 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
231 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
232 flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
235 if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
236 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
237 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
238 flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
243 EXPORT_SYMBOL(soc_camera_apply_board_flags);
245 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
248 static int soc_camera_try_fmt(struct soc_camera_device *icd,
249 struct v4l2_format *f)
251 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
252 const struct soc_camera_format_xlate *xlate;
253 struct v4l2_pix_format *pix = &f->fmt.pix;
256 dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
257 pixfmtstr(pix->pixelformat), pix->width, pix->height);
259 if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
260 !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
261 pix->bytesperline = 0;
265 ret = ici->ops->try_fmt(icd, f);
269 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
273 ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
277 pix->bytesperline = max_t(u32, pix->bytesperline, ret);
279 ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
284 pix->sizeimage = max_t(u32, pix->sizeimage, ret);
289 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
290 struct v4l2_format *f)
292 struct soc_camera_device *icd = file->private_data;
294 WARN_ON(priv != file->private_data);
296 /* Only single-plane capture is supported so far */
297 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
300 /* limit format to hardware capabilities */
301 return soc_camera_try_fmt(icd, f);
304 static int soc_camera_enum_input(struct file *file, void *priv,
305 struct v4l2_input *inp)
307 struct soc_camera_device *icd = file->private_data;
312 /* default is camera */
313 inp->type = V4L2_INPUT_TYPE_CAMERA;
314 inp->std = icd->vdev->tvnorms;
315 strcpy(inp->name, "Camera");
320 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
327 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
335 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
337 struct soc_camera_device *icd = file->private_data;
338 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
340 return v4l2_subdev_call(sd, video, s_std, a);
343 static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
345 struct soc_camera_device *icd = file->private_data;
346 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
348 return v4l2_subdev_call(sd, video, g_std, a);
351 static int soc_camera_enum_framesizes(struct file *file, void *fh,
352 struct v4l2_frmsizeenum *fsize)
354 struct soc_camera_device *icd = file->private_data;
355 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
357 return ici->ops->enum_framesizes(icd, fsize);
360 static int soc_camera_reqbufs(struct file *file, void *priv,
361 struct v4l2_requestbuffers *p)
364 struct soc_camera_device *icd = file->private_data;
366 WARN_ON(priv != file->private_data);
368 if (icd->streamer && icd->streamer != file)
371 ret = vb2_reqbufs(&icd->vb2_vidq, p);
373 icd->streamer = p->count ? file : NULL;
377 static int soc_camera_querybuf(struct file *file, void *priv,
378 struct v4l2_buffer *p)
380 struct soc_camera_device *icd = file->private_data;
382 WARN_ON(priv != file->private_data);
384 return vb2_querybuf(&icd->vb2_vidq, p);
387 static int soc_camera_qbuf(struct file *file, void *priv,
388 struct v4l2_buffer *p)
390 struct soc_camera_device *icd = file->private_data;
392 WARN_ON(priv != file->private_data);
394 if (icd->streamer != file)
397 return vb2_qbuf(&icd->vb2_vidq, p);
400 static int soc_camera_dqbuf(struct file *file, void *priv,
401 struct v4l2_buffer *p)
403 struct soc_camera_device *icd = file->private_data;
405 WARN_ON(priv != file->private_data);
407 if (icd->streamer != file)
410 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
413 static int soc_camera_create_bufs(struct file *file, void *priv,
414 struct v4l2_create_buffers *create)
416 struct soc_camera_device *icd = file->private_data;
419 if (icd->streamer && icd->streamer != file)
422 ret = vb2_create_bufs(&icd->vb2_vidq, create);
424 icd->streamer = file;
428 static int soc_camera_prepare_buf(struct file *file, void *priv,
429 struct v4l2_buffer *b)
431 struct soc_camera_device *icd = file->private_data;
433 return vb2_prepare_buf(&icd->vb2_vidq, b);
436 static int soc_camera_expbuf(struct file *file, void *priv,
437 struct v4l2_exportbuffer *p)
439 struct soc_camera_device *icd = file->private_data;
441 if (icd->streamer && icd->streamer != file)
443 return vb2_expbuf(&icd->vb2_vidq, p);
446 /* Always entered with .host_lock held */
447 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
449 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
450 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
451 unsigned int i, fmts = 0, raw_fmts = 0;
453 struct v4l2_subdev_mbus_code_enum code = {
454 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
457 while (!v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code)) {
462 if (!ici->ops->get_formats)
464 * Fallback mode - the host will have to serve all
465 * sensor-provided formats one-to-one to the user
470 * First pass - only count formats this host-sensor
471 * configuration can provide
473 for (i = 0; i < raw_fmts; i++) {
474 ret = ici->ops->get_formats(icd, i, NULL);
484 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
485 if (!icd->user_formats)
488 dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
490 /* Second pass - actually fill data formats */
492 for (i = 0; i < raw_fmts; i++)
493 if (!ici->ops->get_formats) {
495 v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
496 icd->user_formats[fmts].host_fmt =
497 soc_mbus_get_fmtdesc(code.code);
498 if (icd->user_formats[fmts].host_fmt)
499 icd->user_formats[fmts++].code = code.code;
501 ret = ici->ops->get_formats(icd, i,
502 &icd->user_formats[fmts]);
508 icd->num_user_formats = fmts;
509 icd->current_fmt = &icd->user_formats[0];
514 vfree(icd->user_formats);
518 /* Always entered with .host_lock held */
519 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
521 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
523 if (ici->ops->put_formats)
524 ici->ops->put_formats(icd);
525 icd->current_fmt = NULL;
526 icd->num_user_formats = 0;
527 vfree(icd->user_formats);
528 icd->user_formats = NULL;
531 /* Called with .vb_lock held, or from the first open(2), see comment there */
532 static int soc_camera_set_fmt(struct soc_camera_device *icd,
533 struct v4l2_format *f)
535 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
536 struct v4l2_pix_format *pix = &f->fmt.pix;
539 dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
540 pixfmtstr(pix->pixelformat), pix->width, pix->height);
542 /* We always call try_fmt() before set_fmt() or set_selection() */
543 ret = soc_camera_try_fmt(icd, f);
547 ret = ici->ops->set_fmt(icd, f);
550 } else if (!icd->current_fmt ||
551 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
553 "Host driver hasn't set up current format correctly!\n");
557 icd->user_width = pix->width;
558 icd->user_height = pix->height;
559 icd->bytesperline = pix->bytesperline;
560 icd->sizeimage = pix->sizeimage;
561 icd->colorspace = pix->colorspace;
562 icd->field = pix->field;
564 dev_dbg(icd->pdev, "set width: %d height: %d\n",
565 icd->user_width, icd->user_height);
567 /* set physical bus parameters */
568 return ici->ops->set_bus_param(icd);
571 static int soc_camera_add_device(struct soc_camera_device *icd)
573 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
580 ret = soc_camera_clock_start(ici);
586 ret = ici->ops->add(icd);
597 soc_camera_clock_stop(ici);
601 static void soc_camera_remove_device(struct soc_camera_device *icd)
603 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
605 if (WARN_ON(icd != ici->icd))
608 if (ici->ops->remove)
609 ici->ops->remove(icd);
611 soc_camera_clock_stop(ici);
615 static int soc_camera_open(struct file *file)
617 struct video_device *vdev = video_devdata(file);
618 struct soc_camera_device *icd;
619 struct soc_camera_host *ici;
623 * Don't mess with the host during probe: wait until the loop in
624 * scan_add_host() completes. Also protect against a race with
625 * soc_camera_host_unregister().
627 if (mutex_lock_interruptible(&list_lock))
630 if (!vdev || !video_is_registered(vdev)) {
631 mutex_unlock(&list_lock);
635 icd = video_get_drvdata(vdev);
636 ici = to_soc_camera_host(icd->parent);
638 ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
639 mutex_unlock(&list_lock);
642 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
646 if (!to_soc_camera_control(icd)) {
647 /* No device driver attached */
652 if (mutex_lock_interruptible(&ici->host_lock)) {
658 /* Now we really have to activate the camera */
659 if (icd->use_count == 1) {
660 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
661 /* Restore parameters before the last close() per V4L2 API */
662 struct v4l2_format f = {
663 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
665 .width = icd->user_width,
666 .height = icd->user_height,
668 .colorspace = icd->colorspace,
670 icd->current_fmt->host_fmt->fourcc,
674 /* The camera could have been already on, try to reset */
675 if (sdesc->subdev_desc.reset)
677 sdesc->subdev_desc.reset(icd->control);
679 ret = soc_camera_add_device(icd);
681 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
685 ret = __soc_camera_power_on(icd);
689 pm_runtime_enable(&icd->vdev->dev);
690 ret = pm_runtime_resume(&icd->vdev->dev);
691 if (ret < 0 && ret != -ENOSYS)
695 * Try to configure with default parameters. Notice: this is the
696 * very first open, so, we cannot race against other calls,
697 * apart from someone else calling open() simultaneously, but
698 * .host_lock is protecting us against it.
700 ret = soc_camera_set_fmt(icd, &f);
704 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
707 v4l2_ctrl_handler_setup(&icd->ctrl_handler);
709 mutex_unlock(&ici->host_lock);
711 file->private_data = icd;
712 dev_dbg(icd->pdev, "camera device open\n");
717 * All errors are entered with the .host_lock held, first four also
718 * with use_count == 1
722 pm_runtime_disable(&icd->vdev->dev);
724 __soc_camera_power_off(icd);
726 soc_camera_remove_device(icd);
729 mutex_unlock(&ici->host_lock);
732 module_put(ici->ops->owner);
737 static int soc_camera_close(struct file *file)
739 struct soc_camera_device *icd = file->private_data;
740 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
742 mutex_lock(&ici->host_lock);
743 if (icd->streamer == file) {
744 if (ici->ops->init_videobuf2)
745 vb2_queue_release(&icd->vb2_vidq);
746 icd->streamer = NULL;
749 if (!icd->use_count) {
750 pm_runtime_suspend(&icd->vdev->dev);
751 pm_runtime_disable(&icd->vdev->dev);
753 __soc_camera_power_off(icd);
755 soc_camera_remove_device(icd);
758 mutex_unlock(&ici->host_lock);
760 module_put(ici->ops->owner);
762 dev_dbg(icd->pdev, "camera device close\n");
767 static ssize_t soc_camera_read(struct file *file, char __user *buf,
768 size_t count, loff_t *ppos)
770 struct soc_camera_device *icd = file->private_data;
771 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
773 dev_dbg(icd->pdev, "read called, buf %p\n", buf);
775 if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
776 return vb2_read(&icd->vb2_vidq, buf, count, ppos,
777 file->f_flags & O_NONBLOCK);
779 dev_err(icd->pdev, "camera device read not implemented\n");
784 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
786 struct soc_camera_device *icd = file->private_data;
787 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
790 dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
792 if (icd->streamer != file)
795 if (mutex_lock_interruptible(&ici->host_lock))
797 err = vb2_mmap(&icd->vb2_vidq, vma);
798 mutex_unlock(&ici->host_lock);
800 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
801 (unsigned long)vma->vm_start,
802 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
808 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
810 struct soc_camera_device *icd = file->private_data;
811 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
812 unsigned res = POLLERR;
814 if (icd->streamer != file)
817 mutex_lock(&ici->host_lock);
818 res = ici->ops->poll(file, pt);
819 mutex_unlock(&ici->host_lock);
823 static const struct v4l2_file_operations soc_camera_fops = {
824 .owner = THIS_MODULE,
825 .open = soc_camera_open,
826 .release = soc_camera_close,
827 .unlocked_ioctl = video_ioctl2,
828 .read = soc_camera_read,
829 .mmap = soc_camera_mmap,
830 .poll = soc_camera_poll,
833 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
834 struct v4l2_format *f)
836 struct soc_camera_device *icd = file->private_data;
839 WARN_ON(priv != file->private_data);
841 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
842 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
846 if (icd->streamer && icd->streamer != file)
849 if (vb2_is_streaming(&icd->vb2_vidq)) {
850 dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
854 ret = soc_camera_set_fmt(icd, f);
856 if (!ret && !icd->streamer)
857 icd->streamer = file;
862 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
863 struct v4l2_fmtdesc *f)
865 struct soc_camera_device *icd = file->private_data;
866 const struct soc_mbus_pixelfmt *format;
868 WARN_ON(priv != file->private_data);
870 if (f->index >= icd->num_user_formats)
873 format = icd->user_formats[f->index].host_fmt;
876 strlcpy(f->description, format->name, sizeof(f->description));
877 f->pixelformat = format->fourcc;
881 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
882 struct v4l2_format *f)
884 struct soc_camera_device *icd = file->private_data;
885 struct v4l2_pix_format *pix = &f->fmt.pix;
887 WARN_ON(priv != file->private_data);
889 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
892 pix->width = icd->user_width;
893 pix->height = icd->user_height;
894 pix->bytesperline = icd->bytesperline;
895 pix->sizeimage = icd->sizeimage;
896 pix->field = icd->field;
897 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
898 pix->colorspace = icd->colorspace;
899 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
900 icd->current_fmt->host_fmt->fourcc);
904 static int soc_camera_querycap(struct file *file, void *priv,
905 struct v4l2_capability *cap)
907 struct soc_camera_device *icd = file->private_data;
908 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
910 WARN_ON(priv != file->private_data);
912 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
913 return ici->ops->querycap(ici, cap);
916 static int soc_camera_streamon(struct file *file, void *priv,
917 enum v4l2_buf_type i)
919 struct soc_camera_device *icd = file->private_data;
920 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
923 WARN_ON(priv != file->private_data);
925 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
928 if (icd->streamer != file)
931 /* This calls buf_queue from host driver's videobuf2_queue_ops */
932 ret = vb2_streamon(&icd->vb2_vidq, i);
934 v4l2_subdev_call(sd, video, s_stream, 1);
939 static int soc_camera_streamoff(struct file *file, void *priv,
940 enum v4l2_buf_type i)
942 struct soc_camera_device *icd = file->private_data;
943 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
946 WARN_ON(priv != file->private_data);
948 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
951 if (icd->streamer != file)
955 * This calls buf_release from host driver's videobuf2_queue_ops for all
956 * remaining buffers. When the last buffer is freed, stop capture
958 ret = vb2_streamoff(&icd->vb2_vidq, i);
960 v4l2_subdev_call(sd, video, s_stream, 0);
965 static int soc_camera_g_selection(struct file *file, void *fh,
966 struct v4l2_selection *s)
968 struct soc_camera_device *icd = file->private_data;
969 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
971 /* With a wrong type no need to try to fall back to cropping */
972 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
975 return ici->ops->get_selection(icd, s);
978 static int soc_camera_s_selection(struct file *file, void *fh,
979 struct v4l2_selection *s)
981 struct soc_camera_device *icd = file->private_data;
982 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
985 /* In all these cases cropping emulation will not help */
986 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
987 (s->target != V4L2_SEL_TGT_COMPOSE &&
988 s->target != V4L2_SEL_TGT_CROP))
991 if (s->target == V4L2_SEL_TGT_COMPOSE) {
992 /* No output size change during a running capture! */
993 if (vb2_is_streaming(&icd->vb2_vidq) &&
994 (icd->user_width != s->r.width ||
995 icd->user_height != s->r.height))
999 * Only one user is allowed to change the output format, touch
1000 * buffers, start / stop streaming, poll for data
1002 if (icd->streamer && icd->streamer != file)
1006 if (s->target == V4L2_SEL_TGT_CROP &&
1007 vb2_is_streaming(&icd->vb2_vidq) &&
1008 ici->ops->set_liveselection)
1009 ret = ici->ops->set_liveselection(icd, s);
1011 ret = ici->ops->set_selection(icd, s);
1013 s->target == V4L2_SEL_TGT_COMPOSE) {
1014 icd->user_width = s->r.width;
1015 icd->user_height = s->r.height;
1017 icd->streamer = file;
1023 static int soc_camera_g_parm(struct file *file, void *fh,
1024 struct v4l2_streamparm *a)
1026 struct soc_camera_device *icd = file->private_data;
1027 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1029 if (ici->ops->get_parm)
1030 return ici->ops->get_parm(icd, a);
1032 return -ENOIOCTLCMD;
1035 static int soc_camera_s_parm(struct file *file, void *fh,
1036 struct v4l2_streamparm *a)
1038 struct soc_camera_device *icd = file->private_data;
1039 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1041 if (ici->ops->set_parm)
1042 return ici->ops->set_parm(icd, a);
1044 return -ENOIOCTLCMD;
1047 static int soc_camera_probe(struct soc_camera_host *ici,
1048 struct soc_camera_device *icd);
1050 /* So far this function cannot fail */
1051 static void scan_add_host(struct soc_camera_host *ici)
1053 struct soc_camera_device *icd;
1055 mutex_lock(&list_lock);
1057 list_for_each_entry(icd, &devices, list)
1058 if (icd->iface == ici->nr) {
1059 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1060 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1062 /* The camera could have been already on, try to reset */
1065 ssdd->reset(icd->control);
1067 icd->parent = ici->v4l2_dev.dev;
1070 soc_camera_probe(ici, icd);
1073 mutex_unlock(&list_lock);
1077 * It is invalid to call v4l2_clk_enable() after a successful probing
1078 * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
1080 static int soc_camera_clk_enable(struct v4l2_clk *clk)
1082 struct soc_camera_device *icd = clk->priv;
1083 struct soc_camera_host *ici;
1085 if (!icd || !icd->parent)
1088 ici = to_soc_camera_host(icd->parent);
1090 if (!try_module_get(ici->ops->owner))
1094 * If a different client is currently being probed, the host will tell
1097 return soc_camera_clock_start(ici);
1100 static void soc_camera_clk_disable(struct v4l2_clk *clk)
1102 struct soc_camera_device *icd = clk->priv;
1103 struct soc_camera_host *ici;
1105 if (!icd || !icd->parent)
1108 ici = to_soc_camera_host(icd->parent);
1110 soc_camera_clock_stop(ici);
1112 module_put(ici->ops->owner);
1116 * Eventually, it would be more logical to make the respective host the clock
1117 * owner, but then we would have to copy this struct for each ici. Besides, it
1118 * would introduce the circular dependency problem, unless we port all client
1119 * drivers to release the clock, when not in use.
1121 static const struct v4l2_clk_ops soc_camera_clk_ops = {
1122 .owner = THIS_MODULE,
1123 .enable = soc_camera_clk_enable,
1124 .disable = soc_camera_clk_disable,
1127 static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
1128 struct soc_camera_async_client *sasc)
1130 struct platform_device *pdev;
1133 mutex_lock(&list_lock);
1134 i = find_first_zero_bit(device_map, MAP_MAX_NUM);
1135 if (i < MAP_MAX_NUM)
1136 set_bit(i, device_map);
1137 mutex_unlock(&list_lock);
1138 if (i >= MAP_MAX_NUM)
1141 pdev = platform_device_alloc("soc-camera-pdrv", i);
1145 ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
1147 platform_device_put(pdev);
1156 static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
1158 struct platform_device *pdev = sasc->pdev;
1161 ret = platform_device_add(pdev);
1162 if (ret < 0 || !pdev->dev.driver)
1165 return platform_get_drvdata(pdev);
1168 /* Locking: called with .host_lock held */
1169 static int soc_camera_probe_finish(struct soc_camera_device *icd)
1171 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1172 struct v4l2_subdev_format fmt = {
1173 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1175 struct v4l2_mbus_framefmt *mf = &fmt.format;
1178 sd->grp_id = soc_camera_grp_id(icd);
1179 v4l2_set_subdev_hostdata(sd, icd);
1181 v4l2_subdev_call(sd, video, g_tvnorms, &icd->vdev->tvnorms);
1183 ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
1187 ret = soc_camera_add_device(icd);
1189 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
1193 /* At this point client .probe() should have run already */
1194 ret = soc_camera_init_user_formats(icd);
1198 icd->field = V4L2_FIELD_ANY;
1200 ret = soc_camera_video_start(icd);
1204 /* Try to improve our guess of a reasonable window format */
1205 if (!v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt)) {
1206 icd->user_width = mf->width;
1207 icd->user_height = mf->height;
1208 icd->colorspace = mf->colorspace;
1209 icd->field = mf->field;
1211 soc_camera_remove_device(icd);
1216 soc_camera_free_user_formats(icd);
1218 soc_camera_remove_device(icd);
1223 #ifdef CONFIG_I2C_BOARDINFO
1224 static int soc_camera_i2c_init(struct soc_camera_device *icd,
1225 struct soc_camera_desc *sdesc)
1227 struct soc_camera_subdev_desc *ssdd;
1228 struct i2c_client *client;
1229 struct soc_camera_host *ici;
1230 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1231 struct i2c_adapter *adap;
1232 struct v4l2_subdev *subdev;
1233 char clk_name[V4L2_CLK_NAME_SIZE];
1236 /* First find out how we link the main client */
1238 /* Async non-OF probing handled by the subdevice list */
1239 return -EPROBE_DEFER;
1242 ici = to_soc_camera_host(icd->parent);
1243 adap = i2c_get_adapter(shd->i2c_adapter_id);
1245 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1246 shd->i2c_adapter_id);
1250 ssdd = kmemdup(&sdesc->subdev_desc, sizeof(*ssdd), GFP_KERNEL);
1256 * In synchronous case we request regulators ourselves in
1257 * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
1258 * to allocate them again.
1260 ssdd->sd_pdata.num_regulators = 0;
1261 ssdd->sd_pdata.regulators = NULL;
1262 shd->board_info->platform_data = ssdd;
1264 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1265 shd->i2c_adapter_id, shd->board_info->addr);
1267 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1268 if (IS_ERR(icd->clk)) {
1269 ret = PTR_ERR(icd->clk);
1273 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1274 shd->board_info, NULL);
1280 client = v4l2_get_subdevdata(subdev);
1282 /* Use to_i2c_client(dev) to recover the i2c client */
1283 icd->control = &client->dev;
1287 v4l2_clk_unregister(icd->clk);
1292 i2c_put_adapter(adap);
1296 static void soc_camera_i2c_free(struct soc_camera_device *icd)
1298 struct i2c_client *client =
1299 to_i2c_client(to_soc_camera_control(icd));
1300 struct i2c_adapter *adap;
1301 struct soc_camera_subdev_desc *ssdd;
1303 icd->control = NULL;
1307 adap = client->adapter;
1308 ssdd = client->dev.platform_data;
1309 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1310 i2c_unregister_device(client);
1311 i2c_put_adapter(adap);
1313 v4l2_clk_unregister(icd->clk);
1318 * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
1319 * internal global mutex, therefore cannot race against other asynchronous
1320 * events. Until notifier->complete() (soc_camera_async_complete()) is called,
1321 * the video device node is not registered and no V4L fops can occur. Unloading
1322 * of the host driver also calls a v4l2-async function, so also there we're
1325 static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
1326 struct v4l2_subdev *sd,
1327 struct v4l2_async_subdev *asd)
1329 struct soc_camera_async_client *sasc = container_of(notifier,
1330 struct soc_camera_async_client, notifier);
1331 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1333 if (asd == sasc->sensor && !WARN_ON(icd->control)) {
1334 struct i2c_client *client = v4l2_get_subdevdata(sd);
1337 * Only now we get subdevice-specific information like
1338 * regulators, flags, callbacks, etc.
1341 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1342 struct soc_camera_subdev_desc *ssdd =
1343 soc_camera_i2c_to_desc(client);
1345 memcpy(&sdesc->subdev_desc, ssdd,
1346 sizeof(sdesc->subdev_desc));
1348 ssdd->reset(&client->dev);
1351 icd->control = &client->dev;
1358 static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
1359 struct v4l2_subdev *sd,
1360 struct v4l2_async_subdev *asd)
1362 struct soc_camera_async_client *sasc = container_of(notifier,
1363 struct soc_camera_async_client, notifier);
1364 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1366 icd->control = NULL;
1369 v4l2_clk_unregister(icd->clk);
1374 static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
1376 struct soc_camera_async_client *sasc = container_of(notifier,
1377 struct soc_camera_async_client, notifier);
1378 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1380 if (to_soc_camera_control(icd)) {
1381 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1384 mutex_lock(&list_lock);
1385 ret = soc_camera_probe(ici, icd);
1386 mutex_unlock(&list_lock);
1394 static int scan_async_group(struct soc_camera_host *ici,
1395 struct v4l2_async_subdev **asd, unsigned int size)
1397 struct soc_camera_async_subdev *sasd;
1398 struct soc_camera_async_client *sasc;
1399 struct soc_camera_device *icd;
1400 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1401 char clk_name[V4L2_CLK_NAME_SIZE];
1405 /* First look for a sensor */
1406 for (i = 0; i < size; i++) {
1407 sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
1408 if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
1412 if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
1414 dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
1418 /* Or shall this be managed by the soc-camera device? */
1419 sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
1423 /* HACK: just need a != NULL */
1424 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1426 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1430 sasc->sensor = &sasd->asd;
1432 icd = soc_camera_add_pdev(sasc);
1438 sasc->notifier.subdevs = asd;
1439 sasc->notifier.num_subdevs = size;
1440 sasc->notifier.bound = soc_camera_async_bound;
1441 sasc->notifier.unbind = soc_camera_async_unbind;
1442 sasc->notifier.complete = soc_camera_async_complete;
1445 icd->parent = ici->v4l2_dev.dev;
1447 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1448 sasd->asd.match.i2c.adapter_id,
1449 sasd->asd.match.i2c.address);
1451 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1452 if (IS_ERR(icd->clk)) {
1453 ret = PTR_ERR(icd->clk);
1457 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1461 v4l2_clk_unregister(icd->clk);
1464 platform_device_del(sasc->pdev);
1466 platform_device_put(sasc->pdev);
1468 devm_kfree(ici->v4l2_dev.dev, sasc);
1469 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1474 static void scan_async_host(struct soc_camera_host *ici)
1476 struct v4l2_async_subdev **asd;
1479 for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
1480 scan_async_group(ici, asd, ici->asd_sizes[j]);
1481 asd += ici->asd_sizes[j];
1485 #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
1486 #define soc_camera_i2c_free(icd) do {} while (0)
1487 #define scan_async_host(ici) do {} while (0)
1492 struct soc_of_info {
1493 struct soc_camera_async_subdev sasd;
1494 struct soc_camera_async_client sasc;
1495 struct v4l2_async_subdev *subdev;
1498 static int soc_of_bind(struct soc_camera_host *ici,
1499 struct device_node *ep,
1500 struct device_node *remote)
1502 struct soc_camera_device *icd;
1503 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1504 struct soc_camera_async_client *sasc;
1505 struct soc_of_info *info;
1506 struct i2c_client *client;
1507 char clk_name[V4L2_CLK_NAME_SIZE];
1510 /* allocate a new subdev and add match info to it */
1511 info = devm_kzalloc(ici->v4l2_dev.dev, sizeof(struct soc_of_info),
1516 info->sasd.asd.match.fwnode.fwnode = of_fwnode_handle(remote);
1517 info->sasd.asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
1518 info->subdev = &info->sasd.asd;
1520 /* Or shall this be managed by the soc-camera device? */
1523 /* HACK: just need a != NULL */
1524 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1526 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1530 sasc->sensor = &info->sasd.asd;
1532 icd = soc_camera_add_pdev(sasc);
1538 sasc->notifier.subdevs = &info->subdev;
1539 sasc->notifier.num_subdevs = 1;
1540 sasc->notifier.bound = soc_camera_async_bound;
1541 sasc->notifier.unbind = soc_camera_async_unbind;
1542 sasc->notifier.complete = soc_camera_async_complete;
1545 icd->parent = ici->v4l2_dev.dev;
1547 client = of_find_i2c_device_by_node(remote);
1550 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
1551 client->adapter->nr, client->addr);
1553 v4l2_clk_name_of(clk_name, sizeof(clk_name), remote);
1555 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1556 if (IS_ERR(icd->clk)) {
1557 ret = PTR_ERR(icd->clk);
1561 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1565 v4l2_clk_unregister(icd->clk);
1568 platform_device_del(sasc->pdev);
1570 platform_device_put(sasc->pdev);
1572 devm_kfree(ici->v4l2_dev.dev, info);
1573 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1578 static void scan_of_host(struct soc_camera_host *ici)
1580 struct device *dev = ici->v4l2_dev.dev;
1581 struct device_node *np = dev->of_node;
1582 struct device_node *epn = NULL, *ren;
1585 for (i = 0; ; i++) {
1586 epn = of_graph_get_next_endpoint(np, epn);
1590 ren = of_graph_get_remote_port(epn);
1592 dev_notice(dev, "no remote for %pOF\n", epn);
1596 /* so we now have a remote node to connect */
1598 soc_of_bind(ici, epn, ren->parent);
1603 dev_err(dev, "multiple subdevices aren't supported yet!\n");
1612 static inline void scan_of_host(struct soc_camera_host *ici) { }
1615 /* Called during host-driver probe */
1616 static int soc_camera_probe(struct soc_camera_host *ici,
1617 struct soc_camera_device *icd)
1619 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1620 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1621 struct device *control = NULL;
1624 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1627 * Currently the subdev with the largest number of controls (13) is
1628 * ov6550. So let's pick 16 as a hint for the control handler. Note
1629 * that this is a hint only: too large and you waste some memory, too
1630 * small and there is a (very) small performance hit when looking up
1631 * controls in the internal hash.
1633 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1637 /* Must have icd->vdev before registering the device */
1638 ret = video_dev_create(icd);
1643 * ..._video_start() will create a device node, video_register_device()
1644 * itself is protected against concurrent open() calls, but we also have
1645 * to protect our data also during client probing.
1648 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1649 if (shd->board_info) {
1650 ret = soc_camera_i2c_init(icd, sdesc);
1651 if (ret < 0 && ret != -EPROBE_DEFER)
1653 } else if (!shd->add_device || !shd->del_device) {
1657 ret = soc_camera_clock_start(ici);
1661 if (shd->module_name)
1662 ret = request_module(shd->module_name);
1664 ret = shd->add_device(icd);
1669 * FIXME: this is racy, have to use driver-binding notification,
1670 * when it is available
1672 control = to_soc_camera_control(icd);
1673 if (!control || !control->driver || !dev_get_drvdata(control) ||
1674 !try_module_get(control->driver->owner)) {
1675 shd->del_device(icd);
1681 mutex_lock(&ici->host_lock);
1682 ret = soc_camera_probe_finish(icd);
1683 mutex_unlock(&ici->host_lock);
1690 if (shd->board_info) {
1691 soc_camera_i2c_free(icd);
1693 shd->del_device(icd);
1694 module_put(control->driver->owner);
1697 soc_camera_clock_stop(ici);
1701 video_device_release(icd->vdev);
1705 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1710 * This is called on device_unregister, which only means we have to disconnect
1711 * from the host, but not remove ourselves from the device list. With
1712 * asynchronous client probing this can also be called without
1713 * soc_camera_probe_finish() having run. Careful with clean up.
1715 static int soc_camera_remove(struct soc_camera_device *icd)
1717 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1718 struct video_device *vdev = icd->vdev;
1720 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1722 video_unregister_device(vdev);
1726 if (sdesc->host_desc.board_info) {
1727 soc_camera_i2c_free(icd);
1729 struct device *dev = to_soc_camera_control(icd);
1730 struct device_driver *drv = dev ? dev->driver : NULL;
1732 sdesc->host_desc.del_device(icd);
1733 module_put(drv->owner);
1737 if (icd->num_user_formats)
1738 soc_camera_free_user_formats(icd);
1741 /* For the synchronous case */
1742 v4l2_clk_unregister(icd->clk);
1747 platform_device_unregister(icd->sasc->pdev);
1752 static int default_g_selection(struct soc_camera_device *icd,
1753 struct v4l2_selection *sel)
1755 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1756 struct v4l2_subdev_selection sdsel = {
1757 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1758 .target = sel->target,
1762 ret = v4l2_subdev_call(sd, pad, get_selection, NULL, &sdsel);
1769 static int default_s_selection(struct soc_camera_device *icd,
1770 struct v4l2_selection *sel)
1772 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1773 struct v4l2_subdev_selection sdsel = {
1774 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1775 .target = sel->target,
1776 .flags = sel->flags,
1781 ret = v4l2_subdev_call(sd, pad, set_selection, NULL, &sdsel);
1788 static int default_g_parm(struct soc_camera_device *icd,
1789 struct v4l2_streamparm *parm)
1791 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1792 return v4l2_subdev_call(sd, video, g_parm, parm);
1795 static int default_s_parm(struct soc_camera_device *icd,
1796 struct v4l2_streamparm *parm)
1798 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1799 return v4l2_subdev_call(sd, video, s_parm, parm);
1802 static int default_enum_framesizes(struct soc_camera_device *icd,
1803 struct v4l2_frmsizeenum *fsize)
1806 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1807 const struct soc_camera_format_xlate *xlate;
1808 struct v4l2_subdev_frame_size_enum fse = {
1809 .index = fsize->index,
1810 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1813 xlate = soc_camera_xlate_by_fourcc(icd, fsize->pixel_format);
1816 fse.code = xlate->code;
1818 ret = v4l2_subdev_call(sd, pad, enum_frame_size, NULL, &fse);
1822 if (fse.min_width == fse.max_width &&
1823 fse.min_height == fse.max_height) {
1824 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1825 fsize->discrete.width = fse.min_width;
1826 fsize->discrete.height = fse.min_height;
1829 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1830 fsize->stepwise.min_width = fse.min_width;
1831 fsize->stepwise.max_width = fse.max_width;
1832 fsize->stepwise.min_height = fse.min_height;
1833 fsize->stepwise.max_height = fse.max_height;
1834 fsize->stepwise.step_width = 1;
1835 fsize->stepwise.step_height = 1;
1839 int soc_camera_host_register(struct soc_camera_host *ici)
1841 struct soc_camera_host *ix;
1844 if (!ici || !ici->ops ||
1845 !ici->ops->try_fmt ||
1846 !ici->ops->set_fmt ||
1847 !ici->ops->set_bus_param ||
1848 !ici->ops->querycap ||
1849 !ici->ops->init_videobuf2 ||
1854 if (!ici->ops->set_selection)
1855 ici->ops->set_selection = default_s_selection;
1856 if (!ici->ops->get_selection)
1857 ici->ops->get_selection = default_g_selection;
1858 if (!ici->ops->set_parm)
1859 ici->ops->set_parm = default_s_parm;
1860 if (!ici->ops->get_parm)
1861 ici->ops->get_parm = default_g_parm;
1862 if (!ici->ops->enum_framesizes)
1863 ici->ops->enum_framesizes = default_enum_framesizes;
1865 mutex_lock(&list_lock);
1866 list_for_each_entry(ix, &hosts, list) {
1867 if (ix->nr == ici->nr) {
1873 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1877 list_add_tail(&ici->list, &hosts);
1878 mutex_unlock(&list_lock);
1880 mutex_init(&ici->host_lock);
1881 mutex_init(&ici->clk_lock);
1883 if (ici->v4l2_dev.dev->of_node)
1885 else if (ici->asd_sizes)
1887 * No OF, host with a list of subdevices. Don't try to mix
1888 * modes by initialising some groups statically and some
1891 scan_async_host(ici);
1893 /* Legacy: static platform devices from board data */
1899 mutex_unlock(&list_lock);
1902 EXPORT_SYMBOL(soc_camera_host_register);
1904 /* Unregister all clients! */
1905 void soc_camera_host_unregister(struct soc_camera_host *ici)
1907 struct soc_camera_device *icd, *tmp;
1908 struct soc_camera_async_client *sasc;
1909 LIST_HEAD(notifiers);
1911 mutex_lock(&list_lock);
1912 list_del(&ici->list);
1913 list_for_each_entry(icd, &devices, list)
1914 if (icd->iface == ici->nr && icd->sasc) {
1915 /* as long as we hold the device, sasc won't be freed */
1916 get_device(icd->pdev);
1917 list_add(&icd->sasc->list, ¬ifiers);
1919 mutex_unlock(&list_lock);
1921 list_for_each_entry(sasc, ¬ifiers, list) {
1922 /* Must call unlocked to avoid AB-BA dead-lock */
1923 v4l2_async_notifier_unregister(&sasc->notifier);
1924 put_device(&sasc->pdev->dev);
1927 mutex_lock(&list_lock);
1929 list_for_each_entry_safe(icd, tmp, &devices, list)
1930 if (icd->iface == ici->nr)
1931 soc_camera_remove(icd);
1933 mutex_unlock(&list_lock);
1935 v4l2_device_unregister(&ici->v4l2_dev);
1937 EXPORT_SYMBOL(soc_camera_host_unregister);
1939 /* Image capture device */
1940 static int soc_camera_device_register(struct soc_camera_device *icd)
1942 struct soc_camera_device *ix;
1945 mutex_lock(&list_lock);
1946 for (i = 0; i < 256 && num < 0; i++) {
1948 /* Check if this index is available on this interface */
1949 list_for_each_entry(ix, &devices, list) {
1950 if (ix->iface == icd->iface && ix->devnum == i) {
1959 * ok, we have 256 cameras on this host...
1960 * man, stay reasonable...
1962 mutex_unlock(&list_lock);
1968 icd->host_priv = NULL;
1971 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
1974 i = to_platform_device(icd->pdev)->id;
1976 /* One static (legacy) soc-camera platform device */
1978 if (i >= MAP_MAX_NUM) {
1979 mutex_unlock(&list_lock);
1982 set_bit(i, device_map);
1983 list_add_tail(&icd->list, &devices);
1984 mutex_unlock(&list_lock);
1989 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1990 .vidioc_querycap = soc_camera_querycap,
1991 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1992 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1993 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1994 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1995 .vidioc_enum_input = soc_camera_enum_input,
1996 .vidioc_g_input = soc_camera_g_input,
1997 .vidioc_s_input = soc_camera_s_input,
1998 .vidioc_s_std = soc_camera_s_std,
1999 .vidioc_g_std = soc_camera_g_std,
2000 .vidioc_enum_framesizes = soc_camera_enum_framesizes,
2001 .vidioc_reqbufs = soc_camera_reqbufs,
2002 .vidioc_querybuf = soc_camera_querybuf,
2003 .vidioc_qbuf = soc_camera_qbuf,
2004 .vidioc_dqbuf = soc_camera_dqbuf,
2005 .vidioc_create_bufs = soc_camera_create_bufs,
2006 .vidioc_prepare_buf = soc_camera_prepare_buf,
2007 .vidioc_expbuf = soc_camera_expbuf,
2008 .vidioc_streamon = soc_camera_streamon,
2009 .vidioc_streamoff = soc_camera_streamoff,
2010 .vidioc_g_selection = soc_camera_g_selection,
2011 .vidioc_s_selection = soc_camera_s_selection,
2012 .vidioc_g_parm = soc_camera_g_parm,
2013 .vidioc_s_parm = soc_camera_s_parm,
2016 static int video_dev_create(struct soc_camera_device *icd)
2018 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2019 struct video_device *vdev = video_device_alloc();
2024 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
2026 vdev->v4l2_dev = &ici->v4l2_dev;
2027 vdev->fops = &soc_camera_fops;
2028 vdev->ioctl_ops = &soc_camera_ioctl_ops;
2029 vdev->release = video_device_release;
2030 vdev->ctrl_handler = &icd->ctrl_handler;
2031 vdev->lock = &ici->host_lock;
2039 * Called from soc_camera_probe() above with .host_lock held
2041 static int soc_camera_video_start(struct soc_camera_device *icd)
2043 const struct device_type *type = icd->vdev->dev.type;
2049 video_set_drvdata(icd->vdev, icd);
2050 if (icd->vdev->tvnorms == 0) {
2051 /* disable the STD API if there are no tvnorms defined */
2052 v4l2_disable_ioctl(icd->vdev, VIDIOC_G_STD);
2053 v4l2_disable_ioctl(icd->vdev, VIDIOC_S_STD);
2054 v4l2_disable_ioctl(icd->vdev, VIDIOC_ENUMSTD);
2056 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
2058 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
2062 /* Restore device type, possibly set by the subdevice driver */
2063 icd->vdev->dev.type = type;
2068 static int soc_camera_pdrv_probe(struct platform_device *pdev)
2070 struct soc_camera_desc *sdesc = pdev->dev.platform_data;
2071 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
2072 struct soc_camera_device *icd;
2078 icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
2083 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
2084 * regulator allocation is a dummy. They are actually requested by the
2085 * subdevice driver, using soc_camera_power_init(). Also note, that in
2086 * that case regulators are attached to the I2C device and not to the
2087 * camera platform device.
2089 ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators,
2090 ssdd->sd_pdata.regulators);
2094 icd->iface = sdesc->host_desc.bus_id;
2096 icd->pdev = &pdev->dev;
2097 platform_set_drvdata(pdev, icd);
2099 icd->user_width = DEFAULT_WIDTH;
2100 icd->user_height = DEFAULT_HEIGHT;
2102 return soc_camera_device_register(icd);
2106 * Only called on rmmod for each platform device, since they are not
2107 * hot-pluggable. Now we know, that all our users - hosts and devices have
2108 * been unloaded already
2110 static int soc_camera_pdrv_remove(struct platform_device *pdev)
2112 struct soc_camera_device *icd = platform_get_drvdata(pdev);
2123 * In synchronous mode with static platform devices this is called in a
2124 * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
2125 * no need to lock. In asynchronous case the caller -
2126 * soc_camera_host_unregister() - already holds the lock
2128 if (test_bit(i, device_map)) {
2129 clear_bit(i, device_map);
2130 list_del(&icd->list);
2136 static struct platform_driver __refdata soc_camera_pdrv = {
2137 .probe = soc_camera_pdrv_probe,
2138 .remove = soc_camera_pdrv_remove,
2140 .name = "soc-camera-pdrv",
2144 module_platform_driver(soc_camera_pdrv);
2146 MODULE_DESCRIPTION("Image capture bus driver");
2147 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2148 MODULE_LICENSE("GPL");
2149 MODULE_ALIAS("platform:soc-camera-pdrv");