pxa2xx-ac97-lib.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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/pxa2xx-gpio.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. /*
  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. enum {
  40. RESETGPIO_FORCE_HIGH,
  41. RESETGPIO_FORCE_LOW,
  42. RESETGPIO_NORMAL_ALTFUNC
  43. };
  44. /**
  45. * set_resetgpio_mode - computes and sets the AC97_RESET gpio mode on PXA
  46. * @mode: chosen action
  47. *
  48. * As the PXA27x CPUs suffer from a AC97 bug, a manual control of the reset line
  49. * must be done to insure proper work of AC97 reset line. This function
  50. * computes the correct gpio_mode for further use by reset functions, and
  51. * applied the change through pxa_gpio_mode.
  52. */
  53. static void set_resetgpio_mode(int resetgpio_action)
  54. {
  55. int mode = 0;
  56. if (reset_gpio)
  57. switch (resetgpio_action) {
  58. case RESETGPIO_NORMAL_ALTFUNC:
  59. if (reset_gpio == 113)
  60. mode = 113 | GPIO_OUT | GPIO_DFLT_LOW;
  61. if (reset_gpio == 95)
  62. mode = 95 | GPIO_ALT_FN_1_OUT;
  63. break;
  64. case RESETGPIO_FORCE_LOW:
  65. mode = reset_gpio | GPIO_OUT | GPIO_DFLT_LOW;
  66. break;
  67. case RESETGPIO_FORCE_HIGH:
  68. mode = reset_gpio | GPIO_OUT | GPIO_DFLT_HIGH;
  69. break;
  70. };
  71. if (mode)
  72. pxa_gpio_mode(mode);
  73. }
  74. unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  75. {
  76. unsigned short val = -1;
  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. /* start read access across the ac97 link */
  86. GSR = GSR_CDONE | GSR_SDONE;
  87. gsr_bits = 0;
  88. val = *reg_addr;
  89. if (reg == AC97_GPIO_STATUS)
  90. goto out;
  91. if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1) <= 0 &&
  92. !((GSR | gsr_bits) & GSR_SDONE)) {
  93. printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
  94. __func__, reg, GSR | gsr_bits);
  95. val = -1;
  96. goto out;
  97. }
  98. /* valid data now */
  99. GSR = GSR_CDONE | GSR_SDONE;
  100. gsr_bits = 0;
  101. val = *reg_addr;
  102. /* but we've just started another cycle... */
  103. wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
  104. out: mutex_unlock(&car_mutex);
  105. return val;
  106. }
  107. EXPORT_SYMBOL_GPL(pxa2xx_ac97_read);
  108. void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  109. unsigned short val)
  110. {
  111. volatile u32 *reg_addr;
  112. mutex_lock(&car_mutex);
  113. /* set up primary or secondary codec space */
  114. if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
  115. reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
  116. else
  117. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  118. reg_addr += (reg >> 1);
  119. GSR = GSR_CDONE | GSR_SDONE;
  120. gsr_bits = 0;
  121. *reg_addr = val;
  122. if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1) <= 0 &&
  123. !((GSR | gsr_bits) & GSR_CDONE))
  124. printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
  125. __func__, reg, GSR | gsr_bits);
  126. mutex_unlock(&car_mutex);
  127. }
  128. EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
  129. #ifdef CONFIG_PXA25x
  130. static inline void pxa_ac97_warm_pxa25x(void)
  131. {
  132. gsr_bits = 0;
  133. GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
  134. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  135. }
  136. static inline void pxa_ac97_cold_pxa25x(void)
  137. {
  138. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  139. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  140. gsr_bits = 0;
  141. GCR = GCR_COLD_RST;
  142. GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
  143. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  144. }
  145. #endif
  146. #ifdef CONFIG_PXA27x
  147. static inline void pxa_ac97_warm_pxa27x(void)
  148. {
  149. gsr_bits = 0;
  150. /* warm reset broken on Bulverde,
  151. so manually keep AC97 reset high */
  152. set_resetgpio_mode(RESETGPIO_FORCE_HIGH);
  153. udelay(10);
  154. GCR |= GCR_WARM_RST;
  155. set_resetgpio_mode(RESETGPIO_NORMAL_ALTFUNC);
  156. udelay(500);
  157. }
  158. static inline void pxa_ac97_cold_pxa27x(void)
  159. {
  160. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  161. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  162. gsr_bits = 0;
  163. /* PXA27x Developers Manual section 13.5.2.2.1 */
  164. clk_enable(ac97conf_clk);
  165. udelay(5);
  166. clk_disable(ac97conf_clk);
  167. GCR = GCR_COLD_RST;
  168. udelay(50);
  169. }
  170. #endif
  171. #ifdef CONFIG_PXA3xx
  172. static inline void pxa_ac97_warm_pxa3xx(void)
  173. {
  174. int timeout = 100;
  175. gsr_bits = 0;
  176. /* Can't use interrupts */
  177. GCR |= GCR_WARM_RST;
  178. while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
  179. mdelay(1);
  180. }
  181. static inline void pxa_ac97_cold_pxa3xx(void)
  182. {
  183. int timeout = 1000;
  184. /* Hold CLKBPB for 100us */
  185. GCR = 0;
  186. GCR = GCR_CLKBPB;
  187. udelay(100);
  188. GCR = 0;
  189. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  190. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  191. gsr_bits = 0;
  192. /* Can't use interrupts on PXA3xx */
  193. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  194. GCR = GCR_WARM_RST | GCR_COLD_RST;
  195. while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
  196. mdelay(10);
  197. }
  198. #endif
  199. bool pxa2xx_ac97_try_warm_reset(struct snd_ac97 *ac97)
  200. {
  201. unsigned long gsr;
  202. #ifdef CONFIG_PXA25x
  203. if (cpu_is_pxa25x())
  204. pxa_ac97_warm_pxa25x();
  205. else
  206. #endif
  207. #ifdef CONFIG_PXA27x
  208. if (cpu_is_pxa27x())
  209. pxa_ac97_warm_pxa27x();
  210. else
  211. #endif
  212. #ifdef CONFIG_PXA3xx
  213. if (cpu_is_pxa3xx())
  214. pxa_ac97_warm_pxa3xx();
  215. else
  216. #endif
  217. BUG();
  218. gsr = GSR | gsr_bits;
  219. if (!(gsr & (GSR_PCR | GSR_SCR))) {
  220. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  221. __func__, gsr);
  222. return false;
  223. }
  224. return true;
  225. }
  226. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
  227. bool pxa2xx_ac97_try_cold_reset(struct snd_ac97 *ac97)
  228. {
  229. unsigned long gsr;
  230. #ifdef CONFIG_PXA25x
  231. if (cpu_is_pxa25x())
  232. pxa_ac97_cold_pxa25x();
  233. else
  234. #endif
  235. #ifdef CONFIG_PXA27x
  236. if (cpu_is_pxa27x())
  237. pxa_ac97_cold_pxa27x();
  238. else
  239. #endif
  240. #ifdef CONFIG_PXA3xx
  241. if (cpu_is_pxa3xx())
  242. pxa_ac97_cold_pxa3xx();
  243. else
  244. #endif
  245. BUG();
  246. gsr = GSR | gsr_bits;
  247. if (!(gsr & (GSR_PCR | GSR_SCR))) {
  248. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  249. __func__, gsr);
  250. return false;
  251. }
  252. return true;
  253. }
  254. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
  255. void pxa2xx_ac97_finish_reset(struct snd_ac97 *ac97)
  256. {
  257. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  258. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  259. }
  260. EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
  261. static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
  262. {
  263. long status;
  264. status = GSR;
  265. if (status) {
  266. GSR = status;
  267. gsr_bits |= status;
  268. wake_up(&gsr_wq);
  269. /* Although we don't use those we still need to clear them
  270. since they tend to spuriously trigger when MMC is used
  271. (hardware bug? go figure)... */
  272. if (cpu_is_pxa27x()) {
  273. MISR = MISR_EOC;
  274. PISR = PISR_EOC;
  275. MCSR = MCSR_EOC;
  276. }
  277. return IRQ_HANDLED;
  278. }
  279. return IRQ_NONE;
  280. }
  281. #ifdef CONFIG_PM
  282. int pxa2xx_ac97_hw_suspend(void)
  283. {
  284. GCR |= GCR_ACLINK_OFF;
  285. clk_disable(ac97_clk);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
  289. int pxa2xx_ac97_hw_resume(void)
  290. {
  291. if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
  292. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  293. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  294. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  295. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  296. }
  297. if (cpu_is_pxa27x()) {
  298. /* Use GPIO 113 or 95 as AC97 Reset on Bulverde */
  299. set_resetgpio_mode(RESETGPIO_NORMAL_ALTFUNC);
  300. }
  301. clk_enable(ac97_clk);
  302. return 0;
  303. }
  304. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
  305. #endif
  306. int __devinit pxa2xx_ac97_hw_probe(struct platform_device *dev)
  307. {
  308. int ret;
  309. struct pxa2xx_ac97_platform_data *pdata = dev->dev.platform_data;
  310. if (pdata) {
  311. switch (pdata->reset_gpio) {
  312. case 95:
  313. case 113:
  314. reset_gpio = pdata->reset_gpio;
  315. break;
  316. case 0:
  317. reset_gpio = 113;
  318. break;
  319. case -1:
  320. break;
  321. default:
  322. dev_err(&dev->dev, "Invalid reset GPIO %d\n",
  323. pdata->reset_gpio);
  324. }
  325. } else {
  326. if (cpu_is_pxa27x())
  327. reset_gpio = 113;
  328. }
  329. if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
  330. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  331. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  332. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  333. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  334. }
  335. if (cpu_is_pxa27x()) {
  336. /* Use GPIO 113 as AC97 Reset on Bulverde */
  337. set_resetgpio_mode(RESETGPIO_NORMAL_ALTFUNC);
  338. ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
  339. if (IS_ERR(ac97conf_clk)) {
  340. ret = PTR_ERR(ac97conf_clk);
  341. ac97conf_clk = NULL;
  342. goto err_conf;
  343. }
  344. }
  345. ac97_clk = clk_get(&dev->dev, "AC97CLK");
  346. if (IS_ERR(ac97_clk)) {
  347. ret = PTR_ERR(ac97_clk);
  348. ac97_clk = NULL;
  349. goto err_clk;
  350. }
  351. ret = clk_enable(ac97_clk);
  352. if (ret)
  353. goto err_clk2;
  354. ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, IRQF_DISABLED, "AC97", NULL);
  355. if (ret < 0)
  356. goto err_irq;
  357. return 0;
  358. err_irq:
  359. GCR |= GCR_ACLINK_OFF;
  360. err_clk2:
  361. clk_put(ac97_clk);
  362. ac97_clk = NULL;
  363. err_clk:
  364. if (ac97conf_clk) {
  365. clk_put(ac97conf_clk);
  366. ac97conf_clk = NULL;
  367. }
  368. err_conf:
  369. return ret;
  370. }
  371. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
  372. void pxa2xx_ac97_hw_remove(struct platform_device *dev)
  373. {
  374. GCR |= GCR_ACLINK_OFF;
  375. free_irq(IRQ_AC97, NULL);
  376. if (ac97conf_clk) {
  377. clk_put(ac97conf_clk);
  378. ac97conf_clk = NULL;
  379. }
  380. clk_disable(ac97_clk);
  381. clk_put(ac97_clk);
  382. ac97_clk = NULL;
  383. }
  384. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
  385. MODULE_AUTHOR("Nicolas Pitre");
  386. MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
  387. MODULE_LICENSE("GPL");