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