1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
6 #define LOG_CATEGORY UCLASS_SOUND
9 #include <audio_codec.h>
15 struct sandbox_codec_priv {
23 struct sandbox_i2s_priv {
24 int sum; /* Use to sum the provided audio data */
25 bool silent; /* Sound is silent, don't use SDL */
28 struct sandbox_sound_priv {
30 int sum; /* Use to sum the provided audio data */
31 bool allow_beep; /* true to allow the start_beep() interface */
32 int frequency_hz; /* Beep frequency if active, else 0 */
35 void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
36 int *mclk_freqp, int *bits_per_samplep,
39 struct sandbox_codec_priv *priv = dev_get_priv(dev);
41 *interfacep = priv->interface;
43 *mclk_freqp = priv->mclk_freq;
44 *bits_per_samplep = priv->bits_per_sample;
45 *channelsp = priv->channels;
48 int sandbox_get_i2s_sum(struct udevice *dev)
50 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
55 int sandbox_get_setup_called(struct udevice *dev)
57 struct sandbox_sound_priv *priv = dev_get_priv(dev);
59 return priv->setup_called;
62 int sandbox_get_sound_sum(struct udevice *dev)
64 struct sandbox_sound_priv *priv = dev_get_priv(dev);
69 void sandbox_set_allow_beep(struct udevice *dev, bool allow)
71 struct sandbox_sound_priv *priv = dev_get_priv(dev);
73 priv->allow_beep = allow;
76 int sandbox_get_beep_frequency(struct udevice *dev)
78 struct sandbox_sound_priv *priv = dev_get_priv(dev);
80 return priv->frequency_hz;
83 static int sandbox_codec_set_params(struct udevice *dev, int interface,
84 int rate, int mclk_freq,
85 int bits_per_sample, uint channels)
87 struct sandbox_codec_priv *priv = dev_get_priv(dev);
89 priv->interface = interface;
91 priv->mclk_freq = mclk_freq;
92 priv->bits_per_sample = bits_per_sample;
93 priv->channels = channels;
98 static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
101 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
104 for (i = 0; i < data_size; i++)
105 priv->sum += ((uint8_t *)data)[i];
110 ret = sandbox_sdl_sound_play(data, data_size);
118 static int sandbox_i2s_probe(struct udevice *dev)
120 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
121 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
123 /* Use hard-coded values here */
126 uc_priv->audio_pll_clk = 192000000;
127 uc_priv->samplingrate = 48000;
128 uc_priv->bitspersample = 16;
129 uc_priv->channels = 2;
132 priv->silent = dev_read_bool(dev, "sandbox,silent");
135 log_warning("Sound is silenced\n");
136 } else if (sandbox_sdl_sound_init(uc_priv->samplingrate,
137 uc_priv->channels)) {
138 /* Ignore any error here - we'll just have no sound */
145 static int sandbox_sound_setup(struct udevice *dev)
147 struct sandbox_sound_priv *priv = dev_get_priv(dev);
149 priv->setup_called++;
154 static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
156 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
157 struct sandbox_sound_priv *priv = dev_get_priv(dev);
160 for (i = 0; i < data_size; i++)
161 priv->sum += ((uint8_t *)data)[i];
163 return i2s_tx_data(uc_priv->i2s, data, data_size);
166 int sandbox_sound_start_beep(struct udevice *dev, int frequency_hz)
168 struct sandbox_sound_priv *priv = dev_get_priv(dev);
170 if (!priv->allow_beep)
172 priv->frequency_hz = frequency_hz;
177 int sandbox_sound_stop_beep(struct udevice *dev)
179 struct sandbox_sound_priv *priv = dev_get_priv(dev);
181 if (!priv->allow_beep)
183 priv->frequency_hz = 0;
188 static int sandbox_sound_probe(struct udevice *dev)
190 return sound_find_codec_i2s(dev);
193 static const struct audio_codec_ops sandbox_codec_ops = {
194 .set_params = sandbox_codec_set_params,
197 static const struct udevice_id sandbox_codec_ids[] = {
198 { .compatible = "sandbox,audio-codec" },
202 U_BOOT_DRIVER(sandbox_codec) = {
203 .name = "sandbox_codec",
204 .id = UCLASS_AUDIO_CODEC,
205 .of_match = sandbox_codec_ids,
206 .ops = &sandbox_codec_ops,
207 .priv_auto_alloc_size = sizeof(struct sandbox_codec_priv),
210 static const struct i2s_ops sandbox_i2s_ops = {
211 .tx_data = sandbox_i2s_tx_data,
214 static const struct udevice_id sandbox_i2s_ids[] = {
215 { .compatible = "sandbox,i2s" },
219 U_BOOT_DRIVER(sandbox_i2s) = {
220 .name = "sandbox_i2s",
222 .of_match = sandbox_i2s_ids,
223 .ops = &sandbox_i2s_ops,
224 .probe = sandbox_i2s_probe,
225 .priv_auto_alloc_size = sizeof(struct sandbox_i2s_priv),
228 static const struct sound_ops sandbox_sound_ops = {
229 .setup = sandbox_sound_setup,
230 .play = sandbox_sound_play,
231 .start_beep = sandbox_sound_start_beep,
232 .stop_beep = sandbox_sound_stop_beep,
235 static const struct udevice_id sandbox_sound_ids[] = {
236 { .compatible = "sandbox,sound" },
240 U_BOOT_DRIVER(sandbox_sound) = {
241 .name = "sandbox_sound",
243 .of_match = sandbox_sound_ids,
244 .ops = &sandbox_sound_ops,
245 .priv_auto_alloc_size = sizeof(struct sandbox_sound_priv),
246 .probe = sandbox_sound_probe,