1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
6 #define LOG_CATEGORY UCLASS_SOUND
9 #include <audio_codec.h>
16 struct sandbox_codec_priv {
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 */
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 count; /* Use to count the provided audio data */
33 int sum; /* Use to sum the provided audio data */
34 bool allow_beep; /* true to allow the start_beep() interface */
35 int frequency_hz; /* Beep frequency if active, else 0 */
38 void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
39 int *mclk_freqp, int *bits_per_samplep,
42 struct sandbox_codec_priv *priv = dev_get_priv(dev);
44 *interfacep = priv->interface;
46 *mclk_freqp = priv->mclk_freq;
47 *bits_per_samplep = priv->bits_per_sample;
48 *channelsp = priv->channels;
51 int sandbox_get_i2s_sum(struct udevice *dev)
53 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
58 int sandbox_get_setup_called(struct udevice *dev)
60 struct sandbox_sound_priv *priv = dev_get_priv(dev);
62 return priv->setup_called;
65 int sandbox_get_sound_active(struct udevice *dev)
67 struct sandbox_sound_priv *priv = dev_get_priv(dev);
72 int sandbox_get_sound_count(struct udevice *dev)
74 struct sandbox_sound_priv *priv = dev_get_priv(dev);
79 int sandbox_get_sound_sum(struct udevice *dev)
81 struct sandbox_sound_priv *priv = dev_get_priv(dev);
86 void sandbox_set_allow_beep(struct udevice *dev, bool allow)
88 struct sandbox_sound_priv *priv = dev_get_priv(dev);
90 priv->allow_beep = allow;
93 int sandbox_get_beep_frequency(struct udevice *dev)
95 struct sandbox_sound_priv *priv = dev_get_priv(dev);
97 return priv->frequency_hz;
100 static int sandbox_codec_set_params(struct udevice *dev, int interface,
101 int rate, int mclk_freq,
102 int bits_per_sample, uint channels)
104 struct sandbox_codec_priv *priv = dev_get_priv(dev);
106 priv->interface = interface;
108 priv->mclk_freq = mclk_freq;
109 priv->bits_per_sample = bits_per_sample;
110 priv->channels = channels;
115 static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
118 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
121 for (i = 0; i < data_size; i++)
122 priv->sum += ((uint8_t *)data)[i];
127 ret = sandbox_sdl_sound_play(data, data_size);
135 static int sandbox_i2s_probe(struct udevice *dev)
137 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
138 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
140 /* Use hard-coded values here */
143 uc_priv->audio_pll_clk = 192000000;
144 uc_priv->samplingrate = 48000;
145 uc_priv->bitspersample = 16;
146 uc_priv->channels = 2;
149 priv->silent = dev_read_bool(dev, "sandbox,silent");
152 log_warning("Sound is silenced\n");
153 } else if (sandbox_sdl_sound_init(uc_priv->samplingrate,
154 uc_priv->channels)) {
155 /* Ignore any error here - we'll just have no sound */
162 static int sandbox_sound_setup(struct udevice *dev)
164 struct sandbox_sound_priv *priv = dev_get_priv(dev);
166 priv->setup_called++;
171 static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
173 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
174 struct sandbox_sound_priv *priv = dev_get_priv(dev);
177 for (i = 0; i < data_size; i++)
178 priv->sum += ((uint8_t *)data)[i];
179 priv->count += data_size;
181 return i2s_tx_data(uc_priv->i2s, data, data_size);
184 static int sandbox_sound_stop_play(struct udevice *dev)
186 struct sandbox_sound_priv *priv = dev_get_priv(dev);
188 sandbox_sdl_sound_stop();
189 priv->active = false;
194 int sandbox_sound_start_beep(struct udevice *dev, int frequency_hz)
196 struct sandbox_sound_priv *priv = dev_get_priv(dev);
198 if (!priv->allow_beep)
200 priv->frequency_hz = frequency_hz;
205 int sandbox_sound_stop_beep(struct udevice *dev)
207 struct sandbox_sound_priv *priv = dev_get_priv(dev);
209 if (!priv->allow_beep)
211 priv->frequency_hz = 0;
216 static int sandbox_sound_probe(struct udevice *dev)
218 return sound_find_codec_i2s(dev);
221 static const struct audio_codec_ops sandbox_codec_ops = {
222 .set_params = sandbox_codec_set_params,
225 static const struct udevice_id sandbox_codec_ids[] = {
226 { .compatible = "sandbox,audio-codec" },
230 U_BOOT_DRIVER(sandbox_codec) = {
231 .name = "sandbox_codec",
232 .id = UCLASS_AUDIO_CODEC,
233 .of_match = sandbox_codec_ids,
234 .ops = &sandbox_codec_ops,
235 .priv_auto = sizeof(struct sandbox_codec_priv),
238 static const struct i2s_ops sandbox_i2s_ops = {
239 .tx_data = sandbox_i2s_tx_data,
242 static const struct udevice_id sandbox_i2s_ids[] = {
243 { .compatible = "sandbox,i2s" },
247 U_BOOT_DRIVER(sandbox_i2s) = {
248 .name = "sandbox_i2s",
250 .of_match = sandbox_i2s_ids,
251 .ops = &sandbox_i2s_ops,
252 .probe = sandbox_i2s_probe,
253 .priv_auto = sizeof(struct sandbox_i2s_priv),
256 static const struct sound_ops sandbox_sound_ops = {
257 .setup = sandbox_sound_setup,
258 .play = sandbox_sound_play,
259 .stop_play = sandbox_sound_stop_play,
260 .start_beep = sandbox_sound_start_beep,
261 .stop_beep = sandbox_sound_stop_beep,
264 static const struct udevice_id sandbox_sound_ids[] = {
265 { .compatible = "sandbox,sound" },
269 U_BOOT_DRIVER(sandbox_sound) = {
270 .name = "sandbox_sound",
272 .of_match = sandbox_sound_ids,
273 .ops = &sandbox_sound_ops,
274 .priv_auto = sizeof(struct sandbox_sound_priv),
275 .probe = sandbox_sound_probe,