/* SPDX-License-Identifier: GPL-2.0+1*2* Copyright (C) 2012, Analog Devices Inc.3* Author: Lars-Peter Clausen <[email protected]>4*/56#ifndef __SOUND_DMAENGINE_PCM_H__7#define __SOUND_DMAENGINE_PCM_H__89#include <sound/pcm.h>10#include <sound/soc.h>11#include <linux/dmaengine.h>1213/**14* snd_pcm_substream_to_dma_direction - Get dma_transfer_direction for a PCM15* substream16* @substream: PCM substream17*18* Return: DMA transfer direction19*/20static inline enum dma_transfer_direction21snd_pcm_substream_to_dma_direction(const struct snd_pcm_substream *substream)22{23if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)24return DMA_MEM_TO_DEV;25else26return DMA_DEV_TO_MEM;27}2829int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,30const struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config);31int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd);32snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream);33snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream);3435int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,36struct dma_chan *chan);37int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream);38int snd_dmaengine_pcm_sync_stop(struct snd_pcm_substream *substream);3940int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream);4142struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,43void *filter_data);44struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream);4546/*47* The DAI supports packed transfers, eg 2 16-bit samples in a 32-bit word.48* If this flag is set the dmaengine driver won't put any restriction on49* the supported sample formats and set the DMA transfer size to undefined.50* The DAI driver is responsible to disable any unsupported formats in it's51* configuration and catch corner cases that are not already handled in52* the ALSA core.53*/54#define SND_DMAENGINE_PCM_DAI_FLAG_PACK BIT(0)5556/**57* struct snd_dmaengine_dai_dma_data - DAI DMA configuration data58* @addr: Address of the DAI data source or destination register.59* @addr_width: Width of the DAI data source or destination register.60* @maxburst: Maximum number of words(note: words, as in units of the61* src_addr_width member, not bytes) that can be send to or received from the62* DAI in one burst.63* @filter_data: Custom DMA channel filter data, this will usually be used when64* requesting the DMA channel.65* @chan_name: Custom channel name to use when requesting DMA channel.66* @fifo_size: FIFO size of the DAI controller in bytes67* @flags: PCM_DAI flags, only SND_DMAENGINE_PCM_DAI_FLAG_PACK for now68* @peripheral_config: peripheral configuration for programming peripheral69* for dmaengine transfer70* @peripheral_size: peripheral configuration buffer size71*/72struct snd_dmaengine_dai_dma_data {73dma_addr_t addr;74enum dma_slave_buswidth addr_width;75u32 maxburst;76void *filter_data;77const char *chan_name;78unsigned int fifo_size;79unsigned int flags;80void *peripheral_config;81size_t peripheral_size;82};8384void snd_dmaengine_pcm_set_config_from_dai_data(85const struct snd_pcm_substream *substream,86const struct snd_dmaengine_dai_dma_data *dma_data,87struct dma_slave_config *config);8889int snd_dmaengine_pcm_refine_runtime_hwparams(90struct snd_pcm_substream *substream,91struct snd_dmaengine_dai_dma_data *dma_data,92struct snd_pcm_hardware *hw,93struct dma_chan *chan);9495/*96* Try to request the DMA channel using compat_request_channel or97* compat_filter_fn if it couldn't be requested through devicetree.98*/99#define SND_DMAENGINE_PCM_FLAG_COMPAT BIT(0)100/*101* Don't try to request the DMA channels through devicetree. This flag only102* makes sense if SND_DMAENGINE_PCM_FLAG_COMPAT is set as well.103*/104#define SND_DMAENGINE_PCM_FLAG_NO_DT BIT(1)105/*106* The PCM is half duplex and the DMA channel is shared between capture and107* playback.108*/109#define SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX BIT(3)110111/**112* struct snd_dmaengine_pcm_config - Configuration data for dmaengine based PCM113* @prepare_slave_config: Callback used to fill in the DMA slave_config for a114* PCM substream. Will be called from the PCM drivers hwparams callback.115* @compat_request_channel: Callback to request a DMA channel for platforms116* which do not use devicetree.117* @process: Callback used to apply processing on samples transferred from/to118* user space.119* @name: Component name. If null, dev_name will be used.120* @compat_filter_fn: Will be used as the filter function when requesting a121* channel for platforms which do not use devicetree. The filter parameter122* will be the DAI's DMA data.123* @dma_dev: If set, request DMA channel on this device rather than the DAI124* device.125* @chan_names: If set, these custom DMA channel names will be requested at126* registration time.127* @pcm_hardware: snd_pcm_hardware struct to be used for the PCM.128* @prealloc_buffer_size: Size of the preallocated audio buffer.129*130* Note: If both compat_request_channel and compat_filter_fn are set131* compat_request_channel will be used to request the channel and132* compat_filter_fn will be ignored. Otherwise the channel will be requested133* using dma_request_channel with compat_filter_fn as the filter function.134*/135struct snd_dmaengine_pcm_config {136int (*prepare_slave_config)(struct snd_pcm_substream *substream,137struct snd_pcm_hw_params *params,138struct dma_slave_config *slave_config);139struct dma_chan *(*compat_request_channel)(140struct snd_soc_pcm_runtime *rtd,141struct snd_pcm_substream *substream);142int (*process)(struct snd_pcm_substream *substream,143int channel, unsigned long hwoff,144unsigned long bytes);145const char *name;146dma_filter_fn compat_filter_fn;147struct device *dma_dev;148const char *chan_names[SNDRV_PCM_STREAM_LAST + 1];149150const struct snd_pcm_hardware *pcm_hardware;151unsigned int prealloc_buffer_size;152};153154int snd_dmaengine_pcm_register(struct device *dev,155const struct snd_dmaengine_pcm_config *config,156unsigned int flags);157void snd_dmaengine_pcm_unregister(struct device *dev);158159int devm_snd_dmaengine_pcm_register(struct device *dev,160const struct snd_dmaengine_pcm_config *config,161unsigned int flags);162163int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,164struct snd_pcm_hw_params *params,165struct dma_slave_config *slave_config);166167#define SND_DMAENGINE_PCM_DRV_NAME "snd_dmaengine_pcm"168169struct dmaengine_pcm {170struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];171const struct snd_dmaengine_pcm_config *config;172struct snd_soc_component component;173unsigned int flags;174};175176static inline struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p)177{178return container_of(p, struct dmaengine_pcm, component);179}180#endif181182183