1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/dmaengine.h>
15#include <linux/slab.h>
16#include <sound/pcm.h>
17#include <sound/pcm_params.h>
18#include <sound/soc.h>
19
20#include <sound/dmaengine_pcm.h>
21
22struct dmaengine_pcm_runtime_data {
23 struct dma_chan *dma_chan;
24 dma_cookie_t cookie;
25
26 unsigned int pos;
27};
28
29static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
30 const struct snd_pcm_substream *substream)
31{
32 return substream->runtime->private_data;
33}
34
35struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
36{
37 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
38
39 return prtd->dma_chan;
40}
41EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
42
43
44
45
46
47
48
49
50
51
52int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
53 const struct snd_pcm_hw_params *params,
54 struct dma_slave_config *slave_config)
55{
56 enum dma_slave_buswidth buswidth;
57 int bits;
58
59 bits = params_physical_width(params);
60 if (bits < 8 || bits > 64)
61 return -EINVAL;
62 else if (bits == 8)
63 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
64 else if (bits == 16)
65 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
66 else if (bits == 24)
67 buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
68 else if (bits <= 32)
69 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
70 else
71 buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
72
73 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
74 slave_config->direction = DMA_MEM_TO_DEV;
75 slave_config->dst_addr_width = buswidth;
76 } else {
77 slave_config->direction = DMA_DEV_TO_MEM;
78 slave_config->src_addr_width = buswidth;
79 }
80
81 slave_config->device_fc = false;
82
83 return 0;
84}
85EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104void snd_dmaengine_pcm_set_config_from_dai_data(
105 const struct snd_pcm_substream *substream,
106 const struct snd_dmaengine_dai_dma_data *dma_data,
107 struct dma_slave_config *slave_config)
108{
109 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
110 slave_config->dst_addr = dma_data->addr;
111 slave_config->dst_maxburst = dma_data->maxburst;
112 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
113 slave_config->dst_addr_width =
114 DMA_SLAVE_BUSWIDTH_UNDEFINED;
115 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
116 slave_config->dst_addr_width = dma_data->addr_width;
117 } else {
118 slave_config->src_addr = dma_data->addr;
119 slave_config->src_maxburst = dma_data->maxburst;
120 if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
121 slave_config->src_addr_width =
122 DMA_SLAVE_BUSWIDTH_UNDEFINED;
123 if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
124 slave_config->src_addr_width = dma_data->addr_width;
125 }
126
127 slave_config->peripheral_config = dma_data->peripheral_config;
128 slave_config->peripheral_size = dma_data->peripheral_size;
129}
130EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
131
132static void dmaengine_pcm_dma_complete(void *arg)
133{
134 struct snd_pcm_substream *substream = arg;
135 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
136
137 prtd->pos += snd_pcm_lib_period_bytes(substream);
138 if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
139 prtd->pos = 0;
140
141 snd_pcm_period_elapsed(substream);
142}
143
144static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
145{
146 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
147 struct dma_chan *chan = prtd->dma_chan;
148 struct dma_async_tx_descriptor *desc;
149 enum dma_transfer_direction direction;
150 unsigned long flags = DMA_CTRL_ACK;
151
152 direction = snd_pcm_substream_to_dma_direction(substream);
153
154 if (!substream->runtime->no_period_wakeup)
155 flags |= DMA_PREP_INTERRUPT;
156
157 prtd->pos = 0;
158 desc = dmaengine_prep_dma_cyclic(chan,
159 substream->runtime->dma_addr,
160 snd_pcm_lib_buffer_bytes(substream),
161 snd_pcm_lib_period_bytes(substream), direction, flags);
162
163 if (!desc)
164 return -ENOMEM;
165
166 desc->callback = dmaengine_pcm_dma_complete;
167 desc->callback_param = substream;
168 prtd->cookie = dmaengine_submit(desc);
169
170 return 0;
171}
172
173
174
175
176
177
178
179
180
181
182
183int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
184{
185 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
186 struct snd_pcm_runtime *runtime = substream->runtime;
187 int ret;
188
189 switch (cmd) {
190 case SNDRV_PCM_TRIGGER_START:
191 ret = dmaengine_pcm_prepare_and_submit(substream);
192 if (ret)
193 return ret;
194 dma_async_issue_pending(prtd->dma_chan);
195 break;
196 case SNDRV_PCM_TRIGGER_RESUME:
197 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
198 dmaengine_resume(prtd->dma_chan);
199 break;
200 case SNDRV_PCM_TRIGGER_SUSPEND:
201 if (runtime->info & SNDRV_PCM_INFO_PAUSE)
202 dmaengine_pause(prtd->dma_chan);
203 else
204 dmaengine_terminate_async(prtd->dma_chan);
205 break;
206 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
207 dmaengine_pause(prtd->dma_chan);
208 break;
209 case SNDRV_PCM_TRIGGER_STOP:
210 dmaengine_terminate_async(prtd->dma_chan);
211 break;
212 default:
213 return -EINVAL;
214 }
215
216 return 0;
217}
218EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
219
220
221
222
223
224
225
226
227snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
228{
229 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
230 return bytes_to_frames(substream->runtime, prtd->pos);
231}
232EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
233
234
235
236
237
238
239
240
241snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
242{
243 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
244 struct snd_pcm_runtime *runtime = substream->runtime;
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 runtime->delay = bytes_to_frames(runtime,
257 state.in_flight_bytes);
258 }
259
260 return bytes_to_frames(runtime, pos);
261}
262EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
263
264
265
266
267
268
269
270
271
272
273struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
274 void *filter_data)
275{
276 dma_cap_mask_t mask;
277
278 dma_cap_zero(mask);
279 dma_cap_set(DMA_SLAVE, mask);
280 dma_cap_set(DMA_CYCLIC, mask);
281
282 return dma_request_channel(mask, filter_fn, filter_data);
283}
284EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
285
286
287
288
289
290
291
292
293
294
295
296
297int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
298 struct dma_chan *chan)
299{
300 struct dmaengine_pcm_runtime_data *prtd;
301 int ret;
302
303 if (!chan)
304 return -ENXIO;
305
306 ret = snd_pcm_hw_constraint_integer(substream->runtime,
307 SNDRV_PCM_HW_PARAM_PERIODS);
308 if (ret < 0)
309 return ret;
310
311 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
312 if (!prtd)
313 return -ENOMEM;
314
315 prtd->dma_chan = chan;
316
317 substream->runtime->private_data = prtd;
318
319 return 0;
320}
321EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
337 dma_filter_fn filter_fn, void *filter_data)
338{
339 return snd_dmaengine_pcm_open(substream,
340 snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
341}
342EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
343
344
345
346
347
348int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
349{
350 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
351
352 dmaengine_synchronize(prtd->dma_chan);
353 kfree(prtd);
354
355 return 0;
356}
357EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
358
359
360
361
362
363
364
365
366int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
367{
368 struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
369
370 dmaengine_synchronize(prtd->dma_chan);
371 dma_release_channel(prtd->dma_chan);
372 kfree(prtd);
373
374 return 0;
375}
376EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
377
378
379
380
381
382
383
384
385
386
387
388
389
390int snd_dmaengine_pcm_refine_runtime_hwparams(
391 struct snd_pcm_substream *substream,
392 struct snd_dmaengine_dai_dma_data *dma_data,
393 struct snd_pcm_hardware *hw,
394 struct dma_chan *chan)
395{
396 struct dma_slave_caps dma_caps;
397 u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
398 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
399 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
400 snd_pcm_format_t i;
401 int ret = 0;
402
403 if (!hw || !chan || !dma_data)
404 return -EINVAL;
405
406 ret = dma_get_slave_caps(chan, &dma_caps);
407 if (ret == 0) {
408 if (dma_caps.cmd_pause && dma_caps.cmd_resume)
409 hw->info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
410 if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
411 hw->info |= SNDRV_PCM_INFO_BATCH;
412
413 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
414 addr_widths = dma_caps.dst_addr_widths;
415 else
416 addr_widths = dma_caps.src_addr_widths;
417 }
418
419
420
421
422
423
424
425 if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
426
427
428
429
430
431
432
433
434
435 pcm_for_each_format(i) {
436 int bits = snd_pcm_format_physical_width(i);
437
438
439
440
441
442 switch (bits) {
443 case 8:
444 case 16:
445 case 24:
446 case 32:
447 case 64:
448 if (addr_widths & (1 << (bits / 8)))
449 hw->formats |= pcm_format_to_bits(i);
450 break;
451 default:
452
453 break;
454 }
455 }
456
457 return ret;
458}
459EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_refine_runtime_hwparams);
460
461MODULE_LICENSE("GPL");
462