aaci.c 25 KB

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