Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2018 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_SOUND |
| 8 | |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -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 | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 13 | #include <sound.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 15 | |
| 16 | #define SOUND_BITS_IN_BYTE 8 |
| 17 | |
| 18 | int sound_setup(struct udevice *dev) |
| 19 | { |
| 20 | struct sound_ops *ops = sound_get_ops(dev); |
| 21 | |
| 22 | if (!ops->setup) |
Simon Glass | 7dfe527 | 2019-02-16 20:24:53 -0700 | [diff] [blame] | 23 | return 0; |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 24 | |
| 25 | return ops->setup(dev); |
| 26 | } |
| 27 | |
| 28 | int sound_play(struct udevice *dev, void *data, uint data_size) |
| 29 | { |
| 30 | struct sound_ops *ops = sound_get_ops(dev); |
| 31 | |
| 32 | if (!ops->play) |
| 33 | return -ENOSYS; |
| 34 | |
| 35 | return ops->play(dev, data, data_size); |
| 36 | } |
| 37 | |
Simon Glass | 70bc14b | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 38 | int sound_stop_play(struct udevice *dev) |
| 39 | { |
| 40 | struct sound_ops *ops = sound_get_ops(dev); |
| 41 | |
| 42 | if (!ops->play) |
| 43 | return -ENOSYS; |
| 44 | |
| 45 | return ops->stop_play(dev); |
| 46 | } |
| 47 | |
Simon Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 48 | int sound_start_beep(struct udevice *dev, int frequency_hz) |
| 49 | { |
| 50 | struct sound_ops *ops = sound_get_ops(dev); |
| 51 | |
| 52 | if (!ops->start_beep) |
| 53 | return -ENOSYS; |
| 54 | |
| 55 | return ops->start_beep(dev, frequency_hz); |
| 56 | } |
| 57 | |
| 58 | int sound_stop_beep(struct udevice *dev) |
| 59 | { |
| 60 | struct sound_ops *ops = sound_get_ops(dev); |
| 61 | |
| 62 | if (!ops->stop_beep) |
| 63 | return -ENOSYS; |
| 64 | |
| 65 | return ops->stop_beep(dev); |
| 66 | } |
| 67 | |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 68 | int sound_beep(struct udevice *dev, int msecs, int frequency_hz) |
| 69 | { |
| 70 | struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 71 | struct i2s_uc_priv *i2s_uc_priv; |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 72 | unsigned short *data; |
| 73 | uint data_size; |
| 74 | int ret; |
| 75 | |
| 76 | ret = sound_setup(dev); |
| 77 | if (ret && ret != -EALREADY) |
| 78 | return ret; |
Simon Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 79 | |
| 80 | /* Try using the beep interface if available */ |
| 81 | ret = sound_start_beep(dev, frequency_hz); |
| 82 | if (ret != -ENOSYS) { |
| 83 | if (ret) |
| 84 | return ret; |
| 85 | mdelay(msecs); |
| 86 | ret = sound_stop_beep(dev); |
| 87 | |
| 88 | return ret; |
| 89 | } |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 90 | |
| 91 | /* Buffer length computation */ |
Simon Glass | ecd02e7 | 2019-02-16 20:24:54 -0700 | [diff] [blame] | 92 | i2s_uc_priv = dev_get_uclass_priv(uc_priv->i2s); |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 93 | data_size = i2s_uc_priv->samplingrate * i2s_uc_priv->channels; |
| 94 | data_size *= (i2s_uc_priv->bitspersample / SOUND_BITS_IN_BYTE); |
| 95 | data = malloc(data_size); |
| 96 | if (!data) { |
| 97 | debug("%s: malloc failed\n", __func__); |
| 98 | return -ENOMEM; |
| 99 | } |
| 100 | |
| 101 | sound_create_square_wave(i2s_uc_priv->samplingrate, data, data_size, |
Simon Glass | 51aa860 | 2018-12-10 10:37:51 -0700 | [diff] [blame] | 102 | frequency_hz, i2s_uc_priv->channels); |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 103 | |
Simon Glass | adf598a | 2020-02-03 07:36:07 -0700 | [diff] [blame] | 104 | ret = 0; |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 105 | while (msecs >= 1000) { |
| 106 | ret = sound_play(dev, data, data_size); |
Simon Glass | adf598a | 2020-02-03 07:36:07 -0700 | [diff] [blame] | 107 | if (ret) |
| 108 | break; |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 109 | msecs -= 1000; |
| 110 | } |
Simon Glass | adf598a | 2020-02-03 07:36:07 -0700 | [diff] [blame] | 111 | if (!ret && msecs) { |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 112 | unsigned long size = |
| 113 | (data_size * msecs) / (sizeof(int) * 1000); |
| 114 | |
| 115 | ret = sound_play(dev, data, size); |
| 116 | } |
Simon Glass | 70bc14b | 2020-02-03 07:36:06 -0700 | [diff] [blame] | 117 | sound_stop_play(dev); |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 118 | |
| 119 | free(data); |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | int sound_find_codec_i2s(struct udevice *dev) |
| 125 | { |
| 126 | struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev); |
| 127 | struct ofnode_phandle_args args; |
| 128 | ofnode node; |
| 129 | int ret; |
| 130 | |
| 131 | /* First the codec */ |
| 132 | node = ofnode_find_subnode(dev_ofnode(dev), "codec"); |
| 133 | if (!ofnode_valid(node)) { |
| 134 | debug("Failed to find /cpu subnode\n"); |
| 135 | return -EINVAL; |
| 136 | } |
| 137 | ret = ofnode_parse_phandle_with_args(node, "sound-dai", |
| 138 | "#sound-dai-cells", 0, 0, &args); |
| 139 | if (ret) { |
| 140 | debug("Cannot find phandle: %d\n", ret); |
| 141 | return ret; |
| 142 | } |
| 143 | ret = uclass_get_device_by_ofnode(UCLASS_AUDIO_CODEC, args.node, |
| 144 | &uc_priv->codec); |
| 145 | if (ret) { |
| 146 | debug("Cannot find codec: %d\n", ret); |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /* Now the i2s */ |
| 151 | node = ofnode_find_subnode(dev_ofnode(dev), "cpu"); |
| 152 | if (!ofnode_valid(node)) { |
| 153 | debug("Failed to find /cpu subnode\n"); |
| 154 | return -EINVAL; |
| 155 | } |
| 156 | ret = ofnode_parse_phandle_with_args(node, "sound-dai", |
| 157 | "#sound-dai-cells", 0, 0, &args); |
| 158 | if (ret) { |
| 159 | debug("Cannot find phandle: %d\n", ret); |
| 160 | return ret; |
| 161 | } |
| 162 | ret = uclass_get_device_by_ofnode(UCLASS_I2S, args.node, &uc_priv->i2s); |
| 163 | if (ret) { |
| 164 | debug("Cannot find i2s: %d\n", ret); |
| 165 | return ret; |
| 166 | } |
| 167 | debug("Probed sound '%s' with codec '%s' and i2s '%s'\n", dev->name, |
| 168 | uc_priv->codec->name, uc_priv->i2s->name); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | UCLASS_DRIVER(sound) = { |
| 174 | .id = UCLASS_SOUND, |
| 175 | .name = "sound", |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 176 | .per_device_auto = sizeof(struct sound_uc_priv), |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 177 | }; |