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