aaci.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. void *ptr;
  174. if (!aacirun->substream || !aacirun->start) {
  175. dev_warn(&aaci->dev->dev, "RX interrupt???\n");
  176. writel(0, aacirun->base + AACI_IE);
  177. return;
  178. }
  179. spin_lock(&aacirun->lock);
  180. ptr = aacirun->ptr;
  181. do {
  182. unsigned int len = aacirun->fifosz;
  183. u32 val;
  184. if (aacirun->bytes <= 0) {
  185. aacirun->bytes += aacirun->period;
  186. aacirun->ptr = ptr;
  187. spin_unlock(&aacirun->lock);
  188. snd_pcm_period_elapsed(aacirun->substream);
  189. spin_lock(&aacirun->lock);
  190. }
  191. if (!(aacirun->cr & CR_EN))
  192. break;
  193. val = readl(aacirun->base + AACI_SR);
  194. if (!(val & SR_RXHF))
  195. break;
  196. if (!(val & SR_RXFF))
  197. len >>= 1;
  198. aacirun->bytes -= len;
  199. /* reading 16 bytes at a time */
  200. for( ; len > 0; len -= 16) {
  201. asm(
  202. "ldmia %1, {r0, r1, r2, r3}\n\t"
  203. "stmia %0!, {r0, r1, r2, r3}"
  204. : "+r" (ptr)
  205. : "r" (aacirun->fifo)
  206. : "r0", "r1", "r2", "r3", "cc");
  207. if (ptr >= aacirun->end)
  208. ptr = aacirun->start;
  209. }
  210. } while(1);
  211. aacirun->ptr = ptr;
  212. spin_unlock(&aacirun->lock);
  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. void *ptr;
  221. if (!aacirun->substream || !aacirun->start) {
  222. dev_warn(&aaci->dev->dev, "TX interrupt???\n");
  223. writel(0, aacirun->base + AACI_IE);
  224. return;
  225. }
  226. spin_lock(&aacirun->lock);
  227. ptr = aacirun->ptr;
  228. do {
  229. unsigned int len = aacirun->fifosz;
  230. u32 val;
  231. if (aacirun->bytes <= 0) {
  232. aacirun->bytes += aacirun->period;
  233. aacirun->ptr = ptr;
  234. spin_unlock(&aacirun->lock);
  235. snd_pcm_period_elapsed(aacirun->substream);
  236. spin_lock(&aacirun->lock);
  237. }
  238. if (!(aacirun->cr & CR_EN))
  239. break;
  240. val = readl(aacirun->base + AACI_SR);
  241. if (!(val & SR_TXHE))
  242. break;
  243. if (!(val & SR_TXFE))
  244. len >>= 1;
  245. aacirun->bytes -= len;
  246. /* writing 16 bytes at a time */
  247. for ( ; len > 0; len -= 16) {
  248. asm(
  249. "ldmia %0!, {r0, r1, r2, r3}\n\t"
  250. "stmia %1, {r0, r1, r2, r3}"
  251. : "+r" (ptr)
  252. : "r" (aacirun->fifo)
  253. : "r0", "r1", "r2", "r3", "cc");
  254. if (ptr >= aacirun->end)
  255. ptr = aacirun->start;
  256. }
  257. } while (1);
  258. aacirun->ptr = ptr;
  259. spin_unlock(&aacirun->lock);
  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. * FIXME: ALSA specifies fifo_size in bytes. If we're in normal
  356. * mode, each 32-bit word contains one sample. If we're in
  357. * compact mode, each 32-bit word contains two samples, effectively
  358. * halving the FIFO size. However, we don't know for sure which
  359. * we'll be using at this point. We set this to the lower limit.
  360. */
  361. runtime->hw.fifo_size = aaci->fifosize * 2;
  362. mutex_lock(&aaci->irq_lock);
  363. if (!aaci->users++) {
  364. ret = request_irq(aaci->dev->irq[0], aaci_irq,
  365. IRQF_SHARED | IRQF_DISABLED, DRIVER_NAME, aaci);
  366. if (ret != 0)
  367. aaci->users--;
  368. }
  369. mutex_unlock(&aaci->irq_lock);
  370. return ret;
  371. }
  372. /*
  373. * Common ALSA stuff
  374. */
  375. static int aaci_pcm_close(struct snd_pcm_substream *substream)
  376. {
  377. struct aaci *aaci = substream->private_data;
  378. struct aaci_runtime *aacirun = substream->runtime->private_data;
  379. WARN_ON(aacirun->cr & CR_EN);
  380. aacirun->substream = NULL;
  381. mutex_lock(&aaci->irq_lock);
  382. if (!--aaci->users)
  383. free_irq(aaci->dev->irq[0], aaci);
  384. mutex_unlock(&aaci->irq_lock);
  385. return 0;
  386. }
  387. static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
  388. {
  389. struct aaci_runtime *aacirun = substream->runtime->private_data;
  390. /*
  391. * This must not be called with the device enabled.
  392. */
  393. WARN_ON(aacirun->cr & CR_EN);
  394. if (aacirun->pcm_open)
  395. snd_ac97_pcm_close(aacirun->pcm);
  396. aacirun->pcm_open = 0;
  397. /*
  398. * Clear out the DMA and any allocated buffers.
  399. */
  400. snd_pcm_lib_free_pages(substream);
  401. return 0;
  402. }
  403. /* Channel to slot mask */
  404. static const u32 channels_to_slotmask[] = {
  405. [2] = CR_SL3 | CR_SL4,
  406. [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
  407. [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
  408. };
  409. static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
  410. struct snd_pcm_hw_params *params)
  411. {
  412. struct aaci_runtime *aacirun = substream->runtime->private_data;
  413. unsigned int channels = params_channels(params);
  414. unsigned int rate = params_rate(params);
  415. int dbl = rate > 48000;
  416. int err;
  417. aaci_pcm_hw_free(substream);
  418. if (aacirun->pcm_open) {
  419. snd_ac97_pcm_close(aacirun->pcm);
  420. aacirun->pcm_open = 0;
  421. }
  422. /* channels is already limited to 2, 4, or 6 by aaci_rule_channels */
  423. if (dbl && channels != 2)
  424. return -EINVAL;
  425. err = snd_pcm_lib_malloc_pages(substream,
  426. params_buffer_bytes(params));
  427. if (err >= 0) {
  428. struct aaci *aaci = substream->private_data;
  429. err = snd_ac97_pcm_open(aacirun->pcm, rate, channels,
  430. aacirun->pcm->r[dbl].slots);
  431. aacirun->pcm_open = err == 0;
  432. aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
  433. aacirun->cr |= channels_to_slotmask[channels + dbl * 2];
  434. aacirun->fifosz = aaci->fifosize * 4;
  435. if (aacirun->cr & CR_COMPACT)
  436. aacirun->fifosz >>= 1;
  437. }
  438. return err;
  439. }
  440. static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
  441. {
  442. struct snd_pcm_runtime *runtime = substream->runtime;
  443. struct aaci_runtime *aacirun = runtime->private_data;
  444. aacirun->start = runtime->dma_area;
  445. aacirun->end = aacirun->start + snd_pcm_lib_buffer_bytes(substream);
  446. aacirun->ptr = aacirun->start;
  447. aacirun->period =
  448. aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
  449. return 0;
  450. }
  451. static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
  452. {
  453. struct snd_pcm_runtime *runtime = substream->runtime;
  454. struct aaci_runtime *aacirun = runtime->private_data;
  455. ssize_t bytes = aacirun->ptr - aacirun->start;
  456. return bytes_to_frames(runtime, bytes);
  457. }
  458. /*
  459. * Playback specific ALSA stuff
  460. */
  461. static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
  462. {
  463. u32 ie;
  464. ie = readl(aacirun->base + AACI_IE);
  465. ie &= ~(IE_URIE|IE_TXIE);
  466. writel(ie, aacirun->base + AACI_IE);
  467. aacirun->cr &= ~CR_EN;
  468. aaci_chan_wait_ready(aacirun, SR_TXB);
  469. writel(aacirun->cr, aacirun->base + AACI_TXCR);
  470. }
  471. static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
  472. {
  473. u32 ie;
  474. aaci_chan_wait_ready(aacirun, SR_TXB);
  475. aacirun->cr |= CR_EN;
  476. ie = readl(aacirun->base + AACI_IE);
  477. ie |= IE_URIE | IE_TXIE;
  478. writel(ie, aacirun->base + AACI_IE);
  479. writel(aacirun->cr, aacirun->base + AACI_TXCR);
  480. }
  481. static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  482. {
  483. struct aaci_runtime *aacirun = substream->runtime->private_data;
  484. unsigned long flags;
  485. int ret = 0;
  486. spin_lock_irqsave(&aacirun->lock, flags);
  487. switch (cmd) {
  488. case SNDRV_PCM_TRIGGER_START:
  489. aaci_pcm_playback_start(aacirun);
  490. break;
  491. case SNDRV_PCM_TRIGGER_RESUME:
  492. aaci_pcm_playback_start(aacirun);
  493. break;
  494. case SNDRV_PCM_TRIGGER_STOP:
  495. aaci_pcm_playback_stop(aacirun);
  496. break;
  497. case SNDRV_PCM_TRIGGER_SUSPEND:
  498. aaci_pcm_playback_stop(aacirun);
  499. break;
  500. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  501. break;
  502. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  503. break;
  504. default:
  505. ret = -EINVAL;
  506. }
  507. spin_unlock_irqrestore(&aacirun->lock, flags);
  508. return ret;
  509. }
  510. static struct snd_pcm_ops aaci_playback_ops = {
  511. .open = aaci_pcm_open,
  512. .close = aaci_pcm_close,
  513. .ioctl = snd_pcm_lib_ioctl,
  514. .hw_params = aaci_pcm_hw_params,
  515. .hw_free = aaci_pcm_hw_free,
  516. .prepare = aaci_pcm_prepare,
  517. .trigger = aaci_pcm_playback_trigger,
  518. .pointer = aaci_pcm_pointer,
  519. };
  520. static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
  521. {
  522. u32 ie;
  523. aaci_chan_wait_ready(aacirun, SR_RXB);
  524. ie = readl(aacirun->base + AACI_IE);
  525. ie &= ~(IE_ORIE | IE_RXIE);
  526. writel(ie, aacirun->base+AACI_IE);
  527. aacirun->cr &= ~CR_EN;
  528. writel(aacirun->cr, aacirun->base + AACI_RXCR);
  529. }
  530. static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
  531. {
  532. u32 ie;
  533. aaci_chan_wait_ready(aacirun, SR_RXB);
  534. #ifdef DEBUG
  535. /* RX Timeout value: bits 28:17 in RXCR */
  536. aacirun->cr |= 0xf << 17;
  537. #endif
  538. aacirun->cr |= CR_EN;
  539. writel(aacirun->cr, aacirun->base + AACI_RXCR);
  540. ie = readl(aacirun->base + AACI_IE);
  541. ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
  542. writel(ie, aacirun->base + AACI_IE);
  543. }
  544. static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  545. {
  546. struct aaci_runtime *aacirun = substream->runtime->private_data;
  547. unsigned long flags;
  548. int ret = 0;
  549. spin_lock_irqsave(&aacirun->lock, flags);
  550. switch (cmd) {
  551. case SNDRV_PCM_TRIGGER_START:
  552. aaci_pcm_capture_start(aacirun);
  553. break;
  554. case SNDRV_PCM_TRIGGER_RESUME:
  555. aaci_pcm_capture_start(aacirun);
  556. break;
  557. case SNDRV_PCM_TRIGGER_STOP:
  558. aaci_pcm_capture_stop(aacirun);
  559. break;
  560. case SNDRV_PCM_TRIGGER_SUSPEND:
  561. aaci_pcm_capture_stop(aacirun);
  562. break;
  563. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  564. break;
  565. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  566. break;
  567. default:
  568. ret = -EINVAL;
  569. }
  570. spin_unlock_irqrestore(&aacirun->lock, flags);
  571. return ret;
  572. }
  573. static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
  574. {
  575. struct snd_pcm_runtime *runtime = substream->runtime;
  576. struct aaci *aaci = substream->private_data;
  577. aaci_pcm_prepare(substream);
  578. /* allow changing of sample rate */
  579. aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
  580. aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  581. aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
  582. /* Record select: Mic: 0, Aux: 3, Line: 4 */
  583. aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
  584. return 0;
  585. }
  586. static struct snd_pcm_ops aaci_capture_ops = {
  587. .open = aaci_pcm_open,
  588. .close = aaci_pcm_close,
  589. .ioctl = snd_pcm_lib_ioctl,
  590. .hw_params = aaci_pcm_hw_params,
  591. .hw_free = aaci_pcm_hw_free,
  592. .prepare = aaci_pcm_capture_prepare,
  593. .trigger = aaci_pcm_capture_trigger,
  594. .pointer = aaci_pcm_pointer,
  595. };
  596. /*
  597. * Power Management.
  598. */
  599. #ifdef CONFIG_PM
  600. static int aaci_do_suspend(struct snd_card *card, unsigned int state)
  601. {
  602. struct aaci *aaci = card->private_data;
  603. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  604. snd_pcm_suspend_all(aaci->pcm);
  605. return 0;
  606. }
  607. static int aaci_do_resume(struct snd_card *card, unsigned int state)
  608. {
  609. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  610. return 0;
  611. }
  612. static int aaci_suspend(struct amba_device *dev, pm_message_t state)
  613. {
  614. struct snd_card *card = amba_get_drvdata(dev);
  615. return card ? aaci_do_suspend(card) : 0;
  616. }
  617. static int aaci_resume(struct amba_device *dev)
  618. {
  619. struct snd_card *card = amba_get_drvdata(dev);
  620. return card ? aaci_do_resume(card) : 0;
  621. }
  622. #else
  623. #define aaci_do_suspend NULL
  624. #define aaci_do_resume NULL
  625. #define aaci_suspend NULL
  626. #define aaci_resume NULL
  627. #endif
  628. static struct ac97_pcm ac97_defs[] __devinitdata = {
  629. [0] = { /* Front PCM */
  630. .exclusive = 1,
  631. .r = {
  632. [0] = {
  633. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  634. (1 << AC97_SLOT_PCM_RIGHT) |
  635. (1 << AC97_SLOT_PCM_CENTER) |
  636. (1 << AC97_SLOT_PCM_SLEFT) |
  637. (1 << AC97_SLOT_PCM_SRIGHT) |
  638. (1 << AC97_SLOT_LFE),
  639. },
  640. [1] = {
  641. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  642. (1 << AC97_SLOT_PCM_RIGHT) |
  643. (1 << AC97_SLOT_PCM_LEFT_0) |
  644. (1 << AC97_SLOT_PCM_RIGHT_0),
  645. },
  646. },
  647. },
  648. [1] = { /* PCM in */
  649. .stream = 1,
  650. .exclusive = 1,
  651. .r = {
  652. [0] = {
  653. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  654. (1 << AC97_SLOT_PCM_RIGHT),
  655. },
  656. },
  657. },
  658. [2] = { /* Mic in */
  659. .stream = 1,
  660. .exclusive = 1,
  661. .r = {
  662. [0] = {
  663. .slots = (1 << AC97_SLOT_MIC),
  664. },
  665. },
  666. }
  667. };
  668. static struct snd_ac97_bus_ops aaci_bus_ops = {
  669. .write = aaci_ac97_write,
  670. .read = aaci_ac97_read,
  671. };
  672. static int __devinit aaci_probe_ac97(struct aaci *aaci)
  673. {
  674. struct snd_ac97_template ac97_template;
  675. struct snd_ac97_bus *ac97_bus;
  676. struct snd_ac97 *ac97;
  677. int ret;
  678. /*
  679. * Assert AACIRESET for 2us
  680. */
  681. writel(0, aaci->base + AACI_RESET);
  682. udelay(2);
  683. writel(RESET_NRST, aaci->base + AACI_RESET);
  684. /*
  685. * Give the AC'97 codec more than enough time
  686. * to wake up. (42us = ~2 frames at 48kHz.)
  687. */
  688. udelay(FRAME_PERIOD_US * 2);
  689. ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
  690. if (ret)
  691. goto out;
  692. ac97_bus->clock = 48000;
  693. aaci->ac97_bus = ac97_bus;
  694. memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
  695. ac97_template.private_data = aaci;
  696. ac97_template.num = 0;
  697. ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
  698. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
  699. if (ret)
  700. goto out;
  701. aaci->ac97 = ac97;
  702. /*
  703. * Disable AC97 PC Beep input on audio codecs.
  704. */
  705. if (ac97_is_audio(ac97))
  706. snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
  707. ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
  708. if (ret)
  709. goto out;
  710. aaci->playback.pcm = &ac97_bus->pcms[0];
  711. aaci->capture.pcm = &ac97_bus->pcms[1];
  712. out:
  713. return ret;
  714. }
  715. static void aaci_free_card(struct snd_card *card)
  716. {
  717. struct aaci *aaci = card->private_data;
  718. if (aaci->base)
  719. iounmap(aaci->base);
  720. }
  721. static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
  722. {
  723. struct aaci *aaci;
  724. struct snd_card *card;
  725. int err;
  726. err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  727. THIS_MODULE, sizeof(struct aaci), &card);
  728. if (err < 0)
  729. return NULL;
  730. card->private_free = aaci_free_card;
  731. strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
  732. strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
  733. snprintf(card->longname, sizeof(card->longname),
  734. "%s PL%03x rev%u at 0x%08llx, irq %d",
  735. card->shortname, amba_part(dev), amba_rev(dev),
  736. (unsigned long long)dev->res.start, dev->irq[0]);
  737. aaci = card->private_data;
  738. mutex_init(&aaci->ac97_sem);
  739. mutex_init(&aaci->irq_lock);
  740. aaci->card = card;
  741. aaci->dev = dev;
  742. /* Set MAINCR to allow slot 1 and 2 data IO */
  743. aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
  744. MAINCR_SL2RXEN | MAINCR_SL2TXEN;
  745. return aaci;
  746. }
  747. static int __devinit aaci_init_pcm(struct aaci *aaci)
  748. {
  749. struct snd_pcm *pcm;
  750. int ret;
  751. ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
  752. if (ret == 0) {
  753. aaci->pcm = pcm;
  754. pcm->private_data = aaci;
  755. pcm->info_flags = 0;
  756. strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
  757. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
  758. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
  759. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  760. NULL, 0, 64 * 1024);
  761. }
  762. return ret;
  763. }
  764. static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
  765. {
  766. struct aaci_runtime *aacirun = &aaci->playback;
  767. int i;
  768. writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
  769. for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
  770. writel(0, aacirun->fifo);
  771. writel(0, aacirun->base + AACI_TXCR);
  772. /*
  773. * Re-initialise the AACI after the FIFO depth test, to
  774. * ensure that the FIFOs are empty. Unfortunately, merely
  775. * disabling the channel doesn't clear the FIFO.
  776. */
  777. writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
  778. writel(aaci->maincr, aaci->base + AACI_MAINCR);
  779. /*
  780. * If we hit 4096, we failed. Go back to the specified
  781. * fifo depth.
  782. */
  783. if (i == 4096)
  784. i = 8;
  785. return i;
  786. }
  787. static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
  788. {
  789. struct aaci *aaci;
  790. int ret, i;
  791. ret = amba_request_regions(dev, NULL);
  792. if (ret)
  793. return ret;
  794. aaci = aaci_init_card(dev);
  795. if (!aaci) {
  796. ret = -ENOMEM;
  797. goto out;
  798. }
  799. aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
  800. if (!aaci->base) {
  801. ret = -ENOMEM;
  802. goto out;
  803. }
  804. /*
  805. * Playback uses AACI channel 0
  806. */
  807. spin_lock_init(&aaci->playback.lock);
  808. aaci->playback.base = aaci->base + AACI_CSCH1;
  809. aaci->playback.fifo = aaci->base + AACI_DR1;
  810. /*
  811. * Capture uses AACI channel 0
  812. */
  813. spin_lock_init(&aaci->capture.lock);
  814. aaci->capture.base = aaci->base + AACI_CSCH1;
  815. aaci->capture.fifo = aaci->base + AACI_DR1;
  816. for (i = 0; i < 4; i++) {
  817. void __iomem *base = aaci->base + i * 0x14;
  818. writel(0, base + AACI_IE);
  819. writel(0, base + AACI_TXCR);
  820. writel(0, base + AACI_RXCR);
  821. }
  822. writel(0x1fff, aaci->base + AACI_INTCLR);
  823. writel(aaci->maincr, aaci->base + AACI_MAINCR);
  824. /*
  825. * Fix: ac97 read back fail errors by reading
  826. * from any arbitrary aaci register.
  827. */
  828. readl(aaci->base + AACI_CSCH1);
  829. ret = aaci_probe_ac97(aaci);
  830. if (ret)
  831. goto out;
  832. /*
  833. * Size the FIFOs (must be multiple of 16).
  834. */
  835. aaci->fifosize = aaci_size_fifo(aaci);
  836. if (aaci->fifosize & 15) {
  837. printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
  838. aaci->fifosize);
  839. ret = -ENODEV;
  840. goto out;
  841. }
  842. ret = aaci_init_pcm(aaci);
  843. if (ret)
  844. goto out;
  845. snd_card_set_dev(aaci->card, &dev->dev);
  846. ret = snd_card_register(aaci->card);
  847. if (ret == 0) {
  848. dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
  849. aaci->fifosize);
  850. amba_set_drvdata(dev, aaci->card);
  851. return ret;
  852. }
  853. out:
  854. if (aaci)
  855. snd_card_free(aaci->card);
  856. amba_release_regions(dev);
  857. return ret;
  858. }
  859. static int __devexit aaci_remove(struct amba_device *dev)
  860. {
  861. struct snd_card *card = amba_get_drvdata(dev);
  862. amba_set_drvdata(dev, NULL);
  863. if (card) {
  864. struct aaci *aaci = card->private_data;
  865. writel(0, aaci->base + AACI_MAINCR);
  866. snd_card_free(card);
  867. amba_release_regions(dev);
  868. }
  869. return 0;
  870. }
  871. static struct amba_id aaci_ids[] = {
  872. {
  873. .id = 0x00041041,
  874. .mask = 0x000fffff,
  875. },
  876. { 0, 0 },
  877. };
  878. static struct amba_driver aaci_driver = {
  879. .drv = {
  880. .name = DRIVER_NAME,
  881. },
  882. .probe = aaci_probe,
  883. .remove = __devexit_p(aaci_remove),
  884. .suspend = aaci_suspend,
  885. .resume = aaci_resume,
  886. .id_table = aaci_ids,
  887. };
  888. static int __init aaci_init(void)
  889. {
  890. return amba_driver_register(&aaci_driver);
  891. }
  892. static void __exit aaci_exit(void)
  893. {
  894. amba_driver_unregister(&aaci_driver);
  895. }
  896. module_init(aaci_init);
  897. module_exit(aaci_exit);
  898. MODULE_LICENSE("GPL");
  899. MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");