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