ALSA: HDA: add powersaving hook for Realtek
[safe/jmp/linux-2.6] / sound / sparc / cs4231.c
index 96051dc..8d13d93 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Driver for CS4231 sound chips found on Sparcs.
- * Copyright (C) 2002 David S. Miller <davem@redhat.com>
+ * Copyright (C) 2002, 2008 David S. Miller <davem@davemloft.net>
  *
  * Based entirely upon drivers/sbus/audio/cs4231.c which is:
  * Copyright (C) 1996, 1997, 1998 Derrick J Brashear (shadow@andrew.cmu.edu)
@@ -17,9 +17,9 @@
 #include <linux/moduleparam.h>
 #include <linux/irq.h>
 #include <linux/io.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
 
-
-#include <sound/driver.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
 #include <sound/info.h>
 
 #ifdef CONFIG_SBUS
 #define SBUS_SUPPORT
-#include <asm/sbus.h>
 #endif
 
 #if defined(CONFIG_PCI) && defined(CONFIG_SPARC64)
 #define EBUS_SUPPORT
 #include <linux/pci.h>
-#include <asm/ebus.h>
+#include <asm/ebus_dma.h>
 #endif
 
 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;     /* Index 0-MAX */
@@ -71,8 +70,6 @@ struct cs4231_dma_control {
        int             (*request)(struct cs4231_dma_control *dma_cont,
                                   dma_addr_t bus_addr, size_t len);
        unsigned int    (*address)(struct cs4231_dma_control *dma_cont);
-       void            (*preallocate)(struct snd_cs4231 *chip,
-                                      struct snd_pcm *pcm);
 #ifdef EBUS_SUPPORT
        struct          ebus_dma_info   ebus_info;
 #endif
@@ -115,149 +112,21 @@ struct snd_cs4231 {
        struct mutex            mce_mutex;      /* mutex for mce register */
        struct mutex            open_mutex;     /* mutex for ALSA open/close */
 
-       union {
-#ifdef SBUS_SUPPORT
-               struct sbus_dev         *sdev;
-#endif
-#ifdef EBUS_SUPPORT
-               struct pci_dev          *pdev;
-#endif
-       } dev_u;
+       struct of_device        *op;
        unsigned int            irq[2];
        unsigned int            regs_size;
        struct snd_cs4231       *next;
 };
 
-static struct snd_cs4231 *cs4231_list;
-
 /* Eventually we can use sound/isa/cs423x/cs4231_lib.c directly, but for
  * now....  -DaveM
  */
 
 /* IO ports */
-
-#define CS4231P(chip, x)       ((chip)->port + c_d_c_CS4231##x)
+#include <sound/cs4231-regs.h>
 
 /* XXX offsets are different than PC ISA chips... */
-#define c_d_c_CS4231REGSEL     0x0
-#define c_d_c_CS4231REG                0x4
-#define c_d_c_CS4231STATUS     0x8
-#define c_d_c_CS4231PIO                0xc
-
-/* codec registers */
-
-#define CS4231_LEFT_INPUT      0x00    /* left input control */
-#define CS4231_RIGHT_INPUT     0x01    /* right input control */
-#define CS4231_AUX1_LEFT_INPUT 0x02    /* left AUX1 input control */
-#define CS4231_AUX1_RIGHT_INPUT        0x03    /* right AUX1 input control */
-#define CS4231_AUX2_LEFT_INPUT 0x04    /* left AUX2 input control */
-#define CS4231_AUX2_RIGHT_INPUT        0x05    /* right AUX2 input control */
-#define CS4231_LEFT_OUTPUT     0x06    /* left output control register */
-#define CS4231_RIGHT_OUTPUT    0x07    /* right output control register */
-#define CS4231_PLAYBK_FORMAT   0x08    /* clock and data format - playback - bits 7-0 MCE */
-#define CS4231_IFACE_CTRL      0x09    /* interface control - bits 7-2 MCE */
-#define CS4231_PIN_CTRL                0x0a    /* pin control */
-#define CS4231_TEST_INIT       0x0b    /* test and initialization */
-#define CS4231_MISC_INFO       0x0c    /* miscellaneaous information */
-#define CS4231_LOOPBACK                0x0d    /* loopback control */
-#define CS4231_PLY_UPR_CNT     0x0e    /* playback upper base count */
-#define CS4231_PLY_LWR_CNT     0x0f    /* playback lower base count */
-#define CS4231_ALT_FEATURE_1   0x10    /* alternate #1 feature enable */
-#define CS4231_ALT_FEATURE_2   0x11    /* alternate #2 feature enable */
-#define CS4231_LEFT_LINE_IN    0x12    /* left line input control */
-#define CS4231_RIGHT_LINE_IN   0x13    /* right line input control */
-#define CS4231_TIMER_LOW       0x14    /* timer low byte */
-#define CS4231_TIMER_HIGH      0x15    /* timer high byte */
-#define CS4231_LEFT_MIC_INPUT  0x16    /* left MIC input control register (InterWave only) */
-#define CS4231_RIGHT_MIC_INPUT 0x17    /* right MIC input control register (InterWave only) */
-#define CS4236_EXT_REG         0x17    /* extended register access */
-#define CS4231_IRQ_STATUS      0x18    /* irq status register */
-#define CS4231_LINE_LEFT_OUTPUT        0x19    /* left line output control register (InterWave only) */
-#define CS4231_VERSION         0x19    /* CS4231(A) - version values */
-#define CS4231_MONO_CTRL       0x1a    /* mono input/output control */
-#define CS4231_LINE_RIGHT_OUTPUT 0x1b  /* right line output control register (InterWave only) */
-#define CS4235_LEFT_MASTER     0x1b    /* left master output control */
-#define CS4231_REC_FORMAT      0x1c    /* clock and data format - record - bits 7-0 MCE */
-#define CS4231_PLY_VAR_FREQ    0x1d    /* playback variable frequency */
-#define CS4235_RIGHT_MASTER    0x1d    /* right master output control */
-#define CS4231_REC_UPR_CNT     0x1e    /* record upper count */
-#define CS4231_REC_LWR_CNT     0x1f    /* record lower count */
-
-/* definitions for codec register select port - CODECP( REGSEL ) */
-
-#define CS4231_INIT            0x80    /* CODEC is initializing */
-#define CS4231_MCE             0x40    /* mode change enable */
-#define CS4231_TRD             0x20    /* transfer request disable */
-
-/* definitions for codec status register - CODECP( STATUS ) */
-
-#define CS4231_GLOBALIRQ       0x01    /* IRQ is active */
-
-/* definitions for codec irq status - CS4231_IRQ_STATUS        */
-
-#define CS4231_PLAYBACK_IRQ    0x10
-#define CS4231_RECORD_IRQ      0x20
-#define CS4231_TIMER_IRQ       0x40
-#define CS4231_ALL_IRQS                0x70
-#define CS4231_REC_UNDERRUN    0x08
-#define CS4231_REC_OVERRUN     0x04
-#define CS4231_PLY_OVERRUN     0x02
-#define CS4231_PLY_UNDERRUN    0x01
-
-/* definitions for CS4231_LEFT_INPUT and CS4231_RIGHT_INPUT registers */
-
-#define CS4231_ENABLE_MIC_GAIN 0x20
-
-#define CS4231_MIXS_LINE       0x00
-#define CS4231_MIXS_AUX1       0x40
-#define CS4231_MIXS_MIC                0x80
-#define CS4231_MIXS_ALL                0xc0
-
-/* definitions for clock and data format register - CS4231_PLAYBK_FORMAT */
-
-#define CS4231_LINEAR_8                0x00    /* 8-bit unsigned data */
-#define CS4231_ALAW_8          0x60    /* 8-bit A-law companded */
-#define CS4231_ULAW_8          0x20    /* 8-bit U-law companded */
-#define CS4231_LINEAR_16       0x40    /* 16-bit twos complement data - little endian */
-#define CS4231_LINEAR_16_BIG   0xc0    /* 16-bit twos complement data - big endian */
-#define CS4231_ADPCM_16                0xa0    /* 16-bit ADPCM */
-#define CS4231_STEREO          0x10    /* stereo mode */
-/* bits 3-1 define frequency divisor */
-#define CS4231_XTAL1           0x00    /* 24.576 crystal */
-#define CS4231_XTAL2           0x01    /* 16.9344 crystal */
-
-/* definitions for interface control register - CS4231_IFACE_CTRL */
-
-#define CS4231_RECORD_PIO      0x80    /* record PIO enable */
-#define CS4231_PLAYBACK_PIO    0x40    /* playback PIO enable */
-#define CS4231_CALIB_MODE      0x18    /* calibration mode bits */
-#define CS4231_AUTOCALIB       0x08    /* auto calibrate */
-#define CS4231_SINGLE_DMA      0x04    /* use single DMA channel */
-#define CS4231_RECORD_ENABLE   0x02    /* record enable */
-#define CS4231_PLAYBACK_ENABLE 0x01    /* playback enable */
-
-/* definitions for pin control register - CS4231_PIN_CTRL */
-
-#define CS4231_IRQ_ENABLE      0x02    /* enable IRQ */
-#define CS4231_XCTL1           0x40    /* external control #1 */
-#define CS4231_XCTL0           0x80    /* external control #0 */
-
-/* definitions for test and init register - CS4231_TEST_INIT */
-
-#define CS4231_CALIB_IN_PROGRESS 0x20  /* auto calibrate in progress */
-#define CS4231_DMA_REQUEST     0x10    /* DMA request in progress */
-
-/* definitions for misc control register - CS4231_MISC_INFO */
-
-#define CS4231_MODE2           0x40    /* MODE 2 */
-#define CS4231_IW_MODE3                0x6c    /* MODE 3 - InterWave enhanced mode */
-#define CS4231_4236_MODE3      0xe0    /* MODE 3 - CS4236+ enhanced mode */
-
-/* definitions for alternate feature 1 register - CS4231_ALT_FEATURE_1 */
-
-#define        CS4231_DACZ             0x01    /* zero DAC when underrun */
-#define CS4231_TIMER_ENABLE    0x40    /* codec timer enable */
-#define CS4231_OLB             0x80    /* output level bit */
+#define CS4231U(chip, x)       ((chip)->port + ((c_d_c_CS4231##x) << 2))
 
 /* SBUS DMA register defines.  */
 
@@ -387,27 +256,19 @@ static unsigned char snd_cs4231_original_image[32] =
 
 static u8 __cs4231_readb(struct snd_cs4231 *cp, void __iomem *reg_addr)
 {
-#ifdef EBUS_SUPPORT
        if (cp->flags & CS4231_FLAG_EBUS)
                return readb(reg_addr);
        else
-#endif
-#ifdef SBUS_SUPPORT
                return sbus_readb(reg_addr);
-#endif
 }
 
 static void __cs4231_writeb(struct snd_cs4231 *cp, u8 val,
                            void __iomem *reg_addr)
 {
-#ifdef EBUS_SUPPORT
        if (cp->flags & CS4231_FLAG_EBUS)
                return writeb(val, reg_addr);
        else
-#endif
-#ifdef SBUS_SUPPORT
                return sbus_writeb(val, reg_addr);
-#endif
 }
 
 /*
@@ -418,10 +279,12 @@ static void snd_cs4231_ready(struct snd_cs4231 *chip)
 {
        int timeout;
 
-       for (timeout = 250;
-            timeout > 0 && (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT);
-            timeout--)
-               udelay(100);
+       for (timeout = 250; timeout > 0; timeout--) {
+               int val = __cs4231_readb(chip, CS4231U(chip, REGSEL));
+               if ((val & CS4231_INIT) == 0)
+                       break;
+               udelay(100);
+       }
 }
 
 static void snd_cs4231_dout(struct snd_cs4231 *chip, unsigned char reg,
@@ -429,14 +292,14 @@ static void snd_cs4231_dout(struct snd_cs4231 *chip, unsigned char reg,
 {
        snd_cs4231_ready(chip);
 #ifdef CONFIG_SND_DEBUG
-       if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
+       if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
                snd_printdd("out: auto calibration time out - reg = 0x%x, "
                            "value = 0x%x\n",
                            reg, value);
 #endif
-       __cs4231_writeb(chip, chip->mce_bit | reg, CS4231P(chip, REGSEL));
+       __cs4231_writeb(chip, chip->mce_bit | reg, CS4231U(chip, REGSEL));
        wmb();
-       __cs4231_writeb(chip, value, CS4231P(chip, REG));
+       __cs4231_writeb(chip, value, CS4231U(chip, REG));
        mb();
 }
 
@@ -462,13 +325,13 @@ static unsigned char snd_cs4231_in(struct snd_cs4231 *chip, unsigned char reg)
 {
        snd_cs4231_ready(chip);
 #ifdef CONFIG_SND_DEBUG
-       if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
+       if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
                snd_printdd("in: auto calibration time out - reg = 0x%x\n",
                            reg);
 #endif
-       __cs4231_writeb(chip, chip->mce_bit | reg, CS4231P(chip, REGSEL));
+       __cs4231_writeb(chip, chip->mce_bit | reg, CS4231U(chip, REGSEL));
        mb();
-       return __cs4231_readb(chip, CS4231P(chip, REG));
+       return __cs4231_readb(chip, CS4231U(chip, REG));
 }
 
 /*
@@ -481,13 +344,15 @@ static void snd_cs4231_busy_wait(struct snd_cs4231 *chip)
 
        /* looks like this sequence is proper for CS4231A chip (GUS MAX) */
        for (timeout = 5; timeout > 0; timeout--)
-               __cs4231_readb(chip, CS4231P(chip, REGSEL));
+               __cs4231_readb(chip, CS4231U(chip, REGSEL));
 
        /* end of cleanup sequence */
-       for (timeout = 500;
-            timeout > 0 && (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT);
-            timeout--)
+       for (timeout = 500; timeout > 0; timeout--) {
+               int val = __cs4231_readb(chip, CS4231U(chip, REGSEL));
+               if ((val & CS4231_INIT) == 0)
+                       break;
                msleep(1);
+       }
 }
 
 static void snd_cs4231_mce_up(struct snd_cs4231 *chip)
@@ -498,83 +363,61 @@ static void snd_cs4231_mce_up(struct snd_cs4231 *chip)
        spin_lock_irqsave(&chip->lock, flags);
        snd_cs4231_ready(chip);
 #ifdef CONFIG_SND_DEBUG
-       if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
+       if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
                snd_printdd("mce_up - auto calibration time out (0)\n");
 #endif
        chip->mce_bit |= CS4231_MCE;
-       timeout = __cs4231_readb(chip, CS4231P(chip, REGSEL));
+       timeout = __cs4231_readb(chip, CS4231U(chip, REGSEL));
        if (timeout == 0x80)
                snd_printdd("mce_up [%p]: serious init problem - "
                            "codec still busy\n",
                            chip->port);
        if (!(timeout & CS4231_MCE))
-               __cs4231_writeb(chip, chip->mce_bit | (timeout & 0x1f), CS4231P(chip, REGSEL));
+               __cs4231_writeb(chip, chip->mce_bit | (timeout & 0x1f),
+                               CS4231U(chip, REGSEL));
        spin_unlock_irqrestore(&chip->lock, flags);
 }
 
 static void snd_cs4231_mce_down(struct snd_cs4231 *chip)
 {
-       unsigned long flags;
-       int timeout;
+       unsigned long flags, timeout;
+       int reg;
 
-       spin_lock_irqsave(&chip->lock, flags);
        snd_cs4231_busy_wait(chip);
+       spin_lock_irqsave(&chip->lock, flags);
 #ifdef CONFIG_SND_DEBUG
-       if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
-               snd_printdd("mce_down [%p] - auto calibration time out (0)\n", CS4231P(chip, REGSEL));
+       if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
+               snd_printdd("mce_down [%p] - auto calibration time out (0)\n",
+                           CS4231U(chip, REGSEL));
 #endif
        chip->mce_bit &= ~CS4231_MCE;
-       timeout = __cs4231_readb(chip, CS4231P(chip, REGSEL));
-       __cs4231_writeb(chip, chip->mce_bit | (timeout & 0x1f), CS4231P(chip, REGSEL));
-       if (timeout == 0x80)
-               snd_printdd("mce_down [%p]: serious init problem - "
-                           "codec still busy\n",
-                           chip->port);
-       if ((timeout & CS4231_MCE) == 0) {
+       reg = __cs4231_readb(chip, CS4231U(chip, REGSEL));
+       __cs4231_writeb(chip, chip->mce_bit | (reg & 0x1f),
+                       CS4231U(chip, REGSEL));
+       if (reg == 0x80)
+               snd_printdd("mce_down [%p]: serious init problem "
+                           "- codec still busy\n", chip->port);
+       if ((reg & CS4231_MCE) == 0) {
                spin_unlock_irqrestore(&chip->lock, flags);
                return;
        }
-       snd_cs4231_busy_wait(chip);
-
-       /* calibration process */
-
-       snd_cs4231_ready(chip);
-       snd_cs4231_ready(chip);
-       timeout = snd_cs4231_in(chip, CS4231_TEST_INIT);
-       if ((timeout & CS4231_CALIB_IN_PROGRESS) == 0) {
-               snd_printd("cs4231_mce_down - auto calibration time out (1)\n");
-               spin_unlock_irqrestore(&chip->lock, flags);
-               return;
-       }
-
-       /* in 10ms increments, check condition, up to 250ms */
-       timeout = 25;
-       while (snd_cs4231_in(chip, CS4231_TEST_INIT) &
-               CS4231_CALIB_IN_PROGRESS) {
 
+       /*
+        * Wait for auto-calibration (AC) process to finish, i.e. ACI to go low.
+        */
+       timeout = jiffies + msecs_to_jiffies(250);
+       do {
                spin_unlock_irqrestore(&chip->lock, flags);
-               if (--timeout < 0) {
-                       snd_printk("mce_down - "
-                                  "auto calibration time out (2)\n");
-                       return;
-               }
-               msleep(10);
-               spin_lock_irqsave(&chip->lock, flags);
-       }
-
-       /* in 10ms increments, check condition, up to 100ms */
-       timeout = 10;
-       while (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT) {
-               spin_unlock_irqrestore(&chip->lock, flags);
-               if (--timeout < 0) {
-                       snd_printk("mce_down - "
-                                  "auto calibration time out (3)\n");
-                       return;
-               }
-               msleep(10);
+               msleep(1);
                spin_lock_irqsave(&chip->lock, flags);
-       }
+               reg = snd_cs4231_in(chip, CS4231_TEST_INIT);
+               reg &= CS4231_CALIB_IN_PROGRESS;
+       } while (reg && time_before(jiffies, timeout));
        spin_unlock_irqrestore(&chip->lock, flags);
+
+       if (reg)
+               snd_printk(KERN_ERR
+                          "mce_down - auto calibration time out (2)\n");
 }
 
 static void snd_cs4231_advance_dma(struct cs4231_dma_control *dma_cont,
@@ -860,7 +703,7 @@ static int snd_cs4231_timer_stop(struct snd_timer *timer)
        return 0;
 }
 
-static void __init snd_cs4231_init(struct snd_cs4231 *chip)
+static void __devinit snd_cs4231_init(struct snd_cs4231 *chip)
 {
        unsigned long flags;
 
@@ -944,8 +787,8 @@ static int snd_cs4231_open(struct snd_cs4231 *chip, unsigned int mode)
                       CS4231_RECORD_IRQ |
                       CS4231_TIMER_IRQ);
        snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
-       __cs4231_writeb(chip, 0, CS4231P(chip, STATUS));        /* clear IRQ */
-       __cs4231_writeb(chip, 0, CS4231P(chip, STATUS));        /* clear IRQ */
+       __cs4231_writeb(chip, 0, CS4231U(chip, STATUS));        /* clear IRQ */
+       __cs4231_writeb(chip, 0, CS4231U(chip, STATUS));        /* clear IRQ */
 
        snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
                       CS4231_RECORD_IRQ |
@@ -974,8 +817,8 @@ static void snd_cs4231_close(struct snd_cs4231 *chip, unsigned int mode)
        /* disable IRQ */
        spin_lock_irqsave(&chip->lock, flags);
        snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
-       __cs4231_writeb(chip, 0, CS4231P(chip, STATUS));        /* clear IRQ */
-       __cs4231_writeb(chip, 0, CS4231P(chip, STATUS));        /* clear IRQ */
+       __cs4231_writeb(chip, 0, CS4231U(chip, STATUS));        /* clear IRQ */
+       __cs4231_writeb(chip, 0, CS4231U(chip, STATUS));        /* clear IRQ */
 
        /* now disable record & playback */
 
@@ -997,8 +840,8 @@ static void snd_cs4231_close(struct snd_cs4231 *chip, unsigned int mode)
 
        /* clear IRQ again */
        snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
-       __cs4231_writeb(chip, 0, CS4231P(chip, STATUS));        /* clear IRQ */
-       __cs4231_writeb(chip, 0, CS4231P(chip, STATUS));        /* clear IRQ */
+       __cs4231_writeb(chip, 0, CS4231U(chip, STATUS));        /* clear IRQ */
+       __cs4231_writeb(chip, 0, CS4231U(chip, STATUS));        /* clear IRQ */
        spin_unlock_irqrestore(&chip->lock, flags);
 
        snd_cs4231_calibrate_mute(chip, 0);
@@ -1177,7 +1020,7 @@ static snd_pcm_uframes_t snd_cs4231_capture_pointer(
        return bytes_to_frames(substream->runtime, ptr);
 }
 
-static int __init snd_cs4231_probe(struct snd_cs4231 *chip)
+static int __devinit snd_cs4231_probe(struct snd_cs4231 *chip)
 {
        unsigned long flags;
        int i;
@@ -1187,7 +1030,7 @@ static int __init snd_cs4231_probe(struct snd_cs4231 *chip)
 
        for (i = 0; i < 50; i++) {
                mb();
-               if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
+               if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
                        msleep(2);
                else {
                        spin_lock_irqsave(&chip->lock, flags);
@@ -1205,8 +1048,9 @@ static int __init snd_cs4231_probe(struct snd_cs4231 *chip)
 
        spin_lock_irqsave(&chip->lock, flags);
 
-       __cs4231_readb(chip, CS4231P(chip, STATUS));    /* clear any pendings IRQ */
-       __cs4231_writeb(chip, 0, CS4231P(chip, STATUS));
+       /* clear any pendings IRQ */
+       __cs4231_readb(chip, CS4231U(chip, STATUS));
+       __cs4231_writeb(chip, 0, CS4231U(chip, STATUS));
        mb();
 
        spin_unlock_irqrestore(&chip->lock, flags);
@@ -1375,7 +1219,7 @@ static struct snd_pcm_ops snd_cs4231_capture_ops = {
        .pointer        =       snd_cs4231_capture_pointer,
 };
 
-static int __init snd_cs4231_pcm(struct snd_card *card)
+static int __devinit snd_cs4231_pcm(struct snd_card *card)
 {
        struct snd_cs4231 *chip = card->private_data;
        struct snd_pcm *pcm;
@@ -1395,14 +1239,16 @@ static int __init snd_cs4231_pcm(struct snd_card *card)
        pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
        strcpy(pcm->name, "CS4231");
 
-       chip->p_dma.preallocate(chip, pcm);
+       snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
+                                             &chip->op->dev,
+                                             64 * 1024, 128 * 1024);
 
        chip->pcm = pcm;
 
        return 0;
 }
 
-static int __init snd_cs4231_timer(struct snd_card *card)
+static int __devinit snd_cs4231_timer(struct snd_card *card)
 {
        struct snd_cs4231 *chip = card->private_data;
        struct snd_timer *timer;
@@ -1653,7 +1499,7 @@ static int snd_cs4231_put_double(struct snd_kcontrol *kcontrol,
   .private_value = (left_reg) | ((right_reg) << 8) | ((shift_left) << 16) | \
                   ((shift_right) << 19) | ((mask) << 24) | ((invert) << 22) }
 
-static struct snd_kcontrol_new snd_cs4231_controls[] __initdata = {
+static struct snd_kcontrol_new snd_cs4231_controls[] __devinitdata = {
 CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT,
                CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
 CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT,
@@ -1692,12 +1538,13 @@ CS4231_SINGLE("Line Out Switch", 0, CS4231_PIN_CTRL, 6, 1, 1),
 CS4231_SINGLE("Headphone Out Switch", 0, CS4231_PIN_CTRL, 7, 1, 1)
 };
 
-static int __init snd_cs4231_mixer(struct snd_card *card)
+static int __devinit snd_cs4231_mixer(struct snd_card *card)
 {
        struct snd_cs4231 *chip = card->private_data;
        int err, idx;
 
-       snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
+       if (snd_BUG_ON(!chip || !chip->pcm))
+               return -EINVAL;
 
        strcpy(card->mixername, chip->pcm->name);
 
@@ -1712,10 +1559,11 @@ static int __init snd_cs4231_mixer(struct snd_card *card)
 
 static int dev;
 
-static int __init cs4231_attach_begin(struct snd_card **rcard)
+static int __devinit cs4231_attach_begin(struct snd_card **rcard)
 {
        struct snd_card *card;
        struct snd_cs4231 *chip;
+       int err;
 
        *rcard = NULL;
 
@@ -1727,10 +1575,10 @@ static int __init cs4231_attach_begin(struct snd_card **rcard)
                return -ENOENT;
        }
 
-       card = snd_card_new(index[dev], id[dev], THIS_MODULE,
-                           sizeof(struct snd_cs4231));
-       if (card == NULL)
-               return -ENOMEM;
+       err = snd_card_create(index[dev], id[dev], THIS_MODULE,
+                             sizeof(struct snd_cs4231), &card);
+       if (err < 0)
+               return err;
 
        strcpy(card->driver, "CS4231");
        strcpy(card->shortname, "Sun CS4231");
@@ -1742,7 +1590,7 @@ static int __init cs4231_attach_begin(struct snd_card **rcard)
        return 0;
 }
 
-static int __init cs4231_attach_finish(struct snd_card *card)
+static int __devinit cs4231_attach_finish(struct snd_card *card)
 {
        struct snd_cs4231 *chip = card->private_data;
        int err;
@@ -1763,8 +1611,7 @@ static int __init cs4231_attach_finish(struct snd_card *card)
        if (err < 0)
                goto out_err;
 
-       chip->next = cs4231_list;
-       cs4231_list = chip;
+       dev_set_drvdata(&chip->op->dev, chip);
 
        dev++;
        return 0;
@@ -1784,7 +1631,7 @@ static irqreturn_t snd_cs4231_sbus_interrupt(int irq, void *dev_id)
        struct snd_cs4231 *chip = dev_id;
 
        /*This is IRQ is not raised by the cs4231*/
-       if (!(__cs4231_readb(chip, CS4231P(chip, STATUS)) & CS4231_GLOBALIRQ))
+       if (!(__cs4231_readb(chip, CS4231U(chip, STATUS)) & CS4231_GLOBALIRQ))
                return IRQ_NONE;
 
        /* ACK the APC interrupt. */
@@ -1919,24 +1766,19 @@ static unsigned int sbus_dma_addr(struct cs4231_dma_control *dma_cont)
        return sbus_readl(base->regs + base->dir + APCVA);
 }
 
-static void sbus_dma_preallocate(struct snd_cs4231 *chip, struct snd_pcm *pcm)
-{
-       snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_SBUS,
-                                       snd_dma_sbus_data(chip->dev_u.sdev),
-                                       64 * 1024, 128 * 1024);
-}
-
 /*
  * Init and exit routines
  */
 
 static int snd_cs4231_sbus_free(struct snd_cs4231 *chip)
 {
+       struct of_device *op = chip->op;
+
        if (chip->irq[0])
                free_irq(chip->irq[0], chip);
 
        if (chip->port)
-               sbus_iounmap(chip->port, chip->regs_size);
+               of_iounmap(&op->resource[0], chip->port, chip->regs_size);
 
        return 0;
 }
@@ -1952,9 +1794,9 @@ static struct snd_device_ops snd_cs4231_sbus_dev_ops = {
        .dev_free       =       snd_cs4231_sbus_dev_free,
 };
 
-static int __init snd_cs4231_sbus_create(struct snd_card *card,
-                                        struct sbus_dev *sdev,
-                                        int dev)
+static int __devinit snd_cs4231_sbus_create(struct snd_card *card,
+                                           struct of_device *op,
+                                           int dev)
 {
        struct snd_cs4231 *chip = card->private_data;
        int err;
@@ -1964,13 +1806,13 @@ static int __init snd_cs4231_sbus_create(struct snd_card *card,
        spin_lock_init(&chip->p_dma.sbus_info.lock);
        mutex_init(&chip->mce_mutex);
        mutex_init(&chip->open_mutex);
-       chip->dev_u.sdev = sdev;
-       chip->regs_size = sdev->reg_addrs[0].reg_size;
+       chip->op = op;
+       chip->regs_size = resource_size(&op->resource[0]);
        memcpy(&chip->image, &snd_cs4231_original_image,
               sizeof(snd_cs4231_original_image));
 
-       chip->port = sbus_ioremap(&sdev->resource[0], 0,
-                                 chip->regs_size, "cs4231");
+       chip->port = of_ioremap(&op->resource[0], 0,
+                               chip->regs_size, "cs4231");
        if (!chip->port) {
                snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
                return -EIO;
@@ -1985,22 +1827,20 @@ static int __init snd_cs4231_sbus_create(struct snd_card *card,
        chip->p_dma.enable = sbus_dma_enable;
        chip->p_dma.request = sbus_dma_request;
        chip->p_dma.address = sbus_dma_addr;
-       chip->p_dma.preallocate = sbus_dma_preallocate;
 
        chip->c_dma.prepare = sbus_dma_prepare;
        chip->c_dma.enable = sbus_dma_enable;
        chip->c_dma.request = sbus_dma_request;
        chip->c_dma.address = sbus_dma_addr;
-       chip->c_dma.preallocate = sbus_dma_preallocate;
 
-       if (request_irq(sdev->irqs[0], snd_cs4231_sbus_interrupt,
+       if (request_irq(op->irqs[0], snd_cs4231_sbus_interrupt,
                        IRQF_SHARED, "cs4231", chip)) {
                snd_printdd("cs4231-%d: Unable to grab SBUS IRQ %d\n",
-                           dev, sdev->irqs[0]);
+                           dev, op->irqs[0]);
                snd_cs4231_sbus_free(chip);
                return -EBUSY;
        }
-       chip->irq[0] = sdev->irqs[0];
+       chip->irq[0] = op->irqs[0];
 
        if (snd_cs4231_probe(chip) < 0) {
                snd_cs4231_sbus_free(chip);
@@ -2017,9 +1857,9 @@ static int __init snd_cs4231_sbus_create(struct snd_card *card,
        return 0;
 }
 
-static int __init cs4231_sbus_attach(struct sbus_dev *sdev)
+static int __devinit cs4231_sbus_probe(struct of_device *op, const struct of_device_id *match)
 {
-       struct resource *rp = &sdev->resource[0];
+       struct resource *rp = &op->resource[0];
        struct snd_card *card;
        int err;
 
@@ -2031,9 +1871,9 @@ static int __init cs4231_sbus_attach(struct sbus_dev *sdev)
                card->shortname,
                rp->flags & 0xffL,
                (unsigned long long)rp->start,
-               sdev->irqs[0]);
+               op->irqs[0]);
 
-       err = snd_cs4231_sbus_create(card, sdev, dev);
+       err = snd_cs4231_sbus_create(card, op, dev);
        if (err < 0) {
                snd_card_free(card);
                return err;
@@ -2086,30 +1926,25 @@ static unsigned int _ebus_dma_addr(struct cs4231_dma_control *dma_cont)
        return ebus_dma_addr(&dma_cont->ebus_info);
 }
 
-static void _ebus_dma_preallocate(struct snd_cs4231 *chip, struct snd_pcm *pcm)
-{
-       snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
-                                     snd_dma_pci_data(chip->dev_u.pdev),
-                                     64*1024, 128*1024);
-}
-
 /*
  * Init and exit routines
  */
 
 static int snd_cs4231_ebus_free(struct snd_cs4231 *chip)
 {
+       struct of_device *op = chip->op;
+
        if (chip->c_dma.ebus_info.regs) {
                ebus_dma_unregister(&chip->c_dma.ebus_info);
-               iounmap(chip->c_dma.ebus_info.regs);
+               of_iounmap(&op->resource[2], chip->c_dma.ebus_info.regs, 0x10);
        }
        if (chip->p_dma.ebus_info.regs) {
                ebus_dma_unregister(&chip->p_dma.ebus_info);
-               iounmap(chip->p_dma.ebus_info.regs);
+               of_iounmap(&op->resource[1], chip->p_dma.ebus_info.regs, 0x10);
        }
 
        if (chip->port)
-               iounmap(chip->port);
+               of_iounmap(&op->resource[0], chip->port, 0x10);
 
        return 0;
 }
@@ -2125,9 +1960,9 @@ static struct snd_device_ops snd_cs4231_ebus_dev_ops = {
        .dev_free       =       snd_cs4231_ebus_dev_free,
 };
 
-static int __init snd_cs4231_ebus_create(struct snd_card *card,
-                                        struct linux_ebus_device *edev,
-                                        int dev)
+static int __devinit snd_cs4231_ebus_create(struct snd_card *card,
+                                           struct of_device *op,
+                                           int dev)
 {
        struct snd_cs4231 *chip = card->private_data;
        int err;
@@ -2138,35 +1973,35 @@ static int __init snd_cs4231_ebus_create(struct snd_card *card,
        mutex_init(&chip->mce_mutex);
        mutex_init(&chip->open_mutex);
        chip->flags |= CS4231_FLAG_EBUS;
-       chip->dev_u.pdev = edev->bus->self;
+       chip->op = op;
        memcpy(&chip->image, &snd_cs4231_original_image,
               sizeof(snd_cs4231_original_image));
        strcpy(chip->c_dma.ebus_info.name, "cs4231(capture)");
        chip->c_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
        chip->c_dma.ebus_info.callback = snd_cs4231_ebus_capture_callback;
        chip->c_dma.ebus_info.client_cookie = chip;
-       chip->c_dma.ebus_info.irq = edev->irqs[0];
+       chip->c_dma.ebus_info.irq = op->irqs[0];
        strcpy(chip->p_dma.ebus_info.name, "cs4231(play)");
        chip->p_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
        chip->p_dma.ebus_info.callback = snd_cs4231_ebus_play_callback;
        chip->p_dma.ebus_info.client_cookie = chip;
-       chip->p_dma.ebus_info.irq = edev->irqs[1];
+       chip->p_dma.ebus_info.irq = op->irqs[1];
 
        chip->p_dma.prepare = _ebus_dma_prepare;
        chip->p_dma.enable = _ebus_dma_enable;
        chip->p_dma.request = _ebus_dma_request;
        chip->p_dma.address = _ebus_dma_addr;
-       chip->p_dma.preallocate = _ebus_dma_preallocate;
 
        chip->c_dma.prepare = _ebus_dma_prepare;
        chip->c_dma.enable = _ebus_dma_enable;
        chip->c_dma.request = _ebus_dma_request;
        chip->c_dma.address = _ebus_dma_addr;
-       chip->c_dma.preallocate = _ebus_dma_preallocate;
 
-       chip->port = ioremap(edev->resource[0].start, 0x10);
-       chip->p_dma.ebus_info.regs = ioremap(edev->resource[1].start, 0x10);
-       chip->c_dma.ebus_info.regs = ioremap(edev->resource[2].start, 0x10);
+       chip->port = of_ioremap(&op->resource[0], 0, 0x10, "cs4231");
+       chip->p_dma.ebus_info.regs =
+               of_ioremap(&op->resource[1], 0, 0x10, "cs4231_pdma");
+       chip->c_dma.ebus_info.regs =
+               of_ioremap(&op->resource[2], 0, 0x10, "cs4231_cdma");
        if (!chip->port || !chip->p_dma.ebus_info.regs ||
            !chip->c_dma.ebus_info.regs) {
                snd_cs4231_ebus_free(chip);
@@ -2214,7 +2049,7 @@ static int __init snd_cs4231_ebus_create(struct snd_card *card,
        return 0;
 }
 
-static int __init cs4231_ebus_attach(struct linux_ebus_device *edev)
+static int __devinit cs4231_ebus_probe(struct of_device *op, const struct of_device_id *match)
 {
        struct snd_card *card;
        int err;
@@ -2223,12 +2058,12 @@ static int __init cs4231_ebus_attach(struct linux_ebus_device *edev)
        if (err)
                return err;
 
-       sprintf(card->longname, "%s at 0x%lx, irq %d",
+       sprintf(card->longname, "%s at 0x%llx, irq %d",
                card->shortname,
-               edev->resource[0].start,
-               edev->irqs[0]);
+               op->resource[0].start,
+               op->irqs[0]);
 
-       err = snd_cs4231_ebus_create(card, edev, dev);
+       err = snd_cs4231_ebus_create(card, op, dev);
        if (err < 0) {
                snd_card_free(card);
                return err;
@@ -2238,68 +2073,57 @@ static int __init cs4231_ebus_attach(struct linux_ebus_device *edev)
 }
 #endif
 
-static int __init cs4231_init(void)
+static int __devinit cs4231_probe(struct of_device *op, const struct of_device_id *match)
 {
-#ifdef SBUS_SUPPORT
-       struct sbus_bus *sbus;
-       struct sbus_dev *sdev;
-#endif
 #ifdef EBUS_SUPPORT
-       struct linux_ebus *ebus;
-       struct linux_ebus_device *edev;
+       if (!strcmp(op->node->parent->name, "ebus"))
+               return cs4231_ebus_probe(op, match);
 #endif
-       int found;
-
-       found = 0;
-
 #ifdef SBUS_SUPPORT
-       for_all_sbusdev(sdev, sbus) {
-               if (!strcmp(sdev->prom_name, "SUNW,CS4231")) {
-                       if (cs4231_sbus_attach(sdev) == 0)
-                               found++;
-               }
-       }
+       if (!strcmp(op->node->parent->name, "sbus") ||
+           !strcmp(op->node->parent->name, "sbi"))
+               return cs4231_sbus_probe(op, match);
 #endif
-#ifdef EBUS_SUPPORT
-       for_each_ebus(ebus) {
-               for_each_ebusdev(edev, ebus) {
-                       int match = 0;
-
-                       if (!strcmp(edev->prom_node->name, "SUNW,CS4231")) {
-                               match = 1;
-                       } else if (!strcmp(edev->prom_node->name, "audio")) {
-                               const char *compat;
-
-                               compat = of_get_property(edev->prom_node,
-                                                        "compatible", NULL);
-                               if (compat && !strcmp(compat, "SUNW,CS4231"))
-                                       match = 1;
-                       }
+       return -ENODEV;
+}
 
-                       if (match &&
-                           cs4231_ebus_attach(edev) == 0)
-                               found++;
-               }
-       }
-#endif
+static int __devexit cs4231_remove(struct of_device *op)
+{
+       struct snd_cs4231 *chip = dev_get_drvdata(&op->dev);
 
+       snd_card_free(chip->card);
 
-       return (found > 0) ? 0 : -EIO;
+       return 0;
 }
 
-static void __exit cs4231_exit(void)
-{
-       struct snd_cs4231 *p = cs4231_list;
+static const struct of_device_id cs4231_match[] = {
+       {
+               .name = "SUNW,CS4231",
+       },
+       {
+               .name = "audio",
+               .compatible = "SUNW,CS4231",
+       },
+       {},
+};
 
-       while (p != NULL) {
-               struct snd_cs4231 *next = p->next;
+MODULE_DEVICE_TABLE(of, cs4231_match);
 
-               snd_card_free(p->card);
+static struct of_platform_driver cs4231_driver = {
+       .name           = "audio",
+       .match_table    = cs4231_match,
+       .probe          = cs4231_probe,
+       .remove         = __devexit_p(cs4231_remove),
+};
 
-               p = next;
-       }
+static int __init cs4231_init(void)
+{
+       return of_register_driver(&cs4231_driver, &of_bus_type);
+}
 
-       cs4231_list = NULL;
+static void __exit cs4231_exit(void)
+{
+       of_unregister_driver(&cs4231_driver);
 }
 
 module_init(cs4231_init);