1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2019 Spreadtrum Communications Inc.
4 #include <linux/dma-mapping.h>
5 #include <linux/dmaengine.h>
6 #include <linux/dma/sprd-dma.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/of_reserved_mem.h>
10 #include <linux/platform_device.h>
11 #include <sound/pcm.h>
12 #include <sound/pcm_params.h>
13 #include <sound/soc.h>
15 #include "sprd-pcm-dma.h"
17 #define SPRD_PCM_DMA_LINKLIST_SIZE 64
18 #define SPRD_PCM_DMA_BRUST_LEN 640
20 struct sprd_pcm_dma_data
{
21 struct dma_chan
*chan
;
22 struct dma_async_tx_descriptor
*desc
;
29 struct sprd_pcm_dma_private
{
30 struct snd_pcm_substream
*substream
;
31 struct sprd_pcm_dma_params
*params
;
32 struct sprd_pcm_dma_data data
[SPRD_PCM_CHANNEL_MAX
];
37 static const struct snd_pcm_hardware sprd_pcm_hardware
= {
38 .info
= SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
|
39 SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_PAUSE
|
40 SNDRV_PCM_INFO_RESUME
| SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
,
41 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_LE
,
42 .period_bytes_min
= 1,
43 .period_bytes_max
= 64 * 1024,
45 .periods_max
= PAGE_SIZE
/ SPRD_PCM_DMA_LINKLIST_SIZE
,
46 .buffer_bytes_max
= 64 * 1024,
49 static int sprd_pcm_open(struct snd_soc_component
*component
,
50 struct snd_pcm_substream
*substream
)
52 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
53 struct device
*dev
= component
->dev
;
54 struct sprd_pcm_dma_private
*dma_private
;
55 int hw_chan
= SPRD_PCM_CHANNEL_MAX
;
58 snd_soc_set_runtime_hwparams(substream
, &sprd_pcm_hardware
);
60 ret
= snd_pcm_hw_constraint_step(runtime
, 0,
61 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
62 SPRD_PCM_DMA_BRUST_LEN
);
66 ret
= snd_pcm_hw_constraint_step(runtime
, 0,
67 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
68 SPRD_PCM_DMA_BRUST_LEN
);
72 ret
= snd_pcm_hw_constraint_integer(runtime
,
73 SNDRV_PCM_HW_PARAM_PERIODS
);
77 dma_private
= devm_kzalloc(dev
, sizeof(*dma_private
), GFP_KERNEL
);
81 size
= runtime
->hw
.periods_max
* SPRD_PCM_DMA_LINKLIST_SIZE
;
83 for (i
= 0; i
< hw_chan
; i
++) {
84 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
86 data
->virt
= dmam_alloc_coherent(dev
, size
, &data
->phys
,
94 dma_private
->hw_chan
= hw_chan
;
95 runtime
->private_data
= dma_private
;
96 dma_private
->substream
= substream
;
101 for (i
= 0; i
< hw_chan
; i
++) {
102 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
105 dmam_free_coherent(dev
, size
, data
->virt
, data
->phys
);
108 devm_kfree(dev
, dma_private
);
112 static int sprd_pcm_close(struct snd_soc_component
*component
,
113 struct snd_pcm_substream
*substream
)
115 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
116 struct sprd_pcm_dma_private
*dma_private
= runtime
->private_data
;
117 struct device
*dev
= component
->dev
;
118 int size
= runtime
->hw
.periods_max
* SPRD_PCM_DMA_LINKLIST_SIZE
;
121 for (i
= 0; i
< dma_private
->hw_chan
; i
++) {
122 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
124 dmam_free_coherent(dev
, size
, data
->virt
, data
->phys
);
127 devm_kfree(dev
, dma_private
);
132 static void sprd_pcm_dma_complete(void *data
)
134 struct sprd_pcm_dma_private
*dma_private
= data
;
135 struct snd_pcm_substream
*substream
= dma_private
->substream
;
137 snd_pcm_period_elapsed(substream
);
140 static void sprd_pcm_release_dma_channel(struct snd_pcm_substream
*substream
)
142 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
143 struct sprd_pcm_dma_private
*dma_private
= runtime
->private_data
;
146 for (i
= 0; i
< SPRD_PCM_CHANNEL_MAX
; i
++) {
147 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
150 dma_release_channel(data
->chan
);
156 static int sprd_pcm_request_dma_channel(struct snd_soc_component
*component
,
157 struct snd_pcm_substream
*substream
,
160 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
161 struct sprd_pcm_dma_private
*dma_private
= runtime
->private_data
;
162 struct device
*dev
= component
->dev
;
163 struct sprd_pcm_dma_params
*dma_params
= dma_private
->params
;
166 if (channels
> SPRD_PCM_CHANNEL_MAX
) {
167 dev_err(dev
, "invalid dma channel number:%d\n", channels
);
171 for (i
= 0; i
< channels
; i
++) {
172 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
174 data
->chan
= dma_request_slave_channel(dev
,
175 dma_params
->chan_name
[i
]);
177 dev_err(dev
, "failed to request dma channel:%s\n",
178 dma_params
->chan_name
[i
]);
179 sprd_pcm_release_dma_channel(substream
);
187 static int sprd_pcm_hw_params(struct snd_soc_component
*component
,
188 struct snd_pcm_substream
*substream
,
189 struct snd_pcm_hw_params
*params
)
191 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
192 struct sprd_pcm_dma_private
*dma_private
= runtime
->private_data
;
193 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
194 struct sprd_pcm_dma_params
*dma_params
;
195 size_t totsize
= params_buffer_bytes(params
);
196 size_t period
= params_period_bytes(params
);
197 int channels
= params_channels(params
);
198 int is_playback
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
199 struct scatterlist
*sg
;
201 int ret
, i
, j
, sg_num
;
203 dma_params
= snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd
, 0), substream
);
205 dev_warn(component
->dev
, "no dma parameters setting\n");
206 dma_private
->params
= NULL
;
210 if (!dma_private
->params
) {
211 dma_private
->params
= dma_params
;
212 ret
= sprd_pcm_request_dma_channel(component
,
213 substream
, channels
);
218 sg_num
= totsize
/ period
;
219 dma_private
->dma_addr_offset
= totsize
/ channels
;
221 sg
= devm_kcalloc(component
->dev
, sg_num
, sizeof(*sg
), GFP_KERNEL
);
227 for (i
= 0; i
< channels
; i
++) {
228 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
229 struct dma_chan
*chan
= data
->chan
;
230 struct dma_slave_config config
= { };
231 struct sprd_dma_linklist link
= { };
232 enum dma_transfer_direction dir
;
233 struct scatterlist
*sgt
= sg
;
235 config
.src_maxburst
= dma_params
->fragment_len
[i
];
236 config
.src_addr_width
= dma_params
->datawidth
[i
];
237 config
.dst_addr_width
= dma_params
->datawidth
[i
];
239 config
.src_addr
= runtime
->dma_addr
+
240 i
* dma_private
->dma_addr_offset
;
241 config
.dst_addr
= dma_params
->dev_phys
[i
];
242 dir
= DMA_MEM_TO_DEV
;
244 config
.src_addr
= dma_params
->dev_phys
[i
];
245 config
.dst_addr
= runtime
->dma_addr
+
246 i
* dma_private
->dma_addr_offset
;
247 dir
= DMA_DEV_TO_MEM
;
250 sg_init_table(sgt
, sg_num
);
251 for (j
= 0; j
< sg_num
; j
++, sgt
++) {
252 u32 sg_len
= period
/ channels
;
254 sg_dma_len(sgt
) = sg_len
;
255 sg_dma_address(sgt
) = runtime
->dma_addr
+
256 i
* dma_private
->dma_addr_offset
+ sg_len
* j
;
260 * Configure the link-list address for the DMA engine link-list
263 link
.virt_addr
= (unsigned long)data
->virt
;
264 link
.phy_addr
= data
->phys
;
266 ret
= dmaengine_slave_config(chan
, &config
);
268 dev_err(component
->dev
,
269 "failed to set slave configuration: %d\n", ret
);
274 * We configure the DMA request mode, interrupt mode, channel
275 * mode and channel trigger mode by the flags.
277 flags
= SPRD_DMA_FLAGS(SPRD_DMA_CHN_MODE_NONE
, SPRD_DMA_NO_TRG
,
278 SPRD_DMA_FRAG_REQ
, SPRD_DMA_TRANS_INT
);
279 data
->desc
= chan
->device
->device_prep_slave_sg(chan
, sg
,
283 dev_err(component
->dev
, "failed to prepare slave sg\n");
288 if (!runtime
->no_period_wakeup
) {
289 data
->desc
->callback
= sprd_pcm_dma_complete
;
290 data
->desc
->callback_param
= dma_private
;
294 devm_kfree(component
->dev
, sg
);
299 devm_kfree(component
->dev
, sg
);
301 sprd_pcm_release_dma_channel(substream
);
305 static int sprd_pcm_hw_free(struct snd_soc_component
*component
,
306 struct snd_pcm_substream
*substream
)
308 sprd_pcm_release_dma_channel(substream
);
313 static int sprd_pcm_trigger(struct snd_soc_component
*component
,
314 struct snd_pcm_substream
*substream
, int cmd
)
316 struct sprd_pcm_dma_private
*dma_private
=
317 substream
->runtime
->private_data
;
321 case SNDRV_PCM_TRIGGER_START
:
322 for (i
= 0; i
< dma_private
->hw_chan
; i
++) {
323 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
328 data
->cookie
= dmaengine_submit(data
->desc
);
329 ret
= dma_submit_error(data
->cookie
);
331 dev_err(component
->dev
,
332 "failed to submit dma request: %d\n",
337 dma_async_issue_pending(data
->chan
);
341 case SNDRV_PCM_TRIGGER_RESUME
:
342 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
343 for (i
= 0; i
< dma_private
->hw_chan
; i
++) {
344 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
347 dmaengine_resume(data
->chan
);
351 case SNDRV_PCM_TRIGGER_STOP
:
352 for (i
= 0; i
< dma_private
->hw_chan
; i
++) {
353 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
356 dmaengine_terminate_async(data
->chan
);
360 case SNDRV_PCM_TRIGGER_SUSPEND
:
361 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
362 for (i
= 0; i
< dma_private
->hw_chan
; i
++) {
363 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
366 dmaengine_pause(data
->chan
);
377 static snd_pcm_uframes_t
sprd_pcm_pointer(struct snd_soc_component
*component
,
378 struct snd_pcm_substream
*substream
)
380 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
381 struct sprd_pcm_dma_private
*dma_private
= runtime
->private_data
;
382 int pointer
[SPRD_PCM_CHANNEL_MAX
];
383 int bytes_of_pointer
= 0, sel_max
= 0, i
;
385 struct dma_tx_state state
;
386 enum dma_status status
;
388 for (i
= 0; i
< dma_private
->hw_chan
; i
++) {
389 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
394 status
= dmaengine_tx_status(data
->chan
, data
->cookie
, &state
);
395 if (status
== DMA_ERROR
) {
396 dev_err(component
->dev
,
397 "failed to get dma channel %d status\n", i
);
402 * We just get current transfer address from the DMA engine, so
403 * we need convert to current pointer.
405 pointer
[i
] = state
.residue
- runtime
->dma_addr
-
406 i
* dma_private
->dma_addr_offset
;
409 bytes_of_pointer
= pointer
[i
];
410 sel_max
= pointer
[i
] < data
->pre_pointer
? 1 : 0;
412 sel_max
^= pointer
[i
] < data
->pre_pointer
? 1 : 0;
416 max(pointer
[i
], pointer
[i
- 1]) << 1;
419 min(pointer
[i
], pointer
[i
- 1]) << 1;
422 data
->pre_pointer
= pointer
[i
];
425 x
= bytes_to_frames(runtime
, bytes_of_pointer
);
426 if (x
== runtime
->buffer_size
)
432 static int sprd_pcm_new(struct snd_soc_component
*component
,
433 struct snd_soc_pcm_runtime
*rtd
)
435 struct snd_card
*card
= rtd
->card
->snd_card
;
436 struct snd_pcm
*pcm
= rtd
->pcm
;
439 ret
= dma_coerce_mask_and_coherent(card
->dev
, DMA_BIT_MASK(32));
443 return snd_pcm_set_fixed_buffer_all(pcm
, SNDRV_DMA_TYPE_DEV
,
445 sprd_pcm_hardware
.buffer_bytes_max
);
448 static const struct snd_soc_component_driver sprd_soc_component
= {
450 .open
= sprd_pcm_open
,
451 .close
= sprd_pcm_close
,
452 .hw_params
= sprd_pcm_hw_params
,
453 .hw_free
= sprd_pcm_hw_free
,
454 .trigger
= sprd_pcm_trigger
,
455 .pointer
= sprd_pcm_pointer
,
456 .pcm_construct
= sprd_pcm_new
,
457 .compress_ops
= &sprd_platform_compress_ops
,
460 static int sprd_soc_platform_probe(struct platform_device
*pdev
)
462 struct device_node
*np
= pdev
->dev
.of_node
;
465 ret
= of_reserved_mem_device_init_by_idx(&pdev
->dev
, np
, 0);
468 "no reserved DMA memory for audio platform device\n");
470 ret
= devm_snd_soc_register_component(&pdev
->dev
, &sprd_soc_component
,
473 dev_err(&pdev
->dev
, "could not register platform:%d\n", ret
);
478 static const struct of_device_id sprd_pcm_of_match
[] = {
479 { .compatible
= "sprd,pcm-platform", },
482 MODULE_DEVICE_TABLE(of
, sprd_pcm_of_match
);
484 static struct platform_driver sprd_pcm_driver
= {
486 .name
= "sprd-pcm-audio",
487 .of_match_table
= sprd_pcm_of_match
,
490 .probe
= sprd_soc_platform_probe
,
493 module_platform_driver(sprd_pcm_driver
);
495 MODULE_DESCRIPTION("Spreadtrum ASoC PCM DMA");
496 MODULE_LICENSE("GPL v2");
497 MODULE_ALIAS("platform:sprd-audio");