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