brcm2708: rename target to bcm27xx
[openwrt/staging/wigyori.git] / target / linux / bcm27xx / patches-4.19 / 950-0066-Add-IQaudIO-Sound-Card-support-for-Raspberry-Pi.patch
1 From 76c252645ad542bd35ce52230635f36e3c0f730d 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 ASoC: iqaudio-dac: fix S24_LE format
59
60 Remove set_bclk_ratio call so 24-bit data is transmitted in
61 24 bclk cycles.
62
63 Signed-off-by: Matthias Reichl <hias@horus.com>
64 ---
65 sound/soc/bcm/iqaudio-dac.c | 221 ++++++++++++++++++++++++++++++++++++
66 1 file changed, 221 insertions(+)
67 create mode 100644 sound/soc/bcm/iqaudio-dac.c
68
69 --- /dev/null
70 +++ b/sound/soc/bcm/iqaudio-dac.c
71 @@ -0,0 +1,221 @@
72 +/*
73 + * ASoC Driver for IQaudIO DAC
74 + *
75 + * Author: Florian Meier <florian.meier@koalo.de>
76 + * Copyright 2013
77 + *
78 + * This program is free software; you can redistribute it and/or
79 + * modify it under the terms of the GNU General Public License
80 + * version 2 as published by the Free Software Foundation.
81 + *
82 + * This program is distributed in the hope that it will be useful, but
83 + * WITHOUT ANY WARRANTY; without even the implied warranty of
84 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
85 + * General Public License for more details.
86 + */
87 +
88 +#include <linux/module.h>
89 +#include <linux/gpio/consumer.h>
90 +#include <linux/platform_device.h>
91 +
92 +#include <sound/core.h>
93 +#include <sound/pcm.h>
94 +#include <sound/pcm_params.h>
95 +#include <sound/soc.h>
96 +#include <sound/jack.h>
97 +
98 +static bool digital_gain_0db_limit = true;
99 +
100 +static struct gpio_desc *mute_gpio;
101 +
102 +static int snd_rpi_iqaudio_dac_init(struct snd_soc_pcm_runtime *rtd)
103 +{
104 + if (digital_gain_0db_limit)
105 + {
106 + int ret;
107 + struct snd_soc_card *card = rtd->card;
108 +
109 + ret = snd_soc_limit_volume(card, "Digital Playback Volume", 207);
110 + if (ret < 0)
111 + dev_warn(card->dev, "Failed to set volume limit: %d\n", ret);
112 + }
113 +
114 + return 0;
115 +}
116 +
117 +static void snd_rpi_iqaudio_gpio_mute(struct snd_soc_card *card)
118 +{
119 + if (mute_gpio) {
120 + dev_info(card->dev, "%s: muting amp using GPIO22\n",
121 + __func__);
122 + gpiod_set_value_cansleep(mute_gpio, 0);
123 + }
124 +}
125 +
126 +static void snd_rpi_iqaudio_gpio_unmute(struct snd_soc_card *card)
127 +{
128 + if (mute_gpio) {
129 + dev_info(card->dev, "%s: un-muting amp using GPIO22\n",
130 + __func__);
131 + gpiod_set_value_cansleep(mute_gpio, 1);
132 + }
133 +}
134 +
135 +static int snd_rpi_iqaudio_set_bias_level(struct snd_soc_card *card,
136 + struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
137 +{
138 + struct snd_soc_pcm_runtime *rtd;
139 + struct snd_soc_dai *codec_dai;
140 +
141 + rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
142 + codec_dai = rtd->codec_dai;
143 +
144 + if (dapm->dev != codec_dai->dev)
145 + return 0;
146 +
147 + switch (level) {
148 + case SND_SOC_BIAS_PREPARE:
149 + if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
150 + break;
151 +
152 + /* UNMUTE AMP */
153 + snd_rpi_iqaudio_gpio_unmute(card);
154 +
155 + break;
156 + case SND_SOC_BIAS_STANDBY:
157 + if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
158 + break;
159 +
160 + /* MUTE AMP */
161 + snd_rpi_iqaudio_gpio_mute(card);
162 +
163 + break;
164 + default:
165 + break;
166 + }
167 +
168 + return 0;
169 +}
170 +
171 +static struct snd_soc_dai_link snd_rpi_iqaudio_dac_dai[] = {
172 +{
173 + .cpu_dai_name = "bcm2708-i2s.0",
174 + .codec_dai_name = "pcm512x-hifi",
175 + .platform_name = "bcm2708-i2s.0",
176 + .codec_name = "pcm512x.1-004c",
177 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
178 + SND_SOC_DAIFMT_CBS_CFS,
179 + .init = snd_rpi_iqaudio_dac_init,
180 +},
181 +};
182 +
183 +/* audio machine driver */
184 +static struct snd_soc_card snd_rpi_iqaudio_dac = {
185 + .owner = THIS_MODULE,
186 + .dai_link = snd_rpi_iqaudio_dac_dai,
187 + .num_links = ARRAY_SIZE(snd_rpi_iqaudio_dac_dai),
188 +};
189 +
190 +static int snd_rpi_iqaudio_dac_probe(struct platform_device *pdev)
191 +{
192 + int ret = 0;
193 + bool gpio_unmute = false;
194 +
195 + snd_rpi_iqaudio_dac.dev = &pdev->dev;
196 +
197 + if (pdev->dev.of_node) {
198 + struct device_node *i2s_node;
199 + struct snd_soc_card *card = &snd_rpi_iqaudio_dac;
200 + struct snd_soc_dai_link *dai = &snd_rpi_iqaudio_dac_dai[0];
201 + bool auto_gpio_mute = false;
202 +
203 + i2s_node = of_parse_phandle(pdev->dev.of_node,
204 + "i2s-controller", 0);
205 + if (i2s_node) {
206 + dai->cpu_dai_name = NULL;
207 + dai->cpu_of_node = i2s_node;
208 + dai->platform_name = NULL;
209 + dai->platform_of_node = i2s_node;
210 + }
211 +
212 + digital_gain_0db_limit = !of_property_read_bool(
213 + pdev->dev.of_node, "iqaudio,24db_digital_gain");
214 +
215 + if (of_property_read_string(pdev->dev.of_node, "card_name",
216 + &card->name))
217 + card->name = "IQaudIODAC";
218 +
219 + if (of_property_read_string(pdev->dev.of_node, "dai_name",
220 + &dai->name))
221 + dai->name = "IQaudIO DAC";
222 +
223 + if (of_property_read_string(pdev->dev.of_node,
224 + "dai_stream_name", &dai->stream_name))
225 + dai->stream_name = "IQaudIO DAC HiFi";
226 +
227 + /* gpio_unmute - one time unmute amp using GPIO */
228 + gpio_unmute = of_property_read_bool(pdev->dev.of_node,
229 + "iqaudio-dac,unmute-amp");
230 +
231 + /* auto_gpio_mute - mute/unmute amp using GPIO */
232 + auto_gpio_mute = of_property_read_bool(pdev->dev.of_node,
233 + "iqaudio-dac,auto-mute-amp");
234 +
235 + if (auto_gpio_mute || gpio_unmute) {
236 + mute_gpio = devm_gpiod_get_optional(&pdev->dev, "mute",
237 + GPIOD_OUT_LOW);
238 + if (IS_ERR(mute_gpio)) {
239 + ret = PTR_ERR(mute_gpio);
240 + dev_err(&pdev->dev,
241 + "Failed to get mute gpio: %d\n", ret);
242 + return ret;
243 + }
244 +
245 + if (auto_gpio_mute && mute_gpio)
246 + snd_rpi_iqaudio_dac.set_bias_level =
247 + snd_rpi_iqaudio_set_bias_level;
248 + }
249 + }
250 +
251 + ret = snd_soc_register_card(&snd_rpi_iqaudio_dac);
252 + if (ret) {
253 + if (ret != -EPROBE_DEFER)
254 + dev_err(&pdev->dev,
255 + "snd_soc_register_card() failed: %d\n", ret);
256 + return ret;
257 + }
258 +
259 + if (gpio_unmute && mute_gpio)
260 + snd_rpi_iqaudio_gpio_unmute(&snd_rpi_iqaudio_dac);
261 +
262 + return 0;
263 +}
264 +
265 +static int snd_rpi_iqaudio_dac_remove(struct platform_device *pdev)
266 +{
267 + snd_rpi_iqaudio_gpio_mute(&snd_rpi_iqaudio_dac);
268 +
269 + return snd_soc_unregister_card(&snd_rpi_iqaudio_dac);
270 +}
271 +
272 +static const struct of_device_id iqaudio_of_match[] = {
273 + { .compatible = "iqaudio,iqaudio-dac", },
274 + {},
275 +};
276 +MODULE_DEVICE_TABLE(of, iqaudio_of_match);
277 +
278 +static struct platform_driver snd_rpi_iqaudio_dac_driver = {
279 + .driver = {
280 + .name = "snd-rpi-iqaudio-dac",
281 + .owner = THIS_MODULE,
282 + .of_match_table = iqaudio_of_match,
283 + },
284 + .probe = snd_rpi_iqaudio_dac_probe,
285 + .remove = snd_rpi_iqaudio_dac_remove,
286 +};
287 +
288 +module_platform_driver(snd_rpi_iqaudio_dac_driver);
289 +
290 +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
291 +MODULE_DESCRIPTION("ASoC Driver for IQAudio DAC");
292 +MODULE_LICENSE("GPL v2");