aaci.c 25 KB

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