blob: a2636f6b836210ea8b55645e5906114535fb904f [file] [log] [blame]
Jarkko Nikula2e747962008-04-25 13:55:19 +02001/*
2 * omap-pcm.c -- ALSA PCM interface for the OMAP SoC
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +03006 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
Peter Ujfalusi1c7687b2011-05-03 18:14:43 +03007 * Peter Ujfalusi <peter.ujfalusi@ti.com>
Jarkko Nikula2e747962008-04-25 13:55:19 +02008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Peter Ujfalusi946cc362012-09-14 15:05:58 +030028#include <linux/omap-dma.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020029#include <sound/core.h>
30#include <sound/pcm.h>
31#include <sound/pcm_params.h>
Peter Ujfalusi946cc362012-09-14 15:05:58 +030032#include <sound/dmaengine_pcm.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020033#include <sound/soc.h>
34
Jarkko Nikula2e747962008-04-25 13:55:19 +020035#include "omap-pcm.h"
36
37static const struct snd_pcm_hardware omap_pcm_hardware = {
38 .info = SNDRV_PCM_INFO_MMAP |
39 SNDRV_PCM_INFO_MMAP_VALID |
40 SNDRV_PCM_INFO_INTERLEAVED |
41 SNDRV_PCM_INFO_PAUSE |
Peter Ujfalusib4173822011-05-12 13:04:55 +030042 SNDRV_PCM_INFO_RESUME |
43 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
Misael Lopez Cruze17dd322010-02-22 15:09:19 -060044 .formats = SNDRV_PCM_FMTBIT_S16_LE |
45 SNDRV_PCM_FMTBIT_S32_LE,
Jarkko Nikula2e747962008-04-25 13:55:19 +020046 .period_bytes_min = 32,
47 .period_bytes_max = 64 * 1024,
48 .periods_min = 2,
49 .periods_max = 255,
50 .buffer_bytes_max = 128 * 1024,
51};
52
Peter Ujfalusi946cc362012-09-14 15:05:58 +030053static int omap_pcm_get_dma_buswidth(int num_bits)
Jarkko Nikula2e747962008-04-25 13:55:19 +020054{
Peter Ujfalusi946cc362012-09-14 15:05:58 +030055 int buswidth;
Jarkko Nikula2e747962008-04-25 13:55:19 +020056
Peter Ujfalusi946cc362012-09-14 15:05:58 +030057 switch (num_bits) {
58 case 16:
59 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
60 break;
61 case 32:
62 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
63 break;
64 default:
65 buswidth = -EINVAL;
66 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +020067 }
Peter Ujfalusi946cc362012-09-14 15:05:58 +030068 return buswidth;
Jarkko Nikula2e747962008-04-25 13:55:19 +020069}
70
Peter Ujfalusi946cc362012-09-14 15:05:58 +030071
Jarkko Nikula2e747962008-04-25 13:55:19 +020072/* this may get called several times by oss emulation */
73static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
74 struct snd_pcm_hw_params *params)
75{
76 struct snd_pcm_runtime *runtime = substream->runtime;
77 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +010078 struct omap_pcm_dma_data *dma_data;
Peter Ujfalusi946cc362012-09-14 15:05:58 +030079 struct dma_slave_config config;
80 struct dma_chan *chan;
Jarkko Nikula2e747962008-04-25 13:55:19 +020081 int err = 0;
82
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000083 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Daniel Mack5f712b22010-03-22 10:11:15 +010084
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090085 /* return if this is a bufferless transfer e.g.
86 * codec <--> BT codec or GSM modem -- lg FIXME */
Jarkko Nikula2e747962008-04-25 13:55:19 +020087 if (!dma_data)
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090088 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +020089
90 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
91 runtime->dma_bytes = params_buffer_bytes(params);
92
Peter Ujfalusi946cc362012-09-14 15:05:58 +030093 chan = snd_dmaengine_pcm_get_chan(substream);
94 if (!chan)
95 return -EINVAL;
96
97 /* fills in addr_width and direction */
98 err = snd_hwparams_to_dma_slave_config(substream, params, &config);
99 if (err)
100 return err;
101
102 /* Override the *_dma addr_width if requested by the DAI driver */
103 if (dma_data->data_type) {
104 int buswidth = omap_pcm_get_dma_buswidth(dma_data->data_type);
105
106 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
107 config.dst_addr_width = buswidth;
108 else
109 config.src_addr_width = buswidth;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200110 }
111
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300112 config.src_addr = dma_data->port_addr;
113 config.dst_addr = dma_data->port_addr;
114 config.src_maxburst = dma_data->packet_size;
115 config.dst_maxburst = dma_data->packet_size;
116
117 return dmaengine_slave_config(chan, &config);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200118}
119
120static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
121{
Jarkko Nikula2e747962008-04-25 13:55:19 +0200122 snd_pcm_set_runtime_buffer(substream, NULL);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200123 return 0;
124}
125
126static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
127{
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
129 struct omap_pcm_dma_data *dma_data;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200130 int ret = 0;
131
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300132 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
133
Jarkko Nikula2e747962008-04-25 13:55:19 +0200134 switch (cmd) {
135 case SNDRV_PCM_TRIGGER_START:
136 case SNDRV_PCM_TRIGGER_RESUME:
137 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300138 /* Configure McBSP internal buffer usage */
139 if (dma_data->set_threshold)
140 dma_data->set_threshold(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200141 break;
142
143 case SNDRV_PCM_TRIGGER_STOP:
144 case SNDRV_PCM_TRIGGER_SUSPEND:
145 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200146 break;
147 default:
148 ret = -EINVAL;
149 }
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300150
151 if (ret == 0)
152 ret = snd_dmaengine_pcm_trigger(substream, cmd);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200153
154 return ret;
155}
156
157static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
158{
Jarkko Nikula2e747962008-04-25 13:55:19 +0200159 snd_pcm_uframes_t offset;
160
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300161 if (cpu_is_omap1510())
162 offset = snd_dmaengine_pcm_pointer_no_residue(substream);
163 else
164 offset = snd_dmaengine_pcm_pointer(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200165
166 return offset;
167}
168
169static int omap_pcm_open(struct snd_pcm_substream *substream)
170{
171 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300172 struct snd_soc_pcm_runtime *rtd = substream->private_data;
173 struct omap_pcm_dma_data *dma_data;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200174 int ret;
175
176 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
177
178 /* Ensure that buffer size is a multiple of period size */
179 ret = snd_pcm_hw_constraint_integer(runtime,
180 SNDRV_PCM_HW_PARAM_PERIODS);
181 if (ret < 0)
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300182 return ret;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200183
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300184 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
185 ret = snd_dmaengine_pcm_open(substream, omap_dma_filter_fn,
186 &dma_data->dma_req);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200187 return ret;
188}
189
190static int omap_pcm_close(struct snd_pcm_substream *substream)
191{
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300192 snd_dmaengine_pcm_close(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200193 return 0;
194}
195
196static int omap_pcm_mmap(struct snd_pcm_substream *substream,
197 struct vm_area_struct *vma)
198{
199 struct snd_pcm_runtime *runtime = substream->runtime;
200
201 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
202 runtime->dma_area,
203 runtime->dma_addr,
204 runtime->dma_bytes);
205}
206
Mark Brownb2a19d02009-01-17 19:14:26 +0000207static struct snd_pcm_ops omap_pcm_ops = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200208 .open = omap_pcm_open,
209 .close = omap_pcm_close,
210 .ioctl = snd_pcm_lib_ioctl,
211 .hw_params = omap_pcm_hw_params,
212 .hw_free = omap_pcm_hw_free,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200213 .trigger = omap_pcm_trigger,
214 .pointer = omap_pcm_pointer,
215 .mmap = omap_pcm_mmap,
216};
217
Eduardo Valentina152ff22009-08-20 16:18:22 +0300218static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200219
220static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
221 int stream)
222{
223 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
224 struct snd_dma_buffer *buf = &substream->dma_buffer;
225 size_t size = omap_pcm_hardware.buffer_bytes_max;
226
227 buf->dev.type = SNDRV_DMA_TYPE_DEV;
228 buf->dev.dev = pcm->card->dev;
229 buf->private_data = NULL;
230 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
231 &buf->addr, GFP_KERNEL);
232 if (!buf->area)
233 return -ENOMEM;
234
235 buf->bytes = size;
236 return 0;
237}
238
239static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
240{
241 struct snd_pcm_substream *substream;
242 struct snd_dma_buffer *buf;
243 int stream;
244
245 for (stream = 0; stream < 2; stream++) {
246 substream = pcm->streams[stream].substream;
247 if (!substream)
248 continue;
249
250 buf = &substream->dma_buffer;
251 if (!buf->area)
252 continue;
253
254 dma_free_writecombine(pcm->card->dev, buf->bytes,
255 buf->area, buf->addr);
256 buf->area = NULL;
257 }
258}
259
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100260static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200261{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100262 struct snd_card *card = rtd->card->snd_card;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100263 struct snd_pcm *pcm = rtd->pcm;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200264 int ret = 0;
265
266 if (!card->dev->dma_mask)
267 card->dev->dma_mask = &omap_pcm_dmamask;
268 if (!card->dev->coherent_dma_mask)
Eduardo Valentina152ff22009-08-20 16:18:22 +0300269 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200270
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100271 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200272 ret = omap_pcm_preallocate_dma_buffer(pcm,
273 SNDRV_PCM_STREAM_PLAYBACK);
274 if (ret)
275 goto out;
276 }
277
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100278 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200279 ret = omap_pcm_preallocate_dma_buffer(pcm,
280 SNDRV_PCM_STREAM_CAPTURE);
281 if (ret)
282 goto out;
283 }
284
285out:
Oleg Matcovschifad93652012-04-24 19:02:02 -0700286 /* free preallocated buffers in case of error */
287 if (ret)
288 omap_pcm_free_dma_buffers(pcm);
289
Jarkko Nikula2e747962008-04-25 13:55:19 +0200290 return ret;
291}
292
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000293static struct snd_soc_platform_driver omap_soc_platform = {
294 .ops = &omap_pcm_ops,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200295 .pcm_new = omap_pcm_new,
296 .pcm_free = omap_pcm_free_dma_buffers,
297};
Jarkko Nikula2e747962008-04-25 13:55:19 +0200298
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000299static __devinit int omap_pcm_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000300{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000301 return snd_soc_register_platform(&pdev->dev,
302 &omap_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000303}
Mark Brown958e7922008-12-03 19:58:17 +0000304
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000305static int __devexit omap_pcm_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000306{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000307 snd_soc_unregister_platform(&pdev->dev);
308 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000309}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000310
311static struct platform_driver omap_pcm_driver = {
312 .driver = {
313 .name = "omap-pcm-audio",
314 .owner = THIS_MODULE,
315 },
316
317 .probe = omap_pcm_probe,
318 .remove = __devexit_p(omap_pcm_remove),
319};
320
Axel Linbeda5bf52011-11-25 10:12:16 +0800321module_platform_driver(omap_pcm_driver);
Mark Brown958e7922008-12-03 19:58:17 +0000322
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +0300323MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200324MODULE_DESCRIPTION("OMAP PCM DMA module");
325MODULE_LICENSE("GPL");
Guillaume Gardet5e70b7fc2012-07-12 15:08:16 +0200326MODULE_ALIAS("platform:omap-pcm-audio");