pxa2xx-ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. __func__, 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. __func__, 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. #ifdef CONFIG_PXA3xx
  97. int timeout;
  98. /* Hold CLKBPB for 100us */
  99. GCR = 0;
  100. GCR = GCR_CLKBPB;
  101. udelay(100);
  102. GCR = 0;
  103. #endif
  104. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  105. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  106. gsr_bits = 0;
  107. #ifdef CONFIG_PXA27x
  108. /* PXA27x Developers Manual section 13.5.2.2.1 */
  109. clk_enable(ac97conf_clk);
  110. udelay(5);
  111. clk_disable(ac97conf_clk);
  112. GCR = GCR_COLD_RST;
  113. udelay(50);
  114. #elif defined(CONFIG_PXA3xx)
  115. timeout = 1000;
  116. /* Can't use interrupts on PXA3xx */
  117. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  118. GCR = GCR_WARM_RST | GCR_COLD_RST;
  119. while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
  120. mdelay(10);
  121. #else
  122. GCR = GCR_COLD_RST;
  123. GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
  124. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  125. #endif
  126. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
  127. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  128. __func__, gsr_bits);
  129. /* let's try warm reset */
  130. gsr_bits = 0;
  131. #ifdef CONFIG_PXA27x
  132. /* warm reset broken on Bulverde,
  133. so manually keep AC97 reset high */
  134. pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
  135. udelay(10);
  136. GCR |= GCR_WARM_RST;
  137. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  138. udelay(500);
  139. #elif defined(CONFIG_PXA3xx)
  140. timeout = 100;
  141. /* Can't use interrupts */
  142. GCR |= GCR_WARM_RST;
  143. while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
  144. mdelay(1);
  145. #else
  146. GCR |= GCR_WARM_RST|GCR_PRIRDY_IEN|GCR_SECRDY_IEN;
  147. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  148. #endif
  149. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
  150. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  151. __func__, gsr_bits);
  152. }
  153. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  154. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  155. }
  156. static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
  157. {
  158. long status;
  159. status = GSR;
  160. if (status) {
  161. GSR = status;
  162. gsr_bits |= status;
  163. wake_up(&gsr_wq);
  164. #ifdef CONFIG_PXA27x
  165. /* Although we don't use those we still need to clear them
  166. since they tend to spuriously trigger when MMC is used
  167. (hardware bug? go figure)... */
  168. MISR = MISR_EOC;
  169. PISR = PISR_EOC;
  170. MCSR = MCSR_EOC;
  171. #endif
  172. return IRQ_HANDLED;
  173. }
  174. return IRQ_NONE;
  175. }
  176. static struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
  177. .read = pxa2xx_ac97_read,
  178. .write = pxa2xx_ac97_write,
  179. .reset = pxa2xx_ac97_reset,
  180. };
  181. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_out = {
  182. .name = "AC97 PCM out",
  183. .dev_addr = __PREG(PCDR),
  184. .drcmr = &DRCMRTXPCDR,
  185. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  186. DCMD_BURST32 | DCMD_WIDTH4,
  187. };
  188. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_in = {
  189. .name = "AC97 PCM in",
  190. .dev_addr = __PREG(PCDR),
  191. .drcmr = &DRCMRRXPCDR,
  192. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  193. DCMD_BURST32 | DCMD_WIDTH4,
  194. };
  195. static struct snd_pcm *pxa2xx_ac97_pcm;
  196. static struct snd_ac97 *pxa2xx_ac97_ac97;
  197. static int pxa2xx_ac97_pcm_startup(struct snd_pcm_substream *substream)
  198. {
  199. struct snd_pcm_runtime *runtime = substream->runtime;
  200. pxa2xx_audio_ops_t *platform_ops;
  201. int r;
  202. runtime->hw.channels_min = 2;
  203. runtime->hw.channels_max = 2;
  204. r = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  205. AC97_RATES_FRONT_DAC : AC97_RATES_ADC;
  206. runtime->hw.rates = pxa2xx_ac97_ac97->rates[r];
  207. snd_pcm_limit_hw_rates(runtime);
  208. platform_ops = substream->pcm->card->dev->platform_data;
  209. if (platform_ops && platform_ops->startup)
  210. return platform_ops->startup(substream, platform_ops->priv);
  211. else
  212. return 0;
  213. }
  214. static void pxa2xx_ac97_pcm_shutdown(struct snd_pcm_substream *substream)
  215. {
  216. pxa2xx_audio_ops_t *platform_ops;
  217. platform_ops = substream->pcm->card->dev->platform_data;
  218. if (platform_ops && platform_ops->shutdown)
  219. platform_ops->shutdown(substream, platform_ops->priv);
  220. }
  221. static int pxa2xx_ac97_pcm_prepare(struct snd_pcm_substream *substream)
  222. {
  223. struct snd_pcm_runtime *runtime = substream->runtime;
  224. int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  225. AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
  226. return snd_ac97_set_rate(pxa2xx_ac97_ac97, reg, runtime->rate);
  227. }
  228. static struct pxa2xx_pcm_client pxa2xx_ac97_pcm_client = {
  229. .playback_params = &pxa2xx_ac97_pcm_out,
  230. .capture_params = &pxa2xx_ac97_pcm_in,
  231. .startup = pxa2xx_ac97_pcm_startup,
  232. .shutdown = pxa2xx_ac97_pcm_shutdown,
  233. .prepare = pxa2xx_ac97_pcm_prepare,
  234. };
  235. #ifdef CONFIG_PM
  236. static int pxa2xx_ac97_do_suspend(struct snd_card *card, pm_message_t state)
  237. {
  238. pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
  239. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  240. snd_pcm_suspend_all(pxa2xx_ac97_pcm);
  241. snd_ac97_suspend(pxa2xx_ac97_ac97);
  242. if (platform_ops && platform_ops->suspend)
  243. platform_ops->suspend(platform_ops->priv);
  244. GCR |= GCR_ACLINK_OFF;
  245. clk_disable(ac97_clk);
  246. return 0;
  247. }
  248. static int pxa2xx_ac97_do_resume(struct snd_card *card)
  249. {
  250. pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
  251. clk_enable(ac97_clk);
  252. if (platform_ops && platform_ops->resume)
  253. platform_ops->resume(platform_ops->priv);
  254. snd_ac97_resume(pxa2xx_ac97_ac97);
  255. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  256. return 0;
  257. }
  258. static int pxa2xx_ac97_suspend(struct platform_device *dev, pm_message_t state)
  259. {
  260. struct snd_card *card = platform_get_drvdata(dev);
  261. int ret = 0;
  262. if (card)
  263. ret = pxa2xx_ac97_do_suspend(card, PMSG_SUSPEND);
  264. return ret;
  265. }
  266. static int pxa2xx_ac97_resume(struct platform_device *dev)
  267. {
  268. struct snd_card *card = platform_get_drvdata(dev);
  269. int ret = 0;
  270. if (card)
  271. ret = pxa2xx_ac97_do_resume(card);
  272. return ret;
  273. }
  274. #else
  275. #define pxa2xx_ac97_suspend NULL
  276. #define pxa2xx_ac97_resume NULL
  277. #endif
  278. static int __devinit pxa2xx_ac97_probe(struct platform_device *dev)
  279. {
  280. struct snd_card *card;
  281. struct snd_ac97_bus *ac97_bus;
  282. struct snd_ac97_template ac97_template;
  283. int ret;
  284. ret = -ENOMEM;
  285. card = snd_card_new(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  286. THIS_MODULE, 0);
  287. if (!card)
  288. goto err;
  289. card->dev = &dev->dev;
  290. strncpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
  291. ret = pxa2xx_pcm_new(card, &pxa2xx_ac97_pcm_client, &pxa2xx_ac97_pcm);
  292. if (ret)
  293. goto err;
  294. ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
  295. if (ret < 0)
  296. goto err;
  297. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  298. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  299. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  300. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  301. #ifdef CONFIG_PXA27x
  302. /* Use GPIO 113 as AC97 Reset on Bulverde */
  303. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  304. ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
  305. if (IS_ERR(ac97conf_clk)) {
  306. ret = PTR_ERR(ac97conf_clk);
  307. ac97conf_clk = NULL;
  308. goto err;
  309. }
  310. #endif
  311. ac97_clk = clk_get(&dev->dev, "AC97CLK");
  312. if (IS_ERR(ac97_clk)) {
  313. ret = PTR_ERR(ac97_clk);
  314. ac97_clk = NULL;
  315. goto err;
  316. }
  317. clk_enable(ac97_clk);
  318. ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
  319. if (ret)
  320. goto err;
  321. memset(&ac97_template, 0, sizeof(ac97_template));
  322. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
  323. if (ret)
  324. goto err;
  325. snprintf(card->shortname, sizeof(card->shortname),
  326. "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
  327. snprintf(card->longname, sizeof(card->longname),
  328. "%s (%s)", dev->dev.driver->name, card->mixername);
  329. snd_card_set_dev(card, &dev->dev);
  330. ret = snd_card_register(card);
  331. if (ret == 0) {
  332. platform_set_drvdata(dev, card);
  333. return 0;
  334. }
  335. err:
  336. if (card)
  337. snd_card_free(card);
  338. if (ac97_clk) {
  339. GCR |= GCR_ACLINK_OFF;
  340. free_irq(IRQ_AC97, NULL);
  341. clk_disable(ac97_clk);
  342. clk_put(ac97_clk);
  343. ac97_clk = NULL;
  344. }
  345. #ifdef CONFIG_PXA27x
  346. if (ac97conf_clk) {
  347. clk_put(ac97conf_clk);
  348. ac97conf_clk = NULL;
  349. }
  350. #endif
  351. return ret;
  352. }
  353. static int __devexit pxa2xx_ac97_remove(struct platform_device *dev)
  354. {
  355. struct snd_card *card = platform_get_drvdata(dev);
  356. if (card) {
  357. snd_card_free(card);
  358. platform_set_drvdata(dev, NULL);
  359. GCR |= GCR_ACLINK_OFF;
  360. free_irq(IRQ_AC97, NULL);
  361. clk_disable(ac97_clk);
  362. clk_put(ac97_clk);
  363. ac97_clk = NULL;
  364. #ifdef CONFIG_PXA27x
  365. clk_put(ac97conf_clk);
  366. ac97conf_clk = NULL;
  367. #endif
  368. }
  369. return 0;
  370. }
  371. static struct platform_driver pxa2xx_ac97_driver = {
  372. .probe = pxa2xx_ac97_probe,
  373. .remove = __devexit_p(pxa2xx_ac97_remove),
  374. .suspend = pxa2xx_ac97_suspend,
  375. .resume = pxa2xx_ac97_resume,
  376. .driver = {
  377. .name = "pxa2xx-ac97",
  378. .owner = THIS_MODULE,
  379. },
  380. };
  381. static int __init pxa2xx_ac97_init(void)
  382. {
  383. return platform_driver_register(&pxa2xx_ac97_driver);
  384. }
  385. static void __exit pxa2xx_ac97_exit(void)
  386. {
  387. platform_driver_unregister(&pxa2xx_ac97_driver);
  388. }
  389. module_init(pxa2xx_ac97_init);
  390. module_exit(pxa2xx_ac97_exit);
  391. MODULE_AUTHOR("Nicolas Pitre");
  392. MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
  393. MODULE_LICENSE("GPL");
  394. MODULE_ALIAS("platform:pxa2xx-ac97");