4e15285220472fe1cb653558cf8a6e5fb92749b4
[openwrt/staging/blocktrron.git] /
1 From 0b584f3c2808dba8dc9e74a628b65008302804a5 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
5 AMP100 support
6
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!
10
11 Signed-off-by: Joerg Schambacher <joerg@hifiberry.com>
12 ---
13 sound/soc/bcm/hifiberry_dacplus.c | 124 ++++++++++++++++++++++++++----
14 1 file changed, 111 insertions(+), 13 deletions(-)
15
16 --- a/sound/soc/bcm/hifiberry_dacplus.c
17 +++ b/sound/soc/bcm/hifiberry_dacplus.c
18 @@ -1,10 +1,10 @@
19 /*
20 - * ASoC Driver for HiFiBerry DAC+ / DAC Pro
21 + * ASoC Driver for HiFiBerry DAC+ / DAC Pro / AMP100
22 *
23 * Author: Daniel Matuschek, Stuart MacLean <stuart@hifiberry.com>
24 * Copyright 2014-2015
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>
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 @@ -17,6 +17,8 @@
32 */
33
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;
43 static bool leds_off;
44 +static bool auto_mute;
45 +static int mute_ext_ctl;
46 +static int mute_ext;
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;
50 +
51 +static int snd_rpi_hifiberry_dacplus_mute_set(int mute)
52 +{
53 + gpiod_set_value_cansleep(snd_mute_gpio, mute);
54 + return 1;
55 +}
56 +
57 +static int snd_rpi_hifiberry_dacplus_mute_get(struct snd_kcontrol *kcontrol,
58 + struct snd_ctl_elem_value *ucontrol)
59 +{
60 + ucontrol->value.integer.value[0] = mute_ext;
61 +
62 + return 0;
63 +}
64 +
65 +static int snd_rpi_hifiberry_dacplus_mute_put(struct snd_kcontrol *kcontrol,
66 + struct snd_ctl_elem_value *ucontrol)
67 +{
68 + if (mute_ext == ucontrol->value.integer.value[0])
69 + return 0;
70 +
71 + mute_ext = ucontrol->value.integer.value[0];
72 +
73 + return snd_rpi_hifiberry_dacplus_mute_set(mute_ext);
74 +}
75 +
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);
79 +
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),
84 +};
85
86 static void snd_rpi_hifiberry_dacplus_select_clk(struct snd_soc_component *component,
87 int clk_id)
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);
90 break;
91 }
92 + usleep_range(2000, 2100);
93 }
94
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));
98 }
99
100 -static bool snd_rpi_hifiberry_dacplus_is_sclk_sleep(
101 - struct snd_soc_component *component)
102 -{
103 - msleep(2);
104 - return snd_rpi_hifiberry_dacplus_is_sclk(component);
105 -}
106 -
107 static bool snd_rpi_hifiberry_dacplus_is_pro_card(struct snd_soc_component *component)
108 {
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);
112
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);
116
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);
120
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);
124
125 return (isClk44EN && isClk48En && !isNoClk);
126 }
127 @@ -149,6 +186,7 @@ static int snd_rpi_hifiberry_dacplus_ini
128 {
129 struct snd_soc_component *component = rtd->codec_dai->component;
130 struct pcm512x_priv *priv;
131 + struct snd_soc_card *card = &snd_rpi_hifiberry_dacplus;
132
133 if (slave)
134 snd_rpi_hifiberry_is_dacpro = false;
135 @@ -187,6 +225,20 @@ static int snd_rpi_hifiberry_dacplus_ini
136 if (ret < 0)
137 dev_warn(card->dev, "Failed to set volume limit: %d\n", ret);
138 }
139 + if (snd_reset_gpio) {
140 + gpiod_set_value_cansleep(snd_reset_gpio, 0);
141 + msleep(1);
142 + gpiod_set_value_cansleep(snd_reset_gpio, 1);
143 + msleep(1);
144 + gpiod_set_value_cansleep(snd_reset_gpio, 0);
145 + }
146 +
147 + if (mute_ext_ctl)
148 + snd_soc_add_card_controls(card, hb_dacplus_opt_mute_controls,
149 + ARRAY_SIZE(hb_dacplus_opt_mute_controls));
150 +
151 + if (snd_mute_gpio)
152 + gpiod_set_value_cansleep(snd_mute_gpio, mute_ext);
153
154 return 0;
155 }
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 = rtd->codec_dai->component;
159
160 + if (auto_mute)
161 + gpiod_set_value_cansleep(snd_mute_gpio, 0);
162 if (leds_off)
163 return 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 = rtd->codec_dai->component;
167
168 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
169 + if (auto_mute)
170 + gpiod_set_value_cansleep(snd_mute_gpio, 1);
171 }
172
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;
179 + int tmp;
180
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");
189 +
190 + /*
191 + * check for HW MUTE as defined in DT-overlay
192 + * active high, therefore default to HIGH to MUTE
193 + */
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);
199 + }
200 +
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);
204 + if (pp) {
205 + if (!of_property_read_u32(pdev->dev.of_node,
206 + "hifiberry-dacplus,mute_ext_ctl", &mute_ext)) {
207 + /* ALSA control will be used */
208 + mute_ext_ctl = 1;
209 + }
210 + }
211 +
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);
218 + }
219 +
220 }
221
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)
225 dev_err(&pdev->dev,
226 "snd_soc_register_card() failed: %d\n", ret);
227 -
228 + if (!ret) {
229 + if (snd_mute_gpio)
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));
235 + }
236 return ret;
237 }
238