psc-ac97.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. {
  137. /* FIXME */
  138. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  139. unsigned long r, stat;
  140. int chans, stype = SUBSTREAM_TYPE(substream);
  141. chans = params_channels(params);
  142. r = au_readl(AC97_CFG(pscdata));
  143. stat = au_readl(AC97_STAT(pscdata));
  144. /* already active? */
  145. if (stat & (PSC_AC97STAT_TB | PSC_AC97STAT_RB)) {
  146. /* reject parameters not currently set up */
  147. if ((PSC_AC97CFG_GET_LEN(r) != params->msbits) ||
  148. (pscdata->rate != params_rate(params)))
  149. return -EINVAL;
  150. } else {
  151. /* disable AC97 device controller first */
  152. au_writel(r & ~PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  153. au_sync();
  154. /* set sample bitdepth: REG[24:21]=(BITS-2)/2 */
  155. r &= ~PSC_AC97CFG_LEN_MASK;
  156. r |= PSC_AC97CFG_SET_LEN(params->msbits);
  157. /* channels: enable slots for front L/R channel */
  158. if (stype == PCM_TX) {
  159. r &= ~PSC_AC97CFG_TXSLOT_MASK;
  160. r |= PSC_AC97CFG_TXSLOT_ENA(3);
  161. r |= PSC_AC97CFG_TXSLOT_ENA(4);
  162. } else {
  163. r &= ~PSC_AC97CFG_RXSLOT_MASK;
  164. r |= PSC_AC97CFG_RXSLOT_ENA(3);
  165. r |= PSC_AC97CFG_RXSLOT_ENA(4);
  166. }
  167. /* finally enable the AC97 controller again */
  168. au_writel(r | PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  169. au_sync();
  170. pscdata->cfg = r;
  171. pscdata->rate = params_rate(params);
  172. }
  173. return 0;
  174. }
  175. static int au1xpsc_ac97_trigger(struct snd_pcm_substream *substream,
  176. int cmd)
  177. {
  178. /* FIXME */
  179. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  180. int ret, stype = SUBSTREAM_TYPE(substream);
  181. ret = 0;
  182. switch (cmd) {
  183. case SNDRV_PCM_TRIGGER_START:
  184. case SNDRV_PCM_TRIGGER_RESUME:
  185. au_writel(AC97PCR_START(stype), AC97_PCR(pscdata));
  186. au_sync();
  187. break;
  188. case SNDRV_PCM_TRIGGER_STOP:
  189. case SNDRV_PCM_TRIGGER_SUSPEND:
  190. au_writel(AC97PCR_STOP(stype), AC97_PCR(pscdata));
  191. au_sync();
  192. break;
  193. default:
  194. ret = -EINVAL;
  195. }
  196. return ret;
  197. }
  198. static int au1xpsc_ac97_probe(struct platform_device *pdev,
  199. struct snd_soc_dai *dai)
  200. {
  201. int ret;
  202. struct resource *r;
  203. unsigned long sel;
  204. if (au1xpsc_ac97_workdata)
  205. return -EBUSY;
  206. au1xpsc_ac97_workdata =
  207. kzalloc(sizeof(struct au1xpsc_audio_data), GFP_KERNEL);
  208. if (!au1xpsc_ac97_workdata)
  209. return -ENOMEM;
  210. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  211. if (!r) {
  212. ret = -ENODEV;
  213. goto out0;
  214. }
  215. ret = -EBUSY;
  216. au1xpsc_ac97_workdata->ioarea =
  217. request_mem_region(r->start, r->end - r->start + 1,
  218. "au1xpsc_ac97");
  219. if (!au1xpsc_ac97_workdata->ioarea)
  220. goto out0;
  221. au1xpsc_ac97_workdata->mmio = ioremap(r->start, 0xffff);
  222. if (!au1xpsc_ac97_workdata->mmio)
  223. goto out1;
  224. /* configuration: max dma trigger threshold, enable ac97 */
  225. au1xpsc_ac97_workdata->cfg = PSC_AC97CFG_RT_FIFO8 |
  226. PSC_AC97CFG_TT_FIFO8 |
  227. PSC_AC97CFG_DE_ENABLE;
  228. /* preserve PSC clock source set up by platform (dev.platform_data
  229. * is already occupied by soc layer)
  230. */
  231. sel = au_readl(PSC_SEL(au1xpsc_ac97_workdata)) & PSC_SEL_CLK_MASK;
  232. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_ac97_workdata));
  233. au_sync();
  234. au_writel(0, PSC_SEL(au1xpsc_ac97_workdata));
  235. au_sync();
  236. au_writel(PSC_SEL_PS_AC97MODE | sel, PSC_SEL(au1xpsc_ac97_workdata));
  237. au_sync();
  238. /* next up: cold reset. Dont check for PSC-ready now since
  239. * there may not be any codec clock yet.
  240. */
  241. return 0;
  242. out1:
  243. release_resource(au1xpsc_ac97_workdata->ioarea);
  244. kfree(au1xpsc_ac97_workdata->ioarea);
  245. out0:
  246. kfree(au1xpsc_ac97_workdata);
  247. au1xpsc_ac97_workdata = NULL;
  248. return ret;
  249. }
  250. static void au1xpsc_ac97_remove(struct platform_device *pdev,
  251. struct snd_soc_dai *dai)
  252. {
  253. /* disable PSC completely */
  254. au_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
  255. au_sync();
  256. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_ac97_workdata));
  257. au_sync();
  258. iounmap(au1xpsc_ac97_workdata->mmio);
  259. release_resource(au1xpsc_ac97_workdata->ioarea);
  260. kfree(au1xpsc_ac97_workdata->ioarea);
  261. kfree(au1xpsc_ac97_workdata);
  262. au1xpsc_ac97_workdata = NULL;
  263. }
  264. static int au1xpsc_ac97_suspend(struct platform_device *pdev,
  265. 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 platform_device *pdev,
  277. struct snd_soc_dai *dai)
  278. {
  279. /* restore PSC clock config */
  280. au_writel(au1xpsc_ac97_workdata->pm[0] | PSC_SEL_PS_AC97MODE,
  281. PSC_SEL(au1xpsc_ac97_workdata));
  282. au_sync();
  283. /* after this point the ac97 core will cold-reset the codec.
  284. * During cold-reset the PSC is reinitialized and the last
  285. * configuration set up in hw_params() is restored.
  286. */
  287. return 0;
  288. }
  289. struct snd_soc_dai au1xpsc_ac97_dai = {
  290. .name = "au1xpsc_ac97",
  291. .type = SND_SOC_DAI_AC97,
  292. .probe = au1xpsc_ac97_probe,
  293. .remove = au1xpsc_ac97_remove,
  294. .suspend = au1xpsc_ac97_suspend,
  295. .resume = au1xpsc_ac97_resume,
  296. .playback = {
  297. .rates = AC97_RATES,
  298. .formats = AC97_FMTS,
  299. .channels_min = 2,
  300. .channels_max = 2,
  301. },
  302. .capture = {
  303. .rates = AC97_RATES,
  304. .formats = AC97_FMTS,
  305. .channels_min = 2,
  306. .channels_max = 2,
  307. },
  308. .ops = {
  309. .trigger = au1xpsc_ac97_trigger,
  310. .hw_params = au1xpsc_ac97_hw_params,
  311. },
  312. };
  313. EXPORT_SYMBOL_GPL(au1xpsc_ac97_dai);
  314. static int __init au1xpsc_ac97_init(void)
  315. {
  316. au1xpsc_ac97_workdata = NULL;
  317. return 0;
  318. }
  319. static void __exit au1xpsc_ac97_exit(void)
  320. {
  321. }
  322. module_init(au1xpsc_ac97_init);
  323. module_exit(au1xpsc_ac97_exit);
  324. MODULE_LICENSE("GPL");
  325. MODULE_DESCRIPTION("Au12x0/Au1550 PSC AC97 ALSA ASoC audio driver");
  326. MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");