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 | |
| 6 | #include <common.h> |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 7 | #include <audio_codec.h> |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <i2s.h> |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 10 | #include <sound.h> |
Simon Glass | 279fc16 | 2014-02-27 13:26:18 -0700 | [diff] [blame] | 11 | #include <asm/sdl.h> |
| 12 | |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 13 | struct sandbox_codec_priv { |
| 14 | int interface; |
| 15 | int rate; |
| 16 | int mclk_freq; |
| 17 | int bits_per_sample; |
| 18 | uint channels; |
| 19 | }; |
| 20 | |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 21 | struct sandbox_i2s_priv { |
| 22 | int sum; /* Use to sum the provided audio data */ |
| 23 | }; |
| 24 | |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 25 | struct sandbox_sound_priv { |
| 26 | int setup_called; |
| 27 | int sum; /* Use to sum the provided audio data */ |
| 28 | }; |
| 29 | |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 30 | void 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 Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 43 | int 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 Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 50 | int 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 | |
| 57 | int 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 Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 64 | static 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 Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 79 | static 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 Glass | ab6dbe6 | 2018-12-10 10:37:47 -0700 | [diff] [blame] | 88 | return sandbox_sdl_sound_play(data, data_size); |
Simon Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static 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 Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 104 | /* Ignore any error here - we'll just have no sound */ |
Simon Glass | 658a874 | 2018-12-10 10:37:50 -0700 | [diff] [blame] | 105 | sandbox_sdl_sound_init(uc_priv->samplingrate, uc_priv->channels); |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 106 | |
| 107 | return 0; |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static 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 Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 119 | static 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 | |
| 131 | static int sandbox_sound_probe(struct udevice *dev) |
| 132 | { |
| 133 | return sound_find_codec_i2s(dev); |
| 134 | } |
| 135 | |
Simon Glass | ed96cde | 2018-12-10 10:37:33 -0700 | [diff] [blame] | 136 | static const struct audio_codec_ops sandbox_codec_ops = { |
| 137 | .set_params = sandbox_codec_set_params, |
| 138 | }; |
| 139 | |
| 140 | static const struct udevice_id sandbox_codec_ids[] = { |
| 141 | { .compatible = "sandbox,audio-codec" }, |
| 142 | { } |
| 143 | }; |
| 144 | |
| 145 | U_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 Glass | c953aaf | 2018-12-10 10:37:34 -0700 | [diff] [blame] | 152 | |
| 153 | static const struct i2s_ops sandbox_i2s_ops = { |
| 154 | .tx_data = sandbox_i2s_tx_data, |
| 155 | }; |
| 156 | |
| 157 | static const struct udevice_id sandbox_i2s_ids[] = { |
| 158 | { .compatible = "sandbox,i2s" }, |
| 159 | { } |
| 160 | }; |
| 161 | |
| 162 | U_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 Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 170 | |
| 171 | static const struct sound_ops sandbox_sound_ops = { |
| 172 | .setup = sandbox_sound_setup, |
| 173 | .play = sandbox_sound_play, |
| 174 | }; |
| 175 | |
| 176 | static const struct udevice_id sandbox_sound_ids[] = { |
| 177 | { .compatible = "sandbox,sound" }, |
| 178 | { } |
| 179 | }; |
| 180 | |
| 181 | U_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 | }; |