V4L/DVB (4835): Added support for the Terratec Cinergy HT PCMCIA module
[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_GP11,
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_GP11,
885         .if_freq       = TDA10046_FREQ_045,
886         .request_firmware = NULL,
887 };
888 /* ------------------------------------------------------------------ */
889
890 static int cinergy_ht_tuner_init(struct dvb_frontend *fe)
891 {
892         struct saa7134_dev *dev = fe->dvb->priv;
893         static u8 data[] = { 0x3c, 0x33, 0x62};
894         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
895
896         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
897                 return -EIO;
898         return 0;
899 }
900
901 static int cinergy_ht_tuner_sleep(struct dvb_frontend *fe)
902 {
903         struct saa7134_dev *dev = fe->dvb->priv;
904         static u8 data[] = { 0x3c, 0x33, 0x60};
905         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
906
907         i2c_transfer(&dev->i2c_adap, &msg, 1);
908         philips_tda827xa_tuner_sleep( 0x61, fe);
909         return 0;
910 }
911
912 static struct tda1004x_config cinergy_ht_config = {
913         .demod_address = 0x08,
914         .invert        = 1,
915         .invert_oclk   = 0,
916         .xtal_freq     = TDA10046_XTAL_16M,
917         .agc_config    = TDA10046_AGC_TDA827X_GP01,
918         .if_freq       = TDA10046_FREQ_045,
919         .request_firmware = NULL,
920 };
921
922 /* ------------------------------------------------------------------ */
923
924 static struct tda1004x_config pinnacle_pctv_310i_config = {
925         .demod_address = 0x08,
926         .invert        = 1,
927         .invert_oclk   = 0,
928         .xtal_freq     = TDA10046_XTAL_16M,
929         .agc_config    = TDA10046_AGC_TDA827X_GP11,
930         .if_freq       = TDA10046_FREQ_045,
931         .request_firmware = philips_tda1004x_request_firmware,
932 };
933
934 /* ------------------------------------------------------------------ */
935
936 static struct tda1004x_config hauppauge_hvr_1110_config = {
937         .demod_address = 0x08,
938         .invert        = 1,
939         .invert_oclk   = 0,
940         .xtal_freq     = TDA10046_XTAL_16M,
941         .agc_config    = TDA10046_AGC_TDA827X_GP11,
942         .if_freq       = TDA10046_FREQ_045,
943         .request_firmware = philips_tda1004x_request_firmware,
944 };
945
946 /* ------------------------------------------------------------------ */
947
948 static struct tda1004x_config asus_p7131_dual_config = {
949         .demod_address = 0x08,
950         .invert        = 1,
951         .invert_oclk   = 0,
952         .xtal_freq     = TDA10046_XTAL_16M,
953         .agc_config    = TDA10046_AGC_TDA827X_GP11,
954         .if_freq       = TDA10046_FREQ_045,
955         .request_firmware = philips_tda1004x_request_firmware,
956 };
957
958 static int asus_p7131_dual_tuner_init(struct dvb_frontend *fe)
959 {
960         struct saa7134_dev *dev = fe->dvb->priv;
961         static u8 data[] = { 0x3c, 0x33, 0x6a};
962         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
963
964         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
965                 return -EIO;
966         /* make sure the DVB-T antenna input is set */
967         saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x0200000);
968         return 0;
969 }
970
971 static int asus_p7131_dual_tuner_sleep(struct dvb_frontend *fe)
972 {
973         struct saa7134_dev *dev = fe->dvb->priv;
974         static u8 data[] = { 0x3c, 0x33, 0x68};
975         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
976
977         i2c_transfer(&dev->i2c_adap, &msg, 1);
978         philips_tda827xa_tuner_sleep( 0x61, fe);
979         /* reset antenna inputs for analog usage */
980         saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x0200000);
981         return 0;
982 }
983
984 /* ------------------------------------------------------------------ */
985
986 static int lifeview_trio_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
987 {
988         int ret;
989
990         ret = philips_tda827xa_pll_set(0x60, fe, params);
991         return ret;
992 }
993
994 static int lifeview_trio_tuner_sleep(struct dvb_frontend *fe)
995 {
996         philips_tda827xa_tuner_sleep(0x60, fe);
997         return 0;
998 }
999
1000 static struct tda1004x_config lifeview_trio_config = {
1001         .demod_address = 0x09,
1002         .invert        = 1,
1003         .invert_oclk   = 0,
1004         .xtal_freq     = TDA10046_XTAL_16M,
1005         .agc_config    = TDA10046_AGC_TDA827X_GP00,
1006         .if_freq       = TDA10046_FREQ_045,
1007         .request_firmware = NULL,
1008 };
1009
1010 /* ------------------------------------------------------------------ */
1011
1012 static int ads_duo_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1013 {
1014         int ret;
1015
1016         ret = philips_tda827xa_pll_set(0x61, fe, params);
1017         return ret;
1018 }
1019
1020 static int ads_duo_tuner_init(struct dvb_frontend *fe)
1021 {
1022         struct saa7134_dev *dev = fe->dvb->priv;
1023         /* route TDA8275a AGC input to the channel decoder */
1024         saa_writeb(SAA7134_GPIO_GPSTATUS2, 0x60);
1025         return 0;
1026 }
1027
1028 static int ads_duo_tuner_sleep(struct dvb_frontend *fe)
1029 {
1030         struct saa7134_dev *dev = fe->dvb->priv;
1031         /* route TDA8275a AGC input to the analog IF chip*/
1032         saa_writeb(SAA7134_GPIO_GPSTATUS2, 0x20);
1033         philips_tda827xa_tuner_sleep( 0x61, fe);
1034         return 0;
1035 }
1036
1037 static struct tda1004x_config ads_tech_duo_config = {
1038         .demod_address = 0x08,
1039         .invert        = 1,
1040         .invert_oclk   = 0,
1041         .xtal_freq     = TDA10046_XTAL_16M,
1042         .agc_config    = TDA10046_AGC_TDA827X_GP00,
1043         .if_freq       = TDA10046_FREQ_045,
1044         .request_firmware = NULL,
1045 };
1046
1047 /* ------------------------------------------------------------------ */
1048
1049 static int tevion_dvb220rf_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1050 {
1051         int ret;
1052         ret = philips_tda827xa_pll_set(0x60, fe, params);
1053         return ret;
1054 }
1055
1056 static int tevion_dvb220rf_tuner_sleep(struct dvb_frontend *fe)
1057 {
1058         philips_tda827xa_tuner_sleep( 0x61, fe);
1059         return 0;
1060 }
1061
1062 static struct tda1004x_config tevion_dvbt220rf_config = {
1063         .demod_address = 0x08,
1064         .invert        = 1,
1065         .invert_oclk   = 0,
1066         .xtal_freq     = TDA10046_XTAL_16M,
1067         .agc_config    = TDA10046_AGC_TDA827X_GP11,
1068         .if_freq       = TDA10046_FREQ_045,
1069         .request_firmware = NULL,
1070 };
1071
1072 /* ------------------------------------------------------------------ */
1073
1074 static int md8800_dvbt_analog_mode(struct dvb_frontend *fe)
1075 {
1076         struct saa7134_dev *dev = fe->dvb->priv;
1077         static u8 data[] = { 0x3c, 0x33, 0x68};
1078         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
1079
1080         i2c_transfer(&dev->i2c_adap, &msg, 1);
1081         philips_tda827xa_tuner_sleep( 0x61, fe);
1082         return 0;
1083 }
1084
1085 static int md8800_dvbt_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1086 {
1087         int ret;
1088         struct saa7134_dev *dev = fe->dvb->priv;
1089         static u8 tda8290_close[] = { 0x21, 0xc0};
1090         static u8 tda8290_open[]  = { 0x21, 0x80};
1091         struct i2c_msg tda8290_msg = {.addr = 0x4b,.flags = 0, .len = 2};
1092         /* close tda8290 i2c bridge */
1093         tda8290_msg.buf = tda8290_close;
1094         ret = i2c_transfer(&dev->i2c_adap, &tda8290_msg, 1);
1095         if (ret != 1)
1096                 return -EIO;
1097         msleep(20);
1098         ret = philips_tda827xa_pll_set(0x60, fe, params);
1099         if (ret != 0)
1100                 return ret;
1101         /* open tda8290 i2c bridge */
1102         tda8290_msg.buf = tda8290_open;
1103         i2c_transfer(&dev->i2c_adap, &tda8290_msg, 1);
1104         return ret;
1105 }
1106
1107 static struct tda1004x_config md8800_dvbt_config = {
1108         .demod_address = 0x08,
1109         .invert        = 1,
1110         .invert_oclk   = 0,
1111         .xtal_freq     = TDA10046_XTAL_16M,
1112         .agc_config    = TDA10046_AGC_TDA827X_GP11,
1113         .if_freq       = TDA10046_FREQ_045,
1114         .request_firmware = NULL,
1115 };
1116
1117 static struct tda10086_config flydvbs = {
1118         .demod_address = 0x0e,
1119         .invert = 0,
1120 };
1121
1122 /* ------------------------------------------------------------------ */
1123
1124 static struct nxt200x_config avertvhda180 = {
1125         .demod_address    = 0x0a,
1126 };
1127
1128 static int nxt200x_set_pll_input(u8 *buf, int input)
1129 {
1130         if (input)
1131                 buf[3] |= 0x08;
1132         else
1133                 buf[3] &= ~0x08;
1134         return 0;
1135 }
1136
1137 static struct nxt200x_config kworldatsc110 = {
1138         .demod_address    = 0x0a,
1139         .set_pll_input    = nxt200x_set_pll_input,
1140 };
1141
1142 /* ------------------------------------------------------------------ */
1143
1144 static int dvb_init(struct saa7134_dev *dev)
1145 {
1146         /* init struct videobuf_dvb */
1147         dev->ts.nr_bufs    = 32;
1148         dev->ts.nr_packets = 32*4;
1149         dev->dvb.name = dev->name;
1150         videobuf_queue_init(&dev->dvb.dvbq, &saa7134_ts_qops,
1151                             dev->pci, &dev->slock,
1152                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
1153                             V4L2_FIELD_ALTERNATE,
1154                             sizeof(struct saa7134_buf),
1155                             dev);
1156
1157         switch (dev->board) {
1158         case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1159                 printk("%s: pinnacle 300i dvb setup\n",dev->name);
1160                 dev->dvb.frontend = dvb_attach(mt352_attach, &pinnacle_300i,
1161                                                &dev->i2c_adap);
1162                 if (dev->dvb.frontend) {
1163                         dev->dvb.frontend->ops.tuner_ops.set_params = mt352_pinnacle_tuner_set_params;
1164                 }
1165                 break;
1166         case SAA7134_BOARD_AVERMEDIA_777:
1167         case SAA7134_BOARD_AVERMEDIA_A16AR:
1168                 printk("%s: avertv 777 dvb setup\n",dev->name);
1169                 dev->dvb.frontend = dvb_attach(mt352_attach, &avermedia_777,
1170                                                &dev->i2c_adap);
1171                 if (dev->dvb.frontend) {
1172                         dev->dvb.frontend->ops.tuner_ops.calc_regs = mt352_aver777_tuner_calc_regs;
1173                 }
1174                 break;
1175         case SAA7134_BOARD_MD7134:
1176                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1177                                                &medion_cardbus,
1178                                                &dev->i2c_adap);
1179                 if (dev->dvb.frontend) {
1180                         dev->dvb.frontend->ops.tuner_ops.init = philips_fmd1216_tuner_init;
1181                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_fmd1216_tuner_sleep;
1182                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_fmd1216_tuner_set_params;
1183                 }
1184                 break;
1185         case SAA7134_BOARD_PHILIPS_TOUGH:
1186                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1187                                                &philips_tu1216_60_config,
1188                                                &dev->i2c_adap);
1189                 if (dev->dvb.frontend) {
1190                         dev->dvb.frontend->ops.tuner_ops.init = philips_tu1216_tuner_60_init;
1191                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tu1216_tuner_60_set_params;
1192                 }
1193                 break;
1194         case SAA7134_BOARD_FLYDVBTDUO:
1195                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1196                                                &tda827x_lifeview_config,
1197                                                &dev->i2c_adap);
1198                 if (dev->dvb.frontend) {
1199                         dev->dvb.frontend->ops.tuner_ops.init = philips_tda827x_tuner_init;
1200                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tda827x_tuner_sleep;
1201                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tda827x_tuner_set_params;
1202                 }
1203                 break;
1204         case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
1205                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1206                                                &tda827x_lifeview_config,
1207                                                &dev->i2c_adap);
1208                 if (dev->dvb.frontend) {
1209                         dev->dvb.frontend->ops.tuner_ops.init = philips_tda827x_tuner_init;
1210                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tda827x_tuner_sleep;
1211                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tda827x_tuner_set_params;
1212                 }
1213                 break;
1214         case SAA7134_BOARD_PHILIPS_EUROPA:
1215                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1216                                                &philips_europa_config,
1217                                                &dev->i2c_adap);
1218                 if (dev->dvb.frontend) {
1219                         dev->original_demod_sleep = dev->dvb.frontend->ops.sleep;
1220                         dev->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1221                         dev->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1222                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1223                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1224                 }
1225                 break;
1226         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
1227                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1228                                                &philips_europa_config,
1229                                                &dev->i2c_adap);
1230                 if (dev->dvb.frontend) {
1231                         dev->original_demod_sleep = dev->dvb.frontend->ops.sleep;
1232                         dev->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1233                         dev->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1234                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1235                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1236                 }
1237                 break;
1238         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
1239                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1240                                                &philips_tu1216_61_config,
1241                                                &dev->i2c_adap);
1242                 if (dev->dvb.frontend) {
1243                         dev->dvb.frontend->ops.tuner_ops.init = philips_tu1216_tuner_61_init;
1244                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tu1216_tuner_61_set_params;
1245                 }
1246                 break;
1247         case SAA7134_BOARD_PHILIPS_TIGER:
1248                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1249                                                &philips_tiger_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 = philips_tiger_tuner_init;
1254                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tiger_tuner_sleep;
1255                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1256                 }
1257                 break;
1258         case SAA7134_BOARD_PINNACLE_PCTV_310i:
1259                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1260                                                &pinnacle_pctv_310i_config,
1261                                                &dev->i2c_adap);
1262                 if (dev->dvb.frontend) {
1263                         dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
1264                         dev->dvb.frontend->ops.tuner_ops.init = philips_tiger_tuner_init;
1265                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tiger_tuner_sleep;
1266                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1267                 }
1268                 break;
1269         case SAA7134_BOARD_HAUPPAUGE_HVR1110:
1270                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1271                                                &hauppauge_hvr_1110_config,
1272                                                &dev->i2c_adap);
1273                 if (dev->dvb.frontend) {
1274                         dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
1275                         dev->dvb.frontend->ops.tuner_ops.init = philips_tiger_tuner_init;
1276                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tiger_tuner_sleep;
1277                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1278                 }
1279                 break;
1280         case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
1281                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1282                                                &asus_p7131_dual_config,
1283                                                &dev->i2c_adap);
1284                 if (dev->dvb.frontend) {
1285                         dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
1286                         dev->dvb.frontend->ops.tuner_ops.init = asus_p7131_dual_tuner_init;
1287                         dev->dvb.frontend->ops.tuner_ops.sleep = asus_p7131_dual_tuner_sleep;
1288                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1289                 }
1290                 break;
1291         case SAA7134_BOARD_FLYDVBT_LR301:
1292                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1293                                                &tda827x_lifeview_config,
1294                                                &dev->i2c_adap);
1295                 if (dev->dvb.frontend) {
1296                         dev->dvb.frontend->ops.tuner_ops.init = philips_tda827x_tuner_init;
1297                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tda827x_tuner_sleep;
1298                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tda827x_tuner_set_params;
1299                 }
1300                 break;
1301         case SAA7134_BOARD_FLYDVB_TRIO:
1302                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1303                                                &lifeview_trio_config,
1304                                                &dev->i2c_adap);
1305                 if (dev->dvb.frontend) {
1306                         dev->dvb.frontend->ops.tuner_ops.sleep = lifeview_trio_tuner_sleep;
1307                         dev->dvb.frontend->ops.tuner_ops.set_params = lifeview_trio_tuner_set_params;
1308                 }
1309                 break;
1310         case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
1311                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1312                                                &ads_tech_duo_config,
1313                                                &dev->i2c_adap);
1314                 if (dev->dvb.frontend) {
1315                         dev->dvb.frontend->ops.tuner_ops.init = ads_duo_tuner_init;
1316                         dev->dvb.frontend->ops.tuner_ops.sleep = ads_duo_tuner_sleep;
1317                         dev->dvb.frontend->ops.tuner_ops.set_params = ads_duo_tuner_set_params;
1318                 }
1319                 break;
1320         case SAA7134_BOARD_TEVION_DVBT_220RF:
1321                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1322                                                &tevion_dvbt220rf_config,
1323                                                &dev->i2c_adap);
1324                 if (dev->dvb.frontend) {
1325                         dev->dvb.frontend->ops.tuner_ops.sleep = tevion_dvb220rf_tuner_sleep;
1326                         dev->dvb.frontend->ops.tuner_ops.set_params = tevion_dvb220rf_tuner_set_params;
1327                 }
1328                 break;
1329         case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
1330                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1331                                                &ads_tech_duo_config,
1332                                                &dev->i2c_adap);
1333                 if (dev->dvb.frontend) {
1334                         dev->dvb.frontend->ops.tuner_ops.init = ads_duo_tuner_init;
1335                         dev->dvb.frontend->ops.tuner_ops.sleep = ads_duo_tuner_sleep;
1336                         dev->dvb.frontend->ops.tuner_ops.set_params = ads_duo_tuner_set_params;
1337                 }
1338                 break;
1339         case SAA7134_BOARD_MEDION_MD8800_QUADRO:
1340                 dev->dvb.frontend = tda10046_attach(&md8800_dvbt_config,
1341                                                     &dev->i2c_adap);
1342                 if (dev->dvb.frontend) {
1343                         dev->dvb.frontend->ops.tuner_ops.init = philips_tiger_tuner_init;
1344                         dev->dvb.frontend->ops.tuner_ops.sleep = md8800_dvbt_analog_mode;
1345                         dev->dvb.frontend->ops.tuner_ops.set_params = md8800_dvbt_pll_set;
1346                 }
1347                 break;
1348         case SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180:
1349                 dev->dvb.frontend = dvb_attach(nxt200x_attach, &avertvhda180,
1350                                                &dev->i2c_adap);
1351                 if (dev->dvb.frontend) {
1352                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
1353                                    NULL, &dvb_pll_tdhu2);
1354                 }
1355                 break;
1356         case SAA7134_BOARD_KWORLD_ATSC110:
1357                 dev->dvb.frontend = dvb_attach(nxt200x_attach, &kworldatsc110,
1358                                                &dev->i2c_adap);
1359                 if (dev->dvb.frontend) {
1360                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
1361                                    NULL, &dvb_pll_tuv1236d);
1362                 }
1363                 break;
1364         case SAA7134_BOARD_FLYDVBS_LR300:
1365                 dev->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1366                                                &dev->i2c_adap);
1367                 if (dev->dvb.frontend) {
1368                         if (dvb_attach(tda826x_attach, dev->dvb.frontend, 0x60,
1369                                        &dev->i2c_adap, 0) == NULL) {
1370                                 printk("%s: No tda826x found!\n", __FUNCTION__);
1371                         }
1372                         if (dvb_attach(isl6421_attach, dev->dvb.frontend,
1373                                        &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1374                                 printk("%s: No ISL6421 found!\n", __FUNCTION__);
1375                         }
1376                 }
1377                 break;
1378         case SAA7134_BOARD_ASUS_EUROPA2_HYBRID:
1379                 dev->dvb.frontend = tda10046_attach(&medion_cardbus,
1380                                                     &dev->i2c_adap);
1381                 if (dev->dvb.frontend) {
1382                         dev->original_demod_sleep = dev->dvb.frontend->ops.sleep;
1383                         dev->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1384                         dev->dvb.frontend->ops.tuner_ops.init = philips_fmd1216_tuner_init;
1385                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_fmd1216_tuner_sleep;
1386                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_fmd1216_tuner_set_params;
1387                 }
1388                 break;
1389         case SAA7134_BOARD_VIDEOMATE_DVBT_200A:
1390                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1391                                 &philips_europa_config,
1392                                 &dev->i2c_adap);
1393                 if (dev->dvb.frontend) {
1394                         dev->dvb.frontend->ops.tuner_ops.init = philips_td1316_tuner_init;
1395                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1396                 }
1397                 break;
1398         case SAA7134_BOARD_CINERGY_HT_PCMCIA:
1399                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1400                                                &cinergy_ht_config,
1401                                                &dev->i2c_adap);
1402                 if (dev->dvb.frontend) {
1403                         dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
1404                         dev->dvb.frontend->ops.tuner_ops.init = cinergy_ht_tuner_init;
1405                         dev->dvb.frontend->ops.tuner_ops.sleep = cinergy_ht_tuner_sleep;
1406                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1407
1408                 }
1409                 break;
1410         default:
1411                 printk("%s: Huh? unknown DVB card?\n",dev->name);
1412                 break;
1413         }
1414
1415         if (NULL == dev->dvb.frontend) {
1416                 printk("%s: frontend initialization failed\n",dev->name);
1417                 return -1;
1418         }
1419
1420         /* register everything else */
1421         return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev, &dev->pci->dev);
1422 }
1423
1424 static int dvb_fini(struct saa7134_dev *dev)
1425 {
1426         static int on  = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
1427
1428         switch (dev->board) {
1429         case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1430                 /* otherwise we don't detect the tuner on next insmod */
1431                 saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&on);
1432                 break;
1433         };
1434         videobuf_dvb_unregister(&dev->dvb);
1435         return 0;
1436 }
1437
1438 static struct saa7134_mpeg_ops dvb_ops = {
1439         .type          = SAA7134_MPEG_DVB,
1440         .init          = dvb_init,
1441         .fini          = dvb_fini,
1442 };
1443
1444 static int __init dvb_register(void)
1445 {
1446         return saa7134_ts_register(&dvb_ops);
1447 }
1448
1449 static void __exit dvb_unregister(void)
1450 {
1451         saa7134_ts_unregister(&dvb_ops);
1452 }
1453
1454 module_init(dvb_register);
1455 module_exit(dvb_unregister);
1456
1457 /* ------------------------------------------------------------------ */
1458 /*
1459  * Local variables:
1460  * c-basic-offset: 8
1461  * End:
1462  */