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 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <linux/module.h>
22 #include <linux/stringify.h>
23 #include <linux/delay.h>
24 #include <linux/kthread.h>
25 #include <linux/slab.h>
26 #include <media/v4l2-dev.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/v4l2-common.h>
29 #include <media/videobuf2-dma-contig.h>
31 #include "dt3155v4l.h"
33 #define DT3155_DEVICE_ID 0x1223
35 /* DT3155_CHUNK_SIZE is 4M (2^22) 8 full size buffers */
36 #define DT3155_CHUNK_SIZE (1U << 22)
38 #define DT3155_COH_FLAGS (GFP_KERNEL | GFP_DMA32 | __GFP_COLD | __GFP_NOWARN)
40 #define DT3155_BUF_SIZE (768 * 576)
42 #ifdef CONFIG_DT3155_STREAMING
43 #define DT3155_CAPTURE_METHOD V4L2_CAP_STREAMING
45 #define DT3155_CAPTURE_METHOD V4L2_CAP_READWRITE
48 /* global initializers (for all boards) */
49 #ifdef CONFIG_DT3155_CCIR
50 static const u8 csr2_init = VT_50HZ;
51 #define DT3155_CURRENT_NORM V4L2_STD_625_50
52 static const unsigned int img_width = 768;
53 static const unsigned int img_height = 576;
54 static const unsigned int frames_per_sec = 25;
55 static const struct v4l2_fmtdesc frame_std[] = {
58 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
60 .description = "CCIR/50Hz 8 bits gray",
61 .pixelformat = V4L2_PIX_FMT_GREY,
65 static const u8 csr2_init = VT_60HZ;
66 #define DT3155_CURRENT_NORM V4L2_STD_525_60
67 static const unsigned int img_width = 640;
68 static const unsigned int img_height = 480;
69 static const unsigned int frames_per_sec = 30;
70 static const struct v4l2_fmtdesc frame_std[] = {
73 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
75 .description = "RS-170/60Hz 8 bits gray",
76 .pixelformat = V4L2_PIX_FMT_GREY,
81 #define NUM_OF_FORMATS ARRAY_SIZE(frame_std)
83 static u8 config_init = ACQ_MODE_EVEN;
86 * read_i2c_reg - reads an internal i2c register
88 * @addr: dt3155 mmio base address
89 * @index: index (internal address) of register to read
90 * @data: pointer to byte the read data will be placed in
92 * returns: zero on success or error code
94 * This function starts reading the specified (by index) register
95 * and busy waits for the process to finish. The result is placed
96 * in a byte pointed by data.
99 read_i2c_reg(void __iomem *addr, u8 index, u8 *data)
103 iowrite32((tmp<<17) | IIC_READ, addr + IIC_CSR2);
105 udelay(45); /* wait at least 43 usec for NEW_CYCLE to clear */
106 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
107 return -EIO; /* error: NEW_CYCLE not cleared */
108 tmp = ioread32(addr + IIC_CSR1);
109 if (tmp & DIRECT_ABORT) {
110 /* reset DIRECT_ABORT bit */
111 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
112 return -EIO; /* error: DIRECT_ABORT set */
119 * write_i2c_reg - writes to an internal i2c register
121 * @addr: dt3155 mmio base address
122 * @index: index (internal address) of register to read
123 * @data: data to be written
125 * returns: zero on success or error code
127 * This function starts writting the specified (by index) register
128 * and busy waits for the process to finish.
131 write_i2c_reg(void __iomem *addr, u8 index, u8 data)
135 iowrite32((tmp<<17) | IIC_WRITE | data, addr + IIC_CSR2);
137 udelay(65); /* wait at least 63 usec for NEW_CYCLE to clear */
138 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
139 return -EIO; /* error: NEW_CYCLE not cleared */
140 if (ioread32(addr + IIC_CSR1) & DIRECT_ABORT) {
141 /* reset DIRECT_ABORT bit */
142 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
143 return -EIO; /* error: DIRECT_ABORT set */
149 * write_i2c_reg_nowait - writes to an internal i2c register
151 * @addr: dt3155 mmio base address
152 * @index: index (internal address) of register to read
153 * @data: data to be written
155 * This function starts writting the specified (by index) register
158 static void write_i2c_reg_nowait(void __iomem *addr, u8 index, u8 data)
162 iowrite32((tmp<<17) | IIC_WRITE | data, addr + IIC_CSR2);
167 * wait_i2c_reg - waits the read/write to finish
169 * @addr: dt3155 mmio base address
171 * returns: zero on success or error code
173 * This function waits reading/writting to finish.
175 static int wait_i2c_reg(void __iomem *addr)
177 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
178 udelay(65); /* wait at least 63 usec for NEW_CYCLE to clear */
179 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
180 return -EIO; /* error: NEW_CYCLE not cleared */
181 if (ioread32(addr + IIC_CSR1) & DIRECT_ABORT) {
182 /* reset DIRECT_ABORT bit */
183 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
184 return -EIO; /* error: DIRECT_ABORT set */
190 dt3155_start_acq(struct dt3155_priv *pd)
192 struct vb2_buffer *vb = pd->curr_buf;
195 dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
196 iowrite32(dma_addr, pd->regs + EVEN_DMA_START);
197 iowrite32(dma_addr + img_width, pd->regs + ODD_DMA_START);
198 iowrite32(img_width, pd->regs + EVEN_DMA_STRIDE);
199 iowrite32(img_width, pd->regs + ODD_DMA_STRIDE);
200 /* enable interrupts, clear all irq flags */
201 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START |
202 FLD_END_EVEN | FLD_END_ODD, pd->regs + INT_CSR);
203 iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
204 FLD_DN_ODD | FLD_DN_EVEN | CAP_CONT_EVEN | CAP_CONT_ODD,
206 wait_i2c_reg(pd->regs);
207 write_i2c_reg(pd->regs, CONFIG, pd->config);
208 write_i2c_reg(pd->regs, EVEN_CSR, CSR_ERROR | CSR_DONE);
209 write_i2c_reg(pd->regs, ODD_CSR, CSR_ERROR | CSR_DONE);
211 /* start the board */
212 write_i2c_reg(pd->regs, CSR2, pd->csr2 | BUSY_EVEN | BUSY_ODD);
213 return 0; /* success */
217 * driver-specific callbacks (vb2_ops)
220 dt3155_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
221 unsigned int *num_buffers, unsigned int *num_planes,
222 unsigned int sizes[], void *alloc_ctxs[])
225 struct dt3155_priv *pd = vb2_get_drv_priv(q);
228 if (*num_buffers == 0)
231 sizes[0] = img_width * img_height;
232 if (pd->q->alloc_ctx[0])
234 ret = vb2_dma_contig_init_ctx(&pd->pdev->dev);
237 pd->q->alloc_ctx[0] = ret;
242 dt3155_wait_prepare(struct vb2_queue *q)
244 struct dt3155_priv *pd = vb2_get_drv_priv(q);
246 mutex_unlock(pd->vdev.lock);
250 dt3155_wait_finish(struct vb2_queue *q)
252 struct dt3155_priv *pd = vb2_get_drv_priv(q);
254 mutex_lock(pd->vdev.lock);
258 dt3155_buf_prepare(struct vb2_buffer *vb)
260 vb2_set_plane_payload(vb, 0, img_width * img_height);
265 dt3155_stop_streaming(struct vb2_queue *q)
267 struct dt3155_priv *pd = vb2_get_drv_priv(q);
268 struct vb2_buffer *vb;
270 spin_lock_irq(&pd->lock);
271 while (!list_empty(&pd->dmaq)) {
272 vb = list_first_entry(&pd->dmaq, typeof(*vb), done_entry);
273 list_del(&vb->done_entry);
274 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
276 spin_unlock_irq(&pd->lock);
277 msleep(45); /* irq hendler will stop the hardware */
281 dt3155_buf_queue(struct vb2_buffer *vb)
283 struct dt3155_priv *pd = vb2_get_drv_priv(vb->vb2_queue);
285 /* pd->q->streaming = 1 when dt3155_buf_queue() is invoked */
286 spin_lock_irq(&pd->lock);
288 list_add_tail(&vb->done_entry, &pd->dmaq);
291 dt3155_start_acq(pd);
293 spin_unlock_irq(&pd->lock);
296 * end driver-specific callbacks
299 static const struct vb2_ops q_ops = {
300 .queue_setup = dt3155_queue_setup,
301 .wait_prepare = dt3155_wait_prepare,
302 .wait_finish = dt3155_wait_finish,
303 .buf_prepare = dt3155_buf_prepare,
304 .stop_streaming = dt3155_stop_streaming,
305 .buf_queue = dt3155_buf_queue,
309 dt3155_irq_handler_even(int irq, void *dev_id)
311 struct dt3155_priv *ipd = dev_id;
312 struct vb2_buffer *ivb;
316 tmp = ioread32(ipd->regs + INT_CSR) & (FLD_START | FLD_END_ODD);
318 return IRQ_NONE; /* not our irq */
319 if ((tmp & FLD_START) && !(tmp & FLD_END_ODD)) {
320 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START,
321 ipd->regs + INT_CSR);
323 return IRQ_HANDLED; /* start of field irq */
325 if ((tmp & FLD_START) && (tmp & FLD_END_ODD))
326 ipd->stats.start_before_end++;
327 /* check for corrupted fields */
328 /* write_i2c_reg(ipd->regs, EVEN_CSR, CSR_ERROR | CSR_DONE); */
329 /* write_i2c_reg(ipd->regs, ODD_CSR, CSR_ERROR | CSR_DONE); */
330 tmp = ioread32(ipd->regs + CSR1) & (FLD_CRPT_EVEN | FLD_CRPT_ODD);
332 ipd->stats.corrupted_fields++;
333 iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
334 FLD_DN_ODD | FLD_DN_EVEN |
335 CAP_CONT_EVEN | CAP_CONT_ODD,
340 spin_lock(&ipd->lock);
342 v4l2_get_timestamp(&ipd->curr_buf->v4l2_buf.timestamp);
343 ipd->curr_buf->v4l2_buf.sequence = (ipd->field_count) >> 1;
344 vb2_buffer_done(ipd->curr_buf, VB2_BUF_STATE_DONE);
347 if (!ipd->q->streaming || list_empty(&ipd->dmaq))
349 ivb = list_first_entry(&ipd->dmaq, typeof(*ivb), done_entry);
350 list_del(&ivb->done_entry);
352 dma_addr = vb2_dma_contig_plane_dma_addr(ivb, 0);
353 iowrite32(dma_addr, ipd->regs + EVEN_DMA_START);
354 iowrite32(dma_addr + img_width, ipd->regs + ODD_DMA_START);
355 iowrite32(img_width, ipd->regs + EVEN_DMA_STRIDE);
356 iowrite32(img_width, ipd->regs + ODD_DMA_STRIDE);
358 /* enable interrupts, clear all irq flags */
359 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START |
360 FLD_END_EVEN | FLD_END_ODD, ipd->regs + INT_CSR);
361 spin_unlock(&ipd->lock);
365 ipd->curr_buf = NULL;
367 write_i2c_reg_nowait(ipd->regs, CSR2, ipd->csr2);
368 iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
369 FLD_DN_ODD | FLD_DN_EVEN, ipd->regs + CSR1);
370 /* disable interrupts, clear all irq flags */
371 iowrite32(FLD_START | FLD_END_EVEN | FLD_END_ODD, ipd->regs + INT_CSR);
372 spin_unlock(&ipd->lock);
377 dt3155_open(struct file *filp)
380 struct dt3155_priv *pd = video_drvdata(filp);
382 if (mutex_lock_interruptible(&pd->mux))
385 pd->q = kzalloc(sizeof(*pd->q), GFP_KERNEL);
388 goto err_alloc_queue;
390 pd->q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
391 pd->q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
392 pd->q->io_modes = VB2_READ | VB2_MMAP;
394 pd->q->mem_ops = &vb2_dma_contig_memops;
395 pd->q->drv_priv = pd;
398 ret = vb2_queue_init(pd->q);
400 goto err_request_irq;
401 INIT_LIST_HEAD(&pd->dmaq);
402 spin_lock_init(&pd->lock);
403 /* disable all irqs, clear all irq flags */
404 iowrite32(FLD_START | FLD_END_EVEN | FLD_END_ODD,
406 ret = request_irq(pd->pdev->irq, dt3155_irq_handler_even,
407 IRQF_SHARED, DT3155_NAME, pd);
409 goto err_request_irq;
412 mutex_unlock(&pd->mux);
413 return 0; /* success */
418 mutex_unlock(&pd->mux);
423 dt3155_release(struct file *filp)
425 struct dt3155_priv *pd = video_drvdata(filp);
427 mutex_lock(&pd->mux);
429 BUG_ON(pd->users < 0);
431 vb2_queue_release(pd->q);
432 free_irq(pd->pdev->irq, pd);
433 if (pd->q->alloc_ctx[0])
434 vb2_dma_contig_cleanup_ctx(pd->q->alloc_ctx[0]);
438 mutex_unlock(&pd->mux);
443 dt3155_read(struct file *filp, char __user *user, size_t size, loff_t *loff)
445 struct dt3155_priv *pd = video_drvdata(filp);
448 if (mutex_lock_interruptible(&pd->mux))
450 res = vb2_read(pd->q, user, size, loff, filp->f_flags & O_NONBLOCK);
451 mutex_unlock(&pd->mux);
456 dt3155_poll(struct file *filp, struct poll_table_struct *polltbl)
458 struct dt3155_priv *pd = video_drvdata(filp);
461 mutex_lock(&pd->mux);
462 res = vb2_poll(pd->q, filp, polltbl);
463 mutex_unlock(&pd->mux);
468 dt3155_mmap(struct file *filp, struct vm_area_struct *vma)
470 struct dt3155_priv *pd = video_drvdata(filp);
473 if (mutex_lock_interruptible(&pd->mux))
475 res = vb2_mmap(pd->q, vma);
476 mutex_unlock(&pd->mux);
480 static const struct v4l2_file_operations dt3155_fops = {
481 .owner = THIS_MODULE,
483 .release = dt3155_release,
486 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
491 dt3155_ioc_streamon(struct file *filp, void *p, enum v4l2_buf_type type)
493 struct dt3155_priv *pd = video_drvdata(filp);
495 return vb2_streamon(pd->q, type);
499 dt3155_ioc_streamoff(struct file *filp, void *p, enum v4l2_buf_type type)
501 struct dt3155_priv *pd = video_drvdata(filp);
503 return vb2_streamoff(pd->q, type);
507 dt3155_ioc_querycap(struct file *filp, void *p, struct v4l2_capability *cap)
509 struct dt3155_priv *pd = video_drvdata(filp);
511 strcpy(cap->driver, DT3155_NAME);
512 strcpy(cap->card, DT3155_NAME " frame grabber");
513 sprintf(cap->bus_info, "PCI:%s", pci_name(pd->pdev));
514 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
515 DT3155_CAPTURE_METHOD;
516 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
521 dt3155_ioc_enum_fmt_vid_cap(struct file *filp, void *p, struct v4l2_fmtdesc *f)
523 if (f->index >= NUM_OF_FORMATS)
525 *f = frame_std[f->index];
530 dt3155_ioc_g_fmt_vid_cap(struct file *filp, void *p, struct v4l2_format *f)
532 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
534 f->fmt.pix.width = img_width;
535 f->fmt.pix.height = img_height;
536 f->fmt.pix.pixelformat = V4L2_PIX_FMT_GREY;
537 f->fmt.pix.field = V4L2_FIELD_NONE;
538 f->fmt.pix.bytesperline = f->fmt.pix.width;
539 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height;
540 f->fmt.pix.colorspace = 0;
546 dt3155_ioc_try_fmt_vid_cap(struct file *filp, void *p, struct v4l2_format *f)
548 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
550 if (f->fmt.pix.width == img_width &&
551 f->fmt.pix.height == img_height &&
552 f->fmt.pix.pixelformat == V4L2_PIX_FMT_GREY &&
553 f->fmt.pix.field == V4L2_FIELD_NONE &&
554 f->fmt.pix.bytesperline == f->fmt.pix.width &&
555 f->fmt.pix.sizeimage == f->fmt.pix.width * f->fmt.pix.height)
562 dt3155_ioc_s_fmt_vid_cap(struct file *filp, void *p, struct v4l2_format *f)
564 return dt3155_ioc_g_fmt_vid_cap(filp, p, f);
568 dt3155_ioc_reqbufs(struct file *filp, void *p, struct v4l2_requestbuffers *b)
570 struct dt3155_priv *pd = video_drvdata(filp);
572 return vb2_reqbufs(pd->q, b);
576 dt3155_ioc_querybuf(struct file *filp, void *p, struct v4l2_buffer *b)
578 struct dt3155_priv *pd = video_drvdata(filp);
580 return vb2_querybuf(pd->q, b);
584 dt3155_ioc_qbuf(struct file *filp, void *p, struct v4l2_buffer *b)
586 struct dt3155_priv *pd = video_drvdata(filp);
588 return vb2_qbuf(pd->q, b);
592 dt3155_ioc_dqbuf(struct file *filp, void *p, struct v4l2_buffer *b)
594 struct dt3155_priv *pd = video_drvdata(filp);
596 return vb2_dqbuf(pd->q, b, filp->f_flags & O_NONBLOCK);
600 dt3155_ioc_querystd(struct file *filp, void *p, v4l2_std_id *norm)
602 *norm = DT3155_CURRENT_NORM;
607 dt3155_ioc_g_std(struct file *filp, void *p, v4l2_std_id *norm)
609 *norm = DT3155_CURRENT_NORM;
614 dt3155_ioc_s_std(struct file *filp, void *p, v4l2_std_id norm)
616 if (norm & DT3155_CURRENT_NORM)
622 dt3155_ioc_enum_input(struct file *filp, void *p, struct v4l2_input *input)
626 strcpy(input->name, "Coax in");
627 input->type = V4L2_INPUT_TYPE_CAMERA;
629 * FIXME: input->std = 0 according to v4l2 API
630 * VIDIOC_G_STD, VIDIOC_S_STD, VIDIOC_QUERYSTD and VIDIOC_ENUMSTD
631 * should return -EINVAL
633 input->std = DT3155_CURRENT_NORM;
634 input->status = 0;/* FIXME: add sync detection & V4L2_IN_ST_NO_H_LOCK */
639 dt3155_ioc_g_input(struct file *filp, void *p, unsigned int *i)
646 dt3155_ioc_s_input(struct file *filp, void *p, unsigned int i)
654 dt3155_ioc_g_parm(struct file *filp, void *p, struct v4l2_streamparm *parms)
656 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
658 parms->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
659 parms->parm.capture.capturemode = 0;
660 parms->parm.capture.timeperframe.numerator = 1001;
661 parms->parm.capture.timeperframe.denominator = frames_per_sec * 1000;
662 parms->parm.capture.extendedmode = 0;
663 parms->parm.capture.readbuffers = 1; /* FIXME: 2 buffers? */
668 dt3155_ioc_s_parm(struct file *filp, void *p, struct v4l2_streamparm *parms)
670 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
672 parms->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
673 parms->parm.capture.capturemode = 0;
674 parms->parm.capture.timeperframe.numerator = 1001;
675 parms->parm.capture.timeperframe.denominator = frames_per_sec * 1000;
676 parms->parm.capture.extendedmode = 0;
677 parms->parm.capture.readbuffers = 1; /* FIXME: 2 buffers? */
681 static const struct v4l2_ioctl_ops dt3155_ioctl_ops = {
682 .vidioc_streamon = dt3155_ioc_streamon,
683 .vidioc_streamoff = dt3155_ioc_streamoff,
684 .vidioc_querycap = dt3155_ioc_querycap,
686 .vidioc_g_priority = dt3155_ioc_g_priority,
687 .vidioc_s_priority = dt3155_ioc_s_priority,
689 .vidioc_enum_fmt_vid_cap = dt3155_ioc_enum_fmt_vid_cap,
690 .vidioc_try_fmt_vid_cap = dt3155_ioc_try_fmt_vid_cap,
691 .vidioc_g_fmt_vid_cap = dt3155_ioc_g_fmt_vid_cap,
692 .vidioc_s_fmt_vid_cap = dt3155_ioc_s_fmt_vid_cap,
693 .vidioc_reqbufs = dt3155_ioc_reqbufs,
694 .vidioc_querybuf = dt3155_ioc_querybuf,
695 .vidioc_qbuf = dt3155_ioc_qbuf,
696 .vidioc_dqbuf = dt3155_ioc_dqbuf,
697 .vidioc_querystd = dt3155_ioc_querystd,
698 .vidioc_g_std = dt3155_ioc_g_std,
699 .vidioc_s_std = dt3155_ioc_s_std,
700 .vidioc_enum_input = dt3155_ioc_enum_input,
701 .vidioc_g_input = dt3155_ioc_g_input,
702 .vidioc_s_input = dt3155_ioc_s_input,
704 .vidioc_queryctrl = dt3155_ioc_queryctrl,
705 .vidioc_g_ctrl = dt3155_ioc_g_ctrl,
706 .vidioc_s_ctrl = dt3155_ioc_s_ctrl,
707 .vidioc_querymenu = dt3155_ioc_querymenu,
708 .vidioc_g_ext_ctrls = dt3155_ioc_g_ext_ctrls,
709 .vidioc_s_ext_ctrls = dt3155_ioc_s_ext_ctrls,
711 .vidioc_g_parm = dt3155_ioc_g_parm,
712 .vidioc_s_parm = dt3155_ioc_s_parm,
714 .vidioc_cropcap = dt3155_ioc_cropcap,
715 .vidioc_g_crop = dt3155_ioc_g_crop,
716 .vidioc_s_crop = dt3155_ioc_s_crop,
717 .vidioc_enum_framesizes = dt3155_ioc_enum_framesizes,
718 .vidioc_enum_frameintervals = dt3155_ioc_enum_frameintervals,
723 dt3155_init_board(struct pci_dev *pdev)
725 struct dt3155_priv *pd = pci_get_drvdata(pdev);
731 pci_set_master(pdev); /* dt3155 needs it */
733 /* resetting the adapter */
734 iowrite32(FLD_CRPT_ODD | FLD_CRPT_EVEN | FLD_DN_ODD | FLD_DN_EVEN,
739 /* initializing adaper registers */
740 iowrite32(FIFO_EN | SRST, pd->regs + CSR1);
742 iowrite32(0xEEEEEE01, pd->regs + EVEN_PIXEL_FMT);
743 iowrite32(0xEEEEEE01, pd->regs + ODD_PIXEL_FMT);
744 iowrite32(0x00000020, pd->regs + FIFO_TRIGER);
745 iowrite32(0x00000103, pd->regs + XFER_MODE);
746 iowrite32(0, pd->regs + RETRY_WAIT_CNT);
747 iowrite32(0, pd->regs + INT_CSR);
748 iowrite32(1, pd->regs + EVEN_FLD_MASK);
749 iowrite32(1, pd->regs + ODD_FLD_MASK);
750 iowrite32(0, pd->regs + MASK_LENGTH);
751 iowrite32(0x0005007C, pd->regs + FIFO_FLAG_CNT);
752 iowrite32(0x01010101, pd->regs + IIC_CLK_DUR);
755 /* verifying that we have a DT3155 board (not just a SAA7116 chip) */
756 read_i2c_reg(pd->regs, DT_ID, &tmp);
757 if (tmp != DT3155_ID)
760 /* initialize AD LUT */
761 write_i2c_reg(pd->regs, AD_ADDR, 0);
762 for (i = 0; i < 256; i++)
763 write_i2c_reg(pd->regs, AD_LUT, i);
765 /* initialize ADC references */
766 /* FIXME: pos_ref & neg_ref depend on VT_50HZ */
767 write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
768 write_i2c_reg(pd->regs, AD_CMD, VIDEO_CNL_1 | SYNC_CNL_1 | SYNC_LVL_3);
769 write_i2c_reg(pd->regs, AD_ADDR, AD_POS_REF);
770 write_i2c_reg(pd->regs, AD_CMD, 34);
771 write_i2c_reg(pd->regs, AD_ADDR, AD_NEG_REF);
772 write_i2c_reg(pd->regs, AD_CMD, 0);
774 /* initialize PM LUT */
775 write_i2c_reg(pd->regs, CONFIG, pd->config | PM_LUT_PGM);
776 for (i = 0; i < 256; i++) {
777 write_i2c_reg(pd->regs, PM_LUT_ADDR, i);
778 write_i2c_reg(pd->regs, PM_LUT_DATA, i);
780 write_i2c_reg(pd->regs, CONFIG, pd->config | PM_LUT_PGM | PM_LUT_SEL);
781 for (i = 0; i < 256; i++) {
782 write_i2c_reg(pd->regs, PM_LUT_ADDR, i);
783 write_i2c_reg(pd->regs, PM_LUT_DATA, i);
785 write_i2c_reg(pd->regs, CONFIG, pd->config); /* ACQ_MODE_EVEN */
787 /* select channel 1 for input and set sync level */
788 write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
789 write_i2c_reg(pd->regs, AD_CMD, VIDEO_CNL_1 | SYNC_CNL_1 | SYNC_LVL_3);
791 /* allocate memory, and initialize the DMA machine */
792 buf_cpu = dma_alloc_coherent(&pdev->dev, DT3155_BUF_SIZE, &buf_dma,
796 iowrite32(buf_dma, pd->regs + EVEN_DMA_START);
797 iowrite32(buf_dma, pd->regs + ODD_DMA_START);
798 iowrite32(0, pd->regs + EVEN_DMA_STRIDE);
799 iowrite32(0, pd->regs + ODD_DMA_STRIDE);
801 /* Perform a pseudo even field acquire */
802 iowrite32(FIFO_EN | SRST | CAP_CONT_ODD, pd->regs + CSR1);
803 write_i2c_reg(pd->regs, CSR2, pd->csr2 | SYNC_SNTL);
804 write_i2c_reg(pd->regs, CONFIG, pd->config);
805 write_i2c_reg(pd->regs, EVEN_CSR, CSR_SNGL);
806 write_i2c_reg(pd->regs, CSR2, pd->csr2 | BUSY_EVEN | SYNC_SNTL);
808 read_i2c_reg(pd->regs, CSR2, &tmp);
809 write_i2c_reg(pd->regs, EVEN_CSR, CSR_ERROR | CSR_SNGL | CSR_DONE);
810 write_i2c_reg(pd->regs, ODD_CSR, CSR_ERROR | CSR_SNGL | CSR_DONE);
811 write_i2c_reg(pd->regs, CSR2, pd->csr2);
812 iowrite32(FIFO_EN | SRST | FLD_DN_EVEN | FLD_DN_ODD, pd->regs + CSR1);
814 /* deallocate memory */
815 dma_free_coherent(&pdev->dev, DT3155_BUF_SIZE, buf_cpu, buf_dma);
821 static struct video_device dt3155_vdev = {
823 .fops = &dt3155_fops,
824 .ioctl_ops = &dt3155_ioctl_ops,
826 .release = video_device_release_empty,
827 .tvnorms = DT3155_CURRENT_NORM,
830 /* same as in drivers/base/dma-coherent.c */
831 struct dma_coherent_mem {
833 dma_addr_t device_base;
836 unsigned long *bitmap;
840 dt3155_alloc_coherent(struct device *dev, size_t size, int flags)
842 struct dma_coherent_mem *mem;
844 int pages = size >> PAGE_SHIFT;
845 int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
847 if ((flags & DMA_MEMORY_MAP) == 0)
854 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
857 mem->virt_base = dma_alloc_coherent(dev, size, &dev_base,
860 goto err_alloc_coherent;
861 mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
865 /* coherent_dma_mask is already set to 32 bits */
866 mem->device_base = dev_base;
870 return DMA_MEMORY_MAP;
873 dma_free_coherent(dev, size, mem->virt_base, dev_base);
881 dt3155_free_coherent(struct device *dev)
883 struct dma_coherent_mem *mem = dev->dma_mem;
888 dma_free_coherent(dev, mem->size << PAGE_SHIFT,
889 mem->virt_base, mem->device_base);
895 dt3155_probe(struct pci_dev *pdev, const struct pci_device_id *id)
898 struct dt3155_priv *pd;
900 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
903 pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL);
907 pd->vdev = dt3155_vdev;
908 pci_set_drvdata(pdev, pd); /* for use in dt3155_remove() */
909 video_set_drvdata(&pd->vdev, pd); /* for use in video_fops */
912 INIT_LIST_HEAD(&pd->dmaq);
913 mutex_init(&pd->mux);
914 pd->vdev.lock = &pd->mux; /* for locking v4l2_file_operations */
915 spin_lock_init(&pd->lock);
916 pd->csr2 = csr2_init;
917 pd->config = config_init;
918 err = pci_enable_device(pdev);
921 err = pci_request_region(pdev, 0, pci_name(pdev));
924 pd->regs = pci_iomap(pdev, 0, pci_resource_len(pd->pdev, 0));
929 err = dt3155_init_board(pdev);
932 err = video_register_device(&pd->vdev, VFL_TYPE_GRABBER, -1);
935 if (dt3155_alloc_coherent(&pdev->dev, DT3155_CHUNK_SIZE,
937 dev_info(&pdev->dev, "preallocated 8 buffers\n");
938 dev_info(&pdev->dev, "/dev/video%i is ready\n", pd->vdev.minor);
939 return 0; /* success */
942 pci_iounmap(pdev, pd->regs);
944 pci_release_region(pdev, 0);
946 pci_disable_device(pdev);
951 dt3155_remove(struct pci_dev *pdev)
953 struct dt3155_priv *pd = pci_get_drvdata(pdev);
955 dt3155_free_coherent(&pdev->dev);
956 video_unregister_device(&pd->vdev);
957 pci_iounmap(pdev, pd->regs);
958 pci_release_region(pdev, 0);
959 pci_disable_device(pdev);
962 static const struct pci_device_id pci_ids[] = {
963 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, DT3155_DEVICE_ID) },
964 { 0, /* zero marks the end */ },
966 MODULE_DEVICE_TABLE(pci, pci_ids);
968 static struct pci_driver pci_driver = {
971 .probe = dt3155_probe,
972 .remove = dt3155_remove,
975 module_pci_driver(pci_driver);
977 MODULE_DESCRIPTION("video4linux pci-driver for dt3155 frame grabber");
978 MODULE_AUTHOR("Marin Mitov <mitov@issp.bas.bg>");
979 MODULE_VERSION(DT3155_VERSION);
980 MODULE_LICENSE("GPL");