1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/dmaengine.h>
24#include <linux/slab.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28
29#include <sound/dmaengine_pcm.h>
30
31struct dmaengine_pcm_runtime_data {
32 struct dma_chan *dma_chan;
33 dma_cookie_t cookie;
34
35 unsigned int pos;
36};
37
38static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
39 const struct snd_pcm_substream *substream)
40{
41 return substream->runtime->private_data;
42}
43
44struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
45{
46 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
47
48 return prtd->dma_chan;
49}
50EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
51
52
53
54
55
56
57
58
59
60
61int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
62 const struct snd_pcm_hw_params *params,
63 struct dma_slave_config *slave_config)
64{
65 enum dma_slave_buswidth buswidth;
66 int bits;
67
68 bits = params_physical_width(params);
69 if (bits < 8 || bits > 64)
70 return -EINVAL;
71 else if (bits == 8)
72 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
73 else if (bits == 16)
74 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
75 else if (bits == 24)
76 buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
77 else if (bits <= 32)
78 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
79 else
80 buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
81
82 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
83 slave_config->direction = DMA_MEM_TO_DEV;
84 slave_config->dst_addr_width = buswidth;
85 } else {
86 slave_config->direction = DMA_DEV_TO_MEM;
87 slave_config->src_addr_width = buswidth;
88 }
89
90 slave_config->device_fc = false;
91
92 return 0;
93}
94EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112void snd_dmaengine_pcm_set_config_from_dai_data(
113 const struct snd_pcm_substream *substream,
114 const struct snd_dmaengine_dai_dma_data *dma_data,
115 struct dma_slave_config *slave_config)
116{
117 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
118 slave_config->dst_addr = dma_data->addr;
119 slave_config->dst_maxburst = dma_data->maxburst;
120 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
121 slave_config->dst_addr_width = dma_data->addr_width;
122 } else {
123 slave_config->src_addr = dma_data->addr;
124 slave_config->src_maxburst = dma_data->maxburst;
125 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
126 slave_config->src_addr_width = dma_data->addr_width;
127 }
128
129 slave_config->slave_id = dma_data->slave_id;
130}
131EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
132
133static void dmaengine_pcm_dma_complete(void *arg)
134{
135 struct snd_pcm_substream *substream = arg;
136 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
137
138 prtd->pos += snd_pcm_lib_period_bytes(substream);
139 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
140 prtd->pos = 0;
141
142 snd_pcm_period_elapsed(substream);
143}
144
145static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
146{
147 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
148 struct dma_chan *chan = prtd->dma_chan;
149 struct dma_async_tx_descriptor *desc;
150 enum dma_transfer_direction direction;
151 unsigned long flags = DMA_CTRL_ACK;
152
153 direction = snd_pcm_substream_to_dma_direction(substream);
154
155 if (!substream->runtime->no_period_wakeup)
156 flags |= DMA_PREP_INTERRUPT;
157
158 prtd->pos = 0;
159 desc = dmaengine_prep_dma_cyclic(chan,
160 substream->runtime->dma_addr,
161 snd_pcm_lib_buffer_bytes(substream),
162 snd_pcm_lib_period_bytes(substream), direction, flags);
163
164 if (!desc)
165 return -ENOMEM;
166
167 desc->callback = dmaengine_pcm_dma_complete;
168 desc->callback_param = substream;
169 prtd->cookie = dmaengine_submit(desc);
170
171 return 0;
172}
173
174
175
176
177
178
179
180
181
182
183
184int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
185{
186 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
187 struct snd_pcm_runtime *runtime = substream->runtime;
188 int ret;
189
190 switch (cmd) {
191 case SNDRV_PCM_TRIGGER_START:
192 ret = dmaengine_pcm_prepare_and_submit(substream);
193 if (ret)
194 return ret;
195 dma_async_issue_pending(prtd->dma_chan);
196 break;
197 case SNDRV_PCM_TRIGGER_RESUME:
198 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
199 dmaengine_resume(prtd->dma_chan);
200 break;
201 case SNDRV_PCM_TRIGGER_SUSPEND:
202 if (runtime->info & SNDRV_PCM_INFO_PAUSE)
203 dmaengine_pause(prtd->dma_chan);
204 else
205 dmaengine_terminate_all(prtd->dma_chan);
206 break;
207 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
208 dmaengine_pause(prtd->dma_chan);
209 break;
210 case SNDRV_PCM_TRIGGER_STOP:
211 dmaengine_terminate_all(prtd->dma_chan);
212 break;
213 default:
214 return -EINVAL;
215 }
216
217 return 0;
218}
219EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
220
221
222
223
224
225
226
227
228snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
229{
230 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
231 return bytes_to_frames(substream->runtime, prtd->pos);
232}
233EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
234
235
236
237
238
239
240
241
242snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
243{
244 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
245 struct dma_tx_state state;
246 enum dma_status status;
247 unsigned int buf_size;
248 unsigned int pos = 0;
249
250 status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
251 if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
252 buf_size = snd_pcm_lib_buffer_bytes(substream);
253 if (state.residue > 0 && state.residue <= buf_size)
254 pos = buf_size - state.residue;
255 }
256
257 return bytes_to_frames(substream->runtime, pos);
258}
259EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
260
261
262
263
264
265
266
267
268
269
270struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
271 void *filter_data)
272{
273 dma_cap_mask_t mask;
274
275 dma_cap_zero(mask);
276 dma_cap_set(DMA_SLAVE, mask);
277 dma_cap_set(DMA_CYCLIC, mask);
278
279 return dma_request_channel(mask, filter_fn, filter_data);
280}
281EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
282
283
284
285
286
287
288
289
290
291
292
293
294int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
295 struct dma_chan *chan)
296{
297 struct dmaengine_pcm_runtime_data *prtd;
298 int ret;
299
300 if (!chan)
301 return -ENXIO;
302
303 ret = snd_pcm_hw_constraint_integer(substream->runtime,
304 SNDRV_PCM_HW_PARAM_PERIODS);
305 if (ret < 0)
306 return ret;
307
308 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
309 if (!prtd)
310 return -ENOMEM;
311
312 prtd->dma_chan = chan;
313
314 substream->runtime->private_data = prtd;
315
316 return 0;
317}
318EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
334 dma_filter_fn filter_fn, void *filter_data)
335{
336 return snd_dmaengine_pcm_open(substream,
337 snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
338}
339EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
340
341
342
343
344
345int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
346{
347 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
348
349 kfree(prtd);
350
351 return 0;
352}
353EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
354
355
356
357
358
359
360
361int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
362{
363 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
364
365 dma_release_channel(prtd->dma_chan);
366
367 return snd_dmaengine_pcm_close(substream);
368}
369EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
370
371MODULE_LICENSE("GPL");
372