Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
0005  *
0006  *  Extended 3 / 2005 by Hartmut Hackmann to support various
0007  *  cards with the tda10046 DVB-T channel decoder
0008  */
0009 
0010 #include "saa7134.h"
0011 #include "saa7134-reg.h"
0012 
0013 #include <linux/init.h>
0014 #include <linux/list.h>
0015 #include <linux/module.h>
0016 #include <linux/kernel.h>
0017 #include <linux/delay.h>
0018 #include <linux/kthread.h>
0019 #include <linux/suspend.h>
0020 
0021 #include <media/v4l2-common.h>
0022 #include "dvb-pll.h"
0023 #include <media/dvb_frontend.h>
0024 
0025 #include "mt352.h"
0026 #include "mt352_priv.h" /* FIXME */
0027 #include "tda1004x.h"
0028 #include "nxt200x.h"
0029 #include "xc2028.h"
0030 #include "xc5000.h"
0031 
0032 #include "tda10086.h"
0033 #include "tda826x.h"
0034 #include "tda827x.h"
0035 #include "isl6421.h"
0036 #include "isl6405.h"
0037 #include "lnbp21.h"
0038 #include "tuner-simple.h"
0039 #include "tda10048.h"
0040 #include "tda18271.h"
0041 #include "lgdt3305.h"
0042 #include "tda8290.h"
0043 #include "mb86a20s.h"
0044 #include "lgs8gxx.h"
0045 
0046 #include "zl10353.h"
0047 #include "qt1010.h"
0048 
0049 #include "zl10036.h"
0050 #include "zl10039.h"
0051 #include "mt312.h"
0052 #include "s5h1411.h"
0053 
0054 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
0055 MODULE_LICENSE("GPL");
0056 
0057 static unsigned int antenna_pwr;
0058 
0059 module_param(antenna_pwr, int, 0444);
0060 MODULE_PARM_DESC(antenna_pwr,"enable antenna power (Pinnacle 300i)");
0061 
0062 static int use_frontend;
0063 module_param(use_frontend, int, 0644);
0064 MODULE_PARM_DESC(use_frontend,"for cards with multiple frontends (0: terrestrial, 1: satellite)");
0065 
0066 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
0067 
0068 /* ------------------------------------------------------------------
0069  * mt352 based DVB-T cards
0070  */
0071 
0072 static int pinnacle_antenna_pwr(struct saa7134_dev *dev, int on)
0073 {
0074     u32 ok;
0075 
0076     if (!on) {
0077         saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 26));
0078         saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
0079         return 0;
0080     }
0081 
0082     saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 26));
0083     saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 26));
0084     udelay(10);
0085 
0086     saa_setl(SAA7134_GPIO_GPMODE0 >> 2,     (1 << 28));
0087     saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 28));
0088     udelay(10);
0089     saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 28));
0090     udelay(10);
0091     ok = saa_readl(SAA7134_GPIO_GPSTATUS0) & (1 << 27);
0092     pr_debug("%s %s\n", __func__, ok ? "on" : "off");
0093 
0094     if (!ok)
0095         saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2,   (1 << 26));
0096     return ok;
0097 }
0098 
0099 static int mt352_pinnacle_init(struct dvb_frontend* fe)
0100 {
0101     static u8 clock_config []  = { CLOCK_CTL,  0x3d, 0x28 };
0102     static u8 reset []         = { RESET,      0x80 };
0103     static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
0104     static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0xa0 };
0105     static u8 capt_range_cfg[] = { CAPT_RANGE, 0x31 };
0106     static u8 fsm_ctl_cfg[]    = { 0x7b,       0x04 };
0107     static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x0f };
0108     static u8 scan_ctl_cfg []  = { SCAN_CTL,   0x0d };
0109     static u8 irq_cfg []       = { INTERRUPT_EN_0, 0x00, 0x00, 0x00, 0x00 };
0110 
0111     pr_debug("%s called\n", __func__);
0112 
0113     mt352_write(fe, clock_config,   sizeof(clock_config));
0114     udelay(200);
0115     mt352_write(fe, reset,          sizeof(reset));
0116     mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
0117     mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
0118     mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
0119     mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
0120 
0121     mt352_write(fe, fsm_ctl_cfg,    sizeof(fsm_ctl_cfg));
0122     mt352_write(fe, scan_ctl_cfg,   sizeof(scan_ctl_cfg));
0123     mt352_write(fe, irq_cfg,        sizeof(irq_cfg));
0124 
0125     return 0;
0126 }
0127 
0128 static int mt352_aver777_init(struct dvb_frontend* fe)
0129 {
0130     static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x2d };
0131     static u8 reset []         = { RESET,      0x80 };
0132     static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
0133     static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0xa0 };
0134     static u8 capt_range_cfg[] = { CAPT_RANGE, 0x33 };
0135 
0136     mt352_write(fe, clock_config,   sizeof(clock_config));
0137     udelay(200);
0138     mt352_write(fe, reset,          sizeof(reset));
0139     mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
0140     mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
0141     mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
0142 
0143     return 0;
0144 }
0145 
0146 static int mt352_avermedia_xc3028_init(struct dvb_frontend *fe)
0147 {
0148     static u8 clock_config []  = { CLOCK_CTL, 0x38, 0x2d };
0149     static u8 reset []         = { RESET, 0x80 };
0150     static u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
0151     static u8 agc_cfg []       = { AGC_TARGET, 0xe };
0152     static u8 capt_range_cfg[] = { CAPT_RANGE, 0x33 };
0153 
0154     mt352_write(fe, clock_config,   sizeof(clock_config));
0155     udelay(200);
0156     mt352_write(fe, reset,          sizeof(reset));
0157     mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
0158     mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
0159     mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
0160     return 0;
0161 }
0162 
0163 static int mt352_pinnacle_tuner_set_params(struct dvb_frontend *fe)
0164 {
0165     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0166     u8 off[] = { 0x00, 0xf1};
0167     u8 on[]  = { 0x00, 0x71};
0168     struct i2c_msg msg = {.addr=0x43, .flags=0, .buf=off, .len = sizeof(off)};
0169 
0170     struct saa7134_dev *dev = fe->dvb->priv;
0171     struct v4l2_frequency f;
0172 
0173     /* set frequency (mt2050) */
0174     f.tuner     = 0;
0175     f.type      = V4L2_TUNER_DIGITAL_TV;
0176     f.frequency = c->frequency / 1000 * 16 / 1000;
0177     if (fe->ops.i2c_gate_ctrl)
0178         fe->ops.i2c_gate_ctrl(fe, 1);
0179     i2c_transfer(&dev->i2c_adap, &msg, 1);
0180     saa_call_all(dev, tuner, s_frequency, &f);
0181     msg.buf = on;
0182     if (fe->ops.i2c_gate_ctrl)
0183         fe->ops.i2c_gate_ctrl(fe, 1);
0184     i2c_transfer(&dev->i2c_adap, &msg, 1);
0185 
0186     pinnacle_antenna_pwr(dev, antenna_pwr);
0187 
0188     /* mt352 setup */
0189     return mt352_pinnacle_init(fe);
0190 }
0191 
0192 static struct mt352_config pinnacle_300i = {
0193     .demod_address = 0x3c >> 1,
0194     .adc_clock     = 20333,
0195     .if2           = 36150,
0196     .no_tuner      = 1,
0197     .demod_init    = mt352_pinnacle_init,
0198 };
0199 
0200 static struct mt352_config avermedia_777 = {
0201     .demod_address = 0xf,
0202     .demod_init    = mt352_aver777_init,
0203 };
0204 
0205 static struct mt352_config avermedia_xc3028_mt352_dev = {
0206     .demod_address   = (0x1e >> 1),
0207     .no_tuner        = 1,
0208     .demod_init      = mt352_avermedia_xc3028_init,
0209 };
0210 
0211 static struct tda18271_std_map mb86a20s_tda18271_std_map = {
0212     .dvbt_6   = { .if_freq = 3300, .agc_mode = 3, .std = 4,
0213               .if_lvl = 7, .rfagc_top = 0x37, },
0214 };
0215 
0216 static struct tda18271_config kworld_tda18271_config = {
0217     .std_map = &mb86a20s_tda18271_std_map,
0218     .gate    = TDA18271_GATE_DIGITAL,
0219     .config  = 3,   /* Use tuner callback for AGC */
0220 
0221 };
0222 
0223 static const struct mb86a20s_config kworld_mb86a20s_config = {
0224     .demod_address = 0x10,
0225 };
0226 
0227 static int kworld_sbtvd_gate_ctrl(struct dvb_frontend* fe, int enable)
0228 {
0229     struct saa7134_dev *dev = fe->dvb->priv;
0230 
0231     unsigned char initmsg[] = {0x45, 0x97};
0232     unsigned char msg_enable[] = {0x45, 0xc1};
0233     unsigned char msg_disable[] = {0x45, 0x81};
0234     struct i2c_msg msg = {.addr = 0x4b, .flags = 0, .buf = initmsg, .len = 2};
0235 
0236     if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1) {
0237         pr_warn("could not access the I2C gate\n");
0238         return -EIO;
0239     }
0240     if (enable)
0241         msg.buf = msg_enable;
0242     else
0243         msg.buf = msg_disable;
0244     if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1) {
0245         pr_warn("could not access the I2C gate\n");
0246         return -EIO;
0247     }
0248     msleep(20);
0249     return 0;
0250 }
0251 
0252 /* ==================================================================
0253  * tda1004x based DVB-T cards, helper functions
0254  */
0255 
0256 static int philips_tda1004x_request_firmware(struct dvb_frontend *fe,
0257                        const struct firmware **fw, char *name)
0258 {
0259     struct saa7134_dev *dev = fe->dvb->priv;
0260     return request_firmware(fw, name, &dev->pci->dev);
0261 }
0262 
0263 /* ------------------------------------------------------------------
0264  * these tuners are tu1216, td1316(a)
0265  */
0266 
0267 static int philips_tda6651_pll_set(struct dvb_frontend *fe)
0268 {
0269     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0270     struct saa7134_dev *dev = fe->dvb->priv;
0271     struct tda1004x_state *state = fe->demodulator_priv;
0272     u8 addr = state->config->tuner_address;
0273     u8 tuner_buf[4];
0274     struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tuner_buf,.len =
0275             sizeof(tuner_buf) };
0276     int tuner_frequency = 0;
0277     u8 band, cp, filter;
0278 
0279     /* determine charge pump */
0280     tuner_frequency = c->frequency + 36166000;
0281     if (tuner_frequency < 87000000)
0282         return -EINVAL;
0283     else if (tuner_frequency < 130000000)
0284         cp = 3;
0285     else if (tuner_frequency < 160000000)
0286         cp = 5;
0287     else if (tuner_frequency < 200000000)
0288         cp = 6;
0289     else if (tuner_frequency < 290000000)
0290         cp = 3;
0291     else if (tuner_frequency < 420000000)
0292         cp = 5;
0293     else if (tuner_frequency < 480000000)
0294         cp = 6;
0295     else if (tuner_frequency < 620000000)
0296         cp = 3;
0297     else if (tuner_frequency < 830000000)
0298         cp = 5;
0299     else if (tuner_frequency < 895000000)
0300         cp = 7;
0301     else
0302         return -EINVAL;
0303 
0304     /* determine band */
0305     if (c->frequency < 49000000)
0306         return -EINVAL;
0307     else if (c->frequency < 161000000)
0308         band = 1;
0309     else if (c->frequency < 444000000)
0310         band = 2;
0311     else if (c->frequency < 861000000)
0312         band = 4;
0313     else
0314         return -EINVAL;
0315 
0316     /* setup PLL filter */
0317     switch (c->bandwidth_hz) {
0318     case 6000000:
0319         filter = 0;
0320         break;
0321 
0322     case 7000000:
0323         filter = 0;
0324         break;
0325 
0326     case 8000000:
0327         filter = 1;
0328         break;
0329 
0330     default:
0331         return -EINVAL;
0332     }
0333 
0334     /* calculate divisor
0335      * ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
0336      */
0337     tuner_frequency = (((c->frequency / 1000) * 6) + 217496) / 1000;
0338 
0339     /* setup tuner buffer */
0340     tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
0341     tuner_buf[1] = tuner_frequency & 0xff;
0342     tuner_buf[2] = 0xca;
0343     tuner_buf[3] = (cp << 5) | (filter << 3) | band;
0344 
0345     if (fe->ops.i2c_gate_ctrl)
0346         fe->ops.i2c_gate_ctrl(fe, 1);
0347     if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1) {
0348         pr_warn("could not write to tuner at addr: 0x%02x\n",
0349             addr << 1);
0350         return -EIO;
0351     }
0352     msleep(1);
0353     return 0;
0354 }
0355 
0356 static int philips_tu1216_init(struct dvb_frontend *fe)
0357 {
0358     struct saa7134_dev *dev = fe->dvb->priv;
0359     struct tda1004x_state *state = fe->demodulator_priv;
0360     u8 addr = state->config->tuner_address;
0361     static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
0362     struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
0363 
0364     /* setup PLL configuration */
0365     if (fe->ops.i2c_gate_ctrl)
0366         fe->ops.i2c_gate_ctrl(fe, 1);
0367     if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
0368         return -EIO;
0369     msleep(1);
0370 
0371     return 0;
0372 }
0373 
0374 /* ------------------------------------------------------------------ */
0375 
0376 static struct tda1004x_config philips_tu1216_60_config = {
0377     .demod_address = 0x8,
0378     .invert        = 1,
0379     .invert_oclk   = 0,
0380     .xtal_freq     = TDA10046_XTAL_4M,
0381     .agc_config    = TDA10046_AGC_DEFAULT,
0382     .if_freq       = TDA10046_FREQ_3617,
0383     .tuner_address = 0x60,
0384     .request_firmware = philips_tda1004x_request_firmware
0385 };
0386 
0387 static struct tda1004x_config philips_tu1216_61_config = {
0388 
0389     .demod_address = 0x8,
0390     .invert        = 1,
0391     .invert_oclk   = 0,
0392     .xtal_freq     = TDA10046_XTAL_4M,
0393     .agc_config    = TDA10046_AGC_DEFAULT,
0394     .if_freq       = TDA10046_FREQ_3617,
0395     .tuner_address = 0x61,
0396     .request_firmware = philips_tda1004x_request_firmware
0397 };
0398 
0399 /* ------------------------------------------------------------------ */
0400 
0401 static int philips_td1316_tuner_init(struct dvb_frontend *fe)
0402 {
0403     struct saa7134_dev *dev = fe->dvb->priv;
0404     struct tda1004x_state *state = fe->demodulator_priv;
0405     u8 addr = state->config->tuner_address;
0406     static u8 msg[] = { 0x0b, 0xf5, 0x86, 0xab };
0407     struct i2c_msg init_msg = {.addr = addr,.flags = 0,.buf = msg,.len = sizeof(msg) };
0408 
0409     /* setup PLL configuration */
0410     if (fe->ops.i2c_gate_ctrl)
0411         fe->ops.i2c_gate_ctrl(fe, 1);
0412     if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
0413         return -EIO;
0414     return 0;
0415 }
0416 
0417 static int philips_td1316_tuner_set_params(struct dvb_frontend *fe)
0418 {
0419     return philips_tda6651_pll_set(fe);
0420 }
0421 
0422 static int philips_td1316_tuner_sleep(struct dvb_frontend *fe)
0423 {
0424     struct saa7134_dev *dev = fe->dvb->priv;
0425     struct tda1004x_state *state = fe->demodulator_priv;
0426     u8 addr = state->config->tuner_address;
0427     static u8 msg[] = { 0x0b, 0xdc, 0x86, 0xa4 };
0428     struct i2c_msg analog_msg = {.addr = addr,.flags = 0,.buf = msg,.len = sizeof(msg) };
0429 
0430     /* switch the tuner to analog mode */
0431     if (fe->ops.i2c_gate_ctrl)
0432         fe->ops.i2c_gate_ctrl(fe, 1);
0433     if (i2c_transfer(&dev->i2c_adap, &analog_msg, 1) != 1)
0434         return -EIO;
0435     return 0;
0436 }
0437 
0438 /* ------------------------------------------------------------------ */
0439 
0440 static int philips_europa_tuner_init(struct dvb_frontend *fe)
0441 {
0442     struct saa7134_dev *dev = fe->dvb->priv;
0443     static u8 msg[] = { 0x00, 0x40};
0444     struct i2c_msg init_msg = {.addr = 0x43,.flags = 0,.buf = msg,.len = sizeof(msg) };
0445 
0446 
0447     if (philips_td1316_tuner_init(fe))
0448         return -EIO;
0449     msleep(1);
0450     if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
0451         return -EIO;
0452 
0453     return 0;
0454 }
0455 
0456 static int philips_europa_tuner_sleep(struct dvb_frontend *fe)
0457 {
0458     struct saa7134_dev *dev = fe->dvb->priv;
0459 
0460     static u8 msg[] = { 0x00, 0x14 };
0461     struct i2c_msg analog_msg = {.addr = 0x43,.flags = 0,.buf = msg,.len = sizeof(msg) };
0462 
0463     if (philips_td1316_tuner_sleep(fe))
0464         return -EIO;
0465 
0466     /* switch the board to analog mode */
0467     if (fe->ops.i2c_gate_ctrl)
0468         fe->ops.i2c_gate_ctrl(fe, 1);
0469     i2c_transfer(&dev->i2c_adap, &analog_msg, 1);
0470     return 0;
0471 }
0472 
0473 static int philips_europa_demod_sleep(struct dvb_frontend *fe)
0474 {
0475     struct saa7134_dev *dev = fe->dvb->priv;
0476 
0477     if (dev->original_demod_sleep)
0478         dev->original_demod_sleep(fe);
0479     fe->ops.i2c_gate_ctrl(fe, 1);
0480     return 0;
0481 }
0482 
0483 static struct tda1004x_config philips_europa_config = {
0484 
0485     .demod_address = 0x8,
0486     .invert        = 0,
0487     .invert_oclk   = 0,
0488     .xtal_freq     = TDA10046_XTAL_4M,
0489     .agc_config    = TDA10046_AGC_IFO_AUTO_POS,
0490     .if_freq       = TDA10046_FREQ_052,
0491     .tuner_address = 0x61,
0492     .request_firmware = philips_tda1004x_request_firmware
0493 };
0494 
0495 static struct tda1004x_config medion_cardbus = {
0496     .demod_address = 0x08,
0497     .invert        = 1,
0498     .invert_oclk   = 0,
0499     .xtal_freq     = TDA10046_XTAL_16M,
0500     .agc_config    = TDA10046_AGC_IFO_AUTO_NEG,
0501     .if_freq       = TDA10046_FREQ_3613,
0502     .tuner_address = 0x61,
0503     .request_firmware = philips_tda1004x_request_firmware
0504 };
0505 
0506 static struct tda1004x_config technotrend_budget_t3000_config = {
0507     .demod_address = 0x8,
0508     .invert        = 1,
0509     .invert_oclk   = 0,
0510     .xtal_freq     = TDA10046_XTAL_4M,
0511     .agc_config    = TDA10046_AGC_DEFAULT,
0512     .if_freq       = TDA10046_FREQ_3617,
0513     .tuner_address = 0x63,
0514     .request_firmware = philips_tda1004x_request_firmware
0515 };
0516 
0517 /* ------------------------------------------------------------------
0518  * tda 1004x based cards with philips silicon tuner
0519  */
0520 
0521 static int tda8290_i2c_gate_ctrl( struct dvb_frontend* fe, int enable)
0522 {
0523     struct tda1004x_state *state = fe->demodulator_priv;
0524 
0525     u8 addr = state->config->i2c_gate;
0526     static u8 tda8290_close[] = { 0x21, 0xc0};
0527     static u8 tda8290_open[]  = { 0x21, 0x80};
0528     struct i2c_msg tda8290_msg = {.addr = addr,.flags = 0, .len = 2};
0529     if (enable) {
0530         tda8290_msg.buf = tda8290_close;
0531     } else {
0532         tda8290_msg.buf = tda8290_open;
0533     }
0534     if (i2c_transfer(state->i2c, &tda8290_msg, 1) != 1) {
0535         pr_warn("could not access tda8290 I2C gate\n");
0536         return -EIO;
0537     }
0538     msleep(20);
0539     return 0;
0540 }
0541 
0542 static int philips_tda827x_tuner_init(struct dvb_frontend *fe)
0543 {
0544     struct saa7134_dev *dev = fe->dvb->priv;
0545     struct tda1004x_state *state = fe->demodulator_priv;
0546 
0547     switch (state->config->antenna_switch) {
0548     case 0:
0549         break;
0550     case 1:
0551         pr_debug("setting GPIO21 to 0 (TV antenna?)\n");
0552         saa7134_set_gpio(dev, 21, 0);
0553         break;
0554     case 2:
0555         pr_debug("setting GPIO21 to 1 (Radio antenna?)\n");
0556         saa7134_set_gpio(dev, 21, 1);
0557         break;
0558     }
0559     return 0;
0560 }
0561 
0562 static int philips_tda827x_tuner_sleep(struct dvb_frontend *fe)
0563 {
0564     struct saa7134_dev *dev = fe->dvb->priv;
0565     struct tda1004x_state *state = fe->demodulator_priv;
0566 
0567     switch (state->config->antenna_switch) {
0568     case 0:
0569         break;
0570     case 1:
0571         pr_debug("setting GPIO21 to 1 (Radio antenna?)\n");
0572         saa7134_set_gpio(dev, 21, 1);
0573         break;
0574     case 2:
0575         pr_debug("setting GPIO21 to 0 (TV antenna?)\n");
0576         saa7134_set_gpio(dev, 21, 0);
0577         break;
0578     }
0579     return 0;
0580 }
0581 
0582 static int configure_tda827x_fe(struct saa7134_dev *dev,
0583                 struct tda1004x_config *cdec_conf,
0584                 struct tda827x_config *tuner_conf)
0585 {
0586     struct vb2_dvb_frontend *fe0;
0587 
0588     /* Get the first frontend */
0589     fe0 = vb2_dvb_get_frontend(&dev->frontends, 1);
0590 
0591     if (!fe0)
0592         return -EINVAL;
0593 
0594     fe0->dvb.frontend = dvb_attach(tda10046_attach, cdec_conf, &dev->i2c_adap);
0595     if (fe0->dvb.frontend) {
0596         if (cdec_conf->i2c_gate)
0597             fe0->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
0598         if (dvb_attach(tda827x_attach, fe0->dvb.frontend,
0599                    cdec_conf->tuner_address,
0600                    &dev->i2c_adap, tuner_conf))
0601             return 0;
0602 
0603         pr_warn("no tda827x tuner found at addr: %02x\n",
0604                 cdec_conf->tuner_address);
0605     }
0606     return -EINVAL;
0607 }
0608 
0609 /* ------------------------------------------------------------------ */
0610 
0611 static struct tda827x_config tda827x_cfg_0 = {
0612     .init = philips_tda827x_tuner_init,
0613     .sleep = philips_tda827x_tuner_sleep,
0614     .config = 0,
0615     .switch_addr = 0
0616 };
0617 
0618 static struct tda827x_config tda827x_cfg_1 = {
0619     .init = philips_tda827x_tuner_init,
0620     .sleep = philips_tda827x_tuner_sleep,
0621     .config = 1,
0622     .switch_addr = 0x4b
0623 };
0624 
0625 static struct tda827x_config tda827x_cfg_2 = {
0626     .init = philips_tda827x_tuner_init,
0627     .sleep = philips_tda827x_tuner_sleep,
0628     .config = 2,
0629     .switch_addr = 0x4b
0630 };
0631 
0632 static struct tda827x_config tda827x_cfg_2_sw42 = {
0633     .init = philips_tda827x_tuner_init,
0634     .sleep = philips_tda827x_tuner_sleep,
0635     .config = 2,
0636     .switch_addr = 0x42
0637 };
0638 
0639 /* ------------------------------------------------------------------ */
0640 
0641 static struct tda1004x_config tda827x_lifeview_config = {
0642     .demod_address = 0x08,
0643     .invert        = 1,
0644     .invert_oclk   = 0,
0645     .xtal_freq     = TDA10046_XTAL_16M,
0646     .agc_config    = TDA10046_AGC_TDA827X,
0647     .gpio_config   = TDA10046_GP11_I,
0648     .if_freq       = TDA10046_FREQ_045,
0649     .tuner_address = 0x60,
0650     .request_firmware = philips_tda1004x_request_firmware
0651 };
0652 
0653 static struct tda1004x_config philips_tiger_config = {
0654     .demod_address = 0x08,
0655     .invert        = 1,
0656     .invert_oclk   = 0,
0657     .xtal_freq     = TDA10046_XTAL_16M,
0658     .agc_config    = TDA10046_AGC_TDA827X,
0659     .gpio_config   = TDA10046_GP11_I,
0660     .if_freq       = TDA10046_FREQ_045,
0661     .i2c_gate      = 0x4b,
0662     .tuner_address = 0x61,
0663     .antenna_switch= 1,
0664     .request_firmware = philips_tda1004x_request_firmware
0665 };
0666 
0667 static struct tda1004x_config cinergy_ht_config = {
0668     .demod_address = 0x08,
0669     .invert        = 1,
0670     .invert_oclk   = 0,
0671     .xtal_freq     = TDA10046_XTAL_16M,
0672     .agc_config    = TDA10046_AGC_TDA827X,
0673     .gpio_config   = TDA10046_GP01_I,
0674     .if_freq       = TDA10046_FREQ_045,
0675     .i2c_gate      = 0x4b,
0676     .tuner_address = 0x61,
0677     .request_firmware = philips_tda1004x_request_firmware
0678 };
0679 
0680 static struct tda1004x_config cinergy_ht_pci_config = {
0681     .demod_address = 0x08,
0682     .invert        = 1,
0683     .invert_oclk   = 0,
0684     .xtal_freq     = TDA10046_XTAL_16M,
0685     .agc_config    = TDA10046_AGC_TDA827X,
0686     .gpio_config   = TDA10046_GP01_I,
0687     .if_freq       = TDA10046_FREQ_045,
0688     .i2c_gate      = 0x4b,
0689     .tuner_address = 0x60,
0690     .request_firmware = philips_tda1004x_request_firmware
0691 };
0692 
0693 static struct tda1004x_config philips_tiger_s_config = {
0694     .demod_address = 0x08,
0695     .invert        = 1,
0696     .invert_oclk   = 0,
0697     .xtal_freq     = TDA10046_XTAL_16M,
0698     .agc_config    = TDA10046_AGC_TDA827X,
0699     .gpio_config   = TDA10046_GP01_I,
0700     .if_freq       = TDA10046_FREQ_045,
0701     .i2c_gate      = 0x4b,
0702     .tuner_address = 0x61,
0703     .antenna_switch= 1,
0704     .request_firmware = philips_tda1004x_request_firmware
0705 };
0706 
0707 static struct tda1004x_config pinnacle_pctv_310i_config = {
0708     .demod_address = 0x08,
0709     .invert        = 1,
0710     .invert_oclk   = 0,
0711     .xtal_freq     = TDA10046_XTAL_16M,
0712     .agc_config    = TDA10046_AGC_TDA827X,
0713     .gpio_config   = TDA10046_GP11_I,
0714     .if_freq       = TDA10046_FREQ_045,
0715     .i2c_gate      = 0x4b,
0716     .tuner_address = 0x61,
0717     .request_firmware = philips_tda1004x_request_firmware
0718 };
0719 
0720 static struct tda1004x_config hauppauge_hvr_1110_config = {
0721     .demod_address = 0x08,
0722     .invert        = 1,
0723     .invert_oclk   = 0,
0724     .xtal_freq     = TDA10046_XTAL_16M,
0725     .agc_config    = TDA10046_AGC_TDA827X,
0726     .gpio_config   = TDA10046_GP11_I,
0727     .if_freq       = TDA10046_FREQ_045,
0728     .i2c_gate      = 0x4b,
0729     .tuner_address = 0x61,
0730     .request_firmware = philips_tda1004x_request_firmware
0731 };
0732 
0733 static struct tda1004x_config asus_p7131_dual_config = {
0734     .demod_address = 0x08,
0735     .invert        = 1,
0736     .invert_oclk   = 0,
0737     .xtal_freq     = TDA10046_XTAL_16M,
0738     .agc_config    = TDA10046_AGC_TDA827X,
0739     .gpio_config   = TDA10046_GP11_I,
0740     .if_freq       = TDA10046_FREQ_045,
0741     .i2c_gate      = 0x4b,
0742     .tuner_address = 0x61,
0743     .antenna_switch= 2,
0744     .request_firmware = philips_tda1004x_request_firmware
0745 };
0746 
0747 static struct tda1004x_config lifeview_trio_config = {
0748     .demod_address = 0x09,
0749     .invert        = 1,
0750     .invert_oclk   = 0,
0751     .xtal_freq     = TDA10046_XTAL_16M,
0752     .agc_config    = TDA10046_AGC_TDA827X,
0753     .gpio_config   = TDA10046_GP00_I,
0754     .if_freq       = TDA10046_FREQ_045,
0755     .tuner_address = 0x60,
0756     .request_firmware = philips_tda1004x_request_firmware
0757 };
0758 
0759 static struct tda1004x_config tevion_dvbt220rf_config = {
0760     .demod_address = 0x08,
0761     .invert        = 1,
0762     .invert_oclk   = 0,
0763     .xtal_freq     = TDA10046_XTAL_16M,
0764     .agc_config    = TDA10046_AGC_TDA827X,
0765     .gpio_config   = TDA10046_GP11_I,
0766     .if_freq       = TDA10046_FREQ_045,
0767     .tuner_address = 0x60,
0768     .request_firmware = philips_tda1004x_request_firmware
0769 };
0770 
0771 static struct tda1004x_config md8800_dvbt_config = {
0772     .demod_address = 0x08,
0773     .invert        = 1,
0774     .invert_oclk   = 0,
0775     .xtal_freq     = TDA10046_XTAL_16M,
0776     .agc_config    = TDA10046_AGC_TDA827X,
0777     .gpio_config   = TDA10046_GP01_I,
0778     .if_freq       = TDA10046_FREQ_045,
0779     .i2c_gate      = 0x4b,
0780     .tuner_address = 0x60,
0781     .request_firmware = philips_tda1004x_request_firmware
0782 };
0783 
0784 static struct tda1004x_config asus_p7131_4871_config = {
0785     .demod_address = 0x08,
0786     .invert        = 1,
0787     .invert_oclk   = 0,
0788     .xtal_freq     = TDA10046_XTAL_16M,
0789     .agc_config    = TDA10046_AGC_TDA827X,
0790     .gpio_config   = TDA10046_GP01_I,
0791     .if_freq       = TDA10046_FREQ_045,
0792     .i2c_gate      = 0x4b,
0793     .tuner_address = 0x61,
0794     .antenna_switch= 2,
0795     .request_firmware = philips_tda1004x_request_firmware
0796 };
0797 
0798 static struct tda1004x_config asus_p7131_hybrid_lna_config = {
0799     .demod_address = 0x08,
0800     .invert        = 1,
0801     .invert_oclk   = 0,
0802     .xtal_freq     = TDA10046_XTAL_16M,
0803     .agc_config    = TDA10046_AGC_TDA827X,
0804     .gpio_config   = TDA10046_GP11_I,
0805     .if_freq       = TDA10046_FREQ_045,
0806     .i2c_gate      = 0x4b,
0807     .tuner_address = 0x61,
0808     .antenna_switch= 2,
0809     .request_firmware = philips_tda1004x_request_firmware
0810 };
0811 
0812 static struct tda1004x_config kworld_dvb_t_210_config = {
0813     .demod_address = 0x08,
0814     .invert        = 1,
0815     .invert_oclk   = 0,
0816     .xtal_freq     = TDA10046_XTAL_16M,
0817     .agc_config    = TDA10046_AGC_TDA827X,
0818     .gpio_config   = TDA10046_GP11_I,
0819     .if_freq       = TDA10046_FREQ_045,
0820     .i2c_gate      = 0x4b,
0821     .tuner_address = 0x61,
0822     .antenna_switch= 1,
0823     .request_firmware = philips_tda1004x_request_firmware
0824 };
0825 
0826 static struct tda1004x_config avermedia_super_007_config = {
0827     .demod_address = 0x08,
0828     .invert        = 1,
0829     .invert_oclk   = 0,
0830     .xtal_freq     = TDA10046_XTAL_16M,
0831     .agc_config    = TDA10046_AGC_TDA827X,
0832     .gpio_config   = TDA10046_GP01_I,
0833     .if_freq       = TDA10046_FREQ_045,
0834     .i2c_gate      = 0x4b,
0835     .tuner_address = 0x60,
0836     .antenna_switch= 1,
0837     .request_firmware = philips_tda1004x_request_firmware
0838 };
0839 
0840 static struct tda1004x_config twinhan_dtv_dvb_3056_config = {
0841     .demod_address = 0x08,
0842     .invert        = 1,
0843     .invert_oclk   = 0,
0844     .xtal_freq     = TDA10046_XTAL_16M,
0845     .agc_config    = TDA10046_AGC_TDA827X,
0846     .gpio_config   = TDA10046_GP01_I,
0847     .if_freq       = TDA10046_FREQ_045,
0848     .i2c_gate      = 0x42,
0849     .tuner_address = 0x61,
0850     .antenna_switch = 1,
0851     .request_firmware = philips_tda1004x_request_firmware
0852 };
0853 
0854 static struct tda1004x_config asus_tiger_3in1_config = {
0855     .demod_address = 0x0b,
0856     .invert        = 1,
0857     .invert_oclk   = 0,
0858     .xtal_freq     = TDA10046_XTAL_16M,
0859     .agc_config    = TDA10046_AGC_TDA827X,
0860     .gpio_config   = TDA10046_GP11_I,
0861     .if_freq       = TDA10046_FREQ_045,
0862     .i2c_gate      = 0x4b,
0863     .tuner_address = 0x61,
0864     .antenna_switch = 1,
0865     .request_firmware = philips_tda1004x_request_firmware
0866 };
0867 
0868 static struct tda1004x_config asus_ps3_100_config = {
0869     .demod_address = 0x0b,
0870     .invert        = 1,
0871     .invert_oclk   = 0,
0872     .xtal_freq     = TDA10046_XTAL_16M,
0873     .agc_config    = TDA10046_AGC_TDA827X,
0874     .gpio_config   = TDA10046_GP11_I,
0875     .if_freq       = TDA10046_FREQ_045,
0876     .i2c_gate      = 0x4b,
0877     .tuner_address = 0x61,
0878     .antenna_switch = 1,
0879     .request_firmware = philips_tda1004x_request_firmware
0880 };
0881 
0882 /* ------------------------------------------------------------------
0883  * special case: this card uses saa713x GPIO22 for the mode switch
0884  */
0885 
0886 static int ads_duo_tuner_init(struct dvb_frontend *fe)
0887 {
0888     struct saa7134_dev *dev = fe->dvb->priv;
0889     philips_tda827x_tuner_init(fe);
0890     /* route TDA8275a AGC input to the channel decoder */
0891     saa7134_set_gpio(dev, 22, 1);
0892     return 0;
0893 }
0894 
0895 static int ads_duo_tuner_sleep(struct dvb_frontend *fe)
0896 {
0897     struct saa7134_dev *dev = fe->dvb->priv;
0898     /* route TDA8275a AGC input to the analog IF chip*/
0899     saa7134_set_gpio(dev, 22, 0);
0900     philips_tda827x_tuner_sleep(fe);
0901     return 0;
0902 }
0903 
0904 static struct tda827x_config ads_duo_cfg = {
0905     .init = ads_duo_tuner_init,
0906     .sleep = ads_duo_tuner_sleep,
0907     .config = 0
0908 };
0909 
0910 static struct tda1004x_config ads_tech_duo_config = {
0911     .demod_address = 0x08,
0912     .invert        = 1,
0913     .invert_oclk   = 0,
0914     .xtal_freq     = TDA10046_XTAL_16M,
0915     .agc_config    = TDA10046_AGC_TDA827X,
0916     .gpio_config   = TDA10046_GP00_I,
0917     .if_freq       = TDA10046_FREQ_045,
0918     .tuner_address = 0x61,
0919     .request_firmware = philips_tda1004x_request_firmware
0920 };
0921 
0922 static struct zl10353_config behold_h6_config = {
0923     .demod_address = 0x1e>>1,
0924     .no_tuner      = 1,
0925     .parallel_ts   = 1,
0926     .disable_i2c_gate_ctrl = 1,
0927 };
0928 
0929 static struct xc5000_config behold_x7_tunerconfig = {
0930     .i2c_address      = 0xc2>>1,
0931     .if_khz           = 4560,
0932     .radio_input      = XC5000_RADIO_FM1,
0933 };
0934 
0935 static struct zl10353_config behold_x7_config = {
0936     .demod_address = 0x1e>>1,
0937     .if2           = 45600,
0938     .no_tuner      = 1,
0939     .parallel_ts   = 1,
0940     .disable_i2c_gate_ctrl = 1,
0941 };
0942 
0943 static struct zl10353_config videomate_t750_zl10353_config = {
0944     .demod_address         = 0x0f,
0945     .no_tuner              = 1,
0946     .parallel_ts           = 1,
0947     .disable_i2c_gate_ctrl = 1,
0948 };
0949 
0950 static struct qt1010_config videomate_t750_qt1010_config = {
0951     .i2c_address = 0x62
0952 };
0953 
0954 
0955 /* ==================================================================
0956  * tda10086 based DVB-S cards, helper functions
0957  */
0958 
0959 static struct tda10086_config flydvbs = {
0960     .demod_address = 0x0e,
0961     .invert = 0,
0962     .diseqc_tone = 0,
0963     .xtal_freq = TDA10086_XTAL_16M,
0964 };
0965 
0966 static struct tda10086_config sd1878_4m = {
0967     .demod_address = 0x0e,
0968     .invert = 0,
0969     .diseqc_tone = 0,
0970     .xtal_freq = TDA10086_XTAL_4M,
0971 };
0972 
0973 /* ------------------------------------------------------------------
0974  * special case: lnb supply is connected to the gated i2c
0975  */
0976 
0977 static int md8800_set_voltage(struct dvb_frontend *fe,
0978                   enum fe_sec_voltage voltage)
0979 {
0980     int res = -EIO;
0981     struct saa7134_dev *dev = fe->dvb->priv;
0982     if (fe->ops.i2c_gate_ctrl) {
0983         fe->ops.i2c_gate_ctrl(fe, 1);
0984         if (dev->original_set_voltage)
0985             res = dev->original_set_voltage(fe, voltage);
0986         fe->ops.i2c_gate_ctrl(fe, 0);
0987     }
0988     return res;
0989 };
0990 
0991 static int md8800_set_high_voltage(struct dvb_frontend *fe, long arg)
0992 {
0993     int res = -EIO;
0994     struct saa7134_dev *dev = fe->dvb->priv;
0995     if (fe->ops.i2c_gate_ctrl) {
0996         fe->ops.i2c_gate_ctrl(fe, 1);
0997         if (dev->original_set_high_voltage)
0998             res = dev->original_set_high_voltage(fe, arg);
0999         fe->ops.i2c_gate_ctrl(fe, 0);
1000     }
1001     return res;
1002 };
1003 
1004 static int md8800_set_voltage2(struct dvb_frontend *fe,
1005                    enum fe_sec_voltage voltage)
1006 {
1007     struct saa7134_dev *dev = fe->dvb->priv;
1008     u8 wbuf[2] = { 0x1f, 00 };
1009     u8 rbuf;
1010     struct i2c_msg msg[] = { { .addr = 0x08, .flags = 0, .buf = wbuf, .len = 1 },
1011                  { .addr = 0x08, .flags = I2C_M_RD, .buf = &rbuf, .len = 1 } };
1012 
1013     if (i2c_transfer(&dev->i2c_adap, msg, 2) != 2)
1014         return -EIO;
1015     /* NOTE: this assumes that gpo1 is used, it might be bit 5 (gpo2) */
1016     if (voltage == SEC_VOLTAGE_18)
1017         wbuf[1] = rbuf | 0x10;
1018     else
1019         wbuf[1] = rbuf & 0xef;
1020     msg[0].len = 2;
1021     i2c_transfer(&dev->i2c_adap, msg, 1);
1022     return 0;
1023 }
1024 
1025 static int md8800_set_high_voltage2(struct dvb_frontend *fe, long arg)
1026 {
1027     pr_warn("%s: sorry can't set high LNB supply voltage from here\n",
1028         __func__);
1029     return -EIO;
1030 }
1031 
1032 /* ==================================================================
1033  * nxt200x based ATSC cards, helper functions
1034  */
1035 
1036 static const struct nxt200x_config avertvhda180 = {
1037     .demod_address    = 0x0a,
1038 };
1039 
1040 static const struct nxt200x_config kworldatsc110 = {
1041     .demod_address    = 0x0a,
1042 };
1043 
1044 /* ------------------------------------------------------------------ */
1045 
1046 static struct mt312_config avertv_a700_mt312 = {
1047     .demod_address = 0x0e,
1048     .voltage_inverted = 1,
1049 };
1050 
1051 static struct zl10036_config avertv_a700_tuner = {
1052     .tuner_address = 0x60,
1053 };
1054 
1055 static struct mt312_config zl10313_compro_s350_config = {
1056     .demod_address = 0x0e,
1057 };
1058 
1059 static struct mt312_config zl10313_avermedia_a706_config = {
1060     .demod_address = 0x0e,
1061 };
1062 
1063 static struct lgdt3305_config hcw_lgdt3305_config = {
1064     .i2c_addr           = 0x0e,
1065     .mpeg_mode          = LGDT3305_MPEG_SERIAL,
1066     .tpclk_edge         = LGDT3305_TPCLK_RISING_EDGE,
1067     .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
1068     .deny_i2c_rptr      = 1,
1069     .spectral_inversion = 1,
1070     .qam_if_khz         = 4000,
1071     .vsb_if_khz         = 3250,
1072 };
1073 
1074 static struct tda10048_config hcw_tda10048_config = {
1075     .demod_address    = 0x10 >> 1,
1076     .output_mode      = TDA10048_SERIAL_OUTPUT,
1077     .fwbulkwritelen   = TDA10048_BULKWRITE_200,
1078     .inversion        = TDA10048_INVERSION_ON,
1079     .dtv6_if_freq_khz = TDA10048_IF_3300,
1080     .dtv7_if_freq_khz = TDA10048_IF_3500,
1081     .dtv8_if_freq_khz = TDA10048_IF_4000,
1082     .clk_freq_khz     = TDA10048_CLK_16000,
1083     .disable_gate_access = 1,
1084 };
1085 
1086 static struct tda18271_std_map hauppauge_tda18271_std_map = {
1087     .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 4,
1088               .if_lvl = 1, .rfagc_top = 0x58, },
1089     .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 5,
1090               .if_lvl = 1, .rfagc_top = 0x58, },
1091 };
1092 
1093 static struct tda18271_config hcw_tda18271_config = {
1094     .std_map = &hauppauge_tda18271_std_map,
1095     .gate    = TDA18271_GATE_ANALOG,
1096     .config  = 3,
1097     .output_opt = TDA18271_OUTPUT_LT_OFF,
1098 };
1099 
1100 static struct tda829x_config tda829x_no_probe = {
1101     .probe_tuner = TDA829X_DONT_PROBE,
1102 };
1103 
1104 static struct tda10048_config zolid_tda10048_config = {
1105     .demod_address    = 0x10 >> 1,
1106     .output_mode      = TDA10048_PARALLEL_OUTPUT,
1107     .fwbulkwritelen   = TDA10048_BULKWRITE_200,
1108     .inversion        = TDA10048_INVERSION_ON,
1109     .dtv6_if_freq_khz = TDA10048_IF_3300,
1110     .dtv7_if_freq_khz = TDA10048_IF_3500,
1111     .dtv8_if_freq_khz = TDA10048_IF_4000,
1112     .clk_freq_khz     = TDA10048_CLK_16000,
1113     .disable_gate_access = 1,
1114 };
1115 
1116 static struct tda18271_config zolid_tda18271_config = {
1117     .gate    = TDA18271_GATE_ANALOG,
1118 };
1119 
1120 static struct tda10048_config dtv1000s_tda10048_config = {
1121     .demod_address    = 0x10 >> 1,
1122     .output_mode      = TDA10048_PARALLEL_OUTPUT,
1123     .fwbulkwritelen   = TDA10048_BULKWRITE_200,
1124     .inversion        = TDA10048_INVERSION_ON,
1125     .dtv6_if_freq_khz = TDA10048_IF_3300,
1126     .dtv7_if_freq_khz = TDA10048_IF_3800,
1127     .dtv8_if_freq_khz = TDA10048_IF_4300,
1128     .clk_freq_khz     = TDA10048_CLK_16000,
1129     .disable_gate_access = 1,
1130 };
1131 
1132 static struct tda18271_std_map dtv1000s_tda18271_std_map = {
1133     .dvbt_6   = { .if_freq = 3300, .agc_mode = 3, .std = 4,
1134               .if_lvl = 1, .rfagc_top = 0x37, },
1135     .dvbt_7   = { .if_freq = 3800, .agc_mode = 3, .std = 5,
1136               .if_lvl = 1, .rfagc_top = 0x37, },
1137     .dvbt_8   = { .if_freq = 4300, .agc_mode = 3, .std = 6,
1138               .if_lvl = 1, .rfagc_top = 0x37, },
1139 };
1140 
1141 static struct tda18271_config dtv1000s_tda18271_config = {
1142     .std_map = &dtv1000s_tda18271_std_map,
1143     .gate    = TDA18271_GATE_ANALOG,
1144 };
1145 
1146 static struct lgs8gxx_config prohdtv_pro2_lgs8g75_config = {
1147     .prod = LGS8GXX_PROD_LGS8G75,
1148     .demod_address = 0x1d,
1149     .serial_ts = 0,
1150     .ts_clk_pol = 1,
1151     .ts_clk_gated = 0,
1152     .if_clk_freq = 30400, /* 30.4 MHz */
1153     .if_freq = 4000, /* 4.00 MHz */
1154     .if_neg_center = 0,
1155     .ext_adc = 0,
1156     .adc_signed = 1,
1157     .adc_vpp = 3, /* 2.0 Vpp */
1158     .if_neg_edge = 1,
1159 };
1160 
1161 static struct tda18271_config prohdtv_pro2_tda18271_config = {
1162     .gate = TDA18271_GATE_ANALOG,
1163     .output_opt = TDA18271_OUTPUT_LT_OFF,
1164 };
1165 
1166 static struct tda18271_std_map kworld_tda18271_std_map = {
1167     .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 3,
1168               .if_lvl = 6, .rfagc_top = 0x37 },
1169     .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 0,
1170               .if_lvl = 6, .rfagc_top = 0x37 },
1171 };
1172 
1173 static struct tda18271_config kworld_pc150u_tda18271_config = {
1174     .std_map = &kworld_tda18271_std_map,
1175     .gate    = TDA18271_GATE_ANALOG,
1176     .output_opt = TDA18271_OUTPUT_LT_OFF,
1177     .config  = 3,   /* Use tuner callback for AGC */
1178     .rf_cal_on_startup = 1
1179 };
1180 
1181 static struct s5h1411_config kworld_s5h1411_config = {
1182     .output_mode   = S5H1411_PARALLEL_OUTPUT,
1183     .gpio          = S5H1411_GPIO_OFF,
1184     .qam_if        = S5H1411_IF_4000,
1185     .vsb_if        = S5H1411_IF_3250,
1186     .inversion     = S5H1411_INVERSION_ON,
1187     .status_mode   = S5H1411_DEMODLOCKING,
1188     .mpeg_timing   =
1189         S5H1411_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
1190 };
1191 
1192 static struct tda18271_config hdtv200h_tda18271_config = {
1193     .gate    = TDA18271_GATE_ANALOG,
1194     .config  = 3    /* Use tuner callback for AGC */
1195 };
1196 
1197 static struct s5h1411_config hdtv200h_s5h1411_config = {
1198     .output_mode   = S5H1411_PARALLEL_OUTPUT,
1199     .gpio          = S5H1411_GPIO_OFF,
1200     .qam_if        = S5H1411_IF_4000,
1201     .vsb_if        = S5H1411_IF_3250,
1202     .inversion     = S5H1411_INVERSION_ON,
1203     .status_mode   = S5H1411_DEMODLOCKING,
1204     .mpeg_timing   =
1205         S5H1411_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
1206 };
1207 
1208 
1209 /* ==================================================================
1210  * Core code
1211  */
1212 
1213 static int dvb_init(struct saa7134_dev *dev)
1214 {
1215     int ret;
1216     int attach_xc3028 = 0;
1217     struct vb2_dvb_frontend *fe0;
1218     struct vb2_queue *q;
1219 
1220     /* FIXME: add support for multi-frontend */
1221     mutex_init(&dev->frontends.lock);
1222     INIT_LIST_HEAD(&dev->frontends.felist);
1223 
1224     pr_info("%s() allocating 1 frontend\n", __func__);
1225     fe0 = vb2_dvb_alloc_frontend(&dev->frontends, 1);
1226     if (!fe0) {
1227         pr_err("%s() failed to alloc\n", __func__);
1228         return -ENOMEM;
1229     }
1230 
1231     /* init struct vb2_dvb */
1232     dev->ts.nr_bufs    = 32;
1233     dev->ts.nr_packets = 32*4;
1234     fe0->dvb.name = dev->name;
1235     q = &fe0->dvb.dvbq;
1236     q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1237     q->io_modes = VB2_MMAP | VB2_READ;
1238     q->drv_priv = &dev->ts_q;
1239     q->ops = &saa7134_ts_qops;
1240     q->mem_ops = &vb2_dma_sg_memops;
1241     q->buf_struct_size = sizeof(struct saa7134_buf);
1242     q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1243     q->lock = &dev->lock;
1244     q->dev = &dev->pci->dev;
1245     ret = vb2_queue_init(q);
1246     if (ret) {
1247         vb2_dvb_dealloc_frontends(&dev->frontends);
1248         return ret;
1249     }
1250 
1251     switch (dev->board) {
1252     case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1253         pr_debug("pinnacle 300i dvb setup\n");
1254         fe0->dvb.frontend = dvb_attach(mt352_attach, &pinnacle_300i,
1255                            &dev->i2c_adap);
1256         if (fe0->dvb.frontend) {
1257             fe0->dvb.frontend->ops.tuner_ops.set_params = mt352_pinnacle_tuner_set_params;
1258         }
1259         break;
1260     case SAA7134_BOARD_AVERMEDIA_777:
1261     case SAA7134_BOARD_AVERMEDIA_A16AR:
1262         pr_debug("avertv 777 dvb setup\n");
1263         fe0->dvb.frontend = dvb_attach(mt352_attach, &avermedia_777,
1264                            &dev->i2c_adap);
1265         if (fe0->dvb.frontend) {
1266             dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1267                    &dev->i2c_adap, 0x61,
1268                    TUNER_PHILIPS_TD1316);
1269         }
1270         break;
1271     case SAA7134_BOARD_AVERMEDIA_A16D:
1272         pr_debug("AverMedia A16D dvb setup\n");
1273         fe0->dvb.frontend = dvb_attach(mt352_attach,
1274                         &avermedia_xc3028_mt352_dev,
1275                         &dev->i2c_adap);
1276         attach_xc3028 = 1;
1277         break;
1278     case SAA7134_BOARD_MD7134:
1279         fe0->dvb.frontend = dvb_attach(tda10046_attach,
1280                            &medion_cardbus,
1281                            &dev->i2c_adap);
1282         if (fe0->dvb.frontend) {
1283             /*
1284              * The TV tuner on this board is actually NOT
1285              * behind the demod i2c gate.
1286              * However, the demod EEPROM is indeed there and it
1287              * conflicts with the SAA7134 chip config EEPROM
1288              * if the i2c gate is open (since they have same
1289              * bus addresses) resulting in card PCI SVID / SSID
1290              * being garbage after a reboot from time to time.
1291              *
1292              * Let's just leave the gate permanently closed -
1293              * saa7134_i2c_eeprom_md7134_gate() will close it for
1294              * us at probe time if it was open for some reason.
1295              */
1296             fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1297             dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1298                    &dev->i2c_adap, medion_cardbus.tuner_address,
1299                    TUNER_PHILIPS_FMD1216ME_MK3);
1300         }
1301         break;
1302     case SAA7134_BOARD_PHILIPS_TOUGH:
1303         fe0->dvb.frontend = dvb_attach(tda10046_attach,
1304                            &philips_tu1216_60_config,
1305                            &dev->i2c_adap);
1306         if (fe0->dvb.frontend) {
1307             fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1308             fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1309         }
1310         break;
1311     case SAA7134_BOARD_FLYDVBTDUO:
1312     case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
1313         if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1314                      &tda827x_cfg_0) < 0)
1315             goto detach_frontend;
1316         break;
1317     case SAA7134_BOARD_PHILIPS_EUROPA:
1318     case SAA7134_BOARD_VIDEOMATE_DVBT_300:
1319     case SAA7134_BOARD_ASUS_EUROPA_HYBRID:
1320         fe0->dvb.frontend = dvb_attach(tda10046_attach,
1321                            &philips_europa_config,
1322                            &dev->i2c_adap);
1323         if (fe0->dvb.frontend) {
1324             dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1325             fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1326             fe0->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1327             fe0->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1328             fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1329         }
1330         break;
1331     case SAA7134_BOARD_TECHNOTREND_BUDGET_T3000:
1332         fe0->dvb.frontend = dvb_attach(tda10046_attach,
1333                            &technotrend_budget_t3000_config,
1334                            &dev->i2c_adap);
1335         if (fe0->dvb.frontend) {
1336             dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1337             fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1338             fe0->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1339             fe0->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1340             fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1341         }
1342         break;
1343     case SAA7134_BOARD_VIDEOMATE_DVBT_200:
1344         fe0->dvb.frontend = dvb_attach(tda10046_attach,
1345                            &philips_tu1216_61_config,
1346                            &dev->i2c_adap);
1347         if (fe0->dvb.frontend) {
1348             fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1349             fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1350         }
1351         break;
1352     case SAA7134_BOARD_KWORLD_DVBT_210:
1353         if (configure_tda827x_fe(dev, &kworld_dvb_t_210_config,
1354                      &tda827x_cfg_2) < 0)
1355             goto detach_frontend;
1356         break;
1357     case SAA7134_BOARD_HAUPPAUGE_HVR1120:
1358         fe0->dvb.frontend = dvb_attach(tda10048_attach,
1359                            &hcw_tda10048_config,
1360                            &dev->i2c_adap);
1361         if (fe0->dvb.frontend != NULL) {
1362             dvb_attach(tda829x_attach, fe0->dvb.frontend,
1363                    &dev->i2c_adap, 0x4b,
1364                    &tda829x_no_probe);
1365             dvb_attach(tda18271_attach, fe0->dvb.frontend,
1366                    0x60, &dev->i2c_adap,
1367                    &hcw_tda18271_config);
1368         }
1369         break;
1370     case SAA7134_BOARD_PHILIPS_TIGER:
1371         if (configure_tda827x_fe(dev, &philips_tiger_config,
1372                      &tda827x_cfg_0) < 0)
1373             goto detach_frontend;
1374         break;
1375     case SAA7134_BOARD_PINNACLE_PCTV_310i:
1376         if (configure_tda827x_fe(dev, &pinnacle_pctv_310i_config,
1377                      &tda827x_cfg_1) < 0)
1378             goto detach_frontend;
1379         break;
1380     case SAA7134_BOARD_HAUPPAUGE_HVR1110:
1381         if (configure_tda827x_fe(dev, &hauppauge_hvr_1110_config,
1382                      &tda827x_cfg_1) < 0)
1383             goto detach_frontend;
1384         break;
1385     case SAA7134_BOARD_HAUPPAUGE_HVR1150:
1386         fe0->dvb.frontend = dvb_attach(lgdt3305_attach,
1387                            &hcw_lgdt3305_config,
1388                            &dev->i2c_adap);
1389         if (fe0->dvb.frontend) {
1390             dvb_attach(tda829x_attach, fe0->dvb.frontend,
1391                    &dev->i2c_adap, 0x4b,
1392                    &tda829x_no_probe);
1393             dvb_attach(tda18271_attach, fe0->dvb.frontend,
1394                    0x60, &dev->i2c_adap,
1395                    &hcw_tda18271_config);
1396         }
1397         break;
1398     case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
1399         if (configure_tda827x_fe(dev, &asus_p7131_dual_config,
1400                      &tda827x_cfg_0) < 0)
1401             goto detach_frontend;
1402         break;
1403     case SAA7134_BOARD_FLYDVBT_LR301:
1404         if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1405                      &tda827x_cfg_0) < 0)
1406             goto detach_frontend;
1407         break;
1408     case SAA7134_BOARD_FLYDVB_TRIO:
1409         if (!use_frontend) {    /* terrestrial */
1410             if (configure_tda827x_fe(dev, &lifeview_trio_config,
1411                          &tda827x_cfg_0) < 0)
1412                 goto detach_frontend;
1413         } else {        /* satellite */
1414             fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs, &dev->i2c_adap);
1415             if (fe0->dvb.frontend) {
1416                 if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x63,
1417                                     &dev->i2c_adap, 0) == NULL) {
1418                     pr_warn("%s: Lifeview Trio, No tda826x found!\n",
1419                         __func__);
1420                     goto detach_frontend;
1421                 }
1422                 if (dvb_attach(isl6421_attach, fe0->dvb.frontend,
1423                            &dev->i2c_adap,
1424                            0x08, 0, 0, false) == NULL) {
1425                     pr_warn("%s: Lifeview Trio, No ISL6421 found!\n",
1426                         __func__);
1427                     goto detach_frontend;
1428                 }
1429             }
1430         }
1431         break;
1432     case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
1433     case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
1434         fe0->dvb.frontend = dvb_attach(tda10046_attach,
1435                            &ads_tech_duo_config,
1436                            &dev->i2c_adap);
1437         if (fe0->dvb.frontend) {
1438             if (dvb_attach(tda827x_attach,fe0->dvb.frontend,
1439                    ads_tech_duo_config.tuner_address, &dev->i2c_adap,
1440                                 &ads_duo_cfg) == NULL) {
1441                 pr_warn("no tda827x tuner found at addr: %02x\n",
1442                     ads_tech_duo_config.tuner_address);
1443                 goto detach_frontend;
1444             }
1445         } else
1446             pr_warn("failed to attach tda10046\n");
1447         break;
1448     case SAA7134_BOARD_TEVION_DVBT_220RF:
1449         if (configure_tda827x_fe(dev, &tevion_dvbt220rf_config,
1450                      &tda827x_cfg_0) < 0)
1451             goto detach_frontend;
1452         break;
1453     case SAA7134_BOARD_MEDION_MD8800_QUADRO:
1454         if (!use_frontend) {     /* terrestrial */
1455             if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1456                          &tda827x_cfg_0) < 0)
1457                 goto detach_frontend;
1458         } else {        /* satellite */
1459             fe0->dvb.frontend = dvb_attach(tda10086_attach,
1460                             &flydvbs, &dev->i2c_adap);
1461             if (fe0->dvb.frontend) {
1462                 struct dvb_frontend *fe = fe0->dvb.frontend;
1463                 u8 dev_id = dev->eedata[2];
1464                 u8 data = 0xc4;
1465                 struct i2c_msg msg = {.addr = 0x08, .flags = 0, .len = 1};
1466 
1467                 if (dvb_attach(tda826x_attach, fe0->dvb.frontend,
1468                         0x60, &dev->i2c_adap, 0) == NULL) {
1469                     pr_warn("%s: Medion Quadro, no tda826x found !\n",
1470                         __func__);
1471                     goto detach_frontend;
1472                 }
1473                 if (dev_id != 0x08) {
1474                     /* we need to open the i2c gate (we know it exists) */
1475                     fe->ops.i2c_gate_ctrl(fe, 1);
1476                     if (dvb_attach(isl6405_attach, fe,
1477                             &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1478                         pr_warn("%s: Medion Quadro, no ISL6405 found !\n",
1479                             __func__);
1480                         goto detach_frontend;
1481                     }
1482                     if (dev_id == 0x07) {
1483                         /* fire up the 2nd section of the LNB supply since
1484                            we can't do this from the other section */
1485                         msg.buf = &data;
1486                         i2c_transfer(&dev->i2c_adap, &msg, 1);
1487                     }
1488                     fe->ops.i2c_gate_ctrl(fe, 0);
1489                     dev->original_set_voltage = fe->ops.set_voltage;
1490                     fe->ops.set_voltage = md8800_set_voltage;
1491                     dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1492                     fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1493                 } else {
1494                     fe->ops.set_voltage = md8800_set_voltage2;
1495                     fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage2;
1496                 }
1497             }
1498         }
1499         break;
1500     case SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180:
1501         fe0->dvb.frontend = dvb_attach(nxt200x_attach, &avertvhda180,
1502                            &dev->i2c_adap);
1503         if (fe0->dvb.frontend)
1504             dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x61,
1505                    NULL, DVB_PLL_TDHU2);
1506         break;
1507     case SAA7134_BOARD_ADS_INSTANT_HDTV_PCI:
1508     case SAA7134_BOARD_KWORLD_ATSC110:
1509         fe0->dvb.frontend = dvb_attach(nxt200x_attach, &kworldatsc110,
1510                            &dev->i2c_adap);
1511         if (fe0->dvb.frontend)
1512             dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1513                    &dev->i2c_adap, 0x61,
1514                    TUNER_PHILIPS_TUV1236D);
1515         break;
1516     case SAA7134_BOARD_KWORLD_PC150U:
1517         saa7134_set_gpio(dev, 18, 1); /* Switch to digital mode */
1518         saa7134_tuner_callback(dev, 0,
1519                        TDA18271_CALLBACK_CMD_AGC_ENABLE, 1);
1520         fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1521                            &kworld_s5h1411_config,
1522                            &dev->i2c_adap);
1523         if (fe0->dvb.frontend != NULL) {
1524             dvb_attach(tda829x_attach, fe0->dvb.frontend,
1525                    &dev->i2c_adap, 0x4b,
1526                    &tda829x_no_probe);
1527             dvb_attach(tda18271_attach, fe0->dvb.frontend,
1528                    0x60, &dev->i2c_adap,
1529                    &kworld_pc150u_tda18271_config);
1530         }
1531         break;
1532     case SAA7134_BOARD_FLYDVBS_LR300:
1533         fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1534                            &dev->i2c_adap);
1535         if (fe0->dvb.frontend) {
1536             if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1537                        &dev->i2c_adap, 0) == NULL) {
1538                 pr_warn("%s: No tda826x found!\n", __func__);
1539                 goto detach_frontend;
1540             }
1541             if (dvb_attach(isl6421_attach, fe0->dvb.frontend,
1542                        &dev->i2c_adap,
1543                        0x08, 0, 0, false) == NULL) {
1544                 pr_warn("%s: No ISL6421 found!\n", __func__);
1545                 goto detach_frontend;
1546             }
1547         }
1548         break;
1549     case SAA7134_BOARD_ASUS_EUROPA2_HYBRID:
1550         fe0->dvb.frontend = dvb_attach(tda10046_attach,
1551                            &medion_cardbus,
1552                            &dev->i2c_adap);
1553         if (fe0->dvb.frontend) {
1554             dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1555             fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1556 
1557             dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1558                    &dev->i2c_adap, medion_cardbus.tuner_address,
1559                    TUNER_PHILIPS_FMD1216ME_MK3);
1560         }
1561         break;
1562     case SAA7134_BOARD_VIDEOMATE_DVBT_200A:
1563         fe0->dvb.frontend = dvb_attach(tda10046_attach,
1564                 &philips_europa_config,
1565                 &dev->i2c_adap);
1566         if (fe0->dvb.frontend) {
1567             fe0->dvb.frontend->ops.tuner_ops.init = philips_td1316_tuner_init;
1568             fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1569         }
1570         break;
1571     case SAA7134_BOARD_CINERGY_HT_PCMCIA:
1572         if (configure_tda827x_fe(dev, &cinergy_ht_config,
1573                      &tda827x_cfg_0) < 0)
1574             goto detach_frontend;
1575         break;
1576     case SAA7134_BOARD_CINERGY_HT_PCI:
1577         if (configure_tda827x_fe(dev, &cinergy_ht_pci_config,
1578                      &tda827x_cfg_0) < 0)
1579             goto detach_frontend;
1580         break;
1581     case SAA7134_BOARD_PHILIPS_TIGER_S:
1582         if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1583                      &tda827x_cfg_2) < 0)
1584             goto detach_frontend;
1585         break;
1586     case SAA7134_BOARD_ASUS_P7131_4871:
1587         if (configure_tda827x_fe(dev, &asus_p7131_4871_config,
1588                      &tda827x_cfg_2) < 0)
1589             goto detach_frontend;
1590         break;
1591     case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
1592         if (configure_tda827x_fe(dev, &asus_p7131_hybrid_lna_config,
1593                      &tda827x_cfg_2) < 0)
1594             goto detach_frontend;
1595         break;
1596     case SAA7134_BOARD_AVERMEDIA_SUPER_007:
1597         if (configure_tda827x_fe(dev, &avermedia_super_007_config,
1598                      &tda827x_cfg_0) < 0)
1599             goto detach_frontend;
1600         break;
1601     case SAA7134_BOARD_TWINHAN_DTV_DVB_3056:
1602         if (configure_tda827x_fe(dev, &twinhan_dtv_dvb_3056_config,
1603                      &tda827x_cfg_2_sw42) < 0)
1604             goto detach_frontend;
1605         break;
1606     case SAA7134_BOARD_PHILIPS_SNAKE:
1607         fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1608                         &dev->i2c_adap);
1609         if (fe0->dvb.frontend) {
1610             if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1611                     &dev->i2c_adap, 0) == NULL) {
1612                 pr_warn("%s: No tda826x found!\n", __func__);
1613                 goto detach_frontend;
1614             }
1615             if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1616                     &dev->i2c_adap, 0, 0) == NULL) {
1617                 pr_warn("%s: No lnbp21 found!\n", __func__);
1618                 goto detach_frontend;
1619             }
1620         }
1621         break;
1622     case SAA7134_BOARD_CREATIX_CTX953:
1623         if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1624                      &tda827x_cfg_0) < 0)
1625             goto detach_frontend;
1626         break;
1627     case SAA7134_BOARD_MSI_TVANYWHERE_AD11:
1628         if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1629                      &tda827x_cfg_2) < 0)
1630             goto detach_frontend;
1631         break;
1632     case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
1633         pr_debug("AverMedia E506R dvb setup\n");
1634         saa7134_set_gpio(dev, 25, 0);
1635         msleep(10);
1636         saa7134_set_gpio(dev, 25, 1);
1637         fe0->dvb.frontend = dvb_attach(mt352_attach,
1638                         &avermedia_xc3028_mt352_dev,
1639                         &dev->i2c_adap);
1640         attach_xc3028 = 1;
1641         break;
1642     case SAA7134_BOARD_MD7134_BRIDGE_2:
1643         fe0->dvb.frontend = dvb_attach(tda10086_attach,
1644                         &sd1878_4m, &dev->i2c_adap);
1645         if (fe0->dvb.frontend) {
1646             struct dvb_frontend *fe;
1647             if (dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x60,
1648                   &dev->i2c_adap, DVB_PLL_PHILIPS_SD1878_TDA8261) == NULL) {
1649                 pr_warn("%s: MD7134 DVB-S, no SD1878 found !\n",
1650                     __func__);
1651                 goto detach_frontend;
1652             }
1653             /* we need to open the i2c gate (we know it exists) */
1654             fe = fe0->dvb.frontend;
1655             fe->ops.i2c_gate_ctrl(fe, 1);
1656             if (dvb_attach(isl6405_attach, fe,
1657                     &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1658                 pr_warn("%s: MD7134 DVB-S, no ISL6405 found !\n",
1659                     __func__);
1660                 goto detach_frontend;
1661             }
1662             fe->ops.i2c_gate_ctrl(fe, 0);
1663             dev->original_set_voltage = fe->ops.set_voltage;
1664             fe->ops.set_voltage = md8800_set_voltage;
1665             dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1666             fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1667         }
1668         break;
1669     case SAA7134_BOARD_AVERMEDIA_M103:
1670         saa7134_set_gpio(dev, 25, 0);
1671         msleep(10);
1672         saa7134_set_gpio(dev, 25, 1);
1673         fe0->dvb.frontend = dvb_attach(mt352_attach,
1674                         &avermedia_xc3028_mt352_dev,
1675                         &dev->i2c_adap);
1676         attach_xc3028 = 1;
1677         break;
1678     case SAA7134_BOARD_ASUSTeK_TIGER_3IN1:
1679         if (!use_frontend) {     /* terrestrial */
1680             if (configure_tda827x_fe(dev, &asus_tiger_3in1_config,
1681                             &tda827x_cfg_2) < 0)
1682                 goto detach_frontend;
1683         } else {        /* satellite */
1684             fe0->dvb.frontend = dvb_attach(tda10086_attach,
1685                         &flydvbs, &dev->i2c_adap);
1686             if (fe0->dvb.frontend) {
1687                 if (dvb_attach(tda826x_attach,
1688                         fe0->dvb.frontend, 0x60,
1689                         &dev->i2c_adap, 0) == NULL) {
1690                     pr_warn("%s: Asus Tiger 3in1, no tda826x found!\n",
1691                         __func__);
1692                     goto detach_frontend;
1693                 }
1694                 if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1695                         &dev->i2c_adap, 0, 0) == NULL) {
1696                     pr_warn("%s: Asus Tiger 3in1, no lnbp21 found!\n",
1697                         __func__);
1698                     goto detach_frontend;
1699                    }
1700                }
1701            }
1702            break;
1703     case SAA7134_BOARD_ASUSTeK_PS3_100:
1704         if (!use_frontend) {     /* terrestrial */
1705             if (configure_tda827x_fe(dev, &asus_ps3_100_config,
1706                          &tda827x_cfg_2) < 0)
1707                 goto detach_frontend;
1708            } else {                /* satellite */
1709             fe0->dvb.frontend = dvb_attach(tda10086_attach,
1710                                &flydvbs, &dev->i2c_adap);
1711             if (fe0->dvb.frontend) {
1712                 if (dvb_attach(tda826x_attach,
1713                            fe0->dvb.frontend, 0x60,
1714                            &dev->i2c_adap, 0) == NULL) {
1715                     pr_warn("%s: Asus My Cinema PS3-100, no tda826x found!\n",
1716                         __func__);
1717                     goto detach_frontend;
1718                 }
1719                 if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1720                            &dev->i2c_adap, 0, 0) == NULL) {
1721                     pr_warn("%s: Asus My Cinema PS3-100, no lnbp21 found!\n",
1722                         __func__);
1723                     goto detach_frontend;
1724                 }
1725             }
1726         }
1727         break;
1728     case SAA7134_BOARD_ASUSTeK_TIGER:
1729         if (configure_tda827x_fe(dev, &philips_tiger_config,
1730                      &tda827x_cfg_0) < 0)
1731             goto detach_frontend;
1732         break;
1733     case SAA7134_BOARD_BEHOLD_H6:
1734         fe0->dvb.frontend = dvb_attach(zl10353_attach,
1735                         &behold_h6_config,
1736                         &dev->i2c_adap);
1737         if (fe0->dvb.frontend) {
1738             dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1739                    &dev->i2c_adap, 0x61,
1740                    TUNER_PHILIPS_FMD1216MEX_MK3);
1741         }
1742         break;
1743     case SAA7134_BOARD_BEHOLD_X7:
1744         fe0->dvb.frontend = dvb_attach(zl10353_attach,
1745                         &behold_x7_config,
1746                         &dev->i2c_adap);
1747         if (fe0->dvb.frontend) {
1748             dvb_attach(xc5000_attach, fe0->dvb.frontend,
1749                    &dev->i2c_adap, &behold_x7_tunerconfig);
1750         }
1751         break;
1752     case SAA7134_BOARD_BEHOLD_H7:
1753         fe0->dvb.frontend = dvb_attach(zl10353_attach,
1754                         &behold_x7_config,
1755                         &dev->i2c_adap);
1756         if (fe0->dvb.frontend) {
1757             dvb_attach(xc5000_attach, fe0->dvb.frontend,
1758                    &dev->i2c_adap, &behold_x7_tunerconfig);
1759         }
1760         break;
1761     case SAA7134_BOARD_AVERMEDIA_A700_PRO:
1762     case SAA7134_BOARD_AVERMEDIA_A700_HYBRID:
1763         /* Zarlink ZL10313 */
1764         fe0->dvb.frontend = dvb_attach(mt312_attach,
1765             &avertv_a700_mt312, &dev->i2c_adap);
1766         if (fe0->dvb.frontend) {
1767             if (dvb_attach(zl10036_attach, fe0->dvb.frontend,
1768                     &avertv_a700_tuner, &dev->i2c_adap) == NULL) {
1769                 pr_warn("%s: No zl10036 found!\n",
1770                     __func__);
1771             }
1772         }
1773         break;
1774     case SAA7134_BOARD_VIDEOMATE_S350:
1775         fe0->dvb.frontend = dvb_attach(mt312_attach,
1776                 &zl10313_compro_s350_config, &dev->i2c_adap);
1777         if (fe0->dvb.frontend)
1778             if (dvb_attach(zl10039_attach, fe0->dvb.frontend,
1779                     0x60, &dev->i2c_adap) == NULL)
1780                 pr_warn("%s: No zl10039 found!\n",
1781                     __func__);
1782 
1783         break;
1784     case SAA7134_BOARD_VIDEOMATE_T750:
1785         fe0->dvb.frontend = dvb_attach(zl10353_attach,
1786                         &videomate_t750_zl10353_config,
1787                         &dev->i2c_adap);
1788         if (fe0->dvb.frontend != NULL) {
1789             if (dvb_attach(qt1010_attach,
1790                     fe0->dvb.frontend,
1791                     &dev->i2c_adap,
1792                     &videomate_t750_qt1010_config) == NULL)
1793                 pr_warn("error attaching QT1010\n");
1794         }
1795         break;
1796     case SAA7134_BOARD_ZOLID_HYBRID_PCI:
1797         fe0->dvb.frontend = dvb_attach(tda10048_attach,
1798                            &zolid_tda10048_config,
1799                            &dev->i2c_adap);
1800         if (fe0->dvb.frontend != NULL) {
1801             dvb_attach(tda829x_attach, fe0->dvb.frontend,
1802                    &dev->i2c_adap, 0x4b,
1803                    &tda829x_no_probe);
1804             dvb_attach(tda18271_attach, fe0->dvb.frontend,
1805                    0x60, &dev->i2c_adap,
1806                    &zolid_tda18271_config);
1807         }
1808         break;
1809     case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
1810         fe0->dvb.frontend = dvb_attach(tda10048_attach,
1811                            &dtv1000s_tda10048_config,
1812                            &dev->i2c_adap);
1813         if (fe0->dvb.frontend != NULL) {
1814             dvb_attach(tda829x_attach, fe0->dvb.frontend,
1815                    &dev->i2c_adap, 0x4b,
1816                    &tda829x_no_probe);
1817             dvb_attach(tda18271_attach, fe0->dvb.frontend,
1818                    0x60, &dev->i2c_adap,
1819                    &dtv1000s_tda18271_config);
1820         }
1821         break;
1822     case SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG:
1823         /* Switch to digital mode */
1824         saa7134_tuner_callback(dev, 0,
1825                        TDA18271_CALLBACK_CMD_AGC_ENABLE, 1);
1826         fe0->dvb.frontend = dvb_attach(mb86a20s_attach,
1827                            &kworld_mb86a20s_config,
1828                            &dev->i2c_adap);
1829         if (fe0->dvb.frontend != NULL) {
1830             dvb_attach(tda829x_attach, fe0->dvb.frontend,
1831                    &dev->i2c_adap, 0x4b,
1832                    &tda829x_no_probe);
1833             fe0->dvb.frontend->ops.i2c_gate_ctrl = kworld_sbtvd_gate_ctrl;
1834             dvb_attach(tda18271_attach, fe0->dvb.frontend,
1835                    0x60, &dev->i2c_adap,
1836                    &kworld_tda18271_config);
1837         }
1838 
1839         /* mb86a20s need to use the I2C gateway */
1840         break;
1841     case SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2:
1842         fe0->dvb.frontend = dvb_attach(lgs8gxx_attach,
1843                            &prohdtv_pro2_lgs8g75_config,
1844                            &dev->i2c_adap);
1845         if (fe0->dvb.frontend != NULL) {
1846             dvb_attach(tda829x_attach, fe0->dvb.frontend,
1847                    &dev->i2c_adap, 0x4b,
1848                    &tda829x_no_probe);
1849             dvb_attach(tda18271_attach, fe0->dvb.frontend,
1850                    0x60, &dev->i2c_adap,
1851                    &prohdtv_pro2_tda18271_config);
1852         }
1853         break;
1854     case SAA7134_BOARD_AVERMEDIA_A706:
1855         /* Enable all DVB-S devices now */
1856         /* CE5039 DVB-S tuner SLEEP pin low */
1857         saa7134_set_gpio(dev, 23, 0);
1858         /* CE6313 DVB-S demod SLEEP pin low */
1859         saa7134_set_gpio(dev, 9, 0);
1860         /* CE6313 DVB-S demod RESET# pin high */
1861         saa7134_set_gpio(dev, 25, 1);
1862         msleep(1);
1863         fe0->dvb.frontend = dvb_attach(mt312_attach,
1864                 &zl10313_avermedia_a706_config, &dev->i2c_adap);
1865         if (fe0->dvb.frontend) {
1866             fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1867             if (dvb_attach(zl10039_attach, fe0->dvb.frontend,
1868                     0x60, &dev->i2c_adap) == NULL)
1869                 pr_warn("%s: No zl10039 found!\n",
1870                     __func__);
1871         }
1872         break;
1873     case SAA7134_BOARD_LEADTEK_WINFAST_HDTV200_H:
1874         fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1875                            &hdtv200h_s5h1411_config,
1876                            &dev->i2c_adap);
1877         if (fe0->dvb.frontend) {
1878             dvb_attach(tda829x_attach, fe0->dvb.frontend,
1879                    &dev->i2c_adap, 0x4b,
1880                    &tda829x_no_probe);
1881             dvb_attach(tda18271_attach, fe0->dvb.frontend,
1882                    0x60, &dev->i2c_adap,
1883                    &hdtv200h_tda18271_config);
1884         }
1885         break;
1886     default:
1887         pr_warn("Huh? unknown DVB card?\n");
1888         break;
1889     }
1890 
1891     if (attach_xc3028) {
1892         struct dvb_frontend *fe;
1893         struct xc2028_config cfg = {
1894             .i2c_adap  = &dev->i2c_adap,
1895             .i2c_addr  = 0x61,
1896         };
1897 
1898         if (!fe0->dvb.frontend)
1899             goto detach_frontend;
1900 
1901         fe = dvb_attach(xc2028_attach, fe0->dvb.frontend, &cfg);
1902         if (!fe) {
1903             pr_err("%s/2: xc3028 attach failed\n",
1904                    dev->name);
1905             goto detach_frontend;
1906         }
1907     }
1908 
1909     if (NULL == fe0->dvb.frontend) {
1910         pr_err("%s/dvb: frontend initialization failed\n", dev->name);
1911         goto detach_frontend;
1912     }
1913     /* define general-purpose callback pointer */
1914     fe0->dvb.frontend->callback = saa7134_tuner_callback;
1915 
1916     /* register everything else */
1917 #ifndef CONFIG_MEDIA_CONTROLLER_DVB
1918     ret = vb2_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1919                    &dev->pci->dev, NULL,
1920                    adapter_nr, 0);
1921 #else
1922     ret = vb2_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1923                    &dev->pci->dev, dev->media_dev,
1924                    adapter_nr, 0);
1925 #endif
1926 
1927     /* this sequence is necessary to make the tda1004x load its firmware
1928      * and to enter analog mode of hybrid boards
1929      */
1930     if (!ret) {
1931         if (fe0->dvb.frontend->ops.init)
1932             fe0->dvb.frontend->ops.init(fe0->dvb.frontend);
1933         if (fe0->dvb.frontend->ops.sleep)
1934             fe0->dvb.frontend->ops.sleep(fe0->dvb.frontend);
1935         if (fe0->dvb.frontend->ops.tuner_ops.sleep)
1936             fe0->dvb.frontend->ops.tuner_ops.sleep(fe0->dvb.frontend);
1937     }
1938     return ret;
1939 
1940 detach_frontend:
1941     vb2_dvb_dealloc_frontends(&dev->frontends);
1942     vb2_queue_release(&fe0->dvb.dvbq);
1943     return -EINVAL;
1944 }
1945 
1946 static int dvb_fini(struct saa7134_dev *dev)
1947 {
1948     struct vb2_dvb_frontend *fe0;
1949 
1950     /* Get the first frontend */
1951     fe0 = vb2_dvb_get_frontend(&dev->frontends, 1);
1952     if (!fe0)
1953         return -EINVAL;
1954 
1955     /* FIXME: I suspect that this code is bogus, since the entry for
1956        Pinnacle 300I DVB-T PAL already defines the proper init to allow
1957        the detection of mt2032 (TDA9887_PORT2_INACTIVE)
1958      */
1959     if (dev->board == SAA7134_BOARD_PINNACLE_300I_DVBT_PAL) {
1960         struct v4l2_priv_tun_config tda9887_cfg;
1961         static int on  = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
1962 
1963         tda9887_cfg.tuner = TUNER_TDA9887;
1964         tda9887_cfg.priv  = &on;
1965 
1966         /* otherwise we don't detect the tuner on next insmod */
1967         saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1968     } else if (dev->board == SAA7134_BOARD_MEDION_MD8800_QUADRO) {
1969         if ((dev->eedata[2] == 0x07) && use_frontend) {
1970             /* turn off the 2nd lnb supply */
1971             u8 data = 0x80;
1972             struct i2c_msg msg = {.addr = 0x08, .buf = &data, .flags = 0, .len = 1};
1973             struct dvb_frontend *fe;
1974             fe = fe0->dvb.frontend;
1975             if (fe->ops.i2c_gate_ctrl) {
1976                 fe->ops.i2c_gate_ctrl(fe, 1);
1977                 i2c_transfer(&dev->i2c_adap, &msg, 1);
1978                 fe->ops.i2c_gate_ctrl(fe, 0);
1979             }
1980         }
1981     }
1982     vb2_dvb_unregister_bus(&dev->frontends);
1983     vb2_queue_release(&fe0->dvb.dvbq);
1984     return 0;
1985 }
1986 
1987 static struct saa7134_mpeg_ops dvb_ops = {
1988     .type          = SAA7134_MPEG_DVB,
1989     .init          = dvb_init,
1990     .fini          = dvb_fini,
1991 };
1992 
1993 static int __init dvb_register(void)
1994 {
1995     return saa7134_ts_register(&dvb_ops);
1996 }
1997 
1998 static void __exit dvb_unregister(void)
1999 {
2000     saa7134_ts_unregister(&dvb_ops);
2001 }
2002 
2003 module_init(dvb_register);
2004 module_exit(dvb_unregister);