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