aaci.c 25 KB

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