Pull altix-fpga-reset into release branch
[linux-2.6] / drivers / media / dvb / bt8xx / dvb-bt8xx.c
1 /*
2  * Bt8xx based DVB adapter driver
3  *
4  * Copyright (C) 2002,2003 Florian Schirmer <jolt@tuxbox.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include <linux/bitops.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/device.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30
31 #include "dmxdev.h"
32 #include "dvbdev.h"
33 #include "dvb_demux.h"
34 #include "dvb_frontend.h"
35 #include "dvb-bt8xx.h"
36 #include "bt878.h"
37
38 static int debug;
39
40 module_param(debug, int, 0644);
41 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
42
43 #define dprintk( args... ) \
44         do \
45                 if (debug) printk(KERN_DEBUG args); \
46         while (0)
47
48 #define IF_FREQUENCYx6 217    /* 6 * 36.16666666667MHz */
49
50 static void dvb_bt8xx_task(unsigned long data)
51 {
52         struct dvb_bt8xx_card *card = (struct dvb_bt8xx_card *)data;
53
54         //printk("%d ", card->bt->finished_block);
55
56         while (card->bt->last_block != card->bt->finished_block) {
57                 (card->bt->TS_Size ? dvb_dmx_swfilter_204 : dvb_dmx_swfilter)
58                         (&card->demux,
59                          &card->bt->buf_cpu[card->bt->last_block *
60                                             card->bt->block_bytes],
61                          card->bt->block_bytes);
62                 card->bt->last_block = (card->bt->last_block + 1) %
63                                         card->bt->block_count;
64         }
65 }
66
67 static int dvb_bt8xx_start_feed(struct dvb_demux_feed *dvbdmxfeed)
68 {
69         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
70         struct dvb_bt8xx_card *card = dvbdmx->priv;
71         int rc;
72
73         dprintk("dvb_bt8xx: start_feed\n");
74
75         if (!dvbdmx->dmx.frontend)
76                 return -EINVAL;
77
78         down(&card->lock);
79         card->nfeeds++;
80         rc = card->nfeeds;
81         if (card->nfeeds == 1)
82                 bt878_start(card->bt, card->gpio_mode,
83                             card->op_sync_orin, card->irq_err_ignore);
84         up(&card->lock);
85         return rc;
86 }
87
88 static int dvb_bt8xx_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
89 {
90         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
91         struct dvb_bt8xx_card *card = dvbdmx->priv;
92
93         dprintk("dvb_bt8xx: stop_feed\n");
94
95         if (!dvbdmx->dmx.frontend)
96                 return -EINVAL;
97
98         down(&card->lock);
99         card->nfeeds--;
100         if (card->nfeeds == 0)
101                 bt878_stop(card->bt);
102         up(&card->lock);
103
104         return 0;
105 }
106
107 static int is_pci_slot_eq(struct pci_dev* adev, struct pci_dev* bdev)
108 {
109         if ((adev->subsystem_vendor == bdev->subsystem_vendor) &&
110                 (adev->subsystem_device == bdev->subsystem_device) &&
111                 (adev->bus->number == bdev->bus->number) &&
112                 (PCI_SLOT(adev->devfn) == PCI_SLOT(bdev->devfn)))
113                 return 1;
114         return 0;
115 }
116
117 static struct bt878 __init *dvb_bt8xx_878_match(unsigned int bttv_nr, struct pci_dev* bttv_pci_dev)
118 {
119         unsigned int card_nr;
120
121         /* Hmm, n squared. Hope n is small */
122         for (card_nr = 0; card_nr < bt878_num; card_nr++)
123                 if (is_pci_slot_eq(bt878[card_nr].dev, bttv_pci_dev))
124                         return &bt878[card_nr];
125         return NULL;
126 }
127
128 static int thomson_dtt7579_demod_init(struct dvb_frontend* fe)
129 {
130         static u8 mt352_clock_config [] = { 0x89, 0x38, 0x38 };
131         static u8 mt352_reset [] = { 0x50, 0x80 };
132         static u8 mt352_adc_ctl_1_cfg [] = { 0x8E, 0x40 };
133         static u8 mt352_agc_cfg [] = { 0x67, 0x28, 0x20 };
134         static u8 mt352_gpp_ctl_cfg [] = { 0x8C, 0x33 };
135         static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
136
137         mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
138         udelay(2000);
139         mt352_write(fe, mt352_reset, sizeof(mt352_reset));
140         mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
141
142         mt352_write(fe, mt352_agc_cfg, sizeof(mt352_agc_cfg));
143         mt352_write(fe, mt352_gpp_ctl_cfg, sizeof(mt352_gpp_ctl_cfg));
144         mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
145
146         return 0;
147 }
148
149 static int thomson_dtt7579_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params, u8* pllbuf)
150 {
151         u32 div;
152         unsigned char bs = 0;
153         unsigned char cp = 0;
154
155         div = (((params->frequency + 83333) * 3) / 500000) + IF_FREQUENCYx6;
156
157         if (params->frequency < 542000000)
158                 cp = 0xb4;
159         else if (params->frequency < 771000000)
160                 cp = 0xbc;
161         else
162                 cp = 0xf4;
163
164         if (params->frequency == 0)
165                 bs = 0x03;
166         else if (params->frequency < 443250000)
167                 bs = 0x02;
168         else
169                 bs = 0x08;
170
171         pllbuf[0] = 0xc0; // Note: non-linux standard PLL i2c address
172         pllbuf[1] = div >> 8;
173         pllbuf[2] = div & 0xff;
174         pllbuf[3] = cp;
175         pllbuf[4] = bs;
176
177         return 0;
178 }
179
180 static struct mt352_config thomson_dtt7579_config = {
181         .demod_address = 0x0f,
182         .demod_init = thomson_dtt7579_demod_init,
183         .pll_set = thomson_dtt7579_pll_set,
184 };
185
186 static int cx24108_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
187 {
188         u32 freq = params->frequency;
189
190         int i, a, n, pump;
191         u32 band, pll;
192
193         u32 osci[]={950000,1019000,1075000,1178000,1296000,1432000,
194                 1576000,1718000,1856000,2036000,2150000};
195         u32 bandsel[]={0,0x00020000,0x00040000,0x00100800,0x00101000,
196                 0x00102000,0x00104000,0x00108000,0x00110000,
197                 0x00120000,0x00140000};
198
199         #define XTAL 1011100 /* Hz, really 1.0111 MHz and a /10 prescaler */
200         printk("cx24108 debug: entering SetTunerFreq, freq=%d\n",freq);
201
202         /* This is really the bit driving the tuner chip cx24108 */
203
204         if (freq<950000)
205                 freq = 950000; /* kHz */
206         else if (freq>2150000)
207                 freq = 2150000; /* satellite IF is 950..2150MHz */
208
209         /* decide which VCO to use for the input frequency */
210         for(i=1;(i<sizeof(osci)/sizeof(osci[0]))&&(osci[i]<freq);i++);
211         printk("cx24108 debug: select vco #%d (f=%d)\n",i,freq);
212         band=bandsel[i];
213         /* the gain values must be set by SetSymbolrate */
214         /* compute the pll divider needed, from Conexant data sheet,
215            resolved for (n*32+a), remember f(vco) is f(receive) *2 or *4,
216            depending on the divider bit. It is set to /4 on the 2 lowest
217            bands  */
218         n=((i<=2?2:1)*freq*10L)/(XTAL/100);
219         a=n%32; n/=32; if(a==0) n--;
220         pump=(freq<(osci[i-1]+osci[i])/2);
221         pll=0xf8000000|
222             ((pump?1:2)<<(14+11))|
223             ((n&0x1ff)<<(5+11))|
224             ((a&0x1f)<<11);
225         /* everything is shifted left 11 bits to left-align the bits in the
226            32bit word. Output to the tuner goes MSB-aligned, after all */
227         printk("cx24108 debug: pump=%d, n=%d, a=%d\n",pump,n,a);
228         cx24110_pll_write(fe,band);
229         /* set vga and vca to their widest-band settings, as a precaution.
230            SetSymbolrate might not be called to set this up */
231         cx24110_pll_write(fe,0x500c0000);
232         cx24110_pll_write(fe,0x83f1f800);
233         cx24110_pll_write(fe,pll);
234         //writereg(client,0x56,0x7f);
235
236         return 0;
237 }
238
239 static int pinnsat_pll_init(struct dvb_frontend* fe)
240 {
241         return 0;
242 }
243
244 static struct cx24110_config pctvsat_config = {
245         .demod_address = 0x55,
246         .pll_init = pinnsat_pll_init,
247         .pll_set = cx24108_pll_set,
248 };
249
250 static int microtune_mt7202dtf_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
251 {
252         struct dvb_bt8xx_card *card = (struct dvb_bt8xx_card *) fe->dvb->priv;
253         u8 cfg, cpump, band_select;
254         u8 data[4];
255         u32 div;
256         struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = data, .len = sizeof(data) };
257
258         div = (36000000 + params->frequency + 83333) / 166666;
259         cfg = 0x88;
260
261         if (params->frequency < 175000000)
262                 cpump = 2;
263         else if (params->frequency < 390000000)
264                 cpump = 1;
265         else if (params->frequency < 470000000)
266                 cpump = 2;
267         else if (params->frequency < 750000000)
268                 cpump = 2;
269         else
270                 cpump = 3;
271
272         if (params->frequency < 175000000)
273                 band_select = 0x0e;
274         else if (params->frequency < 470000000)
275                 band_select = 0x05;
276         else
277                 band_select = 0x03;
278
279         data[0] = (div >> 8) & 0x7f;
280         data[1] = div & 0xff;
281         data[2] = ((div >> 10) & 0x60) | cfg;
282         data[3] = cpump | band_select;
283
284         i2c_transfer(card->i2c_adapter, &msg, 1);
285         return (div * 166666 - 36000000);
286 }
287
288 static int microtune_mt7202dtf_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
289 {
290         struct dvb_bt8xx_card* bt = (struct dvb_bt8xx_card*) fe->dvb->priv;
291
292         return request_firmware(fw, name, &bt->bt->dev->dev);
293 }
294
295 static struct sp887x_config microtune_mt7202dtf_config = {
296         .demod_address = 0x70,
297         .pll_set = microtune_mt7202dtf_pll_set,
298         .request_firmware = microtune_mt7202dtf_request_firmware,
299 };
300
301 static int advbt771_samsung_tdtc9251dh0_demod_init(struct dvb_frontend* fe)
302 {
303         static u8 mt352_clock_config [] = { 0x89, 0x38, 0x2d };
304         static u8 mt352_reset [] = { 0x50, 0x80 };
305         static u8 mt352_adc_ctl_1_cfg [] = { 0x8E, 0x40 };
306         static u8 mt352_agc_cfg [] = { 0x67, 0x10, 0x23, 0x00, 0xFF, 0xFF,
307                                        0x00, 0xFF, 0x00, 0x40, 0x40 };
308         static u8 mt352_av771_extra[] = { 0xB5, 0x7A };
309         static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
310
311         mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
312         udelay(2000);
313         mt352_write(fe, mt352_reset, sizeof(mt352_reset));
314         mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
315
316         mt352_write(fe, mt352_agc_cfg,sizeof(mt352_agc_cfg));
317         udelay(2000);
318         mt352_write(fe, mt352_av771_extra,sizeof(mt352_av771_extra));
319         mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
320
321         return 0;
322 }
323
324 static int advbt771_samsung_tdtc9251dh0_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params, u8* pllbuf)
325 {
326         u32 div;
327         unsigned char bs = 0;
328         unsigned char cp = 0;
329
330         div = (((params->frequency + 83333) * 3) / 500000) + IF_FREQUENCYx6;
331
332         if (params->frequency < 150000000)
333                 cp = 0xB4;
334         else if (params->frequency < 173000000)
335                 cp = 0xBC;
336         else if (params->frequency < 250000000)
337                 cp = 0xB4;
338         else if (params->frequency < 400000000)
339                 cp = 0xBC;
340         else if (params->frequency < 420000000)
341                 cp = 0xF4;
342         else if (params->frequency < 470000000)
343                 cp = 0xFC;
344         else if (params->frequency < 600000000)
345                 cp = 0xBC;
346         else if (params->frequency < 730000000)
347                 cp = 0xF4;
348         else
349                 cp = 0xFC;
350
351         if (params->frequency < 150000000)
352                 bs = 0x01;
353         else if (params->frequency < 173000000)
354                 bs = 0x01;
355         else if (params->frequency < 250000000)
356                 bs = 0x02;
357         else if (params->frequency < 400000000)
358                 bs = 0x02;
359         else if (params->frequency < 420000000)
360                 bs = 0x02;
361         else if (params->frequency < 470000000)
362                 bs = 0x02;
363         else if (params->frequency < 600000000)
364                 bs = 0x08;
365         else if (params->frequency < 730000000)
366                 bs = 0x08;
367         else
368                 bs = 0x08;
369
370         pllbuf[0] = 0xc2; // Note: non-linux standard PLL i2c address
371         pllbuf[1] = div >> 8;
372         pllbuf[2] = div & 0xff;
373         pllbuf[3] = cp;
374         pllbuf[4] = bs;
375
376         return 0;
377 }
378
379 static struct mt352_config advbt771_samsung_tdtc9251dh0_config = {
380         .demod_address = 0x0f,
381         .demod_init = advbt771_samsung_tdtc9251dh0_demod_init,
382         .pll_set = advbt771_samsung_tdtc9251dh0_pll_set,
383 };
384
385 static struct dst_config dst_config = {
386         .demod_address = 0x55,
387 };
388
389 static int or51211_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
390 {
391         struct dvb_bt8xx_card* bt = (struct dvb_bt8xx_card*) fe->dvb->priv;
392
393         return request_firmware(fw, name, &bt->bt->dev->dev);
394 }
395
396 static void or51211_setmode(struct dvb_frontend * fe, int mode)
397 {
398         struct dvb_bt8xx_card *bt = fe->dvb->priv;
399         bttv_write_gpio(bt->bttv_nr, 0x0002, mode);   /* Reset */
400         msleep(20);
401 }
402
403 static void or51211_reset(struct dvb_frontend * fe)
404 {
405         struct dvb_bt8xx_card *bt = fe->dvb->priv;
406
407         /* RESET DEVICE
408          * reset is controled by GPIO-0
409          * when set to 0 causes reset and when to 1 for normal op
410          * must remain reset for 128 clock cycles on a 50Mhz clock
411          * also PRM1 PRM2 & PRM4 are controled by GPIO-1,GPIO-2 & GPIO-4
412          * We assume that the reset has be held low long enough or we
413          * have been reset by a power on.  When the driver is unloaded
414          * reset set to 0 so if reloaded we have been reset.
415          */
416         /* reset & PRM1,2&4 are outputs */
417         int ret = bttv_gpio_enable(bt->bttv_nr, 0x001F, 0x001F);
418         if (ret != 0)
419                 printk(KERN_WARNING "or51211: Init Error - Can't Reset DVR (%i)\n", ret);
420         bttv_write_gpio(bt->bttv_nr, 0x001F, 0x0000);   /* Reset */
421         msleep(20);
422         /* Now set for normal operation */
423         bttv_write_gpio(bt->bttv_nr, 0x0001F, 0x0001);
424         /* wait for operation to begin */
425         msleep(500);
426 }
427
428 static void or51211_sleep(struct dvb_frontend * fe)
429 {
430         struct dvb_bt8xx_card *bt = fe->dvb->priv;
431         bttv_write_gpio(bt->bttv_nr, 0x0001, 0x0000);
432 }
433
434 static struct or51211_config or51211_config = {
435         .demod_address = 0x15,
436         .request_firmware = or51211_request_firmware,
437         .setmode = or51211_setmode,
438         .reset = or51211_reset,
439         .sleep = or51211_sleep,
440 };
441
442 static int vp3021_alps_tded4_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
443 {
444         struct dvb_bt8xx_card *card = (struct dvb_bt8xx_card *) fe->dvb->priv;
445         u8 buf[4];
446         u32 div;
447         struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = buf, .len = sizeof(buf) };
448
449         div = (params->frequency + 36166667) / 166667;
450
451         buf[0] = (div >> 8) & 0x7F;
452         buf[1] = div & 0xFF;
453         buf[2] = 0x85;
454         if ((params->frequency >= 47000000) && (params->frequency < 153000000))
455                 buf[3] = 0x01;
456         else if ((params->frequency >= 153000000) && (params->frequency < 430000000))
457                 buf[3] = 0x02;
458         else if ((params->frequency >= 430000000) && (params->frequency < 824000000))
459                 buf[3] = 0x0C;
460         else if ((params->frequency >= 824000000) && (params->frequency < 863000000))
461                 buf[3] = 0x8C;
462         else
463                 return -EINVAL;
464
465         i2c_transfer(card->i2c_adapter, &msg, 1);
466         return 0;
467 }
468
469 static struct nxt6000_config vp3021_alps_tded4_config = {
470         .demod_address = 0x0a,
471         .clock_inversion = 1,
472         .pll_set = vp3021_alps_tded4_pll_set,
473 };
474
475 static int digitv_alps_tded4_demod_init(struct dvb_frontend* fe)
476 {
477         static u8 mt352_clock_config [] = { 0x89, 0x38, 0x2d };
478         static u8 mt352_reset [] = { 0x50, 0x80 };
479         static u8 mt352_adc_ctl_1_cfg [] = { 0x8E, 0x40 };
480         static u8 mt352_agc_cfg [] = { 0x67, 0x20, 0xa0 };
481         static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
482
483         mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
484         udelay(2000);
485         mt352_write(fe, mt352_reset, sizeof(mt352_reset));
486         mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
487         mt352_write(fe, mt352_agc_cfg,sizeof(mt352_agc_cfg));
488         mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
489
490         return 0;
491 }
492
493 static int digitv_alps_tded4_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params, u8* pllbuf)
494 {
495         u32 div;
496         struct dvb_ofdm_parameters *op = &params->u.ofdm;
497
498         div = (((params->frequency + 83333) * 3) / 500000) + IF_FREQUENCYx6;
499
500         pllbuf[0] = 0xc2;
501         pllbuf[1] = (div >> 8) & 0x7F;
502         pllbuf[2] = div & 0xFF;
503         pllbuf[3] = 0x85;
504
505         dprintk("frequency %u, div %u\n", params->frequency, div);
506
507         if (params->frequency < 470000000)
508                 pllbuf[4] = 0x02;
509         else if (params->frequency > 823000000)
510                 pllbuf[4] = 0x88;
511         else
512                 pllbuf[4] = 0x08;
513
514         if (op->bandwidth == 8)
515                 pllbuf[4] |= 0x04;
516
517         return 0;
518 }
519
520 static void digitv_alps_tded4_reset(struct dvb_bt8xx_card *bt)
521 {
522         /*
523          * Reset the frontend, must be called before trying
524          * to initialise the MT352 or mt352_attach
525          * will fail.
526          *
527          * Presumably not required for the NXT6000 frontend.
528          *
529          */
530
531         int ret = bttv_gpio_enable(bt->bttv_nr, 0x08, 0x08);
532         if (ret != 0)
533                 printk(KERN_WARNING "digitv_alps_tded4: Init Error - Can't Reset DVR (%i)\n", ret);
534
535         /* Pulse the reset line */
536         bttv_write_gpio(bt->bttv_nr, 0x08, 0x08); /* High */
537         bttv_write_gpio(bt->bttv_nr, 0x08, 0x00); /* Low  */
538         msleep(100);
539
540         bttv_write_gpio(bt->bttv_nr, 0x08, 0x08); /* High */
541 }
542
543 static struct mt352_config digitv_alps_tded4_config = {
544         .demod_address = 0x0a,
545         .demod_init = digitv_alps_tded4_demod_init,
546         .pll_set = digitv_alps_tded4_pll_set,
547 };
548
549 static void frontend_init(struct dvb_bt8xx_card *card, u32 type)
550 {
551         int ret;
552         struct dst_state* state = NULL;
553
554         switch(type) {
555 #ifdef BTTV_DVICO_DVBT_LITE
556         case BTTV_DVICO_DVBT_LITE:
557                 card->fe = mt352_attach(&thomson_dtt7579_config, card->i2c_adapter);
558                 if (card->fe != NULL) {
559                         card->fe->ops->info.frequency_min = 174000000;
560                         card->fe->ops->info.frequency_max = 862000000;
561                 }
562                 break;
563 #endif
564
565 #ifdef BTTV_TWINHAN_VP3021
566         case BTTV_TWINHAN_VP3021:
567 #else
568         case BTTV_NEBULA_DIGITV:
569 #endif
570                 /*
571                  * It is possible to determine the correct frontend using the I2C bus (see the Nebula SDK);
572                  * this would be a cleaner solution than trying each frontend in turn.
573                  */
574
575                 /* Old Nebula (marked (c)2003 on high profile pci card) has nxt6000 demod */
576                 card->fe = nxt6000_attach(&vp3021_alps_tded4_config, card->i2c_adapter);
577                 if (card->fe != NULL) {
578                         dprintk ("dvb_bt8xx: an nxt6000 was detected on your digitv card\n");
579                         break;
580                 }
581
582                 /* New Nebula (marked (c)2005 on low profile pci card) has mt352 demod */
583                 digitv_alps_tded4_reset(card);
584                 card->fe = mt352_attach(&digitv_alps_tded4_config, card->i2c_adapter);
585
586                 if (card->fe != NULL)
587                         dprintk ("dvb_bt8xx: an mt352 was detected on your digitv card\n");
588                 break;
589
590         case BTTV_AVDVBT_761:
591                 card->fe = sp887x_attach(&microtune_mt7202dtf_config, card->i2c_adapter);
592                 break;
593
594         case BTTV_AVDVBT_771:
595                 card->fe = mt352_attach(&advbt771_samsung_tdtc9251dh0_config, card->i2c_adapter);
596                 if (card->fe != NULL) {
597                         card->fe->ops->info.frequency_min = 174000000;
598                         card->fe->ops->info.frequency_max = 862000000;
599                 }
600                 break;
601
602         case BTTV_TWINHAN_DST:
603                 /*      DST is not a frontend driver !!!                */
604                 state = (struct dst_state *) kmalloc(sizeof (struct dst_state), GFP_KERNEL);
605                 /*      Setup the Card                                  */
606                 state->config = &dst_config;
607                 state->i2c = card->i2c_adapter;
608                 state->bt = card->bt;
609
610                 /*      DST is not a frontend, attaching the ASIC       */
611                 if ((dst_attach(state, &card->dvb_adapter)) == NULL) {
612                         printk("%s: Could not find a Twinhan DST.\n", __FUNCTION__);
613                         break;
614                 }
615                 card->fe = &state->frontend;
616
617                 /*      Attach other DST peripherals if any             */
618                 /*      Conditional Access device                       */
619                 if (state->dst_hw_cap & DST_TYPE_HAS_CA)
620                         ret = dst_ca_attach(state, &card->dvb_adapter);
621                 break;
622
623         case BTTV_PINNACLESAT:
624                 card->fe = cx24110_attach(&pctvsat_config, card->i2c_adapter);
625                 break;
626
627         case BTTV_PC_HDTV:
628                 card->fe = or51211_attach(&or51211_config, card->i2c_adapter);
629                 break;
630         }
631
632         if (card->fe == NULL)
633                 printk("dvb-bt8xx: A frontend driver was not found for device %04x/%04x subsystem %04x/%04x\n",
634                        card->bt->dev->vendor,
635                        card->bt->dev->device,
636                        card->bt->dev->subsystem_vendor,
637                        card->bt->dev->subsystem_device);
638         else
639                 if (dvb_register_frontend(&card->dvb_adapter, card->fe)) {
640                         printk("dvb-bt8xx: Frontend registration failed!\n");
641                         if (card->fe->ops->release)
642                                 card->fe->ops->release(card->fe);
643                         card->fe = NULL;
644                 }
645 }
646
647 static int __init dvb_bt8xx_load_card(struct dvb_bt8xx_card *card, u32 type)
648 {
649         int result;
650
651         if ((result = dvb_register_adapter(&card->dvb_adapter, card->card_name, THIS_MODULE)) < 0) {
652                 printk("dvb_bt8xx: dvb_register_adapter failed (errno = %d)\n", result);
653                 return result;
654         }
655         card->dvb_adapter.priv = card;
656
657         card->bt->adapter = card->i2c_adapter;
658
659         memset(&card->demux, 0, sizeof(struct dvb_demux));
660
661         card->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING;
662
663         card->demux.priv = card;
664         card->demux.filternum = 256;
665         card->demux.feednum = 256;
666         card->demux.start_feed = dvb_bt8xx_start_feed;
667         card->demux.stop_feed = dvb_bt8xx_stop_feed;
668         card->demux.write_to_decoder = NULL;
669
670         if ((result = dvb_dmx_init(&card->demux)) < 0) {
671                 printk("dvb_bt8xx: dvb_dmx_init failed (errno = %d)\n", result);
672
673                 dvb_unregister_adapter(&card->dvb_adapter);
674                 return result;
675         }
676
677         card->dmxdev.filternum = 256;
678         card->dmxdev.demux = &card->demux.dmx;
679         card->dmxdev.capabilities = 0;
680
681         if ((result = dvb_dmxdev_init(&card->dmxdev, &card->dvb_adapter)) < 0) {
682                 printk("dvb_bt8xx: dvb_dmxdev_init failed (errno = %d)\n", result);
683
684                 dvb_dmx_release(&card->demux);
685                 dvb_unregister_adapter(&card->dvb_adapter);
686                 return result;
687         }
688
689         card->fe_hw.source = DMX_FRONTEND_0;
690
691         if ((result = card->demux.dmx.add_frontend(&card->demux.dmx, &card->fe_hw)) < 0) {
692                 printk("dvb_bt8xx: dvb_dmx_init failed (errno = %d)\n", result);
693
694                 dvb_dmxdev_release(&card->dmxdev);
695                 dvb_dmx_release(&card->demux);
696                 dvb_unregister_adapter(&card->dvb_adapter);
697                 return result;
698         }
699
700         card->fe_mem.source = DMX_MEMORY_FE;
701
702         if ((result = card->demux.dmx.add_frontend(&card->demux.dmx, &card->fe_mem)) < 0) {
703                 printk("dvb_bt8xx: dvb_dmx_init failed (errno = %d)\n", result);
704
705                 card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_hw);
706                 dvb_dmxdev_release(&card->dmxdev);
707                 dvb_dmx_release(&card->demux);
708                 dvb_unregister_adapter(&card->dvb_adapter);
709                 return result;
710         }
711
712         if ((result = card->demux.dmx.connect_frontend(&card->demux.dmx, &card->fe_hw)) < 0) {
713                 printk("dvb_bt8xx: dvb_dmx_init failed (errno = %d)\n", result);
714
715                 card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_mem);
716                 card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_hw);
717                 dvb_dmxdev_release(&card->dmxdev);
718                 dvb_dmx_release(&card->demux);
719                 dvb_unregister_adapter(&card->dvb_adapter);
720                 return result;
721         }
722
723         dvb_net_init(&card->dvb_adapter, &card->dvbnet, &card->demux.dmx);
724
725         tasklet_init(&card->bt->tasklet, dvb_bt8xx_task, (unsigned long) card);
726
727         frontend_init(card, type);
728
729         return 0;
730 }
731
732 static int dvb_bt8xx_probe(struct device *dev)
733 {
734         struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
735         struct dvb_bt8xx_card *card;
736         struct pci_dev* bttv_pci_dev;
737         int ret;
738
739         if (!(card = kmalloc(sizeof(struct dvb_bt8xx_card), GFP_KERNEL)))
740                 return -ENOMEM;
741
742         memset(card, 0, sizeof(*card));
743         init_MUTEX(&card->lock);
744         card->bttv_nr = sub->core->nr;
745         strncpy(card->card_name, sub->core->name, sizeof(sub->core->name));
746         card->i2c_adapter = &sub->core->i2c_adap;
747
748         switch(sub->core->type) {
749         case BTTV_PINNACLESAT:
750                 card->gpio_mode = 0x0400c060;
751                 /* should be: BT878_A_GAIN=0,BT878_A_PWRDN,BT878_DA_DPM,BT878_DA_SBR,
752                               BT878_DA_IOM=1,BT878_DA_APP to enable serial highspeed mode. */
753                 card->op_sync_orin = 0;
754                 card->irq_err_ignore = 0;
755                 break;
756
757 #ifdef BTTV_DVICO_DVBT_LITE
758         case BTTV_DVICO_DVBT_LITE:
759 #endif
760                 card->gpio_mode = 0x0400C060;
761                 card->op_sync_orin = 0;
762                 card->irq_err_ignore = 0;
763                 /* 26, 15, 14, 6, 5
764                  * A_PWRDN  DA_DPM DA_SBR DA_IOM_DA
765                  * DA_APP(parallel) */
766                 break;
767
768 #ifdef BTTV_TWINHAN_VP3021
769         case BTTV_TWINHAN_VP3021:
770 #else
771         case BTTV_NEBULA_DIGITV:
772 #endif
773         case BTTV_AVDVBT_761:
774                 card->gpio_mode = (1 << 26) | (1 << 14) | (1 << 5);
775                 card->op_sync_orin = 0;
776                 card->irq_err_ignore = 0;
777                 /* A_PWRDN DA_SBR DA_APP (high speed serial) */
778                 break;
779
780         case BTTV_AVDVBT_771: //case 0x07711461:
781                 card->gpio_mode = 0x0400402B;
782                 card->op_sync_orin = BT878_RISC_SYNC_MASK;
783                 card->irq_err_ignore = 0;
784                 /* A_PWRDN DA_SBR  DA_APP[0] PKTP=10 RISC_ENABLE FIFO_ENABLE*/
785                 break;
786
787         case BTTV_TWINHAN_DST:
788                 card->gpio_mode = 0x2204f2c;
789                 card->op_sync_orin = BT878_RISC_SYNC_MASK;
790                 card->irq_err_ignore = BT878_APABORT | BT878_ARIPERR |
791                                        BT878_APPERR | BT878_AFBUS;
792                 /* 25,21,14,11,10,9,8,3,2 then
793                  * 0x33 = 5,4,1,0
794                  * A_SEL=SML, DA_MLB, DA_SBR,
795                  * DA_SDR=f, fifo trigger = 32 DWORDS
796                  * IOM = 0 == audio A/D
797                  * DPM = 0 == digital audio mode
798                  * == async data parallel port
799                  * then 0x33 (13 is set by start_capture)
800                  * DA_APP = async data parallel port,
801                  * ACAP_EN = 1,
802                  * RISC+FIFO ENABLE */
803                 break;
804
805         case BTTV_PC_HDTV:
806                 card->gpio_mode = 0x0100EC7B;
807                 card->op_sync_orin = 0;
808                 card->irq_err_ignore = 0;
809                 break;
810
811         default:
812                 printk(KERN_WARNING "dvb_bt8xx: Unknown bttv card type: %d.\n",
813                                 sub->core->type);
814                 kfree(card);
815                 return -ENODEV;
816         }
817
818         dprintk("dvb_bt8xx: identified card%d as %s\n", card->bttv_nr, card->card_name);
819
820         if (!(bttv_pci_dev = bttv_get_pcidev(card->bttv_nr))) {
821                 printk("dvb_bt8xx: no pci device for card %d\n", card->bttv_nr);
822                 kfree(card);
823                 return -EFAULT;
824         }
825
826         if (!(card->bt = dvb_bt8xx_878_match(card->bttv_nr, bttv_pci_dev))) {
827                 printk("dvb_bt8xx: unable to determine DMA core of card %d,\n",
828                        card->bttv_nr);
829                 printk("dvb_bt8xx: if you have the ALSA bt87x audio driver "
830                        "installed, try removing it.\n");
831
832                 kfree(card);
833                 return -EFAULT;
834         }
835
836         init_MUTEX(&card->bt->gpio_lock);
837         card->bt->bttv_nr = sub->core->nr;
838
839         if ( (ret = dvb_bt8xx_load_card(card, sub->core->type)) ) {
840                 kfree(card);
841                 return ret;
842         }
843
844         dev_set_drvdata(dev, card);
845         return 0;
846 }
847
848 static int dvb_bt8xx_remove(struct device *dev)
849 {
850         struct dvb_bt8xx_card *card = dev_get_drvdata(dev);
851
852         dprintk("dvb_bt8xx: unloading card%d\n", card->bttv_nr);
853
854         bt878_stop(card->bt);
855         tasklet_kill(&card->bt->tasklet);
856         dvb_net_release(&card->dvbnet);
857         card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_mem);
858         card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_hw);
859         dvb_dmxdev_release(&card->dmxdev);
860         dvb_dmx_release(&card->demux);
861         if (card->fe)
862                 dvb_unregister_frontend(card->fe);
863         dvb_unregister_adapter(&card->dvb_adapter);
864
865         kfree(card);
866
867         return 0;
868 }
869
870 static struct bttv_sub_driver driver = {
871         .drv = {
872                 .name           = "dvb-bt8xx",
873                 .probe          = dvb_bt8xx_probe,
874                 .remove         = dvb_bt8xx_remove,
875                 /* FIXME:
876                  * .shutdown    = dvb_bt8xx_shutdown,
877                  * .suspend     = dvb_bt8xx_suspend,
878                  * .resume      = dvb_bt8xx_resume,
879                  */
880         },
881 };
882
883 static int __init dvb_bt8xx_init(void)
884 {
885         return bttv_sub_register(&driver, "dvb");
886 }
887
888 static void __exit dvb_bt8xx_exit(void)
889 {
890         bttv_sub_unregister(&driver);
891 }
892
893 module_init(dvb_bt8xx_init);
894 module_exit(dvb_bt8xx_exit);
895
896 MODULE_DESCRIPTION("Bt8xx based DVB adapter driver");
897 MODULE_AUTHOR("Florian Schirmer <jolt@tuxbox.org>");
898 MODULE_LICENSE("GPL");