patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / pci / atiixp.c
index 677bfba..8869e7a 100644 (file)
 #include <linux/init.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
+#include <linux/moduleparam.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
 #include <sound/pcm_params.h>
 #include <sound/info.h>
 #include <sound/ac97_codec.h>
-#define SNDRV_GET_ID
 #include <sound/initval.h>
 
 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
 MODULE_DESCRIPTION("ATI IXP AC97 controller");
 MODULE_LICENSE("GPL");
 MODULE_CLASSES("{sound}");
-MODULE_DEVICES("{{ATI,IXP150/200/250}}");
+MODULE_DEVICES("{{ATI,IXP150/200/250/300}}");
 
 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;     /* Index 0-MAX */
 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;      /* ID for this card */
 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;     /* Enable this card */
 static int ac97_clock[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 48000};
 static int spdif_aclink[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
+static int boot_devs;
 
-MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
+module_param_array(index, int, boot_devs, 0444);
 MODULE_PARM_DESC(index, "Index value for ATI IXP controller.");
 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
-MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
+module_param_array(id, charp, boot_devs, 0444);
 MODULE_PARM_DESC(id, "ID string for ATI IXP controller.");
 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
-MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
+module_param_array(enable, bool, boot_devs, 0444);
 MODULE_PARM_DESC(enable, "Enable audio part of ATI IXP controller.");
 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
-MODULE_PARM(ac97_clock, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
+module_param_array(ac97_clock, int, boot_devs, 0444);
 MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
 MODULE_PARM_SYNTAX(ac97_clock, SNDRV_ENABLED ",default:48000");
-MODULE_PARM(spdif_aclink, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
+module_param_array(spdif_aclink, bool, boot_devs, 0444);
 MODULE_PARM_DESC(spdif_aclink, "S/PDIF over AC-link.");
 MODULE_PARM_SYNTAX(spdif_aclink, SNDRV_ENABLED "," SNDRV_BOOLEAN_TRUE_DESC);
 
@@ -223,7 +224,12 @@ typedef struct atiixp_dma_desc {
 /*
  * stream enum
  */
-enum { ATI_DMA_PLAYBACK, ATI_DMA_CAPTURE, ATI_DMA_SPDIF };
+enum { ATI_DMA_PLAYBACK, ATI_DMA_CAPTURE, ATI_DMA_SPDIF, NUM_ATI_DMAS }; /* DMAs */
+enum { ATI_PCM_OUT, ATI_PCM_IN, ATI_PCM_SPDIF, NUM_ATI_PCMS }; /* AC97 pcm slots */
+enum { ATI_PCMDEV_ANALOG, ATI_PCMDEV_DIGITAL, NUM_ATI_PCMDEVS }; /* pcm devices */
+
+#define NUM_ATI_CODECS 3
+
 
 /*
  * constants and callbacks for each DMA type
@@ -231,6 +237,7 @@ enum { ATI_DMA_PLAYBACK, ATI_DMA_CAPTURE, ATI_DMA_SPDIF };
 struct snd_atiixp_dma_ops {
        int type;                       /* ATI_DMA_XXX */
        unsigned int llp_offset;        /* LINKPTR offset */
+       unsigned int dt_cur;            /* DT_CUR offset */
        void (*enable_dma)(atiixp_t *chip, int on);     /* called from open callback */
        void (*enable_transfer)(atiixp_t *chip, int on); /* called from trigger (START/STOP) */
        void (*flush_dma)(atiixp_t *chip);              /* called from trigger (STOP only) */
@@ -246,9 +253,10 @@ struct snd_atiixp_dma {
        snd_pcm_substream_t *substream; /* assigned PCM substream */
        unsigned int buf_addr, buf_bytes;       /* DMA buffer address, bytes */
        unsigned int period_bytes, periods;
+       int opened;
        int running;
-       struct ac97_pcm *pcm;
        int pcm_open_flag;
+       int ac97_pcm_type;      /* index # of ac97_pcm to access, -1 = not used */
 };
 
 /*
@@ -264,25 +272,34 @@ struct snd_atiixp {
        int irq;
        
        ac97_bus_t *ac97_bus;
-       ac97_t *ac97[3];                /* IXP can have up to 3 codecs */
+       ac97_t *ac97[NUM_ATI_CODECS];
 
        spinlock_t reg_lock;
        spinlock_t ac97_lock;
 
-       atiixp_dma_t dmas[3];           /* playback, capture, spdif */
+       atiixp_dma_t dmas[NUM_ATI_DMAS];
+       struct ac97_pcm *pcms[NUM_ATI_PCMS];
+       snd_pcm_t *pcmdevs[NUM_ATI_PCMDEVS];
 
        int max_channels;               /* max. channels for PCM out */
 
        unsigned int codec_not_ready_bits;      /* for codec detection */
 
        int spdif_over_aclink;          /* passed from the module option */
+       struct semaphore open_mutex;    /* playback open mutex */
+
+#ifdef CONFIG_PM
+       u32 pci_state[16];
+#endif
 };
 
 
 /*
  */
 static struct pci_device_id snd_atiixp_ids[] = {
-       { 0x1002, 0x4341, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
+       { 0x1002, 0x4341, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB200 */
+       { 0x1002, 0x4361, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB300 */
+       { 0, }
 };
 
 MODULE_DEVICE_TABLE(pci, snd_atiixp_ids);
@@ -393,7 +410,7 @@ static int atiixp_build_dma_packets(atiixp_t *chip, atiixp_dma_t *dma,
                addr += period_bytes;
        }
 
-       writel(cpu_to_le32((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN),
+       writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
               chip->remap_addr + dma->ops->llp_offset);
 
        dma->period_bytes = period_bytes;
@@ -452,7 +469,9 @@ static unsigned short snd_atiixp_codec_read(atiixp_t *chip, unsigned short codec
                        return data >> ATI_REG_PHYS_IN_DATA_SHIFT;
                udelay(1);
        } while (--timeout);
-       snd_printk(KERN_WARNING "atiixp: codec read timeout\n");
+       /* time out may happen during reset */
+       if (reg < 0x7c)
+               snd_printk(KERN_WARNING "atiixp: codec read timeout (reg %x)\n", reg);
        return 0xffff;
 }
 
@@ -527,13 +546,11 @@ static int snd_atiixp_aclink_reset(atiixp_t *chip)
        return 0;
 }
 
-#if 0 /* for P/M */
+#ifdef CONFIG_PM
 static int snd_atiixp_aclink_down(atiixp_t *chip)
 {
-       unsigned long flags;
-
-       if (atiixp_read(chip, MODEM_MIRROR) & ATI_REG_MODEM_MIRROR_RUNNING)
-               return -EBUSY;
+       // if (atiixp_read(chip, MODEM_MIRROR) & 0x1) /* modem running, too? */
+       //      return -EBUSY;
        atiixp_update(chip, CMD,
                     ATI_REG_CMD_POWERDOWN | ATI_REG_CMD_AC_RESET,
                     ATI_REG_CMD_POWERDOWN);
@@ -585,12 +602,16 @@ static int snd_atiixp_chip_start(atiixp_t *chip)
 {
        unsigned int reg;
 
-       /* enable burst mode */
+       /* set up spdif, enable burst mode */
        reg = atiixp_read(chip, CMD);
        reg |= 0x02 << ATI_REG_CMD_SPDF_THRESHOLD_SHIFT;
        reg |= ATI_REG_CMD_BURST_EN;
        atiixp_write(chip, CMD, reg);
 
+       reg = atiixp_read(chip, SPDF_CMD);
+       reg &= ~(ATI_REG_SPDF_CMD_LFSR|ATI_REG_SPDF_CMD_SINGLE_CH);
+       atiixp_write(chip, SPDF_CMD, reg);
+
        /* clear all interrupt source */
        atiixp_write(chip, ISR, 0xffffffff);
        /* enable irqs */
@@ -635,7 +656,7 @@ static snd_pcm_uframes_t snd_atiixp_pcm_pointer(snd_pcm_substream_t *substream)
        unsigned long flags;
 
        spin_lock_irqsave(&chip->reg_lock, flags);
-       curptr = readl(chip->remap_addr + dma->ops->llp_offset + 12); /* XXX_DMA_DT_CUR */
+       curptr = readl(chip->remap_addr + dma->ops->dt_cur);
        if (curptr < dma->buf_addr) {
                snd_printdd("curptr = %x, base = %x\n", curptr, dma->buf_addr);
                curptr = 0;
@@ -657,7 +678,7 @@ static void snd_atiixp_xrun_dma(atiixp_t *chip, atiixp_dma_t *dma)
 {
        if (! dma->substream || ! dma->running)
                return;
-       snd_printd(KERN_DEBUG "atiixp: XRUN detected (DMA %d)\n", dma->ops->type);
+       snd_printdd("atiixp: XRUN detected (DMA %d)\n", dma->ops->type);
        snd_pcm_stop(dma->substream, SNDRV_PCM_STATE_XRUN);
 }
 
@@ -800,15 +821,10 @@ static void atiixp_spdif_enable_transfer(atiixp_t *chip, int on)
 {
        unsigned int data;
        data = atiixp_read(chip, CMD);
-       if (on) {
+       if (on)
                data |= ATI_REG_CMD_SPDF_OUT_EN;
-               if (chip->spdif_over_aclink)
-                       data |= ATI_REG_CMD_SEND_EN;
-       }  else {
+       else
                data &= ~ATI_REG_CMD_SPDF_OUT_EN;
-               if (chip->spdif_over_aclink)
-                       data &= ~ATI_REG_CMD_SEND_EN;
-       }
        atiixp_write(chip, CMD, data);
 }
 
@@ -835,25 +851,25 @@ static void atiixp_spdif_flush_dma(atiixp_t *chip)
 static int snd_atiixp_spdif_prepare(snd_pcm_substream_t *substream)
 {
        atiixp_t *chip = snd_pcm_substream_chip(substream);
-       unsigned int data;
 
        spin_lock(&chip->reg_lock);
        if (chip->spdif_over_aclink) {
+               unsigned int data;
                /* enable slots 10/11 */
                atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK,
                              ATI_REG_CMD_SPDF_CONFIG_01);
-               data = atiixp_read(chip, OUT_DMA_SLOT);
+               data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
                data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
                        ATI_REG_OUT_DMA_SLOT_BIT(11);
                data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
                atiixp_write(chip, OUT_DMA_SLOT, data);
+               atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
+                             substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
+                             ATI_REG_CMD_INTERLEAVE_OUT : 0);
        } else {
                atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK, 0);
+               atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_SPDF, 0);
        }
-
-       atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_SPDF,
-                     substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
-                     ATI_REG_CMD_INTERLEAVE_SPDF : 0);
        spin_unlock(&chip->reg_lock);
        return 0;
 }
@@ -938,17 +954,18 @@ static int snd_atiixp_pcm_hw_params(snd_pcm_substream_t *substream,
        if (err < 0)
                return err;
 
-       if (dma->pcm) {
+       if (dma->ac97_pcm_type >= 0) {
+               struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
                /* PCM is bound to AC97 codec(s)
                 * set up the AC97 codecs
                 */
                if (dma->pcm_open_flag) {
-                       snd_ac97_pcm_close(dma->pcm);
+                       snd_ac97_pcm_close(pcm);
                        dma->pcm_open_flag = 0;
                }
-               err = snd_ac97_pcm_open(dma->pcm, params_rate(hw_params),
+               err = snd_ac97_pcm_open(pcm, params_rate(hw_params),
                                        params_channels(hw_params),
-                                       dma->pcm->r[0].slots);
+                                       pcm->r[0].slots);
                if (err >= 0)
                        dma->pcm_open_flag = 1;
        }
@@ -962,7 +979,8 @@ static int snd_atiixp_pcm_hw_free(snd_pcm_substream_t * substream)
        atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
 
        if (dma->pcm_open_flag) {
-               snd_ac97_pcm_close(dma->pcm);
+               struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
+               snd_ac97_pcm_close(pcm);
                dma->pcm_open_flag = 0;
        }
        atiixp_clear_dma_packets(chip, dma, substream);
@@ -992,7 +1010,7 @@ static snd_pcm_hardware_t snd_atiixp_pcm_hw =
        .periods_max =          ATI_MAX_DESCRIPTORS,
 };
 
-static int snd_atiixp_pcm_open(snd_pcm_substream_t *substream, atiixp_dma_t *dma)
+static int snd_atiixp_pcm_open(snd_pcm_substream_t *substream, atiixp_dma_t *dma, int pcm_type)
 {
        atiixp_t *chip = snd_pcm_substream_chip(substream);
        snd_pcm_runtime_t *runtime = substream->runtime;
@@ -1001,15 +1019,17 @@ static int snd_atiixp_pcm_open(snd_pcm_substream_t *substream, atiixp_dma_t *dma
 
        snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
 
+       if (dma->opened)
+               return -EBUSY;
        dma->substream = substream;
        runtime->hw = snd_atiixp_pcm_hw;
-       if (dma->pcm) {
-               runtime->hw.rates = dma->pcm->rates;
+       dma->ac97_pcm_type = pcm_type;
+       if (pcm_type >= 0) {
+               runtime->hw.rates = chip->pcms[pcm_type]->rates;
                snd_pcm_limit_hw_rates(runtime);
        } else {
-               /* SPDIF */
-               runtime->hw.rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_32000;
-               runtime->hw.rate_min = 32000;
+               /* direct SPDIF */
+               runtime->hw.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
        }
        if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
                return err;
@@ -1019,6 +1039,7 @@ static int snd_atiixp_pcm_open(snd_pcm_substream_t *substream, atiixp_dma_t *dma
        spin_lock_irqsave(&chip->reg_lock, flags);
        dma->ops->enable_dma(chip, 1);
        spin_unlock_irqrestore(&chip->reg_lock, flags);
+       dma->opened = 1;
 
        return 0;
 }
@@ -1032,6 +1053,7 @@ static int snd_atiixp_pcm_close(snd_pcm_substream_t *substream, atiixp_dma_t *dm
        dma->ops->enable_dma(chip, 0);
        spin_unlock_irq(&chip->reg_lock);
        dma->substream = NULL;
+       dma->opened = 0;
        return 0;
 }
 
@@ -1042,26 +1064,33 @@ static int snd_atiixp_playback_open(snd_pcm_substream_t *substream)
        atiixp_t *chip = snd_pcm_substream_chip(substream);
        int err;
 
-       err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
+       down(&chip->open_mutex);
+       err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
+       up(&chip->open_mutex);
+       if (err < 0)
+               return err;
        substream->runtime->hw.channels_max = chip->max_channels;
        if (chip->max_channels > 2)
                /* channels must be even */
                snd_pcm_hw_constraint_step(substream->runtime, 0,
                                           SNDRV_PCM_HW_PARAM_CHANNELS, 2);
-
        return 0;
 }
 
 static int snd_atiixp_playback_close(snd_pcm_substream_t *substream)
 {
        atiixp_t *chip = snd_pcm_substream_chip(substream);
-       return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
+       int err;
+       down(&chip->open_mutex);
+       err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
+       up(&chip->open_mutex);
+       return err;
 }
 
 static int snd_atiixp_capture_open(snd_pcm_substream_t *substream)
 {
        atiixp_t *chip = snd_pcm_substream_chip(substream);
-       return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE]);
+       return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
 }
 
 static int snd_atiixp_capture_close(snd_pcm_substream_t *substream)
@@ -1073,13 +1102,27 @@ static int snd_atiixp_capture_close(snd_pcm_substream_t *substream)
 static int snd_atiixp_spdif_open(snd_pcm_substream_t *substream)
 {
        atiixp_t *chip = snd_pcm_substream_chip(substream);
-       return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_SPDIF]);
+       int err;
+       down(&chip->open_mutex);
+       if (chip->spdif_over_aclink) /* share DMA_PLAYBACK */
+               err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 2);
+       else
+               err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_SPDIF], -1);
+       up(&chip->open_mutex);
+       return err;
 }
 
 static int snd_atiixp_spdif_close(snd_pcm_substream_t *substream)
 {
        atiixp_t *chip = snd_pcm_substream_chip(substream);
-       return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_SPDIF]);
+       int err;
+       down(&chip->open_mutex);
+       if (chip->spdif_over_aclink)
+               err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
+       else
+               err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_SPDIF]);
+       up(&chip->open_mutex);
+       return err;
 }
 
 /* AC97 playback */
@@ -1157,6 +1200,7 @@ static struct ac97_pcm atiixp_pcm_defs[] __devinitdata = {
 static atiixp_dma_ops_t snd_atiixp_playback_dma_ops = {
        .type = ATI_DMA_PLAYBACK,
        .llp_offset = ATI_REG_OUT_DMA_LINKPTR,
+       .dt_cur = ATI_REG_OUT_DMA_DT_CUR,
        .enable_dma = atiixp_out_enable_dma,
        .enable_transfer = atiixp_out_enable_transfer,
        .flush_dma = atiixp_out_flush_dma,
@@ -1165,6 +1209,7 @@ static atiixp_dma_ops_t snd_atiixp_playback_dma_ops = {
 static atiixp_dma_ops_t snd_atiixp_capture_dma_ops = {
        .type = ATI_DMA_CAPTURE,
        .llp_offset = ATI_REG_IN_DMA_LINKPTR,
+       .dt_cur = ATI_REG_IN_DMA_DT_CUR,
        .enable_dma = atiixp_in_enable_dma,
        .enable_transfer = atiixp_in_enable_transfer,
        .flush_dma = atiixp_in_flush_dma,
@@ -1173,6 +1218,7 @@ static atiixp_dma_ops_t snd_atiixp_capture_dma_ops = {
 static atiixp_dma_ops_t snd_atiixp_spdif_dma_ops = {
        .type = ATI_DMA_SPDIF,
        .llp_offset = ATI_REG_SPDF_DMA_LINKPTR,
+       .dt_cur = ATI_REG_SPDF_DMA_DT_CUR,
        .enable_dma = atiixp_spdif_enable_dma,
        .enable_transfer = atiixp_spdif_enable_transfer,
        .flush_dma = atiixp_spdif_flush_dma,
@@ -1188,7 +1234,8 @@ static int __devinit snd_atiixp_pcm_new(atiixp_t *chip)
        /* initialize constants */
        chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
        chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
-       chip->dmas[ATI_DMA_SPDIF].ops = &snd_atiixp_spdif_dma_ops;
+       if (! chip->spdif_over_aclink)
+               chip->dmas[ATI_DMA_SPDIF].ops = &snd_atiixp_spdif_dma_ops;
 
        /* assign AC97 pcm */
        if (chip->spdif_over_aclink)
@@ -1198,49 +1245,55 @@ static int __devinit snd_atiixp_pcm_new(atiixp_t *chip)
        err = snd_ac97_pcm_assign(pbus, num_pcms, atiixp_pcm_defs);
        if (err < 0)
                return err;
+       for (i = 0; i < num_pcms; i++)
+               chip->pcms[i] = &pbus->pcms[i];
 
        chip->max_channels = 2;
-       if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
-               if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_LFE))
+       if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
+               if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_LFE))
                        chip->max_channels = 6;
                else
                        chip->max_channels = 4;
        }
 
-       chip->dmas[ATI_DMA_PLAYBACK].pcm = &pbus->pcms[0];
-       chip->dmas[ATI_DMA_CAPTURE].pcm = &pbus->pcms[1];
-       if (chip->spdif_over_aclink)
-               chip->dmas[ATI_DMA_SPDIF].pcm = &pbus->pcms[2];
-
        /* PCM #0: analog I/O */
-       err = snd_pcm_new(chip->card, "ATI IXP AC97", 0, 1, 1, &pcm);
+       err = snd_pcm_new(chip->card, "ATI IXP AC97", ATI_PCMDEV_ANALOG, 1, 1, &pcm);
        if (err < 0)
                return err;
        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
        pcm->private_data = chip;
        strcpy(pcm->name, "ATI IXP AC97");
+       chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
 
        snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
                                              snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
 
        /* no SPDIF support on codec? */
-       if (chip->dmas[ATI_DMA_SPDIF].pcm && ! chip->dmas[ATI_DMA_SPDIF].pcm->rates)
+       if (chip->pcms[ATI_PCM_SPDIF] && ! chip->pcms[ATI_PCM_SPDIF]->rates)
                return 0;
                
+       /* FIXME: non-48k sample rate doesn't work on my test machine with AD1888 */
+       if (chip->pcms[ATI_PCM_SPDIF])
+               chip->pcms[ATI_PCM_SPDIF]->rates = SNDRV_PCM_RATE_48000;
+
        /* PCM #1: spdif playback */
-       err = snd_pcm_new(chip->card, "ATI IXP IEC958", 1, 1, 0, &pcm);
+       err = snd_pcm_new(chip->card, "ATI IXP IEC958", ATI_PCMDEV_DIGITAL, 1, 0, &pcm);
        if (err < 0)
                return err;
        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_spdif_ops);
        pcm->private_data = chip;
-       strcpy(pcm->name, "ATI IXP IEC958");
+       if (chip->spdif_over_aclink)
+               strcpy(pcm->name, "ATI IXP IEC958 (AC97)");
+       else
+               strcpy(pcm->name, "ATI IXP IEC958 (Direct)");
+       chip->pcmdevs[ATI_PCMDEV_DIGITAL] = pcm;
 
        snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
                                              snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
 
        /* pre-select AC97 SPDIF slots 10/11 */
-       for (i = 0; i < 3; i++) {
+       for (i = 0; i < NUM_ATI_CODECS; i++) {
                if (chip->ac97[i])
                        snd_ac97_update_bits(chip->ac97[i], AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
        }
@@ -1272,10 +1325,12 @@ static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id, struct pt_regs *r
                snd_atiixp_xrun_dma(chip,  &chip->dmas[ATI_DMA_CAPTURE]);
        else if (status & ATI_REG_ISR_IN_STATUS)
                snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
-       if (status & ATI_REG_ISR_SPDF_XRUN)
-               snd_atiixp_xrun_dma(chip,  &chip->dmas[ATI_DMA_SPDIF]);
-       else if (status & ATI_REG_ISR_SPDF_STATUS)
-               snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
+       if (! chip->spdif_over_aclink) {
+               if (status & ATI_REG_ISR_SPDF_XRUN)
+                       snd_atiixp_xrun_dma(chip,  &chip->dmas[ATI_DMA_SPDIF]);
+               else if (status & ATI_REG_ISR_SPDF_STATUS)
+                       snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
+       }
 
        /* for codec detection */
        if (status & CODEC_CHECK_BITS) {
@@ -1303,7 +1358,8 @@ static int __devinit snd_atiixp_mixer_new(atiixp_t *chip, int clock)
        ac97_bus_t bus, *pbus;
        ac97_t ac97;
        int i, err;
-       static unsigned int codec_skip[3] = {
+       int codec_count;
+       static unsigned int codec_skip[NUM_ATI_CODECS] = {
                ATI_REG_ISR_CODEC0_NOT_READY,
                ATI_REG_ISR_CODEC1_NOT_READY,
                ATI_REG_ISR_CODEC2_NOT_READY,
@@ -1321,15 +1377,34 @@ static int __devinit snd_atiixp_mixer_new(atiixp_t *chip, int clock)
                return err;
        chip->ac97_bus = pbus;
 
-       for (i = 0; i < 3; i++) {
+       codec_count = 0;
+       for (i = 0; i < NUM_ATI_CODECS; i++) {
                if (chip->codec_not_ready_bits & codec_skip[i])
                        continue;
                memset(&ac97, 0, sizeof(ac97));
                ac97.private_data = chip;
                ac97.pci = chip->pci;
                ac97.num = i;
-               if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0)
-                       return err;
+               ac97.scaps = AC97_SCAP_SKIP_MODEM;
+               if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
+                       if (chip->codec_not_ready_bits)
+                               /* codec(s) was detected but not available.
+                                * return the error
+                                */
+                               return err;
+                       else {
+                               /* codec(s) was NOT detected, so just ignore here */
+                               chip->ac97[i] = NULL; /* to be sure */
+                               snd_printd("atiixp: codec %d not found\n", i);
+                               continue;
+                       }
+               }
+               codec_count++;
+       }
+
+       if (! codec_count) {
+               snd_printk(KERN_ERR "atiixp: no codec available\n");
+               return -ENODEV;
        }
 
        /* snd_ac97_tune_hardware(chip->ac97, ac97_quirks); */
@@ -1338,6 +1413,53 @@ static int __devinit snd_atiixp_mixer_new(atiixp_t *chip, int clock)
 }
 
 
+#ifdef CONFIG_PM
+/*
+ * power management
+ */
+static int snd_atiixp_suspend(snd_card_t *card, unsigned int state)
+{
+       atiixp_t *chip = snd_magic_cast(atiixp_t, card->pm_private_data, return -EINVAL);
+       int i;
+
+       for (i = 0; i < NUM_ATI_PCMDEVS; i++)
+               if (chip->pcmdevs[i])
+                       snd_pcm_suspend_all(chip->pcmdevs[i]);
+       for (i = 0; i < NUM_ATI_CODECS; i++)
+               if (chip->ac97[i])
+                       snd_ac97_suspend(chip->ac97[i]);
+       snd_atiixp_aclink_down(chip);
+       snd_atiixp_chip_stop(chip);
+
+       pci_save_state(chip->pci, chip->pci_state);
+       pci_set_power_state(chip->pci, 3);
+       pci_disable_device(chip->pci);
+       snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
+       return 0;
+}
+
+static int snd_atiixp_resume(snd_card_t *card, unsigned int state)
+{
+       atiixp_t *chip = snd_magic_cast(atiixp_t, card->pm_private_data, return -EINVAL);
+       int i;
+
+       pci_enable_device(chip->pci);
+       pci_restore_state(chip->pci, chip->pci_state);
+       pci_set_power_state(chip->pci, 0);
+
+       snd_atiixp_aclink_reset(chip);
+       snd_atiixp_chip_start(chip);
+
+       for (i = 0; i < NUM_ATI_CODECS; i++)
+               if (chip->ac97[i])
+                       snd_ac97_resume(chip->ac97[i]);
+
+       snd_power_change_state(card, SNDRV_CTL_POWER_D0);
+       return 0;
+}
+#endif /* CONFIG_PM */
+
+
 /*
  * proc interface for register dump
  */
@@ -1412,24 +1534,25 @@ static int __devinit snd_atiixp_create(snd_card_t *card,
 
        spin_lock_init(&chip->reg_lock);
        spin_lock_init(&chip->ac97_lock);
+       init_MUTEX(&chip->open_mutex);
        chip->card = card;
        chip->pci = pci;
        chip->irq = -1;
        chip->addr = pci_resource_start(pci, 0);
        if ((chip->res = request_mem_region(chip->addr, ATI_MEM_REGION, "ATI IXP AC97")) == NULL) {
-               snd_printk("unable to grab I/O memory 0x%lx\n", chip->addr);
+               snd_printk(KERN_ERR "unable to grab I/O memory 0x%lx\n", chip->addr);
                snd_atiixp_free(chip);
                return -EBUSY;
        }
        chip->remap_addr = (unsigned long) ioremap_nocache(chip->addr, ATI_MEM_REGION);
        if (chip->remap_addr == 0) {
-               snd_printk("AC'97 space ioremap problem\n");
+               snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
                snd_atiixp_free(chip);
                return -EIO;
        }
 
        if (request_irq(pci->irq, snd_atiixp_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
-               snd_printk("unable to grab IRQ %d\n", pci->irq);
+               snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
                snd_atiixp_free(chip);
                return -EBUSY;
        }
@@ -1471,7 +1594,7 @@ static int __devinit snd_atiixp_probe(struct pci_dev *pci,
 
        pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
 
-       strcpy(card->driver, "ATIIXP");
+       strcpy(card->driver, spdif_aclink[dev] ? "ATIIXP" : "ATIIXP-SPDMA");
        strcpy(card->shortname, "ATI IXP");
        if ((err = snd_atiixp_create(card, pci, &chip)) < 0)
                goto __error;
@@ -1494,10 +1617,12 @@ static int __devinit snd_atiixp_probe(struct pci_dev *pci,
        sprintf(card->longname, "%s rev %x at 0x%lx, irq %i",
                card->shortname, revision, chip->addr, chip->irq);
 
+       snd_card_set_pm_callback(card, snd_atiixp_suspend, snd_atiixp_resume, chip);
+
        if ((err = snd_card_register(card)) < 0)
                goto __error;
 
-       pci_set_drvdata(pci, chip);
+       pci_set_drvdata(pci, card);
        dev++;
        return 0;
 
@@ -1508,9 +1633,7 @@ static int __devinit snd_atiixp_probe(struct pci_dev *pci,
 
 static void __devexit snd_atiixp_remove(struct pci_dev *pci)
 {
-       atiixp_t *chip = snd_magic_cast(atiixp_t, pci_get_drvdata(pci), return);
-       if (chip)
-               snd_card_free(chip->card);
+       snd_card_free(pci_get_drvdata(pci));
        pci_set_drvdata(pci, NULL);
 }
 
@@ -1519,21 +1642,13 @@ static struct pci_driver driver = {
        .id_table = snd_atiixp_ids,
        .probe = snd_atiixp_probe,
        .remove = __devexit_p(snd_atiixp_remove),
+       SND_PCI_PM_CALLBACKS
 };
 
 
 static int __init alsa_card_atiixp_init(void)
 {
-       int err;
-
-        if ((err = pci_module_init(&driver)) < 0) {
-#ifdef MODULE
-               printk(KERN_ERR "ATI IXP AC97 controller not found or device busy\n");
-#endif
-                return err;
-        }
-
-        return 0;
+       return pci_module_init(&driver);
 }
 
 static void __exit alsa_card_atiixp_exit(void)
@@ -1543,27 +1658,3 @@ static void __exit alsa_card_atiixp_exit(void)
 
 module_init(alsa_card_atiixp_init)
 module_exit(alsa_card_atiixp_exit)
-
-#ifndef MODULE
-
-/* format is: snd-atiixp=enable,index,id,ac97_clock,spdif_aclink */
-
-static int __init alsa_card_atiixp_setup(char *str)
-{
-       static unsigned __initdata nr_dev = 0;
-
-       if (nr_dev >= SNDRV_CARDS)
-               return 0;
-       (void)(get_option(&str,&enable[nr_dev]) == 2 &&
-              get_option(&str,&index[nr_dev]) == 2 &&
-              get_id(&str,&id[nr_dev]) == 2 &&
-              get_option(&str,&ac97_clock[nr_dev]) == 2 &&
-              get_option(&str,&spdif_aclink[nr_dev]) == 2
-              );
-       nr_dev++;
-       return 1;
-}
-
-__setup("snd-atiixp=", alsa_card_atiixp_setup);
-
-#endif /* ifndef MODULE */