axi-spdif.c 6.2 KB

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