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