aaci.c 25 KB

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