pxa2xx-ac97-lib.c 8.4 KB

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