Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003 
0004    i2c tv tuner chip device driver
0005    controls the philips tda8290+75 tuner chip combo.
0006 
0007 
0008    This "tda8290" module was split apart from the original "tuner" module.
0009 */
0010 
0011 #include <linux/i2c.h>
0012 #include <linux/slab.h>
0013 #include <linux/delay.h>
0014 #include <linux/videodev2.h>
0015 #include "tuner-i2c.h"
0016 #include "tda8290.h"
0017 #include "tda827x.h"
0018 #include "tda18271.h"
0019 
0020 static int debug;
0021 module_param(debug, int, 0644);
0022 MODULE_PARM_DESC(debug, "enable verbose debug messages");
0023 
0024 static int deemphasis_50;
0025 module_param(deemphasis_50, int, 0644);
0026 MODULE_PARM_DESC(deemphasis_50, "0 - 75us deemphasis; 1 - 50us deemphasis");
0027 
0028 /* ---------------------------------------------------------------------- */
0029 
0030 struct tda8290_priv {
0031     struct tuner_i2c_props i2c_props;
0032 
0033     unsigned char tda8290_easy_mode;
0034 
0035     unsigned char tda827x_addr;
0036 
0037     unsigned char ver;
0038 #define TDA8290   1
0039 #define TDA8295   2
0040 #define TDA8275   4
0041 #define TDA8275A  8
0042 #define TDA18271 16
0043 
0044     struct tda827x_config cfg;
0045     struct tda18271_std_map *tda18271_std_map;
0046 };
0047 
0048 /*---------------------------------------------------------------------*/
0049 
0050 static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
0051 {
0052     struct tda8290_priv *priv = fe->analog_demod_priv;
0053 
0054     static unsigned char  enable[2] = { 0x21, 0xC0 };
0055     static unsigned char disable[2] = { 0x21, 0x00 };
0056     unsigned char *msg;
0057 
0058     if (close) {
0059         msg = enable;
0060         tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
0061         /* let the bridge stabilize */
0062         msleep(20);
0063     } else {
0064         msg = disable;
0065         tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
0066     }
0067 
0068     return 0;
0069 }
0070 
0071 static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
0072 {
0073     struct tda8290_priv *priv = fe->analog_demod_priv;
0074 
0075     static unsigned char  enable[2] = { 0x45, 0xc1 };
0076     static unsigned char disable[2] = { 0x46, 0x00 };
0077     static unsigned char buf[3] = { 0x45, 0x01, 0x00 };
0078     unsigned char *msg;
0079 
0080     if (close) {
0081         msg = enable;
0082         tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
0083         /* let the bridge stabilize */
0084         msleep(20);
0085     } else {
0086         msg = disable;
0087         tuner_i2c_xfer_send_recv(&priv->i2c_props, msg, 1, &msg[1], 1);
0088 
0089         buf[2] = msg[1];
0090         buf[2] &= ~0x04;
0091         tuner_i2c_xfer_send(&priv->i2c_props, buf, 3);
0092         msleep(5);
0093 
0094         msg[1] |= 0x04;
0095         tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
0096     }
0097 
0098     return 0;
0099 }
0100 
0101 /*---------------------------------------------------------------------*/
0102 
0103 static void set_audio(struct dvb_frontend *fe,
0104               struct analog_parameters *params)
0105 {
0106     struct tda8290_priv *priv = fe->analog_demod_priv;
0107     char* mode;
0108 
0109     if (params->std & V4L2_STD_MN) {
0110         priv->tda8290_easy_mode = 0x01;
0111         mode = "MN";
0112     } else if (params->std & V4L2_STD_B) {
0113         priv->tda8290_easy_mode = 0x02;
0114         mode = "B";
0115     } else if (params->std & V4L2_STD_GH) {
0116         priv->tda8290_easy_mode = 0x04;
0117         mode = "GH";
0118     } else if (params->std & V4L2_STD_PAL_I) {
0119         priv->tda8290_easy_mode = 0x08;
0120         mode = "I";
0121     } else if (params->std & V4L2_STD_DK) {
0122         priv->tda8290_easy_mode = 0x10;
0123         mode = "DK";
0124     } else if (params->std & V4L2_STD_SECAM_L) {
0125         priv->tda8290_easy_mode = 0x20;
0126         mode = "L";
0127     } else if (params->std & V4L2_STD_SECAM_LC) {
0128         priv->tda8290_easy_mode = 0x40;
0129         mode = "LC";
0130     } else {
0131         priv->tda8290_easy_mode = 0x10;
0132         mode = "xx";
0133     }
0134 
0135     if (params->mode == V4L2_TUNER_RADIO) {
0136         /* Set TDA8295 to FM radio; Start TDA8290 with MN values */
0137         priv->tda8290_easy_mode = (priv->ver & TDA8295) ? 0x80 : 0x01;
0138         tuner_dbg("setting to radio FM\n");
0139     } else {
0140         tuner_dbg("setting tda829x to system %s\n", mode);
0141     }
0142 }
0143 
0144 static struct {
0145     unsigned char seq[2];
0146 } fm_mode[] = {
0147     { { 0x01, 0x81} },  /* Put device into expert mode */
0148     { { 0x03, 0x48} },  /* Disable NOTCH and VIDEO filters */
0149     { { 0x04, 0x04} },  /* Disable color carrier filter (SSIF) */
0150     { { 0x05, 0x04} },  /* ADC headroom */
0151     { { 0x06, 0x10} },  /* group delay flat */
0152 
0153     { { 0x07, 0x00} },  /* use the same radio DTO values as a tda8295 */
0154     { { 0x08, 0x00} },
0155     { { 0x09, 0x80} },
0156     { { 0x0a, 0xda} },
0157     { { 0x0b, 0x4b} },
0158     { { 0x0c, 0x68} },
0159 
0160     { { 0x0d, 0x00} },  /* PLL off, no video carrier detect */
0161     { { 0x14, 0x00} },  /* disable auto mute if no video */
0162 };
0163 
0164 static void tda8290_set_params(struct dvb_frontend *fe,
0165                    struct analog_parameters *params)
0166 {
0167     struct tda8290_priv *priv = fe->analog_demod_priv;
0168 
0169     static unsigned char soft_reset[]  = { 0x00, 0x00 };
0170     unsigned char easy_mode[]   = { 0x01, priv->tda8290_easy_mode };
0171     static unsigned char expert_mode[] = { 0x01, 0x80 };
0172     static unsigned char agc_out_on[]  = { 0x02, 0x00 };
0173     static unsigned char gainset_off[] = { 0x28, 0x14 };
0174     static unsigned char if_agc_spd[]  = { 0x0f, 0x88 };
0175     static unsigned char adc_head_6[]  = { 0x05, 0x04 };
0176     static unsigned char adc_head_9[]  = { 0x05, 0x02 };
0177     static unsigned char adc_head_12[] = { 0x05, 0x01 };
0178     static unsigned char pll_bw_nom[]  = { 0x0d, 0x47 };
0179     static unsigned char pll_bw_low[]  = { 0x0d, 0x27 };
0180     static unsigned char gainset_2[]   = { 0x28, 0x64 };
0181     static unsigned char agc_rst_on[]  = { 0x0e, 0x0b };
0182     static unsigned char agc_rst_off[] = { 0x0e, 0x09 };
0183     static unsigned char if_agc_set[]  = { 0x0f, 0x81 };
0184     static unsigned char addr_adc_sat  = 0x1a;
0185     static unsigned char addr_agc_stat = 0x1d;
0186     static unsigned char addr_pll_stat = 0x1b;
0187     static unsigned char adc_sat = 0, agc_stat = 0,
0188               pll_stat;
0189     int i;
0190 
0191     set_audio(fe, params);
0192 
0193     if (priv->cfg.config)
0194         tuner_dbg("tda827xa config is 0x%02x\n", priv->cfg.config);
0195     tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2);
0196     tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2);
0197     tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2);
0198     msleep(1);
0199 
0200     if (params->mode == V4L2_TUNER_RADIO) {
0201         unsigned char deemphasis[]  = { 0x13, 1 };
0202 
0203         /* FIXME: allow using a different deemphasis */
0204 
0205         if (deemphasis_50)
0206             deemphasis[1] = 2;
0207 
0208         for (i = 0; i < ARRAY_SIZE(fm_mode); i++)
0209             tuner_i2c_xfer_send(&priv->i2c_props, fm_mode[i].seq, 2);
0210 
0211         tuner_i2c_xfer_send(&priv->i2c_props, deemphasis, 2);
0212     } else {
0213         expert_mode[1] = priv->tda8290_easy_mode + 0x80;
0214         tuner_i2c_xfer_send(&priv->i2c_props, expert_mode, 2);
0215         tuner_i2c_xfer_send(&priv->i2c_props, gainset_off, 2);
0216         tuner_i2c_xfer_send(&priv->i2c_props, if_agc_spd, 2);
0217         if (priv->tda8290_easy_mode & 0x60)
0218             tuner_i2c_xfer_send(&priv->i2c_props, adc_head_9, 2);
0219         else
0220             tuner_i2c_xfer_send(&priv->i2c_props, adc_head_6, 2);
0221         tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_nom, 2);
0222     }
0223 
0224 
0225     if (fe->ops.analog_ops.i2c_gate_ctrl)
0226         fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
0227 
0228     if (fe->ops.tuner_ops.set_analog_params)
0229         fe->ops.tuner_ops.set_analog_params(fe, params);
0230 
0231     for (i = 0; i < 3; i++) {
0232         tuner_i2c_xfer_send_recv(&priv->i2c_props,
0233                      &addr_pll_stat, 1, &pll_stat, 1);
0234         if (pll_stat & 0x80) {
0235             tuner_i2c_xfer_send_recv(&priv->i2c_props,
0236                          &addr_adc_sat, 1,
0237                          &adc_sat, 1);
0238             tuner_i2c_xfer_send_recv(&priv->i2c_props,
0239                          &addr_agc_stat, 1,
0240                          &agc_stat, 1);
0241             tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
0242             break;
0243         } else {
0244             tuner_dbg("tda8290 not locked, no signal?\n");
0245             msleep(100);
0246         }
0247     }
0248     /* adjust headroom resp. gain */
0249     if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat < 20))) {
0250         tuner_dbg("adjust gain, step 1. Agc: %d, ADC stat: %d, lock: %d\n",
0251                agc_stat, adc_sat, pll_stat & 0x80);
0252         tuner_i2c_xfer_send(&priv->i2c_props, gainset_2, 2);
0253         msleep(100);
0254         tuner_i2c_xfer_send_recv(&priv->i2c_props,
0255                      &addr_agc_stat, 1, &agc_stat, 1);
0256         tuner_i2c_xfer_send_recv(&priv->i2c_props,
0257                      &addr_pll_stat, 1, &pll_stat, 1);
0258         if ((agc_stat > 115) || !(pll_stat & 0x80)) {
0259             tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
0260                    agc_stat, pll_stat & 0x80);
0261             if (priv->cfg.agcf)
0262                 priv->cfg.agcf(fe);
0263             msleep(100);
0264             tuner_i2c_xfer_send_recv(&priv->i2c_props,
0265                          &addr_agc_stat, 1,
0266                          &agc_stat, 1);
0267             tuner_i2c_xfer_send_recv(&priv->i2c_props,
0268                          &addr_pll_stat, 1,
0269                          &pll_stat, 1);
0270             if((agc_stat > 115) || !(pll_stat & 0x80)) {
0271                 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
0272                 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_12, 2);
0273                 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_low, 2);
0274                 msleep(100);
0275             }
0276         }
0277     }
0278 
0279     /* l/ l' deadlock? */
0280     if(priv->tda8290_easy_mode & 0x60) {
0281         tuner_i2c_xfer_send_recv(&priv->i2c_props,
0282                      &addr_adc_sat, 1,
0283                      &adc_sat, 1);
0284         tuner_i2c_xfer_send_recv(&priv->i2c_props,
0285                      &addr_pll_stat, 1,
0286                      &pll_stat, 1);
0287         if ((adc_sat > 20) || !(pll_stat & 0x80)) {
0288             tuner_dbg("trying to resolve SECAM L deadlock\n");
0289             tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_on, 2);
0290             msleep(40);
0291             tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_off, 2);
0292         }
0293     }
0294 
0295     if (fe->ops.analog_ops.i2c_gate_ctrl)
0296         fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
0297     tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2);
0298 }
0299 
0300 /*---------------------------------------------------------------------*/
0301 
0302 static void tda8295_power(struct dvb_frontend *fe, int enable)
0303 {
0304     struct tda8290_priv *priv = fe->analog_demod_priv;
0305     unsigned char buf[] = { 0x30, 0x00 }; /* clb_stdbt */
0306 
0307     tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
0308 
0309     if (enable)
0310         buf[1] = 0x01;
0311     else
0312         buf[1] = 0x03;
0313 
0314     tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
0315 }
0316 
0317 static void tda8295_set_easy_mode(struct dvb_frontend *fe, int enable)
0318 {
0319     struct tda8290_priv *priv = fe->analog_demod_priv;
0320     unsigned char buf[] = { 0x01, 0x00 };
0321 
0322     tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
0323 
0324     if (enable)
0325         buf[1] = 0x01; /* rising edge sets regs 0x02 - 0x23 */
0326     else
0327         buf[1] = 0x00; /* reset active bit */
0328 
0329     tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
0330 }
0331 
0332 static void tda8295_set_video_std(struct dvb_frontend *fe)
0333 {
0334     struct tda8290_priv *priv = fe->analog_demod_priv;
0335     unsigned char buf[] = { 0x00, priv->tda8290_easy_mode };
0336 
0337     tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
0338 
0339     tda8295_set_easy_mode(fe, 1);
0340     msleep(20);
0341     tda8295_set_easy_mode(fe, 0);
0342 }
0343 
0344 /*---------------------------------------------------------------------*/
0345 
0346 static void tda8295_agc1_out(struct dvb_frontend *fe, int enable)
0347 {
0348     struct tda8290_priv *priv = fe->analog_demod_priv;
0349     unsigned char buf[] = { 0x02, 0x00 }; /* DIV_FUNC */
0350 
0351     tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
0352 
0353     if (enable)
0354         buf[1] &= ~0x40;
0355     else
0356         buf[1] |= 0x40;
0357 
0358     tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
0359 }
0360 
0361 static void tda8295_agc2_out(struct dvb_frontend *fe, int enable)
0362 {
0363     struct tda8290_priv *priv = fe->analog_demod_priv;
0364     unsigned char set_gpio_cf[]    = { 0x44, 0x00 };
0365     unsigned char set_gpio_val[]   = { 0x46, 0x00 };
0366 
0367     tuner_i2c_xfer_send_recv(&priv->i2c_props,
0368                  &set_gpio_cf[0], 1, &set_gpio_cf[1], 1);
0369     tuner_i2c_xfer_send_recv(&priv->i2c_props,
0370                  &set_gpio_val[0], 1, &set_gpio_val[1], 1);
0371 
0372     set_gpio_cf[1] &= 0xf0; /* clear GPIO_0 bits 3-0 */
0373 
0374     if (enable) {
0375         set_gpio_cf[1]  |= 0x01; /* config GPIO_0 as Open Drain Out */
0376         set_gpio_val[1] &= 0xfe; /* set GPIO_0 pin low */
0377     }
0378     tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_cf, 2);
0379     tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_val, 2);
0380 }
0381 
0382 static int tda8295_has_signal(struct dvb_frontend *fe, u16 *signal)
0383 {
0384     struct tda8290_priv *priv = fe->analog_demod_priv;
0385 
0386     unsigned char hvpll_stat = 0x26;
0387     unsigned char ret;
0388 
0389     tuner_i2c_xfer_send_recv(&priv->i2c_props, &hvpll_stat, 1, &ret, 1);
0390     *signal = (ret & 0x01) ? 65535 : 0;
0391     return 0;
0392 }
0393 
0394 /*---------------------------------------------------------------------*/
0395 
0396 static void tda8295_set_params(struct dvb_frontend *fe,
0397                    struct analog_parameters *params)
0398 {
0399     struct tda8290_priv *priv = fe->analog_demod_priv;
0400     u16 signal = 0;
0401     unsigned char blanking_mode[]     = { 0x1d, 0x00 };
0402 
0403     set_audio(fe, params);
0404 
0405     tuner_dbg("%s: freq = %d\n", __func__, params->frequency);
0406 
0407     tda8295_power(fe, 1);
0408     tda8295_agc1_out(fe, 1);
0409 
0410     tuner_i2c_xfer_send_recv(&priv->i2c_props,
0411                  &blanking_mode[0], 1, &blanking_mode[1], 1);
0412 
0413     tda8295_set_video_std(fe);
0414 
0415     blanking_mode[1] = 0x03;
0416     tuner_i2c_xfer_send(&priv->i2c_props, blanking_mode, 2);
0417     msleep(20);
0418 
0419     if (fe->ops.analog_ops.i2c_gate_ctrl)
0420         fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
0421 
0422     if (fe->ops.tuner_ops.set_analog_params)
0423         fe->ops.tuner_ops.set_analog_params(fe, params);
0424 
0425     if (priv->cfg.agcf)
0426         priv->cfg.agcf(fe);
0427 
0428     tda8295_has_signal(fe, &signal);
0429     if (signal)
0430         tuner_dbg("tda8295 is locked\n");
0431     else
0432         tuner_dbg("tda8295 not locked, no signal?\n");
0433 
0434     if (fe->ops.analog_ops.i2c_gate_ctrl)
0435         fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
0436 }
0437 
0438 /*---------------------------------------------------------------------*/
0439 
0440 static int tda8290_has_signal(struct dvb_frontend *fe, u16 *signal)
0441 {
0442     struct tda8290_priv *priv = fe->analog_demod_priv;
0443 
0444     unsigned char i2c_get_afc[1] = { 0x1B };
0445     unsigned char afc = 0;
0446 
0447     tuner_i2c_xfer_send_recv(&priv->i2c_props,
0448                  i2c_get_afc, ARRAY_SIZE(i2c_get_afc), &afc, 1);
0449     *signal = (afc & 0x80) ? 65535 : 0;
0450     return 0;
0451 }
0452 
0453 /*---------------------------------------------------------------------*/
0454 
0455 static void tda8290_standby(struct dvb_frontend *fe)
0456 {
0457     struct tda8290_priv *priv = fe->analog_demod_priv;
0458 
0459     static unsigned char cb1[] = { 0x30, 0xD0 };
0460     static unsigned char tda8290_standby[] = { 0x00, 0x02 };
0461     static unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
0462     struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
0463 
0464     if (fe->ops.analog_ops.i2c_gate_ctrl)
0465         fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
0466     if (priv->ver & TDA8275A)
0467         cb1[1] = 0x90;
0468     i2c_transfer(priv->i2c_props.adap, &msg, 1);
0469     if (fe->ops.analog_ops.i2c_gate_ctrl)
0470         fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
0471     tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2);
0472     tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2);
0473 }
0474 
0475 static void tda8295_standby(struct dvb_frontend *fe)
0476 {
0477     tda8295_agc1_out(fe, 0); /* Put AGC in tri-state */
0478 
0479     tda8295_power(fe, 0);
0480 }
0481 
0482 static void tda8290_init_if(struct dvb_frontend *fe)
0483 {
0484     struct tda8290_priv *priv = fe->analog_demod_priv;
0485 
0486     static unsigned char set_VS[] = { 0x30, 0x6F };
0487     static unsigned char set_GP00_CF[] = { 0x20, 0x01 };
0488     static unsigned char set_GP01_CF[] = { 0x20, 0x0B };
0489 
0490     if ((priv->cfg.config == TDA8290_LNA_GP0_HIGH_ON) ||
0491         (priv->cfg.config == TDA8290_LNA_GP0_HIGH_OFF))
0492         tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
0493     else
0494         tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
0495     tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2);
0496 }
0497 
0498 static void tda8295_init_if(struct dvb_frontend *fe)
0499 {
0500     struct tda8290_priv *priv = fe->analog_demod_priv;
0501 
0502     static unsigned char set_adc_ctl[]       = { 0x33, 0x14 };
0503     static unsigned char set_adc_ctl2[]      = { 0x34, 0x00 };
0504     static unsigned char set_pll_reg6[]      = { 0x3e, 0x63 };
0505     static unsigned char set_pll_reg0[]      = { 0x38, 0x23 };
0506     static unsigned char set_pll_reg7[]      = { 0x3f, 0x01 };
0507     static unsigned char set_pll_reg10[]     = { 0x42, 0x61 };
0508     static unsigned char set_gpio_reg0[]     = { 0x44, 0x0b };
0509 
0510     tda8295_power(fe, 1);
0511 
0512     tda8295_set_easy_mode(fe, 0);
0513     tda8295_set_video_std(fe);
0514 
0515     tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl, 2);
0516     tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl2, 2);
0517     tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg6, 2);
0518     tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg0, 2);
0519     tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg7, 2);
0520     tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg10, 2);
0521     tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_reg0, 2);
0522 
0523     tda8295_agc1_out(fe, 0);
0524     tda8295_agc2_out(fe, 0);
0525 }
0526 
0527 static void tda8290_init_tuner(struct dvb_frontend *fe)
0528 {
0529     struct tda8290_priv *priv = fe->analog_demod_priv;
0530     static unsigned char tda8275_init[]  =
0531         { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
0532           0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
0533     static unsigned char tda8275a_init[] =
0534          { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
0535            0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
0536     struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
0537                   .buf=tda8275_init, .len = 14};
0538     if (priv->ver & TDA8275A)
0539         msg.buf = tda8275a_init;
0540 
0541     if (fe->ops.analog_ops.i2c_gate_ctrl)
0542         fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
0543     i2c_transfer(priv->i2c_props.adap, &msg, 1);
0544     if (fe->ops.analog_ops.i2c_gate_ctrl)
0545         fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
0546 }
0547 
0548 /*---------------------------------------------------------------------*/
0549 
0550 static void tda829x_release(struct dvb_frontend *fe)
0551 {
0552     struct tda8290_priv *priv = fe->analog_demod_priv;
0553 
0554     /* only try to release the tuner if we've
0555      * attached it from within this module */
0556     if (priv->ver & (TDA18271 | TDA8275 | TDA8275A))
0557         if (fe->ops.tuner_ops.release)
0558             fe->ops.tuner_ops.release(fe);
0559 
0560     kfree(fe->analog_demod_priv);
0561     fe->analog_demod_priv = NULL;
0562 }
0563 
0564 static struct tda18271_config tda829x_tda18271_config = {
0565     .gate    = TDA18271_GATE_ANALOG,
0566 };
0567 
0568 static int tda829x_find_tuner(struct dvb_frontend *fe)
0569 {
0570     struct tda8290_priv *priv = fe->analog_demod_priv;
0571     int i, ret, tuners_found;
0572     u32 tuner_addrs;
0573     u8 data;
0574     struct i2c_msg msg = { .flags = I2C_M_RD, .buf = &data, .len = 1 };
0575 
0576     if (fe->ops.analog_ops.i2c_gate_ctrl)
0577         fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
0578 
0579     /* probe for tuner chip */
0580     tuners_found = 0;
0581     tuner_addrs = 0;
0582     for (i = 0x60; i <= 0x63; i++) {
0583         msg.addr = i;
0584         ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
0585         if (ret == 1) {
0586             tuners_found++;
0587             tuner_addrs = (tuner_addrs << 8) + i;
0588         }
0589     }
0590     /* if there is more than one tuner, we expect the right one is
0591        behind the bridge and we choose the highest address that doesn't
0592        give a response now
0593      */
0594 
0595     if (fe->ops.analog_ops.i2c_gate_ctrl)
0596         fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
0597 
0598     if (tuners_found > 1)
0599         for (i = 0; i < tuners_found; i++) {
0600             msg.addr = tuner_addrs  & 0xff;
0601             ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
0602             if (ret == 1)
0603                 tuner_addrs = tuner_addrs >> 8;
0604             else
0605                 break;
0606         }
0607 
0608     if (tuner_addrs == 0) {
0609         tuner_addrs = 0x60;
0610         tuner_info("could not clearly identify tuner address, defaulting to %x\n",
0611                tuner_addrs);
0612     } else {
0613         tuner_addrs = tuner_addrs & 0xff;
0614         tuner_info("setting tuner address to %x\n", tuner_addrs);
0615     }
0616     priv->tda827x_addr = tuner_addrs;
0617     msg.addr = tuner_addrs;
0618 
0619     if (fe->ops.analog_ops.i2c_gate_ctrl)
0620         fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
0621     ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
0622 
0623     if (ret != 1) {
0624         tuner_warn("tuner access failed!\n");
0625         if (fe->ops.analog_ops.i2c_gate_ctrl)
0626             fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
0627         return -EREMOTEIO;
0628     }
0629 
0630     if ((data == 0x83) || (data == 0x84)) {
0631         priv->ver |= TDA18271;
0632         tda829x_tda18271_config.config = priv->cfg.config;
0633         tda829x_tda18271_config.std_map = priv->tda18271_std_map;
0634         dvb_attach(tda18271_attach, fe, priv->tda827x_addr,
0635                priv->i2c_props.adap, &tda829x_tda18271_config);
0636     } else {
0637         if ((data & 0x3c) == 0)
0638             priv->ver |= TDA8275;
0639         else
0640             priv->ver |= TDA8275A;
0641 
0642         dvb_attach(tda827x_attach, fe, priv->tda827x_addr,
0643                priv->i2c_props.adap, &priv->cfg);
0644         priv->cfg.switch_addr = priv->i2c_props.addr;
0645     }
0646     if (fe->ops.tuner_ops.init)
0647         fe->ops.tuner_ops.init(fe);
0648 
0649     if (fe->ops.tuner_ops.sleep)
0650         fe->ops.tuner_ops.sleep(fe);
0651 
0652     if (fe->ops.analog_ops.i2c_gate_ctrl)
0653         fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
0654 
0655     return 0;
0656 }
0657 
0658 static int tda8290_probe(struct tuner_i2c_props *i2c_props)
0659 {
0660 #define TDA8290_ID 0x89
0661     u8 reg = 0x1f, id;
0662     struct i2c_msg msg_read[] = {
0663         { .addr = i2c_props->addr, .flags = 0, .len = 1, .buf = &reg },
0664         { .addr = i2c_props->addr, .flags = I2C_M_RD, .len = 1, .buf = &id },
0665     };
0666 
0667     /* detect tda8290 */
0668     if (i2c_transfer(i2c_props->adap, msg_read, 2) != 2) {
0669         printk(KERN_WARNING "%s: couldn't read register 0x%02x\n",
0670                    __func__, reg);
0671         return -ENODEV;
0672     }
0673 
0674     if (id == TDA8290_ID) {
0675         if (debug)
0676             printk(KERN_DEBUG "%s: tda8290 detected @ %d-%04x\n",
0677                    __func__, i2c_adapter_id(i2c_props->adap),
0678                    i2c_props->addr);
0679         return 0;
0680     }
0681     return -ENODEV;
0682 }
0683 
0684 static int tda8295_probe(struct tuner_i2c_props *i2c_props)
0685 {
0686 #define TDA8295_ID 0x8a
0687 #define TDA8295C2_ID 0x8b
0688     u8 reg = 0x2f, id;
0689     struct i2c_msg msg_read[] = {
0690         { .addr = i2c_props->addr, .flags = 0, .len = 1, .buf = &reg },
0691         { .addr = i2c_props->addr, .flags = I2C_M_RD, .len = 1, .buf = &id },
0692     };
0693 
0694     /* detect tda8295 */
0695     if (i2c_transfer(i2c_props->adap, msg_read, 2) != 2) {
0696         printk(KERN_WARNING "%s: couldn't read register 0x%02x\n",
0697                    __func__, reg);
0698         return -ENODEV;
0699     }
0700 
0701     if ((id & 0xfe) == TDA8295_ID) {
0702         if (debug)
0703             printk(KERN_DEBUG "%s: %s detected @ %d-%04x\n",
0704                    __func__, (id == TDA8295_ID) ?
0705                    "tda8295c1" : "tda8295c2",
0706                    i2c_adapter_id(i2c_props->adap),
0707                    i2c_props->addr);
0708         return 0;
0709     }
0710 
0711     return -ENODEV;
0712 }
0713 
0714 static const struct analog_demod_ops tda8290_ops = {
0715     .set_params     = tda8290_set_params,
0716     .has_signal     = tda8290_has_signal,
0717     .standby        = tda8290_standby,
0718     .release        = tda829x_release,
0719     .i2c_gate_ctrl  = tda8290_i2c_bridge,
0720 };
0721 
0722 static const struct analog_demod_ops tda8295_ops = {
0723     .set_params     = tda8295_set_params,
0724     .has_signal     = tda8295_has_signal,
0725     .standby        = tda8295_standby,
0726     .release        = tda829x_release,
0727     .i2c_gate_ctrl  = tda8295_i2c_bridge,
0728 };
0729 
0730 struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
0731                     struct i2c_adapter *i2c_adap, u8 i2c_addr,
0732                     struct tda829x_config *cfg)
0733 {
0734     struct tda8290_priv *priv = NULL;
0735     char *name;
0736 
0737     priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
0738     if (priv == NULL)
0739         return NULL;
0740     fe->analog_demod_priv = priv;
0741 
0742     priv->i2c_props.addr     = i2c_addr;
0743     priv->i2c_props.adap     = i2c_adap;
0744     priv->i2c_props.name     = "tda829x";
0745     if (cfg) {
0746         priv->cfg.config = cfg->lna_cfg;
0747         priv->tda18271_std_map = cfg->tda18271_std_map;
0748     }
0749 
0750     if (tda8290_probe(&priv->i2c_props) == 0) {
0751         priv->ver = TDA8290;
0752         memcpy(&fe->ops.analog_ops, &tda8290_ops,
0753                sizeof(struct analog_demod_ops));
0754     }
0755 
0756     if (tda8295_probe(&priv->i2c_props) == 0) {
0757         priv->ver = TDA8295;
0758         memcpy(&fe->ops.analog_ops, &tda8295_ops,
0759                sizeof(struct analog_demod_ops));
0760     }
0761 
0762     if (cfg && cfg->no_i2c_gate)
0763         fe->ops.analog_ops.i2c_gate_ctrl = NULL;
0764 
0765     if (!(cfg) || (TDA829X_PROBE_TUNER == cfg->probe_tuner)) {
0766         tda8295_power(fe, 1);
0767         if (tda829x_find_tuner(fe) < 0)
0768             goto fail;
0769     }
0770 
0771     switch (priv->ver) {
0772     case TDA8290:
0773         name = "tda8290";
0774         break;
0775     case TDA8295:
0776         name = "tda8295";
0777         break;
0778     case TDA8290 | TDA8275:
0779         name = "tda8290+75";
0780         break;
0781     case TDA8295 | TDA8275:
0782         name = "tda8295+75";
0783         break;
0784     case TDA8290 | TDA8275A:
0785         name = "tda8290+75a";
0786         break;
0787     case TDA8295 | TDA8275A:
0788         name = "tda8295+75a";
0789         break;
0790     case TDA8290 | TDA18271:
0791         name = "tda8290+18271";
0792         break;
0793     case TDA8295 | TDA18271:
0794         name = "tda8295+18271";
0795         break;
0796     default:
0797         goto fail;
0798     }
0799     tuner_info("type set to %s\n", name);
0800 
0801     fe->ops.analog_ops.info.name = name;
0802 
0803     if (priv->ver & TDA8290) {
0804         if (priv->ver & (TDA8275 | TDA8275A))
0805             tda8290_init_tuner(fe);
0806         tda8290_init_if(fe);
0807     } else if (priv->ver & TDA8295)
0808         tda8295_init_if(fe);
0809 
0810     return fe;
0811 
0812 fail:
0813     memset(&fe->ops.analog_ops, 0, sizeof(struct analog_demod_ops));
0814 
0815     tda829x_release(fe);
0816     return NULL;
0817 }
0818 EXPORT_SYMBOL_GPL(tda829x_attach);
0819 
0820 int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
0821 {
0822     struct tuner_i2c_props i2c_props = {
0823         .adap = i2c_adap,
0824         .addr = i2c_addr,
0825     };
0826 
0827     static unsigned char soft_reset[]   = { 0x00, 0x00 };
0828     static unsigned char easy_mode_b[]  = { 0x01, 0x02 };
0829     static unsigned char easy_mode_g[]  = { 0x01, 0x04 };
0830     static unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
0831     static unsigned char addr_dto_lsb = 0x07;
0832     unsigned char data;
0833 #define PROBE_BUFFER_SIZE 8
0834     unsigned char buf[PROBE_BUFFER_SIZE];
0835     int i;
0836 
0837     /* rule out tda9887, which would return the same byte repeatedly */
0838     tuner_i2c_xfer_send_recv(&i2c_props,
0839                  soft_reset, 1, buf, PROBE_BUFFER_SIZE);
0840     for (i = 1; i < PROBE_BUFFER_SIZE; i++) {
0841         if (buf[i] != buf[0])
0842             break;
0843     }
0844 
0845     /* all bytes are equal, not a tda829x - probably a tda9887 */
0846     if (i == PROBE_BUFFER_SIZE)
0847         return -ENODEV;
0848 
0849     if ((tda8290_probe(&i2c_props) == 0) ||
0850         (tda8295_probe(&i2c_props) == 0))
0851         return 0;
0852 
0853     /* fall back to old probing method */
0854     tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2);
0855     tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
0856     tuner_i2c_xfer_send_recv(&i2c_props, &addr_dto_lsb, 1, &data, 1);
0857     if (data == 0) {
0858         tuner_i2c_xfer_send(&i2c_props, easy_mode_g, 2);
0859         tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
0860         tuner_i2c_xfer_send_recv(&i2c_props,
0861                      &addr_dto_lsb, 1, &data, 1);
0862         if (data == 0x7b) {
0863             return 0;
0864         }
0865     }
0866     tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
0867     return -ENODEV;
0868 }
0869 EXPORT_SYMBOL_GPL(tda829x_probe);
0870 
0871 MODULE_DESCRIPTION("Philips/NXP TDA8290/TDA8295 analog IF demodulator driver");
0872 MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann, Michael Krufky");
0873 MODULE_LICENSE("GPL");