2 * imx-pcm-dma-mx2.c -- ALSA Soc Audio Layer
4 * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
6 * This code is based on code copyrighted by Freescale,
7 * Liam Girdwood, Javier Martin and probably others.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/dmaengine.h>
24 #include <linux/types.h>
26 #include <sound/core.h>
27 #include <sound/initval.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
36 struct imx_pcm_runtime_data
{
44 struct dma_async_tx_descriptor
*desc
;
45 struct dma_chan
*dma_chan
;
46 struct imx_dma_data dma_data
;
49 static void audio_dma_irq(void *data
)
51 struct snd_pcm_substream
*substream
= (struct snd_pcm_substream
*)data
;
52 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
53 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
55 iprtd
->offset
+= iprtd
->period_bytes
;
56 iprtd
->offset
%= iprtd
->period_bytes
* iprtd
->periods
;
58 snd_pcm_period_elapsed(substream
);
61 static bool filter(struct dma_chan
*chan
, void *param
)
63 struct imx_pcm_runtime_data
*iprtd
= param
;
65 if (!imx_dma_is_general_purpose(chan
))
68 chan
->private = &iprtd
->dma_data
;
73 static int imx_ssi_dma_alloc(struct snd_pcm_substream
*substream
,
74 struct snd_pcm_hw_params
*params
)
76 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
77 struct imx_pcm_dma_params
*dma_params
;
78 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
79 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
80 struct dma_slave_config slave_config
;
82 enum dma_slave_buswidth buswidth
;
85 dma_params
= snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
87 iprtd
->dma_data
.peripheral_type
= IMX_DMATYPE_SSI
;
88 iprtd
->dma_data
.priority
= DMA_PRIO_HIGH
;
89 iprtd
->dma_data
.dma_request
= dma_params
->dma
;
91 /* Try to grab a DMA channel */
92 if (!iprtd
->dma_chan
) {
94 dma_cap_set(DMA_SLAVE
, mask
);
95 iprtd
->dma_chan
= dma_request_channel(mask
, filter
, iprtd
);
100 switch (params_format(params
)) {
101 case SNDRV_PCM_FORMAT_S16_LE
:
102 buswidth
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
104 case SNDRV_PCM_FORMAT_S20_3LE
:
105 case SNDRV_PCM_FORMAT_S24_LE
:
106 buswidth
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
112 slave_config
.device_fc
= false;
114 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
115 slave_config
.direction
= DMA_MEM_TO_DEV
;
116 slave_config
.dst_addr
= dma_params
->dma_addr
;
117 slave_config
.dst_addr_width
= buswidth
;
118 slave_config
.dst_maxburst
= dma_params
->burstsize
;
120 slave_config
.direction
= DMA_DEV_TO_MEM
;
121 slave_config
.src_addr
= dma_params
->dma_addr
;
122 slave_config
.src_addr_width
= buswidth
;
123 slave_config
.src_maxburst
= dma_params
->burstsize
;
126 ret
= dmaengine_slave_config(iprtd
->dma_chan
, &slave_config
);
133 static int snd_imx_pcm_hw_params(struct snd_pcm_substream
*substream
,
134 struct snd_pcm_hw_params
*params
)
136 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
137 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
138 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
139 unsigned long dma_addr
;
140 struct dma_chan
*chan
;
141 struct imx_pcm_dma_params
*dma_params
;
144 dma_params
= snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
145 ret
= imx_ssi_dma_alloc(substream
, params
);
148 chan
= iprtd
->dma_chan
;
150 iprtd
->size
= params_buffer_bytes(params
);
151 iprtd
->periods
= params_periods(params
);
152 iprtd
->period_bytes
= params_period_bytes(params
);
154 iprtd
->period_time
= HZ
/ (params_rate(params
) /
155 params_period_size(params
));
157 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
159 dma_addr
= runtime
->dma_addr
;
161 iprtd
->buf
= (unsigned int *)substream
->dma_buffer
.area
;
163 iprtd
->desc
= dmaengine_prep_dma_cyclic(chan
, dma_addr
,
164 iprtd
->period_bytes
* iprtd
->periods
,
166 substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
?
167 DMA_MEM_TO_DEV
: DMA_DEV_TO_MEM
);
169 dev_err(&chan
->dev
->device
, "cannot prepare slave dma\n");
173 iprtd
->desc
->callback
= audio_dma_irq
;
174 iprtd
->desc
->callback_param
= substream
;
179 static int snd_imx_pcm_hw_free(struct snd_pcm_substream
*substream
)
181 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
182 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
184 if (iprtd
->dma_chan
) {
185 dma_release_channel(iprtd
->dma_chan
);
186 iprtd
->dma_chan
= NULL
;
192 static int snd_imx_pcm_prepare(struct snd_pcm_substream
*substream
)
194 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
195 struct imx_pcm_dma_params
*dma_params
;
197 dma_params
= snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
202 static int snd_imx_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
204 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
205 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
208 case SNDRV_PCM_TRIGGER_START
:
209 case SNDRV_PCM_TRIGGER_RESUME
:
210 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
211 dmaengine_submit(iprtd
->desc
);
212 dma_async_issue_pending(iprtd
->dma_chan
);
216 case SNDRV_PCM_TRIGGER_STOP
:
217 case SNDRV_PCM_TRIGGER_SUSPEND
:
218 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
219 dmaengine_terminate_all(iprtd
->dma_chan
);
229 static snd_pcm_uframes_t
snd_imx_pcm_pointer(struct snd_pcm_substream
*substream
)
231 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
232 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
234 pr_debug("%s: %ld %ld\n", __func__
, iprtd
->offset
,
235 bytes_to_frames(substream
->runtime
, iprtd
->offset
));
237 return bytes_to_frames(substream
->runtime
, iprtd
->offset
);
240 static struct snd_pcm_hardware snd_imx_hardware
= {
241 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
242 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
243 SNDRV_PCM_INFO_MMAP
|
244 SNDRV_PCM_INFO_MMAP_VALID
|
245 SNDRV_PCM_INFO_PAUSE
|
246 SNDRV_PCM_INFO_RESUME
,
247 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
251 .buffer_bytes_max
= IMX_SSI_DMABUF_SIZE
,
252 .period_bytes_min
= 128,
253 .period_bytes_max
= 65535, /* Limited by SDMA engine */
259 static int snd_imx_open(struct snd_pcm_substream
*substream
)
261 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
262 struct imx_pcm_runtime_data
*iprtd
;
265 iprtd
= kzalloc(sizeof(*iprtd
), GFP_KERNEL
);
268 runtime
->private_data
= iprtd
;
270 ret
= snd_pcm_hw_constraint_integer(substream
->runtime
,
271 SNDRV_PCM_HW_PARAM_PERIODS
);
277 snd_soc_set_runtime_hwparams(substream
, &snd_imx_hardware
);
282 static int snd_imx_close(struct snd_pcm_substream
*substream
)
284 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
285 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
292 static struct snd_pcm_ops imx_pcm_ops
= {
293 .open
= snd_imx_open
,
294 .close
= snd_imx_close
,
295 .ioctl
= snd_pcm_lib_ioctl
,
296 .hw_params
= snd_imx_pcm_hw_params
,
297 .hw_free
= snd_imx_pcm_hw_free
,
298 .prepare
= snd_imx_pcm_prepare
,
299 .trigger
= snd_imx_pcm_trigger
,
300 .pointer
= snd_imx_pcm_pointer
,
301 .mmap
= snd_imx_pcm_mmap
,
304 static struct snd_soc_platform_driver imx_soc_platform_mx2
= {
306 .pcm_new
= imx_pcm_new
,
307 .pcm_free
= imx_pcm_free
,
310 static int __devinit
imx_soc_platform_probe(struct platform_device
*pdev
)
312 struct imx_ssi
*ssi
= platform_get_drvdata(pdev
);
314 ssi
->dma_params_tx
.burstsize
= 6;
315 ssi
->dma_params_rx
.burstsize
= 4;
317 return snd_soc_register_platform(&pdev
->dev
, &imx_soc_platform_mx2
);
320 static int __devexit
imx_soc_platform_remove(struct platform_device
*pdev
)
322 snd_soc_unregister_platform(&pdev
->dev
);
326 static struct platform_driver imx_pcm_driver
= {
328 .name
= "imx-pcm-audio",
329 .owner
= THIS_MODULE
,
331 .probe
= imx_soc_platform_probe
,
332 .remove
= __devexit_p(imx_soc_platform_remove
),
335 module_platform_driver(imx_pcm_driver
);
336 MODULE_LICENSE("GPL");
337 MODULE_ALIAS("platform:imx-pcm-audio");