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_set(struct soc_camera_device
*icd
,
54 struct soc_camera_link
*icl
,
60 ret
= regulator_bulk_enable(icl
->num_regulators
,
63 dev_err(icd
->pdev
, "Cannot enable regulators\n");
68 ret
= icl
->power(icd
->pdev
, power_on
);
71 "Platform failed to power-on the camera.\n");
73 regulator_bulk_disable(icl
->num_regulators
,
80 ret
= icl
->power(icd
->pdev
, 0);
83 "Platform failed to power-off the camera.\n");
87 ret
= regulator_bulk_disable(icl
->num_regulators
,
90 dev_err(icd
->pdev
, "Cannot disable regulators\n");
98 const struct soc_camera_format_xlate
*soc_camera_xlate_by_fourcc(
99 struct soc_camera_device
*icd
, unsigned int fourcc
)
103 for (i
= 0; i
< icd
->num_user_formats
; i
++)
104 if (icd
->user_formats
[i
].host_fmt
->fourcc
== fourcc
)
105 return icd
->user_formats
+ i
;
108 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc
);
111 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
112 * @icl: camera platform parameters
113 * @cfg: media bus configuration
114 * @return: resulting flags
116 unsigned long soc_camera_apply_board_flags(struct soc_camera_link
*icl
,
117 const struct v4l2_mbus_config
*cfg
)
119 unsigned long f
, flags
= cfg
->flags
;
121 /* If only one of the two polarities is supported, switch to the opposite */
122 if (icl
->flags
& SOCAM_SENSOR_INVERT_HSYNC
) {
123 f
= flags
& (V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_HSYNC_ACTIVE_LOW
);
124 if (f
== V4L2_MBUS_HSYNC_ACTIVE_HIGH
|| f
== V4L2_MBUS_HSYNC_ACTIVE_LOW
)
125 flags
^= V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_HSYNC_ACTIVE_LOW
;
128 if (icl
->flags
& SOCAM_SENSOR_INVERT_VSYNC
) {
129 f
= flags
& (V4L2_MBUS_VSYNC_ACTIVE_HIGH
| V4L2_MBUS_VSYNC_ACTIVE_LOW
);
130 if (f
== V4L2_MBUS_VSYNC_ACTIVE_HIGH
|| f
== V4L2_MBUS_VSYNC_ACTIVE_LOW
)
131 flags
^= V4L2_MBUS_VSYNC_ACTIVE_HIGH
| V4L2_MBUS_VSYNC_ACTIVE_LOW
;
134 if (icl
->flags
& SOCAM_SENSOR_INVERT_PCLK
) {
135 f
= flags
& (V4L2_MBUS_PCLK_SAMPLE_RISING
| V4L2_MBUS_PCLK_SAMPLE_FALLING
);
136 if (f
== V4L2_MBUS_PCLK_SAMPLE_RISING
|| f
== V4L2_MBUS_PCLK_SAMPLE_FALLING
)
137 flags
^= V4L2_MBUS_PCLK_SAMPLE_RISING
| V4L2_MBUS_PCLK_SAMPLE_FALLING
;
142 EXPORT_SYMBOL(soc_camera_apply_board_flags
);
144 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
147 static int soc_camera_try_fmt(struct soc_camera_device
*icd
,
148 struct v4l2_format
*f
)
150 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
151 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
154 dev_dbg(icd
->pdev
, "TRY_FMT(%c%c%c%c, %ux%u)\n",
155 pixfmtstr(pix
->pixelformat
), pix
->width
, pix
->height
);
157 pix
->bytesperline
= 0;
160 ret
= ici
->ops
->try_fmt(icd
, f
);
164 if (!pix
->sizeimage
) {
165 if (!pix
->bytesperline
) {
166 const struct soc_camera_format_xlate
*xlate
;
168 xlate
= soc_camera_xlate_by_fourcc(icd
, pix
->pixelformat
);
172 ret
= soc_mbus_bytes_per_line(pix
->width
,
175 pix
->bytesperline
= ret
;
177 if (pix
->bytesperline
)
178 pix
->sizeimage
= pix
->bytesperline
* pix
->height
;
184 static int soc_camera_try_fmt_vid_cap(struct file
*file
, void *priv
,
185 struct v4l2_format
*f
)
187 struct soc_camera_device
*icd
= file
->private_data
;
189 WARN_ON(priv
!= file
->private_data
);
191 /* Only single-plane capture is supported so far */
192 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
195 /* limit format to hardware capabilities */
196 return soc_camera_try_fmt(icd
, f
);
199 static int soc_camera_enum_input(struct file
*file
, void *priv
,
200 struct v4l2_input
*inp
)
205 /* default is camera */
206 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
207 inp
->std
= V4L2_STD_UNKNOWN
;
208 strcpy(inp
->name
, "Camera");
213 static int soc_camera_g_input(struct file
*file
, void *priv
, unsigned int *i
)
220 static int soc_camera_s_input(struct file
*file
, void *priv
, unsigned int i
)
228 static int soc_camera_s_std(struct file
*file
, void *priv
, v4l2_std_id
*a
)
230 struct soc_camera_device
*icd
= file
->private_data
;
231 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
233 return v4l2_subdev_call(sd
, core
, s_std
, *a
);
236 static int soc_camera_enum_fsizes(struct file
*file
, void *fh
,
237 struct v4l2_frmsizeenum
*fsize
)
239 struct soc_camera_device
*icd
= file
->private_data
;
240 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
242 return ici
->ops
->enum_fsizes(icd
, fsize
);
245 static int soc_camera_reqbufs(struct file
*file
, void *priv
,
246 struct v4l2_requestbuffers
*p
)
249 struct soc_camera_device
*icd
= file
->private_data
;
250 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
252 WARN_ON(priv
!= file
->private_data
);
254 if (icd
->streamer
&& icd
->streamer
!= file
)
257 if (ici
->ops
->init_videobuf
) {
258 ret
= videobuf_reqbufs(&icd
->vb_vidq
, p
);
262 ret
= ici
->ops
->reqbufs(icd
, p
);
264 ret
= vb2_reqbufs(&icd
->vb2_vidq
, p
);
267 if (!ret
&& !icd
->streamer
)
268 icd
->streamer
= file
;
273 static int soc_camera_querybuf(struct file
*file
, void *priv
,
274 struct v4l2_buffer
*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 (ici
->ops
->init_videobuf
)
282 return videobuf_querybuf(&icd
->vb_vidq
, p
);
284 return vb2_querybuf(&icd
->vb2_vidq
, p
);
287 static int soc_camera_qbuf(struct file
*file
, void *priv
,
288 struct v4l2_buffer
*p
)
290 struct soc_camera_device
*icd
= file
->private_data
;
291 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
293 WARN_ON(priv
!= file
->private_data
);
295 if (icd
->streamer
!= file
)
298 if (ici
->ops
->init_videobuf
)
299 return videobuf_qbuf(&icd
->vb_vidq
, p
);
301 return vb2_qbuf(&icd
->vb2_vidq
, p
);
304 static int soc_camera_dqbuf(struct file
*file
, void *priv
,
305 struct v4l2_buffer
*p
)
307 struct soc_camera_device
*icd
= file
->private_data
;
308 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
310 WARN_ON(priv
!= file
->private_data
);
312 if (icd
->streamer
!= file
)
315 if (ici
->ops
->init_videobuf
)
316 return videobuf_dqbuf(&icd
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
318 return vb2_dqbuf(&icd
->vb2_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
321 static int soc_camera_create_bufs(struct file
*file
, void *priv
,
322 struct v4l2_create_buffers
*create
)
324 struct soc_camera_device
*icd
= file
->private_data
;
325 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
328 if (ici
->ops
->init_videobuf
)
331 return vb2_create_bufs(&icd
->vb2_vidq
, create
);
334 static int soc_camera_prepare_buf(struct file
*file
, void *priv
,
335 struct v4l2_buffer
*b
)
337 struct soc_camera_device
*icd
= file
->private_data
;
338 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
341 if (ici
->ops
->init_videobuf
)
344 return vb2_prepare_buf(&icd
->vb2_vidq
, b
);
347 /* Always entered with .video_lock held */
348 static int soc_camera_init_user_formats(struct soc_camera_device
*icd
)
350 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
351 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
352 unsigned int i
, fmts
= 0, raw_fmts
= 0;
354 enum v4l2_mbus_pixelcode code
;
356 while (!v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, raw_fmts
, &code
))
359 if (!ici
->ops
->get_formats
)
361 * Fallback mode - the host will have to serve all
362 * sensor-provided formats one-to-one to the user
367 * First pass - only count formats this host-sensor
368 * configuration can provide
370 for (i
= 0; i
< raw_fmts
; i
++) {
371 ret
= ici
->ops
->get_formats(icd
, i
, NULL
);
381 vmalloc(fmts
* sizeof(struct soc_camera_format_xlate
));
382 if (!icd
->user_formats
)
385 dev_dbg(icd
->pdev
, "Found %d supported formats.\n", fmts
);
387 /* Second pass - actually fill data formats */
389 for (i
= 0; i
< raw_fmts
; i
++)
390 if (!ici
->ops
->get_formats
) {
391 v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, i
, &code
);
392 icd
->user_formats
[fmts
].host_fmt
=
393 soc_mbus_get_fmtdesc(code
);
394 if (icd
->user_formats
[fmts
].host_fmt
)
395 icd
->user_formats
[fmts
++].code
= code
;
397 ret
= ici
->ops
->get_formats(icd
, i
,
398 &icd
->user_formats
[fmts
]);
404 icd
->num_user_formats
= fmts
;
405 icd
->current_fmt
= &icd
->user_formats
[0];
410 vfree(icd
->user_formats
);
414 /* Always entered with .video_lock held */
415 static void soc_camera_free_user_formats(struct soc_camera_device
*icd
)
417 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
419 if (ici
->ops
->put_formats
)
420 ici
->ops
->put_formats(icd
);
421 icd
->current_fmt
= NULL
;
422 icd
->num_user_formats
= 0;
423 vfree(icd
->user_formats
);
424 icd
->user_formats
= NULL
;
427 /* Called with .vb_lock held, or from the first open(2), see comment there */
428 static int soc_camera_set_fmt(struct soc_camera_device
*icd
,
429 struct v4l2_format
*f
)
431 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
432 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
435 dev_dbg(icd
->pdev
, "S_FMT(%c%c%c%c, %ux%u)\n",
436 pixfmtstr(pix
->pixelformat
), pix
->width
, pix
->height
);
438 /* We always call try_fmt() before set_fmt() or set_crop() */
439 ret
= soc_camera_try_fmt(icd
, f
);
443 ret
= ici
->ops
->set_fmt(icd
, f
);
446 } else if (!icd
->current_fmt
||
447 icd
->current_fmt
->host_fmt
->fourcc
!= pix
->pixelformat
) {
449 "Host driver hasn't set up current format correctly!\n");
453 icd
->user_width
= pix
->width
;
454 icd
->user_height
= pix
->height
;
455 icd
->bytesperline
= pix
->bytesperline
;
456 icd
->sizeimage
= pix
->sizeimage
;
457 icd
->colorspace
= pix
->colorspace
;
458 icd
->field
= pix
->field
;
459 if (ici
->ops
->init_videobuf
)
460 icd
->vb_vidq
.field
= pix
->field
;
462 dev_dbg(icd
->pdev
, "set width: %d height: %d\n",
463 icd
->user_width
, icd
->user_height
);
465 /* set physical bus parameters */
466 return ici
->ops
->set_bus_param(icd
, pix
->pixelformat
);
469 static int soc_camera_open(struct file
*file
)
471 struct video_device
*vdev
= video_devdata(file
);
472 struct soc_camera_device
*icd
= dev_get_drvdata(vdev
->parent
);
473 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
474 struct soc_camera_host
*ici
;
477 if (!to_soc_camera_control(icd
))
478 /* No device driver attached */
481 ici
= to_soc_camera_host(icd
->parent
);
483 if (!try_module_get(ici
->ops
->owner
)) {
484 dev_err(icd
->pdev
, "Couldn't lock capture bus driver.\n");
490 /* Now we really have to activate the camera */
491 if (icd
->use_count
== 1) {
492 /* Restore parameters before the last close() per V4L2 API */
493 struct v4l2_format f
= {
494 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
496 .width
= icd
->user_width
,
497 .height
= icd
->user_height
,
499 .colorspace
= icd
->colorspace
,
501 icd
->current_fmt
->host_fmt
->fourcc
,
505 ret
= soc_camera_power_set(icd
, icl
, 1);
509 /* The camera could have been already on, try to reset */
511 icl
->reset(icd
->pdev
);
513 ret
= ici
->ops
->add(icd
);
515 dev_err(icd
->pdev
, "Couldn't activate the camera: %d\n", ret
);
519 pm_runtime_enable(&icd
->vdev
->dev
);
520 ret
= pm_runtime_resume(&icd
->vdev
->dev
);
521 if (ret
< 0 && ret
!= -ENOSYS
)
525 * Try to configure with default parameters. Notice: this is the
526 * very first open, so, we cannot race against other calls,
527 * apart from someone else calling open() simultaneously, but
528 * .video_lock is protecting us against it.
530 ret
= soc_camera_set_fmt(icd
, &f
);
534 if (ici
->ops
->init_videobuf
) {
535 ici
->ops
->init_videobuf(&icd
->vb_vidq
, icd
);
537 ret
= ici
->ops
->init_videobuf2(&icd
->vb2_vidq
, icd
);
541 v4l2_ctrl_handler_setup(&icd
->ctrl_handler
);
544 file
->private_data
= icd
;
545 dev_dbg(icd
->pdev
, "camera device open\n");
550 * First four errors are entered with the .video_lock held
555 pm_runtime_disable(&icd
->vdev
->dev
);
557 ici
->ops
->remove(icd
);
559 soc_camera_power_set(icd
, icl
, 0);
562 module_put(ici
->ops
->owner
);
567 static int soc_camera_close(struct file
*file
)
569 struct soc_camera_device
*icd
= file
->private_data
;
570 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
573 if (!icd
->use_count
) {
574 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
576 pm_runtime_suspend(&icd
->vdev
->dev
);
577 pm_runtime_disable(&icd
->vdev
->dev
);
579 ici
->ops
->remove(icd
);
580 if (ici
->ops
->init_videobuf2
)
581 vb2_queue_release(&icd
->vb2_vidq
);
583 soc_camera_power_set(icd
, icl
, 0);
586 if (icd
->streamer
== file
)
587 icd
->streamer
= NULL
;
589 module_put(ici
->ops
->owner
);
591 dev_dbg(icd
->pdev
, "camera device close\n");
596 static ssize_t
soc_camera_read(struct file
*file
, char __user
*buf
,
597 size_t count
, loff_t
*ppos
)
599 struct soc_camera_device
*icd
= file
->private_data
;
602 dev_err(icd
->pdev
, "camera device read not implemented\n");
607 static int soc_camera_mmap(struct file
*file
, struct vm_area_struct
*vma
)
609 struct soc_camera_device
*icd
= file
->private_data
;
610 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
613 dev_dbg(icd
->pdev
, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
615 if (icd
->streamer
!= file
)
618 if (ici
->ops
->init_videobuf
)
619 err
= videobuf_mmap_mapper(&icd
->vb_vidq
, vma
);
621 err
= vb2_mmap(&icd
->vb2_vidq
, vma
);
623 dev_dbg(icd
->pdev
, "vma start=0x%08lx, size=%ld, ret=%d\n",
624 (unsigned long)vma
->vm_start
,
625 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
631 static unsigned int soc_camera_poll(struct file
*file
, poll_table
*pt
)
633 struct soc_camera_device
*icd
= file
->private_data
;
634 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
636 if (icd
->streamer
!= file
)
639 if (ici
->ops
->init_videobuf
&& list_empty(&icd
->vb_vidq
.stream
)) {
640 dev_err(icd
->pdev
, "Trying to poll with no queued buffers!\n");
644 return ici
->ops
->poll(file
, pt
);
647 void soc_camera_lock(struct vb2_queue
*vq
)
649 struct soc_camera_device
*icd
= vb2_get_drv_priv(vq
);
650 mutex_lock(&icd
->video_lock
);
652 EXPORT_SYMBOL(soc_camera_lock
);
654 void soc_camera_unlock(struct vb2_queue
*vq
)
656 struct soc_camera_device
*icd
= vb2_get_drv_priv(vq
);
657 mutex_unlock(&icd
->video_lock
);
659 EXPORT_SYMBOL(soc_camera_unlock
);
661 static struct v4l2_file_operations soc_camera_fops
= {
662 .owner
= THIS_MODULE
,
663 .open
= soc_camera_open
,
664 .release
= soc_camera_close
,
665 .unlocked_ioctl
= video_ioctl2
,
666 .read
= soc_camera_read
,
667 .mmap
= soc_camera_mmap
,
668 .poll
= soc_camera_poll
,
671 static int soc_camera_s_fmt_vid_cap(struct file
*file
, void *priv
,
672 struct v4l2_format
*f
)
674 struct soc_camera_device
*icd
= file
->private_data
;
677 WARN_ON(priv
!= file
->private_data
);
679 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
680 dev_warn(icd
->pdev
, "Wrong buf-type %d\n", f
->type
);
684 if (icd
->streamer
&& icd
->streamer
!= file
)
687 if (is_streaming(to_soc_camera_host(icd
->parent
), icd
)) {
688 dev_err(icd
->pdev
, "S_FMT denied: queue initialised\n");
692 ret
= soc_camera_set_fmt(icd
, f
);
694 if (!ret
&& !icd
->streamer
)
695 icd
->streamer
= file
;
700 static int soc_camera_enum_fmt_vid_cap(struct file
*file
, void *priv
,
701 struct v4l2_fmtdesc
*f
)
703 struct soc_camera_device
*icd
= file
->private_data
;
704 const struct soc_mbus_pixelfmt
*format
;
706 WARN_ON(priv
!= file
->private_data
);
708 if (f
->index
>= icd
->num_user_formats
)
711 format
= icd
->user_formats
[f
->index
].host_fmt
;
714 strlcpy(f
->description
, format
->name
, sizeof(f
->description
));
715 f
->pixelformat
= format
->fourcc
;
719 static int soc_camera_g_fmt_vid_cap(struct file
*file
, void *priv
,
720 struct v4l2_format
*f
)
722 struct soc_camera_device
*icd
= file
->private_data
;
723 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
725 WARN_ON(priv
!= file
->private_data
);
727 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
730 pix
->width
= icd
->user_width
;
731 pix
->height
= icd
->user_height
;
732 pix
->bytesperline
= icd
->bytesperline
;
733 pix
->sizeimage
= icd
->sizeimage
;
734 pix
->field
= icd
->field
;
735 pix
->pixelformat
= icd
->current_fmt
->host_fmt
->fourcc
;
736 pix
->colorspace
= icd
->colorspace
;
737 dev_dbg(icd
->pdev
, "current_fmt->fourcc: 0x%08x\n",
738 icd
->current_fmt
->host_fmt
->fourcc
);
742 static int soc_camera_querycap(struct file
*file
, void *priv
,
743 struct v4l2_capability
*cap
)
745 struct soc_camera_device
*icd
= file
->private_data
;
746 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
748 WARN_ON(priv
!= file
->private_data
);
750 strlcpy(cap
->driver
, ici
->drv_name
, sizeof(cap
->driver
));
751 return ici
->ops
->querycap(ici
, cap
);
754 static int soc_camera_streamon(struct file
*file
, void *priv
,
755 enum v4l2_buf_type i
)
757 struct soc_camera_device
*icd
= file
->private_data
;
758 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
759 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
762 WARN_ON(priv
!= file
->private_data
);
764 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
767 if (icd
->streamer
!= file
)
770 /* This calls buf_queue from host driver's videobuf_queue_ops */
771 if (ici
->ops
->init_videobuf
)
772 ret
= videobuf_streamon(&icd
->vb_vidq
);
774 ret
= vb2_streamon(&icd
->vb2_vidq
, i
);
777 v4l2_subdev_call(sd
, video
, s_stream
, 1);
782 static int soc_camera_streamoff(struct file
*file
, void *priv
,
783 enum v4l2_buf_type i
)
785 struct soc_camera_device
*icd
= file
->private_data
;
786 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
787 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
789 WARN_ON(priv
!= file
->private_data
);
791 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
794 if (icd
->streamer
!= file
)
798 * This calls buf_release from host driver's videobuf_queue_ops for all
799 * remaining buffers. When the last buffer is freed, stop capture
801 if (ici
->ops
->init_videobuf
)
802 videobuf_streamoff(&icd
->vb_vidq
);
804 vb2_streamoff(&icd
->vb2_vidq
, i
);
806 v4l2_subdev_call(sd
, video
, s_stream
, 0);
811 static int soc_camera_cropcap(struct file
*file
, void *fh
,
812 struct v4l2_cropcap
*a
)
814 struct soc_camera_device
*icd
= file
->private_data
;
815 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
817 return ici
->ops
->cropcap(icd
, a
);
820 static int soc_camera_g_crop(struct file
*file
, void *fh
,
823 struct soc_camera_device
*icd
= file
->private_data
;
824 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
827 ret
= ici
->ops
->get_crop(icd
, a
);
833 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
834 * argument with the actual geometry, instead, the user shall use G_CROP to
837 static int soc_camera_s_crop(struct file
*file
, void *fh
,
840 struct soc_camera_device
*icd
= file
->private_data
;
841 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
842 struct v4l2_rect
*rect
= &a
->c
;
843 struct v4l2_crop current_crop
;
846 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
849 dev_dbg(icd
->pdev
, "S_CROP(%ux%u@%u:%u)\n",
850 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
852 /* If get_crop fails, we'll let host and / or client drivers decide */
853 ret
= ici
->ops
->get_crop(icd
, ¤t_crop
);
855 /* Prohibit window size change with initialised buffers */
858 "S_CROP denied: getting current crop failed\n");
859 } else if ((a
->c
.width
== current_crop
.c
.width
&&
860 a
->c
.height
== current_crop
.c
.height
) ||
861 !is_streaming(ici
, icd
)) {
862 /* same size or not streaming - use .set_crop() */
863 ret
= ici
->ops
->set_crop(icd
, a
);
864 } else if (ici
->ops
->set_livecrop
) {
865 ret
= ici
->ops
->set_livecrop(icd
, a
);
868 "S_CROP denied: queue initialised and sizes differ\n");
875 static int soc_camera_g_parm(struct file
*file
, void *fh
,
876 struct v4l2_streamparm
*a
)
878 struct soc_camera_device
*icd
= file
->private_data
;
879 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
881 if (ici
->ops
->get_parm
)
882 return ici
->ops
->get_parm(icd
, a
);
887 static int soc_camera_s_parm(struct file
*file
, void *fh
,
888 struct v4l2_streamparm
*a
)
890 struct soc_camera_device
*icd
= file
->private_data
;
891 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
893 if (ici
->ops
->set_parm
)
894 return ici
->ops
->set_parm(icd
, a
);
899 static int soc_camera_g_chip_ident(struct file
*file
, void *fh
,
900 struct v4l2_dbg_chip_ident
*id
)
902 struct soc_camera_device
*icd
= file
->private_data
;
903 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
905 return v4l2_subdev_call(sd
, core
, g_chip_ident
, id
);
908 #ifdef CONFIG_VIDEO_ADV_DEBUG
909 static int soc_camera_g_register(struct file
*file
, void *fh
,
910 struct v4l2_dbg_register
*reg
)
912 struct soc_camera_device
*icd
= file
->private_data
;
913 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
915 return v4l2_subdev_call(sd
, core
, g_register
, reg
);
918 static int soc_camera_s_register(struct file
*file
, void *fh
,
919 struct v4l2_dbg_register
*reg
)
921 struct soc_camera_device
*icd
= file
->private_data
;
922 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
924 return v4l2_subdev_call(sd
, core
, s_register
, reg
);
928 static int soc_camera_probe(struct soc_camera_device
*icd
);
930 /* So far this function cannot fail */
931 static void scan_add_host(struct soc_camera_host
*ici
)
933 struct soc_camera_device
*icd
;
935 mutex_lock(&list_lock
);
937 list_for_each_entry(icd
, &devices
, list
) {
938 if (icd
->iface
== ici
->nr
) {
941 icd
->parent
= ici
->v4l2_dev
.dev
;
942 ret
= soc_camera_probe(icd
);
946 mutex_unlock(&list_lock
);
949 #ifdef CONFIG_I2C_BOARDINFO
950 static int soc_camera_init_i2c(struct soc_camera_device
*icd
,
951 struct soc_camera_link
*icl
)
953 struct i2c_client
*client
;
954 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
955 struct i2c_adapter
*adap
= i2c_get_adapter(icl
->i2c_adapter_id
);
956 struct v4l2_subdev
*subdev
;
959 dev_err(icd
->pdev
, "Cannot get I2C adapter #%d. No driver?\n",
960 icl
->i2c_adapter_id
);
964 icl
->board_info
->platform_data
= icd
;
966 subdev
= v4l2_i2c_new_subdev_board(&ici
->v4l2_dev
, adap
,
967 icl
->board_info
, NULL
);
971 client
= v4l2_get_subdevdata(subdev
);
973 /* Use to_i2c_client(dev) to recover the i2c client */
974 icd
->control
= &client
->dev
;
978 i2c_put_adapter(adap
);
983 static void soc_camera_free_i2c(struct soc_camera_device
*icd
)
985 struct i2c_client
*client
=
986 to_i2c_client(to_soc_camera_control(icd
));
987 struct i2c_adapter
*adap
= client
->adapter
;
990 v4l2_device_unregister_subdev(i2c_get_clientdata(client
));
991 i2c_unregister_device(client
);
992 i2c_put_adapter(adap
);
995 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
996 #define soc_camera_free_i2c(icd) do {} while (0)
999 static int soc_camera_video_start(struct soc_camera_device
*icd
);
1000 static int video_dev_create(struct soc_camera_device
*icd
);
1001 /* Called during host-driver probe */
1002 static int soc_camera_probe(struct soc_camera_device
*icd
)
1004 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1005 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
1006 struct device
*control
= NULL
;
1007 struct v4l2_subdev
*sd
;
1008 struct v4l2_mbus_framefmt mf
;
1011 dev_info(icd
->pdev
, "Probing %s\n", dev_name(icd
->pdev
));
1014 * Currently the subdev with the largest number of controls (13) is
1015 * ov6550. So let's pick 16 as a hint for the control handler. Note
1016 * that this is a hint only: too large and you waste some memory, too
1017 * small and there is a (very) small performance hit when looking up
1018 * controls in the internal hash.
1020 ret
= v4l2_ctrl_handler_init(&icd
->ctrl_handler
, 16);
1024 ret
= regulator_bulk_get(icd
->pdev
, icl
->num_regulators
,
1029 ret
= soc_camera_power_set(icd
, icl
, 1);
1033 /* The camera could have been already on, try to reset */
1035 icl
->reset(icd
->pdev
);
1037 ret
= ici
->ops
->add(icd
);
1041 /* Must have icd->vdev before registering the device */
1042 ret
= video_dev_create(icd
);
1046 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1047 if (icl
->board_info
) {
1048 ret
= soc_camera_init_i2c(icd
, icl
);
1051 } else if (!icl
->add_device
|| !icl
->del_device
) {
1055 if (icl
->module_name
)
1056 ret
= request_module(icl
->module_name
);
1058 ret
= icl
->add_device(icd
);
1063 * FIXME: this is racy, have to use driver-binding notification,
1064 * when it is available
1066 control
= to_soc_camera_control(icd
);
1067 if (!control
|| !control
->driver
|| !dev_get_drvdata(control
) ||
1068 !try_module_get(control
->driver
->owner
)) {
1069 icl
->del_device(icd
);
1075 sd
= soc_camera_to_subdev(icd
);
1076 sd
->grp_id
= (long)icd
;
1078 if (v4l2_ctrl_add_handler(&icd
->ctrl_handler
, sd
->ctrl_handler
))
1081 /* At this point client .probe() should have run already */
1082 ret
= soc_camera_init_user_formats(icd
);
1086 icd
->field
= V4L2_FIELD_ANY
;
1089 * ..._video_start() will create a device node, video_register_device()
1090 * itself is protected against concurrent open() calls, but we also have
1091 * to protect our data.
1093 mutex_lock(&icd
->video_lock
);
1095 ret
= soc_camera_video_start(icd
);
1099 /* Try to improve our guess of a reasonable window format */
1100 if (!v4l2_subdev_call(sd
, video
, g_mbus_fmt
, &mf
)) {
1101 icd
->user_width
= mf
.width
;
1102 icd
->user_height
= mf
.height
;
1103 icd
->colorspace
= mf
.colorspace
;
1104 icd
->field
= mf
.field
;
1107 ici
->ops
->remove(icd
);
1109 soc_camera_power_set(icd
, icl
, 0);
1111 mutex_unlock(&icd
->video_lock
);
1116 mutex_unlock(&icd
->video_lock
);
1117 soc_camera_free_user_formats(icd
);
1120 if (icl
->board_info
) {
1121 soc_camera_free_i2c(icd
);
1123 icl
->del_device(icd
);
1124 module_put(control
->driver
->owner
);
1128 video_device_release(icd
->vdev
);
1130 ici
->ops
->remove(icd
);
1132 soc_camera_power_set(icd
, icl
, 0);
1134 regulator_bulk_free(icl
->num_regulators
, icl
->regulators
);
1136 v4l2_ctrl_handler_free(&icd
->ctrl_handler
);
1141 * This is called on device_unregister, which only means we have to disconnect
1142 * from the host, but not remove ourselves from the device list
1144 static int soc_camera_remove(struct soc_camera_device
*icd
)
1146 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
1147 struct video_device
*vdev
= icd
->vdev
;
1149 BUG_ON(!icd
->parent
);
1151 v4l2_ctrl_handler_free(&icd
->ctrl_handler
);
1153 video_unregister_device(vdev
);
1157 if (icl
->board_info
) {
1158 soc_camera_free_i2c(icd
);
1160 struct device_driver
*drv
= to_soc_camera_control(icd
)->driver
;
1162 icl
->del_device(icd
);
1163 module_put(drv
->owner
);
1166 soc_camera_free_user_formats(icd
);
1168 regulator_bulk_free(icl
->num_regulators
, icl
->regulators
);
1173 static int default_cropcap(struct soc_camera_device
*icd
,
1174 struct v4l2_cropcap
*a
)
1176 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1177 return v4l2_subdev_call(sd
, video
, cropcap
, a
);
1180 static int default_g_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*a
)
1182 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1183 return v4l2_subdev_call(sd
, video
, g_crop
, a
);
1186 static int default_s_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*a
)
1188 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1189 return v4l2_subdev_call(sd
, video
, s_crop
, a
);
1192 static int default_g_parm(struct soc_camera_device
*icd
,
1193 struct v4l2_streamparm
*parm
)
1195 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1196 return v4l2_subdev_call(sd
, video
, g_parm
, parm
);
1199 static int default_s_parm(struct soc_camera_device
*icd
,
1200 struct v4l2_streamparm
*parm
)
1202 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1203 return v4l2_subdev_call(sd
, video
, s_parm
, parm
);
1206 static int default_enum_fsizes(struct soc_camera_device
*icd
,
1207 struct v4l2_frmsizeenum
*fsize
)
1210 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1211 const struct soc_camera_format_xlate
*xlate
;
1212 __u32 pixfmt
= fsize
->pixel_format
;
1213 struct v4l2_frmsizeenum fsize_mbus
= *fsize
;
1215 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1218 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1219 fsize_mbus
.pixel_format
= xlate
->code
;
1221 ret
= v4l2_subdev_call(sd
, video
, enum_mbus_fsizes
, &fsize_mbus
);
1225 *fsize
= fsize_mbus
;
1226 fsize
->pixel_format
= pixfmt
;
1231 int soc_camera_host_register(struct soc_camera_host
*ici
)
1233 struct soc_camera_host
*ix
;
1236 if (!ici
|| !ici
->ops
||
1237 !ici
->ops
->try_fmt
||
1238 !ici
->ops
->set_fmt
||
1239 !ici
->ops
->set_bus_param
||
1240 !ici
->ops
->querycap
||
1241 ((!ici
->ops
->init_videobuf
||
1242 !ici
->ops
->reqbufs
) &&
1243 !ici
->ops
->init_videobuf2
) ||
1245 !ici
->ops
->remove
||
1250 if (!ici
->ops
->set_crop
)
1251 ici
->ops
->set_crop
= default_s_crop
;
1252 if (!ici
->ops
->get_crop
)
1253 ici
->ops
->get_crop
= default_g_crop
;
1254 if (!ici
->ops
->cropcap
)
1255 ici
->ops
->cropcap
= default_cropcap
;
1256 if (!ici
->ops
->set_parm
)
1257 ici
->ops
->set_parm
= default_s_parm
;
1258 if (!ici
->ops
->get_parm
)
1259 ici
->ops
->get_parm
= default_g_parm
;
1260 if (!ici
->ops
->enum_fsizes
)
1261 ici
->ops
->enum_fsizes
= default_enum_fsizes
;
1263 mutex_lock(&list_lock
);
1264 list_for_each_entry(ix
, &hosts
, list
) {
1265 if (ix
->nr
== ici
->nr
) {
1271 ret
= v4l2_device_register(ici
->v4l2_dev
.dev
, &ici
->v4l2_dev
);
1275 list_add_tail(&ici
->list
, &hosts
);
1276 mutex_unlock(&list_lock
);
1283 mutex_unlock(&list_lock
);
1286 EXPORT_SYMBOL(soc_camera_host_register
);
1288 /* Unregister all clients! */
1289 void soc_camera_host_unregister(struct soc_camera_host
*ici
)
1291 struct soc_camera_device
*icd
;
1293 mutex_lock(&list_lock
);
1295 list_del(&ici
->list
);
1296 list_for_each_entry(icd
, &devices
, list
)
1297 if (icd
->iface
== ici
->nr
&& to_soc_camera_control(icd
))
1298 soc_camera_remove(icd
);
1300 mutex_unlock(&list_lock
);
1302 v4l2_device_unregister(&ici
->v4l2_dev
);
1304 EXPORT_SYMBOL(soc_camera_host_unregister
);
1306 /* Image capture device */
1307 static int soc_camera_device_register(struct soc_camera_device
*icd
)
1309 struct soc_camera_device
*ix
;
1312 for (i
= 0; i
< 256 && num
< 0; i
++) {
1314 /* Check if this index is available on this interface */
1315 list_for_each_entry(ix
, &devices
, list
) {
1316 if (ix
->iface
== icd
->iface
&& ix
->devnum
== i
) {
1325 * ok, we have 256 cameras on this host...
1326 * man, stay reasonable...
1332 icd
->host_priv
= NULL
;
1333 mutex_init(&icd
->video_lock
);
1335 list_add_tail(&icd
->list
, &devices
);
1340 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops
= {
1341 .vidioc_querycap
= soc_camera_querycap
,
1342 .vidioc_try_fmt_vid_cap
= soc_camera_try_fmt_vid_cap
,
1343 .vidioc_g_fmt_vid_cap
= soc_camera_g_fmt_vid_cap
,
1344 .vidioc_s_fmt_vid_cap
= soc_camera_s_fmt_vid_cap
,
1345 .vidioc_enum_fmt_vid_cap
= soc_camera_enum_fmt_vid_cap
,
1346 .vidioc_enum_input
= soc_camera_enum_input
,
1347 .vidioc_g_input
= soc_camera_g_input
,
1348 .vidioc_s_input
= soc_camera_s_input
,
1349 .vidioc_s_std
= soc_camera_s_std
,
1350 .vidioc_enum_framesizes
= soc_camera_enum_fsizes
,
1351 .vidioc_reqbufs
= soc_camera_reqbufs
,
1352 .vidioc_querybuf
= soc_camera_querybuf
,
1353 .vidioc_qbuf
= soc_camera_qbuf
,
1354 .vidioc_dqbuf
= soc_camera_dqbuf
,
1355 .vidioc_create_bufs
= soc_camera_create_bufs
,
1356 .vidioc_prepare_buf
= soc_camera_prepare_buf
,
1357 .vidioc_streamon
= soc_camera_streamon
,
1358 .vidioc_streamoff
= soc_camera_streamoff
,
1359 .vidioc_cropcap
= soc_camera_cropcap
,
1360 .vidioc_g_crop
= soc_camera_g_crop
,
1361 .vidioc_s_crop
= soc_camera_s_crop
,
1362 .vidioc_g_parm
= soc_camera_g_parm
,
1363 .vidioc_s_parm
= soc_camera_s_parm
,
1364 .vidioc_g_chip_ident
= soc_camera_g_chip_ident
,
1365 #ifdef CONFIG_VIDEO_ADV_DEBUG
1366 .vidioc_g_register
= soc_camera_g_register
,
1367 .vidioc_s_register
= soc_camera_s_register
,
1371 static int video_dev_create(struct soc_camera_device
*icd
)
1373 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1374 struct video_device
*vdev
= video_device_alloc();
1379 strlcpy(vdev
->name
, ici
->drv_name
, sizeof(vdev
->name
));
1381 vdev
->parent
= icd
->pdev
;
1382 vdev
->current_norm
= V4L2_STD_UNKNOWN
;
1383 vdev
->fops
= &soc_camera_fops
;
1384 vdev
->ioctl_ops
= &soc_camera_ioctl_ops
;
1385 vdev
->release
= video_device_release
;
1386 vdev
->tvnorms
= V4L2_STD_UNKNOWN
;
1387 vdev
->ctrl_handler
= &icd
->ctrl_handler
;
1388 vdev
->lock
= &icd
->video_lock
;
1396 * Called from soc_camera_probe() above (with .video_lock held???)
1398 static int soc_camera_video_start(struct soc_camera_device
*icd
)
1400 const struct device_type
*type
= icd
->vdev
->dev
.type
;
1406 ret
= video_register_device(icd
->vdev
, VFL_TYPE_GRABBER
, -1);
1408 dev_err(icd
->pdev
, "video_register_device failed: %d\n", ret
);
1412 /* Restore device type, possibly set by the subdevice driver */
1413 icd
->vdev
->dev
.type
= type
;
1418 static int __devinit
soc_camera_pdrv_probe(struct platform_device
*pdev
)
1420 struct soc_camera_link
*icl
= pdev
->dev
.platform_data
;
1421 struct soc_camera_device
*icd
;
1427 icd
= kzalloc(sizeof(*icd
), GFP_KERNEL
);
1431 icd
->iface
= icl
->bus_id
;
1433 icd
->pdev
= &pdev
->dev
;
1434 platform_set_drvdata(pdev
, icd
);
1436 ret
= soc_camera_device_register(icd
);
1440 icd
->user_width
= DEFAULT_WIDTH
;
1441 icd
->user_height
= DEFAULT_HEIGHT
;
1452 * Only called on rmmod for each platform device, since they are not
1453 * hot-pluggable. Now we know, that all our users - hosts and devices have
1454 * been unloaded already
1456 static int __devexit
soc_camera_pdrv_remove(struct platform_device
*pdev
)
1458 struct soc_camera_device
*icd
= platform_get_drvdata(pdev
);
1463 list_del(&icd
->list
);
1470 static struct platform_driver __refdata soc_camera_pdrv
= {
1471 .remove
= __devexit_p(soc_camera_pdrv_remove
),
1473 .name
= "soc-camera-pdrv",
1474 .owner
= THIS_MODULE
,
1478 static int __init
soc_camera_init(void)
1480 return platform_driver_probe(&soc_camera_pdrv
, soc_camera_pdrv_probe
);
1483 static void __exit
soc_camera_exit(void)
1485 platform_driver_unregister(&soc_camera_pdrv
);
1488 module_init(soc_camera_init
);
1489 module_exit(soc_camera_exit
);
1491 MODULE_DESCRIPTION("Image capture bus driver");
1492 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1493 MODULE_LICENSE("GPL");
1494 MODULE_ALIAS("platform:soc-camera-pdrv");