Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c34c024 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
Simon Glass | c34c024 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | ecc7973 | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_SOUND |
| 7 | |
Simon Glass | c34c024 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 8 | #include <common.h> |
Simon Glass | ce6d99a | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 9 | #include <audio_codec.h> |
Simon Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <i2s.h> |
Simon Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 12 | #include <sound.h> |
Simon Glass | c34c024 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 13 | #include <asm/sdl.h> |
| 14 | |
Simon Glass | ce6d99a | 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 | e96fa6c | 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 | ecc7973 | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 25 | bool silent; /* Sound is silent, don't use SDL */ |
Simon Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
Simon Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 28 | struct sandbox_sound_priv { |
Simon Glass | 3062cd1 | 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 */ |
Simon Glass | 2850266 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 31 | 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 Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Simon Glass | ce6d99a | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 36 | void 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 Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 49 | int 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 Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 56 | int 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 Glass | 3062cd1 | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 63 | int 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 Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 70 | int 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 Glass | 2850266 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 77 | void 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 | |
| 84 | int 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 Glass | ce6d99a | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 91 | static 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 Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 106 | static 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 Glass | ecc7973 | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 115 | 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 Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int sandbox_i2s_probe(struct udevice *dev) |
| 127 | { |
| 128 | struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | ecc7973 | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 129 | struct sandbox_i2s_priv *priv = dev_get_priv(dev); |
Simon Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 130 | |
| 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 Glass | ecc7973 | 2019-02-16 20:24:56 -0700 | [diff] [blame] | 140 | 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 Glass | d6cadd5 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 149 | |
| 150 | return 0; |
Simon Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static 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 Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
Simon Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 162 | static 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 Glass | 3062cd1 | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 174 | static 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 Glass | 2850266 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 184 | int 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 | |
| 195 | int 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 Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 206 | static int sandbox_sound_probe(struct udevice *dev) |
| 207 | { |
| 208 | return sound_find_codec_i2s(dev); |
| 209 | } |
| 210 | |
Simon Glass | ce6d99a | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 211 | static const struct audio_codec_ops sandbox_codec_ops = { |
| 212 | .set_params = sandbox_codec_set_params, |
| 213 | }; |
| 214 | |
| 215 | static const struct udevice_id sandbox_codec_ids[] = { |
| 216 | { .compatible = "sandbox,audio-codec" }, |
| 217 | { } |
| 218 | }; |
| 219 | |
| 220 | U_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 Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 227 | |
| 228 | static const struct i2s_ops sandbox_i2s_ops = { |
| 229 | .tx_data = sandbox_i2s_tx_data, |
| 230 | }; |
| 231 | |
| 232 | static const struct udevice_id sandbox_i2s_ids[] = { |
| 233 | { .compatible = "sandbox,i2s" }, |
| 234 | { } |
| 235 | }; |
| 236 | |
| 237 | U_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 Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 245 | |
| 246 | static const struct sound_ops sandbox_sound_ops = { |
Simon Glass | 2850266 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 247 | .setup = sandbox_sound_setup, |
| 248 | .play = sandbox_sound_play, |
Simon Glass | 3062cd1 | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 249 | .stop_play = sandbox_sound_stop_play, |
Simon Glass | 2850266 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 250 | .start_beep = sandbox_sound_start_beep, |
| 251 | .stop_beep = sandbox_sound_stop_beep, |
Simon Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | static const struct udevice_id sandbox_sound_ids[] = { |
| 255 | { .compatible = "sandbox,sound" }, |
| 256 | { } |
| 257 | }; |
| 258 | |
| 259 | U_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 | }; |