blob: 56a541b9ff9e2340eabc996c803a9b3a7427853c [file] [log] [blame]
Lars-Peter Clausen28c44682013-04-15 19:19:50 +02001/*
2 * Copyright (C) 2013, Analog Devices Inc.
3 * Author: Lars-Peter Clausen <lars@metafoo.de>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
13 *
14 */
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/dmaengine.h>
18#include <linux/slab.h>
19#include <sound/pcm.h>
20#include <sound/pcm_params.h>
21#include <sound/soc.h>
22#include <linux/dma-mapping.h>
23#include <linux/of.h>
Lars-Peter Clausen28c44682013-04-15 19:19:50 +020024
25#include <sound/dmaengine_pcm.h>
26
Lars-Peter Clausenacde50a2015-04-27 12:44:25 +020027/*
28 * The platforms dmaengine driver does not support reporting the amount of
29 * bytes that are still left to transfer.
30 */
31#define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
32
Lars-Peter Clausen28c44682013-04-15 19:19:50 +020033struct dmaengine_pcm {
Takashi Iwaif82bf8e2013-10-25 18:06:09 +020034 struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];
Lars-Peter Clausen28c44682013-04-15 19:19:50 +020035 const struct snd_dmaengine_pcm_config *config;
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +000036 struct snd_soc_component component;
Lars-Peter Clausend1e14062013-04-20 19:29:00 +020037 unsigned int flags;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +020038};
39
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +000040static struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p)
Lars-Peter Clausen28c44682013-04-15 19:19:50 +020041{
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +000042 return container_of(p, struct dmaengine_pcm, component);
Lars-Peter Clausen28c44682013-04-15 19:19:50 +020043}
44
Lars-Peter Clausenc0de42b2013-10-08 15:07:59 +020045static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
46 struct snd_pcm_substream *substream)
47{
48 if (!pcm->chan[substream->stream])
49 return NULL;
50
51 return pcm->chan[substream->stream]->device->dev;
52}
53
Lars-Peter Clausen28c44682013-04-15 19:19:50 +020054/**
55 * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback
56 * @substream: PCM substream
57 * @params: hw_params
58 * @slave_config: DMA slave config to prepare
59 *
60 * This function can be used as a generic prepare_slave_config callback for
61 * platforms which make use of the snd_dmaengine_dai_dma_data struct for their
62 * DAI DMA data. Internally the function will first call
63 * snd_hwparams_to_dma_slave_config to fill in the slave config based on the
64 * hw_params, followed by snd_dmaengine_set_config_from_dai_data to fill in the
65 * remaining fields based on the DAI DMA data.
66 */
67int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
68 struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
69{
70 struct snd_soc_pcm_runtime *rtd = substream->private_data;
71 struct snd_dmaengine_dai_dma_data *dma_data;
72 int ret;
73
74 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
75
76 ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
77 if (ret)
78 return ret;
79
80 snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
81 slave_config);
82
83 return 0;
84}
85EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
86
87static int dmaengine_pcm_hw_params(struct snd_pcm_substream *substream,
88 struct snd_pcm_hw_params *params)
89{
90 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +000091 struct snd_soc_component *component =
92 snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
93 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
Lars-Peter Clausen28c44682013-04-15 19:19:50 +020094 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
Lars-Peter Clausenfa654e02013-10-08 15:08:00 +020095 int (*prepare_slave_config)(struct snd_pcm_substream *substream,
96 struct snd_pcm_hw_params *params,
97 struct dma_slave_config *slave_config);
Lars-Peter Clausen28c44682013-04-15 19:19:50 +020098 struct dma_slave_config slave_config;
99 int ret;
100
Lee Jonesa894bd72013-11-06 10:16:20 +0000101 memset(&slave_config, 0, sizeof(slave_config));
102
Lars-Peter Clausenfa654e02013-10-08 15:08:00 +0200103 if (!pcm->config)
104 prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
105 else
106 prepare_slave_config = pcm->config->prepare_slave_config;
107
108 if (prepare_slave_config) {
109 ret = prepare_slave_config(substream, params, &slave_config);
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200110 if (ret)
111 return ret;
112
113 ret = dmaengine_slave_config(chan, &slave_config);
114 if (ret)
115 return ret;
116 }
117
118 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
119}
120
Lars-Peter Clausenc0de42b2013-10-08 15:07:59 +0200121static int dmaengine_pcm_set_runtime_hwparams(struct snd_pcm_substream *substream)
122{
123 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000124 struct snd_soc_component *component =
125 snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
126 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
Lars-Peter Clausenc0de42b2013-10-08 15:07:59 +0200127 struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
128 struct dma_chan *chan = pcm->chan[substream->stream];
129 struct snd_dmaengine_dai_dma_data *dma_data;
130 struct dma_slave_caps dma_caps;
131 struct snd_pcm_hardware hw;
Peter Ujfalusi2d38df12014-07-03 07:51:54 +0300132 u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
133 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
134 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
Fabio Estevam7ed310b2018-02-22 16:02:22 -0300135 snd_pcm_format_t i;
136 int ret;
Lars-Peter Clausenc0de42b2013-10-08 15:07:59 +0200137
Lars-Peter Clausenfa654e02013-10-08 15:08:00 +0200138 if (pcm->config && pcm->config->pcm_hardware)
Lars-Peter Clausenc0de42b2013-10-08 15:07:59 +0200139 return snd_soc_set_runtime_hwparams(substream,
140 pcm->config->pcm_hardware);
141
142 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
143
144 memset(&hw, 0, sizeof(hw));
145 hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
146 SNDRV_PCM_INFO_INTERLEAVED;
147 hw.periods_min = 2;
148 hw.periods_max = UINT_MAX;
149 hw.period_bytes_min = 256;
150 hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
151 hw.buffer_bytes_max = SIZE_MAX;
152 hw.fifo_size = dma_data->fifo_size;
153
Lars-Peter Clausena22f33b2013-11-30 18:00:45 +0100154 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
155 hw.info |= SNDRV_PCM_INFO_BATCH;
156
Lars-Peter Clausenc0de42b2013-10-08 15:07:59 +0200157 ret = dma_get_slave_caps(chan, &dma_caps);
158 if (ret == 0) {
159 if (dma_caps.cmd_pause)
160 hw.info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
Lars-Peter Clausen478028e2014-01-11 14:02:19 +0100161 if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
162 hw.info |= SNDRV_PCM_INFO_BATCH;
Peter Ujfalusi2d38df12014-07-03 07:51:54 +0300163
164 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Maxime Ripardceacbdb2014-11-17 14:41:57 +0100165 addr_widths = dma_caps.dst_addr_widths;
Peter Ujfalusi2d38df12014-07-03 07:51:54 +0300166 else
167 addr_widths = dma_caps.src_addr_widths;
168 }
169
170 /*
Matthias Reichl73fe01c2016-04-27 15:26:51 +0200171 * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep
172 * hw.formats set to 0, meaning no restrictions are in place.
173 * In this case it's the responsibility of the DAI driver to
174 * provide the supported format information.
Peter Ujfalusi2d38df12014-07-03 07:51:54 +0300175 */
Matthias Reichl73fe01c2016-04-27 15:26:51 +0200176 if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
177 /*
178 * Prepare formats mask for valid/allowed sample types. If the
179 * dma does not have support for the given physical word size,
180 * it needs to be masked out so user space can not use the
181 * format which produces corrupted audio.
182 * In case the dma driver does not implement the slave_caps the
183 * default assumption is that it supports 1, 2 and 4 bytes
184 * widths.
185 */
Fabio Estevam7ed310b2018-02-22 16:02:22 -0300186 for (i = SNDRV_PCM_FORMAT_FIRST; i <= SNDRV_PCM_FORMAT_LAST; i++) {
Matthias Reichl73fe01c2016-04-27 15:26:51 +0200187 int bits = snd_pcm_format_physical_width(i);
Peter Ujfalusi2d38df12014-07-03 07:51:54 +0300188
Matthias Reichl73fe01c2016-04-27 15:26:51 +0200189 /*
190 * Enable only samples with DMA supported physical
191 * widths
192 */
193 switch (bits) {
194 case 8:
195 case 16:
196 case 24:
197 case 32:
198 case 64:
199 if (addr_widths & (1 << (bits / 8)))
200 hw.formats |= (1LL << i);
201 break;
202 default:
203 /* Unsupported types */
204 break;
205 }
Peter Ujfalusi2d38df12014-07-03 07:51:54 +0300206 }
Lars-Peter Clausenc0de42b2013-10-08 15:07:59 +0200207
208 return snd_soc_set_runtime_hwparams(substream, &hw);
209}
210
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200211static int dmaengine_pcm_open(struct snd_pcm_substream *substream)
212{
213 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000214 struct snd_soc_component *component =
215 snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
216 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200217 struct dma_chan *chan = pcm->chan[substream->stream];
218 int ret;
219
Lars-Peter Clausenc0de42b2013-10-08 15:07:59 +0200220 ret = dmaengine_pcm_set_runtime_hwparams(substream);
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200221 if (ret)
222 return ret;
223
224 return snd_dmaengine_pcm_open(substream, chan);
225}
226
Lars-Peter Clausenc9998362013-04-15 19:19:51 +0200227static struct dma_chan *dmaengine_pcm_compat_request_channel(
228 struct snd_soc_pcm_runtime *rtd,
229 struct snd_pcm_substream *substream)
230{
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000231 struct snd_soc_component *component =
232 snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
233 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
Mark Brown90130d22013-10-19 21:38:26 +0100234 struct snd_dmaengine_dai_dma_data *dma_data;
Xiubo Liec4f2852014-01-16 16:08:04 +0800235 dma_filter_fn fn = NULL;
Mark Brown90130d22013-10-19 21:38:26 +0100236
237 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Lars-Peter Clausenc9998362013-04-15 19:19:51 +0200238
Lars-Peter Clausend1e14062013-04-20 19:29:00 +0200239 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
240 return pcm->chan[0];
241
Xiubo Liec4f2852014-01-16 16:08:04 +0800242 if (pcm->config && pcm->config->compat_request_channel)
Lars-Peter Clausenc9998362013-04-15 19:19:51 +0200243 return pcm->config->compat_request_channel(rtd, substream);
244
Xiubo Liec4f2852014-01-16 16:08:04 +0800245 if (pcm->config)
246 fn = pcm->config->compat_filter_fn;
247
248 return snd_dmaengine_pcm_request_channel(fn, dma_data->filter_data);
Lars-Peter Clausenc9998362013-04-15 19:19:51 +0200249}
250
Lars-Peter Clausenacde50a2015-04-27 12:44:25 +0200251static bool dmaengine_pcm_can_report_residue(struct device *dev,
252 struct dma_chan *chan)
Lars-Peter Clausen478028e2014-01-11 14:02:19 +0100253{
254 struct dma_slave_caps dma_caps;
255 int ret;
256
257 ret = dma_get_slave_caps(chan, &dma_caps);
Lars-Peter Clausenacde50a2015-04-27 12:44:25 +0200258 if (ret != 0) {
259 dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
260 ret);
261 return false;
262 }
Lars-Peter Clausen478028e2014-01-11 14:02:19 +0100263
264 if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
265 return false;
266
267 return true;
268}
269
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200270static int dmaengine_pcm_new(struct snd_soc_pcm_runtime *rtd)
271{
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000272 struct snd_soc_component *component =
273 snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
274 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200275 const struct snd_dmaengine_pcm_config *config = pcm->config;
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000276 struct device *dev = component->dev;
Sylwester Nawrocki9bfa24e2017-01-17 14:16:41 +0100277 struct snd_dmaengine_dai_dma_data *dma_data;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200278 struct snd_pcm_substream *substream;
Lars-Peter Clausenfa654e02013-10-08 15:08:00 +0200279 size_t prealloc_buffer_size;
280 size_t max_buffer_size;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200281 unsigned int i;
282 int ret;
283
Lars-Peter Clausenfa654e02013-10-08 15:08:00 +0200284 if (config && config->prealloc_buffer_size) {
285 prealloc_buffer_size = config->prealloc_buffer_size;
286 max_buffer_size = config->pcm_hardware->buffer_bytes_max;
287 } else {
288 prealloc_buffer_size = 512 * 1024;
289 max_buffer_size = SIZE_MAX;
290 }
291
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200292 for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
293 substream = rtd->pcm->streams[i].substream;
294 if (!substream)
295 continue;
296
Sylwester Nawrocki9bfa24e2017-01-17 14:16:41 +0100297 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
298
299 if (!pcm->chan[i] &&
300 (pcm->flags & SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME))
301 pcm->chan[i] = dma_request_slave_channel(dev,
302 dma_data->chan_name);
303
Lars-Peter Clausend1e14062013-04-20 19:29:00 +0200304 if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
Lars-Peter Clausenc9998362013-04-15 19:19:51 +0200305 pcm->chan[i] = dmaengine_pcm_compat_request_channel(rtd,
306 substream);
307 }
308
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200309 if (!pcm->chan[i]) {
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000310 dev_err(component->dev,
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200311 "Missing dma channel for stream: %d\n", i);
Lars-Peter Clausende7621e2015-01-02 13:56:07 +0100312 return -EINVAL;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200313 }
314
315 ret = snd_pcm_lib_preallocate_pages(substream,
Nicolin Chenca2b0292013-11-07 14:45:16 +0800316 SNDRV_DMA_TYPE_DEV_IRAM,
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200317 dmaengine_dma_dev(pcm, substream),
Lars-Peter Clausenfa654e02013-10-08 15:08:00 +0200318 prealloc_buffer_size,
319 max_buffer_size);
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200320 if (ret)
Lars-Peter Clausende7621e2015-01-02 13:56:07 +0100321 return ret;
Lars-Peter Clausen478028e2014-01-11 14:02:19 +0100322
Lars-Peter Clausenacde50a2015-04-27 12:44:25 +0200323 if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
Lars-Peter Clausen478028e2014-01-11 14:02:19 +0100324 pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200325 }
326
327 return 0;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200328}
329
Lars-Peter Clausen93b943e2014-01-11 14:02:18 +0100330static snd_pcm_uframes_t dmaengine_pcm_pointer(
331 struct snd_pcm_substream *substream)
332{
333 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000334 struct snd_soc_component *component =
335 snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
336 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
Lars-Peter Clausen93b943e2014-01-11 14:02:18 +0100337
338 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
339 return snd_dmaengine_pcm_pointer_no_residue(substream);
340 else
341 return snd_dmaengine_pcm_pointer(substream);
342}
343
Olivier Moysan78648092018-02-19 16:00:36 +0100344static int dmaengine_copy_user(struct snd_pcm_substream *substream,
345 int channel, unsigned long hwoff,
346 void *buf, unsigned long bytes)
347{
348 struct snd_soc_pcm_runtime *rtd = substream->private_data;
349 struct snd_soc_component *component =
350 snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
351 struct snd_pcm_runtime *runtime = substream->runtime;
352 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
353 int (*process)(struct snd_pcm_substream *substream,
354 int channel, unsigned long hwoff,
355 void *buf, unsigned long bytes) = pcm->config->process;
356 bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
357 void *dma_ptr = runtime->dma_area + hwoff +
358 channel * (runtime->dma_bytes / runtime->channels);
359 int ret;
360
361 if (is_playback)
362 if (copy_from_user(dma_ptr, (void __user *)buf, bytes))
363 return -EFAULT;
364
365 if (process) {
366 ret = process(substream, channel, hwoff,
367 (void __user *)buf, bytes);
368 if (ret < 0)
369 return ret;
370 }
371
372 if (!is_playback)
373 if (copy_to_user((void __user *)buf, dma_ptr, bytes))
374 return -EFAULT;
375
376 return 0;
377}
378
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200379static const struct snd_pcm_ops dmaengine_pcm_ops = {
380 .open = dmaengine_pcm_open,
381 .close = snd_dmaengine_pcm_close,
382 .ioctl = snd_pcm_lib_ioctl,
383 .hw_params = dmaengine_pcm_hw_params,
384 .hw_free = snd_pcm_lib_free_pages,
385 .trigger = snd_dmaengine_pcm_trigger,
Lars-Peter Clausen93b943e2014-01-11 14:02:18 +0100386 .pointer = dmaengine_pcm_pointer,
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200387};
388
Olivier Moysan78648092018-02-19 16:00:36 +0100389static const struct snd_pcm_ops dmaengine_pcm_process_ops = {
390 .open = dmaengine_pcm_open,
391 .close = snd_dmaengine_pcm_close,
392 .ioctl = snd_pcm_lib_ioctl,
393 .hw_params = dmaengine_pcm_hw_params,
394 .hw_free = snd_pcm_lib_free_pages,
395 .trigger = snd_dmaengine_pcm_trigger,
396 .pointer = dmaengine_pcm_pointer,
397 .copy_user = dmaengine_copy_user,
398};
399
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000400static const struct snd_soc_component_driver dmaengine_pcm_component = {
401 .name = SND_DMAENGINE_PCM_DRV_NAME,
402 .probe_order = SND_SOC_COMP_ORDER_LATE,
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200403 .ops = &dmaengine_pcm_ops,
404 .pcm_new = dmaengine_pcm_new,
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200405};
406
Olivier Moysan78648092018-02-19 16:00:36 +0100407static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
408 .name = SND_DMAENGINE_PCM_DRV_NAME,
409 .probe_order = SND_SOC_COMP_ORDER_LATE,
410 .ops = &dmaengine_pcm_process_ops,
411 .pcm_new = dmaengine_pcm_new,
412};
413
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200414static const char * const dmaengine_pcm_dma_channel_names[] = {
415 [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
416 [SNDRV_PCM_STREAM_CAPTURE] = "rx",
417};
418
Stephen Warren5eda87b2013-12-10 11:11:02 -0700419static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
Stephen Warren194c7de2013-12-03 14:26:34 -0700420 struct device *dev, const struct snd_dmaengine_pcm_config *config)
Lars-Peter Clausend1e14062013-04-20 19:29:00 +0200421{
422 unsigned int i;
Stephen Warren11b3a7a2013-12-03 14:26:32 -0700423 const char *name;
Stephen Warren5eda87b2013-12-10 11:11:02 -0700424 struct dma_chan *chan;
Lars-Peter Clausend1e14062013-04-20 19:29:00 +0200425
Sylwester Nawrocki9bfa24e2017-01-17 14:16:41 +0100426 if ((pcm->flags & (SND_DMAENGINE_PCM_FLAG_NO_DT |
427 SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME)) ||
428 !dev->of_node)
Stephen Warren5eda87b2013-12-10 11:11:02 -0700429 return 0;
Lars-Peter Clausend1e14062013-04-20 19:29:00 +0200430
Xiubo Li2b67f8b2013-12-17 15:16:40 +0800431 if (config && config->dma_dev) {
Stephen Warren194c7de2013-12-03 14:26:34 -0700432 /*
433 * If this warning is seen, it probably means that your Linux
434 * device structure does not match your HW device structure.
435 * It would be best to refactor the Linux device structure to
436 * correctly match the HW structure.
437 */
438 dev_warn(dev, "DMA channels sourced from device %s",
439 dev_name(config->dma_dev));
440 dev = config->dma_dev;
441 }
442
Stephen Warren11b3a7a2013-12-03 14:26:32 -0700443 for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
444 i++) {
445 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
446 name = "rx-tx";
447 else
448 name = dmaengine_pcm_dma_channel_names[i];
Xiubo Li2b67f8b2013-12-17 15:16:40 +0800449 if (config && config->chan_names[i])
Stephen Warren194c7de2013-12-03 14:26:34 -0700450 name = config->chan_names[i];
Stephen Warren5eda87b2013-12-10 11:11:02 -0700451 chan = dma_request_slave_channel_reason(dev, name);
452 if (IS_ERR(chan)) {
Stephen Warrene9036c22013-12-11 11:20:50 -0700453 if (PTR_ERR(chan) == -EPROBE_DEFER)
Stephen Warren5eda87b2013-12-10 11:11:02 -0700454 return -EPROBE_DEFER;
455 pcm->chan[i] = NULL;
456 } else {
457 pcm->chan[i] = chan;
458 }
Stephen Warren11b3a7a2013-12-03 14:26:32 -0700459 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
460 break;
Lars-Peter Clausend1e14062013-04-20 19:29:00 +0200461 }
Stephen Warren11b3a7a2013-12-03 14:26:32 -0700462
463 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
464 pcm->chan[1] = pcm->chan[0];
Stephen Warren5eda87b2013-12-10 11:11:02 -0700465
466 return 0;
Lars-Peter Clausend1e14062013-04-20 19:29:00 +0200467}
468
Stephen Warren6b9f3e62013-12-03 14:26:33 -0700469static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
470{
471 unsigned int i;
472
473 for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
474 i++) {
475 if (!pcm->chan[i])
476 continue;
477 dma_release_channel(pcm->chan[i]);
478 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
479 break;
480 }
481}
482
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200483/**
484 * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
485 * @dev: The parent device for the PCM device
486 * @config: Platform specific PCM configuration
487 * @flags: Platform specific quirks
488 */
489int snd_dmaengine_pcm_register(struct device *dev,
490 const struct snd_dmaengine_pcm_config *config, unsigned int flags)
491{
492 struct dmaengine_pcm *pcm;
Stephen Warren6b9f3e62013-12-03 14:26:33 -0700493 int ret;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200494
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200495 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
496 if (!pcm)
497 return -ENOMEM;
498
Fabio Estevamf0b3bdb2018-02-21 14:57:33 -0300499#ifdef CONFIG_DEBUG_FS
500 pcm->component.debugfs_prefix = "dma";
501#endif
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200502 pcm->config = config;
Lars-Peter Clausend1e14062013-04-20 19:29:00 +0200503 pcm->flags = flags;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200504
Stephen Warren5eda87b2013-12-10 11:11:02 -0700505 ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
506 if (ret)
Fabio Estevamb84acf42018-02-26 15:55:25 -0300507 goto err_free_dma;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200508
Olivier Moysan78648092018-02-19 16:00:36 +0100509 if (config && config->process)
510 ret = snd_soc_add_component(dev, &pcm->component,
511 &dmaengine_pcm_component_process,
512 NULL, 0);
513 else
514 ret = snd_soc_add_component(dev, &pcm->component,
515 &dmaengine_pcm_component, NULL, 0);
Stephen Warren6b9f3e62013-12-03 14:26:33 -0700516 if (ret)
517 goto err_free_dma;
518
519 return 0;
520
521err_free_dma:
522 dmaengine_pcm_release_chan(pcm);
523 kfree(pcm);
524 return ret;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200525}
526EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
527
528/**
529 * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
530 * @dev: Parent device the PCM was register with
531 *
532 * Removes a dmaengine based PCM device previously registered with
533 * snd_dmaengine_pcm_register.
534 */
535void snd_dmaengine_pcm_unregister(struct device *dev)
536{
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000537 struct snd_soc_component *component;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200538 struct dmaengine_pcm *pcm;
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200539
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000540 component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);
541 if (!component)
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200542 return;
543
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000544 pcm = soc_component_to_pcm(component);
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200545
Kuninori Morimotobe7ee5f2018-01-29 02:41:09 +0000546 snd_soc_unregister_component(dev);
Stephen Warren6b9f3e62013-12-03 14:26:33 -0700547 dmaengine_pcm_release_chan(pcm);
Lars-Peter Clausen28c44682013-04-15 19:19:50 +0200548 kfree(pcm);
549}
550EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
551
552MODULE_LICENSE("GPL");