blob: 9034a8385a8c78b1e868070585a5499728d662ca [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc34c0242014-02-27 13:26:18 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glassc34c0242014-02-27 13:26:18 -07004 */
5
Simon Glassecc79732019-02-16 20:24:56 -07006#define LOG_CATEGORY UCLASS_SOUND
7
Simon Glassc34c0242014-02-27 13:26:18 -07008#include <common.h>
Simon Glassce6d99a2018-12-10 10:37:33 -07009#include <audio_codec.h>
Simon Glasse96fa6c2018-12-10 10:37:34 -070010#include <dm.h>
11#include <i2s.h>
Simon Glassd4901892018-12-10 10:37:36 -070012#include <sound.h>
Simon Glassc34c0242014-02-27 13:26:18 -070013#include <asm/sdl.h>
14
Simon Glassce6d99a2018-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 Glasse96fa6c2018-12-10 10:37:34 -070023struct sandbox_i2s_priv {
24 int sum; /* Use to sum the provided audio data */
Simon Glassecc79732019-02-16 20:24:56 -070025 bool silent; /* Sound is silent, don't use SDL */
Simon Glasse96fa6c2018-12-10 10:37:34 -070026};
27
Simon Glassd4901892018-12-10 10:37:36 -070028struct sandbox_sound_priv {
Simon Glass3062cd12020-02-03 07:36:06 -070029 int setup_called; /* Incremented when setup() method is called */
30 bool active; /* TX data is being sent */
Simon Glass28502662019-02-16 20:24:54 -070031 int sum; /* Use to sum the provided audio data */
32 bool allow_beep; /* true to allow the start_beep() interface */
33 int frequency_hz; /* Beep frequency if active, else 0 */
Simon Glassd4901892018-12-10 10:37:36 -070034};
35
Simon Glassce6d99a2018-12-10 10:37:33 -070036void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
37 int *mclk_freqp, int *bits_per_samplep,
38 uint *channelsp)
39{
40 struct sandbox_codec_priv *priv = dev_get_priv(dev);
41
42 *interfacep = priv->interface;
43 *ratep = priv->rate;
44 *mclk_freqp = priv->mclk_freq;
45 *bits_per_samplep = priv->bits_per_sample;
46 *channelsp = priv->channels;
47}
48
Simon Glasse96fa6c2018-12-10 10:37:34 -070049int sandbox_get_i2s_sum(struct udevice *dev)
50{
51 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
52
53 return priv->sum;
54}
55
Simon Glassd4901892018-12-10 10:37:36 -070056int sandbox_get_setup_called(struct udevice *dev)
57{
58 struct sandbox_sound_priv *priv = dev_get_priv(dev);
59
60 return priv->setup_called;
61}
62
Simon Glass3062cd12020-02-03 07:36:06 -070063int sandbox_get_sound_active(struct udevice *dev)
64{
65 struct sandbox_sound_priv *priv = dev_get_priv(dev);
66
67 return priv->active;
68}
69
Simon Glassd4901892018-12-10 10:37:36 -070070int sandbox_get_sound_sum(struct udevice *dev)
71{
72 struct sandbox_sound_priv *priv = dev_get_priv(dev);
73
74 return priv->sum;
75}
76
Simon Glass28502662019-02-16 20:24:54 -070077void sandbox_set_allow_beep(struct udevice *dev, bool allow)
78{
79 struct sandbox_sound_priv *priv = dev_get_priv(dev);
80
81 priv->allow_beep = allow;
82}
83
84int sandbox_get_beep_frequency(struct udevice *dev)
85{
86 struct sandbox_sound_priv *priv = dev_get_priv(dev);
87
88 return priv->frequency_hz;
89}
90
Simon Glassce6d99a2018-12-10 10:37:33 -070091static int sandbox_codec_set_params(struct udevice *dev, int interface,
92 int rate, int mclk_freq,
93 int bits_per_sample, uint channels)
94{
95 struct sandbox_codec_priv *priv = dev_get_priv(dev);
96
97 priv->interface = interface;
98 priv->rate = rate;
99 priv->mclk_freq = mclk_freq;
100 priv->bits_per_sample = bits_per_sample;
101 priv->channels = channels;
102
103 return 0;
104}
105
Simon Glasse96fa6c2018-12-10 10:37:34 -0700106static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
107 uint data_size)
108{
109 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
110 int i;
111
112 for (i = 0; i < data_size; i++)
113 priv->sum += ((uint8_t *)data)[i];
114
Simon Glassecc79732019-02-16 20:24:56 -0700115 if (!priv->silent) {
116 int ret;
117
118 ret = sandbox_sdl_sound_play(data, data_size);
119 if (ret)
120 return ret;
121 }
122
123 return 0;
Simon Glasse96fa6c2018-12-10 10:37:34 -0700124}
125
126static int sandbox_i2s_probe(struct udevice *dev)
127{
128 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassecc79732019-02-16 20:24:56 -0700129 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
Simon Glasse96fa6c2018-12-10 10:37:34 -0700130
131 /* Use hard-coded values here */
132 uc_priv->rfs = 256;
133 uc_priv->bfs = 32;
134 uc_priv->audio_pll_clk = 192000000;
135 uc_priv->samplingrate = 48000;
136 uc_priv->bitspersample = 16;
137 uc_priv->channels = 2;
138 uc_priv->id = 1;
139
Simon Glassecc79732019-02-16 20:24:56 -0700140 priv->silent = dev_read_bool(dev, "sandbox,silent");
141
142 if (priv->silent) {
143 log_warning("Sound is silenced\n");
144 } else if (sandbox_sdl_sound_init(uc_priv->samplingrate,
145 uc_priv->channels)) {
146 /* Ignore any error here - we'll just have no sound */
147 priv->silent = true;
148 }
Simon Glassd6cadd52018-12-10 10:37:39 -0700149
150 return 0;
Simon Glassd4901892018-12-10 10:37:36 -0700151}
152
153static int sandbox_sound_setup(struct udevice *dev)
154{
155 struct sandbox_sound_priv *priv = dev_get_priv(dev);
156
157 priv->setup_called++;
158
Simon Glasse96fa6c2018-12-10 10:37:34 -0700159 return 0;
160}
161
Simon Glassd4901892018-12-10 10:37:36 -0700162static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
163{
164 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
165 struct sandbox_sound_priv *priv = dev_get_priv(dev);
166 int i;
167
168 for (i = 0; i < data_size; i++)
169 priv->sum += ((uint8_t *)data)[i];
170
171 return i2s_tx_data(uc_priv->i2s, data, data_size);
172}
173
Simon Glass3062cd12020-02-03 07:36:06 -0700174static int sandbox_sound_stop_play(struct udevice *dev)
175{
176 struct sandbox_sound_priv *priv = dev_get_priv(dev);
177
178 sandbox_sdl_sound_stop();
179 priv->active = false;
180
181 return 0;
182}
183
Simon Glass28502662019-02-16 20:24:54 -0700184int sandbox_sound_start_beep(struct udevice *dev, int frequency_hz)
185{
186 struct sandbox_sound_priv *priv = dev_get_priv(dev);
187
188 if (!priv->allow_beep)
189 return -ENOSYS;
190 priv->frequency_hz = frequency_hz;
191
192 return 0;
193}
194
195int sandbox_sound_stop_beep(struct udevice *dev)
196{
197 struct sandbox_sound_priv *priv = dev_get_priv(dev);
198
199 if (!priv->allow_beep)
200 return -ENOSYS;
201 priv->frequency_hz = 0;
202
203 return 0;
204}
205
Simon Glassd4901892018-12-10 10:37:36 -0700206static int sandbox_sound_probe(struct udevice *dev)
207{
208 return sound_find_codec_i2s(dev);
209}
210
Simon Glassce6d99a2018-12-10 10:37:33 -0700211static const struct audio_codec_ops sandbox_codec_ops = {
212 .set_params = sandbox_codec_set_params,
213};
214
215static const struct udevice_id sandbox_codec_ids[] = {
216 { .compatible = "sandbox,audio-codec" },
217 { }
218};
219
220U_BOOT_DRIVER(sandbox_codec) = {
221 .name = "sandbox_codec",
222 .id = UCLASS_AUDIO_CODEC,
223 .of_match = sandbox_codec_ids,
224 .ops = &sandbox_codec_ops,
225 .priv_auto_alloc_size = sizeof(struct sandbox_codec_priv),
226};
Simon Glasse96fa6c2018-12-10 10:37:34 -0700227
228static const struct i2s_ops sandbox_i2s_ops = {
229 .tx_data = sandbox_i2s_tx_data,
230};
231
232static const struct udevice_id sandbox_i2s_ids[] = {
233 { .compatible = "sandbox,i2s" },
234 { }
235};
236
237U_BOOT_DRIVER(sandbox_i2s) = {
238 .name = "sandbox_i2s",
239 .id = UCLASS_I2S,
240 .of_match = sandbox_i2s_ids,
241 .ops = &sandbox_i2s_ops,
242 .probe = sandbox_i2s_probe,
243 .priv_auto_alloc_size = sizeof(struct sandbox_i2s_priv),
244};
Simon Glassd4901892018-12-10 10:37:36 -0700245
246static const struct sound_ops sandbox_sound_ops = {
Simon Glass28502662019-02-16 20:24:54 -0700247 .setup = sandbox_sound_setup,
248 .play = sandbox_sound_play,
Simon Glass3062cd12020-02-03 07:36:06 -0700249 .stop_play = sandbox_sound_stop_play,
Simon Glass28502662019-02-16 20:24:54 -0700250 .start_beep = sandbox_sound_start_beep,
251 .stop_beep = sandbox_sound_stop_beep,
Simon Glassd4901892018-12-10 10:37:36 -0700252};
253
254static const struct udevice_id sandbox_sound_ids[] = {
255 { .compatible = "sandbox,sound" },
256 { }
257};
258
259U_BOOT_DRIVER(sandbox_sound) = {
260 .name = "sandbox_sound",
261 .id = UCLASS_SOUND,
262 .of_match = sandbox_sound_ids,
263 .ops = &sandbox_sound_ops,
264 .priv_auto_alloc_size = sizeof(struct sandbox_sound_priv),
265 .probe = sandbox_sound_probe,
266};