Commit | Line | Data |
---|---|---|
ed6e1d04 DA |
1 | /* |
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> | |
31 | ||
32 | #include <sound/core.h> | |
33 | #include <sound/initval.h> | |
34 | #include <sound/pcm.h> | |
35 | #include <sound/pcm_params.h> | |
36 | #include <sound/soc.h> | |
016ab467 | 37 | #include <sound/dmaengine_pcm.h> |
ed6e1d04 | 38 | |
ed6e1d04 DA |
39 | #include "mxs-pcm.h" |
40 | ||
41 | static struct snd_pcm_hardware snd_mxs_hardware = { | |
42 | .info = SNDRV_PCM_INFO_MMAP | | |
43 | SNDRV_PCM_INFO_MMAP_VALID | | |
44 | SNDRV_PCM_INFO_PAUSE | | |
45 | SNDRV_PCM_INFO_RESUME | | |
57364f9a LPC |
46 | SNDRV_PCM_INFO_INTERLEAVED | |
47 | SNDRV_PCM_INFO_HALF_DUPLEX, | |
ed6e1d04 DA |
48 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
49 | SNDRV_PCM_FMTBIT_S20_3LE | | |
50 | SNDRV_PCM_FMTBIT_S24_LE, | |
51 | .channels_min = 2, | |
52 | .channels_max = 2, | |
53 | .period_bytes_min = 32, | |
54 | .period_bytes_max = 8192, | |
55 | .periods_min = 1, | |
56 | .periods_max = 52, | |
57 | .buffer_bytes_max = 64 * 1024, | |
58 | .fifo_size = 32, | |
59 | ||
60 | }; | |
61 | ||
ed6e1d04 DA |
62 | static bool filter(struct dma_chan *chan, void *param) |
63 | { | |
b7e5e912 | 64 | struct mxs_pcm_dma_params *dma_params = param; |
ed6e1d04 DA |
65 | |
66 | if (!mxs_dma_is_apbx(chan)) | |
67 | return false; | |
68 | ||
69 | if (chan->chan_id != dma_params->chan_num) | |
70 | return false; | |
71 | ||
b7e5e912 | 72 | chan->private = &dma_params->dma_data; |
ed6e1d04 DA |
73 | |
74 | return true; | |
75 | } | |
76 | ||
ed6e1d04 DA |
77 | static int snd_mxs_pcm_hw_params(struct snd_pcm_substream *substream, |
78 | struct snd_pcm_hw_params *params) | |
79 | { | |
ed6e1d04 DA |
80 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
81 | ||
ed6e1d04 DA |
82 | return 0; |
83 | } | |
84 | ||
ed6e1d04 DA |
85 | static int snd_mxs_open(struct snd_pcm_substream *substream) |
86 | { | |
016ab467 | 87 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
95a771ca | 88 | |
ed6e1d04 DA |
89 | snd_soc_set_runtime_hwparams(substream, &snd_mxs_hardware); |
90 | ||
7c1c1d4a | 91 | return snd_dmaengine_pcm_open_request_chan(substream, filter, |
b7e5e912 | 92 | snd_soc_dai_get_dma_data(rtd->cpu_dai, substream)); |
ed6e1d04 DA |
93 | } |
94 | ||
95 | static int snd_mxs_pcm_mmap(struct snd_pcm_substream *substream, | |
96 | struct vm_area_struct *vma) | |
97 | { | |
98 | struct snd_pcm_runtime *runtime = substream->runtime; | |
99 | ||
100 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, | |
101 | runtime->dma_area, | |
102 | runtime->dma_addr, | |
103 | runtime->dma_bytes); | |
104 | } | |
105 | ||
106 | static struct snd_pcm_ops mxs_pcm_ops = { | |
107 | .open = snd_mxs_open, | |
7c1c1d4a | 108 | .close = snd_dmaengine_pcm_close_release_chan, |
ed6e1d04 DA |
109 | .ioctl = snd_pcm_lib_ioctl, |
110 | .hw_params = snd_mxs_pcm_hw_params, | |
016ab467 | 111 | .trigger = snd_dmaengine_pcm_trigger, |
9883ab22 | 112 | .pointer = snd_dmaengine_pcm_pointer_no_residue, |
ed6e1d04 DA |
113 | .mmap = snd_mxs_pcm_mmap, |
114 | }; | |
115 | ||
116 | static int mxs_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream) | |
117 | { | |
118 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; | |
119 | struct snd_dma_buffer *buf = &substream->dma_buffer; | |
120 | size_t size = snd_mxs_hardware.buffer_bytes_max; | |
121 | ||
122 | buf->dev.type = SNDRV_DMA_TYPE_DEV; | |
123 | buf->dev.dev = pcm->card->dev; | |
124 | buf->private_data = NULL; | |
125 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, | |
126 | &buf->addr, GFP_KERNEL); | |
127 | if (!buf->area) | |
128 | return -ENOMEM; | |
129 | buf->bytes = size; | |
130 | ||
131 | return 0; | |
132 | } | |
133 | ||
134 | static u64 mxs_pcm_dmamask = DMA_BIT_MASK(32); | |
135 | static int mxs_pcm_new(struct snd_soc_pcm_runtime *rtd) | |
136 | { | |
137 | struct snd_card *card = rtd->card->snd_card; | |
138 | struct snd_pcm *pcm = rtd->pcm; | |
139 | int ret = 0; | |
140 | ||
141 | if (!card->dev->dma_mask) | |
142 | card->dev->dma_mask = &mxs_pcm_dmamask; | |
143 | if (!card->dev->coherent_dma_mask) | |
144 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); | |
145 | ||
146 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { | |
147 | ret = mxs_pcm_preallocate_dma_buffer(pcm, | |
148 | SNDRV_PCM_STREAM_PLAYBACK); | |
149 | if (ret) | |
150 | goto out; | |
151 | } | |
152 | ||
153 | if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { | |
154 | ret = mxs_pcm_preallocate_dma_buffer(pcm, | |
155 | SNDRV_PCM_STREAM_CAPTURE); | |
156 | if (ret) | |
157 | goto out; | |
158 | } | |
159 | ||
160 | out: | |
161 | return ret; | |
162 | } | |
163 | ||
164 | static void mxs_pcm_free(struct snd_pcm *pcm) | |
165 | { | |
166 | struct snd_pcm_substream *substream; | |
167 | struct snd_dma_buffer *buf; | |
168 | int stream; | |
169 | ||
170 | for (stream = 0; stream < 2; stream++) { | |
171 | substream = pcm->streams[stream].substream; | |
172 | if (!substream) | |
173 | continue; | |
174 | ||
175 | buf = &substream->dma_buffer; | |
176 | if (!buf->area) | |
177 | continue; | |
178 | ||
179 | dma_free_writecombine(pcm->card->dev, buf->bytes, | |
180 | buf->area, buf->addr); | |
181 | buf->area = NULL; | |
182 | } | |
183 | } | |
184 | ||
185 | static struct snd_soc_platform_driver mxs_soc_platform = { | |
186 | .ops = &mxs_pcm_ops, | |
187 | .pcm_new = mxs_pcm_new, | |
188 | .pcm_free = mxs_pcm_free, | |
189 | }; | |
190 | ||
fd582736 | 191 | int mxs_pcm_platform_register(struct device *dev) |
ed6e1d04 | 192 | { |
4da3fe78 | 193 | return snd_soc_register_platform(dev, &mxs_soc_platform); |
ed6e1d04 | 194 | } |
4da3fe78 | 195 | EXPORT_SYMBOL_GPL(mxs_pcm_platform_register); |
ed6e1d04 | 196 | |
fd582736 | 197 | void mxs_pcm_platform_unregister(struct device *dev) |
ed6e1d04 | 198 | { |
4da3fe78 | 199 | snd_soc_unregister_platform(dev); |
ed6e1d04 | 200 | } |
4da3fe78 | 201 | EXPORT_SYMBOL_GPL(mxs_pcm_platform_unregister); |
06c8eb9a LW |
202 | |
203 | MODULE_LICENSE("GPL"); |