// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2022, Analog Devices Inc.

#include <linux/acpi.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/tlv.h>
#include "max98388.h"

static struct reg_default max98388_reg[] = {
	{MAX98388_R2000_SW_RESET, 0x00},
	{MAX98388_R2001_INT_RAW1, 0x00},
	{MAX98388_R2002_INT_RAW2, 0x00},
	{MAX98388_R2004_INT_STATE1, 0x00},
	{MAX98388_R2005_INT_STATE2, 0x00},
	{MAX98388_R2020_THERM_WARN_THRESH, 0x0A},
	{MAX98388_R2031_SPK_MON_THRESH, 0x58},
	{MAX98388_R2032_SPK_MON_LD_SEL, 0x08},
	{MAX98388_R2033_SPK_MON_DURATION, 0x02},
	{MAX98388_R2037_ERR_MON_CTRL, 0x01},
	{MAX98388_R2040_PCM_MODE_CFG, 0xC0},
	{MAX98388_R2041_PCM_CLK_SETUP, 0x04},
	{MAX98388_R2042_PCM_SR_SETUP, 0x88},
	{MAX98388_R2044_PCM_TX_CTRL1, 0x00},
	{MAX98388_R2045_PCM_TX_CTRL2, 0x00},
	{MAX98388_R2050_PCM_TX_HIZ_CTRL1, 0xFF},
	{MAX98388_R2051_PCM_TX_HIZ_CTRL2, 0xFF},
	{MAX98388_R2052_PCM_TX_HIZ_CTRL3, 0xFF},
	{MAX98388_R2053_PCM_TX_HIZ_CTRL4, 0xFF},
	{MAX98388_R2054_PCM_TX_HIZ_CTRL5, 0xFF},
	{MAX98388_R2055_PCM_TX_HIZ_CTRL6, 0xFF},
	{MAX98388_R2056_PCM_TX_HIZ_CTRL7, 0xFF},
	{MAX98388_R2057_PCM_TX_HIZ_CTRL8, 0xFF},
	{MAX98388_R2058_PCM_RX_SRC1, 0x00},
	{MAX98388_R2059_PCM_RX_SRC2, 0x01},
	{MAX98388_R205C_PCM_TX_DRIVE_STRENGTH, 0x00},
	{MAX98388_R205D_PCM_TX_SRC_EN, 0x00},
	{MAX98388_R205E_PCM_RX_EN, 0x00},
	{MAX98388_R205F_PCM_TX_EN, 0x00},
	{MAX98388_R2090_SPK_CH_VOL_CTRL, 0x00},
	{MAX98388_R2091_SPK_CH_CFG, 0x02},
	{MAX98388_R2092_SPK_AMP_OUT_CFG, 0x03},
	{MAX98388_R2093_SPK_AMP_SSM_CFG, 0x01},
	{MAX98388_R2094_SPK_AMP_ER_CTRL, 0x00},
	{MAX98388_R209E_SPK_CH_PINK_NOISE_EN, 0x00},
	{MAX98388_R209F_SPK_CH_AMP_EN, 0x00},
	{MAX98388_R20A0_IV_DATA_DSP_CTRL, 0x10},
	{MAX98388_R20A7_IV_DATA_EN, 0x00},
	{MAX98388_R20E0_BP_ALC_THRESH, 0x04},
	{MAX98388_R20E1_BP_ALC_RATES, 0x20},
	{MAX98388_R20E2_BP_ALC_ATTEN, 0x06},
	{MAX98388_R20E3_BP_ALC_REL, 0x02},
	{MAX98388_R20E4_BP_ALC_MUTE, 0x33},
	{MAX98388_R20EE_BP_INF_HOLD_REL, 0x00},
	{MAX98388_R20EF_BP_ALC_EN, 0x00},
	{MAX98388_R210E_AUTO_RESTART, 0x00},
	{MAX98388_R210F_GLOBAL_EN, 0x00},
	{MAX98388_R22FF_REV_ID, 0x00},
};

static int max98388_dac_event(struct snd_soc_dapm_widget *w,
			      struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
	struct max98388_priv *max98388 = snd_soc_component_get_drvdata(component);

	switch (event) {
	case SND_SOC_DAPM_POST_PMU:
		regmap_write(max98388->regmap,
			     MAX98388_R210F_GLOBAL_EN, 1);
		usleep_range(30000, 31000);
		break;
	case SND_SOC_DAPM_PRE_PMD:
		regmap_write(max98388->regmap,
			     MAX98388_R210F_GLOBAL_EN, 0);
		usleep_range(30000, 31000);
		max98388->tdm_mode = false;
		break;
	default:
		return 0;
	}
	return 0;
}

static const char * const max98388_monomix_switch_text[] = {
	"Left", "Right", "LeftRight"};

static const struct soc_enum dai_sel_enum =
	SOC_ENUM_SINGLE(MAX98388_R2058_PCM_RX_SRC1,
			MAX98388_PCM_TO_SPK_MONOMIX_CFG_SHIFT,
			3, max98388_monomix_switch_text);

static const struct snd_kcontrol_new max98388_dai_controls =
	SOC_DAPM_ENUM("DAI Sel", dai_sel_enum);

static const struct snd_kcontrol_new max98388_vi_control =
	SOC_DAPM_SINGLE("Switch", MAX98388_R205F_PCM_TX_EN, 0, 1, 0);

static const struct snd_soc_dapm_widget max98388_dapm_widgets[] = {
	SND_SOC_DAPM_DAC_E("Amp Enable", "HiFi Playback",
			   MAX98388_R205E_PCM_RX_EN, 0, 0, max98388_dac_event,
			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_MUX("DAI Sel Mux", SND_SOC_NOPM, 0, 0,
			 &max98388_dai_controls),
	SND_SOC_DAPM_OUTPUT("BE_OUT"),
	SND_SOC_DAPM_AIF_OUT("Voltage Sense", "HiFi Capture", 0,
			     MAX98388_R20A7_IV_DATA_EN, 0, 0),
	SND_SOC_DAPM_AIF_OUT("Current Sense", "HiFi Capture", 0,
			     MAX98388_R20A7_IV_DATA_EN, 1, 0),
	SND_SOC_DAPM_ADC("ADC Voltage", NULL,
			 MAX98388_R205D_PCM_TX_SRC_EN, 0, 0),
	SND_SOC_DAPM_ADC("ADC Current", NULL,
			 MAX98388_R205D_PCM_TX_SRC_EN, 1, 0),
	SND_SOC_DAPM_SWITCH("VI Sense", SND_SOC_NOPM, 0, 0,
			    &max98388_vi_control),
	SND_SOC_DAPM_SIGGEN("VMON"),
	SND_SOC_DAPM_SIGGEN("IMON"),
};

static DECLARE_TLV_DB_SCALE(max98388_digital_tlv, -6350, 50, 1);
static DECLARE_TLV_DB_SCALE(max98388_amp_gain_tlv, -300, 300, 0);

static const char * const max98388_alc_max_atten_text[] = {
	"0dBFS", "-1dBFS", "-2dBFS", "-3dBFS", "-4dBFS", "-5dBFS",
	"-6dBFS", "-7dBFS", "-8dBFS", "-9dBFS", "-10dBFS", "-11dBFS",
	"-12dBFS", "-13dBFS", "-14dBFS", "-15dBFS"
};

static SOC_ENUM_SINGLE_DECL(max98388_alc_max_atten_enum,
			    MAX98388_R20E2_BP_ALC_ATTEN,
			    MAX98388_ALC_MAX_ATTEN_SHIFT,
			    max98388_alc_max_atten_text);

static const char * const max98388_thermal_warn_text[] = {
	"95C", "105C", "115C", "125C"
};

static SOC_ENUM_SINGLE_DECL(max98388_thermal_warning_thresh_enum,
			    MAX98388_R2020_THERM_WARN_THRESH,
			    MAX98388_THERM_WARN_THRESH_SHIFT,
			    max98388_thermal_warn_text);

static const char * const max98388_thermal_shutdown_text[] = {
	"135C", "145C", "155C", "165C"
};

static SOC_ENUM_SINGLE_DECL(max98388_thermal_shutdown_thresh_enum,
			    MAX98388_R2020_THERM_WARN_THRESH,
			    MAX98388_THERM_SHDN_THRESH_SHIFT,
			    max98388_thermal_shutdown_text);

static const char * const max98388_alc_thresh_single_text[] = {
	"3.625V", "3.550V", "3.475V", "3.400V", "3.325V", "3.250V",
	"3.175V", "3.100V", "3.025V", "2.950V", "2.875V", "2.800V",
	"2.725V", "2.650V", "2.575V", "2.500V"
};

static SOC_ENUM_SINGLE_DECL(max98388_alc_thresh_single_enum,
			    MAX98388_R20E0_BP_ALC_THRESH,
			    MAX98388_ALC_THRESH_SHIFT,
			    max98388_alc_thresh_single_text);

static const char * const max98388_alc_attack_rate_text[] = {
	"0", "10us", "20us", "40us", "80us", "160us",
	"320us", "640us", "1.28ms", "2.56ms", "5.12ms", "10.24ms",
	"20.48ms", "40.96ms", "81.92ms", "163.84ms"
};

static SOC_ENUM_SINGLE_DECL(max98388_alc_attack_rate_enum,
			    MAX98388_R20E1_BP_ALC_RATES,
			    MAX98388_ALC_ATTACK_RATE_SHIFT,
			    max98388_alc_attack_rate_text);

static const char * const max98388_alc_release_rate_text[] = {
	"20us", "40us", "80us", "160us", "320us", "640us",
	"1.28ms", "2.56ms", "5.12ms", "10.24ms", "20.48ms", "40.96ms",
	"81.92ms", "163.84ms", "327.68ms", "655.36ms"
};

static SOC_ENUM_SINGLE_DECL(max98388_alc_release_rate_enum,
			    MAX98388_R20E1_BP_ALC_RATES,
			    MAX98388_ALC_RELEASE_RATE_SHIFT,
			    max98388_alc_release_rate_text);

static const char * const max98388_alc_debounce_text[] = {
	"0.01ms", "0.1ms", "1ms", "10ms", "100ms", "250ms", "500ms", "hold"
};

static SOC_ENUM_SINGLE_DECL(max98388_alc_debouce_enum,
			    MAX98388_R20E3_BP_ALC_REL,
			    MAX98388_ALC_DEBOUNCE_TIME_SHIFT,
			    max98388_alc_debounce_text);

static const char * const max98388_alc_mute_delay_text[] = {
	"0.01ms", "0.05ms", "0.1ms", "0.5ms", "1ms", "5ms", "25ms", "250ms"
};

static SOC_ENUM_SINGLE_DECL(max98388_alc_mute_delay_enum,
			    MAX98388_R20E4_BP_ALC_MUTE,
			    MAX98388_ALC_MUTE_DELAY_SHIFT,
			    max98388_alc_mute_delay_text);

static const char * const max98388_spkmon_duration_text[] = {
	"10ms", "25ms", "50ms", "75ms", "100ms", "200ms", "300ms", "400ms",
	"500ms", "600ms", "700ms", "800ms", "900ms", "1000ms", "1100ms", "1200ms"
};

static SOC_ENUM_SINGLE_DECL(max98388_spkmon_duration_enum,
			    MAX98388_R2033_SPK_MON_DURATION,
			    MAX98388_SPKMON_DURATION_SHIFT,
			    max98388_spkmon_duration_text);

static const char * const max98388_spkmon_thresh_text[] = {
	"0.03V", "0.06V", "0.09V", "0.12V", "0.15V", "0.18V", "0.20V", "0.23V",
	"0.26V", "0.29V", "0.32V", "0.35V", "0.38V", "0.41V", "0.44V", "0.47V",
	"0.50V", "0.53V", "0.56V", "0.58V", "0.61V", "0.64V", "0.67V", "0.70V",
	"0.73V", "0.76V", "0.79V", "0.82V", "0.85V", "0.88V", "0.91V", "0.94V",
	"0.96V", "0.99V", "1.02V", "1.05V", "1.08V", "1.11V", "1.14V", "1.17V",
	"1.20V", "1.23V", "1.26V", "1.29V", "1.32V", "1.35V", "1.37V", "1.40V",
	"1.43V", "1.46V", "1.49V", "1.52V", "1.55V", "1.58V", "1.61V", "1.64V",
	"1.67V", "1.70V", "1.73V", "1.75V", "1.78V", "1.81V", "1.84V", "1.87V",
	"1.90V", "1.93V", "1.96V", "1.99V", "2.02V", "2.05V", "2.08V", "2.11V",
	"2.13V", "2.16V", "2.19V", "2.22V", "2.25V", "2.28V", "2.31V", "2.34V",
	"2.37V", "2.40V", "2.43V", "2.46V", "2.49V", "2.51V", "2.54V", "2.57V",
	"2.60V", "2.63V", "2.66V", "2.69V", "2.72V", "2.75V", "2.78V", "2.81V",
	"2.84V", "2.87V", "2.89V", "2.92V", "2.95V", "2.98V", "3.01V", "3.04V",
	"3.07V", "3.10V", "3.13V", "3.16V", "3.19V", "3.22V", "3.25V", "3.27V",
	"3.30V", "3.33V", "3.36V", "3.39V", "3.42V", "3.45V", "3.48V", "3.51V",
	"3.54V", "3.57V", "3.60V", "3.63V", "3.66V", "3.68V", "3.71V", "3.74V"
};

static SOC_ENUM_SINGLE_DECL(max98388_spkmon_thresh_enum,
			    MAX98388_R2031_SPK_MON_THRESH,
			    MAX98388_SPKMON_THRESH_SHIFT,
			    max98388_spkmon_thresh_text);

static const char * const max98388_spkmon_load_text[] = {
	"2.00ohm", "2.25ohm", "2.50ohm", "2.75ohm", "3.00ohm", "3.25ohm",
	"3.50ohm", "3.75ohm", "4.00ohm", "4.25ohm", "4.50ohm", "4.75ohm",
	"5.00ohm", "5.25ohm", "5.50ohm", "5.75ohm", "6.00ohm", "6.25ohm",
	"6.50ohm", "6.75ohm", "7.00ohm", "7.25ohm", "7.50ohm", "7.75ohm",
	"8.00ohm", "8.25ohm", "8.50ohm", "8.75ohm", "9.00ohm", "9.25ohm",
	"9.50ohm", "9.75ohm", "10.00ohm", "10.25ohm", "10.50ohm", "10.75ohm",
	"11.00ohm", "11.25ohm", "11.50ohm", "11.75ohm",	"12.00ohm", "12.25ohm",
	"12.50ohm", "12.75ohm", "13.00ohm", "13.25ohm", "13.50ohm", "13.75ohm",
	"14.00ohm", "14.25ohm", "14.50ohm", "14.75ohm", "15.00ohm", "15.25ohm",
	"15.50ohm", "15.75ohm", "16.00ohm", "16.25ohm", "16.50ohm", "16.75ohm",
	"17.00ohm", "17.25ohm", "17.50ohm", "17.75ohm", "18.00ohm", "18.25ohm",
	"18.50ohm", "18.75ohm", "19.00ohm", "19.25ohm", "19.50ohm", "19.75ohm",
	"20.00ohm", "20.25ohm", "20.50ohm", "20.75ohm", "21.00ohm", "21.25ohm",
	"21.50ohm", "21.75ohm",	"22.00ohm", "22.25ohm", "22.50ohm", "22.75ohm",
	"23.00ohm", "23.25ohm", "23.50ohm", "23.75ohm",	"24.00ohm", "24.25ohm",
	"24.50ohm", "24.75ohm", "25.00ohm", "25.25ohm", "25.50ohm", "25.75ohm",
	"26.00ohm", "26.25ohm", "26.50ohm", "26.75ohm", "27.00ohm", "27.25ohm",
	"27.50ohm", "27.75ohm",	"28.00ohm", "28.25ohm", "28.50ohm", "28.75ohm",
	"29.00ohm", "29.25ohm", "29.50ohm", "29.75ohm",	"30.00ohm", "30.25ohm",
	"30.50ohm", "30.75ohm", "31.00ohm", "31.25ohm", "31.50ohm", "31.75ohm",
	"32.00ohm", "32.25ohm", "32.50ohm", "32.75ohm", "33.00ohm", "33.25ohm",
	"33.50ohm", "33.75ohm"
};

static SOC_ENUM_SINGLE_DECL(max98388_spkmon_load_enum,
			    MAX98388_R2032_SPK_MON_LD_SEL,
			    MAX98388_SPKMON_LOAD_SHIFT,
			    max98388_spkmon_load_text);

static const char * const max98388_edge_rate_text[] = {
	"Normal", "Reduced", "Maximum", "Increased",
};

static SOC_ENUM_SINGLE_DECL(max98388_edge_rate_falling_enum,
			    MAX98388_R2094_SPK_AMP_ER_CTRL,
			    MAX98388_EDGE_RATE_FALL_SHIFT,
			    max98388_edge_rate_text);

static SOC_ENUM_SINGLE_DECL(max98388_edge_rate_rising_enum,
			    MAX98388_R2094_SPK_AMP_ER_CTRL,
			    MAX98388_EDGE_RATE_RISE_SHIFT,
			    max98388_edge_rate_text);

static const char * const max98388_ssm_mod_text[] = {
	"1.5%", "3.0%", "4.5%", "6.0%",
};

static SOC_ENUM_SINGLE_DECL(max98388_ssm_mod_enum,
			    MAX98388_R2093_SPK_AMP_SSM_CFG,
			    MAX98388_SPK_AMP_SSM_MOD_SHIFT,
			    max98388_ssm_mod_text);

static const struct snd_kcontrol_new max98388_snd_controls[] = {
	SOC_SINGLE("Ramp Up Switch", MAX98388_R2091_SPK_CH_CFG,
		   MAX98388_SPK_CFG_VOL_RMPUP_SHIFT, 1, 0),
	SOC_SINGLE("Ramp Down Switch", MAX98388_R2091_SPK_CH_CFG,
		   MAX98388_SPK_CFG_VOL_RMPDN_SHIFT, 1, 0),
	/* Two Cell Mode Enable */
	SOC_SINGLE("OP Mode Switch", MAX98388_R2092_SPK_AMP_OUT_CFG,
		   MAX98388_SPK_AMP_OUT_MODE_SHIFT, 1, 0),
	/* Speaker Amplifier Overcurrent Automatic Restart Enable */
	SOC_SINGLE("OVC Autorestart Switch", MAX98388_R210E_AUTO_RESTART,
		   MAX98388_OVC_AUTORESTART_SHIFT, 1, 0),
	/* Thermal Shutdown Automatic Restart Enable */
	SOC_SINGLE("THERM Autorestart Switch", MAX98388_R210E_AUTO_RESTART,
		   MAX98388_THERM_AUTORESTART_SHIFT, 1, 0),
	/* PVDD UVLO Auto Restart */
	SOC_SINGLE("UVLO Autorestart Switch", MAX98388_R210E_AUTO_RESTART,
		   MAX98388_PVDD_UVLO_AUTORESTART_SHIFT, 1, 0),
	/* Clock Monitor Automatic Restart Enable */
	SOC_SINGLE("CMON Autorestart Switch", MAX98388_R210E_AUTO_RESTART,
		   MAX98388_CMON_AUTORESTART_SHIFT, 1, 0),
	SOC_SINGLE("CLK Monitor Switch", MAX98388_R2037_ERR_MON_CTRL,
		   MAX98388_CLOCK_MON_SHIFT, 1, 0),
	/* Pinknoise Generator Enable */
	SOC_SINGLE("Pinknoise Gen Switch", MAX98388_R209E_SPK_CH_PINK_NOISE_EN,
		   MAX98388_PINK_NOISE_GEN_SHIFT, 1, 0),
	/* Dither Enable */
	SOC_SINGLE("Dither Switch", MAX98388_R2091_SPK_CH_CFG,
		   MAX98388_SPK_CFG_DITH_EN_SHIFT, 1, 0),
	SOC_SINGLE("VI Dither Switch", MAX98388_R20A0_IV_DATA_DSP_CTRL,
		   MAX98388_AMP_DSP_CTRL_DITH_SHIFT, 1, 0),
	/* DC Blocker Enable */
	SOC_SINGLE("DC Blocker Switch", MAX98388_R2091_SPK_CH_CFG,
		   MAX98388_SPK_CFG_DCBLK_SHIFT, 1, 0),
	SOC_SINGLE("Voltage DC Blocker Switch", MAX98388_R20A0_IV_DATA_DSP_CTRL,
		   MAX98388_AMP_DSP_CTRL_VOL_DCBLK_SHIFT, 1, 0),
	SOC_SINGLE("Current DC Blocker Switch", MAX98388_R20A0_IV_DATA_DSP_CTRL,
		   MAX98388_AMP_DSP_CTRL_CUR_DCBLK_SHIFT, 1, 0),
	/* Digital Volume */
	SOC_SINGLE_TLV("Digital Volume", MAX98388_R2090_SPK_CH_VOL_CTRL,
		       0, 0x7F, 1, max98388_digital_tlv),
	/* Speaker Volume */
	SOC_SINGLE_TLV("Speaker Volume", MAX98388_R2092_SPK_AMP_OUT_CFG,
		       0, 5, 0, max98388_amp_gain_tlv),
	SOC_ENUM("Thermal Warn Thresh", max98388_thermal_warning_thresh_enum),
	SOC_ENUM("Thermal SHDN Thresh", max98388_thermal_shutdown_thresh_enum),
	/* Brownout Protection Automatic Level Control */
	SOC_SINGLE("ALC Switch", MAX98388_R20EF_BP_ALC_EN, 0, 1, 0),
	SOC_ENUM("ALC Thresh", max98388_alc_thresh_single_enum),
	SOC_ENUM("ALC Attack Rate", max98388_alc_attack_rate_enum),
	SOC_ENUM("ALC Release Rate", max98388_alc_release_rate_enum),
	SOC_ENUM("ALC Max Atten", max98388_alc_max_atten_enum),
	SOC_ENUM("ALC Debounce Time", max98388_alc_debouce_enum),
	SOC_SINGLE("ALC Unmute Ramp Switch", MAX98388_R20E4_BP_ALC_MUTE,
		   MAX98388_ALC_UNMUTE_RAMP_EN_SHIFT, 1, 0),
	SOC_SINGLE("ALC Mute Ramp Switch", MAX98388_R20E4_BP_ALC_MUTE,
		   MAX98388_ALC_MUTE_RAMP_EN_SHIFT, 1, 0),
	SOC_SINGLE("ALC Mute Switch", MAX98388_R20E4_BP_ALC_MUTE,
		   MAX98388_ALC_MUTE_EN_SHIFT, 1, 0),
	SOC_ENUM("ALC Mute Delay", max98388_alc_mute_delay_enum),
	/* Speaker Monitor */
	SOC_SINGLE("SPKMON Switch", MAX98388_R2037_ERR_MON_CTRL,
		   MAX98388_SPK_MON_SHIFT, 1, 0),
	SOC_ENUM("SPKMON Thresh", max98388_spkmon_thresh_enum),
	SOC_ENUM("SPKMON Load", max98388_spkmon_load_enum),
	SOC_ENUM("SPKMON Duration", max98388_spkmon_duration_enum),
	/* General Parameters */
	SOC_ENUM("Fall Slew Rate", max98388_edge_rate_falling_enum),
	SOC_ENUM("Rise Slew Rate", max98388_edge_rate_rising_enum),
	SOC_SINGLE("AMP SSM Switch", MAX98388_R2093_SPK_AMP_SSM_CFG,
		   MAX98388_SPK_AMP_SSM_EN_SHIFT, 1, 0),
	SOC_ENUM("AMP SSM Mod", max98388_ssm_mod_enum),
};

static const struct snd_soc_dapm_route max98388_audio_map[] = {
	/* Plabyack */
	{"DAI Sel Mux", "Left", "Amp Enable"},
	{"DAI Sel Mux", "Right", "Amp Enable"},
	{"DAI Sel Mux", "LeftRight", "Amp Enable"},
	{"BE_OUT", NULL, "DAI Sel Mux"},
	/* Capture */
	{ "ADC Voltage", NULL, "VMON"},
	{ "ADC Current", NULL, "IMON"},
	{ "VI Sense", "Switch", "ADC Voltage"},
	{ "VI Sense", "Switch", "ADC Current"},
	{ "Voltage Sense", NULL, "VI Sense"},
	{ "Current Sense", NULL, "VI Sense"},
};

static void max98388_reset(struct max98388_priv *max98388, struct device *dev)
{
	int ret, reg, count;

	/* Software Reset */
	ret = regmap_update_bits(max98388->regmap,
				 MAX98388_R2000_SW_RESET,
				 MAX98388_SOFT_RESET,
				 MAX98388_SOFT_RESET);
	if (ret)
		dev_err(dev, "Reset command failed. (ret:%d)\n", ret);

	count = 0;
	while (count < 3) {
		usleep_range(10000, 11000);
		/* Software Reset Verification */
		ret = regmap_read(max98388->regmap,
				  MAX98388_R22FF_REV_ID, &reg);
		if (!ret) {
			dev_info(dev, "Reset completed (retry:%d)\n", count);
			return;
		}
		count++;
	}
	dev_err(dev, "Reset failed. (ret:%d)\n", ret);
}

static int max98388_probe(struct snd_soc_component *component)
{
	struct max98388_priv *max98388 = snd_soc_component_get_drvdata(component);

	/* Software Reset */
	max98388_reset(max98388, component->dev);

	/* General channel source configuration */
	regmap_write(max98388->regmap,
		     MAX98388_R2059_PCM_RX_SRC2,
		     0x10);

	/* Enable DC blocker */
	regmap_write(max98388->regmap,
		     MAX98388_R2091_SPK_CH_CFG,
		     0x1);
	/* Enable IMON VMON DC blocker */
	regmap_write(max98388->regmap,
		     MAX98388_R20A0_IV_DATA_DSP_CTRL,
		     0x3);
	/* TX slot configuration */
	regmap_write(max98388->regmap,
		     MAX98388_R2044_PCM_TX_CTRL1,
		     max98388->v_slot);

	regmap_write(max98388->regmap,
		     MAX98388_R2045_PCM_TX_CTRL2,
		     max98388->i_slot);
	/* Enable Auto-restart behavior by default */
	regmap_write(max98388->regmap,
		     MAX98388_R210E_AUTO_RESTART, 0xF);
	/* Set interleave mode */
	if (max98388->interleave_mode)
		regmap_update_bits(max98388->regmap,
				   MAX98388_R2040_PCM_MODE_CFG,
				   MAX98388_PCM_TX_CH_INTERLEAVE_MASK,
				   MAX98388_PCM_TX_CH_INTERLEAVE_MASK);

	/* Speaker Amplifier Channel Enable */
	regmap_update_bits(max98388->regmap,
			   MAX98388_R209F_SPK_CH_AMP_EN,
			   MAX98388_SPK_EN_MASK, 1);

	return 0;
}

static int max98388_dai_set_fmt(struct snd_soc_dai *codec_dai,
				unsigned int fmt)
{
	struct snd_soc_component *component = codec_dai->component;
	struct max98388_priv *max98388 = snd_soc_component_get_drvdata(component);
	unsigned int format = 0;
	unsigned int invert = 0;

	dev_dbg(component->dev, "%s: fmt 0x%08X\n", __func__, fmt);

	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
	case SND_SOC_DAIFMT_NB_NF:
		break;
	case SND_SOC_DAIFMT_IB_NF:
		invert = MAX98388_PCM_MODE_CFG_PCM_BCLKEDGE;
		break;
	default:
		dev_err(component->dev, "DAI invert mode unsupported\n");
		return -EINVAL;
	}

	regmap_update_bits(max98388->regmap,
			   MAX98388_R2041_PCM_CLK_SETUP,
			   MAX98388_PCM_MODE_CFG_PCM_BCLKEDGE,
			   invert);

	/* interface format */
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
		format = MAX98388_PCM_FORMAT_I2S;
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		format = MAX98388_PCM_FORMAT_LJ;
		break;
	case SND_SOC_DAIFMT_DSP_A:
		format = MAX98388_PCM_FORMAT_TDM_MODE1;
		break;
	case SND_SOC_DAIFMT_DSP_B:
		format = MAX98388_PCM_FORMAT_TDM_MODE0;
		break;
	default:
		return -EINVAL;
	}

	regmap_update_bits(max98388->regmap,
			   MAX98388_R2040_PCM_MODE_CFG,
			   MAX98388_PCM_MODE_CFG_FORMAT_MASK,
			   format << MAX98388_PCM_MODE_CFG_FORMAT_SHIFT);

	return 0;
}

/* BCLKs per LRCLK */
static const int bclk_sel_table[] = {
	32, 48, 64, 96, 128, 192, 256, 384, 512, 320,
};

static int max98388_get_bclk_sel(int bclk)
{
	int i;
	/* match BCLKs per LRCLK */
	for (i = 0; i < ARRAY_SIZE(bclk_sel_table); i++) {
		if (bclk_sel_table[i] == bclk)
			return i + 2;
	}
	return 0;
}

static int max98388_set_clock(struct snd_soc_component *component,
			      struct snd_pcm_hw_params *params)
{
	struct max98388_priv *max98388 = snd_soc_component_get_drvdata(component);
	/* BCLK/LRCLK ratio calculation */
	int blr_clk_ratio = params_channels(params) * max98388->ch_size;
	int value;

	if (!max98388->tdm_mode) {
		/* BCLK configuration */
		value = max98388_get_bclk_sel(blr_clk_ratio);
		if (!value) {
			dev_err(component->dev, "format unsupported %d\n",
				params_format(params));
			return -EINVAL;
		}

		regmap_update_bits(max98388->regmap,
				   MAX98388_R2041_PCM_CLK_SETUP,
				   MAX98388_PCM_CLK_SETUP_BSEL_MASK,
				   value);
	}
	return 0;
}

static int max98388_dai_hw_params(struct snd_pcm_substream *substream,
				  struct snd_pcm_hw_params *params,
				  struct snd_soc_dai *dai)
{
	struct snd_soc_component *component = dai->component;
	struct max98388_priv *max98388 = snd_soc_component_get_drvdata(component);
	unsigned int sampling_rate = 0;
	unsigned int chan_sz = 0;
	int ret, reg;
	int status = 0;

	/* pcm mode configuration */
	switch (snd_pcm_format_width(params_format(params))) {
	case 16:
		chan_sz = MAX98388_PCM_MODE_CFG_CHANSZ_16;
		break;
	case 24:
		chan_sz = MAX98388_PCM_MODE_CFG_CHANSZ_24;
		break;
	case 32:
		chan_sz = MAX98388_PCM_MODE_CFG_CHANSZ_32;
		break;
	default:
		dev_err(component->dev, "format unsupported %d\n",
			params_format(params));
		goto err;
	}

	max98388->ch_size = snd_pcm_format_width(params_format(params));

	ret = regmap_read(max98388->regmap,
			  MAX98388_R2040_PCM_MODE_CFG, &reg);
	if (ret < 0)
		goto err;

	/* GLOBAL_EN OFF prior to the channel size re-configure */
	if (chan_sz != (reg & MAX98388_PCM_MODE_CFG_CHANSZ_MASK))	{
		ret = regmap_read(max98388->regmap,
				  MAX98388_R210F_GLOBAL_EN, &status);
		if (ret < 0)
			goto err;

		if (status) {
			regmap_write(max98388->regmap,
				     MAX98388_R210F_GLOBAL_EN, 0);
			usleep_range(30000, 31000);
		}
		regmap_update_bits(max98388->regmap,
				   MAX98388_R2040_PCM_MODE_CFG,
				   MAX98388_PCM_MODE_CFG_CHANSZ_MASK, chan_sz);
	}
	dev_dbg(component->dev, "format supported %d",
		params_format(params));

	/* sampling rate configuration */
	switch (params_rate(params)) {
	case 8000:
		sampling_rate = MAX98388_PCM_SR_8000;
		break;
	case 11025:
		sampling_rate = MAX98388_PCM_SR_11025;
		break;
	case 12000:
		sampling_rate = MAX98388_PCM_SR_12000;
		break;
	case 16000:
		sampling_rate = MAX98388_PCM_SR_16000;
		break;
	case 22050:
		sampling_rate = MAX98388_PCM_SR_22050;
		break;
	case 24000:
		sampling_rate = MAX98388_PCM_SR_24000;
		break;
	case 32000:
		sampling_rate = MAX98388_PCM_SR_32000;
		break;
	case 44100:
		sampling_rate = MAX98388_PCM_SR_44100;
		break;
	case 48000:
		sampling_rate = MAX98388_PCM_SR_48000;
		break;
	case 88200:
		sampling_rate = MAX98388_PCM_SR_88200;
		break;
	case 96000:
		sampling_rate = MAX98388_PCM_SR_96000;
		break;
	default:
		dev_err(component->dev, "rate %d not supported\n",
			params_rate(params));
		goto err;
	}

	/* set DAI_SR to correct LRCLK frequency */
	regmap_update_bits(max98388->regmap,
			   MAX98388_R2042_PCM_SR_SETUP,
			   MAX98388_PCM_SR_MASK,
			   sampling_rate);

	/* set sampling rate of IV */
	if (max98388->interleave_mode &&
	    sampling_rate > MAX98388_PCM_SR_16000)
		regmap_update_bits(max98388->regmap,
				   MAX98388_R2042_PCM_SR_SETUP,
				   MAX98388_PCM_SR_IV_MASK,
				   (sampling_rate - 3) << MAX98388_PCM_SR_IV_SHIFT);
	else
		regmap_update_bits(max98388->regmap,
				   MAX98388_R2042_PCM_SR_SETUP,
				   MAX98388_PCM_SR_IV_MASK,
				   sampling_rate << MAX98388_PCM_SR_IV_SHIFT);

	ret = max98388_set_clock(component, params);

	if (status) {
		regmap_write(max98388->regmap,
			     MAX98388_R210F_GLOBAL_EN, 1);
		usleep_range(30000, 31000);
	}

	return ret;

err:
	return -EINVAL;
}

#define MAX_NUM_SLOTS 16
#define MAX_NUM_CH 2

static int max98388_dai_tdm_slot(struct snd_soc_dai *dai,
				 unsigned int tx_mask, unsigned int rx_mask,
				 int slots, int slot_width)
{
	struct snd_soc_component *component = dai->component;
	struct max98388_priv *max98388 = snd_soc_component_get_drvdata(component);
	int bsel = 0;
	unsigned int chan_sz = 0;
	unsigned int mask;
	int cnt, slot_found;
	int addr, bits;

	if (!tx_mask && !rx_mask && !slots && !slot_width)
		max98388->tdm_mode = false;
	else
		max98388->tdm_mode = true;

	/* BCLK configuration */
	bsel = max98388_get_bclk_sel(slots * slot_width);
	if (bsel == 0) {
		dev_err(component->dev, "BCLK %d not supported\n",
			slots * slot_width);
		return -EINVAL;
	}

	regmap_update_bits(max98388->regmap,
			   MAX98388_R2041_PCM_CLK_SETUP,
			   MAX98388_PCM_CLK_SETUP_BSEL_MASK,
			   bsel);

	/* Channel size configuration */
	switch (slot_width) {
	case 16:
		chan_sz = MAX98388_PCM_MODE_CFG_CHANSZ_16;
		break;
	case 24:
		chan_sz = MAX98388_PCM_MODE_CFG_CHANSZ_24;
		break;
	case 32:
		chan_sz = MAX98388_PCM_MODE_CFG_CHANSZ_32;
		break;
	default:
		dev_err(component->dev, "format unsupported %d\n",
			slot_width);
		return -EINVAL;
	}

	regmap_update_bits(max98388->regmap,
			   MAX98388_R2040_PCM_MODE_CFG,
			   MAX98388_PCM_MODE_CFG_CHANSZ_MASK, chan_sz);

	/* Rx slot configuration */
	slot_found = 0;
	mask = rx_mask;
	for (cnt = 0 ; cnt < MAX_NUM_SLOTS ; cnt++, mask >>= 1) {
		if (mask & 0x1) {
			if (slot_found == 0)
				regmap_update_bits(max98388->regmap,
						   MAX98388_R2059_PCM_RX_SRC2,
						   MAX98388_RX_SRC_CH0_SHIFT,
						   cnt);
			else
				regmap_update_bits(max98388->regmap,
						   MAX98388_R2059_PCM_RX_SRC2,
						   MAX98388_RX_SRC_CH1_SHIFT,
						   cnt);
			slot_found++;
			if (slot_found >= MAX_NUM_CH)
				break;
		}
	}

	/* speaker feedback slot configuration */
	slot_found = 0;
	mask = tx_mask;
	for (cnt = 0 ; cnt < MAX_NUM_SLOTS ; cnt++, mask >>= 1) {
		if (mask & 0x1) {
			addr = MAX98388_R2044_PCM_TX_CTRL1 + (cnt / 8);
			bits = cnt % 8;
			regmap_update_bits(max98388->regmap, addr, bits, bits);
			slot_found++;
			if (slot_found >= MAX_NUM_CH)
				break;
		}
	}

	return 0;
}

#define MAX98388_RATES SNDRV_PCM_RATE_8000_96000

#define MAX98388_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)

static const struct snd_soc_dai_ops max98388_dai_ops = {
	.set_fmt = max98388_dai_set_fmt,
	.hw_params = max98388_dai_hw_params,
	.set_tdm_slot = max98388_dai_tdm_slot,
};

static bool max98388_readable_register(struct device *dev,
				       unsigned int reg)
{
	switch (reg) {
	case MAX98388_R2001_INT_RAW1 ... MAX98388_R2002_INT_RAW2:
	case MAX98388_R2004_INT_STATE1... MAX98388_R2005_INT_STATE2:
	case MAX98388_R2020_THERM_WARN_THRESH:
	case MAX98388_R2031_SPK_MON_THRESH
		... MAX98388_R2033_SPK_MON_DURATION:
	case MAX98388_R2037_ERR_MON_CTRL:
	case MAX98388_R2040_PCM_MODE_CFG
		... MAX98388_R2042_PCM_SR_SETUP:
	case MAX98388_R2044_PCM_TX_CTRL1
		... MAX98388_R2045_PCM_TX_CTRL2:
	case MAX98388_R2050_PCM_TX_HIZ_CTRL1
		... MAX98388_R2059_PCM_RX_SRC2:
	case MAX98388_R205C_PCM_TX_DRIVE_STRENGTH
		... MAX98388_R205F_PCM_TX_EN:
	case MAX98388_R2090_SPK_CH_VOL_CTRL
		... MAX98388_R2094_SPK_AMP_ER_CTRL:
	case MAX98388_R209E_SPK_CH_PINK_NOISE_EN
		... MAX98388_R209F_SPK_CH_AMP_EN:
	case MAX98388_R20A0_IV_DATA_DSP_CTRL:
	case MAX98388_R20A7_IV_DATA_EN:
	case MAX98388_R20E0_BP_ALC_THRESH ... MAX98388_R20E4_BP_ALC_MUTE:
	case MAX98388_R20EE_BP_INF_HOLD_REL ... MAX98388_R20EF_BP_ALC_EN:
	case MAX98388_R210E_AUTO_RESTART:
	case MAX98388_R210F_GLOBAL_EN:
	case MAX98388_R22FF_REV_ID:
		return true;
	default:
		return false;
	}
};

static bool max98388_volatile_reg(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case MAX98388_R2001_INT_RAW1 ... MAX98388_R2005_INT_STATE2:
	case MAX98388_R210F_GLOBAL_EN:
	case MAX98388_R22FF_REV_ID:
		return true;
	default:
		return false;
	}
}

static struct snd_soc_dai_driver max98388_dai[] = {
	{
		.name = "max98388-aif1",
		.playback = {
			.stream_name = "HiFi Playback",
			.channels_min = 1,
			.channels_max = 2,
			.rates = MAX98388_RATES,
			.formats = MAX98388_FORMATS,
		},
		.capture = {
			.stream_name = "HiFi Capture",
			.channels_min = 1,
			.channels_max = 2,
			.rates = MAX98388_RATES,
			.formats = MAX98388_FORMATS,
		},
		.ops = &max98388_dai_ops,
	}
};

static int max98388_suspend(struct device *dev)
{
	struct max98388_priv *max98388 = dev_get_drvdata(dev);

	regcache_cache_only(max98388->regmap, true);
	regcache_mark_dirty(max98388->regmap);

	return 0;
}

static int max98388_resume(struct device *dev)
{
	struct max98388_priv *max98388 = dev_get_drvdata(dev);

	regcache_cache_only(max98388->regmap, false);
	max98388_reset(max98388, dev);
	regcache_sync(max98388->regmap);

	return 0;
}

static const struct dev_pm_ops max98388_pm = {
	SYSTEM_SLEEP_PM_OPS(max98388_suspend, max98388_resume)
};

static const struct regmap_config max98388_regmap = {
	.reg_bits = 16,
	.val_bits = 8,
	.max_register = MAX98388_R22FF_REV_ID,
	.reg_defaults  = max98388_reg,
	.num_reg_defaults = ARRAY_SIZE(max98388_reg),
	.readable_reg = max98388_readable_register,
	.volatile_reg = max98388_volatile_reg,
	.cache_type = REGCACHE_RBTREE,
};

static const struct snd_soc_component_driver soc_codec_dev_max98388 = {
	.probe			= max98388_probe,
	.controls		= max98388_snd_controls,
	.num_controls		= ARRAY_SIZE(max98388_snd_controls),
	.dapm_widgets		= max98388_dapm_widgets,
	.num_dapm_widgets	= ARRAY_SIZE(max98388_dapm_widgets),
	.dapm_routes		= max98388_audio_map,
	.num_dapm_routes	= ARRAY_SIZE(max98388_audio_map),
	.use_pmdown_time	= 1,
	.endianness		= 1,
};

static void max98388_read_deveice_property(struct device *dev,
					   struct max98388_priv *max98388)
{
	int value;

	if (!device_property_read_u32(dev, "adi,vmon-slot-no", &value))
		max98388->v_slot = value & 0xF;
	else
		max98388->v_slot = 0;

	if (!device_property_read_u32(dev, "adi,imon-slot-no", &value))
		max98388->i_slot = value & 0xF;
	else
		max98388->i_slot = 1;

	if (device_property_read_bool(dev, "adi,interleave-mode"))
		max98388->interleave_mode = true;
	else
		max98388->interleave_mode = false;
}

static int max98388_i2c_probe(struct i2c_client *i2c)
{
	int ret = 0;
	int reg = 0;

	struct max98388_priv *max98388 = NULL;

	max98388 = devm_kzalloc(&i2c->dev, sizeof(*max98388), GFP_KERNEL);
	if (!max98388)
		return -ENOMEM;

	i2c_set_clientdata(i2c, max98388);

	/* regmap initialization */
	max98388->regmap = devm_regmap_init_i2c(i2c, &max98388_regmap);
	if (IS_ERR(max98388->regmap))
		return dev_err_probe(&i2c->dev, PTR_ERR(max98388->regmap),
				     "Failed to allocate register map.\n");

	/* voltage/current slot & gpio configuration */
	max98388_read_deveice_property(&i2c->dev, max98388);

	/* Device Reset */
	max98388->reset_gpio = devm_gpiod_get_optional(&i2c->dev,
						       "reset", GPIOD_OUT_HIGH);
	if (IS_ERR(max98388->reset_gpio))
		return dev_err_probe(&i2c->dev, PTR_ERR(max98388->reset_gpio),
				     "Unable to request GPIO\n");

	if (max98388->reset_gpio) {
		usleep_range(5000, 6000);
		gpiod_set_value_cansleep(max98388->reset_gpio, 0);
		/* Wait for the hw reset done */
		usleep_range(5000, 6000);
	}

	/* Read Revision ID */
	ret = regmap_read(max98388->regmap,
			  MAX98388_R22FF_REV_ID, &reg);
	if (ret < 0)
		return dev_err_probe(&i2c->dev, ret,
				     "Failed to read the revision ID\n");

	dev_info(&i2c->dev, "MAX98388 revisionID: 0x%02X\n", reg);

	/* codec registration */
	ret = devm_snd_soc_register_component(&i2c->dev,
					      &soc_codec_dev_max98388,
					      max98388_dai,
					      ARRAY_SIZE(max98388_dai));
	if (ret < 0)
		dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);

	return ret;
}

static const struct i2c_device_id max98388_i2c_id[] = {
	{ "max98388"},
	{ },
};

MODULE_DEVICE_TABLE(i2c, max98388_i2c_id);

static const struct of_device_id max98388_of_match[] = {
	{ .compatible = "adi,max98388", },
	{ }
};
MODULE_DEVICE_TABLE(of, max98388_of_match);

static const struct acpi_device_id max98388_acpi_match[] = {
	{ "ADS8388", 0 },
	{},
};
MODULE_DEVICE_TABLE(acpi, max98388_acpi_match);

static struct i2c_driver max98388_i2c_driver = {
	.driver = {
		.name = "max98388",
		.of_match_table = max98388_of_match,
		.acpi_match_table = max98388_acpi_match,
		.pm = pm_sleep_ptr(&max98388_pm),
	},
	.probe = max98388_i2c_probe,
	.id_table = max98388_i2c_id,
};

module_i2c_driver(max98388_i2c_driver)

MODULE_DESCRIPTION("ALSA SoC MAX98388 driver");
MODULE_AUTHOR("Ryan Lee <ryans.lee@analog.com>");
MODULE_LICENSE("GPL");
