Merge branch 'x86/for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[pandora-kernel.git] / sound / pci / oxygen / oxygen_io.c
index 5f4feea..83f135f 100644 (file)
@@ -17,7 +17,6 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */
 
-#include <sound/driver.h>
 #include <linux/delay.h>
 #include <linux/sched.h>
 #include <sound/core.h>
@@ -45,18 +44,21 @@ EXPORT_SYMBOL(oxygen_read32);
 void oxygen_write8(struct oxygen *chip, unsigned int reg, u8 value)
 {
        outb(value, chip->addr + reg);
+       chip->saved_registers._8[reg] = value;
 }
 EXPORT_SYMBOL(oxygen_write8);
 
 void oxygen_write16(struct oxygen *chip, unsigned int reg, u16 value)
 {
        outw(value, chip->addr + reg);
+       chip->saved_registers._16[reg / 2] = cpu_to_le16(value);
 }
 EXPORT_SYMBOL(oxygen_write16);
 
 void oxygen_write32(struct oxygen *chip, unsigned int reg, u32 value)
 {
        outl(value, chip->addr + reg);
+       chip->saved_registers._32[reg / 4] = cpu_to_le32(value);
 }
 EXPORT_SYMBOL(oxygen_write32);
 
@@ -64,7 +66,10 @@ void oxygen_write8_masked(struct oxygen *chip, unsigned int reg,
                          u8 value, u8 mask)
 {
        u8 tmp = inb(chip->addr + reg);
-       outb((tmp & ~mask) | (value & mask), chip->addr + reg);
+       tmp &= ~mask;
+       tmp |= value & mask;
+       outb(tmp, chip->addr + reg);
+       chip->saved_registers._8[reg] = tmp;
 }
 EXPORT_SYMBOL(oxygen_write8_masked);
 
@@ -72,7 +77,10 @@ void oxygen_write16_masked(struct oxygen *chip, unsigned int reg,
                           u16 value, u16 mask)
 {
        u16 tmp = inw(chip->addr + reg);
-       outw((tmp & ~mask) | (value & mask), chip->addr + reg);
+       tmp &= ~mask;
+       tmp |= value & mask;
+       outw(tmp, chip->addr + reg);
+       chip->saved_registers._16[reg / 2] = cpu_to_le16(tmp);
 }
 EXPORT_SYMBOL(oxygen_write16_masked);
 
@@ -80,20 +88,31 @@ void oxygen_write32_masked(struct oxygen *chip, unsigned int reg,
                           u32 value, u32 mask)
 {
        u32 tmp = inl(chip->addr + reg);
-       outl((tmp & ~mask) | (value & mask), chip->addr + reg);
+       tmp &= ~mask;
+       tmp |= value & mask;
+       outl(tmp, chip->addr + reg);
+       chip->saved_registers._32[reg / 4] = cpu_to_le32(tmp);
 }
 EXPORT_SYMBOL(oxygen_write32_masked);
 
 static int oxygen_ac97_wait(struct oxygen *chip, unsigned int mask)
 {
-       unsigned long timeout = jiffies + msecs_to_jiffies(1);
-       do {
-               udelay(5);
-               cond_resched();
-               if (oxygen_read8(chip, OXYGEN_AC97_INTERRUPT_STATUS) & mask)
-                       return 0;
-       } while (time_after_eq(timeout, jiffies));
-       return -EIO;
+       u8 status = 0;
+
+       /*
+        * Reading the status register also clears the bits, so we have to save
+        * the read bits in status.
+        */
+       wait_event_timeout(chip->ac97_waitqueue,
+                          ({ status |= oxygen_read8(chip, OXYGEN_AC97_INTERRUPT_STATUS);
+                             status & mask; }),
+                          msecs_to_jiffies(1) + 1);
+       /*
+        * Check even after a timeout because this function should not require
+        * the AC'97 interrupt to be enabled.
+        */
+       status |= oxygen_read8(chip, OXYGEN_AC97_INTERRUPT_STATUS);
+       return status & mask ? 0 : -EIO;
 }
 
 /*
@@ -120,9 +139,11 @@ void oxygen_write_ac97(struct oxygen *chip, unsigned int codec,
                udelay(5);
                oxygen_write32(chip, OXYGEN_AC97_REGS, reg);
                /* require two "completed" writes, just to be sure */
-               if (oxygen_ac97_wait(chip, OXYGEN_AC97_WRITE_COMPLETE) >= 0 &&
-                   ++succeeded >= 2)
+               if (oxygen_ac97_wait(chip, OXYGEN_AC97_INT_WRITE_DONE) >= 0 &&
+                   ++succeeded >= 2) {
+                       chip->saved_ac97_registers[codec][index / 2] = data;
                        return;
+               }
        }
        snd_printk(KERN_ERR "AC'97 write timeout\n");
 }
@@ -142,7 +163,7 @@ u16 oxygen_read_ac97(struct oxygen *chip, unsigned int codec,
                udelay(5);
                oxygen_write32(chip, OXYGEN_AC97_REGS, reg);
                udelay(10);
-               if (oxygen_ac97_wait(chip, OXYGEN_AC97_READ_COMPLETE) >= 0) {
+               if (oxygen_ac97_wait(chip, OXYGEN_AC97_INT_READ_DONE) >= 0) {
                        u16 value = oxygen_read16(chip, OXYGEN_AC97_REGS);
                        /* we require two consecutive reads of the same value */
                        if (value == last_read)
@@ -175,7 +196,7 @@ void oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data)
 {
        unsigned int count;
 
-       /* should not need more than 3.84 us (24 * 160 ns) */
+       /* should not need more than 7.68 us (24 * 320 ns) */
        count = 10;
        while ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) & OXYGEN_SPI_BUSY)
               && count > 0) {
@@ -183,12 +204,31 @@ void oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data)
                --count;
        }
 
-       spin_lock_irq(&chip->reg_lock);
        oxygen_write8(chip, OXYGEN_SPI_DATA1, data);
        oxygen_write8(chip, OXYGEN_SPI_DATA2, data >> 8);
        if (control & OXYGEN_SPI_DATA_LENGTH_3)
                oxygen_write8(chip, OXYGEN_SPI_DATA3, data >> 16);
        oxygen_write8(chip, OXYGEN_SPI_CONTROL, control);
-       spin_unlock_irq(&chip->reg_lock);
 }
 EXPORT_SYMBOL(oxygen_write_spi);
+
+void oxygen_write_i2c(struct oxygen *chip, u8 device, u8 map, u8 data)
+{
+       unsigned long timeout;
+
+       /* should not need more than about 300 us */
+       timeout = jiffies + msecs_to_jiffies(1);
+       do {
+               if (!(oxygen_read16(chip, OXYGEN_2WIRE_BUS_STATUS)
+                     & OXYGEN_2WIRE_BUSY))
+                       break;
+               udelay(1);
+               cond_resched();
+       } while (time_after_eq(timeout, jiffies));
+
+       oxygen_write8(chip, OXYGEN_2WIRE_MAP, map);
+       oxygen_write8(chip, OXYGEN_2WIRE_DATA, data);
+       oxygen_write8(chip, OXYGEN_2WIRE_CONTROL,
+                     device | OXYGEN_2WIRE_DIR_WRITE);
+}
+EXPORT_SYMBOL(oxygen_write_i2c);