Kuninori Morimoto | 1356a60 | 2018-07-02 06:25:11 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // Copyright (C) 2013, Analog Devices Inc. |
| 4 | // Author: Lars-Peter Clausen <lars@metafoo.de> |
| 5 | |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 6 | #include <linux/module.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/dmaengine.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <sound/pcm.h> |
| 11 | #include <sound/pcm_params.h> |
| 12 | #include <sound/soc.h> |
| 13 | #include <linux/dma-mapping.h> |
| 14 | #include <linux/of.h> |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 15 | |
| 16 | #include <sound/dmaengine_pcm.h> |
| 17 | |
Lars-Peter Clausen | acde50a | 2015-04-27 12:44:25 +0200 | [diff] [blame] | 18 | /* |
| 19 | * The platforms dmaengine driver does not support reporting the amount of |
| 20 | * bytes that are still left to transfer. |
| 21 | */ |
| 22 | #define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31) |
| 23 | |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 24 | struct dmaengine_pcm { |
Takashi Iwai | f82bf8e | 2013-10-25 18:06:09 +0200 | [diff] [blame] | 25 | struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1]; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 26 | const struct snd_dmaengine_pcm_config *config; |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 27 | struct snd_soc_component component; |
Lars-Peter Clausen | d1e1406 | 2013-04-20 19:29:00 +0200 | [diff] [blame] | 28 | unsigned int flags; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 31 | static struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p) |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 32 | { |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 33 | return container_of(p, struct dmaengine_pcm, component); |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 34 | } |
| 35 | |
Lars-Peter Clausen | c0de42b | 2013-10-08 15:07:59 +0200 | [diff] [blame] | 36 | static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm, |
| 37 | struct snd_pcm_substream *substream) |
| 38 | { |
| 39 | if (!pcm->chan[substream->stream]) |
| 40 | return NULL; |
| 41 | |
| 42 | return pcm->chan[substream->stream]->device->dev; |
| 43 | } |
| 44 | |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 45 | /** |
| 46 | * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback |
| 47 | * @substream: PCM substream |
| 48 | * @params: hw_params |
| 49 | * @slave_config: DMA slave config to prepare |
| 50 | * |
| 51 | * This function can be used as a generic prepare_slave_config callback for |
| 52 | * platforms which make use of the snd_dmaengine_dai_dma_data struct for their |
| 53 | * DAI DMA data. Internally the function will first call |
| 54 | * snd_hwparams_to_dma_slave_config to fill in the slave config based on the |
| 55 | * hw_params, followed by snd_dmaengine_set_config_from_dai_data to fill in the |
| 56 | * remaining fields based on the DAI DMA data. |
| 57 | */ |
| 58 | int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream, |
| 59 | struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config) |
| 60 | { |
| 61 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 62 | struct snd_dmaengine_dai_dma_data *dma_data; |
| 63 | int ret; |
| 64 | |
Bard Liao | 6e1276a | 2020-02-25 21:39:16 +0800 | [diff] [blame] | 65 | if (rtd->num_cpus > 1) { |
| 66 | dev_err(rtd->dev, |
| 67 | "%s doesn't support Multi CPU yet\n", __func__); |
| 68 | return -EINVAL; |
| 69 | } |
| 70 | |
Kuninori Morimoto | c2233a2 | 2020-03-30 10:47:37 +0900 | [diff] [blame] | 71 | dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream); |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 72 | |
| 73 | ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config); |
| 74 | if (ret) |
| 75 | return ret; |
| 76 | |
| 77 | snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data, |
| 78 | slave_config); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config); |
| 83 | |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 84 | static int dmaengine_pcm_hw_params(struct snd_soc_component *component, |
| 85 | struct snd_pcm_substream *substream, |
| 86 | struct snd_pcm_hw_params *params) |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 87 | { |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 88 | struct dmaengine_pcm *pcm = soc_component_to_pcm(component); |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 89 | struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream); |
Lars-Peter Clausen | fa654e0 | 2013-10-08 15:08:00 +0200 | [diff] [blame] | 90 | int (*prepare_slave_config)(struct snd_pcm_substream *substream, |
| 91 | struct snd_pcm_hw_params *params, |
| 92 | struct dma_slave_config *slave_config); |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 93 | struct dma_slave_config slave_config; |
| 94 | int ret; |
| 95 | |
Lee Jones | a894bd7 | 2013-11-06 10:16:20 +0000 | [diff] [blame] | 96 | memset(&slave_config, 0, sizeof(slave_config)); |
| 97 | |
Lars-Peter Clausen | fa654e0 | 2013-10-08 15:08:00 +0200 | [diff] [blame] | 98 | if (!pcm->config) |
| 99 | prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config; |
| 100 | else |
| 101 | prepare_slave_config = pcm->config->prepare_slave_config; |
| 102 | |
| 103 | if (prepare_slave_config) { |
| 104 | ret = prepare_slave_config(substream, params, &slave_config); |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 105 | if (ret) |
| 106 | return ret; |
| 107 | |
| 108 | ret = dmaengine_slave_config(chan, &slave_config); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | } |
| 112 | |
Takashi Iwai | d708c2b | 2019-12-10 15:26:01 +0100 | [diff] [blame] | 113 | return 0; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 116 | static int |
| 117 | dmaengine_pcm_set_runtime_hwparams(struct snd_soc_component *component, |
| 118 | struct snd_pcm_substream *substream) |
Lars-Peter Clausen | c0de42b | 2013-10-08 15:07:59 +0200 | [diff] [blame] | 119 | { |
| 120 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 121 | struct dmaengine_pcm *pcm = soc_component_to_pcm(component); |
Lars-Peter Clausen | c0de42b | 2013-10-08 15:07:59 +0200 | [diff] [blame] | 122 | struct device *dma_dev = dmaengine_dma_dev(pcm, substream); |
| 123 | struct dma_chan *chan = pcm->chan[substream->stream]; |
| 124 | struct snd_dmaengine_dai_dma_data *dma_data; |
Lars-Peter Clausen | c0de42b | 2013-10-08 15:07:59 +0200 | [diff] [blame] | 125 | struct snd_pcm_hardware hw; |
Lars-Peter Clausen | c0de42b | 2013-10-08 15:07:59 +0200 | [diff] [blame] | 126 | |
Bard Liao | 6e1276a | 2020-02-25 21:39:16 +0800 | [diff] [blame] | 127 | if (rtd->num_cpus > 1) { |
| 128 | dev_err(rtd->dev, |
| 129 | "%s doesn't support Multi CPU yet\n", __func__); |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
Lars-Peter Clausen | fa654e0 | 2013-10-08 15:08:00 +0200 | [diff] [blame] | 133 | if (pcm->config && pcm->config->pcm_hardware) |
Lars-Peter Clausen | c0de42b | 2013-10-08 15:07:59 +0200 | [diff] [blame] | 134 | return snd_soc_set_runtime_hwparams(substream, |
| 135 | pcm->config->pcm_hardware); |
| 136 | |
Kuninori Morimoto | c2233a2 | 2020-03-30 10:47:37 +0900 | [diff] [blame] | 137 | dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream); |
Lars-Peter Clausen | c0de42b | 2013-10-08 15:07:59 +0200 | [diff] [blame] | 138 | |
| 139 | memset(&hw, 0, sizeof(hw)); |
| 140 | hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | |
| 141 | SNDRV_PCM_INFO_INTERLEAVED; |
| 142 | hw.periods_min = 2; |
| 143 | hw.periods_max = UINT_MAX; |
| 144 | hw.period_bytes_min = 256; |
| 145 | hw.period_bytes_max = dma_get_max_seg_size(dma_dev); |
| 146 | hw.buffer_bytes_max = SIZE_MAX; |
| 147 | hw.fifo_size = dma_data->fifo_size; |
| 148 | |
Lars-Peter Clausen | a22f33b | 2013-11-30 18:00:45 +0100 | [diff] [blame] | 149 | if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE) |
| 150 | hw.info |= SNDRV_PCM_INFO_BATCH; |
| 151 | |
Shengjiu Wang | 1301280 | 2020-01-20 15:28:06 +0800 | [diff] [blame] | 152 | /** |
| 153 | * FIXME: Remove the return value check to align with the code |
| 154 | * before adding snd_dmaengine_pcm_refine_runtime_hwparams |
| 155 | * function. |
| 156 | */ |
| 157 | snd_dmaengine_pcm_refine_runtime_hwparams(substream, |
| 158 | dma_data, |
| 159 | &hw, |
| 160 | chan); |
Lars-Peter Clausen | c0de42b | 2013-10-08 15:07:59 +0200 | [diff] [blame] | 161 | |
| 162 | return snd_soc_set_runtime_hwparams(substream, &hw); |
| 163 | } |
| 164 | |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 165 | static int dmaengine_pcm_open(struct snd_soc_component *component, |
| 166 | struct snd_pcm_substream *substream) |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 167 | { |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 168 | struct dmaengine_pcm *pcm = soc_component_to_pcm(component); |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 169 | struct dma_chan *chan = pcm->chan[substream->stream]; |
| 170 | int ret; |
| 171 | |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 172 | ret = dmaengine_pcm_set_runtime_hwparams(component, substream); |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 173 | if (ret) |
| 174 | return ret; |
| 175 | |
| 176 | return snd_dmaengine_pcm_open(substream, chan); |
| 177 | } |
| 178 | |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 179 | static int dmaengine_pcm_close(struct snd_soc_component *component, |
| 180 | struct snd_pcm_substream *substream) |
| 181 | { |
| 182 | return snd_dmaengine_pcm_close(substream); |
| 183 | } |
| 184 | |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 185 | static int dmaengine_pcm_trigger(struct snd_soc_component *component, |
| 186 | struct snd_pcm_substream *substream, int cmd) |
| 187 | { |
| 188 | return snd_dmaengine_pcm_trigger(substream, cmd); |
| 189 | } |
| 190 | |
Lars-Peter Clausen | c999836 | 2013-04-15 19:19:51 +0200 | [diff] [blame] | 191 | static struct dma_chan *dmaengine_pcm_compat_request_channel( |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 192 | struct snd_soc_component *component, |
Lars-Peter Clausen | c999836 | 2013-04-15 19:19:51 +0200 | [diff] [blame] | 193 | struct snd_soc_pcm_runtime *rtd, |
| 194 | struct snd_pcm_substream *substream) |
| 195 | { |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 196 | struct dmaengine_pcm *pcm = soc_component_to_pcm(component); |
Mark Brown | 90130d2 | 2013-10-19 21:38:26 +0100 | [diff] [blame] | 197 | struct snd_dmaengine_dai_dma_data *dma_data; |
Xiubo Li | ec4f285 | 2014-01-16 16:08:04 +0800 | [diff] [blame] | 198 | dma_filter_fn fn = NULL; |
Mark Brown | 90130d2 | 2013-10-19 21:38:26 +0100 | [diff] [blame] | 199 | |
Bard Liao | 6e1276a | 2020-02-25 21:39:16 +0800 | [diff] [blame] | 200 | if (rtd->num_cpus > 1) { |
| 201 | dev_err(rtd->dev, |
| 202 | "%s doesn't support Multi CPU yet\n", __func__); |
| 203 | return NULL; |
| 204 | } |
| 205 | |
Kuninori Morimoto | c2233a2 | 2020-03-30 10:47:37 +0900 | [diff] [blame] | 206 | dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream); |
Lars-Peter Clausen | c999836 | 2013-04-15 19:19:51 +0200 | [diff] [blame] | 207 | |
Lars-Peter Clausen | d1e1406 | 2013-04-20 19:29:00 +0200 | [diff] [blame] | 208 | if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0]) |
| 209 | return pcm->chan[0]; |
| 210 | |
Xiubo Li | ec4f285 | 2014-01-16 16:08:04 +0800 | [diff] [blame] | 211 | if (pcm->config && pcm->config->compat_request_channel) |
Lars-Peter Clausen | c999836 | 2013-04-15 19:19:51 +0200 | [diff] [blame] | 212 | return pcm->config->compat_request_channel(rtd, substream); |
| 213 | |
Xiubo Li | ec4f285 | 2014-01-16 16:08:04 +0800 | [diff] [blame] | 214 | if (pcm->config) |
| 215 | fn = pcm->config->compat_filter_fn; |
| 216 | |
| 217 | return snd_dmaengine_pcm_request_channel(fn, dma_data->filter_data); |
Lars-Peter Clausen | c999836 | 2013-04-15 19:19:51 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Lars-Peter Clausen | acde50a | 2015-04-27 12:44:25 +0200 | [diff] [blame] | 220 | static bool dmaengine_pcm_can_report_residue(struct device *dev, |
| 221 | struct dma_chan *chan) |
Lars-Peter Clausen | 478028e | 2014-01-11 14:02:19 +0100 | [diff] [blame] | 222 | { |
| 223 | struct dma_slave_caps dma_caps; |
| 224 | int ret; |
| 225 | |
| 226 | ret = dma_get_slave_caps(chan, &dma_caps); |
Lars-Peter Clausen | acde50a | 2015-04-27 12:44:25 +0200 | [diff] [blame] | 227 | if (ret != 0) { |
| 228 | dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n", |
| 229 | ret); |
| 230 | return false; |
| 231 | } |
Lars-Peter Clausen | 478028e | 2014-01-11 14:02:19 +0100 | [diff] [blame] | 232 | |
| 233 | if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR) |
| 234 | return false; |
| 235 | |
| 236 | return true; |
| 237 | } |
| 238 | |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 239 | static int dmaengine_pcm_new(struct snd_soc_component *component, |
| 240 | struct snd_soc_pcm_runtime *rtd) |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 241 | { |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 242 | struct dmaengine_pcm *pcm = soc_component_to_pcm(component); |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 243 | const struct snd_dmaengine_pcm_config *config = pcm->config; |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 244 | struct device *dev = component->dev; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 245 | struct snd_pcm_substream *substream; |
Lars-Peter Clausen | fa654e0 | 2013-10-08 15:08:00 +0200 | [diff] [blame] | 246 | size_t prealloc_buffer_size; |
| 247 | size_t max_buffer_size; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 248 | unsigned int i; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 249 | |
Lars-Peter Clausen | fa654e0 | 2013-10-08 15:08:00 +0200 | [diff] [blame] | 250 | if (config && config->prealloc_buffer_size) { |
| 251 | prealloc_buffer_size = config->prealloc_buffer_size; |
| 252 | max_buffer_size = config->pcm_hardware->buffer_bytes_max; |
| 253 | } else { |
| 254 | prealloc_buffer_size = 512 * 1024; |
| 255 | max_buffer_size = SIZE_MAX; |
| 256 | } |
| 257 | |
Kuninori Morimoto | ee10fbe | 2020-02-17 17:28:32 +0900 | [diff] [blame] | 258 | for_each_pcm_streams(i) { |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 259 | substream = rtd->pcm->streams[i].substream; |
| 260 | if (!substream) |
| 261 | continue; |
| 262 | |
Sylwester Nawrocki | 76d9c68 | 2019-02-14 16:45:55 +0100 | [diff] [blame] | 263 | if (!pcm->chan[i] && config && config->chan_names[i]) |
Sylwester Nawrocki | 9bfa24e | 2017-01-17 14:16:41 +0100 | [diff] [blame] | 264 | pcm->chan[i] = dma_request_slave_channel(dev, |
Sylwester Nawrocki | 76d9c68 | 2019-02-14 16:45:55 +0100 | [diff] [blame] | 265 | config->chan_names[i]); |
Sylwester Nawrocki | 9bfa24e | 2017-01-17 14:16:41 +0100 | [diff] [blame] | 266 | |
Lars-Peter Clausen | d1e1406 | 2013-04-20 19:29:00 +0200 | [diff] [blame] | 267 | if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) { |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 268 | pcm->chan[i] = dmaengine_pcm_compat_request_channel( |
| 269 | component, rtd, substream); |
Lars-Peter Clausen | c999836 | 2013-04-15 19:19:51 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 272 | if (!pcm->chan[i]) { |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 273 | dev_err(component->dev, |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 274 | "Missing dma channel for stream: %d\n", i); |
Lars-Peter Clausen | de7621e | 2015-01-02 13:56:07 +0100 | [diff] [blame] | 275 | return -EINVAL; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Takashi Iwai | d708c2b | 2019-12-10 15:26:01 +0100 | [diff] [blame] | 278 | snd_pcm_set_managed_buffer(substream, |
Nicolin Chen | ca2b029 | 2013-11-07 14:45:16 +0800 | [diff] [blame] | 279 | SNDRV_DMA_TYPE_DEV_IRAM, |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 280 | dmaengine_dma_dev(pcm, substream), |
Lars-Peter Clausen | fa654e0 | 2013-10-08 15:08:00 +0200 | [diff] [blame] | 281 | prealloc_buffer_size, |
| 282 | max_buffer_size); |
Lars-Peter Clausen | 478028e | 2014-01-11 14:02:19 +0100 | [diff] [blame] | 283 | |
Lars-Peter Clausen | acde50a | 2015-04-27 12:44:25 +0200 | [diff] [blame] | 284 | if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i])) |
Lars-Peter Clausen | 478028e | 2014-01-11 14:02:19 +0100 | [diff] [blame] | 285 | pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE; |
Peter Ujfalusi | 2ec42f3 | 2019-09-06 08:55:24 +0300 | [diff] [blame] | 286 | |
| 287 | if (rtd->pcm->streams[i].pcm->name[0] == '\0') { |
Peter Ujfalusi | 48118a9 | 2019-09-11 11:33:31 +0300 | [diff] [blame] | 288 | strscpy_pad(rtd->pcm->streams[i].pcm->name, |
| 289 | rtd->pcm->streams[i].pcm->id, |
| 290 | sizeof(rtd->pcm->streams[i].pcm->name)); |
Peter Ujfalusi | 2ec42f3 | 2019-09-06 08:55:24 +0300 | [diff] [blame] | 291 | } |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | return 0; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Lars-Peter Clausen | 93b943e | 2014-01-11 14:02:18 +0100 | [diff] [blame] | 297 | static snd_pcm_uframes_t dmaengine_pcm_pointer( |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 298 | struct snd_soc_component *component, |
Lars-Peter Clausen | 93b943e | 2014-01-11 14:02:18 +0100 | [diff] [blame] | 299 | struct snd_pcm_substream *substream) |
| 300 | { |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 301 | struct dmaengine_pcm *pcm = soc_component_to_pcm(component); |
Lars-Peter Clausen | 93b943e | 2014-01-11 14:02:18 +0100 | [diff] [blame] | 302 | |
| 303 | if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE) |
| 304 | return snd_dmaengine_pcm_pointer_no_residue(substream); |
| 305 | else |
| 306 | return snd_dmaengine_pcm_pointer(substream); |
| 307 | } |
| 308 | |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 309 | static int dmaengine_copy_user(struct snd_soc_component *component, |
| 310 | struct snd_pcm_substream *substream, |
Olivier Moysan | 7864809 | 2018-02-19 16:00:36 +0100 | [diff] [blame] | 311 | int channel, unsigned long hwoff, |
Takashi Iwai | 40d1299 | 2018-07-25 22:42:08 +0200 | [diff] [blame] | 312 | void __user *buf, unsigned long bytes) |
Olivier Moysan | 7864809 | 2018-02-19 16:00:36 +0100 | [diff] [blame] | 313 | { |
Olivier Moysan | 7864809 | 2018-02-19 16:00:36 +0100 | [diff] [blame] | 314 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 315 | struct dmaengine_pcm *pcm = soc_component_to_pcm(component); |
| 316 | int (*process)(struct snd_pcm_substream *substream, |
| 317 | int channel, unsigned long hwoff, |
| 318 | void *buf, unsigned long bytes) = pcm->config->process; |
| 319 | bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 320 | void *dma_ptr = runtime->dma_area + hwoff + |
| 321 | channel * (runtime->dma_bytes / runtime->channels); |
| 322 | int ret; |
| 323 | |
| 324 | if (is_playback) |
Takashi Iwai | 40d1299 | 2018-07-25 22:42:08 +0200 | [diff] [blame] | 325 | if (copy_from_user(dma_ptr, buf, bytes)) |
Olivier Moysan | 7864809 | 2018-02-19 16:00:36 +0100 | [diff] [blame] | 326 | return -EFAULT; |
| 327 | |
| 328 | if (process) { |
Takashi Iwai | 40d1299 | 2018-07-25 22:42:08 +0200 | [diff] [blame] | 329 | ret = process(substream, channel, hwoff, (__force void *)buf, bytes); |
Olivier Moysan | 7864809 | 2018-02-19 16:00:36 +0100 | [diff] [blame] | 330 | if (ret < 0) |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | if (!is_playback) |
Takashi Iwai | 40d1299 | 2018-07-25 22:42:08 +0200 | [diff] [blame] | 335 | if (copy_to_user(buf, dma_ptr, bytes)) |
Olivier Moysan | 7864809 | 2018-02-19 16:00:36 +0100 | [diff] [blame] | 336 | return -EFAULT; |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 341 | static const struct snd_soc_component_driver dmaengine_pcm_component = { |
| 342 | .name = SND_DMAENGINE_PCM_DRV_NAME, |
| 343 | .probe_order = SND_SOC_COMP_ORDER_LATE, |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 344 | .open = dmaengine_pcm_open, |
| 345 | .close = dmaengine_pcm_close, |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 346 | .hw_params = dmaengine_pcm_hw_params, |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 347 | .trigger = dmaengine_pcm_trigger, |
| 348 | .pointer = dmaengine_pcm_pointer, |
| 349 | .pcm_construct = dmaengine_pcm_new, |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 350 | }; |
| 351 | |
Olivier Moysan | 7864809 | 2018-02-19 16:00:36 +0100 | [diff] [blame] | 352 | static const struct snd_soc_component_driver dmaengine_pcm_component_process = { |
| 353 | .name = SND_DMAENGINE_PCM_DRV_NAME, |
| 354 | .probe_order = SND_SOC_COMP_ORDER_LATE, |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 355 | .open = dmaengine_pcm_open, |
| 356 | .close = dmaengine_pcm_close, |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 357 | .hw_params = dmaengine_pcm_hw_params, |
Kuninori Morimoto | ece2317 | 2019-10-02 14:35:00 +0900 | [diff] [blame] | 358 | .trigger = dmaengine_pcm_trigger, |
| 359 | .pointer = dmaengine_pcm_pointer, |
| 360 | .copy_user = dmaengine_copy_user, |
| 361 | .pcm_construct = dmaengine_pcm_new, |
Olivier Moysan | 7864809 | 2018-02-19 16:00:36 +0100 | [diff] [blame] | 362 | }; |
| 363 | |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 364 | static const char * const dmaengine_pcm_dma_channel_names[] = { |
| 365 | [SNDRV_PCM_STREAM_PLAYBACK] = "tx", |
| 366 | [SNDRV_PCM_STREAM_CAPTURE] = "rx", |
| 367 | }; |
| 368 | |
Stephen Warren | 5eda87b | 2013-12-10 11:11:02 -0700 | [diff] [blame] | 369 | static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm, |
Stephen Warren | 194c7de | 2013-12-03 14:26:34 -0700 | [diff] [blame] | 370 | struct device *dev, const struct snd_dmaengine_pcm_config *config) |
Lars-Peter Clausen | d1e1406 | 2013-04-20 19:29:00 +0200 | [diff] [blame] | 371 | { |
| 372 | unsigned int i; |
Stephen Warren | 11b3a7a | 2013-12-03 14:26:32 -0700 | [diff] [blame] | 373 | const char *name; |
Stephen Warren | 5eda87b | 2013-12-10 11:11:02 -0700 | [diff] [blame] | 374 | struct dma_chan *chan; |
Lars-Peter Clausen | d1e1406 | 2013-04-20 19:29:00 +0200 | [diff] [blame] | 375 | |
Sylwester Nawrocki | 76d9c68 | 2019-02-14 16:45:55 +0100 | [diff] [blame] | 376 | if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_DT) || (!dev->of_node && |
| 377 | !(config && config->dma_dev && config->dma_dev->of_node))) |
Stephen Warren | 5eda87b | 2013-12-10 11:11:02 -0700 | [diff] [blame] | 378 | return 0; |
Lars-Peter Clausen | d1e1406 | 2013-04-20 19:29:00 +0200 | [diff] [blame] | 379 | |
Xiubo Li | 2b67f8b | 2013-12-17 15:16:40 +0800 | [diff] [blame] | 380 | if (config && config->dma_dev) { |
Stephen Warren | 194c7de | 2013-12-03 14:26:34 -0700 | [diff] [blame] | 381 | /* |
| 382 | * If this warning is seen, it probably means that your Linux |
| 383 | * device structure does not match your HW device structure. |
| 384 | * It would be best to refactor the Linux device structure to |
| 385 | * correctly match the HW structure. |
| 386 | */ |
| 387 | dev_warn(dev, "DMA channels sourced from device %s", |
| 388 | dev_name(config->dma_dev)); |
| 389 | dev = config->dma_dev; |
| 390 | } |
| 391 | |
Kuninori Morimoto | ee10fbe | 2020-02-17 17:28:32 +0900 | [diff] [blame] | 392 | for_each_pcm_streams(i) { |
Stephen Warren | 11b3a7a | 2013-12-03 14:26:32 -0700 | [diff] [blame] | 393 | if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) |
| 394 | name = "rx-tx"; |
| 395 | else |
| 396 | name = dmaengine_pcm_dma_channel_names[i]; |
Xiubo Li | 2b67f8b | 2013-12-17 15:16:40 +0800 | [diff] [blame] | 397 | if (config && config->chan_names[i]) |
Stephen Warren | 194c7de | 2013-12-03 14:26:34 -0700 | [diff] [blame] | 398 | name = config->chan_names[i]; |
Peter Ujfalusi | de8cf952 | 2019-11-13 11:54:44 +0200 | [diff] [blame] | 399 | chan = dma_request_chan(dev, name); |
Stephen Warren | 5eda87b | 2013-12-10 11:11:02 -0700 | [diff] [blame] | 400 | if (IS_ERR(chan)) { |
Stephen Warren | e9036c2 | 2013-12-11 11:20:50 -0700 | [diff] [blame] | 401 | if (PTR_ERR(chan) == -EPROBE_DEFER) |
Stephen Warren | 5eda87b | 2013-12-10 11:11:02 -0700 | [diff] [blame] | 402 | return -EPROBE_DEFER; |
| 403 | pcm->chan[i] = NULL; |
| 404 | } else { |
| 405 | pcm->chan[i] = chan; |
| 406 | } |
Stephen Warren | 11b3a7a | 2013-12-03 14:26:32 -0700 | [diff] [blame] | 407 | if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) |
| 408 | break; |
Lars-Peter Clausen | d1e1406 | 2013-04-20 19:29:00 +0200 | [diff] [blame] | 409 | } |
Stephen Warren | 11b3a7a | 2013-12-03 14:26:32 -0700 | [diff] [blame] | 410 | |
| 411 | if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) |
| 412 | pcm->chan[1] = pcm->chan[0]; |
Stephen Warren | 5eda87b | 2013-12-10 11:11:02 -0700 | [diff] [blame] | 413 | |
| 414 | return 0; |
Lars-Peter Clausen | d1e1406 | 2013-04-20 19:29:00 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Stephen Warren | 6b9f3e6 | 2013-12-03 14:26:33 -0700 | [diff] [blame] | 417 | static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm) |
| 418 | { |
| 419 | unsigned int i; |
| 420 | |
Kuninori Morimoto | ee10fbe | 2020-02-17 17:28:32 +0900 | [diff] [blame] | 421 | for_each_pcm_streams(i) { |
Stephen Warren | 6b9f3e6 | 2013-12-03 14:26:33 -0700 | [diff] [blame] | 422 | if (!pcm->chan[i]) |
| 423 | continue; |
| 424 | dma_release_channel(pcm->chan[i]); |
| 425 | if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 430 | /** |
| 431 | * snd_dmaengine_pcm_register - Register a dmaengine based PCM device |
| 432 | * @dev: The parent device for the PCM device |
| 433 | * @config: Platform specific PCM configuration |
| 434 | * @flags: Platform specific quirks |
| 435 | */ |
| 436 | int snd_dmaengine_pcm_register(struct device *dev, |
| 437 | const struct snd_dmaengine_pcm_config *config, unsigned int flags) |
| 438 | { |
| 439 | struct dmaengine_pcm *pcm; |
Stephen Warren | 6b9f3e6 | 2013-12-03 14:26:33 -0700 | [diff] [blame] | 440 | int ret; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 441 | |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 442 | pcm = kzalloc(sizeof(*pcm), GFP_KERNEL); |
| 443 | if (!pcm) |
| 444 | return -ENOMEM; |
| 445 | |
Fabio Estevam | f0b3bdb | 2018-02-21 14:57:33 -0300 | [diff] [blame] | 446 | #ifdef CONFIG_DEBUG_FS |
| 447 | pcm->component.debugfs_prefix = "dma"; |
| 448 | #endif |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 449 | pcm->config = config; |
Lars-Peter Clausen | d1e1406 | 2013-04-20 19:29:00 +0200 | [diff] [blame] | 450 | pcm->flags = flags; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 451 | |
Stephen Warren | 5eda87b | 2013-12-10 11:11:02 -0700 | [diff] [blame] | 452 | ret = dmaengine_pcm_request_chan_of(pcm, dev, config); |
| 453 | if (ret) |
Fabio Estevam | b84acf4 | 2018-02-26 15:55:25 -0300 | [diff] [blame] | 454 | goto err_free_dma; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 455 | |
Olivier Moysan | 7864809 | 2018-02-19 16:00:36 +0100 | [diff] [blame] | 456 | if (config && config->process) |
| 457 | ret = snd_soc_add_component(dev, &pcm->component, |
| 458 | &dmaengine_pcm_component_process, |
| 459 | NULL, 0); |
| 460 | else |
| 461 | ret = snd_soc_add_component(dev, &pcm->component, |
| 462 | &dmaengine_pcm_component, NULL, 0); |
Stephen Warren | 6b9f3e6 | 2013-12-03 14:26:33 -0700 | [diff] [blame] | 463 | if (ret) |
| 464 | goto err_free_dma; |
| 465 | |
| 466 | return 0; |
| 467 | |
| 468 | err_free_dma: |
| 469 | dmaengine_pcm_release_chan(pcm); |
| 470 | kfree(pcm); |
| 471 | return ret; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 472 | } |
| 473 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register); |
| 474 | |
| 475 | /** |
| 476 | * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device |
| 477 | * @dev: Parent device the PCM was register with |
| 478 | * |
| 479 | * Removes a dmaengine based PCM device previously registered with |
| 480 | * snd_dmaengine_pcm_register. |
| 481 | */ |
| 482 | void snd_dmaengine_pcm_unregister(struct device *dev) |
| 483 | { |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 484 | struct snd_soc_component *component; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 485 | struct dmaengine_pcm *pcm; |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 486 | |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 487 | component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME); |
| 488 | if (!component) |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 489 | return; |
| 490 | |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 491 | pcm = soc_component_to_pcm(component); |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 492 | |
Kuninori Morimoto | be7ee5f | 2018-01-29 02:41:09 +0000 | [diff] [blame] | 493 | snd_soc_unregister_component(dev); |
Stephen Warren | 6b9f3e6 | 2013-12-03 14:26:33 -0700 | [diff] [blame] | 494 | dmaengine_pcm_release_chan(pcm); |
Lars-Peter Clausen | 28c4468 | 2013-04-15 19:19:50 +0200 | [diff] [blame] | 495 | kfree(pcm); |
| 496 | } |
| 497 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister); |
| 498 | |
| 499 | MODULE_LICENSE("GPL"); |