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/module.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/list.h>
23 #include <linux/err.h>
24 #include <linux/mutex.h>
25 #include <linux/vmalloc.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-dev.h>
30 #include <media/videobuf-core.h>
31 #include <media/soc_camera.h>
33 static LIST_HEAD(hosts
);
34 static LIST_HEAD(devices
);
35 static DEFINE_MUTEX(list_lock
);
36 static DEFINE_MUTEX(video_lock
);
38 const struct soc_camera_data_format
*soc_camera_format_by_fourcc(
39 struct soc_camera_device
*icd
, unsigned int fourcc
)
43 for (i
= 0; i
< icd
->num_formats
; i
++)
44 if (icd
->formats
[i
].fourcc
== fourcc
)
45 return icd
->formats
+ i
;
48 EXPORT_SYMBOL(soc_camera_format_by_fourcc
);
50 const struct soc_camera_format_xlate
*soc_camera_xlate_by_fourcc(
51 struct soc_camera_device
*icd
, unsigned int fourcc
)
55 for (i
= 0; i
< icd
->num_user_formats
; i
++)
56 if (icd
->user_formats
[i
].host_fmt
->fourcc
== fourcc
)
57 return icd
->user_formats
+ i
;
60 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc
);
62 static int soc_camera_try_fmt_vid_cap(struct file
*file
, void *priv
,
63 struct v4l2_format
*f
)
65 struct soc_camera_file
*icf
= file
->private_data
;
66 struct soc_camera_device
*icd
= icf
->icd
;
67 struct soc_camera_host
*ici
=
68 to_soc_camera_host(icd
->dev
.parent
);
69 enum v4l2_field field
;
72 WARN_ON(priv
!= file
->private_data
);
75 * TODO: this might also have to migrate to host-drivers, if anyone
76 * wishes to support other fields
78 field
= f
->fmt
.pix
.field
;
80 if (field
== V4L2_FIELD_ANY
) {
81 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
82 } else if (field
!= V4L2_FIELD_NONE
) {
83 dev_err(&icd
->dev
, "Field type invalid.\n");
87 /* limit format to hardware capabilities */
88 ret
= ici
->ops
->try_fmt(icd
, f
);
93 static int soc_camera_enum_input(struct file
*file
, void *priv
,
94 struct v4l2_input
*inp
)
99 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
100 inp
->std
= V4L2_STD_UNKNOWN
;
101 strcpy(inp
->name
, "Camera");
106 static int soc_camera_g_input(struct file
*file
, void *priv
, unsigned int *i
)
113 static int soc_camera_s_input(struct file
*file
, void *priv
, unsigned int i
)
121 static int soc_camera_s_std(struct file
*file
, void *priv
, v4l2_std_id
*a
)
126 static int soc_camera_reqbufs(struct file
*file
, void *priv
,
127 struct v4l2_requestbuffers
*p
)
130 struct soc_camera_file
*icf
= file
->private_data
;
131 struct soc_camera_device
*icd
= icf
->icd
;
132 struct soc_camera_host
*ici
=
133 to_soc_camera_host(icd
->dev
.parent
);
135 WARN_ON(priv
!= file
->private_data
);
137 dev_dbg(&icd
->dev
, "%s: %d\n", __func__
, p
->memory
);
139 ret
= videobuf_reqbufs(&icf
->vb_vidq
, p
);
143 return ici
->ops
->reqbufs(icf
, p
);
146 static int soc_camera_querybuf(struct file
*file
, void *priv
,
147 struct v4l2_buffer
*p
)
149 struct soc_camera_file
*icf
= file
->private_data
;
151 WARN_ON(priv
!= file
->private_data
);
153 return videobuf_querybuf(&icf
->vb_vidq
, p
);
156 static int soc_camera_qbuf(struct file
*file
, void *priv
,
157 struct v4l2_buffer
*p
)
159 struct soc_camera_file
*icf
= file
->private_data
;
161 WARN_ON(priv
!= file
->private_data
);
163 return videobuf_qbuf(&icf
->vb_vidq
, p
);
166 static int soc_camera_dqbuf(struct file
*file
, void *priv
,
167 struct v4l2_buffer
*p
)
169 struct soc_camera_file
*icf
= file
->private_data
;
171 WARN_ON(priv
!= file
->private_data
);
173 return videobuf_dqbuf(&icf
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
176 static int soc_camera_init_user_formats(struct soc_camera_device
*icd
)
178 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
181 if (!ici
->ops
->get_formats
)
183 * Fallback mode - the host will have to serve all
184 * sensor-provided formats one-to-one to the user
186 fmts
= icd
->num_formats
;
189 * First pass - only count formats this host-sensor
190 * configuration can provide
192 for (i
= 0; i
< icd
->num_formats
; i
++)
193 fmts
+= ici
->ops
->get_formats(icd
, i
, NULL
);
199 vmalloc(fmts
* sizeof(struct soc_camera_format_xlate
));
200 if (!icd
->user_formats
)
203 icd
->num_user_formats
= fmts
;
206 dev_dbg(&icd
->dev
, "Found %d supported formats.\n", fmts
);
208 /* Second pass - actually fill data formats */
209 for (i
= 0; i
< icd
->num_formats
; i
++)
210 if (!ici
->ops
->get_formats
) {
211 icd
->user_formats
[i
].host_fmt
= icd
->formats
+ i
;
212 icd
->user_formats
[i
].cam_fmt
= icd
->formats
+ i
;
213 icd
->user_formats
[i
].buswidth
= icd
->formats
[i
].depth
;
215 fmts
+= ici
->ops
->get_formats(icd
, i
,
216 &icd
->user_formats
[fmts
]);
219 icd
->current_fmt
= icd
->user_formats
[0].host_fmt
;
224 static void soc_camera_free_user_formats(struct soc_camera_device
*icd
)
226 vfree(icd
->user_formats
);
229 static int soc_camera_open(struct inode
*inode
, struct file
*file
)
231 struct video_device
*vdev
;
232 struct soc_camera_device
*icd
;
233 struct soc_camera_host
*ici
;
234 struct soc_camera_file
*icf
;
237 icf
= vmalloc(sizeof(*icf
));
241 /* Protect against icd->remove() until we module_get() both drivers. */
242 mutex_lock(&video_lock
);
244 vdev
= video_devdata(file
);
245 icd
= container_of(vdev
->parent
, struct soc_camera_device
, dev
);
246 ici
= to_soc_camera_host(icd
->dev
.parent
);
248 if (!try_module_get(icd
->ops
->owner
)) {
249 dev_err(&icd
->dev
, "Couldn't lock sensor driver.\n");
254 if (!try_module_get(ici
->ops
->owner
)) {
255 dev_err(&icd
->dev
, "Couldn't lock capture bus driver.\n");
263 /* Now we really have to activate the camera */
264 if (icd
->use_count
== 1) {
265 ret
= soc_camera_init_user_formats(icd
);
268 ret
= ici
->ops
->add(icd
);
270 dev_err(&icd
->dev
, "Couldn't activate the camera: %d\n", ret
);
275 mutex_unlock(&video_lock
);
277 file
->private_data
= icf
;
278 dev_dbg(&icd
->dev
, "camera device open\n");
280 ici
->ops
->init_videobuf(&icf
->vb_vidq
, icd
);
284 /* All errors are entered with the video_lock held */
286 soc_camera_free_user_formats(icd
);
289 module_put(ici
->ops
->owner
);
291 module_put(icd
->ops
->owner
);
293 mutex_unlock(&video_lock
);
298 static int soc_camera_close(struct inode
*inode
, struct file
*file
)
300 struct soc_camera_file
*icf
= file
->private_data
;
301 struct soc_camera_device
*icd
= icf
->icd
;
302 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
303 struct video_device
*vdev
= icd
->vdev
;
305 mutex_lock(&video_lock
);
307 if (!icd
->use_count
) {
308 ici
->ops
->remove(icd
);
309 soc_camera_free_user_formats(icd
);
311 module_put(icd
->ops
->owner
);
312 module_put(ici
->ops
->owner
);
313 mutex_unlock(&video_lock
);
317 dev_dbg(vdev
->parent
, "camera device close\n");
322 static ssize_t
soc_camera_read(struct file
*file
, char __user
*buf
,
323 size_t count
, loff_t
*ppos
)
325 struct soc_camera_file
*icf
= file
->private_data
;
326 struct soc_camera_device
*icd
= icf
->icd
;
327 struct video_device
*vdev
= icd
->vdev
;
330 dev_err(vdev
->parent
, "camera device read not implemented\n");
335 static int soc_camera_mmap(struct file
*file
, struct vm_area_struct
*vma
)
337 struct soc_camera_file
*icf
= file
->private_data
;
338 struct soc_camera_device
*icd
= icf
->icd
;
341 dev_dbg(&icd
->dev
, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
343 err
= videobuf_mmap_mapper(&icf
->vb_vidq
, vma
);
345 dev_dbg(&icd
->dev
, "vma start=0x%08lx, size=%ld, ret=%d\n",
346 (unsigned long)vma
->vm_start
,
347 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
353 static unsigned int soc_camera_poll(struct file
*file
, poll_table
*pt
)
355 struct soc_camera_file
*icf
= file
->private_data
;
356 struct soc_camera_device
*icd
= icf
->icd
;
357 struct soc_camera_host
*ici
=
358 to_soc_camera_host(icd
->dev
.parent
);
360 if (list_empty(&icf
->vb_vidq
.stream
)) {
361 dev_err(&icd
->dev
, "Trying to poll with no queued buffers!\n");
365 return ici
->ops
->poll(file
, pt
);
368 static struct file_operations soc_camera_fops
= {
369 .owner
= THIS_MODULE
,
370 .open
= soc_camera_open
,
371 .release
= soc_camera_close
,
372 .ioctl
= video_ioctl2
,
373 .read
= soc_camera_read
,
374 .mmap
= soc_camera_mmap
,
375 .poll
= soc_camera_poll
,
379 static int soc_camera_s_fmt_vid_cap(struct file
*file
, void *priv
,
380 struct v4l2_format
*f
)
382 struct soc_camera_file
*icf
= file
->private_data
;
383 struct soc_camera_device
*icd
= icf
->icd
;
384 struct soc_camera_host
*ici
=
385 to_soc_camera_host(icd
->dev
.parent
);
386 __u32 pixfmt
= f
->fmt
.pix
.pixelformat
;
388 struct v4l2_rect rect
;
390 WARN_ON(priv
!= file
->private_data
);
392 ret
= soc_camera_try_fmt_vid_cap(file
, priv
, f
);
396 rect
.left
= icd
->x_current
;
397 rect
.top
= icd
->y_current
;
398 rect
.width
= f
->fmt
.pix
.width
;
399 rect
.height
= f
->fmt
.pix
.height
;
400 ret
= ici
->ops
->set_fmt(icd
, f
->fmt
.pix
.pixelformat
, &rect
);
403 } else if (!icd
->current_fmt
||
404 icd
->current_fmt
->fourcc
!= pixfmt
) {
406 "Host driver hasn't set up current format correctly!\n");
410 icd
->width
= rect
.width
;
411 icd
->height
= rect
.height
;
412 icf
->vb_vidq
.field
= f
->fmt
.pix
.field
;
413 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
414 dev_warn(&icd
->dev
, "Attention! Wrong buf-type %d\n",
417 dev_dbg(&icd
->dev
, "set width: %d height: %d\n",
418 icd
->width
, icd
->height
);
420 /* set physical bus parameters */
421 return ici
->ops
->set_bus_param(icd
, pixfmt
);
424 static int soc_camera_enum_fmt_vid_cap(struct file
*file
, void *priv
,
425 struct v4l2_fmtdesc
*f
)
427 struct soc_camera_file
*icf
= file
->private_data
;
428 struct soc_camera_device
*icd
= icf
->icd
;
429 const struct soc_camera_data_format
*format
;
431 WARN_ON(priv
!= file
->private_data
);
433 if (f
->index
>= icd
->num_user_formats
)
436 format
= icd
->user_formats
[f
->index
].host_fmt
;
438 strlcpy(f
->description
, format
->name
, sizeof(f
->description
));
439 f
->pixelformat
= format
->fourcc
;
443 static int soc_camera_g_fmt_vid_cap(struct file
*file
, void *priv
,
444 struct v4l2_format
*f
)
446 struct soc_camera_file
*icf
= file
->private_data
;
447 struct soc_camera_device
*icd
= icf
->icd
;
449 WARN_ON(priv
!= file
->private_data
);
451 f
->fmt
.pix
.width
= icd
->width
;
452 f
->fmt
.pix
.height
= icd
->height
;
453 f
->fmt
.pix
.field
= icf
->vb_vidq
.field
;
454 f
->fmt
.pix
.pixelformat
= icd
->current_fmt
->fourcc
;
455 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
*
456 DIV_ROUND_UP(icd
->current_fmt
->depth
, 8);
457 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
458 dev_dbg(&icd
->dev
, "current_fmt->fourcc: 0x%08x\n",
459 icd
->current_fmt
->fourcc
);
463 static int soc_camera_querycap(struct file
*file
, void *priv
,
464 struct v4l2_capability
*cap
)
466 struct soc_camera_file
*icf
= file
->private_data
;
467 struct soc_camera_device
*icd
= icf
->icd
;
468 struct soc_camera_host
*ici
=
469 to_soc_camera_host(icd
->dev
.parent
);
471 WARN_ON(priv
!= file
->private_data
);
473 strlcpy(cap
->driver
, ici
->drv_name
, sizeof(cap
->driver
));
474 return ici
->ops
->querycap(ici
, cap
);
477 static int soc_camera_streamon(struct file
*file
, void *priv
,
478 enum v4l2_buf_type i
)
480 struct soc_camera_file
*icf
= file
->private_data
;
481 struct soc_camera_device
*icd
= icf
->icd
;
483 WARN_ON(priv
!= file
->private_data
);
485 dev_dbg(&icd
->dev
, "%s\n", __func__
);
487 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
490 icd
->ops
->start_capture(icd
);
492 /* This calls buf_queue from host driver's videobuf_queue_ops */
493 return videobuf_streamon(&icf
->vb_vidq
);
496 static int soc_camera_streamoff(struct file
*file
, void *priv
,
497 enum v4l2_buf_type i
)
499 struct soc_camera_file
*icf
= file
->private_data
;
500 struct soc_camera_device
*icd
= icf
->icd
;
502 WARN_ON(priv
!= file
->private_data
);
504 dev_dbg(&icd
->dev
, "%s\n", __func__
);
506 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
509 /* This calls buf_release from host driver's videobuf_queue_ops for all
510 * remaining buffers. When the last buffer is freed, stop capture */
511 videobuf_streamoff(&icf
->vb_vidq
);
513 icd
->ops
->stop_capture(icd
);
518 static int soc_camera_queryctrl(struct file
*file
, void *priv
,
519 struct v4l2_queryctrl
*qc
)
521 struct soc_camera_file
*icf
= file
->private_data
;
522 struct soc_camera_device
*icd
= icf
->icd
;
525 WARN_ON(priv
!= file
->private_data
);
530 for (i
= 0; i
< icd
->ops
->num_controls
; i
++)
531 if (qc
->id
== icd
->ops
->controls
[i
].id
) {
532 memcpy(qc
, &(icd
->ops
->controls
[i
]),
540 static int soc_camera_g_ctrl(struct file
*file
, void *priv
,
541 struct v4l2_control
*ctrl
)
543 struct soc_camera_file
*icf
= file
->private_data
;
544 struct soc_camera_device
*icd
= icf
->icd
;
546 WARN_ON(priv
!= file
->private_data
);
550 if (icd
->gain
== (unsigned short)~0)
552 ctrl
->value
= icd
->gain
;
554 case V4L2_CID_EXPOSURE
:
555 if (icd
->exposure
== (unsigned short)~0)
557 ctrl
->value
= icd
->exposure
;
561 if (icd
->ops
->get_control
)
562 return icd
->ops
->get_control(icd
, ctrl
);
566 static int soc_camera_s_ctrl(struct file
*file
, void *priv
,
567 struct v4l2_control
*ctrl
)
569 struct soc_camera_file
*icf
= file
->private_data
;
570 struct soc_camera_device
*icd
= icf
->icd
;
572 WARN_ON(priv
!= file
->private_data
);
574 if (icd
->ops
->set_control
)
575 return icd
->ops
->set_control(icd
, ctrl
);
579 static int soc_camera_cropcap(struct file
*file
, void *fh
,
580 struct v4l2_cropcap
*a
)
582 struct soc_camera_file
*icf
= file
->private_data
;
583 struct soc_camera_device
*icd
= icf
->icd
;
585 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
586 a
->bounds
.left
= icd
->x_min
;
587 a
->bounds
.top
= icd
->y_min
;
588 a
->bounds
.width
= icd
->width_max
;
589 a
->bounds
.height
= icd
->height_max
;
590 a
->defrect
.left
= icd
->x_min
;
591 a
->defrect
.top
= icd
->y_min
;
592 a
->defrect
.width
= 640;
593 a
->defrect
.height
= 480;
594 a
->pixelaspect
.numerator
= 1;
595 a
->pixelaspect
.denominator
= 1;
600 static int soc_camera_g_crop(struct file
*file
, void *fh
,
603 struct soc_camera_file
*icf
= file
->private_data
;
604 struct soc_camera_device
*icd
= icf
->icd
;
606 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
607 a
->c
.left
= icd
->x_current
;
608 a
->c
.top
= icd
->y_current
;
609 a
->c
.width
= icd
->width
;
610 a
->c
.height
= icd
->height
;
615 static int soc_camera_s_crop(struct file
*file
, void *fh
,
618 struct soc_camera_file
*icf
= file
->private_data
;
619 struct soc_camera_device
*icd
= icf
->icd
;
620 struct soc_camera_host
*ici
=
621 to_soc_camera_host(icd
->dev
.parent
);
624 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
627 ret
= ici
->ops
->set_fmt(icd
, 0, &a
->c
);
629 icd
->width
= a
->c
.width
;
630 icd
->height
= a
->c
.height
;
631 icd
->x_current
= a
->c
.left
;
632 icd
->y_current
= a
->c
.top
;
638 static int soc_camera_g_chip_ident(struct file
*file
, void *fh
,
639 struct v4l2_chip_ident
*id
)
641 struct soc_camera_file
*icf
= file
->private_data
;
642 struct soc_camera_device
*icd
= icf
->icd
;
644 if (!icd
->ops
->get_chip_id
)
647 return icd
->ops
->get_chip_id(icd
, id
);
650 #ifdef CONFIG_VIDEO_ADV_DEBUG
651 static int soc_camera_g_register(struct file
*file
, void *fh
,
652 struct v4l2_register
*reg
)
654 struct soc_camera_file
*icf
= file
->private_data
;
655 struct soc_camera_device
*icd
= icf
->icd
;
657 if (!icd
->ops
->get_register
)
660 return icd
->ops
->get_register(icd
, reg
);
663 static int soc_camera_s_register(struct file
*file
, void *fh
,
664 struct v4l2_register
*reg
)
666 struct soc_camera_file
*icf
= file
->private_data
;
667 struct soc_camera_device
*icd
= icf
->icd
;
669 if (!icd
->ops
->set_register
)
672 return icd
->ops
->set_register(icd
, reg
);
676 static int device_register_link(struct soc_camera_device
*icd
)
678 int ret
= device_register(&icd
->dev
);
681 /* Prevent calling device_unregister() */
682 icd
->dev
.parent
= NULL
;
683 dev_err(&icd
->dev
, "Cannot register device: %d\n", ret
);
684 /* Even if probe() was unsuccessful for all registered drivers,
685 * device_register() returns 0, and we add the link, just to
686 * document this camera's control device */
687 } else if (icd
->control
)
688 /* Have to sysfs_remove_link() before device_unregister()? */
689 if (sysfs_create_link(&icd
->dev
.kobj
, &icd
->control
->kobj
,
692 "Failed creating the control symlink\n");
696 /* So far this function cannot fail */
697 static void scan_add_host(struct soc_camera_host
*ici
)
699 struct soc_camera_device
*icd
;
701 mutex_lock(&list_lock
);
703 list_for_each_entry(icd
, &devices
, list
) {
704 if (icd
->iface
== ici
->nr
) {
705 icd
->dev
.parent
= &ici
->dev
;
706 device_register_link(icd
);
710 mutex_unlock(&list_lock
);
713 /* return: 0 if no match found or a match found and
714 * device_register() successful, error code otherwise */
715 static int scan_add_device(struct soc_camera_device
*icd
)
717 struct soc_camera_host
*ici
;
720 mutex_lock(&list_lock
);
722 list_add_tail(&icd
->list
, &devices
);
724 /* Watch out for class_for_each_device / class_find_device API by
725 * Dave Young <hidave.darkstar@gmail.com> */
726 list_for_each_entry(ici
, &hosts
, list
) {
727 if (icd
->iface
== ici
->nr
) {
729 icd
->dev
.parent
= &ici
->dev
;
734 mutex_unlock(&list_lock
);
737 ret
= device_register_link(icd
);
742 static int soc_camera_probe(struct device
*dev
)
744 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
745 struct soc_camera_host
*ici
=
746 to_soc_camera_host(icd
->dev
.parent
);
749 if (!icd
->ops
->probe
)
752 /* We only call ->add() here to activate and probe the camera.
753 * We shall ->remove() and deactivate it immediately afterwards. */
754 ret
= ici
->ops
->add(icd
);
758 ret
= icd
->ops
->probe(icd
);
760 const struct v4l2_queryctrl
*qctrl
;
762 qctrl
= soc_camera_find_qctrl(icd
->ops
, V4L2_CID_GAIN
);
763 icd
->gain
= qctrl
? qctrl
->default_value
: (unsigned short)~0;
764 qctrl
= soc_camera_find_qctrl(icd
->ops
, V4L2_CID_EXPOSURE
);
765 icd
->exposure
= qctrl
? qctrl
->default_value
:
768 ici
->ops
->remove(icd
);
773 /* This is called on device_unregister, which only means we have to disconnect
774 * from the host, but not remove ourselves from the device list */
775 static int soc_camera_remove(struct device
*dev
)
777 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
779 if (icd
->ops
->remove
)
780 icd
->ops
->remove(icd
);
785 static int soc_camera_suspend(struct device
*dev
, pm_message_t state
)
787 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
788 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
791 if (ici
->ops
->suspend
)
792 ret
= ici
->ops
->suspend(icd
, state
);
797 static int soc_camera_resume(struct device
*dev
)
799 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
800 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
803 if (ici
->ops
->resume
)
804 ret
= ici
->ops
->resume(icd
);
809 static struct bus_type soc_camera_bus_type
= {
810 .name
= "soc-camera",
811 .probe
= soc_camera_probe
,
812 .remove
= soc_camera_remove
,
813 .suspend
= soc_camera_suspend
,
814 .resume
= soc_camera_resume
,
817 static struct device_driver ic_drv
= {
819 .bus
= &soc_camera_bus_type
,
820 .owner
= THIS_MODULE
,
823 static void dummy_release(struct device
*dev
)
827 int soc_camera_host_register(struct soc_camera_host
*ici
)
830 struct soc_camera_host
*ix
;
832 if (!ici
->ops
->init_videobuf
|| !ici
->ops
->add
|| !ici
->ops
->remove
)
835 /* Number might be equal to the platform device ID */
836 dev_set_name(&ici
->dev
, "camera_host%d", ici
->nr
);
838 mutex_lock(&list_lock
);
839 list_for_each_entry(ix
, &hosts
, list
) {
840 if (ix
->nr
== ici
->nr
) {
841 mutex_unlock(&list_lock
);
846 list_add_tail(&ici
->list
, &hosts
);
847 mutex_unlock(&list_lock
);
849 ici
->dev
.release
= dummy_release
;
851 ret
= device_register(&ici
->dev
);
861 mutex_lock(&list_lock
);
862 list_del(&ici
->list
);
863 mutex_unlock(&list_lock
);
867 EXPORT_SYMBOL(soc_camera_host_register
);
869 /* Unregister all clients! */
870 void soc_camera_host_unregister(struct soc_camera_host
*ici
)
872 struct soc_camera_device
*icd
;
874 mutex_lock(&list_lock
);
876 list_del(&ici
->list
);
878 list_for_each_entry(icd
, &devices
, list
) {
879 if (icd
->dev
.parent
== &ici
->dev
) {
880 device_unregister(&icd
->dev
);
881 /* Not before device_unregister(), .remove
882 * needs parent to call ici->ops->remove() */
883 icd
->dev
.parent
= NULL
;
884 memset(&icd
->dev
.kobj
, 0, sizeof(icd
->dev
.kobj
));
888 mutex_unlock(&list_lock
);
890 device_unregister(&ici
->dev
);
892 EXPORT_SYMBOL(soc_camera_host_unregister
);
894 /* Image capture device */
895 int soc_camera_device_register(struct soc_camera_device
*icd
)
897 struct soc_camera_device
*ix
;
903 for (i
= 0; i
< 256 && num
< 0; i
++) {
905 list_for_each_entry(ix
, &devices
, list
) {
906 if (ix
->iface
== icd
->iface
&& ix
->devnum
== i
) {
914 /* ok, we have 256 cameras on this host...
915 * man, stay reasonable... */
919 icd
->dev
.bus
= &soc_camera_bus_type
;
920 dev_set_name(&icd
->dev
, "%u-%u", icd
->iface
, icd
->devnum
);
922 icd
->dev
.release
= dummy_release
;
924 return scan_add_device(icd
);
926 EXPORT_SYMBOL(soc_camera_device_register
);
928 void soc_camera_device_unregister(struct soc_camera_device
*icd
)
930 mutex_lock(&list_lock
);
931 list_del(&icd
->list
);
933 /* The bus->remove will be eventually called */
935 device_unregister(&icd
->dev
);
936 mutex_unlock(&list_lock
);
938 EXPORT_SYMBOL(soc_camera_device_unregister
);
940 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops
= {
941 .vidioc_querycap
= soc_camera_querycap
,
942 .vidioc_g_fmt_vid_cap
= soc_camera_g_fmt_vid_cap
,
943 .vidioc_enum_fmt_vid_cap
= soc_camera_enum_fmt_vid_cap
,
944 .vidioc_s_fmt_vid_cap
= soc_camera_s_fmt_vid_cap
,
945 .vidioc_enum_input
= soc_camera_enum_input
,
946 .vidioc_g_input
= soc_camera_g_input
,
947 .vidioc_s_input
= soc_camera_s_input
,
948 .vidioc_s_std
= soc_camera_s_std
,
949 .vidioc_reqbufs
= soc_camera_reqbufs
,
950 .vidioc_try_fmt_vid_cap
= soc_camera_try_fmt_vid_cap
,
951 .vidioc_querybuf
= soc_camera_querybuf
,
952 .vidioc_qbuf
= soc_camera_qbuf
,
953 .vidioc_dqbuf
= soc_camera_dqbuf
,
954 .vidioc_streamon
= soc_camera_streamon
,
955 .vidioc_streamoff
= soc_camera_streamoff
,
956 .vidioc_queryctrl
= soc_camera_queryctrl
,
957 .vidioc_g_ctrl
= soc_camera_g_ctrl
,
958 .vidioc_s_ctrl
= soc_camera_s_ctrl
,
959 .vidioc_cropcap
= soc_camera_cropcap
,
960 .vidioc_g_crop
= soc_camera_g_crop
,
961 .vidioc_s_crop
= soc_camera_s_crop
,
962 .vidioc_g_chip_ident
= soc_camera_g_chip_ident
,
963 #ifdef CONFIG_VIDEO_ADV_DEBUG
964 .vidioc_g_register
= soc_camera_g_register
,
965 .vidioc_s_register
= soc_camera_s_register
,
969 int soc_camera_video_start(struct soc_camera_device
*icd
)
971 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
973 struct video_device
*vdev
;
975 if (!icd
->dev
.parent
)
978 vdev
= video_device_alloc();
981 dev_dbg(&ici
->dev
, "Allocated video_device %p\n", vdev
);
983 strlcpy(vdev
->name
, ici
->drv_name
, sizeof(vdev
->name
));
984 /* Maybe better &ici->dev */
985 vdev
->parent
= &icd
->dev
;
986 vdev
->current_norm
= V4L2_STD_UNKNOWN
;
987 vdev
->fops
= &soc_camera_fops
;
988 vdev
->ioctl_ops
= &soc_camera_ioctl_ops
;
989 vdev
->release
= video_device_release
;
991 vdev
->tvnorms
= V4L2_STD_UNKNOWN
,
993 err
= video_register_device(vdev
, VFL_TYPE_GRABBER
, vdev
->minor
);
995 dev_err(vdev
->parent
, "video_register_device failed\n");
1003 video_device_release(vdev
);
1007 EXPORT_SYMBOL(soc_camera_video_start
);
1009 void soc_camera_video_stop(struct soc_camera_device
*icd
)
1011 struct video_device
*vdev
= icd
->vdev
;
1013 dev_dbg(&icd
->dev
, "%s\n", __func__
);
1015 if (!icd
->dev
.parent
|| !vdev
)
1018 mutex_lock(&video_lock
);
1019 video_unregister_device(vdev
);
1021 mutex_unlock(&video_lock
);
1023 EXPORT_SYMBOL(soc_camera_video_stop
);
1025 static int __init
soc_camera_init(void)
1027 int ret
= bus_register(&soc_camera_bus_type
);
1030 ret
= driver_register(&ic_drv
);
1037 bus_unregister(&soc_camera_bus_type
);
1041 static void __exit
soc_camera_exit(void)
1043 driver_unregister(&ic_drv
);
1044 bus_unregister(&soc_camera_bus_type
);
1047 module_init(soc_camera_init
);
1048 module_exit(soc_camera_exit
);
1050 MODULE_DESCRIPTION("Image capture bus driver");
1051 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1052 MODULE_LICENSE("GPL");