blob: b0b07f3239ba86cb1a97bcd33499db3a2e86f6e5 [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
6#include <common.h>
Simon Glassed96cde2018-12-10 10:37:33 -07007#include <audio_codec.h>
Simon Glassc953aaf2018-12-10 10:37:34 -07008#include <dm.h>
9#include <i2s.h>
Simon Glass76072ac2018-12-10 10:37:36 -070010#include <sound.h>
Simon Glass279fc162014-02-27 13:26:18 -070011#include <asm/sdl.h>
12
Simon Glassed96cde2018-12-10 10:37:33 -070013struct sandbox_codec_priv {
14 int interface;
15 int rate;
16 int mclk_freq;
17 int bits_per_sample;
18 uint channels;
19};
20
Simon Glassc953aaf2018-12-10 10:37:34 -070021struct sandbox_i2s_priv {
22 int sum; /* Use to sum the provided audio data */
23};
24
Simon Glass76072ac2018-12-10 10:37:36 -070025struct sandbox_sound_priv {
26 int setup_called;
27 int sum; /* Use to sum the provided audio data */
28};
29
Simon Glassed96cde2018-12-10 10:37:33 -070030void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
31 int *mclk_freqp, int *bits_per_samplep,
32 uint *channelsp)
33{
34 struct sandbox_codec_priv *priv = dev_get_priv(dev);
35
36 *interfacep = priv->interface;
37 *ratep = priv->rate;
38 *mclk_freqp = priv->mclk_freq;
39 *bits_per_samplep = priv->bits_per_sample;
40 *channelsp = priv->channels;
41}
42
Simon Glassc953aaf2018-12-10 10:37:34 -070043int sandbox_get_i2s_sum(struct udevice *dev)
44{
45 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
46
47 return priv->sum;
48}
49
Simon Glass76072ac2018-12-10 10:37:36 -070050int sandbox_get_setup_called(struct udevice *dev)
51{
52 struct sandbox_sound_priv *priv = dev_get_priv(dev);
53
54 return priv->setup_called;
55}
56
57int sandbox_get_sound_sum(struct udevice *dev)
58{
59 struct sandbox_sound_priv *priv = dev_get_priv(dev);
60
61 return priv->sum;
62}
63
Simon Glassed96cde2018-12-10 10:37:33 -070064static int sandbox_codec_set_params(struct udevice *dev, int interface,
65 int rate, int mclk_freq,
66 int bits_per_sample, uint channels)
67{
68 struct sandbox_codec_priv *priv = dev_get_priv(dev);
69
70 priv->interface = interface;
71 priv->rate = rate;
72 priv->mclk_freq = mclk_freq;
73 priv->bits_per_sample = bits_per_sample;
74 priv->channels = channels;
75
76 return 0;
77}
78
Simon Glassc953aaf2018-12-10 10:37:34 -070079static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
80 uint data_size)
81{
82 struct sandbox_i2s_priv *priv = dev_get_priv(dev);
83 int i;
84
85 for (i = 0; i < data_size; i++)
86 priv->sum += ((uint8_t *)data)[i];
87
Simon Glassab6dbe62018-12-10 10:37:47 -070088 return sandbox_sdl_sound_play(data, data_size);
Simon Glassc953aaf2018-12-10 10:37:34 -070089}
90
91static int sandbox_i2s_probe(struct udevice *dev)
92{
93 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
94
95 /* Use hard-coded values here */
96 uc_priv->rfs = 256;
97 uc_priv->bfs = 32;
98 uc_priv->audio_pll_clk = 192000000;
99 uc_priv->samplingrate = 48000;
100 uc_priv->bitspersample = 16;
101 uc_priv->channels = 2;
102 uc_priv->id = 1;
103
Simon Glass4070ba62018-12-10 10:37:39 -0700104 /* Ignore any error here - we'll just have no sound */
Simon Glass658a8742018-12-10 10:37:50 -0700105 sandbox_sdl_sound_init(uc_priv->samplingrate, uc_priv->channels);
Simon Glass4070ba62018-12-10 10:37:39 -0700106
107 return 0;
Simon Glass76072ac2018-12-10 10:37:36 -0700108}
109
110static int sandbox_sound_setup(struct udevice *dev)
111{
112 struct sandbox_sound_priv *priv = dev_get_priv(dev);
113
114 priv->setup_called++;
115
Simon Glassc953aaf2018-12-10 10:37:34 -0700116 return 0;
117}
118
Simon Glass76072ac2018-12-10 10:37:36 -0700119static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
120{
121 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
122 struct sandbox_sound_priv *priv = dev_get_priv(dev);
123 int i;
124
125 for (i = 0; i < data_size; i++)
126 priv->sum += ((uint8_t *)data)[i];
127
128 return i2s_tx_data(uc_priv->i2s, data, data_size);
129}
130
131static int sandbox_sound_probe(struct udevice *dev)
132{
133 return sound_find_codec_i2s(dev);
134}
135
Simon Glassed96cde2018-12-10 10:37:33 -0700136static const struct audio_codec_ops sandbox_codec_ops = {
137 .set_params = sandbox_codec_set_params,
138};
139
140static const struct udevice_id sandbox_codec_ids[] = {
141 { .compatible = "sandbox,audio-codec" },
142 { }
143};
144
145U_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),
151};
Simon Glassc953aaf2018-12-10 10:37:34 -0700152
153static const struct i2s_ops sandbox_i2s_ops = {
154 .tx_data = sandbox_i2s_tx_data,
155};
156
157static const struct udevice_id sandbox_i2s_ids[] = {
158 { .compatible = "sandbox,i2s" },
159 { }
160};
161
162U_BOOT_DRIVER(sandbox_i2s) = {
163 .name = "sandbox_i2s",
164 .id = UCLASS_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),
169};
Simon Glass76072ac2018-12-10 10:37:36 -0700170
171static const struct sound_ops sandbox_sound_ops = {
172 .setup = sandbox_sound_setup,
173 .play = sandbox_sound_play,
174};
175
176static const struct udevice_id sandbox_sound_ids[] = {
177 { .compatible = "sandbox,sound" },
178 { }
179};
180
181U_BOOT_DRIVER(sandbox_sound) = {
182 .name = "sandbox_sound",
183 .id = UCLASS_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,
188};