pxa2xx-ac97-lib.c 9.1 KB

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