]> err.no Git - linux-2.6/blobdiff - drivers/media/video/cx25840/cx25840-core.c
V4L/DVB (3249): Generalized cx25840 video/audio input handling
[linux-2.6] / drivers / media / video / cx25840 / cx25840-core.c
index f6afeec499c509b1a692df5cf3d7353215945bb2..a897d6b7d7083f24a1d5609cd307b997dd241a1e 100644 (file)
@@ -115,8 +115,8 @@ int cx25840_and_or(struct i2c_client *client, u16 addr, u8 and_mask,
 
 /* ----------------------------------------------------------------------- */
 
-static int set_input(struct i2c_client *, enum cx25840_input);
-static void input_change(struct i2c_client *);
+static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
+                                               enum cx25840_audio_input aud_input);
 static void log_status(struct i2c_client *client);
 
 /* ----------------------------------------------------------------------- */
@@ -195,10 +195,8 @@ static void cx25840_initialize(struct i2c_client *client, int loadfw)
        /* AC97 shift */
        cx25840_write(client, 0x8cf, 0x0f);
 
-       /* (re)set video input */
-       set_input(client, state->input);
-       /* (re)set audio input */
-       cx25840_audio(client, AUDC_SET_INPUT, &state->audio_input);
+       /* (re)set input */
+       set_input(client, state->vid_input, state->aud_input);
 
        /* start microcontroller */
        cx25840_and_or(client, 0x803, ~0x10, 0x10);
@@ -208,8 +206,11 @@ static void cx25840_initialize(struct i2c_client *client, int loadfw)
 
 static void input_change(struct i2c_client *client)
 {
+       struct cx25840_state *state = i2c_get_clientdata(client);
        v4l2_std_id std = cx25840_get_v4lstd(client);
 
+       /* Note: perhaps V4L2_STD_PAL_M should be handled as V4L2_STD_NTSC
+          instead of V4L2_STD_PAL. Someone needs to test this. */
        if (std & V4L2_STD_PAL) {
                /* Follow tuner change procedure for PAL */
                cx25840_write(client, 0x808, 0xff);
@@ -220,7 +221,32 @@ static void input_change(struct i2c_client *client)
                cx25840_write(client, 0x80b, 0x10);
        } else if (std & V4L2_STD_NTSC) {
                /* NTSC */
-               cx25840_write(client, 0x808, 0xf6);
+               if (state->pvr150_workaround) {
+                       /* Certain Hauppauge PVR150 models have a hardware bug
+                          that causes audio to drop out. For these models the
+                          audio standard must be set explicitly.
+                          To be precise: it affects cards with tuner models
+                          85, 99 and 112 (model numbers from tveeprom). */
+                       if (std == V4L2_STD_NTSC_M_JP) {
+                               /* Japan uses EIAJ audio standard */
+                               cx25840_write(client, 0x808, 0x2f);
+                       } else {
+                               /* Others use the BTSC audio standard */
+                               cx25840_write(client, 0x808, 0x1f);
+                       }
+                       /* South Korea uses the A2-M (aka Zweiton M) audio
+                          standard, and should set 0x808 to 0x3f, but I don't
+                          know how to detect this. */
+               } else if (std == V4L2_STD_NTSC_M_JP) {
+                       /* Japan uses EIAJ audio standard */
+                       cx25840_write(client, 0x808, 0xf7);
+               } else {
+                       /* Others use the BTSC audio standard */
+                       cx25840_write(client, 0x808, 0xf6);
+               }
+               /* South Korea uses the A2-M (aka Zweiton M) audio standard,
+                  and should set 0x808 to 0xf8, but I don't know how to
+                  detect this. */
                cx25840_write(client, 0x80b, 0x00);
        }
 
@@ -231,71 +257,68 @@ static void input_change(struct i2c_client *client)
        }
 }
 
-static int set_input(struct i2c_client *client, enum cx25840_input input)
+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);
+       u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
+                          vid_input <= CX25840_COMPOSITE8);
+       u8 reg;
 
-       cx25840_dbg("decoder set input (%d)\n", input);
-
-       switch (input) {
-       case CX25840_TUNER:
-               cx25840_dbg("now setting Tuner input\n");
-
-               if (state->cardtype == CARDTYPE_PVR150) {
-                       /* CH_SEL_ADC2=1 */
-                       cx25840_and_or(client, 0x102, ~0x2, 0x02);
-               }
-
-               /* Video Input Control */
-               if (state->cardtype == CARDTYPE_PG600) {
-                       cx25840_write(client, 0x103, 0x11);
-               } else {
-                       cx25840_write(client, 0x103, 0x46);
-               }
-
-               /* INPUT_MODE=0 */
-               cx25840_and_or(client, 0x401, ~0x6, 0x00);
-               break;
+       cx25840_dbg("decoder set video input %d, audio input %d\n",
+                       vid_input, aud_input);
 
-       case CX25840_COMPOSITE0:
-       case CX25840_COMPOSITE1:
-               cx25840_dbg("now setting Composite input\n");
+       if (is_composite) {
+               reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
+       } else {
+               int luma = vid_input & 0xf0;
+               int chroma = vid_input & 0xf00;
 
-               /* Video Input Control */
-               if (state->cardtype == CARDTYPE_PG600) {
-                       cx25840_write(client, 0x103, 0x00);
-               } else {
-                       cx25840_write(client, 0x103, 0x02);
+               if ((vid_input & ~0xff0) ||
+                   luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
+                   chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
+                       cx25840_err("0x%04x is not a valid video input!\n", vid_input);
+                       return -EINVAL;
                }
-
-               /* INPUT_MODE=0 */
-               cx25840_and_or(client, 0x401, ~0x6, 0x00);
-               break;
-
-       case CX25840_SVIDEO0:
-       case CX25840_SVIDEO1:
-               cx25840_dbg("now setting S-Video input\n");
-
-               /* CH_SEL_ADC2=0 */
-               cx25840_and_or(client, 0x102, ~0x2, 0x00);
-
-               /* Video Input Control */
-               if (state->cardtype == CARDTYPE_PG600) {
-                       cx25840_write(client, 0x103, 0x02);
+               reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
+               if (chroma >= CX25840_SVIDEO_CHROMA7) {
+                       reg &= 0x3f;
+                       reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
                } else {
-                       cx25840_write(client, 0x103, 0x10);
+                       reg &= 0xcf;
+                       reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
                }
+       }
 
-               /* INPUT_MODE=1 */
-               cx25840_and_or(client, 0x401, ~0x6, 0x02);
+       switch (aud_input) {
+       case CX25840_AUDIO_SERIAL:
+               /* do nothing, use serial audio input */
                break;
+       case CX25840_AUDIO4: reg &= ~0x30; break;
+       case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
+       case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
+       case CX25840_AUDIO7: reg &= ~0xc0; break;
+       case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
 
        default:
-               cx25840_err("%d is not a valid input!\n", input);
+               cx25840_err("0x%04x is not a valid audio input!\n", aud_input);
                return -EINVAL;
        }
 
-       state->input = input;
+       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 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 and CH3 */
+       if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
+               cx25840_and_or(client, 0x102, ~0x4, 4);
+       else
+               cx25840_and_or(client, 0x102, ~0x4, 0);
+
+       state->vid_input = vid_input;
+       state->aud_input = aud_input;
+       cx25840_audio_set_path(client);
        input_change(client);
        return 0;
 }
@@ -304,24 +327,30 @@ static int set_input(struct i2c_client *client, enum cx25840_input input)
 
 static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
 {
-       u8 fmt;
-
-       switch (std) {
-       /* zero is autodetect */
-       case 0: fmt = 0x0; break;
-       /* default ntsc to ntsc-m */
-       case V4L2_STD_NTSC:
-       case V4L2_STD_NTSC_M: fmt = 0x1; break;
-       case V4L2_STD_NTSC_M_JP: fmt = 0x2; break;
-       case V4L2_STD_NTSC_443: fmt = 0x3; break;
-       case V4L2_STD_PAL: fmt = 0x4; break;
-       case V4L2_STD_PAL_M: fmt = 0x5; break;
-       case V4L2_STD_PAL_N: fmt = 0x6; break;
-       case V4L2_STD_PAL_Nc: fmt = 0x7; break;
-       case V4L2_STD_PAL_60: fmt = 0x8; break;
-       case V4L2_STD_SECAM: fmt = 0xc; break;
-       default:
-               return -ERANGE;
+       u8 fmt=0;       /* zero is autodetect */
+
+       /* First tests should be against specific std */
+       if (std & V4L2_STD_NTSC_M_JP) {
+               fmt=0x2;
+       } else if (std & V4L2_STD_NTSC_443) {
+               fmt=0x3;
+       } else if (std & V4L2_STD_PAL_M) {
+               fmt=0x5;
+       } else if (std & V4L2_STD_PAL_N) {
+               fmt=0x6;
+       } else if (std & V4L2_STD_PAL_Nc) {
+               fmt=0x7;
+       } else if (std & V4L2_STD_PAL_60) {
+               fmt=0x8;
+       } else {
+               /* Then, test against generic ones */
+               if (std & V4L2_STD_NTSC) {
+                       fmt=0x1;
+               } else if (std & V4L2_STD_PAL) {
+                       fmt=0x4;
+               } else if (std & V4L2_STD_SECAM) {
+                       fmt=0xc;
+               }
        }
 
        cx25840_and_or(client, 0x400, ~0xf, fmt);
@@ -360,17 +389,9 @@ static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
        struct cx25840_state *state = i2c_get_clientdata(client);
 
        switch (ctrl->id) {
-       case CX25840_CID_CARDTYPE:
-               switch (ctrl->value) {
-               case CARDTYPE_PVR150:
-               case CARDTYPE_PG600:
-                       state->cardtype = ctrl->value;
-                       break;
-               default:
-                       return -ERANGE;
-               }
-
-               set_input(client, state->input);
+       case CX25840_CID_ENABLE_PVR150_WORKAROUND:
+               state->pvr150_workaround = ctrl->value;
+               set_input(client, state->vid_input, state->aud_input);
                break;
 
        case V4L2_CID_BRIGHTNESS:
@@ -429,8 +450,8 @@ static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
        struct cx25840_state *state = i2c_get_clientdata(client);
 
        switch (ctrl->id) {
-       case CX25840_CID_CARDTYPE:
-               ctrl->value = state->cardtype;
+       case CX25840_CID_ENABLE_PVR150_WORKAROUND:
+               ctrl->value = state->pvr150_workaround;
                break;
        case V4L2_CID_BRIGHTNESS:
                ctrl->value = cx25840_read(client, 0x414) + 128;
@@ -579,7 +600,6 @@ static int cx25840_command(struct i2c_client *client, unsigned int cmd,
                return cx25840_vbi(client, cmd, arg);
 
        case VIDIOC_INT_AUDIO_CLOCK_FREQ:
-       case AUDC_SET_INPUT:
                result = cx25840_audio(client, cmd, arg);
                break;
 
@@ -616,13 +636,30 @@ static int cx25840_command(struct i2c_client *client, unsigned int cmd,
                break;
 
        case VIDIOC_G_INPUT:
-               *(int *)arg = state->input;
+               *(int *)arg = state->vid_input;
                break;
 
        case VIDIOC_S_INPUT:
-               result = set_input(client, *(int *)arg);
+               result = set_input(client, *(enum cx25840_video_input *)arg, state->aud_input);
                break;
 
+       case VIDIOC_S_AUDIO:
+       {
+               struct v4l2_audio *input = arg;
+
+               result = set_input(client, state->vid_input, input->index);
+               break;
+       }
+
+       case VIDIOC_G_AUDIO:
+       {
+               struct v4l2_audio *input = arg;
+
+               memset(input, 0, sizeof(*input));
+               input->index = state->aud_input;
+               break;
+       }
+
        case VIDIOC_S_FREQUENCY:
                input_change(client);
                break;
@@ -714,7 +751,7 @@ static int cx25840_command(struct i2c_client *client, unsigned int cmd,
 
 /* ----------------------------------------------------------------------- */
 
-struct i2c_driver i2c_driver_cx25840;
+static struct i2c_driver i2c_driver_cx25840;
 
 static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
                                 int kind)
@@ -737,7 +774,6 @@ static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
        client->addr = address;
        client->adapter = adapter;
        client->driver = &i2c_driver_cx25840;
-       client->flags = I2C_CLIENT_ALLOW_USE;
        snprintf(client->name, sizeof(client->name) - 1, "cx25840");
 
        cx25840_dbg("detecting cx25840 client on address 0x%x\n", address << 1);
@@ -766,10 +802,10 @@ static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
 
        i2c_set_clientdata(client, state);
        memset(state, 0, sizeof(struct cx25840_state));
-       state->input = CX25840_TUNER;
-       state->audclk_freq = V4L2_AUDCLK_48_KHZ;
-       state->audio_input = AUDIO_TUNER;
-       state->cardtype = CARDTYPE_PVR150;
+       state->vid_input = CX25840_COMPOSITE7;
+       state->aud_input = CX25840_AUDIO8;
+       state->audclk_freq = 48000;
+       state->pvr150_workaround = 0;
 
        cx25840_initialize(client, 1);
 
@@ -780,11 +816,7 @@ static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
 
 static int cx25840_attach_adapter(struct i2c_adapter *adapter)
 {
-#ifdef I2C_CLASS_TV_ANALOG
        if (adapter->class & I2C_CLASS_TV_ANALOG)
-#else
-       if (adapter->id == I2C_HW_B_BT848)
-#endif
                return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
        return 0;
 }
@@ -807,16 +839,16 @@ static int cx25840_detach_client(struct i2c_client *client)
 
 /* ----------------------------------------------------------------------- */
 
-struct i2c_driver i2c_driver_cx25840 = {
-       .name = "cx25840",
+static struct i2c_driver i2c_driver_cx25840 = {
+       .driver = {
+               .name = "cx25840",
+       },
 
        .id = I2C_DRIVERID_CX25840,
-       .flags = I2C_DF_NOTIFY,
 
        .attach_adapter = cx25840_attach_adapter,
        .detach_client = cx25840_detach_client,
        .command = cx25840_command,
-       .owner = THIS_MODULE,
 };
 
 
@@ -857,6 +889,8 @@ static void log_status(struct i2c_client *client)
        u8 pref_mode = cx25840_read(client, 0x809);
        u8 afc0 = cx25840_read(client, 0x80b);
        u8 mute_ctl = cx25840_read(client, 0x8d3);
+       int vid_input = state->vid_input;
+       int aud_input = state->aud_input;
        char *p;
 
        cx25840_info("Video signal:              %spresent\n",
@@ -966,24 +1000,21 @@ static void log_status(struct i2c_client *client)
        cx25840_info("Specified standard:        %s\n",
                    vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
 
-       switch (state->input) {
-       case CX25840_COMPOSITE0: p = "Composite 0"; break;
-       case CX25840_COMPOSITE1: p = "Composite 1"; break;
-       case CX25840_SVIDEO0: p = "S-Video 0"; break;
-       case CX25840_SVIDEO1: p = "S-Video 1"; break;
-       case CX25840_TUNER: p = "Tuner"; break;
+       if (vid_input >= CX25840_COMPOSITE1 &&
+           vid_input <= CX25840_COMPOSITE8) {
+               cx25840_info("Specified video input:     Composite %d\n",
+                       vid_input - CX25840_COMPOSITE1 + 1);
+       } else {
+               cx25840_info("Specified video input:     S-Video (Luma In%d, Chroma In%d)\n",
+                       (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
        }
-       cx25840_info("Specified input:           %s\n", p);
-       cx25840_info("Specified audio input:     %s\n",
-                   state->audio_input == 0 ? "Tuner" : "External");
-
-       switch (state->audclk_freq) {
-       case V4L2_AUDCLK_441_KHZ: p = "44.1 kHz"; break;
-       case V4L2_AUDCLK_48_KHZ: p = "48 kHz"; break;
-       case V4L2_AUDCLK_32_KHZ: p = "32 kHz"; break;
-       default: p = "undefined";
+       if (aud_input) {
+               cx25840_info("Specified audio input:     Tuner (In%d)\n", aud_input);
+       } else {
+               cx25840_info("Specified audio input:     External\n");
        }
-       cx25840_info("Specified audioclock freq: %s\n", p);
+
+       cx25840_info("Specified audioclock freq: %d Hz\n", state->audclk_freq);
 
        switch (pref_mode & 0xf) {
        case 0: p = "mono/language A"; break;