2 * budget-core.c: driver for the SAA7146 based Budget DVB cards
4 * Compiled from various sources by Michael Hunold <michael@mihu.de>
6 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
8 * Copyright (C) 1999-2002 Ralph Metzler
9 * & Marcus Metzler for convergence integrated media GmbH
11 * 26feb2004 Support for FS Activy Card (Grundig tuner) by
12 * Michael Dreher <michael@5dot1.de>,
13 * Oliver Endriss <o.endriss@gmx.de>,
14 * Andreas 'randy' Weinberger
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
34 * the project's page is at http://www.linuxtv.org/dvb/
37 #include <linux/moduleparam.h>
40 #include "ttpci-eeprom.h"
42 #define TS_WIDTH (2 * TS_SIZE)
43 #define TS_WIDTH_ACTIVY TS_SIZE
44 #define TS_HEIGHT_MASK 0xf00
45 #define TS_HEIGHT_MASK_ACTIVY 0xc00
46 #define TS_MIN_BUFSIZE_K 188
47 #define TS_MAX_BUFSIZE_K 1410
48 #define TS_MAX_BUFSIZE_K_ACTIVY 564
49 #define BUFFER_WARNING_WAIT (30*HZ)
52 static int dma_buffer_size = TS_MIN_BUFSIZE_K;
53 module_param_named(debug, budget_debug, int, 0644);
54 module_param_named(bufsize, dma_buffer_size, int, 0444);
55 MODULE_PARM_DESC(debug, "Turn on/off budget debugging (default:off).");
56 MODULE_PARM_DESC(bufsize, "DMA buffer size in KB, default: 188, min: 188, max: 1410 (Activy: 564)");
58 /****************************************************************************
59 * TT budget / WinTV Nova
60 ****************************************************************************/
62 static int stop_ts_capture(struct budget *budget)
64 dprintk(2, "budget: %p\n", budget);
66 saa7146_write(budget->dev, MC1, MASK_20); // DMA3 off
67 SAA7146_IER_DISABLE(budget->dev, MASK_10);
71 static int start_ts_capture(struct budget *budget)
73 struct saa7146_dev *dev = budget->dev;
75 dprintk(2, "budget: %p\n", budget);
77 if (!budget->feeding || !budget->fe_synced)
80 saa7146_write(dev, MC1, MASK_20); // DMA3 off
82 memset(budget->grabbing, 0x00, budget->buffer_size);
84 saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
89 * Signal path on the Activy:
91 * tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory
93 * Since the tuner feeds 204 bytes packets into the SAA7146,
94 * DMA3 is configured to strip the trailing 16 FEC bytes:
95 * Pitch: 188, NumBytes3: 188, NumLines3: 1024
98 switch(budget->card->type) {
99 case BUDGET_FS_ACTIVY:
100 saa7146_write(dev, DD1_INIT, 0x04000000);
101 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
102 saa7146_write(dev, BRS_CTRL, 0x00000000);
105 saa7146_write(dev, DD1_INIT, 0x00000200);
106 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
107 saa7146_write(dev, BRS_CTRL, 0x60000000);
110 if (budget->video_port == BUDGET_VIDEO_PORTA) {
111 saa7146_write(dev, DD1_INIT, 0x06000200);
112 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
113 saa7146_write(dev, BRS_CTRL, 0x00000000);
115 saa7146_write(dev, DD1_INIT, 0x02000600);
116 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
117 saa7146_write(dev, BRS_CTRL, 0x60000000);
121 saa7146_write(dev, MC2, (MASK_08 | MASK_24));
124 saa7146_write(dev, BASE_ODD3, 0);
125 saa7146_write(dev, BASE_EVEN3, 0);
126 saa7146_write(dev, PROT_ADDR3, budget->buffer_size);
127 saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90);
129 saa7146_write(dev, PITCH3, budget->buffer_width);
130 saa7146_write(dev, NUM_LINE_BYTE3,
131 (budget->buffer_height << 16) | budget->buffer_width);
133 saa7146_write(dev, MC2, (MASK_04 | MASK_20));
135 SAA7146_ISR_CLEAR(budget->dev, MASK_10); /* VPE */
136 SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */
137 saa7146_write(dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */
142 static int budget_read_fe_status(struct dvb_frontend *fe, fe_status_t *status)
144 struct budget *budget = (struct budget *) fe->dvb->priv;
148 if (budget->read_fe_status)
149 ret = budget->read_fe_status(fe, status);
154 synced = (*status & FE_HAS_LOCK);
155 if (synced != budget->fe_synced) {
156 budget->fe_synced = synced;
157 spin_lock(&budget->feedlock);
159 start_ts_capture(budget);
161 stop_ts_capture(budget);
162 spin_unlock(&budget->feedlock);
168 static void vpeirq(unsigned long data)
170 struct budget *budget = (struct budget *) data;
171 u8 *mem = (u8 *) (budget->grabbing);
172 u32 olddma = budget->ttbp;
173 u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
176 /* nearest lower position divisible by 188 */
177 newdma -= newdma % 188;
179 if (newdma >= budget->buffer_size)
182 budget->ttbp = newdma;
184 if (budget->feeding == 0 || newdma == olddma)
187 if (newdma > olddma) { /* no wraparound, dump olddma..newdma */
188 count = newdma - olddma;
189 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
190 } else { /* wraparound, dump olddma..buflen and 0..newdma */
191 count = budget->buffer_size - olddma;
192 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
194 dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188);
197 if (count > budget->buffer_warning_threshold)
198 budget->buffer_warnings++;
200 if (budget->buffer_warnings && time_after(jiffies, budget->buffer_warning_time)) {
201 printk("%s %s: used %d times >80%% of buffer (%u bytes now)\n",
202 budget->dev->name, __FUNCTION__, budget->buffer_warnings, count);
203 budget->buffer_warning_time = jiffies + BUFFER_WARNING_WAIT;
204 budget->buffer_warnings = 0;
209 int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count,
210 int uselocks, int nobusyloop)
212 struct saa7146_dev *saa = budget->dev;
214 unsigned long flags = 0;
216 if (count > 4 || count <= 0)
220 spin_lock_irqsave(&budget->debilock, flags);
222 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
224 spin_unlock_irqrestore(&budget->debilock, flags);
228 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
229 saa7146_write(saa, DEBI_CONFIG, config);
230 saa7146_write(saa, DEBI_PAGE, 0);
231 saa7146_write(saa, MC2, (2 << 16) | 2);
233 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
235 spin_unlock_irqrestore(&budget->debilock, flags);
239 result = saa7146_read(saa, DEBI_AD);
240 result &= (0xffffffffUL >> ((4 - count) * 8));
243 spin_unlock_irqrestore(&budget->debilock, flags);
248 int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr,
249 int count, u32 value, int uselocks, int nobusyloop)
251 struct saa7146_dev *saa = budget->dev;
252 unsigned long flags = 0;
255 if (count > 4 || count <= 0)
259 spin_lock_irqsave(&budget->debilock, flags);
261 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
263 spin_unlock_irqrestore(&budget->debilock, flags);
267 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff));
268 saa7146_write(saa, DEBI_CONFIG, config);
269 saa7146_write(saa, DEBI_PAGE, 0);
270 saa7146_write(saa, DEBI_AD, value);
271 saa7146_write(saa, MC2, (2 << 16) | 2);
273 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
275 spin_unlock_irqrestore(&budget->debilock, flags);
280 spin_unlock_irqrestore(&budget->debilock, flags);
285 /****************************************************************************
287 ****************************************************************************/
289 static int budget_start_feed(struct dvb_demux_feed *feed)
291 struct dvb_demux *demux = feed->demux;
292 struct budget *budget = (struct budget *) demux->priv;
295 dprintk(2, "budget: %p\n", budget);
297 if (!demux->dmx.frontend)
300 spin_lock(&budget->feedlock);
301 feed->pusi_seen = 0; /* have a clean section start */
302 if (budget->feeding++ == 0)
303 status = start_ts_capture(budget);
304 spin_unlock(&budget->feedlock);
308 static int budget_stop_feed(struct dvb_demux_feed *feed)
310 struct dvb_demux *demux = feed->demux;
311 struct budget *budget = (struct budget *) demux->priv;
314 dprintk(2, "budget: %p\n", budget);
316 spin_lock(&budget->feedlock);
317 if (--budget->feeding == 0)
318 status = stop_ts_capture(budget);
319 spin_unlock(&budget->feedlock);
323 static int budget_register(struct budget *budget)
325 struct dvb_demux *dvbdemux = &budget->demux;
328 dprintk(2, "budget: %p\n", budget);
330 dvbdemux->priv = (void *) budget;
332 dvbdemux->filternum = 256;
333 dvbdemux->feednum = 256;
334 dvbdemux->start_feed = budget_start_feed;
335 dvbdemux->stop_feed = budget_stop_feed;
336 dvbdemux->write_to_decoder = NULL;
338 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
339 DMX_MEMORY_BASED_FILTERING);
341 dvb_dmx_init(&budget->demux);
343 budget->dmxdev.filternum = 256;
344 budget->dmxdev.demux = &dvbdemux->dmx;
345 budget->dmxdev.capabilities = 0;
347 dvb_dmxdev_init(&budget->dmxdev, &budget->dvb_adapter);
349 budget->hw_frontend.source = DMX_FRONTEND_0;
351 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend);
356 budget->mem_frontend.source = DMX_MEMORY_FE;
357 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend);
361 ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend);
365 dvb_net_init(&budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
370 static void budget_unregister(struct budget *budget)
372 struct dvb_demux *dvbdemux = &budget->demux;
374 dprintk(2, "budget: %p\n", budget);
376 dvb_net_release(&budget->dvb_net);
378 dvbdemux->dmx.close(&dvbdemux->dmx);
379 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
380 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
382 dvb_dmxdev_release(&budget->dmxdev);
383 dvb_dmx_release(&budget->demux);
386 int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
387 struct saa7146_pci_extension_data *info,
388 struct module *owner)
391 struct budget_info *bi = info->ext_priv;
395 memset(budget, 0, sizeof(struct budget));
397 dprintk(2, "dev: %p, budget: %p\n", dev, budget);
400 budget->dev = (struct saa7146_dev *) dev;
402 if (budget->card->type == BUDGET_FS_ACTIVY) {
403 budget->buffer_width = TS_WIDTH_ACTIVY;
404 max_bufsize = TS_MAX_BUFSIZE_K_ACTIVY;
405 height_mask = TS_HEIGHT_MASK_ACTIVY;
407 budget->buffer_width = TS_WIDTH;
408 max_bufsize = TS_MAX_BUFSIZE_K;
409 height_mask = TS_HEIGHT_MASK;
412 if (dma_buffer_size < TS_MIN_BUFSIZE_K)
413 dma_buffer_size = TS_MIN_BUFSIZE_K;
414 else if (dma_buffer_size > max_bufsize)
415 dma_buffer_size = max_bufsize;
417 budget->buffer_height = dma_buffer_size * 1024 / budget->buffer_width;
418 budget->buffer_height &= height_mask;
419 budget->buffer_size = budget->buffer_height * budget->buffer_width;
420 budget->buffer_warning_threshold = budget->buffer_size * 80/100;
421 budget->buffer_warnings = 0;
422 budget->buffer_warning_time = jiffies;
424 dprintk(2, "%s: width = %d, height = %d\n",
425 budget->dev->name, budget->buffer_width, budget->buffer_height);
426 printk("%s: dma buffer size %u\n", budget->dev->name, budget->buffer_size);
428 if ((ret = dvb_register_adapter(&budget->dvb_adapter, budget->card->name, owner, &budget->dev->pci->dev)) < 0) {
432 /* set dd1 stream a & b */
433 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
434 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
435 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
436 saa7146_write(dev, DD1_INIT, 0x02000000);
437 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
439 if (bi->type != BUDGET_FS_ACTIVY)
440 budget->video_port = BUDGET_VIDEO_PORTB;
442 budget->video_port = BUDGET_VIDEO_PORTA;
443 spin_lock_init(&budget->feedlock);
444 spin_lock_init(&budget->debilock);
446 /* the Siemens DVB needs this if you want to have the i2c chips
447 get recognized before the main driver is loaded */
448 if (bi->type != BUDGET_FS_ACTIVY)
449 saa7146_write(dev, GPIO_CTRL, 0x500000); /* GPIO 3 = 1 */
451 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
452 budget->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
454 budget->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
457 strlcpy(budget->i2c_adap.name, budget->card->name, sizeof(budget->i2c_adap.name));
459 saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120);
460 strcpy(budget->i2c_adap.name, budget->card->name);
462 if (i2c_add_adapter(&budget->i2c_adap) < 0) {
463 dvb_unregister_adapter(&budget->dvb_adapter);
467 ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac);
470 (budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, budget->buffer_size, &budget->pt))) {
475 saa7146_write(dev, PCI_BT_V1, 0x001c0000);
477 saa7146_write(dev, GPIO_CTRL, 0x000000);
479 tasklet_init(&budget->vpe_tasklet, vpeirq, (unsigned long) budget);
481 /* frontend power on */
482 if (bi->type != BUDGET_FS_ACTIVY)
483 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
485 if (budget_register(budget) == 0) {
489 i2c_del_adapter(&budget->i2c_adap);
491 vfree(budget->grabbing);
493 dvb_unregister_adapter(&budget->dvb_adapter);
498 void ttpci_budget_init_hooks(struct budget *budget)
500 if (budget->dvb_frontend && !budget->read_fe_status) {
501 budget->read_fe_status = budget->dvb_frontend->ops.read_status;
502 budget->dvb_frontend->ops.read_status = budget_read_fe_status;
506 int ttpci_budget_deinit(struct budget *budget)
508 struct saa7146_dev *dev = budget->dev;
510 dprintk(2, "budget: %p\n", budget);
512 budget_unregister(budget);
514 i2c_del_adapter(&budget->i2c_adap);
516 dvb_unregister_adapter(&budget->dvb_adapter);
518 tasklet_kill(&budget->vpe_tasklet);
520 saa7146_pgtable_free(dev->pci, &budget->pt);
522 vfree(budget->grabbing);
527 void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr)
529 struct budget *budget = (struct budget *) dev->ext_priv;
531 dprintk(8, "dev: %p, budget: %p\n", dev, budget);
534 tasklet_schedule(&budget->vpe_tasklet);
537 void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port)
539 struct budget *budget = (struct budget *) dev->ext_priv;
541 spin_lock(&budget->feedlock);
542 budget->video_port = video_port;
543 if (budget->feeding) {
544 stop_ts_capture(budget);
545 start_ts_capture(budget);
547 spin_unlock(&budget->feedlock);
550 EXPORT_SYMBOL_GPL(ttpci_budget_debiread);
551 EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite);
552 EXPORT_SYMBOL_GPL(ttpci_budget_init);
553 EXPORT_SYMBOL_GPL(ttpci_budget_init_hooks);
554 EXPORT_SYMBOL_GPL(ttpci_budget_deinit);
555 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler);
556 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port);
557 EXPORT_SYMBOL_GPL(budget_debug);
559 MODULE_LICENSE("GPL");