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
= asoc_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(asoc_rtd_to_cpu(rtd
, 0), substream
);
205 dev_warn(component
->dev
, "no dma parameters setting\n");
206 dma_private
->params
= NULL
;
207 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
208 runtime
->dma_bytes
= totsize
;
212 if (!dma_private
->params
) {
213 dma_private
->params
= dma_params
;
214 ret
= sprd_pcm_request_dma_channel(component
,
215 substream
, channels
);
220 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
222 runtime
->dma_bytes
= totsize
;
223 sg_num
= totsize
/ period
;
224 dma_private
->dma_addr_offset
= totsize
/ channels
;
226 sg
= devm_kcalloc(component
->dev
, sg_num
, sizeof(*sg
), GFP_KERNEL
);
232 for (i
= 0; i
< channels
; i
++) {
233 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
234 struct dma_chan
*chan
= data
->chan
;
235 struct dma_slave_config config
= { };
236 struct sprd_dma_linklist link
= { };
237 enum dma_transfer_direction dir
;
238 struct scatterlist
*sgt
= sg
;
240 config
.src_maxburst
= dma_params
->fragment_len
[i
];
241 config
.src_addr_width
= dma_params
->datawidth
[i
];
242 config
.dst_addr_width
= dma_params
->datawidth
[i
];
244 config
.src_addr
= runtime
->dma_addr
+
245 i
* dma_private
->dma_addr_offset
;
246 config
.dst_addr
= dma_params
->dev_phys
[i
];
247 dir
= DMA_MEM_TO_DEV
;
249 config
.src_addr
= dma_params
->dev_phys
[i
];
250 config
.dst_addr
= runtime
->dma_addr
+
251 i
* dma_private
->dma_addr_offset
;
252 dir
= DMA_DEV_TO_MEM
;
255 sg_init_table(sgt
, sg_num
);
256 for (j
= 0; j
< sg_num
; j
++, sgt
++) {
257 u32 sg_len
= period
/ channels
;
259 sg_dma_len(sgt
) = sg_len
;
260 sg_dma_address(sgt
) = runtime
->dma_addr
+
261 i
* dma_private
->dma_addr_offset
+ sg_len
* j
;
265 * Configure the link-list address for the DMA engine link-list
268 link
.virt_addr
= (unsigned long)data
->virt
;
269 link
.phy_addr
= data
->phys
;
271 ret
= dmaengine_slave_config(chan
, &config
);
273 dev_err(component
->dev
,
274 "failed to set slave configuration: %d\n", ret
);
279 * We configure the DMA request mode, interrupt mode, channel
280 * mode and channel trigger mode by the flags.
282 flags
= SPRD_DMA_FLAGS(SPRD_DMA_CHN_MODE_NONE
, SPRD_DMA_NO_TRG
,
283 SPRD_DMA_FRAG_REQ
, SPRD_DMA_TRANS_INT
);
284 data
->desc
= chan
->device
->device_prep_slave_sg(chan
, sg
,
288 dev_err(component
->dev
, "failed to prepare slave sg\n");
293 if (!runtime
->no_period_wakeup
) {
294 data
->desc
->callback
= sprd_pcm_dma_complete
;
295 data
->desc
->callback_param
= dma_private
;
299 devm_kfree(component
->dev
, sg
);
304 devm_kfree(component
->dev
, sg
);
306 sprd_pcm_release_dma_channel(substream
);
310 static int sprd_pcm_hw_free(struct snd_soc_component
*component
,
311 struct snd_pcm_substream
*substream
)
313 snd_pcm_set_runtime_buffer(substream
, NULL
);
314 sprd_pcm_release_dma_channel(substream
);
319 static int sprd_pcm_trigger(struct snd_soc_component
*component
,
320 struct snd_pcm_substream
*substream
, int cmd
)
322 struct sprd_pcm_dma_private
*dma_private
=
323 substream
->runtime
->private_data
;
327 case SNDRV_PCM_TRIGGER_START
:
328 for (i
= 0; i
< dma_private
->hw_chan
; i
++) {
329 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
334 data
->cookie
= dmaengine_submit(data
->desc
);
335 ret
= dma_submit_error(data
->cookie
);
337 dev_err(component
->dev
,
338 "failed to submit dma request: %d\n",
343 dma_async_issue_pending(data
->chan
);
347 case SNDRV_PCM_TRIGGER_RESUME
:
348 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
349 for (i
= 0; i
< dma_private
->hw_chan
; i
++) {
350 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
353 dmaengine_resume(data
->chan
);
357 case SNDRV_PCM_TRIGGER_STOP
:
358 for (i
= 0; i
< dma_private
->hw_chan
; i
++) {
359 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
362 dmaengine_terminate_async(data
->chan
);
366 case SNDRV_PCM_TRIGGER_SUSPEND
:
367 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
368 for (i
= 0; i
< dma_private
->hw_chan
; i
++) {
369 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
372 dmaengine_pause(data
->chan
);
383 static snd_pcm_uframes_t
sprd_pcm_pointer(struct snd_soc_component
*component
,
384 struct snd_pcm_substream
*substream
)
386 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
387 struct sprd_pcm_dma_private
*dma_private
= runtime
->private_data
;
388 int pointer
[SPRD_PCM_CHANNEL_MAX
];
389 int bytes_of_pointer
= 0, sel_max
= 0, i
;
391 struct dma_tx_state state
;
392 enum dma_status status
;
394 for (i
= 0; i
< dma_private
->hw_chan
; i
++) {
395 struct sprd_pcm_dma_data
*data
= &dma_private
->data
[i
];
400 status
= dmaengine_tx_status(data
->chan
, data
->cookie
, &state
);
401 if (status
== DMA_ERROR
) {
402 dev_err(component
->dev
,
403 "failed to get dma channel %d status\n", i
);
408 * We just get current transfer address from the DMA engine, so
409 * we need convert to current pointer.
411 pointer
[i
] = state
.residue
- runtime
->dma_addr
-
412 i
* dma_private
->dma_addr_offset
;
415 bytes_of_pointer
= pointer
[i
];
416 sel_max
= pointer
[i
] < data
->pre_pointer
? 1 : 0;
418 sel_max
^= pointer
[i
] < data
->pre_pointer
? 1 : 0;
422 max(pointer
[i
], pointer
[i
- 1]) << 1;
425 min(pointer
[i
], pointer
[i
- 1]) << 1;
428 data
->pre_pointer
= pointer
[i
];
431 x
= bytes_to_frames(runtime
, bytes_of_pointer
);
432 if (x
== runtime
->buffer_size
)
438 static int sprd_pcm_mmap(struct snd_soc_component
*component
,
439 struct snd_pcm_substream
*substream
,
440 struct vm_area_struct
*vma
)
442 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
444 vma
->vm_page_prot
= pgprot_writecombine(vma
->vm_page_prot
);
445 return remap_pfn_range(vma
, vma
->vm_start
,
446 runtime
->dma_addr
>> PAGE_SHIFT
,
447 vma
->vm_end
- vma
->vm_start
,
451 static int sprd_pcm_new(struct snd_soc_component
*component
,
452 struct snd_soc_pcm_runtime
*rtd
)
454 struct snd_card
*card
= rtd
->card
->snd_card
;
455 struct snd_pcm
*pcm
= rtd
->pcm
;
456 struct snd_pcm_substream
*substream
;
459 ret
= dma_coerce_mask_and_coherent(card
->dev
, DMA_BIT_MASK(32));
463 substream
= pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
;
465 ret
= snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, card
->dev
,
466 sprd_pcm_hardware
.buffer_bytes_max
,
467 &substream
->dma_buffer
);
470 "can't alloc playback dma buffer: %d\n", ret
);
475 substream
= pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
;
477 ret
= snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, card
->dev
,
478 sprd_pcm_hardware
.buffer_bytes_max
,
479 &substream
->dma_buffer
);
482 "can't alloc capture dma buffer: %d\n", ret
);
483 snd_dma_free_pages(&pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
->dma_buffer
);
491 static void sprd_pcm_free(struct snd_soc_component
*component
,
494 struct snd_pcm_substream
*substream
;
497 for (i
= 0; i
< ARRAY_SIZE(pcm
->streams
); i
++) {
498 substream
= pcm
->streams
[i
].substream
;
500 snd_dma_free_pages(&substream
->dma_buffer
);
501 substream
->dma_buffer
.area
= NULL
;
502 substream
->dma_buffer
.addr
= 0;
507 static const struct snd_soc_component_driver sprd_soc_component
= {
509 .open
= sprd_pcm_open
,
510 .close
= sprd_pcm_close
,
511 .hw_params
= sprd_pcm_hw_params
,
512 .hw_free
= sprd_pcm_hw_free
,
513 .trigger
= sprd_pcm_trigger
,
514 .pointer
= sprd_pcm_pointer
,
515 .mmap
= sprd_pcm_mmap
,
516 .pcm_construct
= sprd_pcm_new
,
517 .pcm_destruct
= sprd_pcm_free
,
518 .compress_ops
= &sprd_platform_compress_ops
,
521 static int sprd_soc_platform_probe(struct platform_device
*pdev
)
523 struct device_node
*np
= pdev
->dev
.of_node
;
526 ret
= of_reserved_mem_device_init_by_idx(&pdev
->dev
, np
, 0);
529 "no reserved DMA memory for audio platform device\n");
531 ret
= devm_snd_soc_register_component(&pdev
->dev
, &sprd_soc_component
,
534 dev_err(&pdev
->dev
, "could not register platform:%d\n", ret
);
539 static const struct of_device_id sprd_pcm_of_match
[] = {
540 { .compatible
= "sprd,pcm-platform", },
543 MODULE_DEVICE_TABLE(of
, sprd_pcm_of_match
);
545 static struct platform_driver sprd_pcm_driver
= {
547 .name
= "sprd-pcm-audio",
548 .of_match_table
= sprd_pcm_of_match
,
551 .probe
= sprd_soc_platform_probe
,
554 module_platform_driver(sprd_pcm_driver
);
556 MODULE_DESCRIPTION("Spreadtrum ASoC PCM DMA");
557 MODULE_LICENSE("GPL v2");
558 MODULE_ALIAS("platform:sprd-audio");