blob: d9b3a38f18c7ea903a211db887647eb19465d123 [file] [log] [blame]
Simon Glassd4901892018-12-10 10:37:36 -07001// 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 Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Simon Glassd4901892018-12-10 10:37:36 -070011#include <sound.h>
12
13#define SOUND_BITS_IN_BYTE 8
14
15int sound_setup(struct udevice *dev)
16{
17 struct sound_ops *ops = sound_get_ops(dev);
18
19 if (!ops->setup)
Simon Glasse65f9ef2019-02-16 20:24:53 -070020 return 0;
Simon Glassd4901892018-12-10 10:37:36 -070021
22 return ops->setup(dev);
23}
24
25int 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 Glass3062cd12020-02-03 07:36:06 -070035int 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 Glass28502662019-02-16 20:24:54 -070045int 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
55int 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 Glassd4901892018-12-10 10:37:36 -070065int sound_beep(struct udevice *dev, int msecs, int frequency_hz)
66{
67 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass28502662019-02-16 20:24:54 -070068 struct i2s_uc_priv *i2s_uc_priv;
Simon Glassd4901892018-12-10 10:37:36 -070069 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 Glass28502662019-02-16 20:24:54 -070077 /* 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 Glassd4901892018-12-10 10:37:36 -070088 /* Buffer length computation */
Simon Glass28502662019-02-16 20:24:54 -070089 i2s_uc_priv = dev_get_uclass_priv(uc_priv->i2s);
Simon Glassd4901892018-12-10 10:37:36 -070090 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 Glassf9871772018-12-10 10:37:51 -070099 frequency_hz, i2s_uc_priv->channels);
Simon Glassd4901892018-12-10 10:37:36 -0700100
Simon Glass02662482020-02-03 07:36:07 -0700101 ret = 0;
Simon Glassd4901892018-12-10 10:37:36 -0700102 while (msecs >= 1000) {
103 ret = sound_play(dev, data, data_size);
Simon Glass02662482020-02-03 07:36:07 -0700104 if (ret)
105 break;
Simon Glassd4901892018-12-10 10:37:36 -0700106 msecs -= 1000;
107 }
Simon Glass02662482020-02-03 07:36:07 -0700108 if (!ret && msecs) {
Simon Glassd4901892018-12-10 10:37:36 -0700109 unsigned long size =
110 (data_size * msecs) / (sizeof(int) * 1000);
111
112 ret = sound_play(dev, data, size);
113 }
Simon Glass3062cd12020-02-03 07:36:06 -0700114 sound_stop_play(dev);
Simon Glassd4901892018-12-10 10:37:36 -0700115
116 free(data);
117
118 return ret;
119}
120
121int 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
170UCLASS_DRIVER(sound) = {
171 .id = UCLASS_SOUND,
172 .name = "sound",
173 .per_device_auto_alloc_size = sizeof(struct sound_uc_priv),
174};