xref: /openbmc/linux/sound/soc/adi/axi-i2s.c (revision ae213c44)
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 
8 #include <linux/clk.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/slab.h>
16 
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/dmaengine_pcm.h>
22 
23 #define AXI_I2S_REG_RESET	0x00
24 #define AXI_I2S_REG_CTRL	0x04
25 #define AXI_I2S_REG_CLK_CTRL	0x08
26 #define AXI_I2S_REG_STATUS	0x10
27 
28 #define AXI_I2S_REG_RX_FIFO	0x28
29 #define AXI_I2S_REG_TX_FIFO	0x2C
30 
31 #define AXI_I2S_RESET_GLOBAL	BIT(0)
32 #define AXI_I2S_RESET_TX_FIFO	BIT(1)
33 #define AXI_I2S_RESET_RX_FIFO	BIT(2)
34 
35 #define AXI_I2S_CTRL_TX_EN	BIT(0)
36 #define AXI_I2S_CTRL_RX_EN	BIT(1)
37 
38 /* The frame size is configurable, but for now we always set it 64 bit */
39 #define AXI_I2S_BITS_PER_FRAME 64
40 
41 struct axi_i2s {
42 	struct regmap *regmap;
43 	struct clk *clk;
44 	struct clk *clk_ref;
45 
46 	bool   has_capture;
47 	bool   has_playback;
48 
49 	struct snd_soc_dai_driver dai_driver;
50 
51 	struct snd_dmaengine_dai_dma_data capture_dma_data;
52 	struct snd_dmaengine_dai_dma_data playback_dma_data;
53 
54 	struct snd_ratnum ratnum;
55 	struct snd_pcm_hw_constraint_ratnums rate_constraints;
56 };
57 
58 static int axi_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
59 	struct snd_soc_dai *dai)
60 {
61 	struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
62 	unsigned int mask, val;
63 
64 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
65 		mask = AXI_I2S_CTRL_RX_EN;
66 	else
67 		mask = AXI_I2S_CTRL_TX_EN;
68 
69 	switch (cmd) {
70 	case SNDRV_PCM_TRIGGER_START:
71 	case SNDRV_PCM_TRIGGER_RESUME:
72 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
73 		val = mask;
74 		break;
75 	case SNDRV_PCM_TRIGGER_STOP:
76 	case SNDRV_PCM_TRIGGER_SUSPEND:
77 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
78 		val = 0;
79 		break;
80 	default:
81 		return -EINVAL;
82 	}
83 
84 	regmap_update_bits(i2s->regmap, AXI_I2S_REG_CTRL, mask, val);
85 
86 	return 0;
87 }
88 
89 static int axi_i2s_hw_params(struct snd_pcm_substream *substream,
90 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
91 {
92 	struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
93 	unsigned int bclk_div, word_size;
94 	unsigned int bclk_rate;
95 
96 	bclk_rate = params_rate(params) * AXI_I2S_BITS_PER_FRAME;
97 
98 	word_size = AXI_I2S_BITS_PER_FRAME / 2 - 1;
99 	bclk_div = DIV_ROUND_UP(clk_get_rate(i2s->clk_ref), bclk_rate) / 2 - 1;
100 
101 	regmap_write(i2s->regmap, AXI_I2S_REG_CLK_CTRL, (word_size << 16) |
102 		bclk_div);
103 
104 	return 0;
105 }
106 
107 static int axi_i2s_startup(struct snd_pcm_substream *substream,
108 	struct snd_soc_dai *dai)
109 {
110 	struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
111 	uint32_t mask;
112 	int ret;
113 
114 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
115 		mask = AXI_I2S_RESET_RX_FIFO;
116 	else
117 		mask = AXI_I2S_RESET_TX_FIFO;
118 
119 	regmap_write(i2s->regmap, AXI_I2S_REG_RESET, mask);
120 
121 	ret = snd_pcm_hw_constraint_ratnums(substream->runtime, 0,
122 			   SNDRV_PCM_HW_PARAM_RATE,
123 			   &i2s->rate_constraints);
124 	if (ret)
125 		return ret;
126 
127 	return clk_prepare_enable(i2s->clk_ref);
128 }
129 
130 static void axi_i2s_shutdown(struct snd_pcm_substream *substream,
131 	struct snd_soc_dai *dai)
132 {
133 	struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
134 
135 	clk_disable_unprepare(i2s->clk_ref);
136 }
137 
138 static int axi_i2s_dai_probe(struct snd_soc_dai *dai)
139 {
140 	struct axi_i2s *i2s = snd_soc_dai_get_drvdata(dai);
141 
142 	snd_soc_dai_init_dma_data(
143 		dai,
144 		i2s->has_playback ? &i2s->playback_dma_data : NULL,
145 		i2s->has_capture  ? &i2s->capture_dma_data  : NULL);
146 
147 	return 0;
148 }
149 
150 static const struct snd_soc_dai_ops axi_i2s_dai_ops = {
151 	.startup = axi_i2s_startup,
152 	.shutdown = axi_i2s_shutdown,
153 	.trigger = axi_i2s_trigger,
154 	.hw_params = axi_i2s_hw_params,
155 };
156 
157 static struct snd_soc_dai_driver axi_i2s_dai = {
158 	.probe = axi_i2s_dai_probe,
159 	.ops = &axi_i2s_dai_ops,
160 	.symmetric_rates = 1,
161 };
162 
163 static const struct snd_soc_component_driver axi_i2s_component = {
164 	.name = "axi-i2s",
165 };
166 
167 static const struct regmap_config axi_i2s_regmap_config = {
168 	.reg_bits = 32,
169 	.reg_stride = 4,
170 	.val_bits = 32,
171 	.max_register = AXI_I2S_REG_STATUS,
172 };
173 
174 static void axi_i2s_parse_of(struct axi_i2s *i2s, const struct device_node *np)
175 {
176 	struct property *dma_names;
177 	const char *dma_name;
178 
179 	of_property_for_each_string(np, "dma-names", dma_names, dma_name) {
180 		if (strcmp(dma_name, "rx") == 0)
181 			i2s->has_capture = true;
182 		if (strcmp(dma_name, "tx") == 0)
183 			i2s->has_playback = true;
184 	}
185 }
186 
187 static int axi_i2s_probe(struct platform_device *pdev)
188 {
189 	struct resource *res;
190 	struct axi_i2s *i2s;
191 	void __iomem *base;
192 	int ret;
193 
194 	i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
195 	if (!i2s)
196 		return -ENOMEM;
197 
198 	platform_set_drvdata(pdev, i2s);
199 
200 	axi_i2s_parse_of(i2s, pdev->dev.of_node);
201 
202 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
203 	base = devm_ioremap_resource(&pdev->dev, res);
204 	if (IS_ERR(base))
205 		return PTR_ERR(base);
206 
207 	i2s->regmap = devm_regmap_init_mmio(&pdev->dev, base,
208 		&axi_i2s_regmap_config);
209 	if (IS_ERR(i2s->regmap))
210 		return PTR_ERR(i2s->regmap);
211 
212 	i2s->clk = devm_clk_get(&pdev->dev, "axi");
213 	if (IS_ERR(i2s->clk))
214 		return PTR_ERR(i2s->clk);
215 
216 	i2s->clk_ref = devm_clk_get(&pdev->dev, "ref");
217 	if (IS_ERR(i2s->clk_ref))
218 		return PTR_ERR(i2s->clk_ref);
219 
220 	ret = clk_prepare_enable(i2s->clk);
221 	if (ret)
222 		return ret;
223 
224 	if (i2s->has_playback) {
225 		axi_i2s_dai.playback.channels_min = 2;
226 		axi_i2s_dai.playback.channels_max = 2;
227 		axi_i2s_dai.playback.rates = SNDRV_PCM_RATE_KNOT;
228 		axi_i2s_dai.playback.formats =
229 			SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_U32_LE;
230 
231 		i2s->playback_dma_data.addr = res->start + AXI_I2S_REG_TX_FIFO;
232 		i2s->playback_dma_data.addr_width = 4;
233 		i2s->playback_dma_data.maxburst = 1;
234 	}
235 
236 	if (i2s->has_capture) {
237 		axi_i2s_dai.capture.channels_min = 2;
238 		axi_i2s_dai.capture.channels_max = 2;
239 		axi_i2s_dai.capture.rates = SNDRV_PCM_RATE_KNOT;
240 		axi_i2s_dai.capture.formats =
241 			SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_U32_LE;
242 
243 		i2s->capture_dma_data.addr = res->start + AXI_I2S_REG_RX_FIFO;
244 		i2s->capture_dma_data.addr_width = 4;
245 		i2s->capture_dma_data.maxburst = 1;
246 	}
247 
248 	i2s->ratnum.num = clk_get_rate(i2s->clk_ref) / 2 / AXI_I2S_BITS_PER_FRAME;
249 	i2s->ratnum.den_step = 1;
250 	i2s->ratnum.den_min = 1;
251 	i2s->ratnum.den_max = 64;
252 
253 	i2s->rate_constraints.rats = &i2s->ratnum;
254 	i2s->rate_constraints.nrats = 1;
255 
256 	regmap_write(i2s->regmap, AXI_I2S_REG_RESET, AXI_I2S_RESET_GLOBAL);
257 
258 	ret = devm_snd_soc_register_component(&pdev->dev, &axi_i2s_component,
259 					 &axi_i2s_dai, 1);
260 	if (ret)
261 		goto err_clk_disable;
262 
263 	ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
264 	if (ret)
265 		goto err_clk_disable;
266 
267 	dev_info(&pdev->dev, "probed, capture %s, playback %s\n",
268 		 i2s->has_capture ? "enabled" : "disabled",
269 		 i2s->has_playback ? "enabled" : "disabled");
270 
271 	return 0;
272 
273 err_clk_disable:
274 	clk_disable_unprepare(i2s->clk);
275 	return ret;
276 }
277 
278 static int axi_i2s_dev_remove(struct platform_device *pdev)
279 {
280 	struct axi_i2s *i2s = platform_get_drvdata(pdev);
281 
282 	clk_disable_unprepare(i2s->clk);
283 
284 	return 0;
285 }
286 
287 static const struct of_device_id axi_i2s_of_match[] = {
288 	{ .compatible = "adi,axi-i2s-1.00.a", },
289 	{},
290 };
291 MODULE_DEVICE_TABLE(of, axi_i2s_of_match);
292 
293 static struct platform_driver axi_i2s_driver = {
294 	.driver = {
295 		.name = "axi-i2s",
296 		.of_match_table = axi_i2s_of_match,
297 	},
298 	.probe = axi_i2s_probe,
299 	.remove = axi_i2s_dev_remove,
300 };
301 module_platform_driver(axi_i2s_driver);
302 
303 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
304 MODULE_DESCRIPTION("AXI I2S driver");
305 MODULE_LICENSE("GPL");
306