pxa2xx-ac97-lib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on sound/arm/pxa2xx-ac97.c and sound/soc/pxa/pxa2xx-ac97.c
  4. * which contain:
  5. *
  6. * Author: Nicolas Pitre
  7. * Created: Dec 02, 2004
  8. * Copyright: MontaVista Software Inc.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/module.h>
  17. #include <linux/io.h>
  18. #include <linux/soc/pxa/cpu.h>
  19. #include <sound/pxa2xx-lib.h>
  20. #include <linux/platform_data/asoc-pxa.h>
  21. #include "pxa2xx-ac97-regs.h"
  22. static DEFINE_MUTEX(car_mutex);
  23. static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
  24. static volatile long gsr_bits;
  25. static struct clk *ac97_clk;
  26. static struct clk *ac97conf_clk;
  27. static int reset_gpio;
  28. struct gpio_desc *rst_gpio;
  29. static void __iomem *ac97_reg_base;
  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. int pxa2xx_ac97_read(int slot, unsigned short reg)
  40. {
  41. int val = -ENODEV;
  42. u32 __iomem *reg_addr;
  43. if (slot > 0)
  44. return -ENODEV;
  45. guard(mutex)(&car_mutex);
  46. /* set up primary or secondary codec space */
  47. if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
  48. reg_addr = ac97_reg_base +
  49. (slot ? SMC_REG_BASE : PMC_REG_BASE);
  50. else
  51. reg_addr = ac97_reg_base +
  52. (slot ? SAC_REG_BASE : PAC_REG_BASE);
  53. reg_addr += (reg >> 1);
  54. /* start read access across the ac97 link */
  55. writel(GSR_CDONE | GSR_SDONE, ac97_reg_base + GSR);
  56. gsr_bits = 0;
  57. val = (readl(reg_addr) & 0xffff);
  58. if (reg == AC97_GPIO_STATUS)
  59. return val;
  60. if (wait_event_timeout(gsr_wq, (readl(ac97_reg_base + GSR) | gsr_bits) & GSR_SDONE, 1) <= 0 &&
  61. !((readl(ac97_reg_base + GSR) | gsr_bits) & GSR_SDONE)) {
  62. printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
  63. __func__, reg, readl(ac97_reg_base + GSR) | gsr_bits);
  64. return -ETIMEDOUT;
  65. }
  66. /* valid data now */
  67. writel(GSR_CDONE | GSR_SDONE, ac97_reg_base + GSR);
  68. gsr_bits = 0;
  69. val = (readl(reg_addr) & 0xffff);
  70. /* but we've just started another cycle... */
  71. wait_event_timeout(gsr_wq, (readl(ac97_reg_base + GSR) | gsr_bits) & GSR_SDONE, 1);
  72. return val;
  73. }
  74. EXPORT_SYMBOL_GPL(pxa2xx_ac97_read);
  75. int pxa2xx_ac97_write(int slot, unsigned short reg, unsigned short val)
  76. {
  77. u32 __iomem *reg_addr;
  78. int ret = 0;
  79. guard(mutex)(&car_mutex);
  80. /* set up primary or secondary codec space */
  81. if (cpu_is_pxa25x() && reg == AC97_GPIO_STATUS)
  82. reg_addr = ac97_reg_base +
  83. (slot ? SMC_REG_BASE : PMC_REG_BASE);
  84. else
  85. reg_addr = ac97_reg_base +
  86. (slot ? SAC_REG_BASE : PAC_REG_BASE);
  87. reg_addr += (reg >> 1);
  88. writel(GSR_CDONE | GSR_SDONE, ac97_reg_base + GSR);
  89. gsr_bits = 0;
  90. writel(val, reg_addr);
  91. if (wait_event_timeout(gsr_wq, (readl(ac97_reg_base + GSR) | gsr_bits) & GSR_CDONE, 1) <= 0 &&
  92. !((readl(ac97_reg_base + GSR) | gsr_bits) & GSR_CDONE)) {
  93. printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
  94. __func__, reg, readl(ac97_reg_base + GSR) | gsr_bits);
  95. ret = -EIO;
  96. }
  97. return ret;
  98. }
  99. EXPORT_SYMBOL_GPL(pxa2xx_ac97_write);
  100. #ifdef CONFIG_PXA25x
  101. static inline void pxa_ac97_warm_pxa25x(void)
  102. {
  103. gsr_bits = 0;
  104. writel(readl(ac97_reg_base + GCR) | (GCR_WARM_RST), ac97_reg_base + GCR);
  105. }
  106. static inline void pxa_ac97_cold_pxa25x(void)
  107. {
  108. writel(readl(ac97_reg_base + GCR) & ( GCR_COLD_RST), ac97_reg_base + GCR); /* clear everything but nCRST */
  109. writel(readl(ac97_reg_base + GCR) & (~GCR_COLD_RST), ac97_reg_base + GCR); /* then assert nCRST */
  110. gsr_bits = 0;
  111. writel(GCR_COLD_RST, ac97_reg_base + GCR);
  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_configure_ac97reset(reset_gpio, true);
  120. udelay(10);
  121. writel(readl(ac97_reg_base + GCR) | (GCR_WARM_RST), ac97_reg_base + GCR);
  122. pxa27x_configure_ac97reset(reset_gpio, false);
  123. udelay(500);
  124. }
  125. static inline void pxa_ac97_cold_pxa27x(void)
  126. {
  127. writel(readl(ac97_reg_base + GCR) & ( GCR_COLD_RST), ac97_reg_base + GCR); /* clear everything but nCRST */
  128. writel(readl(ac97_reg_base + GCR) & (~GCR_COLD_RST), ac97_reg_base + GCR); /* then assert nCRST */
  129. gsr_bits = 0;
  130. /* PXA27x Developers Manual section 13.5.2.2.1 */
  131. clk_prepare_enable(ac97conf_clk);
  132. udelay(5);
  133. clk_disable_unprepare(ac97conf_clk);
  134. writel(GCR_COLD_RST | GCR_WARM_RST, ac97_reg_base + GCR);
  135. }
  136. #endif
  137. #ifdef CONFIG_PXA3xx
  138. static inline void pxa_ac97_warm_pxa3xx(void)
  139. {
  140. gsr_bits = 0;
  141. /* Can't use interrupts */
  142. writel(readl(ac97_reg_base + GCR) | (GCR_WARM_RST), ac97_reg_base + GCR);
  143. }
  144. static inline void pxa_ac97_cold_pxa3xx(void)
  145. {
  146. /* Hold CLKBPB for 100us */
  147. writel(0, ac97_reg_base + GCR);
  148. writel(GCR_CLKBPB, ac97_reg_base + GCR);
  149. udelay(100);
  150. writel(0, ac97_reg_base + GCR);
  151. writel(readl(ac97_reg_base + GCR) & ( GCR_COLD_RST), ac97_reg_base + GCR); /* clear everything but nCRST */
  152. writel(readl(ac97_reg_base + GCR) & (~GCR_COLD_RST), ac97_reg_base + GCR); /* then assert nCRST */
  153. gsr_bits = 0;
  154. /* Can't use interrupts on PXA3xx */
  155. writel(readl(ac97_reg_base + GCR) & (~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN)), ac97_reg_base + GCR);
  156. writel(GCR_WARM_RST | GCR_COLD_RST, ac97_reg_base + GCR);
  157. }
  158. #endif
  159. bool pxa2xx_ac97_try_warm_reset(void)
  160. {
  161. unsigned long gsr;
  162. unsigned int timeout = 100;
  163. #ifdef CONFIG_PXA25x
  164. if (cpu_is_pxa25x())
  165. pxa_ac97_warm_pxa25x();
  166. else
  167. #endif
  168. #ifdef CONFIG_PXA27x
  169. if (cpu_is_pxa27x())
  170. pxa_ac97_warm_pxa27x();
  171. else
  172. #endif
  173. #ifdef CONFIG_PXA3xx
  174. if (cpu_is_pxa3xx())
  175. pxa_ac97_warm_pxa3xx();
  176. else
  177. #endif
  178. snd_BUG();
  179. while (!((readl(ac97_reg_base + GSR) | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
  180. mdelay(1);
  181. gsr = readl(ac97_reg_base + GSR) | gsr_bits;
  182. if (!(gsr & (GSR_PCR | GSR_SCR))) {
  183. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  184. __func__, gsr);
  185. return false;
  186. }
  187. return true;
  188. }
  189. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_warm_reset);
  190. bool pxa2xx_ac97_try_cold_reset(void)
  191. {
  192. unsigned long gsr;
  193. unsigned int timeout = 1000;
  194. #ifdef CONFIG_PXA25x
  195. if (cpu_is_pxa25x())
  196. pxa_ac97_cold_pxa25x();
  197. else
  198. #endif
  199. #ifdef CONFIG_PXA27x
  200. if (cpu_is_pxa27x())
  201. pxa_ac97_cold_pxa27x();
  202. else
  203. #endif
  204. #ifdef CONFIG_PXA3xx
  205. if (cpu_is_pxa3xx())
  206. pxa_ac97_cold_pxa3xx();
  207. else
  208. #endif
  209. snd_BUG();
  210. while (!((readl(ac97_reg_base + GSR) | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
  211. mdelay(1);
  212. gsr = readl(ac97_reg_base + GSR) | gsr_bits;
  213. if (!(gsr & (GSR_PCR | GSR_SCR))) {
  214. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  215. __func__, gsr);
  216. return false;
  217. }
  218. return true;
  219. }
  220. EXPORT_SYMBOL_GPL(pxa2xx_ac97_try_cold_reset);
  221. void pxa2xx_ac97_finish_reset(void)
  222. {
  223. u32 gcr = readl(ac97_reg_base + GCR);
  224. gcr &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  225. gcr |= GCR_SDONE_IE|GCR_CDONE_IE;
  226. writel(gcr, ac97_reg_base + GCR);
  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 = readl(ac97_reg_base + GSR);
  233. if (status) {
  234. writel(status, ac97_reg_base + GSR);
  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. writel(MISR_EOC, ac97_reg_base + MISR);
  242. writel(PISR_EOC, ac97_reg_base + PISR);
  243. writel(MCSR_EOC, ac97_reg_base + MCSR);
  244. }
  245. return IRQ_HANDLED;
  246. }
  247. return IRQ_NONE;
  248. }
  249. #ifdef CONFIG_PM
  250. int pxa2xx_ac97_hw_suspend(void)
  251. {
  252. writel(readl(ac97_reg_base + GCR) | (GCR_ACLINK_OFF), ac97_reg_base + GCR);
  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. int irq;
  268. ac97_reg_base = devm_platform_ioremap_resource(dev, 0);
  269. if (IS_ERR(ac97_reg_base)) {
  270. dev_err(&dev->dev, "Missing MMIO resource\n");
  271. return PTR_ERR(ac97_reg_base);
  272. }
  273. if (dev->dev.of_node) {
  274. /* Assert reset using GPIOD_OUT_HIGH, because reset is GPIO_ACTIVE_LOW */
  275. rst_gpio = devm_gpiod_get(&dev->dev, "reset", GPIOD_OUT_HIGH);
  276. ret = PTR_ERR(rst_gpio);
  277. if (ret == -ENOENT)
  278. reset_gpio = -1;
  279. else if (ret)
  280. return ret;
  281. reset_gpio = desc_to_gpio(rst_gpio);
  282. } else {
  283. if (cpu_is_pxa27x())
  284. reset_gpio = 113;
  285. }
  286. if (cpu_is_pxa27x()) {
  287. /*
  288. * This gpio is needed for a work-around to a bug in the ac97
  289. * controller during warm reset. The direction and level is set
  290. * here so that it is an output driven high when switching from
  291. * AC97_nRESET alt function to generic gpio.
  292. */
  293. gpiod_set_consumer_name(rst_gpio, "pxa27x ac97 reset");
  294. pxa27x_configure_ac97reset(reset_gpio, false);
  295. ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
  296. if (IS_ERR(ac97conf_clk)) {
  297. ret = PTR_ERR(ac97conf_clk);
  298. ac97conf_clk = NULL;
  299. goto err_conf;
  300. }
  301. }
  302. ac97_clk = clk_get(&dev->dev, "AC97CLK");
  303. if (IS_ERR(ac97_clk)) {
  304. ret = PTR_ERR(ac97_clk);
  305. ac97_clk = NULL;
  306. goto err_clk;
  307. }
  308. ret = clk_prepare_enable(ac97_clk);
  309. if (ret)
  310. goto err_clk2;
  311. irq = platform_get_irq(dev, 0);
  312. if (irq < 0) {
  313. ret = irq;
  314. goto err_irq;
  315. }
  316. ret = request_irq(irq, pxa2xx_ac97_irq, 0, "AC97", NULL);
  317. if (ret < 0)
  318. goto err_irq;
  319. return 0;
  320. err_irq:
  321. writel(readl(ac97_reg_base + GCR) | (GCR_ACLINK_OFF), ac97_reg_base + GCR);
  322. err_clk2:
  323. clk_put(ac97_clk);
  324. ac97_clk = NULL;
  325. err_clk:
  326. if (ac97conf_clk) {
  327. clk_put(ac97conf_clk);
  328. ac97conf_clk = NULL;
  329. }
  330. err_conf:
  331. return ret;
  332. }
  333. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_probe);
  334. void pxa2xx_ac97_hw_remove(struct platform_device *dev)
  335. {
  336. writel(readl(ac97_reg_base + GCR) | (GCR_ACLINK_OFF), ac97_reg_base + GCR);
  337. free_irq(platform_get_irq(dev, 0), NULL);
  338. if (ac97conf_clk) {
  339. clk_put(ac97conf_clk);
  340. ac97conf_clk = NULL;
  341. }
  342. clk_disable_unprepare(ac97_clk);
  343. clk_put(ac97_clk);
  344. ac97_clk = NULL;
  345. }
  346. EXPORT_SYMBOL_GPL(pxa2xx_ac97_hw_remove);
  347. u32 pxa2xx_ac97_read_modr(void)
  348. {
  349. if (!ac97_reg_base)
  350. return 0;
  351. return readl(ac97_reg_base + MODR);
  352. }
  353. EXPORT_SYMBOL_GPL(pxa2xx_ac97_read_modr);
  354. u32 pxa2xx_ac97_read_misr(void)
  355. {
  356. if (!ac97_reg_base)
  357. return 0;
  358. return readl(ac97_reg_base + MISR);
  359. }
  360. EXPORT_SYMBOL_GPL(pxa2xx_ac97_read_misr);
  361. MODULE_AUTHOR("Nicolas Pitre");
  362. MODULE_DESCRIPTION("Intel/Marvell PXA sound library");
  363. MODULE_LICENSE("GPL");