pxa2xx-ac97-lib.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Based on sound/arm/pxa2xx-ac97.c and sound/soc/pxa/pxa2xx-ac97.c
  3. * which contain:
  4. *
  5. * Author: Nicolas Pitre
  6. * Created: Dec 02, 2004
  7. * Copyright: MontaVista Software Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <sound/ac97_codec.h>
  19. #include <sound/pxa2xx-lib.h>
  20. #include <asm/irq.h>
  21. #include <mach/hardware.h>
  22. #include <mach/pxa-regs.h>
  23. #include <mach/pxa2xx-gpio.h>
  24. #include <mach/audio.h>
  25. static DEFINE_MUTEX(car_mutex);
  26. static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
  27. static volatile long gsr_bits;
  28. static struct clk *ac97_clk;
  29. #ifdef CONFIG_PXA27x
  30. static struct clk *ac97conf_clk;
  31. #endif
  32. /*
  33. * Beware PXA27x bugs:
  34. *
  35. * o Slot 12 read from modem space will hang controller.
  36. * o CDONE, SDONE interrupt fails after any slot 12 IO.
  37. *
  38. * We therefore have an hybrid approach for waiting on SDONE (interrupt or
  39. * 1 jiffy timeout if interrupt never comes).
  40. */
  41. unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  42. {
  43. unsigned short val = -1;
  44. volatile u32 *reg_addr;
  45. mutex_lock(&car_mutex);
  46. /* set up primary or secondary codec space */
  47. #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
  48. reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
  49. #else
  50. if (reg == AC97_GPIO_STATUS)
  51. reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
  52. else
  53. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  54. #endif
  55. reg_addr += (reg >> 1);
  56. /* start read access across the ac97 link */
  57. GSR = GSR_CDONE | GSR_SDONE;
  58. gsr_bits = 0;
  59. val = *reg_addr;
  60. if (reg == AC97_GPIO_STATUS)
  61. goto out;
  62. if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1) <= 0 &&
  63. !((GSR | gsr_bits) & GSR_SDONE)) {
  64. printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
  65. __func__, reg, GSR | gsr_bits);
  66. val = -1;
  67. goto out;
  68. }
  69. /* valid data now */
  70. GSR = GSR_CDONE | GSR_SDONE;
  71. gsr_bits = 0;
  72. val = *reg_addr;
  73. /* but we've just started another cycle... */
  74. wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
  75. out: mutex_unlock(&car_mutex);
  76. return val;
  77. }
  78. EXPORT_SYMBOL_GPL(pxa2xx_ac97_read);
  79. void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  80. unsigned short val)
  81. {
  82. volatile u32 *reg_addr;
  83. mutex_lock(&car_mutex);
  84. /* set up primary or secondary codec space */
  85. #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
  86. reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
  87. #else
  88. if (reg == AC97_GPIO_STATUS)
  89. reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
  90. else
  91. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  92. #endif
  93. reg_addr += (reg >> 1);
  94. GSR = GSR_CDONE | GSR_SDONE;
  95. gsr_bits = 0;
  96. *reg_addr = val;
  97. if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1) <= 0 &&
  98. !((GSR | gsr_bits) & GSR_CDONE))
  99. printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
  100. __func__, reg, GSR | gsr_bits);
  101. mutex_unlock(&car_mutex);
  102. }
  103. EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
  104. bool pxa2xx_ac97_try_warm_reset(struct snd_ac97 *ac97)
  105. {
  106. #ifdef CONFIG_PXA3xx
  107. int timeout = 100;
  108. #endif
  109. gsr_bits = 0;
  110. #ifdef CONFIG_PXA27x
  111. /* warm reset broken on Bulverde,
  112. so manually keep AC97 reset high */
  113. pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
  114. udelay(10);
  115. GCR |= GCR_WARM_RST;
  116. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  117. udelay(500);
  118. #elif defined(CONFIG_PXA3xx)
  119. /* Can't use interrupts */
  120. GCR |= GCR_WARM_RST;
  121. while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
  122. mdelay(1);
  123. #else
  124. GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
  125. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  126. #endif
  127. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
  128. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  129. __func__, gsr_bits);
  130. return false;
  131. }
  132. return true;
  133. }
  134. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
  135. bool pxa2xx_ac97_try_cold_reset(struct snd_ac97 *ac97)
  136. {
  137. #ifdef CONFIG_PXA3xx
  138. int timeout = 1000;
  139. /* Hold CLKBPB for 100us */
  140. GCR = 0;
  141. GCR = GCR_CLKBPB;
  142. udelay(100);
  143. GCR = 0;
  144. #endif
  145. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  146. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  147. gsr_bits = 0;
  148. #ifdef CONFIG_PXA27x
  149. /* PXA27x Developers Manual section 13.5.2.2.1 */
  150. clk_enable(ac97conf_clk);
  151. udelay(5);
  152. clk_disable(ac97conf_clk);
  153. GCR = GCR_COLD_RST;
  154. udelay(50);
  155. #elif defined(CONFIG_PXA3xx)
  156. /* Can't use interrupts on PXA3xx */
  157. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  158. GCR = GCR_WARM_RST | GCR_COLD_RST;
  159. while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
  160. mdelay(10);
  161. #else
  162. GCR = GCR_COLD_RST;
  163. GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
  164. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  165. #endif
  166. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
  167. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  168. __func__, gsr_bits);
  169. return false;
  170. }
  171. return true;
  172. }
  173. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
  174. void pxa2xx_ac97_finish_reset(struct snd_ac97 *ac97)
  175. {
  176. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  177. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  178. }
  179. EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
  180. static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
  181. {
  182. long status;
  183. status = GSR;
  184. if (status) {
  185. GSR = status;
  186. gsr_bits |= status;
  187. wake_up(&gsr_wq);
  188. #ifdef CONFIG_PXA27x
  189. /* Although we don't use those we still need to clear them
  190. since they tend to spuriously trigger when MMC is used
  191. (hardware bug? go figure)... */
  192. MISR = MISR_EOC;
  193. PISR = PISR_EOC;
  194. MCSR = MCSR_EOC;
  195. #endif
  196. return IRQ_HANDLED;
  197. }
  198. return IRQ_NONE;
  199. }
  200. #ifdef CONFIG_PM
  201. int pxa2xx_ac97_hw_suspend(void)
  202. {
  203. GCR |= GCR_ACLINK_OFF;
  204. clk_disable(ac97_clk);
  205. return 0;
  206. }
  207. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
  208. int pxa2xx_ac97_hw_resume(void)
  209. {
  210. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  211. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  212. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  213. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  214. #ifdef CONFIG_PXA27x
  215. /* Use GPIO 113 as AC97 Reset on Bulverde */
  216. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  217. #endif
  218. clk_enable(ac97_clk);
  219. return 0;
  220. }
  221. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
  222. #endif
  223. int __devinit pxa2xx_ac97_hw_probe(struct platform_device *dev)
  224. {
  225. int ret;
  226. ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
  227. if (ret < 0)
  228. goto err;
  229. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  230. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  231. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  232. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  233. #ifdef CONFIG_PXA27x
  234. /* Use GPIO 113 as AC97 Reset on Bulverde */
  235. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  236. ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
  237. if (IS_ERR(ac97conf_clk)) {
  238. ret = PTR_ERR(ac97conf_clk);
  239. ac97conf_clk = NULL;
  240. goto err_irq;
  241. }
  242. #endif
  243. ac97_clk = clk_get(&dev->dev, "AC97CLK");
  244. if (IS_ERR(ac97_clk)) {
  245. ret = PTR_ERR(ac97_clk);
  246. ac97_clk = NULL;
  247. goto err_irq;
  248. }
  249. return clk_enable(ac97_clk);
  250. err_irq:
  251. GCR |= GCR_ACLINK_OFF;
  252. #ifdef CONFIG_PXA27x
  253. if (ac97conf_clk) {
  254. clk_put(ac97conf_clk);
  255. ac97conf_clk = NULL;
  256. }
  257. #endif
  258. free_irq(IRQ_AC97, NULL);
  259. err:
  260. return ret;
  261. }
  262. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
  263. void pxa2xx_ac97_hw_remove(struct platform_device *dev)
  264. {
  265. GCR |= GCR_ACLINK_OFF;
  266. free_irq(IRQ_AC97, NULL);
  267. #ifdef CONFIG_PXA27x
  268. clk_put(ac97conf_clk);
  269. ac97conf_clk = NULL;
  270. #endif
  271. clk_disable(ac97_clk);
  272. clk_put(ac97_clk);
  273. ac97_clk = NULL;
  274. }
  275. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
  276. MODULE_AUTHOR("Nicolas Pitre");
  277. MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
  278. MODULE_LICENSE("GPL");