Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
   4 *
   5 *  Copyright (C) 2014 Intel Corp
   6 *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
   7 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   8 *
   9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10 */
  11
  12#include <linux/i2c.h>
  13#include <linux/init.h>
  14#include <linux/module.h>
  15#include <linux/moduleparam.h>
  16#include <linux/platform_device.h>
  17#include <linux/acpi.h>
  18#include <linux/clk.h>
  19#include <linux/device.h>
  20#include <linux/device/bus.h>
  21#include <linux/dmi.h>
  22#include <linux/gpio/consumer.h>
  23#include <linux/gpio/machine.h>
  24#include <linux/input.h>
  25#include <linux/slab.h>
  26#include <sound/pcm.h>
  27#include <sound/pcm_params.h>
  28#include <sound/soc.h>
  29#include <sound/jack.h>
  30#include <sound/soc-acpi.h>
  31#include <dt-bindings/sound/rt5640.h>
  32#include "../../codecs/rt5640.h"
  33#include "../atom/sst-atom-controls.h"
  34#include "../common/soc-intel-quirks.h"
  35
  36#define BYT_RT5640_FALLBACK_CODEC_DEV_NAME	"i2c-rt5640"
  37
  38enum {
  39	BYT_RT5640_DMIC1_MAP,
  40	BYT_RT5640_DMIC2_MAP,
  41	BYT_RT5640_IN1_MAP,
  42	BYT_RT5640_IN3_MAP,
  43	BYT_RT5640_NO_INTERNAL_MIC_MAP,
  44};
  45
  46#define RT5640_JD_SRC_EXT_GPIO			0x0f
  47
  48enum {
  49	BYT_RT5640_JD_SRC_GPIO1		= (RT5640_JD_SRC_GPIO1 << 4),
  50	BYT_RT5640_JD_SRC_JD1_IN4P	= (RT5640_JD_SRC_JD1_IN4P << 4),
  51	BYT_RT5640_JD_SRC_JD2_IN4N	= (RT5640_JD_SRC_JD2_IN4N << 4),
  52	BYT_RT5640_JD_SRC_GPIO2		= (RT5640_JD_SRC_GPIO2 << 4),
  53	BYT_RT5640_JD_SRC_GPIO3		= (RT5640_JD_SRC_GPIO3 << 4),
  54	BYT_RT5640_JD_SRC_GPIO4		= (RT5640_JD_SRC_GPIO4 << 4),
  55	BYT_RT5640_JD_SRC_EXT_GPIO	= (RT5640_JD_SRC_EXT_GPIO << 4)
  56};
  57
  58enum {
  59	BYT_RT5640_OVCD_TH_600UA	= (6 << 8),
  60	BYT_RT5640_OVCD_TH_1500UA	= (15 << 8),
  61	BYT_RT5640_OVCD_TH_2000UA	= (20 << 8),
  62};
  63
  64enum {
  65	BYT_RT5640_OVCD_SF_0P5		= (RT5640_OVCD_SF_0P5 << 13),
  66	BYT_RT5640_OVCD_SF_0P75		= (RT5640_OVCD_SF_0P75 << 13),
  67	BYT_RT5640_OVCD_SF_1P0		= (RT5640_OVCD_SF_1P0 << 13),
  68	BYT_RT5640_OVCD_SF_1P5		= (RT5640_OVCD_SF_1P5 << 13),
  69};
  70
  71#define BYT_RT5640_MAP(quirk)		((quirk) &  GENMASK(3, 0))
  72#define BYT_RT5640_JDSRC(quirk)		(((quirk) & GENMASK(7, 4)) >> 4)
  73#define BYT_RT5640_OVCD_TH(quirk)	(((quirk) & GENMASK(12, 8)) >> 8)
  74#define BYT_RT5640_OVCD_SF(quirk)	(((quirk) & GENMASK(14, 13)) >> 13)
  75#define BYT_RT5640_JD_NOT_INV		BIT(16)
  76#define BYT_RT5640_MONO_SPEAKER		BIT(17)
  77#define BYT_RT5640_DIFF_MIC		BIT(18) /* default is single-ended */
  78#define BYT_RT5640_SSP2_AIF2		BIT(19) /* default is using AIF1  */
  79#define BYT_RT5640_SSP0_AIF1		BIT(20)
  80#define BYT_RT5640_SSP0_AIF2		BIT(21)
  81#define BYT_RT5640_MCLK_EN		BIT(22)
  82#define BYT_RT5640_MCLK_25MHZ		BIT(23)
  83#define BYT_RT5640_NO_SPEAKERS		BIT(24)
  84#define BYT_RT5640_LINEOUT		BIT(25)
  85#define BYT_RT5640_LINEOUT_AS_HP2	BIT(26)
  86#define BYT_RT5640_HSMIC2_ON_IN1	BIT(27)
  87#define BYT_RT5640_JD_HP_ELITEP_1000G2	BIT(28)
  88#define BYT_RT5640_USE_AMCR0F28		BIT(29)
  89#define BYT_RT5640_SWAPPED_SPEAKERS	BIT(30)
  90
  91#define BYTCR_INPUT_DEFAULTS				\
  92	(BYT_RT5640_IN3_MAP |				\
  93	 BYT_RT5640_JD_SRC_JD1_IN4P |			\
  94	 BYT_RT5640_OVCD_TH_2000UA |			\
  95	 BYT_RT5640_OVCD_SF_0P75 |			\
  96	 BYT_RT5640_DIFF_MIC)
  97
  98/* in-diff or dmic-pin + jdsrc + ovcd-th + -sf + jd-inv + terminating entry */
  99#define MAX_NO_PROPS 6
 100
 101struct byt_rt5640_private {
 102	struct snd_soc_jack jack;
 103	struct snd_soc_jack jack2;
 104	struct rt5640_set_jack_data jack_data;
 105	struct gpio_desc *hsmic_detect;
 106	struct clk *mclk;
 107	struct device *codec_dev;
 108};
 109static bool is_bytcr;
 110
 111static unsigned long byt_rt5640_quirk = BYT_RT5640_MCLK_EN;
 112static int quirk_override = -1;
 113module_param_named(quirk, quirk_override, int, 0444);
 114MODULE_PARM_DESC(quirk, "Board-specific quirk override");
 115
 116static void log_quirks(struct device *dev)
 117{
 118	int map;
 119	bool has_mclk = false;
 120	bool has_ssp0 = false;
 121	bool has_ssp0_aif1 = false;
 122	bool has_ssp0_aif2 = false;
 123	bool has_ssp2_aif2 = false;
 124
 125	map = BYT_RT5640_MAP(byt_rt5640_quirk);
 126	switch (map) {
 127	case BYT_RT5640_DMIC1_MAP:
 128		dev_info(dev, "quirk DMIC1_MAP enabled\n");
 129		break;
 130	case BYT_RT5640_DMIC2_MAP:
 131		dev_info(dev, "quirk DMIC2_MAP enabled\n");
 132		break;
 133	case BYT_RT5640_IN1_MAP:
 134		dev_info(dev, "quirk IN1_MAP enabled\n");
 135		break;
 136	case BYT_RT5640_IN3_MAP:
 137		dev_info(dev, "quirk IN3_MAP enabled\n");
 138		break;
 139	case BYT_RT5640_NO_INTERNAL_MIC_MAP:
 140		dev_info(dev, "quirk NO_INTERNAL_MIC_MAP enabled\n");
 141		break;
 142	default:
 143		dev_err(dev, "quirk map 0x%x is not supported, microphone input will not work\n", map);
 144		break;
 145	}
 146	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1)
 147		dev_info(dev, "quirk HSMIC2_ON_IN1 enabled\n");
 148	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
 149		dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
 150			 BYT_RT5640_JDSRC(byt_rt5640_quirk));
 151		dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
 152			 BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
 153		dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
 154			 BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
 155	}
 156	if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
 157		dev_info(dev, "quirk JD_NOT_INV enabled\n");
 158	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
 159		dev_info(dev, "quirk JD_HP_ELITEPAD_1000G2 enabled\n");
 160	if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER)
 161		dev_info(dev, "quirk MONO_SPEAKER enabled\n");
 162	if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)
 163		dev_info(dev, "quirk NO_SPEAKERS enabled\n");
 164	if (byt_rt5640_quirk & BYT_RT5640_SWAPPED_SPEAKERS)
 165		dev_info(dev, "quirk SWAPPED_SPEAKERS enabled\n");
 166	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT)
 167		dev_info(dev, "quirk LINEOUT enabled\n");
 168	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2)
 169		dev_info(dev, "quirk LINEOUT_AS_HP2 enabled\n");
 170	if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
 171		dev_info(dev, "quirk DIFF_MIC enabled\n");
 172	if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
 173		dev_info(dev, "quirk SSP0_AIF1 enabled\n");
 174		has_ssp0 = true;
 175		has_ssp0_aif1 = true;
 176	}
 177	if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
 178		dev_info(dev, "quirk SSP0_AIF2 enabled\n");
 179		has_ssp0 = true;
 180		has_ssp0_aif2 = true;
 181	}
 182	if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
 183		dev_info(dev, "quirk SSP2_AIF2 enabled\n");
 184		has_ssp2_aif2 = true;
 185	}
 186	if (is_bytcr && !has_ssp0)
 187		dev_err(dev, "Invalid routing, bytcr detected but no SSP0-based quirk, audio cannot work with SSP2 on bytcr\n");
 188	if (has_ssp0_aif1 && has_ssp0_aif2)
 189		dev_err(dev, "Invalid routing, SSP0 cannot be connected to both AIF1 and AIF2\n");
 190	if (has_ssp0 && has_ssp2_aif2)
 191		dev_err(dev, "Invalid routing, cannot have both SSP0 and SSP2 connected to codec\n");
 192
 193	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
 194		dev_info(dev, "quirk MCLK_EN enabled\n");
 195		has_mclk = true;
 196	}
 197	if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
 198		if (has_mclk)
 199			dev_info(dev, "quirk MCLK_25MHZ enabled\n");
 200		else
 201			dev_err(dev, "quirk MCLK_25MHZ enabled but quirk MCLK not selected, will be ignored\n");
 202	}
 203}
 204
 205static int byt_rt5640_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
 206					      int rate)
 207{
 208	int ret;
 209
 210	/* Configure the PLL before selecting it */
 211	if (!(byt_rt5640_quirk & BYT_RT5640_MCLK_EN)) {
 212		/* use bitclock as PLL input */
 213		if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
 214		    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
 215			/* 2x16 bit slots on SSP0 */
 216			ret = snd_soc_dai_set_pll(codec_dai, 0,
 217						  RT5640_PLL1_S_BCLK1,
 218						  rate * 32, rate * 512);
 219		} else {
 220			/* 2x15 bit slots on SSP2 */
 221			ret = snd_soc_dai_set_pll(codec_dai, 0,
 222						  RT5640_PLL1_S_BCLK1,
 223						  rate * 50, rate * 512);
 224		}
 225	} else {
 226		if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
 227			ret = snd_soc_dai_set_pll(codec_dai, 0,
 228						  RT5640_PLL1_S_MCLK,
 229						  25000000, rate * 512);
 230		} else {
 231			ret = snd_soc_dai_set_pll(codec_dai, 0,
 232						  RT5640_PLL1_S_MCLK,
 233						  19200000, rate * 512);
 234		}
 235	}
 236
 237	if (ret < 0) {
 238		dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
 239		return ret;
 240	}
 241
 242	ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
 243				     rate * 512, SND_SOC_CLOCK_IN);
 244	if (ret < 0) {
 245		dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
 246		return ret;
 247	}
 248
 249	return 0;
 250}
 251
 252#define BYT_CODEC_DAI1	"rt5640-aif1"
 253#define BYT_CODEC_DAI2	"rt5640-aif2"
 254
 255static struct snd_soc_dai *byt_rt5640_get_codec_dai(struct snd_soc_dapm_context *dapm)
 256{
 257	struct snd_soc_card *card = dapm->card;
 258	struct snd_soc_dai *codec_dai;
 259
 260	codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
 261	if (!codec_dai)
 262		codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
 263	if (!codec_dai)
 264		dev_err(card->dev, "Error codec dai not found\n");
 265
 266	return codec_dai;
 267}
 268
 269static int platform_clock_control(struct snd_soc_dapm_widget *w,
 270				  struct snd_kcontrol *k, int  event)
 271{
 272	struct snd_soc_dapm_context *dapm = w->dapm;
 273	struct snd_soc_card *card = dapm->card;
 274	struct snd_soc_dai *codec_dai;
 275	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
 276	int ret;
 277
 278	codec_dai = byt_rt5640_get_codec_dai(dapm);
 279	if (!codec_dai)
 280		return -EIO;
 281
 282	if (SND_SOC_DAPM_EVENT_ON(event)) {
 283		ret = clk_prepare_enable(priv->mclk);
 284		if (ret < 0) {
 285			dev_err(card->dev, "could not configure MCLK state\n");
 286			return ret;
 287		}
 288		ret = byt_rt5640_prepare_and_enable_pll1(codec_dai, 48000);
 289	} else {
 290		/*
 291		 * Set codec clock source to internal clock before
 292		 * turning off the platform clock. Codec needs clock
 293		 * for Jack detection and button press
 294		 */
 295		ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_RCCLK,
 296					     48000 * 512,
 297					     SND_SOC_CLOCK_IN);
 298		if (!ret)
 299			clk_disable_unprepare(priv->mclk);
 300	}
 301
 302	if (ret < 0) {
 303		dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
 304		return ret;
 305	}
 306
 307	return 0;
 308}
 309
 310static int byt_rt5640_event_lineout(struct snd_soc_dapm_widget *w,
 311			struct snd_kcontrol *k, int event)
 312{
 313	unsigned int gpio_ctrl3_val = RT5640_GP1_PF_OUT;
 314	struct snd_soc_dai *codec_dai;
 315
 316	if (!(byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2))
 317		return 0;
 318
 319	/*
 320	 * On devices which use line-out as a second headphones output,
 321	 * the codec's GPIO1 pin is used to enable an external HP-amp.
 322	 */
 323
 324	codec_dai = byt_rt5640_get_codec_dai(w->dapm);
 325	if (!codec_dai)
 326		return -EIO;
 327
 328	if (SND_SOC_DAPM_EVENT_ON(event))
 329		gpio_ctrl3_val |= RT5640_GP1_OUT_HI;
 330
 331	snd_soc_component_update_bits(codec_dai->component, RT5640_GPIO_CTRL3,
 332		RT5640_GP1_PF_MASK | RT5640_GP1_OUT_MASK, gpio_ctrl3_val);
 333
 334	return 0;
 335}
 336
 337static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
 338	SND_SOC_DAPM_HP("Headphone", NULL),
 339	SND_SOC_DAPM_MIC("Headset Mic", NULL),
 340	SND_SOC_DAPM_MIC("Headset Mic 2", NULL),
 341	SND_SOC_DAPM_MIC("Internal Mic", NULL),
 342	SND_SOC_DAPM_SPK("Speaker", NULL),
 343	SND_SOC_DAPM_LINE("Line Out", byt_rt5640_event_lineout),
 344	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
 345			    platform_clock_control, SND_SOC_DAPM_PRE_PMU |
 346			    SND_SOC_DAPM_POST_PMD),
 347};
 348
 349static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
 350	{"Headphone", NULL, "Platform Clock"},
 351	{"Headset Mic", NULL, "Platform Clock"},
 352	{"Headset Mic", NULL, "MICBIAS1"},
 353	{"IN2P", NULL, "Headset Mic"},
 354	{"Headphone", NULL, "HPOL"},
 355	{"Headphone", NULL, "HPOR"},
 356};
 357
 358static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
 359	{"Internal Mic", NULL, "Platform Clock"},
 360	{"DMIC1", NULL, "Internal Mic"},
 361};
 362
 363static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
 364	{"Internal Mic", NULL, "Platform Clock"},
 365	{"DMIC2", NULL, "Internal Mic"},
 366};
 367
 368static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
 369	{"Internal Mic", NULL, "Platform Clock"},
 370	{"Internal Mic", NULL, "MICBIAS1"},
 371	{"IN1P", NULL, "Internal Mic"},
 372};
 373
 374static const struct snd_soc_dapm_route byt_rt5640_intmic_in3_map[] = {
 375	{"Internal Mic", NULL, "Platform Clock"},
 376	{"Internal Mic", NULL, "MICBIAS1"},
 377	{"IN3P", NULL, "Internal Mic"},
 378};
 379
 380static const struct snd_soc_dapm_route byt_rt5640_hsmic2_in1_map[] = {
 381	{"Headset Mic 2", NULL, "Platform Clock"},
 382	{"Headset Mic 2", NULL, "MICBIAS1"},
 383	{"IN1P", NULL, "Headset Mic 2"},
 384};
 385
 386static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif1_map[] = {
 387	{"ssp2 Tx", NULL, "codec_out0"},
 388	{"ssp2 Tx", NULL, "codec_out1"},
 389	{"codec_in0", NULL, "ssp2 Rx"},
 390	{"codec_in1", NULL, "ssp2 Rx"},
 391
 392	{"AIF1 Playback", NULL, "ssp2 Tx"},
 393	{"ssp2 Rx", NULL, "AIF1 Capture"},
 394};
 395
 396static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif2_map[] = {
 397	{"ssp2 Tx", NULL, "codec_out0"},
 398	{"ssp2 Tx", NULL, "codec_out1"},
 399	{"codec_in0", NULL, "ssp2 Rx"},
 400	{"codec_in1", NULL, "ssp2 Rx"},
 401
 402	{"AIF2 Playback", NULL, "ssp2 Tx"},
 403	{"ssp2 Rx", NULL, "AIF2 Capture"},
 404};
 405
 406static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif1_map[] = {
 407	{"ssp0 Tx", NULL, "modem_out"},
 408	{"modem_in", NULL, "ssp0 Rx"},
 409
 410	{"AIF1 Playback", NULL, "ssp0 Tx"},
 411	{"ssp0 Rx", NULL, "AIF1 Capture"},
 412};
 413
 414static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif2_map[] = {
 415	{"ssp0 Tx", NULL, "modem_out"},
 416	{"modem_in", NULL, "ssp0 Rx"},
 417
 418	{"AIF2 Playback", NULL, "ssp0 Tx"},
 419	{"ssp0 Rx", NULL, "AIF2 Capture"},
 420};
 421
 422static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = {
 423	{"Speaker", NULL, "Platform Clock"},
 424	{"Speaker", NULL, "SPOLP"},
 425	{"Speaker", NULL, "SPOLN"},
 426	{"Speaker", NULL, "SPORP"},
 427	{"Speaker", NULL, "SPORN"},
 428};
 429
 430static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = {
 431	{"Speaker", NULL, "Platform Clock"},
 432	{"Speaker", NULL, "SPOLP"},
 433	{"Speaker", NULL, "SPOLN"},
 434};
 435
 436static const struct snd_soc_dapm_route byt_rt5640_lineout_map[] = {
 437	{"Line Out", NULL, "Platform Clock"},
 438	{"Line Out", NULL, "LOUTR"},
 439	{"Line Out", NULL, "LOUTL"},
 440};
 441
 442static const struct snd_kcontrol_new byt_rt5640_controls[] = {
 443	SOC_DAPM_PIN_SWITCH("Headphone"),
 444	SOC_DAPM_PIN_SWITCH("Headset Mic"),
 445	SOC_DAPM_PIN_SWITCH("Headset Mic 2"),
 446	SOC_DAPM_PIN_SWITCH("Internal Mic"),
 447	SOC_DAPM_PIN_SWITCH("Speaker"),
 448	SOC_DAPM_PIN_SWITCH("Line Out"),
 449};
 450
 451static struct snd_soc_jack_pin rt5640_pins[] = {
 452	{
 453		.pin	= "Headphone",
 454		.mask	= SND_JACK_HEADPHONE,
 455	},
 456	{
 457		.pin	= "Headset Mic",
 458		.mask	= SND_JACK_MICROPHONE,
 459	},
 460};
 461
 462static struct snd_soc_jack_pin rt5640_pins2[] = {
 463	{
 464		/* The 2nd headset jack uses lineout with an external HP-amp */
 465		.pin	= "Line Out",
 466		.mask	= SND_JACK_HEADPHONE,
 467	},
 468	{
 469		.pin	= "Headset Mic 2",
 470		.mask	= SND_JACK_MICROPHONE,
 471	},
 472};
 473
 474static struct snd_soc_jack_gpio rt5640_jack_gpio = {
 475	.name = "hp-detect",
 476	.report = SND_JACK_HEADSET,
 477	.invert = true,
 478	.debounce_time = 200,
 479};
 480
 481static struct snd_soc_jack_gpio rt5640_jack2_gpio = {
 482	.name = "hp2-detect",
 483	.report = SND_JACK_HEADSET,
 484	.invert = true,
 485	.debounce_time = 200,
 486};
 487
 488static const struct acpi_gpio_params acpi_gpio0 = { 0, 0, false };
 489static const struct acpi_gpio_params acpi_gpio1 = { 1, 0, false };
 490static const struct acpi_gpio_params acpi_gpio2 = { 2, 0, false };
 491
 492static const struct acpi_gpio_mapping byt_rt5640_hp_elitepad_1000g2_gpios[] = {
 493	{ "hp-detect-gpios", &acpi_gpio0, 1, },
 494	{ "headset-mic-detect-gpios", &acpi_gpio1, 1, },
 495	{ "hp2-detect-gpios", &acpi_gpio2, 1, },
 496	{ },
 497};
 498
 499static int byt_rt5640_hp_elitepad_1000g2_jack1_check(void *data)
 500{
 501	struct byt_rt5640_private *priv = data;
 502	int jack_status, mic_status;
 503
 504	jack_status = gpiod_get_value_cansleep(rt5640_jack_gpio.desc);
 505	if (jack_status)
 506		return 0;
 507
 508	mic_status = gpiod_get_value_cansleep(priv->hsmic_detect);
 509	if (mic_status)
 510		return SND_JACK_HEADPHONE;
 511	else
 512		return SND_JACK_HEADSET;
 513}
 514
 515static int byt_rt5640_hp_elitepad_1000g2_jack2_check(void *data)
 516{
 517	struct snd_soc_component *component = data;
 518	int jack_status, report;
 519
 520	jack_status = gpiod_get_value_cansleep(rt5640_jack2_gpio.desc);
 521	if (jack_status)
 522		return 0;
 523
 524	rt5640_enable_micbias1_for_ovcd(component);
 525	report = rt5640_detect_headset(component, rt5640_jack2_gpio.desc);
 526	rt5640_disable_micbias1_for_ovcd(component);
 527
 528	return report;
 529}
 530
 531static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
 532					struct snd_pcm_hw_params *params)
 533{
 534	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 535	struct snd_soc_dai *dai = snd_soc_rtd_to_codec(rtd, 0);
 536
 537	return byt_rt5640_prepare_and_enable_pll1(dai, params_rate(params));
 538}
 539
 540/* Please keep this list alphabetically sorted */
 541static const struct dmi_system_id byt_rt5640_quirk_table[] = {
 542	{	/* Acer Iconia One 7 B1-750 */
 543		.matches = {
 544			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
 545			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "VESPA2"),
 546		},
 547		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 548					BYT_RT5640_JD_SRC_JD1_IN4P |
 549					BYT_RT5640_OVCD_TH_1500UA |
 550					BYT_RT5640_OVCD_SF_0P75 |
 551					BYT_RT5640_SSP0_AIF1 |
 552					BYT_RT5640_MCLK_EN),
 553	},
 554	{	/* Acer Iconia Tab 8 W1-810 */
 555		.matches = {
 556			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Acer"),
 557			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Iconia W1-810"),
 558		},
 559		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 560					BYT_RT5640_JD_SRC_JD1_IN4P |
 561					BYT_RT5640_OVCD_TH_1500UA |
 562					BYT_RT5640_OVCD_SF_0P75 |
 563					BYT_RT5640_SSP0_AIF1 |
 564					BYT_RT5640_MCLK_EN),
 565	},
 566	{	/* Acer One 10 S1002 */
 567		.matches = {
 568			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
 569			DMI_MATCH(DMI_PRODUCT_NAME, "One S1002"),
 570		},
 571		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 572					BYT_RT5640_JD_SRC_JD2_IN4N |
 573					BYT_RT5640_OVCD_TH_2000UA |
 574					BYT_RT5640_OVCD_SF_0P75 |
 575					BYT_RT5640_DIFF_MIC |
 576					BYT_RT5640_SSP0_AIF2 |
 577					BYT_RT5640_MCLK_EN),
 578	},
 579	{
 580		.matches = {
 581			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
 582			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
 583		},
 584		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 585					BYT_RT5640_JD_SRC_JD2_IN4N |
 586					BYT_RT5640_OVCD_TH_2000UA |
 587					BYT_RT5640_OVCD_SF_0P75 |
 588					BYT_RT5640_SSP0_AIF1 |
 589					BYT_RT5640_MCLK_EN),
 590	},
 591	{
 592		/* Advantech MICA-071 */
 593		.matches = {
 594			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Advantech"),
 595			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MICA-071"),
 596		},
 597		/* OVCD Th = 1500uA to reliable detect head-phones vs -set */
 598		.driver_data = (void *)(BYT_RT5640_IN3_MAP |
 599					BYT_RT5640_JD_SRC_JD2_IN4N |
 600					BYT_RT5640_OVCD_TH_1500UA |
 601					BYT_RT5640_OVCD_SF_0P75 |
 602					BYT_RT5640_MONO_SPEAKER |
 603					BYT_RT5640_DIFF_MIC |
 604					BYT_RT5640_MCLK_EN),
 605	},
 606	{
 607		.matches = {
 608			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
 609			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 80 Cesium"),
 610		},
 611		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 612					BYT_RT5640_MONO_SPEAKER |
 613					BYT_RT5640_SSP0_AIF1 |
 614					BYT_RT5640_MCLK_EN),
 615	},
 616	{
 617		.matches = {
 618			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
 619			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 101 CESIUM"),
 620		},
 621		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 622					BYT_RT5640_JD_NOT_INV |
 623					BYT_RT5640_DIFF_MIC |
 624					BYT_RT5640_SSP0_AIF1 |
 625					BYT_RT5640_MCLK_EN),
 626	},
 627	{
 628		.matches = {
 629			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
 630			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 140 CESIUM"),
 631		},
 632		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 633					BYT_RT5640_JD_SRC_JD2_IN4N |
 634					BYT_RT5640_OVCD_TH_2000UA |
 635					BYT_RT5640_OVCD_SF_0P75 |
 636					BYT_RT5640_SSP0_AIF1 |
 637					BYT_RT5640_MCLK_EN),
 638	},
 639	{
 640		.matches = {
 641			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
 642			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ME176C"),
 643		},
 644		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 645					BYT_RT5640_JD_SRC_JD2_IN4N |
 646					BYT_RT5640_OVCD_TH_2000UA |
 647					BYT_RT5640_OVCD_SF_0P75 |
 648					BYT_RT5640_SSP0_AIF1 |
 649					BYT_RT5640_MCLK_EN |
 650					BYT_RT5640_USE_AMCR0F28),
 651	},
 652	{
 653		/* Asus T100TAF, unlike other T100TA* models this one has a mono speaker */
 654		.matches = {
 655			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
 656			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
 657		},
 658		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 659					BYT_RT5640_JD_SRC_JD2_IN4N |
 660					BYT_RT5640_OVCD_TH_2000UA |
 661					BYT_RT5640_OVCD_SF_0P75 |
 662					BYT_RT5640_MONO_SPEAKER |
 663					BYT_RT5640_DIFF_MIC |
 664					BYT_RT5640_SSP0_AIF2 |
 665					BYT_RT5640_MCLK_EN),
 666	},
 667	{
 668		/* Asus T100TA and T100TAM, must come after T100TAF (mono spk) match */
 669		.matches = {
 670			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
 671			DMI_MATCH(DMI_PRODUCT_NAME, "T100TA"),
 672		},
 673		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 674					BYT_RT5640_JD_SRC_JD2_IN4N |
 675					BYT_RT5640_OVCD_TH_2000UA |
 676					BYT_RT5640_OVCD_SF_0P75 |
 
 
 
 677					BYT_RT5640_MCLK_EN),
 678	},
 679	{
 680		.matches = {
 681			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
 682			DMI_MATCH(DMI_PRODUCT_NAME, "TF103C"),
 683		},
 684		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 685					BYT_RT5640_JD_SRC_EXT_GPIO |
 686					BYT_RT5640_OVCD_TH_2000UA |
 687					BYT_RT5640_OVCD_SF_0P75 |
 688					BYT_RT5640_SSP0_AIF1 |
 689					BYT_RT5640_MCLK_EN |
 690					BYT_RT5640_USE_AMCR0F28),
 691	},
 692	{	/* Chuwi Vi8 (CWI506) */
 693		.matches = {
 694			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
 695			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "i86"),
 696			/* The above are too generic, also match BIOS info */
 697			DMI_MATCH(DMI_BIOS_VERSION, "CHUWI.D86JLBNR"),
 698		},
 699		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 700					BYT_RT5640_MONO_SPEAKER |
 701					BYT_RT5640_SSP0_AIF1 |
 702					BYT_RT5640_MCLK_EN),
 703	},
 704	{	/* Chuwi Vi8 dual-boot (CWI506) */
 705		.matches = {
 706			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
 707			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "i86"),
 708			/* The above are too generic, also match BIOS info */
 709			DMI_MATCH(DMI_BIOS_VERSION, "CHUWI2.D86JHBNR02"),
 710		},
 711		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 712					BYT_RT5640_MONO_SPEAKER |
 713					BYT_RT5640_SSP0_AIF1 |
 714					BYT_RT5640_MCLK_EN),
 715	},
 716	{
 717		/* Chuwi Vi10 (CWI505) */
 718		.matches = {
 719			DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
 720			DMI_MATCH(DMI_BOARD_NAME, "BYT-PF02"),
 721			DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
 722			DMI_MATCH(DMI_PRODUCT_NAME, "S165"),
 723		},
 724		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 725					BYT_RT5640_JD_SRC_JD2_IN4N |
 726					BYT_RT5640_OVCD_TH_2000UA |
 727					BYT_RT5640_OVCD_SF_0P75 |
 728					BYT_RT5640_DIFF_MIC |
 729					BYT_RT5640_SSP0_AIF1 |
 730					BYT_RT5640_MCLK_EN),
 731	},
 732	{
 733		/* Chuwi Hi8 (CWI509) */
 734		.matches = {
 735			DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
 736			DMI_MATCH(DMI_BOARD_NAME, "BYT-PA03C"),
 737			DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
 738			DMI_MATCH(DMI_PRODUCT_NAME, "S806"),
 739		},
 740		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 741					BYT_RT5640_JD_SRC_JD2_IN4N |
 742					BYT_RT5640_OVCD_TH_2000UA |
 743					BYT_RT5640_OVCD_SF_0P75 |
 744					BYT_RT5640_MONO_SPEAKER |
 745					BYT_RT5640_DIFF_MIC |
 746					BYT_RT5640_SSP0_AIF1 |
 747					BYT_RT5640_MCLK_EN),
 748	},
 749	{
 750		.matches = {
 751			DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
 752			DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
 753		},
 754		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP),
 755	},
 756	{	/* Connect Tablet 9 */
 757		.matches = {
 758			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Connect"),
 759			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Tablet 9"),
 760		},
 761		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 762					BYT_RT5640_MONO_SPEAKER |
 763					BYT_RT5640_SSP0_AIF1 |
 764					BYT_RT5640_MCLK_EN),
 765	},
 766	{
 767		.matches = {
 768			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 769			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
 770		},
 771		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 772					BYT_RT5640_JD_SRC_JD2_IN4N |
 773					BYT_RT5640_OVCD_TH_2000UA |
 774					BYT_RT5640_OVCD_SF_0P75 |
 775					BYT_RT5640_MONO_SPEAKER |
 776					BYT_RT5640_MCLK_EN),
 777	},
 778	{	/* Estar Beauty HD MID 7316R */
 779		.matches = {
 780			DMI_MATCH(DMI_SYS_VENDOR, "Estar"),
 781			DMI_MATCH(DMI_PRODUCT_NAME, "eSTAR BEAUTY HD Intel Quad core"),
 782		},
 783		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 784					BYT_RT5640_MONO_SPEAKER |
 785					BYT_RT5640_SSP0_AIF1 |
 786					BYT_RT5640_MCLK_EN),
 787	},
 788	{	/* Glavey TM800A550L */
 789		.matches = {
 790			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
 791			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
 792			/* Above strings are too generic, also match on BIOS version */
 793			DMI_MATCH(DMI_BIOS_VERSION, "ZY-8-BI-PX4S70VTR400-X423B-005-D"),
 794		},
 795		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 796					BYT_RT5640_SSP0_AIF1 |
 797					BYT_RT5640_MCLK_EN),
 798	},
 799	{
 800		.matches = {
 801			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 802			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
 803		},
 804		.driver_data = (void *)(BYT_RT5640_DMIC2_MAP |
 805					BYT_RT5640_MCLK_EN |
 806					BYT_RT5640_LINEOUT |
 807					BYT_RT5640_LINEOUT_AS_HP2 |
 808					BYT_RT5640_HSMIC2_ON_IN1 |
 809					BYT_RT5640_JD_HP_ELITEP_1000G2),
 810	},
 811	{	/* HP Pavilion x2 10-k0XX, 10-n0XX */
 812		.matches = {
 813			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 814			DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
 815		},
 816		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 817					BYT_RT5640_JD_SRC_JD2_IN4N |
 818					BYT_RT5640_OVCD_TH_1500UA |
 819					BYT_RT5640_OVCD_SF_0P75 |
 820					BYT_RT5640_SSP0_AIF1 |
 821					BYT_RT5640_MCLK_EN),
 822	},
 823	{	/* HP Pavilion x2 10-p0XX */
 824		.matches = {
 825			DMI_MATCH(DMI_SYS_VENDOR, "HP"),
 826			DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
 827		},
 828		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 829					BYT_RT5640_JD_SRC_JD1_IN4P |
 830					BYT_RT5640_OVCD_TH_2000UA |
 831					BYT_RT5640_OVCD_SF_0P75 |
 832					BYT_RT5640_MCLK_EN),
 833	},
 834	{	/* HP Pro Tablet 408 */
 835		.matches = {
 836			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 837			DMI_MATCH(DMI_PRODUCT_NAME, "HP Pro Tablet 408"),
 838		},
 839		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 840					BYT_RT5640_JD_SRC_JD2_IN4N |
 841					BYT_RT5640_OVCD_TH_1500UA |
 842					BYT_RT5640_OVCD_SF_0P75 |
 843					BYT_RT5640_SSP0_AIF1 |
 844					BYT_RT5640_MCLK_EN),
 845	},
 846	{	/* HP Stream 7 */
 847		.matches = {
 848			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 849			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 7 Tablet"),
 850		},
 851		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 852					BYT_RT5640_MONO_SPEAKER |
 853					BYT_RT5640_JD_NOT_INV |
 854					BYT_RT5640_SSP0_AIF1 |
 855					BYT_RT5640_MCLK_EN),
 856	},
 857	{	/* HP Stream 8 */
 858		.matches = {
 859			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 860			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 8 Tablet"),
 861		},
 862		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 863					BYT_RT5640_JD_NOT_INV |
 864					BYT_RT5640_SSP0_AIF1 |
 865					BYT_RT5640_MCLK_EN),
 866	},
 867	{	/* I.T.Works TW891 */
 868		.matches = {
 869			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "To be filled by O.E.M."),
 870			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TW891"),
 871			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
 872			DMI_EXACT_MATCH(DMI_BOARD_NAME, "TW891"),
 873		},
 874		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 875					BYT_RT5640_MONO_SPEAKER |
 876					BYT_RT5640_SSP0_AIF1 |
 877					BYT_RT5640_MCLK_EN),
 878	},
 879	{	/* Lamina I8270 / T701BR.SE */
 880		.matches = {
 881			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Lamina"),
 882			DMI_EXACT_MATCH(DMI_BOARD_NAME, "T701BR.SE"),
 883		},
 884		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 885					BYT_RT5640_MONO_SPEAKER |
 886					BYT_RT5640_JD_NOT_INV |
 887					BYT_RT5640_SSP0_AIF1 |
 888					BYT_RT5640_MCLK_EN),
 889	},
 890	{	/* Lenovo Miix 2 8 */
 891		.matches = {
 892			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 893			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "20326"),
 894			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Hiking"),
 895		},
 896		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 897					BYT_RT5640_JD_SRC_JD2_IN4N |
 898					BYT_RT5640_OVCD_TH_2000UA |
 899					BYT_RT5640_OVCD_SF_0P75 |
 900					BYT_RT5640_MONO_SPEAKER |
 901					BYT_RT5640_MCLK_EN),
 902	},
 903	{	/* Lenovo Miix 3-830 */
 904		.matches = {
 905			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 906			DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 3-830"),
 907		},
 908		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 909					BYT_RT5640_JD_SRC_JD2_IN4N |
 910					BYT_RT5640_OVCD_TH_2000UA |
 911					BYT_RT5640_OVCD_SF_0P75 |
 912					BYT_RT5640_MONO_SPEAKER |
 913					BYT_RT5640_DIFF_MIC |
 914					BYT_RT5640_SSP0_AIF1 |
 915					BYT_RT5640_MCLK_EN),
 916	},
 917	{	/* Linx Linx7 tablet */
 918		.matches = {
 919			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LINX"),
 920			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "LINX7"),
 921		},
 922		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 923					BYT_RT5640_MONO_SPEAKER |
 924					BYT_RT5640_JD_NOT_INV |
 925					BYT_RT5640_SSP0_AIF1 |
 926					BYT_RT5640_MCLK_EN),
 927	},
 928	{
 929		/* Medion Lifetab S10346 */
 930		.matches = {
 931			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
 932			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
 933			/* Above strings are much too generic, also match on BIOS date */
 934			DMI_MATCH(DMI_BIOS_DATE, "10/22/2015"),
 935		},
 936		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 937					BYT_RT5640_SWAPPED_SPEAKERS |
 938					BYT_RT5640_SSP0_AIF1 |
 939					BYT_RT5640_MCLK_EN),
 940	},
 941	{	/* Mele PCG03 Mini PC */
 942		.matches = {
 943			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"),
 944			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"),
 945		},
 946		.driver_data = (void *)(BYT_RT5640_NO_INTERNAL_MIC_MAP |
 947					BYT_RT5640_NO_SPEAKERS |
 948					BYT_RT5640_SSP0_AIF1),
 949	},
 950	{	/* MPMAN Converter 9, similar hw as the I.T.Works TW891 2-in-1 */
 951		.matches = {
 952			DMI_MATCH(DMI_SYS_VENDOR, "MPMAN"),
 953			DMI_MATCH(DMI_PRODUCT_NAME, "Converter9"),
 954		},
 955		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 956					BYT_RT5640_MONO_SPEAKER |
 957					BYT_RT5640_SSP0_AIF1 |
 958					BYT_RT5640_MCLK_EN),
 959	},
 960	{
 961		/* MPMAN MPWIN895CL */
 962		.matches = {
 963			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MPMAN"),
 964			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MPWIN8900CL"),
 965		},
 966		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 967					BYT_RT5640_MONO_SPEAKER |
 968					BYT_RT5640_SSP0_AIF1 |
 969					BYT_RT5640_MCLK_EN),
 970	},
 971	{	/* MSI S100 tablet */
 972		.matches = {
 973			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Micro-Star International Co., Ltd."),
 974			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "S100"),
 975		},
 976		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 977					BYT_RT5640_JD_SRC_JD2_IN4N |
 978					BYT_RT5640_OVCD_TH_2000UA |
 979					BYT_RT5640_OVCD_SF_0P75 |
 980					BYT_RT5640_MONO_SPEAKER |
 981					BYT_RT5640_DIFF_MIC |
 982					BYT_RT5640_MCLK_EN),
 983	},
 984	{	/* Nuvison/TMax TM800W560 */
 985		.matches = {
 986			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TMAX"),
 987			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TM800W560L"),
 988		},
 989		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 990					BYT_RT5640_JD_SRC_JD2_IN4N |
 991					BYT_RT5640_OVCD_TH_2000UA |
 992					BYT_RT5640_OVCD_SF_0P75 |
 993					BYT_RT5640_JD_NOT_INV |
 994					BYT_RT5640_DIFF_MIC |
 995					BYT_RT5640_SSP0_AIF1 |
 996					BYT_RT5640_MCLK_EN),
 997	},
 998	{	/* Onda v975w */
 999		.matches = {
1000			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1001			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1002			/* The above are too generic, also match BIOS info */
1003			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "5.6.5"),
1004			DMI_EXACT_MATCH(DMI_BIOS_DATE, "07/25/2014"),
1005		},
1006		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1007					BYT_RT5640_JD_SRC_JD2_IN4N |
1008					BYT_RT5640_OVCD_TH_2000UA |
1009					BYT_RT5640_OVCD_SF_0P75 |
1010					BYT_RT5640_DIFF_MIC |
1011					BYT_RT5640_MCLK_EN),
1012	},
1013	{	/* Pipo W4 */
1014		.matches = {
1015			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1016			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1017			/* The above are too generic, also match BIOS info */
1018			DMI_MATCH(DMI_BIOS_VERSION, "V8L_WIN32_CHIPHD"),
1019		},
1020		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
1021					BYT_RT5640_MONO_SPEAKER |
1022					BYT_RT5640_SSP0_AIF1 |
1023					BYT_RT5640_MCLK_EN),
1024	},
1025	{	/* Point of View Mobii TAB-P800W (V2.0) */
1026		.matches = {
1027			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1028			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1029			/* The above are too generic, also match BIOS info */
1030			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1014"),
1031			DMI_EXACT_MATCH(DMI_BIOS_DATE, "10/24/2014"),
1032		},
1033		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1034					BYT_RT5640_JD_SRC_JD2_IN4N |
1035					BYT_RT5640_OVCD_TH_2000UA |
1036					BYT_RT5640_OVCD_SF_0P75 |
1037					BYT_RT5640_MONO_SPEAKER |
1038					BYT_RT5640_DIFF_MIC |
1039					BYT_RT5640_SSP0_AIF2 |
1040					BYT_RT5640_MCLK_EN),
1041	},
1042	{	/* Point of View Mobii TAB-P800W (V2.1) */
1043		.matches = {
1044			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1045			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1046			/* The above are too generic, also match BIOS info */
1047			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1048			DMI_EXACT_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1049		},
1050		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1051					BYT_RT5640_JD_SRC_JD2_IN4N |
1052					BYT_RT5640_OVCD_TH_2000UA |
1053					BYT_RT5640_OVCD_SF_0P75 |
1054					BYT_RT5640_MONO_SPEAKER |
1055					BYT_RT5640_DIFF_MIC |
1056					BYT_RT5640_SSP0_AIF2 |
1057					BYT_RT5640_MCLK_EN),
1058	},
1059	{	/* Point of View Mobii TAB-P1005W-232 (V2.0) */
1060		.matches = {
1061			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "POV"),
1062			DMI_EXACT_MATCH(DMI_BOARD_NAME, "I102A"),
1063		},
1064		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1065					BYT_RT5640_JD_SRC_JD2_IN4N |
1066					BYT_RT5640_OVCD_TH_2000UA |
1067					BYT_RT5640_OVCD_SF_0P75 |
1068					BYT_RT5640_DIFF_MIC |
1069					BYT_RT5640_SSP0_AIF1 |
1070					BYT_RT5640_MCLK_EN),
1071	},
1072	{
1073		/* Prowise PT301 */
1074		.matches = {
1075			DMI_MATCH(DMI_SYS_VENDOR, "Prowise"),
1076			DMI_MATCH(DMI_PRODUCT_NAME, "PT301"),
1077		},
1078		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1079					BYT_RT5640_JD_SRC_JD2_IN4N |
1080					BYT_RT5640_OVCD_TH_2000UA |
1081					BYT_RT5640_OVCD_SF_0P75 |
1082					BYT_RT5640_DIFF_MIC |
1083					BYT_RT5640_SSP0_AIF1 |
1084					BYT_RT5640_MCLK_EN),
1085	},
1086	{
1087		/* Teclast X89 */
1088		.matches = {
1089			DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
1090			DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
1091		},
1092		.driver_data = (void *)(BYT_RT5640_IN3_MAP |
1093					BYT_RT5640_JD_SRC_JD1_IN4P |
1094					BYT_RT5640_OVCD_TH_2000UA |
1095					BYT_RT5640_OVCD_SF_1P0 |
1096					BYT_RT5640_SSP0_AIF1 |
1097					BYT_RT5640_MCLK_EN),
1098	},
1099	{	/* Toshiba Satellite Click Mini L9W-B */
1100		.matches = {
1101			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1102			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
1103		},
1104		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1105					BYT_RT5640_JD_SRC_JD2_IN4N |
1106					BYT_RT5640_OVCD_TH_1500UA |
1107					BYT_RT5640_OVCD_SF_0P75 |
1108					BYT_RT5640_SSP0_AIF1 |
1109					BYT_RT5640_MCLK_EN),
1110	},
1111	{	/* Toshiba Encore WT8-A */
1112		.matches = {
1113			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1114			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT8-A"),
1115		},
1116		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1117					BYT_RT5640_JD_SRC_JD2_IN4N |
1118					BYT_RT5640_OVCD_TH_2000UA |
1119					BYT_RT5640_OVCD_SF_0P75 |
1120					BYT_RT5640_JD_NOT_INV |
1121					BYT_RT5640_MCLK_EN),
1122	},
1123	{	/* Toshiba Encore WT10-A */
1124		.matches = {
1125			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1126			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT10-A-103"),
1127		},
1128		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1129					BYT_RT5640_JD_SRC_JD1_IN4P |
1130					BYT_RT5640_OVCD_TH_2000UA |
1131					BYT_RT5640_OVCD_SF_0P75 |
1132					BYT_RT5640_SSP0_AIF2 |
1133					BYT_RT5640_MCLK_EN),
1134	},
1135	{
1136		/* Vexia Edu Atla 10 tablet 5V version */
1137		.matches = {
1138			/* Having all 3 of these not set is somewhat unique */
1139			DMI_MATCH(DMI_SYS_VENDOR, "To be filled by O.E.M."),
1140			DMI_MATCH(DMI_PRODUCT_NAME, "To be filled by O.E.M."),
1141			DMI_MATCH(DMI_BOARD_NAME, "To be filled by O.E.M."),
1142			/* Above strings are too generic, also match on BIOS date */
1143			DMI_MATCH(DMI_BIOS_DATE, "05/14/2015"),
1144		},
1145		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
1146					BYT_RT5640_JD_NOT_INV |
1147					BYT_RT5640_SSP0_AIF1 |
1148					BYT_RT5640_MCLK_EN),
1149	},
1150	{	/* Vexia Edu Atla 10 tablet 9V version */
1151		.matches = {
1152			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1153			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1154			/* Above strings are too generic, also match on BIOS date */
1155			DMI_MATCH(DMI_BIOS_DATE, "08/25/2014"),
1156		},
1157		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1158					BYT_RT5640_JD_SRC_JD2_IN4N |
1159					BYT_RT5640_OVCD_TH_2000UA |
1160					BYT_RT5640_OVCD_SF_0P75 |
1161					BYT_RT5640_DIFF_MIC |
1162					BYT_RT5640_SSP0_AIF2 |
1163					BYT_RT5640_MCLK_EN),
1164	},
1165	{	/* Voyo Winpad A15 */
1166		.matches = {
1167			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1168			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1169			/* Above strings are too generic, also match on BIOS date */
1170			DMI_MATCH(DMI_BIOS_DATE, "11/20/2014"),
1171		},
1172		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1173					BYT_RT5640_JD_SRC_JD2_IN4N |
1174					BYT_RT5640_OVCD_TH_2000UA |
1175					BYT_RT5640_OVCD_SF_0P75 |
1176					BYT_RT5640_DIFF_MIC |
1177					BYT_RT5640_MCLK_EN),
1178	},
1179	{	/* Catch-all for generic Insyde tablets, must be last */
1180		.matches = {
1181			DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
1182		},
1183		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
1184					BYT_RT5640_MCLK_EN |
1185					BYT_RT5640_SSP0_AIF1),
1186
1187	},
1188	{}
1189};
1190
1191/*
1192 * Note this MUST be called before snd_soc_register_card(), so that the props
1193 * are in place before the codec component driver's probe function parses them.
1194 */
1195static int byt_rt5640_add_codec_device_props(struct device *i2c_dev,
1196					     struct byt_rt5640_private *priv)
1197{
1198	struct property_entry props[MAX_NO_PROPS] = {};
1199	struct fwnode_handle *fwnode;
1200	int cnt = 0;
1201	int ret;
1202
1203	switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1204	case BYT_RT5640_DMIC1_MAP:
1205		props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic1-data-pin",
1206						  RT5640_DMIC1_DATA_PIN_IN1P);
1207		break;
1208	case BYT_RT5640_DMIC2_MAP:
1209		props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic2-data-pin",
1210						  RT5640_DMIC2_DATA_PIN_IN1N);
1211		break;
1212	case BYT_RT5640_IN1_MAP:
1213		if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1214			props[cnt++] =
1215				PROPERTY_ENTRY_BOOL("realtek,in1-differential");
1216		break;
1217	case BYT_RT5640_IN3_MAP:
1218		if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1219			props[cnt++] =
1220				PROPERTY_ENTRY_BOOL("realtek,in3-differential");
1221		break;
1222	}
1223
1224	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1225		if (BYT_RT5640_JDSRC(byt_rt5640_quirk) != RT5640_JD_SRC_EXT_GPIO) {
1226			props[cnt++] = PROPERTY_ENTRY_U32(
1227					    "realtek,jack-detect-source",
1228					    BYT_RT5640_JDSRC(byt_rt5640_quirk));
1229		}
1230
1231		props[cnt++] = PROPERTY_ENTRY_U32(
1232				    "realtek,over-current-threshold-microamp",
1233				    BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
1234
1235		props[cnt++] = PROPERTY_ENTRY_U32(
1236				    "realtek,over-current-scale-factor",
1237				    BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
1238	}
1239
1240	if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
1241		props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
1242
1243	fwnode = fwnode_create_software_node(props, NULL);
1244	if (IS_ERR(fwnode)) {
1245		/* put_device() is handled in caller */
1246		return PTR_ERR(fwnode);
1247	}
1248
1249	ret = device_add_software_node(i2c_dev, to_software_node(fwnode));
1250
1251	fwnode_handle_put(fwnode);
1252
1253	return ret;
1254}
1255
1256/* Some Android devs specify IRQs/GPIOS in a special AMCR0F28 ACPI device */
1257static const struct acpi_gpio_params amcr0f28_jd_gpio = { 1, 0, false };
1258
1259static const struct acpi_gpio_mapping amcr0f28_gpios[] = {
1260	{ "rt5640-jd-gpios", &amcr0f28_jd_gpio, 1 },
1261	{ }
1262};
1263
1264static int byt_rt5640_get_amcr0f28_settings(struct snd_soc_card *card)
1265{
1266	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1267	struct rt5640_set_jack_data *data = &priv->jack_data;
1268	struct acpi_device *adev;
1269	int ret = 0;
1270
1271	adev = acpi_dev_get_first_match_dev("AMCR0F28", "1", -1);
1272	if (!adev) {
1273		dev_err(card->dev, "error cannot find AMCR0F28 adev\n");
1274		return -ENOENT;
1275	}
1276
1277	data->codec_irq_override = acpi_dev_gpio_irq_get(adev, 0);
1278	if (data->codec_irq_override < 0) {
1279		ret = data->codec_irq_override;
1280		dev_err(card->dev, "error %d getting codec IRQ\n", ret);
1281		goto put_adev;
1282	}
1283
1284	if (BYT_RT5640_JDSRC(byt_rt5640_quirk) == RT5640_JD_SRC_EXT_GPIO) {
1285		acpi_dev_add_driver_gpios(adev, amcr0f28_gpios);
1286		data->jd_gpio = devm_fwnode_gpiod_get(card->dev, acpi_fwnode_handle(adev),
1287						      "rt5640-jd", GPIOD_IN, "rt5640-jd");
1288		acpi_dev_remove_driver_gpios(adev);
1289
1290		if (IS_ERR(data->jd_gpio)) {
1291			ret = PTR_ERR(data->jd_gpio);
1292			dev_err(card->dev, "error %d getting jd GPIO\n", ret);
1293		}
1294	}
1295
1296put_adev:
1297	acpi_dev_put(adev);
1298	return ret;
1299}
1300
1301static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
1302{
1303	struct snd_soc_card *card = runtime->card;
1304	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1305	struct rt5640_set_jack_data *jack_data = &priv->jack_data;
1306	struct snd_soc_component *component = snd_soc_rtd_to_codec(runtime, 0)->component;
1307	const struct snd_soc_dapm_route *custom_map = NULL;
1308	int num_routes = 0;
1309	int ret;
1310
1311	card->dapm.idle_bias_off = true;
1312	jack_data->use_platform_clock = true;
1313
1314	/* Start with RC clk for jack-detect (we disable MCLK below) */
1315	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
1316		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1317			RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_RCCLK);
1318
1319	rt5640_sel_asrc_clk_src(component,
1320				RT5640_DA_STEREO_FILTER |
1321				RT5640_DA_MONO_L_FILTER	|
1322				RT5640_DA_MONO_R_FILTER	|
1323				RT5640_AD_STEREO_FILTER	|
1324				RT5640_AD_MONO_L_FILTER	|
1325				RT5640_AD_MONO_R_FILTER,
1326				RT5640_CLK_SEL_ASRC);
1327
1328	ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
1329					ARRAY_SIZE(byt_rt5640_controls));
1330	if (ret) {
1331		dev_err(card->dev, "unable to add card controls\n");
1332		return ret;
1333	}
1334
1335	switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1336	case BYT_RT5640_IN1_MAP:
1337		custom_map = byt_rt5640_intmic_in1_map;
1338		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
1339		break;
1340	case BYT_RT5640_IN3_MAP:
1341		custom_map = byt_rt5640_intmic_in3_map;
1342		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
1343		break;
1344	case BYT_RT5640_DMIC1_MAP:
1345		custom_map = byt_rt5640_intmic_dmic1_map;
1346		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
1347		break;
1348	case BYT_RT5640_DMIC2_MAP:
1349		custom_map = byt_rt5640_intmic_dmic2_map;
1350		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
1351		break;
1352	}
1353
1354	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
1355	if (ret)
1356		return ret;
1357
1358	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1) {
1359		ret = snd_soc_dapm_add_routes(&card->dapm,
1360					byt_rt5640_hsmic2_in1_map,
1361					ARRAY_SIZE(byt_rt5640_hsmic2_in1_map));
1362		if (ret)
1363			return ret;
1364	}
1365
1366	if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
1367		ret = snd_soc_dapm_add_routes(&card->dapm,
1368					byt_rt5640_ssp2_aif2_map,
1369					ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
1370	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
1371		ret = snd_soc_dapm_add_routes(&card->dapm,
1372					byt_rt5640_ssp0_aif1_map,
1373					ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
1374	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
1375		ret = snd_soc_dapm_add_routes(&card->dapm,
1376					byt_rt5640_ssp0_aif2_map,
1377					ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
1378	} else {
1379		ret = snd_soc_dapm_add_routes(&card->dapm,
1380					byt_rt5640_ssp2_aif1_map,
1381					ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
1382	}
1383	if (ret)
1384		return ret;
1385
1386	if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1387		ret = snd_soc_dapm_add_routes(&card->dapm,
1388					byt_rt5640_mono_spk_map,
1389					ARRAY_SIZE(byt_rt5640_mono_spk_map));
1390	} else if (!(byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)) {
1391		ret = snd_soc_dapm_add_routes(&card->dapm,
1392					byt_rt5640_stereo_spk_map,
1393					ARRAY_SIZE(byt_rt5640_stereo_spk_map));
1394	}
1395	if (ret)
1396		return ret;
1397
1398	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1399		ret = snd_soc_dapm_add_routes(&card->dapm,
1400					byt_rt5640_lineout_map,
1401					ARRAY_SIZE(byt_rt5640_lineout_map));
1402		if (ret)
1403			return ret;
1404	}
1405
1406	/*
1407	 * The firmware might enable the clock at boot (this information
1408	 * may or may not be reflected in the enable clock register).
1409	 * To change the rate we must disable the clock first to cover
1410	 * these cases. Due to common clock framework restrictions that
1411	 * do not allow to disable a clock that has not been enabled,
1412	 * we need to enable the clock first.
1413	 */
1414	ret = clk_prepare_enable(priv->mclk);
1415	if (!ret)
1416		clk_disable_unprepare(priv->mclk);
1417
1418	if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
1419		ret = clk_set_rate(priv->mclk, 25000000);
1420	else
1421		ret = clk_set_rate(priv->mclk, 19200000);
1422	if (ret) {
1423		dev_err(card->dev, "unable to set MCLK rate\n");
1424		return ret;
1425	}
1426
1427	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1428		ret = snd_soc_card_jack_new_pins(card, "Headset",
1429						 SND_JACK_HEADSET | SND_JACK_BTN_0,
1430						 &priv->jack, rt5640_pins,
1431						 ARRAY_SIZE(rt5640_pins));
1432		if (ret) {
1433			dev_err(card->dev, "Jack creation failed %d\n", ret);
1434			return ret;
1435		}
1436		snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
1437				 KEY_PLAYPAUSE);
1438
1439		if (byt_rt5640_quirk & BYT_RT5640_USE_AMCR0F28) {
1440			ret = byt_rt5640_get_amcr0f28_settings(card);
1441			if (ret)
1442				return ret;
1443		}
1444
1445		snd_soc_component_set_jack(component, &priv->jack, &priv->jack_data);
1446	}
1447
1448	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1449		ret = snd_soc_card_jack_new_pins(card, "Headset",
1450						 SND_JACK_HEADSET,
1451						 &priv->jack, rt5640_pins,
1452						 ARRAY_SIZE(rt5640_pins));
1453		if (ret)
1454			return ret;
1455
1456		ret = snd_soc_card_jack_new_pins(card, "Headset 2",
1457						 SND_JACK_HEADSET,
1458						 &priv->jack2, rt5640_pins2,
1459						 ARRAY_SIZE(rt5640_pins2));
1460		if (ret)
1461			return ret;
1462
1463		rt5640_jack_gpio.data = priv;
1464		rt5640_jack_gpio.gpiod_dev = priv->codec_dev;
1465		rt5640_jack_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack1_check;
1466		ret = snd_soc_jack_add_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1467		if (ret)
1468			return ret;
1469
1470		rt5640_set_ovcd_params(component);
1471		rt5640_jack2_gpio.data = component;
1472		rt5640_jack2_gpio.gpiod_dev = priv->codec_dev;
1473		rt5640_jack2_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack2_check;
1474		ret = snd_soc_jack_add_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1475		if (ret) {
1476			snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1477			return ret;
1478		}
1479	}
1480
1481	return 0;
1482}
1483
1484static void byt_rt5640_exit(struct snd_soc_pcm_runtime *runtime)
1485{
1486	struct snd_soc_card *card = runtime->card;
1487	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1488
1489	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1490		snd_soc_jack_free_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1491		snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1492	}
1493}
1494
1495static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
1496			    struct snd_pcm_hw_params *params)
1497{
1498	struct snd_interval *rate = hw_param_interval(params,
1499			SNDRV_PCM_HW_PARAM_RATE);
1500	struct snd_interval *channels = hw_param_interval(params,
1501						SNDRV_PCM_HW_PARAM_CHANNELS);
1502	int ret, bits;
1503
1504	/* The DSP will convert the FE rate to 48k, stereo */
1505	rate->min = rate->max = 48000;
1506	channels->min = channels->max = 2;
1507
1508	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1509	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1510		/* set SSP0 to 16-bit */
1511		params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
1512		bits = 16;
1513	} else {
1514		/* set SSP2 to 24-bit */
1515		params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
1516		bits = 24;
1517	}
1518
1519	/*
1520	 * Default mode for SSP configuration is TDM 4 slot, override config
1521	 * with explicit setting to I2S 2ch. The word length is set with
1522	 * dai_set_tdm_slot() since there is no other API exposed
1523	 */
1524	ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0),
1525				  SND_SOC_DAIFMT_I2S     |
1526				  SND_SOC_DAIFMT_NB_NF   |
1527				  SND_SOC_DAIFMT_BP_FP);
1528	if (ret < 0) {
1529		dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
1530		return ret;
1531	}
1532
1533	ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
1534	if (ret < 0) {
1535		dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
1536		return ret;
1537	}
1538
1539	return 0;
1540}
1541
1542static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
1543{
1544	return snd_pcm_hw_constraint_single(substream->runtime,
1545			SNDRV_PCM_HW_PARAM_RATE, 48000);
1546}
1547
1548static const struct snd_soc_ops byt_rt5640_aif1_ops = {
1549	.startup = byt_rt5640_aif1_startup,
1550};
1551
1552static const struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
1553	.hw_params = byt_rt5640_aif1_hw_params,
1554};
1555
1556SND_SOC_DAILINK_DEF(dummy,
1557	DAILINK_COMP_ARRAY(COMP_DUMMY()));
1558
1559SND_SOC_DAILINK_DEF(media,
1560	DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
1561
1562SND_SOC_DAILINK_DEF(deepbuffer,
1563	DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
1564
1565SND_SOC_DAILINK_DEF(ssp2_port,
1566	/* overwritten for ssp0 routing */
1567	DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
1568SND_SOC_DAILINK_DEF(ssp2_codec,
1569	DAILINK_COMP_ARRAY(COMP_CODEC(
1570	/* overwritten with HID */ "i2c-10EC5640:00",
1571	/* changed w/ quirk */	"rt5640-aif1")));
1572
1573SND_SOC_DAILINK_DEF(platform,
1574	DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
1575
1576static struct snd_soc_dai_link byt_rt5640_dais[] = {
1577	[MERR_DPCM_AUDIO] = {
1578		.name = "Baytrail Audio Port",
1579		.stream_name = "Baytrail Audio",
1580		.nonatomic = true,
1581		.dynamic = 1,
 
 
1582		.ops = &byt_rt5640_aif1_ops,
1583		SND_SOC_DAILINK_REG(media, dummy, platform),
1584	},
1585	[MERR_DPCM_DEEP_BUFFER] = {
1586		.name = "Deep-Buffer Audio Port",
1587		.stream_name = "Deep-Buffer Audio",
1588		.nonatomic = true,
1589		.dynamic = 1,
1590		.playback_only = 1,
1591		.ops = &byt_rt5640_aif1_ops,
1592		SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
1593	},
1594		/* back ends */
1595	{
1596		.name = "SSP2-Codec",
1597		.id = 0,
1598		.no_pcm = 1,
1599		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1600						| SND_SOC_DAIFMT_CBC_CFC,
1601		.be_hw_params_fixup = byt_rt5640_codec_fixup,
 
 
1602		.init = byt_rt5640_init,
1603		.exit = byt_rt5640_exit,
1604		.ops = &byt_rt5640_be_ssp2_ops,
1605		SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
1606	},
1607};
1608
1609/* SoC card */
1610static char byt_rt5640_codec_name[SND_ACPI_I2C_ID_LEN];
1611#if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1612static char byt_rt5640_long_name[40]; /* = "bytcr-rt5640-*-spk-*-mic" */
1613#endif
1614static char byt_rt5640_components[64]; /* = "cfg-spk:* cfg-mic:* ..." */
1615
1616static int byt_rt5640_suspend(struct snd_soc_card *card)
1617{
1618	struct snd_soc_component *component;
1619
1620	if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1621		return 0;
1622
1623	for_each_card_components(card, component) {
1624		if (!strcmp(component->name, byt_rt5640_codec_name)) {
1625			dev_dbg(component->dev, "disabling jack detect before suspend\n");
1626			snd_soc_component_set_jack(component, NULL, NULL);
1627			break;
1628		}
1629	}
1630
1631	return 0;
1632}
1633
1634static int byt_rt5640_resume(struct snd_soc_card *card)
1635{
1636	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1637	struct snd_soc_component *component;
1638
1639	if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1640		return 0;
1641
1642	for_each_card_components(card, component) {
1643		if (!strcmp(component->name, byt_rt5640_codec_name)) {
1644			dev_dbg(component->dev, "re-enabling jack detect after resume\n");
1645			snd_soc_component_set_jack(component, &priv->jack,
1646						   &priv->jack_data);
1647			break;
1648		}
1649	}
1650
1651	return 0;
1652}
1653
1654/* use space before codec name to simplify card ID, and simplify driver name */
1655#define SOF_CARD_NAME "bytcht rt5640" /* card name will be 'sof-bytcht rt5640' */
1656#define SOF_DRIVER_NAME "SOF"
1657
1658#define CARD_NAME "bytcr-rt5640"
1659#define DRIVER_NAME NULL /* card name will be used for driver name */
1660
1661static struct snd_soc_card byt_rt5640_card = {
1662	.owner = THIS_MODULE,
1663	.dai_link = byt_rt5640_dais,
1664	.num_links = ARRAY_SIZE(byt_rt5640_dais),
1665	.dapm_widgets = byt_rt5640_widgets,
1666	.num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
1667	.dapm_routes = byt_rt5640_audio_map,
1668	.num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
1669	.fully_routed = true,
1670	.suspend_pre = byt_rt5640_suspend,
1671	.resume_post = byt_rt5640_resume,
1672};
1673
1674struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
1675	u64 aif_value;       /* 1: AIF1, 2: AIF2 */
1676	u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
1677};
1678
1679static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
1680{
1681	struct device *dev = &pdev->dev;
1682	static const char * const map_name[] = { "dmic1", "dmic2", "in1", "in3", "none" };
1683	struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
1684	__maybe_unused const char *spk_type;
1685	const struct dmi_system_id *dmi_id;
1686	const char *headset2_string = "";
1687	const char *lineout_string = "";
1688	struct byt_rt5640_private *priv;
1689	const char *platform_name;
1690	struct acpi_device *adev;
1691	struct device *codec_dev;
1692	const char *cfg_spk;
1693	bool sof_parent;
1694	int ret_val = 0;
1695	int dai_index = 0;
1696	int i, aif;
1697
1698	is_bytcr = false;
1699	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1700	if (!priv)
1701		return -ENOMEM;
1702
1703	/* register the soc card */
1704	byt_rt5640_card.dev = dev;
1705	snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
1706
1707	/* fix index of codec dai */
1708	for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
1709		if (byt_rt5640_dais[i].num_codecs &&
1710		    !strcmp(byt_rt5640_dais[i].codecs->name,
1711			    "i2c-10EC5640:00")) {
1712			dai_index = i;
1713			break;
1714		}
1715	}
1716
1717	/* fixup codec name based on HID */
1718	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
1719	if (adev) {
1720		snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
1721			 "i2c-%s", acpi_dev_name(adev));
1722		byt_rt5640_dais[dai_index].codecs->name = byt_rt5640_codec_name;
1723	} else {
1724		dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
1725		return -ENOENT;
1726	}
1727
1728	codec_dev = acpi_get_first_physical_node(adev);
1729	acpi_dev_put(adev);
1730
1731	if (codec_dev) {
1732		priv->codec_dev = get_device(codec_dev);
1733	} else {
1734		/*
1735		 * Special case for Android tablets where the codec i2c_client
1736		 * has been manually instantiated by x86_android_tablets.ko due
1737		 * to a broken DSDT.
1738		 */
1739		codec_dev = bus_find_device_by_name(&i2c_bus_type, NULL,
1740					BYT_RT5640_FALLBACK_CODEC_DEV_NAME);
1741		if (!codec_dev)
1742			return -EPROBE_DEFER;
1743
1744		if (!i2c_verify_client(codec_dev)) {
1745			dev_err(dev, "Error '%s' is not an i2c_client\n",
1746				BYT_RT5640_FALLBACK_CODEC_DEV_NAME);
1747			put_device(codec_dev);
1748		}
1749
1750		/* fixup codec name */
1751		strscpy(byt_rt5640_codec_name, BYT_RT5640_FALLBACK_CODEC_DEV_NAME,
1752			sizeof(byt_rt5640_codec_name));
1753
1754		/* bus_find_device() returns a reference no need to get() */
1755		priv->codec_dev = codec_dev;
1756	}
1757
1758	/*
1759	 * swap SSP0 if bytcr is detected
1760	 * (will be overridden if DMI quirk is detected)
1761	 */
1762	if (soc_intel_is_byt()) {
1763		if (mach->mach_params.acpi_ipc_irq_index == 0)
1764			is_bytcr = true;
1765	}
1766
1767	if (is_bytcr) {
1768		/*
1769		 * Baytrail CR platforms may have CHAN package in BIOS, try
1770		 * to find relevant routing quirk based as done on Windows
1771		 * platforms. We have to read the information directly from the
1772		 * BIOS, at this stage the card is not created and the links
1773		 * with the codec driver/pdata are non-existent
1774		 */
1775
1776		struct acpi_chan_package chan_package = { 0 };
1777
1778		/* format specified: 2 64-bit integers */
1779		struct acpi_buffer format = {sizeof("NN"), "NN"};
1780		struct acpi_buffer state = {0, NULL};
1781		struct snd_soc_acpi_package_context pkg_ctx;
1782		bool pkg_found = false;
1783
1784		state.length = sizeof(chan_package);
1785		state.pointer = &chan_package;
1786
1787		pkg_ctx.name = "CHAN";
1788		pkg_ctx.length = 2;
1789		pkg_ctx.format = &format;
1790		pkg_ctx.state = &state;
1791		pkg_ctx.data_valid = false;
1792
1793		pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
1794							       &pkg_ctx);
1795		if (pkg_found) {
1796			if (chan_package.aif_value == 1) {
1797				dev_info(dev, "BIOS Routing: AIF1 connected\n");
1798				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF1;
1799			} else  if (chan_package.aif_value == 2) {
1800				dev_info(dev, "BIOS Routing: AIF2 connected\n");
1801				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1802			} else {
1803				dev_info(dev, "BIOS Routing isn't valid, ignored\n");
1804				pkg_found = false;
1805			}
1806		}
1807
1808		if (!pkg_found) {
1809			/* no BIOS indications, assume SSP0-AIF2 connection */
1810			byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1811		}
1812
1813		/* change defaults for Baytrail-CR capture */
1814		byt_rt5640_quirk |= BYTCR_INPUT_DEFAULTS;
1815	} else {
1816		byt_rt5640_quirk |= BYT_RT5640_DMIC1_MAP |
1817				    BYT_RT5640_JD_SRC_JD2_IN4N |
1818				    BYT_RT5640_OVCD_TH_2000UA |
1819				    BYT_RT5640_OVCD_SF_0P75;
1820	}
1821
1822	/* check quirks before creating card */
1823	dmi_id = dmi_first_match(byt_rt5640_quirk_table);
1824	if (dmi_id)
1825		byt_rt5640_quirk = (unsigned long)dmi_id->driver_data;
1826	if (quirk_override != -1) {
1827		dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
1828			 byt_rt5640_quirk, quirk_override);
1829		byt_rt5640_quirk = quirk_override;
1830	}
1831
1832	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1833		acpi_dev_add_driver_gpios(ACPI_COMPANION(priv->codec_dev),
1834					  byt_rt5640_hp_elitepad_1000g2_gpios);
1835
1836		priv->hsmic_detect = devm_fwnode_gpiod_get(dev, codec_dev->fwnode,
1837							   "headset-mic-detect", GPIOD_IN,
1838							   "headset-mic-detect");
1839		if (IS_ERR(priv->hsmic_detect)) {
1840			ret_val = dev_err_probe(dev, PTR_ERR(priv->hsmic_detect),
1841						"getting hsmic-detect GPIO\n");
1842			goto err_device;
1843		}
1844	}
1845
1846	/* Must be called before register_card, also see declaration comment. */
1847	ret_val = byt_rt5640_add_codec_device_props(codec_dev, priv);
1848	if (ret_val)
1849		goto err_remove_gpios;
1850
1851	log_quirks(dev);
1852
1853	if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
1854	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1855		byt_rt5640_dais[dai_index].codecs->dai_name = "rt5640-aif2";
1856		aif = 2;
1857	} else {
1858		aif = 1;
1859	}
1860
1861	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1862	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2))
1863		byt_rt5640_dais[dai_index].cpus->dai_name = "ssp0-port";
1864
1865	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
1866		priv->mclk = devm_clk_get_optional(dev, "pmc_plt_clk_3");
1867		if (IS_ERR(priv->mclk)) {
1868			ret_val = dev_err_probe(dev, PTR_ERR(priv->mclk),
1869						"Failed to get MCLK from pmc_plt_clk_3\n");
1870			goto err;
1871		}
1872		/*
1873		 * Fall back to bit clock usage when clock is not
1874		 * available likely due to missing dependencies.
1875		 */
1876		if (!priv->mclk)
1877			byt_rt5640_quirk &= ~BYT_RT5640_MCLK_EN;
1878	}
1879
1880	if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS) {
1881		cfg_spk = "0";
1882		spk_type = "none";
1883	} else if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1884		cfg_spk = "1";
1885		spk_type = "mono";
1886	} else if (byt_rt5640_quirk & BYT_RT5640_SWAPPED_SPEAKERS) {
1887		cfg_spk = "swapped";
1888		spk_type = "swapped";
1889	} else {
1890		cfg_spk = "2";
1891		spk_type = "stereo";
1892	}
1893
1894	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1895		if (byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2)
1896			lineout_string = " cfg-hp2:lineout";
1897		else
1898			lineout_string = " cfg-lineout:2";
1899	}
1900
1901	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1)
1902		headset2_string = " cfg-hs2:in1";
1903
1904	snprintf(byt_rt5640_components, sizeof(byt_rt5640_components),
1905		 "cfg-spk:%s cfg-mic:%s aif:%d%s%s", cfg_spk,
1906		 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)], aif,
1907		 lineout_string, headset2_string);
1908	byt_rt5640_card.components = byt_rt5640_components;
1909#if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1910	snprintf(byt_rt5640_long_name, sizeof(byt_rt5640_long_name),
1911		 "bytcr-rt5640-%s-spk-%s-mic", spk_type,
1912		 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)]);
1913	byt_rt5640_card.long_name = byt_rt5640_long_name;
1914#endif
1915
1916	/* override platform name, if required */
1917	platform_name = mach->mach_params.platform;
1918
1919	ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5640_card,
1920							platform_name);
1921	if (ret_val)
1922		goto err;
1923
1924	sof_parent = snd_soc_acpi_sof_parent(dev);
1925
1926	/* set card and driver name */
1927	if (sof_parent) {
1928		byt_rt5640_card.name = SOF_CARD_NAME;
1929		byt_rt5640_card.driver_name = SOF_DRIVER_NAME;
1930	} else {
1931		byt_rt5640_card.name = CARD_NAME;
1932		byt_rt5640_card.driver_name = DRIVER_NAME;
1933	}
1934
1935	/* set pm ops */
1936	if (sof_parent)
1937		dev->driver->pm = &snd_soc_pm_ops;
1938
1939	ret_val = devm_snd_soc_register_card(dev, &byt_rt5640_card);
1940	if (ret_val) {
1941		dev_err(dev, "devm_snd_soc_register_card failed %d\n", ret_val);
1942		goto err;
1943	}
1944	platform_set_drvdata(pdev, &byt_rt5640_card);
1945	return ret_val;
1946
1947err:
1948	device_remove_software_node(priv->codec_dev);
1949err_remove_gpios:
1950	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1951		acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1952err_device:
1953	put_device(priv->codec_dev);
1954	return ret_val;
1955}
1956
1957static void snd_byt_rt5640_mc_remove(struct platform_device *pdev)
1958{
1959	struct snd_soc_card *card = platform_get_drvdata(pdev);
1960	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1961
1962	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1963		acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1964
1965	device_remove_software_node(priv->codec_dev);
1966	put_device(priv->codec_dev);
1967}
1968
1969static struct platform_driver snd_byt_rt5640_mc_driver = {
1970	.driver = {
1971		.name = "bytcr_rt5640",
1972	},
1973	.probe = snd_byt_rt5640_mc_probe,
1974	.remove = snd_byt_rt5640_mc_remove,
1975};
1976
1977module_platform_driver(snd_byt_rt5640_mc_driver);
1978
1979MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
1980MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
1981MODULE_LICENSE("GPL v2");
1982MODULE_ALIAS("platform:bytcr_rt5640");
v6.8
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
   4 *
   5 *  Copyright (C) 2014 Intel Corp
   6 *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
   7 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   8 *
   9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10 */
  11
  12#include <linux/i2c.h>
  13#include <linux/init.h>
  14#include <linux/module.h>
  15#include <linux/moduleparam.h>
  16#include <linux/platform_device.h>
  17#include <linux/acpi.h>
  18#include <linux/clk.h>
  19#include <linux/device.h>
 
  20#include <linux/dmi.h>
  21#include <linux/gpio/consumer.h>
  22#include <linux/gpio/machine.h>
  23#include <linux/input.h>
  24#include <linux/slab.h>
  25#include <sound/pcm.h>
  26#include <sound/pcm_params.h>
  27#include <sound/soc.h>
  28#include <sound/jack.h>
  29#include <sound/soc-acpi.h>
  30#include <dt-bindings/sound/rt5640.h>
  31#include "../../codecs/rt5640.h"
  32#include "../atom/sst-atom-controls.h"
  33#include "../common/soc-intel-quirks.h"
  34
 
 
  35enum {
  36	BYT_RT5640_DMIC1_MAP,
  37	BYT_RT5640_DMIC2_MAP,
  38	BYT_RT5640_IN1_MAP,
  39	BYT_RT5640_IN3_MAP,
  40	BYT_RT5640_NO_INTERNAL_MIC_MAP,
  41};
  42
  43#define RT5640_JD_SRC_EXT_GPIO			0x0f
  44
  45enum {
  46	BYT_RT5640_JD_SRC_GPIO1		= (RT5640_JD_SRC_GPIO1 << 4),
  47	BYT_RT5640_JD_SRC_JD1_IN4P	= (RT5640_JD_SRC_JD1_IN4P << 4),
  48	BYT_RT5640_JD_SRC_JD2_IN4N	= (RT5640_JD_SRC_JD2_IN4N << 4),
  49	BYT_RT5640_JD_SRC_GPIO2		= (RT5640_JD_SRC_GPIO2 << 4),
  50	BYT_RT5640_JD_SRC_GPIO3		= (RT5640_JD_SRC_GPIO3 << 4),
  51	BYT_RT5640_JD_SRC_GPIO4		= (RT5640_JD_SRC_GPIO4 << 4),
  52	BYT_RT5640_JD_SRC_EXT_GPIO	= (RT5640_JD_SRC_EXT_GPIO << 4)
  53};
  54
  55enum {
  56	BYT_RT5640_OVCD_TH_600UA	= (6 << 8),
  57	BYT_RT5640_OVCD_TH_1500UA	= (15 << 8),
  58	BYT_RT5640_OVCD_TH_2000UA	= (20 << 8),
  59};
  60
  61enum {
  62	BYT_RT5640_OVCD_SF_0P5		= (RT5640_OVCD_SF_0P5 << 13),
  63	BYT_RT5640_OVCD_SF_0P75		= (RT5640_OVCD_SF_0P75 << 13),
  64	BYT_RT5640_OVCD_SF_1P0		= (RT5640_OVCD_SF_1P0 << 13),
  65	BYT_RT5640_OVCD_SF_1P5		= (RT5640_OVCD_SF_1P5 << 13),
  66};
  67
  68#define BYT_RT5640_MAP(quirk)		((quirk) &  GENMASK(3, 0))
  69#define BYT_RT5640_JDSRC(quirk)		(((quirk) & GENMASK(7, 4)) >> 4)
  70#define BYT_RT5640_OVCD_TH(quirk)	(((quirk) & GENMASK(12, 8)) >> 8)
  71#define BYT_RT5640_OVCD_SF(quirk)	(((quirk) & GENMASK(14, 13)) >> 13)
  72#define BYT_RT5640_JD_NOT_INV		BIT(16)
  73#define BYT_RT5640_MONO_SPEAKER		BIT(17)
  74#define BYT_RT5640_DIFF_MIC		BIT(18) /* default is single-ended */
  75#define BYT_RT5640_SSP2_AIF2		BIT(19) /* default is using AIF1  */
  76#define BYT_RT5640_SSP0_AIF1		BIT(20)
  77#define BYT_RT5640_SSP0_AIF2		BIT(21)
  78#define BYT_RT5640_MCLK_EN		BIT(22)
  79#define BYT_RT5640_MCLK_25MHZ		BIT(23)
  80#define BYT_RT5640_NO_SPEAKERS		BIT(24)
  81#define BYT_RT5640_LINEOUT		BIT(25)
  82#define BYT_RT5640_LINEOUT_AS_HP2	BIT(26)
  83#define BYT_RT5640_HSMIC2_ON_IN1	BIT(27)
  84#define BYT_RT5640_JD_HP_ELITEP_1000G2	BIT(28)
  85#define BYT_RT5640_USE_AMCR0F28		BIT(29)
  86#define BYT_RT5640_SWAPPED_SPEAKERS	BIT(30)
  87
  88#define BYTCR_INPUT_DEFAULTS				\
  89	(BYT_RT5640_IN3_MAP |				\
  90	 BYT_RT5640_JD_SRC_JD1_IN4P |			\
  91	 BYT_RT5640_OVCD_TH_2000UA |			\
  92	 BYT_RT5640_OVCD_SF_0P75 |			\
  93	 BYT_RT5640_DIFF_MIC)
  94
  95/* in-diff or dmic-pin + jdsrc + ovcd-th + -sf + jd-inv + terminating entry */
  96#define MAX_NO_PROPS 6
  97
  98struct byt_rt5640_private {
  99	struct snd_soc_jack jack;
 100	struct snd_soc_jack jack2;
 101	struct rt5640_set_jack_data jack_data;
 102	struct gpio_desc *hsmic_detect;
 103	struct clk *mclk;
 104	struct device *codec_dev;
 105};
 106static bool is_bytcr;
 107
 108static unsigned long byt_rt5640_quirk = BYT_RT5640_MCLK_EN;
 109static int quirk_override = -1;
 110module_param_named(quirk, quirk_override, int, 0444);
 111MODULE_PARM_DESC(quirk, "Board-specific quirk override");
 112
 113static void log_quirks(struct device *dev)
 114{
 115	int map;
 116	bool has_mclk = false;
 117	bool has_ssp0 = false;
 118	bool has_ssp0_aif1 = false;
 119	bool has_ssp0_aif2 = false;
 120	bool has_ssp2_aif2 = false;
 121
 122	map = BYT_RT5640_MAP(byt_rt5640_quirk);
 123	switch (map) {
 124	case BYT_RT5640_DMIC1_MAP:
 125		dev_info(dev, "quirk DMIC1_MAP enabled\n");
 126		break;
 127	case BYT_RT5640_DMIC2_MAP:
 128		dev_info(dev, "quirk DMIC2_MAP enabled\n");
 129		break;
 130	case BYT_RT5640_IN1_MAP:
 131		dev_info(dev, "quirk IN1_MAP enabled\n");
 132		break;
 133	case BYT_RT5640_IN3_MAP:
 134		dev_info(dev, "quirk IN3_MAP enabled\n");
 135		break;
 136	case BYT_RT5640_NO_INTERNAL_MIC_MAP:
 137		dev_info(dev, "quirk NO_INTERNAL_MIC_MAP enabled\n");
 138		break;
 139	default:
 140		dev_err(dev, "quirk map 0x%x is not supported, microphone input will not work\n", map);
 141		break;
 142	}
 143	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1)
 144		dev_info(dev, "quirk HSMIC2_ON_IN1 enabled\n");
 145	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
 146		dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
 147			 BYT_RT5640_JDSRC(byt_rt5640_quirk));
 148		dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
 149			 BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
 150		dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
 151			 BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
 152	}
 153	if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
 154		dev_info(dev, "quirk JD_NOT_INV enabled\n");
 155	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
 156		dev_info(dev, "quirk JD_HP_ELITEPAD_1000G2 enabled\n");
 157	if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER)
 158		dev_info(dev, "quirk MONO_SPEAKER enabled\n");
 159	if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)
 160		dev_info(dev, "quirk NO_SPEAKERS enabled\n");
 161	if (byt_rt5640_quirk & BYT_RT5640_SWAPPED_SPEAKERS)
 162		dev_info(dev, "quirk SWAPPED_SPEAKERS enabled\n");
 163	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT)
 164		dev_info(dev, "quirk LINEOUT enabled\n");
 165	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2)
 166		dev_info(dev, "quirk LINEOUT_AS_HP2 enabled\n");
 167	if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
 168		dev_info(dev, "quirk DIFF_MIC enabled\n");
 169	if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
 170		dev_info(dev, "quirk SSP0_AIF1 enabled\n");
 171		has_ssp0 = true;
 172		has_ssp0_aif1 = true;
 173	}
 174	if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
 175		dev_info(dev, "quirk SSP0_AIF2 enabled\n");
 176		has_ssp0 = true;
 177		has_ssp0_aif2 = true;
 178	}
 179	if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
 180		dev_info(dev, "quirk SSP2_AIF2 enabled\n");
 181		has_ssp2_aif2 = true;
 182	}
 183	if (is_bytcr && !has_ssp0)
 184		dev_err(dev, "Invalid routing, bytcr detected but no SSP0-based quirk, audio cannot work with SSP2 on bytcr\n");
 185	if (has_ssp0_aif1 && has_ssp0_aif2)
 186		dev_err(dev, "Invalid routing, SSP0 cannot be connected to both AIF1 and AIF2\n");
 187	if (has_ssp0 && has_ssp2_aif2)
 188		dev_err(dev, "Invalid routing, cannot have both SSP0 and SSP2 connected to codec\n");
 189
 190	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
 191		dev_info(dev, "quirk MCLK_EN enabled\n");
 192		has_mclk = true;
 193	}
 194	if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
 195		if (has_mclk)
 196			dev_info(dev, "quirk MCLK_25MHZ enabled\n");
 197		else
 198			dev_err(dev, "quirk MCLK_25MHZ enabled but quirk MCLK not selected, will be ignored\n");
 199	}
 200}
 201
 202static int byt_rt5640_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
 203					      int rate)
 204{
 205	int ret;
 206
 207	/* Configure the PLL before selecting it */
 208	if (!(byt_rt5640_quirk & BYT_RT5640_MCLK_EN)) {
 209		/* use bitclock as PLL input */
 210		if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
 211		    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
 212			/* 2x16 bit slots on SSP0 */
 213			ret = snd_soc_dai_set_pll(codec_dai, 0,
 214						  RT5640_PLL1_S_BCLK1,
 215						  rate * 32, rate * 512);
 216		} else {
 217			/* 2x15 bit slots on SSP2 */
 218			ret = snd_soc_dai_set_pll(codec_dai, 0,
 219						  RT5640_PLL1_S_BCLK1,
 220						  rate * 50, rate * 512);
 221		}
 222	} else {
 223		if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
 224			ret = snd_soc_dai_set_pll(codec_dai, 0,
 225						  RT5640_PLL1_S_MCLK,
 226						  25000000, rate * 512);
 227		} else {
 228			ret = snd_soc_dai_set_pll(codec_dai, 0,
 229						  RT5640_PLL1_S_MCLK,
 230						  19200000, rate * 512);
 231		}
 232	}
 233
 234	if (ret < 0) {
 235		dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
 236		return ret;
 237	}
 238
 239	ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
 240				     rate * 512, SND_SOC_CLOCK_IN);
 241	if (ret < 0) {
 242		dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
 243		return ret;
 244	}
 245
 246	return 0;
 247}
 248
 249#define BYT_CODEC_DAI1	"rt5640-aif1"
 250#define BYT_CODEC_DAI2	"rt5640-aif2"
 251
 252static struct snd_soc_dai *byt_rt5640_get_codec_dai(struct snd_soc_dapm_context *dapm)
 253{
 254	struct snd_soc_card *card = dapm->card;
 255	struct snd_soc_dai *codec_dai;
 256
 257	codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
 258	if (!codec_dai)
 259		codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
 260	if (!codec_dai)
 261		dev_err(card->dev, "Error codec dai not found\n");
 262
 263	return codec_dai;
 264}
 265
 266static int platform_clock_control(struct snd_soc_dapm_widget *w,
 267				  struct snd_kcontrol *k, int  event)
 268{
 269	struct snd_soc_dapm_context *dapm = w->dapm;
 270	struct snd_soc_card *card = dapm->card;
 271	struct snd_soc_dai *codec_dai;
 272	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
 273	int ret;
 274
 275	codec_dai = byt_rt5640_get_codec_dai(dapm);
 276	if (!codec_dai)
 277		return -EIO;
 278
 279	if (SND_SOC_DAPM_EVENT_ON(event)) {
 280		ret = clk_prepare_enable(priv->mclk);
 281		if (ret < 0) {
 282			dev_err(card->dev, "could not configure MCLK state\n");
 283			return ret;
 284		}
 285		ret = byt_rt5640_prepare_and_enable_pll1(codec_dai, 48000);
 286	} else {
 287		/*
 288		 * Set codec clock source to internal clock before
 289		 * turning off the platform clock. Codec needs clock
 290		 * for Jack detection and button press
 291		 */
 292		ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_RCCLK,
 293					     48000 * 512,
 294					     SND_SOC_CLOCK_IN);
 295		if (!ret)
 296			clk_disable_unprepare(priv->mclk);
 297	}
 298
 299	if (ret < 0) {
 300		dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
 301		return ret;
 302	}
 303
 304	return 0;
 305}
 306
 307static int byt_rt5640_event_lineout(struct snd_soc_dapm_widget *w,
 308			struct snd_kcontrol *k, int event)
 309{
 310	unsigned int gpio_ctrl3_val = RT5640_GP1_PF_OUT;
 311	struct snd_soc_dai *codec_dai;
 312
 313	if (!(byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2))
 314		return 0;
 315
 316	/*
 317	 * On devices which use line-out as a second headphones output,
 318	 * the codec's GPIO1 pin is used to enable an external HP-amp.
 319	 */
 320
 321	codec_dai = byt_rt5640_get_codec_dai(w->dapm);
 322	if (!codec_dai)
 323		return -EIO;
 324
 325	if (SND_SOC_DAPM_EVENT_ON(event))
 326		gpio_ctrl3_val |= RT5640_GP1_OUT_HI;
 327
 328	snd_soc_component_update_bits(codec_dai->component, RT5640_GPIO_CTRL3,
 329		RT5640_GP1_PF_MASK | RT5640_GP1_OUT_MASK, gpio_ctrl3_val);
 330
 331	return 0;
 332}
 333
 334static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
 335	SND_SOC_DAPM_HP("Headphone", NULL),
 336	SND_SOC_DAPM_MIC("Headset Mic", NULL),
 337	SND_SOC_DAPM_MIC("Headset Mic 2", NULL),
 338	SND_SOC_DAPM_MIC("Internal Mic", NULL),
 339	SND_SOC_DAPM_SPK("Speaker", NULL),
 340	SND_SOC_DAPM_LINE("Line Out", byt_rt5640_event_lineout),
 341	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
 342			    platform_clock_control, SND_SOC_DAPM_PRE_PMU |
 343			    SND_SOC_DAPM_POST_PMD),
 344};
 345
 346static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
 347	{"Headphone", NULL, "Platform Clock"},
 348	{"Headset Mic", NULL, "Platform Clock"},
 349	{"Headset Mic", NULL, "MICBIAS1"},
 350	{"IN2P", NULL, "Headset Mic"},
 351	{"Headphone", NULL, "HPOL"},
 352	{"Headphone", NULL, "HPOR"},
 353};
 354
 355static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
 356	{"Internal Mic", NULL, "Platform Clock"},
 357	{"DMIC1", NULL, "Internal Mic"},
 358};
 359
 360static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
 361	{"Internal Mic", NULL, "Platform Clock"},
 362	{"DMIC2", NULL, "Internal Mic"},
 363};
 364
 365static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
 366	{"Internal Mic", NULL, "Platform Clock"},
 367	{"Internal Mic", NULL, "MICBIAS1"},
 368	{"IN1P", NULL, "Internal Mic"},
 369};
 370
 371static const struct snd_soc_dapm_route byt_rt5640_intmic_in3_map[] = {
 372	{"Internal Mic", NULL, "Platform Clock"},
 373	{"Internal Mic", NULL, "MICBIAS1"},
 374	{"IN3P", NULL, "Internal Mic"},
 375};
 376
 377static const struct snd_soc_dapm_route byt_rt5640_hsmic2_in1_map[] = {
 378	{"Headset Mic 2", NULL, "Platform Clock"},
 379	{"Headset Mic 2", NULL, "MICBIAS1"},
 380	{"IN1P", NULL, "Headset Mic 2"},
 381};
 382
 383static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif1_map[] = {
 384	{"ssp2 Tx", NULL, "codec_out0"},
 385	{"ssp2 Tx", NULL, "codec_out1"},
 386	{"codec_in0", NULL, "ssp2 Rx"},
 387	{"codec_in1", NULL, "ssp2 Rx"},
 388
 389	{"AIF1 Playback", NULL, "ssp2 Tx"},
 390	{"ssp2 Rx", NULL, "AIF1 Capture"},
 391};
 392
 393static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif2_map[] = {
 394	{"ssp2 Tx", NULL, "codec_out0"},
 395	{"ssp2 Tx", NULL, "codec_out1"},
 396	{"codec_in0", NULL, "ssp2 Rx"},
 397	{"codec_in1", NULL, "ssp2 Rx"},
 398
 399	{"AIF2 Playback", NULL, "ssp2 Tx"},
 400	{"ssp2 Rx", NULL, "AIF2 Capture"},
 401};
 402
 403static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif1_map[] = {
 404	{"ssp0 Tx", NULL, "modem_out"},
 405	{"modem_in", NULL, "ssp0 Rx"},
 406
 407	{"AIF1 Playback", NULL, "ssp0 Tx"},
 408	{"ssp0 Rx", NULL, "AIF1 Capture"},
 409};
 410
 411static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif2_map[] = {
 412	{"ssp0 Tx", NULL, "modem_out"},
 413	{"modem_in", NULL, "ssp0 Rx"},
 414
 415	{"AIF2 Playback", NULL, "ssp0 Tx"},
 416	{"ssp0 Rx", NULL, "AIF2 Capture"},
 417};
 418
 419static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = {
 420	{"Speaker", NULL, "Platform Clock"},
 421	{"Speaker", NULL, "SPOLP"},
 422	{"Speaker", NULL, "SPOLN"},
 423	{"Speaker", NULL, "SPORP"},
 424	{"Speaker", NULL, "SPORN"},
 425};
 426
 427static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = {
 428	{"Speaker", NULL, "Platform Clock"},
 429	{"Speaker", NULL, "SPOLP"},
 430	{"Speaker", NULL, "SPOLN"},
 431};
 432
 433static const struct snd_soc_dapm_route byt_rt5640_lineout_map[] = {
 434	{"Line Out", NULL, "Platform Clock"},
 435	{"Line Out", NULL, "LOUTR"},
 436	{"Line Out", NULL, "LOUTL"},
 437};
 438
 439static const struct snd_kcontrol_new byt_rt5640_controls[] = {
 440	SOC_DAPM_PIN_SWITCH("Headphone"),
 441	SOC_DAPM_PIN_SWITCH("Headset Mic"),
 442	SOC_DAPM_PIN_SWITCH("Headset Mic 2"),
 443	SOC_DAPM_PIN_SWITCH("Internal Mic"),
 444	SOC_DAPM_PIN_SWITCH("Speaker"),
 445	SOC_DAPM_PIN_SWITCH("Line Out"),
 446};
 447
 448static struct snd_soc_jack_pin rt5640_pins[] = {
 449	{
 450		.pin	= "Headphone",
 451		.mask	= SND_JACK_HEADPHONE,
 452	},
 453	{
 454		.pin	= "Headset Mic",
 455		.mask	= SND_JACK_MICROPHONE,
 456	},
 457};
 458
 459static struct snd_soc_jack_pin rt5640_pins2[] = {
 460	{
 461		/* The 2nd headset jack uses lineout with an external HP-amp */
 462		.pin	= "Line Out",
 463		.mask	= SND_JACK_HEADPHONE,
 464	},
 465	{
 466		.pin	= "Headset Mic 2",
 467		.mask	= SND_JACK_MICROPHONE,
 468	},
 469};
 470
 471static struct snd_soc_jack_gpio rt5640_jack_gpio = {
 472	.name = "hp-detect",
 473	.report = SND_JACK_HEADSET,
 474	.invert = true,
 475	.debounce_time = 200,
 476};
 477
 478static struct snd_soc_jack_gpio rt5640_jack2_gpio = {
 479	.name = "hp2-detect",
 480	.report = SND_JACK_HEADSET,
 481	.invert = true,
 482	.debounce_time = 200,
 483};
 484
 485static const struct acpi_gpio_params acpi_gpio0 = { 0, 0, false };
 486static const struct acpi_gpio_params acpi_gpio1 = { 1, 0, false };
 487static const struct acpi_gpio_params acpi_gpio2 = { 2, 0, false };
 488
 489static const struct acpi_gpio_mapping byt_rt5640_hp_elitepad_1000g2_gpios[] = {
 490	{ "hp-detect-gpios", &acpi_gpio0, 1, },
 491	{ "headset-mic-detect-gpios", &acpi_gpio1, 1, },
 492	{ "hp2-detect-gpios", &acpi_gpio2, 1, },
 493	{ },
 494};
 495
 496static int byt_rt5640_hp_elitepad_1000g2_jack1_check(void *data)
 497{
 498	struct byt_rt5640_private *priv = data;
 499	int jack_status, mic_status;
 500
 501	jack_status = gpiod_get_value_cansleep(rt5640_jack_gpio.desc);
 502	if (jack_status)
 503		return 0;
 504
 505	mic_status = gpiod_get_value_cansleep(priv->hsmic_detect);
 506	if (mic_status)
 507		return SND_JACK_HEADPHONE;
 508	else
 509		return SND_JACK_HEADSET;
 510}
 511
 512static int byt_rt5640_hp_elitepad_1000g2_jack2_check(void *data)
 513{
 514	struct snd_soc_component *component = data;
 515	int jack_status, report;
 516
 517	jack_status = gpiod_get_value_cansleep(rt5640_jack2_gpio.desc);
 518	if (jack_status)
 519		return 0;
 520
 521	rt5640_enable_micbias1_for_ovcd(component);
 522	report = rt5640_detect_headset(component, rt5640_jack2_gpio.desc);
 523	rt5640_disable_micbias1_for_ovcd(component);
 524
 525	return report;
 526}
 527
 528static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
 529					struct snd_pcm_hw_params *params)
 530{
 531	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 532	struct snd_soc_dai *dai = snd_soc_rtd_to_codec(rtd, 0);
 533
 534	return byt_rt5640_prepare_and_enable_pll1(dai, params_rate(params));
 535}
 536
 537/* Please keep this list alphabetically sorted */
 538static const struct dmi_system_id byt_rt5640_quirk_table[] = {
 539	{	/* Acer Iconia One 7 B1-750 */
 540		.matches = {
 541			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
 542			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "VESPA2"),
 543		},
 544		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 545					BYT_RT5640_JD_SRC_JD1_IN4P |
 546					BYT_RT5640_OVCD_TH_1500UA |
 547					BYT_RT5640_OVCD_SF_0P75 |
 548					BYT_RT5640_SSP0_AIF1 |
 549					BYT_RT5640_MCLK_EN),
 550	},
 551	{	/* Acer Iconia Tab 8 W1-810 */
 552		.matches = {
 553			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Acer"),
 554			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Iconia W1-810"),
 555		},
 556		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 557					BYT_RT5640_JD_SRC_JD1_IN4P |
 558					BYT_RT5640_OVCD_TH_1500UA |
 559					BYT_RT5640_OVCD_SF_0P75 |
 560					BYT_RT5640_SSP0_AIF1 |
 561					BYT_RT5640_MCLK_EN),
 562	},
 563	{	/* Acer One 10 S1002 */
 564		.matches = {
 565			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
 566			DMI_MATCH(DMI_PRODUCT_NAME, "One S1002"),
 567		},
 568		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 569					BYT_RT5640_JD_SRC_JD2_IN4N |
 570					BYT_RT5640_OVCD_TH_2000UA |
 571					BYT_RT5640_OVCD_SF_0P75 |
 572					BYT_RT5640_DIFF_MIC |
 573					BYT_RT5640_SSP0_AIF2 |
 574					BYT_RT5640_MCLK_EN),
 575	},
 576	{
 577		.matches = {
 578			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
 579			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
 580		},
 581		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 582					BYT_RT5640_JD_SRC_JD2_IN4N |
 583					BYT_RT5640_OVCD_TH_2000UA |
 584					BYT_RT5640_OVCD_SF_0P75 |
 585					BYT_RT5640_SSP0_AIF1 |
 586					BYT_RT5640_MCLK_EN),
 587	},
 588	{
 589		/* Advantech MICA-071 */
 590		.matches = {
 591			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Advantech"),
 592			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MICA-071"),
 593		},
 594		/* OVCD Th = 1500uA to reliable detect head-phones vs -set */
 595		.driver_data = (void *)(BYT_RT5640_IN3_MAP |
 596					BYT_RT5640_JD_SRC_JD2_IN4N |
 597					BYT_RT5640_OVCD_TH_1500UA |
 598					BYT_RT5640_OVCD_SF_0P75 |
 599					BYT_RT5640_MONO_SPEAKER |
 600					BYT_RT5640_DIFF_MIC |
 601					BYT_RT5640_MCLK_EN),
 602	},
 603	{
 604		.matches = {
 605			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
 606			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 80 Cesium"),
 607		},
 608		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 609					BYT_RT5640_MONO_SPEAKER |
 610					BYT_RT5640_SSP0_AIF1 |
 611					BYT_RT5640_MCLK_EN),
 612	},
 613	{
 614		.matches = {
 615			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
 
 
 
 
 
 
 
 
 
 
 
 616			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 140 CESIUM"),
 617		},
 618		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 619					BYT_RT5640_JD_SRC_JD2_IN4N |
 620					BYT_RT5640_OVCD_TH_2000UA |
 621					BYT_RT5640_OVCD_SF_0P75 |
 622					BYT_RT5640_SSP0_AIF1 |
 623					BYT_RT5640_MCLK_EN),
 624	},
 625	{
 626		.matches = {
 627			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
 628			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ME176C"),
 629		},
 630		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 631					BYT_RT5640_JD_SRC_JD2_IN4N |
 632					BYT_RT5640_OVCD_TH_2000UA |
 633					BYT_RT5640_OVCD_SF_0P75 |
 634					BYT_RT5640_SSP0_AIF1 |
 635					BYT_RT5640_MCLK_EN |
 636					BYT_RT5640_USE_AMCR0F28),
 637	},
 638	{
 
 639		.matches = {
 640			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
 641			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
 642		},
 643		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 644					BYT_RT5640_JD_SRC_JD2_IN4N |
 645					BYT_RT5640_OVCD_TH_2000UA |
 646					BYT_RT5640_OVCD_SF_0P75 |
 
 
 
 647					BYT_RT5640_MCLK_EN),
 648	},
 649	{
 
 650		.matches = {
 651			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
 652			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
 653		},
 654		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 655					BYT_RT5640_JD_SRC_JD2_IN4N |
 656					BYT_RT5640_OVCD_TH_2000UA |
 657					BYT_RT5640_OVCD_SF_0P75 |
 658					BYT_RT5640_MONO_SPEAKER |
 659					BYT_RT5640_DIFF_MIC |
 660					BYT_RT5640_SSP0_AIF2 |
 661					BYT_RT5640_MCLK_EN),
 662	},
 663	{
 664		.matches = {
 665			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
 666			DMI_MATCH(DMI_PRODUCT_NAME, "TF103C"),
 667		},
 668		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 669					BYT_RT5640_JD_SRC_EXT_GPIO |
 670					BYT_RT5640_OVCD_TH_2000UA |
 671					BYT_RT5640_OVCD_SF_0P75 |
 672					BYT_RT5640_SSP0_AIF1 |
 673					BYT_RT5640_MCLK_EN |
 674					BYT_RT5640_USE_AMCR0F28),
 675	},
 676	{	/* Chuwi Vi8 (CWI506) */
 677		.matches = {
 678			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
 679			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "i86"),
 680			/* The above are too generic, also match BIOS info */
 681			DMI_MATCH(DMI_BIOS_VERSION, "CHUWI.D86JLBNR"),
 682		},
 683		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 684					BYT_RT5640_MONO_SPEAKER |
 685					BYT_RT5640_SSP0_AIF1 |
 686					BYT_RT5640_MCLK_EN),
 687	},
 688	{	/* Chuwi Vi8 dual-boot (CWI506) */
 689		.matches = {
 690			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
 691			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "i86"),
 692			/* The above are too generic, also match BIOS info */
 693			DMI_MATCH(DMI_BIOS_VERSION, "CHUWI2.D86JHBNR02"),
 694		},
 695		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 696					BYT_RT5640_MONO_SPEAKER |
 697					BYT_RT5640_SSP0_AIF1 |
 698					BYT_RT5640_MCLK_EN),
 699	},
 700	{
 701		/* Chuwi Vi10 (CWI505) */
 702		.matches = {
 703			DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
 704			DMI_MATCH(DMI_BOARD_NAME, "BYT-PF02"),
 705			DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
 706			DMI_MATCH(DMI_PRODUCT_NAME, "S165"),
 707		},
 708		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 709					BYT_RT5640_JD_SRC_JD2_IN4N |
 710					BYT_RT5640_OVCD_TH_2000UA |
 711					BYT_RT5640_OVCD_SF_0P75 |
 712					BYT_RT5640_DIFF_MIC |
 713					BYT_RT5640_SSP0_AIF1 |
 714					BYT_RT5640_MCLK_EN),
 715	},
 716	{
 717		/* Chuwi Hi8 (CWI509) */
 718		.matches = {
 719			DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
 720			DMI_MATCH(DMI_BOARD_NAME, "BYT-PA03C"),
 721			DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
 722			DMI_MATCH(DMI_PRODUCT_NAME, "S806"),
 723		},
 724		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 725					BYT_RT5640_JD_SRC_JD2_IN4N |
 726					BYT_RT5640_OVCD_TH_2000UA |
 727					BYT_RT5640_OVCD_SF_0P75 |
 728					BYT_RT5640_MONO_SPEAKER |
 729					BYT_RT5640_DIFF_MIC |
 730					BYT_RT5640_SSP0_AIF1 |
 731					BYT_RT5640_MCLK_EN),
 732	},
 733	{
 734		.matches = {
 735			DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
 736			DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
 737		},
 738		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP),
 739	},
 740	{	/* Connect Tablet 9 */
 741		.matches = {
 742			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Connect"),
 743			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Tablet 9"),
 744		},
 745		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 746					BYT_RT5640_MONO_SPEAKER |
 747					BYT_RT5640_SSP0_AIF1 |
 748					BYT_RT5640_MCLK_EN),
 749	},
 750	{
 751		.matches = {
 752			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 753			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
 754		},
 755		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 756					BYT_RT5640_JD_SRC_JD2_IN4N |
 757					BYT_RT5640_OVCD_TH_2000UA |
 758					BYT_RT5640_OVCD_SF_0P75 |
 759					BYT_RT5640_MONO_SPEAKER |
 760					BYT_RT5640_MCLK_EN),
 761	},
 762	{	/* Estar Beauty HD MID 7316R */
 763		.matches = {
 764			DMI_MATCH(DMI_SYS_VENDOR, "Estar"),
 765			DMI_MATCH(DMI_PRODUCT_NAME, "eSTAR BEAUTY HD Intel Quad core"),
 766		},
 767		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 768					BYT_RT5640_MONO_SPEAKER |
 769					BYT_RT5640_SSP0_AIF1 |
 770					BYT_RT5640_MCLK_EN),
 771	},
 772	{	/* Glavey TM800A550L */
 773		.matches = {
 774			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
 775			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
 776			/* Above strings are too generic, also match on BIOS version */
 777			DMI_MATCH(DMI_BIOS_VERSION, "ZY-8-BI-PX4S70VTR400-X423B-005-D"),
 778		},
 779		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 780					BYT_RT5640_SSP0_AIF1 |
 781					BYT_RT5640_MCLK_EN),
 782	},
 783	{
 784		.matches = {
 785			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 786			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
 787		},
 788		.driver_data = (void *)(BYT_RT5640_DMIC2_MAP |
 789					BYT_RT5640_MCLK_EN |
 790					BYT_RT5640_LINEOUT |
 791					BYT_RT5640_LINEOUT_AS_HP2 |
 792					BYT_RT5640_HSMIC2_ON_IN1 |
 793					BYT_RT5640_JD_HP_ELITEP_1000G2),
 794	},
 795	{	/* HP Pavilion x2 10-k0XX, 10-n0XX */
 796		.matches = {
 797			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 798			DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
 799		},
 800		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 801					BYT_RT5640_JD_SRC_JD2_IN4N |
 802					BYT_RT5640_OVCD_TH_1500UA |
 803					BYT_RT5640_OVCD_SF_0P75 |
 804					BYT_RT5640_SSP0_AIF1 |
 805					BYT_RT5640_MCLK_EN),
 806	},
 807	{	/* HP Pavilion x2 10-p0XX */
 808		.matches = {
 809			DMI_MATCH(DMI_SYS_VENDOR, "HP"),
 810			DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
 811		},
 812		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 813					BYT_RT5640_JD_SRC_JD1_IN4P |
 814					BYT_RT5640_OVCD_TH_2000UA |
 815					BYT_RT5640_OVCD_SF_0P75 |
 816					BYT_RT5640_MCLK_EN),
 817	},
 818	{	/* HP Pro Tablet 408 */
 819		.matches = {
 820			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 821			DMI_MATCH(DMI_PRODUCT_NAME, "HP Pro Tablet 408"),
 822		},
 823		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 824					BYT_RT5640_JD_SRC_JD2_IN4N |
 825					BYT_RT5640_OVCD_TH_1500UA |
 826					BYT_RT5640_OVCD_SF_0P75 |
 827					BYT_RT5640_SSP0_AIF1 |
 828					BYT_RT5640_MCLK_EN),
 829	},
 830	{	/* HP Stream 7 */
 831		.matches = {
 832			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 833			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 7 Tablet"),
 834		},
 835		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 836					BYT_RT5640_MONO_SPEAKER |
 837					BYT_RT5640_JD_NOT_INV |
 838					BYT_RT5640_SSP0_AIF1 |
 839					BYT_RT5640_MCLK_EN),
 840	},
 841	{	/* HP Stream 8 */
 842		.matches = {
 843			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 844			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 8 Tablet"),
 845		},
 846		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 847					BYT_RT5640_JD_NOT_INV |
 848					BYT_RT5640_SSP0_AIF1 |
 849					BYT_RT5640_MCLK_EN),
 850	},
 851	{	/* I.T.Works TW891 */
 852		.matches = {
 853			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "To be filled by O.E.M."),
 854			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TW891"),
 855			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
 856			DMI_EXACT_MATCH(DMI_BOARD_NAME, "TW891"),
 857		},
 858		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 859					BYT_RT5640_MONO_SPEAKER |
 860					BYT_RT5640_SSP0_AIF1 |
 861					BYT_RT5640_MCLK_EN),
 862	},
 863	{	/* Lamina I8270 / T701BR.SE */
 864		.matches = {
 865			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Lamina"),
 866			DMI_EXACT_MATCH(DMI_BOARD_NAME, "T701BR.SE"),
 867		},
 868		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 869					BYT_RT5640_MONO_SPEAKER |
 870					BYT_RT5640_JD_NOT_INV |
 871					BYT_RT5640_SSP0_AIF1 |
 872					BYT_RT5640_MCLK_EN),
 873	},
 874	{	/* Lenovo Miix 2 8 */
 875		.matches = {
 876			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 877			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "20326"),
 878			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Hiking"),
 879		},
 880		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
 881					BYT_RT5640_JD_SRC_JD2_IN4N |
 882					BYT_RT5640_OVCD_TH_2000UA |
 883					BYT_RT5640_OVCD_SF_0P75 |
 884					BYT_RT5640_MONO_SPEAKER |
 885					BYT_RT5640_MCLK_EN),
 886	},
 887	{	/* Lenovo Miix 3-830 */
 888		.matches = {
 889			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 890			DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 3-830"),
 891		},
 892		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 893					BYT_RT5640_JD_SRC_JD2_IN4N |
 894					BYT_RT5640_OVCD_TH_2000UA |
 895					BYT_RT5640_OVCD_SF_0P75 |
 896					BYT_RT5640_MONO_SPEAKER |
 897					BYT_RT5640_DIFF_MIC |
 898					BYT_RT5640_SSP0_AIF1 |
 899					BYT_RT5640_MCLK_EN),
 900	},
 901	{	/* Linx Linx7 tablet */
 902		.matches = {
 903			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LINX"),
 904			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "LINX7"),
 905		},
 906		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 907					BYT_RT5640_MONO_SPEAKER |
 908					BYT_RT5640_JD_NOT_INV |
 909					BYT_RT5640_SSP0_AIF1 |
 910					BYT_RT5640_MCLK_EN),
 911	},
 912	{
 913		/* Medion Lifetab S10346 */
 914		.matches = {
 915			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
 916			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
 917			/* Above strings are much too generic, also match on BIOS date */
 918			DMI_MATCH(DMI_BIOS_DATE, "10/22/2015"),
 919		},
 920		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 921					BYT_RT5640_SWAPPED_SPEAKERS |
 922					BYT_RT5640_SSP0_AIF1 |
 923					BYT_RT5640_MCLK_EN),
 924	},
 925	{	/* Mele PCG03 Mini PC */
 926		.matches = {
 927			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"),
 928			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"),
 929		},
 930		.driver_data = (void *)(BYT_RT5640_NO_INTERNAL_MIC_MAP |
 931					BYT_RT5640_NO_SPEAKERS |
 932					BYT_RT5640_SSP0_AIF1),
 933	},
 934	{	/* MPMAN Converter 9, similar hw as the I.T.Works TW891 2-in-1 */
 935		.matches = {
 936			DMI_MATCH(DMI_SYS_VENDOR, "MPMAN"),
 937			DMI_MATCH(DMI_PRODUCT_NAME, "Converter9"),
 938		},
 939		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 940					BYT_RT5640_MONO_SPEAKER |
 941					BYT_RT5640_SSP0_AIF1 |
 942					BYT_RT5640_MCLK_EN),
 943	},
 944	{
 945		/* MPMAN MPWIN895CL */
 946		.matches = {
 947			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MPMAN"),
 948			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MPWIN8900CL"),
 949		},
 950		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
 951					BYT_RT5640_MONO_SPEAKER |
 952					BYT_RT5640_SSP0_AIF1 |
 953					BYT_RT5640_MCLK_EN),
 954	},
 955	{	/* MSI S100 tablet */
 956		.matches = {
 957			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Micro-Star International Co., Ltd."),
 958			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "S100"),
 959		},
 960		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 961					BYT_RT5640_JD_SRC_JD2_IN4N |
 962					BYT_RT5640_OVCD_TH_2000UA |
 963					BYT_RT5640_OVCD_SF_0P75 |
 964					BYT_RT5640_MONO_SPEAKER |
 965					BYT_RT5640_DIFF_MIC |
 966					BYT_RT5640_MCLK_EN),
 967	},
 968	{	/* Nuvison/TMax TM800W560 */
 969		.matches = {
 970			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TMAX"),
 971			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TM800W560L"),
 972		},
 973		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 974					BYT_RT5640_JD_SRC_JD2_IN4N |
 975					BYT_RT5640_OVCD_TH_2000UA |
 976					BYT_RT5640_OVCD_SF_0P75 |
 977					BYT_RT5640_JD_NOT_INV |
 978					BYT_RT5640_DIFF_MIC |
 979					BYT_RT5640_SSP0_AIF1 |
 980					BYT_RT5640_MCLK_EN),
 981	},
 982	{	/* Onda v975w */
 983		.matches = {
 984			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
 985			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
 986			/* The above are too generic, also match BIOS info */
 987			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "5.6.5"),
 988			DMI_EXACT_MATCH(DMI_BIOS_DATE, "07/25/2014"),
 989		},
 990		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
 991					BYT_RT5640_JD_SRC_JD2_IN4N |
 992					BYT_RT5640_OVCD_TH_2000UA |
 993					BYT_RT5640_OVCD_SF_0P75 |
 994					BYT_RT5640_DIFF_MIC |
 995					BYT_RT5640_MCLK_EN),
 996	},
 997	{	/* Pipo W4 */
 998		.matches = {
 999			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1000			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1001			/* The above are too generic, also match BIOS info */
1002			DMI_MATCH(DMI_BIOS_VERSION, "V8L_WIN32_CHIPHD"),
1003		},
1004		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
1005					BYT_RT5640_MONO_SPEAKER |
1006					BYT_RT5640_SSP0_AIF1 |
1007					BYT_RT5640_MCLK_EN),
1008	},
1009	{	/* Point of View Mobii TAB-P800W (V2.0) */
1010		.matches = {
1011			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1012			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1013			/* The above are too generic, also match BIOS info */
1014			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1014"),
1015			DMI_EXACT_MATCH(DMI_BIOS_DATE, "10/24/2014"),
1016		},
1017		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1018					BYT_RT5640_JD_SRC_JD2_IN4N |
1019					BYT_RT5640_OVCD_TH_2000UA |
1020					BYT_RT5640_OVCD_SF_0P75 |
1021					BYT_RT5640_MONO_SPEAKER |
1022					BYT_RT5640_DIFF_MIC |
1023					BYT_RT5640_SSP0_AIF2 |
1024					BYT_RT5640_MCLK_EN),
1025	},
1026	{	/* Point of View Mobii TAB-P800W (V2.1) */
1027		.matches = {
1028			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1029			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1030			/* The above are too generic, also match BIOS info */
1031			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1032			DMI_EXACT_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1033		},
1034		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1035					BYT_RT5640_JD_SRC_JD2_IN4N |
1036					BYT_RT5640_OVCD_TH_2000UA |
1037					BYT_RT5640_OVCD_SF_0P75 |
1038					BYT_RT5640_MONO_SPEAKER |
1039					BYT_RT5640_DIFF_MIC |
1040					BYT_RT5640_SSP0_AIF2 |
1041					BYT_RT5640_MCLK_EN),
1042	},
1043	{	/* Point of View Mobii TAB-P1005W-232 (V2.0) */
1044		.matches = {
1045			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "POV"),
1046			DMI_EXACT_MATCH(DMI_BOARD_NAME, "I102A"),
1047		},
1048		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1049					BYT_RT5640_JD_SRC_JD2_IN4N |
1050					BYT_RT5640_OVCD_TH_2000UA |
1051					BYT_RT5640_OVCD_SF_0P75 |
1052					BYT_RT5640_DIFF_MIC |
1053					BYT_RT5640_SSP0_AIF1 |
1054					BYT_RT5640_MCLK_EN),
1055	},
1056	{
1057		/* Prowise PT301 */
1058		.matches = {
1059			DMI_MATCH(DMI_SYS_VENDOR, "Prowise"),
1060			DMI_MATCH(DMI_PRODUCT_NAME, "PT301"),
1061		},
1062		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1063					BYT_RT5640_JD_SRC_JD2_IN4N |
1064					BYT_RT5640_OVCD_TH_2000UA |
1065					BYT_RT5640_OVCD_SF_0P75 |
1066					BYT_RT5640_DIFF_MIC |
1067					BYT_RT5640_SSP0_AIF1 |
1068					BYT_RT5640_MCLK_EN),
1069	},
1070	{
1071		/* Teclast X89 */
1072		.matches = {
1073			DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
1074			DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
1075		},
1076		.driver_data = (void *)(BYT_RT5640_IN3_MAP |
1077					BYT_RT5640_JD_SRC_JD1_IN4P |
1078					BYT_RT5640_OVCD_TH_2000UA |
1079					BYT_RT5640_OVCD_SF_1P0 |
1080					BYT_RT5640_SSP0_AIF1 |
1081					BYT_RT5640_MCLK_EN),
1082	},
1083	{	/* Toshiba Satellite Click Mini L9W-B */
1084		.matches = {
1085			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1086			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
1087		},
1088		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1089					BYT_RT5640_JD_SRC_JD2_IN4N |
1090					BYT_RT5640_OVCD_TH_1500UA |
1091					BYT_RT5640_OVCD_SF_0P75 |
1092					BYT_RT5640_SSP0_AIF1 |
1093					BYT_RT5640_MCLK_EN),
1094	},
1095	{	/* Toshiba Encore WT8-A */
1096		.matches = {
1097			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1098			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT8-A"),
1099		},
1100		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1101					BYT_RT5640_JD_SRC_JD2_IN4N |
1102					BYT_RT5640_OVCD_TH_2000UA |
1103					BYT_RT5640_OVCD_SF_0P75 |
1104					BYT_RT5640_JD_NOT_INV |
1105					BYT_RT5640_MCLK_EN),
1106	},
1107	{	/* Toshiba Encore WT10-A */
1108		.matches = {
1109			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1110			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT10-A-103"),
1111		},
1112		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1113					BYT_RT5640_JD_SRC_JD1_IN4P |
1114					BYT_RT5640_OVCD_TH_2000UA |
1115					BYT_RT5640_OVCD_SF_0P75 |
1116					BYT_RT5640_SSP0_AIF2 |
1117					BYT_RT5640_MCLK_EN),
1118	},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1119	{	/* Voyo Winpad A15 */
1120		.matches = {
1121			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1122			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1123			/* Above strings are too generic, also match on BIOS date */
1124			DMI_MATCH(DMI_BIOS_DATE, "11/20/2014"),
1125		},
1126		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1127					BYT_RT5640_JD_SRC_JD2_IN4N |
1128					BYT_RT5640_OVCD_TH_2000UA |
1129					BYT_RT5640_OVCD_SF_0P75 |
1130					BYT_RT5640_DIFF_MIC |
1131					BYT_RT5640_MCLK_EN),
1132	},
1133	{	/* Catch-all for generic Insyde tablets, must be last */
1134		.matches = {
1135			DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
1136		},
1137		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
1138					BYT_RT5640_MCLK_EN |
1139					BYT_RT5640_SSP0_AIF1),
1140
1141	},
1142	{}
1143};
1144
1145/*
1146 * Note this MUST be called before snd_soc_register_card(), so that the props
1147 * are in place before the codec component driver's probe function parses them.
1148 */
1149static int byt_rt5640_add_codec_device_props(struct device *i2c_dev,
1150					     struct byt_rt5640_private *priv)
1151{
1152	struct property_entry props[MAX_NO_PROPS] = {};
1153	struct fwnode_handle *fwnode;
1154	int cnt = 0;
1155	int ret;
1156
1157	switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1158	case BYT_RT5640_DMIC1_MAP:
1159		props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic1-data-pin",
1160						  RT5640_DMIC1_DATA_PIN_IN1P);
1161		break;
1162	case BYT_RT5640_DMIC2_MAP:
1163		props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic2-data-pin",
1164						  RT5640_DMIC2_DATA_PIN_IN1N);
1165		break;
1166	case BYT_RT5640_IN1_MAP:
1167		if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1168			props[cnt++] =
1169				PROPERTY_ENTRY_BOOL("realtek,in1-differential");
1170		break;
1171	case BYT_RT5640_IN3_MAP:
1172		if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1173			props[cnt++] =
1174				PROPERTY_ENTRY_BOOL("realtek,in3-differential");
1175		break;
1176	}
1177
1178	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1179		if (BYT_RT5640_JDSRC(byt_rt5640_quirk) != RT5640_JD_SRC_EXT_GPIO) {
1180			props[cnt++] = PROPERTY_ENTRY_U32(
1181					    "realtek,jack-detect-source",
1182					    BYT_RT5640_JDSRC(byt_rt5640_quirk));
1183		}
1184
1185		props[cnt++] = PROPERTY_ENTRY_U32(
1186				    "realtek,over-current-threshold-microamp",
1187				    BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
1188
1189		props[cnt++] = PROPERTY_ENTRY_U32(
1190				    "realtek,over-current-scale-factor",
1191				    BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
1192	}
1193
1194	if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
1195		props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
1196
1197	fwnode = fwnode_create_software_node(props, NULL);
1198	if (IS_ERR(fwnode)) {
1199		/* put_device() is handled in caller */
1200		return PTR_ERR(fwnode);
1201	}
1202
1203	ret = device_add_software_node(i2c_dev, to_software_node(fwnode));
1204
1205	fwnode_handle_put(fwnode);
1206
1207	return ret;
1208}
1209
1210/* Some Android devs specify IRQs/GPIOS in a special AMCR0F28 ACPI device */
1211static const struct acpi_gpio_params amcr0f28_jd_gpio = { 1, 0, false };
1212
1213static const struct acpi_gpio_mapping amcr0f28_gpios[] = {
1214	{ "rt5640-jd-gpios", &amcr0f28_jd_gpio, 1 },
1215	{ }
1216};
1217
1218static int byt_rt5640_get_amcr0f28_settings(struct snd_soc_card *card)
1219{
1220	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1221	struct rt5640_set_jack_data *data = &priv->jack_data;
1222	struct acpi_device *adev;
1223	int ret = 0;
1224
1225	adev = acpi_dev_get_first_match_dev("AMCR0F28", "1", -1);
1226	if (!adev) {
1227		dev_err(card->dev, "error cannot find AMCR0F28 adev\n");
1228		return -ENOENT;
1229	}
1230
1231	data->codec_irq_override = acpi_dev_gpio_irq_get(adev, 0);
1232	if (data->codec_irq_override < 0) {
1233		ret = data->codec_irq_override;
1234		dev_err(card->dev, "error %d getting codec IRQ\n", ret);
1235		goto put_adev;
1236	}
1237
1238	if (BYT_RT5640_JDSRC(byt_rt5640_quirk) == RT5640_JD_SRC_EXT_GPIO) {
1239		acpi_dev_add_driver_gpios(adev, amcr0f28_gpios);
1240		data->jd_gpio = devm_fwnode_gpiod_get(card->dev, acpi_fwnode_handle(adev),
1241						      "rt5640-jd", GPIOD_IN, "rt5640-jd");
1242		acpi_dev_remove_driver_gpios(adev);
1243
1244		if (IS_ERR(data->jd_gpio)) {
1245			ret = PTR_ERR(data->jd_gpio);
1246			dev_err(card->dev, "error %d getting jd GPIO\n", ret);
1247		}
1248	}
1249
1250put_adev:
1251	acpi_dev_put(adev);
1252	return ret;
1253}
1254
1255static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
1256{
1257	struct snd_soc_card *card = runtime->card;
1258	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1259	struct rt5640_set_jack_data *jack_data = &priv->jack_data;
1260	struct snd_soc_component *component = snd_soc_rtd_to_codec(runtime, 0)->component;
1261	const struct snd_soc_dapm_route *custom_map = NULL;
1262	int num_routes = 0;
1263	int ret;
1264
1265	card->dapm.idle_bias_off = true;
1266	jack_data->use_platform_clock = true;
1267
1268	/* Start with RC clk for jack-detect (we disable MCLK below) */
1269	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
1270		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1271			RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_RCCLK);
1272
1273	rt5640_sel_asrc_clk_src(component,
1274				RT5640_DA_STEREO_FILTER |
1275				RT5640_DA_MONO_L_FILTER	|
1276				RT5640_DA_MONO_R_FILTER	|
1277				RT5640_AD_STEREO_FILTER	|
1278				RT5640_AD_MONO_L_FILTER	|
1279				RT5640_AD_MONO_R_FILTER,
1280				RT5640_CLK_SEL_ASRC);
1281
1282	ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
1283					ARRAY_SIZE(byt_rt5640_controls));
1284	if (ret) {
1285		dev_err(card->dev, "unable to add card controls\n");
1286		return ret;
1287	}
1288
1289	switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1290	case BYT_RT5640_IN1_MAP:
1291		custom_map = byt_rt5640_intmic_in1_map;
1292		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
1293		break;
1294	case BYT_RT5640_IN3_MAP:
1295		custom_map = byt_rt5640_intmic_in3_map;
1296		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
1297		break;
1298	case BYT_RT5640_DMIC1_MAP:
1299		custom_map = byt_rt5640_intmic_dmic1_map;
1300		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
1301		break;
1302	case BYT_RT5640_DMIC2_MAP:
1303		custom_map = byt_rt5640_intmic_dmic2_map;
1304		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
1305		break;
1306	}
1307
1308	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
1309	if (ret)
1310		return ret;
1311
1312	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1) {
1313		ret = snd_soc_dapm_add_routes(&card->dapm,
1314					byt_rt5640_hsmic2_in1_map,
1315					ARRAY_SIZE(byt_rt5640_hsmic2_in1_map));
1316		if (ret)
1317			return ret;
1318	}
1319
1320	if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
1321		ret = snd_soc_dapm_add_routes(&card->dapm,
1322					byt_rt5640_ssp2_aif2_map,
1323					ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
1324	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
1325		ret = snd_soc_dapm_add_routes(&card->dapm,
1326					byt_rt5640_ssp0_aif1_map,
1327					ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
1328	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
1329		ret = snd_soc_dapm_add_routes(&card->dapm,
1330					byt_rt5640_ssp0_aif2_map,
1331					ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
1332	} else {
1333		ret = snd_soc_dapm_add_routes(&card->dapm,
1334					byt_rt5640_ssp2_aif1_map,
1335					ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
1336	}
1337	if (ret)
1338		return ret;
1339
1340	if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1341		ret = snd_soc_dapm_add_routes(&card->dapm,
1342					byt_rt5640_mono_spk_map,
1343					ARRAY_SIZE(byt_rt5640_mono_spk_map));
1344	} else if (!(byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)) {
1345		ret = snd_soc_dapm_add_routes(&card->dapm,
1346					byt_rt5640_stereo_spk_map,
1347					ARRAY_SIZE(byt_rt5640_stereo_spk_map));
1348	}
1349	if (ret)
1350		return ret;
1351
1352	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1353		ret = snd_soc_dapm_add_routes(&card->dapm,
1354					byt_rt5640_lineout_map,
1355					ARRAY_SIZE(byt_rt5640_lineout_map));
1356		if (ret)
1357			return ret;
1358	}
1359
1360	/*
1361	 * The firmware might enable the clock at boot (this information
1362	 * may or may not be reflected in the enable clock register).
1363	 * To change the rate we must disable the clock first to cover
1364	 * these cases. Due to common clock framework restrictions that
1365	 * do not allow to disable a clock that has not been enabled,
1366	 * we need to enable the clock first.
1367	 */
1368	ret = clk_prepare_enable(priv->mclk);
1369	if (!ret)
1370		clk_disable_unprepare(priv->mclk);
1371
1372	if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
1373		ret = clk_set_rate(priv->mclk, 25000000);
1374	else
1375		ret = clk_set_rate(priv->mclk, 19200000);
1376	if (ret) {
1377		dev_err(card->dev, "unable to set MCLK rate\n");
1378		return ret;
1379	}
1380
1381	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1382		ret = snd_soc_card_jack_new_pins(card, "Headset",
1383						 SND_JACK_HEADSET | SND_JACK_BTN_0,
1384						 &priv->jack, rt5640_pins,
1385						 ARRAY_SIZE(rt5640_pins));
1386		if (ret) {
1387			dev_err(card->dev, "Jack creation failed %d\n", ret);
1388			return ret;
1389		}
1390		snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
1391				 KEY_PLAYPAUSE);
1392
1393		if (byt_rt5640_quirk & BYT_RT5640_USE_AMCR0F28) {
1394			ret = byt_rt5640_get_amcr0f28_settings(card);
1395			if (ret)
1396				return ret;
1397		}
1398
1399		snd_soc_component_set_jack(component, &priv->jack, &priv->jack_data);
1400	}
1401
1402	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1403		ret = snd_soc_card_jack_new_pins(card, "Headset",
1404						 SND_JACK_HEADSET,
1405						 &priv->jack, rt5640_pins,
1406						 ARRAY_SIZE(rt5640_pins));
1407		if (ret)
1408			return ret;
1409
1410		ret = snd_soc_card_jack_new_pins(card, "Headset 2",
1411						 SND_JACK_HEADSET,
1412						 &priv->jack2, rt5640_pins2,
1413						 ARRAY_SIZE(rt5640_pins2));
1414		if (ret)
1415			return ret;
1416
1417		rt5640_jack_gpio.data = priv;
1418		rt5640_jack_gpio.gpiod_dev = priv->codec_dev;
1419		rt5640_jack_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack1_check;
1420		ret = snd_soc_jack_add_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1421		if (ret)
1422			return ret;
1423
1424		rt5640_set_ovcd_params(component);
1425		rt5640_jack2_gpio.data = component;
1426		rt5640_jack2_gpio.gpiod_dev = priv->codec_dev;
1427		rt5640_jack2_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack2_check;
1428		ret = snd_soc_jack_add_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1429		if (ret) {
1430			snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1431			return ret;
1432		}
1433	}
1434
1435	return 0;
1436}
1437
1438static void byt_rt5640_exit(struct snd_soc_pcm_runtime *runtime)
1439{
1440	struct snd_soc_card *card = runtime->card;
1441	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1442
1443	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1444		snd_soc_jack_free_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1445		snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1446	}
1447}
1448
1449static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
1450			    struct snd_pcm_hw_params *params)
1451{
1452	struct snd_interval *rate = hw_param_interval(params,
1453			SNDRV_PCM_HW_PARAM_RATE);
1454	struct snd_interval *channels = hw_param_interval(params,
1455						SNDRV_PCM_HW_PARAM_CHANNELS);
1456	int ret, bits;
1457
1458	/* The DSP will convert the FE rate to 48k, stereo */
1459	rate->min = rate->max = 48000;
1460	channels->min = channels->max = 2;
1461
1462	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1463	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1464		/* set SSP0 to 16-bit */
1465		params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
1466		bits = 16;
1467	} else {
1468		/* set SSP2 to 24-bit */
1469		params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
1470		bits = 24;
1471	}
1472
1473	/*
1474	 * Default mode for SSP configuration is TDM 4 slot, override config
1475	 * with explicit setting to I2S 2ch. The word length is set with
1476	 * dai_set_tdm_slot() since there is no other API exposed
1477	 */
1478	ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0),
1479				  SND_SOC_DAIFMT_I2S     |
1480				  SND_SOC_DAIFMT_NB_NF   |
1481				  SND_SOC_DAIFMT_BP_FP);
1482	if (ret < 0) {
1483		dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
1484		return ret;
1485	}
1486
1487	ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
1488	if (ret < 0) {
1489		dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
1490		return ret;
1491	}
1492
1493	return 0;
1494}
1495
1496static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
1497{
1498	return snd_pcm_hw_constraint_single(substream->runtime,
1499			SNDRV_PCM_HW_PARAM_RATE, 48000);
1500}
1501
1502static const struct snd_soc_ops byt_rt5640_aif1_ops = {
1503	.startup = byt_rt5640_aif1_startup,
1504};
1505
1506static const struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
1507	.hw_params = byt_rt5640_aif1_hw_params,
1508};
1509
1510SND_SOC_DAILINK_DEF(dummy,
1511	DAILINK_COMP_ARRAY(COMP_DUMMY()));
1512
1513SND_SOC_DAILINK_DEF(media,
1514	DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
1515
1516SND_SOC_DAILINK_DEF(deepbuffer,
1517	DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
1518
1519SND_SOC_DAILINK_DEF(ssp2_port,
1520	/* overwritten for ssp0 routing */
1521	DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
1522SND_SOC_DAILINK_DEF(ssp2_codec,
1523	DAILINK_COMP_ARRAY(COMP_CODEC(
1524	/* overwritten with HID */ "i2c-10EC5640:00",
1525	/* changed w/ quirk */	"rt5640-aif1")));
1526
1527SND_SOC_DAILINK_DEF(platform,
1528	DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
1529
1530static struct snd_soc_dai_link byt_rt5640_dais[] = {
1531	[MERR_DPCM_AUDIO] = {
1532		.name = "Baytrail Audio Port",
1533		.stream_name = "Baytrail Audio",
1534		.nonatomic = true,
1535		.dynamic = 1,
1536		.dpcm_playback = 1,
1537		.dpcm_capture = 1,
1538		.ops = &byt_rt5640_aif1_ops,
1539		SND_SOC_DAILINK_REG(media, dummy, platform),
1540	},
1541	[MERR_DPCM_DEEP_BUFFER] = {
1542		.name = "Deep-Buffer Audio Port",
1543		.stream_name = "Deep-Buffer Audio",
1544		.nonatomic = true,
1545		.dynamic = 1,
1546		.dpcm_playback = 1,
1547		.ops = &byt_rt5640_aif1_ops,
1548		SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
1549	},
1550		/* back ends */
1551	{
1552		.name = "SSP2-Codec",
1553		.id = 0,
1554		.no_pcm = 1,
1555		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1556						| SND_SOC_DAIFMT_CBC_CFC,
1557		.be_hw_params_fixup = byt_rt5640_codec_fixup,
1558		.dpcm_playback = 1,
1559		.dpcm_capture = 1,
1560		.init = byt_rt5640_init,
1561		.exit = byt_rt5640_exit,
1562		.ops = &byt_rt5640_be_ssp2_ops,
1563		SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
1564	},
1565};
1566
1567/* SoC card */
1568static char byt_rt5640_codec_name[SND_ACPI_I2C_ID_LEN];
1569#if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1570static char byt_rt5640_long_name[40]; /* = "bytcr-rt5640-*-spk-*-mic" */
1571#endif
1572static char byt_rt5640_components[64]; /* = "cfg-spk:* cfg-mic:* ..." */
1573
1574static int byt_rt5640_suspend(struct snd_soc_card *card)
1575{
1576	struct snd_soc_component *component;
1577
1578	if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1579		return 0;
1580
1581	for_each_card_components(card, component) {
1582		if (!strcmp(component->name, byt_rt5640_codec_name)) {
1583			dev_dbg(component->dev, "disabling jack detect before suspend\n");
1584			snd_soc_component_set_jack(component, NULL, NULL);
1585			break;
1586		}
1587	}
1588
1589	return 0;
1590}
1591
1592static int byt_rt5640_resume(struct snd_soc_card *card)
1593{
1594	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1595	struct snd_soc_component *component;
1596
1597	if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1598		return 0;
1599
1600	for_each_card_components(card, component) {
1601		if (!strcmp(component->name, byt_rt5640_codec_name)) {
1602			dev_dbg(component->dev, "re-enabling jack detect after resume\n");
1603			snd_soc_component_set_jack(component, &priv->jack,
1604						   &priv->jack_data);
1605			break;
1606		}
1607	}
1608
1609	return 0;
1610}
1611
1612/* use space before codec name to simplify card ID, and simplify driver name */
1613#define SOF_CARD_NAME "bytcht rt5640" /* card name will be 'sof-bytcht rt5640' */
1614#define SOF_DRIVER_NAME "SOF"
1615
1616#define CARD_NAME "bytcr-rt5640"
1617#define DRIVER_NAME NULL /* card name will be used for driver name */
1618
1619static struct snd_soc_card byt_rt5640_card = {
1620	.owner = THIS_MODULE,
1621	.dai_link = byt_rt5640_dais,
1622	.num_links = ARRAY_SIZE(byt_rt5640_dais),
1623	.dapm_widgets = byt_rt5640_widgets,
1624	.num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
1625	.dapm_routes = byt_rt5640_audio_map,
1626	.num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
1627	.fully_routed = true,
1628	.suspend_pre = byt_rt5640_suspend,
1629	.resume_post = byt_rt5640_resume,
1630};
1631
1632struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
1633	u64 aif_value;       /* 1: AIF1, 2: AIF2 */
1634	u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
1635};
1636
1637static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
1638{
1639	struct device *dev = &pdev->dev;
1640	static const char * const map_name[] = { "dmic1", "dmic2", "in1", "in3", "none" };
1641	struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
1642	__maybe_unused const char *spk_type;
1643	const struct dmi_system_id *dmi_id;
1644	const char *headset2_string = "";
1645	const char *lineout_string = "";
1646	struct byt_rt5640_private *priv;
1647	const char *platform_name;
1648	struct acpi_device *adev;
1649	struct device *codec_dev;
1650	const char *cfg_spk;
1651	bool sof_parent;
1652	int ret_val = 0;
1653	int dai_index = 0;
1654	int i, aif;
1655
1656	is_bytcr = false;
1657	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1658	if (!priv)
1659		return -ENOMEM;
1660
1661	/* register the soc card */
1662	byt_rt5640_card.dev = dev;
1663	snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
1664
1665	/* fix index of codec dai */
1666	for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
1667		if (byt_rt5640_dais[i].codecs->name &&
1668		    !strcmp(byt_rt5640_dais[i].codecs->name,
1669			    "i2c-10EC5640:00")) {
1670			dai_index = i;
1671			break;
1672		}
1673	}
1674
1675	/* fixup codec name based on HID */
1676	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
1677	if (adev) {
1678		snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
1679			 "i2c-%s", acpi_dev_name(adev));
1680		byt_rt5640_dais[dai_index].codecs->name = byt_rt5640_codec_name;
1681	} else {
1682		dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
1683		return -ENXIO;
1684	}
1685
1686	codec_dev = acpi_get_first_physical_node(adev);
1687	acpi_dev_put(adev);
1688	if (!codec_dev)
1689		return -EPROBE_DEFER;
1690	priv->codec_dev = get_device(codec_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1691
1692	/*
1693	 * swap SSP0 if bytcr is detected
1694	 * (will be overridden if DMI quirk is detected)
1695	 */
1696	if (soc_intel_is_byt()) {
1697		if (mach->mach_params.acpi_ipc_irq_index == 0)
1698			is_bytcr = true;
1699	}
1700
1701	if (is_bytcr) {
1702		/*
1703		 * Baytrail CR platforms may have CHAN package in BIOS, try
1704		 * to find relevant routing quirk based as done on Windows
1705		 * platforms. We have to read the information directly from the
1706		 * BIOS, at this stage the card is not created and the links
1707		 * with the codec driver/pdata are non-existent
1708		 */
1709
1710		struct acpi_chan_package chan_package = { 0 };
1711
1712		/* format specified: 2 64-bit integers */
1713		struct acpi_buffer format = {sizeof("NN"), "NN"};
1714		struct acpi_buffer state = {0, NULL};
1715		struct snd_soc_acpi_package_context pkg_ctx;
1716		bool pkg_found = false;
1717
1718		state.length = sizeof(chan_package);
1719		state.pointer = &chan_package;
1720
1721		pkg_ctx.name = "CHAN";
1722		pkg_ctx.length = 2;
1723		pkg_ctx.format = &format;
1724		pkg_ctx.state = &state;
1725		pkg_ctx.data_valid = false;
1726
1727		pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
1728							       &pkg_ctx);
1729		if (pkg_found) {
1730			if (chan_package.aif_value == 1) {
1731				dev_info(dev, "BIOS Routing: AIF1 connected\n");
1732				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF1;
1733			} else  if (chan_package.aif_value == 2) {
1734				dev_info(dev, "BIOS Routing: AIF2 connected\n");
1735				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1736			} else {
1737				dev_info(dev, "BIOS Routing isn't valid, ignored\n");
1738				pkg_found = false;
1739			}
1740		}
1741
1742		if (!pkg_found) {
1743			/* no BIOS indications, assume SSP0-AIF2 connection */
1744			byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1745		}
1746
1747		/* change defaults for Baytrail-CR capture */
1748		byt_rt5640_quirk |= BYTCR_INPUT_DEFAULTS;
1749	} else {
1750		byt_rt5640_quirk |= BYT_RT5640_DMIC1_MAP |
1751				    BYT_RT5640_JD_SRC_JD2_IN4N |
1752				    BYT_RT5640_OVCD_TH_2000UA |
1753				    BYT_RT5640_OVCD_SF_0P75;
1754	}
1755
1756	/* check quirks before creating card */
1757	dmi_id = dmi_first_match(byt_rt5640_quirk_table);
1758	if (dmi_id)
1759		byt_rt5640_quirk = (unsigned long)dmi_id->driver_data;
1760	if (quirk_override != -1) {
1761		dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
1762			 byt_rt5640_quirk, quirk_override);
1763		byt_rt5640_quirk = quirk_override;
1764	}
1765
1766	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1767		acpi_dev_add_driver_gpios(ACPI_COMPANION(priv->codec_dev),
1768					  byt_rt5640_hp_elitepad_1000g2_gpios);
1769
1770		priv->hsmic_detect = devm_fwnode_gpiod_get(dev, codec_dev->fwnode,
1771							   "headset-mic-detect", GPIOD_IN,
1772							   "headset-mic-detect");
1773		if (IS_ERR(priv->hsmic_detect)) {
1774			ret_val = dev_err_probe(dev, PTR_ERR(priv->hsmic_detect),
1775						"getting hsmic-detect GPIO\n");
1776			goto err_device;
1777		}
1778	}
1779
1780	/* Must be called before register_card, also see declaration comment. */
1781	ret_val = byt_rt5640_add_codec_device_props(codec_dev, priv);
1782	if (ret_val)
1783		goto err_remove_gpios;
1784
1785	log_quirks(dev);
1786
1787	if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
1788	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1789		byt_rt5640_dais[dai_index].codecs->dai_name = "rt5640-aif2";
1790		aif = 2;
1791	} else {
1792		aif = 1;
1793	}
1794
1795	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1796	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2))
1797		byt_rt5640_dais[dai_index].cpus->dai_name = "ssp0-port";
1798
1799	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
1800		priv->mclk = devm_clk_get_optional(dev, "pmc_plt_clk_3");
1801		if (IS_ERR(priv->mclk)) {
1802			ret_val = dev_err_probe(dev, PTR_ERR(priv->mclk),
1803						"Failed to get MCLK from pmc_plt_clk_3\n");
1804			goto err;
1805		}
1806		/*
1807		 * Fall back to bit clock usage when clock is not
1808		 * available likely due to missing dependencies.
1809		 */
1810		if (!priv->mclk)
1811			byt_rt5640_quirk &= ~BYT_RT5640_MCLK_EN;
1812	}
1813
1814	if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS) {
1815		cfg_spk = "0";
1816		spk_type = "none";
1817	} else if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1818		cfg_spk = "1";
1819		spk_type = "mono";
1820	} else if (byt_rt5640_quirk & BYT_RT5640_SWAPPED_SPEAKERS) {
1821		cfg_spk = "swapped";
1822		spk_type = "swapped";
1823	} else {
1824		cfg_spk = "2";
1825		spk_type = "stereo";
1826	}
1827
1828	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1829		if (byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2)
1830			lineout_string = " cfg-hp2:lineout";
1831		else
1832			lineout_string = " cfg-lineout:2";
1833	}
1834
1835	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1)
1836		headset2_string = " cfg-hs2:in1";
1837
1838	snprintf(byt_rt5640_components, sizeof(byt_rt5640_components),
1839		 "cfg-spk:%s cfg-mic:%s aif:%d%s%s", cfg_spk,
1840		 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)], aif,
1841		 lineout_string, headset2_string);
1842	byt_rt5640_card.components = byt_rt5640_components;
1843#if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1844	snprintf(byt_rt5640_long_name, sizeof(byt_rt5640_long_name),
1845		 "bytcr-rt5640-%s-spk-%s-mic", spk_type,
1846		 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)]);
1847	byt_rt5640_card.long_name = byt_rt5640_long_name;
1848#endif
1849
1850	/* override platform name, if required */
1851	platform_name = mach->mach_params.platform;
1852
1853	ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5640_card,
1854							platform_name);
1855	if (ret_val)
1856		goto err;
1857
1858	sof_parent = snd_soc_acpi_sof_parent(dev);
1859
1860	/* set card and driver name */
1861	if (sof_parent) {
1862		byt_rt5640_card.name = SOF_CARD_NAME;
1863		byt_rt5640_card.driver_name = SOF_DRIVER_NAME;
1864	} else {
1865		byt_rt5640_card.name = CARD_NAME;
1866		byt_rt5640_card.driver_name = DRIVER_NAME;
1867	}
1868
1869	/* set pm ops */
1870	if (sof_parent)
1871		dev->driver->pm = &snd_soc_pm_ops;
1872
1873	ret_val = devm_snd_soc_register_card(dev, &byt_rt5640_card);
1874	if (ret_val) {
1875		dev_err(dev, "devm_snd_soc_register_card failed %d\n", ret_val);
1876		goto err;
1877	}
1878	platform_set_drvdata(pdev, &byt_rt5640_card);
1879	return ret_val;
1880
1881err:
1882	device_remove_software_node(priv->codec_dev);
1883err_remove_gpios:
1884	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1885		acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1886err_device:
1887	put_device(priv->codec_dev);
1888	return ret_val;
1889}
1890
1891static void snd_byt_rt5640_mc_remove(struct platform_device *pdev)
1892{
1893	struct snd_soc_card *card = platform_get_drvdata(pdev);
1894	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1895
1896	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1897		acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1898
1899	device_remove_software_node(priv->codec_dev);
1900	put_device(priv->codec_dev);
1901}
1902
1903static struct platform_driver snd_byt_rt5640_mc_driver = {
1904	.driver = {
1905		.name = "bytcr_rt5640",
1906	},
1907	.probe = snd_byt_rt5640_mc_probe,
1908	.remove_new = snd_byt_rt5640_mc_remove,
1909};
1910
1911module_platform_driver(snd_byt_rt5640_mc_driver);
1912
1913MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
1914MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
1915MODULE_LICENSE("GPL v2");
1916MODULE_ALIAS("platform:bytcr_rt5640");