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