aaci.c 25 KB

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