xlnx_i2s.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <[email protected]>
  8. // Author: Maruthi Srinivas Bayyavarapu <[email protected]>
  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_CORE_CTRL_32BIT_LRCLK BIT(3)
  19. #define I2S_CORE_CTRL_ENABLE BIT(0)
  20. #define I2S_I2STIM_OFFSET 0x20
  21. #define I2S_CH0_OFFSET 0x30
  22. #define I2S_I2STIM_VALID_MASK GENMASK(7, 0)
  23. struct xlnx_i2s_drv_data {
  24. struct snd_soc_dai_driver dai_drv;
  25. void __iomem *base;
  26. unsigned int sysclk;
  27. u32 data_width;
  28. u32 channels;
  29. bool is_32bit_lrclk;
  30. struct snd_ratnum ratnum;
  31. struct snd_pcm_hw_constraint_ratnums rate_constraints;
  32. };
  33. static int xlnx_i2s_set_sclkout_div(struct snd_soc_dai *cpu_dai,
  34. int div_id, int div)
  35. {
  36. struct xlnx_i2s_drv_data *drv_data = snd_soc_dai_get_drvdata(cpu_dai);
  37. if (!div || (div & ~I2S_I2STIM_VALID_MASK))
  38. return -EINVAL;
  39. drv_data->sysclk = 0;
  40. writel(div, drv_data->base + I2S_I2STIM_OFFSET);
  41. return 0;
  42. }
  43. static int xlnx_i2s_set_sysclk(struct snd_soc_dai *dai,
  44. int clk_id, unsigned int freq, int dir)
  45. {
  46. struct xlnx_i2s_drv_data *drv_data = snd_soc_dai_get_drvdata(dai);
  47. drv_data->sysclk = freq;
  48. if (freq) {
  49. unsigned int bits_per_sample;
  50. if (drv_data->is_32bit_lrclk)
  51. bits_per_sample = 32;
  52. else
  53. bits_per_sample = drv_data->data_width;
  54. drv_data->ratnum.num = freq / (bits_per_sample * drv_data->channels) / 2;
  55. drv_data->ratnum.den_step = 1;
  56. drv_data->ratnum.den_min = 1;
  57. drv_data->ratnum.den_max = 255;
  58. drv_data->rate_constraints.rats = &drv_data->ratnum;
  59. drv_data->rate_constraints.nrats = 1;
  60. }
  61. return 0;
  62. }
  63. static int xlnx_i2s_startup(struct snd_pcm_substream *substream,
  64. struct snd_soc_dai *dai)
  65. {
  66. struct xlnx_i2s_drv_data *drv_data = snd_soc_dai_get_drvdata(dai);
  67. if (drv_data->sysclk)
  68. return snd_pcm_hw_constraint_ratnums(substream->runtime, 0,
  69. SNDRV_PCM_HW_PARAM_RATE,
  70. &drv_data->rate_constraints);
  71. return 0;
  72. }
  73. static int xlnx_i2s_hw_params(struct snd_pcm_substream *substream,
  74. struct snd_pcm_hw_params *params,
  75. struct snd_soc_dai *i2s_dai)
  76. {
  77. u32 reg_off, chan_id;
  78. struct xlnx_i2s_drv_data *drv_data = snd_soc_dai_get_drvdata(i2s_dai);
  79. if (drv_data->sysclk) {
  80. unsigned int bits_per_sample, sclk, sclk_div;
  81. if (drv_data->is_32bit_lrclk)
  82. bits_per_sample = 32;
  83. else
  84. bits_per_sample = drv_data->data_width;
  85. sclk = params_rate(params) * bits_per_sample * params_channels(params);
  86. sclk_div = drv_data->sysclk / sclk / 2;
  87. if ((drv_data->sysclk % sclk != 0) ||
  88. !sclk_div || (sclk_div & ~I2S_I2STIM_VALID_MASK)) {
  89. dev_warn(i2s_dai->dev, "invalid SCLK divisor for sysclk %u and sclk %u\n",
  90. drv_data->sysclk, sclk);
  91. return -EINVAL;
  92. }
  93. writel(sclk_div, drv_data->base + I2S_I2STIM_OFFSET);
  94. }
  95. chan_id = params_channels(params) / 2;
  96. while (chan_id > 0) {
  97. reg_off = I2S_CH0_OFFSET + ((chan_id - 1) * 4);
  98. writel(chan_id, drv_data->base + reg_off);
  99. chan_id--;
  100. }
  101. return 0;
  102. }
  103. static int xlnx_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  104. struct snd_soc_dai *i2s_dai)
  105. {
  106. struct xlnx_i2s_drv_data *drv_data = snd_soc_dai_get_drvdata(i2s_dai);
  107. switch (cmd) {
  108. case SNDRV_PCM_TRIGGER_START:
  109. case SNDRV_PCM_TRIGGER_RESUME:
  110. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  111. writel(I2S_CORE_CTRL_ENABLE, drv_data->base + I2S_CORE_CTRL_OFFSET);
  112. break;
  113. case SNDRV_PCM_TRIGGER_STOP:
  114. case SNDRV_PCM_TRIGGER_SUSPEND:
  115. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  116. writel(0, drv_data->base + I2S_CORE_CTRL_OFFSET);
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. return 0;
  122. }
  123. static const struct snd_soc_dai_ops xlnx_i2s_dai_ops = {
  124. .trigger = xlnx_i2s_trigger,
  125. .set_sysclk = xlnx_i2s_set_sysclk,
  126. .set_clkdiv = xlnx_i2s_set_sclkout_div,
  127. .startup = xlnx_i2s_startup,
  128. .hw_params = xlnx_i2s_hw_params
  129. };
  130. static const struct snd_soc_component_driver xlnx_i2s_component = {
  131. .name = DRV_NAME,
  132. .legacy_dai_naming = 1,
  133. };
  134. static const struct of_device_id xlnx_i2s_of_match[] = {
  135. { .compatible = "xlnx,i2s-transmitter-1.0", },
  136. { .compatible = "xlnx,i2s-receiver-1.0", },
  137. {},
  138. };
  139. MODULE_DEVICE_TABLE(of, xlnx_i2s_of_match);
  140. static int xlnx_i2s_probe(struct platform_device *pdev)
  141. {
  142. struct xlnx_i2s_drv_data *drv_data;
  143. int ret;
  144. u32 format;
  145. struct device *dev = &pdev->dev;
  146. struct device_node *node = dev->of_node;
  147. drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
  148. if (!drv_data)
  149. return -ENOMEM;
  150. drv_data->base = devm_platform_ioremap_resource(pdev, 0);
  151. if (IS_ERR(drv_data->base))
  152. return PTR_ERR(drv_data->base);
  153. ret = of_property_read_u32(node, "xlnx,num-channels", &drv_data->channels);
  154. if (ret < 0) {
  155. dev_err(dev, "cannot get supported channels\n");
  156. return ret;
  157. }
  158. drv_data->channels *= 2;
  159. ret = of_property_read_u32(node, "xlnx,dwidth", &drv_data->data_width);
  160. if (ret < 0) {
  161. dev_err(dev, "cannot get data width\n");
  162. return ret;
  163. }
  164. switch (drv_data->data_width) {
  165. case 16:
  166. format = SNDRV_PCM_FMTBIT_S16_LE;
  167. break;
  168. case 24:
  169. format = SNDRV_PCM_FMTBIT_S24_LE;
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. if (of_device_is_compatible(node, "xlnx,i2s-transmitter-1.0")) {
  175. drv_data->dai_drv.name = "xlnx_i2s_playback";
  176. drv_data->dai_drv.playback.stream_name = "Playback";
  177. drv_data->dai_drv.playback.formats = format;
  178. drv_data->dai_drv.playback.channels_min = drv_data->channels;
  179. drv_data->dai_drv.playback.channels_max = drv_data->channels;
  180. drv_data->dai_drv.playback.rates = SNDRV_PCM_RATE_8000_192000;
  181. drv_data->dai_drv.ops = &xlnx_i2s_dai_ops;
  182. } else if (of_device_is_compatible(node, "xlnx,i2s-receiver-1.0")) {
  183. drv_data->dai_drv.name = "xlnx_i2s_capture";
  184. drv_data->dai_drv.capture.stream_name = "Capture";
  185. drv_data->dai_drv.capture.formats = format;
  186. drv_data->dai_drv.capture.channels_min = drv_data->channels;
  187. drv_data->dai_drv.capture.channels_max = drv_data->channels;
  188. drv_data->dai_drv.capture.rates = SNDRV_PCM_RATE_8000_192000;
  189. drv_data->dai_drv.ops = &xlnx_i2s_dai_ops;
  190. } else {
  191. return -ENODEV;
  192. }
  193. drv_data->is_32bit_lrclk = readl(drv_data->base + I2S_CORE_CTRL_OFFSET) &
  194. I2S_CORE_CTRL_32BIT_LRCLK;
  195. dev_set_drvdata(&pdev->dev, drv_data);
  196. ret = devm_snd_soc_register_component(&pdev->dev, &xlnx_i2s_component,
  197. &drv_data->dai_drv, 1);
  198. if (ret) {
  199. dev_err(&pdev->dev, "i2s component registration failed\n");
  200. return ret;
  201. }
  202. dev_info(&pdev->dev, "%s DAI registered\n", drv_data->dai_drv.name);
  203. return ret;
  204. }
  205. static struct platform_driver xlnx_i2s_aud_driver = {
  206. .driver = {
  207. .name = DRV_NAME,
  208. .of_match_table = xlnx_i2s_of_match,
  209. },
  210. .probe = xlnx_i2s_probe,
  211. };
  212. module_platform_driver(xlnx_i2s_aud_driver);
  213. MODULE_LICENSE("GPL v2");
  214. MODULE_AUTHOR("Praveen Vuppala <[email protected]>");
  215. MODULE_AUTHOR("Maruthi Srinivas Bayyavarapu <[email protected]>");