1 From f3dc9ad69589abbf6572636fc2e2a120462a229a Mon Sep 17 00:00:00 2001
2 From: Tim Gover <tim.gover@raspberrypi.org>
3 Date: Wed, 27 Jun 2018 15:59:12 +0100
4 Subject: [PATCH] ASoC: Add generic RPI driver for simple soundcards.
6 The RPI simple sound card driver provides a generic ALSA SOC card driver
7 supporting a variety of Pi HAT soundcards. The intention is to avoid
8 the duplication of code for cards that can't be fully supported by
9 the soc simple/graph cards but are otherwise almost identical.
11 This initial commit adds support for the ADAU1977 ADC, Google VoiceHat,
12 HifiBerry AMP, HifiBerry DAC and RPI DAC.
14 Signed-off-by: Tim Gover <tim.gover@raspberrypi.org>
16 ASoC: Use correct card name in rpi-simple driver
18 Use the specific card name from drvdata instead of the snd_rpi_simple
20 rpi-simple-soundcard: Use nicer driver name "RPi-simple"
22 Rename the driver from "RPI simple soundcard" to "RPi-simple" so that
23 the driver name won't be mangled allowing to be used unaltered as the
26 ASoC: rpi-simple-soundcard: use modern dai_link style
28 Signed-off-by: Hui Wang <hui.wang@canonical.com>
30 sound/soc/bcm/rpi-simple-soundcard.c | 288 +++++++++++++++++++++++++++
31 1 file changed, 288 insertions(+)
32 create mode 100644 sound/soc/bcm/rpi-simple-soundcard.c
35 +++ b/sound/soc/bcm/rpi-simple-soundcard.c
37 +// SPDX-License-Identifier: GPL-2.0
39 + * rpi-simple-soundcard.c -- ALSA SoC Raspberry Pi soundcard.
41 + * Copyright (C) 2018 Raspberry Pi.
43 + * Authors: Tim Gover <tim.gover@raspberrypi.org>
46 + * hifiberry_amp.c, hifiberry_dac.c, rpi-dac.c
47 + * by Florian Meier <florian.meier@koalo.de>
49 + * googlevoicehat-soundcard.c
50 + * by Peter Malkin <petermalkin@google.com>
53 + * by Andrey Grodzovsky <andrey2805@gmail.com>
55 + * This program is free software; you can redistribute it and/or
56 + * modify it under the terms of the GNU General Public License
57 + * version 2 as published by the Free Software Foundation.
59 + * This program is distributed in the hope that it will be useful, but
60 + * WITHOUT ANY WARRANTY; without even the implied warranty of
61 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
62 + * General Public License for more details.
65 +#include <linux/module.h>
66 +#include <linux/platform_device.h>
68 +#include <sound/core.h>
69 +#include <sound/pcm.h>
70 +#include <sound/pcm_params.h>
71 +#include <sound/soc.h>
73 +/* Parameters for generic RPI functions */
74 +struct snd_rpi_simple_drvdata {
75 + struct snd_soc_dai_link *dai;
76 + const char* card_name;
77 + unsigned int fixed_bclk_ratio;
80 +static int snd_rpi_simple_init(struct snd_soc_pcm_runtime *rtd)
82 + struct snd_rpi_simple_drvdata *drvdata =
83 + snd_soc_card_get_drvdata(rtd->card);
84 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
86 + if (drvdata->fixed_bclk_ratio > 0)
87 + return snd_soc_dai_set_bclk_ratio(cpu_dai,
88 + drvdata->fixed_bclk_ratio);
93 +static int snd_rpi_simple_hw_params(struct snd_pcm_substream *substream,
94 + struct snd_pcm_hw_params *params)
96 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
97 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
98 + struct snd_rpi_simple_drvdata *drvdata;
99 + unsigned int sample_bits;
101 + drvdata = snd_soc_card_get_drvdata(rtd->card);
103 + if (drvdata->fixed_bclk_ratio > 0)
104 + return 0; // BCLK is configured in .init
106 + /* The simple drivers just set the bclk_ratio to sample_bits * 2 so
107 + * hard-code this for now. More complex drivers could just replace
108 + * the hw_params routine.
110 + sample_bits = snd_pcm_format_physical_width(params_format(params));
111 + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
114 +static struct snd_soc_ops snd_rpi_simple_ops = {
115 + .hw_params = snd_rpi_simple_hw_params,
118 +enum adau1977_clk_id {
122 +enum adau1977_sysclk_src {
123 + ADAU1977_SYSCLK_SRC_MCLK,
124 + ADAU1977_SYSCLK_SRC_LRCLK,
127 +static int adau1977_init(struct snd_soc_pcm_runtime *rtd)
130 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
132 + ret = snd_soc_dai_set_tdm_slot(codec_dai, 0, 0, 0, 0);
136 + return snd_soc_component_set_sysclk(codec_dai->component,
137 + ADAU1977_SYSCLK, ADAU1977_SYSCLK_SRC_MCLK,
138 + 11289600, SND_SOC_CLOCK_IN);
141 +SND_SOC_DAILINK_DEFS(adau1977,
142 + DAILINK_COMP_ARRAY(COMP_EMPTY()),
143 + DAILINK_COMP_ARRAY(COMP_CODEC("adau1977.1-0011", "adau1977-hifi")),
144 + DAILINK_COMP_ARRAY(COMP_EMPTY()));
146 +static struct snd_soc_dai_link snd_rpi_adau1977_dai[] = {
148 + .name = "adau1977",
149 + .stream_name = "ADAU1977",
150 + .init = adau1977_init,
151 + .dai_fmt = SND_SOC_DAIFMT_I2S |
152 + SND_SOC_DAIFMT_NB_NF |
153 + SND_SOC_DAIFMT_CBM_CFM,
154 + SND_SOC_DAILINK_REG(adau1977),
158 +static struct snd_rpi_simple_drvdata drvdata_adau1977 = {
159 + .card_name = "snd_rpi_adau1977_adc",
160 + .dai = snd_rpi_adau1977_dai,
163 +SND_SOC_DAILINK_DEFS(gvchat,
164 + DAILINK_COMP_ARRAY(COMP_EMPTY()),
165 + DAILINK_COMP_ARRAY(COMP_CODEC("voicehat-codec", "voicehat-hifi")),
166 + DAILINK_COMP_ARRAY(COMP_EMPTY()));
168 +static struct snd_soc_dai_link snd_googlevoicehat_soundcard_dai[] = {
170 + .name = "Google voiceHAT SoundCard",
171 + .stream_name = "Google voiceHAT SoundCard HiFi",
172 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
173 + SND_SOC_DAIFMT_CBS_CFS,
174 + SND_SOC_DAILINK_REG(gvchat),
178 +static struct snd_rpi_simple_drvdata drvdata_googlevoicehat = {
179 + .card_name = "snd_rpi_googlevoicehat_soundcard",
180 + .dai = snd_googlevoicehat_soundcard_dai,
183 +SND_SOC_DAILINK_DEFS(hifiberry_amp,
184 + DAILINK_COMP_ARRAY(COMP_EMPTY()),
185 + DAILINK_COMP_ARRAY(COMP_CODEC("tas5713.1-001b", "tas5713-hifi")),
186 + DAILINK_COMP_ARRAY(COMP_EMPTY()));
188 +static struct snd_soc_dai_link snd_hifiberry_amp_dai[] = {
190 + .name = "HifiBerry AMP",
191 + .stream_name = "HifiBerry AMP HiFi",
192 + .dai_fmt = SND_SOC_DAIFMT_I2S |
193 + SND_SOC_DAIFMT_NB_NF |
194 + SND_SOC_DAIFMT_CBS_CFS,
195 + SND_SOC_DAILINK_REG(hifiberry_amp),
199 +static struct snd_rpi_simple_drvdata drvdata_hifiberry_amp = {
200 + .card_name = "snd_rpi_hifiberry_amp",
201 + .dai = snd_hifiberry_amp_dai,
202 + .fixed_bclk_ratio = 64,
205 +SND_SOC_DAILINK_DEFS(hifiberry_dac,
206 + DAILINK_COMP_ARRAY(COMP_EMPTY()),
207 + DAILINK_COMP_ARRAY(COMP_CODEC("pcm5102a-codec", "pcm5102a-hifi")),
208 + DAILINK_COMP_ARRAY(COMP_EMPTY()));
210 +static struct snd_soc_dai_link snd_hifiberry_dac_dai[] = {
212 + .name = "HifiBerry DAC",
213 + .stream_name = "HifiBerry DAC HiFi",
214 + .dai_fmt = SND_SOC_DAIFMT_I2S |
215 + SND_SOC_DAIFMT_NB_NF |
216 + SND_SOC_DAIFMT_CBS_CFS,
217 + SND_SOC_DAILINK_REG(hifiberry_dac),
221 +static struct snd_rpi_simple_drvdata drvdata_hifiberry_dac = {
222 + .card_name = "snd_rpi_hifiberry_dac",
223 + .dai = snd_hifiberry_dac_dai,
226 +SND_SOC_DAILINK_DEFS(rpi_dac,
227 + DAILINK_COMP_ARRAY(COMP_EMPTY()),
228 + DAILINK_COMP_ARRAY(COMP_CODEC("pcm1794a-codec", "pcm1794a-hifi")),
229 + DAILINK_COMP_ARRAY(COMP_EMPTY()));
231 +static struct snd_soc_dai_link snd_rpi_dac_dai[] = {
234 + .stream_name = "RPi-DAC HiFi",
235 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
236 + SND_SOC_DAIFMT_CBS_CFS,
237 + SND_SOC_DAILINK_REG(rpi_dac),
241 +static struct snd_rpi_simple_drvdata drvdata_rpi_dac = {
242 + .card_name = "snd_rpi_rpi_dac",
243 + .dai = snd_rpi_dac_dai,
244 + .fixed_bclk_ratio = 64,
247 +static const struct of_device_id snd_rpi_simple_of_match[] = {
248 + { .compatible = "adi,adau1977-adc",
249 + .data = (void *) &drvdata_adau1977 },
250 + { .compatible = "googlevoicehat,googlevoicehat-soundcard",
251 + .data = (void *) &drvdata_googlevoicehat },
252 + { .compatible = "hifiberry,hifiberry-amp",
253 + .data = (void *) &drvdata_hifiberry_amp },
254 + { .compatible = "hifiberry,hifiberry-dac",
255 + .data = (void *) &drvdata_hifiberry_dac },
256 + { .compatible = "rpi,rpi-dac", &drvdata_rpi_dac},
260 +static struct snd_soc_card snd_rpi_simple = {
261 + .driver_name = "RPi-simple",
262 + .owner = THIS_MODULE,
264 + .num_links = 1, /* Only a single DAI supported at the moment */
267 +static int snd_rpi_simple_probe(struct platform_device *pdev)
270 + const struct of_device_id *of_id;
272 + snd_rpi_simple.dev = &pdev->dev;
273 + of_id = of_match_node(snd_rpi_simple_of_match, pdev->dev.of_node);
275 + if (pdev->dev.of_node && of_id->data) {
276 + struct device_node *i2s_node;
277 + struct snd_rpi_simple_drvdata *drvdata =
278 + (struct snd_rpi_simple_drvdata *) of_id->data;
279 + struct snd_soc_dai_link *dai = drvdata->dai;
281 + snd_soc_card_set_drvdata(&snd_rpi_simple, drvdata);
283 + /* More complex drivers might override individual functions */
285 + dai->init = snd_rpi_simple_init;
287 + dai->ops = &snd_rpi_simple_ops;
289 + snd_rpi_simple.name = drvdata->card_name;
291 + snd_rpi_simple.dai_link = dai;
292 + i2s_node = of_parse_phandle(pdev->dev.of_node,
293 + "i2s-controller", 0);
295 + pr_err("Failed to find i2s-controller DT node\n");
299 + dai->cpus->of_node = i2s_node;
300 + dai->platforms->of_node = i2s_node;
303 + ret = devm_snd_soc_register_card(&pdev->dev, &snd_rpi_simple);
304 + if (ret && ret != -EPROBE_DEFER)
305 + dev_err(&pdev->dev, "Failed to register card %d\n", ret);
310 +static struct platform_driver snd_rpi_simple_driver = {
312 + .name = "snd-rpi-simple",
313 + .owner = THIS_MODULE,
314 + .of_match_table = snd_rpi_simple_of_match,
316 + .probe = snd_rpi_simple_probe,
318 +MODULE_DEVICE_TABLE(of, snd_rpi_simple_of_match);
320 +module_platform_driver(snd_rpi_simple_driver);
322 +MODULE_AUTHOR("Tim Gover <tim.gover@raspberrypi.org>");
323 +MODULE_DESCRIPTION("ASoC Raspberry Pi simple soundcard driver ");
324 +MODULE_LICENSE("GPL v2");