xlnx_i2s.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Xilinx ASoC I2S audio support
  4. //
  5. // Copyright (C) 2018 Xilinx, Inc.
  6. //
  7. // Author: Praveen Vuppala <praveenv@xilinx.com>
  8. // Author: Maruthi Srinivas Bayyavarapu <maruthis@xilinx.com>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <sound/pcm_params.h>
  15. #include <sound/soc.h>
  16. #define DRV_NAME "xlnx_i2s"
  17. #define I2S_CORE_CTRL_OFFSET 0x08
  18. #define I2S_I2STIM_OFFSET 0x20
  19. #define I2S_CH0_OFFSET 0x30
  20. #define I2S_I2STIM_VALID_MASK GENMASK(7, 0)
  21. static int xlnx_i2s_set_sclkout_div(struct snd_soc_dai *cpu_dai,
  22. int div_id, int div)
  23. {
  24. void __iomem *base = snd_soc_dai_get_drvdata(cpu_dai);
  25. if (!div || (div & ~I2S_I2STIM_VALID_MASK))
  26. return -EINVAL;
  27. writel(div, base + I2S_I2STIM_OFFSET);
  28. return 0;
  29. }
  30. static int xlnx_i2s_hw_params(struct snd_pcm_substream *substream,
  31. struct snd_pcm_hw_params *params,
  32. struct snd_soc_dai *i2s_dai)
  33. {
  34. u32 reg_off, chan_id;
  35. void __iomem *base = snd_soc_dai_get_drvdata(i2s_dai);
  36. chan_id = params_channels(params) / 2;
  37. while (chan_id > 0) {
  38. reg_off = I2S_CH0_OFFSET + ((chan_id - 1) * 4);
  39. writel(chan_id, base + reg_off);
  40. chan_id--;
  41. }
  42. return 0;
  43. }
  44. static int xlnx_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  45. struct snd_soc_dai *i2s_dai)
  46. {
  47. void __iomem *base = snd_soc_dai_get_drvdata(i2s_dai);
  48. switch (cmd) {
  49. case SNDRV_PCM_TRIGGER_START:
  50. case SNDRV_PCM_TRIGGER_RESUME:
  51. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  52. writel(1, base + I2S_CORE_CTRL_OFFSET);
  53. break;
  54. case SNDRV_PCM_TRIGGER_STOP:
  55. case SNDRV_PCM_TRIGGER_SUSPEND:
  56. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  57. writel(0, base + I2S_CORE_CTRL_OFFSET);
  58. break;
  59. default:
  60. return -EINVAL;
  61. }
  62. return 0;
  63. }
  64. static const struct snd_soc_dai_ops xlnx_i2s_dai_ops = {
  65. .trigger = xlnx_i2s_trigger,
  66. .set_clkdiv = xlnx_i2s_set_sclkout_div,
  67. .hw_params = xlnx_i2s_hw_params
  68. };
  69. static const struct snd_soc_component_driver xlnx_i2s_component = {
  70. .name = DRV_NAME,
  71. };
  72. static const struct of_device_id xlnx_i2s_of_match[] = {
  73. { .compatible = "xlnx,i2s-transmitter-1.0", },
  74. { .compatible = "xlnx,i2s-receiver-1.0", },
  75. {},
  76. };
  77. MODULE_DEVICE_TABLE(of, xlnx_i2s_of_match);
  78. static int xlnx_i2s_probe(struct platform_device *pdev)
  79. {
  80. void __iomem *base;
  81. struct snd_soc_dai_driver *dai_drv;
  82. int ret;
  83. u32 ch, format, data_width;
  84. struct device *dev = &pdev->dev;
  85. struct device_node *node = dev->of_node;
  86. dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL);
  87. if (!dai_drv)
  88. return -ENOMEM;
  89. base = devm_platform_ioremap_resource(pdev, 0);
  90. if (IS_ERR(base))
  91. return PTR_ERR(base);
  92. ret = of_property_read_u32(node, "xlnx,num-channels", &ch);
  93. if (ret < 0) {
  94. dev_err(dev, "cannot get supported channels\n");
  95. return ret;
  96. }
  97. ch = ch * 2;
  98. ret = of_property_read_u32(node, "xlnx,dwidth", &data_width);
  99. if (ret < 0) {
  100. dev_err(dev, "cannot get data width\n");
  101. return ret;
  102. }
  103. switch (data_width) {
  104. case 16:
  105. format = SNDRV_PCM_FMTBIT_S16_LE;
  106. break;
  107. case 24:
  108. format = SNDRV_PCM_FMTBIT_S24_LE;
  109. break;
  110. default:
  111. return -EINVAL;
  112. }
  113. if (of_device_is_compatible(node, "xlnx,i2s-transmitter-1.0")) {
  114. dai_drv->name = "xlnx_i2s_playback";
  115. dai_drv->playback.stream_name = "Playback";
  116. dai_drv->playback.formats = format;
  117. dai_drv->playback.channels_min = ch;
  118. dai_drv->playback.channels_max = ch;
  119. dai_drv->playback.rates = SNDRV_PCM_RATE_8000_192000;
  120. dai_drv->ops = &xlnx_i2s_dai_ops;
  121. } else if (of_device_is_compatible(node, "xlnx,i2s-receiver-1.0")) {
  122. dai_drv->name = "xlnx_i2s_capture";
  123. dai_drv->capture.stream_name = "Capture";
  124. dai_drv->capture.formats = format;
  125. dai_drv->capture.channels_min = ch;
  126. dai_drv->capture.channels_max = ch;
  127. dai_drv->capture.rates = SNDRV_PCM_RATE_8000_192000;
  128. dai_drv->ops = &xlnx_i2s_dai_ops;
  129. } else {
  130. return -ENODEV;
  131. }
  132. dev_set_drvdata(&pdev->dev, base);
  133. ret = devm_snd_soc_register_component(&pdev->dev, &xlnx_i2s_component,
  134. dai_drv, 1);
  135. if (ret) {
  136. dev_err(&pdev->dev, "i2s component registration failed\n");
  137. return ret;
  138. }
  139. dev_info(&pdev->dev, "%s DAI registered\n", dai_drv->name);
  140. return ret;
  141. }
  142. static struct platform_driver xlnx_i2s_aud_driver = {
  143. .driver = {
  144. .name = DRV_NAME,
  145. .of_match_table = xlnx_i2s_of_match,
  146. },
  147. .probe = xlnx_i2s_probe,
  148. };
  149. module_platform_driver(xlnx_i2s_aud_driver);
  150. MODULE_LICENSE("GPL v2");
  151. MODULE_AUTHOR("Praveen Vuppala <praveenv@xilinx.com>");
  152. MODULE_AUTHOR("Maruthi Srinivas Bayyavarapu <maruthis@xilinx.com>");