pxa2xx-ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/wait.h>
  18. #include <linux/clk.h>
  19. #include <linux/delay.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/ac97_codec.h>
  23. #include <sound/initval.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. static DEFINE_MUTEX(car_mutex);
  32. static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
  33. static volatile long gsr_bits;
  34. static struct clk *ac97_clk;
  35. #ifdef CONFIG_PXA27x
  36. static struct clk *ac97conf_clk;
  37. #endif
  38. /*
  39. * Beware PXA27x bugs:
  40. *
  41. * o Slot 12 read from modem space will hang controller.
  42. * o CDONE, SDONE interrupt fails after any slot 12 IO.
  43. *
  44. * We therefore have an hybrid approach for waiting on SDONE (interrupt or
  45. * 1 jiffy timeout if interrupt never comes).
  46. */
  47. static unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  48. {
  49. unsigned short val = -1;
  50. volatile u32 *reg_addr;
  51. mutex_lock(&car_mutex);
  52. /* set up primary or secondary codec space */
  53. reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
  54. reg_addr += (reg >> 1);
  55. /* start read access across the ac97 link */
  56. GSR = GSR_CDONE | GSR_SDONE;
  57. gsr_bits = 0;
  58. val = *reg_addr;
  59. if (reg == AC97_GPIO_STATUS)
  60. goto out;
  61. if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1) <= 0 &&
  62. !((GSR | gsr_bits) & GSR_SDONE)) {
  63. printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
  64. __FUNCTION__, reg, GSR | gsr_bits);
  65. val = -1;
  66. goto out;
  67. }
  68. /* valid data now */
  69. GSR = GSR_CDONE | GSR_SDONE;
  70. gsr_bits = 0;
  71. val = *reg_addr;
  72. /* but we've just started another cycle... */
  73. wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
  74. out: mutex_unlock(&car_mutex);
  75. return val;
  76. }
  77. static void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
  78. {
  79. volatile u32 *reg_addr;
  80. mutex_lock(&car_mutex);
  81. /* set up primary or secondary codec space */
  82. reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
  83. reg_addr += (reg >> 1);
  84. GSR = GSR_CDONE | GSR_SDONE;
  85. gsr_bits = 0;
  86. *reg_addr = val;
  87. if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1) <= 0 &&
  88. !((GSR | gsr_bits) & GSR_CDONE))
  89. printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
  90. __FUNCTION__, reg, GSR | gsr_bits);
  91. mutex_unlock(&car_mutex);
  92. }
  93. static void pxa2xx_ac97_reset(struct snd_ac97 *ac97)
  94. {
  95. /* First, try cold reset */
  96. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  97. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  98. gsr_bits = 0;
  99. #ifdef CONFIG_PXA27x
  100. /* PXA27x Developers Manual section 13.5.2.2.1 */
  101. clk_enable(ac97conf_clk);
  102. udelay(5);
  103. clk_disable(ac97conf_clk);
  104. GCR = GCR_COLD_RST;
  105. udelay(50);
  106. #else
  107. GCR = GCR_COLD_RST;
  108. GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
  109. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  110. #endif
  111. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
  112. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  113. __FUNCTION__, gsr_bits);
  114. /* let's try warm reset */
  115. gsr_bits = 0;
  116. #ifdef CONFIG_PXA27x
  117. /* warm reset broken on Bulverde,
  118. so manually keep AC97 reset high */
  119. pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
  120. udelay(10);
  121. GCR |= GCR_WARM_RST;
  122. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  123. udelay(500);
  124. #else
  125. GCR |= GCR_WARM_RST|GCR_PRIRDY_IEN|GCR_SECRDY_IEN;
  126. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  127. #endif
  128. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
  129. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  130. __FUNCTION__, gsr_bits);
  131. }
  132. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  133. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  134. }
  135. static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
  136. {
  137. long status;
  138. status = GSR;
  139. if (status) {
  140. GSR = status;
  141. gsr_bits |= status;
  142. wake_up(&gsr_wq);
  143. #ifdef CONFIG_PXA27x
  144. /* Although we don't use those we still need to clear them
  145. since they tend to spuriously trigger when MMC is used
  146. (hardware bug? go figure)... */
  147. MISR = MISR_EOC;
  148. PISR = PISR_EOC;
  149. MCSR = MCSR_EOC;
  150. #endif
  151. return IRQ_HANDLED;
  152. }
  153. return IRQ_NONE;
  154. }
  155. static struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
  156. .read = pxa2xx_ac97_read,
  157. .write = pxa2xx_ac97_write,
  158. .reset = pxa2xx_ac97_reset,
  159. };
  160. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_out = {
  161. .name = "AC97 PCM out",
  162. .dev_addr = __PREG(PCDR),
  163. .drcmr = &DRCMRTXPCDR,
  164. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  165. DCMD_BURST32 | DCMD_WIDTH4,
  166. };
  167. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_in = {
  168. .name = "AC97 PCM in",
  169. .dev_addr = __PREG(PCDR),
  170. .drcmr = &DRCMRRXPCDR,
  171. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  172. DCMD_BURST32 | DCMD_WIDTH4,
  173. };
  174. static struct snd_pcm *pxa2xx_ac97_pcm;
  175. static struct snd_ac97 *pxa2xx_ac97_ac97;
  176. static int pxa2xx_ac97_pcm_startup(struct snd_pcm_substream *substream)
  177. {
  178. struct snd_pcm_runtime *runtime = substream->runtime;
  179. pxa2xx_audio_ops_t *platform_ops;
  180. int r;
  181. runtime->hw.channels_min = 2;
  182. runtime->hw.channels_max = 2;
  183. r = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  184. AC97_RATES_FRONT_DAC : AC97_RATES_ADC;
  185. runtime->hw.rates = pxa2xx_ac97_ac97->rates[r];
  186. snd_pcm_limit_hw_rates(runtime);
  187. platform_ops = substream->pcm->card->dev->platform_data;
  188. if (platform_ops && platform_ops->startup)
  189. return platform_ops->startup(substream, platform_ops->priv);
  190. else
  191. return 0;
  192. }
  193. static void pxa2xx_ac97_pcm_shutdown(struct snd_pcm_substream *substream)
  194. {
  195. pxa2xx_audio_ops_t *platform_ops;
  196. platform_ops = substream->pcm->card->dev->platform_data;
  197. if (platform_ops && platform_ops->shutdown)
  198. platform_ops->shutdown(substream, platform_ops->priv);
  199. }
  200. static int pxa2xx_ac97_pcm_prepare(struct snd_pcm_substream *substream)
  201. {
  202. struct snd_pcm_runtime *runtime = substream->runtime;
  203. int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  204. AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
  205. return snd_ac97_set_rate(pxa2xx_ac97_ac97, reg, runtime->rate);
  206. }
  207. static struct pxa2xx_pcm_client pxa2xx_ac97_pcm_client = {
  208. .playback_params = &pxa2xx_ac97_pcm_out,
  209. .capture_params = &pxa2xx_ac97_pcm_in,
  210. .startup = pxa2xx_ac97_pcm_startup,
  211. .shutdown = pxa2xx_ac97_pcm_shutdown,
  212. .prepare = pxa2xx_ac97_pcm_prepare,
  213. };
  214. #ifdef CONFIG_PM
  215. static int pxa2xx_ac97_do_suspend(struct snd_card *card, pm_message_t state)
  216. {
  217. pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
  218. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  219. snd_pcm_suspend_all(pxa2xx_ac97_pcm);
  220. snd_ac97_suspend(pxa2xx_ac97_ac97);
  221. if (platform_ops && platform_ops->suspend)
  222. platform_ops->suspend(platform_ops->priv);
  223. GCR |= GCR_ACLINK_OFF;
  224. clk_disable(ac97_clk);
  225. return 0;
  226. }
  227. static int pxa2xx_ac97_do_resume(struct snd_card *card)
  228. {
  229. pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
  230. clk_enable(ac97_clk);
  231. if (platform_ops && platform_ops->resume)
  232. platform_ops->resume(platform_ops->priv);
  233. snd_ac97_resume(pxa2xx_ac97_ac97);
  234. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  235. return 0;
  236. }
  237. static int pxa2xx_ac97_suspend(struct platform_device *dev, pm_message_t state)
  238. {
  239. struct snd_card *card = platform_get_drvdata(dev);
  240. int ret = 0;
  241. if (card)
  242. ret = pxa2xx_ac97_do_suspend(card, PMSG_SUSPEND);
  243. return ret;
  244. }
  245. static int pxa2xx_ac97_resume(struct platform_device *dev)
  246. {
  247. struct snd_card *card = platform_get_drvdata(dev);
  248. int ret = 0;
  249. if (card)
  250. ret = pxa2xx_ac97_do_resume(card);
  251. return ret;
  252. }
  253. #else
  254. #define pxa2xx_ac97_suspend NULL
  255. #define pxa2xx_ac97_resume NULL
  256. #endif
  257. static int __devinit pxa2xx_ac97_probe(struct platform_device *dev)
  258. {
  259. struct snd_card *card;
  260. struct snd_ac97_bus *ac97_bus;
  261. struct snd_ac97_template ac97_template;
  262. int ret;
  263. ret = -ENOMEM;
  264. card = snd_card_new(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  265. THIS_MODULE, 0);
  266. if (!card)
  267. goto err;
  268. card->dev = &dev->dev;
  269. strncpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
  270. ret = pxa2xx_pcm_new(card, &pxa2xx_ac97_pcm_client, &pxa2xx_ac97_pcm);
  271. if (ret)
  272. goto err;
  273. ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
  274. if (ret < 0)
  275. goto err;
  276. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  277. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  278. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  279. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  280. #ifdef CONFIG_PXA27x
  281. /* Use GPIO 113 as AC97 Reset on Bulverde */
  282. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  283. ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
  284. if (IS_ERR(ac97conf_clk)) {
  285. ret = PTR_ERR(ac97conf_clk);
  286. ac97conf_clk = NULL;
  287. goto err;
  288. }
  289. #endif
  290. ac97_clk = clk_get(&dev->dev, "AC97CLK");
  291. if (IS_ERR(ac97_clk)) {
  292. ret = PTR_ERR(ac97_clk);
  293. ac97_clk = NULL;
  294. goto err;
  295. }
  296. clk_enable(ac97_clk);
  297. ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
  298. if (ret)
  299. goto err;
  300. memset(&ac97_template, 0, sizeof(ac97_template));
  301. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
  302. if (ret)
  303. goto err;
  304. snprintf(card->shortname, sizeof(card->shortname),
  305. "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
  306. snprintf(card->longname, sizeof(card->longname),
  307. "%s (%s)", dev->dev.driver->name, card->mixername);
  308. snd_card_set_dev(card, &dev->dev);
  309. ret = snd_card_register(card);
  310. if (ret == 0) {
  311. platform_set_drvdata(dev, card);
  312. return 0;
  313. }
  314. err:
  315. if (card)
  316. snd_card_free(card);
  317. if (ac97_clk) {
  318. GCR |= GCR_ACLINK_OFF;
  319. free_irq(IRQ_AC97, NULL);
  320. clk_disable(ac97_clk);
  321. clk_put(ac97_clk);
  322. ac97_clk = NULL;
  323. }
  324. #ifdef CONFIG_PXA27x
  325. if (ac97conf_clk) {
  326. clk_put(ac97conf_clk);
  327. ac97conf_clk = NULL;
  328. }
  329. #endif
  330. return ret;
  331. }
  332. static int __devexit pxa2xx_ac97_remove(struct platform_device *dev)
  333. {
  334. struct snd_card *card = platform_get_drvdata(dev);
  335. if (card) {
  336. snd_card_free(card);
  337. platform_set_drvdata(dev, NULL);
  338. GCR |= GCR_ACLINK_OFF;
  339. free_irq(IRQ_AC97, NULL);
  340. clk_disable(ac97_clk);
  341. clk_put(ac97_clk);
  342. ac97_clk = NULL;
  343. #ifdef CONFIG_PXA27x
  344. clk_put(ac97conf_clk);
  345. ac97conf_clk = NULL;
  346. #endif
  347. }
  348. return 0;
  349. }
  350. static struct platform_driver pxa2xx_ac97_driver = {
  351. .probe = pxa2xx_ac97_probe,
  352. .remove = __devexit_p(pxa2xx_ac97_remove),
  353. .suspend = pxa2xx_ac97_suspend,
  354. .resume = pxa2xx_ac97_resume,
  355. .driver = {
  356. .name = "pxa2xx-ac97",
  357. },
  358. };
  359. static int __init pxa2xx_ac97_init(void)
  360. {
  361. return platform_driver_register(&pxa2xx_ac97_driver);
  362. }
  363. static void __exit pxa2xx_ac97_exit(void)
  364. {
  365. platform_driver_unregister(&pxa2xx_ac97_driver);
  366. }
  367. module_init(pxa2xx_ac97_init);
  368. module_exit(pxa2xx_ac97_exit);
  369. MODULE_AUTHOR("Nicolas Pitre");
  370. MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
  371. MODULE_LICENSE("GPL");