aaci.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/sound/arm/aaci.c - ARM PrimeCell AACI PL041 driver
  4. *
  5. * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
  6. *
  7. * 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>
  17. #include <linux/amba/bus.h>
  18. #include <linux/io.h>
  19. #include <sound/core.h>
  20. #include <sound/initval.h>
  21. #include <sound/ac97_codec.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include "aaci.h"
  25. #define DRIVER_NAME "aaci-pl041"
  26. #define FRAME_PERIOD_US 21
  27. /*
  28. * PM support is not complete. Turn it off.
  29. */
  30. #undef CONFIG_PM
  31. static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
  32. {
  33. u32 v, maincr = aaci->maincr | MAINCR_SCRA(ac97->num);
  34. /*
  35. * Ensure that the slot 1/2 RX registers are empty.
  36. */
  37. v = readl(aaci->base + AACI_SLFR);
  38. if (v & SLFR_2RXV)
  39. readl(aaci->base + AACI_SL2RX);
  40. if (v & SLFR_1RXV)
  41. readl(aaci->base + AACI_SL1RX);
  42. if (maincr != readl(aaci->base + AACI_MAINCR)) {
  43. writel(maincr, aaci->base + AACI_MAINCR);
  44. readl(aaci->base + AACI_MAINCR);
  45. udelay(1);
  46. }
  47. }
  48. /*
  49. * P29:
  50. * The recommended use of programming the external codec through slot 1
  51. * and slot 2 data is to use the channels during setup routines and the
  52. * slot register at any other time. The data written into slot 1, slot 2
  53. * and slot 12 registers is transmitted only when their corresponding
  54. * SI1TxEn, SI2TxEn and SI12TxEn bits are set in the AACI_MAINCR
  55. * register.
  56. */
  57. static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  58. unsigned short val)
  59. {
  60. struct aaci *aaci = ac97->private_data;
  61. int timeout;
  62. u32 v;
  63. if (ac97->num >= 4)
  64. return;
  65. mutex_lock(&aaci->ac97_sem);
  66. aaci_ac97_select_codec(aaci, ac97);
  67. /*
  68. * P54: You must ensure that AACI_SL2TX is always written
  69. * to, if required, before data is written to AACI_SL1TX.
  70. */
  71. writel(val << 4, aaci->base + AACI_SL2TX);
  72. writel(reg << 12, aaci->base + AACI_SL1TX);
  73. /* Initially, wait one frame period */
  74. udelay(FRAME_PERIOD_US);
  75. /* And then wait an additional eight frame periods for it to be sent */
  76. timeout = FRAME_PERIOD_US * 8;
  77. do {
  78. udelay(1);
  79. v = readl(aaci->base + AACI_SLFR);
  80. } while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout);
  81. if (v & (SLFR_1TXB|SLFR_2TXB))
  82. dev_err(&aaci->dev->dev,
  83. "timeout waiting for write to complete\n");
  84. mutex_unlock(&aaci->ac97_sem);
  85. }
  86. /*
  87. * Read an AC'97 register.
  88. */
  89. static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  90. {
  91. struct aaci *aaci = ac97->private_data;
  92. int timeout, retries = 10;
  93. u32 v;
  94. if (ac97->num >= 4)
  95. return ~0;
  96. mutex_lock(&aaci->ac97_sem);
  97. aaci_ac97_select_codec(aaci, ac97);
  98. /*
  99. * Write the register address to slot 1.
  100. */
  101. writel((reg << 12) | (1 << 19), aaci->base + AACI_SL1TX);
  102. /* Initially, wait one frame period */
  103. udelay(FRAME_PERIOD_US);
  104. /* And then wait an additional eight frame periods for it to be sent */
  105. timeout = FRAME_PERIOD_US * 8;
  106. do {
  107. udelay(1);
  108. v = readl(aaci->base + AACI_SLFR);
  109. } while ((v & SLFR_1TXB) && --timeout);
  110. if (v & SLFR_1TXB) {
  111. dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
  112. v = ~0;
  113. goto out;
  114. }
  115. /* Now wait for the response frame */
  116. udelay(FRAME_PERIOD_US);
  117. /* And then wait an additional eight frame periods for data */
  118. timeout = FRAME_PERIOD_US * 8;
  119. do {
  120. udelay(1);
  121. cond_resched();
  122. v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV);
  123. } while ((v != (SLFR_1RXV|SLFR_2RXV)) && --timeout);
  124. if (v != (SLFR_1RXV|SLFR_2RXV)) {
  125. dev_err(&aaci->dev->dev, "timeout on RX valid\n");
  126. v = ~0;
  127. goto out;
  128. }
  129. do {
  130. v = readl(aaci->base + AACI_SL1RX) >> 12;
  131. if (v == reg) {
  132. v = readl(aaci->base + AACI_SL2RX) >> 4;
  133. break;
  134. } else if (--retries) {
  135. dev_warn(&aaci->dev->dev,
  136. "ac97 read back fail. retry\n");
  137. continue;
  138. } else {
  139. dev_warn(&aaci->dev->dev,
  140. "wrong ac97 register read back (%x != %x)\n",
  141. v, reg);
  142. v = ~0;
  143. }
  144. } while (retries);
  145. out:
  146. mutex_unlock(&aaci->ac97_sem);
  147. return v;
  148. }
  149. static inline void
  150. aaci_chan_wait_ready(struct aaci_runtime *aacirun, unsigned long mask)
  151. {
  152. u32 val;
  153. int timeout = 5000;
  154. do {
  155. udelay(1);
  156. val = readl(aacirun->base + AACI_SR);
  157. } while (val & mask && timeout--);
  158. }
  159. /*
  160. * Interrupt support.
  161. */
  162. static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
  163. {
  164. if (mask & ISR_ORINTR) {
  165. dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel);
  166. writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR);
  167. }
  168. if (mask & ISR_RXTOINTR) {
  169. dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel);
  170. writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR);
  171. }
  172. if (mask & ISR_RXINTR) {
  173. struct aaci_runtime *aacirun = &aaci->capture;
  174. bool period_elapsed = false;
  175. void *ptr;
  176. if (!aacirun->substream || !aacirun->start) {
  177. dev_warn(&aaci->dev->dev, "RX interrupt???\n");
  178. writel(0, aacirun->base + AACI_IE);
  179. return;
  180. }
  181. spin_lock(&aacirun->lock);
  182. ptr = aacirun->ptr;
  183. do {
  184. unsigned int len = aacirun->fifo_bytes;
  185. u32 val;
  186. if (aacirun->bytes <= 0) {
  187. aacirun->bytes += aacirun->period;
  188. period_elapsed = true;
  189. }
  190. if (!(aacirun->cr & CR_EN))
  191. break;
  192. val = readl(aacirun->base + AACI_SR);
  193. if (!(val & SR_RXHF))
  194. break;
  195. if (!(val & SR_RXFF))
  196. len >>= 1;
  197. aacirun->bytes -= len;
  198. /* reading 16 bytes at a time */
  199. for( ; len > 0; len -= 16) {
  200. asm(
  201. "ldmia %1, {r0, r1, r2, r3}\n\t"
  202. "stmia %0!, {r0, r1, r2, r3}"
  203. : "+r" (ptr)
  204. : "r" (aacirun->fifo)
  205. : "r0", "r1", "r2", "r3", "cc");
  206. if (ptr >= aacirun->end)
  207. ptr = aacirun->start;
  208. }
  209. } while(1);
  210. aacirun->ptr = ptr;
  211. spin_unlock(&aacirun->lock);
  212. if (period_elapsed)
  213. snd_pcm_period_elapsed(aacirun->substream);
  214. }
  215. if (mask & ISR_URINTR) {
  216. dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
  217. writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
  218. }
  219. if (mask & ISR_TXINTR) {
  220. struct aaci_runtime *aacirun = &aaci->playback;
  221. bool period_elapsed = false;
  222. void *ptr;
  223. if (!aacirun->substream || !aacirun->start) {
  224. dev_warn(&aaci->dev->dev, "TX interrupt???\n");
  225. writel(0, aacirun->base + AACI_IE);
  226. return;
  227. }
  228. spin_lock(&aacirun->lock);
  229. ptr = aacirun->ptr;
  230. do {
  231. unsigned int len = aacirun->fifo_bytes;
  232. u32 val;
  233. if (aacirun->bytes <= 0) {
  234. aacirun->bytes += aacirun->period;
  235. period_elapsed = true;
  236. }
  237. if (!(aacirun->cr & CR_EN))
  238. break;
  239. val = readl(aacirun->base + AACI_SR);
  240. if (!(val & SR_TXHE))
  241. break;
  242. if (!(val & SR_TXFE))
  243. len >>= 1;
  244. aacirun->bytes -= len;
  245. /* writing 16 bytes at a time */
  246. for ( ; len > 0; len -= 16) {
  247. asm(
  248. "ldmia %0!, {r0, r1, r2, r3}\n\t"
  249. "stmia %1, {r0, r1, r2, r3}"
  250. : "+r" (ptr)
  251. : "r" (aacirun->fifo)
  252. : "r0", "r1", "r2", "r3", "cc");
  253. if (ptr >= aacirun->end)
  254. ptr = aacirun->start;
  255. }
  256. } while (1);
  257. aacirun->ptr = ptr;
  258. spin_unlock(&aacirun->lock);
  259. if (period_elapsed)
  260. snd_pcm_period_elapsed(aacirun->substream);
  261. }
  262. }
  263. static irqreturn_t aaci_irq(int irq, void *devid)
  264. {
  265. struct aaci *aaci = devid;
  266. u32 mask;
  267. int i;
  268. mask = readl(aaci->base + AACI_ALLINTS);
  269. if (mask) {
  270. u32 m = mask;
  271. for (i = 0; i < 4; i++, m >>= 7) {
  272. if (m & 0x7f) {
  273. aaci_fifo_irq(aaci, i, m);
  274. }
  275. }
  276. }
  277. return mask ? IRQ_HANDLED : IRQ_NONE;
  278. }
  279. /*
  280. * ALSA support.
  281. */
  282. static const struct snd_pcm_hardware aaci_hw_info = {
  283. .info = SNDRV_PCM_INFO_MMAP |
  284. SNDRV_PCM_INFO_MMAP_VALID |
  285. SNDRV_PCM_INFO_INTERLEAVED |
  286. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  287. SNDRV_PCM_INFO_RESUME,
  288. /*
  289. * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
  290. * words. It also doesn't support 12-bit at all.
  291. */
  292. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  293. /* rates are setup from the AC'97 codec */
  294. .channels_min = 2,
  295. .channels_max = 2,
  296. .buffer_bytes_max = 64 * 1024,
  297. .period_bytes_min = 256,
  298. .period_bytes_max = PAGE_SIZE,
  299. .periods_min = 4,
  300. .periods_max = PAGE_SIZE / 16,
  301. };
  302. /*
  303. * We can support two and four channel audio. Unfortunately
  304. * six channel audio requires a non-standard channel ordering:
  305. * 2 -> FL(3), FR(4)
  306. * 4 -> FL(3), FR(4), SL(7), SR(8)
  307. * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
  308. * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
  309. * This requires an ALSA configuration file to correct.
  310. */
  311. static int aaci_rule_channels(struct snd_pcm_hw_params *p,
  312. struct snd_pcm_hw_rule *rule)
  313. {
  314. static const unsigned int channel_list[] = { 2, 4, 6 };
  315. struct aaci *aaci = rule->private;
  316. unsigned int mask = 1 << 0, slots;
  317. /* pcms[0] is the our 5.1 PCM instance. */
  318. slots = aaci->ac97_bus->pcms[0].r[0].slots;
  319. if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
  320. mask |= 1 << 1;
  321. if (slots & (1 << AC97_SLOT_LFE))
  322. mask |= 1 << 2;
  323. }
  324. return snd_interval_list(hw_param_interval(p, rule->var),
  325. ARRAY_SIZE(channel_list), channel_list, mask);
  326. }
  327. static int aaci_pcm_open(struct snd_pcm_substream *substream)
  328. {
  329. struct snd_pcm_runtime *runtime = substream->runtime;
  330. struct aaci *aaci = substream->private_data;
  331. struct aaci_runtime *aacirun;
  332. int ret = 0;
  333. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  334. aacirun = &aaci->playback;
  335. } else {
  336. aacirun = &aaci->capture;
  337. }
  338. aacirun->substream = substream;
  339. runtime->private_data = aacirun;
  340. runtime->hw = aaci_hw_info;
  341. runtime->hw.rates = aacirun->pcm->rates;
  342. snd_pcm_limit_hw_rates(runtime);
  343. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  344. runtime->hw.channels_max = 6;
  345. /* Add rule describing channel dependency. */
  346. ret = snd_pcm_hw_rule_add(substream->runtime, 0,
  347. SNDRV_PCM_HW_PARAM_CHANNELS,
  348. aaci_rule_channels, aaci,
  349. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  350. if (ret)
  351. return ret;
  352. if (aacirun->pcm->r[1].slots)
  353. snd_ac97_pcm_double_rate_rules(runtime);
  354. }
  355. /*
  356. * ALSA wants the byte-size of the FIFOs. As we only support
  357. * 16-bit samples, this is twice the FIFO depth irrespective
  358. * of whether it's in compact mode or not.
  359. */
  360. runtime->hw.fifo_size = aaci->fifo_depth * 2;
  361. mutex_lock(&aaci->irq_lock);
  362. if (!aaci->users++) {
  363. ret = request_irq(aaci->dev->irq[0], aaci_irq,
  364. IRQF_SHARED, DRIVER_NAME, aaci);
  365. if (ret != 0)
  366. aaci->users--;
  367. }
  368. mutex_unlock(&aaci->irq_lock);
  369. return ret;
  370. }
  371. /*
  372. * Common ALSA stuff
  373. */
  374. static int aaci_pcm_close(struct snd_pcm_substream *substream)
  375. {
  376. struct aaci *aaci = substream->private_data;
  377. struct aaci_runtime *aacirun = substream->runtime->private_data;
  378. WARN_ON(aacirun->cr & CR_EN);
  379. aacirun->substream = NULL;
  380. mutex_lock(&aaci->irq_lock);
  381. if (!--aaci->users)
  382. free_irq(aaci->dev->irq[0], aaci);
  383. mutex_unlock(&aaci->irq_lock);
  384. return 0;
  385. }
  386. static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
  387. {
  388. struct aaci_runtime *aacirun = substream->runtime->private_data;
  389. /*
  390. * This must not be called with the device enabled.
  391. */
  392. WARN_ON(aacirun->cr & CR_EN);
  393. if (aacirun->pcm_open)
  394. snd_ac97_pcm_close(aacirun->pcm);
  395. aacirun->pcm_open = 0;
  396. return 0;
  397. }
  398. /* Channel to slot mask */
  399. static const u32 channels_to_slotmask[] = {
  400. [2] = CR_SL3 | CR_SL4,
  401. [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
  402. [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
  403. };
  404. static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
  405. struct snd_pcm_hw_params *params)
  406. {
  407. struct aaci_runtime *aacirun = substream->runtime->private_data;
  408. struct aaci *aaci = substream->private_data;
  409. unsigned int channels = params_channels(params);
  410. unsigned int rate = params_rate(params);
  411. int dbl = rate > 48000;
  412. int err;
  413. aaci_pcm_hw_free(substream);
  414. if (aacirun->pcm_open) {
  415. snd_ac97_pcm_close(aacirun->pcm);
  416. aacirun->pcm_open = 0;
  417. }
  418. /* channels is already limited to 2, 4, or 6 by aaci_rule_channels */
  419. if (dbl && channels != 2)
  420. return -EINVAL;
  421. err = snd_ac97_pcm_open(aacirun->pcm, rate, channels,
  422. aacirun->pcm->r[dbl].slots);
  423. aacirun->pcm_open = err == 0;
  424. aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
  425. aacirun->cr |= channels_to_slotmask[channels + dbl * 2];
  426. /*
  427. * fifo_bytes is the number of bytes we transfer to/from
  428. * the FIFO, including padding. So that's x4. As we're
  429. * in compact mode, the FIFO is half the size.
  430. */
  431. aacirun->fifo_bytes = aaci->fifo_depth * 4 / 2;
  432. return err;
  433. }
  434. static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
  435. {
  436. struct snd_pcm_runtime *runtime = substream->runtime;
  437. struct aaci_runtime *aacirun = runtime->private_data;
  438. aacirun->period = snd_pcm_lib_period_bytes(substream);
  439. aacirun->start = runtime->dma_area;
  440. aacirun->end = aacirun->start + snd_pcm_lib_buffer_bytes(substream);
  441. aacirun->ptr = aacirun->start;
  442. aacirun->bytes = aacirun->period;
  443. return 0;
  444. }
  445. static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
  446. {
  447. struct snd_pcm_runtime *runtime = substream->runtime;
  448. struct aaci_runtime *aacirun = runtime->private_data;
  449. ssize_t bytes = aacirun->ptr - aacirun->start;
  450. return bytes_to_frames(runtime, bytes);
  451. }
  452. /*
  453. * Playback specific ALSA stuff
  454. */
  455. static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
  456. {
  457. u32 ie;
  458. ie = readl(aacirun->base + AACI_IE);
  459. ie &= ~(IE_URIE|IE_TXIE);
  460. writel(ie, aacirun->base + AACI_IE);
  461. aacirun->cr &= ~CR_EN;
  462. aaci_chan_wait_ready(aacirun, SR_TXB);
  463. writel(aacirun->cr, aacirun->base + AACI_TXCR);
  464. }
  465. static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
  466. {
  467. u32 ie;
  468. aaci_chan_wait_ready(aacirun, SR_TXB);
  469. aacirun->cr |= CR_EN;
  470. ie = readl(aacirun->base + AACI_IE);
  471. ie |= IE_URIE | IE_TXIE;
  472. writel(ie, aacirun->base + AACI_IE);
  473. writel(aacirun->cr, aacirun->base + AACI_TXCR);
  474. }
  475. static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  476. {
  477. struct aaci_runtime *aacirun = substream->runtime->private_data;
  478. unsigned long flags;
  479. int ret = 0;
  480. spin_lock_irqsave(&aacirun->lock, flags);
  481. switch (cmd) {
  482. case SNDRV_PCM_TRIGGER_START:
  483. aaci_pcm_playback_start(aacirun);
  484. break;
  485. case SNDRV_PCM_TRIGGER_RESUME:
  486. aaci_pcm_playback_start(aacirun);
  487. break;
  488. case SNDRV_PCM_TRIGGER_STOP:
  489. aaci_pcm_playback_stop(aacirun);
  490. break;
  491. case SNDRV_PCM_TRIGGER_SUSPEND:
  492. aaci_pcm_playback_stop(aacirun);
  493. break;
  494. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  495. break;
  496. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  497. break;
  498. default:
  499. ret = -EINVAL;
  500. }
  501. spin_unlock_irqrestore(&aacirun->lock, flags);
  502. return ret;
  503. }
  504. static const struct snd_pcm_ops aaci_playback_ops = {
  505. .open = aaci_pcm_open,
  506. .close = aaci_pcm_close,
  507. .hw_params = aaci_pcm_hw_params,
  508. .hw_free = aaci_pcm_hw_free,
  509. .prepare = aaci_pcm_prepare,
  510. .trigger = aaci_pcm_playback_trigger,
  511. .pointer = aaci_pcm_pointer,
  512. };
  513. static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
  514. {
  515. u32 ie;
  516. aaci_chan_wait_ready(aacirun, SR_RXB);
  517. ie = readl(aacirun->base + AACI_IE);
  518. ie &= ~(IE_ORIE | IE_RXIE);
  519. writel(ie, aacirun->base+AACI_IE);
  520. aacirun->cr &= ~CR_EN;
  521. writel(aacirun->cr, aacirun->base + AACI_RXCR);
  522. }
  523. static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
  524. {
  525. u32 ie;
  526. aaci_chan_wait_ready(aacirun, SR_RXB);
  527. #ifdef DEBUG
  528. /* RX Timeout value: bits 28:17 in RXCR */
  529. aacirun->cr |= 0xf << 17;
  530. #endif
  531. aacirun->cr |= CR_EN;
  532. writel(aacirun->cr, aacirun->base + AACI_RXCR);
  533. ie = readl(aacirun->base + AACI_IE);
  534. ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
  535. writel(ie, aacirun->base + AACI_IE);
  536. }
  537. static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  538. {
  539. struct aaci_runtime *aacirun = substream->runtime->private_data;
  540. unsigned long flags;
  541. int ret = 0;
  542. spin_lock_irqsave(&aacirun->lock, flags);
  543. switch (cmd) {
  544. case SNDRV_PCM_TRIGGER_START:
  545. aaci_pcm_capture_start(aacirun);
  546. break;
  547. case SNDRV_PCM_TRIGGER_RESUME:
  548. aaci_pcm_capture_start(aacirun);
  549. break;
  550. case SNDRV_PCM_TRIGGER_STOP:
  551. aaci_pcm_capture_stop(aacirun);
  552. break;
  553. case SNDRV_PCM_TRIGGER_SUSPEND:
  554. aaci_pcm_capture_stop(aacirun);
  555. break;
  556. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  557. break;
  558. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  559. break;
  560. default:
  561. ret = -EINVAL;
  562. }
  563. spin_unlock_irqrestore(&aacirun->lock, flags);
  564. return ret;
  565. }
  566. static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
  567. {
  568. struct snd_pcm_runtime *runtime = substream->runtime;
  569. struct aaci *aaci = substream->private_data;
  570. aaci_pcm_prepare(substream);
  571. /* allow changing of sample rate */
  572. aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
  573. aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  574. aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
  575. /* Record select: Mic: 0, Aux: 3, Line: 4 */
  576. aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
  577. return 0;
  578. }
  579. static const struct snd_pcm_ops aaci_capture_ops = {
  580. .open = aaci_pcm_open,
  581. .close = aaci_pcm_close,
  582. .hw_params = aaci_pcm_hw_params,
  583. .hw_free = aaci_pcm_hw_free,
  584. .prepare = aaci_pcm_capture_prepare,
  585. .trigger = aaci_pcm_capture_trigger,
  586. .pointer = aaci_pcm_pointer,
  587. };
  588. /*
  589. * Power Management.
  590. */
  591. static int aaci_do_suspend(struct snd_card *card)
  592. {
  593. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  594. return 0;
  595. }
  596. static int aaci_do_resume(struct snd_card *card)
  597. {
  598. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  599. return 0;
  600. }
  601. static int aaci_suspend(struct device *dev)
  602. {
  603. struct snd_card *card = dev_get_drvdata(dev);
  604. return card ? aaci_do_suspend(card) : 0;
  605. }
  606. static int aaci_resume(struct device *dev)
  607. {
  608. struct snd_card *card = dev_get_drvdata(dev);
  609. return card ? aaci_do_resume(card) : 0;
  610. }
  611. static DEFINE_SIMPLE_DEV_PM_OPS(aaci_dev_pm_ops, aaci_suspend, aaci_resume);
  612. static const struct ac97_pcm ac97_defs[] = {
  613. [0] = { /* Front PCM */
  614. .exclusive = 1,
  615. .r = {
  616. [0] = {
  617. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  618. (1 << AC97_SLOT_PCM_RIGHT) |
  619. (1 << AC97_SLOT_PCM_CENTER) |
  620. (1 << AC97_SLOT_PCM_SLEFT) |
  621. (1 << AC97_SLOT_PCM_SRIGHT) |
  622. (1 << AC97_SLOT_LFE),
  623. },
  624. [1] = {
  625. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  626. (1 << AC97_SLOT_PCM_RIGHT) |
  627. (1 << AC97_SLOT_PCM_LEFT_0) |
  628. (1 << AC97_SLOT_PCM_RIGHT_0),
  629. },
  630. },
  631. },
  632. [1] = { /* PCM in */
  633. .stream = 1,
  634. .exclusive = 1,
  635. .r = {
  636. [0] = {
  637. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  638. (1 << AC97_SLOT_PCM_RIGHT),
  639. },
  640. },
  641. },
  642. [2] = { /* Mic in */
  643. .stream = 1,
  644. .exclusive = 1,
  645. .r = {
  646. [0] = {
  647. .slots = (1 << AC97_SLOT_MIC),
  648. },
  649. },
  650. }
  651. };
  652. static const struct snd_ac97_bus_ops aaci_bus_ops = {
  653. .write = aaci_ac97_write,
  654. .read = aaci_ac97_read,
  655. };
  656. static int aaci_probe_ac97(struct aaci *aaci)
  657. {
  658. struct snd_ac97_template ac97_template;
  659. struct snd_ac97_bus *ac97_bus;
  660. struct snd_ac97 *ac97;
  661. int ret;
  662. /*
  663. * Assert AACIRESET for 2us
  664. */
  665. writel(0, aaci->base + AACI_RESET);
  666. udelay(2);
  667. writel(RESET_NRST, aaci->base + AACI_RESET);
  668. /*
  669. * Give the AC'97 codec more than enough time
  670. * to wake up. (42us = ~2 frames at 48kHz.)
  671. */
  672. udelay(FRAME_PERIOD_US * 2);
  673. ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
  674. if (ret)
  675. goto out;
  676. ac97_bus->clock = 48000;
  677. aaci->ac97_bus = ac97_bus;
  678. memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
  679. ac97_template.private_data = aaci;
  680. ac97_template.num = 0;
  681. ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
  682. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
  683. if (ret)
  684. goto out;
  685. aaci->ac97 = ac97;
  686. /*
  687. * Disable AC97 PC Beep input on audio codecs.
  688. */
  689. if (ac97_is_audio(ac97))
  690. snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
  691. ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
  692. if (ret)
  693. goto out;
  694. aaci->playback.pcm = &ac97_bus->pcms[0];
  695. aaci->capture.pcm = &ac97_bus->pcms[1];
  696. out:
  697. return ret;
  698. }
  699. static void aaci_free_card(struct snd_card *card)
  700. {
  701. struct aaci *aaci = card->private_data;
  702. iounmap(aaci->base);
  703. }
  704. static struct aaci *aaci_init_card(struct amba_device *dev)
  705. {
  706. struct aaci *aaci;
  707. struct snd_card *card;
  708. int err;
  709. err = snd_card_new(&dev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  710. THIS_MODULE, sizeof(struct aaci), &card);
  711. if (err < 0)
  712. return NULL;
  713. card->private_free = aaci_free_card;
  714. strscpy(card->driver, DRIVER_NAME, sizeof(card->driver));
  715. strscpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
  716. snprintf(card->longname, sizeof(card->longname),
  717. "%s PL%03x rev%u at 0x%08llx, irq %d",
  718. card->shortname, amba_part(dev), amba_rev(dev),
  719. (unsigned long long)dev->res.start, dev->irq[0]);
  720. aaci = card->private_data;
  721. mutex_init(&aaci->ac97_sem);
  722. mutex_init(&aaci->irq_lock);
  723. aaci->card = card;
  724. aaci->dev = dev;
  725. /* Set MAINCR to allow slot 1 and 2 data IO */
  726. aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
  727. MAINCR_SL2RXEN | MAINCR_SL2TXEN;
  728. return aaci;
  729. }
  730. static int aaci_init_pcm(struct aaci *aaci)
  731. {
  732. struct snd_pcm *pcm;
  733. int ret;
  734. ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
  735. if (ret == 0) {
  736. aaci->pcm = pcm;
  737. pcm->private_data = aaci;
  738. pcm->info_flags = 0;
  739. strscpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
  740. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
  741. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
  742. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
  743. aaci->card->dev,
  744. 0, 64 * 1024);
  745. }
  746. return ret;
  747. }
  748. static unsigned int aaci_size_fifo(struct aaci *aaci)
  749. {
  750. struct aaci_runtime *aacirun = &aaci->playback;
  751. int i;
  752. /*
  753. * Enable the channel, but don't assign it to any slots, so
  754. * it won't empty onto the AC'97 link.
  755. */
  756. writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
  757. for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
  758. writel(0, aacirun->fifo);
  759. writel(0, aacirun->base + AACI_TXCR);
  760. /*
  761. * Re-initialise the AACI after the FIFO depth test, to
  762. * ensure that the FIFOs are empty. Unfortunately, merely
  763. * disabling the channel doesn't clear the FIFO.
  764. */
  765. writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
  766. readl(aaci->base + AACI_MAINCR);
  767. udelay(1);
  768. writel(aaci->maincr, aaci->base + AACI_MAINCR);
  769. /*
  770. * If we hit 4096 entries, we failed. Go back to the specified
  771. * fifo depth.
  772. */
  773. if (i == 4096)
  774. i = 8;
  775. return i;
  776. }
  777. static int aaci_probe(struct amba_device *dev,
  778. const struct amba_id *id)
  779. {
  780. struct aaci *aaci;
  781. int ret, i;
  782. ret = amba_request_regions(dev, NULL);
  783. if (ret)
  784. return ret;
  785. aaci = aaci_init_card(dev);
  786. if (!aaci) {
  787. ret = -ENOMEM;
  788. goto out;
  789. }
  790. aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
  791. if (!aaci->base) {
  792. ret = -ENOMEM;
  793. goto out;
  794. }
  795. /*
  796. * Playback uses AACI channel 0
  797. */
  798. spin_lock_init(&aaci->playback.lock);
  799. aaci->playback.base = aaci->base + AACI_CSCH1;
  800. aaci->playback.fifo = aaci->base + AACI_DR1;
  801. /*
  802. * Capture uses AACI channel 0
  803. */
  804. spin_lock_init(&aaci->capture.lock);
  805. aaci->capture.base = aaci->base + AACI_CSCH1;
  806. aaci->capture.fifo = aaci->base + AACI_DR1;
  807. for (i = 0; i < 4; i++) {
  808. void __iomem *base = aaci->base + i * 0x14;
  809. writel(0, base + AACI_IE);
  810. writel(0, base + AACI_TXCR);
  811. writel(0, base + AACI_RXCR);
  812. }
  813. writel(0x1fff, aaci->base + AACI_INTCLR);
  814. writel(aaci->maincr, aaci->base + AACI_MAINCR);
  815. /*
  816. * Fix: ac97 read back fail errors by reading
  817. * from any arbitrary aaci register.
  818. */
  819. readl(aaci->base + AACI_CSCH1);
  820. ret = aaci_probe_ac97(aaci);
  821. if (ret)
  822. goto out;
  823. /*
  824. * Size the FIFOs (must be multiple of 16).
  825. * This is the number of entries in the FIFO.
  826. */
  827. aaci->fifo_depth = aaci_size_fifo(aaci);
  828. if (aaci->fifo_depth & 15) {
  829. printk(KERN_WARNING "AACI: FIFO depth %d not supported\n",
  830. aaci->fifo_depth);
  831. ret = -ENODEV;
  832. goto out;
  833. }
  834. ret = aaci_init_pcm(aaci);
  835. if (ret)
  836. goto out;
  837. ret = snd_card_register(aaci->card);
  838. if (ret == 0) {
  839. dev_info(&dev->dev, "%s\n", aaci->card->longname);
  840. dev_info(&dev->dev, "FIFO %u entries\n", aaci->fifo_depth);
  841. amba_set_drvdata(dev, aaci->card);
  842. return ret;
  843. }
  844. out:
  845. if (aaci)
  846. snd_card_free(aaci->card);
  847. amba_release_regions(dev);
  848. return ret;
  849. }
  850. static void aaci_remove(struct amba_device *dev)
  851. {
  852. struct snd_card *card = amba_get_drvdata(dev);
  853. if (card) {
  854. struct aaci *aaci = card->private_data;
  855. writel(0, aaci->base + AACI_MAINCR);
  856. snd_card_free(card);
  857. amba_release_regions(dev);
  858. }
  859. }
  860. static struct amba_id aaci_ids[] = {
  861. {
  862. .id = 0x00041041,
  863. .mask = 0x000fffff,
  864. },
  865. { 0, 0 },
  866. };
  867. MODULE_DEVICE_TABLE(amba, aaci_ids);
  868. static struct amba_driver aaci_driver = {
  869. .drv = {
  870. .name = DRIVER_NAME,
  871. .pm = &aaci_dev_pm_ops,
  872. },
  873. .probe = aaci_probe,
  874. .remove = aaci_remove,
  875. .id_table = aaci_ids,
  876. };
  877. module_amba_driver(aaci_driver);
  878. MODULE_LICENSE("GPL");
  879. MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");