aaci.c 24 KB

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