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 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 */
37 void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
38 int *mclk_freqp, int *bits_per_samplep,
41 struct sandbox_codec_priv *priv = dev_get_priv(dev);
43 *interfacep = priv->interface;
45 *mclk_freqp = priv->mclk_freq;
46 *bits_per_samplep = priv->bits_per_sample;
47 *channelsp = priv->channels;
50 int sandbox_get_i2s_sum(struct udevice *dev)
52 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
57 int sandbox_get_setup_called(struct udevice *dev)
59 struct sandbox_sound_priv *priv = dev_get_priv(dev);
61 return priv->setup_called;
64 int sandbox_get_sound_active(struct udevice *dev)
66 struct sandbox_sound_priv *priv = dev_get_priv(dev);
71 int sandbox_get_sound_sum(struct udevice *dev)
73 struct sandbox_sound_priv *priv = dev_get_priv(dev);
78 void sandbox_set_allow_beep(struct udevice *dev, bool allow)
80 struct sandbox_sound_priv *priv = dev_get_priv(dev);
82 priv->allow_beep = allow;
85 int sandbox_get_beep_frequency(struct udevice *dev)
87 struct sandbox_sound_priv *priv = dev_get_priv(dev);
89 return priv->frequency_hz;
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)
96 struct sandbox_codec_priv *priv = dev_get_priv(dev);
98 priv->interface = interface;
100 priv->mclk_freq = mclk_freq;
101 priv->bits_per_sample = bits_per_sample;
102 priv->channels = channels;
107 static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
110 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
113 for (i = 0; i < data_size; i++)
114 priv->sum += ((uint8_t *)data)[i];
119 ret = sandbox_sdl_sound_play(data, data_size);
127 static int sandbox_i2s_probe(struct udevice *dev)
129 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
130 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
132 /* Use hard-coded values here */
135 uc_priv->audio_pll_clk = 192000000;
136 uc_priv->samplingrate = 48000;
137 uc_priv->bitspersample = 16;
138 uc_priv->channels = 2;
141 priv->silent = dev_read_bool(dev, "sandbox,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 */
154 static int sandbox_sound_setup(struct udevice *dev)
156 struct sandbox_sound_priv *priv = dev_get_priv(dev);
158 priv->setup_called++;
163 static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
165 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
166 struct sandbox_sound_priv *priv = dev_get_priv(dev);
169 for (i = 0; i < data_size; i++)
170 priv->sum += ((uint8_t *)data)[i];
172 return i2s_tx_data(uc_priv->i2s, data, data_size);
175 static int sandbox_sound_stop_play(struct udevice *dev)
177 struct sandbox_sound_priv *priv = dev_get_priv(dev);
179 sandbox_sdl_sound_stop();
180 priv->active = false;
185 int sandbox_sound_start_beep(struct udevice *dev, int frequency_hz)
187 struct sandbox_sound_priv *priv = dev_get_priv(dev);
189 if (!priv->allow_beep)
191 priv->frequency_hz = frequency_hz;
196 int sandbox_sound_stop_beep(struct udevice *dev)
198 struct sandbox_sound_priv *priv = dev_get_priv(dev);
200 if (!priv->allow_beep)
202 priv->frequency_hz = 0;
207 static int sandbox_sound_probe(struct udevice *dev)
209 return sound_find_codec_i2s(dev);
212 static const struct audio_codec_ops sandbox_codec_ops = {
213 .set_params = sandbox_codec_set_params,
216 static const struct udevice_id sandbox_codec_ids[] = {
217 { .compatible = "sandbox,audio-codec" },
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_alloc_size = sizeof(struct sandbox_codec_priv),
229 static const struct i2s_ops sandbox_i2s_ops = {
230 .tx_data = sandbox_i2s_tx_data,
233 static const struct udevice_id sandbox_i2s_ids[] = {
234 { .compatible = "sandbox,i2s" },
238 U_BOOT_DRIVER(sandbox_i2s) = {
239 .name = "sandbox_i2s",
241 .of_match = sandbox_i2s_ids,
242 .ops = &sandbox_i2s_ops,
243 .probe = sandbox_i2s_probe,
244 .priv_auto_alloc_size = sizeof(struct sandbox_i2s_priv),
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,
255 static const struct udevice_id sandbox_sound_ids[] = {
256 { .compatible = "sandbox,sound" },
260 U_BOOT_DRIVER(sandbox_sound) = {
261 .name = "sandbox_sound",
263 .of_match = sandbox_sound_ids,
264 .ops = &sandbox_sound_ops,
265 .priv_auto_alloc_size = sizeof(struct sandbox_sound_priv),
266 .probe = sandbox_sound_probe,