Marin Mitov | 717f4a5 | 2010-05-07 11:00:35 +0300 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 Mitov | 717f4a5 | 2010-05-07 11:00:35 +0300 | [diff] [blame] | 15 | ***************************************************************************/ |
| 16 | |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 18 | #include <linux/version.h> |
| 19 | #include <linux/stringify.h> |
Marin Mitov | 7ec2118 | 2010-05-05 20:31:38 +0300 | [diff] [blame] | 20 | #include <linux/delay.h> |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 21 | #include <linux/kthread.h> |
Andrew Morton | dac95cb | 2011-07-28 13:59:36 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
Marin Mitov | a57941c | 2010-05-18 13:05:29 +0300 | [diff] [blame] | 23 | #include <media/v4l2-dev.h> |
| 24 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | 0dcb953 | 2013-04-10 08:07:07 -0300 | [diff] [blame] | 25 | #include <media/v4l2-common.h> |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 26 | #include <media/videobuf2-dma-contig.h> |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 27 | |
Hans Verkuil | cc11b14 | 2015-04-25 12:36:18 -0300 | [diff] [blame] | 28 | #include "dt3155.h" |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 29 | |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 30 | #define DT3155_DEVICE_ID 0x1223 |
| 31 | |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 32 | /** |
| 33 | * read_i2c_reg - reads an internal i2c register |
| 34 | * |
| 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 |
| 38 | * |
| 39 | * returns: zero on success or error code |
| 40 | * |
| 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. |
| 44 | */ |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 45 | static int read_i2c_reg(void __iomem *addr, u8 index, u8 *data) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 46 | { |
| 47 | u32 tmp = index; |
| 48 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 49 | iowrite32((tmp << 17) | IIC_READ, addr + IIC_CSR2); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 50 | mmiowb(); |
| 51 | udelay(45); /* wait at least 43 usec for NEW_CYCLE to clear */ |
H Hartley Sweeten | c94a2e4 | 2011-09-07 10:20:48 -0700 | [diff] [blame] | 52 | if (ioread32(addr + IIC_CSR2) & NEW_CYCLE) |
| 53 | return -EIO; /* error: NEW_CYCLE not cleared */ |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 54 | tmp = ioread32(addr + IIC_CSR1); |
| 55 | if (tmp & DIRECT_ABORT) { |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 56 | /* reset DIRECT_ABORT bit */ |
| 57 | iowrite32(DIRECT_ABORT, addr + IIC_CSR1); |
H Hartley Sweeten | c94a2e4 | 2011-09-07 10:20:48 -0700 | [diff] [blame] | 58 | return -EIO; /* error: DIRECT_ABORT set */ |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 59 | } |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 60 | *data = tmp >> 24; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * write_i2c_reg - writes to an internal i2c register |
| 66 | * |
| 67 | * @addr: dt3155 mmio base address |
| 68 | * @index: index (internal address) of register to read |
| 69 | * @data: data to be written |
| 70 | * |
| 71 | * returns: zero on success or error code |
| 72 | * |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 73 | * This function starts writing the specified (by index) register |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 74 | * and busy waits for the process to finish. |
| 75 | */ |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 76 | static int write_i2c_reg(void __iomem *addr, u8 index, u8 data) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 77 | { |
| 78 | u32 tmp = index; |
| 79 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 80 | iowrite32((tmp << 17) | IIC_WRITE | data, addr + IIC_CSR2); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 81 | mmiowb(); |
| 82 | udelay(65); /* wait at least 63 usec for NEW_CYCLE to clear */ |
H Hartley Sweeten | c94a2e4 | 2011-09-07 10:20:48 -0700 | [diff] [blame] | 83 | if (ioread32(addr + IIC_CSR2) & NEW_CYCLE) |
| 84 | return -EIO; /* error: NEW_CYCLE not cleared */ |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 85 | if (ioread32(addr + IIC_CSR1) & DIRECT_ABORT) { |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 86 | /* reset DIRECT_ABORT bit */ |
| 87 | iowrite32(DIRECT_ABORT, addr + IIC_CSR1); |
H Hartley Sweeten | c94a2e4 | 2011-09-07 10:20:48 -0700 | [diff] [blame] | 88 | return -EIO; /* error: DIRECT_ABORT set */ |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * write_i2c_reg_nowait - writes to an internal i2c register |
| 95 | * |
| 96 | * @addr: dt3155 mmio base address |
| 97 | * @index: index (internal address) of register to read |
| 98 | * @data: data to be written |
| 99 | * |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 100 | * This function starts writing the specified (by index) register |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 101 | * and then returns. |
| 102 | */ |
Greg Kroah-Hartman | 2342df0 | 2010-05-05 10:45:16 -0700 | [diff] [blame] | 103 | static void write_i2c_reg_nowait(void __iomem *addr, u8 index, u8 data) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 104 | { |
| 105 | u32 tmp = index; |
| 106 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 107 | iowrite32((tmp << 17) | IIC_WRITE | data, addr + IIC_CSR2); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 108 | mmiowb(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * wait_i2c_reg - waits the read/write to finish |
| 113 | * |
| 114 | * @addr: dt3155 mmio base address |
| 115 | * |
| 116 | * returns: zero on success or error code |
| 117 | * |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 118 | * This function waits reading/writing to finish. |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 119 | */ |
Greg Kroah-Hartman | 2342df0 | 2010-05-05 10:45:16 -0700 | [diff] [blame] | 120 | static int wait_i2c_reg(void __iomem *addr) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 121 | { |
| 122 | if (ioread32(addr + IIC_CSR2) & NEW_CYCLE) |
| 123 | udelay(65); /* wait at least 63 usec for NEW_CYCLE to clear */ |
H Hartley Sweeten | c94a2e4 | 2011-09-07 10:20:48 -0700 | [diff] [blame] | 124 | if (ioread32(addr + IIC_CSR2) & NEW_CYCLE) |
| 125 | return -EIO; /* error: NEW_CYCLE not cleared */ |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 126 | if (ioread32(addr + IIC_CSR1) & DIRECT_ABORT) { |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 127 | /* reset DIRECT_ABORT bit */ |
| 128 | iowrite32(DIRECT_ABORT, addr + IIC_CSR1); |
H Hartley Sweeten | c94a2e4 | 2011-09-07 10:20:48 -0700 | [diff] [blame] | 129 | return -EIO; /* error: DIRECT_ABORT set */ |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 130 | } |
| 131 | return 0; |
| 132 | } |
| 133 | |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 134 | static int |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 135 | dt3155_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt, |
| 136 | unsigned int *nbuffers, unsigned int *num_planes, |
Dan Carpenter | 527f18b | 2011-12-22 02:29:07 -0300 | [diff] [blame] | 137 | unsigned int sizes[], void *alloc_ctxs[]) |
| 138 | |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 139 | { |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 140 | struct dt3155_priv *pd = vb2_get_drv_priv(vq); |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 141 | unsigned size = pd->width * pd->height; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 142 | |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 143 | if (vq->num_buffers + *nbuffers < 2) |
| 144 | *nbuffers = 2 - vq->num_buffers; |
| 145 | if (fmt && fmt->fmt.pix.sizeimage < size) |
| 146 | return -EINVAL; |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 147 | *num_planes = 1; |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 148 | sizes[0] = fmt ? fmt->fmt.pix.sizeimage : size; |
| 149 | alloc_ctxs[0] = pd->alloc_ctx; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 153 | static int dt3155_buf_prepare(struct vb2_buffer *vb) |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 154 | { |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 155 | struct dt3155_priv *pd = vb2_get_drv_priv(vb->vb2_queue); |
| 156 | |
| 157 | vb2_set_plane_payload(vb, 0, pd->width * pd->height); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
Hans Verkuil | 9db8baf | 2015-04-25 12:01:55 -0300 | [diff] [blame] | 161 | static int dt3155_start_streaming(struct vb2_queue *q, unsigned count) |
| 162 | { |
| 163 | struct dt3155_priv *pd = vb2_get_drv_priv(q); |
| 164 | struct vb2_buffer *vb = pd->curr_buf; |
| 165 | dma_addr_t dma_addr; |
| 166 | |
| 167 | pd->sequence = 0; |
| 168 | dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0); |
| 169 | iowrite32(dma_addr, pd->regs + EVEN_DMA_START); |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 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); |
Hans Verkuil | 9db8baf | 2015-04-25 12:01:55 -0300 | [diff] [blame] | 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, |
| 178 | pd->regs + CSR1); |
| 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); |
| 183 | |
| 184 | /* start the board */ |
| 185 | write_i2c_reg(pd->regs, CSR2, pd->csr2 | BUSY_EVEN | BUSY_ODD); |
| 186 | return 0; |
| 187 | } |
| 188 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 189 | static void dt3155_stop_streaming(struct vb2_queue *q) |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 190 | { |
| 191 | struct dt3155_priv *pd = vb2_get_drv_priv(q); |
| 192 | struct vb2_buffer *vb; |
| 193 | |
| 194 | spin_lock_irq(&pd->lock); |
Hans Verkuil | 9db8baf | 2015-04-25 12:01:55 -0300 | [diff] [blame] | 195 | /* stop the board */ |
| 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); |
| 202 | |
| 203 | /* |
| 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. |
| 207 | */ |
| 208 | msleep(45); |
| 209 | |
| 210 | spin_lock_irq(&pd->lock); |
| 211 | if (pd->curr_buf) { |
| 212 | vb2_buffer_done(pd->curr_buf, VB2_BUF_STATE_ERROR); |
| 213 | pd->curr_buf = NULL; |
| 214 | } |
| 215 | |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 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); |
| 220 | } |
| 221 | spin_unlock_irq(&pd->lock); |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 222 | } |
| 223 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 224 | static void dt3155_buf_queue(struct vb2_buffer *vb) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 225 | { |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 226 | struct dt3155_priv *pd = vb2_get_drv_priv(vb->vb2_queue); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 227 | |
Hans Verkuil | 9db8baf | 2015-04-25 12:01:55 -0300 | [diff] [blame] | 228 | /* pd->vidq.streaming = 1 when dt3155_buf_queue() is invoked */ |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 229 | spin_lock_irq(&pd->lock); |
| 230 | if (pd->curr_buf) |
| 231 | list_add_tail(&vb->done_entry, &pd->dmaq); |
Hans Verkuil | 9db8baf | 2015-04-25 12:01:55 -0300 | [diff] [blame] | 232 | else |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 233 | pd->curr_buf = vb; |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 234 | spin_unlock_irq(&pd->lock); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 235 | } |
| 236 | |
Kristina Martšenko | 5ae7437 | 2014-03-14 17:20:17 +0200 | [diff] [blame] | 237 | static const struct vb2_ops q_ops = { |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 238 | .queue_setup = dt3155_queue_setup, |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 239 | .wait_prepare = vb2_ops_wait_prepare, |
| 240 | .wait_finish = vb2_ops_wait_finish, |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 241 | .buf_prepare = dt3155_buf_prepare, |
Hans Verkuil | 9db8baf | 2015-04-25 12:01:55 -0300 | [diff] [blame] | 242 | .start_streaming = dt3155_start_streaming, |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 243 | .stop_streaming = dt3155_stop_streaming, |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 244 | .buf_queue = dt3155_buf_queue, |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 245 | }; |
| 246 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 247 | static irqreturn_t dt3155_irq_handler_even(int irq, void *dev_id) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 248 | { |
| 249 | struct dt3155_priv *ipd = dev_id; |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 250 | struct vb2_buffer *ivb; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 251 | dma_addr_t dma_addr; |
| 252 | u32 tmp; |
| 253 | |
| 254 | tmp = ioread32(ipd->regs + INT_CSR) & (FLD_START | FLD_END_ODD); |
| 255 | if (!tmp) |
| 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); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 260 | return IRQ_HANDLED; /* start of field irq */ |
| 261 | } |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 262 | tmp = ioread32(ipd->regs + CSR1) & (FLD_CRPT_EVEN | FLD_CRPT_ODD); |
| 263 | if (tmp) { |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 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, |
| 267 | ipd->regs + CSR1); |
| 268 | mmiowb(); |
| 269 | } |
| 270 | |
| 271 | spin_lock(&ipd->lock); |
Hans Verkuil | 9db8baf | 2015-04-25 12:01:55 -0300 | [diff] [blame] | 272 | if (ipd->curr_buf && !list_empty(&ipd->dmaq)) { |
Hans Verkuil | 0dcb953 | 2013-04-10 08:07:07 -0300 | [diff] [blame] | 273 | v4l2_get_timestamp(&ipd->curr_buf->v4l2_buf.timestamp); |
Hans Verkuil | 9db8baf | 2015-04-25 12:01:55 -0300 | [diff] [blame] | 274 | ipd->curr_buf->v4l2_buf.sequence = ipd->sequence++; |
| 275 | ipd->curr_buf->v4l2_buf.field = V4L2_FIELD_NONE; |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 276 | vb2_buffer_done(ipd->curr_buf, VB2_BUF_STATE_DONE); |
Hans Verkuil | 9db8baf | 2015-04-25 12:01:55 -0300 | [diff] [blame] | 277 | |
| 278 | ivb = list_first_entry(&ipd->dmaq, typeof(*ivb), done_entry); |
| 279 | list_del(&ivb->done_entry); |
| 280 | ipd->curr_buf = ivb; |
| 281 | dma_addr = vb2_dma_contig_plane_dma_addr(ivb, 0); |
| 282 | iowrite32(dma_addr, ipd->regs + EVEN_DMA_START); |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 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); |
Hans Verkuil | 9db8baf | 2015-04-25 12:01:55 -0300 | [diff] [blame] | 286 | mmiowb(); |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 287 | } |
| 288 | |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 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); |
| 293 | return IRQ_HANDLED; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 294 | } |
| 295 | |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 296 | static const struct v4l2_file_operations dt3155_fops = { |
| 297 | .owner = THIS_MODULE, |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 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, |
| 303 | .poll = vb2_fop_poll |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 304 | }; |
| 305 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 306 | static int dt3155_querycap(struct file *filp, void *p, struct v4l2_capability *cap) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 307 | { |
| 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 Verkuil | 57e774c | 2014-11-24 06:37:22 -0300 | [diff] [blame] | 313 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | |
Hans Verkuil | a6e9514 | 2015-04-25 11:54:49 -0300 | [diff] [blame] | 314 | V4L2_CAP_STREAMING | V4L2_CAP_READWRITE; |
Hans Verkuil | 57e774c | 2014-11-24 06:37:22 -0300 | [diff] [blame] | 315 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 319 | static int dt3155_enum_fmt_vid_cap(struct file *filp, void *p, struct v4l2_fmtdesc *f) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 320 | { |
Hans Verkuil | 44a38df | 2015-04-25 12:16:45 -0300 | [diff] [blame] | 321 | if (f->index) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 322 | return -EINVAL; |
Hans Verkuil | 44a38df | 2015-04-25 12:16:45 -0300 | [diff] [blame] | 323 | f->pixelformat = V4L2_PIX_FMT_GREY; |
| 324 | strcpy(f->description, "8-bit Greyscale"); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
Hans Verkuil | 44a38df | 2015-04-25 12:16:45 -0300 | [diff] [blame] | 328 | static int dt3155_fmt_vid_cap(struct file *filp, void *p, struct v4l2_format *f) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 329 | { |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 330 | struct dt3155_priv *pd = video_drvdata(filp); |
| 331 | |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 332 | f->fmt.pix.width = pd->width; |
| 333 | f->fmt.pix.height = pd->height; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 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; |
Hans Verkuil | 44a38df | 2015-04-25 12:16:45 -0300 | [diff] [blame] | 338 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 342 | static int dt3155_g_std(struct file *filp, void *p, v4l2_std_id *norm) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 343 | { |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 344 | struct dt3155_priv *pd = video_drvdata(filp); |
| 345 | |
| 346 | *norm = pd->std; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 350 | static int dt3155_s_std(struct file *filp, void *p, v4l2_std_id norm) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 351 | { |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 352 | struct dt3155_priv *pd = video_drvdata(filp); |
| 353 | |
| 354 | if (pd->std == norm) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 355 | return 0; |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 356 | if (vb2_is_busy(&pd->vidq)) |
| 357 | return -EBUSY; |
| 358 | pd->std = norm; |
| 359 | if (pd->std & V4L2_STD_525_60) { |
| 360 | pd->csr2 = VT_60HZ; |
| 361 | pd->width = 640; |
| 362 | pd->height = 480; |
| 363 | } else { |
| 364 | pd->csr2 = VT_50HZ; |
| 365 | pd->width = 768; |
| 366 | pd->height = 576; |
| 367 | } |
| 368 | return 0; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 369 | } |
| 370 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 371 | static int dt3155_enum_input(struct file *filp, void *p, struct v4l2_input *input) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 372 | { |
Hans Verkuil | c34b7ef | 2015-04-25 12:19:02 -0300 | [diff] [blame] | 373 | if (input->index > 3) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 374 | return -EINVAL; |
Hans Verkuil | c34b7ef | 2015-04-25 12:19:02 -0300 | [diff] [blame] | 375 | if (input->index) |
| 376 | snprintf(input->name, sizeof(input->name), "VID%d", input->index); |
| 377 | else |
| 378 | strlcpy(input->name, "J2/VID0", sizeof(input->name)); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 379 | input->type = V4L2_INPUT_TYPE_CAMERA; |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 380 | input->std = V4L2_STD_ALL; |
| 381 | input->status = 0; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 385 | static int dt3155_g_input(struct file *filp, void *p, unsigned int *i) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 386 | { |
Hans Verkuil | c34b7ef | 2015-04-25 12:19:02 -0300 | [diff] [blame] | 387 | struct dt3155_priv *pd = video_drvdata(filp); |
| 388 | |
| 389 | *i = pd->input; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 393 | static int dt3155_s_input(struct file *filp, void *p, unsigned int i) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 394 | { |
Hans Verkuil | c34b7ef | 2015-04-25 12:19:02 -0300 | [diff] [blame] | 395 | struct dt3155_priv *pd = video_drvdata(filp); |
| 396 | |
| 397 | if (i > 3) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 398 | return -EINVAL; |
Hans Verkuil | c34b7ef | 2015-04-25 12:19:02 -0300 | [diff] [blame] | 399 | pd->input = i; |
| 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); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 405 | static const struct v4l2_ioctl_ops dt3155_ioctl_ops = { |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 406 | .vidioc_querycap = dt3155_querycap, |
| 407 | .vidioc_enum_fmt_vid_cap = dt3155_enum_fmt_vid_cap, |
Hans Verkuil | 44a38df | 2015-04-25 12:16:45 -0300 | [diff] [blame] | 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, |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 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, |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 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, |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 424 | }; |
| 425 | |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 426 | static int dt3155_init_board(struct dt3155_priv *pd) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 427 | { |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 428 | struct pci_dev *pdev = pd->pdev; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 429 | int i; |
Hans Verkuil | deb2897 | 2015-04-25 11:41:31 -0300 | [diff] [blame] | 430 | u8 tmp = 0; |
Marin Mitov | a57941c | 2010-05-18 13:05:29 +0300 | [diff] [blame] | 431 | |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 432 | pci_set_master(pdev); /* dt3155 needs it */ |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 433 | |
| 434 | /* resetting the adapter */ |
Hans Verkuil | deb2897 | 2015-04-25 11:41:31 -0300 | [diff] [blame] | 435 | iowrite32(ADDR_ERR_ODD | ADDR_ERR_EVEN | FLD_CRPT_ODD | FLD_CRPT_EVEN | |
| 436 | FLD_DN_ODD | FLD_DN_EVEN, pd->regs + CSR1); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 437 | mmiowb(); |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 438 | msleep(20); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 439 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 440 | /* initializing adapter registers */ |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 441 | iowrite32(FIFO_EN | SRST, pd->regs + CSR1); |
| 442 | mmiowb(); |
| 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); |
| 454 | mmiowb(); |
| 455 | |
| 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) |
| 459 | return -ENODEV; |
| 460 | |
| 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); |
| 465 | |
| 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); |
| 474 | |
| 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); |
| 480 | } |
| 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); |
| 485 | } |
| 486 | write_i2c_reg(pd->regs, CONFIG, pd->config); /* ACQ_MODE_EVEN */ |
| 487 | |
Masanari Iida | 6dc8f38 | 2012-10-31 11:52:45 -0300 | [diff] [blame] | 488 | /* select channel 1 for input and set sync level */ |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 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); |
| 491 | |
Hans Verkuil | deb2897 | 2015-04-25 11:41:31 -0300 | [diff] [blame] | 492 | /* disable all irqs, clear all irq flags */ |
| 493 | iowrite32(FLD_START | FLD_END_EVEN | FLD_END_ODD, |
| 494 | pd->regs + INT_CSR); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 495 | |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static struct video_device dt3155_vdev = { |
| 500 | .name = DT3155_NAME, |
| 501 | .fops = &dt3155_fops, |
| 502 | .ioctl_ops = &dt3155_ioctl_ops, |
| 503 | .minor = -1, |
Hans Verkuil | f91fccd | 2015-03-09 13:33:59 -0300 | [diff] [blame] | 504 | .release = video_device_release_empty, |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 505 | .tvnorms = V4L2_STD_ALL, |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 506 | }; |
| 507 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 508 | static int dt3155_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 509 | { |
Marin Mitov | a57941c | 2010-05-18 13:05:29 +0300 | [diff] [blame] | 510 | int err; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 511 | struct dt3155_priv *pd; |
| 512 | |
Russell King | 6878897 | 2013-06-26 23:49:11 +0100 | [diff] [blame] | 513 | err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
H Hartley Sweeten | c94a2e4 | 2011-09-07 10:20:48 -0700 | [diff] [blame] | 514 | if (err) |
Marin Mitov | a57941c | 2010-05-18 13:05:29 +0300 | [diff] [blame] | 515 | return -ENODEV; |
Kiran Padwal | 92afdc1 | 2015-02-13 05:52:10 -0300 | [diff] [blame] | 516 | pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL); |
H Hartley Sweeten | c94a2e4 | 2011-09-07 10:20:48 -0700 | [diff] [blame] | 517 | if (!pd) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 518 | return -ENOMEM; |
Kiran Padwal | 92afdc1 | 2015-02-13 05:52:10 -0300 | [diff] [blame] | 519 | |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 520 | err = v4l2_device_register(&pdev->dev, &pd->v4l2_dev); |
| 521 | if (err) |
| 522 | return err; |
Hans Verkuil | f91fccd | 2015-03-09 13:33:59 -0300 | [diff] [blame] | 523 | pd->vdev = dt3155_vdev; |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 524 | pd->vdev.v4l2_dev = &pd->v4l2_dev; |
Hans Verkuil | f91fccd | 2015-03-09 13:33:59 -0300 | [diff] [blame] | 525 | video_set_drvdata(&pd->vdev, pd); /* for use in video_fops */ |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 526 | pd->pdev = pdev; |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 527 | pd->std = V4L2_STD_625_50; |
| 528 | pd->csr2 = VT_50HZ; |
| 529 | pd->width = 768; |
| 530 | pd->height = 576; |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 531 | INIT_LIST_HEAD(&pd->dmaq); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 532 | mutex_init(&pd->mux); |
Hans Verkuil | f91fccd | 2015-03-09 13:33:59 -0300 | [diff] [blame] | 533 | pd->vdev.lock = &pd->mux; /* for locking v4l2_file_operations */ |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 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; |
Hans Verkuil | 7c89a21 | 2015-04-26 06:27:00 -0300 | [diff] [blame^] | 541 | pd->vidq.gfp_flags = GFP_DMA32; |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 542 | pd->vidq.lock = &pd->mux; /* for locking v4l2_file_operations */ |
| 543 | pd->vdev.queue = &pd->vidq; |
| 544 | err = vb2_queue_init(&pd->vidq); |
| 545 | if (err < 0) |
| 546 | goto err_v4l2_dev_unreg; |
| 547 | pd->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); |
| 548 | if (IS_ERR(pd->alloc_ctx)) { |
| 549 | dev_err(&pdev->dev, "Can't allocate buffer context"); |
| 550 | err = PTR_ERR(pd->alloc_ctx); |
| 551 | goto err_v4l2_dev_unreg; |
| 552 | } |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 553 | spin_lock_init(&pd->lock); |
Hans Verkuil | 5c9ede4 | 2015-04-25 12:11:50 -0300 | [diff] [blame] | 554 | pd->config = ACQ_MODE_EVEN; |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 555 | err = pci_enable_device(pdev); |
H Hartley Sweeten | c94a2e4 | 2011-09-07 10:20:48 -0700 | [diff] [blame] | 556 | if (err) |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 557 | goto err_free_ctx; |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 558 | err = pci_request_region(pdev, 0, pci_name(pdev)); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 559 | if (err) |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 560 | goto err_pci_disable; |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 561 | pd->regs = pci_iomap(pdev, 0, pci_resource_len(pd->pdev, 0)); |
Dan Carpenter | aecf33d | 2011-12-22 02:29:34 -0300 | [diff] [blame] | 562 | if (!pd->regs) { |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 563 | err = -ENOMEM; |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 564 | goto err_free_reg; |
Dan Carpenter | aecf33d | 2011-12-22 02:29:34 -0300 | [diff] [blame] | 565 | } |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 566 | err = dt3155_init_board(pd); |
Marin Mitov | a57941c | 2010-05-18 13:05:29 +0300 | [diff] [blame] | 567 | if (err) |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 568 | goto err_iounmap; |
| 569 | err = request_irq(pd->pdev->irq, dt3155_irq_handler_even, |
| 570 | IRQF_SHARED, DT3155_NAME, pd); |
| 571 | if (err) |
| 572 | goto err_iounmap; |
Hans Verkuil | f91fccd | 2015-03-09 13:33:59 -0300 | [diff] [blame] | 573 | err = video_register_device(&pd->vdev, VFL_TYPE_GRABBER, -1); |
H Hartley Sweeten | c94a2e4 | 2011-09-07 10:20:48 -0700 | [diff] [blame] | 574 | if (err) |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 575 | goto err_free_irq; |
Hans Verkuil | f91fccd | 2015-03-09 13:33:59 -0300 | [diff] [blame] | 576 | dev_info(&pdev->dev, "/dev/video%i is ready\n", pd->vdev.minor); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 577 | return 0; /* success */ |
| 578 | |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 579 | err_free_irq: |
| 580 | free_irq(pd->pdev->irq, pd); |
| 581 | err_iounmap: |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 582 | pci_iounmap(pdev, pd->regs); |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 583 | err_free_reg: |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 584 | pci_release_region(pdev, 0); |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 585 | err_pci_disable: |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 586 | pci_disable_device(pdev); |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 587 | err_free_ctx: |
| 588 | vb2_dma_contig_cleanup_ctx(pd->alloc_ctx); |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 589 | err_v4l2_dev_unreg: |
| 590 | v4l2_device_unregister(&pd->v4l2_dev); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 591 | return err; |
| 592 | } |
| 593 | |
Hans Verkuil | 6a11087 | 2015-04-25 11:19:50 -0300 | [diff] [blame] | 594 | static void dt3155_remove(struct pci_dev *pdev) |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 595 | { |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 596 | struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev); |
| 597 | struct dt3155_priv *pd = container_of(v4l2_dev, struct dt3155_priv, v4l2_dev); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 598 | |
Hans Verkuil | f91fccd | 2015-03-09 13:33:59 -0300 | [diff] [blame] | 599 | video_unregister_device(&pd->vdev); |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 600 | free_irq(pd->pdev->irq, pd); |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 601 | vb2_queue_release(&pd->vidq); |
Hans Verkuil | 168b509 | 2015-04-25 11:32:54 -0300 | [diff] [blame] | 602 | v4l2_device_unregister(&pd->v4l2_dev); |
Marin Mitov | 8ded351 | 2011-05-28 21:45:27 +0300 | [diff] [blame] | 603 | pci_iounmap(pdev, pd->regs); |
| 604 | pci_release_region(pdev, 0); |
| 605 | pci_disable_device(pdev); |
Hans Verkuil | 9556be1 | 2015-04-25 11:51:36 -0300 | [diff] [blame] | 606 | vb2_dma_contig_cleanup_ctx(pd->alloc_ctx); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 607 | } |
| 608 | |
Jingoo Han | 41e043f | 2013-12-03 08:26:00 +0900 | [diff] [blame] | 609 | static const struct pci_device_id pci_ids[] = { |
Jon Mason | 6fb0e40 | 2014-03-03 14:00:38 -0300 | [diff] [blame] | 610 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, DT3155_DEVICE_ID) }, |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 611 | { 0, /* zero marks the end */ }, |
| 612 | }; |
| 613 | MODULE_DEVICE_TABLE(pci, pci_ids); |
| 614 | |
| 615 | static struct pci_driver pci_driver = { |
| 616 | .name = DT3155_NAME, |
| 617 | .id_table = pci_ids, |
| 618 | .probe = dt3155_probe, |
Bill Pemberton | 79fc8d8 | 2012-11-19 13:20:52 -0500 | [diff] [blame] | 619 | .remove = dt3155_remove, |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 620 | }; |
| 621 | |
Devendra Naga | 1a3acd3 | 2012-07-10 02:43:48 -0300 | [diff] [blame] | 622 | module_pci_driver(pci_driver); |
Marin Mitov | d42bffb | 2010-04-30 18:36:09 +0300 | [diff] [blame] | 623 | |
| 624 | MODULE_DESCRIPTION("video4linux pci-driver for dt3155 frame grabber"); |
| 625 | MODULE_AUTHOR("Marin Mitov <mitov@issp.bas.bg>"); |
| 626 | MODULE_VERSION(DT3155_VERSION); |
| 627 | MODULE_LICENSE("GPL"); |