pxa2xx-ac97-lib.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. unsigned int timeout;
  127. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  128. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  129. gsr_bits = 0;
  130. /* PXA27x Developers Manual section 13.5.2.2.1 */
  131. clk_enable(ac97conf_clk);
  132. udelay(5);
  133. clk_disable(ac97conf_clk);
  134. GCR = GCR_COLD_RST | GCR_WARM_RST;
  135. timeout = 100; /* wait for the codec-ready bit to be set */
  136. while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
  137. mdelay(1);
  138. }
  139. #endif
  140. #ifdef CONFIG_PXA3xx
  141. static inline void pxa_ac97_warm_pxa3xx(void)
  142. {
  143. int timeout = 100;
  144. gsr_bits = 0;
  145. /* Can't use interrupts */
  146. GCR |= GCR_WARM_RST;
  147. while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
  148. mdelay(1);
  149. }
  150. static inline void pxa_ac97_cold_pxa3xx(void)
  151. {
  152. int timeout = 1000;
  153. /* Hold CLKBPB for 100us */
  154. GCR = 0;
  155. GCR = GCR_CLKBPB;
  156. udelay(100);
  157. GCR = 0;
  158. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  159. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  160. gsr_bits = 0;
  161. /* Can't use interrupts on PXA3xx */
  162. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  163. GCR = GCR_WARM_RST | GCR_COLD_RST;
  164. while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
  165. mdelay(10);
  166. }
  167. #endif
  168. bool pxa2xx_ac97_try_warm_reset(struct snd_ac97 *ac97)
  169. {
  170. unsigned long gsr;
  171. #ifdef CONFIG_PXA25x
  172. if (cpu_is_pxa25x())
  173. pxa_ac97_warm_pxa25x();
  174. else
  175. #endif
  176. #ifdef CONFIG_PXA27x
  177. if (cpu_is_pxa27x())
  178. pxa_ac97_warm_pxa27x();
  179. else
  180. #endif
  181. #ifdef CONFIG_PXA3xx
  182. if (cpu_is_pxa3xx())
  183. pxa_ac97_warm_pxa3xx();
  184. else
  185. #endif
  186. BUG();
  187. gsr = GSR | gsr_bits;
  188. if (!(gsr & (GSR_PCR | GSR_SCR))) {
  189. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  190. __func__, gsr);
  191. return false;
  192. }
  193. return true;
  194. }
  195. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
  196. bool pxa2xx_ac97_try_cold_reset(struct snd_ac97 *ac97)
  197. {
  198. unsigned long gsr;
  199. #ifdef CONFIG_PXA25x
  200. if (cpu_is_pxa25x())
  201. pxa_ac97_cold_pxa25x();
  202. else
  203. #endif
  204. #ifdef CONFIG_PXA27x
  205. if (cpu_is_pxa27x())
  206. pxa_ac97_cold_pxa27x();
  207. else
  208. #endif
  209. #ifdef CONFIG_PXA3xx
  210. if (cpu_is_pxa3xx())
  211. pxa_ac97_cold_pxa3xx();
  212. else
  213. #endif
  214. BUG();
  215. gsr = GSR | gsr_bits;
  216. if (!(gsr & (GSR_PCR | GSR_SCR))) {
  217. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  218. __func__, gsr);
  219. return false;
  220. }
  221. return true;
  222. }
  223. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
  224. void pxa2xx_ac97_finish_reset(struct snd_ac97 *ac97)
  225. {
  226. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  227. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  228. }
  229. EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
  230. static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
  231. {
  232. long status;
  233. status = GSR;
  234. if (status) {
  235. GSR = status;
  236. gsr_bits |= status;
  237. wake_up(&gsr_wq);
  238. /* Although we don't use those we still need to clear them
  239. since they tend to spuriously trigger when MMC is used
  240. (hardware bug? go figure)... */
  241. if (cpu_is_pxa27x()) {
  242. MISR = MISR_EOC;
  243. PISR = PISR_EOC;
  244. MCSR = MCSR_EOC;
  245. }
  246. return IRQ_HANDLED;
  247. }
  248. return IRQ_NONE;
  249. }
  250. #ifdef CONFIG_PM
  251. int pxa2xx_ac97_hw_suspend(void)
  252. {
  253. GCR |= GCR_ACLINK_OFF;
  254. clk_disable(ac97_clk);
  255. return 0;
  256. }
  257. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
  258. int pxa2xx_ac97_hw_resume(void)
  259. {
  260. clk_enable(ac97_clk);
  261. return 0;
  262. }
  263. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
  264. #endif
  265. int pxa2xx_ac97_hw_probe(struct platform_device *dev)
  266. {
  267. int ret;
  268. pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
  269. if (pdata) {
  270. switch (pdata->reset_gpio) {
  271. case 95:
  272. case 113:
  273. reset_gpio = pdata->reset_gpio;
  274. break;
  275. case 0:
  276. reset_gpio = 113;
  277. break;
  278. case -1:
  279. break;
  280. default:
  281. dev_err(&dev->dev, "Invalid reset GPIO %d\n",
  282. pdata->reset_gpio);
  283. }
  284. } else {
  285. if (cpu_is_pxa27x())
  286. reset_gpio = 113;
  287. }
  288. if (cpu_is_pxa27x()) {
  289. /* Use GPIO 113 as AC97 Reset on Bulverde */
  290. pxa27x_assert_ac97reset(reset_gpio, 0);
  291. ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
  292. if (IS_ERR(ac97conf_clk)) {
  293. ret = PTR_ERR(ac97conf_clk);
  294. ac97conf_clk = NULL;
  295. goto err_conf;
  296. }
  297. }
  298. ac97_clk = clk_get(&dev->dev, "AC97CLK");
  299. if (IS_ERR(ac97_clk)) {
  300. ret = PTR_ERR(ac97_clk);
  301. ac97_clk = NULL;
  302. goto err_clk;
  303. }
  304. ret = clk_enable(ac97_clk);
  305. if (ret)
  306. goto err_clk2;
  307. ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
  308. if (ret < 0)
  309. goto err_irq;
  310. return 0;
  311. err_irq:
  312. GCR |= GCR_ACLINK_OFF;
  313. err_clk2:
  314. clk_put(ac97_clk);
  315. ac97_clk = NULL;
  316. err_clk:
  317. if (ac97conf_clk) {
  318. clk_put(ac97conf_clk);
  319. ac97conf_clk = NULL;
  320. }
  321. err_conf:
  322. return ret;
  323. }
  324. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
  325. void pxa2xx_ac97_hw_remove(struct platform_device *dev)
  326. {
  327. GCR |= GCR_ACLINK_OFF;
  328. free_irq(IRQ_AC97, NULL);
  329. if (ac97conf_clk) {
  330. clk_put(ac97conf_clk);
  331. ac97conf_clk = NULL;
  332. }
  333. clk_disable(ac97_clk);
  334. clk_put(ac97_clk);
  335. ac97_clk = NULL;
  336. }
  337. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
  338. MODULE_AUTHOR("Nicolas Pitre");
  339. MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
  340. MODULE_LICENSE("GPL");