2 * Driver for the Google voiceHAT audio codec for Raspberry Pi.
4 * Author: Peter Malkin <petermalkin@google.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/gpio.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/version.h>
28 #include <sound/pcm.h>
29 #include <sound/soc.h>
30 #include <sound/soc-dai.h>
31 #include <sound/soc-dapm.h>
33 #define ICS43432_RATE_MIN_HZ 7190 /* from data sheet */
34 #define ICS43432_RATE_MAX_HZ 52800 /* from data sheet */
35 /* Delay in enabling SDMODE after clock settles to remove pop */
36 #define SDMODE_DELAY_MS 5
38 struct voicehat_priv {
39 struct delayed_work enable_sdmode_work;
40 struct gpio_desc *sdmode_gpio;
41 unsigned long sdmode_delay_jiffies;
44 static void voicehat_enable_sdmode_work(struct work_struct *work)
46 struct voicehat_priv *voicehat = container_of(work,
48 enable_sdmode_work.work);
49 gpiod_set_value(voicehat->sdmode_gpio, 1);
52 static int voicehat_component_probe(struct snd_soc_component *component)
54 struct voicehat_priv *voicehat =
55 snd_soc_component_get_drvdata(component);
57 voicehat->sdmode_gpio = devm_gpiod_get(component->dev, "sdmode",
59 if (IS_ERR(voicehat->sdmode_gpio)) {
60 dev_err(component->dev, "Unable to allocate GPIO pin\n");
61 return PTR_ERR(voicehat->sdmode_gpio);
64 INIT_DELAYED_WORK(&voicehat->enable_sdmode_work,
65 voicehat_enable_sdmode_work);
69 static void voicehat_component_remove(struct snd_soc_component *component)
71 struct voicehat_priv *voicehat =
72 snd_soc_component_get_drvdata(component);
74 cancel_delayed_work_sync(&voicehat->enable_sdmode_work);
77 static const struct snd_soc_dapm_widget voicehat_dapm_widgets[] = {
78 SND_SOC_DAPM_OUTPUT("Speaker"),
81 static const struct snd_soc_dapm_route voicehat_dapm_routes[] = {
82 {"Speaker", NULL, "HiFi Playback"},
85 static const struct snd_soc_component_driver voicehat_component_driver = {
86 .probe = voicehat_component_probe,
87 .remove = voicehat_component_remove,
88 .dapm_widgets = voicehat_dapm_widgets,
89 .num_dapm_widgets = ARRAY_SIZE(voicehat_dapm_widgets),
90 .dapm_routes = voicehat_dapm_routes,
91 .num_dapm_routes = ARRAY_SIZE(voicehat_dapm_routes),
94 static int voicehat_daiops_trigger(struct snd_pcm_substream *substream, int cmd,
95 struct snd_soc_dai *dai)
97 struct snd_soc_component *component = dai->component;
98 struct voicehat_priv *voicehat =
99 snd_soc_component_get_drvdata(component);
101 if (voicehat->sdmode_delay_jiffies == 0)
104 dev_dbg(dai->dev, "CMD %d", cmd);
105 dev_dbg(dai->dev, "Playback Active %d", dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK]);
106 dev_dbg(dai->dev, "Capture Active %d", dai->stream_active[SNDRV_PCM_STREAM_CAPTURE]);
109 case SNDRV_PCM_TRIGGER_START:
110 case SNDRV_PCM_TRIGGER_RESUME:
111 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
112 if (dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK]) {
113 dev_info(dai->dev, "Enabling audio amp...\n");
115 system_power_efficient_wq,
116 &voicehat->enable_sdmode_work,
117 voicehat->sdmode_delay_jiffies);
120 case SNDRV_PCM_TRIGGER_STOP:
121 case SNDRV_PCM_TRIGGER_SUSPEND:
122 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
123 if (dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK]) {
124 cancel_delayed_work(&voicehat->enable_sdmode_work);
125 dev_info(dai->dev, "Disabling audio amp...\n");
126 gpiod_set_value(voicehat->sdmode_gpio, 0);
133 static const struct snd_soc_dai_ops voicehat_dai_ops = {
134 .trigger = voicehat_daiops_trigger,
137 static struct snd_soc_dai_driver voicehat_dai = {
138 .name = "voicehat-hifi",
140 .stream_name = "HiFi Capture",
143 .rates = SNDRV_PCM_RATE_48000,
144 .formats = SNDRV_PCM_FMTBIT_S32_LE
147 .stream_name = "HiFi Playback",
150 .rates = SNDRV_PCM_RATE_48000,
151 .formats = SNDRV_PCM_FMTBIT_S32_LE
153 .ops = &voicehat_dai_ops,
158 static const struct of_device_id voicehat_ids[] = {
159 { .compatible = "google,voicehat", }, {}
161 MODULE_DEVICE_TABLE(of, voicehat_ids);
164 static int voicehat_platform_probe(struct platform_device *pdev)
166 struct voicehat_priv *voicehat;
167 unsigned int sdmode_delay;
170 voicehat = devm_kzalloc(&pdev->dev, sizeof(*voicehat), GFP_KERNEL);
174 ret = device_property_read_u32(&pdev->dev, "voicehat_sdmode_delay",
178 sdmode_delay = SDMODE_DELAY_MS;
180 "property 'voicehat_sdmode_delay' not found default 5 mS");
182 dev_info(&pdev->dev, "property 'voicehat_sdmode_delay' found delay= %d mS",
185 voicehat->sdmode_delay_jiffies = msecs_to_jiffies(sdmode_delay);
187 dev_set_drvdata(&pdev->dev, voicehat);
189 return snd_soc_register_component(&pdev->dev,
190 &voicehat_component_driver,
195 static int voicehat_platform_remove(struct platform_device *pdev)
197 snd_soc_unregister_component(&pdev->dev);
201 static struct platform_driver voicehat_driver = {
203 .name = "voicehat-codec",
204 .of_match_table = of_match_ptr(voicehat_ids),
206 .probe = voicehat_platform_probe,
207 .remove = voicehat_platform_remove,
210 module_platform_driver(voicehat_driver);
212 MODULE_DESCRIPTION("Google voiceHAT Codec driver");
213 MODULE_AUTHOR("Peter Malkin <petermalkin@google.com>");
214 MODULE_LICENSE("GPL v2");