1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
12 #define SOUND_BITS_IN_BYTE 8
14 int sound_setup(struct udevice *dev)
16 struct sound_ops *ops = sound_get_ops(dev);
21 return ops->setup(dev);
24 int sound_play(struct udevice *dev, void *data, uint data_size)
26 struct sound_ops *ops = sound_get_ops(dev);
31 return ops->play(dev, data, data_size);
34 int sound_start_beep(struct udevice *dev, int frequency_hz)
36 struct sound_ops *ops = sound_get_ops(dev);
41 return ops->start_beep(dev, frequency_hz);
44 int sound_stop_beep(struct udevice *dev)
46 struct sound_ops *ops = sound_get_ops(dev);
51 return ops->stop_beep(dev);
54 int sound_beep(struct udevice *dev, int msecs, int frequency_hz)
56 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
57 struct i2s_uc_priv *i2s_uc_priv;
62 ret = sound_setup(dev);
63 if (ret && ret != -EALREADY)
66 /* Try using the beep interface if available */
67 ret = sound_start_beep(dev, frequency_hz);
72 ret = sound_stop_beep(dev);
77 /* Buffer length computation */
78 i2s_uc_priv = dev_get_uclass_priv(uc_priv->i2s);
79 data_size = i2s_uc_priv->samplingrate * i2s_uc_priv->channels;
80 data_size *= (i2s_uc_priv->bitspersample / SOUND_BITS_IN_BYTE);
81 data = malloc(data_size);
83 debug("%s: malloc failed\n", __func__);
87 sound_create_square_wave(i2s_uc_priv->samplingrate, data, data_size,
88 frequency_hz, i2s_uc_priv->channels);
90 while (msecs >= 1000) {
91 ret = sound_play(dev, data, data_size);
96 (data_size * msecs) / (sizeof(int) * 1000);
98 ret = sound_play(dev, data, size);
106 int sound_find_codec_i2s(struct udevice *dev)
108 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
109 struct ofnode_phandle_args args;
113 /* First the codec */
114 node = ofnode_find_subnode(dev_ofnode(dev), "codec");
115 if (!ofnode_valid(node)) {
116 debug("Failed to find /cpu subnode\n");
119 ret = ofnode_parse_phandle_with_args(node, "sound-dai",
120 "#sound-dai-cells", 0, 0, &args);
122 debug("Cannot find phandle: %d\n", ret);
125 ret = uclass_get_device_by_ofnode(UCLASS_AUDIO_CODEC, args.node,
128 debug("Cannot find codec: %d\n", ret);
133 node = ofnode_find_subnode(dev_ofnode(dev), "cpu");
134 if (!ofnode_valid(node)) {
135 debug("Failed to find /cpu subnode\n");
138 ret = ofnode_parse_phandle_with_args(node, "sound-dai",
139 "#sound-dai-cells", 0, 0, &args);
141 debug("Cannot find phandle: %d\n", ret);
144 ret = uclass_get_device_by_ofnode(UCLASS_I2S, args.node, &uc_priv->i2s);
146 debug("Cannot find i2s: %d\n", ret);
149 debug("Probed sound '%s' with codec '%s' and i2s '%s'\n", dev->name,
150 uc_priv->codec->name, uc_priv->i2s->name);
155 UCLASS_DRIVER(sound) = {
158 .per_device_auto_alloc_size = sizeof(struct sound_uc_priv),