| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | *  linux/sound/arm/aaci.c - ARM PrimeCell AACI PL041 driver | 
|  | 3 | * | 
|  | 4 | *  Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved. | 
|  | 5 | * | 
|  | 6 | * This program is free software; you can redistribute it and/or modify | 
|  | 7 | * it under the terms of the GNU General Public License version 2 as | 
|  | 8 | * published by the Free Software Foundation. | 
|  | 9 | * | 
|  | 10 | *  Documentation: ARM DDI 0173B | 
|  | 11 | */ | 
|  | 12 | #include <linux/module.h> | 
|  | 13 | #include <linux/delay.h> | 
|  | 14 | #include <linux/init.h> | 
|  | 15 | #include <linux/ioport.h> | 
|  | 16 | #include <linux/device.h> | 
|  | 17 | #include <linux/spinlock.h> | 
|  | 18 | #include <linux/interrupt.h> | 
|  | 19 | #include <linux/err.h> | 
| Russell King | a62c80e | 2006-01-07 13:52:45 +0000 | [diff] [blame] | 20 | #include <linux/amba/bus.h> | 
| Russell King | 88cdca9 | 2009-11-23 09:44:10 +0100 | [diff] [blame] | 21 | #include <linux/io.h> | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 22 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 23 | #include <sound/core.h> | 
|  | 24 | #include <sound/initval.h> | 
|  | 25 | #include <sound/ac97_codec.h> | 
|  | 26 | #include <sound/pcm.h> | 
|  | 27 | #include <sound/pcm_params.h> | 
|  | 28 |  | 
|  | 29 | #include "aaci.h" | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 30 |  | 
|  | 31 | #define DRIVER_NAME	"aaci-pl041" | 
|  | 32 |  | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 33 | #define FRAME_PERIOD_US	21 | 
|  | 34 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 35 | /* | 
|  | 36 | * PM support is not complete.  Turn it off. | 
|  | 37 | */ | 
|  | 38 | #undef CONFIG_PM | 
|  | 39 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 40 | static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 41 | { | 
|  | 42 | u32 v, maincr = aaci->maincr | MAINCR_SCRA(ac97->num); | 
|  | 43 |  | 
|  | 44 | /* | 
|  | 45 | * Ensure that the slot 1/2 RX registers are empty. | 
|  | 46 | */ | 
|  | 47 | v = readl(aaci->base + AACI_SLFR); | 
|  | 48 | if (v & SLFR_2RXV) | 
|  | 49 | readl(aaci->base + AACI_SL2RX); | 
|  | 50 | if (v & SLFR_1RXV) | 
|  | 51 | readl(aaci->base + AACI_SL1RX); | 
|  | 52 |  | 
|  | 53 | writel(maincr, aaci->base + AACI_MAINCR); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | /* | 
|  | 57 | * P29: | 
|  | 58 | *  The recommended use of programming the external codec through slot 1 | 
|  | 59 | *  and slot 2 data is to use the channels during setup routines and the | 
|  | 60 | *  slot register at any other time.  The data written into slot 1, slot 2 | 
|  | 61 | *  and slot 12 registers is transmitted only when their corresponding | 
|  | 62 | *  SI1TxEn, SI2TxEn and SI12TxEn bits are set in the AACI_MAINCR | 
|  | 63 | *  register. | 
|  | 64 | */ | 
| Kevin Hilman | 14d178a | 2007-02-07 05:46:47 +0100 | [diff] [blame] | 65 | static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg, | 
|  | 66 | unsigned short val) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 67 | { | 
|  | 68 | struct aaci *aaci = ac97->private_data; | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 69 | int timeout; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 70 | u32 v; | 
|  | 71 |  | 
|  | 72 | if (ac97->num >= 4) | 
|  | 73 | return; | 
|  | 74 |  | 
| Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 75 | mutex_lock(&aaci->ac97_sem); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 76 |  | 
|  | 77 | aaci_ac97_select_codec(aaci, ac97); | 
|  | 78 |  | 
|  | 79 | /* | 
|  | 80 | * P54: You must ensure that AACI_SL2TX is always written | 
|  | 81 | * to, if required, before data is written to AACI_SL1TX. | 
|  | 82 | */ | 
|  | 83 | writel(val << 4, aaci->base + AACI_SL2TX); | 
|  | 84 | writel(reg << 12, aaci->base + AACI_SL1TX); | 
|  | 85 |  | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 86 | /* Initially, wait one frame period */ | 
|  | 87 | udelay(FRAME_PERIOD_US); | 
|  | 88 |  | 
|  | 89 | /* And then wait an additional eight frame periods for it to be sent */ | 
|  | 90 | timeout = FRAME_PERIOD_US * 8; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 91 | do { | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 92 | udelay(1); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 93 | v = readl(aaci->base + AACI_SLFR); | 
| Roel Kluin | f6f35bb | 2009-02-08 15:22:25 +0100 | [diff] [blame] | 94 | } while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout); | 
| Kevin Hilman | 14d178a | 2007-02-07 05:46:47 +0100 | [diff] [blame] | 95 |  | 
| Russell King | 69058cd | 2011-01-12 23:17:24 +0000 | [diff] [blame] | 96 | if (v & (SLFR_1TXB|SLFR_2TXB)) | 
| Kevin Hilman | 14d178a | 2007-02-07 05:46:47 +0100 | [diff] [blame] | 97 | dev_err(&aaci->dev->dev, | 
|  | 98 | "timeout waiting for write to complete\n"); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 99 |  | 
| Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 100 | mutex_unlock(&aaci->ac97_sem); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
|  | 103 | /* | 
|  | 104 | * Read an AC'97 register. | 
|  | 105 | */ | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 106 | static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 107 | { | 
|  | 108 | struct aaci *aaci = ac97->private_data; | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 109 | int timeout, retries = 10; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 110 | u32 v; | 
|  | 111 |  | 
|  | 112 | if (ac97->num >= 4) | 
|  | 113 | return ~0; | 
|  | 114 |  | 
| Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 115 | mutex_lock(&aaci->ac97_sem); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 116 |  | 
|  | 117 | aaci_ac97_select_codec(aaci, ac97); | 
|  | 118 |  | 
|  | 119 | /* | 
|  | 120 | * Write the register address to slot 1. | 
|  | 121 | */ | 
|  | 122 | writel((reg << 12) | (1 << 19), aaci->base + AACI_SL1TX); | 
|  | 123 |  | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 124 | /* Initially, wait one frame period */ | 
|  | 125 | udelay(FRAME_PERIOD_US); | 
|  | 126 |  | 
|  | 127 | /* And then wait an additional eight frame periods for it to be sent */ | 
|  | 128 | timeout = FRAME_PERIOD_US * 8; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 129 | do { | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 130 | udelay(1); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 131 | v = readl(aaci->base + AACI_SLFR); | 
| Roel Kluin | f6f35bb | 2009-02-08 15:22:25 +0100 | [diff] [blame] | 132 | } while ((v & SLFR_1TXB) && --timeout); | 
| Kevin Hilman | 14d178a | 2007-02-07 05:46:47 +0100 | [diff] [blame] | 133 |  | 
| Russell King | 69058cd | 2011-01-12 23:17:24 +0000 | [diff] [blame] | 134 | if (v & SLFR_1TXB) { | 
| Kevin Hilman | 14d178a | 2007-02-07 05:46:47 +0100 | [diff] [blame] | 135 | dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n"); | 
|  | 136 | v = ~0; | 
|  | 137 | goto out; | 
|  | 138 | } | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 139 |  | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 140 | /* Now wait for the response frame */ | 
|  | 141 | udelay(FRAME_PERIOD_US); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 142 |  | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 143 | /* And then wait an additional eight frame periods for data */ | 
|  | 144 | timeout = FRAME_PERIOD_US * 8; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 145 | do { | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 146 | udelay(1); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 147 | cond_resched(); | 
|  | 148 | v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV); | 
| Roel Kluin | f6f35bb | 2009-02-08 15:22:25 +0100 | [diff] [blame] | 149 | } while ((v != (SLFR_1RXV|SLFR_2RXV)) && --timeout); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 150 |  | 
| Russell King | 69058cd | 2011-01-12 23:17:24 +0000 | [diff] [blame] | 151 | if (v != (SLFR_1RXV|SLFR_2RXV)) { | 
| Kevin Hilman | 14d178a | 2007-02-07 05:46:47 +0100 | [diff] [blame] | 152 | dev_err(&aaci->dev->dev, "timeout on RX valid\n"); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 153 | v = ~0; | 
| Kevin Hilman | 14d178a | 2007-02-07 05:46:47 +0100 | [diff] [blame] | 154 | goto out; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
| Kevin Hilman | 14d178a | 2007-02-07 05:46:47 +0100 | [diff] [blame] | 157 | do { | 
|  | 158 | v = readl(aaci->base + AACI_SL1RX) >> 12; | 
|  | 159 | if (v == reg) { | 
|  | 160 | v = readl(aaci->base + AACI_SL2RX) >> 4; | 
|  | 161 | break; | 
|  | 162 | } else if (--retries) { | 
|  | 163 | dev_warn(&aaci->dev->dev, | 
|  | 164 | "ac97 read back fail.  retry\n"); | 
|  | 165 | continue; | 
|  | 166 | } else { | 
|  | 167 | dev_warn(&aaci->dev->dev, | 
|  | 168 | "wrong ac97 register read back (%x != %x)\n", | 
|  | 169 | v, reg); | 
|  | 170 | v = ~0; | 
|  | 171 | } | 
|  | 172 | } while (retries); | 
|  | 173 | out: | 
| Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 174 | mutex_unlock(&aaci->ac97_sem); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 175 | return v; | 
|  | 176 | } | 
|  | 177 |  | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 178 | static inline void | 
|  | 179 | aaci_chan_wait_ready(struct aaci_runtime *aacirun, unsigned long mask) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 180 | { | 
|  | 181 | u32 val; | 
|  | 182 | int timeout = 5000; | 
|  | 183 |  | 
|  | 184 | do { | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 185 | udelay(1); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 186 | val = readl(aacirun->base + AACI_SR); | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 187 | } while (val & mask && timeout--); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
|  | 190 |  | 
|  | 191 |  | 
|  | 192 | /* | 
|  | 193 | * Interrupt support. | 
|  | 194 | */ | 
| Kevin Hilman | 62578cb | 2007-02-07 05:41:37 +0100 | [diff] [blame] | 195 | static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 196 | { | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 197 | if (mask & ISR_ORINTR) { | 
|  | 198 | dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel); | 
|  | 199 | writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR); | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | if (mask & ISR_RXTOINTR) { | 
|  | 203 | dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel); | 
|  | 204 | writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR); | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | if (mask & ISR_RXINTR) { | 
|  | 208 | struct aaci_runtime *aacirun = &aaci->capture; | 
|  | 209 | void *ptr; | 
|  | 210 |  | 
|  | 211 | if (!aacirun->substream || !aacirun->start) { | 
| Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 212 | dev_warn(&aaci->dev->dev, "RX interrupt???\n"); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 213 | writel(0, aacirun->base + AACI_IE); | 
|  | 214 | return; | 
|  | 215 | } | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 216 |  | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 217 | spin_lock(&aacirun->lock); | 
|  | 218 |  | 
|  | 219 | ptr = aacirun->ptr; | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 220 | do { | 
|  | 221 | unsigned int len = aacirun->fifosz; | 
|  | 222 | u32 val; | 
|  | 223 |  | 
|  | 224 | if (aacirun->bytes <= 0) { | 
|  | 225 | aacirun->bytes += aacirun->period; | 
|  | 226 | aacirun->ptr = ptr; | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 227 | spin_unlock(&aacirun->lock); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 228 | snd_pcm_period_elapsed(aacirun->substream); | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 229 | spin_lock(&aacirun->lock); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 230 | } | 
|  | 231 | if (!(aacirun->cr & CR_EN)) | 
|  | 232 | break; | 
|  | 233 |  | 
|  | 234 | val = readl(aacirun->base + AACI_SR); | 
|  | 235 | if (!(val & SR_RXHF)) | 
|  | 236 | break; | 
|  | 237 | if (!(val & SR_RXFF)) | 
|  | 238 | len >>= 1; | 
|  | 239 |  | 
|  | 240 | aacirun->bytes -= len; | 
|  | 241 |  | 
|  | 242 | /* reading 16 bytes at a time */ | 
|  | 243 | for( ; len > 0; len -= 16) { | 
|  | 244 | asm( | 
|  | 245 | "ldmia	%1, {r0, r1, r2, r3}\n\t" | 
|  | 246 | "stmia	%0!, {r0, r1, r2, r3}" | 
|  | 247 | : "+r" (ptr) | 
|  | 248 | : "r" (aacirun->fifo) | 
|  | 249 | : "r0", "r1", "r2", "r3", "cc"); | 
|  | 250 |  | 
|  | 251 | if (ptr >= aacirun->end) | 
|  | 252 | ptr = aacirun->start; | 
|  | 253 | } | 
|  | 254 | } while(1); | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 255 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 256 | aacirun->ptr = ptr; | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 257 |  | 
|  | 258 | spin_unlock(&aacirun->lock); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 259 | } | 
|  | 260 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 261 | if (mask & ISR_URINTR) { | 
| Kevin Hilman | 62578cb | 2007-02-07 05:41:37 +0100 | [diff] [blame] | 262 | dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel); | 
|  | 263 | writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 264 | } | 
|  | 265 |  | 
|  | 266 | if (mask & ISR_TXINTR) { | 
|  | 267 | struct aaci_runtime *aacirun = &aaci->playback; | 
|  | 268 | void *ptr; | 
|  | 269 |  | 
|  | 270 | if (!aacirun->substream || !aacirun->start) { | 
| Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 271 | dev_warn(&aaci->dev->dev, "TX interrupt???\n"); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 272 | writel(0, aacirun->base + AACI_IE); | 
|  | 273 | return; | 
|  | 274 | } | 
|  | 275 |  | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 276 | spin_lock(&aacirun->lock); | 
|  | 277 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 278 | ptr = aacirun->ptr; | 
|  | 279 | do { | 
|  | 280 | unsigned int len = aacirun->fifosz; | 
|  | 281 | u32 val; | 
|  | 282 |  | 
|  | 283 | if (aacirun->bytes <= 0) { | 
|  | 284 | aacirun->bytes += aacirun->period; | 
|  | 285 | aacirun->ptr = ptr; | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 286 | spin_unlock(&aacirun->lock); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 287 | snd_pcm_period_elapsed(aacirun->substream); | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 288 | spin_lock(&aacirun->lock); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 289 | } | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 290 | if (!(aacirun->cr & CR_EN)) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 291 | break; | 
|  | 292 |  | 
|  | 293 | val = readl(aacirun->base + AACI_SR); | 
|  | 294 | if (!(val & SR_TXHE)) | 
|  | 295 | break; | 
|  | 296 | if (!(val & SR_TXFE)) | 
|  | 297 | len >>= 1; | 
|  | 298 |  | 
|  | 299 | aacirun->bytes -= len; | 
|  | 300 |  | 
|  | 301 | /* writing 16 bytes at a time */ | 
|  | 302 | for ( ; len > 0; len -= 16) { | 
|  | 303 | asm( | 
|  | 304 | "ldmia	%0!, {r0, r1, r2, r3}\n\t" | 
|  | 305 | "stmia	%1, {r0, r1, r2, r3}" | 
|  | 306 | : "+r" (ptr) | 
|  | 307 | : "r" (aacirun->fifo) | 
|  | 308 | : "r0", "r1", "r2", "r3", "cc"); | 
|  | 309 |  | 
|  | 310 | if (ptr >= aacirun->end) | 
|  | 311 | ptr = aacirun->start; | 
|  | 312 | } | 
|  | 313 | } while (1); | 
|  | 314 |  | 
|  | 315 | aacirun->ptr = ptr; | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 316 |  | 
|  | 317 | spin_unlock(&aacirun->lock); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 318 | } | 
|  | 319 | } | 
|  | 320 |  | 
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 321 | static irqreturn_t aaci_irq(int irq, void *devid) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 322 | { | 
|  | 323 | struct aaci *aaci = devid; | 
|  | 324 | u32 mask; | 
|  | 325 | int i; | 
|  | 326 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 327 | mask = readl(aaci->base + AACI_ALLINTS); | 
|  | 328 | if (mask) { | 
|  | 329 | u32 m = mask; | 
|  | 330 | for (i = 0; i < 4; i++, m >>= 7) { | 
|  | 331 | if (m & 0x7f) { | 
| Kevin Hilman | 62578cb | 2007-02-07 05:41:37 +0100 | [diff] [blame] | 332 | aaci_fifo_irq(aaci, i, m); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 333 | } | 
|  | 334 | } | 
|  | 335 | } | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 336 |  | 
|  | 337 | return mask ? IRQ_HANDLED : IRQ_NONE; | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 |  | 
|  | 341 |  | 
|  | 342 | /* | 
|  | 343 | * ALSA support. | 
|  | 344 | */ | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 345 | static struct snd_pcm_hardware aaci_hw_info = { | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 346 | .info			= SNDRV_PCM_INFO_MMAP | | 
|  | 347 | SNDRV_PCM_INFO_MMAP_VALID | | 
|  | 348 | SNDRV_PCM_INFO_INTERLEAVED | | 
|  | 349 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | 
|  | 350 | SNDRV_PCM_INFO_RESUME, | 
|  | 351 |  | 
|  | 352 | /* | 
|  | 353 | * ALSA doesn't support 18-bit or 20-bit packed into 32-bit | 
|  | 354 | * words.  It also doesn't support 12-bit at all. | 
|  | 355 | */ | 
|  | 356 | .formats		= SNDRV_PCM_FMTBIT_S16_LE, | 
|  | 357 |  | 
| Russell King | 6ca867c | 2009-12-18 17:48:35 +0000 | [diff] [blame] | 358 | /* rates are setup from the AC'97 codec */ | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 359 | .channels_min		= 2, | 
|  | 360 | .channels_max		= 6, | 
|  | 361 | .buffer_bytes_max	= 64 * 1024, | 
|  | 362 | .period_bytes_min	= 256, | 
|  | 363 | .period_bytes_max	= PAGE_SIZE, | 
|  | 364 | .periods_min		= 4, | 
|  | 365 | .periods_max		= PAGE_SIZE / 16, | 
|  | 366 | }; | 
|  | 367 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 368 | static int __aaci_pcm_open(struct aaci *aaci, | 
|  | 369 | struct snd_pcm_substream *substream, | 
|  | 370 | struct aaci_runtime *aacirun) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 371 | { | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 372 | struct snd_pcm_runtime *runtime = substream->runtime; | 
| Russell King | b60fb51 | 2011-01-25 15:52:33 +0000 | [diff] [blame^] | 373 | int ret = 0; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 374 |  | 
|  | 375 | aacirun->substream = substream; | 
|  | 376 | runtime->private_data = aacirun; | 
|  | 377 | runtime->hw = aaci_hw_info; | 
| Russell King | 6ca867c | 2009-12-18 17:48:35 +0000 | [diff] [blame] | 378 | runtime->hw.rates = aacirun->pcm->rates; | 
|  | 379 | snd_pcm_limit_hw_rates(runtime); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 380 |  | 
| Russell King | a08d565 | 2009-12-18 17:48:45 +0000 | [diff] [blame] | 381 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && | 
|  | 382 | aacirun->pcm->r[1].slots) | 
|  | 383 | snd_ac97_pcm_double_rate_rules(runtime); | 
|  | 384 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 385 | /* | 
|  | 386 | * FIXME: ALSA specifies fifo_size in bytes.  If we're in normal | 
|  | 387 | * mode, each 32-bit word contains one sample.  If we're in | 
|  | 388 | * compact mode, each 32-bit word contains two samples, effectively | 
|  | 389 | * halving the FIFO size.  However, we don't know for sure which | 
|  | 390 | * we'll be using at this point.  We set this to the lower limit. | 
|  | 391 | */ | 
|  | 392 | runtime->hw.fifo_size = aaci->fifosize * 2; | 
|  | 393 |  | 
| Russell King | b60fb51 | 2011-01-25 15:52:33 +0000 | [diff] [blame^] | 394 | mutex_lock(&aaci->irq_lock); | 
|  | 395 | if (!aaci->users++) { | 
|  | 396 | ret = request_irq(aaci->dev->irq[0], aaci_irq, | 
|  | 397 | IRQF_SHARED | IRQF_DISABLED, DRIVER_NAME, aaci); | 
|  | 398 | if (ret != 0) | 
|  | 399 | aaci->users--; | 
|  | 400 | } | 
|  | 401 | mutex_unlock(&aaci->irq_lock); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 402 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 403 | return ret; | 
|  | 404 | } | 
|  | 405 |  | 
|  | 406 |  | 
|  | 407 | /* | 
|  | 408 | * Common ALSA stuff | 
|  | 409 | */ | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 410 | static int aaci_pcm_close(struct snd_pcm_substream *substream) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 411 | { | 
|  | 412 | struct aaci *aaci = substream->private_data; | 
|  | 413 | struct aaci_runtime *aacirun = substream->runtime->private_data; | 
|  | 414 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 415 | WARN_ON(aacirun->cr & CR_EN); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 416 |  | 
|  | 417 | aacirun->substream = NULL; | 
| Russell King | b60fb51 | 2011-01-25 15:52:33 +0000 | [diff] [blame^] | 418 |  | 
|  | 419 | mutex_lock(&aaci->irq_lock); | 
|  | 420 | if (!--aaci->users) | 
|  | 421 | free_irq(aaci->dev->irq[0], aaci); | 
|  | 422 | mutex_unlock(&aaci->irq_lock); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 423 |  | 
|  | 424 | return 0; | 
|  | 425 | } | 
|  | 426 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 427 | static int aaci_pcm_hw_free(struct snd_pcm_substream *substream) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 428 | { | 
|  | 429 | struct aaci_runtime *aacirun = substream->runtime->private_data; | 
|  | 430 |  | 
|  | 431 | /* | 
|  | 432 | * This must not be called with the device enabled. | 
|  | 433 | */ | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 434 | WARN_ON(aacirun->cr & CR_EN); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 435 |  | 
|  | 436 | if (aacirun->pcm_open) | 
|  | 437 | snd_ac97_pcm_close(aacirun->pcm); | 
|  | 438 | aacirun->pcm_open = 0; | 
|  | 439 |  | 
|  | 440 | /* | 
|  | 441 | * Clear out the DMA and any allocated buffers. | 
|  | 442 | */ | 
| Takashi Iwai | d679732 | 2009-11-26 15:08:54 +0100 | [diff] [blame] | 443 | snd_pcm_lib_free_pages(substream); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 444 |  | 
|  | 445 | return 0; | 
|  | 446 | } | 
|  | 447 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 448 | static int aaci_pcm_hw_params(struct snd_pcm_substream *substream, | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 449 | struct aaci_runtime *aacirun, | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 450 | struct snd_pcm_hw_params *params) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 451 | { | 
|  | 452 | int err; | 
| Peter Huewe | 903b0eb | 2009-12-26 03:27:45 +0100 | [diff] [blame] | 453 | struct aaci *aaci = substream->private_data; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 454 |  | 
|  | 455 | aaci_pcm_hw_free(substream); | 
| Russell King | 4acd57c | 2009-11-29 16:39:52 +0000 | [diff] [blame] | 456 | if (aacirun->pcm_open) { | 
|  | 457 | snd_ac97_pcm_close(aacirun->pcm); | 
|  | 458 | aacirun->pcm_open = 0; | 
|  | 459 | } | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 460 |  | 
| Takashi Iwai | d679732 | 2009-11-26 15:08:54 +0100 | [diff] [blame] | 461 | err = snd_pcm_lib_malloc_pages(substream, | 
|  | 462 | params_buffer_bytes(params)); | 
| Russell King | 4e30b69 | 2009-12-18 17:48:37 +0000 | [diff] [blame] | 463 | if (err >= 0) { | 
| Russell King | a08d565 | 2009-12-18 17:48:45 +0000 | [diff] [blame] | 464 | unsigned int rate = params_rate(params); | 
|  | 465 | int dbl = rate > 48000; | 
|  | 466 |  | 
|  | 467 | err = snd_ac97_pcm_open(aacirun->pcm, rate, | 
| Russell King | 4e30b69 | 2009-12-18 17:48:37 +0000 | [diff] [blame] | 468 | params_channels(params), | 
| Russell King | a08d565 | 2009-12-18 17:48:45 +0000 | [diff] [blame] | 469 | aacirun->pcm->r[dbl].slots); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 470 |  | 
| Russell King | 4e30b69 | 2009-12-18 17:48:37 +0000 | [diff] [blame] | 471 | aacirun->pcm_open = err == 0; | 
| Russell King | d3aee79 | 2009-12-18 17:48:40 +0000 | [diff] [blame] | 472 | aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16; | 
|  | 473 | aacirun->fifosz = aaci->fifosize * 4; | 
|  | 474 |  | 
|  | 475 | if (aacirun->cr & CR_COMPACT) | 
|  | 476 | aacirun->fifosz >>= 1; | 
| Russell King | 4e30b69 | 2009-12-18 17:48:37 +0000 | [diff] [blame] | 477 | } | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 478 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 479 | return err; | 
|  | 480 | } | 
|  | 481 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 482 | static int aaci_pcm_prepare(struct snd_pcm_substream *substream) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 483 | { | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 484 | struct snd_pcm_runtime *runtime = substream->runtime; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 485 | struct aaci_runtime *aacirun = runtime->private_data; | 
|  | 486 |  | 
| Russell King | 4e30b69 | 2009-12-18 17:48:37 +0000 | [diff] [blame] | 487 | aacirun->start	= runtime->dma_area; | 
| Russell King | 88cdca9 | 2009-11-23 09:44:10 +0100 | [diff] [blame] | 488 | aacirun->end	= aacirun->start + snd_pcm_lib_buffer_bytes(substream); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 489 | aacirun->ptr	= aacirun->start; | 
|  | 490 | aacirun->period	= | 
|  | 491 | aacirun->bytes	= frames_to_bytes(runtime, runtime->period_size); | 
|  | 492 |  | 
|  | 493 | return 0; | 
|  | 494 | } | 
|  | 495 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 496 | static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 497 | { | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 498 | struct snd_pcm_runtime *runtime = substream->runtime; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 499 | struct aaci_runtime *aacirun = runtime->private_data; | 
|  | 500 | ssize_t bytes = aacirun->ptr - aacirun->start; | 
|  | 501 |  | 
|  | 502 | return bytes_to_frames(runtime, bytes); | 
|  | 503 | } | 
|  | 504 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 505 |  | 
|  | 506 | /* | 
|  | 507 | * Playback specific ALSA stuff | 
|  | 508 | */ | 
|  | 509 | static const u32 channels_to_txmask[] = { | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 510 | [2] = CR_SL3 | CR_SL4, | 
|  | 511 | [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8, | 
|  | 512 | [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9, | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 513 | }; | 
|  | 514 |  | 
|  | 515 | /* | 
|  | 516 | * We can support two and four channel audio.  Unfortunately | 
|  | 517 | * six channel audio requires a non-standard channel ordering: | 
|  | 518 | *   2 -> FL(3), FR(4) | 
|  | 519 | *   4 -> FL(3), FR(4), SL(7), SR(8) | 
|  | 520 | *   6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required) | 
|  | 521 | *        FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual) | 
|  | 522 | * This requires an ALSA configuration file to correct. | 
|  | 523 | */ | 
|  | 524 | static unsigned int channel_list[] = { 2, 4, 6 }; | 
|  | 525 |  | 
|  | 526 | static int | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 527 | aaci_rule_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 528 | { | 
|  | 529 | struct aaci *aaci = rule->private; | 
|  | 530 | unsigned int chan_mask = 1 << 0, slots; | 
|  | 531 |  | 
|  | 532 | /* | 
|  | 533 | * pcms[0] is the our 5.1 PCM instance. | 
|  | 534 | */ | 
|  | 535 | slots = aaci->ac97_bus->pcms[0].r[0].slots; | 
|  | 536 | if (slots & (1 << AC97_SLOT_PCM_SLEFT)) { | 
|  | 537 | chan_mask |= 1 << 1; | 
|  | 538 | if (slots & (1 << AC97_SLOT_LFE)) | 
|  | 539 | chan_mask |= 1 << 2; | 
|  | 540 | } | 
|  | 541 |  | 
|  | 542 | return snd_interval_list(hw_param_interval(p, rule->var), | 
|  | 543 | ARRAY_SIZE(channel_list), channel_list, | 
|  | 544 | chan_mask); | 
|  | 545 | } | 
|  | 546 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 547 | static int aaci_pcm_open(struct snd_pcm_substream *substream) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 548 | { | 
|  | 549 | struct aaci *aaci = substream->private_data; | 
|  | 550 | int ret; | 
|  | 551 |  | 
|  | 552 | /* | 
|  | 553 | * Add rule describing channel dependency. | 
|  | 554 | */ | 
|  | 555 | ret = snd_pcm_hw_rule_add(substream->runtime, 0, | 
|  | 556 | SNDRV_PCM_HW_PARAM_CHANNELS, | 
|  | 557 | aaci_rule_channels, aaci, | 
|  | 558 | SNDRV_PCM_HW_PARAM_CHANNELS, -1); | 
|  | 559 | if (ret) | 
|  | 560 | return ret; | 
|  | 561 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 562 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | 
|  | 563 | ret = __aaci_pcm_open(aaci, substream, &aaci->playback); | 
|  | 564 | } else { | 
|  | 565 | ret = __aaci_pcm_open(aaci, substream, &aaci->capture); | 
|  | 566 | } | 
|  | 567 | return ret; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 568 | } | 
|  | 569 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 570 | static int aaci_pcm_playback_hw_params(struct snd_pcm_substream *substream, | 
|  | 571 | struct snd_pcm_hw_params *params) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 572 | { | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 573 | struct aaci_runtime *aacirun = substream->runtime->private_data; | 
|  | 574 | unsigned int channels = params_channels(params); | 
|  | 575 | int ret; | 
|  | 576 |  | 
|  | 577 | WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) || | 
|  | 578 | !channels_to_txmask[channels]); | 
|  | 579 |  | 
|  | 580 | ret = aaci_pcm_hw_params(substream, aacirun, params); | 
|  | 581 |  | 
|  | 582 | /* | 
|  | 583 | * Enable FIFO, compact mode, 16 bits per sample. | 
|  | 584 | * FIXME: double rate slots? | 
|  | 585 | */ | 
| Russell King | d3aee79 | 2009-12-18 17:48:40 +0000 | [diff] [blame] | 586 | if (ret >= 0) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 587 | aacirun->cr |= channels_to_txmask[channels]; | 
|  | 588 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 589 | return ret; | 
|  | 590 | } | 
|  | 591 |  | 
|  | 592 | static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun) | 
|  | 593 | { | 
|  | 594 | u32 ie; | 
|  | 595 |  | 
|  | 596 | ie = readl(aacirun->base + AACI_IE); | 
|  | 597 | ie &= ~(IE_URIE|IE_TXIE); | 
|  | 598 | writel(ie, aacirun->base + AACI_IE); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 599 | aacirun->cr &= ~CR_EN; | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 600 | aaci_chan_wait_ready(aacirun, SR_TXB); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 601 | writel(aacirun->cr, aacirun->base + AACI_TXCR); | 
|  | 602 | } | 
|  | 603 |  | 
|  | 604 | static void aaci_pcm_playback_start(struct aaci_runtime *aacirun) | 
|  | 605 | { | 
|  | 606 | u32 ie; | 
|  | 607 |  | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 608 | aaci_chan_wait_ready(aacirun, SR_TXB); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 609 | aacirun->cr |= CR_EN; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 610 |  | 
|  | 611 | ie = readl(aacirun->base + AACI_IE); | 
|  | 612 | ie |= IE_URIE | IE_TXIE; | 
|  | 613 | writel(ie, aacirun->base + AACI_IE); | 
|  | 614 | writel(aacirun->cr, aacirun->base + AACI_TXCR); | 
|  | 615 | } | 
|  | 616 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 617 | static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 618 | { | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 619 | struct aaci_runtime *aacirun = substream->runtime->private_data; | 
|  | 620 | unsigned long flags; | 
|  | 621 | int ret = 0; | 
|  | 622 |  | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 623 | spin_lock_irqsave(&aacirun->lock, flags); | 
|  | 624 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 625 | switch (cmd) { | 
|  | 626 | case SNDRV_PCM_TRIGGER_START: | 
|  | 627 | aaci_pcm_playback_start(aacirun); | 
|  | 628 | break; | 
|  | 629 |  | 
|  | 630 | case SNDRV_PCM_TRIGGER_RESUME: | 
|  | 631 | aaci_pcm_playback_start(aacirun); | 
|  | 632 | break; | 
|  | 633 |  | 
|  | 634 | case SNDRV_PCM_TRIGGER_STOP: | 
|  | 635 | aaci_pcm_playback_stop(aacirun); | 
|  | 636 | break; | 
|  | 637 |  | 
|  | 638 | case SNDRV_PCM_TRIGGER_SUSPEND: | 
|  | 639 | aaci_pcm_playback_stop(aacirun); | 
|  | 640 | break; | 
|  | 641 |  | 
|  | 642 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | 
|  | 643 | break; | 
|  | 644 |  | 
|  | 645 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | 
|  | 646 | break; | 
|  | 647 |  | 
|  | 648 | default: | 
|  | 649 | ret = -EINVAL; | 
|  | 650 | } | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 651 |  | 
|  | 652 | spin_unlock_irqrestore(&aacirun->lock, flags); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 653 |  | 
|  | 654 | return ret; | 
|  | 655 | } | 
|  | 656 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 657 | static struct snd_pcm_ops aaci_playback_ops = { | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 658 | .open		= aaci_pcm_open, | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 659 | .close		= aaci_pcm_close, | 
|  | 660 | .ioctl		= snd_pcm_lib_ioctl, | 
|  | 661 | .hw_params	= aaci_pcm_playback_hw_params, | 
|  | 662 | .hw_free	= aaci_pcm_hw_free, | 
|  | 663 | .prepare	= aaci_pcm_prepare, | 
|  | 664 | .trigger	= aaci_pcm_playback_trigger, | 
|  | 665 | .pointer	= aaci_pcm_pointer, | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 666 | }; | 
|  | 667 |  | 
| Russell King | 8a37184 | 2007-02-20 15:44:23 +0000 | [diff] [blame] | 668 | static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream, | 
|  | 669 | struct snd_pcm_hw_params *params) | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 670 | { | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 671 | struct aaci_runtime *aacirun = substream->runtime->private_data; | 
|  | 672 | int ret; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 673 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 674 | ret = aaci_pcm_hw_params(substream, aacirun, params); | 
| Russell King | d3aee79 | 2009-12-18 17:48:40 +0000 | [diff] [blame] | 675 | if (ret >= 0) | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 676 | /* Line in record: slot 3 and 4 */ | 
|  | 677 | aacirun->cr |= CR_SL3 | CR_SL4; | 
|  | 678 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 679 | return ret; | 
|  | 680 | } | 
|  | 681 |  | 
|  | 682 | static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun) | 
|  | 683 | { | 
|  | 684 | u32 ie; | 
|  | 685 |  | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 686 | aaci_chan_wait_ready(aacirun, SR_RXB); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 687 |  | 
|  | 688 | ie = readl(aacirun->base + AACI_IE); | 
|  | 689 | ie &= ~(IE_ORIE | IE_RXIE); | 
|  | 690 | writel(ie, aacirun->base+AACI_IE); | 
|  | 691 |  | 
|  | 692 | aacirun->cr &= ~CR_EN; | 
|  | 693 |  | 
|  | 694 | writel(aacirun->cr, aacirun->base + AACI_RXCR); | 
|  | 695 | } | 
|  | 696 |  | 
|  | 697 | static void aaci_pcm_capture_start(struct aaci_runtime *aacirun) | 
|  | 698 | { | 
|  | 699 | u32 ie; | 
|  | 700 |  | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 701 | aaci_chan_wait_ready(aacirun, SR_RXB); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 702 |  | 
|  | 703 | #ifdef DEBUG | 
|  | 704 | /* RX Timeout value: bits 28:17 in RXCR */ | 
|  | 705 | aacirun->cr |= 0xf << 17; | 
|  | 706 | #endif | 
|  | 707 |  | 
|  | 708 | aacirun->cr |= CR_EN; | 
|  | 709 | writel(aacirun->cr, aacirun->base + AACI_RXCR); | 
|  | 710 |  | 
|  | 711 | ie = readl(aacirun->base + AACI_IE); | 
|  | 712 | ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full | 
|  | 713 | writel(ie, aacirun->base + AACI_IE); | 
|  | 714 | } | 
|  | 715 |  | 
| Russell King | 8a37184 | 2007-02-20 15:44:23 +0000 | [diff] [blame] | 716 | static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd) | 
|  | 717 | { | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 718 | struct aaci_runtime *aacirun = substream->runtime->private_data; | 
|  | 719 | unsigned long flags; | 
|  | 720 | int ret = 0; | 
|  | 721 |  | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 722 | spin_lock_irqsave(&aacirun->lock, flags); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 723 |  | 
|  | 724 | switch (cmd) { | 
|  | 725 | case SNDRV_PCM_TRIGGER_START: | 
|  | 726 | aaci_pcm_capture_start(aacirun); | 
|  | 727 | break; | 
|  | 728 |  | 
|  | 729 | case SNDRV_PCM_TRIGGER_RESUME: | 
|  | 730 | aaci_pcm_capture_start(aacirun); | 
|  | 731 | break; | 
|  | 732 |  | 
|  | 733 | case SNDRV_PCM_TRIGGER_STOP: | 
|  | 734 | aaci_pcm_capture_stop(aacirun); | 
|  | 735 | break; | 
|  | 736 |  | 
|  | 737 | case SNDRV_PCM_TRIGGER_SUSPEND: | 
|  | 738 | aaci_pcm_capture_stop(aacirun); | 
|  | 739 | break; | 
|  | 740 |  | 
|  | 741 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | 
|  | 742 | break; | 
|  | 743 |  | 
|  | 744 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | 
|  | 745 | break; | 
|  | 746 |  | 
|  | 747 | default: | 
|  | 748 | ret = -EINVAL; | 
|  | 749 | } | 
|  | 750 |  | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 751 | spin_unlock_irqrestore(&aacirun->lock, flags); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 752 |  | 
|  | 753 | return ret; | 
|  | 754 | } | 
|  | 755 |  | 
| Russell King | 8a37184 | 2007-02-20 15:44:23 +0000 | [diff] [blame] | 756 | static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream) | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 757 | { | 
|  | 758 | struct snd_pcm_runtime *runtime = substream->runtime; | 
|  | 759 | struct aaci *aaci = substream->private_data; | 
|  | 760 |  | 
|  | 761 | aaci_pcm_prepare(substream); | 
|  | 762 |  | 
|  | 763 | /* allow changing of sample rate */ | 
|  | 764 | aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */ | 
|  | 765 | aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate); | 
|  | 766 | aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate); | 
|  | 767 |  | 
|  | 768 | /* Record select: Mic: 0, Aux: 3, Line: 4 */ | 
|  | 769 | aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404); | 
|  | 770 |  | 
|  | 771 | return 0; | 
|  | 772 | } | 
|  | 773 |  | 
| Russell King | 8a37184 | 2007-02-20 15:44:23 +0000 | [diff] [blame] | 774 | static struct snd_pcm_ops aaci_capture_ops = { | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 775 | .open		= aaci_pcm_open, | 
|  | 776 | .close		= aaci_pcm_close, | 
|  | 777 | .ioctl		= snd_pcm_lib_ioctl, | 
|  | 778 | .hw_params	= aaci_pcm_capture_hw_params, | 
|  | 779 | .hw_free	= aaci_pcm_hw_free, | 
|  | 780 | .prepare	= aaci_pcm_capture_prepare, | 
|  | 781 | .trigger	= aaci_pcm_capture_trigger, | 
|  | 782 | .pointer	= aaci_pcm_pointer, | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 783 | }; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 784 |  | 
|  | 785 | /* | 
|  | 786 | * Power Management. | 
|  | 787 | */ | 
|  | 788 | #ifdef CONFIG_PM | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 789 | static int aaci_do_suspend(struct snd_card *card, unsigned int state) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 790 | { | 
|  | 791 | struct aaci *aaci = card->private_data; | 
| Takashi Iwai | 792a6c5 | 2005-11-17 17:19:25 +0100 | [diff] [blame] | 792 | snd_power_change_state(card, SNDRV_CTL_POWER_D3cold); | 
|  | 793 | snd_pcm_suspend_all(aaci->pcm); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 794 | return 0; | 
|  | 795 | } | 
|  | 796 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 797 | static int aaci_do_resume(struct snd_card *card, unsigned int state) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 798 | { | 
| Takashi Iwai | 792a6c5 | 2005-11-17 17:19:25 +0100 | [diff] [blame] | 799 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 800 | return 0; | 
|  | 801 | } | 
|  | 802 |  | 
| Richard Purdie | e36d394 | 2005-09-16 19:27:53 -0700 | [diff] [blame] | 803 | static int aaci_suspend(struct amba_device *dev, pm_message_t state) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 804 | { | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 805 | struct snd_card *card = amba_get_drvdata(dev); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 806 | return card ? aaci_do_suspend(card) : 0; | 
|  | 807 | } | 
|  | 808 |  | 
|  | 809 | static int aaci_resume(struct amba_device *dev) | 
|  | 810 | { | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 811 | struct snd_card *card = amba_get_drvdata(dev); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 812 | return card ? aaci_do_resume(card) : 0; | 
|  | 813 | } | 
|  | 814 | #else | 
|  | 815 | #define aaci_do_suspend		NULL | 
|  | 816 | #define aaci_do_resume		NULL | 
|  | 817 | #define aaci_suspend		NULL | 
|  | 818 | #define aaci_resume		NULL | 
|  | 819 | #endif | 
|  | 820 |  | 
|  | 821 |  | 
|  | 822 | static struct ac97_pcm ac97_defs[] __devinitdata = { | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 823 | [0] = {	/* Front PCM */ | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 824 | .exclusive = 1, | 
|  | 825 | .r = { | 
|  | 826 | [0] = { | 
|  | 827 | .slots	= (1 << AC97_SLOT_PCM_LEFT) | | 
|  | 828 | (1 << AC97_SLOT_PCM_RIGHT) | | 
|  | 829 | (1 << AC97_SLOT_PCM_CENTER) | | 
|  | 830 | (1 << AC97_SLOT_PCM_SLEFT) | | 
|  | 831 | (1 << AC97_SLOT_PCM_SRIGHT) | | 
|  | 832 | (1 << AC97_SLOT_LFE), | 
|  | 833 | }, | 
| Russell King | a08d565 | 2009-12-18 17:48:45 +0000 | [diff] [blame] | 834 | [1] = { | 
|  | 835 | .slots	= (1 << AC97_SLOT_PCM_LEFT) | | 
|  | 836 | (1 << AC97_SLOT_PCM_RIGHT) | | 
|  | 837 | (1 << AC97_SLOT_PCM_LEFT_0) | | 
|  | 838 | (1 << AC97_SLOT_PCM_RIGHT_0), | 
|  | 839 | }, | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 840 | }, | 
|  | 841 | }, | 
|  | 842 | [1] = {	/* PCM in */ | 
|  | 843 | .stream = 1, | 
|  | 844 | .exclusive = 1, | 
|  | 845 | .r = { | 
|  | 846 | [0] = { | 
|  | 847 | .slots	= (1 << AC97_SLOT_PCM_LEFT) | | 
|  | 848 | (1 << AC97_SLOT_PCM_RIGHT), | 
|  | 849 | }, | 
|  | 850 | }, | 
|  | 851 | }, | 
|  | 852 | [2] = {	/* Mic in */ | 
|  | 853 | .stream = 1, | 
|  | 854 | .exclusive = 1, | 
|  | 855 | .r = { | 
|  | 856 | [0] = { | 
|  | 857 | .slots	= (1 << AC97_SLOT_MIC), | 
|  | 858 | }, | 
|  | 859 | }, | 
|  | 860 | } | 
|  | 861 | }; | 
|  | 862 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 863 | static struct snd_ac97_bus_ops aaci_bus_ops = { | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 864 | .write	= aaci_ac97_write, | 
|  | 865 | .read	= aaci_ac97_read, | 
|  | 866 | }; | 
|  | 867 |  | 
|  | 868 | static int __devinit aaci_probe_ac97(struct aaci *aaci) | 
|  | 869 | { | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 870 | struct snd_ac97_template ac97_template; | 
|  | 871 | struct snd_ac97_bus *ac97_bus; | 
|  | 872 | struct snd_ac97 *ac97; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 873 | int ret; | 
|  | 874 |  | 
|  | 875 | /* | 
|  | 876 | * Assert AACIRESET for 2us | 
|  | 877 | */ | 
|  | 878 | writel(0, aaci->base + AACI_RESET); | 
|  | 879 | udelay(2); | 
|  | 880 | writel(RESET_NRST, aaci->base + AACI_RESET); | 
|  | 881 |  | 
|  | 882 | /* | 
|  | 883 | * Give the AC'97 codec more than enough time | 
|  | 884 | * to wake up. (42us = ~2 frames at 48kHz.) | 
|  | 885 | */ | 
| Russell King | 250c7a6 | 2011-01-12 23:42:57 +0000 | [diff] [blame] | 886 | udelay(FRAME_PERIOD_US * 2); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 887 |  | 
|  | 888 | ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus); | 
|  | 889 | if (ret) | 
|  | 890 | goto out; | 
|  | 891 |  | 
|  | 892 | ac97_bus->clock = 48000; | 
|  | 893 | aaci->ac97_bus = ac97_bus; | 
|  | 894 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 895 | memset(&ac97_template, 0, sizeof(struct snd_ac97_template)); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 896 | ac97_template.private_data = aaci; | 
|  | 897 | ac97_template.num = 0; | 
|  | 898 | ac97_template.scaps = AC97_SCAP_SKIP_MODEM; | 
|  | 899 |  | 
|  | 900 | ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97); | 
|  | 901 | if (ret) | 
|  | 902 | goto out; | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 903 | aaci->ac97 = ac97; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 904 |  | 
|  | 905 | /* | 
|  | 906 | * Disable AC97 PC Beep input on audio codecs. | 
|  | 907 | */ | 
|  | 908 | if (ac97_is_audio(ac97)) | 
|  | 909 | snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e); | 
|  | 910 |  | 
|  | 911 | ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs); | 
|  | 912 | if (ret) | 
|  | 913 | goto out; | 
|  | 914 |  | 
|  | 915 | aaci->playback.pcm = &ac97_bus->pcms[0]; | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 916 | aaci->capture.pcm  = &ac97_bus->pcms[1]; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 917 |  | 
|  | 918 | out: | 
|  | 919 | return ret; | 
|  | 920 | } | 
|  | 921 |  | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 922 | static void aaci_free_card(struct snd_card *card) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 923 | { | 
|  | 924 | struct aaci *aaci = card->private_data; | 
|  | 925 | if (aaci->base) | 
|  | 926 | iounmap(aaci->base); | 
|  | 927 | } | 
|  | 928 |  | 
|  | 929 | static struct aaci * __devinit aaci_init_card(struct amba_device *dev) | 
|  | 930 | { | 
|  | 931 | struct aaci *aaci; | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 932 | struct snd_card *card; | 
| Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 933 | int err; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 934 |  | 
| Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 935 | err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, | 
|  | 936 | THIS_MODULE, sizeof(struct aaci), &card); | 
|  | 937 | if (err < 0) | 
| Takashi Iwai | 631e8ad | 2008-09-01 15:31:50 +0200 | [diff] [blame] | 938 | return NULL; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 939 |  | 
|  | 940 | card->private_free = aaci_free_card; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 941 |  | 
|  | 942 | strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver)); | 
|  | 943 | strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname)); | 
|  | 944 | snprintf(card->longname, sizeof(card->longname), | 
| Greg Kroah-Hartman | aa0a2dd | 2006-06-12 14:50:27 -0700 | [diff] [blame] | 945 | "%s at 0x%016llx, irq %d", | 
|  | 946 | card->shortname, (unsigned long long)dev->res.start, | 
|  | 947 | dev->irq[0]); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 948 |  | 
|  | 949 | aaci = card->private_data; | 
| Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 950 | mutex_init(&aaci->ac97_sem); | 
| Russell King | b60fb51 | 2011-01-25 15:52:33 +0000 | [diff] [blame^] | 951 | mutex_init(&aaci->irq_lock); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 952 | aaci->card = card; | 
|  | 953 | aaci->dev = dev; | 
|  | 954 |  | 
|  | 955 | /* Set MAINCR to allow slot 1 and 2 data IO */ | 
|  | 956 | aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN | | 
|  | 957 | MAINCR_SL2RXEN | MAINCR_SL2TXEN; | 
|  | 958 |  | 
|  | 959 | return aaci; | 
|  | 960 | } | 
|  | 961 |  | 
|  | 962 | static int __devinit aaci_init_pcm(struct aaci *aaci) | 
|  | 963 | { | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 964 | struct snd_pcm *pcm; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 965 | int ret; | 
|  | 966 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 967 | ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 968 | if (ret == 0) { | 
|  | 969 | aaci->pcm = pcm; | 
|  | 970 | pcm->private_data = aaci; | 
|  | 971 | pcm->info_flags = 0; | 
|  | 972 |  | 
|  | 973 | strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name)); | 
|  | 974 |  | 
|  | 975 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 976 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops); | 
| Takashi Iwai | d679732 | 2009-11-26 15:08:54 +0100 | [diff] [blame] | 977 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, | 
| Takashi Iwai | d494643 | 2009-12-18 20:25:30 +0100 | [diff] [blame] | 978 | NULL, 0, 64 * 1024); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 979 | } | 
|  | 980 |  | 
|  | 981 | return ret; | 
|  | 982 | } | 
|  | 983 |  | 
|  | 984 | static unsigned int __devinit aaci_size_fifo(struct aaci *aaci) | 
|  | 985 | { | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 986 | struct aaci_runtime *aacirun = &aaci->playback; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 987 | int i; | 
|  | 988 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 989 | writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 990 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 991 | for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++) | 
|  | 992 | writel(0, aacirun->fifo); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 993 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 994 | writel(0, aacirun->base + AACI_TXCR); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 995 |  | 
|  | 996 | /* | 
|  | 997 | * Re-initialise the AACI after the FIFO depth test, to | 
|  | 998 | * ensure that the FIFOs are empty.  Unfortunately, merely | 
|  | 999 | * disabling the channel doesn't clear the FIFO. | 
|  | 1000 | */ | 
|  | 1001 | writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR); | 
|  | 1002 | writel(aaci->maincr, aaci->base + AACI_MAINCR); | 
|  | 1003 |  | 
|  | 1004 | /* | 
|  | 1005 | * If we hit 4096, we failed.  Go back to the specified | 
|  | 1006 | * fifo depth. | 
|  | 1007 | */ | 
|  | 1008 | if (i == 4096) | 
|  | 1009 | i = 8; | 
|  | 1010 |  | 
|  | 1011 | return i; | 
|  | 1012 | } | 
|  | 1013 |  | 
| Alessandro Rubini | 03fbdb1 | 2009-05-20 22:39:08 +0100 | [diff] [blame] | 1014 | static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id) | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1015 | { | 
|  | 1016 | struct aaci *aaci; | 
|  | 1017 | int ret, i; | 
|  | 1018 |  | 
|  | 1019 | ret = amba_request_regions(dev, NULL); | 
|  | 1020 | if (ret) | 
|  | 1021 | return ret; | 
|  | 1022 |  | 
|  | 1023 | aaci = aaci_init_card(dev); | 
| Takashi Iwai | 631e8ad | 2008-09-01 15:31:50 +0200 | [diff] [blame] | 1024 | if (!aaci) { | 
|  | 1025 | ret = -ENOMEM; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1026 | goto out; | 
|  | 1027 | } | 
|  | 1028 |  | 
| Linus Walleij | dc890c2 | 2009-06-07 23:27:31 +0100 | [diff] [blame] | 1029 | aaci->base = ioremap(dev->res.start, resource_size(&dev->res)); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1030 | if (!aaci->base) { | 
|  | 1031 | ret = -ENOMEM; | 
|  | 1032 | goto out; | 
|  | 1033 | } | 
|  | 1034 |  | 
|  | 1035 | /* | 
|  | 1036 | * Playback uses AACI channel 0 | 
|  | 1037 | */ | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 1038 | spin_lock_init(&aaci->playback.lock); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1039 | aaci->playback.base = aaci->base + AACI_CSCH1; | 
|  | 1040 | aaci->playback.fifo = aaci->base + AACI_DR1; | 
|  | 1041 |  | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 1042 | /* | 
|  | 1043 | * Capture uses AACI channel 0 | 
|  | 1044 | */ | 
| Russell King | d6a89fe | 2009-12-18 17:48:50 +0000 | [diff] [blame] | 1045 | spin_lock_init(&aaci->capture.lock); | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 1046 | aaci->capture.base = aaci->base + AACI_CSCH1; | 
|  | 1047 | aaci->capture.fifo = aaci->base + AACI_DR1; | 
|  | 1048 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1049 | for (i = 0; i < 4; i++) { | 
| viro@ZenIV.linux.org.uk | e12ba64 | 2005-09-06 02:06:57 +0100 | [diff] [blame] | 1050 | void __iomem *base = aaci->base + i * 0x14; | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1051 |  | 
|  | 1052 | writel(0, base + AACI_IE); | 
|  | 1053 | writel(0, base + AACI_TXCR); | 
|  | 1054 | writel(0, base + AACI_RXCR); | 
|  | 1055 | } | 
|  | 1056 |  | 
|  | 1057 | writel(0x1fff, aaci->base + AACI_INTCLR); | 
|  | 1058 | writel(aaci->maincr, aaci->base + AACI_MAINCR); | 
| Philby John | b68b58f | 2010-03-26 21:37:51 +0530 | [diff] [blame] | 1059 | /* | 
|  | 1060 | * Fix: ac97 read back fail errors by reading | 
|  | 1061 | * from any arbitrary aaci register. | 
|  | 1062 | */ | 
|  | 1063 | readl(aaci->base + AACI_CSCH1); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1064 | ret = aaci_probe_ac97(aaci); | 
|  | 1065 | if (ret) | 
|  | 1066 | goto out; | 
|  | 1067 |  | 
| Catalin Marinas | f27f218 | 2006-02-01 19:25:58 +0000 | [diff] [blame] | 1068 | /* | 
|  | 1069 | * Size the FIFOs (must be multiple of 16). | 
|  | 1070 | */ | 
|  | 1071 | aaci->fifosize = aaci_size_fifo(aaci); | 
|  | 1072 | if (aaci->fifosize & 15) { | 
|  | 1073 | printk(KERN_WARNING "AACI: fifosize = %d not supported\n", | 
|  | 1074 | aaci->fifosize); | 
|  | 1075 | ret = -ENODEV; | 
|  | 1076 | goto out; | 
|  | 1077 | } | 
|  | 1078 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1079 | ret = aaci_init_pcm(aaci); | 
|  | 1080 | if (ret) | 
|  | 1081 | goto out; | 
|  | 1082 |  | 
| Takashi Iwai | a76af19 | 2005-09-05 16:56:40 +0200 | [diff] [blame] | 1083 | snd_card_set_dev(aaci->card, &dev->dev); | 
|  | 1084 |  | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1085 | ret = snd_card_register(aaci->card); | 
|  | 1086 | if (ret == 0) { | 
|  | 1087 | dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname, | 
| Kevin Hilman | 41762b8 | 2007-02-07 05:45:32 +0100 | [diff] [blame] | 1088 | aaci->fifosize); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1089 | amba_set_drvdata(dev, aaci->card); | 
|  | 1090 | return ret; | 
|  | 1091 | } | 
|  | 1092 |  | 
|  | 1093 | out: | 
|  | 1094 | if (aaci) | 
|  | 1095 | snd_card_free(aaci->card); | 
|  | 1096 | amba_release_regions(dev); | 
|  | 1097 | return ret; | 
|  | 1098 | } | 
|  | 1099 |  | 
|  | 1100 | static int __devexit aaci_remove(struct amba_device *dev) | 
|  | 1101 | { | 
| Takashi Iwai | ceb9e47 | 2005-11-17 15:10:16 +0100 | [diff] [blame] | 1102 | struct snd_card *card = amba_get_drvdata(dev); | 
| Russell King | cb5a6ff | 2005-05-12 14:04:59 +0200 | [diff] [blame] | 1103 |  | 
|  | 1104 | amba_set_drvdata(dev, NULL); | 
|  | 1105 |  | 
|  | 1106 | if (card) { | 
|  | 1107 | struct aaci *aaci = card->private_data; | 
|  | 1108 | writel(0, aaci->base + AACI_MAINCR); | 
|  | 1109 |  | 
|  | 1110 | snd_card_free(card); | 
|  | 1111 | amba_release_regions(dev); | 
|  | 1112 | } | 
|  | 1113 |  | 
|  | 1114 | return 0; | 
|  | 1115 | } | 
|  | 1116 |  | 
|  | 1117 | static struct amba_id aaci_ids[] = { | 
|  | 1118 | { | 
|  | 1119 | .id	= 0x00041041, | 
|  | 1120 | .mask	= 0x000fffff, | 
|  | 1121 | }, | 
|  | 1122 | { 0, 0 }, | 
|  | 1123 | }; | 
|  | 1124 |  | 
|  | 1125 | static struct amba_driver aaci_driver = { | 
|  | 1126 | .drv		= { | 
|  | 1127 | .name	= DRIVER_NAME, | 
|  | 1128 | }, | 
|  | 1129 | .probe		= aaci_probe, | 
|  | 1130 | .remove		= __devexit_p(aaci_remove), | 
|  | 1131 | .suspend	= aaci_suspend, | 
|  | 1132 | .resume		= aaci_resume, | 
|  | 1133 | .id_table	= aaci_ids, | 
|  | 1134 | }; | 
|  | 1135 |  | 
|  | 1136 | static int __init aaci_init(void) | 
|  | 1137 | { | 
|  | 1138 | return amba_driver_register(&aaci_driver); | 
|  | 1139 | } | 
|  | 1140 |  | 
|  | 1141 | static void __exit aaci_exit(void) | 
|  | 1142 | { | 
|  | 1143 | amba_driver_unregister(&aaci_driver); | 
|  | 1144 | } | 
|  | 1145 |  | 
|  | 1146 | module_init(aaci_init); | 
|  | 1147 | module_exit(aaci_exit); | 
|  | 1148 |  | 
|  | 1149 | MODULE_LICENSE("GPL"); | 
|  | 1150 | MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver"); |