ath79: add support for COMFAST CF-E130N v2
[oweals/openwrt.git] / target / linux / bcm27xx / patches-4.19 / 950-0082-Driver-support-for-Google-voiceHAT-soundcard.patch
1 From ae0077658c007643020b88e233150cf1eca6cea8 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] Driver support for Google voiceHAT soundcard.
5
6 ASoC: googlevoicehat-codec: Use correct device when grabbing GPIO
7
8 The fixup for the VoiceHAT in 4.18 incorrectly tried to find the
9 sdmode GPIO pin under the card device, not the codec device.
10 This failed, and therefore caused the device probe to fail.
11
12 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
13
14 ASoC: googlevoicehat-codec: Reformat for kernel coding standards
15
16 Fix all whitespace, indentation, and bracing errors.
17
18 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
19
20 ASoC: googlevoicehat-codec: Make driver function structure const
21
22 Make voicehat_component_driver a const structure.
23
24 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
25
26 ASoC: googlevoicehat-codec: Only convert from ms to jiffies once
27
28 Minor optimisation and allows to become checkpatch clean.
29 A msec value is read out of DT or from a define, and convert once to
30 jiffies, rather than every time that it is used.
31
32 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
33 ---
34  sound/soc/bcm/googlevoicehat-codec.c | 214 +++++++++++++++++++++++++++
35  1 file changed, 214 insertions(+)
36  create mode 100644 sound/soc/bcm/googlevoicehat-codec.c
37
38 --- /dev/null
39 +++ b/sound/soc/bcm/googlevoicehat-codec.c
40 @@ -0,0 +1,214 @@
41 +/*
42 + * Driver for the Google voiceHAT audio codec for Raspberry Pi.
43 + *
44 + * Author:     Peter Malkin <petermalkin@google.com>
45 + *             Copyright 2016
46 + *
47 + * This program is free software; you can redistribute it and/or
48 + * modify it under the terms of the GNU General Public License
49 + * version 2 as published by the Free Software Foundation.
50 + *
51 + * This program is distributed in the hope that it will be useful, but
52 + * WITHOUT ANY WARRANTY; without even the implied warranty of
53 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
54 + * General Public License for more details.
55 + */
56 +
57 +#include <linux/device.h>
58 +#include <linux/err.h>
59 +#include <linux/gpio.h>
60 +#include <linux/gpio/consumer.h>
61 +#include <linux/init.h>
62 +#include <linux/kernel.h>
63 +#include <linux/mod_devicetable.h>
64 +#include <linux/module.h>
65 +#include <linux/of.h>
66 +#include <linux/platform_device.h>
67 +#include <linux/version.h>
68 +#include <sound/pcm.h>
69 +#include <sound/soc.h>
70 +#include <sound/soc-dai.h>
71 +#include <sound/soc-dapm.h>
72 +
73 +#define ICS43432_RATE_MIN_HZ   7190  /* from data sheet */
74 +#define ICS43432_RATE_MAX_HZ   52800 /* from data sheet */
75 +/* Delay in enabling SDMODE after clock settles to remove pop */
76 +#define SDMODE_DELAY_MS                5
77 +
78 +struct voicehat_priv {
79 +       struct delayed_work enable_sdmode_work;
80 +       struct gpio_desc *sdmode_gpio;
81 +       unsigned long sdmode_delay_jiffies;
82 +};
83 +
84 +static void voicehat_enable_sdmode_work(struct work_struct *work)
85 +{
86 +       struct voicehat_priv *voicehat = container_of(work,
87 +                                                     struct voicehat_priv,
88 +                                                     enable_sdmode_work.work);
89 +       gpiod_set_value(voicehat->sdmode_gpio, 1);
90 +}
91 +
92 +static int voicehat_component_probe(struct snd_soc_component *component)
93 +{
94 +       struct voicehat_priv *voicehat =
95 +                               snd_soc_component_get_drvdata(component);
96 +
97 +       voicehat->sdmode_gpio = devm_gpiod_get(component->dev, "sdmode",
98 +                                              GPIOD_OUT_LOW);
99 +       if (IS_ERR(voicehat->sdmode_gpio)) {
100 +               dev_err(component->dev, "Unable to allocate GPIO pin\n");
101 +               return PTR_ERR(voicehat->sdmode_gpio);
102 +       }
103 +
104 +       INIT_DELAYED_WORK(&voicehat->enable_sdmode_work,
105 +                         voicehat_enable_sdmode_work);
106 +       return 0;
107 +}
108 +
109 +static void voicehat_component_remove(struct snd_soc_component *component)
110 +{
111 +       struct voicehat_priv *voicehat =
112 +                               snd_soc_component_get_drvdata(component);
113 +
114 +       cancel_delayed_work_sync(&voicehat->enable_sdmode_work);
115 +}
116 +
117 +static const struct snd_soc_dapm_widget voicehat_dapm_widgets[] = {
118 +       SND_SOC_DAPM_OUTPUT("Speaker"),
119 +};
120 +
121 +static const struct snd_soc_dapm_route voicehat_dapm_routes[] = {
122 +       {"Speaker", NULL, "HiFi Playback"},
123 +};
124 +
125 +static const struct snd_soc_component_driver voicehat_component_driver = {
126 +       .probe = voicehat_component_probe,
127 +       .remove = voicehat_component_remove,
128 +       .dapm_widgets = voicehat_dapm_widgets,
129 +       .num_dapm_widgets = ARRAY_SIZE(voicehat_dapm_widgets),
130 +       .dapm_routes = voicehat_dapm_routes,
131 +       .num_dapm_routes = ARRAY_SIZE(voicehat_dapm_routes),
132 +};
133 +
134 +static int voicehat_daiops_trigger(struct snd_pcm_substream *substream, int cmd,
135 +                                  struct snd_soc_dai *dai)
136 +{
137 +       struct snd_soc_component *component = dai->component;
138 +       struct voicehat_priv *voicehat =
139 +                               snd_soc_component_get_drvdata(component);
140 +
141 +       if (voicehat->sdmode_delay_jiffies == 0)
142 +               return 0;
143 +
144 +       dev_dbg(dai->dev, "CMD             %d", cmd);
145 +       dev_dbg(dai->dev, "Playback Active %d", dai->playback_active);
146 +       dev_dbg(dai->dev, "Capture Active  %d", dai->capture_active);
147 +
148 +       switch (cmd) {
149 +       case SNDRV_PCM_TRIGGER_START:
150 +       case SNDRV_PCM_TRIGGER_RESUME:
151 +       case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
152 +               if (dai->playback_active) {
153 +                       dev_info(dai->dev, "Enabling audio amp...\n");
154 +                       queue_delayed_work(
155 +                               system_power_efficient_wq,
156 +                               &voicehat->enable_sdmode_work,
157 +                               voicehat->sdmode_delay_jiffies);
158 +               }
159 +               break;
160 +       case SNDRV_PCM_TRIGGER_STOP:
161 +       case SNDRV_PCM_TRIGGER_SUSPEND:
162 +       case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
163 +               if (dai->playback_active) {
164 +                       cancel_delayed_work(&voicehat->enable_sdmode_work);
165 +                       dev_info(dai->dev, "Disabling audio amp...\n");
166 +                       gpiod_set_value(voicehat->sdmode_gpio, 0);
167 +               }
168 +               break;
169 +       }
170 +       return 0;
171 +}
172 +
173 +static const struct snd_soc_dai_ops voicehat_dai_ops = {
174 +       .trigger = voicehat_daiops_trigger,
175 +};
176 +
177 +static struct snd_soc_dai_driver voicehat_dai = {
178 +       .name = "voicehat-hifi",
179 +       .capture = {
180 +               .stream_name = "HiFi Capture",
181 +               .channels_min = 2,
182 +               .channels_max = 2,
183 +               .rates = SNDRV_PCM_RATE_48000,
184 +               .formats = SNDRV_PCM_FMTBIT_S32_LE
185 +       },
186 +       .playback = {
187 +               .stream_name = "HiFi Playback",
188 +               .channels_min = 2,
189 +               .channels_max = 2,
190 +               .rates = SNDRV_PCM_RATE_48000,
191 +               .formats = SNDRV_PCM_FMTBIT_S32_LE
192 +       },
193 +       .ops = &voicehat_dai_ops,
194 +       .symmetric_rates = 1
195 +};
196 +
197 +#ifdef CONFIG_OF
198 +static const struct of_device_id voicehat_ids[] = {
199 +               { .compatible = "google,voicehat", }, {}
200 +       };
201 +       MODULE_DEVICE_TABLE(of, voicehat_ids);
202 +#endif
203 +
204 +static int voicehat_platform_probe(struct platform_device *pdev)
205 +{
206 +       struct voicehat_priv *voicehat;
207 +       unsigned int sdmode_delay;
208 +       int ret;
209 +
210 +       voicehat = devm_kzalloc(&pdev->dev, sizeof(*voicehat), GFP_KERNEL);
211 +       if (!voicehat)
212 +               return -ENOMEM;
213 +
214 +       ret = device_property_read_u32(&pdev->dev, "voicehat_sdmode_delay",
215 +                                      &sdmode_delay);
216 +
217 +       if (ret) {
218 +               sdmode_delay = SDMODE_DELAY_MS;
219 +               dev_info(&pdev->dev,
220 +                        "property 'voicehat_sdmode_delay' not found default 5 mS");
221 +       } else {
222 +               dev_info(&pdev->dev, "property 'voicehat_sdmode_delay' found delay= %d mS",
223 +                        sdmode_delay);
224 +       }
225 +       voicehat->sdmode_delay_jiffies = msecs_to_jiffies(sdmode_delay);
226 +
227 +       dev_set_drvdata(&pdev->dev, voicehat);
228 +
229 +       return snd_soc_register_component(&pdev->dev,
230 +                                         &voicehat_component_driver,
231 +                                         &voicehat_dai,
232 +                                         1);
233 +}
234 +
235 +static int voicehat_platform_remove(struct platform_device *pdev)
236 +{
237 +       snd_soc_unregister_component(&pdev->dev);
238 +       return 0;
239 +}
240 +
241 +static struct platform_driver voicehat_driver = {
242 +       .driver = {
243 +               .name = "voicehat-codec",
244 +               .of_match_table = of_match_ptr(voicehat_ids),
245 +       },
246 +       .probe = voicehat_platform_probe,
247 +       .remove = voicehat_platform_remove,
248 +};
249 +
250 +module_platform_driver(voicehat_driver);
251 +
252 +MODULE_DESCRIPTION("Google voiceHAT Codec driver");
253 +MODULE_AUTHOR("Peter Malkin <petermalkin@google.com>");
254 +MODULE_LICENSE("GPL v2");