i2c: Add support for device alias names
[safe/jmp/linux-2.6] / drivers / media / video / msp3400-driver.c
index c40e8ba..e627316 100644 (file)
@@ -42,7 +42,8 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
  */
 
 
 #include <linux/videodev.h>
 #include <linux/videodev2.h>
 #include <media/v4l2-common.h>
+#include <media/v4l2-i2c-drv-legacy.h>
 #include <media/tvaudio.h>
 #include <media/msp3400.h>
 #include <linux/kthread.h>
-#include <linux/suspend.h>
+#include <linux/freezer.h>
 #include "msp3400-driver.h"
 
 /* ---------------------------------------------------------------------- */
@@ -71,7 +73,8 @@ 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_standard = 1;    /* Override auto detect of audio msp_standard,
+                           if needed. */
 int msp_dolby;
 
 int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
@@ -81,12 +84,12 @@ int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
 module_param(opmode,           int, 0444);
 
 /* read-write */
-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_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");
@@ -157,16 +160,16 @@ static int msp_read(struct i2c_client *client, int dev, int addr)
                        break;
                v4l_warn(client, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
                       dev, addr);
-               current->state = TASK_INTERRUPTIBLE;
-               schedule_timeout(msecs_to_jiffies(10));
+               schedule_timeout_interruptible(msecs_to_jiffies(10));
        }
        if (err == 3) {
-               v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n");
+               v4l_warn(client, "resetting chip, sound will go off.\n");
                msp_reset(client);
                return -1;
        }
        retval = read[0] << 8 | read[1];
-       v4l_dbg(3, msp_debug, 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;
 }
 
@@ -191,17 +194,17 @@ 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, msp_debug, 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;
                v4l_warn(client, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
                       dev, addr);
-               current->state = TASK_INTERRUPTIBLE;
-               schedule_timeout(msecs_to_jiffies(10));
+               schedule_timeout_interruptible(msecs_to_jiffies(10));
        }
        if (err == 3) {
-               v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n");
+               v4l_warn(client, "resetting chip, sound will go off.\n");
                msp_reset(client);
                return -1;
        }
@@ -246,17 +249,17 @@ int msp_write_dsp(struct i2c_client *client, int addr, int val)
  * ----------------------------------------------------------------------- */
 
 static int scarts[3][9] = {
-       /* MASK   IN1     IN2     IN3     IN4     IN1_DA  IN2_DA  MONO    MUTE   */
+       /* MASK   IN1     IN2     IN3     IN4     IN1_DA  IN2_DA  MONO    MUTE   */
        /* SCART DSP Input select */
-       { 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1,     -1,     0x0100, 0x0320 },
+       { 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1,     -1,     0x0100, 0x0320 },
        /* SCART1 Output select */
-       { 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
+       { 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
        /* SCART2 Output select */
-       { 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
+       { 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
 };
 
 static char *scart_names[] = {
-       "in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
+       "in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
 };
 
 void msp_set_scart(struct i2c_client *client, int in, int out)
@@ -275,24 +278,12 @@ void msp_set_scart(struct i2c_client *client, int in, int out)
                state->acb = 0xf60; /* Mute Input and SCART 1 Output */
 
        v4l_dbg(1, msp_debug, client, "scart switch: %s => %d (ACB=0x%04x)\n",
-                                               scart_names[in], out, state->acb);
+                                       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)
-{
-       struct msp_state *state = i2c_get_clientdata(client);
-
-       v4l_dbg(1, msp_debug, client, "mute audio\n");
-       msp_write_dsp(client, 0x0000, 0);
-       msp_write_dsp(client, 0x0007, 1);
-       if (state->has_scart2_out_volume)
-               msp_write_dsp(client, 0x0040, 1);
-       if (state->has_headphones)
-               msp_write_dsp(client, 0x0006, 0);
+       if (state->has_i2s_conf)
+               msp_write_dem(client, 0x40, state->i2s_mode);
 }
 
 void msp_set_audio(struct i2c_client *client)
@@ -300,17 +291,20 @@ void msp_set_audio(struct i2c_client *client)
        struct msp_state *state = i2c_get_clientdata(client);
        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 volume=%d\n",
-               state->muted ? "on" : "off", state->volume);
+       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, state->muted ? 0x1 : (val | 0x1));
+       msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
        if (state->has_scart2_out_volume)
-               msp_write_dsp(client, 0x0040, state->muted ? 0x1 : (val | 0x1));
+               msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
        if (state->has_headphones)
                msp_write_dsp(client, 0x0006, val);
        if (!state->has_sound_processing)
@@ -346,7 +340,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);
@@ -373,20 +366,16 @@ int msp_sleep(struct msp_state *state, int timeout)
 }
 
 /* ------------------------------------------------------------------------ */
-
-static int msp_mode_v4l2_to_v4l1(int rxsubchans)
+#ifdef CONFIG_VIDEO_ALLOW_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 | VIDEO_SOUND_STEREO;
-       if (rxsubchans & V4L2_TUNER_SUB_LANG1)
-               mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_STEREO;
-       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)
@@ -399,67 +388,7 @@ static int msp_mode_v4l1_to_v4l2(int mode)
                return V4L2_TUNER_MODE_LANG1;
        return V4L2_TUNER_MODE_MONO;
 }
-
-static struct v4l2_queryctrl msp_qctrl_std[] = {
-       {
-               .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_MUTE,
-               .name          = "Mute",
-               .minimum       = 0,
-               .maximum       = 1,
-               .step          = 1,
-               .default_value = 1,
-               .flags         = 0,
-               .type          = V4L2_CTRL_TYPE_BOOLEAN,
-       },
-};
-
-static struct v4l2_queryctrl msp_qctrl_sound_processing[] = {
-       {
-               .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_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,
-       },{
-               .id            = V4L2_CID_AUDIO_LOUDNESS,
-               .name          = "Loudness",
-               .minimum       = 0,
-               .maximum       = 1,
-               .step          = 1,
-               .default_value = 1,
-               .flags         = 0,
-               .type          = V4L2_CTRL_TYPE_BOOLEAN,
-       },
-};
-
+#endif
 
 static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
 {
@@ -585,6 +514,7 @@ 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_ALLOW_V4L1
        case VIDIOCGAUDIO:
        {
                struct video_audio *va = arg;
@@ -605,7 +535,7 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                        break;
                if (state->opmode == OPMODE_AUTOSELECT)
                        msp_detect_stereo(client);
-               va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans);
+               va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans, state->audmode);
                break;
        }
 
@@ -620,7 +550,8 @@ 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->radio == 0) {
+               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);
                }
@@ -652,6 +583,12 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
        }
 
        case VIDIOCSFREQ:
+       {
+               /* new channel -- kick audio carrier scan */
+               msp_wake_thread(client);
+               break;
+       }
+#endif
        case VIDIOC_S_FREQUENCY:
        {
                /* new channel -- kick audio carrier scan */
@@ -687,20 +624,29 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                int sc_in = rt->input & 0x7;
                int sc1_out = rt->output & 0xf;
                int sc2_out = (rt->output >> 4) & 0xf;
-               u16 val;
+               u16 val, reg;
+               int i;
+               int extern_input = 1;
 
+               if (state->routing.input == rt->input &&
+                   state->routing.output == rt->output)
+                       break;
                state->routing = *rt;
-               if (state->opmode == OPMODE_AUTOSELECT) {
-                       val = msp_read_dem(client, 0x30) & ~0x100;
-                       msp_write_dem(client, 0x30, val | (tuner ? 0x100 : 0));
-               } else {
-                       val = msp_read_dem(client, 0xbb) & ~0x100;
-                       msp_write_dem(client, 0xbb, val | (tuner ? 0x100 : 0));
+               /* check if the tuner input is used */
+               for (i = 0; i < 5; i++) {
+                       if (((rt->input >> (4 + i * 4)) & 0xf) == 0)
+                               extern_input = 0;
                }
+               state->mode = extern_input ? MSP_MODE_EXTERN : MSP_MODE_AM_DETECT;
+               state->rxsubchans = V4L2_TUNER_SUB_STEREO;
                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;
        }
@@ -715,7 +661,7 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                        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;
        }
@@ -726,6 +672,8 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
 
                if (state->radio)  /* TODO: add mono/stereo support for radio */
                        break;
+               if (state->audmode == vt->audmode)
+                       break;
                state->audmode = vt->audmode;
                /* only set audmode */
                msp_set_audmode(client);
@@ -739,14 +687,14 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", *a);
 
                switch (*a) {
-                       case 1024000:
-                               state->i2s_mode = 0;
-                               break;
-                       case 2048000:
-                               state->i2s_mode = 1;
-                               break;
-                       default:
-                               return -EINVAL;
+               case 1024000:
+                       state->i2s_mode = 0;
+                       break;
+               case 2048000:
+                       state->i2s_mode = 1;
+                       break;
+               default:
+                       return -EINVAL;
                }
                break;
        }
@@ -754,21 +702,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_std); i++)
-                       if (qc->id && qc->id == msp_qctrl_std[i].id) {
-                               memcpy(qc, &msp_qctrl_std[i], sizeof(*qc));
-                               return 0;
-                       }
+               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;
-               for (i = 0; i < ARRAY_SIZE(msp_qctrl_sound_processing); i++)
-                       if (qc->id && qc->id == msp_qctrl_sound_processing[i].id) {
-                               memcpy(qc, &msp_qctrl_sound_processing[i], sizeof(*qc));
-                               return 0;
-                       }
-               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:
@@ -789,13 +741,14 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                                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->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_TERRA: p = "Terrestial FM-mono/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;
@@ -804,7 +757,9 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                case MSP_MODE_EXTERN: p = "External input"; break;
                default: p = "unknown"; break;
                }
-               if (state->opmode == OPMODE_MANUAL) {
+               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" : "");
@@ -823,6 +778,10 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                break;
        }
 
+       case VIDIOC_G_CHIP_IDENT:
+               return v4l2_chip_ident_i2c_client(client, arg, state->ident,
+                               (state->rev1 << 16) | state->rev2);
+
        default:
                /* unknown */
                return -EINVAL;
@@ -830,19 +789,15 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
        return 0;
 }
 
-static int msp_suspend(struct device * dev, pm_message_t state)
+static int msp_suspend(struct i2c_client *client, pm_message_t state)
 {
-       struct i2c_client *client = container_of(dev, struct i2c_client, dev);
-
        v4l_dbg(1, msp_debug, client, "suspend\n");
        msp_reset(client);
        return 0;
 }
 
-static int msp_resume(struct device * dev)
+static int msp_resume(struct i2c_client *client)
 {
-       struct i2c_client *client = container_of(dev, struct i2c_client, dev);
-
        v4l_dbg(1, msp_debug, client, "resume\n");
        msp_wake_thread(client);
        return 0;
@@ -850,11 +805,8 @@ static int msp_resume(struct device * dev)
 
 /* ----------------------------------------------------------------------- */
 
-static struct i2c_driver i2c_driver;
-
-static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
+static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
 {
-       struct i2c_client *client;
        struct msp_state *state;
        int (*thread_func)(void *data) = NULL;
        int msp_hard;
@@ -863,31 +815,21 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
        int msp_product, msp_prod_hi, msp_prod_lo;
        int msp_rom;
 
-       client = kmalloc(sizeof(*client), GFP_KERNEL);
-       if (client == NULL)
-               return -ENOMEM;
-       memset(client, 0, sizeof(*client));
-       client->addr = address;
-       client->adapter = adapter;
-       client->driver = &i2c_driver;
        snprintf(client->name, sizeof(client->name) - 1, "msp3400");
 
        if (msp_reset(client) == -1) {
                v4l_dbg(1, msp_debug, client, "msp3400 not found\n");
-               kfree(client);
-               return -1;
+               return -ENODEV;
        }
 
-       state = kmalloc(sizeof(*state), GFP_KERNEL);
-       if (state == NULL) {
-               kfree(client);
+       state = kzalloc(sizeof(*state), GFP_KERNEL);
+       if (!state)
                return -ENOMEM;
-       }
+
        i2c_set_clientdata(client, state);
 
-       memset(state, 0, sizeof(*state));
        state->v4l2_std = V4L2_STD_NTSC;
-       state->audmode = V4L2_TUNER_MODE_LANG1;
+       state->audmode = V4L2_TUNER_MODE_STEREO;
        state->volume = 58880;  /* 0db gain */
        state->balance = 32768; /* 0db gain */
        state->bass = 32768;
@@ -904,12 +846,13 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
        state->rev1 = msp_read_dsp(client, 0x1e);
        if (state->rev1 != -1)
                state->rev2 = msp_read_dsp(client, 0x1f);
-       v4l_dbg(1, msp_debug, 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, msp_debug, 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;
+               return -ENODEV;
        }
 
        msp_set_audio(client);
@@ -924,31 +867,56 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
        snprintf(client->name, sizeof(client->name), "MSP%d4%02d%c-%c%d",
                        msp_family, msp_product,
                        msp_revision, msp_hard, msp_rom);
+       /* Rev B=2, C=3, D=4, G=7 */
+       state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
+                       msp_revision - '@';
 
        /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
-       state->has_nicam = msp_prod_hi == 1 || msp_prod_hi == 5;
+       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';
+       state->has_radio =
+               msp_revision >= 'G';
        /* Has headphones output: not for stripped down products */
-       state->has_headphones = msp_prod_lo < 5;
+       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 and scart3 inputs and scart2 output: not in stripped
-          down products of the '3' family */
-       state->has_scart23_in_scart2_out = msp_family >= 4 || msp_prod_lo < 5;
+       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_scart23_in_scart2_out;
+       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;
+       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;
+       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->has_dolby_pro_logic =
+               msp_revision == 'G' && msp_prod_lo == 2;
+       /* The msp343xG supports BTSC only and cannot do Automatic Standard
+        * Detection. */
+       state->force_btsc =
+               msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
 
        state->opmode = opmode;
        if (state->opmode == OPMODE_AUTO) {
@@ -963,59 +931,48 @@ 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 found @ 0x%x (%s)\n", client->name,
+                       client->addr << 1, client->adapter->name);
        v4l_info(client, "%s ", client->name);
        if (state->has_nicam && state->has_radio)
-               printk("supports nicam and radio, ");
+               printk(KERN_CONT "supports nicam and radio, ");
        else if (state->has_nicam)
-               printk("supports nicam, ");
+               printk(KERN_CONT "supports nicam, ");
        else if (state->has_radio)
-               printk("supports radio, ");
-       printk("mode is ");
+               printk(KERN_CONT "supports radio, ");
+       printk(KERN_CONT "mode is ");
 
        /* version-specific initialization */
        switch (state->opmode) {
        case OPMODE_MANUAL:
-               printk("manual");
+               printk(KERN_CONT "manual");
                thread_func = msp3400c_thread;
                break;
        case OPMODE_AUTODETECT:
-               printk("autodetect");
+               printk(KERN_CONT "autodetect");
                thread_func = msp3410d_thread;
                break;
        case OPMODE_AUTOSELECT:
-               printk("autodetect and autoselect");
+               printk(KERN_CONT "autodetect and autoselect");
                thread_func = msp34xxg_thread;
                break;
        }
-       printk("\n");
+       printk(KERN_CONT "\n");
 
        /* startup control thread if needed */
        if (thread_func) {
                state->kthread = kthread_run(thread_func, client, "msp34xx");
 
-               if (state->kthread == NULL)
+               if (IS_ERR(state->kthread))
                        v4l_warn(client, "kernel_thread() failed\n");
                msp_wake_thread(client);
        }
-
-       /* done */
-       i2c_attach_client(client);
-
        return 0;
 }
 
-static int msp_probe(struct i2c_adapter *adapter)
-{
-       if (adapter->class & I2C_CLASS_TV_ANALOG)
-               return i2c_probe(adapter, &addr_data, msp_attach);
-       return 0;
-}
-
-static int msp_detach(struct i2c_client *client)
+static int msp_remove(struct i2c_client *client)
 {
        struct msp_state *state = i2c_get_clientdata(client);
-       int err;
 
        /* shutdown control thread */
        if (state->kthread) {
@@ -1024,43 +981,22 @@ static int msp_detach(struct i2c_client *client)
        }
        msp_reset(client);
 
-       err = i2c_detach_client(client);
-       if (err) {
-               return err;
-       }
-
        kfree(state);
-       kfree(client);
        return 0;
 }
 
 /* ----------------------------------------------------------------------- */
 
-/* i2c implementation */
-static struct i2c_driver i2c_driver = {
-       .id             = I2C_DRIVERID_MSP3400,
-       .attach_adapter = msp_probe,
-       .detach_client  = msp_detach,
-       .command        = msp_command,
-       .driver = {
-               .name    = "msp3400",
-               .suspend = msp_suspend,
-               .resume  = msp_resume,
-       },
+static struct v4l2_i2c_driver_data v4l2_i2c_data = {
+       .name = "msp3400",
+       .driverid = I2C_DRIVERID_MSP3400,
+       .command = msp_command,
+       .probe = msp_probe,
+       .remove = msp_remove,
+       .suspend = msp_suspend,
+       .resume = msp_resume,
 };
 
-static int __init msp3400_init_module(void)
-{
-       return i2c_add_driver(&i2c_driver);
-}
-
-static void __exit msp3400_cleanup_module(void)
-{
-       i2c_del_driver(&i2c_driver);
-}
-
-module_init(msp3400_init_module);
-module_exit(msp3400_cleanup_module);
 
 /*
  * Overrides for Emacs so that we follow Linus's tabbing style.