Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 279fc16 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
Simon Glass | 279fc16 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 4d5814c | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_SOUND |
| 7 | |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 8 | #include <audio_codec.h> |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <i2s.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 12 | #include <sound.h> |
Simon Glass | 279fc16 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 13 | #include <asm/sdl.h> |
| 14 | |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 15 | struct sandbox_codec_priv { |
| 16 | int interface; |
| 17 | int rate; |
| 18 | int mclk_freq; |
| 19 | int bits_per_sample; |
| 20 | uint channels; |
| 21 | }; |
| 22 | |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 23 | struct sandbox_i2s_priv { |
| 24 | int sum; /* Use to sum the provided audio data */ |
Simon Glass | 4d5814c | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 25 | bool silent; /* Sound is silent, don't use SDL */ |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 28 | struct sandbox_sound_priv { |
Simon Glass | 70bc14b | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 29 | int setup_called; /* Incremented when setup() method is called */ |
| 30 | bool active; /* TX data is being sent */ |
Heinrich Schuchardt | 707a6fc | 2022-12-04 17:11:41 +0100 | [diff] [blame] | 31 | int count; /* Use to count the provided audio data */ |
Simon Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 32 | 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 Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 37 | void 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 Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 50 | int 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 Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 57 | int 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 Glass | 70bc14b | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 64 | int 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 Schuchardt | 707a6fc | 2022-12-04 17:11:41 +0100 | [diff] [blame] | 71 | int 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 Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 78 | int 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 Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 85 | void 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 | |
| 92 | int 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 Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 99 | static 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 Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 114 | static 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 Glass | 4d5814c | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 123 | 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 Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static int sandbox_i2s_probe(struct udevice *dev) |
| 135 | { |
| 136 | struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 4d5814c | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 137 | struct sandbox_i2s_priv *priv = dev_get_priv(dev); |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 138 | |
| 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 Glass | 4d5814c | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 148 | 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 Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 157 | |
| 158 | return 0; |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static 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 Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 170 | static 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 Schuchardt | 707a6fc | 2022-12-04 17:11:41 +0100 | [diff] [blame] | 178 | priv->count += data_size; |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 179 | |
| 180 | return i2s_tx_data(uc_priv->i2s, data, data_size); |
| 181 | } |
| 182 | |
Simon Glass | 70bc14b | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 183 | static 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 Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 193 | int 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 | |
| 204 | int 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 Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 215 | static int sandbox_sound_probe(struct udevice *dev) |
| 216 | { |
| 217 | return sound_find_codec_i2s(dev); |
| 218 | } |
| 219 | |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 220 | static const struct audio_codec_ops sandbox_codec_ops = { |
| 221 | .set_params = sandbox_codec_set_params, |
| 222 | }; |
| 223 | |
| 224 | static const struct udevice_id sandbox_codec_ids[] = { |
| 225 | { .compatible = "sandbox,audio-codec" }, |
| 226 | { } |
| 227 | }; |
| 228 | |
| 229 | U_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 Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 234 | .priv_auto = sizeof(struct sandbox_codec_priv), |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 235 | }; |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 236 | |
| 237 | static const struct i2s_ops sandbox_i2s_ops = { |
| 238 | .tx_data = sandbox_i2s_tx_data, |
| 239 | }; |
| 240 | |
| 241 | static const struct udevice_id sandbox_i2s_ids[] = { |
| 242 | { .compatible = "sandbox,i2s" }, |
| 243 | { } |
| 244 | }; |
| 245 | |
| 246 | U_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 Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 252 | .priv_auto = sizeof(struct sandbox_i2s_priv), |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 253 | }; |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 254 | |
| 255 | static const struct sound_ops sandbox_sound_ops = { |
Simon Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 256 | .setup = sandbox_sound_setup, |
| 257 | .play = sandbox_sound_play, |
Simon Glass | 70bc14b | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 258 | .stop_play = sandbox_sound_stop_play, |
Simon Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 259 | .start_beep = sandbox_sound_start_beep, |
| 260 | .stop_beep = sandbox_sound_stop_beep, |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | static const struct udevice_id sandbox_sound_ids[] = { |
| 264 | { .compatible = "sandbox,sound" }, |
| 265 | { } |
| 266 | }; |
| 267 | |
| 268 | U_BOOT_DRIVER(sandbox_sound) = { |
| 269 | .name = "sandbox_sound", |
| 270 | .id = UCLASS_SOUND, |
| 271 | .of_match = sandbox_sound_ids, |
| 272 | .ops = &sandbox_sound_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 273 | .priv_auto = sizeof(struct sandbox_sound_priv), |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 274 | .probe = sandbox_sound_probe, |
| 275 | }; |