blob: 52977aa303554d54113530805e421f450db68e33 [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
Tony Lindgren27615a92012-10-15 16:24:23 -070037#ifdef CONFIG_ARCH_OMAP1
38#define pcm_omap1510() cpu_is_omap1510()
39#else
40#define pcm_omap1510() 0
41#endif
42
Jarkko Nikula2e747962008-04-25 13:55:19 +020043static const struct snd_pcm_hardware omap_pcm_hardware = {
44 .info = SNDRV_PCM_INFO_MMAP |
45 SNDRV_PCM_INFO_MMAP_VALID |
46 SNDRV_PCM_INFO_INTERLEAVED |
47 SNDRV_PCM_INFO_PAUSE |
Peter Ujfalusib4173822011-05-12 13:04:55 +030048 SNDRV_PCM_INFO_RESUME |
49 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
Misael Lopez Cruze17dd322010-02-22 15:09:19 -060050 .formats = SNDRV_PCM_FMTBIT_S16_LE |
51 SNDRV_PCM_FMTBIT_S32_LE,
Jarkko Nikula2e747962008-04-25 13:55:19 +020052 .period_bytes_min = 32,
53 .period_bytes_max = 64 * 1024,
54 .periods_min = 2,
55 .periods_max = 255,
56 .buffer_bytes_max = 128 * 1024,
57};
58
Peter Ujfalusi946cc362012-09-14 15:05:58 +030059static int omap_pcm_get_dma_buswidth(int num_bits)
Jarkko Nikula2e747962008-04-25 13:55:19 +020060{
Peter Ujfalusi946cc362012-09-14 15:05:58 +030061 int buswidth;
Jarkko Nikula2e747962008-04-25 13:55:19 +020062
Peter Ujfalusi946cc362012-09-14 15:05:58 +030063 switch (num_bits) {
64 case 16:
65 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
66 break;
67 case 32:
68 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
69 break;
70 default:
71 buswidth = -EINVAL;
72 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +020073 }
Peter Ujfalusi946cc362012-09-14 15:05:58 +030074 return buswidth;
Jarkko Nikula2e747962008-04-25 13:55:19 +020075}
76
Peter Ujfalusi946cc362012-09-14 15:05:58 +030077
Jarkko Nikula2e747962008-04-25 13:55:19 +020078/* this may get called several times by oss emulation */
79static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
80 struct snd_pcm_hw_params *params)
81{
82 struct snd_pcm_runtime *runtime = substream->runtime;
83 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +010084 struct omap_pcm_dma_data *dma_data;
Peter Ujfalusi946cc362012-09-14 15:05:58 +030085 struct dma_slave_config config;
86 struct dma_chan *chan;
Jarkko Nikula2e747962008-04-25 13:55:19 +020087 int err = 0;
88
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000089 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Daniel Mack5f712b22010-03-22 10:11:15 +010090
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090091 /* return if this is a bufferless transfer e.g.
92 * codec <--> BT codec or GSM modem -- lg FIXME */
Jarkko Nikula2e747962008-04-25 13:55:19 +020093 if (!dma_data)
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090094 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +020095
96 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
97 runtime->dma_bytes = params_buffer_bytes(params);
98
Peter Ujfalusi946cc362012-09-14 15:05:58 +030099 chan = snd_dmaengine_pcm_get_chan(substream);
100 if (!chan)
101 return -EINVAL;
102
103 /* fills in addr_width and direction */
104 err = snd_hwparams_to_dma_slave_config(substream, params, &config);
105 if (err)
106 return err;
107
108 /* Override the *_dma addr_width if requested by the DAI driver */
109 if (dma_data->data_type) {
110 int buswidth = omap_pcm_get_dma_buswidth(dma_data->data_type);
111
112 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
113 config.dst_addr_width = buswidth;
114 else
115 config.src_addr_width = buswidth;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200116 }
117
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300118 config.src_addr = dma_data->port_addr;
119 config.dst_addr = dma_data->port_addr;
120 config.src_maxburst = dma_data->packet_size;
121 config.dst_maxburst = dma_data->packet_size;
122
123 return dmaengine_slave_config(chan, &config);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200124}
125
126static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
127{
Jarkko Nikula2e747962008-04-25 13:55:19 +0200128 snd_pcm_set_runtime_buffer(substream, NULL);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200129 return 0;
130}
131
132static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
133{
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300134 struct snd_soc_pcm_runtime *rtd = substream->private_data;
135 struct omap_pcm_dma_data *dma_data;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200136 int ret = 0;
137
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300138 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
139
Jarkko Nikula2e747962008-04-25 13:55:19 +0200140 switch (cmd) {
141 case SNDRV_PCM_TRIGGER_START:
142 case SNDRV_PCM_TRIGGER_RESUME:
143 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300144 /* Configure McBSP internal buffer usage */
145 if (dma_data->set_threshold)
146 dma_data->set_threshold(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200147 break;
148
149 case SNDRV_PCM_TRIGGER_STOP:
150 case SNDRV_PCM_TRIGGER_SUSPEND:
151 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200152 break;
153 default:
154 ret = -EINVAL;
155 }
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300156
157 if (ret == 0)
158 ret = snd_dmaengine_pcm_trigger(substream, cmd);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200159
160 return ret;
161}
162
163static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
164{
Jarkko Nikula2e747962008-04-25 13:55:19 +0200165 snd_pcm_uframes_t offset;
166
Tony Lindgren27615a92012-10-15 16:24:23 -0700167 if (pcm_omap1510())
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300168 offset = snd_dmaengine_pcm_pointer_no_residue(substream);
169 else
170 offset = snd_dmaengine_pcm_pointer(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200171
172 return offset;
173}
174
175static int omap_pcm_open(struct snd_pcm_substream *substream)
176{
177 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300178 struct snd_soc_pcm_runtime *rtd = substream->private_data;
179 struct omap_pcm_dma_data *dma_data;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200180 int ret;
181
182 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
183
184 /* Ensure that buffer size is a multiple of period size */
185 ret = snd_pcm_hw_constraint_integer(runtime,
186 SNDRV_PCM_HW_PARAM_PERIODS);
187 if (ret < 0)
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300188 return ret;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200189
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300190 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
191 ret = snd_dmaengine_pcm_open(substream, omap_dma_filter_fn,
192 &dma_data->dma_req);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200193 return ret;
194}
195
196static int omap_pcm_close(struct snd_pcm_substream *substream)
197{
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300198 snd_dmaengine_pcm_close(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200199 return 0;
200}
201
202static int omap_pcm_mmap(struct snd_pcm_substream *substream,
203 struct vm_area_struct *vma)
204{
205 struct snd_pcm_runtime *runtime = substream->runtime;
206
207 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
208 runtime->dma_area,
209 runtime->dma_addr,
210 runtime->dma_bytes);
211}
212
Mark Brownb2a19d02009-01-17 19:14:26 +0000213static struct snd_pcm_ops omap_pcm_ops = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200214 .open = omap_pcm_open,
215 .close = omap_pcm_close,
216 .ioctl = snd_pcm_lib_ioctl,
217 .hw_params = omap_pcm_hw_params,
218 .hw_free = omap_pcm_hw_free,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200219 .trigger = omap_pcm_trigger,
220 .pointer = omap_pcm_pointer,
221 .mmap = omap_pcm_mmap,
222};
223
Eduardo Valentina152ff22009-08-20 16:18:22 +0300224static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200225
226static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
227 int stream)
228{
229 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
230 struct snd_dma_buffer *buf = &substream->dma_buffer;
231 size_t size = omap_pcm_hardware.buffer_bytes_max;
232
233 buf->dev.type = SNDRV_DMA_TYPE_DEV;
234 buf->dev.dev = pcm->card->dev;
235 buf->private_data = NULL;
236 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
237 &buf->addr, GFP_KERNEL);
238 if (!buf->area)
239 return -ENOMEM;
240
241 buf->bytes = size;
242 return 0;
243}
244
245static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
246{
247 struct snd_pcm_substream *substream;
248 struct snd_dma_buffer *buf;
249 int stream;
250
251 for (stream = 0; stream < 2; stream++) {
252 substream = pcm->streams[stream].substream;
253 if (!substream)
254 continue;
255
256 buf = &substream->dma_buffer;
257 if (!buf->area)
258 continue;
259
260 dma_free_writecombine(pcm->card->dev, buf->bytes,
261 buf->area, buf->addr);
262 buf->area = NULL;
263 }
264}
265
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100266static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200267{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100268 struct snd_card *card = rtd->card->snd_card;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100269 struct snd_pcm *pcm = rtd->pcm;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200270 int ret = 0;
271
272 if (!card->dev->dma_mask)
273 card->dev->dma_mask = &omap_pcm_dmamask;
274 if (!card->dev->coherent_dma_mask)
Eduardo Valentina152ff22009-08-20 16:18:22 +0300275 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200276
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100277 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200278 ret = omap_pcm_preallocate_dma_buffer(pcm,
279 SNDRV_PCM_STREAM_PLAYBACK);
280 if (ret)
281 goto out;
282 }
283
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100284 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200285 ret = omap_pcm_preallocate_dma_buffer(pcm,
286 SNDRV_PCM_STREAM_CAPTURE);
287 if (ret)
288 goto out;
289 }
290
291out:
Oleg Matcovschifad93652012-04-24 19:02:02 -0700292 /* free preallocated buffers in case of error */
293 if (ret)
294 omap_pcm_free_dma_buffers(pcm);
295
Jarkko Nikula2e747962008-04-25 13:55:19 +0200296 return ret;
297}
298
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000299static struct snd_soc_platform_driver omap_soc_platform = {
300 .ops = &omap_pcm_ops,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200301 .pcm_new = omap_pcm_new,
302 .pcm_free = omap_pcm_free_dma_buffers,
303};
Jarkko Nikula2e747962008-04-25 13:55:19 +0200304
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000305static __devinit int omap_pcm_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000306{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000307 return snd_soc_register_platform(&pdev->dev,
308 &omap_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000309}
Mark Brown958e7922008-12-03 19:58:17 +0000310
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000311static int __devexit omap_pcm_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000312{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000313 snd_soc_unregister_platform(&pdev->dev);
314 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000315}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000316
317static struct platform_driver omap_pcm_driver = {
318 .driver = {
319 .name = "omap-pcm-audio",
320 .owner = THIS_MODULE,
321 },
322
323 .probe = omap_pcm_probe,
324 .remove = __devexit_p(omap_pcm_remove),
325};
326
Axel Linbeda5bf52011-11-25 10:12:16 +0800327module_platform_driver(omap_pcm_driver);
Mark Brown958e7922008-12-03 19:58:17 +0000328
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +0300329MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200330MODULE_DESCRIPTION("OMAP PCM DMA module");
331MODULE_LICENSE("GPL");
Guillaume Gardet5e70b7fc2012-07-12 15:08:16 +0200332MODULE_ALIAS("platform:omap-pcm-audio");