22 #include <linux/errno.h>
24 #include <linux/i2c.h>
29 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/time.h>
33 #include <linux/types.h>
47 #define CAPTURE_DRV_NAME "bfin_capture"
48 #define BCAP_MIN_NUM_BUF 2
115 .desc =
"YCbCr 4:2:2 Interleaved UYVY",
121 .desc =
"YCbCr 4:2:2 Interleaved YUYV",
140 #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
149 static int bcap_init_sensor_formats(
struct bcap_device *bcap_dev)
153 unsigned int num_formats = 0;
157 enum_mbus_fmt, num_formats, &code))
162 sf = kzalloc(num_formats *
sizeof(*sf),
GFP_KERNEL);
166 for (i = 0; i < num_formats; i++) {
168 enum_mbus_fmt, i, &code);
172 if (j == BCAP_MAX_FMTS) {
177 sf[
i] = bcap_formats[
j];
184 static void bcap_free_sensor_formats(
struct bcap_device *bcap_dev)
191 static int bcap_open(
struct file *
file)
193 struct bcap_device *bcap_dev = video_drvdata(file);
202 bcap_fh = kzalloc(
sizeof(*bcap_fh),
GFP_KERNEL);
205 "unable to allocate memory for file handle object\n");
218 static int bcap_release(
struct file *file)
220 struct bcap_device *bcap_dev = video_drvdata(file);
222 struct bcap_fh *bcap_fh =
container_of(fh,
struct bcap_fh, fh);
235 static int bcap_mmap(
struct file *file,
struct vm_area_struct *vma)
237 struct bcap_device *bcap_dev = video_drvdata(file);
248 static unsigned long bcap_get_unmapped_area(
struct file *file,
254 struct bcap_device *bcap_dev = video_drvdata(file);
264 static unsigned int bcap_poll(
struct file *file,
poll_table *
wait)
266 struct bcap_device *bcap_dev = video_drvdata(file);
275 static int bcap_queue_setup(
struct vb2_queue *vq,
277 unsigned int *nbuffers,
unsigned int *nplanes,
278 unsigned int sizes[],
void *alloc_ctxs[])
280 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
286 sizes[0] = bcap_dev->
fmt.sizeimage;
292 static int bcap_buffer_init(
struct vb2_buffer *vb)
296 INIT_LIST_HEAD(&buf->
list);
306 size = bcap_dev->
fmt.sizeimage;
307 if (vb2_plane_size(vb, 0) < size) {
309 vb2_plane_size(vb, 0), size);
312 vb2_set_plane_payload(&buf->
vb, 0, size);
317 static void bcap_buffer_queue(
struct vb2_buffer *vb)
325 spin_unlock_irqrestore(&bcap_dev->
lock, flags);
328 static void bcap_buffer_cleanup(
struct vb2_buffer *vb)
335 list_del_init(&buf->
list);
336 spin_unlock_irqrestore(&bcap_dev->
lock, flags);
339 static void bcap_lock(
struct vb2_queue *vq)
341 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
345 static void bcap_unlock(
struct vb2_queue *vq)
347 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
351 static int bcap_start_streaming(
struct vb2_queue *vq,
unsigned int count)
353 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
369 params.ppi_control = bcap_dev->
cfg->ppi_control;
370 params.int_mask = bcap_dev->
cfg->int_mask;
371 params.blank_clocks = bcap_dev->
cfg->blank_clocks;
372 ret = ppi->
ops->set_params(ppi, &
params);
375 "Error in setting ppi params\n");
380 ret = ppi->
ops->attach_irq(ppi, bcap_isr);
383 "Error in attaching interrupt handler\n");
388 bcap_dev->
stop =
false;
392 static int bcap_stop_streaming(
struct vb2_queue *vq)
394 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
398 if (!vb2_is_streaming(vq))
401 bcap_dev->
stop =
true;
404 ppi->
ops->detach_irq(ppi);
408 "stream off failed in subdev\n");
411 while (!list_empty(&bcap_dev->
dma_queue)) {
420 static struct vb2_ops bcap_video_qops = {
421 .queue_setup = bcap_queue_setup,
422 .buf_init = bcap_buffer_init,
423 .buf_prepare = bcap_buffer_prepare,
424 .buf_cleanup = bcap_buffer_cleanup,
425 .buf_queue = bcap_buffer_queue,
426 .wait_prepare = bcap_unlock,
427 .wait_finish = bcap_lock,
428 .start_streaming = bcap_start_streaming,
429 .stop_streaming = bcap_stop_streaming,
432 static int bcap_reqbufs(
struct file *file,
void *
priv,
435 struct bcap_device *bcap_dev = video_drvdata(file);
438 struct bcap_fh *bcap_fh =
container_of(fh,
struct bcap_fh, fh);
448 static int bcap_querybuf(
struct file *file,
void *priv,
451 struct bcap_device *bcap_dev = video_drvdata(file);
456 static int bcap_qbuf(
struct file *file,
void *priv,
459 struct bcap_device *bcap_dev = video_drvdata(file);
461 struct bcap_fh *bcap_fh =
container_of(fh,
struct bcap_fh, fh);
469 static int bcap_dqbuf(
struct file *file,
void *priv,
472 struct bcap_device *bcap_dev = video_drvdata(file);
474 struct bcap_fh *bcap_fh =
container_of(fh,
struct bcap_fh, fh);
491 spin_lock(&bcap_dev->
lock);
502 if (bcap_dev->
stop) {
509 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->
next_frm->vb, 0);
510 ppi->
ops->update_addr(ppi, (
unsigned long)addr);
512 ppi->
ops->start(ppi);
515 spin_unlock(&bcap_dev->
lock);
520 static int bcap_streamon(
struct file *file,
void *priv,
523 struct bcap_device *bcap_dev = video_drvdata(file);
550 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->
cur_frm->vb, 0);
552 ppi->
ops->update_addr(ppi, (
unsigned long)addr);
554 ppi->
ops->start(ppi);
562 static int bcap_streamoff(
struct file *file,
void *priv,
565 struct bcap_device *bcap_dev = video_drvdata(file);
574 static int bcap_querystd(
struct file *file,
void *priv,
v4l2_std_id *
std)
576 struct bcap_device *bcap_dev = video_drvdata(file);
581 static int bcap_g_std(
struct file *file,
void *priv,
v4l2_std_id *
std)
583 struct bcap_device *bcap_dev = video_drvdata(file);
585 *std = bcap_dev->
std;
589 static int bcap_s_std(
struct file *file,
void *priv,
v4l2_std_id *
std)
591 struct bcap_device *bcap_dev = video_drvdata(file);
605 static int bcap_enum_input(
struct file *file,
void *priv,
608 struct bcap_device *bcap_dev = video_drvdata(file);
624 static int bcap_g_input(
struct file *file,
void *priv,
unsigned int *
index)
626 struct bcap_device *bcap_dev = video_drvdata(file);
632 static int bcap_s_input(
struct file *file,
void *priv,
unsigned int index)
634 struct bcap_device *bcap_dev = video_drvdata(file);
656 static int bcap_try_format(
struct bcap_device *bcap,
663 struct v4l2_mbus_framefmt mbus_fmt;
678 v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->
mbus_code);
680 try_mbus_fmt, &mbus_fmt);
683 v4l2_fill_pix_format(pixfmt, &mbus_fmt);
689 static int bcap_enum_fmt_vid_cap(
struct file *file,
void *priv,
692 struct bcap_device *bcap_dev = video_drvdata(file);
706 static int bcap_try_fmt_vid_cap(
struct file *file,
void *priv,
709 struct bcap_device *bcap_dev = video_drvdata(file);
712 return bcap_try_format(bcap_dev, pixfmt,
NULL,
NULL);
715 static int bcap_g_fmt_vid_cap(
struct file *file,
void *priv,
718 struct bcap_device *bcap_dev = video_drvdata(file);
724 static int bcap_s_fmt_vid_cap(
struct file *file,
void *priv,
727 struct bcap_device *bcap_dev = video_drvdata(file);
728 struct v4l2_mbus_framefmt mbus_fmt;
737 ret = bcap_try_format(bcap_dev, pixfmt, &mbus_code, &bpp);
741 v4l2_fill_mbus_format(&mbus_fmt, pixfmt, mbus_code);
745 bcap_dev->
fmt = *pixfmt;
750 static int bcap_querycap(
struct file *file,
void *priv,
753 struct bcap_device *bcap_dev = video_drvdata(file);
762 static int bcap_g_parm(
struct file *file,
void *fh,
765 struct bcap_device *bcap_dev = video_drvdata(file);
772 static int bcap_s_parm(
struct file *file,
void *fh,
775 struct bcap_device *bcap_dev = video_drvdata(file);
782 static int bcap_g_chip_ident(
struct file *file,
void *priv,
785 struct bcap_device *bcap_dev = video_drvdata(file);
797 #ifdef CONFIG_VIDEO_ADV_DEBUG
798 static int bcap_dbg_g_register(
struct file *file,
void *priv,
801 struct bcap_device *bcap_dev = video_drvdata(file);
807 static int bcap_dbg_s_register(
struct file *file,
void *priv,
810 struct bcap_device *bcap_dev = video_drvdata(file);
817 static int bcap_log_status(
struct file *file,
void *priv)
819 struct bcap_device *bcap_dev = video_drvdata(file);
826 .vidioc_querycap = bcap_querycap,
827 .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap,
828 .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
829 .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap,
830 .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap,
831 .vidioc_enum_input = bcap_enum_input,
832 .vidioc_g_input = bcap_g_input,
833 .vidioc_s_input = bcap_s_input,
834 .vidioc_querystd = bcap_querystd,
835 .vidioc_s_std = bcap_s_std,
836 .vidioc_g_std = bcap_g_std,
837 .vidioc_reqbufs = bcap_reqbufs,
838 .vidioc_querybuf = bcap_querybuf,
839 .vidioc_qbuf = bcap_qbuf,
840 .vidioc_dqbuf = bcap_dqbuf,
841 .vidioc_streamon = bcap_streamon,
842 .vidioc_streamoff = bcap_streamoff,
843 .vidioc_g_parm = bcap_g_parm,
844 .vidioc_s_parm = bcap_s_parm,
845 .vidioc_g_chip_ident = bcap_g_chip_ident,
846 #ifdef CONFIG_VIDEO_ADV_DEBUG
847 .vidioc_g_register = bcap_dbg_g_register,
848 .vidioc_s_register = bcap_dbg_s_register,
850 .vidioc_log_status = bcap_log_status,
856 .release = bcap_release,
860 .get_unmapped_area = bcap_get_unmapped_area,
874 config = pdev->
dev.platform_data;
876 v4l2_err(pdev->
dev.driver,
"Unable to get board config\n");
880 bcap_dev = kzalloc(
sizeof(*bcap_dev),
GFP_KERNEL);
882 v4l2_err(pdev->
dev.driver,
"Unable to alloc bcap_dev\n");
889 if (!bcap_dev->
ppi) {
890 v4l2_err(pdev->
dev.driver,
"Unable to create ppi\n");
894 bcap_dev->
ppi->priv = bcap_dev;
905 v4l2_err(pdev->
dev.driver,
"Unable to alloc video device\n");
906 goto err_cleanup_ctx;
911 vfd->
fops = &bcap_fops;
922 "Unable to register v4l2 device\n");
923 goto err_release_vdev;
931 "Unable to init control handler\n");
942 q->
ops = &bcap_video_qops;
948 init_completion(&bcap_dev->
comp);
959 "Unable to register video device\n");
960 goto err_free_handler;
962 video_set_drvdata(bcap_dev->
video_dev, bcap_dev);
964 video_device_node_name(vfd));
970 "Unable to find i2c adapter\n");
986 "Unable to register sub device\n");
998 "Unable to get std\n");
1003 ret = bcap_init_sensor_formats(bcap_dev);
1006 "Unable to create sensor formats table\n");
1007 goto err_unreg_vdev;
1031 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1035 bcap_free_sensor_formats(bcap_dev);
1050 .probe = bcap_probe,
1054 static __init int bcap_init(
void)
1059 static __exit void bcap_exit(
void)