pxa2xx-ac97-lib.c 9.8 KB

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