bcm27xx: add linux 5.4 support
[openwrt/staging/jogo.git] / target / linux / bcm27xx / patches-5.4 / 950-0136-Added-driver-for-the-HiFiBerry-DAC-ADC-2694.patch
1 From b6eb457e794f8c48b7004cc88aa82889bcd5f8e6 Mon Sep 17 00:00:00 2001
2 From: HiFiBerry <info@hifiberry.com>
3 Date: Mon, 8 Oct 2018 18:10:12 +0200
4 Subject: [PATCH] Added driver for the HiFiBerry DAC+ ADC (#2694)
5
6 Signed-off-by: Daniel Matuschek <daniel@hifiberry.com>
7
8 hifiberry_dacplusadc: switch to snd_soc_dai_set_bclk_ratio
9
10 Signed-off-by: Matthias Reichl <hias@horus.com>
11
12 ASoC: hifiberry_dacplusadc: fix DAI link setup
13
14 The driver only defines a single DAI link and the code that tries
15 to setup the second (non-existent) DAI link looks wrong - using dmic
16 as a CPU/platform driver doesn't make any sense.
17
18 The DT overlay doesn't define a dmic property, so the code was never
19 executed (otherwise it would have resulted in a memory corruption).
20
21 So drop the offending code to prevent issues if a dmic property
22 should be added to the DT overlay.
23
24 Signed-off-by: Matthias Reichl <hias@horus.com>
25
26 ASoC: hifiberry_dacplusadc: use modern dai_link style
27
28 Signed-off-by: Matthias Reichl <hias@horus.com>
29 ---
30 sound/soc/bcm/Kconfig | 9 +
31 sound/soc/bcm/Makefile | 2 +
32 sound/soc/bcm/hifiberry_dacplusadc.c | 390 +++++++++++++++++++++++++++
33 3 files changed, 401 insertions(+)
34 create mode 100644 sound/soc/bcm/hifiberry_dacplusadc.c
35
36 --- a/sound/soc/bcm/Kconfig
37 +++ b/sound/soc/bcm/Kconfig
38 @@ -41,6 +41,15 @@ config SND_BCM2708_SOC_HIFIBERRY_DACPLUS
39 help
40 Say Y or M if you want to add support for HifiBerry DAC+.
41
42 +config SND_BCM2708_SOC_HIFIBERRY_DACPLUSADC
43 + tristate "Support for HifiBerry DAC+ADC"
44 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
45 + select SND_SOC_PCM512x_I2C
46 + select SND_SOC_DMIC
47 + select COMMON_CLK_HIFIBERRY_DACPRO
48 + help
49 + Say Y or M if you want to add support for HifiBerry DAC+ADC.
50 +
51 config SND_BCM2708_SOC_HIFIBERRY_DIGI
52 tristate "Support for HifiBerry Digi"
53 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
54 --- a/sound/soc/bcm/Makefile
55 +++ b/sound/soc/bcm/Makefile
56 @@ -14,6 +14,7 @@ snd-soc-googlevoicehat-codec-objs := goo
57
58 # BCM2708 Machine Support
59 snd-soc-hifiberry-dacplus-objs := hifiberry_dacplus.o
60 +snd-soc-hifiberry-dacplusadc-objs := hifiberry_dacplusadc.o
61 snd-soc-justboom-dac-objs := justboom-dac.o
62 snd-soc-rpi-cirrus-objs := rpi-cirrus.o
63 snd-soc-rpi-proto-objs := rpi-proto.o
64 @@ -35,6 +36,7 @@ snd-soc-rpi-wm8804-soundcard-objs := rpi
65
66 obj-$(CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD) += snd-soc-googlevoicehat-codec.o
67 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) += snd-soc-hifiberry-dacplus.o
68 +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUSADC) += snd-soc-hifiberry-dacplusadc.o
69 obj-$(CONFIG_SND_BCM2708_SOC_JUSTBOOM_DAC) += snd-soc-justboom-dac.o
70 obj-$(CONFIG_SND_BCM2708_SOC_RPI_CIRRUS) += snd-soc-rpi-cirrus.o
71 obj-$(CONFIG_SND_BCM2708_SOC_RPI_PROTO) += snd-soc-rpi-proto.o
72 --- /dev/null
73 +++ b/sound/soc/bcm/hifiberry_dacplusadc.c
74 @@ -0,0 +1,390 @@
75 +/*
76 + * ASoC Driver for HiFiBerry DAC+ / DAC Pro with ADC
77 + *
78 + * Author: Daniel Matuschek, Stuart MacLean <stuart@hifiberry.com>
79 + * Copyright 2014-2015
80 + * based on code by Florian Meier <florian.meier@koalo.de>
81 + * ADC added by Joerg Schambacher <joscha@schambacher.com>
82 + * Copyright 2018
83 + *
84 + * This program is free software; you can redistribute it and/or
85 + * modify it under the terms of the GNU General Public License
86 + * version 2 as published by the Free Software Foundation.
87 + *
88 + * This program is distributed in the hope that it will be useful, but
89 + * WITHOUT ANY WARRANTY; without even the implied warranty of
90 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
91 + * General Public License for more details.
92 + */
93 +
94 +#include <linux/module.h>
95 +#include <linux/platform_device.h>
96 +#include <linux/kernel.h>
97 +#include <linux/clk.h>
98 +#include <linux/kernel.h>
99 +#include <linux/module.h>
100 +#include <linux/of.h>
101 +#include <linux/slab.h>
102 +#include <linux/delay.h>
103 +
104 +#include <sound/core.h>
105 +#include <sound/pcm.h>
106 +#include <sound/pcm_params.h>
107 +#include <sound/soc.h>
108 +#include <sound/jack.h>
109 +
110 +#include "../codecs/pcm512x.h"
111 +
112 +#define HIFIBERRY_DACPRO_NOCLOCK 0
113 +#define HIFIBERRY_DACPRO_CLK44EN 1
114 +#define HIFIBERRY_DACPRO_CLK48EN 2
115 +
116 +struct platform_device *dmic_codec_dev;
117 +
118 +struct pcm512x_priv {
119 + struct regmap *regmap;
120 + struct clk *sclk;
121 +};
122 +
123 +/* Clock rate of CLK44EN attached to GPIO6 pin */
124 +#define CLK_44EN_RATE 22579200UL
125 +/* Clock rate of CLK48EN attached to GPIO3 pin */
126 +#define CLK_48EN_RATE 24576000UL
127 +
128 +static bool slave;
129 +static bool snd_rpi_hifiberry_is_dacpro;
130 +static bool digital_gain_0db_limit = true;
131 +
132 +static void snd_rpi_hifiberry_dacplusadc_select_clk(struct snd_soc_component *component,
133 + int clk_id)
134 +{
135 + switch (clk_id) {
136 + case HIFIBERRY_DACPRO_NOCLOCK:
137 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x00);
138 + break;
139 + case HIFIBERRY_DACPRO_CLK44EN:
140 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x20);
141 + break;
142 + case HIFIBERRY_DACPRO_CLK48EN:
143 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x04);
144 + break;
145 + }
146 +}
147 +
148 +static void snd_rpi_hifiberry_dacplusadc_clk_gpio(struct snd_soc_component *component)
149 +{
150 + snd_soc_component_update_bits(component, PCM512x_GPIO_EN, 0x24, 0x24);
151 + snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_3, 0x0f, 0x02);
152 + snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_6, 0x0f, 0x02);
153 +}
154 +
155 +static bool snd_rpi_hifiberry_dacplusadc_is_sclk(struct snd_soc_component *component)
156 +{
157 + unsigned int sck;
158 +
159 + snd_soc_component_read(component, PCM512x_RATE_DET_4, &sck);
160 + return (!(sck & 0x40));
161 +}
162 +
163 +static bool snd_rpi_hifiberry_dacplusadc_is_sclk_sleep(
164 + struct snd_soc_component *component)
165 +{
166 + msleep(2);
167 + return snd_rpi_hifiberry_dacplusadc_is_sclk(component);
168 +}
169 +
170 +static bool snd_rpi_hifiberry_dacplusadc_is_pro_card(struct snd_soc_component *component)
171 +{
172 + bool isClk44EN, isClk48En, isNoClk;
173 +
174 + snd_rpi_hifiberry_dacplusadc_clk_gpio(component);
175 +
176 + snd_rpi_hifiberry_dacplusadc_select_clk(component, HIFIBERRY_DACPRO_CLK44EN);
177 + isClk44EN = snd_rpi_hifiberry_dacplusadc_is_sclk_sleep(component);
178 +
179 + snd_rpi_hifiberry_dacplusadc_select_clk(component, HIFIBERRY_DACPRO_NOCLOCK);
180 + isNoClk = snd_rpi_hifiberry_dacplusadc_is_sclk_sleep(component);
181 +
182 + snd_rpi_hifiberry_dacplusadc_select_clk(component, HIFIBERRY_DACPRO_CLK48EN);
183 + isClk48En = snd_rpi_hifiberry_dacplusadc_is_sclk_sleep(component);
184 +
185 + return (isClk44EN && isClk48En && !isNoClk);
186 +}
187 +
188 +static int snd_rpi_hifiberry_dacplusadc_clk_for_rate(int sample_rate)
189 +{
190 + int type;
191 +
192 + switch (sample_rate) {
193 + case 11025:
194 + case 22050:
195 + case 44100:
196 + case 88200:
197 + case 176400:
198 + case 352800:
199 + type = HIFIBERRY_DACPRO_CLK44EN;
200 + break;
201 + default:
202 + type = HIFIBERRY_DACPRO_CLK48EN;
203 + break;
204 + }
205 + return type;
206 +}
207 +
208 +static void snd_rpi_hifiberry_dacplusadc_set_sclk(struct snd_soc_component *component,
209 + int sample_rate)
210 +{
211 + struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
212 +
213 + if (!IS_ERR(pcm512x->sclk)) {
214 + int ctype;
215 +
216 + ctype = snd_rpi_hifiberry_dacplusadc_clk_for_rate(sample_rate);
217 + clk_set_rate(pcm512x->sclk, (ctype == HIFIBERRY_DACPRO_CLK44EN)
218 + ? CLK_44EN_RATE : CLK_48EN_RATE);
219 + snd_rpi_hifiberry_dacplusadc_select_clk(component, ctype);
220 + }
221 +}
222 +
223 +static int snd_rpi_hifiberry_dacplusadc_init(struct snd_soc_pcm_runtime *rtd)
224 +{
225 + struct snd_soc_component *component = rtd->codec_dai->component;
226 + struct pcm512x_priv *priv;
227 +
228 + if (slave)
229 + snd_rpi_hifiberry_is_dacpro = false;
230 + else
231 + snd_rpi_hifiberry_is_dacpro =
232 + snd_rpi_hifiberry_dacplusadc_is_pro_card(component);
233 +
234 + if (snd_rpi_hifiberry_is_dacpro) {
235 + struct snd_soc_dai_link *dai = rtd->dai_link;
236 +
237 + dai->name = "HiFiBerry ADCDAC+ Pro";
238 + dai->stream_name = "HiFiBerry ADCDAC+ Pro HiFi";
239 + dai->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
240 + | SND_SOC_DAIFMT_CBM_CFM;
241 +
242 + snd_soc_component_update_bits(component, PCM512x_BCLK_LRCLK_CFG, 0x31, 0x11);
243 + snd_soc_component_update_bits(component, PCM512x_MASTER_MODE, 0x03, 0x03);
244 + snd_soc_component_update_bits(component, PCM512x_MASTER_CLKDIV_2, 0x7f, 63);
245 + } else {
246 + priv = snd_soc_component_get_drvdata(component);
247 + priv->sclk = ERR_PTR(-ENOENT);
248 + }
249 +
250 + snd_soc_component_update_bits(component, PCM512x_GPIO_EN, 0x08, 0x08);
251 + snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_4, 0x0f, 0x02);
252 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
253 +
254 + if (digital_gain_0db_limit) {
255 + int ret;
256 + struct snd_soc_card *card = rtd->card;
257 +
258 + ret = snd_soc_limit_volume(card, "Digital Playback Volume", 207);
259 + if (ret < 0)
260 + dev_warn(card->dev, "Failed to set volume limit: %d\n", ret);
261 + }
262 +
263 + return 0;
264 +}
265 +
266 +static int snd_rpi_hifiberry_dacplusadc_update_rate_den(
267 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
268 +{
269 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
270 + struct snd_soc_component *component = rtd->codec_dai->component;
271 + struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
272 + struct snd_ratnum *rats_no_pll;
273 + unsigned int num = 0, den = 0;
274 + int err;
275 +
276 + rats_no_pll = devm_kzalloc(rtd->dev, sizeof(*rats_no_pll), GFP_KERNEL);
277 + if (!rats_no_pll)
278 + return -ENOMEM;
279 +
280 + rats_no_pll->num = clk_get_rate(pcm512x->sclk) / 64;
281 + rats_no_pll->den_min = 1;
282 + rats_no_pll->den_max = 128;
283 + rats_no_pll->den_step = 1;
284 +
285 + err = snd_interval_ratnum(hw_param_interval(params,
286 + SNDRV_PCM_HW_PARAM_RATE), 1, rats_no_pll, &num, &den);
287 + if (err >= 0 && den) {
288 + params->rate_num = num;
289 + params->rate_den = den;
290 + }
291 +
292 + devm_kfree(rtd->dev, rats_no_pll);
293 + return 0;
294 +}
295 +
296 +static int snd_rpi_hifiberry_dacplusadc_hw_params(
297 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
298 +{
299 + int ret = 0;
300 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
301 + int channels = params_channels(params);
302 + int width = 32;
303 +
304 + if (snd_rpi_hifiberry_is_dacpro) {
305 + struct snd_soc_component *component = rtd->codec_dai->component;
306 +
307 + width = snd_pcm_format_physical_width(params_format(params));
308 +
309 + snd_rpi_hifiberry_dacplusadc_set_sclk(component,
310 + params_rate(params));
311 +
312 + ret = snd_rpi_hifiberry_dacplusadc_update_rate_den(
313 + substream, params);
314 + }
315 +
316 + ret = snd_soc_dai_set_bclk_ratio(rtd->cpu_dai, channels * width);
317 + if (ret)
318 + return ret;
319 + ret = snd_soc_dai_set_bclk_ratio(rtd->codec_dai, channels * width);
320 + return ret;
321 +}
322 +
323 +static int hifiberry_dacplusadc_LED_cnt;
324 +
325 +static int snd_rpi_hifiberry_dacplusadc_startup(
326 + struct snd_pcm_substream *substream)
327 +{
328 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
329 + struct snd_soc_component *component = rtd->codec_dai->component;
330 +
331 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1,
332 + 0x08, 0x08);
333 + hifiberry_dacplusadc_LED_cnt++;
334 + return 0;
335 +}
336 +
337 +static void snd_rpi_hifiberry_dacplusadc_shutdown(
338 + struct snd_pcm_substream *substream)
339 +{
340 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
341 + struct snd_soc_component *component = rtd->codec_dai->component;
342 +
343 + hifiberry_dacplusadc_LED_cnt--;
344 + if (!hifiberry_dacplusadc_LED_cnt)
345 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1,
346 + 0x08, 0x00);
347 +}
348 +
349 +/* machine stream operations */
350 +static struct snd_soc_ops snd_rpi_hifiberry_dacplusadc_ops = {
351 + .hw_params = snd_rpi_hifiberry_dacplusadc_hw_params,
352 + .startup = snd_rpi_hifiberry_dacplusadc_startup,
353 + .shutdown = snd_rpi_hifiberry_dacplusadc_shutdown,
354 +};
355 +
356 +SND_SOC_DAILINK_DEFS(hifi,
357 + DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
358 + DAILINK_COMP_ARRAY(COMP_CODEC("pcm512x.1-004d", "pcm512x-hifi"),
359 + COMP_CODEC("dmic-codec", "dmic-hifi")),
360 + DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2708-i2s.0")));
361 +
362 +static struct snd_soc_dai_link snd_rpi_hifiberry_dacplusadc_dai[] = {
363 +{
364 + .name = "HiFiBerry DAC+ADC",
365 + .stream_name = "HiFiBerry DAC+ADC HiFi",
366 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
367 + SND_SOC_DAIFMT_CBS_CFS,
368 + .ops = &snd_rpi_hifiberry_dacplusadc_ops,
369 + .init = snd_rpi_hifiberry_dacplusadc_init,
370 + SND_SOC_DAILINK_REG(hifi),
371 +},
372 +};
373 +
374 +/* audio machine driver */
375 +static struct snd_soc_card snd_rpi_hifiberry_dacplusadc = {
376 + .name = "snd_rpi_hifiberry_dacplusadc",
377 + .driver_name = "HifiberryDacpAdc",
378 + .owner = THIS_MODULE,
379 + .dai_link = snd_rpi_hifiberry_dacplusadc_dai,
380 + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dacplusadc_dai),
381 +};
382 +
383 +
384 +static int snd_rpi_hifiberry_dacplusadc_probe(struct platform_device *pdev)
385 +{
386 + int ret = 0;
387 +
388 + snd_rpi_hifiberry_dacplusadc.dev = &pdev->dev;
389 + if (pdev->dev.of_node) {
390 + struct device_node *i2s_node;
391 + struct snd_soc_dai_link *dai;
392 +
393 + dai = &snd_rpi_hifiberry_dacplusadc_dai[0];
394 + i2s_node = of_parse_phandle(pdev->dev.of_node,
395 + "i2s-controller", 0);
396 + if (i2s_node) {
397 + dai->cpus->of_node = i2s_node;
398 + dai->platforms->of_node = i2s_node;
399 + dai->cpus->dai_name = NULL;
400 + dai->platforms->name = NULL;
401 + }
402 + }
403 + digital_gain_0db_limit = !of_property_read_bool(
404 + pdev->dev.of_node, "hifiberry,24db_digital_gain");
405 + slave = of_property_read_bool(pdev->dev.of_node,
406 + "hifiberry-dacplusadc,slave");
407 +
408 + ret = devm_snd_soc_register_card(&pdev->dev,
409 + &snd_rpi_hifiberry_dacplusadc);
410 + if (ret && ret != -EPROBE_DEFER)
411 + dev_err(&pdev->dev,
412 + "snd_soc_register_card() failed: %d\n", ret);
413 +
414 + return ret;
415 +}
416 +
417 +static const struct of_device_id snd_rpi_hifiberry_dacplusadc_of_match[] = {
418 + { .compatible = "hifiberry,hifiberry-dacplusadc", },
419 + {},
420 +};
421 +
422 +MODULE_DEVICE_TABLE(of, snd_rpi_hifiberry_dacplusadc_of_match);
423 +
424 +static struct platform_driver snd_rpi_hifiberry_dacplusadc_driver = {
425 + .driver = {
426 + .name = "snd-rpi-hifiberry-dacplusadc",
427 + .owner = THIS_MODULE,
428 + .of_match_table = snd_rpi_hifiberry_dacplusadc_of_match,
429 + },
430 + .probe = snd_rpi_hifiberry_dacplusadc_probe,
431 +};
432 +
433 +static int __init hifiberry_dacplusadc_init(void)
434 +{
435 + int ret;
436 +
437 + dmic_codec_dev = platform_device_register_simple("dmic-codec", -1, NULL,
438 + 0);
439 + if (IS_ERR(dmic_codec_dev)) {
440 + pr_err("%s: dmic-codec device registration failed\n", __func__);
441 + return PTR_ERR(dmic_codec_dev);
442 + }
443 +
444 + ret = platform_driver_register(&snd_rpi_hifiberry_dacplusadc_driver);
445 + if (ret) {
446 + pr_err("%s: platform driver registration failed\n", __func__);
447 + platform_device_unregister(dmic_codec_dev);
448 + }
449 +
450 + return ret;
451 +}
452 +module_init(hifiberry_dacplusadc_init);
453 +
454 +static void __exit hifiberry_dacplusadc_exit(void)
455 +{
456 + platform_driver_unregister(&snd_rpi_hifiberry_dacplusadc_driver);
457 + platform_device_unregister(dmic_codec_dev);
458 +}
459 +module_exit(hifiberry_dacplusadc_exit);
460 +
461 +MODULE_AUTHOR("Joerg Schambacher <joscha@schambacher.com>");
462 +MODULE_AUTHOR("Daniel Matuschek <daniel@hifiberry.com>");
463 +MODULE_DESCRIPTION("ASoC Driver for HiFiBerry DAC+ADC");
464 +MODULE_LICENSE("GPL v2");