Merge branch 'drm-ttm-unmappable' into drm-core-next
[pandora-kernel.git] / drivers / media / video / cx23885 / cx23885-vbi.c
1 /*
2  *  Driver for the Conexant CX23885 PCIe bridge
3  *
4  *  Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
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>
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
33 static unsigned int vbi_debug;
34 module_param(vbi_debug, int, 0644);
35 MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
36
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)
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
65 static int cx23885_start_vbi_dma(struct cx23885_dev    *dev,
66                          struct cx23885_dmaqueue *q,
67                          struct cx23885_buffer   *buf)
68 {
69         /* setup fifo + format */
70         cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
71                                 buf->vb.width, buf->risc.dma);
72
73         /* reset counter */
74         q->count = 1;
75
76         /* enable irqs */
77         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
78         cx_set(VID_A_INT_MSK, 0x000022);
79
80         /* start dma */
81         cx_set(DEV_CNTRL2, (1<<5));
82         cx_set(VID_A_DMA_CTL, 0x00000022);
83
84         return 0;
85 }
86
87
88 static int cx23885_restart_vbi_queue(struct cx23885_dev    *dev,
89                              struct cx23885_dmaqueue *q)
90 {
91         struct cx23885_buffer *buf;
92         struct list_head *item;
93
94         if (list_empty(&q->active))
95                 return 0;
96
97         buf = list_entry(q->active.next, struct cx23885_buffer, vb.queue);
98         dprintk(2, "restart_queue [%p/%d]: restart dma\n",
99                 buf, buf->vb.i);
100         cx23885_start_vbi_dma(dev, q, buf);
101         list_for_each(item, &q->active) {
102                 buf = list_entry(item, struct cx23885_buffer, vb.queue);
103                 buf->count = q->count++;
104         }
105         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
106         return 0;
107 }
108
109 void cx23885_vbi_timeout(unsigned long data)
110 {
111         struct cx23885_dev *dev = (struct cx23885_dev *)data;
112         struct cx23885_dmaqueue *q = &dev->vbiq;
113         struct cx23885_buffer *buf;
114         unsigned long flags;
115
116         cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH02]);
117
118         cx_clear(VID_A_DMA_CTL, 0x22);
119
120         spin_lock_irqsave(&dev->slock, flags);
121         while (!list_empty(&q->active)) {
122                 buf = list_entry(q->active.next, struct cx23885_buffer,
123                         vb.queue);
124                 list_del(&buf->vb.queue);
125                 buf->vb.state = VIDEOBUF_ERROR;
126                 wake_up(&buf->vb.done);
127                 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->name,
128                        buf, buf->vb.i, (unsigned long)buf->risc.dma);
129         }
130         cx23885_restart_vbi_queue(dev, q);
131         spin_unlock_irqrestore(&dev->slock, flags);
132 }
133
134 /* ------------------------------------------------------------------ */
135 #define VBI_LINE_LENGTH 2048
136 #define VBI_LINE_COUNT 17
137
138 static int
139 vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
140 {
141         *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
142         if (0 == *count)
143                 *count = vbibufs;
144         if (*count < 2)
145                 *count = 2;
146         if (*count > 32)
147                 *count = 32;
148         return 0;
149 }
150
151 static int
152 vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
153             enum v4l2_field field)
154 {
155         struct cx23885_fh *fh  = q->priv_data;
156         struct cx23885_dev *dev = fh->dev;
157         struct cx23885_buffer *buf = container_of(vb,
158                 struct cx23885_buffer, vb);
159         struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
160         unsigned int size;
161         int rc;
162
163         size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
164         if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
165                 return -EINVAL;
166
167         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
168                 buf->vb.width  = VBI_LINE_LENGTH;
169                 buf->vb.height = VBI_LINE_COUNT;
170                 buf->vb.size   = size;
171                 buf->vb.field  = V4L2_FIELD_SEQ_TB;
172
173                 rc = videobuf_iolock(q, &buf->vb, NULL);
174                 if (0 != rc)
175                         goto fail;
176                 cx23885_risc_buffer(dev->pci, &buf->risc,
177                                  dma->sglist,
178                                  0, buf->vb.width * buf->vb.height,
179                                  buf->vb.width, 0,
180                                  buf->vb.height);
181         }
182         buf->vb.state = VIDEOBUF_PREPARED;
183         return 0;
184
185  fail:
186         cx23885_free_buffer(q, buf);
187         return rc;
188 }
189
190 static void
191 vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
192 {
193         struct cx23885_buffer   *buf =
194                 container_of(vb, struct cx23885_buffer, vb);
195         struct cx23885_buffer   *prev;
196         struct cx23885_fh       *fh   = vq->priv_data;
197         struct cx23885_dev      *dev  = fh->dev;
198         struct cx23885_dmaqueue *q    = &dev->vbiq;
199
200         /* add jump to stopper */
201         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
202         buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
203         buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
204
205         if (list_empty(&q->active)) {
206                 list_add_tail(&buf->vb.queue, &q->active);
207                 cx23885_start_vbi_dma(dev, q, buf);
208                 buf->vb.state = VIDEOBUF_ACTIVE;
209                 buf->count    = q->count++;
210                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
211                 dprintk(2, "[%p/%d] vbi_queue - first active\n",
212                         buf, buf->vb.i);
213
214         } else {
215                 prev = list_entry(q->active.prev, struct cx23885_buffer,
216                         vb.queue);
217                 list_add_tail(&buf->vb.queue, &q->active);
218                 buf->vb.state = VIDEOBUF_ACTIVE;
219                 buf->count    = q->count++;
220                 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
221                 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63-32 */
222                 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
223                         buf, buf->vb.i);
224         }
225 }
226
227 static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
228 {
229         struct cx23885_buffer *buf =
230                 container_of(vb, struct cx23885_buffer, vb);
231
232         cx23885_free_buffer(q, buf);
233 }
234
235 struct videobuf_queue_ops cx23885_vbi_qops = {
236         .buf_setup    = vbi_setup,
237         .buf_prepare  = vbi_prepare,
238         .buf_queue    = vbi_queue,
239         .buf_release  = vbi_release,
240 };
241
242 /* ------------------------------------------------------------------ */
243 /*
244  * Local variables:
245  * c-basic-offset: 8
246  * End:
247  */