/* 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* @port_window_size: The length of the register area in words the data need72* to be accessed on the device side. It is only used for devices which is using73* an area instead of a single register to send/receive the data. Typically the74* DMA loops in this area in order to transfer the data.75*/76struct snd_dmaengine_dai_dma_data {77dma_addr_t addr;78enum dma_slave_buswidth addr_width;79u32 maxburst;80void *filter_data;81const char *chan_name;82unsigned int fifo_size;83unsigned int flags;84void *peripheral_config;85size_t peripheral_size;86u32 port_window_size;87};8889void snd_dmaengine_pcm_set_config_from_dai_data(90const struct snd_pcm_substream *substream,91const struct snd_dmaengine_dai_dma_data *dma_data,92struct dma_slave_config *config);9394int snd_dmaengine_pcm_refine_runtime_hwparams(95struct snd_pcm_substream *substream,96struct snd_dmaengine_dai_dma_data *dma_data,97struct snd_pcm_hardware *hw,98struct dma_chan *chan);99100/*101* Try to request the DMA channel using compat_request_channel or102* compat_filter_fn if it couldn't be requested through devicetree.103*/104#define SND_DMAENGINE_PCM_FLAG_COMPAT BIT(0)105/*106* Don't try to request the DMA channels through devicetree. This flag only107* makes sense if SND_DMAENGINE_PCM_FLAG_COMPAT is set as well.108*/109#define SND_DMAENGINE_PCM_FLAG_NO_DT BIT(1)110/*111* The PCM is half duplex and the DMA channel is shared between capture and112* playback.113*/114#define SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX BIT(3)115116/**117* struct snd_dmaengine_pcm_config - Configuration data for dmaengine based PCM118* @prepare_slave_config: Callback used to fill in the DMA slave_config for a119* PCM substream. Will be called from the PCM drivers hwparams callback.120* @compat_request_channel: Callback to request a DMA channel for platforms121* which do not use devicetree.122* @process: Callback used to apply processing on samples transferred from/to123* user space.124* @name: Component name. If null, dev_name will be used.125* @compat_filter_fn: Will be used as the filter function when requesting a126* channel for platforms which do not use devicetree. The filter parameter127* will be the DAI's DMA data.128* @dma_dev: If set, request DMA channel on this device rather than the DAI129* device.130* @chan_names: If set, these custom DMA channel names will be requested at131* registration time.132* @pcm_hardware: snd_pcm_hardware struct to be used for the PCM.133* @prealloc_buffer_size: Size of the preallocated audio buffer.134*135* Note: If both compat_request_channel and compat_filter_fn are set136* compat_request_channel will be used to request the channel and137* compat_filter_fn will be ignored. Otherwise the channel will be requested138* using dma_request_channel with compat_filter_fn as the filter function.139*/140struct snd_dmaengine_pcm_config {141int (*prepare_slave_config)(struct snd_pcm_substream *substream,142struct snd_pcm_hw_params *params,143struct dma_slave_config *slave_config);144struct dma_chan *(*compat_request_channel)(145struct snd_soc_pcm_runtime *rtd,146struct snd_pcm_substream *substream);147int (*process)(struct snd_pcm_substream *substream,148int channel, unsigned long hwoff,149unsigned long bytes);150const char *name;151dma_filter_fn compat_filter_fn;152struct device *dma_dev;153const char *chan_names[SNDRV_PCM_STREAM_LAST + 1];154155const struct snd_pcm_hardware *pcm_hardware;156unsigned int prealloc_buffer_size;157};158159int snd_dmaengine_pcm_register(struct device *dev,160const struct snd_dmaengine_pcm_config *config,161unsigned int flags);162void snd_dmaengine_pcm_unregister(struct device *dev);163164int devm_snd_dmaengine_pcm_register(struct device *dev,165const struct snd_dmaengine_pcm_config *config,166unsigned int flags);167168int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,169struct snd_pcm_hw_params *params,170struct dma_slave_config *slave_config);171172#define SND_DMAENGINE_PCM_DRV_NAME "snd_dmaengine_pcm"173174struct dmaengine_pcm {175struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];176const struct snd_dmaengine_pcm_config *config;177struct snd_soc_component component;178unsigned int flags;179};180181static inline struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p)182{183return container_of(p, struct dmaengine_pcm, component);184}185#endif186187188