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