aaci.c 25 KB

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