1 From 8cf85a58066800ed638b4e4fca6f704275e0f588 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 086/806] 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 sound/soc/bcm/rpi-simple-soundcard.c | 268 +++++++++++++++++++++++++++
27 1 file changed, 268 insertions(+)
28 create mode 100644 sound/soc/bcm/rpi-simple-soundcard.c
31 +++ b/sound/soc/bcm/rpi-simple-soundcard.c
33 +// SPDX-License-Identifier: GPL-2.0
35 + * rpi-simple-soundcard.c -- ALSA SoC Raspberry Pi soundcard.
37 + * Copyright (C) 2018 Raspberry Pi.
39 + * Authors: Tim Gover <tim.gover@raspberrypi.org>
42 + * hifiberry_amp.c, hifiberry_dac.c, rpi-dac.c
43 + * by Florian Meier <florian.meier@koalo.de>
45 + * googlevoicehat-soundcard.c
46 + * by Peter Malkin <petermalkin@google.com>
49 + * by Andrey Grodzovsky <andrey2805@gmail.com>
51 + * This program is free software; you can redistribute it and/or
52 + * modify it under the terms of the GNU General Public License
53 + * version 2 as published by the Free Software Foundation.
55 + * This program is distributed in the hope that it will be useful, but
56 + * WITHOUT ANY WARRANTY; without even the implied warranty of
57 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
58 + * General Public License for more details.
61 +#include <linux/module.h>
62 +#include <linux/platform_device.h>
64 +#include <sound/core.h>
65 +#include <sound/pcm.h>
66 +#include <sound/pcm_params.h>
67 +#include <sound/soc.h>
69 +/* Parameters for generic RPI functions */
70 +struct snd_rpi_simple_drvdata {
71 + struct snd_soc_dai_link *dai;
72 + const char* card_name;
73 + unsigned int fixed_bclk_ratio;
76 +static int snd_rpi_simple_init(struct snd_soc_pcm_runtime *rtd)
78 + struct snd_rpi_simple_drvdata *drvdata =
79 + snd_soc_card_get_drvdata(rtd->card);
80 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
82 + if (drvdata->fixed_bclk_ratio > 0)
83 + return snd_soc_dai_set_bclk_ratio(cpu_dai,
84 + drvdata->fixed_bclk_ratio);
89 +static int snd_rpi_simple_hw_params(struct snd_pcm_substream *substream,
90 + struct snd_pcm_hw_params *params)
92 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
93 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
94 + struct snd_rpi_simple_drvdata *drvdata;
95 + unsigned int sample_bits;
97 + drvdata = snd_soc_card_get_drvdata(rtd->card);
99 + if (drvdata->fixed_bclk_ratio > 0)
100 + return 0; // BCLK is configured in .init
102 + /* The simple drivers just set the bclk_ratio to sample_bits * 2 so
103 + * hard-code this for now. More complex drivers could just replace
104 + * the hw_params routine.
106 + sample_bits = snd_pcm_format_physical_width(params_format(params));
107 + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
110 +static struct snd_soc_ops snd_rpi_simple_ops = {
111 + .hw_params = snd_rpi_simple_hw_params,
114 +enum adau1977_clk_id {
118 +enum adau1977_sysclk_src {
119 + ADAU1977_SYSCLK_SRC_MCLK,
120 + ADAU1977_SYSCLK_SRC_LRCLK,
123 +static int adau1977_init(struct snd_soc_pcm_runtime *rtd)
126 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
128 + ret = snd_soc_dai_set_tdm_slot(codec_dai, 0, 0, 0, 0);
132 + return snd_soc_component_set_sysclk(codec_dai->component,
133 + ADAU1977_SYSCLK, ADAU1977_SYSCLK_SRC_MCLK,
134 + 11289600, SND_SOC_CLOCK_IN);
137 +static struct snd_soc_dai_link snd_rpi_adau1977_dai[] = {
139 + .name = "adau1977",
140 + .stream_name = "ADAU1977",
141 + .codec_dai_name = "adau1977-hifi",
142 + .codec_name = "adau1977.1-0011",
143 + .init = adau1977_init,
144 + .dai_fmt = SND_SOC_DAIFMT_I2S |
145 + SND_SOC_DAIFMT_NB_NF |
146 + SND_SOC_DAIFMT_CBM_CFM,
150 +static struct snd_rpi_simple_drvdata drvdata_adau1977 = {
151 + .card_name = "snd_rpi_adau1977_adc",
152 + .dai = snd_rpi_adau1977_dai,
155 +static struct snd_soc_dai_link snd_googlevoicehat_soundcard_dai[] = {
157 + .name = "Google voiceHAT SoundCard",
158 + .stream_name = "Google voiceHAT SoundCard HiFi",
159 + .codec_dai_name = "voicehat-hifi",
160 + .codec_name = "voicehat-codec",
161 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
162 + SND_SOC_DAIFMT_CBS_CFS,
166 +static struct snd_rpi_simple_drvdata drvdata_googlevoicehat = {
167 + .card_name = "snd_rpi_googlevoicehat_soundcard",
168 + .dai = snd_googlevoicehat_soundcard_dai,
171 +static struct snd_soc_dai_link snd_hifiberry_amp_dai[] = {
173 + .name = "HifiBerry AMP",
174 + .stream_name = "HifiBerry AMP HiFi",
175 + .codec_dai_name = "tas5713-hifi",
176 + .codec_name = "tas5713.1-001b",
177 + .dai_fmt = SND_SOC_DAIFMT_I2S |
178 + SND_SOC_DAIFMT_NB_NF |
179 + SND_SOC_DAIFMT_CBS_CFS,
183 +static struct snd_rpi_simple_drvdata drvdata_hifiberry_amp = {
184 + .card_name = "snd_rpi_hifiberry_amp",
185 + .dai = snd_hifiberry_amp_dai,
186 + .fixed_bclk_ratio = 64,
189 +static struct snd_soc_dai_link snd_hifiberry_dac_dai[] = {
191 + .name = "HifiBerry DAC",
192 + .stream_name = "HifiBerry DAC HiFi",
193 + .codec_dai_name = "pcm5102a-hifi",
194 + .codec_name = "pcm5102a-codec",
195 + .dai_fmt = SND_SOC_DAIFMT_I2S |
196 + SND_SOC_DAIFMT_NB_NF |
197 + SND_SOC_DAIFMT_CBS_CFS,
201 +static struct snd_rpi_simple_drvdata drvdata_hifiberry_dac = {
202 + .card_name = "snd_rpi_hifiberry_dac",
203 + .dai = snd_hifiberry_dac_dai,
206 +static struct snd_soc_dai_link snd_rpi_dac_dai[] = {
209 + .stream_name = "RPi-DAC HiFi",
210 + .codec_dai_name = "pcm1794a-hifi",
211 + .codec_name = "pcm1794a-codec",
212 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
213 + SND_SOC_DAIFMT_CBS_CFS,
217 +static struct snd_rpi_simple_drvdata drvdata_rpi_dac = {
218 + .card_name = "snd_rpi_rpi_dac",
219 + .dai = snd_rpi_dac_dai,
220 + .fixed_bclk_ratio = 64,
223 +static const struct of_device_id snd_rpi_simple_of_match[] = {
224 + { .compatible = "adi,adau1977-adc",
225 + .data = (void *) &drvdata_adau1977 },
226 + { .compatible = "googlevoicehat,googlevoicehat-soundcard",
227 + .data = (void *) &drvdata_googlevoicehat },
228 + { .compatible = "hifiberry,hifiberry-amp",
229 + .data = (void *) &drvdata_hifiberry_amp },
230 + { .compatible = "hifiberry,hifiberry-dac",
231 + .data = (void *) &drvdata_hifiberry_dac },
232 + { .compatible = "rpi,rpi-dac", &drvdata_rpi_dac},
236 +static struct snd_soc_card snd_rpi_simple = {
237 + .driver_name = "RPi-simple",
238 + .owner = THIS_MODULE,
240 + .num_links = 1, /* Only a single DAI supported at the moment */
243 +static int snd_rpi_simple_probe(struct platform_device *pdev)
246 + const struct of_device_id *of_id;
248 + snd_rpi_simple.dev = &pdev->dev;
249 + of_id = of_match_node(snd_rpi_simple_of_match, pdev->dev.of_node);
251 + if (pdev->dev.of_node && of_id->data) {
252 + struct device_node *i2s_node;
253 + struct snd_rpi_simple_drvdata *drvdata =
254 + (struct snd_rpi_simple_drvdata *) of_id->data;
255 + struct snd_soc_dai_link *dai = drvdata->dai;
257 + snd_soc_card_set_drvdata(&snd_rpi_simple, drvdata);
259 + /* More complex drivers might override individual functions */
261 + dai->init = snd_rpi_simple_init;
263 + dai->ops = &snd_rpi_simple_ops;
265 + snd_rpi_simple.name = drvdata->card_name;
267 + snd_rpi_simple.dai_link = dai;
268 + i2s_node = of_parse_phandle(pdev->dev.of_node,
269 + "i2s-controller", 0);
271 + pr_err("Failed to find i2s-controller DT node\n");
275 + dai->cpu_of_node = i2s_node;
276 + dai->platform_of_node = i2s_node;
279 + ret = devm_snd_soc_register_card(&pdev->dev, &snd_rpi_simple);
280 + if (ret && ret != -EPROBE_DEFER)
281 + dev_err(&pdev->dev, "Failed to register card %d\n", ret);
286 +static struct platform_driver snd_rpi_simple_driver = {
288 + .name = "snd-rpi-simple",
289 + .owner = THIS_MODULE,
290 + .of_match_table = snd_rpi_simple_of_match,
292 + .probe = snd_rpi_simple_probe,
294 +MODULE_DEVICE_TABLE(of, snd_rpi_simple_of_match);
296 +module_platform_driver(snd_rpi_simple_driver);
298 +MODULE_AUTHOR("Tim Gover <tim.gover@raspberrypi.org>");
299 +MODULE_DESCRIPTION("ASoC Raspberry Pi simple soundcard driver ");
300 +MODULE_LICENSE("GPL v2");