3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
6 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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.
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.
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.
24 #include "saa7134-reg.h"
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
32 /* ------------------------------------------------------------------ */
34 static unsigned int ts_debug
;
35 module_param(ts_debug
, int, 0644);
36 MODULE_PARM_DESC(ts_debug
,"enable debug messages [ts]");
38 #define ts_dbg(fmt, arg...) do { \
40 printk(KERN_DEBUG pr_fmt("ts: " fmt), ## arg); \
43 /* ------------------------------------------------------------------ */
44 static int buffer_activate(struct saa7134_dev
*dev
,
45 struct saa7134_buf
*buf
,
46 struct saa7134_buf
*next
)
49 ts_dbg("buffer_activate [%p]", buf
);
53 dev
->ts_field
= V4L2_FIELD_TOP
;
57 if (V4L2_FIELD_TOP
== dev
->ts_field
) {
58 ts_dbg("- [top] buf=%p next=%p\n", buf
, next
);
59 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(buf
));
60 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(next
));
61 dev
->ts_field
= V4L2_FIELD_BOTTOM
;
63 ts_dbg("- [bottom] buf=%p next=%p\n", buf
, next
);
64 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(next
));
65 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(buf
));
66 dev
->ts_field
= V4L2_FIELD_TOP
;
70 saa7134_set_dmabits(dev
);
72 mod_timer(&dev
->ts_q
.timeout
, jiffies
+TS_BUFFER_TIMEOUT
);
75 saa7134_ts_start(dev
);
80 int saa7134_ts_buffer_init(struct vb2_buffer
*vb2
)
82 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb2
);
83 struct saa7134_dmaqueue
*dmaq
= vb2
->vb2_queue
->drv_priv
;
84 struct saa7134_buf
*buf
= container_of(vbuf
, struct saa7134_buf
, vb2
);
87 buf
->activate
= buffer_activate
;
91 EXPORT_SYMBOL_GPL(saa7134_ts_buffer_init
);
93 int saa7134_ts_buffer_prepare(struct vb2_buffer
*vb2
)
95 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb2
);
96 struct saa7134_dmaqueue
*dmaq
= vb2
->vb2_queue
->drv_priv
;
97 struct saa7134_dev
*dev
= dmaq
->dev
;
98 struct saa7134_buf
*buf
= container_of(vbuf
, struct saa7134_buf
, vb2
);
99 struct sg_table
*dma
= vb2_dma_sg_plane_desc(vb2
, 0);
100 unsigned int lines
, llength
, size
;
102 ts_dbg("buffer_prepare [%p]\n", buf
);
104 llength
= TS_PACKET_SIZE
;
105 lines
= dev
->ts
.nr_packets
;
107 size
= lines
* llength
;
108 if (vb2_plane_size(vb2
, 0) < size
)
111 vb2_set_plane_payload(vb2
, 0, size
);
112 vbuf
->field
= dev
->field
;
114 return saa7134_pgtable_build(dev
->pci
, &dmaq
->pt
, dma
->sgl
, dma
->nents
,
115 saa7134_buffer_startpage(buf
));
117 EXPORT_SYMBOL_GPL(saa7134_ts_buffer_prepare
);
119 int saa7134_ts_queue_setup(struct vb2_queue
*q
,
120 unsigned int *nbuffers
, unsigned int *nplanes
,
121 unsigned int sizes
[], struct device
*alloc_devs
[])
123 struct saa7134_dmaqueue
*dmaq
= q
->drv_priv
;
124 struct saa7134_dev
*dev
= dmaq
->dev
;
125 int size
= TS_PACKET_SIZE
* dev
->ts
.nr_packets
;
128 *nbuffers
= dev
->ts
.nr_bufs
;
129 *nbuffers
= saa7134_buffer_count(size
, *nbuffers
);
136 EXPORT_SYMBOL_GPL(saa7134_ts_queue_setup
);
138 int saa7134_ts_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
140 struct saa7134_dmaqueue
*dmaq
= vq
->drv_priv
;
141 struct saa7134_dev
*dev
= dmaq
->dev
;
144 * Planar video capture and TS share the same DMA channel,
145 * so only one can be active at a time.
147 if (vb2_is_busy(&dev
->video_vbq
) && dev
->fmt
->planar
) {
148 struct saa7134_buf
*buf
, *tmp
;
150 list_for_each_entry_safe(buf
, tmp
, &dmaq
->queue
, entry
) {
151 list_del(&buf
->entry
);
152 vb2_buffer_done(&buf
->vb2
.vb2_buf
,
153 VB2_BUF_STATE_QUEUED
);
156 vb2_buffer_done(&dmaq
->curr
->vb2
.vb2_buf
,
157 VB2_BUF_STATE_QUEUED
);
165 EXPORT_SYMBOL_GPL(saa7134_ts_start_streaming
);
167 void saa7134_ts_stop_streaming(struct vb2_queue
*vq
)
169 struct saa7134_dmaqueue
*dmaq
= vq
->drv_priv
;
170 struct saa7134_dev
*dev
= dmaq
->dev
;
172 saa7134_ts_stop(dev
);
173 saa7134_stop_streaming(dev
, dmaq
);
175 EXPORT_SYMBOL_GPL(saa7134_ts_stop_streaming
);
177 struct vb2_ops saa7134_ts_qops
= {
178 .queue_setup
= saa7134_ts_queue_setup
,
179 .buf_init
= saa7134_ts_buffer_init
,
180 .buf_prepare
= saa7134_ts_buffer_prepare
,
181 .buf_queue
= saa7134_vb2_buffer_queue
,
182 .wait_prepare
= vb2_ops_wait_prepare
,
183 .wait_finish
= vb2_ops_wait_finish
,
184 .stop_streaming
= saa7134_ts_stop_streaming
,
186 EXPORT_SYMBOL_GPL(saa7134_ts_qops
);
188 /* ----------------------------------------------------------- */
191 static unsigned int tsbufs
= 8;
192 module_param(tsbufs
, int, 0444);
193 MODULE_PARM_DESC(tsbufs
, "number of ts buffers for read/write IO, range 2-32");
195 static unsigned int ts_nr_packets
= 64;
196 module_param(ts_nr_packets
, int, 0444);
197 MODULE_PARM_DESC(ts_nr_packets
,"size of a ts buffers (in ts packets)");
199 int saa7134_ts_init_hw(struct saa7134_dev
*dev
)
201 /* deactivate TS softreset */
202 saa_writeb(SAA7134_TS_SERIAL1
, 0x00);
203 /* TSSOP high active, TSVAL high active, TSLOCK ignored */
204 saa_writeb(SAA7134_TS_PARALLEL
, 0x6c);
205 saa_writeb(SAA7134_TS_PARALLEL_SERIAL
, (TS_PACKET_SIZE
-1));
206 saa_writeb(SAA7134_TS_DMA0
, ((dev
->ts
.nr_packets
-1)&0xff));
207 saa_writeb(SAA7134_TS_DMA1
, (((dev
->ts
.nr_packets
-1)>>8)&0xff));
208 /* TSNOPIT=0, TSCOLAP=0 */
209 saa_writeb(SAA7134_TS_DMA2
,
210 ((((dev
->ts
.nr_packets
-1)>>16)&0x3f) | 0x00));
215 int saa7134_ts_init1(struct saa7134_dev
*dev
)
217 /* sanitycheck insmod options */
220 if (tsbufs
> VIDEO_MAX_FRAME
)
221 tsbufs
= VIDEO_MAX_FRAME
;
222 if (ts_nr_packets
< 4)
224 if (ts_nr_packets
> 312)
226 dev
->ts
.nr_bufs
= tsbufs
;
227 dev
->ts
.nr_packets
= ts_nr_packets
;
229 INIT_LIST_HEAD(&dev
->ts_q
.queue
);
230 init_timer(&dev
->ts_q
.timeout
);
231 dev
->ts_q
.timeout
.function
= saa7134_buffer_timeout
;
232 dev
->ts_q
.timeout
.data
= (unsigned long)(&dev
->ts_q
);
234 dev
->ts_q
.need_two
= 1;
236 saa7134_pgtable_alloc(dev
->pci
, &dev
->ts_q
.pt
);
239 saa7134_ts_init_hw(dev
);
244 /* Function for stop TS */
245 int saa7134_ts_stop(struct saa7134_dev
*dev
)
249 if (!dev
->ts_started
)
253 switch (saa7134_boards
[dev
->board
].ts_type
) {
254 case SAA7134_MPEG_TS_PARALLEL
:
255 saa_writeb(SAA7134_TS_PARALLEL
, 0x6c);
258 case SAA7134_MPEG_TS_SERIAL
:
259 saa_writeb(SAA7134_TS_SERIAL0
, 0x40);
266 /* Function for start TS */
267 int saa7134_ts_start(struct saa7134_dev
*dev
)
269 ts_dbg("TS start\n");
271 if (WARN_ON(dev
->ts_started
))
274 /* dma: setup channel 5 (= TS) */
275 saa_writeb(SAA7134_TS_DMA0
, (dev
->ts
.nr_packets
- 1) & 0xff);
276 saa_writeb(SAA7134_TS_DMA1
,
277 ((dev
->ts
.nr_packets
- 1) >> 8) & 0xff);
278 /* TSNOPIT=0, TSCOLAP=0 */
279 saa_writeb(SAA7134_TS_DMA2
,
280 (((dev
->ts
.nr_packets
- 1) >> 16) & 0x3f) | 0x00);
281 saa_writel(SAA7134_RS_PITCH(5), TS_PACKET_SIZE
);
282 saa_writel(SAA7134_RS_CONTROL(5), SAA7134_RS_CONTROL_BURST_16
|
283 SAA7134_RS_CONTROL_ME
|
284 (dev
->ts_q
.pt
.dma
>> 12));
286 /* reset hardware TS buffers */
287 saa_writeb(SAA7134_TS_SERIAL1
, 0x00);
288 saa_writeb(SAA7134_TS_SERIAL1
, 0x03);
289 saa_writeb(SAA7134_TS_SERIAL1
, 0x00);
290 saa_writeb(SAA7134_TS_SERIAL1
, 0x01);
292 /* TS clock non-inverted */
293 saa_writeb(SAA7134_TS_SERIAL1
, 0x00);
295 /* Start TS stream */
296 switch (saa7134_boards
[dev
->board
].ts_type
) {
297 case SAA7134_MPEG_TS_PARALLEL
:
298 saa_writeb(SAA7134_TS_SERIAL0
, 0x40);
299 saa_writeb(SAA7134_TS_PARALLEL
, 0xec |
300 (saa7134_boards
[dev
->board
].ts_force_val
<< 4));
302 case SAA7134_MPEG_TS_SERIAL
:
303 saa_writeb(SAA7134_TS_SERIAL0
, 0xd8);
304 saa_writeb(SAA7134_TS_PARALLEL
, 0x6c |
305 (saa7134_boards
[dev
->board
].ts_force_val
<< 4));
306 saa_writeb(SAA7134_TS_PARALLEL_SERIAL
, 0xbc);
307 saa_writeb(SAA7134_TS_SERIAL1
, 0x02);
316 int saa7134_ts_fini(struct saa7134_dev
*dev
)
318 saa7134_pgtable_free(dev
->pci
, &dev
->ts_q
.pt
);
322 void saa7134_irq_ts_done(struct saa7134_dev
*dev
, unsigned long status
)
324 enum v4l2_field field
;
326 spin_lock(&dev
->slock
);
327 if (dev
->ts_q
.curr
) {
328 field
= dev
->ts_field
;
329 if (field
!= V4L2_FIELD_TOP
) {
330 if ((status
& 0x100000) != 0x000000)
333 if ((status
& 0x100000) != 0x100000)
336 saa7134_buffer_finish(dev
, &dev
->ts_q
, VB2_BUF_STATE_DONE
);
338 saa7134_buffer_next(dev
,&dev
->ts_q
);
341 spin_unlock(&dev
->slock
);