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 | |
| 6 | #include <common.h> |
Simon Glass | ce6d99a | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 7 | #include <audio_codec.h> |
Simon Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <i2s.h> |
Simon Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame^] | 10 | #include <sound.h> |
Masahiro Yamada | a8b0f9b | 2014-06-24 22:10:52 +0900 | [diff] [blame] | 11 | #include <asm/sound.h> |
Simon Glass | c34c024 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 12 | #include <asm/sdl.h> |
| 13 | |
Simon Glass | ce6d99a | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 14 | struct sandbox_codec_priv { |
| 15 | int interface; |
| 16 | int rate; |
| 17 | int mclk_freq; |
| 18 | int bits_per_sample; |
| 19 | uint channels; |
| 20 | }; |
| 21 | |
Simon Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 22 | struct sandbox_i2s_priv { |
| 23 | int sum; /* Use to sum the provided audio data */ |
| 24 | }; |
| 25 | |
Simon Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame^] | 26 | struct 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 Glass | c34c024 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 32 | int 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 Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame^] | 40 | #endif /* CONFIG_DM_SOUND */ |
Simon Glass | c34c024 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 41 | |
| 42 | int sound_init(const void *blob) |
| 43 | { |
| 44 | return sandbox_sdl_sound_init(); |
| 45 | } |
Simon Glass | ce6d99a | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 46 | |
| 47 | void 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 Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 60 | int 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 Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame^] | 67 | int 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 | |
| 74 | int 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 Glass | ce6d99a | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 81 | static 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 Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 96 | static 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 | |
| 108 | static 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 Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame^] | 121 | return sandbox_sdl_sound_init(); |
| 122 | } |
| 123 | |
| 124 | static 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 Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
Simon Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame^] | 133 | static 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 | |
| 145 | static int sandbox_sound_probe(struct udevice *dev) |
| 146 | { |
| 147 | return sound_find_codec_i2s(dev); |
| 148 | } |
| 149 | |
Simon Glass | ce6d99a | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 150 | static const struct audio_codec_ops sandbox_codec_ops = { |
| 151 | .set_params = sandbox_codec_set_params, |
| 152 | }; |
| 153 | |
| 154 | static const struct udevice_id sandbox_codec_ids[] = { |
| 155 | { .compatible = "sandbox,audio-codec" }, |
| 156 | { } |
| 157 | }; |
| 158 | |
| 159 | U_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 Glass | e96fa6c | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 166 | |
| 167 | static const struct i2s_ops sandbox_i2s_ops = { |
| 168 | .tx_data = sandbox_i2s_tx_data, |
| 169 | }; |
| 170 | |
| 171 | static const struct udevice_id sandbox_i2s_ids[] = { |
| 172 | { .compatible = "sandbox,i2s" }, |
| 173 | { } |
| 174 | }; |
| 175 | |
| 176 | U_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 Glass | d490189 | 2018-12-10 10:37:36 -0700 | [diff] [blame^] | 184 | |
| 185 | static const struct sound_ops sandbox_sound_ops = { |
| 186 | .setup = sandbox_sound_setup, |
| 187 | .play = sandbox_sound_play, |
| 188 | }; |
| 189 | |
| 190 | static const struct udevice_id sandbox_sound_ids[] = { |
| 191 | { .compatible = "sandbox,sound" }, |
| 192 | { } |
| 193 | }; |
| 194 | |
| 195 | U_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 | }; |