blob: a11405de86e82bda8801e837ec48183093bd99db [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 Lindgren27615a92012-10-15 16:24:23 -070035#ifdef CONFIG_ARCH_OMAP1
36#define pcm_omap1510() cpu_is_omap1510()
37#else
38#define pcm_omap1510() 0
39#endif
40
Jarkko Nikula2e747962008-04-25 13:55:19 +020041static const struct snd_pcm_hardware omap_pcm_hardware = {
42 .info = SNDRV_PCM_INFO_MMAP |
43 SNDRV_PCM_INFO_MMAP_VALID |
44 SNDRV_PCM_INFO_INTERLEAVED |
45 SNDRV_PCM_INFO_PAUSE |
Peter Ujfalusib4173822011-05-12 13:04:55 +030046 SNDRV_PCM_INFO_RESUME |
47 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
Misael Lopez Cruze17dd322010-02-22 15:09:19 -060048 .formats = SNDRV_PCM_FMTBIT_S16_LE |
49 SNDRV_PCM_FMTBIT_S32_LE,
Jarkko Nikula2e747962008-04-25 13:55:19 +020050 .period_bytes_min = 32,
51 .period_bytes_max = 64 * 1024,
52 .periods_min = 2,
53 .periods_max = 255,
54 .buffer_bytes_max = 128 * 1024,
55};
56
Jarkko Nikula2e747962008-04-25 13:55:19 +020057/* this may get called several times by oss emulation */
58static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
59 struct snd_pcm_hw_params *params)
60{
61 struct snd_pcm_runtime *runtime = substream->runtime;
62 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +010063 struct omap_pcm_dma_data *dma_data;
Peter Ujfalusi946cc362012-09-14 15:05:58 +030064 struct dma_slave_config config;
65 struct dma_chan *chan;
Jarkko Nikula2e747962008-04-25 13:55:19 +020066 int err = 0;
67
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000068 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Daniel Mack5f712b22010-03-22 10:11:15 +010069
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090070 /* return if this is a bufferless transfer e.g.
71 * codec <--> BT codec or GSM modem -- lg FIXME */
Jarkko Nikula2e747962008-04-25 13:55:19 +020072 if (!dma_data)
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090073 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +020074
75 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
76 runtime->dma_bytes = params_buffer_bytes(params);
77
Peter Ujfalusi946cc362012-09-14 15:05:58 +030078 chan = snd_dmaengine_pcm_get_chan(substream);
79 if (!chan)
80 return -EINVAL;
81
82 /* fills in addr_width and direction */
83 err = snd_hwparams_to_dma_slave_config(substream, params, &config);
84 if (err)
85 return err;
86
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +020087 snd_dmaengine_pcm_set_config_from_dai_data(substream,
88 snd_soc_dai_get_dma_data(rtd->cpu_dai, substream),
89 &config);
Peter Ujfalusi946cc362012-09-14 15:05:58 +030090
91 return dmaengine_slave_config(chan, &config);
Jarkko Nikula2e747962008-04-25 13:55:19 +020092}
93
94static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
95{
Jarkko Nikula2e747962008-04-25 13:55:19 +020096 snd_pcm_set_runtime_buffer(substream, NULL);
Jarkko Nikula2e747962008-04-25 13:55:19 +020097 return 0;
98}
99
Jarkko Nikula2e747962008-04-25 13:55:19 +0200100static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
101{
Jarkko Nikula2e747962008-04-25 13:55:19 +0200102 snd_pcm_uframes_t offset;
103
Tony Lindgren27615a92012-10-15 16:24:23 -0700104 if (pcm_omap1510())
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300105 offset = snd_dmaengine_pcm_pointer_no_residue(substream);
106 else
107 offset = snd_dmaengine_pcm_pointer(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200108
109 return offset;
110}
111
112static int omap_pcm_open(struct snd_pcm_substream *substream)
113{
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300114 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +0200115 struct snd_dmaengine_dai_dma_data *dma_data;
Peter Ujfalusif6becf02013-07-11 14:35:43 +0200116 int ret;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200117
118 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
119
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300120 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Peter Ujfalusia92b5312013-02-05 13:43:39 +0100121
Peter Ujfalusif6becf02013-07-11 14:35:43 +0200122 /* DT boot: filter_data is the DMA name */
123 if (rtd->cpu_dai->dev->of_node) {
124 struct dma_chan *chan;
125
126 chan = dma_request_slave_channel(rtd->cpu_dai->dev,
127 dma_data->filter_data);
128 ret = snd_dmaengine_pcm_open(substream, chan);
129 } else {
130 ret = snd_dmaengine_pcm_open_request_chan(substream,
131 omap_dma_filter_fn,
132 dma_data->filter_data);
133 }
134 return ret;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200135}
136
Jarkko Nikula2e747962008-04-25 13:55:19 +0200137static int omap_pcm_mmap(struct snd_pcm_substream *substream,
138 struct vm_area_struct *vma)
139{
140 struct snd_pcm_runtime *runtime = substream->runtime;
141
142 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
143 runtime->dma_area,
144 runtime->dma_addr,
145 runtime->dma_bytes);
146}
147
Mark Brownb2a19d02009-01-17 19:14:26 +0000148static struct snd_pcm_ops omap_pcm_ops = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200149 .open = omap_pcm_open,
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200150 .close = snd_dmaengine_pcm_close_release_chan,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200151 .ioctl = snd_pcm_lib_ioctl,
152 .hw_params = omap_pcm_hw_params,
153 .hw_free = omap_pcm_hw_free,
Lars-Peter Clausenabe99372013-03-25 16:58:16 +0100154 .trigger = snd_dmaengine_pcm_trigger,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200155 .pointer = omap_pcm_pointer,
156 .mmap = omap_pcm_mmap,
157};
158
Eduardo Valentina152ff22009-08-20 16:18:22 +0300159static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200160
161static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
162 int stream)
163{
164 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
165 struct snd_dma_buffer *buf = &substream->dma_buffer;
166 size_t size = omap_pcm_hardware.buffer_bytes_max;
167
168 buf->dev.type = SNDRV_DMA_TYPE_DEV;
169 buf->dev.dev = pcm->card->dev;
170 buf->private_data = NULL;
171 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
172 &buf->addr, GFP_KERNEL);
173 if (!buf->area)
174 return -ENOMEM;
175
176 buf->bytes = size;
177 return 0;
178}
179
180static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
181{
182 struct snd_pcm_substream *substream;
183 struct snd_dma_buffer *buf;
184 int stream;
185
186 for (stream = 0; stream < 2; stream++) {
187 substream = pcm->streams[stream].substream;
188 if (!substream)
189 continue;
190
191 buf = &substream->dma_buffer;
192 if (!buf->area)
193 continue;
194
195 dma_free_writecombine(pcm->card->dev, buf->bytes,
196 buf->area, buf->addr);
197 buf->area = NULL;
198 }
199}
200
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100201static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200202{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100203 struct snd_card *card = rtd->card->snd_card;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100204 struct snd_pcm *pcm = rtd->pcm;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200205 int ret = 0;
206
207 if (!card->dev->dma_mask)
208 card->dev->dma_mask = &omap_pcm_dmamask;
209 if (!card->dev->coherent_dma_mask)
Eduardo Valentina152ff22009-08-20 16:18:22 +0300210 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200211
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100212 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200213 ret = omap_pcm_preallocate_dma_buffer(pcm,
214 SNDRV_PCM_STREAM_PLAYBACK);
215 if (ret)
216 goto out;
217 }
218
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100219 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200220 ret = omap_pcm_preallocate_dma_buffer(pcm,
221 SNDRV_PCM_STREAM_CAPTURE);
222 if (ret)
223 goto out;
224 }
225
226out:
Oleg Matcovschifad93652012-04-24 19:02:02 -0700227 /* free preallocated buffers in case of error */
228 if (ret)
229 omap_pcm_free_dma_buffers(pcm);
230
Jarkko Nikula2e747962008-04-25 13:55:19 +0200231 return ret;
232}
233
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000234static struct snd_soc_platform_driver omap_soc_platform = {
235 .ops = &omap_pcm_ops,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200236 .pcm_new = omap_pcm_new,
237 .pcm_free = omap_pcm_free_dma_buffers,
238};
Jarkko Nikula2e747962008-04-25 13:55:19 +0200239
Bill Pemberton7ff60002012-12-07 09:26:29 -0500240static int omap_pcm_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000241{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000242 return snd_soc_register_platform(&pdev->dev,
243 &omap_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000244}
Mark Brown958e7922008-12-03 19:58:17 +0000245
Bill Pemberton7ff60002012-12-07 09:26:29 -0500246static int omap_pcm_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000247{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000248 snd_soc_unregister_platform(&pdev->dev);
249 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000250}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000251
252static struct platform_driver omap_pcm_driver = {
253 .driver = {
254 .name = "omap-pcm-audio",
255 .owner = THIS_MODULE,
256 },
257
258 .probe = omap_pcm_probe,
Bill Pemberton7ff60002012-12-07 09:26:29 -0500259 .remove = omap_pcm_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000260};
261
Axel Linbeda5bf52011-11-25 10:12:16 +0800262module_platform_driver(omap_pcm_driver);
Mark Brown958e7922008-12-03 19:58:17 +0000263
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +0300264MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200265MODULE_DESCRIPTION("OMAP PCM DMA module");
266MODULE_LICENSE("GPL");
Guillaume Gardet5e70b7fc2012-07-12 15:08:16 +0200267MODULE_ALIAS("platform:omap-pcm-audio");