1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Copyright (C) 2012, Analog Devices Inc.
4*4882a593Smuzhiyun * Author: Lars-Peter Clausen <lars@metafoo.de>
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #ifndef __SOUND_DMAENGINE_PCM_H__
8*4882a593Smuzhiyun #define __SOUND_DMAENGINE_PCM_H__
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <sound/pcm.h>
11*4882a593Smuzhiyun #include <sound/soc.h>
12*4882a593Smuzhiyun #include <linux/dmaengine.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun /**
15*4882a593Smuzhiyun * snd_pcm_substream_to_dma_direction - Get dma_transfer_direction for a PCM
16*4882a593Smuzhiyun * substream
17*4882a593Smuzhiyun * @substream: PCM substream
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun static inline enum dma_transfer_direction
snd_pcm_substream_to_dma_direction(const struct snd_pcm_substream * substream)20*4882a593Smuzhiyun snd_pcm_substream_to_dma_direction(const struct snd_pcm_substream *substream)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
23*4882a593Smuzhiyun return DMA_MEM_TO_DEV;
24*4882a593Smuzhiyun else
25*4882a593Smuzhiyun return DMA_DEV_TO_MEM;
26*4882a593Smuzhiyun }
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
29*4882a593Smuzhiyun const struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config);
30*4882a593Smuzhiyun int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd);
31*4882a593Smuzhiyun snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream);
32*4882a593Smuzhiyun snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream);
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
35*4882a593Smuzhiyun struct dma_chan *chan);
36*4882a593Smuzhiyun int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream);
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
39*4882a593Smuzhiyun dma_filter_fn filter_fn, void *filter_data);
40*4882a593Smuzhiyun int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
43*4882a593Smuzhiyun void *filter_data);
44*4882a593Smuzhiyun struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream);
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * The DAI supports packed transfers, eg 2 16-bit samples in a 32-bit word.
48*4882a593Smuzhiyun * If this flag is set the dmaengine driver won't put any restriction on
49*4882a593Smuzhiyun * the supported sample formats and set the DMA transfer size to undefined.
50*4882a593Smuzhiyun * The DAI driver is responsible to disable any unsupported formats in it's
51*4882a593Smuzhiyun * configuration and catch corner cases that are not already handled in
52*4882a593Smuzhiyun * the ALSA core.
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun #define SND_DMAENGINE_PCM_DAI_FLAG_PACK BIT(0)
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /**
57*4882a593Smuzhiyun * struct snd_dmaengine_dai_dma_data - DAI DMA configuration data
58*4882a593Smuzhiyun * @addr: Address of the DAI data source or destination register.
59*4882a593Smuzhiyun * @addr_width: Width of the DAI data source or destination register.
60*4882a593Smuzhiyun * @maxburst: Maximum number of words(note: words, as in units of the
61*4882a593Smuzhiyun * src_addr_width member, not bytes) that can be send to or received from the
62*4882a593Smuzhiyun * DAI in one burst.
63*4882a593Smuzhiyun * @slave_id: Slave requester id for the DMA channel.
64*4882a593Smuzhiyun * @filter_data: Custom DMA channel filter data, this will usually be used when
65*4882a593Smuzhiyun * requesting the DMA channel.
66*4882a593Smuzhiyun * @chan_name: Custom channel name to use when requesting DMA channel.
67*4882a593Smuzhiyun * @fifo_size: FIFO size of the DAI controller in bytes
68*4882a593Smuzhiyun * @flags: PCM_DAI flags, only SND_DMAENGINE_PCM_DAI_FLAG_PACK for now
69*4882a593Smuzhiyun * @peripheral_config: peripheral configuration for programming peripheral
70*4882a593Smuzhiyun * for dmaengine transfer
71*4882a593Smuzhiyun * @peripheral_size: peripheral configuration buffer size
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun struct snd_dmaengine_dai_dma_data {
74*4882a593Smuzhiyun dma_addr_t addr;
75*4882a593Smuzhiyun enum dma_slave_buswidth addr_width;
76*4882a593Smuzhiyun u32 maxburst;
77*4882a593Smuzhiyun unsigned int slave_id;
78*4882a593Smuzhiyun void *filter_data;
79*4882a593Smuzhiyun const char *chan_name;
80*4882a593Smuzhiyun unsigned int fifo_size;
81*4882a593Smuzhiyun unsigned int flags;
82*4882a593Smuzhiyun void *peripheral_config;
83*4882a593Smuzhiyun size_t peripheral_size;
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun void snd_dmaengine_pcm_set_config_from_dai_data(
87*4882a593Smuzhiyun const struct snd_pcm_substream *substream,
88*4882a593Smuzhiyun const struct snd_dmaengine_dai_dma_data *dma_data,
89*4882a593Smuzhiyun struct dma_slave_config *config);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun int snd_dmaengine_pcm_refine_runtime_hwparams(
92*4882a593Smuzhiyun struct snd_pcm_substream *substream,
93*4882a593Smuzhiyun struct snd_dmaengine_dai_dma_data *dma_data,
94*4882a593Smuzhiyun struct snd_pcm_hardware *hw,
95*4882a593Smuzhiyun struct dma_chan *chan);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun * Try to request the DMA channel using compat_request_channel or
99*4882a593Smuzhiyun * compat_filter_fn if it couldn't be requested through devicetree.
100*4882a593Smuzhiyun */
101*4882a593Smuzhiyun #define SND_DMAENGINE_PCM_FLAG_COMPAT BIT(0)
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun * Don't try to request the DMA channels through devicetree. This flag only
104*4882a593Smuzhiyun * makes sense if SND_DMAENGINE_PCM_FLAG_COMPAT is set as well.
105*4882a593Smuzhiyun */
106*4882a593Smuzhiyun #define SND_DMAENGINE_PCM_FLAG_NO_DT BIT(1)
107*4882a593Smuzhiyun /*
108*4882a593Smuzhiyun * The PCM is half duplex and the DMA channel is shared between capture and
109*4882a593Smuzhiyun * playback.
110*4882a593Smuzhiyun */
111*4882a593Smuzhiyun #define SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX BIT(3)
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /**
114*4882a593Smuzhiyun * struct snd_dmaengine_pcm_config - Configuration data for dmaengine based PCM
115*4882a593Smuzhiyun * @prepare_slave_config: Callback used to fill in the DMA slave_config for a
116*4882a593Smuzhiyun * PCM substream. Will be called from the PCM drivers hwparams callback.
117*4882a593Smuzhiyun * @compat_request_channel: Callback to request a DMA channel for platforms
118*4882a593Smuzhiyun * which do not use devicetree.
119*4882a593Smuzhiyun * @process: Callback used to apply processing on samples transferred from/to
120*4882a593Smuzhiyun * user space.
121*4882a593Smuzhiyun * @compat_filter_fn: Will be used as the filter function when requesting a
122*4882a593Smuzhiyun * channel for platforms which do not use devicetree. The filter parameter
123*4882a593Smuzhiyun * will be the DAI's DMA data.
124*4882a593Smuzhiyun * @dma_dev: If set, request DMA channel on this device rather than the DAI
125*4882a593Smuzhiyun * device.
126*4882a593Smuzhiyun * @chan_names: If set, these custom DMA channel names will be requested at
127*4882a593Smuzhiyun * registration time.
128*4882a593Smuzhiyun * @pcm_hardware: snd_pcm_hardware struct to be used for the PCM.
129*4882a593Smuzhiyun * @prealloc_buffer_size: Size of the preallocated audio buffer.
130*4882a593Smuzhiyun *
131*4882a593Smuzhiyun * Note: If both compat_request_channel and compat_filter_fn are set
132*4882a593Smuzhiyun * compat_request_channel will be used to request the channel and
133*4882a593Smuzhiyun * compat_filter_fn will be ignored. Otherwise the channel will be requested
134*4882a593Smuzhiyun * using dma_request_channel with compat_filter_fn as the filter function.
135*4882a593Smuzhiyun */
136*4882a593Smuzhiyun struct snd_dmaengine_pcm_config {
137*4882a593Smuzhiyun int (*prepare_slave_config)(struct snd_pcm_substream *substream,
138*4882a593Smuzhiyun struct snd_pcm_hw_params *params,
139*4882a593Smuzhiyun struct dma_slave_config *slave_config);
140*4882a593Smuzhiyun struct dma_chan *(*compat_request_channel)(
141*4882a593Smuzhiyun struct snd_soc_pcm_runtime *rtd,
142*4882a593Smuzhiyun struct snd_pcm_substream *substream);
143*4882a593Smuzhiyun int (*process)(struct snd_pcm_substream *substream,
144*4882a593Smuzhiyun int channel, unsigned long hwoff,
145*4882a593Smuzhiyun void *buf, unsigned long bytes);
146*4882a593Smuzhiyun dma_filter_fn compat_filter_fn;
147*4882a593Smuzhiyun struct device *dma_dev;
148*4882a593Smuzhiyun const char *chan_names[SNDRV_PCM_STREAM_LAST + 1];
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun const struct snd_pcm_hardware *pcm_hardware;
151*4882a593Smuzhiyun unsigned int prealloc_buffer_size;
152*4882a593Smuzhiyun };
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun int snd_dmaengine_pcm_register(struct device *dev,
155*4882a593Smuzhiyun const struct snd_dmaengine_pcm_config *config,
156*4882a593Smuzhiyun unsigned int flags);
157*4882a593Smuzhiyun void snd_dmaengine_pcm_unregister(struct device *dev);
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun int devm_snd_dmaengine_pcm_register(struct device *dev,
160*4882a593Smuzhiyun const struct snd_dmaengine_pcm_config *config,
161*4882a593Smuzhiyun unsigned int flags);
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
164*4882a593Smuzhiyun struct snd_pcm_hw_params *params,
165*4882a593Smuzhiyun struct dma_slave_config *slave_config);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun #define SND_DMAENGINE_PCM_DRV_NAME "snd_dmaengine_pcm"
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun struct dmaengine_pcm {
170*4882a593Smuzhiyun struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];
171*4882a593Smuzhiyun const struct snd_dmaengine_pcm_config *config;
172*4882a593Smuzhiyun struct snd_soc_component component;
173*4882a593Smuzhiyun unsigned int flags;
174*4882a593Smuzhiyun };
175*4882a593Smuzhiyun
soc_component_to_pcm(struct snd_soc_component * p)176*4882a593Smuzhiyun static inline struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun return container_of(p, struct dmaengine_pcm, component);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun #endif
181