1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // linux/sound/bcm/bcm63xx-pcm-whistler.c
3 // BCM63xx whistler pcm interface
4 // Copyright (c) 2020 Broadcom Corporation
5 // Author: Kevin-Ke Li <kevin-ke.li@broadcom.com>
7 #include <linux/dma-mapping.h>
9 #include <linux/module.h>
10 #include <sound/pcm_params.h>
11 #include <linux/regmap.h>
12 #include <linux/of_device.h>
13 #include <sound/soc.h>
14 #include "bcm63xx-i2s.h"
18 unsigned char *dma_area;
23 struct bcm63xx_runtime_data {
26 dma_addr_t dma_addr_next;
29 static const struct snd_pcm_hardware bcm63xx_pcm_hardware = {
30 .info = SNDRV_PCM_INFO_MMAP |
31 SNDRV_PCM_INFO_MMAP_VALID |
32 SNDRV_PCM_INFO_INTERLEAVED |
33 SNDRV_PCM_INFO_PAUSE |
34 SNDRV_PCM_INFO_RESUME,
35 .formats = SNDRV_PCM_FMTBIT_S32_LE, /* support S32 only */
36 .period_bytes_max = 8192 - 32,
38 .periods_max = PAGE_SIZE/sizeof(struct i2s_dma_desc),
39 .buffer_bytes_max = 128 * 1024,
43 static int bcm63xx_pcm_hw_params(struct snd_soc_component *component,
44 struct snd_pcm_substream *substream,
45 struct snd_pcm_hw_params *params)
47 struct i2s_dma_desc *dma_desc;
48 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
49 struct snd_pcm_runtime *runtime = substream->runtime;
51 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
52 runtime->dma_bytes = params_buffer_bytes(params);
54 dma_desc = kzalloc(sizeof(*dma_desc), GFP_NOWAIT);
58 snd_soc_dai_set_dma_data(asoc_rtd_to_cpu(rtd, 0), substream, dma_desc);
63 static int bcm63xx_pcm_hw_free(struct snd_soc_component *component,
64 struct snd_pcm_substream *substream)
66 struct i2s_dma_desc *dma_desc;
67 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
69 dma_desc = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
71 snd_pcm_set_runtime_buffer(substream, NULL);
76 static int bcm63xx_pcm_trigger(struct snd_soc_component *component,
77 struct snd_pcm_substream *substream, int cmd)
80 struct snd_soc_pcm_runtime *rtd;
81 struct bcm_i2s_priv *i2s_priv;
82 struct regmap *regmap_i2s;
84 rtd = asoc_substream_to_rtd(substream);
85 i2s_priv = dev_get_drvdata(asoc_rtd_to_cpu(rtd, 0)->dev);
86 regmap_i2s = i2s_priv->regmap_i2s;
88 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
90 case SNDRV_PCM_TRIGGER_START:
91 regmap_update_bits(regmap_i2s,
93 I2S_TX_DESC_OFF_INTR_EN,
94 I2S_TX_DESC_OFF_INTR_EN);
95 regmap_update_bits(regmap_i2s,
100 case SNDRV_PCM_TRIGGER_STOP:
101 case SNDRV_PCM_TRIGGER_SUSPEND:
102 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
103 regmap_write(regmap_i2s,
106 regmap_update_bits(regmap_i2s,
116 case SNDRV_PCM_TRIGGER_START:
117 regmap_update_bits(regmap_i2s,
119 I2S_RX_DESC_OFF_INTR_EN_MSK,
120 I2S_RX_DESC_OFF_INTR_EN);
121 regmap_update_bits(regmap_i2s,
126 case SNDRV_PCM_TRIGGER_STOP:
127 case SNDRV_PCM_TRIGGER_SUSPEND:
128 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
129 regmap_update_bits(regmap_i2s,
131 I2S_RX_DESC_OFF_INTR_EN_MSK,
133 regmap_update_bits(regmap_i2s,
145 static int bcm63xx_pcm_prepare(struct snd_soc_component *component,
146 struct snd_pcm_substream *substream)
148 struct i2s_dma_desc *dma_desc;
149 struct regmap *regmap_i2s;
150 struct bcm_i2s_priv *i2s_priv;
151 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
152 struct snd_pcm_runtime *runtime = substream->runtime;
153 uint32_t regaddr_desclen, regaddr_descaddr;
155 dma_desc = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
156 dma_desc->dma_len = snd_pcm_lib_period_bytes(substream);
157 dma_desc->dma_addr = runtime->dma_addr;
158 dma_desc->dma_area = runtime->dma_area;
160 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
161 regaddr_desclen = I2S_TX_DESC_IFF_LEN;
162 regaddr_descaddr = I2S_TX_DESC_IFF_ADDR;
164 regaddr_desclen = I2S_RX_DESC_IFF_LEN;
165 regaddr_descaddr = I2S_RX_DESC_IFF_ADDR;
168 i2s_priv = dev_get_drvdata(asoc_rtd_to_cpu(rtd, 0)->dev);
169 regmap_i2s = i2s_priv->regmap_i2s;
171 regmap_write(regmap_i2s, regaddr_desclen, dma_desc->dma_len);
172 regmap_write(regmap_i2s, regaddr_descaddr, dma_desc->dma_addr);
177 static snd_pcm_uframes_t
178 bcm63xx_pcm_pointer(struct snd_soc_component *component,
179 struct snd_pcm_substream *substream)
182 struct bcm63xx_runtime_data *prtd = substream->runtime->private_data;
184 if (!prtd->dma_addr_next)
185 prtd->dma_addr_next = substream->runtime->dma_addr;
187 x = bytes_to_frames(substream->runtime,
188 prtd->dma_addr_next - substream->runtime->dma_addr);
190 return x == substream->runtime->buffer_size ? 0 : x;
193 static int bcm63xx_pcm_mmap(struct snd_soc_component *component,
194 struct snd_pcm_substream *substream,
195 struct vm_area_struct *vma)
197 struct snd_pcm_runtime *runtime = substream->runtime;
199 return dma_mmap_wc(substream->pcm->card->dev, vma,
206 static int bcm63xx_pcm_open(struct snd_soc_component *component,
207 struct snd_pcm_substream *substream)
210 struct snd_pcm_runtime *runtime = substream->runtime;
211 struct bcm63xx_runtime_data *prtd;
213 runtime->hw = bcm63xx_pcm_hardware;
214 ret = snd_pcm_hw_constraint_step(runtime, 0,
215 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
219 ret = snd_pcm_hw_constraint_step(runtime, 0,
220 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
224 ret = snd_pcm_hw_constraint_integer(runtime,
225 SNDRV_PCM_HW_PARAM_PERIODS);
230 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
234 runtime->private_data = prtd;
240 static int bcm63xx_pcm_close(struct snd_soc_component *component,
241 struct snd_pcm_substream *substream)
243 struct snd_pcm_runtime *runtime = substream->runtime;
244 struct bcm63xx_runtime_data *prtd = runtime->private_data;
250 static irqreturn_t i2s_dma_isr(int irq, void *bcm_i2s_priv)
252 unsigned int availdepth, ifflevel, offlevel, int_status, val_1, val_2;
253 struct bcm63xx_runtime_data *prtd;
254 struct snd_pcm_substream *substream;
255 struct snd_pcm_runtime *runtime;
256 struct regmap *regmap_i2s;
257 struct i2s_dma_desc *dma_desc;
258 struct snd_soc_pcm_runtime *rtd;
259 struct bcm_i2s_priv *i2s_priv;
261 i2s_priv = (struct bcm_i2s_priv *)bcm_i2s_priv;
262 regmap_i2s = i2s_priv->regmap_i2s;
265 regmap_read(regmap_i2s, I2S_RX_IRQ_CTL, &int_status);
267 if (int_status & I2S_RX_DESC_OFF_INTR_EN_MSK) {
268 substream = i2s_priv->capture_substream;
269 runtime = substream->runtime;
270 rtd = asoc_substream_to_rtd(substream);
271 prtd = runtime->private_data;
272 dma_desc = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
274 offlevel = (int_status & I2S_RX_DESC_OFF_LEVEL_MASK) >>
275 I2S_RX_DESC_OFF_LEVEL_SHIFT;
277 regmap_read(regmap_i2s, I2S_RX_DESC_OFF_ADDR, &val_1);
278 regmap_read(regmap_i2s, I2S_RX_DESC_OFF_LEN, &val_2);
281 prtd->dma_addr_next = val_1 + val_2;
282 ifflevel = (int_status & I2S_RX_DESC_IFF_LEVEL_MASK) >>
283 I2S_RX_DESC_IFF_LEVEL_SHIFT;
285 availdepth = I2S_DESC_FIFO_DEPTH - ifflevel;
287 dma_desc->dma_addr +=
288 snd_pcm_lib_period_bytes(substream);
289 dma_desc->dma_area +=
290 snd_pcm_lib_period_bytes(substream);
291 if (dma_desc->dma_addr - runtime->dma_addr >=
292 runtime->dma_bytes) {
293 dma_desc->dma_addr = runtime->dma_addr;
294 dma_desc->dma_area = runtime->dma_area;
297 prtd->dma_addr = dma_desc->dma_addr;
298 regmap_write(regmap_i2s, I2S_RX_DESC_IFF_LEN,
299 snd_pcm_lib_period_bytes(substream));
300 regmap_write(regmap_i2s, I2S_RX_DESC_IFF_ADDR,
305 snd_pcm_period_elapsed(substream);
307 /* Clear interrupt by writing 0 */
308 regmap_update_bits(regmap_i2s, I2S_RX_IRQ_CTL,
309 I2S_RX_INTR_MASK, 0);
313 regmap_read(regmap_i2s, I2S_TX_IRQ_CTL, &int_status);
315 if (int_status & I2S_TX_DESC_OFF_INTR_EN_MSK) {
316 substream = i2s_priv->play_substream;
317 runtime = substream->runtime;
318 rtd = asoc_substream_to_rtd(substream);
319 prtd = runtime->private_data;
320 dma_desc = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
322 offlevel = (int_status & I2S_TX_DESC_OFF_LEVEL_MASK) >>
323 I2S_TX_DESC_OFF_LEVEL_SHIFT;
325 regmap_read(regmap_i2s, I2S_TX_DESC_OFF_ADDR, &val_1);
326 regmap_read(regmap_i2s, I2S_TX_DESC_OFF_LEN, &val_2);
327 prtd->dma_addr_next = val_1 + val_2;
331 ifflevel = (int_status & I2S_TX_DESC_IFF_LEVEL_MASK) >>
332 I2S_TX_DESC_IFF_LEVEL_SHIFT;
333 availdepth = I2S_DESC_FIFO_DEPTH - ifflevel;
336 dma_desc->dma_addr +=
337 snd_pcm_lib_period_bytes(substream);
338 dma_desc->dma_area +=
339 snd_pcm_lib_period_bytes(substream);
341 if (dma_desc->dma_addr - runtime->dma_addr >=
342 runtime->dma_bytes) {
343 dma_desc->dma_addr = runtime->dma_addr;
344 dma_desc->dma_area = runtime->dma_area;
347 prtd->dma_addr = dma_desc->dma_addr;
348 regmap_write(regmap_i2s, I2S_TX_DESC_IFF_LEN,
349 snd_pcm_lib_period_bytes(substream));
350 regmap_write(regmap_i2s, I2S_TX_DESC_IFF_ADDR,
355 snd_pcm_period_elapsed(substream);
357 /* Clear interrupt by writing 0 */
358 regmap_update_bits(regmap_i2s, I2S_TX_IRQ_CTL,
359 I2S_TX_INTR_MASK, 0);
365 static int bcm63xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
367 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
368 struct snd_dma_buffer *buf = &substream->dma_buffer;
369 size_t size = bcm63xx_pcm_hardware.buffer_bytes_max;
371 buf->dev.type = SNDRV_DMA_TYPE_DEV;
372 buf->dev.dev = pcm->card->dev;
373 buf->private_data = NULL;
375 buf->area = dma_alloc_wc(pcm->card->dev,
384 static int bcm63xx_soc_pcm_new(struct snd_soc_component *component,
385 struct snd_soc_pcm_runtime *rtd)
387 struct snd_pcm *pcm = rtd->pcm;
388 struct bcm_i2s_priv *i2s_priv;
391 i2s_priv = dev_get_drvdata(asoc_rtd_to_cpu(rtd, 0)->dev);
393 of_dma_configure(pcm->card->dev, pcm->card->dev->of_node, 1);
395 ret = dma_coerce_mask_and_coherent(pcm->card->dev, DMA_BIT_MASK(32));
399 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
400 ret = bcm63xx_pcm_preallocate_dma_buffer(pcm,
401 SNDRV_PCM_STREAM_PLAYBACK);
405 i2s_priv->play_substream =
406 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
409 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
410 ret = bcm63xx_pcm_preallocate_dma_buffer(pcm,
411 SNDRV_PCM_STREAM_CAPTURE);
414 i2s_priv->capture_substream =
415 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
422 static void bcm63xx_pcm_free_dma_buffers(struct snd_soc_component *component,
426 struct snd_dma_buffer *buf;
427 struct snd_pcm_substream *substream;
429 for (stream = 0; stream < 2; stream++) {
430 substream = pcm->streams[stream].substream;
433 buf = &substream->dma_buffer;
436 dma_free_wc(pcm->card->dev, buf->bytes,
437 buf->area, buf->addr);
442 static const struct snd_soc_component_driver bcm63xx_soc_platform = {
443 .open = bcm63xx_pcm_open,
444 .close = bcm63xx_pcm_close,
445 .hw_params = bcm63xx_pcm_hw_params,
446 .hw_free = bcm63xx_pcm_hw_free,
447 .prepare = bcm63xx_pcm_prepare,
448 .trigger = bcm63xx_pcm_trigger,
449 .pointer = bcm63xx_pcm_pointer,
450 .mmap = bcm63xx_pcm_mmap,
451 .pcm_construct = bcm63xx_soc_pcm_new,
452 .pcm_destruct = bcm63xx_pcm_free_dma_buffers,
455 int bcm63xx_soc_platform_probe(struct platform_device *pdev,
456 struct bcm_i2s_priv *i2s_priv)
460 i2s_priv->r_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
461 if (!i2s_priv->r_irq) {
462 dev_err(&pdev->dev, "Unable to get register irq resource.\n");
466 ret = devm_request_irq(&pdev->dev, i2s_priv->r_irq->start, i2s_dma_isr,
467 i2s_priv->r_irq->flags, "i2s_dma", (void *)i2s_priv);
470 "i2s_init: failed to request interrupt.ret=%d\n", ret);
474 return devm_snd_soc_register_component(&pdev->dev,
475 &bcm63xx_soc_platform, NULL, 0);
478 int bcm63xx_soc_platform_remove(struct platform_device *pdev)
483 MODULE_AUTHOR("Kevin,Li <kevin-ke.li@broadcom.com>");
484 MODULE_DESCRIPTION("Broadcom DSL XPON ASOC PCM Interface");
485 MODULE_LICENSE("GPL v2");