1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Marin Mitov *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 ***************************************************************************/
17 #include <linux/module.h>
18 #include <linux/version.h>
19 #include <linux/stringify.h>
20 #include <linux/delay.h>
21 #include <linux/kthread.h>
22 #include <linux/slab.h>
23 #include <media/v4l2-dev.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-common.h>
26 #include <media/videobuf2-dma-contig.h>
28 #include "dt3155v4l.h"
30 #define DT3155_DEVICE_ID 0x1223
33 * read_i2c_reg - reads an internal i2c register
35 * @addr: dt3155 mmio base address
36 * @index: index (internal address) of register to read
37 * @data: pointer to byte the read data will be placed in
39 * returns: zero on success or error code
41 * This function starts reading the specified (by index) register
42 * and busy waits for the process to finish. The result is placed
43 * in a byte pointed by data.
45 static int read_i2c_reg(void __iomem *addr, u8 index, u8 *data)
49 iowrite32((tmp << 17) | IIC_READ, addr + IIC_CSR2);
51 udelay(45); /* wait at least 43 usec for NEW_CYCLE to clear */
52 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
53 return -EIO; /* error: NEW_CYCLE not cleared */
54 tmp = ioread32(addr + IIC_CSR1);
55 if (tmp & DIRECT_ABORT) {
56 /* reset DIRECT_ABORT bit */
57 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
58 return -EIO; /* error: DIRECT_ABORT set */
65 * write_i2c_reg - writes to an internal i2c register
67 * @addr: dt3155 mmio base address
68 * @index: index (internal address) of register to read
69 * @data: data to be written
71 * returns: zero on success or error code
73 * This function starts writing the specified (by index) register
74 * and busy waits for the process to finish.
76 static int write_i2c_reg(void __iomem *addr, u8 index, u8 data)
80 iowrite32((tmp << 17) | IIC_WRITE | data, addr + IIC_CSR2);
82 udelay(65); /* wait at least 63 usec for NEW_CYCLE to clear */
83 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
84 return -EIO; /* error: NEW_CYCLE not cleared */
85 if (ioread32(addr + IIC_CSR1) & DIRECT_ABORT) {
86 /* reset DIRECT_ABORT bit */
87 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
88 return -EIO; /* error: DIRECT_ABORT set */
94 * write_i2c_reg_nowait - writes to an internal i2c register
96 * @addr: dt3155 mmio base address
97 * @index: index (internal address) of register to read
98 * @data: data to be written
100 * This function starts writing the specified (by index) register
103 static void write_i2c_reg_nowait(void __iomem *addr, u8 index, u8 data)
107 iowrite32((tmp << 17) | IIC_WRITE | data, addr + IIC_CSR2);
112 * wait_i2c_reg - waits the read/write to finish
114 * @addr: dt3155 mmio base address
116 * returns: zero on success or error code
118 * This function waits reading/writing to finish.
120 static int wait_i2c_reg(void __iomem *addr)
122 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
123 udelay(65); /* wait at least 63 usec for NEW_CYCLE to clear */
124 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
125 return -EIO; /* error: NEW_CYCLE not cleared */
126 if (ioread32(addr + IIC_CSR1) & DIRECT_ABORT) {
127 /* reset DIRECT_ABORT bit */
128 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
129 return -EIO; /* error: DIRECT_ABORT set */
135 dt3155_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
136 unsigned int *nbuffers, unsigned int *num_planes,
137 unsigned int sizes[], void *alloc_ctxs[])
140 struct dt3155_priv *pd = vb2_get_drv_priv(vq);
141 unsigned size = pd->width * pd->height;
143 if (vq->num_buffers + *nbuffers < 2)
144 *nbuffers = 2 - vq->num_buffers;
145 if (fmt && fmt->fmt.pix.sizeimage < size)
148 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : size;
149 alloc_ctxs[0] = pd->alloc_ctx;
153 static int dt3155_buf_prepare(struct vb2_buffer *vb)
155 struct dt3155_priv *pd = vb2_get_drv_priv(vb->vb2_queue);
157 vb2_set_plane_payload(vb, 0, pd->width * pd->height);
161 static int dt3155_start_streaming(struct vb2_queue *q, unsigned count)
163 struct dt3155_priv *pd = vb2_get_drv_priv(q);
164 struct vb2_buffer *vb = pd->curr_buf;
168 dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
169 iowrite32(dma_addr, pd->regs + EVEN_DMA_START);
170 iowrite32(dma_addr + pd->width, pd->regs + ODD_DMA_START);
171 iowrite32(pd->width, pd->regs + EVEN_DMA_STRIDE);
172 iowrite32(pd->width, pd->regs + ODD_DMA_STRIDE);
173 /* enable interrupts, clear all irq flags */
174 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START |
175 FLD_END_EVEN | FLD_END_ODD, pd->regs + INT_CSR);
176 iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
177 FLD_DN_ODD | FLD_DN_EVEN | CAP_CONT_EVEN | CAP_CONT_ODD,
179 wait_i2c_reg(pd->regs);
180 write_i2c_reg(pd->regs, CONFIG, pd->config);
181 write_i2c_reg(pd->regs, EVEN_CSR, CSR_ERROR | CSR_DONE);
182 write_i2c_reg(pd->regs, ODD_CSR, CSR_ERROR | CSR_DONE);
184 /* start the board */
185 write_i2c_reg(pd->regs, CSR2, pd->csr2 | BUSY_EVEN | BUSY_ODD);
189 static void dt3155_stop_streaming(struct vb2_queue *q)
191 struct dt3155_priv *pd = vb2_get_drv_priv(q);
192 struct vb2_buffer *vb;
194 spin_lock_irq(&pd->lock);
196 write_i2c_reg_nowait(pd->regs, CSR2, pd->csr2);
197 iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
198 FLD_DN_ODD | FLD_DN_EVEN, pd->regs + CSR1);
199 /* disable interrupts, clear all irq flags */
200 iowrite32(FLD_START | FLD_END_EVEN | FLD_END_ODD, pd->regs + INT_CSR);
201 spin_unlock_irq(&pd->lock);
204 * It is not clear whether the DMA stops at once or whether it
205 * will finish the current frame or field first. To be on the
206 * safe side we wait a bit.
210 spin_lock_irq(&pd->lock);
212 vb2_buffer_done(pd->curr_buf, VB2_BUF_STATE_ERROR);
216 while (!list_empty(&pd->dmaq)) {
217 vb = list_first_entry(&pd->dmaq, typeof(*vb), done_entry);
218 list_del(&vb->done_entry);
219 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
221 spin_unlock_irq(&pd->lock);
224 static void dt3155_buf_queue(struct vb2_buffer *vb)
226 struct dt3155_priv *pd = vb2_get_drv_priv(vb->vb2_queue);
228 /* pd->vidq.streaming = 1 when dt3155_buf_queue() is invoked */
229 spin_lock_irq(&pd->lock);
231 list_add_tail(&vb->done_entry, &pd->dmaq);
234 spin_unlock_irq(&pd->lock);
237 static const struct vb2_ops q_ops = {
238 .queue_setup = dt3155_queue_setup,
239 .wait_prepare = vb2_ops_wait_prepare,
240 .wait_finish = vb2_ops_wait_finish,
241 .buf_prepare = dt3155_buf_prepare,
242 .start_streaming = dt3155_start_streaming,
243 .stop_streaming = dt3155_stop_streaming,
244 .buf_queue = dt3155_buf_queue,
247 static irqreturn_t dt3155_irq_handler_even(int irq, void *dev_id)
249 struct dt3155_priv *ipd = dev_id;
250 struct vb2_buffer *ivb;
254 tmp = ioread32(ipd->regs + INT_CSR) & (FLD_START | FLD_END_ODD);
256 return IRQ_NONE; /* not our irq */
257 if ((tmp & FLD_START) && !(tmp & FLD_END_ODD)) {
258 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START,
259 ipd->regs + INT_CSR);
260 return IRQ_HANDLED; /* start of field irq */
262 tmp = ioread32(ipd->regs + CSR1) & (FLD_CRPT_EVEN | FLD_CRPT_ODD);
264 iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
265 FLD_DN_ODD | FLD_DN_EVEN |
266 CAP_CONT_EVEN | CAP_CONT_ODD,
271 spin_lock(&ipd->lock);
272 if (ipd->curr_buf && !list_empty(&ipd->dmaq)) {
273 v4l2_get_timestamp(&ipd->curr_buf->v4l2_buf.timestamp);
274 ipd->curr_buf->v4l2_buf.sequence = ipd->sequence++;
275 ipd->curr_buf->v4l2_buf.field = V4L2_FIELD_NONE;
276 vb2_buffer_done(ipd->curr_buf, VB2_BUF_STATE_DONE);
278 ivb = list_first_entry(&ipd->dmaq, typeof(*ivb), done_entry);
279 list_del(&ivb->done_entry);
281 dma_addr = vb2_dma_contig_plane_dma_addr(ivb, 0);
282 iowrite32(dma_addr, ipd->regs + EVEN_DMA_START);
283 iowrite32(dma_addr + ipd->width, ipd->regs + ODD_DMA_START);
284 iowrite32(ipd->width, ipd->regs + EVEN_DMA_STRIDE);
285 iowrite32(ipd->width, ipd->regs + ODD_DMA_STRIDE);
289 /* enable interrupts, clear all irq flags */
290 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START |
291 FLD_END_EVEN | FLD_END_ODD, ipd->regs + INT_CSR);
292 spin_unlock(&ipd->lock);
296 static const struct v4l2_file_operations dt3155_fops = {
297 .owner = THIS_MODULE,
298 .open = v4l2_fh_open,
299 .release = vb2_fop_release,
300 .unlocked_ioctl = video_ioctl2,
301 .read = vb2_fop_read,
302 .mmap = vb2_fop_mmap,
306 static int dt3155_querycap(struct file *filp, void *p, struct v4l2_capability *cap)
308 struct dt3155_priv *pd = video_drvdata(filp);
310 strcpy(cap->driver, DT3155_NAME);
311 strcpy(cap->card, DT3155_NAME " frame grabber");
312 sprintf(cap->bus_info, "PCI:%s", pci_name(pd->pdev));
313 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
314 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
315 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
319 static int dt3155_enum_fmt_vid_cap(struct file *filp, void *p, struct v4l2_fmtdesc *f)
323 f->pixelformat = V4L2_PIX_FMT_GREY;
324 strcpy(f->description, "8-bit Greyscale");
328 static int dt3155_fmt_vid_cap(struct file *filp, void *p, struct v4l2_format *f)
330 struct dt3155_priv *pd = video_drvdata(filp);
332 f->fmt.pix.width = pd->width;
333 f->fmt.pix.height = pd->height;
334 f->fmt.pix.pixelformat = V4L2_PIX_FMT_GREY;
335 f->fmt.pix.field = V4L2_FIELD_NONE;
336 f->fmt.pix.bytesperline = f->fmt.pix.width;
337 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height;
338 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
342 static int dt3155_g_std(struct file *filp, void *p, v4l2_std_id *norm)
344 struct dt3155_priv *pd = video_drvdata(filp);
350 static int dt3155_s_std(struct file *filp, void *p, v4l2_std_id norm)
352 struct dt3155_priv *pd = video_drvdata(filp);
356 if (vb2_is_busy(&pd->vidq))
359 if (pd->std & V4L2_STD_525_60) {
371 static int dt3155_enum_input(struct file *filp, void *p, struct v4l2_input *input)
373 if (input->index > 3)
376 snprintf(input->name, sizeof(input->name), "VID%d", input->index);
378 strlcpy(input->name, "J2/VID0", sizeof(input->name));
379 input->type = V4L2_INPUT_TYPE_CAMERA;
380 input->std = V4L2_STD_ALL;
385 static int dt3155_g_input(struct file *filp, void *p, unsigned int *i)
387 struct dt3155_priv *pd = video_drvdata(filp);
393 static int dt3155_s_input(struct file *filp, void *p, unsigned int i)
395 struct dt3155_priv *pd = video_drvdata(filp);
400 write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
401 write_i2c_reg(pd->regs, AD_CMD, (i << 6) | (i << 4) | SYNC_LVL_3);
405 static const struct v4l2_ioctl_ops dt3155_ioctl_ops = {
406 .vidioc_querycap = dt3155_querycap,
407 .vidioc_enum_fmt_vid_cap = dt3155_enum_fmt_vid_cap,
408 .vidioc_try_fmt_vid_cap = dt3155_fmt_vid_cap,
409 .vidioc_g_fmt_vid_cap = dt3155_fmt_vid_cap,
410 .vidioc_s_fmt_vid_cap = dt3155_fmt_vid_cap,
411 .vidioc_reqbufs = vb2_ioctl_reqbufs,
412 .vidioc_create_bufs = vb2_ioctl_create_bufs,
413 .vidioc_querybuf = vb2_ioctl_querybuf,
414 .vidioc_expbuf = vb2_ioctl_expbuf,
415 .vidioc_qbuf = vb2_ioctl_qbuf,
416 .vidioc_dqbuf = vb2_ioctl_dqbuf,
417 .vidioc_streamon = vb2_ioctl_streamon,
418 .vidioc_streamoff = vb2_ioctl_streamoff,
419 .vidioc_g_std = dt3155_g_std,
420 .vidioc_s_std = dt3155_s_std,
421 .vidioc_enum_input = dt3155_enum_input,
422 .vidioc_g_input = dt3155_g_input,
423 .vidioc_s_input = dt3155_s_input,
426 static int dt3155_init_board(struct dt3155_priv *pd)
428 struct pci_dev *pdev = pd->pdev;
432 pci_set_master(pdev); /* dt3155 needs it */
434 /* resetting the adapter */
435 iowrite32(ADDR_ERR_ODD | ADDR_ERR_EVEN | FLD_CRPT_ODD | FLD_CRPT_EVEN |
436 FLD_DN_ODD | FLD_DN_EVEN, pd->regs + CSR1);
440 /* initializing adapter registers */
441 iowrite32(FIFO_EN | SRST, pd->regs + CSR1);
443 iowrite32(0xEEEEEE01, pd->regs + EVEN_PIXEL_FMT);
444 iowrite32(0xEEEEEE01, pd->regs + ODD_PIXEL_FMT);
445 iowrite32(0x00000020, pd->regs + FIFO_TRIGER);
446 iowrite32(0x00000103, pd->regs + XFER_MODE);
447 iowrite32(0, pd->regs + RETRY_WAIT_CNT);
448 iowrite32(0, pd->regs + INT_CSR);
449 iowrite32(1, pd->regs + EVEN_FLD_MASK);
450 iowrite32(1, pd->regs + ODD_FLD_MASK);
451 iowrite32(0, pd->regs + MASK_LENGTH);
452 iowrite32(0x0005007C, pd->regs + FIFO_FLAG_CNT);
453 iowrite32(0x01010101, pd->regs + IIC_CLK_DUR);
456 /* verifying that we have a DT3155 board (not just a SAA7116 chip) */
457 read_i2c_reg(pd->regs, DT_ID, &tmp);
458 if (tmp != DT3155_ID)
461 /* initialize AD LUT */
462 write_i2c_reg(pd->regs, AD_ADDR, 0);
463 for (i = 0; i < 256; i++)
464 write_i2c_reg(pd->regs, AD_LUT, i);
466 /* initialize ADC references */
467 /* FIXME: pos_ref & neg_ref depend on VT_50HZ */
468 write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
469 write_i2c_reg(pd->regs, AD_CMD, VIDEO_CNL_1 | SYNC_CNL_1 | SYNC_LVL_3);
470 write_i2c_reg(pd->regs, AD_ADDR, AD_POS_REF);
471 write_i2c_reg(pd->regs, AD_CMD, 34);
472 write_i2c_reg(pd->regs, AD_ADDR, AD_NEG_REF);
473 write_i2c_reg(pd->regs, AD_CMD, 0);
475 /* initialize PM LUT */
476 write_i2c_reg(pd->regs, CONFIG, pd->config | PM_LUT_PGM);
477 for (i = 0; i < 256; i++) {
478 write_i2c_reg(pd->regs, PM_LUT_ADDR, i);
479 write_i2c_reg(pd->regs, PM_LUT_DATA, i);
481 write_i2c_reg(pd->regs, CONFIG, pd->config | PM_LUT_PGM | PM_LUT_SEL);
482 for (i = 0; i < 256; i++) {
483 write_i2c_reg(pd->regs, PM_LUT_ADDR, i);
484 write_i2c_reg(pd->regs, PM_LUT_DATA, i);
486 write_i2c_reg(pd->regs, CONFIG, pd->config); /* ACQ_MODE_EVEN */
488 /* select channel 1 for input and set sync level */
489 write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
490 write_i2c_reg(pd->regs, AD_CMD, VIDEO_CNL_1 | SYNC_CNL_1 | SYNC_LVL_3);
492 /* disable all irqs, clear all irq flags */
493 iowrite32(FLD_START | FLD_END_EVEN | FLD_END_ODD,
499 static struct video_device dt3155_vdev = {
501 .fops = &dt3155_fops,
502 .ioctl_ops = &dt3155_ioctl_ops,
504 .release = video_device_release_empty,
505 .tvnorms = V4L2_STD_ALL,
508 static int dt3155_probe(struct pci_dev *pdev, const struct pci_device_id *id)
511 struct dt3155_priv *pd;
513 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
516 pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL);
520 err = v4l2_device_register(&pdev->dev, &pd->v4l2_dev);
523 pd->vdev = dt3155_vdev;
524 pd->vdev.v4l2_dev = &pd->v4l2_dev;
525 video_set_drvdata(&pd->vdev, pd); /* for use in video_fops */
527 pd->std = V4L2_STD_625_50;
531 INIT_LIST_HEAD(&pd->dmaq);
532 mutex_init(&pd->mux);
533 pd->vdev.lock = &pd->mux; /* for locking v4l2_file_operations */
534 pd->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
535 pd->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
536 pd->vidq.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
537 pd->vidq.ops = &q_ops;
538 pd->vidq.mem_ops = &vb2_dma_contig_memops;
539 pd->vidq.drv_priv = pd;
540 pd->vidq.min_buffers_needed = 2;
541 pd->vidq.lock = &pd->mux; /* for locking v4l2_file_operations */
542 pd->vdev.queue = &pd->vidq;
543 err = vb2_queue_init(&pd->vidq);
545 goto err_v4l2_dev_unreg;
546 pd->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
547 if (IS_ERR(pd->alloc_ctx)) {
548 dev_err(&pdev->dev, "Can't allocate buffer context");
549 err = PTR_ERR(pd->alloc_ctx);
550 goto err_v4l2_dev_unreg;
552 spin_lock_init(&pd->lock);
553 pd->config = ACQ_MODE_EVEN;
554 err = pci_enable_device(pdev);
557 err = pci_request_region(pdev, 0, pci_name(pdev));
559 goto err_pci_disable;
560 pd->regs = pci_iomap(pdev, 0, pci_resource_len(pd->pdev, 0));
565 err = dt3155_init_board(pd);
568 err = request_irq(pd->pdev->irq, dt3155_irq_handler_even,
569 IRQF_SHARED, DT3155_NAME, pd);
572 err = video_register_device(&pd->vdev, VFL_TYPE_GRABBER, -1);
575 dev_info(&pdev->dev, "/dev/video%i is ready\n", pd->vdev.minor);
576 return 0; /* success */
579 free_irq(pd->pdev->irq, pd);
581 pci_iounmap(pdev, pd->regs);
583 pci_release_region(pdev, 0);
585 pci_disable_device(pdev);
587 vb2_dma_contig_cleanup_ctx(pd->alloc_ctx);
589 v4l2_device_unregister(&pd->v4l2_dev);
593 static void dt3155_remove(struct pci_dev *pdev)
595 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
596 struct dt3155_priv *pd = container_of(v4l2_dev, struct dt3155_priv, v4l2_dev);
598 video_unregister_device(&pd->vdev);
599 free_irq(pd->pdev->irq, pd);
600 vb2_queue_release(&pd->vidq);
601 v4l2_device_unregister(&pd->v4l2_dev);
602 pci_iounmap(pdev, pd->regs);
603 pci_release_region(pdev, 0);
604 pci_disable_device(pdev);
605 vb2_dma_contig_cleanup_ctx(pd->alloc_ctx);
608 static const struct pci_device_id pci_ids[] = {
609 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, DT3155_DEVICE_ID) },
610 { 0, /* zero marks the end */ },
612 MODULE_DEVICE_TABLE(pci, pci_ids);
614 static struct pci_driver pci_driver = {
617 .probe = dt3155_probe,
618 .remove = dt3155_remove,
621 module_pci_driver(pci_driver);
623 MODULE_DESCRIPTION("video4linux pci-driver for dt3155 frame grabber");
624 MODULE_AUTHOR("Marin Mitov <mitov@issp.bas.bg>");
625 MODULE_VERSION(DT3155_VERSION);
626 MODULE_LICENSE("GPL");