ac02aca893705138cfa9d0ac64fab296094667c6
[openwrt/staging/lynxis.git] / target / linux / brcm2708 / patches-4.14 / 950-0087-Driver-support-for-Google-voiceHAT-soundcard.patch
1 From 5f1aa4352ca253ca76ee49c51292994d517d7e4e Mon Sep 17 00:00:00 2001
2 From: Peter Malkin <petermalkin@google.com>
3 Date: Mon, 27 Mar 2017 16:38:21 -0700
4 Subject: [PATCH 087/454] Driver support for Google voiceHAT soundcard.
5
6 ---
7 sound/soc/bcm/Kconfig | 7 +
8 sound/soc/bcm/Makefile | 6 +
9 sound/soc/bcm/googlevoicehat-codec.c | 199 +++++++++++++++++++++++
10 sound/soc/bcm/googlevoicehat-soundcard.c | 124 ++++++++++++++
11 4 files changed, 336 insertions(+)
12 create mode 100644 sound/soc/bcm/googlevoicehat-codec.c
13 create mode 100644 sound/soc/bcm/googlevoicehat-soundcard.c
14
15 --- a/sound/soc/bcm/Kconfig
16 +++ b/sound/soc/bcm/Kconfig
17 @@ -18,6 +18,13 @@ config SND_SOC_CYGNUS
18
19 If you don't know what to do here, say N.
20
21 +config SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD
22 + tristate "Support for Google voiceHAT soundcard"
23 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
24 + select SND_SOC_VOICEHAT
25 + help
26 + Say Y or M if you want to add support for voiceHAT soundcard.
27 +
28 config SND_BCM2708_SOC_HIFIBERRY_DAC
29 tristate "Support for HifiBerry DAC"
30 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
31 --- a/sound/soc/bcm/Makefile
32 +++ b/sound/soc/bcm/Makefile
33 @@ -8,8 +8,12 @@ snd-soc-cygnus-objs := cygnus-pcm.o cygn
34
35 obj-$(CONFIG_SND_SOC_CYGNUS) += snd-soc-cygnus.o
36
37 +# Google voiceHAT custom codec support
38 +snd-soc-googlevoicehat-codec-objs := googlevoicehat-codec.o
39 +
40 # BCM2708 Machine Support
41 snd-soc-adau1977-adc-objs := adau1977-adc.o
42 +snd-soc-googlevoicehat-soundcard-objs := googlevoicehat-soundcard.o
43 snd-soc-hifiberry-amp-objs := hifiberry_amp.o
44 snd-soc-hifiberry-dac-objs := hifiberry_dac.o
45 snd-soc-hifiberry-dacplus-objs := hifiberry_dacplus.o
46 @@ -34,6 +38,8 @@ snd-soc-pisound-objs := pisound.o
47 snd-soc-fe-pi-audio-objs := fe-pi-audio.o
48
49 obj-$(CONFIG_SND_BCM2708_SOC_ADAU1977_ADC) += snd-soc-adau1977-adc.o
50 +obj-$(CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD) += snd-soc-googlevoicehat-soundcard.o
51 +obj-$(CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD) += snd-soc-googlevoicehat-codec.o
52 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP) += snd-soc-hifiberry-amp.o
53 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
54 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) += snd-soc-hifiberry-dacplus.o
55 --- /dev/null
56 +++ b/sound/soc/bcm/googlevoicehat-codec.c
57 @@ -0,0 +1,199 @@
58 +/*
59 + * Driver for the Google voiceHAT audio codec for Raspberry Pi.
60 + *
61 + * Author: Peter Malkin <petermalkin@google.com>
62 + * Copyright 2016
63 + *
64 + * This program is free software; you can redistribute it and/or
65 + * modify it under the terms of the GNU General Public License
66 + * version 2 as published by the Free Software Foundation.
67 + *
68 + * This program is distributed in the hope that it will be useful, but
69 + * WITHOUT ANY WARRANTY; without even the implied warranty of
70 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
71 + * General Public License for more details.
72 + */
73 +
74 +#include <linux/device.h>
75 +#include <linux/err.h>
76 +#include <linux/gpio.h>
77 +#include <linux/gpio/consumer.h>
78 +#include <linux/init.h>
79 +#include <linux/kernel.h>
80 +#include <linux/mod_devicetable.h>
81 +#include <linux/module.h>
82 +#include <linux/of.h>
83 +#include <linux/platform_device.h>
84 +#include <linux/version.h>
85 +#include <sound/pcm.h>
86 +#include <sound/soc.h>
87 +#include <sound/soc-dai.h>
88 +#include <sound/soc-dapm.h>
89 +
90 +#define ICS43432_RATE_MIN_HZ 7190 /* from data sheet */
91 +#define ICS43432_RATE_MAX_HZ 52800 /* from data sheet */
92 +#define SDMODE_DELAY_MS \
93 + 5 /* Delay in enabling SDMODE after clock settles to remove pop */
94 +
95 +struct voicehat_priv {
96 + struct delayed_work enable_sdmode_work;
97 + struct gpio_desc *sdmode_gpio;
98 + unsigned int sdmode_delay;
99 +};
100 +
101 +static void voicehat_enable_sdmode_work(struct work_struct *work) {
102 + struct voicehat_priv *voicehat =
103 + container_of(work, struct voicehat_priv, enable_sdmode_work.work);
104 + gpiod_set_value(voicehat->sdmode_gpio, 1);
105 +}
106 +
107 +static int voicehat_codec_probe(struct snd_soc_codec *codec) {
108 + struct voicehat_priv *voicehat = snd_soc_codec_get_drvdata(codec);
109 +
110 + voicehat->sdmode_gpio = devm_gpiod_get(codec->dev, "sdmode", GPIOD_OUT_LOW);
111 + if (IS_ERR(voicehat->sdmode_gpio)) {
112 + dev_err(codec->dev, "Unable to allocate GPIO pin\n");
113 + return PTR_ERR(voicehat->sdmode_gpio);
114 + }
115 +
116 + INIT_DELAYED_WORK(&voicehat->enable_sdmode_work, voicehat_enable_sdmode_work);
117 + return 0;
118 +}
119 +
120 +static int voicehat_codec_remove(struct snd_soc_codec *codec) {
121 + struct voicehat_priv *voicehat = snd_soc_codec_get_drvdata(codec);
122 +
123 + cancel_delayed_work_sync(&voicehat->enable_sdmode_work);
124 +
125 + return 0;
126 +}
127 +
128 +static const struct snd_soc_dapm_widget voicehat_dapm_widgets[] = {
129 + SND_SOC_DAPM_OUTPUT("Speaker"),
130 +};
131 +
132 +static const struct snd_soc_dapm_route voicehat_dapm_routes[] = {
133 + {"Speaker", NULL, "HiFi Playback"},
134 +};
135 +
136 +static struct snd_soc_codec_driver voicehat_codec_driver = {
137 + .probe = voicehat_codec_probe,
138 + .remove = voicehat_codec_remove,
139 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
140 + .component_driver = {
141 +#endif
142 + .dapm_widgets = voicehat_dapm_widgets,
143 + .num_dapm_widgets = ARRAY_SIZE(voicehat_dapm_widgets),
144 + .dapm_routes = voicehat_dapm_routes,
145 + .num_dapm_routes = ARRAY_SIZE(voicehat_dapm_routes),
146 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
147 + },
148 +#endif
149 +};
150 +
151 +static int voicehat_daiops_trigger(struct snd_pcm_substream *substream, int cmd,
152 + struct snd_soc_dai *dai) {
153 + struct snd_soc_codec *codec = dai->codec;
154 + struct voicehat_priv *voicehat = snd_soc_codec_get_drvdata(codec);
155 +
156 + if (voicehat->sdmode_delay == 0) return 0;
157 +
158 + dev_dbg(dai->dev, "CMD %d", cmd);
159 + dev_dbg(dai->dev, "Playback Active %d", dai->playback_active);
160 + dev_dbg(dai->dev, "Capture Active %d", dai->capture_active);
161 +
162 + switch (cmd) {
163 + case SNDRV_PCM_TRIGGER_START:
164 + case SNDRV_PCM_TRIGGER_RESUME:
165 + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
166 + if (dai->playback_active) {
167 + dev_info(dai->dev, "Enabling audio amp...\n");
168 + queue_delayed_work(system_power_efficient_wq,
169 + &voicehat->enable_sdmode_work,
170 + msecs_to_jiffies(voicehat->sdmode_delay));
171 + }
172 + break;
173 + case SNDRV_PCM_TRIGGER_STOP:
174 + case SNDRV_PCM_TRIGGER_SUSPEND:
175 + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
176 + if (dai->playback_active) {
177 + cancel_delayed_work(&voicehat->enable_sdmode_work);
178 + dev_info(dai->dev, "Disabling audio amp...\n");
179 + gpiod_set_value(voicehat->sdmode_gpio, 0);
180 + }
181 + break;
182 + }
183 + return 0;
184 +}
185 +
186 +static const struct snd_soc_dai_ops voicehat_dai_ops = {
187 + .trigger = voicehat_daiops_trigger,
188 +};
189 +
190 +static struct snd_soc_dai_driver voicehat_dai = {
191 + .name = "voicehat-hifi",
192 + .capture = {.stream_name = "HiFi Capture",
193 + .channels_min = 2,
194 + .channels_max = 2,
195 + .rates = SNDRV_PCM_RATE_48000,
196 + .formats = SNDRV_PCM_FMTBIT_S32_LE},
197 + .playback = {.stream_name = "HiFi Playback",
198 + .channels_min = 2,
199 + .channels_max = 2,
200 + .rates = SNDRV_PCM_RATE_48000,
201 + .formats = SNDRV_PCM_FMTBIT_S32_LE},
202 + .ops = &voicehat_dai_ops,
203 + .symmetric_rates = 1};
204 +
205 +#ifdef CONFIG_OF
206 +static const struct of_device_id voicehat_ids[] = {
207 + {
208 + .compatible = "google,voicehat",
209 + },
210 + {}};
211 +MODULE_DEVICE_TABLE(of, voicehat_ids);
212 +#endif
213 +
214 +static int voicehat_platform_probe(struct platform_device *pdev) {
215 + struct voicehat_priv *voicehat;
216 + int ret;
217 +
218 + voicehat = devm_kzalloc(&pdev->dev, sizeof(*voicehat), GFP_KERNEL);
219 + if (!voicehat) return -ENOMEM;
220 +
221 + ret = device_property_read_u32(&pdev->dev, "voicehat_sdmode_delay",
222 + &voicehat->sdmode_delay);
223 +
224 + if (ret) {
225 + voicehat->sdmode_delay = SDMODE_DELAY_MS;
226 + dev_info(&pdev->dev,
227 + "property 'voicehat_sdmode_delay' not found default 5 mS");
228 + } else {
229 + dev_info(&pdev->dev, "property 'voicehat_sdmode_delay' found delay= %d mS",
230 + voicehat->sdmode_delay);
231 + }
232 +
233 + dev_set_drvdata(&pdev->dev, voicehat);
234 +
235 + return snd_soc_register_codec(&pdev->dev, &voicehat_codec_driver, &voicehat_dai, 1);
236 +}
237 +
238 +static int voicehat_platform_remove(struct platform_device *pdev) {
239 + snd_soc_unregister_codec(&pdev->dev);
240 + return 0;
241 +}
242 +
243 +static struct platform_driver voicehat_driver = {
244 + .driver =
245 + {
246 + .name = "voicehat-codec", .of_match_table = of_match_ptr(voicehat_ids),
247 + },
248 + .probe = voicehat_platform_probe,
249 + .remove = voicehat_platform_remove,
250 +};
251 +
252 +module_platform_driver(voicehat_driver);
253 +
254 +MODULE_DESCRIPTION("Google voiceHAT Codec driver");
255 +MODULE_AUTHOR("Peter Malkin <petermalkin@google.com>");
256 +MODULE_LICENSE("GPL v2");
257 --- /dev/null
258 +++ b/sound/soc/bcm/googlevoicehat-soundcard.c
259 @@ -0,0 +1,124 @@
260 +/*
261 + * ASoC Driver for Google voiceHAT SoundCard
262 + *
263 + * Author: Peter Malkin <petermalkin@google.com>
264 + * Copyright 2016
265 + *
266 + * This program is free software; you can redistribute it and/or
267 + * modify it under the terms of the GNU General Public License
268 + * version 2 as published by the Free Software Foundation.
269 + *
270 + * This program is distributed in the hope that it will be useful, but
271 + * WITHOUT ANY WARRANTY; without even the implied warranty of
272 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
273 + * General Public License for more details.
274 + */
275 +
276 +#include <linux/module.h>
277 +#include <linux/platform_device.h>
278 +
279 +#include <sound/core.h>
280 +#include <sound/pcm.h>
281 +#include <sound/pcm_params.h>
282 +#include <sound/soc.h>
283 +#include <sound/jack.h>
284 +
285 +static int snd_rpi_googlevoicehat_soundcard_init(struct snd_soc_pcm_runtime *rtd)
286 +{
287 + return 0;
288 +}
289 +
290 +static int snd_rpi_googlevoicehat_soundcard_hw_params(
291 + struct snd_pcm_substream *substream,
292 + struct snd_pcm_hw_params *params)
293 +{
294 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
295 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
296 +
297 + unsigned int sample_bits =
298 + snd_pcm_format_physical_width(params_format(params));
299 +
300 + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
301 +}
302 +
303 +/* machine stream operations */
304 +static struct snd_soc_ops snd_rpi_googlevoicehat_soundcard_ops = {
305 + .hw_params = snd_rpi_googlevoicehat_soundcard_hw_params,
306 +};
307 +
308 +static struct snd_soc_dai_link snd_rpi_googlevoicehat_soundcard_dai[] = {
309 +{
310 + .name = "Google voiceHAT SoundCard",
311 + .stream_name = "Google voiceHAT SoundCard HiFi",
312 + .cpu_dai_name = "bcm2708-i2s.0",
313 + .codec_dai_name = "voicehat-hifi",
314 + .platform_name = "bcm2708-i2s.0",
315 + .codec_name = "voicehat-codec",
316 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
317 + SND_SOC_DAIFMT_CBS_CFS,
318 + .ops = &snd_rpi_googlevoicehat_soundcard_ops,
319 + .init = snd_rpi_googlevoicehat_soundcard_init,
320 +},
321 +};
322 +
323 +/* audio machine driver */
324 +static struct snd_soc_card snd_rpi_googlevoicehat_soundcard = {
325 + .name = "snd_rpi_googlevoicehat_soundcard",
326 + .owner = THIS_MODULE,
327 + .dai_link = snd_rpi_googlevoicehat_soundcard_dai,
328 + .num_links = ARRAY_SIZE(snd_rpi_googlevoicehat_soundcard_dai),
329 +};
330 +
331 +static int snd_rpi_googlevoicehat_soundcard_probe(struct platform_device *pdev)
332 +{
333 + int ret = 0;
334 +
335 + snd_rpi_googlevoicehat_soundcard.dev = &pdev->dev;
336 +
337 + if (pdev->dev.of_node) {
338 + struct device_node *i2s_node;
339 + struct snd_soc_dai_link *dai = &snd_rpi_googlevoicehat_soundcard_dai[0];
340 + i2s_node = of_parse_phandle(pdev->dev.of_node,
341 + "i2s-controller", 0);
342 +
343 + if (i2s_node) {
344 + dai->cpu_dai_name = NULL;
345 + dai->cpu_of_node = i2s_node;
346 + dai->platform_name = NULL;
347 + dai->platform_of_node = i2s_node;
348 + }
349 + }
350 +
351 + ret = snd_soc_register_card(&snd_rpi_googlevoicehat_soundcard);
352 + if (ret)
353 + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
354 +
355 + return ret;
356 +}
357 +
358 +static int snd_rpi_googlevoicehat_soundcard_remove(struct platform_device *pdev)
359 +{
360 + return snd_soc_unregister_card(&snd_rpi_googlevoicehat_soundcard);
361 +}
362 +
363 +static const struct of_device_id snd_rpi_googlevoicehat_soundcard_of_match[] = {
364 + { .compatible = "googlevoicehat,googlevoicehat-soundcard", },
365 + {},
366 +};
367 +MODULE_DEVICE_TABLE(of, snd_rpi_googlevoicehat_soundcard_of_match);
368 +
369 +static struct platform_driver snd_rpi_googlevoicehat_soundcard_driver = {
370 + .driver = {
371 + .name = "snd-googlevoicehat-soundcard",
372 + .owner = THIS_MODULE,
373 + .of_match_table = snd_rpi_googlevoicehat_soundcard_of_match,
374 + },
375 + .probe = snd_rpi_googlevoicehat_soundcard_probe,
376 + .remove = snd_rpi_googlevoicehat_soundcard_remove,
377 +};
378 +
379 +module_platform_driver(snd_rpi_googlevoicehat_soundcard_driver);
380 +
381 +MODULE_AUTHOR("Peter Malkin <petermalkin@google.com>");
382 +MODULE_DESCRIPTION("ASoC Driver for Google voiceHAT SoundCard");
383 +MODULE_LICENSE("GPL v2");