pxa2xx-ac97-lib.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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/audio.h>
  23. static DEFINE_MUTEX(car_mutex);
  24. static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
  25. static volatile long gsr_bits;
  26. static struct clk *ac97_clk;
  27. static struct clk *ac97conf_clk;
  28. static int reset_gpio;
  29. extern void pxa27x_assert_ac97reset(int reset_gpio, int on);
  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, so manually keep AC97 reset high */
  116. pxa27x_assert_ac97reset(reset_gpio, 1);
  117. udelay(10);
  118. GCR |= GCR_WARM_RST;
  119. pxa27x_assert_ac97reset(reset_gpio, 0);
  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. unsigned long gsr;
  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. gsr = GSR | gsr_bits;
  183. if (!(gsr & (GSR_PCR | GSR_SCR))) {
  184. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  185. __func__, gsr);
  186. return false;
  187. }
  188. return true;
  189. }
  190. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
  191. bool pxa2xx_ac97_try_cold_reset(struct snd_ac97 *ac97)
  192. {
  193. unsigned long gsr;
  194. #ifdef CONFIG_PXA25x
  195. if (cpu_is_pxa25x())
  196. pxa_ac97_cold_pxa25x();
  197. else
  198. #endif
  199. #ifdef CONFIG_PXA27x
  200. if (cpu_is_pxa27x())
  201. pxa_ac97_cold_pxa27x();
  202. else
  203. #endif
  204. #ifdef CONFIG_PXA3xx
  205. if (cpu_is_pxa3xx())
  206. pxa_ac97_cold_pxa3xx();
  207. else
  208. #endif
  209. BUG();
  210. gsr = GSR | gsr_bits;
  211. if (!(gsr & (GSR_PCR | GSR_SCR))) {
  212. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  213. __func__, gsr);
  214. return false;
  215. }
  216. return true;
  217. }
  218. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
  219. void pxa2xx_ac97_finish_reset(struct snd_ac97 *ac97)
  220. {
  221. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  222. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  223. }
  224. EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
  225. static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
  226. {
  227. long status;
  228. status = GSR;
  229. if (status) {
  230. GSR = status;
  231. gsr_bits |= status;
  232. wake_up(&gsr_wq);
  233. /* Although we don't use those we still need to clear them
  234. since they tend to spuriously trigger when MMC is used
  235. (hardware bug? go figure)... */
  236. if (cpu_is_pxa27x()) {
  237. MISR = MISR_EOC;
  238. PISR = PISR_EOC;
  239. MCSR = MCSR_EOC;
  240. }
  241. return IRQ_HANDLED;
  242. }
  243. return IRQ_NONE;
  244. }
  245. #ifdef CONFIG_PM
  246. int pxa2xx_ac97_hw_suspend(void)
  247. {
  248. GCR |= GCR_ACLINK_OFF;
  249. clk_disable(ac97_clk);
  250. return 0;
  251. }
  252. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
  253. int pxa2xx_ac97_hw_resume(void)
  254. {
  255. clk_enable(ac97_clk);
  256. return 0;
  257. }
  258. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
  259. #endif
  260. int __devinit pxa2xx_ac97_hw_probe(struct platform_device *dev)
  261. {
  262. int ret;
  263. pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
  264. if (pdata) {
  265. switch (pdata->reset_gpio) {
  266. case 95:
  267. case 113:
  268. reset_gpio = pdata->reset_gpio;
  269. break;
  270. case 0:
  271. reset_gpio = 113;
  272. break;
  273. case -1:
  274. break;
  275. default:
  276. dev_err(&dev->dev, "Invalid reset GPIO %d\n",
  277. pdata->reset_gpio);
  278. }
  279. } else {
  280. if (cpu_is_pxa27x())
  281. reset_gpio = 113;
  282. }
  283. if (cpu_is_pxa27x()) {
  284. /* Use GPIO 113 as AC97 Reset on Bulverde */
  285. pxa27x_assert_ac97reset(reset_gpio, 0);
  286. ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
  287. if (IS_ERR(ac97conf_clk)) {
  288. ret = PTR_ERR(ac97conf_clk);
  289. ac97conf_clk = NULL;
  290. goto err_conf;
  291. }
  292. }
  293. ac97_clk = clk_get(&dev->dev, "AC97CLK");
  294. if (IS_ERR(ac97_clk)) {
  295. ret = PTR_ERR(ac97_clk);
  296. ac97_clk = NULL;
  297. goto err_clk;
  298. }
  299. ret = clk_enable(ac97_clk);
  300. if (ret)
  301. goto err_clk2;
  302. ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, IRQF_DISABLED, "AC97", NULL);
  303. if (ret < 0)
  304. goto err_irq;
  305. return 0;
  306. err_irq:
  307. GCR |= GCR_ACLINK_OFF;
  308. err_clk2:
  309. clk_put(ac97_clk);
  310. ac97_clk = NULL;
  311. err_clk:
  312. if (ac97conf_clk) {
  313. clk_put(ac97conf_clk);
  314. ac97conf_clk = NULL;
  315. }
  316. err_conf:
  317. return ret;
  318. }
  319. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
  320. void pxa2xx_ac97_hw_remove(struct platform_device *dev)
  321. {
  322. GCR |= GCR_ACLINK_OFF;
  323. free_irq(IRQ_AC97, NULL);
  324. if (ac97conf_clk) {
  325. clk_put(ac97conf_clk);
  326. ac97conf_clk = NULL;
  327. }
  328. clk_disable(ac97_clk);
  329. clk_put(ac97_clk);
  330. ac97_clk = NULL;
  331. }
  332. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
  333. MODULE_AUTHOR("Nicolas Pitre");
  334. MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
  335. MODULE_LICENSE("GPL");