aaci.c 21 KB

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