aaci.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. void *ptr;
  178. if (!aacirun->substream || !aacirun->start) {
  179. dev_warn(&aaci->dev->dev, "RX interrupt???\n");
  180. writel(0, aacirun->base + AACI_IE);
  181. return;
  182. }
  183. spin_lock(&aacirun->lock);
  184. ptr = aacirun->ptr;
  185. do {
  186. unsigned int len = aacirun->fifosz;
  187. u32 val;
  188. if (aacirun->bytes <= 0) {
  189. aacirun->bytes += aacirun->period;
  190. aacirun->ptr = ptr;
  191. spin_unlock(&aacirun->lock);
  192. snd_pcm_period_elapsed(aacirun->substream);
  193. spin_lock(&aacirun->lock);
  194. }
  195. if (!(aacirun->cr & CR_EN))
  196. break;
  197. val = readl(aacirun->base + AACI_SR);
  198. if (!(val & SR_RXHF))
  199. break;
  200. if (!(val & SR_RXFF))
  201. len >>= 1;
  202. aacirun->bytes -= len;
  203. /* reading 16 bytes at a time */
  204. for( ; len > 0; len -= 16) {
  205. asm(
  206. "ldmia %1, {r0, r1, r2, r3}\n\t"
  207. "stmia %0!, {r0, r1, r2, r3}"
  208. : "+r" (ptr)
  209. : "r" (aacirun->fifo)
  210. : "r0", "r1", "r2", "r3", "cc");
  211. if (ptr >= aacirun->end)
  212. ptr = aacirun->start;
  213. }
  214. } while(1);
  215. aacirun->ptr = ptr;
  216. spin_unlock(&aacirun->lock);
  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. void *ptr;
  225. if (!aacirun->substream || !aacirun->start) {
  226. dev_warn(&aaci->dev->dev, "TX interrupt???\n");
  227. writel(0, aacirun->base + AACI_IE);
  228. return;
  229. }
  230. spin_lock(&aacirun->lock);
  231. ptr = aacirun->ptr;
  232. do {
  233. unsigned int len = aacirun->fifosz;
  234. u32 val;
  235. if (aacirun->bytes <= 0) {
  236. aacirun->bytes += aacirun->period;
  237. aacirun->ptr = ptr;
  238. spin_unlock(&aacirun->lock);
  239. snd_pcm_period_elapsed(aacirun->substream);
  240. spin_lock(&aacirun->lock);
  241. }
  242. if (!(aacirun->cr & CR_EN))
  243. break;
  244. val = readl(aacirun->base + AACI_SR);
  245. if (!(val & SR_TXHE))
  246. break;
  247. if (!(val & SR_TXFE))
  248. len >>= 1;
  249. aacirun->bytes -= len;
  250. /* writing 16 bytes at a time */
  251. for ( ; len > 0; len -= 16) {
  252. asm(
  253. "ldmia %0!, {r0, r1, r2, r3}\n\t"
  254. "stmia %1, {r0, r1, r2, r3}"
  255. : "+r" (ptr)
  256. : "r" (aacirun->fifo)
  257. : "r0", "r1", "r2", "r3", "cc");
  258. if (ptr >= aacirun->end)
  259. ptr = aacirun->start;
  260. }
  261. } while (1);
  262. aacirun->ptr = ptr;
  263. spin_unlock(&aacirun->lock);
  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 = 6,
  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. static int __aaci_pcm_open(struct aaci *aaci,
  306. struct snd_pcm_substream *substream,
  307. struct aaci_runtime *aacirun)
  308. {
  309. struct snd_pcm_runtime *runtime = substream->runtime;
  310. int ret;
  311. aacirun->substream = substream;
  312. runtime->private_data = aacirun;
  313. runtime->hw = aaci_hw_info;
  314. runtime->hw.rates = aacirun->pcm->rates;
  315. snd_pcm_limit_hw_rates(runtime);
  316. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  317. aacirun->pcm->r[1].slots)
  318. snd_ac97_pcm_double_rate_rules(runtime);
  319. /*
  320. * FIXME: ALSA specifies fifo_size in bytes. If we're in normal
  321. * mode, each 32-bit word contains one sample. If we're in
  322. * compact mode, each 32-bit word contains two samples, effectively
  323. * halving the FIFO size. However, we don't know for sure which
  324. * we'll be using at this point. We set this to the lower limit.
  325. */
  326. runtime->hw.fifo_size = aaci->fifosize * 2;
  327. ret = request_irq(aaci->dev->irq[0], aaci_irq, IRQF_SHARED|IRQF_DISABLED,
  328. DRIVER_NAME, aaci);
  329. if (ret)
  330. goto out;
  331. return 0;
  332. out:
  333. return ret;
  334. }
  335. /*
  336. * Common ALSA stuff
  337. */
  338. static int aaci_pcm_close(struct snd_pcm_substream *substream)
  339. {
  340. struct aaci *aaci = substream->private_data;
  341. struct aaci_runtime *aacirun = substream->runtime->private_data;
  342. WARN_ON(aacirun->cr & CR_EN);
  343. aacirun->substream = NULL;
  344. free_irq(aaci->dev->irq[0], aaci);
  345. return 0;
  346. }
  347. static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
  348. {
  349. struct aaci_runtime *aacirun = substream->runtime->private_data;
  350. /*
  351. * This must not be called with the device enabled.
  352. */
  353. WARN_ON(aacirun->cr & CR_EN);
  354. if (aacirun->pcm_open)
  355. snd_ac97_pcm_close(aacirun->pcm);
  356. aacirun->pcm_open = 0;
  357. /*
  358. * Clear out the DMA and any allocated buffers.
  359. */
  360. snd_pcm_lib_free_pages(substream);
  361. return 0;
  362. }
  363. static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
  364. struct aaci_runtime *aacirun,
  365. struct snd_pcm_hw_params *params)
  366. {
  367. int err;
  368. struct aaci *aaci = substream->private_data;
  369. aaci_pcm_hw_free(substream);
  370. if (aacirun->pcm_open) {
  371. snd_ac97_pcm_close(aacirun->pcm);
  372. aacirun->pcm_open = 0;
  373. }
  374. err = snd_pcm_lib_malloc_pages(substream,
  375. params_buffer_bytes(params));
  376. if (err >= 0) {
  377. unsigned int rate = params_rate(params);
  378. int dbl = rate > 48000;
  379. err = snd_ac97_pcm_open(aacirun->pcm, rate,
  380. params_channels(params),
  381. aacirun->pcm->r[dbl].slots);
  382. aacirun->pcm_open = err == 0;
  383. aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
  384. aacirun->fifosz = aaci->fifosize * 4;
  385. if (aacirun->cr & CR_COMPACT)
  386. aacirun->fifosz >>= 1;
  387. }
  388. return err;
  389. }
  390. static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
  391. {
  392. struct snd_pcm_runtime *runtime = substream->runtime;
  393. struct aaci_runtime *aacirun = runtime->private_data;
  394. aacirun->start = runtime->dma_area;
  395. aacirun->end = aacirun->start + snd_pcm_lib_buffer_bytes(substream);
  396. aacirun->ptr = aacirun->start;
  397. aacirun->period =
  398. aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
  399. return 0;
  400. }
  401. static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
  402. {
  403. struct snd_pcm_runtime *runtime = substream->runtime;
  404. struct aaci_runtime *aacirun = runtime->private_data;
  405. ssize_t bytes = aacirun->ptr - aacirun->start;
  406. return bytes_to_frames(runtime, bytes);
  407. }
  408. /*
  409. * Playback specific ALSA stuff
  410. */
  411. static const u32 channels_to_txmask[] = {
  412. [2] = CR_SL3 | CR_SL4,
  413. [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
  414. [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
  415. };
  416. /*
  417. * We can support two and four channel audio. Unfortunately
  418. * six channel audio requires a non-standard channel ordering:
  419. * 2 -> FL(3), FR(4)
  420. * 4 -> FL(3), FR(4), SL(7), SR(8)
  421. * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
  422. * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
  423. * This requires an ALSA configuration file to correct.
  424. */
  425. static unsigned int channel_list[] = { 2, 4, 6 };
  426. static int
  427. aaci_rule_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
  428. {
  429. struct aaci *aaci = rule->private;
  430. unsigned int chan_mask = 1 << 0, slots;
  431. /*
  432. * pcms[0] is the our 5.1 PCM instance.
  433. */
  434. slots = aaci->ac97_bus->pcms[0].r[0].slots;
  435. if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
  436. chan_mask |= 1 << 1;
  437. if (slots & (1 << AC97_SLOT_LFE))
  438. chan_mask |= 1 << 2;
  439. }
  440. return snd_interval_list(hw_param_interval(p, rule->var),
  441. ARRAY_SIZE(channel_list), channel_list,
  442. chan_mask);
  443. }
  444. static int aaci_pcm_open(struct snd_pcm_substream *substream)
  445. {
  446. struct aaci *aaci = substream->private_data;
  447. int ret;
  448. /*
  449. * Add rule describing channel dependency.
  450. */
  451. ret = snd_pcm_hw_rule_add(substream->runtime, 0,
  452. SNDRV_PCM_HW_PARAM_CHANNELS,
  453. aaci_rule_channels, aaci,
  454. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  455. if (ret)
  456. return ret;
  457. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  458. ret = __aaci_pcm_open(aaci, substream, &aaci->playback);
  459. } else {
  460. ret = __aaci_pcm_open(aaci, substream, &aaci->capture);
  461. }
  462. return ret;
  463. }
  464. static int aaci_pcm_playback_hw_params(struct snd_pcm_substream *substream,
  465. struct snd_pcm_hw_params *params)
  466. {
  467. struct aaci_runtime *aacirun = substream->runtime->private_data;
  468. unsigned int channels = params_channels(params);
  469. int ret;
  470. WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) ||
  471. !channels_to_txmask[channels]);
  472. ret = aaci_pcm_hw_params(substream, aacirun, params);
  473. /*
  474. * Enable FIFO, compact mode, 16 bits per sample.
  475. * FIXME: double rate slots?
  476. */
  477. if (ret >= 0)
  478. aacirun->cr |= channels_to_txmask[channels];
  479. return ret;
  480. }
  481. static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
  482. {
  483. u32 ie;
  484. ie = readl(aacirun->base + AACI_IE);
  485. ie &= ~(IE_URIE|IE_TXIE);
  486. writel(ie, aacirun->base + AACI_IE);
  487. aacirun->cr &= ~CR_EN;
  488. aaci_chan_wait_ready(aacirun, SR_TXB);
  489. writel(aacirun->cr, aacirun->base + AACI_TXCR);
  490. }
  491. static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
  492. {
  493. u32 ie;
  494. aaci_chan_wait_ready(aacirun, SR_TXB);
  495. aacirun->cr |= CR_EN;
  496. ie = readl(aacirun->base + AACI_IE);
  497. ie |= IE_URIE | IE_TXIE;
  498. writel(ie, aacirun->base + AACI_IE);
  499. writel(aacirun->cr, aacirun->base + AACI_TXCR);
  500. }
  501. static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  502. {
  503. struct aaci_runtime *aacirun = substream->runtime->private_data;
  504. unsigned long flags;
  505. int ret = 0;
  506. spin_lock_irqsave(&aacirun->lock, flags);
  507. switch (cmd) {
  508. case SNDRV_PCM_TRIGGER_START:
  509. aaci_pcm_playback_start(aacirun);
  510. break;
  511. case SNDRV_PCM_TRIGGER_RESUME:
  512. aaci_pcm_playback_start(aacirun);
  513. break;
  514. case SNDRV_PCM_TRIGGER_STOP:
  515. aaci_pcm_playback_stop(aacirun);
  516. break;
  517. case SNDRV_PCM_TRIGGER_SUSPEND:
  518. aaci_pcm_playback_stop(aacirun);
  519. break;
  520. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  521. break;
  522. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  523. break;
  524. default:
  525. ret = -EINVAL;
  526. }
  527. spin_unlock_irqrestore(&aacirun->lock, flags);
  528. return ret;
  529. }
  530. static struct snd_pcm_ops aaci_playback_ops = {
  531. .open = aaci_pcm_open,
  532. .close = aaci_pcm_close,
  533. .ioctl = snd_pcm_lib_ioctl,
  534. .hw_params = aaci_pcm_playback_hw_params,
  535. .hw_free = aaci_pcm_hw_free,
  536. .prepare = aaci_pcm_prepare,
  537. .trigger = aaci_pcm_playback_trigger,
  538. .pointer = aaci_pcm_pointer,
  539. };
  540. static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream,
  541. struct snd_pcm_hw_params *params)
  542. {
  543. struct aaci_runtime *aacirun = substream->runtime->private_data;
  544. int ret;
  545. ret = aaci_pcm_hw_params(substream, aacirun, params);
  546. if (ret >= 0)
  547. /* Line in record: slot 3 and 4 */
  548. aacirun->cr |= CR_SL3 | CR_SL4;
  549. return ret;
  550. }
  551. static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
  552. {
  553. u32 ie;
  554. aaci_chan_wait_ready(aacirun, SR_RXB);
  555. ie = readl(aacirun->base + AACI_IE);
  556. ie &= ~(IE_ORIE | IE_RXIE);
  557. writel(ie, aacirun->base+AACI_IE);
  558. aacirun->cr &= ~CR_EN;
  559. writel(aacirun->cr, aacirun->base + AACI_RXCR);
  560. }
  561. static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
  562. {
  563. u32 ie;
  564. aaci_chan_wait_ready(aacirun, SR_RXB);
  565. #ifdef DEBUG
  566. /* RX Timeout value: bits 28:17 in RXCR */
  567. aacirun->cr |= 0xf << 17;
  568. #endif
  569. aacirun->cr |= CR_EN;
  570. writel(aacirun->cr, aacirun->base + AACI_RXCR);
  571. ie = readl(aacirun->base + AACI_IE);
  572. ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
  573. writel(ie, aacirun->base + AACI_IE);
  574. }
  575. static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  576. {
  577. struct aaci_runtime *aacirun = substream->runtime->private_data;
  578. unsigned long flags;
  579. int ret = 0;
  580. spin_lock_irqsave(&aacirun->lock, flags);
  581. switch (cmd) {
  582. case SNDRV_PCM_TRIGGER_START:
  583. aaci_pcm_capture_start(aacirun);
  584. break;
  585. case SNDRV_PCM_TRIGGER_RESUME:
  586. aaci_pcm_capture_start(aacirun);
  587. break;
  588. case SNDRV_PCM_TRIGGER_STOP:
  589. aaci_pcm_capture_stop(aacirun);
  590. break;
  591. case SNDRV_PCM_TRIGGER_SUSPEND:
  592. aaci_pcm_capture_stop(aacirun);
  593. break;
  594. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  595. break;
  596. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  597. break;
  598. default:
  599. ret = -EINVAL;
  600. }
  601. spin_unlock_irqrestore(&aacirun->lock, flags);
  602. return ret;
  603. }
  604. static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
  605. {
  606. struct snd_pcm_runtime *runtime = substream->runtime;
  607. struct aaci *aaci = substream->private_data;
  608. aaci_pcm_prepare(substream);
  609. /* allow changing of sample rate */
  610. aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
  611. aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  612. aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
  613. /* Record select: Mic: 0, Aux: 3, Line: 4 */
  614. aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
  615. return 0;
  616. }
  617. static struct snd_pcm_ops aaci_capture_ops = {
  618. .open = aaci_pcm_open,
  619. .close = aaci_pcm_close,
  620. .ioctl = snd_pcm_lib_ioctl,
  621. .hw_params = aaci_pcm_capture_hw_params,
  622. .hw_free = aaci_pcm_hw_free,
  623. .prepare = aaci_pcm_capture_prepare,
  624. .trigger = aaci_pcm_capture_trigger,
  625. .pointer = aaci_pcm_pointer,
  626. };
  627. /*
  628. * Power Management.
  629. */
  630. #ifdef CONFIG_PM
  631. static int aaci_do_suspend(struct snd_card *card, unsigned int state)
  632. {
  633. struct aaci *aaci = card->private_data;
  634. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  635. snd_pcm_suspend_all(aaci->pcm);
  636. return 0;
  637. }
  638. static int aaci_do_resume(struct snd_card *card, unsigned int state)
  639. {
  640. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  641. return 0;
  642. }
  643. static int aaci_suspend(struct amba_device *dev, pm_message_t state)
  644. {
  645. struct snd_card *card = amba_get_drvdata(dev);
  646. return card ? aaci_do_suspend(card) : 0;
  647. }
  648. static int aaci_resume(struct amba_device *dev)
  649. {
  650. struct snd_card *card = amba_get_drvdata(dev);
  651. return card ? aaci_do_resume(card) : 0;
  652. }
  653. #else
  654. #define aaci_do_suspend NULL
  655. #define aaci_do_resume NULL
  656. #define aaci_suspend NULL
  657. #define aaci_resume NULL
  658. #endif
  659. static struct ac97_pcm ac97_defs[] __devinitdata = {
  660. [0] = { /* Front PCM */
  661. .exclusive = 1,
  662. .r = {
  663. [0] = {
  664. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  665. (1 << AC97_SLOT_PCM_RIGHT) |
  666. (1 << AC97_SLOT_PCM_CENTER) |
  667. (1 << AC97_SLOT_PCM_SLEFT) |
  668. (1 << AC97_SLOT_PCM_SRIGHT) |
  669. (1 << AC97_SLOT_LFE),
  670. },
  671. [1] = {
  672. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  673. (1 << AC97_SLOT_PCM_RIGHT) |
  674. (1 << AC97_SLOT_PCM_LEFT_0) |
  675. (1 << AC97_SLOT_PCM_RIGHT_0),
  676. },
  677. },
  678. },
  679. [1] = { /* PCM in */
  680. .stream = 1,
  681. .exclusive = 1,
  682. .r = {
  683. [0] = {
  684. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  685. (1 << AC97_SLOT_PCM_RIGHT),
  686. },
  687. },
  688. },
  689. [2] = { /* Mic in */
  690. .stream = 1,
  691. .exclusive = 1,
  692. .r = {
  693. [0] = {
  694. .slots = (1 << AC97_SLOT_MIC),
  695. },
  696. },
  697. }
  698. };
  699. static struct snd_ac97_bus_ops aaci_bus_ops = {
  700. .write = aaci_ac97_write,
  701. .read = aaci_ac97_read,
  702. };
  703. static int __devinit aaci_probe_ac97(struct aaci *aaci)
  704. {
  705. struct snd_ac97_template ac97_template;
  706. struct snd_ac97_bus *ac97_bus;
  707. struct snd_ac97 *ac97;
  708. int ret;
  709. /*
  710. * Assert AACIRESET for 2us
  711. */
  712. writel(0, aaci->base + AACI_RESET);
  713. udelay(2);
  714. writel(RESET_NRST, aaci->base + AACI_RESET);
  715. /*
  716. * Give the AC'97 codec more than enough time
  717. * to wake up. (42us = ~2 frames at 48kHz.)
  718. */
  719. udelay(FRAME_PERIOD_US * 2);
  720. ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
  721. if (ret)
  722. goto out;
  723. ac97_bus->clock = 48000;
  724. aaci->ac97_bus = ac97_bus;
  725. memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
  726. ac97_template.private_data = aaci;
  727. ac97_template.num = 0;
  728. ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
  729. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
  730. if (ret)
  731. goto out;
  732. aaci->ac97 = ac97;
  733. /*
  734. * Disable AC97 PC Beep input on audio codecs.
  735. */
  736. if (ac97_is_audio(ac97))
  737. snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
  738. ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
  739. if (ret)
  740. goto out;
  741. aaci->playback.pcm = &ac97_bus->pcms[0];
  742. aaci->capture.pcm = &ac97_bus->pcms[1];
  743. out:
  744. return ret;
  745. }
  746. static void aaci_free_card(struct snd_card *card)
  747. {
  748. struct aaci *aaci = card->private_data;
  749. if (aaci->base)
  750. iounmap(aaci->base);
  751. }
  752. static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
  753. {
  754. struct aaci *aaci;
  755. struct snd_card *card;
  756. int err;
  757. err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  758. THIS_MODULE, sizeof(struct aaci), &card);
  759. if (err < 0)
  760. return NULL;
  761. card->private_free = aaci_free_card;
  762. strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
  763. strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
  764. snprintf(card->longname, sizeof(card->longname),
  765. "%s at 0x%016llx, irq %d",
  766. card->shortname, (unsigned long long)dev->res.start,
  767. dev->irq[0]);
  768. aaci = card->private_data;
  769. mutex_init(&aaci->ac97_sem);
  770. aaci->card = card;
  771. aaci->dev = dev;
  772. /* Set MAINCR to allow slot 1 and 2 data IO */
  773. aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
  774. MAINCR_SL2RXEN | MAINCR_SL2TXEN;
  775. return aaci;
  776. }
  777. static int __devinit aaci_init_pcm(struct aaci *aaci)
  778. {
  779. struct snd_pcm *pcm;
  780. int ret;
  781. ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
  782. if (ret == 0) {
  783. aaci->pcm = pcm;
  784. pcm->private_data = aaci;
  785. pcm->info_flags = 0;
  786. strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
  787. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
  788. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
  789. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  790. NULL, 0, 64 * 1024);
  791. }
  792. return ret;
  793. }
  794. static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
  795. {
  796. struct aaci_runtime *aacirun = &aaci->playback;
  797. int i;
  798. writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
  799. for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
  800. writel(0, aacirun->fifo);
  801. writel(0, aacirun->base + AACI_TXCR);
  802. /*
  803. * Re-initialise the AACI after the FIFO depth test, to
  804. * ensure that the FIFOs are empty. Unfortunately, merely
  805. * disabling the channel doesn't clear the FIFO.
  806. */
  807. writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
  808. readl(aaci->base + AACI_MAINCR);
  809. udelay(1);
  810. writel(aaci->maincr, aaci->base + AACI_MAINCR);
  811. /*
  812. * If we hit 4096, we failed. Go back to the specified
  813. * fifo depth.
  814. */
  815. if (i == 4096)
  816. i = 8;
  817. return i;
  818. }
  819. static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
  820. {
  821. struct aaci *aaci;
  822. int ret, i;
  823. ret = amba_request_regions(dev, NULL);
  824. if (ret)
  825. return ret;
  826. aaci = aaci_init_card(dev);
  827. if (!aaci) {
  828. ret = -ENOMEM;
  829. goto out;
  830. }
  831. aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
  832. if (!aaci->base) {
  833. ret = -ENOMEM;
  834. goto out;
  835. }
  836. /*
  837. * Playback uses AACI channel 0
  838. */
  839. spin_lock_init(&aaci->playback.lock);
  840. aaci->playback.base = aaci->base + AACI_CSCH1;
  841. aaci->playback.fifo = aaci->base + AACI_DR1;
  842. /*
  843. * Capture uses AACI channel 0
  844. */
  845. spin_lock_init(&aaci->capture.lock);
  846. aaci->capture.base = aaci->base + AACI_CSCH1;
  847. aaci->capture.fifo = aaci->base + AACI_DR1;
  848. for (i = 0; i < 4; i++) {
  849. void __iomem *base = aaci->base + i * 0x14;
  850. writel(0, base + AACI_IE);
  851. writel(0, base + AACI_TXCR);
  852. writel(0, base + AACI_RXCR);
  853. }
  854. writel(0x1fff, aaci->base + AACI_INTCLR);
  855. writel(aaci->maincr, aaci->base + AACI_MAINCR);
  856. /*
  857. * Fix: ac97 read back fail errors by reading
  858. * from any arbitrary aaci register.
  859. */
  860. readl(aaci->base + AACI_CSCH1);
  861. ret = aaci_probe_ac97(aaci);
  862. if (ret)
  863. goto out;
  864. /*
  865. * Size the FIFOs (must be multiple of 16).
  866. */
  867. aaci->fifosize = aaci_size_fifo(aaci);
  868. if (aaci->fifosize & 15) {
  869. printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
  870. aaci->fifosize);
  871. ret = -ENODEV;
  872. goto out;
  873. }
  874. ret = aaci_init_pcm(aaci);
  875. if (ret)
  876. goto out;
  877. snd_card_set_dev(aaci->card, &dev->dev);
  878. ret = snd_card_register(aaci->card);
  879. if (ret == 0) {
  880. dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
  881. aaci->fifosize);
  882. amba_set_drvdata(dev, aaci->card);
  883. return ret;
  884. }
  885. out:
  886. if (aaci)
  887. snd_card_free(aaci->card);
  888. amba_release_regions(dev);
  889. return ret;
  890. }
  891. static int __devexit aaci_remove(struct amba_device *dev)
  892. {
  893. struct snd_card *card = amba_get_drvdata(dev);
  894. amba_set_drvdata(dev, NULL);
  895. if (card) {
  896. struct aaci *aaci = card->private_data;
  897. writel(0, aaci->base + AACI_MAINCR);
  898. snd_card_free(card);
  899. amba_release_regions(dev);
  900. }
  901. return 0;
  902. }
  903. static struct amba_id aaci_ids[] = {
  904. {
  905. .id = 0x00041041,
  906. .mask = 0x000fffff,
  907. },
  908. { 0, 0 },
  909. };
  910. static struct amba_driver aaci_driver = {
  911. .drv = {
  912. .name = DRIVER_NAME,
  913. },
  914. .probe = aaci_probe,
  915. .remove = __devexit_p(aaci_remove),
  916. .suspend = aaci_suspend,
  917. .resume = aaci_resume,
  918. .id_table = aaci_ids,
  919. };
  920. static int __init aaci_init(void)
  921. {
  922. return amba_driver_register(&aaci_driver);
  923. }
  924. static void __exit aaci_exit(void)
  925. {
  926. amba_driver_unregister(&aaci_driver);
  927. }
  928. module_init(aaci_init);
  929. module_exit(aaci_exit);
  930. MODULE_LICENSE("GPL");
  931. MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");