blob: ee2635f41d256ad402d0e17ac399fe7ca584f24a [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
6#include <common.h>
Simon Glassce6d99a2018-12-10 10:37:33 -07007#include <audio_codec.h>
Simon Glasse96fa6c2018-12-10 10:37:34 -07008#include <dm.h>
9#include <i2s.h>
Simon Glassd4901892018-12-10 10:37:36 -070010#include <sound.h>
Masahiro Yamadaa8b0f9b2014-06-24 22:10:52 +090011#include <asm/sound.h>
Simon Glassc34c0242014-02-27 13:26:18 -070012#include <asm/sdl.h>
13
Simon Glassce6d99a2018-12-10 10:37:33 -070014struct sandbox_codec_priv {
15 int interface;
16 int rate;
17 int mclk_freq;
18 int bits_per_sample;
19 uint channels;
20};
21
Simon Glasse96fa6c2018-12-10 10:37:34 -070022struct sandbox_i2s_priv {
23 int sum; /* Use to sum the provided audio data */
24};
25
Simon Glassd4901892018-12-10 10:37:36 -070026struct sandbox_sound_priv {
27 int setup_called;
28 int sum; /* Use to sum the provided audio data */
29};
30
31#ifndef CONFIG_DM_SOUND
Simon Glassc34c0242014-02-27 13:26:18 -070032int sound_play(uint32_t msec, uint32_t frequency)
33{
34 sandbox_sdl_sound_start(frequency);
35 mdelay(msec);
36 sandbox_sdl_sound_stop();
37
38 return 0;
39}
Simon Glassd4901892018-12-10 10:37:36 -070040#endif /* CONFIG_DM_SOUND */
Simon Glassc34c0242014-02-27 13:26:18 -070041
42int sound_init(const void *blob)
43{
44 return sandbox_sdl_sound_init();
45}
Simon Glassce6d99a2018-12-10 10:37:33 -070046
47void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
48 int *mclk_freqp, int *bits_per_samplep,
49 uint *channelsp)
50{
51 struct sandbox_codec_priv *priv = dev_get_priv(dev);
52
53 *interfacep = priv->interface;
54 *ratep = priv->rate;
55 *mclk_freqp = priv->mclk_freq;
56 *bits_per_samplep = priv->bits_per_sample;
57 *channelsp = priv->channels;
58}
59
Simon Glasse96fa6c2018-12-10 10:37:34 -070060int sandbox_get_i2s_sum(struct udevice *dev)
61{
62 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
63
64 return priv->sum;
65}
66
Simon Glassd4901892018-12-10 10:37:36 -070067int sandbox_get_setup_called(struct udevice *dev)
68{
69 struct sandbox_sound_priv *priv = dev_get_priv(dev);
70
71 return priv->setup_called;
72}
73
74int sandbox_get_sound_sum(struct udevice *dev)
75{
76 struct sandbox_sound_priv *priv = dev_get_priv(dev);
77
78 return priv->sum;
79}
80
Simon Glassce6d99a2018-12-10 10:37:33 -070081static int sandbox_codec_set_params(struct udevice *dev, int interface,
82 int rate, int mclk_freq,
83 int bits_per_sample, uint channels)
84{
85 struct sandbox_codec_priv *priv = dev_get_priv(dev);
86
87 priv->interface = interface;
88 priv->rate = rate;
89 priv->mclk_freq = mclk_freq;
90 priv->bits_per_sample = bits_per_sample;
91 priv->channels = channels;
92
93 return 0;
94}
95
Simon Glasse96fa6c2018-12-10 10:37:34 -070096static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
97 uint data_size)
98{
99 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
100 int i;
101
102 for (i = 0; i < data_size; i++)
103 priv->sum += ((uint8_t *)data)[i];
104
105 return 0;
106}
107
108static int sandbox_i2s_probe(struct udevice *dev)
109{
110 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
111
112 /* Use hard-coded values here */
113 uc_priv->rfs = 256;
114 uc_priv->bfs = 32;
115 uc_priv->audio_pll_clk = 192000000;
116 uc_priv->samplingrate = 48000;
117 uc_priv->bitspersample = 16;
118 uc_priv->channels = 2;
119 uc_priv->id = 1;
120
Simon Glassd4901892018-12-10 10:37:36 -0700121 return sandbox_sdl_sound_init();
122}
123
124static int sandbox_sound_setup(struct udevice *dev)
125{
126 struct sandbox_sound_priv *priv = dev_get_priv(dev);
127
128 priv->setup_called++;
129
Simon Glasse96fa6c2018-12-10 10:37:34 -0700130 return 0;
131}
132
Simon Glassd4901892018-12-10 10:37:36 -0700133static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
134{
135 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
136 struct sandbox_sound_priv *priv = dev_get_priv(dev);
137 int i;
138
139 for (i = 0; i < data_size; i++)
140 priv->sum += ((uint8_t *)data)[i];
141
142 return i2s_tx_data(uc_priv->i2s, data, data_size);
143}
144
145static int sandbox_sound_probe(struct udevice *dev)
146{
147 return sound_find_codec_i2s(dev);
148}
149
Simon Glassce6d99a2018-12-10 10:37:33 -0700150static const struct audio_codec_ops sandbox_codec_ops = {
151 .set_params = sandbox_codec_set_params,
152};
153
154static const struct udevice_id sandbox_codec_ids[] = {
155 { .compatible = "sandbox,audio-codec" },
156 { }
157};
158
159U_BOOT_DRIVER(sandbox_codec) = {
160 .name = "sandbox_codec",
161 .id = UCLASS_AUDIO_CODEC,
162 .of_match = sandbox_codec_ids,
163 .ops = &sandbox_codec_ops,
164 .priv_auto_alloc_size = sizeof(struct sandbox_codec_priv),
165};
Simon Glasse96fa6c2018-12-10 10:37:34 -0700166
167static const struct i2s_ops sandbox_i2s_ops = {
168 .tx_data = sandbox_i2s_tx_data,
169};
170
171static const struct udevice_id sandbox_i2s_ids[] = {
172 { .compatible = "sandbox,i2s" },
173 { }
174};
175
176U_BOOT_DRIVER(sandbox_i2s) = {
177 .name = "sandbox_i2s",
178 .id = UCLASS_I2S,
179 .of_match = sandbox_i2s_ids,
180 .ops = &sandbox_i2s_ops,
181 .probe = sandbox_i2s_probe,
182 .priv_auto_alloc_size = sizeof(struct sandbox_i2s_priv),
183};
Simon Glassd4901892018-12-10 10:37:36 -0700184
185static const struct sound_ops sandbox_sound_ops = {
186 .setup = sandbox_sound_setup,
187 .play = sandbox_sound_play,
188};
189
190static const struct udevice_id sandbox_sound_ids[] = {
191 { .compatible = "sandbox,sound" },
192 { }
193};
194
195U_BOOT_DRIVER(sandbox_sound) = {
196 .name = "sandbox_sound",
197 .id = UCLASS_SOUND,
198 .of_match = sandbox_sound_ids,
199 .ops = &sandbox_sound_ops,
200 .priv_auto_alloc_size = sizeof(struct sandbox_sound_priv),
201 .probe = sandbox_sound_probe,
202};