psc-i2s.c 11 KB

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