Merge branch 'upstream'
[linux-2.6] / drivers / media / dvb / ttpci / budget-core.c
1 /*
2  * budget-core.c: driver for the SAA7146 based Budget DVB cards
3  *
4  * Compiled from various sources by Michael Hunold <michael@mihu.de>
5  *
6  * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
7  *
8  * Copyright (C) 1999-2002 Ralph  Metzler
9  *                       & Marcus Metzler for convergence integrated media GmbH
10  *
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
15  *
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.
20  *
21  *
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.
26  *
27  *
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
32  *
33  *
34  * the project's page is at http://www.linuxtv.org/dvb/
35  */
36
37 #include <linux/moduleparam.h>
38
39 #include "budget.h"
40 #include "ttpci-eeprom.h"
41
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)
50
51 int budget_debug;
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)");
57
58 /****************************************************************************
59  * TT budget / WinTV Nova
60  ****************************************************************************/
61
62 static int stop_ts_capture(struct budget *budget)
63 {
64         dprintk(2, "budget: %p\n", budget);
65
66         if (--budget->feeding)
67                 return budget->feeding;
68
69         saa7146_write(budget->dev, MC1, MASK_20);       // DMA3 off
70         SAA7146_IER_DISABLE(budget->dev, MASK_10);
71         return 0;
72 }
73
74 static int start_ts_capture(struct budget *budget)
75 {
76         struct saa7146_dev *dev = budget->dev;
77
78         dprintk(2, "budget: %p\n", budget);
79
80         if (budget->feeding)
81                 return ++budget->feeding;
82
83         saa7146_write(dev, MC1, MASK_20);       // DMA3 off
84
85         memset(budget->grabbing, 0x00, budget->buffer_size);
86
87         saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
88
89         budget->ttbp = 0;
90
91         /*
92          *  Signal path on the Activy:
93          *
94          *  tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory
95          *
96          *  Since the tuner feeds 204 bytes packets into the SAA7146,
97          *  DMA3 is configured to strip the trailing 16 FEC bytes:
98          *      Pitch: 188, NumBytes3: 188, NumLines3: 1024
99          */
100
101         switch(budget->card->type) {
102         case BUDGET_FS_ACTIVY:
103                 saa7146_write(dev, DD1_INIT, 0x04000000);
104                 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
105                 saa7146_write(dev, BRS_CTRL, 0x00000000);
106                 break;
107         case BUDGET_PATCH:
108                 saa7146_write(dev, DD1_INIT, 0x00000200);
109                 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
110                 saa7146_write(dev, BRS_CTRL, 0x60000000);
111                 break;
112         default:
113                 if (budget->video_port == BUDGET_VIDEO_PORTA) {
114                         saa7146_write(dev, DD1_INIT, 0x06000200);
115                         saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
116                         saa7146_write(dev, BRS_CTRL, 0x00000000);
117                 } else {
118                         saa7146_write(dev, DD1_INIT, 0x02000600);
119                         saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
120                         saa7146_write(dev, BRS_CTRL, 0x60000000);
121                 }
122         }
123
124         saa7146_write(dev, MC2, (MASK_08 | MASK_24));
125         mdelay(10);
126
127         saa7146_write(dev, BASE_ODD3, 0);
128         saa7146_write(dev, BASE_EVEN3, 0);
129         saa7146_write(dev, PROT_ADDR3, budget->buffer_size);
130         saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90);
131
132         saa7146_write(dev, PITCH3, budget->buffer_width);
133         saa7146_write(dev, NUM_LINE_BYTE3,
134                         (budget->buffer_height << 16) | budget->buffer_width);
135
136         saa7146_write(dev, MC2, (MASK_04 | MASK_20));
137
138         SAA7146_ISR_CLEAR(budget->dev, MASK_10);        /* VPE */
139         SAA7146_IER_ENABLE(budget->dev, MASK_10);       /* VPE */
140         saa7146_write(dev, MC1, (MASK_04 | MASK_20));   /* DMA3 on */
141
142         return ++budget->feeding;
143 }
144
145 static void vpeirq(unsigned long data)
146 {
147         struct budget *budget = (struct budget *) data;
148         u8 *mem = (u8 *) (budget->grabbing);
149         u32 olddma = budget->ttbp;
150         u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
151         u32 count;
152
153         /* nearest lower position divisible by 188 */
154         newdma -= newdma % 188;
155
156         if (newdma >= budget->buffer_size)
157                 return;
158
159         budget->ttbp = newdma;
160
161         if (budget->feeding == 0 || newdma == olddma)
162                 return;
163
164         if (newdma > olddma) {  /* no wraparound, dump olddma..newdma */
165                 count = newdma - olddma;
166                 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
167         } else {                /* wraparound, dump olddma..buflen and 0..newdma */
168                 count = budget->buffer_size - olddma;
169                 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
170                 count += newdma;
171                 dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188);
172         }
173
174         if (count > budget->buffer_warning_threshold)
175                 budget->buffer_warnings++;
176
177         if (budget->buffer_warnings && time_after(jiffies, budget->buffer_warning_time)) {
178                 printk("%s %s: used %d times >80%% of buffer (%u bytes now)\n",
179                         budget->dev->name, __FUNCTION__, budget->buffer_warnings, count);
180                 budget->buffer_warning_time = jiffies + BUFFER_WARNING_WAIT;
181                 budget->buffer_warnings = 0;
182         }
183 }
184
185
186 int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count,
187                           int uselocks, int nobusyloop)
188 {
189         struct saa7146_dev *saa = budget->dev;
190         int result = 0;
191         unsigned long flags = 0;
192
193         if (count > 4 || count <= 0)
194                 return 0;
195
196         if (uselocks)
197                 spin_lock_irqsave(&budget->debilock, flags);
198
199         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
200                 if (uselocks)
201                         spin_unlock_irqrestore(&budget->debilock, flags);
202                 return result;
203         }
204
205         saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
206         saa7146_write(saa, DEBI_CONFIG, config);
207         saa7146_write(saa, DEBI_PAGE, 0);
208         saa7146_write(saa, MC2, (2 << 16) | 2);
209
210         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
211                 if (uselocks)
212                         spin_unlock_irqrestore(&budget->debilock, flags);
213                 return result;
214         }
215
216         result = saa7146_read(saa, DEBI_AD);
217         result &= (0xffffffffUL >> ((4 - count) * 8));
218
219         if (uselocks)
220                 spin_unlock_irqrestore(&budget->debilock, flags);
221
222         return result;
223 }
224
225 int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr,
226                            int count, u32 value, int uselocks, int nobusyloop)
227 {
228         struct saa7146_dev *saa = budget->dev;
229         unsigned long flags = 0;
230         int result;
231
232         if (count > 4 || count <= 0)
233                 return 0;
234
235         if (uselocks)
236                 spin_lock_irqsave(&budget->debilock, flags);
237
238         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
239                 if (uselocks)
240                         spin_unlock_irqrestore(&budget->debilock, flags);
241                 return result;
242         }
243
244         saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff));
245         saa7146_write(saa, DEBI_CONFIG, config);
246         saa7146_write(saa, DEBI_PAGE, 0);
247         saa7146_write(saa, DEBI_AD, value);
248         saa7146_write(saa, MC2, (2 << 16) | 2);
249
250         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
251                 if (uselocks)
252                         spin_unlock_irqrestore(&budget->debilock, flags);
253                 return result;
254         }
255
256         if (uselocks)
257                 spin_unlock_irqrestore(&budget->debilock, flags);
258         return 0;
259 }
260
261
262 /****************************************************************************
263  * DVB API SECTION
264  ****************************************************************************/
265
266 static int budget_start_feed(struct dvb_demux_feed *feed)
267 {
268         struct dvb_demux *demux = feed->demux;
269         struct budget *budget = (struct budget *) demux->priv;
270         int status;
271
272         dprintk(2, "budget: %p\n", budget);
273
274         if (!demux->dmx.frontend)
275                 return -EINVAL;
276
277         spin_lock(&budget->feedlock);
278         feed->pusi_seen = 0; /* have a clean section start */
279         status = start_ts_capture(budget);
280         spin_unlock(&budget->feedlock);
281         return status;
282 }
283
284 static int budget_stop_feed(struct dvb_demux_feed *feed)
285 {
286         struct dvb_demux *demux = feed->demux;
287         struct budget *budget = (struct budget *) demux->priv;
288         int status;
289
290         dprintk(2, "budget: %p\n", budget);
291
292         spin_lock(&budget->feedlock);
293         status = stop_ts_capture(budget);
294         spin_unlock(&budget->feedlock);
295         return status;
296 }
297
298 static int budget_register(struct budget *budget)
299 {
300         struct dvb_demux *dvbdemux = &budget->demux;
301         int ret;
302
303         dprintk(2, "budget: %p\n", budget);
304
305         dvbdemux->priv = (void *) budget;
306
307         dvbdemux->filternum = 256;
308         dvbdemux->feednum = 256;
309         dvbdemux->start_feed = budget_start_feed;
310         dvbdemux->stop_feed = budget_stop_feed;
311         dvbdemux->write_to_decoder = NULL;
312
313         dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
314                                       DMX_MEMORY_BASED_FILTERING);
315
316         dvb_dmx_init(&budget->demux);
317
318         budget->dmxdev.filternum = 256;
319         budget->dmxdev.demux = &dvbdemux->dmx;
320         budget->dmxdev.capabilities = 0;
321
322         dvb_dmxdev_init(&budget->dmxdev, &budget->dvb_adapter);
323
324         budget->hw_frontend.source = DMX_FRONTEND_0;
325
326         ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend);
327
328         if (ret < 0)
329                 return ret;
330
331         budget->mem_frontend.source = DMX_MEMORY_FE;
332         ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend);
333         if (ret < 0)
334                 return ret;
335
336         ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend);
337         if (ret < 0)
338                 return ret;
339
340         dvb_net_init(&budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
341
342         return 0;
343 }
344
345 static void budget_unregister(struct budget *budget)
346 {
347         struct dvb_demux *dvbdemux = &budget->demux;
348
349         dprintk(2, "budget: %p\n", budget);
350
351         dvb_net_release(&budget->dvb_net);
352
353         dvbdemux->dmx.close(&dvbdemux->dmx);
354         dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
355         dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
356
357         dvb_dmxdev_release(&budget->dmxdev);
358         dvb_dmx_release(&budget->demux);
359 }
360
361 int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
362                       struct saa7146_pci_extension_data *info,
363                       struct module *owner)
364 {
365         int ret = 0;
366         struct budget_info *bi = info->ext_priv;
367         int max_bufsize;
368         int height_mask;
369
370         memset(budget, 0, sizeof(struct budget));
371
372         dprintk(2, "dev: %p, budget: %p\n", dev, budget);
373
374         budget->card = bi;
375         budget->dev = (struct saa7146_dev *) dev;
376
377         if (budget->card->type == BUDGET_FS_ACTIVY) {
378                 budget->buffer_width = TS_WIDTH_ACTIVY;
379                 max_bufsize = TS_MAX_BUFSIZE_K_ACTIVY;
380                 height_mask = TS_HEIGHT_MASK_ACTIVY;
381         } else {
382                 budget->buffer_width = TS_WIDTH;
383                 max_bufsize = TS_MAX_BUFSIZE_K;
384                 height_mask = TS_HEIGHT_MASK;
385         }
386
387         if (dma_buffer_size < TS_MIN_BUFSIZE_K)
388                 dma_buffer_size = TS_MIN_BUFSIZE_K;
389         else if (dma_buffer_size > max_bufsize)
390                 dma_buffer_size = max_bufsize;
391
392         budget->buffer_height = dma_buffer_size * 1024 / budget->buffer_width;
393         budget->buffer_height &= height_mask;
394         budget->buffer_size = budget->buffer_height * budget->buffer_width;
395         budget->buffer_warning_threshold = budget->buffer_size * 80/100;
396         budget->buffer_warnings = 0;
397         budget->buffer_warning_time = jiffies;
398
399         dprintk(2, "%s: width = %d, height = %d\n",
400                 budget->dev->name, budget->buffer_width, budget->buffer_height);
401         printk("%s: dma buffer size %u\n", budget->dev->name, budget->buffer_size);
402
403         dvb_register_adapter(&budget->dvb_adapter, budget->card->name, owner);
404
405         /* set dd1 stream a & b */
406         saa7146_write(dev, DD1_STREAM_B, 0x00000000);
407         saa7146_write(dev, MC2, (MASK_09 | MASK_25));
408         saa7146_write(dev, MC2, (MASK_10 | MASK_26));
409         saa7146_write(dev, DD1_INIT, 0x02000000);
410         saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
411
412         if (bi->type != BUDGET_FS_ACTIVY)
413                 budget->video_port = BUDGET_VIDEO_PORTB;
414         else
415                 budget->video_port = BUDGET_VIDEO_PORTA;
416         spin_lock_init(&budget->feedlock);
417         spin_lock_init(&budget->debilock);
418
419         /* the Siemens DVB needs this if you want to have the i2c chips
420            get recognized before the main driver is loaded */
421         if (bi->type != BUDGET_FS_ACTIVY)
422                 saa7146_write(dev, GPIO_CTRL, 0x500000);        /* GPIO 3 = 1 */
423
424 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
425         budget->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
426 #else
427         budget->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
428 #endif
429
430         strlcpy(budget->i2c_adap.name, budget->card->name, sizeof(budget->i2c_adap.name));
431
432         saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120);
433         strcpy(budget->i2c_adap.name, budget->card->name);
434
435         if (i2c_add_adapter(&budget->i2c_adap) < 0) {
436                 dvb_unregister_adapter(&budget->dvb_adapter);
437                 return -ENOMEM;
438         }
439
440         ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac);
441
442         if (NULL ==
443             (budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, budget->buffer_size, &budget->pt))) {
444                 ret = -ENOMEM;
445                 goto err;
446         }
447
448         saa7146_write(dev, PCI_BT_V1, 0x001c0000);
449         /* upload all */
450         saa7146_write(dev, GPIO_CTRL, 0x000000);
451
452         tasklet_init(&budget->vpe_tasklet, vpeirq, (unsigned long) budget);
453
454         /* frontend power on */
455         if (bi->type != BUDGET_FS_ACTIVY)
456                 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
457
458         if (budget_register(budget) == 0) {
459                 return 0;
460         }
461 err:
462         i2c_del_adapter(&budget->i2c_adap);
463
464         vfree(budget->grabbing);
465
466         dvb_unregister_adapter(&budget->dvb_adapter);
467
468         return ret;
469 }
470
471 int ttpci_budget_deinit(struct budget *budget)
472 {
473         struct saa7146_dev *dev = budget->dev;
474
475         dprintk(2, "budget: %p\n", budget);
476
477         budget_unregister(budget);
478
479         i2c_del_adapter(&budget->i2c_adap);
480
481         dvb_unregister_adapter(&budget->dvb_adapter);
482
483         tasklet_kill(&budget->vpe_tasklet);
484
485         saa7146_pgtable_free(dev->pci, &budget->pt);
486
487         vfree(budget->grabbing);
488
489         return 0;
490 }
491
492 void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr)
493 {
494         struct budget *budget = (struct budget *) dev->ext_priv;
495
496         dprintk(8, "dev: %p, budget: %p\n", dev, budget);
497
498         if (*isr & MASK_10)
499                 tasklet_schedule(&budget->vpe_tasklet);
500 }
501
502 void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port)
503 {
504         struct budget *budget = (struct budget *) dev->ext_priv;
505
506         spin_lock(&budget->feedlock);
507         budget->video_port = video_port;
508         if (budget->feeding) {
509                 int oldfeeding = budget->feeding;
510                 budget->feeding = 1;
511                 stop_ts_capture(budget);
512                 start_ts_capture(budget);
513                 budget->feeding = oldfeeding;
514         }
515         spin_unlock(&budget->feedlock);
516 }
517
518 EXPORT_SYMBOL_GPL(ttpci_budget_debiread);
519 EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite);
520 EXPORT_SYMBOL_GPL(ttpci_budget_init);
521 EXPORT_SYMBOL_GPL(ttpci_budget_deinit);
522 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler);
523 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port);
524 EXPORT_SYMBOL_GPL(budget_debug);
525
526 MODULE_LICENSE("GPL");