pxa2xx-ac97-lib.c 11 KB

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