V4L/DVB (6785): tda8290: remove dependency on struct tuner
[pandora-kernel.git] / drivers / media / video / tda8290.c
index 18e6a09..d0d13bc 100644 (file)
 #include <linux/i2c.h>
 #include <linux/delay.h>
 #include <linux/videodev.h>
+#include "tuner-driver.h"
+#include "tuner-i2c.h"
 #include "tda8290.h"
 #include "tda827x.h"
 #include "tda18271.h"
 
-static int tuner_debug;
-module_param_named(debug, tuner_debug, int, 0644);
+static int debug;
+module_param(debug, int, 0644);
 MODULE_PARM_DESC(debug, "enable verbose debug messages");
 
-#define PREFIX "tda8290 "
+#define PREFIX "tda8290"
 
 /* ---------------------------------------------------------------------- */
 
@@ -50,8 +52,6 @@ struct tda8290_priv {
 #define TDA18271 16
 
        struct tda827x_config cfg;
-
-       struct tuner *t;
 };
 
 /*---------------------------------------------------------------------*/
@@ -110,31 +110,31 @@ static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
 
 /*---------------------------------------------------------------------*/
 
-static void set_audio(struct dvb_frontend *fe)
+static void set_audio(struct dvb_frontend *fe,
+                     struct analog_parameters *params)
 {
        struct tda8290_priv *priv = fe->analog_demod_priv;
-       struct tuner *t = priv->t;
        char* mode;
 
-       if (t->std & V4L2_STD_MN) {
+       if (params->std & V4L2_STD_MN) {
                priv->tda8290_easy_mode = 0x01;
                mode = "MN";
-       } else if (t->std & V4L2_STD_B) {
+       } else if (params->std & V4L2_STD_B) {
                priv->tda8290_easy_mode = 0x02;
                mode = "B";
-       } else if (t->std & V4L2_STD_GH) {
+       } else if (params->std & V4L2_STD_GH) {
                priv->tda8290_easy_mode = 0x04;
                mode = "GH";
-       } else if (t->std & V4L2_STD_PAL_I) {
+       } else if (params->std & V4L2_STD_PAL_I) {
                priv->tda8290_easy_mode = 0x08;
                mode = "I";
-       } else if (t->std & V4L2_STD_DK) {
+       } else if (params->std & V4L2_STD_DK) {
                priv->tda8290_easy_mode = 0x10;
                mode = "DK";
-       } else if (t->std & V4L2_STD_SECAM_L) {
+       } else if (params->std & V4L2_STD_SECAM_L) {
                priv->tda8290_easy_mode = 0x20;
                mode = "L";
-       } else if (t->std & V4L2_STD_SECAM_LC) {
+       } else if (params->std & V4L2_STD_SECAM_LC) {
                priv->tda8290_easy_mode = 0x40;
                mode = "LC";
        } else {
@@ -145,10 +145,10 @@ static void set_audio(struct dvb_frontend *fe)
        tuner_dbg("setting tda829x to system %s\n", mode);
 }
 
-static void tda8290_set_freq(struct dvb_frontend *fe, unsigned int freq)
+static void tda8290_set_params(struct dvb_frontend *fe,
+                              struct analog_parameters *params)
 {
        struct tda8290_priv *priv = fe->analog_demod_priv;
-       struct tuner *t = priv->t;
 
        unsigned char soft_reset[]  = { 0x00, 0x00 };
        unsigned char easy_mode[]   = { 0x01, priv->tda8290_easy_mode };
@@ -172,16 +172,10 @@ static void tda8290_set_freq(struct dvb_frontend *fe, unsigned int freq)
                      pll_stat;
        int i;
 
-       struct analog_parameters params = {
-               .frequency = freq,
-               .mode      = t->mode,
-               .audmode   = t->audmode,
-               .std       = t->std
-       };
-
-       set_audio(fe);
+       set_audio(fe, params);
 
-       tuner_dbg("tda827xa config is 0x%02x\n", t->config);
+       if (priv->cfg.config)
+               tuner_dbg("tda827xa config is 0x%02x\n", *priv->cfg.config);
        tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2);
        tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2);
        tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2);
@@ -200,7 +194,7 @@ static void tda8290_set_freq(struct dvb_frontend *fe, unsigned int freq)
        tda8290_i2c_bridge(fe, 1);
 
        if (fe->ops.tuner_ops.set_analog_params)
-               fe->ops.tuner_ops.set_analog_params(fe, &params);
+               fe->ops.tuner_ops.set_analog_params(fe, params);
 
        for (i = 0; i < 3; i++) {
                tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
@@ -363,23 +357,16 @@ static int tda8295_has_signal(struct dvb_frontend *fe)
 
 /*---------------------------------------------------------------------*/
 
-static void tda8295_set_freq(struct dvb_frontend *fe, unsigned int freq)
+static void tda8295_set_params(struct dvb_frontend *fe,
+                              struct analog_parameters *params)
 {
        struct tda8290_priv *priv = fe->analog_demod_priv;
-       struct tuner *t = priv->t;
 
        unsigned char blanking_mode[]     = { 0x1d, 0x00 };
 
-       struct analog_parameters params = {
-               .frequency = freq,
-               .mode      = t->mode,
-               .audmode   = t->audmode,
-               .std       = t->std
-       };
+       set_audio(fe, params);
 
-       set_audio(fe);
-
-       tuner_dbg("%s: freq = %d\n", __FUNCTION__, freq);
+       tuner_dbg("%s: freq = %d\n", __FUNCTION__, params->frequency);
 
        tda8295_power(fe, 1);
        tda8295_agc1_out(fe, 1);
@@ -396,7 +383,7 @@ static void tda8295_set_freq(struct dvb_frontend *fe, unsigned int freq)
        tda8295_i2c_bridge(fe, 1);
 
        if (fe->ops.tuner_ops.set_analog_params)
-               fe->ops.tuner_ops.set_analog_params(fe, &params);
+               fe->ops.tuner_ops.set_analog_params(fe, params);
 
        if (priv->cfg.agcf)
                priv->cfg.agcf(fe);
@@ -453,13 +440,13 @@ static void tda8295_standby(struct dvb_frontend *fe)
 static void tda8290_init_if(struct dvb_frontend *fe)
 {
        struct tda8290_priv *priv = fe->analog_demod_priv;
-       struct tuner *t = priv->t;
 
        unsigned char set_VS[] = { 0x30, 0x6F };
        unsigned char set_GP00_CF[] = { 0x20, 0x01 };
        unsigned char set_GP01_CF[] = { 0x20, 0x0B };
 
-       if ((t->config == 1) || (t->config == 2))
+       if ((priv->cfg.config) &&
+           ((*priv->cfg.config == 1) || (*priv->cfg.config == 2)))
                tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
        else
                tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
@@ -527,7 +514,6 @@ static int tda829x_find_tuner(struct dvb_frontend *fe)
 {
        struct tda8290_priv *priv = fe->analog_demod_priv;
        struct analog_tuner_ops *ops = fe->ops.analog_demod_ops;
-       struct tuner *t = priv->t;
        int i, ret, tuners_found;
        u32 tuner_addrs;
        u8 data;
@@ -587,7 +573,7 @@ static int tda829x_find_tuner(struct dvb_frontend *fe)
 
        if (data == 0x83) {
                priv->ver |= TDA18271;
-               tda18271_attach(&t->fe, priv->tda827x_addr,
+               tda18271_attach(fe, priv->tda827x_addr,
                                priv->i2c_props.adap);
        } else {
                if ((data & 0x3c) == 0)
@@ -595,39 +581,16 @@ static int tda829x_find_tuner(struct dvb_frontend *fe)
                else
                        priv->ver |= TDA8275A;
 
-               tda827x_attach(&t->fe, priv->tda827x_addr,
+               tda827x_attach(fe, priv->tda827x_addr,
                               priv->i2c_props.adap, &priv->cfg);
-
-               /* FIXME: tda827x module doesn't probe the tuner until
-                * tda827x_initial_sleep is called
-                */
-               if (t->fe.ops.tuner_ops.sleep)
-                       t->fe.ops.tuner_ops.sleep(&t->fe);
        }
-       ops->i2c_gate_ctrl(fe, 0);
+       if (fe->ops.tuner_ops.init)
+               fe->ops.tuner_ops.init(fe);
 
-       switch (priv->ver) {
-       case TDA8290 | TDA8275:
-               strlcpy(t->i2c->name, "tda8290+75", sizeof(t->i2c->name));
-               break;
-       case TDA8295 | TDA8275:
-               strlcpy(t->i2c->name, "tda8295+75", sizeof(t->i2c->name));
-               break;
-       case TDA8290 | TDA8275A:
-               strlcpy(t->i2c->name, "tda8290+75a", sizeof(t->i2c->name));
-               break;
-       case TDA8295 | TDA8275A:
-               strlcpy(t->i2c->name, "tda8295+75a", sizeof(t->i2c->name));
-               break;
-       case TDA8290 | TDA18271:
-               strlcpy(t->i2c->name, "tda8290+18271", sizeof(t->i2c->name));
-               break;
-       case TDA8295 | TDA18271:
-               strlcpy(t->i2c->name, "tda8295+18271", sizeof(t->i2c->name));
-               break;
-       default:
-               return -EINVAL;
-       }
+       if (fe->ops.tuner_ops.sleep)
+               fe->ops.tuner_ops.sleep(fe);
+
+       ops->i2c_gate_ctrl(fe, 0);
 
        return 0;
 }
@@ -642,7 +605,7 @@ static int tda8290_probe(struct tuner_i2c_props *i2c_props)
        tuner_i2c_xfer_recv(i2c_props, &tda8290_id[1], 1);
 
        if (tda8290_id[1] == TDA8290_ID) {
-               if (tuner_debug)
+               if (debug)
                        printk(KERN_DEBUG "%s: tda8290 detected @ %d-%04x\n",
                               __FUNCTION__, i2c_adapter_id(i2c_props->adap),
                               i2c_props->addr);
@@ -662,7 +625,7 @@ static int tda8295_probe(struct tuner_i2c_props *i2c_props)
        tuner_i2c_xfer_recv(i2c_props, &tda8295_id[1], 1);
 
        if (tda8295_id[1] == TDA8295_ID) {
-               if (tuner_debug)
+               if (debug)
                        printk(KERN_DEBUG "%s: tda8295 detected @ %d-%04x\n",
                               __FUNCTION__, i2c_adapter_id(i2c_props->adap),
                               i2c_props->addr);
@@ -673,8 +636,7 @@ static int tda8295_probe(struct tuner_i2c_props *i2c_props)
 }
 
 static struct analog_tuner_ops tda8290_tuner_ops = {
-       .set_tv_freq    = tda8290_set_freq,
-       .set_radio_freq = tda8290_set_freq,
+       .set_params     = tda8290_set_params,
        .has_signal     = tda8290_has_signal,
        .standby        = tda8290_standby,
        .release        = tda829x_release,
@@ -682,29 +644,31 @@ static struct analog_tuner_ops tda8290_tuner_ops = {
 };
 
 static struct analog_tuner_ops tda8295_tuner_ops = {
-       .set_tv_freq    = tda8295_set_freq,
-       .set_radio_freq = tda8295_set_freq,
+       .set_params     = tda8295_set_params,
        .has_signal     = tda8295_has_signal,
        .standby        = tda8295_standby,
        .release        = tda829x_release,
        .i2c_gate_ctrl  = tda8295_i2c_bridge,
 };
 
-int tda829x_attach(struct tuner *t)
+struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
+                                   struct i2c_adapter *i2c_adap, u8 i2c_addr,
+                                   struct tda829x_config *cfg)
 {
-       struct dvb_frontend *fe = &t->fe;
        struct tda8290_priv *priv = NULL;
+       char *name;
 
        priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
        if (priv == NULL)
-               return -ENOMEM;
+               return NULL;
        fe->analog_demod_priv = priv;
 
-       priv->i2c_props.addr     = t->i2c->addr;
-       priv->i2c_props.adap     = t->i2c->adapter;
-       priv->cfg.config         = &t->config;
-       priv->cfg.tuner_callback = t->tuner_callback;
-       priv->t = t;
+       priv->i2c_props.addr     = i2c_addr;
+       priv->i2c_props.adap     = i2c_adap;
+       if (cfg) {
+               priv->cfg.config         = cfg->lna_cfg;
+               priv->cfg.tuner_callback = cfg->tuner_callback;
+       }
 
        if (tda8290_probe(&priv->i2c_props) == 0) {
                priv->ver = TDA8290;
@@ -717,7 +681,31 @@ int tda829x_attach(struct tuner *t)
        }
 
        if (tda829x_find_tuner(fe) < 0)
-               return -EINVAL;
+               goto fail;
+
+       switch (priv->ver) {
+       case TDA8290 | TDA8275:
+               name = "tda8290+75";
+               break;
+       case TDA8295 | TDA8275:
+               name = "tda8295+75";
+               break;
+       case TDA8290 | TDA8275A:
+               name = "tda8290+75a";
+               break;
+       case TDA8295 | TDA8275A:
+               name = "tda8295+75a";
+               break;
+       case TDA8290 | TDA18271:
+               name = "tda8290+18271";
+               break;
+       case TDA8295 | TDA18271:
+               name = "tda8295+18271";
+               break;
+       default:
+               goto fail;
+       }
+       tuner_info("type set to %s\n", name);
 
        if (priv->ver & TDA8290) {
                tda8290_init_tuner(fe);
@@ -725,19 +713,20 @@ int tda829x_attach(struct tuner *t)
        } else if (priv->ver & TDA8295)
                tda8295_init_if(fe);
 
-       tuner_info("type set to %s\n", t->i2c->name);
-
-       t->mode = V4L2_TUNER_ANALOG_TV;
+       return fe;
 
-       return 0;
+fail:
+       tda829x_release(fe);
+       fe->ops.analog_demod_ops = NULL;
+       return NULL;
 }
 EXPORT_SYMBOL_GPL(tda829x_attach);
 
-int tda829x_probe(struct tuner *t)
+int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
 {
        struct tuner_i2c_props i2c_props = {
-               .adap = t->i2c->adapter,
-               .addr = t->i2c->addr
+               .adap = i2c_adap,
+               .addr = i2c_addr,
        };
 
        unsigned char soft_reset[]   = { 0x00, 0x00 };
@@ -746,6 +735,21 @@ int tda829x_probe(struct tuner *t)
        unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
        unsigned char addr_dto_lsb = 0x07;
        unsigned char data;
+#define PROBE_BUFFER_SIZE 8
+       unsigned char buf[PROBE_BUFFER_SIZE];
+       int i;
+
+       /* rule out tda9887, which would return the same byte repeatedly */
+       tuner_i2c_xfer_send(&i2c_props, soft_reset, 1);
+       tuner_i2c_xfer_recv(&i2c_props, buf, PROBE_BUFFER_SIZE);
+       for (i = 1; i < PROBE_BUFFER_SIZE; i++) {
+               if (buf[i] != buf[0])
+                       break;
+       }
+
+       /* all bytes are equal, not a tda829x - probably a tda9887 */
+       if (i == PROBE_BUFFER_SIZE)
+               return -ENODEV;
 
        if ((tda8290_probe(&i2c_props) == 0) ||
            (tda8295_probe(&i2c_props) == 0))