blob: 564b5b60319d7af26de86810515f7e9a276e93c0 [file] [log] [blame]
Dong Aishenged6e1d02011-07-21 12:36:55 +08001/*
2 * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * Based on sound/soc/imx/imx-pcm-dma-mx2.c
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include <linux/clk.h>
22#include <linux/delay.h>
23#include <linux/device.h>
24#include <linux/dma-mapping.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/module.h>
28#include <linux/platform_device.h>
29#include <linux/slab.h>
30#include <linux/dmaengine.h>
Huang Shijie39468602012-02-16 14:17:32 +080031#include <linux/fsl/mxs-dma.h>
Dong Aishenged6e1d02011-07-21 12:36:55 +080032
33#include <sound/core.h>
34#include <sound/initval.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/soc.h>
Lars-Peter Clausen016ab462012-02-22 10:49:10 +010038#include <sound/dmaengine_pcm.h>
Dong Aishenged6e1d02011-07-21 12:36:55 +080039
Dong Aishenged6e1d02011-07-21 12:36:55 +080040#include "mxs-pcm.h"
41
Lars-Peter Clausen016ab462012-02-22 10:49:10 +010042struct mxs_pcm_dma_data {
43 struct mxs_dma_data dma_data;
44 struct mxs_pcm_dma_params *dma_params;
45};
46
Dong Aishenged6e1d02011-07-21 12:36:55 +080047static struct snd_pcm_hardware snd_mxs_hardware = {
48 .info = SNDRV_PCM_INFO_MMAP |
49 SNDRV_PCM_INFO_MMAP_VALID |
50 SNDRV_PCM_INFO_PAUSE |
51 SNDRV_PCM_INFO_RESUME |
52 SNDRV_PCM_INFO_INTERLEAVED,
53 .formats = SNDRV_PCM_FMTBIT_S16_LE |
54 SNDRV_PCM_FMTBIT_S20_3LE |
55 SNDRV_PCM_FMTBIT_S24_LE,
56 .channels_min = 2,
57 .channels_max = 2,
58 .period_bytes_min = 32,
59 .period_bytes_max = 8192,
60 .periods_min = 1,
61 .periods_max = 52,
62 .buffer_bytes_max = 64 * 1024,
63 .fifo_size = 32,
64
65};
66
Dong Aishenged6e1d02011-07-21 12:36:55 +080067static bool filter(struct dma_chan *chan, void *param)
68{
Lars-Peter Clausen016ab462012-02-22 10:49:10 +010069 struct mxs_pcm_dma_data *pcm_dma_data = param;
70 struct mxs_pcm_dma_params *dma_params = pcm_dma_data->dma_params;
Dong Aishenged6e1d02011-07-21 12:36:55 +080071
72 if (!mxs_dma_is_apbx(chan))
73 return false;
74
75 if (chan->chan_id != dma_params->chan_num)
76 return false;
77
Lars-Peter Clausen016ab462012-02-22 10:49:10 +010078 chan->private = &pcm_dma_data->dma_data;
Dong Aishenged6e1d02011-07-21 12:36:55 +080079
80 return true;
81}
82
Dong Aishenged6e1d02011-07-21 12:36:55 +080083static int snd_mxs_pcm_hw_params(struct snd_pcm_substream *substream,
84 struct snd_pcm_hw_params *params)
85{
Dong Aishenged6e1d02011-07-21 12:36:55 +080086 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
87
Dong Aishenged6e1d02011-07-21 12:36:55 +080088 return 0;
89}
90
Dong Aishenged6e1d02011-07-21 12:36:55 +080091static int snd_mxs_open(struct snd_pcm_substream *substream)
92{
Lars-Peter Clausen016ab462012-02-22 10:49:10 +010093 struct snd_soc_pcm_runtime *rtd = substream->private_data;
94 struct mxs_pcm_dma_data *pcm_dma_data;
Dong Aishenged6e1d02011-07-21 12:36:55 +080095 int ret;
96
Lars-Peter Clausen016ab462012-02-22 10:49:10 +010097 pcm_dma_data = kzalloc(sizeof(*pcm_dma_data), GFP_KERNEL);
98 if (pcm_dma_data == NULL)
Dong Aishenged6e1d02011-07-21 12:36:55 +080099 return -ENOMEM;
Dong Aishenged6e1d02011-07-21 12:36:55 +0800100
Lars-Peter Clausen016ab462012-02-22 10:49:10 +0100101 pcm_dma_data->dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
102 pcm_dma_data->dma_data.chan_irq = pcm_dma_data->dma_params->chan_irq;
Dong Aishenged6e1d02011-07-21 12:36:55 +0800103
Lars-Peter Clausen016ab462012-02-22 10:49:10 +0100104 ret = snd_dmaengine_pcm_open(substream, filter, pcm_dma_data);
Lars-Peter Clausen95a771c2012-02-22 10:49:07 +0100105 if (ret) {
Lars-Peter Clausen016ab462012-02-22 10:49:10 +0100106 kfree(pcm_dma_data);
Lars-Peter Clausen95a771c2012-02-22 10:49:07 +0100107 return ret;
108 }
109
Dong Aishenged6e1d02011-07-21 12:36:55 +0800110 snd_soc_set_runtime_hwparams(substream, &snd_mxs_hardware);
111
Lars-Peter Clausen016ab462012-02-22 10:49:10 +0100112 snd_dmaengine_pcm_set_data(substream, pcm_dma_data);
113
Dong Aishenged6e1d02011-07-21 12:36:55 +0800114 return 0;
115}
116
117static int snd_mxs_close(struct snd_pcm_substream *substream)
118{
Lars-Peter Clausen016ab462012-02-22 10:49:10 +0100119 struct mxs_pcm_dma_data *pcm_dma_data = snd_dmaengine_pcm_get_data(substream);
Dong Aishenged6e1d02011-07-21 12:36:55 +0800120
Lars-Peter Clausen016ab462012-02-22 10:49:10 +0100121 snd_dmaengine_pcm_close(substream);
122 kfree(pcm_dma_data);
Dong Aishenged6e1d02011-07-21 12:36:55 +0800123
124 return 0;
125}
126
127static int snd_mxs_pcm_mmap(struct snd_pcm_substream *substream,
128 struct vm_area_struct *vma)
129{
130 struct snd_pcm_runtime *runtime = substream->runtime;
131
132 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
133 runtime->dma_area,
134 runtime->dma_addr,
135 runtime->dma_bytes);
136}
137
138static struct snd_pcm_ops mxs_pcm_ops = {
139 .open = snd_mxs_open,
140 .close = snd_mxs_close,
141 .ioctl = snd_pcm_lib_ioctl,
142 .hw_params = snd_mxs_pcm_hw_params,
Lars-Peter Clausen016ab462012-02-22 10:49:10 +0100143 .trigger = snd_dmaengine_pcm_trigger,
Lars-Peter Clausen9883ab22012-06-11 20:11:41 +0200144 .pointer = snd_dmaengine_pcm_pointer_no_residue,
Dong Aishenged6e1d02011-07-21 12:36:55 +0800145 .mmap = snd_mxs_pcm_mmap,
146};
147
148static int mxs_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
149{
150 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
151 struct snd_dma_buffer *buf = &substream->dma_buffer;
152 size_t size = snd_mxs_hardware.buffer_bytes_max;
153
154 buf->dev.type = SNDRV_DMA_TYPE_DEV;
155 buf->dev.dev = pcm->card->dev;
156 buf->private_data = NULL;
157 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
158 &buf->addr, GFP_KERNEL);
159 if (!buf->area)
160 return -ENOMEM;
161 buf->bytes = size;
162
163 return 0;
164}
165
166static u64 mxs_pcm_dmamask = DMA_BIT_MASK(32);
167static int mxs_pcm_new(struct snd_soc_pcm_runtime *rtd)
168{
169 struct snd_card *card = rtd->card->snd_card;
170 struct snd_pcm *pcm = rtd->pcm;
171 int ret = 0;
172
173 if (!card->dev->dma_mask)
174 card->dev->dma_mask = &mxs_pcm_dmamask;
175 if (!card->dev->coherent_dma_mask)
176 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
177
178 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
179 ret = mxs_pcm_preallocate_dma_buffer(pcm,
180 SNDRV_PCM_STREAM_PLAYBACK);
181 if (ret)
182 goto out;
183 }
184
185 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
186 ret = mxs_pcm_preallocate_dma_buffer(pcm,
187 SNDRV_PCM_STREAM_CAPTURE);
188 if (ret)
189 goto out;
190 }
191
192out:
193 return ret;
194}
195
196static void mxs_pcm_free(struct snd_pcm *pcm)
197{
198 struct snd_pcm_substream *substream;
199 struct snd_dma_buffer *buf;
200 int stream;
201
202 for (stream = 0; stream < 2; stream++) {
203 substream = pcm->streams[stream].substream;
204 if (!substream)
205 continue;
206
207 buf = &substream->dma_buffer;
208 if (!buf->area)
209 continue;
210
211 dma_free_writecombine(pcm->card->dev, buf->bytes,
212 buf->area, buf->addr);
213 buf->area = NULL;
214 }
215}
216
217static struct snd_soc_platform_driver mxs_soc_platform = {
218 .ops = &mxs_pcm_ops,
219 .pcm_new = mxs_pcm_new,
220 .pcm_free = mxs_pcm_free,
221};
222
Bill Pembertonfd582732012-12-07 09:26:27 -0500223int mxs_pcm_platform_register(struct device *dev)
Dong Aishenged6e1d02011-07-21 12:36:55 +0800224{
Shawn Guo4da3fe72012-05-11 22:24:16 +0800225 return snd_soc_register_platform(dev, &mxs_soc_platform);
Dong Aishenged6e1d02011-07-21 12:36:55 +0800226}
Shawn Guo4da3fe72012-05-11 22:24:16 +0800227EXPORT_SYMBOL_GPL(mxs_pcm_platform_register);
Dong Aishenged6e1d02011-07-21 12:36:55 +0800228
Bill Pembertonfd582732012-12-07 09:26:27 -0500229void mxs_pcm_platform_unregister(struct device *dev)
Dong Aishenged6e1d02011-07-21 12:36:55 +0800230{
Shawn Guo4da3fe72012-05-11 22:24:16 +0800231 snd_soc_unregister_platform(dev);
Dong Aishenged6e1d02011-07-21 12:36:55 +0800232}
Shawn Guo4da3fe72012-05-11 22:24:16 +0800233EXPORT_SYMBOL_GPL(mxs_pcm_platform_unregister);
Lothar Waßmann06c8eb92011-12-09 14:38:11 +0100234
235MODULE_LICENSE("GPL");