blob: b8a3dab447d9c5494637082bc79af45c45971deb [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
Patrick Delaunayb953ec22021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_SOUND
8
Simon Glassd4901892018-12-10 10:37:36 -07009#include <dm.h>
10#include <i2s.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <malloc.h>
Simon Glassd4901892018-12-10 10:37:36 -070013#include <sound.h>
Simon Glassc05ed002020-05-10 11:40:11 -060014#include <linux/delay.h>
Simon Glassd4901892018-12-10 10:37:36 -070015
16#define SOUND_BITS_IN_BYTE 8
17
18int sound_setup(struct udevice *dev)
19{
20 struct sound_ops *ops = sound_get_ops(dev);
21
22 if (!ops->setup)
Simon Glasse65f9ef2019-02-16 20:24:53 -070023 return 0;
Simon Glassd4901892018-12-10 10:37:36 -070024
25 return ops->setup(dev);
26}
27
28int sound_play(struct udevice *dev, void *data, uint data_size)
29{
30 struct sound_ops *ops = sound_get_ops(dev);
31
32 if (!ops->play)
33 return -ENOSYS;
34
35 return ops->play(dev, data, data_size);
36}
37
Simon Glass3062cd12020-02-03 07:36:06 -070038int sound_stop_play(struct udevice *dev)
39{
40 struct sound_ops *ops = sound_get_ops(dev);
41
42 if (!ops->play)
43 return -ENOSYS;
44
45 return ops->stop_play(dev);
46}
47
Simon Glass28502662019-02-16 20:24:54 -070048int sound_start_beep(struct udevice *dev, int frequency_hz)
49{
50 struct sound_ops *ops = sound_get_ops(dev);
51
52 if (!ops->start_beep)
53 return -ENOSYS;
54
55 return ops->start_beep(dev, frequency_hz);
56}
57
58int sound_stop_beep(struct udevice *dev)
59{
60 struct sound_ops *ops = sound_get_ops(dev);
61
62 if (!ops->stop_beep)
63 return -ENOSYS;
64
65 return ops->stop_beep(dev);
66}
67
Simon Glassd4901892018-12-10 10:37:36 -070068int sound_beep(struct udevice *dev, int msecs, int frequency_hz)
69{
70 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass28502662019-02-16 20:24:54 -070071 struct i2s_uc_priv *i2s_uc_priv;
Simon Glassd4901892018-12-10 10:37:36 -070072 unsigned short *data;
73 uint data_size;
74 int ret;
75
76 ret = sound_setup(dev);
77 if (ret && ret != -EALREADY)
78 return ret;
79
Simon Glass28502662019-02-16 20:24:54 -070080 /* Try using the beep interface if available */
81 ret = sound_start_beep(dev, frequency_hz);
82 if (ret != -ENOSYS) {
83 if (ret)
84 return ret;
85 mdelay(msecs);
86 ret = sound_stop_beep(dev);
87
88 return ret;
89 }
90
Simon Glassd4901892018-12-10 10:37:36 -070091 /* Buffer length computation */
Simon Glass28502662019-02-16 20:24:54 -070092 i2s_uc_priv = dev_get_uclass_priv(uc_priv->i2s);
Simon Glassd4901892018-12-10 10:37:36 -070093 data_size = i2s_uc_priv->samplingrate * i2s_uc_priv->channels;
94 data_size *= (i2s_uc_priv->bitspersample / SOUND_BITS_IN_BYTE);
95 data = malloc(data_size);
96 if (!data) {
97 debug("%s: malloc failed\n", __func__);
98 return -ENOMEM;
99 }
100
101 sound_create_square_wave(i2s_uc_priv->samplingrate, data, data_size,
Simon Glassf9871772018-12-10 10:37:51 -0700102 frequency_hz, i2s_uc_priv->channels);
Simon Glassd4901892018-12-10 10:37:36 -0700103
Simon Glass02662482020-02-03 07:36:07 -0700104 ret = 0;
Simon Glassd4901892018-12-10 10:37:36 -0700105 while (msecs >= 1000) {
106 ret = sound_play(dev, data, data_size);
Simon Glass02662482020-02-03 07:36:07 -0700107 if (ret)
108 break;
Simon Glassd4901892018-12-10 10:37:36 -0700109 msecs -= 1000;
110 }
Simon Glass02662482020-02-03 07:36:07 -0700111 if (!ret && msecs) {
Simon Glassd4901892018-12-10 10:37:36 -0700112 unsigned long size =
113 (data_size * msecs) / (sizeof(int) * 1000);
114
115 ret = sound_play(dev, data, size);
116 }
Simon Glass3062cd12020-02-03 07:36:06 -0700117 sound_stop_play(dev);
Simon Glassd4901892018-12-10 10:37:36 -0700118
119 free(data);
120
121 return ret;
122}
123
124int sound_find_codec_i2s(struct udevice *dev)
125{
126 struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
127 struct ofnode_phandle_args args;
128 ofnode node;
129 int ret;
130
131 /* First the codec */
132 node = ofnode_find_subnode(dev_ofnode(dev), "codec");
133 if (!ofnode_valid(node)) {
134 debug("Failed to find /cpu subnode\n");
135 return -EINVAL;
136 }
137 ret = ofnode_parse_phandle_with_args(node, "sound-dai",
138 "#sound-dai-cells", 0, 0, &args);
139 if (ret) {
140 debug("Cannot find phandle: %d\n", ret);
141 return ret;
142 }
143 ret = uclass_get_device_by_ofnode(UCLASS_AUDIO_CODEC, args.node,
144 &uc_priv->codec);
145 if (ret) {
146 debug("Cannot find codec: %d\n", ret);
147 return ret;
148 }
149
150 /* Now the i2s */
151 node = ofnode_find_subnode(dev_ofnode(dev), "cpu");
152 if (!ofnode_valid(node)) {
153 debug("Failed to find /cpu subnode\n");
154 return -EINVAL;
155 }
156 ret = ofnode_parse_phandle_with_args(node, "sound-dai",
157 "#sound-dai-cells", 0, 0, &args);
158 if (ret) {
159 debug("Cannot find phandle: %d\n", ret);
160 return ret;
161 }
162 ret = uclass_get_device_by_ofnode(UCLASS_I2S, args.node, &uc_priv->i2s);
163 if (ret) {
164 debug("Cannot find i2s: %d\n", ret);
165 return ret;
166 }
167 debug("Probed sound '%s' with codec '%s' and i2s '%s'\n", dev->name,
168 uc_priv->codec->name, uc_priv->i2s->name);
169
170 return 0;
171}
172
173UCLASS_DRIVER(sound) = {
174 .id = UCLASS_SOUND,
175 .name = "sound",
Simon Glass41575d82020-12-03 16:55:17 -0700176 .per_device_auto = sizeof(struct sound_uc_priv),
Simon Glassd4901892018-12-10 10:37:36 -0700177};