pxa2xx-ac97-lib.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. #ifdef CONFIG_PXA25x
  202. if (cpu_is_pxa25x())
  203. pxa_ac97_warm_pxa25x();
  204. else
  205. #endif
  206. #ifdef CONFIG_PXA27x
  207. if (cpu_is_pxa27x())
  208. pxa_ac97_warm_pxa27x();
  209. else
  210. #endif
  211. #ifdef CONFIG_PXA3xx
  212. if (cpu_is_pxa3xx())
  213. pxa_ac97_warm_pxa3xx();
  214. else
  215. #endif
  216. BUG();
  217. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
  218. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  219. __func__, gsr_bits);
  220. return false;
  221. }
  222. return true;
  223. }
  224. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
  225. bool pxa2xx_ac97_try_cold_reset(struct snd_ac97 *ac97)
  226. {
  227. #ifdef CONFIG_PXA25x
  228. if (cpu_is_pxa25x())
  229. pxa_ac97_cold_pxa25x();
  230. else
  231. #endif
  232. #ifdef CONFIG_PXA27x
  233. if (cpu_is_pxa27x())
  234. pxa_ac97_cold_pxa27x();
  235. else
  236. #endif
  237. #ifdef CONFIG_PXA3xx
  238. if (cpu_is_pxa3xx())
  239. pxa_ac97_cold_pxa3xx();
  240. else
  241. #endif
  242. BUG();
  243. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
  244. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  245. __func__, gsr_bits);
  246. return false;
  247. }
  248. return true;
  249. }
  250. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
  251. void pxa2xx_ac97_finish_reset(struct snd_ac97 *ac97)
  252. {
  253. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  254. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  255. }
  256. EXPORT_SYMBOL_GPL(pxa2xx_ac97_finish_reset);
  257. static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
  258. {
  259. long status;
  260. status = GSR;
  261. if (status) {
  262. GSR = status;
  263. gsr_bits |= status;
  264. wake_up(&gsr_wq);
  265. /* Although we don't use those we still need to clear them
  266. since they tend to spuriously trigger when MMC is used
  267. (hardware bug? go figure)... */
  268. if (cpu_is_pxa27x()) {
  269. MISR = MISR_EOC;
  270. PISR = PISR_EOC;
  271. MCSR = MCSR_EOC;
  272. }
  273. return IRQ_HANDLED;
  274. }
  275. return IRQ_NONE;
  276. }
  277. #ifdef CONFIG_PM
  278. int pxa2xx_ac97_hw_suspend(void)
  279. {
  280. GCR |= GCR_ACLINK_OFF;
  281. clk_disable(ac97_clk);
  282. return 0;
  283. }
  284. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_suspend);
  285. int pxa2xx_ac97_hw_resume(void)
  286. {
  287. if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
  288. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  289. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  290. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  291. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  292. }
  293. if (cpu_is_pxa27x()) {
  294. /* Use GPIO 113 or 95 as AC97 Reset on Bulverde */
  295. set_resetgpio_mode(RESETGPIO_NORMAL_ALTFUNC);
  296. }
  297. clk_enable(ac97_clk);
  298. return 0;
  299. }
  300. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_resume);
  301. #endif
  302. int __devinit pxa2xx_ac97_hw_probe(struct platform_device *dev)
  303. {
  304. int ret;
  305. struct pxa2xx_ac97_platform_data *pdata = dev->dev.platform_data;
  306. if (pdata) {
  307. switch (pdata->reset_gpio) {
  308. case 95:
  309. case 113:
  310. reset_gpio = pdata->reset_gpio;
  311. break;
  312. case 0:
  313. reset_gpio = 113;
  314. break;
  315. case -1:
  316. break;
  317. default:
  318. dev_err(&dev->dev, "Invalid reset GPIO %d\n",
  319. pdata->reset_gpio);
  320. }
  321. } else {
  322. if (cpu_is_pxa27x())
  323. reset_gpio = 113;
  324. }
  325. if (cpu_is_pxa25x() || cpu_is_pxa27x()) {
  326. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  327. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  328. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  329. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  330. }
  331. if (cpu_is_pxa27x()) {
  332. /* Use GPIO 113 as AC97 Reset on Bulverde */
  333. set_resetgpio_mode(RESETGPIO_NORMAL_ALTFUNC);
  334. ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
  335. if (IS_ERR(ac97conf_clk)) {
  336. ret = PTR_ERR(ac97conf_clk);
  337. ac97conf_clk = NULL;
  338. goto err_conf;
  339. }
  340. }
  341. ac97_clk = clk_get(&dev->dev, "AC97CLK");
  342. if (IS_ERR(ac97_clk)) {
  343. ret = PTR_ERR(ac97_clk);
  344. ac97_clk = NULL;
  345. goto err_clk;
  346. }
  347. ret = clk_enable(ac97_clk);
  348. if (ret)
  349. goto err_clk2;
  350. ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, IRQF_DISABLED, "AC97", NULL);
  351. if (ret < 0)
  352. goto err_irq;
  353. return 0;
  354. err_irq:
  355. GCR |= GCR_ACLINK_OFF;
  356. err_clk2:
  357. clk_put(ac97_clk);
  358. ac97_clk = NULL;
  359. err_clk:
  360. if (ac97conf_clk) {
  361. clk_put(ac97conf_clk);
  362. ac97conf_clk = NULL;
  363. }
  364. err_conf:
  365. return ret;
  366. }
  367. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
  368. void pxa2xx_ac97_hw_remove(struct platform_device *dev)
  369. {
  370. GCR |= GCR_ACLINK_OFF;
  371. free_irq(IRQ_AC97, NULL);
  372. if (ac97conf_clk) {
  373. clk_put(ac97conf_clk);
  374. ac97conf_clk = NULL;
  375. }
  376. clk_disable(ac97_clk);
  377. clk_put(ac97_clk);
  378. ac97_clk = NULL;
  379. }
  380. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
  381. MODULE_AUTHOR("Nicolas Pitre");
  382. MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
  383. MODULE_LICENSE("GPL");