linux/sound/soc/codecs/rt5640.c
<<
>>
Prefs
   1/*
   2 * rt5640.c  --  RT5640/RT5639 ALSA SoC audio codec driver
   3 *
   4 * Copyright 2011 Realtek Semiconductor Corp.
   5 * Author: Johnny Hsu <johnnyhsu@realtek.com>
   6 * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 */
  12
  13#include <linux/module.h>
  14#include <linux/moduleparam.h>
  15#include <linux/init.h>
  16#include <linux/delay.h>
  17#include <linux/pm.h>
  18#include <linux/gpio.h>
  19#include <linux/i2c.h>
  20#include <linux/regmap.h>
  21#include <linux/of.h>
  22#include <linux/of_gpio.h>
  23#include <linux/platform_device.h>
  24#include <linux/spi/spi.h>
  25#include <linux/acpi.h>
  26#include <sound/core.h>
  27#include <sound/jack.h>
  28#include <sound/pcm.h>
  29#include <sound/pcm_params.h>
  30#include <sound/soc.h>
  31#include <sound/soc-dapm.h>
  32#include <sound/initval.h>
  33#include <sound/tlv.h>
  34
  35#include "rl6231.h"
  36#include "rt5640.h"
  37
  38#define RT5640_DEVICE_ID 0x6231
  39
  40#define RT5640_PR_RANGE_BASE (0xff + 1)
  41#define RT5640_PR_SPACING 0x100
  42
  43#define RT5640_PR_BASE (RT5640_PR_RANGE_BASE + (0 * RT5640_PR_SPACING))
  44
  45static const struct regmap_range_cfg rt5640_ranges[] = {
  46        { .name = "PR", .range_min = RT5640_PR_BASE,
  47          .range_max = RT5640_PR_BASE + 0xb4,
  48          .selector_reg = RT5640_PRIV_INDEX,
  49          .selector_mask = 0xff,
  50          .selector_shift = 0x0,
  51          .window_start = RT5640_PRIV_DATA,
  52          .window_len = 0x1, },
  53};
  54
  55static const struct reg_sequence init_list[] = {
  56        {RT5640_PR_BASE + 0x3d, 0x3600},
  57        {RT5640_PR_BASE + 0x12, 0x0aa8},
  58        {RT5640_PR_BASE + 0x14, 0x0aaa},
  59        {RT5640_PR_BASE + 0x20, 0x6110},
  60        {RT5640_PR_BASE + 0x21, 0xe0e0},
  61        {RT5640_PR_BASE + 0x23, 0x1804},
  62};
  63
  64static const struct reg_default rt5640_reg[] = {
  65        { 0x00, 0x000e },
  66        { 0x01, 0xc8c8 },
  67        { 0x02, 0xc8c8 },
  68        { 0x03, 0xc8c8 },
  69        { 0x04, 0x8000 },
  70        { 0x0d, 0x0000 },
  71        { 0x0e, 0x0000 },
  72        { 0x0f, 0x0808 },
  73        { 0x19, 0xafaf },
  74        { 0x1a, 0xafaf },
  75        { 0x1b, 0x0000 },
  76        { 0x1c, 0x2f2f },
  77        { 0x1d, 0x2f2f },
  78        { 0x1e, 0x0000 },
  79        { 0x27, 0x7060 },
  80        { 0x28, 0x7070 },
  81        { 0x29, 0x8080 },
  82        { 0x2a, 0x5454 },
  83        { 0x2b, 0x5454 },
  84        { 0x2c, 0xaa00 },
  85        { 0x2d, 0x0000 },
  86        { 0x2e, 0xa000 },
  87        { 0x2f, 0x0000 },
  88        { 0x3b, 0x0000 },
  89        { 0x3c, 0x007f },
  90        { 0x3d, 0x0000 },
  91        { 0x3e, 0x007f },
  92        { 0x45, 0xe000 },
  93        { 0x46, 0x003e },
  94        { 0x47, 0x003e },
  95        { 0x48, 0xf800 },
  96        { 0x49, 0x3800 },
  97        { 0x4a, 0x0004 },
  98        { 0x4c, 0xfc00 },
  99        { 0x4d, 0x0000 },
 100        { 0x4f, 0x01ff },
 101        { 0x50, 0x0000 },
 102        { 0x51, 0x0000 },
 103        { 0x52, 0x01ff },
 104        { 0x53, 0xf000 },
 105        { 0x61, 0x0000 },
 106        { 0x62, 0x0000 },
 107        { 0x63, 0x00c0 },
 108        { 0x64, 0x0000 },
 109        { 0x65, 0x0000 },
 110        { 0x66, 0x0000 },
 111        { 0x6a, 0x0000 },
 112        { 0x6c, 0x0000 },
 113        { 0x70, 0x8000 },
 114        { 0x71, 0x8000 },
 115        { 0x72, 0x8000 },
 116        { 0x73, 0x1114 },
 117        { 0x74, 0x0c00 },
 118        { 0x75, 0x1d00 },
 119        { 0x80, 0x0000 },
 120        { 0x81, 0x0000 },
 121        { 0x82, 0x0000 },
 122        { 0x83, 0x0000 },
 123        { 0x84, 0x0000 },
 124        { 0x85, 0x0008 },
 125        { 0x89, 0x0000 },
 126        { 0x8a, 0x0000 },
 127        { 0x8b, 0x0600 },
 128        { 0x8c, 0x0228 },
 129        { 0x8d, 0xa000 },
 130        { 0x8e, 0x0004 },
 131        { 0x8f, 0x1100 },
 132        { 0x90, 0x0646 },
 133        { 0x91, 0x0c00 },
 134        { 0x92, 0x0000 },
 135        { 0x93, 0x3000 },
 136        { 0xb0, 0x2080 },
 137        { 0xb1, 0x0000 },
 138        { 0xb4, 0x2206 },
 139        { 0xb5, 0x1f00 },
 140        { 0xb6, 0x0000 },
 141        { 0xb8, 0x034b },
 142        { 0xb9, 0x0066 },
 143        { 0xba, 0x000b },
 144        { 0xbb, 0x0000 },
 145        { 0xbc, 0x0000 },
 146        { 0xbd, 0x0000 },
 147        { 0xbe, 0x0000 },
 148        { 0xbf, 0x0000 },
 149        { 0xc0, 0x0400 },
 150        { 0xc2, 0x0000 },
 151        { 0xc4, 0x0000 },
 152        { 0xc5, 0x0000 },
 153        { 0xc6, 0x2000 },
 154        { 0xc8, 0x0000 },
 155        { 0xc9, 0x0000 },
 156        { 0xca, 0x0000 },
 157        { 0xcb, 0x0000 },
 158        { 0xcc, 0x0000 },
 159        { 0xcf, 0x0013 },
 160        { 0xd0, 0x0680 },
 161        { 0xd1, 0x1c17 },
 162        { 0xd2, 0x8c00 },
 163        { 0xd3, 0xaa20 },
 164        { 0xd6, 0x0400 },
 165        { 0xd9, 0x0809 },
 166        { 0xfe, 0x10ec },
 167        { 0xff, 0x6231 },
 168};
 169
 170static int rt5640_reset(struct snd_soc_component *component)
 171{
 172        return snd_soc_component_write(component, RT5640_RESET, 0);
 173}
 174
 175static bool rt5640_volatile_register(struct device *dev, unsigned int reg)
 176{
 177        int i;
 178
 179        for (i = 0; i < ARRAY_SIZE(rt5640_ranges); i++)
 180                if ((reg >= rt5640_ranges[i].window_start &&
 181                     reg <= rt5640_ranges[i].window_start +
 182                     rt5640_ranges[i].window_len) ||
 183                    (reg >= rt5640_ranges[i].range_min &&
 184                     reg <= rt5640_ranges[i].range_max))
 185                        return true;
 186
 187        switch (reg) {
 188        case RT5640_RESET:
 189        case RT5640_ASRC_5:
 190        case RT5640_EQ_CTRL1:
 191        case RT5640_DRC_AGC_1:
 192        case RT5640_ANC_CTRL1:
 193        case RT5640_IRQ_CTRL2:
 194        case RT5640_INT_IRQ_ST:
 195        case RT5640_DSP_CTRL2:
 196        case RT5640_DSP_CTRL3:
 197        case RT5640_PRIV_INDEX:
 198        case RT5640_PRIV_DATA:
 199        case RT5640_PGM_REG_ARR1:
 200        case RT5640_PGM_REG_ARR3:
 201        case RT5640_VENDOR_ID:
 202        case RT5640_VENDOR_ID1:
 203        case RT5640_VENDOR_ID2:
 204                return true;
 205        default:
 206                return false;
 207        }
 208}
 209
 210static bool rt5640_readable_register(struct device *dev, unsigned int reg)
 211{
 212        int i;
 213
 214        for (i = 0; i < ARRAY_SIZE(rt5640_ranges); i++)
 215                if ((reg >= rt5640_ranges[i].window_start &&
 216                     reg <= rt5640_ranges[i].window_start +
 217                     rt5640_ranges[i].window_len) ||
 218                    (reg >= rt5640_ranges[i].range_min &&
 219                     reg <= rt5640_ranges[i].range_max))
 220                        return true;
 221
 222        switch (reg) {
 223        case RT5640_RESET:
 224        case RT5640_SPK_VOL:
 225        case RT5640_HP_VOL:
 226        case RT5640_OUTPUT:
 227        case RT5640_MONO_OUT:
 228        case RT5640_IN1_IN2:
 229        case RT5640_IN3_IN4:
 230        case RT5640_INL_INR_VOL:
 231        case RT5640_DAC1_DIG_VOL:
 232        case RT5640_DAC2_DIG_VOL:
 233        case RT5640_DAC2_CTRL:
 234        case RT5640_ADC_DIG_VOL:
 235        case RT5640_ADC_DATA:
 236        case RT5640_ADC_BST_VOL:
 237        case RT5640_STO_ADC_MIXER:
 238        case RT5640_MONO_ADC_MIXER:
 239        case RT5640_AD_DA_MIXER:
 240        case RT5640_STO_DAC_MIXER:
 241        case RT5640_MONO_DAC_MIXER:
 242        case RT5640_DIG_MIXER:
 243        case RT5640_DSP_PATH1:
 244        case RT5640_DSP_PATH2:
 245        case RT5640_DIG_INF_DATA:
 246        case RT5640_REC_L1_MIXER:
 247        case RT5640_REC_L2_MIXER:
 248        case RT5640_REC_R1_MIXER:
 249        case RT5640_REC_R2_MIXER:
 250        case RT5640_HPO_MIXER:
 251        case RT5640_SPK_L_MIXER:
 252        case RT5640_SPK_R_MIXER:
 253        case RT5640_SPO_L_MIXER:
 254        case RT5640_SPO_R_MIXER:
 255        case RT5640_SPO_CLSD_RATIO:
 256        case RT5640_MONO_MIXER:
 257        case RT5640_OUT_L1_MIXER:
 258        case RT5640_OUT_L2_MIXER:
 259        case RT5640_OUT_L3_MIXER:
 260        case RT5640_OUT_R1_MIXER:
 261        case RT5640_OUT_R2_MIXER:
 262        case RT5640_OUT_R3_MIXER:
 263        case RT5640_LOUT_MIXER:
 264        case RT5640_PWR_DIG1:
 265        case RT5640_PWR_DIG2:
 266        case RT5640_PWR_ANLG1:
 267        case RT5640_PWR_ANLG2:
 268        case RT5640_PWR_MIXER:
 269        case RT5640_PWR_VOL:
 270        case RT5640_PRIV_INDEX:
 271        case RT5640_PRIV_DATA:
 272        case RT5640_I2S1_SDP:
 273        case RT5640_I2S2_SDP:
 274        case RT5640_ADDA_CLK1:
 275        case RT5640_ADDA_CLK2:
 276        case RT5640_DMIC:
 277        case RT5640_GLB_CLK:
 278        case RT5640_PLL_CTRL1:
 279        case RT5640_PLL_CTRL2:
 280        case RT5640_ASRC_1:
 281        case RT5640_ASRC_2:
 282        case RT5640_ASRC_3:
 283        case RT5640_ASRC_4:
 284        case RT5640_ASRC_5:
 285        case RT5640_HP_OVCD:
 286        case RT5640_CLS_D_OVCD:
 287        case RT5640_CLS_D_OUT:
 288        case RT5640_DEPOP_M1:
 289        case RT5640_DEPOP_M2:
 290        case RT5640_DEPOP_M3:
 291        case RT5640_CHARGE_PUMP:
 292        case RT5640_PV_DET_SPK_G:
 293        case RT5640_MICBIAS:
 294        case RT5640_EQ_CTRL1:
 295        case RT5640_EQ_CTRL2:
 296        case RT5640_WIND_FILTER:
 297        case RT5640_DRC_AGC_1:
 298        case RT5640_DRC_AGC_2:
 299        case RT5640_DRC_AGC_3:
 300        case RT5640_SVOL_ZC:
 301        case RT5640_ANC_CTRL1:
 302        case RT5640_ANC_CTRL2:
 303        case RT5640_ANC_CTRL3:
 304        case RT5640_JD_CTRL:
 305        case RT5640_ANC_JD:
 306        case RT5640_IRQ_CTRL1:
 307        case RT5640_IRQ_CTRL2:
 308        case RT5640_INT_IRQ_ST:
 309        case RT5640_GPIO_CTRL1:
 310        case RT5640_GPIO_CTRL2:
 311        case RT5640_GPIO_CTRL3:
 312        case RT5640_DSP_CTRL1:
 313        case RT5640_DSP_CTRL2:
 314        case RT5640_DSP_CTRL3:
 315        case RT5640_DSP_CTRL4:
 316        case RT5640_PGM_REG_ARR1:
 317        case RT5640_PGM_REG_ARR2:
 318        case RT5640_PGM_REG_ARR3:
 319        case RT5640_PGM_REG_ARR4:
 320        case RT5640_PGM_REG_ARR5:
 321        case RT5640_SCB_FUNC:
 322        case RT5640_SCB_CTRL:
 323        case RT5640_BASE_BACK:
 324        case RT5640_MP3_PLUS1:
 325        case RT5640_MP3_PLUS2:
 326        case RT5640_3D_HP:
 327        case RT5640_ADJ_HPF:
 328        case RT5640_HP_CALIB_AMP_DET:
 329        case RT5640_HP_CALIB2:
 330        case RT5640_SV_ZCD1:
 331        case RT5640_SV_ZCD2:
 332        case RT5640_DUMMY1:
 333        case RT5640_DUMMY2:
 334        case RT5640_DUMMY3:
 335        case RT5640_VENDOR_ID:
 336        case RT5640_VENDOR_ID1:
 337        case RT5640_VENDOR_ID2:
 338                return true;
 339        default:
 340                return false;
 341        }
 342}
 343
 344static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
 345static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -65625, 375, 0);
 346static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
 347static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -17625, 375, 0);
 348static const DECLARE_TLV_DB_SCALE(adc_bst_tlv, 0, 1200, 0);
 349
 350/* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
 351static const DECLARE_TLV_DB_RANGE(bst_tlv,
 352        0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
 353        1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
 354        2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
 355        3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
 356        6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
 357        7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
 358        8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0)
 359);
 360
 361/* Interface data select */
 362static const char * const rt5640_data_select[] = {
 363        "Normal", "Swap", "left copy to right", "right copy to left"};
 364
 365static SOC_ENUM_SINGLE_DECL(rt5640_if1_dac_enum, RT5640_DIG_INF_DATA,
 366                            RT5640_IF1_DAC_SEL_SFT, rt5640_data_select);
 367
 368static SOC_ENUM_SINGLE_DECL(rt5640_if1_adc_enum, RT5640_DIG_INF_DATA,
 369                            RT5640_IF1_ADC_SEL_SFT, rt5640_data_select);
 370
 371static SOC_ENUM_SINGLE_DECL(rt5640_if2_dac_enum, RT5640_DIG_INF_DATA,
 372                            RT5640_IF2_DAC_SEL_SFT, rt5640_data_select);
 373
 374static SOC_ENUM_SINGLE_DECL(rt5640_if2_adc_enum, RT5640_DIG_INF_DATA,
 375                            RT5640_IF2_ADC_SEL_SFT, rt5640_data_select);
 376
 377/* Class D speaker gain ratio */
 378static const char * const rt5640_clsd_spk_ratio[] = {"1.66x", "1.83x", "1.94x",
 379        "2x", "2.11x", "2.22x", "2.33x", "2.44x", "2.55x", "2.66x", "2.77x"};
 380
 381static SOC_ENUM_SINGLE_DECL(rt5640_clsd_spk_ratio_enum, RT5640_CLS_D_OUT,
 382                            RT5640_CLSD_RATIO_SFT, rt5640_clsd_spk_ratio);
 383
 384static const struct snd_kcontrol_new rt5640_snd_controls[] = {
 385        /* Speaker Output Volume */
 386        SOC_DOUBLE("Speaker Channel Switch", RT5640_SPK_VOL,
 387                RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
 388        SOC_DOUBLE_TLV("Speaker Playback Volume", RT5640_SPK_VOL,
 389                RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
 390        /* Headphone Output Volume */
 391        SOC_DOUBLE("HP Channel Switch", RT5640_HP_VOL,
 392                RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
 393        SOC_DOUBLE_TLV("HP Playback Volume", RT5640_HP_VOL,
 394                RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
 395        /* OUTPUT Control */
 396        SOC_DOUBLE("OUT Playback Switch", RT5640_OUTPUT,
 397                RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
 398        SOC_DOUBLE("OUT Channel Switch", RT5640_OUTPUT,
 399                RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
 400        SOC_DOUBLE_TLV("OUT Playback Volume", RT5640_OUTPUT,
 401                RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
 402
 403        /* DAC Digital Volume */
 404        SOC_DOUBLE("DAC2 Playback Switch", RT5640_DAC2_CTRL,
 405                RT5640_M_DAC_L2_VOL_SFT, RT5640_M_DAC_R2_VOL_SFT, 1, 1),
 406        SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5640_DAC1_DIG_VOL,
 407                        RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
 408                        175, 0, dac_vol_tlv),
 409        /* IN1/IN2/IN3 Control */
 410        SOC_SINGLE_TLV("IN1 Boost", RT5640_IN1_IN2,
 411                RT5640_BST_SFT1, 8, 0, bst_tlv),
 412        SOC_SINGLE_TLV("IN2 Boost", RT5640_IN3_IN4,
 413                RT5640_BST_SFT2, 8, 0, bst_tlv),
 414        SOC_SINGLE_TLV("IN3 Boost", RT5640_IN1_IN2,
 415                RT5640_BST_SFT2, 8, 0, bst_tlv),
 416
 417        /* INL/INR Volume Control */
 418        SOC_DOUBLE_TLV("IN Capture Volume", RT5640_INL_INR_VOL,
 419                        RT5640_INL_VOL_SFT, RT5640_INR_VOL_SFT,
 420                        31, 1, in_vol_tlv),
 421        /* ADC Digital Volume Control */
 422        SOC_DOUBLE("ADC Capture Switch", RT5640_ADC_DIG_VOL,
 423                RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
 424        SOC_DOUBLE_TLV("ADC Capture Volume", RT5640_ADC_DIG_VOL,
 425                        RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
 426                        127, 0, adc_vol_tlv),
 427        SOC_DOUBLE("Mono ADC Capture Switch", RT5640_DUMMY1,
 428                RT5640_M_MONO_ADC_L_SFT, RT5640_M_MONO_ADC_R_SFT, 1, 1),
 429        SOC_DOUBLE_TLV("Mono ADC Capture Volume", RT5640_ADC_DATA,
 430                        RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
 431                        127, 0, adc_vol_tlv),
 432        /* ADC Boost Volume Control */
 433        SOC_DOUBLE_TLV("ADC Boost Gain", RT5640_ADC_BST_VOL,
 434                        RT5640_ADC_L_BST_SFT, RT5640_ADC_R_BST_SFT,
 435                        3, 0, adc_bst_tlv),
 436        /* Class D speaker gain ratio */
 437        SOC_ENUM("Class D SPK Ratio Control", rt5640_clsd_spk_ratio_enum),
 438
 439        SOC_ENUM("ADC IF1 Data Switch", rt5640_if1_adc_enum),
 440        SOC_ENUM("DAC IF1 Data Switch", rt5640_if1_dac_enum),
 441        SOC_ENUM("ADC IF2 Data Switch", rt5640_if2_adc_enum),
 442        SOC_ENUM("DAC IF2 Data Switch", rt5640_if2_dac_enum),
 443};
 444
 445static const struct snd_kcontrol_new rt5640_specific_snd_controls[] = {
 446        /* MONO Output Control */
 447        SOC_SINGLE("Mono Playback Switch", RT5640_MONO_OUT, RT5640_L_MUTE_SFT,
 448                1, 1),
 449
 450        SOC_DOUBLE_TLV("Mono DAC Playback Volume", RT5640_DAC2_DIG_VOL,
 451                RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 175, 0, dac_vol_tlv),
 452};
 453
 454/**
 455 * set_dmic_clk - Set parameter of dmic.
 456 *
 457 * @w: DAPM widget.
 458 * @kcontrol: The kcontrol of this widget.
 459 * @event: Event id.
 460 *
 461 */
 462static int set_dmic_clk(struct snd_soc_dapm_widget *w,
 463        struct snd_kcontrol *kcontrol, int event)
 464{
 465        struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
 466        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
 467        int idx, rate;
 468
 469        rate = rt5640->sysclk / rl6231_get_pre_div(rt5640->regmap,
 470                RT5640_ADDA_CLK1, RT5640_I2S_PD1_SFT);
 471        idx = rl6231_calc_dmic_clk(rate);
 472        if (idx < 0)
 473                dev_err(component->dev, "Failed to set DMIC clock\n");
 474        else
 475                snd_soc_component_update_bits(component, RT5640_DMIC, RT5640_DMIC_CLK_MASK,
 476                                        idx << RT5640_DMIC_CLK_SFT);
 477        return idx;
 478}
 479
 480static int is_using_asrc(struct snd_soc_dapm_widget *source,
 481                         struct snd_soc_dapm_widget *sink)
 482{
 483        struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
 484        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
 485
 486        if (!rt5640->asrc_en)
 487                return 0;
 488
 489        return 1;
 490}
 491
 492/* Digital Mixer */
 493static const struct snd_kcontrol_new rt5640_sto_adc_l_mix[] = {
 494        SOC_DAPM_SINGLE("ADC1 Switch", RT5640_STO_ADC_MIXER,
 495                        RT5640_M_ADC_L1_SFT, 1, 1),
 496        SOC_DAPM_SINGLE("ADC2 Switch", RT5640_STO_ADC_MIXER,
 497                        RT5640_M_ADC_L2_SFT, 1, 1),
 498};
 499
 500static const struct snd_kcontrol_new rt5640_sto_adc_r_mix[] = {
 501        SOC_DAPM_SINGLE("ADC1 Switch", RT5640_STO_ADC_MIXER,
 502                        RT5640_M_ADC_R1_SFT, 1, 1),
 503        SOC_DAPM_SINGLE("ADC2 Switch", RT5640_STO_ADC_MIXER,
 504                        RT5640_M_ADC_R2_SFT, 1, 1),
 505};
 506
 507static const struct snd_kcontrol_new rt5640_mono_adc_l_mix[] = {
 508        SOC_DAPM_SINGLE("ADC1 Switch", RT5640_MONO_ADC_MIXER,
 509                        RT5640_M_MONO_ADC_L1_SFT, 1, 1),
 510        SOC_DAPM_SINGLE("ADC2 Switch", RT5640_MONO_ADC_MIXER,
 511                        RT5640_M_MONO_ADC_L2_SFT, 1, 1),
 512};
 513
 514static const struct snd_kcontrol_new rt5640_mono_adc_r_mix[] = {
 515        SOC_DAPM_SINGLE("ADC1 Switch", RT5640_MONO_ADC_MIXER,
 516                        RT5640_M_MONO_ADC_R1_SFT, 1, 1),
 517        SOC_DAPM_SINGLE("ADC2 Switch", RT5640_MONO_ADC_MIXER,
 518                        RT5640_M_MONO_ADC_R2_SFT, 1, 1),
 519};
 520
 521static const struct snd_kcontrol_new rt5640_dac_l_mix[] = {
 522        SOC_DAPM_SINGLE("Stereo ADC Switch", RT5640_AD_DA_MIXER,
 523                        RT5640_M_ADCMIX_L_SFT, 1, 1),
 524        SOC_DAPM_SINGLE("INF1 Switch", RT5640_AD_DA_MIXER,
 525                        RT5640_M_IF1_DAC_L_SFT, 1, 1),
 526};
 527
 528static const struct snd_kcontrol_new rt5640_dac_r_mix[] = {
 529        SOC_DAPM_SINGLE("Stereo ADC Switch", RT5640_AD_DA_MIXER,
 530                        RT5640_M_ADCMIX_R_SFT, 1, 1),
 531        SOC_DAPM_SINGLE("INF1 Switch", RT5640_AD_DA_MIXER,
 532                        RT5640_M_IF1_DAC_R_SFT, 1, 1),
 533};
 534
 535static const struct snd_kcontrol_new rt5640_sto_dac_l_mix[] = {
 536        SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_STO_DAC_MIXER,
 537                        RT5640_M_DAC_L1_SFT, 1, 1),
 538        SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_STO_DAC_MIXER,
 539                        RT5640_M_DAC_L2_SFT, 1, 1),
 540        SOC_DAPM_SINGLE("ANC Switch", RT5640_STO_DAC_MIXER,
 541                        RT5640_M_ANC_DAC_L_SFT, 1, 1),
 542};
 543
 544static const struct snd_kcontrol_new rt5640_sto_dac_r_mix[] = {
 545        SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_STO_DAC_MIXER,
 546                        RT5640_M_DAC_R1_SFT, 1, 1),
 547        SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_STO_DAC_MIXER,
 548                        RT5640_M_DAC_R2_SFT, 1, 1),
 549        SOC_DAPM_SINGLE("ANC Switch", RT5640_STO_DAC_MIXER,
 550                        RT5640_M_ANC_DAC_R_SFT, 1, 1),
 551};
 552
 553static const struct snd_kcontrol_new rt5639_sto_dac_l_mix[] = {
 554        SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_STO_DAC_MIXER,
 555                        RT5640_M_DAC_L1_SFT, 1, 1),
 556        SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_STO_DAC_MIXER,
 557                        RT5640_M_DAC_L2_SFT, 1, 1),
 558};
 559
 560static const struct snd_kcontrol_new rt5639_sto_dac_r_mix[] = {
 561        SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_STO_DAC_MIXER,
 562                        RT5640_M_DAC_R1_SFT, 1, 1),
 563        SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_STO_DAC_MIXER,
 564                        RT5640_M_DAC_R2_SFT, 1, 1),
 565};
 566
 567static const struct snd_kcontrol_new rt5640_mono_dac_l_mix[] = {
 568        SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_MONO_DAC_MIXER,
 569                        RT5640_M_DAC_L1_MONO_L_SFT, 1, 1),
 570        SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_DAC_MIXER,
 571                        RT5640_M_DAC_L2_MONO_L_SFT, 1, 1),
 572        SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_DAC_MIXER,
 573                        RT5640_M_DAC_R2_MONO_L_SFT, 1, 1),
 574};
 575
 576static const struct snd_kcontrol_new rt5640_mono_dac_r_mix[] = {
 577        SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_MONO_DAC_MIXER,
 578                        RT5640_M_DAC_R1_MONO_R_SFT, 1, 1),
 579        SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_DAC_MIXER,
 580                        RT5640_M_DAC_R2_MONO_R_SFT, 1, 1),
 581        SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_DAC_MIXER,
 582                        RT5640_M_DAC_L2_MONO_R_SFT, 1, 1),
 583};
 584
 585static const struct snd_kcontrol_new rt5640_dig_l_mix[] = {
 586        SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_DIG_MIXER,
 587                        RT5640_M_STO_L_DAC_L_SFT, 1, 1),
 588        SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_DIG_MIXER,
 589                        RT5640_M_DAC_L2_DAC_L_SFT, 1, 1),
 590};
 591
 592static const struct snd_kcontrol_new rt5640_dig_r_mix[] = {
 593        SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_DIG_MIXER,
 594                        RT5640_M_STO_R_DAC_R_SFT, 1, 1),
 595        SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_DIG_MIXER,
 596                        RT5640_M_DAC_R2_DAC_R_SFT, 1, 1),
 597};
 598
 599/* Analog Input Mixer */
 600static const struct snd_kcontrol_new rt5640_rec_l_mix[] = {
 601        SOC_DAPM_SINGLE("HPOL Switch", RT5640_REC_L2_MIXER,
 602                        RT5640_M_HP_L_RM_L_SFT, 1, 1),
 603        SOC_DAPM_SINGLE("INL Switch", RT5640_REC_L2_MIXER,
 604                        RT5640_M_IN_L_RM_L_SFT, 1, 1),
 605        SOC_DAPM_SINGLE("BST3 Switch", RT5640_REC_L2_MIXER,
 606                        RT5640_M_BST2_RM_L_SFT, 1, 1),
 607        SOC_DAPM_SINGLE("BST2 Switch", RT5640_REC_L2_MIXER,
 608                        RT5640_M_BST4_RM_L_SFT, 1, 1),
 609        SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_L2_MIXER,
 610                        RT5640_M_BST1_RM_L_SFT, 1, 1),
 611        SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_REC_L2_MIXER,
 612                        RT5640_M_OM_L_RM_L_SFT, 1, 1),
 613};
 614
 615static const struct snd_kcontrol_new rt5640_rec_r_mix[] = {
 616        SOC_DAPM_SINGLE("HPOR Switch", RT5640_REC_R2_MIXER,
 617                        RT5640_M_HP_R_RM_R_SFT, 1, 1),
 618        SOC_DAPM_SINGLE("INR Switch", RT5640_REC_R2_MIXER,
 619                        RT5640_M_IN_R_RM_R_SFT, 1, 1),
 620        SOC_DAPM_SINGLE("BST3 Switch", RT5640_REC_R2_MIXER,
 621                        RT5640_M_BST2_RM_R_SFT, 1, 1),
 622        SOC_DAPM_SINGLE("BST2 Switch", RT5640_REC_R2_MIXER,
 623                        RT5640_M_BST4_RM_R_SFT, 1, 1),
 624        SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_R2_MIXER,
 625                        RT5640_M_BST1_RM_R_SFT, 1, 1),
 626        SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_REC_R2_MIXER,
 627                        RT5640_M_OM_R_RM_R_SFT, 1, 1),
 628};
 629
 630/* Analog Output Mixer */
 631static const struct snd_kcontrol_new rt5640_spk_l_mix[] = {
 632        SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_SPK_L_MIXER,
 633                        RT5640_M_RM_L_SM_L_SFT, 1, 1),
 634        SOC_DAPM_SINGLE("INL Switch", RT5640_SPK_L_MIXER,
 635                        RT5640_M_IN_L_SM_L_SFT, 1, 1),
 636        SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPK_L_MIXER,
 637                        RT5640_M_DAC_L1_SM_L_SFT, 1, 1),
 638        SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_SPK_L_MIXER,
 639                        RT5640_M_DAC_L2_SM_L_SFT, 1, 1),
 640        SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_SPK_L_MIXER,
 641                        RT5640_M_OM_L_SM_L_SFT, 1, 1),
 642};
 643
 644static const struct snd_kcontrol_new rt5640_spk_r_mix[] = {
 645        SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_SPK_R_MIXER,
 646                        RT5640_M_RM_R_SM_R_SFT, 1, 1),
 647        SOC_DAPM_SINGLE("INR Switch", RT5640_SPK_R_MIXER,
 648                        RT5640_M_IN_R_SM_R_SFT, 1, 1),
 649        SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPK_R_MIXER,
 650                        RT5640_M_DAC_R1_SM_R_SFT, 1, 1),
 651        SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_SPK_R_MIXER,
 652                        RT5640_M_DAC_R2_SM_R_SFT, 1, 1),
 653        SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_SPK_R_MIXER,
 654                        RT5640_M_OM_R_SM_R_SFT, 1, 1),
 655};
 656
 657static const struct snd_kcontrol_new rt5640_out_l_mix[] = {
 658        SOC_DAPM_SINGLE("SPK MIXL Switch", RT5640_OUT_L3_MIXER,
 659                        RT5640_M_SM_L_OM_L_SFT, 1, 1),
 660        SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_L3_MIXER,
 661                        RT5640_M_BST1_OM_L_SFT, 1, 1),
 662        SOC_DAPM_SINGLE("INL Switch", RT5640_OUT_L3_MIXER,
 663                        RT5640_M_IN_L_OM_L_SFT, 1, 1),
 664        SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_OUT_L3_MIXER,
 665                        RT5640_M_RM_L_OM_L_SFT, 1, 1),
 666        SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_L3_MIXER,
 667                        RT5640_M_DAC_R2_OM_L_SFT, 1, 1),
 668        SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_L3_MIXER,
 669                        RT5640_M_DAC_L2_OM_L_SFT, 1, 1),
 670        SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_OUT_L3_MIXER,
 671                        RT5640_M_DAC_L1_OM_L_SFT, 1, 1),
 672};
 673
 674static const struct snd_kcontrol_new rt5640_out_r_mix[] = {
 675        SOC_DAPM_SINGLE("SPK MIXR Switch", RT5640_OUT_R3_MIXER,
 676                        RT5640_M_SM_L_OM_R_SFT, 1, 1),
 677        SOC_DAPM_SINGLE("BST2 Switch", RT5640_OUT_R3_MIXER,
 678                        RT5640_M_BST4_OM_R_SFT, 1, 1),
 679        SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_R3_MIXER,
 680                        RT5640_M_BST1_OM_R_SFT, 1, 1),
 681        SOC_DAPM_SINGLE("INR Switch", RT5640_OUT_R3_MIXER,
 682                        RT5640_M_IN_R_OM_R_SFT, 1, 1),
 683        SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_OUT_R3_MIXER,
 684                        RT5640_M_RM_R_OM_R_SFT, 1, 1),
 685        SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_R3_MIXER,
 686                        RT5640_M_DAC_L2_OM_R_SFT, 1, 1),
 687        SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_R3_MIXER,
 688                        RT5640_M_DAC_R2_OM_R_SFT, 1, 1),
 689        SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_OUT_R3_MIXER,
 690                        RT5640_M_DAC_R1_OM_R_SFT, 1, 1),
 691};
 692
 693static const struct snd_kcontrol_new rt5639_out_l_mix[] = {
 694        SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_L3_MIXER,
 695                        RT5640_M_BST1_OM_L_SFT, 1, 1),
 696        SOC_DAPM_SINGLE("INL Switch", RT5640_OUT_L3_MIXER,
 697                        RT5640_M_IN_L_OM_L_SFT, 1, 1),
 698        SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_OUT_L3_MIXER,
 699                        RT5640_M_RM_L_OM_L_SFT, 1, 1),
 700        SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_OUT_L3_MIXER,
 701                        RT5640_M_DAC_L1_OM_L_SFT, 1, 1),
 702};
 703
 704static const struct snd_kcontrol_new rt5639_out_r_mix[] = {
 705        SOC_DAPM_SINGLE("BST2 Switch", RT5640_OUT_R3_MIXER,
 706                        RT5640_M_BST4_OM_R_SFT, 1, 1),
 707        SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_R3_MIXER,
 708                        RT5640_M_BST1_OM_R_SFT, 1, 1),
 709        SOC_DAPM_SINGLE("INR Switch", RT5640_OUT_R3_MIXER,
 710                        RT5640_M_IN_R_OM_R_SFT, 1, 1),
 711        SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_OUT_R3_MIXER,
 712                        RT5640_M_RM_R_OM_R_SFT, 1, 1),
 713        SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_OUT_R3_MIXER,
 714                        RT5640_M_DAC_R1_OM_R_SFT, 1, 1),
 715};
 716
 717static const struct snd_kcontrol_new rt5640_spo_l_mix[] = {
 718        SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_L_MIXER,
 719                        RT5640_M_DAC_R1_SPM_L_SFT, 1, 1),
 720        SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPO_L_MIXER,
 721                        RT5640_M_DAC_L1_SPM_L_SFT, 1, 1),
 722        SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_L_MIXER,
 723                        RT5640_M_SV_R_SPM_L_SFT, 1, 1),
 724        SOC_DAPM_SINGLE("SPKVOL L Switch", RT5640_SPO_L_MIXER,
 725                        RT5640_M_SV_L_SPM_L_SFT, 1, 1),
 726        SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_L_MIXER,
 727                        RT5640_M_BST1_SPM_L_SFT, 1, 1),
 728};
 729
 730static const struct snd_kcontrol_new rt5640_spo_r_mix[] = {
 731        SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_R_MIXER,
 732                        RT5640_M_DAC_R1_SPM_R_SFT, 1, 1),
 733        SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_R_MIXER,
 734                        RT5640_M_SV_R_SPM_R_SFT, 1, 1),
 735        SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_R_MIXER,
 736                        RT5640_M_BST1_SPM_R_SFT, 1, 1),
 737};
 738
 739static const struct snd_kcontrol_new rt5640_hpo_mix[] = {
 740        SOC_DAPM_SINGLE("HPO MIX DAC2 Switch", RT5640_HPO_MIXER,
 741                        RT5640_M_DAC2_HM_SFT, 1, 1),
 742        SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5640_HPO_MIXER,
 743                        RT5640_M_DAC1_HM_SFT, 1, 1),
 744        SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5640_HPO_MIXER,
 745                        RT5640_M_HPVOL_HM_SFT, 1, 1),
 746};
 747
 748static const struct snd_kcontrol_new rt5639_hpo_mix[] = {
 749        SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5640_HPO_MIXER,
 750                        RT5640_M_DAC1_HM_SFT, 1, 1),
 751        SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5640_HPO_MIXER,
 752                        RT5640_M_HPVOL_HM_SFT, 1, 1),
 753};
 754
 755static const struct snd_kcontrol_new rt5640_lout_mix[] = {
 756        SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_LOUT_MIXER,
 757                        RT5640_M_DAC_L1_LM_SFT, 1, 1),
 758        SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_LOUT_MIXER,
 759                        RT5640_M_DAC_R1_LM_SFT, 1, 1),
 760        SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_LOUT_MIXER,
 761                        RT5640_M_OV_L_LM_SFT, 1, 1),
 762        SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_LOUT_MIXER,
 763                        RT5640_M_OV_R_LM_SFT, 1, 1),
 764};
 765
 766static const struct snd_kcontrol_new rt5640_mono_mix[] = {
 767        SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_MIXER,
 768                        RT5640_M_DAC_R2_MM_SFT, 1, 1),
 769        SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_MIXER,
 770                        RT5640_M_DAC_L2_MM_SFT, 1, 1),
 771        SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_MONO_MIXER,
 772                        RT5640_M_OV_R_MM_SFT, 1, 1),
 773        SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_MONO_MIXER,
 774                        RT5640_M_OV_L_MM_SFT, 1, 1),
 775        SOC_DAPM_SINGLE("BST1 Switch", RT5640_MONO_MIXER,
 776                        RT5640_M_BST1_MM_SFT, 1, 1),
 777};
 778
 779static const struct snd_kcontrol_new spk_l_enable_control =
 780        SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_SPK_VOL,
 781                RT5640_L_MUTE_SFT, 1, 1);
 782
 783static const struct snd_kcontrol_new spk_r_enable_control =
 784        SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_SPK_VOL,
 785                RT5640_R_MUTE_SFT, 1, 1);
 786
 787static const struct snd_kcontrol_new hp_l_enable_control =
 788        SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_HP_VOL,
 789                RT5640_L_MUTE_SFT, 1, 1);
 790
 791static const struct snd_kcontrol_new hp_r_enable_control =
 792        SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_HP_VOL,
 793                RT5640_R_MUTE_SFT, 1, 1);
 794
 795/* Stereo ADC source */
 796static const char * const rt5640_stereo_adc1_src[] = {
 797        "DIG MIX", "ADC"
 798};
 799
 800static SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc1_enum, RT5640_STO_ADC_MIXER,
 801                            RT5640_ADC_1_SRC_SFT, rt5640_stereo_adc1_src);
 802
 803static const struct snd_kcontrol_new rt5640_sto_adc_1_mux =
 804        SOC_DAPM_ENUM("Stereo ADC1 Mux", rt5640_stereo_adc1_enum);
 805
 806static const char * const rt5640_stereo_adc2_src[] = {
 807        "DMIC1", "DMIC2", "DIG MIX"
 808};
 809
 810static SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc2_enum, RT5640_STO_ADC_MIXER,
 811                            RT5640_ADC_2_SRC_SFT, rt5640_stereo_adc2_src);
 812
 813static const struct snd_kcontrol_new rt5640_sto_adc_2_mux =
 814        SOC_DAPM_ENUM("Stereo ADC2 Mux", rt5640_stereo_adc2_enum);
 815
 816/* Mono ADC source */
 817static const char * const rt5640_mono_adc_l1_src[] = {
 818        "Mono DAC MIXL", "ADCL"
 819};
 820
 821static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_l1_enum, RT5640_MONO_ADC_MIXER,
 822                            RT5640_MONO_ADC_L1_SRC_SFT, rt5640_mono_adc_l1_src);
 823
 824static const struct snd_kcontrol_new rt5640_mono_adc_l1_mux =
 825        SOC_DAPM_ENUM("Mono ADC1 left source", rt5640_mono_adc_l1_enum);
 826
 827static const char * const rt5640_mono_adc_l2_src[] = {
 828        "DMIC L1", "DMIC L2", "Mono DAC MIXL"
 829};
 830
 831static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_l2_enum, RT5640_MONO_ADC_MIXER,
 832                            RT5640_MONO_ADC_L2_SRC_SFT, rt5640_mono_adc_l2_src);
 833
 834static const struct snd_kcontrol_new rt5640_mono_adc_l2_mux =
 835        SOC_DAPM_ENUM("Mono ADC2 left source", rt5640_mono_adc_l2_enum);
 836
 837static const char * const rt5640_mono_adc_r1_src[] = {
 838        "Mono DAC MIXR", "ADCR"
 839};
 840
 841static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_r1_enum, RT5640_MONO_ADC_MIXER,
 842                            RT5640_MONO_ADC_R1_SRC_SFT, rt5640_mono_adc_r1_src);
 843
 844static const struct snd_kcontrol_new rt5640_mono_adc_r1_mux =
 845        SOC_DAPM_ENUM("Mono ADC1 right source", rt5640_mono_adc_r1_enum);
 846
 847static const char * const rt5640_mono_adc_r2_src[] = {
 848        "DMIC R1", "DMIC R2", "Mono DAC MIXR"
 849};
 850
 851static SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_r2_enum, RT5640_MONO_ADC_MIXER,
 852                            RT5640_MONO_ADC_R2_SRC_SFT, rt5640_mono_adc_r2_src);
 853
 854static const struct snd_kcontrol_new rt5640_mono_adc_r2_mux =
 855        SOC_DAPM_ENUM("Mono ADC2 right source", rt5640_mono_adc_r2_enum);
 856
 857/* DAC2 channel source */
 858static const char * const rt5640_dac_l2_src[] = {
 859        "IF2", "Base L/R"
 860};
 861
 862static int rt5640_dac_l2_values[] = {
 863        0,
 864        3,
 865};
 866
 867static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dac_l2_enum,
 868                                  RT5640_DSP_PATH2, RT5640_DAC_L2_SEL_SFT,
 869                                  0x3, rt5640_dac_l2_src, rt5640_dac_l2_values);
 870
 871static const struct snd_kcontrol_new rt5640_dac_l2_mux =
 872        SOC_DAPM_ENUM("DAC2 left channel source", rt5640_dac_l2_enum);
 873
 874static const char * const rt5640_dac_r2_src[] = {
 875        "IF2",
 876};
 877
 878static int rt5640_dac_r2_values[] = {
 879        0,
 880};
 881
 882static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dac_r2_enum,
 883                                  RT5640_DSP_PATH2, RT5640_DAC_R2_SEL_SFT,
 884                                  0x3, rt5640_dac_r2_src, rt5640_dac_r2_values);
 885
 886static const struct snd_kcontrol_new rt5640_dac_r2_mux =
 887        SOC_DAPM_ENUM("DAC2 right channel source", rt5640_dac_r2_enum);
 888
 889/* digital interface and iis interface map */
 890static const char * const rt5640_dai_iis_map[] = {
 891        "1:1|2:2", "1:2|2:1", "1:1|2:1", "1:2|2:2"
 892};
 893
 894static int rt5640_dai_iis_map_values[] = {
 895        0,
 896        5,
 897        6,
 898        7,
 899};
 900
 901static SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dai_iis_map_enum,
 902                                  RT5640_I2S1_SDP, RT5640_I2S_IF_SFT,
 903                                  0x7, rt5640_dai_iis_map,
 904                                  rt5640_dai_iis_map_values);
 905
 906static const struct snd_kcontrol_new rt5640_dai_mux =
 907        SOC_DAPM_ENUM("DAI select", rt5640_dai_iis_map_enum);
 908
 909/* SDI select */
 910static const char * const rt5640_sdi_sel[] = {
 911        "IF1", "IF2"
 912};
 913
 914static SOC_ENUM_SINGLE_DECL(rt5640_sdi_sel_enum, RT5640_I2S2_SDP,
 915                            RT5640_I2S2_SDI_SFT, rt5640_sdi_sel);
 916
 917static const struct snd_kcontrol_new rt5640_sdi_mux =
 918        SOC_DAPM_ENUM("SDI select", rt5640_sdi_sel_enum);
 919
 920static void hp_amp_power_on(struct snd_soc_component *component)
 921{
 922        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
 923
 924        /* depop parameters */
 925        regmap_update_bits(rt5640->regmap, RT5640_PR_BASE +
 926                RT5640_CHPUMP_INT_REG1, 0x0700, 0x0200);
 927        regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M2,
 928                RT5640_DEPOP_MASK, RT5640_DEPOP_MAN);
 929        regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M1,
 930                RT5640_HP_CP_MASK | RT5640_HP_SG_MASK | RT5640_HP_CB_MASK,
 931                RT5640_HP_CP_PU | RT5640_HP_SG_DIS | RT5640_HP_CB_PU);
 932        regmap_write(rt5640->regmap, RT5640_PR_BASE + RT5640_HP_DCC_INT1,
 933                           0x9f00);
 934        /* headphone amp power on */
 935        regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
 936                RT5640_PWR_FV1 | RT5640_PWR_FV2, 0);
 937        regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
 938                RT5640_PWR_HA,
 939                RT5640_PWR_HA);
 940        usleep_range(10000, 15000);
 941        regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
 942                RT5640_PWR_FV1 | RT5640_PWR_FV2 ,
 943                RT5640_PWR_FV1 | RT5640_PWR_FV2);
 944}
 945
 946static void rt5640_pmu_depop(struct snd_soc_component *component)
 947{
 948        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
 949
 950        regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M2,
 951                RT5640_DEPOP_MASK | RT5640_DIG_DP_MASK,
 952                RT5640_DEPOP_AUTO | RT5640_DIG_DP_EN);
 953        regmap_update_bits(rt5640->regmap, RT5640_CHARGE_PUMP,
 954                RT5640_PM_HP_MASK, RT5640_PM_HP_HV);
 955
 956        regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M3,
 957                RT5640_CP_FQ1_MASK | RT5640_CP_FQ2_MASK | RT5640_CP_FQ3_MASK,
 958                (RT5640_CP_FQ_192_KHZ << RT5640_CP_FQ1_SFT) |
 959                (RT5640_CP_FQ_12_KHZ << RT5640_CP_FQ2_SFT) |
 960                (RT5640_CP_FQ_192_KHZ << RT5640_CP_FQ3_SFT));
 961
 962        regmap_write(rt5640->regmap, RT5640_PR_BASE +
 963                RT5640_MAMP_INT_REG2, 0x1c00);
 964        regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M1,
 965                RT5640_HP_CP_MASK | RT5640_HP_SG_MASK,
 966                RT5640_HP_CP_PD | RT5640_HP_SG_EN);
 967        regmap_update_bits(rt5640->regmap, RT5640_PR_BASE +
 968                RT5640_CHPUMP_INT_REG1, 0x0700, 0x0400);
 969}
 970
 971static int rt5640_hp_event(struct snd_soc_dapm_widget *w,
 972                           struct snd_kcontrol *kcontrol, int event)
 973{
 974        struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
 975        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
 976
 977        switch (event) {
 978        case SND_SOC_DAPM_POST_PMU:
 979                rt5640_pmu_depop(component);
 980                rt5640->hp_mute = 0;
 981                break;
 982
 983        case SND_SOC_DAPM_PRE_PMD:
 984                rt5640->hp_mute = 1;
 985                msleep(70);
 986                break;
 987
 988        default:
 989                return 0;
 990        }
 991
 992        return 0;
 993}
 994
 995static int rt5640_lout_event(struct snd_soc_dapm_widget *w,
 996        struct snd_kcontrol *kcontrol, int event)
 997{
 998        struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
 999
1000        switch (event) {
1001        case SND_SOC_DAPM_POST_PMU:
1002                hp_amp_power_on(component);
1003                snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
1004                        RT5640_PWR_LM, RT5640_PWR_LM);
1005                snd_soc_component_update_bits(component, RT5640_OUTPUT,
1006                        RT5640_L_MUTE | RT5640_R_MUTE, 0);
1007                break;
1008
1009        case SND_SOC_DAPM_PRE_PMD:
1010                snd_soc_component_update_bits(component, RT5640_OUTPUT,
1011                        RT5640_L_MUTE | RT5640_R_MUTE,
1012                        RT5640_L_MUTE | RT5640_R_MUTE);
1013                snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
1014                        RT5640_PWR_LM, 0);
1015                break;
1016
1017        default:
1018                return 0;
1019        }
1020
1021        return 0;
1022}
1023
1024static int rt5640_hp_power_event(struct snd_soc_dapm_widget *w,
1025                           struct snd_kcontrol *kcontrol, int event)
1026{
1027        struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1028
1029        switch (event) {
1030        case SND_SOC_DAPM_POST_PMU:
1031                hp_amp_power_on(component);
1032                break;
1033        default:
1034                return 0;
1035        }
1036
1037        return 0;
1038}
1039
1040static int rt5640_hp_post_event(struct snd_soc_dapm_widget *w,
1041                           struct snd_kcontrol *kcontrol, int event)
1042{
1043        struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1044        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
1045
1046        switch (event) {
1047        case SND_SOC_DAPM_POST_PMU:
1048                if (!rt5640->hp_mute)
1049                        msleep(80);
1050
1051                break;
1052
1053        default:
1054                return 0;
1055        }
1056
1057        return 0;
1058}
1059
1060static const struct snd_soc_dapm_widget rt5640_dapm_widgets[] = {
1061        /* ASRC */
1062        SND_SOC_DAPM_SUPPLY_S("Stereo Filter ASRC", 1, RT5640_ASRC_1,
1063                         15, 0, NULL, 0),
1064        SND_SOC_DAPM_SUPPLY_S("I2S2 Filter ASRC", 1, RT5640_ASRC_1,
1065                         12, 0, NULL, 0),
1066        SND_SOC_DAPM_SUPPLY_S("I2S2 ASRC", 1, RT5640_ASRC_1,
1067                         11, 0, NULL, 0),
1068        SND_SOC_DAPM_SUPPLY_S("DMIC1 ASRC", 1, RT5640_ASRC_1,
1069                         9, 0, NULL, 0),
1070        SND_SOC_DAPM_SUPPLY_S("DMIC2 ASRC", 1, RT5640_ASRC_1,
1071                         8, 0, NULL, 0),
1072
1073
1074        /* Input Side */
1075        /* micbias */
1076        SND_SOC_DAPM_SUPPLY("LDO2", RT5640_PWR_ANLG1,
1077                        RT5640_PWR_LDO2_BIT, 0, NULL, 0),
1078        SND_SOC_DAPM_SUPPLY("MICBIAS1", RT5640_PWR_ANLG2,
1079                        RT5640_PWR_MB1_BIT, 0, NULL, 0),
1080        /* Input Lines */
1081        SND_SOC_DAPM_INPUT("DMIC1"),
1082        SND_SOC_DAPM_INPUT("DMIC2"),
1083        SND_SOC_DAPM_INPUT("IN1P"),
1084        SND_SOC_DAPM_INPUT("IN1N"),
1085        SND_SOC_DAPM_INPUT("IN2P"),
1086        SND_SOC_DAPM_INPUT("IN2N"),
1087        SND_SOC_DAPM_INPUT("IN3P"),
1088        SND_SOC_DAPM_INPUT("IN3N"),
1089        SND_SOC_DAPM_PGA("DMIC L1", SND_SOC_NOPM, 0, 0, NULL, 0),
1090        SND_SOC_DAPM_PGA("DMIC R1", SND_SOC_NOPM, 0, 0, NULL, 0),
1091        SND_SOC_DAPM_PGA("DMIC L2", SND_SOC_NOPM, 0, 0, NULL, 0),
1092        SND_SOC_DAPM_PGA("DMIC R2", SND_SOC_NOPM, 0, 0, NULL, 0),
1093
1094        SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
1095                set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
1096        SND_SOC_DAPM_SUPPLY("DMIC1 Power", RT5640_DMIC, RT5640_DMIC_1_EN_SFT, 0,
1097                NULL, 0),
1098        SND_SOC_DAPM_SUPPLY("DMIC2 Power", RT5640_DMIC, RT5640_DMIC_2_EN_SFT, 0,
1099                NULL, 0),
1100        /* Boost */
1101        SND_SOC_DAPM_PGA("BST1", RT5640_PWR_ANLG2,
1102                RT5640_PWR_BST1_BIT, 0, NULL, 0),
1103        SND_SOC_DAPM_PGA("BST2", RT5640_PWR_ANLG2,
1104                RT5640_PWR_BST4_BIT, 0, NULL, 0),
1105        SND_SOC_DAPM_PGA("BST3", RT5640_PWR_ANLG2,
1106                RT5640_PWR_BST2_BIT, 0, NULL, 0),
1107        /* Input Volume */
1108        SND_SOC_DAPM_PGA("INL VOL", RT5640_PWR_VOL,
1109                RT5640_PWR_IN_L_BIT, 0, NULL, 0),
1110        SND_SOC_DAPM_PGA("INR VOL", RT5640_PWR_VOL,
1111                RT5640_PWR_IN_R_BIT, 0, NULL, 0),
1112        /* REC Mixer */
1113        SND_SOC_DAPM_MIXER("RECMIXL", RT5640_PWR_MIXER, RT5640_PWR_RM_L_BIT, 0,
1114                        rt5640_rec_l_mix, ARRAY_SIZE(rt5640_rec_l_mix)),
1115        SND_SOC_DAPM_MIXER("RECMIXR", RT5640_PWR_MIXER, RT5640_PWR_RM_R_BIT, 0,
1116                        rt5640_rec_r_mix, ARRAY_SIZE(rt5640_rec_r_mix)),
1117        /* ADCs */
1118        SND_SOC_DAPM_ADC("ADC L", NULL, RT5640_PWR_DIG1,
1119                        RT5640_PWR_ADC_L_BIT, 0),
1120        SND_SOC_DAPM_ADC("ADC R", NULL, RT5640_PWR_DIG1,
1121                        RT5640_PWR_ADC_R_BIT, 0),
1122        /* ADC Mux */
1123        SND_SOC_DAPM_MUX("Stereo ADC L2 Mux", SND_SOC_NOPM, 0, 0,
1124                                &rt5640_sto_adc_2_mux),
1125        SND_SOC_DAPM_MUX("Stereo ADC R2 Mux", SND_SOC_NOPM, 0, 0,
1126                                &rt5640_sto_adc_2_mux),
1127        SND_SOC_DAPM_MUX("Stereo ADC L1 Mux", SND_SOC_NOPM, 0, 0,
1128                                &rt5640_sto_adc_1_mux),
1129        SND_SOC_DAPM_MUX("Stereo ADC R1 Mux", SND_SOC_NOPM, 0, 0,
1130                                &rt5640_sto_adc_1_mux),
1131        SND_SOC_DAPM_MUX("Mono ADC L2 Mux", SND_SOC_NOPM, 0, 0,
1132                                &rt5640_mono_adc_l2_mux),
1133        SND_SOC_DAPM_MUX("Mono ADC L1 Mux", SND_SOC_NOPM, 0, 0,
1134                                &rt5640_mono_adc_l1_mux),
1135        SND_SOC_DAPM_MUX("Mono ADC R1 Mux", SND_SOC_NOPM, 0, 0,
1136                                &rt5640_mono_adc_r1_mux),
1137        SND_SOC_DAPM_MUX("Mono ADC R2 Mux", SND_SOC_NOPM, 0, 0,
1138                                &rt5640_mono_adc_r2_mux),
1139        /* ADC Mixer */
1140        SND_SOC_DAPM_SUPPLY("Stereo Filter", RT5640_PWR_DIG2,
1141                RT5640_PWR_ADC_SF_BIT, 0, NULL, 0),
1142        SND_SOC_DAPM_MIXER("Stereo ADC MIXL", SND_SOC_NOPM, 0, 0,
1143                rt5640_sto_adc_l_mix, ARRAY_SIZE(rt5640_sto_adc_l_mix)),
1144        SND_SOC_DAPM_MIXER("Stereo ADC MIXR", SND_SOC_NOPM, 0, 0,
1145                rt5640_sto_adc_r_mix, ARRAY_SIZE(rt5640_sto_adc_r_mix)),
1146        SND_SOC_DAPM_SUPPLY("Mono Left Filter", RT5640_PWR_DIG2,
1147                RT5640_PWR_ADC_MF_L_BIT, 0, NULL, 0),
1148        SND_SOC_DAPM_MIXER("Mono ADC MIXL", SND_SOC_NOPM, 0, 0,
1149                rt5640_mono_adc_l_mix, ARRAY_SIZE(rt5640_mono_adc_l_mix)),
1150        SND_SOC_DAPM_SUPPLY("Mono Right Filter", RT5640_PWR_DIG2,
1151                RT5640_PWR_ADC_MF_R_BIT, 0, NULL, 0),
1152        SND_SOC_DAPM_MIXER("Mono ADC MIXR", SND_SOC_NOPM, 0, 0,
1153                rt5640_mono_adc_r_mix, ARRAY_SIZE(rt5640_mono_adc_r_mix)),
1154
1155        /* Digital Interface */
1156        SND_SOC_DAPM_SUPPLY("I2S1", RT5640_PWR_DIG1,
1157                RT5640_PWR_I2S1_BIT, 0, NULL, 0),
1158        SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
1159        SND_SOC_DAPM_PGA("IF1 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1160        SND_SOC_DAPM_PGA("IF1 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1161        SND_SOC_DAPM_PGA("IF1 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
1162        SND_SOC_DAPM_PGA("IF1 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1163        SND_SOC_DAPM_PGA("IF1 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1164        SND_SOC_DAPM_SUPPLY("I2S2", RT5640_PWR_DIG1,
1165                RT5640_PWR_I2S2_BIT, 0, NULL, 0),
1166        SND_SOC_DAPM_PGA("IF2 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
1167        SND_SOC_DAPM_PGA("IF2 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1168        SND_SOC_DAPM_PGA("IF2 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1169        SND_SOC_DAPM_PGA("IF2 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
1170        SND_SOC_DAPM_PGA("IF2 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1171        SND_SOC_DAPM_PGA("IF2 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1172        /* Digital Interface Select */
1173        SND_SOC_DAPM_MUX("DAI1 RX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1174        SND_SOC_DAPM_MUX("DAI1 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1175        SND_SOC_DAPM_MUX("DAI1 IF1 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1176        SND_SOC_DAPM_MUX("DAI1 IF2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1177        SND_SOC_DAPM_MUX("SDI1 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_sdi_mux),
1178        SND_SOC_DAPM_MUX("DAI2 RX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1179        SND_SOC_DAPM_MUX("DAI2 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1180        SND_SOC_DAPM_MUX("DAI2 IF1 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1181        SND_SOC_DAPM_MUX("DAI2 IF2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1182        SND_SOC_DAPM_MUX("SDI2 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_sdi_mux),
1183        /* Audio Interface */
1184        SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1185        SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
1186        SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
1187        SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
1188
1189        /* Output Side */
1190        /* DAC mixer before sound effect  */
1191        SND_SOC_DAPM_MIXER("DAC MIXL", SND_SOC_NOPM, 0, 0,
1192                rt5640_dac_l_mix, ARRAY_SIZE(rt5640_dac_l_mix)),
1193        SND_SOC_DAPM_MIXER("DAC MIXR", SND_SOC_NOPM, 0, 0,
1194                rt5640_dac_r_mix, ARRAY_SIZE(rt5640_dac_r_mix)),
1195
1196        /* DAC Mixer */
1197        SND_SOC_DAPM_MIXER("Mono DAC MIXL", SND_SOC_NOPM, 0, 0,
1198                rt5640_mono_dac_l_mix, ARRAY_SIZE(rt5640_mono_dac_l_mix)),
1199        SND_SOC_DAPM_MIXER("Mono DAC MIXR", SND_SOC_NOPM, 0, 0,
1200                rt5640_mono_dac_r_mix, ARRAY_SIZE(rt5640_mono_dac_r_mix)),
1201        SND_SOC_DAPM_MIXER("DIG MIXL", SND_SOC_NOPM, 0, 0,
1202                rt5640_dig_l_mix, ARRAY_SIZE(rt5640_dig_l_mix)),
1203        SND_SOC_DAPM_MIXER("DIG MIXR", SND_SOC_NOPM, 0, 0,
1204                rt5640_dig_r_mix, ARRAY_SIZE(rt5640_dig_r_mix)),
1205        /* DACs */
1206        SND_SOC_DAPM_DAC("DAC L1", NULL, SND_SOC_NOPM,
1207                        0, 0),
1208        SND_SOC_DAPM_DAC("DAC R1", NULL, SND_SOC_NOPM,
1209                        0, 0),
1210        SND_SOC_DAPM_SUPPLY("DAC L1 Power", RT5640_PWR_DIG1,
1211                RT5640_PWR_DAC_L1_BIT, 0, NULL, 0),
1212        SND_SOC_DAPM_SUPPLY("DAC R1 Power", RT5640_PWR_DIG1,
1213                RT5640_PWR_DAC_R1_BIT, 0, NULL, 0),
1214        SND_SOC_DAPM_SUPPLY("DAC L2 Power", RT5640_PWR_DIG1,
1215                RT5640_PWR_DAC_L2_BIT, 0, NULL, 0),
1216        SND_SOC_DAPM_SUPPLY("DAC R2 Power", RT5640_PWR_DIG1,
1217                RT5640_PWR_DAC_R2_BIT, 0, NULL, 0),
1218        /* SPK/OUT Mixer */
1219        SND_SOC_DAPM_MIXER("SPK MIXL", RT5640_PWR_MIXER, RT5640_PWR_SM_L_BIT,
1220                0, rt5640_spk_l_mix, ARRAY_SIZE(rt5640_spk_l_mix)),
1221        SND_SOC_DAPM_MIXER("SPK MIXR", RT5640_PWR_MIXER, RT5640_PWR_SM_R_BIT,
1222                0, rt5640_spk_r_mix, ARRAY_SIZE(rt5640_spk_r_mix)),
1223        /* Ouput Volume */
1224        SND_SOC_DAPM_PGA("SPKVOL L", RT5640_PWR_VOL,
1225                RT5640_PWR_SV_L_BIT, 0, NULL, 0),
1226        SND_SOC_DAPM_PGA("SPKVOL R", RT5640_PWR_VOL,
1227                RT5640_PWR_SV_R_BIT, 0, NULL, 0),
1228        SND_SOC_DAPM_PGA("OUTVOL L", RT5640_PWR_VOL,
1229                RT5640_PWR_OV_L_BIT, 0, NULL, 0),
1230        SND_SOC_DAPM_PGA("OUTVOL R", RT5640_PWR_VOL,
1231                RT5640_PWR_OV_R_BIT, 0, NULL, 0),
1232        SND_SOC_DAPM_PGA("HPOVOL L", RT5640_PWR_VOL,
1233                RT5640_PWR_HV_L_BIT, 0, NULL, 0),
1234        SND_SOC_DAPM_PGA("HPOVOL R", RT5640_PWR_VOL,
1235                RT5640_PWR_HV_R_BIT, 0, NULL, 0),
1236        /* SPO/HPO/LOUT/Mono Mixer */
1237        SND_SOC_DAPM_MIXER("SPOL MIX", SND_SOC_NOPM, 0,
1238                0, rt5640_spo_l_mix, ARRAY_SIZE(rt5640_spo_l_mix)),
1239        SND_SOC_DAPM_MIXER("SPOR MIX", SND_SOC_NOPM, 0,
1240                0, rt5640_spo_r_mix, ARRAY_SIZE(rt5640_spo_r_mix)),
1241        SND_SOC_DAPM_MIXER("LOUT MIX", SND_SOC_NOPM, 0, 0,
1242                rt5640_lout_mix, ARRAY_SIZE(rt5640_lout_mix)),
1243        SND_SOC_DAPM_SUPPLY_S("Improve HP Amp Drv", 1, SND_SOC_NOPM,
1244                0, 0, rt5640_hp_power_event, SND_SOC_DAPM_POST_PMU),
1245        SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0,
1246                rt5640_hp_event,
1247                SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
1248        SND_SOC_DAPM_PGA_S("LOUT amp", 1, SND_SOC_NOPM, 0, 0,
1249                rt5640_lout_event,
1250                SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
1251        SND_SOC_DAPM_SUPPLY("HP L Amp", RT5640_PWR_ANLG1,
1252                RT5640_PWR_HP_L_BIT, 0, NULL, 0),
1253        SND_SOC_DAPM_SUPPLY("HP R Amp", RT5640_PWR_ANLG1,
1254                RT5640_PWR_HP_R_BIT, 0, NULL, 0),
1255        SND_SOC_DAPM_SUPPLY("Improve SPK Amp Drv", RT5640_PWR_DIG1,
1256                RT5640_PWR_CLS_D_BIT, 0, NULL, 0),
1257
1258        /* Output Switch */
1259        SND_SOC_DAPM_SWITCH("Speaker L Playback", SND_SOC_NOPM, 0, 0,
1260                        &spk_l_enable_control),
1261        SND_SOC_DAPM_SWITCH("Speaker R Playback", SND_SOC_NOPM, 0, 0,
1262                        &spk_r_enable_control),
1263        SND_SOC_DAPM_SWITCH("HP L Playback", SND_SOC_NOPM, 0, 0,
1264                        &hp_l_enable_control),
1265        SND_SOC_DAPM_SWITCH("HP R Playback", SND_SOC_NOPM, 0, 0,
1266                        &hp_r_enable_control),
1267        SND_SOC_DAPM_POST("HP Post", rt5640_hp_post_event),
1268        /* Output Lines */
1269        SND_SOC_DAPM_OUTPUT("SPOLP"),
1270        SND_SOC_DAPM_OUTPUT("SPOLN"),
1271        SND_SOC_DAPM_OUTPUT("SPORP"),
1272        SND_SOC_DAPM_OUTPUT("SPORN"),
1273        SND_SOC_DAPM_OUTPUT("HPOL"),
1274        SND_SOC_DAPM_OUTPUT("HPOR"),
1275        SND_SOC_DAPM_OUTPUT("LOUTL"),
1276        SND_SOC_DAPM_OUTPUT("LOUTR"),
1277};
1278
1279static const struct snd_soc_dapm_widget rt5640_specific_dapm_widgets[] = {
1280        /* Audio DSP */
1281        SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),
1282        /* ANC */
1283        SND_SOC_DAPM_PGA("ANC", SND_SOC_NOPM, 0, 0, NULL, 0),
1284
1285        /* DAC2 channel Mux */
1286        SND_SOC_DAPM_MUX("DAC L2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dac_l2_mux),
1287        SND_SOC_DAPM_MUX("DAC R2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dac_r2_mux),
1288
1289        SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
1290                rt5640_sto_dac_l_mix, ARRAY_SIZE(rt5640_sto_dac_l_mix)),
1291        SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
1292                rt5640_sto_dac_r_mix, ARRAY_SIZE(rt5640_sto_dac_r_mix)),
1293
1294        SND_SOC_DAPM_DAC("DAC R2", NULL, SND_SOC_NOPM, 0,
1295                0),
1296        SND_SOC_DAPM_DAC("DAC L2", NULL, SND_SOC_NOPM, 0,
1297                0),
1298
1299        SND_SOC_DAPM_MIXER("OUT MIXL", RT5640_PWR_MIXER, RT5640_PWR_OM_L_BIT,
1300                0, rt5640_out_l_mix, ARRAY_SIZE(rt5640_out_l_mix)),
1301        SND_SOC_DAPM_MIXER("OUT MIXR", RT5640_PWR_MIXER, RT5640_PWR_OM_R_BIT,
1302                0, rt5640_out_r_mix, ARRAY_SIZE(rt5640_out_r_mix)),
1303
1304        SND_SOC_DAPM_MIXER("HPO MIX L", SND_SOC_NOPM, 0, 0,
1305                rt5640_hpo_mix, ARRAY_SIZE(rt5640_hpo_mix)),
1306        SND_SOC_DAPM_MIXER("HPO MIX R", SND_SOC_NOPM, 0, 0,
1307                rt5640_hpo_mix, ARRAY_SIZE(rt5640_hpo_mix)),
1308
1309        SND_SOC_DAPM_MIXER("Mono MIX", RT5640_PWR_ANLG1, RT5640_PWR_MM_BIT, 0,
1310                rt5640_mono_mix, ARRAY_SIZE(rt5640_mono_mix)),
1311        SND_SOC_DAPM_SUPPLY("Improve MONO Amp Drv", RT5640_PWR_ANLG1,
1312                RT5640_PWR_MA_BIT, 0, NULL, 0),
1313
1314        SND_SOC_DAPM_OUTPUT("MONOP"),
1315        SND_SOC_DAPM_OUTPUT("MONON"),
1316};
1317
1318static const struct snd_soc_dapm_widget rt5639_specific_dapm_widgets[] = {
1319        SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
1320                rt5639_sto_dac_l_mix, ARRAY_SIZE(rt5639_sto_dac_l_mix)),
1321        SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
1322                rt5639_sto_dac_r_mix, ARRAY_SIZE(rt5639_sto_dac_r_mix)),
1323
1324        SND_SOC_DAPM_MIXER("OUT MIXL", RT5640_PWR_MIXER, RT5640_PWR_OM_L_BIT,
1325                0, rt5639_out_l_mix, ARRAY_SIZE(rt5639_out_l_mix)),
1326        SND_SOC_DAPM_MIXER("OUT MIXR", RT5640_PWR_MIXER, RT5640_PWR_OM_R_BIT,
1327                0, rt5639_out_r_mix, ARRAY_SIZE(rt5639_out_r_mix)),
1328
1329        SND_SOC_DAPM_MIXER("HPO MIX L", SND_SOC_NOPM, 0, 0,
1330                rt5639_hpo_mix, ARRAY_SIZE(rt5639_hpo_mix)),
1331        SND_SOC_DAPM_MIXER("HPO MIX R", SND_SOC_NOPM, 0, 0,
1332                rt5639_hpo_mix, ARRAY_SIZE(rt5639_hpo_mix)),
1333};
1334
1335static const struct snd_soc_dapm_route rt5640_dapm_routes[] = {
1336        { "I2S1", NULL, "Stereo Filter ASRC", is_using_asrc },
1337        { "I2S2", NULL, "I2S2 ASRC", is_using_asrc },
1338        { "I2S2", NULL, "I2S2 Filter ASRC", is_using_asrc },
1339        { "DMIC1", NULL, "DMIC1 ASRC", is_using_asrc },
1340        { "DMIC2", NULL, "DMIC2 ASRC", is_using_asrc },
1341
1342        {"IN1P", NULL, "LDO2"},
1343        {"IN2P", NULL, "LDO2"},
1344        {"IN3P", NULL, "LDO2"},
1345
1346        {"DMIC L1", NULL, "DMIC1"},
1347        {"DMIC R1", NULL, "DMIC1"},
1348        {"DMIC L2", NULL, "DMIC2"},
1349        {"DMIC R2", NULL, "DMIC2"},
1350
1351        {"BST1", NULL, "IN1P"},
1352        {"BST1", NULL, "IN1N"},
1353        {"BST2", NULL, "IN2P"},
1354        {"BST2", NULL, "IN2N"},
1355        {"BST3", NULL, "IN3P"},
1356        {"BST3", NULL, "IN3N"},
1357
1358        {"INL VOL", NULL, "IN2P"},
1359        {"INR VOL", NULL, "IN2N"},
1360
1361        {"RECMIXL", "HPOL Switch", "HPOL"},
1362        {"RECMIXL", "INL Switch", "INL VOL"},
1363        {"RECMIXL", "BST3 Switch", "BST3"},
1364        {"RECMIXL", "BST2 Switch", "BST2"},
1365        {"RECMIXL", "BST1 Switch", "BST1"},
1366        {"RECMIXL", "OUT MIXL Switch", "OUT MIXL"},
1367
1368        {"RECMIXR", "HPOR Switch", "HPOR"},
1369        {"RECMIXR", "INR Switch", "INR VOL"},
1370        {"RECMIXR", "BST3 Switch", "BST3"},
1371        {"RECMIXR", "BST2 Switch", "BST2"},
1372        {"RECMIXR", "BST1 Switch", "BST1"},
1373        {"RECMIXR", "OUT MIXR Switch", "OUT MIXR"},
1374
1375        {"ADC L", NULL, "RECMIXL"},
1376        {"ADC R", NULL, "RECMIXR"},
1377
1378        {"DMIC L1", NULL, "DMIC CLK"},
1379        {"DMIC L1", NULL, "DMIC1 Power"},
1380        {"DMIC R1", NULL, "DMIC CLK"},
1381        {"DMIC R1", NULL, "DMIC1 Power"},
1382        {"DMIC L2", NULL, "DMIC CLK"},
1383        {"DMIC L2", NULL, "DMIC2 Power"},
1384        {"DMIC R2", NULL, "DMIC CLK"},
1385        {"DMIC R2", NULL, "DMIC2 Power"},
1386
1387        {"Stereo ADC L2 Mux", "DMIC1", "DMIC L1"},
1388        {"Stereo ADC L2 Mux", "DMIC2", "DMIC L2"},
1389        {"Stereo ADC L2 Mux", "DIG MIX", "DIG MIXL"},
1390        {"Stereo ADC L1 Mux", "ADC", "ADC L"},
1391        {"Stereo ADC L1 Mux", "DIG MIX", "DIG MIXL"},
1392
1393        {"Stereo ADC R1 Mux", "ADC", "ADC R"},
1394        {"Stereo ADC R1 Mux", "DIG MIX", "DIG MIXR"},
1395        {"Stereo ADC R2 Mux", "DMIC1", "DMIC R1"},
1396        {"Stereo ADC R2 Mux", "DMIC2", "DMIC R2"},
1397        {"Stereo ADC R2 Mux", "DIG MIX", "DIG MIXR"},
1398
1399        {"Mono ADC L2 Mux", "DMIC L1", "DMIC L1"},
1400        {"Mono ADC L2 Mux", "DMIC L2", "DMIC L2"},
1401        {"Mono ADC L2 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
1402        {"Mono ADC L1 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
1403        {"Mono ADC L1 Mux", "ADCL", "ADC L"},
1404
1405        {"Mono ADC R1 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
1406        {"Mono ADC R1 Mux", "ADCR", "ADC R"},
1407        {"Mono ADC R2 Mux", "DMIC R1", "DMIC R1"},
1408        {"Mono ADC R2 Mux", "DMIC R2", "DMIC R2"},
1409        {"Mono ADC R2 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
1410
1411        {"Stereo ADC MIXL", "ADC1 Switch", "Stereo ADC L1 Mux"},
1412        {"Stereo ADC MIXL", "ADC2 Switch", "Stereo ADC L2 Mux"},
1413        {"Stereo ADC MIXL", NULL, "Stereo Filter"},
1414
1415        {"Stereo ADC MIXR", "ADC1 Switch", "Stereo ADC R1 Mux"},
1416        {"Stereo ADC MIXR", "ADC2 Switch", "Stereo ADC R2 Mux"},
1417        {"Stereo ADC MIXR", NULL, "Stereo Filter"},
1418
1419        {"Mono ADC MIXL", "ADC1 Switch", "Mono ADC L1 Mux"},
1420        {"Mono ADC MIXL", "ADC2 Switch", "Mono ADC L2 Mux"},
1421        {"Mono ADC MIXL", NULL, "Mono Left Filter"},
1422
1423        {"Mono ADC MIXR", "ADC1 Switch", "Mono ADC R1 Mux"},
1424        {"Mono ADC MIXR", "ADC2 Switch", "Mono ADC R2 Mux"},
1425        {"Mono ADC MIXR", NULL, "Mono Right Filter"},
1426
1427        {"IF2 ADC L", NULL, "Mono ADC MIXL"},
1428        {"IF2 ADC R", NULL, "Mono ADC MIXR"},
1429        {"IF1 ADC L", NULL, "Stereo ADC MIXL"},
1430        {"IF1 ADC R", NULL, "Stereo ADC MIXR"},
1431
1432        {"IF1 ADC", NULL, "I2S1"},
1433        {"IF1 ADC", NULL, "IF1 ADC L"},
1434        {"IF1 ADC", NULL, "IF1 ADC R"},
1435        {"IF2 ADC", NULL, "I2S2"},
1436        {"IF2 ADC", NULL, "IF2 ADC L"},
1437        {"IF2 ADC", NULL, "IF2 ADC R"},
1438
1439        {"DAI1 TX Mux", "1:1|2:2", "IF1 ADC"},
1440        {"DAI1 TX Mux", "1:2|2:1", "IF2 ADC"},
1441        {"DAI1 IF1 Mux", "1:1|2:1", "IF1 ADC"},
1442        {"DAI1 IF2 Mux", "1:1|2:1", "IF2 ADC"},
1443        {"SDI1 TX Mux", "IF1", "DAI1 IF1 Mux"},
1444        {"SDI1 TX Mux", "IF2", "DAI1 IF2 Mux"},
1445
1446        {"DAI2 TX Mux", "1:2|2:1", "IF1 ADC"},
1447        {"DAI2 TX Mux", "1:1|2:2", "IF2 ADC"},
1448        {"DAI2 IF1 Mux", "1:2|2:2", "IF1 ADC"},
1449        {"DAI2 IF2 Mux", "1:2|2:2", "IF2 ADC"},
1450        {"SDI2 TX Mux", "IF1", "DAI2 IF1 Mux"},
1451        {"SDI2 TX Mux", "IF2", "DAI2 IF2 Mux"},
1452
1453        {"AIF1TX", NULL, "DAI1 TX Mux"},
1454        {"AIF1TX", NULL, "SDI1 TX Mux"},
1455        {"AIF2TX", NULL, "DAI2 TX Mux"},
1456        {"AIF2TX", NULL, "SDI2 TX Mux"},
1457
1458        {"DAI1 RX Mux", "1:1|2:2", "AIF1RX"},
1459        {"DAI1 RX Mux", "1:1|2:1", "AIF1RX"},
1460        {"DAI1 RX Mux", "1:2|2:1", "AIF2RX"},
1461        {"DAI1 RX Mux", "1:2|2:2", "AIF2RX"},
1462
1463        {"DAI2 RX Mux", "1:2|2:1", "AIF1RX"},
1464        {"DAI2 RX Mux", "1:1|2:1", "AIF1RX"},
1465        {"DAI2 RX Mux", "1:1|2:2", "AIF2RX"},
1466        {"DAI2 RX Mux", "1:2|2:2", "AIF2RX"},
1467
1468        {"IF1 DAC", NULL, "I2S1"},
1469        {"IF1 DAC", NULL, "DAI1 RX Mux"},
1470        {"IF2 DAC", NULL, "I2S2"},
1471        {"IF2 DAC", NULL, "DAI2 RX Mux"},
1472
1473        {"IF1 DAC L", NULL, "IF1 DAC"},
1474        {"IF1 DAC R", NULL, "IF1 DAC"},
1475        {"IF2 DAC L", NULL, "IF2 DAC"},
1476        {"IF2 DAC R", NULL, "IF2 DAC"},
1477
1478        {"DAC MIXL", "Stereo ADC Switch", "Stereo ADC MIXL"},
1479        {"DAC MIXL", "INF1 Switch", "IF1 DAC L"},
1480        {"DAC MIXL", NULL, "DAC L1 Power"},
1481        {"DAC MIXR", "Stereo ADC Switch", "Stereo ADC MIXR"},
1482        {"DAC MIXR", "INF1 Switch", "IF1 DAC R"},
1483        {"DAC MIXR", NULL, "DAC R1 Power"},
1484
1485        {"Stereo DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
1486        {"Stereo DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
1487
1488        {"Mono DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
1489        {"Mono DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
1490
1491        {"DIG MIXL", "DAC L1 Switch", "DAC MIXL"},
1492        {"DIG MIXR", "DAC R1 Switch", "DAC MIXR"},
1493
1494        {"DAC L1", NULL, "Stereo DAC MIXL"},
1495        {"DAC L1", NULL, "DAC L1 Power"},
1496        {"DAC R1", NULL, "Stereo DAC MIXR"},
1497        {"DAC R1", NULL, "DAC R1 Power"},
1498
1499        {"SPK MIXL", "REC MIXL Switch", "RECMIXL"},
1500        {"SPK MIXL", "INL Switch", "INL VOL"},
1501        {"SPK MIXL", "DAC L1 Switch", "DAC L1"},
1502        {"SPK MIXL", "OUT MIXL Switch", "OUT MIXL"},
1503        {"SPK MIXR", "REC MIXR Switch", "RECMIXR"},
1504        {"SPK MIXR", "INR Switch", "INR VOL"},
1505        {"SPK MIXR", "DAC R1 Switch", "DAC R1"},
1506        {"SPK MIXR", "OUT MIXR Switch", "OUT MIXR"},
1507
1508        {"OUT MIXL", "BST1 Switch", "BST1"},
1509        {"OUT MIXL", "INL Switch", "INL VOL"},
1510        {"OUT MIXL", "REC MIXL Switch", "RECMIXL"},
1511        {"OUT MIXL", "DAC L1 Switch", "DAC L1"},
1512
1513        {"OUT MIXR", "BST2 Switch", "BST2"},
1514        {"OUT MIXR", "BST1 Switch", "BST1"},
1515        {"OUT MIXR", "INR Switch", "INR VOL"},
1516        {"OUT MIXR", "REC MIXR Switch", "RECMIXR"},
1517        {"OUT MIXR", "DAC R1 Switch", "DAC R1"},
1518
1519        {"SPKVOL L", NULL, "SPK MIXL"},
1520        {"SPKVOL R", NULL, "SPK MIXR"},
1521        {"HPOVOL L", NULL, "OUT MIXL"},
1522        {"HPOVOL R", NULL, "OUT MIXR"},
1523        {"OUTVOL L", NULL, "OUT MIXL"},
1524        {"OUTVOL R", NULL, "OUT MIXR"},
1525
1526        {"SPOL MIX", "DAC R1 Switch", "DAC R1"},
1527        {"SPOL MIX", "DAC L1 Switch", "DAC L1"},
1528        {"SPOL MIX", "SPKVOL R Switch", "SPKVOL R"},
1529        {"SPOL MIX", "SPKVOL L Switch", "SPKVOL L"},
1530        {"SPOL MIX", "BST1 Switch", "BST1"},
1531        {"SPOR MIX", "DAC R1 Switch", "DAC R1"},
1532        {"SPOR MIX", "SPKVOL R Switch", "SPKVOL R"},
1533        {"SPOR MIX", "BST1 Switch", "BST1"},
1534
1535        {"HPO MIX L", "HPO MIX DAC1 Switch", "DAC L1"},
1536        {"HPO MIX L", "HPO MIX HPVOL Switch", "HPOVOL L"},
1537        {"HPO MIX L", NULL, "HP L Amp"},
1538        {"HPO MIX R", "HPO MIX DAC1 Switch", "DAC R1"},
1539        {"HPO MIX R", "HPO MIX HPVOL Switch", "HPOVOL R"},
1540        {"HPO MIX R", NULL, "HP R Amp"},
1541
1542        {"LOUT MIX", "DAC L1 Switch", "DAC L1"},
1543        {"LOUT MIX", "DAC R1 Switch", "DAC R1"},
1544        {"LOUT MIX", "OUTVOL L Switch", "OUTVOL L"},
1545        {"LOUT MIX", "OUTVOL R Switch", "OUTVOL R"},
1546
1547        {"HP Amp", NULL, "HPO MIX L"},
1548        {"HP Amp", NULL, "HPO MIX R"},
1549
1550        {"Speaker L Playback", "Switch", "SPOL MIX"},
1551        {"Speaker R Playback", "Switch", "SPOR MIX"},
1552        {"SPOLP", NULL, "Speaker L Playback"},
1553        {"SPOLN", NULL, "Speaker L Playback"},
1554        {"SPORP", NULL, "Speaker R Playback"},
1555        {"SPORN", NULL, "Speaker R Playback"},
1556
1557        {"SPOLP", NULL, "Improve SPK Amp Drv"},
1558        {"SPOLN", NULL, "Improve SPK Amp Drv"},
1559        {"SPORP", NULL, "Improve SPK Amp Drv"},
1560        {"SPORN", NULL, "Improve SPK Amp Drv"},
1561
1562        {"HPOL", NULL, "Improve HP Amp Drv"},
1563        {"HPOR", NULL, "Improve HP Amp Drv"},
1564
1565        {"HP L Playback", "Switch", "HP Amp"},
1566        {"HP R Playback", "Switch", "HP Amp"},
1567        {"HPOL", NULL, "HP L Playback"},
1568        {"HPOR", NULL, "HP R Playback"},
1569
1570        {"LOUT amp", NULL, "LOUT MIX"},
1571        {"LOUTL", NULL, "LOUT amp"},
1572        {"LOUTR", NULL, "LOUT amp"},
1573};
1574
1575static const struct snd_soc_dapm_route rt5640_specific_dapm_routes[] = {
1576        {"ANC", NULL, "Stereo ADC MIXL"},
1577        {"ANC", NULL, "Stereo ADC MIXR"},
1578
1579        {"Audio DSP", NULL, "DAC MIXL"},
1580        {"Audio DSP", NULL, "DAC MIXR"},
1581
1582        {"DAC L2 Mux", "IF2", "IF2 DAC L"},
1583        {"DAC L2 Mux", "Base L/R", "Audio DSP"},
1584        {"DAC L2 Mux", NULL, "DAC L2 Power"},
1585        {"DAC R2 Mux", "IF2", "IF2 DAC R"},
1586        {"DAC R2 Mux", NULL, "DAC R2 Power"},
1587
1588        {"Stereo DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1589        {"Stereo DAC MIXL", "ANC Switch", "ANC"},
1590        {"Stereo DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1591        {"Stereo DAC MIXR", "ANC Switch", "ANC"},
1592
1593        {"Mono DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1594        {"Mono DAC MIXL", "DAC R2 Switch", "DAC R2 Mux"},
1595
1596        {"Mono DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1597        {"Mono DAC MIXR", "DAC L2 Switch", "DAC L2 Mux"},
1598
1599        {"DIG MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1600        {"DIG MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1601
1602        {"DAC L2", NULL, "Mono DAC MIXL"},
1603        {"DAC L2", NULL, "DAC L2 Power"},
1604        {"DAC R2", NULL, "Mono DAC MIXR"},
1605        {"DAC R2", NULL, "DAC R2 Power"},
1606
1607        {"SPK MIXL", "DAC L2 Switch", "DAC L2"},
1608        {"SPK MIXR", "DAC R2 Switch", "DAC R2"},
1609
1610        {"OUT MIXL", "SPK MIXL Switch", "SPK MIXL"},
1611        {"OUT MIXR", "SPK MIXR Switch", "SPK MIXR"},
1612
1613        {"OUT MIXL", "DAC R2 Switch", "DAC R2"},
1614        {"OUT MIXL", "DAC L2 Switch", "DAC L2"},
1615
1616        {"OUT MIXR", "DAC L2 Switch", "DAC L2"},
1617        {"OUT MIXR", "DAC R2 Switch", "DAC R2"},
1618
1619        {"HPO MIX L", "HPO MIX DAC2 Switch", "DAC L2"},
1620        {"HPO MIX R", "HPO MIX DAC2 Switch", "DAC R2"},
1621
1622        {"Mono MIX", "DAC R2 Switch", "DAC R2"},
1623        {"Mono MIX", "DAC L2 Switch", "DAC L2"},
1624        {"Mono MIX", "OUTVOL R Switch", "OUTVOL R"},
1625        {"Mono MIX", "OUTVOL L Switch", "OUTVOL L"},
1626        {"Mono MIX", "BST1 Switch", "BST1"},
1627
1628        {"MONOP", NULL, "Mono MIX"},
1629        {"MONON", NULL, "Mono MIX"},
1630        {"MONOP", NULL, "Improve MONO Amp Drv"},
1631};
1632
1633static const struct snd_soc_dapm_route rt5639_specific_dapm_routes[] = {
1634        {"Stereo DAC MIXL", "DAC L2 Switch", "IF2 DAC L"},
1635        {"Stereo DAC MIXR", "DAC R2 Switch", "IF2 DAC R"},
1636
1637        {"Mono DAC MIXL", "DAC L2 Switch", "IF2 DAC L"},
1638        {"Mono DAC MIXL", "DAC R2 Switch", "IF2 DAC R"},
1639
1640        {"Mono DAC MIXR", "DAC R2 Switch", "IF2 DAC R"},
1641        {"Mono DAC MIXR", "DAC L2 Switch", "IF2 DAC L"},
1642
1643        {"DIG MIXL", "DAC L2 Switch", "IF2 DAC L"},
1644        {"DIG MIXR", "DAC R2 Switch", "IF2 DAC R"},
1645
1646        {"IF2 DAC L", NULL, "DAC L2 Power"},
1647        {"IF2 DAC R", NULL, "DAC R2 Power"},
1648};
1649
1650static int get_sdp_info(struct snd_soc_component *component, int dai_id)
1651{
1652        int ret = 0, val;
1653
1654        if (component == NULL)
1655                return -EINVAL;
1656
1657        val = snd_soc_component_read32(component, RT5640_I2S1_SDP);
1658        val = (val & RT5640_I2S_IF_MASK) >> RT5640_I2S_IF_SFT;
1659        switch (dai_id) {
1660        case RT5640_AIF1:
1661                switch (val) {
1662                case RT5640_IF_123:
1663                case RT5640_IF_132:
1664                        ret |= RT5640_U_IF1;
1665                        break;
1666                case RT5640_IF_113:
1667                        ret |= RT5640_U_IF1;
1668                        /* fall through */
1669                case RT5640_IF_312:
1670                case RT5640_IF_213:
1671                        ret |= RT5640_U_IF2;
1672                        break;
1673                }
1674                break;
1675
1676        case RT5640_AIF2:
1677                switch (val) {
1678                case RT5640_IF_231:
1679                case RT5640_IF_213:
1680                        ret |= RT5640_U_IF1;
1681                        break;
1682                case RT5640_IF_223:
1683                        ret |= RT5640_U_IF1;
1684                        /* fall through */
1685                case RT5640_IF_123:
1686                case RT5640_IF_321:
1687                        ret |= RT5640_U_IF2;
1688                        break;
1689                }
1690                break;
1691
1692        default:
1693                ret = -EINVAL;
1694                break;
1695        }
1696
1697        return ret;
1698}
1699
1700static int rt5640_hw_params(struct snd_pcm_substream *substream,
1701        struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
1702{
1703        struct snd_soc_component *component = dai->component;
1704        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
1705        unsigned int val_len = 0, val_clk, mask_clk;
1706        int dai_sel, pre_div, bclk_ms, frame_size;
1707
1708        rt5640->lrck[dai->id] = params_rate(params);
1709        pre_div = rl6231_get_clk_info(rt5640->sysclk, rt5640->lrck[dai->id]);
1710        if (pre_div < 0) {
1711                dev_err(component->dev, "Unsupported clock setting %d for DAI %d\n",
1712                        rt5640->lrck[dai->id], dai->id);
1713                return -EINVAL;
1714        }
1715        frame_size = snd_soc_params_to_frame_size(params);
1716        if (frame_size < 0) {
1717                dev_err(component->dev, "Unsupported frame size: %d\n", frame_size);
1718                return frame_size;
1719        }
1720        if (frame_size > 32)
1721                bclk_ms = 1;
1722        else
1723                bclk_ms = 0;
1724        rt5640->bclk[dai->id] = rt5640->lrck[dai->id] * (32 << bclk_ms);
1725
1726        dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
1727                rt5640->bclk[dai->id], rt5640->lrck[dai->id]);
1728        dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
1729                                bclk_ms, pre_div, dai->id);
1730
1731        switch (params_width(params)) {
1732        case 16:
1733                break;
1734        case 20:
1735                val_len |= RT5640_I2S_DL_20;
1736                break;
1737        case 24:
1738                val_len |= RT5640_I2S_DL_24;
1739                break;
1740        case 8:
1741                val_len |= RT5640_I2S_DL_8;
1742                break;
1743        default:
1744                return -EINVAL;
1745        }
1746
1747        dai_sel = get_sdp_info(component, dai->id);
1748        if (dai_sel < 0) {
1749                dev_err(component->dev, "Failed to get sdp info: %d\n", dai_sel);
1750                return -EINVAL;
1751        }
1752        if (dai_sel & RT5640_U_IF1) {
1753                mask_clk = RT5640_I2S_BCLK_MS1_MASK | RT5640_I2S_PD1_MASK;
1754                val_clk = bclk_ms << RT5640_I2S_BCLK_MS1_SFT |
1755                        pre_div << RT5640_I2S_PD1_SFT;
1756                snd_soc_component_update_bits(component, RT5640_I2S1_SDP,
1757                        RT5640_I2S_DL_MASK, val_len);
1758                snd_soc_component_update_bits(component, RT5640_ADDA_CLK1, mask_clk, val_clk);
1759        }
1760        if (dai_sel & RT5640_U_IF2) {
1761                mask_clk = RT5640_I2S_BCLK_MS2_MASK | RT5640_I2S_PD2_MASK;
1762                val_clk = bclk_ms << RT5640_I2S_BCLK_MS2_SFT |
1763                        pre_div << RT5640_I2S_PD2_SFT;
1764                snd_soc_component_update_bits(component, RT5640_I2S2_SDP,
1765                        RT5640_I2S_DL_MASK, val_len);
1766                snd_soc_component_update_bits(component, RT5640_ADDA_CLK1, mask_clk, val_clk);
1767        }
1768
1769        return 0;
1770}
1771
1772static int rt5640_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1773{
1774        struct snd_soc_component *component = dai->component;
1775        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
1776        unsigned int reg_val = 0;
1777        int dai_sel;
1778
1779        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1780        case SND_SOC_DAIFMT_CBM_CFM:
1781                rt5640->master[dai->id] = 1;
1782                break;
1783        case SND_SOC_DAIFMT_CBS_CFS:
1784                reg_val |= RT5640_I2S_MS_S;
1785                rt5640->master[dai->id] = 0;
1786                break;
1787        default:
1788                return -EINVAL;
1789        }
1790
1791        switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1792        case SND_SOC_DAIFMT_NB_NF:
1793                break;
1794        case SND_SOC_DAIFMT_IB_NF:
1795                reg_val |= RT5640_I2S_BP_INV;
1796                break;
1797        default:
1798                return -EINVAL;
1799        }
1800
1801        switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1802        case SND_SOC_DAIFMT_I2S:
1803                break;
1804        case SND_SOC_DAIFMT_LEFT_J:
1805                reg_val |= RT5640_I2S_DF_LEFT;
1806                break;
1807        case SND_SOC_DAIFMT_DSP_A:
1808                reg_val |= RT5640_I2S_DF_PCM_A;
1809                break;
1810        case SND_SOC_DAIFMT_DSP_B:
1811                reg_val  |= RT5640_I2S_DF_PCM_B;
1812                break;
1813        default:
1814                return -EINVAL;
1815        }
1816
1817        dai_sel = get_sdp_info(component, dai->id);
1818        if (dai_sel < 0) {
1819                dev_err(component->dev, "Failed to get sdp info: %d\n", dai_sel);
1820                return -EINVAL;
1821        }
1822        if (dai_sel & RT5640_U_IF1) {
1823                snd_soc_component_update_bits(component, RT5640_I2S1_SDP,
1824                        RT5640_I2S_MS_MASK | RT5640_I2S_BP_MASK |
1825                        RT5640_I2S_DF_MASK, reg_val);
1826        }
1827        if (dai_sel & RT5640_U_IF2) {
1828                snd_soc_component_update_bits(component, RT5640_I2S2_SDP,
1829                        RT5640_I2S_MS_MASK | RT5640_I2S_BP_MASK |
1830                        RT5640_I2S_DF_MASK, reg_val);
1831        }
1832
1833        return 0;
1834}
1835
1836static int rt5640_set_dai_sysclk(struct snd_soc_dai *dai,
1837                int clk_id, unsigned int freq, int dir)
1838{
1839        struct snd_soc_component *component = dai->component;
1840        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
1841        unsigned int reg_val = 0;
1842        unsigned int pll_bit = 0;
1843
1844        if (freq == rt5640->sysclk && clk_id == rt5640->sysclk_src)
1845                return 0;
1846
1847        switch (clk_id) {
1848        case RT5640_SCLK_S_MCLK:
1849                reg_val |= RT5640_SCLK_SRC_MCLK;
1850                break;
1851        case RT5640_SCLK_S_PLL1:
1852                reg_val |= RT5640_SCLK_SRC_PLL1;
1853                pll_bit |= RT5640_PWR_PLL;
1854                break;
1855        case RT5640_SCLK_S_RCCLK:
1856                reg_val |= RT5640_SCLK_SRC_RCCLK;
1857                break;
1858        default:
1859                dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
1860                return -EINVAL;
1861        }
1862        snd_soc_component_update_bits(component, RT5640_PWR_ANLG2,
1863                RT5640_PWR_PLL, pll_bit);
1864        snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1865                RT5640_SCLK_SRC_MASK, reg_val);
1866        rt5640->sysclk = freq;
1867        rt5640->sysclk_src = clk_id;
1868
1869        dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
1870        return 0;
1871}
1872
1873static int rt5640_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
1874                        unsigned int freq_in, unsigned int freq_out)
1875{
1876        struct snd_soc_component *component = dai->component;
1877        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
1878        struct rl6231_pll_code pll_code;
1879        int ret;
1880
1881        if (source == rt5640->pll_src && freq_in == rt5640->pll_in &&
1882            freq_out == rt5640->pll_out)
1883                return 0;
1884
1885        if (!freq_in || !freq_out) {
1886                dev_dbg(component->dev, "PLL disabled\n");
1887
1888                rt5640->pll_in = 0;
1889                rt5640->pll_out = 0;
1890                snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1891                        RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_MCLK);
1892                return 0;
1893        }
1894
1895        switch (source) {
1896        case RT5640_PLL1_S_MCLK:
1897                snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1898                        RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_MCLK);
1899                break;
1900        case RT5640_PLL1_S_BCLK1:
1901                snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1902                        RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK1);
1903                break;
1904        case RT5640_PLL1_S_BCLK2:
1905                snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1906                        RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK2);
1907                break;
1908        default:
1909                dev_err(component->dev, "Unknown PLL source %d\n", source);
1910                return -EINVAL;
1911        }
1912
1913        ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
1914        if (ret < 0) {
1915                dev_err(component->dev, "Unsupport input clock %d\n", freq_in);
1916                return ret;
1917        }
1918
1919        dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
1920                pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
1921                pll_code.n_code, pll_code.k_code);
1922
1923        snd_soc_component_write(component, RT5640_PLL_CTRL1,
1924                pll_code.n_code << RT5640_PLL_N_SFT | pll_code.k_code);
1925        snd_soc_component_write(component, RT5640_PLL_CTRL2,
1926                (pll_code.m_bp ? 0 : pll_code.m_code) << RT5640_PLL_M_SFT |
1927                pll_code.m_bp << RT5640_PLL_M_BP_SFT);
1928
1929        rt5640->pll_in = freq_in;
1930        rt5640->pll_out = freq_out;
1931        rt5640->pll_src = source;
1932
1933        return 0;
1934}
1935
1936static int rt5640_set_bias_level(struct snd_soc_component *component,
1937                        enum snd_soc_bias_level level)
1938{
1939        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
1940        int ret;
1941
1942        switch (level) {
1943        case SND_SOC_BIAS_ON:
1944                break;
1945
1946        case SND_SOC_BIAS_PREPARE:
1947                /*
1948                 * SND_SOC_BIAS_PREPARE is called while preparing for a
1949                 * transition to ON or away from ON. If current bias_level
1950                 * is SND_SOC_BIAS_ON, then it is preparing for a transition
1951                 * away from ON. Disable the clock in that case, otherwise
1952                 * enable it.
1953                 */
1954                if (IS_ERR(rt5640->mclk))
1955                        break;
1956
1957                if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_ON) {
1958                        clk_disable_unprepare(rt5640->mclk);
1959                } else {
1960                        ret = clk_prepare_enable(rt5640->mclk);
1961                        if (ret)
1962                                return ret;
1963                }
1964                break;
1965
1966        case SND_SOC_BIAS_STANDBY:
1967                if (SND_SOC_BIAS_OFF == snd_soc_component_get_bias_level(component)) {
1968                        snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
1969                                RT5640_PWR_VREF1 | RT5640_PWR_MB |
1970                                RT5640_PWR_BG | RT5640_PWR_VREF2,
1971                                RT5640_PWR_VREF1 | RT5640_PWR_MB |
1972                                RT5640_PWR_BG | RT5640_PWR_VREF2);
1973                        usleep_range(10000, 15000);
1974                        snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
1975                                RT5640_PWR_FV1 | RT5640_PWR_FV2,
1976                                RT5640_PWR_FV1 | RT5640_PWR_FV2);
1977                        snd_soc_component_update_bits(component, RT5640_DUMMY1,
1978                                                0x0301, 0x0301);
1979                        snd_soc_component_update_bits(component, RT5640_MICBIAS,
1980                                                0x0030, 0x0030);
1981                }
1982                break;
1983
1984        case SND_SOC_BIAS_OFF:
1985                snd_soc_component_write(component, RT5640_DEPOP_M1, 0x0004);
1986                snd_soc_component_write(component, RT5640_DEPOP_M2, 0x1100);
1987                snd_soc_component_update_bits(component, RT5640_DUMMY1, 0x1, 0);
1988                snd_soc_component_write(component, RT5640_PWR_DIG1, 0x0000);
1989                snd_soc_component_write(component, RT5640_PWR_DIG2, 0x0000);
1990                snd_soc_component_write(component, RT5640_PWR_VOL, 0x0000);
1991                snd_soc_component_write(component, RT5640_PWR_MIXER, 0x0000);
1992                snd_soc_component_write(component, RT5640_PWR_ANLG1, 0x0000);
1993                snd_soc_component_write(component, RT5640_PWR_ANLG2, 0x0000);
1994                break;
1995
1996        default:
1997                break;
1998        }
1999
2000        return 0;
2001}
2002
2003int rt5640_dmic_enable(struct snd_soc_component *component,
2004                       bool dmic1_data_pin, bool dmic2_data_pin)
2005{
2006        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2007
2008        regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
2009                RT5640_GP2_PIN_MASK, RT5640_GP2_PIN_DMIC1_SCL);
2010
2011        if (dmic1_data_pin) {
2012                regmap_update_bits(rt5640->regmap, RT5640_DMIC,
2013                        RT5640_DMIC_1_DP_MASK, RT5640_DMIC_1_DP_GPIO3);
2014                regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
2015                        RT5640_GP3_PIN_MASK, RT5640_GP3_PIN_DMIC1_SDA);
2016        }
2017
2018        if (dmic2_data_pin) {
2019                regmap_update_bits(rt5640->regmap, RT5640_DMIC,
2020                        RT5640_DMIC_2_DP_MASK, RT5640_DMIC_2_DP_GPIO4);
2021                regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
2022                        RT5640_GP4_PIN_MASK, RT5640_GP4_PIN_DMIC2_SDA);
2023        }
2024
2025        return 0;
2026}
2027EXPORT_SYMBOL_GPL(rt5640_dmic_enable);
2028
2029int rt5640_sel_asrc_clk_src(struct snd_soc_component *component,
2030                unsigned int filter_mask, unsigned int clk_src)
2031{
2032        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2033        unsigned int asrc2_mask = 0;
2034        unsigned int asrc2_value = 0;
2035
2036        switch (clk_src) {
2037        case RT5640_CLK_SEL_SYS:
2038        case RT5640_CLK_SEL_ASRC:
2039                break;
2040
2041        default:
2042                return -EINVAL;
2043        }
2044
2045        if (!filter_mask)
2046                return -EINVAL;
2047
2048        if (filter_mask & RT5640_DA_STEREO_FILTER) {
2049                asrc2_mask |= RT5640_STO_DAC_M_MASK;
2050                asrc2_value = (asrc2_value & ~RT5640_STO_DAC_M_MASK)
2051                        | (clk_src << RT5640_STO_DAC_M_SFT);
2052        }
2053
2054        if (filter_mask & RT5640_DA_MONO_L_FILTER) {
2055                asrc2_mask |= RT5640_MDA_L_M_MASK;
2056                asrc2_value = (asrc2_value & ~RT5640_MDA_L_M_MASK)
2057                        | (clk_src << RT5640_MDA_L_M_SFT);
2058        }
2059
2060        if (filter_mask & RT5640_DA_MONO_R_FILTER) {
2061                asrc2_mask |= RT5640_MDA_R_M_MASK;
2062                asrc2_value = (asrc2_value & ~RT5640_MDA_R_M_MASK)
2063                        | (clk_src << RT5640_MDA_R_M_SFT);
2064        }
2065
2066        if (filter_mask & RT5640_AD_STEREO_FILTER) {
2067                asrc2_mask |= RT5640_ADC_M_MASK;
2068                asrc2_value = (asrc2_value & ~RT5640_ADC_M_MASK)
2069                        | (clk_src << RT5640_ADC_M_SFT);
2070        }
2071
2072        if (filter_mask & RT5640_AD_MONO_L_FILTER) {
2073                asrc2_mask |= RT5640_MAD_L_M_MASK;
2074                asrc2_value = (asrc2_value & ~RT5640_MAD_L_M_MASK)
2075                        | (clk_src << RT5640_MAD_L_M_SFT);
2076        }
2077
2078        if (filter_mask & RT5640_AD_MONO_R_FILTER)  {
2079                asrc2_mask |= RT5640_MAD_R_M_MASK;
2080                asrc2_value = (asrc2_value & ~RT5640_MAD_R_M_MASK)
2081                        | (clk_src << RT5640_MAD_R_M_SFT);
2082        }
2083
2084        snd_soc_component_update_bits(component, RT5640_ASRC_2,
2085                asrc2_mask, asrc2_value);
2086
2087        if (snd_soc_component_read32(component, RT5640_ASRC_2)) {
2088                rt5640->asrc_en = true;
2089                snd_soc_component_update_bits(component, RT5640_JD_CTRL, 0x3, 0x3);
2090        } else {
2091                rt5640->asrc_en = false;
2092                snd_soc_component_update_bits(component, RT5640_JD_CTRL, 0x3, 0x0);
2093        }
2094
2095        return 0;
2096}
2097EXPORT_SYMBOL_GPL(rt5640_sel_asrc_clk_src);
2098
2099static void rt5640_enable_micbias1_for_ovcd(struct snd_soc_component *component)
2100{
2101        struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
2102
2103        snd_soc_dapm_mutex_lock(dapm);
2104        snd_soc_dapm_force_enable_pin_unlocked(dapm, "LDO2");
2105        snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS1");
2106        /* OVCD is unreliable when used with RCCLK as sysclk-source */
2107        snd_soc_dapm_force_enable_pin_unlocked(dapm, "Platform Clock");
2108        snd_soc_dapm_sync_unlocked(dapm);
2109        snd_soc_dapm_mutex_unlock(dapm);
2110}
2111
2112static void rt5640_disable_micbias1_for_ovcd(struct snd_soc_component *component)
2113{
2114        struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
2115
2116        snd_soc_dapm_mutex_lock(dapm);
2117        snd_soc_dapm_disable_pin_unlocked(dapm, "Platform Clock");
2118        snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS1");
2119        snd_soc_dapm_disable_pin_unlocked(dapm, "LDO2");
2120        snd_soc_dapm_sync_unlocked(dapm);
2121        snd_soc_dapm_mutex_unlock(dapm);
2122}
2123
2124static void rt5640_enable_micbias1_ovcd_irq(struct snd_soc_component *component)
2125{
2126        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2127
2128        snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
2129                RT5640_IRQ_MB1_OC_MASK, RT5640_IRQ_MB1_OC_NOR);
2130        rt5640->ovcd_irq_enabled = true;
2131}
2132
2133static void rt5640_disable_micbias1_ovcd_irq(struct snd_soc_component *component)
2134{
2135        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2136
2137        snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
2138                RT5640_IRQ_MB1_OC_MASK, RT5640_IRQ_MB1_OC_BP);
2139        rt5640->ovcd_irq_enabled = false;
2140}
2141
2142static void rt5640_clear_micbias1_ovcd(struct snd_soc_component *component)
2143{
2144        snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
2145                RT5640_MB1_OC_STATUS, 0);
2146}
2147
2148static bool rt5640_micbias1_ovcd(struct snd_soc_component *component)
2149{
2150        int val;
2151
2152        val = snd_soc_component_read32(component, RT5640_IRQ_CTRL2);
2153        dev_dbg(component->dev, "irq ctrl2 %#04x\n", val);
2154
2155        return (val & RT5640_MB1_OC_STATUS);
2156}
2157
2158static bool rt5640_jack_inserted(struct snd_soc_component *component)
2159{
2160        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2161        int val;
2162
2163        val = snd_soc_component_read32(component, RT5640_INT_IRQ_ST);
2164        dev_dbg(component->dev, "irq status %#04x\n", val);
2165
2166        if (rt5640->jd_inverted)
2167                return !(val & RT5640_JD_STATUS);
2168        else
2169                return (val & RT5640_JD_STATUS);
2170}
2171
2172/* Jack detect and button-press timings */
2173#define JACK_SETTLE_TIME        100 /* milli seconds */
2174#define JACK_DETECT_COUNT       5
2175#define JACK_DETECT_MAXCOUNT    20  /* Aprox. 2 seconds worth of tries */
2176#define JACK_UNPLUG_TIME        80  /* milli seconds */
2177#define BP_POLL_TIME            10  /* milli seconds */
2178#define BP_POLL_MAXCOUNT        200 /* assume something is wrong after this */
2179#define BP_THRESHOLD            3
2180
2181static void rt5640_start_button_press_work(struct snd_soc_component *component)
2182{
2183        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2184
2185        rt5640->poll_count = 0;
2186        rt5640->press_count = 0;
2187        rt5640->release_count = 0;
2188        rt5640->pressed = false;
2189        rt5640->press_reported = false;
2190        rt5640_clear_micbias1_ovcd(component);
2191        schedule_delayed_work(&rt5640->bp_work, msecs_to_jiffies(BP_POLL_TIME));
2192}
2193
2194static void rt5640_button_press_work(struct work_struct *work)
2195{
2196        struct rt5640_priv *rt5640 =
2197                container_of(work, struct rt5640_priv, bp_work.work);
2198        struct snd_soc_component *component = rt5640->component;
2199
2200        /* Check the jack was not removed underneath us */
2201        if (!rt5640_jack_inserted(component))
2202                return;
2203
2204        if (rt5640_micbias1_ovcd(component)) {
2205                rt5640->release_count = 0;
2206                rt5640->press_count++;
2207                /* Remember till after JACK_UNPLUG_TIME wait */
2208                if (rt5640->press_count >= BP_THRESHOLD)
2209                        rt5640->pressed = true;
2210                rt5640_clear_micbias1_ovcd(component);
2211        } else {
2212                rt5640->press_count = 0;
2213                rt5640->release_count++;
2214        }
2215
2216        /*
2217         * The pins get temporarily shorted on jack unplug, so we poll for
2218         * at least JACK_UNPLUG_TIME milli-seconds before reporting a press.
2219         */
2220        rt5640->poll_count++;
2221        if (rt5640->poll_count < (JACK_UNPLUG_TIME / BP_POLL_TIME)) {
2222                schedule_delayed_work(&rt5640->bp_work,
2223                                      msecs_to_jiffies(BP_POLL_TIME));
2224                return;
2225        }
2226
2227        if (rt5640->pressed && !rt5640->press_reported) {
2228                dev_dbg(component->dev, "headset button press\n");
2229                snd_soc_jack_report(rt5640->jack, SND_JACK_BTN_0,
2230                                    SND_JACK_BTN_0);
2231                rt5640->press_reported = true;
2232        }
2233
2234        if (rt5640->release_count >= BP_THRESHOLD) {
2235                if (rt5640->press_reported) {
2236                        dev_dbg(component->dev, "headset button release\n");
2237                        snd_soc_jack_report(rt5640->jack, 0, SND_JACK_BTN_0);
2238                }
2239                /* Re-enable OVCD IRQ to detect next press */
2240                rt5640_enable_micbias1_ovcd_irq(component);
2241                return; /* Stop polling */
2242        }
2243
2244        schedule_delayed_work(&rt5640->bp_work, msecs_to_jiffies(BP_POLL_TIME));
2245}
2246
2247static int rt5640_detect_headset(struct snd_soc_component *component)
2248{
2249        int i, headset_count = 0, headphone_count = 0;
2250
2251        /*
2252         * We get the insertion event before the jack is fully inserted at which
2253         * point the second ring on a TRRS connector may short the 2nd ring and
2254         * sleeve contacts, also the overcurrent detection is not entirely
2255         * reliable. So we try several times with a wait in between until we
2256         * detect the same type JACK_DETECT_COUNT times in a row.
2257         */
2258        for (i = 0; i < JACK_DETECT_MAXCOUNT; i++) {
2259                /* Clear any previous over-current status flag */
2260                rt5640_clear_micbias1_ovcd(component);
2261
2262                msleep(JACK_SETTLE_TIME);
2263
2264                /* Check the jack is still connected before checking ovcd */
2265                if (!rt5640_jack_inserted(component))
2266                        return 0;
2267
2268                if (rt5640_micbias1_ovcd(component)) {
2269                        /*
2270                         * Over current detected, there is a short between the
2271                         * 2nd ring contact and the ground, so a TRS connector
2272                         * without a mic contact and thus plain headphones.
2273                         */
2274                        dev_dbg(component->dev, "jack mic-gnd shorted\n");
2275                        headset_count = 0;
2276                        headphone_count++;
2277                        if (headphone_count == JACK_DETECT_COUNT)
2278                                return SND_JACK_HEADPHONE;
2279                } else {
2280                        dev_dbg(component->dev, "jack mic-gnd open\n");
2281                        headphone_count = 0;
2282                        headset_count++;
2283                        if (headset_count == JACK_DETECT_COUNT)
2284                                return SND_JACK_HEADSET;
2285                }
2286        }
2287
2288        dev_err(component->dev, "Error detecting headset vs headphones, bad contact?, assuming headphones\n");
2289        return SND_JACK_HEADPHONE;
2290}
2291
2292static void rt5640_jack_work(struct work_struct *work)
2293{
2294        struct rt5640_priv *rt5640 =
2295                container_of(work, struct rt5640_priv, jack_work);
2296        struct snd_soc_component *component = rt5640->component;
2297        int status;
2298
2299        if (!rt5640_jack_inserted(component)) {
2300                /* Jack removed, or spurious IRQ? */
2301                if (rt5640->jack->status & SND_JACK_HEADPHONE) {
2302                        if (rt5640->jack->status & SND_JACK_MICROPHONE) {
2303                                cancel_delayed_work_sync(&rt5640->bp_work);
2304                                rt5640_disable_micbias1_ovcd_irq(component);
2305                                rt5640_disable_micbias1_for_ovcd(component);
2306                        }
2307                        snd_soc_jack_report(rt5640->jack, 0,
2308                                            SND_JACK_HEADSET | SND_JACK_BTN_0);
2309                        dev_dbg(component->dev, "jack unplugged\n");
2310                }
2311        } else if (!(rt5640->jack->status & SND_JACK_HEADPHONE)) {
2312                /* Jack inserted */
2313                WARN_ON(rt5640->ovcd_irq_enabled);
2314                rt5640_enable_micbias1_for_ovcd(component);
2315                status = rt5640_detect_headset(component);
2316                if (status == SND_JACK_HEADSET) {
2317                        /* Enable ovcd IRQ for button press detect. */
2318                        rt5640_enable_micbias1_ovcd_irq(component);
2319                } else {
2320                        /* No more need for overcurrent detect. */
2321                        rt5640_disable_micbias1_for_ovcd(component);
2322                }
2323                dev_dbg(component->dev, "detect status %#02x\n", status);
2324                snd_soc_jack_report(rt5640->jack, status, SND_JACK_HEADSET);
2325        } else if (rt5640->ovcd_irq_enabled && rt5640_micbias1_ovcd(component)) {
2326                dev_dbg(component->dev, "OVCD IRQ\n");
2327
2328                /*
2329                 * The ovcd IRQ keeps firing while the button is pressed, so
2330                 * we disable it and start polling the button until released.
2331                 *
2332                 * The disable will make the IRQ pin 0 again and since we get
2333                 * IRQs on both edges (so as to detect both jack plugin and
2334                 * unplug) this means we will immediately get another IRQ.
2335                 * The ovcd_irq_enabled check above makes the 2ND IRQ a NOP.
2336                 */
2337                rt5640_disable_micbias1_ovcd_irq(component);
2338                rt5640_start_button_press_work(component);
2339
2340                /*
2341                 * If the jack-detect IRQ flag goes high (unplug) after our
2342                 * above rt5640_jack_inserted() check and before we have
2343                 * disabled the OVCD IRQ, the IRQ pin will stay high and as
2344                 * we react to edges, we miss the unplug event -> recheck.
2345                 */
2346                queue_work(system_long_wq, &rt5640->jack_work);
2347        }
2348}
2349
2350static irqreturn_t rt5640_irq(int irq, void *data)
2351{
2352        struct rt5640_priv *rt5640 = data;
2353
2354        if (rt5640->jack)
2355                queue_work(system_long_wq, &rt5640->jack_work);
2356
2357        return IRQ_HANDLED;
2358}
2359
2360static void rt5640_cancel_work(void *data)
2361{
2362        struct rt5640_priv *rt5640 = data;
2363
2364        cancel_work_sync(&rt5640->jack_work);
2365        cancel_delayed_work_sync(&rt5640->bp_work);
2366}
2367
2368static void rt5640_enable_jack_detect(struct snd_soc_component *component,
2369                                      struct snd_soc_jack *jack)
2370{
2371        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2372
2373        /* Select JD-source */
2374        snd_soc_component_update_bits(component, RT5640_JD_CTRL,
2375                RT5640_JD_MASK, rt5640->jd_src);
2376
2377        /* Selecting GPIO01 as an interrupt */
2378        snd_soc_component_update_bits(component, RT5640_GPIO_CTRL1,
2379                RT5640_GP1_PIN_MASK, RT5640_GP1_PIN_IRQ);
2380
2381        /* Set GPIO1 output */
2382        snd_soc_component_update_bits(component, RT5640_GPIO_CTRL3,
2383                RT5640_GP1_PF_MASK, RT5640_GP1_PF_OUT);
2384
2385        /* Enabling jd2 in general control 1 */
2386        snd_soc_component_write(component, RT5640_DUMMY1, 0x3f41);
2387
2388        /* Enabling jd2 in general control 2 */
2389        snd_soc_component_write(component, RT5640_DUMMY2, 0x4001);
2390
2391        snd_soc_component_write(component, RT5640_PR_BASE + RT5640_BIAS_CUR4,
2392                0xa800 | rt5640->ovcd_sf);
2393
2394        snd_soc_component_update_bits(component, RT5640_MICBIAS,
2395                RT5640_MIC1_OVTH_MASK | RT5640_MIC1_OVCD_MASK,
2396                rt5640->ovcd_th | RT5640_MIC1_OVCD_EN);
2397
2398        /*
2399         * The over-current-detect is only reliable in detecting the absence
2400         * of over-current, when the mic-contact in the jack is short-circuited,
2401         * the hardware periodically retries if it can apply the bias-current
2402         * leading to the ovcd status flip-flopping 1-0-1 with it being 0 about
2403         * 10% of the time, as we poll the ovcd status bit we might hit that
2404         * 10%, so we enable sticky mode and when checking OVCD we clear the
2405         * status, msleep() a bit and then check to get a reliable reading.
2406         */
2407        snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
2408                RT5640_MB1_OC_STKY_MASK, RT5640_MB1_OC_STKY_EN);
2409
2410        /*
2411         * All IRQs get or-ed together, so we need the jack IRQ to report 0
2412         * when a jack is inserted so that the OVCD IRQ then toggles the IRQ
2413         * pin 0/1 instead of it being stuck to 1. So we invert the JD polarity
2414         * on systems where the hardware does not already do this.
2415         */
2416        if (rt5640->jd_inverted)
2417                snd_soc_component_write(component, RT5640_IRQ_CTRL1,
2418                                        RT5640_IRQ_JD_NOR);
2419        else
2420                snd_soc_component_write(component, RT5640_IRQ_CTRL1,
2421                                        RT5640_IRQ_JD_NOR | RT5640_JD_P_INV);
2422
2423        rt5640->jack = jack;
2424        if (rt5640->jack->status & SND_JACK_MICROPHONE) {
2425                rt5640_enable_micbias1_for_ovcd(component);
2426                rt5640_enable_micbias1_ovcd_irq(component);
2427        }
2428
2429        enable_irq(rt5640->irq);
2430        /* sync initial jack state */
2431        queue_work(system_long_wq, &rt5640->jack_work);
2432}
2433
2434static void rt5640_disable_jack_detect(struct snd_soc_component *component)
2435{
2436        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2437
2438        disable_irq(rt5640->irq);
2439        rt5640_cancel_work(rt5640);
2440
2441        if (rt5640->jack->status & SND_JACK_MICROPHONE) {
2442                rt5640_disable_micbias1_ovcd_irq(component);
2443                rt5640_disable_micbias1_for_ovcd(component);
2444                snd_soc_jack_report(rt5640->jack, 0, SND_JACK_BTN_0);
2445        }
2446
2447        rt5640->jack = NULL;
2448}
2449
2450static int rt5640_set_jack(struct snd_soc_component *component,
2451                           struct snd_soc_jack *jack, void *data)
2452{
2453        if (jack)
2454                rt5640_enable_jack_detect(component, jack);
2455        else
2456                rt5640_disable_jack_detect(component);
2457
2458        return 0;
2459}
2460
2461static int rt5640_probe(struct snd_soc_component *component)
2462{
2463        struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
2464        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2465        u32 dmic1_data_pin = 0;
2466        u32 dmic2_data_pin = 0;
2467        bool dmic_en = false;
2468        u32 val;
2469
2470        /* Check if MCLK provided */
2471        rt5640->mclk = devm_clk_get(component->dev, "mclk");
2472        if (PTR_ERR(rt5640->mclk) == -EPROBE_DEFER)
2473                return -EPROBE_DEFER;
2474
2475        rt5640->component = component;
2476
2477        snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
2478
2479        snd_soc_component_update_bits(component, RT5640_DUMMY1, 0x0301, 0x0301);
2480        snd_soc_component_update_bits(component, RT5640_MICBIAS, 0x0030, 0x0030);
2481        snd_soc_component_update_bits(component, RT5640_DSP_PATH2, 0xfc00, 0x0c00);
2482
2483        switch (snd_soc_component_read32(component, RT5640_RESET) & RT5640_ID_MASK) {
2484        case RT5640_ID_5640:
2485        case RT5640_ID_5642:
2486                snd_soc_add_component_controls(component,
2487                        rt5640_specific_snd_controls,
2488                        ARRAY_SIZE(rt5640_specific_snd_controls));
2489                snd_soc_dapm_new_controls(dapm,
2490                        rt5640_specific_dapm_widgets,
2491                        ARRAY_SIZE(rt5640_specific_dapm_widgets));
2492                snd_soc_dapm_add_routes(dapm,
2493                        rt5640_specific_dapm_routes,
2494                        ARRAY_SIZE(rt5640_specific_dapm_routes));
2495                break;
2496        case RT5640_ID_5639:
2497                snd_soc_dapm_new_controls(dapm,
2498                        rt5639_specific_dapm_widgets,
2499                        ARRAY_SIZE(rt5639_specific_dapm_widgets));
2500                snd_soc_dapm_add_routes(dapm,
2501                        rt5639_specific_dapm_routes,
2502                        ARRAY_SIZE(rt5639_specific_dapm_routes));
2503                break;
2504        default:
2505                dev_err(component->dev,
2506                        "The driver is for RT5639 RT5640 or RT5642 only\n");
2507                return -ENODEV;
2508        }
2509
2510        /*
2511         * Note on some platforms the platform code may need to add device-props
2512         * rather then relying only on properties set by the firmware.
2513         * Therefor the property parsing MUST be done here, rather then from
2514         * rt5640_i2c_probe(), so that the platform-code can attach extra
2515         * properties before calling snd_soc_register_card().
2516         */
2517        if (device_property_read_bool(component->dev, "realtek,in1-differential"))
2518                snd_soc_component_update_bits(component, RT5640_IN1_IN2,
2519                                              RT5640_IN_DF1, RT5640_IN_DF1);
2520
2521        if (device_property_read_bool(component->dev, "realtek,in2-differential"))
2522                snd_soc_component_update_bits(component, RT5640_IN3_IN4,
2523                                              RT5640_IN_DF2, RT5640_IN_DF2);
2524
2525        if (device_property_read_bool(component->dev, "realtek,in3-differential"))
2526                snd_soc_component_update_bits(component, RT5640_IN1_IN2,
2527                                              RT5640_IN_DF2, RT5640_IN_DF2);
2528
2529        if (device_property_read_u32(component->dev, "realtek,dmic1-data-pin",
2530                                     &val) == 0 && val) {
2531                dmic1_data_pin = val - 1;
2532                dmic_en = true;
2533        }
2534
2535        if (device_property_read_u32(component->dev, "realtek,dmic2-data-pin",
2536                                     &val) == 0 && val) {
2537                dmic2_data_pin = val - 1;
2538                dmic_en = true;
2539        }
2540
2541        if (dmic_en)
2542                rt5640_dmic_enable(component, dmic1_data_pin, dmic2_data_pin);
2543
2544        if (device_property_read_u32(component->dev,
2545                                     "realtek,jack-detect-source", &val) == 0) {
2546                if (val <= RT5640_JD_SRC_GPIO4)
2547                        rt5640->jd_src = val << RT5640_JD_SFT;
2548                else
2549                        dev_warn(component->dev, "Warning: Invalid jack-detect-source value: %d, leaving jack-detect disabled\n",
2550                                 val);
2551        }
2552
2553        if (!device_property_read_bool(component->dev, "realtek,jack-detect-not-inverted"))
2554                rt5640->jd_inverted = true;
2555
2556        /*
2557         * Testing on various boards has shown that good defaults for the OVCD
2558         * threshold and scale-factor are 2000ľA and 0.75. For an effective
2559         * limit of 1500ľA, this seems to be more reliable then 1500ľA and 1.0.
2560         */
2561        rt5640->ovcd_th = RT5640_MIC1_OVTH_2000UA;
2562        rt5640->ovcd_sf = RT5640_MIC_OVCD_SF_0P75;
2563
2564        if (device_property_read_u32(component->dev,
2565                        "realtek,over-current-threshold-microamp", &val) == 0) {
2566                switch (val) {
2567                case 600:
2568                        rt5640->ovcd_th = RT5640_MIC1_OVTH_600UA;
2569                        break;
2570                case 1500:
2571                        rt5640->ovcd_th = RT5640_MIC1_OVTH_1500UA;
2572                        break;
2573                case 2000:
2574                        rt5640->ovcd_th = RT5640_MIC1_OVTH_2000UA;
2575                        break;
2576                default:
2577                        dev_warn(component->dev, "Warning: Invalid over-current-threshold-microamp value: %d, defaulting to 2000uA\n",
2578                                 val);
2579                }
2580        }
2581
2582        if (device_property_read_u32(component->dev,
2583                        "realtek,over-current-scale-factor", &val) == 0) {
2584                if (val <= RT5640_OVCD_SF_1P5)
2585                        rt5640->ovcd_sf = val << RT5640_MIC_OVCD_SF_SFT;
2586                else
2587                        dev_warn(component->dev, "Warning: Invalid over-current-scale-factor value: %d, defaulting to 0.75\n",
2588                                 val);
2589        }
2590
2591        return 0;
2592}
2593
2594static void rt5640_remove(struct snd_soc_component *component)
2595{
2596        rt5640_reset(component);
2597}
2598
2599#ifdef CONFIG_PM
2600static int rt5640_suspend(struct snd_soc_component *component)
2601{
2602        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2603
2604        snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
2605        rt5640_reset(component);
2606        regcache_cache_only(rt5640->regmap, true);
2607        regcache_mark_dirty(rt5640->regmap);
2608        if (gpio_is_valid(rt5640->ldo1_en))
2609                gpio_set_value_cansleep(rt5640->ldo1_en, 0);
2610
2611        return 0;
2612}
2613
2614static int rt5640_resume(struct snd_soc_component *component)
2615{
2616        struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
2617
2618        if (gpio_is_valid(rt5640->ldo1_en)) {
2619                gpio_set_value_cansleep(rt5640->ldo1_en, 1);
2620                msleep(400);
2621        }
2622
2623        regcache_cache_only(rt5640->regmap, false);
2624        regcache_sync(rt5640->regmap);
2625
2626        return 0;
2627}
2628#else
2629#define rt5640_suspend NULL
2630#define rt5640_resume NULL
2631#endif
2632
2633#define RT5640_STEREO_RATES SNDRV_PCM_RATE_8000_96000
2634#define RT5640_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
2635                        SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
2636
2637static const struct snd_soc_dai_ops rt5640_aif_dai_ops = {
2638        .hw_params = rt5640_hw_params,
2639        .set_fmt = rt5640_set_dai_fmt,
2640        .set_sysclk = rt5640_set_dai_sysclk,
2641        .set_pll = rt5640_set_dai_pll,
2642};
2643
2644static struct snd_soc_dai_driver rt5640_dai[] = {
2645        {
2646                .name = "rt5640-aif1",
2647                .id = RT5640_AIF1,
2648                .playback = {
2649                        .stream_name = "AIF1 Playback",
2650                        .channels_min = 1,
2651                        .channels_max = 2,
2652                        .rates = RT5640_STEREO_RATES,
2653                        .formats = RT5640_FORMATS,
2654                },
2655                .capture = {
2656                        .stream_name = "AIF1 Capture",
2657                        .channels_min = 1,
2658                        .channels_max = 2,
2659                        .rates = RT5640_STEREO_RATES,
2660                        .formats = RT5640_FORMATS,
2661                },
2662                .ops = &rt5640_aif_dai_ops,
2663        },
2664        {
2665                .name = "rt5640-aif2",
2666                .id = RT5640_AIF2,
2667                .playback = {
2668                        .stream_name = "AIF2 Playback",
2669                        .channels_min = 1,
2670                        .channels_max = 2,
2671                        .rates = RT5640_STEREO_RATES,
2672                        .formats = RT5640_FORMATS,
2673                },
2674                .capture = {
2675                        .stream_name = "AIF2 Capture",
2676                        .channels_min = 1,
2677                        .channels_max = 2,
2678                        .rates = RT5640_STEREO_RATES,
2679                        .formats = RT5640_FORMATS,
2680                },
2681                .ops = &rt5640_aif_dai_ops,
2682        },
2683};
2684
2685static const struct snd_soc_component_driver soc_component_dev_rt5640 = {
2686        .probe                  = rt5640_probe,
2687        .remove                 = rt5640_remove,
2688        .suspend                = rt5640_suspend,
2689        .resume                 = rt5640_resume,
2690        .set_bias_level         = rt5640_set_bias_level,
2691        .set_jack               = rt5640_set_jack,
2692        .controls               = rt5640_snd_controls,
2693        .num_controls           = ARRAY_SIZE(rt5640_snd_controls),
2694        .dapm_widgets           = rt5640_dapm_widgets,
2695        .num_dapm_widgets       = ARRAY_SIZE(rt5640_dapm_widgets),
2696        .dapm_routes            = rt5640_dapm_routes,
2697        .num_dapm_routes        = ARRAY_SIZE(rt5640_dapm_routes),
2698        .use_pmdown_time        = 1,
2699        .endianness             = 1,
2700        .non_legacy_dai_naming  = 1,
2701
2702};
2703
2704static const struct regmap_config rt5640_regmap = {
2705        .reg_bits = 8,
2706        .val_bits = 16,
2707        .use_single_read = true,
2708        .use_single_write = true,
2709
2710        .max_register = RT5640_VENDOR_ID2 + 1 + (ARRAY_SIZE(rt5640_ranges) *
2711                                               RT5640_PR_SPACING),
2712        .volatile_reg = rt5640_volatile_register,
2713        .readable_reg = rt5640_readable_register,
2714
2715        .cache_type = REGCACHE_RBTREE,
2716        .reg_defaults = rt5640_reg,
2717        .num_reg_defaults = ARRAY_SIZE(rt5640_reg),
2718        .ranges = rt5640_ranges,
2719        .num_ranges = ARRAY_SIZE(rt5640_ranges),
2720};
2721
2722static const struct i2c_device_id rt5640_i2c_id[] = {
2723        { "rt5640", 0 },
2724        { "rt5639", 0 },
2725        { "rt5642", 0 },
2726        { }
2727};
2728MODULE_DEVICE_TABLE(i2c, rt5640_i2c_id);
2729
2730#if defined(CONFIG_OF)
2731static const struct of_device_id rt5640_of_match[] = {
2732        { .compatible = "realtek,rt5639", },
2733        { .compatible = "realtek,rt5640", },
2734        {},
2735};
2736MODULE_DEVICE_TABLE(of, rt5640_of_match);
2737#endif
2738
2739#ifdef CONFIG_ACPI
2740static const struct acpi_device_id rt5640_acpi_match[] = {
2741        { "INT33CA", 0 },
2742        { "10EC3276", 0 },
2743        { "10EC5640", 0 },
2744        { "10EC5642", 0 },
2745        { "INTCCFFD", 0 },
2746        { },
2747};
2748MODULE_DEVICE_TABLE(acpi, rt5640_acpi_match);
2749#endif
2750
2751static int rt5640_parse_dt(struct rt5640_priv *rt5640, struct device_node *np)
2752{
2753        rt5640->ldo1_en = of_get_named_gpio(np, "realtek,ldo1-en-gpios", 0);
2754        /*
2755         * LDO1_EN is optional (it may be statically tied on the board).
2756         * -ENOENT means that the property doesn't exist, i.e. there is no
2757         * GPIO, so is not an error. Any other error code means the property
2758         * exists, but could not be parsed.
2759         */
2760        if (!gpio_is_valid(rt5640->ldo1_en) &&
2761                        (rt5640->ldo1_en != -ENOENT))
2762                return rt5640->ldo1_en;
2763
2764        return 0;
2765}
2766
2767static int rt5640_i2c_probe(struct i2c_client *i2c,
2768                    const struct i2c_device_id *id)
2769{
2770        struct rt5640_priv *rt5640;
2771        int ret;
2772        unsigned int val;
2773
2774        rt5640 = devm_kzalloc(&i2c->dev,
2775                                sizeof(struct rt5640_priv),
2776                                GFP_KERNEL);
2777        if (NULL == rt5640)
2778                return -ENOMEM;
2779        i2c_set_clientdata(i2c, rt5640);
2780
2781        if (i2c->dev.of_node) {
2782                ret = rt5640_parse_dt(rt5640, i2c->dev.of_node);
2783                if (ret)
2784                        return ret;
2785        } else
2786                rt5640->ldo1_en = -EINVAL;
2787
2788        rt5640->regmap = devm_regmap_init_i2c(i2c, &rt5640_regmap);
2789        if (IS_ERR(rt5640->regmap)) {
2790                ret = PTR_ERR(rt5640->regmap);
2791                dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
2792                        ret);
2793                return ret;
2794        }
2795
2796        if (gpio_is_valid(rt5640->ldo1_en)) {
2797                ret = devm_gpio_request_one(&i2c->dev, rt5640->ldo1_en,
2798                                            GPIOF_OUT_INIT_HIGH,
2799                                            "RT5640 LDO1_EN");
2800                if (ret < 0) {
2801                        dev_err(&i2c->dev, "Failed to request LDO1_EN %d: %d\n",
2802                                rt5640->ldo1_en, ret);
2803                        return ret;
2804                }
2805                msleep(400);
2806        }
2807
2808        regmap_read(rt5640->regmap, RT5640_VENDOR_ID2, &val);
2809        if (val != RT5640_DEVICE_ID) {
2810                dev_err(&i2c->dev,
2811                        "Device with ID register %#x is not rt5640/39\n", val);
2812                return -ENODEV;
2813        }
2814
2815        regmap_write(rt5640->regmap, RT5640_RESET, 0);
2816
2817        ret = regmap_register_patch(rt5640->regmap, init_list,
2818                                    ARRAY_SIZE(init_list));
2819        if (ret != 0)
2820                dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
2821
2822        regmap_update_bits(rt5640->regmap, RT5640_DUMMY1,
2823                                RT5640_MCLK_DET, RT5640_MCLK_DET);
2824
2825        rt5640->hp_mute = 1;
2826        rt5640->irq = i2c->irq;
2827        INIT_DELAYED_WORK(&rt5640->bp_work, rt5640_button_press_work);
2828        INIT_WORK(&rt5640->jack_work, rt5640_jack_work);
2829
2830        /* Make sure work is stopped on probe-error / remove */
2831        ret = devm_add_action_or_reset(&i2c->dev, rt5640_cancel_work, rt5640);
2832        if (ret)
2833                return ret;
2834
2835        ret = devm_request_irq(&i2c->dev, rt5640->irq, rt5640_irq,
2836                               IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2837                               | IRQF_ONESHOT, "rt5640", rt5640);
2838        if (ret == 0) {
2839                /* Gets re-enabled by rt5640_set_jack() */
2840                disable_irq(rt5640->irq);
2841        } else {
2842                dev_warn(&i2c->dev, "Failed to reguest IRQ %d: %d\n",
2843                         rt5640->irq, ret);
2844                rt5640->irq = -ENXIO;
2845        }
2846
2847        return devm_snd_soc_register_component(&i2c->dev,
2848                                      &soc_component_dev_rt5640,
2849                                      rt5640_dai, ARRAY_SIZE(rt5640_dai));
2850}
2851
2852static struct i2c_driver rt5640_i2c_driver = {
2853        .driver = {
2854                .name = "rt5640",
2855                .acpi_match_table = ACPI_PTR(rt5640_acpi_match),
2856                .of_match_table = of_match_ptr(rt5640_of_match),
2857        },
2858        .probe = rt5640_i2c_probe,
2859        .id_table = rt5640_i2c_id,
2860};
2861module_i2c_driver(rt5640_i2c_driver);
2862
2863MODULE_DESCRIPTION("ASoC RT5640/RT5639 driver");
2864MODULE_AUTHOR("Johnny Hsu <johnnyhsu@realtek.com>");
2865MODULE_LICENSE("GPL v2");
2866