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 | 279fc16 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 8 | #include <common.h> |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 9 | #include <audio_codec.h> |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <i2s.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 13 | #include <sound.h> |
Simon Glass | 279fc16 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 14 | #include <asm/sdl.h> |
| 15 | |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 16 | struct sandbox_codec_priv { |
| 17 | int interface; |
| 18 | int rate; |
| 19 | int mclk_freq; |
| 20 | int bits_per_sample; |
| 21 | uint channels; |
| 22 | }; |
| 23 | |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 24 | struct sandbox_i2s_priv { |
| 25 | int sum; /* Use to sum the provided audio data */ |
Simon Glass | 4d5814c | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 26 | bool silent; /* Sound is silent, don't use SDL */ |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 29 | struct sandbox_sound_priv { |
Simon Glass | 70bc14b | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 30 | int setup_called; /* Incremented when setup() method is called */ |
| 31 | bool active; /* TX data is being sent */ |
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 | |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 71 | int 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 Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 78 | void 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 | |
| 85 | int 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 Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 92 | static 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 Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 107 | static 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 Glass | 4d5814c | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 116 | 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 Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static int sandbox_i2s_probe(struct udevice *dev) |
| 128 | { |
| 129 | struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 4d5814c | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 130 | struct sandbox_i2s_priv *priv = dev_get_priv(dev); |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 131 | |
| 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 Glass | 4d5814c | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 141 | 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 Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 150 | |
| 151 | return 0; |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static 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 Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 163 | static 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 Glass | 70bc14b | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 175 | static 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 Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 185 | int 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 | |
| 196 | int 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 Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 207 | static int sandbox_sound_probe(struct udevice *dev) |
| 208 | { |
| 209 | return sound_find_codec_i2s(dev); |
| 210 | } |
| 211 | |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 212 | static const struct audio_codec_ops sandbox_codec_ops = { |
| 213 | .set_params = sandbox_codec_set_params, |
| 214 | }; |
| 215 | |
| 216 | static const struct udevice_id sandbox_codec_ids[] = { |
| 217 | { .compatible = "sandbox,audio-codec" }, |
| 218 | { } |
| 219 | }; |
| 220 | |
| 221 | U_BOOT_DRIVER(sandbox_codec) = { |
| 222 | .name = "sandbox_codec", |
| 223 | .id = UCLASS_AUDIO_CODEC, |
| 224 | .of_match = sandbox_codec_ids, |
| 225 | .ops = &sandbox_codec_ops, |
| 226 | .priv_auto_alloc_size = sizeof(struct sandbox_codec_priv), |
| 227 | }; |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 228 | |
| 229 | static const struct i2s_ops sandbox_i2s_ops = { |
| 230 | .tx_data = sandbox_i2s_tx_data, |
| 231 | }; |
| 232 | |
| 233 | static const struct udevice_id sandbox_i2s_ids[] = { |
| 234 | { .compatible = "sandbox,i2s" }, |
| 235 | { } |
| 236 | }; |
| 237 | |
| 238 | U_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, |
| 244 | .priv_auto_alloc_size = sizeof(struct sandbox_i2s_priv), |
| 245 | }; |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 246 | |
| 247 | static const struct sound_ops sandbox_sound_ops = { |
Simon Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 248 | .setup = sandbox_sound_setup, |
| 249 | .play = sandbox_sound_play, |
Simon Glass | 70bc14b | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 250 | .stop_play = sandbox_sound_stop_play, |
Simon Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 251 | .start_beep = sandbox_sound_start_beep, |
| 252 | .stop_beep = sandbox_sound_stop_beep, |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | static const struct udevice_id sandbox_sound_ids[] = { |
| 256 | { .compatible = "sandbox,sound" }, |
| 257 | { } |
| 258 | }; |
| 259 | |
| 260 | U_BOOT_DRIVER(sandbox_sound) = { |
| 261 | .name = "sandbox_sound", |
| 262 | .id = UCLASS_SOUND, |
| 263 | .of_match = sandbox_sound_ids, |
| 264 | .ops = &sandbox_sound_ops, |
| 265 | .priv_auto_alloc_size = sizeof(struct sandbox_sound_priv), |
| 266 | .probe = sandbox_sound_probe, |
| 267 | }; |