1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
From 8cf85a58066800ed638b4e4fca6f704275e0f588 Mon Sep 17 00:00:00 2001
From: Tim Gover <tim.gover@raspberrypi.org>
Date: Wed, 27 Jun 2018 15:59:12 +0100
Subject: [PATCH 086/806] ASoC: Add generic RPI driver for simple soundcards.
The RPI simple sound card driver provides a generic ALSA SOC card driver
supporting a variety of Pi HAT soundcards. The intention is to avoid
the duplication of code for cards that can't be fully supported by
the soc simple/graph cards but are otherwise almost identical.
This initial commit adds support for the ADAU1977 ADC, Google VoiceHat,
HifiBerry AMP, HifiBerry DAC and RPI DAC.
Signed-off-by: Tim Gover <tim.gover@raspberrypi.org>
ASoC: Use correct card name in rpi-simple driver
Use the specific card name from drvdata instead of the snd_rpi_simple
rpi-simple-soundcard: Use nicer driver name "RPi-simple"
Rename the driver from "RPI simple soundcard" to "RPi-simple" so that
the driver name won't be mangled allowing to be used unaltered as the
card conf filename.
---
sound/soc/bcm/rpi-simple-soundcard.c | 268 +++++++++++++++++++++++++++
1 file changed, 268 insertions(+)
create mode 100644 sound/soc/bcm/rpi-simple-soundcard.c
--- /dev/null
+++ b/sound/soc/bcm/rpi-simple-soundcard.c
@@ -0,0 +1,268 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * rpi-simple-soundcard.c -- ALSA SoC Raspberry Pi soundcard.
+ *
+ * Copyright (C) 2018 Raspberry Pi.
+ *
+ * Authors: Tim Gover <tim.gover@raspberrypi.org>
+ *
+ * Based on code:
+ * hifiberry_amp.c, hifiberry_dac.c, rpi-dac.c
+ * by Florian Meier <florian.meier@koalo.de>
+ *
+ * googlevoicehat-soundcard.c
+ * by Peter Malkin <petermalkin@google.com>
+ *
+ * adau1977-adc.c
+ * by Andrey Grodzovsky <andrey2805@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+#include <sound/core.h>
+#include <sound/pcm.h>
+#include <sound/pcm_params.h>
+#include <sound/soc.h>
+
+/* Parameters for generic RPI functions */
+struct snd_rpi_simple_drvdata {
+ struct snd_soc_dai_link *dai;
+ const char* card_name;
+ unsigned int fixed_bclk_ratio;
+};
+
+static int snd_rpi_simple_init(struct snd_soc_pcm_runtime *rtd)
+{
+ struct snd_rpi_simple_drvdata *drvdata =
+ snd_soc_card_get_drvdata(rtd->card);
+ struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
+
+ if (drvdata->fixed_bclk_ratio > 0)
+ return snd_soc_dai_set_bclk_ratio(cpu_dai,
+ drvdata->fixed_bclk_ratio);
+
+ return 0;
+}
+
+static int snd_rpi_simple_hw_params(struct snd_pcm_substream *substream,
+ struct snd_pcm_hw_params *params)
+{
+ struct snd_soc_pcm_runtime *rtd = substream->private_data;
+ struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
+ struct snd_rpi_simple_drvdata *drvdata;
+ unsigned int sample_bits;
+
+ drvdata = snd_soc_card_get_drvdata(rtd->card);
+
+ if (drvdata->fixed_bclk_ratio > 0)
+ return 0; // BCLK is configured in .init
+
+ /* The simple drivers just set the bclk_ratio to sample_bits * 2 so
+ * hard-code this for now. More complex drivers could just replace
+ * the hw_params routine.
+ */
+ sample_bits = snd_pcm_format_physical_width(params_format(params));
+ return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
+}
+
+static struct snd_soc_ops snd_rpi_simple_ops = {
+ .hw_params = snd_rpi_simple_hw_params,
+};
+
+enum adau1977_clk_id {
+ ADAU1977_SYSCLK,
+};
+
+enum adau1977_sysclk_src {
+ ADAU1977_SYSCLK_SRC_MCLK,
+ ADAU1977_SYSCLK_SRC_LRCLK,
+};
+
+static int adau1977_init(struct snd_soc_pcm_runtime *rtd)
+{
+ int ret;
+ struct snd_soc_dai *codec_dai = rtd->codec_dai;
+
+ ret = snd_soc_dai_set_tdm_slot(codec_dai, 0, 0, 0, 0);
+ if (ret < 0)
+ return ret;
+
+ return snd_soc_component_set_sysclk(codec_dai->component,
+ ADAU1977_SYSCLK, ADAU1977_SYSCLK_SRC_MCLK,
+ 11289600, SND_SOC_CLOCK_IN);
+}
+
+static struct snd_soc_dai_link snd_rpi_adau1977_dai[] = {
+ {
+ .name = "adau1977",
+ .stream_name = "ADAU1977",
+ .codec_dai_name = "adau1977-hifi",
+ .codec_name = "adau1977.1-0011",
+ .init = adau1977_init,
+ .dai_fmt = SND_SOC_DAIFMT_I2S |
+ SND_SOC_DAIFMT_NB_NF |
+ SND_SOC_DAIFMT_CBM_CFM,
+ },
+};
+
+static struct snd_rpi_simple_drvdata drvdata_adau1977 = {
+ .card_name = "snd_rpi_adau1977_adc",
+ .dai = snd_rpi_adau1977_dai,
+};
+
+static struct snd_soc_dai_link snd_googlevoicehat_soundcard_dai[] = {
+{
+ .name = "Google voiceHAT SoundCard",
+ .stream_name = "Google voiceHAT SoundCard HiFi",
+ .codec_dai_name = "voicehat-hifi",
+ .codec_name = "voicehat-codec",
+ .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
+ SND_SOC_DAIFMT_CBS_CFS,
+},
+};
+
+static struct snd_rpi_simple_drvdata drvdata_googlevoicehat = {
+ .card_name = "snd_rpi_googlevoicehat_soundcard",
+ .dai = snd_googlevoicehat_soundcard_dai,
+};
+
+static struct snd_soc_dai_link snd_hifiberry_amp_dai[] = {
+ {
+ .name = "HifiBerry AMP",
+ .stream_name = "HifiBerry AMP HiFi",
+ .codec_dai_name = "tas5713-hifi",
+ .codec_name = "tas5713.1-001b",
+ .dai_fmt = SND_SOC_DAIFMT_I2S |
+ SND_SOC_DAIFMT_NB_NF |
+ SND_SOC_DAIFMT_CBS_CFS,
+ },
+};
+
+static struct snd_rpi_simple_drvdata drvdata_hifiberry_amp = {
+ .card_name = "snd_rpi_hifiberry_amp",
+ .dai = snd_hifiberry_amp_dai,
+ .fixed_bclk_ratio = 64,
+};
+
+static struct snd_soc_dai_link snd_hifiberry_dac_dai[] = {
+ {
+ .name = "HifiBerry DAC",
+ .stream_name = "HifiBerry DAC HiFi",
+ .codec_dai_name = "pcm5102a-hifi",
+ .codec_name = "pcm5102a-codec",
+ .dai_fmt = SND_SOC_DAIFMT_I2S |
+ SND_SOC_DAIFMT_NB_NF |
+ SND_SOC_DAIFMT_CBS_CFS,
+ },
+};
+
+static struct snd_rpi_simple_drvdata drvdata_hifiberry_dac = {
+ .card_name = "snd_rpi_hifiberry_dac",
+ .dai = snd_hifiberry_dac_dai,
+};
+
+static struct snd_soc_dai_link snd_rpi_dac_dai[] = {
+{
+ .name = "RPi-DAC",
+ .stream_name = "RPi-DAC HiFi",
+ .codec_dai_name = "pcm1794a-hifi",
+ .codec_name = "pcm1794a-codec",
+ .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
+ SND_SOC_DAIFMT_CBS_CFS,
+},
+};
+
+static struct snd_rpi_simple_drvdata drvdata_rpi_dac = {
+ .card_name = "snd_rpi_rpi_dac",
+ .dai = snd_rpi_dac_dai,
+ .fixed_bclk_ratio = 64,
+};
+
+static const struct of_device_id snd_rpi_simple_of_match[] = {
+ { .compatible = "adi,adau1977-adc",
+ .data = (void *) &drvdata_adau1977 },
+ { .compatible = "googlevoicehat,googlevoicehat-soundcard",
+ .data = (void *) &drvdata_googlevoicehat },
+ { .compatible = "hifiberry,hifiberry-amp",
+ .data = (void *) &drvdata_hifiberry_amp },
+ { .compatible = "hifiberry,hifiberry-dac",
+ .data = (void *) &drvdata_hifiberry_dac },
+ { .compatible = "rpi,rpi-dac", &drvdata_rpi_dac},
+ {},
+};
+
+static struct snd_soc_card snd_rpi_simple = {
+ .driver_name = "RPi-simple",
+ .owner = THIS_MODULE,
+ .dai_link = NULL,
+ .num_links = 1, /* Only a single DAI supported at the moment */
+};
+
+static int snd_rpi_simple_probe(struct platform_device *pdev)
+{
+ int ret = 0;
+ const struct of_device_id *of_id;
+
+ snd_rpi_simple.dev = &pdev->dev;
+ of_id = of_match_node(snd_rpi_simple_of_match, pdev->dev.of_node);
+
+ if (pdev->dev.of_node && of_id->data) {
+ struct device_node *i2s_node;
+ struct snd_rpi_simple_drvdata *drvdata =
+ (struct snd_rpi_simple_drvdata *) of_id->data;
+ struct snd_soc_dai_link *dai = drvdata->dai;
+
+ snd_soc_card_set_drvdata(&snd_rpi_simple, drvdata);
+
+ /* More complex drivers might override individual functions */
+ if (!dai->init)
+ dai->init = snd_rpi_simple_init;
+ if (!dai->ops)
+ dai->ops = &snd_rpi_simple_ops;
+
+ snd_rpi_simple.name = drvdata->card_name;
+
+ snd_rpi_simple.dai_link = dai;
+ i2s_node = of_parse_phandle(pdev->dev.of_node,
+ "i2s-controller", 0);
+ if (!i2s_node) {
+ pr_err("Failed to find i2s-controller DT node\n");
+ return -ENODEV;
+ }
+
+ dai->cpu_of_node = i2s_node;
+ dai->platform_of_node = i2s_node;
+ }
+
+ ret = devm_snd_soc_register_card(&pdev->dev, &snd_rpi_simple);
+ if (ret && ret != -EPROBE_DEFER)
+ dev_err(&pdev->dev, "Failed to register card %d\n", ret);
+
+ return ret;
+}
+
+static struct platform_driver snd_rpi_simple_driver = {
+ .driver = {
+ .name = "snd-rpi-simple",
+ .owner = THIS_MODULE,
+ .of_match_table = snd_rpi_simple_of_match,
+ },
+ .probe = snd_rpi_simple_probe,
+};
+MODULE_DEVICE_TABLE(of, snd_rpi_simple_of_match);
+
+module_platform_driver(snd_rpi_simple_driver);
+
+MODULE_AUTHOR("Tim Gover <tim.gover@raspberrypi.org>");
+MODULE_DESCRIPTION("ASoC Raspberry Pi simple soundcard driver ");
+MODULE_LICENSE("GPL v2");
|