Linux-2.6.12-rc2
[linux-2.6/next.git] / drivers / media / dvb / ttpci / budget-core.c
blob93a9b40917e4302f172fde11ed012bcc72fd5dd3
1 /*
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>
39 #include "budget.h"
40 #include "ttpci-eeprom.h"
42 int budget_debug;
43 module_param_named(debug, budget_debug, int, 0644);
44 MODULE_PARM_DESC(debug, "Turn on/off budget debugging (default:off).");
46 /****************************************************************************
47 * TT budget / WinTV Nova
48 ****************************************************************************/
50 static int stop_ts_capture(struct budget *budget)
52 dprintk(2, "budget: %p\n", budget);
54 if (--budget->feeding)
55 return budget->feeding;
57 saa7146_write(budget->dev, MC1, MASK_20); // DMA3 off
58 SAA7146_IER_DISABLE(budget->dev, MASK_10);
59 return 0;
62 static int start_ts_capture(struct budget *budget)
64 struct saa7146_dev *dev = budget->dev;
66 dprintk(2, "budget: %p\n", budget);
68 if (budget->feeding)
69 return ++budget->feeding;
71 saa7146_write(dev, MC1, MASK_20); // DMA3 off
73 memset(budget->grabbing, 0x00, TS_HEIGHT * TS_WIDTH);
75 saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
77 budget->tsf = 0xff;
78 budget->ttbp = 0;
81 * Signal path on the Activy:
83 * tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory
85 * Since the tuner feeds 204 bytes packets into the SAA7146,
86 * DMA3 is configured to strip the trailing 16 FEC bytes:
87 * Pitch: 188, NumBytes3: 188, NumLines3: 1024
90 switch(budget->card->type) {
91 case BUDGET_FS_ACTIVY:
92 saa7146_write(dev, DD1_INIT, 0x04000000);
93 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
94 saa7146_write(dev, BRS_CTRL, 0x00000000);
95 break;
96 case BUDGET_PATCH:
97 saa7146_write(dev, DD1_INIT, 0x00000200);
98 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
99 saa7146_write(dev, BRS_CTRL, 0x60000000);
100 break;
101 default:
102 if (budget->video_port == BUDGET_VIDEO_PORTA) {
103 saa7146_write(dev, DD1_INIT, 0x06000200);
104 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
105 saa7146_write(dev, BRS_CTRL, 0x00000000);
106 } else {
107 saa7146_write(dev, DD1_INIT, 0x02000600);
108 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
109 saa7146_write(dev, BRS_CTRL, 0x60000000);
113 saa7146_write(dev, MC2, (MASK_08 | MASK_24));
114 mdelay(10);
116 saa7146_write(dev, BASE_ODD3, 0);
117 saa7146_write(dev, BASE_EVEN3, 0);
118 saa7146_write(dev, PROT_ADDR3, TS_WIDTH * TS_HEIGHT);
119 saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90);
121 if (budget->card->type == BUDGET_FS_ACTIVY) {
122 saa7146_write(dev, PITCH3, TS_WIDTH / 2);
123 saa7146_write(dev, NUM_LINE_BYTE3, ((TS_HEIGHT * 2) << 16) | (TS_WIDTH / 2));
124 } else {
125 saa7146_write(dev, PITCH3, TS_WIDTH);
126 saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT << 16) | TS_WIDTH);
129 saa7146_write(dev, MC2, (MASK_04 | MASK_20));
131 SAA7146_ISR_CLEAR(budget->dev, MASK_10); /* VPE */
132 SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */
133 saa7146_write(dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */
135 return ++budget->feeding;
138 static void vpeirq(unsigned long data)
140 struct budget *budget = (struct budget *) data;
141 u8 *mem = (u8 *) (budget->grabbing);
142 u32 olddma = budget->ttbp;
143 u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
145 /* nearest lower position divisible by 188 */
146 newdma -= newdma % 188;
148 if (newdma >= TS_BUFLEN)
149 return;
151 budget->ttbp = newdma;
153 if (budget->feeding == 0 || newdma == olddma)
154 return;
156 if (newdma > olddma) { /* no wraparound, dump olddma..newdma */
157 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, (newdma - olddma) / 188);
158 } else { /* wraparound, dump olddma..buflen and 0..newdma */
159 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, (TS_BUFLEN - olddma) / 188);
160 dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188);
165 int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count,
166 int uselocks, int nobusyloop)
168 struct saa7146_dev *saa = budget->dev;
169 int result = 0;
170 unsigned long flags = 0;
172 if (count > 4 || count <= 0)
173 return 0;
175 if (uselocks)
176 spin_lock_irqsave(&budget->debilock, flags);
178 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
179 if (uselocks)
180 spin_unlock_irqrestore(&budget->debilock, flags);
181 return result;
184 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
185 saa7146_write(saa, DEBI_CONFIG, config);
186 saa7146_write(saa, DEBI_PAGE, 0);
187 saa7146_write(saa, MC2, (2 << 16) | 2);
189 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
190 if (uselocks)
191 spin_unlock_irqrestore(&budget->debilock, flags);
192 return result;
195 result = saa7146_read(saa, DEBI_AD);
196 result &= (0xffffffffUL >> ((4 - count) * 8));
198 if (uselocks)
199 spin_unlock_irqrestore(&budget->debilock, flags);
201 return result;
204 int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr,
205 int count, u32 value, int uselocks, int nobusyloop)
207 struct saa7146_dev *saa = budget->dev;
208 unsigned long flags = 0;
209 int result;
211 if (count > 4 || count <= 0)
212 return 0;
214 if (uselocks)
215 spin_lock_irqsave(&budget->debilock, flags);
217 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
218 if (uselocks)
219 spin_unlock_irqrestore(&budget->debilock, flags);
220 return result;
223 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff));
224 saa7146_write(saa, DEBI_CONFIG, config);
225 saa7146_write(saa, DEBI_PAGE, 0);
226 saa7146_write(saa, DEBI_AD, value);
227 saa7146_write(saa, MC2, (2 << 16) | 2);
229 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
230 if (uselocks)
231 spin_unlock_irqrestore(&budget->debilock, flags);
232 return result;
235 if (uselocks)
236 spin_unlock_irqrestore(&budget->debilock, flags);
237 return 0;
241 /****************************************************************************
242 * DVB API SECTION
243 ****************************************************************************/
245 static int budget_start_feed(struct dvb_demux_feed *feed)
247 struct dvb_demux *demux = feed->demux;
248 struct budget *budget = (struct budget *) demux->priv;
249 int status;
251 dprintk(2, "budget: %p\n", budget);
253 if (!demux->dmx.frontend)
254 return -EINVAL;
256 spin_lock(&budget->feedlock);
257 feed->pusi_seen = 0; /* have a clean section start */
258 status = start_ts_capture(budget);
259 spin_unlock(&budget->feedlock);
260 return status;
263 static int budget_stop_feed(struct dvb_demux_feed *feed)
265 struct dvb_demux *demux = feed->demux;
266 struct budget *budget = (struct budget *) demux->priv;
267 int status;
269 dprintk(2, "budget: %p\n", budget);
271 spin_lock(&budget->feedlock);
272 status = stop_ts_capture(budget);
273 spin_unlock(&budget->feedlock);
274 return status;
277 static int budget_register(struct budget *budget)
279 struct dvb_demux *dvbdemux = &budget->demux;
280 int ret;
282 dprintk(2, "budget: %p\n", budget);
284 dvbdemux->priv = (void *) budget;
286 dvbdemux->filternum = 256;
287 dvbdemux->feednum = 256;
288 dvbdemux->start_feed = budget_start_feed;
289 dvbdemux->stop_feed = budget_stop_feed;
290 dvbdemux->write_to_decoder = NULL;
292 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
293 DMX_MEMORY_BASED_FILTERING);
295 dvb_dmx_init(&budget->demux);
297 budget->dmxdev.filternum = 256;
298 budget->dmxdev.demux = &dvbdemux->dmx;
299 budget->dmxdev.capabilities = 0;
301 dvb_dmxdev_init(&budget->dmxdev, budget->dvb_adapter);
303 budget->hw_frontend.source = DMX_FRONTEND_0;
305 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend);
307 if (ret < 0)
308 return ret;
310 budget->mem_frontend.source = DMX_MEMORY_FE;
311 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend);
312 if (ret < 0)
313 return ret;
315 ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend);
316 if (ret < 0)
317 return ret;
319 dvb_net_init(budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
321 return 0;
324 static void budget_unregister(struct budget *budget)
326 struct dvb_demux *dvbdemux = &budget->demux;
328 dprintk(2, "budget: %p\n", budget);
330 dvb_net_release(&budget->dvb_net);
332 dvbdemux->dmx.close(&dvbdemux->dmx);
333 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
334 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
336 dvb_dmxdev_release(&budget->dmxdev);
337 dvb_dmx_release(&budget->demux);
340 int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
341 struct saa7146_pci_extension_data *info,
342 struct module *owner)
344 int length = TS_WIDTH * TS_HEIGHT;
345 int ret = 0;
346 struct budget_info *bi = info->ext_priv;
348 memset(budget, 0, sizeof(struct budget));
350 dprintk(2, "dev: %p, budget: %p\n", dev, budget);
352 budget->card = bi;
353 budget->dev = (struct saa7146_dev *) dev;
355 dvb_register_adapter(&budget->dvb_adapter, budget->card->name, owner);
357 /* set dd1 stream a & b */
358 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
359 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
360 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
361 saa7146_write(dev, DD1_INIT, 0x02000000);
362 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
364 if (bi->type != BUDGET_FS_ACTIVY)
365 budget->video_port = BUDGET_VIDEO_PORTB;
366 else
367 budget->video_port = BUDGET_VIDEO_PORTA;
368 spin_lock_init(&budget->feedlock);
369 spin_lock_init(&budget->debilock);
371 /* the Siemens DVB needs this if you want to have the i2c chips
372 get recognized before the main driver is loaded */
373 if (bi->type != BUDGET_FS_ACTIVY)
374 saa7146_write(dev, GPIO_CTRL, 0x500000); /* GPIO 3 = 1 */
376 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
377 budget->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
378 #else
379 budget->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
380 #endif
382 strlcpy(budget->i2c_adap.name, budget->card->name, sizeof(budget->i2c_adap.name));
384 saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120);
385 strcpy(budget->i2c_adap.name, budget->card->name);
387 if (i2c_add_adapter(&budget->i2c_adap) < 0) {
388 dvb_unregister_adapter(budget->dvb_adapter);
389 return -ENOMEM;
392 ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter->proposed_mac);
394 if (NULL ==
395 (budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, length, &budget->pt))) {
396 ret = -ENOMEM;
397 goto err;
400 saa7146_write(dev, PCI_BT_V1, 0x001c0000);
401 /* upload all */
402 saa7146_write(dev, GPIO_CTRL, 0x000000);
404 tasklet_init(&budget->vpe_tasklet, vpeirq, (unsigned long) budget);
406 /* frontend power on */
407 if (bi->type == BUDGET_FS_ACTIVY)
408 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI);
409 else
410 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
412 if (budget_register(budget) == 0) {
413 return 0;
415 err:
416 i2c_del_adapter(&budget->i2c_adap);
418 vfree(budget->grabbing);
420 dvb_unregister_adapter(budget->dvb_adapter);
422 return ret;
425 int ttpci_budget_deinit(struct budget *budget)
427 struct saa7146_dev *dev = budget->dev;
429 dprintk(2, "budget: %p\n", budget);
431 budget_unregister(budget);
433 i2c_del_adapter(&budget->i2c_adap);
435 dvb_unregister_adapter(budget->dvb_adapter);
437 tasklet_kill(&budget->vpe_tasklet);
439 saa7146_pgtable_free(dev->pci, &budget->pt);
441 vfree(budget->grabbing);
443 return 0;
446 void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr)
448 struct budget *budget = (struct budget *) dev->ext_priv;
450 dprintk(8, "dev: %p, budget: %p\n", dev, budget);
452 if (*isr & MASK_10)
453 tasklet_schedule(&budget->vpe_tasklet);
456 void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port)
458 struct budget *budget = (struct budget *) dev->ext_priv;
460 spin_lock(&budget->feedlock);
461 budget->video_port = video_port;
462 if (budget->feeding) {
463 int oldfeeding = budget->feeding;
464 budget->feeding = 1;
465 stop_ts_capture(budget);
466 start_ts_capture(budget);
467 budget->feeding = oldfeeding;
469 spin_unlock(&budget->feedlock);
472 EXPORT_SYMBOL_GPL(ttpci_budget_debiread);
473 EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite);
474 EXPORT_SYMBOL_GPL(ttpci_budget_init);
475 EXPORT_SYMBOL_GPL(ttpci_budget_deinit);
476 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler);
477 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port);
478 EXPORT_SYMBOL_GPL(budget_debug);
480 MODULE_LICENSE("GPL");