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/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
32 #include <media/soc_camera.h>
33 #include <media/soc_mediabus.h>
34 #include <media/v4l2-async.h>
35 #include <media/v4l2-clk.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-dev.h>
39 #include <media/v4l2-of.h>
40 #include <media/videobuf-core.h>
41 #include <media/videobuf2-core.h>
43 /* Default to VGA resolution */
44 #define DEFAULT_WIDTH 640
45 #define DEFAULT_HEIGHT 480
47 #define is_streaming(ici, icd) \
48 (((ici)->ops->init_videobuf) ? \
49 (icd)->vb_vidq.streaming : \
50 vb2_is_streaming(&(icd)->vb2_vidq))
52 #define MAP_MAX_NUM 32
53 static DECLARE_BITMAP(device_map, MAP_MAX_NUM);
54 static LIST_HEAD(hosts);
55 static LIST_HEAD(devices);
57 * Protects lists and bitmaps of hosts and devices.
58 * Lock nesting: Ok to take ->host_lock under list_lock.
60 static DEFINE_MUTEX(list_lock);
62 struct soc_camera_async_client {
63 struct v4l2_async_subdev *sensor;
64 struct v4l2_async_notifier notifier;
65 struct platform_device *pdev;
66 struct list_head list; /* needed for clean up */
69 static int soc_camera_video_start(struct soc_camera_device *icd);
70 static int video_dev_create(struct soc_camera_device *icd);
72 int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
78 if (clk && (!ssdd->unbalanced_power ||
79 !test_and_set_bit(0, &ssdd->clock_state))) {
80 ret = v4l2_clk_enable(clk);
82 dev_err(dev, "Cannot enable clock: %d\n", ret);
90 ret = regulator_bulk_enable(ssdd->sd_pdata.num_regulators,
91 ssdd->sd_pdata.regulators);
93 dev_err(dev, "Cannot enable regulators\n");
98 ret = ssdd->power(dev, 1);
101 "Platform failed to power-on the camera.\n");
109 regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
110 ssdd->sd_pdata.regulators);
113 v4l2_clk_disable(clk);
117 EXPORT_SYMBOL(soc_camera_power_on);
119 int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
120 struct v4l2_clk *clk)
126 err = ssdd->power(dev, 0);
129 "Platform failed to power-off the camera.\n");
134 err = regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
135 ssdd->sd_pdata.regulators);
137 dev_err(dev, "Cannot disable regulators\n");
141 if (clk && (!ssdd->unbalanced_power || test_and_clear_bit(0, &ssdd->clock_state)))
142 v4l2_clk_disable(clk);
146 EXPORT_SYMBOL(soc_camera_power_off);
148 int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd)
150 /* Should not have any effect in synchronous case */
151 return devm_regulator_bulk_get(dev, ssdd->sd_pdata.num_regulators,
152 ssdd->sd_pdata.regulators);
154 EXPORT_SYMBOL(soc_camera_power_init);
156 static int __soc_camera_power_on(struct soc_camera_device *icd)
158 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
161 ret = v4l2_subdev_call(sd, core, s_power, 1);
162 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
168 static int __soc_camera_power_off(struct soc_camera_device *icd)
170 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
173 ret = v4l2_subdev_call(sd, core, s_power, 0);
174 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
180 static int soc_camera_clock_start(struct soc_camera_host *ici)
184 if (!ici->ops->clock_start)
187 mutex_lock(&ici->clk_lock);
188 ret = ici->ops->clock_start(ici);
189 mutex_unlock(&ici->clk_lock);
194 static void soc_camera_clock_stop(struct soc_camera_host *ici)
196 if (!ici->ops->clock_stop)
199 mutex_lock(&ici->clk_lock);
200 ici->ops->clock_stop(ici);
201 mutex_unlock(&ici->clk_lock);
204 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
205 struct soc_camera_device *icd, unsigned int fourcc)
209 for (i = 0; i < icd->num_user_formats; i++)
210 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
211 return icd->user_formats + i;
214 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
217 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
218 * @ssdd: camera platform parameters
219 * @cfg: media bus configuration
220 * @return: resulting flags
222 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
223 const struct v4l2_mbus_config *cfg)
225 unsigned long f, flags = cfg->flags;
227 /* If only one of the two polarities is supported, switch to the opposite */
228 if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
229 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
230 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
231 flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
234 if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
235 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
236 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
237 flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
240 if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
241 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
242 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
243 flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
248 EXPORT_SYMBOL(soc_camera_apply_board_flags);
250 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
253 static int soc_camera_try_fmt(struct soc_camera_device *icd,
254 struct v4l2_format *f)
256 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
257 const struct soc_camera_format_xlate *xlate;
258 struct v4l2_pix_format *pix = &f->fmt.pix;
261 dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
262 pixfmtstr(pix->pixelformat), pix->width, pix->height);
264 if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
265 !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
266 pix->bytesperline = 0;
270 ret = ici->ops->try_fmt(icd, f);
274 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
278 ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
282 pix->bytesperline = max_t(u32, pix->bytesperline, ret);
284 ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
289 pix->sizeimage = max_t(u32, pix->sizeimage, ret);
294 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
295 struct v4l2_format *f)
297 struct soc_camera_device *icd = file->private_data;
299 WARN_ON(priv != file->private_data);
301 /* Only single-plane capture is supported so far */
302 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
305 /* limit format to hardware capabilities */
306 return soc_camera_try_fmt(icd, f);
309 static int soc_camera_enum_input(struct file *file, void *priv,
310 struct v4l2_input *inp)
315 /* default is camera */
316 inp->type = V4L2_INPUT_TYPE_CAMERA;
317 strcpy(inp->name, "Camera");
322 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
329 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
337 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
339 struct soc_camera_device *icd = file->private_data;
340 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
342 return v4l2_subdev_call(sd, video, s_std, a);
345 static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
347 struct soc_camera_device *icd = file->private_data;
348 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
350 return v4l2_subdev_call(sd, video, g_std, a);
353 static int soc_camera_enum_framesizes(struct file *file, void *fh,
354 struct v4l2_frmsizeenum *fsize)
356 struct soc_camera_device *icd = file->private_data;
357 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
359 return ici->ops->enum_framesizes(icd, fsize);
362 static int soc_camera_reqbufs(struct file *file, void *priv,
363 struct v4l2_requestbuffers *p)
366 struct soc_camera_device *icd = file->private_data;
367 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
369 WARN_ON(priv != file->private_data);
371 if (icd->streamer && icd->streamer != file)
374 if (ici->ops->init_videobuf) {
375 ret = videobuf_reqbufs(&icd->vb_vidq, p);
379 ret = ici->ops->reqbufs(icd, p);
381 ret = vb2_reqbufs(&icd->vb2_vidq, p);
384 if (!ret && !icd->streamer)
385 icd->streamer = file;
390 static int soc_camera_querybuf(struct file *file, void *priv,
391 struct v4l2_buffer *p)
393 struct soc_camera_device *icd = file->private_data;
394 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
396 WARN_ON(priv != file->private_data);
398 if (ici->ops->init_videobuf)
399 return videobuf_querybuf(&icd->vb_vidq, p);
401 return vb2_querybuf(&icd->vb2_vidq, p);
404 static int soc_camera_qbuf(struct file *file, void *priv,
405 struct v4l2_buffer *p)
407 struct soc_camera_device *icd = file->private_data;
408 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
410 WARN_ON(priv != file->private_data);
412 if (icd->streamer != file)
415 if (ici->ops->init_videobuf)
416 return videobuf_qbuf(&icd->vb_vidq, p);
418 return vb2_qbuf(&icd->vb2_vidq, p);
421 static int soc_camera_dqbuf(struct file *file, void *priv,
422 struct v4l2_buffer *p)
424 struct soc_camera_device *icd = file->private_data;
425 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
427 WARN_ON(priv != file->private_data);
429 if (icd->streamer != file)
432 if (ici->ops->init_videobuf)
433 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
435 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
438 static int soc_camera_create_bufs(struct file *file, void *priv,
439 struct v4l2_create_buffers *create)
441 struct soc_camera_device *icd = file->private_data;
442 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
445 if (ici->ops->init_videobuf)
448 return vb2_create_bufs(&icd->vb2_vidq, create);
451 static int soc_camera_prepare_buf(struct file *file, void *priv,
452 struct v4l2_buffer *b)
454 struct soc_camera_device *icd = file->private_data;
455 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
458 if (ici->ops->init_videobuf)
461 return vb2_prepare_buf(&icd->vb2_vidq, b);
464 static int soc_camera_expbuf(struct file *file, void *priv,
465 struct v4l2_exportbuffer *p)
467 struct soc_camera_device *icd = file->private_data;
468 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
470 if (icd->streamer != file)
474 if (ici->ops->init_videobuf)
477 return vb2_expbuf(&icd->vb2_vidq, p);
480 /* Always entered with .host_lock held */
481 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
483 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
484 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
485 unsigned int i, fmts = 0, raw_fmts = 0;
489 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
492 if (!ici->ops->get_formats)
494 * Fallback mode - the host will have to serve all
495 * sensor-provided formats one-to-one to the user
500 * First pass - only count formats this host-sensor
501 * configuration can provide
503 for (i = 0; i < raw_fmts; i++) {
504 ret = ici->ops->get_formats(icd, i, NULL);
514 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
515 if (!icd->user_formats)
518 dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
520 /* Second pass - actually fill data formats */
522 for (i = 0; i < raw_fmts; i++)
523 if (!ici->ops->get_formats) {
524 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
525 icd->user_formats[fmts].host_fmt =
526 soc_mbus_get_fmtdesc(code);
527 if (icd->user_formats[fmts].host_fmt)
528 icd->user_formats[fmts++].code = code;
530 ret = ici->ops->get_formats(icd, i,
531 &icd->user_formats[fmts]);
537 icd->num_user_formats = fmts;
538 icd->current_fmt = &icd->user_formats[0];
543 vfree(icd->user_formats);
547 /* Always entered with .host_lock held */
548 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
550 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
552 if (ici->ops->put_formats)
553 ici->ops->put_formats(icd);
554 icd->current_fmt = NULL;
555 icd->num_user_formats = 0;
556 vfree(icd->user_formats);
557 icd->user_formats = NULL;
560 /* Called with .vb_lock held, or from the first open(2), see comment there */
561 static int soc_camera_set_fmt(struct soc_camera_device *icd,
562 struct v4l2_format *f)
564 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
565 struct v4l2_pix_format *pix = &f->fmt.pix;
568 dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
569 pixfmtstr(pix->pixelformat), pix->width, pix->height);
571 /* We always call try_fmt() before set_fmt() or set_crop() */
572 ret = soc_camera_try_fmt(icd, f);
576 ret = ici->ops->set_fmt(icd, f);
579 } else if (!icd->current_fmt ||
580 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
582 "Host driver hasn't set up current format correctly!\n");
586 icd->user_width = pix->width;
587 icd->user_height = pix->height;
588 icd->bytesperline = pix->bytesperline;
589 icd->sizeimage = pix->sizeimage;
590 icd->colorspace = pix->colorspace;
591 icd->field = pix->field;
592 if (ici->ops->init_videobuf)
593 icd->vb_vidq.field = pix->field;
595 dev_dbg(icd->pdev, "set width: %d height: %d\n",
596 icd->user_width, icd->user_height);
598 /* set physical bus parameters */
599 return ici->ops->set_bus_param(icd);
602 static int soc_camera_add_device(struct soc_camera_device *icd)
604 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
611 ret = soc_camera_clock_start(ici);
617 ret = ici->ops->add(icd);
628 soc_camera_clock_stop(ici);
632 static void soc_camera_remove_device(struct soc_camera_device *icd)
634 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
636 if (WARN_ON(icd != ici->icd))
639 if (ici->ops->remove)
640 ici->ops->remove(icd);
642 soc_camera_clock_stop(ici);
646 static int soc_camera_open(struct file *file)
648 struct video_device *vdev = video_devdata(file);
649 struct soc_camera_device *icd;
650 struct soc_camera_host *ici;
654 * Don't mess with the host during probe: wait until the loop in
655 * scan_add_host() completes. Also protect against a race with
656 * soc_camera_host_unregister().
658 if (mutex_lock_interruptible(&list_lock))
661 if (!vdev || !video_is_registered(vdev)) {
662 mutex_unlock(&list_lock);
666 icd = video_get_drvdata(vdev);
667 ici = to_soc_camera_host(icd->parent);
669 ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
670 mutex_unlock(&list_lock);
673 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
677 if (!to_soc_camera_control(icd)) {
678 /* No device driver attached */
683 if (mutex_lock_interruptible(&ici->host_lock)) {
689 /* Now we really have to activate the camera */
690 if (icd->use_count == 1) {
691 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
692 /* Restore parameters before the last close() per V4L2 API */
693 struct v4l2_format f = {
694 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
696 .width = icd->user_width,
697 .height = icd->user_height,
699 .colorspace = icd->colorspace,
701 icd->current_fmt->host_fmt->fourcc,
705 /* The camera could have been already on, try to reset */
706 if (sdesc->subdev_desc.reset)
708 sdesc->subdev_desc.reset(icd->control);
710 ret = soc_camera_add_device(icd);
712 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
716 ret = __soc_camera_power_on(icd);
720 pm_runtime_enable(&icd->vdev->dev);
721 ret = pm_runtime_resume(&icd->vdev->dev);
722 if (ret < 0 && ret != -ENOSYS)
726 * Try to configure with default parameters. Notice: this is the
727 * very first open, so, we cannot race against other calls,
728 * apart from someone else calling open() simultaneously, but
729 * .host_lock is protecting us against it.
731 ret = soc_camera_set_fmt(icd, &f);
735 if (ici->ops->init_videobuf) {
736 ici->ops->init_videobuf(&icd->vb_vidq, icd);
738 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
742 v4l2_ctrl_handler_setup(&icd->ctrl_handler);
744 mutex_unlock(&ici->host_lock);
746 file->private_data = icd;
747 dev_dbg(icd->pdev, "camera device open\n");
752 * All errors are entered with the .host_lock held, first four also
753 * with use_count == 1
757 pm_runtime_disable(&icd->vdev->dev);
759 __soc_camera_power_off(icd);
761 soc_camera_remove_device(icd);
764 mutex_unlock(&ici->host_lock);
767 module_put(ici->ops->owner);
772 static int soc_camera_close(struct file *file)
774 struct soc_camera_device *icd = file->private_data;
775 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
777 mutex_lock(&ici->host_lock);
779 if (!icd->use_count) {
780 pm_runtime_suspend(&icd->vdev->dev);
781 pm_runtime_disable(&icd->vdev->dev);
783 if (ici->ops->init_videobuf2)
784 vb2_queue_release(&icd->vb2_vidq);
785 __soc_camera_power_off(icd);
787 soc_camera_remove_device(icd);
790 if (icd->streamer == file)
791 icd->streamer = NULL;
792 mutex_unlock(&ici->host_lock);
794 module_put(ici->ops->owner);
796 dev_dbg(icd->pdev, "camera device close\n");
801 static ssize_t soc_camera_read(struct file *file, char __user *buf,
802 size_t count, loff_t *ppos)
804 struct soc_camera_device *icd = file->private_data;
805 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
807 dev_dbg(icd->pdev, "read called, buf %p\n", buf);
809 if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
810 return vb2_read(&icd->vb2_vidq, buf, count, ppos,
811 file->f_flags & O_NONBLOCK);
813 dev_err(icd->pdev, "camera device read not implemented\n");
818 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
820 struct soc_camera_device *icd = file->private_data;
821 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
824 dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
826 if (icd->streamer != file)
829 if (mutex_lock_interruptible(&ici->host_lock))
831 if (ici->ops->init_videobuf)
832 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
834 err = vb2_mmap(&icd->vb2_vidq, vma);
835 mutex_unlock(&ici->host_lock);
837 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
838 (unsigned long)vma->vm_start,
839 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
845 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
847 struct soc_camera_device *icd = file->private_data;
848 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
849 unsigned res = POLLERR;
851 if (icd->streamer != file)
854 mutex_lock(&ici->host_lock);
855 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream))
856 dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
858 res = ici->ops->poll(file, pt);
859 mutex_unlock(&ici->host_lock);
863 static struct v4l2_file_operations soc_camera_fops = {
864 .owner = THIS_MODULE,
865 .open = soc_camera_open,
866 .release = soc_camera_close,
867 .unlocked_ioctl = video_ioctl2,
868 .read = soc_camera_read,
869 .mmap = soc_camera_mmap,
870 .poll = soc_camera_poll,
873 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
874 struct v4l2_format *f)
876 struct soc_camera_device *icd = file->private_data;
879 WARN_ON(priv != file->private_data);
881 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
882 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
886 if (icd->streamer && icd->streamer != file)
889 if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
890 dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
894 ret = soc_camera_set_fmt(icd, f);
896 if (!ret && !icd->streamer)
897 icd->streamer = file;
902 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
903 struct v4l2_fmtdesc *f)
905 struct soc_camera_device *icd = file->private_data;
906 const struct soc_mbus_pixelfmt *format;
908 WARN_ON(priv != file->private_data);
910 if (f->index >= icd->num_user_formats)
913 format = icd->user_formats[f->index].host_fmt;
916 strlcpy(f->description, format->name, sizeof(f->description));
917 f->pixelformat = format->fourcc;
921 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
922 struct v4l2_format *f)
924 struct soc_camera_device *icd = file->private_data;
925 struct v4l2_pix_format *pix = &f->fmt.pix;
927 WARN_ON(priv != file->private_data);
929 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
932 pix->width = icd->user_width;
933 pix->height = icd->user_height;
934 pix->bytesperline = icd->bytesperline;
935 pix->sizeimage = icd->sizeimage;
936 pix->field = icd->field;
937 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
938 pix->colorspace = icd->colorspace;
939 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
940 icd->current_fmt->host_fmt->fourcc);
944 static int soc_camera_querycap(struct file *file, void *priv,
945 struct v4l2_capability *cap)
947 struct soc_camera_device *icd = file->private_data;
948 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
950 WARN_ON(priv != file->private_data);
952 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
953 return ici->ops->querycap(ici, cap);
956 static int soc_camera_streamon(struct file *file, void *priv,
957 enum v4l2_buf_type i)
959 struct soc_camera_device *icd = file->private_data;
960 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
961 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
964 WARN_ON(priv != file->private_data);
966 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
969 if (icd->streamer != file)
972 /* This calls buf_queue from host driver's videobuf_queue_ops */
973 if (ici->ops->init_videobuf)
974 ret = videobuf_streamon(&icd->vb_vidq);
976 ret = vb2_streamon(&icd->vb2_vidq, i);
979 v4l2_subdev_call(sd, video, s_stream, 1);
984 static int soc_camera_streamoff(struct file *file, void *priv,
985 enum v4l2_buf_type i)
987 struct soc_camera_device *icd = file->private_data;
988 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
989 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
991 WARN_ON(priv != file->private_data);
993 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
996 if (icd->streamer != file)
1000 * This calls buf_release from host driver's videobuf_queue_ops for all
1001 * remaining buffers. When the last buffer is freed, stop capture
1003 if (ici->ops->init_videobuf)
1004 videobuf_streamoff(&icd->vb_vidq);
1006 vb2_streamoff(&icd->vb2_vidq, i);
1008 v4l2_subdev_call(sd, video, s_stream, 0);
1013 static int soc_camera_cropcap(struct file *file, void *fh,
1014 struct v4l2_cropcap *a)
1016 struct soc_camera_device *icd = file->private_data;
1017 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1019 return ici->ops->cropcap(icd, a);
1022 static int soc_camera_g_crop(struct file *file, void *fh,
1023 struct v4l2_crop *a)
1025 struct soc_camera_device *icd = file->private_data;
1026 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1029 ret = ici->ops->get_crop(icd, a);
1035 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
1036 * argument with the actual geometry, instead, the user shall use G_CROP to
1039 static int soc_camera_s_crop(struct file *file, void *fh,
1040 const struct v4l2_crop *a)
1042 struct soc_camera_device *icd = file->private_data;
1043 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1044 const struct v4l2_rect *rect = &a->c;
1045 struct v4l2_crop current_crop;
1048 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1051 dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
1052 rect->width, rect->height, rect->left, rect->top);
1054 current_crop.type = a->type;
1056 /* If get_crop fails, we'll let host and / or client drivers decide */
1057 ret = ici->ops->get_crop(icd, ¤t_crop);
1059 /* Prohibit window size change with initialised buffers */
1062 "S_CROP denied: getting current crop failed\n");
1063 } else if ((a->c.width == current_crop.c.width &&
1064 a->c.height == current_crop.c.height) ||
1065 !is_streaming(ici, icd)) {
1066 /* same size or not streaming - use .set_crop() */
1067 ret = ici->ops->set_crop(icd, a);
1068 } else if (ici->ops->set_livecrop) {
1069 ret = ici->ops->set_livecrop(icd, a);
1072 "S_CROP denied: queue initialised and sizes differ\n");
1079 static int soc_camera_g_selection(struct file *file, void *fh,
1080 struct v4l2_selection *s)
1082 struct soc_camera_device *icd = file->private_data;
1083 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1085 /* With a wrong type no need to try to fall back to cropping */
1086 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1089 if (!ici->ops->get_selection)
1092 return ici->ops->get_selection(icd, s);
1095 static int soc_camera_s_selection(struct file *file, void *fh,
1096 struct v4l2_selection *s)
1098 struct soc_camera_device *icd = file->private_data;
1099 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1102 /* In all these cases cropping emulation will not help */
1103 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1104 (s->target != V4L2_SEL_TGT_COMPOSE &&
1105 s->target != V4L2_SEL_TGT_CROP))
1108 if (s->target == V4L2_SEL_TGT_COMPOSE) {
1109 /* No output size change during a running capture! */
1110 if (is_streaming(ici, icd) &&
1111 (icd->user_width != s->r.width ||
1112 icd->user_height != s->r.height))
1116 * Only one user is allowed to change the output format, touch
1117 * buffers, start / stop streaming, poll for data
1119 if (icd->streamer && icd->streamer != file)
1123 if (!ici->ops->set_selection)
1126 ret = ici->ops->set_selection(icd, s);
1128 s->target == V4L2_SEL_TGT_COMPOSE) {
1129 icd->user_width = s->r.width;
1130 icd->user_height = s->r.height;
1132 icd->streamer = file;
1138 static int soc_camera_g_parm(struct file *file, void *fh,
1139 struct v4l2_streamparm *a)
1141 struct soc_camera_device *icd = file->private_data;
1142 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1144 if (ici->ops->get_parm)
1145 return ici->ops->get_parm(icd, a);
1147 return -ENOIOCTLCMD;
1150 static int soc_camera_s_parm(struct file *file, void *fh,
1151 struct v4l2_streamparm *a)
1153 struct soc_camera_device *icd = file->private_data;
1154 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1156 if (ici->ops->set_parm)
1157 return ici->ops->set_parm(icd, a);
1159 return -ENOIOCTLCMD;
1162 static int soc_camera_probe(struct soc_camera_host *ici,
1163 struct soc_camera_device *icd);
1165 /* So far this function cannot fail */
1166 static void scan_add_host(struct soc_camera_host *ici)
1168 struct soc_camera_device *icd;
1170 mutex_lock(&list_lock);
1172 list_for_each_entry(icd, &devices, list)
1173 if (icd->iface == ici->nr) {
1174 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1175 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
1177 /* The camera could have been already on, try to reset */
1180 ssdd->reset(icd->control);
1182 icd->parent = ici->v4l2_dev.dev;
1185 soc_camera_probe(ici, icd);
1188 mutex_unlock(&list_lock);
1192 * It is invalid to call v4l2_clk_enable() after a successful probing
1193 * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
1195 static int soc_camera_clk_enable(struct v4l2_clk *clk)
1197 struct soc_camera_device *icd = clk->priv;
1198 struct soc_camera_host *ici;
1200 if (!icd || !icd->parent)
1203 ici = to_soc_camera_host(icd->parent);
1205 if (!try_module_get(ici->ops->owner))
1209 * If a different client is currently being probed, the host will tell
1212 return soc_camera_clock_start(ici);
1215 static void soc_camera_clk_disable(struct v4l2_clk *clk)
1217 struct soc_camera_device *icd = clk->priv;
1218 struct soc_camera_host *ici;
1220 if (!icd || !icd->parent)
1223 ici = to_soc_camera_host(icd->parent);
1225 soc_camera_clock_stop(ici);
1227 module_put(ici->ops->owner);
1231 * Eventually, it would be more logical to make the respective host the clock
1232 * owner, but then we would have to copy this struct for each ici. Besides, it
1233 * would introduce the circular dependency problem, unless we port all client
1234 * drivers to release the clock, when not in use.
1236 static const struct v4l2_clk_ops soc_camera_clk_ops = {
1237 .owner = THIS_MODULE,
1238 .enable = soc_camera_clk_enable,
1239 .disable = soc_camera_clk_disable,
1242 static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
1243 struct soc_camera_async_client *sasc)
1245 struct platform_device *pdev;
1248 mutex_lock(&list_lock);
1249 i = find_first_zero_bit(device_map, MAP_MAX_NUM);
1250 if (i < MAP_MAX_NUM)
1251 set_bit(i, device_map);
1252 mutex_unlock(&list_lock);
1253 if (i >= MAP_MAX_NUM)
1256 pdev = platform_device_alloc("soc-camera-pdrv", i);
1260 ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
1262 platform_device_put(pdev);
1271 static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
1273 struct platform_device *pdev = sasc->pdev;
1276 ret = platform_device_add(pdev);
1277 if (ret < 0 || !pdev->dev.driver)
1280 return platform_get_drvdata(pdev);
1283 /* Locking: called with .host_lock held */
1284 static int soc_camera_probe_finish(struct soc_camera_device *icd)
1286 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1287 struct v4l2_mbus_framefmt mf;
1290 sd->grp_id = soc_camera_grp_id(icd);
1291 v4l2_set_subdev_hostdata(sd, icd);
1293 v4l2_subdev_call(sd, video, g_tvnorms, &icd->vdev->tvnorms);
1295 ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
1299 ret = soc_camera_add_device(icd);
1301 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
1305 /* At this point client .probe() should have run already */
1306 ret = soc_camera_init_user_formats(icd);
1310 icd->field = V4L2_FIELD_ANY;
1312 ret = soc_camera_video_start(icd);
1316 /* Try to improve our guess of a reasonable window format */
1317 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1318 icd->user_width = mf.width;
1319 icd->user_height = mf.height;
1320 icd->colorspace = mf.colorspace;
1321 icd->field = mf.field;
1323 soc_camera_remove_device(icd);
1328 soc_camera_free_user_formats(icd);
1330 soc_camera_remove_device(icd);
1335 #ifdef CONFIG_I2C_BOARDINFO
1336 static int soc_camera_i2c_init(struct soc_camera_device *icd,
1337 struct soc_camera_desc *sdesc)
1339 struct soc_camera_subdev_desc *ssdd;
1340 struct i2c_client *client;
1341 struct soc_camera_host *ici;
1342 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1343 struct i2c_adapter *adap;
1344 struct v4l2_subdev *subdev;
1345 char clk_name[V4L2_SUBDEV_NAME_SIZE];
1348 /* First find out how we link the main client */
1350 /* Async non-OF probing handled by the subdevice list */
1351 return -EPROBE_DEFER;
1354 ici = to_soc_camera_host(icd->parent);
1355 adap = i2c_get_adapter(shd->i2c_adapter_id);
1357 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
1358 shd->i2c_adapter_id);
1362 ssdd = kmemdup(&sdesc->subdev_desc, sizeof(*ssdd), GFP_KERNEL);
1368 * In synchronous case we request regulators ourselves in
1369 * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
1370 * to allocate them again.
1372 ssdd->sd_pdata.num_regulators = 0;
1373 ssdd->sd_pdata.regulators = NULL;
1374 shd->board_info->platform_data = ssdd;
1376 snprintf(clk_name, sizeof(clk_name), "%d-%04x",
1377 shd->i2c_adapter_id, shd->board_info->addr);
1379 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1380 if (IS_ERR(icd->clk)) {
1381 ret = PTR_ERR(icd->clk);
1385 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1386 shd->board_info, NULL);
1392 client = v4l2_get_subdevdata(subdev);
1394 /* Use to_i2c_client(dev) to recover the i2c client */
1395 icd->control = &client->dev;
1399 v4l2_clk_unregister(icd->clk);
1404 i2c_put_adapter(adap);
1408 static void soc_camera_i2c_free(struct soc_camera_device *icd)
1410 struct i2c_client *client =
1411 to_i2c_client(to_soc_camera_control(icd));
1412 struct i2c_adapter *adap;
1413 struct soc_camera_subdev_desc *ssdd;
1415 icd->control = NULL;
1419 adap = client->adapter;
1420 ssdd = client->dev.platform_data;
1421 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1422 i2c_unregister_device(client);
1423 i2c_put_adapter(adap);
1425 v4l2_clk_unregister(icd->clk);
1430 * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
1431 * internal global mutex, therefore cannot race against other asynchronous
1432 * events. Until notifier->complete() (soc_camera_async_complete()) is called,
1433 * the video device node is not registered and no V4L fops can occur. Unloading
1434 * of the host driver also calls a v4l2-async function, so also there we're
1437 static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
1438 struct v4l2_subdev *sd,
1439 struct v4l2_async_subdev *asd)
1441 struct soc_camera_async_client *sasc = container_of(notifier,
1442 struct soc_camera_async_client, notifier);
1443 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1445 if (asd == sasc->sensor && !WARN_ON(icd->control)) {
1446 struct i2c_client *client = v4l2_get_subdevdata(sd);
1449 * Only now we get subdevice-specific information like
1450 * regulators, flags, callbacks, etc.
1453 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1454 struct soc_camera_subdev_desc *ssdd =
1455 soc_camera_i2c_to_desc(client);
1457 memcpy(&sdesc->subdev_desc, ssdd,
1458 sizeof(sdesc->subdev_desc));
1460 ssdd->reset(&client->dev);
1463 icd->control = &client->dev;
1470 static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
1471 struct v4l2_subdev *sd,
1472 struct v4l2_async_subdev *asd)
1474 struct soc_camera_async_client *sasc = container_of(notifier,
1475 struct soc_camera_async_client, notifier);
1476 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1479 v4l2_clk_unregister(icd->clk);
1484 static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
1486 struct soc_camera_async_client *sasc = container_of(notifier,
1487 struct soc_camera_async_client, notifier);
1488 struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
1490 if (to_soc_camera_control(icd)) {
1491 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1494 mutex_lock(&list_lock);
1495 ret = soc_camera_probe(ici, icd);
1496 mutex_unlock(&list_lock);
1504 static int scan_async_group(struct soc_camera_host *ici,
1505 struct v4l2_async_subdev **asd, unsigned int size)
1507 struct soc_camera_async_subdev *sasd;
1508 struct soc_camera_async_client *sasc;
1509 struct soc_camera_device *icd;
1510 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1511 char clk_name[V4L2_SUBDEV_NAME_SIZE];
1515 /* First look for a sensor */
1516 for (i = 0; i < size; i++) {
1517 sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
1518 if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
1522 if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
1524 dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
1528 /* Or shall this be managed by the soc-camera device? */
1529 sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
1533 /* HACK: just need a != NULL */
1534 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1536 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1540 sasc->sensor = &sasd->asd;
1542 icd = soc_camera_add_pdev(sasc);
1548 sasc->notifier.subdevs = asd;
1549 sasc->notifier.num_subdevs = size;
1550 sasc->notifier.bound = soc_camera_async_bound;
1551 sasc->notifier.unbind = soc_camera_async_unbind;
1552 sasc->notifier.complete = soc_camera_async_complete;
1555 icd->parent = ici->v4l2_dev.dev;
1557 snprintf(clk_name, sizeof(clk_name), "%d-%04x",
1558 sasd->asd.match.i2c.adapter_id, sasd->asd.match.i2c.address);
1560 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1561 if (IS_ERR(icd->clk)) {
1562 ret = PTR_ERR(icd->clk);
1566 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1570 v4l2_clk_unregister(icd->clk);
1573 platform_device_del(sasc->pdev);
1575 platform_device_put(sasc->pdev);
1577 devm_kfree(ici->v4l2_dev.dev, sasc);
1578 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1583 static void scan_async_host(struct soc_camera_host *ici)
1585 struct v4l2_async_subdev **asd;
1588 for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
1589 scan_async_group(ici, asd, ici->asd_sizes[j]);
1590 asd += ici->asd_sizes[j];
1594 #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
1595 #define soc_camera_i2c_free(icd) do {} while (0)
1596 #define scan_async_host(ici) do {} while (0)
1601 struct soc_of_info {
1602 struct soc_camera_async_subdev sasd;
1603 struct soc_camera_async_client sasc;
1604 struct v4l2_async_subdev *subdev;
1607 static int soc_of_bind(struct soc_camera_host *ici,
1608 struct device_node *ep,
1609 struct device_node *remote)
1611 struct soc_camera_device *icd;
1612 struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
1613 struct soc_camera_async_client *sasc;
1614 struct soc_of_info *info;
1615 struct i2c_client *client;
1616 char clk_name[V4L2_SUBDEV_NAME_SIZE];
1619 /* allocate a new subdev and add match info to it */
1620 info = devm_kzalloc(ici->v4l2_dev.dev, sizeof(struct soc_of_info),
1625 info->sasd.asd.match.of.node = remote;
1626 info->sasd.asd.match_type = V4L2_ASYNC_MATCH_OF;
1627 info->subdev = &info->sasd.asd;
1629 /* Or shall this be managed by the soc-camera device? */
1632 /* HACK: just need a != NULL */
1633 sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
1635 ret = soc_camera_dyn_pdev(&sdesc, sasc);
1639 sasc->sensor = &info->sasd.asd;
1641 icd = soc_camera_add_pdev(sasc);
1647 sasc->notifier.subdevs = &info->subdev;
1648 sasc->notifier.num_subdevs = 1;
1649 sasc->notifier.bound = soc_camera_async_bound;
1650 sasc->notifier.unbind = soc_camera_async_unbind;
1651 sasc->notifier.complete = soc_camera_async_complete;
1654 icd->parent = ici->v4l2_dev.dev;
1656 client = of_find_i2c_device_by_node(remote);
1659 snprintf(clk_name, sizeof(clk_name), "%d-%04x",
1660 client->adapter->nr, client->addr);
1662 snprintf(clk_name, sizeof(clk_name), "of-%s",
1663 of_node_full_name(remote));
1665 icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
1666 if (IS_ERR(icd->clk)) {
1667 ret = PTR_ERR(icd->clk);
1671 ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
1675 v4l2_clk_unregister(icd->clk);
1678 platform_device_del(sasc->pdev);
1680 platform_device_put(sasc->pdev);
1682 devm_kfree(ici->v4l2_dev.dev, info);
1683 dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
1688 static void scan_of_host(struct soc_camera_host *ici)
1690 struct device *dev = ici->v4l2_dev.dev;
1691 struct device_node *np = dev->of_node;
1692 struct device_node *epn = NULL, *ren;
1695 for (i = 0; ; i++) {
1696 epn = of_graph_get_next_endpoint(np, epn);
1700 ren = of_graph_get_remote_port(epn);
1702 dev_notice(dev, "no remote for %s\n",
1703 of_node_full_name(epn));
1707 /* so we now have a remote node to connect */
1709 soc_of_bind(ici, epn, ren->parent);
1715 dev_err(dev, "multiple subdevices aren't supported yet!\n");
1722 static inline void scan_of_host(struct soc_camera_host *ici) { }
1725 /* Called during host-driver probe */
1726 static int soc_camera_probe(struct soc_camera_host *ici,
1727 struct soc_camera_device *icd)
1729 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1730 struct soc_camera_host_desc *shd = &sdesc->host_desc;
1731 struct device *control = NULL;
1734 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1737 * Currently the subdev with the largest number of controls (13) is
1738 * ov6550. So let's pick 16 as a hint for the control handler. Note
1739 * that this is a hint only: too large and you waste some memory, too
1740 * small and there is a (very) small performance hit when looking up
1741 * controls in the internal hash.
1743 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1747 /* Must have icd->vdev before registering the device */
1748 ret = video_dev_create(icd);
1753 * ..._video_start() will create a device node, video_register_device()
1754 * itself is protected against concurrent open() calls, but we also have
1755 * to protect our data also during client probing.
1758 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1759 if (shd->board_info) {
1760 ret = soc_camera_i2c_init(icd, sdesc);
1761 if (ret < 0 && ret != -EPROBE_DEFER)
1763 } else if (!shd->add_device || !shd->del_device) {
1767 ret = soc_camera_clock_start(ici);
1771 if (shd->module_name)
1772 ret = request_module(shd->module_name);
1774 ret = shd->add_device(icd);
1779 * FIXME: this is racy, have to use driver-binding notification,
1780 * when it is available
1782 control = to_soc_camera_control(icd);
1783 if (!control || !control->driver || !dev_get_drvdata(control) ||
1784 !try_module_get(control->driver->owner)) {
1785 shd->del_device(icd);
1791 mutex_lock(&ici->host_lock);
1792 ret = soc_camera_probe_finish(icd);
1793 mutex_unlock(&ici->host_lock);
1800 if (shd->board_info) {
1801 soc_camera_i2c_free(icd);
1803 shd->del_device(icd);
1804 module_put(control->driver->owner);
1807 soc_camera_clock_stop(ici);
1811 video_device_release(icd->vdev);
1815 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1820 * This is called on device_unregister, which only means we have to disconnect
1821 * from the host, but not remove ourselves from the device list. With
1822 * asynchronous client probing this can also be called without
1823 * soc_camera_probe_finish() having run. Careful with clean up.
1825 static int soc_camera_remove(struct soc_camera_device *icd)
1827 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
1828 struct video_device *vdev = icd->vdev;
1830 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1832 video_unregister_device(vdev);
1836 if (sdesc->host_desc.board_info) {
1837 soc_camera_i2c_free(icd);
1839 struct device *dev = to_soc_camera_control(icd);
1840 struct device_driver *drv = dev ? dev->driver : NULL;
1842 sdesc->host_desc.del_device(icd);
1843 module_put(drv->owner);
1847 if (icd->num_user_formats)
1848 soc_camera_free_user_formats(icd);
1851 /* For the synchronous case */
1852 v4l2_clk_unregister(icd->clk);
1857 platform_device_unregister(icd->sasc->pdev);
1862 static int default_cropcap(struct soc_camera_device *icd,
1863 struct v4l2_cropcap *a)
1865 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1866 return v4l2_subdev_call(sd, video, cropcap, a);
1869 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1871 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1872 return v4l2_subdev_call(sd, video, g_crop, a);
1875 static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
1877 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1878 return v4l2_subdev_call(sd, video, s_crop, a);
1881 static int default_g_parm(struct soc_camera_device *icd,
1882 struct v4l2_streamparm *parm)
1884 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1885 return v4l2_subdev_call(sd, video, g_parm, parm);
1888 static int default_s_parm(struct soc_camera_device *icd,
1889 struct v4l2_streamparm *parm)
1891 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1892 return v4l2_subdev_call(sd, video, s_parm, parm);
1895 static int default_enum_framesizes(struct soc_camera_device *icd,
1896 struct v4l2_frmsizeenum *fsize)
1899 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1900 const struct soc_camera_format_xlate *xlate;
1901 struct v4l2_subdev_frame_size_enum fse = {
1902 .index = fsize->index,
1903 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1906 xlate = soc_camera_xlate_by_fourcc(icd, fsize->pixel_format);
1909 fse.code = xlate->code;
1911 ret = v4l2_subdev_call(sd, pad, enum_frame_size, NULL, &fse);
1915 if (fse.min_width == fse.max_width &&
1916 fse.min_height == fse.max_height) {
1917 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1918 fsize->discrete.width = fse.min_width;
1919 fsize->discrete.height = fse.min_height;
1922 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1923 fsize->stepwise.min_width = fse.min_width;
1924 fsize->stepwise.max_width = fse.max_width;
1925 fsize->stepwise.min_height = fse.min_height;
1926 fsize->stepwise.max_height = fse.max_height;
1927 fsize->stepwise.step_width = 1;
1928 fsize->stepwise.step_height = 1;
1932 int soc_camera_host_register(struct soc_camera_host *ici)
1934 struct soc_camera_host *ix;
1937 if (!ici || !ici->ops ||
1938 !ici->ops->try_fmt ||
1939 !ici->ops->set_fmt ||
1940 !ici->ops->set_bus_param ||
1941 !ici->ops->querycap ||
1942 ((!ici->ops->init_videobuf ||
1943 !ici->ops->reqbufs) &&
1944 !ici->ops->init_videobuf2) ||
1949 if (!ici->ops->set_crop)
1950 ici->ops->set_crop = default_s_crop;
1951 if (!ici->ops->get_crop)
1952 ici->ops->get_crop = default_g_crop;
1953 if (!ici->ops->cropcap)
1954 ici->ops->cropcap = default_cropcap;
1955 if (!ici->ops->set_parm)
1956 ici->ops->set_parm = default_s_parm;
1957 if (!ici->ops->get_parm)
1958 ici->ops->get_parm = default_g_parm;
1959 if (!ici->ops->enum_framesizes)
1960 ici->ops->enum_framesizes = default_enum_framesizes;
1962 mutex_lock(&list_lock);
1963 list_for_each_entry(ix, &hosts, list) {
1964 if (ix->nr == ici->nr) {
1970 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1974 list_add_tail(&ici->list, &hosts);
1975 mutex_unlock(&list_lock);
1977 mutex_init(&ici->host_lock);
1978 mutex_init(&ici->clk_lock);
1980 if (ici->v4l2_dev.dev->of_node)
1982 else if (ici->asd_sizes)
1984 * No OF, host with a list of subdevices. Don't try to mix
1985 * modes by initialising some groups statically and some
1988 scan_async_host(ici);
1990 /* Legacy: static platform devices from board data */
1996 mutex_unlock(&list_lock);
1999 EXPORT_SYMBOL(soc_camera_host_register);
2001 /* Unregister all clients! */
2002 void soc_camera_host_unregister(struct soc_camera_host *ici)
2004 struct soc_camera_device *icd, *tmp;
2005 struct soc_camera_async_client *sasc;
2006 LIST_HEAD(notifiers);
2008 mutex_lock(&list_lock);
2009 list_del(&ici->list);
2010 list_for_each_entry(icd, &devices, list)
2011 if (icd->iface == ici->nr && icd->sasc) {
2012 /* as long as we hold the device, sasc won't be freed */
2013 get_device(icd->pdev);
2014 list_add(&icd->sasc->list, ¬ifiers);
2016 mutex_unlock(&list_lock);
2018 list_for_each_entry(sasc, ¬ifiers, list) {
2019 /* Must call unlocked to avoid AB-BA dead-lock */
2020 v4l2_async_notifier_unregister(&sasc->notifier);
2021 put_device(&sasc->pdev->dev);
2024 mutex_lock(&list_lock);
2026 list_for_each_entry_safe(icd, tmp, &devices, list)
2027 if (icd->iface == ici->nr)
2028 soc_camera_remove(icd);
2030 mutex_unlock(&list_lock);
2032 v4l2_device_unregister(&ici->v4l2_dev);
2034 EXPORT_SYMBOL(soc_camera_host_unregister);
2036 /* Image capture device */
2037 static int soc_camera_device_register(struct soc_camera_device *icd)
2039 struct soc_camera_device *ix;
2042 mutex_lock(&list_lock);
2043 for (i = 0; i < 256 && num < 0; i++) {
2045 /* Check if this index is available on this interface */
2046 list_for_each_entry(ix, &devices, list) {
2047 if (ix->iface == icd->iface && ix->devnum == i) {
2056 * ok, we have 256 cameras on this host...
2057 * man, stay reasonable...
2059 mutex_unlock(&list_lock);
2065 icd->host_priv = NULL;
2068 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
2071 i = to_platform_device(icd->pdev)->id;
2073 /* One static (legacy) soc-camera platform device */
2075 if (i >= MAP_MAX_NUM) {
2076 mutex_unlock(&list_lock);
2079 set_bit(i, device_map);
2080 list_add_tail(&icd->list, &devices);
2081 mutex_unlock(&list_lock);
2086 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
2087 .vidioc_querycap = soc_camera_querycap,
2088 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
2089 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
2090 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
2091 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
2092 .vidioc_enum_input = soc_camera_enum_input,
2093 .vidioc_g_input = soc_camera_g_input,
2094 .vidioc_s_input = soc_camera_s_input,
2095 .vidioc_s_std = soc_camera_s_std,
2096 .vidioc_g_std = soc_camera_g_std,
2097 .vidioc_enum_framesizes = soc_camera_enum_framesizes,
2098 .vidioc_reqbufs = soc_camera_reqbufs,
2099 .vidioc_querybuf = soc_camera_querybuf,
2100 .vidioc_qbuf = soc_camera_qbuf,
2101 .vidioc_dqbuf = soc_camera_dqbuf,
2102 .vidioc_create_bufs = soc_camera_create_bufs,
2103 .vidioc_prepare_buf = soc_camera_prepare_buf,
2104 .vidioc_expbuf = soc_camera_expbuf,
2105 .vidioc_streamon = soc_camera_streamon,
2106 .vidioc_streamoff = soc_camera_streamoff,
2107 .vidioc_cropcap = soc_camera_cropcap,
2108 .vidioc_g_crop = soc_camera_g_crop,
2109 .vidioc_s_crop = soc_camera_s_crop,
2110 .vidioc_g_selection = soc_camera_g_selection,
2111 .vidioc_s_selection = soc_camera_s_selection,
2112 .vidioc_g_parm = soc_camera_g_parm,
2113 .vidioc_s_parm = soc_camera_s_parm,
2116 static int video_dev_create(struct soc_camera_device *icd)
2118 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
2119 struct video_device *vdev = video_device_alloc();
2124 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
2126 vdev->v4l2_dev = &ici->v4l2_dev;
2127 vdev->fops = &soc_camera_fops;
2128 vdev->ioctl_ops = &soc_camera_ioctl_ops;
2129 vdev->release = video_device_release;
2130 vdev->ctrl_handler = &icd->ctrl_handler;
2131 vdev->lock = &ici->host_lock;
2139 * Called from soc_camera_probe() above with .host_lock held
2141 static int soc_camera_video_start(struct soc_camera_device *icd)
2143 const struct device_type *type = icd->vdev->dev.type;
2149 video_set_drvdata(icd->vdev, icd);
2150 if (icd->vdev->tvnorms == 0) {
2151 /* disable the STD API if there are no tvnorms defined */
2152 v4l2_disable_ioctl(icd->vdev, VIDIOC_G_STD);
2153 v4l2_disable_ioctl(icd->vdev, VIDIOC_S_STD);
2154 v4l2_disable_ioctl(icd->vdev, VIDIOC_ENUMSTD);
2156 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
2158 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
2162 /* Restore device type, possibly set by the subdevice driver */
2163 icd->vdev->dev.type = type;
2168 static int soc_camera_pdrv_probe(struct platform_device *pdev)
2170 struct soc_camera_desc *sdesc = pdev->dev.platform_data;
2171 struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
2172 struct soc_camera_device *icd;
2178 icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
2183 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
2184 * regulator allocation is a dummy. They are actually requested by the
2185 * subdevice driver, using soc_camera_power_init(). Also note, that in
2186 * that case regulators are attached to the I2C device and not to the
2187 * camera platform device.
2189 ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators,
2190 ssdd->sd_pdata.regulators);
2194 icd->iface = sdesc->host_desc.bus_id;
2196 icd->pdev = &pdev->dev;
2197 platform_set_drvdata(pdev, icd);
2199 icd->user_width = DEFAULT_WIDTH;
2200 icd->user_height = DEFAULT_HEIGHT;
2202 return soc_camera_device_register(icd);
2206 * Only called on rmmod for each platform device, since they are not
2207 * hot-pluggable. Now we know, that all our users - hosts and devices have
2208 * been unloaded already
2210 static int soc_camera_pdrv_remove(struct platform_device *pdev)
2212 struct soc_camera_device *icd = platform_get_drvdata(pdev);
2223 * In synchronous mode with static platform devices this is called in a
2224 * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
2225 * no need to lock. In asynchronous case the caller -
2226 * soc_camera_host_unregister() - already holds the lock
2228 if (test_bit(i, device_map)) {
2229 clear_bit(i, device_map);
2230 list_del(&icd->list);
2236 static struct platform_driver __refdata soc_camera_pdrv = {
2237 .probe = soc_camera_pdrv_probe,
2238 .remove = soc_camera_pdrv_remove,
2240 .name = "soc-camera-pdrv",
2244 module_platform_driver(soc_camera_pdrv);
2246 MODULE_DESCRIPTION("Image capture bus driver");
2247 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2248 MODULE_LICENSE("GPL");
2249 MODULE_ALIAS("platform:soc-camera-pdrv");