axi-spdif.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/of.h>
  12. #include <linux/clk.h>
  13. #include <linux/regmap.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/initval.h>
  19. #include <sound/dmaengine_pcm.h>
  20. #define AXI_SPDIF_REG_CTRL 0x0
  21. #define AXI_SPDIF_REG_STAT 0x4
  22. #define AXI_SPDIF_REG_TX_FIFO 0xc
  23. #define AXI_SPDIF_CTRL_TXDATA BIT(1)
  24. #define AXI_SPDIF_CTRL_TXEN BIT(0)
  25. #define AXI_SPDIF_CTRL_CLKDIV_OFFSET 8
  26. #define AXI_SPDIF_CTRL_CLKDIV_MASK (0xff << 8)
  27. #define AXI_SPDIF_FREQ_44100 (0x0 << 6)
  28. #define AXI_SPDIF_FREQ_48000 (0x1 << 6)
  29. #define AXI_SPDIF_FREQ_32000 (0x2 << 6)
  30. #define AXI_SPDIF_FREQ_NA (0x3 << 6)
  31. struct axi_spdif {
  32. struct regmap *regmap;
  33. struct clk *clk;
  34. struct clk *clk_ref;
  35. struct snd_dmaengine_dai_dma_data dma_data;
  36. struct snd_ratnum ratnum;
  37. struct snd_pcm_hw_constraint_ratnums rate_constraints;
  38. };
  39. static int axi_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
  40. struct snd_soc_dai *dai)
  41. {
  42. struct axi_spdif *spdif = snd_soc_dai_get_drvdata(dai);
  43. unsigned int val;
  44. switch (cmd) {
  45. case SNDRV_PCM_TRIGGER_START:
  46. case SNDRV_PCM_TRIGGER_RESUME:
  47. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  48. val = AXI_SPDIF_CTRL_TXDATA;
  49. break;
  50. case SNDRV_PCM_TRIGGER_STOP:
  51. case SNDRV_PCM_TRIGGER_SUSPEND:
  52. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  53. val = 0;
  54. break;
  55. default:
  56. return -EINVAL;
  57. }
  58. regmap_update_bits(spdif->regmap, AXI_SPDIF_REG_CTRL,
  59. AXI_SPDIF_CTRL_TXDATA, val);
  60. return 0;
  61. }
  62. static int axi_spdif_hw_params(struct snd_pcm_substream *substream,
  63. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  64. {
  65. struct axi_spdif *spdif = snd_soc_dai_get_drvdata(dai);
  66. unsigned int rate = params_rate(params);
  67. unsigned int clkdiv, stat;
  68. switch (params_rate(params)) {
  69. case 32000:
  70. stat = AXI_SPDIF_FREQ_32000;
  71. break;
  72. case 44100:
  73. stat = AXI_SPDIF_FREQ_44100;
  74. break;
  75. case 48000:
  76. stat = AXI_SPDIF_FREQ_48000;
  77. break;
  78. default:
  79. stat = AXI_SPDIF_FREQ_NA;
  80. break;
  81. }
  82. clkdiv = DIV_ROUND_CLOSEST(clk_get_rate(spdif->clk_ref),
  83. rate * 64 * 2) - 1;
  84. clkdiv <<= AXI_SPDIF_CTRL_CLKDIV_OFFSET;
  85. regmap_write(spdif->regmap, AXI_SPDIF_REG_STAT, stat);
  86. regmap_update_bits(spdif->regmap, AXI_SPDIF_REG_CTRL,
  87. AXI_SPDIF_CTRL_CLKDIV_MASK, clkdiv);
  88. return 0;
  89. }
  90. static int axi_spdif_dai_probe(struct snd_soc_dai *dai)
  91. {
  92. struct axi_spdif *spdif = snd_soc_dai_get_drvdata(dai);
  93. snd_soc_dai_init_dma_data(dai, &spdif->dma_data, NULL);
  94. return 0;
  95. }
  96. static int axi_spdif_startup(struct snd_pcm_substream *substream,
  97. struct snd_soc_dai *dai)
  98. {
  99. struct axi_spdif *spdif = snd_soc_dai_get_drvdata(dai);
  100. int ret;
  101. ret = snd_pcm_hw_constraint_ratnums(substream->runtime, 0,
  102. SNDRV_PCM_HW_PARAM_RATE,
  103. &spdif->rate_constraints);
  104. if (ret)
  105. return ret;
  106. ret = clk_prepare_enable(spdif->clk_ref);
  107. if (ret)
  108. return ret;
  109. regmap_update_bits(spdif->regmap, AXI_SPDIF_REG_CTRL,
  110. AXI_SPDIF_CTRL_TXEN, AXI_SPDIF_CTRL_TXEN);
  111. return 0;
  112. }
  113. static void axi_spdif_shutdown(struct snd_pcm_substream *substream,
  114. struct snd_soc_dai *dai)
  115. {
  116. struct axi_spdif *spdif = snd_soc_dai_get_drvdata(dai);
  117. regmap_update_bits(spdif->regmap, AXI_SPDIF_REG_CTRL,
  118. AXI_SPDIF_CTRL_TXEN, 0);
  119. clk_disable_unprepare(spdif->clk_ref);
  120. }
  121. static const struct snd_soc_dai_ops axi_spdif_dai_ops = {
  122. .startup = axi_spdif_startup,
  123. .shutdown = axi_spdif_shutdown,
  124. .trigger = axi_spdif_trigger,
  125. .hw_params = axi_spdif_hw_params,
  126. };
  127. static struct snd_soc_dai_driver axi_spdif_dai = {
  128. .probe = axi_spdif_dai_probe,
  129. .playback = {
  130. .channels_min = 2,
  131. .channels_max = 2,
  132. .rates = SNDRV_PCM_RATE_KNOT,
  133. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  134. },
  135. .ops = &axi_spdif_dai_ops,
  136. };
  137. static const struct snd_soc_component_driver axi_spdif_component = {
  138. .name = "axi-spdif",
  139. };
  140. static const struct regmap_config axi_spdif_regmap_config = {
  141. .reg_bits = 32,
  142. .reg_stride = 4,
  143. .val_bits = 32,
  144. .max_register = AXI_SPDIF_REG_STAT,
  145. };
  146. static int axi_spdif_probe(struct platform_device *pdev)
  147. {
  148. struct axi_spdif *spdif;
  149. struct resource *res;
  150. void __iomem *base;
  151. int ret;
  152. spdif = devm_kzalloc(&pdev->dev, sizeof(*spdif), GFP_KERNEL);
  153. if (!spdif)
  154. return -ENOMEM;
  155. platform_set_drvdata(pdev, spdif);
  156. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  157. base = devm_ioremap_resource(&pdev->dev, res);
  158. if (IS_ERR(base))
  159. return PTR_ERR(base);
  160. spdif->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  161. &axi_spdif_regmap_config);
  162. if (IS_ERR(spdif->regmap))
  163. return PTR_ERR(spdif->regmap);
  164. spdif->clk = devm_clk_get(&pdev->dev, "axi");
  165. if (IS_ERR(spdif->clk))
  166. return PTR_ERR(spdif->clk);
  167. spdif->clk_ref = devm_clk_get(&pdev->dev, "ref");
  168. if (IS_ERR(spdif->clk_ref))
  169. return PTR_ERR(spdif->clk_ref);
  170. ret = clk_prepare_enable(spdif->clk);
  171. if (ret)
  172. return ret;
  173. spdif->dma_data.addr = res->start + AXI_SPDIF_REG_TX_FIFO;
  174. spdif->dma_data.addr_width = 4;
  175. spdif->dma_data.maxburst = 1;
  176. spdif->ratnum.num = clk_get_rate(spdif->clk_ref) / 128;
  177. spdif->ratnum.den_step = 1;
  178. spdif->ratnum.den_min = 1;
  179. spdif->ratnum.den_max = 64;
  180. spdif->rate_constraints.rats = &spdif->ratnum;
  181. spdif->rate_constraints.nrats = 1;
  182. ret = devm_snd_soc_register_component(&pdev->dev, &axi_spdif_component,
  183. &axi_spdif_dai, 1);
  184. if (ret)
  185. goto err_clk_disable;
  186. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  187. if (ret)
  188. goto err_clk_disable;
  189. return 0;
  190. err_clk_disable:
  191. clk_disable_unprepare(spdif->clk);
  192. return ret;
  193. }
  194. static int axi_spdif_dev_remove(struct platform_device *pdev)
  195. {
  196. struct axi_spdif *spdif = platform_get_drvdata(pdev);
  197. clk_disable_unprepare(spdif->clk);
  198. return 0;
  199. }
  200. static const struct of_device_id axi_spdif_of_match[] = {
  201. { .compatible = "adi,axi-spdif-tx-1.00.a", },
  202. {},
  203. };
  204. MODULE_DEVICE_TABLE(of, axi_spdif_of_match);
  205. static struct platform_driver axi_spdif_driver = {
  206. .driver = {
  207. .name = "axi-spdif",
  208. .of_match_table = axi_spdif_of_match,
  209. },
  210. .probe = axi_spdif_probe,
  211. .remove = axi_spdif_dev_remove,
  212. };
  213. module_platform_driver(axi_spdif_driver);
  214. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  215. MODULE_DESCRIPTION("AXI SPDIF driver");
  216. MODULE_LICENSE("GPL");