21 #include <linux/module.h>
22 #include <linux/version.h>
26 #include <linux/slab.h>
33 #define DT3155_VENDOR_ID 0x8086
34 #define DT3155_DEVICE_ID 0x1223
37 #define DT3155_CHUNK_SIZE (1U << 22)
39 #define DT3155_COH_FLAGS (GFP_KERNEL | GFP_DMA32 | __GFP_COLD | __GFP_NOWARN)
41 #define DT3155_BUF_SIZE (768 * 576)
43 #ifdef CONFIG_DT3155_STREAMING
44 #define DT3155_CAPTURE_METHOD V4L2_CAP_STREAMING
46 #define DT3155_CAPTURE_METHOD V4L2_CAP_READWRITE
50 #ifdef CONFIG_DT3155_CCIR
51 static const u8 csr2_init = VT_50HZ;
52 #define DT3155_CURRENT_NORM V4L2_STD_625_50
53 static const unsigned int img_width = 768;
54 static const unsigned int img_height = 576;
55 static const unsigned int frames_per_sec = 25;
61 .description =
"CCIR/50Hz 8 bits gray",
66 static const u8 csr2_init = VT_60HZ;
67 #define DT3155_CURRENT_NORM V4L2_STD_525_60
68 static const unsigned int img_width = 640;
69 static const unsigned int img_height = 480;
70 static const unsigned int frames_per_sec = 30;
76 .description =
"RS-170/60Hz 8 bits gray",
82 #define NUM_OF_FORMATS ARRAY_SIZE(frame_std)
84 static u8 config_init = ACQ_MODE_EVEN;
104 iowrite32((tmp<<17) | IIC_READ, addr + IIC_CSR2);
107 if (
ioread32(addr + IIC_CSR2) & NEW_CYCLE)
110 if (tmp & DIRECT_ABORT) {
112 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
136 iowrite32((tmp<<17) | IIC_WRITE | data, addr + IIC_CSR2);
139 if (
ioread32(addr + IIC_CSR2) & NEW_CYCLE)
141 if (
ioread32(addr + IIC_CSR1) & DIRECT_ABORT) {
143 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
159 static void write_i2c_reg_nowait(
void __iomem *addr,
u8 index,
u8 data)
163 iowrite32((tmp<<17) | IIC_WRITE | data, addr + IIC_CSR2);
176 static int wait_i2c_reg(
void __iomem *addr)
178 if (
ioread32(addr + IIC_CSR2) & NEW_CYCLE)
180 if (
ioread32(addr + IIC_CSR2) & NEW_CYCLE)
182 if (
ioread32(addr + IIC_CSR1) & DIRECT_ABORT) {
184 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
191 dt3155_start_acq(
struct dt3155_priv *pd)
196 dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
197 iowrite32(dma_addr, pd->regs + EVEN_DMA_START);
198 iowrite32(dma_addr + img_width, pd->regs + ODD_DMA_START);
199 iowrite32(img_width, pd->regs + EVEN_DMA_STRIDE);
200 iowrite32(img_width, pd->regs + ODD_DMA_STRIDE);
202 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START |
203 FLD_END_EVEN | FLD_END_ODD, pd->regs + INT_CSR);
205 FLD_DN_ODD | FLD_DN_EVEN | CAP_CONT_EVEN | CAP_CONT_ODD,
207 wait_i2c_reg(pd->regs);
208 write_i2c_reg(pd->regs,
CONFIG, pd->config);
209 write_i2c_reg(pd->regs, EVEN_CSR, CSR_ERROR | CSR_DONE);
210 write_i2c_reg(pd->regs, ODD_CSR, CSR_ERROR | CSR_DONE);
213 write_i2c_reg(pd->regs,
CSR2, pd->csr2 | BUSY_EVEN | BUSY_ODD);
222 unsigned int *num_buffers,
unsigned int *
num_planes,
223 unsigned int sizes[],
void *alloc_ctxs[])
226 struct dt3155_priv *
pd = vb2_get_drv_priv(q);
229 if (*num_buffers == 0)
232 sizes[0] = img_width * img_height;
233 if (pd->q->alloc_ctx[0])
238 pd->q->alloc_ctx[0] =
ret;
245 struct dt3155_priv *pd = vb2_get_drv_priv(q);
253 struct dt3155_priv *pd = vb2_get_drv_priv(q);
261 vb2_set_plane_payload(vb, 0, img_width * img_height);
266 dt3155_stop_streaming(
struct vb2_queue *q)
268 struct dt3155_priv *pd = vb2_get_drv_priv(q);
271 spin_lock_irq(&pd->lock);
272 while (!list_empty(&pd->dmaq)) {
277 spin_unlock_irq(&pd->lock);
285 struct dt3155_priv *pd = vb2_get_drv_priv(vb->
vb2_queue);
288 spin_lock_irq(&pd->lock);
293 dt3155_start_acq(pd);
295 spin_unlock_irq(&pd->lock);
302 .queue_setup = dt3155_queue_setup,
303 .wait_prepare = dt3155_wait_prepare,
304 .wait_finish = dt3155_wait_finish,
305 .buf_prepare = dt3155_buf_prepare,
306 .stop_streaming = dt3155_stop_streaming,
307 .buf_queue = dt3155_buf_queue,
311 dt3155_irq_handler_even(
int irq,
void *
dev_id)
313 struct dt3155_priv *ipd =
dev_id;
318 tmp =
ioread32(ipd->regs + INT_CSR) & (FLD_START | FLD_END_ODD);
321 if ((tmp & FLD_START) && !(tmp & FLD_END_ODD)) {
322 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START,
323 ipd->regs + INT_CSR);
327 if ((tmp & FLD_START) && (tmp & FLD_END_ODD))
328 ipd->stats.start_before_end++;
332 tmp =
ioread32(ipd->regs +
CSR1) & (FLD_CRPT_EVEN | FLD_CRPT_ODD);
334 ipd->stats.corrupted_fields++;
336 FLD_DN_ODD | FLD_DN_EVEN |
337 CAP_CONT_EVEN | CAP_CONT_ODD,
342 spin_lock(&ipd->lock);
345 ipd->curr_buf->v4l2_buf.sequence = (ipd->field_count) >> 1;
349 if (!ipd->q->streaming || list_empty(&ipd->dmaq))
354 dma_addr = vb2_dma_contig_plane_dma_addr(ivb, 0);
355 iowrite32(dma_addr, ipd->regs + EVEN_DMA_START);
356 iowrite32(dma_addr + img_width, ipd->regs + ODD_DMA_START);
357 iowrite32(img_width, ipd->regs + EVEN_DMA_STRIDE);
358 iowrite32(img_width, ipd->regs + ODD_DMA_STRIDE);
361 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START |
362 FLD_END_EVEN | FLD_END_ODD, ipd->regs + INT_CSR);
363 spin_unlock(&ipd->lock);
367 ipd->curr_buf =
NULL;
369 write_i2c_reg_nowait(ipd->regs,
CSR2, ipd->csr2);
371 FLD_DN_ODD | FLD_DN_EVEN, ipd->regs +
CSR1);
373 iowrite32(FLD_START | FLD_END_EVEN | FLD_END_ODD, ipd->regs + INT_CSR);
374 spin_unlock(&ipd->lock);
379 dt3155_open(
struct file *filp)
382 struct dt3155_priv *pd = video_drvdata(filp);
390 goto err_alloc_queue;
396 pd->q->drv_priv = pd;
400 INIT_LIST_HEAD(&pd->dmaq);
403 iowrite32(FLD_START | FLD_END_EVEN | FLD_END_ODD,
405 ret =
request_irq(pd->pdev->irq, dt3155_irq_handler_even,
408 goto err_request_irq;
421 dt3155_release(
struct file *filp)
423 struct dt3155_priv *pd = video_drvdata(filp);
431 if (pd->q->alloc_ctx[0])
441 dt3155_read(
struct file *filp,
char __user *
user,
size_t size, loff_t *loff)
443 struct dt3155_priv *pd = video_drvdata(filp);
456 struct dt3155_priv *pd = video_drvdata(filp);
460 res =
vb2_poll(pd->q, filp, polltbl);
468 struct dt3155_priv *pd = video_drvdata(filp);
481 .release = dt3155_release,
491 struct dt3155_priv *pd = video_drvdata(filp);
499 struct dt3155_priv *pd = video_drvdata(filp);
507 struct dt3155_priv *pd = video_drvdata(filp);
510 strcpy(cap->
card, DT3155_NAME
" frame grabber");
513 KERNEL_VERSION(DT3155_VER_MAJ, DT3155_VER_MIN, DT3155_VER_EXT);
524 *f = frame_std[f->
index];
529 dt3155_ioc_g_fmt_vid_cap(
struct file *filp,
void *p,
struct v4l2_format *f)
533 f->
fmt.
pix.width = img_width;
534 f->
fmt.
pix.height = img_height;
539 f->
fmt.
pix.colorspace = 0;
545 dt3155_ioc_try_fmt_vid_cap(
struct file *filp,
void *p,
struct v4l2_format *f)
549 if (f->
fmt.
pix.width == img_width &&
550 f->
fmt.
pix.height == img_height &&
561 dt3155_ioc_s_fmt_vid_cap(
struct file *filp,
void *p,
struct v4l2_format *f)
563 return dt3155_ioc_g_fmt_vid_cap(filp, p, f);
569 struct dt3155_priv *pd = video_drvdata(filp);
577 struct dt3155_priv *pd = video_drvdata(filp);
585 struct dt3155_priv *pd = video_drvdata(filp);
593 struct dt3155_priv *pd = video_drvdata(filp);
638 dt3155_ioc_g_input(
struct file *filp,
void *p,
unsigned int *
i)
645 dt3155_ioc_s_input(
struct file *filp,
void *p,
unsigned int i)
660 parms->
parm.
capture.timeperframe.denominator = frames_per_sec * 1000;
674 parms->
parm.
capture.timeperframe.denominator = frames_per_sec * 1000;
681 .vidioc_streamon = dt3155_ioc_streamon,
682 .vidioc_streamoff = dt3155_ioc_streamoff,
683 .vidioc_querycap = dt3155_ioc_querycap,
688 .vidioc_enum_fmt_vid_cap = dt3155_ioc_enum_fmt_vid_cap,
689 .vidioc_try_fmt_vid_cap = dt3155_ioc_try_fmt_vid_cap,
690 .vidioc_g_fmt_vid_cap = dt3155_ioc_g_fmt_vid_cap,
691 .vidioc_s_fmt_vid_cap = dt3155_ioc_s_fmt_vid_cap,
692 .vidioc_reqbufs = dt3155_ioc_reqbufs,
693 .vidioc_querybuf = dt3155_ioc_querybuf,
694 .vidioc_qbuf = dt3155_ioc_qbuf,
695 .vidioc_dqbuf = dt3155_ioc_dqbuf,
696 .vidioc_querystd = dt3155_ioc_querystd,
697 .vidioc_g_std = dt3155_ioc_g_std,
698 .vidioc_s_std = dt3155_ioc_s_std,
699 .vidioc_enum_input = dt3155_ioc_enum_input,
700 .vidioc_g_input = dt3155_ioc_g_input,
701 .vidioc_s_input = dt3155_ioc_s_input,
710 .vidioc_g_parm = dt3155_ioc_g_parm,
711 .vidioc_s_parm = dt3155_ioc_s_parm,
722 dt3155_init_board(
struct pci_dev *pdev)
724 struct dt3155_priv *pd = pci_get_drvdata(pdev);
733 iowrite32(FLD_CRPT_ODD | FLD_CRPT_EVEN | FLD_DN_ODD | FLD_DN_EVEN,
741 iowrite32(0xEEEEEE01, pd->regs + EVEN_PIXEL_FMT);
742 iowrite32(0xEEEEEE01, pd->regs + ODD_PIXEL_FMT);
743 iowrite32(0x00000020, pd->regs + FIFO_TRIGER);
744 iowrite32(0x00000103, pd->regs + XFER_MODE);
750 iowrite32(0x0005007C, pd->regs + FIFO_FLAG_CNT);
751 iowrite32(0x01010101, pd->regs + IIC_CLK_DUR);
755 read_i2c_reg(pd->regs, DT_ID, &tmp);
756 if (tmp != DT3155_ID)
760 write_i2c_reg(pd->regs, AD_ADDR, 0);
761 for (i = 0; i < 256; i++)
762 write_i2c_reg(pd->regs, AD_LUT, i);
766 write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
767 write_i2c_reg(pd->regs, AD_CMD, VIDEO_CNL_1 | SYNC_CNL_1 | SYNC_LVL_3);
768 write_i2c_reg(pd->regs, AD_ADDR, AD_POS_REF);
769 write_i2c_reg(pd->regs, AD_CMD, 34);
770 write_i2c_reg(pd->regs, AD_ADDR, AD_NEG_REF);
771 write_i2c_reg(pd->regs, AD_CMD, 0);
774 write_i2c_reg(pd->regs,
CONFIG, pd->config | PM_LUT_PGM);
775 for (i = 0; i < 256; i++) {
776 write_i2c_reg(pd->regs, PM_LUT_ADDR, i);
777 write_i2c_reg(pd->regs, PM_LUT_DATA, i);
779 write_i2c_reg(pd->regs,
CONFIG, pd->config | PM_LUT_PGM | PM_LUT_SEL);
780 for (i = 0; i < 256; i++) {
781 write_i2c_reg(pd->regs, PM_LUT_ADDR, i);
782 write_i2c_reg(pd->regs, PM_LUT_DATA, i);
784 write_i2c_reg(pd->regs,
CONFIG, pd->config);
787 write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
788 write_i2c_reg(pd->regs, AD_CMD, VIDEO_CNL_1 | SYNC_CNL_1 | SYNC_LVL_3);
795 iowrite32(buf_dma, pd->regs + EVEN_DMA_START);
796 iowrite32(buf_dma, pd->regs + ODD_DMA_START);
797 iowrite32(0, pd->regs + EVEN_DMA_STRIDE);
802 write_i2c_reg(pd->regs,
CSR2, pd->csr2 | SYNC_SNTL);
803 write_i2c_reg(pd->regs,
CONFIG, pd->config);
804 write_i2c_reg(pd->regs, EVEN_CSR, CSR_SNGL);
805 write_i2c_reg(pd->regs,
CSR2, pd->csr2 | BUSY_EVEN | SYNC_SNTL);
807 read_i2c_reg(pd->regs,
CSR2, &tmp);
808 write_i2c_reg(pd->regs, EVEN_CSR, CSR_ERROR | CSR_SNGL | CSR_DONE);
809 write_i2c_reg(pd->regs, ODD_CSR, CSR_ERROR | CSR_SNGL | CSR_DONE);
810 write_i2c_reg(pd->regs,
CSR2, pd->csr2);
822 .fops = &dt3155_fops,
823 .ioctl_ops = &dt3155_ioctl_ops,
860 goto err_alloc_coherent;
881 dt3155_free_coherent(
struct device *dev)
898 struct dt3155_priv *pd;
911 goto err_video_device_alloc;
912 *pd->vdev = dt3155_vdev;
913 pci_set_drvdata(pdev, pd);
914 video_set_drvdata(pd->vdev, pd);
917 INIT_LIST_HEAD(&pd->dmaq);
919 pd->vdev->lock = &pd->mux;
921 pd->csr2 = csr2_init;
922 pd->config = config_init;
934 err = dt3155_init_board(pdev);
943 dev_info(&pdev->
dev,
"/dev/video%i is ready\n", pd->vdev->minor);
954 err_video_device_alloc:
960 dt3155_remove(
struct pci_dev *pdev)
962 struct dt3155_priv *pd = pci_get_drvdata(pdev);
964 dt3155_free_coherent(&pdev->
dev);
985 .probe = dt3155_probe,