2 * bytcht_es8316.c - ASoc Machine driver for Intel Baytrail/Cherrytrail
3 * platforms with Everest ES8316 SoC
5 * Copyright (C) 2017 Endless Mobile, Inc.
6 * Authors: David Yang <yangxiaohua@everest-semi.com>,
7 * Daniel Drake <drake@endlessm.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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"
42 /* jd-inv + terminating entry */
43 #define MAX_NO_PROPS 2
45 struct byt_cht_es8316_private {
47 struct snd_soc_jack jack;
48 struct gpio_desc *speaker_en_gpio;
53 BYT_CHT_ES8316_INTMIC_IN1_MAP,
54 BYT_CHT_ES8316_INTMIC_IN2_MAP,
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)
62 static unsigned long quirk;
64 static int quirk_override = -1;
65 module_param_named(quirk, quirk_override, int, 0444);
66 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
68 static void log_quirks(struct device *dev)
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");
82 static int byt_cht_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
83 struct snd_kcontrol *kcontrol, int event)
85 struct snd_soc_card *card = w->dapm->card;
86 struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
88 if (SND_SOC_DAPM_EVENT_ON(event))
89 priv->speaker_en = true;
91 priv->speaker_en = false;
93 gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
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),
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),
109 static const struct snd_soc_dapm_route byt_cht_es8316_audio_map[] = {
110 {"Headphone", NULL, "HPOL"},
111 {"Headphone", NULL, "HPOR"},
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.
117 {"Speaker", NULL, "HPOL"},
118 {"Speaker", NULL, "HPOR"},
119 {"Speaker", NULL, "Speaker Power"},
122 static const struct snd_soc_dapm_route byt_cht_es8316_intmic_in1_map[] = {
123 {"MIC1", NULL, "Internal Mic"},
124 {"MIC2", NULL, "Headset Mic"},
127 static const struct snd_soc_dapm_route byt_cht_es8316_intmic_in2_map[] = {
128 {"MIC2", NULL, "Internal Mic"},
129 {"MIC1", NULL, "Headset Mic"},
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"},
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"},
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"),
155 static struct snd_soc_jack_pin byt_cht_es8316_jack_pins[] = {
158 .mask = SND_JACK_HEADPHONE,
161 .pin = "Headset Mic",
162 .mask = SND_JACK_MICROPHONE,
166 static int byt_cht_es8316_init(struct snd_soc_pcm_runtime *runtime)
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;
175 card->dapm.idle_bias_off = true;
177 switch (BYT_CHT_ES8316_MAP(quirk)) {
178 case BYT_CHT_ES8316_INTMIC_IN1_MAP:
180 custom_map = byt_cht_es8316_intmic_in1_map;
181 num_routes = ARRAY_SIZE(byt_cht_es8316_intmic_in1_map);
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);
188 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
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);
196 custom_map = byt_cht_es8316_ssp2_map;
197 num_routes = ARRAY_SIZE(byt_cht_es8316_ssp2_map);
199 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
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
211 ret = clk_prepare_enable(priv->mclk);
213 clk_disable_unprepare(priv->mclk);
215 ret = clk_set_rate(priv->mclk, 19200000);
217 dev_err(card->dev, "unable to set MCLK rate\n");
219 ret = clk_prepare_enable(priv->mclk);
221 dev_err(card->dev, "unable to enable MCLK\n");
223 ret = snd_soc_dai_set_sysclk(runtime->codec_dai, 0, 19200000,
226 dev_err(card->dev, "can't set codec clock %d\n", ret);
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));
235 dev_err(card->dev, "jack creation failed %d\n", ret);
239 snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
240 snd_soc_component_set_jack(codec, &priv->jack, NULL);
245 static const struct snd_soc_pcm_stream byt_cht_es8316_dai_params = {
246 .formats = SNDRV_PCM_FMTBIT_S24_LE,
253 static int byt_cht_es8316_codec_fixup(struct snd_soc_pcm_runtime *rtd,
254 struct snd_pcm_hw_params *params)
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);
262 /* The DSP will covert the FE rate to 48k, stereo */
263 rate->min = rate->max = 48000;
264 channels->min = channels->max = 2;
266 if (quirk & BYT_CHT_ES8316_SSP0) {
267 /* set SSP0 to 16-bit */
268 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
271 /* set SSP2 to 24-bit */
272 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
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
281 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
283 SND_SOC_DAIFMT_NB_NF |
284 SND_SOC_DAIFMT_CBS_CFS
287 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
291 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, bits);
293 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
300 static int byt_cht_es8316_aif1_startup(struct snd_pcm_substream *substream)
302 return snd_pcm_hw_constraint_single(substream->runtime,
303 SNDRV_PCM_HW_PARAM_RATE, 48000);
306 static const struct snd_soc_ops byt_cht_es8316_aif1_ops = {
307 .startup = byt_cht_es8316_aif1_startup,
310 SND_SOC_DAILINK_DEF(dummy,
311 DAILINK_COMP_ARRAY(COMP_DUMMY()));
313 SND_SOC_DAILINK_DEF(media,
314 DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
316 SND_SOC_DAILINK_DEF(deepbuffer,
317 DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
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")));
324 SND_SOC_DAILINK_DEF(platform,
325 DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
327 static struct snd_soc_dai_link byt_cht_es8316_dais[] = {
328 [MERR_DPCM_AUDIO] = {
329 .name = "Audio Port",
330 .stream_name = "Audio",
335 .ops = &byt_cht_es8316_aif1_ops,
336 SND_SOC_DAILINK_REG(media, dummy, platform),
339 [MERR_DPCM_DEEP_BUFFER] = {
340 .name = "Deep-Buffer Audio Port",
341 .stream_name = "Deep-Buffer Audio",
345 .ops = &byt_cht_es8316_aif1_ops,
346 SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
351 /* Only SSP2 has been tested here, so BYT-CR platforms that
352 * require SSP0 will not work.
354 .name = "SSP2-Codec",
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,
363 .init = byt_cht_es8316_init,
364 SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
370 static char codec_name[SND_ACPI_I2C_ID_LEN];
371 static char long_name[50]; /* = "bytcht-es8316-*-spk-*-mic" */
373 static int byt_cht_es8316_suspend(struct snd_soc_card *card)
375 struct snd_soc_component *component;
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);
388 static int byt_cht_es8316_resume(struct snd_soc_card *card)
390 struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
391 struct snd_soc_component *component;
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);
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.
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).
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.
418 gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
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,
439 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
441 static const struct acpi_gpio_mapping byt_cht_es8316_gpios[] = {
442 { "speaker-enable-gpios", &first_gpio, 1 },
446 /* Please keep this list alphabetically sorted */
447 static const struct dmi_system_id byt_cht_es8316_quirk_table[] = {
448 { /* Teclast X98 Plus II */
450 DMI_MATCH(DMI_SYS_VENDOR, "TECLAST"),
451 DMI_MATCH(DMI_PRODUCT_NAME, "X98 Plus II"),
453 .driver_data = (void *)(BYT_CHT_ES8316_INTMIC_IN1_MAP
454 | BYT_CHT_ES8316_JD_INVERTED),
459 static int snd_byt_cht_es8316_mc_probe(struct platform_device *pdev)
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;
475 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
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")) {
489 /* fixup codec name based on HID */
490 adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
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;
498 /* override plaform name, if required */
499 byt_cht_es8316_card.dev = dev;
500 platform_name = mach->mach_params.platform;
502 ret = snd_soc_fixup_dai_links_platform_name(&byt_cht_es8316_card,
507 /* Check for BYTCR or other platform and setup quirks */
508 dmi_id = dmi_first_match(byt_cht_es8316_quirk_table);
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;
517 /* Others default to internal-mic-in1-map, mono-speaker */
518 quirk = BYT_CHT_ES8316_INTMIC_IN1_MAP |
519 BYT_CHT_ES8316_MONO_SPEAKER;
521 if (quirk_override != -1) {
522 dev_info(dev, "Overriding quirk 0x%x => 0x%x\n",
525 quirk = quirk_override;
529 if (quirk & BYT_CHT_ES8316_SSP0)
530 byt_cht_es8316_dais[dai_index].cpus->dai_name = "ssp0-port";
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);
540 /* get speaker enable GPIO */
541 codec_dev = bus_find_device_by_name(&i2c_bus_type, NULL, codec_name);
543 return -EPROBE_DEFER;
545 if (quirk & BYT_CHT_ES8316_JD_INVERTED)
546 props[cnt++] = PROPERTY_ENTRY_BOOL("everest,jack-detect-inverted");
549 ret = device_add_properties(codec_dev, props);
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);
561 if (IS_ERR(priv->speaker_en_gpio)) {
562 ret = PTR_ERR(priv->speaker_en_gpio);
565 priv->speaker_en_gpio = NULL;
568 dev_err(dev, "get speaker GPIO failed: %d\n", ret);
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);
582 ret = devm_snd_soc_register_card(dev, &byt_cht_es8316_card);
584 gpiod_put(priv->speaker_en_gpio);
585 dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
588 platform_set_drvdata(pdev, &byt_cht_es8316_card);
592 static int snd_byt_cht_es8316_mc_remove(struct platform_device *pdev)
594 struct snd_soc_card *card = platform_get_drvdata(pdev);
595 struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
597 gpiod_put(priv->speaker_en_gpio);
601 static struct platform_driver snd_byt_cht_es8316_mc_driver = {
603 .name = "bytcht_es8316",
605 .probe = snd_byt_cht_es8316_mc_probe,
606 .remove = snd_byt_cht_es8316_mc_remove,
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");