blob: 4a2c87a84c6857ed8aed7da0f83e6f29bcdc518d [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 Glass279fc162014-02-27 13:26:18 -07008#include <common.h>
Simon Glassed96cde2018-12-10 10:37:33 -07009#include <audio_codec.h>
Simon Glassc953aaf2018-12-10 10:37:34 -070010#include <dm.h>
11#include <i2s.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass76072ac2018-12-10 10:37:36 -070013#include <sound.h>
Simon Glass279fc162014-02-27 13:26:18 -070014#include <asm/sdl.h>
15
Simon Glassed96cde2018-12-10 10:37:33 -070016struct sandbox_codec_priv {
17 int interface;
18 int rate;
19 int mclk_freq;
20 int bits_per_sample;
21 uint channels;
22};
23
Simon Glassc953aaf2018-12-10 10:37:34 -070024struct sandbox_i2s_priv {
25 int sum; /* Use to sum the provided audio data */
Simon Glass4d5814c2019-02-16 20:24:56 -070026 bool silent; /* Sound is silent, don't use SDL */
Simon Glassc953aaf2018-12-10 10:37:34 -070027};
28
Simon Glass76072ac2018-12-10 10:37:36 -070029struct sandbox_sound_priv {
Simon Glass70bc14b2020-02-03 07:36:06 -070030 int setup_called; /* Incremented when setup() method is called */
31 bool active; /* TX data is being sent */
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
Simon Glass76072ac2018-12-10 10:37:36 -070071int sandbox_get_sound_sum(struct udevice *dev)
72{
73 struct sandbox_sound_priv *priv = dev_get_priv(dev);
74
75 return priv->sum;
76}
77
Simon Glassecd02e72019-02-16 20:24:54 -070078void sandbox_set_allow_beep(struct udevice *dev, bool allow)
79{
80 struct sandbox_sound_priv *priv = dev_get_priv(dev);
81
82 priv->allow_beep = allow;
83}
84
85int sandbox_get_beep_frequency(struct udevice *dev)
86{
87 struct sandbox_sound_priv *priv = dev_get_priv(dev);
88
89 return priv->frequency_hz;
90}
91
Simon Glassed96cde2018-12-10 10:37:33 -070092static int sandbox_codec_set_params(struct udevice *dev, int interface,
93 int rate, int mclk_freq,
94 int bits_per_sample, uint channels)
95{
96 struct sandbox_codec_priv *priv = dev_get_priv(dev);
97
98 priv->interface = interface;
99 priv->rate = rate;
100 priv->mclk_freq = mclk_freq;
101 priv->bits_per_sample = bits_per_sample;
102 priv->channels = channels;
103
104 return 0;
105}
106
Simon Glassc953aaf2018-12-10 10:37:34 -0700107static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
108 uint data_size)
109{
110 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
111 int i;
112
113 for (i = 0; i < data_size; i++)
114 priv->sum += ((uint8_t *)data)[i];
115
Simon Glass4d5814c2019-02-16 20:24:56 -0700116 if (!priv->silent) {
117 int ret;
118
119 ret = sandbox_sdl_sound_play(data, data_size);
120 if (ret)
121 return ret;
122 }
123
124 return 0;
Simon Glassc953aaf2018-12-10 10:37:34 -0700125}
126
127static int sandbox_i2s_probe(struct udevice *dev)
128{
129 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass4d5814c2019-02-16 20:24:56 -0700130 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
Simon Glassc953aaf2018-12-10 10:37:34 -0700131
132 /* Use hard-coded values here */
133 uc_priv->rfs = 256;
134 uc_priv->bfs = 32;
135 uc_priv->audio_pll_clk = 192000000;
136 uc_priv->samplingrate = 48000;
137 uc_priv->bitspersample = 16;
138 uc_priv->channels = 2;
139 uc_priv->id = 1;
140
Simon Glass4d5814c2019-02-16 20:24:56 -0700141 priv->silent = dev_read_bool(dev, "sandbox,silent");
142
143 if (priv->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 */
148 priv->silent = true;
149 }
Simon Glass4070ba62018-12-10 10:37:39 -0700150
151 return 0;
Simon Glass76072ac2018-12-10 10:37:36 -0700152}
153
154static int sandbox_sound_setup(struct udevice *dev)
155{
156 struct sandbox_sound_priv *priv = dev_get_priv(dev);
157
158 priv->setup_called++;
159
Simon Glassc953aaf2018-12-10 10:37:34 -0700160 return 0;
161}
162
Simon Glass76072ac2018-12-10 10:37:36 -0700163static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
164{
165 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
166 struct sandbox_sound_priv *priv = dev_get_priv(dev);
167 int i;
168
169 for (i = 0; i < data_size; i++)
170 priv->sum += ((uint8_t *)data)[i];
171
172 return i2s_tx_data(uc_priv->i2s, data, data_size);
173}
174
Simon Glass70bc14b2020-02-03 07:36:06 -0700175static int sandbox_sound_stop_play(struct udevice *dev)
176{
177 struct sandbox_sound_priv *priv = dev_get_priv(dev);
178
179 sandbox_sdl_sound_stop();
180 priv->active = false;
181
182 return 0;
183}
184
Simon Glassecd02e72019-02-16 20:24:54 -0700185int sandbox_sound_start_beep(struct udevice *dev, int frequency_hz)
186{
187 struct sandbox_sound_priv *priv = dev_get_priv(dev);
188
189 if (!priv->allow_beep)
190 return -ENOSYS;
191 priv->frequency_hz = frequency_hz;
192
193 return 0;
194}
195
196int sandbox_sound_stop_beep(struct udevice *dev)
197{
198 struct sandbox_sound_priv *priv = dev_get_priv(dev);
199
200 if (!priv->allow_beep)
201 return -ENOSYS;
202 priv->frequency_hz = 0;
203
204 return 0;
205}
206
Simon Glass76072ac2018-12-10 10:37:36 -0700207static int sandbox_sound_probe(struct udevice *dev)
208{
209 return sound_find_codec_i2s(dev);
210}
211
Simon Glassed96cde2018-12-10 10:37:33 -0700212static const struct audio_codec_ops sandbox_codec_ops = {
213 .set_params = sandbox_codec_set_params,
214};
215
216static const struct udevice_id sandbox_codec_ids[] = {
217 { .compatible = "sandbox,audio-codec" },
218 { }
219};
220
221U_BOOT_DRIVER(sandbox_codec) = {
222 .name = "sandbox_codec",
223 .id = UCLASS_AUDIO_CODEC,
224 .of_match = sandbox_codec_ids,
225 .ops = &sandbox_codec_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700226 .priv_auto = sizeof(struct sandbox_codec_priv),
Simon Glassed96cde2018-12-10 10:37:33 -0700227};
Simon Glassc953aaf2018-12-10 10:37:34 -0700228
229static const struct i2s_ops sandbox_i2s_ops = {
230 .tx_data = sandbox_i2s_tx_data,
231};
232
233static const struct udevice_id sandbox_i2s_ids[] = {
234 { .compatible = "sandbox,i2s" },
235 { }
236};
237
238U_BOOT_DRIVER(sandbox_i2s) = {
239 .name = "sandbox_i2s",
240 .id = UCLASS_I2S,
241 .of_match = sandbox_i2s_ids,
242 .ops = &sandbox_i2s_ops,
243 .probe = sandbox_i2s_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700244 .priv_auto = sizeof(struct sandbox_i2s_priv),
Simon Glassc953aaf2018-12-10 10:37:34 -0700245};
Simon Glass76072ac2018-12-10 10:37:36 -0700246
247static const struct sound_ops sandbox_sound_ops = {
Simon Glassecd02e72019-02-16 20:24:54 -0700248 .setup = sandbox_sound_setup,
249 .play = sandbox_sound_play,
Simon Glass70bc14b2020-02-03 07:36:06 -0700250 .stop_play = sandbox_sound_stop_play,
Simon Glassecd02e72019-02-16 20:24:54 -0700251 .start_beep = sandbox_sound_start_beep,
252 .stop_beep = sandbox_sound_stop_beep,
Simon Glass76072ac2018-12-10 10:37:36 -0700253};
254
255static const struct udevice_id sandbox_sound_ids[] = {
256 { .compatible = "sandbox,sound" },
257 { }
258};
259
260U_BOOT_DRIVER(sandbox_sound) = {
261 .name = "sandbox_sound",
262 .id = UCLASS_SOUND,
263 .of_match = sandbox_sound_ids,
264 .ops = &sandbox_sound_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700265 .priv_auto = sizeof(struct sandbox_sound_priv),
Simon Glass76072ac2018-12-10 10:37:36 -0700266 .probe = sandbox_sound_probe,
267};