Merge branch 'topic/hda-optimize' into topic/hda
[safe/jmp/linux-2.6] / sound / pci / es1938.c
index 577877d..4cd9a1f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  Driver for ESS Solo-1 (ES1938, ES1946, ES1969) soundcard
  *  Copyright (c) by Jaromir Koutek <miri@punknet.cz>,
- *                   Jaroslav Kysela <perex@suse.cz>,
+ *                   Jaroslav Kysela <perex@perex.cz>,
  *                   Thomas Sailer <sailer@ife.ee.ethz.ch>,
  *                   Abramo Bagnara <abramo@alsa-project.org>,
  *                   Markus Gruber <gruber@eikon.tum.de>
@@ -47,7 +47,6 @@
 */
 
 
-#include <sound/driver.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
 #include <linux/pci.h>
 #include <linux/gameport.h>
 #include <linux/moduleparam.h>
 #include <linux/delay.h>
+#include <linux/dma-mapping.h>
 #include <sound/core.h>
 #include <sound/control.h>
 #include <sound/pcm.h>
 #include <sound/opl3.h>
 #include <sound/mpu401.h>
 #include <sound/initval.h>
+#include <sound/tlv.h>
 
 #include <asm/io.h>
 
@@ -225,6 +226,7 @@ struct es1938 {
        unsigned int dma2_start;
        unsigned int dma1_shift;
        unsigned int dma2_shift;
+       unsigned int last_capture_dmaaddr;
        unsigned int active;
 
        spinlock_t reg_lock;
@@ -239,7 +241,7 @@ struct es1938 {
 #endif
 };
 
-static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id, struct pt_regs *regs);
+static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id);
 
 static struct pci_device_id snd_es1938_ids[] = {
         { 0x125d, 0x1969, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* Solo-1 */
@@ -527,6 +529,7 @@ static void snd_es1938_capture_setdma(struct es1938 *chip)
        outb(1, SLDM_REG(chip, DMAMASK));
        outb(0x14, SLDM_REG(chip, DMAMODE));
        outl(chip->dma1_start, SLDM_REG(chip, DMAADDR));
+       chip->last_capture_dmaaddr = chip->dma1_start;
        outw(chip->dma1_size - 1, SLDM_REG(chip, DMACOUNT));
        /* 3. Unmask DMA */
        outb(0, SLDM_REG(chip, DMAMASK));
@@ -768,19 +771,40 @@ static int snd_es1938_playback_prepare(struct snd_pcm_substream *substream)
        return -EINVAL;
 }
 
+/* during the incrementing of dma counters the DMA register reads sometimes
+   returns garbage. To ensure a valid hw pointer, the following checks which
+   should be very unlikely to fail are used:
+   - is the current DMA address in the valid DMA range ?
+   - is the sum of DMA address and DMA counter pointing to the last DMA byte ?
+   One can argue this could differ by one byte depending on which register is
+   updated first, so the implementation below allows for that.
+*/
 static snd_pcm_uframes_t snd_es1938_capture_pointer(struct snd_pcm_substream *substream)
 {
        struct es1938 *chip = snd_pcm_substream_chip(substream);
        size_t ptr;
+#if 0
        size_t old, new;
-#if 1
        /* This stuff is *needed*, don't ask why - AB */
        old = inw(SLDM_REG(chip, DMACOUNT));
        while ((new = inw(SLDM_REG(chip, DMACOUNT))) != old)
                old = new;
        ptr = chip->dma1_size - 1 - new;
 #else
-       ptr = inl(SLDM_REG(chip, DMAADDR)) - chip->dma1_start;
+       size_t count;
+       unsigned int diff;
+
+       ptr = inl(SLDM_REG(chip, DMAADDR));
+       count = inw(SLDM_REG(chip, DMACOUNT));
+       diff = chip->dma1_start + chip->dma1_size - ptr - count;
+
+       if (diff > 3 || ptr < chip->dma1_start
+             || ptr >= chip->dma1_start+chip->dma1_size)
+         ptr = chip->last_capture_dmaaddr;            /* bad, use last saved */
+       else
+         chip->last_capture_dmaaddr = ptr;            /* good, remember it */
+
+       ptr -= chip->dma1_start;
 #endif
        return ptr >> chip->dma1_shift;
 }
@@ -836,7 +860,8 @@ static int snd_es1938_capture_copy(struct snd_pcm_substream *substream,
        struct es1938 *chip = snd_pcm_substream_chip(substream);
        pos <<= chip->dma1_shift;
        count <<= chip->dma1_shift;
-       snd_assert(pos + count <= chip->dma1_size, return -EINVAL);
+       if (snd_BUG_ON(pos + count > chip->dma1_size))
+               return -EINVAL;
        if (pos + count < chip->dma1_size) {
                if (copy_to_user(dst, runtime->dma_area + pos + 1, count))
                        return -EFAULT;
@@ -1064,15 +1089,7 @@ static int snd_es1938_put_mux(struct snd_kcontrol *kcontrol,
        return snd_es1938_mixer_bits(chip, 0x1c, 0x07, val) != val;
 }
 
-static int snd_es1938_info_spatializer_enable(struct snd_kcontrol *kcontrol,
-                                             struct snd_ctl_elem_info *uinfo)
-{
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
-       uinfo->count = 1;
-       uinfo->value.integer.min = 0;
-       uinfo->value.integer.max = 1;
-       return 0;
-}
+#define snd_es1938_info_spatializer_enable     snd_ctl_boolean_mono_info
 
 static int snd_es1938_get_spatializer_enable(struct snd_kcontrol *kcontrol,
                                             struct snd_ctl_elem_value *ucontrol)
@@ -1118,15 +1135,7 @@ static int snd_es1938_get_hw_volume(struct snd_kcontrol *kcontrol,
        return 0;
 }
 
-static int snd_es1938_info_hw_switch(struct snd_kcontrol *kcontrol,
-                                    struct snd_ctl_elem_info *uinfo)
-{
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
-       uinfo->count = 2;
-       uinfo->value.integer.min = 0;
-       uinfo->value.integer.max = 1;
-       return 0;
-}
+#define snd_es1938_info_hw_switch              snd_ctl_boolean_stereo_info
 
 static int snd_es1938_get_hw_switch(struct snd_kcontrol *kcontrol,
                                    struct snd_ctl_elem_value *ucontrol)
@@ -1163,6 +1172,14 @@ static int snd_es1938_reg_read(struct es1938 *chip, unsigned char reg)
                return snd_es1938_read(chip, reg);
 }
 
+#define ES1938_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv)    \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
+  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,\
+  .name = xname, .index = xindex, \
+  .info = snd_es1938_info_single, \
+  .get = snd_es1938_get_single, .put = snd_es1938_put_single, \
+  .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
+  .tlv = { .p = xtlv } }
 #define ES1938_SINGLE(xname, xindex, reg, shift, mask, invert) \
 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
   .info = snd_es1938_info_single, \
@@ -1216,6 +1233,14 @@ static int snd_es1938_put_single(struct snd_kcontrol *kcontrol,
        return snd_es1938_reg_bits(chip, reg, mask, val) != val;
 }
 
+#define ES1938_DOUBLE_TLV(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert, xtlv) \
+{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
+  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,\
+  .name = xname, .index = xindex, \
+  .info = snd_es1938_info_double, \
+  .get = snd_es1938_get_double, .put = snd_es1938_put_double, \
+  .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22), \
+  .tlv = { .p = xtlv } }
 #define ES1938_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
   .info = snd_es1938_info_double, \
@@ -1296,8 +1321,41 @@ static int snd_es1938_put_double(struct snd_kcontrol *kcontrol,
        return change;
 }
 
+static unsigned int db_scale_master[] = {
+       TLV_DB_RANGE_HEAD(2),
+       0, 54, TLV_DB_SCALE_ITEM(-3600, 50, 1),
+       54, 63, TLV_DB_SCALE_ITEM(-900, 100, 0),
+};
+
+static unsigned int db_scale_audio1[] = {
+       TLV_DB_RANGE_HEAD(2),
+       0, 8, TLV_DB_SCALE_ITEM(-3300, 300, 1),
+       8, 15, TLV_DB_SCALE_ITEM(-900, 150, 0),
+};
+
+static unsigned int db_scale_audio2[] = {
+       TLV_DB_RANGE_HEAD(2),
+       0, 8, TLV_DB_SCALE_ITEM(-3450, 300, 1),
+       8, 15, TLV_DB_SCALE_ITEM(-1050, 150, 0),
+};
+
+static unsigned int db_scale_mic[] = {
+       TLV_DB_RANGE_HEAD(2),
+       0, 8, TLV_DB_SCALE_ITEM(-2400, 300, 1),
+       8, 15, TLV_DB_SCALE_ITEM(0, 150, 0),
+};
+
+static unsigned int db_scale_line[] = {
+       TLV_DB_RANGE_HEAD(2),
+       0, 8, TLV_DB_SCALE_ITEM(-3150, 300, 1),
+       8, 15, TLV_DB_SCALE_ITEM(-750, 150, 0),
+};
+
+static const DECLARE_TLV_DB_SCALE(db_scale_capture, 0, 150, 0);
+
 static struct snd_kcontrol_new snd_es1938_controls[] = {
-ES1938_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
+ES1938_DOUBLE_TLV("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0,
+                 db_scale_master),
 ES1938_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
 {
        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
@@ -1308,19 +1366,27 @@ ES1938_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
 },
 {
        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+       .access = (SNDRV_CTL_ELEM_ACCESS_READ |
+                  SNDRV_CTL_ELEM_ACCESS_TLV_READ),
        .name = "Hardware Master Playback Switch",
-       .access = SNDRV_CTL_ELEM_ACCESS_READ,
        .info = snd_es1938_info_hw_switch,
        .get = snd_es1938_get_hw_switch,
+       .tlv = { .p = db_scale_master },
 },
 ES1938_SINGLE("Hardware Volume Split", 0, 0x64, 7, 1, 0),
-ES1938_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
+ES1938_DOUBLE_TLV("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0,
+                 db_scale_line),
 ES1938_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
-ES1938_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
-ES1938_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
-ES1938_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
-ES1938_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
-ES1938_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
+ES1938_DOUBLE_TLV("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0,
+                 db_scale_mic),
+ES1938_DOUBLE_TLV("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0,
+                 db_scale_line),
+ES1938_DOUBLE_TLV("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0,
+                 db_scale_mic),
+ES1938_DOUBLE_TLV("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0,
+                 db_scale_line),
+ES1938_DOUBLE_TLV("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0,
+                 db_scale_capture),
 ES1938_SINGLE("PC Speaker Volume", 0, 0x3c, 0, 7, 0),
 ES1938_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
 ES1938_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
@@ -1331,16 +1397,26 @@ ES1938_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
        .get = snd_es1938_get_mux,
        .put = snd_es1938_put_mux,
 },
-ES1938_DOUBLE("Mono Input Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
-ES1938_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
-ES1938_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
-ES1938_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
-ES1938_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
-ES1938_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0),
-ES1938_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
-ES1938_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0),
-ES1938_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
-ES1938_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0),
+ES1938_DOUBLE_TLV("Mono Input Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0,
+                 db_scale_line),
+ES1938_DOUBLE_TLV("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0,
+                 db_scale_audio2),
+ES1938_DOUBLE_TLV("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0,
+                 db_scale_mic),
+ES1938_DOUBLE_TLV("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0,
+                 db_scale_line),
+ES1938_DOUBLE_TLV("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0,
+                 db_scale_mic),
+ES1938_DOUBLE_TLV("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0,
+                 db_scale_line),
+ES1938_DOUBLE_TLV("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0,
+                 db_scale_line),
+ES1938_DOUBLE_TLV("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0,
+                 db_scale_line),
+ES1938_DOUBLE_TLV("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0,
+                 db_scale_audio2),
+ES1938_DOUBLE_TLV("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0,
+                 db_scale_audio1),
 ES1938_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
 {
        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
@@ -1398,11 +1474,13 @@ static unsigned char saved_regs[SAVED_REG_SIZE+1] = {
 };
 
 
-static int es1938_suspend(struct snd_card *card, pm_message_t state)
+static int es1938_suspend(struct pci_dev *pci, pm_message_t state)
 {
-       struct es1938 *chip = card->pm_private_data;
+       struct snd_card *card = pci_get_drvdata(pci);
+       struct es1938 *chip = card->private_data;
        unsigned char *s, *d;
 
+       snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
        snd_pcm_suspend_all(chip->pcm);
 
        /* save mixer-related registers */
@@ -1410,21 +1488,39 @@ static int es1938_suspend(struct snd_card *card, pm_message_t state)
                *d = snd_es1938_reg_read(chip, *s);
 
        outb(0x00, SLIO_REG(chip, IRQCONTROL)); /* disable irqs */
-       if (chip->irq >= 0)
-               free_irq(chip->irq, chip);  
-       pci_disable_device(chip->pci);
+       if (chip->irq >= 0) {
+               free_irq(chip->irq, chip);
+               chip->irq = -1;
+       }
+       pci_disable_device(pci);
+       pci_save_state(pci);
+       pci_set_power_state(pci, pci_choose_state(pci, state));
        return 0;
 }
 
-static int es1938_resume(struct snd_card *card)
+static int es1938_resume(struct pci_dev *pci)
 {
-       struct es1938 *chip = card->pm_private_data;
+       struct snd_card *card = pci_get_drvdata(pci);
+       struct es1938 *chip = card->private_data;
        unsigned char *s, *d;
 
-       pci_enable_device(chip->pci);
-       request_irq(chip->pci->irq, snd_es1938_interrupt,
-                   SA_INTERRUPT|SA_SHIRQ, "ES1938", chip);
-       chip->irq = chip->pci->irq;
+       pci_set_power_state(pci, PCI_D0);
+       pci_restore_state(pci);
+       if (pci_enable_device(pci) < 0) {
+               printk(KERN_ERR "es1938: pci_enable_device failed, "
+                      "disabling device\n");
+               snd_card_disconnect(card);
+               return -EIO;
+       }
+
+       if (request_irq(pci->irq, snd_es1938_interrupt,
+                       IRQF_SHARED, "ES1938", chip)) {
+               printk(KERN_ERR "es1938: unable to grab IRQ %d, "
+                      "disabling device\n", pci->irq);
+               snd_card_disconnect(card);
+               return -EIO;
+       }
+       chip->irq = pci->irq;
        snd_es1938_chip_init(chip);
 
        /* restore mixer-related registers */
@@ -1435,6 +1531,7 @@ static int es1938_resume(struct snd_card *card)
                        snd_es1938_write(chip, *s, *d);
        }
 
+       snd_power_change_state(card, SNDRV_CTL_POWER_D0);
        return 0;
 }
 #endif /* CONFIG_PM */
@@ -1511,8 +1608,8 @@ static int __devinit snd_es1938_create(struct snd_card *card,
        if ((err = pci_enable_device(pci)) < 0)
                return err;
         /* check, if we can restrict PCI DMA transfers to 24 bits */
-       if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
-           pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
+       if (pci_set_dma_mask(pci, DMA_24BIT_MASK) < 0 ||
+           pci_set_consistent_dma_mask(pci, DMA_24BIT_MASK) < 0) {
                snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
                pci_disable_device(pci);
                 return -ENXIO;
@@ -1527,6 +1624,7 @@ static int __devinit snd_es1938_create(struct snd_card *card,
        spin_lock_init(&chip->mixer_lock);
        chip->card = card;
        chip->pci = pci;
+       chip->irq = -1;
        if ((err = pci_request_regions(pci, "ESS Solo-1")) < 0) {
                kfree(chip);
                pci_disable_device(pci);
@@ -1537,7 +1635,7 @@ static int __devinit snd_es1938_create(struct snd_card *card,
        chip->vc_port = pci_resource_start(pci, 2);
        chip->mpu_port = pci_resource_start(pci, 3);
        chip->game_port = pci_resource_start(pci, 4);
-       if (request_irq(pci->irq, snd_es1938_interrupt, SA_INTERRUPT|SA_SHIRQ,
+       if (request_irq(pci->irq, snd_es1938_interrupt, IRQF_SHARED,
                        "ES1938", chip)) {
                snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
                snd_es1938_free(chip);
@@ -1553,8 +1651,6 @@ static int __devinit snd_es1938_create(struct snd_card *card,
 
        snd_es1938_chip_init(chip);
 
-       snd_card_set_pm_callback(card, es1938_suspend, es1938_resume, chip);
-
        if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
                snd_es1938_free(chip);
                return err;
@@ -1569,7 +1665,7 @@ static int __devinit snd_es1938_create(struct snd_card *card,
 /* --------------------------------------------------------------------
  * Interrupt handler
  * -------------------------------------------------------------------- */
-static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id)
 {
        struct es1938 *chip = dev_id;
        unsigned char status, audiostatus;
@@ -1641,7 +1737,7 @@ static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id, struct pt_regs *r
                // snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0); /* ack? */
                if (chip->rmidi) {
                        handled = 1;
-                       snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
+                       snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
                }
        }
        return IRQ_RETVAL(handled);
@@ -1717,6 +1813,7 @@ static int __devinit snd_es1938_probe(struct pci_dev *pci,
                snd_card_free(card);
                return err;
        }
+       card->private_data = chip;
 
        strcpy(card->driver, "ES1938");
        strcpy(card->shortname, "ESS ES1938 (Solo-1)");
@@ -1750,7 +1847,8 @@ static int __devinit snd_es1938_probe(struct pci_dev *pci,
                }
        }
        if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
-                               chip->mpu_port, 1, chip->irq, 0, &chip->rmidi) < 0) {
+                               chip->mpu_port, MPU401_INFO_INTEGRATED,
+                               chip->irq, 0, &chip->rmidi) < 0) {
                printk(KERN_ERR "es1938: unable to initialize MPU-401\n");
        } else {
                // this line is vital for MIDI interrupt handling on ess-solo1
@@ -1781,7 +1879,10 @@ static struct pci_driver driver = {
        .id_table = snd_es1938_ids,
        .probe = snd_es1938_probe,
        .remove = __devexit_p(snd_es1938_remove),
-       SND_PCI_PM_CALLBACKS
+#ifdef CONFIG_PM
+       .suspend = es1938_suspend,
+       .resume = es1938_resume,
+#endif
 };
 
 static int __init alsa_card_es1938_init(void)