pxa2xx-ac97-lib.c 8.3 KB

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