blob: 99381a27295bbbd9452f0d0050c1509651373bff [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>
Lars-Peter Clausen76a77f42014-06-19 09:40:29 +020034#include <sound/omap-pcm.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020035
Tony Lindgren27615a92012-10-15 16:24:23 -070036#ifdef CONFIG_ARCH_OMAP1
37#define pcm_omap1510() cpu_is_omap1510()
38#else
39#define pcm_omap1510() 0
40#endif
41
Jyri Sarha6742e152015-03-03 13:28:53 +020042static struct snd_pcm_hardware omap_pcm_hardware = {
Jarkko Nikula2e747962008-04-25 13:55:19 +020043 .info = SNDRV_PCM_INFO_MMAP |
44 SNDRV_PCM_INFO_MMAP_VALID |
45 SNDRV_PCM_INFO_INTERLEAVED |
46 SNDRV_PCM_INFO_PAUSE |
Peter Ujfalusib4173822011-05-12 13:04:55 +030047 SNDRV_PCM_INFO_RESUME |
48 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
Jarkko Nikula2e747962008-04-25 13:55:19 +020049 .period_bytes_min = 32,
50 .period_bytes_max = 64 * 1024,
51 .periods_min = 2,
52 .periods_max = 255,
53 .buffer_bytes_max = 128 * 1024,
54};
55
Jyri Sarha6742e152015-03-03 13:28:53 +020056/* sDMA supports only 1, 2, and 4 byte transfer elements. */
57static void omap_pcm_limit_supported_formats(void)
58{
59 int i;
60
61 for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
62 switch (snd_pcm_format_physical_width(i)) {
63 case 8:
64 case 16:
65 case 32:
66 omap_pcm_hardware.formats |= (1LL << i);
67 break;
68 default:
69 break;
70 }
71 }
72}
73
Jarkko Nikula2e747962008-04-25 13:55:19 +020074/* this may get called several times by oss emulation */
75static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
76 struct snd_pcm_hw_params *params)
77{
78 struct snd_pcm_runtime *runtime = substream->runtime;
79 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mack5f712b22010-03-22 10:11:15 +010080 struct omap_pcm_dma_data *dma_data;
Peter Ujfalusi946cc362012-09-14 15:05:58 +030081 struct dma_slave_config config;
82 struct dma_chan *chan;
Jarkko Nikula2e747962008-04-25 13:55:19 +020083 int err = 0;
84
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000085 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Daniel Mack5f712b22010-03-22 10:11:15 +010086
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090087 /* return if this is a bufferless transfer e.g.
88 * codec <--> BT codec or GSM modem -- lg FIXME */
Jarkko Nikula2e747962008-04-25 13:55:19 +020089 if (!dma_data)
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090090 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +020091
92 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
93 runtime->dma_bytes = params_buffer_bytes(params);
94
Peter Ujfalusi946cc362012-09-14 15:05:58 +030095 chan = snd_dmaengine_pcm_get_chan(substream);
96 if (!chan)
97 return -EINVAL;
98
99 /* fills in addr_width and direction */
100 err = snd_hwparams_to_dma_slave_config(substream, params, &config);
101 if (err)
102 return err;
103
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +0200104 snd_dmaengine_pcm_set_config_from_dai_data(substream,
105 snd_soc_dai_get_dma_data(rtd->cpu_dai, substream),
106 &config);
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300107
108 return dmaengine_slave_config(chan, &config);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200109}
110
111static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
112{
Jarkko Nikula2e747962008-04-25 13:55:19 +0200113 snd_pcm_set_runtime_buffer(substream, NULL);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200114 return 0;
115}
116
Jarkko Nikula2e747962008-04-25 13:55:19 +0200117static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
118{
Jarkko Nikula2e747962008-04-25 13:55:19 +0200119 snd_pcm_uframes_t offset;
120
Tony Lindgren27615a92012-10-15 16:24:23 -0700121 if (pcm_omap1510())
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300122 offset = snd_dmaengine_pcm_pointer_no_residue(substream);
123 else
124 offset = snd_dmaengine_pcm_pointer(substream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200125
126 return offset;
127}
128
129static int omap_pcm_open(struct snd_pcm_substream *substream)
130{
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300131 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +0200132 struct snd_dmaengine_dai_dma_data *dma_data;
Peter Ujfalusif6becf02013-07-11 14:35:43 +0200133 int ret;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200134
135 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
136
Peter Ujfalusi946cc362012-09-14 15:05:58 +0300137 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Peter Ujfalusia92b5312013-02-05 13:43:39 +0100138
Peter Ujfalusif6becf02013-07-11 14:35:43 +0200139 /* DT boot: filter_data is the DMA name */
140 if (rtd->cpu_dai->dev->of_node) {
141 struct dma_chan *chan;
142
143 chan = dma_request_slave_channel(rtd->cpu_dai->dev,
144 dma_data->filter_data);
145 ret = snd_dmaengine_pcm_open(substream, chan);
146 } else {
147 ret = snd_dmaengine_pcm_open_request_chan(substream,
148 omap_dma_filter_fn,
149 dma_data->filter_data);
150 }
151 return ret;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200152}
153
Jarkko Nikula2e747962008-04-25 13:55:19 +0200154static int omap_pcm_mmap(struct snd_pcm_substream *substream,
155 struct vm_area_struct *vma)
156{
157 struct snd_pcm_runtime *runtime = substream->runtime;
158
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800159 return dma_mmap_wc(substream->pcm->card->dev, vma, runtime->dma_area,
160 runtime->dma_addr, runtime->dma_bytes);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200161}
162
Mark Brownb2a19d02009-01-17 19:14:26 +0000163static struct snd_pcm_ops omap_pcm_ops = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200164 .open = omap_pcm_open,
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200165 .close = snd_dmaengine_pcm_close_release_chan,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200166 .ioctl = snd_pcm_lib_ioctl,
167 .hw_params = omap_pcm_hw_params,
168 .hw_free = omap_pcm_hw_free,
Lars-Peter Clausenabe99372013-03-25 16:58:16 +0100169 .trigger = snd_dmaengine_pcm_trigger,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200170 .pointer = omap_pcm_pointer,
171 .mmap = omap_pcm_mmap,
172};
173
Jarkko Nikula2e747962008-04-25 13:55:19 +0200174static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
175 int stream)
176{
177 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
178 struct snd_dma_buffer *buf = &substream->dma_buffer;
179 size_t size = omap_pcm_hardware.buffer_bytes_max;
180
181 buf->dev.type = SNDRV_DMA_TYPE_DEV;
182 buf->dev.dev = pcm->card->dev;
183 buf->private_data = NULL;
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800184 buf->area = dma_alloc_wc(pcm->card->dev, size, &buf->addr, GFP_KERNEL);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200185 if (!buf->area)
186 return -ENOMEM;
187
188 buf->bytes = size;
189 return 0;
190}
191
192static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
193{
194 struct snd_pcm_substream *substream;
195 struct snd_dma_buffer *buf;
196 int stream;
197
198 for (stream = 0; stream < 2; stream++) {
199 substream = pcm->streams[stream].substream;
200 if (!substream)
201 continue;
202
203 buf = &substream->dma_buffer;
204 if (!buf->area)
205 continue;
206
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800207 dma_free_wc(pcm->card->dev, buf->bytes, buf->area, buf->addr);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200208 buf->area = NULL;
209 }
210}
211
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100212static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200213{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100214 struct snd_card *card = rtd->card->snd_card;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100215 struct snd_pcm *pcm = rtd->pcm;
Russell Kingc9bd5e62013-06-27 12:53:37 +0100216 int ret;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200217
Peter Ujfalusid51199a2015-03-03 13:38:14 +0200218 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
Russell Kingc9bd5e62013-06-27 12:53:37 +0100219 if (ret)
220 return ret;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200221
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100222 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200223 ret = omap_pcm_preallocate_dma_buffer(pcm,
224 SNDRV_PCM_STREAM_PLAYBACK);
225 if (ret)
226 goto out;
227 }
228
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100229 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200230 ret = omap_pcm_preallocate_dma_buffer(pcm,
231 SNDRV_PCM_STREAM_CAPTURE);
232 if (ret)
233 goto out;
234 }
235
236out:
Oleg Matcovschifad93652012-04-24 19:02:02 -0700237 /* free preallocated buffers in case of error */
238 if (ret)
239 omap_pcm_free_dma_buffers(pcm);
240
Jarkko Nikula2e747962008-04-25 13:55:19 +0200241 return ret;
242}
243
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000244static struct snd_soc_platform_driver omap_soc_platform = {
245 .ops = &omap_pcm_ops,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200246 .pcm_new = omap_pcm_new,
247 .pcm_free = omap_pcm_free_dma_buffers,
248};
Jarkko Nikula2e747962008-04-25 13:55:19 +0200249
Peter Ujfalusife7b5862014-04-16 15:46:12 +0300250int omap_pcm_platform_register(struct device *dev)
251{
Jyri Sarha6742e152015-03-03 13:28:53 +0200252 omap_pcm_limit_supported_formats();
Peter Ujfalusife7b5862014-04-16 15:46:12 +0300253 return devm_snd_soc_register_platform(dev, &omap_soc_platform);
254}
255EXPORT_SYMBOL_GPL(omap_pcm_platform_register);
256
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +0300257MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200258MODULE_DESCRIPTION("OMAP PCM DMA module");
259MODULE_LICENSE("GPL");