pxa2xx-ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. return 0;
  273. err_irq:
  274. GCR |= GCR_ACLINK_OFF;
  275. #ifdef CONFIG_PXA27x
  276. if (ac97conf_clk) {
  277. clk_put(ac97conf_clk);
  278. ac97conf_clk = NULL;
  279. }
  280. #endif
  281. free_irq(IRQ_AC97, NULL);
  282. err:
  283. return ret;
  284. }
  285. static void pxa2xx_ac97_remove(struct platform_device *pdev)
  286. {
  287. GCR |= GCR_ACLINK_OFF;
  288. free_irq(IRQ_AC97, NULL);
  289. #ifdef CONFIG_PXA27x
  290. clk_put(ac97conf_clk);
  291. ac97conf_clk = NULL;
  292. #endif
  293. clk_disable(ac97_clk);
  294. clk_put(ac97_clk);
  295. ac97_clk = NULL;
  296. }
  297. static int pxa2xx_ac97_hw_params(struct snd_pcm_substream *substream,
  298. struct snd_pcm_hw_params *params)
  299. {
  300. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  301. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  302. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  303. cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_out;
  304. else
  305. cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_in;
  306. return 0;
  307. }
  308. static int pxa2xx_ac97_hw_aux_params(struct snd_pcm_substream *substream,
  309. struct snd_pcm_hw_params *params)
  310. {
  311. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  312. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  313. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  314. cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_out;
  315. else
  316. cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_in;
  317. return 0;
  318. }
  319. static int pxa2xx_ac97_hw_mic_params(struct snd_pcm_substream *substream,
  320. struct snd_pcm_hw_params *params)
  321. {
  322. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  323. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  324. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  325. return -ENODEV;
  326. else
  327. cpu_dai->dma_data = &pxa2xx_ac97_pcm_mic_mono_in;
  328. return 0;
  329. }
  330. #define PXA2XX_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  331. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
  332. SNDRV_PCM_RATE_48000)
  333. /*
  334. * There is only 1 physical AC97 interface for pxa2xx, but it
  335. * has extra fifo's that can be used for aux DACs and ADCs.
  336. */
  337. struct snd_soc_cpu_dai pxa_ac97_dai[] = {
  338. {
  339. .name = "pxa2xx-ac97",
  340. .id = 0,
  341. .type = SND_SOC_DAI_AC97,
  342. .probe = pxa2xx_ac97_probe,
  343. .remove = pxa2xx_ac97_remove,
  344. .suspend = pxa2xx_ac97_suspend,
  345. .resume = pxa2xx_ac97_resume,
  346. .playback = {
  347. .stream_name = "AC97 Playback",
  348. .channels_min = 2,
  349. .channels_max = 2,
  350. .rates = PXA2XX_AC97_RATES,
  351. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  352. .capture = {
  353. .stream_name = "AC97 Capture",
  354. .channels_min = 2,
  355. .channels_max = 2,
  356. .rates = PXA2XX_AC97_RATES,
  357. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  358. .ops = {
  359. .hw_params = pxa2xx_ac97_hw_params,},
  360. },
  361. {
  362. .name = "pxa2xx-ac97-aux",
  363. .id = 1,
  364. .type = SND_SOC_DAI_AC97,
  365. .playback = {
  366. .stream_name = "AC97 Aux Playback",
  367. .channels_min = 1,
  368. .channels_max = 1,
  369. .rates = PXA2XX_AC97_RATES,
  370. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  371. .capture = {
  372. .stream_name = "AC97 Aux Capture",
  373. .channels_min = 1,
  374. .channels_max = 1,
  375. .rates = PXA2XX_AC97_RATES,
  376. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  377. .ops = {
  378. .hw_params = pxa2xx_ac97_hw_aux_params,},
  379. },
  380. {
  381. .name = "pxa2xx-ac97-mic",
  382. .id = 2,
  383. .type = SND_SOC_DAI_AC97,
  384. .capture = {
  385. .stream_name = "AC97 Mic Capture",
  386. .channels_min = 1,
  387. .channels_max = 1,
  388. .rates = PXA2XX_AC97_RATES,
  389. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  390. .ops = {
  391. .hw_params = pxa2xx_ac97_hw_mic_params,},
  392. },
  393. };
  394. EXPORT_SYMBOL_GPL(pxa_ac97_dai);
  395. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  396. MODULE_AUTHOR("Nicolas Pitre");
  397. MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
  398. MODULE_LICENSE("GPL");