V4L/DVB (6271): V4L: Add basic support for suspend/resume for saa7134
[safe/jmp/linux-2.6] / drivers / media / video / tuner-core.c
index 0363eae..9484308 100644 (file)
 #include <linux/init.h>
 #include <linux/videodev.h>
 #include <media/tuner.h>
+#include <media/tuner-types.h>
 #include <media/v4l2-common.h>
 #include "tuner-driver.h"
+#include "mt20xx.h"
+#include "tda8290.h"
+#include "tea5761.h"
+#include "tea5767.h"
+#include "tuner-simple.h"
 
 #define UNSET (-1U)
 
@@ -70,6 +76,51 @@ static struct i2c_client client_template;
 
 /* ---------------------------------------------------------------------- */
 
+static void fe_set_freq(struct tuner *t, unsigned int freq)
+{
+       struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
+
+       struct analog_parameters params = {
+               .frequency = freq,
+               .mode      = t->mode,
+               .audmode   = t->audmode,
+               .std       = t->std
+       };
+
+       if (NULL == fe_tuner_ops->set_analog_params) {
+               tuner_warn("Tuner frontend module has no way to set freq\n");
+               return;
+       }
+       fe_tuner_ops->set_analog_params(&t->fe, &params);
+}
+
+static void fe_release(struct tuner *t)
+{
+       struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
+
+       if (fe_tuner_ops->release)
+               fe_tuner_ops->release(&t->fe);
+}
+
+static void fe_standby(struct tuner *t)
+{
+       struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
+
+       if (fe_tuner_ops->sleep)
+               fe_tuner_ops->sleep(&t->fe);
+}
+
+static int fe_has_signal(struct tuner *t)
+{
+       struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
+       u16 strength;
+
+       if (fe_tuner_ops->get_rf_strength)
+               fe_tuner_ops->get_rf_strength(&t->fe, &strength);
+
+       return strength;
+}
+
 /* Set tuner frequency,  freq in Units of 62.5kHz = 1/16MHz */
 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
 {
@@ -166,11 +217,30 @@ static void tuner_i2c_address_check(struct tuner *t)
        tuner_warn("====================== WARNING! ======================\n");
 }
 
+static void attach_tda8290(struct tuner *t)
+{
+       struct tda8290_config cfg = {
+               .lna_cfg        = &t->config,
+               .tuner_callback = t->tuner_callback
+       };
+       tda8290_attach(&t->fe, t->i2c.adapter, t->i2c.addr, &cfg);
+}
+
+static void attach_simple_tuner(struct tuner *t)
+{
+       struct simple_tuner_config cfg = {
+               .type = t->type,
+               .tun  = &tuners[t->type]
+       };
+       simple_tuner_attach(&t->fe, t->i2c.adapter, t->i2c.addr, &cfg);
+}
+
 static void set_type(struct i2c_client *c, unsigned int type,
                     unsigned int new_mode_mask, unsigned int new_config,
                     int (*tuner_callback) (void *dev, int command,int arg))
 {
        struct tuner *t = i2c_get_clientdata(c);
+       struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
        unsigned char buffer[4];
 
        if (type == UNSET || type == TUNER_ABSENT) {
@@ -207,13 +277,15 @@ static void set_type(struct i2c_client *c, unsigned int type,
 
        switch (t->type) {
        case TUNER_MT2032:
-               microtune_init(t);
+               microtune_attach(&t->fe, t->i2c.adapter, t->i2c.addr);
                break;
        case TUNER_PHILIPS_TDA8290:
-               tda8290_init(t);
+       {
+               attach_tda8290(t);
                break;
+       }
        case TUNER_TEA5767:
-               if (tea5767_tuner_init(t) == EINVAL) {
+               if (tea5767_attach(&t->fe, t->i2c.adapter, t->i2c.addr) == NULL) {
                        t->type = TUNER_ABSENT;
                        t->mode_mask = T_UNINITIALIZED;
                        return;
@@ -222,7 +294,7 @@ static void set_type(struct i2c_client *c, unsigned int type,
                break;
 #ifdef CONFIG_TUNER_TEA5761
        case TUNER_TEA5761:
-               if (tea5761_tuner_init(t) == EINVAL) {
+               if (tea5761_attach(&t->fe, t->i2c.adapter, t->i2c.addr) == NULL) {
                        t->type = TUNER_ABSENT;
                        t->mode_mask = T_UNINITIALIZED;
                        return;
@@ -240,7 +312,7 @@ static void set_type(struct i2c_client *c, unsigned int type,
                buffer[2] = 0x86;
                buffer[3] = 0x54;
                i2c_master_send(c, buffer, 4);
-               default_tuner_init(t);
+               attach_simple_tuner(t);
                break;
        case TUNER_PHILIPS_TD1316:
                buffer[0] = 0x0b;
@@ -248,16 +320,28 @@ static void set_type(struct i2c_client *c, unsigned int type,
                buffer[2] = 0x86;
                buffer[3] = 0xa4;
                i2c_master_send(c,buffer,4);
-               default_tuner_init(t);
+               attach_simple_tuner(t);
                break;
        case TUNER_TDA9887:
                tda9887_tuner_init(t);
                break;
        default:
-               default_tuner_init(t);
+               attach_simple_tuner(t);
                break;
        }
 
+       if (fe_tuner_ops->set_analog_params) {
+               strlcpy(t->i2c.name, fe_tuner_ops->info.name, sizeof(t->i2c.name));
+
+               t->ops.set_tv_freq    = fe_set_freq;
+               t->ops.set_radio_freq = fe_set_freq;
+               t->ops.standby        = fe_standby;
+               t->ops.release        = fe_release;
+               t->ops.has_signal     = fe_has_signal;
+       }
+
+       tuner_info("type set to %s\n", t->i2c.name);
+
        if (t->mode_mask == T_UNINITIALIZED)
                t->mode_mask = new_mode_mask;
 
@@ -429,6 +513,7 @@ static int tuner_fixup_std(struct tuner *t)
 static void tuner_status(struct tuner *t)
 {
        unsigned long freq, freq_fraction;
+       struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
        const char *p;
 
        switch (t->mode) {
@@ -449,6 +534,15 @@ static void tuner_status(struct tuner *t)
        tuner_info("Standard:        0x%08lx\n", (unsigned long)t->std);
        if (t->mode != V4L2_TUNER_RADIO)
               return;
+       if (fe_tuner_ops->get_status) {
+               u32 tuner_status;
+
+               fe_tuner_ops->get_status(&t->fe, &tuner_status);
+               if (tuner_status & TUNER_STATUS_LOCKED)
+                       tuner_info("Tuner is locked.\n");
+               if (tuner_status & TUNER_STATUS_STEREO)
+                       tuner_info("Stereo:          yes\n");
+       }
        if (t->ops.has_signal) {
                tuner_info("Signal strength: %d\n", t->ops.has_signal(t));
        }
@@ -502,7 +596,7 @@ static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
                switch (addr) {
 #ifdef CONFIG_TUNER_TEA5761
                case 0x10:
-                       if (tea5761_autodetection(t) != EINVAL) {
+                       if (tea5761_autodetection(t->i2c.adapter, t->i2c.addr) != EINVAL) {
                                t->type = TUNER_TEA5761;
                                t->mode_mask = T_RADIO;
                                t->mode = T_STANDBY;
@@ -519,7 +613,7 @@ static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
                case 0x4b:
                        /* If chip is not tda8290, don't register.
                           since it can be tda9887*/
-                       if (tda8290_probe(t) == 0) {
+                       if (tda8290_probe(t->i2c.adapter, t->i2c.addr) == 0) {
                                tuner_dbg("chip at addr %x is a tda8290\n", addr);
                        } else {
                                /* Default is being tda9887 */
@@ -530,7 +624,7 @@ static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
                        }
                        break;
                case 0x60:
-                       if (tea5767_autodetection(t) != EINVAL) {
+                       if (tea5767_autodetection(t->i2c.adapter, t->i2c.addr) != EINVAL) {
                                t->type = TUNER_TEA5767;
                                t->mode_mask = T_RADIO;
                                t->mode = T_STANDBY;
@@ -567,6 +661,28 @@ static int tuner_probe(struct i2c_adapter *adap)
                normal_i2c[1] = I2C_CLIENT_END;
        }
 
+       /* HACK: Ignore 0x6b and 0x6f on cx88 boards.
+        * FusionHDTV5 RT Gold has an ir receiver at 0x6b
+        * and an RTC at 0x6f which can get corrupted if probed.
+        */
+       if ((adap->id == I2C_HW_B_CX2388x) ||
+           (adap->id == I2C_HW_B_CX23885)) {
+               unsigned int i = 0;
+
+               while (i < I2C_CLIENT_MAX_OPTS && ignore[i] != I2C_CLIENT_END)
+                       i += 2;
+               if (i + 4 < I2C_CLIENT_MAX_OPTS) {
+                       ignore[i+0] = adap->nr;
+                       ignore[i+1] = 0x6b;
+                       ignore[i+2] = adap->nr;
+                       ignore[i+3] = 0x6f;
+                       ignore[i+4] = I2C_CLIENT_END;
+               } else
+                       printk(KERN_WARNING "tuner: "
+                              "too many options specified "
+                              "in i2c probe ignore list!\n");
+       }
+
        default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
 
        if (adap->class & I2C_CLASS_TV_ANALOG)
@@ -634,6 +750,7 @@ static inline int check_v4l2(struct tuner *t)
 static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
 {
        struct tuner *t = i2c_get_clientdata(client);
+       struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
 
        if (tuner_debug>1)
                v4l_i2c_print_ioctl(&(t->i2c),cmd);
@@ -720,16 +837,27 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
                                return 0;
 
                        if (V4L2_TUNER_RADIO == t->mode) {
-                               if (t->ops.has_signal)
-                                       vt->signal = t->ops.has_signal(t);
-                               if (t->ops.is_stereo) {
-                                       if (t->ops.is_stereo(t))
-                                               vt->flags |=
-                                                   VIDEO_TUNER_STEREO_ON;
+                               if (fe_tuner_ops->get_status) {
+                                       u32 tuner_status;
+
+                                       fe_tuner_ops->get_status(&t->fe, &tuner_status);
+                                       if (tuner_status & TUNER_STATUS_STEREO)
+                                               vt->flags |= VIDEO_TUNER_STEREO_ON;
                                        else
-                                               vt->flags &=
-                                                   ~VIDEO_TUNER_STEREO_ON;
+                                               vt->flags &= ~VIDEO_TUNER_STEREO_ON;
+                               } else {
+                                       if (t->ops.is_stereo) {
+                                               if (t->ops.is_stereo(t))
+                                                       vt->flags |=
+                                                               VIDEO_TUNER_STEREO_ON;
+                                               else
+                                                       vt->flags &=
+                                                               ~VIDEO_TUNER_STEREO_ON;
+                                       }
                                }
+                               if (t->ops.has_signal)
+                                       vt->signal = t->ops.has_signal(t);
+
                                vt->flags |= VIDEO_TUNER_LOW;   /* Allow freqs at 62.5 Hz */
 
                                vt->rangelow = radio_range[0] * 16000;
@@ -751,9 +879,17 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
                        if (check_v4l2(t) == EINVAL)
                                return 0;
 
-                       if (V4L2_TUNER_RADIO == t->mode && t->ops.is_stereo)
-                               va->mode = t->ops.is_stereo(t)
-                                   ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
+                       if (V4L2_TUNER_RADIO == t->mode) {
+                               if (fe_tuner_ops->get_status) {
+                                       u32 tuner_status;
+
+                                       fe_tuner_ops->get_status(&t->fe, &tuner_status);
+                                       va->mode = (tuner_status & TUNER_STATUS_STEREO)
+                                           ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
+                               } else if (t->ops.is_stereo)
+                                       va->mode = t->ops.is_stereo(t)
+                                           ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
+                       }
                        return 0;
                }
 #endif
@@ -804,6 +940,15 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
                                return 0;
                        switch_v4l2();
                        f->type = t->mode;
+                       if (fe_tuner_ops->get_frequency) {
+                               u32 abs_freq;
+
+                               fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
+                               f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
+                                       (abs_freq * 2 + 125/2) / 125 :
+                                       (abs_freq + 62500/2) / 62500;
+                               break;
+                       }
                        f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
                                t->radio_freq : t->tv_freq;
                        break;
@@ -828,16 +973,22 @@ static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
                        }
 
                        /* radio mode */
-                       if (t->ops.has_signal)
-                               tuner->signal = t->ops.has_signal(t);
-
                        tuner->rxsubchans =
                                V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
-                       if (t->ops.is_stereo) {
-                               tuner->rxsubchans = t->ops.is_stereo(t) ?
+                       if (fe_tuner_ops->get_status) {
+                               u32 tuner_status;
+
+                               fe_tuner_ops->get_status(&t->fe, &tuner_status);
+                               tuner->rxsubchans = (tuner_status & TUNER_STATUS_STEREO) ?
                                        V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
+                       } else {
+                               if (t->ops.is_stereo) {
+                                       tuner->rxsubchans = t->ops.is_stereo(t) ?
+                                               V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
+                               }
                        }
-
+                       if (t->ops.has_signal)
+                               tuner->signal = t->ops.has_signal(t);
                        tuner->capability |=
                            V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
                        tuner->audmode = t->audmode;