18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci// Copyright (c) 2017, Maxim Integrated
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/acpi.h>
58c2ecf20Sopenharmony_ci#include <linux/delay.h>
68c2ecf20Sopenharmony_ci#include <linux/i2c.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/regmap.h>
98c2ecf20Sopenharmony_ci#include <linux/slab.h>
108c2ecf20Sopenharmony_ci#include <linux/cdev.h>
118c2ecf20Sopenharmony_ci#include <sound/pcm.h>
128c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
138c2ecf20Sopenharmony_ci#include <sound/soc.h>
148c2ecf20Sopenharmony_ci#include <linux/gpio.h>
158c2ecf20Sopenharmony_ci#include <linux/of.h>
168c2ecf20Sopenharmony_ci#include <linux/of_gpio.h>
178c2ecf20Sopenharmony_ci#include <sound/tlv.h>
188c2ecf20Sopenharmony_ci#include "max98373.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic int max98373_dac_event(struct snd_soc_dapm_widget *w,
218c2ecf20Sopenharmony_ci	struct snd_kcontrol *kcontrol, int event)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
248c2ecf20Sopenharmony_ci	struct max98373_priv *max98373 = snd_soc_component_get_drvdata(component);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	switch (event) {
278c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_POST_PMU:
288c2ecf20Sopenharmony_ci		regmap_update_bits(max98373->regmap,
298c2ecf20Sopenharmony_ci			MAX98373_R20FF_GLOBAL_SHDN,
308c2ecf20Sopenharmony_ci			MAX98373_GLOBAL_EN_MASK, 1);
318c2ecf20Sopenharmony_ci		usleep_range(30000, 31000);
328c2ecf20Sopenharmony_ci		break;
338c2ecf20Sopenharmony_ci	case SND_SOC_DAPM_POST_PMD:
348c2ecf20Sopenharmony_ci		regmap_update_bits(max98373->regmap,
358c2ecf20Sopenharmony_ci			MAX98373_R20FF_GLOBAL_SHDN,
368c2ecf20Sopenharmony_ci			MAX98373_GLOBAL_EN_MASK, 0);
378c2ecf20Sopenharmony_ci		usleep_range(30000, 31000);
388c2ecf20Sopenharmony_ci		max98373->tdm_mode = false;
398c2ecf20Sopenharmony_ci		break;
408c2ecf20Sopenharmony_ci	default:
418c2ecf20Sopenharmony_ci		return 0;
428c2ecf20Sopenharmony_ci	}
438c2ecf20Sopenharmony_ci	return 0;
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic const char * const max98373_switch_text[] = {
478c2ecf20Sopenharmony_ci	"Left", "Right", "LeftRight"};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic const struct soc_enum dai_sel_enum =
508c2ecf20Sopenharmony_ci	SOC_ENUM_SINGLE(MAX98373_R2029_PCM_TO_SPK_MONO_MIX_1,
518c2ecf20Sopenharmony_ci		MAX98373_PCM_TO_SPK_MONOMIX_CFG_SHIFT,
528c2ecf20Sopenharmony_ci		3, max98373_switch_text);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max98373_dai_controls =
558c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("DAI Sel", dai_sel_enum);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max98373_vi_control =
588c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("Switch", MAX98373_R202C_PCM_TX_EN, 0, 1, 0);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max98373_spkfb_control =
618c2ecf20Sopenharmony_ci	SOC_DAPM_SINGLE("Switch", MAX98373_R2043_AMP_EN, 1, 1, 0);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget max98373_dapm_widgets[] = {
648c2ecf20Sopenharmony_ciSND_SOC_DAPM_DAC_E("Amp Enable", "HiFi Playback",
658c2ecf20Sopenharmony_ci	MAX98373_R202B_PCM_RX_EN, 0, 0, max98373_dac_event,
668c2ecf20Sopenharmony_ci	SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
678c2ecf20Sopenharmony_ciSND_SOC_DAPM_MUX("DAI Sel Mux", SND_SOC_NOPM, 0, 0,
688c2ecf20Sopenharmony_ci	&max98373_dai_controls),
698c2ecf20Sopenharmony_ciSND_SOC_DAPM_OUTPUT("BE_OUT"),
708c2ecf20Sopenharmony_ciSND_SOC_DAPM_AIF_OUT("Voltage Sense", "HiFi Capture", 0,
718c2ecf20Sopenharmony_ci	MAX98373_R2047_IV_SENSE_ADC_EN, 0, 0),
728c2ecf20Sopenharmony_ciSND_SOC_DAPM_AIF_OUT("Current Sense", "HiFi Capture", 0,
738c2ecf20Sopenharmony_ci	MAX98373_R2047_IV_SENSE_ADC_EN, 1, 0),
748c2ecf20Sopenharmony_ciSND_SOC_DAPM_AIF_OUT("Speaker FB Sense", "HiFi Capture", 0,
758c2ecf20Sopenharmony_ci	SND_SOC_NOPM, 0, 0),
768c2ecf20Sopenharmony_ciSND_SOC_DAPM_SWITCH("VI Sense", SND_SOC_NOPM, 0, 0,
778c2ecf20Sopenharmony_ci	&max98373_vi_control),
788c2ecf20Sopenharmony_ciSND_SOC_DAPM_SWITCH("SpkFB Sense", SND_SOC_NOPM, 0, 0,
798c2ecf20Sopenharmony_ci	&max98373_spkfb_control),
808c2ecf20Sopenharmony_ciSND_SOC_DAPM_SIGGEN("VMON"),
818c2ecf20Sopenharmony_ciSND_SOC_DAPM_SIGGEN("IMON"),
828c2ecf20Sopenharmony_ciSND_SOC_DAPM_SIGGEN("FBMON"),
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic DECLARE_TLV_DB_SCALE(max98373_digital_tlv, -6350, 50, 1);
868c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_RANGE(max98373_spk_tlv,
878c2ecf20Sopenharmony_ci	0, 8, TLV_DB_SCALE_ITEM(0, 50, 0),
888c2ecf20Sopenharmony_ci	9, 10, TLV_DB_SCALE_ITEM(500, 100, 0),
898c2ecf20Sopenharmony_ci);
908c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_RANGE(max98373_spkgain_max_tlv,
918c2ecf20Sopenharmony_ci	0, 9, TLV_DB_SCALE_ITEM(800, 100, 0),
928c2ecf20Sopenharmony_ci);
938c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_RANGE(max98373_dht_step_size_tlv,
948c2ecf20Sopenharmony_ci	0, 1, TLV_DB_SCALE_ITEM(25, 25, 0),
958c2ecf20Sopenharmony_ci	2, 4, TLV_DB_SCALE_ITEM(100, 100, 0),
968c2ecf20Sopenharmony_ci);
978c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_RANGE(max98373_dht_spkgain_min_tlv,
988c2ecf20Sopenharmony_ci	0, 9, TLV_DB_SCALE_ITEM(800, 100, 0),
998c2ecf20Sopenharmony_ci);
1008c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_RANGE(max98373_dht_rotation_point_tlv,
1018c2ecf20Sopenharmony_ci	0, 1, TLV_DB_SCALE_ITEM(-3000, 500, 0),
1028c2ecf20Sopenharmony_ci	2, 4, TLV_DB_SCALE_ITEM(-2200, 200, 0),
1038c2ecf20Sopenharmony_ci	5, 6, TLV_DB_SCALE_ITEM(-1500, 300, 0),
1048c2ecf20Sopenharmony_ci	7, 9, TLV_DB_SCALE_ITEM(-1000, 200, 0),
1058c2ecf20Sopenharmony_ci	10, 13, TLV_DB_SCALE_ITEM(-500, 100, 0),
1068c2ecf20Sopenharmony_ci	14, 15, TLV_DB_SCALE_ITEM(-100, 50, 0),
1078c2ecf20Sopenharmony_ci);
1088c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_RANGE(max98373_limiter_thresh_tlv,
1098c2ecf20Sopenharmony_ci	0, 15, TLV_DB_SCALE_ITEM(-1500, 100, 0),
1108c2ecf20Sopenharmony_ci);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_RANGE(max98373_bde_gain_tlv,
1138c2ecf20Sopenharmony_ci	0, 60, TLV_DB_SCALE_ITEM(-1500, 25, 0),
1148c2ecf20Sopenharmony_ci);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic const char * const max98373_output_voltage_lvl_text[] = {
1178c2ecf20Sopenharmony_ci	"5.43V", "6.09V", "6.83V", "7.67V", "8.60V",
1188c2ecf20Sopenharmony_ci	"9.65V", "10.83V", "12.15V", "13.63V", "15.29V"
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(max98373_out_volt_enum,
1228c2ecf20Sopenharmony_ci			    MAX98373_R203E_AMP_PATH_GAIN, 0,
1238c2ecf20Sopenharmony_ci			    max98373_output_voltage_lvl_text);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic const char * const max98373_dht_attack_rate_text[] = {
1268c2ecf20Sopenharmony_ci	"17.5us", "35us", "70us", "140us",
1278c2ecf20Sopenharmony_ci	"280us", "560us", "1120us", "2240us"
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(max98373_dht_attack_rate_enum,
1318c2ecf20Sopenharmony_ci			    MAX98373_R20D2_DHT_ATTACK_CFG, 0,
1328c2ecf20Sopenharmony_ci			    max98373_dht_attack_rate_text);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic const char * const max98373_dht_release_rate_text[] = {
1358c2ecf20Sopenharmony_ci	"45ms", "225ms", "450ms", "1150ms",
1368c2ecf20Sopenharmony_ci	"2250ms", "3100ms", "4500ms", "6750ms"
1378c2ecf20Sopenharmony_ci};
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(max98373_dht_release_rate_enum,
1408c2ecf20Sopenharmony_ci			    MAX98373_R20D3_DHT_RELEASE_CFG, 0,
1418c2ecf20Sopenharmony_ci			    max98373_dht_release_rate_text);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic const char * const max98373_limiter_attack_rate_text[] = {
1448c2ecf20Sopenharmony_ci	"10us", "20us", "40us", "80us",
1458c2ecf20Sopenharmony_ci	"160us", "320us", "640us", "1.28ms",
1468c2ecf20Sopenharmony_ci	"2.56ms", "5.12ms", "10.24ms", "20.48ms",
1478c2ecf20Sopenharmony_ci	"40.96ms", "81.92ms", "16.384ms", "32.768ms"
1488c2ecf20Sopenharmony_ci};
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(max98373_limiter_attack_rate_enum,
1518c2ecf20Sopenharmony_ci			    MAX98373_R20E1_LIMITER_ATK_REL_RATES, 4,
1528c2ecf20Sopenharmony_ci			    max98373_limiter_attack_rate_text);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic const char * const max98373_limiter_release_rate_text[] = {
1558c2ecf20Sopenharmony_ci	"40us", "80us", "160us", "320us",
1568c2ecf20Sopenharmony_ci	"640us", "1.28ms", "2.56ms", "5.120ms",
1578c2ecf20Sopenharmony_ci	"10.24ms", "20.48ms", "40.96ms", "81.92ms",
1588c2ecf20Sopenharmony_ci	"163.84ms", "327.68ms", "655.36ms", "1310.72ms"
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(max98373_limiter_release_rate_enum,
1628c2ecf20Sopenharmony_ci			    MAX98373_R20E1_LIMITER_ATK_REL_RATES, 0,
1638c2ecf20Sopenharmony_ci			    max98373_limiter_release_rate_text);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic const char * const max98373_ADC_samplerate_text[] = {
1668c2ecf20Sopenharmony_ci	"333kHz", "192kHz", "64kHz", "48kHz"
1678c2ecf20Sopenharmony_ci};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(max98373_adc_samplerate_enum,
1708c2ecf20Sopenharmony_ci			    MAX98373_R2051_MEAS_ADC_SAMPLING_RATE, 0,
1718c2ecf20Sopenharmony_ci			    max98373_ADC_samplerate_text);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max98373_snd_controls[] = {
1748c2ecf20Sopenharmony_ciSOC_SINGLE("Digital Vol Sel Switch", MAX98373_R203F_AMP_DSP_CFG,
1758c2ecf20Sopenharmony_ci	MAX98373_AMP_VOL_SEL_SHIFT, 1, 0),
1768c2ecf20Sopenharmony_ciSOC_SINGLE("Volume Location Switch", MAX98373_R203F_AMP_DSP_CFG,
1778c2ecf20Sopenharmony_ci	MAX98373_AMP_VOL_SEL_SHIFT, 1, 0),
1788c2ecf20Sopenharmony_ciSOC_SINGLE("Ramp Up Switch", MAX98373_R203F_AMP_DSP_CFG,
1798c2ecf20Sopenharmony_ci	MAX98373_AMP_DSP_CFG_RMP_UP_SHIFT, 1, 0),
1808c2ecf20Sopenharmony_ciSOC_SINGLE("Ramp Down Switch", MAX98373_R203F_AMP_DSP_CFG,
1818c2ecf20Sopenharmony_ci	MAX98373_AMP_DSP_CFG_RMP_DN_SHIFT, 1, 0),
1828c2ecf20Sopenharmony_ciSOC_SINGLE("CLK Monitor Switch", MAX98373_R20FE_DEVICE_AUTO_RESTART_CFG,
1838c2ecf20Sopenharmony_ci	MAX98373_CLOCK_MON_SHIFT, 1, 0),
1848c2ecf20Sopenharmony_ciSOC_SINGLE("Dither Switch", MAX98373_R203F_AMP_DSP_CFG,
1858c2ecf20Sopenharmony_ci	MAX98373_AMP_DSP_CFG_DITH_SHIFT, 1, 0),
1868c2ecf20Sopenharmony_ciSOC_SINGLE("DC Blocker Switch", MAX98373_R203F_AMP_DSP_CFG,
1878c2ecf20Sopenharmony_ci	MAX98373_AMP_DSP_CFG_DCBLK_SHIFT, 1, 0),
1888c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Digital Volume", MAX98373_R203D_AMP_DIG_VOL_CTRL,
1898c2ecf20Sopenharmony_ci	0, 0x7F, 1, max98373_digital_tlv),
1908c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Speaker Volume", MAX98373_R203E_AMP_PATH_GAIN,
1918c2ecf20Sopenharmony_ci	MAX98373_SPK_DIGI_GAIN_SHIFT, 10, 0, max98373_spk_tlv),
1928c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("FS Max Volume", MAX98373_R203E_AMP_PATH_GAIN,
1938c2ecf20Sopenharmony_ci	MAX98373_FS_GAIN_MAX_SHIFT, 9, 0, max98373_spkgain_max_tlv),
1948c2ecf20Sopenharmony_ciSOC_ENUM("Output Voltage", max98373_out_volt_enum),
1958c2ecf20Sopenharmony_ci/* Dynamic Headroom Tracking */
1968c2ecf20Sopenharmony_ciSOC_SINGLE("DHT Switch", MAX98373_R20D4_DHT_EN,
1978c2ecf20Sopenharmony_ci	MAX98373_DHT_EN_SHIFT, 1, 0),
1988c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("DHT Min Volume", MAX98373_R20D1_DHT_CFG,
1998c2ecf20Sopenharmony_ci	MAX98373_DHT_SPK_GAIN_MIN_SHIFT, 9, 0, max98373_dht_spkgain_min_tlv),
2008c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("DHT Rot Pnt Volume", MAX98373_R20D1_DHT_CFG,
2018c2ecf20Sopenharmony_ci	MAX98373_DHT_ROT_PNT_SHIFT, 15, 1, max98373_dht_rotation_point_tlv),
2028c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("DHT Attack Step Volume", MAX98373_R20D2_DHT_ATTACK_CFG,
2038c2ecf20Sopenharmony_ci	MAX98373_DHT_ATTACK_STEP_SHIFT, 4, 0, max98373_dht_step_size_tlv),
2048c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("DHT Release Step Volume", MAX98373_R20D3_DHT_RELEASE_CFG,
2058c2ecf20Sopenharmony_ci	MAX98373_DHT_RELEASE_STEP_SHIFT, 4, 0, max98373_dht_step_size_tlv),
2068c2ecf20Sopenharmony_ciSOC_ENUM("DHT Attack Rate", max98373_dht_attack_rate_enum),
2078c2ecf20Sopenharmony_ciSOC_ENUM("DHT Release Rate", max98373_dht_release_rate_enum),
2088c2ecf20Sopenharmony_ci/* ADC configuration */
2098c2ecf20Sopenharmony_ciSOC_SINGLE("ADC PVDD CH Switch", MAX98373_R2056_MEAS_ADC_PVDD_CH_EN, 0, 1, 0),
2108c2ecf20Sopenharmony_ciSOC_SINGLE("ADC PVDD FLT Switch", MAX98373_R2052_MEAS_ADC_PVDD_FLT_CFG,
2118c2ecf20Sopenharmony_ci	MAX98373_FLT_EN_SHIFT, 1, 0),
2128c2ecf20Sopenharmony_ciSOC_SINGLE("ADC TEMP FLT Switch", MAX98373_R2053_MEAS_ADC_THERM_FLT_CFG,
2138c2ecf20Sopenharmony_ci	MAX98373_FLT_EN_SHIFT, 1, 0),
2148c2ecf20Sopenharmony_ciSOC_SINGLE("ADC PVDD", MAX98373_R2054_MEAS_ADC_PVDD_CH_READBACK, 0, 0xFF, 0),
2158c2ecf20Sopenharmony_ciSOC_SINGLE("ADC TEMP", MAX98373_R2055_MEAS_ADC_THERM_CH_READBACK, 0, 0xFF, 0),
2168c2ecf20Sopenharmony_ciSOC_SINGLE("ADC PVDD FLT Coeff", MAX98373_R2052_MEAS_ADC_PVDD_FLT_CFG,
2178c2ecf20Sopenharmony_ci	0, 0x3, 0),
2188c2ecf20Sopenharmony_ciSOC_SINGLE("ADC TEMP FLT Coeff", MAX98373_R2053_MEAS_ADC_THERM_FLT_CFG,
2198c2ecf20Sopenharmony_ci	0, 0x3, 0),
2208c2ecf20Sopenharmony_ciSOC_ENUM("ADC SampleRate", max98373_adc_samplerate_enum),
2218c2ecf20Sopenharmony_ci/* Brownout Detection Engine */
2228c2ecf20Sopenharmony_ciSOC_SINGLE("BDE Switch", MAX98373_R20B5_BDE_EN, MAX98373_BDE_EN_SHIFT, 1, 0),
2238c2ecf20Sopenharmony_ciSOC_SINGLE("BDE LVL4 Mute Switch", MAX98373_R20B2_BDE_L4_CFG_2,
2248c2ecf20Sopenharmony_ci	MAX98373_LVL4_MUTE_EN_SHIFT, 1, 0),
2258c2ecf20Sopenharmony_ciSOC_SINGLE("BDE LVL4 Hold Switch", MAX98373_R20B2_BDE_L4_CFG_2,
2268c2ecf20Sopenharmony_ci	MAX98373_LVL4_HOLD_EN_SHIFT, 1, 0),
2278c2ecf20Sopenharmony_ciSOC_SINGLE("BDE LVL1 Thresh", MAX98373_R2097_BDE_L1_THRESH, 0, 0xFF, 0),
2288c2ecf20Sopenharmony_ciSOC_SINGLE("BDE LVL2 Thresh", MAX98373_R2098_BDE_L2_THRESH, 0, 0xFF, 0),
2298c2ecf20Sopenharmony_ciSOC_SINGLE("BDE LVL3 Thresh", MAX98373_R2099_BDE_L3_THRESH, 0, 0xFF, 0),
2308c2ecf20Sopenharmony_ciSOC_SINGLE("BDE LVL4 Thresh", MAX98373_R209A_BDE_L4_THRESH, 0, 0xFF, 0),
2318c2ecf20Sopenharmony_ciSOC_SINGLE("BDE Active Level", MAX98373_R20B6_BDE_CUR_STATE_READBACK, 0, 8, 0),
2328c2ecf20Sopenharmony_ciSOC_SINGLE("BDE Clip Mode Switch", MAX98373_R2092_BDE_CLIPPER_MODE, 0, 1, 0),
2338c2ecf20Sopenharmony_ciSOC_SINGLE("BDE Thresh Hysteresis", MAX98373_R209B_BDE_THRESH_HYST, 0, 0xFF, 0),
2348c2ecf20Sopenharmony_ciSOC_SINGLE("BDE Hold Time", MAX98373_R2090_BDE_LVL_HOLD, 0, 0xFF, 0),
2358c2ecf20Sopenharmony_ciSOC_SINGLE("BDE Attack Rate", MAX98373_R2091_BDE_GAIN_ATK_REL_RATE, 4, 0xF, 0),
2368c2ecf20Sopenharmony_ciSOC_SINGLE("BDE Release Rate", MAX98373_R2091_BDE_GAIN_ATK_REL_RATE, 0, 0xF, 0),
2378c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL1 Clip Thresh Volume", MAX98373_R20A9_BDE_L1_CFG_2,
2388c2ecf20Sopenharmony_ci	0, 0x3C, 1, max98373_bde_gain_tlv),
2398c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL2 Clip Thresh Volume", MAX98373_R20AC_BDE_L2_CFG_2,
2408c2ecf20Sopenharmony_ci	0, 0x3C, 1, max98373_bde_gain_tlv),
2418c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL3 Clip Thresh Volume", MAX98373_R20AF_BDE_L3_CFG_2,
2428c2ecf20Sopenharmony_ci	0, 0x3C, 1, max98373_bde_gain_tlv),
2438c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL4 Clip Thresh Volume", MAX98373_R20B2_BDE_L4_CFG_2,
2448c2ecf20Sopenharmony_ci	0, 0x3C, 1, max98373_bde_gain_tlv),
2458c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL1 Clip Reduction Volume", MAX98373_R20AA_BDE_L1_CFG_3,
2468c2ecf20Sopenharmony_ci	0, 0x3C, 1, max98373_bde_gain_tlv),
2478c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL2 Clip Reduction Volume", MAX98373_R20AD_BDE_L2_CFG_3,
2488c2ecf20Sopenharmony_ci	0, 0x3C, 1, max98373_bde_gain_tlv),
2498c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL3 Clip Reduction Volume", MAX98373_R20B0_BDE_L3_CFG_3,
2508c2ecf20Sopenharmony_ci	0, 0x3C, 1, max98373_bde_gain_tlv),
2518c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL4 Clip Reduction Volume", MAX98373_R20B3_BDE_L4_CFG_3,
2528c2ecf20Sopenharmony_ci	0, 0x3C, 1, max98373_bde_gain_tlv),
2538c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL1 Limiter Thresh Volume", MAX98373_R20A8_BDE_L1_CFG_1,
2548c2ecf20Sopenharmony_ci	0, 0xF, 1, max98373_limiter_thresh_tlv),
2558c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL2 Limiter Thresh Volume", MAX98373_R20AB_BDE_L2_CFG_1,
2568c2ecf20Sopenharmony_ci	0, 0xF, 1, max98373_limiter_thresh_tlv),
2578c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL3 Limiter Thresh Volume", MAX98373_R20AE_BDE_L3_CFG_1,
2588c2ecf20Sopenharmony_ci	0, 0xF, 1, max98373_limiter_thresh_tlv),
2598c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("BDE LVL4 Limiter Thresh Volume", MAX98373_R20B1_BDE_L4_CFG_1,
2608c2ecf20Sopenharmony_ci	0, 0xF, 1, max98373_limiter_thresh_tlv),
2618c2ecf20Sopenharmony_ci/* Limiter */
2628c2ecf20Sopenharmony_ciSOC_SINGLE("Limiter Switch", MAX98373_R20E2_LIMITER_EN,
2638c2ecf20Sopenharmony_ci	MAX98373_LIMITER_EN_SHIFT, 1, 0),
2648c2ecf20Sopenharmony_ciSOC_SINGLE("Limiter Src Switch", MAX98373_R20E0_LIMITER_THRESH_CFG,
2658c2ecf20Sopenharmony_ci	MAX98373_LIMITER_THRESH_SRC_SHIFT, 1, 0),
2668c2ecf20Sopenharmony_ciSOC_SINGLE_TLV("Limiter Thresh Volume", MAX98373_R20E0_LIMITER_THRESH_CFG,
2678c2ecf20Sopenharmony_ci	MAX98373_LIMITER_THRESH_SHIFT, 15, 0, max98373_limiter_thresh_tlv),
2688c2ecf20Sopenharmony_ciSOC_ENUM("Limiter Attack Rate", max98373_limiter_attack_rate_enum),
2698c2ecf20Sopenharmony_ciSOC_ENUM("Limiter Release Rate", max98373_limiter_release_rate_enum),
2708c2ecf20Sopenharmony_ci};
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route max98373_audio_map[] = {
2738c2ecf20Sopenharmony_ci	/* Plabyack */
2748c2ecf20Sopenharmony_ci	{"DAI Sel Mux", "Left", "Amp Enable"},
2758c2ecf20Sopenharmony_ci	{"DAI Sel Mux", "Right", "Amp Enable"},
2768c2ecf20Sopenharmony_ci	{"DAI Sel Mux", "LeftRight", "Amp Enable"},
2778c2ecf20Sopenharmony_ci	{"BE_OUT", NULL, "DAI Sel Mux"},
2788c2ecf20Sopenharmony_ci	/* Capture */
2798c2ecf20Sopenharmony_ci	{ "VI Sense", "Switch", "VMON" },
2808c2ecf20Sopenharmony_ci	{ "VI Sense", "Switch", "IMON" },
2818c2ecf20Sopenharmony_ci	{ "SpkFB Sense", "Switch", "FBMON" },
2828c2ecf20Sopenharmony_ci	{ "Voltage Sense", NULL, "VI Sense" },
2838c2ecf20Sopenharmony_ci	{ "Current Sense", NULL, "VI Sense" },
2848c2ecf20Sopenharmony_ci	{ "Speaker FB Sense", NULL, "SpkFB Sense" },
2858c2ecf20Sopenharmony_ci};
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_civoid max98373_reset(struct max98373_priv *max98373, struct device *dev)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	int ret, reg, count;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* Software Reset */
2928c2ecf20Sopenharmony_ci	ret = regmap_update_bits(max98373->regmap,
2938c2ecf20Sopenharmony_ci		MAX98373_R2000_SW_RESET,
2948c2ecf20Sopenharmony_ci		MAX98373_SOFT_RESET,
2958c2ecf20Sopenharmony_ci		MAX98373_SOFT_RESET);
2968c2ecf20Sopenharmony_ci	if (ret)
2978c2ecf20Sopenharmony_ci		dev_err(dev, "Reset command failed. (ret:%d)\n", ret);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	count = 0;
3008c2ecf20Sopenharmony_ci	while (count < 3) {
3018c2ecf20Sopenharmony_ci		usleep_range(10000, 11000);
3028c2ecf20Sopenharmony_ci		/* Software Reset Verification */
3038c2ecf20Sopenharmony_ci		ret = regmap_read(max98373->regmap,
3048c2ecf20Sopenharmony_ci			MAX98373_R21FF_REV_ID, &reg);
3058c2ecf20Sopenharmony_ci		if (!ret) {
3068c2ecf20Sopenharmony_ci			dev_info(dev, "Reset completed (retry:%d)\n", count);
3078c2ecf20Sopenharmony_ci			return;
3088c2ecf20Sopenharmony_ci		}
3098c2ecf20Sopenharmony_ci		count++;
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci	dev_err(dev, "Reset failed. (ret:%d)\n", ret);
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(max98373_reset);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic int max98373_probe(struct snd_soc_component *component)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	struct max98373_priv *max98373 = snd_soc_component_get_drvdata(component);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/* Software Reset */
3208c2ecf20Sopenharmony_ci	max98373_reset(max98373, component->dev);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	/* IV default slot configuration */
3238c2ecf20Sopenharmony_ci	regmap_write(max98373->regmap,
3248c2ecf20Sopenharmony_ci		MAX98373_R2020_PCM_TX_HIZ_EN_1,
3258c2ecf20Sopenharmony_ci		0xFF);
3268c2ecf20Sopenharmony_ci	regmap_write(max98373->regmap,
3278c2ecf20Sopenharmony_ci		MAX98373_R2021_PCM_TX_HIZ_EN_2,
3288c2ecf20Sopenharmony_ci		0xFF);
3298c2ecf20Sopenharmony_ci	/* L/R mix configuration */
3308c2ecf20Sopenharmony_ci	regmap_write(max98373->regmap,
3318c2ecf20Sopenharmony_ci		MAX98373_R2029_PCM_TO_SPK_MONO_MIX_1,
3328c2ecf20Sopenharmony_ci		0x80);
3338c2ecf20Sopenharmony_ci	regmap_write(max98373->regmap,
3348c2ecf20Sopenharmony_ci		MAX98373_R202A_PCM_TO_SPK_MONO_MIX_2,
3358c2ecf20Sopenharmony_ci		0x1);
3368c2ecf20Sopenharmony_ci	/* Enable DC blocker */
3378c2ecf20Sopenharmony_ci	regmap_write(max98373->regmap,
3388c2ecf20Sopenharmony_ci		MAX98373_R203F_AMP_DSP_CFG,
3398c2ecf20Sopenharmony_ci		0x3);
3408c2ecf20Sopenharmony_ci	/* Enable IMON VMON DC blocker */
3418c2ecf20Sopenharmony_ci	regmap_write(max98373->regmap,
3428c2ecf20Sopenharmony_ci		MAX98373_R2046_IV_SENSE_ADC_DSP_CFG,
3438c2ecf20Sopenharmony_ci		0x7);
3448c2ecf20Sopenharmony_ci	/* voltage, current slot configuration */
3458c2ecf20Sopenharmony_ci	regmap_write(max98373->regmap,
3468c2ecf20Sopenharmony_ci		MAX98373_R2022_PCM_TX_SRC_1,
3478c2ecf20Sopenharmony_ci		(max98373->i_slot << MAX98373_PCM_TX_CH_SRC_A_I_SHIFT |
3488c2ecf20Sopenharmony_ci		max98373->v_slot) & 0xFF);
3498c2ecf20Sopenharmony_ci	if (max98373->v_slot < 8)
3508c2ecf20Sopenharmony_ci		regmap_update_bits(max98373->regmap,
3518c2ecf20Sopenharmony_ci			MAX98373_R2020_PCM_TX_HIZ_EN_1,
3528c2ecf20Sopenharmony_ci			1 << max98373->v_slot, 0);
3538c2ecf20Sopenharmony_ci	else
3548c2ecf20Sopenharmony_ci		regmap_update_bits(max98373->regmap,
3558c2ecf20Sopenharmony_ci			MAX98373_R2021_PCM_TX_HIZ_EN_2,
3568c2ecf20Sopenharmony_ci			1 << (max98373->v_slot - 8), 0);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	if (max98373->i_slot < 8)
3598c2ecf20Sopenharmony_ci		regmap_update_bits(max98373->regmap,
3608c2ecf20Sopenharmony_ci			MAX98373_R2020_PCM_TX_HIZ_EN_1,
3618c2ecf20Sopenharmony_ci			1 << max98373->i_slot, 0);
3628c2ecf20Sopenharmony_ci	else
3638c2ecf20Sopenharmony_ci		regmap_update_bits(max98373->regmap,
3648c2ecf20Sopenharmony_ci			MAX98373_R2021_PCM_TX_HIZ_EN_2,
3658c2ecf20Sopenharmony_ci			1 << (max98373->i_slot - 8), 0);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	/* speaker feedback slot configuration */
3688c2ecf20Sopenharmony_ci	regmap_write(max98373->regmap,
3698c2ecf20Sopenharmony_ci		MAX98373_R2023_PCM_TX_SRC_2,
3708c2ecf20Sopenharmony_ci		max98373->spkfb_slot & 0xFF);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	/* Set interleave mode */
3738c2ecf20Sopenharmony_ci	if (max98373->interleave_mode)
3748c2ecf20Sopenharmony_ci		regmap_update_bits(max98373->regmap,
3758c2ecf20Sopenharmony_ci			MAX98373_R2024_PCM_DATA_FMT_CFG,
3768c2ecf20Sopenharmony_ci			MAX98373_PCM_TX_CH_INTERLEAVE_MASK,
3778c2ecf20Sopenharmony_ci			MAX98373_PCM_TX_CH_INTERLEAVE_MASK);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/* Speaker enable */
3808c2ecf20Sopenharmony_ci	regmap_update_bits(max98373->regmap,
3818c2ecf20Sopenharmony_ci		MAX98373_R2043_AMP_EN,
3828c2ecf20Sopenharmony_ci		MAX98373_SPK_EN_MASK, 1);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	return 0;
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ciconst struct snd_soc_component_driver soc_codec_dev_max98373 = {
3888c2ecf20Sopenharmony_ci	.probe			= max98373_probe,
3898c2ecf20Sopenharmony_ci	.controls		= max98373_snd_controls,
3908c2ecf20Sopenharmony_ci	.num_controls		= ARRAY_SIZE(max98373_snd_controls),
3918c2ecf20Sopenharmony_ci	.dapm_widgets		= max98373_dapm_widgets,
3928c2ecf20Sopenharmony_ci	.num_dapm_widgets	= ARRAY_SIZE(max98373_dapm_widgets),
3938c2ecf20Sopenharmony_ci	.dapm_routes		= max98373_audio_map,
3948c2ecf20Sopenharmony_ci	.num_dapm_routes	= ARRAY_SIZE(max98373_audio_map),
3958c2ecf20Sopenharmony_ci	.use_pmdown_time	= 1,
3968c2ecf20Sopenharmony_ci	.endianness		= 1,
3978c2ecf20Sopenharmony_ci	.non_legacy_dai_naming	= 1,
3988c2ecf20Sopenharmony_ci};
3998c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(soc_codec_dev_max98373);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ciconst struct snd_soc_component_driver soc_codec_dev_max98373_sdw = {
4028c2ecf20Sopenharmony_ci	.probe			= NULL,
4038c2ecf20Sopenharmony_ci	.controls		= max98373_snd_controls,
4048c2ecf20Sopenharmony_ci	.num_controls		= ARRAY_SIZE(max98373_snd_controls),
4058c2ecf20Sopenharmony_ci	.dapm_widgets		= max98373_dapm_widgets,
4068c2ecf20Sopenharmony_ci	.num_dapm_widgets	= ARRAY_SIZE(max98373_dapm_widgets),
4078c2ecf20Sopenharmony_ci	.dapm_routes		= max98373_audio_map,
4088c2ecf20Sopenharmony_ci	.num_dapm_routes	= ARRAY_SIZE(max98373_audio_map),
4098c2ecf20Sopenharmony_ci	.use_pmdown_time	= 1,
4108c2ecf20Sopenharmony_ci	.endianness		= 1,
4118c2ecf20Sopenharmony_ci	.non_legacy_dai_naming	= 1,
4128c2ecf20Sopenharmony_ci};
4138c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(soc_codec_dev_max98373_sdw);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_civoid max98373_slot_config(struct device *dev,
4168c2ecf20Sopenharmony_ci			  struct max98373_priv *max98373)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	int value;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	if (!device_property_read_u32(dev, "maxim,vmon-slot-no", &value))
4218c2ecf20Sopenharmony_ci		max98373->v_slot = value & 0xF;
4228c2ecf20Sopenharmony_ci	else
4238c2ecf20Sopenharmony_ci		max98373->v_slot = 0;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	if (!device_property_read_u32(dev, "maxim,imon-slot-no", &value))
4268c2ecf20Sopenharmony_ci		max98373->i_slot = value & 0xF;
4278c2ecf20Sopenharmony_ci	else
4288c2ecf20Sopenharmony_ci		max98373->i_slot = 1;
4298c2ecf20Sopenharmony_ci	if (dev->of_node) {
4308c2ecf20Sopenharmony_ci		max98373->reset_gpio = of_get_named_gpio(dev->of_node,
4318c2ecf20Sopenharmony_ci						"maxim,reset-gpio", 0);
4328c2ecf20Sopenharmony_ci		if (!gpio_is_valid(max98373->reset_gpio)) {
4338c2ecf20Sopenharmony_ci			dev_err(dev, "Looking up %s property in node %s failed %d\n",
4348c2ecf20Sopenharmony_ci				"maxim,reset-gpio", dev->of_node->full_name,
4358c2ecf20Sopenharmony_ci				max98373->reset_gpio);
4368c2ecf20Sopenharmony_ci		} else {
4378c2ecf20Sopenharmony_ci			dev_dbg(dev, "maxim,reset-gpio=%d",
4388c2ecf20Sopenharmony_ci				max98373->reset_gpio);
4398c2ecf20Sopenharmony_ci		}
4408c2ecf20Sopenharmony_ci	} else {
4418c2ecf20Sopenharmony_ci		/* this makes reset_gpio as invalid */
4428c2ecf20Sopenharmony_ci		max98373->reset_gpio = -1;
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	if (!device_property_read_u32(dev, "maxim,spkfb-slot-no", &value))
4468c2ecf20Sopenharmony_ci		max98373->spkfb_slot = value & 0xF;
4478c2ecf20Sopenharmony_ci	else
4488c2ecf20Sopenharmony_ci		max98373->spkfb_slot = 2;
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(max98373_slot_config);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ALSA SoC MAX98373 driver");
4538c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ryan Lee <ryans.lee@maximintegrated.com>");
4548c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
455