i2sc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Au1000/Au1500/Au1100 I2S controller driver for ASoC
  4. *
  5. * (c) 2011 Manuel Lauss <[email protected]>
  6. *
  7. * Note: clock supplied to the I2S controller must be 256x samplerate.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/suspend.h>
  13. #include <sound/core.h>
  14. #include <sound/pcm.h>
  15. #include <sound/initval.h>
  16. #include <sound/soc.h>
  17. #include <asm/mach-au1x00/au1000.h>
  18. #include "psc.h"
  19. #define I2S_RXTX 0x00
  20. #define I2S_CFG 0x04
  21. #define I2S_ENABLE 0x08
  22. #define CFG_XU (1 << 25) /* tx underflow */
  23. #define CFG_XO (1 << 24)
  24. #define CFG_RU (1 << 23)
  25. #define CFG_RO (1 << 22)
  26. #define CFG_TR (1 << 21)
  27. #define CFG_TE (1 << 20)
  28. #define CFG_TF (1 << 19)
  29. #define CFG_RR (1 << 18)
  30. #define CFG_RF (1 << 17)
  31. #define CFG_ICK (1 << 12) /* clock invert */
  32. #define CFG_PD (1 << 11) /* set to make I2SDIO INPUT */
  33. #define CFG_LB (1 << 10) /* loopback */
  34. #define CFG_IC (1 << 9) /* word select invert */
  35. #define CFG_FM_I2S (0 << 7) /* I2S format */
  36. #define CFG_FM_LJ (1 << 7) /* left-justified */
  37. #define CFG_FM_RJ (2 << 7) /* right-justified */
  38. #define CFG_FM_MASK (3 << 7)
  39. #define CFG_TN (1 << 6) /* tx fifo en */
  40. #define CFG_RN (1 << 5) /* rx fifo en */
  41. #define CFG_SZ_8 (0x08)
  42. #define CFG_SZ_16 (0x10)
  43. #define CFG_SZ_18 (0x12)
  44. #define CFG_SZ_20 (0x14)
  45. #define CFG_SZ_24 (0x18)
  46. #define CFG_SZ_MASK (0x1f)
  47. #define EN_D (1 << 1) /* DISable */
  48. #define EN_CE (1 << 0) /* clock enable */
  49. /* only limited by clock generator and board design */
  50. #define AU1XI2SC_RATES \
  51. SNDRV_PCM_RATE_CONTINUOUS
  52. #define AU1XI2SC_FMTS \
  53. (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | \
  54. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  55. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE | \
  56. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_U18_3LE | \
  57. SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_U18_3BE | \
  58. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_U20_3LE | \
  59. SNDRV_PCM_FMTBIT_S20_3BE | SNDRV_PCM_FMTBIT_U20_3BE | \
  60. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE | \
  61. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE | \
  62. 0)
  63. static inline unsigned long RD(struct au1xpsc_audio_data *ctx, int reg)
  64. {
  65. return __raw_readl(ctx->mmio + reg);
  66. }
  67. static inline void WR(struct au1xpsc_audio_data *ctx, int reg, unsigned long v)
  68. {
  69. __raw_writel(v, ctx->mmio + reg);
  70. wmb();
  71. }
  72. static int au1xi2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  73. {
  74. struct au1xpsc_audio_data *ctx = snd_soc_dai_get_drvdata(cpu_dai);
  75. unsigned long c;
  76. int ret;
  77. ret = -EINVAL;
  78. c = ctx->cfg;
  79. c &= ~CFG_FM_MASK;
  80. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  81. case SND_SOC_DAIFMT_I2S:
  82. c |= CFG_FM_I2S;
  83. break;
  84. case SND_SOC_DAIFMT_MSB:
  85. c |= CFG_FM_RJ;
  86. break;
  87. case SND_SOC_DAIFMT_LSB:
  88. c |= CFG_FM_LJ;
  89. break;
  90. default:
  91. goto out;
  92. }
  93. c &= ~(CFG_IC | CFG_ICK); /* IB-IF */
  94. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  95. case SND_SOC_DAIFMT_NB_NF:
  96. c |= CFG_IC | CFG_ICK;
  97. break;
  98. case SND_SOC_DAIFMT_NB_IF:
  99. c |= CFG_IC;
  100. break;
  101. case SND_SOC_DAIFMT_IB_NF:
  102. c |= CFG_ICK;
  103. break;
  104. case SND_SOC_DAIFMT_IB_IF:
  105. break;
  106. default:
  107. goto out;
  108. }
  109. /* I2S controller only supports provider */
  110. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  111. case SND_SOC_DAIFMT_BP_FP: /* CODEC consumer */
  112. break;
  113. default:
  114. goto out;
  115. }
  116. ret = 0;
  117. ctx->cfg = c;
  118. out:
  119. return ret;
  120. }
  121. static int au1xi2s_trigger(struct snd_pcm_substream *substream,
  122. int cmd, struct snd_soc_dai *dai)
  123. {
  124. struct au1xpsc_audio_data *ctx = snd_soc_dai_get_drvdata(dai);
  125. int stype = SUBSTREAM_TYPE(substream);
  126. switch (cmd) {
  127. case SNDRV_PCM_TRIGGER_START:
  128. case SNDRV_PCM_TRIGGER_RESUME:
  129. /* power up */
  130. WR(ctx, I2S_ENABLE, EN_D | EN_CE);
  131. WR(ctx, I2S_ENABLE, EN_CE);
  132. ctx->cfg |= (stype == PCM_TX) ? CFG_TN : CFG_RN;
  133. WR(ctx, I2S_CFG, ctx->cfg);
  134. break;
  135. case SNDRV_PCM_TRIGGER_STOP:
  136. case SNDRV_PCM_TRIGGER_SUSPEND:
  137. ctx->cfg &= ~((stype == PCM_TX) ? CFG_TN : CFG_RN);
  138. WR(ctx, I2S_CFG, ctx->cfg);
  139. WR(ctx, I2S_ENABLE, EN_D); /* power off */
  140. break;
  141. default:
  142. return -EINVAL;
  143. }
  144. return 0;
  145. }
  146. static unsigned long msbits_to_reg(int msbits)
  147. {
  148. switch (msbits) {
  149. case 8:
  150. return CFG_SZ_8;
  151. case 16:
  152. return CFG_SZ_16;
  153. case 18:
  154. return CFG_SZ_18;
  155. case 20:
  156. return CFG_SZ_20;
  157. case 24:
  158. return CFG_SZ_24;
  159. }
  160. return 0;
  161. }
  162. static int au1xi2s_hw_params(struct snd_pcm_substream *substream,
  163. struct snd_pcm_hw_params *params,
  164. struct snd_soc_dai *dai)
  165. {
  166. struct au1xpsc_audio_data *ctx = snd_soc_dai_get_drvdata(dai);
  167. unsigned long v;
  168. v = msbits_to_reg(params->msbits);
  169. if (!v)
  170. return -EINVAL;
  171. ctx->cfg &= ~CFG_SZ_MASK;
  172. ctx->cfg |= v;
  173. return 0;
  174. }
  175. static int au1xi2s_startup(struct snd_pcm_substream *substream,
  176. struct snd_soc_dai *dai)
  177. {
  178. struct au1xpsc_audio_data *ctx = snd_soc_dai_get_drvdata(dai);
  179. snd_soc_dai_set_dma_data(dai, substream, &ctx->dmaids[0]);
  180. return 0;
  181. }
  182. static const struct snd_soc_dai_ops au1xi2s_dai_ops = {
  183. .startup = au1xi2s_startup,
  184. .trigger = au1xi2s_trigger,
  185. .hw_params = au1xi2s_hw_params,
  186. .set_fmt = au1xi2s_set_fmt,
  187. };
  188. static struct snd_soc_dai_driver au1xi2s_dai_driver = {
  189. .symmetric_rate = 1,
  190. .playback = {
  191. .rates = AU1XI2SC_RATES,
  192. .formats = AU1XI2SC_FMTS,
  193. .channels_min = 2,
  194. .channels_max = 2,
  195. },
  196. .capture = {
  197. .rates = AU1XI2SC_RATES,
  198. .formats = AU1XI2SC_FMTS,
  199. .channels_min = 2,
  200. .channels_max = 2,
  201. },
  202. .ops = &au1xi2s_dai_ops,
  203. };
  204. static const struct snd_soc_component_driver au1xi2s_component = {
  205. .name = "au1xi2s",
  206. .legacy_dai_naming = 1,
  207. };
  208. static int au1xi2s_drvprobe(struct platform_device *pdev)
  209. {
  210. struct resource *iores, *dmares;
  211. struct au1xpsc_audio_data *ctx;
  212. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  213. if (!ctx)
  214. return -ENOMEM;
  215. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  216. if (!iores)
  217. return -ENODEV;
  218. if (!devm_request_mem_region(&pdev->dev, iores->start,
  219. resource_size(iores),
  220. pdev->name))
  221. return -EBUSY;
  222. ctx->mmio = devm_ioremap(&pdev->dev, iores->start,
  223. resource_size(iores));
  224. if (!ctx->mmio)
  225. return -EBUSY;
  226. dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  227. if (!dmares)
  228. return -EBUSY;
  229. ctx->dmaids[SNDRV_PCM_STREAM_PLAYBACK] = dmares->start;
  230. dmares = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  231. if (!dmares)
  232. return -EBUSY;
  233. ctx->dmaids[SNDRV_PCM_STREAM_CAPTURE] = dmares->start;
  234. platform_set_drvdata(pdev, ctx);
  235. return snd_soc_register_component(&pdev->dev, &au1xi2s_component,
  236. &au1xi2s_dai_driver, 1);
  237. }
  238. static int au1xi2s_drvremove(struct platform_device *pdev)
  239. {
  240. struct au1xpsc_audio_data *ctx = platform_get_drvdata(pdev);
  241. snd_soc_unregister_component(&pdev->dev);
  242. WR(ctx, I2S_ENABLE, EN_D); /* clock off, disable */
  243. return 0;
  244. }
  245. #ifdef CONFIG_PM
  246. static int au1xi2s_drvsuspend(struct device *dev)
  247. {
  248. struct au1xpsc_audio_data *ctx = dev_get_drvdata(dev);
  249. WR(ctx, I2S_ENABLE, EN_D); /* clock off, disable */
  250. return 0;
  251. }
  252. static int au1xi2s_drvresume(struct device *dev)
  253. {
  254. return 0;
  255. }
  256. static const struct dev_pm_ops au1xi2sc_pmops = {
  257. .suspend = au1xi2s_drvsuspend,
  258. .resume = au1xi2s_drvresume,
  259. };
  260. #define AU1XI2SC_PMOPS (&au1xi2sc_pmops)
  261. #else
  262. #define AU1XI2SC_PMOPS NULL
  263. #endif
  264. static struct platform_driver au1xi2s_driver = {
  265. .driver = {
  266. .name = "alchemy-i2sc",
  267. .pm = AU1XI2SC_PMOPS,
  268. },
  269. .probe = au1xi2s_drvprobe,
  270. .remove = au1xi2s_drvremove,
  271. };
  272. module_platform_driver(au1xi2s_driver);
  273. MODULE_LICENSE("GPL");
  274. MODULE_DESCRIPTION("Au1000/1500/1100 I2S ASoC driver");
  275. MODULE_AUTHOR("Manuel Lauss");