2 * camera image capture (abstract) bus driver
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 * This driver provides an interface between platform-specific camera
7 * busses and camera devices. It should be used if the camera is
8 * connected not over a "proper" bus like PCI or USB, but over a
9 * special bus, like, for example, the Quick Capture interface on PXA270
10 * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
11 * It can handle multiple cameras and / or multiple busses, which can
12 * be used, e.g., in stereo-vision applications.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/slab.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/vmalloc.h>
32 #include <media/soc_camera.h>
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-dev.h>
36 #include <media/videobuf-core.h>
37 #include <media/videobuf2-core.h>
38 #include <media/soc_mediabus.h>
40 /* Default to VGA resolution */
41 #define DEFAULT_WIDTH 640
42 #define DEFAULT_HEIGHT 480
44 #define is_streaming(ici, icd) \
45 (((ici)->ops->init_videobuf) ? \
46 (icd)->vb_vidq.streaming : \
47 vb2_is_streaming(&(icd)->vb2_vidq))
49 static LIST_HEAD(hosts);
50 static LIST_HEAD(devices);
51 static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
53 static int soc_camera_power_on(struct soc_camera_device *icd,
54 struct soc_camera_link *icl)
56 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
57 int ret = regulator_bulk_enable(icl->num_regulators,
60 dev_err(icd->pdev, "Cannot enable regulators\n");
65 ret = icl->power(icd->pdev, 1);
68 "Platform failed to power-on the camera.\n");
73 ret = v4l2_subdev_call(sd, core, s_power, 1);
74 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
81 icl->power(icd->pdev, 0);
83 regulator_bulk_disable(icl->num_regulators,
88 static int soc_camera_power_off(struct soc_camera_device *icd,
89 struct soc_camera_link *icl)
91 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
92 int ret = v4l2_subdev_call(sd, core, s_power, 0);
94 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
98 ret = icl->power(icd->pdev, 0);
101 "Platform failed to power-off the camera.\n");
106 ret = regulator_bulk_disable(icl->num_regulators,
109 dev_err(icd->pdev, "Cannot disable regulators\n");
114 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
115 struct soc_camera_device *icd, unsigned int fourcc)
119 for (i = 0; i < icd->num_user_formats; i++)
120 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
121 return icd->user_formats + i;
124 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
127 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
128 * @icl: camera platform parameters
129 * @cfg: media bus configuration
130 * @return: resulting flags
132 unsigned long soc_camera_apply_board_flags(struct soc_camera_link *icl,
133 const struct v4l2_mbus_config *cfg)
135 unsigned long f, flags = cfg->flags;
137 /* If only one of the two polarities is supported, switch to the opposite */
138 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
139 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
140 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
141 flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
144 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
145 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
146 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
147 flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
150 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
151 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
152 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
153 flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
158 EXPORT_SYMBOL(soc_camera_apply_board_flags);
160 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
163 static int soc_camera_try_fmt(struct soc_camera_device *icd,
164 struct v4l2_format *f)
166 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
167 const struct soc_camera_format_xlate *xlate;
168 struct v4l2_pix_format *pix = &f->fmt.pix;
171 dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
172 pixfmtstr(pix->pixelformat), pix->width, pix->height);
174 if (!(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
175 pix->bytesperline = 0;
179 ret = ici->ops->try_fmt(icd, f);
183 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
187 ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
191 pix->bytesperline = max_t(u32, pix->bytesperline, ret);
193 ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
198 pix->sizeimage = max_t(u32, pix->sizeimage, ret);
203 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
204 struct v4l2_format *f)
206 struct soc_camera_device *icd = file->private_data;
208 WARN_ON(priv != file->private_data);
210 /* Only single-plane capture is supported so far */
211 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
214 /* limit format to hardware capabilities */
215 return soc_camera_try_fmt(icd, f);
218 static int soc_camera_enum_input(struct file *file, void *priv,
219 struct v4l2_input *inp)
224 /* default is camera */
225 inp->type = V4L2_INPUT_TYPE_CAMERA;
226 inp->std = V4L2_STD_UNKNOWN;
227 strcpy(inp->name, "Camera");
232 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
239 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
247 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
249 struct soc_camera_device *icd = file->private_data;
250 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
252 return v4l2_subdev_call(sd, core, s_std, *a);
255 static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
257 struct soc_camera_device *icd = file->private_data;
258 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
260 return v4l2_subdev_call(sd, core, g_std, a);
263 static int soc_camera_enum_framesizes(struct file *file, void *fh,
264 struct v4l2_frmsizeenum *fsize)
266 struct soc_camera_device *icd = file->private_data;
267 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
269 return ici->ops->enum_framesizes(icd, fsize);
272 static int soc_camera_reqbufs(struct file *file, void *priv,
273 struct v4l2_requestbuffers *p)
276 struct soc_camera_device *icd = file->private_data;
277 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
279 WARN_ON(priv != file->private_data);
281 if (icd->streamer && icd->streamer != file)
284 if (ici->ops->init_videobuf) {
285 ret = videobuf_reqbufs(&icd->vb_vidq, p);
289 ret = ici->ops->reqbufs(icd, p);
291 ret = vb2_reqbufs(&icd->vb2_vidq, p);
294 if (!ret && !icd->streamer)
295 icd->streamer = file;
300 static int soc_camera_querybuf(struct file *file, void *priv,
301 struct v4l2_buffer *p)
303 struct soc_camera_device *icd = file->private_data;
304 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
306 WARN_ON(priv != file->private_data);
308 if (ici->ops->init_videobuf)
309 return videobuf_querybuf(&icd->vb_vidq, p);
311 return vb2_querybuf(&icd->vb2_vidq, p);
314 static int soc_camera_qbuf(struct file *file, void *priv,
315 struct v4l2_buffer *p)
317 struct soc_camera_device *icd = file->private_data;
318 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
320 WARN_ON(priv != file->private_data);
322 if (icd->streamer != file)
325 if (ici->ops->init_videobuf)
326 return videobuf_qbuf(&icd->vb_vidq, p);
328 return vb2_qbuf(&icd->vb2_vidq, p);
331 static int soc_camera_dqbuf(struct file *file, void *priv,
332 struct v4l2_buffer *p)
334 struct soc_camera_device *icd = file->private_data;
335 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
337 WARN_ON(priv != file->private_data);
339 if (icd->streamer != file)
342 if (ici->ops->init_videobuf)
343 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
345 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
348 static int soc_camera_create_bufs(struct file *file, void *priv,
349 struct v4l2_create_buffers *create)
351 struct soc_camera_device *icd = file->private_data;
352 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
355 if (ici->ops->init_videobuf)
358 return vb2_create_bufs(&icd->vb2_vidq, create);
361 static int soc_camera_prepare_buf(struct file *file, void *priv,
362 struct v4l2_buffer *b)
364 struct soc_camera_device *icd = file->private_data;
365 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
368 if (ici->ops->init_videobuf)
371 return vb2_prepare_buf(&icd->vb2_vidq, b);
374 /* Always entered with .video_lock held */
375 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
377 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
378 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
379 unsigned int i, fmts = 0, raw_fmts = 0;
381 enum v4l2_mbus_pixelcode code;
383 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
386 if (!ici->ops->get_formats)
388 * Fallback mode - the host will have to serve all
389 * sensor-provided formats one-to-one to the user
394 * First pass - only count formats this host-sensor
395 * configuration can provide
397 for (i = 0; i < raw_fmts; i++) {
398 ret = ici->ops->get_formats(icd, i, NULL);
408 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
409 if (!icd->user_formats)
412 dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
414 /* Second pass - actually fill data formats */
416 for (i = 0; i < raw_fmts; i++)
417 if (!ici->ops->get_formats) {
418 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
419 icd->user_formats[fmts].host_fmt =
420 soc_mbus_get_fmtdesc(code);
421 if (icd->user_formats[fmts].host_fmt)
422 icd->user_formats[fmts++].code = code;
424 ret = ici->ops->get_formats(icd, i,
425 &icd->user_formats[fmts]);
431 icd->num_user_formats = fmts;
432 icd->current_fmt = &icd->user_formats[0];
437 vfree(icd->user_formats);
441 /* Always entered with .video_lock held */
442 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
444 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
446 if (ici->ops->put_formats)
447 ici->ops->put_formats(icd);
448 icd->current_fmt = NULL;
449 icd->num_user_formats = 0;
450 vfree(icd->user_formats);
451 icd->user_formats = NULL;
454 /* Called with .vb_lock held, or from the first open(2), see comment there */
455 static int soc_camera_set_fmt(struct soc_camera_device *icd,
456 struct v4l2_format *f)
458 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
459 struct v4l2_pix_format *pix = &f->fmt.pix;
462 dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
463 pixfmtstr(pix->pixelformat), pix->width, pix->height);
465 /* We always call try_fmt() before set_fmt() or set_crop() */
466 ret = soc_camera_try_fmt(icd, f);
470 ret = ici->ops->set_fmt(icd, f);
473 } else if (!icd->current_fmt ||
474 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
476 "Host driver hasn't set up current format correctly!\n");
480 icd->user_width = pix->width;
481 icd->user_height = pix->height;
482 icd->bytesperline = pix->bytesperline;
483 icd->sizeimage = pix->sizeimage;
484 icd->colorspace = pix->colorspace;
485 icd->field = pix->field;
486 if (ici->ops->init_videobuf)
487 icd->vb_vidq.field = pix->field;
489 dev_dbg(icd->pdev, "set width: %d height: %d\n",
490 icd->user_width, icd->user_height);
492 /* set physical bus parameters */
493 return ici->ops->set_bus_param(icd);
496 static int soc_camera_open(struct file *file)
498 struct video_device *vdev = video_devdata(file);
499 struct soc_camera_device *icd = dev_get_drvdata(vdev->parent);
500 struct soc_camera_link *icl = to_soc_camera_link(icd);
501 struct soc_camera_host *ici;
504 if (!to_soc_camera_control(icd))
505 /* No device driver attached */
508 ici = to_soc_camera_host(icd->parent);
510 if (!try_module_get(ici->ops->owner)) {
511 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
517 /* Now we really have to activate the camera */
518 if (icd->use_count == 1) {
519 /* Restore parameters before the last close() per V4L2 API */
520 struct v4l2_format f = {
521 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
523 .width = icd->user_width,
524 .height = icd->user_height,
526 .colorspace = icd->colorspace,
528 icd->current_fmt->host_fmt->fourcc,
532 /* The camera could have been already on, try to reset */
534 icl->reset(icd->pdev);
536 /* Don't mess with the host during probe */
537 mutex_lock(&ici->host_lock);
538 ret = ici->ops->add(icd);
539 mutex_unlock(&ici->host_lock);
541 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
545 ret = soc_camera_power_on(icd, icl);
549 pm_runtime_enable(&icd->vdev->dev);
550 ret = pm_runtime_resume(&icd->vdev->dev);
551 if (ret < 0 && ret != -ENOSYS)
555 * Try to configure with default parameters. Notice: this is the
556 * very first open, so, we cannot race against other calls,
557 * apart from someone else calling open() simultaneously, but
558 * .video_lock is protecting us against it.
560 ret = soc_camera_set_fmt(icd, &f);
564 if (ici->ops->init_videobuf) {
565 ici->ops->init_videobuf(&icd->vb_vidq, icd);
567 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
571 v4l2_ctrl_handler_setup(&icd->ctrl_handler);
574 file->private_data = icd;
575 dev_dbg(icd->pdev, "camera device open\n");
580 * First four errors are entered with the .video_lock held
585 pm_runtime_disable(&icd->vdev->dev);
587 soc_camera_power_off(icd, icl);
589 ici->ops->remove(icd);
592 module_put(ici->ops->owner);
597 static int soc_camera_close(struct file *file)
599 struct soc_camera_device *icd = file->private_data;
600 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
603 if (!icd->use_count) {
604 struct soc_camera_link *icl = to_soc_camera_link(icd);
606 pm_runtime_suspend(&icd->vdev->dev);
607 pm_runtime_disable(&icd->vdev->dev);
609 if (ici->ops->init_videobuf2)
610 vb2_queue_release(&icd->vb2_vidq);
611 ici->ops->remove(icd);
613 soc_camera_power_off(icd, icl);
616 if (icd->streamer == file)
617 icd->streamer = NULL;
619 module_put(ici->ops->owner);
621 dev_dbg(icd->pdev, "camera device close\n");
626 static ssize_t soc_camera_read(struct file *file, char __user *buf,
627 size_t count, loff_t *ppos)
629 struct soc_camera_device *icd = file->private_data;
632 dev_err(icd->pdev, "camera device read not implemented\n");
637 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
639 struct soc_camera_device *icd = file->private_data;
640 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
643 dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
645 if (icd->streamer != file)
648 if (ici->ops->init_videobuf)
649 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
651 err = vb2_mmap(&icd->vb2_vidq, vma);
653 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
654 (unsigned long)vma->vm_start,
655 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
661 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
663 struct soc_camera_device *icd = file->private_data;
664 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
666 if (icd->streamer != file)
669 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
670 dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
674 return ici->ops->poll(file, pt);
677 void soc_camera_lock(struct vb2_queue *vq)
679 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
680 mutex_lock(&icd->video_lock);
682 EXPORT_SYMBOL(soc_camera_lock);
684 void soc_camera_unlock(struct vb2_queue *vq)
686 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
687 mutex_unlock(&icd->video_lock);
689 EXPORT_SYMBOL(soc_camera_unlock);
691 static struct v4l2_file_operations soc_camera_fops = {
692 .owner = THIS_MODULE,
693 .open = soc_camera_open,
694 .release = soc_camera_close,
695 .unlocked_ioctl = video_ioctl2,
696 .read = soc_camera_read,
697 .mmap = soc_camera_mmap,
698 .poll = soc_camera_poll,
701 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
702 struct v4l2_format *f)
704 struct soc_camera_device *icd = file->private_data;
707 WARN_ON(priv != file->private_data);
709 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
710 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
714 if (icd->streamer && icd->streamer != file)
717 if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
718 dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
722 ret = soc_camera_set_fmt(icd, f);
724 if (!ret && !icd->streamer)
725 icd->streamer = file;
730 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
731 struct v4l2_fmtdesc *f)
733 struct soc_camera_device *icd = file->private_data;
734 const struct soc_mbus_pixelfmt *format;
736 WARN_ON(priv != file->private_data);
738 if (f->index >= icd->num_user_formats)
741 format = icd->user_formats[f->index].host_fmt;
744 strlcpy(f->description, format->name, sizeof(f->description));
745 f->pixelformat = format->fourcc;
749 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
750 struct v4l2_format *f)
752 struct soc_camera_device *icd = file->private_data;
753 struct v4l2_pix_format *pix = &f->fmt.pix;
755 WARN_ON(priv != file->private_data);
757 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
760 pix->width = icd->user_width;
761 pix->height = icd->user_height;
762 pix->bytesperline = icd->bytesperline;
763 pix->sizeimage = icd->sizeimage;
764 pix->field = icd->field;
765 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
766 pix->colorspace = icd->colorspace;
767 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
768 icd->current_fmt->host_fmt->fourcc);
772 static int soc_camera_querycap(struct file *file, void *priv,
773 struct v4l2_capability *cap)
775 struct soc_camera_device *icd = file->private_data;
776 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
778 WARN_ON(priv != file->private_data);
780 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
781 return ici->ops->querycap(ici, cap);
784 static int soc_camera_streamon(struct file *file, void *priv,
785 enum v4l2_buf_type i)
787 struct soc_camera_device *icd = file->private_data;
788 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
789 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
792 WARN_ON(priv != file->private_data);
794 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
797 if (icd->streamer != file)
800 /* This calls buf_queue from host driver's videobuf_queue_ops */
801 if (ici->ops->init_videobuf)
802 ret = videobuf_streamon(&icd->vb_vidq);
804 ret = vb2_streamon(&icd->vb2_vidq, i);
807 v4l2_subdev_call(sd, video, s_stream, 1);
812 static int soc_camera_streamoff(struct file *file, void *priv,
813 enum v4l2_buf_type i)
815 struct soc_camera_device *icd = file->private_data;
816 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
817 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
819 WARN_ON(priv != file->private_data);
821 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
824 if (icd->streamer != file)
828 * This calls buf_release from host driver's videobuf_queue_ops for all
829 * remaining buffers. When the last buffer is freed, stop capture
831 if (ici->ops->init_videobuf)
832 videobuf_streamoff(&icd->vb_vidq);
834 vb2_streamoff(&icd->vb2_vidq, i);
836 v4l2_subdev_call(sd, video, s_stream, 0);
841 static int soc_camera_cropcap(struct file *file, void *fh,
842 struct v4l2_cropcap *a)
844 struct soc_camera_device *icd = file->private_data;
845 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
847 return ici->ops->cropcap(icd, a);
850 static int soc_camera_g_crop(struct file *file, void *fh,
853 struct soc_camera_device *icd = file->private_data;
854 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
857 ret = ici->ops->get_crop(icd, a);
863 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
864 * argument with the actual geometry, instead, the user shall use G_CROP to
867 static int soc_camera_s_crop(struct file *file, void *fh,
870 struct soc_camera_device *icd = file->private_data;
871 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
872 struct v4l2_rect *rect = &a->c;
873 struct v4l2_crop current_crop;
876 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
879 dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
880 rect->width, rect->height, rect->left, rect->top);
882 /* If get_crop fails, we'll let host and / or client drivers decide */
883 ret = ici->ops->get_crop(icd, ¤t_crop);
885 /* Prohibit window size change with initialised buffers */
888 "S_CROP denied: getting current crop failed\n");
889 } else if ((a->c.width == current_crop.c.width &&
890 a->c.height == current_crop.c.height) ||
891 !is_streaming(ici, icd)) {
892 /* same size or not streaming - use .set_crop() */
893 ret = ici->ops->set_crop(icd, a);
894 } else if (ici->ops->set_livecrop) {
895 ret = ici->ops->set_livecrop(icd, a);
898 "S_CROP denied: queue initialised and sizes differ\n");
905 static int soc_camera_g_parm(struct file *file, void *fh,
906 struct v4l2_streamparm *a)
908 struct soc_camera_device *icd = file->private_data;
909 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
911 if (ici->ops->get_parm)
912 return ici->ops->get_parm(icd, a);
917 static int soc_camera_s_parm(struct file *file, void *fh,
918 struct v4l2_streamparm *a)
920 struct soc_camera_device *icd = file->private_data;
921 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
923 if (ici->ops->set_parm)
924 return ici->ops->set_parm(icd, a);
929 static int soc_camera_g_chip_ident(struct file *file, void *fh,
930 struct v4l2_dbg_chip_ident *id)
932 struct soc_camera_device *icd = file->private_data;
933 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
935 return v4l2_subdev_call(sd, core, g_chip_ident, id);
938 #ifdef CONFIG_VIDEO_ADV_DEBUG
939 static int soc_camera_g_register(struct file *file, void *fh,
940 struct v4l2_dbg_register *reg)
942 struct soc_camera_device *icd = file->private_data;
943 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
945 return v4l2_subdev_call(sd, core, g_register, reg);
948 static int soc_camera_s_register(struct file *file, void *fh,
949 struct v4l2_dbg_register *reg)
951 struct soc_camera_device *icd = file->private_data;
952 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
954 return v4l2_subdev_call(sd, core, s_register, reg);
958 static int soc_camera_probe(struct soc_camera_device *icd);
960 /* So far this function cannot fail */
961 static void scan_add_host(struct soc_camera_host *ici)
963 struct soc_camera_device *icd;
965 mutex_lock(&ici->host_lock);
967 list_for_each_entry(icd, &devices, list) {
968 if (icd->iface == ici->nr) {
971 icd->parent = ici->v4l2_dev.dev;
972 ret = soc_camera_probe(icd);
976 mutex_unlock(&ici->host_lock);
979 #ifdef CONFIG_I2C_BOARDINFO
980 static int soc_camera_init_i2c(struct soc_camera_device *icd,
981 struct soc_camera_link *icl)
983 struct i2c_client *client;
984 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
985 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
986 struct v4l2_subdev *subdev;
989 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
990 icl->i2c_adapter_id);
994 icl->board_info->platform_data = icl;
996 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
997 icl->board_info, NULL);
1001 client = v4l2_get_subdevdata(subdev);
1003 /* Use to_i2c_client(dev) to recover the i2c client */
1004 icd->control = &client->dev;
1008 i2c_put_adapter(adap);
1013 static void soc_camera_free_i2c(struct soc_camera_device *icd)
1015 struct i2c_client *client =
1016 to_i2c_client(to_soc_camera_control(icd));
1017 struct i2c_adapter *adap = client->adapter;
1019 icd->control = NULL;
1020 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1021 i2c_unregister_device(client);
1022 i2c_put_adapter(adap);
1025 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
1026 #define soc_camera_free_i2c(icd) do {} while (0)
1029 static int soc_camera_video_start(struct soc_camera_device *icd);
1030 static int video_dev_create(struct soc_camera_device *icd);
1031 /* Called during host-driver probe */
1032 static int soc_camera_probe(struct soc_camera_device *icd)
1034 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1035 struct soc_camera_link *icl = to_soc_camera_link(icd);
1036 struct device *control = NULL;
1037 struct v4l2_subdev *sd;
1038 struct v4l2_mbus_framefmt mf;
1041 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1044 * Currently the subdev with the largest number of controls (13) is
1045 * ov6550. So let's pick 16 as a hint for the control handler. Note
1046 * that this is a hint only: too large and you waste some memory, too
1047 * small and there is a (very) small performance hit when looking up
1048 * controls in the internal hash.
1050 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1054 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
1059 /* The camera could have been already on, try to reset */
1061 icl->reset(icd->pdev);
1063 ret = ici->ops->add(icd);
1068 * This will not yet call v4l2_subdev_core_ops::s_power(1), because the
1069 * subdevice has not been initialised yet. We'll have to call it once
1070 * again after initialisation, even though it shouldn't be needed, we
1071 * don't do any IO here.
1073 ret = soc_camera_power_on(icd, icl);
1077 /* Must have icd->vdev before registering the device */
1078 ret = video_dev_create(icd);
1082 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1083 if (icl->board_info) {
1084 ret = soc_camera_init_i2c(icd, icl);
1087 } else if (!icl->add_device || !icl->del_device) {
1091 if (icl->module_name)
1092 ret = request_module(icl->module_name);
1094 ret = icl->add_device(icd);
1099 * FIXME: this is racy, have to use driver-binding notification,
1100 * when it is available
1102 control = to_soc_camera_control(icd);
1103 if (!control || !control->driver || !dev_get_drvdata(control) ||
1104 !try_module_get(control->driver->owner)) {
1105 icl->del_device(icd);
1111 sd = soc_camera_to_subdev(icd);
1112 sd->grp_id = soc_camera_grp_id(icd);
1113 v4l2_set_subdev_hostdata(sd, icd);
1115 if (v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler))
1118 /* At this point client .probe() should have run already */
1119 ret = soc_camera_init_user_formats(icd);
1123 icd->field = V4L2_FIELD_ANY;
1126 * ..._video_start() will create a device node, video_register_device()
1127 * itself is protected against concurrent open() calls, but we also have
1128 * to protect our data.
1130 mutex_lock(&icd->video_lock);
1132 ret = soc_camera_video_start(icd);
1136 ret = v4l2_subdev_call(sd, core, s_power, 1);
1137 if (ret < 0 && ret != -ENOIOCTLCMD)
1140 /* Try to improve our guess of a reasonable window format */
1141 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1142 icd->user_width = mf.width;
1143 icd->user_height = mf.height;
1144 icd->colorspace = mf.colorspace;
1145 icd->field = mf.field;
1148 ici->ops->remove(icd);
1150 soc_camera_power_off(icd, icl);
1152 mutex_unlock(&icd->video_lock);
1157 video_unregister_device(icd->vdev);
1159 mutex_unlock(&icd->video_lock);
1160 soc_camera_free_user_formats(icd);
1163 if (icl->board_info) {
1164 soc_camera_free_i2c(icd);
1166 icl->del_device(icd);
1167 module_put(control->driver->owner);
1171 video_device_release(icd->vdev);
1174 soc_camera_power_off(icd, icl);
1176 ici->ops->remove(icd);
1178 regulator_bulk_free(icl->num_regulators, icl->regulators);
1180 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1185 * This is called on device_unregister, which only means we have to disconnect
1186 * from the host, but not remove ourselves from the device list
1188 static int soc_camera_remove(struct soc_camera_device *icd)
1190 struct soc_camera_link *icl = to_soc_camera_link(icd);
1191 struct video_device *vdev = icd->vdev;
1193 BUG_ON(!icd->parent);
1195 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1197 video_unregister_device(vdev);
1201 if (icl->board_info) {
1202 soc_camera_free_i2c(icd);
1204 struct device_driver *drv = to_soc_camera_control(icd)->driver;
1206 icl->del_device(icd);
1207 module_put(drv->owner);
1210 soc_camera_free_user_formats(icd);
1212 regulator_bulk_free(icl->num_regulators, icl->regulators);
1217 static int default_cropcap(struct soc_camera_device *icd,
1218 struct v4l2_cropcap *a)
1220 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1221 return v4l2_subdev_call(sd, video, cropcap, a);
1224 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1226 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1227 return v4l2_subdev_call(sd, video, g_crop, a);
1230 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1232 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1233 return v4l2_subdev_call(sd, video, s_crop, a);
1236 static int default_g_parm(struct soc_camera_device *icd,
1237 struct v4l2_streamparm *parm)
1239 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1240 return v4l2_subdev_call(sd, video, g_parm, parm);
1243 static int default_s_parm(struct soc_camera_device *icd,
1244 struct v4l2_streamparm *parm)
1246 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1247 return v4l2_subdev_call(sd, video, s_parm, parm);
1250 static int default_enum_framesizes(struct soc_camera_device *icd,
1251 struct v4l2_frmsizeenum *fsize)
1254 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1255 const struct soc_camera_format_xlate *xlate;
1256 __u32 pixfmt = fsize->pixel_format;
1257 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1259 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1262 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1263 fsize_mbus.pixel_format = xlate->code;
1265 ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
1269 *fsize = fsize_mbus;
1270 fsize->pixel_format = pixfmt;
1275 int soc_camera_host_register(struct soc_camera_host *ici)
1277 struct soc_camera_host *ix;
1280 if (!ici || !ici->ops ||
1281 !ici->ops->try_fmt ||
1282 !ici->ops->set_fmt ||
1283 !ici->ops->set_bus_param ||
1284 !ici->ops->querycap ||
1285 ((!ici->ops->init_videobuf ||
1286 !ici->ops->reqbufs) &&
1287 !ici->ops->init_videobuf2) ||
1289 !ici->ops->remove ||
1294 if (!ici->ops->set_crop)
1295 ici->ops->set_crop = default_s_crop;
1296 if (!ici->ops->get_crop)
1297 ici->ops->get_crop = default_g_crop;
1298 if (!ici->ops->cropcap)
1299 ici->ops->cropcap = default_cropcap;
1300 if (!ici->ops->set_parm)
1301 ici->ops->set_parm = default_s_parm;
1302 if (!ici->ops->get_parm)
1303 ici->ops->get_parm = default_g_parm;
1304 if (!ici->ops->enum_framesizes)
1305 ici->ops->enum_framesizes = default_enum_framesizes;
1307 mutex_lock(&list_lock);
1308 list_for_each_entry(ix, &hosts, list) {
1309 if (ix->nr == ici->nr) {
1315 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1319 list_add_tail(&ici->list, &hosts);
1320 mutex_unlock(&list_lock);
1322 mutex_init(&ici->host_lock);
1328 mutex_unlock(&list_lock);
1331 EXPORT_SYMBOL(soc_camera_host_register);
1333 /* Unregister all clients! */
1334 void soc_camera_host_unregister(struct soc_camera_host *ici)
1336 struct soc_camera_device *icd;
1338 mutex_lock(&list_lock);
1340 list_del(&ici->list);
1341 list_for_each_entry(icd, &devices, list)
1342 if (icd->iface == ici->nr && to_soc_camera_control(icd))
1343 soc_camera_remove(icd);
1345 mutex_unlock(&list_lock);
1347 v4l2_device_unregister(&ici->v4l2_dev);
1349 EXPORT_SYMBOL(soc_camera_host_unregister);
1351 /* Image capture device */
1352 static int soc_camera_device_register(struct soc_camera_device *icd)
1354 struct soc_camera_device *ix;
1357 for (i = 0; i < 256 && num < 0; i++) {
1359 /* Check if this index is available on this interface */
1360 list_for_each_entry(ix, &devices, list) {
1361 if (ix->iface == icd->iface && ix->devnum == i) {
1370 * ok, we have 256 cameras on this host...
1371 * man, stay reasonable...
1377 icd->host_priv = NULL;
1378 mutex_init(&icd->video_lock);
1380 list_add_tail(&icd->list, &devices);
1385 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1386 .vidioc_querycap = soc_camera_querycap,
1387 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1388 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1389 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1390 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1391 .vidioc_enum_input = soc_camera_enum_input,
1392 .vidioc_g_input = soc_camera_g_input,
1393 .vidioc_s_input = soc_camera_s_input,
1394 .vidioc_s_std = soc_camera_s_std,
1395 .vidioc_g_std = soc_camera_g_std,
1396 .vidioc_enum_framesizes = soc_camera_enum_framesizes,
1397 .vidioc_reqbufs = soc_camera_reqbufs,
1398 .vidioc_querybuf = soc_camera_querybuf,
1399 .vidioc_qbuf = soc_camera_qbuf,
1400 .vidioc_dqbuf = soc_camera_dqbuf,
1401 .vidioc_create_bufs = soc_camera_create_bufs,
1402 .vidioc_prepare_buf = soc_camera_prepare_buf,
1403 .vidioc_streamon = soc_camera_streamon,
1404 .vidioc_streamoff = soc_camera_streamoff,
1405 .vidioc_cropcap = soc_camera_cropcap,
1406 .vidioc_g_crop = soc_camera_g_crop,
1407 .vidioc_s_crop = soc_camera_s_crop,
1408 .vidioc_g_parm = soc_camera_g_parm,
1409 .vidioc_s_parm = soc_camera_s_parm,
1410 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1411 #ifdef CONFIG_VIDEO_ADV_DEBUG
1412 .vidioc_g_register = soc_camera_g_register,
1413 .vidioc_s_register = soc_camera_s_register,
1417 static int video_dev_create(struct soc_camera_device *icd)
1419 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1420 struct video_device *vdev = video_device_alloc();
1425 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1427 vdev->parent = icd->pdev;
1428 vdev->current_norm = V4L2_STD_UNKNOWN;
1429 vdev->fops = &soc_camera_fops;
1430 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1431 vdev->release = video_device_release;
1432 vdev->tvnorms = V4L2_STD_UNKNOWN;
1433 vdev->ctrl_handler = &icd->ctrl_handler;
1434 vdev->lock = &icd->video_lock;
1435 /* Locking in file operations other than ioctl should be done
1436 by the driver, not the V4L2 core.
1437 This driver needs auditing so that this flag can be removed. */
1438 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vdev->flags);
1446 * Called from soc_camera_probe() above (with .video_lock held???)
1448 static int soc_camera_video_start(struct soc_camera_device *icd)
1450 const struct device_type *type = icd->vdev->dev.type;
1456 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1458 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
1462 /* Restore device type, possibly set by the subdevice driver */
1463 icd->vdev->dev.type = type;
1468 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1470 struct soc_camera_link *icl = pdev->dev.platform_data;
1471 struct soc_camera_device *icd;
1477 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1481 icd->iface = icl->bus_id;
1483 icd->pdev = &pdev->dev;
1484 platform_set_drvdata(pdev, icd);
1486 ret = soc_camera_device_register(icd);
1490 icd->user_width = DEFAULT_WIDTH;
1491 icd->user_height = DEFAULT_HEIGHT;
1502 * Only called on rmmod for each platform device, since they are not
1503 * hot-pluggable. Now we know, that all our users - hosts and devices have
1504 * been unloaded already
1506 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1508 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1513 list_del(&icd->list);
1520 static struct platform_driver __refdata soc_camera_pdrv = {
1521 .remove = __devexit_p(soc_camera_pdrv_remove),
1523 .name = "soc-camera-pdrv",
1524 .owner = THIS_MODULE,
1528 static int __init soc_camera_init(void)
1530 return platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1533 static void __exit soc_camera_exit(void)
1535 platform_driver_unregister(&soc_camera_pdrv);
1538 module_init(soc_camera_init);
1539 module_exit(soc_camera_exit);
1541 MODULE_DESCRIPTION("Image capture bus driver");
1542 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1543 MODULE_LICENSE("GPL");
1544 MODULE_ALIAS("platform:soc-camera-pdrv");