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