Merge branch 'asoc-5.2' into asoc-5.3
[linux-2.6-microblaze.git] / sound / soc / intel / boards / bytcht_es8316.c
1 /*
2  *  bytcht_es8316.c - ASoc Machine driver for Intel Baytrail/Cherrytrail
3  *                    platforms with Everest ES8316 SoC
4  *
5  *  Copyright (C) 2017 Endless Mobile, Inc.
6  *  Authors: David Yang <yangxiaohua@everest-semi.com>,
7  *           Daniel Drake <drake@endlessm.com>
8  *
9  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; version 2 of the License.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21  */
22 #include <linux/acpi.h>
23 #include <linux/clk.h>
24 #include <linux/device.h>
25 #include <linux/dmi.h>
26 #include <linux/gpio/consumer.h>
27 #include <linux/i2c.h>
28 #include <linux/init.h>
29 #include <linux/input.h>
30 #include <linux/module.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33 #include <sound/jack.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/soc.h>
37 #include <sound/soc-acpi.h>
38 #include "../atom/sst-atom-controls.h"
39 #include "../common/sst-dsp.h"
40 #include "../common/soc-intel-quirks.h"
41
42 /* jd-inv + terminating entry */
43 #define MAX_NO_PROPS 2
44
45 struct byt_cht_es8316_private {
46         struct clk *mclk;
47         struct snd_soc_jack jack;
48         struct gpio_desc *speaker_en_gpio;
49         bool speaker_en;
50 };
51
52 enum {
53         BYT_CHT_ES8316_INTMIC_IN1_MAP,
54         BYT_CHT_ES8316_INTMIC_IN2_MAP,
55 };
56
57 #define BYT_CHT_ES8316_MAP(quirk)               ((quirk) & GENMASK(3, 0))
58 #define BYT_CHT_ES8316_SSP0                     BIT(16)
59 #define BYT_CHT_ES8316_MONO_SPEAKER             BIT(17)
60 #define BYT_CHT_ES8316_JD_INVERTED              BIT(18)
61
62 static unsigned long quirk;
63
64 static int quirk_override = -1;
65 module_param_named(quirk, quirk_override, int, 0444);
66 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
67
68 static void log_quirks(struct device *dev)
69 {
70         if (BYT_CHT_ES8316_MAP(quirk) == BYT_CHT_ES8316_INTMIC_IN1_MAP)
71                 dev_info(dev, "quirk IN1_MAP enabled");
72         if (BYT_CHT_ES8316_MAP(quirk) == BYT_CHT_ES8316_INTMIC_IN2_MAP)
73                 dev_info(dev, "quirk IN2_MAP enabled");
74         if (quirk & BYT_CHT_ES8316_SSP0)
75                 dev_info(dev, "quirk SSP0 enabled");
76         if (quirk & BYT_CHT_ES8316_MONO_SPEAKER)
77                 dev_info(dev, "quirk MONO_SPEAKER enabled\n");
78         if (quirk & BYT_CHT_ES8316_JD_INVERTED)
79                 dev_info(dev, "quirk JD_INVERTED enabled\n");
80 }
81
82 static int byt_cht_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
83         struct snd_kcontrol *kcontrol, int event)
84 {
85         struct snd_soc_card *card = w->dapm->card;
86         struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
87
88         if (SND_SOC_DAPM_EVENT_ON(event))
89                 priv->speaker_en = true;
90         else
91                 priv->speaker_en = false;
92
93         gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
94
95         return 0;
96 }
97
98 static const struct snd_soc_dapm_widget byt_cht_es8316_widgets[] = {
99         SND_SOC_DAPM_SPK("Speaker", NULL),
100         SND_SOC_DAPM_HP("Headphone", NULL),
101         SND_SOC_DAPM_MIC("Headset Mic", NULL),
102         SND_SOC_DAPM_MIC("Internal Mic", NULL),
103
104         SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
105                             byt_cht_es8316_speaker_power_event,
106                             SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
107 };
108
109 static const struct snd_soc_dapm_route byt_cht_es8316_audio_map[] = {
110         {"Headphone", NULL, "HPOL"},
111         {"Headphone", NULL, "HPOR"},
112
113         /*
114          * There is no separate speaker output instead the speakers are muxed to
115          * the HP outputs. The mux is controlled by the "Speaker Power" supply.
116          */
117         {"Speaker", NULL, "HPOL"},
118         {"Speaker", NULL, "HPOR"},
119         {"Speaker", NULL, "Speaker Power"},
120 };
121
122 static const struct snd_soc_dapm_route byt_cht_es8316_intmic_in1_map[] = {
123         {"MIC1", NULL, "Internal Mic"},
124         {"MIC2", NULL, "Headset Mic"},
125 };
126
127 static const struct snd_soc_dapm_route byt_cht_es8316_intmic_in2_map[] = {
128         {"MIC2", NULL, "Internal Mic"},
129         {"MIC1", NULL, "Headset Mic"},
130 };
131
132 static const struct snd_soc_dapm_route byt_cht_es8316_ssp0_map[] = {
133         {"Playback", NULL, "ssp0 Tx"},
134         {"ssp0 Tx", NULL, "modem_out"},
135         {"modem_in", NULL, "ssp0 Rx"},
136         {"ssp0 Rx", NULL, "Capture"},
137 };
138
139 static const struct snd_soc_dapm_route byt_cht_es8316_ssp2_map[] = {
140         {"Playback", NULL, "ssp2 Tx"},
141         {"ssp2 Tx", NULL, "codec_out0"},
142         {"ssp2 Tx", NULL, "codec_out1"},
143         {"codec_in0", NULL, "ssp2 Rx" },
144         {"codec_in1", NULL, "ssp2 Rx" },
145         {"ssp2 Rx", NULL, "Capture"},
146 };
147
148 static const struct snd_kcontrol_new byt_cht_es8316_controls[] = {
149         SOC_DAPM_PIN_SWITCH("Speaker"),
150         SOC_DAPM_PIN_SWITCH("Headphone"),
151         SOC_DAPM_PIN_SWITCH("Headset Mic"),
152         SOC_DAPM_PIN_SWITCH("Internal Mic"),
153 };
154
155 static struct snd_soc_jack_pin byt_cht_es8316_jack_pins[] = {
156         {
157                 .pin    = "Headphone",
158                 .mask   = SND_JACK_HEADPHONE,
159         },
160         {
161                 .pin    = "Headset Mic",
162                 .mask   = SND_JACK_MICROPHONE,
163         },
164 };
165
166 static int byt_cht_es8316_init(struct snd_soc_pcm_runtime *runtime)
167 {
168         struct snd_soc_component *codec = runtime->codec_dai->component;
169         struct snd_soc_card *card = runtime->card;
170         struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
171         const struct snd_soc_dapm_route *custom_map;
172         int num_routes;
173         int ret;
174
175         card->dapm.idle_bias_off = true;
176
177         switch (BYT_CHT_ES8316_MAP(quirk)) {
178         case BYT_CHT_ES8316_INTMIC_IN1_MAP:
179         default:
180                 custom_map = byt_cht_es8316_intmic_in1_map;
181                 num_routes = ARRAY_SIZE(byt_cht_es8316_intmic_in1_map);
182                 break;
183         case BYT_CHT_ES8316_INTMIC_IN2_MAP:
184                 custom_map = byt_cht_es8316_intmic_in2_map;
185                 num_routes = ARRAY_SIZE(byt_cht_es8316_intmic_in2_map);
186                 break;
187         }
188         ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
189         if (ret)
190                 return ret;
191
192         if (quirk & BYT_CHT_ES8316_SSP0) {
193                 custom_map = byt_cht_es8316_ssp0_map;
194                 num_routes = ARRAY_SIZE(byt_cht_es8316_ssp0_map);
195         } else {
196                 custom_map = byt_cht_es8316_ssp2_map;
197                 num_routes = ARRAY_SIZE(byt_cht_es8316_ssp2_map);
198         }
199         ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
200         if (ret)
201                 return ret;
202
203         /*
204          * The firmware might enable the clock at boot (this information
205          * may or may not be reflected in the enable clock register).
206          * To change the rate we must disable the clock first to cover these
207          * cases. Due to common clock framework restrictions that do not allow
208          * to disable a clock that has not been enabled, we need to enable
209          * the clock first.
210          */
211         ret = clk_prepare_enable(priv->mclk);
212         if (!ret)
213                 clk_disable_unprepare(priv->mclk);
214
215         ret = clk_set_rate(priv->mclk, 19200000);
216         if (ret)
217                 dev_err(card->dev, "unable to set MCLK rate\n");
218
219         ret = clk_prepare_enable(priv->mclk);
220         if (ret)
221                 dev_err(card->dev, "unable to enable MCLK\n");
222
223         ret = snd_soc_dai_set_sysclk(runtime->codec_dai, 0, 19200000,
224                                      SND_SOC_CLOCK_IN);
225         if (ret < 0) {
226                 dev_err(card->dev, "can't set codec clock %d\n", ret);
227                 return ret;
228         }
229
230         ret = snd_soc_card_jack_new(card, "Headset",
231                                     SND_JACK_HEADSET | SND_JACK_BTN_0,
232                                     &priv->jack, byt_cht_es8316_jack_pins,
233                                     ARRAY_SIZE(byt_cht_es8316_jack_pins));
234         if (ret) {
235                 dev_err(card->dev, "jack creation failed %d\n", ret);
236                 return ret;
237         }
238
239         snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
240         snd_soc_component_set_jack(codec, &priv->jack, NULL);
241
242         return 0;
243 }
244
245 static const struct snd_soc_pcm_stream byt_cht_es8316_dai_params = {
246         .formats = SNDRV_PCM_FMTBIT_S24_LE,
247         .rate_min = 48000,
248         .rate_max = 48000,
249         .channels_min = 2,
250         .channels_max = 2,
251 };
252
253 static int byt_cht_es8316_codec_fixup(struct snd_soc_pcm_runtime *rtd,
254                             struct snd_pcm_hw_params *params)
255 {
256         struct snd_interval *rate = hw_param_interval(params,
257                         SNDRV_PCM_HW_PARAM_RATE);
258         struct snd_interval *channels = hw_param_interval(params,
259                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
260         int ret, bits;
261
262         /* The DSP will covert the FE rate to 48k, stereo */
263         rate->min = rate->max = 48000;
264         channels->min = channels->max = 2;
265
266         if (quirk & BYT_CHT_ES8316_SSP0) {
267                 /* set SSP0 to 16-bit */
268                 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
269                 bits = 16;
270         } else {
271                 /* set SSP2 to 24-bit */
272                 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
273                 bits = 24;
274         }
275
276         /*
277          * Default mode for SSP configuration is TDM 4 slot, override config
278          * with explicit setting to I2S 2ch 24-bit. The word length is set with
279          * dai_set_tdm_slot() since there is no other API exposed
280          */
281         ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
282                                 SND_SOC_DAIFMT_I2S     |
283                                 SND_SOC_DAIFMT_NB_NF   |
284                                 SND_SOC_DAIFMT_CBS_CFS
285                 );
286         if (ret < 0) {
287                 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
288                 return ret;
289         }
290
291         ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, bits);
292         if (ret < 0) {
293                 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
294                 return ret;
295         }
296
297         return 0;
298 }
299
300 static int byt_cht_es8316_aif1_startup(struct snd_pcm_substream *substream)
301 {
302         return snd_pcm_hw_constraint_single(substream->runtime,
303                         SNDRV_PCM_HW_PARAM_RATE, 48000);
304 }
305
306 static const struct snd_soc_ops byt_cht_es8316_aif1_ops = {
307         .startup = byt_cht_es8316_aif1_startup,
308 };
309
310 SND_SOC_DAILINK_DEF(dummy,
311         DAILINK_COMP_ARRAY(COMP_DUMMY()));
312
313 SND_SOC_DAILINK_DEF(media,
314         DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
315
316 SND_SOC_DAILINK_DEF(deepbuffer,
317         DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
318
319 SND_SOC_DAILINK_DEF(ssp2_port,
320         DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
321 SND_SOC_DAILINK_DEF(ssp2_codec,
322         DAILINK_COMP_ARRAY(COMP_CODEC("i2c-ESSX8316:00", "ES8316 HiFi")));
323
324 SND_SOC_DAILINK_DEF(platform,
325         DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
326
327 static struct snd_soc_dai_link byt_cht_es8316_dais[] = {
328         [MERR_DPCM_AUDIO] = {
329                 .name = "Audio Port",
330                 .stream_name = "Audio",
331                 .nonatomic = true,
332                 .dynamic = 1,
333                 .dpcm_playback = 1,
334                 .dpcm_capture = 1,
335                 .ops = &byt_cht_es8316_aif1_ops,
336                 SND_SOC_DAILINK_REG(media, dummy, platform),
337         },
338
339         [MERR_DPCM_DEEP_BUFFER] = {
340                 .name = "Deep-Buffer Audio Port",
341                 .stream_name = "Deep-Buffer Audio",
342                 .nonatomic = true,
343                 .dynamic = 1,
344                 .dpcm_playback = 1,
345                 .ops = &byt_cht_es8316_aif1_ops,
346                 SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
347         },
348
349                 /* back ends */
350         {
351                 /* Only SSP2 has been tested here, so BYT-CR platforms that
352                  * require SSP0 will not work.
353                  */
354                 .name = "SSP2-Codec",
355                 .id = 0,
356                 .no_pcm = 1,
357                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
358                                                 | SND_SOC_DAIFMT_CBS_CFS,
359                 .be_hw_params_fixup = byt_cht_es8316_codec_fixup,
360                 .nonatomic = true,
361                 .dpcm_playback = 1,
362                 .dpcm_capture = 1,
363                 .init = byt_cht_es8316_init,
364                 SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
365         },
366 };
367
368
369 /* SoC card */
370 static char codec_name[SND_ACPI_I2C_ID_LEN];
371 static char long_name[50]; /* = "bytcht-es8316-*-spk-*-mic" */
372
373 static int byt_cht_es8316_suspend(struct snd_soc_card *card)
374 {
375         struct snd_soc_component *component;
376
377         for_each_card_components(card, component) {
378                 if (!strcmp(component->name, codec_name)) {
379                         dev_dbg(component->dev, "disabling jack detect before suspend\n");
380                         snd_soc_component_set_jack(component, NULL, NULL);
381                         break;
382                 }
383         }
384
385         return 0;
386 }
387
388 static int byt_cht_es8316_resume(struct snd_soc_card *card)
389 {
390         struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
391         struct snd_soc_component *component;
392
393         for_each_card_components(card, component) {
394                 if (!strcmp(component->name, codec_name)) {
395                         dev_dbg(component->dev, "re-enabling jack detect after resume\n");
396                         snd_soc_component_set_jack(component, &priv->jack, NULL);
397                         break;
398                 }
399         }
400
401         /*
402          * Some Cherry Trail boards with an ES8316 codec have a bug in their
403          * ACPI tables where the MSSL1680 touchscreen's _PS0 and _PS3 methods
404          * wrongly also set the speaker-enable GPIO to 1/0. Testing has shown
405          * that this really is a bug and the GPIO has no influence on the
406          * touchscreen at all.
407          *
408          * The silead.c touchscreen driver does not support runtime suspend, so
409          * the GPIO can only be changed underneath us during a system suspend.
410          * This resume() function runs from a pm complete() callback, and thus
411          * is guaranteed to run after the touchscreen driver/ACPI-subsys has
412          * brought the touchscreen back up again (and thus changed the GPIO).
413          *
414          * So to work around this we pass GPIOD_FLAGS_BIT_NONEXCLUSIVE when
415          * requesting the GPIO and we set its value here to undo any changes
416          * done by the touchscreen's broken _PS0 ACPI method.
417          */
418         gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
419
420         return 0;
421 }
422
423 static struct snd_soc_card byt_cht_es8316_card = {
424         .name = "bytcht-es8316",
425         .owner = THIS_MODULE,
426         .dai_link = byt_cht_es8316_dais,
427         .num_links = ARRAY_SIZE(byt_cht_es8316_dais),
428         .dapm_widgets = byt_cht_es8316_widgets,
429         .num_dapm_widgets = ARRAY_SIZE(byt_cht_es8316_widgets),
430         .dapm_routes = byt_cht_es8316_audio_map,
431         .num_dapm_routes = ARRAY_SIZE(byt_cht_es8316_audio_map),
432         .controls = byt_cht_es8316_controls,
433         .num_controls = ARRAY_SIZE(byt_cht_es8316_controls),
434         .fully_routed = true,
435         .suspend_pre = byt_cht_es8316_suspend,
436         .resume_post = byt_cht_es8316_resume,
437 };
438
439 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
440
441 static const struct acpi_gpio_mapping byt_cht_es8316_gpios[] = {
442         { "speaker-enable-gpios", &first_gpio, 1 },
443         { },
444 };
445
446 /* Please keep this list alphabetically sorted */
447 static const struct dmi_system_id byt_cht_es8316_quirk_table[] = {
448         {       /* Teclast X98 Plus II */
449                 .matches = {
450                         DMI_MATCH(DMI_SYS_VENDOR, "TECLAST"),
451                         DMI_MATCH(DMI_PRODUCT_NAME, "X98 Plus II"),
452                 },
453                 .driver_data = (void *)(BYT_CHT_ES8316_INTMIC_IN1_MAP
454                                         | BYT_CHT_ES8316_JD_INVERTED),
455         },
456         {}
457 };
458
459 static int snd_byt_cht_es8316_mc_probe(struct platform_device *pdev)
460 {
461         static const char * const mic_name[] = { "in1", "in2" };
462         struct property_entry props[MAX_NO_PROPS] = {};
463         struct byt_cht_es8316_private *priv;
464         const struct dmi_system_id *dmi_id;
465         struct device *dev = &pdev->dev;
466         struct snd_soc_acpi_mach *mach;
467         const char *platform_name;
468         struct acpi_device *adev;
469         struct device *codec_dev;
470         unsigned int cnt = 0;
471         int dai_index = 0;
472         int i;
473         int ret = 0;
474
475         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
476         if (!priv)
477                 return -ENOMEM;
478
479         mach = dev->platform_data;
480         /* fix index of codec dai */
481         for (i = 0; i < ARRAY_SIZE(byt_cht_es8316_dais); i++) {
482                 if (!strcmp(byt_cht_es8316_dais[i].codecs->name,
483                             "i2c-ESSX8316:00")) {
484                         dai_index = i;
485                         break;
486                 }
487         }
488
489         /* fixup codec name based on HID */
490         adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
491         if (adev) {
492                 snprintf(codec_name, sizeof(codec_name),
493                          "i2c-%s", acpi_dev_name(adev));
494                 put_device(&adev->dev);
495                 byt_cht_es8316_dais[dai_index].codecs->name = codec_name;
496         }
497
498         /* override plaform name, if required */
499         byt_cht_es8316_card.dev = dev;
500         platform_name = mach->mach_params.platform;
501
502         ret = snd_soc_fixup_dai_links_platform_name(&byt_cht_es8316_card,
503                                                     platform_name);
504         if (ret)
505                 return ret;
506
507         /* Check for BYTCR or other platform and setup quirks */
508         dmi_id = dmi_first_match(byt_cht_es8316_quirk_table);
509         if (dmi_id) {
510                 quirk = (unsigned long)dmi_id->driver_data;
511         } else if (soc_intel_is_byt() &&
512                    mach->mach_params.acpi_ipc_irq_index == 0) {
513                 /* On BYTCR default to SSP0, internal-mic-in2-map, mono-spk */
514                 quirk = BYT_CHT_ES8316_SSP0 | BYT_CHT_ES8316_INTMIC_IN2_MAP |
515                         BYT_CHT_ES8316_MONO_SPEAKER;
516         } else {
517                 /* Others default to internal-mic-in1-map, mono-speaker */
518                 quirk = BYT_CHT_ES8316_INTMIC_IN1_MAP |
519                         BYT_CHT_ES8316_MONO_SPEAKER;
520         }
521         if (quirk_override != -1) {
522                 dev_info(dev, "Overriding quirk 0x%x => 0x%x\n",
523                          (unsigned int)quirk,
524                          quirk_override);
525                 quirk = quirk_override;
526         }
527         log_quirks(dev);
528
529         if (quirk & BYT_CHT_ES8316_SSP0)
530                 byt_cht_es8316_dais[dai_index].cpus->dai_name = "ssp0-port";
531
532         /* get the clock */
533         priv->mclk = devm_clk_get(dev, "pmc_plt_clk_3");
534         if (IS_ERR(priv->mclk)) {
535                 ret = PTR_ERR(priv->mclk);
536                 dev_err(dev, "clk_get pmc_plt_clk_3 failed: %d\n", ret);
537                 return ret;
538         }
539
540         /* get speaker enable GPIO */
541         codec_dev = bus_find_device_by_name(&i2c_bus_type, NULL, codec_name);
542         if (!codec_dev)
543                 return -EPROBE_DEFER;
544
545         if (quirk & BYT_CHT_ES8316_JD_INVERTED)
546                 props[cnt++] = PROPERTY_ENTRY_BOOL("everest,jack-detect-inverted");
547
548         if (cnt) {
549                 ret = device_add_properties(codec_dev, props);
550                 if (ret)
551                         return ret;
552         }
553
554         devm_acpi_dev_add_driver_gpios(codec_dev, byt_cht_es8316_gpios);
555         priv->speaker_en_gpio =
556                 gpiod_get_index(codec_dev, "speaker-enable", 0,
557                                 /* see comment in byt_cht_es8316_resume */
558                                 GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
559         put_device(codec_dev);
560
561         if (IS_ERR(priv->speaker_en_gpio)) {
562                 ret = PTR_ERR(priv->speaker_en_gpio);
563                 switch (ret) {
564                 case -ENOENT:
565                         priv->speaker_en_gpio = NULL;
566                         break;
567                 default:
568                         dev_err(dev, "get speaker GPIO failed: %d\n", ret);
569                         /* fall through */
570                 case -EPROBE_DEFER:
571                         return ret;
572                 }
573         }
574
575         /* register the soc card */
576         snprintf(long_name, sizeof(long_name), "bytcht-es8316-%s-spk-%s-mic",
577                  (quirk & BYT_CHT_ES8316_MONO_SPEAKER) ? "mono" : "stereo",
578                  mic_name[BYT_CHT_ES8316_MAP(quirk)]);
579         byt_cht_es8316_card.long_name = long_name;
580         snd_soc_card_set_drvdata(&byt_cht_es8316_card, priv);
581
582         ret = devm_snd_soc_register_card(dev, &byt_cht_es8316_card);
583         if (ret) {
584                 gpiod_put(priv->speaker_en_gpio);
585                 dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
586                 return ret;
587         }
588         platform_set_drvdata(pdev, &byt_cht_es8316_card);
589         return 0;
590 }
591
592 static int snd_byt_cht_es8316_mc_remove(struct platform_device *pdev)
593 {
594         struct snd_soc_card *card = platform_get_drvdata(pdev);
595         struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
596
597         gpiod_put(priv->speaker_en_gpio);
598         return 0;
599 }
600
601 static struct platform_driver snd_byt_cht_es8316_mc_driver = {
602         .driver = {
603                 .name = "bytcht_es8316",
604         },
605         .probe = snd_byt_cht_es8316_mc_probe,
606         .remove = snd_byt_cht_es8316_mc_remove,
607 };
608
609 module_platform_driver(snd_byt_cht_es8316_mc_driver);
610 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail Machine driver");
611 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
612 MODULE_LICENSE("GPL v2");
613 MODULE_ALIAS("platform:bytcht_es8316");