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)
6 Signed-off-by: Daniel Matuschek <daniel@hifiberry.com>
8 hifiberry_dacplusadc: switch to snd_soc_dai_set_bclk_ratio
10 Signed-off-by: Matthias Reichl <hias@horus.com>
12 ASoC: hifiberry_dacplusadc: fix DAI link setup
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.
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).
21 So drop the offending code to prevent issues if a dmic property
22 should be added to the DT overlay.
24 Signed-off-by: Matthias Reichl <hias@horus.com>
26 ASoC: hifiberry_dacplusadc: use modern dai_link style
28 Signed-off-by: Matthias Reichl <hias@horus.com>
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
36 --- a/sound/soc/bcm/Kconfig
37 +++ b/sound/soc/bcm/Kconfig
38 @@ -41,6 +41,15 @@ config SND_BCM2708_SOC_HIFIBERRY_DACPLUS
40 Say Y or M if you want to add support for HifiBerry DAC+.
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
47 + select COMMON_CLK_HIFIBERRY_DACPRO
49 + Say Y or M if you want to add support for HifiBerry DAC+ADC.
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
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
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
73 +++ b/sound/soc/bcm/hifiberry_dacplusadc.c
76 + * ASoC Driver for HiFiBerry DAC+ / DAC Pro with ADC
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>
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.
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.
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>
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>
110 +#include "../codecs/pcm512x.h"
112 +#define HIFIBERRY_DACPRO_NOCLOCK 0
113 +#define HIFIBERRY_DACPRO_CLK44EN 1
114 +#define HIFIBERRY_DACPRO_CLK48EN 2
116 +struct platform_device *dmic_codec_dev;
118 +struct pcm512x_priv {
119 + struct regmap *regmap;
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
129 +static bool snd_rpi_hifiberry_is_dacpro;
130 +static bool digital_gain_0db_limit = true;
132 +static void snd_rpi_hifiberry_dacplusadc_select_clk(struct snd_soc_component *component,
136 + case HIFIBERRY_DACPRO_NOCLOCK:
137 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x00);
139 + case HIFIBERRY_DACPRO_CLK44EN:
140 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x20);
142 + case HIFIBERRY_DACPRO_CLK48EN:
143 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x04);
148 +static void snd_rpi_hifiberry_dacplusadc_clk_gpio(struct snd_soc_component *component)
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);
155 +static bool snd_rpi_hifiberry_dacplusadc_is_sclk(struct snd_soc_component *component)
159 + snd_soc_component_read(component, PCM512x_RATE_DET_4, &sck);
160 + return (!(sck & 0x40));
163 +static bool snd_rpi_hifiberry_dacplusadc_is_sclk_sleep(
164 + struct snd_soc_component *component)
167 + return snd_rpi_hifiberry_dacplusadc_is_sclk(component);
170 +static bool snd_rpi_hifiberry_dacplusadc_is_pro_card(struct snd_soc_component *component)
172 + bool isClk44EN, isClk48En, isNoClk;
174 + snd_rpi_hifiberry_dacplusadc_clk_gpio(component);
176 + snd_rpi_hifiberry_dacplusadc_select_clk(component, HIFIBERRY_DACPRO_CLK44EN);
177 + isClk44EN = snd_rpi_hifiberry_dacplusadc_is_sclk_sleep(component);
179 + snd_rpi_hifiberry_dacplusadc_select_clk(component, HIFIBERRY_DACPRO_NOCLOCK);
180 + isNoClk = snd_rpi_hifiberry_dacplusadc_is_sclk_sleep(component);
182 + snd_rpi_hifiberry_dacplusadc_select_clk(component, HIFIBERRY_DACPRO_CLK48EN);
183 + isClk48En = snd_rpi_hifiberry_dacplusadc_is_sclk_sleep(component);
185 + return (isClk44EN && isClk48En && !isNoClk);
188 +static int snd_rpi_hifiberry_dacplusadc_clk_for_rate(int sample_rate)
192 + switch (sample_rate) {
199 + type = HIFIBERRY_DACPRO_CLK44EN;
202 + type = HIFIBERRY_DACPRO_CLK48EN;
208 +static void snd_rpi_hifiberry_dacplusadc_set_sclk(struct snd_soc_component *component,
211 + struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
213 + if (!IS_ERR(pcm512x->sclk)) {
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);
223 +static int snd_rpi_hifiberry_dacplusadc_init(struct snd_soc_pcm_runtime *rtd)
225 + struct snd_soc_component *component = rtd->codec_dai->component;
226 + struct pcm512x_priv *priv;
229 + snd_rpi_hifiberry_is_dacpro = false;
231 + snd_rpi_hifiberry_is_dacpro =
232 + snd_rpi_hifiberry_dacplusadc_is_pro_card(component);
234 + if (snd_rpi_hifiberry_is_dacpro) {
235 + struct snd_soc_dai_link *dai = rtd->dai_link;
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;
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);
246 + priv = snd_soc_component_get_drvdata(component);
247 + priv->sclk = ERR_PTR(-ENOENT);
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);
254 + if (digital_gain_0db_limit) {
256 + struct snd_soc_card *card = rtd->card;
258 + ret = snd_soc_limit_volume(card, "Digital Playback Volume", 207);
260 + dev_warn(card->dev, "Failed to set volume limit: %d\n", ret);
266 +static int snd_rpi_hifiberry_dacplusadc_update_rate_den(
267 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
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;
276 + rats_no_pll = devm_kzalloc(rtd->dev, sizeof(*rats_no_pll), GFP_KERNEL);
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;
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;
292 + devm_kfree(rtd->dev, rats_no_pll);
296 +static int snd_rpi_hifiberry_dacplusadc_hw_params(
297 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
300 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
301 + int channels = params_channels(params);
304 + if (snd_rpi_hifiberry_is_dacpro) {
305 + struct snd_soc_component *component = rtd->codec_dai->component;
307 + width = snd_pcm_format_physical_width(params_format(params));
309 + snd_rpi_hifiberry_dacplusadc_set_sclk(component,
310 + params_rate(params));
312 + ret = snd_rpi_hifiberry_dacplusadc_update_rate_den(
313 + substream, params);
316 + ret = snd_soc_dai_set_bclk_ratio(rtd->cpu_dai, channels * width);
319 + ret = snd_soc_dai_set_bclk_ratio(rtd->codec_dai, channels * width);
323 +static int hifiberry_dacplusadc_LED_cnt;
325 +static int snd_rpi_hifiberry_dacplusadc_startup(
326 + struct snd_pcm_substream *substream)
328 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
329 + struct snd_soc_component *component = rtd->codec_dai->component;
331 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1,
333 + hifiberry_dacplusadc_LED_cnt++;
337 +static void snd_rpi_hifiberry_dacplusadc_shutdown(
338 + struct snd_pcm_substream *substream)
340 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
341 + struct snd_soc_component *component = rtd->codec_dai->component;
343 + hifiberry_dacplusadc_LED_cnt--;
344 + if (!hifiberry_dacplusadc_LED_cnt)
345 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1,
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,
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")));
362 +static struct snd_soc_dai_link snd_rpi_hifiberry_dacplusadc_dai[] = {
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),
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),
384 +static int snd_rpi_hifiberry_dacplusadc_probe(struct platform_device *pdev)
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;
393 + dai = &snd_rpi_hifiberry_dacplusadc_dai[0];
394 + i2s_node = of_parse_phandle(pdev->dev.of_node,
395 + "i2s-controller", 0);
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;
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");
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);
417 +static const struct of_device_id snd_rpi_hifiberry_dacplusadc_of_match[] = {
418 + { .compatible = "hifiberry,hifiberry-dacplusadc", },
422 +MODULE_DEVICE_TABLE(of, snd_rpi_hifiberry_dacplusadc_of_match);
424 +static struct platform_driver snd_rpi_hifiberry_dacplusadc_driver = {
426 + .name = "snd-rpi-hifiberry-dacplusadc",
427 + .owner = THIS_MODULE,
428 + .of_match_table = snd_rpi_hifiberry_dacplusadc_of_match,
430 + .probe = snd_rpi_hifiberry_dacplusadc_probe,
433 +static int __init hifiberry_dacplusadc_init(void)
437 + dmic_codec_dev = platform_device_register_simple("dmic-codec", -1, NULL,
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);
444 + ret = platform_driver_register(&snd_rpi_hifiberry_dacplusadc_driver);
446 + pr_err("%s: platform driver registration failed\n", __func__);
447 + platform_device_unregister(dmic_codec_dev);
452 +module_init(hifiberry_dacplusadc_init);
454 +static void __exit hifiberry_dacplusadc_exit(void)
456 + platform_driver_unregister(&snd_rpi_hifiberry_dacplusadc_driver);
457 + platform_device_unregister(dmic_codec_dev);
459 +module_exit(hifiberry_dacplusadc_exit);
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");