blob: a418265c030a5faf1ac3519feaaa26031768eb17 [file] [log] [blame]
Jose Abreu79361b22016-06-09 12:47:05 +01001/*
2 * ALSA SoC Synopsys PIO PCM for I2S driver
3 *
4 * sound/soc/dwc/designware_pcm.c
5 *
6 * Copyright (C) 2016 Synopsys
7 * Jose Abreu <joabreu@synopsys.com>
8 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
14#include <linux/io.h>
15#include <linux/rcupdate.h>
16#include <sound/pcm.h>
17#include <sound/pcm_params.h>
18#include "local.h"
19
20#define BUFFER_BYTES_MAX (3 * 2 * 8 * PERIOD_BYTES_MIN)
21#define PERIOD_BYTES_MIN 4096
22#define PERIODS_MIN 2
23
24#define dw_pcm_tx_fn(sample_bits) \
25static unsigned int dw_pcm_tx_##sample_bits(struct dw_i2s_dev *dev, \
26 struct snd_pcm_runtime *runtime, unsigned int tx_ptr, \
27 bool *period_elapsed) \
28{ \
29 const u##sample_bits (*p)[2] = (void *)runtime->dma_area; \
30 unsigned int period_pos = tx_ptr % runtime->period_size; \
31 int i; \
32\
33 for (i = 0; i < dev->fifo_th; i++) { \
Maxim Kochetkov221acc12023-06-22 23:00:29 +030034 iowrite32(p[tx_ptr][0], dev->i2s_base + dev->l_reg); \
35 iowrite32(p[tx_ptr][1], dev->i2s_base + dev->r_reg); \
Jose Abreu79361b22016-06-09 12:47:05 +010036 period_pos++; \
37 if (++tx_ptr >= runtime->buffer_size) \
38 tx_ptr = 0; \
39 } \
40 *period_elapsed = period_pos >= runtime->period_size; \
41 return tx_ptr; \
42}
43
Jose Abreue2f748e2016-12-27 14:00:53 +000044#define dw_pcm_rx_fn(sample_bits) \
45static unsigned int dw_pcm_rx_##sample_bits(struct dw_i2s_dev *dev, \
46 struct snd_pcm_runtime *runtime, unsigned int rx_ptr, \
47 bool *period_elapsed) \
48{ \
49 u##sample_bits (*p)[2] = (void *)runtime->dma_area; \
50 unsigned int period_pos = rx_ptr % runtime->period_size; \
51 int i; \
52\
53 for (i = 0; i < dev->fifo_th; i++) { \
Maxim Kochetkov221acc12023-06-22 23:00:29 +030054 p[rx_ptr][0] = ioread32(dev->i2s_base + dev->l_reg); \
55 p[rx_ptr][1] = ioread32(dev->i2s_base + dev->r_reg); \
Jose Abreue2f748e2016-12-27 14:00:53 +000056 period_pos++; \
57 if (++rx_ptr >= runtime->buffer_size) \
58 rx_ptr = 0; \
59 } \
60 *period_elapsed = period_pos >= runtime->period_size; \
61 return rx_ptr; \
62}
63
Jose Abreu79361b22016-06-09 12:47:05 +010064dw_pcm_tx_fn(16);
65dw_pcm_tx_fn(32);
Jose Abreue2f748e2016-12-27 14:00:53 +000066dw_pcm_rx_fn(16);
67dw_pcm_rx_fn(32);
Jose Abreu79361b22016-06-09 12:47:05 +010068
69#undef dw_pcm_tx_fn
Jose Abreue2f748e2016-12-27 14:00:53 +000070#undef dw_pcm_rx_fn
Jose Abreu79361b22016-06-09 12:47:05 +010071
72static const struct snd_pcm_hardware dw_pcm_hardware = {
73 .info = SNDRV_PCM_INFO_INTERLEAVED |
74 SNDRV_PCM_INFO_MMAP |
75 SNDRV_PCM_INFO_MMAP_VALID |
76 SNDRV_PCM_INFO_BLOCK_TRANSFER,
77 .rates = SNDRV_PCM_RATE_32000 |
78 SNDRV_PCM_RATE_44100 |
79 SNDRV_PCM_RATE_48000,
80 .rate_min = 32000,
81 .rate_max = 48000,
82 .formats = SNDRV_PCM_FMTBIT_S16_LE |
Jose Abreue21ab172016-12-27 14:00:54 +000083 SNDRV_PCM_FMTBIT_S24_LE |
Jose Abreu79361b22016-06-09 12:47:05 +010084 SNDRV_PCM_FMTBIT_S32_LE,
85 .channels_min = 2,
86 .channels_max = 2,
87 .buffer_bytes_max = BUFFER_BYTES_MAX,
88 .period_bytes_min = PERIOD_BYTES_MIN,
89 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
90 .periods_min = PERIODS_MIN,
91 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
92 .fifo_size = 16,
93};
94
Jose Abreue2f748e2016-12-27 14:00:53 +000095static void dw_pcm_transfer(struct dw_i2s_dev *dev, bool push)
Jose Abreu79361b22016-06-09 12:47:05 +010096{
Jose Abreue2f748e2016-12-27 14:00:53 +000097 struct snd_pcm_substream *substream;
98 bool active, period_elapsed;
Jose Abreu79361b22016-06-09 12:47:05 +010099
100 rcu_read_lock();
Jose Abreue2f748e2016-12-27 14:00:53 +0000101 if (push)
102 substream = rcu_dereference(dev->tx_substream);
103 else
104 substream = rcu_dereference(dev->rx_substream);
105 active = substream && snd_pcm_running(substream);
106 if (active) {
107 unsigned int ptr;
108 unsigned int new_ptr;
109
110 if (push) {
111 ptr = READ_ONCE(dev->tx_ptr);
112 new_ptr = dev->tx_fn(dev, substream->runtime, ptr,
113 &period_elapsed);
114 cmpxchg(&dev->tx_ptr, ptr, new_ptr);
115 } else {
116 ptr = READ_ONCE(dev->rx_ptr);
117 new_ptr = dev->rx_fn(dev, substream->runtime, ptr,
118 &period_elapsed);
119 cmpxchg(&dev->rx_ptr, ptr, new_ptr);
120 }
Jose Abreu79361b22016-06-09 12:47:05 +0100121
122 if (period_elapsed)
Jose Abreue2f748e2016-12-27 14:00:53 +0000123 snd_pcm_period_elapsed(substream);
Jose Abreu79361b22016-06-09 12:47:05 +0100124 }
125 rcu_read_unlock();
126}
Jose Abreue2f748e2016-12-27 14:00:53 +0000127
128void dw_pcm_push_tx(struct dw_i2s_dev *dev)
129{
130 dw_pcm_transfer(dev, true);
131}
Jose Abreu79361b22016-06-09 12:47:05 +0100132
Jose Abreue2f748e2016-12-27 14:00:53 +0000133void dw_pcm_pop_rx(struct dw_i2s_dev *dev)
134{
135 dw_pcm_transfer(dev, false);
136}
Jose Abreue2f748e2016-12-27 14:00:53 +0000137
Kuninori Morimotodfd00af2019-10-02 14:34:15 +0900138static int dw_pcm_open(struct snd_soc_component *component,
139 struct snd_pcm_substream *substream)
Jose Abreu79361b22016-06-09 12:47:05 +0100140{
141 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimotof8af41a2023-09-11 23:47:56 +0000142 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
143 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
Jose Abreu79361b22016-06-09 12:47:05 +0100144
145 snd_soc_set_runtime_hwparams(substream, &dw_pcm_hardware);
146 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
147 runtime->private_data = dev;
148
149 return 0;
150}
151
Kuninori Morimotodfd00af2019-10-02 14:34:15 +0900152static int dw_pcm_close(struct snd_soc_component *component,
153 struct snd_pcm_substream *substream)
Jose Abreu79361b22016-06-09 12:47:05 +0100154{
155 synchronize_rcu();
156 return 0;
157}
158
Kuninori Morimotodfd00af2019-10-02 14:34:15 +0900159static int dw_pcm_hw_params(struct snd_soc_component *component,
160 struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *hw_params)
Jose Abreu79361b22016-06-09 12:47:05 +0100162{
163 struct snd_pcm_runtime *runtime = substream->runtime;
164 struct dw_i2s_dev *dev = runtime->private_data;
Jose Abreu79361b22016-06-09 12:47:05 +0100165
166 switch (params_channels(hw_params)) {
167 case 2:
168 break;
169 default:
170 dev_err(dev->dev, "invalid channels number\n");
171 return -EINVAL;
172 }
173
174 switch (params_format(hw_params)) {
175 case SNDRV_PCM_FORMAT_S16_LE:
176 dev->tx_fn = dw_pcm_tx_16;
Jose Abreue2f748e2016-12-27 14:00:53 +0000177 dev->rx_fn = dw_pcm_rx_16;
Jose Abreu79361b22016-06-09 12:47:05 +0100178 break;
Jose Abreue21ab172016-12-27 14:00:54 +0000179 case SNDRV_PCM_FORMAT_S24_LE:
Jose Abreu79361b22016-06-09 12:47:05 +0100180 case SNDRV_PCM_FORMAT_S32_LE:
181 dev->tx_fn = dw_pcm_tx_32;
Jose Abreue2f748e2016-12-27 14:00:53 +0000182 dev->rx_fn = dw_pcm_rx_32;
Jose Abreu79361b22016-06-09 12:47:05 +0100183 break;
184 default:
185 dev_err(dev->dev, "invalid format\n");
186 return -EINVAL;
187 }
188
Takashi Iwaifcf306ef2019-12-10 15:25:54 +0100189 return 0;
Jose Abreu79361b22016-06-09 12:47:05 +0100190}
191
Kuninori Morimotodfd00af2019-10-02 14:34:15 +0900192static int dw_pcm_trigger(struct snd_soc_component *component,
193 struct snd_pcm_substream *substream, int cmd)
Jose Abreu79361b22016-06-09 12:47:05 +0100194{
195 struct snd_pcm_runtime *runtime = substream->runtime;
196 struct dw_i2s_dev *dev = runtime->private_data;
197 int ret = 0;
198
199 switch (cmd) {
200 case SNDRV_PCM_TRIGGER_START:
201 case SNDRV_PCM_TRIGGER_RESUME:
202 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Jose Abreue2f748e2016-12-27 14:00:53 +0000203 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
204 WRITE_ONCE(dev->tx_ptr, 0);
205 rcu_assign_pointer(dev->tx_substream, substream);
206 } else {
207 WRITE_ONCE(dev->rx_ptr, 0);
208 rcu_assign_pointer(dev->rx_substream, substream);
209 }
Jose Abreu79361b22016-06-09 12:47:05 +0100210 break;
211 case SNDRV_PCM_TRIGGER_STOP:
212 case SNDRV_PCM_TRIGGER_SUSPEND:
213 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jose Abreue2f748e2016-12-27 14:00:53 +0000214 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
215 rcu_assign_pointer(dev->tx_substream, NULL);
216 else
217 rcu_assign_pointer(dev->rx_substream, NULL);
Jose Abreu79361b22016-06-09 12:47:05 +0100218 break;
219 default:
220 ret = -EINVAL;
221 break;
222 }
223
224 return ret;
225}
226
Kuninori Morimotodfd00af2019-10-02 14:34:15 +0900227static snd_pcm_uframes_t dw_pcm_pointer(struct snd_soc_component *component,
228 struct snd_pcm_substream *substream)
Jose Abreu79361b22016-06-09 12:47:05 +0100229{
230 struct snd_pcm_runtime *runtime = substream->runtime;
231 struct dw_i2s_dev *dev = runtime->private_data;
Jose Abreue2f748e2016-12-27 14:00:53 +0000232 snd_pcm_uframes_t pos;
233
234 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
235 pos = READ_ONCE(dev->tx_ptr);
236 else
237 pos = READ_ONCE(dev->rx_ptr);
Jose Abreu79361b22016-06-09 12:47:05 +0100238
239 return pos < runtime->buffer_size ? pos : 0;
240}
241
Kuninori Morimotodfd00af2019-10-02 14:34:15 +0900242static int dw_pcm_new(struct snd_soc_component *component,
243 struct snd_soc_pcm_runtime *rtd)
Jose Abreu79361b22016-06-09 12:47:05 +0100244{
245 size_t size = dw_pcm_hardware.buffer_bytes_max;
246
Takashi Iwaifcf306ef2019-12-10 15:25:54 +0100247 snd_pcm_set_managed_buffer_all(rtd->pcm,
Jose Abreu79361b22016-06-09 12:47:05 +0100248 SNDRV_DMA_TYPE_CONTINUOUS,
Takashi Iwai1a486032019-11-08 10:46:34 +0100249 NULL, size, size);
Takashi Iwaiad8ba772019-02-04 16:38:20 +0100250 return 0;
Jose Abreu79361b22016-06-09 12:47:05 +0100251}
252
Kuninori Morimotoddce03c2018-01-29 02:47:38 +0000253static const struct snd_soc_component_driver dw_pcm_component = {
Kuninori Morimotodfd00af2019-10-02 14:34:15 +0900254 .open = dw_pcm_open,
255 .close = dw_pcm_close,
Kuninori Morimotodfd00af2019-10-02 14:34:15 +0900256 .hw_params = dw_pcm_hw_params,
Kuninori Morimotodfd00af2019-10-02 14:34:15 +0900257 .trigger = dw_pcm_trigger,
258 .pointer = dw_pcm_pointer,
259 .pcm_construct = dw_pcm_new,
Jose Abreu79361b22016-06-09 12:47:05 +0100260};
261
262int dw_pcm_register(struct platform_device *pdev)
263{
Kuninori Morimotoddce03c2018-01-29 02:47:38 +0000264 return devm_snd_soc_register_component(&pdev->dev, &dw_pcm_component,
265 NULL, 0);
Jose Abreu79361b22016-06-09 12:47:05 +0100266}