pxa2xx-ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #ifdef CONFIG_PXA27x
  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. __FUNCTION__, 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. #ifdef CONFIG_PXA27x
  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. __FUNCTION__, reg, GSR | gsr_bits);
  113. mutex_unlock(&car_mutex);
  114. }
  115. static void pxa2xx_ac97_warm_reset(struct snd_ac97 *ac97)
  116. {
  117. gsr_bits = 0;
  118. #ifdef CONFIG_PXA27x
  119. /* warm reset broken on Bulverde,
  120. so manually keep AC97 reset high */
  121. pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
  122. udelay(10);
  123. GCR |= GCR_WARM_RST;
  124. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  125. udelay(500);
  126. #else
  127. GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
  128. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  129. #endif
  130. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
  131. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  132. __FUNCTION__, gsr_bits);
  133. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  134. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  135. }
  136. static void pxa2xx_ac97_cold_reset(struct snd_ac97 *ac97)
  137. {
  138. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  139. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  140. gsr_bits = 0;
  141. #ifdef CONFIG_PXA27x
  142. /* PXA27x Developers Manual section 13.5.2.2.1 */
  143. clk_enable(ac97conf_clk);
  144. udelay(5);
  145. clk_disable(ac97conf_clk);
  146. GCR = GCR_COLD_RST;
  147. udelay(50);
  148. #else
  149. GCR = GCR_COLD_RST;
  150. GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
  151. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  152. #endif
  153. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
  154. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  155. __FUNCTION__, gsr_bits);
  156. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  157. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  158. }
  159. static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
  160. {
  161. long status;
  162. status = GSR;
  163. if (status) {
  164. GSR = status;
  165. gsr_bits |= status;
  166. wake_up(&gsr_wq);
  167. #ifdef CONFIG_PXA27x
  168. /* Although we don't use those we still need to clear them
  169. since they tend to spuriously trigger when MMC is used
  170. (hardware bug? go figure)... */
  171. MISR = MISR_EOC;
  172. PISR = PISR_EOC;
  173. MCSR = MCSR_EOC;
  174. #endif
  175. return IRQ_HANDLED;
  176. }
  177. return IRQ_NONE;
  178. }
  179. struct snd_ac97_bus_ops soc_ac97_ops = {
  180. .read = pxa2xx_ac97_read,
  181. .write = pxa2xx_ac97_write,
  182. .warm_reset = pxa2xx_ac97_warm_reset,
  183. .reset = pxa2xx_ac97_cold_reset,
  184. };
  185. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_out = {
  186. .name = "AC97 PCM Stereo out",
  187. .dev_addr = __PREG(PCDR),
  188. .drcmr = &DRCMRTXPCDR,
  189. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  190. DCMD_BURST32 | DCMD_WIDTH4,
  191. };
  192. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_in = {
  193. .name = "AC97 PCM Stereo in",
  194. .dev_addr = __PREG(PCDR),
  195. .drcmr = &DRCMRRXPCDR,
  196. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  197. DCMD_BURST32 | DCMD_WIDTH4,
  198. };
  199. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_out = {
  200. .name = "AC97 Aux PCM (Slot 5) Mono out",
  201. .dev_addr = __PREG(MODR),
  202. .drcmr = &DRCMRTXMODR,
  203. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  204. DCMD_BURST16 | DCMD_WIDTH2,
  205. };
  206. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_in = {
  207. .name = "AC97 Aux PCM (Slot 5) Mono in",
  208. .dev_addr = __PREG(MODR),
  209. .drcmr = &DRCMRRXMODR,
  210. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  211. DCMD_BURST16 | DCMD_WIDTH2,
  212. };
  213. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_mic_mono_in = {
  214. .name = "AC97 Mic PCM (Slot 6) Mono in",
  215. .dev_addr = __PREG(MCDR),
  216. .drcmr = &DRCMRRXMCDR,
  217. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  218. DCMD_BURST16 | DCMD_WIDTH2,
  219. };
  220. #ifdef CONFIG_PM
  221. static int pxa2xx_ac97_suspend(struct platform_device *pdev,
  222. struct snd_soc_cpu_dai *dai)
  223. {
  224. GCR |= GCR_ACLINK_OFF;
  225. clk_disable(ac97_clk);
  226. return 0;
  227. }
  228. static int pxa2xx_ac97_resume(struct platform_device *pdev,
  229. struct snd_soc_cpu_dai *dai)
  230. {
  231. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  232. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  233. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  234. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  235. #ifdef CONFIG_PXA27x
  236. /* Use GPIO 113 as AC97 Reset on Bulverde */
  237. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  238. #endif
  239. clk_enable(ac97_clk);
  240. return 0;
  241. }
  242. #else
  243. #define pxa2xx_ac97_suspend NULL
  244. #define pxa2xx_ac97_resume NULL
  245. #endif
  246. static int pxa2xx_ac97_probe(struct platform_device *pdev)
  247. {
  248. int ret;
  249. ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, IRQF_DISABLED, "AC97", NULL);
  250. if (ret < 0)
  251. goto err;
  252. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  253. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  254. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  255. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  256. #ifdef CONFIG_PXA27x
  257. /* Use GPIO 113 as AC97 Reset on Bulverde */
  258. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  259. ac97conf_clk = clk_get(&pdev->dev, "AC97CONFCLK");
  260. if (IS_ERR(ac97conf_clk)) {
  261. ret = PTR_ERR(ac97conf_clk);
  262. ac97conf_clk = NULL;
  263. goto err_irq;
  264. }
  265. #endif
  266. ac97_clk = clk_get(&pdev->dev, "AC97CLK");
  267. if (IS_ERR(ac97_clk)) {
  268. ret = PTR_ERR(ac97_clk);
  269. ac97_clk = NULL;
  270. goto err_irq;
  271. }
  272. clk_enable(ac97_clk);
  273. return 0;
  274. err_irq:
  275. GCR |= GCR_ACLINK_OFF;
  276. #ifdef CONFIG_PXA27x
  277. if (ac97conf_clk) {
  278. clk_put(ac97conf_clk);
  279. ac97conf_clk = NULL;
  280. }
  281. #endif
  282. free_irq(IRQ_AC97, NULL);
  283. err:
  284. return ret;
  285. }
  286. static void pxa2xx_ac97_remove(struct platform_device *pdev)
  287. {
  288. GCR |= GCR_ACLINK_OFF;
  289. free_irq(IRQ_AC97, NULL);
  290. #ifdef CONFIG_PXA27x
  291. clk_put(ac97conf_clk);
  292. ac97conf_clk = NULL;
  293. #endif
  294. clk_disable(ac97_clk);
  295. clk_put(ac97_clk);
  296. ac97_clk = NULL;
  297. }
  298. static int pxa2xx_ac97_hw_params(struct snd_pcm_substream *substream,
  299. struct snd_pcm_hw_params *params)
  300. {
  301. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  302. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  303. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  304. cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_out;
  305. else
  306. cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_in;
  307. return 0;
  308. }
  309. static int pxa2xx_ac97_hw_aux_params(struct snd_pcm_substream *substream,
  310. struct snd_pcm_hw_params *params)
  311. {
  312. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  313. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  314. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  315. cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_out;
  316. else
  317. cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_in;
  318. return 0;
  319. }
  320. static int pxa2xx_ac97_hw_mic_params(struct snd_pcm_substream *substream,
  321. struct snd_pcm_hw_params *params)
  322. {
  323. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  324. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  325. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  326. return -ENODEV;
  327. else
  328. cpu_dai->dma_data = &pxa2xx_ac97_pcm_mic_mono_in;
  329. return 0;
  330. }
  331. #define PXA2XX_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  332. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
  333. SNDRV_PCM_RATE_48000)
  334. /*
  335. * There is only 1 physical AC97 interface for pxa2xx, but it
  336. * has extra fifo's that can be used for aux DACs and ADCs.
  337. */
  338. struct snd_soc_cpu_dai pxa_ac97_dai[] = {
  339. {
  340. .name = "pxa2xx-ac97",
  341. .id = 0,
  342. .type = SND_SOC_DAI_AC97,
  343. .probe = pxa2xx_ac97_probe,
  344. .remove = pxa2xx_ac97_remove,
  345. .suspend = pxa2xx_ac97_suspend,
  346. .resume = pxa2xx_ac97_resume,
  347. .playback = {
  348. .stream_name = "AC97 Playback",
  349. .channels_min = 2,
  350. .channels_max = 2,
  351. .rates = PXA2XX_AC97_RATES,
  352. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  353. .capture = {
  354. .stream_name = "AC97 Capture",
  355. .channels_min = 2,
  356. .channels_max = 2,
  357. .rates = PXA2XX_AC97_RATES,
  358. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  359. .ops = {
  360. .hw_params = pxa2xx_ac97_hw_params,},
  361. },
  362. {
  363. .name = "pxa2xx-ac97-aux",
  364. .id = 1,
  365. .type = SND_SOC_DAI_AC97,
  366. .playback = {
  367. .stream_name = "AC97 Aux Playback",
  368. .channels_min = 1,
  369. .channels_max = 1,
  370. .rates = PXA2XX_AC97_RATES,
  371. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  372. .capture = {
  373. .stream_name = "AC97 Aux Capture",
  374. .channels_min = 1,
  375. .channels_max = 1,
  376. .rates = PXA2XX_AC97_RATES,
  377. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  378. .ops = {
  379. .hw_params = pxa2xx_ac97_hw_aux_params,},
  380. },
  381. {
  382. .name = "pxa2xx-ac97-mic",
  383. .id = 2,
  384. .type = SND_SOC_DAI_AC97,
  385. .capture = {
  386. .stream_name = "AC97 Mic Capture",
  387. .channels_min = 1,
  388. .channels_max = 1,
  389. .rates = PXA2XX_AC97_RATES,
  390. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  391. .ops = {
  392. .hw_params = pxa2xx_ac97_hw_mic_params,},
  393. },
  394. };
  395. EXPORT_SYMBOL_GPL(pxa_ac97_dai);
  396. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  397. MODULE_AUTHOR("Nicolas Pitre");
  398. MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
  399. MODULE_LICENSE("GPL");