psc-i2s.c 10 KB

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