txx9aclc-ac97.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * TXx9 ACLC AC97 driver
  3. *
  4. * Copyright (C) 2009 Atsushi Nemoto
  5. *
  6. * Based on RBTX49xx patch from CELF patch archive.
  7. * (C) Copyright TOSHIBA CORPORATION 2004-2006
  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/init.h>
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/gfp.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/soc.h>
  22. #include "txx9aclc.h"
  23. #define AC97_DIR \
  24. (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
  25. #define AC97_RATES \
  26. SNDRV_PCM_RATE_8000_48000
  27. #ifdef __BIG_ENDIAN
  28. #define AC97_FMTS SNDRV_PCM_FMTBIT_S16_BE
  29. #else
  30. #define AC97_FMTS SNDRV_PCM_FMTBIT_S16_LE
  31. #endif
  32. static DECLARE_WAIT_QUEUE_HEAD(ac97_waitq);
  33. /* REVISIT: How to find txx9aclc_drvdata from snd_ac97? */
  34. static struct txx9aclc_plat_drvdata *txx9aclc_drvdata;
  35. static int txx9aclc_regready(struct txx9aclc_plat_drvdata *drvdata)
  36. {
  37. return __raw_readl(drvdata->base + ACINTSTS) & ACINT_REGACCRDY;
  38. }
  39. /* AC97 controller reads codec register */
  40. static unsigned short txx9aclc_ac97_read(struct snd_ac97 *ac97,
  41. unsigned short reg)
  42. {
  43. struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
  44. void __iomem *base = drvdata->base;
  45. u32 dat;
  46. if (!(__raw_readl(base + ACINTSTS) & ACINT_CODECRDY(ac97->num)))
  47. return 0xffff;
  48. reg |= ac97->num << 7;
  49. dat = (reg << ACREGACC_REG_SHIFT) | ACREGACC_READ;
  50. __raw_writel(dat, base + ACREGACC);
  51. __raw_writel(ACINT_REGACCRDY, base + ACINTEN);
  52. if (!wait_event_timeout(ac97_waitq, txx9aclc_regready(txx9aclc_drvdata), HZ)) {
  53. __raw_writel(ACINT_REGACCRDY, base + ACINTDIS);
  54. printk(KERN_ERR "ac97 read timeout (reg %#x)\n", reg);
  55. dat = 0xffff;
  56. goto done;
  57. }
  58. dat = __raw_readl(base + ACREGACC);
  59. if (((dat >> ACREGACC_REG_SHIFT) & 0xff) != reg) {
  60. printk(KERN_ERR "reg mismatch %x with %x\n",
  61. dat, reg);
  62. dat = 0xffff;
  63. goto done;
  64. }
  65. dat = (dat >> ACREGACC_DAT_SHIFT) & 0xffff;
  66. done:
  67. __raw_writel(ACINT_REGACCRDY, base + ACINTDIS);
  68. return dat;
  69. }
  70. /* AC97 controller writes to codec register */
  71. static void txx9aclc_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  72. unsigned short val)
  73. {
  74. struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
  75. void __iomem *base = drvdata->base;
  76. __raw_writel(((reg | (ac97->num << 7)) << ACREGACC_REG_SHIFT) |
  77. (val << ACREGACC_DAT_SHIFT),
  78. base + ACREGACC);
  79. __raw_writel(ACINT_REGACCRDY, base + ACINTEN);
  80. if (!wait_event_timeout(ac97_waitq, txx9aclc_regready(txx9aclc_drvdata), HZ)) {
  81. printk(KERN_ERR
  82. "ac97 write timeout (reg %#x)\n", reg);
  83. }
  84. __raw_writel(ACINT_REGACCRDY, base + ACINTDIS);
  85. }
  86. static void txx9aclc_ac97_cold_reset(struct snd_ac97 *ac97)
  87. {
  88. struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
  89. void __iomem *base = drvdata->base;
  90. u32 ready = ACINT_CODECRDY(ac97->num) | ACINT_REGACCRDY;
  91. __raw_writel(ACCTL_ENLINK, base + ACCTLDIS);
  92. mmiowb();
  93. udelay(1);
  94. __raw_writel(ACCTL_ENLINK, base + ACCTLEN);
  95. /* wait for primary codec ready status */
  96. __raw_writel(ready, base + ACINTEN);
  97. if (!wait_event_timeout(ac97_waitq,
  98. (__raw_readl(base + ACINTSTS) & ready) == ready,
  99. HZ)) {
  100. dev_err(&ac97->dev, "primary codec is not ready "
  101. "(status %#x)\n",
  102. __raw_readl(base + ACINTSTS));
  103. }
  104. __raw_writel(ACINT_REGACCRDY, base + ACINTSTS);
  105. __raw_writel(ready, base + ACINTDIS);
  106. }
  107. /* AC97 controller operations */
  108. struct snd_ac97_bus_ops soc_ac97_ops = {
  109. .read = txx9aclc_ac97_read,
  110. .write = txx9aclc_ac97_write,
  111. .reset = txx9aclc_ac97_cold_reset,
  112. };
  113. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  114. static irqreturn_t txx9aclc_ac97_irq(int irq, void *dev_id)
  115. {
  116. struct txx9aclc_plat_drvdata *drvdata = dev_id;
  117. void __iomem *base = drvdata->base;
  118. __raw_writel(__raw_readl(base + ACINTMSTS), base + ACINTDIS);
  119. wake_up(&ac97_waitq);
  120. return IRQ_HANDLED;
  121. }
  122. static int txx9aclc_ac97_probe(struct snd_soc_dai *dai)
  123. {
  124. txx9aclc_drvdata = snd_soc_dai_get_drvdata(dai);
  125. return 0;
  126. }
  127. static int txx9aclc_ac97_remove(struct snd_soc_dai *dai)
  128. {
  129. struct txx9aclc_plat_drvdata *drvdata = snd_soc_dai_get_drvdata(dai);
  130. /* disable AC-link */
  131. __raw_writel(ACCTL_ENLINK, drvdata->base + ACCTLDIS);
  132. txx9aclc_drvdata = NULL;
  133. return 0;
  134. }
  135. static struct snd_soc_dai_driver txx9aclc_ac97_dai = {
  136. .ac97_control = 1,
  137. .probe = txx9aclc_ac97_probe,
  138. .remove = txx9aclc_ac97_remove,
  139. .playback = {
  140. .rates = AC97_RATES,
  141. .formats = AC97_FMTS,
  142. .channels_min = 2,
  143. .channels_max = 2,
  144. },
  145. .capture = {
  146. .rates = AC97_RATES,
  147. .formats = AC97_FMTS,
  148. .channels_min = 2,
  149. .channels_max = 2,
  150. },
  151. };
  152. static int __devinit txx9aclc_ac97_dev_probe(struct platform_device *pdev)
  153. {
  154. struct txx9aclc_plat_drvdata *drvdata;
  155. struct resource *r;
  156. int err;
  157. int irq;
  158. irq = platform_get_irq(pdev, 0);
  159. if (irq < 0)
  160. return irq;
  161. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  162. if (!r)
  163. return -EBUSY;
  164. if (!devm_request_mem_region(&pdev->dev, r->start, resource_size(r),
  165. dev_name(&pdev->dev)))
  166. return -EBUSY;
  167. drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
  168. if (!drvdata)
  169. return -ENOMEM;
  170. platform_set_drvdata(pdev, drvdata);
  171. drvdata->physbase = r->start;
  172. if (sizeof(drvdata->physbase) > sizeof(r->start) &&
  173. r->start >= TXX9_DIRECTMAP_BASE &&
  174. r->start < TXX9_DIRECTMAP_BASE + 0x400000)
  175. drvdata->physbase |= 0xf00000000ull;
  176. drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  177. if (!drvdata->base)
  178. return -EBUSY;
  179. err = devm_request_irq(&pdev->dev, irq, txx9aclc_ac97_irq,
  180. IRQF_DISABLED, dev_name(&pdev->dev), drvdata);
  181. if (err < 0)
  182. return err;
  183. return snd_soc_register_dai(&pdev->dev, &txx9aclc_ac97_dai);
  184. }
  185. static int __devexit txx9aclc_ac97_dev_remove(struct platform_device *pdev)
  186. {
  187. snd_soc_unregister_dai(&pdev->dev);
  188. return 0;
  189. }
  190. static struct platform_driver txx9aclc_ac97_driver = {
  191. .probe = txx9aclc_ac97_dev_probe,
  192. .remove = __devexit_p(txx9aclc_ac97_dev_remove),
  193. .driver = {
  194. .name = "txx9aclc-ac97",
  195. .owner = THIS_MODULE,
  196. },
  197. };
  198. static int __init txx9aclc_ac97_init(void)
  199. {
  200. return platform_driver_register(&txx9aclc_ac97_driver);
  201. }
  202. static void __exit txx9aclc_ac97_exit(void)
  203. {
  204. platform_driver_unregister(&txx9aclc_ac97_driver);
  205. }
  206. module_init(txx9aclc_ac97_init);
  207. module_exit(txx9aclc_ac97_exit);
  208. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  209. MODULE_DESCRIPTION("TXx9 ACLC AC97 driver");
  210. MODULE_LICENSE("GPL");
  211. MODULE_ALIAS("platform:txx9aclc-ac97");