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/drv-intf/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/v4l2-of.h>
40 #include <media/videobuf-core.h>
41 #include <media/videobuf2-v4l2.h>
43 /* Default to VGA resolution */
44 #define DEFAULT_WIDTH 640
45 #define DEFAULT_HEIGHT 480
47 #define is_streaming(ici, icd) \
48 (((ici)->ops->init_videobuf) ? \
49 (icd)->vb_vidq.streaming : \
50 vb2_is_streaming(&(icd)->vb2_vidq))
52 #define MAP_MAX_NUM 32
53 static DECLARE_BITMAP(device_map
, MAP_MAX_NUM
);
54 static LIST_HEAD(hosts
);
55 static LIST_HEAD(devices
);
57 * Protects lists and bitmaps of hosts and devices.
58 * Lock nesting: Ok to take ->host_lock under list_lock.
60 static DEFINE_MUTEX(list_lock
);
62 struct soc_camera_async_client
{
63 struct v4l2_async_subdev
*sensor
;
64 struct v4l2_async_notifier notifier
;
65 struct platform_device
*pdev
;
66 struct list_head list
; /* needed for clean up */
69 static int soc_camera_video_start(struct soc_camera_device
*icd
);
70 static int video_dev_create(struct soc_camera_device
*icd
);
72 int soc_camera_power_on(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
,
78 if (clk
&& (!ssdd
->unbalanced_power
||
79 !test_and_set_bit(0, &ssdd
->clock_state
))) {
80 ret
= v4l2_clk_enable(clk
);
82 dev_err(dev
, "Cannot enable clock: %d\n", ret
);
90 ret
= regulator_bulk_enable(ssdd
->sd_pdata
.num_regulators
,
91 ssdd
->sd_pdata
.regulators
);
93 dev_err(dev
, "Cannot enable regulators\n");
98 ret
= ssdd
->power(dev
, 1);
101 "Platform failed to power-on the camera.\n");
109 regulator_bulk_disable(ssdd
->sd_pdata
.num_regulators
,
110 ssdd
->sd_pdata
.regulators
);
113 v4l2_clk_disable(clk
);
117 EXPORT_SYMBOL(soc_camera_power_on
);
119 int soc_camera_power_off(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
,
120 struct v4l2_clk
*clk
)
126 err
= ssdd
->power(dev
, 0);
129 "Platform failed to power-off the camera.\n");
134 err
= regulator_bulk_disable(ssdd
->sd_pdata
.num_regulators
,
135 ssdd
->sd_pdata
.regulators
);
137 dev_err(dev
, "Cannot disable regulators\n");
141 if (clk
&& (!ssdd
->unbalanced_power
|| test_and_clear_bit(0, &ssdd
->clock_state
)))
142 v4l2_clk_disable(clk
);
146 EXPORT_SYMBOL(soc_camera_power_off
);
148 int soc_camera_power_init(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
)
150 /* Should not have any effect in synchronous case */
151 return devm_regulator_bulk_get(dev
, ssdd
->sd_pdata
.num_regulators
,
152 ssdd
->sd_pdata
.regulators
);
154 EXPORT_SYMBOL(soc_camera_power_init
);
156 static int __soc_camera_power_on(struct soc_camera_device
*icd
)
158 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
161 ret
= v4l2_subdev_call(sd
, core
, s_power
, 1);
162 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
168 static int __soc_camera_power_off(struct soc_camera_device
*icd
)
170 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
173 ret
= v4l2_subdev_call(sd
, core
, s_power
, 0);
174 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
180 static int soc_camera_clock_start(struct soc_camera_host
*ici
)
184 if (!ici
->ops
->clock_start
)
187 mutex_lock(&ici
->clk_lock
);
188 ret
= ici
->ops
->clock_start(ici
);
189 mutex_unlock(&ici
->clk_lock
);
194 static void soc_camera_clock_stop(struct soc_camera_host
*ici
)
196 if (!ici
->ops
->clock_stop
)
199 mutex_lock(&ici
->clk_lock
);
200 ici
->ops
->clock_stop(ici
);
201 mutex_unlock(&ici
->clk_lock
);
204 const struct soc_camera_format_xlate
*soc_camera_xlate_by_fourcc(
205 struct soc_camera_device
*icd
, unsigned int fourcc
)
209 for (i
= 0; i
< icd
->num_user_formats
; i
++)
210 if (icd
->user_formats
[i
].host_fmt
->fourcc
== fourcc
)
211 return icd
->user_formats
+ i
;
214 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc
);
217 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
218 * @ssdd: camera platform parameters
219 * @cfg: media bus configuration
220 * @return: resulting flags
222 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc
*ssdd
,
223 const struct v4l2_mbus_config
*cfg
)
225 unsigned long f
, flags
= cfg
->flags
;
227 /* If only one of the two polarities is supported, switch to the opposite */
228 if (ssdd
->flags
& SOCAM_SENSOR_INVERT_HSYNC
) {
229 f
= flags
& (V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_HSYNC_ACTIVE_LOW
);
230 if (f
== V4L2_MBUS_HSYNC_ACTIVE_HIGH
|| f
== V4L2_MBUS_HSYNC_ACTIVE_LOW
)
231 flags
^= V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_HSYNC_ACTIVE_LOW
;
234 if (ssdd
->flags
& SOCAM_SENSOR_INVERT_VSYNC
) {
235 f
= flags
& (V4L2_MBUS_VSYNC_ACTIVE_HIGH
| V4L2_MBUS_VSYNC_ACTIVE_LOW
);
236 if (f
== V4L2_MBUS_VSYNC_ACTIVE_HIGH
|| f
== V4L2_MBUS_VSYNC_ACTIVE_LOW
)
237 flags
^= V4L2_MBUS_VSYNC_ACTIVE_HIGH
| V4L2_MBUS_VSYNC_ACTIVE_LOW
;
240 if (ssdd
->flags
& SOCAM_SENSOR_INVERT_PCLK
) {
241 f
= flags
& (V4L2_MBUS_PCLK_SAMPLE_RISING
| V4L2_MBUS_PCLK_SAMPLE_FALLING
);
242 if (f
== V4L2_MBUS_PCLK_SAMPLE_RISING
|| f
== V4L2_MBUS_PCLK_SAMPLE_FALLING
)
243 flags
^= V4L2_MBUS_PCLK_SAMPLE_RISING
| V4L2_MBUS_PCLK_SAMPLE_FALLING
;
248 EXPORT_SYMBOL(soc_camera_apply_board_flags
);
250 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
253 static int soc_camera_try_fmt(struct soc_camera_device
*icd
,
254 struct v4l2_format
*f
)
256 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
257 const struct soc_camera_format_xlate
*xlate
;
258 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
261 dev_dbg(icd
->pdev
, "TRY_FMT(%c%c%c%c, %ux%u)\n",
262 pixfmtstr(pix
->pixelformat
), pix
->width
, pix
->height
);
264 if (pix
->pixelformat
!= V4L2_PIX_FMT_JPEG
&&
265 !(ici
->capabilities
& SOCAM_HOST_CAP_STRIDE
)) {
266 pix
->bytesperline
= 0;
270 ret
= ici
->ops
->try_fmt(icd
, f
);
274 xlate
= soc_camera_xlate_by_fourcc(icd
, pix
->pixelformat
);
278 ret
= soc_mbus_bytes_per_line(pix
->width
, xlate
->host_fmt
);
282 pix
->bytesperline
= max_t(u32
, pix
->bytesperline
, ret
);
284 ret
= soc_mbus_image_size(xlate
->host_fmt
, pix
->bytesperline
,
289 pix
->sizeimage
= max_t(u32
, pix
->sizeimage
, ret
);
294 static int soc_camera_try_fmt_vid_cap(struct file
*file
, void *priv
,
295 struct v4l2_format
*f
)
297 struct soc_camera_device
*icd
= file
->private_data
;
299 WARN_ON(priv
!= file
->private_data
);
301 /* Only single-plane capture is supported so far */
302 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
305 /* limit format to hardware capabilities */
306 return soc_camera_try_fmt(icd
, f
);
309 static int soc_camera_enum_input(struct file
*file
, void *priv
,
310 struct v4l2_input
*inp
)
312 struct soc_camera_device
*icd
= file
->private_data
;
317 /* default is camera */
318 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
319 inp
->std
= icd
->vdev
->tvnorms
;
320 strcpy(inp
->name
, "Camera");
325 static int soc_camera_g_input(struct file
*file
, void *priv
, unsigned int *i
)
332 static int soc_camera_s_input(struct file
*file
, void *priv
, unsigned int i
)
340 static int soc_camera_s_std(struct file
*file
, void *priv
, v4l2_std_id a
)
342 struct soc_camera_device
*icd
= file
->private_data
;
343 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
345 return v4l2_subdev_call(sd
, video
, s_std
, a
);
348 static int soc_camera_g_std(struct file
*file
, void *priv
, v4l2_std_id
*a
)
350 struct soc_camera_device
*icd
= file
->private_data
;
351 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
353 return v4l2_subdev_call(sd
, video
, g_std
, a
);
356 static int soc_camera_enum_framesizes(struct file
*file
, void *fh
,
357 struct v4l2_frmsizeenum
*fsize
)
359 struct soc_camera_device
*icd
= file
->private_data
;
360 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
362 return ici
->ops
->enum_framesizes(icd
, fsize
);
365 static int soc_camera_reqbufs(struct file
*file
, void *priv
,
366 struct v4l2_requestbuffers
*p
)
369 struct soc_camera_device
*icd
= file
->private_data
;
370 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
372 WARN_ON(priv
!= file
->private_data
);
374 if (icd
->streamer
&& icd
->streamer
!= file
)
377 if (ici
->ops
->init_videobuf
) {
378 ret
= videobuf_reqbufs(&icd
->vb_vidq
, p
);
382 ret
= ici
->ops
->reqbufs(icd
, p
);
384 ret
= vb2_reqbufs(&icd
->vb2_vidq
, p
);
388 icd
->streamer
= p
->count
? file
: NULL
;
392 static int soc_camera_querybuf(struct file
*file
, void *priv
,
393 struct v4l2_buffer
*p
)
395 struct soc_camera_device
*icd
= file
->private_data
;
396 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
398 WARN_ON(priv
!= file
->private_data
);
400 if (ici
->ops
->init_videobuf
)
401 return videobuf_querybuf(&icd
->vb_vidq
, p
);
403 return vb2_querybuf(&icd
->vb2_vidq
, p
);
406 static int soc_camera_qbuf(struct file
*file
, void *priv
,
407 struct v4l2_buffer
*p
)
409 struct soc_camera_device
*icd
= file
->private_data
;
410 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
412 WARN_ON(priv
!= file
->private_data
);
414 if (icd
->streamer
!= file
)
417 if (ici
->ops
->init_videobuf
)
418 return videobuf_qbuf(&icd
->vb_vidq
, p
);
420 return vb2_qbuf(&icd
->vb2_vidq
, p
);
423 static int soc_camera_dqbuf(struct file
*file
, void *priv
,
424 struct v4l2_buffer
*p
)
426 struct soc_camera_device
*icd
= file
->private_data
;
427 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
429 WARN_ON(priv
!= file
->private_data
);
431 if (icd
->streamer
!= file
)
434 if (ici
->ops
->init_videobuf
)
435 return videobuf_dqbuf(&icd
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
437 return vb2_dqbuf(&icd
->vb2_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
440 static int soc_camera_create_bufs(struct file
*file
, void *priv
,
441 struct v4l2_create_buffers
*create
)
443 struct soc_camera_device
*icd
= file
->private_data
;
444 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
448 if (ici
->ops
->init_videobuf
)
451 if (icd
->streamer
&& icd
->streamer
!= file
)
454 ret
= vb2_create_bufs(&icd
->vb2_vidq
, create
);
456 icd
->streamer
= file
;
460 static int soc_camera_prepare_buf(struct file
*file
, void *priv
,
461 struct v4l2_buffer
*b
)
463 struct soc_camera_device
*icd
= file
->private_data
;
464 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
467 if (ici
->ops
->init_videobuf
)
470 return vb2_prepare_buf(&icd
->vb2_vidq
, b
);
473 static int soc_camera_expbuf(struct file
*file
, void *priv
,
474 struct v4l2_exportbuffer
*p
)
476 struct soc_camera_device
*icd
= file
->private_data
;
477 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
480 if (ici
->ops
->init_videobuf
)
483 if (icd
->streamer
&& icd
->streamer
!= file
)
485 return vb2_expbuf(&icd
->vb2_vidq
, p
);
488 /* Always entered with .host_lock held */
489 static int soc_camera_init_user_formats(struct soc_camera_device
*icd
)
491 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
492 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
493 unsigned int i
, fmts
= 0, raw_fmts
= 0;
495 struct v4l2_subdev_mbus_code_enum code
= {
496 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
499 while (!v4l2_subdev_call(sd
, pad
, enum_mbus_code
, NULL
, &code
)) {
504 if (!ici
->ops
->get_formats
)
506 * Fallback mode - the host will have to serve all
507 * sensor-provided formats one-to-one to the user
512 * First pass - only count formats this host-sensor
513 * configuration can provide
515 for (i
= 0; i
< raw_fmts
; i
++) {
516 ret
= ici
->ops
->get_formats(icd
, i
, NULL
);
526 vmalloc(fmts
* sizeof(struct soc_camera_format_xlate
));
527 if (!icd
->user_formats
)
530 dev_dbg(icd
->pdev
, "Found %d supported formats.\n", fmts
);
532 /* Second pass - actually fill data formats */
534 for (i
= 0; i
< raw_fmts
; i
++)
535 if (!ici
->ops
->get_formats
) {
537 v4l2_subdev_call(sd
, pad
, enum_mbus_code
, NULL
, &code
);
538 icd
->user_formats
[fmts
].host_fmt
=
539 soc_mbus_get_fmtdesc(code
.code
);
540 if (icd
->user_formats
[fmts
].host_fmt
)
541 icd
->user_formats
[fmts
++].code
= code
.code
;
543 ret
= ici
->ops
->get_formats(icd
, i
,
544 &icd
->user_formats
[fmts
]);
550 icd
->num_user_formats
= fmts
;
551 icd
->current_fmt
= &icd
->user_formats
[0];
556 vfree(icd
->user_formats
);
560 /* Always entered with .host_lock held */
561 static void soc_camera_free_user_formats(struct soc_camera_device
*icd
)
563 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
565 if (ici
->ops
->put_formats
)
566 ici
->ops
->put_formats(icd
);
567 icd
->current_fmt
= NULL
;
568 icd
->num_user_formats
= 0;
569 vfree(icd
->user_formats
);
570 icd
->user_formats
= NULL
;
573 /* Called with .vb_lock held, or from the first open(2), see comment there */
574 static int soc_camera_set_fmt(struct soc_camera_device
*icd
,
575 struct v4l2_format
*f
)
577 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
578 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
581 dev_dbg(icd
->pdev
, "S_FMT(%c%c%c%c, %ux%u)\n",
582 pixfmtstr(pix
->pixelformat
), pix
->width
, pix
->height
);
584 /* We always call try_fmt() before set_fmt() or set_selection() */
585 ret
= soc_camera_try_fmt(icd
, f
);
589 ret
= ici
->ops
->set_fmt(icd
, f
);
592 } else if (!icd
->current_fmt
||
593 icd
->current_fmt
->host_fmt
->fourcc
!= pix
->pixelformat
) {
595 "Host driver hasn't set up current format correctly!\n");
599 icd
->user_width
= pix
->width
;
600 icd
->user_height
= pix
->height
;
601 icd
->bytesperline
= pix
->bytesperline
;
602 icd
->sizeimage
= pix
->sizeimage
;
603 icd
->colorspace
= pix
->colorspace
;
604 icd
->field
= pix
->field
;
605 if (ici
->ops
->init_videobuf
)
606 icd
->vb_vidq
.field
= pix
->field
;
608 dev_dbg(icd
->pdev
, "set width: %d height: %d\n",
609 icd
->user_width
, icd
->user_height
);
611 /* set physical bus parameters */
612 return ici
->ops
->set_bus_param(icd
);
615 static int soc_camera_add_device(struct soc_camera_device
*icd
)
617 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
624 ret
= soc_camera_clock_start(ici
);
630 ret
= ici
->ops
->add(icd
);
641 soc_camera_clock_stop(ici
);
645 static void soc_camera_remove_device(struct soc_camera_device
*icd
)
647 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
649 if (WARN_ON(icd
!= ici
->icd
))
652 if (ici
->ops
->remove
)
653 ici
->ops
->remove(icd
);
655 soc_camera_clock_stop(ici
);
659 static int soc_camera_open(struct file
*file
)
661 struct video_device
*vdev
= video_devdata(file
);
662 struct soc_camera_device
*icd
;
663 struct soc_camera_host
*ici
;
667 * Don't mess with the host during probe: wait until the loop in
668 * scan_add_host() completes. Also protect against a race with
669 * soc_camera_host_unregister().
671 if (mutex_lock_interruptible(&list_lock
))
674 if (!vdev
|| !video_is_registered(vdev
)) {
675 mutex_unlock(&list_lock
);
679 icd
= video_get_drvdata(vdev
);
680 ici
= to_soc_camera_host(icd
->parent
);
682 ret
= try_module_get(ici
->ops
->owner
) ? 0 : -ENODEV
;
683 mutex_unlock(&list_lock
);
686 dev_err(icd
->pdev
, "Couldn't lock capture bus driver.\n");
690 if (!to_soc_camera_control(icd
)) {
691 /* No device driver attached */
696 if (mutex_lock_interruptible(&ici
->host_lock
)) {
702 /* Now we really have to activate the camera */
703 if (icd
->use_count
== 1) {
704 struct soc_camera_desc
*sdesc
= to_soc_camera_desc(icd
);
705 /* Restore parameters before the last close() per V4L2 API */
706 struct v4l2_format f
= {
707 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
709 .width
= icd
->user_width
,
710 .height
= icd
->user_height
,
712 .colorspace
= icd
->colorspace
,
714 icd
->current_fmt
->host_fmt
->fourcc
,
718 /* The camera could have been already on, try to reset */
719 if (sdesc
->subdev_desc
.reset
)
721 sdesc
->subdev_desc
.reset(icd
->control
);
723 ret
= soc_camera_add_device(icd
);
725 dev_err(icd
->pdev
, "Couldn't activate the camera: %d\n", ret
);
729 ret
= __soc_camera_power_on(icd
);
733 pm_runtime_enable(&icd
->vdev
->dev
);
734 ret
= pm_runtime_resume(&icd
->vdev
->dev
);
735 if (ret
< 0 && ret
!= -ENOSYS
)
739 * Try to configure with default parameters. Notice: this is the
740 * very first open, so, we cannot race against other calls,
741 * apart from someone else calling open() simultaneously, but
742 * .host_lock is protecting us against it.
744 ret
= soc_camera_set_fmt(icd
, &f
);
748 if (ici
->ops
->init_videobuf
) {
749 ici
->ops
->init_videobuf(&icd
->vb_vidq
, icd
);
751 ret
= ici
->ops
->init_videobuf2(&icd
->vb2_vidq
, icd
);
755 v4l2_ctrl_handler_setup(&icd
->ctrl_handler
);
757 mutex_unlock(&ici
->host_lock
);
759 file
->private_data
= icd
;
760 dev_dbg(icd
->pdev
, "camera device open\n");
765 * All errors are entered with the .host_lock held, first four also
766 * with use_count == 1
770 pm_runtime_disable(&icd
->vdev
->dev
);
772 __soc_camera_power_off(icd
);
774 soc_camera_remove_device(icd
);
777 mutex_unlock(&ici
->host_lock
);
780 module_put(ici
->ops
->owner
);
785 static int soc_camera_close(struct file
*file
)
787 struct soc_camera_device
*icd
= file
->private_data
;
788 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
790 mutex_lock(&ici
->host_lock
);
791 if (icd
->streamer
== file
) {
792 if (ici
->ops
->init_videobuf2
)
793 vb2_queue_release(&icd
->vb2_vidq
);
794 icd
->streamer
= NULL
;
797 if (!icd
->use_count
) {
798 pm_runtime_suspend(&icd
->vdev
->dev
);
799 pm_runtime_disable(&icd
->vdev
->dev
);
801 __soc_camera_power_off(icd
);
803 soc_camera_remove_device(icd
);
806 mutex_unlock(&ici
->host_lock
);
808 module_put(ici
->ops
->owner
);
810 dev_dbg(icd
->pdev
, "camera device close\n");
815 static ssize_t
soc_camera_read(struct file
*file
, char __user
*buf
,
816 size_t count
, loff_t
*ppos
)
818 struct soc_camera_device
*icd
= file
->private_data
;
819 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
821 dev_dbg(icd
->pdev
, "read called, buf %p\n", buf
);
823 if (ici
->ops
->init_videobuf2
&& icd
->vb2_vidq
.io_modes
& VB2_READ
)
824 return vb2_read(&icd
->vb2_vidq
, buf
, count
, ppos
,
825 file
->f_flags
& O_NONBLOCK
);
827 dev_err(icd
->pdev
, "camera device read not implemented\n");
832 static int soc_camera_mmap(struct file
*file
, struct vm_area_struct
*vma
)
834 struct soc_camera_device
*icd
= file
->private_data
;
835 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
838 dev_dbg(icd
->pdev
, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
840 if (icd
->streamer
!= file
)
843 if (mutex_lock_interruptible(&ici
->host_lock
))
845 if (ici
->ops
->init_videobuf
)
846 err
= videobuf_mmap_mapper(&icd
->vb_vidq
, vma
);
848 err
= vb2_mmap(&icd
->vb2_vidq
, vma
);
849 mutex_unlock(&ici
->host_lock
);
851 dev_dbg(icd
->pdev
, "vma start=0x%08lx, size=%ld, ret=%d\n",
852 (unsigned long)vma
->vm_start
,
853 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
859 static unsigned int soc_camera_poll(struct file
*file
, poll_table
*pt
)
861 struct soc_camera_device
*icd
= file
->private_data
;
862 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
863 unsigned res
= POLLERR
;
865 if (icd
->streamer
!= file
)
868 mutex_lock(&ici
->host_lock
);
869 if (ici
->ops
->init_videobuf
&& list_empty(&icd
->vb_vidq
.stream
))
870 dev_err(icd
->pdev
, "Trying to poll with no queued buffers!\n");
872 res
= ici
->ops
->poll(file
, pt
);
873 mutex_unlock(&ici
->host_lock
);
877 static struct v4l2_file_operations soc_camera_fops
= {
878 .owner
= THIS_MODULE
,
879 .open
= soc_camera_open
,
880 .release
= soc_camera_close
,
881 .unlocked_ioctl
= video_ioctl2
,
882 .read
= soc_camera_read
,
883 .mmap
= soc_camera_mmap
,
884 .poll
= soc_camera_poll
,
887 static int soc_camera_s_fmt_vid_cap(struct file
*file
, void *priv
,
888 struct v4l2_format
*f
)
890 struct soc_camera_device
*icd
= file
->private_data
;
893 WARN_ON(priv
!= file
->private_data
);
895 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
896 dev_warn(icd
->pdev
, "Wrong buf-type %d\n", f
->type
);
900 if (icd
->streamer
&& icd
->streamer
!= file
)
903 if (is_streaming(to_soc_camera_host(icd
->parent
), icd
)) {
904 dev_err(icd
->pdev
, "S_FMT denied: queue initialised\n");
908 ret
= soc_camera_set_fmt(icd
, f
);
910 if (!ret
&& !icd
->streamer
)
911 icd
->streamer
= file
;
916 static int soc_camera_enum_fmt_vid_cap(struct file
*file
, void *priv
,
917 struct v4l2_fmtdesc
*f
)
919 struct soc_camera_device
*icd
= file
->private_data
;
920 const struct soc_mbus_pixelfmt
*format
;
922 WARN_ON(priv
!= file
->private_data
);
924 if (f
->index
>= icd
->num_user_formats
)
927 format
= icd
->user_formats
[f
->index
].host_fmt
;
930 strlcpy(f
->description
, format
->name
, sizeof(f
->description
));
931 f
->pixelformat
= format
->fourcc
;
935 static int soc_camera_g_fmt_vid_cap(struct file
*file
, void *priv
,
936 struct v4l2_format
*f
)
938 struct soc_camera_device
*icd
= file
->private_data
;
939 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
941 WARN_ON(priv
!= file
->private_data
);
943 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
946 pix
->width
= icd
->user_width
;
947 pix
->height
= icd
->user_height
;
948 pix
->bytesperline
= icd
->bytesperline
;
949 pix
->sizeimage
= icd
->sizeimage
;
950 pix
->field
= icd
->field
;
951 pix
->pixelformat
= icd
->current_fmt
->host_fmt
->fourcc
;
952 pix
->colorspace
= icd
->colorspace
;
953 dev_dbg(icd
->pdev
, "current_fmt->fourcc: 0x%08x\n",
954 icd
->current_fmt
->host_fmt
->fourcc
);
958 static int soc_camera_querycap(struct file
*file
, void *priv
,
959 struct v4l2_capability
*cap
)
961 struct soc_camera_device
*icd
= file
->private_data
;
962 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
964 WARN_ON(priv
!= file
->private_data
);
966 strlcpy(cap
->driver
, ici
->drv_name
, sizeof(cap
->driver
));
967 return ici
->ops
->querycap(ici
, cap
);
970 static int soc_camera_streamon(struct file
*file
, void *priv
,
971 enum v4l2_buf_type i
)
973 struct soc_camera_device
*icd
= file
->private_data
;
974 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
975 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
978 WARN_ON(priv
!= file
->private_data
);
980 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
983 if (icd
->streamer
!= file
)
986 /* This calls buf_queue from host driver's videobuf_queue_ops */
987 if (ici
->ops
->init_videobuf
)
988 ret
= videobuf_streamon(&icd
->vb_vidq
);
990 ret
= vb2_streamon(&icd
->vb2_vidq
, i
);
993 v4l2_subdev_call(sd
, video
, s_stream
, 1);
998 static int soc_camera_streamoff(struct file
*file
, void *priv
,
999 enum v4l2_buf_type i
)
1001 struct soc_camera_device
*icd
= file
->private_data
;
1002 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1003 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1006 WARN_ON(priv
!= file
->private_data
);
1008 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1011 if (icd
->streamer
!= file
)
1015 * This calls buf_release from host driver's videobuf_queue_ops for all
1016 * remaining buffers. When the last buffer is freed, stop capture
1018 if (ici
->ops
->init_videobuf
)
1019 ret
= videobuf_streamoff(&icd
->vb_vidq
);
1021 ret
= vb2_streamoff(&icd
->vb2_vidq
, i
);
1023 v4l2_subdev_call(sd
, video
, s_stream
, 0);
1028 static int soc_camera_g_selection(struct file
*file
, void *fh
,
1029 struct v4l2_selection
*s
)
1031 struct soc_camera_device
*icd
= file
->private_data
;
1032 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1034 /* With a wrong type no need to try to fall back to cropping */
1035 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1038 return ici
->ops
->get_selection(icd
, s
);
1041 static int soc_camera_s_selection(struct file
*file
, void *fh
,
1042 struct v4l2_selection
*s
)
1044 struct soc_camera_device
*icd
= file
->private_data
;
1045 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1048 /* In all these cases cropping emulation will not help */
1049 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1050 (s
->target
!= V4L2_SEL_TGT_COMPOSE
&&
1051 s
->target
!= V4L2_SEL_TGT_CROP
))
1054 if (s
->target
== V4L2_SEL_TGT_COMPOSE
) {
1055 /* No output size change during a running capture! */
1056 if (is_streaming(ici
, icd
) &&
1057 (icd
->user_width
!= s
->r
.width
||
1058 icd
->user_height
!= s
->r
.height
))
1062 * Only one user is allowed to change the output format, touch
1063 * buffers, start / stop streaming, poll for data
1065 if (icd
->streamer
&& icd
->streamer
!= file
)
1069 if (s
->target
== V4L2_SEL_TGT_CROP
&& is_streaming(ici
, icd
) &&
1070 ici
->ops
->set_liveselection
)
1071 ret
= ici
->ops
->set_liveselection(icd
, s
);
1073 ret
= ici
->ops
->set_selection(icd
, s
);
1075 s
->target
== V4L2_SEL_TGT_COMPOSE
) {
1076 icd
->user_width
= s
->r
.width
;
1077 icd
->user_height
= s
->r
.height
;
1079 icd
->streamer
= file
;
1085 static int soc_camera_g_parm(struct file
*file
, void *fh
,
1086 struct v4l2_streamparm
*a
)
1088 struct soc_camera_device
*icd
= file
->private_data
;
1089 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1091 if (ici
->ops
->get_parm
)
1092 return ici
->ops
->get_parm(icd
, a
);
1094 return -ENOIOCTLCMD
;
1097 static int soc_camera_s_parm(struct file
*file
, void *fh
,
1098 struct v4l2_streamparm
*a
)
1100 struct soc_camera_device
*icd
= file
->private_data
;
1101 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1103 if (ici
->ops
->set_parm
)
1104 return ici
->ops
->set_parm(icd
, a
);
1106 return -ENOIOCTLCMD
;
1109 static int soc_camera_probe(struct soc_camera_host
*ici
,
1110 struct soc_camera_device
*icd
);
1112 /* So far this function cannot fail */
1113 static void scan_add_host(struct soc_camera_host
*ici
)
1115 struct soc_camera_device
*icd
;
1117 mutex_lock(&list_lock
);
1119 list_for_each_entry(icd
, &devices
, list
)
1120 if (icd
->iface
== ici
->nr
) {
1121 struct soc_camera_desc
*sdesc
= to_soc_camera_desc(icd
);
1122 struct soc_camera_subdev_desc
*ssdd
= &sdesc
->subdev_desc
;
1124 /* The camera could have been already on, try to reset */
1127 ssdd
->reset(icd
->control
);
1129 icd
->parent
= ici
->v4l2_dev
.dev
;
1132 soc_camera_probe(ici
, icd
);
1135 mutex_unlock(&list_lock
);
1139 * It is invalid to call v4l2_clk_enable() after a successful probing
1140 * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
1142 static int soc_camera_clk_enable(struct v4l2_clk
*clk
)
1144 struct soc_camera_device
*icd
= clk
->priv
;
1145 struct soc_camera_host
*ici
;
1147 if (!icd
|| !icd
->parent
)
1150 ici
= to_soc_camera_host(icd
->parent
);
1152 if (!try_module_get(ici
->ops
->owner
))
1156 * If a different client is currently being probed, the host will tell
1159 return soc_camera_clock_start(ici
);
1162 static void soc_camera_clk_disable(struct v4l2_clk
*clk
)
1164 struct soc_camera_device
*icd
= clk
->priv
;
1165 struct soc_camera_host
*ici
;
1167 if (!icd
|| !icd
->parent
)
1170 ici
= to_soc_camera_host(icd
->parent
);
1172 soc_camera_clock_stop(ici
);
1174 module_put(ici
->ops
->owner
);
1178 * Eventually, it would be more logical to make the respective host the clock
1179 * owner, but then we would have to copy this struct for each ici. Besides, it
1180 * would introduce the circular dependency problem, unless we port all client
1181 * drivers to release the clock, when not in use.
1183 static const struct v4l2_clk_ops soc_camera_clk_ops
= {
1184 .owner
= THIS_MODULE
,
1185 .enable
= soc_camera_clk_enable
,
1186 .disable
= soc_camera_clk_disable
,
1189 static int soc_camera_dyn_pdev(struct soc_camera_desc
*sdesc
,
1190 struct soc_camera_async_client
*sasc
)
1192 struct platform_device
*pdev
;
1195 mutex_lock(&list_lock
);
1196 i
= find_first_zero_bit(device_map
, MAP_MAX_NUM
);
1197 if (i
< MAP_MAX_NUM
)
1198 set_bit(i
, device_map
);
1199 mutex_unlock(&list_lock
);
1200 if (i
>= MAP_MAX_NUM
)
1203 pdev
= platform_device_alloc("soc-camera-pdrv", i
);
1207 ret
= platform_device_add_data(pdev
, sdesc
, sizeof(*sdesc
));
1209 platform_device_put(pdev
);
1218 static struct soc_camera_device
*soc_camera_add_pdev(struct soc_camera_async_client
*sasc
)
1220 struct platform_device
*pdev
= sasc
->pdev
;
1223 ret
= platform_device_add(pdev
);
1224 if (ret
< 0 || !pdev
->dev
.driver
)
1227 return platform_get_drvdata(pdev
);
1230 /* Locking: called with .host_lock held */
1231 static int soc_camera_probe_finish(struct soc_camera_device
*icd
)
1233 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1234 struct v4l2_subdev_format fmt
= {
1235 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1237 struct v4l2_mbus_framefmt
*mf
= &fmt
.format
;
1240 sd
->grp_id
= soc_camera_grp_id(icd
);
1241 v4l2_set_subdev_hostdata(sd
, icd
);
1243 v4l2_subdev_call(sd
, video
, g_tvnorms
, &icd
->vdev
->tvnorms
);
1245 ret
= v4l2_ctrl_add_handler(&icd
->ctrl_handler
, sd
->ctrl_handler
, NULL
);
1249 ret
= soc_camera_add_device(icd
);
1251 dev_err(icd
->pdev
, "Couldn't activate the camera: %d\n", ret
);
1255 /* At this point client .probe() should have run already */
1256 ret
= soc_camera_init_user_formats(icd
);
1260 icd
->field
= V4L2_FIELD_ANY
;
1262 ret
= soc_camera_video_start(icd
);
1266 /* Try to improve our guess of a reasonable window format */
1267 if (!v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &fmt
)) {
1268 icd
->user_width
= mf
->width
;
1269 icd
->user_height
= mf
->height
;
1270 icd
->colorspace
= mf
->colorspace
;
1271 icd
->field
= mf
->field
;
1273 soc_camera_remove_device(icd
);
1278 soc_camera_free_user_formats(icd
);
1280 soc_camera_remove_device(icd
);
1285 #ifdef CONFIG_I2C_BOARDINFO
1286 static int soc_camera_i2c_init(struct soc_camera_device
*icd
,
1287 struct soc_camera_desc
*sdesc
)
1289 struct soc_camera_subdev_desc
*ssdd
;
1290 struct i2c_client
*client
;
1291 struct soc_camera_host
*ici
;
1292 struct soc_camera_host_desc
*shd
= &sdesc
->host_desc
;
1293 struct i2c_adapter
*adap
;
1294 struct v4l2_subdev
*subdev
;
1295 char clk_name
[V4L2_CLK_NAME_SIZE
];
1298 /* First find out how we link the main client */
1300 /* Async non-OF probing handled by the subdevice list */
1301 return -EPROBE_DEFER
;
1304 ici
= to_soc_camera_host(icd
->parent
);
1305 adap
= i2c_get_adapter(shd
->i2c_adapter_id
);
1307 dev_err(icd
->pdev
, "Cannot get I2C adapter #%d. No driver?\n",
1308 shd
->i2c_adapter_id
);
1312 ssdd
= kmemdup(&sdesc
->subdev_desc
, sizeof(*ssdd
), GFP_KERNEL
);
1318 * In synchronous case we request regulators ourselves in
1319 * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
1320 * to allocate them again.
1322 ssdd
->sd_pdata
.num_regulators
= 0;
1323 ssdd
->sd_pdata
.regulators
= NULL
;
1324 shd
->board_info
->platform_data
= ssdd
;
1326 v4l2_clk_name_i2c(clk_name
, sizeof(clk_name
),
1327 shd
->i2c_adapter_id
, shd
->board_info
->addr
);
1329 icd
->clk
= v4l2_clk_register(&soc_camera_clk_ops
, clk_name
, icd
);
1330 if (IS_ERR(icd
->clk
)) {
1331 ret
= PTR_ERR(icd
->clk
);
1335 subdev
= v4l2_i2c_new_subdev_board(&ici
->v4l2_dev
, adap
,
1336 shd
->board_info
, NULL
);
1342 client
= v4l2_get_subdevdata(subdev
);
1344 /* Use to_i2c_client(dev) to recover the i2c client */
1345 icd
->control
= &client
->dev
;
1349 v4l2_clk_unregister(icd
->clk
);
1354 i2c_put_adapter(adap
);
1358 static void soc_camera_i2c_free(struct soc_camera_device
*icd
)
1360 struct i2c_client
*client
=
1361 to_i2c_client(to_soc_camera_control(icd
));
1362 struct i2c_adapter
*adap
;
1363 struct soc_camera_subdev_desc
*ssdd
;
1365 icd
->control
= NULL
;
1369 adap
= client
->adapter
;
1370 ssdd
= client
->dev
.platform_data
;
1371 v4l2_device_unregister_subdev(i2c_get_clientdata(client
));
1372 i2c_unregister_device(client
);
1373 i2c_put_adapter(adap
);
1375 v4l2_clk_unregister(icd
->clk
);
1380 * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
1381 * internal global mutex, therefore cannot race against other asynchronous
1382 * events. Until notifier->complete() (soc_camera_async_complete()) is called,
1383 * the video device node is not registered and no V4L fops can occur. Unloading
1384 * of the host driver also calls a v4l2-async function, so also there we're
1387 static int soc_camera_async_bound(struct v4l2_async_notifier
*notifier
,
1388 struct v4l2_subdev
*sd
,
1389 struct v4l2_async_subdev
*asd
)
1391 struct soc_camera_async_client
*sasc
= container_of(notifier
,
1392 struct soc_camera_async_client
, notifier
);
1393 struct soc_camera_device
*icd
= platform_get_drvdata(sasc
->pdev
);
1395 if (asd
== sasc
->sensor
&& !WARN_ON(icd
->control
)) {
1396 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1399 * Only now we get subdevice-specific information like
1400 * regulators, flags, callbacks, etc.
1403 struct soc_camera_desc
*sdesc
= to_soc_camera_desc(icd
);
1404 struct soc_camera_subdev_desc
*ssdd
=
1405 soc_camera_i2c_to_desc(client
);
1407 memcpy(&sdesc
->subdev_desc
, ssdd
,
1408 sizeof(sdesc
->subdev_desc
));
1410 ssdd
->reset(&client
->dev
);
1413 icd
->control
= &client
->dev
;
1420 static void soc_camera_async_unbind(struct v4l2_async_notifier
*notifier
,
1421 struct v4l2_subdev
*sd
,
1422 struct v4l2_async_subdev
*asd
)
1424 struct soc_camera_async_client
*sasc
= container_of(notifier
,
1425 struct soc_camera_async_client
, notifier
);
1426 struct soc_camera_device
*icd
= platform_get_drvdata(sasc
->pdev
);
1428 icd
->control
= NULL
;
1431 v4l2_clk_unregister(icd
->clk
);
1436 static int soc_camera_async_complete(struct v4l2_async_notifier
*notifier
)
1438 struct soc_camera_async_client
*sasc
= container_of(notifier
,
1439 struct soc_camera_async_client
, notifier
);
1440 struct soc_camera_device
*icd
= platform_get_drvdata(sasc
->pdev
);
1442 if (to_soc_camera_control(icd
)) {
1443 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
1446 mutex_lock(&list_lock
);
1447 ret
= soc_camera_probe(ici
, icd
);
1448 mutex_unlock(&list_lock
);
1456 static int scan_async_group(struct soc_camera_host
*ici
,
1457 struct v4l2_async_subdev
**asd
, unsigned int size
)
1459 struct soc_camera_async_subdev
*sasd
;
1460 struct soc_camera_async_client
*sasc
;
1461 struct soc_camera_device
*icd
;
1462 struct soc_camera_desc sdesc
= {.host_desc
.bus_id
= ici
->nr
,};
1463 char clk_name
[V4L2_CLK_NAME_SIZE
];
1467 /* First look for a sensor */
1468 for (i
= 0; i
< size
; i
++) {
1469 sasd
= container_of(asd
[i
], struct soc_camera_async_subdev
, asd
);
1470 if (sasd
->role
== SOCAM_SUBDEV_DATA_SOURCE
)
1474 if (i
>= size
|| asd
[i
]->match_type
!= V4L2_ASYNC_MATCH_I2C
) {
1476 dev_err(ici
->v4l2_dev
.dev
, "No I2C data source found!\n");
1480 /* Or shall this be managed by the soc-camera device? */
1481 sasc
= devm_kzalloc(ici
->v4l2_dev
.dev
, sizeof(*sasc
), GFP_KERNEL
);
1485 /* HACK: just need a != NULL */
1486 sdesc
.host_desc
.board_info
= ERR_PTR(-ENODATA
);
1488 ret
= soc_camera_dyn_pdev(&sdesc
, sasc
);
1492 sasc
->sensor
= &sasd
->asd
;
1494 icd
= soc_camera_add_pdev(sasc
);
1500 sasc
->notifier
.subdevs
= asd
;
1501 sasc
->notifier
.num_subdevs
= size
;
1502 sasc
->notifier
.bound
= soc_camera_async_bound
;
1503 sasc
->notifier
.unbind
= soc_camera_async_unbind
;
1504 sasc
->notifier
.complete
= soc_camera_async_complete
;
1507 icd
->parent
= ici
->v4l2_dev
.dev
;
1509 v4l2_clk_name_i2c(clk_name
, sizeof(clk_name
),
1510 sasd
->asd
.match
.i2c
.adapter_id
,
1511 sasd
->asd
.match
.i2c
.address
);
1513 icd
->clk
= v4l2_clk_register(&soc_camera_clk_ops
, clk_name
, icd
);
1514 if (IS_ERR(icd
->clk
)) {
1515 ret
= PTR_ERR(icd
->clk
);
1519 ret
= v4l2_async_notifier_register(&ici
->v4l2_dev
, &sasc
->notifier
);
1523 v4l2_clk_unregister(icd
->clk
);
1526 platform_device_del(sasc
->pdev
);
1528 platform_device_put(sasc
->pdev
);
1530 devm_kfree(ici
->v4l2_dev
.dev
, sasc
);
1531 dev_err(ici
->v4l2_dev
.dev
, "group probe failed: %d\n", ret
);
1536 static void scan_async_host(struct soc_camera_host
*ici
)
1538 struct v4l2_async_subdev
**asd
;
1541 for (j
= 0, asd
= ici
->asd
; ici
->asd_sizes
[j
]; j
++) {
1542 scan_async_group(ici
, asd
, ici
->asd_sizes
[j
]);
1543 asd
+= ici
->asd_sizes
[j
];
1547 #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
1548 #define soc_camera_i2c_free(icd) do {} while (0)
1549 #define scan_async_host(ici) do {} while (0)
1554 struct soc_of_info
{
1555 struct soc_camera_async_subdev sasd
;
1556 struct soc_camera_async_client sasc
;
1557 struct v4l2_async_subdev
*subdev
;
1560 static int soc_of_bind(struct soc_camera_host
*ici
,
1561 struct device_node
*ep
,
1562 struct device_node
*remote
)
1564 struct soc_camera_device
*icd
;
1565 struct soc_camera_desc sdesc
= {.host_desc
.bus_id
= ici
->nr
,};
1566 struct soc_camera_async_client
*sasc
;
1567 struct soc_of_info
*info
;
1568 struct i2c_client
*client
;
1569 char clk_name
[V4L2_CLK_NAME_SIZE
];
1572 /* allocate a new subdev and add match info to it */
1573 info
= devm_kzalloc(ici
->v4l2_dev
.dev
, sizeof(struct soc_of_info
),
1578 info
->sasd
.asd
.match
.of
.node
= remote
;
1579 info
->sasd
.asd
.match_type
= V4L2_ASYNC_MATCH_OF
;
1580 info
->subdev
= &info
->sasd
.asd
;
1582 /* Or shall this be managed by the soc-camera device? */
1585 /* HACK: just need a != NULL */
1586 sdesc
.host_desc
.board_info
= ERR_PTR(-ENODATA
);
1588 ret
= soc_camera_dyn_pdev(&sdesc
, sasc
);
1592 sasc
->sensor
= &info
->sasd
.asd
;
1594 icd
= soc_camera_add_pdev(sasc
);
1600 sasc
->notifier
.subdevs
= &info
->subdev
;
1601 sasc
->notifier
.num_subdevs
= 1;
1602 sasc
->notifier
.bound
= soc_camera_async_bound
;
1603 sasc
->notifier
.unbind
= soc_camera_async_unbind
;
1604 sasc
->notifier
.complete
= soc_camera_async_complete
;
1607 icd
->parent
= ici
->v4l2_dev
.dev
;
1609 client
= of_find_i2c_device_by_node(remote
);
1612 v4l2_clk_name_i2c(clk_name
, sizeof(clk_name
),
1613 client
->adapter
->nr
, client
->addr
);
1615 v4l2_clk_name_of(clk_name
, sizeof(clk_name
),
1616 of_node_full_name(remote
));
1618 icd
->clk
= v4l2_clk_register(&soc_camera_clk_ops
, clk_name
, icd
);
1619 if (IS_ERR(icd
->clk
)) {
1620 ret
= PTR_ERR(icd
->clk
);
1624 ret
= v4l2_async_notifier_register(&ici
->v4l2_dev
, &sasc
->notifier
);
1628 v4l2_clk_unregister(icd
->clk
);
1631 platform_device_del(sasc
->pdev
);
1633 platform_device_put(sasc
->pdev
);
1635 devm_kfree(ici
->v4l2_dev
.dev
, info
);
1636 dev_err(ici
->v4l2_dev
.dev
, "group probe failed: %d\n", ret
);
1641 static void scan_of_host(struct soc_camera_host
*ici
)
1643 struct device
*dev
= ici
->v4l2_dev
.dev
;
1644 struct device_node
*np
= dev
->of_node
;
1645 struct device_node
*epn
= NULL
, *ren
;
1648 for (i
= 0; ; i
++) {
1649 epn
= of_graph_get_next_endpoint(np
, epn
);
1653 ren
= of_graph_get_remote_port(epn
);
1655 dev_notice(dev
, "no remote for %s\n",
1656 of_node_full_name(epn
));
1660 /* so we now have a remote node to connect */
1662 soc_of_bind(ici
, epn
, ren
->parent
);
1667 dev_err(dev
, "multiple subdevices aren't supported yet!\n");
1676 static inline void scan_of_host(struct soc_camera_host
*ici
) { }
1679 /* Called during host-driver probe */
1680 static int soc_camera_probe(struct soc_camera_host
*ici
,
1681 struct soc_camera_device
*icd
)
1683 struct soc_camera_desc
*sdesc
= to_soc_camera_desc(icd
);
1684 struct soc_camera_host_desc
*shd
= &sdesc
->host_desc
;
1685 struct device
*control
= NULL
;
1688 dev_info(icd
->pdev
, "Probing %s\n", dev_name(icd
->pdev
));
1691 * Currently the subdev with the largest number of controls (13) is
1692 * ov6550. So let's pick 16 as a hint for the control handler. Note
1693 * that this is a hint only: too large and you waste some memory, too
1694 * small and there is a (very) small performance hit when looking up
1695 * controls in the internal hash.
1697 ret
= v4l2_ctrl_handler_init(&icd
->ctrl_handler
, 16);
1701 /* Must have icd->vdev before registering the device */
1702 ret
= video_dev_create(icd
);
1707 * ..._video_start() will create a device node, video_register_device()
1708 * itself is protected against concurrent open() calls, but we also have
1709 * to protect our data also during client probing.
1712 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1713 if (shd
->board_info
) {
1714 ret
= soc_camera_i2c_init(icd
, sdesc
);
1715 if (ret
< 0 && ret
!= -EPROBE_DEFER
)
1717 } else if (!shd
->add_device
|| !shd
->del_device
) {
1721 ret
= soc_camera_clock_start(ici
);
1725 if (shd
->module_name
)
1726 ret
= request_module(shd
->module_name
);
1728 ret
= shd
->add_device(icd
);
1733 * FIXME: this is racy, have to use driver-binding notification,
1734 * when it is available
1736 control
= to_soc_camera_control(icd
);
1737 if (!control
|| !control
->driver
|| !dev_get_drvdata(control
) ||
1738 !try_module_get(control
->driver
->owner
)) {
1739 shd
->del_device(icd
);
1745 mutex_lock(&ici
->host_lock
);
1746 ret
= soc_camera_probe_finish(icd
);
1747 mutex_unlock(&ici
->host_lock
);
1754 if (shd
->board_info
) {
1755 soc_camera_i2c_free(icd
);
1757 shd
->del_device(icd
);
1758 module_put(control
->driver
->owner
);
1761 soc_camera_clock_stop(ici
);
1765 video_device_release(icd
->vdev
);
1769 v4l2_ctrl_handler_free(&icd
->ctrl_handler
);
1774 * This is called on device_unregister, which only means we have to disconnect
1775 * from the host, but not remove ourselves from the device list. With
1776 * asynchronous client probing this can also be called without
1777 * soc_camera_probe_finish() having run. Careful with clean up.
1779 static int soc_camera_remove(struct soc_camera_device
*icd
)
1781 struct soc_camera_desc
*sdesc
= to_soc_camera_desc(icd
);
1782 struct video_device
*vdev
= icd
->vdev
;
1784 v4l2_ctrl_handler_free(&icd
->ctrl_handler
);
1786 video_unregister_device(vdev
);
1790 if (sdesc
->host_desc
.board_info
) {
1791 soc_camera_i2c_free(icd
);
1793 struct device
*dev
= to_soc_camera_control(icd
);
1794 struct device_driver
*drv
= dev
? dev
->driver
: NULL
;
1796 sdesc
->host_desc
.del_device(icd
);
1797 module_put(drv
->owner
);
1801 if (icd
->num_user_formats
)
1802 soc_camera_free_user_formats(icd
);
1805 /* For the synchronous case */
1806 v4l2_clk_unregister(icd
->clk
);
1811 platform_device_unregister(icd
->sasc
->pdev
);
1816 static int default_g_selection(struct soc_camera_device
*icd
,
1817 struct v4l2_selection
*sel
)
1819 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1820 struct v4l2_subdev_selection sdsel
= {
1821 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1822 .target
= sel
->target
,
1826 ret
= v4l2_subdev_call(sd
, pad
, get_selection
, NULL
, &sdsel
);
1833 static int default_s_selection(struct soc_camera_device
*icd
,
1834 struct v4l2_selection
*sel
)
1836 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1837 struct v4l2_subdev_selection sdsel
= {
1838 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1839 .target
= sel
->target
,
1840 .flags
= sel
->flags
,
1845 ret
= v4l2_subdev_call(sd
, pad
, set_selection
, NULL
, &sdsel
);
1852 static int default_g_parm(struct soc_camera_device
*icd
,
1853 struct v4l2_streamparm
*parm
)
1855 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1856 return v4l2_subdev_call(sd
, video
, g_parm
, parm
);
1859 static int default_s_parm(struct soc_camera_device
*icd
,
1860 struct v4l2_streamparm
*parm
)
1862 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1863 return v4l2_subdev_call(sd
, video
, s_parm
, parm
);
1866 static int default_enum_framesizes(struct soc_camera_device
*icd
,
1867 struct v4l2_frmsizeenum
*fsize
)
1870 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1871 const struct soc_camera_format_xlate
*xlate
;
1872 struct v4l2_subdev_frame_size_enum fse
= {
1873 .index
= fsize
->index
,
1874 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1877 xlate
= soc_camera_xlate_by_fourcc(icd
, fsize
->pixel_format
);
1880 fse
.code
= xlate
->code
;
1882 ret
= v4l2_subdev_call(sd
, pad
, enum_frame_size
, NULL
, &fse
);
1886 if (fse
.min_width
== fse
.max_width
&&
1887 fse
.min_height
== fse
.max_height
) {
1888 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1889 fsize
->discrete
.width
= fse
.min_width
;
1890 fsize
->discrete
.height
= fse
.min_height
;
1893 fsize
->type
= V4L2_FRMSIZE_TYPE_CONTINUOUS
;
1894 fsize
->stepwise
.min_width
= fse
.min_width
;
1895 fsize
->stepwise
.max_width
= fse
.max_width
;
1896 fsize
->stepwise
.min_height
= fse
.min_height
;
1897 fsize
->stepwise
.max_height
= fse
.max_height
;
1898 fsize
->stepwise
.step_width
= 1;
1899 fsize
->stepwise
.step_height
= 1;
1903 int soc_camera_host_register(struct soc_camera_host
*ici
)
1905 struct soc_camera_host
*ix
;
1908 if (!ici
|| !ici
->ops
||
1909 !ici
->ops
->try_fmt
||
1910 !ici
->ops
->set_fmt
||
1911 !ici
->ops
->set_bus_param
||
1912 !ici
->ops
->querycap
||
1913 ((!ici
->ops
->init_videobuf
||
1914 !ici
->ops
->reqbufs
) &&
1915 !ici
->ops
->init_videobuf2
) ||
1920 if (!ici
->ops
->set_selection
)
1921 ici
->ops
->set_selection
= default_s_selection
;
1922 if (!ici
->ops
->get_selection
)
1923 ici
->ops
->get_selection
= default_g_selection
;
1924 if (!ici
->ops
->set_parm
)
1925 ici
->ops
->set_parm
= default_s_parm
;
1926 if (!ici
->ops
->get_parm
)
1927 ici
->ops
->get_parm
= default_g_parm
;
1928 if (!ici
->ops
->enum_framesizes
)
1929 ici
->ops
->enum_framesizes
= default_enum_framesizes
;
1931 mutex_lock(&list_lock
);
1932 list_for_each_entry(ix
, &hosts
, list
) {
1933 if (ix
->nr
== ici
->nr
) {
1939 ret
= v4l2_device_register(ici
->v4l2_dev
.dev
, &ici
->v4l2_dev
);
1943 list_add_tail(&ici
->list
, &hosts
);
1944 mutex_unlock(&list_lock
);
1946 mutex_init(&ici
->host_lock
);
1947 mutex_init(&ici
->clk_lock
);
1949 if (ici
->v4l2_dev
.dev
->of_node
)
1951 else if (ici
->asd_sizes
)
1953 * No OF, host with a list of subdevices. Don't try to mix
1954 * modes by initialising some groups statically and some
1957 scan_async_host(ici
);
1959 /* Legacy: static platform devices from board data */
1965 mutex_unlock(&list_lock
);
1968 EXPORT_SYMBOL(soc_camera_host_register
);
1970 /* Unregister all clients! */
1971 void soc_camera_host_unregister(struct soc_camera_host
*ici
)
1973 struct soc_camera_device
*icd
, *tmp
;
1974 struct soc_camera_async_client
*sasc
;
1975 LIST_HEAD(notifiers
);
1977 mutex_lock(&list_lock
);
1978 list_del(&ici
->list
);
1979 list_for_each_entry(icd
, &devices
, list
)
1980 if (icd
->iface
== ici
->nr
&& icd
->sasc
) {
1981 /* as long as we hold the device, sasc won't be freed */
1982 get_device(icd
->pdev
);
1983 list_add(&icd
->sasc
->list
, ¬ifiers
);
1985 mutex_unlock(&list_lock
);
1987 list_for_each_entry(sasc
, ¬ifiers
, list
) {
1988 /* Must call unlocked to avoid AB-BA dead-lock */
1989 v4l2_async_notifier_unregister(&sasc
->notifier
);
1990 put_device(&sasc
->pdev
->dev
);
1993 mutex_lock(&list_lock
);
1995 list_for_each_entry_safe(icd
, tmp
, &devices
, list
)
1996 if (icd
->iface
== ici
->nr
)
1997 soc_camera_remove(icd
);
1999 mutex_unlock(&list_lock
);
2001 v4l2_device_unregister(&ici
->v4l2_dev
);
2003 EXPORT_SYMBOL(soc_camera_host_unregister
);
2005 /* Image capture device */
2006 static int soc_camera_device_register(struct soc_camera_device
*icd
)
2008 struct soc_camera_device
*ix
;
2011 mutex_lock(&list_lock
);
2012 for (i
= 0; i
< 256 && num
< 0; i
++) {
2014 /* Check if this index is available on this interface */
2015 list_for_each_entry(ix
, &devices
, list
) {
2016 if (ix
->iface
== icd
->iface
&& ix
->devnum
== i
) {
2025 * ok, we have 256 cameras on this host...
2026 * man, stay reasonable...
2028 mutex_unlock(&list_lock
);
2034 icd
->host_priv
= NULL
;
2037 * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
2040 i
= to_platform_device(icd
->pdev
)->id
;
2042 /* One static (legacy) soc-camera platform device */
2044 if (i
>= MAP_MAX_NUM
) {
2045 mutex_unlock(&list_lock
);
2048 set_bit(i
, device_map
);
2049 list_add_tail(&icd
->list
, &devices
);
2050 mutex_unlock(&list_lock
);
2055 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops
= {
2056 .vidioc_querycap
= soc_camera_querycap
,
2057 .vidioc_try_fmt_vid_cap
= soc_camera_try_fmt_vid_cap
,
2058 .vidioc_g_fmt_vid_cap
= soc_camera_g_fmt_vid_cap
,
2059 .vidioc_s_fmt_vid_cap
= soc_camera_s_fmt_vid_cap
,
2060 .vidioc_enum_fmt_vid_cap
= soc_camera_enum_fmt_vid_cap
,
2061 .vidioc_enum_input
= soc_camera_enum_input
,
2062 .vidioc_g_input
= soc_camera_g_input
,
2063 .vidioc_s_input
= soc_camera_s_input
,
2064 .vidioc_s_std
= soc_camera_s_std
,
2065 .vidioc_g_std
= soc_camera_g_std
,
2066 .vidioc_enum_framesizes
= soc_camera_enum_framesizes
,
2067 .vidioc_reqbufs
= soc_camera_reqbufs
,
2068 .vidioc_querybuf
= soc_camera_querybuf
,
2069 .vidioc_qbuf
= soc_camera_qbuf
,
2070 .vidioc_dqbuf
= soc_camera_dqbuf
,
2071 .vidioc_create_bufs
= soc_camera_create_bufs
,
2072 .vidioc_prepare_buf
= soc_camera_prepare_buf
,
2073 .vidioc_expbuf
= soc_camera_expbuf
,
2074 .vidioc_streamon
= soc_camera_streamon
,
2075 .vidioc_streamoff
= soc_camera_streamoff
,
2076 .vidioc_g_selection
= soc_camera_g_selection
,
2077 .vidioc_s_selection
= soc_camera_s_selection
,
2078 .vidioc_g_parm
= soc_camera_g_parm
,
2079 .vidioc_s_parm
= soc_camera_s_parm
,
2082 static int video_dev_create(struct soc_camera_device
*icd
)
2084 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->parent
);
2085 struct video_device
*vdev
= video_device_alloc();
2090 strlcpy(vdev
->name
, ici
->drv_name
, sizeof(vdev
->name
));
2092 vdev
->v4l2_dev
= &ici
->v4l2_dev
;
2093 vdev
->fops
= &soc_camera_fops
;
2094 vdev
->ioctl_ops
= &soc_camera_ioctl_ops
;
2095 vdev
->release
= video_device_release
;
2096 vdev
->ctrl_handler
= &icd
->ctrl_handler
;
2097 vdev
->lock
= &ici
->host_lock
;
2105 * Called from soc_camera_probe() above with .host_lock held
2107 static int soc_camera_video_start(struct soc_camera_device
*icd
)
2109 const struct device_type
*type
= icd
->vdev
->dev
.type
;
2115 video_set_drvdata(icd
->vdev
, icd
);
2116 if (icd
->vdev
->tvnorms
== 0) {
2117 /* disable the STD API if there are no tvnorms defined */
2118 v4l2_disable_ioctl(icd
->vdev
, VIDIOC_G_STD
);
2119 v4l2_disable_ioctl(icd
->vdev
, VIDIOC_S_STD
);
2120 v4l2_disable_ioctl(icd
->vdev
, VIDIOC_ENUMSTD
);
2122 ret
= video_register_device(icd
->vdev
, VFL_TYPE_GRABBER
, -1);
2124 dev_err(icd
->pdev
, "video_register_device failed: %d\n", ret
);
2128 /* Restore device type, possibly set by the subdevice driver */
2129 icd
->vdev
->dev
.type
= type
;
2134 static int soc_camera_pdrv_probe(struct platform_device
*pdev
)
2136 struct soc_camera_desc
*sdesc
= pdev
->dev
.platform_data
;
2137 struct soc_camera_subdev_desc
*ssdd
= &sdesc
->subdev_desc
;
2138 struct soc_camera_device
*icd
;
2144 icd
= devm_kzalloc(&pdev
->dev
, sizeof(*icd
), GFP_KERNEL
);
2149 * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
2150 * regulator allocation is a dummy. They are actually requested by the
2151 * subdevice driver, using soc_camera_power_init(). Also note, that in
2152 * that case regulators are attached to the I2C device and not to the
2153 * camera platform device.
2155 ret
= devm_regulator_bulk_get(&pdev
->dev
, ssdd
->sd_pdata
.num_regulators
,
2156 ssdd
->sd_pdata
.regulators
);
2160 icd
->iface
= sdesc
->host_desc
.bus_id
;
2162 icd
->pdev
= &pdev
->dev
;
2163 platform_set_drvdata(pdev
, icd
);
2165 icd
->user_width
= DEFAULT_WIDTH
;
2166 icd
->user_height
= DEFAULT_HEIGHT
;
2168 return soc_camera_device_register(icd
);
2172 * Only called on rmmod for each platform device, since they are not
2173 * hot-pluggable. Now we know, that all our users - hosts and devices have
2174 * been unloaded already
2176 static int soc_camera_pdrv_remove(struct platform_device
*pdev
)
2178 struct soc_camera_device
*icd
= platform_get_drvdata(pdev
);
2189 * In synchronous mode with static platform devices this is called in a
2190 * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
2191 * no need to lock. In asynchronous case the caller -
2192 * soc_camera_host_unregister() - already holds the lock
2194 if (test_bit(i
, device_map
)) {
2195 clear_bit(i
, device_map
);
2196 list_del(&icd
->list
);
2202 static struct platform_driver __refdata soc_camera_pdrv
= {
2203 .probe
= soc_camera_pdrv_probe
,
2204 .remove
= soc_camera_pdrv_remove
,
2206 .name
= "soc-camera-pdrv",
2210 module_platform_driver(soc_camera_pdrv
);
2212 MODULE_DESCRIPTION("Image capture bus driver");
2213 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2214 MODULE_LICENSE("GPL");
2215 MODULE_ALIAS("platform:soc-camera-pdrv");