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/vmalloc.h>
30 #include <media/soc_camera.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/v4l2-dev.h>
34 #include <media/videobuf-core.h>
35 #include <media/soc_mediabus.h>
37 /* Default to VGA resolution */
38 #define DEFAULT_WIDTH 640
39 #define DEFAULT_HEIGHT 480
41 static LIST_HEAD(hosts
);
42 static LIST_HEAD(devices
);
43 static DEFINE_MUTEX(list_lock
); /* Protects the list of hosts */
45 const struct soc_camera_format_xlate
*soc_camera_xlate_by_fourcc(
46 struct soc_camera_device
*icd
, unsigned int fourcc
)
50 for (i
= 0; i
< icd
->num_user_formats
; i
++)
51 if (icd
->user_formats
[i
].host_fmt
->fourcc
== fourcc
)
52 return icd
->user_formats
+ i
;
55 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc
);
58 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
59 * @icl: camera platform parameters
60 * @flags: flags to be inverted according to platform configuration
61 * @return: resulting flags
63 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link
*icl
,
68 /* If only one of the two polarities is supported, switch to the opposite */
69 if (icl
->flags
& SOCAM_SENSOR_INVERT_HSYNC
) {
70 f
= flags
& (SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_HSYNC_ACTIVE_LOW
);
71 if (f
== SOCAM_HSYNC_ACTIVE_HIGH
|| f
== SOCAM_HSYNC_ACTIVE_LOW
)
72 flags
^= SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_HSYNC_ACTIVE_LOW
;
75 if (icl
->flags
& SOCAM_SENSOR_INVERT_VSYNC
) {
76 f
= flags
& (SOCAM_VSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_LOW
);
77 if (f
== SOCAM_VSYNC_ACTIVE_HIGH
|| f
== SOCAM_VSYNC_ACTIVE_LOW
)
78 flags
^= SOCAM_VSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_LOW
;
81 if (icl
->flags
& SOCAM_SENSOR_INVERT_PCLK
) {
82 f
= flags
& (SOCAM_PCLK_SAMPLE_RISING
| SOCAM_PCLK_SAMPLE_FALLING
);
83 if (f
== SOCAM_PCLK_SAMPLE_RISING
|| f
== SOCAM_PCLK_SAMPLE_FALLING
)
84 flags
^= SOCAM_PCLK_SAMPLE_RISING
| SOCAM_PCLK_SAMPLE_FALLING
;
89 EXPORT_SYMBOL(soc_camera_apply_sensor_flags
);
91 static int soc_camera_try_fmt_vid_cap(struct file
*file
, void *priv
,
92 struct v4l2_format
*f
)
94 struct soc_camera_file
*icf
= file
->private_data
;
95 struct soc_camera_device
*icd
= icf
->icd
;
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_file
*icf
= file
->private_data
;
108 struct soc_camera_device
*icd
= icf
->icd
;
114 if (icd
->ops
->enum_input
)
115 ret
= icd
->ops
->enum_input(icd
, inp
);
117 /* default is camera */
118 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
119 inp
->std
= V4L2_STD_UNKNOWN
;
120 strcpy(inp
->name
, "Camera");
126 static int soc_camera_g_input(struct file
*file
, void *priv
, unsigned int *i
)
133 static int soc_camera_s_input(struct file
*file
, void *priv
, unsigned int i
)
141 static int soc_camera_s_std(struct file
*file
, void *priv
, v4l2_std_id
*a
)
143 struct soc_camera_file
*icf
= file
->private_data
;
144 struct soc_camera_device
*icd
= icf
->icd
;
145 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
147 return v4l2_subdev_call(sd
, core
, s_std
, *a
);
150 static int soc_camera_reqbufs(struct file
*file
, void *priv
,
151 struct v4l2_requestbuffers
*p
)
154 struct soc_camera_file
*icf
= file
->private_data
;
155 struct soc_camera_device
*icd
= icf
->icd
;
156 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
158 WARN_ON(priv
!= file
->private_data
);
160 ret
= videobuf_reqbufs(&icf
->vb_vidq
, p
);
164 return ici
->ops
->reqbufs(icf
, p
);
167 static int soc_camera_querybuf(struct file
*file
, void *priv
,
168 struct v4l2_buffer
*p
)
170 struct soc_camera_file
*icf
= file
->private_data
;
172 WARN_ON(priv
!= file
->private_data
);
174 return videobuf_querybuf(&icf
->vb_vidq
, p
);
177 static int soc_camera_qbuf(struct file
*file
, void *priv
,
178 struct v4l2_buffer
*p
)
180 struct soc_camera_file
*icf
= file
->private_data
;
182 WARN_ON(priv
!= file
->private_data
);
184 return videobuf_qbuf(&icf
->vb_vidq
, p
);
187 static int soc_camera_dqbuf(struct file
*file
, void *priv
,
188 struct v4l2_buffer
*p
)
190 struct soc_camera_file
*icf
= file
->private_data
;
192 WARN_ON(priv
!= file
->private_data
);
194 return videobuf_dqbuf(&icf
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
197 /* Always entered with .video_lock held */
198 static int soc_camera_init_user_formats(struct soc_camera_device
*icd
)
200 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
201 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
202 int i
, fmts
= 0, raw_fmts
= 0, ret
;
203 enum v4l2_mbus_pixelcode code
;
205 while (!v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, raw_fmts
, &code
))
208 if (!ici
->ops
->get_formats
)
210 * Fallback mode - the host will have to serve all
211 * sensor-provided formats one-to-one to the user
216 * First pass - only count formats this host-sensor
217 * configuration can provide
219 for (i
= 0; i
< raw_fmts
; i
++) {
220 ret
= ici
->ops
->get_formats(icd
, i
, NULL
);
230 vmalloc(fmts
* sizeof(struct soc_camera_format_xlate
));
231 if (!icd
->user_formats
)
234 icd
->num_user_formats
= fmts
;
236 dev_dbg(&icd
->dev
, "Found %d supported formats.\n", fmts
);
238 /* Second pass - actually fill data formats */
240 for (i
= 0; i
< raw_fmts
; i
++)
241 if (!ici
->ops
->get_formats
) {
242 v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, i
, &code
);
243 icd
->user_formats
[i
].host_fmt
=
244 soc_mbus_get_fmtdesc(code
);
245 icd
->user_formats
[i
].code
= code
;
247 ret
= ici
->ops
->get_formats(icd
, i
,
248 &icd
->user_formats
[fmts
]);
254 icd
->current_fmt
= &icd
->user_formats
[0];
259 icd
->num_user_formats
= 0;
260 vfree(icd
->user_formats
);
264 /* Always entered with .video_lock held */
265 static void soc_camera_free_user_formats(struct soc_camera_device
*icd
)
267 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
269 if (ici
->ops
->put_formats
)
270 ici
->ops
->put_formats(icd
);
271 icd
->current_fmt
= NULL
;
272 icd
->num_user_formats
= 0;
273 vfree(icd
->user_formats
);
274 icd
->user_formats
= NULL
;
277 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
280 /* Called with .vb_lock held, or from the first open(2), see comment there */
281 static int soc_camera_set_fmt(struct soc_camera_file
*icf
,
282 struct v4l2_format
*f
)
284 struct soc_camera_device
*icd
= icf
->icd
;
285 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
286 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
289 dev_dbg(&icd
->dev
, "S_FMT(%c%c%c%c, %ux%u)\n",
290 pixfmtstr(pix
->pixelformat
), pix
->width
, pix
->height
);
292 /* We always call try_fmt() before set_fmt() or set_crop() */
293 ret
= ici
->ops
->try_fmt(icd
, f
);
297 ret
= ici
->ops
->set_fmt(icd
, f
);
300 } else if (!icd
->current_fmt
||
301 icd
->current_fmt
->host_fmt
->fourcc
!= pix
->pixelformat
) {
303 "Host driver hasn't set up current format correctly!\n");
307 icd
->user_width
= pix
->width
;
308 icd
->user_height
= pix
->height
;
309 icd
->colorspace
= pix
->colorspace
;
311 icd
->field
= pix
->field
;
313 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
314 dev_warn(&icd
->dev
, "Attention! Wrong buf-type %d\n",
317 dev_dbg(&icd
->dev
, "set width: %d height: %d\n",
318 icd
->user_width
, icd
->user_height
);
320 /* set physical bus parameters */
321 return ici
->ops
->set_bus_param(icd
, pix
->pixelformat
);
324 static int soc_camera_open(struct file
*file
)
326 struct video_device
*vdev
= video_devdata(file
);
327 struct soc_camera_device
*icd
= container_of(vdev
->parent
,
328 struct soc_camera_device
,
330 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
331 struct soc_camera_host
*ici
;
332 struct soc_camera_file
*icf
;
336 /* No device driver attached */
339 ici
= to_soc_camera_host(icd
->dev
.parent
);
341 icf
= vmalloc(sizeof(*icf
));
345 if (!try_module_get(ici
->ops
->owner
)) {
346 dev_err(&icd
->dev
, "Couldn't lock capture bus driver.\n");
352 * Protect against icd->ops->remove() until we module_get() both
355 mutex_lock(&icd
->video_lock
);
360 /* Now we really have to activate the camera */
361 if (icd
->use_count
== 1) {
362 /* Restore parameters before the last close() per V4L2 API */
363 struct v4l2_format f
= {
364 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
366 .width
= icd
->user_width
,
367 .height
= icd
->user_height
,
369 .colorspace
= icd
->colorspace
,
371 icd
->current_fmt
->host_fmt
->fourcc
,
376 ret
= icl
->power(icd
->pdev
, 1);
381 /* The camera could have been already on, try to reset */
383 icl
->reset(icd
->pdev
);
385 ret
= ici
->ops
->add(icd
);
387 dev_err(&icd
->dev
, "Couldn't activate the camera: %d\n", ret
);
392 * Try to configure with default parameters. Notice: this is the
393 * very first open, so, we cannot race against other calls,
394 * apart from someone else calling open() simultaneously, but
395 * .video_lock is protecting us against it.
397 ret
= soc_camera_set_fmt(icf
, &f
);
402 file
->private_data
= icf
;
403 dev_dbg(&icd
->dev
, "camera device open\n");
405 ici
->ops
->init_videobuf(&icf
->vb_vidq
, icd
);
407 mutex_unlock(&icd
->video_lock
);
412 * First five errors are entered with the .video_lock held
416 ici
->ops
->remove(icd
);
419 icl
->power(icd
->pdev
, 0);
422 mutex_unlock(&icd
->video_lock
);
423 module_put(ici
->ops
->owner
);
429 static int soc_camera_close(struct file
*file
)
431 struct soc_camera_file
*icf
= file
->private_data
;
432 struct soc_camera_device
*icd
= icf
->icd
;
433 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
435 mutex_lock(&icd
->video_lock
);
437 if (!icd
->use_count
) {
438 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
440 ici
->ops
->remove(icd
);
442 icl
->power(icd
->pdev
, 0);
445 mutex_unlock(&icd
->video_lock
);
447 module_put(ici
->ops
->owner
);
451 dev_dbg(&icd
->dev
, "camera device close\n");
456 static ssize_t
soc_camera_read(struct file
*file
, char __user
*buf
,
457 size_t count
, loff_t
*ppos
)
459 struct soc_camera_file
*icf
= file
->private_data
;
460 struct soc_camera_device
*icd
= icf
->icd
;
463 dev_err(&icd
->dev
, "camera device read not implemented\n");
468 static int soc_camera_mmap(struct file
*file
, struct vm_area_struct
*vma
)
470 struct soc_camera_file
*icf
= file
->private_data
;
471 struct soc_camera_device
*icd
= icf
->icd
;
474 dev_dbg(&icd
->dev
, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
476 err
= videobuf_mmap_mapper(&icf
->vb_vidq
, vma
);
478 dev_dbg(&icd
->dev
, "vma start=0x%08lx, size=%ld, ret=%d\n",
479 (unsigned long)vma
->vm_start
,
480 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
486 static unsigned int soc_camera_poll(struct file
*file
, poll_table
*pt
)
488 struct soc_camera_file
*icf
= file
->private_data
;
489 struct soc_camera_device
*icd
= icf
->icd
;
490 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
492 if (list_empty(&icf
->vb_vidq
.stream
)) {
493 dev_err(&icd
->dev
, "Trying to poll with no queued buffers!\n");
497 return ici
->ops
->poll(file
, pt
);
500 static struct v4l2_file_operations soc_camera_fops
= {
501 .owner
= THIS_MODULE
,
502 .open
= soc_camera_open
,
503 .release
= soc_camera_close
,
504 .ioctl
= video_ioctl2
,
505 .read
= soc_camera_read
,
506 .mmap
= soc_camera_mmap
,
507 .poll
= soc_camera_poll
,
510 static int soc_camera_s_fmt_vid_cap(struct file
*file
, void *priv
,
511 struct v4l2_format
*f
)
513 struct soc_camera_file
*icf
= file
->private_data
;
514 struct soc_camera_device
*icd
= icf
->icd
;
517 WARN_ON(priv
!= file
->private_data
);
519 mutex_lock(&icf
->vb_vidq
.vb_lock
);
521 if (icf
->vb_vidq
.bufs
[0]) {
522 dev_err(&icd
->dev
, "S_FMT denied: queue initialised\n");
527 ret
= soc_camera_set_fmt(icf
, f
);
530 mutex_unlock(&icf
->vb_vidq
.vb_lock
);
535 static int soc_camera_enum_fmt_vid_cap(struct file
*file
, void *priv
,
536 struct v4l2_fmtdesc
*f
)
538 struct soc_camera_file
*icf
= file
->private_data
;
539 struct soc_camera_device
*icd
= icf
->icd
;
540 const struct soc_mbus_pixelfmt
*format
;
542 WARN_ON(priv
!= file
->private_data
);
544 if (f
->index
>= icd
->num_user_formats
)
547 format
= icd
->user_formats
[f
->index
].host_fmt
;
550 strlcpy(f
->description
, format
->name
, sizeof(f
->description
));
551 f
->pixelformat
= format
->fourcc
;
555 static int soc_camera_g_fmt_vid_cap(struct file
*file
, void *priv
,
556 struct v4l2_format
*f
)
558 struct soc_camera_file
*icf
= file
->private_data
;
559 struct soc_camera_device
*icd
= icf
->icd
;
560 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
562 WARN_ON(priv
!= file
->private_data
);
564 pix
->width
= icd
->user_width
;
565 pix
->height
= icd
->user_height
;
566 pix
->field
= icf
->vb_vidq
.field
;
567 pix
->pixelformat
= icd
->current_fmt
->host_fmt
->fourcc
;
568 pix
->bytesperline
= soc_mbus_bytes_per_line(pix
->width
,
569 icd
->current_fmt
->host_fmt
);
570 pix
->colorspace
= icd
->colorspace
;
571 if (pix
->bytesperline
< 0)
572 return pix
->bytesperline
;
573 pix
->sizeimage
= pix
->height
* pix
->bytesperline
;
574 dev_dbg(&icd
->dev
, "current_fmt->fourcc: 0x%08x\n",
575 icd
->current_fmt
->host_fmt
->fourcc
);
579 static int soc_camera_querycap(struct file
*file
, void *priv
,
580 struct v4l2_capability
*cap
)
582 struct soc_camera_file
*icf
= file
->private_data
;
583 struct soc_camera_device
*icd
= icf
->icd
;
584 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
586 WARN_ON(priv
!= file
->private_data
);
588 strlcpy(cap
->driver
, ici
->drv_name
, sizeof(cap
->driver
));
589 return ici
->ops
->querycap(ici
, cap
);
592 static int soc_camera_streamon(struct file
*file
, void *priv
,
593 enum v4l2_buf_type i
)
595 struct soc_camera_file
*icf
= file
->private_data
;
596 struct soc_camera_device
*icd
= icf
->icd
;
597 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
600 WARN_ON(priv
!= file
->private_data
);
602 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
605 mutex_lock(&icd
->video_lock
);
607 v4l2_subdev_call(sd
, video
, s_stream
, 1);
609 /* This calls buf_queue from host driver's videobuf_queue_ops */
610 ret
= videobuf_streamon(&icf
->vb_vidq
);
612 mutex_unlock(&icd
->video_lock
);
617 static int soc_camera_streamoff(struct file
*file
, void *priv
,
618 enum v4l2_buf_type i
)
620 struct soc_camera_file
*icf
= file
->private_data
;
621 struct soc_camera_device
*icd
= icf
->icd
;
622 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
624 WARN_ON(priv
!= file
->private_data
);
626 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
629 mutex_lock(&icd
->video_lock
);
632 * This calls buf_release from host driver's videobuf_queue_ops for all
633 * remaining buffers. When the last buffer is freed, stop capture
635 videobuf_streamoff(&icf
->vb_vidq
);
637 v4l2_subdev_call(sd
, video
, s_stream
, 0);
639 mutex_unlock(&icd
->video_lock
);
644 static int soc_camera_queryctrl(struct file
*file
, void *priv
,
645 struct v4l2_queryctrl
*qc
)
647 struct soc_camera_file
*icf
= file
->private_data
;
648 struct soc_camera_device
*icd
= icf
->icd
;
649 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
652 WARN_ON(priv
!= file
->private_data
);
657 /* First check host controls */
658 for (i
= 0; i
< ici
->ops
->num_controls
; i
++)
659 if (qc
->id
== ici
->ops
->controls
[i
].id
) {
660 memcpy(qc
, &(ici
->ops
->controls
[i
]),
665 /* Then device controls */
666 for (i
= 0; i
< icd
->ops
->num_controls
; i
++)
667 if (qc
->id
== icd
->ops
->controls
[i
].id
) {
668 memcpy(qc
, &(icd
->ops
->controls
[i
]),
676 static int soc_camera_g_ctrl(struct file
*file
, void *priv
,
677 struct v4l2_control
*ctrl
)
679 struct soc_camera_file
*icf
= file
->private_data
;
680 struct soc_camera_device
*icd
= icf
->icd
;
681 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
682 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
685 WARN_ON(priv
!= file
->private_data
);
687 if (ici
->ops
->get_ctrl
) {
688 ret
= ici
->ops
->get_ctrl(icd
, ctrl
);
689 if (ret
!= -ENOIOCTLCMD
)
693 return v4l2_subdev_call(sd
, core
, g_ctrl
, ctrl
);
696 static int soc_camera_s_ctrl(struct file
*file
, void *priv
,
697 struct v4l2_control
*ctrl
)
699 struct soc_camera_file
*icf
= file
->private_data
;
700 struct soc_camera_device
*icd
= icf
->icd
;
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
->set_ctrl
) {
708 ret
= ici
->ops
->set_ctrl(icd
, ctrl
);
709 if (ret
!= -ENOIOCTLCMD
)
713 return v4l2_subdev_call(sd
, core
, s_ctrl
, ctrl
);
716 static int soc_camera_cropcap(struct file
*file
, void *fh
,
717 struct v4l2_cropcap
*a
)
719 struct soc_camera_file
*icf
= file
->private_data
;
720 struct soc_camera_device
*icd
= icf
->icd
;
721 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
723 return ici
->ops
->cropcap(icd
, a
);
726 static int soc_camera_g_crop(struct file
*file
, void *fh
,
729 struct soc_camera_file
*icf
= file
->private_data
;
730 struct soc_camera_device
*icd
= icf
->icd
;
731 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
734 mutex_lock(&icf
->vb_vidq
.vb_lock
);
735 ret
= ici
->ops
->get_crop(icd
, a
);
736 mutex_unlock(&icf
->vb_vidq
.vb_lock
);
742 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
743 * argument with the actual geometry, instead, the user shall use G_CROP to
744 * retrieve it. However, we expect camera host and client drivers to update
745 * the argument, which we then use internally, but do not return to the user.
747 static int soc_camera_s_crop(struct file
*file
, void *fh
,
750 struct soc_camera_file
*icf
= file
->private_data
;
751 struct soc_camera_device
*icd
= icf
->icd
;
752 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
753 struct v4l2_rect
*rect
= &a
->c
;
754 struct v4l2_crop current_crop
;
757 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
760 dev_dbg(&icd
->dev
, "S_CROP(%ux%u@%u:%u)\n",
761 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
763 /* Cropping is allowed during a running capture, guard consistency */
764 mutex_lock(&icf
->vb_vidq
.vb_lock
);
766 /* If get_crop fails, we'll let host and / or client drivers decide */
767 ret
= ici
->ops
->get_crop(icd
, ¤t_crop
);
769 /* Prohibit window size change with initialised buffers */
770 if (icf
->vb_vidq
.bufs
[0] && !ret
&&
771 (a
->c
.width
!= current_crop
.c
.width
||
772 a
->c
.height
!= current_crop
.c
.height
)) {
774 "S_CROP denied: queue initialised and sizes differ\n");
777 ret
= ici
->ops
->set_crop(icd
, a
);
780 mutex_unlock(&icf
->vb_vidq
.vb_lock
);
785 static int soc_camera_g_parm(struct file
*file
, void *fh
,
786 struct v4l2_streamparm
*a
)
788 struct soc_camera_file
*icf
= file
->private_data
;
789 struct soc_camera_device
*icd
= icf
->icd
;
790 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
792 if (ici
->ops
->get_parm
)
793 return ici
->ops
->get_parm(icd
, a
);
798 static int soc_camera_s_parm(struct file
*file
, void *fh
,
799 struct v4l2_streamparm
*a
)
801 struct soc_camera_file
*icf
= file
->private_data
;
802 struct soc_camera_device
*icd
= icf
->icd
;
803 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
805 if (ici
->ops
->set_parm
)
806 return ici
->ops
->set_parm(icd
, a
);
811 static int soc_camera_g_chip_ident(struct file
*file
, void *fh
,
812 struct v4l2_dbg_chip_ident
*id
)
814 struct soc_camera_file
*icf
= file
->private_data
;
815 struct soc_camera_device
*icd
= icf
->icd
;
816 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
818 return v4l2_subdev_call(sd
, core
, g_chip_ident
, id
);
821 #ifdef CONFIG_VIDEO_ADV_DEBUG
822 static int soc_camera_g_register(struct file
*file
, void *fh
,
823 struct v4l2_dbg_register
*reg
)
825 struct soc_camera_file
*icf
= file
->private_data
;
826 struct soc_camera_device
*icd
= icf
->icd
;
827 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
829 return v4l2_subdev_call(sd
, core
, g_register
, reg
);
832 static int soc_camera_s_register(struct file
*file
, void *fh
,
833 struct v4l2_dbg_register
*reg
)
835 struct soc_camera_file
*icf
= file
->private_data
;
836 struct soc_camera_device
*icd
= icf
->icd
;
837 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
839 return v4l2_subdev_call(sd
, core
, s_register
, reg
);
843 /* So far this function cannot fail */
844 static void scan_add_host(struct soc_camera_host
*ici
)
846 struct soc_camera_device
*icd
;
848 mutex_lock(&list_lock
);
850 list_for_each_entry(icd
, &devices
, list
) {
851 if (icd
->iface
== ici
->nr
) {
853 icd
->dev
.parent
= ici
->v4l2_dev
.dev
;
854 dev_set_name(&icd
->dev
, "%u-%u", icd
->iface
,
856 ret
= device_register(&icd
->dev
);
858 icd
->dev
.parent
= NULL
;
860 "Cannot register device: %d\n", ret
);
865 mutex_unlock(&list_lock
);
868 #ifdef CONFIG_I2C_BOARDINFO
869 static int soc_camera_init_i2c(struct soc_camera_device
*icd
,
870 struct soc_camera_link
*icl
)
872 struct i2c_client
*client
;
873 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
874 struct i2c_adapter
*adap
= i2c_get_adapter(icl
->i2c_adapter_id
);
875 struct v4l2_subdev
*subdev
;
878 dev_err(&icd
->dev
, "Cannot get I2C adapter #%d. No driver?\n",
879 icl
->i2c_adapter_id
);
883 icl
->board_info
->platform_data
= icd
;
885 subdev
= v4l2_i2c_new_subdev_board(&ici
->v4l2_dev
, adap
,
886 icl
->module_name
, icl
->board_info
, NULL
);
890 client
= subdev
->priv
;
892 /* Use to_i2c_client(dev) to recover the i2c client */
893 dev_set_drvdata(&icd
->dev
, &client
->dev
);
897 i2c_put_adapter(adap
);
902 static void soc_camera_free_i2c(struct soc_camera_device
*icd
)
904 struct i2c_client
*client
=
905 to_i2c_client(to_soc_camera_control(icd
));
906 dev_set_drvdata(&icd
->dev
, NULL
);
907 v4l2_device_unregister_subdev(i2c_get_clientdata(client
));
908 i2c_unregister_device(client
);
909 i2c_put_adapter(client
->adapter
);
912 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
913 #define soc_camera_free_i2c(icd) do {} while (0)
916 static int soc_camera_video_start(struct soc_camera_device
*icd
);
917 static int video_dev_create(struct soc_camera_device
*icd
);
918 /* Called during host-driver probe */
919 static int soc_camera_probe(struct device
*dev
)
921 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
922 struct soc_camera_host
*ici
= to_soc_camera_host(dev
->parent
);
923 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
924 struct device
*control
= NULL
;
925 struct v4l2_subdev
*sd
;
926 struct v4l2_mbus_framefmt mf
;
929 dev_info(dev
, "Probing %s\n", dev_name(dev
));
932 ret
= icl
->power(icd
->pdev
, 1);
935 "Platform failed to power-on the camera.\n");
940 /* The camera could have been already on, try to reset */
942 icl
->reset(icd
->pdev
);
944 ret
= ici
->ops
->add(icd
);
948 /* Must have icd->vdev before registering the device */
949 ret
= video_dev_create(icd
);
953 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
954 if (icl
->board_info
) {
955 ret
= soc_camera_init_i2c(icd
, icl
);
958 } else if (!icl
->add_device
|| !icl
->del_device
) {
962 if (icl
->module_name
)
963 ret
= request_module(icl
->module_name
);
965 ret
= icl
->add_device(icl
, &icd
->dev
);
970 * FIXME: this is racy, have to use driver-binding notification,
971 * when it is available
973 control
= to_soc_camera_control(icd
);
974 if (!control
|| !control
->driver
|| !dev_get_drvdata(control
) ||
975 !try_module_get(control
->driver
->owner
)) {
976 icl
->del_device(icl
);
981 /* At this point client .probe() should have run already */
982 ret
= soc_camera_init_user_formats(icd
);
986 icd
->field
= V4L2_FIELD_ANY
;
988 /* ..._video_start() will create a device node, so we have to protect */
989 mutex_lock(&icd
->video_lock
);
991 ret
= soc_camera_video_start(icd
);
995 /* Try to improve our guess of a reasonable window format */
996 sd
= soc_camera_to_subdev(icd
);
997 if (!v4l2_subdev_call(sd
, video
, g_mbus_fmt
, &mf
)) {
998 icd
->user_width
= mf
.width
;
999 icd
->user_height
= mf
.height
;
1000 icd
->colorspace
= mf
.colorspace
;
1001 icd
->field
= mf
.field
;
1004 /* Do we have to sysfs_remove_link() before device_unregister()? */
1005 if (sysfs_create_link(&icd
->dev
.kobj
, &to_soc_camera_control(icd
)->kobj
,
1007 dev_warn(&icd
->dev
, "Failed creating the control symlink\n");
1009 ici
->ops
->remove(icd
);
1012 icl
->power(icd
->pdev
, 0);
1014 mutex_unlock(&icd
->video_lock
);
1019 mutex_unlock(&icd
->video_lock
);
1020 soc_camera_free_user_formats(icd
);
1022 if (icl
->board_info
) {
1023 soc_camera_free_i2c(icd
);
1025 icl
->del_device(icl
);
1026 module_put(control
->driver
->owner
);
1030 video_device_release(icd
->vdev
);
1032 ici
->ops
->remove(icd
);
1035 icl
->power(icd
->pdev
, 0);
1041 * This is called on device_unregister, which only means we have to disconnect
1042 * from the host, but not remove ourselves from the device list
1044 static int soc_camera_remove(struct device
*dev
)
1046 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
1047 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
1048 struct video_device
*vdev
= icd
->vdev
;
1050 BUG_ON(!dev
->parent
);
1053 mutex_lock(&icd
->video_lock
);
1054 video_unregister_device(vdev
);
1056 mutex_unlock(&icd
->video_lock
);
1059 if (icl
->board_info
) {
1060 soc_camera_free_i2c(icd
);
1062 struct device_driver
*drv
= to_soc_camera_control(icd
) ?
1063 to_soc_camera_control(icd
)->driver
: NULL
;
1065 icl
->del_device(icl
);
1066 module_put(drv
->owner
);
1069 soc_camera_free_user_formats(icd
);
1074 static int soc_camera_suspend(struct device
*dev
, pm_message_t state
)
1076 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
1077 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1080 if (ici
->ops
->suspend
)
1081 ret
= ici
->ops
->suspend(icd
, state
);
1086 static int soc_camera_resume(struct device
*dev
)
1088 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
1089 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1092 if (ici
->ops
->resume
)
1093 ret
= ici
->ops
->resume(icd
);
1098 static struct bus_type soc_camera_bus_type
= {
1099 .name
= "soc-camera",
1100 .probe
= soc_camera_probe
,
1101 .remove
= soc_camera_remove
,
1102 .suspend
= soc_camera_suspend
,
1103 .resume
= soc_camera_resume
,
1106 static struct device_driver ic_drv
= {
1108 .bus
= &soc_camera_bus_type
,
1109 .owner
= THIS_MODULE
,
1112 static void dummy_release(struct device
*dev
)
1116 static int default_cropcap(struct soc_camera_device
*icd
,
1117 struct v4l2_cropcap
*a
)
1119 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1120 return v4l2_subdev_call(sd
, video
, cropcap
, a
);
1123 static int default_g_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*a
)
1125 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1126 return v4l2_subdev_call(sd
, video
, g_crop
, a
);
1129 static int default_s_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*a
)
1131 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1132 return v4l2_subdev_call(sd
, video
, s_crop
, a
);
1135 static void soc_camera_device_init(struct device
*dev
, void *pdata
)
1137 dev
->platform_data
= pdata
;
1138 dev
->bus
= &soc_camera_bus_type
;
1139 dev
->release
= dummy_release
;
1142 int soc_camera_host_register(struct soc_camera_host
*ici
)
1144 struct soc_camera_host
*ix
;
1147 if (!ici
|| !ici
->ops
||
1148 !ici
->ops
->try_fmt
||
1149 !ici
->ops
->set_fmt
||
1150 !ici
->ops
->set_bus_param
||
1151 !ici
->ops
->querycap
||
1152 !ici
->ops
->init_videobuf
||
1153 !ici
->ops
->reqbufs
||
1155 !ici
->ops
->remove
||
1160 if (!ici
->ops
->set_crop
)
1161 ici
->ops
->set_crop
= default_s_crop
;
1162 if (!ici
->ops
->get_crop
)
1163 ici
->ops
->get_crop
= default_g_crop
;
1164 if (!ici
->ops
->cropcap
)
1165 ici
->ops
->cropcap
= default_cropcap
;
1167 mutex_lock(&list_lock
);
1168 list_for_each_entry(ix
, &hosts
, list
) {
1169 if (ix
->nr
== ici
->nr
) {
1175 ret
= v4l2_device_register(ici
->v4l2_dev
.dev
, &ici
->v4l2_dev
);
1179 list_add_tail(&ici
->list
, &hosts
);
1180 mutex_unlock(&list_lock
);
1187 mutex_unlock(&list_lock
);
1190 EXPORT_SYMBOL(soc_camera_host_register
);
1192 /* Unregister all clients! */
1193 void soc_camera_host_unregister(struct soc_camera_host
*ici
)
1195 struct soc_camera_device
*icd
;
1197 mutex_lock(&list_lock
);
1199 list_del(&ici
->list
);
1201 list_for_each_entry(icd
, &devices
, list
) {
1202 if (icd
->iface
== ici
->nr
) {
1203 void *pdata
= icd
->dev
.platform_data
;
1204 /* The bus->remove will be called */
1205 device_unregister(&icd
->dev
);
1207 * Not before device_unregister(), .remove
1208 * needs parent to call ici->ops->remove().
1209 * If the host module is loaded again, device_register()
1210 * would complain "already initialised," since 2.6.32
1211 * this is also needed to prevent use-after-free of the
1212 * device private data.
1214 memset(&icd
->dev
, 0, sizeof(icd
->dev
));
1215 soc_camera_device_init(&icd
->dev
, pdata
);
1219 mutex_unlock(&list_lock
);
1221 v4l2_device_unregister(&ici
->v4l2_dev
);
1223 EXPORT_SYMBOL(soc_camera_host_unregister
);
1225 /* Image capture device */
1226 static int soc_camera_device_register(struct soc_camera_device
*icd
)
1228 struct soc_camera_device
*ix
;
1231 for (i
= 0; i
< 256 && num
< 0; i
++) {
1233 /* Check if this index is available on this interface */
1234 list_for_each_entry(ix
, &devices
, list
) {
1235 if (ix
->iface
== icd
->iface
&& ix
->devnum
== i
) {
1244 * ok, we have 256 cameras on this host...
1245 * man, stay reasonable...
1251 icd
->host_priv
= NULL
;
1252 mutex_init(&icd
->video_lock
);
1254 list_add_tail(&icd
->list
, &devices
);
1259 static void soc_camera_device_unregister(struct soc_camera_device
*icd
)
1261 list_del(&icd
->list
);
1264 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops
= {
1265 .vidioc_querycap
= soc_camera_querycap
,
1266 .vidioc_g_fmt_vid_cap
= soc_camera_g_fmt_vid_cap
,
1267 .vidioc_enum_fmt_vid_cap
= soc_camera_enum_fmt_vid_cap
,
1268 .vidioc_s_fmt_vid_cap
= soc_camera_s_fmt_vid_cap
,
1269 .vidioc_enum_input
= soc_camera_enum_input
,
1270 .vidioc_g_input
= soc_camera_g_input
,
1271 .vidioc_s_input
= soc_camera_s_input
,
1272 .vidioc_s_std
= soc_camera_s_std
,
1273 .vidioc_reqbufs
= soc_camera_reqbufs
,
1274 .vidioc_try_fmt_vid_cap
= soc_camera_try_fmt_vid_cap
,
1275 .vidioc_querybuf
= soc_camera_querybuf
,
1276 .vidioc_qbuf
= soc_camera_qbuf
,
1277 .vidioc_dqbuf
= soc_camera_dqbuf
,
1278 .vidioc_streamon
= soc_camera_streamon
,
1279 .vidioc_streamoff
= soc_camera_streamoff
,
1280 .vidioc_queryctrl
= soc_camera_queryctrl
,
1281 .vidioc_g_ctrl
= soc_camera_g_ctrl
,
1282 .vidioc_s_ctrl
= soc_camera_s_ctrl
,
1283 .vidioc_cropcap
= soc_camera_cropcap
,
1284 .vidioc_g_crop
= soc_camera_g_crop
,
1285 .vidioc_s_crop
= soc_camera_s_crop
,
1286 .vidioc_g_parm
= soc_camera_g_parm
,
1287 .vidioc_s_parm
= soc_camera_s_parm
,
1288 .vidioc_g_chip_ident
= soc_camera_g_chip_ident
,
1289 #ifdef CONFIG_VIDEO_ADV_DEBUG
1290 .vidioc_g_register
= soc_camera_g_register
,
1291 .vidioc_s_register
= soc_camera_s_register
,
1295 static int video_dev_create(struct soc_camera_device
*icd
)
1297 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1298 struct video_device
*vdev
= video_device_alloc();
1303 strlcpy(vdev
->name
, ici
->drv_name
, sizeof(vdev
->name
));
1305 vdev
->parent
= &icd
->dev
;
1306 vdev
->current_norm
= V4L2_STD_UNKNOWN
;
1307 vdev
->fops
= &soc_camera_fops
;
1308 vdev
->ioctl_ops
= &soc_camera_ioctl_ops
;
1309 vdev
->release
= video_device_release
;
1310 vdev
->tvnorms
= V4L2_STD_UNKNOWN
;
1318 * Called from soc_camera_probe() above (with .video_lock held???)
1320 static int soc_camera_video_start(struct soc_camera_device
*icd
)
1324 if (!icd
->dev
.parent
)
1328 !icd
->ops
->query_bus_param
||
1329 !icd
->ops
->set_bus_param
)
1332 ret
= video_register_device(icd
->vdev
, VFL_TYPE_GRABBER
, -1);
1334 dev_err(&icd
->dev
, "video_register_device failed: %d\n", ret
);
1341 static int __devinit
soc_camera_pdrv_probe(struct platform_device
*pdev
)
1343 struct soc_camera_link
*icl
= pdev
->dev
.platform_data
;
1344 struct soc_camera_device
*icd
;
1350 icd
= kzalloc(sizeof(*icd
), GFP_KERNEL
);
1354 icd
->iface
= icl
->bus_id
;
1355 icd
->pdev
= &pdev
->dev
;
1356 platform_set_drvdata(pdev
, icd
);
1358 ret
= soc_camera_device_register(icd
);
1362 soc_camera_device_init(&icd
->dev
, icl
);
1364 icd
->user_width
= DEFAULT_WIDTH
;
1365 icd
->user_height
= DEFAULT_HEIGHT
;
1376 * Only called on rmmod for each platform device, since they are not
1377 * hot-pluggable. Now we know, that all our users - hosts and devices have
1378 * been unloaded already
1380 static int __devexit
soc_camera_pdrv_remove(struct platform_device
*pdev
)
1382 struct soc_camera_device
*icd
= platform_get_drvdata(pdev
);
1387 soc_camera_device_unregister(icd
);
1394 static struct platform_driver __refdata soc_camera_pdrv
= {
1395 .remove
= __devexit_p(soc_camera_pdrv_remove
),
1397 .name
= "soc-camera-pdrv",
1398 .owner
= THIS_MODULE
,
1402 static int __init
soc_camera_init(void)
1404 int ret
= bus_register(&soc_camera_bus_type
);
1407 ret
= driver_register(&ic_drv
);
1411 ret
= platform_driver_probe(&soc_camera_pdrv
, soc_camera_pdrv_probe
);
1418 driver_unregister(&ic_drv
);
1420 bus_unregister(&soc_camera_bus_type
);
1424 static void __exit
soc_camera_exit(void)
1426 platform_driver_unregister(&soc_camera_pdrv
);
1427 driver_unregister(&ic_drv
);
1428 bus_unregister(&soc_camera_bus_type
);
1431 module_init(soc_camera_init
);
1432 module_exit(soc_camera_exit
);
1434 MODULE_DESCRIPTION("Image capture bus driver");
1435 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1436 MODULE_LICENSE("GPL");
1437 MODULE_ALIAS("platform:soc-camera-pdrv");