layerscape: add u-boot environment support for OpenWrt boot
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.9 / 950-0070-Add-IQaudIO-Sound-Card-support-for-Raspberry-Pi.patch
1 From fcfe9087b79176b5d8afd0ecc4c66013866ef239 Mon Sep 17 00:00:00 2001
2 From: Gordon Garrity <gordon@iqaudio.com>
3 Date: Sat, 8 Mar 2014 16:56:57 +0000
4 Subject: [PATCH] Add IQaudIO Sound Card support for Raspberry Pi
5
6 Set a limit of 0dB on Digital Volume Control
7
8 The main volume control in the PCM512x DAC has a range up to
9 +24dB. This is dangerously loud and can potentially cause massive
10 clipping in the output stages. Therefore this sets a sensible
11 limit of 0dB for this control.
12
13 Allow up to 24dB digital gain to be applied when using IQAudIO DAC+
14
15 24db_digital_gain DT param can be used to specify that PCM512x
16 codec "Digital" volume control should not be limited to 0dB gain,
17 and if specified will allow the full 24dB gain.
18
19 Modify IQAudIO DAC+ ASoC driver to set card/dai config from dt
20
21 Add the ability to set the card name, dai name and dai stream name, from
22 dt config.
23
24 Signed-off-by: DigitalDreamtime <clive.messer@digitaldreamtime.co.uk>
25
26 IQaudIO: auto-mute for AMP+ and DigiAMP+
27
28 IQAudIO amplifier mute via GPIO22. Add dt params for "one-shot" unmute
29 and auto mute.
30
31 Revision 2, auto mute implementing HiassofT suggestion to mute/unmute
32 using set_bias_level, rather than startup/shutdown....
33 "By default DAPM waits 5 seconds (pmdown_time) before shutting down
34 playback streams so a close/stop immediately followed by open/start
35 doesn't trigger an amp mute+unmute."
36
37 Tested on both AMP+ (via DAC+) and DigiAMP+, with both options...
38
39 dtoverlay=iqaudio-dacplus,unmute_amp
40 "one-shot" unmute when kernel module loads.
41
42 dtoverlay=iqaudio-dacplus,auto_mute_amp
43 Unmute amp when ALSA device opened by a client. Mute, with 5 second delay
44 when ALSA device closed. (Re-opening the device within the 5 second close
45 window, will cancel mute.)
46
47 Revision 4, using gpiod.
48
49 Revision 5, clean-up formatting before adding mute code.
50 - Convert tab plus 4 space formatting to 2x tab
51 - Remove '// NOT USED' commented code
52
53 Revision 6, don't attempt to "one-shot" unmute amp, unless card is
54 successfully registered.
55
56 Signed-off-by: DigitalDreamtime <clive.messer@digitaldreamtime.co.uk>
57 ---
58 sound/soc/bcm/Kconfig | 7 ++
59 sound/soc/bcm/Makefile | 2 +
60 sound/soc/bcm/iqaudio-dac.c | 234 ++++++++++++++++++++++++++++++++++++++++++++
61 3 files changed, 243 insertions(+)
62 create mode 100644 sound/soc/bcm/iqaudio-dac.c
63
64 --- a/sound/soc/bcm/Kconfig
65 +++ b/sound/soc/bcm/Kconfig
66 @@ -37,3 +37,10 @@ config SND_BCM2708_SOC_RPI_DAC
67 select SND_SOC_PCM1794A
68 help
69 Say Y or M if you want to add support for RPi-DAC.
70 +
71 +config SND_BCM2708_SOC_IQAUDIO_DAC
72 + tristate "Support for IQaudIO-DAC"
73 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
74 + select SND_SOC_PCM512x_I2C
75 + help
76 + Say Y or M if you want to add support for IQaudIO-DAC.
77 --- a/sound/soc/bcm/Makefile
78 +++ b/sound/soc/bcm/Makefile
79 @@ -12,7 +12,9 @@ obj-$(CONFIG_SND_SOC_CYGNUS) += snd-soc-
80 snd-soc-hifiberry-dac-objs := hifiberry_dac.o
81 snd-soc-hifiberry-digi-objs := hifiberry_digi.o
82 snd-soc-rpi-dac-objs := rpi-dac.o
83 +snd-soc-iqaudio-dac-objs := iqaudio-dac.o
84
85 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
86 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
87 obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
88 +obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
89 --- /dev/null
90 +++ b/sound/soc/bcm/iqaudio-dac.c
91 @@ -0,0 +1,234 @@
92 +/*
93 + * ASoC Driver for IQaudIO DAC
94 + *
95 + * Author: Florian Meier <florian.meier@koalo.de>
96 + * Copyright 2013
97 + *
98 + * This program is free software; you can redistribute it and/or
99 + * modify it under the terms of the GNU General Public License
100 + * version 2 as published by the Free Software Foundation.
101 + *
102 + * This program is distributed in the hope that it will be useful, but
103 + * WITHOUT ANY WARRANTY; without even the implied warranty of
104 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
105 + * General Public License for more details.
106 + */
107 +
108 +#include <linux/module.h>
109 +#include <linux/gpio/consumer.h>
110 +#include <linux/platform_device.h>
111 +
112 +#include <sound/core.h>
113 +#include <sound/pcm.h>
114 +#include <sound/pcm_params.h>
115 +#include <sound/soc.h>
116 +#include <sound/jack.h>
117 +
118 +static bool digital_gain_0db_limit = true;
119 +
120 +static struct gpio_desc *mute_gpio;
121 +
122 +static int snd_rpi_iqaudio_dac_init(struct snd_soc_pcm_runtime *rtd)
123 +{
124 + if (digital_gain_0db_limit)
125 + {
126 + int ret;
127 + struct snd_soc_card *card = rtd->card;
128 +
129 + ret = snd_soc_limit_volume(card, "Digital Playback Volume", 207);
130 + if (ret < 0)
131 + dev_warn(card->dev, "Failed to set volume limit: %d\n", ret);
132 + }
133 +
134 + return 0;
135 +}
136 +
137 +static int snd_rpi_iqaudio_dac_hw_params(struct snd_pcm_substream *substream,
138 + struct snd_pcm_hw_params *params)
139 +{
140 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
141 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
142 +
143 + unsigned int sample_bits =
144 + snd_pcm_format_physical_width(params_format(params));
145 +
146 + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
147 +}
148 +
149 +static void snd_rpi_iqaudio_gpio_mute(struct snd_soc_card *card)
150 +{
151 + if (mute_gpio) {
152 + dev_info(card->dev, "%s: muting amp using GPIO22\n",
153 + __func__);
154 + gpiod_set_value_cansleep(mute_gpio, 0);
155 + }
156 +}
157 +
158 +static void snd_rpi_iqaudio_gpio_unmute(struct snd_soc_card *card)
159 +{
160 + if (mute_gpio) {
161 + dev_info(card->dev, "%s: un-muting amp using GPIO22\n",
162 + __func__);
163 + gpiod_set_value_cansleep(mute_gpio, 1);
164 + }
165 +}
166 +
167 +static int snd_rpi_iqaudio_set_bias_level(struct snd_soc_card *card,
168 + struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
169 +{
170 + struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
171 +
172 + if (dapm->dev != codec_dai->dev)
173 + return 0;
174 +
175 + switch (level) {
176 + case SND_SOC_BIAS_PREPARE:
177 + if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
178 + break;
179 +
180 + /* UNMUTE AMP */
181 + snd_rpi_iqaudio_gpio_unmute(card);
182 +
183 + break;
184 + case SND_SOC_BIAS_STANDBY:
185 + if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
186 + break;
187 +
188 + /* MUTE AMP */
189 + snd_rpi_iqaudio_gpio_mute(card);
190 +
191 + break;
192 + default:
193 + break;
194 + }
195 +
196 + return 0;
197 +}
198 +
199 +/* machine stream operations */
200 +static struct snd_soc_ops snd_rpi_iqaudio_dac_ops = {
201 + .hw_params = snd_rpi_iqaudio_dac_hw_params,
202 +};
203 +
204 +static struct snd_soc_dai_link snd_rpi_iqaudio_dac_dai[] = {
205 +{
206 + .cpu_dai_name = "bcm2708-i2s.0",
207 + .codec_dai_name = "pcm512x-hifi",
208 + .platform_name = "bcm2708-i2s.0",
209 + .codec_name = "pcm512x.1-004c",
210 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
211 + SND_SOC_DAIFMT_CBS_CFS,
212 + .ops = &snd_rpi_iqaudio_dac_ops,
213 + .init = snd_rpi_iqaudio_dac_init,
214 +},
215 +};
216 +
217 +/* audio machine driver */
218 +static struct snd_soc_card snd_rpi_iqaudio_dac = {
219 + .owner = THIS_MODULE,
220 + .dai_link = snd_rpi_iqaudio_dac_dai,
221 + .num_links = ARRAY_SIZE(snd_rpi_iqaudio_dac_dai),
222 +};
223 +
224 +static int snd_rpi_iqaudio_dac_probe(struct platform_device *pdev)
225 +{
226 + int ret = 0;
227 + bool gpio_unmute = false;
228 +
229 + snd_rpi_iqaudio_dac.dev = &pdev->dev;
230 +
231 + if (pdev->dev.of_node) {
232 + struct device_node *i2s_node;
233 + struct snd_soc_card *card = &snd_rpi_iqaudio_dac;
234 + struct snd_soc_dai_link *dai = &snd_rpi_iqaudio_dac_dai[0];
235 + bool auto_gpio_mute = false;
236 +
237 + i2s_node = of_parse_phandle(pdev->dev.of_node,
238 + "i2s-controller", 0);
239 + if (i2s_node) {
240 + dai->cpu_dai_name = NULL;
241 + dai->cpu_of_node = i2s_node;
242 + dai->platform_name = NULL;
243 + dai->platform_of_node = i2s_node;
244 + }
245 +
246 + digital_gain_0db_limit = !of_property_read_bool(
247 + pdev->dev.of_node, "iqaudio,24db_digital_gain");
248 +
249 + if (of_property_read_string(pdev->dev.of_node, "card_name",
250 + &card->name))
251 + card->name = "IQaudIODAC";
252 +
253 + if (of_property_read_string(pdev->dev.of_node, "dai_name",
254 + &dai->name))
255 + dai->name = "IQaudIO DAC";
256 +
257 + if (of_property_read_string(pdev->dev.of_node,
258 + "dai_stream_name", &dai->stream_name))
259 + dai->stream_name = "IQaudIO DAC HiFi";
260 +
261 + /* gpio_unmute - one time unmute amp using GPIO */
262 + gpio_unmute = of_property_read_bool(pdev->dev.of_node,
263 + "iqaudio-dac,unmute-amp");
264 +
265 + /* auto_gpio_mute - mute/unmute amp using GPIO */
266 + auto_gpio_mute = of_property_read_bool(pdev->dev.of_node,
267 + "iqaudio-dac,auto-mute-amp");
268 +
269 + if (auto_gpio_mute || gpio_unmute) {
270 + mute_gpio = devm_gpiod_get_optional(&pdev->dev, "mute",
271 + GPIOD_OUT_LOW);
272 + if (IS_ERR(mute_gpio)) {
273 + ret = PTR_ERR(mute_gpio);
274 + dev_err(&pdev->dev,
275 + "Failed to get mute gpio: %d\n", ret);
276 + return ret;
277 + }
278 +
279 + if (auto_gpio_mute && mute_gpio)
280 + snd_rpi_iqaudio_dac.set_bias_level =
281 + snd_rpi_iqaudio_set_bias_level;
282 + }
283 + }
284 +
285 + ret = snd_soc_register_card(&snd_rpi_iqaudio_dac);
286 + if (ret) {
287 + dev_err(&pdev->dev,
288 + "snd_soc_register_card() failed: %d\n", ret);
289 + return ret;
290 + }
291 +
292 + if (gpio_unmute && mute_gpio)
293 + snd_rpi_iqaudio_gpio_unmute(&snd_rpi_iqaudio_dac);
294 +
295 + return 0;
296 +}
297 +
298 +static int snd_rpi_iqaudio_dac_remove(struct platform_device *pdev)
299 +{
300 + snd_rpi_iqaudio_gpio_mute(&snd_rpi_iqaudio_dac);
301 +
302 + return snd_soc_unregister_card(&snd_rpi_iqaudio_dac);
303 +}
304 +
305 +static const struct of_device_id iqaudio_of_match[] = {
306 + { .compatible = "iqaudio,iqaudio-dac", },
307 + {},
308 +};
309 +MODULE_DEVICE_TABLE(of, iqaudio_of_match);
310 +
311 +static struct platform_driver snd_rpi_iqaudio_dac_driver = {
312 + .driver = {
313 + .name = "snd-rpi-iqaudio-dac",
314 + .owner = THIS_MODULE,
315 + .of_match_table = iqaudio_of_match,
316 + },
317 + .probe = snd_rpi_iqaudio_dac_probe,
318 + .remove = snd_rpi_iqaudio_dac_remove,
319 +};
320 +
321 +module_platform_driver(snd_rpi_iqaudio_dac_driver);
322 +
323 +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
324 +MODULE_DESCRIPTION("ASoC Driver for IQAudio DAC");
325 +MODULE_LICENSE("GPL v2");