pxa2xx-ac97-lib.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. static struct clk *ac97conf_clk;
  30. /*
  31. * Beware PXA27x bugs:
  32. *
  33. * o Slot 12 read from modem space will hang controller.
  34. * o CDONE, SDONE interrupt fails after any slot 12 IO.
  35. *
  36. * We therefore have an hybrid approach for waiting on SDONE (interrupt or
  37. * 1 jiffy timeout if interrupt never comes).
  38. */
  39. unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  40. {
  41. unsigned short val = -1;
  42. volatile u32 *reg_addr;
  43. mutex_lock(&car_mutex);
  44. /* set up primary or secondary codec space */
  45. if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
  46. reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
  47. else
  48. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  49. reg_addr += (reg >> 1);
  50. /* start read access across the ac97 link */
  51. GSR = GSR_CDONE | GSR_SDONE;
  52. gsr_bits = 0;
  53. val = *reg_addr;
  54. if (reg == AC97_GPIO_STATUS)
  55. goto out;
  56. if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1) <= 0 &&
  57. !((GSR | gsr_bits) & GSR_SDONE)) {
  58. printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
  59. __func__, reg, GSR | gsr_bits);
  60. val = -1;
  61. goto out;
  62. }
  63. /* valid data now */
  64. GSR = GSR_CDONE | GSR_SDONE;
  65. gsr_bits = 0;
  66. val = *reg_addr;
  67. /* but we've just started another cycle... */
  68. wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
  69. out: mutex_unlock(&car_mutex);
  70. return val;
  71. }
  72. EXPORT_SYMBOL_GPL(pxa2xx_ac97_read);
  73. void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  74. unsigned short val)
  75. {
  76. volatile u32 *reg_addr;
  77. mutex_lock(&car_mutex);
  78. /* set up primary or secondary codec space */
  79. if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
  80. reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
  81. else
  82. reg_addr = ac97->num ? &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. EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
  94. #ifdef CONFIG_PXA25x
  95. static inline void pxa_ac97_warm_pxa25x(void)
  96. {
  97. gsr_bits = 0;
  98. GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
  99. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  100. }
  101. static inline void pxa_ac97_cold_pxa25x(void)
  102. {
  103. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  104. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  105. gsr_bits = 0;
  106. GCR = GCR_COLD_RST;
  107. GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
  108. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  109. }
  110. #endif
  111. #ifdef CONFIG_PXA27x
  112. static inline void pxa_ac97_warm_pxa27x(void)
  113. {
  114. gsr_bits = 0;
  115. /* warm reset broken on Bulverde,
  116. so manually keep AC97 reset high */
  117. pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
  118. udelay(10);
  119. GCR |= GCR_WARM_RST;
  120. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  121. udelay(500);
  122. }
  123. static inline void pxa_ac97_cold_pxa27x(void)
  124. {
  125. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  126. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  127. gsr_bits = 0;
  128. /* PXA27x Developers Manual section 13.5.2.2.1 */
  129. clk_enable(ac97conf_clk);
  130. udelay(5);
  131. clk_disable(ac97conf_clk);
  132. GCR = GCR_COLD_RST;
  133. udelay(50);
  134. }
  135. #endif
  136. #ifdef CONFIG_PXA3xx
  137. static inline void pxa_ac97_warm_pxa3xx(void)
  138. {
  139. int timeout = 100;
  140. gsr_bits = 0;
  141. /* Can't use interrupts */
  142. GCR |= GCR_WARM_RST;
  143. while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
  144. mdelay(1);
  145. }
  146. static inline void pxa_ac97_cold_pxa3xx(void)
  147. {
  148. int timeout = 1000;
  149. /* Hold CLKBPB for 100us */
  150. GCR = 0;
  151. GCR = GCR_CLKBPB;
  152. udelay(100);
  153. GCR = 0;
  154. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  155. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  156. gsr_bits = 0;
  157. /* Can't use interrupts on PXA3xx */
  158. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  159. GCR = GCR_WARM_RST | GCR_COLD_RST;
  160. while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
  161. mdelay(10);
  162. }
  163. #endif
  164. bool pxa2xx_ac97_try_warm_reset(struct snd_ac97 *ac97)
  165. {
  166. #ifdef CONFIG_PXA25x
  167. if (cpu_is_pxa25x())
  168. pxa_ac97_warm_pxa25x();
  169. else
  170. #endif
  171. #ifdef CONFIG_PXA27x
  172. if (cpu_is_pxa27x())
  173. pxa_ac97_warm_pxa27x();
  174. else
  175. #endif
  176. #ifdef CONFIG_PXA3xx
  177. if (cpu_is_pxa3xx())
  178. pxa_ac97_warm_pxa3xx();
  179. else
  180. #endif
  181. BUG();
  182. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
  183. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  184. __func__, gsr_bits);
  185. return false;
  186. }
  187. return true;
  188. }
  189. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
  190. bool pxa2xx_ac97_try_cold_reset(struct snd_ac97 *ac97)
  191. {
  192. #ifdef CONFIG_PXA25x
  193. if (cpu_is_pxa25x())
  194. pxa_ac97_cold_pxa25x();
  195. else
  196. #endif
  197. #ifdef CONFIG_PXA27x
  198. if (cpu_is_pxa27x())
  199. pxa_ac97_cold_pxa27x();
  200. else
  201. #endif
  202. #ifdef CONFIG_PXA3xx
  203. if (cpu_is_pxa3xx())
  204. pxa_ac97_cold_pxa3xx();
  205. else
  206. #endif
  207. BUG();
  208. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
  209. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  210. __func__, gsr_bits);
  211. return false;
  212. }
  213. return true;
  214. }
  215. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
  216. void pxa2xx_ac97_finish_reset(struct snd_ac97 *ac97)
  217. {
  218. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  219. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  220. }
  221. EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
  222. static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
  223. {
  224. long status;
  225. status = GSR;
  226. if (status) {
  227. GSR = status;
  228. gsr_bits |= status;
  229. wake_up(&gsr_wq);
  230. /* Although we don't use those we still need to clear them
  231. since they tend to spuriously trigger when MMC is used
  232. (hardware bug? go figure)... */
  233. if (cpu_is_pxa27x()) {
  234. MISR = MISR_EOC;
  235. PISR = PISR_EOC;
  236. MCSR = MCSR_EOC;
  237. }
  238. return IRQ_HANDLED;
  239. }
  240. return IRQ_NONE;
  241. }
  242. #ifdef CONFIG_PM
  243. int pxa2xx_ac97_hw_suspend(void)
  244. {
  245. GCR |= GCR_ACLINK_OFF;
  246. clk_disable(ac97_clk);
  247. return 0;
  248. }
  249. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
  250. int pxa2xx_ac97_hw_resume(void)
  251. {
  252. if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
  253. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  254. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  255. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  256. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  257. }
  258. if (cpu_is_pxa27x()) {
  259. /* Use GPIO 113 as AC97 Reset on Bulverde */
  260. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  261. }
  262. clk_enable(ac97_clk);
  263. return 0;
  264. }
  265. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
  266. #endif
  267. int __devinit pxa2xx_ac97_hw_probe(struct platform_device *dev)
  268. {
  269. int ret;
  270. ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
  271. if (ret < 0)
  272. goto err;
  273. if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
  274. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  275. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  276. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  277. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  278. }
  279. if (cpu_is_pxa27x()) {
  280. /* Use GPIO 113 as AC97 Reset on Bulverde */
  281. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  282. ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
  283. if (IS_ERR(ac97conf_clk)) {
  284. ret = PTR_ERR(ac97conf_clk);
  285. ac97conf_clk = NULL;
  286. goto err_irq;
  287. }
  288. }
  289. ac97_clk = clk_get(&dev->dev, "AC97CLK");
  290. if (IS_ERR(ac97_clk)) {
  291. ret = PTR_ERR(ac97_clk);
  292. ac97_clk = NULL;
  293. goto err_irq;
  294. }
  295. return clk_enable(ac97_clk);
  296. err_irq:
  297. GCR |= GCR_ACLINK_OFF;
  298. if (ac97conf_clk) {
  299. clk_put(ac97conf_clk);
  300. ac97conf_clk = NULL;
  301. }
  302. free_irq(IRQ_AC97, NULL);
  303. err:
  304. return ret;
  305. }
  306. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
  307. void pxa2xx_ac97_hw_remove(struct platform_device *dev)
  308. {
  309. GCR |= GCR_ACLINK_OFF;
  310. free_irq(IRQ_AC97, NULL);
  311. if (ac97conf_clk) {
  312. clk_put(ac97conf_clk);
  313. ac97conf_clk = NULL;
  314. }
  315. clk_disable(ac97_clk);
  316. clk_put(ac97_clk);
  317. ac97_clk = NULL;
  318. }
  319. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
  320. MODULE_AUTHOR("Nicolas Pitre");
  321. MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
  322. MODULE_LICENSE("GPL");