blob: 0a48805e513a5186e2c2b92262385d4fc1ba5271 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Dmitry Baryshkova6d77312008-09-10 05:01:20 +04002
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/slab.h>
Dmitry Baryshkova6d77312008-09-10 05:01:20 +04004#include <linux/module.h>
5#include <linux/dma-mapping.h>
Daniel Mackd65a1452013-08-12 10:42:39 +02006#include <linux/dmaengine.h>
Daniel Mack58ceb572015-09-30 22:51:52 +02007#include <linux/dma/pxa-dma.h>
Dmitry Baryshkova6d77312008-09-10 05:01:20 +04008
9#include <sound/core.h>
10#include <sound/pcm.h>
11#include <sound/pcm_params.h>
12#include <sound/pxa2xx-lib.h>
Daniel Mackd65a1452013-08-12 10:42:39 +020013#include <sound/dmaengine_pcm.h>
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040014
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040015static 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 |
Daniel Mack456ec802018-06-27 21:33:56 +020022 SNDRV_PCM_FMTBIT_S24_LE |
23 SNDRV_PCM_FMTBIT_S32_LE,
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040024 .period_bytes_min = 32,
25 .period_bytes_max = 8192 - 32,
26 .periods_min = 1,
Daniel Mack58ceb572015-09-30 22:51:52 +020027 .periods_max = 256,
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040028 .buffer_bytes_max = 128 * 1024,
29 .fifo_size = 32,
30};
31
Daniel Macka7160672018-06-27 21:33:54 +020032int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
33 struct snd_pcm_hw_params *params)
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040034{
Daniel Mack58ceb572015-09-30 22:51:52 +020035 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;
Daniel Mackd65a1452013-08-12 10:42:39 +020040
Kuninori Morimoto575be882020-03-23 14:21:49 +090041 dma_params = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
Daniel Mack58ceb572015-09-30 22:51:52 +020042 if (!dma_params)
43 return 0;
Daniel Mackd65a1452013-08-12 10:42:39 +020044
Daniel Mack58ceb572015-09-30 22:51:52 +020045 ret = snd_hwparams_to_dma_slave_config(substream, params, &config);
46 if (ret)
47 return ret;
48
49 snd_dmaengine_pcm_set_config_from_dai_data(substream,
Kuninori Morimoto575be882020-03-23 14:21:49 +090050 snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream),
Daniel Mack58ceb572015-09-30 22:51:52 +020051 &config);
52
53 ret = dmaengine_slave_config(chan, &config);
54 if (ret)
55 return ret;
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040056
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040057 return 0;
58}
Daniel Macka7160672018-06-27 21:33:54 +020059EXPORT_SYMBOL(pxa2xx_pcm_hw_params);
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040060
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040061int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
62{
Daniel Mack58ceb572015-09-30 22:51:52 +020063 return snd_dmaengine_pcm_trigger(substream, cmd);
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040064}
65EXPORT_SYMBOL(pxa2xx_pcm_trigger);
66
67snd_pcm_uframes_t
68pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
69{
Daniel Mack58ceb572015-09-30 22:51:52 +020070 return snd_dmaengine_pcm_pointer(substream);
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040071}
72EXPORT_SYMBOL(pxa2xx_pcm_pointer);
73
Daniel Macka7160672018-06-27 21:33:54 +020074int pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040075{
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040076 return 0;
77}
Daniel Macka7160672018-06-27 21:33:54 +020078EXPORT_SYMBOL(pxa2xx_pcm_prepare);
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040079
Daniel Macka7160672018-06-27 21:33:54 +020080int pxa2xx_pcm_open(struct snd_pcm_substream *substream)
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040081{
Daniel Mack58ceb572015-09-30 22:51:52 +020082 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040083 struct snd_pcm_runtime *runtime = substream->runtime;
Daniel Mack58ceb572015-09-30 22:51:52 +020084 struct snd_dmaengine_dai_dma_data *dma_params;
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040085 int ret;
86
87 runtime->hw = pxa2xx_pcm_hardware;
88
Kuninori Morimoto575be882020-03-23 14:21:49 +090089 dma_params = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
Daniel Mack58ceb572015-09-30 22:51:52 +020090 if (!dma_params)
91 return 0;
92
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040093 /*
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)
Daniel Mack58ceb572015-09-30 22:51:52 +0200101 return ret;
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400102
103 ret = snd_pcm_hw_constraint_step(runtime, 0,
104 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
105 if (ret)
Daniel Mack58ceb572015-09-30 22:51:52 +0200106 return ret;
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400107
108 ret = snd_pcm_hw_constraint_integer(runtime,
109 SNDRV_PCM_HW_PARAM_PERIODS);
110 if (ret < 0)
Daniel Mack58ceb572015-09-30 22:51:52 +0200111 return ret;
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400112
Robert Jarzmik8f540612018-06-28 22:08:37 +0200113 return snd_dmaengine_pcm_open(
Kuninori Morimoto575be882020-03-23 14:21:49 +0900114 substream, dma_request_slave_channel(asoc_rtd_to_cpu(rtd, 0)->dev,
Robert Jarzmik8f540612018-06-28 22:08:37 +0200115 dma_params->chan_name));
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400116}
Daniel Macka7160672018-06-27 21:33:54 +0200117EXPORT_SYMBOL(pxa2xx_pcm_open);
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400118
Daniel Macka7160672018-06-27 21:33:54 +0200119int pxa2xx_pcm_close(struct snd_pcm_substream *substream)
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400120{
Daniel Mack58ceb572015-09-30 22:51:52 +0200121 return snd_dmaengine_pcm_close_release_chan(substream);
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400122}
Daniel Macka7160672018-06-27 21:33:54 +0200123EXPORT_SYMBOL(pxa2xx_pcm_close);
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400124
Takashi Iwai7f2da3d2021-08-02 09:28:05 +0200125int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm)
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400126{
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400127 size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
Takashi Iwai7f2da3d2021-08-02 09:28:05 +0200128
129 return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_WC,
130 pcm->card->dev, size);
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400131}
132EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer);
133
Kuninori Morimotof8772e12019-10-02 14:33:50 +0900134int pxa2xx_soc_pcm_new(struct snd_soc_component *component,
135 struct snd_soc_pcm_runtime *rtd)
Daniel Mack7afd1b02018-06-27 21:33:55 +0200136{
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
Takashi Iwai7f2da3d2021-08-02 09:28:05 +0200145 return pxa2xx_pcm_preallocate_dma_buffer(pcm);
Daniel Mack7afd1b02018-06-27 21:33:55 +0200146}
147EXPORT_SYMBOL(pxa2xx_soc_pcm_new);
148
Kuninori Morimotof8772e12019-10-02 14:33:50 +0900149int pxa2xx_soc_pcm_open(struct snd_soc_component *component,
150 struct snd_pcm_substream *substream)
151{
152 return pxa2xx_pcm_open(substream);
153}
154EXPORT_SYMBOL(pxa2xx_soc_pcm_open);
155
156int pxa2xx_soc_pcm_close(struct snd_soc_component *component,
157 struct snd_pcm_substream *substream)
158{
159 return pxa2xx_pcm_close(substream);
160}
161EXPORT_SYMBOL(pxa2xx_soc_pcm_close);
162
163int 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}
169EXPORT_SYMBOL(pxa2xx_soc_pcm_hw_params);
170
Kuninori Morimotof8772e12019-10-02 14:33:50 +0900171int pxa2xx_soc_pcm_prepare(struct snd_soc_component *component,
172 struct snd_pcm_substream *substream)
173{
174 return pxa2xx_pcm_prepare(substream);
175}
176EXPORT_SYMBOL(pxa2xx_soc_pcm_prepare);
177
178int 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}
183EXPORT_SYMBOL(pxa2xx_soc_pcm_trigger);
184
185snd_pcm_uframes_t
186pxa2xx_soc_pcm_pointer(struct snd_soc_component *component,
187 struct snd_pcm_substream *substream)
188{
189 return pxa2xx_pcm_pointer(substream);
190}
191EXPORT_SYMBOL(pxa2xx_soc_pcm_pointer);
192
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400193MODULE_AUTHOR("Nicolas Pitre");
194MODULE_DESCRIPTION("Intel PXA2xx sound library");
195MODULE_LICENSE("GPL");