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/vmalloc.h>
29 #include <media/soc_camera.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-dev.h>
33 #include <media/videobuf-core.h>
35 /* Default to VGA resolution */
36 #define DEFAULT_WIDTH 640
37 #define DEFAULT_HEIGHT 480
39 static LIST_HEAD(hosts);
40 static LIST_HEAD(devices);
41 static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
43 const struct soc_camera_data_format *soc_camera_format_by_fourcc(
44 struct soc_camera_device *icd, unsigned int fourcc)
48 for (i = 0; i < icd->num_formats; i++)
49 if (icd->formats[i].fourcc == fourcc)
50 return icd->formats + i;
53 EXPORT_SYMBOL(soc_camera_format_by_fourcc);
55 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
56 struct soc_camera_device *icd, unsigned int fourcc)
60 for (i = 0; i < icd->num_user_formats; i++)
61 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
62 return icd->user_formats + i;
65 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
68 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
69 * @icl: camera platform parameters
70 * @flags: flags to be inverted according to platform configuration
71 * @return: resulting flags
73 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
78 /* If only one of the two polarities is supported, switch to the opposite */
79 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
80 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
81 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
82 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
85 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
86 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
87 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
88 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
91 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
92 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
93 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
94 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
99 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
101 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
102 struct v4l2_format *f)
104 struct soc_camera_file *icf = file->private_data;
105 struct soc_camera_device *icd = icf->icd;
106 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
108 WARN_ON(priv != file->private_data);
110 /* limit format to hardware capabilities */
111 return ici->ops->try_fmt(icd, f);
114 static int soc_camera_enum_input(struct file *file, void *priv,
115 struct v4l2_input *inp)
117 struct soc_camera_file *icf = file->private_data;
118 struct soc_camera_device *icd = icf->icd;
124 if (icd->ops->enum_input)
125 ret = icd->ops->enum_input(icd, inp);
127 /* default is camera */
128 inp->type = V4L2_INPUT_TYPE_CAMERA;
129 inp->std = V4L2_STD_UNKNOWN;
130 strcpy(inp->name, "Camera");
136 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
143 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
151 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
153 struct soc_camera_file *icf = file->private_data;
154 struct soc_camera_device *icd = icf->icd;
155 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
157 return v4l2_subdev_call(sd, core, s_std, *a);
160 static int soc_camera_reqbufs(struct file *file, void *priv,
161 struct v4l2_requestbuffers *p)
164 struct soc_camera_file *icf = file->private_data;
165 struct soc_camera_device *icd = icf->icd;
166 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
168 WARN_ON(priv != file->private_data);
170 ret = videobuf_reqbufs(&icf->vb_vidq, p);
174 return ici->ops->reqbufs(icf, p);
177 static int soc_camera_querybuf(struct file *file, void *priv,
178 struct v4l2_buffer *p)
180 struct soc_camera_file *icf = file->private_data;
182 WARN_ON(priv != file->private_data);
184 return videobuf_querybuf(&icf->vb_vidq, p);
187 static int soc_camera_qbuf(struct file *file, void *priv,
188 struct v4l2_buffer *p)
190 struct soc_camera_file *icf = file->private_data;
192 WARN_ON(priv != file->private_data);
194 return videobuf_qbuf(&icf->vb_vidq, p);
197 static int soc_camera_dqbuf(struct file *file, void *priv,
198 struct v4l2_buffer *p)
200 struct soc_camera_file *icf = file->private_data;
202 WARN_ON(priv != file->private_data);
204 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
207 /* Always entered with .video_lock held */
208 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
210 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
211 int i, fmts = 0, ret;
213 if (!ici->ops->get_formats)
215 * Fallback mode - the host will have to serve all
216 * sensor-provided formats one-to-one to the user
218 fmts = icd->num_formats;
221 * First pass - only count formats this host-sensor
222 * configuration can provide
224 for (i = 0; i < icd->num_formats; i++) {
225 ret = ici->ops->get_formats(icd, i, NULL);
235 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
236 if (!icd->user_formats)
239 icd->num_user_formats = fmts;
241 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
243 /* Second pass - actually fill data formats */
245 for (i = 0; i < icd->num_formats; i++)
246 if (!ici->ops->get_formats) {
247 icd->user_formats[i].host_fmt = icd->formats + i;
248 icd->user_formats[i].cam_fmt = icd->formats + i;
249 icd->user_formats[i].buswidth = icd->formats[i].depth;
251 ret = ici->ops->get_formats(icd, i,
252 &icd->user_formats[fmts]);
258 icd->current_fmt = icd->user_formats[0].host_fmt;
263 icd->num_user_formats = 0;
264 vfree(icd->user_formats);
268 /* Always entered with .video_lock held */
269 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
271 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
273 if (ici->ops->put_formats)
274 ici->ops->put_formats(icd);
275 icd->current_fmt = NULL;
276 icd->num_user_formats = 0;
277 vfree(icd->user_formats);
278 icd->user_formats = NULL;
281 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
284 /* Called with .vb_lock held */
285 static int soc_camera_set_fmt(struct soc_camera_file *icf,
286 struct v4l2_format *f)
288 struct soc_camera_device *icd = icf->icd;
289 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
290 struct v4l2_pix_format *pix = &f->fmt.pix;
293 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
294 pixfmtstr(pix->pixelformat), pix->width, pix->height);
296 /* We always call try_fmt() before set_fmt() or set_crop() */
297 ret = ici->ops->try_fmt(icd, f);
301 ret = ici->ops->set_fmt(icd, f);
304 } else if (!icd->current_fmt ||
305 icd->current_fmt->fourcc != pix->pixelformat) {
307 "Host driver hasn't set up current format correctly!\n");
311 icd->user_width = pix->width;
312 icd->user_height = pix->height;
314 icd->field = pix->field;
316 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
317 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
320 dev_dbg(&icd->dev, "set width: %d height: %d\n",
321 icd->user_width, icd->user_height);
323 /* set physical bus parameters */
324 return ici->ops->set_bus_param(icd, pix->pixelformat);
327 static int soc_camera_open(struct file *file)
329 struct video_device *vdev = video_devdata(file);
330 struct soc_camera_device *icd = container_of(vdev->parent,
331 struct soc_camera_device,
333 struct soc_camera_link *icl = to_soc_camera_link(icd);
334 struct soc_camera_host *ici;
335 struct soc_camera_file *icf;
339 /* No device driver attached */
342 ici = to_soc_camera_host(icd->dev.parent);
344 icf = vmalloc(sizeof(*icf));
348 if (!try_module_get(ici->ops->owner)) {
349 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
355 * Protect against icd->ops->remove() until we module_get() both
358 mutex_lock(&icd->video_lock);
363 /* Now we really have to activate the camera */
364 if (icd->use_count == 1) {
365 /* Restore parameters before the last close() per V4L2 API */
366 struct v4l2_format f = {
367 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
369 .width = icd->user_width,
370 .height = icd->user_height,
372 .pixelformat = icd->current_fmt->fourcc,
373 .colorspace = icd->current_fmt->colorspace,
376 /* ddl@rock-chips.com : accelerate device open */
377 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
379 ret = icl->power(icd->pdev, 1);
384 /* The camera could have been already on, try to reset */
386 icl->reset(icd->pdev);
389 ret = ici->ops->add(icd);
391 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
395 /* Try to configure with default parameters */
396 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
397 ret = soc_camera_set_fmt(icf, &f);
404 file->private_data = icf;
405 dev_dbg(&icd->dev, "camera device open\n");
407 ici->ops->init_videobuf(&icf->vb_vidq, icd);
409 mutex_unlock(&icd->video_lock);
414 * First five errors are entered with the .video_lock held
418 ici->ops->remove(icd);
421 icl->power(icd->pdev, 0);
424 mutex_unlock(&icd->video_lock);
425 module_put(ici->ops->owner);
431 static int soc_camera_close(struct file *file)
433 struct soc_camera_file *icf = file->private_data;
434 struct soc_camera_device *icd = icf->icd;
435 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
437 mutex_lock(&icd->video_lock);
439 if (!icd->use_count) {
440 struct soc_camera_link *icl = to_soc_camera_link(icd);
442 ici->ops->remove(icd);
443 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
445 icl->power(icd->pdev, 0);
449 mutex_unlock(&icd->video_lock);
451 module_put(ici->ops->owner);
455 dev_dbg(&icd->dev, "camera device close\n");
460 static ssize_t soc_camera_read(struct file *file, char __user *buf,
461 size_t count, loff_t *ppos)
463 struct soc_camera_file *icf = file->private_data;
464 struct soc_camera_device *icd = icf->icd;
467 dev_err(&icd->dev, "camera device read not implemented\n");
472 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
474 struct soc_camera_file *icf = file->private_data;
475 struct soc_camera_device *icd = icf->icd;
478 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
480 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
482 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
483 (unsigned long)vma->vm_start,
484 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
490 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
492 struct soc_camera_file *icf = file->private_data;
493 struct soc_camera_device *icd = icf->icd;
494 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
496 if (list_empty(&icf->vb_vidq.stream)) {
497 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
501 return ici->ops->poll(file, pt);
504 static struct v4l2_file_operations soc_camera_fops = {
505 .owner = THIS_MODULE,
506 .open = soc_camera_open,
507 .release = soc_camera_close,
508 .ioctl = video_ioctl2,
509 .read = soc_camera_read,
510 .mmap = soc_camera_mmap,
511 .poll = soc_camera_poll,
514 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
515 struct v4l2_format *f)
517 struct soc_camera_file *icf = file->private_data;
518 struct soc_camera_device *icd = icf->icd;
521 WARN_ON(priv != file->private_data);
523 mutex_lock(&icf->vb_vidq.vb_lock);
526 if (icf->vb_vidq.bufs[0]) {
527 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
533 /* ddl@rock-chips.com :
534 Judge queue initialised by Judge icf->vb_vidq.bufs[0] whether is NULL , it is error. */
537 while (icf->vb_vidq.bufs[i] && (i<VIDEO_MAX_FRAME)) {
538 if (icf->vb_vidq.bufs[i]->state != VIDEOBUF_NEEDS_INIT) {
539 dev_err(&icd->dev, "S_FMT denied: queue initialised, icf->vb_vidq.bufs[%d]->state:0x%x\n",i,icf->vb_vidq.bufs[i]->state);
548 ret = soc_camera_set_fmt(icf, f);
551 mutex_unlock(&icf->vb_vidq.vb_lock);
556 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
557 struct v4l2_fmtdesc *f)
559 struct soc_camera_file *icf = file->private_data;
560 struct soc_camera_device *icd = icf->icd;
561 const struct soc_camera_data_format *format;
563 WARN_ON(priv != file->private_data);
565 if (f->index >= icd->num_user_formats)
568 format = icd->user_formats[f->index].host_fmt;
570 strlcpy(f->description, format->name, sizeof(f->description));
571 f->pixelformat = format->fourcc;
575 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
576 struct v4l2_format *f)
578 struct soc_camera_file *icf = file->private_data;
579 struct soc_camera_device *icd = icf->icd;
580 struct v4l2_pix_format *pix = &f->fmt.pix;
582 WARN_ON(priv != file->private_data);
584 pix->width = icd->user_width;
585 pix->height = icd->user_height;
586 pix->field = icf->vb_vidq.field;
587 pix->pixelformat = icd->current_fmt->fourcc;
588 pix->bytesperline = pix->width *
589 DIV_ROUND_UP(icd->current_fmt->depth, 8);
590 pix->sizeimage = pix->height * pix->bytesperline;
591 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
592 icd->current_fmt->fourcc);
596 static int soc_camera_querycap(struct file *file, void *priv,
597 struct v4l2_capability *cap)
599 struct soc_camera_file *icf = file->private_data;
600 struct soc_camera_device *icd = icf->icd;
601 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
603 WARN_ON(priv != file->private_data);
605 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
606 return ici->ops->querycap(ici, cap);
609 static int soc_camera_streamon(struct file *file, void *priv,
610 enum v4l2_buf_type i)
612 struct soc_camera_file *icf = file->private_data;
613 struct soc_camera_device *icd = icf->icd;
614 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
615 struct soc_camera_host *ici =
616 to_soc_camera_host(icd->dev.parent);
619 WARN_ON(priv != file->private_data);
621 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
624 mutex_lock(&icd->video_lock);
626 v4l2_subdev_call(sd, video, s_stream, 1);
627 if (ici->ops->s_stream)
628 ici->ops->s_stream(icd, 1); /* ddl@rock-chips.com : Add stream control for host */
629 /* This calls buf_queue from host driver's videobuf_queue_ops */
630 ret = videobuf_streamon(&icf->vb_vidq);
632 mutex_unlock(&icd->video_lock);
637 static int soc_camera_streamoff(struct file *file, void *priv,
638 enum v4l2_buf_type i)
640 struct soc_camera_file *icf = file->private_data;
641 struct soc_camera_device *icd = icf->icd;
642 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
643 struct soc_camera_host *ici =
644 to_soc_camera_host(icd->dev.parent);
646 WARN_ON(priv != file->private_data);
648 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
651 mutex_lock(&icd->video_lock);
652 /* This calls buf_release from host driver's videobuf_queue_ops for all
653 * remaining buffers. When the last buffer is freed, stop capture */
654 videobuf_streamoff(&icf->vb_vidq);
656 v4l2_subdev_call(sd, video, s_stream, 0);
657 if (ici->ops->s_stream)
658 ici->ops->s_stream(icd, 0); /* ddl@rock-chips.com : Add stream control for host */
660 videobuf_mmap_free(&icf->vb_vidq); /* ddl@rock-chips.com : free video buf */
661 mutex_unlock(&icd->video_lock);
666 static int soc_camera_queryctrl(struct file *file, void *priv,
667 struct v4l2_queryctrl *qc)
669 struct soc_camera_file *icf = file->private_data;
670 struct soc_camera_device *icd = icf->icd;
671 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
674 WARN_ON(priv != file->private_data);
679 /* First check host controls */
680 for (i = 0; i < ici->ops->num_controls; i++)
681 if (qc->id == ici->ops->controls[i].id) {
682 memcpy(qc, &(ici->ops->controls[i]),
687 /* Then device controls */
688 for (i = 0; i < icd->ops->num_controls; i++)
689 if (qc->id == icd->ops->controls[i].id) {
690 memcpy(qc, &(icd->ops->controls[i]),
698 /* ddl@rock-chips.com : Add ioctrl -VIDIOC_QUERYMENU */
699 static int soc_camera_querymenu(struct file *file, void *priv,
700 struct v4l2_querymenu *qm)
702 struct soc_camera_file *icf = file->private_data;
703 struct soc_camera_device *icd = icf->icd;
704 struct v4l2_queryctrl qctrl;
709 if (soc_camera_queryctrl(file,priv, &qctrl) == 0) {
710 for (i = 0; i < icd->ops->num_menus; i++) {
711 if (qm->id == icd->ops->menus[i].id) {
712 for (j=0; j<=(qctrl.maximum - qctrl.minimum); j++) {
714 if (qm->index == icd->ops->menus[i].index) {
715 snprintf(qm->name, sizeof(qm->name), icd->ops->menus[i].name);
721 if ( i >= icd->ops->num_menus)
732 static int soc_camera_g_ctrl(struct file *file, void *priv,
733 struct v4l2_control *ctrl)
735 struct soc_camera_file *icf = file->private_data;
736 struct soc_camera_device *icd = icf->icd;
737 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
738 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
741 WARN_ON(priv != file->private_data);
743 if (ici->ops->get_ctrl) {
744 ret = ici->ops->get_ctrl(icd, ctrl);
745 if (ret != -ENOIOCTLCMD)
749 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
752 static int soc_camera_s_ctrl(struct file *file, void *priv,
753 struct v4l2_control *ctrl)
755 struct soc_camera_file *icf = file->private_data;
756 struct soc_camera_device *icd = icf->icd;
757 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
758 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
761 WARN_ON(priv != file->private_data);
763 if (ici->ops->set_ctrl) {
764 ret = ici->ops->set_ctrl(icd, ctrl);
765 if (ret != -ENOIOCTLCMD)
769 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
773 /* ddl@rock-chips.com : Add ioctrl -VIDIOC_XXX_ext_ctrl for soc-camera */
774 static int soc_camera_try_ext_ctrl(struct file *file, void *priv,
775 struct v4l2_ext_controls *ctrl)
777 struct soc_camera_file *icf = file->private_data;
778 struct soc_camera_device *icd = icf->icd;
779 const struct v4l2_queryctrl *qctrl;
782 WARN_ON(priv != file->private_data);
784 if (ctrl->ctrl_class != V4L2_CTRL_CLASS_CAMERA)
787 for (i=0; i<ctrl->count; i++) {
788 qctrl = soc_camera_find_qctrl(icd->ops, ctrl->controls[i].id);
792 if ((ctrl->controls[i].value < qctrl->minimum) ||(ctrl->controls[i].value > qctrl->minimum))
798 /* ddl@rock-chips.com : Add ioctrl -VIDIOC_XXX_ext_ctrl for soc-camera */
799 static int soc_camera_g_ext_ctrl(struct file *file, void *priv,
800 struct v4l2_ext_controls *ctrl)
802 struct soc_camera_file *icf = file->private_data;
803 struct soc_camera_device *icd = icf->icd;
804 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
806 WARN_ON(priv != file->private_data);
808 if (ctrl->ctrl_class != V4L2_CTRL_CLASS_CAMERA)
811 return v4l2_subdev_call(sd, core, g_ext_ctrls, ctrl);
813 /* ddl@rock-chips.com : Add ioctrl -VIDIOC_XXX_ext_ctrl for soc-camera */
814 static int soc_camera_s_ext_ctrl(struct file *file, void *priv,
815 struct v4l2_ext_controls *ctrl)
817 struct soc_camera_file *icf = file->private_data;
818 struct soc_camera_device *icd = icf->icd;
819 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
821 WARN_ON(priv != file->private_data);
823 if (ctrl->ctrl_class != V4L2_CTRL_CLASS_CAMERA)
826 return v4l2_subdev_call(sd, core, s_ext_ctrls, ctrl);
830 static int soc_camera_cropcap(struct file *file, void *fh,
831 struct v4l2_cropcap *a)
833 struct soc_camera_file *icf = file->private_data;
834 struct soc_camera_device *icd = icf->icd;
835 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
837 return ici->ops->cropcap(icd, a);
840 static int soc_camera_g_crop(struct file *file, void *fh,
843 struct soc_camera_file *icf = file->private_data;
844 struct soc_camera_device *icd = icf->icd;
845 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
848 mutex_lock(&icf->vb_vidq.vb_lock);
849 ret = ici->ops->get_crop(icd, a);
850 mutex_unlock(&icf->vb_vidq.vb_lock);
856 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
857 * argument with the actual geometry, instead, the user shall use G_CROP to
858 * retrieve it. However, we expect camera host and client drivers to update
859 * the argument, which we then use internally, but do not return to the user.
861 static int soc_camera_s_crop(struct file *file, void *fh,
864 struct soc_camera_file *icf = file->private_data;
865 struct soc_camera_device *icd = icf->icd;
866 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
867 struct v4l2_rect *rect = &a->c;
868 struct v4l2_crop current_crop;
871 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
874 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
875 rect->width, rect->height, rect->left, rect->top);
877 /* Cropping is allowed during a running capture, guard consistency */
878 mutex_lock(&icf->vb_vidq.vb_lock);
880 /* If get_crop fails, we'll let host and / or client drivers decide */
881 ret = ici->ops->get_crop(icd, ¤t_crop);
883 /* Prohibit window size change with initialised buffers */
884 if (icf->vb_vidq.bufs[0] && !ret &&
885 (a->c.width != current_crop.c.width ||
886 a->c.height != current_crop.c.height)) {
888 "S_CROP denied: queue initialised and sizes differ\n");
891 ret = ici->ops->set_crop(icd, a);
894 mutex_unlock(&icf->vb_vidq.vb_lock);
899 static int soc_camera_g_chip_ident(struct file *file, void *fh,
900 struct v4l2_dbg_chip_ident *id)
902 struct soc_camera_file *icf = file->private_data;
903 struct soc_camera_device *icd = icf->icd;
904 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
906 return v4l2_subdev_call(sd, core, g_chip_ident, id);
909 #ifdef CONFIG_VIDEO_ADV_DEBUG
910 static int soc_camera_g_register(struct file *file, void *fh,
911 struct v4l2_dbg_register *reg)
913 struct soc_camera_file *icf = file->private_data;
914 struct soc_camera_device *icd = icf->icd;
915 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
917 return v4l2_subdev_call(sd, core, g_register, reg);
920 static int soc_camera_s_register(struct file *file, void *fh,
921 struct v4l2_dbg_register *reg)
923 struct soc_camera_file *icf = file->private_data;
924 struct soc_camera_device *icd = icf->icd;
925 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
927 return v4l2_subdev_call(sd, core, s_register, reg);
931 /* So far this function cannot fail */
932 static void scan_add_host(struct soc_camera_host *ici)
934 struct soc_camera_device *icd;
936 mutex_lock(&list_lock);
938 list_for_each_entry(icd, &devices, list) {
939 if (icd->iface == ici->nr) {
941 icd->dev.parent = ici->v4l2_dev.dev;
942 dev_set_name(&icd->dev, "%u-%u", icd->iface,
944 ret = device_register(&icd->dev);
946 icd->dev.parent = NULL;
948 "Cannot register device: %d\n", ret);
953 mutex_unlock(&list_lock);
956 #ifdef CONFIG_I2C_BOARDINFO
957 static int soc_camera_init_i2c(struct soc_camera_device *icd,
958 struct soc_camera_link *icl)
960 struct i2c_client *client;
961 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
962 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
963 struct v4l2_subdev *subdev;
968 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
969 icl->i2c_adapter_id);
973 icl->board_info->platform_data = icd;
975 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
976 icl->module_name, icl->board_info, NULL);
982 client = subdev->priv;
984 /* Use to_i2c_client(dev) to recover the i2c client */
985 dev_set_drvdata(&icd->dev, &client->dev);
989 i2c_put_adapter(adap);
994 static void soc_camera_free_i2c(struct soc_camera_device *icd)
996 struct i2c_client *client =
997 to_i2c_client(to_soc_camera_control(icd));
998 dev_set_drvdata(&icd->dev, NULL);
999 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1000 i2c_unregister_device(client);
1001 i2c_put_adapter(client->adapter);
1004 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
1005 #define soc_camera_free_i2c(icd) do {} while (0)
1008 static int soc_camera_video_start(struct soc_camera_device *icd);
1009 static int video_dev_create(struct soc_camera_device *icd);
1010 /* Called during host-driver probe */
1011 static int soc_camera_probe(struct device *dev)
1013 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1014 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
1015 struct soc_camera_link *icl = to_soc_camera_link(icd);
1016 struct device *control = NULL;
1017 struct v4l2_subdev *sd;
1018 struct v4l2_format f = {.type = V4L2_BUF_TYPE_VIDEO_CAPTURE};
1021 dev_info(dev, "Probing %s\n", dev_name(dev));
1024 ret = icl->power(icd->pdev, 1);
1027 "Platform failed to power-on the camera.\n");
1032 /* The camera could have been already on, try to reset */
1034 icl->reset(icd->pdev);
1036 ret = ici->ops->add(icd);
1040 /* Must have icd->vdev before registering the device */
1041 ret = video_dev_create(icd);
1045 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1046 if (icl->board_info) {
1047 ret = soc_camera_init_i2c(icd, icl);
1050 } else if (!icl->add_device || !icl->del_device) {
1054 if (icl->module_name)
1055 ret = request_module(icl->module_name);
1057 ret = icl->add_device(icl, &icd->dev);
1062 * FIXME: this is racy, have to use driver-binding notification,
1063 * when it is available
1065 control = to_soc_camera_control(icd);
1066 if (!control || !control->driver || !dev_get_drvdata(control) ||
1067 !try_module_get(control->driver->owner)) {
1068 icl->del_device(icl);
1073 /* At this point client .probe() should have run already */
1074 ret = soc_camera_init_user_formats(icd);
1078 icd->field = V4L2_FIELD_ANY;
1080 /* ..._video_start() will create a device node, so we have to protect */
1081 mutex_lock(&icd->video_lock);
1083 ret = soc_camera_video_start(icd);
1087 /* Try to improve our guess of a reasonable window format */
1088 sd = soc_camera_to_subdev(icd);
1089 if (!v4l2_subdev_call(sd, video, g_fmt, &f)) {
1090 icd->user_width = f.fmt.pix.width;
1091 icd->user_height = f.fmt.pix.height;
1094 /* Do we have to sysfs_remove_link() before device_unregister()? */
1095 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
1097 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1099 ici->ops->remove(icd);
1102 icl->power(icd->pdev, 0);
1104 mutex_unlock(&icd->video_lock);
1109 mutex_unlock(&icd->video_lock);
1110 soc_camera_free_user_formats(icd);
1112 if (icl->board_info) {
1113 soc_camera_free_i2c(icd);
1115 icl->del_device(icl);
1116 module_put(control->driver->owner);
1120 video_device_release(icd->vdev);
1122 ici->ops->remove(icd);
1125 icl->power(icd->pdev, 0);
1130 /* This is called on device_unregister, which only means we have to disconnect
1131 * from the host, but not remove ourselves from the device list */
1132 static int soc_camera_remove(struct device *dev)
1134 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1135 struct soc_camera_link *icl = to_soc_camera_link(icd);
1136 struct video_device *vdev = icd->vdev;
1138 BUG_ON(!dev->parent);
1141 mutex_lock(&icd->video_lock);
1142 video_unregister_device(vdev);
1144 mutex_unlock(&icd->video_lock);
1147 if (icl->board_info) {
1148 soc_camera_free_i2c(icd);
1150 struct device_driver *drv = to_soc_camera_control(icd) ?
1151 to_soc_camera_control(icd)->driver : NULL;
1153 icl->del_device(icl);
1154 module_put(drv->owner);
1157 soc_camera_free_user_formats(icd);
1162 static int soc_camera_suspend(struct device *dev, pm_message_t state)
1164 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1165 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1168 if (ici->ops->suspend)
1169 ret = ici->ops->suspend(icd, state);
1174 static int soc_camera_resume(struct device *dev)
1176 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1177 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1180 if (ici->ops->resume)
1181 ret = ici->ops->resume(icd);
1186 static struct bus_type soc_camera_bus_type = {
1187 .name = "soc-camera",
1188 .probe = soc_camera_probe,
1189 .remove = soc_camera_remove,
1190 .suspend = soc_camera_suspend,
1191 .resume = soc_camera_resume,
1194 static struct device_driver ic_drv = {
1196 .bus = &soc_camera_bus_type,
1197 .owner = THIS_MODULE,
1200 static void dummy_release(struct device *dev)
1204 static int default_cropcap(struct soc_camera_device *icd,
1205 struct v4l2_cropcap *a)
1207 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1208 return v4l2_subdev_call(sd, video, cropcap, a);
1211 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1213 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1214 return v4l2_subdev_call(sd, video, g_crop, a);
1217 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1219 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1220 return v4l2_subdev_call(sd, video, s_crop, a);
1223 static void soc_camera_device_init(struct device *dev, void *pdata)
1225 dev->platform_data = pdata;
1226 dev->bus = &soc_camera_bus_type;
1227 dev->release = dummy_release;
1230 int soc_camera_host_register(struct soc_camera_host *ici)
1232 struct soc_camera_host *ix;
1235 if (!ici || !ici->ops ||
1236 !ici->ops->try_fmt ||
1237 !ici->ops->set_fmt ||
1238 !ici->ops->set_bus_param ||
1239 !ici->ops->querycap ||
1240 !ici->ops->init_videobuf ||
1241 !ici->ops->reqbufs ||
1243 !ici->ops->remove ||
1248 if (!ici->ops->set_crop)
1249 ici->ops->set_crop = default_s_crop;
1250 if (!ici->ops->get_crop)
1251 ici->ops->get_crop = default_g_crop;
1252 if (!ici->ops->cropcap)
1253 ici->ops->cropcap = default_cropcap;
1255 mutex_lock(&list_lock);
1256 list_for_each_entry(ix, &hosts, list) {
1257 if (ix->nr == ici->nr) {
1263 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1267 list_add_tail(&ici->list, &hosts);
1268 mutex_unlock(&list_lock);
1275 mutex_unlock(&list_lock);
1278 EXPORT_SYMBOL(soc_camera_host_register);
1280 /* Unregister all clients! */
1281 void soc_camera_host_unregister(struct soc_camera_host *ici)
1283 struct soc_camera_device *icd;
1285 mutex_lock(&list_lock);
1287 list_del(&ici->list);
1289 list_for_each_entry(icd, &devices, list) {
1290 if (icd->iface == ici->nr) {
1291 void *pdata = icd->dev.platform_data;
1292 /* The bus->remove will be called */
1293 device_unregister(&icd->dev);
1295 * Not before device_unregister(), .remove
1296 * needs parent to call ici->ops->remove().
1297 * If the host module is loaded again, device_register()
1298 * would complain "already initialised," since 2.6.32
1299 * this is also needed to prevent use-after-free of the
1300 * device private data.
1302 memset(&icd->dev, 0, sizeof(icd->dev));
1303 soc_camera_device_init(&icd->dev, pdata);
1307 mutex_unlock(&list_lock);
1309 v4l2_device_unregister(&ici->v4l2_dev);
1311 EXPORT_SYMBOL(soc_camera_host_unregister);
1313 /* Image capture device */
1314 static int soc_camera_device_register(struct soc_camera_device *icd)
1316 struct soc_camera_device *ix;
1319 for (i = 0; i < 256 && num < 0; i++) {
1321 /* Check if this index is available on this interface */
1322 list_for_each_entry(ix, &devices, list) {
1323 if (ix->iface == icd->iface && ix->devnum == i) {
1331 /* ok, we have 256 cameras on this host...
1332 * man, stay reasonable... */
1337 icd->host_priv = NULL;
1338 mutex_init(&icd->video_lock);
1340 list_add_tail(&icd->list, &devices);
1345 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1347 list_del(&icd->list);
1350 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1351 .vidioc_querycap = soc_camera_querycap,
1352 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1353 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1354 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1355 .vidioc_enum_input = soc_camera_enum_input,
1356 .vidioc_g_input = soc_camera_g_input,
1357 .vidioc_s_input = soc_camera_s_input,
1358 .vidioc_s_std = soc_camera_s_std,
1359 .vidioc_reqbufs = soc_camera_reqbufs,
1360 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1361 .vidioc_querybuf = soc_camera_querybuf,
1362 .vidioc_qbuf = soc_camera_qbuf,
1363 .vidioc_dqbuf = soc_camera_dqbuf,
1364 .vidioc_streamon = soc_camera_streamon,
1365 .vidioc_streamoff = soc_camera_streamoff,
1366 .vidioc_queryctrl = soc_camera_queryctrl,
1367 .vidioc_querymenu = soc_camera_querymenu, /* ddl@rock-chips.com: Add ioctrl - vidioc_querymenu for soc-camera */
1368 .vidioc_g_ctrl = soc_camera_g_ctrl,
1369 .vidioc_s_ctrl = soc_camera_s_ctrl,
1370 .vidioc_g_ext_ctrls = soc_camera_g_ext_ctrl, /* ddl@rock-chips.com: Add ioctrl - vidioc_g_ext_ctrls for soc-camera */
1371 .vidioc_s_ext_ctrls = soc_camera_s_ext_ctrl, /* ddl@rock-chips.com: Add ioctrl - vidioc_s_ext_ctrls for soc-camera */
1372 .vidioc_try_ext_ctrls = soc_camera_try_ext_ctrl, /* ddl@rock-chips.com: Add ioctrl - vidioc_try_ext_ctrls for soc-camera */
1374 .vidioc_cropcap = soc_camera_cropcap,
1375 .vidioc_g_crop = soc_camera_g_crop,
1376 .vidioc_s_crop = soc_camera_s_crop,
1377 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1378 #ifdef CONFIG_VIDEO_ADV_DEBUG
1379 .vidioc_g_register = soc_camera_g_register,
1380 .vidioc_s_register = soc_camera_s_register,
1384 static int video_dev_create(struct soc_camera_device *icd)
1386 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1387 struct video_device *vdev = video_device_alloc();
1392 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1394 vdev->parent = &icd->dev;
1395 vdev->current_norm = V4L2_STD_UNKNOWN;
1396 vdev->fops = &soc_camera_fops;
1397 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1398 vdev->release = video_device_release;
1400 vdev->tvnorms = V4L2_STD_UNKNOWN;
1408 * Called from soc_camera_probe() above (with .video_lock held???)
1410 static int soc_camera_video_start(struct soc_camera_device *icd)
1414 if (!icd->dev.parent)
1418 !icd->ops->query_bus_param ||
1419 !icd->ops->set_bus_param)
1422 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER,
1425 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1432 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1434 struct soc_camera_link *icl = pdev->dev.platform_data;
1435 struct soc_camera_device *icd;
1441 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1445 icd->iface = icl->bus_id;
1446 icd->pdev = &pdev->dev;
1447 platform_set_drvdata(pdev, icd);
1449 ret = soc_camera_device_register(icd);
1453 soc_camera_device_init(&icd->dev, icl);
1455 icd->user_width = DEFAULT_WIDTH;
1456 icd->user_height = DEFAULT_HEIGHT;
1466 /* Only called on rmmod for each platform device, since they are not
1467 * hot-pluggable. Now we know, that all our users - hosts and devices have
1468 * been unloaded already */
1469 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1471 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1476 soc_camera_device_unregister(icd);
1483 static struct platform_driver __refdata soc_camera_pdrv = {
1484 .remove = __devexit_p(soc_camera_pdrv_remove),
1486 .name = "soc-camera-pdrv",
1487 .owner = THIS_MODULE,
1491 static int __init soc_camera_init(void)
1493 int ret = bus_register(&soc_camera_bus_type);
1496 ret = driver_register(&ic_drv);
1500 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1507 driver_unregister(&ic_drv);
1509 bus_unregister(&soc_camera_bus_type);
1513 static void __exit soc_camera_exit(void)
1515 platform_driver_unregister(&soc_camera_pdrv);
1516 driver_unregister(&ic_drv);
1517 bus_unregister(&soc_camera_bus_type);
1520 module_init(soc_camera_init);
1521 module_exit(soc_camera_exit);
1523 MODULE_DESCRIPTION("Image capture bus driver");
1524 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1525 MODULE_LICENSE("GPL");
1526 MODULE_ALIAS("platform:soc-camera-pdrv");