Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+
0002  *
0003  *  Copyright (C) 2012, Analog Devices Inc.
0004  *  Author: Lars-Peter Clausen <lars@metafoo.de>
0005  */
0006 
0007 #ifndef __SOUND_DMAENGINE_PCM_H__
0008 #define __SOUND_DMAENGINE_PCM_H__
0009 
0010 #include <sound/pcm.h>
0011 #include <sound/soc.h>
0012 #include <linux/dmaengine.h>
0013 
0014 /**
0015  * snd_pcm_substream_to_dma_direction - Get dma_transfer_direction for a PCM
0016  *   substream
0017  * @substream: PCM substream
0018  *
0019  * Return: DMA transfer direction
0020  */
0021 static inline enum dma_transfer_direction
0022 snd_pcm_substream_to_dma_direction(const struct snd_pcm_substream *substream)
0023 {
0024     if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0025         return DMA_MEM_TO_DEV;
0026     else
0027         return DMA_DEV_TO_MEM;
0028 }
0029 
0030 int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
0031     const struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config);
0032 int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd);
0033 snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream);
0034 snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream);
0035 
0036 int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
0037     struct dma_chan *chan);
0038 int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream);
0039 
0040 int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
0041     dma_filter_fn filter_fn, void *filter_data);
0042 int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream);
0043 
0044 struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
0045     void *filter_data);
0046 struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream);
0047 
0048 /*
0049  * The DAI supports packed transfers, eg 2 16-bit samples in a 32-bit word.
0050  * If this flag is set the dmaengine driver won't put any restriction on
0051  * the supported sample formats and set the DMA transfer size to undefined.
0052  * The DAI driver is responsible to disable any unsupported formats in it's
0053  * configuration and catch corner cases that are not already handled in
0054  * the ALSA core.
0055  */
0056 #define SND_DMAENGINE_PCM_DAI_FLAG_PACK BIT(0)
0057 
0058 /**
0059  * struct snd_dmaengine_dai_dma_data - DAI DMA configuration data
0060  * @addr: Address of the DAI data source or destination register.
0061  * @addr_width: Width of the DAI data source or destination register.
0062  * @maxburst: Maximum number of words(note: words, as in units of the
0063  * src_addr_width member, not bytes) that can be send to or received from the
0064  * DAI in one burst.
0065  * @filter_data: Custom DMA channel filter data, this will usually be used when
0066  * requesting the DMA channel.
0067  * @chan_name: Custom channel name to use when requesting DMA channel.
0068  * @fifo_size: FIFO size of the DAI controller in bytes
0069  * @flags: PCM_DAI flags, only SND_DMAENGINE_PCM_DAI_FLAG_PACK for now
0070  * @peripheral_config: peripheral configuration for programming peripheral
0071  * for dmaengine transfer
0072  * @peripheral_size: peripheral configuration buffer size
0073  */
0074 struct snd_dmaengine_dai_dma_data {
0075     dma_addr_t addr;
0076     enum dma_slave_buswidth addr_width;
0077     u32 maxburst;
0078     void *filter_data;
0079     const char *chan_name;
0080     unsigned int fifo_size;
0081     unsigned int flags;
0082     void *peripheral_config;
0083     size_t peripheral_size;
0084 };
0085 
0086 void snd_dmaengine_pcm_set_config_from_dai_data(
0087     const struct snd_pcm_substream *substream,
0088     const struct snd_dmaengine_dai_dma_data *dma_data,
0089     struct dma_slave_config *config);
0090 
0091 int snd_dmaengine_pcm_refine_runtime_hwparams(
0092     struct snd_pcm_substream *substream,
0093     struct snd_dmaengine_dai_dma_data *dma_data,
0094     struct snd_pcm_hardware *hw,
0095     struct dma_chan *chan);
0096 
0097 /*
0098  * Try to request the DMA channel using compat_request_channel or
0099  * compat_filter_fn if it couldn't be requested through devicetree.
0100  */
0101 #define SND_DMAENGINE_PCM_FLAG_COMPAT BIT(0)
0102 /*
0103  * Don't try to request the DMA channels through devicetree. This flag only
0104  * makes sense if SND_DMAENGINE_PCM_FLAG_COMPAT is set as well.
0105  */
0106 #define SND_DMAENGINE_PCM_FLAG_NO_DT BIT(1)
0107 /*
0108  * The PCM is half duplex and the DMA channel is shared between capture and
0109  * playback.
0110  */
0111 #define SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX BIT(3)
0112 
0113 /**
0114  * struct snd_dmaengine_pcm_config - Configuration data for dmaengine based PCM
0115  * @prepare_slave_config: Callback used to fill in the DMA slave_config for a
0116  *   PCM substream. Will be called from the PCM drivers hwparams callback.
0117  * @compat_request_channel: Callback to request a DMA channel for platforms
0118  *   which do not use devicetree.
0119  * @process: Callback used to apply processing on samples transferred from/to
0120  *   user space.
0121  * @compat_filter_fn: Will be used as the filter function when requesting a
0122  *  channel for platforms which do not use devicetree. The filter parameter
0123  *  will be the DAI's DMA data.
0124  * @dma_dev: If set, request DMA channel on this device rather than the DAI
0125  *  device.
0126  * @chan_names: If set, these custom DMA channel names will be requested at
0127  *  registration time.
0128  * @pcm_hardware: snd_pcm_hardware struct to be used for the PCM.
0129  * @prealloc_buffer_size: Size of the preallocated audio buffer.
0130  *
0131  * Note: If both compat_request_channel and compat_filter_fn are set
0132  * compat_request_channel will be used to request the channel and
0133  * compat_filter_fn will be ignored. Otherwise the channel will be requested
0134  * using dma_request_channel with compat_filter_fn as the filter function.
0135  */
0136 struct snd_dmaengine_pcm_config {
0137     int (*prepare_slave_config)(struct snd_pcm_substream *substream,
0138             struct snd_pcm_hw_params *params,
0139             struct dma_slave_config *slave_config);
0140     struct dma_chan *(*compat_request_channel)(
0141             struct snd_soc_pcm_runtime *rtd,
0142             struct snd_pcm_substream *substream);
0143     int (*process)(struct snd_pcm_substream *substream,
0144                int channel, unsigned long hwoff,
0145                void *buf, unsigned long bytes);
0146     dma_filter_fn compat_filter_fn;
0147     struct device *dma_dev;
0148     const char *chan_names[SNDRV_PCM_STREAM_LAST + 1];
0149 
0150     const struct snd_pcm_hardware *pcm_hardware;
0151     unsigned int prealloc_buffer_size;
0152 };
0153 
0154 int snd_dmaengine_pcm_register(struct device *dev,
0155     const struct snd_dmaengine_pcm_config *config,
0156     unsigned int flags);
0157 void snd_dmaengine_pcm_unregister(struct device *dev);
0158 
0159 int devm_snd_dmaengine_pcm_register(struct device *dev,
0160     const struct snd_dmaengine_pcm_config *config,
0161     unsigned int flags);
0162 
0163 int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
0164     struct snd_pcm_hw_params *params,
0165     struct dma_slave_config *slave_config);
0166 
0167 #define SND_DMAENGINE_PCM_DRV_NAME "snd_dmaengine_pcm"
0168 
0169 struct dmaengine_pcm {
0170     struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];
0171     const struct snd_dmaengine_pcm_config *config;
0172     struct snd_soc_component component;
0173     unsigned int flags;
0174 };
0175 
0176 static inline struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p)
0177 {
0178     return container_of(p, struct dmaengine_pcm, component);
0179 }
0180 #endif