psc-ac97.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Au12x0/Au1550 PSC ALSA ASoC audio support.
  3. *
  4. * (c) 2007-2008 MSC Vertriebsges.m.b.H.,
  5. * Manuel Lauss <mano@roarinelk.homelinux.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Au1xxx-PSC AC97 glue.
  12. *
  13. * NOTE: all of these drivers can only work with a SINGLE instance
  14. * of a PSC. Multiple independent audio devices are impossible
  15. * with ASoC v1.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/delay.h>
  21. #include <linux/suspend.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/initval.h>
  25. #include <sound/soc.h>
  26. #include <asm/mach-au1x00/au1000.h>
  27. #include <asm/mach-au1x00/au1xxx_psc.h>
  28. #include "psc.h"
  29. #define AC97_DIR \
  30. (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
  31. #define AC97_RATES \
  32. SNDRV_PCM_RATE_8000_48000
  33. #define AC97_FMTS \
  34. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3BE)
  35. #define AC97PCR_START(stype) \
  36. ((stype) == PCM_TX ? PSC_AC97PCR_TS : PSC_AC97PCR_RS)
  37. #define AC97PCR_STOP(stype) \
  38. ((stype) == PCM_TX ? PSC_AC97PCR_TP : PSC_AC97PCR_RP)
  39. #define AC97PCR_CLRFIFO(stype) \
  40. ((stype) == PCM_TX ? PSC_AC97PCR_TC : PSC_AC97PCR_RC)
  41. /* instance data. There can be only one, MacLeod!!!! */
  42. static struct au1xpsc_audio_data *au1xpsc_ac97_workdata;
  43. /* AC97 controller reads codec register */
  44. static unsigned short au1xpsc_ac97_read(struct snd_ac97 *ac97,
  45. unsigned short reg)
  46. {
  47. /* FIXME */
  48. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  49. unsigned short data, tmo;
  50. au_writel(PSC_AC97CDC_RD | PSC_AC97CDC_INDX(reg), AC97_CDC(pscdata));
  51. au_sync();
  52. tmo = 1000;
  53. while ((!(au_readl(AC97_EVNT(pscdata)) & PSC_AC97EVNT_CD)) && --tmo)
  54. udelay(2);
  55. if (!tmo)
  56. data = 0xffff;
  57. else
  58. data = au_readl(AC97_CDC(pscdata)) & 0xffff;
  59. au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  60. au_sync();
  61. return data;
  62. }
  63. /* AC97 controller writes to codec register */
  64. static void au1xpsc_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  65. unsigned short val)
  66. {
  67. /* FIXME */
  68. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  69. unsigned int tmo;
  70. au_writel(PSC_AC97CDC_INDX(reg) | (val & 0xffff), AC97_CDC(pscdata));
  71. au_sync();
  72. tmo = 1000;
  73. while ((!(au_readl(AC97_EVNT(pscdata)) & PSC_AC97EVNT_CD)) && --tmo)
  74. au_sync();
  75. au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  76. au_sync();
  77. }
  78. /* AC97 controller asserts a warm reset */
  79. static void au1xpsc_ac97_warm_reset(struct snd_ac97 *ac97)
  80. {
  81. /* FIXME */
  82. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  83. au_writel(PSC_AC97RST_SNC, AC97_RST(pscdata));
  84. au_sync();
  85. msleep(10);
  86. au_writel(0, AC97_RST(pscdata));
  87. au_sync();
  88. }
  89. static void au1xpsc_ac97_cold_reset(struct snd_ac97 *ac97)
  90. {
  91. /* FIXME */
  92. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  93. int i;
  94. /* disable PSC during cold reset */
  95. au_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
  96. au_sync();
  97. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(pscdata));
  98. au_sync();
  99. /* issue cold reset */
  100. au_writel(PSC_AC97RST_RST, AC97_RST(pscdata));
  101. au_sync();
  102. msleep(500);
  103. au_writel(0, AC97_RST(pscdata));
  104. au_sync();
  105. /* enable PSC */
  106. au_writel(PSC_CTRL_ENABLE, PSC_CTRL(pscdata));
  107. au_sync();
  108. /* wait for PSC to indicate it's ready */
  109. i = 100000;
  110. while (!((au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_SR)) && (--i))
  111. au_sync();
  112. if (i == 0) {
  113. printk(KERN_ERR "au1xpsc-ac97: PSC not ready!\n");
  114. return;
  115. }
  116. /* enable the ac97 function */
  117. au_writel(pscdata->cfg | PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  118. au_sync();
  119. /* wait for AC97 core to become ready */
  120. i = 100000;
  121. while (!((au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_DR)) && (--i))
  122. au_sync();
  123. if (i == 0)
  124. printk(KERN_ERR "au1xpsc-ac97: AC97 ctrl not ready\n");
  125. }
  126. /* AC97 controller operations */
  127. struct snd_ac97_bus_ops soc_ac97_ops = {
  128. .read = au1xpsc_ac97_read,
  129. .write = au1xpsc_ac97_write,
  130. .reset = au1xpsc_ac97_cold_reset,
  131. .warm_reset = au1xpsc_ac97_warm_reset,
  132. };
  133. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  134. static int au1xpsc_ac97_hw_params(struct snd_pcm_substream *substream,
  135. struct snd_pcm_hw_params *params,
  136. struct snd_soc_dai *dai)
  137. {
  138. /* FIXME */
  139. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  140. unsigned long r, stat;
  141. int chans, stype = SUBSTREAM_TYPE(substream);
  142. chans = params_channels(params);
  143. r = au_readl(AC97_CFG(pscdata));
  144. stat = au_readl(AC97_STAT(pscdata));
  145. /* already active? */
  146. if (stat & (PSC_AC97STAT_TB | PSC_AC97STAT_RB)) {
  147. /* reject parameters not currently set up */
  148. if ((PSC_AC97CFG_GET_LEN(r) != params->msbits) ||
  149. (pscdata->rate != params_rate(params)))
  150. return -EINVAL;
  151. } else {
  152. /* disable AC97 device controller first */
  153. au_writel(r & ~PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  154. au_sync();
  155. /* set sample bitdepth: REG[24:21]=(BITS-2)/2 */
  156. r &= ~PSC_AC97CFG_LEN_MASK;
  157. r |= PSC_AC97CFG_SET_LEN(params->msbits);
  158. /* channels: enable slots for front L/R channel */
  159. if (stype == PCM_TX) {
  160. r &= ~PSC_AC97CFG_TXSLOT_MASK;
  161. r |= PSC_AC97CFG_TXSLOT_ENA(3);
  162. r |= PSC_AC97CFG_TXSLOT_ENA(4);
  163. } else {
  164. r &= ~PSC_AC97CFG_RXSLOT_MASK;
  165. r |= PSC_AC97CFG_RXSLOT_ENA(3);
  166. r |= PSC_AC97CFG_RXSLOT_ENA(4);
  167. }
  168. /* finally enable the AC97 controller again */
  169. au_writel(r | PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  170. au_sync();
  171. pscdata->cfg = r;
  172. pscdata->rate = params_rate(params);
  173. }
  174. return 0;
  175. }
  176. static int au1xpsc_ac97_trigger(struct snd_pcm_substream *substream,
  177. int cmd, struct snd_soc_dai *dai)
  178. {
  179. /* FIXME */
  180. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  181. int ret, stype = SUBSTREAM_TYPE(substream);
  182. ret = 0;
  183. switch (cmd) {
  184. case SNDRV_PCM_TRIGGER_START:
  185. case SNDRV_PCM_TRIGGER_RESUME:
  186. au_writel(AC97PCR_START(stype), AC97_PCR(pscdata));
  187. au_sync();
  188. break;
  189. case SNDRV_PCM_TRIGGER_STOP:
  190. case SNDRV_PCM_TRIGGER_SUSPEND:
  191. au_writel(AC97PCR_STOP(stype), AC97_PCR(pscdata));
  192. au_sync();
  193. break;
  194. default:
  195. ret = -EINVAL;
  196. }
  197. return ret;
  198. }
  199. static int au1xpsc_ac97_probe(struct platform_device *pdev,
  200. struct snd_soc_dai *dai)
  201. {
  202. int ret;
  203. struct resource *r;
  204. unsigned long sel;
  205. if (au1xpsc_ac97_workdata)
  206. return -EBUSY;
  207. au1xpsc_ac97_workdata =
  208. kzalloc(sizeof(struct au1xpsc_audio_data), GFP_KERNEL);
  209. if (!au1xpsc_ac97_workdata)
  210. return -ENOMEM;
  211. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  212. if (!r) {
  213. ret = -ENODEV;
  214. goto out0;
  215. }
  216. ret = -EBUSY;
  217. au1xpsc_ac97_workdata->ioarea =
  218. request_mem_region(r->start, r->end - r->start + 1,
  219. "au1xpsc_ac97");
  220. if (!au1xpsc_ac97_workdata->ioarea)
  221. goto out0;
  222. au1xpsc_ac97_workdata->mmio = ioremap(r->start, 0xffff);
  223. if (!au1xpsc_ac97_workdata->mmio)
  224. goto out1;
  225. /* configuration: max dma trigger threshold, enable ac97 */
  226. au1xpsc_ac97_workdata->cfg = PSC_AC97CFG_RT_FIFO8 |
  227. PSC_AC97CFG_TT_FIFO8 |
  228. PSC_AC97CFG_DE_ENABLE;
  229. /* preserve PSC clock source set up by platform (dev.platform_data
  230. * is already occupied by soc layer)
  231. */
  232. sel = au_readl(PSC_SEL(au1xpsc_ac97_workdata)) & PSC_SEL_CLK_MASK;
  233. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_ac97_workdata));
  234. au_sync();
  235. au_writel(0, PSC_SEL(au1xpsc_ac97_workdata));
  236. au_sync();
  237. au_writel(PSC_SEL_PS_AC97MODE | sel, PSC_SEL(au1xpsc_ac97_workdata));
  238. au_sync();
  239. /* next up: cold reset. Dont check for PSC-ready now since
  240. * there may not be any codec clock yet.
  241. */
  242. return 0;
  243. out1:
  244. release_resource(au1xpsc_ac97_workdata->ioarea);
  245. kfree(au1xpsc_ac97_workdata->ioarea);
  246. out0:
  247. kfree(au1xpsc_ac97_workdata);
  248. au1xpsc_ac97_workdata = NULL;
  249. return ret;
  250. }
  251. static void au1xpsc_ac97_remove(struct platform_device *pdev,
  252. struct snd_soc_dai *dai)
  253. {
  254. /* disable PSC completely */
  255. au_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
  256. au_sync();
  257. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_ac97_workdata));
  258. au_sync();
  259. iounmap(au1xpsc_ac97_workdata->mmio);
  260. release_resource(au1xpsc_ac97_workdata->ioarea);
  261. kfree(au1xpsc_ac97_workdata->ioarea);
  262. kfree(au1xpsc_ac97_workdata);
  263. au1xpsc_ac97_workdata = NULL;
  264. }
  265. static int au1xpsc_ac97_suspend(struct snd_soc_dai *dai)
  266. {
  267. /* save interesting registers and disable PSC */
  268. au1xpsc_ac97_workdata->pm[0] =
  269. au_readl(PSC_SEL(au1xpsc_ac97_workdata));
  270. au_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
  271. au_sync();
  272. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_ac97_workdata));
  273. au_sync();
  274. return 0;
  275. }
  276. static int au1xpsc_ac97_resume(struct snd_soc_dai *dai)
  277. {
  278. /* restore PSC clock config */
  279. au_writel(au1xpsc_ac97_workdata->pm[0] | PSC_SEL_PS_AC97MODE,
  280. PSC_SEL(au1xpsc_ac97_workdata));
  281. au_sync();
  282. /* after this point the ac97 core will cold-reset the codec.
  283. * During cold-reset the PSC is reinitialized and the last
  284. * configuration set up in hw_params() is restored.
  285. */
  286. return 0;
  287. }
  288. static struct snd_soc_dai_ops au1xpsc_ac97_dai_ops = {
  289. .trigger = au1xpsc_ac97_trigger,
  290. .hw_params = au1xpsc_ac97_hw_params,
  291. };
  292. struct snd_soc_dai au1xpsc_ac97_dai = {
  293. .name = "au1xpsc_ac97",
  294. .ac97_control = 1,
  295. .probe = au1xpsc_ac97_probe,
  296. .remove = au1xpsc_ac97_remove,
  297. .suspend = au1xpsc_ac97_suspend,
  298. .resume = au1xpsc_ac97_resume,
  299. .playback = {
  300. .rates = AC97_RATES,
  301. .formats = AC97_FMTS,
  302. .channels_min = 2,
  303. .channels_max = 2,
  304. },
  305. .capture = {
  306. .rates = AC97_RATES,
  307. .formats = AC97_FMTS,
  308. .channels_min = 2,
  309. .channels_max = 2,
  310. },
  311. .ops = &au1xpsc_ac97_dai_ops,
  312. };
  313. EXPORT_SYMBOL_GPL(au1xpsc_ac97_dai);
  314. static int __init au1xpsc_ac97_init(void)
  315. {
  316. au1xpsc_ac97_workdata = NULL;
  317. return snd_soc_register_dai(&au1xpsc_ac97_dai);
  318. }
  319. static void __exit au1xpsc_ac97_exit(void)
  320. {
  321. snd_soc_unregister_dai(&au1xpsc_ac97_dai);
  322. }
  323. module_init(au1xpsc_ac97_init);
  324. module_exit(au1xpsc_ac97_exit);
  325. MODULE_LICENSE("GPL");
  326. MODULE_DESCRIPTION("Au12x0/Au1550 PSC AC97 ALSA ASoC audio driver");
  327. MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");