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_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
112 * @icl: camera platform parameters
113 * @flags: flags to be inverted according to platform configuration
114 * @return: resulting flags
116 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link
*icl
,
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
& (SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_HSYNC_ACTIVE_LOW
);
124 if (f
== SOCAM_HSYNC_ACTIVE_HIGH
|| f
== SOCAM_HSYNC_ACTIVE_LOW
)
125 flags
^= SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_HSYNC_ACTIVE_LOW
;
128 if (icl
->flags
& SOCAM_SENSOR_INVERT_VSYNC
) {
129 f
= flags
& (SOCAM_VSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_LOW
);
130 if (f
== SOCAM_VSYNC_ACTIVE_HIGH
|| f
== SOCAM_VSYNC_ACTIVE_LOW
)
131 flags
^= SOCAM_VSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_LOW
;
134 if (icl
->flags
& SOCAM_SENSOR_INVERT_PCLK
) {
135 f
= flags
& (SOCAM_PCLK_SAMPLE_RISING
| SOCAM_PCLK_SAMPLE_FALLING
);
136 if (f
== SOCAM_PCLK_SAMPLE_RISING
|| f
== SOCAM_PCLK_SAMPLE_FALLING
)
137 flags
^= SOCAM_PCLK_SAMPLE_RISING
| SOCAM_PCLK_SAMPLE_FALLING
;
142 EXPORT_SYMBOL(soc_camera_apply_sensor_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 /* Always entered with .video_lock held */
322 static int soc_camera_init_user_formats(struct soc_camera_device
*icd
)
324 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
325 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
326 unsigned int i
, fmts
= 0, raw_fmts
= 0;
328 enum v4l2_mbus_pixelcode code
;
330 while (!v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, raw_fmts
, &code
))
333 if (!ici
->ops
->get_formats
)
335 * Fallback mode - the host will have to serve all
336 * sensor-provided formats one-to-one to the user
341 * First pass - only count formats this host-sensor
342 * configuration can provide
344 for (i
= 0; i
< raw_fmts
; i
++) {
345 ret
= ici
->ops
->get_formats(icd
, i
, NULL
);
355 vmalloc(fmts
* sizeof(struct soc_camera_format_xlate
));
356 if (!icd
->user_formats
)
359 dev_dbg(icd
->pdev
, "Found %d supported formats.\n", fmts
);
361 /* Second pass - actually fill data formats */
363 for (i
= 0; i
< raw_fmts
; i
++)
364 if (!ici
->ops
->get_formats
) {
365 v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, i
, &code
);
366 icd
->user_formats
[fmts
].host_fmt
=
367 soc_mbus_get_fmtdesc(code
);
368 if (icd
->user_formats
[fmts
].host_fmt
)
369 icd
->user_formats
[fmts
++].code
= code
;
371 ret
= ici
->ops
->get_formats(icd
, i
,
372 &icd
->user_formats
[fmts
]);
378 icd
->num_user_formats
= fmts
;
379 icd
->current_fmt
= &icd
->user_formats
[0];
384 vfree(icd
->user_formats
);
388 /* Always entered with .video_lock held */
389 static void soc_camera_free_user_formats(struct soc_camera_device
*icd
)
391 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
393 if (ici
->ops
->put_formats
)
394 ici
->ops
->put_formats(icd
);
395 icd
->current_fmt
= NULL
;
396 icd
->num_user_formats
= 0;
397 vfree(icd
->user_formats
);
398 icd
->user_formats
= NULL
;
401 /* Called with .vb_lock held, or from the first open(2), see comment there */
402 static int soc_camera_set_fmt(struct soc_camera_device
*icd
,
403 struct v4l2_format
*f
)
405 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
406 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
409 dev_dbg(icd
->pdev
, "S_FMT(%c%c%c%c, %ux%u)\n",
410 pixfmtstr(pix
->pixelformat
), pix
->width
, pix
->height
);
412 /* We always call try_fmt() before set_fmt() or set_crop() */
413 ret
= soc_camera_try_fmt(icd
, f
);
417 ret
= ici
->ops
->set_fmt(icd
, f
);
420 } else if (!icd
->current_fmt
||
421 icd
->current_fmt
->host_fmt
->fourcc
!= pix
->pixelformat
) {
423 "Host driver hasn't set up current format correctly!\n");
427 icd
->user_width
= pix
->width
;
428 icd
->user_height
= pix
->height
;
429 icd
->bytesperline
= pix
->bytesperline
;
430 icd
->sizeimage
= pix
->sizeimage
;
431 icd
->colorspace
= pix
->colorspace
;
432 icd
->field
= pix
->field
;
433 if (ici
->ops
->init_videobuf
)
434 icd
->vb_vidq
.field
= pix
->field
;
436 dev_dbg(icd
->pdev
, "set width: %d height: %d\n",
437 icd
->user_width
, icd
->user_height
);
439 /* set physical bus parameters */
440 return ici
->ops
->set_bus_param(icd
, pix
->pixelformat
);
443 static int soc_camera_open(struct file
*file
)
445 struct video_device
*vdev
= video_devdata(file
);
446 struct soc_camera_device
*icd
= dev_get_drvdata(vdev
->parent
);
447 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
448 struct soc_camera_host
*ici
;
452 /* No device driver attached */
455 ici
= to_soc_camera_host(icd
->parent
);
457 if (!try_module_get(ici
->ops
->owner
)) {
458 dev_err(icd
->pdev
, "Couldn't lock capture bus driver.\n");
464 /* Now we really have to activate the camera */
465 if (icd
->use_count
== 1) {
466 /* Restore parameters before the last close() per V4L2 API */
467 struct v4l2_format f
= {
468 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
470 .width
= icd
->user_width
,
471 .height
= icd
->user_height
,
473 .colorspace
= icd
->colorspace
,
475 icd
->current_fmt
->host_fmt
->fourcc
,
479 ret
= soc_camera_power_set(icd
, icl
, 1);
483 /* The camera could have been already on, try to reset */
485 icl
->reset(icd
->pdev
);
487 ret
= ici
->ops
->add(icd
);
489 dev_err(icd
->pdev
, "Couldn't activate the camera: %d\n", ret
);
493 pm_runtime_enable(&icd
->vdev
->dev
);
494 ret
= pm_runtime_resume(&icd
->vdev
->dev
);
495 if (ret
< 0 && ret
!= -ENOSYS
)
499 * Try to configure with default parameters. Notice: this is the
500 * very first open, so, we cannot race against other calls,
501 * apart from someone else calling open() simultaneously, but
502 * .video_lock is protecting us against it.
504 ret
= soc_camera_set_fmt(icd
, &f
);
508 if (ici
->ops
->init_videobuf
) {
509 ici
->ops
->init_videobuf(&icd
->vb_vidq
, icd
);
511 ret
= ici
->ops
->init_videobuf2(&icd
->vb2_vidq
, icd
);
517 file
->private_data
= icd
;
518 dev_dbg(icd
->pdev
, "camera device open\n");
523 * First four errors are entered with the .video_lock held
528 pm_runtime_disable(&icd
->vdev
->dev
);
530 ici
->ops
->remove(icd
);
532 soc_camera_power_set(icd
, icl
, 0);
535 module_put(ici
->ops
->owner
);
540 static int soc_camera_close(struct file
*file
)
542 struct soc_camera_device
*icd
= file
->private_data
;
543 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
546 if (!icd
->use_count
) {
547 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
549 pm_runtime_suspend(&icd
->vdev
->dev
);
550 pm_runtime_disable(&icd
->vdev
->dev
);
552 ici
->ops
->remove(icd
);
553 if (ici
->ops
->init_videobuf2
)
554 vb2_queue_release(&icd
->vb2_vidq
);
556 soc_camera_power_set(icd
, icl
, 0);
559 if (icd
->streamer
== file
)
560 icd
->streamer
= NULL
;
562 module_put(ici
->ops
->owner
);
564 dev_dbg(icd
->pdev
, "camera device close\n");
569 static ssize_t
soc_camera_read(struct file
*file
, char __user
*buf
,
570 size_t count
, loff_t
*ppos
)
572 struct soc_camera_device
*icd
= file
->private_data
;
575 dev_err(icd
->pdev
, "camera device read not implemented\n");
580 static int soc_camera_mmap(struct file
*file
, struct vm_area_struct
*vma
)
582 struct soc_camera_device
*icd
= file
->private_data
;
583 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
586 dev_dbg(icd
->pdev
, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
588 if (icd
->streamer
!= file
)
591 if (ici
->ops
->init_videobuf
)
592 err
= videobuf_mmap_mapper(&icd
->vb_vidq
, vma
);
594 err
= vb2_mmap(&icd
->vb2_vidq
, vma
);
596 dev_dbg(icd
->pdev
, "vma start=0x%08lx, size=%ld, ret=%d\n",
597 (unsigned long)vma
->vm_start
,
598 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
604 static unsigned int soc_camera_poll(struct file
*file
, poll_table
*pt
)
606 struct soc_camera_device
*icd
= file
->private_data
;
607 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
609 if (icd
->streamer
!= file
)
612 if (ici
->ops
->init_videobuf
&& list_empty(&icd
->vb_vidq
.stream
)) {
613 dev_err(icd
->pdev
, "Trying to poll with no queued buffers!\n");
617 return ici
->ops
->poll(file
, pt
);
620 void soc_camera_lock(struct vb2_queue
*vq
)
622 struct soc_camera_device
*icd
= vb2_get_drv_priv(vq
);
623 mutex_lock(&icd
->video_lock
);
625 EXPORT_SYMBOL(soc_camera_lock
);
627 void soc_camera_unlock(struct vb2_queue
*vq
)
629 struct soc_camera_device
*icd
= vb2_get_drv_priv(vq
);
630 mutex_unlock(&icd
->video_lock
);
632 EXPORT_SYMBOL(soc_camera_unlock
);
634 static struct v4l2_file_operations soc_camera_fops
= {
635 .owner
= THIS_MODULE
,
636 .open
= soc_camera_open
,
637 .release
= soc_camera_close
,
638 .unlocked_ioctl
= video_ioctl2
,
639 .read
= soc_camera_read
,
640 .mmap
= soc_camera_mmap
,
641 .poll
= soc_camera_poll
,
644 static int soc_camera_s_fmt_vid_cap(struct file
*file
, void *priv
,
645 struct v4l2_format
*f
)
647 struct soc_camera_device
*icd
= file
->private_data
;
650 WARN_ON(priv
!= file
->private_data
);
652 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
653 dev_warn(icd
->pdev
, "Wrong buf-type %d\n", f
->type
);
657 if (icd
->streamer
&& icd
->streamer
!= file
)
660 if (is_streaming(to_soc_camera_host(icd
->parent
), icd
)) {
661 dev_err(icd
->pdev
, "S_FMT denied: queue initialised\n");
665 ret
= soc_camera_set_fmt(icd
, f
);
667 if (!ret
&& !icd
->streamer
)
668 icd
->streamer
= file
;
673 static int soc_camera_enum_fmt_vid_cap(struct file
*file
, void *priv
,
674 struct v4l2_fmtdesc
*f
)
676 struct soc_camera_device
*icd
= file
->private_data
;
677 const struct soc_mbus_pixelfmt
*format
;
679 WARN_ON(priv
!= file
->private_data
);
681 if (f
->index
>= icd
->num_user_formats
)
684 format
= icd
->user_formats
[f
->index
].host_fmt
;
687 strlcpy(f
->description
, format
->name
, sizeof(f
->description
));
688 f
->pixelformat
= format
->fourcc
;
692 static int soc_camera_g_fmt_vid_cap(struct file
*file
, void *priv
,
693 struct v4l2_format
*f
)
695 struct soc_camera_device
*icd
= file
->private_data
;
696 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
698 WARN_ON(priv
!= file
->private_data
);
700 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
703 pix
->width
= icd
->user_width
;
704 pix
->height
= icd
->user_height
;
705 pix
->bytesperline
= icd
->bytesperline
;
706 pix
->sizeimage
= icd
->sizeimage
;
707 pix
->field
= icd
->field
;
708 pix
->pixelformat
= icd
->current_fmt
->host_fmt
->fourcc
;
709 pix
->colorspace
= icd
->colorspace
;
710 dev_dbg(icd
->pdev
, "current_fmt->fourcc: 0x%08x\n",
711 icd
->current_fmt
->host_fmt
->fourcc
);
715 static int soc_camera_querycap(struct file
*file
, void *priv
,
716 struct v4l2_capability
*cap
)
718 struct soc_camera_device
*icd
= file
->private_data
;
719 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
721 WARN_ON(priv
!= file
->private_data
);
723 strlcpy(cap
->driver
, ici
->drv_name
, sizeof(cap
->driver
));
724 return ici
->ops
->querycap(ici
, cap
);
727 static int soc_camera_streamon(struct file
*file
, void *priv
,
728 enum v4l2_buf_type i
)
730 struct soc_camera_device
*icd
= file
->private_data
;
731 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
732 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
735 WARN_ON(priv
!= file
->private_data
);
737 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
740 if (icd
->streamer
!= file
)
743 /* This calls buf_queue from host driver's videobuf_queue_ops */
744 if (ici
->ops
->init_videobuf
)
745 ret
= videobuf_streamon(&icd
->vb_vidq
);
747 ret
= vb2_streamon(&icd
->vb2_vidq
, i
);
750 v4l2_subdev_call(sd
, video
, s_stream
, 1);
755 static int soc_camera_streamoff(struct file
*file
, void *priv
,
756 enum v4l2_buf_type i
)
758 struct soc_camera_device
*icd
= file
->private_data
;
759 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
760 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
762 WARN_ON(priv
!= file
->private_data
);
764 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
767 if (icd
->streamer
!= file
)
771 * This calls buf_release from host driver's videobuf_queue_ops for all
772 * remaining buffers. When the last buffer is freed, stop capture
774 if (ici
->ops
->init_videobuf
)
775 videobuf_streamoff(&icd
->vb_vidq
);
777 vb2_streamoff(&icd
->vb2_vidq
, i
);
779 v4l2_subdev_call(sd
, video
, s_stream
, 0);
784 static int soc_camera_queryctrl(struct file
*file
, void *priv
,
785 struct v4l2_queryctrl
*qc
)
787 struct soc_camera_device
*icd
= file
->private_data
;
788 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
791 WARN_ON(priv
!= file
->private_data
);
796 /* First check host controls */
797 for (i
= 0; i
< ici
->ops
->num_controls
; i
++)
798 if (qc
->id
== ici
->ops
->controls
[i
].id
) {
799 memcpy(qc
, &(ici
->ops
->controls
[i
]),
804 /* Then device controls */
805 for (i
= 0; i
< icd
->ops
->num_controls
; i
++)
806 if (qc
->id
== icd
->ops
->controls
[i
].id
) {
807 memcpy(qc
, &(icd
->ops
->controls
[i
]),
815 static int soc_camera_g_ctrl(struct file
*file
, void *priv
,
816 struct v4l2_control
*ctrl
)
818 struct soc_camera_device
*icd
= file
->private_data
;
819 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
820 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
823 WARN_ON(priv
!= file
->private_data
);
825 if (ici
->ops
->get_ctrl
) {
826 ret
= ici
->ops
->get_ctrl(icd
, ctrl
);
827 if (ret
!= -ENOIOCTLCMD
)
831 return v4l2_subdev_call(sd
, core
, g_ctrl
, ctrl
);
834 static int soc_camera_s_ctrl(struct file
*file
, void *priv
,
835 struct v4l2_control
*ctrl
)
837 struct soc_camera_device
*icd
= file
->private_data
;
838 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
839 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
842 WARN_ON(priv
!= file
->private_data
);
844 if (ici
->ops
->set_ctrl
) {
845 ret
= ici
->ops
->set_ctrl(icd
, ctrl
);
846 if (ret
!= -ENOIOCTLCMD
)
850 return v4l2_subdev_call(sd
, core
, s_ctrl
, ctrl
);
853 static int soc_camera_cropcap(struct file
*file
, void *fh
,
854 struct v4l2_cropcap
*a
)
856 struct soc_camera_device
*icd
= file
->private_data
;
857 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
859 return ici
->ops
->cropcap(icd
, a
);
862 static int soc_camera_g_crop(struct file
*file
, void *fh
,
865 struct soc_camera_device
*icd
= file
->private_data
;
866 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
869 ret
= ici
->ops
->get_crop(icd
, a
);
875 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
876 * argument with the actual geometry, instead, the user shall use G_CROP to
879 static int soc_camera_s_crop(struct file
*file
, void *fh
,
882 struct soc_camera_device
*icd
= file
->private_data
;
883 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
884 struct v4l2_rect
*rect
= &a
->c
;
885 struct v4l2_crop current_crop
;
888 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
891 dev_dbg(icd
->pdev
, "S_CROP(%ux%u@%u:%u)\n",
892 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
894 /* If get_crop fails, we'll let host and / or client drivers decide */
895 ret
= ici
->ops
->get_crop(icd
, ¤t_crop
);
897 /* Prohibit window size change with initialised buffers */
900 "S_CROP denied: getting current crop failed\n");
901 } else if ((a
->c
.width
== current_crop
.c
.width
&&
902 a
->c
.height
== current_crop
.c
.height
) ||
903 !is_streaming(ici
, icd
)) {
904 /* same size or not streaming - use .set_crop() */
905 ret
= ici
->ops
->set_crop(icd
, a
);
906 } else if (ici
->ops
->set_livecrop
) {
907 ret
= ici
->ops
->set_livecrop(icd
, a
);
910 "S_CROP denied: queue initialised and sizes differ\n");
917 static int soc_camera_g_parm(struct file
*file
, void *fh
,
918 struct v4l2_streamparm
*a
)
920 struct soc_camera_device
*icd
= file
->private_data
;
921 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
923 if (ici
->ops
->get_parm
)
924 return ici
->ops
->get_parm(icd
, a
);
929 static int soc_camera_s_parm(struct file
*file
, void *fh
,
930 struct v4l2_streamparm
*a
)
932 struct soc_camera_device
*icd
= file
->private_data
;
933 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
935 if (ici
->ops
->set_parm
)
936 return ici
->ops
->set_parm(icd
, a
);
941 static int soc_camera_g_chip_ident(struct file
*file
, void *fh
,
942 struct v4l2_dbg_chip_ident
*id
)
944 struct soc_camera_device
*icd
= file
->private_data
;
945 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
947 return v4l2_subdev_call(sd
, core
, g_chip_ident
, id
);
950 #ifdef CONFIG_VIDEO_ADV_DEBUG
951 static int soc_camera_g_register(struct file
*file
, void *fh
,
952 struct v4l2_dbg_register
*reg
)
954 struct soc_camera_device
*icd
= file
->private_data
;
955 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
957 return v4l2_subdev_call(sd
, core
, g_register
, reg
);
960 static int soc_camera_s_register(struct file
*file
, void *fh
,
961 struct v4l2_dbg_register
*reg
)
963 struct soc_camera_device
*icd
= file
->private_data
;
964 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
966 return v4l2_subdev_call(sd
, core
, s_register
, reg
);
970 static int soc_camera_probe(struct soc_camera_device
*icd
);
972 /* So far this function cannot fail */
973 static void scan_add_host(struct soc_camera_host
*ici
)
975 struct soc_camera_device
*icd
;
977 mutex_lock(&list_lock
);
979 list_for_each_entry(icd
, &devices
, list
) {
980 if (icd
->iface
== ici
->nr
) {
983 icd
->parent
= ici
->v4l2_dev
.dev
;
984 ret
= soc_camera_probe(icd
);
988 mutex_unlock(&list_lock
);
991 #ifdef CONFIG_I2C_BOARDINFO
992 static int soc_camera_init_i2c(struct soc_camera_device
*icd
,
993 struct soc_camera_link
*icl
)
995 struct i2c_client
*client
;
996 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
997 struct i2c_adapter
*adap
= i2c_get_adapter(icl
->i2c_adapter_id
);
998 struct v4l2_subdev
*subdev
;
1001 dev_err(icd
->pdev
, "Cannot get I2C adapter #%d. No driver?\n",
1002 icl
->i2c_adapter_id
);
1006 icl
->board_info
->platform_data
= icd
;
1008 subdev
= v4l2_i2c_new_subdev_board(&ici
->v4l2_dev
, adap
,
1009 icl
->board_info
, NULL
);
1013 client
= v4l2_get_subdevdata(subdev
);
1015 /* Use to_i2c_client(dev) to recover the i2c client */
1016 icd
->control
= &client
->dev
;
1020 i2c_put_adapter(adap
);
1025 static void soc_camera_free_i2c(struct soc_camera_device
*icd
)
1027 struct i2c_client
*client
=
1028 to_i2c_client(to_soc_camera_control(icd
));
1029 struct i2c_adapter
*adap
= client
->adapter
;
1031 icd
->control
= NULL
;
1032 v4l2_device_unregister_subdev(i2c_get_clientdata(client
));
1033 i2c_unregister_device(client
);
1034 i2c_put_adapter(adap
);
1037 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
1038 #define soc_camera_free_i2c(icd) do {} while (0)
1041 static int soc_camera_video_start(struct soc_camera_device
*icd
);
1042 static int video_dev_create(struct soc_camera_device
*icd
);
1043 /* Called during host-driver probe */
1044 static int soc_camera_probe(struct soc_camera_device
*icd
)
1046 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1047 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
1048 struct device
*control
= NULL
;
1049 struct v4l2_subdev
*sd
;
1050 struct v4l2_mbus_framefmt mf
;
1053 dev_info(icd
->pdev
, "Probing %s\n", dev_name(icd
->pdev
));
1055 ret
= regulator_bulk_get(icd
->pdev
, icl
->num_regulators
,
1060 ret
= soc_camera_power_set(icd
, icl
, 1);
1064 /* The camera could have been already on, try to reset */
1066 icl
->reset(icd
->pdev
);
1068 ret
= ici
->ops
->add(icd
);
1072 /* Must have icd->vdev before registering the device */
1073 ret
= video_dev_create(icd
);
1077 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1078 if (icl
->board_info
) {
1079 ret
= soc_camera_init_i2c(icd
, icl
);
1082 } else if (!icl
->add_device
|| !icl
->del_device
) {
1086 if (icl
->module_name
)
1087 ret
= request_module(icl
->module_name
);
1089 ret
= icl
->add_device(icd
);
1094 * FIXME: this is racy, have to use driver-binding notification,
1095 * when it is available
1097 control
= to_soc_camera_control(icd
);
1098 if (!control
|| !control
->driver
|| !dev_get_drvdata(control
) ||
1099 !try_module_get(control
->driver
->owner
)) {
1100 icl
->del_device(icd
);
1105 sd
= soc_camera_to_subdev(icd
);
1106 sd
->grp_id
= (long)icd
;
1108 /* At this point client .probe() should have run already */
1109 ret
= soc_camera_init_user_formats(icd
);
1113 icd
->field
= V4L2_FIELD_ANY
;
1116 * ..._video_start() will create a device node, video_register_device()
1117 * itself is protected against concurrent open() calls, but we also have
1118 * to protect our data.
1120 mutex_lock(&icd
->video_lock
);
1122 ret
= soc_camera_video_start(icd
);
1126 /* Try to improve our guess of a reasonable window format */
1127 if (!v4l2_subdev_call(sd
, video
, g_mbus_fmt
, &mf
)) {
1128 icd
->user_width
= mf
.width
;
1129 icd
->user_height
= mf
.height
;
1130 icd
->colorspace
= mf
.colorspace
;
1131 icd
->field
= mf
.field
;
1134 ici
->ops
->remove(icd
);
1136 soc_camera_power_set(icd
, icl
, 0);
1138 mutex_unlock(&icd
->video_lock
);
1143 mutex_unlock(&icd
->video_lock
);
1144 soc_camera_free_user_formats(icd
);
1146 if (icl
->board_info
) {
1147 soc_camera_free_i2c(icd
);
1149 icl
->del_device(icd
);
1150 module_put(control
->driver
->owner
);
1154 video_device_release(icd
->vdev
);
1156 ici
->ops
->remove(icd
);
1158 soc_camera_power_set(icd
, icl
, 0);
1160 regulator_bulk_free(icl
->num_regulators
, icl
->regulators
);
1166 * This is called on device_unregister, which only means we have to disconnect
1167 * from the host, but not remove ourselves from the device list
1169 static int soc_camera_remove(struct soc_camera_device
*icd
)
1171 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
1172 struct video_device
*vdev
= icd
->vdev
;
1174 BUG_ON(!icd
->parent
);
1177 video_unregister_device(vdev
);
1181 if (icl
->board_info
) {
1182 soc_camera_free_i2c(icd
);
1184 struct device_driver
*drv
= to_soc_camera_control(icd
)->driver
;
1186 icl
->del_device(icd
);
1187 module_put(drv
->owner
);
1190 soc_camera_free_user_formats(icd
);
1192 regulator_bulk_free(icl
->num_regulators
, icl
->regulators
);
1197 static int default_cropcap(struct soc_camera_device
*icd
,
1198 struct v4l2_cropcap
*a
)
1200 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1201 return v4l2_subdev_call(sd
, video
, cropcap
, a
);
1204 static int default_g_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*a
)
1206 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1207 return v4l2_subdev_call(sd
, video
, g_crop
, a
);
1210 static int default_s_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*a
)
1212 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1213 return v4l2_subdev_call(sd
, video
, s_crop
, a
);
1216 static int default_g_parm(struct soc_camera_device
*icd
,
1217 struct v4l2_streamparm
*parm
)
1219 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1220 return v4l2_subdev_call(sd
, video
, g_parm
, parm
);
1223 static int default_s_parm(struct soc_camera_device
*icd
,
1224 struct v4l2_streamparm
*parm
)
1226 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1227 return v4l2_subdev_call(sd
, video
, s_parm
, parm
);
1230 static int default_enum_fsizes(struct soc_camera_device
*icd
,
1231 struct v4l2_frmsizeenum
*fsize
)
1234 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1235 const struct soc_camera_format_xlate
*xlate
;
1236 __u32 pixfmt
= fsize
->pixel_format
;
1237 struct v4l2_frmsizeenum fsize_mbus
= *fsize
;
1239 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1242 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1243 fsize_mbus
.pixel_format
= xlate
->code
;
1245 ret
= v4l2_subdev_call(sd
, video
, enum_mbus_fsizes
, &fsize_mbus
);
1249 *fsize
= fsize_mbus
;
1250 fsize
->pixel_format
= pixfmt
;
1255 int soc_camera_host_register(struct soc_camera_host
*ici
)
1257 struct soc_camera_host
*ix
;
1260 if (!ici
|| !ici
->ops
||
1261 !ici
->ops
->try_fmt
||
1262 !ici
->ops
->set_fmt
||
1263 !ici
->ops
->set_bus_param
||
1264 !ici
->ops
->querycap
||
1265 ((!ici
->ops
->init_videobuf
||
1266 !ici
->ops
->reqbufs
) &&
1267 !ici
->ops
->init_videobuf2
) ||
1269 !ici
->ops
->remove
||
1274 if (!ici
->ops
->set_crop
)
1275 ici
->ops
->set_crop
= default_s_crop
;
1276 if (!ici
->ops
->get_crop
)
1277 ici
->ops
->get_crop
= default_g_crop
;
1278 if (!ici
->ops
->cropcap
)
1279 ici
->ops
->cropcap
= default_cropcap
;
1280 if (!ici
->ops
->set_parm
)
1281 ici
->ops
->set_parm
= default_s_parm
;
1282 if (!ici
->ops
->get_parm
)
1283 ici
->ops
->get_parm
= default_g_parm
;
1284 if (!ici
->ops
->enum_fsizes
)
1285 ici
->ops
->enum_fsizes
= default_enum_fsizes
;
1287 mutex_lock(&list_lock
);
1288 list_for_each_entry(ix
, &hosts
, list
) {
1289 if (ix
->nr
== ici
->nr
) {
1295 ret
= v4l2_device_register(ici
->v4l2_dev
.dev
, &ici
->v4l2_dev
);
1299 list_add_tail(&ici
->list
, &hosts
);
1300 mutex_unlock(&list_lock
);
1307 mutex_unlock(&list_lock
);
1310 EXPORT_SYMBOL(soc_camera_host_register
);
1312 /* Unregister all clients! */
1313 void soc_camera_host_unregister(struct soc_camera_host
*ici
)
1315 struct soc_camera_device
*icd
;
1317 mutex_lock(&list_lock
);
1319 list_del(&ici
->list
);
1320 list_for_each_entry(icd
, &devices
, list
)
1321 if (icd
->iface
== ici
->nr
&& to_soc_camera_control(icd
))
1322 soc_camera_remove(icd
);
1324 mutex_unlock(&list_lock
);
1326 v4l2_device_unregister(&ici
->v4l2_dev
);
1328 EXPORT_SYMBOL(soc_camera_host_unregister
);
1330 /* Image capture device */
1331 static int soc_camera_device_register(struct soc_camera_device
*icd
)
1333 struct soc_camera_device
*ix
;
1336 for (i
= 0; i
< 256 && num
< 0; i
++) {
1338 /* Check if this index is available on this interface */
1339 list_for_each_entry(ix
, &devices
, list
) {
1340 if (ix
->iface
== icd
->iface
&& ix
->devnum
== i
) {
1349 * ok, we have 256 cameras on this host...
1350 * man, stay reasonable...
1356 icd
->host_priv
= NULL
;
1357 mutex_init(&icd
->video_lock
);
1359 list_add_tail(&icd
->list
, &devices
);
1364 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops
= {
1365 .vidioc_querycap
= soc_camera_querycap
,
1366 .vidioc_g_fmt_vid_cap
= soc_camera_g_fmt_vid_cap
,
1367 .vidioc_enum_fmt_vid_cap
= soc_camera_enum_fmt_vid_cap
,
1368 .vidioc_s_fmt_vid_cap
= soc_camera_s_fmt_vid_cap
,
1369 .vidioc_enum_input
= soc_camera_enum_input
,
1370 .vidioc_g_input
= soc_camera_g_input
,
1371 .vidioc_s_input
= soc_camera_s_input
,
1372 .vidioc_s_std
= soc_camera_s_std
,
1373 .vidioc_enum_framesizes
= soc_camera_enum_fsizes
,
1374 .vidioc_reqbufs
= soc_camera_reqbufs
,
1375 .vidioc_try_fmt_vid_cap
= soc_camera_try_fmt_vid_cap
,
1376 .vidioc_querybuf
= soc_camera_querybuf
,
1377 .vidioc_qbuf
= soc_camera_qbuf
,
1378 .vidioc_dqbuf
= soc_camera_dqbuf
,
1379 .vidioc_streamon
= soc_camera_streamon
,
1380 .vidioc_streamoff
= soc_camera_streamoff
,
1381 .vidioc_queryctrl
= soc_camera_queryctrl
,
1382 .vidioc_g_ctrl
= soc_camera_g_ctrl
,
1383 .vidioc_s_ctrl
= soc_camera_s_ctrl
,
1384 .vidioc_cropcap
= soc_camera_cropcap
,
1385 .vidioc_g_crop
= soc_camera_g_crop
,
1386 .vidioc_s_crop
= soc_camera_s_crop
,
1387 .vidioc_g_parm
= soc_camera_g_parm
,
1388 .vidioc_s_parm
= soc_camera_s_parm
,
1389 .vidioc_g_chip_ident
= soc_camera_g_chip_ident
,
1390 #ifdef CONFIG_VIDEO_ADV_DEBUG
1391 .vidioc_g_register
= soc_camera_g_register
,
1392 .vidioc_s_register
= soc_camera_s_register
,
1396 static int video_dev_create(struct soc_camera_device
*icd
)
1398 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1399 struct video_device
*vdev
= video_device_alloc();
1404 strlcpy(vdev
->name
, ici
->drv_name
, sizeof(vdev
->name
));
1406 vdev
->parent
= icd
->pdev
;
1407 vdev
->current_norm
= V4L2_STD_UNKNOWN
;
1408 vdev
->fops
= &soc_camera_fops
;
1409 vdev
->ioctl_ops
= &soc_camera_ioctl_ops
;
1410 vdev
->release
= video_device_release
;
1411 vdev
->tvnorms
= V4L2_STD_UNKNOWN
;
1412 vdev
->lock
= &icd
->video_lock
;
1420 * Called from soc_camera_probe() above (with .video_lock held???)
1422 static int soc_camera_video_start(struct soc_camera_device
*icd
)
1424 const struct device_type
*type
= icd
->vdev
->dev
.type
;
1431 !icd
->ops
->query_bus_param
||
1432 !icd
->ops
->set_bus_param
)
1435 ret
= video_register_device(icd
->vdev
, VFL_TYPE_GRABBER
, -1);
1437 dev_err(icd
->pdev
, "video_register_device failed: %d\n", ret
);
1441 /* Restore device type, possibly set by the subdevice driver */
1442 icd
->vdev
->dev
.type
= type
;
1447 static int __devinit
soc_camera_pdrv_probe(struct platform_device
*pdev
)
1449 struct soc_camera_link
*icl
= pdev
->dev
.platform_data
;
1450 struct soc_camera_device
*icd
;
1456 icd
= kzalloc(sizeof(*icd
), GFP_KERNEL
);
1460 icd
->iface
= icl
->bus_id
;
1462 icd
->pdev
= &pdev
->dev
;
1463 platform_set_drvdata(pdev
, icd
);
1465 ret
= soc_camera_device_register(icd
);
1469 icd
->user_width
= DEFAULT_WIDTH
;
1470 icd
->user_height
= DEFAULT_HEIGHT
;
1481 * Only called on rmmod for each platform device, since they are not
1482 * hot-pluggable. Now we know, that all our users - hosts and devices have
1483 * been unloaded already
1485 static int __devexit
soc_camera_pdrv_remove(struct platform_device
*pdev
)
1487 struct soc_camera_device
*icd
= platform_get_drvdata(pdev
);
1492 list_del(&icd
->list
);
1499 static struct platform_driver __refdata soc_camera_pdrv
= {
1500 .remove
= __devexit_p(soc_camera_pdrv_remove
),
1502 .name
= "soc-camera-pdrv",
1503 .owner
= THIS_MODULE
,
1507 static int __init
soc_camera_init(void)
1509 return platform_driver_probe(&soc_camera_pdrv
, soc_camera_pdrv_probe
);
1512 static void __exit
soc_camera_exit(void)
1514 platform_driver_unregister(&soc_camera_pdrv
);
1517 module_init(soc_camera_init
);
1518 module_exit(soc_camera_exit
);
1520 MODULE_DESCRIPTION("Image capture bus driver");
1521 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1522 MODULE_LICENSE("GPL");
1523 MODULE_ALIAS("platform:soc-camera-pdrv");