blob: b8ff459d0d3085208845938c4bb15c90d4b94b9f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
5 *
6 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/slab.h>
28#include <linux/delay.h>
29
30#include "saa7134-reg.h"
31#include "saa7134.h"
32
33/* ------------------------------------------------------------------ */
34
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030035static unsigned int ts_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036module_param(ts_debug, int, 0644);
37MODULE_PARM_DESC(ts_debug,"enable debug messages [ts]");
38
39#define dprintk(fmt, arg...) if (ts_debug) \
40 printk(KERN_DEBUG "%s/ts: " fmt, dev->name , ## arg)
41
42/* ------------------------------------------------------------------ */
43
44static int buffer_activate(struct saa7134_dev *dev,
45 struct saa7134_buf *buf,
46 struct saa7134_buf *next)
47{
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 dprintk("buffer_activate [%p]",buf);
Brandon Philips0fc06862007-11-06 20:02:36 -030050 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 buf->top_seen = 0;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (NULL == next)
54 next = buf;
55 if (V4L2_FIELD_TOP == buf->vb.field) {
56 dprintk("- [top] buf=%p next=%p\n",buf,next);
57 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(buf));
58 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(next));
59 } else {
60 dprintk("- [bottom] buf=%p next=%p\n",buf,next);
61 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(next));
62 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(buf));
63 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 /* start DMA */
66 saa7134_set_dmabits(dev);
67
Dmitri Belimov82aa98f2009-04-28 06:41:08 -030068 mod_timer(&dev->ts_q.timeout, jiffies+TS_BUFFER_TIMEOUT);
Dmitry Belimov92c36142008-08-26 14:16:33 -030069
70 if (dev->ts_state == SAA7134_TS_BUFF_DONE) {
71 /* Clear TS cache */
72 dev->buff_cnt = 0;
73 saa_writeb(SAA7134_TS_SERIAL1, 0x00);
74 saa_writeb(SAA7134_TS_SERIAL1, 0x03);
75 saa_writeb(SAA7134_TS_SERIAL1, 0x00);
76 saa_writeb(SAA7134_TS_SERIAL1, 0x01);
77
78 /* TS clock non-inverted */
79 saa_writeb(SAA7134_TS_SERIAL1, 0x00);
80
81 /* Start TS stream */
Michael Krufky90a4cc72009-01-13 04:03:26 -030082 switch (saa7134_boards[dev->board].ts_type) {
83 case SAA7134_MPEG_TS_PARALLEL:
84 saa_writeb(SAA7134_TS_SERIAL0, 0x40);
85 saa_writeb(SAA7134_TS_PARALLEL, 0xec);
86 break;
87 case SAA7134_MPEG_TS_SERIAL:
88 saa_writeb(SAA7134_TS_SERIAL0, 0xd8);
89 saa_writeb(SAA7134_TS_PARALLEL, 0x6c);
90 saa_writeb(SAA7134_TS_PARALLEL_SERIAL, 0xbc);
91 saa_writeb(SAA7134_TS_SERIAL1, 0x02);
92 break;
93 }
94
Dmitry Belimov92c36142008-08-26 14:16:33 -030095 dev->ts_state = SAA7134_TS_STARTED;
96 }
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
99}
100
101static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
Dmitry Belimov92c36142008-08-26 14:16:33 -0300102 enum v4l2_field field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct saa7134_dev *dev = q->priv_data;
105 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
106 unsigned int lines, llength, size;
Hartmut Hackmann3aa4f482005-11-08 21:36:30 -0800107 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int err;
109
110 dprintk("buffer_prepare [%p,%s]\n",buf,v4l2_field_names[field]);
111
112 llength = TS_PACKET_SIZE;
113 lines = dev->ts.nr_packets;
114
115 size = lines * llength;
116 if (0 != buf->vb.baddr && buf->vb.bsize < size)
117 return -EINVAL;
118
119 if (buf->vb.size != size) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300120 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
Brandon Philips0fc06862007-11-06 20:02:36 -0300123 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300124 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 buf->vb.width = llength;
127 buf->vb.height = lines;
128 buf->vb.size = size;
129 buf->pt = &dev->ts.pt_ts;
130
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300131 err = videobuf_iolock(q,&buf->vb,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (err)
133 goto oops;
134 err = saa7134_pgtable_build(dev->pci,buf->pt,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300135 dma->sglist,
136 dma->sglen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 saa7134_buffer_startpage(buf));
138 if (err)
139 goto oops;
140 }
Hartmut Hackmann3aa4f482005-11-08 21:36:30 -0800141
Dmitry Belimov92c36142008-08-26 14:16:33 -0300142 dev->buff_cnt++;
Hartmut Hackmann3aa4f482005-11-08 21:36:30 -0800143
Dmitry Belimov92c36142008-08-26 14:16:33 -0300144 if (dev->buff_cnt == dev->ts.nr_bufs) {
145 dev->ts_state = SAA7134_TS_BUFF_DONE;
146 /* dma: setup channel 5 (= TS) */
147 control = SAA7134_RS_CONTROL_BURST_16 |
148 SAA7134_RS_CONTROL_ME |
149 (buf->pt->dma >> 12);
150
151 saa_writeb(SAA7134_TS_DMA0, (lines - 1) & 0xff);
152 saa_writeb(SAA7134_TS_DMA1, ((lines - 1) >> 8) & 0xff);
153 /* TSNOPIT=0, TSCOLAP=0 */
154 saa_writeb(SAA7134_TS_DMA2, (((lines - 1) >> 16) & 0x3f) | 0x00);
155 saa_writel(SAA7134_RS_PITCH(5), TS_PACKET_SIZE);
156 saa_writel(SAA7134_RS_CONTROL(5), control);
157 }
Hartmut Hackmann3aa4f482005-11-08 21:36:30 -0800158
Brandon Philips0fc06862007-11-06 20:02:36 -0300159 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 buf->activate = buffer_activate;
161 buf->vb.field = field;
162 return 0;
163
164 oops:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300165 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return err;
167}
168
169static int
170buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
171{
172 struct saa7134_dev *dev = q->priv_data;
173
174 *size = TS_PACKET_SIZE * dev->ts.nr_packets;
175 if (0 == *count)
176 *count = dev->ts.nr_bufs;
177 *count = saa7134_buffer_count(*size,*count);
Dmitry Belimov92c36142008-08-26 14:16:33 -0300178 dev->buff_cnt = 0;
179 dev->ts_state = SAA7134_TS_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
181}
182
183static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
184{
185 struct saa7134_dev *dev = q->priv_data;
186 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
187
188 saa7134_buffer_queue(dev,&dev->ts_q,buf);
189}
190
191static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
192{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
Dmitry Belimov92c36142008-08-26 14:16:33 -0300194 struct saa7134_dev *dev = q->priv_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Dmitry Belimov92c36142008-08-26 14:16:33 -0300196 if (dev->ts_state == SAA7134_TS_STARTED) {
197 /* Stop TS transport */
198 saa_writeb(SAA7134_TS_PARALLEL, 0x6c);
199 dev->ts_state = SAA7134_TS_STOPPED;
200 }
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300201 saa7134_dma_free(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204struct videobuf_queue_ops saa7134_ts_qops = {
205 .buf_setup = buffer_setup,
206 .buf_prepare = buffer_prepare,
207 .buf_queue = buffer_queue,
208 .buf_release = buffer_release,
209};
210EXPORT_SYMBOL_GPL(saa7134_ts_qops);
211
212/* ----------------------------------------------------------- */
213/* exported stuff */
214
Hartmut Hackmann3aa4f482005-11-08 21:36:30 -0800215static unsigned int tsbufs = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216module_param(tsbufs, int, 0444);
217MODULE_PARM_DESC(tsbufs,"number of ts buffers, range 2-32");
218
Hartmut Hackmann3aa4f482005-11-08 21:36:30 -0800219static unsigned int ts_nr_packets = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220module_param(ts_nr_packets, int, 0444);
221MODULE_PARM_DESC(ts_nr_packets,"size of a ts buffers (in ts packets)");
222
Maxim Levitskycb712012007-09-27 20:34:25 -0300223int saa7134_ts_init_hw(struct saa7134_dev *dev)
224{
225 /* deactivate TS softreset */
226 saa_writeb(SAA7134_TS_SERIAL1, 0x00);
227 /* TSSOP high active, TSVAL high active, TSLOCK ignored */
Dmitry Belimov92c36142008-08-26 14:16:33 -0300228 saa_writeb(SAA7134_TS_PARALLEL, 0x6c);
Maxim Levitskycb712012007-09-27 20:34:25 -0300229 saa_writeb(SAA7134_TS_PARALLEL_SERIAL, (TS_PACKET_SIZE-1));
230 saa_writeb(SAA7134_TS_DMA0, ((dev->ts.nr_packets-1)&0xff));
231 saa_writeb(SAA7134_TS_DMA1, (((dev->ts.nr_packets-1)>>8)&0xff));
232 /* TSNOPIT=0, TSCOLAP=0 */
233 saa_writeb(SAA7134_TS_DMA2,
234 ((((dev->ts.nr_packets-1)>>16)&0x3f) | 0x00));
235
236 return 0;
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239int saa7134_ts_init1(struct saa7134_dev *dev)
240{
241 /* sanitycheck insmod options */
242 if (tsbufs < 2)
243 tsbufs = 2;
244 if (tsbufs > VIDEO_MAX_FRAME)
245 tsbufs = VIDEO_MAX_FRAME;
246 if (ts_nr_packets < 4)
247 ts_nr_packets = 4;
248 if (ts_nr_packets > 312)
249 ts_nr_packets = 312;
250 dev->ts.nr_bufs = tsbufs;
251 dev->ts.nr_packets = ts_nr_packets;
252
253 INIT_LIST_HEAD(&dev->ts_q.queue);
254 init_timer(&dev->ts_q.timeout);
255 dev->ts_q.timeout.function = saa7134_buffer_timeout;
256 dev->ts_q.timeout.data = (unsigned long)(&dev->ts_q);
257 dev->ts_q.dev = dev;
258 dev->ts_q.need_two = 1;
259 saa7134_pgtable_alloc(dev->pci,&dev->ts.pt_ts);
260
261 /* init TS hw */
Maxim Levitskycb712012007-09-27 20:34:25 -0300262 saa7134_ts_init_hw(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 return 0;
265}
266
267int saa7134_ts_fini(struct saa7134_dev *dev)
268{
269 saa7134_pgtable_free(dev->pci,&dev->ts.pt_ts);
270 return 0;
271}
272
273
274void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status)
275{
276 enum v4l2_field field;
277
278 spin_lock(&dev->slock);
279 if (dev->ts_q.curr) {
280 field = dev->ts_q.curr->vb.field;
281 if (field == V4L2_FIELD_TOP) {
Hartmut Hackmann3aa4f482005-11-08 21:36:30 -0800282 if ((status & 0x100000) != 0x000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 goto done;
284 } else {
Hartmut Hackmann3aa4f482005-11-08 21:36:30 -0800285 if ((status & 0x100000) != 0x100000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto done;
287 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300288 saa7134_buffer_finish(dev,&dev->ts_q,VIDEOBUF_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 saa7134_buffer_next(dev,&dev->ts_q);
291
292 done:
293 spin_unlock(&dev->slock);
294}
295
296/* ----------------------------------------------------------- */
297/*
298 * Local variables:
299 * c-basic-offset: 8
300 * End:
301 */