1 /* 2 * bytcr_rt5651.c - ASoc Machine driver for Intel Byt CR platform 3 * (derived from bytcr_rt5640.c) 4 * 5 * Copyright (C) 2015 Intel Corp 6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; version 2 of the License. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 18 */ 19 20 #include <linux/init.h> 21 #include <linux/i2c.h> 22 #include <linux/module.h> 23 #include <linux/platform_device.h> 24 #include <linux/property.h> 25 #include <linux/acpi.h> 26 #include <linux/clk.h> 27 #include <linux/device.h> 28 #include <linux/dmi.h> 29 #include <linux/input.h> 30 #include <linux/gpio/consumer.h> 31 #include <linux/gpio/machine.h> 32 #include <linux/slab.h> 33 #include <asm/cpu_device_id.h> 34 #include <asm/intel-family.h> 35 #include <sound/pcm.h> 36 #include <sound/pcm_params.h> 37 #include <sound/soc.h> 38 #include <sound/jack.h> 39 #include <sound/soc-acpi.h> 40 #include "../../codecs/rt5651.h" 41 #include "../atom/sst-atom-controls.h" 42 43 enum { 44 BYT_RT5651_DMIC_MAP, 45 BYT_RT5651_IN1_MAP, 46 BYT_RT5651_IN2_MAP, 47 BYT_RT5651_IN1_IN2_MAP, 48 }; 49 50 enum { 51 BYT_RT5651_JD_NULL = (RT5651_JD_NULL << 4), 52 BYT_RT5651_JD1_1 = (RT5651_JD1_1 << 4), 53 BYT_RT5651_JD1_2 = (RT5651_JD1_2 << 4), 54 BYT_RT5651_JD2 = (RT5651_JD2 << 4), 55 }; 56 57 enum { 58 BYT_RT5651_OVCD_TH_600UA = (6 << 8), 59 BYT_RT5651_OVCD_TH_1500UA = (15 << 8), 60 BYT_RT5651_OVCD_TH_2000UA = (20 << 8), 61 }; 62 63 enum { 64 BYT_RT5651_OVCD_SF_0P5 = (RT5651_OVCD_SF_0P5 << 13), 65 BYT_RT5651_OVCD_SF_0P75 = (RT5651_OVCD_SF_0P75 << 13), 66 BYT_RT5651_OVCD_SF_1P0 = (RT5651_OVCD_SF_1P0 << 13), 67 BYT_RT5651_OVCD_SF_1P5 = (RT5651_OVCD_SF_1P5 << 13), 68 }; 69 70 #define BYT_RT5651_MAP(quirk) ((quirk) & GENMASK(3, 0)) 71 #define BYT_RT5651_JDSRC(quirk) (((quirk) & GENMASK(7, 4)) >> 4) 72 #define BYT_RT5651_OVCD_TH(quirk) (((quirk) & GENMASK(12, 8)) >> 8) 73 #define BYT_RT5651_OVCD_SF(quirk) (((quirk) & GENMASK(14, 13)) >> 13) 74 #define BYT_RT5651_DMIC_EN BIT(16) 75 #define BYT_RT5651_MCLK_EN BIT(17) 76 #define BYT_RT5651_MCLK_25MHZ BIT(18) 77 #define BYT_RT5651_SSP2_AIF2 BIT(19) /* default is using AIF1 */ 78 #define BYT_RT5651_SSP0_AIF1 BIT(20) 79 #define BYT_RT5651_SSP0_AIF2 BIT(21) 80 #define BYT_RT5651_HP_LR_SWAPPED BIT(22) 81 #define BYT_RT5651_MONO_SPEAKER BIT(23) 82 83 #define BYT_RT5651_DEFAULT_QUIRKS (BYT_RT5651_MCLK_EN | \ 84 BYT_RT5651_JD1_1 | \ 85 BYT_RT5651_OVCD_TH_2000UA | \ 86 BYT_RT5651_OVCD_SF_0P75) 87 88 /* jack-detect-source + dmic-en + ovcd-th + -sf + terminating empty entry */ 89 #define MAX_NO_PROPS 5 90 91 struct byt_rt5651_private { 92 struct clk *mclk; 93 struct gpio_desc *ext_amp_gpio; 94 struct snd_soc_jack jack; 95 }; 96 97 /* Default: jack-detect on JD1_1, internal mic on in2, headsetmic on in3 */ 98 static unsigned long byt_rt5651_quirk = BYT_RT5651_DEFAULT_QUIRKS | 99 BYT_RT5651_IN2_MAP; 100 101 static void log_quirks(struct device *dev) 102 { 103 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_DMIC_MAP) 104 dev_info(dev, "quirk DMIC_MAP enabled"); 105 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_MAP) 106 dev_info(dev, "quirk IN1_MAP enabled"); 107 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN2_MAP) 108 dev_info(dev, "quirk IN2_MAP enabled"); 109 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_IN2_MAP) 110 dev_info(dev, "quirk IN1_IN2_MAP enabled"); 111 if (BYT_RT5651_JDSRC(byt_rt5651_quirk)) { 112 dev_info(dev, "quirk realtek,jack-detect-source %ld\n", 113 BYT_RT5651_JDSRC(byt_rt5651_quirk)); 114 dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n", 115 BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100); 116 dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n", 117 BYT_RT5651_OVCD_SF(byt_rt5651_quirk)); 118 } 119 if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN) 120 dev_info(dev, "quirk DMIC enabled"); 121 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) 122 dev_info(dev, "quirk MCLK_EN enabled"); 123 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ) 124 dev_info(dev, "quirk MCLK_25MHZ enabled"); 125 if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) 126 dev_info(dev, "quirk SSP2_AIF2 enabled\n"); 127 if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) 128 dev_info(dev, "quirk SSP0_AIF1 enabled\n"); 129 if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2) 130 dev_info(dev, "quirk SSP0_AIF2 enabled\n"); 131 if (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER) 132 dev_info(dev, "quirk MONO_SPEAKER enabled\n"); 133 } 134 135 #define BYT_CODEC_DAI1 "rt5651-aif1" 136 #define BYT_CODEC_DAI2 "rt5651-aif2" 137 138 static int byt_rt5651_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai, 139 int rate, int bclk_ratio) 140 { 141 int clk_id, clk_freq, ret; 142 143 /* Configure the PLL before selecting it */ 144 if (!(byt_rt5651_quirk & BYT_RT5651_MCLK_EN)) { 145 clk_id = RT5651_PLL1_S_BCLK1, 146 clk_freq = rate * bclk_ratio; 147 } else { 148 clk_id = RT5651_PLL1_S_MCLK; 149 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ) 150 clk_freq = 25000000; 151 else 152 clk_freq = 19200000; 153 } 154 ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, rate * 512); 155 if (ret < 0) { 156 dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret); 157 return ret; 158 } 159 160 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1, 161 rate * 512, SND_SOC_CLOCK_IN); 162 if (ret < 0) { 163 dev_err(codec_dai->component->dev, "can't set clock %d\n", ret); 164 return ret; 165 } 166 167 return 0; 168 } 169 170 static int platform_clock_control(struct snd_soc_dapm_widget *w, 171 struct snd_kcontrol *k, int event) 172 { 173 struct snd_soc_dapm_context *dapm = w->dapm; 174 struct snd_soc_card *card = dapm->card; 175 struct snd_soc_dai *codec_dai; 176 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card); 177 int ret; 178 179 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1); 180 if (!codec_dai) 181 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2); 182 if (!codec_dai) { 183 dev_err(card->dev, 184 "Codec dai not found; Unable to set platform clock\n"); 185 return -EIO; 186 } 187 188 if (SND_SOC_DAPM_EVENT_ON(event)) { 189 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) { 190 ret = clk_prepare_enable(priv->mclk); 191 if (ret < 0) { 192 dev_err(card->dev, 193 "could not configure MCLK state"); 194 return ret; 195 } 196 } 197 ret = byt_rt5651_prepare_and_enable_pll1(codec_dai, 48000, 50); 198 } else { 199 /* 200 * Set codec clock source to internal clock before 201 * turning off the platform clock. Codec needs clock 202 * for Jack detection and button press 203 */ 204 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_RCCLK, 205 48000 * 512, 206 SND_SOC_CLOCK_IN); 207 if (!ret) 208 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) 209 clk_disable_unprepare(priv->mclk); 210 } 211 212 if (ret < 0) { 213 dev_err(card->dev, "can't set codec sysclk: %d\n", ret); 214 return ret; 215 } 216 217 return 0; 218 } 219 220 static int rt5651_ext_amp_power_event(struct snd_soc_dapm_widget *w, 221 struct snd_kcontrol *kcontrol, int event) 222 { 223 struct snd_soc_card *card = w->dapm->card; 224 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card); 225 226 if (SND_SOC_DAPM_EVENT_ON(event)) 227 gpiod_set_value_cansleep(priv->ext_amp_gpio, 1); 228 else 229 gpiod_set_value_cansleep(priv->ext_amp_gpio, 0); 230 231 return 0; 232 } 233 234 static const struct snd_soc_dapm_widget byt_rt5651_widgets[] = { 235 SND_SOC_DAPM_HP("Headphone", NULL), 236 SND_SOC_DAPM_MIC("Headset Mic", NULL), 237 SND_SOC_DAPM_MIC("Internal Mic", NULL), 238 SND_SOC_DAPM_SPK("Speaker", NULL), 239 SND_SOC_DAPM_LINE("Line In", NULL), 240 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, 241 platform_clock_control, SND_SOC_DAPM_PRE_PMU | 242 SND_SOC_DAPM_POST_PMD), 243 SND_SOC_DAPM_SUPPLY("Ext Amp Power", SND_SOC_NOPM, 0, 0, 244 rt5651_ext_amp_power_event, 245 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU), 246 }; 247 248 static const struct snd_soc_dapm_route byt_rt5651_audio_map[] = { 249 {"Headphone", NULL, "Platform Clock"}, 250 {"Headset Mic", NULL, "Platform Clock"}, 251 {"Internal Mic", NULL, "Platform Clock"}, 252 {"Speaker", NULL, "Platform Clock"}, 253 {"Speaker", NULL, "Ext Amp Power"}, 254 {"Line In", NULL, "Platform Clock"}, 255 256 {"Headset Mic", NULL, "micbias1"}, /* lowercase for rt5651 */ 257 {"Headphone", NULL, "HPOL"}, 258 {"Headphone", NULL, "HPOR"}, 259 {"Speaker", NULL, "LOUTL"}, 260 {"Speaker", NULL, "LOUTR"}, 261 {"IN2P", NULL, "Line In"}, 262 {"IN2N", NULL, "Line In"}, 263 264 }; 265 266 static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic_map[] = { 267 {"DMIC L1", NULL, "Internal Mic"}, 268 {"DMIC R1", NULL, "Internal Mic"}, 269 {"IN3P", NULL, "Headset Mic"}, 270 }; 271 272 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_map[] = { 273 {"Internal Mic", NULL, "micbias1"}, 274 {"IN1P", NULL, "Internal Mic"}, 275 {"IN3P", NULL, "Headset Mic"}, 276 }; 277 278 static const struct snd_soc_dapm_route byt_rt5651_intmic_in2_map[] = { 279 {"Internal Mic", NULL, "micbias1"}, 280 {"IN2P", NULL, "Internal Mic"}, 281 {"IN3P", NULL, "Headset Mic"}, 282 }; 283 284 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_in2_map[] = { 285 {"Internal Mic", NULL, "micbias1"}, 286 {"IN1P", NULL, "Internal Mic"}, 287 {"IN2P", NULL, "Internal Mic"}, 288 {"IN3P", NULL, "Headset Mic"}, 289 }; 290 291 static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif1_map[] = { 292 {"ssp0 Tx", NULL, "modem_out"}, 293 {"modem_in", NULL, "ssp0 Rx"}, 294 295 {"AIF1 Playback", NULL, "ssp0 Tx"}, 296 {"ssp0 Rx", NULL, "AIF1 Capture"}, 297 }; 298 299 static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif2_map[] = { 300 {"ssp0 Tx", NULL, "modem_out"}, 301 {"modem_in", NULL, "ssp0 Rx"}, 302 303 {"AIF2 Playback", NULL, "ssp0 Tx"}, 304 {"ssp0 Rx", NULL, "AIF2 Capture"}, 305 }; 306 307 static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif1_map[] = { 308 {"ssp2 Tx", NULL, "codec_out0"}, 309 {"ssp2 Tx", NULL, "codec_out1"}, 310 {"codec_in0", NULL, "ssp2 Rx"}, 311 {"codec_in1", NULL, "ssp2 Rx"}, 312 313 {"AIF1 Playback", NULL, "ssp2 Tx"}, 314 {"ssp2 Rx", NULL, "AIF1 Capture"}, 315 }; 316 317 static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif2_map[] = { 318 {"ssp2 Tx", NULL, "codec_out0"}, 319 {"ssp2 Tx", NULL, "codec_out1"}, 320 {"codec_in0", NULL, "ssp2 Rx"}, 321 {"codec_in1", NULL, "ssp2 Rx"}, 322 323 {"AIF2 Playback", NULL, "ssp2 Tx"}, 324 {"ssp2 Rx", NULL, "AIF2 Capture"}, 325 }; 326 327 static const struct snd_kcontrol_new byt_rt5651_controls[] = { 328 SOC_DAPM_PIN_SWITCH("Headphone"), 329 SOC_DAPM_PIN_SWITCH("Headset Mic"), 330 SOC_DAPM_PIN_SWITCH("Internal Mic"), 331 SOC_DAPM_PIN_SWITCH("Speaker"), 332 SOC_DAPM_PIN_SWITCH("Line In"), 333 }; 334 335 static struct snd_soc_jack_pin bytcr_jack_pins[] = { 336 { 337 .pin = "Headphone", 338 .mask = SND_JACK_HEADPHONE, 339 }, 340 { 341 .pin = "Headset Mic", 342 .mask = SND_JACK_MICROPHONE, 343 }, 344 }; 345 346 static int byt_rt5651_aif1_hw_params(struct snd_pcm_substream *substream, 347 struct snd_pcm_hw_params *params) 348 { 349 struct snd_soc_pcm_runtime *rtd = substream->private_data; 350 struct snd_soc_dai *codec_dai = rtd->codec_dai; 351 snd_pcm_format_t format = params_format(params); 352 int rate = params_rate(params); 353 int bclk_ratio; 354 355 if (format == SNDRV_PCM_FORMAT_S16_LE) 356 bclk_ratio = 32; 357 else 358 bclk_ratio = 50; 359 360 return byt_rt5651_prepare_and_enable_pll1(codec_dai, rate, bclk_ratio); 361 } 362 363 static int byt_rt5651_quirk_cb(const struct dmi_system_id *id) 364 { 365 byt_rt5651_quirk = (unsigned long)id->driver_data; 366 return 1; 367 } 368 369 static const struct dmi_system_id byt_rt5651_quirk_table[] = { 370 { 371 /* Chuwi Hi8 Pro (CWI513) */ 372 .callback = byt_rt5651_quirk_cb, 373 .matches = { 374 DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"), 375 DMI_MATCH(DMI_PRODUCT_NAME, "X1D3_C806N"), 376 }, 377 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS | 378 BYT_RT5651_IN2_MAP | 379 BYT_RT5651_HP_LR_SWAPPED | 380 BYT_RT5651_MONO_SPEAKER), 381 }, 382 { 383 /* Chuwi Vi8 Plus (CWI519) */ 384 .callback = byt_rt5651_quirk_cb, 385 .matches = { 386 DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"), 387 DMI_MATCH(DMI_PRODUCT_NAME, "D2D3_Vi8A1"), 388 }, 389 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS | 390 BYT_RT5651_IN2_MAP | 391 BYT_RT5651_HP_LR_SWAPPED | 392 BYT_RT5651_MONO_SPEAKER), 393 }, 394 { 395 /* I.T.Works TW701, Ployer Momo7w and Trekstor ST70416-6 396 * (these all use the same mainboard) */ 397 .callback = byt_rt5651_quirk_cb, 398 .matches = { 399 DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."), 400 /* Partial match for all of itWORKS.G.WI71C.JGBMRBA, 401 * TREK.G.WI71C.JGBMRBA0x and MOMO.G.WI71C.MABMRBA02 */ 402 DMI_MATCH(DMI_BIOS_VERSION, ".G.WI71C."), 403 }, 404 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS | 405 BYT_RT5651_IN2_MAP | 406 BYT_RT5651_SSP0_AIF1 | 407 BYT_RT5651_MONO_SPEAKER), 408 }, 409 { 410 /* KIANO SlimNote 14.2 */ 411 .callback = byt_rt5651_quirk_cb, 412 .matches = { 413 DMI_MATCH(DMI_SYS_VENDOR, "KIANO"), 414 DMI_MATCH(DMI_PRODUCT_NAME, "KIANO SlimNote 14.2"), 415 }, 416 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS | 417 BYT_RT5651_IN1_IN2_MAP), 418 }, 419 { 420 /* Minnowboard Max B3 */ 421 .callback = byt_rt5651_quirk_cb, 422 .matches = { 423 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"), 424 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"), 425 }, 426 .driver_data = (void *)(BYT_RT5651_IN1_MAP), 427 }, 428 { 429 /* Minnowboard Turbot */ 430 .callback = byt_rt5651_quirk_cb, 431 .matches = { 432 DMI_MATCH(DMI_SYS_VENDOR, "ADI"), 433 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Turbot"), 434 }, 435 .driver_data = (void *)(BYT_RT5651_MCLK_EN | 436 BYT_RT5651_IN1_MAP), 437 }, 438 { 439 /* VIOS LTH17 */ 440 .callback = byt_rt5651_quirk_cb, 441 .matches = { 442 DMI_MATCH(DMI_SYS_VENDOR, "VIOS"), 443 DMI_MATCH(DMI_PRODUCT_NAME, "LTH17"), 444 }, 445 .driver_data = (void *)(BYT_RT5651_IN1_IN2_MAP | 446 BYT_RT5651_JD1_1 | 447 BYT_RT5651_OVCD_TH_2000UA | 448 BYT_RT5651_OVCD_SF_1P0 | 449 BYT_RT5651_MCLK_EN), 450 }, 451 { 452 /* Yours Y8W81 (and others using the same mainboard) */ 453 .callback = byt_rt5651_quirk_cb, 454 .matches = { 455 DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."), 456 /* Partial match for all devs with a W86C mainboard */ 457 DMI_MATCH(DMI_BIOS_VERSION, ".F.W86C."), 458 }, 459 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS | 460 BYT_RT5651_IN2_MAP | 461 BYT_RT5651_SSP0_AIF1 | 462 BYT_RT5651_MONO_SPEAKER), 463 }, 464 {} 465 }; 466 467 /* 468 * Note this MUST be called before snd_soc_register_card(), so that the props 469 * are in place before the codec component driver's probe function parses them. 470 */ 471 static int byt_rt5651_add_codec_device_props(struct device *i2c_dev) 472 { 473 struct property_entry props[MAX_NO_PROPS] = {}; 474 int cnt = 0; 475 476 props[cnt++] = PROPERTY_ENTRY_U32("realtek,jack-detect-source", 477 BYT_RT5651_JDSRC(byt_rt5651_quirk)); 478 479 props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-threshold-microamp", 480 BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100); 481 482 props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-scale-factor", 483 BYT_RT5651_OVCD_SF(byt_rt5651_quirk)); 484 485 if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN) 486 props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,dmic-en"); 487 488 return device_add_properties(i2c_dev, props); 489 } 490 491 static int byt_rt5651_init(struct snd_soc_pcm_runtime *runtime) 492 { 493 struct snd_soc_card *card = runtime->card; 494 struct snd_soc_component *codec = runtime->codec_dai->component; 495 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card); 496 const struct snd_soc_dapm_route *custom_map; 497 int num_routes; 498 int ret; 499 500 card->dapm.idle_bias_off = true; 501 502 /* Start with RC clk for jack-detect (we disable MCLK below) */ 503 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) 504 snd_soc_component_update_bits(codec, RT5651_GLB_CLK, 505 RT5651_SCLK_SRC_MASK, RT5651_SCLK_SRC_RCCLK); 506 507 switch (BYT_RT5651_MAP(byt_rt5651_quirk)) { 508 case BYT_RT5651_IN1_MAP: 509 custom_map = byt_rt5651_intmic_in1_map; 510 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_map); 511 break; 512 case BYT_RT5651_IN2_MAP: 513 custom_map = byt_rt5651_intmic_in2_map; 514 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in2_map); 515 break; 516 case BYT_RT5651_IN1_IN2_MAP: 517 custom_map = byt_rt5651_intmic_in1_in2_map; 518 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_in2_map); 519 break; 520 default: 521 custom_map = byt_rt5651_intmic_dmic_map; 522 num_routes = ARRAY_SIZE(byt_rt5651_intmic_dmic_map); 523 } 524 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes); 525 if (ret) 526 return ret; 527 528 if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) { 529 ret = snd_soc_dapm_add_routes(&card->dapm, 530 byt_rt5651_ssp2_aif2_map, 531 ARRAY_SIZE(byt_rt5651_ssp2_aif2_map)); 532 } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) { 533 ret = snd_soc_dapm_add_routes(&card->dapm, 534 byt_rt5651_ssp0_aif1_map, 535 ARRAY_SIZE(byt_rt5651_ssp0_aif1_map)); 536 } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2) { 537 ret = snd_soc_dapm_add_routes(&card->dapm, 538 byt_rt5651_ssp0_aif2_map, 539 ARRAY_SIZE(byt_rt5651_ssp0_aif2_map)); 540 } else { 541 ret = snd_soc_dapm_add_routes(&card->dapm, 542 byt_rt5651_ssp2_aif1_map, 543 ARRAY_SIZE(byt_rt5651_ssp2_aif1_map)); 544 } 545 if (ret) 546 return ret; 547 548 ret = snd_soc_add_card_controls(card, byt_rt5651_controls, 549 ARRAY_SIZE(byt_rt5651_controls)); 550 if (ret) { 551 dev_err(card->dev, "unable to add card controls\n"); 552 return ret; 553 } 554 snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone"); 555 snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker"); 556 557 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) { 558 /* 559 * The firmware might enable the clock at 560 * boot (this information may or may not 561 * be reflected in the enable clock register). 562 * To change the rate we must disable the clock 563 * first to cover these cases. Due to common 564 * clock framework restrictions that do not allow 565 * to disable a clock that has not been enabled, 566 * we need to enable the clock first. 567 */ 568 ret = clk_prepare_enable(priv->mclk); 569 if (!ret) 570 clk_disable_unprepare(priv->mclk); 571 572 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ) 573 ret = clk_set_rate(priv->mclk, 25000000); 574 else 575 ret = clk_set_rate(priv->mclk, 19200000); 576 577 if (ret) 578 dev_err(card->dev, "unable to set MCLK rate\n"); 579 } 580 581 if (BYT_RT5651_JDSRC(byt_rt5651_quirk)) { 582 ret = snd_soc_card_jack_new(runtime->card, "Headset", 583 SND_JACK_HEADSET | SND_JACK_BTN_0, 584 &priv->jack, bytcr_jack_pins, 585 ARRAY_SIZE(bytcr_jack_pins)); 586 if (ret) { 587 dev_err(runtime->dev, "jack creation failed %d\n", ret); 588 return ret; 589 } 590 591 snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0, 592 KEY_PLAYPAUSE); 593 594 ret = snd_soc_component_set_jack(codec, &priv->jack, NULL); 595 if (ret) 596 return ret; 597 } 598 599 return 0; 600 } 601 602 static const struct snd_soc_pcm_stream byt_rt5651_dai_params = { 603 .formats = SNDRV_PCM_FMTBIT_S24_LE, 604 .rate_min = 48000, 605 .rate_max = 48000, 606 .channels_min = 2, 607 .channels_max = 2, 608 }; 609 610 static int byt_rt5651_codec_fixup(struct snd_soc_pcm_runtime *rtd, 611 struct snd_pcm_hw_params *params) 612 { 613 struct snd_interval *rate = hw_param_interval(params, 614 SNDRV_PCM_HW_PARAM_RATE); 615 struct snd_interval *channels = hw_param_interval(params, 616 SNDRV_PCM_HW_PARAM_CHANNELS); 617 int ret, bits; 618 619 /* The DSP will covert the FE rate to 48k, stereo */ 620 rate->min = rate->max = 48000; 621 channels->min = channels->max = 2; 622 623 if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) || 624 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) { 625 /* set SSP0 to 16-bit */ 626 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE); 627 bits = 16; 628 } else { 629 /* set SSP2 to 24-bit */ 630 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE); 631 bits = 24; 632 } 633 634 /* 635 * Default mode for SSP configuration is TDM 4 slot, override config 636 * with explicit setting to I2S 2ch. The word length is set with 637 * dai_set_tdm_slot() since there is no other API exposed 638 */ 639 ret = snd_soc_dai_set_fmt(rtd->cpu_dai, 640 SND_SOC_DAIFMT_I2S | 641 SND_SOC_DAIFMT_NB_NF | 642 SND_SOC_DAIFMT_CBS_CFS 643 ); 644 645 if (ret < 0) { 646 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret); 647 return ret; 648 } 649 650 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, bits); 651 if (ret < 0) { 652 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret); 653 return ret; 654 } 655 656 return 0; 657 } 658 659 static const unsigned int rates_48000[] = { 660 48000, 661 }; 662 663 static const struct snd_pcm_hw_constraint_list constraints_48000 = { 664 .count = ARRAY_SIZE(rates_48000), 665 .list = rates_48000, 666 }; 667 668 static int byt_rt5651_aif1_startup(struct snd_pcm_substream *substream) 669 { 670 return snd_pcm_hw_constraint_list(substream->runtime, 0, 671 SNDRV_PCM_HW_PARAM_RATE, 672 &constraints_48000); 673 } 674 675 static const struct snd_soc_ops byt_rt5651_aif1_ops = { 676 .startup = byt_rt5651_aif1_startup, 677 }; 678 679 static const struct snd_soc_ops byt_rt5651_be_ssp2_ops = { 680 .hw_params = byt_rt5651_aif1_hw_params, 681 }; 682 683 static struct snd_soc_dai_link byt_rt5651_dais[] = { 684 [MERR_DPCM_AUDIO] = { 685 .name = "Audio Port", 686 .stream_name = "Audio", 687 .cpu_dai_name = "media-cpu-dai", 688 .codec_dai_name = "snd-soc-dummy-dai", 689 .codec_name = "snd-soc-dummy", 690 .platform_name = "sst-mfld-platform", 691 .nonatomic = true, 692 .dynamic = 1, 693 .dpcm_playback = 1, 694 .dpcm_capture = 1, 695 .ops = &byt_rt5651_aif1_ops, 696 }, 697 [MERR_DPCM_DEEP_BUFFER] = { 698 .name = "Deep-Buffer Audio Port", 699 .stream_name = "Deep-Buffer Audio", 700 .cpu_dai_name = "deepbuffer-cpu-dai", 701 .codec_dai_name = "snd-soc-dummy-dai", 702 .codec_name = "snd-soc-dummy", 703 .platform_name = "sst-mfld-platform", 704 .nonatomic = true, 705 .dynamic = 1, 706 .dpcm_playback = 1, 707 .ops = &byt_rt5651_aif1_ops, 708 }, 709 /* CODEC<->CODEC link */ 710 /* back ends */ 711 { 712 .name = "SSP2-Codec", 713 .id = 0, 714 .cpu_dai_name = "ssp2-port", 715 .platform_name = "sst-mfld-platform", 716 .no_pcm = 1, 717 .codec_dai_name = "rt5651-aif1", 718 .codec_name = "i2c-10EC5651:00", 719 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF 720 | SND_SOC_DAIFMT_CBS_CFS, 721 .be_hw_params_fixup = byt_rt5651_codec_fixup, 722 .ignore_suspend = 1, 723 .nonatomic = true, 724 .dpcm_playback = 1, 725 .dpcm_capture = 1, 726 .init = byt_rt5651_init, 727 .ops = &byt_rt5651_be_ssp2_ops, 728 }, 729 }; 730 731 /* SoC card */ 732 static char byt_rt5651_codec_name[SND_ACPI_I2C_ID_LEN]; 733 static char byt_rt5651_codec_aif_name[12]; /* = "rt5651-aif[1|2]" */ 734 static char byt_rt5651_cpu_dai_name[10]; /* = "ssp[0|2]-port" */ 735 static char byt_rt5651_long_name[50]; /* = "bytcr-rt5651-*-spk-*-mic[-swapped-hp]" */ 736 737 static int byt_rt5651_suspend(struct snd_soc_card *card) 738 { 739 struct snd_soc_component *component; 740 741 if (!BYT_RT5651_JDSRC(byt_rt5651_quirk)) 742 return 0; 743 744 for_each_card_components(card, component) { 745 if (!strcmp(component->name, byt_rt5651_codec_name)) { 746 dev_dbg(component->dev, "disabling jack detect before suspend\n"); 747 snd_soc_component_set_jack(component, NULL, NULL); 748 break; 749 } 750 } 751 752 return 0; 753 } 754 755 static int byt_rt5651_resume(struct snd_soc_card *card) 756 { 757 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card); 758 struct snd_soc_component *component; 759 760 if (!BYT_RT5651_JDSRC(byt_rt5651_quirk)) 761 return 0; 762 763 for_each_card_components(card, component) { 764 if (!strcmp(component->name, byt_rt5651_codec_name)) { 765 dev_dbg(component->dev, "re-enabling jack detect after resume\n"); 766 snd_soc_component_set_jack(component, &priv->jack, NULL); 767 break; 768 } 769 } 770 771 return 0; 772 } 773 774 static struct snd_soc_card byt_rt5651_card = { 775 .name = "bytcr-rt5651", 776 .owner = THIS_MODULE, 777 .dai_link = byt_rt5651_dais, 778 .num_links = ARRAY_SIZE(byt_rt5651_dais), 779 .dapm_widgets = byt_rt5651_widgets, 780 .num_dapm_widgets = ARRAY_SIZE(byt_rt5651_widgets), 781 .dapm_routes = byt_rt5651_audio_map, 782 .num_dapm_routes = ARRAY_SIZE(byt_rt5651_audio_map), 783 .fully_routed = true, 784 .suspend_pre = byt_rt5651_suspend, 785 .resume_post = byt_rt5651_resume, 786 }; 787 788 static const struct x86_cpu_id baytrail_cpu_ids[] = { 789 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT }, /* Valleyview */ 790 {} 791 }; 792 793 static const struct x86_cpu_id cherrytrail_cpu_ids[] = { 794 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT }, /* Braswell */ 795 {} 796 }; 797 798 static const struct acpi_gpio_params first_gpio = { 0, 0, false }; 799 static const struct acpi_gpio_params second_gpio = { 1, 0, false }; 800 801 static const struct acpi_gpio_mapping byt_rt5651_amp_en_first[] = { 802 { "ext-amp-enable-gpios", &first_gpio, 1 }, 803 { }, 804 }; 805 806 static const struct acpi_gpio_mapping byt_rt5651_amp_en_second[] = { 807 { "ext-amp-enable-gpios", &second_gpio, 1 }, 808 { }, 809 }; 810 811 /* 812 * Some boards have I2cSerialBusV2, GpioIo, GpioInt as ACPI resources, other 813 * boards may have I2cSerialBusV2, GpioInt, GpioIo instead. We want the 814 * GpioIo one for the ext-amp-enable-gpio and both count for the index in 815 * acpi_gpio_params index. So we have 2 different mappings and the code 816 * below figures out which one to use. 817 */ 818 struct byt_rt5651_acpi_resource_data { 819 int gpio_count; 820 int gpio_int_idx; 821 }; 822 823 static int snd_byt_rt5651_acpi_resource(struct acpi_resource *ares, void *arg) 824 { 825 struct byt_rt5651_acpi_resource_data *data = arg; 826 827 if (ares->type != ACPI_RESOURCE_TYPE_GPIO) 828 return 0; 829 830 if (ares->data.gpio.connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) 831 data->gpio_int_idx = data->gpio_count; 832 833 data->gpio_count++; 834 return 0; 835 } 836 837 static void snd_byt_rt5651_mc_add_amp_en_gpio_mapping(struct device *codec) 838 { 839 struct byt_rt5651_acpi_resource_data data = { 0, -1 }; 840 LIST_HEAD(resources); 841 int ret; 842 843 ret = acpi_dev_get_resources(ACPI_COMPANION(codec), &resources, 844 snd_byt_rt5651_acpi_resource, &data); 845 if (ret < 0) { 846 dev_warn(codec, "Failed to get ACPI resources, not adding external amplifier GPIO mapping\n"); 847 return; 848 } 849 850 /* All info we need is gathered during the walk */ 851 acpi_dev_free_resource_list(&resources); 852 853 switch (data.gpio_int_idx) { 854 case 0: 855 devm_acpi_dev_add_driver_gpios(codec, byt_rt5651_amp_en_second); 856 break; 857 case 1: 858 devm_acpi_dev_add_driver_gpios(codec, byt_rt5651_amp_en_first); 859 break; 860 default: 861 dev_warn(codec, "Unknown GpioInt index %d, not adding external amplifier GPIO mapping\n", 862 data.gpio_int_idx); 863 } 864 } 865 866 struct acpi_chan_package { /* ACPICA seems to require 64 bit integers */ 867 u64 aif_value; /* 1: AIF1, 2: AIF2 */ 868 u64 mclock_value; /* usually 25MHz (0x17d7940), ignored */ 869 }; 870 871 static int snd_byt_rt5651_mc_probe(struct platform_device *pdev) 872 { 873 const char * const mic_name[] = { "dmic", "in1", "in2", "in12" }; 874 struct byt_rt5651_private *priv; 875 struct snd_soc_acpi_mach *mach; 876 struct device *codec_dev; 877 const char *i2c_name = NULL; 878 const char *hp_swapped; 879 bool is_bytcr = false; 880 int ret_val = 0; 881 int dai_index = 0; 882 int i; 883 884 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 885 if (!priv) 886 return -ENOMEM; 887 888 /* register the soc card */ 889 byt_rt5651_card.dev = &pdev->dev; 890 891 mach = byt_rt5651_card.dev->platform_data; 892 snd_soc_card_set_drvdata(&byt_rt5651_card, priv); 893 894 /* fix index of codec dai */ 895 for (i = 0; i < ARRAY_SIZE(byt_rt5651_dais); i++) { 896 if (!strcmp(byt_rt5651_dais[i].codec_name, "i2c-10EC5651:00")) { 897 dai_index = i; 898 break; 899 } 900 } 901 902 /* fixup codec name based on HID */ 903 i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1); 904 if (!i2c_name) { 905 dev_err(&pdev->dev, "Error cannot find '%s' dev\n", mach->id); 906 return -ENODEV; 907 } 908 snprintf(byt_rt5651_codec_name, sizeof(byt_rt5651_codec_name), 909 "%s%s", "i2c-", i2c_name); 910 byt_rt5651_dais[dai_index].codec_name = byt_rt5651_codec_name; 911 912 codec_dev = bus_find_device_by_name(&i2c_bus_type, NULL, 913 byt_rt5651_codec_name); 914 if (!codec_dev) 915 return -EPROBE_DEFER; 916 917 /* 918 * swap SSP0 if bytcr is detected 919 * (will be overridden if DMI quirk is detected) 920 */ 921 if (x86_match_cpu(baytrail_cpu_ids)) { 922 if (mach->mach_params.acpi_ipc_irq_index == 0) 923 is_bytcr = true; 924 } 925 926 if (is_bytcr) { 927 /* 928 * Baytrail CR platforms may have CHAN package in BIOS, try 929 * to find relevant routing quirk based as done on Windows 930 * platforms. We have to read the information directly from the 931 * BIOS, at this stage the card is not created and the links 932 * with the codec driver/pdata are non-existent 933 */ 934 935 struct acpi_chan_package chan_package; 936 937 /* format specified: 2 64-bit integers */ 938 struct acpi_buffer format = {sizeof("NN"), "NN"}; 939 struct acpi_buffer state = {0, NULL}; 940 struct snd_soc_acpi_package_context pkg_ctx; 941 bool pkg_found = false; 942 943 state.length = sizeof(chan_package); 944 state.pointer = &chan_package; 945 946 pkg_ctx.name = "CHAN"; 947 pkg_ctx.length = 2; 948 pkg_ctx.format = &format; 949 pkg_ctx.state = &state; 950 pkg_ctx.data_valid = false; 951 952 pkg_found = snd_soc_acpi_find_package_from_hid(mach->id, 953 &pkg_ctx); 954 if (pkg_found) { 955 if (chan_package.aif_value == 1) { 956 dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n"); 957 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF1; 958 } else if (chan_package.aif_value == 2) { 959 dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n"); 960 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2; 961 } else { 962 dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n"); 963 pkg_found = false; 964 } 965 } 966 967 if (!pkg_found) { 968 /* no BIOS indications, assume SSP0-AIF2 connection */ 969 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2; 970 } 971 } 972 973 /* check quirks before creating card */ 974 dmi_check_system(byt_rt5651_quirk_table); 975 976 /* Must be called before register_card, also see declaration comment. */ 977 ret_val = byt_rt5651_add_codec_device_props(codec_dev); 978 if (ret_val) { 979 put_device(codec_dev); 980 return ret_val; 981 } 982 983 /* Cherry Trail devices use an external amplifier enable gpio */ 984 if (x86_match_cpu(cherrytrail_cpu_ids)) { 985 snd_byt_rt5651_mc_add_amp_en_gpio_mapping(codec_dev); 986 priv->ext_amp_gpio = devm_fwnode_get_index_gpiod_from_child( 987 &pdev->dev, "ext-amp-enable", 0, 988 codec_dev->fwnode, 989 GPIOD_OUT_LOW, "speaker-amp"); 990 if (IS_ERR(priv->ext_amp_gpio)) { 991 ret_val = PTR_ERR(priv->ext_amp_gpio); 992 switch (ret_val) { 993 case -ENOENT: 994 priv->ext_amp_gpio = NULL; 995 break; 996 default: 997 dev_err(&pdev->dev, "Failed to get ext-amp-enable GPIO: %d\n", 998 ret_val); 999 /* fall through */ 1000 case -EPROBE_DEFER: 1001 put_device(codec_dev); 1002 return ret_val; 1003 } 1004 } 1005 } 1006 1007 put_device(codec_dev); 1008 1009 log_quirks(&pdev->dev); 1010 1011 if ((byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) || 1012 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) { 1013 /* fixup codec aif name */ 1014 snprintf(byt_rt5651_codec_aif_name, 1015 sizeof(byt_rt5651_codec_aif_name), 1016 "%s", "rt5651-aif2"); 1017 1018 byt_rt5651_dais[dai_index].codec_dai_name = 1019 byt_rt5651_codec_aif_name; 1020 } 1021 1022 if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) || 1023 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) { 1024 /* fixup cpu dai name name */ 1025 snprintf(byt_rt5651_cpu_dai_name, 1026 sizeof(byt_rt5651_cpu_dai_name), 1027 "%s", "ssp0-port"); 1028 1029 byt_rt5651_dais[dai_index].cpu_dai_name = 1030 byt_rt5651_cpu_dai_name; 1031 } 1032 1033 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) { 1034 priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3"); 1035 if (IS_ERR(priv->mclk)) { 1036 ret_val = PTR_ERR(priv->mclk); 1037 dev_err(&pdev->dev, 1038 "Failed to get MCLK from pmc_plt_clk_3: %d\n", 1039 ret_val); 1040 /* 1041 * Fall back to bit clock usage for -ENOENT (clock not 1042 * available likely due to missing dependencies), bail 1043 * for all other errors, including -EPROBE_DEFER 1044 */ 1045 if (ret_val != -ENOENT) 1046 return ret_val; 1047 byt_rt5651_quirk &= ~BYT_RT5651_MCLK_EN; 1048 } 1049 } 1050 1051 if (byt_rt5651_quirk & BYT_RT5651_HP_LR_SWAPPED) 1052 hp_swapped = "-hp-swapped"; 1053 else 1054 hp_swapped = ""; 1055 1056 snprintf(byt_rt5651_long_name, sizeof(byt_rt5651_long_name), 1057 "bytcr-rt5651-%s-spk-%s-mic%s", 1058 (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER) ? 1059 "mono" : "stereo", 1060 mic_name[BYT_RT5651_MAP(byt_rt5651_quirk)], hp_swapped); 1061 byt_rt5651_card.long_name = byt_rt5651_long_name; 1062 1063 ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5651_card); 1064 1065 if (ret_val) { 1066 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n", 1067 ret_val); 1068 return ret_val; 1069 } 1070 platform_set_drvdata(pdev, &byt_rt5651_card); 1071 return ret_val; 1072 } 1073 1074 static struct platform_driver snd_byt_rt5651_mc_driver = { 1075 .driver = { 1076 .name = "bytcr_rt5651", 1077 }, 1078 .probe = snd_byt_rt5651_mc_probe, 1079 }; 1080 1081 module_platform_driver(snd_byt_rt5651_mc_driver); 1082 1083 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver for RT5651"); 1084 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>"); 1085 MODULE_LICENSE("GPL v2"); 1086 MODULE_ALIAS("platform:bytcr_rt5651"); 1087