Path: blob/master/sound/soc/soc-generic-dmaengine-pcm.c
26381 views
// SPDX-License-Identifier: GPL-2.0+1//2// Copyright (C) 2013, Analog Devices Inc.3// Author: Lars-Peter Clausen <[email protected]>45#include <linux/module.h>6#include <linux/init.h>7#include <linux/dmaengine.h>8#include <linux/slab.h>9#include <sound/pcm.h>10#include <sound/pcm_params.h>11#include <sound/soc.h>12#include <linux/dma-mapping.h>13#include <linux/of.h>1415#include <sound/dmaengine_pcm.h>1617static unsigned int prealloc_buffer_size_kbytes = 512;18module_param(prealloc_buffer_size_kbytes, uint, 0444);19MODULE_PARM_DESC(prealloc_buffer_size_kbytes, "Preallocate DMA buffer size (KB).");2021/*22* The platforms dmaengine driver does not support reporting the amount of23* bytes that are still left to transfer.24*/25#define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)2627static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,28struct snd_pcm_substream *substream)29{30if (!pcm->chan[substream->stream])31return NULL;3233return pcm->chan[substream->stream]->device->dev;34}3536/**37* snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback38* @substream: PCM substream39* @params: hw_params40* @slave_config: DMA slave config to prepare41*42* This function can be used as a generic prepare_slave_config callback for43* platforms which make use of the snd_dmaengine_dai_dma_data struct for their44* DAI DMA data. Internally the function will first call45* snd_hwparams_to_dma_slave_config to fill in the slave config based on the46* hw_params, followed by snd_dmaengine_pcm_set_config_from_dai_data to fill in47* the remaining fields based on the DAI DMA data.48*/49int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,50struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)51{52struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);53struct snd_dmaengine_dai_dma_data *dma_data;54int ret;5556if (rtd->dai_link->num_cpus > 1) {57dev_err(rtd->dev,58"%s doesn't support Multi CPU yet\n", __func__);59return -EINVAL;60}6162dma_data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);6364ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);65if (ret)66return ret;6768snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,69slave_config);7071return 0;72}73EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);7475static int dmaengine_pcm_hw_params(struct snd_soc_component *component,76struct snd_pcm_substream *substream,77struct snd_pcm_hw_params *params)78{79struct dmaengine_pcm *pcm = soc_component_to_pcm(component);80struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);81struct dma_slave_config slave_config;82int ret;8384if (!pcm->config->prepare_slave_config)85return 0;8687memset(&slave_config, 0, sizeof(slave_config));8889ret = pcm->config->prepare_slave_config(substream, params, &slave_config);90if (ret)91return ret;9293return dmaengine_slave_config(chan, &slave_config);94}9596static int97dmaengine_pcm_set_runtime_hwparams(struct snd_soc_component *component,98struct snd_pcm_substream *substream)99{100struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);101struct dmaengine_pcm *pcm = soc_component_to_pcm(component);102struct device *dma_dev = dmaengine_dma_dev(pcm, substream);103struct dma_chan *chan = pcm->chan[substream->stream];104struct snd_dmaengine_dai_dma_data *dma_data;105struct snd_pcm_hardware hw;106107if (rtd->dai_link->num_cpus > 1) {108dev_err(rtd->dev,109"%s doesn't support Multi CPU yet\n", __func__);110return -EINVAL;111}112113if (pcm->config->pcm_hardware)114return snd_soc_set_runtime_hwparams(substream,115pcm->config->pcm_hardware);116117dma_data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);118119memset(&hw, 0, sizeof(hw));120hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |121SNDRV_PCM_INFO_INTERLEAVED;122hw.periods_min = 2;123hw.periods_max = UINT_MAX;124hw.period_bytes_min = dma_data->maxburst * DMA_SLAVE_BUSWIDTH_8_BYTES;125if (!hw.period_bytes_min)126hw.period_bytes_min = 256;127hw.period_bytes_max = dma_get_max_seg_size(dma_dev);128hw.buffer_bytes_max = SIZE_MAX;129hw.fifo_size = dma_data->fifo_size;130131if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)132hw.info |= SNDRV_PCM_INFO_BATCH;133134/**135* FIXME: Remove the return value check to align with the code136* before adding snd_dmaengine_pcm_refine_runtime_hwparams137* function.138*/139snd_dmaengine_pcm_refine_runtime_hwparams(substream,140dma_data,141&hw,142chan);143144return snd_soc_set_runtime_hwparams(substream, &hw);145}146147static int dmaengine_pcm_open(struct snd_soc_component *component,148struct snd_pcm_substream *substream)149{150struct dmaengine_pcm *pcm = soc_component_to_pcm(component);151struct dma_chan *chan = pcm->chan[substream->stream];152int ret;153154ret = dmaengine_pcm_set_runtime_hwparams(component, substream);155if (ret)156return ret;157158return snd_dmaengine_pcm_open(substream, chan);159}160161static int dmaengine_pcm_close(struct snd_soc_component *component,162struct snd_pcm_substream *substream)163{164return snd_dmaengine_pcm_close(substream);165}166167static int dmaengine_pcm_trigger(struct snd_soc_component *component,168struct snd_pcm_substream *substream, int cmd)169{170return snd_dmaengine_pcm_trigger(substream, cmd);171}172173static struct dma_chan *dmaengine_pcm_compat_request_channel(174struct snd_soc_component *component,175struct snd_soc_pcm_runtime *rtd,176struct snd_pcm_substream *substream)177{178struct dmaengine_pcm *pcm = soc_component_to_pcm(component);179struct snd_dmaengine_dai_dma_data *dma_data;180181if (rtd->dai_link->num_cpus > 1) {182dev_err(rtd->dev,183"%s doesn't support Multi CPU yet\n", __func__);184return NULL;185}186187dma_data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);188189if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])190return pcm->chan[0];191192if (pcm->config->compat_request_channel)193return pcm->config->compat_request_channel(rtd, substream);194195return snd_dmaengine_pcm_request_channel(pcm->config->compat_filter_fn,196dma_data->filter_data);197}198199static bool dmaengine_pcm_can_report_residue(struct device *dev,200struct dma_chan *chan)201{202struct dma_slave_caps dma_caps;203int ret;204205ret = dma_get_slave_caps(chan, &dma_caps);206if (ret != 0) {207dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",208ret);209return false;210}211212if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)213return false;214215return true;216}217218static int dmaengine_pcm_new(struct snd_soc_component *component,219struct snd_soc_pcm_runtime *rtd)220{221struct dmaengine_pcm *pcm = soc_component_to_pcm(component);222const struct snd_dmaengine_pcm_config *config = pcm->config;223struct device *dev = component->dev;224size_t prealloc_buffer_size;225size_t max_buffer_size;226unsigned int i;227228if (config->prealloc_buffer_size)229prealloc_buffer_size = config->prealloc_buffer_size;230else231prealloc_buffer_size = prealloc_buffer_size_kbytes * 1024;232233if (config->pcm_hardware && config->pcm_hardware->buffer_bytes_max)234max_buffer_size = config->pcm_hardware->buffer_bytes_max;235else236max_buffer_size = SIZE_MAX;237238for_each_pcm_streams(i) {239struct snd_pcm_substream *substream = rtd->pcm->streams[i].substream;240if (!substream)241continue;242243if (!pcm->chan[i] && config->chan_names[i])244pcm->chan[i] = dma_request_slave_channel(dev,245config->chan_names[i]);246247if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {248pcm->chan[i] = dmaengine_pcm_compat_request_channel(249component, rtd, substream);250}251252if (!pcm->chan[i]) {253dev_err(component->dev,254"Missing dma channel for stream: %d\n", i);255return -EINVAL;256}257258snd_pcm_set_managed_buffer(substream,259SNDRV_DMA_TYPE_DEV_IRAM,260dmaengine_dma_dev(pcm, substream),261prealloc_buffer_size,262max_buffer_size);263264if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))265pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;266267if (rtd->pcm->streams[i].pcm->name[0] == '\0') {268strscpy_pad(rtd->pcm->streams[i].pcm->name,269rtd->pcm->streams[i].pcm->id,270sizeof(rtd->pcm->streams[i].pcm->name));271}272}273274return 0;275}276277static snd_pcm_uframes_t dmaengine_pcm_pointer(278struct snd_soc_component *component,279struct snd_pcm_substream *substream)280{281struct dmaengine_pcm *pcm = soc_component_to_pcm(component);282283if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)284return snd_dmaengine_pcm_pointer_no_residue(substream);285else286return snd_dmaengine_pcm_pointer(substream);287}288289static int dmaengine_copy(struct snd_soc_component *component,290struct snd_pcm_substream *substream,291int channel, unsigned long hwoff,292struct iov_iter *iter, unsigned long bytes)293{294struct snd_pcm_runtime *runtime = substream->runtime;295struct dmaengine_pcm *pcm = soc_component_to_pcm(component);296int (*process)(struct snd_pcm_substream *substream,297int channel, unsigned long hwoff,298unsigned long bytes) = pcm->config->process;299bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;300void *dma_ptr = runtime->dma_area + hwoff +301channel * (runtime->dma_bytes / runtime->channels);302303if (is_playback)304if (copy_from_iter(dma_ptr, bytes, iter) != bytes)305return -EFAULT;306307if (process) {308int ret = process(substream, channel, hwoff, bytes);309if (ret < 0)310return ret;311}312313if (!is_playback)314if (copy_to_iter(dma_ptr, bytes, iter) != bytes)315return -EFAULT;316317return 0;318}319320static int dmaengine_pcm_sync_stop(struct snd_soc_component *component,321struct snd_pcm_substream *substream)322{323return snd_dmaengine_pcm_sync_stop(substream);324}325326static const struct snd_soc_component_driver dmaengine_pcm_component = {327.name = SND_DMAENGINE_PCM_DRV_NAME,328.probe_order = SND_SOC_COMP_ORDER_LATE,329.open = dmaengine_pcm_open,330.close = dmaengine_pcm_close,331.hw_params = dmaengine_pcm_hw_params,332.trigger = dmaengine_pcm_trigger,333.pointer = dmaengine_pcm_pointer,334.pcm_construct = dmaengine_pcm_new,335.sync_stop = dmaengine_pcm_sync_stop,336};337338static const struct snd_soc_component_driver dmaengine_pcm_component_process = {339.name = SND_DMAENGINE_PCM_DRV_NAME,340.probe_order = SND_SOC_COMP_ORDER_LATE,341.open = dmaengine_pcm_open,342.close = dmaengine_pcm_close,343.hw_params = dmaengine_pcm_hw_params,344.trigger = dmaengine_pcm_trigger,345.pointer = dmaengine_pcm_pointer,346.copy = dmaengine_copy,347.pcm_construct = dmaengine_pcm_new,348.sync_stop = dmaengine_pcm_sync_stop,349};350351static const char * const dmaengine_pcm_dma_channel_names[] = {352[SNDRV_PCM_STREAM_PLAYBACK] = "tx",353[SNDRV_PCM_STREAM_CAPTURE] = "rx",354};355356static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,357struct device *dev, const struct snd_dmaengine_pcm_config *config)358{359unsigned int i;360const char *name;361struct dma_chan *chan;362363if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_DT) || (!dev->of_node &&364!(config->dma_dev && config->dma_dev->of_node)))365return 0;366367if (config->dma_dev) {368/*369* If this warning is seen, it probably means that your Linux370* device structure does not match your HW device structure.371* It would be best to refactor the Linux device structure to372* correctly match the HW structure.373*/374dev_warn(dev, "DMA channels sourced from device %s",375dev_name(config->dma_dev));376dev = config->dma_dev;377}378379for_each_pcm_streams(i) {380if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)381name = "rx-tx";382else383name = dmaengine_pcm_dma_channel_names[i];384if (config->chan_names[i])385name = config->chan_names[i];386chan = dma_request_chan(dev, name);387if (IS_ERR(chan)) {388/*389* Only report probe deferral errors, channels390* might not be present for devices that391* support only TX or only RX.392*/393if (PTR_ERR(chan) == -EPROBE_DEFER)394return -EPROBE_DEFER;395pcm->chan[i] = NULL;396} else {397pcm->chan[i] = chan;398}399if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)400break;401}402403if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)404pcm->chan[1] = pcm->chan[0];405406return 0;407}408409static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)410{411unsigned int i;412413for_each_pcm_streams(i) {414if (!pcm->chan[i])415continue;416dma_release_channel(pcm->chan[i]);417if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)418break;419}420}421422static const struct snd_dmaengine_pcm_config snd_dmaengine_pcm_default_config = {423.prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,424};425426/**427* snd_dmaengine_pcm_register - Register a dmaengine based PCM device428* @dev: The parent device for the PCM device429* @config: Platform specific PCM configuration430* @flags: Platform specific quirks431*/432int snd_dmaengine_pcm_register(struct device *dev,433const struct snd_dmaengine_pcm_config *config, unsigned int flags)434{435const struct snd_soc_component_driver *driver;436struct dmaengine_pcm *pcm;437int ret;438439pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);440if (!pcm)441return -ENOMEM;442443#ifdef CONFIG_DEBUG_FS444pcm->component.debugfs_prefix = "dma";445#endif446if (!config)447config = &snd_dmaengine_pcm_default_config;448pcm->config = config;449pcm->flags = flags;450451if (config->name)452pcm->component.name = config->name;453454ret = dmaengine_pcm_request_chan_of(pcm, dev, config);455if (ret)456goto err_free_dma;457458if (config->process)459driver = &dmaengine_pcm_component_process;460else461driver = &dmaengine_pcm_component;462463ret = snd_soc_component_initialize(&pcm->component, driver, dev);464if (ret)465goto err_free_dma;466467ret = snd_soc_add_component(&pcm->component, NULL, 0);468if (ret)469goto err_free_dma;470471return 0;472473err_free_dma:474dmaengine_pcm_release_chan(pcm);475kfree(pcm);476return ret;477}478EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);479480/**481* snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device482* @dev: Parent device the PCM was register with483*484* Removes a dmaengine based PCM device previously registered with485* snd_dmaengine_pcm_register.486*/487void snd_dmaengine_pcm_unregister(struct device *dev)488{489struct snd_soc_component *component;490struct dmaengine_pcm *pcm;491492component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);493if (!component)494return;495496pcm = soc_component_to_pcm(component);497498snd_soc_unregister_component_by_driver(dev, component->driver);499dmaengine_pcm_release_chan(pcm);500kfree(pcm);501}502EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);503504MODULE_DESCRIPTION("ASoC helpers for generic PCM dmaengine API");505MODULE_LICENSE("GPL");506507508