aaci.c 27 KB

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