V4L/DVB (4768): Add the Asus P7131 Dual hybrid to the new tda8290_i2c_gate_ctrl
[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_europa_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         msleep(1);
351
352         /* switch the board to dvb mode */
353         init_msg.addr = 0x43;
354         init_msg.len  = 0x02;
355         msg[0] = 0x00;
356         msg[1] = 0x40;
357         if (fe->ops.i2c_gate_ctrl)
358                 fe->ops.i2c_gate_ctrl(fe, 1);
359         if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
360                 return -EIO;
361
362         return 0;
363 }
364
365 static int philips_td1316_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
366 {
367         return philips_tda6651_pll_set(0x61, fe, params);
368 }
369
370 static int philips_europa_tuner_sleep(struct dvb_frontend *fe)
371 {
372         struct saa7134_dev *dev = fe->dvb->priv;
373         /* this message actually turns the tuner back to analog mode */
374         static u8 msg[] = { 0x0b, 0xdc, 0x86, 0xa4 };
375         struct i2c_msg analog_msg = {.addr = 0x61,.flags = 0,.buf = msg,.len = sizeof(msg) };
376
377         i2c_transfer(&dev->i2c_adap, &analog_msg, 1);
378         msleep(1);
379
380         /* switch the board to analog mode */
381         analog_msg.addr = 0x43;
382         analog_msg.len  = 0x02;
383         msg[0] = 0x00;
384         msg[1] = 0x14;
385         if (fe->ops.i2c_gate_ctrl)
386                 fe->ops.i2c_gate_ctrl(fe, 1);
387         i2c_transfer(&dev->i2c_adap, &analog_msg, 1);
388         return 0;
389 }
390
391 static int philips_europa_demod_sleep(struct dvb_frontend *fe)
392 {
393         struct saa7134_dev *dev = fe->dvb->priv;
394
395         if (dev->original_demod_sleep)
396                 dev->original_demod_sleep(fe);
397         fe->ops.i2c_gate_ctrl(fe, 1);
398         return 0;
399 }
400
401 static struct tda1004x_config philips_europa_config = {
402
403         .demod_address = 0x8,
404         .invert        = 0,
405         .invert_oclk   = 0,
406         .xtal_freq     = TDA10046_XTAL_4M,
407         .agc_config    = TDA10046_AGC_IFO_AUTO_POS,
408         .if_freq       = TDA10046_FREQ_052,
409         .request_firmware = NULL,
410 };
411
412 /* ------------------------------------------------------------------ */
413
414 static int philips_fmd1216_tuner_init(struct dvb_frontend *fe)
415 {
416         struct saa7134_dev *dev = fe->dvb->priv;
417         /* this message is to set up ATC and ALC */
418         static u8 fmd1216_init[] = { 0x0b, 0xdc, 0x9c, 0xa0 };
419         struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = fmd1216_init,.len = sizeof(fmd1216_init) };
420
421         if (fe->ops.i2c_gate_ctrl)
422                 fe->ops.i2c_gate_ctrl(fe, 1);
423         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
424                 return -EIO;
425         msleep(1);
426
427         return 0;
428 }
429
430 static int philips_fmd1216_tuner_sleep(struct dvb_frontend *fe)
431 {
432         struct saa7134_dev *dev = fe->dvb->priv;
433         /* this message actually turns the tuner back to analog mode */
434         static u8 fmd1216_init[] = { 0x0b, 0xdc, 0x9c, 0x60 };
435         struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = fmd1216_init,.len = sizeof(fmd1216_init) };
436
437         if (fe->ops.i2c_gate_ctrl)
438                 fe->ops.i2c_gate_ctrl(fe, 1);
439         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
440         msleep(1);
441         fmd1216_init[2] = 0x86;
442         fmd1216_init[3] = 0x54;
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         return 0;
448 }
449
450 static int philips_fmd1216_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
451 {
452         struct saa7134_dev *dev = fe->dvb->priv;
453         u8 tuner_buf[4];
454         struct i2c_msg tuner_msg = {.addr = 0x61,.flags = 0,.buf = tuner_buf,.len =
455                         sizeof(tuner_buf) };
456         int tuner_frequency = 0;
457         int divider = 0;
458         u8 band, mode, cp;
459
460         /* determine charge pump */
461         tuner_frequency = params->frequency + 36130000;
462         if (tuner_frequency < 87000000)
463                 return -EINVAL;
464         /* low band */
465         else if (tuner_frequency < 180000000) {
466                 band = 1;
467                 mode = 7;
468                 cp   = 0;
469         } else if (tuner_frequency < 195000000) {
470                 band = 1;
471                 mode = 6;
472                 cp   = 1;
473         /* mid band     */
474         } else if (tuner_frequency < 366000000) {
475                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
476                         band = 10;
477                 } else {
478                         band = 2;
479                 }
480                 mode = 7;
481                 cp   = 0;
482         } else if (tuner_frequency < 478000000) {
483                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
484                         band = 10;
485                 } else {
486                         band = 2;
487                 }
488                 mode = 6;
489                 cp   = 1;
490         /* high band */
491         } else if (tuner_frequency < 662000000) {
492                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
493                         band = 12;
494                 } else {
495                         band = 4;
496                 }
497                 mode = 7;
498                 cp   = 0;
499         } else if (tuner_frequency < 840000000) {
500                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
501                         band = 12;
502                 } else {
503                         band = 4;
504                 }
505                 mode = 6;
506                 cp   = 1;
507         } else {
508                 if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
509                         band = 12;
510                 } else {
511                         band = 4;
512                 }
513                 mode = 7;
514                 cp   = 1;
515
516         }
517         /* calculate divisor */
518         /* ((36166000 + Finput) / 166666) rounded! */
519         divider = (tuner_frequency + 83333) / 166667;
520
521         /* setup tuner buffer */
522         tuner_buf[0] = (divider >> 8) & 0x7f;
523         tuner_buf[1] = divider & 0xff;
524         tuner_buf[2] = 0x80 | (cp << 6) | (mode  << 3) | 4;
525         tuner_buf[3] = 0x40 | band;
526
527         if (fe->ops.i2c_gate_ctrl)
528                 fe->ops.i2c_gate_ctrl(fe, 1);
529         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
530                 return -EIO;
531         return 0;
532 }
533
534 static struct tda1004x_config medion_cardbus = {
535         .demod_address = 0x08,
536         .invert        = 1,
537         .invert_oclk   = 0,
538         .xtal_freq     = TDA10046_XTAL_16M,
539         .agc_config    = TDA10046_AGC_IFO_AUTO_NEG,
540         .if_freq       = TDA10046_FREQ_3613,
541         .request_firmware = NULL,
542 };
543
544 /* ------------------------------------------------------------------ */
545
546 struct tda827x_data {
547         u32 lomax;
548         u8  spd;
549         u8  bs;
550         u8  bp;
551         u8  cp;
552         u8  gc3;
553         u8 div1p5;
554 };
555
556 static struct tda827x_data tda827x_dvbt[] = {
557         { .lomax =  62000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
558         { .lomax =  66000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
559         { .lomax =  76000000, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
560         { .lomax =  84000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
561         { .lomax =  93000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
562         { .lomax =  98000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
563         { .lomax = 109000000, .spd = 3, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
564         { .lomax = 123000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
565         { .lomax = 133000000, .spd = 2, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
566         { .lomax = 151000000, .spd = 2, .bs = 1, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
567         { .lomax = 154000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
568         { .lomax = 181000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 0, .div1p5 = 0},
569         { .lomax = 185000000, .spd = 2, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
570         { .lomax = 217000000, .spd = 2, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
571         { .lomax = 244000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
572         { .lomax = 265000000, .spd = 1, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
573         { .lomax = 302000000, .spd = 1, .bs = 1, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
574         { .lomax = 324000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
575         { .lomax = 370000000, .spd = 1, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
576         { .lomax = 454000000, .spd = 1, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
577         { .lomax = 493000000, .spd = 0, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
578         { .lomax = 530000000, .spd = 0, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
579         { .lomax = 554000000, .spd = 0, .bs = 1, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
580         { .lomax = 604000000, .spd = 0, .bs = 1, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
581         { .lomax = 696000000, .spd = 0, .bs = 2, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
582         { .lomax = 740000000, .spd = 0, .bs = 2, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
583         { .lomax = 820000000, .spd = 0, .bs = 3, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
584         { .lomax = 865000000, .spd = 0, .bs = 3, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
585         { .lomax =         0, .spd = 0, .bs = 0, .bp = 0, .cp = 0, .gc3 = 0, .div1p5 = 0}
586 };
587
588 static int philips_tda827x_tuner_init(struct dvb_frontend *fe)
589 {
590         return 0;
591 }
592
593 static int philips_tda827x_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
594 {
595         struct saa7134_dev *dev = fe->dvb->priv;
596         u8 tuner_buf[14];
597
598         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,
599                                         .len = sizeof(tuner_buf) };
600         int i, tuner_freq, if_freq;
601         u32 N;
602         switch (params->u.ofdm.bandwidth) {
603         case BANDWIDTH_6_MHZ:
604                 if_freq = 4000000;
605                 break;
606         case BANDWIDTH_7_MHZ:
607                 if_freq = 4500000;
608                 break;
609         default:                   /* 8 MHz or Auto */
610                 if_freq = 5000000;
611                 break;
612         }
613         tuner_freq = params->frequency + if_freq;
614
615         i = 0;
616         while (tda827x_dvbt[i].lomax < tuner_freq) {
617                 if(tda827x_dvbt[i + 1].lomax == 0)
618                         break;
619                 i++;
620         }
621
622         N = ((tuner_freq + 125000) / 250000) << (tda827x_dvbt[i].spd + 2);
623         tuner_buf[0] = 0;
624         tuner_buf[1] = (N>>8) | 0x40;
625         tuner_buf[2] = N & 0xff;
626         tuner_buf[3] = 0;
627         tuner_buf[4] = 0x52;
628         tuner_buf[5] = (tda827x_dvbt[i].spd << 6) + (tda827x_dvbt[i].div1p5 << 5) +
629                                    (tda827x_dvbt[i].bs << 3) + tda827x_dvbt[i].bp;
630         tuner_buf[6] = (tda827x_dvbt[i].gc3 << 4) + 0x8f;
631         tuner_buf[7] = 0xbf;
632         tuner_buf[8] = 0x2a;
633         tuner_buf[9] = 0x05;
634         tuner_buf[10] = 0xff;
635         tuner_buf[11] = 0x00;
636         tuner_buf[12] = 0x00;
637         tuner_buf[13] = 0x40;
638
639         tuner_msg.len = 14;
640         if (fe->ops.i2c_gate_ctrl)
641                 fe->ops.i2c_gate_ctrl(fe, 1);
642         if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
643                 return -EIO;
644
645         msleep(500);
646         /* correct CP value */
647         tuner_buf[0] = 0x30;
648         tuner_buf[1] = 0x50 + tda827x_dvbt[i].cp;
649         tuner_msg.len = 2;
650         if (fe->ops.i2c_gate_ctrl)
651                 fe->ops.i2c_gate_ctrl(fe, 1);
652         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
653
654         return 0;
655 }
656
657 static int philips_tda827x_tuner_sleep(struct dvb_frontend *fe)
658 {
659         struct saa7134_dev *dev = fe->dvb->priv;
660         static u8 tda827x_sleep[] = { 0x30, 0xd0};
661         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tda827x_sleep,
662                                     .len = sizeof(tda827x_sleep) };
663         if (fe->ops.i2c_gate_ctrl)
664                 fe->ops.i2c_gate_ctrl(fe, 1);
665         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
666         return 0;
667 }
668
669 static struct tda1004x_config tda827x_lifeview_config = {
670         .demod_address = 0x08,
671         .invert        = 1,
672         .invert_oclk   = 0,
673         .xtal_freq     = TDA10046_XTAL_16M,
674         .agc_config    = TDA10046_AGC_TDA827X,
675         .if_freq       = TDA10046_FREQ_045,
676         .request_firmware = NULL,
677 };
678
679 /* ------------------------------------------------------------------ */
680
681 struct tda827xa_data {
682         u32 lomax;
683         u8  svco;
684         u8  spd;
685         u8  scr;
686         u8  sbs;
687         u8  gc3;
688 };
689
690 static struct tda827xa_data tda827xa_dvbt[] = {
691         { .lomax =  56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 1},
692         { .lomax =  67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
693         { .lomax =  81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
694         { .lomax =  97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
695         { .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
696         { .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
697         { .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
698         { .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
699         { .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
700         { .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
701         { .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
702         { .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
703         { .lomax = 290000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
704         { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
705         { .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
706         { .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
707         { .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
708         { .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
709         { .lomax = 550000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
710         { .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
711         { .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
712         { .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
713         { .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
714         { .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
715         { .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
716         { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
717         { .lomax =         0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}};
718
719
720 static int philips_tda827xa_pll_set(u8 addr, struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
721 {
722         struct saa7134_dev *dev = fe->dvb->priv;
723         u8 tuner_buf[14];
724         unsigned char reg2[2];
725
726         struct i2c_msg msg = {.addr = addr,.flags = 0,.buf = tuner_buf};
727         int i, tuner_freq, if_freq;
728         u32 N;
729
730         switch (params->u.ofdm.bandwidth) {
731         case BANDWIDTH_6_MHZ:
732                 if_freq = 4000000;
733                 break;
734         case BANDWIDTH_7_MHZ:
735                 if_freq = 4500000;
736                 break;
737         default:                   /* 8 MHz or Auto */
738                 if_freq = 5000000;
739                 break;
740         }
741         tuner_freq = params->frequency + if_freq;
742
743         i = 0;
744         while (tda827xa_dvbt[i].lomax < tuner_freq) {
745                 if(tda827xa_dvbt[i + 1].lomax == 0)
746                         break;
747                 i++;
748         }
749
750         N = ((tuner_freq + 31250) / 62500) << tda827xa_dvbt[i].spd;
751         tuner_buf[0] = 0;            // subaddress
752         tuner_buf[1] = N >> 8;
753         tuner_buf[2] = N & 0xff;
754         tuner_buf[3] = 0;
755         tuner_buf[4] = 0x16;
756         tuner_buf[5] = (tda827xa_dvbt[i].spd << 5) + (tda827xa_dvbt[i].svco << 3) +
757                         tda827xa_dvbt[i].sbs;
758         tuner_buf[6] = 0x4b + (tda827xa_dvbt[i].gc3 << 4);
759         tuner_buf[7] = 0x0c;
760         tuner_buf[8] = 0x06;
761         tuner_buf[9] = 0x24;
762         tuner_buf[10] = 0xff;
763         tuner_buf[11] = 0x60;
764         tuner_buf[12] = 0x00;
765         tuner_buf[13] = 0x39;  // lpsel
766         msg.len = 14;
767         if (fe->ops.i2c_gate_ctrl)
768                 fe->ops.i2c_gate_ctrl(fe, 1);
769         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
770                 return -EIO;
771
772         msg.buf= reg2;
773         msg.len = 2;
774         reg2[0] = 0x60;
775         reg2[1] = 0x3c;
776         if (fe->ops.i2c_gate_ctrl)
777                 fe->ops.i2c_gate_ctrl(fe, 1);
778         i2c_transfer(&dev->i2c_adap, &msg, 1);
779
780         reg2[0] = 0xa0;
781         reg2[1] = 0x40;
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         msleep(2);
787         /* correct CP value */
788         reg2[0] = 0x30;
789         reg2[1] = 0x10 + tda827xa_dvbt[i].scr;
790         msg.len = 2;
791         if (fe->ops.i2c_gate_ctrl)
792                 fe->ops.i2c_gate_ctrl(fe, 1);
793         i2c_transfer(&dev->i2c_adap, &msg, 1);
794
795         msleep(550);
796         reg2[0] = 0x50;
797         reg2[1] = 0x4f + (tda827xa_dvbt[i].gc3 << 4);
798         if (fe->ops.i2c_gate_ctrl)
799                 fe->ops.i2c_gate_ctrl(fe, 1);
800         i2c_transfer(&dev->i2c_adap, &msg, 1);
801
802         return 0;
803
804 }
805
806 static int philips_tda827xa_tuner_sleep(u8 addr, struct dvb_frontend *fe)
807 {
808         struct saa7134_dev *dev = fe->dvb->priv;
809         static u8 tda827xa_sleep[] = { 0x30, 0x90};
810         struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tda827xa_sleep,
811                                     .len = sizeof(tda827xa_sleep) };
812         if (fe->ops.i2c_gate_ctrl)
813                 fe->ops.i2c_gate_ctrl(fe, 1);
814         i2c_transfer(&dev->i2c_adap, &tuner_msg, 1);
815         if (fe->ops.i2c_gate_ctrl)
816                 fe->ops.i2c_gate_ctrl(fe, 0);
817         return 0;
818 }
819
820 /* ------------------------------------------------------------------ */
821
822 static int tda8290_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
823 {
824         struct saa7134_dev *dev = fe->dvb->priv;
825         static u8 tda8290_close[] = { 0x21, 0xc0};
826         static u8 tda8290_open[]  = { 0x21, 0x80};
827         struct i2c_msg tda8290_msg = {.addr = 0x4b,.flags = 0, .len = 2};
828         if (enable) {
829                 tda8290_msg.buf = tda8290_close;
830         } else {
831                 tda8290_msg.buf = tda8290_open;
832         }
833         if (i2c_transfer(&dev->i2c_adap, &tda8290_msg, 1) != 1)
834                 return -EIO;
835         msleep(20);
836         return 0;
837 }
838
839 /* ------------------------------------------------------------------ */
840
841 static int philips_tiger_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
842 {
843         int ret;
844
845         ret = philips_tda827xa_pll_set(0x61, fe, params);
846         if (ret != 0)
847                 return ret;
848         return 0;
849 }
850
851 static int philips_tiger_tuner_init(struct dvb_frontend *fe)
852 {
853         struct saa7134_dev *dev = fe->dvb->priv;
854         static u8 data[] = { 0x3c, 0x33, 0x6a};
855         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
856
857         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
858                 return -EIO;
859         return 0;
860 }
861
862 static int philips_tiger_tuner_sleep(struct dvb_frontend *fe)
863 {
864         struct saa7134_dev *dev = fe->dvb->priv;
865         static u8 data[] = { 0x3c, 0x33, 0x68};
866         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
867
868         i2c_transfer(&dev->i2c_adap, &msg, 1);
869         philips_tda827xa_tuner_sleep( 0x61, fe);
870         return 0;
871 }
872
873 static struct tda1004x_config philips_tiger_config = {
874         .demod_address = 0x08,
875         .invert        = 1,
876         .invert_oclk   = 0,
877         .xtal_freq     = TDA10046_XTAL_16M,
878         .agc_config    = TDA10046_AGC_TDA827X,
879         .if_freq       = TDA10046_FREQ_045,
880         .request_firmware = NULL,
881 };
882
883 /* ------------------------------------------------------------------ */
884
885 static struct tda1004x_config pinnacle_pctv_310i_config = {
886         .demod_address = 0x08,
887         .invert        = 1,
888         .invert_oclk   = 0,
889         .xtal_freq     = TDA10046_XTAL_16M,
890         .agc_config    = TDA10046_AGC_TDA827X,
891         .if_freq       = TDA10046_FREQ_045,
892         .request_firmware = philips_tda1004x_request_firmware,
893 };
894
895 /* ------------------------------------------------------------------ */
896
897 static int asus_p7131_dual_tuner_init(struct dvb_frontend *fe)
898 {
899         struct saa7134_dev *dev = fe->dvb->priv;
900         static u8 data[] = { 0x3c, 0x33, 0x6a};
901         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
902
903         if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
904                 return -EIO;
905         /* make sure the DVB-T antenna input is set */
906         saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x0200000);
907         return 0;
908 }
909
910 static int asus_p7131_dual_tuner_sleep(struct dvb_frontend *fe)
911 {
912         struct saa7134_dev *dev = fe->dvb->priv;
913         static u8 data[] = { 0x3c, 0x33, 0x68};
914         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
915
916         i2c_transfer(&dev->i2c_adap, &msg, 1);
917         philips_tda827xa_tuner_sleep( 0x61, fe);
918         /* reset antenna inputs for analog usage */
919         saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x0200000);
920         return 0;
921 }
922
923 /* ------------------------------------------------------------------ */
924
925 static int lifeview_trio_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
926 {
927         int ret;
928
929         ret = philips_tda827xa_pll_set(0x60, fe, params);
930         return ret;
931 }
932
933 static int lifeview_trio_tuner_sleep(struct dvb_frontend *fe)
934 {
935         philips_tda827xa_tuner_sleep(0x60, fe);
936         return 0;
937 }
938
939 static struct tda1004x_config lifeview_trio_config = {
940         .demod_address = 0x09,
941         .invert        = 1,
942         .invert_oclk   = 0,
943         .xtal_freq     = TDA10046_XTAL_16M,
944         .agc_config    = TDA10046_AGC_TDA827X_GPL,
945         .if_freq       = TDA10046_FREQ_045,
946         .request_firmware = NULL,
947 };
948
949 /* ------------------------------------------------------------------ */
950
951 static int ads_duo_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
952 {
953         int ret;
954
955         ret = philips_tda827xa_pll_set(0x61, fe, params);
956         return ret;
957 }
958
959 static int ads_duo_tuner_init(struct dvb_frontend *fe)
960 {
961         struct saa7134_dev *dev = fe->dvb->priv;
962         /* route TDA8275a AGC input to the channel decoder */
963         saa_writeb(SAA7134_GPIO_GPSTATUS2, 0x60);
964         return 0;
965 }
966
967 static int ads_duo_tuner_sleep(struct dvb_frontend *fe)
968 {
969         struct saa7134_dev *dev = fe->dvb->priv;
970         /* route TDA8275a AGC input to the analog IF chip*/
971         saa_writeb(SAA7134_GPIO_GPSTATUS2, 0x20);
972         philips_tda827xa_tuner_sleep( 0x61, fe);
973         return 0;
974 }
975
976 static struct tda1004x_config ads_tech_duo_config = {
977         .demod_address = 0x08,
978         .invert        = 1,
979         .invert_oclk   = 0,
980         .xtal_freq     = TDA10046_XTAL_16M,
981         .agc_config    = TDA10046_AGC_TDA827X_GPL,
982         .if_freq       = TDA10046_FREQ_045,
983         .request_firmware = NULL,
984 };
985
986 /* ------------------------------------------------------------------ */
987
988 static int tevion_dvb220rf_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
989 {
990         int ret;
991         ret = philips_tda827xa_pll_set(0x60, fe, params);
992         return ret;
993 }
994
995 static int tevion_dvb220rf_tuner_sleep(struct dvb_frontend *fe)
996 {
997         philips_tda827xa_tuner_sleep( 0x61, fe);
998         return 0;
999 }
1000
1001 static struct tda1004x_config tevion_dvbt220rf_config = {
1002         .demod_address = 0x08,
1003         .invert        = 1,
1004         .invert_oclk   = 0,
1005         .xtal_freq     = TDA10046_XTAL_16M,
1006         .agc_config    = TDA10046_AGC_TDA827X,
1007         .if_freq       = TDA10046_FREQ_045,
1008         .request_firmware = NULL,
1009 };
1010
1011 /* ------------------------------------------------------------------ */
1012
1013 static int md8800_dvbt_analog_mode(struct dvb_frontend *fe)
1014 {
1015         struct saa7134_dev *dev = fe->dvb->priv;
1016         static u8 data[] = { 0x3c, 0x33, 0x68};
1017         struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
1018
1019         i2c_transfer(&dev->i2c_adap, &msg, 1);
1020         philips_tda827xa_tuner_sleep( 0x61, fe);
1021         return 0;
1022 }
1023
1024 static int md8800_dvbt_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1025 {
1026         int ret;
1027         struct saa7134_dev *dev = fe->dvb->priv;
1028         static u8 tda8290_close[] = { 0x21, 0xc0};
1029         static u8 tda8290_open[]  = { 0x21, 0x80};
1030         struct i2c_msg tda8290_msg = {.addr = 0x4b,.flags = 0, .len = 2};
1031         /* close tda8290 i2c bridge */
1032         tda8290_msg.buf = tda8290_close;
1033         ret = i2c_transfer(&dev->i2c_adap, &tda8290_msg, 1);
1034         if (ret != 1)
1035                 return -EIO;
1036         msleep(20);
1037         ret = philips_tda827xa_pll_set(0x60, fe, params);
1038         if (ret != 0)
1039                 return ret;
1040         /* open tda8290 i2c bridge */
1041         tda8290_msg.buf = tda8290_open;
1042         i2c_transfer(&dev->i2c_adap, &tda8290_msg, 1);
1043         return ret;
1044 }
1045
1046 static struct tda1004x_config md8800_dvbt_config = {
1047         .demod_address = 0x08,
1048         .invert        = 1,
1049         .invert_oclk   = 0,
1050         .xtal_freq     = TDA10046_XTAL_16M,
1051         .agc_config    = TDA10046_AGC_TDA827X,
1052         .if_freq       = TDA10046_FREQ_045,
1053         .request_firmware = NULL,
1054 };
1055
1056 static struct tda10086_config flydvbs = {
1057         .demod_address = 0x0e,
1058         .invert = 0,
1059 };
1060
1061 /* ------------------------------------------------------------------ */
1062
1063 static struct nxt200x_config avertvhda180 = {
1064         .demod_address    = 0x0a,
1065 };
1066
1067 static int nxt200x_set_pll_input(u8 *buf, int input)
1068 {
1069         if (input)
1070                 buf[3] |= 0x08;
1071         else
1072                 buf[3] &= ~0x08;
1073         return 0;
1074 }
1075
1076 static struct nxt200x_config kworldatsc110 = {
1077         .demod_address    = 0x0a,
1078         .set_pll_input    = nxt200x_set_pll_input,
1079 };
1080
1081 /* ------------------------------------------------------------------ */
1082
1083 static int dvb_init(struct saa7134_dev *dev)
1084 {
1085         /* init struct videobuf_dvb */
1086         dev->ts.nr_bufs    = 32;
1087         dev->ts.nr_packets = 32*4;
1088         dev->dvb.name = dev->name;
1089         videobuf_queue_init(&dev->dvb.dvbq, &saa7134_ts_qops,
1090                             dev->pci, &dev->slock,
1091                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
1092                             V4L2_FIELD_ALTERNATE,
1093                             sizeof(struct saa7134_buf),
1094                             dev);
1095
1096         switch (dev->board) {
1097         case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1098                 printk("%s: pinnacle 300i dvb setup\n",dev->name);
1099                 dev->dvb.frontend = dvb_attach(mt352_attach, &pinnacle_300i,
1100                                                &dev->i2c_adap);
1101                 if (dev->dvb.frontend) {
1102                         dev->dvb.frontend->ops.tuner_ops.set_params = mt352_pinnacle_tuner_set_params;
1103                 }
1104                 break;
1105         case SAA7134_BOARD_AVERMEDIA_777:
1106         case SAA7134_BOARD_AVERMEDIA_A16AR:
1107                 printk("%s: avertv 777 dvb setup\n",dev->name);
1108                 dev->dvb.frontend = dvb_attach(mt352_attach, &avermedia_777,
1109                                                &dev->i2c_adap);
1110                 if (dev->dvb.frontend) {
1111                         dev->dvb.frontend->ops.tuner_ops.calc_regs = mt352_aver777_tuner_calc_regs;
1112                 }
1113                 break;
1114         case SAA7134_BOARD_MD7134:
1115                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1116                                                &medion_cardbus,
1117                                                &dev->i2c_adap);
1118                 if (dev->dvb.frontend) {
1119                         dev->dvb.frontend->ops.tuner_ops.init = philips_fmd1216_tuner_init;
1120                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_fmd1216_tuner_sleep;
1121                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_fmd1216_tuner_set_params;
1122                 }
1123                 break;
1124         case SAA7134_BOARD_PHILIPS_TOUGH:
1125                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1126                                                &philips_tu1216_60_config,
1127                                                &dev->i2c_adap);
1128                 if (dev->dvb.frontend) {
1129                         dev->dvb.frontend->ops.tuner_ops.init = philips_tu1216_tuner_60_init;
1130                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tu1216_tuner_60_set_params;
1131                 }
1132                 break;
1133         case SAA7134_BOARD_FLYDVBTDUO:
1134                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1135                                                &tda827x_lifeview_config,
1136                                                &dev->i2c_adap);
1137                 if (dev->dvb.frontend) {
1138                         dev->dvb.frontend->ops.tuner_ops.init = philips_tda827x_tuner_init;
1139                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tda827x_tuner_sleep;
1140                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tda827x_tuner_set_params;
1141                 }
1142                 break;
1143         case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
1144                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1145                                                &tda827x_lifeview_config,
1146                                                &dev->i2c_adap);
1147                 if (dev->dvb.frontend) {
1148                         dev->dvb.frontend->ops.tuner_ops.init = philips_tda827x_tuner_init;
1149                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tda827x_tuner_sleep;
1150                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tda827x_tuner_set_params;
1151                 }
1152                 break;
1153         case SAA7134_BOARD_PHILIPS_EUROPA:
1154                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1155                                                &philips_europa_config,
1156                                                &dev->i2c_adap);
1157                 if (dev->dvb.frontend) {
1158                         dev->original_demod_sleep = dev->dvb.frontend->ops.sleep;
1159                         dev->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1160                         dev->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1161                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1162                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1163                 }
1164                 break;
1165         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
1166                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1167                                                &philips_europa_config,
1168                                                &dev->i2c_adap);
1169                 if (dev->dvb.frontend) {
1170                         dev->original_demod_sleep = dev->dvb.frontend->ops.sleep;
1171                         dev->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1172                         dev->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1173                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1174                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1175                 }
1176                 break;
1177         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
1178                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1179                                                &philips_tu1216_61_config,
1180                                                &dev->i2c_adap);
1181                 if (dev->dvb.frontend) {
1182                         dev->dvb.frontend->ops.tuner_ops.init = philips_tu1216_tuner_61_init;
1183                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tu1216_tuner_61_set_params;
1184                 }
1185                 break;
1186         case SAA7134_BOARD_PHILIPS_TIGER:
1187                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1188                                                &philips_tiger_config,
1189                                                &dev->i2c_adap);
1190                 if (dev->dvb.frontend) {
1191                         dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
1192                         dev->dvb.frontend->ops.tuner_ops.init = philips_tiger_tuner_init;
1193                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tiger_tuner_sleep;
1194                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1195                 }
1196                 break;
1197         case SAA7134_BOARD_PINNACLE_PCTV_310i:
1198                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1199                                                &pinnacle_pctv_310i_config,
1200                                                &dev->i2c_adap);
1201                 if (dev->dvb.frontend) {
1202                         dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
1203                         dev->dvb.frontend->ops.tuner_ops.init = philips_tiger_tuner_init;
1204                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tiger_tuner_sleep;
1205                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1206                 }
1207                 break;
1208         case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
1209                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1210                                                &philips_tiger_config,
1211                                                &dev->i2c_adap);
1212                 if (dev->dvb.frontend) {
1213                         dev->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
1214                         dev->dvb.frontend->ops.tuner_ops.init = asus_p7131_dual_tuner_init;
1215                         dev->dvb.frontend->ops.tuner_ops.sleep = asus_p7131_dual_tuner_sleep;
1216                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tiger_tuner_set_params;
1217                 }
1218                 break;
1219         case SAA7134_BOARD_FLYDVBT_LR301:
1220                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1221                                                &tda827x_lifeview_config,
1222                                                &dev->i2c_adap);
1223                 if (dev->dvb.frontend) {
1224                         dev->dvb.frontend->ops.tuner_ops.init = philips_tda827x_tuner_init;
1225                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_tda827x_tuner_sleep;
1226                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_tda827x_tuner_set_params;
1227                 }
1228                 break;
1229         case SAA7134_BOARD_FLYDVB_TRIO:
1230                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1231                                                &lifeview_trio_config,
1232                                                &dev->i2c_adap);
1233                 if (dev->dvb.frontend) {
1234                         dev->dvb.frontend->ops.tuner_ops.sleep = lifeview_trio_tuner_sleep;
1235                         dev->dvb.frontend->ops.tuner_ops.set_params = lifeview_trio_tuner_set_params;
1236                 }
1237                 break;
1238         case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
1239                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1240                                                &ads_tech_duo_config,
1241                                                &dev->i2c_adap);
1242                 if (dev->dvb.frontend) {
1243                         dev->dvb.frontend->ops.tuner_ops.init = ads_duo_tuner_init;
1244                         dev->dvb.frontend->ops.tuner_ops.sleep = ads_duo_tuner_sleep;
1245                         dev->dvb.frontend->ops.tuner_ops.set_params = ads_duo_tuner_set_params;
1246                 }
1247                 break;
1248         case SAA7134_BOARD_TEVION_DVBT_220RF:
1249                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1250                                                &tevion_dvbt220rf_config,
1251                                                &dev->i2c_adap);
1252                 if (dev->dvb.frontend) {
1253                         dev->dvb.frontend->ops.tuner_ops.sleep = tevion_dvb220rf_tuner_sleep;
1254                         dev->dvb.frontend->ops.tuner_ops.set_params = tevion_dvb220rf_tuner_set_params;
1255                 }
1256                 break;
1257         case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
1258                 dev->dvb.frontend = dvb_attach(tda10046_attach,
1259                                                &ads_tech_duo_config,
1260                                                &dev->i2c_adap);
1261                 if (dev->dvb.frontend) {
1262                         dev->dvb.frontend->ops.tuner_ops.init = ads_duo_tuner_init;
1263                         dev->dvb.frontend->ops.tuner_ops.sleep = ads_duo_tuner_sleep;
1264                         dev->dvb.frontend->ops.tuner_ops.set_params = ads_duo_tuner_set_params;
1265                 }
1266                 break;
1267         case SAA7134_BOARD_MEDION_MD8800_QUADRO:
1268                 dev->dvb.frontend = tda10046_attach(&md8800_dvbt_config,
1269                                                     &dev->i2c_adap);
1270                 if (dev->dvb.frontend) {
1271                         dev->dvb.frontend->ops.tuner_ops.init = philips_tiger_tuner_init;
1272                         dev->dvb.frontend->ops.tuner_ops.sleep = md8800_dvbt_analog_mode;
1273                         dev->dvb.frontend->ops.tuner_ops.set_params = md8800_dvbt_pll_set;
1274                 }
1275                 break;
1276         case SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180:
1277                 dev->dvb.frontend = dvb_attach(nxt200x_attach, &avertvhda180,
1278                                                &dev->i2c_adap);
1279                 if (dev->dvb.frontend) {
1280                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
1281                                    NULL, &dvb_pll_tdhu2);
1282                 }
1283                 break;
1284         case SAA7134_BOARD_KWORLD_ATSC110:
1285                 dev->dvb.frontend = dvb_attach(nxt200x_attach, &kworldatsc110,
1286                                                &dev->i2c_adap);
1287                 if (dev->dvb.frontend) {
1288                         dvb_attach(dvb_pll_attach, dev->dvb.frontend, 0x61,
1289                                    NULL, &dvb_pll_tuv1236d);
1290                 }
1291                 break;
1292         case SAA7134_BOARD_FLYDVBS_LR300:
1293                 dev->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1294                                                &dev->i2c_adap);
1295                 if (dev->dvb.frontend) {
1296                         if (dvb_attach(tda826x_attach, dev->dvb.frontend, 0x60,
1297                                        &dev->i2c_adap, 0) == NULL) {
1298                                 printk("%s: No tda826x found!\n", __FUNCTION__);
1299                         }
1300                         if (dvb_attach(isl6421_attach, dev->dvb.frontend,
1301                                        &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1302                                 printk("%s: No ISL6421 found!\n", __FUNCTION__);
1303                         }
1304                 }
1305                 break;
1306         case SAA7134_BOARD_ASUS_EUROPA2_HYBRID:
1307                 dev->dvb.frontend = tda10046_attach(&medion_cardbus,
1308                                                     &dev->i2c_adap);
1309                 if (dev->dvb.frontend) {
1310                         dev->original_demod_sleep = dev->dvb.frontend->ops.sleep;
1311                         dev->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1312                         dev->dvb.frontend->ops.tuner_ops.init = philips_fmd1216_tuner_init;
1313                         dev->dvb.frontend->ops.tuner_ops.sleep = philips_fmd1216_tuner_sleep;
1314                         dev->dvb.frontend->ops.tuner_ops.set_params = philips_fmd1216_tuner_set_params;
1315                 }
1316                 break;
1317
1318         default:
1319                 printk("%s: Huh? unknown DVB card?\n",dev->name);
1320                 break;
1321         }
1322
1323         if (NULL == dev->dvb.frontend) {
1324                 printk("%s: frontend initialization failed\n",dev->name);
1325                 return -1;
1326         }
1327
1328         /* register everything else */
1329         return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev, &dev->pci->dev);
1330 }
1331
1332 static int dvb_fini(struct saa7134_dev *dev)
1333 {
1334         static int on  = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
1335
1336         switch (dev->board) {
1337         case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1338                 /* otherwise we don't detect the tuner on next insmod */
1339                 saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&on);
1340                 break;
1341         };
1342         videobuf_dvb_unregister(&dev->dvb);
1343         return 0;
1344 }
1345
1346 static struct saa7134_mpeg_ops dvb_ops = {
1347         .type          = SAA7134_MPEG_DVB,
1348         .init          = dvb_init,
1349         .fini          = dvb_fini,
1350 };
1351
1352 static int __init dvb_register(void)
1353 {
1354         return saa7134_ts_register(&dvb_ops);
1355 }
1356
1357 static void __exit dvb_unregister(void)
1358 {
1359         saa7134_ts_unregister(&dvb_ops);
1360 }
1361
1362 module_init(dvb_register);
1363 module_exit(dvb_unregister);
1364
1365 /* ------------------------------------------------------------------ */
1366 /*
1367  * Local variables:
1368  * c-basic-offset: 8
1369  * End:
1370  */