1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
7 #include <audio_codec.h>
13 struct sandbox_codec_priv {
21 struct sandbox_i2s_priv {
22 int sum; /* Use to sum the provided audio data */
25 struct sandbox_sound_priv {
27 int sum; /* Use to sum the provided audio data */
30 void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
31 int *mclk_freqp, int *bits_per_samplep,
34 struct sandbox_codec_priv *priv = dev_get_priv(dev);
36 *interfacep = priv->interface;
38 *mclk_freqp = priv->mclk_freq;
39 *bits_per_samplep = priv->bits_per_sample;
40 *channelsp = priv->channels;
43 int sandbox_get_i2s_sum(struct udevice *dev)
45 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
50 int sandbox_get_setup_called(struct udevice *dev)
52 struct sandbox_sound_priv *priv = dev_get_priv(dev);
54 return priv->setup_called;
57 int sandbox_get_sound_sum(struct udevice *dev)
59 struct sandbox_sound_priv *priv = dev_get_priv(dev);
64 static int sandbox_codec_set_params(struct udevice *dev, int interface,
65 int rate, int mclk_freq,
66 int bits_per_sample, uint channels)
68 struct sandbox_codec_priv *priv = dev_get_priv(dev);
70 priv->interface = interface;
72 priv->mclk_freq = mclk_freq;
73 priv->bits_per_sample = bits_per_sample;
74 priv->channels = channels;
79 static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
82 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
85 for (i = 0; i < data_size; i++)
86 priv->sum += ((uint8_t *)data)[i];
88 return sandbox_sdl_sound_play(data, data_size);
91 static int sandbox_i2s_probe(struct udevice *dev)
93 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
95 /* Use hard-coded values here */
98 uc_priv->audio_pll_clk = 192000000;
99 uc_priv->samplingrate = 48000;
100 uc_priv->bitspersample = 16;
101 uc_priv->channels = 2;
104 /* Ignore any error here - we'll just have no sound */
105 sandbox_sdl_sound_init(uc_priv->samplingrate, uc_priv->channels);
110 static int sandbox_sound_setup(struct udevice *dev)
112 struct sandbox_sound_priv *priv = dev_get_priv(dev);
114 priv->setup_called++;
119 static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
121 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
122 struct sandbox_sound_priv *priv = dev_get_priv(dev);
125 for (i = 0; i < data_size; i++)
126 priv->sum += ((uint8_t *)data)[i];
128 return i2s_tx_data(uc_priv->i2s, data, data_size);
131 static int sandbox_sound_probe(struct udevice *dev)
133 return sound_find_codec_i2s(dev);
136 static const struct audio_codec_ops sandbox_codec_ops = {
137 .set_params = sandbox_codec_set_params,
140 static const struct udevice_id sandbox_codec_ids[] = {
141 { .compatible = "sandbox,audio-codec" },
145 U_BOOT_DRIVER(sandbox_codec) = {
146 .name = "sandbox_codec",
147 .id = UCLASS_AUDIO_CODEC,
148 .of_match = sandbox_codec_ids,
149 .ops = &sandbox_codec_ops,
150 .priv_auto_alloc_size = sizeof(struct sandbox_codec_priv),
153 static const struct i2s_ops sandbox_i2s_ops = {
154 .tx_data = sandbox_i2s_tx_data,
157 static const struct udevice_id sandbox_i2s_ids[] = {
158 { .compatible = "sandbox,i2s" },
162 U_BOOT_DRIVER(sandbox_i2s) = {
163 .name = "sandbox_i2s",
165 .of_match = sandbox_i2s_ids,
166 .ops = &sandbox_i2s_ops,
167 .probe = sandbox_i2s_probe,
168 .priv_auto_alloc_size = sizeof(struct sandbox_i2s_priv),
171 static const struct sound_ops sandbox_sound_ops = {
172 .setup = sandbox_sound_setup,
173 .play = sandbox_sound_play,
176 static const struct udevice_id sandbox_sound_ids[] = {
177 { .compatible = "sandbox,sound" },
181 U_BOOT_DRIVER(sandbox_sound) = {
182 .name = "sandbox_sound",
184 .of_match = sandbox_sound_ids,
185 .ops = &sandbox_sound_ops,
186 .priv_auto_alloc_size = sizeof(struct sandbox_sound_priv),
187 .probe = sandbox_sound_probe,