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