V4L/DVB (4806): Saa7134: add support for Hauppauge WinTV-HVR1110 DVB-T/Hybrid
[safe/jmp/linux-2.6] / drivers / media / video / saa7134 / saa7134-dvb.c
1 /*
2  *
3  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
4  *
5  *  Extended 3 / 2005 by Hartmut Hackmann to support various
6  *  cards with the tda10046 DVB-T channel decoder
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
29 #include <linux/kthread.h>
30 #include <linux/suspend.h>
31
32 #include "saa7134-reg.h"
33 #include "saa7134.h"
34 #include <media/v4l2-common.h>
35 #include "dvb-pll.h"
36
37 #include "mt352.h"
38 #include "mt352_priv.h" /* FIXME */
39 #include "tda1004x.h"
40 #include "nxt200x.h"
41
42 #include "tda10086.h"
43 #include "tda826x.h"
44 #include "isl6421.h"
45 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
46 MODULE_LICENSE("GPL");
47
48 static unsigned int antenna_pwr = 0;
49
50 module_param(antenna_pwr, int, 0444);
51 MODULE_PARM_DESC(antenna_pwr,"enable antenna power (Pinnacle 300i)");
52
53 /* ------------------------------------------------------------------ */
54 static int pinnacle_antenna_pwr(struct saa7134_dev *dev, int on)
55 {
56         u32 ok;
57
58         if (!on) {
59                 saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 26));
60                 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
61                 return 0;
62         }
63
64         saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 26));
65         saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 26));
66         udelay(10);
67
68         saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 28));
69         saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 28));
70         udelay(10);
71         saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 28));
72         udelay(10);
73         ok = saa_readl(SAA7134_GPIO_GPSTATUS0) & (1 << 27);
74         printk("%s: %s %s\n", dev->name, __FUNCTION__,
75                ok ? "on" : "off");
76
77         if (!ok)
78                 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 26));
79         return ok;
80 }
81
82 static int mt352_pinnacle_init(struct dvb_frontend* fe)
83 {
84         static u8 clock_config []  = { CLOCK_CTL,  0x3d, 0x28 };
85         static u8 reset []         = { RESET,      0x80 };
86         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
87         static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0xa0 };
88         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x31 };
89         static u8 fsm_ctl_cfg[]    = { 0x7b,       0x04 };
90         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x0f };
91         static u8 scan_ctl_cfg []  = { SCAN_CTL,   0x0d };
92         static u8 irq_cfg []       = { INTERRUPT_EN_0, 0x00, 0x00, 0x00, 0x00 };
93         struct saa7134_dev *dev= fe->dvb->priv;
94
95         printk("%s: %s called\n",dev->name,__FUNCTION__);
96
97         mt352_write(fe, clock_config,   sizeof(clock_config));
98         udelay(200);
99         mt352_write(fe, reset,          sizeof(reset));
100         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
101         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
102         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
103         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
104
105         mt352_write(fe, fsm_ctl_cfg,    sizeof(fsm_ctl_cfg));
106         mt352_write(fe, scan_ctl_cfg,   sizeof(scan_ctl_cfg));
107         mt352_write(fe, irq_cfg,        sizeof(irq_cfg));
108
109         return 0;
110 }
111
112 static int mt352_aver777_init(struct dvb_frontend* fe)
113 {
114         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x2d };
115         static u8 reset []         = { RESET,      0x80 };
116         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
117         static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0xa0 };
118         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x33 };
119
120         mt352_write(fe, clock_config,   sizeof(clock_config));
121         udelay(200);
122         mt352_write(fe, reset,          sizeof(reset));
123         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
124         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
125         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
126
127         return 0;
128 }
129
130 static int mt352_pinnacle_tuner_set_params(struct dvb_frontend* fe,
131                                            struct dvb_frontend_parameters* params)
132 {
133         u8 off[] = { 0x00, 0xf1};
134         u8 on[]  = { 0x00, 0x71};
135         struct i2c_msg msg = {.addr=0x43, .flags=0, .buf=off, .len = sizeof(off)};
136
137         struct saa7134_dev *dev = fe->dvb->priv;
138         struct v4l2_frequency f;
139
140         /* set frequency (mt2050) */
141         f.tuner     = 0;
142         f.type      = V4L2_TUNER_DIGITAL_TV;
143         f.frequency = params->frequency / 1000 * 16 / 1000;
144         if (fe->ops.i2c_gate_ctrl)
145                 fe->ops.i2c_gate_ctrl(fe, 1);
146         i2c_transfer(&dev->i2c_adap, &msg, 1);
147         saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,&f);
148         msg.buf = on;
149         if (fe->ops.i2c_gate_ctrl)
150                 fe->ops.i2c_gate_ctrl(fe, 1);
151         i2c_transfer(&dev->i2c_adap, &msg, 1);
152
153         pinnacle_antenna_pwr(dev, antenna_pwr);
154
155         /* mt352 setup */
156         return mt352_pinnacle_init(fe);
157 }
158
159 static int mt352_aver777_tuner_calc_regs(struct dvb_frontend *fe, struct dvb_frontend_parameters *params, u8* pllbuf, int buf_len)
160 {
161         if (buf_len < 5)
162                 return -EINVAL;
163
164         pllbuf[0] = 0x61;
165         dvb_pll_configure(&dvb_pll_philips_td1316, pllbuf+1,
166                           params->frequency,
167                           params->u.ofdm.bandwidth);
168         return 5;
169 }
170
171 static struct mt352_config pinnacle_300i = {
172         .demod_address = 0x3c >> 1,
173         .adc_clock     = 20333,
174         .if2           = 36150,
175         .no_tuner      = 1,
176         .demod_init    = mt352_pinnacle_init,
177 };
178
179 static struct mt352_config avermedia_777 = {
180         .demod_address = 0xf,
181         .demod_init    = mt352_aver777_init,
182 };
183
184 /* ------------------------------------------------------------------ */
185 static int philips_tda6651_pll_set(u8 addr, struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
186 {
187         struct saa7134_dev *dev = fe->dvb->priv;
188         u8 tuner_buf[4];
189         struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tuner_buf,.len =
190                         sizeof(tuner_buf) };
191         int tuner_frequency = 0;
192         u8 band, cp, filter;
193
194         /* determine charge pump */
195         tuner_frequency = params->frequency + 36166000;
196         if (tuner_frequency < 87000000)
197                 return -EINVAL;
198         else if (tuner_frequency < 130000000)
199                 cp = 3;
200         else if (tuner_frequency < 160000000)
201                 cp = 5;
202         else if (tuner_frequency < 200000000)
203                 cp = 6;
204         else if (tuner_frequency < 290000000)
205                 cp = 3;
206         else if (tuner_frequency < 420000000)
207                 cp = 5;
208         else if (tuner_frequency < 480000000)
209                 cp = 6;
210         else if (tuner_frequency < 620000000)
211                 cp = 3;
212         else if (tuner_frequency < 830000000)
213                 cp = 5;
214         else if (tuner_frequency < 895000000)
215                 cp = 7;
216         else
217                 return -EINVAL;
218
219         /* determine band */
220         if (params->frequency < 49000000)
221                 return -EINVAL;
222         else if (params->frequency < 161000000)
223                 band = 1;
224         else if (params->frequency < 444000000)
225                 band = 2;
226         else if (params->frequency < 861000000)
227                 band = 4;
228         else
229                 return -EINVAL;
230
231         /* setup PLL filter */
232         switch (params->u.ofdm.bandwidth) {
233         case BANDWIDTH_6_MHZ:
234                 filter = 0;
235                 break;
236
237         case BANDWIDTH_7_MHZ:
238                 filter = 0;
239                 break;
240
241         case BANDWIDTH_8_MHZ:
242                 filter = 1;
243                 break;
244
245         default:
246                 return -EINVAL;
247         }
248
249         /* calculate divisor
250          * ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
251          */
252         tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
253
254         /* setup tuner buffer */
255         tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
256         tuner_buf[1] = tuner_frequency & 0xff;
257         tuner_buf[2] = 0xca;
258         tuner_buf[3] = (cp << 5) | (filter << 3) | band;
259
260         if (fe->ops.i2c_gate_ctrl)
261                 fe->ops.i2c_gate_ctrl(fe, 1);
262         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
263                 return -EIO;
264         msleep(1);
265         return 0;
266 }
267
268 static int philips_tda6651_pll_init(u8 addr, struct dvb_frontend *fe)
269 {
270         struct saa7134_dev *dev = fe->dvb->priv;
271         static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
272         struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
273
274         /* setup PLL configuration */
275         if (fe->ops.i2c_gate_ctrl)
276                 fe->ops.i2c_gate_ctrl(fe, 1);
277         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
278                 return -EIO;
279         msleep(1);
280
281         return 0;
282 }
283
284 /* ------------------------------------------------------------------ */
285
286 static int philips_tu1216_tuner_60_init(struct dvb_frontend *fe)
287 {
288         return philips_tda6651_pll_init(0x60, fe);
289 }
290
291 static int philips_tu1216_tuner_60_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
292 {
293         return philips_tda6651_pll_set(0x60, fe, params);
294 }
295
296 static int philips_tda1004x_request_firmware(struct dvb_frontend *fe,
297                                            const struct firmware **fw, char *name)
298 {
299         struct saa7134_dev *dev = fe->dvb->priv;
300         return request_firmware(fw, name, &dev->pci->dev);
301 }
302
303 static struct tda1004x_config philips_tu1216_60_config = {
304
305         .demod_address = 0x8,
306         .invert        = 1,
307         .invert_oclk   = 0,
308         .xtal_freq     = TDA10046_XTAL_4M,
309         .agc_config    = TDA10046_AGC_DEFAULT,
310         .if_freq       = TDA10046_FREQ_3617,
311         .request_firmware = philips_tda1004x_request_firmware,
312 };
313
314 /* ------------------------------------------------------------------ */
315
316 static int philips_tu1216_tuner_61_init(struct dvb_frontend *fe)
317 {
318         return philips_tda6651_pll_init(0x61, fe);
319 }
320
321 static int philips_tu1216_tuner_61_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
322 {
323         return philips_tda6651_pll_set(0x61, fe, params);
324 }
325
326 static struct tda1004x_config philips_tu1216_61_config = {
327
328         .demod_address = 0x8,
329         .invert        = 1,
330         .invert_oclk   = 0,
331         .xtal_freq     = TDA10046_XTAL_4M,
332         .agc_config    = TDA10046_AGC_DEFAULT,
333         .if_freq       = TDA10046_FREQ_3617,
334         .request_firmware = philips_tda1004x_request_firmware,
335 };
336
337 /* ------------------------------------------------------------------ */
338
339 static int philips_td1316_tuner_init(struct dvb_frontend *fe)
340 {
341         struct saa7134_dev *dev = fe->dvb->priv;
342         static u8 msg[] = { 0x0b, 0xf5, 0x86, 0xab };
343         struct i2c_msg init_msg = {.addr = 0x61,.flags = 0,.buf = msg,.len = sizeof(msg) };
344
345         /* setup PLL configuration */
346         if (fe->ops.i2c_gate_ctrl)
347                 fe->ops.i2c_gate_ctrl(fe, 1);
348         if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
349                 return -EIO;
350         if (fe->ops.i2c_gate_ctrl)
351                 fe->ops.i2c_gate_ctrl(fe, 0);
352         return 0;
353 }
354
355 static int philips_td1316_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
356 {
357         return philips_tda6651_pll_set(0x61, fe, params);
358 }
359
360 static int philips_europa_tuner_init(struct dvb_frontend *fe)
361 {
362         struct saa7134_dev *dev = fe->dvb->priv;
363         static u8 msg[] = { 0x00, 0x40};
364         struct i2c_msg init_msg = {.addr = 0x43,.flags = 0,.buf = msg,.len = sizeof(msg) };
365
366
367         if (philips_td1316_tuner_init(fe))
368                 return -EIO;
369         msleep(1);
370         if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
371                 return -EIO;
372
373         return 0;
374 }
375
376 static int philips_europa_tuner_sleep(struct dvb_frontend *fe)
377 {
378         struct saa7134_dev *dev = fe->dvb->priv;
379         /* this message actually turns the tuner back to analog mode */
380         static u8 msg[] = { 0x0b, 0xdc, 0x86, 0xa4 };
381         struct i2c_msg analog_msg = {.addr = 0x61,.flags = 0,.buf = msg,.len = sizeof(msg) };
382
383         i2c_transfer(&dev->i2c_adap, &analog_msg, 1);
384         msleep(1);
385
386         /* switch the board to analog mode */
387         analog_msg.addr = 0x43;
388         analog_msg.len  = 0x02;
389         msg[0] = 0x00;
390         msg[1] = 0x14;
391         if (fe->ops.i2c_gate_ctrl)
392                 fe->ops.i2c_gate_ctrl(fe, 1);
393         i2c_transfer(&dev->i2c_adap, &analog_msg, 1);
394         return 0;
395 }
396
397 static int philips_europa_demod_sleep(struct dvb_frontend *fe)
398 {
399         struct saa7134_dev *dev = fe->dvb->priv;
400
401         if (dev->original_demod_sleep)
402                 dev->original_demod_sleep(fe);
403         fe->ops.i2c_gate_ctrl(fe, 1);
404         return 0;
405 }
406
407 static struct tda1004x_config philips_europa_config = {
408
409         .demod_address = 0x8,
410         .invert        = 0,
411         .invert_oclk   = 0,
412         .xtal_freq     = TDA10046_XTAL_4M,
413         .agc_config    = TDA10046_AGC_IFO_AUTO_POS,
414         .if_freq       = TDA10046_FREQ_052,
415         .request_firmware = NULL,
416 };
417
418 /* ------------------------------------------------------------------ */
419
420 static int philips_fmd1216_tuner_init(struct dvb_frontend *fe)
421 {
422         struct saa7134_dev *dev = fe->dvb->priv;
423         /* this message is to set up ATC and ALC */
424         static u8 fmd1216_init[] = { 0x0b, 0xdc, 0x9c, 0xa0 };
425         struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = fmd1216_init,.len = sizeof(fmd1216_init) };
426
427         if (fe->ops.i2c_gate_ctrl)
428                 fe->ops.i2c_gate_ctrl(fe, 1);
429         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
430                 return -EIO;
431         msleep(1);
432
433         return 0;
434 }
435
436 static int philips_fmd1216_tuner_sleep(struct dvb_frontend *fe)
437 {
438         struct saa7134_dev *dev = fe->dvb->priv;
439         /* this message actually turns the tuner back to analog mode */
440         static u8 fmd1216_init[] = { 0x0b, 0xdc, 0x9c, 0x60 };
441         struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = fmd1216_init,.len = sizeof(fmd1216_init) };
442
443         if (fe->ops.i2c_gate_ctrl)
444                 fe->ops.i2c_gate_ctrl(fe, 1);
445         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
446         msleep(1);
447         fmd1216_init[2] = 0x86;
448         fmd1216_init[3] = 0x54;
449         if (fe->ops.i2c_gate_ctrl)
450                 fe->ops.i2c_gate_ctrl(fe, 1);
451         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
452         msleep(1);
453         return 0;
454 }
455
456 static int philips_fmd1216_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
457 {
458         struct saa7134_dev *dev = fe->dvb->priv;
459         u8 tuner_buf[4];
460         struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = tuner_buf,.len =
461                         sizeof(tuner_buf) };
462         int tuner_frequency = 0;
463         int divider = 0;
464         u8 band, mode, cp;
465
466         /* determine charge pump */
467         tuner_frequency = params->frequency + 36130000;
468         if (tuner_frequency < 87000000)
469                 return -EINVAL;
470         /* low band */
471         else if (tuner_frequency < 180000000) {
472                 band = 1;
473                 mode = 7;
474                 cp   = 0;
475         } else if (tuner_frequency < 195000000) {
476                 band = 1;
477                 mode = 6;
478                 cp   = 1;
479         /* mid band     */
480         } else if (tuner_frequency < 366000000) {
481                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
482                         band = 10;
483                 } else {
484                         band = 2;
485                 }
486                 mode = 7;
487                 cp   = 0;
488         } else if (tuner_frequency < 478000000) {
489                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
490                         band = 10;
491                 } else {
492                         band = 2;
493                 }
494                 mode = 6;
495                 cp   = 1;
496         /* high band */
497         } else if (tuner_frequency < 662000000) {
498                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
499                         band = 12;
500                 } else {
501                         band = 4;
502                 }
503                 mode = 7;
504                 cp   = 0;
505         } else if (tuner_frequency < 840000000) {
506                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
507                         band = 12;
508                 } else {
509                         band = 4;
510                 }
511                 mode = 6;
512                 cp   = 1;
513         } else {
514                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
515                         band = 12;
516                 } else {
517                         band = 4;
518                 }
519                 mode = 7;
520                 cp   = 1;
521
522         }
523         /* calculate divisor */
524         /* ((36166000 + Finput) / 166666) rounded! */
525         divider = (tuner_frequency + 83333) / 166667;
526
527         /* setup tuner buffer */
528         tuner_buf[0] = (divider >> 8) & 0x7f;
529         tuner_buf[1] = divider & 0xff;
530         tuner_buf[2] = 0x80 | (cp << 6) | (mode  << 3) | 4;
531         tuner_buf[3] = 0x40 | band;
532
533         if (fe->ops.i2c_gate_ctrl)
534                 fe->ops.i2c_gate_ctrl(fe, 1);
535         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
536                 return -EIO;
537         return 0;
538 }
539
540 static struct tda1004x_config medion_cardbus = {
541         .demod_address = 0x08,
542         .invert        = 1,
543         .invert_oclk   = 0,
544         .xtal_freq     = TDA10046_XTAL_16M,
545         .agc_config    = TDA10046_AGC_IFO_AUTO_NEG,
546         .if_freq       = TDA10046_FREQ_3613,
547         .request_firmware = NULL,
548 };
549
550 /* ------------------------------------------------------------------ */
551
552 struct tda827x_data {
553         u32 lomax;
554         u8  spd;
555         u8  bs;
556         u8  bp;
557         u8  cp;
558         u8  gc3;
559         u8 div1p5;
560 };
561
562 static struct tda827x_data tda827x_dvbt[] = {
563         { .lomax =  62000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
564         { .lomax =  66000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
565         { .lomax =  76000000, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
566         { .lomax =  84000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
567         { .lomax =  93000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
568         { .lomax =  98000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
569         { .lomax = 109000000, .spd = 3, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
570         { .lomax = 123000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
571         { .lomax = 133000000, .spd = 2, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
572         { .lomax = 151000000, .spd = 2, .bs = 1, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
573         { .lomax = 154000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
574         { .lomax = 181000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 0, .div1p5 = 0},
575         { .lomax = 185000000, .spd = 2, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
576         { .lomax = 217000000, .spd = 2, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
577         { .lomax = 244000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
578         { .lomax = 265000000, .spd = 1, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
579         { .lomax = 302000000, .spd = 1, .bs = 1, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
580         { .lomax = 324000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
581         { .lomax = 370000000, .spd = 1, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
582         { .lomax = 454000000, .spd = 1, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
583         { .lomax = 493000000, .spd = 0, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
584         { .lomax = 530000000, .spd = 0, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
585         { .lomax = 554000000, .spd = 0, .bs = 1, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
586         { .lomax = 604000000, .spd = 0, .bs = 1, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
587         { .lomax = 696000000, .spd = 0, .bs = 2, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
588         { .lomax = 740000000, .spd = 0, .bs = 2, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
589         { .lomax = 820000000, .spd = 0, .bs = 3, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
590         { .lomax = 865000000, .spd = 0, .bs = 3, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
591         { .lomax =         0, .spd = 0, .bs = 0, .bp = 0, .cp = 0, .gc3 = 0, .div1p5 = 0}
592 };
593
594 static int philips_tda827x_tuner_init(struct dvb_frontend *fe)
595 {
596         return 0;
597 }
598
599 static int philips_tda827x_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
600 {
601         struct saa7134_dev *dev = fe->dvb->priv;
602         u8 tuner_buf[14];
603
604         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,
605                                         .len = sizeof(tuner_buf) };
606         int i, tuner_freq, if_freq;
607         u32 N;
608         switch (params->u.ofdm.bandwidth) {
609         case BANDWIDTH_6_MHZ:
610                 if_freq = 4000000;
611                 break;
612         case BANDWIDTH_7_MHZ:
613                 if_freq = 4500000;
614                 break;
615         default:                   /* 8 MHz or Auto */
616                 if_freq = 5000000;
617                 break;
618         }
619         tuner_freq = params->frequency + if_freq;
620
621         i = 0;
622         while (tda827x_dvbt[i].lomax < tuner_freq) {
623                 if(tda827x_dvbt[i + 1].lomax == 0)
624                         break;
625                 i++;
626         }
627
628         N = ((tuner_freq + 125000) / 250000) << (tda827x_dvbt[i].spd + 2);
629         tuner_buf[0] = 0;
630         tuner_buf[1] = (N>>8) | 0x40;
631         tuner_buf[2] = N & 0xff;
632         tuner_buf[3] = 0;
633         tuner_buf[4] = 0x52;
634         tuner_buf[5] = (tda827x_dvbt[i].spd << 6) + (tda827x_dvbt[i].div1p5 << 5) +
635                                    (tda827x_dvbt[i].bs << 3) + tda827x_dvbt[i].bp;
636         tuner_buf[6] = (tda827x_dvbt[i].gc3 << 4) + 0x8f;
637         tuner_buf[7] = 0xbf;
638         tuner_buf[8] = 0x2a;
639         tuner_buf[9] = 0x05;
640         tuner_buf[10] = 0xff;
641         tuner_buf[11] = 0x00;
642         tuner_buf[12] = 0x00;
643         tuner_buf[13] = 0x40;
644
645         tuner_msg.len = 14;
646         if (fe->ops.i2c_gate_ctrl)
647                 fe->ops.i2c_gate_ctrl(fe, 1);
648         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
649                 return -EIO;
650
651         msleep(500);
652         /* correct CP value */
653         tuner_buf[0] = 0x30;
654         tuner_buf[1] = 0x50 + tda827x_dvbt[i].cp;
655         tuner_msg.len = 2;
656         if (fe->ops.i2c_gate_ctrl)
657                 fe->ops.i2c_gate_ctrl(fe, 1);
658         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
659
660         return 0;
661 }
662
663 static int philips_tda827x_tuner_sleep(struct dvb_frontend *fe)
664 {
665         struct saa7134_dev *dev = fe->dvb->priv;
666         static u8 tda827x_sleep[] = { 0x30, 0xd0};
667         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tda827x_sleep,
668                                     .len = sizeof(tda827x_sleep) };
669         if (fe->ops.i2c_gate_ctrl)
670                 fe->ops.i2c_gate_ctrl(fe, 1);
671         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
672         return 0;
673 }
674
675 static struct tda1004x_config tda827x_lifeview_config = {
676         .demod_address = 0x08,
677         .invert        = 1,
678         .invert_oclk   = 0,
679         .xtal_freq     = TDA10046_XTAL_16M,
680         .agc_config    = TDA10046_AGC_TDA827X,
681         .if_freq       = TDA10046_FREQ_045,
682         .request_firmware = NULL,
683 };
684
685 /* ------------------------------------------------------------------ */
686
687 struct tda827xa_data {
688         u32 lomax;
689         u8  svco;
690         u8  spd;
691         u8  scr;
692         u8  sbs;
693         u8  gc3;
694 };
695
696 static struct tda827xa_data tda827xa_dvbt[] = {
697         { .lomax =  56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 1},
698         { .lomax =  67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
699         { .lomax =  81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
700         { .lomax =  97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
701         { .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
702         { .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
703         { .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
704         { .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
705         { .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
706         { .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
707         { .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
708         { .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
709         { .lomax = 290000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
710         { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
711         { .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
712         { .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
713         { .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
714         { .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
715         { .lomax = 550000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
716         { .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
717         { .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
718         { .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
719         { .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
720         { .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
721         { .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
722         { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
723         { .lomax =         0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}};
724
725
726 static int philips_tda827xa_pll_set(u8 addr, struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
727 {
728         struct saa7134_dev *dev = fe->dvb->priv;
729         u8 tuner_buf[14];
730         unsigned char reg2[2];
731
732         struct i2c_msg msg = {.addr = addr,.flags = 0,.buf = tuner_buf};
733         int i, tuner_freq, if_freq;
734         u32 N;
735
736         switch (params->u.ofdm.bandwidth) {
737         case BANDWIDTH_6_MHZ:
738                 if_freq = 4000000;
739                 break;
740         case BANDWIDTH_7_MHZ:
741                 if_freq = 4500000;
742                 break;
743         default:                   /* 8 MHz or Auto */
744                 if_freq = 5000000;
745                 break;
746         }
747         tuner_freq = params->frequency + if_freq;
748
749         i = 0;
750         while (tda827xa_dvbt[i].lomax < tuner_freq) {
751                 if(tda827xa_dvbt[i + 1].lomax == 0)
752                         break;
753                 i++;
754         }
755
756         N = ((tuner_freq + 31250) / 62500) << tda827xa_dvbt[i].spd;
757         tuner_buf[0] = 0;            // subaddress
758         tuner_buf[1] = N >> 8;
759         tuner_buf[2] = N & 0xff;
760         tuner_buf[3] = 0;
761         tuner_buf[4] = 0x16;
762         tuner_buf[5] = (tda827xa_dvbt[i].spd << 5) + (tda827xa_dvbt[i].svco << 3) +
763                         tda827xa_dvbt[i].sbs;
764         tuner_buf[6] = 0x4b + (tda827xa_dvbt[i].gc3 << 4);
765         tuner_buf[7] = 0x0c;
766         tuner_buf[8] = 0x06;
767         tuner_buf[9] = 0x24;
768         tuner_buf[10] = 0xff;
769         tuner_buf[11] = 0x60;
770         tuner_buf[12] = 0x00;
771         tuner_buf[13] = 0x39;  // lpsel
772         msg.len = 14;
773         if (fe->ops.i2c_gate_ctrl)
774                 fe->ops.i2c_gate_ctrl(fe, 1);
775         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
776                 return -EIO;
777
778         msg.buf= reg2;
779         msg.len = 2;
780         reg2[0] = 0x60;
781         reg2[1] = 0x3c;
782         if (fe->ops.i2c_gate_ctrl)
783                 fe->ops.i2c_gate_ctrl(fe, 1);
784         i2c_transfer(&dev->i2c_adap, &msg, 1);
785
786         reg2[0] = 0xa0;
787         reg2[1] = 0x40;
788         if (fe->ops.i2c_gate_ctrl)
789                 fe->ops.i2c_gate_ctrl(fe, 1);
790         i2c_transfer(&dev->i2c_adap, &msg, 1);
791
792         msleep(2);
793         /* correct CP value */
794         reg2[0] = 0x30;
795         reg2[1] = 0x10 + tda827xa_dvbt[i].scr;
796         msg.len = 2;
797         if (fe->ops.i2c_gate_ctrl)
798                 fe->ops.i2c_gate_ctrl(fe, 1);
799         i2c_transfer(&dev->i2c_adap, &msg, 1);
800
801         msleep(550);
802         reg2[0] = 0x50;
803         reg2[1] = 0x4f + (tda827xa_dvbt[i].gc3 << 4);
804         if (fe->ops.i2c_gate_ctrl)
805                 fe->ops.i2c_gate_ctrl(fe, 1);
806         i2c_transfer(&dev->i2c_adap, &msg, 1);
807
808         return 0;
809
810 }
811
812 static int philips_tda827xa_tuner_sleep(u8 addr, struct dvb_frontend *fe)
813 {
814         struct saa7134_dev *dev = fe->dvb->priv;
815         static u8 tda827xa_sleep[] = { 0x30, 0x90};
816         struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tda827xa_sleep,
817                                     .len = sizeof(tda827xa_sleep) };
818         if (fe->ops.i2c_gate_ctrl)
819                 fe->ops.i2c_gate_ctrl(fe, 1);
820         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
821         if (fe->ops.i2c_gate_ctrl)
822                 fe->ops.i2c_gate_ctrl(fe, 0);
823         return 0;
824 }
825
826 /* ------------------------------------------------------------------ */
827
828 static int tda8290_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
829 {
830         struct saa7134_dev *dev = fe->dvb->priv;
831         static u8 tda8290_close[] = { 0x21, 0xc0};
832         static u8 tda8290_open[]  = { 0x21, 0x80};
833         struct i2c_msg tda8290_msg = {.addr = 0x4b,.flags = 0, .len = 2};
834         if (enable) {
835                 tda8290_msg.buf = tda8290_close;
836         } else {
837                 tda8290_msg.buf = tda8290_open;
838         }
839         if (i2c_transfer(&dev->i2c_adap, &tda8290_msg, 1) != 1)
840                 return -EIO;
841         msleep(20);
842         return 0;
843 }
844
845 /* ------------------------------------------------------------------ */
846
847 static int philips_tiger_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
848 {
849         int ret;
850
851         ret = philips_tda827xa_pll_set(0x61, fe, params);
852         if (ret != 0)
853                 return ret;
854         return 0;
855 }
856
857 static int philips_tiger_tuner_init(struct dvb_frontend *fe)
858 {
859         struct saa7134_dev *dev = fe->dvb->priv;
860         static u8 data[] = { 0x3c, 0x33, 0x6a};
861         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
862
863         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
864                 return -EIO;
865         return 0;
866 }
867
868 static int philips_tiger_tuner_sleep(struct dvb_frontend *fe)
869 {
870         struct saa7134_dev *dev = fe->dvb->priv;
871         static u8 data[] = { 0x3c, 0x33, 0x68};
872         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
873
874         i2c_transfer(&dev->i2c_adap, &msg, 1);
875         philips_tda827xa_tuner_sleep( 0x61, fe);
876         return 0;
877 }
878
879 static struct tda1004x_config philips_tiger_config = {
880         .demod_address = 0x08,
881         .invert        = 1,
882         .invert_oclk   = 0,
883         .xtal_freq     = TDA10046_XTAL_16M,
884         .agc_config    = TDA10046_AGC_TDA827X,
885         .if_freq       = TDA10046_FREQ_045,
886         .request_firmware = NULL,
887 };
888
889 /* ------------------------------------------------------------------ */
890
891 static struct tda1004x_config pinnacle_pctv_310i_config = {
892         .demod_address = 0x08,
893         .invert        = 1,
894         .invert_oclk   = 0,
895         .xtal_freq     = TDA10046_XTAL_16M,
896         .agc_config    = TDA10046_AGC_TDA827X,
897         .if_freq       = TDA10046_FREQ_045,
898         .request_firmware = philips_tda1004x_request_firmware,
899 };
900
901 /* ------------------------------------------------------------------ */
902
903 static struct tda1004x_config hauppauge_hvr_1110_config = {
904         .demod_address = 0x08,
905         .invert        = 1,
906         .invert_oclk   = 0,
907         .xtal_freq     = TDA10046_XTAL_16M,
908         .agc_config    = TDA10046_AGC_TDA827X,
909         .if_freq       = TDA10046_FREQ_045,
910         .request_firmware = philips_tda1004x_request_firmware,
911 };
912
913 /* ------------------------------------------------------------------ */
914
915 static struct tda1004x_config asus_p7131_dual_config = {
916         .demod_address = 0x08,
917         .invert        = 1,
918         .invert_oclk   = 0,
919         .xtal_freq     = TDA10046_XTAL_16M,
920         .agc_config    = TDA10046_AGC_TDA827X,
921         .if_freq       = TDA10046_FREQ_045,
922         .request_firmware = philips_tda1004x_request_firmware,
923 };
924
925 static int asus_p7131_dual_tuner_init(struct dvb_frontend *fe)
926 {
927         struct saa7134_dev *dev = fe->dvb->priv;
928         static u8 data[] = { 0x3c, 0x33, 0x6a};
929         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
930
931         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
932                 return -EIO;
933         /* make sure the DVB-T antenna input is set */
934         saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x0200000);
935         return 0;
936 }
937
938 static int asus_p7131_dual_tuner_sleep(struct dvb_frontend *fe)
939 {
940         struct saa7134_dev *dev = fe->dvb->priv;
941         static u8 data[] = { 0x3c, 0x33, 0x68};
942         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
943
944         i2c_transfer(&dev->i2c_adap, &msg, 1);
945         philips_tda827xa_tuner_sleep( 0x61, fe);
946         /* reset antenna inputs for analog usage */
947         saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x0200000);
948         return 0;
949 }
950
951 /* ------------------------------------------------------------------ */
952
953 static int lifeview_trio_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
954 {
955         int ret;
956
957         ret = philips_tda827xa_pll_set(0x60, fe, params);
958         return ret;
959 }
960
961 static int lifeview_trio_tuner_sleep(struct dvb_frontend *fe)
962 {
963         philips_tda827xa_tuner_sleep(0x60, fe);
964         return 0;
965 }
966
967 static struct tda1004x_config lifeview_trio_config = {
968         .demod_address = 0x09,
969         .invert        = 1,
970         .invert_oclk   = 0,
971         .xtal_freq     = TDA10046_XTAL_16M,
972         .agc_config    = TDA10046_AGC_TDA827X_GPL,
973         .if_freq       = TDA10046_FREQ_045,
974         .request_firmware = NULL,
975 };
976
977 /* ------------------------------------------------------------------ */
978
979 static int ads_duo_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
980 {
981         int ret;
982
983         ret = philips_tda827xa_pll_set(0x61, fe, params);
984         return ret;
985 }
986
987 static int ads_duo_tuner_init(struct dvb_frontend *fe)
988 {
989         struct saa7134_dev *dev = fe->dvb->priv;
990         /* route TDA8275a AGC input to the channel decoder */
991         saa_writeb(SAA7134_GPIO_GPSTATUS2, 0x60);
992         return 0;
993 }
994
995 static int ads_duo_tuner_sleep(struct dvb_frontend *fe)
996 {
997         struct saa7134_dev *dev = fe->dvb->priv;
998         /* route TDA8275a AGC input to the analog IF chip*/
999         saa_writeb(SAA7134_GPIO_GPSTATUS2, 0x20);
1000         philips_tda827xa_tuner_sleep( 0x61, fe);
1001         return 0;
1002 }
1003
1004 static struct tda1004x_config ads_tech_duo_config = {
1005         .demod_address = 0x08,
1006         .invert        = 1,
1007         .invert_oclk   = 0,
1008         .xtal_freq     = TDA10046_XTAL_16M,
1009         .agc_config    = TDA10046_AGC_TDA827X_GPL,
1010         .if_freq       = TDA10046_FREQ_045,
1011         .request_firmware = NULL,
1012 };
1013
1014 /* ------------------------------------------------------------------ */
1015
1016 static int tevion_dvb220rf_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1017 {
1018         int ret;
1019         ret = philips_tda827xa_pll_set(0x60, fe, params);
1020         return ret;
1021 }
1022
1023 static int tevion_dvb220rf_tuner_sleep(struct dvb_frontend *fe)
1024 {
1025         philips_tda827xa_tuner_sleep( 0x61, fe);
1026         return 0;
1027 }
1028
1029 static struct tda1004x_config tevion_dvbt220rf_config = {
1030         .demod_address = 0x08,
1031         .invert        = 1,
1032         .invert_oclk   = 0,
1033         .xtal_freq     = TDA10046_XTAL_16M,
1034         .agc_config    = TDA10046_AGC_TDA827X,
1035         .if_freq       = TDA10046_FREQ_045,
1036         .request_firmware = NULL,
1037 };
1038
1039 /* ------------------------------------------------------------------ */
1040
1041 static int md8800_dvbt_analog_mode(struct dvb_frontend *fe)
1042 {
1043         struct saa7134_dev *dev = fe->dvb->priv;
1044         static u8 data[] = { 0x3c, 0x33, 0x68};
1045         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
1046
1047         i2c_transfer(&dev->i2c_adap, &msg, 1);
1048         philips_tda827xa_tuner_sleep( 0x61, fe);
1049         return 0;
1050 }
1051
1052 static int md8800_dvbt_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1053 {
1054         int ret;
1055         struct saa7134_dev *dev = fe->dvb->priv;
1056         static u8 tda8290_close[] = { 0x21, 0xc0};
1057         static u8 tda8290_open[]  = { 0x21, 0x80};
1058         struct i2c_msg tda8290_msg = {.addr = 0x4b,.flags = 0, .len = 2};
1059         /* close tda8290 i2c bridge */
1060         tda8290_msg.buf = tda8290_close;
1061         ret = i2c_transfer(&dev->i2c_adap, &tda8290_msg, 1);
1062         if (ret != 1)
1063                 return -EIO;
1064         msleep(20);
1065         ret = philips_tda827xa_pll_set(0x60, fe, params);
1066         if (ret != 0)
1067                 return ret;
1068         /* open tda8290 i2c bridge */
1069         tda8290_msg.buf = tda8290_open;
1070         i2c_transfer(&dev->i2c_adap, &tda8290_msg, 1);
1071         return ret;
1072 }
1073
1074 static struct tda1004x_config md8800_dvbt_config = {
1075         .demod_address = 0x08,
1076         .invert        = 1,
1077         .invert_oclk   = 0,
1078         .xtal_freq     = TDA10046_XTAL_16M,
1079         .agc_config    = TDA10046_AGC_TDA827X,
1080         .if_freq       = TDA10046_FREQ_045,
1081         .request_firmware = NULL,
1082 };
1083
1084 static struct tda10086_config flydvbs = {
1085         .demod_address = 0x0e,
1086         .invert = 0,
1087 };
1088
1089 /* ------------------------------------------------------------------ */
1090
1091 static struct nxt200x_config avertvhda180 = {
1092         .demod_address    = 0x0a,
1093 };
1094
1095 static int nxt200x_set_pll_input(u8 *buf, int input)
1096 {
1097         if (input)
1098                 buf[3] |= 0x08;
1099         else
1100                 buf[3] &= ~0x08;
1101         return 0;
1102 }
1103
1104 static struct nxt200x_config kworldatsc110 = {
1105         .demod_address    = 0x0a,
1106         .set_pll_input    = nxt200x_set_pll_input,
1107 };
1108
1109 /* ------------------------------------------------------------------ */
1110
1111 static int dvb_init(struct saa7134_dev *dev)
1112 {
1113         /* init struct videobuf_dvb */
1114         dev->ts.nr_bufs    = 32;
1115         dev->ts.nr_packets = 32*4;
1116         dev->dvb.name = dev->name;
1117         videobuf_queue_init(&dev->dvb.dvbq, &saa7134_ts_qops,
1118                             dev->pci, &dev->slock,
1119                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
1120                             V4L2_FIELD_ALTERNATE,
1121                             sizeof(struct saa7134_buf),
1122                             dev);
1123
1124         switch (dev->board) {
1125         case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1126                 printk("%s: pinnacle 300i dvb setup\n",dev->name);
1127                 dev->dvb.frontend = dvb_attach(mt352_attach, &pinnacle_300i,
1128                                                &dev->i2c_adap);
1129                 if (dev->dvb.frontend) {
1130                         dev->dvb.frontend->ops.tuner_ops.set_params = mt352_pinnacle_tuner_set_params;
1131                 }
1132                 break;
1133         case SAA7134_BOARD_AVERMEDIA_777:
1134         case SAA7134_BOARD_AVERMEDIA_A16AR:
1135                 printk("%s: avertv 777 dvb setup\n",dev->name);
1136                 dev->dvb.frontend = dvb_attach(mt352_attach, &avermedia_777,
1137                                                &dev->i2c_adap);
1138                 if (dev->dvb.frontend) {
1139                         dev->dvb.frontend->ops.tuner_ops.calc_regs = mt352_aver777_tuner_calc_regs;
1140                 }
1141                 break;
1142         case SAA7134_BOARD_MD7134:
1143                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1144                                                &medion_cardbus,
1145                                                &dev->i2c_adap);
1146                 if (dev->dvb.frontend) {
1147                         dev->dvb.frontend->ops.tuner_ops.init = philips_fmd1216_tuner_init;
1148                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_fmd1216_tuner_sleep;
1149                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_fmd1216_tuner_set_params;
1150                 }
1151                 break;
1152         case SAA7134_BOARD_PHILIPS_TOUGH:
1153                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1154                                                &philips_tu1216_60_config,
1155                                                &dev->i2c_adap);
1156                 if (dev->dvb.frontend) {
1157                         dev->dvb.frontend->ops.tuner_ops.init = philips_tu1216_tuner_60_init;
1158                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tu1216_tuner_60_set_params;
1159                 }
1160                 break;
1161         case SAA7134_BOARD_FLYDVBTDUO:
1162                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1163                                                &tda827x_lifeview_config,
1164                                                &dev->i2c_adap);
1165                 if (dev->dvb.frontend) {
1166                         dev->dvb.frontend->ops.tuner_ops.init = philips_tda827x_tuner_init;
1167                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tda827x_tuner_sleep;
1168                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tda827x_tuner_set_params;
1169                 }
1170                 break;
1171         case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
1172                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1173                                                &tda827x_lifeview_config,
1174                                                &dev->i2c_adap);
1175                 if (dev->dvb.frontend) {
1176                         dev->dvb.frontend->ops.tuner_ops.init = philips_tda827x_tuner_init;
1177                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tda827x_tuner_sleep;
1178                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tda827x_tuner_set_params;
1179                 }
1180                 break;
1181         case SAA7134_BOARD_PHILIPS_EUROPA:
1182                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1183                                                &philips_europa_config,
1184                                                &dev->i2c_adap);
1185                 if (dev->dvb.frontend) {
1186                         dev->original_demod_sleep = dev->dvb.frontend->ops.sleep;
1187                         dev->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1188                         dev->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1189                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1190                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1191                 }
1192                 break;
1193         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
1194                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1195                                                &philips_europa_config,
1196                                                &dev->i2c_adap);
1197                 if (dev->dvb.frontend) {
1198                         dev->original_demod_sleep = dev->dvb.frontend->ops.sleep;
1199                         dev->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1200                         dev->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1201                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1202                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1203                 }
1204                 break;
1205         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
1206                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1207                                                &philips_tu1216_61_config,
1208                                                &dev->i2c_adap);
1209                 if (dev->dvb.frontend) {
1210                         dev->dvb.frontend->ops.tuner_ops.init = philips_tu1216_tuner_61_init;
1211                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tu1216_tuner_61_set_params;
1212                 }
1213                 break;
1214         case SAA7134_BOARD_PHILIPS_TIGER:
1215                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1216                                                &philips_tiger_config,
1217                                                &dev->i2c_adap);
1218                 if (dev->dvb.frontend) {
1219                         dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
1220                         dev->dvb.frontend->ops.tuner_ops.init = philips_tiger_tuner_init;
1221                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tiger_tuner_sleep;
1222                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1223                 }
1224                 break;
1225         case SAA7134_BOARD_PINNACLE_PCTV_310i:
1226                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1227                                                &pinnacle_pctv_310i_config,
1228                                                &dev->i2c_adap);
1229                 if (dev->dvb.frontend) {
1230                         dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
1231                         dev->dvb.frontend->ops.tuner_ops.init = philips_tiger_tuner_init;
1232                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tiger_tuner_sleep;
1233                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1234                 }
1235                 break;
1236         case SAA7134_BOARD_HAUPPAUGE_HVR1110:
1237                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1238                                                &hauppauge_hvr_1110_config,
1239                                                &dev->i2c_adap);
1240                 if (dev->dvb.frontend) {
1241                         dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
1242                         dev->dvb.frontend->ops.tuner_ops.init = philips_tiger_tuner_init;
1243                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tiger_tuner_sleep;
1244                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1245                 }
1246                 break;
1247         case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
1248                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1249                                                &asus_p7131_dual_config,
1250                                                &dev->i2c_adap);
1251                 if (dev->dvb.frontend) {
1252                         dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
1253                         dev->dvb.frontend->ops.tuner_ops.init = asus_p7131_dual_tuner_init;
1254                         dev->dvb.frontend->ops.tuner_ops.sleep = asus_p7131_dual_tuner_sleep;
1255                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1256                 }
1257                 break;
1258         case SAA7134_BOARD_FLYDVBT_LR301:
1259                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1260                                                &tda827x_lifeview_config,
1261                                                &dev->i2c_adap);
1262                 if (dev->dvb.frontend) {
1263                         dev->dvb.frontend->ops.tuner_ops.init = philips_tda827x_tuner_init;
1264                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tda827x_tuner_sleep;
1265                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tda827x_tuner_set_params;
1266                 }
1267                 break;
1268         case SAA7134_BOARD_FLYDVB_TRIO:
1269                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1270                                                &lifeview_trio_config,
1271                                                &dev->i2c_adap);
1272                 if (dev->dvb.frontend) {
1273                         dev->dvb.frontend->ops.tuner_ops.sleep = lifeview_trio_tuner_sleep;
1274                         dev->dvb.frontend->ops.tuner_ops.set_params = lifeview_trio_tuner_set_params;
1275                 }
1276                 break;
1277         case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
1278                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1279                                                &ads_tech_duo_config,
1280                                                &dev->i2c_adap);
1281                 if (dev->dvb.frontend) {
1282                         dev->dvb.frontend->ops.tuner_ops.init = ads_duo_tuner_init;
1283                         dev->dvb.frontend->ops.tuner_ops.sleep = ads_duo_tuner_sleep;
1284                         dev->dvb.frontend->ops.tuner_ops.set_params = ads_duo_tuner_set_params;
1285                 }
1286                 break;
1287         case SAA7134_BOARD_TEVION_DVBT_220RF:
1288                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1289                                                &tevion_dvbt220rf_config,
1290                                                &dev->i2c_adap);
1291                 if (dev->dvb.frontend) {
1292                         dev->dvb.frontend->ops.tuner_ops.sleep = tevion_dvb220rf_tuner_sleep;
1293                         dev->dvb.frontend->ops.tuner_ops.set_params = tevion_dvb220rf_tuner_set_params;
1294                 }
1295                 break;
1296         case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
1297                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1298                                                &ads_tech_duo_config,
1299                                                &dev->i2c_adap);
1300                 if (dev->dvb.frontend) {
1301                         dev->dvb.frontend->ops.tuner_ops.init = ads_duo_tuner_init;
1302                         dev->dvb.frontend->ops.tuner_ops.sleep = ads_duo_tuner_sleep;
1303                         dev->dvb.frontend->ops.tuner_ops.set_params = ads_duo_tuner_set_params;
1304                 }
1305                 break;
1306         case SAA7134_BOARD_MEDION_MD8800_QUADRO:
1307                 dev->dvb.frontend = tda10046_attach(&md8800_dvbt_config,
1308                                                     &dev->i2c_adap);
1309                 if (dev->dvb.frontend) {
1310                         dev->dvb.frontend->ops.tuner_ops.init = philips_tiger_tuner_init;
1311                         dev->dvb.frontend->ops.tuner_ops.sleep = md8800_dvbt_analog_mode;
1312                         dev->dvb.frontend->ops.tuner_ops.set_params = md8800_dvbt_pll_set;
1313                 }
1314                 break;
1315         case SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180:
1316                 dev->dvb.frontend = dvb_attach(nxt200x_attach, &avertvhda180,
1317                                                &dev->i2c_adap);
1318                 if (dev->dvb.frontend) {
1319                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
1320                                    NULL, &dvb_pll_tdhu2);
1321                 }
1322                 break;
1323         case SAA7134_BOARD_KWORLD_ATSC110:
1324                 dev->dvb.frontend = dvb_attach(nxt200x_attach, &kworldatsc110,
1325                                                &dev->i2c_adap);
1326                 if (dev->dvb.frontend) {
1327                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
1328                                    NULL, &dvb_pll_tuv1236d);
1329                 }
1330                 break;
1331         case SAA7134_BOARD_FLYDVBS_LR300:
1332                 dev->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1333                                                &dev->i2c_adap);
1334                 if (dev->dvb.frontend) {
1335                         if (dvb_attach(tda826x_attach, dev->dvb.frontend, 0x60,
1336                                        &dev->i2c_adap, 0) == NULL) {
1337                                 printk("%s: No tda826x found!\n", __FUNCTION__);
1338                         }
1339                         if (dvb_attach(isl6421_attach, dev->dvb.frontend,
1340                                        &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1341                                 printk("%s: No ISL6421 found!\n", __FUNCTION__);
1342                         }
1343                 }
1344                 break;
1345         case SAA7134_BOARD_ASUS_EUROPA2_HYBRID:
1346                 dev->dvb.frontend = tda10046_attach(&medion_cardbus,
1347                                                     &dev->i2c_adap);
1348                 if (dev->dvb.frontend) {
1349                         dev->original_demod_sleep = dev->dvb.frontend->ops.sleep;
1350                         dev->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1351                         dev->dvb.frontend->ops.tuner_ops.init = philips_fmd1216_tuner_init;
1352                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_fmd1216_tuner_sleep;
1353                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_fmd1216_tuner_set_params;
1354                 }
1355                 break;
1356         case SAA7134_BOARD_VIDEOMATE_DVBT_200A:
1357                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1358                                 &philips_europa_config,
1359                                 &dev->i2c_adap);
1360                 if (dev->dvb.frontend) {
1361                         dev->dvb.frontend->ops.tuner_ops.init = philips_td1316_tuner_init;
1362                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1363                 }
1364                 break;
1365         default:
1366                 printk("%s: Huh? unknown DVB card?\n",dev->name);
1367                 break;
1368         }
1369
1370         if (NULL == dev->dvb.frontend) {
1371                 printk("%s: frontend initialization failed\n",dev->name);
1372                 return -1;
1373         }
1374
1375         /* register everything else */
1376         return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev, &dev->pci->dev);
1377 }
1378
1379 static int dvb_fini(struct saa7134_dev *dev)
1380 {
1381         static int on  = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
1382
1383         switch (dev->board) {
1384         case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1385                 /* otherwise we don't detect the tuner on next insmod */
1386                 saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&on);
1387                 break;
1388         };
1389         videobuf_dvb_unregister(&dev->dvb);
1390         return 0;
1391 }
1392
1393 static struct saa7134_mpeg_ops dvb_ops = {
1394         .type          = SAA7134_MPEG_DVB,
1395         .init          = dvb_init,
1396         .fini          = dvb_fini,
1397 };
1398
1399 static int __init dvb_register(void)
1400 {
1401         return saa7134_ts_register(&dvb_ops);
1402 }
1403
1404 static void __exit dvb_unregister(void)
1405 {
1406         saa7134_ts_unregister(&dvb_ops);
1407 }
1408
1409 module_init(dvb_register);
1410 module_exit(dvb_unregister);
1411
1412 /* ------------------------------------------------------------------ */
1413 /*
1414  * Local variables:
1415  * c-basic-offset: 8
1416  * End:
1417  */