i2sc.c 7.3 KB

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