Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Conexant CX23885 PCIe bridge |
| 3 | * |
Steven Toth | 6d89761 | 2008-09-03 17:12:12 -0300 | [diff] [blame] | 4 | * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org> |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/moduleparam.h> |
| 25 | #include <linux/init.h> |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 26 | |
| 27 | #include "cx23885.h" |
| 28 | |
| 29 | static unsigned int vbibufs = 4; |
| 30 | module_param(vbibufs, int, 0644); |
| 31 | MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32"); |
| 32 | |
Steven Toth | 4513fc69 | 2008-01-12 11:36:36 -0300 | [diff] [blame] | 33 | static unsigned int vbi_debug; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 34 | module_param(vbi_debug, int, 0644); |
| 35 | MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]"); |
| 36 | |
Steven Toth | 4513fc69 | 2008-01-12 11:36:36 -0300 | [diff] [blame] | 37 | #define dprintk(level, fmt, arg...)\ |
| 38 | do { if (vbi_debug >= level)\ |
| 39 | printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\ |
| 40 | } while (0) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 41 | |
| 42 | /* ------------------------------------------------------------------ */ |
| 43 | |
| 44 | int cx23885_vbi_fmt(struct file *file, void *priv, |
| 45 | struct v4l2_format *f) |
| 46 | { |
| 47 | struct cx23885_fh *fh = priv; |
| 48 | struct cx23885_dev *dev = fh->dev; |
| 49 | |
| 50 | if (dev->tvnorm & V4L2_STD_525_60) { |
| 51 | /* ntsc */ |
| 52 | f->fmt.vbi.sampling_rate = 28636363; |
| 53 | f->fmt.vbi.start[0] = 10; |
| 54 | f->fmt.vbi.start[1] = 273; |
| 55 | |
| 56 | } else if (dev->tvnorm & V4L2_STD_625_50) { |
| 57 | /* pal */ |
| 58 | f->fmt.vbi.sampling_rate = 35468950; |
| 59 | f->fmt.vbi.start[0] = 7 - 1; |
| 60 | f->fmt.vbi.start[1] = 319 - 1; |
| 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |
Steven Toth | b5f7405 | 2011-10-10 11:09:54 -0300 | [diff] [blame^] | 65 | /* We're given the Video Interrupt status register. |
| 66 | * The cx23885_video_irq() func has already validated |
| 67 | * the potential error bits, we just need to |
| 68 | * deal with vbi payload and return indication if |
| 69 | * we actually processed any payload. |
| 70 | */ |
| 71 | int cx23885_vbi_irq(struct cx23885_dev *dev, u32 status) |
| 72 | { |
| 73 | u32 count; |
| 74 | int handled = 0; |
| 75 | |
| 76 | if (status & VID_BC_MSK_VBI_RISCI1) { |
| 77 | dprintk(1, "%s() VID_BC_MSK_VBI_RISCI1\n", __func__); |
| 78 | spin_lock(&dev->slock); |
| 79 | count = cx_read(VID_A_GPCNT); |
| 80 | cx23885_video_wakeup(dev, &dev->vbiq, count); |
| 81 | spin_unlock(&dev->slock); |
| 82 | handled++; |
| 83 | } |
| 84 | |
| 85 | if (status & VID_BC_MSK_VBI_RISCI2) { |
| 86 | dprintk(1, "%s() VID_BC_MSK_VBI_RISCI2\n", __func__); |
| 87 | dprintk(2, "stopper vbi\n"); |
| 88 | spin_lock(&dev->slock); |
| 89 | cx23885_restart_vbi_queue(dev, &dev->vbiq); |
| 90 | spin_unlock(&dev->slock); |
| 91 | handled++; |
| 92 | } |
| 93 | |
| 94 | return handled; |
| 95 | } |
| 96 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 97 | static int cx23885_start_vbi_dma(struct cx23885_dev *dev, |
| 98 | struct cx23885_dmaqueue *q, |
| 99 | struct cx23885_buffer *buf) |
| 100 | { |
Steven Toth | b5f7405 | 2011-10-10 11:09:54 -0300 | [diff] [blame^] | 101 | dprintk(1, "%s()\n", __func__); |
| 102 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 103 | /* setup fifo + format */ |
| 104 | cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02], |
| 105 | buf->vb.width, buf->risc.dma); |
| 106 | |
| 107 | /* reset counter */ |
Steven Toth | b5f7405 | 2011-10-10 11:09:54 -0300 | [diff] [blame^] | 108 | cx_write(VID_A_GPCNT_CTL, 3); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 109 | q->count = 1; |
| 110 | |
Steven Toth | b5f7405 | 2011-10-10 11:09:54 -0300 | [diff] [blame^] | 111 | /* enable irq */ |
Andy Walls | dbe83a3 | 2010-07-19 01:19:43 -0300 | [diff] [blame] | 112 | cx23885_irq_add_enable(dev, 0x01); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 113 | cx_set(VID_A_INT_MSK, 0x000022); |
| 114 | |
| 115 | /* start dma */ |
| 116 | cx_set(DEV_CNTRL2, (1<<5)); |
Steven Toth | b5f7405 | 2011-10-10 11:09:54 -0300 | [diff] [blame^] | 117 | cx_set(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */ |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 122 | |
Steven Toth | b5f7405 | 2011-10-10 11:09:54 -0300 | [diff] [blame^] | 123 | int cx23885_restart_vbi_queue(struct cx23885_dev *dev, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 124 | struct cx23885_dmaqueue *q) |
| 125 | { |
| 126 | struct cx23885_buffer *buf; |
| 127 | struct list_head *item; |
| 128 | |
| 129 | if (list_empty(&q->active)) |
| 130 | return 0; |
| 131 | |
| 132 | buf = list_entry(q->active.next, struct cx23885_buffer, vb.queue); |
| 133 | dprintk(2, "restart_queue [%p/%d]: restart dma\n", |
| 134 | buf, buf->vb.i); |
| 135 | cx23885_start_vbi_dma(dev, q, buf); |
| 136 | list_for_each(item, &q->active) { |
| 137 | buf = list_entry(item, struct cx23885_buffer, vb.queue); |
| 138 | buf->count = q->count++; |
| 139 | } |
| 140 | mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | void cx23885_vbi_timeout(unsigned long data) |
| 145 | { |
| 146 | struct cx23885_dev *dev = (struct cx23885_dev *)data; |
| 147 | struct cx23885_dmaqueue *q = &dev->vbiq; |
| 148 | struct cx23885_buffer *buf; |
| 149 | unsigned long flags; |
| 150 | |
| 151 | cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH02]); |
| 152 | |
Steven Toth | b5f7405 | 2011-10-10 11:09:54 -0300 | [diff] [blame^] | 153 | /* Stop the VBI engine */ |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 154 | cx_clear(VID_A_DMA_CTL, 0x22); |
| 155 | |
| 156 | spin_lock_irqsave(&dev->slock, flags); |
| 157 | while (!list_empty(&q->active)) { |
| 158 | buf = list_entry(q->active.next, struct cx23885_buffer, |
| 159 | vb.queue); |
| 160 | list_del(&buf->vb.queue); |
| 161 | buf->vb.state = VIDEOBUF_ERROR; |
| 162 | wake_up(&buf->vb.done); |
| 163 | printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->name, |
| 164 | buf, buf->vb.i, (unsigned long)buf->risc.dma); |
| 165 | } |
| 166 | cx23885_restart_vbi_queue(dev, q); |
| 167 | spin_unlock_irqrestore(&dev->slock, flags); |
| 168 | } |
| 169 | |
| 170 | /* ------------------------------------------------------------------ */ |
| 171 | #define VBI_LINE_LENGTH 2048 |
| 172 | #define VBI_LINE_COUNT 17 |
| 173 | |
| 174 | static int |
| 175 | vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size) |
| 176 | { |
| 177 | *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2; |
| 178 | if (0 == *count) |
| 179 | *count = vbibufs; |
| 180 | if (*count < 2) |
| 181 | *count = 2; |
| 182 | if (*count > 32) |
| 183 | *count = 32; |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static int |
| 188 | vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, |
| 189 | enum v4l2_field field) |
| 190 | { |
| 191 | struct cx23885_fh *fh = q->priv_data; |
| 192 | struct cx23885_dev *dev = fh->dev; |
| 193 | struct cx23885_buffer *buf = container_of(vb, |
| 194 | struct cx23885_buffer, vb); |
| 195 | struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb); |
| 196 | unsigned int size; |
| 197 | int rc; |
| 198 | |
| 199 | size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2; |
| 200 | if (0 != buf->vb.baddr && buf->vb.bsize < size) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { |
| 204 | buf->vb.width = VBI_LINE_LENGTH; |
| 205 | buf->vb.height = VBI_LINE_COUNT; |
| 206 | buf->vb.size = size; |
| 207 | buf->vb.field = V4L2_FIELD_SEQ_TB; |
| 208 | |
| 209 | rc = videobuf_iolock(q, &buf->vb, NULL); |
| 210 | if (0 != rc) |
| 211 | goto fail; |
| 212 | cx23885_risc_buffer(dev->pci, &buf->risc, |
| 213 | dma->sglist, |
| 214 | 0, buf->vb.width * buf->vb.height, |
| 215 | buf->vb.width, 0, |
| 216 | buf->vb.height); |
| 217 | } |
| 218 | buf->vb.state = VIDEOBUF_PREPARED; |
| 219 | return 0; |
| 220 | |
| 221 | fail: |
| 222 | cx23885_free_buffer(q, buf); |
| 223 | return rc; |
| 224 | } |
| 225 | |
| 226 | static void |
| 227 | vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) |
| 228 | { |
| 229 | struct cx23885_buffer *buf = |
| 230 | container_of(vb, struct cx23885_buffer, vb); |
| 231 | struct cx23885_buffer *prev; |
| 232 | struct cx23885_fh *fh = vq->priv_data; |
| 233 | struct cx23885_dev *dev = fh->dev; |
| 234 | struct cx23885_dmaqueue *q = &dev->vbiq; |
| 235 | |
| 236 | /* add jump to stopper */ |
| 237 | buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC); |
| 238 | buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma); |
| 239 | buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */ |
| 240 | |
| 241 | if (list_empty(&q->active)) { |
| 242 | list_add_tail(&buf->vb.queue, &q->active); |
| 243 | cx23885_start_vbi_dma(dev, q, buf); |
| 244 | buf->vb.state = VIDEOBUF_ACTIVE; |
| 245 | buf->count = q->count++; |
| 246 | mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); |
| 247 | dprintk(2, "[%p/%d] vbi_queue - first active\n", |
| 248 | buf, buf->vb.i); |
| 249 | |
| 250 | } else { |
| 251 | prev = list_entry(q->active.prev, struct cx23885_buffer, |
| 252 | vb.queue); |
| 253 | list_add_tail(&buf->vb.queue, &q->active); |
| 254 | buf->vb.state = VIDEOBUF_ACTIVE; |
| 255 | buf->count = q->count++; |
| 256 | prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); |
| 257 | prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63-32 */ |
| 258 | dprintk(2, "[%p/%d] buffer_queue - append to active\n", |
| 259 | buf, buf->vb.i); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb) |
| 264 | { |
| 265 | struct cx23885_buffer *buf = |
| 266 | container_of(vb, struct cx23885_buffer, vb); |
| 267 | |
| 268 | cx23885_free_buffer(q, buf); |
| 269 | } |
| 270 | |
| 271 | struct videobuf_queue_ops cx23885_vbi_qops = { |
| 272 | .buf_setup = vbi_setup, |
| 273 | .buf_prepare = vbi_prepare, |
| 274 | .buf_queue = vbi_queue, |
| 275 | .buf_release = vbi_release, |
| 276 | }; |
| 277 | |
| 278 | /* ------------------------------------------------------------------ */ |
| 279 | /* |
| 280 | * Local variables: |
| 281 | * c-basic-offset: 8 |
| 282 | * End: |
| 283 | */ |