blob: 31ae153530e8832a185dc4dcd2bf270c7c3f67c9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass279fc162014-02-27 13:26:18 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glass279fc162014-02-27 13:26:18 -07004 */
5
Simon Glass4d5814c2019-02-16 20:24:56 -07006#define LOG_CATEGORY UCLASS_SOUND
7
Simon Glassed96cde2018-12-10 10:37:33 -07008#include <audio_codec.h>
Simon Glassc953aaf2018-12-10 10:37:34 -07009#include <dm.h>
10#include <i2s.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass76072ac2018-12-10 10:37:36 -070012#include <sound.h>
Simon Glass279fc162014-02-27 13:26:18 -070013#include <asm/sdl.h>
14
Simon Glassed96cde2018-12-10 10:37:33 -070015struct sandbox_codec_priv {
16 int interface;
17 int rate;
18 int mclk_freq;
19 int bits_per_sample;
20 uint channels;
21};
22
Simon Glassc953aaf2018-12-10 10:37:34 -070023struct sandbox_i2s_priv {
24 int sum; /* Use to sum the provided audio data */
Simon Glass4d5814c2019-02-16 20:24:56 -070025 bool silent; /* Sound is silent, don't use SDL */
Simon Glassc953aaf2018-12-10 10:37:34 -070026};
27
Simon Glass76072ac2018-12-10 10:37:36 -070028struct sandbox_sound_priv {
Simon Glass70bc14b2020-02-03 07:36:06 -070029 int setup_called; /* Incremented when setup() method is called */
30 bool active; /* TX data is being sent */
Heinrich Schuchardt707a6fc2022-12-04 17:11:41 +010031 int count; /* Use to count the provided audio data */
Simon Glassecd02e72019-02-16 20:24:54 -070032 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 */
Simon Glass76072ac2018-12-10 10:37:36 -070035};
36
Simon Glassed96cde2018-12-10 10:37:33 -070037void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
38 int *mclk_freqp, int *bits_per_samplep,
39 uint *channelsp)
40{
41 struct sandbox_codec_priv *priv = dev_get_priv(dev);
42
43 *interfacep = priv->interface;
44 *ratep = priv->rate;
45 *mclk_freqp = priv->mclk_freq;
46 *bits_per_samplep = priv->bits_per_sample;
47 *channelsp = priv->channels;
48}
49
Simon Glassc953aaf2018-12-10 10:37:34 -070050int sandbox_get_i2s_sum(struct udevice *dev)
51{
52 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
53
54 return priv->sum;
55}
56
Simon Glass76072ac2018-12-10 10:37:36 -070057int sandbox_get_setup_called(struct udevice *dev)
58{
59 struct sandbox_sound_priv *priv = dev_get_priv(dev);
60
61 return priv->setup_called;
62}
63
Simon Glass70bc14b2020-02-03 07:36:06 -070064int sandbox_get_sound_active(struct udevice *dev)
65{
66 struct sandbox_sound_priv *priv = dev_get_priv(dev);
67
68 return priv->active;
69}
70
Heinrich Schuchardt707a6fc2022-12-04 17:11:41 +010071int sandbox_get_sound_count(struct udevice *dev)
72{
73 struct sandbox_sound_priv *priv = dev_get_priv(dev);
74
75 return priv->count;
76}
77
Simon Glass76072ac2018-12-10 10:37:36 -070078int sandbox_get_sound_sum(struct udevice *dev)
79{
80 struct sandbox_sound_priv *priv = dev_get_priv(dev);
81
82 return priv->sum;
83}
84
Simon Glassecd02e72019-02-16 20:24:54 -070085void sandbox_set_allow_beep(struct udevice *dev, bool allow)
86{
87 struct sandbox_sound_priv *priv = dev_get_priv(dev);
88
89 priv->allow_beep = allow;
90}
91
92int sandbox_get_beep_frequency(struct udevice *dev)
93{
94 struct sandbox_sound_priv *priv = dev_get_priv(dev);
95
96 return priv->frequency_hz;
97}
98
Simon Glassed96cde2018-12-10 10:37:33 -070099static int sandbox_codec_set_params(struct udevice *dev, int interface,
100 int rate, int mclk_freq,
101 int bits_per_sample, uint channels)
102{
103 struct sandbox_codec_priv *priv = dev_get_priv(dev);
104
105 priv->interface = interface;
106 priv->rate = rate;
107 priv->mclk_freq = mclk_freq;
108 priv->bits_per_sample = bits_per_sample;
109 priv->channels = channels;
110
111 return 0;
112}
113
Simon Glassc953aaf2018-12-10 10:37:34 -0700114static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
115 uint data_size)
116{
117 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
118 int i;
119
120 for (i = 0; i < data_size; i++)
121 priv->sum += ((uint8_t *)data)[i];
122
Simon Glass4d5814c2019-02-16 20:24:56 -0700123 if (!priv->silent) {
124 int ret;
125
126 ret = sandbox_sdl_sound_play(data, data_size);
127 if (ret)
128 return ret;
129 }
130
131 return 0;
Simon Glassc953aaf2018-12-10 10:37:34 -0700132}
133
134static int sandbox_i2s_probe(struct udevice *dev)
135{
136 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass4d5814c2019-02-16 20:24:56 -0700137 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
Simon Glassc953aaf2018-12-10 10:37:34 -0700138
139 /* Use hard-coded values here */
140 uc_priv->rfs = 256;
141 uc_priv->bfs = 32;
142 uc_priv->audio_pll_clk = 192000000;
143 uc_priv->samplingrate = 48000;
144 uc_priv->bitspersample = 16;
145 uc_priv->channels = 2;
146 uc_priv->id = 1;
147
Simon Glass4d5814c2019-02-16 20:24:56 -0700148 priv->silent = dev_read_bool(dev, "sandbox,silent");
149
150 if (priv->silent) {
151 log_warning("Sound is silenced\n");
152 } else if (sandbox_sdl_sound_init(uc_priv->samplingrate,
153 uc_priv->channels)) {
154 /* Ignore any error here - we'll just have no sound */
155 priv->silent = true;
156 }
Simon Glass4070ba62018-12-10 10:37:39 -0700157
158 return 0;
Simon Glass76072ac2018-12-10 10:37:36 -0700159}
160
161static int sandbox_sound_setup(struct udevice *dev)
162{
163 struct sandbox_sound_priv *priv = dev_get_priv(dev);
164
165 priv->setup_called++;
166
Simon Glassc953aaf2018-12-10 10:37:34 -0700167 return 0;
168}
169
Simon Glass76072ac2018-12-10 10:37:36 -0700170static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
171{
172 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
173 struct sandbox_sound_priv *priv = dev_get_priv(dev);
174 int i;
175
176 for (i = 0; i < data_size; i++)
177 priv->sum += ((uint8_t *)data)[i];
Heinrich Schuchardt707a6fc2022-12-04 17:11:41 +0100178 priv->count += data_size;
Simon Glass76072ac2018-12-10 10:37:36 -0700179
180 return i2s_tx_data(uc_priv->i2s, data, data_size);
181}
182
Simon Glass70bc14b2020-02-03 07:36:06 -0700183static int sandbox_sound_stop_play(struct udevice *dev)
184{
185 struct sandbox_sound_priv *priv = dev_get_priv(dev);
186
187 sandbox_sdl_sound_stop();
188 priv->active = false;
189
190 return 0;
191}
192
Simon Glassecd02e72019-02-16 20:24:54 -0700193int sandbox_sound_start_beep(struct udevice *dev, int frequency_hz)
194{
195 struct sandbox_sound_priv *priv = dev_get_priv(dev);
196
197 if (!priv->allow_beep)
198 return -ENOSYS;
199 priv->frequency_hz = frequency_hz;
200
201 return 0;
202}
203
204int sandbox_sound_stop_beep(struct udevice *dev)
205{
206 struct sandbox_sound_priv *priv = dev_get_priv(dev);
207
208 if (!priv->allow_beep)
209 return -ENOSYS;
210 priv->frequency_hz = 0;
211
212 return 0;
213}
214
Simon Glass76072ac2018-12-10 10:37:36 -0700215static int sandbox_sound_probe(struct udevice *dev)
216{
217 return sound_find_codec_i2s(dev);
218}
219
Simon Glassed96cde2018-12-10 10:37:33 -0700220static const struct audio_codec_ops sandbox_codec_ops = {
221 .set_params = sandbox_codec_set_params,
222};
223
224static const struct udevice_id sandbox_codec_ids[] = {
225 { .compatible = "sandbox,audio-codec" },
226 { }
227};
228
229U_BOOT_DRIVER(sandbox_codec) = {
230 .name = "sandbox_codec",
231 .id = UCLASS_AUDIO_CODEC,
232 .of_match = sandbox_codec_ids,
233 .ops = &sandbox_codec_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700234 .priv_auto = sizeof(struct sandbox_codec_priv),
Simon Glassed96cde2018-12-10 10:37:33 -0700235};
Simon Glassc953aaf2018-12-10 10:37:34 -0700236
237static const struct i2s_ops sandbox_i2s_ops = {
238 .tx_data = sandbox_i2s_tx_data,
239};
240
241static const struct udevice_id sandbox_i2s_ids[] = {
242 { .compatible = "sandbox,i2s" },
243 { }
244};
245
246U_BOOT_DRIVER(sandbox_i2s) = {
247 .name = "sandbox_i2s",
248 .id = UCLASS_I2S,
249 .of_match = sandbox_i2s_ids,
250 .ops = &sandbox_i2s_ops,
251 .probe = sandbox_i2s_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700252 .priv_auto = sizeof(struct sandbox_i2s_priv),
Simon Glassc953aaf2018-12-10 10:37:34 -0700253};
Simon Glass76072ac2018-12-10 10:37:36 -0700254
255static const struct sound_ops sandbox_sound_ops = {
Simon Glassecd02e72019-02-16 20:24:54 -0700256 .setup = sandbox_sound_setup,
257 .play = sandbox_sound_play,
Simon Glass70bc14b2020-02-03 07:36:06 -0700258 .stop_play = sandbox_sound_stop_play,
Simon Glassecd02e72019-02-16 20:24:54 -0700259 .start_beep = sandbox_sound_start_beep,
260 .stop_beep = sandbox_sound_stop_beep,
Simon Glass76072ac2018-12-10 10:37:36 -0700261};
262
263static const struct udevice_id sandbox_sound_ids[] = {
264 { .compatible = "sandbox,sound" },
265 { }
266};
267
268U_BOOT_DRIVER(sandbox_sound) = {
269 .name = "sandbox_sound",
270 .id = UCLASS_SOUND,
271 .of_match = sandbox_sound_ids,
272 .ops = &sandbox_sound_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700273 .priv_auto = sizeof(struct sandbox_sound_priv),
Simon Glass76072ac2018-12-10 10:37:36 -0700274 .probe = sandbox_sound_probe,
275};