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.
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
15 --- a/sound/soc/bcm/Kconfig
16 +++ b/sound/soc/bcm/Kconfig
17 @@ -18,6 +18,13 @@ config SND_SOC_CYGNUS
19 If you don't know what to do here, say N.
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
26 + Say Y or M if you want to add support for voiceHAT soundcard.
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
35 obj-$(CONFIG_SND_SOC_CYGNUS) += snd-soc-cygnus.o
37 +# Google voiceHAT custom codec support
38 +snd-soc-googlevoicehat-codec-objs := googlevoicehat-codec.o
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
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
56 +++ b/sound/soc/bcm/googlevoicehat-codec.c
59 + * Driver for the Google voiceHAT audio codec for Raspberry Pi.
61 + * Author: Peter Malkin <petermalkin@google.com>
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.
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.
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>
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 */
95 +struct voicehat_priv {
96 + struct delayed_work enable_sdmode_work;
97 + struct gpio_desc *sdmode_gpio;
98 + unsigned int sdmode_delay;
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);
107 +static int voicehat_codec_probe(struct snd_soc_codec *codec) {
108 + struct voicehat_priv *voicehat = snd_soc_codec_get_drvdata(codec);
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);
116 + INIT_DELAYED_WORK(&voicehat->enable_sdmode_work, voicehat_enable_sdmode_work);
120 +static int voicehat_codec_remove(struct snd_soc_codec *codec) {
121 + struct voicehat_priv *voicehat = snd_soc_codec_get_drvdata(codec);
123 + cancel_delayed_work_sync(&voicehat->enable_sdmode_work);
128 +static const struct snd_soc_dapm_widget voicehat_dapm_widgets[] = {
129 + SND_SOC_DAPM_OUTPUT("Speaker"),
132 +static const struct snd_soc_dapm_route voicehat_dapm_routes[] = {
133 + {"Speaker", NULL, "HiFi Playback"},
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 = {
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)
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);
156 + if (voicehat->sdmode_delay == 0) return 0;
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);
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));
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);
186 +static const struct snd_soc_dai_ops voicehat_dai_ops = {
187 + .trigger = voicehat_daiops_trigger,
190 +static struct snd_soc_dai_driver voicehat_dai = {
191 + .name = "voicehat-hifi",
192 + .capture = {.stream_name = "HiFi Capture",
195 + .rates = SNDRV_PCM_RATE_48000,
196 + .formats = SNDRV_PCM_FMTBIT_S32_LE},
197 + .playback = {.stream_name = "HiFi Playback",
200 + .rates = SNDRV_PCM_RATE_48000,
201 + .formats = SNDRV_PCM_FMTBIT_S32_LE},
202 + .ops = &voicehat_dai_ops,
203 + .symmetric_rates = 1};
206 +static const struct of_device_id voicehat_ids[] = {
208 + .compatible = "google,voicehat",
211 +MODULE_DEVICE_TABLE(of, voicehat_ids);
214 +static int voicehat_platform_probe(struct platform_device *pdev) {
215 + struct voicehat_priv *voicehat;
218 + voicehat = devm_kzalloc(&pdev->dev, sizeof(*voicehat), GFP_KERNEL);
219 + if (!voicehat) return -ENOMEM;
221 + ret = device_property_read_u32(&pdev->dev, "voicehat_sdmode_delay",
222 + &voicehat->sdmode_delay);
225 + voicehat->sdmode_delay = SDMODE_DELAY_MS;
226 + dev_info(&pdev->dev,
227 + "property 'voicehat_sdmode_delay' not found default 5 mS");
229 + dev_info(&pdev->dev, "property 'voicehat_sdmode_delay' found delay= %d mS",
230 + voicehat->sdmode_delay);
233 + dev_set_drvdata(&pdev->dev, voicehat);
235 + return snd_soc_register_codec(&pdev->dev, &voicehat_codec_driver, &voicehat_dai, 1);
238 +static int voicehat_platform_remove(struct platform_device *pdev) {
239 + snd_soc_unregister_codec(&pdev->dev);
243 +static struct platform_driver voicehat_driver = {
246 + .name = "voicehat-codec", .of_match_table = of_match_ptr(voicehat_ids),
248 + .probe = voicehat_platform_probe,
249 + .remove = voicehat_platform_remove,
252 +module_platform_driver(voicehat_driver);
254 +MODULE_DESCRIPTION("Google voiceHAT Codec driver");
255 +MODULE_AUTHOR("Peter Malkin <petermalkin@google.com>");
256 +MODULE_LICENSE("GPL v2");
258 +++ b/sound/soc/bcm/googlevoicehat-soundcard.c
261 + * ASoC Driver for Google voiceHAT SoundCard
263 + * Author: Peter Malkin <petermalkin@google.com>
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.
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.
276 +#include <linux/module.h>
277 +#include <linux/platform_device.h>
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>
285 +static int snd_rpi_googlevoicehat_soundcard_init(struct snd_soc_pcm_runtime *rtd)
290 +static int snd_rpi_googlevoicehat_soundcard_hw_params(
291 + struct snd_pcm_substream *substream,
292 + struct snd_pcm_hw_params *params)
294 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
295 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
297 + unsigned int sample_bits =
298 + snd_pcm_format_physical_width(params_format(params));
300 + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
303 +/* machine stream operations */
304 +static struct snd_soc_ops snd_rpi_googlevoicehat_soundcard_ops = {
305 + .hw_params = snd_rpi_googlevoicehat_soundcard_hw_params,
308 +static struct snd_soc_dai_link snd_rpi_googlevoicehat_soundcard_dai[] = {
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,
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),
331 +static int snd_rpi_googlevoicehat_soundcard_probe(struct platform_device *pdev)
335 + snd_rpi_googlevoicehat_soundcard.dev = &pdev->dev;
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);
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;
351 + ret = snd_soc_register_card(&snd_rpi_googlevoicehat_soundcard);
353 + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
358 +static int snd_rpi_googlevoicehat_soundcard_remove(struct platform_device *pdev)
360 + return snd_soc_unregister_card(&snd_rpi_googlevoicehat_soundcard);
363 +static const struct of_device_id snd_rpi_googlevoicehat_soundcard_of_match[] = {
364 + { .compatible = "googlevoicehat,googlevoicehat-soundcard", },
367 +MODULE_DEVICE_TABLE(of, snd_rpi_googlevoicehat_soundcard_of_match);
369 +static struct platform_driver snd_rpi_googlevoicehat_soundcard_driver = {
371 + .name = "snd-googlevoicehat-soundcard",
372 + .owner = THIS_MODULE,
373 + .of_match_table = snd_rpi_googlevoicehat_soundcard_of_match,
375 + .probe = snd_rpi_googlevoicehat_soundcard_probe,
376 + .remove = snd_rpi_googlevoicehat_soundcard_remove,
379 +module_platform_driver(snd_rpi_googlevoicehat_soundcard_driver);
381 +MODULE_AUTHOR("Peter Malkin <petermalkin@google.com>");
382 +MODULE_DESCRIPTION("ASoC Driver for Google voiceHAT SoundCard");
383 +MODULE_LICENSE("GPL v2");