]>
Commit | Line | Data |
---|---|---|
a6d77317 DB |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License version 2 as | |
4 | * published by the Free Software Foundation. | |
5 | */ | |
6 | ||
5a0e3ad6 | 7 | #include <linux/slab.h> |
a6d77317 DB |
8 | #include <linux/module.h> |
9 | #include <linux/dma-mapping.h> | |
d65a1458 | 10 | #include <linux/dmaengine.h> |
58ceb57e | 11 | #include <linux/dma/pxa-dma.h> |
a6d77317 DB |
12 | |
13 | #include <sound/core.h> | |
14 | #include <sound/pcm.h> | |
15 | #include <sound/pcm_params.h> | |
16 | #include <sound/pxa2xx-lib.h> | |
d65a1458 | 17 | #include <sound/dmaengine_pcm.h> |
a6d77317 | 18 | |
a6d77317 DB |
19 | static const struct snd_pcm_hardware pxa2xx_pcm_hardware = { |
20 | .info = SNDRV_PCM_INFO_MMAP | | |
21 | SNDRV_PCM_INFO_MMAP_VALID | | |
22 | SNDRV_PCM_INFO_INTERLEAVED | | |
23 | SNDRV_PCM_INFO_PAUSE | | |
24 | SNDRV_PCM_INFO_RESUME, | |
25 | .formats = SNDRV_PCM_FMTBIT_S16_LE | | |
26 | SNDRV_PCM_FMTBIT_S24_LE | | |
27 | SNDRV_PCM_FMTBIT_S32_LE, | |
28 | .period_bytes_min = 32, | |
29 | .period_bytes_max = 8192 - 32, | |
30 | .periods_min = 1, | |
58ceb57e | 31 | .periods_max = 256, |
a6d77317 DB |
32 | .buffer_bytes_max = 128 * 1024, |
33 | .fifo_size = 32, | |
34 | }; | |
35 | ||
a7160670 DM |
36 | int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream, |
37 | struct snd_pcm_hw_params *params) | |
a6d77317 | 38 | { |
58ceb57e DM |
39 | struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream); |
40 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
41 | struct snd_dmaengine_dai_dma_data *dma_params; | |
42 | struct dma_slave_config config; | |
43 | int ret; | |
44 | ||
45 | dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); | |
46 | if (!dma_params) | |
47 | return 0; | |
d65a1458 | 48 | |
58ceb57e DM |
49 | ret = snd_hwparams_to_dma_slave_config(substream, params, &config); |
50 | if (ret) | |
51 | return ret; | |
d65a1458 | 52 | |
58ceb57e DM |
53 | snd_dmaengine_pcm_set_config_from_dai_data(substream, |
54 | snd_soc_dai_get_dma_data(rtd->cpu_dai, substream), | |
55 | &config); | |
a6d77317 | 56 | |
58ceb57e DM |
57 | ret = dmaengine_slave_config(chan, &config); |
58 | if (ret) | |
59 | return ret; | |
a6d77317 | 60 | |
58ceb57e | 61 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
a6d77317 DB |
62 | |
63 | return 0; | |
64 | } | |
a7160670 | 65 | EXPORT_SYMBOL(pxa2xx_pcm_hw_params); |
a6d77317 | 66 | |
a7160670 | 67 | int pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream) |
a6d77317 | 68 | { |
a6d77317 DB |
69 | snd_pcm_set_runtime_buffer(substream, NULL); |
70 | return 0; | |
71 | } | |
a7160670 | 72 | EXPORT_SYMBOL(pxa2xx_pcm_hw_free); |
a6d77317 DB |
73 | |
74 | int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
75 | { | |
58ceb57e | 76 | return snd_dmaengine_pcm_trigger(substream, cmd); |
a6d77317 DB |
77 | } |
78 | EXPORT_SYMBOL(pxa2xx_pcm_trigger); | |
79 | ||
80 | snd_pcm_uframes_t | |
81 | pxa2xx_pcm_pointer(struct snd_pcm_substream *substream) | |
82 | { | |
58ceb57e | 83 | return snd_dmaengine_pcm_pointer(substream); |
a6d77317 DB |
84 | } |
85 | EXPORT_SYMBOL(pxa2xx_pcm_pointer); | |
86 | ||
a7160670 | 87 | int pxa2xx_pcm_prepare(struct snd_pcm_substream *substream) |
a6d77317 | 88 | { |
a6d77317 DB |
89 | return 0; |
90 | } | |
a7160670 | 91 | EXPORT_SYMBOL(pxa2xx_pcm_prepare); |
a6d77317 | 92 | |
a7160670 | 93 | int pxa2xx_pcm_open(struct snd_pcm_substream *substream) |
a6d77317 | 94 | { |
58ceb57e | 95 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
a6d77317 | 96 | struct snd_pcm_runtime *runtime = substream->runtime; |
58ceb57e | 97 | struct snd_dmaengine_dai_dma_data *dma_params; |
a6d77317 DB |
98 | int ret; |
99 | ||
100 | runtime->hw = pxa2xx_pcm_hardware; | |
101 | ||
58ceb57e DM |
102 | dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
103 | if (!dma_params) | |
104 | return 0; | |
105 | ||
a6d77317 DB |
106 | /* |
107 | * For mysterious reasons (and despite what the manual says) | |
108 | * playback samples are lost if the DMA count is not a multiple | |
109 | * of the DMA burst size. Let's add a rule to enforce that. | |
110 | */ | |
111 | ret = snd_pcm_hw_constraint_step(runtime, 0, | |
112 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32); | |
113 | if (ret) | |
58ceb57e | 114 | return ret; |
a6d77317 DB |
115 | |
116 | ret = snd_pcm_hw_constraint_step(runtime, 0, | |
117 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32); | |
118 | if (ret) | |
58ceb57e | 119 | return ret; |
a6d77317 DB |
120 | |
121 | ret = snd_pcm_hw_constraint_integer(runtime, | |
122 | SNDRV_PCM_HW_PARAM_PERIODS); | |
123 | if (ret < 0) | |
58ceb57e | 124 | return ret; |
a6d77317 | 125 | |
8f54061d RJ |
126 | return snd_dmaengine_pcm_open( |
127 | substream, dma_request_slave_channel(rtd->cpu_dai->dev, | |
128 | dma_params->chan_name)); | |
a6d77317 | 129 | } |
a7160670 | 130 | EXPORT_SYMBOL(pxa2xx_pcm_open); |
a6d77317 | 131 | |
a7160670 | 132 | int pxa2xx_pcm_close(struct snd_pcm_substream *substream) |
a6d77317 | 133 | { |
58ceb57e | 134 | return snd_dmaengine_pcm_close_release_chan(substream); |
a6d77317 | 135 | } |
a7160670 | 136 | EXPORT_SYMBOL(pxa2xx_pcm_close); |
a6d77317 DB |
137 | |
138 | int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream, | |
139 | struct vm_area_struct *vma) | |
140 | { | |
141 | struct snd_pcm_runtime *runtime = substream->runtime; | |
f6e45661 LR |
142 | return dma_mmap_wc(substream->pcm->card->dev, vma, runtime->dma_area, |
143 | runtime->dma_addr, runtime->dma_bytes); | |
a6d77317 DB |
144 | } |
145 | EXPORT_SYMBOL(pxa2xx_pcm_mmap); | |
146 | ||
147 | int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream) | |
148 | { | |
149 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; | |
150 | struct snd_dma_buffer *buf = &substream->dma_buffer; | |
151 | size_t size = pxa2xx_pcm_hardware.buffer_bytes_max; | |
152 | buf->dev.type = SNDRV_DMA_TYPE_DEV; | |
153 | buf->dev.dev = pcm->card->dev; | |
154 | buf->private_data = NULL; | |
f6e45661 | 155 | buf->area = dma_alloc_wc(pcm->card->dev, size, &buf->addr, GFP_KERNEL); |
a6d77317 DB |
156 | if (!buf->area) |
157 | return -ENOMEM; | |
158 | buf->bytes = size; | |
159 | return 0; | |
160 | } | |
161 | EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer); | |
162 | ||
163 | void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm) | |
164 | { | |
165 | struct snd_pcm_substream *substream; | |
166 | struct snd_dma_buffer *buf; | |
167 | int stream; | |
168 | ||
169 | for (stream = 0; stream < 2; stream++) { | |
170 | substream = pcm->streams[stream].substream; | |
171 | if (!substream) | |
172 | continue; | |
173 | buf = &substream->dma_buffer; | |
174 | if (!buf->area) | |
175 | continue; | |
f6e45661 | 176 | dma_free_wc(pcm->card->dev, buf->bytes, buf->area, buf->addr); |
a6d77317 DB |
177 | buf->area = NULL; |
178 | } | |
179 | } | |
180 | EXPORT_SYMBOL(pxa2xx_pcm_free_dma_buffers); | |
181 | ||
182 | MODULE_AUTHOR("Nicolas Pitre"); | |
183 | MODULE_DESCRIPTION("Intel PXA2xx sound library"); | |
184 | MODULE_LICENSE("GPL"); |