[ALSA] hda-codec - Improve the auto-configuration
[safe/jmp/linux-2.6] / sound / pci / cmipci.c
index 51ba704..1fa5f00 100644 (file)
@@ -246,10 +246,9 @@ MODULE_PARM_DESC(joystick_port, "Joystick port address.");
 #define CM_MMODE_MASK          0x00000E00      /* model DAA interface mode */
 #define CM_SPDIF_SELECT2       0x00000100      /* for model > 039 ? */
 #define CM_ENCENTER            0x00000080
-#define CM_FLINKON             0x00000080      /* force modem link detection on, model 037 */
+#define CM_FLINKON             0x00000040      /* force modem link detection on, model 037 */
 #define CM_MUTECH1             0x00000040      /* mute PCI ch1 to DAC */
-#define CM_FLINKOFF            0x00000040      /* force modem link detection off, model 037 */
-#define CM_UNKNOWN_18_5                0x00000020      /* ? */
+#define CM_FLINKOFF            0x00000020      /* force modem link detection off, model 037 */
 #define CM_MIDSMP              0x00000010      /* 1/2 interpolation at front end DAC */
 #define CM_UPDDMA_MASK         0x0000000C      /* TDMA position update notification */
 #define CM_UPDDMA_2048         0x00000000
@@ -431,15 +430,14 @@ MODULE_PARM_DESC(joystick_port, "Joystick port address.");
 
 struct cmipci_pcm {
        struct snd_pcm_substream *substream;
-       int running;            /* dac/adc running? */
+       u8 running;             /* dac/adc running? */
+       u8 fmt;                 /* format bits */
+       u8 is_dac;
+       u8 needs_silencing;
        unsigned int dma_size;  /* in frames */
-       unsigned int period_size;       /* in frames */
+       unsigned int shift;
+       unsigned int ch;        /* channel (0/1) */
        unsigned int offset;    /* physical address of the buffer */
-       unsigned int fmt;       /* format bits */
-       int ch;                 /* channel (0/1) */
-       unsigned int is_dac;            /* is dac? */
-       int bytes_per_frame;
-       int shift;
 };
 
 /* mixer elements toggled/resumed during ac3 playback */
@@ -604,6 +602,9 @@ static unsigned int rates[] = { 5512, 11025, 22050, 44100, 8000, 16000, 32000, 4
 static unsigned int snd_cmipci_rate_freq(unsigned int rate)
 {
        unsigned int i;
+
+       if (rate > 48000)
+               rate /= 2;
        for (i = 0; i < ARRAY_SIZE(rates); i++) {
                if (rates[i] == rate)
                        return i;
@@ -718,19 +719,19 @@ static int snd_cmipci_hw_free(struct snd_pcm_substream *substream)
 /*
  */
 
-static unsigned int hw_channels[] = {1, 2, 4, 5, 6, 8};
+static unsigned int hw_channels[] = {1, 2, 4, 6, 8};
 static struct snd_pcm_hw_constraint_list hw_constraints_channels_4 = {
        .count = 3,
        .list = hw_channels,
        .mask = 0,
 };
 static struct snd_pcm_hw_constraint_list hw_constraints_channels_6 = {
-       .count = 5,
+       .count = 4,
        .list = hw_channels,
        .mask = 0,
 };
 static struct snd_pcm_hw_constraint_list hw_constraints_channels_8 = {
-       .count = 6,
+       .count = 5,
        .list = hw_channels,
        .mask = 0,
 };
@@ -738,48 +739,37 @@ static struct snd_pcm_hw_constraint_list hw_constraints_channels_8 = {
 static int set_dac_channels(struct cmipci *cm, struct cmipci_pcm *rec, int channels)
 {
        if (channels > 2) {
-               if (! cm->can_multi_ch)
+               if (!cm->can_multi_ch || !rec->ch)
                        return -EINVAL;
                if (rec->fmt != 0x03) /* stereo 16bit only */
                        return -EINVAL;
+       }
 
+       if (cm->can_multi_ch) {
                spin_lock_irq(&cm->reg_lock);
-               snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
-               snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
-               if (channels > 4) {
-                       snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
-                       snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
+               if (channels > 2) {
+                       snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
+                       snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
                } else {
-                       snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
-                       snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
+                       snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
+                       snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
                }
-               if (channels >= 6) {
+               if (channels == 8)
+                       snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
+               else
+                       snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
+               if (channels == 6) {
+                       snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
                        snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
-                       snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENCENTER);
                } else {
-                       snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
-                       snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENCENTER);
-               }
-               if (cm->chip_version == 68) {
-                       if (channels == 8) {
-                               snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
-                       } else {
-                               snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
-                       }
-               }
-               spin_unlock_irq(&cm->reg_lock);
-
-       } else {
-               if (cm->can_multi_ch) {
-                       spin_lock_irq(&cm->reg_lock);
-                       snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
-                       snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
                        snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
                        snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
-                       snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENCENTER);
-                       snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
-                       spin_unlock_irq(&cm->reg_lock);
                }
+               if (channels == 4)
+                       snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
+               else
+                       snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
+               spin_unlock_irq(&cm->reg_lock);
        }
        return 0;
 }
@@ -793,6 +783,7 @@ static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
                                 struct snd_pcm_substream *substream)
 {
        unsigned int reg, freq, val;
+       unsigned int period_size;
        struct snd_pcm_runtime *runtime = substream->runtime;
 
        rec->fmt = 0;
@@ -812,11 +803,11 @@ static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
        rec->offset = runtime->dma_addr;
        /* buffer and period sizes in frame */
        rec->dma_size = runtime->buffer_size << rec->shift;
-       rec->period_size = runtime->period_size << rec->shift;
+       period_size = runtime->period_size << rec->shift;
        if (runtime->channels > 2) {
                /* multi-channels */
                rec->dma_size = (rec->dma_size * runtime->channels) / 2;
-               rec->period_size = (rec->period_size * runtime->channels) / 2;
+               period_size = (period_size * runtime->channels) / 2;
        }
 
        spin_lock_irq(&cm->reg_lock);
@@ -827,7 +818,7 @@ static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
        /* program sample counts */
        reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
        snd_cmipci_write_w(cm, reg, rec->dma_size - 1);
-       snd_cmipci_write_w(cm, reg + 2, rec->period_size - 1);
+       snd_cmipci_write_w(cm, reg + 2, period_size - 1);
 
        /* set adc/dac flag */
        val = rec->ch ? CM_CHADC1 : CM_CHADC0;
@@ -883,7 +874,7 @@ static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
  * PCM trigger/stop
  */
 static int snd_cmipci_pcm_trigger(struct cmipci *cm, struct cmipci_pcm *rec,
-                                 struct snd_pcm_substream *substream, int cmd)
+                                 int cmd)
 {
        unsigned int inthld, chen, reset, pause;
        int result = 0;
@@ -912,6 +903,7 @@ static int snd_cmipci_pcm_trigger(struct cmipci *cm, struct cmipci_pcm *rec,
                cm->ctrl &= ~chen;
                snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
                snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
+               rec->needs_silencing = rec->is_dac;
                break;
        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
        case SNDRV_PCM_TRIGGER_SUSPEND:
@@ -963,7 +955,7 @@ static int snd_cmipci_playback_trigger(struct snd_pcm_substream *substream,
                                       int cmd)
 {
        struct cmipci *cm = snd_pcm_substream_chip(substream);
-       return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], substream, cmd);
+       return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], cmd);
 }
 
 static snd_pcm_uframes_t snd_cmipci_playback_pointer(struct snd_pcm_substream *substream)
@@ -982,7 +974,7 @@ static int snd_cmipci_capture_trigger(struct snd_pcm_substream *substream,
                                     int cmd)
 {
        struct cmipci *cm = snd_pcm_substream_chip(substream);
-       return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], substream, cmd);
+       return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], cmd);
 }
 
 static snd_pcm_uframes_t snd_cmipci_capture_pointer(struct snd_pcm_substream *substream)
@@ -1313,11 +1305,75 @@ static int snd_cmipci_playback_spdif_prepare(struct snd_pcm_substream *substream
        return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
 }
 
+/*
+ * Apparently, the samples last played on channel A stay in some buffer, even
+ * after the channel is reset, and get added to the data for the rear DACs when
+ * playing a multichannel stream on channel B.  This is likely to generate
+ * wraparounds and thus distortions.
+ * To avoid this, we play at least one zero sample after the actual stream has
+ * stopped.
+ */
+static void snd_cmipci_silence_hack(struct cmipci *cm, struct cmipci_pcm *rec)
+{
+       struct snd_pcm_runtime *runtime = rec->substream->runtime;
+       unsigned int reg, val;
+
+       if (rec->needs_silencing && runtime && runtime->dma_area) {
+               /* set up a small silence buffer */
+               memset(runtime->dma_area, 0, PAGE_SIZE);
+               reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
+               val = ((PAGE_SIZE / 4) - 1) | (((PAGE_SIZE / 4) / 2 - 1) << 16);
+               snd_cmipci_write(cm, reg, val);
+       
+               /* configure for 16 bits, 2 channels, 8 kHz */
+               if (runtime->channels > 2)
+                       set_dac_channels(cm, rec, 2);
+               spin_lock_irq(&cm->reg_lock);
+               val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
+               val &= ~(CM_ASFC_MASK << (rec->ch * 3));
+               val |= (4 << CM_ASFC_SHIFT) << (rec->ch * 3);
+               snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
+               val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
+               val &= ~(CM_CH0FMT_MASK << (rec->ch * 2));
+               val |= (3 << CM_CH0FMT_SHIFT) << (rec->ch * 2);
+               if (cm->chip_version == 68) {
+                       val &= ~(CM_CH0_SRATE_88K << (rec->ch * 2));
+                       val &= ~(CM_CH0_SRATE_96K << (rec->ch * 2));
+               }
+               snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
+       
+               /* start stream (we don't need interrupts) */
+               cm->ctrl |= CM_CHEN0 << rec->ch;
+               snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
+               spin_unlock_irq(&cm->reg_lock);
+
+               msleep(1);
+
+               /* stop and reset stream */
+               spin_lock_irq(&cm->reg_lock);
+               cm->ctrl &= ~(CM_CHEN0 << rec->ch);
+               val = CM_RST_CH0 << rec->ch;
+               snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | val);
+               snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~val);
+               spin_unlock_irq(&cm->reg_lock);
+
+               rec->needs_silencing = 0;
+       }
+}
+
 static int snd_cmipci_playback_hw_free(struct snd_pcm_substream *substream)
 {
        struct cmipci *cm = snd_pcm_substream_chip(substream);
        setup_spdif_playback(cm, substream, 0, 0);
        restore_mixer_state(cm);
+       snd_cmipci_silence_hack(cm, &cm->channel[0]);
+       return snd_cmipci_hw_free(substream);
+}
+
+static int snd_cmipci_playback2_hw_free(struct snd_pcm_substream *substream)
+{
+       struct cmipci *cm = snd_pcm_substream_chip(substream);
+       snd_cmipci_silence_hack(cm, &cm->channel[1]);
        return snd_cmipci_hw_free(substream);
 }
 
@@ -1745,7 +1801,7 @@ static struct snd_pcm_ops snd_cmipci_playback2_ops = {
        .close =        snd_cmipci_playback2_close,
        .ioctl =        snd_pcm_lib_ioctl,
        .hw_params =    snd_cmipci_playback2_hw_params,
-       .hw_free =      snd_cmipci_hw_free,
+       .hw_free =      snd_cmipci_playback2_hw_free,
        .prepare =      snd_cmipci_capture_prepare,     /* channel B */
        .trigger =      snd_cmipci_capture_trigger,     /* channel B */
        .pointer =      snd_cmipci_capture_pointer,     /* channel B */