blob: 5f7e5b9c87a85c64c1cdaf4cd96ffe71013b1b5c [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
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070035#include <plat/cpu.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020036#include "omap-pcm.h"
37
38static const struct snd_pcm_hardware omap_pcm_hardware = {
39 .info = SNDRV_PCM_INFO_MMAP |
40 SNDRV_PCM_INFO_MMAP_VALID |
41 SNDRV_PCM_INFO_INTERLEAVED |
42 SNDRV_PCM_INFO_PAUSE |
Peter Ujfalusib4173822011-05-12 13:04:55 +030043 SNDRV_PCM_INFO_RESUME |
44 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
Misael Lopez Cruze17dd322010-02-22 15:09:19 -060045 .formats = SNDRV_PCM_FMTBIT_S16_LE |
46 SNDRV_PCM_FMTBIT_S32_LE,
Jarkko Nikula2e747962008-04-25 13:55:19 +020047 .period_bytes_min = 32,
48 .period_bytes_max = 64 * 1024,
49 .periods_min = 2,
50 .periods_max = 255,
51 .buffer_bytes_max = 128 * 1024,
52};
53
Peter Ujfalusi946cc362012-09-14 15:05:58 +030054static int omap_pcm_get_dma_buswidth(int num_bits)
Jarkko Nikula2e747962008-04-25 13:55:19 +020055{
Peter Ujfalusi946cc362012-09-14 15:05:58 +030056 int buswidth;
Jarkko Nikula2e747962008-04-25 13:55:19 +020057
Peter Ujfalusi946cc362012-09-14 15:05:58 +030058 switch (num_bits) {
59 case 16:
60 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
61 break;
62 case 32:
63 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
64 break;
65 default:
66 buswidth = -EINVAL;
67 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +020068 }
Peter Ujfalusi946cc362012-09-14 15:05:58 +030069 return buswidth;
Jarkko Nikula2e747962008-04-25 13:55:19 +020070}
71
Peter Ujfalusi946cc362012-09-14 15:05:58 +030072
Jarkko Nikula2e747962008-04-25 13:55:19 +020073/* this may get called several times by oss emulation */
74static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
75 struct snd_pcm_hw_params *params)
76{
77 struct snd_pcm_runtime *runtime = substream->runtime;
78 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +010079 struct omap_pcm_dma_data *dma_data;
Peter Ujfalusi946cc362012-09-14 15:05:58 +030080 struct dma_slave_config config;
81 struct dma_chan *chan;
Jarkko Nikula2e747962008-04-25 13:55:19 +020082 int err = 0;
83
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000084 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Daniel Mack5f712b22010-03-22 10:11:15 +010085
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090086 /* return if this is a bufferless transfer e.g.
87 * codec <--> BT codec or GSM modem -- lg FIXME */
Jarkko Nikula2e747962008-04-25 13:55:19 +020088 if (!dma_data)
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090089 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +020090
91 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
92 runtime->dma_bytes = params_buffer_bytes(params);
93
Peter Ujfalusi946cc362012-09-14 15:05:58 +030094 chan = snd_dmaengine_pcm_get_chan(substream);
95 if (!chan)
96 return -EINVAL;
97
98 /* fills in addr_width and direction */
99 err = snd_hwparams_to_dma_slave_config(substream, params, &config);
100 if (err)
101 return err;
102
103 /* Override the *_dma addr_width if requested by the DAI driver */
104 if (dma_data->data_type) {
105 int buswidth = omap_pcm_get_dma_buswidth(dma_data->data_type);
106
107 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
108 config.dst_addr_width = buswidth;
109 else
110 config.src_addr_width = buswidth;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200111 }
112
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300113 config.src_addr = dma_data->port_addr;
114 config.dst_addr = dma_data->port_addr;
115 config.src_maxburst = dma_data->packet_size;
116 config.dst_maxburst = dma_data->packet_size;
117
118 return dmaengine_slave_config(chan, &config);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200119}
120
121static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
122{
Jarkko Nikula2e747962008-04-25 13:55:19 +0200123 snd_pcm_set_runtime_buffer(substream, NULL);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200124 return 0;
125}
126
127static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
128{
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300129 struct snd_soc_pcm_runtime *rtd = substream->private_data;
130 struct omap_pcm_dma_data *dma_data;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200131 int ret = 0;
132
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300133 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
134
Jarkko Nikula2e747962008-04-25 13:55:19 +0200135 switch (cmd) {
136 case SNDRV_PCM_TRIGGER_START:
137 case SNDRV_PCM_TRIGGER_RESUME:
138 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300139 /* Configure McBSP internal buffer usage */
140 if (dma_data->set_threshold)
141 dma_data->set_threshold(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200142 break;
143
144 case SNDRV_PCM_TRIGGER_STOP:
145 case SNDRV_PCM_TRIGGER_SUSPEND:
146 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200147 break;
148 default:
149 ret = -EINVAL;
150 }
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300151
152 if (ret == 0)
153 ret = snd_dmaengine_pcm_trigger(substream, cmd);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200154
155 return ret;
156}
157
158static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
159{
Jarkko Nikula2e747962008-04-25 13:55:19 +0200160 snd_pcm_uframes_t offset;
161
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300162 if (cpu_is_omap1510())
163 offset = snd_dmaengine_pcm_pointer_no_residue(substream);
164 else
165 offset = snd_dmaengine_pcm_pointer(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200166
167 return offset;
168}
169
170static int omap_pcm_open(struct snd_pcm_substream *substream)
171{
172 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300173 struct snd_soc_pcm_runtime *rtd = substream->private_data;
174 struct omap_pcm_dma_data *dma_data;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200175 int ret;
176
177 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
178
179 /* Ensure that buffer size is a multiple of period size */
180 ret = snd_pcm_hw_constraint_integer(runtime,
181 SNDRV_PCM_HW_PARAM_PERIODS);
182 if (ret < 0)
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300183 return ret;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200184
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300185 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
186 ret = snd_dmaengine_pcm_open(substream, omap_dma_filter_fn,
187 &dma_data->dma_req);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200188 return ret;
189}
190
191static int omap_pcm_close(struct snd_pcm_substream *substream)
192{
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300193 snd_dmaengine_pcm_close(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200194 return 0;
195}
196
197static int omap_pcm_mmap(struct snd_pcm_substream *substream,
198 struct vm_area_struct *vma)
199{
200 struct snd_pcm_runtime *runtime = substream->runtime;
201
202 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
203 runtime->dma_area,
204 runtime->dma_addr,
205 runtime->dma_bytes);
206}
207
Mark Brownb2a19d02009-01-17 19:14:26 +0000208static struct snd_pcm_ops omap_pcm_ops = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200209 .open = omap_pcm_open,
210 .close = omap_pcm_close,
211 .ioctl = snd_pcm_lib_ioctl,
212 .hw_params = omap_pcm_hw_params,
213 .hw_free = omap_pcm_hw_free,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200214 .trigger = omap_pcm_trigger,
215 .pointer = omap_pcm_pointer,
216 .mmap = omap_pcm_mmap,
217};
218
Eduardo Valentina152ff22009-08-20 16:18:22 +0300219static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200220
221static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
222 int stream)
223{
224 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
225 struct snd_dma_buffer *buf = &substream->dma_buffer;
226 size_t size = omap_pcm_hardware.buffer_bytes_max;
227
228 buf->dev.type = SNDRV_DMA_TYPE_DEV;
229 buf->dev.dev = pcm->card->dev;
230 buf->private_data = NULL;
231 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
232 &buf->addr, GFP_KERNEL);
233 if (!buf->area)
234 return -ENOMEM;
235
236 buf->bytes = size;
237 return 0;
238}
239
240static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
241{
242 struct snd_pcm_substream *substream;
243 struct snd_dma_buffer *buf;
244 int stream;
245
246 for (stream = 0; stream < 2; stream++) {
247 substream = pcm->streams[stream].substream;
248 if (!substream)
249 continue;
250
251 buf = &substream->dma_buffer;
252 if (!buf->area)
253 continue;
254
255 dma_free_writecombine(pcm->card->dev, buf->bytes,
256 buf->area, buf->addr);
257 buf->area = NULL;
258 }
259}
260
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100261static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200262{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100263 struct snd_card *card = rtd->card->snd_card;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100264 struct snd_pcm *pcm = rtd->pcm;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200265 int ret = 0;
266
267 if (!card->dev->dma_mask)
268 card->dev->dma_mask = &omap_pcm_dmamask;
269 if (!card->dev->coherent_dma_mask)
Eduardo Valentina152ff22009-08-20 16:18:22 +0300270 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200271
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100272 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200273 ret = omap_pcm_preallocate_dma_buffer(pcm,
274 SNDRV_PCM_STREAM_PLAYBACK);
275 if (ret)
276 goto out;
277 }
278
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100279 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200280 ret = omap_pcm_preallocate_dma_buffer(pcm,
281 SNDRV_PCM_STREAM_CAPTURE);
282 if (ret)
283 goto out;
284 }
285
286out:
Oleg Matcovschifad93652012-04-24 19:02:02 -0700287 /* free preallocated buffers in case of error */
288 if (ret)
289 omap_pcm_free_dma_buffers(pcm);
290
Jarkko Nikula2e747962008-04-25 13:55:19 +0200291 return ret;
292}
293
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000294static struct snd_soc_platform_driver omap_soc_platform = {
295 .ops = &omap_pcm_ops,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200296 .pcm_new = omap_pcm_new,
297 .pcm_free = omap_pcm_free_dma_buffers,
298};
Jarkko Nikula2e747962008-04-25 13:55:19 +0200299
Bill Pemberton7ff60002012-12-07 09:26:29 -0500300static int omap_pcm_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000301{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000302 return snd_soc_register_platform(&pdev->dev,
303 &omap_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000304}
Mark Brown958e7922008-12-03 19:58:17 +0000305
Bill Pemberton7ff60002012-12-07 09:26:29 -0500306static int omap_pcm_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000307{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000308 snd_soc_unregister_platform(&pdev->dev);
309 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000310}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000311
312static struct platform_driver omap_pcm_driver = {
313 .driver = {
314 .name = "omap-pcm-audio",
315 .owner = THIS_MODULE,
316 },
317
318 .probe = omap_pcm_probe,
Bill Pemberton7ff60002012-12-07 09:26:29 -0500319 .remove = omap_pcm_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000320};
321
Axel Linbeda5bf52011-11-25 10:12:16 +0800322module_platform_driver(omap_pcm_driver);
Mark Brown958e7922008-12-03 19:58:17 +0000323
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +0300324MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200325MODULE_DESCRIPTION("OMAP PCM DMA module");
326MODULE_LICENSE("GPL");
Guillaume Gardet5e70b7fc2012-07-12 15:08:16 +0200327MODULE_ALIAS("platform:omap-pcm-audio");