1 // SPDX-License-Identifier: GPL-2.0
3 // Xilinx ASoC I2S audio support
5 // Copyright (C) 2018 Xilinx, Inc.
7 // Author: Praveen Vuppala <praveenv@xilinx.com>
8 // Author: Maruthi Srinivas Bayyavarapu <maruthis@xilinx.com>
11 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <sound/pcm_params.h>
16 #include <sound/soc.h>
18 #define DRV_NAME "xlnx_i2s"
20 #define I2S_CORE_CTRL_OFFSET 0x08
21 #define I2S_CORE_CTRL_32BIT_LRCLK BIT(3)
22 #define I2S_CORE_CTRL_ENABLE BIT(0)
23 #define I2S_I2STIM_OFFSET 0x20
24 #define I2S_CH0_OFFSET 0x30
25 #define I2S_I2STIM_VALID_MASK GENMASK(7, 0)
27 struct xlnx_i2s_drv_data {
28 struct snd_soc_dai_driver dai_drv;
34 struct snd_ratnum ratnum;
35 struct snd_pcm_hw_constraint_ratnums rate_constraints;
38 static int xlnx_i2s_set_sclkout_div(struct snd_soc_dai *cpu_dai,
41 struct xlnx_i2s_drv_data *drv_data = snd_soc_dai_get_drvdata(cpu_dai);
43 if (!div || (div & ~I2S_I2STIM_VALID_MASK))
48 writel(div, drv_data->base + I2S_I2STIM_OFFSET);
53 static int xlnx_i2s_set_sysclk(struct snd_soc_dai *dai,
54 int clk_id, unsigned int freq, int dir)
56 struct xlnx_i2s_drv_data *drv_data = snd_soc_dai_get_drvdata(dai);
58 drv_data->sysclk = freq;
60 unsigned int bits_per_sample;
62 if (drv_data->is_32bit_lrclk)
65 bits_per_sample = drv_data->data_width;
67 drv_data->ratnum.num = freq / (bits_per_sample * drv_data->channels) / 2;
68 drv_data->ratnum.den_step = 1;
69 drv_data->ratnum.den_min = 1;
70 drv_data->ratnum.den_max = 255;
71 drv_data->rate_constraints.rats = &drv_data->ratnum;
72 drv_data->rate_constraints.nrats = 1;
77 static int xlnx_i2s_startup(struct snd_pcm_substream *substream,
78 struct snd_soc_dai *dai)
80 struct xlnx_i2s_drv_data *drv_data = snd_soc_dai_get_drvdata(dai);
83 return snd_pcm_hw_constraint_ratnums(substream->runtime, 0,
84 SNDRV_PCM_HW_PARAM_RATE,
85 &drv_data->rate_constraints);
90 static int xlnx_i2s_hw_params(struct snd_pcm_substream *substream,
91 struct snd_pcm_hw_params *params,
92 struct snd_soc_dai *i2s_dai)
95 struct xlnx_i2s_drv_data *drv_data = snd_soc_dai_get_drvdata(i2s_dai);
97 if (drv_data->sysclk) {
98 unsigned int bits_per_sample, sclk, sclk_div;
100 if (drv_data->is_32bit_lrclk)
101 bits_per_sample = 32;
103 bits_per_sample = drv_data->data_width;
105 sclk = params_rate(params) * bits_per_sample * params_channels(params);
106 sclk_div = drv_data->sysclk / sclk / 2;
108 if ((drv_data->sysclk % sclk != 0) ||
109 !sclk_div || (sclk_div & ~I2S_I2STIM_VALID_MASK)) {
110 dev_warn(i2s_dai->dev, "invalid SCLK divisor for sysclk %u and sclk %u\n",
111 drv_data->sysclk, sclk);
114 writel(sclk_div, drv_data->base + I2S_I2STIM_OFFSET);
117 chan_id = params_channels(params) / 2;
119 while (chan_id > 0) {
120 reg_off = I2S_CH0_OFFSET + ((chan_id - 1) * 4);
121 writel(chan_id, drv_data->base + reg_off);
128 static int xlnx_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
129 struct snd_soc_dai *i2s_dai)
131 struct xlnx_i2s_drv_data *drv_data = snd_soc_dai_get_drvdata(i2s_dai);
134 case SNDRV_PCM_TRIGGER_START:
135 case SNDRV_PCM_TRIGGER_RESUME:
136 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
137 writel(I2S_CORE_CTRL_ENABLE, drv_data->base + I2S_CORE_CTRL_OFFSET);
139 case SNDRV_PCM_TRIGGER_STOP:
140 case SNDRV_PCM_TRIGGER_SUSPEND:
141 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
142 writel(0, drv_data->base + I2S_CORE_CTRL_OFFSET);
151 static const struct snd_soc_dai_ops xlnx_i2s_dai_ops = {
152 .trigger = xlnx_i2s_trigger,
153 .set_sysclk = xlnx_i2s_set_sysclk,
154 .set_clkdiv = xlnx_i2s_set_sclkout_div,
155 .startup = xlnx_i2s_startup,
156 .hw_params = xlnx_i2s_hw_params
159 static const struct snd_soc_component_driver xlnx_i2s_component = {
163 static const struct of_device_id xlnx_i2s_of_match[] = {
164 { .compatible = "xlnx,i2s-transmitter-1.0", },
165 { .compatible = "xlnx,i2s-receiver-1.0", },
168 MODULE_DEVICE_TABLE(of, xlnx_i2s_of_match);
170 static int xlnx_i2s_probe(struct platform_device *pdev)
172 struct xlnx_i2s_drv_data *drv_data;
175 struct device *dev = &pdev->dev;
176 struct device_node *node = dev->of_node;
178 drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
182 drv_data->base = devm_platform_ioremap_resource(pdev, 0);
183 if (IS_ERR(drv_data->base))
184 return PTR_ERR(drv_data->base);
186 ret = of_property_read_u32(node, "xlnx,num-channels", &drv_data->channels);
188 dev_err(dev, "cannot get supported channels\n");
191 drv_data->channels *= 2;
193 ret = of_property_read_u32(node, "xlnx,dwidth", &drv_data->data_width);
195 dev_err(dev, "cannot get data width\n");
198 switch (drv_data->data_width) {
200 format = SNDRV_PCM_FMTBIT_S16_LE;
203 format = SNDRV_PCM_FMTBIT_S24_LE;
209 if (of_device_is_compatible(node, "xlnx,i2s-transmitter-1.0")) {
210 drv_data->dai_drv.name = "xlnx_i2s_playback";
211 drv_data->dai_drv.playback.stream_name = "Playback";
212 drv_data->dai_drv.playback.formats = format;
213 drv_data->dai_drv.playback.channels_min = drv_data->channels;
214 drv_data->dai_drv.playback.channels_max = drv_data->channels;
215 drv_data->dai_drv.playback.rates = SNDRV_PCM_RATE_8000_192000;
216 drv_data->dai_drv.ops = &xlnx_i2s_dai_ops;
217 } else if (of_device_is_compatible(node, "xlnx,i2s-receiver-1.0")) {
218 drv_data->dai_drv.name = "xlnx_i2s_capture";
219 drv_data->dai_drv.capture.stream_name = "Capture";
220 drv_data->dai_drv.capture.formats = format;
221 drv_data->dai_drv.capture.channels_min = drv_data->channels;
222 drv_data->dai_drv.capture.channels_max = drv_data->channels;
223 drv_data->dai_drv.capture.rates = SNDRV_PCM_RATE_8000_192000;
224 drv_data->dai_drv.ops = &xlnx_i2s_dai_ops;
228 drv_data->is_32bit_lrclk = readl(drv_data->base + I2S_CORE_CTRL_OFFSET) &
229 I2S_CORE_CTRL_32BIT_LRCLK;
231 dev_set_drvdata(&pdev->dev, drv_data);
233 ret = devm_snd_soc_register_component(&pdev->dev, &xlnx_i2s_component,
234 &drv_data->dai_drv, 1);
236 dev_err(&pdev->dev, "i2s component registration failed\n");
240 dev_info(&pdev->dev, "%s DAI registered\n", drv_data->dai_drv.name);
245 static struct platform_driver xlnx_i2s_aud_driver = {
248 .of_match_table = xlnx_i2s_of_match,
250 .probe = xlnx_i2s_probe,
253 module_platform_driver(xlnx_i2s_aud_driver);
255 MODULE_LICENSE("GPL v2");
256 MODULE_AUTHOR("Praveen Vuppala <praveenv@xilinx.com>");
257 MODULE_AUTHOR("Maruthi Srinivas Bayyavarapu <maruthis@xilinx.com>");