aaci.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. if (aacirun->pcm_open) {
  422. snd_ac97_pcm_close(aacirun->pcm);
  423. aacirun->pcm_open = 0;
  424. }
  425. err = devdma_hw_alloc(NULL, substream,
  426. params_buffer_bytes(params));
  427. if (err < 0)
  428. goto out;
  429. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  430. err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
  431. params_channels(params),
  432. aacirun->pcm->r[0].slots);
  433. else
  434. err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
  435. params_channels(params),
  436. aacirun->pcm->r[0].slots);
  437. if (err)
  438. goto out;
  439. aacirun->pcm_open = 1;
  440. out:
  441. return err;
  442. }
  443. static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
  444. {
  445. struct snd_pcm_runtime *runtime = substream->runtime;
  446. struct aaci_runtime *aacirun = runtime->private_data;
  447. aacirun->start = (void *)runtime->dma_area;
  448. aacirun->end = aacirun->start + runtime->dma_bytes;
  449. aacirun->ptr = aacirun->start;
  450. aacirun->period =
  451. aacirun->bytes = frames_to_bytes(runtime, runtime->period_size);
  452. return 0;
  453. }
  454. static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
  455. {
  456. struct snd_pcm_runtime *runtime = substream->runtime;
  457. struct aaci_runtime *aacirun = runtime->private_data;
  458. ssize_t bytes = aacirun->ptr - aacirun->start;
  459. return bytes_to_frames(runtime, bytes);
  460. }
  461. static int aaci_pcm_mmap(struct snd_pcm_substream *substream, struct vm_area_struct *vma)
  462. {
  463. return devdma_mmap(NULL, substream, vma);
  464. }
  465. /*
  466. * Playback specific ALSA stuff
  467. */
  468. static const u32 channels_to_txmask[] = {
  469. [2] = CR_SL3 | CR_SL4,
  470. [4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
  471. [6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
  472. };
  473. /*
  474. * We can support two and four channel audio. Unfortunately
  475. * six channel audio requires a non-standard channel ordering:
  476. * 2 -> FL(3), FR(4)
  477. * 4 -> FL(3), FR(4), SL(7), SR(8)
  478. * 6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
  479. * FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
  480. * This requires an ALSA configuration file to correct.
  481. */
  482. static unsigned int channel_list[] = { 2, 4, 6 };
  483. static int
  484. aaci_rule_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
  485. {
  486. struct aaci *aaci = rule->private;
  487. unsigned int chan_mask = 1 << 0, slots;
  488. /*
  489. * pcms[0] is the our 5.1 PCM instance.
  490. */
  491. slots = aaci->ac97_bus->pcms[0].r[0].slots;
  492. if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
  493. chan_mask |= 1 << 1;
  494. if (slots & (1 << AC97_SLOT_LFE))
  495. chan_mask |= 1 << 2;
  496. }
  497. return snd_interval_list(hw_param_interval(p, rule->var),
  498. ARRAY_SIZE(channel_list), channel_list,
  499. chan_mask);
  500. }
  501. static int aaci_pcm_open(struct snd_pcm_substream *substream)
  502. {
  503. struct aaci *aaci = substream->private_data;
  504. int ret;
  505. /*
  506. * Add rule describing channel dependency.
  507. */
  508. ret = snd_pcm_hw_rule_add(substream->runtime, 0,
  509. SNDRV_PCM_HW_PARAM_CHANNELS,
  510. aaci_rule_channels, aaci,
  511. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  512. if (ret)
  513. return ret;
  514. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  515. ret = __aaci_pcm_open(aaci, substream, &aaci->playback);
  516. } else {
  517. ret = __aaci_pcm_open(aaci, substream, &aaci->capture);
  518. }
  519. return ret;
  520. }
  521. static int aaci_pcm_playback_hw_params(struct snd_pcm_substream *substream,
  522. struct snd_pcm_hw_params *params)
  523. {
  524. struct aaci *aaci = substream->private_data;
  525. struct aaci_runtime *aacirun = substream->runtime->private_data;
  526. unsigned int channels = params_channels(params);
  527. int ret;
  528. WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) ||
  529. !channels_to_txmask[channels]);
  530. ret = aaci_pcm_hw_params(substream, aacirun, params);
  531. /*
  532. * Enable FIFO, compact mode, 16 bits per sample.
  533. * FIXME: double rate slots?
  534. */
  535. if (ret >= 0) {
  536. aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
  537. aacirun->cr |= channels_to_txmask[channels];
  538. aacirun->fifosz = aaci->fifosize * 4;
  539. if (aacirun->cr & CR_COMPACT)
  540. aacirun->fifosz >>= 1;
  541. }
  542. return ret;
  543. }
  544. static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
  545. {
  546. u32 ie;
  547. ie = readl(aacirun->base + AACI_IE);
  548. ie &= ~(IE_URIE|IE_TXIE);
  549. writel(ie, aacirun->base + AACI_IE);
  550. aacirun->cr &= ~CR_EN;
  551. aaci_chan_wait_ready(aacirun);
  552. writel(aacirun->cr, aacirun->base + AACI_TXCR);
  553. }
  554. static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
  555. {
  556. u32 ie;
  557. aaci_chan_wait_ready(aacirun);
  558. aacirun->cr |= CR_EN;
  559. ie = readl(aacirun->base + AACI_IE);
  560. ie |= IE_URIE | IE_TXIE;
  561. writel(ie, aacirun->base + AACI_IE);
  562. writel(aacirun->cr, aacirun->base + AACI_TXCR);
  563. }
  564. static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  565. {
  566. struct aaci *aaci = substream->private_data;
  567. struct aaci_runtime *aacirun = substream->runtime->private_data;
  568. unsigned long flags;
  569. int ret = 0;
  570. spin_lock_irqsave(&aaci->lock, flags);
  571. switch (cmd) {
  572. case SNDRV_PCM_TRIGGER_START:
  573. aaci_pcm_playback_start(aacirun);
  574. break;
  575. case SNDRV_PCM_TRIGGER_RESUME:
  576. aaci_pcm_playback_start(aacirun);
  577. break;
  578. case SNDRV_PCM_TRIGGER_STOP:
  579. aaci_pcm_playback_stop(aacirun);
  580. break;
  581. case SNDRV_PCM_TRIGGER_SUSPEND:
  582. aaci_pcm_playback_stop(aacirun);
  583. break;
  584. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  585. break;
  586. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  587. break;
  588. default:
  589. ret = -EINVAL;
  590. }
  591. spin_unlock_irqrestore(&aaci->lock, flags);
  592. return ret;
  593. }
  594. static struct snd_pcm_ops aaci_playback_ops = {
  595. .open = aaci_pcm_open,
  596. .close = aaci_pcm_close,
  597. .ioctl = snd_pcm_lib_ioctl,
  598. .hw_params = aaci_pcm_playback_hw_params,
  599. .hw_free = aaci_pcm_hw_free,
  600. .prepare = aaci_pcm_prepare,
  601. .trigger = aaci_pcm_playback_trigger,
  602. .pointer = aaci_pcm_pointer,
  603. .mmap = aaci_pcm_mmap,
  604. };
  605. static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream,
  606. struct snd_pcm_hw_params *params)
  607. {
  608. struct aaci *aaci = substream->private_data;
  609. struct aaci_runtime *aacirun = substream->runtime->private_data;
  610. int ret;
  611. ret = aaci_pcm_hw_params(substream, aacirun, params);
  612. if (ret >= 0) {
  613. aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
  614. /* Line in record: slot 3 and 4 */
  615. aacirun->cr |= CR_SL3 | CR_SL4;
  616. aacirun->fifosz = aaci->fifosize * 4;
  617. if (aacirun->cr & CR_COMPACT)
  618. aacirun->fifosz >>= 1;
  619. }
  620. return ret;
  621. }
  622. static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
  623. {
  624. u32 ie;
  625. aaci_chan_wait_ready(aacirun);
  626. ie = readl(aacirun->base + AACI_IE);
  627. ie &= ~(IE_ORIE | IE_RXIE);
  628. writel(ie, aacirun->base+AACI_IE);
  629. aacirun->cr &= ~CR_EN;
  630. writel(aacirun->cr, aacirun->base + AACI_RXCR);
  631. }
  632. static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
  633. {
  634. u32 ie;
  635. aaci_chan_wait_ready(aacirun);
  636. #ifdef DEBUG
  637. /* RX Timeout value: bits 28:17 in RXCR */
  638. aacirun->cr |= 0xf << 17;
  639. #endif
  640. aacirun->cr |= CR_EN;
  641. writel(aacirun->cr, aacirun->base + AACI_RXCR);
  642. ie = readl(aacirun->base + AACI_IE);
  643. ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
  644. writel(ie, aacirun->base + AACI_IE);
  645. }
  646. static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  647. {
  648. struct aaci *aaci = substream->private_data;
  649. struct aaci_runtime *aacirun = substream->runtime->private_data;
  650. unsigned long flags;
  651. int ret = 0;
  652. spin_lock_irqsave(&aaci->lock, flags);
  653. switch (cmd) {
  654. case SNDRV_PCM_TRIGGER_START:
  655. aaci_pcm_capture_start(aacirun);
  656. break;
  657. case SNDRV_PCM_TRIGGER_RESUME:
  658. aaci_pcm_capture_start(aacirun);
  659. break;
  660. case SNDRV_PCM_TRIGGER_STOP:
  661. aaci_pcm_capture_stop(aacirun);
  662. break;
  663. case SNDRV_PCM_TRIGGER_SUSPEND:
  664. aaci_pcm_capture_stop(aacirun);
  665. break;
  666. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  667. break;
  668. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  669. break;
  670. default:
  671. ret = -EINVAL;
  672. }
  673. spin_unlock_irqrestore(&aaci->lock, flags);
  674. return ret;
  675. }
  676. static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
  677. {
  678. struct snd_pcm_runtime *runtime = substream->runtime;
  679. struct aaci *aaci = substream->private_data;
  680. aaci_pcm_prepare(substream);
  681. /* allow changing of sample rate */
  682. aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
  683. aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  684. aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);
  685. /* Record select: Mic: 0, Aux: 3, Line: 4 */
  686. aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);
  687. return 0;
  688. }
  689. static struct snd_pcm_ops aaci_capture_ops = {
  690. .open = aaci_pcm_open,
  691. .close = aaci_pcm_close,
  692. .ioctl = snd_pcm_lib_ioctl,
  693. .hw_params = aaci_pcm_capture_hw_params,
  694. .hw_free = aaci_pcm_hw_free,
  695. .prepare = aaci_pcm_capture_prepare,
  696. .trigger = aaci_pcm_capture_trigger,
  697. .pointer = aaci_pcm_pointer,
  698. .mmap = aaci_pcm_mmap,
  699. };
  700. /*
  701. * Power Management.
  702. */
  703. #ifdef CONFIG_PM
  704. static int aaci_do_suspend(struct snd_card *card, unsigned int state)
  705. {
  706. struct aaci *aaci = card->private_data;
  707. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  708. snd_pcm_suspend_all(aaci->pcm);
  709. return 0;
  710. }
  711. static int aaci_do_resume(struct snd_card *card, unsigned int state)
  712. {
  713. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  714. return 0;
  715. }
  716. static int aaci_suspend(struct amba_device *dev, pm_message_t state)
  717. {
  718. struct snd_card *card = amba_get_drvdata(dev);
  719. return card ? aaci_do_suspend(card) : 0;
  720. }
  721. static int aaci_resume(struct amba_device *dev)
  722. {
  723. struct snd_card *card = amba_get_drvdata(dev);
  724. return card ? aaci_do_resume(card) : 0;
  725. }
  726. #else
  727. #define aaci_do_suspend NULL
  728. #define aaci_do_resume NULL
  729. #define aaci_suspend NULL
  730. #define aaci_resume NULL
  731. #endif
  732. static struct ac97_pcm ac97_defs[] __devinitdata = {
  733. [0] = { /* Front PCM */
  734. .exclusive = 1,
  735. .r = {
  736. [0] = {
  737. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  738. (1 << AC97_SLOT_PCM_RIGHT) |
  739. (1 << AC97_SLOT_PCM_CENTER) |
  740. (1 << AC97_SLOT_PCM_SLEFT) |
  741. (1 << AC97_SLOT_PCM_SRIGHT) |
  742. (1 << AC97_SLOT_LFE),
  743. },
  744. },
  745. },
  746. [1] = { /* PCM in */
  747. .stream = 1,
  748. .exclusive = 1,
  749. .r = {
  750. [0] = {
  751. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  752. (1 << AC97_SLOT_PCM_RIGHT),
  753. },
  754. },
  755. },
  756. [2] = { /* Mic in */
  757. .stream = 1,
  758. .exclusive = 1,
  759. .r = {
  760. [0] = {
  761. .slots = (1 << AC97_SLOT_MIC),
  762. },
  763. },
  764. }
  765. };
  766. static struct snd_ac97_bus_ops aaci_bus_ops = {
  767. .write = aaci_ac97_write,
  768. .read = aaci_ac97_read,
  769. };
  770. static int __devinit aaci_probe_ac97(struct aaci *aaci)
  771. {
  772. struct snd_ac97_template ac97_template;
  773. struct snd_ac97_bus *ac97_bus;
  774. struct snd_ac97 *ac97;
  775. int ret;
  776. writel(0, aaci->base + AC97_POWERDOWN);
  777. /*
  778. * Assert AACIRESET for 2us
  779. */
  780. writel(0, aaci->base + AACI_RESET);
  781. udelay(2);
  782. writel(RESET_NRST, aaci->base + AACI_RESET);
  783. /*
  784. * Give the AC'97 codec more than enough time
  785. * to wake up. (42us = ~2 frames at 48kHz.)
  786. */
  787. udelay(42);
  788. ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
  789. if (ret)
  790. goto out;
  791. ac97_bus->clock = 48000;
  792. aaci->ac97_bus = ac97_bus;
  793. memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
  794. ac97_template.private_data = aaci;
  795. ac97_template.num = 0;
  796. ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
  797. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
  798. if (ret)
  799. goto out;
  800. aaci->ac97 = ac97;
  801. /*
  802. * Disable AC97 PC Beep input on audio codecs.
  803. */
  804. if (ac97_is_audio(ac97))
  805. snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
  806. ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
  807. if (ret)
  808. goto out;
  809. aaci->playback.pcm = &ac97_bus->pcms[0];
  810. aaci->capture.pcm = &ac97_bus->pcms[1];
  811. out:
  812. return ret;
  813. }
  814. static void aaci_free_card(struct snd_card *card)
  815. {
  816. struct aaci *aaci = card->private_data;
  817. if (aaci->base)
  818. iounmap(aaci->base);
  819. }
  820. static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
  821. {
  822. struct aaci *aaci;
  823. struct snd_card *card;
  824. int err;
  825. err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  826. THIS_MODULE, sizeof(struct aaci), &card);
  827. if (err < 0)
  828. return NULL;
  829. card->private_free = aaci_free_card;
  830. strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
  831. strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
  832. snprintf(card->longname, sizeof(card->longname),
  833. "%s at 0x%016llx, irq %d",
  834. card->shortname, (unsigned long long)dev->res.start,
  835. dev->irq[0]);
  836. aaci = card->private_data;
  837. mutex_init(&aaci->ac97_sem);
  838. spin_lock_init(&aaci->lock);
  839. aaci->card = card;
  840. aaci->dev = dev;
  841. /* Set MAINCR to allow slot 1 and 2 data IO */
  842. aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
  843. MAINCR_SL2RXEN | MAINCR_SL2TXEN;
  844. return aaci;
  845. }
  846. static int __devinit aaci_init_pcm(struct aaci *aaci)
  847. {
  848. struct snd_pcm *pcm;
  849. int ret;
  850. ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
  851. if (ret == 0) {
  852. aaci->pcm = pcm;
  853. pcm->private_data = aaci;
  854. pcm->info_flags = 0;
  855. strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
  856. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
  857. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
  858. }
  859. return ret;
  860. }
  861. static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
  862. {
  863. struct aaci_runtime *aacirun = &aaci->playback;
  864. int i;
  865. writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
  866. for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
  867. writel(0, aacirun->fifo);
  868. writel(0, aacirun->base + AACI_TXCR);
  869. /*
  870. * Re-initialise the AACI after the FIFO depth test, to
  871. * ensure that the FIFOs are empty. Unfortunately, merely
  872. * disabling the channel doesn't clear the FIFO.
  873. */
  874. writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
  875. writel(aaci->maincr, aaci->base + AACI_MAINCR);
  876. /*
  877. * If we hit 4096, we failed. Go back to the specified
  878. * fifo depth.
  879. */
  880. if (i == 4096)
  881. i = 8;
  882. return i;
  883. }
  884. static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
  885. {
  886. struct aaci *aaci;
  887. int ret, i;
  888. ret = amba_request_regions(dev, NULL);
  889. if (ret)
  890. return ret;
  891. aaci = aaci_init_card(dev);
  892. if (!aaci) {
  893. ret = -ENOMEM;
  894. goto out;
  895. }
  896. aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
  897. if (!aaci->base) {
  898. ret = -ENOMEM;
  899. goto out;
  900. }
  901. /*
  902. * Playback uses AACI channel 0
  903. */
  904. aaci->playback.base = aaci->base + AACI_CSCH1;
  905. aaci->playback.fifo = aaci->base + AACI_DR1;
  906. /*
  907. * Capture uses AACI channel 0
  908. */
  909. aaci->capture.base = aaci->base + AACI_CSCH1;
  910. aaci->capture.fifo = aaci->base + AACI_DR1;
  911. for (i = 0; i < 4; i++) {
  912. void __iomem *base = aaci->base + i * 0x14;
  913. writel(0, base + AACI_IE);
  914. writel(0, base + AACI_TXCR);
  915. writel(0, base + AACI_RXCR);
  916. }
  917. writel(0x1fff, aaci->base + AACI_INTCLR);
  918. writel(aaci->maincr, aaci->base + AACI_MAINCR);
  919. ret = aaci_probe_ac97(aaci);
  920. if (ret)
  921. goto out;
  922. /*
  923. * Size the FIFOs (must be multiple of 16).
  924. */
  925. aaci->fifosize = aaci_size_fifo(aaci);
  926. if (aaci->fifosize & 15) {
  927. printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
  928. aaci->fifosize);
  929. ret = -ENODEV;
  930. goto out;
  931. }
  932. ret = aaci_init_pcm(aaci);
  933. if (ret)
  934. goto out;
  935. snd_card_set_dev(aaci->card, &dev->dev);
  936. ret = snd_card_register(aaci->card);
  937. if (ret == 0) {
  938. dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
  939. aaci->fifosize);
  940. amba_set_drvdata(dev, aaci->card);
  941. return ret;
  942. }
  943. out:
  944. if (aaci)
  945. snd_card_free(aaci->card);
  946. amba_release_regions(dev);
  947. return ret;
  948. }
  949. static int __devexit aaci_remove(struct amba_device *dev)
  950. {
  951. struct snd_card *card = amba_get_drvdata(dev);
  952. amba_set_drvdata(dev, NULL);
  953. if (card) {
  954. struct aaci *aaci = card->private_data;
  955. writel(0, aaci->base + AACI_MAINCR);
  956. snd_card_free(card);
  957. amba_release_regions(dev);
  958. }
  959. return 0;
  960. }
  961. static struct amba_id aaci_ids[] = {
  962. {
  963. .id = 0x00041041,
  964. .mask = 0x000fffff,
  965. },
  966. { 0, 0 },
  967. };
  968. static struct amba_driver aaci_driver = {
  969. .drv = {
  970. .name = DRIVER_NAME,
  971. },
  972. .probe = aaci_probe,
  973. .remove = __devexit_p(aaci_remove),
  974. .suspend = aaci_suspend,
  975. .resume = aaci_resume,
  976. .id_table = aaci_ids,
  977. };
  978. static int __init aaci_init(void)
  979. {
  980. return amba_driver_register(&aaci_driver);
  981. }
  982. static void __exit aaci_exit(void)
  983. {
  984. amba_driver_unregister(&aaci_driver);
  985. }
  986. module_init(aaci_init);
  987. module_exit(aaci_exit);
  988. MODULE_LICENSE("GPL");
  989. MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");