aaci.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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 <asm/io.h>
  22. #include <asm/irq.h>
  23. #include <asm/sizes.h>
  24. #include <sound/core.h>
  25. #include <sound/initval.h>
  26. #include <sound/ac97_codec.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include "aaci.h"
  30. #include "devdma.h"
  31. #define DRIVER_NAME "aaci-pl041"
  32. /*
  33. * PM support is not complete. Turn it off.
  34. */
  35. #undef CONFIG_PM
  36. static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
  37. {
  38. u32 v, maincr = aaci->maincr | MAINCR_SCRA(ac97->num);
  39. /*
  40. * Ensure that the slot 1/2 RX registers are empty.
  41. */
  42. v = readl(aaci->base + AACI_SLFR);
  43. if (v & SLFR_2RXV)
  44. readl(aaci->base + AACI_SL2RX);
  45. if (v & SLFR_1RXV)
  46. readl(aaci->base + AACI_SL1RX);
  47. writel(maincr, aaci->base + AACI_MAINCR);
  48. }
  49. /*
  50. * P29:
  51. * The recommended use of programming the external codec through slot 1
  52. * and slot 2 data is to use the channels during setup routines and the
  53. * slot register at any other time. The data written into slot 1, slot 2
  54. * and slot 12 registers is transmitted only when their corresponding
  55. * SI1TxEn, SI2TxEn and SI12TxEn bits are set in the AACI_MAINCR
  56. * register.
  57. */
  58. static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  59. unsigned short val)
  60. {
  61. struct aaci *aaci = ac97->private_data;
  62. u32 v;
  63. int timeout = 5000;
  64. if (ac97->num >= 4)
  65. return;
  66. mutex_lock(&aaci->ac97_sem);
  67. aaci_ac97_select_codec(aaci, ac97);
  68. /*
  69. * P54: You must ensure that AACI_SL2TX is always written
  70. * to, if required, before data is written to AACI_SL1TX.
  71. */
  72. writel(val << 4, aaci->base + AACI_SL2TX);
  73. writel(reg << 12, aaci->base + AACI_SL1TX);
  74. /*
  75. * Wait for the transmission of both slots to complete.
  76. */
  77. do {
  78. v = readl(aaci->base + AACI_SLFR);
  79. } while ((v & (SLFR_1TXB|SLFR_2TXB)) && timeout--);
  80. if (!timeout)
  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. u32 v;
  92. int timeout = 5000;
  93. int retries = 10;
  94. if (ac97->num >= 4)
  95. return ~0;
  96. mutex_lock(&aaci->ac97_sem);
  97. aaci_ac97_select_codec(aaci, ac97);
  98. /*
  99. * Write the register address to slot 1.
  100. */
  101. writel((reg << 12) | (1 << 19), aaci->base + AACI_SL1TX);
  102. /*
  103. * Wait for the transmission to complete.
  104. */
  105. do {
  106. v = readl(aaci->base + AACI_SLFR);
  107. } while ((v & SLFR_1TXB) && timeout--);
  108. if (!timeout) {
  109. dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
  110. v = ~0;
  111. goto out;
  112. }
  113. /*
  114. * Give the AC'97 codec more than enough time
  115. * to respond. (42us = ~2 frames at 48kHz.)
  116. */
  117. udelay(42);
  118. /*
  119. * Wait for slot 2 to indicate data.
  120. */
  121. timeout = 5000;
  122. do {
  123. cond_resched();
  124. v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV);
  125. } while ((v != (SLFR_1RXV|SLFR_2RXV)) && timeout--);
  126. if (!timeout) {
  127. dev_err(&aaci->dev->dev, "timeout on RX valid\n");
  128. v = ~0;
  129. goto out;
  130. }
  131. do {
  132. v = readl(aaci->base + AACI_SL1RX) >> 12;
  133. if (v == reg) {
  134. v = readl(aaci->base + AACI_SL2RX) >> 4;
  135. break;
  136. } else if (--retries) {
  137. dev_warn(&aaci->dev->dev,
  138. "ac97 read back fail. retry\n");
  139. continue;
  140. } else {
  141. dev_warn(&aaci->dev->dev,
  142. "wrong ac97 register read back (%x != %x)\n",
  143. v, reg);
  144. v = ~0;
  145. }
  146. } while (retries);
  147. out:
  148. mutex_unlock(&aaci->ac97_sem);
  149. return v;
  150. }
  151. static inline void aaci_chan_wait_ready(struct aaci_runtime *aacirun)
  152. {
  153. u32 val;
  154. int timeout = 5000;
  155. do {
  156. val = readl(aacirun->base + AACI_SR);
  157. } while (val & (SR_TXB|SR_RXB) && timeout--);
  158. }
  159. /*
  160. * Interrupt support.
  161. */
  162. static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
  163. {
  164. if (mask & ISR_ORINTR) {
  165. dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel);
  166. writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR);
  167. }
  168. if (mask & ISR_RXTOINTR) {
  169. dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel);
  170. writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR);
  171. }
  172. if (mask & ISR_RXINTR) {
  173. struct aaci_runtime *aacirun = &aaci->capture;
  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. 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(&aaci->lock);
  188. snd_pcm_period_elapsed(aacirun->substream);
  189. spin_lock(&aaci->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. }
  213. if (mask & ISR_URINTR) {
  214. dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
  215. writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
  216. }
  217. if (mask & ISR_TXINTR) {
  218. struct aaci_runtime *aacirun = &aaci->playback;
  219. void *ptr;
  220. if (!aacirun->substream || !aacirun->start) {
  221. dev_warn(&aaci->dev->dev, "TX interrupt???\n");
  222. writel(0, aacirun->base + AACI_IE);
  223. return;
  224. }
  225. ptr = aacirun->ptr;
  226. do {
  227. unsigned int len = aacirun->fifosz;
  228. u32 val;
  229. if (aacirun->bytes <= 0) {
  230. aacirun->bytes += aacirun->period;
  231. aacirun->ptr = ptr;
  232. spin_unlock(&aaci->lock);
  233. snd_pcm_period_elapsed(aacirun->substream);
  234. spin_lock(&aaci->lock);
  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. }
  258. }
  259. static irqreturn_t aaci_irq(int irq, void *devid)
  260. {
  261. struct aaci *aaci = devid;
  262. u32 mask;
  263. int i;
  264. spin_lock(&aaci->lock);
  265. mask = readl(aaci->base + AACI_ALLINTS);
  266. if (mask) {
  267. u32 m = mask;
  268. for (i = 0; i < 4; i++, m >>= 7) {
  269. if (m & 0x7f) {
  270. aaci_fifo_irq(aaci, i, m);
  271. }
  272. }
  273. }
  274. spin_unlock(&aaci->lock);
  275. return mask ? IRQ_HANDLED : IRQ_NONE;
  276. }
  277. /*
  278. * ALSA support.
  279. */
  280. struct aaci_stream {
  281. unsigned char codec_idx;
  282. unsigned char rate_idx;
  283. };
  284. static struct aaci_stream aaci_streams[] = {
  285. [ACSTREAM_FRONT] = {
  286. .codec_idx = 0,
  287. .rate_idx = AC97_RATES_FRONT_DAC,
  288. },
  289. [ACSTREAM_SURROUND] = {
  290. .codec_idx = 0,
  291. .rate_idx = AC97_RATES_SURR_DAC,
  292. },
  293. [ACSTREAM_LFE] = {
  294. .codec_idx = 0,
  295. .rate_idx = AC97_RATES_LFE_DAC,
  296. },
  297. };
  298. static inline unsigned int aaci_rate_mask(struct aaci *aaci, int streamid)
  299. {
  300. struct aaci_stream *s = aaci_streams + streamid;
  301. return aaci->ac97_bus->codec[s->codec_idx]->rates[s->rate_idx];
  302. }
  303. static unsigned int rate_list[] = {
  304. 5512, 8000, 11025, 16000, 22050, 32000, 44100,
  305. 48000, 64000, 88200, 96000, 176400, 192000
  306. };
  307. /*
  308. * Double-rate rule: we can support double rate iff channels == 2
  309. * (unimplemented)
  310. */
  311. static int
  312. aaci_rule_rate_by_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
  313. {
  314. struct aaci *aaci = rule->private;
  315. unsigned int rate_mask = SNDRV_PCM_RATE_8000_48000|SNDRV_PCM_RATE_5512;
  316. struct snd_interval *c = hw_param_interval(p, SNDRV_PCM_HW_PARAM_CHANNELS);
  317. switch (c->max) {
  318. case 6:
  319. rate_mask &= aaci_rate_mask(aaci, ACSTREAM_LFE);
  320. case 4:
  321. rate_mask &= aaci_rate_mask(aaci, ACSTREAM_SURROUND);
  322. case 2:
  323. rate_mask &= aaci_rate_mask(aaci, ACSTREAM_FRONT);
  324. }
  325. return snd_interval_list(hw_param_interval(p, rule->var),
  326. ARRAY_SIZE(rate_list), rate_list,
  327. rate_mask);
  328. }
  329. static struct snd_pcm_hardware aaci_hw_info = {
  330. .info = SNDRV_PCM_INFO_MMAP |
  331. SNDRV_PCM_INFO_MMAP_VALID |
  332. SNDRV_PCM_INFO_INTERLEAVED |
  333. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  334. SNDRV_PCM_INFO_RESUME,
  335. /*
  336. * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
  337. * words. It also doesn't support 12-bit at all.
  338. */
  339. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  340. /* should this be continuous or knot? */
  341. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  342. .rate_max = 48000,
  343. .rate_min = 4000,
  344. .channels_min = 2,
  345. .channels_max = 6,
  346. .buffer_bytes_max = 64 * 1024,
  347. .period_bytes_min = 256,
  348. .period_bytes_max = PAGE_SIZE,
  349. .periods_min = 4,
  350. .periods_max = PAGE_SIZE / 16,
  351. };
  352. static int __aaci_pcm_open(struct aaci *aaci,
  353. struct snd_pcm_substream *substream,
  354. struct aaci_runtime *aacirun)
  355. {
  356. struct snd_pcm_runtime *runtime = substream->runtime;
  357. int ret;
  358. aacirun->substream = substream;
  359. runtime->private_data = aacirun;
  360. runtime->hw = aaci_hw_info;
  361. /*
  362. * FIXME: ALSA specifies fifo_size in bytes. If we're in normal
  363. * mode, each 32-bit word contains one sample. If we're in
  364. * compact mode, each 32-bit word contains two samples, effectively
  365. * halving the FIFO size. However, we don't know for sure which
  366. * we'll be using at this point. We set this to the lower limit.
  367. */
  368. runtime->hw.fifo_size = aaci->fifosize * 2;
  369. /*
  370. * Add rule describing hardware rate dependency
  371. * on the number of channels.
  372. */
  373. ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  374. aaci_rule_rate_by_channels, aaci,
  375. SNDRV_PCM_HW_PARAM_CHANNELS,
  376. SNDRV_PCM_HW_PARAM_RATE, -1);
  377. if (ret)
  378. goto out;
  379. ret = request_irq(aaci->dev->irq[0], aaci_irq, IRQF_SHARED|IRQF_DISABLED,
  380. DRIVER_NAME, aaci);
  381. if (ret)
  382. goto out;
  383. return 0;
  384. out:
  385. return ret;
  386. }
  387. /*
  388. * Common ALSA stuff
  389. */
  390. static int aaci_pcm_close(struct snd_pcm_substream *substream)
  391. {
  392. struct aaci *aaci = substream->private_data;
  393. struct aaci_runtime *aacirun = substream->runtime->private_data;
  394. WARN_ON(aacirun->cr & CR_EN);
  395. aacirun->substream = NULL;
  396. free_irq(aaci->dev->irq[0], aaci);
  397. return 0;
  398. }
  399. static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
  400. {
  401. struct aaci_runtime *aacirun = substream->runtime->private_data;
  402. /*
  403. * This must not be called with the device enabled.
  404. */
  405. WARN_ON(aacirun->cr & CR_EN);
  406. if (aacirun->pcm_open)
  407. snd_ac97_pcm_close(aacirun->pcm);
  408. aacirun->pcm_open = 0;
  409. /*
  410. * Clear out the DMA and any allocated buffers.
  411. */
  412. devdma_hw_free(NULL, substream);
  413. return 0;
  414. }
  415. static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
  416. struct aaci_runtime *aacirun,
  417. struct snd_pcm_hw_params *params)
  418. {
  419. int err;
  420. aaci_pcm_hw_free(substream);
  421. err = devdma_hw_alloc(NULL, substream,
  422. params_buffer_bytes(params));
  423. if (err < 0)
  424. goto out;
  425. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  426. err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
  427. params_channels(params),
  428. aacirun->pcm->r[0].slots);
  429. else
  430. err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
  431. params_channels(params),
  432. aacirun->pcm->r[1].slots);
  433. if (err)
  434. goto out;
  435. aacirun->pcm_open = 1;
  436. out:
  437. return err;
  438. }
  439. static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
  440. {
  441. struct snd_pcm_runtime *runtime = substream->runtime;
  442. struct aaci_runtime *aacirun = runtime->private_data;
  443. aacirun->start = (void *)runtime->dma_area;
  444. aacirun->end = aacirun->start + runtime->dma_bytes;
  445. aacirun->ptr = aacirun->start;
  446. aacirun->period =
  447. aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
  448. return 0;
  449. }
  450. static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
  451. {
  452. struct snd_pcm_runtime *runtime = substream->runtime;
  453. struct aaci_runtime *aacirun = runtime->private_data;
  454. ssize_t bytes = aacirun->ptr - aacirun->start;
  455. return bytes_to_frames(runtime, bytes);
  456. }
  457. static int aaci_pcm_mmap(struct snd_pcm_substream *substream, struct vm_area_struct *vma)
  458. {
  459. return devdma_mmap(NULL, substream, vma);
  460. }
  461. /*
  462. * Playback specific ALSA stuff
  463. */
  464. static const u32 channels_to_txmask[] = {
  465. [2] = CR_SL3 | CR_SL4,
  466. [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
  467. [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
  468. };
  469. /*
  470. * We can support two and four channel audio. Unfortunately
  471. * six channel audio requires a non-standard channel ordering:
  472. * 2 -> FL(3), FR(4)
  473. * 4 -> FL(3), FR(4), SL(7), SR(8)
  474. * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
  475. * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
  476. * This requires an ALSA configuration file to correct.
  477. */
  478. static unsigned int channel_list[] = { 2, 4, 6 };
  479. static int
  480. aaci_rule_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
  481. {
  482. struct aaci *aaci = rule->private;
  483. unsigned int chan_mask = 1 << 0, slots;
  484. /*
  485. * pcms[0] is the our 5.1 PCM instance.
  486. */
  487. slots = aaci->ac97_bus->pcms[0].r[0].slots;
  488. if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
  489. chan_mask |= 1 << 1;
  490. if (slots & (1 << AC97_SLOT_LFE))
  491. chan_mask |= 1 << 2;
  492. }
  493. return snd_interval_list(hw_param_interval(p, rule->var),
  494. ARRAY_SIZE(channel_list), channel_list,
  495. chan_mask);
  496. }
  497. static int aaci_pcm_open(struct snd_pcm_substream *substream)
  498. {
  499. struct aaci *aaci = substream->private_data;
  500. int ret;
  501. /*
  502. * Add rule describing channel dependency.
  503. */
  504. ret = snd_pcm_hw_rule_add(substream->runtime, 0,
  505. SNDRV_PCM_HW_PARAM_CHANNELS,
  506. aaci_rule_channels, aaci,
  507. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  508. if (ret)
  509. return ret;
  510. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  511. ret = __aaci_pcm_open(aaci, substream, &aaci->playback);
  512. } else {
  513. ret = __aaci_pcm_open(aaci, substream, &aaci->capture);
  514. }
  515. return ret;
  516. }
  517. static int aaci_pcm_playback_hw_params(struct snd_pcm_substream *substream,
  518. struct snd_pcm_hw_params *params)
  519. {
  520. struct aaci *aaci = substream->private_data;
  521. struct aaci_runtime *aacirun = substream->runtime->private_data;
  522. unsigned int channels = params_channels(params);
  523. int ret;
  524. WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) ||
  525. !channels_to_txmask[channels]);
  526. ret = aaci_pcm_hw_params(substream, aacirun, params);
  527. /*
  528. * Enable FIFO, compact mode, 16 bits per sample.
  529. * FIXME: double rate slots?
  530. */
  531. if (ret >= 0) {
  532. aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
  533. aacirun->cr |= channels_to_txmask[channels];
  534. aacirun->fifosz = aaci->fifosize * 4;
  535. if (aacirun->cr & CR_COMPACT)
  536. aacirun->fifosz >>= 1;
  537. }
  538. return ret;
  539. }
  540. static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
  541. {
  542. u32 ie;
  543. ie = readl(aacirun->base + AACI_IE);
  544. ie &= ~(IE_URIE|IE_TXIE);
  545. writel(ie, aacirun->base + AACI_IE);
  546. aacirun->cr &= ~CR_EN;
  547. aaci_chan_wait_ready(aacirun);
  548. writel(aacirun->cr, aacirun->base + AACI_TXCR);
  549. }
  550. static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
  551. {
  552. u32 ie;
  553. aaci_chan_wait_ready(aacirun);
  554. aacirun->cr |= CR_EN;
  555. ie = readl(aacirun->base + AACI_IE);
  556. ie |= IE_URIE | IE_TXIE;
  557. writel(ie, aacirun->base + AACI_IE);
  558. writel(aacirun->cr, aacirun->base + AACI_TXCR);
  559. }
  560. static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  561. {
  562. struct aaci *aaci = substream->private_data;
  563. struct aaci_runtime *aacirun = substream->runtime->private_data;
  564. unsigned long flags;
  565. int ret = 0;
  566. spin_lock_irqsave(&aaci->lock, flags);
  567. switch (cmd) {
  568. case SNDRV_PCM_TRIGGER_START:
  569. aaci_pcm_playback_start(aacirun);
  570. break;
  571. case SNDRV_PCM_TRIGGER_RESUME:
  572. aaci_pcm_playback_start(aacirun);
  573. break;
  574. case SNDRV_PCM_TRIGGER_STOP:
  575. aaci_pcm_playback_stop(aacirun);
  576. break;
  577. case SNDRV_PCM_TRIGGER_SUSPEND:
  578. aaci_pcm_playback_stop(aacirun);
  579. break;
  580. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  581. break;
  582. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  583. break;
  584. default:
  585. ret = -EINVAL;
  586. }
  587. spin_unlock_irqrestore(&aaci->lock, flags);
  588. return ret;
  589. }
  590. static struct snd_pcm_ops aaci_playback_ops = {
  591. .open = aaci_pcm_open,
  592. .close = aaci_pcm_close,
  593. .ioctl = snd_pcm_lib_ioctl,
  594. .hw_params = aaci_pcm_playback_hw_params,
  595. .hw_free = aaci_pcm_hw_free,
  596. .prepare = aaci_pcm_prepare,
  597. .trigger = aaci_pcm_playback_trigger,
  598. .pointer = aaci_pcm_pointer,
  599. .mmap = aaci_pcm_mmap,
  600. };
  601. static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream,
  602. struct snd_pcm_hw_params *params)
  603. {
  604. struct aaci *aaci = substream->private_data;
  605. struct aaci_runtime *aacirun = substream->runtime->private_data;
  606. int ret;
  607. ret = aaci_pcm_hw_params(substream, aacirun, params);
  608. if (ret >= 0) {
  609. aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
  610. /* Line in record: slot 3 and 4 */
  611. aacirun->cr |= CR_SL3 | CR_SL4;
  612. aacirun->fifosz = aaci->fifosize * 4;
  613. if (aacirun->cr & CR_COMPACT)
  614. aacirun->fifosz >>= 1;
  615. }
  616. return ret;
  617. }
  618. static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
  619. {
  620. u32 ie;
  621. aaci_chan_wait_ready(aacirun);
  622. ie = readl(aacirun->base + AACI_IE);
  623. ie &= ~(IE_ORIE | IE_RXIE);
  624. writel(ie, aacirun->base+AACI_IE);
  625. aacirun->cr &= ~CR_EN;
  626. writel(aacirun->cr, aacirun->base + AACI_RXCR);
  627. }
  628. static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
  629. {
  630. u32 ie;
  631. aaci_chan_wait_ready(aacirun);
  632. #ifdef DEBUG
  633. /* RX Timeout value: bits 28:17 in RXCR */
  634. aacirun->cr |= 0xf << 17;
  635. #endif
  636. aacirun->cr |= CR_EN;
  637. writel(aacirun->cr, aacirun->base + AACI_RXCR);
  638. ie = readl(aacirun->base + AACI_IE);
  639. ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
  640. writel(ie, aacirun->base + AACI_IE);
  641. }
  642. static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  643. {
  644. struct aaci *aaci = substream->private_data;
  645. struct aaci_runtime *aacirun = substream->runtime->private_data;
  646. unsigned long flags;
  647. int ret = 0;
  648. spin_lock_irqsave(&aaci->lock, flags);
  649. switch (cmd) {
  650. case SNDRV_PCM_TRIGGER_START:
  651. aaci_pcm_capture_start(aacirun);
  652. break;
  653. case SNDRV_PCM_TRIGGER_RESUME:
  654. aaci_pcm_capture_start(aacirun);
  655. break;
  656. case SNDRV_PCM_TRIGGER_STOP:
  657. aaci_pcm_capture_stop(aacirun);
  658. break;
  659. case SNDRV_PCM_TRIGGER_SUSPEND:
  660. aaci_pcm_capture_stop(aacirun);
  661. break;
  662. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  663. break;
  664. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  665. break;
  666. default:
  667. ret = -EINVAL;
  668. }
  669. spin_unlock_irqrestore(&aaci->lock, flags);
  670. return ret;
  671. }
  672. static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
  673. {
  674. struct snd_pcm_runtime *runtime = substream->runtime;
  675. struct aaci *aaci = substream->private_data;
  676. aaci_pcm_prepare(substream);
  677. /* allow changing of sample rate */
  678. aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
  679. aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  680. aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
  681. /* Record select: Mic: 0, Aux: 3, Line: 4 */
  682. aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
  683. return 0;
  684. }
  685. static struct snd_pcm_ops aaci_capture_ops = {
  686. .open = aaci_pcm_open,
  687. .close = aaci_pcm_close,
  688. .ioctl = snd_pcm_lib_ioctl,
  689. .hw_params = aaci_pcm_capture_hw_params,
  690. .hw_free = aaci_pcm_hw_free,
  691. .prepare = aaci_pcm_capture_prepare,
  692. .trigger = aaci_pcm_capture_trigger,
  693. .pointer = aaci_pcm_pointer,
  694. .mmap = aaci_pcm_mmap,
  695. };
  696. /*
  697. * Power Management.
  698. */
  699. #ifdef CONFIG_PM
  700. static int aaci_do_suspend(struct snd_card *card, unsigned int state)
  701. {
  702. struct aaci *aaci = card->private_data;
  703. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  704. snd_pcm_suspend_all(aaci->pcm);
  705. return 0;
  706. }
  707. static int aaci_do_resume(struct snd_card *card, unsigned int state)
  708. {
  709. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  710. return 0;
  711. }
  712. static int aaci_suspend(struct amba_device *dev, pm_message_t state)
  713. {
  714. struct snd_card *card = amba_get_drvdata(dev);
  715. return card ? aaci_do_suspend(card) : 0;
  716. }
  717. static int aaci_resume(struct amba_device *dev)
  718. {
  719. struct snd_card *card = amba_get_drvdata(dev);
  720. return card ? aaci_do_resume(card) : 0;
  721. }
  722. #else
  723. #define aaci_do_suspend NULL
  724. #define aaci_do_resume NULL
  725. #define aaci_suspend NULL
  726. #define aaci_resume NULL
  727. #endif
  728. static struct ac97_pcm ac97_defs[] __devinitdata = {
  729. [0] = { /* Front PCM */
  730. .exclusive = 1,
  731. .r = {
  732. [0] = {
  733. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  734. (1 << AC97_SLOT_PCM_RIGHT) |
  735. (1 << AC97_SLOT_PCM_CENTER) |
  736. (1 << AC97_SLOT_PCM_SLEFT) |
  737. (1 << AC97_SLOT_PCM_SRIGHT) |
  738. (1 << AC97_SLOT_LFE),
  739. },
  740. },
  741. },
  742. [1] = { /* PCM in */
  743. .stream = 1,
  744. .exclusive = 1,
  745. .r = {
  746. [0] = {
  747. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  748. (1 << AC97_SLOT_PCM_RIGHT),
  749. },
  750. },
  751. },
  752. [2] = { /* Mic in */
  753. .stream = 1,
  754. .exclusive = 1,
  755. .r = {
  756. [0] = {
  757. .slots = (1 << AC97_SLOT_MIC),
  758. },
  759. },
  760. }
  761. };
  762. static struct snd_ac97_bus_ops aaci_bus_ops = {
  763. .write = aaci_ac97_write,
  764. .read = aaci_ac97_read,
  765. };
  766. static int __devinit aaci_probe_ac97(struct aaci *aaci)
  767. {
  768. struct snd_ac97_template ac97_template;
  769. struct snd_ac97_bus *ac97_bus;
  770. struct snd_ac97 *ac97;
  771. int ret;
  772. /*
  773. * Assert AACIRESET for 2us
  774. */
  775. writel(0, aaci->base + AACI_RESET);
  776. udelay(2);
  777. writel(RESET_NRST, aaci->base + AACI_RESET);
  778. /*
  779. * Give the AC'97 codec more than enough time
  780. * to wake up. (42us = ~2 frames at 48kHz.)
  781. */
  782. udelay(42);
  783. ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
  784. if (ret)
  785. goto out;
  786. ac97_bus->clock = 48000;
  787. aaci->ac97_bus = ac97_bus;
  788. memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
  789. ac97_template.private_data = aaci;
  790. ac97_template.num = 0;
  791. ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
  792. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
  793. if (ret)
  794. goto out;
  795. aaci->ac97 = ac97;
  796. /*
  797. * Disable AC97 PC Beep input on audio codecs.
  798. */
  799. if (ac97_is_audio(ac97))
  800. snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
  801. ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
  802. if (ret)
  803. goto out;
  804. aaci->playback.pcm = &ac97_bus->pcms[0];
  805. aaci->capture.pcm = &ac97_bus->pcms[1];
  806. out:
  807. return ret;
  808. }
  809. static void aaci_free_card(struct snd_card *card)
  810. {
  811. struct aaci *aaci = card->private_data;
  812. if (aaci->base)
  813. iounmap(aaci->base);
  814. }
  815. static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
  816. {
  817. struct aaci *aaci;
  818. struct snd_card *card;
  819. card = snd_card_new(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  820. THIS_MODULE, sizeof(struct aaci));
  821. if (card == NULL)
  822. return NULL;
  823. card->private_free = aaci_free_card;
  824. strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
  825. strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
  826. snprintf(card->longname, sizeof(card->longname),
  827. "%s at 0x%016llx, irq %d",
  828. card->shortname, (unsigned long long)dev->res.start,
  829. dev->irq[0]);
  830. aaci = card->private_data;
  831. mutex_init(&aaci->ac97_sem);
  832. spin_lock_init(&aaci->lock);
  833. aaci->card = card;
  834. aaci->dev = dev;
  835. /* Set MAINCR to allow slot 1 and 2 data IO */
  836. aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
  837. MAINCR_SL2RXEN | MAINCR_SL2TXEN;
  838. return aaci;
  839. }
  840. static int __devinit aaci_init_pcm(struct aaci *aaci)
  841. {
  842. struct snd_pcm *pcm;
  843. int ret;
  844. ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
  845. if (ret == 0) {
  846. aaci->pcm = pcm;
  847. pcm->private_data = aaci;
  848. pcm->info_flags = 0;
  849. strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
  850. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
  851. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
  852. }
  853. return ret;
  854. }
  855. static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
  856. {
  857. struct aaci_runtime *aacirun = &aaci->playback;
  858. int i;
  859. writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
  860. for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
  861. writel(0, aacirun->fifo);
  862. writel(0, aacirun->base + AACI_TXCR);
  863. /*
  864. * Re-initialise the AACI after the FIFO depth test, to
  865. * ensure that the FIFOs are empty. Unfortunately, merely
  866. * disabling the channel doesn't clear the FIFO.
  867. */
  868. writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
  869. writel(aaci->maincr, aaci->base + AACI_MAINCR);
  870. /*
  871. * If we hit 4096, we failed. Go back to the specified
  872. * fifo depth.
  873. */
  874. if (i == 4096)
  875. i = 8;
  876. return i;
  877. }
  878. static int __devinit aaci_probe(struct amba_device *dev, void *id)
  879. {
  880. struct aaci *aaci;
  881. int ret, i;
  882. ret = amba_request_regions(dev, NULL);
  883. if (ret)
  884. return ret;
  885. aaci = aaci_init_card(dev);
  886. if (!aaci) {
  887. ret = -ENOMEM;
  888. goto out;
  889. }
  890. aaci->base = ioremap(dev->res.start, SZ_4K);
  891. if (!aaci->base) {
  892. ret = -ENOMEM;
  893. goto out;
  894. }
  895. /*
  896. * Playback uses AACI channel 0
  897. */
  898. aaci->playback.base = aaci->base + AACI_CSCH1;
  899. aaci->playback.fifo = aaci->base + AACI_DR1;
  900. /*
  901. * Capture uses AACI channel 0
  902. */
  903. aaci->capture.base = aaci->base + AACI_CSCH1;
  904. aaci->capture.fifo = aaci->base + AACI_DR1;
  905. for (i = 0; i < 4; i++) {
  906. void __iomem *base = aaci->base + i * 0x14;
  907. writel(0, base + AACI_IE);
  908. writel(0, base + AACI_TXCR);
  909. writel(0, base + AACI_RXCR);
  910. }
  911. writel(0x1fff, aaci->base + AACI_INTCLR);
  912. writel(aaci->maincr, aaci->base + AACI_MAINCR);
  913. ret = aaci_probe_ac97(aaci);
  914. if (ret)
  915. goto out;
  916. /*
  917. * Size the FIFOs (must be multiple of 16).
  918. */
  919. aaci->fifosize = aaci_size_fifo(aaci);
  920. if (aaci->fifosize & 15) {
  921. printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
  922. aaci->fifosize);
  923. ret = -ENODEV;
  924. goto out;
  925. }
  926. ret = aaci_init_pcm(aaci);
  927. if (ret)
  928. goto out;
  929. snd_card_set_dev(aaci->card, &dev->dev);
  930. ret = snd_card_register(aaci->card);
  931. if (ret == 0) {
  932. dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
  933. aaci->fifosize);
  934. amba_set_drvdata(dev, aaci->card);
  935. return ret;
  936. }
  937. out:
  938. if (aaci)
  939. snd_card_free(aaci->card);
  940. amba_release_regions(dev);
  941. return ret;
  942. }
  943. static int __devexit aaci_remove(struct amba_device *dev)
  944. {
  945. struct snd_card *card = amba_get_drvdata(dev);
  946. amba_set_drvdata(dev, NULL);
  947. if (card) {
  948. struct aaci *aaci = card->private_data;
  949. writel(0, aaci->base + AACI_MAINCR);
  950. snd_card_free(card);
  951. amba_release_regions(dev);
  952. }
  953. return 0;
  954. }
  955. static struct amba_id aaci_ids[] = {
  956. {
  957. .id = 0x00041041,
  958. .mask = 0x000fffff,
  959. },
  960. { 0, 0 },
  961. };
  962. static struct amba_driver aaci_driver = {
  963. .drv = {
  964. .name = DRIVER_NAME,
  965. },
  966. .probe = aaci_probe,
  967. .remove = __devexit_p(aaci_remove),
  968. .suspend = aaci_suspend,
  969. .resume = aaci_resume,
  970. .id_table = aaci_ids,
  971. };
  972. static int __init aaci_init(void)
  973. {
  974. return amba_driver_register(&aaci_driver);
  975. }
  976. static void __exit aaci_exit(void)
  977. {
  978. amba_driver_unregister(&aaci_driver);
  979. }
  980. module_init(aaci_init);
  981. module_exit(aaci_exit);
  982. MODULE_LICENSE("GPL");
  983. MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");