pxa2xx-ac97-lib.c 8.3 KB

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