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_device
*icd
= file
->private_data
;
96 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
98 WARN_ON(priv
!= file
->private_data
);
100 /* limit format to hardware capabilities */
101 return ici
->ops
->try_fmt(icd
, f
);
104 static int soc_camera_enum_input(struct file
*file
, void *priv
,
105 struct v4l2_input
*inp
)
107 struct soc_camera_device
*icd
= file
->private_data
;
113 if (icd
->ops
->enum_input
)
114 ret
= icd
->ops
->enum_input(icd
, inp
);
116 /* default is camera */
117 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
118 inp
->std
= V4L2_STD_UNKNOWN
;
119 strcpy(inp
->name
, "Camera");
125 static int soc_camera_g_input(struct file
*file
, void *priv
, unsigned int *i
)
132 static int soc_camera_s_input(struct file
*file
, void *priv
, unsigned int i
)
140 static int soc_camera_s_std(struct file
*file
, void *priv
, v4l2_std_id
*a
)
142 struct soc_camera_device
*icd
= file
->private_data
;
143 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
145 return v4l2_subdev_call(sd
, core
, s_std
, *a
);
148 static int soc_camera_reqbufs(struct file
*file
, void *priv
,
149 struct v4l2_requestbuffers
*p
)
152 struct soc_camera_device
*icd
= file
->private_data
;
153 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
155 WARN_ON(priv
!= file
->private_data
);
157 if (icd
->streamer
&& icd
->streamer
!= file
)
160 ret
= videobuf_reqbufs(&icd
->vb_vidq
, p
);
164 ret
= ici
->ops
->reqbufs(icd
, p
);
165 if (!ret
&& !icd
->streamer
)
166 icd
->streamer
= file
;
171 static int soc_camera_querybuf(struct file
*file
, void *priv
,
172 struct v4l2_buffer
*p
)
174 struct soc_camera_device
*icd
= file
->private_data
;
176 WARN_ON(priv
!= file
->private_data
);
178 return videobuf_querybuf(&icd
->vb_vidq
, p
);
181 static int soc_camera_qbuf(struct file
*file
, void *priv
,
182 struct v4l2_buffer
*p
)
184 struct soc_camera_device
*icd
= file
->private_data
;
186 WARN_ON(priv
!= file
->private_data
);
188 if (icd
->streamer
!= file
)
191 return videobuf_qbuf(&icd
->vb_vidq
, p
);
194 static int soc_camera_dqbuf(struct file
*file
, void *priv
,
195 struct v4l2_buffer
*p
)
197 struct soc_camera_device
*icd
= file
->private_data
;
199 WARN_ON(priv
!= file
->private_data
);
201 if (icd
->streamer
!= file
)
204 return videobuf_dqbuf(&icd
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
207 /* Always entered with .video_lock held */
208 static int soc_camera_init_user_formats(struct soc_camera_device
*icd
)
210 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
211 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
212 unsigned int i
, fmts
= 0, raw_fmts
= 0;
214 enum v4l2_mbus_pixelcode code
;
216 while (!v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, raw_fmts
, &code
))
219 if (!ici
->ops
->get_formats
)
221 * Fallback mode - the host will have to serve all
222 * sensor-provided formats one-to-one to the user
227 * First pass - only count formats this host-sensor
228 * configuration can provide
230 for (i
= 0; i
< raw_fmts
; i
++) {
231 ret
= ici
->ops
->get_formats(icd
, i
, NULL
);
241 vmalloc(fmts
* sizeof(struct soc_camera_format_xlate
));
242 if (!icd
->user_formats
)
245 icd
->num_user_formats
= fmts
;
247 dev_dbg(&icd
->dev
, "Found %d supported formats.\n", fmts
);
249 /* Second pass - actually fill data formats */
251 for (i
= 0; i
< raw_fmts
; i
++)
252 if (!ici
->ops
->get_formats
) {
253 v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, i
, &code
);
254 icd
->user_formats
[i
].host_fmt
=
255 soc_mbus_get_fmtdesc(code
);
256 icd
->user_formats
[i
].code
= code
;
258 ret
= ici
->ops
->get_formats(icd
, i
,
259 &icd
->user_formats
[fmts
]);
265 icd
->current_fmt
= &icd
->user_formats
[0];
270 icd
->num_user_formats
= 0;
271 vfree(icd
->user_formats
);
275 /* Always entered with .video_lock held */
276 static void soc_camera_free_user_formats(struct soc_camera_device
*icd
)
278 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
280 if (ici
->ops
->put_formats
)
281 ici
->ops
->put_formats(icd
);
282 icd
->current_fmt
= NULL
;
283 icd
->num_user_formats
= 0;
284 vfree(icd
->user_formats
);
285 icd
->user_formats
= NULL
;
288 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
291 /* Called with .vb_lock held, or from the first open(2), see comment there */
292 static int soc_camera_set_fmt(struct soc_camera_device
*icd
,
293 struct v4l2_format
*f
)
295 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
296 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
299 dev_dbg(&icd
->dev
, "S_FMT(%c%c%c%c, %ux%u)\n",
300 pixfmtstr(pix
->pixelformat
), pix
->width
, pix
->height
);
302 /* We always call try_fmt() before set_fmt() or set_crop() */
303 ret
= ici
->ops
->try_fmt(icd
, f
);
307 ret
= ici
->ops
->set_fmt(icd
, f
);
310 } else if (!icd
->current_fmt
||
311 icd
->current_fmt
->host_fmt
->fourcc
!= pix
->pixelformat
) {
313 "Host driver hasn't set up current format correctly!\n");
317 icd
->user_width
= pix
->width
;
318 icd
->user_height
= pix
->height
;
319 icd
->colorspace
= pix
->colorspace
;
321 icd
->field
= pix
->field
;
323 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
324 dev_warn(&icd
->dev
, "Attention! Wrong buf-type %d\n",
327 dev_dbg(&icd
->dev
, "set width: %d height: %d\n",
328 icd
->user_width
, icd
->user_height
);
330 /* set physical bus parameters */
331 return ici
->ops
->set_bus_param(icd
, pix
->pixelformat
);
334 static int soc_camera_open(struct file
*file
)
336 struct video_device
*vdev
= video_devdata(file
);
337 struct soc_camera_device
*icd
= container_of(vdev
->parent
,
338 struct soc_camera_device
,
340 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
341 struct soc_camera_host
*ici
;
345 /* No device driver attached */
348 ici
= to_soc_camera_host(icd
->dev
.parent
);
350 if (!try_module_get(ici
->ops
->owner
)) {
351 dev_err(&icd
->dev
, "Couldn't lock capture bus driver.\n");
356 * Protect against icd->ops->remove() until we module_get() both
359 mutex_lock(&icd
->video_lock
);
363 /* Now we really have to activate the camera */
364 if (icd
->use_count
== 1) {
365 /* Restore parameters before the last close() per V4L2 API */
366 struct v4l2_format f
= {
367 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
369 .width
= icd
->user_width
,
370 .height
= icd
->user_height
,
372 .colorspace
= icd
->colorspace
,
374 icd
->current_fmt
->host_fmt
->fourcc
,
379 ret
= icl
->power(icd
->pdev
, 1);
384 /* The camera could have been already on, try to reset */
386 icl
->reset(icd
->pdev
);
388 ret
= ici
->ops
->add(icd
);
390 dev_err(&icd
->dev
, "Couldn't activate the camera: %d\n", ret
);
394 pm_runtime_enable(&icd
->vdev
->dev
);
395 ret
= pm_runtime_resume(&icd
->vdev
->dev
);
396 if (ret
< 0 && ret
!= -ENOSYS
)
400 * Try to configure with default parameters. Notice: this is the
401 * very first open, so, we cannot race against other calls,
402 * apart from someone else calling open() simultaneously, but
403 * .video_lock is protecting us against it.
405 ret
= soc_camera_set_fmt(icd
, &f
);
410 file
->private_data
= icd
;
411 dev_dbg(&icd
->dev
, "camera device open\n");
413 ici
->ops
->init_videobuf(&icd
->vb_vidq
, icd
);
415 mutex_unlock(&icd
->video_lock
);
420 * First four errors are entered with the .video_lock held
424 pm_runtime_disable(&icd
->vdev
->dev
);
426 ici
->ops
->remove(icd
);
429 icl
->power(icd
->pdev
, 0);
432 mutex_unlock(&icd
->video_lock
);
433 module_put(ici
->ops
->owner
);
438 static int soc_camera_close(struct file
*file
)
440 struct soc_camera_device
*icd
= file
->private_data
;
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 if (icd
->streamer
== file
)
458 icd
->streamer
= NULL
;
460 mutex_unlock(&icd
->video_lock
);
462 module_put(ici
->ops
->owner
);
464 dev_dbg(&icd
->dev
, "camera device close\n");
469 static ssize_t
soc_camera_read(struct file
*file
, char __user
*buf
,
470 size_t count
, loff_t
*ppos
)
472 struct soc_camera_device
*icd
= file
->private_data
;
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_device
*icd
= file
->private_data
;
485 dev_dbg(&icd
->dev
, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
487 if (icd
->streamer
!= file
)
490 err
= videobuf_mmap_mapper(&icd
->vb_vidq
, vma
);
492 dev_dbg(&icd
->dev
, "vma start=0x%08lx, size=%ld, ret=%d\n",
493 (unsigned long)vma
->vm_start
,
494 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
500 static unsigned int soc_camera_poll(struct file
*file
, poll_table
*pt
)
502 struct soc_camera_device
*icd
= file
->private_data
;
503 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
505 if (icd
->streamer
!= file
)
508 if (list_empty(&icd
->vb_vidq
.stream
)) {
509 dev_err(&icd
->dev
, "Trying to poll with no queued buffers!\n");
513 return ici
->ops
->poll(file
, pt
);
516 static struct v4l2_file_operations soc_camera_fops
= {
517 .owner
= THIS_MODULE
,
518 .open
= soc_camera_open
,
519 .release
= soc_camera_close
,
520 .ioctl
= video_ioctl2
,
521 .read
= soc_camera_read
,
522 .mmap
= soc_camera_mmap
,
523 .poll
= soc_camera_poll
,
526 static int soc_camera_s_fmt_vid_cap(struct file
*file
, void *priv
,
527 struct v4l2_format
*f
)
529 struct soc_camera_device
*icd
= file
->private_data
;
532 WARN_ON(priv
!= file
->private_data
);
534 if (icd
->streamer
&& icd
->streamer
!= file
)
537 mutex_lock(&icd
->vb_vidq
.vb_lock
);
539 if (icd
->vb_vidq
.bufs
[0]) {
540 dev_err(&icd
->dev
, "S_FMT denied: queue initialised\n");
545 ret
= soc_camera_set_fmt(icd
, f
);
547 if (!ret
&& !icd
->streamer
)
548 icd
->streamer
= file
;
551 mutex_unlock(&icd
->vb_vidq
.vb_lock
);
556 static int soc_camera_enum_fmt_vid_cap(struct file
*file
, void *priv
,
557 struct v4l2_fmtdesc
*f
)
559 struct soc_camera_device
*icd
= file
->private_data
;
560 const struct soc_mbus_pixelfmt
*format
;
562 WARN_ON(priv
!= file
->private_data
);
564 if (f
->index
>= icd
->num_user_formats
)
567 format
= icd
->user_formats
[f
->index
].host_fmt
;
570 strlcpy(f
->description
, format
->name
, sizeof(f
->description
));
571 f
->pixelformat
= format
->fourcc
;
575 static int soc_camera_g_fmt_vid_cap(struct file
*file
, void *priv
,
576 struct v4l2_format
*f
)
578 struct soc_camera_device
*icd
= file
->private_data
;
579 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
581 WARN_ON(priv
!= file
->private_data
);
583 pix
->width
= icd
->user_width
;
584 pix
->height
= icd
->user_height
;
585 pix
->field
= icd
->vb_vidq
.field
;
586 pix
->pixelformat
= icd
->current_fmt
->host_fmt
->fourcc
;
587 pix
->bytesperline
= soc_mbus_bytes_per_line(pix
->width
,
588 icd
->current_fmt
->host_fmt
);
589 pix
->colorspace
= icd
->colorspace
;
590 if (pix
->bytesperline
< 0)
591 return pix
->bytesperline
;
592 pix
->sizeimage
= pix
->height
* pix
->bytesperline
;
593 dev_dbg(&icd
->dev
, "current_fmt->fourcc: 0x%08x\n",
594 icd
->current_fmt
->host_fmt
->fourcc
);
598 static int soc_camera_querycap(struct file
*file
, void *priv
,
599 struct v4l2_capability
*cap
)
601 struct soc_camera_device
*icd
= file
->private_data
;
602 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
604 WARN_ON(priv
!= file
->private_data
);
606 strlcpy(cap
->driver
, ici
->drv_name
, sizeof(cap
->driver
));
607 return ici
->ops
->querycap(ici
, cap
);
610 static int soc_camera_streamon(struct file
*file
, void *priv
,
611 enum v4l2_buf_type i
)
613 struct soc_camera_device
*icd
= file
->private_data
;
614 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
617 WARN_ON(priv
!= file
->private_data
);
619 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
622 if (icd
->streamer
!= file
)
625 mutex_lock(&icd
->video_lock
);
627 v4l2_subdev_call(sd
, video
, s_stream
, 1);
629 /* This calls buf_queue from host driver's videobuf_queue_ops */
630 ret
= videobuf_streamon(&icd
->vb_vidq
);
632 mutex_unlock(&icd
->video_lock
);
637 static int soc_camera_streamoff(struct file
*file
, void *priv
,
638 enum v4l2_buf_type i
)
640 struct soc_camera_device
*icd
= file
->private_data
;
641 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
643 WARN_ON(priv
!= file
->private_data
);
645 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
648 if (icd
->streamer
!= file
)
651 mutex_lock(&icd
->video_lock
);
654 * This calls buf_release from host driver's videobuf_queue_ops for all
655 * remaining buffers. When the last buffer is freed, stop capture
657 videobuf_streamoff(&icd
->vb_vidq
);
659 v4l2_subdev_call(sd
, video
, s_stream
, 0);
661 mutex_unlock(&icd
->video_lock
);
666 static int soc_camera_queryctrl(struct file
*file
, void *priv
,
667 struct v4l2_queryctrl
*qc
)
669 struct soc_camera_device
*icd
= file
->private_data
;
670 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
673 WARN_ON(priv
!= file
->private_data
);
678 /* First check host controls */
679 for (i
= 0; i
< ici
->ops
->num_controls
; i
++)
680 if (qc
->id
== ici
->ops
->controls
[i
].id
) {
681 memcpy(qc
, &(ici
->ops
->controls
[i
]),
686 /* Then device controls */
687 for (i
= 0; i
< icd
->ops
->num_controls
; i
++)
688 if (qc
->id
== icd
->ops
->controls
[i
].id
) {
689 memcpy(qc
, &(icd
->ops
->controls
[i
]),
697 static int soc_camera_g_ctrl(struct file
*file
, void *priv
,
698 struct v4l2_control
*ctrl
)
700 struct soc_camera_device
*icd
= file
->private_data
;
701 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
702 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
705 WARN_ON(priv
!= file
->private_data
);
707 if (ici
->ops
->get_ctrl
) {
708 ret
= ici
->ops
->get_ctrl(icd
, ctrl
);
709 if (ret
!= -ENOIOCTLCMD
)
713 return v4l2_subdev_call(sd
, core
, g_ctrl
, ctrl
);
716 static int soc_camera_s_ctrl(struct file
*file
, void *priv
,
717 struct v4l2_control
*ctrl
)
719 struct soc_camera_device
*icd
= file
->private_data
;
720 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
721 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
724 WARN_ON(priv
!= file
->private_data
);
726 if (ici
->ops
->set_ctrl
) {
727 ret
= ici
->ops
->set_ctrl(icd
, ctrl
);
728 if (ret
!= -ENOIOCTLCMD
)
732 return v4l2_subdev_call(sd
, core
, s_ctrl
, ctrl
);
735 static int soc_camera_cropcap(struct file
*file
, void *fh
,
736 struct v4l2_cropcap
*a
)
738 struct soc_camera_device
*icd
= file
->private_data
;
739 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
741 return ici
->ops
->cropcap(icd
, a
);
744 static int soc_camera_g_crop(struct file
*file
, void *fh
,
747 struct soc_camera_device
*icd
= file
->private_data
;
748 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
751 mutex_lock(&icd
->vb_vidq
.vb_lock
);
752 ret
= ici
->ops
->get_crop(icd
, a
);
753 mutex_unlock(&icd
->vb_vidq
.vb_lock
);
759 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
760 * argument with the actual geometry, instead, the user shall use G_CROP to
763 static int soc_camera_s_crop(struct file
*file
, void *fh
,
766 struct soc_camera_device
*icd
= file
->private_data
;
767 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
768 struct v4l2_rect
*rect
= &a
->c
;
769 struct v4l2_crop current_crop
;
772 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
775 dev_dbg(&icd
->dev
, "S_CROP(%ux%u@%u:%u)\n",
776 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
778 /* Cropping is allowed during a running capture, guard consistency */
779 mutex_lock(&icd
->vb_vidq
.vb_lock
);
781 /* If get_crop fails, we'll let host and / or client drivers decide */
782 ret
= ici
->ops
->get_crop(icd
, ¤t_crop
);
784 /* Prohibit window size change with initialised buffers */
787 "S_CROP denied: getting current crop failed\n");
788 } else if (icd
->vb_vidq
.bufs
[0] &&
789 (a
->c
.width
!= current_crop
.c
.width
||
790 a
->c
.height
!= current_crop
.c
.height
)) {
792 "S_CROP denied: queue initialised and sizes differ\n");
795 ret
= ici
->ops
->set_crop(icd
, a
);
798 mutex_unlock(&icd
->vb_vidq
.vb_lock
);
803 static int soc_camera_g_parm(struct file
*file
, void *fh
,
804 struct v4l2_streamparm
*a
)
806 struct soc_camera_device
*icd
= file
->private_data
;
807 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
809 if (ici
->ops
->get_parm
)
810 return ici
->ops
->get_parm(icd
, a
);
815 static int soc_camera_s_parm(struct file
*file
, void *fh
,
816 struct v4l2_streamparm
*a
)
818 struct soc_camera_device
*icd
= file
->private_data
;
819 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
821 if (ici
->ops
->set_parm
)
822 return ici
->ops
->set_parm(icd
, a
);
827 static int soc_camera_g_chip_ident(struct file
*file
, void *fh
,
828 struct v4l2_dbg_chip_ident
*id
)
830 struct soc_camera_device
*icd
= file
->private_data
;
831 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
833 return v4l2_subdev_call(sd
, core
, g_chip_ident
, id
);
836 #ifdef CONFIG_VIDEO_ADV_DEBUG
837 static int soc_camera_g_register(struct file
*file
, void *fh
,
838 struct v4l2_dbg_register
*reg
)
840 struct soc_camera_device
*icd
= file
->private_data
;
841 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
843 return v4l2_subdev_call(sd
, core
, g_register
, reg
);
846 static int soc_camera_s_register(struct file
*file
, void *fh
,
847 struct v4l2_dbg_register
*reg
)
849 struct soc_camera_device
*icd
= file
->private_data
;
850 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
852 return v4l2_subdev_call(sd
, core
, s_register
, reg
);
856 /* So far this function cannot fail */
857 static void scan_add_host(struct soc_camera_host
*ici
)
859 struct soc_camera_device
*icd
;
861 mutex_lock(&list_lock
);
863 list_for_each_entry(icd
, &devices
, list
) {
864 if (icd
->iface
== ici
->nr
) {
866 icd
->dev
.parent
= ici
->v4l2_dev
.dev
;
867 dev_set_name(&icd
->dev
, "%u-%u", icd
->iface
,
869 ret
= device_register(&icd
->dev
);
871 icd
->dev
.parent
= NULL
;
873 "Cannot register device: %d\n", ret
);
878 mutex_unlock(&list_lock
);
881 #ifdef CONFIG_I2C_BOARDINFO
882 static int soc_camera_init_i2c(struct soc_camera_device
*icd
,
883 struct soc_camera_link
*icl
)
885 struct i2c_client
*client
;
886 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
887 struct i2c_adapter
*adap
= i2c_get_adapter(icl
->i2c_adapter_id
);
888 struct v4l2_subdev
*subdev
;
891 dev_err(&icd
->dev
, "Cannot get I2C adapter #%d. No driver?\n",
892 icl
->i2c_adapter_id
);
896 icl
->board_info
->platform_data
= icd
;
898 subdev
= v4l2_i2c_new_subdev_board(&ici
->v4l2_dev
, adap
,
899 NULL
, icl
->board_info
, NULL
);
903 client
= v4l2_get_subdevdata(subdev
);
905 /* Use to_i2c_client(dev) to recover the i2c client */
906 dev_set_drvdata(&icd
->dev
, &client
->dev
);
910 i2c_put_adapter(adap
);
915 static void soc_camera_free_i2c(struct soc_camera_device
*icd
)
917 struct i2c_client
*client
=
918 to_i2c_client(to_soc_camera_control(icd
));
919 dev_set_drvdata(&icd
->dev
, NULL
);
920 v4l2_device_unregister_subdev(i2c_get_clientdata(client
));
921 i2c_unregister_device(client
);
922 i2c_put_adapter(client
->adapter
);
925 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
926 #define soc_camera_free_i2c(icd) do {} while (0)
929 static int soc_camera_video_start(struct soc_camera_device
*icd
);
930 static int video_dev_create(struct soc_camera_device
*icd
);
931 /* Called during host-driver probe */
932 static int soc_camera_probe(struct device
*dev
)
934 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
935 struct soc_camera_host
*ici
= to_soc_camera_host(dev
->parent
);
936 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
937 struct device
*control
= NULL
;
938 struct v4l2_subdev
*sd
;
939 struct v4l2_mbus_framefmt mf
;
942 dev_info(dev
, "Probing %s\n", dev_name(dev
));
945 ret
= icl
->power(icd
->pdev
, 1);
948 "Platform failed to power-on the camera.\n");
953 /* The camera could have been already on, try to reset */
955 icl
->reset(icd
->pdev
);
957 ret
= ici
->ops
->add(icd
);
961 /* Must have icd->vdev before registering the device */
962 ret
= video_dev_create(icd
);
966 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
967 if (icl
->board_info
) {
968 ret
= soc_camera_init_i2c(icd
, icl
);
971 } else if (!icl
->add_device
|| !icl
->del_device
) {
975 if (icl
->module_name
)
976 ret
= request_module(icl
->module_name
);
978 ret
= icl
->add_device(icl
, &icd
->dev
);
983 * FIXME: this is racy, have to use driver-binding notification,
984 * when it is available
986 control
= to_soc_camera_control(icd
);
987 if (!control
|| !control
->driver
|| !dev_get_drvdata(control
) ||
988 !try_module_get(control
->driver
->owner
)) {
989 icl
->del_device(icl
);
994 /* At this point client .probe() should have run already */
995 ret
= soc_camera_init_user_formats(icd
);
999 icd
->field
= V4L2_FIELD_ANY
;
1001 /* ..._video_start() will create a device node, so we have to protect */
1002 mutex_lock(&icd
->video_lock
);
1004 ret
= soc_camera_video_start(icd
);
1008 /* Try to improve our guess of a reasonable window format */
1009 sd
= soc_camera_to_subdev(icd
);
1010 if (!v4l2_subdev_call(sd
, video
, g_mbus_fmt
, &mf
)) {
1011 icd
->user_width
= mf
.width
;
1012 icd
->user_height
= mf
.height
;
1013 icd
->colorspace
= mf
.colorspace
;
1014 icd
->field
= mf
.field
;
1017 /* Do we have to sysfs_remove_link() before device_unregister()? */
1018 if (sysfs_create_link(&icd
->dev
.kobj
, &to_soc_camera_control(icd
)->kobj
,
1020 dev_warn(&icd
->dev
, "Failed creating the control symlink\n");
1022 ici
->ops
->remove(icd
);
1025 icl
->power(icd
->pdev
, 0);
1027 mutex_unlock(&icd
->video_lock
);
1032 mutex_unlock(&icd
->video_lock
);
1033 soc_camera_free_user_formats(icd
);
1035 if (icl
->board_info
) {
1036 soc_camera_free_i2c(icd
);
1038 icl
->del_device(icl
);
1039 module_put(control
->driver
->owner
);
1043 video_device_release(icd
->vdev
);
1045 ici
->ops
->remove(icd
);
1048 icl
->power(icd
->pdev
, 0);
1054 * This is called on device_unregister, which only means we have to disconnect
1055 * from the host, but not remove ourselves from the device list
1057 static int soc_camera_remove(struct device
*dev
)
1059 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
1060 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
1061 struct video_device
*vdev
= icd
->vdev
;
1063 BUG_ON(!dev
->parent
);
1066 mutex_lock(&icd
->video_lock
);
1067 video_unregister_device(vdev
);
1069 mutex_unlock(&icd
->video_lock
);
1072 if (icl
->board_info
) {
1073 soc_camera_free_i2c(icd
);
1075 struct device_driver
*drv
= to_soc_camera_control(icd
) ?
1076 to_soc_camera_control(icd
)->driver
: NULL
;
1078 icl
->del_device(icl
);
1079 module_put(drv
->owner
);
1082 soc_camera_free_user_formats(icd
);
1087 static int soc_camera_suspend(struct device
*dev
, pm_message_t state
)
1089 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
1090 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1093 if (ici
->ops
->suspend
)
1094 ret
= ici
->ops
->suspend(icd
, state
);
1099 static int soc_camera_resume(struct device
*dev
)
1101 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
1102 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1105 if (ici
->ops
->resume
)
1106 ret
= ici
->ops
->resume(icd
);
1111 struct bus_type soc_camera_bus_type
= {
1112 .name
= "soc-camera",
1113 .probe
= soc_camera_probe
,
1114 .remove
= soc_camera_remove
,
1115 .suspend
= soc_camera_suspend
,
1116 .resume
= soc_camera_resume
,
1118 EXPORT_SYMBOL_GPL(soc_camera_bus_type
);
1120 static struct device_driver ic_drv
= {
1122 .bus
= &soc_camera_bus_type
,
1123 .owner
= THIS_MODULE
,
1126 static void dummy_release(struct device
*dev
)
1130 static int default_cropcap(struct soc_camera_device
*icd
,
1131 struct v4l2_cropcap
*a
)
1133 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1134 return v4l2_subdev_call(sd
, video
, cropcap
, a
);
1137 static int default_g_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*a
)
1139 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1140 return v4l2_subdev_call(sd
, video
, g_crop
, a
);
1143 static int default_s_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*a
)
1145 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1146 return v4l2_subdev_call(sd
, video
, s_crop
, a
);
1149 static int default_g_parm(struct soc_camera_device
*icd
,
1150 struct v4l2_streamparm
*parm
)
1152 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1153 return v4l2_subdev_call(sd
, video
, g_parm
, parm
);
1156 static int default_s_parm(struct soc_camera_device
*icd
,
1157 struct v4l2_streamparm
*parm
)
1159 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1160 return v4l2_subdev_call(sd
, video
, s_parm
, parm
);
1163 static void soc_camera_device_init(struct device
*dev
, void *pdata
)
1165 dev
->platform_data
= pdata
;
1166 dev
->bus
= &soc_camera_bus_type
;
1167 dev
->release
= dummy_release
;
1170 int soc_camera_host_register(struct soc_camera_host
*ici
)
1172 struct soc_camera_host
*ix
;
1175 if (!ici
|| !ici
->ops
||
1176 !ici
->ops
->try_fmt
||
1177 !ici
->ops
->set_fmt
||
1178 !ici
->ops
->set_bus_param
||
1179 !ici
->ops
->querycap
||
1180 !ici
->ops
->init_videobuf
||
1181 !ici
->ops
->reqbufs
||
1183 !ici
->ops
->remove
||
1188 if (!ici
->ops
->set_crop
)
1189 ici
->ops
->set_crop
= default_s_crop
;
1190 if (!ici
->ops
->get_crop
)
1191 ici
->ops
->get_crop
= default_g_crop
;
1192 if (!ici
->ops
->cropcap
)
1193 ici
->ops
->cropcap
= default_cropcap
;
1194 if (!ici
->ops
->set_parm
)
1195 ici
->ops
->set_parm
= default_s_parm
;
1196 if (!ici
->ops
->get_parm
)
1197 ici
->ops
->get_parm
= default_g_parm
;
1199 mutex_lock(&list_lock
);
1200 list_for_each_entry(ix
, &hosts
, list
) {
1201 if (ix
->nr
== ici
->nr
) {
1207 ret
= v4l2_device_register(ici
->v4l2_dev
.dev
, &ici
->v4l2_dev
);
1211 list_add_tail(&ici
->list
, &hosts
);
1212 mutex_unlock(&list_lock
);
1219 mutex_unlock(&list_lock
);
1222 EXPORT_SYMBOL(soc_camera_host_register
);
1224 /* Unregister all clients! */
1225 void soc_camera_host_unregister(struct soc_camera_host
*ici
)
1227 struct soc_camera_device
*icd
;
1229 mutex_lock(&list_lock
);
1231 list_del(&ici
->list
);
1233 list_for_each_entry(icd
, &devices
, list
) {
1234 if (icd
->iface
== ici
->nr
) {
1235 void *pdata
= icd
->dev
.platform_data
;
1236 /* The bus->remove will be called */
1237 device_unregister(&icd
->dev
);
1239 * Not before device_unregister(), .remove
1240 * needs parent to call ici->ops->remove().
1241 * If the host module is loaded again, device_register()
1242 * would complain "already initialised," since 2.6.32
1243 * this is also needed to prevent use-after-free of the
1244 * device private data.
1246 memset(&icd
->dev
, 0, sizeof(icd
->dev
));
1247 soc_camera_device_init(&icd
->dev
, pdata
);
1251 mutex_unlock(&list_lock
);
1253 v4l2_device_unregister(&ici
->v4l2_dev
);
1255 EXPORT_SYMBOL(soc_camera_host_unregister
);
1257 /* Image capture device */
1258 static int soc_camera_device_register(struct soc_camera_device
*icd
)
1260 struct soc_camera_device
*ix
;
1263 for (i
= 0; i
< 256 && num
< 0; i
++) {
1265 /* Check if this index is available on this interface */
1266 list_for_each_entry(ix
, &devices
, list
) {
1267 if (ix
->iface
== icd
->iface
&& ix
->devnum
== i
) {
1276 * ok, we have 256 cameras on this host...
1277 * man, stay reasonable...
1283 icd
->host_priv
= NULL
;
1284 mutex_init(&icd
->video_lock
);
1286 list_add_tail(&icd
->list
, &devices
);
1291 static void soc_camera_device_unregister(struct soc_camera_device
*icd
)
1293 list_del(&icd
->list
);
1296 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops
= {
1297 .vidioc_querycap
= soc_camera_querycap
,
1298 .vidioc_g_fmt_vid_cap
= soc_camera_g_fmt_vid_cap
,
1299 .vidioc_enum_fmt_vid_cap
= soc_camera_enum_fmt_vid_cap
,
1300 .vidioc_s_fmt_vid_cap
= soc_camera_s_fmt_vid_cap
,
1301 .vidioc_enum_input
= soc_camera_enum_input
,
1302 .vidioc_g_input
= soc_camera_g_input
,
1303 .vidioc_s_input
= soc_camera_s_input
,
1304 .vidioc_s_std
= soc_camera_s_std
,
1305 .vidioc_reqbufs
= soc_camera_reqbufs
,
1306 .vidioc_try_fmt_vid_cap
= soc_camera_try_fmt_vid_cap
,
1307 .vidioc_querybuf
= soc_camera_querybuf
,
1308 .vidioc_qbuf
= soc_camera_qbuf
,
1309 .vidioc_dqbuf
= soc_camera_dqbuf
,
1310 .vidioc_streamon
= soc_camera_streamon
,
1311 .vidioc_streamoff
= soc_camera_streamoff
,
1312 .vidioc_queryctrl
= soc_camera_queryctrl
,
1313 .vidioc_g_ctrl
= soc_camera_g_ctrl
,
1314 .vidioc_s_ctrl
= soc_camera_s_ctrl
,
1315 .vidioc_cropcap
= soc_camera_cropcap
,
1316 .vidioc_g_crop
= soc_camera_g_crop
,
1317 .vidioc_s_crop
= soc_camera_s_crop
,
1318 .vidioc_g_parm
= soc_camera_g_parm
,
1319 .vidioc_s_parm
= soc_camera_s_parm
,
1320 .vidioc_g_chip_ident
= soc_camera_g_chip_ident
,
1321 #ifdef CONFIG_VIDEO_ADV_DEBUG
1322 .vidioc_g_register
= soc_camera_g_register
,
1323 .vidioc_s_register
= soc_camera_s_register
,
1327 static int video_dev_create(struct soc_camera_device
*icd
)
1329 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1330 struct video_device
*vdev
= video_device_alloc();
1335 strlcpy(vdev
->name
, ici
->drv_name
, sizeof(vdev
->name
));
1337 vdev
->parent
= &icd
->dev
;
1338 vdev
->current_norm
= V4L2_STD_UNKNOWN
;
1339 vdev
->fops
= &soc_camera_fops
;
1340 vdev
->ioctl_ops
= &soc_camera_ioctl_ops
;
1341 vdev
->release
= video_device_release
;
1342 vdev
->tvnorms
= V4L2_STD_UNKNOWN
;
1350 * Called from soc_camera_probe() above (with .video_lock held???)
1352 static int soc_camera_video_start(struct soc_camera_device
*icd
)
1354 struct device_type
*type
= icd
->vdev
->dev
.type
;
1357 if (!icd
->dev
.parent
)
1361 !icd
->ops
->query_bus_param
||
1362 !icd
->ops
->set_bus_param
)
1365 ret
= video_register_device(icd
->vdev
, VFL_TYPE_GRABBER
, -1);
1367 dev_err(&icd
->dev
, "video_register_device failed: %d\n", ret
);
1371 /* Restore device type, possibly set by the subdevice driver */
1372 icd
->vdev
->dev
.type
= type
;
1377 static int __devinit
soc_camera_pdrv_probe(struct platform_device
*pdev
)
1379 struct soc_camera_link
*icl
= pdev
->dev
.platform_data
;
1380 struct soc_camera_device
*icd
;
1386 icd
= kzalloc(sizeof(*icd
), GFP_KERNEL
);
1390 icd
->iface
= icl
->bus_id
;
1391 icd
->pdev
= &pdev
->dev
;
1392 platform_set_drvdata(pdev
, icd
);
1394 ret
= soc_camera_device_register(icd
);
1398 soc_camera_device_init(&icd
->dev
, icl
);
1400 icd
->user_width
= DEFAULT_WIDTH
;
1401 icd
->user_height
= DEFAULT_HEIGHT
;
1412 * Only called on rmmod for each platform device, since they are not
1413 * hot-pluggable. Now we know, that all our users - hosts and devices have
1414 * been unloaded already
1416 static int __devexit
soc_camera_pdrv_remove(struct platform_device
*pdev
)
1418 struct soc_camera_device
*icd
= platform_get_drvdata(pdev
);
1423 soc_camera_device_unregister(icd
);
1430 static struct platform_driver __refdata soc_camera_pdrv
= {
1431 .remove
= __devexit_p(soc_camera_pdrv_remove
),
1433 .name
= "soc-camera-pdrv",
1434 .owner
= THIS_MODULE
,
1438 static int __init
soc_camera_init(void)
1440 int ret
= bus_register(&soc_camera_bus_type
);
1443 ret
= driver_register(&ic_drv
);
1447 ret
= platform_driver_probe(&soc_camera_pdrv
, soc_camera_pdrv_probe
);
1454 driver_unregister(&ic_drv
);
1456 bus_unregister(&soc_camera_bus_type
);
1460 static void __exit
soc_camera_exit(void)
1462 platform_driver_unregister(&soc_camera_pdrv
);
1463 driver_unregister(&ic_drv
);
1464 bus_unregister(&soc_camera_bus_type
);
1467 module_init(soc_camera_init
);
1468 module_exit(soc_camera_exit
);
1470 MODULE_DESCRIPTION("Image capture bus driver");
1471 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1472 MODULE_LICENSE("GPL");
1473 MODULE_ALIAS("platform:soc-camera-pdrv");