1 // SPDX-License-Identifier: GPL-2.0-only
3 * tegra_pcm.c - Tegra PCM driver
5 * Author: Stephen Warren <swarren@nvidia.com>
6 * Copyright (C) 2010,2012 - NVIDIA, Inc.
8 * Based on code copyright/by:
10 * Copyright (c) 2009-2010, NVIDIA Corporation.
11 * Scott Peterson <speterson@nvidia.com>
12 * Vijay Mali <vmali@nvidia.com>
14 * Copyright (C) 2010 Google, Inc.
15 * Iliyan Malchev <malchev@google.com>
18 #include <linux/module.h>
19 #include <linux/dma-mapping.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include <sound/dmaengine_pcm.h>
25 #include "tegra_pcm.h"
27 static const struct snd_pcm_hardware tegra_pcm_hardware = {
28 .info = SNDRV_PCM_INFO_MMAP |
29 SNDRV_PCM_INFO_MMAP_VALID |
30 SNDRV_PCM_INFO_INTERLEAVED,
31 .period_bytes_min = 1024,
32 .period_bytes_max = PAGE_SIZE,
35 .buffer_bytes_max = PAGE_SIZE * 8,
39 static const struct snd_dmaengine_pcm_config tegra_dmaengine_pcm_config = {
40 .pcm_hardware = &tegra_pcm_hardware,
41 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
42 .prealloc_buffer_size = PAGE_SIZE * 8,
45 int tegra_pcm_platform_register(struct device *dev)
47 return snd_dmaengine_pcm_register(dev, &tegra_dmaengine_pcm_config, 0);
49 EXPORT_SYMBOL_GPL(tegra_pcm_platform_register);
51 int tegra_pcm_platform_register_with_chan_names(struct device *dev,
52 struct snd_dmaengine_pcm_config *config,
53 char *txdmachan, char *rxdmachan)
55 *config = tegra_dmaengine_pcm_config;
56 config->dma_dev = dev->parent;
57 config->chan_names[0] = txdmachan;
58 config->chan_names[1] = rxdmachan;
60 return snd_dmaengine_pcm_register(dev, config, 0);
62 EXPORT_SYMBOL_GPL(tegra_pcm_platform_register_with_chan_names);
64 void tegra_pcm_platform_unregister(struct device *dev)
66 return snd_dmaengine_pcm_unregister(dev);
68 EXPORT_SYMBOL_GPL(tegra_pcm_platform_unregister);
70 int tegra_pcm_open(struct snd_soc_component *component,
71 struct snd_pcm_substream *substream)
73 struct snd_soc_pcm_runtime *rtd = substream->private_data;
74 struct snd_dmaengine_dai_dma_data *dmap;
75 struct dma_chan *chan;
76 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
79 if (rtd->dai_link->no_pcm)
82 dmap = snd_soc_dai_get_dma_data(cpu_dai, substream);
84 /* Set HW params now that initialization is complete */
85 snd_soc_set_runtime_hwparams(substream, &tegra_pcm_hardware);
87 /* Ensure period size is multiple of 8 */
88 ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
89 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 0x8);
91 dev_err(rtd->dev, "failed to set constraint %d\n", ret);
95 chan = dma_request_slave_channel(cpu_dai->dev, dmap->chan_name);
98 "dmaengine request slave channel failed! (%s)\n",
103 ret = snd_dmaengine_pcm_open(substream, chan);
106 "dmaengine pcm open failed with err %d (%s)\n", ret,
109 dma_release_channel(chan);
116 EXPORT_SYMBOL_GPL(tegra_pcm_open);
118 int tegra_pcm_close(struct snd_soc_component *component,
119 struct snd_pcm_substream *substream)
121 struct snd_soc_pcm_runtime *rtd = substream->private_data;
123 if (rtd->dai_link->no_pcm)
126 snd_dmaengine_pcm_close_release_chan(substream);
130 EXPORT_SYMBOL_GPL(tegra_pcm_close);
132 int tegra_pcm_hw_params(struct snd_soc_component *component,
133 struct snd_pcm_substream *substream,
134 struct snd_pcm_hw_params *params)
136 struct snd_soc_pcm_runtime *rtd = substream->private_data;
137 struct snd_dmaengine_dai_dma_data *dmap;
138 struct dma_slave_config slave_config;
139 struct dma_chan *chan;
142 if (rtd->dai_link->no_pcm)
145 dmap = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
149 chan = snd_dmaengine_pcm_get_chan(substream);
151 ret = snd_hwparams_to_dma_slave_config(substream, params,
154 dev_err(rtd->dev, "hw params config failed with err %d\n", ret);
158 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
159 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
160 slave_config.dst_addr = dmap->addr;
161 slave_config.dst_maxburst = 8;
163 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
164 slave_config.src_addr = dmap->addr;
165 slave_config.src_maxburst = 8;
168 ret = dmaengine_slave_config(chan, &slave_config);
170 dev_err(rtd->dev, "dma slave config failed with err %d\n", ret);
174 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
178 EXPORT_SYMBOL_GPL(tegra_pcm_hw_params);
180 int tegra_pcm_hw_free(struct snd_soc_component *component,
181 struct snd_pcm_substream *substream)
183 struct snd_soc_pcm_runtime *rtd = substream->private_data;
185 if (rtd->dai_link->no_pcm)
188 snd_pcm_set_runtime_buffer(substream, NULL);
192 EXPORT_SYMBOL_GPL(tegra_pcm_hw_free);
194 int tegra_pcm_mmap(struct snd_soc_component *component,
195 struct snd_pcm_substream *substream,
196 struct vm_area_struct *vma)
198 struct snd_soc_pcm_runtime *rtd = substream->private_data;
199 struct snd_pcm_runtime *runtime = substream->runtime;
201 if (rtd->dai_link->no_pcm)
204 return dma_mmap_wc(substream->pcm->card->dev, vma, runtime->dma_area,
205 runtime->dma_addr, runtime->dma_bytes);
207 EXPORT_SYMBOL_GPL(tegra_pcm_mmap);
209 snd_pcm_uframes_t tegra_pcm_pointer(struct snd_soc_component *component,
210 struct snd_pcm_substream *substream)
212 return snd_dmaengine_pcm_pointer(substream);
214 EXPORT_SYMBOL_GPL(tegra_pcm_pointer);
216 static int tegra_pcm_preallocate_dma_buffer(struct device *dev, struct snd_pcm *pcm, int stream,
219 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
220 struct snd_dma_buffer *buf = &substream->dma_buffer;
222 buf->area = dma_alloc_wc(dev, size, &buf->addr, GFP_KERNEL);
226 buf->private_data = NULL;
227 buf->dev.type = SNDRV_DMA_TYPE_DEV;
234 static void tegra_pcm_deallocate_dma_buffer(struct snd_pcm *pcm, int stream)
236 struct snd_pcm_substream *substream;
237 struct snd_dma_buffer *buf;
239 substream = pcm->streams[stream].substream;
243 buf = &substream->dma_buffer;
247 dma_free_wc(buf->dev.dev, buf->bytes, buf->area, buf->addr);
251 static int tegra_pcm_dma_allocate(struct device *dev, struct snd_soc_pcm_runtime *rtd,
254 struct snd_pcm *pcm = rtd->pcm;
257 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
261 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
262 ret = tegra_pcm_preallocate_dma_buffer(dev, pcm, SNDRV_PCM_STREAM_PLAYBACK, size);
267 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
268 ret = tegra_pcm_preallocate_dma_buffer(dev, pcm, SNDRV_PCM_STREAM_CAPTURE, size);
276 tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
281 int tegra_pcm_construct(struct snd_soc_component *component,
282 struct snd_soc_pcm_runtime *rtd)
284 struct device *dev = component->dev;
287 * Fallback for backwards-compatibility with older device trees that
288 * have the iommus property in the virtual, top-level "sound" node.
290 if (!of_get_property(dev->of_node, "iommus", NULL))
291 dev = rtd->card->snd_card->dev;
293 return tegra_pcm_dma_allocate(dev, rtd, tegra_pcm_hardware.buffer_bytes_max);
295 EXPORT_SYMBOL_GPL(tegra_pcm_construct);
297 void tegra_pcm_destruct(struct snd_soc_component *component,
300 tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_CAPTURE);
301 tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
303 EXPORT_SYMBOL_GPL(tegra_pcm_destruct);
305 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
306 MODULE_DESCRIPTION("Tegra PCM ASoC driver");
307 MODULE_LICENSE("GPL");