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