ALSA: hda: Use mb31 quirk for an iMac model
[safe/jmp/linux-2.6] / sound / pci / nm256 / nm256.c
index 0d0ff54..5a60492 100644 (file)
@@ -24,7 +24,6 @@
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */
   
-#include <sound/driver.h>
 #include <asm/io.h>
 #include <linux/delay.h>
 #include <linux/interrupt.h>
@@ -32,6 +31,8 @@
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/moduleparam.h>
+#include <linux/mutex.h>
+
 #include <sound/core.h>
 #include <sound/info.h>
 #include <sound/control.h>
@@ -226,6 +227,7 @@ struct nm256 {
        unsigned int use_cache: 1;      /* use one big coef. table */
        unsigned int reset_workaround: 1; /* Workaround for some laptops to avoid freeze */
        unsigned int reset_workaround_2: 1; /* Extended workaround for some other laptops to avoid freeze */
+       unsigned int in_resume: 1;
 
        int mixer_base;                 /* register offset of ac97 mixer */
        int mixer_status_offset;        /* offset of mixer status reg. */
@@ -233,13 +235,14 @@ struct nm256 {
 
        int irq;
        int irq_acks;
-       irqreturn_t (*interrupt)(int, void *, struct pt_regs *);
+       irq_handler_t interrupt;
        int badintrcount;               /* counter to check bogus interrupts */
-       struct semaphore irq_mutex;
+       struct mutex irq_mutex;
 
        struct nm256_stream streams[2];
 
        struct snd_ac97 *ac97;
+       unsigned short *ac97_regs; /* register caches, only for valid regs */
 
        struct snd_pcm *pcm;
 
@@ -259,10 +262,10 @@ struct nm256 {
 /*
  * PCI ids
  */
-static struct pci_device_id snd_nm256_ids[] = {
-       {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
-       {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
-       {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
+static DEFINE_PCI_DEVICE_TABLE(snd_nm256_ids) = {
+       {PCI_VDEVICE(NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO), 0},
+       {PCI_VDEVICE(NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO), 0},
+       {PCI_VDEVICE(NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO), 0},
        {0,},
 };
 
@@ -459,32 +462,32 @@ snd_nm256_set_format(struct nm256 *chip, struct nm256_stream *s,
 /* acquire interrupt */
 static int snd_nm256_acquire_irq(struct nm256 *chip)
 {
-       down(&chip->irq_mutex);
+       mutex_lock(&chip->irq_mutex);
        if (chip->irq < 0) {
-               if (request_irq(chip->pci->irq, chip->interrupt, SA_INTERRUPT|SA_SHIRQ,
+               if (request_irq(chip->pci->irq, chip->interrupt, IRQF_SHARED,
                                chip->card->driver, chip)) {
                        snd_printk(KERN_ERR "unable to grab IRQ %d\n", chip->pci->irq);
-                       up(&chip->irq_mutex);
+                       mutex_unlock(&chip->irq_mutex);
                        return -EBUSY;
                }
                chip->irq = chip->pci->irq;
        }
        chip->irq_acks++;
-       up(&chip->irq_mutex);
+       mutex_unlock(&chip->irq_mutex);
        return 0;
 }
 
 /* release interrupt */
 static void snd_nm256_release_irq(struct nm256 *chip)
 {
-       down(&chip->irq_mutex);
+       mutex_lock(&chip->irq_mutex);
        if (chip->irq_acks > 0)
                chip->irq_acks--;
        if (chip->irq_acks == 0 && chip->irq >= 0) {
                free_irq(chip->irq, chip);
                chip->irq = -1;
        }
-       up(&chip->irq_mutex);
+       mutex_unlock(&chip->irq_mutex);
 }
 
 /*
@@ -559,7 +562,8 @@ snd_nm256_playback_trigger(struct snd_pcm_substream *substream, int cmd)
        struct nm256_stream *s = substream->runtime->private_data;
        int err = 0;
 
-       snd_assert(s != NULL, return -ENXIO);
+       if (snd_BUG_ON(!s))
+               return -ENXIO;
 
        spin_lock(&chip->reg_lock);
        switch (cmd) {
@@ -596,7 +600,8 @@ snd_nm256_capture_trigger(struct snd_pcm_substream *substream, int cmd)
        struct nm256_stream *s = substream->runtime->private_data;
        int err = 0;
 
-       snd_assert(s != NULL, return -ENXIO);
+       if (snd_BUG_ON(!s))
+               return -ENXIO;
 
        spin_lock(&chip->reg_lock);
        switch (cmd) {
@@ -632,7 +637,8 @@ static int snd_nm256_pcm_prepare(struct snd_pcm_substream *substream)
        struct snd_pcm_runtime *runtime = substream->runtime;
        struct nm256_stream *s = runtime->private_data;
 
-       snd_assert(s, return -ENXIO);
+       if (snd_BUG_ON(!s))
+               return -ENXIO;
        s->dma_size = frames_to_bytes(runtime, substream->runtime->buffer_size);
        s->period_size = frames_to_bytes(runtime, substream->runtime->period_size);
        s->periods = substream->runtime->periods;
@@ -657,7 +663,8 @@ snd_nm256_playback_pointer(struct snd_pcm_substream *substream)
        struct nm256_stream *s = substream->runtime->private_data;
        unsigned long curp;
 
-       snd_assert(s, return 0);
+       if (snd_BUG_ON(!s))
+               return 0;
        curp = snd_nm256_readl(chip, NM_PBUFFER_CURRP) - (unsigned long)s->buf;
        curp %= s->dma_size;
        return bytes_to_frames(substream->runtime, curp);
@@ -670,7 +677,8 @@ snd_nm256_capture_pointer(struct snd_pcm_substream *substream)
        struct nm256_stream *s = substream->runtime->private_data;
        unsigned long curp;
 
-       snd_assert(s != NULL, return 0);
+       if (snd_BUG_ON(!s))
+               return 0;
        curp = snd_nm256_readl(chip, NM_RBUFFER_CURRP) - (unsigned long)s->buf;
        curp %= s->dma_size;    
        return bytes_to_frames(substream->runtime, curp);
@@ -838,7 +846,6 @@ static void snd_nm256_setup_stream(struct nm256 *chip, struct nm256_stream *s,
        runtime->private_data = s;
        s->substream = substream;
 
-       snd_pcm_set_sync(substream);
        snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
                                   &constraints_rates);
 }
@@ -1000,7 +1007,7 @@ snd_nm256_intr_check(struct nm256 *chip)
  */
 
 static irqreturn_t
-snd_nm256_interrupt(int irq, void *dev_id, struct pt_regs *dummy)
+snd_nm256_interrupt(int irq, void *dev_id)
 {
        struct nm256 *chip = dev_id;
        u16 status;
@@ -1065,7 +1072,7 @@ snd_nm256_interrupt(int irq, void *dev_id, struct pt_regs *dummy)
  */
 
 static irqreturn_t
-snd_nm256_interrupt_zx(int irq, void *dev_id, struct pt_regs *dummy)
+snd_nm256_interrupt_zx(int irq, void *dev_id)
 {
        struct nm256 *chip = dev_id;
        u32 status;
@@ -1151,23 +1158,63 @@ snd_nm256_ac97_ready(struct nm256 *chip)
        return 0;
 }
 
+/* 
+ * Initial register values to be written to the AC97 mixer.
+ * While most of these are identical to the reset values, we do this
+ * so that we have most of the register contents cached--this avoids
+ * reading from the mixer directly (which seems to be problematic,
+ * probably due to ignorance).
+ */
+
+struct initialValues {
+       unsigned short reg;
+       unsigned short value;
+};
+
+static struct initialValues nm256_ac97_init_val[] =
+{
+       { AC97_MASTER,          0x8000 },
+       { AC97_HEADPHONE,       0x8000 },
+       { AC97_MASTER_MONO,     0x8000 },
+       { AC97_PC_BEEP,         0x8000 },
+       { AC97_PHONE,           0x8008 },
+       { AC97_MIC,             0x8000 },
+       { AC97_LINE,            0x8808 },
+       { AC97_CD,              0x8808 },
+       { AC97_VIDEO,           0x8808 },
+       { AC97_AUX,             0x8808 },
+       { AC97_PCM,             0x8808 },
+       { AC97_REC_SEL,         0x0000 },
+       { AC97_REC_GAIN,        0x0B0B },
+       { AC97_GENERAL_PURPOSE, 0x0000 },
+       { AC97_3D_CONTROL,      0x8000 }, 
+       { AC97_VENDOR_ID1,      0x8384 },
+       { AC97_VENDOR_ID2,      0x7609 },
+};
+
+static int nm256_ac97_idx(unsigned short reg)
+{
+       int i;
+       for (i = 0; i < ARRAY_SIZE(nm256_ac97_init_val); i++)
+               if (nm256_ac97_init_val[i].reg == reg)
+                       return i;
+       return -1;
+}
+
 /*
+ * some nm256 easily crash when reading from mixer registers
+ * thus we're treating it as a write-only mixer and cache the
+ * written values
  */
 static unsigned short
 snd_nm256_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
 {
        struct nm256 *chip = ac97->private_data;
-       int res;
+       int idx = nm256_ac97_idx(reg);
 
-       if (reg >= 128)
+       if (idx < 0)
                return 0;
-
-       if (! snd_nm256_ac97_ready(chip))
-               return 0;
-       res = snd_nm256_readw(chip, chip->mixer_base + reg);
-       /* Magic delay.  Bleah yucky.  */
-       msleep(1);
-       return res;
+       return chip->ac97_regs[idx];
 }
 
 /* 
@@ -1178,8 +1225,12 @@ snd_nm256_ac97_write(struct snd_ac97 *ac97,
 {
        struct nm256 *chip = ac97->private_data;
        int tries = 2;
+       int idx = nm256_ac97_idx(reg);
        u32 base;
 
+       if (idx < 0)
+               return;
+
        base = chip->mixer_base;
 
        snd_nm256_ac97_ready(chip);
@@ -1188,12 +1239,32 @@ snd_nm256_ac97_write(struct snd_ac97 *ac97,
        while (tries-- > 0) {
                snd_nm256_writew(chip, base + reg, val);
                msleep(1);  /* a little delay here seems better.. */
-               if (snd_nm256_ac97_ready(chip))
+               if (snd_nm256_ac97_ready(chip)) {
+                       /* successful write: set cache */
+                       chip->ac97_regs[idx] = val;
                        return;
+               }
        }
        snd_printd("nm256: ac97 codec not ready..\n");
 }
 
+/* static resolution table */
+static struct snd_ac97_res_table nm256_res_table[] = {
+       { AC97_MASTER, 0x1f1f },
+       { AC97_HEADPHONE, 0x1f1f },
+       { AC97_MASTER_MONO, 0x001f },
+       { AC97_PC_BEEP, 0x001f },
+       { AC97_PHONE, 0x001f },
+       { AC97_MIC, 0x001f },
+       { AC97_LINE, 0x1f1f },
+       { AC97_CD, 0x1f1f },
+       { AC97_VIDEO, 0x1f1f },
+       { AC97_AUX, 0x1f1f },
+       { AC97_PCM, 0x1f1f },
+       { AC97_REC_GAIN, 0x0f0f },
+       { } /* terminator */
+};
+
 /* initialize the ac97 into a known state */
 static void
 snd_nm256_ac97_reset(struct snd_ac97 *ac97)
@@ -1211,6 +1282,16 @@ snd_nm256_ac97_reset(struct snd_ac97 *ac97)
                snd_nm256_writeb(chip, 0x6cc, 0x80);
                snd_nm256_writeb(chip, 0x6cc, 0x0);
        }
+       if (! chip->in_resume) {
+               int i;
+               for (i = 0; i < ARRAY_SIZE(nm256_ac97_init_val); i++) {
+                       /* preload the cache, so as to avoid even a single
+                        * read of the mixer regs
+                        */
+                       snd_nm256_ac97_write(ac97, nm256_ac97_init_val[i].reg,
+                                            nm256_ac97_init_val[i].value);
+               }
+       }
 }
 
 /* create an ac97 mixer interface */
@@ -1219,32 +1300,25 @@ snd_nm256_mixer(struct nm256 *chip)
 {
        struct snd_ac97_bus *pbus;
        struct snd_ac97_template ac97;
-       int i, err;
+       int err;
        static struct snd_ac97_bus_ops ops = {
                .reset = snd_nm256_ac97_reset,
                .write = snd_nm256_ac97_write,
                .read = snd_nm256_ac97_read,
        };
-       /* looks like nm256 hangs up when unexpected registers are touched... */
-       static int mixer_regs[] = {
-               AC97_MASTER, AC97_HEADPHONE, AC97_MASTER_MONO,
-               AC97_PC_BEEP, AC97_PHONE, AC97_MIC, AC97_LINE, AC97_CD,
-               AC97_VIDEO, AC97_AUX, AC97_PCM, AC97_REC_SEL,
-               AC97_REC_GAIN, AC97_GENERAL_PURPOSE, AC97_3D_CONTROL,
-               /*AC97_EXTENDED_ID,*/
-               AC97_VENDOR_ID1, AC97_VENDOR_ID2,
-               -1
-       };
+
+       chip->ac97_regs = kcalloc(ARRAY_SIZE(nm256_ac97_init_val),
+                                 sizeof(short), GFP_KERNEL);
+       if (! chip->ac97_regs)
+               return -ENOMEM;
 
        if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
                return err;
 
        memset(&ac97, 0, sizeof(ac97));
        ac97.scaps = AC97_SCAP_AUDIO; /* we support audio! */
-       ac97.limited_regs = 1;
-       for (i = 0; mixer_regs[i] >= 0; i++)
-               set_bit(mixer_regs[i], ac97.reg_accessed);
        ac97.private_data = chip;
+       ac97.res_table = nm256_res_table;
        pbus->no_vra = 1;
        err = snd_ac97_mixer(pbus, &ac97, &chip->ac97);
        if (err < 0)
@@ -1319,6 +1393,7 @@ static int nm256_suspend(struct pci_dev *pci, pm_message_t state)
        chip->coeffs_current = 0;
        pci_disable_device(pci);
        pci_save_state(pci);
+       pci_set_power_state(pci, pci_choose_state(pci, state));
        return 0;
 }
 
@@ -1329,8 +1404,18 @@ static int nm256_resume(struct pci_dev *pci)
        int i;
 
        /* Perform a full reset on the hardware */
+       chip->in_resume = 1;
+
+       pci_set_power_state(pci, PCI_D0);
        pci_restore_state(pci);
-       pci_enable_device(pci);
+       if (pci_enable_device(pci) < 0) {
+               printk(KERN_ERR "nm256: pci_enable_device failed, "
+                      "disabling device\n");
+               snd_card_disconnect(card);
+               return -EIO;
+       }
+       pci_set_master(pci);
+
        snd_nm256_init_chip(chip);
 
        /* restore ac97 */
@@ -1346,6 +1431,7 @@ static int nm256_resume(struct pci_dev *pci)
        }
 
        snd_power_change_state(card, SNDRV_CTL_POWER_D0);
+       chip->in_resume = 0;
        return 0;
 }
 #endif /* CONFIG_PM */
@@ -1358,7 +1444,7 @@ static int snd_nm256_free(struct nm256 *chip)
                snd_nm256_capture_stop(chip);
 
        if (chip->irq >= 0)
-               synchronize_irq(chip->irq);
+               free_irq(chip->irq, chip);
 
        if (chip->cport)
                iounmap(chip->cport);
@@ -1366,10 +1452,9 @@ static int snd_nm256_free(struct nm256 *chip)
                iounmap(chip->buffer);
        release_and_free_resource(chip->res_cport);
        release_and_free_resource(chip->res_buffer);
-       if (chip->irq >= 0)
-               free_irq(chip->irq, chip);
 
        pci_disable_device(chip->pci);
+       kfree(chip->ac97_regs);
        kfree(chip);
        return 0;
 }
@@ -1407,7 +1492,7 @@ snd_nm256_create(struct snd_card *card, struct pci_dev *pci,
        chip->use_cache = use_cache;
        spin_lock_init(&chip->reg_lock);
        chip->irq = -1;
-       init_MUTEX(&chip->irq_mutex);
+       mutex_init(&chip->irq_mutex);
 
        /* store buffer sizes in bytes */
        chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize = playback_bufsize * 1024;
@@ -1449,7 +1534,8 @@ snd_nm256_create(struct snd_card *card, struct pci_dev *pci,
                                printk(KERN_ERR "  force the driver to load by "
                                       "passing in the module parameter\n");
                                printk(KERN_ERR "    force_ac97=1\n");
-                               printk(KERN_ERR "  or try sb16 or cs423x drivers instead.\n");
+                               printk(KERN_ERR "  or try sb16, opl3sa2, or "
+                                      "cs423x drivers instead.\n");
                                err = -ENXIO;
                                goto __error;
                        }
@@ -1544,23 +1630,15 @@ __error:
 }
 
 
-struct nm256_quirk {
-       unsigned short vendor;
-       unsigned short device;
-       int type;
-};
-
 enum { NM_BLACKLISTED, NM_RESET_WORKAROUND, NM_RESET_WORKAROUND_2 };
 
-static struct nm256_quirk nm256_quirks[] __devinitdata = {
+static struct snd_pci_quirk nm256_quirks[] __devinitdata = {
        /* HP omnibook 4150 has cs4232 codec internally */
-       { .vendor = 0x103c, .device = 0x0007, .type = NM_BLACKLISTED },
-       /* Sony PCG-F305 */
-       { .vendor = 0x104d, .device = 0x8041, .type = NM_RESET_WORKAROUND },
-       /* Dell Latitude LS */
-       { .vendor = 0x1028, .device = 0x0080, .type = NM_RESET_WORKAROUND },
-       /* Dell Latitude CSx */
-       { .vendor = 0x1028, .device = 0x0091, .type = NM_RESET_WORKAROUND_2 },
+       SND_PCI_QUIRK(0x103c, 0x0007, "HP omnibook 4150", NM_BLACKLISTED),
+       /* Reset workarounds to avoid lock-ups */
+       SND_PCI_QUIRK(0x104d, 0x8041, "Sony PCG-F305", NM_RESET_WORKAROUND),
+       SND_PCI_QUIRK(0x1028, 0x0080, "Dell Latitude LS", NM_RESET_WORKAROUND),
+       SND_PCI_QUIRK(0x1028, 0x0091, "Dell Latitude CSx", NM_RESET_WORKAROUND_2),
        { } /* terminator */
 };
 
@@ -1571,32 +1649,28 @@ static int __devinit snd_nm256_probe(struct pci_dev *pci,
        struct snd_card *card;
        struct nm256 *chip;
        int err;
-       struct nm256_quirk *q;
-       u16 subsystem_vendor, subsystem_device;
-
-       pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
-       pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
-
-       for (q = nm256_quirks; q->vendor; q++) {
-               if (q->vendor == subsystem_vendor && q->device == subsystem_device) {
-                       switch (q->type) {
-                       case NM_BLACKLISTED:
-                               printk(KERN_INFO "nm256: The device is blacklisted. "
-                                      "Loading stopped\n");
-                               return -ENODEV;
-                       case NM_RESET_WORKAROUND_2:
-                               reset_workaround_2 = 1;
-                               /* Fall-through */
-                       case NM_RESET_WORKAROUND:
-                               reset_workaround = 1;
-                               break;
-                       }
+       const struct snd_pci_quirk *q;
+
+       q = snd_pci_quirk_lookup(pci, nm256_quirks);
+       if (q) {
+               snd_printdd(KERN_INFO "nm256: Enabled quirk for %s.\n", q->name);
+               switch (q->value) {
+               case NM_BLACKLISTED:
+                       printk(KERN_INFO "nm256: The device is blacklisted. "
+                              "Loading stopped\n");
+                       return -ENODEV;
+               case NM_RESET_WORKAROUND_2:
+                       reset_workaround_2 = 1;
+                       /* Fall-through */
+               case NM_RESET_WORKAROUND:
+                       reset_workaround = 1;
+                       break;
                }
        }
 
-       card = snd_card_new(index, id, THIS_MODULE, 0);
-       if (card == NULL)
-               return -ENOMEM;
+       err = snd_card_create(index, id, THIS_MODULE, 0, &card);
+       if (err < 0)
+               return err;
 
        switch (pci->device) {
        case PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO: