1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
7 #include <audio_codec.h>
11 #include <asm/sound.h>
14 struct sandbox_codec_priv {
22 struct sandbox_i2s_priv {
23 int sum; /* Use to sum the provided audio data */
26 struct sandbox_sound_priv {
28 int sum; /* Use to sum the provided audio data */
31 #ifndef CONFIG_DM_SOUND
32 int sound_play(uint32_t msec, uint32_t frequency)
34 sandbox_sdl_sound_start(frequency);
36 sandbox_sdl_sound_stop();
40 #endif /* CONFIG_DM_SOUND */
42 int sound_init(const void *blob)
44 return sandbox_sdl_sound_init();
47 void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
48 int *mclk_freqp, int *bits_per_samplep,
51 struct sandbox_codec_priv *priv = dev_get_priv(dev);
53 *interfacep = priv->interface;
55 *mclk_freqp = priv->mclk_freq;
56 *bits_per_samplep = priv->bits_per_sample;
57 *channelsp = priv->channels;
60 int sandbox_get_i2s_sum(struct udevice *dev)
62 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
67 int sandbox_get_setup_called(struct udevice *dev)
69 struct sandbox_sound_priv *priv = dev_get_priv(dev);
71 return priv->setup_called;
74 int sandbox_get_sound_sum(struct udevice *dev)
76 struct sandbox_sound_priv *priv = dev_get_priv(dev);
81 static int sandbox_codec_set_params(struct udevice *dev, int interface,
82 int rate, int mclk_freq,
83 int bits_per_sample, uint channels)
85 struct sandbox_codec_priv *priv = dev_get_priv(dev);
87 priv->interface = interface;
89 priv->mclk_freq = mclk_freq;
90 priv->bits_per_sample = bits_per_sample;
91 priv->channels = channels;
96 static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
99 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
102 for (i = 0; i < data_size; i++)
103 priv->sum += ((uint8_t *)data)[i];
108 static int sandbox_i2s_probe(struct udevice *dev)
110 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
112 /* Use hard-coded values here */
115 uc_priv->audio_pll_clk = 192000000;
116 uc_priv->samplingrate = 48000;
117 uc_priv->bitspersample = 16;
118 uc_priv->channels = 2;
121 /* Ignore any error here - we'll just have no sound */
122 sandbox_sdl_sound_init();
127 static int sandbox_sound_setup(struct udevice *dev)
129 struct sandbox_sound_priv *priv = dev_get_priv(dev);
131 priv->setup_called++;
136 static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
138 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
139 struct sandbox_sound_priv *priv = dev_get_priv(dev);
142 for (i = 0; i < data_size; i++)
143 priv->sum += ((uint8_t *)data)[i];
145 return i2s_tx_data(uc_priv->i2s, data, data_size);
148 static int sandbox_sound_probe(struct udevice *dev)
150 return sound_find_codec_i2s(dev);
153 static const struct audio_codec_ops sandbox_codec_ops = {
154 .set_params = sandbox_codec_set_params,
157 static const struct udevice_id sandbox_codec_ids[] = {
158 { .compatible = "sandbox,audio-codec" },
162 U_BOOT_DRIVER(sandbox_codec) = {
163 .name = "sandbox_codec",
164 .id = UCLASS_AUDIO_CODEC,
165 .of_match = sandbox_codec_ids,
166 .ops = &sandbox_codec_ops,
167 .priv_auto_alloc_size = sizeof(struct sandbox_codec_priv),
170 static const struct i2s_ops sandbox_i2s_ops = {
171 .tx_data = sandbox_i2s_tx_data,
174 static const struct udevice_id sandbox_i2s_ids[] = {
175 { .compatible = "sandbox,i2s" },
179 U_BOOT_DRIVER(sandbox_i2s) = {
180 .name = "sandbox_i2s",
182 .of_match = sandbox_i2s_ids,
183 .ops = &sandbox_i2s_ops,
184 .probe = sandbox_i2s_probe,
185 .priv_auto_alloc_size = sizeof(struct sandbox_i2s_priv),
188 static const struct sound_ops sandbox_sound_ops = {
189 .setup = sandbox_sound_setup,
190 .play = sandbox_sound_play,
193 static const struct udevice_id sandbox_sound_ids[] = {
194 { .compatible = "sandbox,sound" },
198 U_BOOT_DRIVER(sandbox_sound) = {
199 .name = "sandbox_sound",
201 .of_match = sandbox_sound_ids,
202 .ops = &sandbox_sound_ops,
203 .priv_auto_alloc_size = sizeof(struct sandbox_sound_priv),
204 .probe = sandbox_sound_probe,