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/module.h>
25 #include <linux/mutex.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
32 #include <media/soc_camera.h>
33 #include <media/soc_mediabus.h>
34 #include <media/v4l2-async.h>
35 #include <media/v4l2-clk.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-dev.h>
39 #include <media/videobuf-core.h>
40 #include <media/videobuf2-core.h>
42 /* Default to VGA resolution */
43 #define DEFAULT_WIDTH 640
44 #define DEFAULT_HEIGHT 480
46 #define is_streaming(ici, icd) \
47 (((ici)->ops->init_videobuf) ? \
48 (icd)->vb_vidq.streaming : \
49 vb2_is_streaming(&(icd)->vb2_vidq))
51 #define MAP_MAX_NUM 32
52 static DECLARE_BITMAP(device_map
, MAP_MAX_NUM
);
53 static LIST_HEAD(hosts
);
54 static LIST_HEAD(devices
);
56 * Protects lists and bitmaps of hosts and devices.
57 * Lock nesting: Ok to take ->host_lock under list_lock.
59 static DEFINE_MUTEX(list_lock
);
61 struct soc_camera_async_client
{
62 struct v4l2_async_subdev
*sensor
;
63 struct v4l2_async_notifier notifier
;
64 struct platform_device
*pdev
;
65 struct list_head list
; /* needed for clean up */
68 static int soc_camera_video_start(struct soc_camera_device
*icd
);
69 static int video_dev_create(struct soc_camera_device
*icd
);
71 int soc_camera_power_on(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
,
74 int ret
= clk
? v4l2_clk_enable(clk
) : 0;
76 dev_err(dev
, "Cannot enable clock: %d\n", ret
);
79 ret
= regulator_bulk_enable(ssdd
->num_regulators
,
82 dev_err(dev
, "Cannot enable regulators\n");
87 ret
= ssdd
->power(dev
, 1);
90 "Platform failed to power-on the camera.\n");
98 regulator_bulk_disable(ssdd
->num_regulators
,
102 v4l2_clk_disable(clk
);
106 EXPORT_SYMBOL(soc_camera_power_on
);
108 int soc_camera_power_off(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
,
109 struct v4l2_clk
*clk
)
115 err
= ssdd
->power(dev
, 0);
118 "Platform failed to power-off the camera.\n");
123 err
= regulator_bulk_disable(ssdd
->num_regulators
,
126 dev_err(dev
, "Cannot disable regulators\n");
131 v4l2_clk_disable(clk
);
135 EXPORT_SYMBOL(soc_camera_power_off
);
137 int soc_camera_power_init(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
)
139 /* Should not have any effect in synchronous case */
140 return devm_regulator_bulk_get(dev
, ssdd
->num_regulators
,
143 EXPORT_SYMBOL(soc_camera_power_init
);
145 static int __soc_camera_power_on(struct soc_camera_device
*icd
)
147 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
150 ret
= v4l2_subdev_call(sd
, core
, s_power
, 1);
151 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
157 static int __soc_camera_power_off(struct soc_camera_device
*icd
)
159 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
162 ret
= v4l2_subdev_call(sd
, core
, s_power
, 0);
163 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
169 const struct soc_camera_format_xlate
*soc_camera_xlate_by_fourcc(
170 struct soc_camera_device
*icd
, unsigned int fourcc
)
174 for (i
= 0; i
< icd
->num_user_formats
; i
++)
175 if (icd
->user_formats
[i
].host_fmt
->fourcc
== fourcc
)
176 return icd
->user_formats
+ i
;
179 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc
);
182 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
183 * @ssdd: camera platform parameters
184 * @cfg: media bus configuration
185 * @return: resulting flags
187 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc
*ssdd
,
188 const struct v4l2_mbus_config
*cfg
)
190 unsigned long f
, flags
= cfg
->flags
;
192 /* If only one of the two polarities is supported, switch to the opposite */
193 if (ssdd
->flags
& SOCAM_SENSOR_INVERT_HSYNC
) {
194 f
= flags
& (V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_HSYNC_ACTIVE_LOW
);
195 if (f
== V4L2_MBUS_HSYNC_ACTIVE_HIGH
|| f
== V4L2_MBUS_HSYNC_ACTIVE_LOW
)
196 flags
^= V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_HSYNC_ACTIVE_LOW
;
199 if (ssdd
->flags
& SOCAM_SENSOR_INVERT_VSYNC
) {
200 f
= flags
& (V4L2_MBUS_VSYNC_ACTIVE_HIGH
| V4L2_MBUS_VSYNC_ACTIVE_LOW
);
201 if (f
== V4L2_MBUS_VSYNC_ACTIVE_HIGH
|| f
== V4L2_MBUS_VSYNC_ACTIVE_LOW
)
202 flags
^= V4L2_MBUS_VSYNC_ACTIVE_HIGH
| V4L2_MBUS_VSYNC_ACTIVE_LOW
;
205 if (ssdd
->flags
& SOCAM_SENSOR_INVERT_PCLK
) {
206 f
= flags
& (V4L2_MBUS_PCLK_SAMPLE_RISING
| V4L2_MBUS_PCLK_SAMPLE_FALLING
);
207 if (f
== V4L2_MBUS_PCLK_SAMPLE_RISING
|| f
== V4L2_MBUS_PCLK_SAMPLE_FALLING
)
208 flags
^= V4L2_MBUS_PCLK_SAMPLE_RISING
| V4L2_MBUS_PCLK_SAMPLE_FALLING
;
213 EXPORT_SYMBOL(soc_camera_apply_board_flags
);
215 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
218 static int soc_camera_try_fmt(struct soc_camera_device
*icd
,
219 struct v4l2_format
*f
)
221 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
222 const struct soc_camera_format_xlate
*xlate
;
223 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
226 dev_dbg(icd
->pdev
, "TRY_FMT(%c%c%c%c, %ux%u)\n",
227 pixfmtstr(pix
->pixelformat
), pix
->width
, pix
->height
);
229 if (pix
->pixelformat
!= V4L2_PIX_FMT_JPEG
&&
230 !(ici
->capabilities
& SOCAM_HOST_CAP_STRIDE
)) {
231 pix
->bytesperline
= 0;
235 ret
= ici
->ops
->try_fmt(icd
, f
);
239 xlate
= soc_camera_xlate_by_fourcc(icd
, pix
->pixelformat
);
243 ret
= soc_mbus_bytes_per_line(pix
->width
, xlate
->host_fmt
);
247 pix
->bytesperline
= max_t(u32
, pix
->bytesperline
, ret
);
249 ret
= soc_mbus_image_size(xlate
->host_fmt
, pix
->bytesperline
,
254 pix
->sizeimage
= max_t(u32
, pix
->sizeimage
, ret
);
259 static int soc_camera_try_fmt_vid_cap(struct file
*file
, void *priv
,
260 struct v4l2_format
*f
)
262 struct soc_camera_device
*icd
= file
->private_data
;
264 WARN_ON(priv
!= file
->private_data
);
266 /* Only single-plane capture is supported so far */
267 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
270 /* limit format to hardware capabilities */
271 return soc_camera_try_fmt(icd
, f
);
274 static int soc_camera_enum_input(struct file
*file
, void *priv
,
275 struct v4l2_input
*inp
)
280 /* default is camera */
281 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
282 strcpy(inp
->name
, "Camera");
287 static int soc_camera_g_input(struct file
*file
, void *priv
, unsigned int *i
)
294 static int soc_camera_s_input(struct file
*file
, void *priv
, unsigned int i
)
302 static int soc_camera_s_std(struct file
*file
, void *priv
, v4l2_std_id a
)
304 struct soc_camera_device
*icd
= file
->private_data
;
305 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
307 return v4l2_subdev_call(sd
, core
, s_std
, a
);
310 static int soc_camera_g_std(struct file
*file
, void *priv
, v4l2_std_id
*a
)
312 struct soc_camera_device
*icd
= file
->private_data
;
313 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
315 return v4l2_subdev_call(sd
, core
, g_std
, a
);
318 static int soc_camera_enum_framesizes(struct file
*file
, void *fh
,
319 struct v4l2_frmsizeenum
*fsize
)
321 struct soc_camera_device
*icd
= file
->private_data
;
322 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
324 return ici
->ops
->enum_framesizes(icd
, fsize
);
327 static int soc_camera_reqbufs(struct file
*file
, void *priv
,
328 struct v4l2_requestbuffers
*p
)
331 struct soc_camera_device
*icd
= file
->private_data
;
332 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
334 WARN_ON(priv
!= file
->private_data
);
336 if (icd
->streamer
&& icd
->streamer
!= file
)
339 if (ici
->ops
->init_videobuf
) {
340 ret
= videobuf_reqbufs(&icd
->vb_vidq
, p
);
344 ret
= ici
->ops
->reqbufs(icd
, p
);
346 ret
= vb2_reqbufs(&icd
->vb2_vidq
, p
);
349 if (!ret
&& !icd
->streamer
)
350 icd
->streamer
= file
;
355 static int soc_camera_querybuf(struct file
*file
, void *priv
,
356 struct v4l2_buffer
*p
)
358 struct soc_camera_device
*icd
= file
->private_data
;
359 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
361 WARN_ON(priv
!= file
->private_data
);
363 if (ici
->ops
->init_videobuf
)
364 return videobuf_querybuf(&icd
->vb_vidq
, p
);
366 return vb2_querybuf(&icd
->vb2_vidq
, p
);
369 static int soc_camera_qbuf(struct file
*file
, void *priv
,
370 struct v4l2_buffer
*p
)
372 struct soc_camera_device
*icd
= file
->private_data
;
373 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
375 WARN_ON(priv
!= file
->private_data
);
377 if (icd
->streamer
!= file
)
380 if (ici
->ops
->init_videobuf
)
381 return videobuf_qbuf(&icd
->vb_vidq
, p
);
383 return vb2_qbuf(&icd
->vb2_vidq
, p
);
386 static int soc_camera_dqbuf(struct file
*file
, void *priv
,
387 struct v4l2_buffer
*p
)
389 struct soc_camera_device
*icd
= file
->private_data
;
390 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
392 WARN_ON(priv
!= file
->private_data
);
394 if (icd
->streamer
!= file
)
397 if (ici
->ops
->init_videobuf
)
398 return videobuf_dqbuf(&icd
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
400 return vb2_dqbuf(&icd
->vb2_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
403 static int soc_camera_create_bufs(struct file
*file
, void *priv
,
404 struct v4l2_create_buffers
*create
)
406 struct soc_camera_device
*icd
= file
->private_data
;
407 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
410 if (ici
->ops
->init_videobuf
)
413 return vb2_create_bufs(&icd
->vb2_vidq
, create
);
416 static int soc_camera_prepare_buf(struct file
*file
, void *priv
,
417 struct v4l2_buffer
*b
)
419 struct soc_camera_device
*icd
= file
->private_data
;
420 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
423 if (ici
->ops
->init_videobuf
)
426 return vb2_prepare_buf(&icd
->vb2_vidq
, b
);
429 /* Always entered with .host_lock held */
430 static int soc_camera_init_user_formats(struct soc_camera_device
*icd
)
432 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
433 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
434 unsigned int i
, fmts
= 0, raw_fmts
= 0;
436 enum v4l2_mbus_pixelcode code
;
438 while (!v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, raw_fmts
, &code
))
441 if (!ici
->ops
->get_formats
)
443 * Fallback mode - the host will have to serve all
444 * sensor-provided formats one-to-one to the user
449 * First pass - only count formats this host-sensor
450 * configuration can provide
452 for (i
= 0; i
< raw_fmts
; i
++) {
453 ret
= ici
->ops
->get_formats(icd
, i
, NULL
);
463 vmalloc(fmts
* sizeof(struct soc_camera_format_xlate
));
464 if (!icd
->user_formats
)
467 dev_dbg(icd
->pdev
, "Found %d supported formats.\n", fmts
);
469 /* Second pass - actually fill data formats */
471 for (i
= 0; i
< raw_fmts
; i
++)
472 if (!ici
->ops
->get_formats
) {
473 v4l2_subdev_call(sd
, video
, enum_mbus_fmt
, i
, &code
);
474 icd
->user_formats
[fmts
].host_fmt
=
475 soc_mbus_get_fmtdesc(code
);
476 if (icd
->user_formats
[fmts
].host_fmt
)
477 icd
->user_formats
[fmts
++].code
= code
;
479 ret
= ici
->ops
->get_formats(icd
, i
,
480 &icd
->user_formats
[fmts
]);
486 icd
->num_user_formats
= fmts
;
487 icd
->current_fmt
= &icd
->user_formats
[0];
492 vfree(icd
->user_formats
);
496 /* Always entered with .host_lock held */
497 static void soc_camera_free_user_formats(struct soc_camera_device
*icd
)
499 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
501 if (ici
->ops
->put_formats
)
502 ici
->ops
->put_formats(icd
);
503 icd
->current_fmt
= NULL
;
504 icd
->num_user_formats
= 0;
505 vfree(icd
->user_formats
);
506 icd
->user_formats
= NULL
;
509 /* Called with .vb_lock held, or from the first open(2), see comment there */
510 static int soc_camera_set_fmt(struct soc_camera_device
*icd
,
511 struct v4l2_format
*f
)
513 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
514 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
517 dev_dbg(icd
->pdev
, "S_FMT(%c%c%c%c, %ux%u)\n",
518 pixfmtstr(pix
->pixelformat
), pix
->width
, pix
->height
);
520 /* We always call try_fmt() before set_fmt() or set_crop() */
521 ret
= soc_camera_try_fmt(icd
, f
);
525 ret
= ici
->ops
->set_fmt(icd
, f
);
528 } else if (!icd
->current_fmt
||
529 icd
->current_fmt
->host_fmt
->fourcc
!= pix
->pixelformat
) {
531 "Host driver hasn't set up current format correctly!\n");
535 icd
->user_width
= pix
->width
;
536 icd
->user_height
= pix
->height
;
537 icd
->bytesperline
= pix
->bytesperline
;
538 icd
->sizeimage
= pix
->sizeimage
;
539 icd
->colorspace
= pix
->colorspace
;
540 icd
->field
= pix
->field
;
541 if (ici
->ops
->init_videobuf
)
542 icd
->vb_vidq
.field
= pix
->field
;
544 dev_dbg(icd
->pdev
, "set width: %d height: %d\n",
545 icd
->user_width
, icd
->user_height
);
547 /* set physical bus parameters */
548 return ici
->ops
->set_bus_param(icd
);
551 static int soc_camera_add_device(struct soc_camera_device
*icd
)
553 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
560 mutex_lock(&ici
->clk_lock
);
561 ret
= ici
->ops
->clock_start(ici
);
562 mutex_unlock(&ici
->clk_lock
);
568 ret
= ici
->ops
->add(icd
);
579 mutex_lock(&ici
->clk_lock
);
580 ici
->ops
->clock_stop(ici
);
581 mutex_unlock(&ici
->clk_lock
);
586 static void soc_camera_remove_device(struct soc_camera_device
*icd
)
588 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
590 if (WARN_ON(icd
!= ici
->icd
))
593 if (ici
->ops
->remove
)
594 ici
->ops
->remove(icd
);
596 mutex_lock(&ici
->clk_lock
);
597 ici
->ops
->clock_stop(ici
);
598 mutex_unlock(&ici
->clk_lock
);
603 static int soc_camera_open(struct file
*file
)
605 struct video_device
*vdev
= video_devdata(file
);
606 struct soc_camera_device
*icd
;
607 struct soc_camera_host
*ici
;
611 * Don't mess with the host during probe: wait until the loop in
612 * scan_add_host() completes. Also protect against a race with
613 * soc_camera_host_unregister().
615 if (mutex_lock_interruptible(&list_lock
))
618 if (!vdev
|| !video_is_registered(vdev
)) {
619 mutex_unlock(&list_lock
);
623 icd
= video_get_drvdata(vdev
);
624 ici
= to_soc_camera_host(icd
->parent
);
626 ret
= try_module_get(ici
->ops
->owner
) ? 0 : -ENODEV
;
627 mutex_unlock(&list_lock
);
630 dev_err(icd
->pdev
, "Couldn't lock capture bus driver.\n");
634 if (!to_soc_camera_control(icd
)) {
635 /* No device driver attached */
640 if (mutex_lock_interruptible(&ici
->host_lock
)) {
646 /* Now we really have to activate the camera */
647 if (icd
->use_count
== 1) {
648 struct soc_camera_desc
*sdesc
= to_soc_camera_desc(icd
);
649 /* Restore parameters before the last close() per V4L2 API */
650 struct v4l2_format f
= {
651 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
653 .width
= icd
->user_width
,
654 .height
= icd
->user_height
,
656 .colorspace
= icd
->colorspace
,
658 icd
->current_fmt
->host_fmt
->fourcc
,
662 /* The camera could have been already on, try to reset */
663 if (sdesc
->subdev_desc
.reset
)
664 sdesc
->subdev_desc
.reset(icd
->pdev
);
666 ret
= soc_camera_add_device(icd
);
668 dev_err(icd
->pdev
, "Couldn't activate the camera: %d\n", ret
);
672 ret
= __soc_camera_power_on(icd
);
676 pm_runtime_enable(&icd
->vdev
->dev
);
677 ret
= pm_runtime_resume(&icd
->vdev
->dev
);
678 if (ret
< 0 && ret
!= -ENOSYS
)
682 * Try to configure with default parameters. Notice: this is the
683 * very first open, so, we cannot race against other calls,
684 * apart from someone else calling open() simultaneously, but
685 * .host_lock is protecting us against it.
687 ret
= soc_camera_set_fmt(icd
, &f
);
691 if (ici
->ops
->init_videobuf
) {
692 ici
->ops
->init_videobuf(&icd
->vb_vidq
, icd
);
694 ret
= ici
->ops
->init_videobuf2(&icd
->vb2_vidq
, icd
);
698 v4l2_ctrl_handler_setup(&icd
->ctrl_handler
);
700 mutex_unlock(&ici
->host_lock
);
702 file
->private_data
= icd
;
703 dev_dbg(icd
->pdev
, "camera device open\n");
708 * All errors are entered with the .host_lock held, first four also
709 * with use_count == 1
713 pm_runtime_disable(&icd
->vdev
->dev
);
715 __soc_camera_power_off(icd
);
717 soc_camera_remove_device(icd
);
720 mutex_unlock(&ici
->host_lock
);
723 module_put(ici
->ops
->owner
);
728 static int soc_camera_close(struct file
*file
)
730 struct soc_camera_device
*icd
= file
->private_data
;
731 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
733 mutex_lock(&ici
->host_lock
);
735 if (!icd
->use_count
) {
736 pm_runtime_suspend(&icd
->vdev
->dev
);
737 pm_runtime_disable(&icd
->vdev
->dev
);
739 if (ici
->ops
->init_videobuf2
)
740 vb2_queue_release(&icd
->vb2_vidq
);
741 __soc_camera_power_off(icd
);
743 soc_camera_remove_device(icd
);
746 if (icd
->streamer
== file
)
747 icd
->streamer
= NULL
;
748 mutex_unlock(&ici
->host_lock
);
750 module_put(ici
->ops
->owner
);
752 dev_dbg(icd
->pdev
, "camera device close\n");
757 static ssize_t
soc_camera_read(struct file
*file
, char __user
*buf
,
758 size_t count
, loff_t
*ppos
)
760 struct soc_camera_device
*icd
= file
->private_data
;
761 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
763 dev_dbg(icd
->pdev
, "read called, buf %p\n", buf
);
765 if (ici
->ops
->init_videobuf2
&& icd
->vb2_vidq
.io_modes
& VB2_READ
)
766 return vb2_read(&icd
->vb2_vidq
, buf
, count
, ppos
,
767 file
->f_flags
& O_NONBLOCK
);
769 dev_err(icd
->pdev
, "camera device read not implemented\n");
774 static int soc_camera_mmap(struct file
*file
, struct vm_area_struct
*vma
)
776 struct soc_camera_device
*icd
= file
->private_data
;
777 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
780 dev_dbg(icd
->pdev
, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
782 if (icd
->streamer
!= file
)
785 if (mutex_lock_interruptible(&ici
->host_lock
))
787 if (ici
->ops
->init_videobuf
)
788 err
= videobuf_mmap_mapper(&icd
->vb_vidq
, vma
);
790 err
= vb2_mmap(&icd
->vb2_vidq
, vma
);
791 mutex_unlock(&ici
->host_lock
);
793 dev_dbg(icd
->pdev
, "vma start=0x%08lx, size=%ld, ret=%d\n",
794 (unsigned long)vma
->vm_start
,
795 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
801 static unsigned int soc_camera_poll(struct file
*file
, poll_table
*pt
)
803 struct soc_camera_device
*icd
= file
->private_data
;
804 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
805 unsigned res
= POLLERR
;
807 if (icd
->streamer
!= file
)
810 mutex_lock(&ici
->host_lock
);
811 if (ici
->ops
->init_videobuf
&& list_empty(&icd
->vb_vidq
.stream
))
812 dev_err(icd
->pdev
, "Trying to poll with no queued buffers!\n");
814 res
= ici
->ops
->poll(file
, pt
);
815 mutex_unlock(&ici
->host_lock
);
819 void soc_camera_lock(struct vb2_queue
*vq
)
821 struct soc_camera_device
*icd
= vb2_get_drv_priv(vq
);
822 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
823 mutex_lock(&ici
->host_lock
);
825 EXPORT_SYMBOL(soc_camera_lock
);
827 void soc_camera_unlock(struct vb2_queue
*vq
)
829 struct soc_camera_device
*icd
= vb2_get_drv_priv(vq
);
830 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
831 mutex_unlock(&ici
->host_lock
);
833 EXPORT_SYMBOL(soc_camera_unlock
);
835 static struct v4l2_file_operations soc_camera_fops
= {
836 .owner
= THIS_MODULE
,
837 .open
= soc_camera_open
,
838 .release
= soc_camera_close
,
839 .unlocked_ioctl
= video_ioctl2
,
840 .read
= soc_camera_read
,
841 .mmap
= soc_camera_mmap
,
842 .poll
= soc_camera_poll
,
845 static int soc_camera_s_fmt_vid_cap(struct file
*file
, void *priv
,
846 struct v4l2_format
*f
)
848 struct soc_camera_device
*icd
= file
->private_data
;
851 WARN_ON(priv
!= file
->private_data
);
853 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
854 dev_warn(icd
->pdev
, "Wrong buf-type %d\n", f
->type
);
858 if (icd
->streamer
&& icd
->streamer
!= file
)
861 if (is_streaming(to_soc_camera_host(icd
->parent
), icd
)) {
862 dev_err(icd
->pdev
, "S_FMT denied: queue initialised\n");
866 ret
= soc_camera_set_fmt(icd
, f
);
868 if (!ret
&& !icd
->streamer
)
869 icd
->streamer
= file
;
874 static int soc_camera_enum_fmt_vid_cap(struct file
*file
, void *priv
,
875 struct v4l2_fmtdesc
*f
)
877 struct soc_camera_device
*icd
= file
->private_data
;
878 const struct soc_mbus_pixelfmt
*format
;
880 WARN_ON(priv
!= file
->private_data
);
882 if (f
->index
>= icd
->num_user_formats
)
885 format
= icd
->user_formats
[f
->index
].host_fmt
;
888 strlcpy(f
->description
, format
->name
, sizeof(f
->description
));
889 f
->pixelformat
= format
->fourcc
;
893 static int soc_camera_g_fmt_vid_cap(struct file
*file
, void *priv
,
894 struct v4l2_format
*f
)
896 struct soc_camera_device
*icd
= file
->private_data
;
897 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
899 WARN_ON(priv
!= file
->private_data
);
901 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
904 pix
->width
= icd
->user_width
;
905 pix
->height
= icd
->user_height
;
906 pix
->bytesperline
= icd
->bytesperline
;
907 pix
->sizeimage
= icd
->sizeimage
;
908 pix
->field
= icd
->field
;
909 pix
->pixelformat
= icd
->current_fmt
->host_fmt
->fourcc
;
910 pix
->colorspace
= icd
->colorspace
;
911 dev_dbg(icd
->pdev
, "current_fmt->fourcc: 0x%08x\n",
912 icd
->current_fmt
->host_fmt
->fourcc
);
916 static int soc_camera_querycap(struct file
*file
, void *priv
,
917 struct v4l2_capability
*cap
)
919 struct soc_camera_device
*icd
= file
->private_data
;
920 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
922 WARN_ON(priv
!= file
->private_data
);
924 strlcpy(cap
->driver
, ici
->drv_name
, sizeof(cap
->driver
));
925 return ici
->ops
->querycap(ici
, cap
);
928 static int soc_camera_streamon(struct file
*file
, void *priv
,
929 enum v4l2_buf_type i
)
931 struct soc_camera_device
*icd
= file
->private_data
;
932 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
933 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
936 WARN_ON(priv
!= file
->private_data
);
938 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
941 if (icd
->streamer
!= file
)
944 /* This calls buf_queue from host driver's videobuf_queue_ops */
945 if (ici
->ops
->init_videobuf
)
946 ret
= videobuf_streamon(&icd
->vb_vidq
);
948 ret
= vb2_streamon(&icd
->vb2_vidq
, i
);
951 v4l2_subdev_call(sd
, video
, s_stream
, 1);
956 static int soc_camera_streamoff(struct file
*file
, void *priv
,
957 enum v4l2_buf_type i
)
959 struct soc_camera_device
*icd
= file
->private_data
;
960 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
961 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
963 WARN_ON(priv
!= file
->private_data
);
965 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
968 if (icd
->streamer
!= file
)
972 * This calls buf_release from host driver's videobuf_queue_ops for all
973 * remaining buffers. When the last buffer is freed, stop capture
975 if (ici
->ops
->init_videobuf
)
976 videobuf_streamoff(&icd
->vb_vidq
);
978 vb2_streamoff(&icd
->vb2_vidq
, i
);
980 v4l2_subdev_call(sd
, video
, s_stream
, 0);
985 static int soc_camera_cropcap(struct file
*file
, void *fh
,
986 struct v4l2_cropcap
*a
)
988 struct soc_camera_device
*icd
= file
->private_data
;
989 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
991 return ici
->ops
->cropcap(icd
, a
);
994 static int soc_camera_g_crop(struct file
*file
, void *fh
,
997 struct soc_camera_device
*icd
= file
->private_data
;
998 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1001 ret
= ici
->ops
->get_crop(icd
, a
);
1007 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
1008 * argument with the actual geometry, instead, the user shall use G_CROP to
1011 static int soc_camera_s_crop(struct file
*file
, void *fh
,
1012 const struct v4l2_crop
*a
)
1014 struct soc_camera_device
*icd
= file
->private_data
;
1015 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1016 const struct v4l2_rect
*rect
= &a
->c
;
1017 struct v4l2_crop current_crop
;
1020 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1023 dev_dbg(icd
->pdev
, "S_CROP(%ux%u@%u:%u)\n",
1024 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
1026 current_crop
.type
= a
->type
;
1028 /* If get_crop fails, we'll let host and / or client drivers decide */
1029 ret
= ici
->ops
->get_crop(icd
, ¤t_crop
);
1031 /* Prohibit window size change with initialised buffers */
1034 "S_CROP denied: getting current crop failed\n");
1035 } else if ((a
->c
.width
== current_crop
.c
.width
&&
1036 a
->c
.height
== current_crop
.c
.height
) ||
1037 !is_streaming(ici
, icd
)) {
1038 /* same size or not streaming - use .set_crop() */
1039 ret
= ici
->ops
->set_crop(icd
, a
);
1040 } else if (ici
->ops
->set_livecrop
) {
1041 ret
= ici
->ops
->set_livecrop(icd
, a
);
1044 "S_CROP denied: queue initialised and sizes differ\n");
1051 static int soc_camera_g_selection(struct file
*file
, void *fh
,
1052 struct v4l2_selection
*s
)
1054 struct soc_camera_device
*icd
= file
->private_data
;
1055 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1057 /* With a wrong type no need to try to fall back to cropping */
1058 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1061 if (!ici
->ops
->get_selection
)
1064 return ici
->ops
->get_selection(icd
, s
);
1067 static int soc_camera_s_selection(struct file
*file
, void *fh
,
1068 struct v4l2_selection
*s
)
1070 struct soc_camera_device
*icd
= file
->private_data
;
1071 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1074 /* In all these cases cropping emulation will not help */
1075 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1076 (s
->target
!= V4L2_SEL_TGT_COMPOSE
&&
1077 s
->target
!= V4L2_SEL_TGT_CROP
))
1080 if (s
->target
== V4L2_SEL_TGT_COMPOSE
) {
1081 /* No output size change during a running capture! */
1082 if (is_streaming(ici
, icd
) &&
1083 (icd
->user_width
!= s
->r
.width
||
1084 icd
->user_height
!= s
->r
.height
))
1088 * Only one user is allowed to change the output format, touch
1089 * buffers, start / stop streaming, poll for data
1091 if (icd
->streamer
&& icd
->streamer
!= file
)
1095 if (!ici
->ops
->set_selection
)
1098 ret
= ici
->ops
->set_selection(icd
, s
);
1100 s
->target
== V4L2_SEL_TGT_COMPOSE
) {
1101 icd
->user_width
= s
->r
.width
;
1102 icd
->user_height
= s
->r
.height
;
1104 icd
->streamer
= file
;
1110 static int soc_camera_g_parm(struct file
*file
, void *fh
,
1111 struct v4l2_streamparm
*a
)
1113 struct soc_camera_device
*icd
= file
->private_data
;
1114 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1116 if (ici
->ops
->get_parm
)
1117 return ici
->ops
->get_parm(icd
, a
);
1119 return -ENOIOCTLCMD
;
1122 static int soc_camera_s_parm(struct file
*file
, void *fh
,
1123 struct v4l2_streamparm
*a
)
1125 struct soc_camera_device
*icd
= file
->private_data
;
1126 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1128 if (ici
->ops
->set_parm
)
1129 return ici
->ops
->set_parm(icd
, a
);
1131 return -ENOIOCTLCMD
;
1134 static int soc_camera_probe(struct soc_camera_host
*ici
,
1135 struct soc_camera_device
*icd
);
1137 /* So far this function cannot fail */
1138 static void scan_add_host(struct soc_camera_host
*ici
)
1140 struct soc_camera_device
*icd
;
1142 mutex_lock(&list_lock
);
1144 list_for_each_entry(icd
, &devices
, list
)
1145 if (icd
->iface
== ici
->nr
) {
1146 struct soc_camera_desc
*sdesc
= to_soc_camera_desc(icd
);
1147 struct soc_camera_subdev_desc
*ssdd
= &sdesc
->subdev_desc
;
1149 /* The camera could have been already on, try to reset */
1151 ssdd
->reset(icd
->pdev
);
1153 icd
->parent
= ici
->v4l2_dev
.dev
;
1156 soc_camera_probe(ici
, icd
);
1159 mutex_unlock(&list_lock
);
1163 * It is invalid to call v4l2_clk_enable() after a successful probing
1164 * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
1166 static int soc_camera_clk_enable(struct v4l2_clk
*clk
)
1168 struct soc_camera_device
*icd
= clk
->priv
;
1169 struct soc_camera_host
*ici
;
1172 if (!icd
|| !icd
->parent
)
1175 ici
= to_soc_camera_host(icd
->parent
);
1177 if (!try_module_get(ici
->ops
->owner
))
1181 * If a different client is currently being probed, the host will tell
1184 mutex_lock(&ici
->clk_lock
);
1185 ret
= ici
->ops
->clock_start(ici
);
1186 mutex_unlock(&ici
->clk_lock
);
1190 static void soc_camera_clk_disable(struct v4l2_clk
*clk
)
1192 struct soc_camera_device
*icd
= clk
->priv
;
1193 struct soc_camera_host
*ici
;
1195 if (!icd
|| !icd
->parent
)
1198 ici
= to_soc_camera_host(icd
->parent
);
1200 mutex_lock(&ici
->clk_lock
);
1201 ici
->ops
->clock_stop(ici
);
1202 mutex_unlock(&ici
->clk_lock
);
1204 module_put(ici
->ops
->owner
);
1208 * Eventually, it would be more logical to make the respective host the clock
1209 * owner, but then we would have to copy this struct for each ici. Besides, it
1210 * would introduce the circular dependency problem, unless we port all client
1211 * drivers to release the clock, when not in use.
1213 static const struct v4l2_clk_ops soc_camera_clk_ops
= {
1214 .owner
= THIS_MODULE
,
1215 .enable
= soc_camera_clk_enable
,
1216 .disable
= soc_camera_clk_disable
,
1219 static int soc_camera_dyn_pdev(struct soc_camera_desc
*sdesc
,
1220 struct soc_camera_async_client
*sasc
)
1222 struct platform_device
*pdev
;
1225 mutex_lock(&list_lock
);
1226 i
= find_first_zero_bit(device_map
, MAP_MAX_NUM
);
1227 if (i
< MAP_MAX_NUM
)
1228 set_bit(i
, device_map
);
1229 mutex_unlock(&list_lock
);
1230 if (i
>= MAP_MAX_NUM
)
1233 pdev
= platform_device_alloc("soc-camera-pdrv", i
);
1237 ret
= platform_device_add_data(pdev
, sdesc
, sizeof(*sdesc
));
1239 platform_device_put(pdev
);
1248 static struct soc_camera_device
*soc_camera_add_pdev(struct soc_camera_async_client
*sasc
)
1250 struct platform_device
*pdev
= sasc
->pdev
;
1253 ret
= platform_device_add(pdev
);
1254 if (ret
< 0 || !pdev
->dev
.driver
)
1257 return platform_get_drvdata(pdev
);
1260 /* Locking: called with .host_lock held */
1261 static int soc_camera_probe_finish(struct soc_camera_device
*icd
)
1263 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1264 struct v4l2_mbus_framefmt mf
;
1267 sd
->grp_id
= soc_camera_grp_id(icd
);
1268 v4l2_set_subdev_hostdata(sd
, icd
);
1270 ret
= v4l2_ctrl_add_handler(&icd
->ctrl_handler
, sd
->ctrl_handler
, NULL
);
1274 ret
= soc_camera_add_device(icd
);
1276 dev_err(icd
->pdev
, "Couldn't activate the camera: %d\n", ret
);
1280 /* At this point client .probe() should have run already */
1281 ret
= soc_camera_init_user_formats(icd
);
1285 icd
->field
= V4L2_FIELD_ANY
;
1287 ret
= soc_camera_video_start(icd
);
1291 /* Try to improve our guess of a reasonable window format */
1292 if (!v4l2_subdev_call(sd
, video
, g_mbus_fmt
, &mf
)) {
1293 icd
->user_width
= mf
.width
;
1294 icd
->user_height
= mf
.height
;
1295 icd
->colorspace
= mf
.colorspace
;
1296 icd
->field
= mf
.field
;
1298 soc_camera_remove_device(icd
);
1303 soc_camera_free_user_formats(icd
);
1305 soc_camera_remove_device(icd
);
1310 #ifdef CONFIG_I2C_BOARDINFO
1311 static int soc_camera_i2c_init(struct soc_camera_device
*icd
,
1312 struct soc_camera_desc
*sdesc
)
1314 struct soc_camera_subdev_desc
*ssdd
;
1315 struct i2c_client
*client
;
1316 struct soc_camera_host
*ici
;
1317 struct soc_camera_host_desc
*shd
= &sdesc
->host_desc
;
1318 struct i2c_adapter
*adap
;
1319 struct v4l2_subdev
*subdev
;
1320 char clk_name
[V4L2_SUBDEV_NAME_SIZE
];
1323 /* First find out how we link the main client */
1325 /* Async non-OF probing handled by the subdevice list */
1326 return -EPROBE_DEFER
;
1329 ici
= to_soc_camera_host(icd
->parent
);
1330 adap
= i2c_get_adapter(shd
->i2c_adapter_id
);
1332 dev_err(icd
->pdev
, "Cannot get I2C adapter #%d. No driver?\n",
1333 shd
->i2c_adapter_id
);
1337 ssdd
= kzalloc(sizeof(*ssdd
), GFP_KERNEL
);
1343 memcpy(ssdd
, &sdesc
->subdev_desc
, sizeof(*ssdd
));
1345 * In synchronous case we request regulators ourselves in
1346 * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
1347 * to allocate them again.
1349 ssdd
->num_regulators
= 0;
1350 ssdd
->regulators
= NULL
;
1351 shd
->board_info
->platform_data
= ssdd
;
1353 snprintf(clk_name
, sizeof(clk_name
), "%d-%04x",
1354 shd
->i2c_adapter_id
, shd
->board_info
->addr
);
1356 icd
->clk
= v4l2_clk_register(&soc_camera_clk_ops
, clk_name
, "mclk", icd
);
1357 if (IS_ERR(icd
->clk
)) {
1358 ret
= PTR_ERR(icd
->clk
);
1362 subdev
= v4l2_i2c_new_subdev_board(&ici
->v4l2_dev
, adap
,
1363 shd
->board_info
, NULL
);
1369 client
= v4l2_get_subdevdata(subdev
);
1371 /* Use to_i2c_client(dev) to recover the i2c client */
1372 icd
->control
= &client
->dev
;
1376 v4l2_clk_unregister(icd
->clk
);
1381 i2c_put_adapter(adap
);
1385 static void soc_camera_i2c_free(struct soc_camera_device
*icd
)
1387 struct i2c_client
*client
=
1388 to_i2c_client(to_soc_camera_control(icd
));
1389 struct i2c_adapter
*adap
;
1390 struct soc_camera_subdev_desc
*ssdd
;
1392 icd
->control
= NULL
;
1396 adap
= client
->adapter
;
1397 ssdd
= client
->dev
.platform_data
;
1398 v4l2_device_unregister_subdev(i2c_get_clientdata(client
));
1399 i2c_unregister_device(client
);
1400 i2c_put_adapter(adap
);
1402 v4l2_clk_unregister(icd
->clk
);
1407 * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
1408 * internal global mutex, therefore cannot race against other asynchronous
1409 * events. Until notifier->complete() (soc_camera_async_complete()) is called,
1410 * the video device node is not registered and no V4L fops can occur. Unloading
1411 * of the host driver also calls a v4l2-async function, so also there we're
1414 static int soc_camera_async_bound(struct v4l2_async_notifier
*notifier
,
1415 struct v4l2_subdev
*sd
,
1416 struct v4l2_async_subdev
*asd
)
1418 struct soc_camera_async_client
*sasc
= container_of(notifier
,
1419 struct soc_camera_async_client
, notifier
);
1420 struct soc_camera_device
*icd
= platform_get_drvdata(sasc
->pdev
);
1422 if (asd
== sasc
->sensor
&& !WARN_ON(icd
->control
)) {
1423 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1426 * Only now we get subdevice-specific information like
1427 * regulators, flags, callbacks, etc.
1430 struct soc_camera_desc
*sdesc
= to_soc_camera_desc(icd
);
1431 struct soc_camera_subdev_desc
*ssdd
=
1432 soc_camera_i2c_to_desc(client
);
1434 memcpy(&sdesc
->subdev_desc
, ssdd
,
1435 sizeof(sdesc
->subdev_desc
));
1437 ssdd
->reset(icd
->pdev
);
1440 icd
->control
= &client
->dev
;
1447 static void soc_camera_async_unbind(struct v4l2_async_notifier
*notifier
,
1448 struct v4l2_subdev
*sd
,
1449 struct v4l2_async_subdev
*asd
)
1451 struct soc_camera_async_client
*sasc
= container_of(notifier
,
1452 struct soc_camera_async_client
, notifier
);
1453 struct soc_camera_device
*icd
= platform_get_drvdata(sasc
->pdev
);
1456 v4l2_clk_unregister(icd
->clk
);
1461 static int soc_camera_async_complete(struct v4l2_async_notifier
*notifier
)
1463 struct soc_camera_async_client
*sasc
= container_of(notifier
,
1464 struct soc_camera_async_client
, notifier
);
1465 struct soc_camera_device
*icd
= platform_get_drvdata(sasc
->pdev
);
1467 if (to_soc_camera_control(icd
)) {
1468 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1471 mutex_lock(&list_lock
);
1472 ret
= soc_camera_probe(ici
, icd
);
1473 mutex_unlock(&list_lock
);
1481 static int scan_async_group(struct soc_camera_host
*ici
,
1482 struct v4l2_async_subdev
**asd
, unsigned int size
)
1484 struct soc_camera_async_subdev
*sasd
;
1485 struct soc_camera_async_client
*sasc
;
1486 struct soc_camera_device
*icd
;
1487 struct soc_camera_desc sdesc
= {.host_desc
.bus_id
= ici
->nr
,};
1488 char clk_name
[V4L2_SUBDEV_NAME_SIZE
];
1492 /* First look for a sensor */
1493 for (i
= 0; i
< size
; i
++) {
1494 sasd
= container_of(asd
[i
], struct soc_camera_async_subdev
, asd
);
1495 if (sasd
->role
== SOCAM_SUBDEV_DATA_SOURCE
)
1499 if (i
>= size
|| asd
[i
]->match_type
!= V4L2_ASYNC_MATCH_I2C
) {
1501 dev_err(ici
->v4l2_dev
.dev
, "No I2C data source found!\n");
1505 /* Or shall this be managed by the soc-camera device? */
1506 sasc
= devm_kzalloc(ici
->v4l2_dev
.dev
, sizeof(*sasc
), GFP_KERNEL
);
1510 /* HACK: just need a != NULL */
1511 sdesc
.host_desc
.board_info
= ERR_PTR(-ENODATA
);
1513 ret
= soc_camera_dyn_pdev(&sdesc
, sasc
);
1517 sasc
->sensor
= &sasd
->asd
;
1519 icd
= soc_camera_add_pdev(sasc
);
1521 platform_device_put(sasc
->pdev
);
1525 sasc
->notifier
.subdevs
= asd
;
1526 sasc
->notifier
.num_subdevs
= size
;
1527 sasc
->notifier
.bound
= soc_camera_async_bound
;
1528 sasc
->notifier
.unbind
= soc_camera_async_unbind
;
1529 sasc
->notifier
.complete
= soc_camera_async_complete
;
1532 icd
->parent
= ici
->v4l2_dev
.dev
;
1534 snprintf(clk_name
, sizeof(clk_name
), "%d-%04x",
1535 sasd
->asd
.match
.i2c
.adapter_id
, sasd
->asd
.match
.i2c
.address
);
1537 icd
->clk
= v4l2_clk_register(&soc_camera_clk_ops
, clk_name
, "mclk", icd
);
1538 if (IS_ERR(icd
->clk
)) {
1539 ret
= PTR_ERR(icd
->clk
);
1543 ret
= v4l2_async_notifier_register(&ici
->v4l2_dev
, &sasc
->notifier
);
1547 v4l2_clk_unregister(icd
->clk
);
1550 platform_device_unregister(sasc
->pdev
);
1551 dev_err(ici
->v4l2_dev
.dev
, "group probe failed: %d\n", ret
);
1556 static void scan_async_host(struct soc_camera_host
*ici
)
1558 struct v4l2_async_subdev
**asd
;
1561 for (j
= 0, asd
= ici
->asd
; ici
->asd_sizes
[j
]; j
++) {
1562 scan_async_group(ici
, asd
, ici
->asd_sizes
[j
]);
1563 asd
+= ici
->asd_sizes
[j
];
1567 #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
1568 #define soc_camera_i2c_free(icd) do {} while (0)
1569 #define scan_async_host(ici) do {} while (0)
1572 /* Called during host-driver probe */
1573 static int soc_camera_probe(struct soc_camera_host
*ici
,
1574 struct soc_camera_device
*icd
)
1576 struct soc_camera_desc
*sdesc
= to_soc_camera_desc(icd
);
1577 struct soc_camera_host_desc
*shd
= &sdesc
->host_desc
;
1578 struct device
*control
= NULL
;
1581 dev_info(icd
->pdev
, "Probing %s\n", dev_name(icd
->pdev
));
1584 * Currently the subdev with the largest number of controls (13) is
1585 * ov6550. So let's pick 16 as a hint for the control handler. Note
1586 * that this is a hint only: too large and you waste some memory, too
1587 * small and there is a (very) small performance hit when looking up
1588 * controls in the internal hash.
1590 ret
= v4l2_ctrl_handler_init(&icd
->ctrl_handler
, 16);
1594 /* Must have icd->vdev before registering the device */
1595 ret
= video_dev_create(icd
);
1600 * ..._video_start() will create a device node, video_register_device()
1601 * itself is protected against concurrent open() calls, but we also have
1602 * to protect our data also during client probing.
1605 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1606 if (shd
->board_info
) {
1607 ret
= soc_camera_i2c_init(icd
, sdesc
);
1608 if (ret
< 0 && ret
!= -EPROBE_DEFER
)
1610 } else if (!shd
->add_device
|| !shd
->del_device
) {
1614 mutex_lock(&ici
->clk_lock
);
1615 ret
= ici
->ops
->clock_start(ici
);
1616 mutex_unlock(&ici
->clk_lock
);
1620 if (shd
->module_name
)
1621 ret
= request_module(shd
->module_name
);
1623 ret
= shd
->add_device(icd
);
1628 * FIXME: this is racy, have to use driver-binding notification,
1629 * when it is available
1631 control
= to_soc_camera_control(icd
);
1632 if (!control
|| !control
->driver
|| !dev_get_drvdata(control
) ||
1633 !try_module_get(control
->driver
->owner
)) {
1634 shd
->del_device(icd
);
1640 mutex_lock(&ici
->host_lock
);
1641 ret
= soc_camera_probe_finish(icd
);
1642 mutex_unlock(&ici
->host_lock
);
1649 if (shd
->board_info
) {
1650 soc_camera_i2c_free(icd
);
1652 shd
->del_device(icd
);
1653 module_put(control
->driver
->owner
);
1656 mutex_lock(&ici
->clk_lock
);
1657 ici
->ops
->clock_stop(ici
);
1658 mutex_unlock(&ici
->clk_lock
);
1661 video_device_release(icd
->vdev
);
1664 video_device_release(icd
->vdev
);
1668 v4l2_ctrl_handler_free(&icd
->ctrl_handler
);
1673 * This is called on device_unregister, which only means we have to disconnect
1674 * from the host, but not remove ourselves from the device list. With
1675 * asynchronous client probing this can also be called without
1676 * soc_camera_probe_finish() having run. Careful with clean up.
1678 static int soc_camera_remove(struct soc_camera_device
*icd
)
1680 struct soc_camera_desc
*sdesc
= to_soc_camera_desc(icd
);
1681 struct video_device
*vdev
= icd
->vdev
;
1683 v4l2_ctrl_handler_free(&icd
->ctrl_handler
);
1685 video_unregister_device(vdev
);
1689 if (sdesc
->host_desc
.board_info
) {
1690 soc_camera_i2c_free(icd
);
1692 struct device
*dev
= to_soc_camera_control(icd
);
1693 struct device_driver
*drv
= dev
? dev
->driver
: NULL
;
1695 sdesc
->host_desc
.del_device(icd
);
1696 module_put(drv
->owner
);
1700 if (icd
->num_user_formats
)
1701 soc_camera_free_user_formats(icd
);
1704 /* For the synchronous case */
1705 v4l2_clk_unregister(icd
->clk
);
1710 platform_device_unregister(icd
->sasc
->pdev
);
1715 static int default_cropcap(struct soc_camera_device
*icd
,
1716 struct v4l2_cropcap
*a
)
1718 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1719 return v4l2_subdev_call(sd
, video
, cropcap
, a
);
1722 static int default_g_crop(struct soc_camera_device
*icd
, struct v4l2_crop
*a
)
1724 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1725 return v4l2_subdev_call(sd
, video
, g_crop
, a
);
1728 static int default_s_crop(struct soc_camera_device
*icd
, const struct v4l2_crop
*a
)
1730 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1731 return v4l2_subdev_call(sd
, video
, s_crop
, a
);
1734 static int default_g_parm(struct soc_camera_device
*icd
,
1735 struct v4l2_streamparm
*parm
)
1737 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1738 return v4l2_subdev_call(sd
, video
, g_parm
, parm
);
1741 static int default_s_parm(struct soc_camera_device
*icd
,
1742 struct v4l2_streamparm
*parm
)
1744 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1745 return v4l2_subdev_call(sd
, video
, s_parm
, parm
);
1748 static int default_enum_framesizes(struct soc_camera_device
*icd
,
1749 struct v4l2_frmsizeenum
*fsize
)
1752 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1753 const struct soc_camera_format_xlate
*xlate
;
1754 __u32 pixfmt
= fsize
->pixel_format
;
1755 struct v4l2_frmsizeenum fsize_mbus
= *fsize
;
1757 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1760 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1761 fsize_mbus
.pixel_format
= xlate
->code
;
1763 ret
= v4l2_subdev_call(sd
, video
, enum_framesizes
, &fsize_mbus
);
1767 *fsize
= fsize_mbus
;
1768 fsize
->pixel_format
= pixfmt
;
1773 int soc_camera_host_register(struct soc_camera_host
*ici
)
1775 struct soc_camera_host
*ix
;
1778 if (!ici
|| !ici
->ops
||
1779 !ici
->ops
->try_fmt
||
1780 !ici
->ops
->set_fmt
||
1781 !ici
->ops
->set_bus_param
||
1782 !ici
->ops
->querycap
||
1783 ((!ici
->ops
->init_videobuf
||
1784 !ici
->ops
->reqbufs
) &&
1785 !ici
->ops
->init_videobuf2
) ||
1786 !ici
->ops
->clock_start
||
1787 !ici
->ops
->clock_stop
||
1792 if (!ici
->ops
->set_crop
)
1793 ici
->ops
->set_crop
= default_s_crop
;
1794 if (!ici
->ops
->get_crop
)
1795 ici
->ops
->get_crop
= default_g_crop
;
1796 if (!ici
->ops
->cropcap
)
1797 ici
->ops
->cropcap
= default_cropcap
;
1798 if (!ici
->ops
->set_parm
)
1799 ici
->ops
->set_parm
= default_s_parm
;
1800 if (!ici
->ops
->get_parm
)
1801 ici
->ops
->get_parm
= default_g_parm
;
1802 if (!ici
->ops
->enum_framesizes
)
1803 ici
->ops
->enum_framesizes
= default_enum_framesizes
;
1805 mutex_lock(&list_lock
);
1806 list_for_each_entry(ix
, &hosts
, list
) {
1807 if (ix
->nr
== ici
->nr
) {
1813 ret
= v4l2_device_register(ici
->v4l2_dev
.dev
, &ici
->v4l2_dev
);
1817 list_add_tail(&ici
->list
, &hosts
);
1818 mutex_unlock(&list_lock
);
1820 mutex_init(&ici
->host_lock
);
1821 mutex_init(&ici
->clk_lock
);
1825 * No OF, host with a list of subdevices. Don't try to mix
1826 * modes by initialising some groups statically and some
1829 scan_async_host(ici
);
1831 /* Legacy: static platform devices from board data */
1837 mutex_unlock(&list_lock
);
1840 EXPORT_SYMBOL(soc_camera_host_register
);
1842 /* Unregister all clients! */
1843 void soc_camera_host_unregister(struct soc_camera_host
*ici
)
1845 struct soc_camera_device
*icd
, *tmp
;
1846 struct soc_camera_async_client
*sasc
;
1847 LIST_HEAD(notifiers
);
1849 mutex_lock(&list_lock
);
1850 list_del(&ici
->list
);
1851 list_for_each_entry(icd
, &devices
, list
)
1852 if (icd
->iface
== ici
->nr
&& icd
->sasc
) {
1853 /* as long as we hold the device, sasc won't be freed */
1854 get_device(icd
->pdev
);
1855 list_add(&icd
->sasc
->list
, ¬ifiers
);
1857 mutex_unlock(&list_lock
);
1859 list_for_each_entry(sasc
, ¬ifiers
, list
) {
1860 /* Must call unlocked to avoid AB-BA dead-lock */
1861 v4l2_async_notifier_unregister(&sasc
->notifier
);
1862 put_device(&sasc
->pdev
->dev
);
1865 mutex_lock(&list_lock
);
1867 list_for_each_entry_safe(icd
, tmp
, &devices
, list
)
1868 if (icd
->iface
== ici
->nr
)
1869 soc_camera_remove(icd
);
1871 mutex_unlock(&list_lock
);
1873 v4l2_device_unregister(&ici
->v4l2_dev
);
1875 EXPORT_SYMBOL(soc_camera_host_unregister
);
1877 /* Image capture device */
1878 static int soc_camera_device_register(struct soc_camera_device
*icd
)
1880 struct soc_camera_device
*ix
;
1883 mutex_lock(&list_lock
);
1884 for (i
= 0; i
< 256 && num
< 0; i
++) {
1886 /* Check if this index is available on this interface */
1887 list_for_each_entry(ix
, &devices
, list
) {
1888 if (ix
->iface
== icd
->iface
&& ix
->devnum
== i
) {
1897 * ok, we have 256 cameras on this host...
1898 * man, stay reasonable...
1900 mutex_unlock(&list_lock
);
1906 icd
->host_priv
= NULL
;
1909 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
1912 i
= to_platform_device(icd
->pdev
)->id
;
1914 /* One static (legacy) soc-camera platform device */
1916 if (i
>= MAP_MAX_NUM
) {
1917 mutex_unlock(&list_lock
);
1920 set_bit(i
, device_map
);
1921 list_add_tail(&icd
->list
, &devices
);
1922 mutex_unlock(&list_lock
);
1927 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops
= {
1928 .vidioc_querycap
= soc_camera_querycap
,
1929 .vidioc_try_fmt_vid_cap
= soc_camera_try_fmt_vid_cap
,
1930 .vidioc_g_fmt_vid_cap
= soc_camera_g_fmt_vid_cap
,
1931 .vidioc_s_fmt_vid_cap
= soc_camera_s_fmt_vid_cap
,
1932 .vidioc_enum_fmt_vid_cap
= soc_camera_enum_fmt_vid_cap
,
1933 .vidioc_enum_input
= soc_camera_enum_input
,
1934 .vidioc_g_input
= soc_camera_g_input
,
1935 .vidioc_s_input
= soc_camera_s_input
,
1936 .vidioc_s_std
= soc_camera_s_std
,
1937 .vidioc_g_std
= soc_camera_g_std
,
1938 .vidioc_enum_framesizes
= soc_camera_enum_framesizes
,
1939 .vidioc_reqbufs
= soc_camera_reqbufs
,
1940 .vidioc_querybuf
= soc_camera_querybuf
,
1941 .vidioc_qbuf
= soc_camera_qbuf
,
1942 .vidioc_dqbuf
= soc_camera_dqbuf
,
1943 .vidioc_create_bufs
= soc_camera_create_bufs
,
1944 .vidioc_prepare_buf
= soc_camera_prepare_buf
,
1945 .vidioc_streamon
= soc_camera_streamon
,
1946 .vidioc_streamoff
= soc_camera_streamoff
,
1947 .vidioc_cropcap
= soc_camera_cropcap
,
1948 .vidioc_g_crop
= soc_camera_g_crop
,
1949 .vidioc_s_crop
= soc_camera_s_crop
,
1950 .vidioc_g_selection
= soc_camera_g_selection
,
1951 .vidioc_s_selection
= soc_camera_s_selection
,
1952 .vidioc_g_parm
= soc_camera_g_parm
,
1953 .vidioc_s_parm
= soc_camera_s_parm
,
1956 static int video_dev_create(struct soc_camera_device
*icd
)
1958 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1959 struct video_device
*vdev
= video_device_alloc();
1964 strlcpy(vdev
->name
, ici
->drv_name
, sizeof(vdev
->name
));
1966 vdev
->v4l2_dev
= &ici
->v4l2_dev
;
1967 vdev
->fops
= &soc_camera_fops
;
1968 vdev
->ioctl_ops
= &soc_camera_ioctl_ops
;
1969 vdev
->release
= video_device_release
;
1970 vdev
->ctrl_handler
= &icd
->ctrl_handler
;
1971 vdev
->lock
= &ici
->host_lock
;
1979 * Called from soc_camera_probe() above with .host_lock held
1981 static int soc_camera_video_start(struct soc_camera_device
*icd
)
1983 const struct device_type
*type
= icd
->vdev
->dev
.type
;
1989 video_set_drvdata(icd
->vdev
, icd
);
1990 ret
= video_register_device(icd
->vdev
, VFL_TYPE_GRABBER
, -1);
1992 dev_err(icd
->pdev
, "video_register_device failed: %d\n", ret
);
1996 /* Restore device type, possibly set by the subdevice driver */
1997 icd
->vdev
->dev
.type
= type
;
2002 static int soc_camera_pdrv_probe(struct platform_device
*pdev
)
2004 struct soc_camera_desc
*sdesc
= pdev
->dev
.platform_data
;
2005 struct soc_camera_subdev_desc
*ssdd
= &sdesc
->subdev_desc
;
2006 struct soc_camera_device
*icd
;
2012 icd
= devm_kzalloc(&pdev
->dev
, sizeof(*icd
), GFP_KERNEL
);
2017 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
2018 * regulator allocation is a dummy. They are actually requested by the
2019 * subdevice driver, using soc_camera_power_init(). Also note, that in
2020 * that case regulators are attached to the I2C device and not to the
2021 * camera platform device.
2023 ret
= devm_regulator_bulk_get(&pdev
->dev
, ssdd
->num_regulators
,
2028 icd
->iface
= sdesc
->host_desc
.bus_id
;
2030 icd
->pdev
= &pdev
->dev
;
2031 platform_set_drvdata(pdev
, icd
);
2033 icd
->user_width
= DEFAULT_WIDTH
;
2034 icd
->user_height
= DEFAULT_HEIGHT
;
2036 return soc_camera_device_register(icd
);
2040 * Only called on rmmod for each platform device, since they are not
2041 * hot-pluggable. Now we know, that all our users - hosts and devices have
2042 * been unloaded already
2044 static int soc_camera_pdrv_remove(struct platform_device
*pdev
)
2046 struct soc_camera_device
*icd
= platform_get_drvdata(pdev
);
2057 * In synchronous mode with static platform devices this is called in a
2058 * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
2059 * no need to lock. In asynchronous case the caller -
2060 * soc_camera_host_unregister() - already holds the lock
2062 if (test_bit(i
, device_map
)) {
2063 clear_bit(i
, device_map
);
2064 list_del(&icd
->list
);
2070 static struct platform_driver __refdata soc_camera_pdrv
= {
2071 .probe
= soc_camera_pdrv_probe
,
2072 .remove
= soc_camera_pdrv_remove
,
2074 .name
= "soc-camera-pdrv",
2075 .owner
= THIS_MODULE
,
2079 module_platform_driver(soc_camera_pdrv
);
2081 MODULE_DESCRIPTION("Image capture bus driver");
2082 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2083 MODULE_LICENSE("GPL");
2084 MODULE_ALIAS("platform:soc-camera-pdrv");