blob: 48803269037847ef7ad272fc07d61dc6d6590d0f [file] [log] [blame]
Ryan Mallondb5bf412010-06-04 17:11:24 +12001/*
2 * linux/sound/arm/ep93xx-pcm.c - EP93xx ALSA PCM interface
3 *
4 * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
5 * Copyright (C) 2006 Applied Data Systems
6 *
7 * Rewritten for the SoC audio subsystem (Based on PXA2xx code):
Ryan Mallon1c5454e2011-06-15 14:45:36 +10008 * Copyright (c) 2008 Ryan Mallon
Ryan Mallondb5bf412010-06-04 17:11:24 +12009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/device.h>
18#include <linux/slab.h>
Mika Westerberg51e2cc02011-05-29 13:10:04 +030019#include <linux/dmaengine.h>
Ryan Mallondb5bf412010-06-04 17:11:24 +120020#include <linux/dma-mapping.h>
21
22#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
Lars-Peter Clausend7a42e12012-03-05 14:02:15 +010026#include <sound/dmaengine_pcm.h>
Ryan Mallondb5bf412010-06-04 17:11:24 +120027
Arnd Bergmanna3b29242012-08-24 15:12:11 +020028#include <linux/platform_data/dma-ep93xx.h>
Ryan Mallondb5bf412010-06-04 17:11:24 +120029#include <mach/hardware.h>
30#include <mach/ep93xx-regs.h>
31
Ryan Mallondb5bf412010-06-04 17:11:24 +120032static const struct snd_pcm_hardware ep93xx_pcm_hardware = {
33 .info = (SNDRV_PCM_INFO_MMAP |
34 SNDRV_PCM_INFO_MMAP_VALID |
35 SNDRV_PCM_INFO_INTERLEAVED |
36 SNDRV_PCM_INFO_BLOCK_TRANSFER),
37
Alexander Sverdlin4cfeb692011-03-07 20:30:12 +030038 .rates = SNDRV_PCM_RATE_8000_192000,
Ryan Mallondb5bf412010-06-04 17:11:24 +120039 .rate_min = SNDRV_PCM_RATE_8000,
Alexander Sverdlin4cfeb692011-03-07 20:30:12 +030040 .rate_max = SNDRV_PCM_RATE_192000,
Ryan Mallondb5bf412010-06-04 17:11:24 +120041
42 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
43 SNDRV_PCM_FMTBIT_S24_LE |
44 SNDRV_PCM_FMTBIT_S32_LE),
45
46 .buffer_bytes_max = 131072,
47 .period_bytes_min = 32,
48 .period_bytes_max = 32768,
49 .periods_min = 1,
50 .periods_max = 32,
51 .fifo_size = 32,
52};
53
Mika Westerberg51e2cc02011-05-29 13:10:04 +030054static bool ep93xx_pcm_dma_filter(struct dma_chan *chan, void *filter_param)
Ryan Mallondb5bf412010-06-04 17:11:24 +120055{
Mika Westerberg51e2cc02011-05-29 13:10:04 +030056 struct ep93xx_dma_data *data = filter_param;
Ryan Mallondb5bf412010-06-04 17:11:24 +120057
Mika Westerberg51e2cc02011-05-29 13:10:04 +030058 if (data->direction == ep93xx_dma_chan_direction(chan)) {
59 chan->private = data;
60 return true;
Ryan Mallondb5bf412010-06-04 17:11:24 +120061 }
Mika Westerberg51e2cc02011-05-29 13:10:04 +030062
63 return false;
Ryan Mallondb5bf412010-06-04 17:11:24 +120064}
65
66static int ep93xx_pcm_open(struct snd_pcm_substream *substream)
67{
Lars-Peter Clausend7a42e12012-03-05 14:02:15 +010068 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Ryan Mallondb5bf412010-06-04 17:11:24 +120069
Ryan Mallondb5bf412010-06-04 17:11:24 +120070 snd_soc_set_runtime_hwparams(substream, &ep93xx_pcm_hardware);
71
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +020072 return snd_dmaengine_pcm_open_request_chan(substream,
73 ep93xx_pcm_dma_filter,
Lars-Peter Clausen453807f2013-03-22 14:12:10 +010074 snd_soc_dai_get_dma_data(rtd->cpu_dai, substream));
Ryan Mallondb5bf412010-06-04 17:11:24 +120075}
76
77static int ep93xx_pcm_hw_params(struct snd_pcm_substream *substream,
78 struct snd_pcm_hw_params *params)
79{
Ryan Mallondb5bf412010-06-04 17:11:24 +120080 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
Ryan Mallondb5bf412010-06-04 17:11:24 +120081
Ryan Mallondb5bf412010-06-04 17:11:24 +120082 return 0;
83}
84
85static int ep93xx_pcm_hw_free(struct snd_pcm_substream *substream)
86{
87 snd_pcm_set_runtime_buffer(substream, NULL);
88 return 0;
89}
90
Ryan Mallondb5bf412010-06-04 17:11:24 +120091static int ep93xx_pcm_mmap(struct snd_pcm_substream *substream,
92 struct vm_area_struct *vma)
93{
94 struct snd_pcm_runtime *runtime = substream->runtime;
95
96 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
97 runtime->dma_area,
98 runtime->dma_addr,
99 runtime->dma_bytes);
100}
101
102static struct snd_pcm_ops ep93xx_pcm_ops = {
103 .open = ep93xx_pcm_open,
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200104 .close = snd_dmaengine_pcm_close_release_chan,
Ryan Mallondb5bf412010-06-04 17:11:24 +1200105 .ioctl = snd_pcm_lib_ioctl,
106 .hw_params = ep93xx_pcm_hw_params,
107 .hw_free = ep93xx_pcm_hw_free,
Lars-Peter Clausend7a42e12012-03-05 14:02:15 +0100108 .trigger = snd_dmaengine_pcm_trigger,
Lars-Peter Clausen9883ab22012-06-11 20:11:41 +0200109 .pointer = snd_dmaengine_pcm_pointer_no_residue,
Ryan Mallondb5bf412010-06-04 17:11:24 +1200110 .mmap = ep93xx_pcm_mmap,
111};
112
113static int ep93xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
114{
115 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
116 struct snd_dma_buffer *buf = &substream->dma_buffer;
117 size_t size = ep93xx_pcm_hardware.buffer_bytes_max;
118
119 buf->dev.type = SNDRV_DMA_TYPE_DEV;
120 buf->dev.dev = pcm->card->dev;
121 buf->private_data = NULL;
122 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
123 &buf->addr, GFP_KERNEL);
124 buf->bytes = size;
125
126 return (buf->area == NULL) ? -ENOMEM : 0;
127}
128
129static void ep93xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
130{
131 struct snd_pcm_substream *substream;
132 struct snd_dma_buffer *buf;
133 int stream;
134
135 for (stream = 0; stream < 2; stream++) {
136 substream = pcm->streams[stream].substream;
137 if (!substream)
138 continue;
139
140 buf = &substream->dma_buffer;
141 if (!buf->area)
142 continue;
143
144 dma_free_writecombine(pcm->card->dev, buf->bytes, buf->area,
145 buf->addr);
146 buf->area = NULL;
147 }
148}
149
Joachim Eastwood350e16d2012-01-01 02:43:03 +0100150static u64 ep93xx_pcm_dmamask = DMA_BIT_MASK(32);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200151
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100152static int ep93xx_pcm_new(struct snd_soc_pcm_runtime *rtd)
Ryan Mallondb5bf412010-06-04 17:11:24 +1200153{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100154 struct snd_card *card = rtd->card->snd_card;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100155 struct snd_pcm *pcm = rtd->pcm;
Ryan Mallondb5bf412010-06-04 17:11:24 +1200156 int ret = 0;
157
158 if (!card->dev->dma_mask)
159 card->dev->dma_mask = &ep93xx_pcm_dmamask;
160 if (!card->dev->coherent_dma_mask)
Joachim Eastwood350e16d2012-01-01 02:43:03 +0100161 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200162
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100163 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
Ryan Mallondb5bf412010-06-04 17:11:24 +1200164 ret = ep93xx_pcm_preallocate_dma_buffer(pcm,
165 SNDRV_PCM_STREAM_PLAYBACK);
166 if (ret)
167 return ret;
168 }
169
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100170 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
Ryan Mallondb5bf412010-06-04 17:11:24 +1200171 ret = ep93xx_pcm_preallocate_dma_buffer(pcm,
172 SNDRV_PCM_STREAM_CAPTURE);
173 if (ret)
174 return ret;
175 }
176
177 return 0;
178}
179
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000180static struct snd_soc_platform_driver ep93xx_soc_platform = {
181 .ops = &ep93xx_pcm_ops,
Ryan Mallondb5bf412010-06-04 17:11:24 +1200182 .pcm_new = &ep93xx_pcm_new,
183 .pcm_free = &ep93xx_pcm_free_dma_buffers,
184};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000185
Bill Pemberton145e2872012-12-07 09:26:23 -0500186static int ep93xx_soc_platform_probe(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000187{
188 return snd_soc_register_platform(&pdev->dev, &ep93xx_soc_platform);
189}
190
Bill Pemberton145e2872012-12-07 09:26:23 -0500191static int ep93xx_soc_platform_remove(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000192{
193 snd_soc_unregister_platform(&pdev->dev);
194 return 0;
195}
196
197static struct platform_driver ep93xx_pcm_driver = {
198 .driver = {
199 .name = "ep93xx-pcm-audio",
200 .owner = THIS_MODULE,
201 },
202
203 .probe = ep93xx_soc_platform_probe,
Bill Pemberton145e2872012-12-07 09:26:23 -0500204 .remove = ep93xx_soc_platform_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000205};
Ryan Mallondb5bf412010-06-04 17:11:24 +1200206
Axel Linee18f632011-11-24 12:07:55 +0800207module_platform_driver(ep93xx_pcm_driver);
Ryan Mallondb5bf412010-06-04 17:11:24 +1200208
Ryan Mallon1c5454e2011-06-15 14:45:36 +1000209MODULE_AUTHOR("Ryan Mallon");
Ryan Mallondb5bf412010-06-04 17:11:24 +1200210MODULE_DESCRIPTION("EP93xx ALSA PCM interface");
211MODULE_LICENSE("GPL");
Mika Westerberg93068162011-09-11 12:28:49 +0300212MODULE_ALIAS("platform:ep93xx-pcm-audio");