V4L/DVB (4608c): Fix I2C dependencies for saa7146 modules
[safe/jmp/linux-2.6] / drivers / media / video / msp3400-driver.c
index 4c0c7be..56246b8 100644 (file)
 #include <linux/videodev.h>
 #include <linux/videodev2.h>
 #include <media/v4l2-common.h>
-#include <media/audiochip.h>
+#include <media/tvaudio.h>
+#include <media/msp3400.h>
 #include <linux/kthread.h>
 #include <linux/suspend.h>
-#include "msp3400.h"
+#include "msp3400-driver.h"
 
 /* ---------------------------------------------------------------------- */
 
@@ -66,26 +67,26 @@ MODULE_LICENSE("GPL");
 
 /* module parameters */
 static int opmode   = OPMODE_AUTO;
-int debug    = 0;    /* debug output */
-int once     = 0;    /* no continous stereo monitoring */
-int amsound  = 0;    /* hard-wire AM sound at 6.5 Hz (france),
-                              the autoscan seems work well only with FM... */
-int standard = 1;    /* Override auto detect of audio standard, if needed. */
-int dolby    = 0;
-
-int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual
+int msp_debug;          /* msp_debug output */
+int msp_once;           /* no continous stereo monitoring */
+int msp_amsound;        /* hard-wire AM sound at 6.5 Hz (france),
+                           the autoscan seems work well only with FM... */
+int msp_standard = 1;    /* Override auto detect of audio msp_standard, if needed. */
+int msp_dolby;
+
+int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
                                        (msp34xxg only) 0x00a0-0x03c0 */
 
 /* read-only */
 module_param(opmode,           int, 0444);
 
 /* read-write */
-module_param(once,             int, 0644);
-module_param(debug,            int, 0644);
-module_param(stereo_threshold, int, 0644);
-module_param(standard,         int, 0644);
-module_param(amsound,          int, 0644);
-module_param(dolby,            int, 0644);
+module_param_named(once,msp_once,                      bool, 0644);
+module_param_named(debug,msp_debug,                    int,  0644);
+module_param_named(stereo_threshold,msp_stereo_thresh, int,  0644);
+module_param_named(standard,msp_standard,              int,  0644);
+module_param_named(amsound,msp_amsound,                bool, 0644);
+module_param_named(dolby,msp_dolby,                    bool, 0644);
 
 MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
@@ -106,10 +107,8 @@ MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
 
 /* Addresses to scan */
 static unsigned short normal_i2c[] = { 0x80 >> 1, 0x88 >> 1, I2C_CLIENT_END };
-
 I2C_CLIENT_INSMOD;
 
-
 /* ----------------------------------------------------------------------- */
 /* functions for talking to the MSP3400C Sound processor                   */
 
@@ -129,7 +128,7 @@ int msp_reset(struct i2c_client *client)
                { client->addr, I2C_M_RD, 2, read  },
        };
 
-       v4l_dbg(3, client, "msp_reset\n");
+       v4l_dbg(3, msp_debug, client, "msp_reset\n");
        if (i2c_transfer(client->adapter, &reset[0], 1) != 1 ||
            i2c_transfer(client->adapter, &reset[1], 1) != 1 ||
            i2c_transfer(client->adapter, test, 2) != 2) {
@@ -167,7 +166,7 @@ static int msp_read(struct i2c_client *client, int dev, int addr)
                return -1;
        }
        retval = read[0] << 8 | read[1];
-       v4l_dbg(3, client, "msp_read(0x%x, 0x%x): 0x%x\n", dev, addr, retval);
+       v4l_dbg(3, msp_debug, client, "msp_read(0x%x, 0x%x): 0x%x\n", dev, addr, retval);
        return retval;
 }
 
@@ -192,7 +191,7 @@ static int msp_write(struct i2c_client *client, int dev, int addr, int val)
        buffer[3] = val  >> 8;
        buffer[4] = val  &  0xff;
 
-       v4l_dbg(3, client, "msp_write(0x%x, 0x%x, 0x%x)\n", dev, addr, val);
+       v4l_dbg(3, msp_debug, client, "msp_write(0x%x, 0x%x, 0x%x)\n", dev, addr, val);
        for (err = 0; err < 3; err++) {
                if (i2c_master_send(client, buffer, 5) == 5)
                        break;
@@ -247,117 +246,85 @@ int msp_write_dsp(struct i2c_client *client, int addr, int val)
  * ----------------------------------------------------------------------- */
 
 static int scarts[3][9] = {
-       /* MASK    IN1     IN2     IN1_DA  IN2_DA  IN3     IN4     MONO    MUTE   */
+       /* MASK   IN1     IN2     IN3     IN4     IN1_DA  IN2_DA  MONO    MUTE   */
        /* SCART DSP Input select */
-       { 0x0320, 0x0000, 0x0200, -1,     -1,     0x0300, 0x0020, 0x0100, 0x0320 },
+       { 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1,     -1,     0x0100, 0x0320 },
        /* SCART1 Output select */
-       { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
+       { 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
        /* SCART2 Output select */
-       { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
+       { 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
 };
 
 static char *scart_names[] = {
-       "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
+       "in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
 };
 
 void msp_set_scart(struct i2c_client *client, int in, int out)
 {
        struct msp_state *state = i2c_get_clientdata(client);
 
-       state->in_scart=in;
+       state->in_scart = in;
 
-       if (in >= 1 && in <= 8 && out >= 0 && out <= 2) {
-               if (-1 == scarts[out][in])
+       if (in >= 0 && in <= 7 && out >= 0 && out <= 2) {
+               if (-1 == scarts[out][in + 1])
                        return;
 
-               state->acb &= ~scarts[out][SCART_MASK];
-               state->acb |=  scarts[out][in];
+               state->acb &= ~scarts[out][0];
+               state->acb |=  scarts[out][in + 1];
        } else
                state->acb = 0xf60; /* Mute Input and SCART 1 Output */
 
-       v4l_dbg(1, client, "scart switch: %s => %d (ACB=0x%04x)\n",
+       v4l_dbg(1, msp_debug, client, "scart switch: %s => %d (ACB=0x%04x)\n",
                                                scart_names[in], out, state->acb);
        msp_write_dsp(client, 0x13, state->acb);
 
        /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
-       msp_write_dem(client, 0x40, state->i2s_mode);
-}
-
-void msp_set_mute(struct i2c_client *client)
-{
-       v4l_dbg(1, client, "mute audio\n");
-       msp_write_dsp(client, 0x0000, 0); /* loudspeaker */
-       msp_write_dsp(client, 0x0006, 0); /* headphones */
+       if (state->has_i2s_conf)
+               msp_write_dem(client, 0x40, state->i2s_mode);
 }
 
 void msp_set_audio(struct i2c_client *client)
 {
        struct msp_state *state = i2c_get_clientdata(client);
-       int val = 0, bal = 0, bass, treble;
+       int bal = 0, bass, treble, loudness;
+       int val = 0;
+       int reallymuted = state->muted | state->scan_in_progress;
 
-       if (!state->muted)
+       if (!reallymuted)
                val = (state->volume * 0x7f / 65535) << 8;
+
+       v4l_dbg(1, msp_debug, client, "mute=%s scanning=%s volume=%d\n",
+               state->muted ? "on" : "off", state->scan_in_progress ? "yes" : "no",
+               state->volume);
+
+       msp_write_dsp(client, 0x0000, val);
+       msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
+       if (state->has_scart2_out_volume)
+               msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
+       if (state->has_headphones)
+               msp_write_dsp(client, 0x0006, val);
+       if (!state->has_sound_processing)
+               return;
+
        if (val)
-               bal = (state->balance / 256) - 128;
+               bal = (u8)((state->balance / 256) - 128);
        bass = ((state->bass - 32768) * 0x60 / 65535) << 8;
        treble = ((state->treble - 32768) * 0x60 / 65535) << 8;
+       loudness = state->loudness ? ((5 * 4) << 8) : 0;
 
-       v4l_dbg(1, client, "mute=%s volume=%d balance=%d bass=%d treble=%d\n",
-               state->muted ? "on" : "off", state->volume, state->balance,
-               state->bass, state->treble);
+       v4l_dbg(1, msp_debug, client, "balance=%d bass=%d treble=%d loudness=%d\n",
+               state->balance, state->bass, state->treble, state->loudness);
 
-       msp_write_dsp(client, 0x0000, val); /* loudspeaker */
-       msp_write_dsp(client, 0x0006, val); /* headphones */
-       msp_write_dsp(client, 0x0007, state->muted ? 0x1 : (val | 0x1));
        msp_write_dsp(client, 0x0001, bal << 8);
-       msp_write_dsp(client, 0x0002, bass); /* loudspeaker */
-       msp_write_dsp(client, 0x0003, treble); /* loudspeaker */
-}
-
-int msp_modus(struct i2c_client *client, int norm)
-{
-       switch (norm) {
-       case VIDEO_MODE_PAL:
-               v4l_dbg(1, client, "video mode selected to PAL\n");
-
-#if 1
-               /* experimental: not sure this works with all chip versions */
-               return 0x7003;
-#else
-               /* previous value, try this if it breaks ... */
-               return 0x1003;
-#endif
-       case VIDEO_MODE_NTSC:  /* BTSC */
-               v4l_dbg(1, client, "video mode selected to NTSC\n");
-               return 0x2003;
-       case VIDEO_MODE_SECAM:
-               v4l_dbg(1, client, "video mode selected to SECAM\n");
-               return 0x0003;
-       case VIDEO_MODE_RADIO:
-               v4l_dbg(1, client, "video mode selected to Radio\n");
-               return 0x0003;
-       case VIDEO_MODE_AUTO:
-               v4l_dbg(1, client, "video mode selected to Auto\n");
-               return 0x2003;
-       default:
-               return 0x0003;
-       }
-}
-
-int msp_standard(int norm)
-{
-       switch (norm) {
-       case VIDEO_MODE_PAL:
-               return 1;
-       case VIDEO_MODE_NTSC:  /* BTSC */
-               return 0x0020;
-       case VIDEO_MODE_SECAM:
-               return 1;
-       case VIDEO_MODE_RADIO:
-               return 0x0040;
-       default:
-               return 1;
-       }
+       msp_write_dsp(client, 0x0002, bass);
+       msp_write_dsp(client, 0x0003, treble);
+       msp_write_dsp(client, 0x0004, loudness);
+       if (!state->has_headphones)
+               return;
+       msp_write_dsp(client, 0x0030, bal << 8);
+       msp_write_dsp(client, 0x0031, bass);
+       msp_write_dsp(client, 0x0032, treble);
+       msp_write_dsp(client, 0x0033, loudness);
 }
 
 /* ------------------------------------------------------------------------ */
@@ -369,7 +336,6 @@ static void msp_wake_thread(struct i2c_client *client)
 
        if (NULL == state->kthread)
                return;
-       msp_set_mute(client);
        state->watch_stereo = 0;
        state->restart = 1;
        wake_up_interruptible(&state->wq);
@@ -396,20 +362,16 @@ int msp_sleep(struct msp_state *state, int timeout)
 }
 
 /* ------------------------------------------------------------------------ */
-
-static int msp_mode_v4l2_to_v4l1(int rxsubchans)
+#ifdef CONFIG_VIDEO_V4L1
+static int msp_mode_v4l2_to_v4l1(int rxsubchans, int audmode)
 {
-       int mode = 0;
-
-       if (rxsubchans & V4L2_TUNER_SUB_STEREO)
-               mode |= VIDEO_SOUND_STEREO;
-       if (rxsubchans & V4L2_TUNER_SUB_LANG2)
-               mode |= VIDEO_SOUND_LANG2;
-       if (rxsubchans & V4L2_TUNER_SUB_LANG1)
-               mode |= VIDEO_SOUND_LANG1;
-       if (mode == 0)
-               mode |= VIDEO_SOUND_MONO;
-       return mode;
+       if (rxsubchans == V4L2_TUNER_SUB_MONO)
+               return VIDEO_SOUND_MONO;
+       if (rxsubchans == V4L2_TUNER_SUB_STEREO)
+               return VIDEO_SOUND_STEREO;
+       if (audmode == V4L2_TUNER_MODE_LANG2)
+               return VIDEO_SOUND_LANG2;
+       return VIDEO_SOUND_LANG1;
 }
 
 static int msp_mode_v4l1_to_v4l2(int mode)
@@ -422,109 +384,43 @@ static int msp_mode_v4l1_to_v4l2(int mode)
                return V4L2_TUNER_MODE_LANG1;
        return V4L2_TUNER_MODE_MONO;
 }
-
-static void msp_any_detect_stereo(struct i2c_client *client)
-{
-       struct msp_state *state  = i2c_get_clientdata(client);
-
-       switch (state->opmode) {
-       case OPMODE_MANUAL:
-       case OPMODE_AUTODETECT:
-               autodetect_stereo(client);
-               break;
-       case OPMODE_AUTOSELECT:
-               msp34xxg_detect_stereo(client);
-               break;
-       }
-}
-
-static struct v4l2_queryctrl msp_qctrl[] = {
-       {
-               .id            = V4L2_CID_AUDIO_VOLUME,
-               .name          = "Volume",
-               .minimum       = 0,
-               .maximum       = 65535,
-               .step          = 65535/100,
-               .default_value = 58880,
-               .flags         = 0,
-               .type          = V4L2_CTRL_TYPE_INTEGER,
-       },{
-               .id            = V4L2_CID_AUDIO_BALANCE,
-               .name          = "Balance",
-               .minimum       = 0,
-               .maximum       = 65535,
-               .step          = 65535/100,
-               .default_value = 32768,
-               .flags         = 0,
-               .type          = V4L2_CTRL_TYPE_INTEGER,
-       },{
-               .id            = V4L2_CID_AUDIO_MUTE,
-               .name          = "Mute",
-               .minimum       = 0,
-               .maximum       = 1,
-               .step          = 1,
-               .default_value = 1,
-               .flags         = 0,
-               .type          = V4L2_CTRL_TYPE_BOOLEAN,
-       },{
-               .id            = V4L2_CID_AUDIO_BASS,
-               .name          = "Bass",
-               .minimum       = 0,
-               .maximum       = 65535,
-               .step          = 65535/100,
-               .default_value = 32768,
-               .type          = V4L2_CTRL_TYPE_INTEGER,
-       },{
-               .id            = V4L2_CID_AUDIO_TREBLE,
-               .name          = "Treble",
-               .minimum       = 0,
-               .maximum       = 65535,
-               .step          = 65535/100,
-               .default_value = 32768,
-               .type          = V4L2_CTRL_TYPE_INTEGER,
-       },
-};
-
-
-static void msp_any_set_audmode(struct i2c_client *client, int audmode)
-{
-       struct msp_state *state = i2c_get_clientdata(client);
-
-       switch (state->opmode) {
-       case OPMODE_MANUAL:
-       case OPMODE_AUTODETECT:
-               state->watch_stereo = 0;
-               msp3400c_setstereo(client, audmode);
-               break;
-       case OPMODE_AUTOSELECT:
-               msp34xxg_set_audmode(client, audmode);
-               break;
-       }
-}
+#endif
 
 static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
 {
        struct msp_state *state = i2c_get_clientdata(client);
 
        switch (ctrl->id) {
+       case V4L2_CID_AUDIO_VOLUME:
+               ctrl->value = state->volume;
+               break;
+
        case V4L2_CID_AUDIO_MUTE:
                ctrl->value = state->muted;
                break;
 
        case V4L2_CID_AUDIO_BALANCE:
+               if (!state->has_sound_processing)
+                       return -EINVAL;
                ctrl->value = state->balance;
                break;
 
        case V4L2_CID_AUDIO_BASS:
+               if (!state->has_sound_processing)
+                       return -EINVAL;
                ctrl->value = state->bass;
                break;
 
        case V4L2_CID_AUDIO_TREBLE:
+               if (!state->has_sound_processing)
+                       return -EINVAL;
                ctrl->value = state->treble;
                break;
 
-       case V4L2_CID_AUDIO_VOLUME:
-               ctrl->value = state->volume;
+       case V4L2_CID_AUDIO_LOUDNESS:
+               if (!state->has_sound_processing)
+                       return -EINVAL;
+               ctrl->value = state->loudness;
                break;
 
        default:
@@ -538,6 +434,12 @@ static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
        struct msp_state *state = i2c_get_clientdata(client);
 
        switch (ctrl->id) {
+       case V4L2_CID_AUDIO_VOLUME:
+               state->volume = ctrl->value;
+               if (state->volume == 0)
+                       state->balance = 32768;
+               break;
+
        case V4L2_CID_AUDIO_MUTE:
                if (ctrl->value < 0 || ctrl->value >= 2)
                        return -ERANGE;
@@ -545,21 +447,27 @@ static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
                break;
 
        case V4L2_CID_AUDIO_BASS:
+               if (!state->has_sound_processing)
+                       return -EINVAL;
                state->bass = ctrl->value;
                break;
 
        case V4L2_CID_AUDIO_TREBLE:
+               if (!state->has_sound_processing)
+                       return -EINVAL;
                state->treble = ctrl->value;
                break;
 
-       case V4L2_CID_AUDIO_BALANCE:
-               state->balance = ctrl->value;
+       case V4L2_CID_AUDIO_LOUDNESS:
+               if (!state->has_sound_processing)
+                       return -EINVAL;
+               state->loudness = ctrl->value;
                break;
 
-       case V4L2_CID_AUDIO_VOLUME:
-               state->volume = ctrl->value;
-               if (state->volume == 0)
-                       state->balance = 32768;
+       case V4L2_CID_AUDIO_BALANCE:
+               if (!state->has_sound_processing)
+                       return -EINVAL;
+               state->balance = ctrl->value;
                break;
 
        default:
@@ -572,61 +480,22 @@ static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
 {
        struct msp_state *state = i2c_get_clientdata(client);
-       u16 *sarg = arg;
-       int scart = 0;
 
-       if (debug >= 2)
+       if (msp_debug >= 2)
                v4l_i2c_print_ioctl(client, cmd);
 
        switch (cmd) {
-       case AUDC_SET_INPUT:
-               if (*sarg == state->input)
-                       break;
-               state->input = *sarg;
-               switch (*sarg) {
-               case AUDIO_RADIO:
-                       /* Hauppauge uses IN2 for the radio */
-                       state->mode = MSP_MODE_FM_RADIO;
-                       scart       = SCART_IN2;
-                       break;
-               case AUDIO_EXTERN_1:
-                       /* IN1 is often used for external input ... */
-                       state->mode = MSP_MODE_EXTERN;
-                       scart       = SCART_IN1;
-                       break;
-               case AUDIO_EXTERN_2:
-                       /* ... sometimes it is IN2 through ;) */
-                       state->mode = MSP_MODE_EXTERN;
-                       scart       = SCART_IN2;
-                       break;
-               case AUDIO_TUNER:
-                       state->mode = -1;
-                       break;
-               default:
-                       if (*sarg & AUDIO_MUTE)
-                               msp_set_scart(client, SCART_MUTE, 0);
-                       break;
-               }
-               if (scart) {
-                       state->rxsubchans = V4L2_TUNER_SUB_STEREO;
-                       state->audmode = V4L2_TUNER_MODE_STEREO;
-                       msp_set_scart(client, scart, 0);
-                       msp_write_dsp(client, 0x000d, 0x1900);
-                       if (state->opmode != OPMODE_AUTOSELECT)
-                               msp3400c_setstereo(client, state->audmode);
-               }
-               msp_wake_thread(client);
-               break;
-
        case AUDC_SET_RADIO:
-               state->norm = VIDEO_MODE_RADIO;
-               v4l_dbg(1, client, "switching to radio mode\n");
+               if (state->radio)
+                       return 0;
+               state->radio = 1;
+               v4l_dbg(1, msp_debug, client, "switching to radio mode\n");
                state->watch_stereo = 0;
                switch (state->opmode) {
                case OPMODE_MANUAL:
                        /* set msp3400 to FM radio mode */
-                       msp3400c_setmode(client, MSP_MODE_FM_RADIO);
-                       msp3400c_setcarrier(client, MSP_CARRIER(10.7),
+                       msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
+                       msp3400c_set_carrier(client, MSP_CARRIER(10.7),
                                            MSP_CARRIER(10.7));
                        msp_set_audio(client);
                        break;
@@ -641,17 +510,16 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
        /* --- v4l ioctls --- */
        /* take care: bttv does userspace copying, we'll get a
           kernel pointer here... */
+#ifdef CONFIG_VIDEO_V4L1
        case VIDIOCGAUDIO:
        {
                struct video_audio *va = arg;
 
-               va->flags |= VIDEO_AUDIO_VOLUME |
-                       VIDEO_AUDIO_BASS |
-                       VIDEO_AUDIO_TREBLE |
-                       VIDEO_AUDIO_MUTABLE;
-               if (state->muted)
-                       va->flags |= VIDEO_AUDIO_MUTE;
-
+               va->flags |= VIDEO_AUDIO_VOLUME | VIDEO_AUDIO_MUTABLE;
+               if (state->has_sound_processing)
+                       va->flags |= VIDEO_AUDIO_BALANCE |
+                               VIDEO_AUDIO_BASS |
+                               VIDEO_AUDIO_TREBLE;
                if (state->muted)
                        va->flags |= VIDEO_AUDIO_MUTE;
                va->volume = state->volume;
@@ -659,8 +527,11 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                va->bass = state->bass;
                va->treble = state->treble;
 
-               msp_any_detect_stereo(client);
-               va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans);
+               if (state->radio)
+                       break;
+               if (state->opmode == OPMODE_AUTOSELECT)
+                       msp_detect_stereo(client);
+               va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans, state->audmode);
                break;
        }
 
@@ -675,34 +546,49 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                state->treble = va->treble;
                msp_set_audio(client);
 
-               if (va->mode != 0 && state->norm != VIDEO_MODE_RADIO)
-                       msp_any_set_audmode(client, msp_mode_v4l1_to_v4l2(va->mode));
+               if (va->mode != 0 && state->radio == 0 &&
+                   state->audmode != msp_mode_v4l1_to_v4l2(va->mode)) {
+                       state->audmode = msp_mode_v4l1_to_v4l2(va->mode);
+                       msp_set_audmode(client);
+               }
                break;
        }
 
        case VIDIOCSCHAN:
        {
                struct video_channel *vc = arg;
-
-               state->norm = vc->norm;
-               msp_wake_thread(client);
+               int update = 0;
+               v4l2_std_id std;
+
+               if (state->radio)
+                       update = 1;
+               state->radio = 0;
+               if (vc->norm == VIDEO_MODE_PAL)
+                       std = V4L2_STD_PAL;
+               else if (vc->norm == VIDEO_MODE_SECAM)
+                       std = V4L2_STD_SECAM;
+               else
+                       std = V4L2_STD_NTSC;
+               if (std != state->v4l2_std) {
+                       state->v4l2_std = std;
+                       update = 1;
+               }
+               if (update)
+                       msp_wake_thread(client);
                break;
        }
 
        case VIDIOCSFREQ:
-       case VIDIOC_S_FREQUENCY:
        {
                /* new channel -- kick audio carrier scan */
                msp_wake_thread(client);
                break;
        }
-
-       /* msp34xx specific */
-       case MSP_SET_MATRIX:
+#endif
+       case VIDIOC_S_FREQUENCY:
        {
-               struct msp_matrix *mspm = arg;
-
-               msp_set_scart(client, mspm->input, mspm->output);
+               /* new channel -- kick audio carrier scan */
+               msp_wake_thread(client);
                break;
        }
 
@@ -710,114 +596,55 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
        case VIDIOC_S_STD:
        {
                v4l2_std_id *id = arg;
+               int update = state->radio || state->v4l2_std != *id;
 
-               /*FIXME: use V4L2 mode flags on msp3400 instead of V4L1*/
-               if (*id & V4L2_STD_PAL) {
-                       state->norm = VIDEO_MODE_PAL;
-               } else if (*id & V4L2_STD_SECAM) {
-                       state->norm = VIDEO_MODE_SECAM;
-               } else {
-                       state->norm = VIDEO_MODE_NTSC;
-               }
-
-               msp_wake_thread(client);
-               return 0;
-       }
-
-       case VIDIOC_ENUMINPUT:
-       {
-               struct v4l2_input *i = arg;
-
-               if (i->index != 0)
-                       return -EINVAL;
-
-               i->type = V4L2_INPUT_TYPE_TUNER;
-               switch (i->index) {
-               case AUDIO_RADIO:
-                       strcpy(i->name, "Radio");
-                       break;
-               case AUDIO_EXTERN_1:
-                       strcpy(i->name, "Extern 1");
-                       break;
-               case AUDIO_EXTERN_2:
-                       strcpy(i->name, "Extern 2");
-                       break;
-               case AUDIO_TUNER:
-                       strcpy(i->name, "Television");
-                       break;
-               default:
-                       return -EINVAL;
-               }
+               state->v4l2_std = *id;
+               state->radio = 0;
+               if (update)
+                       msp_wake_thread(client);
                return 0;
        }
 
-       case VIDIOC_G_AUDIO:
+       case VIDIOC_INT_G_AUDIO_ROUTING:
        {
-               struct v4l2_audio *a = arg;
-
-               memset(a, 0, sizeof(*a));
-
-               switch (a->index) {
-               case AUDIO_RADIO:
-                       strcpy(a->name, "Radio");
-                       break;
-               case AUDIO_EXTERN_1:
-                       strcpy(a->name, "Extern 1");
-                       break;
-               case AUDIO_EXTERN_2:
-                       strcpy(a->name, "Extern 2");
-                       break;
-               case AUDIO_TUNER:
-                       strcpy(a->name, "Television");
-                       break;
-               default:
-                       return -EINVAL;
-               }
-
-               msp_any_detect_stereo(client);
-               if (state->audmode == V4L2_TUNER_MODE_STEREO) {
-                       a->capability = V4L2_AUDCAP_STEREO;
-               }
+               struct v4l2_routing *rt = arg;
 
+               *rt = state->routing;
                break;
        }
 
-       case VIDIOC_S_AUDIO:
+       case VIDIOC_INT_S_AUDIO_ROUTING:
        {
-               struct v4l2_audio *sarg = arg;
+               struct v4l2_routing *rt = arg;
+               int tuner = (rt->input >> 3) & 1;
+               int sc_in = rt->input & 0x7;
+               int sc1_out = rt->output & 0xf;
+               int sc2_out = (rt->output >> 4) & 0xf;
+               u16 val, reg;
+               int i;
+               int extern_input = 1;
 
-               switch (sarg->index) {
-               case AUDIO_RADIO:
-                       /* Hauppauge uses IN2 for the radio */
-                       state->mode = MSP_MODE_FM_RADIO;
-                       scart       = SCART_IN2;
+               if (state->routing.input == rt->input &&
+                   state->routing.output == rt->output)
                        break;
-               case AUDIO_EXTERN_1:
-                       /* IN1 is often used for external input ... */
-                       state->mode = MSP_MODE_EXTERN;
-                       scart       = SCART_IN1;
-                       break;
-               case AUDIO_EXTERN_2:
-                       /* ... sometimes it is IN2 through ;) */
-                       state->mode = MSP_MODE_EXTERN;
-                       scart       = SCART_IN2;
-                       break;
-               case AUDIO_TUNER:
-                       state->mode = -1;
-                       break;
-               }
-               if (scart) {
-                       state->rxsubchans = V4L2_TUNER_SUB_STEREO;
-                       state->audmode = V4L2_TUNER_MODE_STEREO;
-                       msp_set_scart(client, scart, 0);
-                       msp_write_dsp(client, 0x000d, 0x1900);
+               state->routing = *rt;
+               /* check if the tuner input is used */
+               for (i = 0; i < 5; i++) {
+                       if (((rt->input >> (4 + i * 4)) & 0xf) == 0)
+                               extern_input = 0;
                }
-               if (sarg->capability == V4L2_AUDCAP_STEREO) {
-                       state->audmode = V4L2_TUNER_MODE_STEREO;
-               } else {
-                       state->audmode &= ~V4L2_TUNER_MODE_STEREO;
-               }
-               msp_any_set_audmode(client, state->audmode);
+               if (extern_input)
+                       state->mode = MSP_MODE_EXTERN;
+               else
+                       state->mode = MSP_MODE_AM_DETECT;
+               msp_set_scart(client, sc_in, 0);
+               msp_set_scart(client, sc1_out, 1);
+               msp_set_scart(client, sc2_out, 2);
+               msp_set_audmode(client);
+               reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
+               val = msp_read_dem(client, reg);
+               msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
+               /* wake thread when a new input is chosen */
                msp_wake_thread(client);
                break;
        }
@@ -826,10 +653,13 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
        {
                struct v4l2_tuner *vt = arg;
 
-               msp_any_detect_stereo(client);
+               if (state->radio)
+                       break;
+               if (state->opmode == OPMODE_AUTOSELECT)
+                       msp_detect_stereo(client);
                vt->audmode    = state->audmode;
                vt->rxsubchans = state->rxsubchans;
-               vt->capability = V4L2_TUNER_CAP_STEREO |
+               vt->capability |= V4L2_TUNER_CAP_STEREO |
                        V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
                break;
        }
@@ -838,46 +668,13 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
        {
                struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
 
-               /* only set audmode */
-               if (vt->audmode != -1 && vt->audmode != 0)
-                       msp_any_set_audmode(client, vt->audmode);
-               break;
-       }
-
-       case VIDIOC_G_AUDOUT:
-       {
-               struct v4l2_audioout *a = (struct v4l2_audioout *)arg;
-               int idx = a->index;
-
-               memset(a, 0, sizeof(*a));
-
-               switch (idx) {
-               case 0:
-                       strcpy(a->name, "Scart1 Out");
+               if (state->radio)  /* TODO: add mono/stereo support for radio */
                        break;
-               case 1:
-                       strcpy(a->name, "Scart2 Out");
+               if (state->audmode == vt->audmode)
                        break;
-               case 2:
-                       strcpy(a->name, "I2S Out");
-                       break;
-               default:
-                       return -EINVAL;
-               }
-               break;
-
-       }
-
-       case VIDIOC_S_AUDOUT:
-       {
-               struct v4l2_audioout *a = (struct v4l2_audioout *)arg;
-
-               if (a->index < 0 || a->index > 2)
-                       return -EINVAL;
-
-               v4l_dbg(1, client, "Setting audio out on msp34xx to input %i\n", a->index);
-               msp_set_scart(client, state->in_scart, a->index + 1);
-
+               state->audmode = vt->audmode;
+               /* only set audmode */
+               msp_set_audmode(client);
                break;
        }
 
@@ -885,7 +682,7 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
        {
                u32 *a = (u32 *)arg;
 
-               v4l_dbg(1, client, "Setting I2S speed to %d\n", *a);
+               v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", *a);
 
                switch (*a) {
                        case 1024000:
@@ -903,14 +700,25 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
        case VIDIOC_QUERYCTRL:
        {
                struct v4l2_queryctrl *qc = arg;
-               int i;
 
-               for (i = 0; i < ARRAY_SIZE(msp_qctrl); i++)
-                       if (qc->id && qc->id == msp_qctrl[i].id) {
-                               memcpy(qc, &msp_qctrl[i], sizeof(*qc));
-                               return 0;
-                       }
-               return -EINVAL;
+               switch (qc->id) {
+                       case V4L2_CID_AUDIO_VOLUME:
+                       case V4L2_CID_AUDIO_MUTE:
+                               return v4l2_ctrl_query_fill_std(qc);
+                       default:
+                               break;
+               }
+               if (!state->has_sound_processing)
+                       return -EINVAL;
+               switch (qc->id) {
+                       case V4L2_CID_AUDIO_LOUDNESS:
+                       case V4L2_CID_AUDIO_BALANCE:
+                       case V4L2_CID_AUDIO_BASS:
+                       case V4L2_CID_AUDIO_TREBLE:
+                               return v4l2_ctrl_query_fill_std(qc);
+                       default:
+                               return -EINVAL;
+               }
        }
 
        case VIDIOC_G_CTRL:
@@ -920,22 +728,56 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                return msp_set_ctrl(client, arg);
 
        case VIDIOC_LOG_STATUS:
-               msp_any_detect_stereo(client);
+       {
+               const char *p;
+
+               if (state->opmode == OPMODE_AUTOSELECT)
+                       msp_detect_stereo(client);
                v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
                                client->name, state->rev1, state->rev2);
-               v4l_info(client, "Audio:  volume %d balance %d bass %d treble %d%s\n",
-                               state->volume, state->balance,
-                               state->bass, state->treble,
-                               state->muted ? " (muted)" : "");
-               v4l_info(client, "Mode:   %s (%s%s)\n", msp_standard_mode_name(state->mode),
-                       (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
-                       (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
-               v4l_info(client, "ACB:    0x%04x\n", state->acb);
+               v4l_info(client, "Audio:    volume %d%s\n",
+                               state->volume, state->muted ? " (muted)" : "");
+               if (state->has_sound_processing) {
+                       v4l_info(client, "Audio:    balance %d bass %d treble %d loudness %s\n",
+                                       state->balance, state->bass, state->treble,
+                                       state->loudness ? "on" : "off");
+               }
+               switch (state->mode) {
+               case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
+               case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
+               case MSP_MODE_FM_TERRA: p = "Terrestial FM-mono + FM-stereo"; break;
+               case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
+               case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
+               case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
+               case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
+               case MSP_MODE_BTSC: p = "BTSC"; break;
+               case MSP_MODE_EXTERN: p = "External input"; break;
+               default: p = "unknown"; break;
+               }
+               if (state->mode == MSP_MODE_EXTERN) {
+                       v4l_info(client, "Mode:     %s\n", p);
+               } else if (state->opmode == OPMODE_MANUAL) {
+                       v4l_info(client, "Mode:     %s (%s%s)\n", p,
+                               (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
+                               (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
+               } else {
+                       if (state->opmode == OPMODE_AUTODETECT)
+                               v4l_info(client, "Mode:     %s\n", p);
+                       v4l_info(client, "Standard: %s (%s%s)\n",
+                               msp_standard_std_name(state->std),
+                               (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
+                               (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
+               }
+               v4l_info(client, "Audmode:  0x%04x\n", state->audmode);
+               v4l_info(client, "Routing:  0x%08x (input) 0x%08x (output)\n",
+                               state->routing.input, state->routing.output);
+               v4l_info(client, "ACB:      0x%04x\n", state->acb);
                break;
+       }
 
        default:
-               /* nothing */
-               break;
+               /* unknown */
+               return -EINVAL;
        }
        return 0;
 }
@@ -944,7 +786,7 @@ static int msp_suspend(struct device * dev, pm_message_t state)
 {
        struct i2c_client *client = container_of(dev, struct i2c_client, dev);
 
-       v4l_dbg(1, client, "suspend\n");
+       v4l_dbg(1, msp_debug, client, "suspend\n");
        msp_reset(client);
        return 0;
 }
@@ -953,7 +795,7 @@ static int msp_resume(struct device * dev)
 {
        struct i2c_client *client = container_of(dev, struct i2c_client, dev);
 
-       v4l_dbg(1, client, "resume\n");
+       v4l_dbg(1, msp_debug, client, "resume\n");
        msp_wake_thread(client);
        return 0;
 }
@@ -967,6 +809,11 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
        struct i2c_client *client;
        struct msp_state *state;
        int (*thread_func)(void *data) = NULL;
+       int msp_hard;
+       int msp_family;
+       int msp_revision;
+       int msp_product, msp_prod_hi, msp_prod_lo;
+       int msp_rom;
 
        client = kmalloc(sizeof(*client), GFP_KERNEL);
        if (client == NULL)
@@ -975,11 +822,10 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
        client->addr = address;
        client->adapter = adapter;
        client->driver = &i2c_driver;
-       client->flags = I2C_CLIENT_ALLOW_USE;
        snprintf(client->name, sizeof(client->name) - 1, "msp3400");
 
        if (msp_reset(client) == -1) {
-               v4l_dbg(1, client, "msp3400 not found\n");
+               v4l_dbg(1, msp_debug, client, "msp3400 not found\n");
                kfree(client);
                return -1;
        }
@@ -992,22 +838,27 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
        i2c_set_clientdata(client, state);
 
        memset(state, 0, sizeof(*state));
-       state->norm = VIDEO_MODE_NTSC;
+       state->v4l2_std = V4L2_STD_NTSC;
+       state->audmode = V4L2_TUNER_MODE_STEREO;
        state->volume = 58880;  /* 0db gain */
        state->balance = 32768; /* 0db gain */
        state->bass = 32768;
        state->treble = 32768;
+       state->loudness = 0;
        state->input = -1;
        state->muted = 0;
        state->i2s_mode = 0;
        init_waitqueue_head(&state->wq);
+       /* These are the reset input/output positions */
+       state->routing.input = MSP_INPUT_DEFAULT;
+       state->routing.output = MSP_OUTPUT_DEFAULT;
 
        state->rev1 = msp_read_dsp(client, 0x1e);
        if (state->rev1 != -1)
                state->rev2 = msp_read_dsp(client, 0x1f);
-       v4l_dbg(1, client, "rev1=0x%04x, rev2=0x%04x\n", state->rev1, state->rev2);
+       v4l_dbg(1, msp_debug, client, "rev1=0x%04x, rev2=0x%04x\n", state->rev1, state->rev2);
        if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
-               v4l_dbg(1, client, "not an msp3400 (cannot read chip version)\n");
+               v4l_dbg(1, msp_debug, client, "not an msp3400 (cannot read chip version)\n");
                kfree(state);
                kfree(client);
                return -1;
@@ -1015,20 +866,52 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
 
        msp_set_audio(client);
 
-       snprintf(client->name, sizeof(client->name), "MSP%c4%02d%c-%c%d",
-                ((state->rev1 >> 4) & 0x0f) + '3',
-                (state->rev2 >> 8) & 0xff,
-                (state->rev1 & 0x0f) + '@',
-                ((state->rev1 >> 8) & 0xff) + '@',
-                state->rev2 & 0x1f);
+       msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
+       msp_product = (state->rev2 >> 8) & 0xff;
+       msp_prod_hi = msp_product / 10;
+       msp_prod_lo = msp_product % 10;
+       msp_revision = (state->rev1 & 0x0f) + '@';
+       msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
+       msp_rom = state->rev2 & 0x1f;
+       snprintf(client->name, sizeof(client->name), "MSP%d4%02d%c-%c%d",
+                       msp_family, msp_product,
+                       msp_revision, msp_hard, msp_rom);
+
+       /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
+       state->has_nicam = msp_prod_hi == 1 || msp_prod_hi == 5;
+       /* Has radio support: was added with revision G */
+       state->has_radio = msp_revision >= 'G';
+       /* Has headphones output: not for stripped down products */
+       state->has_headphones = msp_prod_lo < 5;
+       /* Has scart2 input: not in stripped down products of the '3' family */
+       state->has_scart2 = msp_family >= 4 || msp_prod_lo < 7;
+       /* Has scart3 input: not in stripped down products of the '3' family */
+       state->has_scart3 = msp_family >= 4 || msp_prod_lo < 5;
+       /* Has scart4 input: not in pre D revisions, not in stripped D revs */
+       state->has_scart4 = msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
+       /* Has scart2 output: not in stripped down products of the '3' family */
+       state->has_scart2_out = msp_family >= 4 || msp_prod_lo < 5;
+       /* Has scart2 a volume control? Not in pre-D revisions. */
+       state->has_scart2_out_volume = msp_revision > 'C' && state->has_scart2_out;
+       /* Has a configurable i2s out? */
+       state->has_i2s_conf = msp_revision >= 'G' && msp_prod_lo < 7;
+       /* Has subwoofer output: not in pre-D revs and not in stripped down products */
+       state->has_subwoofer = msp_revision >= 'D' && msp_prod_lo < 5;
+       /* Has soundprocessing (bass/treble/balance/loudness/equalizer): not in
+          stripped down products */
+       state->has_sound_processing = msp_prod_lo < 7;
+       /* Has Virtual Dolby Surround: only in msp34x1 */
+       state->has_virtual_dolby_surround = msp_revision == 'G' && msp_prod_lo == 1;
+       /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
+       state->has_dolby_pro_logic = msp_revision == 'G' && msp_prod_lo == 2;
 
        state->opmode = opmode;
        if (state->opmode == OPMODE_AUTO) {
                /* MSP revision G and up have both autodetect and autoselect */
-               if ((state->rev1 & 0x0f) >= 'G'-'@')
+               if (msp_revision >= 'G')
                        state->opmode = OPMODE_AUTOSELECT;
                /* MSP revision D and up have autodetect */
-               else if ((state->rev1 & 0x0f) >= 'D'-'@')
+               else if (msp_revision >= 'D')
                        state->opmode = OPMODE_AUTODETECT;
                else
                        state->opmode = OPMODE_MANUAL;
@@ -1037,11 +920,11 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
        /* hello world :-) */
        v4l_info(client, "%s found @ 0x%x (%s)\n", client->name, address << 1, adapter->name);
        v4l_info(client, "%s ", client->name);
-       if (HAVE_NICAM(state) && HAVE_RADIO(state))
+       if (state->has_nicam && state->has_radio)
                printk("supports nicam and radio, ");
-       else if (HAVE_NICAM(state))
+       else if (state->has_nicam)
                printk("supports nicam, ");
-       else if (HAVE_RADIO(state))
+       else if (state->has_radio)
                printk("supports radio, ");
        printk("mode is ");
 
@@ -1119,7 +1002,6 @@ static struct i2c_driver i2c_driver = {
                .suspend = msp_suspend,
                .resume  = msp_resume,
        },
-       .owner          = THIS_MODULE,
 };
 
 static int __init msp3400_init_module(void)