blob: 8df6345189272eef437eefeff52600fe322db7cc [file] [log] [blame]
Marin Mitov717f4a52010-05-07 11:00:35 +03001/***************************************************************************
2 * Copyright (C) 2006-2010 by Marin Mitov *
3 * mitov@issp.bas.bg *
4 * *
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. *
9 * *
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. *
14 * *
Marin Mitov717f4a52010-05-07 11:00:35 +030015 ***************************************************************************/
16
Paul Gortmaker99c97852011-07-03 15:49:50 -040017#include <linux/module.h>
Marin Mitovd42bffb2010-04-30 18:36:09 +030018#include <linux/stringify.h>
Marin Mitov7ec21182010-05-05 20:31:38 +030019#include <linux/delay.h>
Marin Mitovd42bffb2010-04-30 18:36:09 +030020#include <linux/kthread.h>
Andrew Mortondac95cb2011-07-28 13:59:36 -070021#include <linux/slab.h>
Marin Mitova57941c2010-05-18 13:05:29 +030022#include <media/v4l2-dev.h>
23#include <media/v4l2-ioctl.h>
Hans Verkuil0dcb9532013-04-10 08:07:07 -030024#include <media/v4l2-common.h>
Marin Mitov8ded3512011-05-28 21:45:27 +030025#include <media/videobuf2-dma-contig.h>
Marin Mitovd42bffb2010-04-30 18:36:09 +030026
Hans Verkuilcc11b142015-04-25 12:36:18 -030027#include "dt3155.h"
Marin Mitovd42bffb2010-04-30 18:36:09 +030028
Marin Mitovd42bffb2010-04-30 18:36:09 +030029#define DT3155_DEVICE_ID 0x1223
30
Marin Mitovd42bffb2010-04-30 18:36:09 +030031/**
32 * read_i2c_reg - reads an internal i2c register
33 *
34 * @addr: dt3155 mmio base address
35 * @index: index (internal address) of register to read
36 * @data: pointer to byte the read data will be placed in
37 *
38 * returns: zero on success or error code
39 *
40 * This function starts reading the specified (by index) register
41 * and busy waits for the process to finish. The result is placed
42 * in a byte pointed by data.
43 */
Hans Verkuil6a110872015-04-25 11:19:50 -030044static int read_i2c_reg(void __iomem *addr, u8 index, u8 *data)
Marin Mitovd42bffb2010-04-30 18:36:09 +030045{
46 u32 tmp = index;
47
Hans Verkuil6a110872015-04-25 11:19:50 -030048 iowrite32((tmp << 17) | IIC_READ, addr + IIC_CSR2);
Marin Mitovd42bffb2010-04-30 18:36:09 +030049 mmiowb();
50 udelay(45); /* wait at least 43 usec for NEW_CYCLE to clear */
H Hartley Sweetenc94a2e42011-09-07 10:20:48 -070051 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
52 return -EIO; /* error: NEW_CYCLE not cleared */
Marin Mitovd42bffb2010-04-30 18:36:09 +030053 tmp = ioread32(addr + IIC_CSR1);
54 if (tmp & DIRECT_ABORT) {
Marin Mitovd42bffb2010-04-30 18:36:09 +030055 /* reset DIRECT_ABORT bit */
56 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
H Hartley Sweetenc94a2e42011-09-07 10:20:48 -070057 return -EIO; /* error: DIRECT_ABORT set */
Marin Mitovd42bffb2010-04-30 18:36:09 +030058 }
Hans Verkuil6a110872015-04-25 11:19:50 -030059 *data = tmp >> 24;
Marin Mitovd42bffb2010-04-30 18:36:09 +030060 return 0;
61}
62
63/**
64 * write_i2c_reg - writes to an internal i2c register
65 *
66 * @addr: dt3155 mmio base address
67 * @index: index (internal address) of register to read
68 * @data: data to be written
69 *
70 * returns: zero on success or error code
71 *
Hans Verkuil6a110872015-04-25 11:19:50 -030072 * This function starts writing the specified (by index) register
Marin Mitovd42bffb2010-04-30 18:36:09 +030073 * and busy waits for the process to finish.
74 */
Hans Verkuil6a110872015-04-25 11:19:50 -030075static int write_i2c_reg(void __iomem *addr, u8 index, u8 data)
Marin Mitovd42bffb2010-04-30 18:36:09 +030076{
77 u32 tmp = index;
78
Hans Verkuil6a110872015-04-25 11:19:50 -030079 iowrite32((tmp << 17) | IIC_WRITE | data, addr + IIC_CSR2);
Marin Mitovd42bffb2010-04-30 18:36:09 +030080 mmiowb();
81 udelay(65); /* wait at least 63 usec for NEW_CYCLE to clear */
H Hartley Sweetenc94a2e42011-09-07 10:20:48 -070082 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
83 return -EIO; /* error: NEW_CYCLE not cleared */
Marin Mitovd42bffb2010-04-30 18:36:09 +030084 if (ioread32(addr + IIC_CSR1) & DIRECT_ABORT) {
Marin Mitovd42bffb2010-04-30 18:36:09 +030085 /* reset DIRECT_ABORT bit */
86 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
H Hartley Sweetenc94a2e42011-09-07 10:20:48 -070087 return -EIO; /* error: DIRECT_ABORT set */
Marin Mitovd42bffb2010-04-30 18:36:09 +030088 }
89 return 0;
90}
91
92/**
93 * write_i2c_reg_nowait - writes to an internal i2c register
94 *
95 * @addr: dt3155 mmio base address
96 * @index: index (internal address) of register to read
97 * @data: data to be written
98 *
Hans Verkuil6a110872015-04-25 11:19:50 -030099 * This function starts writing the specified (by index) register
Marin Mitovd42bffb2010-04-30 18:36:09 +0300100 * and then returns.
101 */
Greg Kroah-Hartman2342df02010-05-05 10:45:16 -0700102static void write_i2c_reg_nowait(void __iomem *addr, u8 index, u8 data)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300103{
104 u32 tmp = index;
105
Hans Verkuil6a110872015-04-25 11:19:50 -0300106 iowrite32((tmp << 17) | IIC_WRITE | data, addr + IIC_CSR2);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300107 mmiowb();
108}
109
110/**
111 * wait_i2c_reg - waits the read/write to finish
112 *
113 * @addr: dt3155 mmio base address
114 *
115 * returns: zero on success or error code
116 *
Hans Verkuil6a110872015-04-25 11:19:50 -0300117 * This function waits reading/writing to finish.
Marin Mitovd42bffb2010-04-30 18:36:09 +0300118 */
Greg Kroah-Hartman2342df02010-05-05 10:45:16 -0700119static int wait_i2c_reg(void __iomem *addr)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300120{
121 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
122 udelay(65); /* wait at least 63 usec for NEW_CYCLE to clear */
H Hartley Sweetenc94a2e42011-09-07 10:20:48 -0700123 if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
124 return -EIO; /* error: NEW_CYCLE not cleared */
Marin Mitovd42bffb2010-04-30 18:36:09 +0300125 if (ioread32(addr + IIC_CSR1) & DIRECT_ABORT) {
Marin Mitovd42bffb2010-04-30 18:36:09 +0300126 /* reset DIRECT_ABORT bit */
127 iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
H Hartley Sweetenc94a2e42011-09-07 10:20:48 -0700128 return -EIO; /* error: DIRECT_ABORT set */
Marin Mitovd42bffb2010-04-30 18:36:09 +0300129 }
130 return 0;
131}
132
Marin Mitovd42bffb2010-04-30 18:36:09 +0300133static int
Hans Verkuil9556be12015-04-25 11:51:36 -0300134dt3155_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
135 unsigned int *nbuffers, unsigned int *num_planes,
Dan Carpenter527f18b2011-12-22 02:29:07 -0300136 unsigned int sizes[], void *alloc_ctxs[])
137
Marin Mitovd42bffb2010-04-30 18:36:09 +0300138{
Hans Verkuil9556be12015-04-25 11:51:36 -0300139 struct dt3155_priv *pd = vb2_get_drv_priv(vq);
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300140 unsigned size = pd->width * pd->height;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300141
Hans Verkuil9556be12015-04-25 11:51:36 -0300142 if (vq->num_buffers + *nbuffers < 2)
143 *nbuffers = 2 - vq->num_buffers;
144 if (fmt && fmt->fmt.pix.sizeimage < size)
145 return -EINVAL;
Marin Mitov8ded3512011-05-28 21:45:27 +0300146 *num_planes = 1;
Hans Verkuil9556be12015-04-25 11:51:36 -0300147 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : size;
148 alloc_ctxs[0] = pd->alloc_ctx;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300149 return 0;
150}
151
Hans Verkuil6a110872015-04-25 11:19:50 -0300152static int dt3155_buf_prepare(struct vb2_buffer *vb)
Marin Mitov8ded3512011-05-28 21:45:27 +0300153{
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300154 struct dt3155_priv *pd = vb2_get_drv_priv(vb->vb2_queue);
155
156 vb2_set_plane_payload(vb, 0, pd->width * pd->height);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300157 return 0;
158}
159
Hans Verkuil9db8baf2015-04-25 12:01:55 -0300160static int dt3155_start_streaming(struct vb2_queue *q, unsigned count)
161{
162 struct dt3155_priv *pd = vb2_get_drv_priv(q);
163 struct vb2_buffer *vb = pd->curr_buf;
164 dma_addr_t dma_addr;
165
166 pd->sequence = 0;
167 dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
168 iowrite32(dma_addr, pd->regs + EVEN_DMA_START);
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300169 iowrite32(dma_addr + pd->width, pd->regs + ODD_DMA_START);
170 iowrite32(pd->width, pd->regs + EVEN_DMA_STRIDE);
171 iowrite32(pd->width, pd->regs + ODD_DMA_STRIDE);
Hans Verkuil9db8baf2015-04-25 12:01:55 -0300172 /* enable interrupts, clear all irq flags */
173 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START |
174 FLD_END_EVEN | FLD_END_ODD, pd->regs + INT_CSR);
175 iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
176 FLD_DN_ODD | FLD_DN_EVEN | CAP_CONT_EVEN | CAP_CONT_ODD,
177 pd->regs + CSR1);
178 wait_i2c_reg(pd->regs);
179 write_i2c_reg(pd->regs, CONFIG, pd->config);
180 write_i2c_reg(pd->regs, EVEN_CSR, CSR_ERROR | CSR_DONE);
181 write_i2c_reg(pd->regs, ODD_CSR, CSR_ERROR | CSR_DONE);
182
183 /* start the board */
184 write_i2c_reg(pd->regs, CSR2, pd->csr2 | BUSY_EVEN | BUSY_ODD);
185 return 0;
186}
187
Hans Verkuil6a110872015-04-25 11:19:50 -0300188static void dt3155_stop_streaming(struct vb2_queue *q)
Marin Mitov8ded3512011-05-28 21:45:27 +0300189{
190 struct dt3155_priv *pd = vb2_get_drv_priv(q);
191 struct vb2_buffer *vb;
192
193 spin_lock_irq(&pd->lock);
Hans Verkuil9db8baf2015-04-25 12:01:55 -0300194 /* stop the board */
195 write_i2c_reg_nowait(pd->regs, CSR2, pd->csr2);
196 iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
197 FLD_DN_ODD | FLD_DN_EVEN, pd->regs + CSR1);
198 /* disable interrupts, clear all irq flags */
199 iowrite32(FLD_START | FLD_END_EVEN | FLD_END_ODD, pd->regs + INT_CSR);
200 spin_unlock_irq(&pd->lock);
201
202 /*
203 * It is not clear whether the DMA stops at once or whether it
204 * will finish the current frame or field first. To be on the
205 * safe side we wait a bit.
206 */
207 msleep(45);
208
209 spin_lock_irq(&pd->lock);
210 if (pd->curr_buf) {
211 vb2_buffer_done(pd->curr_buf, VB2_BUF_STATE_ERROR);
212 pd->curr_buf = NULL;
213 }
214
Marin Mitov8ded3512011-05-28 21:45:27 +0300215 while (!list_empty(&pd->dmaq)) {
216 vb = list_first_entry(&pd->dmaq, typeof(*vb), done_entry);
217 list_del(&vb->done_entry);
218 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
219 }
220 spin_unlock_irq(&pd->lock);
Marin Mitov8ded3512011-05-28 21:45:27 +0300221}
222
Hans Verkuil6a110872015-04-25 11:19:50 -0300223static void dt3155_buf_queue(struct vb2_buffer *vb)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300224{
Marin Mitov8ded3512011-05-28 21:45:27 +0300225 struct dt3155_priv *pd = vb2_get_drv_priv(vb->vb2_queue);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300226
Hans Verkuil9db8baf2015-04-25 12:01:55 -0300227 /* pd->vidq.streaming = 1 when dt3155_buf_queue() is invoked */
Marin Mitov8ded3512011-05-28 21:45:27 +0300228 spin_lock_irq(&pd->lock);
229 if (pd->curr_buf)
230 list_add_tail(&vb->done_entry, &pd->dmaq);
Hans Verkuil9db8baf2015-04-25 12:01:55 -0300231 else
Marin Mitov8ded3512011-05-28 21:45:27 +0300232 pd->curr_buf = vb;
Marin Mitov8ded3512011-05-28 21:45:27 +0300233 spin_unlock_irq(&pd->lock);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300234}
235
Kristina Martšenko5ae74372014-03-14 17:20:17 +0200236static const struct vb2_ops q_ops = {
Marin Mitov8ded3512011-05-28 21:45:27 +0300237 .queue_setup = dt3155_queue_setup,
Hans Verkuil9556be12015-04-25 11:51:36 -0300238 .wait_prepare = vb2_ops_wait_prepare,
239 .wait_finish = vb2_ops_wait_finish,
Marin Mitovd42bffb2010-04-30 18:36:09 +0300240 .buf_prepare = dt3155_buf_prepare,
Hans Verkuil9db8baf2015-04-25 12:01:55 -0300241 .start_streaming = dt3155_start_streaming,
Marin Mitov8ded3512011-05-28 21:45:27 +0300242 .stop_streaming = dt3155_stop_streaming,
Marin Mitovd42bffb2010-04-30 18:36:09 +0300243 .buf_queue = dt3155_buf_queue,
Marin Mitovd42bffb2010-04-30 18:36:09 +0300244};
245
Hans Verkuil6a110872015-04-25 11:19:50 -0300246static irqreturn_t dt3155_irq_handler_even(int irq, void *dev_id)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300247{
248 struct dt3155_priv *ipd = dev_id;
Marin Mitov8ded3512011-05-28 21:45:27 +0300249 struct vb2_buffer *ivb;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300250 dma_addr_t dma_addr;
251 u32 tmp;
252
253 tmp = ioread32(ipd->regs + INT_CSR) & (FLD_START | FLD_END_ODD);
254 if (!tmp)
255 return IRQ_NONE; /* not our irq */
256 if ((tmp & FLD_START) && !(tmp & FLD_END_ODD)) {
257 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START,
258 ipd->regs + INT_CSR);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300259 return IRQ_HANDLED; /* start of field irq */
260 }
Marin Mitovd42bffb2010-04-30 18:36:09 +0300261 tmp = ioread32(ipd->regs + CSR1) & (FLD_CRPT_EVEN | FLD_CRPT_ODD);
262 if (tmp) {
Marin Mitovd42bffb2010-04-30 18:36:09 +0300263 iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
264 FLD_DN_ODD | FLD_DN_EVEN |
265 CAP_CONT_EVEN | CAP_CONT_ODD,
266 ipd->regs + CSR1);
267 mmiowb();
268 }
269
270 spin_lock(&ipd->lock);
Hans Verkuil9db8baf2015-04-25 12:01:55 -0300271 if (ipd->curr_buf && !list_empty(&ipd->dmaq)) {
Hans Verkuil0dcb9532013-04-10 08:07:07 -0300272 v4l2_get_timestamp(&ipd->curr_buf->v4l2_buf.timestamp);
Hans Verkuil9db8baf2015-04-25 12:01:55 -0300273 ipd->curr_buf->v4l2_buf.sequence = ipd->sequence++;
274 ipd->curr_buf->v4l2_buf.field = V4L2_FIELD_NONE;
Marin Mitov8ded3512011-05-28 21:45:27 +0300275 vb2_buffer_done(ipd->curr_buf, VB2_BUF_STATE_DONE);
Hans Verkuil9db8baf2015-04-25 12:01:55 -0300276
277 ivb = list_first_entry(&ipd->dmaq, typeof(*ivb), done_entry);
278 list_del(&ivb->done_entry);
279 ipd->curr_buf = ivb;
280 dma_addr = vb2_dma_contig_plane_dma_addr(ivb, 0);
281 iowrite32(dma_addr, ipd->regs + EVEN_DMA_START);
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300282 iowrite32(dma_addr + ipd->width, ipd->regs + ODD_DMA_START);
283 iowrite32(ipd->width, ipd->regs + EVEN_DMA_STRIDE);
284 iowrite32(ipd->width, ipd->regs + ODD_DMA_STRIDE);
Hans Verkuil9db8baf2015-04-25 12:01:55 -0300285 mmiowb();
Marin Mitov8ded3512011-05-28 21:45:27 +0300286 }
287
Marin Mitovd42bffb2010-04-30 18:36:09 +0300288 /* enable interrupts, clear all irq flags */
289 iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START |
290 FLD_END_EVEN | FLD_END_ODD, ipd->regs + INT_CSR);
291 spin_unlock(&ipd->lock);
292 return IRQ_HANDLED;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300293}
294
Marin Mitovd42bffb2010-04-30 18:36:09 +0300295static const struct v4l2_file_operations dt3155_fops = {
296 .owner = THIS_MODULE,
Hans Verkuil9556be12015-04-25 11:51:36 -0300297 .open = v4l2_fh_open,
298 .release = vb2_fop_release,
299 .unlocked_ioctl = video_ioctl2,
300 .read = vb2_fop_read,
301 .mmap = vb2_fop_mmap,
302 .poll = vb2_fop_poll
Marin Mitovd42bffb2010-04-30 18:36:09 +0300303};
304
Mauro Carvalho Chehab90874cd2015-05-01 08:30:06 -0300305static int dt3155_querycap(struct file *filp, void *p,
306 struct v4l2_capability *cap)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300307{
308 struct dt3155_priv *pd = video_drvdata(filp);
309
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));
Hans Verkuil57e774c2014-11-24 06:37:22 -0300313 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
Hans Verkuila6e95142015-04-25 11:54:49 -0300314 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
Hans Verkuil57e774c2014-11-24 06:37:22 -0300315 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300316 return 0;
317}
318
Mauro Carvalho Chehab90874cd2015-05-01 08:30:06 -0300319static int dt3155_enum_fmt_vid_cap(struct file *filp,
320 void *p, struct v4l2_fmtdesc *f)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300321{
Hans Verkuil44a38df2015-04-25 12:16:45 -0300322 if (f->index)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300323 return -EINVAL;
Hans Verkuil44a38df2015-04-25 12:16:45 -0300324 f->pixelformat = V4L2_PIX_FMT_GREY;
325 strcpy(f->description, "8-bit Greyscale");
Marin Mitovd42bffb2010-04-30 18:36:09 +0300326 return 0;
327}
328
Hans Verkuil44a38df2015-04-25 12:16:45 -0300329static int dt3155_fmt_vid_cap(struct file *filp, void *p, struct v4l2_format *f)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300330{
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300331 struct dt3155_priv *pd = video_drvdata(filp);
332
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300333 f->fmt.pix.width = pd->width;
334 f->fmt.pix.height = pd->height;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300335 f->fmt.pix.pixelformat = V4L2_PIX_FMT_GREY;
336 f->fmt.pix.field = V4L2_FIELD_NONE;
337 f->fmt.pix.bytesperline = f->fmt.pix.width;
338 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height;
Hans Verkuil44a38df2015-04-25 12:16:45 -0300339 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300340 return 0;
341}
342
Hans Verkuil6a110872015-04-25 11:19:50 -0300343static int dt3155_g_std(struct file *filp, void *p, v4l2_std_id *norm)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300344{
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300345 struct dt3155_priv *pd = video_drvdata(filp);
346
347 *norm = pd->std;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300348 return 0;
349}
350
Hans Verkuil6a110872015-04-25 11:19:50 -0300351static int dt3155_s_std(struct file *filp, void *p, v4l2_std_id norm)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300352{
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300353 struct dt3155_priv *pd = video_drvdata(filp);
354
355 if (pd->std == norm)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300356 return 0;
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300357 if (vb2_is_busy(&pd->vidq))
358 return -EBUSY;
359 pd->std = norm;
360 if (pd->std & V4L2_STD_525_60) {
361 pd->csr2 = VT_60HZ;
362 pd->width = 640;
363 pd->height = 480;
364 } else {
365 pd->csr2 = VT_50HZ;
366 pd->width = 768;
367 pd->height = 576;
368 }
369 return 0;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300370}
371
Mauro Carvalho Chehab90874cd2015-05-01 08:30:06 -0300372static int dt3155_enum_input(struct file *filp, void *p,
373 struct v4l2_input *input)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300374{
Hans Verkuilc34b7ef2015-04-25 12:19:02 -0300375 if (input->index > 3)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300376 return -EINVAL;
Hans Verkuilc34b7ef2015-04-25 12:19:02 -0300377 if (input->index)
Mauro Carvalho Chehab90874cd2015-05-01 08:30:06 -0300378 snprintf(input->name, sizeof(input->name), "VID%d",
379 input->index);
Hans Verkuilc34b7ef2015-04-25 12:19:02 -0300380 else
381 strlcpy(input->name, "J2/VID0", sizeof(input->name));
Marin Mitovd42bffb2010-04-30 18:36:09 +0300382 input->type = V4L2_INPUT_TYPE_CAMERA;
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300383 input->std = V4L2_STD_ALL;
384 input->status = 0;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300385 return 0;
386}
387
Hans Verkuil6a110872015-04-25 11:19:50 -0300388static int dt3155_g_input(struct file *filp, void *p, unsigned int *i)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300389{
Hans Verkuilc34b7ef2015-04-25 12:19:02 -0300390 struct dt3155_priv *pd = video_drvdata(filp);
391
392 *i = pd->input;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300393 return 0;
394}
395
Hans Verkuil6a110872015-04-25 11:19:50 -0300396static int dt3155_s_input(struct file *filp, void *p, unsigned int i)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300397{
Hans Verkuilc34b7ef2015-04-25 12:19:02 -0300398 struct dt3155_priv *pd = video_drvdata(filp);
399
400 if (i > 3)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300401 return -EINVAL;
Hans Verkuilc34b7ef2015-04-25 12:19:02 -0300402 pd->input = i;
403 write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
404 write_i2c_reg(pd->regs, AD_CMD, (i << 6) | (i << 4) | SYNC_LVL_3);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300405 return 0;
406}
407
Marin Mitovd42bffb2010-04-30 18:36:09 +0300408static const struct v4l2_ioctl_ops dt3155_ioctl_ops = {
Hans Verkuil6a110872015-04-25 11:19:50 -0300409 .vidioc_querycap = dt3155_querycap,
410 .vidioc_enum_fmt_vid_cap = dt3155_enum_fmt_vid_cap,
Hans Verkuil44a38df2015-04-25 12:16:45 -0300411 .vidioc_try_fmt_vid_cap = dt3155_fmt_vid_cap,
412 .vidioc_g_fmt_vid_cap = dt3155_fmt_vid_cap,
413 .vidioc_s_fmt_vid_cap = dt3155_fmt_vid_cap,
Hans Verkuil9556be12015-04-25 11:51:36 -0300414 .vidioc_reqbufs = vb2_ioctl_reqbufs,
415 .vidioc_create_bufs = vb2_ioctl_create_bufs,
416 .vidioc_querybuf = vb2_ioctl_querybuf,
417 .vidioc_expbuf = vb2_ioctl_expbuf,
418 .vidioc_qbuf = vb2_ioctl_qbuf,
419 .vidioc_dqbuf = vb2_ioctl_dqbuf,
420 .vidioc_streamon = vb2_ioctl_streamon,
421 .vidioc_streamoff = vb2_ioctl_streamoff,
Hans Verkuil6a110872015-04-25 11:19:50 -0300422 .vidioc_g_std = dt3155_g_std,
423 .vidioc_s_std = dt3155_s_std,
424 .vidioc_enum_input = dt3155_enum_input,
425 .vidioc_g_input = dt3155_g_input,
426 .vidioc_s_input = dt3155_s_input,
Marin Mitovd42bffb2010-04-30 18:36:09 +0300427};
428
Hans Verkuil168b5092015-04-25 11:32:54 -0300429static int dt3155_init_board(struct dt3155_priv *pd)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300430{
Hans Verkuil168b5092015-04-25 11:32:54 -0300431 struct pci_dev *pdev = pd->pdev;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300432 int i;
Hans Verkuildeb28972015-04-25 11:41:31 -0300433 u8 tmp = 0;
Marin Mitova57941c2010-05-18 13:05:29 +0300434
Marin Mitov8ded3512011-05-28 21:45:27 +0300435 pci_set_master(pdev); /* dt3155 needs it */
Marin Mitovd42bffb2010-04-30 18:36:09 +0300436
437 /* resetting the adapter */
Hans Verkuildeb28972015-04-25 11:41:31 -0300438 iowrite32(ADDR_ERR_ODD | ADDR_ERR_EVEN | FLD_CRPT_ODD | FLD_CRPT_EVEN |
439 FLD_DN_ODD | FLD_DN_EVEN, pd->regs + CSR1);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300440 mmiowb();
Marin Mitov8ded3512011-05-28 21:45:27 +0300441 msleep(20);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300442
Hans Verkuil6a110872015-04-25 11:19:50 -0300443 /* initializing adapter registers */
Marin Mitovd42bffb2010-04-30 18:36:09 +0300444 iowrite32(FIFO_EN | SRST, pd->regs + CSR1);
445 mmiowb();
446 iowrite32(0xEEEEEE01, pd->regs + EVEN_PIXEL_FMT);
447 iowrite32(0xEEEEEE01, pd->regs + ODD_PIXEL_FMT);
448 iowrite32(0x00000020, pd->regs + FIFO_TRIGER);
449 iowrite32(0x00000103, pd->regs + XFER_MODE);
450 iowrite32(0, pd->regs + RETRY_WAIT_CNT);
451 iowrite32(0, pd->regs + INT_CSR);
452 iowrite32(1, pd->regs + EVEN_FLD_MASK);
453 iowrite32(1, pd->regs + ODD_FLD_MASK);
454 iowrite32(0, pd->regs + MASK_LENGTH);
455 iowrite32(0x0005007C, pd->regs + FIFO_FLAG_CNT);
456 iowrite32(0x01010101, pd->regs + IIC_CLK_DUR);
457 mmiowb();
458
459 /* verifying that we have a DT3155 board (not just a SAA7116 chip) */
460 read_i2c_reg(pd->regs, DT_ID, &tmp);
461 if (tmp != DT3155_ID)
462 return -ENODEV;
463
464 /* initialize AD LUT */
465 write_i2c_reg(pd->regs, AD_ADDR, 0);
466 for (i = 0; i < 256; i++)
467 write_i2c_reg(pd->regs, AD_LUT, i);
468
469 /* initialize ADC references */
470 /* FIXME: pos_ref & neg_ref depend on VT_50HZ */
471 write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
472 write_i2c_reg(pd->regs, AD_CMD, VIDEO_CNL_1 | SYNC_CNL_1 | SYNC_LVL_3);
473 write_i2c_reg(pd->regs, AD_ADDR, AD_POS_REF);
474 write_i2c_reg(pd->regs, AD_CMD, 34);
475 write_i2c_reg(pd->regs, AD_ADDR, AD_NEG_REF);
476 write_i2c_reg(pd->regs, AD_CMD, 0);
477
478 /* initialize PM LUT */
479 write_i2c_reg(pd->regs, CONFIG, pd->config | PM_LUT_PGM);
480 for (i = 0; i < 256; i++) {
481 write_i2c_reg(pd->regs, PM_LUT_ADDR, i);
482 write_i2c_reg(pd->regs, PM_LUT_DATA, i);
483 }
484 write_i2c_reg(pd->regs, CONFIG, pd->config | PM_LUT_PGM | PM_LUT_SEL);
485 for (i = 0; i < 256; i++) {
486 write_i2c_reg(pd->regs, PM_LUT_ADDR, i);
487 write_i2c_reg(pd->regs, PM_LUT_DATA, i);
488 }
489 write_i2c_reg(pd->regs, CONFIG, pd->config); /* ACQ_MODE_EVEN */
490
Masanari Iida6dc8f382012-10-31 11:52:45 -0300491 /* select channel 1 for input and set sync level */
Marin Mitovd42bffb2010-04-30 18:36:09 +0300492 write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
493 write_i2c_reg(pd->regs, AD_CMD, VIDEO_CNL_1 | SYNC_CNL_1 | SYNC_LVL_3);
494
Hans Verkuildeb28972015-04-25 11:41:31 -0300495 /* disable all irqs, clear all irq flags */
496 iowrite32(FLD_START | FLD_END_EVEN | FLD_END_ODD,
497 pd->regs + INT_CSR);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300498
Marin Mitovd42bffb2010-04-30 18:36:09 +0300499 return 0;
500}
501
502static struct video_device dt3155_vdev = {
503 .name = DT3155_NAME,
504 .fops = &dt3155_fops,
505 .ioctl_ops = &dt3155_ioctl_ops,
506 .minor = -1,
Hans Verkuilf91fccd2015-03-09 13:33:59 -0300507 .release = video_device_release_empty,
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300508 .tvnorms = V4L2_STD_ALL,
Marin Mitovd42bffb2010-04-30 18:36:09 +0300509};
510
Hans Verkuil6a110872015-04-25 11:19:50 -0300511static int dt3155_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300512{
Marin Mitova57941c2010-05-18 13:05:29 +0300513 int err;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300514 struct dt3155_priv *pd;
515
Russell King68788972013-06-26 23:49:11 +0100516 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
H Hartley Sweetenc94a2e42011-09-07 10:20:48 -0700517 if (err)
Marin Mitova57941c2010-05-18 13:05:29 +0300518 return -ENODEV;
Kiran Padwal92afdc12015-02-13 05:52:10 -0300519 pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL);
H Hartley Sweetenc94a2e42011-09-07 10:20:48 -0700520 if (!pd)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300521 return -ENOMEM;
Kiran Padwal92afdc12015-02-13 05:52:10 -0300522
Hans Verkuil168b5092015-04-25 11:32:54 -0300523 err = v4l2_device_register(&pdev->dev, &pd->v4l2_dev);
524 if (err)
525 return err;
Hans Verkuilf91fccd2015-03-09 13:33:59 -0300526 pd->vdev = dt3155_vdev;
Hans Verkuil168b5092015-04-25 11:32:54 -0300527 pd->vdev.v4l2_dev = &pd->v4l2_dev;
Hans Verkuilf91fccd2015-03-09 13:33:59 -0300528 video_set_drvdata(&pd->vdev, pd); /* for use in video_fops */
Marin Mitov8ded3512011-05-28 21:45:27 +0300529 pd->pdev = pdev;
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300530 pd->std = V4L2_STD_625_50;
531 pd->csr2 = VT_50HZ;
532 pd->width = 768;
533 pd->height = 576;
Marin Mitovd42bffb2010-04-30 18:36:09 +0300534 INIT_LIST_HEAD(&pd->dmaq);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300535 mutex_init(&pd->mux);
Hans Verkuilf91fccd2015-03-09 13:33:59 -0300536 pd->vdev.lock = &pd->mux; /* for locking v4l2_file_operations */
Hans Verkuil9556be12015-04-25 11:51:36 -0300537 pd->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
538 pd->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
539 pd->vidq.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
540 pd->vidq.ops = &q_ops;
541 pd->vidq.mem_ops = &vb2_dma_contig_memops;
542 pd->vidq.drv_priv = pd;
543 pd->vidq.min_buffers_needed = 2;
Hans Verkuil7c89a212015-04-26 06:27:00 -0300544 pd->vidq.gfp_flags = GFP_DMA32;
Hans Verkuil9556be12015-04-25 11:51:36 -0300545 pd->vidq.lock = &pd->mux; /* for locking v4l2_file_operations */
546 pd->vdev.queue = &pd->vidq;
547 err = vb2_queue_init(&pd->vidq);
548 if (err < 0)
549 goto err_v4l2_dev_unreg;
550 pd->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
551 if (IS_ERR(pd->alloc_ctx)) {
552 dev_err(&pdev->dev, "Can't allocate buffer context");
553 err = PTR_ERR(pd->alloc_ctx);
554 goto err_v4l2_dev_unreg;
555 }
Marin Mitov8ded3512011-05-28 21:45:27 +0300556 spin_lock_init(&pd->lock);
Hans Verkuil5c9ede42015-04-25 12:11:50 -0300557 pd->config = ACQ_MODE_EVEN;
Marin Mitov8ded3512011-05-28 21:45:27 +0300558 err = pci_enable_device(pdev);
H Hartley Sweetenc94a2e42011-09-07 10:20:48 -0700559 if (err)
Hans Verkuil9556be12015-04-25 11:51:36 -0300560 goto err_free_ctx;
Marin Mitov8ded3512011-05-28 21:45:27 +0300561 err = pci_request_region(pdev, 0, pci_name(pdev));
Marin Mitovd42bffb2010-04-30 18:36:09 +0300562 if (err)
Hans Verkuil168b5092015-04-25 11:32:54 -0300563 goto err_pci_disable;
Marin Mitov8ded3512011-05-28 21:45:27 +0300564 pd->regs = pci_iomap(pdev, 0, pci_resource_len(pd->pdev, 0));
Dan Carpenteraecf33d2011-12-22 02:29:34 -0300565 if (!pd->regs) {
Marin Mitovd42bffb2010-04-30 18:36:09 +0300566 err = -ENOMEM;
Hans Verkuil168b5092015-04-25 11:32:54 -0300567 goto err_free_reg;
Dan Carpenteraecf33d2011-12-22 02:29:34 -0300568 }
Hans Verkuil168b5092015-04-25 11:32:54 -0300569 err = dt3155_init_board(pd);
Marin Mitova57941c2010-05-18 13:05:29 +0300570 if (err)
Hans Verkuil168b5092015-04-25 11:32:54 -0300571 goto err_iounmap;
572 err = request_irq(pd->pdev->irq, dt3155_irq_handler_even,
573 IRQF_SHARED, DT3155_NAME, pd);
574 if (err)
575 goto err_iounmap;
Hans Verkuilf91fccd2015-03-09 13:33:59 -0300576 err = video_register_device(&pd->vdev, VFL_TYPE_GRABBER, -1);
H Hartley Sweetenc94a2e42011-09-07 10:20:48 -0700577 if (err)
Hans Verkuil168b5092015-04-25 11:32:54 -0300578 goto err_free_irq;
Hans Verkuilf91fccd2015-03-09 13:33:59 -0300579 dev_info(&pdev->dev, "/dev/video%i is ready\n", pd->vdev.minor);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300580 return 0; /* success */
581
Hans Verkuil168b5092015-04-25 11:32:54 -0300582err_free_irq:
583 free_irq(pd->pdev->irq, pd);
584err_iounmap:
Marin Mitov8ded3512011-05-28 21:45:27 +0300585 pci_iounmap(pdev, pd->regs);
Hans Verkuil168b5092015-04-25 11:32:54 -0300586err_free_reg:
Marin Mitov8ded3512011-05-28 21:45:27 +0300587 pci_release_region(pdev, 0);
Hans Verkuil168b5092015-04-25 11:32:54 -0300588err_pci_disable:
Marin Mitov8ded3512011-05-28 21:45:27 +0300589 pci_disable_device(pdev);
Hans Verkuil9556be12015-04-25 11:51:36 -0300590err_free_ctx:
591 vb2_dma_contig_cleanup_ctx(pd->alloc_ctx);
Hans Verkuil168b5092015-04-25 11:32:54 -0300592err_v4l2_dev_unreg:
593 v4l2_device_unregister(&pd->v4l2_dev);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300594 return err;
595}
596
Hans Verkuil6a110872015-04-25 11:19:50 -0300597static void dt3155_remove(struct pci_dev *pdev)
Marin Mitovd42bffb2010-04-30 18:36:09 +0300598{
Hans Verkuil168b5092015-04-25 11:32:54 -0300599 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
Mauro Carvalho Chehab90874cd2015-05-01 08:30:06 -0300600 struct dt3155_priv *pd = container_of(v4l2_dev, struct dt3155_priv,
601 v4l2_dev);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300602
Hans Verkuilf91fccd2015-03-09 13:33:59 -0300603 video_unregister_device(&pd->vdev);
Hans Verkuil168b5092015-04-25 11:32:54 -0300604 free_irq(pd->pdev->irq, pd);
Hans Verkuil9556be12015-04-25 11:51:36 -0300605 vb2_queue_release(&pd->vidq);
Hans Verkuil168b5092015-04-25 11:32:54 -0300606 v4l2_device_unregister(&pd->v4l2_dev);
Marin Mitov8ded3512011-05-28 21:45:27 +0300607 pci_iounmap(pdev, pd->regs);
608 pci_release_region(pdev, 0);
609 pci_disable_device(pdev);
Hans Verkuil9556be12015-04-25 11:51:36 -0300610 vb2_dma_contig_cleanup_ctx(pd->alloc_ctx);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300611}
612
Jingoo Han41e043f2013-12-03 08:26:00 +0900613static const struct pci_device_id pci_ids[] = {
Jon Mason6fb0e402014-03-03 14:00:38 -0300614 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, DT3155_DEVICE_ID) },
Marin Mitovd42bffb2010-04-30 18:36:09 +0300615 { 0, /* zero marks the end */ },
616};
617MODULE_DEVICE_TABLE(pci, pci_ids);
618
619static struct pci_driver pci_driver = {
620 .name = DT3155_NAME,
621 .id_table = pci_ids,
622 .probe = dt3155_probe,
Bill Pemberton79fc8d82012-11-19 13:20:52 -0500623 .remove = dt3155_remove,
Marin Mitovd42bffb2010-04-30 18:36:09 +0300624};
625
Devendra Naga1a3acd32012-07-10 02:43:48 -0300626module_pci_driver(pci_driver);
Marin Mitovd42bffb2010-04-30 18:36:09 +0300627
628MODULE_DESCRIPTION("video4linux pci-driver for dt3155 frame grabber");
629MODULE_AUTHOR("Marin Mitov <mitov@issp.bas.bg>");
630MODULE_VERSION(DT3155_VERSION);
631MODULE_LICENSE("GPL");