pxa2xx-ac97-lib.c 9.0 KB

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