Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
v3.5.6
  1/*
  2 * This program is free software; you can redistribute it and/or modify
  3 * it under the terms of the GNU General Public License version 2 as
  4 * published by the Free Software Foundation.
  5 */
  6
  7#include <linux/slab.h>
  8#include <linux/module.h>
  9#include <linux/dma-mapping.h>
 
 
 10
 11#include <sound/core.h>
 12#include <sound/pcm.h>
 13#include <sound/pcm_params.h>
 14#include <sound/pxa2xx-lib.h>
 15
 16#include <mach/dma.h>
 17
 18#include "pxa2xx-pcm.h"
 19
 20static const struct snd_pcm_hardware pxa2xx_pcm_hardware = {
 21	.info			= SNDRV_PCM_INFO_MMAP |
 22				  SNDRV_PCM_INFO_MMAP_VALID |
 23				  SNDRV_PCM_INFO_INTERLEAVED |
 24				  SNDRV_PCM_INFO_PAUSE |
 25				  SNDRV_PCM_INFO_RESUME,
 26	.formats		= SNDRV_PCM_FMTBIT_S16_LE |
 27					SNDRV_PCM_FMTBIT_S24_LE |
 28					SNDRV_PCM_FMTBIT_S32_LE,
 29	.period_bytes_min	= 32,
 30	.period_bytes_max	= 8192 - 32,
 31	.periods_min		= 1,
 32	.periods_max		= PAGE_SIZE/sizeof(pxa_dma_desc),
 33	.buffer_bytes_max	= 128 * 1024,
 34	.fifo_size		= 32,
 35};
 36
 37int __pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
 38				struct snd_pcm_hw_params *params)
 39{
 40	struct snd_pcm_runtime *runtime = substream->runtime;
 41	struct pxa2xx_runtime_data *rtd = runtime->private_data;
 42	size_t totsize = params_buffer_bytes(params);
 43	size_t period = params_period_bytes(params);
 44	pxa_dma_desc *dma_desc;
 45	dma_addr_t dma_buff_phys, next_desc_phys;
 46
 47	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
 48	runtime->dma_bytes = totsize;
 
 49
 50	dma_desc = rtd->dma_desc_array;
 51	next_desc_phys = rtd->dma_desc_array_phys;
 52	dma_buff_phys = runtime->dma_addr;
 53	do {
 54		next_desc_phys += sizeof(pxa_dma_desc);
 55		dma_desc->ddadr = next_desc_phys;
 56		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 57			dma_desc->dsadr = dma_buff_phys;
 58			dma_desc->dtadr = rtd->params->dev_addr;
 59		} else {
 60			dma_desc->dsadr = rtd->params->dev_addr;
 61			dma_desc->dtadr = dma_buff_phys;
 62		}
 63		if (period > totsize)
 64			period = totsize;
 65		dma_desc->dcmd = rtd->params->dcmd | period | DCMD_ENDIRQEN;
 66		dma_desc++;
 67		dma_buff_phys += period;
 68	} while (totsize -= period);
 69	dma_desc[-1].ddadr = rtd->dma_desc_array_phys;
 70
 71	return 0;
 72}
 73EXPORT_SYMBOL(__pxa2xx_pcm_hw_params);
 74
 75int __pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
 76{
 77	struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
 78
 79	if (rtd && rtd->params && rtd->params->drcmr)
 80		*rtd->params->drcmr = 0;
 81
 82	snd_pcm_set_runtime_buffer(substream, NULL);
 83	return 0;
 84}
 85EXPORT_SYMBOL(__pxa2xx_pcm_hw_free);
 86
 87int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 88{
 89	struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
 90	int ret = 0;
 91
 92	switch (cmd) {
 93	case SNDRV_PCM_TRIGGER_START:
 94		DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
 95		DCSR(prtd->dma_ch) = DCSR_RUN;
 96		break;
 97
 98	case SNDRV_PCM_TRIGGER_STOP:
 99	case SNDRV_PCM_TRIGGER_SUSPEND:
100	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
101		DCSR(prtd->dma_ch) &= ~DCSR_RUN;
102		break;
103
104	case SNDRV_PCM_TRIGGER_RESUME:
105		DCSR(prtd->dma_ch) |= DCSR_RUN;
106		break;
107	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
108		DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
109		DCSR(prtd->dma_ch) |= DCSR_RUN;
110		break;
111
112	default:
113		ret = -EINVAL;
114	}
115
116	return ret;
117}
118EXPORT_SYMBOL(pxa2xx_pcm_trigger);
119
120snd_pcm_uframes_t
121pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
122{
123	struct snd_pcm_runtime *runtime = substream->runtime;
124	struct pxa2xx_runtime_data *prtd = runtime->private_data;
125
126	dma_addr_t ptr = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
127			 DSADR(prtd->dma_ch) : DTADR(prtd->dma_ch);
128	snd_pcm_uframes_t x = bytes_to_frames(runtime, ptr - runtime->dma_addr);
129
130	if (x == runtime->buffer_size)
131		x = 0;
132	return x;
133}
134EXPORT_SYMBOL(pxa2xx_pcm_pointer);
135
136int __pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
137{
138	struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
139
140	if (!prtd || !prtd->params)
141		return 0;
142
143	if (prtd->dma_ch == -1)
144		return -EINVAL;
145
146	DCSR(prtd->dma_ch) &= ~DCSR_RUN;
147	DCSR(prtd->dma_ch) = 0;
148	DCMD(prtd->dma_ch) = 0;
149	*prtd->params->drcmr = prtd->dma_ch | DRCMR_MAPVLD;
150
151	return 0;
152}
153EXPORT_SYMBOL(__pxa2xx_pcm_prepare);
154
155void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id)
156{
157	struct snd_pcm_substream *substream = dev_id;
158	struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
159	int dcsr;
160
161	dcsr = DCSR(dma_ch);
162	DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
163
164	if (dcsr & DCSR_ENDINTR) {
165		snd_pcm_period_elapsed(substream);
166	} else {
167		printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
168			rtd->params->name, dma_ch, dcsr);
169		snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
170	}
171}
172EXPORT_SYMBOL(pxa2xx_pcm_dma_irq);
173
174int __pxa2xx_pcm_open(struct snd_pcm_substream *substream)
175{
 
176	struct snd_pcm_runtime *runtime = substream->runtime;
177	struct pxa2xx_runtime_data *rtd;
178	int ret;
179
180	runtime->hw = pxa2xx_pcm_hardware;
181
 
 
 
 
182	/*
183	 * For mysterious reasons (and despite what the manual says)
184	 * playback samples are lost if the DMA count is not a multiple
185	 * of the DMA burst size.  Let's add a rule to enforce that.
186	 */
187	ret = snd_pcm_hw_constraint_step(runtime, 0,
188		SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
189	if (ret)
190		goto out;
191
192	ret = snd_pcm_hw_constraint_step(runtime, 0,
193		SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
194	if (ret)
195		goto out;
196
197	ret = snd_pcm_hw_constraint_integer(runtime,
198					    SNDRV_PCM_HW_PARAM_PERIODS);
199	if (ret < 0)
200		goto out;
201
202	ret = -ENOMEM;
203	rtd = kzalloc(sizeof(*rtd), GFP_KERNEL);
204	if (!rtd)
205		goto out;
206	rtd->dma_desc_array =
207		dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE,
208				       &rtd->dma_desc_array_phys, GFP_KERNEL);
209	if (!rtd->dma_desc_array)
210		goto err1;
211
212	rtd->dma_ch = -1;
213	runtime->private_data = rtd;
214	return 0;
215
216 err1:
217	kfree(rtd);
218 out:
219	return ret;
220}
221EXPORT_SYMBOL(__pxa2xx_pcm_open);
222
223int __pxa2xx_pcm_close(struct snd_pcm_substream *substream)
224{
225	struct snd_pcm_runtime *runtime = substream->runtime;
226	struct pxa2xx_runtime_data *rtd = runtime->private_data;
227
228	dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
229			      rtd->dma_desc_array, rtd->dma_desc_array_phys);
230	kfree(rtd);
231	return 0;
232}
233EXPORT_SYMBOL(__pxa2xx_pcm_close);
234
235int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
236	struct vm_area_struct *vma)
237{
238	struct snd_pcm_runtime *runtime = substream->runtime;
239	return dma_mmap_writecombine(substream->pcm->card->dev, vma,
240				     runtime->dma_area,
241				     runtime->dma_addr,
242				     runtime->dma_bytes);
243}
244EXPORT_SYMBOL(pxa2xx_pcm_mmap);
245
246int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
247{
248	struct snd_pcm_substream *substream = pcm->streams[stream].substream;
249	struct snd_dma_buffer *buf = &substream->dma_buffer;
250	size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
251	buf->dev.type = SNDRV_DMA_TYPE_DEV;
252	buf->dev.dev = pcm->card->dev;
253	buf->private_data = NULL;
254	buf->area = dma_alloc_writecombine(pcm->card->dev, size,
255					   &buf->addr, GFP_KERNEL);
256	if (!buf->area)
257		return -ENOMEM;
258	buf->bytes = size;
259	return 0;
260}
261EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer);
262
263void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
264{
265	struct snd_pcm_substream *substream;
266	struct snd_dma_buffer *buf;
267	int stream;
268
269	for (stream = 0; stream < 2; stream++) {
270		substream = pcm->streams[stream].substream;
271		if (!substream)
272			continue;
273		buf = &substream->dma_buffer;
274		if (!buf->area)
275			continue;
276		dma_free_writecombine(pcm->card->dev, buf->bytes,
277				      buf->area, buf->addr);
278		buf->area = NULL;
279	}
280}
281EXPORT_SYMBOL(pxa2xx_pcm_free_dma_buffers);
282
283MODULE_AUTHOR("Nicolas Pitre");
284MODULE_DESCRIPTION("Intel PXA2xx sound library");
285MODULE_LICENSE("GPL");
v4.6
  1/*
  2 * This program is free software; you can redistribute it and/or modify
  3 * it under the terms of the GNU General Public License version 2 as
  4 * published by the Free Software Foundation.
  5 */
  6
  7#include <linux/slab.h>
  8#include <linux/module.h>
  9#include <linux/dma-mapping.h>
 10#include <linux/dmaengine.h>
 11#include <linux/dma/pxa-dma.h>
 12
 13#include <sound/core.h>
 14#include <sound/pcm.h>
 15#include <sound/pcm_params.h>
 16#include <sound/pxa2xx-lib.h>
 17#include <sound/dmaengine_pcm.h>
 
 18
 19#include "pxa2xx-pcm.h"
 20
 21static const struct snd_pcm_hardware pxa2xx_pcm_hardware = {
 22	.info			= SNDRV_PCM_INFO_MMAP |
 23				  SNDRV_PCM_INFO_MMAP_VALID |
 24				  SNDRV_PCM_INFO_INTERLEAVED |
 25				  SNDRV_PCM_INFO_PAUSE |
 26				  SNDRV_PCM_INFO_RESUME,
 27	.formats		= SNDRV_PCM_FMTBIT_S16_LE |
 28					SNDRV_PCM_FMTBIT_S24_LE |
 29					SNDRV_PCM_FMTBIT_S32_LE,
 30	.period_bytes_min	= 32,
 31	.period_bytes_max	= 8192 - 32,
 32	.periods_min		= 1,
 33	.periods_max		= 256,
 34	.buffer_bytes_max	= 128 * 1024,
 35	.fifo_size		= 32,
 36};
 37
 38int __pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
 39				struct snd_pcm_hw_params *params)
 40{
 41	struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
 42	struct snd_soc_pcm_runtime *rtd = substream->private_data;
 43	struct snd_dmaengine_dai_dma_data *dma_params;
 44	struct dma_slave_config config;
 45	int ret;
 
 46
 47	dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
 48	if (!dma_params)
 49		return 0;
 50
 51	ret = snd_hwparams_to_dma_slave_config(substream, params, &config);
 52	if (ret)
 53		return ret;
 54
 55	snd_dmaengine_pcm_set_config_from_dai_data(substream,
 56			snd_soc_dai_get_dma_data(rtd->cpu_dai, substream),
 57			&config);
 58
 59	ret = dmaengine_slave_config(chan, &config);
 60	if (ret)
 61		return ret;
 62
 63	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
 
 
 
 
 
 
 
 64
 65	return 0;
 66}
 67EXPORT_SYMBOL(__pxa2xx_pcm_hw_params);
 68
 69int __pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
 70{
 
 
 
 
 
 71	snd_pcm_set_runtime_buffer(substream, NULL);
 72	return 0;
 73}
 74EXPORT_SYMBOL(__pxa2xx_pcm_hw_free);
 75
 76int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 77{
 78	return snd_dmaengine_pcm_trigger(substream, cmd);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 79}
 80EXPORT_SYMBOL(pxa2xx_pcm_trigger);
 81
 82snd_pcm_uframes_t
 83pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
 84{
 85	return snd_dmaengine_pcm_pointer(substream);
 
 
 
 
 
 
 
 
 
 86}
 87EXPORT_SYMBOL(pxa2xx_pcm_pointer);
 88
 89int __pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
 90{
 
 
 
 
 
 
 
 
 
 
 
 
 
 91	return 0;
 92}
 93EXPORT_SYMBOL(__pxa2xx_pcm_prepare);
 94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 95int __pxa2xx_pcm_open(struct snd_pcm_substream *substream)
 96{
 97	struct snd_soc_pcm_runtime *rtd = substream->private_data;
 98	struct snd_pcm_runtime *runtime = substream->runtime;
 99	struct snd_dmaengine_dai_dma_data *dma_params;
100	int ret;
101
102	runtime->hw = pxa2xx_pcm_hardware;
103
104	dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
105	if (!dma_params)
106		return 0;
107
108	/*
109	 * For mysterious reasons (and despite what the manual says)
110	 * playback samples are lost if the DMA count is not a multiple
111	 * of the DMA burst size.  Let's add a rule to enforce that.
112	 */
113	ret = snd_pcm_hw_constraint_step(runtime, 0,
114		SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
115	if (ret)
116		return ret;
117
118	ret = snd_pcm_hw_constraint_step(runtime, 0,
119		SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
120	if (ret)
121		return ret;
122
123	ret = snd_pcm_hw_constraint_integer(runtime,
124					    SNDRV_PCM_HW_PARAM_PERIODS);
125	if (ret < 0)
126		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
128	return snd_dmaengine_pcm_open_request_chan(substream,
129					pxad_filter_fn,
130					dma_params->filter_data);
 
131}
132EXPORT_SYMBOL(__pxa2xx_pcm_open);
133
134int __pxa2xx_pcm_close(struct snd_pcm_substream *substream)
135{
136	return snd_dmaengine_pcm_close_release_chan(substream);
 
 
 
 
 
 
137}
138EXPORT_SYMBOL(__pxa2xx_pcm_close);
139
140int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
141	struct vm_area_struct *vma)
142{
143	struct snd_pcm_runtime *runtime = substream->runtime;
144	return dma_mmap_wc(substream->pcm->card->dev, vma, runtime->dma_area,
145			   runtime->dma_addr, runtime->dma_bytes);
 
 
146}
147EXPORT_SYMBOL(pxa2xx_pcm_mmap);
148
149int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
150{
151	struct snd_pcm_substream *substream = pcm->streams[stream].substream;
152	struct snd_dma_buffer *buf = &substream->dma_buffer;
153	size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
154	buf->dev.type = SNDRV_DMA_TYPE_DEV;
155	buf->dev.dev = pcm->card->dev;
156	buf->private_data = NULL;
157	buf->area = dma_alloc_wc(pcm->card->dev, size, &buf->addr, GFP_KERNEL);
 
158	if (!buf->area)
159		return -ENOMEM;
160	buf->bytes = size;
161	return 0;
162}
163EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer);
164
165void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
166{
167	struct snd_pcm_substream *substream;
168	struct snd_dma_buffer *buf;
169	int stream;
170
171	for (stream = 0; stream < 2; stream++) {
172		substream = pcm->streams[stream].substream;
173		if (!substream)
174			continue;
175		buf = &substream->dma_buffer;
176		if (!buf->area)
177			continue;
178		dma_free_wc(pcm->card->dev, buf->bytes, buf->area, buf->addr);
 
179		buf->area = NULL;
180	}
181}
182EXPORT_SYMBOL(pxa2xx_pcm_free_dma_buffers);
183
184MODULE_AUTHOR("Nicolas Pitre");
185MODULE_DESCRIPTION("Intel PXA2xx sound library");
186MODULE_LICENSE("GPL");