brcm63xx: rename target to bcm63xx
[openwrt/staging/wigyori.git] / target / linux / brcm2708 / patches-4.19 / 950-0084-ASoC-Add-generic-RPI-driver-for-simple-soundcards.patch
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] ASoC: Add generic RPI driver for simple soundcards.
5
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.
10
11 This initial commit adds support for the ADAU1977 ADC, Google VoiceHat,
12 HifiBerry AMP, HifiBerry DAC and RPI DAC.
13
14 Signed-off-by: Tim Gover <tim.gover@raspberrypi.org>
15
16 ASoC: Use correct card name in rpi-simple driver
17
18 Use the specific card name from drvdata instead of the snd_rpi_simple
19
20 rpi-simple-soundcard: Use nicer driver name "RPi-simple"
21
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
24 card conf filename.
25 ---
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
29
30 --- /dev/null
31 +++ b/sound/soc/bcm/rpi-simple-soundcard.c
32 @@ -0,0 +1,268 @@
33 +// SPDX-License-Identifier: GPL-2.0
34 +/*
35 + * rpi-simple-soundcard.c -- ALSA SoC Raspberry Pi soundcard.
36 + *
37 + * Copyright (C) 2018 Raspberry Pi.
38 + *
39 + * Authors: Tim Gover <tim.gover@raspberrypi.org>
40 + *
41 + * Based on code:
42 + * hifiberry_amp.c, hifiberry_dac.c, rpi-dac.c
43 + * by Florian Meier <florian.meier@koalo.de>
44 + *
45 + * googlevoicehat-soundcard.c
46 + * by Peter Malkin <petermalkin@google.com>
47 + *
48 + * adau1977-adc.c
49 + * by Andrey Grodzovsky <andrey2805@gmail.com>
50 + *
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.
54 + *
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.
59 + */
60 +
61 +#include <linux/module.h>
62 +#include <linux/platform_device.h>
63 +
64 +#include <sound/core.h>
65 +#include <sound/pcm.h>
66 +#include <sound/pcm_params.h>
67 +#include <sound/soc.h>
68 +
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;
74 +};
75 +
76 +static int snd_rpi_simple_init(struct snd_soc_pcm_runtime *rtd)
77 +{
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;
81 +
82 + if (drvdata->fixed_bclk_ratio > 0)
83 + return snd_soc_dai_set_bclk_ratio(cpu_dai,
84 + drvdata->fixed_bclk_ratio);
85 +
86 + return 0;
87 +}
88 +
89 +static int snd_rpi_simple_hw_params(struct snd_pcm_substream *substream,
90 + struct snd_pcm_hw_params *params)
91 +{
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;
96 +
97 + drvdata = snd_soc_card_get_drvdata(rtd->card);
98 +
99 + if (drvdata->fixed_bclk_ratio > 0)
100 + return 0; // BCLK is configured in .init
101 +
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.
105 + */
106 + sample_bits = snd_pcm_format_physical_width(params_format(params));
107 + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
108 +}
109 +
110 +static struct snd_soc_ops snd_rpi_simple_ops = {
111 + .hw_params = snd_rpi_simple_hw_params,
112 +};
113 +
114 +enum adau1977_clk_id {
115 + ADAU1977_SYSCLK,
116 +};
117 +
118 +enum adau1977_sysclk_src {
119 + ADAU1977_SYSCLK_SRC_MCLK,
120 + ADAU1977_SYSCLK_SRC_LRCLK,
121 +};
122 +
123 +static int adau1977_init(struct snd_soc_pcm_runtime *rtd)
124 +{
125 + int ret;
126 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
127 +
128 + ret = snd_soc_dai_set_tdm_slot(codec_dai, 0, 0, 0, 0);
129 + if (ret < 0)
130 + return ret;
131 +
132 + return snd_soc_component_set_sysclk(codec_dai->component,
133 + ADAU1977_SYSCLK, ADAU1977_SYSCLK_SRC_MCLK,
134 + 11289600, SND_SOC_CLOCK_IN);
135 +}
136 +
137 +static struct snd_soc_dai_link snd_rpi_adau1977_dai[] = {
138 + {
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,
147 + },
148 +};
149 +
150 +static struct snd_rpi_simple_drvdata drvdata_adau1977 = {
151 + .card_name = "snd_rpi_adau1977_adc",
152 + .dai = snd_rpi_adau1977_dai,
153 +};
154 +
155 +static struct snd_soc_dai_link snd_googlevoicehat_soundcard_dai[] = {
156 +{
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,
163 +},
164 +};
165 +
166 +static struct snd_rpi_simple_drvdata drvdata_googlevoicehat = {
167 + .card_name = "snd_rpi_googlevoicehat_soundcard",
168 + .dai = snd_googlevoicehat_soundcard_dai,
169 +};
170 +
171 +static struct snd_soc_dai_link snd_hifiberry_amp_dai[] = {
172 + {
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,
180 + },
181 +};
182 +
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,
187 +};
188 +
189 +static struct snd_soc_dai_link snd_hifiberry_dac_dai[] = {
190 + {
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,
198 + },
199 +};
200 +
201 +static struct snd_rpi_simple_drvdata drvdata_hifiberry_dac = {
202 + .card_name = "snd_rpi_hifiberry_dac",
203 + .dai = snd_hifiberry_dac_dai,
204 +};
205 +
206 +static struct snd_soc_dai_link snd_rpi_dac_dai[] = {
207 +{
208 + .name = "RPi-DAC",
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,
214 +},
215 +};
216 +
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,
221 +};
222 +
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},
233 + {},
234 +};
235 +
236 +static struct snd_soc_card snd_rpi_simple = {
237 + .driver_name = "RPi-simple",
238 + .owner = THIS_MODULE,
239 + .dai_link = NULL,
240 + .num_links = 1, /* Only a single DAI supported at the moment */
241 +};
242 +
243 +static int snd_rpi_simple_probe(struct platform_device *pdev)
244 +{
245 + int ret = 0;
246 + const struct of_device_id *of_id;
247 +
248 + snd_rpi_simple.dev = &pdev->dev;
249 + of_id = of_match_node(snd_rpi_simple_of_match, pdev->dev.of_node);
250 +
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;
256 +
257 + snd_soc_card_set_drvdata(&snd_rpi_simple, drvdata);
258 +
259 + /* More complex drivers might override individual functions */
260 + if (!dai->init)
261 + dai->init = snd_rpi_simple_init;
262 + if (!dai->ops)
263 + dai->ops = &snd_rpi_simple_ops;
264 +
265 + snd_rpi_simple.name = drvdata->card_name;
266 +
267 + snd_rpi_simple.dai_link = dai;
268 + i2s_node = of_parse_phandle(pdev->dev.of_node,
269 + "i2s-controller", 0);
270 + if (!i2s_node) {
271 + pr_err("Failed to find i2s-controller DT node\n");
272 + return -ENODEV;
273 + }
274 +
275 + dai->cpu_of_node = i2s_node;
276 + dai->platform_of_node = i2s_node;
277 + }
278 +
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);
282 +
283 + return ret;
284 +}
285 +
286 +static struct platform_driver snd_rpi_simple_driver = {
287 + .driver = {
288 + .name = "snd-rpi-simple",
289 + .owner = THIS_MODULE,
290 + .of_match_table = snd_rpi_simple_of_match,
291 + },
292 + .probe = snd_rpi_simple_probe,
293 +};
294 +MODULE_DEVICE_TABLE(of, snd_rpi_simple_of_match);
295 +
296 +module_platform_driver(snd_rpi_simple_driver);
297 +
298 +MODULE_AUTHOR("Tim Gover <tim.gover@raspberrypi.org>");
299 +MODULE_DESCRIPTION("ASoC Raspberry Pi simple soundcard driver ");
300 +MODULE_LICENSE("GPL v2");