axi-i2s.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012-2013, Analog Devices Inc.
  4. * Author: Lars-Peter Clausen <lars@metafoo.de>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/slab.h>
  14. #include <sound/core.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/soc.h>
  18. #include <sound/dmaengine_pcm.h>
  19. #define AXI_I2S_REG_RESET 0x00
  20. #define AXI_I2S_REG_CTRL 0x04
  21. #define AXI_I2S_REG_CLK_CTRL 0x08
  22. #define AXI_I2S_REG_STATUS 0x10
  23. #define AXI_I2S_REG_RX_FIFO 0x28
  24. #define AXI_I2S_REG_TX_FIFO 0x2C
  25. #define AXI_I2S_RESET_GLOBAL BIT(0)
  26. #define AXI_I2S_RESET_TX_FIFO BIT(1)
  27. #define AXI_I2S_RESET_RX_FIFO BIT(2)
  28. #define AXI_I2S_CTRL_TX_EN BIT(0)
  29. #define AXI_I2S_CTRL_RX_EN BIT(1)
  30. /* The frame size is configurable, but for now we always set it 64 bit */
  31. #define AXI_I2S_BITS_PER_FRAME 64
  32. struct axi_i2s {
  33. struct regmap *regmap;
  34. struct clk *clk;
  35. struct clk *clk_ref;
  36. bool has_capture;
  37. bool has_playback;
  38. struct snd_soc_dai_driver dai_driver;
  39. struct snd_dmaengine_dai_dma_data capture_dma_data;
  40. struct snd_dmaengine_dai_dma_data playback_dma_data;
  41. struct snd_ratnum ratnum;
  42. struct snd_pcm_hw_constraint_ratnums rate_constraints;
  43. };
  44. static int axi_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  45. struct snd_soc_dai *dai)
  46. {
  47. struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  48. unsigned int mask, val;
  49. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  50. mask = AXI_I2S_CTRL_RX_EN;
  51. else
  52. mask = AXI_I2S_CTRL_TX_EN;
  53. switch (cmd) {
  54. case SNDRV_PCM_TRIGGER_START:
  55. case SNDRV_PCM_TRIGGER_RESUME:
  56. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  57. val = mask;
  58. break;
  59. case SNDRV_PCM_TRIGGER_STOP:
  60. case SNDRV_PCM_TRIGGER_SUSPEND:
  61. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  62. val = 0;
  63. break;
  64. default:
  65. return -EINVAL;
  66. }
  67. regmap_update_bits(i2s->regmap, AXI_I2S_REG_CTRL, mask, val);
  68. return 0;
  69. }
  70. static int axi_i2s_hw_params(struct snd_pcm_substream *substream,
  71. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  72. {
  73. struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  74. unsigned int bclk_div, word_size;
  75. unsigned int bclk_rate;
  76. bclk_rate = params_rate(params) * AXI_I2S_BITS_PER_FRAME;
  77. word_size = AXI_I2S_BITS_PER_FRAME / 2 - 1;
  78. bclk_div = DIV_ROUND_UP(clk_get_rate(i2s->clk_ref), bclk_rate) / 2 - 1;
  79. regmap_write(i2s->regmap, AXI_I2S_REG_CLK_CTRL, (word_size << 16) |
  80. bclk_div);
  81. return 0;
  82. }
  83. static int axi_i2s_startup(struct snd_pcm_substream *substream,
  84. struct snd_soc_dai *dai)
  85. {
  86. struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  87. uint32_t mask;
  88. int ret;
  89. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  90. mask = AXI_I2S_RESET_RX_FIFO;
  91. else
  92. mask = AXI_I2S_RESET_TX_FIFO;
  93. regmap_write(i2s->regmap, AXI_I2S_REG_RESET, mask);
  94. ret = snd_pcm_hw_constraint_ratnums(substream->runtime, 0,
  95. SNDRV_PCM_HW_PARAM_RATE,
  96. &i2s->rate_constraints);
  97. if (ret)
  98. return ret;
  99. return clk_prepare_enable(i2s->clk_ref);
  100. }
  101. static void axi_i2s_shutdown(struct snd_pcm_substream *substream,
  102. struct snd_soc_dai *dai)
  103. {
  104. struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  105. clk_disable_unprepare(i2s->clk_ref);
  106. }
  107. static int axi_i2s_dai_probe(struct snd_soc_dai *dai)
  108. {
  109. struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  110. snd_soc_dai_init_dma_data(
  111. dai,
  112. i2s->has_playback ? &i2s->playback_dma_data : NULL,
  113. i2s->has_capture ? &i2s->capture_dma_data : NULL);
  114. return 0;
  115. }
  116. static const struct snd_soc_dai_ops axi_i2s_dai_ops = {
  117. .startup = axi_i2s_startup,
  118. .shutdown = axi_i2s_shutdown,
  119. .trigger = axi_i2s_trigger,
  120. .hw_params = axi_i2s_hw_params,
  121. };
  122. static struct snd_soc_dai_driver axi_i2s_dai = {
  123. .probe = axi_i2s_dai_probe,
  124. .ops = &axi_i2s_dai_ops,
  125. .symmetric_rates = 1,
  126. };
  127. static const struct snd_soc_component_driver axi_i2s_component = {
  128. .name = "axi-i2s",
  129. };
  130. static const struct regmap_config axi_i2s_regmap_config = {
  131. .reg_bits = 32,
  132. .reg_stride = 4,
  133. .val_bits = 32,
  134. .max_register = AXI_I2S_REG_STATUS,
  135. };
  136. static void axi_i2s_parse_of(struct axi_i2s *i2s, const struct device_node *np)
  137. {
  138. struct property *dma_names;
  139. const char *dma_name;
  140. of_property_for_each_string(np, "dma-names", dma_names, dma_name) {
  141. if (strcmp(dma_name, "rx") == 0)
  142. i2s->has_capture = true;
  143. if (strcmp(dma_name, "tx") == 0)
  144. i2s->has_playback = true;
  145. }
  146. }
  147. static int axi_i2s_probe(struct platform_device *pdev)
  148. {
  149. struct resource *res;
  150. struct axi_i2s *i2s;
  151. void __iomem *base;
  152. int ret;
  153. i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
  154. if (!i2s)
  155. return -ENOMEM;
  156. platform_set_drvdata(pdev, i2s);
  157. axi_i2s_parse_of(i2s, pdev->dev.of_node);
  158. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  159. base = devm_ioremap_resource(&pdev->dev, res);
  160. if (IS_ERR(base))
  161. return PTR_ERR(base);
  162. i2s->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  163. &axi_i2s_regmap_config);
  164. if (IS_ERR(i2s->regmap))
  165. return PTR_ERR(i2s->regmap);
  166. i2s->clk = devm_clk_get(&pdev->dev, "axi");
  167. if (IS_ERR(i2s->clk))
  168. return PTR_ERR(i2s->clk);
  169. i2s->clk_ref = devm_clk_get(&pdev->dev, "ref");
  170. if (IS_ERR(i2s->clk_ref))
  171. return PTR_ERR(i2s->clk_ref);
  172. ret = clk_prepare_enable(i2s->clk);
  173. if (ret)
  174. return ret;
  175. if (i2s->has_playback) {
  176. axi_i2s_dai.playback.channels_min = 2;
  177. axi_i2s_dai.playback.channels_max = 2;
  178. axi_i2s_dai.playback.rates = SNDRV_PCM_RATE_KNOT;
  179. axi_i2s_dai.playback.formats =
  180. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_U32_LE;
  181. i2s->playback_dma_data.addr = res->start + AXI_I2S_REG_TX_FIFO;
  182. i2s->playback_dma_data.addr_width = 4;
  183. i2s->playback_dma_data.maxburst = 1;
  184. }
  185. if (i2s->has_capture) {
  186. axi_i2s_dai.capture.channels_min = 2;
  187. axi_i2s_dai.capture.channels_max = 2;
  188. axi_i2s_dai.capture.rates = SNDRV_PCM_RATE_KNOT;
  189. axi_i2s_dai.capture.formats =
  190. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_U32_LE;
  191. i2s->capture_dma_data.addr = res->start + AXI_I2S_REG_RX_FIFO;
  192. i2s->capture_dma_data.addr_width = 4;
  193. i2s->capture_dma_data.maxburst = 1;
  194. }
  195. i2s->ratnum.num = clk_get_rate(i2s->clk_ref) / 2 / AXI_I2S_BITS_PER_FRAME;
  196. i2s->ratnum.den_step = 1;
  197. i2s->ratnum.den_min = 1;
  198. i2s->ratnum.den_max = 64;
  199. i2s->rate_constraints.rats = &i2s->ratnum;
  200. i2s->rate_constraints.nrats = 1;
  201. regmap_write(i2s->regmap, AXI_I2S_REG_RESET, AXI_I2S_RESET_GLOBAL);
  202. ret = devm_snd_soc_register_component(&pdev->dev, &axi_i2s_component,
  203. &axi_i2s_dai, 1);
  204. if (ret)
  205. goto err_clk_disable;
  206. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  207. if (ret)
  208. goto err_clk_disable;
  209. dev_info(&pdev->dev, "probed, capture %s, playback %s\n",
  210. i2s->has_capture ? "enabled" : "disabled",
  211. i2s->has_playback ? "enabled" : "disabled");
  212. return 0;
  213. err_clk_disable:
  214. clk_disable_unprepare(i2s->clk);
  215. return ret;
  216. }
  217. static int axi_i2s_dev_remove(struct platform_device *pdev)
  218. {
  219. struct axi_i2s *i2s = platform_get_drvdata(pdev);
  220. clk_disable_unprepare(i2s->clk);
  221. return 0;
  222. }
  223. static const struct of_device_id axi_i2s_of_match[] = {
  224. { .compatible = "adi,axi-i2s-1.00.a", },
  225. {},
  226. };
  227. MODULE_DEVICE_TABLE(of, axi_i2s_of_match);
  228. static struct platform_driver axi_i2s_driver = {
  229. .driver = {
  230. .name = "axi-i2s",
  231. .of_match_table = axi_i2s_of_match,
  232. },
  233. .probe = axi_i2s_probe,
  234. .remove = axi_i2s_dev_remove,
  235. };
  236. module_platform_driver(axi_i2s_driver);
  237. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  238. MODULE_DESCRIPTION("AXI I2S driver");
  239. MODULE_LICENSE("GPL");