aaci.c 25 KB

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