0001
0002
0003
0004
0005
0006 #include <linux/module.h>
0007 #include <linux/init.h>
0008 #include <linux/dmaengine.h>
0009 #include <linux/slab.h>
0010 #include <sound/pcm.h>
0011 #include <sound/pcm_params.h>
0012 #include <sound/soc.h>
0013 #include <linux/dma-mapping.h>
0014 #include <linux/of.h>
0015
0016 #include <sound/dmaengine_pcm.h>
0017
0018 static unsigned int prealloc_buffer_size_kbytes = 512;
0019 module_param(prealloc_buffer_size_kbytes, uint, 0444);
0020 MODULE_PARM_DESC(prealloc_buffer_size_kbytes, "Preallocate DMA buffer size (KB).");
0021
0022
0023
0024
0025
0026 #define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
0027
0028 static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
0029 struct snd_pcm_substream *substream)
0030 {
0031 if (!pcm->chan[substream->stream])
0032 return NULL;
0033
0034 return pcm->chan[substream->stream]->device->dev;
0035 }
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
0051 struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
0052 {
0053 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0054 struct snd_dmaengine_dai_dma_data *dma_data;
0055 int ret;
0056
0057 if (rtd->num_cpus > 1) {
0058 dev_err(rtd->dev,
0059 "%s doesn't support Multi CPU yet\n", __func__);
0060 return -EINVAL;
0061 }
0062
0063 dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
0064
0065 ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
0066 if (ret)
0067 return ret;
0068
0069 snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
0070 slave_config);
0071
0072 return 0;
0073 }
0074 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
0075
0076 static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
0077 struct snd_pcm_substream *substream,
0078 struct snd_pcm_hw_params *params)
0079 {
0080 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
0081 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
0082 struct dma_slave_config slave_config;
0083 int ret;
0084
0085 if (!pcm->config->prepare_slave_config)
0086 return 0;
0087
0088 memset(&slave_config, 0, sizeof(slave_config));
0089
0090 ret = pcm->config->prepare_slave_config(substream, params, &slave_config);
0091 if (ret)
0092 return ret;
0093
0094 return dmaengine_slave_config(chan, &slave_config);
0095 }
0096
0097 static int
0098 dmaengine_pcm_set_runtime_hwparams(struct snd_soc_component *component,
0099 struct snd_pcm_substream *substream)
0100 {
0101 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0102 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
0103 struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
0104 struct dma_chan *chan = pcm->chan[substream->stream];
0105 struct snd_dmaengine_dai_dma_data *dma_data;
0106 struct snd_pcm_hardware hw;
0107
0108 if (rtd->num_cpus > 1) {
0109 dev_err(rtd->dev,
0110 "%s doesn't support Multi CPU yet\n", __func__);
0111 return -EINVAL;
0112 }
0113
0114 if (pcm->config->pcm_hardware)
0115 return snd_soc_set_runtime_hwparams(substream,
0116 pcm->config->pcm_hardware);
0117
0118 dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
0119
0120 memset(&hw, 0, sizeof(hw));
0121 hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
0122 SNDRV_PCM_INFO_INTERLEAVED;
0123 hw.periods_min = 2;
0124 hw.periods_max = UINT_MAX;
0125 hw.period_bytes_min = dma_data->maxburst * DMA_SLAVE_BUSWIDTH_8_BYTES;
0126 if (!hw.period_bytes_min)
0127 hw.period_bytes_min = 256;
0128 hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
0129 hw.buffer_bytes_max = SIZE_MAX;
0130 hw.fifo_size = dma_data->fifo_size;
0131
0132 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
0133 hw.info |= SNDRV_PCM_INFO_BATCH;
0134
0135
0136
0137
0138
0139
0140 snd_dmaengine_pcm_refine_runtime_hwparams(substream,
0141 dma_data,
0142 &hw,
0143 chan);
0144
0145 return snd_soc_set_runtime_hwparams(substream, &hw);
0146 }
0147
0148 static int dmaengine_pcm_open(struct snd_soc_component *component,
0149 struct snd_pcm_substream *substream)
0150 {
0151 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
0152 struct dma_chan *chan = pcm->chan[substream->stream];
0153 int ret;
0154
0155 ret = dmaengine_pcm_set_runtime_hwparams(component, substream);
0156 if (ret)
0157 return ret;
0158
0159 return snd_dmaengine_pcm_open(substream, chan);
0160 }
0161
0162 static int dmaengine_pcm_close(struct snd_soc_component *component,
0163 struct snd_pcm_substream *substream)
0164 {
0165 return snd_dmaengine_pcm_close(substream);
0166 }
0167
0168 static int dmaengine_pcm_trigger(struct snd_soc_component *component,
0169 struct snd_pcm_substream *substream, int cmd)
0170 {
0171 return snd_dmaengine_pcm_trigger(substream, cmd);
0172 }
0173
0174 static struct dma_chan *dmaengine_pcm_compat_request_channel(
0175 struct snd_soc_component *component,
0176 struct snd_soc_pcm_runtime *rtd,
0177 struct snd_pcm_substream *substream)
0178 {
0179 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
0180 struct snd_dmaengine_dai_dma_data *dma_data;
0181
0182 if (rtd->num_cpus > 1) {
0183 dev_err(rtd->dev,
0184 "%s doesn't support Multi CPU yet\n", __func__);
0185 return NULL;
0186 }
0187
0188 dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
0189
0190 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
0191 return pcm->chan[0];
0192
0193 if (pcm->config->compat_request_channel)
0194 return pcm->config->compat_request_channel(rtd, substream);
0195
0196 return snd_dmaengine_pcm_request_channel(pcm->config->compat_filter_fn,
0197 dma_data->filter_data);
0198 }
0199
0200 static bool dmaengine_pcm_can_report_residue(struct device *dev,
0201 struct dma_chan *chan)
0202 {
0203 struct dma_slave_caps dma_caps;
0204 int ret;
0205
0206 ret = dma_get_slave_caps(chan, &dma_caps);
0207 if (ret != 0) {
0208 dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
0209 ret);
0210 return false;
0211 }
0212
0213 if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
0214 return false;
0215
0216 return true;
0217 }
0218
0219 static int dmaengine_pcm_new(struct snd_soc_component *component,
0220 struct snd_soc_pcm_runtime *rtd)
0221 {
0222 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
0223 const struct snd_dmaengine_pcm_config *config = pcm->config;
0224 struct device *dev = component->dev;
0225 size_t prealloc_buffer_size;
0226 size_t max_buffer_size;
0227 unsigned int i;
0228
0229 if (config->prealloc_buffer_size)
0230 prealloc_buffer_size = config->prealloc_buffer_size;
0231 else
0232 prealloc_buffer_size = prealloc_buffer_size_kbytes * 1024;
0233
0234 if (config->pcm_hardware && config->pcm_hardware->buffer_bytes_max)
0235 max_buffer_size = config->pcm_hardware->buffer_bytes_max;
0236 else
0237 max_buffer_size = SIZE_MAX;
0238
0239 for_each_pcm_streams(i) {
0240 struct snd_pcm_substream *substream = rtd->pcm->streams[i].substream;
0241 if (!substream)
0242 continue;
0243
0244 if (!pcm->chan[i] && config->chan_names[i])
0245 pcm->chan[i] = dma_request_slave_channel(dev,
0246 config->chan_names[i]);
0247
0248 if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
0249 pcm->chan[i] = dmaengine_pcm_compat_request_channel(
0250 component, rtd, substream);
0251 }
0252
0253 if (!pcm->chan[i]) {
0254 dev_err(component->dev,
0255 "Missing dma channel for stream: %d\n", i);
0256 return -EINVAL;
0257 }
0258
0259 snd_pcm_set_managed_buffer(substream,
0260 SNDRV_DMA_TYPE_DEV_IRAM,
0261 dmaengine_dma_dev(pcm, substream),
0262 prealloc_buffer_size,
0263 max_buffer_size);
0264
0265 if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
0266 pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
0267
0268 if (rtd->pcm->streams[i].pcm->name[0] == '\0') {
0269 strscpy_pad(rtd->pcm->streams[i].pcm->name,
0270 rtd->pcm->streams[i].pcm->id,
0271 sizeof(rtd->pcm->streams[i].pcm->name));
0272 }
0273 }
0274
0275 return 0;
0276 }
0277
0278 static snd_pcm_uframes_t dmaengine_pcm_pointer(
0279 struct snd_soc_component *component,
0280 struct snd_pcm_substream *substream)
0281 {
0282 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
0283
0284 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
0285 return snd_dmaengine_pcm_pointer_no_residue(substream);
0286 else
0287 return snd_dmaengine_pcm_pointer(substream);
0288 }
0289
0290 static int dmaengine_copy_user(struct snd_soc_component *component,
0291 struct snd_pcm_substream *substream,
0292 int channel, unsigned long hwoff,
0293 void __user *buf, unsigned long bytes)
0294 {
0295 struct snd_pcm_runtime *runtime = substream->runtime;
0296 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
0297 int (*process)(struct snd_pcm_substream *substream,
0298 int channel, unsigned long hwoff,
0299 void *buf, unsigned long bytes) = pcm->config->process;
0300 bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
0301 void *dma_ptr = runtime->dma_area + hwoff +
0302 channel * (runtime->dma_bytes / runtime->channels);
0303
0304 if (is_playback)
0305 if (copy_from_user(dma_ptr, buf, bytes))
0306 return -EFAULT;
0307
0308 if (process) {
0309 int ret = process(substream, channel, hwoff, (__force void *)buf, bytes);
0310 if (ret < 0)
0311 return ret;
0312 }
0313
0314 if (!is_playback)
0315 if (copy_to_user(buf, dma_ptr, bytes))
0316 return -EFAULT;
0317
0318 return 0;
0319 }
0320
0321 static const struct snd_soc_component_driver dmaengine_pcm_component = {
0322 .name = SND_DMAENGINE_PCM_DRV_NAME,
0323 .probe_order = SND_SOC_COMP_ORDER_LATE,
0324 .open = dmaengine_pcm_open,
0325 .close = dmaengine_pcm_close,
0326 .hw_params = dmaengine_pcm_hw_params,
0327 .trigger = dmaengine_pcm_trigger,
0328 .pointer = dmaengine_pcm_pointer,
0329 .pcm_construct = dmaengine_pcm_new,
0330 };
0331
0332 static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
0333 .name = SND_DMAENGINE_PCM_DRV_NAME,
0334 .probe_order = SND_SOC_COMP_ORDER_LATE,
0335 .open = dmaengine_pcm_open,
0336 .close = dmaengine_pcm_close,
0337 .hw_params = dmaengine_pcm_hw_params,
0338 .trigger = dmaengine_pcm_trigger,
0339 .pointer = dmaengine_pcm_pointer,
0340 .copy_user = dmaengine_copy_user,
0341 .pcm_construct = dmaengine_pcm_new,
0342 };
0343
0344 static const char * const dmaengine_pcm_dma_channel_names[] = {
0345 [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
0346 [SNDRV_PCM_STREAM_CAPTURE] = "rx",
0347 };
0348
0349 static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
0350 struct device *dev, const struct snd_dmaengine_pcm_config *config)
0351 {
0352 unsigned int i;
0353 const char *name;
0354 struct dma_chan *chan;
0355
0356 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_DT) || (!dev->of_node &&
0357 !(config->dma_dev && config->dma_dev->of_node)))
0358 return 0;
0359
0360 if (config->dma_dev) {
0361
0362
0363
0364
0365
0366
0367 dev_warn(dev, "DMA channels sourced from device %s",
0368 dev_name(config->dma_dev));
0369 dev = config->dma_dev;
0370 }
0371
0372 for_each_pcm_streams(i) {
0373 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
0374 name = "rx-tx";
0375 else
0376 name = dmaengine_pcm_dma_channel_names[i];
0377 if (config->chan_names[i])
0378 name = config->chan_names[i];
0379 chan = dma_request_chan(dev, name);
0380 if (IS_ERR(chan)) {
0381
0382
0383
0384
0385
0386 if (PTR_ERR(chan) == -EPROBE_DEFER)
0387 return -EPROBE_DEFER;
0388 pcm->chan[i] = NULL;
0389 } else {
0390 pcm->chan[i] = chan;
0391 }
0392 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
0393 break;
0394 }
0395
0396 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
0397 pcm->chan[1] = pcm->chan[0];
0398
0399 return 0;
0400 }
0401
0402 static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
0403 {
0404 unsigned int i;
0405
0406 for_each_pcm_streams(i) {
0407 if (!pcm->chan[i])
0408 continue;
0409 dma_release_channel(pcm->chan[i]);
0410 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
0411 break;
0412 }
0413 }
0414
0415 static const struct snd_dmaengine_pcm_config snd_dmaengine_pcm_default_config = {
0416 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
0417 };
0418
0419
0420
0421
0422
0423
0424
0425 int snd_dmaengine_pcm_register(struct device *dev,
0426 const struct snd_dmaengine_pcm_config *config, unsigned int flags)
0427 {
0428 const struct snd_soc_component_driver *driver;
0429 struct dmaengine_pcm *pcm;
0430 int ret;
0431
0432 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
0433 if (!pcm)
0434 return -ENOMEM;
0435
0436 #ifdef CONFIG_DEBUG_FS
0437 pcm->component.debugfs_prefix = "dma";
0438 #endif
0439 if (!config)
0440 config = &snd_dmaengine_pcm_default_config;
0441 pcm->config = config;
0442 pcm->flags = flags;
0443
0444 ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
0445 if (ret)
0446 goto err_free_dma;
0447
0448 if (config->process)
0449 driver = &dmaengine_pcm_component_process;
0450 else
0451 driver = &dmaengine_pcm_component;
0452
0453 ret = snd_soc_component_initialize(&pcm->component, driver, dev);
0454 if (ret)
0455 goto err_free_dma;
0456
0457 ret = snd_soc_add_component(&pcm->component, NULL, 0);
0458 if (ret)
0459 goto err_free_dma;
0460
0461 return 0;
0462
0463 err_free_dma:
0464 dmaengine_pcm_release_chan(pcm);
0465 kfree(pcm);
0466 return ret;
0467 }
0468 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
0469
0470
0471
0472
0473
0474
0475
0476
0477 void snd_dmaengine_pcm_unregister(struct device *dev)
0478 {
0479 struct snd_soc_component *component;
0480 struct dmaengine_pcm *pcm;
0481
0482 component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);
0483 if (!component)
0484 return;
0485
0486 pcm = soc_component_to_pcm(component);
0487
0488 snd_soc_unregister_component_by_driver(dev, component->driver);
0489 dmaengine_pcm_release_chan(pcm);
0490 kfree(pcm);
0491 }
0492 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
0493
0494 MODULE_LICENSE("GPL");