18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * rt5640.c  --  RT5640/RT5639 ALSA SoC audio codec driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2011 Realtek Semiconductor Corp.
68c2ecf20Sopenharmony_ci * Author: Johnny Hsu <johnnyhsu@realtek.com>
78c2ecf20Sopenharmony_ci * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/pm.h>
158c2ecf20Sopenharmony_ci#include <linux/gpio.h>
168c2ecf20Sopenharmony_ci#include <linux/i2c.h>
178c2ecf20Sopenharmony_ci#include <linux/regmap.h>
188c2ecf20Sopenharmony_ci#include <linux/of.h>
198c2ecf20Sopenharmony_ci#include <linux/of_gpio.h>
208c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
218c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
228c2ecf20Sopenharmony_ci#include <linux/acpi.h>
238c2ecf20Sopenharmony_ci#include <sound/core.h>
248c2ecf20Sopenharmony_ci#include <sound/jack.h>
258c2ecf20Sopenharmony_ci#include <sound/pcm.h>
268c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
278c2ecf20Sopenharmony_ci#include <sound/soc.h>
288c2ecf20Sopenharmony_ci#include <sound/soc-dapm.h>
298c2ecf20Sopenharmony_ci#include <sound/initval.h>
308c2ecf20Sopenharmony_ci#include <sound/tlv.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "rl6231.h"
338c2ecf20Sopenharmony_ci#include "rt5640.h"
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define RT5640_DEVICE_ID 0x6231
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define RT5640_PR_RANGE_BASE (0xff + 1)
388c2ecf20Sopenharmony_ci#define RT5640_PR_SPACING 0x100
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define RT5640_PR_BASE (RT5640_PR_RANGE_BASE + (0 * RT5640_PR_SPACING))
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic const struct regmap_range_cfg rt5640_ranges[] = {
438c2ecf20Sopenharmony_ci	{ .name = "PR", .range_min = RT5640_PR_BASE,
448c2ecf20Sopenharmony_ci	  .range_max = RT5640_PR_BASE + 0xb4,
458c2ecf20Sopenharmony_ci	  .selector_reg = RT5640_PRIV_INDEX,
468c2ecf20Sopenharmony_ci	  .selector_mask = 0xff,
478c2ecf20Sopenharmony_ci	  .selector_shift = 0x0,
488c2ecf20Sopenharmony_ci	  .window_start = RT5640_PRIV_DATA,
498c2ecf20Sopenharmony_ci	  .window_len = 0x1, },
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic const struct reg_sequence init_list[] = {
538c2ecf20Sopenharmony_ci	{RT5640_PR_BASE + 0x3d,	0x3600},
548c2ecf20Sopenharmony_ci	{RT5640_PR_BASE + 0x12,	0x0aa8},
558c2ecf20Sopenharmony_ci	{RT5640_PR_BASE + 0x14,	0x0aaa},
568c2ecf20Sopenharmony_ci	{RT5640_PR_BASE + 0x20,	0x6110},
578c2ecf20Sopenharmony_ci	{RT5640_PR_BASE + 0x21,	0xe0e0},
588c2ecf20Sopenharmony_ci	{RT5640_PR_BASE + 0x23,	0x1804},
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic const struct reg_default rt5640_reg[] = {
628c2ecf20Sopenharmony_ci	{ 0x00, 0x000e },
638c2ecf20Sopenharmony_ci	{ 0x01, 0xc8c8 },
648c2ecf20Sopenharmony_ci	{ 0x02, 0xc8c8 },
658c2ecf20Sopenharmony_ci	{ 0x03, 0xc8c8 },
668c2ecf20Sopenharmony_ci	{ 0x04, 0x8000 },
678c2ecf20Sopenharmony_ci	{ 0x0d, 0x0000 },
688c2ecf20Sopenharmony_ci	{ 0x0e, 0x0000 },
698c2ecf20Sopenharmony_ci	{ 0x0f, 0x0808 },
708c2ecf20Sopenharmony_ci	{ 0x19, 0xafaf },
718c2ecf20Sopenharmony_ci	{ 0x1a, 0xafaf },
728c2ecf20Sopenharmony_ci	{ 0x1b, 0x0000 },
738c2ecf20Sopenharmony_ci	{ 0x1c, 0x2f2f },
748c2ecf20Sopenharmony_ci	{ 0x1d, 0x2f2f },
758c2ecf20Sopenharmony_ci	{ 0x1e, 0x0000 },
768c2ecf20Sopenharmony_ci	{ 0x27, 0x7060 },
778c2ecf20Sopenharmony_ci	{ 0x28, 0x7070 },
788c2ecf20Sopenharmony_ci	{ 0x29, 0x8080 },
798c2ecf20Sopenharmony_ci	{ 0x2a, 0x5454 },
808c2ecf20Sopenharmony_ci	{ 0x2b, 0x5454 },
818c2ecf20Sopenharmony_ci	{ 0x2c, 0xaa00 },
828c2ecf20Sopenharmony_ci	{ 0x2d, 0x0000 },
838c2ecf20Sopenharmony_ci	{ 0x2e, 0xa000 },
848c2ecf20Sopenharmony_ci	{ 0x2f, 0x0000 },
858c2ecf20Sopenharmony_ci	{ 0x3b, 0x0000 },
868c2ecf20Sopenharmony_ci	{ 0x3c, 0x007f },
878c2ecf20Sopenharmony_ci	{ 0x3d, 0x0000 },
888c2ecf20Sopenharmony_ci	{ 0x3e, 0x007f },
898c2ecf20Sopenharmony_ci	{ 0x45, 0xe000 },
908c2ecf20Sopenharmony_ci	{ 0x46, 0x003e },
918c2ecf20Sopenharmony_ci	{ 0x47, 0x003e },
928c2ecf20Sopenharmony_ci	{ 0x48, 0xf800 },
938c2ecf20Sopenharmony_ci	{ 0x49, 0x3800 },
948c2ecf20Sopenharmony_ci	{ 0x4a, 0x0004 },
958c2ecf20Sopenharmony_ci	{ 0x4c, 0xfc00 },
968c2ecf20Sopenharmony_ci	{ 0x4d, 0x0000 },
978c2ecf20Sopenharmony_ci	{ 0x4f, 0x01ff },
988c2ecf20Sopenharmony_ci	{ 0x50, 0x0000 },
998c2ecf20Sopenharmony_ci	{ 0x51, 0x0000 },
1008c2ecf20Sopenharmony_ci	{ 0x52, 0x01ff },
1018c2ecf20Sopenharmony_ci	{ 0x53, 0xf000 },
1028c2ecf20Sopenharmony_ci	{ 0x61, 0x0000 },
1038c2ecf20Sopenharmony_ci	{ 0x62, 0x0000 },
1048c2ecf20Sopenharmony_ci	{ 0x63, 0x00c0 },
1058c2ecf20Sopenharmony_ci	{ 0x64, 0x0000 },
1068c2ecf20Sopenharmony_ci	{ 0x65, 0x0000 },
1078c2ecf20Sopenharmony_ci	{ 0x66, 0x0000 },
1088c2ecf20Sopenharmony_ci	{ 0x6a, 0x0000 },
1098c2ecf20Sopenharmony_ci	{ 0x6c, 0x0000 },
1108c2ecf20Sopenharmony_ci	{ 0x70, 0x8000 },
1118c2ecf20Sopenharmony_ci	{ 0x71, 0x8000 },
1128c2ecf20Sopenharmony_ci	{ 0x72, 0x8000 },
1138c2ecf20Sopenharmony_ci	{ 0x73, 0x1114 },
1148c2ecf20Sopenharmony_ci	{ 0x74, 0x0c00 },
1158c2ecf20Sopenharmony_ci	{ 0x75, 0x1d00 },
1168c2ecf20Sopenharmony_ci	{ 0x80, 0x0000 },
1178c2ecf20Sopenharmony_ci	{ 0x81, 0x0000 },
1188c2ecf20Sopenharmony_ci	{ 0x82, 0x0000 },
1198c2ecf20Sopenharmony_ci	{ 0x83, 0x0000 },
1208c2ecf20Sopenharmony_ci	{ 0x84, 0x0000 },
1218c2ecf20Sopenharmony_ci	{ 0x85, 0x0008 },
1228c2ecf20Sopenharmony_ci	{ 0x89, 0x0000 },
1238c2ecf20Sopenharmony_ci	{ 0x8a, 0x0000 },
1248c2ecf20Sopenharmony_ci	{ 0x8b, 0x0600 },
1258c2ecf20Sopenharmony_ci	{ 0x8c, 0x0228 },
1268c2ecf20Sopenharmony_ci	{ 0x8d, 0xa000 },
1278c2ecf20Sopenharmony_ci	{ 0x8e, 0x0004 },
1288c2ecf20Sopenharmony_ci	{ 0x8f, 0x1100 },
1298c2ecf20Sopenharmony_ci	{ 0x90, 0x0646 },
1308c2ecf20Sopenharmony_ci	{ 0x91, 0x0c00 },
1318c2ecf20Sopenharmony_ci	{ 0x92, 0x0000 },
1328c2ecf20Sopenharmony_ci	{ 0x93, 0x3000 },
1338c2ecf20Sopenharmony_ci	{ 0xb0, 0x2080 },
1348c2ecf20Sopenharmony_ci	{ 0xb1, 0x0000 },
1358c2ecf20Sopenharmony_ci	{ 0xb4, 0x2206 },
1368c2ecf20Sopenharmony_ci	{ 0xb5, 0x1f00 },
1378c2ecf20Sopenharmony_ci	{ 0xb6, 0x0000 },
1388c2ecf20Sopenharmony_ci	{ 0xb8, 0x034b },
1398c2ecf20Sopenharmony_ci	{ 0xb9, 0x0066 },
1408c2ecf20Sopenharmony_ci	{ 0xba, 0x000b },
1418c2ecf20Sopenharmony_ci	{ 0xbb, 0x0000 },
1428c2ecf20Sopenharmony_ci	{ 0xbc, 0x0000 },
1438c2ecf20Sopenharmony_ci	{ 0xbd, 0x0000 },
1448c2ecf20Sopenharmony_ci	{ 0xbe, 0x0000 },
1458c2ecf20Sopenharmony_ci	{ 0xbf, 0x0000 },
1468c2ecf20Sopenharmony_ci	{ 0xc0, 0x0400 },
1478c2ecf20Sopenharmony_ci	{ 0xc2, 0x0000 },
1488c2ecf20Sopenharmony_ci	{ 0xc4, 0x0000 },
1498c2ecf20Sopenharmony_ci	{ 0xc5, 0x0000 },
1508c2ecf20Sopenharmony_ci	{ 0xc6, 0x2000 },
1518c2ecf20Sopenharmony_ci	{ 0xc8, 0x0000 },
1528c2ecf20Sopenharmony_ci	{ 0xc9, 0x0000 },
1538c2ecf20Sopenharmony_ci	{ 0xca, 0x0000 },
1548c2ecf20Sopenharmony_ci	{ 0xcb, 0x0000 },
1558c2ecf20Sopenharmony_ci	{ 0xcc, 0x0000 },
1568c2ecf20Sopenharmony_ci	{ 0xcf, 0x0013 },
1578c2ecf20Sopenharmony_ci	{ 0xd0, 0x0680 },
1588c2ecf20Sopenharmony_ci	{ 0xd1, 0x1c17 },
1598c2ecf20Sopenharmony_ci	{ 0xd2, 0x8c00 },
1608c2ecf20Sopenharmony_ci	{ 0xd3, 0xaa20 },
1618c2ecf20Sopenharmony_ci	{ 0xd6, 0x0400 },
1628c2ecf20Sopenharmony_ci	{ 0xd9, 0x0809 },
1638c2ecf20Sopenharmony_ci	{ 0xfe, 0x10ec },
1648c2ecf20Sopenharmony_ci	{ 0xff, 0x6231 },
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int rt5640_reset(struct snd_soc_component *component)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	return snd_soc_component_write(component, RT5640_RESET, 0);
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic bool rt5640_volatile_register(struct device *dev, unsigned int reg)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	int i;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rt5640_ranges); i++)
1778c2ecf20Sopenharmony_ci		if ((reg >= rt5640_ranges[i].window_start &&
1788c2ecf20Sopenharmony_ci		     reg <= rt5640_ranges[i].window_start +
1798c2ecf20Sopenharmony_ci		     rt5640_ranges[i].window_len) ||
1808c2ecf20Sopenharmony_ci		    (reg >= rt5640_ranges[i].range_min &&
1818c2ecf20Sopenharmony_ci		     reg <= rt5640_ranges[i].range_max))
1828c2ecf20Sopenharmony_ci			return true;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	switch (reg) {
1858c2ecf20Sopenharmony_ci	case RT5640_RESET:
1868c2ecf20Sopenharmony_ci	case RT5640_ASRC_5:
1878c2ecf20Sopenharmony_ci	case RT5640_EQ_CTRL1:
1888c2ecf20Sopenharmony_ci	case RT5640_DRC_AGC_1:
1898c2ecf20Sopenharmony_ci	case RT5640_ANC_CTRL1:
1908c2ecf20Sopenharmony_ci	case RT5640_IRQ_CTRL2:
1918c2ecf20Sopenharmony_ci	case RT5640_INT_IRQ_ST:
1928c2ecf20Sopenharmony_ci	case RT5640_DSP_CTRL2:
1938c2ecf20Sopenharmony_ci	case RT5640_DSP_CTRL3:
1948c2ecf20Sopenharmony_ci	case RT5640_PRIV_INDEX:
1958c2ecf20Sopenharmony_ci	case RT5640_PRIV_DATA:
1968c2ecf20Sopenharmony_ci	case RT5640_PGM_REG_ARR1:
1978c2ecf20Sopenharmony_ci	case RT5640_PGM_REG_ARR3:
1988c2ecf20Sopenharmony_ci	case RT5640_VENDOR_ID:
1998c2ecf20Sopenharmony_ci	case RT5640_VENDOR_ID1:
2008c2ecf20Sopenharmony_ci	case RT5640_VENDOR_ID2:
2018c2ecf20Sopenharmony_ci		return true;
2028c2ecf20Sopenharmony_ci	default:
2038c2ecf20Sopenharmony_ci		return false;
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic bool rt5640_readable_register(struct device *dev, unsigned int reg)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	int i;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rt5640_ranges); i++)
2128c2ecf20Sopenharmony_ci		if ((reg >= rt5640_ranges[i].window_start &&
2138c2ecf20Sopenharmony_ci		     reg <= rt5640_ranges[i].window_start +
2148c2ecf20Sopenharmony_ci		     rt5640_ranges[i].window_len) ||
2158c2ecf20Sopenharmony_ci		    (reg >= rt5640_ranges[i].range_min &&
2168c2ecf20Sopenharmony_ci		     reg <= rt5640_ranges[i].range_max))
2178c2ecf20Sopenharmony_ci			return true;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	switch (reg) {
2208c2ecf20Sopenharmony_ci	case RT5640_RESET:
2218c2ecf20Sopenharmony_ci	case RT5640_SPK_VOL:
2228c2ecf20Sopenharmony_ci	case RT5640_HP_VOL:
2238c2ecf20Sopenharmony_ci	case RT5640_OUTPUT:
2248c2ecf20Sopenharmony_ci	case RT5640_MONO_OUT:
2258c2ecf20Sopenharmony_ci	case RT5640_IN1_IN2:
2268c2ecf20Sopenharmony_ci	case RT5640_IN3_IN4:
2278c2ecf20Sopenharmony_ci	case RT5640_INL_INR_VOL:
2288c2ecf20Sopenharmony_ci	case RT5640_DAC1_DIG_VOL:
2298c2ecf20Sopenharmony_ci	case RT5640_DAC2_DIG_VOL:
2308c2ecf20Sopenharmony_ci	case RT5640_DAC2_CTRL:
2318c2ecf20Sopenharmony_ci	case RT5640_ADC_DIG_VOL:
2328c2ecf20Sopenharmony_ci	case RT5640_ADC_DATA:
2338c2ecf20Sopenharmony_ci	case RT5640_ADC_BST_VOL:
2348c2ecf20Sopenharmony_ci	case RT5640_STO_ADC_MIXER:
2358c2ecf20Sopenharmony_ci	case RT5640_MONO_ADC_MIXER:
2368c2ecf20Sopenharmony_ci	case RT5640_AD_DA_MIXER:
2378c2ecf20Sopenharmony_ci	case RT5640_STO_DAC_MIXER:
2388c2ecf20Sopenharmony_ci	case RT5640_MONO_DAC_MIXER:
2398c2ecf20Sopenharmony_ci	case RT5640_DIG_MIXER:
2408c2ecf20Sopenharmony_ci	case RT5640_DSP_PATH1:
2418c2ecf20Sopenharmony_ci	case RT5640_DSP_PATH2:
2428c2ecf20Sopenharmony_ci	case RT5640_DIG_INF_DATA:
2438c2ecf20Sopenharmony_ci	case RT5640_REC_L1_MIXER:
2448c2ecf20Sopenharmony_ci	case RT5640_REC_L2_MIXER:
2458c2ecf20Sopenharmony_ci	case RT5640_REC_R1_MIXER:
2468c2ecf20Sopenharmony_ci	case RT5640_REC_R2_MIXER:
2478c2ecf20Sopenharmony_ci	case RT5640_HPO_MIXER:
2488c2ecf20Sopenharmony_ci	case RT5640_SPK_L_MIXER:
2498c2ecf20Sopenharmony_ci	case RT5640_SPK_R_MIXER:
2508c2ecf20Sopenharmony_ci	case RT5640_SPO_L_MIXER:
2518c2ecf20Sopenharmony_ci	case RT5640_SPO_R_MIXER:
2528c2ecf20Sopenharmony_ci	case RT5640_SPO_CLSD_RATIO:
2538c2ecf20Sopenharmony_ci	case RT5640_MONO_MIXER:
2548c2ecf20Sopenharmony_ci	case RT5640_OUT_L1_MIXER:
2558c2ecf20Sopenharmony_ci	case RT5640_OUT_L2_MIXER:
2568c2ecf20Sopenharmony_ci	case RT5640_OUT_L3_MIXER:
2578c2ecf20Sopenharmony_ci	case RT5640_OUT_R1_MIXER:
2588c2ecf20Sopenharmony_ci	case RT5640_OUT_R2_MIXER:
2598c2ecf20Sopenharmony_ci	case RT5640_OUT_R3_MIXER:
2608c2ecf20Sopenharmony_ci	case RT5640_LOUT_MIXER:
2618c2ecf20Sopenharmony_ci	case RT5640_PWR_DIG1:
2628c2ecf20Sopenharmony_ci	case RT5640_PWR_DIG2:
2638c2ecf20Sopenharmony_ci	case RT5640_PWR_ANLG1:
2648c2ecf20Sopenharmony_ci	case RT5640_PWR_ANLG2:
2658c2ecf20Sopenharmony_ci	case RT5640_PWR_MIXER:
2668c2ecf20Sopenharmony_ci	case RT5640_PWR_VOL:
2678c2ecf20Sopenharmony_ci	case RT5640_PRIV_INDEX:
2688c2ecf20Sopenharmony_ci	case RT5640_PRIV_DATA:
2698c2ecf20Sopenharmony_ci	case RT5640_I2S1_SDP:
2708c2ecf20Sopenharmony_ci	case RT5640_I2S2_SDP:
2718c2ecf20Sopenharmony_ci	case RT5640_ADDA_CLK1:
2728c2ecf20Sopenharmony_ci	case RT5640_ADDA_CLK2:
2738c2ecf20Sopenharmony_ci	case RT5640_DMIC:
2748c2ecf20Sopenharmony_ci	case RT5640_GLB_CLK:
2758c2ecf20Sopenharmony_ci	case RT5640_PLL_CTRL1:
2768c2ecf20Sopenharmony_ci	case RT5640_PLL_CTRL2:
2778c2ecf20Sopenharmony_ci	case RT5640_ASRC_1:
2788c2ecf20Sopenharmony_ci	case RT5640_ASRC_2:
2798c2ecf20Sopenharmony_ci	case RT5640_ASRC_3:
2808c2ecf20Sopenharmony_ci	case RT5640_ASRC_4:
2818c2ecf20Sopenharmony_ci	case RT5640_ASRC_5:
2828c2ecf20Sopenharmony_ci	case RT5640_HP_OVCD:
2838c2ecf20Sopenharmony_ci	case RT5640_CLS_D_OVCD:
2848c2ecf20Sopenharmony_ci	case RT5640_CLS_D_OUT:
2858c2ecf20Sopenharmony_ci	case RT5640_DEPOP_M1:
2868c2ecf20Sopenharmony_ci	case RT5640_DEPOP_M2:
2878c2ecf20Sopenharmony_ci	case RT5640_DEPOP_M3:
2888c2ecf20Sopenharmony_ci	case RT5640_CHARGE_PUMP:
2898c2ecf20Sopenharmony_ci	case RT5640_PV_DET_SPK_G:
2908c2ecf20Sopenharmony_ci	case RT5640_MICBIAS:
2918c2ecf20Sopenharmony_ci	case RT5640_EQ_CTRL1:
2928c2ecf20Sopenharmony_ci	case RT5640_EQ_CTRL2:
2938c2ecf20Sopenharmony_ci	case RT5640_WIND_FILTER:
2948c2ecf20Sopenharmony_ci	case RT5640_DRC_AGC_1:
2958c2ecf20Sopenharmony_ci	case RT5640_DRC_AGC_2:
2968c2ecf20Sopenharmony_ci	case RT5640_DRC_AGC_3:
2978c2ecf20Sopenharmony_ci	case RT5640_SVOL_ZC:
2988c2ecf20Sopenharmony_ci	case RT5640_ANC_CTRL1:
2998c2ecf20Sopenharmony_ci	case RT5640_ANC_CTRL2:
3008c2ecf20Sopenharmony_ci	case RT5640_ANC_CTRL3:
3018c2ecf20Sopenharmony_ci	case RT5640_JD_CTRL:
3028c2ecf20Sopenharmony_ci	case RT5640_ANC_JD:
3038c2ecf20Sopenharmony_ci	case RT5640_IRQ_CTRL1:
3048c2ecf20Sopenharmony_ci	case RT5640_IRQ_CTRL2:
3058c2ecf20Sopenharmony_ci	case RT5640_INT_IRQ_ST:
3068c2ecf20Sopenharmony_ci	case RT5640_GPIO_CTRL1:
3078c2ecf20Sopenharmony_ci	case RT5640_GPIO_CTRL2:
3088c2ecf20Sopenharmony_ci	case RT5640_GPIO_CTRL3:
3098c2ecf20Sopenharmony_ci	case RT5640_DSP_CTRL1:
3108c2ecf20Sopenharmony_ci	case RT5640_DSP_CTRL2:
3118c2ecf20Sopenharmony_ci	case RT5640_DSP_CTRL3:
3128c2ecf20Sopenharmony_ci	case RT5640_DSP_CTRL4:
3138c2ecf20Sopenharmony_ci	case RT5640_PGM_REG_ARR1:
3148c2ecf20Sopenharmony_ci	case RT5640_PGM_REG_ARR2:
3158c2ecf20Sopenharmony_ci	case RT5640_PGM_REG_ARR3:
3168c2ecf20Sopenharmony_ci	case RT5640_PGM_REG_ARR4:
3178c2ecf20Sopenharmony_ci	case RT5640_PGM_REG_ARR5:
3188c2ecf20Sopenharmony_ci	case RT5640_SCB_FUNC:
3198c2ecf20Sopenharmony_ci	case RT5640_SCB_CTRL:
3208c2ecf20Sopenharmony_ci	case RT5640_BASE_BACK:
3218c2ecf20Sopenharmony_ci	case RT5640_MP3_PLUS1:
3228c2ecf20Sopenharmony_ci	case RT5640_MP3_PLUS2:
3238c2ecf20Sopenharmony_ci	case RT5640_3D_HP:
3248c2ecf20Sopenharmony_ci	case RT5640_ADJ_HPF:
3258c2ecf20Sopenharmony_ci	case RT5640_HP_CALIB_AMP_DET:
3268c2ecf20Sopenharmony_ci	case RT5640_HP_CALIB2:
3278c2ecf20Sopenharmony_ci	case RT5640_SV_ZCD1:
3288c2ecf20Sopenharmony_ci	case RT5640_SV_ZCD2:
3298c2ecf20Sopenharmony_ci	case RT5640_DUMMY1:
3308c2ecf20Sopenharmony_ci	case RT5640_DUMMY2:
3318c2ecf20Sopenharmony_ci	case RT5640_DUMMY3:
3328c2ecf20Sopenharmony_ci	case RT5640_VENDOR_ID:
3338c2ecf20Sopenharmony_ci	case RT5640_VENDOR_ID1:
3348c2ecf20Sopenharmony_ci	case RT5640_VENDOR_ID2:
3358c2ecf20Sopenharmony_ci		return true;
3368c2ecf20Sopenharmony_ci	default:
3378c2ecf20Sopenharmony_ci		return false;
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
3428c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_MINMAX(dac_vol_tlv, -6562, 0);
3438c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
3448c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_MINMAX(adc_vol_tlv, -1762, 3000);
3458c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(adc_bst_tlv, 0, 1200, 0);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci/* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
3488c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_RANGE(bst_tlv,
3498c2ecf20Sopenharmony_ci	0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
3508c2ecf20Sopenharmony_ci	1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
3518c2ecf20Sopenharmony_ci	2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
3528c2ecf20Sopenharmony_ci	3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
3538c2ecf20Sopenharmony_ci	6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
3548c2ecf20Sopenharmony_ci	7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
3558c2ecf20Sopenharmony_ci	8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0)
3568c2ecf20Sopenharmony_ci);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci/* Interface data select */
3598c2ecf20Sopenharmony_cistatic const char * const rt5640_data_select[] = {
3608c2ecf20Sopenharmony_ci	"Normal", "Swap", "left copy to right", "right copy to left"};
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_if1_dac_enum, RT5640_DIG_INF_DATA,
3638c2ecf20Sopenharmony_ci			    RT5640_IF1_DAC_SEL_SFT, rt5640_data_select);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_if1_adc_enum, RT5640_DIG_INF_DATA,
3668c2ecf20Sopenharmony_ci			    RT5640_IF1_ADC_SEL_SFT, rt5640_data_select);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_if2_dac_enum, RT5640_DIG_INF_DATA,
3698c2ecf20Sopenharmony_ci			    RT5640_IF2_DAC_SEL_SFT, rt5640_data_select);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_if2_adc_enum, RT5640_DIG_INF_DATA,
3728c2ecf20Sopenharmony_ci			    RT5640_IF2_ADC_SEL_SFT, rt5640_data_select);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci/* Class D speaker gain ratio */
3758c2ecf20Sopenharmony_cistatic const char * const rt5640_clsd_spk_ratio[] = {"1.66x", "1.83x", "1.94x",
3768c2ecf20Sopenharmony_ci	"2x", "2.11x", "2.22x", "2.33x", "2.44x", "2.55x", "2.66x", "2.77x"};
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_clsd_spk_ratio_enum, RT5640_CLS_D_OUT,
3798c2ecf20Sopenharmony_ci			    RT5640_CLSD_RATIO_SFT, rt5640_clsd_spk_ratio);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_snd_controls[] = {
3828c2ecf20Sopenharmony_ci	/* Speaker Output Volume */
3838c2ecf20Sopenharmony_ci	SOC_DOUBLE("Speaker Channel Switch", RT5640_SPK_VOL,
3848c2ecf20Sopenharmony_ci		RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
3858c2ecf20Sopenharmony_ci	SOC_DOUBLE_TLV("Speaker Playback Volume", RT5640_SPK_VOL,
3868c2ecf20Sopenharmony_ci		RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
3878c2ecf20Sopenharmony_ci	/* Headphone Output Volume */
3888c2ecf20Sopenharmony_ci	SOC_DOUBLE("HP Channel Switch", RT5640_HP_VOL,
3898c2ecf20Sopenharmony_ci		RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
3908c2ecf20Sopenharmony_ci	SOC_DOUBLE_TLV("HP Playback Volume", RT5640_HP_VOL,
3918c2ecf20Sopenharmony_ci		RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
3928c2ecf20Sopenharmony_ci	/* OUTPUT Control */
3938c2ecf20Sopenharmony_ci	SOC_DOUBLE("OUT Playback Switch", RT5640_OUTPUT,
3948c2ecf20Sopenharmony_ci		RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
3958c2ecf20Sopenharmony_ci	SOC_DOUBLE("OUT Channel Switch", RT5640_OUTPUT,
3968c2ecf20Sopenharmony_ci		RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
3978c2ecf20Sopenharmony_ci	SOC_DOUBLE_TLV("OUT Playback Volume", RT5640_OUTPUT,
3988c2ecf20Sopenharmony_ci		RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	/* DAC Digital Volume */
4018c2ecf20Sopenharmony_ci	SOC_DOUBLE("DAC2 Playback Switch", RT5640_DAC2_CTRL,
4028c2ecf20Sopenharmony_ci		RT5640_M_DAC_L2_VOL_SFT, RT5640_M_DAC_R2_VOL_SFT, 1, 1),
4038c2ecf20Sopenharmony_ci	SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5640_DAC1_DIG_VOL,
4048c2ecf20Sopenharmony_ci			RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
4058c2ecf20Sopenharmony_ci			175, 0, dac_vol_tlv),
4068c2ecf20Sopenharmony_ci	/* IN1/IN2/IN3 Control */
4078c2ecf20Sopenharmony_ci	SOC_SINGLE_TLV("IN1 Boost", RT5640_IN1_IN2,
4088c2ecf20Sopenharmony_ci		RT5640_BST_SFT1, 8, 0, bst_tlv),
4098c2ecf20Sopenharmony_ci	SOC_SINGLE_TLV("IN2 Boost", RT5640_IN3_IN4,
4108c2ecf20Sopenharmony_ci		RT5640_BST_SFT2, 8, 0, bst_tlv),
4118c2ecf20Sopenharmony_ci	SOC_SINGLE_TLV("IN3 Boost", RT5640_IN1_IN2,
4128c2ecf20Sopenharmony_ci		RT5640_BST_SFT2, 8, 0, bst_tlv),
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* INL/INR Volume Control */
4158c2ecf20Sopenharmony_ci	SOC_DOUBLE_TLV("IN Capture Volume", RT5640_INL_INR_VOL,
4168c2ecf20Sopenharmony_ci			RT5640_INL_VOL_SFT, RT5640_INR_VOL_SFT,
4178c2ecf20Sopenharmony_ci			31, 1, in_vol_tlv),
4188c2ecf20Sopenharmony_ci	/* ADC Digital Volume Control */
4198c2ecf20Sopenharmony_ci	SOC_DOUBLE("ADC Capture Switch", RT5640_ADC_DIG_VOL,
4208c2ecf20Sopenharmony_ci		RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
4218c2ecf20Sopenharmony_ci	SOC_DOUBLE_TLV("ADC Capture Volume", RT5640_ADC_DIG_VOL,
4228c2ecf20Sopenharmony_ci			RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
4238c2ecf20Sopenharmony_ci			127, 0, adc_vol_tlv),
4248c2ecf20Sopenharmony_ci	SOC_DOUBLE("Mono ADC Capture Switch", RT5640_DUMMY1,
4258c2ecf20Sopenharmony_ci		RT5640_M_MONO_ADC_L_SFT, RT5640_M_MONO_ADC_R_SFT, 1, 1),
4268c2ecf20Sopenharmony_ci	SOC_DOUBLE_TLV("Mono ADC Capture Volume", RT5640_ADC_DATA,
4278c2ecf20Sopenharmony_ci			RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
4288c2ecf20Sopenharmony_ci			127, 0, adc_vol_tlv),
4298c2ecf20Sopenharmony_ci	/* ADC Boost Volume Control */
4308c2ecf20Sopenharmony_ci	SOC_DOUBLE_TLV("ADC Boost Gain", RT5640_ADC_BST_VOL,
4318c2ecf20Sopenharmony_ci			RT5640_ADC_L_BST_SFT, RT5640_ADC_R_BST_SFT,
4328c2ecf20Sopenharmony_ci			3, 0, adc_bst_tlv),
4338c2ecf20Sopenharmony_ci	/* Class D speaker gain ratio */
4348c2ecf20Sopenharmony_ci	SOC_ENUM("Class D SPK Ratio Control", rt5640_clsd_spk_ratio_enum),
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	SOC_ENUM("ADC IF1 Data Switch", rt5640_if1_adc_enum),
4378c2ecf20Sopenharmony_ci	SOC_ENUM("DAC IF1 Data Switch", rt5640_if1_dac_enum),
4388c2ecf20Sopenharmony_ci	SOC_ENUM("ADC IF2 Data Switch", rt5640_if2_adc_enum),
4398c2ecf20Sopenharmony_ci	SOC_ENUM("DAC IF2 Data Switch", rt5640_if2_dac_enum),
4408c2ecf20Sopenharmony_ci};
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_specific_snd_controls[] = {
4438c2ecf20Sopenharmony_ci	/* MONO Output Control */
4448c2ecf20Sopenharmony_ci	SOC_SINGLE("Mono Playback Switch", RT5640_MONO_OUT, RT5640_L_MUTE_SFT,
4458c2ecf20Sopenharmony_ci		1, 1),
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	SOC_DOUBLE_TLV("Mono DAC Playback Volume", RT5640_DAC2_DIG_VOL,
4488c2ecf20Sopenharmony_ci		RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 175, 0, dac_vol_tlv),
4498c2ecf20Sopenharmony_ci};
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci/**
4528c2ecf20Sopenharmony_ci * set_dmic_clk - Set parameter of dmic.
4538c2ecf20Sopenharmony_ci *
4548c2ecf20Sopenharmony_ci * @w: DAPM widget.
4558c2ecf20Sopenharmony_ci * @kcontrol: The kcontrol of this widget.
4568c2ecf20Sopenharmony_ci * @event: Event id.
4578c2ecf20Sopenharmony_ci *
4588c2ecf20Sopenharmony_ci */
4598c2ecf20Sopenharmony_cistatic int set_dmic_clk(struct snd_soc_dapm_widget *w,
4608c2ecf20Sopenharmony_ci	struct snd_kcontrol *kcontrol, int event)
4618c2ecf20Sopenharmony_ci{
4628c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
4638c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
4648c2ecf20Sopenharmony_ci	int idx, rate;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	rate = rt5640->sysclk / rl6231_get_pre_div(rt5640->regmap,
4678c2ecf20Sopenharmony_ci		RT5640_ADDA_CLK1, RT5640_I2S_PD1_SFT);
4688c2ecf20Sopenharmony_ci	idx = rl6231_calc_dmic_clk(rate);
4698c2ecf20Sopenharmony_ci	if (idx < 0)
4708c2ecf20Sopenharmony_ci		dev_err(component->dev, "Failed to set DMIC clock\n");
4718c2ecf20Sopenharmony_ci	else
4728c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_DMIC, RT5640_DMIC_CLK_MASK,
4738c2ecf20Sopenharmony_ci					idx << RT5640_DMIC_CLK_SFT);
4748c2ecf20Sopenharmony_ci	return idx;
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_cistatic int is_using_asrc(struct snd_soc_dapm_widget *source,
4788c2ecf20Sopenharmony_ci			 struct snd_soc_dapm_widget *sink)
4798c2ecf20Sopenharmony_ci{
4808c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
4818c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	if (!rt5640->asrc_en)
4848c2ecf20Sopenharmony_ci		return 0;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	return 1;
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci/* Digital Mixer */
4908c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_sto_adc_l_mix[] = {
4918c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("ADC1 Switch", RT5640_STO_ADC_MIXER,
4928c2ecf20Sopenharmony_ci			RT5640_M_ADC_L1_SFT, 1, 1),
4938c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("ADC2 Switch", RT5640_STO_ADC_MIXER,
4948c2ecf20Sopenharmony_ci			RT5640_M_ADC_L2_SFT, 1, 1),
4958c2ecf20Sopenharmony_ci};
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_sto_adc_r_mix[] = {
4988c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("ADC1 Switch", RT5640_STO_ADC_MIXER,
4998c2ecf20Sopenharmony_ci			RT5640_M_ADC_R1_SFT, 1, 1),
5008c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("ADC2 Switch", RT5640_STO_ADC_MIXER,
5018c2ecf20Sopenharmony_ci			RT5640_M_ADC_R2_SFT, 1, 1),
5028c2ecf20Sopenharmony_ci};
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_mono_adc_l_mix[] = {
5058c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("ADC1 Switch", RT5640_MONO_ADC_MIXER,
5068c2ecf20Sopenharmony_ci			RT5640_M_MONO_ADC_L1_SFT, 1, 1),
5078c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("ADC2 Switch", RT5640_MONO_ADC_MIXER,
5088c2ecf20Sopenharmony_ci			RT5640_M_MONO_ADC_L2_SFT, 1, 1),
5098c2ecf20Sopenharmony_ci};
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_mono_adc_r_mix[] = {
5128c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("ADC1 Switch", RT5640_MONO_ADC_MIXER,
5138c2ecf20Sopenharmony_ci			RT5640_M_MONO_ADC_R1_SFT, 1, 1),
5148c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("ADC2 Switch", RT5640_MONO_ADC_MIXER,
5158c2ecf20Sopenharmony_ci			RT5640_M_MONO_ADC_R2_SFT, 1, 1),
5168c2ecf20Sopenharmony_ci};
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_dac_l_mix[] = {
5198c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("Stereo ADC Switch", RT5640_AD_DA_MIXER,
5208c2ecf20Sopenharmony_ci			RT5640_M_ADCMIX_L_SFT, 1, 1),
5218c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("INF1 Switch", RT5640_AD_DA_MIXER,
5228c2ecf20Sopenharmony_ci			RT5640_M_IF1_DAC_L_SFT, 1, 1),
5238c2ecf20Sopenharmony_ci};
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_dac_r_mix[] = {
5268c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("Stereo ADC Switch", RT5640_AD_DA_MIXER,
5278c2ecf20Sopenharmony_ci			RT5640_M_ADCMIX_R_SFT, 1, 1),
5288c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("INF1 Switch", RT5640_AD_DA_MIXER,
5298c2ecf20Sopenharmony_ci			RT5640_M_IF1_DAC_R_SFT, 1, 1),
5308c2ecf20Sopenharmony_ci};
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_sto_dac_l_mix[] = {
5338c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_STO_DAC_MIXER,
5348c2ecf20Sopenharmony_ci			RT5640_M_DAC_L1_SFT, 1, 1),
5358c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_STO_DAC_MIXER,
5368c2ecf20Sopenharmony_ci			RT5640_M_DAC_L2_SFT, 1, 1),
5378c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("ANC Switch", RT5640_STO_DAC_MIXER,
5388c2ecf20Sopenharmony_ci			RT5640_M_ANC_DAC_L_SFT, 1, 1),
5398c2ecf20Sopenharmony_ci};
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_sto_dac_r_mix[] = {
5428c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_STO_DAC_MIXER,
5438c2ecf20Sopenharmony_ci			RT5640_M_DAC_R1_SFT, 1, 1),
5448c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_STO_DAC_MIXER,
5458c2ecf20Sopenharmony_ci			RT5640_M_DAC_R2_SFT, 1, 1),
5468c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("ANC Switch", RT5640_STO_DAC_MIXER,
5478c2ecf20Sopenharmony_ci			RT5640_M_ANC_DAC_R_SFT, 1, 1),
5488c2ecf20Sopenharmony_ci};
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5639_sto_dac_l_mix[] = {
5518c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_STO_DAC_MIXER,
5528c2ecf20Sopenharmony_ci			RT5640_M_DAC_L1_SFT, 1, 1),
5538c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_STO_DAC_MIXER,
5548c2ecf20Sopenharmony_ci			RT5640_M_DAC_L2_SFT, 1, 1),
5558c2ecf20Sopenharmony_ci};
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5639_sto_dac_r_mix[] = {
5588c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_STO_DAC_MIXER,
5598c2ecf20Sopenharmony_ci			RT5640_M_DAC_R1_SFT, 1, 1),
5608c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_STO_DAC_MIXER,
5618c2ecf20Sopenharmony_ci			RT5640_M_DAC_R2_SFT, 1, 1),
5628c2ecf20Sopenharmony_ci};
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_mono_dac_l_mix[] = {
5658c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_MONO_DAC_MIXER,
5668c2ecf20Sopenharmony_ci			RT5640_M_DAC_L1_MONO_L_SFT, 1, 1),
5678c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_DAC_MIXER,
5688c2ecf20Sopenharmony_ci			RT5640_M_DAC_L2_MONO_L_SFT, 1, 1),
5698c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_DAC_MIXER,
5708c2ecf20Sopenharmony_ci			RT5640_M_DAC_R2_MONO_L_SFT, 1, 1),
5718c2ecf20Sopenharmony_ci};
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_mono_dac_r_mix[] = {
5748c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_MONO_DAC_MIXER,
5758c2ecf20Sopenharmony_ci			RT5640_M_DAC_R1_MONO_R_SFT, 1, 1),
5768c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_DAC_MIXER,
5778c2ecf20Sopenharmony_ci			RT5640_M_DAC_R2_MONO_R_SFT, 1, 1),
5788c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_DAC_MIXER,
5798c2ecf20Sopenharmony_ci			RT5640_M_DAC_L2_MONO_R_SFT, 1, 1),
5808c2ecf20Sopenharmony_ci};
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_dig_l_mix[] = {
5838c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_DIG_MIXER,
5848c2ecf20Sopenharmony_ci			RT5640_M_STO_L_DAC_L_SFT, 1, 1),
5858c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_DIG_MIXER,
5868c2ecf20Sopenharmony_ci			RT5640_M_DAC_L2_DAC_L_SFT, 1, 1),
5878c2ecf20Sopenharmony_ci};
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_dig_r_mix[] = {
5908c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_DIG_MIXER,
5918c2ecf20Sopenharmony_ci			RT5640_M_STO_R_DAC_R_SFT, 1, 1),
5928c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_DIG_MIXER,
5938c2ecf20Sopenharmony_ci			RT5640_M_DAC_R2_DAC_R_SFT, 1, 1),
5948c2ecf20Sopenharmony_ci};
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci/* Analog Input Mixer */
5978c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_rec_l_mix[] = {
5988c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("HPOL Switch", RT5640_REC_L2_MIXER,
5998c2ecf20Sopenharmony_ci			RT5640_M_HP_L_RM_L_SFT, 1, 1),
6008c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("INL Switch", RT5640_REC_L2_MIXER,
6018c2ecf20Sopenharmony_ci			RT5640_M_IN_L_RM_L_SFT, 1, 1),
6028c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST3 Switch", RT5640_REC_L2_MIXER,
6038c2ecf20Sopenharmony_ci			RT5640_M_BST2_RM_L_SFT, 1, 1),
6048c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST2 Switch", RT5640_REC_L2_MIXER,
6058c2ecf20Sopenharmony_ci			RT5640_M_BST4_RM_L_SFT, 1, 1),
6068c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_L2_MIXER,
6078c2ecf20Sopenharmony_ci			RT5640_M_BST1_RM_L_SFT, 1, 1),
6088c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_REC_L2_MIXER,
6098c2ecf20Sopenharmony_ci			RT5640_M_OM_L_RM_L_SFT, 1, 1),
6108c2ecf20Sopenharmony_ci};
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_rec_r_mix[] = {
6138c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("HPOR Switch", RT5640_REC_R2_MIXER,
6148c2ecf20Sopenharmony_ci			RT5640_M_HP_R_RM_R_SFT, 1, 1),
6158c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("INR Switch", RT5640_REC_R2_MIXER,
6168c2ecf20Sopenharmony_ci			RT5640_M_IN_R_RM_R_SFT, 1, 1),
6178c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST3 Switch", RT5640_REC_R2_MIXER,
6188c2ecf20Sopenharmony_ci			RT5640_M_BST2_RM_R_SFT, 1, 1),
6198c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST2 Switch", RT5640_REC_R2_MIXER,
6208c2ecf20Sopenharmony_ci			RT5640_M_BST4_RM_R_SFT, 1, 1),
6218c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_R2_MIXER,
6228c2ecf20Sopenharmony_ci			RT5640_M_BST1_RM_R_SFT, 1, 1),
6238c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_REC_R2_MIXER,
6248c2ecf20Sopenharmony_ci			RT5640_M_OM_R_RM_R_SFT, 1, 1),
6258c2ecf20Sopenharmony_ci};
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci/* Analog Output Mixer */
6288c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_spk_l_mix[] = {
6298c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_SPK_L_MIXER,
6308c2ecf20Sopenharmony_ci			RT5640_M_RM_L_SM_L_SFT, 1, 1),
6318c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("INL Switch", RT5640_SPK_L_MIXER,
6328c2ecf20Sopenharmony_ci			RT5640_M_IN_L_SM_L_SFT, 1, 1),
6338c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPK_L_MIXER,
6348c2ecf20Sopenharmony_ci			RT5640_M_DAC_L1_SM_L_SFT, 1, 1),
6358c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_SPK_L_MIXER,
6368c2ecf20Sopenharmony_ci			RT5640_M_DAC_L2_SM_L_SFT, 1, 1),
6378c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_SPK_L_MIXER,
6388c2ecf20Sopenharmony_ci			RT5640_M_OM_L_SM_L_SFT, 1, 1),
6398c2ecf20Sopenharmony_ci};
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_spk_r_mix[] = {
6428c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_SPK_R_MIXER,
6438c2ecf20Sopenharmony_ci			RT5640_M_RM_R_SM_R_SFT, 1, 1),
6448c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("INR Switch", RT5640_SPK_R_MIXER,
6458c2ecf20Sopenharmony_ci			RT5640_M_IN_R_SM_R_SFT, 1, 1),
6468c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPK_R_MIXER,
6478c2ecf20Sopenharmony_ci			RT5640_M_DAC_R1_SM_R_SFT, 1, 1),
6488c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_SPK_R_MIXER,
6498c2ecf20Sopenharmony_ci			RT5640_M_DAC_R2_SM_R_SFT, 1, 1),
6508c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_SPK_R_MIXER,
6518c2ecf20Sopenharmony_ci			RT5640_M_OM_R_SM_R_SFT, 1, 1),
6528c2ecf20Sopenharmony_ci};
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_out_l_mix[] = {
6558c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("SPK MIXL Switch", RT5640_OUT_L3_MIXER,
6568c2ecf20Sopenharmony_ci			RT5640_M_SM_L_OM_L_SFT, 1, 1),
6578c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_L3_MIXER,
6588c2ecf20Sopenharmony_ci			RT5640_M_BST1_OM_L_SFT, 1, 1),
6598c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("INL Switch", RT5640_OUT_L3_MIXER,
6608c2ecf20Sopenharmony_ci			RT5640_M_IN_L_OM_L_SFT, 1, 1),
6618c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_OUT_L3_MIXER,
6628c2ecf20Sopenharmony_ci			RT5640_M_RM_L_OM_L_SFT, 1, 1),
6638c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_L3_MIXER,
6648c2ecf20Sopenharmony_ci			RT5640_M_DAC_R2_OM_L_SFT, 1, 1),
6658c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_L3_MIXER,
6668c2ecf20Sopenharmony_ci			RT5640_M_DAC_L2_OM_L_SFT, 1, 1),
6678c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_OUT_L3_MIXER,
6688c2ecf20Sopenharmony_ci			RT5640_M_DAC_L1_OM_L_SFT, 1, 1),
6698c2ecf20Sopenharmony_ci};
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_out_r_mix[] = {
6728c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("SPK MIXR Switch", RT5640_OUT_R3_MIXER,
6738c2ecf20Sopenharmony_ci			RT5640_M_SM_L_OM_R_SFT, 1, 1),
6748c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST2 Switch", RT5640_OUT_R3_MIXER,
6758c2ecf20Sopenharmony_ci			RT5640_M_BST4_OM_R_SFT, 1, 1),
6768c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_R3_MIXER,
6778c2ecf20Sopenharmony_ci			RT5640_M_BST1_OM_R_SFT, 1, 1),
6788c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("INR Switch", RT5640_OUT_R3_MIXER,
6798c2ecf20Sopenharmony_ci			RT5640_M_IN_R_OM_R_SFT, 1, 1),
6808c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_OUT_R3_MIXER,
6818c2ecf20Sopenharmony_ci			RT5640_M_RM_R_OM_R_SFT, 1, 1),
6828c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_R3_MIXER,
6838c2ecf20Sopenharmony_ci			RT5640_M_DAC_L2_OM_R_SFT, 1, 1),
6848c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_R3_MIXER,
6858c2ecf20Sopenharmony_ci			RT5640_M_DAC_R2_OM_R_SFT, 1, 1),
6868c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_OUT_R3_MIXER,
6878c2ecf20Sopenharmony_ci			RT5640_M_DAC_R1_OM_R_SFT, 1, 1),
6888c2ecf20Sopenharmony_ci};
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5639_out_l_mix[] = {
6918c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_L3_MIXER,
6928c2ecf20Sopenharmony_ci			RT5640_M_BST1_OM_L_SFT, 1, 1),
6938c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("INL Switch", RT5640_OUT_L3_MIXER,
6948c2ecf20Sopenharmony_ci			RT5640_M_IN_L_OM_L_SFT, 1, 1),
6958c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_OUT_L3_MIXER,
6968c2ecf20Sopenharmony_ci			RT5640_M_RM_L_OM_L_SFT, 1, 1),
6978c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_OUT_L3_MIXER,
6988c2ecf20Sopenharmony_ci			RT5640_M_DAC_L1_OM_L_SFT, 1, 1),
6998c2ecf20Sopenharmony_ci};
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5639_out_r_mix[] = {
7028c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST2 Switch", RT5640_OUT_R3_MIXER,
7038c2ecf20Sopenharmony_ci			RT5640_M_BST4_OM_R_SFT, 1, 1),
7048c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_R3_MIXER,
7058c2ecf20Sopenharmony_ci			RT5640_M_BST1_OM_R_SFT, 1, 1),
7068c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("INR Switch", RT5640_OUT_R3_MIXER,
7078c2ecf20Sopenharmony_ci			RT5640_M_IN_R_OM_R_SFT, 1, 1),
7088c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_OUT_R3_MIXER,
7098c2ecf20Sopenharmony_ci			RT5640_M_RM_R_OM_R_SFT, 1, 1),
7108c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_OUT_R3_MIXER,
7118c2ecf20Sopenharmony_ci			RT5640_M_DAC_R1_OM_R_SFT, 1, 1),
7128c2ecf20Sopenharmony_ci};
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_spo_l_mix[] = {
7158c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_L_MIXER,
7168c2ecf20Sopenharmony_ci			RT5640_M_DAC_R1_SPM_L_SFT, 1, 1),
7178c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPO_L_MIXER,
7188c2ecf20Sopenharmony_ci			RT5640_M_DAC_L1_SPM_L_SFT, 1, 1),
7198c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_L_MIXER,
7208c2ecf20Sopenharmony_ci			RT5640_M_SV_R_SPM_L_SFT, 1, 1),
7218c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("SPKVOL L Switch", RT5640_SPO_L_MIXER,
7228c2ecf20Sopenharmony_ci			RT5640_M_SV_L_SPM_L_SFT, 1, 1),
7238c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_L_MIXER,
7248c2ecf20Sopenharmony_ci			RT5640_M_BST1_SPM_L_SFT, 1, 1),
7258c2ecf20Sopenharmony_ci};
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_spo_r_mix[] = {
7288c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_R_MIXER,
7298c2ecf20Sopenharmony_ci			RT5640_M_DAC_R1_SPM_R_SFT, 1, 1),
7308c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_R_MIXER,
7318c2ecf20Sopenharmony_ci			RT5640_M_SV_R_SPM_R_SFT, 1, 1),
7328c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_R_MIXER,
7338c2ecf20Sopenharmony_ci			RT5640_M_BST1_SPM_R_SFT, 1, 1),
7348c2ecf20Sopenharmony_ci};
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_hpo_mix[] = {
7378c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("HPO MIX DAC2 Switch", RT5640_HPO_MIXER,
7388c2ecf20Sopenharmony_ci			RT5640_M_DAC2_HM_SFT, 1, 1),
7398c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5640_HPO_MIXER,
7408c2ecf20Sopenharmony_ci			RT5640_M_DAC1_HM_SFT, 1, 1),
7418c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5640_HPO_MIXER,
7428c2ecf20Sopenharmony_ci			RT5640_M_HPVOL_HM_SFT, 1, 1),
7438c2ecf20Sopenharmony_ci};
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5639_hpo_mix[] = {
7468c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5640_HPO_MIXER,
7478c2ecf20Sopenharmony_ci			RT5640_M_DAC1_HM_SFT, 1, 1),
7488c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5640_HPO_MIXER,
7498c2ecf20Sopenharmony_ci			RT5640_M_HPVOL_HM_SFT, 1, 1),
7508c2ecf20Sopenharmony_ci};
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_lout_mix[] = {
7538c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_LOUT_MIXER,
7548c2ecf20Sopenharmony_ci			RT5640_M_DAC_L1_LM_SFT, 1, 1),
7558c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_LOUT_MIXER,
7568c2ecf20Sopenharmony_ci			RT5640_M_DAC_R1_LM_SFT, 1, 1),
7578c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_LOUT_MIXER,
7588c2ecf20Sopenharmony_ci			RT5640_M_OV_L_LM_SFT, 1, 1),
7598c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_LOUT_MIXER,
7608c2ecf20Sopenharmony_ci			RT5640_M_OV_R_LM_SFT, 1, 1),
7618c2ecf20Sopenharmony_ci};
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_mono_mix[] = {
7648c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_MIXER,
7658c2ecf20Sopenharmony_ci			RT5640_M_DAC_R2_MM_SFT, 1, 1),
7668c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_MIXER,
7678c2ecf20Sopenharmony_ci			RT5640_M_DAC_L2_MM_SFT, 1, 1),
7688c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_MONO_MIXER,
7698c2ecf20Sopenharmony_ci			RT5640_M_OV_R_MM_SFT, 1, 1),
7708c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_MONO_MIXER,
7718c2ecf20Sopenharmony_ci			RT5640_M_OV_L_MM_SFT, 1, 1),
7728c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("BST1 Switch", RT5640_MONO_MIXER,
7738c2ecf20Sopenharmony_ci			RT5640_M_BST1_MM_SFT, 1, 1),
7748c2ecf20Sopenharmony_ci};
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new spk_l_enable_control =
7778c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_SPK_VOL,
7788c2ecf20Sopenharmony_ci		RT5640_L_MUTE_SFT, 1, 1);
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new spk_r_enable_control =
7818c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_SPK_VOL,
7828c2ecf20Sopenharmony_ci		RT5640_R_MUTE_SFT, 1, 1);
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new hp_l_enable_control =
7858c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_HP_VOL,
7868c2ecf20Sopenharmony_ci		RT5640_L_MUTE_SFT, 1, 1);
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new hp_r_enable_control =
7898c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5640_HP_VOL,
7908c2ecf20Sopenharmony_ci		RT5640_R_MUTE_SFT, 1, 1);
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci/* Stereo ADC source */
7938c2ecf20Sopenharmony_cistatic const char * const rt5640_stereo_adc1_src[] = {
7948c2ecf20Sopenharmony_ci	"DIG MIX", "ADC"
7958c2ecf20Sopenharmony_ci};
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc1_enum, RT5640_STO_ADC_MIXER,
7988c2ecf20Sopenharmony_ci			    RT5640_ADC_1_SRC_SFT, rt5640_stereo_adc1_src);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_sto_adc_1_mux =
8018c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("Stereo ADC1 Mux", rt5640_stereo_adc1_enum);
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistatic const char * const rt5640_stereo_adc2_src[] = {
8048c2ecf20Sopenharmony_ci	"DMIC1", "DMIC2", "DIG MIX"
8058c2ecf20Sopenharmony_ci};
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_stereo_adc2_enum, RT5640_STO_ADC_MIXER,
8088c2ecf20Sopenharmony_ci			    RT5640_ADC_2_SRC_SFT, rt5640_stereo_adc2_src);
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_sto_adc_2_mux =
8118c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("Stereo ADC2 Mux", rt5640_stereo_adc2_enum);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci/* Mono ADC source */
8148c2ecf20Sopenharmony_cistatic const char * const rt5640_mono_adc_l1_src[] = {
8158c2ecf20Sopenharmony_ci	"Mono DAC MIXL", "ADCL"
8168c2ecf20Sopenharmony_ci};
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_l1_enum, RT5640_MONO_ADC_MIXER,
8198c2ecf20Sopenharmony_ci			    RT5640_MONO_ADC_L1_SRC_SFT, rt5640_mono_adc_l1_src);
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_mono_adc_l1_mux =
8228c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("Mono ADC1 left source", rt5640_mono_adc_l1_enum);
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_cistatic const char * const rt5640_mono_adc_l2_src[] = {
8258c2ecf20Sopenharmony_ci	"DMIC L1", "DMIC L2", "Mono DAC MIXL"
8268c2ecf20Sopenharmony_ci};
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_l2_enum, RT5640_MONO_ADC_MIXER,
8298c2ecf20Sopenharmony_ci			    RT5640_MONO_ADC_L2_SRC_SFT, rt5640_mono_adc_l2_src);
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_mono_adc_l2_mux =
8328c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("Mono ADC2 left source", rt5640_mono_adc_l2_enum);
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_cistatic const char * const rt5640_mono_adc_r1_src[] = {
8358c2ecf20Sopenharmony_ci	"Mono DAC MIXR", "ADCR"
8368c2ecf20Sopenharmony_ci};
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_r1_enum, RT5640_MONO_ADC_MIXER,
8398c2ecf20Sopenharmony_ci			    RT5640_MONO_ADC_R1_SRC_SFT, rt5640_mono_adc_r1_src);
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_mono_adc_r1_mux =
8428c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("Mono ADC1 right source", rt5640_mono_adc_r1_enum);
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_cistatic const char * const rt5640_mono_adc_r2_src[] = {
8458c2ecf20Sopenharmony_ci	"DMIC R1", "DMIC R2", "Mono DAC MIXR"
8468c2ecf20Sopenharmony_ci};
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_mono_adc_r2_enum, RT5640_MONO_ADC_MIXER,
8498c2ecf20Sopenharmony_ci			    RT5640_MONO_ADC_R2_SRC_SFT, rt5640_mono_adc_r2_src);
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_mono_adc_r2_mux =
8528c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("Mono ADC2 right source", rt5640_mono_adc_r2_enum);
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci/* DAC2 channel source */
8558c2ecf20Sopenharmony_cistatic const char * const rt5640_dac_l2_src[] = {
8568c2ecf20Sopenharmony_ci	"IF2", "Base L/R"
8578c2ecf20Sopenharmony_ci};
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cistatic int rt5640_dac_l2_values[] = {
8608c2ecf20Sopenharmony_ci	0,
8618c2ecf20Sopenharmony_ci	3,
8628c2ecf20Sopenharmony_ci};
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_cistatic SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dac_l2_enum,
8658c2ecf20Sopenharmony_ci				  RT5640_DSP_PATH2, RT5640_DAC_L2_SEL_SFT,
8668c2ecf20Sopenharmony_ci				  0x3, rt5640_dac_l2_src, rt5640_dac_l2_values);
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_dac_l2_mux =
8698c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("DAC2 left channel source", rt5640_dac_l2_enum);
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_cistatic const char * const rt5640_dac_r2_src[] = {
8728c2ecf20Sopenharmony_ci	"IF2",
8738c2ecf20Sopenharmony_ci};
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_cistatic int rt5640_dac_r2_values[] = {
8768c2ecf20Sopenharmony_ci	0,
8778c2ecf20Sopenharmony_ci};
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_cistatic SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dac_r2_enum,
8808c2ecf20Sopenharmony_ci				  RT5640_DSP_PATH2, RT5640_DAC_R2_SEL_SFT,
8818c2ecf20Sopenharmony_ci				  0x3, rt5640_dac_r2_src, rt5640_dac_r2_values);
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_dac_r2_mux =
8848c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("DAC2 right channel source", rt5640_dac_r2_enum);
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci/* digital interface and iis interface map */
8878c2ecf20Sopenharmony_cistatic const char * const rt5640_dai_iis_map[] = {
8888c2ecf20Sopenharmony_ci	"1:1|2:2", "1:2|2:1", "1:1|2:1", "1:2|2:2"
8898c2ecf20Sopenharmony_ci};
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_cistatic int rt5640_dai_iis_map_values[] = {
8928c2ecf20Sopenharmony_ci	0,
8938c2ecf20Sopenharmony_ci	5,
8948c2ecf20Sopenharmony_ci	6,
8958c2ecf20Sopenharmony_ci	7,
8968c2ecf20Sopenharmony_ci};
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_cistatic SOC_VALUE_ENUM_SINGLE_DECL(rt5640_dai_iis_map_enum,
8998c2ecf20Sopenharmony_ci				  RT5640_I2S1_SDP, RT5640_I2S_IF_SFT,
9008c2ecf20Sopenharmony_ci				  0x7, rt5640_dai_iis_map,
9018c2ecf20Sopenharmony_ci				  rt5640_dai_iis_map_values);
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_dai_mux =
9048c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("DAI select", rt5640_dai_iis_map_enum);
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci/* SDI select */
9078c2ecf20Sopenharmony_cistatic const char * const rt5640_sdi_sel[] = {
9088c2ecf20Sopenharmony_ci	"IF1", "IF2"
9098c2ecf20Sopenharmony_ci};
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(rt5640_sdi_sel_enum, RT5640_I2S2_SDP,
9128c2ecf20Sopenharmony_ci			    RT5640_I2S2_SDI_SFT, rt5640_sdi_sel);
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5640_sdi_mux =
9158c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("SDI select", rt5640_sdi_sel_enum);
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_cistatic void hp_amp_power_on(struct snd_soc_component *component)
9188c2ecf20Sopenharmony_ci{
9198c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	/* depop parameters */
9228c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_PR_BASE +
9238c2ecf20Sopenharmony_ci		RT5640_CHPUMP_INT_REG1, 0x0700, 0x0200);
9248c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M2,
9258c2ecf20Sopenharmony_ci		RT5640_DEPOP_MASK, RT5640_DEPOP_MAN);
9268c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M1,
9278c2ecf20Sopenharmony_ci		RT5640_HP_CP_MASK | RT5640_HP_SG_MASK | RT5640_HP_CB_MASK,
9288c2ecf20Sopenharmony_ci		RT5640_HP_CP_PU | RT5640_HP_SG_DIS | RT5640_HP_CB_PU);
9298c2ecf20Sopenharmony_ci	regmap_write(rt5640->regmap, RT5640_PR_BASE + RT5640_HP_DCC_INT1,
9308c2ecf20Sopenharmony_ci			   0x9f00);
9318c2ecf20Sopenharmony_ci	/* headphone amp power on */
9328c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
9338c2ecf20Sopenharmony_ci		RT5640_PWR_FV1 | RT5640_PWR_FV2, 0);
9348c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
9358c2ecf20Sopenharmony_ci		RT5640_PWR_HA,
9368c2ecf20Sopenharmony_ci		RT5640_PWR_HA);
9378c2ecf20Sopenharmony_ci	usleep_range(10000, 15000);
9388c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1,
9398c2ecf20Sopenharmony_ci		RT5640_PWR_FV1 | RT5640_PWR_FV2 ,
9408c2ecf20Sopenharmony_ci		RT5640_PWR_FV1 | RT5640_PWR_FV2);
9418c2ecf20Sopenharmony_ci}
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_cistatic void rt5640_pmu_depop(struct snd_soc_component *component)
9448c2ecf20Sopenharmony_ci{
9458c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M2,
9488c2ecf20Sopenharmony_ci		RT5640_DEPOP_MASK | RT5640_DIG_DP_MASK,
9498c2ecf20Sopenharmony_ci		RT5640_DEPOP_AUTO | RT5640_DIG_DP_EN);
9508c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_CHARGE_PUMP,
9518c2ecf20Sopenharmony_ci		RT5640_PM_HP_MASK, RT5640_PM_HP_HV);
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M3,
9548c2ecf20Sopenharmony_ci		RT5640_CP_FQ1_MASK | RT5640_CP_FQ2_MASK | RT5640_CP_FQ3_MASK,
9558c2ecf20Sopenharmony_ci		(RT5640_CP_FQ_192_KHZ << RT5640_CP_FQ1_SFT) |
9568c2ecf20Sopenharmony_ci		(RT5640_CP_FQ_12_KHZ << RT5640_CP_FQ2_SFT) |
9578c2ecf20Sopenharmony_ci		(RT5640_CP_FQ_192_KHZ << RT5640_CP_FQ3_SFT));
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	regmap_write(rt5640->regmap, RT5640_PR_BASE +
9608c2ecf20Sopenharmony_ci		RT5640_MAMP_INT_REG2, 0x1c00);
9618c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M1,
9628c2ecf20Sopenharmony_ci		RT5640_HP_CP_MASK | RT5640_HP_SG_MASK,
9638c2ecf20Sopenharmony_ci		RT5640_HP_CP_PD | RT5640_HP_SG_EN);
9648c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_PR_BASE +
9658c2ecf20Sopenharmony_ci		RT5640_CHPUMP_INT_REG1, 0x0700, 0x0400);
9668c2ecf20Sopenharmony_ci}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_cistatic int rt5640_hp_event(struct snd_soc_dapm_widget *w,
9698c2ecf20Sopenharmony_ci			   struct snd_kcontrol *kcontrol, int event)
9708c2ecf20Sopenharmony_ci{
9718c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
9728c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	switch (event) {
9758c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_POST_PMU:
9768c2ecf20Sopenharmony_ci		rt5640_pmu_depop(component);
9778c2ecf20Sopenharmony_ci		rt5640->hp_mute = false;
9788c2ecf20Sopenharmony_ci		break;
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_PRE_PMD:
9818c2ecf20Sopenharmony_ci		rt5640->hp_mute = true;
9828c2ecf20Sopenharmony_ci		msleep(70);
9838c2ecf20Sopenharmony_ci		break;
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	default:
9868c2ecf20Sopenharmony_ci		return 0;
9878c2ecf20Sopenharmony_ci	}
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	return 0;
9908c2ecf20Sopenharmony_ci}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_cistatic int rt5640_lout_event(struct snd_soc_dapm_widget *w,
9938c2ecf20Sopenharmony_ci	struct snd_kcontrol *kcontrol, int event)
9948c2ecf20Sopenharmony_ci{
9958c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	switch (event) {
9988c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_POST_PMU:
9998c2ecf20Sopenharmony_ci		hp_amp_power_on(component);
10008c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
10018c2ecf20Sopenharmony_ci			RT5640_PWR_LM, RT5640_PWR_LM);
10028c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_OUTPUT,
10038c2ecf20Sopenharmony_ci			RT5640_L_MUTE | RT5640_R_MUTE, 0);
10048c2ecf20Sopenharmony_ci		break;
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_PRE_PMD:
10078c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_OUTPUT,
10088c2ecf20Sopenharmony_ci			RT5640_L_MUTE | RT5640_R_MUTE,
10098c2ecf20Sopenharmony_ci			RT5640_L_MUTE | RT5640_R_MUTE);
10108c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
10118c2ecf20Sopenharmony_ci			RT5640_PWR_LM, 0);
10128c2ecf20Sopenharmony_ci		break;
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	default:
10158c2ecf20Sopenharmony_ci		return 0;
10168c2ecf20Sopenharmony_ci	}
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	return 0;
10198c2ecf20Sopenharmony_ci}
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_cistatic int rt5640_hp_power_event(struct snd_soc_dapm_widget *w,
10228c2ecf20Sopenharmony_ci			   struct snd_kcontrol *kcontrol, int event)
10238c2ecf20Sopenharmony_ci{
10248c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	switch (event) {
10278c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_POST_PMU:
10288c2ecf20Sopenharmony_ci		hp_amp_power_on(component);
10298c2ecf20Sopenharmony_ci		break;
10308c2ecf20Sopenharmony_ci	default:
10318c2ecf20Sopenharmony_ci		return 0;
10328c2ecf20Sopenharmony_ci	}
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	return 0;
10358c2ecf20Sopenharmony_ci}
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_cistatic int rt5640_hp_post_event(struct snd_soc_dapm_widget *w,
10388c2ecf20Sopenharmony_ci			   struct snd_kcontrol *kcontrol, int event)
10398c2ecf20Sopenharmony_ci{
10408c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
10418c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	switch (event) {
10448c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_POST_PMU:
10458c2ecf20Sopenharmony_ci		if (!rt5640->hp_mute)
10468c2ecf20Sopenharmony_ci			msleep(80);
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci		break;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	default:
10518c2ecf20Sopenharmony_ci		return 0;
10528c2ecf20Sopenharmony_ci	}
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	return 0;
10558c2ecf20Sopenharmony_ci}
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget rt5640_dapm_widgets[] = {
10588c2ecf20Sopenharmony_ci	/* ASRC */
10598c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY_S("Stereo Filter ASRC", 1, RT5640_ASRC_1,
10608c2ecf20Sopenharmony_ci			 15, 0, NULL, 0),
10618c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY_S("I2S2 Filter ASRC", 1, RT5640_ASRC_1,
10628c2ecf20Sopenharmony_ci			 12, 0, NULL, 0),
10638c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY_S("I2S2 ASRC", 1, RT5640_ASRC_1,
10648c2ecf20Sopenharmony_ci			 11, 0, NULL, 0),
10658c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY_S("DMIC1 ASRC", 1, RT5640_ASRC_1,
10668c2ecf20Sopenharmony_ci			 9, 0, NULL, 0),
10678c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY_S("DMIC2 ASRC", 1, RT5640_ASRC_1,
10688c2ecf20Sopenharmony_ci			 8, 0, NULL, 0),
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	/* Input Side */
10728c2ecf20Sopenharmony_ci	/* micbias */
10738c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("LDO2", RT5640_PWR_ANLG1,
10748c2ecf20Sopenharmony_ci			RT5640_PWR_LDO2_BIT, 0, NULL, 0),
10758c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("MICBIAS1", RT5640_PWR_ANLG2,
10768c2ecf20Sopenharmony_ci			RT5640_PWR_MB1_BIT, 0, NULL, 0),
10778c2ecf20Sopenharmony_ci	/* Input Lines */
10788c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("DMIC1"),
10798c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("DMIC2"),
10808c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("IN1P"),
10818c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("IN1N"),
10828c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("IN2P"),
10838c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("IN2N"),
10848c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("IN3P"),
10858c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("IN3N"),
10868c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("DMIC L1", SND_SOC_NOPM, 0, 0, NULL, 0),
10878c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("DMIC R1", SND_SOC_NOPM, 0, 0, NULL, 0),
10888c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("DMIC L2", SND_SOC_NOPM, 0, 0, NULL, 0),
10898c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("DMIC R2", SND_SOC_NOPM, 0, 0, NULL, 0),
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
10928c2ecf20Sopenharmony_ci		set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
10938c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("DMIC1 Power", RT5640_DMIC, RT5640_DMIC_1_EN_SFT, 0,
10948c2ecf20Sopenharmony_ci		NULL, 0),
10958c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("DMIC2 Power", RT5640_DMIC, RT5640_DMIC_2_EN_SFT, 0,
10968c2ecf20Sopenharmony_ci		NULL, 0),
10978c2ecf20Sopenharmony_ci	/* Boost */
10988c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("BST1", RT5640_PWR_ANLG2,
10998c2ecf20Sopenharmony_ci		RT5640_PWR_BST1_BIT, 0, NULL, 0),
11008c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("BST2", RT5640_PWR_ANLG2,
11018c2ecf20Sopenharmony_ci		RT5640_PWR_BST4_BIT, 0, NULL, 0),
11028c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("BST3", RT5640_PWR_ANLG2,
11038c2ecf20Sopenharmony_ci		RT5640_PWR_BST2_BIT, 0, NULL, 0),
11048c2ecf20Sopenharmony_ci	/* Input Volume */
11058c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("INL VOL", RT5640_PWR_VOL,
11068c2ecf20Sopenharmony_ci		RT5640_PWR_IN_L_BIT, 0, NULL, 0),
11078c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("INR VOL", RT5640_PWR_VOL,
11088c2ecf20Sopenharmony_ci		RT5640_PWR_IN_R_BIT, 0, NULL, 0),
11098c2ecf20Sopenharmony_ci	/* REC Mixer */
11108c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("RECMIXL", RT5640_PWR_MIXER, RT5640_PWR_RM_L_BIT, 0,
11118c2ecf20Sopenharmony_ci			rt5640_rec_l_mix, ARRAY_SIZE(rt5640_rec_l_mix)),
11128c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("RECMIXR", RT5640_PWR_MIXER, RT5640_PWR_RM_R_BIT, 0,
11138c2ecf20Sopenharmony_ci			rt5640_rec_r_mix, ARRAY_SIZE(rt5640_rec_r_mix)),
11148c2ecf20Sopenharmony_ci	/* ADCs */
11158c2ecf20Sopenharmony_ci	SND_SOC_DAPM_ADC("ADC L", NULL, RT5640_PWR_DIG1,
11168c2ecf20Sopenharmony_ci			RT5640_PWR_ADC_L_BIT, 0),
11178c2ecf20Sopenharmony_ci	SND_SOC_DAPM_ADC("ADC R", NULL, RT5640_PWR_DIG1,
11188c2ecf20Sopenharmony_ci			RT5640_PWR_ADC_R_BIT, 0),
11198c2ecf20Sopenharmony_ci	/* ADC Mux */
11208c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("Stereo ADC L2 Mux", SND_SOC_NOPM, 0, 0,
11218c2ecf20Sopenharmony_ci				&rt5640_sto_adc_2_mux),
11228c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("Stereo ADC R2 Mux", SND_SOC_NOPM, 0, 0,
11238c2ecf20Sopenharmony_ci				&rt5640_sto_adc_2_mux),
11248c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("Stereo ADC L1 Mux", SND_SOC_NOPM, 0, 0,
11258c2ecf20Sopenharmony_ci				&rt5640_sto_adc_1_mux),
11268c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("Stereo ADC R1 Mux", SND_SOC_NOPM, 0, 0,
11278c2ecf20Sopenharmony_ci				&rt5640_sto_adc_1_mux),
11288c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("Mono ADC L2 Mux", SND_SOC_NOPM, 0, 0,
11298c2ecf20Sopenharmony_ci				&rt5640_mono_adc_l2_mux),
11308c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("Mono ADC L1 Mux", SND_SOC_NOPM, 0, 0,
11318c2ecf20Sopenharmony_ci				&rt5640_mono_adc_l1_mux),
11328c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("Mono ADC R1 Mux", SND_SOC_NOPM, 0, 0,
11338c2ecf20Sopenharmony_ci				&rt5640_mono_adc_r1_mux),
11348c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("Mono ADC R2 Mux", SND_SOC_NOPM, 0, 0,
11358c2ecf20Sopenharmony_ci				&rt5640_mono_adc_r2_mux),
11368c2ecf20Sopenharmony_ci	/* ADC Mixer */
11378c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("Stereo Filter", RT5640_PWR_DIG2,
11388c2ecf20Sopenharmony_ci		RT5640_PWR_ADC_SF_BIT, 0, NULL, 0),
11398c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Stereo ADC MIXL", SND_SOC_NOPM, 0, 0,
11408c2ecf20Sopenharmony_ci		rt5640_sto_adc_l_mix, ARRAY_SIZE(rt5640_sto_adc_l_mix)),
11418c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Stereo ADC MIXR", SND_SOC_NOPM, 0, 0,
11428c2ecf20Sopenharmony_ci		rt5640_sto_adc_r_mix, ARRAY_SIZE(rt5640_sto_adc_r_mix)),
11438c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("Mono Left Filter", RT5640_PWR_DIG2,
11448c2ecf20Sopenharmony_ci		RT5640_PWR_ADC_MF_L_BIT, 0, NULL, 0),
11458c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Mono ADC MIXL", SND_SOC_NOPM, 0, 0,
11468c2ecf20Sopenharmony_ci		rt5640_mono_adc_l_mix, ARRAY_SIZE(rt5640_mono_adc_l_mix)),
11478c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("Mono Right Filter", RT5640_PWR_DIG2,
11488c2ecf20Sopenharmony_ci		RT5640_PWR_ADC_MF_R_BIT, 0, NULL, 0),
11498c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Mono ADC MIXR", SND_SOC_NOPM, 0, 0,
11508c2ecf20Sopenharmony_ci		rt5640_mono_adc_r_mix, ARRAY_SIZE(rt5640_mono_adc_r_mix)),
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci	/* Digital Interface */
11538c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("I2S1", RT5640_PWR_DIG1,
11548c2ecf20Sopenharmony_ci		RT5640_PWR_I2S1_BIT, 0, NULL, 0),
11558c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
11568c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF1 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
11578c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF1 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
11588c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF1 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
11598c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF1 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
11608c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF1 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
11618c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("I2S2", RT5640_PWR_DIG1,
11628c2ecf20Sopenharmony_ci		RT5640_PWR_I2S2_BIT, 0, NULL, 0),
11638c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF2 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
11648c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF2 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
11658c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF2 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
11668c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF2 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
11678c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF2 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
11688c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("IF2 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
11698c2ecf20Sopenharmony_ci	/* Digital Interface Select */
11708c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DAI1 RX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
11718c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DAI1 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
11728c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DAI1 IF1 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
11738c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DAI1 IF2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
11748c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("SDI1 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_sdi_mux),
11758c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DAI2 RX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
11768c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DAI2 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
11778c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DAI2 IF1 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
11788c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DAI2 IF2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
11798c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("SDI2 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_sdi_mux),
11808c2ecf20Sopenharmony_ci	/* Audio Interface */
11818c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
11828c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
11838c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
11848c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	/* Output Side */
11878c2ecf20Sopenharmony_ci	/* DAC mixer before sound effect  */
11888c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("DAC MIXL", SND_SOC_NOPM, 0, 0,
11898c2ecf20Sopenharmony_ci		rt5640_dac_l_mix, ARRAY_SIZE(rt5640_dac_l_mix)),
11908c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("DAC MIXR", SND_SOC_NOPM, 0, 0,
11918c2ecf20Sopenharmony_ci		rt5640_dac_r_mix, ARRAY_SIZE(rt5640_dac_r_mix)),
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	/* DAC Mixer */
11948c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Mono DAC MIXL", SND_SOC_NOPM, 0, 0,
11958c2ecf20Sopenharmony_ci		rt5640_mono_dac_l_mix, ARRAY_SIZE(rt5640_mono_dac_l_mix)),
11968c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Mono DAC MIXR", SND_SOC_NOPM, 0, 0,
11978c2ecf20Sopenharmony_ci		rt5640_mono_dac_r_mix, ARRAY_SIZE(rt5640_mono_dac_r_mix)),
11988c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("DIG MIXL", SND_SOC_NOPM, 0, 0,
11998c2ecf20Sopenharmony_ci		rt5640_dig_l_mix, ARRAY_SIZE(rt5640_dig_l_mix)),
12008c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("DIG MIXR", SND_SOC_NOPM, 0, 0,
12018c2ecf20Sopenharmony_ci		rt5640_dig_r_mix, ARRAY_SIZE(rt5640_dig_r_mix)),
12028c2ecf20Sopenharmony_ci	/* DACs */
12038c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC("DAC L1", NULL, SND_SOC_NOPM,
12048c2ecf20Sopenharmony_ci			0, 0),
12058c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC("DAC R1", NULL, SND_SOC_NOPM,
12068c2ecf20Sopenharmony_ci			0, 0),
12078c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("DAC L1 Power", RT5640_PWR_DIG1,
12088c2ecf20Sopenharmony_ci		RT5640_PWR_DAC_L1_BIT, 0, NULL, 0),
12098c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("DAC R1 Power", RT5640_PWR_DIG1,
12108c2ecf20Sopenharmony_ci		RT5640_PWR_DAC_R1_BIT, 0, NULL, 0),
12118c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("DAC L2 Power", RT5640_PWR_DIG1,
12128c2ecf20Sopenharmony_ci		RT5640_PWR_DAC_L2_BIT, 0, NULL, 0),
12138c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("DAC R2 Power", RT5640_PWR_DIG1,
12148c2ecf20Sopenharmony_ci		RT5640_PWR_DAC_R2_BIT, 0, NULL, 0),
12158c2ecf20Sopenharmony_ci	/* SPK/OUT Mixer */
12168c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("SPK MIXL", RT5640_PWR_MIXER, RT5640_PWR_SM_L_BIT,
12178c2ecf20Sopenharmony_ci		0, rt5640_spk_l_mix, ARRAY_SIZE(rt5640_spk_l_mix)),
12188c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("SPK MIXR", RT5640_PWR_MIXER, RT5640_PWR_SM_R_BIT,
12198c2ecf20Sopenharmony_ci		0, rt5640_spk_r_mix, ARRAY_SIZE(rt5640_spk_r_mix)),
12208c2ecf20Sopenharmony_ci	/* Ouput Volume */
12218c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("SPKVOL L", RT5640_PWR_VOL,
12228c2ecf20Sopenharmony_ci		RT5640_PWR_SV_L_BIT, 0, NULL, 0),
12238c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("SPKVOL R", RT5640_PWR_VOL,
12248c2ecf20Sopenharmony_ci		RT5640_PWR_SV_R_BIT, 0, NULL, 0),
12258c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("OUTVOL L", RT5640_PWR_VOL,
12268c2ecf20Sopenharmony_ci		RT5640_PWR_OV_L_BIT, 0, NULL, 0),
12278c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("OUTVOL R", RT5640_PWR_VOL,
12288c2ecf20Sopenharmony_ci		RT5640_PWR_OV_R_BIT, 0, NULL, 0),
12298c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("HPOVOL L", RT5640_PWR_VOL,
12308c2ecf20Sopenharmony_ci		RT5640_PWR_HV_L_BIT, 0, NULL, 0),
12318c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("HPOVOL R", RT5640_PWR_VOL,
12328c2ecf20Sopenharmony_ci		RT5640_PWR_HV_R_BIT, 0, NULL, 0),
12338c2ecf20Sopenharmony_ci	/* SPO/HPO/LOUT/Mono Mixer */
12348c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("SPOL MIX", SND_SOC_NOPM, 0,
12358c2ecf20Sopenharmony_ci		0, rt5640_spo_l_mix, ARRAY_SIZE(rt5640_spo_l_mix)),
12368c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("SPOR MIX", SND_SOC_NOPM, 0,
12378c2ecf20Sopenharmony_ci		0, rt5640_spo_r_mix, ARRAY_SIZE(rt5640_spo_r_mix)),
12388c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("LOUT MIX", SND_SOC_NOPM, 0, 0,
12398c2ecf20Sopenharmony_ci		rt5640_lout_mix, ARRAY_SIZE(rt5640_lout_mix)),
12408c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY_S("Improve HP Amp Drv", 1, SND_SOC_NOPM,
12418c2ecf20Sopenharmony_ci		0, 0, rt5640_hp_power_event, SND_SOC_DAPM_POST_PMU),
12428c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0,
12438c2ecf20Sopenharmony_ci		rt5640_hp_event,
12448c2ecf20Sopenharmony_ci		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
12458c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA_S("LOUT amp", 1, SND_SOC_NOPM, 0, 0,
12468c2ecf20Sopenharmony_ci		rt5640_lout_event,
12478c2ecf20Sopenharmony_ci		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
12488c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("HP L Amp", RT5640_PWR_ANLG1,
12498c2ecf20Sopenharmony_ci		RT5640_PWR_HP_L_BIT, 0, NULL, 0),
12508c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("HP R Amp", RT5640_PWR_ANLG1,
12518c2ecf20Sopenharmony_ci		RT5640_PWR_HP_R_BIT, 0, NULL, 0),
12528c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("Improve SPK Amp Drv", RT5640_PWR_DIG1,
12538c2ecf20Sopenharmony_ci		RT5640_PWR_CLS_D_BIT, 0, NULL, 0),
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci	/* Output Switch */
12568c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SWITCH("Speaker L Playback", SND_SOC_NOPM, 0, 0,
12578c2ecf20Sopenharmony_ci			&spk_l_enable_control),
12588c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SWITCH("Speaker R Playback", SND_SOC_NOPM, 0, 0,
12598c2ecf20Sopenharmony_ci			&spk_r_enable_control),
12608c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SWITCH("HP L Playback", SND_SOC_NOPM, 0, 0,
12618c2ecf20Sopenharmony_ci			&hp_l_enable_control),
12628c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SWITCH("HP R Playback", SND_SOC_NOPM, 0, 0,
12638c2ecf20Sopenharmony_ci			&hp_r_enable_control),
12648c2ecf20Sopenharmony_ci	SND_SOC_DAPM_POST("HP Post", rt5640_hp_post_event),
12658c2ecf20Sopenharmony_ci	/* Output Lines */
12668c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("SPOLP"),
12678c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("SPOLN"),
12688c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("SPORP"),
12698c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("SPORN"),
12708c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("HPOL"),
12718c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("HPOR"),
12728c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("LOUTL"),
12738c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("LOUTR"),
12748c2ecf20Sopenharmony_ci};
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget rt5640_specific_dapm_widgets[] = {
12778c2ecf20Sopenharmony_ci	/* Audio DSP */
12788c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),
12798c2ecf20Sopenharmony_ci	/* ANC */
12808c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("ANC", SND_SOC_NOPM, 0, 0, NULL, 0),
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci	/* DAC2 channel Mux */
12838c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DAC L2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dac_l2_mux),
12848c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("DAC R2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dac_r2_mux),
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
12878c2ecf20Sopenharmony_ci		rt5640_sto_dac_l_mix, ARRAY_SIZE(rt5640_sto_dac_l_mix)),
12888c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
12898c2ecf20Sopenharmony_ci		rt5640_sto_dac_r_mix, ARRAY_SIZE(rt5640_sto_dac_r_mix)),
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC("DAC R2", NULL, SND_SOC_NOPM, 0,
12928c2ecf20Sopenharmony_ci		0),
12938c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC("DAC L2", NULL, SND_SOC_NOPM, 0,
12948c2ecf20Sopenharmony_ci		0),
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("OUT MIXL", RT5640_PWR_MIXER, RT5640_PWR_OM_L_BIT,
12978c2ecf20Sopenharmony_ci		0, rt5640_out_l_mix, ARRAY_SIZE(rt5640_out_l_mix)),
12988c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("OUT MIXR", RT5640_PWR_MIXER, RT5640_PWR_OM_R_BIT,
12998c2ecf20Sopenharmony_ci		0, rt5640_out_r_mix, ARRAY_SIZE(rt5640_out_r_mix)),
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("HPO MIX L", SND_SOC_NOPM, 0, 0,
13028c2ecf20Sopenharmony_ci		rt5640_hpo_mix, ARRAY_SIZE(rt5640_hpo_mix)),
13038c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("HPO MIX R", SND_SOC_NOPM, 0, 0,
13048c2ecf20Sopenharmony_ci		rt5640_hpo_mix, ARRAY_SIZE(rt5640_hpo_mix)),
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Mono MIX", RT5640_PWR_ANLG1, RT5640_PWR_MM_BIT, 0,
13078c2ecf20Sopenharmony_ci		rt5640_mono_mix, ARRAY_SIZE(rt5640_mono_mix)),
13088c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("Improve MONO Amp Drv", RT5640_PWR_ANLG1,
13098c2ecf20Sopenharmony_ci		RT5640_PWR_MA_BIT, 0, NULL, 0),
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("MONOP"),
13128c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("MONON"),
13138c2ecf20Sopenharmony_ci};
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget rt5639_specific_dapm_widgets[] = {
13168c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
13178c2ecf20Sopenharmony_ci		rt5639_sto_dac_l_mix, ARRAY_SIZE(rt5639_sto_dac_l_mix)),
13188c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
13198c2ecf20Sopenharmony_ci		rt5639_sto_dac_r_mix, ARRAY_SIZE(rt5639_sto_dac_r_mix)),
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("OUT MIXL", RT5640_PWR_MIXER, RT5640_PWR_OM_L_BIT,
13228c2ecf20Sopenharmony_ci		0, rt5639_out_l_mix, ARRAY_SIZE(rt5639_out_l_mix)),
13238c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("OUT MIXR", RT5640_PWR_MIXER, RT5640_PWR_OM_R_BIT,
13248c2ecf20Sopenharmony_ci		0, rt5639_out_r_mix, ARRAY_SIZE(rt5639_out_r_mix)),
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("HPO MIX L", SND_SOC_NOPM, 0, 0,
13278c2ecf20Sopenharmony_ci		rt5639_hpo_mix, ARRAY_SIZE(rt5639_hpo_mix)),
13288c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIXER("HPO MIX R", SND_SOC_NOPM, 0, 0,
13298c2ecf20Sopenharmony_ci		rt5639_hpo_mix, ARRAY_SIZE(rt5639_hpo_mix)),
13308c2ecf20Sopenharmony_ci};
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route rt5640_dapm_routes[] = {
13338c2ecf20Sopenharmony_ci	{ "I2S1", NULL, "Stereo Filter ASRC", is_using_asrc },
13348c2ecf20Sopenharmony_ci	{ "I2S2", NULL, "I2S2 ASRC", is_using_asrc },
13358c2ecf20Sopenharmony_ci	{ "I2S2", NULL, "I2S2 Filter ASRC", is_using_asrc },
13368c2ecf20Sopenharmony_ci	{ "DMIC1", NULL, "DMIC1 ASRC", is_using_asrc },
13378c2ecf20Sopenharmony_ci	{ "DMIC2", NULL, "DMIC2 ASRC", is_using_asrc },
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	{"IN1P", NULL, "LDO2"},
13408c2ecf20Sopenharmony_ci	{"IN2P", NULL, "LDO2"},
13418c2ecf20Sopenharmony_ci	{"IN3P", NULL, "LDO2"},
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	{"DMIC L1", NULL, "DMIC1"},
13448c2ecf20Sopenharmony_ci	{"DMIC R1", NULL, "DMIC1"},
13458c2ecf20Sopenharmony_ci	{"DMIC L2", NULL, "DMIC2"},
13468c2ecf20Sopenharmony_ci	{"DMIC R2", NULL, "DMIC2"},
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	{"BST1", NULL, "IN1P"},
13498c2ecf20Sopenharmony_ci	{"BST1", NULL, "IN1N"},
13508c2ecf20Sopenharmony_ci	{"BST2", NULL, "IN2P"},
13518c2ecf20Sopenharmony_ci	{"BST2", NULL, "IN2N"},
13528c2ecf20Sopenharmony_ci	{"BST3", NULL, "IN3P"},
13538c2ecf20Sopenharmony_ci	{"BST3", NULL, "IN3N"},
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	{"INL VOL", NULL, "IN2P"},
13568c2ecf20Sopenharmony_ci	{"INR VOL", NULL, "IN2N"},
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci	{"RECMIXL", "HPOL Switch", "HPOL"},
13598c2ecf20Sopenharmony_ci	{"RECMIXL", "INL Switch", "INL VOL"},
13608c2ecf20Sopenharmony_ci	{"RECMIXL", "BST3 Switch", "BST3"},
13618c2ecf20Sopenharmony_ci	{"RECMIXL", "BST2 Switch", "BST2"},
13628c2ecf20Sopenharmony_ci	{"RECMIXL", "BST1 Switch", "BST1"},
13638c2ecf20Sopenharmony_ci	{"RECMIXL", "OUT MIXL Switch", "OUT MIXL"},
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	{"RECMIXR", "HPOR Switch", "HPOR"},
13668c2ecf20Sopenharmony_ci	{"RECMIXR", "INR Switch", "INR VOL"},
13678c2ecf20Sopenharmony_ci	{"RECMIXR", "BST3 Switch", "BST3"},
13688c2ecf20Sopenharmony_ci	{"RECMIXR", "BST2 Switch", "BST2"},
13698c2ecf20Sopenharmony_ci	{"RECMIXR", "BST1 Switch", "BST1"},
13708c2ecf20Sopenharmony_ci	{"RECMIXR", "OUT MIXR Switch", "OUT MIXR"},
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ci	{"ADC L", NULL, "RECMIXL"},
13738c2ecf20Sopenharmony_ci	{"ADC R", NULL, "RECMIXR"},
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	{"DMIC L1", NULL, "DMIC CLK"},
13768c2ecf20Sopenharmony_ci	{"DMIC L1", NULL, "DMIC1 Power"},
13778c2ecf20Sopenharmony_ci	{"DMIC R1", NULL, "DMIC CLK"},
13788c2ecf20Sopenharmony_ci	{"DMIC R1", NULL, "DMIC1 Power"},
13798c2ecf20Sopenharmony_ci	{"DMIC L2", NULL, "DMIC CLK"},
13808c2ecf20Sopenharmony_ci	{"DMIC L2", NULL, "DMIC2 Power"},
13818c2ecf20Sopenharmony_ci	{"DMIC R2", NULL, "DMIC CLK"},
13828c2ecf20Sopenharmony_ci	{"DMIC R2", NULL, "DMIC2 Power"},
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	{"Stereo ADC L2 Mux", "DMIC1", "DMIC L1"},
13858c2ecf20Sopenharmony_ci	{"Stereo ADC L2 Mux", "DMIC2", "DMIC L2"},
13868c2ecf20Sopenharmony_ci	{"Stereo ADC L2 Mux", "DIG MIX", "DIG MIXL"},
13878c2ecf20Sopenharmony_ci	{"Stereo ADC L1 Mux", "ADC", "ADC L"},
13888c2ecf20Sopenharmony_ci	{"Stereo ADC L1 Mux", "DIG MIX", "DIG MIXL"},
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci	{"Stereo ADC R1 Mux", "ADC", "ADC R"},
13918c2ecf20Sopenharmony_ci	{"Stereo ADC R1 Mux", "DIG MIX", "DIG MIXR"},
13928c2ecf20Sopenharmony_ci	{"Stereo ADC R2 Mux", "DMIC1", "DMIC R1"},
13938c2ecf20Sopenharmony_ci	{"Stereo ADC R2 Mux", "DMIC2", "DMIC R2"},
13948c2ecf20Sopenharmony_ci	{"Stereo ADC R2 Mux", "DIG MIX", "DIG MIXR"},
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci	{"Mono ADC L2 Mux", "DMIC L1", "DMIC L1"},
13978c2ecf20Sopenharmony_ci	{"Mono ADC L2 Mux", "DMIC L2", "DMIC L2"},
13988c2ecf20Sopenharmony_ci	{"Mono ADC L2 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
13998c2ecf20Sopenharmony_ci	{"Mono ADC L1 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
14008c2ecf20Sopenharmony_ci	{"Mono ADC L1 Mux", "ADCL", "ADC L"},
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci	{"Mono ADC R1 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
14038c2ecf20Sopenharmony_ci	{"Mono ADC R1 Mux", "ADCR", "ADC R"},
14048c2ecf20Sopenharmony_ci	{"Mono ADC R2 Mux", "DMIC R1", "DMIC R1"},
14058c2ecf20Sopenharmony_ci	{"Mono ADC R2 Mux", "DMIC R2", "DMIC R2"},
14068c2ecf20Sopenharmony_ci	{"Mono ADC R2 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_ci	{"Stereo ADC MIXL", "ADC1 Switch", "Stereo ADC L1 Mux"},
14098c2ecf20Sopenharmony_ci	{"Stereo ADC MIXL", "ADC2 Switch", "Stereo ADC L2 Mux"},
14108c2ecf20Sopenharmony_ci	{"Stereo ADC MIXL", NULL, "Stereo Filter"},
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ci	{"Stereo ADC MIXR", "ADC1 Switch", "Stereo ADC R1 Mux"},
14138c2ecf20Sopenharmony_ci	{"Stereo ADC MIXR", "ADC2 Switch", "Stereo ADC R2 Mux"},
14148c2ecf20Sopenharmony_ci	{"Stereo ADC MIXR", NULL, "Stereo Filter"},
14158c2ecf20Sopenharmony_ci
14168c2ecf20Sopenharmony_ci	{"Mono ADC MIXL", "ADC1 Switch", "Mono ADC L1 Mux"},
14178c2ecf20Sopenharmony_ci	{"Mono ADC MIXL", "ADC2 Switch", "Mono ADC L2 Mux"},
14188c2ecf20Sopenharmony_ci	{"Mono ADC MIXL", NULL, "Mono Left Filter"},
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	{"Mono ADC MIXR", "ADC1 Switch", "Mono ADC R1 Mux"},
14218c2ecf20Sopenharmony_ci	{"Mono ADC MIXR", "ADC2 Switch", "Mono ADC R2 Mux"},
14228c2ecf20Sopenharmony_ci	{"Mono ADC MIXR", NULL, "Mono Right Filter"},
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_ci	{"IF2 ADC L", NULL, "Mono ADC MIXL"},
14258c2ecf20Sopenharmony_ci	{"IF2 ADC R", NULL, "Mono ADC MIXR"},
14268c2ecf20Sopenharmony_ci	{"IF1 ADC L", NULL, "Stereo ADC MIXL"},
14278c2ecf20Sopenharmony_ci	{"IF1 ADC R", NULL, "Stereo ADC MIXR"},
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci	{"IF1 ADC", NULL, "I2S1"},
14308c2ecf20Sopenharmony_ci	{"IF1 ADC", NULL, "IF1 ADC L"},
14318c2ecf20Sopenharmony_ci	{"IF1 ADC", NULL, "IF1 ADC R"},
14328c2ecf20Sopenharmony_ci	{"IF2 ADC", NULL, "I2S2"},
14338c2ecf20Sopenharmony_ci	{"IF2 ADC", NULL, "IF2 ADC L"},
14348c2ecf20Sopenharmony_ci	{"IF2 ADC", NULL, "IF2 ADC R"},
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci	{"DAI1 TX Mux", "1:1|2:2", "IF1 ADC"},
14378c2ecf20Sopenharmony_ci	{"DAI1 TX Mux", "1:2|2:1", "IF2 ADC"},
14388c2ecf20Sopenharmony_ci	{"DAI1 IF1 Mux", "1:1|2:1", "IF1 ADC"},
14398c2ecf20Sopenharmony_ci	{"DAI1 IF2 Mux", "1:1|2:1", "IF2 ADC"},
14408c2ecf20Sopenharmony_ci	{"SDI1 TX Mux", "IF1", "DAI1 IF1 Mux"},
14418c2ecf20Sopenharmony_ci	{"SDI1 TX Mux", "IF2", "DAI1 IF2 Mux"},
14428c2ecf20Sopenharmony_ci
14438c2ecf20Sopenharmony_ci	{"DAI2 TX Mux", "1:2|2:1", "IF1 ADC"},
14448c2ecf20Sopenharmony_ci	{"DAI2 TX Mux", "1:1|2:2", "IF2 ADC"},
14458c2ecf20Sopenharmony_ci	{"DAI2 IF1 Mux", "1:2|2:2", "IF1 ADC"},
14468c2ecf20Sopenharmony_ci	{"DAI2 IF2 Mux", "1:2|2:2", "IF2 ADC"},
14478c2ecf20Sopenharmony_ci	{"SDI2 TX Mux", "IF1", "DAI2 IF1 Mux"},
14488c2ecf20Sopenharmony_ci	{"SDI2 TX Mux", "IF2", "DAI2 IF2 Mux"},
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	{"AIF1TX", NULL, "DAI1 TX Mux"},
14518c2ecf20Sopenharmony_ci	{"AIF1TX", NULL, "SDI1 TX Mux"},
14528c2ecf20Sopenharmony_ci	{"AIF2TX", NULL, "DAI2 TX Mux"},
14538c2ecf20Sopenharmony_ci	{"AIF2TX", NULL, "SDI2 TX Mux"},
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ci	{"DAI1 RX Mux", "1:1|2:2", "AIF1RX"},
14568c2ecf20Sopenharmony_ci	{"DAI1 RX Mux", "1:1|2:1", "AIF1RX"},
14578c2ecf20Sopenharmony_ci	{"DAI1 RX Mux", "1:2|2:1", "AIF2RX"},
14588c2ecf20Sopenharmony_ci	{"DAI1 RX Mux", "1:2|2:2", "AIF2RX"},
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_ci	{"DAI2 RX Mux", "1:2|2:1", "AIF1RX"},
14618c2ecf20Sopenharmony_ci	{"DAI2 RX Mux", "1:1|2:1", "AIF1RX"},
14628c2ecf20Sopenharmony_ci	{"DAI2 RX Mux", "1:1|2:2", "AIF2RX"},
14638c2ecf20Sopenharmony_ci	{"DAI2 RX Mux", "1:2|2:2", "AIF2RX"},
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	{"IF1 DAC", NULL, "I2S1"},
14668c2ecf20Sopenharmony_ci	{"IF1 DAC", NULL, "DAI1 RX Mux"},
14678c2ecf20Sopenharmony_ci	{"IF2 DAC", NULL, "I2S2"},
14688c2ecf20Sopenharmony_ci	{"IF2 DAC", NULL, "DAI2 RX Mux"},
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	{"IF1 DAC L", NULL, "IF1 DAC"},
14718c2ecf20Sopenharmony_ci	{"IF1 DAC R", NULL, "IF1 DAC"},
14728c2ecf20Sopenharmony_ci	{"IF2 DAC L", NULL, "IF2 DAC"},
14738c2ecf20Sopenharmony_ci	{"IF2 DAC R", NULL, "IF2 DAC"},
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci	{"DAC MIXL", "Stereo ADC Switch", "Stereo ADC MIXL"},
14768c2ecf20Sopenharmony_ci	{"DAC MIXL", "INF1 Switch", "IF1 DAC L"},
14778c2ecf20Sopenharmony_ci	{"DAC MIXL", NULL, "DAC L1 Power"},
14788c2ecf20Sopenharmony_ci	{"DAC MIXR", "Stereo ADC Switch", "Stereo ADC MIXR"},
14798c2ecf20Sopenharmony_ci	{"DAC MIXR", "INF1 Switch", "IF1 DAC R"},
14808c2ecf20Sopenharmony_ci	{"DAC MIXR", NULL, "DAC R1 Power"},
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci	{"Stereo DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
14838c2ecf20Sopenharmony_ci	{"Stereo DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	{"Mono DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
14868c2ecf20Sopenharmony_ci	{"Mono DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci	{"DIG MIXL", "DAC L1 Switch", "DAC MIXL"},
14898c2ecf20Sopenharmony_ci	{"DIG MIXR", "DAC R1 Switch", "DAC MIXR"},
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci	{"DAC L1", NULL, "Stereo DAC MIXL"},
14928c2ecf20Sopenharmony_ci	{"DAC L1", NULL, "DAC L1 Power"},
14938c2ecf20Sopenharmony_ci	{"DAC R1", NULL, "Stereo DAC MIXR"},
14948c2ecf20Sopenharmony_ci	{"DAC R1", NULL, "DAC R1 Power"},
14958c2ecf20Sopenharmony_ci
14968c2ecf20Sopenharmony_ci	{"SPK MIXL", "REC MIXL Switch", "RECMIXL"},
14978c2ecf20Sopenharmony_ci	{"SPK MIXL", "INL Switch", "INL VOL"},
14988c2ecf20Sopenharmony_ci	{"SPK MIXL", "DAC L1 Switch", "DAC L1"},
14998c2ecf20Sopenharmony_ci	{"SPK MIXL", "OUT MIXL Switch", "OUT MIXL"},
15008c2ecf20Sopenharmony_ci	{"SPK MIXR", "REC MIXR Switch", "RECMIXR"},
15018c2ecf20Sopenharmony_ci	{"SPK MIXR", "INR Switch", "INR VOL"},
15028c2ecf20Sopenharmony_ci	{"SPK MIXR", "DAC R1 Switch", "DAC R1"},
15038c2ecf20Sopenharmony_ci	{"SPK MIXR", "OUT MIXR Switch", "OUT MIXR"},
15048c2ecf20Sopenharmony_ci
15058c2ecf20Sopenharmony_ci	{"OUT MIXL", "BST1 Switch", "BST1"},
15068c2ecf20Sopenharmony_ci	{"OUT MIXL", "INL Switch", "INL VOL"},
15078c2ecf20Sopenharmony_ci	{"OUT MIXL", "REC MIXL Switch", "RECMIXL"},
15088c2ecf20Sopenharmony_ci	{"OUT MIXL", "DAC L1 Switch", "DAC L1"},
15098c2ecf20Sopenharmony_ci
15108c2ecf20Sopenharmony_ci	{"OUT MIXR", "BST2 Switch", "BST2"},
15118c2ecf20Sopenharmony_ci	{"OUT MIXR", "BST1 Switch", "BST1"},
15128c2ecf20Sopenharmony_ci	{"OUT MIXR", "INR Switch", "INR VOL"},
15138c2ecf20Sopenharmony_ci	{"OUT MIXR", "REC MIXR Switch", "RECMIXR"},
15148c2ecf20Sopenharmony_ci	{"OUT MIXR", "DAC R1 Switch", "DAC R1"},
15158c2ecf20Sopenharmony_ci
15168c2ecf20Sopenharmony_ci	{"SPKVOL L", NULL, "SPK MIXL"},
15178c2ecf20Sopenharmony_ci	{"SPKVOL R", NULL, "SPK MIXR"},
15188c2ecf20Sopenharmony_ci	{"HPOVOL L", NULL, "OUT MIXL"},
15198c2ecf20Sopenharmony_ci	{"HPOVOL R", NULL, "OUT MIXR"},
15208c2ecf20Sopenharmony_ci	{"OUTVOL L", NULL, "OUT MIXL"},
15218c2ecf20Sopenharmony_ci	{"OUTVOL R", NULL, "OUT MIXR"},
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci	{"SPOL MIX", "DAC R1 Switch", "DAC R1"},
15248c2ecf20Sopenharmony_ci	{"SPOL MIX", "DAC L1 Switch", "DAC L1"},
15258c2ecf20Sopenharmony_ci	{"SPOL MIX", "SPKVOL R Switch", "SPKVOL R"},
15268c2ecf20Sopenharmony_ci	{"SPOL MIX", "SPKVOL L Switch", "SPKVOL L"},
15278c2ecf20Sopenharmony_ci	{"SPOL MIX", "BST1 Switch", "BST1"},
15288c2ecf20Sopenharmony_ci	{"SPOR MIX", "DAC R1 Switch", "DAC R1"},
15298c2ecf20Sopenharmony_ci	{"SPOR MIX", "SPKVOL R Switch", "SPKVOL R"},
15308c2ecf20Sopenharmony_ci	{"SPOR MIX", "BST1 Switch", "BST1"},
15318c2ecf20Sopenharmony_ci
15328c2ecf20Sopenharmony_ci	{"HPO MIX L", "HPO MIX DAC1 Switch", "DAC L1"},
15338c2ecf20Sopenharmony_ci	{"HPO MIX L", "HPO MIX HPVOL Switch", "HPOVOL L"},
15348c2ecf20Sopenharmony_ci	{"HPO MIX L", NULL, "HP L Amp"},
15358c2ecf20Sopenharmony_ci	{"HPO MIX R", "HPO MIX DAC1 Switch", "DAC R1"},
15368c2ecf20Sopenharmony_ci	{"HPO MIX R", "HPO MIX HPVOL Switch", "HPOVOL R"},
15378c2ecf20Sopenharmony_ci	{"HPO MIX R", NULL, "HP R Amp"},
15388c2ecf20Sopenharmony_ci
15398c2ecf20Sopenharmony_ci	{"LOUT MIX", "DAC L1 Switch", "DAC L1"},
15408c2ecf20Sopenharmony_ci	{"LOUT MIX", "DAC R1 Switch", "DAC R1"},
15418c2ecf20Sopenharmony_ci	{"LOUT MIX", "OUTVOL L Switch", "OUTVOL L"},
15428c2ecf20Sopenharmony_ci	{"LOUT MIX", "OUTVOL R Switch", "OUTVOL R"},
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci	{"HP Amp", NULL, "HPO MIX L"},
15458c2ecf20Sopenharmony_ci	{"HP Amp", NULL, "HPO MIX R"},
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_ci	{"Speaker L Playback", "Switch", "SPOL MIX"},
15488c2ecf20Sopenharmony_ci	{"Speaker R Playback", "Switch", "SPOR MIX"},
15498c2ecf20Sopenharmony_ci	{"SPOLP", NULL, "Speaker L Playback"},
15508c2ecf20Sopenharmony_ci	{"SPOLN", NULL, "Speaker L Playback"},
15518c2ecf20Sopenharmony_ci	{"SPORP", NULL, "Speaker R Playback"},
15528c2ecf20Sopenharmony_ci	{"SPORN", NULL, "Speaker R Playback"},
15538c2ecf20Sopenharmony_ci
15548c2ecf20Sopenharmony_ci	{"SPOLP", NULL, "Improve SPK Amp Drv"},
15558c2ecf20Sopenharmony_ci	{"SPOLN", NULL, "Improve SPK Amp Drv"},
15568c2ecf20Sopenharmony_ci	{"SPORP", NULL, "Improve SPK Amp Drv"},
15578c2ecf20Sopenharmony_ci	{"SPORN", NULL, "Improve SPK Amp Drv"},
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	{"HPOL", NULL, "Improve HP Amp Drv"},
15608c2ecf20Sopenharmony_ci	{"HPOR", NULL, "Improve HP Amp Drv"},
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_ci	{"HP L Playback", "Switch", "HP Amp"},
15638c2ecf20Sopenharmony_ci	{"HP R Playback", "Switch", "HP Amp"},
15648c2ecf20Sopenharmony_ci	{"HPOL", NULL, "HP L Playback"},
15658c2ecf20Sopenharmony_ci	{"HPOR", NULL, "HP R Playback"},
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci	{"LOUT amp", NULL, "LOUT MIX"},
15688c2ecf20Sopenharmony_ci	{"LOUTL", NULL, "LOUT amp"},
15698c2ecf20Sopenharmony_ci	{"LOUTR", NULL, "LOUT amp"},
15708c2ecf20Sopenharmony_ci};
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route rt5640_specific_dapm_routes[] = {
15738c2ecf20Sopenharmony_ci	{"ANC", NULL, "Stereo ADC MIXL"},
15748c2ecf20Sopenharmony_ci	{"ANC", NULL, "Stereo ADC MIXR"},
15758c2ecf20Sopenharmony_ci
15768c2ecf20Sopenharmony_ci	{"Audio DSP", NULL, "DAC MIXL"},
15778c2ecf20Sopenharmony_ci	{"Audio DSP", NULL, "DAC MIXR"},
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_ci	{"DAC L2 Mux", "IF2", "IF2 DAC L"},
15808c2ecf20Sopenharmony_ci	{"DAC L2 Mux", "Base L/R", "Audio DSP"},
15818c2ecf20Sopenharmony_ci	{"DAC L2 Mux", NULL, "DAC L2 Power"},
15828c2ecf20Sopenharmony_ci	{"DAC R2 Mux", "IF2", "IF2 DAC R"},
15838c2ecf20Sopenharmony_ci	{"DAC R2 Mux", NULL, "DAC R2 Power"},
15848c2ecf20Sopenharmony_ci
15858c2ecf20Sopenharmony_ci	{"Stereo DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
15868c2ecf20Sopenharmony_ci	{"Stereo DAC MIXL", "ANC Switch", "ANC"},
15878c2ecf20Sopenharmony_ci	{"Stereo DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
15888c2ecf20Sopenharmony_ci	{"Stereo DAC MIXR", "ANC Switch", "ANC"},
15898c2ecf20Sopenharmony_ci
15908c2ecf20Sopenharmony_ci	{"Mono DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
15918c2ecf20Sopenharmony_ci	{"Mono DAC MIXL", "DAC R2 Switch", "DAC R2 Mux"},
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci	{"Mono DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
15948c2ecf20Sopenharmony_ci	{"Mono DAC MIXR", "DAC L2 Switch", "DAC L2 Mux"},
15958c2ecf20Sopenharmony_ci
15968c2ecf20Sopenharmony_ci	{"DIG MIXR", "DAC R2 Switch", "DAC R2 Mux"},
15978c2ecf20Sopenharmony_ci	{"DIG MIXL", "DAC L2 Switch", "DAC L2 Mux"},
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci	{"DAC L2", NULL, "Mono DAC MIXL"},
16008c2ecf20Sopenharmony_ci	{"DAC L2", NULL, "DAC L2 Power"},
16018c2ecf20Sopenharmony_ci	{"DAC R2", NULL, "Mono DAC MIXR"},
16028c2ecf20Sopenharmony_ci	{"DAC R2", NULL, "DAC R2 Power"},
16038c2ecf20Sopenharmony_ci
16048c2ecf20Sopenharmony_ci	{"SPK MIXL", "DAC L2 Switch", "DAC L2"},
16058c2ecf20Sopenharmony_ci	{"SPK MIXR", "DAC R2 Switch", "DAC R2"},
16068c2ecf20Sopenharmony_ci
16078c2ecf20Sopenharmony_ci	{"OUT MIXL", "SPK MIXL Switch", "SPK MIXL"},
16088c2ecf20Sopenharmony_ci	{"OUT MIXR", "SPK MIXR Switch", "SPK MIXR"},
16098c2ecf20Sopenharmony_ci
16108c2ecf20Sopenharmony_ci	{"OUT MIXL", "DAC R2 Switch", "DAC R2"},
16118c2ecf20Sopenharmony_ci	{"OUT MIXL", "DAC L2 Switch", "DAC L2"},
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_ci	{"OUT MIXR", "DAC L2 Switch", "DAC L2"},
16148c2ecf20Sopenharmony_ci	{"OUT MIXR", "DAC R2 Switch", "DAC R2"},
16158c2ecf20Sopenharmony_ci
16168c2ecf20Sopenharmony_ci	{"HPO MIX L", "HPO MIX DAC2 Switch", "DAC L2"},
16178c2ecf20Sopenharmony_ci	{"HPO MIX R", "HPO MIX DAC2 Switch", "DAC R2"},
16188c2ecf20Sopenharmony_ci
16198c2ecf20Sopenharmony_ci	{"Mono MIX", "DAC R2 Switch", "DAC R2"},
16208c2ecf20Sopenharmony_ci	{"Mono MIX", "DAC L2 Switch", "DAC L2"},
16218c2ecf20Sopenharmony_ci	{"Mono MIX", "OUTVOL R Switch", "OUTVOL R"},
16228c2ecf20Sopenharmony_ci	{"Mono MIX", "OUTVOL L Switch", "OUTVOL L"},
16238c2ecf20Sopenharmony_ci	{"Mono MIX", "BST1 Switch", "BST1"},
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	{"MONOP", NULL, "Mono MIX"},
16268c2ecf20Sopenharmony_ci	{"MONON", NULL, "Mono MIX"},
16278c2ecf20Sopenharmony_ci	{"MONOP", NULL, "Improve MONO Amp Drv"},
16288c2ecf20Sopenharmony_ci};
16298c2ecf20Sopenharmony_ci
16308c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route rt5639_specific_dapm_routes[] = {
16318c2ecf20Sopenharmony_ci	{"Stereo DAC MIXL", "DAC L2 Switch", "IF2 DAC L"},
16328c2ecf20Sopenharmony_ci	{"Stereo DAC MIXR", "DAC R2 Switch", "IF2 DAC R"},
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci	{"Mono DAC MIXL", "DAC L2 Switch", "IF2 DAC L"},
16358c2ecf20Sopenharmony_ci	{"Mono DAC MIXL", "DAC R2 Switch", "IF2 DAC R"},
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_ci	{"Mono DAC MIXR", "DAC R2 Switch", "IF2 DAC R"},
16388c2ecf20Sopenharmony_ci	{"Mono DAC MIXR", "DAC L2 Switch", "IF2 DAC L"},
16398c2ecf20Sopenharmony_ci
16408c2ecf20Sopenharmony_ci	{"DIG MIXL", "DAC L2 Switch", "IF2 DAC L"},
16418c2ecf20Sopenharmony_ci	{"DIG MIXR", "DAC R2 Switch", "IF2 DAC R"},
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci	{"IF2 DAC L", NULL, "DAC L2 Power"},
16448c2ecf20Sopenharmony_ci	{"IF2 DAC R", NULL, "DAC R2 Power"},
16458c2ecf20Sopenharmony_ci};
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_cistatic int get_sdp_info(struct snd_soc_component *component, int dai_id)
16488c2ecf20Sopenharmony_ci{
16498c2ecf20Sopenharmony_ci	int ret = 0, val;
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	if (component == NULL)
16528c2ecf20Sopenharmony_ci		return -EINVAL;
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_ci	val = snd_soc_component_read(component, RT5640_I2S1_SDP);
16558c2ecf20Sopenharmony_ci	val = (val & RT5640_I2S_IF_MASK) >> RT5640_I2S_IF_SFT;
16568c2ecf20Sopenharmony_ci	switch (dai_id) {
16578c2ecf20Sopenharmony_ci	case RT5640_AIF1:
16588c2ecf20Sopenharmony_ci		switch (val) {
16598c2ecf20Sopenharmony_ci		case RT5640_IF_123:
16608c2ecf20Sopenharmony_ci		case RT5640_IF_132:
16618c2ecf20Sopenharmony_ci			ret |= RT5640_U_IF1;
16628c2ecf20Sopenharmony_ci			break;
16638c2ecf20Sopenharmony_ci		case RT5640_IF_113:
16648c2ecf20Sopenharmony_ci			ret |= RT5640_U_IF1;
16658c2ecf20Sopenharmony_ci			fallthrough;
16668c2ecf20Sopenharmony_ci		case RT5640_IF_312:
16678c2ecf20Sopenharmony_ci		case RT5640_IF_213:
16688c2ecf20Sopenharmony_ci			ret |= RT5640_U_IF2;
16698c2ecf20Sopenharmony_ci			break;
16708c2ecf20Sopenharmony_ci		}
16718c2ecf20Sopenharmony_ci		break;
16728c2ecf20Sopenharmony_ci
16738c2ecf20Sopenharmony_ci	case RT5640_AIF2:
16748c2ecf20Sopenharmony_ci		switch (val) {
16758c2ecf20Sopenharmony_ci		case RT5640_IF_231:
16768c2ecf20Sopenharmony_ci		case RT5640_IF_213:
16778c2ecf20Sopenharmony_ci			ret |= RT5640_U_IF1;
16788c2ecf20Sopenharmony_ci			break;
16798c2ecf20Sopenharmony_ci		case RT5640_IF_223:
16808c2ecf20Sopenharmony_ci			ret |= RT5640_U_IF1;
16818c2ecf20Sopenharmony_ci			fallthrough;
16828c2ecf20Sopenharmony_ci		case RT5640_IF_123:
16838c2ecf20Sopenharmony_ci		case RT5640_IF_321:
16848c2ecf20Sopenharmony_ci			ret |= RT5640_U_IF2;
16858c2ecf20Sopenharmony_ci			break;
16868c2ecf20Sopenharmony_ci		}
16878c2ecf20Sopenharmony_ci		break;
16888c2ecf20Sopenharmony_ci
16898c2ecf20Sopenharmony_ci	default:
16908c2ecf20Sopenharmony_ci		ret = -EINVAL;
16918c2ecf20Sopenharmony_ci		break;
16928c2ecf20Sopenharmony_ci	}
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci	return ret;
16958c2ecf20Sopenharmony_ci}
16968c2ecf20Sopenharmony_ci
16978c2ecf20Sopenharmony_cistatic int rt5640_hw_params(struct snd_pcm_substream *substream,
16988c2ecf20Sopenharmony_ci	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
16998c2ecf20Sopenharmony_ci{
17008c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
17018c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
17028c2ecf20Sopenharmony_ci	unsigned int val_len = 0, val_clk, mask_clk;
17038c2ecf20Sopenharmony_ci	int dai_sel, pre_div, bclk_ms, frame_size;
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_ci	rt5640->lrck[dai->id] = params_rate(params);
17068c2ecf20Sopenharmony_ci	pre_div = rl6231_get_clk_info(rt5640->sysclk, rt5640->lrck[dai->id]);
17078c2ecf20Sopenharmony_ci	if (pre_div < 0) {
17088c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupported clock setting %d for DAI %d\n",
17098c2ecf20Sopenharmony_ci			rt5640->lrck[dai->id], dai->id);
17108c2ecf20Sopenharmony_ci		return -EINVAL;
17118c2ecf20Sopenharmony_ci	}
17128c2ecf20Sopenharmony_ci	frame_size = snd_soc_params_to_frame_size(params);
17138c2ecf20Sopenharmony_ci	if (frame_size < 0) {
17148c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupported frame size: %d\n", frame_size);
17158c2ecf20Sopenharmony_ci		return frame_size;
17168c2ecf20Sopenharmony_ci	}
17178c2ecf20Sopenharmony_ci	if (frame_size > 32)
17188c2ecf20Sopenharmony_ci		bclk_ms = 1;
17198c2ecf20Sopenharmony_ci	else
17208c2ecf20Sopenharmony_ci		bclk_ms = 0;
17218c2ecf20Sopenharmony_ci	rt5640->bclk[dai->id] = rt5640->lrck[dai->id] * (32 << bclk_ms);
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci	dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
17248c2ecf20Sopenharmony_ci		rt5640->bclk[dai->id], rt5640->lrck[dai->id]);
17258c2ecf20Sopenharmony_ci	dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
17268c2ecf20Sopenharmony_ci				bclk_ms, pre_div, dai->id);
17278c2ecf20Sopenharmony_ci
17288c2ecf20Sopenharmony_ci	switch (params_width(params)) {
17298c2ecf20Sopenharmony_ci	case 16:
17308c2ecf20Sopenharmony_ci		break;
17318c2ecf20Sopenharmony_ci	case 20:
17328c2ecf20Sopenharmony_ci		val_len |= RT5640_I2S_DL_20;
17338c2ecf20Sopenharmony_ci		break;
17348c2ecf20Sopenharmony_ci	case 24:
17358c2ecf20Sopenharmony_ci		val_len |= RT5640_I2S_DL_24;
17368c2ecf20Sopenharmony_ci		break;
17378c2ecf20Sopenharmony_ci	case 8:
17388c2ecf20Sopenharmony_ci		val_len |= RT5640_I2S_DL_8;
17398c2ecf20Sopenharmony_ci		break;
17408c2ecf20Sopenharmony_ci	default:
17418c2ecf20Sopenharmony_ci		return -EINVAL;
17428c2ecf20Sopenharmony_ci	}
17438c2ecf20Sopenharmony_ci
17448c2ecf20Sopenharmony_ci	dai_sel = get_sdp_info(component, dai->id);
17458c2ecf20Sopenharmony_ci	if (dai_sel < 0) {
17468c2ecf20Sopenharmony_ci		dev_err(component->dev, "Failed to get sdp info: %d\n", dai_sel);
17478c2ecf20Sopenharmony_ci		return -EINVAL;
17488c2ecf20Sopenharmony_ci	}
17498c2ecf20Sopenharmony_ci	if (dai_sel & RT5640_U_IF1) {
17508c2ecf20Sopenharmony_ci		mask_clk = RT5640_I2S_BCLK_MS1_MASK | RT5640_I2S_PD1_MASK;
17518c2ecf20Sopenharmony_ci		val_clk = bclk_ms << RT5640_I2S_BCLK_MS1_SFT |
17528c2ecf20Sopenharmony_ci			pre_div << RT5640_I2S_PD1_SFT;
17538c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_I2S1_SDP,
17548c2ecf20Sopenharmony_ci			RT5640_I2S_DL_MASK, val_len);
17558c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_ADDA_CLK1, mask_clk, val_clk);
17568c2ecf20Sopenharmony_ci	}
17578c2ecf20Sopenharmony_ci	if (dai_sel & RT5640_U_IF2) {
17588c2ecf20Sopenharmony_ci		mask_clk = RT5640_I2S_BCLK_MS2_MASK | RT5640_I2S_PD2_MASK;
17598c2ecf20Sopenharmony_ci		val_clk = bclk_ms << RT5640_I2S_BCLK_MS2_SFT |
17608c2ecf20Sopenharmony_ci			pre_div << RT5640_I2S_PD2_SFT;
17618c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_I2S2_SDP,
17628c2ecf20Sopenharmony_ci			RT5640_I2S_DL_MASK, val_len);
17638c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_ADDA_CLK1, mask_clk, val_clk);
17648c2ecf20Sopenharmony_ci	}
17658c2ecf20Sopenharmony_ci
17668c2ecf20Sopenharmony_ci	return 0;
17678c2ecf20Sopenharmony_ci}
17688c2ecf20Sopenharmony_ci
17698c2ecf20Sopenharmony_cistatic int rt5640_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
17708c2ecf20Sopenharmony_ci{
17718c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
17728c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
17738c2ecf20Sopenharmony_ci	unsigned int reg_val = 0;
17748c2ecf20Sopenharmony_ci	int dai_sel;
17758c2ecf20Sopenharmony_ci
17768c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
17778c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM:
17788c2ecf20Sopenharmony_ci		rt5640->master[dai->id] = 1;
17798c2ecf20Sopenharmony_ci		break;
17808c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:
17818c2ecf20Sopenharmony_ci		reg_val |= RT5640_I2S_MS_S;
17828c2ecf20Sopenharmony_ci		rt5640->master[dai->id] = 0;
17838c2ecf20Sopenharmony_ci		break;
17848c2ecf20Sopenharmony_ci	default:
17858c2ecf20Sopenharmony_ci		return -EINVAL;
17868c2ecf20Sopenharmony_ci	}
17878c2ecf20Sopenharmony_ci
17888c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
17898c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_NF:
17908c2ecf20Sopenharmony_ci		break;
17918c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_NF:
17928c2ecf20Sopenharmony_ci		reg_val |= RT5640_I2S_BP_INV;
17938c2ecf20Sopenharmony_ci		break;
17948c2ecf20Sopenharmony_ci	default:
17958c2ecf20Sopenharmony_ci		return -EINVAL;
17968c2ecf20Sopenharmony_ci	}
17978c2ecf20Sopenharmony_ci
17988c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
17998c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
18008c2ecf20Sopenharmony_ci		break;
18018c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_LEFT_J:
18028c2ecf20Sopenharmony_ci		reg_val |= RT5640_I2S_DF_LEFT;
18038c2ecf20Sopenharmony_ci		break;
18048c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_A:
18058c2ecf20Sopenharmony_ci		reg_val |= RT5640_I2S_DF_PCM_A;
18068c2ecf20Sopenharmony_ci		break;
18078c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_B:
18088c2ecf20Sopenharmony_ci		reg_val  |= RT5640_I2S_DF_PCM_B;
18098c2ecf20Sopenharmony_ci		break;
18108c2ecf20Sopenharmony_ci	default:
18118c2ecf20Sopenharmony_ci		return -EINVAL;
18128c2ecf20Sopenharmony_ci	}
18138c2ecf20Sopenharmony_ci
18148c2ecf20Sopenharmony_ci	dai_sel = get_sdp_info(component, dai->id);
18158c2ecf20Sopenharmony_ci	if (dai_sel < 0) {
18168c2ecf20Sopenharmony_ci		dev_err(component->dev, "Failed to get sdp info: %d\n", dai_sel);
18178c2ecf20Sopenharmony_ci		return -EINVAL;
18188c2ecf20Sopenharmony_ci	}
18198c2ecf20Sopenharmony_ci	if (dai_sel & RT5640_U_IF1) {
18208c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_I2S1_SDP,
18218c2ecf20Sopenharmony_ci			RT5640_I2S_MS_MASK | RT5640_I2S_BP_MASK |
18228c2ecf20Sopenharmony_ci			RT5640_I2S_DF_MASK, reg_val);
18238c2ecf20Sopenharmony_ci	}
18248c2ecf20Sopenharmony_ci	if (dai_sel & RT5640_U_IF2) {
18258c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_I2S2_SDP,
18268c2ecf20Sopenharmony_ci			RT5640_I2S_MS_MASK | RT5640_I2S_BP_MASK |
18278c2ecf20Sopenharmony_ci			RT5640_I2S_DF_MASK, reg_val);
18288c2ecf20Sopenharmony_ci	}
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	return 0;
18318c2ecf20Sopenharmony_ci}
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_cistatic int rt5640_set_dai_sysclk(struct snd_soc_dai *dai,
18348c2ecf20Sopenharmony_ci		int clk_id, unsigned int freq, int dir)
18358c2ecf20Sopenharmony_ci{
18368c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
18378c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
18388c2ecf20Sopenharmony_ci	unsigned int reg_val = 0;
18398c2ecf20Sopenharmony_ci	unsigned int pll_bit = 0;
18408c2ecf20Sopenharmony_ci
18418c2ecf20Sopenharmony_ci	if (freq == rt5640->sysclk && clk_id == rt5640->sysclk_src)
18428c2ecf20Sopenharmony_ci		return 0;
18438c2ecf20Sopenharmony_ci
18448c2ecf20Sopenharmony_ci	switch (clk_id) {
18458c2ecf20Sopenharmony_ci	case RT5640_SCLK_S_MCLK:
18468c2ecf20Sopenharmony_ci		reg_val |= RT5640_SCLK_SRC_MCLK;
18478c2ecf20Sopenharmony_ci		break;
18488c2ecf20Sopenharmony_ci	case RT5640_SCLK_S_PLL1:
18498c2ecf20Sopenharmony_ci		reg_val |= RT5640_SCLK_SRC_PLL1;
18508c2ecf20Sopenharmony_ci		pll_bit |= RT5640_PWR_PLL;
18518c2ecf20Sopenharmony_ci		break;
18528c2ecf20Sopenharmony_ci	case RT5640_SCLK_S_RCCLK:
18538c2ecf20Sopenharmony_ci		reg_val |= RT5640_SCLK_SRC_RCCLK;
18548c2ecf20Sopenharmony_ci		break;
18558c2ecf20Sopenharmony_ci	default:
18568c2ecf20Sopenharmony_ci		dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
18578c2ecf20Sopenharmony_ci		return -EINVAL;
18588c2ecf20Sopenharmony_ci	}
18598c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_PWR_ANLG2,
18608c2ecf20Sopenharmony_ci		RT5640_PWR_PLL, pll_bit);
18618c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_GLB_CLK,
18628c2ecf20Sopenharmony_ci		RT5640_SCLK_SRC_MASK, reg_val);
18638c2ecf20Sopenharmony_ci	rt5640->sysclk = freq;
18648c2ecf20Sopenharmony_ci	rt5640->sysclk_src = clk_id;
18658c2ecf20Sopenharmony_ci
18668c2ecf20Sopenharmony_ci	dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
18678c2ecf20Sopenharmony_ci	return 0;
18688c2ecf20Sopenharmony_ci}
18698c2ecf20Sopenharmony_ci
18708c2ecf20Sopenharmony_cistatic int rt5640_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
18718c2ecf20Sopenharmony_ci			unsigned int freq_in, unsigned int freq_out)
18728c2ecf20Sopenharmony_ci{
18738c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
18748c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
18758c2ecf20Sopenharmony_ci	struct rl6231_pll_code pll_code;
18768c2ecf20Sopenharmony_ci	int ret;
18778c2ecf20Sopenharmony_ci
18788c2ecf20Sopenharmony_ci	if (source == rt5640->pll_src && freq_in == rt5640->pll_in &&
18798c2ecf20Sopenharmony_ci	    freq_out == rt5640->pll_out)
18808c2ecf20Sopenharmony_ci		return 0;
18818c2ecf20Sopenharmony_ci
18828c2ecf20Sopenharmony_ci	if (!freq_in || !freq_out) {
18838c2ecf20Sopenharmony_ci		dev_dbg(component->dev, "PLL disabled\n");
18848c2ecf20Sopenharmony_ci
18858c2ecf20Sopenharmony_ci		rt5640->pll_in = 0;
18868c2ecf20Sopenharmony_ci		rt5640->pll_out = 0;
18878c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
18888c2ecf20Sopenharmony_ci			RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_MCLK);
18898c2ecf20Sopenharmony_ci		return 0;
18908c2ecf20Sopenharmony_ci	}
18918c2ecf20Sopenharmony_ci
18928c2ecf20Sopenharmony_ci	switch (source) {
18938c2ecf20Sopenharmony_ci	case RT5640_PLL1_S_MCLK:
18948c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
18958c2ecf20Sopenharmony_ci			RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_MCLK);
18968c2ecf20Sopenharmony_ci		break;
18978c2ecf20Sopenharmony_ci	case RT5640_PLL1_S_BCLK1:
18988c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
18998c2ecf20Sopenharmony_ci			RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK1);
19008c2ecf20Sopenharmony_ci		break;
19018c2ecf20Sopenharmony_ci	case RT5640_PLL1_S_BCLK2:
19028c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
19038c2ecf20Sopenharmony_ci			RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK2);
19048c2ecf20Sopenharmony_ci		break;
19058c2ecf20Sopenharmony_ci	default:
19068c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unknown PLL source %d\n", source);
19078c2ecf20Sopenharmony_ci		return -EINVAL;
19088c2ecf20Sopenharmony_ci	}
19098c2ecf20Sopenharmony_ci
19108c2ecf20Sopenharmony_ci	ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
19118c2ecf20Sopenharmony_ci	if (ret < 0) {
19128c2ecf20Sopenharmony_ci		dev_err(component->dev, "Unsupport input clock %d\n", freq_in);
19138c2ecf20Sopenharmony_ci		return ret;
19148c2ecf20Sopenharmony_ci	}
19158c2ecf20Sopenharmony_ci
19168c2ecf20Sopenharmony_ci	dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
19178c2ecf20Sopenharmony_ci		pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
19188c2ecf20Sopenharmony_ci		pll_code.n_code, pll_code.k_code);
19198c2ecf20Sopenharmony_ci
19208c2ecf20Sopenharmony_ci	snd_soc_component_write(component, RT5640_PLL_CTRL1,
19218c2ecf20Sopenharmony_ci		pll_code.n_code << RT5640_PLL_N_SFT | pll_code.k_code);
19228c2ecf20Sopenharmony_ci	snd_soc_component_write(component, RT5640_PLL_CTRL2,
19238c2ecf20Sopenharmony_ci		(pll_code.m_bp ? 0 : pll_code.m_code) << RT5640_PLL_M_SFT |
19248c2ecf20Sopenharmony_ci		pll_code.m_bp << RT5640_PLL_M_BP_SFT);
19258c2ecf20Sopenharmony_ci
19268c2ecf20Sopenharmony_ci	rt5640->pll_in = freq_in;
19278c2ecf20Sopenharmony_ci	rt5640->pll_out = freq_out;
19288c2ecf20Sopenharmony_ci	rt5640->pll_src = source;
19298c2ecf20Sopenharmony_ci
19308c2ecf20Sopenharmony_ci	return 0;
19318c2ecf20Sopenharmony_ci}
19328c2ecf20Sopenharmony_ci
19338c2ecf20Sopenharmony_cistatic int rt5640_set_bias_level(struct snd_soc_component *component,
19348c2ecf20Sopenharmony_ci			enum snd_soc_bias_level level)
19358c2ecf20Sopenharmony_ci{
19368c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
19378c2ecf20Sopenharmony_ci	int ret;
19388c2ecf20Sopenharmony_ci
19398c2ecf20Sopenharmony_ci	switch (level) {
19408c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_ON:
19418c2ecf20Sopenharmony_ci		break;
19428c2ecf20Sopenharmony_ci
19438c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_PREPARE:
19448c2ecf20Sopenharmony_ci		/*
19458c2ecf20Sopenharmony_ci		 * SND_SOC_BIAS_PREPARE is called while preparing for a
19468c2ecf20Sopenharmony_ci		 * transition to ON or away from ON. If current bias_level
19478c2ecf20Sopenharmony_ci		 * is SND_SOC_BIAS_ON, then it is preparing for a transition
19488c2ecf20Sopenharmony_ci		 * away from ON. Disable the clock in that case, otherwise
19498c2ecf20Sopenharmony_ci		 * enable it.
19508c2ecf20Sopenharmony_ci		 */
19518c2ecf20Sopenharmony_ci		if (IS_ERR(rt5640->mclk))
19528c2ecf20Sopenharmony_ci			break;
19538c2ecf20Sopenharmony_ci
19548c2ecf20Sopenharmony_ci		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_ON) {
19558c2ecf20Sopenharmony_ci			clk_disable_unprepare(rt5640->mclk);
19568c2ecf20Sopenharmony_ci		} else {
19578c2ecf20Sopenharmony_ci			ret = clk_prepare_enable(rt5640->mclk);
19588c2ecf20Sopenharmony_ci			if (ret)
19598c2ecf20Sopenharmony_ci				return ret;
19608c2ecf20Sopenharmony_ci		}
19618c2ecf20Sopenharmony_ci		break;
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_STANDBY:
19648c2ecf20Sopenharmony_ci		if (SND_SOC_BIAS_OFF == snd_soc_component_get_bias_level(component)) {
19658c2ecf20Sopenharmony_ci			snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
19668c2ecf20Sopenharmony_ci				RT5640_PWR_VREF1 | RT5640_PWR_MB |
19678c2ecf20Sopenharmony_ci				RT5640_PWR_BG | RT5640_PWR_VREF2,
19688c2ecf20Sopenharmony_ci				RT5640_PWR_VREF1 | RT5640_PWR_MB |
19698c2ecf20Sopenharmony_ci				RT5640_PWR_BG | RT5640_PWR_VREF2);
19708c2ecf20Sopenharmony_ci			usleep_range(10000, 15000);
19718c2ecf20Sopenharmony_ci			snd_soc_component_update_bits(component, RT5640_PWR_ANLG1,
19728c2ecf20Sopenharmony_ci				RT5640_PWR_FV1 | RT5640_PWR_FV2,
19738c2ecf20Sopenharmony_ci				RT5640_PWR_FV1 | RT5640_PWR_FV2);
19748c2ecf20Sopenharmony_ci			snd_soc_component_update_bits(component, RT5640_DUMMY1,
19758c2ecf20Sopenharmony_ci						0x0301, 0x0301);
19768c2ecf20Sopenharmony_ci			snd_soc_component_update_bits(component, RT5640_MICBIAS,
19778c2ecf20Sopenharmony_ci						0x0030, 0x0030);
19788c2ecf20Sopenharmony_ci		}
19798c2ecf20Sopenharmony_ci		break;
19808c2ecf20Sopenharmony_ci
19818c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_OFF:
19828c2ecf20Sopenharmony_ci		snd_soc_component_write(component, RT5640_DEPOP_M1, 0x0004);
19838c2ecf20Sopenharmony_ci		snd_soc_component_write(component, RT5640_DEPOP_M2, 0x1100);
19848c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_DUMMY1, 0x1, 0);
19858c2ecf20Sopenharmony_ci		snd_soc_component_write(component, RT5640_PWR_DIG1, 0x0000);
19868c2ecf20Sopenharmony_ci		snd_soc_component_write(component, RT5640_PWR_DIG2, 0x0000);
19878c2ecf20Sopenharmony_ci		snd_soc_component_write(component, RT5640_PWR_VOL, 0x0000);
19888c2ecf20Sopenharmony_ci		snd_soc_component_write(component, RT5640_PWR_MIXER, 0x0000);
19898c2ecf20Sopenharmony_ci		snd_soc_component_write(component, RT5640_PWR_ANLG1, 0x0000);
19908c2ecf20Sopenharmony_ci		snd_soc_component_write(component, RT5640_PWR_ANLG2, 0x0000);
19918c2ecf20Sopenharmony_ci		break;
19928c2ecf20Sopenharmony_ci
19938c2ecf20Sopenharmony_ci	default:
19948c2ecf20Sopenharmony_ci		break;
19958c2ecf20Sopenharmony_ci	}
19968c2ecf20Sopenharmony_ci
19978c2ecf20Sopenharmony_ci	return 0;
19988c2ecf20Sopenharmony_ci}
19998c2ecf20Sopenharmony_ci
20008c2ecf20Sopenharmony_ciint rt5640_dmic_enable(struct snd_soc_component *component,
20018c2ecf20Sopenharmony_ci		       bool dmic1_data_pin, bool dmic2_data_pin)
20028c2ecf20Sopenharmony_ci{
20038c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
20048c2ecf20Sopenharmony_ci
20058c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
20068c2ecf20Sopenharmony_ci		RT5640_GP2_PIN_MASK, RT5640_GP2_PIN_DMIC1_SCL);
20078c2ecf20Sopenharmony_ci
20088c2ecf20Sopenharmony_ci	if (dmic1_data_pin) {
20098c2ecf20Sopenharmony_ci		regmap_update_bits(rt5640->regmap, RT5640_DMIC,
20108c2ecf20Sopenharmony_ci			RT5640_DMIC_1_DP_MASK, RT5640_DMIC_1_DP_GPIO3);
20118c2ecf20Sopenharmony_ci		regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
20128c2ecf20Sopenharmony_ci			RT5640_GP3_PIN_MASK, RT5640_GP3_PIN_DMIC1_SDA);
20138c2ecf20Sopenharmony_ci	}
20148c2ecf20Sopenharmony_ci
20158c2ecf20Sopenharmony_ci	if (dmic2_data_pin) {
20168c2ecf20Sopenharmony_ci		regmap_update_bits(rt5640->regmap, RT5640_DMIC,
20178c2ecf20Sopenharmony_ci			RT5640_DMIC_2_DP_MASK, RT5640_DMIC_2_DP_GPIO4);
20188c2ecf20Sopenharmony_ci		regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1,
20198c2ecf20Sopenharmony_ci			RT5640_GP4_PIN_MASK, RT5640_GP4_PIN_DMIC2_SDA);
20208c2ecf20Sopenharmony_ci	}
20218c2ecf20Sopenharmony_ci
20228c2ecf20Sopenharmony_ci	return 0;
20238c2ecf20Sopenharmony_ci}
20248c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt5640_dmic_enable);
20258c2ecf20Sopenharmony_ci
20268c2ecf20Sopenharmony_ciint rt5640_sel_asrc_clk_src(struct snd_soc_component *component,
20278c2ecf20Sopenharmony_ci		unsigned int filter_mask, unsigned int clk_src)
20288c2ecf20Sopenharmony_ci{
20298c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
20308c2ecf20Sopenharmony_ci	unsigned int asrc2_mask = 0;
20318c2ecf20Sopenharmony_ci	unsigned int asrc2_value = 0;
20328c2ecf20Sopenharmony_ci
20338c2ecf20Sopenharmony_ci	switch (clk_src) {
20348c2ecf20Sopenharmony_ci	case RT5640_CLK_SEL_SYS:
20358c2ecf20Sopenharmony_ci	case RT5640_CLK_SEL_ASRC:
20368c2ecf20Sopenharmony_ci		break;
20378c2ecf20Sopenharmony_ci
20388c2ecf20Sopenharmony_ci	default:
20398c2ecf20Sopenharmony_ci		return -EINVAL;
20408c2ecf20Sopenharmony_ci	}
20418c2ecf20Sopenharmony_ci
20428c2ecf20Sopenharmony_ci	if (!filter_mask)
20438c2ecf20Sopenharmony_ci		return -EINVAL;
20448c2ecf20Sopenharmony_ci
20458c2ecf20Sopenharmony_ci	if (filter_mask & RT5640_DA_STEREO_FILTER) {
20468c2ecf20Sopenharmony_ci		asrc2_mask |= RT5640_STO_DAC_M_MASK;
20478c2ecf20Sopenharmony_ci		asrc2_value = (asrc2_value & ~RT5640_STO_DAC_M_MASK)
20488c2ecf20Sopenharmony_ci			| (clk_src << RT5640_STO_DAC_M_SFT);
20498c2ecf20Sopenharmony_ci	}
20508c2ecf20Sopenharmony_ci
20518c2ecf20Sopenharmony_ci	if (filter_mask & RT5640_DA_MONO_L_FILTER) {
20528c2ecf20Sopenharmony_ci		asrc2_mask |= RT5640_MDA_L_M_MASK;
20538c2ecf20Sopenharmony_ci		asrc2_value = (asrc2_value & ~RT5640_MDA_L_M_MASK)
20548c2ecf20Sopenharmony_ci			| (clk_src << RT5640_MDA_L_M_SFT);
20558c2ecf20Sopenharmony_ci	}
20568c2ecf20Sopenharmony_ci
20578c2ecf20Sopenharmony_ci	if (filter_mask & RT5640_DA_MONO_R_FILTER) {
20588c2ecf20Sopenharmony_ci		asrc2_mask |= RT5640_MDA_R_M_MASK;
20598c2ecf20Sopenharmony_ci		asrc2_value = (asrc2_value & ~RT5640_MDA_R_M_MASK)
20608c2ecf20Sopenharmony_ci			| (clk_src << RT5640_MDA_R_M_SFT);
20618c2ecf20Sopenharmony_ci	}
20628c2ecf20Sopenharmony_ci
20638c2ecf20Sopenharmony_ci	if (filter_mask & RT5640_AD_STEREO_FILTER) {
20648c2ecf20Sopenharmony_ci		asrc2_mask |= RT5640_ADC_M_MASK;
20658c2ecf20Sopenharmony_ci		asrc2_value = (asrc2_value & ~RT5640_ADC_M_MASK)
20668c2ecf20Sopenharmony_ci			| (clk_src << RT5640_ADC_M_SFT);
20678c2ecf20Sopenharmony_ci	}
20688c2ecf20Sopenharmony_ci
20698c2ecf20Sopenharmony_ci	if (filter_mask & RT5640_AD_MONO_L_FILTER) {
20708c2ecf20Sopenharmony_ci		asrc2_mask |= RT5640_MAD_L_M_MASK;
20718c2ecf20Sopenharmony_ci		asrc2_value = (asrc2_value & ~RT5640_MAD_L_M_MASK)
20728c2ecf20Sopenharmony_ci			| (clk_src << RT5640_MAD_L_M_SFT);
20738c2ecf20Sopenharmony_ci	}
20748c2ecf20Sopenharmony_ci
20758c2ecf20Sopenharmony_ci	if (filter_mask & RT5640_AD_MONO_R_FILTER)  {
20768c2ecf20Sopenharmony_ci		asrc2_mask |= RT5640_MAD_R_M_MASK;
20778c2ecf20Sopenharmony_ci		asrc2_value = (asrc2_value & ~RT5640_MAD_R_M_MASK)
20788c2ecf20Sopenharmony_ci			| (clk_src << RT5640_MAD_R_M_SFT);
20798c2ecf20Sopenharmony_ci	}
20808c2ecf20Sopenharmony_ci
20818c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_ASRC_2,
20828c2ecf20Sopenharmony_ci		asrc2_mask, asrc2_value);
20838c2ecf20Sopenharmony_ci
20848c2ecf20Sopenharmony_ci	if (snd_soc_component_read(component, RT5640_ASRC_2)) {
20858c2ecf20Sopenharmony_ci		rt5640->asrc_en = true;
20868c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_JD_CTRL, 0x3, 0x3);
20878c2ecf20Sopenharmony_ci	} else {
20888c2ecf20Sopenharmony_ci		rt5640->asrc_en = false;
20898c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_JD_CTRL, 0x3, 0x0);
20908c2ecf20Sopenharmony_ci	}
20918c2ecf20Sopenharmony_ci
20928c2ecf20Sopenharmony_ci	return 0;
20938c2ecf20Sopenharmony_ci}
20948c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt5640_sel_asrc_clk_src);
20958c2ecf20Sopenharmony_ci
20968c2ecf20Sopenharmony_cistatic void rt5640_enable_micbias1_for_ovcd(struct snd_soc_component *component)
20978c2ecf20Sopenharmony_ci{
20988c2ecf20Sopenharmony_ci	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
20998c2ecf20Sopenharmony_ci
21008c2ecf20Sopenharmony_ci	snd_soc_dapm_mutex_lock(dapm);
21018c2ecf20Sopenharmony_ci	snd_soc_dapm_force_enable_pin_unlocked(dapm, "LDO2");
21028c2ecf20Sopenharmony_ci	snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS1");
21038c2ecf20Sopenharmony_ci	/* OVCD is unreliable when used with RCCLK as sysclk-source */
21048c2ecf20Sopenharmony_ci	snd_soc_dapm_force_enable_pin_unlocked(dapm, "Platform Clock");
21058c2ecf20Sopenharmony_ci	snd_soc_dapm_sync_unlocked(dapm);
21068c2ecf20Sopenharmony_ci	snd_soc_dapm_mutex_unlock(dapm);
21078c2ecf20Sopenharmony_ci}
21088c2ecf20Sopenharmony_ci
21098c2ecf20Sopenharmony_cistatic void rt5640_disable_micbias1_for_ovcd(struct snd_soc_component *component)
21108c2ecf20Sopenharmony_ci{
21118c2ecf20Sopenharmony_ci	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
21128c2ecf20Sopenharmony_ci
21138c2ecf20Sopenharmony_ci	snd_soc_dapm_mutex_lock(dapm);
21148c2ecf20Sopenharmony_ci	snd_soc_dapm_disable_pin_unlocked(dapm, "Platform Clock");
21158c2ecf20Sopenharmony_ci	snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS1");
21168c2ecf20Sopenharmony_ci	snd_soc_dapm_disable_pin_unlocked(dapm, "LDO2");
21178c2ecf20Sopenharmony_ci	snd_soc_dapm_sync_unlocked(dapm);
21188c2ecf20Sopenharmony_ci	snd_soc_dapm_mutex_unlock(dapm);
21198c2ecf20Sopenharmony_ci}
21208c2ecf20Sopenharmony_ci
21218c2ecf20Sopenharmony_cistatic void rt5640_enable_micbias1_ovcd_irq(struct snd_soc_component *component)
21228c2ecf20Sopenharmony_ci{
21238c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
21248c2ecf20Sopenharmony_ci
21258c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
21268c2ecf20Sopenharmony_ci		RT5640_IRQ_MB1_OC_MASK, RT5640_IRQ_MB1_OC_NOR);
21278c2ecf20Sopenharmony_ci	rt5640->ovcd_irq_enabled = true;
21288c2ecf20Sopenharmony_ci}
21298c2ecf20Sopenharmony_ci
21308c2ecf20Sopenharmony_cistatic void rt5640_disable_micbias1_ovcd_irq(struct snd_soc_component *component)
21318c2ecf20Sopenharmony_ci{
21328c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
21338c2ecf20Sopenharmony_ci
21348c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
21358c2ecf20Sopenharmony_ci		RT5640_IRQ_MB1_OC_MASK, RT5640_IRQ_MB1_OC_BP);
21368c2ecf20Sopenharmony_ci	rt5640->ovcd_irq_enabled = false;
21378c2ecf20Sopenharmony_ci}
21388c2ecf20Sopenharmony_ci
21398c2ecf20Sopenharmony_cistatic void rt5640_clear_micbias1_ovcd(struct snd_soc_component *component)
21408c2ecf20Sopenharmony_ci{
21418c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
21428c2ecf20Sopenharmony_ci		RT5640_MB1_OC_STATUS, 0);
21438c2ecf20Sopenharmony_ci}
21448c2ecf20Sopenharmony_ci
21458c2ecf20Sopenharmony_cistatic bool rt5640_micbias1_ovcd(struct snd_soc_component *component)
21468c2ecf20Sopenharmony_ci{
21478c2ecf20Sopenharmony_ci	int val;
21488c2ecf20Sopenharmony_ci
21498c2ecf20Sopenharmony_ci	val = snd_soc_component_read(component, RT5640_IRQ_CTRL2);
21508c2ecf20Sopenharmony_ci	dev_dbg(component->dev, "irq ctrl2 %#04x\n", val);
21518c2ecf20Sopenharmony_ci
21528c2ecf20Sopenharmony_ci	return (val & RT5640_MB1_OC_STATUS);
21538c2ecf20Sopenharmony_ci}
21548c2ecf20Sopenharmony_ci
21558c2ecf20Sopenharmony_cistatic bool rt5640_jack_inserted(struct snd_soc_component *component)
21568c2ecf20Sopenharmony_ci{
21578c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
21588c2ecf20Sopenharmony_ci	int val;
21598c2ecf20Sopenharmony_ci
21608c2ecf20Sopenharmony_ci	val = snd_soc_component_read(component, RT5640_INT_IRQ_ST);
21618c2ecf20Sopenharmony_ci	dev_dbg(component->dev, "irq status %#04x\n", val);
21628c2ecf20Sopenharmony_ci
21638c2ecf20Sopenharmony_ci	if (rt5640->jd_inverted)
21648c2ecf20Sopenharmony_ci		return !(val & RT5640_JD_STATUS);
21658c2ecf20Sopenharmony_ci	else
21668c2ecf20Sopenharmony_ci		return (val & RT5640_JD_STATUS);
21678c2ecf20Sopenharmony_ci}
21688c2ecf20Sopenharmony_ci
21698c2ecf20Sopenharmony_ci/* Jack detect and button-press timings */
21708c2ecf20Sopenharmony_ci#define JACK_SETTLE_TIME	100 /* milli seconds */
21718c2ecf20Sopenharmony_ci#define JACK_DETECT_COUNT	5
21728c2ecf20Sopenharmony_ci#define JACK_DETECT_MAXCOUNT	20  /* Aprox. 2 seconds worth of tries */
21738c2ecf20Sopenharmony_ci#define JACK_UNPLUG_TIME	80  /* milli seconds */
21748c2ecf20Sopenharmony_ci#define BP_POLL_TIME		10  /* milli seconds */
21758c2ecf20Sopenharmony_ci#define BP_POLL_MAXCOUNT	200 /* assume something is wrong after this */
21768c2ecf20Sopenharmony_ci#define BP_THRESHOLD		3
21778c2ecf20Sopenharmony_ci
21788c2ecf20Sopenharmony_cistatic void rt5640_start_button_press_work(struct snd_soc_component *component)
21798c2ecf20Sopenharmony_ci{
21808c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
21818c2ecf20Sopenharmony_ci
21828c2ecf20Sopenharmony_ci	rt5640->poll_count = 0;
21838c2ecf20Sopenharmony_ci	rt5640->press_count = 0;
21848c2ecf20Sopenharmony_ci	rt5640->release_count = 0;
21858c2ecf20Sopenharmony_ci	rt5640->pressed = false;
21868c2ecf20Sopenharmony_ci	rt5640->press_reported = false;
21878c2ecf20Sopenharmony_ci	rt5640_clear_micbias1_ovcd(component);
21888c2ecf20Sopenharmony_ci	schedule_delayed_work(&rt5640->bp_work, msecs_to_jiffies(BP_POLL_TIME));
21898c2ecf20Sopenharmony_ci}
21908c2ecf20Sopenharmony_ci
21918c2ecf20Sopenharmony_cistatic void rt5640_button_press_work(struct work_struct *work)
21928c2ecf20Sopenharmony_ci{
21938c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 =
21948c2ecf20Sopenharmony_ci		container_of(work, struct rt5640_priv, bp_work.work);
21958c2ecf20Sopenharmony_ci	struct snd_soc_component *component = rt5640->component;
21968c2ecf20Sopenharmony_ci
21978c2ecf20Sopenharmony_ci	/* Check the jack was not removed underneath us */
21988c2ecf20Sopenharmony_ci	if (!rt5640_jack_inserted(component))
21998c2ecf20Sopenharmony_ci		return;
22008c2ecf20Sopenharmony_ci
22018c2ecf20Sopenharmony_ci	if (rt5640_micbias1_ovcd(component)) {
22028c2ecf20Sopenharmony_ci		rt5640->release_count = 0;
22038c2ecf20Sopenharmony_ci		rt5640->press_count++;
22048c2ecf20Sopenharmony_ci		/* Remember till after JACK_UNPLUG_TIME wait */
22058c2ecf20Sopenharmony_ci		if (rt5640->press_count >= BP_THRESHOLD)
22068c2ecf20Sopenharmony_ci			rt5640->pressed = true;
22078c2ecf20Sopenharmony_ci		rt5640_clear_micbias1_ovcd(component);
22088c2ecf20Sopenharmony_ci	} else {
22098c2ecf20Sopenharmony_ci		rt5640->press_count = 0;
22108c2ecf20Sopenharmony_ci		rt5640->release_count++;
22118c2ecf20Sopenharmony_ci	}
22128c2ecf20Sopenharmony_ci
22138c2ecf20Sopenharmony_ci	/*
22148c2ecf20Sopenharmony_ci	 * The pins get temporarily shorted on jack unplug, so we poll for
22158c2ecf20Sopenharmony_ci	 * at least JACK_UNPLUG_TIME milli-seconds before reporting a press.
22168c2ecf20Sopenharmony_ci	 */
22178c2ecf20Sopenharmony_ci	rt5640->poll_count++;
22188c2ecf20Sopenharmony_ci	if (rt5640->poll_count < (JACK_UNPLUG_TIME / BP_POLL_TIME)) {
22198c2ecf20Sopenharmony_ci		schedule_delayed_work(&rt5640->bp_work,
22208c2ecf20Sopenharmony_ci				      msecs_to_jiffies(BP_POLL_TIME));
22218c2ecf20Sopenharmony_ci		return;
22228c2ecf20Sopenharmony_ci	}
22238c2ecf20Sopenharmony_ci
22248c2ecf20Sopenharmony_ci	if (rt5640->pressed && !rt5640->press_reported) {
22258c2ecf20Sopenharmony_ci		dev_dbg(component->dev, "headset button press\n");
22268c2ecf20Sopenharmony_ci		snd_soc_jack_report(rt5640->jack, SND_JACK_BTN_0,
22278c2ecf20Sopenharmony_ci				    SND_JACK_BTN_0);
22288c2ecf20Sopenharmony_ci		rt5640->press_reported = true;
22298c2ecf20Sopenharmony_ci	}
22308c2ecf20Sopenharmony_ci
22318c2ecf20Sopenharmony_ci	if (rt5640->release_count >= BP_THRESHOLD) {
22328c2ecf20Sopenharmony_ci		if (rt5640->press_reported) {
22338c2ecf20Sopenharmony_ci			dev_dbg(component->dev, "headset button release\n");
22348c2ecf20Sopenharmony_ci			snd_soc_jack_report(rt5640->jack, 0, SND_JACK_BTN_0);
22358c2ecf20Sopenharmony_ci		}
22368c2ecf20Sopenharmony_ci		/* Re-enable OVCD IRQ to detect next press */
22378c2ecf20Sopenharmony_ci		rt5640_enable_micbias1_ovcd_irq(component);
22388c2ecf20Sopenharmony_ci		return; /* Stop polling */
22398c2ecf20Sopenharmony_ci	}
22408c2ecf20Sopenharmony_ci
22418c2ecf20Sopenharmony_ci	schedule_delayed_work(&rt5640->bp_work, msecs_to_jiffies(BP_POLL_TIME));
22428c2ecf20Sopenharmony_ci}
22438c2ecf20Sopenharmony_ci
22448c2ecf20Sopenharmony_cistatic int rt5640_detect_headset(struct snd_soc_component *component)
22458c2ecf20Sopenharmony_ci{
22468c2ecf20Sopenharmony_ci	int i, headset_count = 0, headphone_count = 0;
22478c2ecf20Sopenharmony_ci
22488c2ecf20Sopenharmony_ci	/*
22498c2ecf20Sopenharmony_ci	 * We get the insertion event before the jack is fully inserted at which
22508c2ecf20Sopenharmony_ci	 * point the second ring on a TRRS connector may short the 2nd ring and
22518c2ecf20Sopenharmony_ci	 * sleeve contacts, also the overcurrent detection is not entirely
22528c2ecf20Sopenharmony_ci	 * reliable. So we try several times with a wait in between until we
22538c2ecf20Sopenharmony_ci	 * detect the same type JACK_DETECT_COUNT times in a row.
22548c2ecf20Sopenharmony_ci	 */
22558c2ecf20Sopenharmony_ci	for (i = 0; i < JACK_DETECT_MAXCOUNT; i++) {
22568c2ecf20Sopenharmony_ci		/* Clear any previous over-current status flag */
22578c2ecf20Sopenharmony_ci		rt5640_clear_micbias1_ovcd(component);
22588c2ecf20Sopenharmony_ci
22598c2ecf20Sopenharmony_ci		msleep(JACK_SETTLE_TIME);
22608c2ecf20Sopenharmony_ci
22618c2ecf20Sopenharmony_ci		/* Check the jack is still connected before checking ovcd */
22628c2ecf20Sopenharmony_ci		if (!rt5640_jack_inserted(component))
22638c2ecf20Sopenharmony_ci			return 0;
22648c2ecf20Sopenharmony_ci
22658c2ecf20Sopenharmony_ci		if (rt5640_micbias1_ovcd(component)) {
22668c2ecf20Sopenharmony_ci			/*
22678c2ecf20Sopenharmony_ci			 * Over current detected, there is a short between the
22688c2ecf20Sopenharmony_ci			 * 2nd ring contact and the ground, so a TRS connector
22698c2ecf20Sopenharmony_ci			 * without a mic contact and thus plain headphones.
22708c2ecf20Sopenharmony_ci			 */
22718c2ecf20Sopenharmony_ci			dev_dbg(component->dev, "jack mic-gnd shorted\n");
22728c2ecf20Sopenharmony_ci			headset_count = 0;
22738c2ecf20Sopenharmony_ci			headphone_count++;
22748c2ecf20Sopenharmony_ci			if (headphone_count == JACK_DETECT_COUNT)
22758c2ecf20Sopenharmony_ci				return SND_JACK_HEADPHONE;
22768c2ecf20Sopenharmony_ci		} else {
22778c2ecf20Sopenharmony_ci			dev_dbg(component->dev, "jack mic-gnd open\n");
22788c2ecf20Sopenharmony_ci			headphone_count = 0;
22798c2ecf20Sopenharmony_ci			headset_count++;
22808c2ecf20Sopenharmony_ci			if (headset_count == JACK_DETECT_COUNT)
22818c2ecf20Sopenharmony_ci				return SND_JACK_HEADSET;
22828c2ecf20Sopenharmony_ci		}
22838c2ecf20Sopenharmony_ci	}
22848c2ecf20Sopenharmony_ci
22858c2ecf20Sopenharmony_ci	dev_err(component->dev, "Error detecting headset vs headphones, bad contact?, assuming headphones\n");
22868c2ecf20Sopenharmony_ci	return SND_JACK_HEADPHONE;
22878c2ecf20Sopenharmony_ci}
22888c2ecf20Sopenharmony_ci
22898c2ecf20Sopenharmony_cistatic void rt5640_jack_work(struct work_struct *work)
22908c2ecf20Sopenharmony_ci{
22918c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 =
22928c2ecf20Sopenharmony_ci		container_of(work, struct rt5640_priv, jack_work);
22938c2ecf20Sopenharmony_ci	struct snd_soc_component *component = rt5640->component;
22948c2ecf20Sopenharmony_ci	int status;
22958c2ecf20Sopenharmony_ci
22968c2ecf20Sopenharmony_ci	if (!rt5640_jack_inserted(component)) {
22978c2ecf20Sopenharmony_ci		/* Jack removed, or spurious IRQ? */
22988c2ecf20Sopenharmony_ci		if (rt5640->jack->status & SND_JACK_HEADPHONE) {
22998c2ecf20Sopenharmony_ci			if (rt5640->jack->status & SND_JACK_MICROPHONE) {
23008c2ecf20Sopenharmony_ci				cancel_delayed_work_sync(&rt5640->bp_work);
23018c2ecf20Sopenharmony_ci				rt5640_disable_micbias1_ovcd_irq(component);
23028c2ecf20Sopenharmony_ci				rt5640_disable_micbias1_for_ovcd(component);
23038c2ecf20Sopenharmony_ci			}
23048c2ecf20Sopenharmony_ci			snd_soc_jack_report(rt5640->jack, 0,
23058c2ecf20Sopenharmony_ci					    SND_JACK_HEADSET | SND_JACK_BTN_0);
23068c2ecf20Sopenharmony_ci			dev_dbg(component->dev, "jack unplugged\n");
23078c2ecf20Sopenharmony_ci		}
23088c2ecf20Sopenharmony_ci	} else if (!(rt5640->jack->status & SND_JACK_HEADPHONE)) {
23098c2ecf20Sopenharmony_ci		/* Jack inserted */
23108c2ecf20Sopenharmony_ci		WARN_ON(rt5640->ovcd_irq_enabled);
23118c2ecf20Sopenharmony_ci		rt5640_enable_micbias1_for_ovcd(component);
23128c2ecf20Sopenharmony_ci		status = rt5640_detect_headset(component);
23138c2ecf20Sopenharmony_ci		if (status == SND_JACK_HEADSET) {
23148c2ecf20Sopenharmony_ci			/* Enable ovcd IRQ for button press detect. */
23158c2ecf20Sopenharmony_ci			rt5640_enable_micbias1_ovcd_irq(component);
23168c2ecf20Sopenharmony_ci		} else {
23178c2ecf20Sopenharmony_ci			/* No more need for overcurrent detect. */
23188c2ecf20Sopenharmony_ci			rt5640_disable_micbias1_for_ovcd(component);
23198c2ecf20Sopenharmony_ci		}
23208c2ecf20Sopenharmony_ci		dev_dbg(component->dev, "detect status %#02x\n", status);
23218c2ecf20Sopenharmony_ci		snd_soc_jack_report(rt5640->jack, status, SND_JACK_HEADSET);
23228c2ecf20Sopenharmony_ci	} else if (rt5640->ovcd_irq_enabled && rt5640_micbias1_ovcd(component)) {
23238c2ecf20Sopenharmony_ci		dev_dbg(component->dev, "OVCD IRQ\n");
23248c2ecf20Sopenharmony_ci
23258c2ecf20Sopenharmony_ci		/*
23268c2ecf20Sopenharmony_ci		 * The ovcd IRQ keeps firing while the button is pressed, so
23278c2ecf20Sopenharmony_ci		 * we disable it and start polling the button until released.
23288c2ecf20Sopenharmony_ci		 *
23298c2ecf20Sopenharmony_ci		 * The disable will make the IRQ pin 0 again and since we get
23308c2ecf20Sopenharmony_ci		 * IRQs on both edges (so as to detect both jack plugin and
23318c2ecf20Sopenharmony_ci		 * unplug) this means we will immediately get another IRQ.
23328c2ecf20Sopenharmony_ci		 * The ovcd_irq_enabled check above makes the 2ND IRQ a NOP.
23338c2ecf20Sopenharmony_ci		 */
23348c2ecf20Sopenharmony_ci		rt5640_disable_micbias1_ovcd_irq(component);
23358c2ecf20Sopenharmony_ci		rt5640_start_button_press_work(component);
23368c2ecf20Sopenharmony_ci
23378c2ecf20Sopenharmony_ci		/*
23388c2ecf20Sopenharmony_ci		 * If the jack-detect IRQ flag goes high (unplug) after our
23398c2ecf20Sopenharmony_ci		 * above rt5640_jack_inserted() check and before we have
23408c2ecf20Sopenharmony_ci		 * disabled the OVCD IRQ, the IRQ pin will stay high and as
23418c2ecf20Sopenharmony_ci		 * we react to edges, we miss the unplug event -> recheck.
23428c2ecf20Sopenharmony_ci		 */
23438c2ecf20Sopenharmony_ci		queue_work(system_long_wq, &rt5640->jack_work);
23448c2ecf20Sopenharmony_ci	}
23458c2ecf20Sopenharmony_ci}
23468c2ecf20Sopenharmony_ci
23478c2ecf20Sopenharmony_cistatic irqreturn_t rt5640_irq(int irq, void *data)
23488c2ecf20Sopenharmony_ci{
23498c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = data;
23508c2ecf20Sopenharmony_ci
23518c2ecf20Sopenharmony_ci	if (rt5640->jack)
23528c2ecf20Sopenharmony_ci		queue_work(system_long_wq, &rt5640->jack_work);
23538c2ecf20Sopenharmony_ci
23548c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
23558c2ecf20Sopenharmony_ci}
23568c2ecf20Sopenharmony_ci
23578c2ecf20Sopenharmony_cistatic void rt5640_cancel_work(void *data)
23588c2ecf20Sopenharmony_ci{
23598c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = data;
23608c2ecf20Sopenharmony_ci
23618c2ecf20Sopenharmony_ci	cancel_work_sync(&rt5640->jack_work);
23628c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&rt5640->bp_work);
23638c2ecf20Sopenharmony_ci}
23648c2ecf20Sopenharmony_ci
23658c2ecf20Sopenharmony_cistatic void rt5640_enable_jack_detect(struct snd_soc_component *component,
23668c2ecf20Sopenharmony_ci				      struct snd_soc_jack *jack)
23678c2ecf20Sopenharmony_ci{
23688c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
23698c2ecf20Sopenharmony_ci
23708c2ecf20Sopenharmony_ci	/* Select JD-source */
23718c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_JD_CTRL,
23728c2ecf20Sopenharmony_ci		RT5640_JD_MASK, rt5640->jd_src);
23738c2ecf20Sopenharmony_ci
23748c2ecf20Sopenharmony_ci	/* Selecting GPIO01 as an interrupt */
23758c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_GPIO_CTRL1,
23768c2ecf20Sopenharmony_ci		RT5640_GP1_PIN_MASK, RT5640_GP1_PIN_IRQ);
23778c2ecf20Sopenharmony_ci
23788c2ecf20Sopenharmony_ci	/* Set GPIO1 output */
23798c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_GPIO_CTRL3,
23808c2ecf20Sopenharmony_ci		RT5640_GP1_PF_MASK, RT5640_GP1_PF_OUT);
23818c2ecf20Sopenharmony_ci
23828c2ecf20Sopenharmony_ci	/* Enabling jd2 in general control 1 */
23838c2ecf20Sopenharmony_ci	snd_soc_component_write(component, RT5640_DUMMY1, 0x3f41);
23848c2ecf20Sopenharmony_ci
23858c2ecf20Sopenharmony_ci	/* Enabling jd2 in general control 2 */
23868c2ecf20Sopenharmony_ci	snd_soc_component_write(component, RT5640_DUMMY2, 0x4001);
23878c2ecf20Sopenharmony_ci
23888c2ecf20Sopenharmony_ci	snd_soc_component_write(component, RT5640_PR_BASE + RT5640_BIAS_CUR4,
23898c2ecf20Sopenharmony_ci		0xa800 | rt5640->ovcd_sf);
23908c2ecf20Sopenharmony_ci
23918c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_MICBIAS,
23928c2ecf20Sopenharmony_ci		RT5640_MIC1_OVTH_MASK | RT5640_MIC1_OVCD_MASK,
23938c2ecf20Sopenharmony_ci		rt5640->ovcd_th | RT5640_MIC1_OVCD_EN);
23948c2ecf20Sopenharmony_ci
23958c2ecf20Sopenharmony_ci	/*
23968c2ecf20Sopenharmony_ci	 * The over-current-detect is only reliable in detecting the absence
23978c2ecf20Sopenharmony_ci	 * of over-current, when the mic-contact in the jack is short-circuited,
23988c2ecf20Sopenharmony_ci	 * the hardware periodically retries if it can apply the bias-current
23998c2ecf20Sopenharmony_ci	 * leading to the ovcd status flip-flopping 1-0-1 with it being 0 about
24008c2ecf20Sopenharmony_ci	 * 10% of the time, as we poll the ovcd status bit we might hit that
24018c2ecf20Sopenharmony_ci	 * 10%, so we enable sticky mode and when checking OVCD we clear the
24028c2ecf20Sopenharmony_ci	 * status, msleep() a bit and then check to get a reliable reading.
24038c2ecf20Sopenharmony_ci	 */
24048c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_IRQ_CTRL2,
24058c2ecf20Sopenharmony_ci		RT5640_MB1_OC_STKY_MASK, RT5640_MB1_OC_STKY_EN);
24068c2ecf20Sopenharmony_ci
24078c2ecf20Sopenharmony_ci	/*
24088c2ecf20Sopenharmony_ci	 * All IRQs get or-ed together, so we need the jack IRQ to report 0
24098c2ecf20Sopenharmony_ci	 * when a jack is inserted so that the OVCD IRQ then toggles the IRQ
24108c2ecf20Sopenharmony_ci	 * pin 0/1 instead of it being stuck to 1. So we invert the JD polarity
24118c2ecf20Sopenharmony_ci	 * on systems where the hardware does not already do this.
24128c2ecf20Sopenharmony_ci	 */
24138c2ecf20Sopenharmony_ci	if (rt5640->jd_inverted)
24148c2ecf20Sopenharmony_ci		snd_soc_component_write(component, RT5640_IRQ_CTRL1,
24158c2ecf20Sopenharmony_ci					RT5640_IRQ_JD_NOR);
24168c2ecf20Sopenharmony_ci	else
24178c2ecf20Sopenharmony_ci		snd_soc_component_write(component, RT5640_IRQ_CTRL1,
24188c2ecf20Sopenharmony_ci					RT5640_IRQ_JD_NOR | RT5640_JD_P_INV);
24198c2ecf20Sopenharmony_ci
24208c2ecf20Sopenharmony_ci	rt5640->jack = jack;
24218c2ecf20Sopenharmony_ci	if (rt5640->jack->status & SND_JACK_MICROPHONE) {
24228c2ecf20Sopenharmony_ci		rt5640_enable_micbias1_for_ovcd(component);
24238c2ecf20Sopenharmony_ci		rt5640_enable_micbias1_ovcd_irq(component);
24248c2ecf20Sopenharmony_ci	}
24258c2ecf20Sopenharmony_ci
24268c2ecf20Sopenharmony_ci	enable_irq(rt5640->irq);
24278c2ecf20Sopenharmony_ci	/* sync initial jack state */
24288c2ecf20Sopenharmony_ci	queue_work(system_long_wq, &rt5640->jack_work);
24298c2ecf20Sopenharmony_ci}
24308c2ecf20Sopenharmony_ci
24318c2ecf20Sopenharmony_cistatic void rt5640_disable_jack_detect(struct snd_soc_component *component)
24328c2ecf20Sopenharmony_ci{
24338c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
24348c2ecf20Sopenharmony_ci
24358c2ecf20Sopenharmony_ci	/*
24368c2ecf20Sopenharmony_ci	 * soc_remove_component() force-disables jack and thus rt5640->jack
24378c2ecf20Sopenharmony_ci	 * could be NULL at the time of driver's module unloading.
24388c2ecf20Sopenharmony_ci	 */
24398c2ecf20Sopenharmony_ci	if (!rt5640->jack)
24408c2ecf20Sopenharmony_ci		return;
24418c2ecf20Sopenharmony_ci
24428c2ecf20Sopenharmony_ci	disable_irq(rt5640->irq);
24438c2ecf20Sopenharmony_ci	rt5640_cancel_work(rt5640);
24448c2ecf20Sopenharmony_ci
24458c2ecf20Sopenharmony_ci	if (rt5640->jack->status & SND_JACK_MICROPHONE) {
24468c2ecf20Sopenharmony_ci		rt5640_disable_micbias1_ovcd_irq(component);
24478c2ecf20Sopenharmony_ci		rt5640_disable_micbias1_for_ovcd(component);
24488c2ecf20Sopenharmony_ci		snd_soc_jack_report(rt5640->jack, 0, SND_JACK_BTN_0);
24498c2ecf20Sopenharmony_ci	}
24508c2ecf20Sopenharmony_ci
24518c2ecf20Sopenharmony_ci	rt5640->jack = NULL;
24528c2ecf20Sopenharmony_ci}
24538c2ecf20Sopenharmony_ci
24548c2ecf20Sopenharmony_cistatic int rt5640_set_jack(struct snd_soc_component *component,
24558c2ecf20Sopenharmony_ci			   struct snd_soc_jack *jack, void *data)
24568c2ecf20Sopenharmony_ci{
24578c2ecf20Sopenharmony_ci	if (jack)
24588c2ecf20Sopenharmony_ci		rt5640_enable_jack_detect(component, jack);
24598c2ecf20Sopenharmony_ci	else
24608c2ecf20Sopenharmony_ci		rt5640_disable_jack_detect(component);
24618c2ecf20Sopenharmony_ci
24628c2ecf20Sopenharmony_ci	return 0;
24638c2ecf20Sopenharmony_ci}
24648c2ecf20Sopenharmony_ci
24658c2ecf20Sopenharmony_cistatic int rt5640_probe(struct snd_soc_component *component)
24668c2ecf20Sopenharmony_ci{
24678c2ecf20Sopenharmony_ci	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
24688c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
24698c2ecf20Sopenharmony_ci	u32 dmic1_data_pin = 0;
24708c2ecf20Sopenharmony_ci	u32 dmic2_data_pin = 0;
24718c2ecf20Sopenharmony_ci	bool dmic_en = false;
24728c2ecf20Sopenharmony_ci	u32 val;
24738c2ecf20Sopenharmony_ci
24748c2ecf20Sopenharmony_ci	/* Check if MCLK provided */
24758c2ecf20Sopenharmony_ci	rt5640->mclk = devm_clk_get(component->dev, "mclk");
24768c2ecf20Sopenharmony_ci	if (PTR_ERR(rt5640->mclk) == -EPROBE_DEFER)
24778c2ecf20Sopenharmony_ci		return -EPROBE_DEFER;
24788c2ecf20Sopenharmony_ci
24798c2ecf20Sopenharmony_ci	rt5640->component = component;
24808c2ecf20Sopenharmony_ci
24818c2ecf20Sopenharmony_ci	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
24828c2ecf20Sopenharmony_ci
24838c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_DUMMY1, 0x0301, 0x0301);
24848c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_MICBIAS, 0x0030, 0x0030);
24858c2ecf20Sopenharmony_ci	snd_soc_component_update_bits(component, RT5640_DSP_PATH2, 0xfc00, 0x0c00);
24868c2ecf20Sopenharmony_ci
24878c2ecf20Sopenharmony_ci	switch (snd_soc_component_read(component, RT5640_RESET) & RT5640_ID_MASK) {
24888c2ecf20Sopenharmony_ci	case RT5640_ID_5640:
24898c2ecf20Sopenharmony_ci	case RT5640_ID_5642:
24908c2ecf20Sopenharmony_ci		snd_soc_add_component_controls(component,
24918c2ecf20Sopenharmony_ci			rt5640_specific_snd_controls,
24928c2ecf20Sopenharmony_ci			ARRAY_SIZE(rt5640_specific_snd_controls));
24938c2ecf20Sopenharmony_ci		snd_soc_dapm_new_controls(dapm,
24948c2ecf20Sopenharmony_ci			rt5640_specific_dapm_widgets,
24958c2ecf20Sopenharmony_ci			ARRAY_SIZE(rt5640_specific_dapm_widgets));
24968c2ecf20Sopenharmony_ci		snd_soc_dapm_add_routes(dapm,
24978c2ecf20Sopenharmony_ci			rt5640_specific_dapm_routes,
24988c2ecf20Sopenharmony_ci			ARRAY_SIZE(rt5640_specific_dapm_routes));
24998c2ecf20Sopenharmony_ci		break;
25008c2ecf20Sopenharmony_ci	case RT5640_ID_5639:
25018c2ecf20Sopenharmony_ci		snd_soc_dapm_new_controls(dapm,
25028c2ecf20Sopenharmony_ci			rt5639_specific_dapm_widgets,
25038c2ecf20Sopenharmony_ci			ARRAY_SIZE(rt5639_specific_dapm_widgets));
25048c2ecf20Sopenharmony_ci		snd_soc_dapm_add_routes(dapm,
25058c2ecf20Sopenharmony_ci			rt5639_specific_dapm_routes,
25068c2ecf20Sopenharmony_ci			ARRAY_SIZE(rt5639_specific_dapm_routes));
25078c2ecf20Sopenharmony_ci		break;
25088c2ecf20Sopenharmony_ci	default:
25098c2ecf20Sopenharmony_ci		dev_err(component->dev,
25108c2ecf20Sopenharmony_ci			"The driver is for RT5639 RT5640 or RT5642 only\n");
25118c2ecf20Sopenharmony_ci		return -ENODEV;
25128c2ecf20Sopenharmony_ci	}
25138c2ecf20Sopenharmony_ci
25148c2ecf20Sopenharmony_ci	/*
25158c2ecf20Sopenharmony_ci	 * Note on some platforms the platform code may need to add device-props
25168c2ecf20Sopenharmony_ci	 * rather then relying only on properties set by the firmware.
25178c2ecf20Sopenharmony_ci	 * Therefor the property parsing MUST be done here, rather then from
25188c2ecf20Sopenharmony_ci	 * rt5640_i2c_probe(), so that the platform-code can attach extra
25198c2ecf20Sopenharmony_ci	 * properties before calling snd_soc_register_card().
25208c2ecf20Sopenharmony_ci	 */
25218c2ecf20Sopenharmony_ci	if (device_property_read_bool(component->dev, "realtek,in1-differential"))
25228c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_IN1_IN2,
25238c2ecf20Sopenharmony_ci					      RT5640_IN_DF1, RT5640_IN_DF1);
25248c2ecf20Sopenharmony_ci
25258c2ecf20Sopenharmony_ci	if (device_property_read_bool(component->dev, "realtek,in2-differential"))
25268c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_IN3_IN4,
25278c2ecf20Sopenharmony_ci					      RT5640_IN_DF2, RT5640_IN_DF2);
25288c2ecf20Sopenharmony_ci
25298c2ecf20Sopenharmony_ci	if (device_property_read_bool(component->dev, "realtek,in3-differential"))
25308c2ecf20Sopenharmony_ci		snd_soc_component_update_bits(component, RT5640_IN1_IN2,
25318c2ecf20Sopenharmony_ci					      RT5640_IN_DF2, RT5640_IN_DF2);
25328c2ecf20Sopenharmony_ci
25338c2ecf20Sopenharmony_ci	if (device_property_read_u32(component->dev, "realtek,dmic1-data-pin",
25348c2ecf20Sopenharmony_ci				     &val) == 0 && val) {
25358c2ecf20Sopenharmony_ci		dmic1_data_pin = val - 1;
25368c2ecf20Sopenharmony_ci		dmic_en = true;
25378c2ecf20Sopenharmony_ci	}
25388c2ecf20Sopenharmony_ci
25398c2ecf20Sopenharmony_ci	if (device_property_read_u32(component->dev, "realtek,dmic2-data-pin",
25408c2ecf20Sopenharmony_ci				     &val) == 0 && val) {
25418c2ecf20Sopenharmony_ci		dmic2_data_pin = val - 1;
25428c2ecf20Sopenharmony_ci		dmic_en = true;
25438c2ecf20Sopenharmony_ci	}
25448c2ecf20Sopenharmony_ci
25458c2ecf20Sopenharmony_ci	if (dmic_en)
25468c2ecf20Sopenharmony_ci		rt5640_dmic_enable(component, dmic1_data_pin, dmic2_data_pin);
25478c2ecf20Sopenharmony_ci
25488c2ecf20Sopenharmony_ci	if (device_property_read_u32(component->dev,
25498c2ecf20Sopenharmony_ci				     "realtek,jack-detect-source", &val) == 0) {
25508c2ecf20Sopenharmony_ci		if (val <= RT5640_JD_SRC_GPIO4)
25518c2ecf20Sopenharmony_ci			rt5640->jd_src = val << RT5640_JD_SFT;
25528c2ecf20Sopenharmony_ci		else
25538c2ecf20Sopenharmony_ci			dev_warn(component->dev, "Warning: Invalid jack-detect-source value: %d, leaving jack-detect disabled\n",
25548c2ecf20Sopenharmony_ci				 val);
25558c2ecf20Sopenharmony_ci	}
25568c2ecf20Sopenharmony_ci
25578c2ecf20Sopenharmony_ci	if (!device_property_read_bool(component->dev, "realtek,jack-detect-not-inverted"))
25588c2ecf20Sopenharmony_ci		rt5640->jd_inverted = true;
25598c2ecf20Sopenharmony_ci
25608c2ecf20Sopenharmony_ci	/*
25618c2ecf20Sopenharmony_ci	 * Testing on various boards has shown that good defaults for the OVCD
25628c2ecf20Sopenharmony_ci	 * threshold and scale-factor are 2000µA and 0.75. For an effective
25638c2ecf20Sopenharmony_ci	 * limit of 1500µA, this seems to be more reliable then 1500µA and 1.0.
25648c2ecf20Sopenharmony_ci	 */
25658c2ecf20Sopenharmony_ci	rt5640->ovcd_th = RT5640_MIC1_OVTH_2000UA;
25668c2ecf20Sopenharmony_ci	rt5640->ovcd_sf = RT5640_MIC_OVCD_SF_0P75;
25678c2ecf20Sopenharmony_ci
25688c2ecf20Sopenharmony_ci	if (device_property_read_u32(component->dev,
25698c2ecf20Sopenharmony_ci			"realtek,over-current-threshold-microamp", &val) == 0) {
25708c2ecf20Sopenharmony_ci		switch (val) {
25718c2ecf20Sopenharmony_ci		case 600:
25728c2ecf20Sopenharmony_ci			rt5640->ovcd_th = RT5640_MIC1_OVTH_600UA;
25738c2ecf20Sopenharmony_ci			break;
25748c2ecf20Sopenharmony_ci		case 1500:
25758c2ecf20Sopenharmony_ci			rt5640->ovcd_th = RT5640_MIC1_OVTH_1500UA;
25768c2ecf20Sopenharmony_ci			break;
25778c2ecf20Sopenharmony_ci		case 2000:
25788c2ecf20Sopenharmony_ci			rt5640->ovcd_th = RT5640_MIC1_OVTH_2000UA;
25798c2ecf20Sopenharmony_ci			break;
25808c2ecf20Sopenharmony_ci		default:
25818c2ecf20Sopenharmony_ci			dev_warn(component->dev, "Warning: Invalid over-current-threshold-microamp value: %d, defaulting to 2000uA\n",
25828c2ecf20Sopenharmony_ci				 val);
25838c2ecf20Sopenharmony_ci		}
25848c2ecf20Sopenharmony_ci	}
25858c2ecf20Sopenharmony_ci
25868c2ecf20Sopenharmony_ci	if (device_property_read_u32(component->dev,
25878c2ecf20Sopenharmony_ci			"realtek,over-current-scale-factor", &val) == 0) {
25888c2ecf20Sopenharmony_ci		if (val <= RT5640_OVCD_SF_1P5)
25898c2ecf20Sopenharmony_ci			rt5640->ovcd_sf = val << RT5640_MIC_OVCD_SF_SFT;
25908c2ecf20Sopenharmony_ci		else
25918c2ecf20Sopenharmony_ci			dev_warn(component->dev, "Warning: Invalid over-current-scale-factor value: %d, defaulting to 0.75\n",
25928c2ecf20Sopenharmony_ci				 val);
25938c2ecf20Sopenharmony_ci	}
25948c2ecf20Sopenharmony_ci
25958c2ecf20Sopenharmony_ci	return 0;
25968c2ecf20Sopenharmony_ci}
25978c2ecf20Sopenharmony_ci
25988c2ecf20Sopenharmony_cistatic void rt5640_remove(struct snd_soc_component *component)
25998c2ecf20Sopenharmony_ci{
26008c2ecf20Sopenharmony_ci	rt5640_reset(component);
26018c2ecf20Sopenharmony_ci}
26028c2ecf20Sopenharmony_ci
26038c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
26048c2ecf20Sopenharmony_cistatic int rt5640_suspend(struct snd_soc_component *component)
26058c2ecf20Sopenharmony_ci{
26068c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
26078c2ecf20Sopenharmony_ci
26088c2ecf20Sopenharmony_ci	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
26098c2ecf20Sopenharmony_ci	rt5640_reset(component);
26108c2ecf20Sopenharmony_ci	regcache_cache_only(rt5640->regmap, true);
26118c2ecf20Sopenharmony_ci	regcache_mark_dirty(rt5640->regmap);
26128c2ecf20Sopenharmony_ci	if (gpio_is_valid(rt5640->ldo1_en))
26138c2ecf20Sopenharmony_ci		gpio_set_value_cansleep(rt5640->ldo1_en, 0);
26148c2ecf20Sopenharmony_ci
26158c2ecf20Sopenharmony_ci	return 0;
26168c2ecf20Sopenharmony_ci}
26178c2ecf20Sopenharmony_ci
26188c2ecf20Sopenharmony_cistatic int rt5640_resume(struct snd_soc_component *component)
26198c2ecf20Sopenharmony_ci{
26208c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640 = snd_soc_component_get_drvdata(component);
26218c2ecf20Sopenharmony_ci
26228c2ecf20Sopenharmony_ci	if (gpio_is_valid(rt5640->ldo1_en)) {
26238c2ecf20Sopenharmony_ci		gpio_set_value_cansleep(rt5640->ldo1_en, 1);
26248c2ecf20Sopenharmony_ci		msleep(400);
26258c2ecf20Sopenharmony_ci	}
26268c2ecf20Sopenharmony_ci
26278c2ecf20Sopenharmony_ci	regcache_cache_only(rt5640->regmap, false);
26288c2ecf20Sopenharmony_ci	regcache_sync(rt5640->regmap);
26298c2ecf20Sopenharmony_ci
26308c2ecf20Sopenharmony_ci	return 0;
26318c2ecf20Sopenharmony_ci}
26328c2ecf20Sopenharmony_ci#else
26338c2ecf20Sopenharmony_ci#define rt5640_suspend NULL
26348c2ecf20Sopenharmony_ci#define rt5640_resume NULL
26358c2ecf20Sopenharmony_ci#endif
26368c2ecf20Sopenharmony_ci
26378c2ecf20Sopenharmony_ci#define RT5640_STEREO_RATES SNDRV_PCM_RATE_8000_96000
26388c2ecf20Sopenharmony_ci#define RT5640_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
26398c2ecf20Sopenharmony_ci			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
26408c2ecf20Sopenharmony_ci
26418c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops rt5640_aif_dai_ops = {
26428c2ecf20Sopenharmony_ci	.hw_params = rt5640_hw_params,
26438c2ecf20Sopenharmony_ci	.set_fmt = rt5640_set_dai_fmt,
26448c2ecf20Sopenharmony_ci	.set_sysclk = rt5640_set_dai_sysclk,
26458c2ecf20Sopenharmony_ci	.set_pll = rt5640_set_dai_pll,
26468c2ecf20Sopenharmony_ci};
26478c2ecf20Sopenharmony_ci
26488c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver rt5640_dai[] = {
26498c2ecf20Sopenharmony_ci	{
26508c2ecf20Sopenharmony_ci		.name = "rt5640-aif1",
26518c2ecf20Sopenharmony_ci		.id = RT5640_AIF1,
26528c2ecf20Sopenharmony_ci		.playback = {
26538c2ecf20Sopenharmony_ci			.stream_name = "AIF1 Playback",
26548c2ecf20Sopenharmony_ci			.channels_min = 1,
26558c2ecf20Sopenharmony_ci			.channels_max = 2,
26568c2ecf20Sopenharmony_ci			.rates = RT5640_STEREO_RATES,
26578c2ecf20Sopenharmony_ci			.formats = RT5640_FORMATS,
26588c2ecf20Sopenharmony_ci		},
26598c2ecf20Sopenharmony_ci		.capture = {
26608c2ecf20Sopenharmony_ci			.stream_name = "AIF1 Capture",
26618c2ecf20Sopenharmony_ci			.channels_min = 1,
26628c2ecf20Sopenharmony_ci			.channels_max = 2,
26638c2ecf20Sopenharmony_ci			.rates = RT5640_STEREO_RATES,
26648c2ecf20Sopenharmony_ci			.formats = RT5640_FORMATS,
26658c2ecf20Sopenharmony_ci		},
26668c2ecf20Sopenharmony_ci		.ops = &rt5640_aif_dai_ops,
26678c2ecf20Sopenharmony_ci	},
26688c2ecf20Sopenharmony_ci	{
26698c2ecf20Sopenharmony_ci		.name = "rt5640-aif2",
26708c2ecf20Sopenharmony_ci		.id = RT5640_AIF2,
26718c2ecf20Sopenharmony_ci		.playback = {
26728c2ecf20Sopenharmony_ci			.stream_name = "AIF2 Playback",
26738c2ecf20Sopenharmony_ci			.channels_min = 1,
26748c2ecf20Sopenharmony_ci			.channels_max = 2,
26758c2ecf20Sopenharmony_ci			.rates = RT5640_STEREO_RATES,
26768c2ecf20Sopenharmony_ci			.formats = RT5640_FORMATS,
26778c2ecf20Sopenharmony_ci		},
26788c2ecf20Sopenharmony_ci		.capture = {
26798c2ecf20Sopenharmony_ci			.stream_name = "AIF2 Capture",
26808c2ecf20Sopenharmony_ci			.channels_min = 1,
26818c2ecf20Sopenharmony_ci			.channels_max = 2,
26828c2ecf20Sopenharmony_ci			.rates = RT5640_STEREO_RATES,
26838c2ecf20Sopenharmony_ci			.formats = RT5640_FORMATS,
26848c2ecf20Sopenharmony_ci		},
26858c2ecf20Sopenharmony_ci		.ops = &rt5640_aif_dai_ops,
26868c2ecf20Sopenharmony_ci	},
26878c2ecf20Sopenharmony_ci};
26888c2ecf20Sopenharmony_ci
26898c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver soc_component_dev_rt5640 = {
26908c2ecf20Sopenharmony_ci	.probe			= rt5640_probe,
26918c2ecf20Sopenharmony_ci	.remove			= rt5640_remove,
26928c2ecf20Sopenharmony_ci	.suspend		= rt5640_suspend,
26938c2ecf20Sopenharmony_ci	.resume			= rt5640_resume,
26948c2ecf20Sopenharmony_ci	.set_bias_level		= rt5640_set_bias_level,
26958c2ecf20Sopenharmony_ci	.set_jack		= rt5640_set_jack,
26968c2ecf20Sopenharmony_ci	.controls		= rt5640_snd_controls,
26978c2ecf20Sopenharmony_ci	.num_controls		= ARRAY_SIZE(rt5640_snd_controls),
26988c2ecf20Sopenharmony_ci	.dapm_widgets		= rt5640_dapm_widgets,
26998c2ecf20Sopenharmony_ci	.num_dapm_widgets	= ARRAY_SIZE(rt5640_dapm_widgets),
27008c2ecf20Sopenharmony_ci	.dapm_routes		= rt5640_dapm_routes,
27018c2ecf20Sopenharmony_ci	.num_dapm_routes	= ARRAY_SIZE(rt5640_dapm_routes),
27028c2ecf20Sopenharmony_ci	.use_pmdown_time	= 1,
27038c2ecf20Sopenharmony_ci	.endianness		= 1,
27048c2ecf20Sopenharmony_ci	.non_legacy_dai_naming	= 1,
27058c2ecf20Sopenharmony_ci
27068c2ecf20Sopenharmony_ci};
27078c2ecf20Sopenharmony_ci
27088c2ecf20Sopenharmony_cistatic const struct regmap_config rt5640_regmap = {
27098c2ecf20Sopenharmony_ci	.reg_bits = 8,
27108c2ecf20Sopenharmony_ci	.val_bits = 16,
27118c2ecf20Sopenharmony_ci	.use_single_read = true,
27128c2ecf20Sopenharmony_ci	.use_single_write = true,
27138c2ecf20Sopenharmony_ci
27148c2ecf20Sopenharmony_ci	.max_register = RT5640_VENDOR_ID2 + 1 + (ARRAY_SIZE(rt5640_ranges) *
27158c2ecf20Sopenharmony_ci					       RT5640_PR_SPACING),
27168c2ecf20Sopenharmony_ci	.volatile_reg = rt5640_volatile_register,
27178c2ecf20Sopenharmony_ci	.readable_reg = rt5640_readable_register,
27188c2ecf20Sopenharmony_ci
27198c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
27208c2ecf20Sopenharmony_ci	.reg_defaults = rt5640_reg,
27218c2ecf20Sopenharmony_ci	.num_reg_defaults = ARRAY_SIZE(rt5640_reg),
27228c2ecf20Sopenharmony_ci	.ranges = rt5640_ranges,
27238c2ecf20Sopenharmony_ci	.num_ranges = ARRAY_SIZE(rt5640_ranges),
27248c2ecf20Sopenharmony_ci};
27258c2ecf20Sopenharmony_ci
27268c2ecf20Sopenharmony_cistatic const struct i2c_device_id rt5640_i2c_id[] = {
27278c2ecf20Sopenharmony_ci	{ "rt5640", 0 },
27288c2ecf20Sopenharmony_ci	{ "rt5639", 0 },
27298c2ecf20Sopenharmony_ci	{ "rt5642", 0 },
27308c2ecf20Sopenharmony_ci	{ }
27318c2ecf20Sopenharmony_ci};
27328c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, rt5640_i2c_id);
27338c2ecf20Sopenharmony_ci
27348c2ecf20Sopenharmony_ci#if defined(CONFIG_OF)
27358c2ecf20Sopenharmony_cistatic const struct of_device_id rt5640_of_match[] = {
27368c2ecf20Sopenharmony_ci	{ .compatible = "realtek,rt5639", },
27378c2ecf20Sopenharmony_ci	{ .compatible = "realtek,rt5640", },
27388c2ecf20Sopenharmony_ci	{},
27398c2ecf20Sopenharmony_ci};
27408c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, rt5640_of_match);
27418c2ecf20Sopenharmony_ci#endif
27428c2ecf20Sopenharmony_ci
27438c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI
27448c2ecf20Sopenharmony_cistatic const struct acpi_device_id rt5640_acpi_match[] = {
27458c2ecf20Sopenharmony_ci	{ "INT33CA", 0 },
27468c2ecf20Sopenharmony_ci	{ "10EC3276", 0 },
27478c2ecf20Sopenharmony_ci	{ "10EC5640", 0 },
27488c2ecf20Sopenharmony_ci	{ "10EC5642", 0 },
27498c2ecf20Sopenharmony_ci	{ "INTCCFFD", 0 },
27508c2ecf20Sopenharmony_ci	{ },
27518c2ecf20Sopenharmony_ci};
27528c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, rt5640_acpi_match);
27538c2ecf20Sopenharmony_ci#endif
27548c2ecf20Sopenharmony_ci
27558c2ecf20Sopenharmony_cistatic int rt5640_parse_dt(struct rt5640_priv *rt5640, struct device_node *np)
27568c2ecf20Sopenharmony_ci{
27578c2ecf20Sopenharmony_ci	rt5640->ldo1_en = of_get_named_gpio(np, "realtek,ldo1-en-gpios", 0);
27588c2ecf20Sopenharmony_ci	/*
27598c2ecf20Sopenharmony_ci	 * LDO1_EN is optional (it may be statically tied on the board).
27608c2ecf20Sopenharmony_ci	 * -ENOENT means that the property doesn't exist, i.e. there is no
27618c2ecf20Sopenharmony_ci	 * GPIO, so is not an error. Any other error code means the property
27628c2ecf20Sopenharmony_ci	 * exists, but could not be parsed.
27638c2ecf20Sopenharmony_ci	 */
27648c2ecf20Sopenharmony_ci	if (!gpio_is_valid(rt5640->ldo1_en) &&
27658c2ecf20Sopenharmony_ci			(rt5640->ldo1_en != -ENOENT))
27668c2ecf20Sopenharmony_ci		return rt5640->ldo1_en;
27678c2ecf20Sopenharmony_ci
27688c2ecf20Sopenharmony_ci	return 0;
27698c2ecf20Sopenharmony_ci}
27708c2ecf20Sopenharmony_ci
27718c2ecf20Sopenharmony_cistatic int rt5640_i2c_probe(struct i2c_client *i2c,
27728c2ecf20Sopenharmony_ci		    const struct i2c_device_id *id)
27738c2ecf20Sopenharmony_ci{
27748c2ecf20Sopenharmony_ci	struct rt5640_priv *rt5640;
27758c2ecf20Sopenharmony_ci	int ret;
27768c2ecf20Sopenharmony_ci	unsigned int val;
27778c2ecf20Sopenharmony_ci
27788c2ecf20Sopenharmony_ci	rt5640 = devm_kzalloc(&i2c->dev,
27798c2ecf20Sopenharmony_ci				sizeof(struct rt5640_priv),
27808c2ecf20Sopenharmony_ci				GFP_KERNEL);
27818c2ecf20Sopenharmony_ci	if (NULL == rt5640)
27828c2ecf20Sopenharmony_ci		return -ENOMEM;
27838c2ecf20Sopenharmony_ci	i2c_set_clientdata(i2c, rt5640);
27848c2ecf20Sopenharmony_ci
27858c2ecf20Sopenharmony_ci	if (i2c->dev.of_node) {
27868c2ecf20Sopenharmony_ci		ret = rt5640_parse_dt(rt5640, i2c->dev.of_node);
27878c2ecf20Sopenharmony_ci		if (ret)
27888c2ecf20Sopenharmony_ci			return ret;
27898c2ecf20Sopenharmony_ci	} else
27908c2ecf20Sopenharmony_ci		rt5640->ldo1_en = -EINVAL;
27918c2ecf20Sopenharmony_ci
27928c2ecf20Sopenharmony_ci	rt5640->regmap = devm_regmap_init_i2c(i2c, &rt5640_regmap);
27938c2ecf20Sopenharmony_ci	if (IS_ERR(rt5640->regmap)) {
27948c2ecf20Sopenharmony_ci		ret = PTR_ERR(rt5640->regmap);
27958c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
27968c2ecf20Sopenharmony_ci			ret);
27978c2ecf20Sopenharmony_ci		return ret;
27988c2ecf20Sopenharmony_ci	}
27998c2ecf20Sopenharmony_ci
28008c2ecf20Sopenharmony_ci	if (gpio_is_valid(rt5640->ldo1_en)) {
28018c2ecf20Sopenharmony_ci		ret = devm_gpio_request_one(&i2c->dev, rt5640->ldo1_en,
28028c2ecf20Sopenharmony_ci					    GPIOF_OUT_INIT_HIGH,
28038c2ecf20Sopenharmony_ci					    "RT5640 LDO1_EN");
28048c2ecf20Sopenharmony_ci		if (ret < 0) {
28058c2ecf20Sopenharmony_ci			dev_err(&i2c->dev, "Failed to request LDO1_EN %d: %d\n",
28068c2ecf20Sopenharmony_ci				rt5640->ldo1_en, ret);
28078c2ecf20Sopenharmony_ci			return ret;
28088c2ecf20Sopenharmony_ci		}
28098c2ecf20Sopenharmony_ci		msleep(400);
28108c2ecf20Sopenharmony_ci	}
28118c2ecf20Sopenharmony_ci
28128c2ecf20Sopenharmony_ci	regmap_read(rt5640->regmap, RT5640_VENDOR_ID2, &val);
28138c2ecf20Sopenharmony_ci	if (val != RT5640_DEVICE_ID) {
28148c2ecf20Sopenharmony_ci		dev_err(&i2c->dev,
28158c2ecf20Sopenharmony_ci			"Device with ID register %#x is not rt5640/39\n", val);
28168c2ecf20Sopenharmony_ci		return -ENODEV;
28178c2ecf20Sopenharmony_ci	}
28188c2ecf20Sopenharmony_ci
28198c2ecf20Sopenharmony_ci	regmap_write(rt5640->regmap, RT5640_RESET, 0);
28208c2ecf20Sopenharmony_ci
28218c2ecf20Sopenharmony_ci	ret = regmap_register_patch(rt5640->regmap, init_list,
28228c2ecf20Sopenharmony_ci				    ARRAY_SIZE(init_list));
28238c2ecf20Sopenharmony_ci	if (ret != 0)
28248c2ecf20Sopenharmony_ci		dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
28258c2ecf20Sopenharmony_ci
28268c2ecf20Sopenharmony_ci	regmap_update_bits(rt5640->regmap, RT5640_DUMMY1,
28278c2ecf20Sopenharmony_ci				RT5640_MCLK_DET, RT5640_MCLK_DET);
28288c2ecf20Sopenharmony_ci
28298c2ecf20Sopenharmony_ci	rt5640->hp_mute = true;
28308c2ecf20Sopenharmony_ci	rt5640->irq = i2c->irq;
28318c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&rt5640->bp_work, rt5640_button_press_work);
28328c2ecf20Sopenharmony_ci	INIT_WORK(&rt5640->jack_work, rt5640_jack_work);
28338c2ecf20Sopenharmony_ci
28348c2ecf20Sopenharmony_ci	/* Make sure work is stopped on probe-error / remove */
28358c2ecf20Sopenharmony_ci	ret = devm_add_action_or_reset(&i2c->dev, rt5640_cancel_work, rt5640);
28368c2ecf20Sopenharmony_ci	if (ret)
28378c2ecf20Sopenharmony_ci		return ret;
28388c2ecf20Sopenharmony_ci
28398c2ecf20Sopenharmony_ci	ret = devm_request_irq(&i2c->dev, rt5640->irq, rt5640_irq,
28408c2ecf20Sopenharmony_ci			       IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
28418c2ecf20Sopenharmony_ci			       | IRQF_ONESHOT, "rt5640", rt5640);
28428c2ecf20Sopenharmony_ci	if (ret == 0) {
28438c2ecf20Sopenharmony_ci		/* Gets re-enabled by rt5640_set_jack() */
28448c2ecf20Sopenharmony_ci		disable_irq(rt5640->irq);
28458c2ecf20Sopenharmony_ci	} else {
28468c2ecf20Sopenharmony_ci		dev_warn(&i2c->dev, "Failed to reguest IRQ %d: %d\n",
28478c2ecf20Sopenharmony_ci			 rt5640->irq, ret);
28488c2ecf20Sopenharmony_ci		rt5640->irq = -ENXIO;
28498c2ecf20Sopenharmony_ci	}
28508c2ecf20Sopenharmony_ci
28518c2ecf20Sopenharmony_ci	return devm_snd_soc_register_component(&i2c->dev,
28528c2ecf20Sopenharmony_ci				      &soc_component_dev_rt5640,
28538c2ecf20Sopenharmony_ci				      rt5640_dai, ARRAY_SIZE(rt5640_dai));
28548c2ecf20Sopenharmony_ci}
28558c2ecf20Sopenharmony_ci
28568c2ecf20Sopenharmony_cistatic struct i2c_driver rt5640_i2c_driver = {
28578c2ecf20Sopenharmony_ci	.driver = {
28588c2ecf20Sopenharmony_ci		.name = "rt5640",
28598c2ecf20Sopenharmony_ci		.acpi_match_table = ACPI_PTR(rt5640_acpi_match),
28608c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(rt5640_of_match),
28618c2ecf20Sopenharmony_ci	},
28628c2ecf20Sopenharmony_ci	.probe = rt5640_i2c_probe,
28638c2ecf20Sopenharmony_ci	.id_table = rt5640_i2c_id,
28648c2ecf20Sopenharmony_ci};
28658c2ecf20Sopenharmony_cimodule_i2c_driver(rt5640_i2c_driver);
28668c2ecf20Sopenharmony_ci
28678c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ASoC RT5640/RT5639 driver");
28688c2ecf20Sopenharmony_ciMODULE_AUTHOR("Johnny Hsu <johnnyhsu@realtek.com>");
28698c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
2870