blob: c722c2ef9665c9d6b0e3082077a77755bd4d95c9 [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{
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
178 struct omap_pcm_dma_data *dma_data;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200179
180 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
181
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300182 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Peter Ujfalusia92b5312013-02-05 13:43:39 +0100183
184 return snd_dmaengine_pcm_open(substream, omap_dma_filter_fn,
185 &dma_data->dma_req);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200186}
187
188static int omap_pcm_close(struct snd_pcm_substream *substream)
189{
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300190 snd_dmaengine_pcm_close(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200191 return 0;
192}
193
194static int omap_pcm_mmap(struct snd_pcm_substream *substream,
195 struct vm_area_struct *vma)
196{
197 struct snd_pcm_runtime *runtime = substream->runtime;
198
199 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
200 runtime->dma_area,
201 runtime->dma_addr,
202 runtime->dma_bytes);
203}
204
Mark Brownb2a19d02009-01-17 19:14:26 +0000205static struct snd_pcm_ops omap_pcm_ops = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200206 .open = omap_pcm_open,
207 .close = omap_pcm_close,
208 .ioctl = snd_pcm_lib_ioctl,
209 .hw_params = omap_pcm_hw_params,
210 .hw_free = omap_pcm_hw_free,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200211 .trigger = omap_pcm_trigger,
212 .pointer = omap_pcm_pointer,
213 .mmap = omap_pcm_mmap,
214};
215
Eduardo Valentina152ff22009-08-20 16:18:22 +0300216static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200217
218static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
219 int stream)
220{
221 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
222 struct snd_dma_buffer *buf = &substream->dma_buffer;
223 size_t size = omap_pcm_hardware.buffer_bytes_max;
224
225 buf->dev.type = SNDRV_DMA_TYPE_DEV;
226 buf->dev.dev = pcm->card->dev;
227 buf->private_data = NULL;
228 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
229 &buf->addr, GFP_KERNEL);
230 if (!buf->area)
231 return -ENOMEM;
232
233 buf->bytes = size;
234 return 0;
235}
236
237static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
238{
239 struct snd_pcm_substream *substream;
240 struct snd_dma_buffer *buf;
241 int stream;
242
243 for (stream = 0; stream < 2; stream++) {
244 substream = pcm->streams[stream].substream;
245 if (!substream)
246 continue;
247
248 buf = &substream->dma_buffer;
249 if (!buf->area)
250 continue;
251
252 dma_free_writecombine(pcm->card->dev, buf->bytes,
253 buf->area, buf->addr);
254 buf->area = NULL;
255 }
256}
257
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100258static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200259{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100260 struct snd_card *card = rtd->card->snd_card;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100261 struct snd_pcm *pcm = rtd->pcm;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200262 int ret = 0;
263
264 if (!card->dev->dma_mask)
265 card->dev->dma_mask = &omap_pcm_dmamask;
266 if (!card->dev->coherent_dma_mask)
Eduardo Valentina152ff22009-08-20 16:18:22 +0300267 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200268
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100269 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200270 ret = omap_pcm_preallocate_dma_buffer(pcm,
271 SNDRV_PCM_STREAM_PLAYBACK);
272 if (ret)
273 goto out;
274 }
275
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100276 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200277 ret = omap_pcm_preallocate_dma_buffer(pcm,
278 SNDRV_PCM_STREAM_CAPTURE);
279 if (ret)
280 goto out;
281 }
282
283out:
Oleg Matcovschifad93652012-04-24 19:02:02 -0700284 /* free preallocated buffers in case of error */
285 if (ret)
286 omap_pcm_free_dma_buffers(pcm);
287
Jarkko Nikula2e747962008-04-25 13:55:19 +0200288 return ret;
289}
290
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000291static struct snd_soc_platform_driver omap_soc_platform = {
292 .ops = &omap_pcm_ops,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200293 .pcm_new = omap_pcm_new,
294 .pcm_free = omap_pcm_free_dma_buffers,
295};
Jarkko Nikula2e747962008-04-25 13:55:19 +0200296
Bill Pemberton7ff60002012-12-07 09:26:29 -0500297static int omap_pcm_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000298{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000299 return snd_soc_register_platform(&pdev->dev,
300 &omap_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000301}
Mark Brown958e7922008-12-03 19:58:17 +0000302
Bill Pemberton7ff60002012-12-07 09:26:29 -0500303static int omap_pcm_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000304{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000305 snd_soc_unregister_platform(&pdev->dev);
306 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000307}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000308
309static struct platform_driver omap_pcm_driver = {
310 .driver = {
311 .name = "omap-pcm-audio",
312 .owner = THIS_MODULE,
313 },
314
315 .probe = omap_pcm_probe,
Bill Pemberton7ff60002012-12-07 09:26:29 -0500316 .remove = omap_pcm_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000317};
318
Axel Linbeda5bf52011-11-25 10:12:16 +0800319module_platform_driver(omap_pcm_driver);
Mark Brown958e7922008-12-03 19:58:17 +0000320
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +0300321MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200322MODULE_DESCRIPTION("OMAP PCM DMA module");
323MODULE_LICENSE("GPL");
Guillaume Gardet5e70b7fc2012-07-12 15:08:16 +0200324MODULE_ALIAS("platform:omap-pcm-audio");