pxa2xx-ac97.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * linux/sound/pxa2xx-ac97.c -- AC97 support for the Intel PXA2xx chip.
  3. *
  4. * Author: Nicolas Pitre
  5. * Created: Dec 02, 2004
  6. * Copyright: MontaVista Software Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/wait.h>
  17. #include <linux/clk.h>
  18. #include <linux/delay.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/ac97_codec.h>
  22. #include <sound/initval.h>
  23. #include <sound/soc.h>
  24. #include <asm/irq.h>
  25. #include <linux/mutex.h>
  26. #include <asm/hardware.h>
  27. #include <asm/arch/pxa-regs.h>
  28. #include <asm/arch/pxa2xx-gpio.h>
  29. #include <asm/arch/audio.h>
  30. #include "pxa2xx-pcm.h"
  31. #include "pxa2xx-ac97.h"
  32. static DEFINE_MUTEX(car_mutex);
  33. static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
  34. static volatile long gsr_bits;
  35. static struct clk *ac97_clk;
  36. #ifdef CONFIG_PXA27x
  37. static struct clk *ac97conf_clk;
  38. #endif
  39. /*
  40. * Beware PXA27x bugs:
  41. *
  42. * o Slot 12 read from modem space will hang controller.
  43. * o CDONE, SDONE interrupt fails after any slot 12 IO.
  44. *
  45. * We therefore have an hybrid approach for waiting on SDONE (interrupt or
  46. * 1 jiffy timeout if interrupt never comes).
  47. */
  48. static unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97,
  49. unsigned short reg)
  50. {
  51. unsigned short val = -1;
  52. volatile u32 *reg_addr;
  53. mutex_lock(&car_mutex);
  54. /* set up primary or secondary codec/modem space */
  55. #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
  56. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  57. #else
  58. if (reg == AC97_GPIO_STATUS)
  59. reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
  60. else
  61. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  62. #endif
  63. reg_addr += (reg >> 1);
  64. #ifndef CONFIG_PXA27x
  65. if (reg == AC97_GPIO_STATUS) {
  66. /* read from controller cache */
  67. val = *reg_addr;
  68. goto out;
  69. }
  70. #endif
  71. /* start read access across the ac97 link */
  72. GSR = GSR_CDONE | GSR_SDONE;
  73. gsr_bits = 0;
  74. val = *reg_addr;
  75. wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
  76. if (!((GSR | gsr_bits) & GSR_SDONE)) {
  77. printk(KERN_ERR "%s: read error (ac97_reg=%x GSR=%#lx)\n",
  78. __func__, reg, GSR | gsr_bits);
  79. val = -1;
  80. goto out;
  81. }
  82. /* valid data now */
  83. GSR = GSR_CDONE | GSR_SDONE;
  84. gsr_bits = 0;
  85. val = *reg_addr;
  86. /* but we've just started another cycle... */
  87. wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
  88. out: mutex_unlock(&car_mutex);
  89. return val;
  90. }
  91. static void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  92. unsigned short val)
  93. {
  94. volatile u32 *reg_addr;
  95. mutex_lock(&car_mutex);
  96. /* set up primary or secondary codec/modem space */
  97. #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
  98. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  99. #else
  100. if (reg == AC97_GPIO_STATUS)
  101. reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
  102. else
  103. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  104. #endif
  105. reg_addr += (reg >> 1);
  106. GSR = GSR_CDONE | GSR_SDONE;
  107. gsr_bits = 0;
  108. *reg_addr = val;
  109. wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1);
  110. if (!((GSR | gsr_bits) & GSR_CDONE))
  111. printk(KERN_ERR "%s: write error (ac97_reg=%x GSR=%#lx)\n",
  112. __func__, reg, GSR | gsr_bits);
  113. mutex_unlock(&car_mutex);
  114. }
  115. static void pxa2xx_ac97_warm_reset(struct snd_ac97 *ac97)
  116. {
  117. #ifdef CONFIG_PXA3xx
  118. int timeout = 100;
  119. #endif
  120. gsr_bits = 0;
  121. #ifdef CONFIG_PXA27x
  122. /* warm reset broken on Bulverde,
  123. so manually keep AC97 reset high */
  124. pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
  125. udelay(10);
  126. GCR |= GCR_WARM_RST;
  127. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  128. udelay(500);
  129. #elif defined(CONFIG_PXA3xx)
  130. /* Can't use interrupts */
  131. GCR |= GCR_WARM_RST;
  132. while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
  133. mdelay(1);
  134. #else
  135. GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
  136. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  137. #endif
  138. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
  139. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  140. __func__, gsr_bits);
  141. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  142. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  143. }
  144. static void pxa2xx_ac97_cold_reset(struct snd_ac97 *ac97)
  145. {
  146. #ifdef CONFIG_PXA3xx
  147. int timeout = 1000;
  148. /* Hold CLKBPB for 100us */
  149. GCR = 0;
  150. GCR = GCR_CLKBPB;
  151. udelay(100);
  152. GCR = 0;
  153. #endif
  154. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  155. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  156. gsr_bits = 0;
  157. #ifdef CONFIG_PXA27x
  158. /* PXA27x Developers Manual section 13.5.2.2.1 */
  159. clk_enable(ac97conf_clk);
  160. udelay(5);
  161. clk_disable(ac97conf_clk);
  162. GCR = GCR_COLD_RST;
  163. udelay(50);
  164. #elif defined(CONFIG_PXA3xx)
  165. /* Can't use interrupts on PXA3xx */
  166. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  167. GCR = GCR_WARM_RST | GCR_COLD_RST;
  168. while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
  169. mdelay(10);
  170. #else
  171. GCR = GCR_COLD_RST;
  172. GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
  173. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  174. #endif
  175. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
  176. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  177. __func__, gsr_bits);
  178. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  179. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  180. }
  181. static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
  182. {
  183. long status;
  184. status = GSR;
  185. if (status) {
  186. GSR = status;
  187. gsr_bits |= status;
  188. wake_up(&gsr_wq);
  189. #ifdef CONFIG_PXA27x
  190. /* Although we don't use those we still need to clear them
  191. since they tend to spuriously trigger when MMC is used
  192. (hardware bug? go figure)... */
  193. MISR = MISR_EOC;
  194. PISR = PISR_EOC;
  195. MCSR = MCSR_EOC;
  196. #endif
  197. return IRQ_HANDLED;
  198. }
  199. return IRQ_NONE;
  200. }
  201. struct snd_ac97_bus_ops soc_ac97_ops = {
  202. .read = pxa2xx_ac97_read,
  203. .write = pxa2xx_ac97_write,
  204. .warm_reset = pxa2xx_ac97_warm_reset,
  205. .reset = pxa2xx_ac97_cold_reset,
  206. };
  207. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_out = {
  208. .name = "AC97 PCM Stereo out",
  209. .dev_addr = __PREG(PCDR),
  210. .drcmr = &DRCMRTXPCDR,
  211. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  212. DCMD_BURST32 | DCMD_WIDTH4,
  213. };
  214. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_in = {
  215. .name = "AC97 PCM Stereo in",
  216. .dev_addr = __PREG(PCDR),
  217. .drcmr = &DRCMRRXPCDR,
  218. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  219. DCMD_BURST32 | DCMD_WIDTH4,
  220. };
  221. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_out = {
  222. .name = "AC97 Aux PCM (Slot 5) Mono out",
  223. .dev_addr = __PREG(MODR),
  224. .drcmr = &DRCMRTXMODR,
  225. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  226. DCMD_BURST16 | DCMD_WIDTH2,
  227. };
  228. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_in = {
  229. .name = "AC97 Aux PCM (Slot 5) Mono in",
  230. .dev_addr = __PREG(MODR),
  231. .drcmr = &DRCMRRXMODR,
  232. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  233. DCMD_BURST16 | DCMD_WIDTH2,
  234. };
  235. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_mic_mono_in = {
  236. .name = "AC97 Mic PCM (Slot 6) Mono in",
  237. .dev_addr = __PREG(MCDR),
  238. .drcmr = &DRCMRRXMCDR,
  239. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  240. DCMD_BURST16 | DCMD_WIDTH2,
  241. };
  242. #ifdef CONFIG_PM
  243. static int pxa2xx_ac97_suspend(struct platform_device *pdev,
  244. struct snd_soc_dai *dai)
  245. {
  246. GCR |= GCR_ACLINK_OFF;
  247. clk_disable(ac97_clk);
  248. return 0;
  249. }
  250. static int pxa2xx_ac97_resume(struct platform_device *pdev,
  251. struct snd_soc_dai *dai)
  252. {
  253. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  254. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  255. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  256. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  257. #ifdef CONFIG_PXA27x
  258. /* Use GPIO 113 as AC97 Reset on Bulverde */
  259. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  260. #endif
  261. clk_enable(ac97_clk);
  262. return 0;
  263. }
  264. #else
  265. #define pxa2xx_ac97_suspend NULL
  266. #define pxa2xx_ac97_resume NULL
  267. #endif
  268. static int pxa2xx_ac97_probe(struct platform_device *pdev,
  269. struct snd_soc_dai *dai)
  270. {
  271. int ret;
  272. ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, IRQF_DISABLED, "AC97", NULL);
  273. if (ret < 0)
  274. goto err;
  275. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  276. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  277. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  278. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  279. #ifdef CONFIG_PXA27x
  280. /* Use GPIO 113 as AC97 Reset on Bulverde */
  281. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  282. ac97conf_clk = clk_get(&pdev->dev, "AC97CONFCLK");
  283. if (IS_ERR(ac97conf_clk)) {
  284. ret = PTR_ERR(ac97conf_clk);
  285. ac97conf_clk = NULL;
  286. goto err_irq;
  287. }
  288. #endif
  289. ac97_clk = clk_get(&pdev->dev, "AC97CLK");
  290. if (IS_ERR(ac97_clk)) {
  291. ret = PTR_ERR(ac97_clk);
  292. ac97_clk = NULL;
  293. goto err_irq;
  294. }
  295. clk_enable(ac97_clk);
  296. return 0;
  297. err_irq:
  298. GCR |= GCR_ACLINK_OFF;
  299. #ifdef CONFIG_PXA27x
  300. if (ac97conf_clk) {
  301. clk_put(ac97conf_clk);
  302. ac97conf_clk = NULL;
  303. }
  304. #endif
  305. free_irq(IRQ_AC97, NULL);
  306. err:
  307. return ret;
  308. }
  309. static void pxa2xx_ac97_remove(struct platform_device *pdev,
  310. struct snd_soc_dai *dai)
  311. {
  312. GCR |= GCR_ACLINK_OFF;
  313. free_irq(IRQ_AC97, NULL);
  314. #ifdef CONFIG_PXA27x
  315. clk_put(ac97conf_clk);
  316. ac97conf_clk = NULL;
  317. #endif
  318. clk_disable(ac97_clk);
  319. clk_put(ac97_clk);
  320. ac97_clk = NULL;
  321. }
  322. static int pxa2xx_ac97_hw_params(struct snd_pcm_substream *substream,
  323. struct snd_pcm_hw_params *params)
  324. {
  325. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  326. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  327. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  328. cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_out;
  329. else
  330. cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_in;
  331. return 0;
  332. }
  333. static int pxa2xx_ac97_hw_aux_params(struct snd_pcm_substream *substream,
  334. struct snd_pcm_hw_params *params)
  335. {
  336. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  337. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  338. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  339. cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_out;
  340. else
  341. cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_in;
  342. return 0;
  343. }
  344. static int pxa2xx_ac97_hw_mic_params(struct snd_pcm_substream *substream,
  345. struct snd_pcm_hw_params *params)
  346. {
  347. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  348. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  349. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  350. return -ENODEV;
  351. else
  352. cpu_dai->dma_data = &pxa2xx_ac97_pcm_mic_mono_in;
  353. return 0;
  354. }
  355. #define PXA2XX_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  356. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
  357. SNDRV_PCM_RATE_48000)
  358. /*
  359. * There is only 1 physical AC97 interface for pxa2xx, but it
  360. * has extra fifo's that can be used for aux DACs and ADCs.
  361. */
  362. struct snd_soc_dai pxa_ac97_dai[] = {
  363. {
  364. .name = "pxa2xx-ac97",
  365. .id = 0,
  366. .type = SND_SOC_DAI_AC97,
  367. .probe = pxa2xx_ac97_probe,
  368. .remove = pxa2xx_ac97_remove,
  369. .suspend = pxa2xx_ac97_suspend,
  370. .resume = pxa2xx_ac97_resume,
  371. .playback = {
  372. .stream_name = "AC97 Playback",
  373. .channels_min = 2,
  374. .channels_max = 2,
  375. .rates = PXA2XX_AC97_RATES,
  376. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  377. .capture = {
  378. .stream_name = "AC97 Capture",
  379. .channels_min = 2,
  380. .channels_max = 2,
  381. .rates = PXA2XX_AC97_RATES,
  382. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  383. .ops = {
  384. .hw_params = pxa2xx_ac97_hw_params,},
  385. },
  386. {
  387. .name = "pxa2xx-ac97-aux",
  388. .id = 1,
  389. .type = SND_SOC_DAI_AC97,
  390. .playback = {
  391. .stream_name = "AC97 Aux Playback",
  392. .channels_min = 1,
  393. .channels_max = 1,
  394. .rates = PXA2XX_AC97_RATES,
  395. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  396. .capture = {
  397. .stream_name = "AC97 Aux Capture",
  398. .channels_min = 1,
  399. .channels_max = 1,
  400. .rates = PXA2XX_AC97_RATES,
  401. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  402. .ops = {
  403. .hw_params = pxa2xx_ac97_hw_aux_params,},
  404. },
  405. {
  406. .name = "pxa2xx-ac97-mic",
  407. .id = 2,
  408. .type = SND_SOC_DAI_AC97,
  409. .capture = {
  410. .stream_name = "AC97 Mic Capture",
  411. .channels_min = 1,
  412. .channels_max = 1,
  413. .rates = PXA2XX_AC97_RATES,
  414. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  415. .ops = {
  416. .hw_params = pxa2xx_ac97_hw_mic_params,},
  417. },
  418. };
  419. EXPORT_SYMBOL_GPL(pxa_ac97_dai);
  420. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  421. MODULE_AUTHOR("Nicolas Pitre");
  422. MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
  423. MODULE_LICENSE("GPL");