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