V4L/DVB: gspca - main: Change version to 2.9.0.
[safe/jmp/linux-2.6] / drivers / media / video / cx25840 / cx25840-core.c
index e7bf4f4..796f12d 100644 (file)
@@ -13,7 +13,7 @@
  * NTSC sliced VBI support by Christopher Neufeld <television@cneufeld.ca>
  * with additional fixes by Hans Verkuil <hverkuil@xs4all.nl>.
  *
- * CX23885 support by Steven Toth <stoth@hauppauge.com>.
+ * CX23885 support by Steven Toth <stoth@linuxtv.org>.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -39,7 +39,7 @@
 #include <linux/delay.h>
 #include <media/v4l2-common.h>
 #include <media/v4l2-chip-ident.h>
-#include <media/v4l2-i2c-drv-legacy.h>
+#include <media/v4l2-i2c-drv.h>
 #include <media/cx25840.h>
 
 #include "cx25840-core.h"
@@ -48,15 +48,12 @@ MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
 MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
 MODULE_LICENSE("GPL");
 
-static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
-
-int cx25840_debug;
+static int cx25840_debug;
 
 module_param_named(debug,cx25840_debug, int, 0644);
 
 MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
 
-I2C_CLIENT_INSMOD;
 
 /* ----------------------------------------------------------------------- */
 
@@ -191,7 +188,7 @@ static void cx25840_work_handler(struct work_struct *work)
 static void cx25840_initialize(struct i2c_client *client)
 {
        DEFINE_WAIT(wait);
-       struct cx25840_state *state = i2c_get_clientdata(client);
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
        struct workqueue_struct *q;
 
        /* datasheet startup in numbered steps, refer to page 3-77 */
@@ -259,9 +256,16 @@ static void cx25840_initialize(struct i2c_client *client)
 static void cx23885_initialize(struct i2c_client *client)
 {
        DEFINE_WAIT(wait);
-       struct cx25840_state *state = i2c_get_clientdata(client);
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
        struct workqueue_struct *q;
 
+       /*
+        * Come out of digital power down
+        * The CX23888, at least, needs this, otherwise registers aside from
+        * 0x0-0x2 can't be read or written.
+        */
+       cx25840_write(client, 0x000, 0);
+
        /* Internal Reset */
        cx25840_and_or(client, 0x102, ~0x01, 0x01);
        cx25840_and_or(client, 0x102, ~0x01, 0x00);
@@ -272,18 +276,45 @@ static void cx23885_initialize(struct i2c_client *client)
        /* DIF in reset? */
        cx25840_write(client, 0x398, 0);
 
-       /* Trust the default xtal, no division */
-       /* This changes for the cx23888 products */
+       /*
+        * Trust the default xtal, no division
+        * '885: 28.636363... MHz
+        * '887: 25.000000 MHz
+        * '888: 50.000000 MHz
+        */
        cx25840_write(client, 0x2, 0x76);
 
-       /* Bring down the regulator for AUX clk */
+       /* Power up all the PLL's and DLL */
        cx25840_write(client, 0x1, 0x40);
 
-       /* Sys PLL frac */
-       cx25840_write4(client, 0x11c, 0x01d1744c);
-
-       /* Sys PLL int */
-       cx25840_write4(client, 0x118, 0x00000416);
+       /* Sys PLL */
+       switch (state->id) {
+       case V4L2_IDENT_CX23888_AV:
+               /*
+                * 50.0 MHz * (0xb + 0xe8ba26/0x2000000)/4 = 5 * 28.636363 MHz
+                * 572.73 MHz before post divide
+                */
+               cx25840_write4(client, 0x11c, 0x00e8ba26);
+               cx25840_write4(client, 0x118, 0x0000040b);
+               break;
+       case V4L2_IDENT_CX23887_AV:
+               /*
+                * 25.0 MHz * (0x16 + 0x1d1744c/0x2000000)/4 = 5 * 28.636363 MHz
+                * 572.73 MHz before post divide
+                */
+               cx25840_write4(client, 0x11c, 0x01d1744c);
+               cx25840_write4(client, 0x118, 0x00000416);
+               break;
+       case V4L2_IDENT_CX23885_AV:
+       default:
+               /*
+                * 28.636363 MHz * (0x14 + 0x0/0x2000000)/4 = 5 * 28.636363 MHz
+                * 572.73 MHz before post divide
+                */
+               cx25840_write4(client, 0x11c, 0x00000000);
+               cx25840_write4(client, 0x118, 0x00000414);
+               break;
+       }
 
        /* Disable DIF bypass */
        cx25840_write4(client, 0x33c, 0x00000001);
@@ -291,11 +322,15 @@ static void cx23885_initialize(struct i2c_client *client)
        /* DIF Src phase inc */
        cx25840_write4(client, 0x340, 0x0df7df83);
 
-       /* Vid PLL frac */
-       cx25840_write4(client, 0x10c, 0x01b6db7b);
-
-       /* Vid PLL int */
-       cx25840_write4(client, 0x108, 0x00000512);
+       /*
+        * Vid PLL
+        * Setup for a BT.656 pixel clock of 13.5 Mpixels/second
+        *
+        * 28.636363 MHz * (0xf + 0x02be2c9/0x2000000)/4 = 8 * 13.5 MHz
+        * 432.0 MHz before post divide
+        */
+       cx25840_write4(client, 0x10c, 0x002be2c9);
+       cx25840_write4(client, 0x108, 0x0000040f);
 
        /* Luma */
        cx25840_write4(client, 0x414, 0x00107d12);
@@ -303,11 +338,43 @@ static void cx23885_initialize(struct i2c_client *client)
        /* Chroma */
        cx25840_write4(client, 0x420, 0x3d008282);
 
-       /* Aux PLL frac */
-       cx25840_write4(client, 0x114, 0x017dbf48);
-
-       /* Aux PLL int */
-       cx25840_write4(client, 0x110, 0x000a030e);
+       /*
+        * Aux PLL
+        * Initial setup for audio sample clock:
+        * 48 ksps, 16 bits/sample, x160 multiplier = 122.88 MHz
+        * Intial I2S output/master clock(?):
+        * 48 ksps, 16 bits/sample, x16 multiplier = 12.288 MHz
+        */
+       switch (state->id) {
+       case V4L2_IDENT_CX23888_AV:
+               /*
+                * 50.0 MHz * (0x7 + 0x0bedfa4/0x2000000)/3 = 122.88 MHz
+                * 368.64 MHz before post divide
+                * 122.88 MHz / 0xa = 12.288 MHz
+                */
+               cx25840_write4(client, 0x114, 0x00bedfa4);
+               cx25840_write4(client, 0x110, 0x000a0307);
+               break;
+       case V4L2_IDENT_CX23887_AV:
+               /*
+                * 25.0 MHz * (0xe + 0x17dbf48/0x2000000)/3 = 122.88 MHz
+                * 368.64 MHz before post divide
+                * 122.88 MHz / 0xa = 12.288 MHz
+                */
+               cx25840_write4(client, 0x114, 0x017dbf48);
+               cx25840_write4(client, 0x110, 0x000a030e);
+               break;
+       case V4L2_IDENT_CX23885_AV:
+       default:
+               /*
+                * 28.636363 MHz * (0xc + 0x1bf0c9e/0x2000000)/3 = 122.88 MHz
+                * 368.64 MHz before post divide
+                * 122.88 MHz / 0xa = 12.288 MHz
+                */
+               cx25840_write4(client, 0x114, 0x01bf0c9e);
+               cx25840_write4(client, 0x110, 0x000a030c);
+               break;
+       };
 
        /* ADC2 input select */
        cx25840_write(client, 0x102, 0x10);
@@ -324,6 +391,86 @@ static void cx23885_initialize(struct i2c_client *client)
        /* Select AFE clock pad output source */
        cx25840_write(client, 0x144, 0x05);
 
+       /* Drive GPIO2 direction and values for HVR1700
+        * where an onboard mux selects the output of demodulator
+        * vs the 417. Failure to set this results in no DTV.
+        * It's safe to set this across all Hauppauge boards
+        * currently, regardless of the board type.
+        */
+       cx25840_write(client, 0x160, 0x1d);
+       cx25840_write(client, 0x164, 0x00);
+
+       /* Do the firmware load in a work handler to prevent.
+          Otherwise the kernel is blocked waiting for the
+          bit-banging i2c interface to finish uploading the
+          firmware. */
+       INIT_WORK(&state->fw_work, cx25840_work_handler);
+       init_waitqueue_head(&state->fw_wait);
+       q = create_singlethread_workqueue("cx25840_fw");
+       prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
+       queue_work(q, &state->fw_work);
+       schedule();
+       finish_wait(&state->fw_wait, &wait);
+       destroy_workqueue(q);
+
+       cx25840_std_setup(client);
+
+       /* (re)set input */
+       set_input(client, state->vid_input, state->aud_input);
+
+       /* start microcontroller */
+       cx25840_and_or(client, 0x803, ~0x10, 0x10);
+}
+
+/* ----------------------------------------------------------------------- */
+
+static void cx231xx_initialize(struct i2c_client *client)
+{
+       DEFINE_WAIT(wait);
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
+       struct workqueue_struct *q;
+
+       /* Internal Reset */
+       cx25840_and_or(client, 0x102, ~0x01, 0x01);
+       cx25840_and_or(client, 0x102, ~0x01, 0x00);
+
+       /* Stop microcontroller */
+       cx25840_and_or(client, 0x803, ~0x10, 0x00);
+
+       /* DIF in reset? */
+       cx25840_write(client, 0x398, 0);
+
+       /* Trust the default xtal, no division */
+       /* This changes for the cx23888 products */
+       cx25840_write(client, 0x2, 0x76);
+
+       /* Bring down the regulator for AUX clk */
+       cx25840_write(client, 0x1, 0x40);
+
+       /* Disable DIF bypass */
+       cx25840_write4(client, 0x33c, 0x00000001);
+
+       /* DIF Src phase inc */
+       cx25840_write4(client, 0x340, 0x0df7df83);
+
+       /* Luma */
+       cx25840_write4(client, 0x414, 0x00107d12);
+
+       /* Chroma */
+       cx25840_write4(client, 0x420, 0x3d008282);
+
+       /* ADC2 input select */
+       cx25840_write(client, 0x102, 0x10);
+
+       /* VIN1 & VIN5 */
+       cx25840_write(client, 0x103, 0x11);
+
+       /* Enable format auto detect */
+       cx25840_write(client, 0x400, 0);
+       /* Fast subchroma lock */
+       /* White crush, Chroma AGC & Chroma Killer enabled */
+       cx25840_write(client, 0x401, 0xe8);
+
        /* Do the firmware load in a work handler to prevent.
           Otherwise the kernel is blocked waiting for the
           bit-banging i2c interface to finish uploading the
@@ -350,7 +497,7 @@ static void cx23885_initialize(struct i2c_client *client)
 
 void cx25840_std_setup(struct i2c_client *client)
 {
-       struct cx25840_state *state = i2c_get_clientdata(client);
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
        v4l2_std_id std = state->std;
        int hblank, hactive, burst, vblank, vactive, sc;
        int vblank656, src_decimation;
@@ -417,39 +564,41 @@ void cx25840_std_setup(struct i2c_client *client)
        }
 
        /* DEBUG: Displays configured PLL frequency */
-       pll_int = cx25840_read(client, 0x108);
-       pll_frac = cx25840_read4(client, 0x10c) & 0x1ffffff;
-       pll_post = cx25840_read(client, 0x109);
-       v4l_dbg(1, cx25840_debug, client,
-                               "PLL regs = int: %u, frac: %u, post: %u\n",
-                               pll_int, pll_frac, pll_post);
-
-       if (pll_post) {
-               int fin, fsc;
-               int pll = (28636363L * ((((u64)pll_int) << 25L) + pll_frac)) >> 25L;
-
-               pll /= pll_post;
-               v4l_dbg(1, cx25840_debug, client, "PLL = %d.%06d MHz\n",
-                               pll / 1000000, pll % 1000000);
-               v4l_dbg(1, cx25840_debug, client, "PLL/8 = %d.%06d MHz\n",
-                               pll / 8000000, (pll / 8) % 1000000);
-
-               fin = ((u64)src_decimation * pll) >> 12;
-               v4l_dbg(1, cx25840_debug, client,
-                               "ADC Sampling freq = %d.%06d MHz\n",
-                               fin / 1000000, fin % 1000000);
-
-               fsc = (((u64)sc) * pll) >> 24L;
+       if (!is_cx231xx(state)) {
+               pll_int = cx25840_read(client, 0x108);
+               pll_frac = cx25840_read4(client, 0x10c) & 0x1ffffff;
+               pll_post = cx25840_read(client, 0x109);
                v4l_dbg(1, cx25840_debug, client,
-                               "Chroma sub-carrier freq = %d.%06d MHz\n",
-                               fsc / 1000000, fsc % 1000000);
-
-               v4l_dbg(1, cx25840_debug, client, "hblank %i, hactive %i, "
-                       "vblank %i, vactive %i, vblank656 %i, src_dec %i, "
-                       "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x, "
-                       "sc 0x%06x\n",
-                       hblank, hactive, vblank, vactive, vblank656,
-                       src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
+                       "PLL regs = int: %u, frac: %u, post: %u\n",
+                       pll_int, pll_frac, pll_post);
+
+               if (pll_post) {
+                       int fin, fsc;
+                       int pll = (28636363L * ((((u64)pll_int) << 25L) + pll_frac)) >> 25L;
+
+                       pll /= pll_post;
+                       v4l_dbg(1, cx25840_debug, client, "PLL = %d.%06d MHz\n",
+                                       pll / 1000000, pll % 1000000);
+                       v4l_dbg(1, cx25840_debug, client, "PLL/8 = %d.%06d MHz\n",
+                                       pll / 8000000, (pll / 8) % 1000000);
+
+                       fin = ((u64)src_decimation * pll) >> 12;
+                       v4l_dbg(1, cx25840_debug, client,
+                                       "ADC Sampling freq = %d.%06d MHz\n",
+                                       fin / 1000000, fin % 1000000);
+
+                       fsc = (((u64)sc) * pll) >> 24L;
+                       v4l_dbg(1, cx25840_debug, client,
+                                       "Chroma sub-carrier freq = %d.%06d MHz\n",
+                                       fsc / 1000000, fsc % 1000000);
+
+                       v4l_dbg(1, cx25840_debug, client, "hblank %i, hactive %i, "
+                               "vblank %i, vactive %i, vblank656 %i, src_dec %i, "
+                               "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x, "
+                               "sc 0x%06x\n",
+                               hblank, hactive, vblank, vactive, vblank656,
+                               src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
+               }
        }
 
        /* Sets horizontal blanking delay and active lines */
@@ -497,7 +646,7 @@ void cx25840_std_setup(struct i2c_client *client)
 
 static void input_change(struct i2c_client *client)
 {
-       struct cx25840_state *state = i2c_get_clientdata(client);
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
        v4l2_std_id std = state->std;
 
        /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
@@ -536,13 +685,30 @@ static void input_change(struct i2c_client *client)
                }
                cx25840_write(client, 0x80b, 0x00);
        } else if (std & V4L2_STD_PAL) {
-               /* Follow tuner change procedure for PAL */
+               /* Autodetect audio standard and audio system */
                cx25840_write(client, 0x808, 0xff);
-               cx25840_write(client, 0x80b, 0x10);
+               /* Since system PAL-L is pretty much non-existant and
+                  not used by any public broadcast network, force
+                  6.5 MHz carrier to be interpreted as System DK,
+                  this avoids DK audio detection instability */
+              cx25840_write(client, 0x80b, 0x00);
        } else if (std & V4L2_STD_SECAM) {
-               /* Select autodetect for SECAM */
+               /* Autodetect audio standard and audio system */
                cx25840_write(client, 0x808, 0xff);
-               cx25840_write(client, 0x80b, 0x10);
+               /* If only one of SECAM-DK / SECAM-L is required, then force
+                 6.5MHz carrier, else autodetect it */
+               if ((std & V4L2_STD_SECAM_DK) &&
+                   !(std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {
+                       /* 6.5 MHz carrier to be interpreted as System DK */
+                       cx25840_write(client, 0x80b, 0x00);
+              } else if (!(std & V4L2_STD_SECAM_DK) &&
+                         (std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {
+                       /* 6.5 MHz carrier to be interpreted as System L */
+                       cx25840_write(client, 0x80b, 0x08);
+              } else {
+                       /* 6.5 MHz carrier to be autodetected */
+                       cx25840_write(client, 0x80b, 0x10);
+              }
        }
 
        cx25840_and_or(client, 0x810, ~0x01, 0);
@@ -551,9 +717,13 @@ static void input_change(struct i2c_client *client)
 static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
                                                enum cx25840_audio_input aud_input)
 {
-       struct cx25840_state *state = i2c_get_clientdata(client);
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
        u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
                           vid_input <= CX25840_COMPOSITE8);
+       u8 is_component = (vid_input & CX25840_COMPONENT_ON) ==
+                       CX25840_COMPONENT_ON;
+       int luma = vid_input & 0xf0;
+       int chroma = vid_input & 0xf00;
        u8 reg;
 
        v4l_dbg(1, cx25840_debug, client,
@@ -566,18 +736,14 @@ static int set_input(struct i2c_client *client, enum cx25840_video_input vid_inp
                reg = vid_input & 0xff;
                if ((vid_input & CX25840_SVIDEO_ON) == CX25840_SVIDEO_ON)
                        is_composite = 0;
-               else
+               else if ((vid_input & CX25840_COMPONENT_ON) == 0)
                        is_composite = 1;
 
                v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",
                        reg, is_composite);
-       } else
-       if (is_composite) {
+       } else if (is_composite) {
                reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
        } else {
-               int luma = vid_input & 0xf0;
-               int chroma = vid_input & 0xf00;
-
                if ((vid_input & ~0xff0) ||
                    luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA8 ||
                    chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
@@ -599,7 +765,7 @@ static int set_input(struct i2c_client *client, enum cx25840_video_input vid_inp
         * configuration in reg (for the cx23885) so we have no
         * need to attempt to flip bits for earlier av decoders.
         */
-       if (!state->is_cx23885) {
+       if (!is_cx2388x(state) && !is_cx231xx(state)) {
                switch (aud_input) {
                case CX25840_AUDIO_SERIAL:
                        /* do nothing, use serial audio input */
@@ -619,10 +785,13 @@ static int set_input(struct i2c_client *client, enum cx25840_video_input vid_inp
 
        cx25840_write(client, 0x103, reg);
 
-       /* Set INPUT_MODE to Composite (0) or S-Video (1) */
-       cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
+       /* Set INPUT_MODE to Composite, S-Video or Component */
+       if (is_component)
+               cx25840_and_or(client, 0x401, ~0x6, 0x6);
+       else
+               cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
 
-       if (!state->is_cx23885) {
+       if (!is_cx2388x(state) && !is_cx231xx(state)) {
                /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
                cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
                /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */
@@ -631,22 +800,31 @@ static int set_input(struct i2c_client *client, enum cx25840_video_input vid_inp
                else
                        cx25840_and_or(client, 0x102, ~0x4, 0);
        } else {
-               if (is_composite)
+               /* Set DUAL_MODE_ADC2 to 1 if component*/
+               cx25840_and_or(client, 0x102, ~0x4, is_component ? 0x4 : 0x0);
+               if (is_composite) {
                        /* ADC2 input select channel 2 */
                        cx25840_and_or(client, 0x102, ~0x2, 0);
-               else
-                       /* ADC2 input select channel 3 */
-                       cx25840_and_or(client, 0x102, ~0x2, 2);
+               } else if (!is_component) {
+                       /* S-Video */
+                       if (chroma >= CX25840_SVIDEO_CHROMA7) {
+                               /* ADC2 input select channel 3 */
+                               cx25840_and_or(client, 0x102, ~0x2, 2);
+                       } else {
+                               /* ADC2 input select channel 2 */
+                               cx25840_and_or(client, 0x102, ~0x2, 0);
+                       }
+               }
        }
 
        state->vid_input = vid_input;
        state->aud_input = aud_input;
-       if (!state->is_cx25836) {
+       if (!is_cx2583x(state)) {
                cx25840_audio_set_path(client);
                input_change(client);
        }
 
-       if (state->is_cx23885) {
+       if (is_cx2388x(state)) {
                /* Audio channel 1 src : Parallel 1 */
                cx25840_write(client, 0x124, 0x03);
 
@@ -662,6 +840,19 @@ static int set_input(struct i2c_client *client, enum cx25840_video_input vid_inp
                 */
                cx25840_write(client, 0x918, 0xa0);
                cx25840_write(client, 0x919, 0x01);
+       } else if (is_cx231xx(state)) {
+               /* Audio channel 1 src : Parallel 1 */
+               cx25840_write(client, 0x124, 0x03);
+
+               /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
+               cx25840_write(client, 0x914, 0xa0);
+
+               /* I2S_OUT_CTL:
+                * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
+                * I2S_OUT_MASTER_MODE = Master
+                */
+               cx25840_write(client, 0x918, 0xa0);
+               cx25840_write(client, 0x919, 0x01);
        }
 
        return 0;
@@ -671,7 +862,7 @@ static int set_input(struct i2c_client *client, enum cx25840_video_input vid_inp
 
 static int set_v4lstd(struct i2c_client *client)
 {
-       struct cx25840_state *state = i2c_get_clientdata(client);
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
        u8 fmt = 0;     /* zero is autodetect */
        u8 pal_m = 0;
 
@@ -713,16 +904,17 @@ static int set_v4lstd(struct i2c_client *client)
        cx25840_and_or(client, 0x400, ~0xf, fmt);
        cx25840_and_or(client, 0x403, ~0x3, pal_m);
        cx25840_std_setup(client);
-       if (!state->is_cx25836)
+       if (!is_cx2583x(state))
                input_change(client);
        return 0;
 }
 
 /* ----------------------------------------------------------------------- */
 
-static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
+static int cx25840_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
 {
-       struct cx25840_state *state = i2c_get_clientdata(client);
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
        switch (ctrl->id) {
        case CX25840_CID_ENABLE_PVR150_WORKAROUND:
@@ -762,7 +954,7 @@ static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
                break;
 
        case V4L2_CID_HUE:
-               if (ctrl->value < -127 || ctrl->value > 127) {
+               if (ctrl->value < -128 || ctrl->value > 127) {
                        v4l_err(client, "invalid hue setting %d\n", ctrl->value);
                        return -ERANGE;
                }
@@ -775,9 +967,9 @@ static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
        case V4L2_CID_AUDIO_TREBLE:
        case V4L2_CID_AUDIO_BALANCE:
        case V4L2_CID_AUDIO_MUTE:
-               if (state->is_cx25836)
+               if (is_cx2583x(state))
                        return -EINVAL;
-               return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
+               return cx25840_audio_s_ctrl(sd, ctrl);
 
        default:
                return -EINVAL;
@@ -786,9 +978,10 @@ static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
        return 0;
 }
 
-static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
+static int cx25840_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
 {
-       struct cx25840_state *state = i2c_get_clientdata(client);
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
        switch (ctrl->id) {
        case CX25840_CID_ENABLE_PVR150_WORKAROUND:
@@ -811,9 +1004,9 @@ static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
        case V4L2_CID_AUDIO_TREBLE:
        case V4L2_CID_AUDIO_BALANCE:
        case V4L2_CID_AUDIO_MUTE:
-               if (state->is_cx25836)
+               if (is_cx2583x(state))
                        return -EINVAL;
-               return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
+               return cx25840_audio_g_ctrl(sd, ctrl);
        default:
                return -EINVAL;
        }
@@ -823,21 +1016,21 @@ static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
 
 /* ----------------------------------------------------------------------- */
 
-static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
+static int cx25840_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
 {
        switch (fmt->type) {
        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
-               return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
+               return cx25840_vbi_g_fmt(sd, fmt);
        default:
                return -EINVAL;
        }
-
        return 0;
 }
 
-static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
+static int cx25840_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
 {
-       struct cx25840_state *state = i2c_get_clientdata(client);
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
        struct v4l2_pix_format *pix;
        int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
        int is_50Hz = !(state->std & V4L2_STD_525_60);
@@ -889,10 +1082,10 @@ static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
                break;
 
        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
-               return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
+               return cx25840_vbi_s_fmt(sd, fmt);
 
        case V4L2_BUF_TYPE_VBI_CAPTURE:
-               return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
+               return cx25840_vbi_s_fmt(sd, fmt);
 
        default:
                return -EINVAL;
@@ -914,7 +1107,7 @@ static void log_video_status(struct i2c_client *client)
                "0xD", "0xE", "0xF"
        };
 
-       struct cx25840_state *state = i2c_get_clientdata(client);
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
        u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
        u8 gen_stat1 = cx25840_read(client, 0x40d);
        u8 gen_stat2 = cx25840_read(client, 0x40e);
@@ -944,7 +1137,7 @@ static void log_video_status(struct i2c_client *client)
 
 static void log_audio_status(struct i2c_client *client)
 {
-       struct cx25840_state *state = i2c_get_clientdata(client);
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
        u8 download_ctl = cx25840_read(client, 0x803);
        u8 mod_det_stat0 = cx25840_read(client, 0x804);
        u8 mod_det_stat1 = cx25840_read(client, 0x805);
@@ -1097,208 +1290,252 @@ static void log_audio_status(struct i2c_client *client)
 
 /* ----------------------------------------------------------------------- */
 
-static int cx25840_command(struct i2c_client *client, unsigned int cmd,
-                          void *arg)
+/* This load_fw operation must be called to load the driver's firmware.
+   Without this the audio standard detection will fail and you will
+   only get mono.
+
+   Since loading the firmware is often problematic when the driver is
+   compiled into the kernel I recommend postponing calling this function
+   until the first open of the video device. Another reason for
+   postponing it is that loading this firmware takes a long time (seconds)
+   due to the slow i2c bus speed. So it will speed up the boot process if
+   you can avoid loading the fw as long as the video device isn't used.  */
+static int cx25840_load_fw(struct v4l2_subdev *sd)
 {
-       struct cx25840_state *state = i2c_get_clientdata(client);
-       struct v4l2_tuner *vt = arg;
-       struct v4l2_routing *route = arg;
-
-       /* ignore these commands */
-       switch (cmd) {
-               case TUNER_SET_TYPE_ADDR:
-                       return 0;
-       }
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
        if (!state->is_initialized) {
-               v4l_dbg(1, cx25840_debug, client, "cmd %08x triggered fw load\n", cmd);
-               /* initialize on first use */
+               /* initialize and load firmware */
                state->is_initialized = 1;
-               if (state->is_cx25836)
+               if (is_cx2583x(state))
                        cx25836_initialize(client);
-               else if (state->is_cx23885)
+               else if (is_cx2388x(state))
                        cx23885_initialize(client);
+               else if (is_cx231xx(state))
+                       cx231xx_initialize(client);
                else
                        cx25840_initialize(client);
        }
+       return 0;
+}
 
-       switch (cmd) {
 #ifdef CONFIG_VIDEO_ADV_DEBUG
-       /* ioctls to allow direct access to the
-        * cx25840 registers for testing */
-       case VIDIOC_DBG_G_REGISTER:
-       case VIDIOC_DBG_S_REGISTER:
-       {
-               struct v4l2_register *reg = arg;
-
-               if (!v4l2_chip_match_i2c_client(client, reg->match_type, reg->match_chip))
-                       return -EINVAL;
-               if (!capable(CAP_SYS_ADMIN))
-                       return -EPERM;
+static int cx25840_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
+{
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-               if (cmd == VIDIOC_DBG_G_REGISTER)
-                       reg->val = cx25840_read(client, reg->reg & 0x0fff);
-               else
-                       cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
-               break;
-       }
+       if (!v4l2_chip_match_i2c_client(client, &reg->match))
+               return -EINVAL;
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+       reg->size = 1;
+       reg->val = cx25840_read(client, reg->reg & 0x0fff);
+       return 0;
+}
+
+static int cx25840_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
+{
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
+
+       if (!v4l2_chip_match_i2c_client(client, &reg->match))
+               return -EINVAL;
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+       cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
+       return 0;
+}
 #endif
 
-       case VIDIOC_INT_DECODE_VBI_LINE:
-               return cx25840_vbi(client, cmd, arg);
+static int cx25840_s_audio_stream(struct v4l2_subdev *sd, int enable)
+{
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
+       u8 v;
+
+       if (is_cx2583x(state) || is_cx2388x(state) || is_cx231xx(state))
+               return 0;
+
+       v4l_dbg(1, cx25840_debug, client, "%s audio output\n",
+                       enable ? "enable" : "disable");
 
-       case VIDIOC_INT_AUDIO_CLOCK_FREQ:
-               return cx25840_audio(client, cmd, arg);
+       if (enable) {
+               v = cx25840_read(client, 0x115) | 0x80;
+               cx25840_write(client, 0x115, v);
+               v = cx25840_read(client, 0x116) | 0x03;
+               cx25840_write(client, 0x116, v);
+       } else {
+               v = cx25840_read(client, 0x115) & ~(0x80);
+               cx25840_write(client, 0x115, v);
+               v = cx25840_read(client, 0x116) & ~(0x03);
+               cx25840_write(client, 0x116, v);
+       }
+       return 0;
+}
 
-       case VIDIOC_STREAMON:
-               v4l_dbg(1, cx25840_debug, client, "enable output\n");
-               if (state->is_cx23885) {
-                       u8 v = (cx25840_read(client, 0x421) | 0x0b);
+static int cx25840_s_stream(struct v4l2_subdev *sd, int enable)
+{
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
+       u8 v;
+
+       v4l_dbg(1, cx25840_debug, client, "%s video output\n",
+                       enable ? "enable" : "disable");
+       if (enable) {
+               if (is_cx2388x(state) || is_cx231xx(state)) {
+                       v = cx25840_read(client, 0x421) | 0x0b;
                        cx25840_write(client, 0x421, v);
                } else {
-                       cx25840_write(client, 0x115,
-                               state->is_cx25836 ? 0x0c : 0x8c);
-                       cx25840_write(client, 0x116,
-                               state->is_cx25836 ? 0x04 : 0x07);
+                       v = cx25840_read(client, 0x115) | 0x0c;
+                       cx25840_write(client, 0x115, v);
+                       v = cx25840_read(client, 0x116) | 0x04;
+                       cx25840_write(client, 0x116, v);
                }
-               break;
-
-       case VIDIOC_STREAMOFF:
-               v4l_dbg(1, cx25840_debug, client, "disable output\n");
-               if (state->is_cx23885) {
-                       u8 v = cx25840_read(client, 0x421) & ~(0x0b);
+       } else {
+               if (is_cx2388x(state) || is_cx231xx(state)) {
+                       v = cx25840_read(client, 0x421) & ~(0x0b);
                        cx25840_write(client, 0x421, v);
                } else {
-                       cx25840_write(client, 0x115, 0x00);
-                       cx25840_write(client, 0x116, 0x00);
+                       v = cx25840_read(client, 0x115) & ~(0x0c);
+                       cx25840_write(client, 0x115, v);
+                       v = cx25840_read(client, 0x116) & ~(0x04);
+                       cx25840_write(client, 0x116, v);
                }
-               break;
+       }
+       return 0;
+}
 
-       case VIDIOC_LOG_STATUS:
-               log_video_status(client);
-               if (!state->is_cx25836)
-                       log_audio_status(client);
-               break;
+static int cx25840_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
+{
+       struct cx25840_state *state = to_state(sd);
 
-       case VIDIOC_G_CTRL:
-               return get_v4lctrl(client, (struct v4l2_control *)arg);
+       switch (qc->id) {
+       case V4L2_CID_BRIGHTNESS:
+               return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
+       case V4L2_CID_CONTRAST:
+       case V4L2_CID_SATURATION:
+               return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
+       case V4L2_CID_HUE:
+               return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
+       default:
+               break;
+       }
+       if (is_cx2583x(state))
+               return -EINVAL;
 
-       case VIDIOC_S_CTRL:
-               return set_v4lctrl(client, (struct v4l2_control *)arg);
+       switch (qc->id) {
+       case V4L2_CID_AUDIO_VOLUME:
+               return v4l2_ctrl_query_fill(qc, 0, 65535,
+                               65535 / 100, state->default_volume);
+       case V4L2_CID_AUDIO_MUTE:
+               return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
+       case V4L2_CID_AUDIO_BALANCE:
+       case V4L2_CID_AUDIO_BASS:
+       case V4L2_CID_AUDIO_TREBLE:
+               return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
+       default:
+               return -EINVAL;
+       }
+       return -EINVAL;
+}
 
-       case VIDIOC_QUERYCTRL:
-       {
-               struct v4l2_queryctrl *qc = arg;
+static int cx25840_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
+{
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-               switch (qc->id) {
-                       case V4L2_CID_BRIGHTNESS:
-                       case V4L2_CID_CONTRAST:
-                       case V4L2_CID_SATURATION:
-                       case V4L2_CID_HUE:
-                               return v4l2_ctrl_query_fill_std(qc);
-                       default:
-                               break;
-               }
-               if (state->is_cx25836)
-                       return -EINVAL;
+       if (state->radio == 0 && state->std == std)
+               return 0;
+       state->radio = 0;
+       state->std = std;
+       return set_v4lstd(client);
+}
 
-               switch (qc->id) {
-                       case V4L2_CID_AUDIO_VOLUME:
-                               return v4l2_ctrl_query_fill(qc, 0, 65535,
-                                       65535 / 100, state->default_volume);
-                       case V4L2_CID_AUDIO_MUTE:
-                       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;
-               }
-               return -EINVAL;
-       }
+static int cx25840_s_radio(struct v4l2_subdev *sd)
+{
+       struct cx25840_state *state = to_state(sd);
 
-       case VIDIOC_G_STD:
-               *(v4l2_std_id *)arg = state->std;
-               break;
+       state->radio = 1;
+       return 0;
+}
 
-       case VIDIOC_S_STD:
-               if (state->radio == 0 && state->std == *(v4l2_std_id *)arg)
-                       return 0;
-               state->radio = 0;
-               state->std = *(v4l2_std_id *)arg;
-               return set_v4lstd(client);
+static int cx25840_s_video_routing(struct v4l2_subdev *sd,
+                                  u32 input, u32 output, u32 config)
+{
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-       case AUDC_SET_RADIO:
-               state->radio = 1;
-               break;
+       return set_input(client, input, state->aud_input);
+}
 
-       case VIDIOC_INT_G_VIDEO_ROUTING:
-               route->input = state->vid_input;
-               route->output = 0;
-               break;
+static int cx25840_s_audio_routing(struct v4l2_subdev *sd,
+                                  u32 input, u32 output, u32 config)
+{
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-       case VIDIOC_INT_S_VIDEO_ROUTING:
-               return set_input(client, route->input, state->aud_input);
+       if (is_cx2583x(state))
+               return -EINVAL;
+       return set_input(client, state->vid_input, input);
+}
 
-       case VIDIOC_INT_G_AUDIO_ROUTING:
-               if (state->is_cx25836)
-                       return -EINVAL;
-               route->input = state->aud_input;
-               route->output = 0;
-               break;
+static int cx25840_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)
+{
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-       case VIDIOC_INT_S_AUDIO_ROUTING:
-               if (state->is_cx25836)
-                       return -EINVAL;
-               return set_input(client, state->vid_input, route->input);
+       if (!is_cx2583x(state))
+               input_change(client);
+       return 0;
+}
 
-       case VIDIOC_S_FREQUENCY:
-               if (!state->is_cx25836) {
-                       input_change(client);
-               }
-               break;
+static int cx25840_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
+{
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
+       u8 vpres = cx25840_read(client, 0x40e) & 0x20;
+       u8 mode;
+       int val = 0;
 
-       case VIDIOC_G_TUNER:
-       {
-               u8 vpres = cx25840_read(client, 0x40e) & 0x20;
-               u8 mode;
-               int val = 0;
+       if (state->radio)
+               return 0;
 
-               if (state->radio)
-                       break;
+       vt->signal = vpres ? 0xffff : 0x0;
+       if (is_cx2583x(state))
+               return 0;
 
-               vt->signal = vpres ? 0xffff : 0x0;
-               if (state->is_cx25836)
-                       break;
+       vt->capability |=
+               V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
+               V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
 
-               vt->capability |=
-                   V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
-                   V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
+       mode = cx25840_read(client, 0x804);
 
-               mode = cx25840_read(client, 0x804);
+       /* get rxsubchans and audmode */
+       if ((mode & 0xf) == 1)
+               val |= V4L2_TUNER_SUB_STEREO;
+       else
+               val |= V4L2_TUNER_SUB_MONO;
 
-               /* get rxsubchans and audmode */
-               if ((mode & 0xf) == 1)
-                       val |= V4L2_TUNER_SUB_STEREO;
-               else
-                       val |= V4L2_TUNER_SUB_MONO;
+       if (mode == 2 || mode == 4)
+               val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
 
-               if (mode == 2 || mode == 4)
-                       val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
+       if (mode & 0x10)
+               val |= V4L2_TUNER_SUB_SAP;
 
-               if (mode & 0x10)
-                       val |= V4L2_TUNER_SUB_SAP;
+       vt->rxsubchans = val;
+       vt->audmode = state->audmode;
+       return 0;
+}
 
-               vt->rxsubchans = val;
-               vt->audmode = state->audmode;
-               break;
-       }
+static int cx25840_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
+{
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-       case VIDIOC_S_TUNER:
-               if (state->radio || state->is_cx25836)
-                       break;
+       if (state->radio || is_cx2583x(state))
+               return 0;
 
-               switch (vt->audmode) {
+       switch (vt->audmode) {
                case V4L2_TUNER_MODE_MONO:
                        /* mono      -> mono
                           stereo    -> mono
@@ -1326,42 +1563,137 @@ static int cx25840_command(struct i2c_client *client, unsigned int cmd,
                        break;
                default:
                        return -EINVAL;
-               }
-               state->audmode = vt->audmode;
-               break;
-
-       case VIDIOC_G_FMT:
-               return get_v4lfmt(client, (struct v4l2_format *)arg);
+       }
+       state->audmode = vt->audmode;
+       return 0;
+}
 
-       case VIDIOC_S_FMT:
-               return set_v4lfmt(client, (struct v4l2_format *)arg);
+static int cx25840_reset(struct v4l2_subdev *sd, u32 val)
+{
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
+
+       if (is_cx2583x(state))
+               cx25836_initialize(client);
+       else if (is_cx2388x(state))
+               cx23885_initialize(client);
+       else if (is_cx231xx(state))
+               cx231xx_initialize(client);
+       else
+               cx25840_initialize(client);
+       return 0;
+}
 
-       case VIDIOC_INT_RESET:
-               if (state->is_cx25836)
-                       cx25836_initialize(client);
-               else if (state->is_cx23885)
-                       cx23885_initialize(client);
-               else
-                       cx25840_initialize(client);
-               break;
+static int cx25840_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
+{
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-       case VIDIOC_G_CHIP_IDENT:
-               return v4l2_chip_ident_i2c_client(client, arg, state->id, state->rev);
+       return v4l2_chip_ident_i2c_client(client, chip, state->id, state->rev);
+}
 
-       default:
-               return -EINVAL;
-       }
+static int cx25840_log_status(struct v4l2_subdev *sd)
+{
+       struct cx25840_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
+       log_video_status(client);
+       if (!is_cx2583x(state))
+               log_audio_status(client);
        return 0;
 }
 
 /* ----------------------------------------------------------------------- */
 
+static const struct v4l2_subdev_core_ops cx25840_core_ops = {
+       .log_status = cx25840_log_status,
+       .g_chip_ident = cx25840_g_chip_ident,
+       .g_ctrl = cx25840_g_ctrl,
+       .s_ctrl = cx25840_s_ctrl,
+       .queryctrl = cx25840_queryctrl,
+       .s_std = cx25840_s_std,
+       .reset = cx25840_reset,
+       .load_fw = cx25840_load_fw,
+#ifdef CONFIG_VIDEO_ADV_DEBUG
+       .g_register = cx25840_g_register,
+       .s_register = cx25840_s_register,
+#endif
+};
+
+static const struct v4l2_subdev_tuner_ops cx25840_tuner_ops = {
+       .s_frequency = cx25840_s_frequency,
+       .s_radio = cx25840_s_radio,
+       .g_tuner = cx25840_g_tuner,
+       .s_tuner = cx25840_s_tuner,
+};
+
+static const struct v4l2_subdev_audio_ops cx25840_audio_ops = {
+       .s_clock_freq = cx25840_s_clock_freq,
+       .s_routing = cx25840_s_audio_routing,
+       .s_stream = cx25840_s_audio_stream,
+};
+
+static const struct v4l2_subdev_video_ops cx25840_video_ops = {
+       .s_routing = cx25840_s_video_routing,
+       .g_fmt = cx25840_g_fmt,
+       .s_fmt = cx25840_s_fmt,
+       .decode_vbi_line = cx25840_decode_vbi_line,
+       .s_stream = cx25840_s_stream,
+};
+
+static const struct v4l2_subdev_ops cx25840_ops = {
+       .core = &cx25840_core_ops,
+       .tuner = &cx25840_tuner_ops,
+       .audio = &cx25840_audio_ops,
+       .video = &cx25840_video_ops,
+};
+
+/* ----------------------------------------------------------------------- */
+
+static u32 get_cx2388x_ident(struct i2c_client *client)
+{
+       u32 ret;
+
+       /* Come out of digital power down */
+       cx25840_write(client, 0x000, 0);
+
+       /* Detecting whether the part is cx23885/7/8 is more
+        * difficult than it needs to be. No ID register. Instead we
+        * probe certain registers indicated in the datasheets to look
+        * for specific defaults that differ between the silicon designs. */
+
+       /* It's either 885/7 if the IR Tx Clk Divider register exists */
+       if (cx25840_read4(client, 0x204) & 0xffff) {
+               /* CX23885 returns bogus repetitive byte values for the DIF,
+                * which doesn't exist for it. (Ex. 8a8a8a8a or 31313131) */
+               ret = cx25840_read4(client, 0x300);
+               if (((ret & 0xffff0000) >> 16) == (ret & 0xffff)) {
+                       /* No DIF */
+                       ret = V4L2_IDENT_CX23885_AV;
+               } else {
+                       /* CX23887 has a broken DIF, but the registers
+                        * appear valid (but unsed), good enough to detect. */
+                       ret = V4L2_IDENT_CX23887_AV;
+               }
+       } else if (cx25840_read4(client, 0x300) & 0x0fffffff) {
+               /* DIF PLL Freq Word reg exists; chip must be a CX23888 */
+               ret = V4L2_IDENT_CX23888_AV;
+       } else {
+               v4l_err(client, "Unable to detect h/w, assuming cx23887\n");
+               ret = V4L2_IDENT_CX23887_AV;
+       }
+
+       /* Back into digital power down */
+       cx25840_write(client, 0x000, 2);
+       return ret;
+}
+
 static int cx25840_probe(struct i2c_client *client,
                         const struct i2c_device_id *did)
 {
        struct cx25840_state *state;
-       u32 id;
+       struct v4l2_subdev *sd;
+       u32 id = V4L2_IDENT_NONE;
        u16 device_id;
 
        /* Check if the adapter supports the needed features */
@@ -1378,35 +1710,71 @@ static int cx25840_probe(struct i2c_client *client,
         * 0x83 for the cx2583x and 0x84 for the cx2584x */
        if ((device_id & 0xff00) == 0x8300) {
                id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
-       }
-       else if ((device_id & 0xff00) == 0x8400) {
+       } else if ((device_id & 0xff00) == 0x8400) {
                id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
        } else if (device_id == 0x0000) {
-               id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
-       } else if (device_id == 0x1313) {
-               id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
-       }
-       else {
+               id = get_cx2388x_ident(client);
+       } else if ((device_id & 0xfff0) == 0x5A30) {
+               /* The CX23100 (0x5A3C = 23100) doesn't have an A/V decoder */
+               id = V4L2_IDENT_CX2310X_AV;
+       } else if ((device_id & 0xff) == (device_id >> 8)) {
+               v4l_err(client,
+                       "likely a confused/unresponsive cx2388[578] A/V decoder"
+                       " found @ 0x%x (%s)\n",
+                       client->addr << 1, client->adapter->name);
+               v4l_err(client, "A method to reset it from the cx25840 driver"
+                       " software is not known at this time\n");
+               return -ENODEV;
+       } else {
                v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
                return -ENODEV;
        }
 
        state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
-       if (state == NULL) {
+       if (state == NULL)
                return -ENOMEM;
-       }
 
-       /* Note: revision '(device_id & 0x0f) == 2' was never built. The
-          marking skips from 0x1 == 22 to 0x3 == 23. */
-       v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
-                   (device_id & 0xfff0) >> 4,
-                   (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : (device_id & 0x0f),
-                   client->addr << 1, client->adapter->name);
+       sd = &state->sd;
+       v4l2_i2c_subdev_init(sd, client, &cx25840_ops);
+       switch (id) {
+       case V4L2_IDENT_CX23885_AV:
+               v4l_info(client, "cx23885 A/V decoder found @ 0x%x (%s)\n",
+                        client->addr << 1, client->adapter->name);
+               break;
+       case V4L2_IDENT_CX23887_AV:
+               v4l_info(client, "cx23887 A/V decoder found @ 0x%x (%s)\n",
+                        client->addr << 1, client->adapter->name);
+               break;
+       case V4L2_IDENT_CX23888_AV:
+               v4l_info(client, "cx23888 A/V decoder found @ 0x%x (%s)\n",
+                        client->addr << 1, client->adapter->name);
+               break;
+       case V4L2_IDENT_CX2310X_AV:
+               v4l_info(client, "cx%d A/V decoder found @ 0x%x (%s)\n",
+                        device_id, client->addr << 1, client->adapter->name);
+               break;
+       case V4L2_IDENT_CX25840:
+       case V4L2_IDENT_CX25841:
+       case V4L2_IDENT_CX25842:
+       case V4L2_IDENT_CX25843:
+               /* Note: revision '(device_id & 0x0f) == 2' was never built. The
+                  marking skips from 0x1 == 22 to 0x3 == 23. */
+               v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
+                        (device_id & 0xfff0) >> 4,
+                        (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1
+                                               : (device_id & 0x0f),
+                        client->addr << 1, client->adapter->name);
+               break;
+       case V4L2_IDENT_CX25836:
+       case V4L2_IDENT_CX25837:
+       default:
+               v4l_info(client, "cx25%3x-%x found @ 0x%x (%s)\n",
+                        (device_id & 0xfff0) >> 4, device_id & 0x0f,
+                        client->addr << 1, client->adapter->name);
+               break;
+       }
 
-       i2c_set_clientdata(client, state);
        state->c = client;
-       state->is_cx25836 = ((device_id & 0xff00) == 0x8300);
-       state->is_cx23885 = (device_id == 0x0000) || (device_id == 0x1313);
        state->vid_input = CX25840_COMPOSITE7;
        state->aud_input = CX25840_AUDIO8;
        state->audclk_freq = 48000;
@@ -1419,18 +1787,15 @@ static int cx25840_probe(struct i2c_client *client,
        state->id = id;
        state->rev = device_id;
 
-       if (state->is_cx23885) {
-               /* Drive GPIO2 direction and values */
-               cx25840_write(client, 0x160, 0x1d);
-               cx25840_write(client, 0x164, 0x00);
-       }
-
        return 0;
 }
 
 static int cx25840_remove(struct i2c_client *client)
 {
-       kfree(i2c_get_clientdata(client));
+       struct v4l2_subdev *sd = i2c_get_clientdata(client);
+
+       v4l2_device_unregister_subdev(sd);
+       kfree(to_state(sd));
        return 0;
 }
 
@@ -1442,8 +1807,6 @@ MODULE_DEVICE_TABLE(i2c, cx25840_id);
 
 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
        .name = "cx25840",
-       .driverid = I2C_DRIVERID_CX25840,
-       .command = cx25840_command,
        .probe = cx25840_probe,
        .remove = cx25840_remove,
        .id_table = cx25840_id,