1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2013 Google, Inc
4 */
5
6 #define LOG_CATEGORY UCLASS_SOUND
7
8 #include <common.h>
9 #include <audio_codec.h>
10 #include <dm.h>
11 #include <i2s.h>
12 #include <log.h>
13 #include <sound.h>
14 #include <asm/sdl.h>
15
16 struct sandbox_codec_priv {
17 int interface;
18 int rate;
19 int mclk_freq;
20 int bits_per_sample;
21 uint channels;
22 };
23
24 struct sandbox_i2s_priv {
25 int sum; /* Use to sum the provided audio data */
26 bool silent; /* Sound is silent, don't use SDL */
27 };
28
29 struct sandbox_sound_priv {
30 int setup_called; /* Incremented when setup() method is called */
31 bool active; /* TX data is being sent */
32 int sum; /* Use to sum the provided audio data */
33 bool allow_beep; /* true to allow the start_beep() interface */
34 int frequency_hz; /* Beep frequency if active, else 0 */
35 };
36
sandbox_get_codec_params(struct udevice * dev,int * interfacep,int * ratep,int * mclk_freqp,int * bits_per_samplep,uint * channelsp)37 void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
38 int *mclk_freqp, int *bits_per_samplep,
39 uint *channelsp)
40 {
41 struct sandbox_codec_priv *priv = dev_get_priv(dev);
42
43 *interfacep = priv->interface;
44 *ratep = priv->rate;
45 *mclk_freqp = priv->mclk_freq;
46 *bits_per_samplep = priv->bits_per_sample;
47 *channelsp = priv->channels;
48 }
49
sandbox_get_i2s_sum(struct udevice * dev)50 int sandbox_get_i2s_sum(struct udevice *dev)
51 {
52 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
53
54 return priv->sum;
55 }
56
sandbox_get_setup_called(struct udevice * dev)57 int sandbox_get_setup_called(struct udevice *dev)
58 {
59 struct sandbox_sound_priv *priv = dev_get_priv(dev);
60
61 return priv->setup_called;
62 }
63
sandbox_get_sound_active(struct udevice * dev)64 int sandbox_get_sound_active(struct udevice *dev)
65 {
66 struct sandbox_sound_priv *priv = dev_get_priv(dev);
67
68 return priv->active;
69 }
70
sandbox_get_sound_sum(struct udevice * dev)71 int sandbox_get_sound_sum(struct udevice *dev)
72 {
73 struct sandbox_sound_priv *priv = dev_get_priv(dev);
74
75 return priv->sum;
76 }
77
sandbox_set_allow_beep(struct udevice * dev,bool allow)78 void sandbox_set_allow_beep(struct udevice *dev, bool allow)
79 {
80 struct sandbox_sound_priv *priv = dev_get_priv(dev);
81
82 priv->allow_beep = allow;
83 }
84
sandbox_get_beep_frequency(struct udevice * dev)85 int sandbox_get_beep_frequency(struct udevice *dev)
86 {
87 struct sandbox_sound_priv *priv = dev_get_priv(dev);
88
89 return priv->frequency_hz;
90 }
91
sandbox_codec_set_params(struct udevice * dev,int interface,int rate,int mclk_freq,int bits_per_sample,uint channels)92 static int sandbox_codec_set_params(struct udevice *dev, int interface,
93 int rate, int mclk_freq,
94 int bits_per_sample, uint channels)
95 {
96 struct sandbox_codec_priv *priv = dev_get_priv(dev);
97
98 priv->interface = interface;
99 priv->rate = rate;
100 priv->mclk_freq = mclk_freq;
101 priv->bits_per_sample = bits_per_sample;
102 priv->channels = channels;
103
104 return 0;
105 }
106
sandbox_i2s_tx_data(struct udevice * dev,void * data,uint data_size)107 static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
108 uint data_size)
109 {
110 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
111 int i;
112
113 for (i = 0; i < data_size; i++)
114 priv->sum += ((uint8_t *)data)[i];
115
116 if (!priv->silent) {
117 int ret;
118
119 ret = sandbox_sdl_sound_play(data, data_size);
120 if (ret)
121 return ret;
122 }
123
124 return 0;
125 }
126
sandbox_i2s_probe(struct udevice * dev)127 static int sandbox_i2s_probe(struct udevice *dev)
128 {
129 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
130 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
131
132 /* Use hard-coded values here */
133 uc_priv->rfs = 256;
134 uc_priv->bfs = 32;
135 uc_priv->audio_pll_clk = 192000000;
136 uc_priv->samplingrate = 48000;
137 uc_priv->bitspersample = 16;
138 uc_priv->channels = 2;
139 uc_priv->id = 1;
140
141 priv->silent = dev_read_bool(dev, "sandbox,silent");
142
143 if (priv->silent) {
144 log_warning("Sound is silenced\n");
145 } else if (sandbox_sdl_sound_init(uc_priv->samplingrate,
146 uc_priv->channels)) {
147 /* Ignore any error here - we'll just have no sound */
148 priv->silent = true;
149 }
150
151 return 0;
152 }
153
sandbox_sound_setup(struct udevice * dev)154 static int sandbox_sound_setup(struct udevice *dev)
155 {
156 struct sandbox_sound_priv *priv = dev_get_priv(dev);
157
158 priv->setup_called++;
159
160 return 0;
161 }
162
sandbox_sound_play(struct udevice * dev,void * data,uint data_size)163 static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
164 {
165 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
166 struct sandbox_sound_priv *priv = dev_get_priv(dev);
167 int i;
168
169 for (i = 0; i < data_size; i++)
170 priv->sum += ((uint8_t *)data)[i];
171
172 return i2s_tx_data(uc_priv->i2s, data, data_size);
173 }
174
sandbox_sound_stop_play(struct udevice * dev)175 static int sandbox_sound_stop_play(struct udevice *dev)
176 {
177 struct sandbox_sound_priv *priv = dev_get_priv(dev);
178
179 sandbox_sdl_sound_stop();
180 priv->active = false;
181
182 return 0;
183 }
184
sandbox_sound_start_beep(struct udevice * dev,int frequency_hz)185 int sandbox_sound_start_beep(struct udevice *dev, int frequency_hz)
186 {
187 struct sandbox_sound_priv *priv = dev_get_priv(dev);
188
189 if (!priv->allow_beep)
190 return -ENOSYS;
191 priv->frequency_hz = frequency_hz;
192
193 return 0;
194 }
195
sandbox_sound_stop_beep(struct udevice * dev)196 int sandbox_sound_stop_beep(struct udevice *dev)
197 {
198 struct sandbox_sound_priv *priv = dev_get_priv(dev);
199
200 if (!priv->allow_beep)
201 return -ENOSYS;
202 priv->frequency_hz = 0;
203
204 return 0;
205 }
206
sandbox_sound_probe(struct udevice * dev)207 static int sandbox_sound_probe(struct udevice *dev)
208 {
209 return sound_find_codec_i2s(dev);
210 }
211
212 static const struct audio_codec_ops sandbox_codec_ops = {
213 .set_params = sandbox_codec_set_params,
214 };
215
216 static const struct udevice_id sandbox_codec_ids[] = {
217 { .compatible = "sandbox,audio-codec" },
218 { }
219 };
220
221 U_BOOT_DRIVER(sandbox_codec) = {
222 .name = "sandbox_codec",
223 .id = UCLASS_AUDIO_CODEC,
224 .of_match = sandbox_codec_ids,
225 .ops = &sandbox_codec_ops,
226 .priv_auto = sizeof(struct sandbox_codec_priv),
227 };
228
229 static const struct i2s_ops sandbox_i2s_ops = {
230 .tx_data = sandbox_i2s_tx_data,
231 };
232
233 static const struct udevice_id sandbox_i2s_ids[] = {
234 { .compatible = "sandbox,i2s" },
235 { }
236 };
237
238 U_BOOT_DRIVER(sandbox_i2s) = {
239 .name = "sandbox_i2s",
240 .id = UCLASS_I2S,
241 .of_match = sandbox_i2s_ids,
242 .ops = &sandbox_i2s_ops,
243 .probe = sandbox_i2s_probe,
244 .priv_auto = sizeof(struct sandbox_i2s_priv),
245 };
246
247 static const struct sound_ops sandbox_sound_ops = {
248 .setup = sandbox_sound_setup,
249 .play = sandbox_sound_play,
250 .stop_play = sandbox_sound_stop_play,
251 .start_beep = sandbox_sound_start_beep,
252 .stop_beep = sandbox_sound_stop_beep,
253 };
254
255 static const struct udevice_id sandbox_sound_ids[] = {
256 { .compatible = "sandbox,sound" },
257 { }
258 };
259
260 U_BOOT_DRIVER(sandbox_sound) = {
261 .name = "sandbox_sound",
262 .id = UCLASS_SOUND,
263 .of_match = sandbox_sound_ids,
264 .ops = &sandbox_sound_ops,
265 .priv_auto = sizeof(struct sandbox_sound_priv),
266 .probe = sandbox_sound_probe,
267 };
268