*
  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  */
+#include <linux/acpi.h>
 #include <linux/clk.h>
 #include <linux/device.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
 #include <linux/init.h>
 #include <linux/input.h>
 #include <linux/module.h>
 struct byt_cht_es8316_private {
        struct clk *mclk;
        struct snd_soc_jack jack;
+       struct gpio_desc *speaker_en_gpio;
+       bool speaker_en;
 };
 
 #define BYT_CHT_ES8316_SSP0                    BIT(16)
                dev_info(dev, "quirk SSP0 enabled");
 }
 
+static int byt_cht_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
+       struct snd_kcontrol *kcontrol, int event)
+{
+       struct snd_soc_card *card = w->dapm->card;
+       struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
+
+       if (SND_SOC_DAPM_EVENT_ON(event))
+               priv->speaker_en = true;
+       else
+               priv->speaker_en = false;
+
+       gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
+
+       return 0;
+}
+
 static const struct snd_soc_dapm_widget byt_cht_es8316_widgets[] = {
+       SND_SOC_DAPM_SPK("Speaker", NULL),
        SND_SOC_DAPM_HP("Headphone", NULL),
        SND_SOC_DAPM_MIC("Headset Mic", NULL),
 
         */
        SND_SOC_DAPM_MIC("Microphone 1", NULL),
        SND_SOC_DAPM_MIC("Microphone 2", NULL),
+
+       SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
+                           byt_cht_es8316_speaker_power_event,
+                           SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
 };
 
 static const struct snd_soc_dapm_route byt_cht_es8316_audio_map[] = {
 
        {"Headphone", NULL, "HPOL"},
        {"Headphone", NULL, "HPOR"},
+
+       /*
+        * There is no separate speaker output instead the speakers are muxed to
+        * the HP outputs. The mux is controlled by the "Speaker Power" supply.
+        */
+       {"Speaker", NULL, "HPOL"},
+       {"Speaker", NULL, "HPOR"},
+       {"Speaker", NULL, "Speaker Power"},
 };
 
 static const struct snd_soc_dapm_route byt_cht_es8316_ssp0_map[] = {
 };
 
 static const struct snd_kcontrol_new byt_cht_es8316_controls[] = {
+       SOC_DAPM_PIN_SWITCH("Speaker"),
        SOC_DAPM_PIN_SWITCH("Headphone"),
        SOC_DAPM_PIN_SWITCH("Headset Mic"),
        SOC_DAPM_PIN_SWITCH("Microphone 1"),
                }
        }
 
+       /*
+        * Some Cherry Trail boards with an ES8316 codec have a bug in their
+        * ACPI tables where the MSSL1680 touchscreen's _PS0 and _PS3 methods
+        * wrongly also set the speaker-enable GPIO to 1/0. Testing has shown
+        * that this really is a bug and the GPIO has no influence on the
+        * touchscreen at all.
+        *
+        * The silead.c touchscreen driver does not support runtime suspend, so
+        * the GPIO can only be changed underneath us during a system suspend.
+        * This resume() function runs from a pm complete() callback, and thus
+        * is guaranteed to run after the touchscreen driver/ACPI-subsys has
+        * brought the touchscreen back up again (and thus changed the GPIO).
+        *
+        * So to work around this we pass GPIOD_FLAGS_BIT_NONEXCLUSIVE when
+        * requesting the GPIO and we set its value here to undo any changes
+        * done by the touchscreen's broken _PS0 ACPI method.
+        */
+       gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
+
        return 0;
 }
 
        {}
 };
 
+static const struct acpi_gpio_params first_gpio = { 0, 0, false };
+
+static const struct acpi_gpio_mapping byt_cht_es8316_gpios[] = {
+       { "speaker-enable-gpios", &first_gpio, 1 },
+       { },
+};
+
 static int snd_byt_cht_es8316_mc_probe(struct platform_device *pdev)
 {
        struct byt_cht_es8316_private *priv;
        struct device *dev = &pdev->dev;
        struct snd_soc_acpi_mach *mach;
        const char *i2c_name = NULL;
+       struct device *codec_dev;
        int dai_index = 0;
        int i;
        int ret = 0;
                return ret;
        }
 
+       /* get speaker enable GPIO */
+       codec_dev = bus_find_device_by_name(&i2c_bus_type, NULL, codec_name);
+       if (!codec_dev)
+               return -EPROBE_DEFER;
+
+       devm_acpi_dev_add_driver_gpios(codec_dev, byt_cht_es8316_gpios);
+       priv->speaker_en_gpio =
+               gpiod_get_index(codec_dev, "speaker-enable", 0,
+                               /* see comment in byt_cht_es8316_resume */
+                               GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
+       put_device(codec_dev);
+
+       if (IS_ERR(priv->speaker_en_gpio)) {
+               ret = PTR_ERR(priv->speaker_en_gpio);
+               switch (ret) {
+               case -ENOENT:
+                       priv->speaker_en_gpio = NULL;
+                       break;
+               default:
+                       dev_err(dev, "get speaker GPIO failed: %d\n", ret);
+                       /* fall through */
+               case -EPROBE_DEFER:
+                       return ret;
+               }
+       }
+
        /* register the soc card */
        byt_cht_es8316_card.dev = dev;
        snd_soc_card_set_drvdata(&byt_cht_es8316_card, priv);
 
        ret = devm_snd_soc_register_card(dev, &byt_cht_es8316_card);
        if (ret) {
+               gpiod_put(priv->speaker_en_gpio);
                dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
                return ret;
        }
        return 0;
 }
 
+static int snd_byt_cht_es8316_mc_remove(struct platform_device *pdev)
+{
+       struct byt_cht_es8316_private *priv = platform_get_drvdata(pdev);
+
+       gpiod_put(priv->speaker_en_gpio);
+       return 0;
+}
+
 static struct platform_driver snd_byt_cht_es8316_mc_driver = {
        .driver = {
                .name = "bytcht_es8316",
        },
        .probe = snd_byt_cht_es8316_mc_probe,
+       .remove = snd_byt_cht_es8316_mc_remove,
 };
 
 module_platform_driver(snd_byt_cht_es8316_mc_driver);