1 // SPDX-License-Identifier: GPL-2.0
3 // Loongson ALSA SoC Platform (DMA) driver
5 // Copyright (C) 2023 Loongson Technology Corporation Limited
6 // Author: Yingkun Meng <mengyingkun@loongson.cn>
9 #include <linux/module.h>
10 #include <linux/io-64-nonatomic-lo-hi.h>
11 #include <linux/delay.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/dma-mapping.h>
14 #include <sound/soc.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
17 #include "loongson_i2s.h"
19 /* DMA dma_order Register */
20 #define DMA_ORDER_STOP (1 << 4) /* DMA stop */
21 #define DMA_ORDER_START (1 << 3) /* DMA start */
22 #define DMA_ORDER_ASK_VALID (1 << 2) /* DMA ask valid flag */
23 #define DMA_ORDER_AXI_UNCO (1 << 1) /* Uncache access */
24 #define DMA_ORDER_ADDR_64 (1 << 0) /* 64bits address support */
26 #define DMA_ORDER_ASK_MASK (~0x1fUL) /* Ask addr mask */
27 #define DMA_ORDER_CTRL_MASK (0x0fUL) /* Control mask */
30 * DMA registers descriptor.
32 struct loongson_dma_desc {
33 u32 order; /* Next descriptor address register */
34 u32 saddr; /* Source address register */
35 u32 daddr; /* Device address register */
36 u32 length; /* Total length register */
37 u32 step_length; /* Memory stride register */
38 u32 step_times; /* Repeat time register */
39 u32 cmd; /* Command register */
40 u32 stats; /* Status register */
41 u32 order_hi; /* Next descriptor high address register */
42 u32 saddr_hi; /* High source address register */
43 u32 res[6]; /* Reserved */
46 struct loongson_runtime_data {
47 struct loongson_dma_data *dma_data;
49 struct loongson_dma_desc *dma_desc_arr;
50 dma_addr_t dma_desc_arr_phy;
51 int dma_desc_arr_size;
53 struct loongson_dma_desc *dma_pos_desc;
54 dma_addr_t dma_pos_desc_phy;
57 static const struct snd_pcm_hardware ls_pcm_hardware = {
58 .info = SNDRV_PCM_INFO_MMAP |
59 SNDRV_PCM_INFO_INTERLEAVED |
60 SNDRV_PCM_INFO_MMAP_VALID |
61 SNDRV_PCM_INFO_RESUME |
63 .formats = (SNDRV_PCM_FMTBIT_S8 |
64 SNDRV_PCM_FMTBIT_S16_LE |
65 SNDRV_PCM_FMTBIT_S20_3LE |
66 SNDRV_PCM_FMTBIT_S24_LE),
67 .period_bytes_min = 128,
68 .period_bytes_max = 128 * 1024,
70 .periods_max = PAGE_SIZE / sizeof(struct loongson_dma_desc),
71 .buffer_bytes_max = 1024 * 1024,
75 loongson_dma_desc *dma_desc_save(struct loongson_runtime_data *prtd)
77 void __iomem *order_reg = prtd->dma_data->order_addr;
80 val = (u64)prtd->dma_pos_desc_phy & DMA_ORDER_ASK_MASK;
81 val |= (readq(order_reg) & DMA_ORDER_CTRL_MASK);
82 val |= DMA_ORDER_ASK_VALID;
83 writeq(val, order_reg);
85 while (readl(order_reg) & DMA_ORDER_ASK_VALID)
88 return prtd->dma_pos_desc;
91 static int loongson_pcm_trigger(struct snd_soc_component *component,
92 struct snd_pcm_substream *substream, int cmd)
94 struct loongson_runtime_data *prtd = substream->runtime->private_data;
95 struct device *dev = substream->pcm->card->dev;
96 void __iomem *order_reg = prtd->dma_data->order_addr;
101 case SNDRV_PCM_TRIGGER_START:
102 case SNDRV_PCM_TRIGGER_RESUME:
103 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
104 val = prtd->dma_pos_desc_phy & DMA_ORDER_ASK_MASK;
105 if (dev->coherent_dma_mask == DMA_BIT_MASK(64))
106 val |= DMA_ORDER_ADDR_64;
108 val &= ~DMA_ORDER_ADDR_64;
109 val |= (readq(order_reg) & DMA_ORDER_CTRL_MASK);
110 val |= DMA_ORDER_START;
111 writeq(val, order_reg);
113 while ((readl(order_reg) & DMA_ORDER_START))
116 case SNDRV_PCM_TRIGGER_STOP:
117 case SNDRV_PCM_TRIGGER_SUSPEND:
118 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
122 val = readq(order_reg) | DMA_ORDER_STOP;
123 writeq(val, order_reg);
128 dev_err(dev, "Invalid pcm trigger operation\n");
135 static int loongson_pcm_hw_params(struct snd_soc_component *component,
136 struct snd_pcm_substream *substream,
137 struct snd_pcm_hw_params *params)
139 struct snd_pcm_runtime *runtime = substream->runtime;
140 struct device *dev = substream->pcm->card->dev;
141 struct loongson_runtime_data *prtd = runtime->private_data;
142 size_t buf_len = params_buffer_bytes(params);
143 size_t period_len = params_period_bytes(params);
144 dma_addr_t order_addr, mem_addr;
145 struct loongson_dma_desc *desc;
149 if (buf_len % period_len) {
150 dev_err(dev, "buf len not multiply of period len\n");
154 num_periods = buf_len / period_len;
155 if (!num_periods || num_periods > prtd->dma_desc_arr_size) {
156 dev_err(dev, "dma data too small or too big\n");
160 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
161 runtime->dma_bytes = buf_len;
163 /* initialize dma descriptor array */
164 mem_addr = runtime->dma_addr;
165 order_addr = prtd->dma_desc_arr_phy;
166 for (i = 0; i < num_periods; i++) {
167 desc = &prtd->dma_desc_arr[i];
169 /* next descriptor physical address */
170 order_addr += sizeof(*desc);
171 desc->order = lower_32_bits(order_addr | BIT(0));
172 desc->order_hi = upper_32_bits(order_addr);
174 desc->saddr = lower_32_bits(mem_addr);
175 desc->saddr_hi = upper_32_bits(mem_addr);
176 desc->daddr = prtd->dma_data->dev_addr;
179 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
180 desc->cmd |= BIT(12);
182 desc->length = period_len >> 2;
183 desc->step_length = 0;
184 desc->step_times = 1;
186 mem_addr += period_len;
188 desc = &prtd->dma_desc_arr[num_periods - 1];
189 desc->order = lower_32_bits(prtd->dma_desc_arr_phy | BIT(0));
190 desc->order_hi = upper_32_bits(prtd->dma_desc_arr_phy);
192 /* init position descriptor */
193 *prtd->dma_pos_desc = *prtd->dma_desc_arr;
198 static snd_pcm_uframes_t
199 loongson_pcm_pointer(struct snd_soc_component *component,
200 struct snd_pcm_substream *substream)
202 struct snd_pcm_runtime *runtime = substream->runtime;
203 struct loongson_runtime_data *prtd = runtime->private_data;
204 struct loongson_dma_desc *desc;
208 desc = dma_desc_save(prtd);
209 addr = ((u64)desc->saddr_hi << 32) | desc->saddr;
211 x = bytes_to_frames(runtime, addr - runtime->dma_addr);
212 if (x == runtime->buffer_size)
217 static irqreturn_t loongson_pcm_dma_irq(int irq, void *devid)
219 struct snd_pcm_substream *substream = devid;
221 snd_pcm_period_elapsed(substream);
225 static int loongson_pcm_open(struct snd_soc_component *component,
226 struct snd_pcm_substream *substream)
228 struct snd_pcm_runtime *runtime = substream->runtime;
229 struct snd_soc_pcm_runtime *rtd = substream->private_data;
230 struct snd_card *card = substream->pcm->card;
231 struct loongson_runtime_data *prtd;
232 struct loongson_dma_data *dma_data;
236 * For mysterious reasons (and despite what the manual says)
237 * playback samples are lost if the DMA count is not a multiple
238 * of the DMA burst size. Let's add a rule to enforce that.
240 snd_pcm_hw_constraint_step(runtime, 0,
241 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 128);
242 snd_pcm_hw_constraint_step(runtime, 0,
243 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 128);
244 snd_pcm_hw_constraint_integer(substream->runtime,
245 SNDRV_PCM_HW_PARAM_PERIODS);
246 snd_soc_set_runtime_hwparams(substream, &ls_pcm_hardware);
248 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
252 prtd->dma_desc_arr = dma_alloc_coherent(card->dev, PAGE_SIZE,
253 &prtd->dma_desc_arr_phy,
255 if (!prtd->dma_desc_arr) {
259 prtd->dma_desc_arr_size = PAGE_SIZE / sizeof(*prtd->dma_desc_arr);
261 prtd->dma_pos_desc = dma_alloc_coherent(card->dev,
262 sizeof(*prtd->dma_pos_desc),
263 &prtd->dma_pos_desc_phy,
265 if (!prtd->dma_pos_desc) {
270 dma_data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);
271 prtd->dma_data = dma_data;
273 substream->runtime->private_data = prtd;
277 dma_free_coherent(card->dev, PAGE_SIZE, prtd->dma_desc_arr,
278 prtd->dma_desc_arr_phy);
285 static int loongson_pcm_close(struct snd_soc_component *component,
286 struct snd_pcm_substream *substream)
288 struct snd_card *card = substream->pcm->card;
289 struct loongson_runtime_data *prtd = substream->runtime->private_data;
291 dma_free_coherent(card->dev, PAGE_SIZE, prtd->dma_desc_arr,
292 prtd->dma_desc_arr_phy);
294 dma_free_coherent(card->dev, sizeof(*prtd->dma_pos_desc),
295 prtd->dma_pos_desc, prtd->dma_pos_desc_phy);
301 static int loongson_pcm_mmap(struct snd_soc_component *component,
302 struct snd_pcm_substream *substream,
303 struct vm_area_struct *vma)
305 return remap_pfn_range(vma, vma->vm_start,
306 substream->dma_buffer.addr >> PAGE_SHIFT,
307 vma->vm_end - vma->vm_start, vma->vm_page_prot);
310 static int loongson_pcm_new(struct snd_soc_component *component,
311 struct snd_soc_pcm_runtime *rtd)
313 struct snd_card *card = rtd->card->snd_card;
314 struct snd_pcm_substream *substream;
315 struct loongson_dma_data *dma_data;
319 for_each_pcm_streams(i) {
320 substream = rtd->pcm->streams[i].substream;
324 dma_data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0),
326 ret = devm_request_irq(card->dev, dma_data->irq,
327 loongson_pcm_dma_irq,
328 IRQF_TRIGGER_HIGH, LS_I2S_DRVNAME,
331 dev_err(card->dev, "request irq for DMA failed\n");
336 return snd_pcm_set_fixed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
338 ls_pcm_hardware.buffer_bytes_max);
341 const struct snd_soc_component_driver loongson_i2s_component = {
342 .name = LS_I2S_DRVNAME,
343 .open = loongson_pcm_open,
344 .close = loongson_pcm_close,
345 .hw_params = loongson_pcm_hw_params,
346 .trigger = loongson_pcm_trigger,
347 .pointer = loongson_pcm_pointer,
348 .mmap = loongson_pcm_mmap,
349 .pcm_construct = loongson_pcm_new,