Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
a6d77317 | 2 | |
5a0e3ad6 | 3 | #include <linux/slab.h> |
a6d77317 DB |
4 | #include <linux/module.h> |
5 | #include <linux/dma-mapping.h> | |
d65a1458 | 6 | #include <linux/dmaengine.h> |
58ceb57e | 7 | #include <linux/dma/pxa-dma.h> |
a6d77317 DB |
8 | |
9 | #include <sound/core.h> | |
10 | #include <sound/pcm.h> | |
11 | #include <sound/pcm_params.h> | |
12 | #include <sound/pxa2xx-lib.h> | |
d65a1458 | 13 | #include <sound/dmaengine_pcm.h> |
a6d77317 | 14 | |
a6d77317 DB |
15 | static const struct snd_pcm_hardware pxa2xx_pcm_hardware = { |
16 | .info = SNDRV_PCM_INFO_MMAP | | |
17 | SNDRV_PCM_INFO_MMAP_VALID | | |
18 | SNDRV_PCM_INFO_INTERLEAVED | | |
19 | SNDRV_PCM_INFO_PAUSE | | |
20 | SNDRV_PCM_INFO_RESUME, | |
21 | .formats = SNDRV_PCM_FMTBIT_S16_LE | | |
456ec808 DM |
22 | SNDRV_PCM_FMTBIT_S24_LE | |
23 | SNDRV_PCM_FMTBIT_S32_LE, | |
a6d77317 DB |
24 | .period_bytes_min = 32, |
25 | .period_bytes_max = 8192 - 32, | |
26 | .periods_min = 1, | |
58ceb57e | 27 | .periods_max = 256, |
a6d77317 DB |
28 | .buffer_bytes_max = 128 * 1024, |
29 | .fifo_size = 32, | |
30 | }; | |
31 | ||
a7160670 DM |
32 | int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream, |
33 | struct snd_pcm_hw_params *params) | |
a6d77317 | 34 | { |
58ceb57e DM |
35 | struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream); |
36 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
37 | struct snd_dmaengine_dai_dma_data *dma_params; | |
38 | struct dma_slave_config config; | |
39 | int ret; | |
40 | ||
575be883 | 41 | dma_params = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream); |
58ceb57e DM |
42 | if (!dma_params) |
43 | return 0; | |
d65a1458 | 44 | |
58ceb57e DM |
45 | ret = snd_hwparams_to_dma_slave_config(substream, params, &config); |
46 | if (ret) | |
47 | return ret; | |
d65a1458 | 48 | |
58ceb57e | 49 | snd_dmaengine_pcm_set_config_from_dai_data(substream, |
575be883 | 50 | snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream), |
58ceb57e | 51 | &config); |
a6d77317 | 52 | |
58ceb57e DM |
53 | ret = dmaengine_slave_config(chan, &config); |
54 | if (ret) | |
55 | return ret; | |
a6d77317 | 56 | |
a6d77317 DB |
57 | return 0; |
58 | } | |
a7160670 | 59 | EXPORT_SYMBOL(pxa2xx_pcm_hw_params); |
a6d77317 | 60 | |
a6d77317 DB |
61 | int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
62 | { | |
58ceb57e | 63 | return snd_dmaengine_pcm_trigger(substream, cmd); |
a6d77317 DB |
64 | } |
65 | EXPORT_SYMBOL(pxa2xx_pcm_trigger); | |
66 | ||
67 | snd_pcm_uframes_t | |
68 | pxa2xx_pcm_pointer(struct snd_pcm_substream *substream) | |
69 | { | |
58ceb57e | 70 | return snd_dmaengine_pcm_pointer(substream); |
a6d77317 DB |
71 | } |
72 | EXPORT_SYMBOL(pxa2xx_pcm_pointer); | |
73 | ||
a7160670 | 74 | int pxa2xx_pcm_prepare(struct snd_pcm_substream *substream) |
a6d77317 | 75 | { |
a6d77317 DB |
76 | return 0; |
77 | } | |
a7160670 | 78 | EXPORT_SYMBOL(pxa2xx_pcm_prepare); |
a6d77317 | 79 | |
a7160670 | 80 | int pxa2xx_pcm_open(struct snd_pcm_substream *substream) |
a6d77317 | 81 | { |
58ceb57e | 82 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
a6d77317 | 83 | struct snd_pcm_runtime *runtime = substream->runtime; |
58ceb57e | 84 | struct snd_dmaengine_dai_dma_data *dma_params; |
a6d77317 DB |
85 | int ret; |
86 | ||
87 | runtime->hw = pxa2xx_pcm_hardware; | |
88 | ||
575be883 | 89 | dma_params = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream); |
58ceb57e DM |
90 | if (!dma_params) |
91 | return 0; | |
92 | ||
a6d77317 DB |
93 | /* |
94 | * For mysterious reasons (and despite what the manual says) | |
95 | * playback samples are lost if the DMA count is not a multiple | |
96 | * of the DMA burst size. Let's add a rule to enforce that. | |
97 | */ | |
98 | ret = snd_pcm_hw_constraint_step(runtime, 0, | |
99 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32); | |
100 | if (ret) | |
58ceb57e | 101 | return ret; |
a6d77317 DB |
102 | |
103 | ret = snd_pcm_hw_constraint_step(runtime, 0, | |
104 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32); | |
105 | if (ret) | |
58ceb57e | 106 | return ret; |
a6d77317 DB |
107 | |
108 | ret = snd_pcm_hw_constraint_integer(runtime, | |
109 | SNDRV_PCM_HW_PARAM_PERIODS); | |
110 | if (ret < 0) | |
58ceb57e | 111 | return ret; |
a6d77317 | 112 | |
8f54061d | 113 | return snd_dmaengine_pcm_open( |
575be883 | 114 | substream, dma_request_slave_channel(asoc_rtd_to_cpu(rtd, 0)->dev, |
8f54061d | 115 | dma_params->chan_name)); |
a6d77317 | 116 | } |
a7160670 | 117 | EXPORT_SYMBOL(pxa2xx_pcm_open); |
a6d77317 | 118 | |
a7160670 | 119 | int pxa2xx_pcm_close(struct snd_pcm_substream *substream) |
a6d77317 | 120 | { |
58ceb57e | 121 | return snd_dmaengine_pcm_close_release_chan(substream); |
a6d77317 | 122 | } |
a7160670 | 123 | EXPORT_SYMBOL(pxa2xx_pcm_close); |
a6d77317 | 124 | |
7f2da3d7 | 125 | int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm) |
a6d77317 | 126 | { |
a6d77317 | 127 | size_t size = pxa2xx_pcm_hardware.buffer_bytes_max; |
a6d77317 | 128 | |
7f2da3d7 TI |
129 | return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_WC, |
130 | pcm->card->dev, size); | |
f8772e17 | 131 | } |
7f2da3d7 | 132 | EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer); |
f8772e17 KM |
133 | |
134 | int pxa2xx_soc_pcm_new(struct snd_soc_component *component, | |
135 | struct snd_soc_pcm_runtime *rtd) | |
7afd1b0b DM |
136 | { |
137 | struct snd_card *card = rtd->card->snd_card; | |
138 | struct snd_pcm *pcm = rtd->pcm; | |
139 | int ret; | |
140 | ||
141 | ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32)); | |
142 | if (ret) | |
143 | return ret; | |
144 | ||
7f2da3d7 | 145 | return pxa2xx_pcm_preallocate_dma_buffer(pcm); |
7afd1b0b DM |
146 | } |
147 | EXPORT_SYMBOL(pxa2xx_soc_pcm_new); | |
148 | ||
f8772e17 KM |
149 | int pxa2xx_soc_pcm_open(struct snd_soc_component *component, |
150 | struct snd_pcm_substream *substream) | |
151 | { | |
152 | return pxa2xx_pcm_open(substream); | |
153 | } | |
154 | EXPORT_SYMBOL(pxa2xx_soc_pcm_open); | |
155 | ||
156 | int pxa2xx_soc_pcm_close(struct snd_soc_component *component, | |
157 | struct snd_pcm_substream *substream) | |
158 | { | |
159 | return pxa2xx_pcm_close(substream); | |
160 | } | |
161 | EXPORT_SYMBOL(pxa2xx_soc_pcm_close); | |
162 | ||
163 | int pxa2xx_soc_pcm_hw_params(struct snd_soc_component *component, | |
164 | struct snd_pcm_substream *substream, | |
165 | struct snd_pcm_hw_params *params) | |
166 | { | |
167 | return pxa2xx_pcm_hw_params(substream, params); | |
168 | } | |
169 | EXPORT_SYMBOL(pxa2xx_soc_pcm_hw_params); | |
170 | ||
f8772e17 KM |
171 | int pxa2xx_soc_pcm_prepare(struct snd_soc_component *component, |
172 | struct snd_pcm_substream *substream) | |
173 | { | |
174 | return pxa2xx_pcm_prepare(substream); | |
175 | } | |
176 | EXPORT_SYMBOL(pxa2xx_soc_pcm_prepare); | |
177 | ||
178 | int pxa2xx_soc_pcm_trigger(struct snd_soc_component *component, | |
179 | struct snd_pcm_substream *substream, int cmd) | |
180 | { | |
181 | return pxa2xx_pcm_trigger(substream, cmd); | |
182 | } | |
183 | EXPORT_SYMBOL(pxa2xx_soc_pcm_trigger); | |
184 | ||
185 | snd_pcm_uframes_t | |
186 | pxa2xx_soc_pcm_pointer(struct snd_soc_component *component, | |
187 | struct snd_pcm_substream *substream) | |
188 | { | |
189 | return pxa2xx_pcm_pointer(substream); | |
190 | } | |
191 | EXPORT_SYMBOL(pxa2xx_soc_pcm_pointer); | |
192 | ||
a6d77317 DB |
193 | MODULE_AUTHOR("Nicolas Pitre"); |
194 | MODULE_DESCRIPTION("Intel PXA2xx sound library"); | |
195 | MODULE_LICENSE("GPL"); |