1 From 1aebaa90d468875109f0bf37cd89c7cfd9ac69bf Mon Sep 17 00:00:00 2001
2 From: Joerg Schambacher <joerg@i2audio.com>
3 Date: Fri, 29 Jan 2021 16:16:39 +0100
4 Subject: [PATCH] Enhances the Hifiberry DAC+ driver for Hifiberry
7 Adds the necessary GPIO handling and ALSA mixer extensions.
8 Also fixes a problem with the PLL/CLK control when switching sample rates.
9 Thanks to Clive Messer for the support!
11 Signed-off-by: Joerg Schambacher <joerg@hifiberry.com>
13 sound/soc/bcm/hifiberry_dacplus.c | 124 ++++++++++++++++++++++++++----
14 1 file changed, 111 insertions(+), 13 deletions(-)
16 --- a/sound/soc/bcm/hifiberry_dacplus.c
17 +++ b/sound/soc/bcm/hifiberry_dacplus.c
20 - * ASoC Driver for HiFiBerry DAC+ / DAC Pro
21 + * ASoC Driver for HiFiBerry DAC+ / DAC Pro / AMP100
23 * Author: Daniel Matuschek, Stuart MacLean <stuart@hifiberry.com>
25 * based on code by Florian Meier <florian.meier@koalo.de>
26 - * Headphone added by Joerg Schambacher, joerg@i2audio.com
27 + * Headphone/AMP100 Joerg Schambacher <joerg@hifiberry.com>
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
34 #include <linux/module.h>
35 +#include <linux/gpio/consumer.h>
36 +#include <../drivers/gpio/gpiolib.h>
37 #include <linux/platform_device.h>
38 #include <linux/kernel.h>
39 #include <linux/clk.h>
40 @@ -53,6 +55,47 @@ static bool slave;
41 static bool snd_rpi_hifiberry_is_dacpro;
42 static bool digital_gain_0db_limit = true;
44 +static bool auto_mute;
45 +static int mute_ext_ctl;
47 +static struct gpio_desc *snd_mute_gpio;
48 +static struct gpio_desc *snd_reset_gpio;
49 +static struct snd_soc_card snd_rpi_hifiberry_dacplus;
51 +static int snd_rpi_hifiberry_dacplus_mute_set(int mute)
53 + gpiod_set_value_cansleep(snd_mute_gpio, mute);
57 +static int snd_rpi_hifiberry_dacplus_mute_get(struct snd_kcontrol *kcontrol,
58 + struct snd_ctl_elem_value *ucontrol)
60 + ucontrol->value.integer.value[0] = mute_ext;
65 +static int snd_rpi_hifiberry_dacplus_mute_put(struct snd_kcontrol *kcontrol,
66 + struct snd_ctl_elem_value *ucontrol)
68 + if (mute_ext == ucontrol->value.integer.value[0])
71 + mute_ext = ucontrol->value.integer.value[0];
73 + return snd_rpi_hifiberry_dacplus_mute_set(mute_ext);
76 +static const char * const mute_text[] = {"Play", "Mute"};
77 +static const struct soc_enum hb_dacplus_opt_mute_enum =
78 + SOC_ENUM_SINGLE_EXT(2, mute_text);
80 +static const struct snd_kcontrol_new hb_dacplus_opt_mute_controls[] = {
81 + SOC_ENUM_EXT("Mute(ext)", hb_dacplus_opt_mute_enum,
82 + snd_rpi_hifiberry_dacplus_mute_get,
83 + snd_rpi_hifiberry_dacplus_mute_put),
86 static void snd_rpi_hifiberry_dacplus_select_clk(struct snd_soc_component *component,
88 @@ -68,6 +111,7 @@ static void snd_rpi_hifiberry_dacplus_se
89 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x04);
92 + usleep_range(2000, 2100);
95 static void snd_rpi_hifiberry_dacplus_clk_gpio(struct snd_soc_component *component)
96 @@ -85,13 +129,6 @@ static bool snd_rpi_hifiberry_dacplus_is
97 return (!(sck & 0x40));
100 -static bool snd_rpi_hifiberry_dacplus_is_sclk_sleep(
101 - struct snd_soc_component *component)
104 - return snd_rpi_hifiberry_dacplus_is_sclk(component);
107 static bool snd_rpi_hifiberry_dacplus_is_pro_card(struct snd_soc_component *component)
109 bool isClk44EN, isClk48En, isNoClk;
110 @@ -99,13 +136,13 @@ static bool snd_rpi_hifiberry_dacplus_is
111 snd_rpi_hifiberry_dacplus_clk_gpio(component);
113 snd_rpi_hifiberry_dacplus_select_clk(component, HIFIBERRY_DACPRO_CLK44EN);
114 - isClk44EN = snd_rpi_hifiberry_dacplus_is_sclk_sleep(component);
115 + isClk44EN = snd_rpi_hifiberry_dacplus_is_sclk(component);
117 snd_rpi_hifiberry_dacplus_select_clk(component, HIFIBERRY_DACPRO_NOCLOCK);
118 - isNoClk = snd_rpi_hifiberry_dacplus_is_sclk_sleep(component);
119 + isNoClk = snd_rpi_hifiberry_dacplus_is_sclk(component);
121 snd_rpi_hifiberry_dacplus_select_clk(component, HIFIBERRY_DACPRO_CLK48EN);
122 - isClk48En = snd_rpi_hifiberry_dacplus_is_sclk_sleep(component);
123 + isClk48En = snd_rpi_hifiberry_dacplus_is_sclk(component);
125 return (isClk44EN && isClk48En && !isNoClk);
127 @@ -149,6 +186,7 @@ static int snd_rpi_hifiberry_dacplus_ini
129 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
130 struct pcm512x_priv *priv;
131 + struct snd_soc_card *card = &snd_rpi_hifiberry_dacplus;
134 snd_rpi_hifiberry_is_dacpro = false;
135 @@ -187,6 +225,20 @@ static int snd_rpi_hifiberry_dacplus_ini
137 dev_warn(card->dev, "Failed to set volume limit: %d\n", ret);
139 + if (snd_reset_gpio) {
140 + gpiod_set_value_cansleep(snd_reset_gpio, 0);
142 + gpiod_set_value_cansleep(snd_reset_gpio, 1);
144 + gpiod_set_value_cansleep(snd_reset_gpio, 0);
148 + snd_soc_add_card_controls(card, hb_dacplus_opt_mute_controls,
149 + ARRAY_SIZE(hb_dacplus_opt_mute_controls));
152 + gpiod_set_value_cansleep(snd_mute_gpio, mute_ext);
156 @@ -254,6 +306,8 @@ static int snd_rpi_hifiberry_dacplus_sta
157 struct snd_soc_pcm_runtime *rtd = substream->private_data;
158 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
161 + gpiod_set_value_cansleep(snd_mute_gpio, 0);
164 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
165 @@ -267,6 +321,8 @@ static void snd_rpi_hifiberry_dacplus_sh
166 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
168 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
170 + gpiod_set_value_cansleep(snd_mute_gpio, 1);
173 /* machine stream operations */
174 @@ -342,6 +398,8 @@ static int snd_rpi_hifiberry_dacplus_pro
175 struct device_node *tpa_node;
176 struct property *tpa_prop;
177 struct of_changeset ocs;
178 + struct property *pp;
181 /* probe for head phone amp */
182 ret = hb_hp_detect();
183 @@ -396,6 +454,39 @@ static int snd_rpi_hifiberry_dacplus_pro
184 "hifiberry-dacplus,slave");
185 leds_off = of_property_read_bool(pdev->dev.of_node,
186 "hifiberry-dacplus,leds_off");
187 + auto_mute = of_property_read_bool(pdev->dev.of_node,
188 + "hifiberry-dacplus,auto_mute");
191 + * check for HW MUTE as defined in DT-overlay
192 + * active high, therefore default to HIGH to MUTE
194 + snd_mute_gpio = devm_gpiod_get_optional(&pdev->dev,
195 + "mute", GPIOD_OUT_HIGH);
196 + if (IS_ERR(snd_mute_gpio)) {
197 + dev_err(&pdev->dev, "Can't allocate GPIO (HW-MUTE)");
198 + return PTR_ERR(snd_mute_gpio);
201 + /* add ALSA control if requested in DT-overlay (AMP100) */
202 + pp = of_find_property(pdev->dev.of_node,
203 + "hifiberry-dacplus,mute_ext_ctl", &tmp);
205 + if (!of_property_read_u32(pdev->dev.of_node,
206 + "hifiberry-dacplus,mute_ext_ctl", &mute_ext)) {
207 + /* ALSA control will be used */
212 + /* check for HW RESET (AMP100) */
213 + snd_reset_gpio = devm_gpiod_get_optional(&pdev->dev,
214 + "reset", GPIOD_OUT_HIGH);
215 + if (IS_ERR(snd_reset_gpio)) {
216 + dev_err(&pdev->dev, "Can't allocate GPIO (HW-RESET)");
217 + return PTR_ERR(snd_reset_gpio);
222 ret = devm_snd_soc_register_card(&pdev->dev,
223 @@ -403,7 +494,14 @@ static int snd_rpi_hifiberry_dacplus_pro
224 if (ret && ret != -EPROBE_DEFER)
226 "snd_soc_register_card() failed: %d\n", ret);
230 + dev_info(&pdev->dev, "GPIO%i for HW-MUTE selected",
231 + gpio_chip_hwgpio(snd_mute_gpio));
232 + if (snd_reset_gpio)
233 + dev_info(&pdev->dev, "GPIO%i for HW-RESET selected",
234 + gpio_chip_hwgpio(snd_reset_gpio));