psc-i2s.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Au12x0/Au1550 PSC ALSA ASoC audio support.
  3. *
  4. * (c) 2007-2008 MSC Vertriebsges.m.b.H.,
  5. * Manuel Lauss <manuel.lauss@gmail.com>
  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 I2S glue.
  12. *
  13. * NOTE: so far only PSC slave mode (bit- and frameclock) is supported.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/suspend.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/initval.h>
  22. #include <sound/soc.h>
  23. #include <asm/mach-au1x00/au1000.h>
  24. #include <asm/mach-au1x00/au1xxx_psc.h>
  25. #include "psc.h"
  26. /* supported I2S DAI hardware formats */
  27. #define AU1XPSC_I2S_DAIFMT \
  28. (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J | \
  29. SND_SOC_DAIFMT_NB_NF)
  30. /* supported I2S direction */
  31. #define AU1XPSC_I2S_DIR \
  32. (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
  33. #define AU1XPSC_I2S_RATES \
  34. SNDRV_PCM_RATE_8000_192000
  35. #define AU1XPSC_I2S_FMTS \
  36. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
  37. #define I2SSTAT_BUSY(stype) \
  38. ((stype) == SNDRV_PCM_STREAM_PLAYBACK ? PSC_I2SSTAT_TB : PSC_I2SSTAT_RB)
  39. #define I2SPCR_START(stype) \
  40. ((stype) == SNDRV_PCM_STREAM_PLAYBACK ? PSC_I2SPCR_TS : PSC_I2SPCR_RS)
  41. #define I2SPCR_STOP(stype) \
  42. ((stype) == SNDRV_PCM_STREAM_PLAYBACK ? PSC_I2SPCR_TP : PSC_I2SPCR_RP)
  43. #define I2SPCR_CLRFIFO(stype) \
  44. ((stype) == SNDRV_PCM_STREAM_PLAYBACK ? PSC_I2SPCR_TC : PSC_I2SPCR_RC)
  45. static int au1xpsc_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
  46. unsigned int fmt)
  47. {
  48. struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(cpu_dai);
  49. unsigned long ct;
  50. int ret;
  51. ret = -EINVAL;
  52. ct = pscdata->cfg;
  53. ct &= ~(PSC_I2SCFG_XM | PSC_I2SCFG_MLJ); /* left-justified */
  54. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  55. case SND_SOC_DAIFMT_I2S:
  56. ct |= PSC_I2SCFG_XM; /* enable I2S mode */
  57. break;
  58. case SND_SOC_DAIFMT_MSB:
  59. break;
  60. case SND_SOC_DAIFMT_LSB:
  61. ct |= PSC_I2SCFG_MLJ; /* LSB (right-) justified */
  62. break;
  63. default:
  64. goto out;
  65. }
  66. ct &= ~(PSC_I2SCFG_BI | PSC_I2SCFG_WI); /* IB-IF */
  67. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  68. case SND_SOC_DAIFMT_NB_NF:
  69. ct |= PSC_I2SCFG_BI | PSC_I2SCFG_WI;
  70. break;
  71. case SND_SOC_DAIFMT_NB_IF:
  72. ct |= PSC_I2SCFG_BI;
  73. break;
  74. case SND_SOC_DAIFMT_IB_NF:
  75. ct |= PSC_I2SCFG_WI;
  76. break;
  77. case SND_SOC_DAIFMT_IB_IF:
  78. break;
  79. default:
  80. goto out;
  81. }
  82. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  83. case SND_SOC_DAIFMT_CBM_CFM: /* CODEC master */
  84. ct |= PSC_I2SCFG_MS; /* PSC I2S slave mode */
  85. break;
  86. case SND_SOC_DAIFMT_CBS_CFS: /* CODEC slave */
  87. ct &= ~PSC_I2SCFG_MS; /* PSC I2S Master mode */
  88. break;
  89. default:
  90. goto out;
  91. }
  92. pscdata->cfg = ct;
  93. ret = 0;
  94. out:
  95. return ret;
  96. }
  97. static int au1xpsc_i2s_hw_params(struct snd_pcm_substream *substream,
  98. struct snd_pcm_hw_params *params,
  99. struct snd_soc_dai *dai)
  100. {
  101. struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(dai);
  102. int cfgbits;
  103. unsigned long stat;
  104. /* check if the PSC is already streaming data */
  105. stat = __raw_readl(I2S_STAT(pscdata));
  106. if (stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB)) {
  107. /* reject parameters not currently set up in hardware */
  108. cfgbits = __raw_readl(I2S_CFG(pscdata));
  109. if ((PSC_I2SCFG_GET_LEN(cfgbits) != params->msbits) ||
  110. (params_rate(params) != pscdata->rate))
  111. return -EINVAL;
  112. } else {
  113. /* set sample bitdepth */
  114. pscdata->cfg &= ~(0x1f << 4);
  115. pscdata->cfg |= PSC_I2SCFG_SET_LEN(params->msbits);
  116. /* remember current rate for other stream */
  117. pscdata->rate = params_rate(params);
  118. }
  119. return 0;
  120. }
  121. /* Configure PSC late: on my devel systems the codec is I2S master and
  122. * supplies the i2sbitclock __AND__ i2sMclk (!) to the PSC unit. ASoC
  123. * uses aggressive PM and switches the codec off when it is not in use
  124. * which also means the PSC unit doesn't get any clocks and is therefore
  125. * dead. That's why this chunk here gets called from the trigger callback
  126. * because I can be reasonably certain the codec is driving the clocks.
  127. */
  128. static int au1xpsc_i2s_configure(struct au1xpsc_audio_data *pscdata)
  129. {
  130. unsigned long tmo;
  131. /* bring PSC out of sleep, and configure I2S unit */
  132. __raw_writel(PSC_CTRL_ENABLE, PSC_CTRL(pscdata));
  133. wmb(); /* drain writebuffer */
  134. tmo = 1000000;
  135. while (!(__raw_readl(I2S_STAT(pscdata)) & PSC_I2SSTAT_SR) && tmo)
  136. tmo--;
  137. if (!tmo)
  138. goto psc_err;
  139. __raw_writel(0, I2S_CFG(pscdata));
  140. wmb(); /* drain writebuffer */
  141. __raw_writel(pscdata->cfg | PSC_I2SCFG_DE_ENABLE, I2S_CFG(pscdata));
  142. wmb(); /* drain writebuffer */
  143. /* wait for I2S controller to become ready */
  144. tmo = 1000000;
  145. while (!(__raw_readl(I2S_STAT(pscdata)) & PSC_I2SSTAT_DR) && tmo)
  146. tmo--;
  147. if (tmo)
  148. return 0;
  149. psc_err:
  150. __raw_writel(0, I2S_CFG(pscdata));
  151. __raw_writel(PSC_CTRL_SUSPEND, PSC_CTRL(pscdata));
  152. wmb(); /* drain writebuffer */
  153. return -ETIMEDOUT;
  154. }
  155. static int au1xpsc_i2s_start(struct au1xpsc_audio_data *pscdata, int stype)
  156. {
  157. unsigned long tmo, stat;
  158. int ret;
  159. ret = 0;
  160. /* if both TX and RX are idle, configure the PSC */
  161. stat = __raw_readl(I2S_STAT(pscdata));
  162. if (!(stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB))) {
  163. ret = au1xpsc_i2s_configure(pscdata);
  164. if (ret)
  165. goto out;
  166. }
  167. __raw_writel(I2SPCR_CLRFIFO(stype), I2S_PCR(pscdata));
  168. wmb(); /* drain writebuffer */
  169. __raw_writel(I2SPCR_START(stype), I2S_PCR(pscdata));
  170. wmb(); /* drain writebuffer */
  171. /* wait for start confirmation */
  172. tmo = 1000000;
  173. while (!(__raw_readl(I2S_STAT(pscdata)) & I2SSTAT_BUSY(stype)) && tmo)
  174. tmo--;
  175. if (!tmo) {
  176. __raw_writel(I2SPCR_STOP(stype), I2S_PCR(pscdata));
  177. wmb(); /* drain writebuffer */
  178. ret = -ETIMEDOUT;
  179. }
  180. out:
  181. return ret;
  182. }
  183. static int au1xpsc_i2s_stop(struct au1xpsc_audio_data *pscdata, int stype)
  184. {
  185. unsigned long tmo, stat;
  186. __raw_writel(I2SPCR_STOP(stype), I2S_PCR(pscdata));
  187. wmb(); /* drain writebuffer */
  188. /* wait for stop confirmation */
  189. tmo = 1000000;
  190. while ((__raw_readl(I2S_STAT(pscdata)) & I2SSTAT_BUSY(stype)) && tmo)
  191. tmo--;
  192. /* if both TX and RX are idle, disable PSC */
  193. stat = __raw_readl(I2S_STAT(pscdata));
  194. if (!(stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB))) {
  195. __raw_writel(0, I2S_CFG(pscdata));
  196. wmb(); /* drain writebuffer */
  197. __raw_writel(PSC_CTRL_SUSPEND, PSC_CTRL(pscdata));
  198. wmb(); /* drain writebuffer */
  199. }
  200. return 0;
  201. }
  202. static int au1xpsc_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  203. struct snd_soc_dai *dai)
  204. {
  205. struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(dai);
  206. int ret, stype = substream->stream;
  207. switch (cmd) {
  208. case SNDRV_PCM_TRIGGER_START:
  209. case SNDRV_PCM_TRIGGER_RESUME:
  210. ret = au1xpsc_i2s_start(pscdata, stype);
  211. break;
  212. case SNDRV_PCM_TRIGGER_STOP:
  213. case SNDRV_PCM_TRIGGER_SUSPEND:
  214. ret = au1xpsc_i2s_stop(pscdata, stype);
  215. break;
  216. default:
  217. ret = -EINVAL;
  218. }
  219. return ret;
  220. }
  221. static int au1xpsc_i2s_startup(struct snd_pcm_substream *substream,
  222. struct snd_soc_dai *dai)
  223. {
  224. struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(dai);
  225. snd_soc_dai_set_dma_data(dai, substream, &pscdata->dmaids[0]);
  226. return 0;
  227. }
  228. static const struct snd_soc_dai_ops au1xpsc_i2s_dai_ops = {
  229. .startup = au1xpsc_i2s_startup,
  230. .trigger = au1xpsc_i2s_trigger,
  231. .hw_params = au1xpsc_i2s_hw_params,
  232. .set_fmt = au1xpsc_i2s_set_fmt,
  233. };
  234. static const struct snd_soc_dai_driver au1xpsc_i2s_dai_template = {
  235. .playback = {
  236. .rates = AU1XPSC_I2S_RATES,
  237. .formats = AU1XPSC_I2S_FMTS,
  238. .channels_min = 2,
  239. .channels_max = 8, /* 2 without external help */
  240. },
  241. .capture = {
  242. .rates = AU1XPSC_I2S_RATES,
  243. .formats = AU1XPSC_I2S_FMTS,
  244. .channels_min = 2,
  245. .channels_max = 8, /* 2 without external help */
  246. },
  247. .ops = &au1xpsc_i2s_dai_ops,
  248. };
  249. static const struct snd_soc_component_driver au1xpsc_i2s_component = {
  250. .name = "au1xpsc-i2s",
  251. };
  252. static int au1xpsc_i2s_drvprobe(struct platform_device *pdev)
  253. {
  254. struct resource *iores, *dmares;
  255. unsigned long sel;
  256. struct au1xpsc_audio_data *wd;
  257. wd = devm_kzalloc(&pdev->dev, sizeof(struct au1xpsc_audio_data),
  258. GFP_KERNEL);
  259. if (!wd)
  260. return -ENOMEM;
  261. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  262. wd->mmio = devm_ioremap_resource(&pdev->dev, iores);
  263. if (IS_ERR(wd->mmio))
  264. return PTR_ERR(wd->mmio);
  265. dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  266. if (!dmares)
  267. return -EBUSY;
  268. wd->dmaids[SNDRV_PCM_STREAM_PLAYBACK] = dmares->start;
  269. dmares = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  270. if (!dmares)
  271. return -EBUSY;
  272. wd->dmaids[SNDRV_PCM_STREAM_CAPTURE] = dmares->start;
  273. /* preserve PSC clock source set up by platform (dev.platform_data
  274. * is already occupied by soc layer)
  275. */
  276. sel = __raw_readl(PSC_SEL(wd)) & PSC_SEL_CLK_MASK;
  277. __raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
  278. wmb(); /* drain writebuffer */
  279. __raw_writel(PSC_SEL_PS_I2SMODE | sel, PSC_SEL(wd));
  280. __raw_writel(0, I2S_CFG(wd));
  281. wmb(); /* drain writebuffer */
  282. /* preconfigure: set max rx/tx fifo depths */
  283. wd->cfg |= PSC_I2SCFG_RT_FIFO8 | PSC_I2SCFG_TT_FIFO8;
  284. /* don't wait for I2S core to become ready now; clocks may not
  285. * be running yet; depending on clock input for PSC a wait might
  286. * time out.
  287. */
  288. /* name the DAI like this device instance ("au1xpsc-i2s.PSCINDEX") */
  289. memcpy(&wd->dai_drv, &au1xpsc_i2s_dai_template,
  290. sizeof(struct snd_soc_dai_driver));
  291. wd->dai_drv.name = dev_name(&pdev->dev);
  292. platform_set_drvdata(pdev, wd);
  293. return snd_soc_register_component(&pdev->dev, &au1xpsc_i2s_component,
  294. &wd->dai_drv, 1);
  295. }
  296. static int au1xpsc_i2s_drvremove(struct platform_device *pdev)
  297. {
  298. struct au1xpsc_audio_data *wd = platform_get_drvdata(pdev);
  299. snd_soc_unregister_component(&pdev->dev);
  300. __raw_writel(0, I2S_CFG(wd));
  301. wmb(); /* drain writebuffer */
  302. __raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
  303. wmb(); /* drain writebuffer */
  304. return 0;
  305. }
  306. #ifdef CONFIG_PM
  307. static int au1xpsc_i2s_drvsuspend(struct device *dev)
  308. {
  309. struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
  310. /* save interesting register and disable PSC */
  311. wd->pm[0] = __raw_readl(PSC_SEL(wd));
  312. __raw_writel(0, I2S_CFG(wd));
  313. wmb(); /* drain writebuffer */
  314. __raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
  315. wmb(); /* drain writebuffer */
  316. return 0;
  317. }
  318. static int au1xpsc_i2s_drvresume(struct device *dev)
  319. {
  320. struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
  321. /* select I2S mode and PSC clock */
  322. __raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
  323. wmb(); /* drain writebuffer */
  324. __raw_writel(0, PSC_SEL(wd));
  325. wmb(); /* drain writebuffer */
  326. __raw_writel(wd->pm[0], PSC_SEL(wd));
  327. wmb(); /* drain writebuffer */
  328. return 0;
  329. }
  330. static struct dev_pm_ops au1xpsci2s_pmops = {
  331. .suspend = au1xpsc_i2s_drvsuspend,
  332. .resume = au1xpsc_i2s_drvresume,
  333. };
  334. #define AU1XPSCI2S_PMOPS &au1xpsci2s_pmops
  335. #else
  336. #define AU1XPSCI2S_PMOPS NULL
  337. #endif
  338. static struct platform_driver au1xpsc_i2s_driver = {
  339. .driver = {
  340. .name = "au1xpsc_i2s",
  341. .pm = AU1XPSCI2S_PMOPS,
  342. },
  343. .probe = au1xpsc_i2s_drvprobe,
  344. .remove = au1xpsc_i2s_drvremove,
  345. };
  346. module_platform_driver(au1xpsc_i2s_driver);
  347. MODULE_LICENSE("GPL");
  348. MODULE_DESCRIPTION("Au12x0/Au1550 PSC I2S ALSA ASoC audio driver");
  349. MODULE_AUTHOR("Manuel Lauss");