Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Au12x0/Au1550 PSC ALSA ASoC audio support.
  4 *
  5 * (c) 2007-2008 MSC Vertriebsges.m.b.H.,
  6 *	Manuel Lauss <manuel.lauss@gmail.com>
  7 *
 
 
 
 
  8 * DMA glue for Au1x-PSC audio.
 
  9 */
 10
 11
 12#include <linux/module.h>
 13#include <linux/init.h>
 14#include <linux/platform_device.h>
 15#include <linux/slab.h>
 16#include <linux/dma-mapping.h>
 17
 18#include <sound/core.h>
 19#include <sound/pcm.h>
 20#include <sound/pcm_params.h>
 21#include <sound/soc.h>
 22
 23#include <asm/mach-au1x00/au1000.h>
 24#include <asm/mach-au1x00/au1xxx_dbdma.h>
 25#include <asm/mach-au1x00/au1xxx_psc.h>
 26
 27#include "psc.h"
 28
 29/*#define PCM_DEBUG*/
 30
 31#define DRV_NAME "dbdma2"
 32
 33#define MSG(x...)	printk(KERN_INFO "au1xpsc_pcm: " x)
 34#ifdef PCM_DEBUG
 35#define DBG		MSG
 36#else
 37#define DBG(x...)	do {} while (0)
 38#endif
 39
 40struct au1xpsc_audio_dmadata {
 41	/* DDMA control data */
 42	unsigned int ddma_id;		/* DDMA direction ID for this PSC */
 43	u32 ddma_chan;			/* DDMA context */
 44
 45	/* PCM context (for irq handlers) */
 46	struct snd_pcm_substream *substream;
 47	unsigned long curr_period;	/* current segment DDMA is working on */
 48	unsigned long q_period;		/* queue period(s) */
 49	dma_addr_t dma_area;		/* address of queued DMA area */
 50	dma_addr_t dma_area_s;		/* start address of DMA area */
 51	unsigned long pos;		/* current byte position being played */
 52	unsigned long periods;		/* number of SG segments in total */
 53	unsigned long period_bytes;	/* size in bytes of one SG segment */
 54
 55	/* runtime data */
 56	int msbits;
 57};
 58
 59/*
 60 * These settings are somewhat okay, at least on my machine audio plays
 61 * almost skip-free. Especially the 64kB buffer seems to help a LOT.
 62 */
 63#define AU1XPSC_PERIOD_MIN_BYTES	1024
 64#define AU1XPSC_BUFFER_MIN_BYTES	65536
 65
 
 
 
 
 
 
 
 
 66/* PCM hardware DMA capabilities - platform specific */
 67static const struct snd_pcm_hardware au1xpsc_pcm_hardware = {
 68	.info		  = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
 69			    SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BATCH,
 
 70	.period_bytes_min = AU1XPSC_PERIOD_MIN_BYTES,
 71	.period_bytes_max = 4096 * 1024 - 1,
 72	.periods_min	  = 2,
 73	.periods_max	  = 4096,	/* 2 to as-much-as-you-like */
 74	.buffer_bytes_max = 4096 * 1024 - 1,
 75	.fifo_size	  = 16,		/* fifo entries of AC97/I2S PSC */
 76};
 77
 78static void au1x_pcm_queue_tx(struct au1xpsc_audio_dmadata *cd)
 79{
 80	au1xxx_dbdma_put_source(cd->ddma_chan, cd->dma_area,
 81				cd->period_bytes, DDMA_FLAGS_IE);
 82
 83	/* update next-to-queue period */
 84	++cd->q_period;
 85	cd->dma_area += cd->period_bytes;
 86	if (cd->q_period >= cd->periods) {
 87		cd->q_period = 0;
 88		cd->dma_area = cd->dma_area_s;
 89	}
 90}
 91
 92static void au1x_pcm_queue_rx(struct au1xpsc_audio_dmadata *cd)
 93{
 94	au1xxx_dbdma_put_dest(cd->ddma_chan, cd->dma_area,
 95			      cd->period_bytes, DDMA_FLAGS_IE);
 96
 97	/* update next-to-queue period */
 98	++cd->q_period;
 99	cd->dma_area += cd->period_bytes;
100	if (cd->q_period >= cd->periods) {
101		cd->q_period = 0;
102		cd->dma_area = cd->dma_area_s;
103	}
104}
105
106static void au1x_pcm_dmatx_cb(int irq, void *dev_id)
107{
108	struct au1xpsc_audio_dmadata *cd = dev_id;
109
110	cd->pos += cd->period_bytes;
111	if (++cd->curr_period >= cd->periods) {
112		cd->pos = 0;
113		cd->curr_period = 0;
114	}
115	snd_pcm_period_elapsed(cd->substream);
116	au1x_pcm_queue_tx(cd);
117}
118
119static void au1x_pcm_dmarx_cb(int irq, void *dev_id)
120{
121	struct au1xpsc_audio_dmadata *cd = dev_id;
122
123	cd->pos += cd->period_bytes;
124	if (++cd->curr_period >= cd->periods) {
125		cd->pos = 0;
126		cd->curr_period = 0;
127	}
128	snd_pcm_period_elapsed(cd->substream);
129	au1x_pcm_queue_rx(cd);
130}
131
132static void au1x_pcm_dbdma_free(struct au1xpsc_audio_dmadata *pcd)
133{
134	if (pcd->ddma_chan) {
135		au1xxx_dbdma_stop(pcd->ddma_chan);
136		au1xxx_dbdma_reset(pcd->ddma_chan);
137		au1xxx_dbdma_chan_free(pcd->ddma_chan);
138		pcd->ddma_chan = 0;
139		pcd->msbits = 0;
140	}
141}
142
143/* in case of missing DMA ring or changed TX-source / RX-dest bit widths,
144 * allocate (or reallocate) a 2-descriptor DMA ring with bit depth according
145 * to ALSA-supplied sample depth.  This is due to limitations in the dbdma api
146 * (cannot adjust source/dest widths of already allocated descriptor ring).
147 */
148static int au1x_pcm_dbdma_realloc(struct au1xpsc_audio_dmadata *pcd,
149				 int stype, int msbits)
150{
151	/* DMA only in 8/16/32 bit widths */
152	if (msbits == 24)
153		msbits = 32;
154
155	/* check current config: correct bits and descriptors allocated? */
156	if ((pcd->ddma_chan) && (msbits == pcd->msbits))
157		goto out;	/* all ok! */
158
159	au1x_pcm_dbdma_free(pcd);
160
161	if (stype == SNDRV_PCM_STREAM_CAPTURE)
162		pcd->ddma_chan = au1xxx_dbdma_chan_alloc(pcd->ddma_id,
163					DSCR_CMD0_ALWAYS,
164					au1x_pcm_dmarx_cb, (void *)pcd);
165	else
166		pcd->ddma_chan = au1xxx_dbdma_chan_alloc(DSCR_CMD0_ALWAYS,
167					pcd->ddma_id,
168					au1x_pcm_dmatx_cb, (void *)pcd);
169
170	if (!pcd->ddma_chan)
171		return -ENOMEM;
172
173	au1xxx_dbdma_set_devwidth(pcd->ddma_chan, msbits);
174	au1xxx_dbdma_ring_alloc(pcd->ddma_chan, 2);
175
176	pcd->msbits = msbits;
177
178	au1xxx_dbdma_stop(pcd->ddma_chan);
179	au1xxx_dbdma_reset(pcd->ddma_chan);
180
181out:
182	return 0;
183}
184
185static inline struct au1xpsc_audio_dmadata *to_dmadata(struct snd_pcm_substream *ss,
186						       struct snd_soc_component *component)
187{
188	struct au1xpsc_audio_dmadata *pcd = snd_soc_component_get_drvdata(component);
189	return &pcd[ss->stream];
 
 
190}
191
192static int au1xpsc_pcm_hw_params(struct snd_soc_component *component,
193				 struct snd_pcm_substream *substream,
194				 struct snd_pcm_hw_params *params)
195{
196	struct snd_pcm_runtime *runtime = substream->runtime;
197	struct au1xpsc_audio_dmadata *pcd;
198	int stype, ret;
199
200	stype = substream->stream;
201	pcd = to_dmadata(substream, component);
 
202
203	DBG("runtime->dma_area = 0x%08lx dma_addr_t = 0x%08lx dma_size = %zu "
204	    "runtime->min_align %lu\n",
 
 
 
205		(unsigned long)runtime->dma_area,
206		(unsigned long)runtime->dma_addr, runtime->dma_bytes,
207		runtime->min_align);
208
209	DBG("bits %d  frags %d  frag_bytes %d  is_rx %d\n", params->msbits,
210		params_periods(params), params_period_bytes(params), stype);
211
212	ret = au1x_pcm_dbdma_realloc(pcd, stype, params->msbits);
213	if (ret) {
214		MSG("DDMA channel (re)alloc failed!\n");
215		goto out;
216	}
217
218	pcd->substream = substream;
219	pcd->period_bytes = params_period_bytes(params);
220	pcd->periods = params_periods(params);
221	pcd->dma_area_s = pcd->dma_area = runtime->dma_addr;
222	pcd->q_period = 0;
223	pcd->curr_period = 0;
224	pcd->pos = 0;
225
226	ret = 0;
227out:
228	return ret;
229}
230
231static int au1xpsc_pcm_prepare(struct snd_soc_component *component,
232			       struct snd_pcm_substream *substream)
233{
234	struct au1xpsc_audio_dmadata *pcd = to_dmadata(substream, component);
 
 
 
 
 
 
235
236	au1xxx_dbdma_reset(pcd->ddma_chan);
237
238	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
239		au1x_pcm_queue_rx(pcd);
240		au1x_pcm_queue_rx(pcd);
241	} else {
242		au1x_pcm_queue_tx(pcd);
243		au1x_pcm_queue_tx(pcd);
244	}
245
246	return 0;
247}
248
249static int au1xpsc_pcm_trigger(struct snd_soc_component *component,
250			       struct snd_pcm_substream *substream, int cmd)
251{
252	u32 c = to_dmadata(substream, component)->ddma_chan;
253
254	switch (cmd) {
255	case SNDRV_PCM_TRIGGER_START:
256	case SNDRV_PCM_TRIGGER_RESUME:
257		au1xxx_dbdma_start(c);
258		break;
259	case SNDRV_PCM_TRIGGER_STOP:
260	case SNDRV_PCM_TRIGGER_SUSPEND:
261		au1xxx_dbdma_stop(c);
262		break;
263	default:
264		return -EINVAL;
265	}
266	return 0;
267}
268
269static snd_pcm_uframes_t
270au1xpsc_pcm_pointer(struct snd_soc_component *component,
271		    struct snd_pcm_substream *substream)
272{
273	return bytes_to_frames(substream->runtime,
274			       to_dmadata(substream, component)->pos);
275}
276
277static int au1xpsc_pcm_open(struct snd_soc_component *component,
278			    struct snd_pcm_substream *substream)
279{
280	struct au1xpsc_audio_dmadata *pcd = to_dmadata(substream, component);
281	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
282	int stype = substream->stream, *dmaids;
283
284	dmaids = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
285	if (!dmaids)
286		return -ENODEV;	/* whoa, has ordering changed? */
287
288	pcd->ddma_id = dmaids[stype];
289
290	snd_soc_set_runtime_hwparams(substream, &au1xpsc_pcm_hardware);
291	return 0;
292}
293
294static int au1xpsc_pcm_close(struct snd_soc_component *component,
295			     struct snd_pcm_substream *substream)
296{
297	au1x_pcm_dbdma_free(to_dmadata(substream, component));
298	return 0;
299}
300
301static int au1xpsc_pcm_new(struct snd_soc_component *component,
302			   struct snd_soc_pcm_runtime *rtd)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303{
304	struct snd_card *card = rtd->card->snd_card;
305	struct snd_pcm *pcm = rtd->pcm;
306
307	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
308		card->dev, AU1XPSC_BUFFER_MIN_BYTES, (4096 * 1024) - 1);
309
310	return 0;
311}
312
313/* au1xpsc audio platform */
314static struct snd_soc_component_driver au1xpsc_soc_component = {
315	.name		= DRV_NAME,
316	.open		= au1xpsc_pcm_open,
317	.close		= au1xpsc_pcm_close,
318	.hw_params	= au1xpsc_pcm_hw_params,
319	.prepare	= au1xpsc_pcm_prepare,
320	.trigger	= au1xpsc_pcm_trigger,
321	.pointer	= au1xpsc_pcm_pointer,
322	.pcm_construct	= au1xpsc_pcm_new,
323};
324
325static int au1xpsc_pcm_drvprobe(struct platform_device *pdev)
326{
327	struct au1xpsc_audio_dmadata *dmadata;
 
 
328
329	dmadata = devm_kcalloc(&pdev->dev,
330			       2, sizeof(struct au1xpsc_audio_dmadata),
331			       GFP_KERNEL);
332	if (!dmadata)
333		return -ENOMEM;
334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335	platform_set_drvdata(pdev, dmadata);
336
337	return devm_snd_soc_register_component(&pdev->dev,
338					&au1xpsc_soc_component, NULL, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
339}
340
341static struct platform_driver au1xpsc_pcm_driver = {
342	.driver	= {
343		.name	= "au1xpsc-pcm",
 
344	},
345	.probe		= au1xpsc_pcm_drvprobe,
 
346};
347
348module_platform_driver(au1xpsc_pcm_driver);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
349
350MODULE_LICENSE("GPL");
351MODULE_DESCRIPTION("Au12x0/Au1550 PSC Audio DMA driver");
352MODULE_AUTHOR("Manuel Lauss");
v3.1
 
  1/*
  2 * Au12x0/Au1550 PSC ALSA ASoC audio support.
  3 *
  4 * (c) 2007-2008 MSC Vertriebsges.m.b.H.,
  5 *	Manuel Lauss <manuel.lauss@gmail.com>
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License version 2 as
  9 * published by the Free Software Foundation.
 10 *
 11 * DMA glue for Au1x-PSC audio.
 12 *
 13 */
 14
 15
 16#include <linux/module.h>
 17#include <linux/init.h>
 18#include <linux/platform_device.h>
 19#include <linux/slab.h>
 20#include <linux/dma-mapping.h>
 21
 22#include <sound/core.h>
 23#include <sound/pcm.h>
 24#include <sound/pcm_params.h>
 25#include <sound/soc.h>
 26
 27#include <asm/mach-au1x00/au1000.h>
 28#include <asm/mach-au1x00/au1xxx_dbdma.h>
 29#include <asm/mach-au1x00/au1xxx_psc.h>
 30
 31#include "psc.h"
 32
 33/*#define PCM_DEBUG*/
 34
 
 
 35#define MSG(x...)	printk(KERN_INFO "au1xpsc_pcm: " x)
 36#ifdef PCM_DEBUG
 37#define DBG		MSG
 38#else
 39#define DBG(x...)	do {} while (0)
 40#endif
 41
 42struct au1xpsc_audio_dmadata {
 43	/* DDMA control data */
 44	unsigned int ddma_id;		/* DDMA direction ID for this PSC */
 45	u32 ddma_chan;			/* DDMA context */
 46
 47	/* PCM context (for irq handlers) */
 48	struct snd_pcm_substream *substream;
 49	unsigned long curr_period;	/* current segment DDMA is working on */
 50	unsigned long q_period;		/* queue period(s) */
 51	dma_addr_t dma_area;		/* address of queued DMA area */
 52	dma_addr_t dma_area_s;		/* start address of DMA area */
 53	unsigned long pos;		/* current byte position being played */
 54	unsigned long periods;		/* number of SG segments in total */
 55	unsigned long period_bytes;	/* size in bytes of one SG segment */
 56
 57	/* runtime data */
 58	int msbits;
 59};
 60
 61/*
 62 * These settings are somewhat okay, at least on my machine audio plays
 63 * almost skip-free. Especially the 64kB buffer seems to help a LOT.
 64 */
 65#define AU1XPSC_PERIOD_MIN_BYTES	1024
 66#define AU1XPSC_BUFFER_MIN_BYTES	65536
 67
 68#define AU1XPSC_PCM_FMTS					\
 69	(SNDRV_PCM_FMTBIT_S8     | SNDRV_PCM_FMTBIT_U8 |	\
 70	 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |	\
 71	 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE |	\
 72	 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |	\
 73	 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE |	\
 74	 0)
 75
 76/* PCM hardware DMA capabilities - platform specific */
 77static const struct snd_pcm_hardware au1xpsc_pcm_hardware = {
 78	.info		  = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
 79			    SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BATCH,
 80	.formats	  = AU1XPSC_PCM_FMTS,
 81	.period_bytes_min = AU1XPSC_PERIOD_MIN_BYTES,
 82	.period_bytes_max = 4096 * 1024 - 1,
 83	.periods_min	  = 2,
 84	.periods_max	  = 4096,	/* 2 to as-much-as-you-like */
 85	.buffer_bytes_max = 4096 * 1024 - 1,
 86	.fifo_size	  = 16,		/* fifo entries of AC97/I2S PSC */
 87};
 88
 89static void au1x_pcm_queue_tx(struct au1xpsc_audio_dmadata *cd)
 90{
 91	au1xxx_dbdma_put_source(cd->ddma_chan, cd->dma_area,
 92				cd->period_bytes, DDMA_FLAGS_IE);
 93
 94	/* update next-to-queue period */
 95	++cd->q_period;
 96	cd->dma_area += cd->period_bytes;
 97	if (cd->q_period >= cd->periods) {
 98		cd->q_period = 0;
 99		cd->dma_area = cd->dma_area_s;
100	}
101}
102
103static void au1x_pcm_queue_rx(struct au1xpsc_audio_dmadata *cd)
104{
105	au1xxx_dbdma_put_dest(cd->ddma_chan, cd->dma_area,
106			      cd->period_bytes, DDMA_FLAGS_IE);
107
108	/* update next-to-queue period */
109	++cd->q_period;
110	cd->dma_area += cd->period_bytes;
111	if (cd->q_period >= cd->periods) {
112		cd->q_period = 0;
113		cd->dma_area = cd->dma_area_s;
114	}
115}
116
117static void au1x_pcm_dmatx_cb(int irq, void *dev_id)
118{
119	struct au1xpsc_audio_dmadata *cd = dev_id;
120
121	cd->pos += cd->period_bytes;
122	if (++cd->curr_period >= cd->periods) {
123		cd->pos = 0;
124		cd->curr_period = 0;
125	}
126	snd_pcm_period_elapsed(cd->substream);
127	au1x_pcm_queue_tx(cd);
128}
129
130static void au1x_pcm_dmarx_cb(int irq, void *dev_id)
131{
132	struct au1xpsc_audio_dmadata *cd = dev_id;
133
134	cd->pos += cd->period_bytes;
135	if (++cd->curr_period >= cd->periods) {
136		cd->pos = 0;
137		cd->curr_period = 0;
138	}
139	snd_pcm_period_elapsed(cd->substream);
140	au1x_pcm_queue_rx(cd);
141}
142
143static void au1x_pcm_dbdma_free(struct au1xpsc_audio_dmadata *pcd)
144{
145	if (pcd->ddma_chan) {
146		au1xxx_dbdma_stop(pcd->ddma_chan);
147		au1xxx_dbdma_reset(pcd->ddma_chan);
148		au1xxx_dbdma_chan_free(pcd->ddma_chan);
149		pcd->ddma_chan = 0;
150		pcd->msbits = 0;
151	}
152}
153
154/* in case of missing DMA ring or changed TX-source / RX-dest bit widths,
155 * allocate (or reallocate) a 2-descriptor DMA ring with bit depth according
156 * to ALSA-supplied sample depth.  This is due to limitations in the dbdma api
157 * (cannot adjust source/dest widths of already allocated descriptor ring).
158 */
159static int au1x_pcm_dbdma_realloc(struct au1xpsc_audio_dmadata *pcd,
160				 int stype, int msbits)
161{
162	/* DMA only in 8/16/32 bit widths */
163	if (msbits == 24)
164		msbits = 32;
165
166	/* check current config: correct bits and descriptors allocated? */
167	if ((pcd->ddma_chan) && (msbits == pcd->msbits))
168		goto out;	/* all ok! */
169
170	au1x_pcm_dbdma_free(pcd);
171
172	if (stype == PCM_RX)
173		pcd->ddma_chan = au1xxx_dbdma_chan_alloc(pcd->ddma_id,
174					DSCR_CMD0_ALWAYS,
175					au1x_pcm_dmarx_cb, (void *)pcd);
176	else
177		pcd->ddma_chan = au1xxx_dbdma_chan_alloc(DSCR_CMD0_ALWAYS,
178					pcd->ddma_id,
179					au1x_pcm_dmatx_cb, (void *)pcd);
180
181	if (!pcd->ddma_chan)
182		return -ENOMEM;
183
184	au1xxx_dbdma_set_devwidth(pcd->ddma_chan, msbits);
185	au1xxx_dbdma_ring_alloc(pcd->ddma_chan, 2);
186
187	pcd->msbits = msbits;
188
189	au1xxx_dbdma_stop(pcd->ddma_chan);
190	au1xxx_dbdma_reset(pcd->ddma_chan);
191
192out:
193	return 0;
194}
195
196static inline struct au1xpsc_audio_dmadata *to_dmadata(struct snd_pcm_substream *ss)
 
197{
198	struct snd_soc_pcm_runtime *rtd = ss->private_data;
199	struct au1xpsc_audio_dmadata *pcd =
200				snd_soc_platform_get_drvdata(rtd->platform);
201	return &pcd[SUBSTREAM_TYPE(ss)];
202}
203
204static int au1xpsc_pcm_hw_params(struct snd_pcm_substream *substream,
 
205				 struct snd_pcm_hw_params *params)
206{
207	struct snd_pcm_runtime *runtime = substream->runtime;
208	struct au1xpsc_audio_dmadata *pcd;
209	int stype, ret;
210
211	ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
212	if (ret < 0)
213		goto out;
214
215	stype = SUBSTREAM_TYPE(substream);
216	pcd = to_dmadata(substream);
217
218	DBG("runtime->dma_area = 0x%08lx dma_addr_t = 0x%08lx dma_size = %d "
219	    "runtime->min_align %d\n",
220		(unsigned long)runtime->dma_area,
221		(unsigned long)runtime->dma_addr, runtime->dma_bytes,
222		runtime->min_align);
223
224	DBG("bits %d  frags %d  frag_bytes %d  is_rx %d\n", params->msbits,
225		params_periods(params), params_period_bytes(params), stype);
226
227	ret = au1x_pcm_dbdma_realloc(pcd, stype, params->msbits);
228	if (ret) {
229		MSG("DDMA channel (re)alloc failed!\n");
230		goto out;
231	}
232
233	pcd->substream = substream;
234	pcd->period_bytes = params_period_bytes(params);
235	pcd->periods = params_periods(params);
236	pcd->dma_area_s = pcd->dma_area = runtime->dma_addr;
237	pcd->q_period = 0;
238	pcd->curr_period = 0;
239	pcd->pos = 0;
240
241	ret = 0;
242out:
243	return ret;
244}
245
246static int au1xpsc_pcm_hw_free(struct snd_pcm_substream *substream)
 
247{
248	snd_pcm_lib_free_pages(substream);
249	return 0;
250}
251
252static int au1xpsc_pcm_prepare(struct snd_pcm_substream *substream)
253{
254	struct au1xpsc_audio_dmadata *pcd = to_dmadata(substream);
255
256	au1xxx_dbdma_reset(pcd->ddma_chan);
257
258	if (SUBSTREAM_TYPE(substream) == PCM_RX) {
259		au1x_pcm_queue_rx(pcd);
260		au1x_pcm_queue_rx(pcd);
261	} else {
262		au1x_pcm_queue_tx(pcd);
263		au1x_pcm_queue_tx(pcd);
264	}
265
266	return 0;
267}
268
269static int au1xpsc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 
270{
271	u32 c = to_dmadata(substream)->ddma_chan;
272
273	switch (cmd) {
274	case SNDRV_PCM_TRIGGER_START:
275	case SNDRV_PCM_TRIGGER_RESUME:
276		au1xxx_dbdma_start(c);
277		break;
278	case SNDRV_PCM_TRIGGER_STOP:
279	case SNDRV_PCM_TRIGGER_SUSPEND:
280		au1xxx_dbdma_stop(c);
281		break;
282	default:
283		return -EINVAL;
284	}
285	return 0;
286}
287
288static snd_pcm_uframes_t
289au1xpsc_pcm_pointer(struct snd_pcm_substream *substream)
 
290{
291	return bytes_to_frames(substream->runtime, to_dmadata(substream)->pos);
 
292}
293
294static int au1xpsc_pcm_open(struct snd_pcm_substream *substream)
 
295{
 
 
 
 
 
 
 
 
 
 
296	snd_soc_set_runtime_hwparams(substream, &au1xpsc_pcm_hardware);
297	return 0;
298}
299
300static int au1xpsc_pcm_close(struct snd_pcm_substream *substream)
 
301{
302	au1x_pcm_dbdma_free(to_dmadata(substream));
303	return 0;
304}
305
306static struct snd_pcm_ops au1xpsc_pcm_ops = {
307	.open		= au1xpsc_pcm_open,
308	.close		= au1xpsc_pcm_close,
309	.ioctl		= snd_pcm_lib_ioctl,
310	.hw_params	= au1xpsc_pcm_hw_params,
311	.hw_free	= au1xpsc_pcm_hw_free,
312	.prepare	= au1xpsc_pcm_prepare,
313	.trigger	= au1xpsc_pcm_trigger,
314	.pointer	= au1xpsc_pcm_pointer,
315};
316
317static void au1xpsc_pcm_free_dma_buffers(struct snd_pcm *pcm)
318{
319	snd_pcm_lib_preallocate_free_for_all(pcm);
320}
321
322static int au1xpsc_pcm_new(struct snd_soc_pcm_runtime *rtd)
323{
324	struct snd_card *card = rtd->card->snd_card;
325	struct snd_pcm *pcm = rtd->pcm;
326
327	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
328		card->dev, AU1XPSC_BUFFER_MIN_BYTES, (4096 * 1024) - 1);
329
330	return 0;
331}
332
333/* au1xpsc audio platform */
334struct snd_soc_platform_driver au1xpsc_soc_platform = {
335	.ops		= &au1xpsc_pcm_ops,
336	.pcm_new	= au1xpsc_pcm_new,
337	.pcm_free	= au1xpsc_pcm_free_dma_buffers,
 
 
 
 
 
338};
339
340static int __devinit au1xpsc_pcm_drvprobe(struct platform_device *pdev)
341{
342	struct au1xpsc_audio_dmadata *dmadata;
343	struct resource *r;
344	int ret;
345
346	dmadata = kzalloc(2 * sizeof(struct au1xpsc_audio_dmadata), GFP_KERNEL);
 
 
347	if (!dmadata)
348		return -ENOMEM;
349
350	r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
351	if (!r) {
352		ret = -ENODEV;
353		goto out1;
354	}
355	dmadata[PCM_TX].ddma_id = r->start;
356
357	/* RX DMA */
358	r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
359	if (!r) {
360		ret = -ENODEV;
361		goto out1;
362	}
363	dmadata[PCM_RX].ddma_id = r->start;
364
365	platform_set_drvdata(pdev, dmadata);
366
367	ret = snd_soc_register_platform(&pdev->dev, &au1xpsc_soc_platform);
368	if (!ret)
369		return ret;
370
371out1:
372	kfree(dmadata);
373	return ret;
374}
375
376static int __devexit au1xpsc_pcm_drvremove(struct platform_device *pdev)
377{
378	struct au1xpsc_audio_dmadata *dmadata = platform_get_drvdata(pdev);
379
380	snd_soc_unregister_platform(&pdev->dev);
381	kfree(dmadata);
382
383	return 0;
384}
385
386static struct platform_driver au1xpsc_pcm_driver = {
387	.driver	= {
388		.name	= "au1xpsc-pcm",
389		.owner	= THIS_MODULE,
390	},
391	.probe		= au1xpsc_pcm_drvprobe,
392	.remove		= __devexit_p(au1xpsc_pcm_drvremove),
393};
394
395static int __init au1xpsc_audio_dbdma_load(void)
396{
397	return platform_driver_register(&au1xpsc_pcm_driver);
398}
399
400static void __exit au1xpsc_audio_dbdma_unload(void)
401{
402	platform_driver_unregister(&au1xpsc_pcm_driver);
403}
404
405module_init(au1xpsc_audio_dbdma_load);
406module_exit(au1xpsc_audio_dbdma_unload);
407
408
409struct platform_device *au1xpsc_pcm_add(struct platform_device *pdev)
410{
411	struct resource *res, *r;
412	struct platform_device *pd;
413	int id[2];
414	int ret;
415
416	r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
417	if (!r)
418		return NULL;
419	id[0] = r->start;
420
421	r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
422	if (!r)
423		return NULL;
424	id[1] = r->start;
425
426	res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
427	if (!res)
428		return NULL;
429
430	res[0].start = res[0].end = id[0];
431	res[1].start = res[1].end = id[1];
432	res[0].flags = res[1].flags = IORESOURCE_DMA;
433
434	pd = platform_device_alloc("au1xpsc-pcm", pdev->id);
435	if (!pd)
436		goto out;
437
438	pd->resource = res;
439	pd->num_resources = 2;
440
441	ret = platform_device_add(pd);
442	if (!ret)
443		return pd;
444
445	platform_device_put(pd);
446out:
447	kfree(res);
448	return NULL;
449}
450EXPORT_SYMBOL_GPL(au1xpsc_pcm_add);
451
452void au1xpsc_pcm_destroy(struct platform_device *dmapd)
453{
454	if (dmapd)
455		platform_device_unregister(dmapd);
456}
457EXPORT_SYMBOL_GPL(au1xpsc_pcm_destroy);
458
459MODULE_LICENSE("GPL");
460MODULE_DESCRIPTION("Au12x0/Au1550 PSC Audio DMA driver");
461MODULE_AUTHOR("Manuel Lauss");