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/slab.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/vmalloc.h>
31 #include <media/soc_camera.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-dev.h>
35 #include <media/videobuf-core.h>
36 #include <media/soc_mediabus.h>
38 /* Default to VGA resolution */
39 #define DEFAULT_WIDTH 640
40 #define DEFAULT_HEIGHT 480
42 static LIST_HEAD(hosts
);
43 static LIST_HEAD(devices
);
44 static DEFINE_MUTEX(list_lock
); /* Protects the list of hosts */
46 const struct soc_camera_format_xlate
*soc_camera_xlate_by_fourcc(
47 struct soc_camera_device
*icd
, unsigned int fourcc
)
51 for (i
= 0; i
< icd
->num_user_formats
; i
++)
52 if (icd
->user_formats
[i
].host_fmt
->fourcc
== fourcc
)
53 return icd
->user_formats
+ i
;
56 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc
);
59 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
60 * @icl: camera platform parameters
61 * @flags: flags to be inverted according to platform configuration
62 * @return: resulting flags
64 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link
*icl
,
69 /* If only one of the two polarities is supported, switch to the opposite */
70 if (icl
->flags
& SOCAM_SENSOR_INVERT_HSYNC
) {
71 f
= flags
& (SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_HSYNC_ACTIVE_LOW
);
72 if (f
== SOCAM_HSYNC_ACTIVE_HIGH
|| f
== SOCAM_HSYNC_ACTIVE_LOW
)
73 flags
^= SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_HSYNC_ACTIVE_LOW
;
76 if (icl
->flags
& SOCAM_SENSOR_INVERT_VSYNC
) {
77 f
= flags
& (SOCAM_VSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_LOW
);
78 if (f
== SOCAM_VSYNC_ACTIVE_HIGH
|| f
== SOCAM_VSYNC_ACTIVE_LOW
)
79 flags
^= SOCAM_VSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_LOW
;
82 if (icl
->flags
& SOCAM_SENSOR_INVERT_PCLK
) {
83 f
= flags
& (SOCAM_PCLK_SAMPLE_RISING
| SOCAM_PCLK_SAMPLE_FALLING
);
84 if (f
== SOCAM_PCLK_SAMPLE_RISING
|| f
== SOCAM_PCLK_SAMPLE_FALLING
)
85 flags
^= SOCAM_PCLK_SAMPLE_RISING
| SOCAM_PCLK_SAMPLE_FALLING
;
90 EXPORT_SYMBOL(soc_camera_apply_sensor_flags
);
92 static int soc_camera_try_fmt_vid_cap(struct file
*file
, void *priv
,
93 struct v4l2_format
*f
)
95 struct soc_camera_file
*icf
= file
->private_data
;
96 struct soc_camera_device
*icd
= icf
->icd
;
97 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
99 WARN_ON(priv
!= file
->private_data
);
101 /* limit format to hardware capabilities */
102 return ici
->ops
->try_fmt(icd
, f
);
105 static int soc_camera_enum_input(struct file
*file
, void *priv
,
106 struct v4l2_input
*inp
)
108 struct soc_camera_file
*icf
= file
->private_data
;
109 struct soc_camera_device
*icd
= icf
->icd
;
115 if (icd
->ops
->enum_input
)
116 ret
= icd
->ops
->enum_input(icd
, inp
);
118 /* default is camera */
119 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
120 inp
->std
= V4L2_STD_UNKNOWN
;
121 strcpy(inp
->name
, "Camera");
127 static int soc_camera_g_input(struct file
*file
, void *priv
, unsigned int *i
)
134 static int soc_camera_s_input(struct file
*file
, void *priv
, unsigned int i
)
142 static int soc_camera_s_std(struct file
*file
, void *priv
, v4l2_std_id
*a
)
144 struct soc_camera_file
*icf
= file
->private_data
;
145 struct soc_camera_device
*icd
= icf
->icd
;
146 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
148 return v4l2_subdev_call(sd
, core
, s_std
, *a
);
151 static int soc_camera_reqbufs(struct file
*file
, void *priv
,
152 struct v4l2_requestbuffers
*p
)
155 struct soc_camera_file
*icf
= file
->private_data
;
156 struct soc_camera_device
*icd
= icf
->icd
;
157 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
159 WARN_ON(priv
!= file
->private_data
);
161 ret
= videobuf_reqbufs(&icf
->vb_vidq
, p
);
165 return ici
->ops
->reqbufs(icf
, p
);
168 static int soc_camera_querybuf(struct file
*file
, void *priv
,
169 struct v4l2_buffer
*p
)
171 struct soc_camera_file
*icf
= file
->private_data
;
173 WARN_ON(priv
!= file
->private_data
);
175 return videobuf_querybuf(&icf
->vb_vidq
, p
);
178 static int soc_camera_qbuf(struct file
*file
, void *priv
,
179 struct v4l2_buffer
*p
)
181 struct soc_camera_file
*icf
= file
->private_data
;
183 WARN_ON(priv
!= file
->private_data
);
185 return videobuf_qbuf(&icf
->vb_vidq
, p
);
188 static int soc_camera_dqbuf(struct file
*file
, void *priv
,
189 struct v4l2_buffer
*p
)
191 struct soc_camera_file
*icf
= file
->private_data
;
193 WARN_ON(priv
!= file
->private_data
);
195 return videobuf_dqbuf(&icf
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
198 /* Always entered with .video_lock held */
199 static int soc_camera_init_user_formats(struct soc_camera_device
*icd
)
201 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
202 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
203 int i
, fmts
= 0, raw_fmts
= 0, ret
;
204 enum v4l2_mbus_pixelcode code
;
206 while (!v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, raw_fmts
, &code
))
209 if (!ici
->ops
->get_formats
)
211 * Fallback mode - the host will have to serve all
212 * sensor-provided formats one-to-one to the user
217 * First pass - only count formats this host-sensor
218 * configuration can provide
220 for (i
= 0; i
< raw_fmts
; i
++) {
221 ret
= ici
->ops
->get_formats(icd
, i
, NULL
);
231 vmalloc(fmts
* sizeof(struct soc_camera_format_xlate
));
232 if (!icd
->user_formats
)
235 icd
->num_user_formats
= fmts
;
237 dev_dbg(&icd
->dev
, "Found %d supported formats.\n", fmts
);
239 /* Second pass - actually fill data formats */
241 for (i
= 0; i
< raw_fmts
; i
++)
242 if (!ici
->ops
->get_formats
) {
243 v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, i
, &code
);
244 icd
->user_formats
[i
].host_fmt
=
245 soc_mbus_get_fmtdesc(code
);
246 icd
->user_formats
[i
].code
= code
;
248 ret
= ici
->ops
->get_formats(icd
, i
,
249 &icd
->user_formats
[fmts
]);
255 icd
->current_fmt
= &icd
->user_formats
[0];
260 icd
->num_user_formats
= 0;
261 vfree(icd
->user_formats
);
265 /* Always entered with .video_lock held */
266 static void soc_camera_free_user_formats(struct soc_camera_device
*icd
)
268 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
270 if (ici
->ops
->put_formats
)
271 ici
->ops
->put_formats(icd
);
272 icd
->current_fmt
= NULL
;
273 icd
->num_user_formats
= 0;
274 vfree(icd
->user_formats
);
275 icd
->user_formats
= NULL
;
278 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
281 /* Called with .vb_lock held, or from the first open(2), see comment there */
282 static int soc_camera_set_fmt(struct soc_camera_file
*icf
,
283 struct v4l2_format
*f
)
285 struct soc_camera_device
*icd
= icf
->icd
;
286 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
287 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
290 dev_dbg(&icd
->dev
, "S_FMT(%c%c%c%c, %ux%u)\n",
291 pixfmtstr(pix
->pixelformat
), pix
->width
, pix
->height
);
293 /* We always call try_fmt() before set_fmt() or set_crop() */
294 ret
= ici
->ops
->try_fmt(icd
, f
);
298 ret
= ici
->ops
->set_fmt(icd
, f
);
301 } else if (!icd
->current_fmt
||
302 icd
->current_fmt
->host_fmt
->fourcc
!= pix
->pixelformat
) {
304 "Host driver hasn't set up current format correctly!\n");
308 icd
->user_width
= pix
->width
;
309 icd
->user_height
= pix
->height
;
310 icd
->colorspace
= pix
->colorspace
;
312 icd
->field
= pix
->field
;
314 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
315 dev_warn(&icd
->dev
, "Attention! Wrong buf-type %d\n",
318 dev_dbg(&icd
->dev
, "set width: %d height: %d\n",
319 icd
->user_width
, icd
->user_height
);
321 /* set physical bus parameters */
322 return ici
->ops
->set_bus_param(icd
, pix
->pixelformat
);
325 static int soc_camera_open(struct file
*file
)
327 struct video_device
*vdev
= video_devdata(file
);
328 struct soc_camera_device
*icd
= container_of(vdev
->parent
,
329 struct soc_camera_device
,
331 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
332 struct soc_camera_host
*ici
;
333 struct soc_camera_file
*icf
;
337 /* No device driver attached */
340 ici
= to_soc_camera_host(icd
->dev
.parent
);
342 icf
= vmalloc(sizeof(*icf
));
346 if (!try_module_get(ici
->ops
->owner
)) {
347 dev_err(&icd
->dev
, "Couldn't lock capture bus driver.\n");
353 * Protect against icd->ops->remove() until we module_get() both
356 mutex_lock(&icd
->video_lock
);
361 /* Now we really have to activate the camera */
362 if (icd
->use_count
== 1) {
363 /* Restore parameters before the last close() per V4L2 API */
364 struct v4l2_format f
= {
365 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
367 .width
= icd
->user_width
,
368 .height
= icd
->user_height
,
370 .colorspace
= icd
->colorspace
,
372 icd
->current_fmt
->host_fmt
->fourcc
,
377 ret
= icl
->power(icd
->pdev
, 1);
382 /* The camera could have been already on, try to reset */
384 icl
->reset(icd
->pdev
);
386 ret
= ici
->ops
->add(icd
);
388 dev_err(&icd
->dev
, "Couldn't activate the camera: %d\n", ret
);
392 pm_runtime_enable(&icd
->vdev
->dev
);
393 ret
= pm_runtime_resume(&icd
->vdev
->dev
);
394 if (ret
< 0 && ret
!= -ENOSYS
)
398 * Try to configure with default parameters. Notice: this is the
399 * very first open, so, we cannot race against other calls,
400 * apart from someone else calling open() simultaneously, but
401 * .video_lock is protecting us against it.
403 ret
= soc_camera_set_fmt(icf
, &f
);
408 file
->private_data
= icf
;
409 dev_dbg(&icd
->dev
, "camera device open\n");
411 ici
->ops
->init_videobuf(&icf
->vb_vidq
, icd
);
413 mutex_unlock(&icd
->video_lock
);
418 * First four errors are entered with the .video_lock held
422 pm_runtime_disable(&icd
->vdev
->dev
);
424 ici
->ops
->remove(icd
);
427 icl
->power(icd
->pdev
, 0);
430 mutex_unlock(&icd
->video_lock
);
431 module_put(ici
->ops
->owner
);
437 static int soc_camera_close(struct file
*file
)
439 struct soc_camera_file
*icf
= file
->private_data
;
440 struct soc_camera_device
*icd
= icf
->icd
;
441 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
443 mutex_lock(&icd
->video_lock
);
445 if (!icd
->use_count
) {
446 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
448 pm_runtime_suspend(&icd
->vdev
->dev
);
449 pm_runtime_disable(&icd
->vdev
->dev
);
451 ici
->ops
->remove(icd
);
454 icl
->power(icd
->pdev
, 0);
457 mutex_unlock(&icd
->video_lock
);
459 module_put(ici
->ops
->owner
);
463 dev_dbg(&icd
->dev
, "camera device close\n");
468 static ssize_t
soc_camera_read(struct file
*file
, char __user
*buf
,
469 size_t count
, loff_t
*ppos
)
471 struct soc_camera_file
*icf
= file
->private_data
;
472 struct soc_camera_device
*icd
= icf
->icd
;
475 dev_err(&icd
->dev
, "camera device read not implemented\n");
480 static int soc_camera_mmap(struct file
*file
, struct vm_area_struct
*vma
)
482 struct soc_camera_file
*icf
= file
->private_data
;
483 struct soc_camera_device
*icd
= icf
->icd
;
486 dev_dbg(&icd
->dev
, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
488 err
= videobuf_mmap_mapper(&icf
->vb_vidq
, vma
);
490 dev_dbg(&icd
->dev
, "vma start=0x%08lx, size=%ld, ret=%d\n",
491 (unsigned long)vma
->vm_start
,
492 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
498 static unsigned int soc_camera_poll(struct file
*file
, poll_table
*pt
)
500 struct soc_camera_file
*icf
= file
->private_data
;
501 struct soc_camera_device
*icd
= icf
->icd
;
502 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
504 if (list_empty(&icf
->vb_vidq
.stream
)) {
505 dev_err(&icd
->dev
, "Trying to poll with no queued buffers!\n");
509 return ici
->ops
->poll(file
, pt
);
512 static struct v4l2_file_operations soc_camera_fops
= {
513 .owner
= THIS_MODULE
,
514 .open
= soc_camera_open
,
515 .release
= soc_camera_close
,
516 .ioctl
= video_ioctl2
,
517 .read
= soc_camera_read
,
518 .mmap
= soc_camera_mmap
,
519 .poll
= soc_camera_poll
,
522 static int soc_camera_s_fmt_vid_cap(struct file
*file
, void *priv
,
523 struct v4l2_format
*f
)
525 struct soc_camera_file
*icf
= file
->private_data
;
526 struct soc_camera_device
*icd
= icf
->icd
;
529 WARN_ON(priv
!= file
->private_data
);
531 mutex_lock(&icf
->vb_vidq
.vb_lock
);
533 if (icf
->vb_vidq
.bufs
[0]) {
534 dev_err(&icd
->dev
, "S_FMT denied: queue initialised\n");
539 ret
= soc_camera_set_fmt(icf
, f
);
542 mutex_unlock(&icf
->vb_vidq
.vb_lock
);
547 static int soc_camera_enum_fmt_vid_cap(struct file
*file
, void *priv
,
548 struct v4l2_fmtdesc
*f
)
550 struct soc_camera_file
*icf
= file
->private_data
;
551 struct soc_camera_device
*icd
= icf
->icd
;
552 const struct soc_mbus_pixelfmt
*format
;
554 WARN_ON(priv
!= file
->private_data
);
556 if (f
->index
>= icd
->num_user_formats
)
559 format
= icd
->user_formats
[f
->index
].host_fmt
;
562 strlcpy(f
->description
, format
->name
, sizeof(f
->description
));
563 f
->pixelformat
= format
->fourcc
;
567 static int soc_camera_g_fmt_vid_cap(struct file
*file
, void *priv
,
568 struct v4l2_format
*f
)
570 struct soc_camera_file
*icf
= file
->private_data
;
571 struct soc_camera_device
*icd
= icf
->icd
;
572 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
574 WARN_ON(priv
!= file
->private_data
);
576 pix
->width
= icd
->user_width
;
577 pix
->height
= icd
->user_height
;
578 pix
->field
= icf
->vb_vidq
.field
;
579 pix
->pixelformat
= icd
->current_fmt
->host_fmt
->fourcc
;
580 pix
->bytesperline
= soc_mbus_bytes_per_line(pix
->width
,
581 icd
->current_fmt
->host_fmt
);
582 pix
->colorspace
= icd
->colorspace
;
583 if (pix
->bytesperline
< 0)
584 return pix
->bytesperline
;
585 pix
->sizeimage
= pix
->height
* pix
->bytesperline
;
586 dev_dbg(&icd
->dev
, "current_fmt->fourcc: 0x%08x\n",
587 icd
->current_fmt
->host_fmt
->fourcc
);
591 static int soc_camera_querycap(struct file
*file
, void *priv
,
592 struct v4l2_capability
*cap
)
594 struct soc_camera_file
*icf
= file
->private_data
;
595 struct soc_camera_device
*icd
= icf
->icd
;
596 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
598 WARN_ON(priv
!= file
->private_data
);
600 strlcpy(cap
->driver
, ici
->drv_name
, sizeof(cap
->driver
));
601 return ici
->ops
->querycap(ici
, cap
);
604 static int soc_camera_streamon(struct file
*file
, void *priv
,
605 enum v4l2_buf_type i
)
607 struct soc_camera_file
*icf
= file
->private_data
;
608 struct soc_camera_device
*icd
= icf
->icd
;
609 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
612 WARN_ON(priv
!= file
->private_data
);
614 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
617 mutex_lock(&icd
->video_lock
);
619 v4l2_subdev_call(sd
, video
, s_stream
, 1);
621 /* This calls buf_queue from host driver's videobuf_queue_ops */
622 ret
= videobuf_streamon(&icf
->vb_vidq
);
624 mutex_unlock(&icd
->video_lock
);
629 static int soc_camera_streamoff(struct file
*file
, void *priv
,
630 enum v4l2_buf_type i
)
632 struct soc_camera_file
*icf
= file
->private_data
;
633 struct soc_camera_device
*icd
= icf
->icd
;
634 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
636 WARN_ON(priv
!= file
->private_data
);
638 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
641 mutex_lock(&icd
->video_lock
);
644 * This calls buf_release from host driver's videobuf_queue_ops for all
645 * remaining buffers. When the last buffer is freed, stop capture
647 videobuf_streamoff(&icf
->vb_vidq
);
649 v4l2_subdev_call(sd
, video
, s_stream
, 0);
651 mutex_unlock(&icd
->video_lock
);
656 static int soc_camera_queryctrl(struct file
*file
, void *priv
,
657 struct v4l2_queryctrl
*qc
)
659 struct soc_camera_file
*icf
= file
->private_data
;
660 struct soc_camera_device
*icd
= icf
->icd
;
661 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
664 WARN_ON(priv
!= file
->private_data
);
669 /* First check host controls */
670 for (i
= 0; i
< ici
->ops
->num_controls
; i
++)
671 if (qc
->id
== ici
->ops
->controls
[i
].id
) {
672 memcpy(qc
, &(ici
->ops
->controls
[i
]),
677 /* Then device controls */
678 for (i
= 0; i
< icd
->ops
->num_controls
; i
++)
679 if (qc
->id
== icd
->ops
->controls
[i
].id
) {
680 memcpy(qc
, &(icd
->ops
->controls
[i
]),
688 static int soc_camera_g_ctrl(struct file
*file
, void *priv
,
689 struct v4l2_control
*ctrl
)
691 struct soc_camera_file
*icf
= file
->private_data
;
692 struct soc_camera_device
*icd
= icf
->icd
;
693 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
694 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
697 WARN_ON(priv
!= file
->private_data
);
699 if (ici
->ops
->get_ctrl
) {
700 ret
= ici
->ops
->get_ctrl(icd
, ctrl
);
701 if (ret
!= -ENOIOCTLCMD
)
705 return v4l2_subdev_call(sd
, core
, g_ctrl
, ctrl
);
708 static int soc_camera_s_ctrl(struct file
*file
, void *priv
,
709 struct v4l2_control
*ctrl
)
711 struct soc_camera_file
*icf
= file
->private_data
;
712 struct soc_camera_device
*icd
= icf
->icd
;
713 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
714 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
717 WARN_ON(priv
!= file
->private_data
);
719 if (ici
->ops
->set_ctrl
) {
720 ret
= ici
->ops
->set_ctrl(icd
, ctrl
);
721 if (ret
!= -ENOIOCTLCMD
)
725 return v4l2_subdev_call(sd
, core
, s_ctrl
, ctrl
);
728 static int soc_camera_cropcap(struct file
*file
, void *fh
,
729 struct v4l2_cropcap
*a
)
731 struct soc_camera_file
*icf
= file
->private_data
;
732 struct soc_camera_device
*icd
= icf
->icd
;
733 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
735 return ici
->ops
->cropcap(icd
, a
);
738 static int soc_camera_g_crop(struct file
*file
, void *fh
,
741 struct soc_camera_file
*icf
= file
->private_data
;
742 struct soc_camera_device
*icd
= icf
->icd
;
743 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
746 mutex_lock(&icf
->vb_vidq
.vb_lock
);
747 ret
= ici
->ops
->get_crop(icd
, a
);
748 mutex_unlock(&icf
->vb_vidq
.vb_lock
);
754 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
755 * argument with the actual geometry, instead, the user shall use G_CROP to
758 static int soc_camera_s_crop(struct file
*file
, void *fh
,
761 struct soc_camera_file
*icf
= file
->private_data
;
762 struct soc_camera_device
*icd
= icf
->icd
;
763 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
764 struct v4l2_rect
*rect
= &a
->c
;
765 struct v4l2_crop current_crop
;
768 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
771 dev_dbg(&icd
->dev
, "S_CROP(%ux%u@%u:%u)\n",
772 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
774 /* Cropping is allowed during a running capture, guard consistency */
775 mutex_lock(&icf
->vb_vidq
.vb_lock
);
777 /* If get_crop fails, we'll let host and / or client drivers decide */
778 ret
= ici
->ops
->get_crop(icd
, ¤t_crop
);
780 /* Prohibit window size change with initialised buffers */
781 if (icf
->vb_vidq
.bufs
[0] && !ret
&&
782 (a
->c
.width
!= current_crop
.c
.width
||
783 a
->c
.height
!= current_crop
.c
.height
)) {
785 "S_CROP denied: queue initialised and sizes differ\n");
788 ret
= ici
->ops
->set_crop(icd
, a
);
791 mutex_unlock(&icf
->vb_vidq
.vb_lock
);
796 static int soc_camera_g_parm(struct file
*file
, void *fh
,
797 struct v4l2_streamparm
*a
)
799 struct soc_camera_file
*icf
= file
->private_data
;
800 struct soc_camera_device
*icd
= icf
->icd
;
801 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
803 if (ici
->ops
->get_parm
)
804 return ici
->ops
->get_parm(icd
, a
);
809 static int soc_camera_s_parm(struct file
*file
, void *fh
,
810 struct v4l2_streamparm
*a
)
812 struct soc_camera_file
*icf
= file
->private_data
;
813 struct soc_camera_device
*icd
= icf
->icd
;
814 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
816 if (ici
->ops
->set_parm
)
817 return ici
->ops
->set_parm(icd
, a
);
822 static int soc_camera_g_chip_ident(struct file
*file
, void *fh
,
823 struct v4l2_dbg_chip_ident
*id
)
825 struct soc_camera_file
*icf
= file
->private_data
;
826 struct soc_camera_device
*icd
= icf
->icd
;
827 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
829 return v4l2_subdev_call(sd
, core
, g_chip_ident
, id
);
832 #ifdef CONFIG_VIDEO_ADV_DEBUG
833 static int soc_camera_g_register(struct file
*file
, void *fh
,
834 struct v4l2_dbg_register
*reg
)
836 struct soc_camera_file
*icf
= file
->private_data
;
837 struct soc_camera_device
*icd
= icf
->icd
;
838 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
840 return v4l2_subdev_call(sd
, core
, g_register
, reg
);
843 static int soc_camera_s_register(struct file
*file
, void *fh
,
844 struct v4l2_dbg_register
*reg
)
846 struct soc_camera_file
*icf
= file
->private_data
;
847 struct soc_camera_device
*icd
= icf
->icd
;
848 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
850 return v4l2_subdev_call(sd
, core
, s_register
, reg
);
854 /* So far this function cannot fail */
855 static void scan_add_host(struct soc_camera_host
*ici
)
857 struct soc_camera_device
*icd
;
859 mutex_lock(&list_lock
);
861 list_for_each_entry(icd
, &devices
, list
) {
862 if (icd
->iface
== ici
->nr
) {
864 icd
->dev
.parent
= ici
->v4l2_dev
.dev
;
865 dev_set_name(&icd
->dev
, "%u-%u", icd
->iface
,
867 ret
= device_register(&icd
->dev
);
869 icd
->dev
.parent
= NULL
;
871 "Cannot register device: %d\n", ret
);
876 mutex_unlock(&list_lock
);
879 #ifdef CONFIG_I2C_BOARDINFO
880 static int soc_camera_init_i2c(struct soc_camera_device
*icd
,
881 struct soc_camera_link
*icl
)
883 struct i2c_client
*client
;
884 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
885 struct i2c_adapter
*adap
= i2c_get_adapter(icl
->i2c_adapter_id
);
886 struct v4l2_subdev
*subdev
;
889 dev_err(&icd
->dev
, "Cannot get I2C adapter #%d. No driver?\n",
890 icl
->i2c_adapter_id
);
894 icl
->board_info
->platform_data
= icd
;
896 subdev
= v4l2_i2c_new_subdev_board(&ici
->v4l2_dev
, adap
,
897 icl
->module_name
, icl
->board_info
, NULL
);
901 client
= subdev
->priv
;
903 /* Use to_i2c_client(dev) to recover the i2c client */
904 dev_set_drvdata(&icd
->dev
, &client
->dev
);
908 i2c_put_adapter(adap
);
913 static void soc_camera_free_i2c(struct soc_camera_device
*icd
)
915 struct i2c_client
*client
=
916 to_i2c_client(to_soc_camera_control(icd
));
917 dev_set_drvdata(&icd
->dev
, NULL
);
918 v4l2_device_unregister_subdev(i2c_get_clientdata(client
));
919 i2c_unregister_device(client
);
920 i2c_put_adapter(client
->adapter
);
923 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
924 #define soc_camera_free_i2c(icd) do {} while (0)
927 static int soc_camera_video_start(struct soc_camera_device
*icd
);
928 static int video_dev_create(struct soc_camera_device
*icd
);
929 /* Called during host-driver probe */
930 static int soc_camera_probe(struct device
*dev
)
932 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
933 struct soc_camera_host
*ici
= to_soc_camera_host(dev
->parent
);
934 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
935 struct device
*control
= NULL
;
936 struct v4l2_subdev
*sd
;
937 struct v4l2_mbus_framefmt mf
;
940 dev_info(dev
, "Probing %s\n", dev_name(dev
));
943 ret
= icl
->power(icd
->pdev
, 1);
946 "Platform failed to power-on the camera.\n");
951 /* The camera could have been already on, try to reset */
953 icl
->reset(icd
->pdev
);
955 ret
= ici
->ops
->add(icd
);
959 /* Must have icd->vdev before registering the device */
960 ret
= video_dev_create(icd
);
964 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
965 if (icl
->board_info
) {
966 ret
= soc_camera_init_i2c(icd
, icl
);
969 } else if (!icl
->add_device
|| !icl
->del_device
) {
973 if (icl
->module_name
)
974 ret
= request_module(icl
->module_name
);
976 ret
= icl
->add_device(icl
, &icd
->dev
);
981 * FIXME: this is racy, have to use driver-binding notification,
982 * when it is available
984 control
= to_soc_camera_control(icd
);
985 if (!control
|| !control
->driver
|| !dev_get_drvdata(control
) ||
986 !try_module_get(control
->driver
->owner
)) {
987 icl
->del_device(icl
);
992 /* At this point client .probe() should have run already */
993 ret
= soc_camera_init_user_formats(icd
);
997 icd
->field
= V4L2_FIELD_ANY
;
999 /* ..._video_start() will create a device node, so we have to protect */
1000 mutex_lock(&icd
->video_lock
);
1002 ret
= soc_camera_video_start(icd
);
1006 /* Try to improve our guess of a reasonable window format */
1007 sd
= soc_camera_to_subdev(icd
);
1008 if (!v4l2_subdev_call(sd
, video
, g_mbus_fmt
, &mf
)) {
1009 icd
->user_width
= mf
.width
;
1010 icd
->user_height
= mf
.height
;
1011 icd
->colorspace
= mf
.colorspace
;
1012 icd
->field
= mf
.field
;
1015 /* Do we have to sysfs_remove_link() before device_unregister()? */
1016 if (sysfs_create_link(&icd
->dev
.kobj
, &to_soc_camera_control(icd
)->kobj
,
1018 dev_warn(&icd
->dev
, "Failed creating the control symlink\n");
1020 ici
->ops
->remove(icd
);
1023 icl
->power(icd
->pdev
, 0);
1025 mutex_unlock(&icd
->video_lock
);
1030 mutex_unlock(&icd
->video_lock
);
1031 soc_camera_free_user_formats(icd
);
1033 if (icl
->board_info
) {
1034 soc_camera_free_i2c(icd
);
1036 icl
->del_device(icl
);
1037 module_put(control
->driver
->owner
);
1041 video_device_release(icd
->vdev
);
1043 ici
->ops
->remove(icd
);
1046 icl
->power(icd
->pdev
, 0);
1052 * This is called on device_unregister, which only means we have to disconnect
1053 * from the host, but not remove ourselves from the device list
1055 static int soc_camera_remove(struct device
*dev
)
1057 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
1058 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
1059 struct video_device
*vdev
= icd
->vdev
;
1061 BUG_ON(!dev
->parent
);
1064 mutex_lock(&icd
->video_lock
);
1065 video_unregister_device(vdev
);
1067 mutex_unlock(&icd
->video_lock
);
1070 if (icl
->board_info
) {
1071 soc_camera_free_i2c(icd
);
1073 struct device_driver
*drv
= to_soc_camera_control(icd
) ?
1074 to_soc_camera_control(icd
)->driver
: NULL
;
1076 icl
->del_device(icl
);
1077 module_put(drv
->owner
);
1080 soc_camera_free_user_formats(icd
);
1085 static int soc_camera_suspend(struct device
*dev
, pm_message_t state
)
1087 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
1088 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1091 if (ici
->ops
->suspend
)
1092 ret
= ici
->ops
->suspend(icd
, state
);
1097 static int soc_camera_resume(struct device
*dev
)
1099 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
1100 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1103 if (ici
->ops
->resume
)
1104 ret
= ici
->ops
->resume(icd
);
1109 static struct bus_type soc_camera_bus_type
= {
1110 .name
= "soc-camera",
1111 .probe
= soc_camera_probe
,
1112 .remove
= soc_camera_remove
,
1113 .suspend
= soc_camera_suspend
,
1114 .resume
= soc_camera_resume
,
1117 static struct device_driver ic_drv
= {
1119 .bus
= &soc_camera_bus_type
,
1120 .owner
= THIS_MODULE
,
1123 static void dummy_release(struct device
*dev
)
1127 static int default_cropcap(struct soc_camera_device
*icd
,
1128 struct v4l2_cropcap
*a
)
1130 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1131 return v4l2_subdev_call(sd
, video
, cropcap
, a
);
1134 static int default_g_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*a
)
1136 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1137 return v4l2_subdev_call(sd
, video
, g_crop
, a
);
1140 static int default_s_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*a
)
1142 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1143 return v4l2_subdev_call(sd
, video
, s_crop
, a
);
1146 static void soc_camera_device_init(struct device
*dev
, void *pdata
)
1148 dev
->platform_data
= pdata
;
1149 dev
->bus
= &soc_camera_bus_type
;
1150 dev
->release
= dummy_release
;
1153 int soc_camera_host_register(struct soc_camera_host
*ici
)
1155 struct soc_camera_host
*ix
;
1158 if (!ici
|| !ici
->ops
||
1159 !ici
->ops
->try_fmt
||
1160 !ici
->ops
->set_fmt
||
1161 !ici
->ops
->set_bus_param
||
1162 !ici
->ops
->querycap
||
1163 !ici
->ops
->init_videobuf
||
1164 !ici
->ops
->reqbufs
||
1166 !ici
->ops
->remove
||
1171 if (!ici
->ops
->set_crop
)
1172 ici
->ops
->set_crop
= default_s_crop
;
1173 if (!ici
->ops
->get_crop
)
1174 ici
->ops
->get_crop
= default_g_crop
;
1175 if (!ici
->ops
->cropcap
)
1176 ici
->ops
->cropcap
= default_cropcap
;
1178 mutex_lock(&list_lock
);
1179 list_for_each_entry(ix
, &hosts
, list
) {
1180 if (ix
->nr
== ici
->nr
) {
1186 ret
= v4l2_device_register(ici
->v4l2_dev
.dev
, &ici
->v4l2_dev
);
1190 list_add_tail(&ici
->list
, &hosts
);
1191 mutex_unlock(&list_lock
);
1198 mutex_unlock(&list_lock
);
1201 EXPORT_SYMBOL(soc_camera_host_register
);
1203 /* Unregister all clients! */
1204 void soc_camera_host_unregister(struct soc_camera_host
*ici
)
1206 struct soc_camera_device
*icd
;
1208 mutex_lock(&list_lock
);
1210 list_del(&ici
->list
);
1212 list_for_each_entry(icd
, &devices
, list
) {
1213 if (icd
->iface
== ici
->nr
) {
1214 void *pdata
= icd
->dev
.platform_data
;
1215 /* The bus->remove will be called */
1216 device_unregister(&icd
->dev
);
1218 * Not before device_unregister(), .remove
1219 * needs parent to call ici->ops->remove().
1220 * If the host module is loaded again, device_register()
1221 * would complain "already initialised," since 2.6.32
1222 * this is also needed to prevent use-after-free of the
1223 * device private data.
1225 memset(&icd
->dev
, 0, sizeof(icd
->dev
));
1226 soc_camera_device_init(&icd
->dev
, pdata
);
1230 mutex_unlock(&list_lock
);
1232 v4l2_device_unregister(&ici
->v4l2_dev
);
1234 EXPORT_SYMBOL(soc_camera_host_unregister
);
1236 /* Image capture device */
1237 static int soc_camera_device_register(struct soc_camera_device
*icd
)
1239 struct soc_camera_device
*ix
;
1242 for (i
= 0; i
< 256 && num
< 0; i
++) {
1244 /* Check if this index is available on this interface */
1245 list_for_each_entry(ix
, &devices
, list
) {
1246 if (ix
->iface
== icd
->iface
&& ix
->devnum
== i
) {
1255 * ok, we have 256 cameras on this host...
1256 * man, stay reasonable...
1262 icd
->host_priv
= NULL
;
1263 mutex_init(&icd
->video_lock
);
1265 list_add_tail(&icd
->list
, &devices
);
1270 static void soc_camera_device_unregister(struct soc_camera_device
*icd
)
1272 list_del(&icd
->list
);
1275 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops
= {
1276 .vidioc_querycap
= soc_camera_querycap
,
1277 .vidioc_g_fmt_vid_cap
= soc_camera_g_fmt_vid_cap
,
1278 .vidioc_enum_fmt_vid_cap
= soc_camera_enum_fmt_vid_cap
,
1279 .vidioc_s_fmt_vid_cap
= soc_camera_s_fmt_vid_cap
,
1280 .vidioc_enum_input
= soc_camera_enum_input
,
1281 .vidioc_g_input
= soc_camera_g_input
,
1282 .vidioc_s_input
= soc_camera_s_input
,
1283 .vidioc_s_std
= soc_camera_s_std
,
1284 .vidioc_reqbufs
= soc_camera_reqbufs
,
1285 .vidioc_try_fmt_vid_cap
= soc_camera_try_fmt_vid_cap
,
1286 .vidioc_querybuf
= soc_camera_querybuf
,
1287 .vidioc_qbuf
= soc_camera_qbuf
,
1288 .vidioc_dqbuf
= soc_camera_dqbuf
,
1289 .vidioc_streamon
= soc_camera_streamon
,
1290 .vidioc_streamoff
= soc_camera_streamoff
,
1291 .vidioc_queryctrl
= soc_camera_queryctrl
,
1292 .vidioc_g_ctrl
= soc_camera_g_ctrl
,
1293 .vidioc_s_ctrl
= soc_camera_s_ctrl
,
1294 .vidioc_cropcap
= soc_camera_cropcap
,
1295 .vidioc_g_crop
= soc_camera_g_crop
,
1296 .vidioc_s_crop
= soc_camera_s_crop
,
1297 .vidioc_g_parm
= soc_camera_g_parm
,
1298 .vidioc_s_parm
= soc_camera_s_parm
,
1299 .vidioc_g_chip_ident
= soc_camera_g_chip_ident
,
1300 #ifdef CONFIG_VIDEO_ADV_DEBUG
1301 .vidioc_g_register
= soc_camera_g_register
,
1302 .vidioc_s_register
= soc_camera_s_register
,
1306 static int video_dev_create(struct soc_camera_device
*icd
)
1308 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1309 struct video_device
*vdev
= video_device_alloc();
1314 strlcpy(vdev
->name
, ici
->drv_name
, sizeof(vdev
->name
));
1316 vdev
->parent
= &icd
->dev
;
1317 vdev
->current_norm
= V4L2_STD_UNKNOWN
;
1318 vdev
->fops
= &soc_camera_fops
;
1319 vdev
->ioctl_ops
= &soc_camera_ioctl_ops
;
1320 vdev
->release
= video_device_release
;
1321 vdev
->tvnorms
= V4L2_STD_UNKNOWN
;
1329 * Called from soc_camera_probe() above (with .video_lock held???)
1331 static int soc_camera_video_start(struct soc_camera_device
*icd
)
1333 struct device_type
*type
= icd
->vdev
->dev
.type
;
1336 if (!icd
->dev
.parent
)
1340 !icd
->ops
->query_bus_param
||
1341 !icd
->ops
->set_bus_param
)
1344 ret
= video_register_device(icd
->vdev
, VFL_TYPE_GRABBER
, -1);
1346 dev_err(&icd
->dev
, "video_register_device failed: %d\n", ret
);
1350 /* Restore device type, possibly set by the subdevice driver */
1351 icd
->vdev
->dev
.type
= type
;
1356 static int __devinit
soc_camera_pdrv_probe(struct platform_device
*pdev
)
1358 struct soc_camera_link
*icl
= pdev
->dev
.platform_data
;
1359 struct soc_camera_device
*icd
;
1365 icd
= kzalloc(sizeof(*icd
), GFP_KERNEL
);
1369 icd
->iface
= icl
->bus_id
;
1370 icd
->pdev
= &pdev
->dev
;
1371 platform_set_drvdata(pdev
, icd
);
1373 ret
= soc_camera_device_register(icd
);
1377 soc_camera_device_init(&icd
->dev
, icl
);
1379 icd
->user_width
= DEFAULT_WIDTH
;
1380 icd
->user_height
= DEFAULT_HEIGHT
;
1391 * Only called on rmmod for each platform device, since they are not
1392 * hot-pluggable. Now we know, that all our users - hosts and devices have
1393 * been unloaded already
1395 static int __devexit
soc_camera_pdrv_remove(struct platform_device
*pdev
)
1397 struct soc_camera_device
*icd
= platform_get_drvdata(pdev
);
1402 soc_camera_device_unregister(icd
);
1409 static struct platform_driver __refdata soc_camera_pdrv
= {
1410 .remove
= __devexit_p(soc_camera_pdrv_remove
),
1412 .name
= "soc-camera-pdrv",
1413 .owner
= THIS_MODULE
,
1417 static int __init
soc_camera_init(void)
1419 int ret
= bus_register(&soc_camera_bus_type
);
1422 ret
= driver_register(&ic_drv
);
1426 ret
= platform_driver_probe(&soc_camera_pdrv
, soc_camera_pdrv_probe
);
1433 driver_unregister(&ic_drv
);
1435 bus_unregister(&soc_camera_bus_type
);
1439 static void __exit
soc_camera_exit(void)
1441 platform_driver_unregister(&soc_camera_pdrv
);
1442 driver_unregister(&ic_drv
);
1443 bus_unregister(&soc_camera_bus_type
);
1446 module_init(soc_camera_init
);
1447 module_exit(soc_camera_exit
);
1449 MODULE_DESCRIPTION("Image capture bus driver");
1450 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1451 MODULE_LICENSE("GPL");
1452 MODULE_ALIAS("platform:soc-camera-pdrv");