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 | |
| 34 | int sound_beep(struct udevice *dev, int msecs, int frequency_hz) |
| 35 | { |
| 36 | struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev); |
| 37 | struct i2s_uc_priv *i2s_uc_priv = dev_get_uclass_priv(uc_priv->i2s); |
| 38 | unsigned short *data; |
| 39 | uint data_size; |
| 40 | int ret; |
| 41 | |
| 42 | ret = sound_setup(dev); |
| 43 | if (ret && ret != -EALREADY) |
| 44 | return ret; |
| 45 | |
| 46 | /* Buffer length computation */ |
| 47 | data_size = i2s_uc_priv->samplingrate * i2s_uc_priv->channels; |
| 48 | data_size *= (i2s_uc_priv->bitspersample / SOUND_BITS_IN_BYTE); |
| 49 | data = malloc(data_size); |
| 50 | if (!data) { |
| 51 | debug("%s: malloc failed\n", __func__); |
| 52 | return -ENOMEM; |
| 53 | } |
| 54 | |
| 55 | sound_create_square_wave(i2s_uc_priv->samplingrate, data, data_size, |
Simon Glass | 51aa860 | 2018-12-10 10:37:51 -0700 | [diff] [blame] | 56 | frequency_hz, i2s_uc_priv->channels); |
Simon Glass | 76072ac | 2018-12-10 10:37:36 -0700 | [diff] [blame] | 57 | |
| 58 | while (msecs >= 1000) { |
| 59 | ret = sound_play(dev, data, data_size); |
| 60 | msecs -= 1000; |
| 61 | } |
| 62 | if (msecs) { |
| 63 | unsigned long size = |
| 64 | (data_size * msecs) / (sizeof(int) * 1000); |
| 65 | |
| 66 | ret = sound_play(dev, data, size); |
| 67 | } |
| 68 | |
| 69 | free(data); |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | int sound_find_codec_i2s(struct udevice *dev) |
| 75 | { |
| 76 | struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev); |
| 77 | struct ofnode_phandle_args args; |
| 78 | ofnode node; |
| 79 | int ret; |
| 80 | |
| 81 | /* First the codec */ |
| 82 | node = ofnode_find_subnode(dev_ofnode(dev), "codec"); |
| 83 | if (!ofnode_valid(node)) { |
| 84 | debug("Failed to find /cpu subnode\n"); |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | ret = ofnode_parse_phandle_with_args(node, "sound-dai", |
| 88 | "#sound-dai-cells", 0, 0, &args); |
| 89 | if (ret) { |
| 90 | debug("Cannot find phandle: %d\n", ret); |
| 91 | return ret; |
| 92 | } |
| 93 | ret = uclass_get_device_by_ofnode(UCLASS_AUDIO_CODEC, args.node, |
| 94 | &uc_priv->codec); |
| 95 | if (ret) { |
| 96 | debug("Cannot find codec: %d\n", ret); |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | /* Now the i2s */ |
| 101 | node = ofnode_find_subnode(dev_ofnode(dev), "cpu"); |
| 102 | if (!ofnode_valid(node)) { |
| 103 | debug("Failed to find /cpu subnode\n"); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | ret = ofnode_parse_phandle_with_args(node, "sound-dai", |
| 107 | "#sound-dai-cells", 0, 0, &args); |
| 108 | if (ret) { |
| 109 | debug("Cannot find phandle: %d\n", ret); |
| 110 | return ret; |
| 111 | } |
| 112 | ret = uclass_get_device_by_ofnode(UCLASS_I2S, args.node, &uc_priv->i2s); |
| 113 | if (ret) { |
| 114 | debug("Cannot find i2s: %d\n", ret); |
| 115 | return ret; |
| 116 | } |
| 117 | debug("Probed sound '%s' with codec '%s' and i2s '%s'\n", dev->name, |
| 118 | uc_priv->codec->name, uc_priv->i2s->name); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | UCLASS_DRIVER(sound) = { |
| 124 | .id = UCLASS_SOUND, |
| 125 | .name = "sound", |
| 126 | .per_device_auto_alloc_size = sizeof(struct sound_uc_priv), |
| 127 | }; |