aaci.c 26 KB

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