blob: b5399b0090a77a53c0eb09ac032771d66298b3db [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Russell Kingcb5a6ff2005-05-12 14:04:59 +02002/*
3 * linux/sound/arm/aaci.c - ARM PrimeCell AACI PL041 driver
4 *
5 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
6 *
Russell Kingcb5a6ff2005-05-12 14:04:59 +02007 * Documentation: ARM DDI 0173B
8 */
9#include <linux/module.h>
10#include <linux/delay.h>
11#include <linux/init.h>
12#include <linux/ioport.h>
13#include <linux/device.h>
14#include <linux/spinlock.h>
15#include <linux/interrupt.h>
16#include <linux/err.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000017#include <linux/amba/bus.h>
Russell King88cdca92009-11-23 09:44:10 +010018#include <linux/io.h>
Russell Kingcb5a6ff2005-05-12 14:04:59 +020019
Russell Kingcb5a6ff2005-05-12 14:04:59 +020020#include <sound/core.h>
21#include <sound/initval.h>
22#include <sound/ac97_codec.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25
26#include "aaci.h"
Russell Kingcb5a6ff2005-05-12 14:04:59 +020027
28#define DRIVER_NAME "aaci-pl041"
29
Russell King250c7a62011-01-12 23:42:57 +000030#define FRAME_PERIOD_US 21
31
Russell Kingcb5a6ff2005-05-12 14:04:59 +020032/*
33 * PM support is not complete. Turn it off.
34 */
35#undef CONFIG_PM
36
Takashi Iwaiceb9e472005-11-17 15:10:16 +010037static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020038{
39 u32 v, maincr = aaci->maincr | MAINCR_SCRA(ac97->num);
40
41 /*
42 * Ensure that the slot 1/2 RX registers are empty.
43 */
44 v = readl(aaci->base + AACI_SLFR);
45 if (v & SLFR_2RXV)
46 readl(aaci->base + AACI_SL2RX);
47 if (v & SLFR_1RXV)
48 readl(aaci->base + AACI_SL1RX);
49
Russell King7c289382011-02-05 10:41:55 +000050 if (maincr != readl(aaci->base + AACI_MAINCR)) {
51 writel(maincr, aaci->base + AACI_MAINCR);
52 readl(aaci->base + AACI_MAINCR);
53 udelay(1);
54 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +020055}
56
57/*
58 * P29:
59 * The recommended use of programming the external codec through slot 1
60 * and slot 2 data is to use the channels during setup routines and the
61 * slot register at any other time. The data written into slot 1, slot 2
62 * and slot 12 registers is transmitted only when their corresponding
63 * SI1TxEn, SI2TxEn and SI12TxEn bits are set in the AACI_MAINCR
64 * register.
65 */
Kevin Hilman14d178a2007-02-07 05:46:47 +010066static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
67 unsigned short val)
Russell Kingcb5a6ff2005-05-12 14:04:59 +020068{
69 struct aaci *aaci = ac97->private_data;
Russell King250c7a62011-01-12 23:42:57 +000070 int timeout;
Russell Kingcb5a6ff2005-05-12 14:04:59 +020071 u32 v;
72
73 if (ac97->num >= 4)
74 return;
75
Ingo Molnar12aa7572006-01-16 16:36:05 +010076 mutex_lock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +020077
78 aaci_ac97_select_codec(aaci, ac97);
79
80 /*
81 * P54: You must ensure that AACI_SL2TX is always written
82 * to, if required, before data is written to AACI_SL1TX.
83 */
84 writel(val << 4, aaci->base + AACI_SL2TX);
85 writel(reg << 12, aaci->base + AACI_SL1TX);
86
Russell King250c7a62011-01-12 23:42:57 +000087 /* Initially, wait one frame period */
88 udelay(FRAME_PERIOD_US);
89
90 /* And then wait an additional eight frame periods for it to be sent */
91 timeout = FRAME_PERIOD_US * 8;
Russell Kingcb5a6ff2005-05-12 14:04:59 +020092 do {
Russell King250c7a62011-01-12 23:42:57 +000093 udelay(1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +020094 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +010095 } while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +010096
Russell King69058cd2011-01-12 23:17:24 +000097 if (v & (SLFR_1TXB|SLFR_2TXB))
Kevin Hilman14d178a2007-02-07 05:46:47 +010098 dev_err(&aaci->dev->dev,
99 "timeout waiting for write to complete\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200100
Ingo Molnar12aa7572006-01-16 16:36:05 +0100101 mutex_unlock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200102}
103
104/*
105 * Read an AC'97 register.
106 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100107static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200108{
109 struct aaci *aaci = ac97->private_data;
Russell King250c7a62011-01-12 23:42:57 +0000110 int timeout, retries = 10;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200111 u32 v;
112
113 if (ac97->num >= 4)
114 return ~0;
115
Ingo Molnar12aa7572006-01-16 16:36:05 +0100116 mutex_lock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200117
118 aaci_ac97_select_codec(aaci, ac97);
119
120 /*
121 * Write the register address to slot 1.
122 */
123 writel((reg << 12) | (1 << 19), aaci->base + AACI_SL1TX);
124
Russell King250c7a62011-01-12 23:42:57 +0000125 /* Initially, wait one frame period */
126 udelay(FRAME_PERIOD_US);
127
128 /* And then wait an additional eight frame periods for it to be sent */
129 timeout = FRAME_PERIOD_US * 8;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200130 do {
Russell King250c7a62011-01-12 23:42:57 +0000131 udelay(1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200132 v = readl(aaci->base + AACI_SLFR);
Roel Kluinf6f35bb2009-02-08 15:22:25 +0100133 } while ((v & SLFR_1TXB) && --timeout);
Kevin Hilman14d178a2007-02-07 05:46:47 +0100134
Russell King69058cd2011-01-12 23:17:24 +0000135 if (v & SLFR_1TXB) {
Kevin Hilman14d178a2007-02-07 05:46:47 +0100136 dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
137 v = ~0;
138 goto out;
139 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200140
Russell King250c7a62011-01-12 23:42:57 +0000141 /* Now wait for the response frame */
142 udelay(FRAME_PERIOD_US);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200143
Russell King250c7a62011-01-12 23:42:57 +0000144 /* And then wait an additional eight frame periods for data */
145 timeout = FRAME_PERIOD_US * 8;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200146 do {
Russell King250c7a62011-01-12 23:42:57 +0000147 udelay(1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200148 cond_resched();
149 v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV);
Roel Kluinf6f35bb2009-02-08 15:22:25 +0100150 } while ((v != (SLFR_1RXV|SLFR_2RXV)) && --timeout);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200151
Russell King69058cd2011-01-12 23:17:24 +0000152 if (v != (SLFR_1RXV|SLFR_2RXV)) {
Kevin Hilman14d178a2007-02-07 05:46:47 +0100153 dev_err(&aaci->dev->dev, "timeout on RX valid\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200154 v = ~0;
Kevin Hilman14d178a2007-02-07 05:46:47 +0100155 goto out;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200156 }
157
Kevin Hilman14d178a2007-02-07 05:46:47 +0100158 do {
159 v = readl(aaci->base + AACI_SL1RX) >> 12;
160 if (v == reg) {
161 v = readl(aaci->base + AACI_SL2RX) >> 4;
162 break;
163 } else if (--retries) {
164 dev_warn(&aaci->dev->dev,
165 "ac97 read back fail. retry\n");
166 continue;
167 } else {
168 dev_warn(&aaci->dev->dev,
169 "wrong ac97 register read back (%x != %x)\n",
170 v, reg);
171 v = ~0;
172 }
173 } while (retries);
174 out:
Ingo Molnar12aa7572006-01-16 16:36:05 +0100175 mutex_unlock(&aaci->ac97_sem);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200176 return v;
177}
178
Russell Kingd6a89fe2009-12-18 17:48:50 +0000179static inline void
180aaci_chan_wait_ready(struct aaci_runtime *aacirun, unsigned long mask)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200181{
182 u32 val;
183 int timeout = 5000;
184
185 do {
Russell King250c7a62011-01-12 23:42:57 +0000186 udelay(1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200187 val = readl(aacirun->base + AACI_SR);
Russell Kingd6a89fe2009-12-18 17:48:50 +0000188 } while (val & mask && timeout--);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200189}
190
191
192
193/*
194 * Interrupt support.
195 */
Kevin Hilman62578cb2007-02-07 05:41:37 +0100196static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200197{
Kevin Hilman41762b82007-02-07 05:45:32 +0100198 if (mask & ISR_ORINTR) {
199 dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel);
200 writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR);
201 }
202
203 if (mask & ISR_RXTOINTR) {
204 dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel);
205 writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR);
206 }
207
208 if (mask & ISR_RXINTR) {
209 struct aaci_runtime *aacirun = &aaci->capture;
Russell Kingea51d0b2011-01-13 08:47:35 +0000210 bool period_elapsed = false;
Kevin Hilman41762b82007-02-07 05:45:32 +0100211 void *ptr;
212
213 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700214 dev_warn(&aaci->dev->dev, "RX interrupt???\n");
Kevin Hilman41762b82007-02-07 05:45:32 +0100215 writel(0, aacirun->base + AACI_IE);
216 return;
217 }
Kevin Hilman41762b82007-02-07 05:45:32 +0100218
Russell Kingd6a89fe2009-12-18 17:48:50 +0000219 spin_lock(&aacirun->lock);
220
221 ptr = aacirun->ptr;
Kevin Hilman41762b82007-02-07 05:45:32 +0100222 do {
Russell King5d350cb2011-01-13 22:25:10 +0000223 unsigned int len = aacirun->fifo_bytes;
Kevin Hilman41762b82007-02-07 05:45:32 +0100224 u32 val;
225
226 if (aacirun->bytes <= 0) {
227 aacirun->bytes += aacirun->period;
Russell Kingea51d0b2011-01-13 08:47:35 +0000228 period_elapsed = true;
Kevin Hilman41762b82007-02-07 05:45:32 +0100229 }
230 if (!(aacirun->cr & CR_EN))
231 break;
232
233 val = readl(aacirun->base + AACI_SR);
234 if (!(val & SR_RXHF))
235 break;
236 if (!(val & SR_RXFF))
237 len >>= 1;
238
239 aacirun->bytes -= len;
240
241 /* reading 16 bytes at a time */
242 for( ; len > 0; len -= 16) {
243 asm(
244 "ldmia %1, {r0, r1, r2, r3}\n\t"
245 "stmia %0!, {r0, r1, r2, r3}"
246 : "+r" (ptr)
247 : "r" (aacirun->fifo)
248 : "r0", "r1", "r2", "r3", "cc");
249
250 if (ptr >= aacirun->end)
251 ptr = aacirun->start;
252 }
253 } while(1);
Russell Kingd6a89fe2009-12-18 17:48:50 +0000254
Kevin Hilman41762b82007-02-07 05:45:32 +0100255 aacirun->ptr = ptr;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000256
257 spin_unlock(&aacirun->lock);
Russell Kingea51d0b2011-01-13 08:47:35 +0000258
259 if (period_elapsed)
260 snd_pcm_period_elapsed(aacirun->substream);
Kevin Hilman41762b82007-02-07 05:45:32 +0100261 }
262
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200263 if (mask & ISR_URINTR) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100264 dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
265 writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200266 }
267
268 if (mask & ISR_TXINTR) {
269 struct aaci_runtime *aacirun = &aaci->playback;
Russell Kingea51d0b2011-01-13 08:47:35 +0000270 bool period_elapsed = false;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200271 void *ptr;
272
273 if (!aacirun->substream || !aacirun->start) {
Joe Perches898eb712007-10-18 03:06:30 -0700274 dev_warn(&aaci->dev->dev, "TX interrupt???\n");
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200275 writel(0, aacirun->base + AACI_IE);
276 return;
277 }
278
Russell Kingd6a89fe2009-12-18 17:48:50 +0000279 spin_lock(&aacirun->lock);
280
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200281 ptr = aacirun->ptr;
282 do {
Russell King5d350cb2011-01-13 22:25:10 +0000283 unsigned int len = aacirun->fifo_bytes;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200284 u32 val;
285
286 if (aacirun->bytes <= 0) {
287 aacirun->bytes += aacirun->period;
Russell Kingea51d0b2011-01-13 08:47:35 +0000288 period_elapsed = true;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200289 }
Kevin Hilman41762b82007-02-07 05:45:32 +0100290 if (!(aacirun->cr & CR_EN))
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200291 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 Kingd6a89fe2009-12-18 17:48:50 +0000316
317 spin_unlock(&aacirun->lock);
Russell Kingea51d0b2011-01-13 08:47:35 +0000318
319 if (period_elapsed)
320 snd_pcm_period_elapsed(aacirun->substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200321 }
322}
323
David Howells7d12e782006-10-05 14:55:46 +0100324static irqreturn_t aaci_irq(int irq, void *devid)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200325{
326 struct aaci *aaci = devid;
327 u32 mask;
328 int i;
329
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200330 mask = readl(aaci->base + AACI_ALLINTS);
331 if (mask) {
332 u32 m = mask;
333 for (i = 0; i < 4; i++, m >>= 7) {
334 if (m & 0x7f) {
Kevin Hilman62578cb2007-02-07 05:41:37 +0100335 aaci_fifo_irq(aaci, i, m);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200336 }
337 }
338 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200339
340 return mask ? IRQ_HANDLED : IRQ_NONE;
341}
342
343
344
345/*
346 * ALSA support.
347 */
Bhumika Goyaldb566fb2017-08-17 14:45:49 +0530348static const struct snd_pcm_hardware aaci_hw_info = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200349 .info = SNDRV_PCM_INFO_MMAP |
350 SNDRV_PCM_INFO_MMAP_VALID |
351 SNDRV_PCM_INFO_INTERLEAVED |
352 SNDRV_PCM_INFO_BLOCK_TRANSFER |
353 SNDRV_PCM_INFO_RESUME,
354
355 /*
356 * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
357 * words. It also doesn't support 12-bit at all.
358 */
359 .formats = SNDRV_PCM_FMTBIT_S16_LE,
360
Russell King6ca867c2009-12-18 17:48:35 +0000361 /* rates are setup from the AC'97 codec */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200362 .channels_min = 2,
Russell Kinge831d802011-01-13 10:13:17 +0000363 .channels_max = 2,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200364 .buffer_bytes_max = 64 * 1024,
365 .period_bytes_min = 256,
366 .period_bytes_max = PAGE_SIZE,
367 .periods_min = 4,
368 .periods_max = PAGE_SIZE / 16,
369};
370
Russell Kinge831d802011-01-13 10:13:17 +0000371/*
372 * We can support two and four channel audio. Unfortunately
373 * six channel audio requires a non-standard channel ordering:
374 * 2 -> FL(3), FR(4)
375 * 4 -> FL(3), FR(4), SL(7), SR(8)
376 * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
377 * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
378 * This requires an ALSA configuration file to correct.
379 */
380static int aaci_rule_channels(struct snd_pcm_hw_params *p,
381 struct snd_pcm_hw_rule *rule)
382{
383 static unsigned int channel_list[] = { 2, 4, 6 };
384 struct aaci *aaci = rule->private;
385 unsigned int mask = 1 << 0, slots;
386
387 /* pcms[0] is the our 5.1 PCM instance. */
388 slots = aaci->ac97_bus->pcms[0].r[0].slots;
389 if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
390 mask |= 1 << 1;
391 if (slots & (1 << AC97_SLOT_LFE))
392 mask |= 1 << 2;
393 }
394
395 return snd_interval_list(hw_param_interval(p, rule->var),
396 ARRAY_SIZE(channel_list), channel_list, mask);
397}
398
399static int aaci_pcm_open(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200400{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100401 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kinge831d802011-01-13 10:13:17 +0000402 struct aaci *aaci = substream->private_data;
403 struct aaci_runtime *aacirun;
Russell Kingb60fb512011-01-25 15:52:33 +0000404 int ret = 0;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200405
Russell Kinge831d802011-01-13 10:13:17 +0000406 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
407 aacirun = &aaci->playback;
408 } else {
409 aacirun = &aaci->capture;
410 }
411
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200412 aacirun->substream = substream;
413 runtime->private_data = aacirun;
414 runtime->hw = aaci_hw_info;
Russell King6ca867c2009-12-18 17:48:35 +0000415 runtime->hw.rates = aacirun->pcm->rates;
416 snd_pcm_limit_hw_rates(runtime);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200417
Russell Kinge831d802011-01-13 10:13:17 +0000418 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
419 runtime->hw.channels_max = 6;
420
421 /* Add rule describing channel dependency. */
422 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
423 SNDRV_PCM_HW_PARAM_CHANNELS,
424 aaci_rule_channels, aaci,
425 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
426 if (ret)
427 return ret;
428
429 if (aacirun->pcm->r[1].slots)
430 snd_ac97_pcm_double_rate_rules(runtime);
431 }
Russell Kinga08d5652009-12-18 17:48:45 +0000432
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200433 /*
Russell King5d350cb2011-01-13 22:25:10 +0000434 * ALSA wants the byte-size of the FIFOs. As we only support
435 * 16-bit samples, this is twice the FIFO depth irrespective
436 * of whether it's in compact mode or not.
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200437 */
Russell King5d350cb2011-01-13 22:25:10 +0000438 runtime->hw.fifo_size = aaci->fifo_depth * 2;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200439
Russell Kingb60fb512011-01-25 15:52:33 +0000440 mutex_lock(&aaci->irq_lock);
441 if (!aaci->users++) {
442 ret = request_irq(aaci->dev->irq[0], aaci_irq,
Yong Zhang88e24c32011-09-22 16:59:20 +0800443 IRQF_SHARED, DRIVER_NAME, aaci);
Russell Kingb60fb512011-01-25 15:52:33 +0000444 if (ret != 0)
445 aaci->users--;
446 }
447 mutex_unlock(&aaci->irq_lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200448
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200449 return ret;
450}
451
452
453/*
454 * Common ALSA stuff
455 */
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100456static int aaci_pcm_close(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200457{
458 struct aaci *aaci = substream->private_data;
459 struct aaci_runtime *aacirun = substream->runtime->private_data;
460
Kevin Hilman41762b82007-02-07 05:45:32 +0100461 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200462
463 aacirun->substream = NULL;
Russell Kingb60fb512011-01-25 15:52:33 +0000464
465 mutex_lock(&aaci->irq_lock);
466 if (!--aaci->users)
467 free_irq(aaci->dev->irq[0], aaci);
468 mutex_unlock(&aaci->irq_lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200469
470 return 0;
471}
472
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100473static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200474{
475 struct aaci_runtime *aacirun = substream->runtime->private_data;
476
477 /*
478 * This must not be called with the device enabled.
479 */
Kevin Hilman41762b82007-02-07 05:45:32 +0100480 WARN_ON(aacirun->cr & CR_EN);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200481
482 if (aacirun->pcm_open)
483 snd_ac97_pcm_close(aacirun->pcm);
484 aacirun->pcm_open = 0;
485
486 /*
487 * Clear out the DMA and any allocated buffers.
488 */
Takashi Iwaid6797322009-11-26 15:08:54 +0100489 snd_pcm_lib_free_pages(substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200490
491 return 0;
492}
493
Russell King58e8a472011-01-26 16:59:39 +0000494/* Channel to slot mask */
495static const u32 channels_to_slotmask[] = {
496 [2] = CR_SL3 | CR_SL4,
497 [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
498 [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
499};
500
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100501static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100502 struct snd_pcm_hw_params *params)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200503{
Russell King58e8a472011-01-26 16:59:39 +0000504 struct aaci_runtime *aacirun = substream->runtime->private_data;
505 unsigned int channels = params_channels(params);
506 unsigned int rate = params_rate(params);
507 int dbl = rate > 48000;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200508 int err;
509
510 aaci_pcm_hw_free(substream);
Russell King4acd57c2009-11-29 16:39:52 +0000511 if (aacirun->pcm_open) {
512 snd_ac97_pcm_close(aacirun->pcm);
513 aacirun->pcm_open = 0;
514 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200515
Russell King58e8a472011-01-26 16:59:39 +0000516 /* channels is already limited to 2, 4, or 6 by aaci_rule_channels */
517 if (dbl && channels != 2)
518 return -EINVAL;
519
Takashi Iwaid6797322009-11-26 15:08:54 +0100520 err = snd_pcm_lib_malloc_pages(substream,
521 params_buffer_bytes(params));
Russell King4e30b692009-12-18 17:48:37 +0000522 if (err >= 0) {
Russell King58e8a472011-01-26 16:59:39 +0000523 struct aaci *aaci = substream->private_data;
Russell Kinga08d5652009-12-18 17:48:45 +0000524
Russell King58e8a472011-01-26 16:59:39 +0000525 err = snd_ac97_pcm_open(aacirun->pcm, rate, channels,
Russell Kinga08d5652009-12-18 17:48:45 +0000526 aacirun->pcm->r[dbl].slots);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200527
Russell King4e30b692009-12-18 17:48:37 +0000528 aacirun->pcm_open = err == 0;
Russell Kingd3aee792009-12-18 17:48:40 +0000529 aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
Russell King58e8a472011-01-26 16:59:39 +0000530 aacirun->cr |= channels_to_slotmask[channels + dbl * 2];
Russell Kingd3aee792009-12-18 17:48:40 +0000531
Russell King5d350cb2011-01-13 22:25:10 +0000532 /*
533 * fifo_bytes is the number of bytes we transfer to/from
534 * the FIFO, including padding. So that's x4. As we're
535 * in compact mode, the FIFO is half the size.
536 */
537 aacirun->fifo_bytes = aaci->fifo_depth * 4 / 2;
Russell King4e30b692009-12-18 17:48:37 +0000538 }
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200539
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200540 return err;
541}
542
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100543static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200544{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100545 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200546 struct aaci_runtime *aacirun = runtime->private_data;
547
Russell Kingc0dea822011-01-13 00:34:08 +0000548 aacirun->period = snd_pcm_lib_period_bytes(substream);
Russell King4e30b692009-12-18 17:48:37 +0000549 aacirun->start = runtime->dma_area;
Russell King88cdca92009-11-23 09:44:10 +0100550 aacirun->end = aacirun->start + snd_pcm_lib_buffer_bytes(substream);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200551 aacirun->ptr = aacirun->start;
Russell Kingc0dea822011-01-13 00:34:08 +0000552 aacirun->bytes = aacirun->period;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200553
554 return 0;
555}
556
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100557static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200558{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100559 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200560 struct aaci_runtime *aacirun = runtime->private_data;
561 ssize_t bytes = aacirun->ptr - aacirun->start;
562
563 return bytes_to_frames(runtime, bytes);
564}
565
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200566
567/*
568 * Playback specific ALSA stuff
569 */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200570static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
571{
572 u32 ie;
573
574 ie = readl(aacirun->base + AACI_IE);
575 ie &= ~(IE_URIE|IE_TXIE);
576 writel(ie, aacirun->base + AACI_IE);
Kevin Hilman41762b82007-02-07 05:45:32 +0100577 aacirun->cr &= ~CR_EN;
Russell Kingd6a89fe2009-12-18 17:48:50 +0000578 aaci_chan_wait_ready(aacirun, SR_TXB);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200579 writel(aacirun->cr, aacirun->base + AACI_TXCR);
580}
581
582static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
583{
584 u32 ie;
585
Russell Kingd6a89fe2009-12-18 17:48:50 +0000586 aaci_chan_wait_ready(aacirun, SR_TXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100587 aacirun->cr |= CR_EN;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200588
589 ie = readl(aacirun->base + AACI_IE);
590 ie |= IE_URIE | IE_TXIE;
591 writel(ie, aacirun->base + AACI_IE);
592 writel(aacirun->cr, aacirun->base + AACI_TXCR);
593}
594
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100595static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200596{
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200597 struct aaci_runtime *aacirun = substream->runtime->private_data;
598 unsigned long flags;
599 int ret = 0;
600
Russell Kingd6a89fe2009-12-18 17:48:50 +0000601 spin_lock_irqsave(&aacirun->lock, flags);
602
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200603 switch (cmd) {
604 case SNDRV_PCM_TRIGGER_START:
605 aaci_pcm_playback_start(aacirun);
606 break;
607
608 case SNDRV_PCM_TRIGGER_RESUME:
609 aaci_pcm_playback_start(aacirun);
610 break;
611
612 case SNDRV_PCM_TRIGGER_STOP:
613 aaci_pcm_playback_stop(aacirun);
614 break;
615
616 case SNDRV_PCM_TRIGGER_SUSPEND:
617 aaci_pcm_playback_stop(aacirun);
618 break;
619
620 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
621 break;
622
623 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
624 break;
625
626 default:
627 ret = -EINVAL;
628 }
Russell Kingd6a89fe2009-12-18 17:48:50 +0000629
630 spin_unlock_irqrestore(&aacirun->lock, flags);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200631
632 return ret;
633}
634
Arvind Yadav28f05482017-08-18 13:15:10 +0530635static const struct snd_pcm_ops aaci_playback_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100636 .open = aaci_pcm_open,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200637 .close = aaci_pcm_close,
638 .ioctl = snd_pcm_lib_ioctl,
Russell King58e8a472011-01-26 16:59:39 +0000639 .hw_params = aaci_pcm_hw_params,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200640 .hw_free = aaci_pcm_hw_free,
641 .prepare = aaci_pcm_prepare,
642 .trigger = aaci_pcm_playback_trigger,
643 .pointer = aaci_pcm_pointer,
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200644};
645
Kevin Hilman41762b82007-02-07 05:45:32 +0100646static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
647{
648 u32 ie;
649
Russell Kingd6a89fe2009-12-18 17:48:50 +0000650 aaci_chan_wait_ready(aacirun, SR_RXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100651
652 ie = readl(aacirun->base + AACI_IE);
653 ie &= ~(IE_ORIE | IE_RXIE);
654 writel(ie, aacirun->base+AACI_IE);
655
656 aacirun->cr &= ~CR_EN;
657
658 writel(aacirun->cr, aacirun->base + AACI_RXCR);
659}
660
661static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
662{
663 u32 ie;
664
Russell Kingd6a89fe2009-12-18 17:48:50 +0000665 aaci_chan_wait_ready(aacirun, SR_RXB);
Kevin Hilman41762b82007-02-07 05:45:32 +0100666
667#ifdef DEBUG
668 /* RX Timeout value: bits 28:17 in RXCR */
669 aacirun->cr |= 0xf << 17;
670#endif
671
672 aacirun->cr |= CR_EN;
673 writel(aacirun->cr, aacirun->base + AACI_RXCR);
674
675 ie = readl(aacirun->base + AACI_IE);
676 ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
677 writel(ie, aacirun->base + AACI_IE);
678}
679
Russell King8a371842007-02-20 15:44:23 +0000680static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
681{
Kevin Hilman41762b82007-02-07 05:45:32 +0100682 struct aaci_runtime *aacirun = substream->runtime->private_data;
683 unsigned long flags;
684 int ret = 0;
685
Russell Kingd6a89fe2009-12-18 17:48:50 +0000686 spin_lock_irqsave(&aacirun->lock, flags);
Kevin Hilman41762b82007-02-07 05:45:32 +0100687
688 switch (cmd) {
689 case SNDRV_PCM_TRIGGER_START:
690 aaci_pcm_capture_start(aacirun);
691 break;
692
693 case SNDRV_PCM_TRIGGER_RESUME:
694 aaci_pcm_capture_start(aacirun);
695 break;
696
697 case SNDRV_PCM_TRIGGER_STOP:
698 aaci_pcm_capture_stop(aacirun);
699 break;
700
701 case SNDRV_PCM_TRIGGER_SUSPEND:
702 aaci_pcm_capture_stop(aacirun);
703 break;
704
705 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
706 break;
707
708 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
709 break;
710
711 default:
712 ret = -EINVAL;
713 }
714
Russell Kingd6a89fe2009-12-18 17:48:50 +0000715 spin_unlock_irqrestore(&aacirun->lock, flags);
Kevin Hilman41762b82007-02-07 05:45:32 +0100716
717 return ret;
718}
719
Russell King8a371842007-02-20 15:44:23 +0000720static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
Kevin Hilman41762b82007-02-07 05:45:32 +0100721{
722 struct snd_pcm_runtime *runtime = substream->runtime;
723 struct aaci *aaci = substream->private_data;
724
725 aaci_pcm_prepare(substream);
726
727 /* allow changing of sample rate */
728 aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
729 aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
730 aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
731
732 /* Record select: Mic: 0, Aux: 3, Line: 4 */
733 aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
734
735 return 0;
736}
737
Arvind Yadav28f05482017-08-18 13:15:10 +0530738static const struct snd_pcm_ops aaci_capture_ops = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100739 .open = aaci_pcm_open,
740 .close = aaci_pcm_close,
741 .ioctl = snd_pcm_lib_ioctl,
Russell King58e8a472011-01-26 16:59:39 +0000742 .hw_params = aaci_pcm_hw_params,
Kevin Hilman41762b82007-02-07 05:45:32 +0100743 .hw_free = aaci_pcm_hw_free,
744 .prepare = aaci_pcm_capture_prepare,
745 .trigger = aaci_pcm_capture_trigger,
746 .pointer = aaci_pcm_pointer,
Kevin Hilman41762b82007-02-07 05:45:32 +0100747};
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200748
749/*
750 * Power Management.
751 */
752#ifdef CONFIG_PM
Ulf Hanssonb13a7142013-12-03 11:04:26 +0100753static int aaci_do_suspend(struct snd_card *card)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200754{
755 struct aaci *aaci = card->private_data;
Takashi Iwai792a6c52005-11-17 17:19:25 +0100756 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200757 return 0;
758}
759
Ulf Hanssonb13a7142013-12-03 11:04:26 +0100760static int aaci_do_resume(struct snd_card *card)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200761{
Takashi Iwai792a6c52005-11-17 17:19:25 +0100762 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200763 return 0;
764}
765
Ulf Hanssonb13a7142013-12-03 11:04:26 +0100766static int aaci_suspend(struct device *dev)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200767{
Ulf Hanssonb13a7142013-12-03 11:04:26 +0100768 struct snd_card *card = dev_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200769 return card ? aaci_do_suspend(card) : 0;
770}
771
Ulf Hanssonb13a7142013-12-03 11:04:26 +0100772static int aaci_resume(struct device *dev)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200773{
Ulf Hanssonb13a7142013-12-03 11:04:26 +0100774 struct snd_card *card = dev_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200775 return card ? aaci_do_resume(card) : 0;
776}
Ulf Hanssonb13a7142013-12-03 11:04:26 +0100777
778static SIMPLE_DEV_PM_OPS(aaci_dev_pm_ops, aaci_suspend, aaci_resume);
779#define AACI_DEV_PM_OPS (&aaci_dev_pm_ops)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200780#else
Ulf Hanssonb13a7142013-12-03 11:04:26 +0100781#define AACI_DEV_PM_OPS NULL
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200782#endif
783
784
Arvind Yadav9b419cd2017-08-23 17:39:25 +0530785static const struct ac97_pcm ac97_defs[] = {
Kevin Hilman41762b82007-02-07 05:45:32 +0100786 [0] = { /* Front PCM */
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200787 .exclusive = 1,
788 .r = {
789 [0] = {
790 .slots = (1 << AC97_SLOT_PCM_LEFT) |
791 (1 << AC97_SLOT_PCM_RIGHT) |
792 (1 << AC97_SLOT_PCM_CENTER) |
793 (1 << AC97_SLOT_PCM_SLEFT) |
794 (1 << AC97_SLOT_PCM_SRIGHT) |
795 (1 << AC97_SLOT_LFE),
796 },
Russell Kinga08d5652009-12-18 17:48:45 +0000797 [1] = {
798 .slots = (1 << AC97_SLOT_PCM_LEFT) |
799 (1 << AC97_SLOT_PCM_RIGHT) |
800 (1 << AC97_SLOT_PCM_LEFT_0) |
801 (1 << AC97_SLOT_PCM_RIGHT_0),
802 },
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200803 },
804 },
805 [1] = { /* PCM in */
806 .stream = 1,
807 .exclusive = 1,
808 .r = {
809 [0] = {
810 .slots = (1 << AC97_SLOT_PCM_LEFT) |
811 (1 << AC97_SLOT_PCM_RIGHT),
812 },
813 },
814 },
815 [2] = { /* Mic in */
816 .stream = 1,
817 .exclusive = 1,
818 .r = {
819 [0] = {
820 .slots = (1 << AC97_SLOT_MIC),
821 },
822 },
823 }
824};
825
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100826static struct snd_ac97_bus_ops aaci_bus_ops = {
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200827 .write = aaci_ac97_write,
828 .read = aaci_ac97_read,
829};
830
Bill Pemberton6c9dc192012-12-06 12:35:11 -0500831static int aaci_probe_ac97(struct aaci *aaci)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200832{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100833 struct snd_ac97_template ac97_template;
834 struct snd_ac97_bus *ac97_bus;
835 struct snd_ac97 *ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200836 int ret;
837
838 /*
839 * Assert AACIRESET for 2us
840 */
841 writel(0, aaci->base + AACI_RESET);
842 udelay(2);
843 writel(RESET_NRST, aaci->base + AACI_RESET);
844
845 /*
846 * Give the AC'97 codec more than enough time
847 * to wake up. (42us = ~2 frames at 48kHz.)
848 */
Russell King250c7a62011-01-12 23:42:57 +0000849 udelay(FRAME_PERIOD_US * 2);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200850
851 ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
852 if (ret)
853 goto out;
854
855 ac97_bus->clock = 48000;
856 aaci->ac97_bus = ac97_bus;
857
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100858 memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200859 ac97_template.private_data = aaci;
860 ac97_template.num = 0;
861 ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
862
863 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
864 if (ret)
865 goto out;
Kevin Hilman41762b82007-02-07 05:45:32 +0100866 aaci->ac97 = ac97;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200867
868 /*
869 * Disable AC97 PC Beep input on audio codecs.
870 */
871 if (ac97_is_audio(ac97))
872 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
873
874 ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
875 if (ret)
876 goto out;
877
878 aaci->playback.pcm = &ac97_bus->pcms[0];
Kevin Hilman41762b82007-02-07 05:45:32 +0100879 aaci->capture.pcm = &ac97_bus->pcms[1];
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200880
881 out:
882 return ret;
883}
884
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100885static void aaci_free_card(struct snd_card *card)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200886{
887 struct aaci *aaci = card->private_data;
Markus Elfringff6defa2015-01-03 22:55:54 +0100888
889 iounmap(aaci->base);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200890}
891
Bill Pemberton6c9dc192012-12-06 12:35:11 -0500892static struct aaci *aaci_init_card(struct amba_device *dev)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200893{
894 struct aaci *aaci;
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100895 struct snd_card *card;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100896 int err;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200897
Takashi Iwai4a875582014-01-29 14:25:18 +0100898 err = snd_card_new(&dev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
899 THIS_MODULE, sizeof(struct aaci), &card);
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100900 if (err < 0)
Takashi Iwai631e8ad2008-09-01 15:31:50 +0200901 return NULL;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200902
903 card->private_free = aaci_free_card;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200904
905 strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
906 strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
907 snprintf(card->longname, sizeof(card->longname),
Russell Kingf006d8fc2011-01-12 23:46:03 +0000908 "%s PL%03x rev%u at 0x%08llx, irq %d",
909 card->shortname, amba_part(dev), amba_rev(dev),
910 (unsigned long long)dev->res.start, dev->irq[0]);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200911
912 aaci = card->private_data;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100913 mutex_init(&aaci->ac97_sem);
Russell Kingb60fb512011-01-25 15:52:33 +0000914 mutex_init(&aaci->irq_lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200915 aaci->card = card;
916 aaci->dev = dev;
917
918 /* Set MAINCR to allow slot 1 and 2 data IO */
919 aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
920 MAINCR_SL2RXEN | MAINCR_SL2TXEN;
921
922 return aaci;
923}
924
Bill Pemberton6c9dc192012-12-06 12:35:11 -0500925static int aaci_init_pcm(struct aaci *aaci)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200926{
Takashi Iwaiceb9e472005-11-17 15:10:16 +0100927 struct snd_pcm *pcm;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200928 int ret;
929
Kevin Hilman41762b82007-02-07 05:45:32 +0100930 ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200931 if (ret == 0) {
932 aaci->pcm = pcm;
933 pcm->private_data = aaci;
934 pcm->info_flags = 0;
935
936 strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
937
938 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
Kevin Hilman41762b82007-02-07 05:45:32 +0100939 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
Takashi Iwaid6797322009-11-26 15:08:54 +0100940 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaibc70a9d2019-02-04 14:26:27 +0100941 aaci->card->dev,
942 0, 64 * 1024);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200943 }
944
945 return ret;
946}
947
Bill Pemberton6c9dc192012-12-06 12:35:11 -0500948static unsigned int aaci_size_fifo(struct aaci *aaci)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200949{
Kevin Hilman41762b82007-02-07 05:45:32 +0100950 struct aaci_runtime *aacirun = &aaci->playback;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200951 int i;
952
Russell King5d350cb2011-01-13 22:25:10 +0000953 /*
954 * Enable the channel, but don't assign it to any slots, so
955 * it won't empty onto the AC'97 link.
956 */
Kevin Hilman41762b82007-02-07 05:45:32 +0100957 writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200958
Kevin Hilman41762b82007-02-07 05:45:32 +0100959 for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
960 writel(0, aacirun->fifo);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200961
Kevin Hilman41762b82007-02-07 05:45:32 +0100962 writel(0, aacirun->base + AACI_TXCR);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200963
964 /*
965 * Re-initialise the AACI after the FIFO depth test, to
966 * ensure that the FIFOs are empty. Unfortunately, merely
967 * disabling the channel doesn't clear the FIFO.
968 */
969 writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
Russell King7c289382011-02-05 10:41:55 +0000970 readl(aaci->base + AACI_MAINCR);
971 udelay(1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200972 writel(aaci->maincr, aaci->base + AACI_MAINCR);
973
974 /*
Russell King5d350cb2011-01-13 22:25:10 +0000975 * If we hit 4096 entries, we failed. Go back to the specified
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200976 * fifo depth.
977 */
978 if (i == 4096)
979 i = 8;
980
981 return i;
982}
983
Bill Pemberton6c9dc192012-12-06 12:35:11 -0500984static int aaci_probe(struct amba_device *dev,
985 const struct amba_id *id)
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200986{
987 struct aaci *aaci;
988 int ret, i;
989
990 ret = amba_request_regions(dev, NULL);
991 if (ret)
992 return ret;
993
994 aaci = aaci_init_card(dev);
Takashi Iwai631e8ad2008-09-01 15:31:50 +0200995 if (!aaci) {
996 ret = -ENOMEM;
Russell Kingcb5a6ff2005-05-12 14:04:59 +0200997 goto out;
998 }
999
Linus Walleijdc890c22009-06-07 23:27:31 +01001000 aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001001 if (!aaci->base) {
1002 ret = -ENOMEM;
1003 goto out;
1004 }
1005
1006 /*
1007 * Playback uses AACI channel 0
1008 */
Russell Kingd6a89fe2009-12-18 17:48:50 +00001009 spin_lock_init(&aaci->playback.lock);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001010 aaci->playback.base = aaci->base + AACI_CSCH1;
1011 aaci->playback.fifo = aaci->base + AACI_DR1;
1012
Kevin Hilman41762b82007-02-07 05:45:32 +01001013 /*
1014 * Capture uses AACI channel 0
1015 */
Russell Kingd6a89fe2009-12-18 17:48:50 +00001016 spin_lock_init(&aaci->capture.lock);
Kevin Hilman41762b82007-02-07 05:45:32 +01001017 aaci->capture.base = aaci->base + AACI_CSCH1;
1018 aaci->capture.fifo = aaci->base + AACI_DR1;
1019
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001020 for (i = 0; i < 4; i++) {
viro@ZenIV.linux.org.uke12ba642005-09-06 02:06:57 +01001021 void __iomem *base = aaci->base + i * 0x14;
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001022
1023 writel(0, base + AACI_IE);
1024 writel(0, base + AACI_TXCR);
1025 writel(0, base + AACI_RXCR);
1026 }
1027
1028 writel(0x1fff, aaci->base + AACI_INTCLR);
1029 writel(aaci->maincr, aaci->base + AACI_MAINCR);
Philby Johnb68b58f2010-03-26 21:37:51 +05301030 /*
1031 * Fix: ac97 read back fail errors by reading
1032 * from any arbitrary aaci register.
1033 */
1034 readl(aaci->base + AACI_CSCH1);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001035 ret = aaci_probe_ac97(aaci);
1036 if (ret)
1037 goto out;
1038
Catalin Marinasf27f2182006-02-01 19:25:58 +00001039 /*
1040 * Size the FIFOs (must be multiple of 16).
Russell King5d350cb2011-01-13 22:25:10 +00001041 * This is the number of entries in the FIFO.
Catalin Marinasf27f2182006-02-01 19:25:58 +00001042 */
Russell King5d350cb2011-01-13 22:25:10 +00001043 aaci->fifo_depth = aaci_size_fifo(aaci);
1044 if (aaci->fifo_depth & 15) {
1045 printk(KERN_WARNING "AACI: FIFO depth %d not supported\n",
1046 aaci->fifo_depth);
Catalin Marinasf27f2182006-02-01 19:25:58 +00001047 ret = -ENODEV;
1048 goto out;
1049 }
1050
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001051 ret = aaci_init_pcm(aaci);
1052 if (ret)
1053 goto out;
1054
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001055 ret = snd_card_register(aaci->card);
1056 if (ret == 0) {
Russell King5d350cb2011-01-13 22:25:10 +00001057 dev_info(&dev->dev, "%s\n", aaci->card->longname);
1058 dev_info(&dev->dev, "FIFO %u entries\n", aaci->fifo_depth);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001059 amba_set_drvdata(dev, aaci->card);
1060 return ret;
1061 }
1062
1063 out:
1064 if (aaci)
1065 snd_card_free(aaci->card);
1066 amba_release_regions(dev);
1067 return ret;
1068}
1069
Bill Pemberton6c9dc192012-12-06 12:35:11 -05001070static int aaci_remove(struct amba_device *dev)
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001071{
Takashi Iwaiceb9e472005-11-17 15:10:16 +01001072 struct snd_card *card = amba_get_drvdata(dev);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001073
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001074 if (card) {
1075 struct aaci *aaci = card->private_data;
1076 writel(0, aaci->base + AACI_MAINCR);
1077
1078 snd_card_free(card);
1079 amba_release_regions(dev);
1080 }
1081
1082 return 0;
1083}
1084
1085static struct amba_id aaci_ids[] = {
1086 {
1087 .id = 0x00041041,
1088 .mask = 0x000fffff,
1089 },
1090 { 0, 0 },
1091};
1092
Dave Martin9d5c6272011-10-05 15:50:36 +01001093MODULE_DEVICE_TABLE(amba, aaci_ids);
1094
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001095static struct amba_driver aaci_driver = {
1096 .drv = {
1097 .name = DRIVER_NAME,
Ulf Hanssonb13a7142013-12-03 11:04:26 +01001098 .pm = AACI_DEV_PM_OPS,
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001099 },
1100 .probe = aaci_probe,
Bill Pemberton6c9dc192012-12-06 12:35:11 -05001101 .remove = aaci_remove,
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001102 .id_table = aaci_ids,
1103};
1104
viresh kumar9e5ed092012-03-15 10:40:38 +01001105module_amba_driver(aaci_driver);
Russell Kingcb5a6ff2005-05-12 14:04:59 +02001106
1107MODULE_LICENSE("GPL");
1108MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");