xref: /linux/sound/soc/codecs/max98396.c (revision 0b7f644f52e81e2a653a353966933b47d01df9bb)
1b5858113SRyan Lee // SPDX-License-Identifier: GPL-2.0
2b5858113SRyan Lee // Copyright (c) 2022, Analog Devices Inc.
3b5858113SRyan Lee 
4*0b7f644fSHui Tang #include <linux/gpio/consumer.h>
5b5858113SRyan Lee #include <linux/i2c.h>
6b5858113SRyan Lee #include <linux/module.h>
7b5858113SRyan Lee #include <sound/pcm_params.h>
8b5858113SRyan Lee #include <sound/soc.h>
9b5858113SRyan Lee #include <linux/gpio.h>
10b5858113SRyan Lee #include <sound/tlv.h>
11b5858113SRyan Lee #include "max98396.h"
12b5858113SRyan Lee 
13b5858113SRyan Lee static struct reg_default max98396_reg[] = {
14b5858113SRyan Lee 	{MAX98396_R2000_SW_RESET, 0x00},
15b5858113SRyan Lee 	{MAX98396_R2001_INT_RAW1, 0x00},
16b5858113SRyan Lee 	{MAX98396_R2002_INT_RAW2, 0x00},
17b5858113SRyan Lee 	{MAX98396_R2003_INT_RAW3, 0x00},
18b5858113SRyan Lee 	{MAX98396_R2004_INT_RAW4, 0x00},
19b5858113SRyan Lee 	{MAX98396_R2006_INT_STATE1, 0x00},
20b5858113SRyan Lee 	{MAX98396_R2007_INT_STATE2, 0x00},
21b5858113SRyan Lee 	{MAX98396_R2008_INT_STATE3, 0x00},
22b5858113SRyan Lee 	{MAX98396_R2009_INT_STATE4, 0x00},
23b5858113SRyan Lee 	{MAX98396_R200B_INT_FLAG1, 0x00},
24b5858113SRyan Lee 	{MAX98396_R200C_INT_FLAG2, 0x00},
25b5858113SRyan Lee 	{MAX98396_R200D_INT_FLAG3, 0x00},
26b5858113SRyan Lee 	{MAX98396_R200E_INT_FLAG4, 0x00},
27b5858113SRyan Lee 	{MAX98396_R2010_INT_EN1, 0x02},
28b5858113SRyan Lee 	{MAX98396_R2011_INT_EN2, 0x00},
29b5858113SRyan Lee 	{MAX98396_R2012_INT_EN3, 0x00},
30b5858113SRyan Lee 	{MAX98396_R2013_INT_EN4, 0x00},
31b5858113SRyan Lee 	{MAX98396_R2015_INT_FLAG_CLR1, 0x00},
32b5858113SRyan Lee 	{MAX98396_R2016_INT_FLAG_CLR2, 0x00},
33b5858113SRyan Lee 	{MAX98396_R2017_INT_FLAG_CLR3, 0x00},
34b5858113SRyan Lee 	{MAX98396_R2018_INT_FLAG_CLR4, 0x00},
35b5858113SRyan Lee 	{MAX98396_R201F_IRQ_CTRL, 0x00},
36b5858113SRyan Lee 	{MAX98396_R2020_THERM_WARN_THRESH, 0x46},
37b5858113SRyan Lee 	{MAX98396_R2021_THERM_WARN_THRESH2, 0x46},
38b5858113SRyan Lee 	{MAX98396_R2022_THERM_SHDN_THRESH, 0x64},
39b5858113SRyan Lee 	{MAX98396_R2023_THERM_HYSTERESIS, 0x02},
40b5858113SRyan Lee 	{MAX98396_R2024_THERM_FOLDBACK_SET, 0xC5},
41b5858113SRyan Lee 	{MAX98396_R2027_THERM_FOLDBACK_EN, 0x01},
42b5858113SRyan Lee 	{MAX98396_R2030_NOISEGATE_MODE_CTRL, 0x32},
43b5858113SRyan Lee 	{MAX98396_R2033_NOISEGATE_MODE_EN, 0x00},
44b5858113SRyan Lee 	{MAX98396_R2038_CLK_MON_CTRL, 0x00},
45b5858113SRyan Lee 	{MAX98396_R2039_DATA_MON_CTRL, 0x00},
46b5858113SRyan Lee 	{MAX98396_R203F_ENABLE_CTRLS, 0x0F},
47b5858113SRyan Lee 	{MAX98396_R2040_PIN_CFG, 0x55},
48b5858113SRyan Lee 	{MAX98396_R2041_PCM_MODE_CFG, 0xC0},
49b5858113SRyan Lee 	{MAX98396_R2042_PCM_CLK_SETUP, 0x04},
50b5858113SRyan Lee 	{MAX98396_R2043_PCM_SR_SETUP, 0x88},
51b5858113SRyan Lee 	{MAX98396_R2044_PCM_TX_CTRL_1, 0x00},
52b5858113SRyan Lee 	{MAX98396_R2045_PCM_TX_CTRL_2, 0x00},
53b5858113SRyan Lee 	{MAX98396_R2046_PCM_TX_CTRL_3, 0x00},
54b5858113SRyan Lee 	{MAX98396_R2047_PCM_TX_CTRL_4, 0x00},
55b5858113SRyan Lee 	{MAX98396_R2048_PCM_TX_CTRL_5, 0x00},
56b5858113SRyan Lee 	{MAX98396_R2049_PCM_TX_CTRL_6, 0x00},
57b5858113SRyan Lee 	{MAX98396_R204A_PCM_TX_CTRL_7, 0x00},
58b5858113SRyan Lee 	{MAX98396_R204B_PCM_TX_CTRL_8, 0x00},
59b5858113SRyan Lee 	{MAX98396_R204C_PCM_TX_HIZ_CTRL_1, 0xFF},
60b5858113SRyan Lee 	{MAX98396_R204D_PCM_TX_HIZ_CTRL_2, 0xFF},
61b5858113SRyan Lee 	{MAX98396_R204E_PCM_TX_HIZ_CTRL_3, 0xFF},
62b5858113SRyan Lee 	{MAX98396_R204F_PCM_TX_HIZ_CTRL_4, 0xFF},
63b5858113SRyan Lee 	{MAX98396_R2050_PCM_TX_HIZ_CTRL_5, 0xFF},
64b5858113SRyan Lee 	{MAX98396_R2051_PCM_TX_HIZ_CTRL_6, 0xFF},
65b5858113SRyan Lee 	{MAX98396_R2052_PCM_TX_HIZ_CTRL_7, 0xFF},
66b5858113SRyan Lee 	{MAX98396_R2053_PCM_TX_HIZ_CTRL_8, 0xFF},
67b5858113SRyan Lee 	{MAX98396_R2055_PCM_RX_SRC1, 0x00},
68b5858113SRyan Lee 	{MAX98396_R2056_PCM_RX_SRC2, 0x00},
69b5858113SRyan Lee 	{MAX98396_R2058_PCM_BYPASS_SRC, 0x00},
70b5858113SRyan Lee 	{MAX98396_R205D_PCM_TX_SRC_EN, 0x00},
71b5858113SRyan Lee 	{MAX98396_R205E_PCM_RX_EN, 0x00},
72b5858113SRyan Lee 	{MAX98396_R205F_PCM_TX_EN, 0x00},
73b5858113SRyan Lee 	{MAX98396_R2070_ICC_RX_EN_A, 0x00},
74b5858113SRyan Lee 	{MAX98396_R2071_ICC_RX_EN_B, 0x00},
75b5858113SRyan Lee 	{MAX98396_R2072_ICC_TX_CTRL, 0x00},
76b5858113SRyan Lee 	{MAX98396_R207F_ICC_EN, 0x00},
77b5858113SRyan Lee 	{MAX98396_R2083_TONE_GEN_DC_CFG, 0x04},
78b5858113SRyan Lee 	{MAX98396_R2084_TONE_GEN_DC_LVL1, 0x00},
79b5858113SRyan Lee 	{MAX98396_R2085_TONE_GEN_DC_LVL2, 0x00},
80b5858113SRyan Lee 	{MAX98396_R2086_TONE_GEN_DC_LVL3, 0x00},
81b5858113SRyan Lee 	{MAX98396_R208F_TONE_GEN_EN, 0x00},
82b5858113SRyan Lee 	{MAX98396_R2090_AMP_VOL_CTRL, 0x00},
83b5858113SRyan Lee 	{MAX98396_R2091_AMP_PATH_GAIN, 0x0B},
84b5858113SRyan Lee 	{MAX98396_R2092_AMP_DSP_CFG, 0x23},
85b5858113SRyan Lee 	{MAX98396_R2093_SSM_CFG, 0x0D},
86b5858113SRyan Lee 	{MAX98396_R2094_SPK_CLS_DG_THRESH, 0x12},
87b5858113SRyan Lee 	{MAX98396_R2095_SPK_CLS_DG_HDR, 0x17},
88b5858113SRyan Lee 	{MAX98396_R2096_SPK_CLS_DG_HOLD_TIME, 0x17},
89b5858113SRyan Lee 	{MAX98396_R2097_SPK_CLS_DG_DELAY, 0x00},
90b5858113SRyan Lee 	{MAX98396_R2098_SPK_CLS_DG_MODE, 0x00},
91b5858113SRyan Lee 	{MAX98396_R2099_SPK_CLS_DG_VBAT_LVL, 0x03},
92b5858113SRyan Lee 	{MAX98396_R209A_SPK_EDGE_CTRL, 0x00},
93b5858113SRyan Lee 	{MAX98396_R209C_SPK_EDGE_CTRL1, 0x0A},
94b5858113SRyan Lee 	{MAX98396_R209D_SPK_EDGE_CTRL2, 0xAA},
95b5858113SRyan Lee 	{MAX98396_R209E_AMP_CLIP_GAIN, 0x00},
96b5858113SRyan Lee 	{MAX98396_R209F_BYPASS_PATH_CFG, 0x00},
97b5858113SRyan Lee 	{MAX98396_R20A0_AMP_SUPPLY_CTL, 0x00},
98b5858113SRyan Lee 	{MAX98396_R20AF_AMP_EN, 0x00},
99b5858113SRyan Lee 	{MAX98396_R20B0_ADC_SR, 0x30},
100b5858113SRyan Lee 	{MAX98396_R20B1_ADC_PVDD_CFG, 0x00},
101b5858113SRyan Lee 	{MAX98396_R20B2_ADC_VBAT_CFG, 0x00},
102b5858113SRyan Lee 	{MAX98396_R20B3_ADC_THERMAL_CFG, 0x00},
103b5858113SRyan Lee 	{MAX98396_R20B4_ADC_READBACK_CTRL1, 0x00},
104b5858113SRyan Lee 	{MAX98396_R20B5_ADC_READBACK_CTRL2, 0x00},
105b5858113SRyan Lee 	{MAX98396_R20B6_ADC_PVDD_READBACK_MSB, 0x00},
106b5858113SRyan Lee 	{MAX98396_R20B7_ADC_PVDD_READBACK_LSB, 0x00},
107b5858113SRyan Lee 	{MAX98396_R20B8_ADC_VBAT_READBACK_MSB, 0x00},
108b5858113SRyan Lee 	{MAX98396_R20B9_ADC_VBAT_READBACK_LSB, 0x00},
109b5858113SRyan Lee 	{MAX98396_R20BA_ADC_TEMP_READBACK_MSB, 0x00},
110b5858113SRyan Lee 	{MAX98396_R20BB_ADC_TEMP_READBACK_LSB, 0x00},
111b5858113SRyan Lee 	{MAX98396_R20BC_ADC_LO_PVDD_READBACK_MSB, 0x00},
112b5858113SRyan Lee 	{MAX98396_R20BD_ADC_LO_PVDD_READBACK_LSB, 0x00},
113b5858113SRyan Lee 	{MAX98396_R20BE_ADC_LO_VBAT_READBACK_MSB, 0x00},
114b5858113SRyan Lee 	{MAX98396_R20BF_ADC_LO_VBAT_READBACK_LSB, 0x00},
115b5858113SRyan Lee 	{MAX98396_R20C7_ADC_CFG, 0x00},
116b5858113SRyan Lee 	{MAX98396_R20D0_DHT_CFG1, 0x00},
117b5858113SRyan Lee 	{MAX98396_R20D1_LIMITER_CFG1, 0x08},
118b5858113SRyan Lee 	{MAX98396_R20D2_LIMITER_CFG2, 0x00},
119b5858113SRyan Lee 	{MAX98396_R20D3_DHT_CFG2, 0x14},
120b5858113SRyan Lee 	{MAX98396_R20D4_DHT_CFG3, 0x02},
121b5858113SRyan Lee 	{MAX98396_R20D5_DHT_CFG4, 0x04},
122b5858113SRyan Lee 	{MAX98396_R20D6_DHT_HYSTERESIS_CFG, 0x07},
123b5858113SRyan Lee 	{MAX98396_R20DF_DHT_EN, 0x00},
124b5858113SRyan Lee 	{MAX98396_R20E0_IV_SENSE_PATH_CFG, 0x04},
125b5858113SRyan Lee 	{MAX98396_R20E4_IV_SENSE_PATH_EN, 0x00},
126b5858113SRyan Lee 	{MAX98396_R20E5_BPE_STATE, 0x00},
127b5858113SRyan Lee 	{MAX98396_R20E6_BPE_L3_THRESH_MSB, 0x00},
128b5858113SRyan Lee 	{MAX98396_R20E7_BPE_L3_THRESH_LSB, 0x00},
129b5858113SRyan Lee 	{MAX98396_R20E8_BPE_L2_THRESH_MSB, 0x00},
130b5858113SRyan Lee 	{MAX98396_R20E9_BPE_L2_THRESH_LSB, 0x00},
131b5858113SRyan Lee 	{MAX98396_R20EA_BPE_L1_THRESH_MSB, 0x00},
132b5858113SRyan Lee 	{MAX98396_R20EB_BPE_L1_THRESH_LSB, 0x00},
133b5858113SRyan Lee 	{MAX98396_R20EC_BPE_L0_THRESH_MSB, 0x00},
134b5858113SRyan Lee 	{MAX98396_R20ED_BPE_L0_THRESH_LSB, 0x00},
135b5858113SRyan Lee 	{MAX98396_R20EE_BPE_L3_DWELL_HOLD_TIME, 0x00},
136b5858113SRyan Lee 	{MAX98396_R20EF_BPE_L2_DWELL_HOLD_TIME, 0x00},
137b5858113SRyan Lee 	{MAX98396_R20F0_BPE_L1_DWELL_HOLD_TIME, 0x00},
138b5858113SRyan Lee 	{MAX98396_R20F1_BPE_L0_HOLD_TIME, 0x00},
139b5858113SRyan Lee 	{MAX98396_R20F2_BPE_L3_ATTACK_REL_STEP, 0x00},
140b5858113SRyan Lee 	{MAX98396_R20F3_BPE_L2_ATTACK_REL_STEP, 0x00},
141b5858113SRyan Lee 	{MAX98396_R20F4_BPE_L1_ATTACK_REL_STEP, 0x00},
142b5858113SRyan Lee 	{MAX98396_R20F5_BPE_L0_ATTACK_REL_STEP, 0x00},
143b5858113SRyan Lee 	{MAX98396_R20F6_BPE_L3_MAX_GAIN_ATTN, 0x00},
144b5858113SRyan Lee 	{MAX98396_R20F7_BPE_L2_MAX_GAIN_ATTN, 0x00},
145b5858113SRyan Lee 	{MAX98396_R20F8_BPE_L1_MAX_GAIN_ATTN, 0x00},
146b5858113SRyan Lee 	{MAX98396_R20F9_BPE_L0_MAX_GAIN_ATTN, 0x00},
147b5858113SRyan Lee 	{MAX98396_R20FA_BPE_L3_ATT_REL_RATE, 0x00},
148b5858113SRyan Lee 	{MAX98396_R20FB_BPE_L2_ATT_REL_RATE, 0x00},
149b5858113SRyan Lee 	{MAX98396_R20FC_BPE_L1_ATT_REL_RATE, 0x00},
150b5858113SRyan Lee 	{MAX98396_R20FD_BPE_L0_ATT_REL_RATE, 0x00},
151b5858113SRyan Lee 	{MAX98396_R20FE_BPE_L3_LIMITER_CFG, 0x00},
152b5858113SRyan Lee 	{MAX98396_R20FF_BPE_L2_LIMITER_CFG, 0x00},
153b5858113SRyan Lee 	{MAX98396_R2100_BPE_L1_LIMITER_CFG, 0x00},
154b5858113SRyan Lee 	{MAX98396_R2101_BPE_L0_LIMITER_CFG, 0x00},
155b5858113SRyan Lee 	{MAX98396_R2102_BPE_L3_LIM_ATT_REL_RATE, 0x00},
156b5858113SRyan Lee 	{MAX98396_R2103_BPE_L2_LIM_ATT_REL_RATE, 0x00},
157b5858113SRyan Lee 	{MAX98396_R2104_BPE_L1_LIM_ATT_REL_RATE, 0x00},
158b5858113SRyan Lee 	{MAX98396_R2105_BPE_L0_LIM_ATT_REL_RATE, 0x00},
159b5858113SRyan Lee 	{MAX98396_R2106_BPE_THRESH_HYSTERESIS, 0x00},
160b5858113SRyan Lee 	{MAX98396_R2107_BPE_INFINITE_HOLD_CLR, 0x00},
161b5858113SRyan Lee 	{MAX98396_R2108_BPE_SUPPLY_SRC, 0x00},
162b5858113SRyan Lee 	{MAX98396_R2109_BPE_LOW_STATE, 0x00},
163b5858113SRyan Lee 	{MAX98396_R210A_BPE_LOW_GAIN, 0x00},
164b5858113SRyan Lee 	{MAX98396_R210B_BPE_LOW_LIMITER, 0x00},
165b5858113SRyan Lee 	{MAX98396_R210D_BPE_EN, 0x00},
166b5858113SRyan Lee 	{MAX98396_R210E_AUTO_RESTART, 0x00},
167b5858113SRyan Lee 	{MAX98396_R210F_GLOBAL_EN, 0x00},
168b5858113SRyan Lee 	{MAX98396_R21FF_REVISION_ID, 0x00},
169b5858113SRyan Lee };
170b5858113SRyan Lee 
171b5858113SRyan Lee static struct reg_default max98397_reg[] = {
172b5858113SRyan Lee 	{MAX98396_R2000_SW_RESET, 0x00},
173b5858113SRyan Lee 	{MAX98396_R2001_INT_RAW1, 0x00},
174b5858113SRyan Lee 	{MAX98396_R2002_INT_RAW2, 0x00},
175b5858113SRyan Lee 	{MAX98396_R2003_INT_RAW3, 0x00},
176b5858113SRyan Lee 	{MAX98396_R2004_INT_RAW4, 0x00},
177b5858113SRyan Lee 	{MAX98396_R2006_INT_STATE1, 0x00},
178b5858113SRyan Lee 	{MAX98396_R2007_INT_STATE2, 0x00},
179b5858113SRyan Lee 	{MAX98396_R2008_INT_STATE3, 0x00},
180b5858113SRyan Lee 	{MAX98396_R2009_INT_STATE4, 0x00},
181b5858113SRyan Lee 	{MAX98396_R200B_INT_FLAG1, 0x00},
182b5858113SRyan Lee 	{MAX98396_R200C_INT_FLAG2, 0x00},
183b5858113SRyan Lee 	{MAX98396_R200D_INT_FLAG3, 0x00},
184b5858113SRyan Lee 	{MAX98396_R200E_INT_FLAG4, 0x00},
185b5858113SRyan Lee 	{MAX98396_R2010_INT_EN1, 0x02},
186b5858113SRyan Lee 	{MAX98396_R2011_INT_EN2, 0x00},
187b5858113SRyan Lee 	{MAX98396_R2012_INT_EN3, 0x00},
188b5858113SRyan Lee 	{MAX98396_R2013_INT_EN4, 0x00},
189b5858113SRyan Lee 	{MAX98396_R2015_INT_FLAG_CLR1, 0x00},
190b5858113SRyan Lee 	{MAX98396_R2016_INT_FLAG_CLR2, 0x00},
191b5858113SRyan Lee 	{MAX98396_R2017_INT_FLAG_CLR3, 0x00},
192b5858113SRyan Lee 	{MAX98396_R2018_INT_FLAG_CLR4, 0x00},
193b5858113SRyan Lee 	{MAX98396_R201F_IRQ_CTRL, 0x00},
194b5858113SRyan Lee 	{MAX98396_R2020_THERM_WARN_THRESH, 0x46},
195b5858113SRyan Lee 	{MAX98396_R2021_THERM_WARN_THRESH2, 0x46},
196b5858113SRyan Lee 	{MAX98396_R2022_THERM_SHDN_THRESH, 0x64},
197b5858113SRyan Lee 	{MAX98396_R2023_THERM_HYSTERESIS, 0x02},
198b5858113SRyan Lee 	{MAX98396_R2024_THERM_FOLDBACK_SET, 0xC5},
199b5858113SRyan Lee 	{MAX98396_R2027_THERM_FOLDBACK_EN, 0x01},
200b5858113SRyan Lee 	{MAX98396_R2030_NOISEGATE_MODE_CTRL, 0x32},
201b5858113SRyan Lee 	{MAX98396_R2033_NOISEGATE_MODE_EN, 0x00},
202b5858113SRyan Lee 	{MAX98396_R2038_CLK_MON_CTRL, 0x00},
203b5858113SRyan Lee 	{MAX98396_R2039_DATA_MON_CTRL, 0x00},
204b5858113SRyan Lee 	{MAX98397_R203A_SPK_MON_THRESH, 0x03},
205b5858113SRyan Lee 	{MAX98396_R203F_ENABLE_CTRLS, 0x0F},
206b5858113SRyan Lee 	{MAX98396_R2040_PIN_CFG, 0x55},
207b5858113SRyan Lee 	{MAX98396_R2041_PCM_MODE_CFG, 0xC0},
208b5858113SRyan Lee 	{MAX98396_R2042_PCM_CLK_SETUP, 0x04},
209b5858113SRyan Lee 	{MAX98396_R2043_PCM_SR_SETUP, 0x88},
210b5858113SRyan Lee 	{MAX98396_R2044_PCM_TX_CTRL_1, 0x00},
211b5858113SRyan Lee 	{MAX98396_R2045_PCM_TX_CTRL_2, 0x00},
212b5858113SRyan Lee 	{MAX98396_R2046_PCM_TX_CTRL_3, 0x00},
213b5858113SRyan Lee 	{MAX98396_R2047_PCM_TX_CTRL_4, 0x00},
214b5858113SRyan Lee 	{MAX98396_R2048_PCM_TX_CTRL_5, 0x00},
215b5858113SRyan Lee 	{MAX98396_R2049_PCM_TX_CTRL_6, 0x00},
216b5858113SRyan Lee 	{MAX98396_R204A_PCM_TX_CTRL_7, 0x00},
217b5858113SRyan Lee 	{MAX98396_R204B_PCM_TX_CTRL_8, 0x00},
218b5858113SRyan Lee 	{MAX98397_R204C_PCM_TX_CTRL_9, 0x00},
219b5858113SRyan Lee 	{MAX98397_R204D_PCM_TX_HIZ_CTRL_1, 0xFF},
220b5858113SRyan Lee 	{MAX98397_R204E_PCM_TX_HIZ_CTRL_2, 0xFF},
221b5858113SRyan Lee 	{MAX98397_R204F_PCM_TX_HIZ_CTRL_3, 0xFF},
222b5858113SRyan Lee 	{MAX98397_R2050_PCM_TX_HIZ_CTRL_4, 0xFF},
223b5858113SRyan Lee 	{MAX98397_R2051_PCM_TX_HIZ_CTRL_5, 0xFF},
224b5858113SRyan Lee 	{MAX98397_R2052_PCM_TX_HIZ_CTRL_6, 0xFF},
225b5858113SRyan Lee 	{MAX98397_R2053_PCM_TX_HIZ_CTRL_7, 0xFF},
226b5858113SRyan Lee 	{MAX98397_R2054_PCM_TX_HIZ_CTRL_8, 0xFF},
227b5858113SRyan Lee 	{MAX98397_R2056_PCM_RX_SRC1, 0x00},
228b5858113SRyan Lee 	{MAX98397_R2057_PCM_RX_SRC2, 0x00},
229b5858113SRyan Lee 	{MAX98396_R2058_PCM_BYPASS_SRC, 0x00},
230b5858113SRyan Lee 	{MAX98396_R205D_PCM_TX_SRC_EN, 0x00},
231b5858113SRyan Lee 	{MAX98396_R205E_PCM_RX_EN, 0x00},
232b5858113SRyan Lee 	{MAX98396_R205F_PCM_TX_EN, 0x00},
233b5858113SRyan Lee 	{MAX98397_R2060_PCM_TX_SUPPLY_SEL, 0x00},
234b5858113SRyan Lee 	{MAX98396_R2070_ICC_RX_EN_A, 0x00},
235b5858113SRyan Lee 	{MAX98396_R2071_ICC_RX_EN_B, 0x00},
236b5858113SRyan Lee 	{MAX98396_R2072_ICC_TX_CTRL, 0x00},
237b5858113SRyan Lee 	{MAX98396_R207F_ICC_EN, 0x00},
238b5858113SRyan Lee 	{MAX98396_R2083_TONE_GEN_DC_CFG, 0x04},
239b5858113SRyan Lee 	{MAX98396_R2084_TONE_GEN_DC_LVL1, 0x00},
240b5858113SRyan Lee 	{MAX98396_R2085_TONE_GEN_DC_LVL2, 0x00},
241b5858113SRyan Lee 	{MAX98396_R2086_TONE_GEN_DC_LVL3, 0x00},
242b5858113SRyan Lee 	{MAX98396_R208F_TONE_GEN_EN, 0x00},
243b5858113SRyan Lee 	{MAX98396_R2090_AMP_VOL_CTRL, 0x00},
244b5858113SRyan Lee 	{MAX98396_R2091_AMP_PATH_GAIN, 0x12},
245b5858113SRyan Lee 	{MAX98396_R2092_AMP_DSP_CFG, 0x22},
246b5858113SRyan Lee 	{MAX98396_R2093_SSM_CFG, 0x08},
247b5858113SRyan Lee 	{MAX98396_R2094_SPK_CLS_DG_THRESH, 0x12},
248b5858113SRyan Lee 	{MAX98396_R2095_SPK_CLS_DG_HDR, 0x17},
249b5858113SRyan Lee 	{MAX98396_R2096_SPK_CLS_DG_HOLD_TIME, 0x17},
250b5858113SRyan Lee 	{MAX98396_R2097_SPK_CLS_DG_DELAY, 0x00},
251b5858113SRyan Lee 	{MAX98396_R2098_SPK_CLS_DG_MODE, 0x00},
252b5858113SRyan Lee 	{MAX98396_R2099_SPK_CLS_DG_VBAT_LVL, 0x03},
253b5858113SRyan Lee 	{MAX98396_R209A_SPK_EDGE_CTRL, 0x00},
254b5858113SRyan Lee 	{MAX98397_R209B_SPK_PATH_WB_ONLY, 0x00},
255b5858113SRyan Lee 	{MAX98396_R209C_SPK_EDGE_CTRL1, 0x03},
256b5858113SRyan Lee 	{MAX98396_R209D_SPK_EDGE_CTRL2, 0xFC},
257b5858113SRyan Lee 	{MAX98396_R209E_AMP_CLIP_GAIN, 0x00},
258b5858113SRyan Lee 	{MAX98396_R209F_BYPASS_PATH_CFG, 0x00},
259b5858113SRyan Lee 	{MAX98396_R20AF_AMP_EN, 0x00},
260b5858113SRyan Lee 	{MAX98396_R20B0_ADC_SR, 0x30},
261b5858113SRyan Lee 	{MAX98396_R20B1_ADC_PVDD_CFG, 0x00},
262b5858113SRyan Lee 	{MAX98396_R20B2_ADC_VBAT_CFG, 0x00},
263b5858113SRyan Lee 	{MAX98396_R20B3_ADC_THERMAL_CFG, 0x00},
264b5858113SRyan Lee 	{MAX98397_R20B4_ADC_VDDH_CFG, 0x00},
265b5858113SRyan Lee 	{MAX98397_R20B5_ADC_READBACK_CTRL1, 0x00},
266b5858113SRyan Lee 	{MAX98397_R20B6_ADC_READBACK_CTRL2, 0x00},
267b5858113SRyan Lee 	{MAX98397_R20B7_ADC_PVDD_READBACK_MSB, 0x00},
268b5858113SRyan Lee 	{MAX98397_R20B8_ADC_PVDD_READBACK_LSB, 0x00},
269b5858113SRyan Lee 	{MAX98397_R20B9_ADC_VBAT_READBACK_MSB, 0x00},
270b5858113SRyan Lee 	{MAX98397_R20BA_ADC_VBAT_READBACK_LSB, 0x00},
271b5858113SRyan Lee 	{MAX98397_R20BB_ADC_TEMP_READBACK_MSB, 0x00},
272b5858113SRyan Lee 	{MAX98397_R20BC_ADC_TEMP_READBACK_LSB, 0x00},
273b5858113SRyan Lee 	{MAX98397_R20BD_ADC_VDDH__READBACK_MSB, 0x00},
274b5858113SRyan Lee 	{MAX98397_R20BE_ADC_VDDH_READBACK_LSB, 0x00},
275b5858113SRyan Lee 	{MAX98396_R20BF_ADC_LO_VBAT_READBACK_LSB, 0x00},
276b5858113SRyan Lee 	{MAX98397_R20C3_ADC_LO_VDDH_READBACK_MSB, 0x00},
277b5858113SRyan Lee 	{MAX98397_R20C4_ADC_LO_VDDH_READBACK_LSB, 0x00},
278b5858113SRyan Lee 	{MAX98397_R20C5_MEAS_ADC_OPTIMAL_MODE, 0x04},
279b5858113SRyan Lee 	{MAX98396_R20C7_ADC_CFG, 0x00},
280b5858113SRyan Lee 	{MAX98396_R20D0_DHT_CFG1, 0x00},
281b5858113SRyan Lee 	{MAX98396_R20D1_LIMITER_CFG1, 0x08},
282b5858113SRyan Lee 	{MAX98396_R20D2_LIMITER_CFG2, 0x00},
283b5858113SRyan Lee 	{MAX98396_R20D3_DHT_CFG2, 0x14},
284b5858113SRyan Lee 	{MAX98396_R20D4_DHT_CFG3, 0x02},
285b5858113SRyan Lee 	{MAX98396_R20D5_DHT_CFG4, 0x04},
286b5858113SRyan Lee 	{MAX98396_R20D6_DHT_HYSTERESIS_CFG, 0x07},
287b5858113SRyan Lee 	{MAX98396_R20DF_DHT_EN, 0x00},
288b5858113SRyan Lee 	{MAX98396_R20E0_IV_SENSE_PATH_CFG, 0x04},
289b5858113SRyan Lee 	{MAX98396_R20E4_IV_SENSE_PATH_EN, 0x00},
290b5858113SRyan Lee 	{MAX98396_R20E5_BPE_STATE, 0x00},
291b5858113SRyan Lee 	{MAX98396_R20E6_BPE_L3_THRESH_MSB, 0x00},
292b5858113SRyan Lee 	{MAX98396_R20E7_BPE_L3_THRESH_LSB, 0x00},
293b5858113SRyan Lee 	{MAX98396_R20E8_BPE_L2_THRESH_MSB, 0x00},
294b5858113SRyan Lee 	{MAX98396_R20E9_BPE_L2_THRESH_LSB, 0x00},
295b5858113SRyan Lee 	{MAX98396_R20EA_BPE_L1_THRESH_MSB, 0x00},
296b5858113SRyan Lee 	{MAX98396_R20EB_BPE_L1_THRESH_LSB, 0x00},
297b5858113SRyan Lee 	{MAX98396_R20EC_BPE_L0_THRESH_MSB, 0x00},
298b5858113SRyan Lee 	{MAX98396_R20ED_BPE_L0_THRESH_LSB, 0x00},
299b5858113SRyan Lee 	{MAX98396_R20EE_BPE_L3_DWELL_HOLD_TIME, 0x00},
300b5858113SRyan Lee 	{MAX98396_R20EF_BPE_L2_DWELL_HOLD_TIME, 0x00},
301b5858113SRyan Lee 	{MAX98396_R20F0_BPE_L1_DWELL_HOLD_TIME, 0x00},
302b5858113SRyan Lee 	{MAX98396_R20F1_BPE_L0_HOLD_TIME, 0x00},
303b5858113SRyan Lee 	{MAX98396_R20F2_BPE_L3_ATTACK_REL_STEP, 0x00},
304b5858113SRyan Lee 	{MAX98396_R20F3_BPE_L2_ATTACK_REL_STEP, 0x00},
305b5858113SRyan Lee 	{MAX98396_R20F4_BPE_L1_ATTACK_REL_STEP, 0x00},
306b5858113SRyan Lee 	{MAX98396_R20F5_BPE_L0_ATTACK_REL_STEP, 0x00},
307b5858113SRyan Lee 	{MAX98396_R20F6_BPE_L3_MAX_GAIN_ATTN, 0x00},
308b5858113SRyan Lee 	{MAX98396_R20F7_BPE_L2_MAX_GAIN_ATTN, 0x00},
309b5858113SRyan Lee 	{MAX98396_R20F8_BPE_L1_MAX_GAIN_ATTN, 0x00},
310b5858113SRyan Lee 	{MAX98396_R20F9_BPE_L0_MAX_GAIN_ATTN, 0x00},
311b5858113SRyan Lee 	{MAX98396_R20FA_BPE_L3_ATT_REL_RATE, 0x00},
312b5858113SRyan Lee 	{MAX98396_R20FB_BPE_L2_ATT_REL_RATE, 0x00},
313b5858113SRyan Lee 	{MAX98396_R20FC_BPE_L1_ATT_REL_RATE, 0x00},
314b5858113SRyan Lee 	{MAX98396_R20FD_BPE_L0_ATT_REL_RATE, 0x00},
315b5858113SRyan Lee 	{MAX98396_R20FE_BPE_L3_LIMITER_CFG, 0x00},
316b5858113SRyan Lee 	{MAX98396_R20FF_BPE_L2_LIMITER_CFG, 0x00},
317b5858113SRyan Lee 	{MAX98396_R2100_BPE_L1_LIMITER_CFG, 0x00},
318b5858113SRyan Lee 	{MAX98396_R2101_BPE_L0_LIMITER_CFG, 0x00},
319b5858113SRyan Lee 	{MAX98396_R2102_BPE_L3_LIM_ATT_REL_RATE, 0x00},
320b5858113SRyan Lee 	{MAX98396_R2103_BPE_L2_LIM_ATT_REL_RATE, 0x00},
321b5858113SRyan Lee 	{MAX98396_R2104_BPE_L1_LIM_ATT_REL_RATE, 0x00},
322b5858113SRyan Lee 	{MAX98396_R2105_BPE_L0_LIM_ATT_REL_RATE, 0x00},
323b5858113SRyan Lee 	{MAX98396_R2106_BPE_THRESH_HYSTERESIS, 0x00},
324b5858113SRyan Lee 	{MAX98396_R2107_BPE_INFINITE_HOLD_CLR, 0x00},
325b5858113SRyan Lee 	{MAX98396_R2108_BPE_SUPPLY_SRC, 0x00},
326b5858113SRyan Lee 	{MAX98396_R2109_BPE_LOW_STATE, 0x00},
327b5858113SRyan Lee 	{MAX98396_R210A_BPE_LOW_GAIN, 0x00},
328b5858113SRyan Lee 	{MAX98396_R210B_BPE_LOW_LIMITER, 0x00},
329b5858113SRyan Lee 	{MAX98396_R210D_BPE_EN, 0x00},
330b5858113SRyan Lee 	{MAX98396_R210E_AUTO_RESTART, 0x00},
331b5858113SRyan Lee 	{MAX98396_R210F_GLOBAL_EN, 0x00},
332b5858113SRyan Lee 	{MAX98397_R22FF_REVISION_ID, 0x00},
333b5858113SRyan Lee };
334b5858113SRyan Lee 
335b5858113SRyan Lee static void max98396_global_enable_onoff(struct regmap *regmap, bool onoff)
336b5858113SRyan Lee {
337b5858113SRyan Lee 	regmap_write(regmap, MAX98396_R210F_GLOBAL_EN, onoff ? 1 : 0);
338b5858113SRyan Lee 	usleep_range(11000, 12000);
339b5858113SRyan Lee }
340b5858113SRyan Lee 
341b5858113SRyan Lee static int max98396_dai_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
342b5858113SRyan Lee {
343b5858113SRyan Lee 	struct snd_soc_component *component = codec_dai->component;
344b5858113SRyan Lee 	struct max98396_priv *max98396 = snd_soc_component_get_drvdata(component);
345b5858113SRyan Lee 	unsigned int format = 0;
346b5858113SRyan Lee 	unsigned int bclk_pol = 0;
347b5858113SRyan Lee 	int ret, status;
348b5858113SRyan Lee 	int reg;
349b5858113SRyan Lee 	bool update = false;
350b5858113SRyan Lee 
351b5858113SRyan Lee 	dev_dbg(component->dev, "%s: fmt 0x%08X\n", __func__, fmt);
352b5858113SRyan Lee 
353b5858113SRyan Lee 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
354b5858113SRyan Lee 	case SND_SOC_DAIFMT_NB_NF:
355b5858113SRyan Lee 		break;
356b5858113SRyan Lee 	case SND_SOC_DAIFMT_NB_IF:
357b5858113SRyan Lee 		format = MAX98396_PCM_MODE_CFG_LRCLKEDGE;
358b5858113SRyan Lee 		break;
359b5858113SRyan Lee 	case SND_SOC_DAIFMT_IB_NF:
360b5858113SRyan Lee 		bclk_pol = MAX98396_PCM_MODE_CFG_BCLKEDGE;
361b5858113SRyan Lee 		break;
362b5858113SRyan Lee 	case SND_SOC_DAIFMT_IB_IF:
363b5858113SRyan Lee 		bclk_pol = MAX98396_PCM_MODE_CFG_BCLKEDGE;
364b5858113SRyan Lee 		format = MAX98396_PCM_MODE_CFG_LRCLKEDGE;
365b5858113SRyan Lee 		break;
366b5858113SRyan Lee 
367b5858113SRyan Lee 	default:
368b5858113SRyan Lee 		dev_err(component->dev, "DAI invert mode unsupported\n");
369b5858113SRyan Lee 		return -EINVAL;
370b5858113SRyan Lee 	}
371b5858113SRyan Lee 
372b5858113SRyan Lee 	/* interface format */
373b5858113SRyan Lee 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
374b5858113SRyan Lee 	case SND_SOC_DAIFMT_I2S:
375b5858113SRyan Lee 		format |= MAX98396_PCM_FORMAT_I2S;
376b5858113SRyan Lee 		break;
377b5858113SRyan Lee 	case SND_SOC_DAIFMT_LEFT_J:
378b5858113SRyan Lee 		format |= MAX98396_PCM_FORMAT_LJ;
379b5858113SRyan Lee 		break;
380b5858113SRyan Lee 	case SND_SOC_DAIFMT_DSP_A:
381b5858113SRyan Lee 		format |= MAX98396_PCM_FORMAT_TDM_MODE1;
382b5858113SRyan Lee 		break;
383b5858113SRyan Lee 	case SND_SOC_DAIFMT_DSP_B:
384b5858113SRyan Lee 		format |= MAX98396_PCM_FORMAT_TDM_MODE0;
385b5858113SRyan Lee 		break;
386b5858113SRyan Lee 	default:
387b5858113SRyan Lee 		return -EINVAL;
388b5858113SRyan Lee 	}
389b5858113SRyan Lee 
390b5858113SRyan Lee 	ret = regmap_read(max98396->regmap, MAX98396_R210F_GLOBAL_EN, &status);
391b5858113SRyan Lee 	if (ret < 0)
392b5858113SRyan Lee 		return -EINVAL;
393b5858113SRyan Lee 
394b5858113SRyan Lee 	if (status) {
395b5858113SRyan Lee 		ret = regmap_read(max98396->regmap, MAX98396_R2041_PCM_MODE_CFG, &reg);
396b5858113SRyan Lee 		if (ret < 0)
397b5858113SRyan Lee 			return -EINVAL;
398b5858113SRyan Lee 		if (format != (reg & MAX98396_PCM_BCLKEDGE_BSEL_MASK)) {
399b5858113SRyan Lee 			update = true;
400b5858113SRyan Lee 		} else {
401b5858113SRyan Lee 			ret = regmap_read(max98396->regmap,
402b5858113SRyan Lee 					  MAX98396_R2042_PCM_CLK_SETUP, &reg);
403b5858113SRyan Lee 			if (ret < 0)
404b5858113SRyan Lee 				return -EINVAL;
405b5858113SRyan Lee 			if (bclk_pol != (reg & MAX98396_PCM_MODE_CFG_BCLKEDGE))
406b5858113SRyan Lee 				update = true;
407b5858113SRyan Lee 		}
408b5858113SRyan Lee 		/* GLOBAL_EN OFF prior to pcm mode, clock configuration change */
409b5858113SRyan Lee 		if (update)
410b5858113SRyan Lee 			max98396_global_enable_onoff(max98396->regmap, false);
411b5858113SRyan Lee 	}
412b5858113SRyan Lee 
413b5858113SRyan Lee 	regmap_update_bits(max98396->regmap,
414b5858113SRyan Lee 			   MAX98396_R2041_PCM_MODE_CFG,
415b5858113SRyan Lee 			   MAX98396_PCM_BCLKEDGE_BSEL_MASK,
416b5858113SRyan Lee 			   format);
417b5858113SRyan Lee 
418b5858113SRyan Lee 	regmap_update_bits(max98396->regmap,
419b5858113SRyan Lee 			   MAX98396_R2042_PCM_CLK_SETUP,
420b5858113SRyan Lee 			   MAX98396_PCM_MODE_CFG_BCLKEDGE,
421b5858113SRyan Lee 			   bclk_pol);
422b5858113SRyan Lee 
423b5858113SRyan Lee 	if (status && update)
424b5858113SRyan Lee 		max98396_global_enable_onoff(max98396->regmap, true);
425b5858113SRyan Lee 
426b5858113SRyan Lee 	return 0;
427b5858113SRyan Lee }
428b5858113SRyan Lee 
429b5858113SRyan Lee /* BCLKs per LRCLK */
430b5858113SRyan Lee static const int bclk_sel_table[] = {
431b5858113SRyan Lee 	32, 48, 64, 96, 128, 192, 256, 384, 512, 320,
432b5858113SRyan Lee };
433b5858113SRyan Lee 
434b5858113SRyan Lee static int max98396_get_bclk_sel(int bclk)
435b5858113SRyan Lee {
436b5858113SRyan Lee 	int i;
437b5858113SRyan Lee 	/* match BCLKs per LRCLK */
438b5858113SRyan Lee 	for (i = 0; i < ARRAY_SIZE(bclk_sel_table); i++) {
439b5858113SRyan Lee 		if (bclk_sel_table[i] == bclk)
440b5858113SRyan Lee 			return i + 2;
441b5858113SRyan Lee 	}
442b5858113SRyan Lee 	return 0;
443b5858113SRyan Lee }
444b5858113SRyan Lee 
445b5858113SRyan Lee static int max98396_set_clock(struct snd_soc_component *component,
446b5858113SRyan Lee 			      struct snd_pcm_hw_params *params)
447b5858113SRyan Lee {
448b5858113SRyan Lee 	struct max98396_priv *max98396 = snd_soc_component_get_drvdata(component);
449b5858113SRyan Lee 	/* BCLK/LRCLK ratio calculation */
450b5858113SRyan Lee 	int blr_clk_ratio = params_channels(params) * max98396->ch_size;
451b5858113SRyan Lee 	int value;
452b5858113SRyan Lee 
453b5858113SRyan Lee 	if (!max98396->tdm_mode) {
454b5858113SRyan Lee 		/* BCLK configuration */
455b5858113SRyan Lee 		value = max98396_get_bclk_sel(blr_clk_ratio);
456b5858113SRyan Lee 		if (!value) {
457b5858113SRyan Lee 			dev_err(component->dev, "format unsupported %d\n",
458b5858113SRyan Lee 				params_format(params));
459b5858113SRyan Lee 			return -EINVAL;
460b5858113SRyan Lee 		}
461b5858113SRyan Lee 
462b5858113SRyan Lee 		regmap_update_bits(max98396->regmap,
463b5858113SRyan Lee 				   MAX98396_R2042_PCM_CLK_SETUP,
464b5858113SRyan Lee 				   MAX98396_PCM_CLK_SETUP_BSEL_MASK,
465b5858113SRyan Lee 				   value);
466b5858113SRyan Lee 	}
467b5858113SRyan Lee 
468b5858113SRyan Lee 	return 0;
469b5858113SRyan Lee }
470b5858113SRyan Lee 
471b5858113SRyan Lee static int max98396_dai_hw_params(struct snd_pcm_substream *substream,
472b5858113SRyan Lee 				  struct snd_pcm_hw_params *params,
473b5858113SRyan Lee 				  struct snd_soc_dai *dai)
474b5858113SRyan Lee {
475b5858113SRyan Lee 	struct snd_soc_component *component = dai->component;
476b5858113SRyan Lee 	struct max98396_priv *max98396 = snd_soc_component_get_drvdata(component);
477b5858113SRyan Lee 	unsigned int sampling_rate = 0;
478b5858113SRyan Lee 	unsigned int chan_sz = 0;
479b5858113SRyan Lee 	int ret, reg;
480b5858113SRyan Lee 	int status;
481b5858113SRyan Lee 	bool update = false;
482b5858113SRyan Lee 
483b5858113SRyan Lee 	/* pcm mode configuration */
484b5858113SRyan Lee 	switch (snd_pcm_format_width(params_format(params))) {
485b5858113SRyan Lee 	case 16:
486b5858113SRyan Lee 		chan_sz = MAX98396_PCM_MODE_CFG_CHANSZ_16;
487b5858113SRyan Lee 		break;
488b5858113SRyan Lee 	case 24:
489b5858113SRyan Lee 		chan_sz = MAX98396_PCM_MODE_CFG_CHANSZ_24;
490b5858113SRyan Lee 		break;
491b5858113SRyan Lee 	case 32:
492b5858113SRyan Lee 		chan_sz = MAX98396_PCM_MODE_CFG_CHANSZ_32;
493b5858113SRyan Lee 		break;
494b5858113SRyan Lee 	default:
495b5858113SRyan Lee 		dev_err(component->dev, "format unsupported %d\n",
496b5858113SRyan Lee 			params_format(params));
497b5858113SRyan Lee 		goto err;
498b5858113SRyan Lee 	}
499b5858113SRyan Lee 
500b5858113SRyan Lee 	max98396->ch_size = snd_pcm_format_width(params_format(params));
501b5858113SRyan Lee 
502b5858113SRyan Lee 	dev_dbg(component->dev, "format supported %d",
503b5858113SRyan Lee 		params_format(params));
504b5858113SRyan Lee 
505b5858113SRyan Lee 	/* sampling rate configuration */
506b5858113SRyan Lee 	switch (params_rate(params)) {
507b5858113SRyan Lee 	case 8000:
508b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_8000;
509b5858113SRyan Lee 		break;
510b5858113SRyan Lee 	case 11025:
511b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_11025;
512b5858113SRyan Lee 		break;
513b5858113SRyan Lee 	case 12000:
514b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_12000;
515b5858113SRyan Lee 		break;
516b5858113SRyan Lee 	case 16000:
517b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_16000;
518b5858113SRyan Lee 		break;
519b5858113SRyan Lee 	case 22050:
520b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_22050;
521b5858113SRyan Lee 		break;
522b5858113SRyan Lee 	case 24000:
523b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_24000;
524b5858113SRyan Lee 		break;
525b5858113SRyan Lee 	case 32000:
526b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_32000;
527b5858113SRyan Lee 		break;
528b5858113SRyan Lee 	case 44100:
529b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_44100;
530b5858113SRyan Lee 		break;
531b5858113SRyan Lee 	case 48000:
532b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_48000;
533b5858113SRyan Lee 		break;
534b5858113SRyan Lee 	case 88200:
535b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_88200;
536b5858113SRyan Lee 		break;
537b5858113SRyan Lee 	case 96000:
538b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_96000;
539b5858113SRyan Lee 		break;
540b5858113SRyan Lee 	case 192000:
541b5858113SRyan Lee 		sampling_rate = MAX98396_PCM_SR_192000;
542b5858113SRyan Lee 		break;
543b5858113SRyan Lee 	default:
544b5858113SRyan Lee 		dev_err(component->dev, "rate %d not supported\n",
545b5858113SRyan Lee 			params_rate(params));
546b5858113SRyan Lee 		goto err;
547b5858113SRyan Lee 	}
548b5858113SRyan Lee 
549b5858113SRyan Lee 	ret = regmap_read(max98396->regmap, MAX98396_R210F_GLOBAL_EN, &status);
550b5858113SRyan Lee 	if (ret < 0)
551b5858113SRyan Lee 		goto err;
552b5858113SRyan Lee 
553b5858113SRyan Lee 	if (status) {
554b5858113SRyan Lee 		ret = regmap_read(max98396->regmap, MAX98396_R2041_PCM_MODE_CFG, &reg);
555b5858113SRyan Lee 		if (ret < 0)
556b5858113SRyan Lee 			goto err;
557b5858113SRyan Lee 		if (chan_sz != (reg & MAX98396_PCM_MODE_CFG_CHANSZ_MASK)) {
558b5858113SRyan Lee 			update = true;
559b5858113SRyan Lee 		} else {
560b5858113SRyan Lee 			ret = regmap_read(max98396->regmap, MAX98396_R2043_PCM_SR_SETUP, &reg);
561b5858113SRyan Lee 			if (ret < 0)
562b5858113SRyan Lee 				goto err;
563b5858113SRyan Lee 			if (sampling_rate != (reg & MAX98396_PCM_SR_MASK))
564b5858113SRyan Lee 				update = true;
565b5858113SRyan Lee 		}
566b5858113SRyan Lee 
567b5858113SRyan Lee 		/* GLOBAL_EN OFF prior to channel size and sampling rate change */
568b5858113SRyan Lee 		if (update)
569b5858113SRyan Lee 			max98396_global_enable_onoff(max98396->regmap, false);
570b5858113SRyan Lee 	}
571b5858113SRyan Lee 
572b5858113SRyan Lee 	/* set channel size */
573b5858113SRyan Lee 	regmap_update_bits(max98396->regmap, MAX98396_R2041_PCM_MODE_CFG,
574b5858113SRyan Lee 			   MAX98396_PCM_MODE_CFG_CHANSZ_MASK, chan_sz);
575b5858113SRyan Lee 
576b5858113SRyan Lee 	/* set DAI_SR to correct LRCLK frequency */
577b5858113SRyan Lee 	regmap_update_bits(max98396->regmap, MAX98396_R2043_PCM_SR_SETUP,
578b5858113SRyan Lee 			   MAX98396_PCM_SR_MASK, sampling_rate);
579b5858113SRyan Lee 
580b5858113SRyan Lee 	/* set sampling rate of IV */
581b5858113SRyan Lee 	if (max98396->interleave_mode &&
582b5858113SRyan Lee 	    sampling_rate > MAX98396_PCM_SR_16000)
583b5858113SRyan Lee 		regmap_update_bits(max98396->regmap,
584b5858113SRyan Lee 				   MAX98396_R2043_PCM_SR_SETUP,
585b5858113SRyan Lee 				   MAX98396_IVADC_SR_MASK,
586b5858113SRyan Lee 				   (sampling_rate - 3)
587b5858113SRyan Lee 				   << MAX98396_IVADC_SR_SHIFT);
588b5858113SRyan Lee 	else
589b5858113SRyan Lee 		regmap_update_bits(max98396->regmap,
590b5858113SRyan Lee 				   MAX98396_R2043_PCM_SR_SETUP,
591b5858113SRyan Lee 				   MAX98396_IVADC_SR_MASK,
592b5858113SRyan Lee 				   sampling_rate << MAX98396_IVADC_SR_SHIFT);
593b5858113SRyan Lee 
594b5858113SRyan Lee 	ret = max98396_set_clock(component, params);
595b5858113SRyan Lee 
596b5858113SRyan Lee 	if (status && update)
597b5858113SRyan Lee 		max98396_global_enable_onoff(max98396->regmap, true);
598b5858113SRyan Lee 
599b5858113SRyan Lee 	return ret;
600b5858113SRyan Lee 
601b5858113SRyan Lee err:
602b5858113SRyan Lee 	return -EINVAL;
603b5858113SRyan Lee }
604b5858113SRyan Lee 
605b5858113SRyan Lee static int max98396_dai_tdm_slot(struct snd_soc_dai *dai,
606b5858113SRyan Lee 				 unsigned int tx_mask, unsigned int rx_mask,
607b5858113SRyan Lee 				 int slots, int slot_width)
608b5858113SRyan Lee {
609b5858113SRyan Lee 	struct snd_soc_component *component = dai->component;
610b5858113SRyan Lee 	struct max98396_priv *max98396 =
611b5858113SRyan Lee 		snd_soc_component_get_drvdata(component);
612b5858113SRyan Lee 	int bsel;
613b5858113SRyan Lee 	unsigned int chan_sz = 0;
614b5858113SRyan Lee 	int ret, status;
615b5858113SRyan Lee 	int reg;
616b5858113SRyan Lee 	bool update = false;
617b5858113SRyan Lee 
618b5858113SRyan Lee 	if (!tx_mask && !rx_mask && !slots && !slot_width)
619b5858113SRyan Lee 		max98396->tdm_mode = false;
620b5858113SRyan Lee 	else
621b5858113SRyan Lee 		max98396->tdm_mode = true;
622b5858113SRyan Lee 
623b5858113SRyan Lee 	/* BCLK configuration */
624b5858113SRyan Lee 	bsel = max98396_get_bclk_sel(slots * slot_width);
625b5858113SRyan Lee 	if (bsel == 0) {
626b5858113SRyan Lee 		dev_err(component->dev, "BCLK %d not supported\n",
627b5858113SRyan Lee 			slots * slot_width);
628b5858113SRyan Lee 		return -EINVAL;
629b5858113SRyan Lee 	}
630b5858113SRyan Lee 
631b5858113SRyan Lee 	/* Channel size configuration */
632b5858113SRyan Lee 	switch (slot_width) {
633b5858113SRyan Lee 	case 16:
634b5858113SRyan Lee 		chan_sz = MAX98396_PCM_MODE_CFG_CHANSZ_16;
635b5858113SRyan Lee 		break;
636b5858113SRyan Lee 	case 24:
637b5858113SRyan Lee 		chan_sz = MAX98396_PCM_MODE_CFG_CHANSZ_24;
638b5858113SRyan Lee 		break;
639b5858113SRyan Lee 	case 32:
640b5858113SRyan Lee 		chan_sz = MAX98396_PCM_MODE_CFG_CHANSZ_32;
641b5858113SRyan Lee 		break;
642b5858113SRyan Lee 	default:
643b5858113SRyan Lee 		dev_err(component->dev, "format unsupported %d\n",
644b5858113SRyan Lee 			slot_width);
645b5858113SRyan Lee 		return -EINVAL;
646b5858113SRyan Lee 	}
647b5858113SRyan Lee 
648b5858113SRyan Lee 	ret = regmap_read(max98396->regmap, MAX98396_R210F_GLOBAL_EN, &status);
649b5858113SRyan Lee 	if (ret < 0)
650b5858113SRyan Lee 		return -EINVAL;
651b5858113SRyan Lee 
652b5858113SRyan Lee 	if (status) {
653b5858113SRyan Lee 		ret = regmap_read(max98396->regmap, MAX98396_R2042_PCM_CLK_SETUP, &reg);
654b5858113SRyan Lee 		if (ret < 0)
655b5858113SRyan Lee 			return -EINVAL;
656b5858113SRyan Lee 		if (bsel != (reg & MAX98396_PCM_CLK_SETUP_BSEL_MASK)) {
657b5858113SRyan Lee 			update = true;
658b5858113SRyan Lee 		} else {
659b5858113SRyan Lee 			ret = regmap_read(max98396->regmap, MAX98396_R2041_PCM_MODE_CFG, &reg);
660b5858113SRyan Lee 			if (ret < 0)
661b5858113SRyan Lee 				return -EINVAL;
662b5858113SRyan Lee 			if (chan_sz != (reg & MAX98396_PCM_MODE_CFG_CHANSZ_MASK))
663b5858113SRyan Lee 				update = true;
664b5858113SRyan Lee 		}
665b5858113SRyan Lee 
666b5858113SRyan Lee 		/* GLOBAL_EN OFF prior to channel size and BCLK per LRCLK change */
667b5858113SRyan Lee 		if (update)
668b5858113SRyan Lee 			max98396_global_enable_onoff(max98396->regmap, false);
669b5858113SRyan Lee 	}
670b5858113SRyan Lee 
671b5858113SRyan Lee 	regmap_update_bits(max98396->regmap,
672b5858113SRyan Lee 			   MAX98396_R2042_PCM_CLK_SETUP,
673b5858113SRyan Lee 			   MAX98396_PCM_CLK_SETUP_BSEL_MASK,
674b5858113SRyan Lee 			   bsel);
675b5858113SRyan Lee 
676b5858113SRyan Lee 	regmap_update_bits(max98396->regmap,
677b5858113SRyan Lee 			   MAX98396_R2041_PCM_MODE_CFG,
678b5858113SRyan Lee 			   MAX98396_PCM_MODE_CFG_CHANSZ_MASK, chan_sz);
679b5858113SRyan Lee 
680b5858113SRyan Lee 	/* Rx slot configuration */
681b5858113SRyan Lee 	if (max98396->device_id == CODEC_TYPE_MAX98396) {
682b5858113SRyan Lee 		regmap_update_bits(max98396->regmap,
683b5858113SRyan Lee 				   MAX98396_R2056_PCM_RX_SRC2,
684b5858113SRyan Lee 				   MAX98396_PCM_DMIX_CH0_SRC_MASK,
685b5858113SRyan Lee 				   rx_mask);
686b5858113SRyan Lee 		regmap_update_bits(max98396->regmap,
687b5858113SRyan Lee 				   MAX98396_R2056_PCM_RX_SRC2,
688b5858113SRyan Lee 				   MAX98396_PCM_DMIX_CH1_SRC_MASK,
689b5858113SRyan Lee 				   rx_mask << MAX98396_PCM_DMIX_CH1_SHIFT);
690b5858113SRyan Lee 	} else {
691b5858113SRyan Lee 		regmap_update_bits(max98396->regmap,
692b5858113SRyan Lee 				   MAX98397_R2057_PCM_RX_SRC2,
693b5858113SRyan Lee 				   MAX98396_PCM_DMIX_CH0_SRC_MASK,
694b5858113SRyan Lee 				   rx_mask);
695b5858113SRyan Lee 		regmap_update_bits(max98396->regmap,
696b5858113SRyan Lee 				   MAX98397_R2057_PCM_RX_SRC2,
697b5858113SRyan Lee 				   MAX98396_PCM_DMIX_CH1_SRC_MASK,
698b5858113SRyan Lee 				   rx_mask << MAX98396_PCM_DMIX_CH1_SHIFT);
699b5858113SRyan Lee 	}
700b5858113SRyan Lee 
701b5858113SRyan Lee 	/* Tx slot Hi-Z configuration */
702b5858113SRyan Lee 	if (max98396->device_id == CODEC_TYPE_MAX98396) {
703b5858113SRyan Lee 		regmap_write(max98396->regmap,
704b5858113SRyan Lee 			     MAX98396_R2053_PCM_TX_HIZ_CTRL_8,
705b5858113SRyan Lee 			     ~tx_mask & 0xFF);
706b5858113SRyan Lee 		regmap_write(max98396->regmap,
707b5858113SRyan Lee 			     MAX98396_R2052_PCM_TX_HIZ_CTRL_7,
708b5858113SRyan Lee 			     (~tx_mask & 0xFF00) >> 8);
709b5858113SRyan Lee 	} else {
710b5858113SRyan Lee 		regmap_write(max98396->regmap,
711b5858113SRyan Lee 			     MAX98397_R2054_PCM_TX_HIZ_CTRL_8,
712b5858113SRyan Lee 			     ~tx_mask & 0xFF);
713b5858113SRyan Lee 		regmap_write(max98396->regmap,
714b5858113SRyan Lee 			     MAX98397_R2053_PCM_TX_HIZ_CTRL_7,
715b5858113SRyan Lee 			     (~tx_mask & 0xFF00) >> 8);
716b5858113SRyan Lee 	}
717b5858113SRyan Lee 
718b5858113SRyan Lee 	if (status && update)
719b5858113SRyan Lee 		max98396_global_enable_onoff(max98396->regmap, true);
720b5858113SRyan Lee 
721b5858113SRyan Lee 	return 0;
722b5858113SRyan Lee }
723b5858113SRyan Lee 
724b5858113SRyan Lee #define MAX98396_RATES SNDRV_PCM_RATE_8000_192000
725b5858113SRyan Lee 
726b5858113SRyan Lee #define MAX98396_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
727b5858113SRyan Lee 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
728b5858113SRyan Lee 
729b5858113SRyan Lee static const struct snd_soc_dai_ops max98396_dai_ops = {
730b5858113SRyan Lee 	.set_fmt = max98396_dai_set_fmt,
731b5858113SRyan Lee 	.hw_params = max98396_dai_hw_params,
732b5858113SRyan Lee 	.set_tdm_slot = max98396_dai_tdm_slot,
733b5858113SRyan Lee };
734b5858113SRyan Lee 
735b5858113SRyan Lee static int max98396_dac_event(struct snd_soc_dapm_widget *w,
736b5858113SRyan Lee 			      struct snd_kcontrol *kcontrol, int event)
737b5858113SRyan Lee {
738b5858113SRyan Lee 	struct snd_soc_component *component =
739b5858113SRyan Lee 		snd_soc_dapm_to_component(w->dapm);
740b5858113SRyan Lee 	struct max98396_priv *max98396 =
741b5858113SRyan Lee 		snd_soc_component_get_drvdata(component);
742b5858113SRyan Lee 
743b5858113SRyan Lee 	switch (event) {
744b5858113SRyan Lee 	case SND_SOC_DAPM_POST_PMU:
745b5858113SRyan Lee 		max98396_global_enable_onoff(max98396->regmap, true);
746b5858113SRyan Lee 		break;
747b5858113SRyan Lee 	case SND_SOC_DAPM_PRE_PMD:
748b5858113SRyan Lee 		max98396_global_enable_onoff(max98396->regmap, false);
749b5858113SRyan Lee 
750b5858113SRyan Lee 		max98396->tdm_mode = false;
751b5858113SRyan Lee 		break;
752b5858113SRyan Lee 	default:
753b5858113SRyan Lee 		return 0;
754b5858113SRyan Lee 	}
755b5858113SRyan Lee 	return 0;
756b5858113SRyan Lee }
757b5858113SRyan Lee 
758b5858113SRyan Lee static bool max98396_readable_register(struct device *dev, unsigned int reg)
759b5858113SRyan Lee {
760b5858113SRyan Lee 	switch (reg) {
761b5858113SRyan Lee 	case MAX98396_R2001_INT_RAW1 ... MAX98396_R2004_INT_RAW4:
762b5858113SRyan Lee 	case MAX98396_R2006_INT_STATE1 ... MAX98396_R2009_INT_STATE4:
763b5858113SRyan Lee 	case MAX98396_R200B_INT_FLAG1 ... MAX98396_R200E_INT_FLAG4:
764b5858113SRyan Lee 	case MAX98396_R2010_INT_EN1 ... MAX98396_R2013_INT_EN4:
765b5858113SRyan Lee 	case MAX98396_R2015_INT_FLAG_CLR1 ... MAX98396_R2018_INT_FLAG_CLR4:
766b5858113SRyan Lee 	case MAX98396_R201F_IRQ_CTRL ... MAX98396_R2024_THERM_FOLDBACK_SET:
767b5858113SRyan Lee 	case MAX98396_R2027_THERM_FOLDBACK_EN:
768b5858113SRyan Lee 	case MAX98396_R2030_NOISEGATE_MODE_CTRL:
769b5858113SRyan Lee 	case MAX98396_R2033_NOISEGATE_MODE_EN:
770b5858113SRyan Lee 	case MAX98396_R2038_CLK_MON_CTRL ... MAX98396_R2039_DATA_MON_CTRL:
771b5858113SRyan Lee 	case MAX98396_R203F_ENABLE_CTRLS ... MAX98396_R2053_PCM_TX_HIZ_CTRL_8:
772b5858113SRyan Lee 	case MAX98396_R2055_PCM_RX_SRC1 ... MAX98396_R2056_PCM_RX_SRC2:
773b5858113SRyan Lee 	case MAX98396_R2058_PCM_BYPASS_SRC:
774b5858113SRyan Lee 	case MAX98396_R205D_PCM_TX_SRC_EN ... MAX98396_R205F_PCM_TX_EN:
775b5858113SRyan Lee 	case MAX98396_R2070_ICC_RX_EN_A... MAX98396_R2072_ICC_TX_CTRL:
776b5858113SRyan Lee 	case MAX98396_R207F_ICC_EN:
777b5858113SRyan Lee 	case MAX98396_R2083_TONE_GEN_DC_CFG ... MAX98396_R2086_TONE_GEN_DC_LVL3:
778b5858113SRyan Lee 	case MAX98396_R208F_TONE_GEN_EN ... MAX98396_R209A_SPK_EDGE_CTRL:
779b5858113SRyan Lee 	case MAX98396_R209C_SPK_EDGE_CTRL1 ... MAX98396_R20A0_AMP_SUPPLY_CTL:
780b5858113SRyan Lee 	case MAX98396_R20AF_AMP_EN ... MAX98396_R20BF_ADC_LO_VBAT_READBACK_LSB:
781b5858113SRyan Lee 	case MAX98396_R20C7_ADC_CFG:
782b5858113SRyan Lee 	case MAX98396_R20D0_DHT_CFG1 ... MAX98396_R20D6_DHT_HYSTERESIS_CFG:
783b5858113SRyan Lee 	case MAX98396_R20DF_DHT_EN:
784b5858113SRyan Lee 	case MAX98396_R20E0_IV_SENSE_PATH_CFG:
785b5858113SRyan Lee 	case MAX98396_R20E4_IV_SENSE_PATH_EN
786b5858113SRyan Lee 		... MAX98396_R2106_BPE_THRESH_HYSTERESIS:
787b5858113SRyan Lee 	case MAX98396_R2108_BPE_SUPPLY_SRC ... MAX98396_R210B_BPE_LOW_LIMITER:
788b5858113SRyan Lee 	case MAX98396_R210D_BPE_EN ... MAX98396_R210F_GLOBAL_EN:
789b5858113SRyan Lee 	case MAX98396_R21FF_REVISION_ID:
790b5858113SRyan Lee 		return true;
791b5858113SRyan Lee 	default:
792b5858113SRyan Lee 		return false;
793b5858113SRyan Lee 	}
794b5858113SRyan Lee };
795b5858113SRyan Lee 
796b5858113SRyan Lee static bool max98396_volatile_reg(struct device *dev, unsigned int reg)
797b5858113SRyan Lee {
798b5858113SRyan Lee 	switch (reg) {
799b5858113SRyan Lee 	case MAX98396_R2000_SW_RESET:
800b5858113SRyan Lee 	case MAX98396_R2001_INT_RAW1 ... MAX98396_R200E_INT_FLAG4:
801b5858113SRyan Lee 	case MAX98396_R2041_PCM_MODE_CFG:
802b5858113SRyan Lee 	case MAX98396_R20B6_ADC_PVDD_READBACK_MSB
803b5858113SRyan Lee 		... MAX98396_R20BF_ADC_LO_VBAT_READBACK_LSB:
804b5858113SRyan Lee 	case MAX98396_R20E5_BPE_STATE:
805b5858113SRyan Lee 	case MAX98396_R2109_BPE_LOW_STATE
806b5858113SRyan Lee 		... MAX98396_R210B_BPE_LOW_LIMITER:
807b5858113SRyan Lee 	case MAX98396_R210F_GLOBAL_EN:
808b5858113SRyan Lee 	case MAX98396_R21FF_REVISION_ID:
809b5858113SRyan Lee 		return true;
810b5858113SRyan Lee 	default:
811b5858113SRyan Lee 		return false;
812b5858113SRyan Lee 	}
813b5858113SRyan Lee }
814b5858113SRyan Lee 
815b5858113SRyan Lee static bool max98397_readable_register(struct device *dev, unsigned int reg)
816b5858113SRyan Lee {
817b5858113SRyan Lee 	switch (reg) {
818b5858113SRyan Lee 	case MAX98396_R2001_INT_RAW1 ... MAX98396_R2004_INT_RAW4:
819b5858113SRyan Lee 	case MAX98396_R2006_INT_STATE1 ... MAX98396_R2009_INT_STATE4:
820b5858113SRyan Lee 	case MAX98396_R200B_INT_FLAG1 ... MAX98396_R200E_INT_FLAG4:
821b5858113SRyan Lee 	case MAX98396_R2010_INT_EN1 ... MAX98396_R2013_INT_EN4:
822b5858113SRyan Lee 	case MAX98396_R2015_INT_FLAG_CLR1 ... MAX98396_R2018_INT_FLAG_CLR4:
823b5858113SRyan Lee 	case MAX98396_R201F_IRQ_CTRL ... MAX98396_R2024_THERM_FOLDBACK_SET:
824b5858113SRyan Lee 	case MAX98396_R2027_THERM_FOLDBACK_EN:
825b5858113SRyan Lee 	case MAX98396_R2030_NOISEGATE_MODE_CTRL:
826b5858113SRyan Lee 	case MAX98396_R2033_NOISEGATE_MODE_EN:
827b5858113SRyan Lee 	case MAX98396_R2038_CLK_MON_CTRL ... MAX98397_R203A_SPK_MON_THRESH:
828b5858113SRyan Lee 	case MAX98396_R203F_ENABLE_CTRLS ... MAX98397_R2054_PCM_TX_HIZ_CTRL_8:
829b5858113SRyan Lee 	case MAX98397_R2056_PCM_RX_SRC1... MAX98396_R2058_PCM_BYPASS_SRC:
830b5858113SRyan Lee 	case MAX98396_R205D_PCM_TX_SRC_EN ... MAX98397_R2060_PCM_TX_SUPPLY_SEL:
831b5858113SRyan Lee 	case MAX98396_R2070_ICC_RX_EN_A... MAX98396_R2072_ICC_TX_CTRL:
832b5858113SRyan Lee 	case MAX98396_R207F_ICC_EN:
833b5858113SRyan Lee 	case MAX98396_R2083_TONE_GEN_DC_CFG ... MAX98396_R2086_TONE_GEN_DC_LVL3:
834b5858113SRyan Lee 	case MAX98396_R208F_TONE_GEN_EN ... MAX98396_R209F_BYPASS_PATH_CFG:
835b5858113SRyan Lee 	case MAX98396_R20AF_AMP_EN ... MAX98397_R20C5_MEAS_ADC_OPTIMAL_MODE:
836b5858113SRyan Lee 	case MAX98396_R20C7_ADC_CFG:
837b5858113SRyan Lee 	case MAX98396_R20D0_DHT_CFG1 ... MAX98396_R20D6_DHT_HYSTERESIS_CFG:
838b5858113SRyan Lee 	case MAX98396_R20DF_DHT_EN:
839b5858113SRyan Lee 	case MAX98396_R20E0_IV_SENSE_PATH_CFG:
840b5858113SRyan Lee 	case MAX98396_R20E4_IV_SENSE_PATH_EN
841b5858113SRyan Lee 		... MAX98396_R2106_BPE_THRESH_HYSTERESIS:
842b5858113SRyan Lee 	case MAX98396_R2108_BPE_SUPPLY_SRC ... MAX98396_R210B_BPE_LOW_LIMITER:
843b5858113SRyan Lee 	case MAX98396_R210D_BPE_EN ... MAX98396_R210F_GLOBAL_EN:
844b5858113SRyan Lee 	case MAX98397_R22FF_REVISION_ID:
845b5858113SRyan Lee 		return true;
846b5858113SRyan Lee 	default:
847b5858113SRyan Lee 		return false;
848b5858113SRyan Lee 	}
849b5858113SRyan Lee };
850b5858113SRyan Lee 
851b5858113SRyan Lee static bool max98397_volatile_reg(struct device *dev, unsigned int reg)
852b5858113SRyan Lee {
853b5858113SRyan Lee 	switch (reg) {
854b5858113SRyan Lee 	case MAX98396_R2001_INT_RAW1 ... MAX98396_R200E_INT_FLAG4:
855b5858113SRyan Lee 	case MAX98396_R2041_PCM_MODE_CFG:
856b5858113SRyan Lee 	case MAX98397_R20B7_ADC_PVDD_READBACK_MSB
857b5858113SRyan Lee 		... MAX98397_R20C4_ADC_LO_VDDH_READBACK_LSB:
858b5858113SRyan Lee 	case MAX98396_R20E5_BPE_STATE:
859b5858113SRyan Lee 	case MAX98396_R2109_BPE_LOW_STATE
860b5858113SRyan Lee 		... MAX98396_R210B_BPE_LOW_LIMITER:
861b5858113SRyan Lee 	case MAX98396_R210F_GLOBAL_EN:
862b5858113SRyan Lee 	case MAX98397_R22FF_REVISION_ID:
863b5858113SRyan Lee 		return true;
864b5858113SRyan Lee 	default:
865b5858113SRyan Lee 		return false;
866b5858113SRyan Lee 	}
867b5858113SRyan Lee }
868b5858113SRyan Lee 
869b5858113SRyan Lee static const char * const max98396_op_mod_text[] = {
870b5858113SRyan Lee 	"DG", "PVDD", "VBAT",
871b5858113SRyan Lee };
872b5858113SRyan Lee 
873b5858113SRyan Lee static SOC_ENUM_SINGLE_DECL(max98396_op_mod_enum,
874b5858113SRyan Lee 			    MAX98396_R2098_SPK_CLS_DG_MODE,
875b5858113SRyan Lee 			    0, max98396_op_mod_text);
876b5858113SRyan Lee 
877b5858113SRyan Lee static DECLARE_TLV_DB_SCALE(max98396_digital_tlv, -6350, 50, 1);
878b5858113SRyan Lee static const DECLARE_TLV_DB_RANGE(max98396_spk_tlv,
879b5858113SRyan Lee 	0, 0x11, TLV_DB_SCALE_ITEM(400, 100, 0),
880b5858113SRyan Lee );
881b5858113SRyan Lee static DECLARE_TLV_DB_RANGE(max98397_digital_tlv,
882b5858113SRyan Lee 	0, 0x4A, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
883b5858113SRyan Lee 	0x4B, 0xFF, TLV_DB_SCALE_ITEM(-9000, 50, 0),
884b5858113SRyan Lee );
885b5858113SRyan Lee static const DECLARE_TLV_DB_RANGE(max98397_spk_tlv,
886b5858113SRyan Lee 	0, 0x15, TLV_DB_SCALE_ITEM(600, 100, 0),
887b5858113SRyan Lee );
888b5858113SRyan Lee 
889b5858113SRyan Lee static int max98396_mux_get(struct snd_kcontrol *kcontrol,
890b5858113SRyan Lee 			    struct snd_ctl_elem_value *ucontrol)
891b5858113SRyan Lee {
892b5858113SRyan Lee 	struct snd_soc_component *component =
893b5858113SRyan Lee 		snd_soc_dapm_kcontrol_component(kcontrol);
894b5858113SRyan Lee 	struct max98396_priv *max98396 = snd_soc_component_get_drvdata(component);
895b5858113SRyan Lee 	int reg, val;
896b5858113SRyan Lee 
897b5858113SRyan Lee 	if (max98396->device_id == CODEC_TYPE_MAX98396)
898b5858113SRyan Lee 		reg = MAX98396_R2055_PCM_RX_SRC1;
899b5858113SRyan Lee 	else
900b5858113SRyan Lee 		reg = MAX98397_R2056_PCM_RX_SRC1;
901b5858113SRyan Lee 
902b5858113SRyan Lee 	regmap_read(max98396->regmap, reg, &val);
903b5858113SRyan Lee 
904b5858113SRyan Lee 	ucontrol->value.enumerated.item[0] = val;
905b5858113SRyan Lee 
906b5858113SRyan Lee 	return 0;
907b5858113SRyan Lee }
908b5858113SRyan Lee 
909b5858113SRyan Lee static int max98396_mux_put(struct snd_kcontrol *kcontrol,
910b5858113SRyan Lee 			    struct snd_ctl_elem_value *ucontrol)
911b5858113SRyan Lee {
912b5858113SRyan Lee 	struct snd_soc_component *component =
913b5858113SRyan Lee 		snd_soc_dapm_kcontrol_component(kcontrol);
914b5858113SRyan Lee 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
915b5858113SRyan Lee 	struct max98396_priv *max98396 = snd_soc_component_get_drvdata(component);
916b5858113SRyan Lee 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
917b5858113SRyan Lee 	unsigned int *item = ucontrol->value.enumerated.item;
918b5858113SRyan Lee 	int reg, val;
919b5858113SRyan Lee 	int change;
920b5858113SRyan Lee 
921b5858113SRyan Lee 	if (item[0] >= e->items)
922b5858113SRyan Lee 		return -EINVAL;
923b5858113SRyan Lee 
924b5858113SRyan Lee 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
925b5858113SRyan Lee 
926b5858113SRyan Lee 	if (max98396->device_id == CODEC_TYPE_MAX98396)
927b5858113SRyan Lee 		reg = MAX98396_R2055_PCM_RX_SRC1;
928b5858113SRyan Lee 	else
929b5858113SRyan Lee 		reg = MAX98397_R2056_PCM_RX_SRC1;
930b5858113SRyan Lee 
931b5858113SRyan Lee 	change = snd_soc_component_test_bits(component, reg,
932b5858113SRyan Lee 					     MAX98396_PCM_RX_MASK, val);
933b5858113SRyan Lee 
934b5858113SRyan Lee 	if (change)
935b5858113SRyan Lee 		regmap_update_bits(max98396->regmap, reg,
936b5858113SRyan Lee 				   MAX98396_PCM_RX_MASK, val);
937b5858113SRyan Lee 
938b5858113SRyan Lee 	snd_soc_dapm_mux_update_power(dapm, kcontrol, item[0], e, NULL);
939b5858113SRyan Lee 
940b5858113SRyan Lee 	return change;
941b5858113SRyan Lee }
942b5858113SRyan Lee 
943b5858113SRyan Lee static const char * const max98396_switch_text[] = {
944b5858113SRyan Lee 	"Left", "Right", "LeftRight"};
945b5858113SRyan Lee 
946b5858113SRyan Lee static SOC_ENUM_SINGLE_DECL(dai_sel_enum, SND_SOC_NOPM, 0,
947b5858113SRyan Lee 			    max98396_switch_text);
948b5858113SRyan Lee 
949b5858113SRyan Lee static const struct snd_kcontrol_new max98396_dai_mux =
950b5858113SRyan Lee 	SOC_DAPM_ENUM_EXT("DAI Sel Mux", dai_sel_enum,
951b5858113SRyan Lee 			  max98396_mux_get, max98396_mux_put);
952b5858113SRyan Lee 
953b5858113SRyan Lee static const struct snd_kcontrol_new max98396_vi_control =
954b5858113SRyan Lee 	SOC_DAPM_SINGLE("Switch", MAX98396_R205F_PCM_TX_EN, 0, 1, 0);
955b5858113SRyan Lee 
956b5858113SRyan Lee static const struct snd_soc_dapm_widget max98396_dapm_widgets[] = {
957b5858113SRyan Lee 	SND_SOC_DAPM_DAC_E("Amp Enable", "HiFi Playback",
958b5858113SRyan Lee 			   MAX98396_R20AF_AMP_EN, 0, 0, max98396_dac_event,
959b5858113SRyan Lee 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
960b5858113SRyan Lee 	SND_SOC_DAPM_MUX("DAI Sel Mux", SND_SOC_NOPM, 0, 0,
961b5858113SRyan Lee 			 &max98396_dai_mux),
962b5858113SRyan Lee 	SND_SOC_DAPM_OUTPUT("BE_OUT"),
963b5858113SRyan Lee 	SND_SOC_DAPM_AIF_OUT("Voltage Sense", "HiFi Capture", 0,
964b5858113SRyan Lee 			     MAX98396_R20E4_IV_SENSE_PATH_EN, 0, 0),
965b5858113SRyan Lee 	SND_SOC_DAPM_AIF_OUT("Current Sense", "HiFi Capture", 0,
966b5858113SRyan Lee 			     MAX98396_R20E4_IV_SENSE_PATH_EN, 1, 0),
967b5858113SRyan Lee 	SND_SOC_DAPM_SWITCH("VI Sense", SND_SOC_NOPM, 0, 0,
968b5858113SRyan Lee 			    &max98396_vi_control),
969b5858113SRyan Lee 	SND_SOC_DAPM_SIGGEN("VMON"),
970b5858113SRyan Lee 	SND_SOC_DAPM_SIGGEN("IMON"),
971b5858113SRyan Lee 	SND_SOC_DAPM_SIGGEN("FBMON"),
972b5858113SRyan Lee };
973b5858113SRyan Lee 
974b5858113SRyan Lee static const char * const max98396_thermal_thresh_text[] = {
975b5858113SRyan Lee 	"50C", "51C", "52C", "53C", "54C", "55C", "56C", "57C",
976b5858113SRyan Lee 	"58C", "59C", "60C", "61C", "62C", "63C", "64C", "65C",
977b5858113SRyan Lee 	"66C", "67C", "68C", "69C", "70C", "71C", "72C", "73C",
978b5858113SRyan Lee 	"74C", "75C", "76C", "77C", "78C", "79C", "80C", "81C",
979b5858113SRyan Lee 	"82C", "83C", "84C", "85C", "86C", "87C", "88C", "89C",
980b5858113SRyan Lee 	"90C", "91C", "92C", "93C", "94C", "95C", "96C", "97C",
981b5858113SRyan Lee 	"98C", "99C", "100C", "101C", "102C", "103C", "104C", "105C",
982b5858113SRyan Lee 	"106C", "107C", "108C", "109C", "110C", "111C", "112C", "113C",
983b5858113SRyan Lee 	"114C", "115C", "116C", "117C", "118C", "119C", "120C", "121C",
984b5858113SRyan Lee 	"122C", "123C", "124C", "125C", "126C", "127C", "128C", "129C",
985b5858113SRyan Lee 	"130C", "131C", "132C", "133C", "134C", "135C", "136C", "137C",
986b5858113SRyan Lee 	"138C", "139C", "140C", "141C", "142C", "143C", "144C", "145C",
987b5858113SRyan Lee 	"146C", "147C", "148C", "149C", "150C"
988b5858113SRyan Lee };
989b5858113SRyan Lee 
990b5858113SRyan Lee static SOC_ENUM_SINGLE_DECL(max98396_thermal_warn_thresh1_enum,
991b5858113SRyan Lee 			    MAX98396_R2020_THERM_WARN_THRESH, 0,
992b5858113SRyan Lee 			    max98396_thermal_thresh_text);
993b5858113SRyan Lee 
994b5858113SRyan Lee static SOC_ENUM_SINGLE_DECL(max98396_thermal_warn_thresh2_enum,
995b5858113SRyan Lee 			    MAX98396_R2021_THERM_WARN_THRESH2, 0,
996b5858113SRyan Lee 			    max98396_thermal_thresh_text);
997b5858113SRyan Lee 
998b5858113SRyan Lee static SOC_ENUM_SINGLE_DECL(max98396_thermal_shdn_thresh_enum,
999b5858113SRyan Lee 			    MAX98396_R2022_THERM_SHDN_THRESH, 0,
1000b5858113SRyan Lee 			    max98396_thermal_thresh_text);
1001b5858113SRyan Lee 
1002b5858113SRyan Lee static const char * const max98396_thermal_hyteresis_text[] = {
1003b5858113SRyan Lee 	"2C", "5C", "7C", "10C"
1004b5858113SRyan Lee };
1005b5858113SRyan Lee 
1006b5858113SRyan Lee static SOC_ENUM_SINGLE_DECL(max98396_thermal_hysteresis_enum,
1007b5858113SRyan Lee 			    MAX98396_R2023_THERM_HYSTERESIS, 0,
1008b5858113SRyan Lee 			    max98396_thermal_hyteresis_text);
1009b5858113SRyan Lee 
1010b5858113SRyan Lee static const char * const max98396_foldback_slope_text[] = {
1011b5858113SRyan Lee 	"0.25", "0.5", "1.0", "2.0"
1012b5858113SRyan Lee };
1013b5858113SRyan Lee 
1014b5858113SRyan Lee static SOC_ENUM_SINGLE_DECL(max98396_thermal_fb_slope1_enum,
1015b5858113SRyan Lee 			    MAX98396_R2024_THERM_FOLDBACK_SET,
1016b5858113SRyan Lee 			    MAX98396_THERM_FB_SLOPE1_SHIFT,
1017b5858113SRyan Lee 			    max98396_foldback_slope_text);
1018b5858113SRyan Lee 
1019b5858113SRyan Lee static SOC_ENUM_SINGLE_DECL(max98396_thermal_fb_slope2_enum,
1020b5858113SRyan Lee 			    MAX98396_R2024_THERM_FOLDBACK_SET,
1021b5858113SRyan Lee 			    MAX98396_THERM_FB_SLOPE2_SHIFT,
1022b5858113SRyan Lee 			    max98396_foldback_slope_text);
1023b5858113SRyan Lee 
1024b5858113SRyan Lee static const char * const max98396_foldback_reltime_text[] = {
1025b5858113SRyan Lee 	"3ms", "10ms", "100ms", "300ms"
1026b5858113SRyan Lee };
1027b5858113SRyan Lee 
1028b5858113SRyan Lee static SOC_ENUM_SINGLE_DECL(max98396_thermal_fb_reltime_enum,
1029b5858113SRyan Lee 			    MAX98396_R2024_THERM_FOLDBACK_SET,
1030b5858113SRyan Lee 			    MAX98396_THERM_FB_REL_SHIFT,
1031b5858113SRyan Lee 			    max98396_foldback_reltime_text);
1032b5858113SRyan Lee 
1033b5858113SRyan Lee static const char * const max98396_foldback_holdtime_text[] = {
1034b5858113SRyan Lee 	"0ms", "20ms", "40ms", "80ms"
1035b5858113SRyan Lee };
1036b5858113SRyan Lee 
1037b5858113SRyan Lee static SOC_ENUM_SINGLE_DECL(max98396_thermal_fb_holdtime_enum,
1038b5858113SRyan Lee 			    MAX98396_R2024_THERM_FOLDBACK_SET,
1039b5858113SRyan Lee 			    MAX98396_THERM_FB_HOLD_SHIFT,
1040b5858113SRyan Lee 			    max98396_foldback_holdtime_text);
1041b5858113SRyan Lee 
1042b5858113SRyan Lee static int max98396_adc_value_get(struct snd_kcontrol *kcontrol,
1043b5858113SRyan Lee 				  struct snd_ctl_elem_value *ucontrol)
1044b5858113SRyan Lee {
1045b5858113SRyan Lee 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1046b5858113SRyan Lee 	struct soc_mixer_control *mc =
1047b5858113SRyan Lee 		(struct soc_mixer_control *)kcontrol->private_value;
1048b5858113SRyan Lee 	struct max98396_priv *max98396 = snd_soc_component_get_drvdata(component);
1049b5858113SRyan Lee 	int ret;
1050b5858113SRyan Lee 	u8 val[2];
1051b5858113SRyan Lee 	int reg = mc->reg;
1052b5858113SRyan Lee 
1053b5858113SRyan Lee 	/* ADC value is not available if the device is powered down */
1054b5858113SRyan Lee 	if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
1055b5858113SRyan Lee 		goto exit;
1056b5858113SRyan Lee 
1057b5858113SRyan Lee 	if (max98396->device_id == CODEC_TYPE_MAX98397) {
1058b5858113SRyan Lee 		switch (mc->reg) {
1059b5858113SRyan Lee 		case MAX98396_R20B6_ADC_PVDD_READBACK_MSB:
1060b5858113SRyan Lee 			reg = MAX98397_R20B7_ADC_PVDD_READBACK_MSB;
1061b5858113SRyan Lee 			break;
1062b5858113SRyan Lee 		case MAX98396_R20B8_ADC_VBAT_READBACK_MSB:
1063b5858113SRyan Lee 			reg = MAX98397_R20B9_ADC_VBAT_READBACK_MSB;
1064b5858113SRyan Lee 			break;
1065b5858113SRyan Lee 		case MAX98396_R20BA_ADC_TEMP_READBACK_MSB:
1066b5858113SRyan Lee 			reg = MAX98397_R20BB_ADC_TEMP_READBACK_MSB;
1067b5858113SRyan Lee 			break;
1068b5858113SRyan Lee 		default:
1069b5858113SRyan Lee 			goto exit;
1070b5858113SRyan Lee 		}
1071b5858113SRyan Lee 	}
1072b5858113SRyan Lee 
1073b5858113SRyan Lee 	ret = regmap_raw_read(max98396->regmap, reg, &val, 2);
1074b5858113SRyan Lee 	if (ret)
1075b5858113SRyan Lee 		goto exit;
1076b5858113SRyan Lee 
1077b5858113SRyan Lee 	/* ADC readback bits[8:0] rearrangement */
1078b5858113SRyan Lee 	ucontrol->value.integer.value[0] = (val[0] << 1) | (val[1] & 1);
1079b5858113SRyan Lee 	return 0;
1080b5858113SRyan Lee 
1081b5858113SRyan Lee exit:
1082b5858113SRyan Lee 	ucontrol->value.integer.value[0] = 0;
1083b5858113SRyan Lee 	return 0;
1084b5858113SRyan Lee }
1085b5858113SRyan Lee 
1086b5858113SRyan Lee static const struct snd_kcontrol_new max98396_snd_controls[] = {
1087b5858113SRyan Lee 	/* Volume */
1088b5858113SRyan Lee 	SOC_SINGLE_TLV("Digital Volume", MAX98396_R2090_AMP_VOL_CTRL,
1089b5858113SRyan Lee 		       0, 0x7F, 1, max98396_digital_tlv),
1090b5858113SRyan Lee 	SOC_SINGLE_TLV("Speaker Volume", MAX98396_R2091_AMP_PATH_GAIN,
1091b5858113SRyan Lee 		       0, 0x11, 0, max98396_spk_tlv),
1092b5858113SRyan Lee 	/* Volume Ramp Up/Down Enable*/
1093b5858113SRyan Lee 	SOC_SINGLE("Ramp Up Switch", MAX98396_R2092_AMP_DSP_CFG,
1094b5858113SRyan Lee 		   MAX98396_DSP_SPK_VOL_RMPUP_SHIFT, 1, 0),
1095b5858113SRyan Lee 	SOC_SINGLE("Ramp Down Switch", MAX98396_R2092_AMP_DSP_CFG,
1096b5858113SRyan Lee 		   MAX98396_DSP_SPK_VOL_RMPDN_SHIFT, 1, 0),
1097b5858113SRyan Lee 	/* Clock Monitor Enable */
1098b5858113SRyan Lee 	SOC_SINGLE("CLK Monitor Switch", MAX98396_R203F_ENABLE_CTRLS,
1099b5858113SRyan Lee 		   MAX98396_CTRL_CMON_EN_SHIFT, 1, 0),
1100b5858113SRyan Lee 	/* Dither Enable */
1101b5858113SRyan Lee 	SOC_SINGLE("Dither Switch", MAX98396_R2092_AMP_DSP_CFG,
1102b5858113SRyan Lee 		   MAX98396_DSP_SPK_DITH_EN_SHIFT, 1, 0),
1103b5858113SRyan Lee 	SOC_SINGLE("IV Dither Switch", MAX98396_R20E0_IV_SENSE_PATH_CFG,
1104b5858113SRyan Lee 		   MAX98396_IV_SENSE_DITH_EN_SHIFT, 1, 0),
1105b5858113SRyan Lee 	/* DC Blocker Enable */
1106b5858113SRyan Lee 	SOC_SINGLE("DC Blocker Switch", MAX98396_R2092_AMP_DSP_CFG,
1107b5858113SRyan Lee 		   MAX98396_DSP_SPK_DCBLK_EN_SHIFT, 1, 0),
1108b5858113SRyan Lee 	SOC_SINGLE("IV DC Blocker Switch", MAX98396_R20E0_IV_SENSE_PATH_CFG,
1109b5858113SRyan Lee 		   MAX98396_IV_SENSE_DCBLK_EN_SHIFT, 3, 0),
1110b5858113SRyan Lee 	/* Speaker Safe Mode Enable */
1111b5858113SRyan Lee 	SOC_SINGLE("Safe Mode Switch", MAX98396_R2092_AMP_DSP_CFG,
1112b5858113SRyan Lee 		   MAX98396_DSP_SPK_SAFE_EN_SHIFT, 1, 0),
1113b5858113SRyan Lee 	/* Wideband Filter Enable */
1114b5858113SRyan Lee 	SOC_SINGLE("WB Filter Switch", MAX98396_R2092_AMP_DSP_CFG,
1115b5858113SRyan Lee 		   MAX98396_DSP_SPK_WB_FLT_EN_SHIFT, 1, 0),
1116b5858113SRyan Lee 	SOC_SINGLE("IV WB Filter Switch", MAX98396_R20E0_IV_SENSE_PATH_CFG,
1117b5858113SRyan Lee 		   MAX98396_IV_SENSE_WB_FLT_EN_SHIFT, 1, 0),
1118b5858113SRyan Lee 	/* Dynamic Headroom Tracking */
1119b5858113SRyan Lee 	SOC_SINGLE("DHT Switch", MAX98396_R20DF_DHT_EN, 0, 1, 0),
1120b5858113SRyan Lee 	/* Brownout Protection Engine */
1121b5858113SRyan Lee 	SOC_SINGLE("BPE Switch", MAX98396_R210D_BPE_EN, 0, 1, 0),
1122b5858113SRyan Lee 	SOC_SINGLE("BPE Limiter Switch", MAX98396_R210D_BPE_EN, 1, 1, 0),
1123b5858113SRyan Lee 	/* Bypass Path Enable */
1124b5858113SRyan Lee 	SOC_SINGLE("Bypass Path Switch",
1125b5858113SRyan Lee 		   MAX98396_R205E_PCM_RX_EN, 1, 1, 0),
1126b5858113SRyan Lee 	/* Speaker Operation Mode */
1127b5858113SRyan Lee 	SOC_ENUM("OP Mode", max98396_op_mod_enum),
1128b5858113SRyan Lee 	/* Auto Restart functions */
1129b5858113SRyan Lee 	SOC_SINGLE("CMON Auto Restart Switch", MAX98396_R2038_CLK_MON_CTRL,
1130b5858113SRyan Lee 		   MAX98396_CLK_MON_AUTO_RESTART_SHIFT, 1, 0),
1131b5858113SRyan Lee 	SOC_SINGLE("PVDD Auto Restart Switch", MAX98396_R210E_AUTO_RESTART,
1132b5858113SRyan Lee 		   MAX98396_PVDD_UVLO_RESTART_SHFT, 1, 0),
1133b5858113SRyan Lee 	SOC_SINGLE("VBAT Auto Restart Switch", MAX98396_R210E_AUTO_RESTART,
1134b5858113SRyan Lee 		   MAX98396_VBAT_UVLO_RESTART_SHFT, 1, 0),
1135b5858113SRyan Lee 	SOC_SINGLE("THERM Auto Restart Switch", MAX98396_R210E_AUTO_RESTART,
1136b5858113SRyan Lee 		   MAX98396_THEM_SHDN_RESTART_SHFT, 1, 0),
1137b5858113SRyan Lee 	SOC_SINGLE("OVC Auto Restart Switch", MAX98396_R210E_AUTO_RESTART,
1138b5858113SRyan Lee 		   MAX98396_OVC_RESTART_SHFT, 1, 0),
1139b5858113SRyan Lee 	/* Thermal Threshold */
1140b5858113SRyan Lee 	SOC_ENUM("THERM Thresh1", max98396_thermal_warn_thresh1_enum),
1141b5858113SRyan Lee 	SOC_ENUM("THERM Thresh2", max98396_thermal_warn_thresh2_enum),
1142b5858113SRyan Lee 	SOC_ENUM("THERM SHDN Thresh", max98396_thermal_shdn_thresh_enum),
1143b5858113SRyan Lee 	SOC_ENUM("THERM Hysteresis", max98396_thermal_hysteresis_enum),
1144b5858113SRyan Lee 	SOC_SINGLE("THERM Foldback Switch",
1145b5858113SRyan Lee 		   MAX98396_R2027_THERM_FOLDBACK_EN, 0, 1, 0),
1146b5858113SRyan Lee 	SOC_ENUM("THERM Slope1", max98396_thermal_fb_slope1_enum),
1147b5858113SRyan Lee 	SOC_ENUM("THERM Slope2", max98396_thermal_fb_slope2_enum),
1148b5858113SRyan Lee 	SOC_ENUM("THERM Release", max98396_thermal_fb_reltime_enum),
1149b5858113SRyan Lee 	SOC_ENUM("THERM Hold", max98396_thermal_fb_holdtime_enum),
1150b5858113SRyan Lee 	/* ADC */
1151b5858113SRyan Lee 	SOC_SINGLE_EXT("ADC PVDD", MAX98396_R20B6_ADC_PVDD_READBACK_MSB, 0, 0x1FF, 0,
1152b5858113SRyan Lee 		       max98396_adc_value_get, NULL),
1153b5858113SRyan Lee 	SOC_SINGLE_EXT("ADC VBAT", MAX98396_R20B8_ADC_VBAT_READBACK_MSB, 0, 0x1FF, 0,
1154b5858113SRyan Lee 		       max98396_adc_value_get, NULL),
1155b5858113SRyan Lee 	SOC_SINGLE_EXT("ADC TEMP", MAX98396_R20BA_ADC_TEMP_READBACK_MSB, 0, 0x1FF, 0,
1156b5858113SRyan Lee 		       max98396_adc_value_get, NULL),
1157b5858113SRyan Lee };
1158b5858113SRyan Lee 
1159b5858113SRyan Lee static const struct snd_kcontrol_new max98397_snd_controls[] = {
1160b5858113SRyan Lee 	/* Volume */
1161b5858113SRyan Lee 	SOC_SINGLE_TLV("Digital Volume", MAX98396_R2090_AMP_VOL_CTRL,
1162b5858113SRyan Lee 		       0, 0xFF, 1, max98397_digital_tlv),
1163b5858113SRyan Lee 	SOC_SINGLE_TLV("Speaker Volume", MAX98396_R2091_AMP_PATH_GAIN,
1164b5858113SRyan Lee 		       0, 0x15, 0, max98397_spk_tlv),
1165b5858113SRyan Lee 	/* Volume Ramp Up/Down Enable*/
1166b5858113SRyan Lee 	SOC_SINGLE("Ramp Up Switch", MAX98396_R2092_AMP_DSP_CFG,
1167b5858113SRyan Lee 		   MAX98396_DSP_SPK_VOL_RMPUP_SHIFT, 1, 0),
1168b5858113SRyan Lee 	SOC_SINGLE("Ramp Down Switch", MAX98396_R2092_AMP_DSP_CFG,
1169b5858113SRyan Lee 		   MAX98396_DSP_SPK_VOL_RMPDN_SHIFT, 1, 0),
1170b5858113SRyan Lee 	/* Clock Monitor Enable */
1171b5858113SRyan Lee 	SOC_SINGLE("CLK Monitor Switch", MAX98396_R203F_ENABLE_CTRLS,
1172b5858113SRyan Lee 		   MAX98396_CTRL_CMON_EN_SHIFT, 1, 0),
1173b5858113SRyan Lee 	/* Dither Enable */
1174b5858113SRyan Lee 	SOC_SINGLE("Dither Switch", MAX98396_R2092_AMP_DSP_CFG,
1175b5858113SRyan Lee 		   MAX98396_DSP_SPK_DITH_EN_SHIFT, 1, 0),
1176b5858113SRyan Lee 	SOC_SINGLE("IV Dither Switch", MAX98396_R20E0_IV_SENSE_PATH_CFG,
1177b5858113SRyan Lee 		   MAX98396_IV_SENSE_DITH_EN_SHIFT, 1, 0),
1178b5858113SRyan Lee 	/* DC Blocker Enable */
1179b5858113SRyan Lee 	SOC_SINGLE("DC Blocker Switch", MAX98396_R2092_AMP_DSP_CFG,
1180b5858113SRyan Lee 		   MAX98396_DSP_SPK_DCBLK_EN_SHIFT, 1, 0),
1181b5858113SRyan Lee 	SOC_SINGLE("IV DC Blocker Switch", MAX98396_R20E0_IV_SENSE_PATH_CFG,
1182b5858113SRyan Lee 		   MAX98396_IV_SENSE_DCBLK_EN_SHIFT, 3, 0),
1183b5858113SRyan Lee 	/* Speaker Safe Mode Enable */
1184b5858113SRyan Lee 	SOC_SINGLE("Safe Mode Switch", MAX98396_R2092_AMP_DSP_CFG,
1185b5858113SRyan Lee 		   MAX98396_DSP_SPK_SAFE_EN_SHIFT, 1, 0),
1186b5858113SRyan Lee 	/* Wideband Filter Enable */
1187b5858113SRyan Lee 	SOC_SINGLE("WB Filter Switch", MAX98396_R2092_AMP_DSP_CFG,
1188b5858113SRyan Lee 		   MAX98396_DSP_SPK_WB_FLT_EN_SHIFT, 1, 0),
1189b5858113SRyan Lee 	SOC_SINGLE("IV WB Filter Switch", MAX98396_R20E0_IV_SENSE_PATH_CFG,
1190b5858113SRyan Lee 		   MAX98396_IV_SENSE_WB_FLT_EN_SHIFT, 1, 0),
1191b5858113SRyan Lee 	/* Dynamic Headroom Tracking */
1192b5858113SRyan Lee 	SOC_SINGLE("DHT Switch", MAX98396_R20DF_DHT_EN, 0, 1, 0),
1193b5858113SRyan Lee 	/* Brownout Protection Engine */
1194b5858113SRyan Lee 	SOC_SINGLE("BPE Switch", MAX98396_R210D_BPE_EN, 0, 1, 0),
1195b5858113SRyan Lee 	SOC_SINGLE("BPE Limiter Switch", MAX98396_R210D_BPE_EN, 1, 1, 0),
1196b5858113SRyan Lee 	/* Bypass Path Enable */
1197b5858113SRyan Lee 	SOC_SINGLE("Bypass Path Switch",
1198b5858113SRyan Lee 		   MAX98396_R205E_PCM_RX_EN, 1, 1, 0),
1199b5858113SRyan Lee 	/* Speaker Operation Mode */
1200b5858113SRyan Lee 	SOC_ENUM("OP Mode", max98396_op_mod_enum),
1201b5858113SRyan Lee 	/* Auto Restart functions */
1202b5858113SRyan Lee 	SOC_SINGLE("CMON Auto Restart Switch", MAX98396_R2038_CLK_MON_CTRL,
1203b5858113SRyan Lee 		   MAX98396_CLK_MON_AUTO_RESTART_SHIFT, 1, 0),
1204b5858113SRyan Lee 	SOC_SINGLE("PVDD Auto Restart Switch", MAX98396_R210E_AUTO_RESTART,
1205b5858113SRyan Lee 		   MAX98396_PVDD_UVLO_RESTART_SHFT, 1, 0),
1206b5858113SRyan Lee 	SOC_SINGLE("VBAT Auto Restart Switch", MAX98396_R210E_AUTO_RESTART,
1207b5858113SRyan Lee 		   MAX98396_VBAT_UVLO_RESTART_SHFT, 1, 0),
1208b5858113SRyan Lee 	SOC_SINGLE("THERM Auto Restart Switch", MAX98396_R210E_AUTO_RESTART,
1209b5858113SRyan Lee 		   MAX98396_THEM_SHDN_RESTART_SHFT, 1, 0),
1210b5858113SRyan Lee 	SOC_SINGLE("OVC Auto Restart Switch", MAX98396_R210E_AUTO_RESTART,
1211b5858113SRyan Lee 		   MAX98396_OVC_RESTART_SHFT, 1, 0),
1212b5858113SRyan Lee 	/* Thermal Threshold */
1213b5858113SRyan Lee 	SOC_ENUM("THERM Thresh1", max98396_thermal_warn_thresh1_enum),
1214b5858113SRyan Lee 	SOC_ENUM("THERM Thresh2", max98396_thermal_warn_thresh2_enum),
1215b5858113SRyan Lee 	SOC_ENUM("THERM SHDN Thresh", max98396_thermal_shdn_thresh_enum),
1216b5858113SRyan Lee 	SOC_ENUM("THERM Hysteresis", max98396_thermal_hysteresis_enum),
1217b5858113SRyan Lee 	SOC_SINGLE("THERM Foldback Switch",
1218b5858113SRyan Lee 		   MAX98396_R2027_THERM_FOLDBACK_EN, 0, 1, 0),
1219b5858113SRyan Lee 	SOC_ENUM("THERM Slope1", max98396_thermal_fb_slope1_enum),
1220b5858113SRyan Lee 	SOC_ENUM("THERM Slope2", max98396_thermal_fb_slope2_enum),
1221b5858113SRyan Lee 	SOC_ENUM("THERM Release", max98396_thermal_fb_reltime_enum),
1222b5858113SRyan Lee 	SOC_ENUM("THERM Hold", max98396_thermal_fb_holdtime_enum),
1223b5858113SRyan Lee 	/* ADC */
1224b5858113SRyan Lee 	SOC_SINGLE_EXT("ADC PVDD", MAX98396_R20B6_ADC_PVDD_READBACK_MSB, 0, 0x1FF, 0,
1225b5858113SRyan Lee 		       max98396_adc_value_get, NULL),
1226b5858113SRyan Lee 	SOC_SINGLE_EXT("ADC VBAT", MAX98396_R20B8_ADC_VBAT_READBACK_MSB, 0, 0x1FF, 0,
1227b5858113SRyan Lee 		       max98396_adc_value_get, NULL),
1228b5858113SRyan Lee 	SOC_SINGLE_EXT("ADC TEMP", MAX98396_R20BA_ADC_TEMP_READBACK_MSB, 0, 0x1FF, 0,
1229b5858113SRyan Lee 		       max98396_adc_value_get, NULL),
1230b5858113SRyan Lee };
1231b5858113SRyan Lee 
1232b5858113SRyan Lee static const struct snd_soc_dapm_route max98396_audio_map[] = {
1233b5858113SRyan Lee 	/* Plabyack */
1234b5858113SRyan Lee 	{"DAI Sel Mux", "Left", "Amp Enable"},
1235b5858113SRyan Lee 	{"DAI Sel Mux", "Right", "Amp Enable"},
1236b5858113SRyan Lee 	{"DAI Sel Mux", "LeftRight", "Amp Enable"},
1237b5858113SRyan Lee 	{"BE_OUT", NULL, "DAI Sel Mux"},
1238b5858113SRyan Lee 	/* Capture */
1239b5858113SRyan Lee 	{ "VI Sense", "Switch", "VMON" },
1240b5858113SRyan Lee 	{ "VI Sense", "Switch", "IMON" },
1241b5858113SRyan Lee 	{ "Voltage Sense", NULL, "VI Sense" },
1242b5858113SRyan Lee 	{ "Current Sense", NULL, "VI Sense" },
1243b5858113SRyan Lee };
1244b5858113SRyan Lee 
1245b5858113SRyan Lee static struct snd_soc_dai_driver max98396_dai[] = {
1246b5858113SRyan Lee 	{
1247b5858113SRyan Lee 		.name = "max98396-aif1",
1248b5858113SRyan Lee 		.playback = {
1249b5858113SRyan Lee 			.stream_name = "HiFi Playback",
1250b5858113SRyan Lee 			.channels_min = 1,
1251b5858113SRyan Lee 			.channels_max = 2,
1252b5858113SRyan Lee 			.rates = MAX98396_RATES,
1253b5858113SRyan Lee 			.formats = MAX98396_FORMATS,
1254b5858113SRyan Lee 		},
1255b5858113SRyan Lee 		.capture = {
1256b5858113SRyan Lee 			.stream_name = "HiFi Capture",
1257b5858113SRyan Lee 			.channels_min = 1,
1258b5858113SRyan Lee 			.channels_max = 2,
1259b5858113SRyan Lee 			.rates = MAX98396_RATES,
1260b5858113SRyan Lee 			.formats = MAX98396_FORMATS,
1261b5858113SRyan Lee 		},
1262b5858113SRyan Lee 		.ops = &max98396_dai_ops,
1263b5858113SRyan Lee 	}
1264b5858113SRyan Lee };
1265b5858113SRyan Lee 
1266b5858113SRyan Lee static struct snd_soc_dai_driver max98397_dai[] = {
1267b5858113SRyan Lee 	{
1268b5858113SRyan Lee 		.name = "max98397-aif1",
1269b5858113SRyan Lee 		.playback = {
1270b5858113SRyan Lee 			.stream_name = "HiFi Playback",
1271b5858113SRyan Lee 			.channels_min = 1,
1272b5858113SRyan Lee 			.channels_max = 2,
1273b5858113SRyan Lee 			.rates = MAX98396_RATES,
1274b5858113SRyan Lee 			.formats = MAX98396_FORMATS,
1275b5858113SRyan Lee 		},
1276b5858113SRyan Lee 		.capture = {
1277b5858113SRyan Lee 			.stream_name = "HiFi Capture",
1278b5858113SRyan Lee 			.channels_min = 1,
1279b5858113SRyan Lee 			.channels_max = 2,
1280b5858113SRyan Lee 			.rates = MAX98396_RATES,
1281b5858113SRyan Lee 			.formats = MAX98396_FORMATS,
1282b5858113SRyan Lee 		},
1283b5858113SRyan Lee 		.ops = &max98396_dai_ops,
1284b5858113SRyan Lee 	}
1285b5858113SRyan Lee };
1286b5858113SRyan Lee 
1287b5858113SRyan Lee static void max98396_reset(struct max98396_priv *max98396, struct device *dev)
1288b5858113SRyan Lee {
1289b5858113SRyan Lee 	int ret, reg, count;
1290b5858113SRyan Lee 
1291b5858113SRyan Lee 	/* Software Reset */
1292b5858113SRyan Lee 	ret = regmap_write(max98396->regmap,
1293b5858113SRyan Lee 			   MAX98396_R2000_SW_RESET, 1);
1294b5858113SRyan Lee 	if (ret)
1295b5858113SRyan Lee 		dev_err(dev, "Reset command failed. (ret:%d)\n", ret);
1296b5858113SRyan Lee 
1297b5858113SRyan Lee 	count = 0;
1298b5858113SRyan Lee 	while (count < 3) {
1299b5858113SRyan Lee 		usleep_range(5000, 6000);
1300b5858113SRyan Lee 		/* Software Reset Verification */
1301b5858113SRyan Lee 		ret = regmap_read(max98396->regmap,
1302b5858113SRyan Lee 				  GET_REG_ADDR_REV_ID(max98396->device_id), &reg);
1303b5858113SRyan Lee 		if (!ret) {
1304b5858113SRyan Lee 			dev_info(dev, "Reset completed (retry:%d)\n", count);
1305b5858113SRyan Lee 			return;
1306b5858113SRyan Lee 		}
1307b5858113SRyan Lee 		count++;
1308b5858113SRyan Lee 	}
1309b5858113SRyan Lee 	dev_err(dev, "Reset failed. (ret:%d)\n", ret);
1310b5858113SRyan Lee }
1311b5858113SRyan Lee 
1312b5858113SRyan Lee static int max98396_probe(struct snd_soc_component *component)
1313b5858113SRyan Lee {
1314b5858113SRyan Lee 	struct max98396_priv *max98396 =
1315b5858113SRyan Lee 		snd_soc_component_get_drvdata(component);
1316b5858113SRyan Lee 
1317b5858113SRyan Lee 	/* Software Reset */
1318b5858113SRyan Lee 	max98396_reset(max98396, component->dev);
1319b5858113SRyan Lee 
1320b5858113SRyan Lee 	/* L/R mix configuration */
1321b5858113SRyan Lee 	if (max98396->device_id == CODEC_TYPE_MAX98396) {
1322b5858113SRyan Lee 		regmap_write(max98396->regmap,
1323b5858113SRyan Lee 			     MAX98396_R2055_PCM_RX_SRC1, 0x02);
1324b5858113SRyan Lee 		regmap_write(max98396->regmap,
1325b5858113SRyan Lee 			     MAX98396_R2056_PCM_RX_SRC2, 0x10);
1326b5858113SRyan Lee 	} else {
1327b5858113SRyan Lee 		regmap_write(max98396->regmap,
1328b5858113SRyan Lee 			     MAX98397_R2056_PCM_RX_SRC1, 0x02);
1329b5858113SRyan Lee 		regmap_write(max98396->regmap,
1330b5858113SRyan Lee 			     MAX98397_R2057_PCM_RX_SRC2, 0x10);
1331b5858113SRyan Lee 	}
1332b5858113SRyan Lee 	/* Enable DC blocker */
1333b5858113SRyan Lee 	regmap_update_bits(max98396->regmap,
1334b5858113SRyan Lee 			   MAX98396_R2092_AMP_DSP_CFG, 1, 1);
1335b5858113SRyan Lee 	/* Enable IV Monitor DC blocker */
1336b5858113SRyan Lee 	regmap_update_bits(max98396->regmap,
1337b5858113SRyan Lee 			   MAX98396_R20E0_IV_SENSE_PATH_CFG,
1338b5858113SRyan Lee 			   MAX98396_IV_SENSE_DCBLK_EN_MASK,
1339b5858113SRyan Lee 			   MAX98396_IV_SENSE_DCBLK_EN_MASK);
1340b5858113SRyan Lee 	/* Configure default data output sources */
1341b5858113SRyan Lee 	regmap_write(max98396->regmap,
1342b5858113SRyan Lee 		     MAX98396_R205D_PCM_TX_SRC_EN, 3);
1343b5858113SRyan Lee 	/* Enable Wideband Filter */
1344b5858113SRyan Lee 	regmap_update_bits(max98396->regmap,
1345b5858113SRyan Lee 			   MAX98396_R2092_AMP_DSP_CFG, 0x40, 0x40);
1346b5858113SRyan Lee 	/* Enable IV Wideband Filter */
1347b5858113SRyan Lee 	regmap_update_bits(max98396->regmap,
1348b5858113SRyan Lee 			   MAX98396_R20E0_IV_SENSE_PATH_CFG, 8, 8);
1349b5858113SRyan Lee 
1350b5858113SRyan Lee 	/* Enable Bypass Source */
1351b5858113SRyan Lee 	regmap_write(max98396->regmap,
1352b5858113SRyan Lee 		     MAX98396_R2058_PCM_BYPASS_SRC,
1353b5858113SRyan Lee 		     max98396->bypass_slot);
1354b5858113SRyan Lee 	/* Voltage, current slot configuration */
1355b5858113SRyan Lee 	regmap_write(max98396->regmap,
1356b5858113SRyan Lee 		     MAX98396_R2044_PCM_TX_CTRL_1,
1357b5858113SRyan Lee 		     max98396->v_slot);
1358b5858113SRyan Lee 	regmap_write(max98396->regmap,
1359b5858113SRyan Lee 		     MAX98396_R2045_PCM_TX_CTRL_2,
1360b5858113SRyan Lee 		     max98396->i_slot);
1361b5858113SRyan Lee 
1362b5858113SRyan Lee 	if (max98396->v_slot < 8)
1363b5858113SRyan Lee 		if (max98396->device_id == CODEC_TYPE_MAX98396)
1364b5858113SRyan Lee 			regmap_update_bits(max98396->regmap,
1365b5858113SRyan Lee 					   MAX98396_R2053_PCM_TX_HIZ_CTRL_8,
1366b5858113SRyan Lee 					   1 << max98396->v_slot, 0);
1367b5858113SRyan Lee 		else
1368b5858113SRyan Lee 			regmap_update_bits(max98396->regmap,
1369b5858113SRyan Lee 					   MAX98397_R2054_PCM_TX_HIZ_CTRL_8,
1370b5858113SRyan Lee 					   1 << max98396->v_slot, 0);
1371b5858113SRyan Lee 	else
1372b5858113SRyan Lee 		if (max98396->device_id == CODEC_TYPE_MAX98396)
1373b5858113SRyan Lee 			regmap_update_bits(max98396->regmap,
1374b5858113SRyan Lee 					   MAX98396_R2052_PCM_TX_HIZ_CTRL_7,
1375b5858113SRyan Lee 					   1 << (max98396->v_slot - 8), 0);
1376b5858113SRyan Lee 		else
1377b5858113SRyan Lee 			regmap_update_bits(max98396->regmap,
1378b5858113SRyan Lee 					   MAX98397_R2053_PCM_TX_HIZ_CTRL_7,
1379b5858113SRyan Lee 					   1 << (max98396->v_slot - 8), 0);
1380b5858113SRyan Lee 
1381b5858113SRyan Lee 	if (max98396->i_slot < 8)
1382b5858113SRyan Lee 		if (max98396->device_id == CODEC_TYPE_MAX98396)
1383b5858113SRyan Lee 			regmap_update_bits(max98396->regmap,
1384b5858113SRyan Lee 					   MAX98396_R2053_PCM_TX_HIZ_CTRL_8,
1385b5858113SRyan Lee 					   1 << max98396->i_slot, 0);
1386b5858113SRyan Lee 		else
1387b5858113SRyan Lee 			regmap_update_bits(max98396->regmap,
1388b5858113SRyan Lee 					   MAX98397_R2054_PCM_TX_HIZ_CTRL_8,
1389b5858113SRyan Lee 					   1 << max98396->i_slot, 0);
1390b5858113SRyan Lee 	else
1391b5858113SRyan Lee 		if (max98396->device_id == CODEC_TYPE_MAX98396)
1392b5858113SRyan Lee 			regmap_update_bits(max98396->regmap,
1393b5858113SRyan Lee 					   MAX98396_R2052_PCM_TX_HIZ_CTRL_7,
1394b5858113SRyan Lee 					   1 << (max98396->i_slot - 8), 0);
1395b5858113SRyan Lee 		else
1396b5858113SRyan Lee 			regmap_update_bits(max98396->regmap,
1397b5858113SRyan Lee 					   MAX98397_R2053_PCM_TX_HIZ_CTRL_7,
1398b5858113SRyan Lee 					   1 << (max98396->i_slot - 8), 0);
1399b5858113SRyan Lee 
1400b5858113SRyan Lee 	/* Set interleave mode */
1401b5858113SRyan Lee 	if (max98396->interleave_mode)
1402b5858113SRyan Lee 		regmap_update_bits(max98396->regmap,
1403b5858113SRyan Lee 				   MAX98396_R2041_PCM_MODE_CFG,
1404b5858113SRyan Lee 				   MAX98396_PCM_TX_CH_INTERLEAVE_MASK,
1405b5858113SRyan Lee 				   MAX98396_PCM_TX_CH_INTERLEAVE_MASK);
1406b5858113SRyan Lee 
1407b5858113SRyan Lee 	regmap_update_bits(max98396->regmap,
1408b5858113SRyan Lee 			   MAX98396_R2038_CLK_MON_CTRL,
1409b5858113SRyan Lee 			   MAX98396_CLK_MON_AUTO_RESTART_MASK,
1410b5858113SRyan Lee 			   MAX98396_CLK_MON_AUTO_RESTART_MASK);
1411b5858113SRyan Lee 
1412b5858113SRyan Lee 	/* Speaker Amplifier PCM RX Enable by default */
1413b5858113SRyan Lee 	regmap_update_bits(max98396->regmap,
1414b5858113SRyan Lee 			   MAX98396_R205E_PCM_RX_EN,
1415b5858113SRyan Lee 			   MAX98396_PCM_RX_EN_MASK, 1);
1416b5858113SRyan Lee 
1417b5858113SRyan Lee 	return 0;
1418b5858113SRyan Lee }
1419b5858113SRyan Lee 
1420b5858113SRyan Lee #ifdef CONFIG_PM_SLEEP
1421b5858113SRyan Lee static int max98396_suspend(struct device *dev)
1422b5858113SRyan Lee {
1423b5858113SRyan Lee 	struct max98396_priv *max98396 = dev_get_drvdata(dev);
1424b5858113SRyan Lee 
1425b5858113SRyan Lee 	regcache_cache_only(max98396->regmap, true);
1426b5858113SRyan Lee 	regcache_mark_dirty(max98396->regmap);
1427b5858113SRyan Lee 	return 0;
1428b5858113SRyan Lee }
1429b5858113SRyan Lee 
1430b5858113SRyan Lee static int max98396_resume(struct device *dev)
1431b5858113SRyan Lee {
1432b5858113SRyan Lee 	struct max98396_priv *max98396 = dev_get_drvdata(dev);
1433b5858113SRyan Lee 
1434b5858113SRyan Lee 	regcache_cache_only(max98396->regmap, false);
1435b5858113SRyan Lee 	max98396_reset(max98396, dev);
1436b5858113SRyan Lee 	regcache_sync(max98396->regmap);
1437b5858113SRyan Lee 	return 0;
1438b5858113SRyan Lee }
1439b5858113SRyan Lee #endif
1440b5858113SRyan Lee 
1441b5858113SRyan Lee static const struct dev_pm_ops max98396_pm = {
1442b5858113SRyan Lee 	SET_SYSTEM_SLEEP_PM_OPS(max98396_suspend, max98396_resume)
1443b5858113SRyan Lee };
1444b5858113SRyan Lee 
1445b5858113SRyan Lee static const struct snd_soc_component_driver soc_codec_dev_max98396 = {
1446b5858113SRyan Lee 	.probe			= max98396_probe,
1447b5858113SRyan Lee 	.controls		= max98396_snd_controls,
1448b5858113SRyan Lee 	.num_controls		= ARRAY_SIZE(max98396_snd_controls),
1449b5858113SRyan Lee 	.dapm_widgets		= max98396_dapm_widgets,
1450b5858113SRyan Lee 	.num_dapm_widgets	= ARRAY_SIZE(max98396_dapm_widgets),
1451b5858113SRyan Lee 	.dapm_routes		= max98396_audio_map,
1452b5858113SRyan Lee 	.num_dapm_routes	= ARRAY_SIZE(max98396_audio_map),
1453b5858113SRyan Lee 	.idle_bias_on		= 1,
1454b5858113SRyan Lee 	.use_pmdown_time	= 1,
1455b5858113SRyan Lee 	.endianness		= 1,
1456b5858113SRyan Lee 	.non_legacy_dai_naming	= 1,
1457b5858113SRyan Lee };
1458b5858113SRyan Lee 
1459b5858113SRyan Lee static const struct snd_soc_component_driver soc_codec_dev_max98397 = {
1460b5858113SRyan Lee 	.probe			= max98396_probe,
1461b5858113SRyan Lee 	.controls		= max98397_snd_controls,
1462b5858113SRyan Lee 	.num_controls		= ARRAY_SIZE(max98397_snd_controls),
1463b5858113SRyan Lee 	.dapm_widgets		= max98396_dapm_widgets,
1464b5858113SRyan Lee 	.num_dapm_widgets	= ARRAY_SIZE(max98396_dapm_widgets),
1465b5858113SRyan Lee 	.dapm_routes		= max98396_audio_map,
1466b5858113SRyan Lee 	.num_dapm_routes	= ARRAY_SIZE(max98396_audio_map),
1467b5858113SRyan Lee 	.idle_bias_on		= 1,
1468b5858113SRyan Lee 	.use_pmdown_time	= 1,
1469b5858113SRyan Lee 	.endianness		= 1,
1470b5858113SRyan Lee 	.non_legacy_dai_naming	= 1,
1471b5858113SRyan Lee };
1472b5858113SRyan Lee 
1473b5858113SRyan Lee static const struct regmap_config max98396_regmap = {
1474b5858113SRyan Lee 	.reg_bits = 16,
1475b5858113SRyan Lee 	.val_bits = 8,
1476b5858113SRyan Lee 	.max_register = MAX98396_R21FF_REVISION_ID,
1477b5858113SRyan Lee 	.reg_defaults  = max98396_reg,
1478b5858113SRyan Lee 	.num_reg_defaults = ARRAY_SIZE(max98396_reg),
1479b5858113SRyan Lee 	.readable_reg = max98396_readable_register,
1480b5858113SRyan Lee 	.volatile_reg = max98396_volatile_reg,
1481b5858113SRyan Lee 	.cache_type = REGCACHE_RBTREE,
1482b5858113SRyan Lee };
1483b5858113SRyan Lee 
1484b5858113SRyan Lee static const struct regmap_config max98397_regmap = {
1485b5858113SRyan Lee 	.reg_bits = 16,
1486b5858113SRyan Lee 	.val_bits = 8,
1487b5858113SRyan Lee 	.max_register = MAX98397_R22FF_REVISION_ID,
1488b5858113SRyan Lee 	.reg_defaults  = max98397_reg,
1489b5858113SRyan Lee 	.num_reg_defaults = ARRAY_SIZE(max98397_reg),
1490b5858113SRyan Lee 	.readable_reg = max98397_readable_register,
1491b5858113SRyan Lee 	.volatile_reg = max98397_volatile_reg,
1492b5858113SRyan Lee 	.cache_type = REGCACHE_RBTREE,
1493b5858113SRyan Lee };
1494b5858113SRyan Lee 
1495b5858113SRyan Lee static void max98396_read_device_property(struct device *dev,
1496b5858113SRyan Lee 					  struct max98396_priv *max98396)
1497b5858113SRyan Lee {
1498b5858113SRyan Lee 	int value;
1499b5858113SRyan Lee 
1500b5858113SRyan Lee 	if (!device_property_read_u32(dev, "adi,vmon-slot-no", &value))
1501b5858113SRyan Lee 		max98396->v_slot = value & 0xF;
1502b5858113SRyan Lee 	else
1503b5858113SRyan Lee 		max98396->v_slot = 0;
1504b5858113SRyan Lee 
1505b5858113SRyan Lee 	if (!device_property_read_u32(dev, "adi,imon-slot-no", &value))
1506b5858113SRyan Lee 		max98396->i_slot = value & 0xF;
1507b5858113SRyan Lee 	else
1508b5858113SRyan Lee 		max98396->i_slot = 1;
1509b5858113SRyan Lee 
1510b5858113SRyan Lee 	if (!device_property_read_u32(dev, "adi,bypass-slot-no", &value))
1511b5858113SRyan Lee 		max98396->bypass_slot = value & 0xF;
1512b5858113SRyan Lee 	else
1513b5858113SRyan Lee 		max98396->bypass_slot = 0;
1514b5858113SRyan Lee }
1515b5858113SRyan Lee 
1516b5858113SRyan Lee static int max98396_i2c_probe(struct i2c_client *i2c,
1517b5858113SRyan Lee 			      const struct i2c_device_id *id)
1518b5858113SRyan Lee {
1519b5858113SRyan Lee 	struct max98396_priv *max98396 = NULL;
1520b5858113SRyan Lee 	int ret, reg;
1521b5858113SRyan Lee 
1522b5858113SRyan Lee 	max98396 = devm_kzalloc(&i2c->dev, sizeof(*max98396), GFP_KERNEL);
1523b5858113SRyan Lee 
1524b5858113SRyan Lee 	if (!max98396) {
1525b5858113SRyan Lee 		ret = -ENOMEM;
1526b5858113SRyan Lee 		return ret;
1527b5858113SRyan Lee 	}
1528b5858113SRyan Lee 	i2c_set_clientdata(i2c, max98396);
1529b5858113SRyan Lee 
1530b5858113SRyan Lee 	max98396->device_id =  id->driver_data;
1531b5858113SRyan Lee 
1532b5858113SRyan Lee 	/* regmap initialization */
1533b5858113SRyan Lee 	if (max98396->device_id == CODEC_TYPE_MAX98396)
1534b5858113SRyan Lee 		max98396->regmap = devm_regmap_init_i2c(i2c, &max98396_regmap);
1535b5858113SRyan Lee 
1536b5858113SRyan Lee 	else
1537b5858113SRyan Lee 		max98396->regmap = devm_regmap_init_i2c(i2c, &max98397_regmap);
1538b5858113SRyan Lee 
1539b5858113SRyan Lee 	if (IS_ERR(max98396->regmap)) {
1540b5858113SRyan Lee 		ret = PTR_ERR(max98396->regmap);
1541b5858113SRyan Lee 		dev_err(&i2c->dev,
1542b5858113SRyan Lee 			"Failed to allocate regmap: %d\n", ret);
1543b5858113SRyan Lee 		return ret;
1544b5858113SRyan Lee 	}
1545b5858113SRyan Lee 
1546b5858113SRyan Lee 	/* update interleave mode info */
1547b5858113SRyan Lee 	if (device_property_read_bool(&i2c->dev, "adi,interleave_mode"))
1548b5858113SRyan Lee 		max98396->interleave_mode = true;
1549b5858113SRyan Lee 	else
1550b5858113SRyan Lee 		max98396->interleave_mode = false;
1551b5858113SRyan Lee 
1552b5858113SRyan Lee 	/* voltage/current slot & gpio configuration */
1553b5858113SRyan Lee 	max98396_read_device_property(&i2c->dev, max98396);
1554b5858113SRyan Lee 
1555b5858113SRyan Lee 	/* Reset the Device */
1556b5858113SRyan Lee 	max98396->reset_gpio = devm_gpiod_get_optional(&i2c->dev,
1557b5858113SRyan Lee 						       "reset", GPIOD_OUT_HIGH);
1558b5858113SRyan Lee 	if (IS_ERR(max98396->reset_gpio)) {
1559b5858113SRyan Lee 		ret = PTR_ERR(max98396->reset_gpio);
1560b5858113SRyan Lee 		dev_err(&i2c->dev, "Unable to request GPIO pin: %d.\n", ret);
1561b5858113SRyan Lee 		return ret;
1562b5858113SRyan Lee 	}
1563b5858113SRyan Lee 
1564b5858113SRyan Lee 	if (max98396->reset_gpio) {
1565b5858113SRyan Lee 		usleep_range(5000, 6000);
1566b5858113SRyan Lee 		gpiod_set_value_cansleep(max98396->reset_gpio, 0);
1567b5858113SRyan Lee 		/* Wait for the hw reset done */
1568b5858113SRyan Lee 		usleep_range(5000, 6000);
1569b5858113SRyan Lee 	}
1570b5858113SRyan Lee 
1571b5858113SRyan Lee 	ret = regmap_read(max98396->regmap,
1572b5858113SRyan Lee 			  GET_REG_ADDR_REV_ID(max98396->device_id), &reg);
1573b5858113SRyan Lee 	if (ret < 0) {
1574b5858113SRyan Lee 		dev_err(&i2c->dev, "%s: failed to read revision of the device.\n",  id->name);
1575b5858113SRyan Lee 		return ret;
1576b5858113SRyan Lee 	}
1577b5858113SRyan Lee 	dev_info(&i2c->dev, "%s revision ID: 0x%02X\n", id->name, reg);
1578b5858113SRyan Lee 
1579b5858113SRyan Lee 	/* codec registration */
1580b5858113SRyan Lee 	if (max98396->device_id == CODEC_TYPE_MAX98396)
1581b5858113SRyan Lee 		ret = devm_snd_soc_register_component(&i2c->dev,
1582b5858113SRyan Lee 						      &soc_codec_dev_max98396,
1583b5858113SRyan Lee 						      max98396_dai,
1584b5858113SRyan Lee 						      ARRAY_SIZE(max98396_dai));
1585b5858113SRyan Lee 	else
1586b5858113SRyan Lee 		ret = devm_snd_soc_register_component(&i2c->dev,
1587b5858113SRyan Lee 						      &soc_codec_dev_max98397,
1588b5858113SRyan Lee 						      max98397_dai,
1589b5858113SRyan Lee 						      ARRAY_SIZE(max98397_dai));
1590b5858113SRyan Lee 	if (ret < 0)
1591b5858113SRyan Lee 		dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
1592b5858113SRyan Lee 
1593b5858113SRyan Lee 	return ret;
1594b5858113SRyan Lee }
1595b5858113SRyan Lee 
1596b5858113SRyan Lee static const struct i2c_device_id max98396_i2c_id[] = {
1597b5858113SRyan Lee 	{ "max98396", CODEC_TYPE_MAX98396},
1598b5858113SRyan Lee 	{ "max98397", CODEC_TYPE_MAX98397},
1599b5858113SRyan Lee 	{ },
1600b5858113SRyan Lee };
1601b5858113SRyan Lee 
1602b5858113SRyan Lee MODULE_DEVICE_TABLE(i2c, max98396_i2c_id);
1603b5858113SRyan Lee 
1604b5858113SRyan Lee #if defined(CONFIG_OF)
1605b5858113SRyan Lee static const struct of_device_id max98396_of_match[] = {
1606b5858113SRyan Lee 	{ .compatible = "adi,max98396", },
1607b5858113SRyan Lee 	{ .compatible = "adi,max98397", },
1608b5858113SRyan Lee 	{ }
1609b5858113SRyan Lee };
1610b5858113SRyan Lee MODULE_DEVICE_TABLE(of, max98396_of_match);
1611b5858113SRyan Lee #endif
1612b5858113SRyan Lee 
1613b5858113SRyan Lee #ifdef CONFIG_ACPI
1614b5858113SRyan Lee static const struct acpi_device_id max98396_acpi_match[] = {
1615b5858113SRyan Lee 	{ "ADS8396", 0 },
1616b5858113SRyan Lee 	{ "ADS8397", 0 },
1617b5858113SRyan Lee 	{},
1618b5858113SRyan Lee };
1619b5858113SRyan Lee MODULE_DEVICE_TABLE(acpi, max98396_acpi_match);
1620b5858113SRyan Lee #endif
1621b5858113SRyan Lee 
1622b5858113SRyan Lee static struct i2c_driver max98396_i2c_driver = {
1623b5858113SRyan Lee 	.driver = {
1624b5858113SRyan Lee 		.name = "max98396",
1625b5858113SRyan Lee 		.of_match_table = of_match_ptr(max98396_of_match),
1626b5858113SRyan Lee 		.acpi_match_table = ACPI_PTR(max98396_acpi_match),
1627b5858113SRyan Lee 		.pm = &max98396_pm,
1628b5858113SRyan Lee 	},
1629b5858113SRyan Lee 	.probe = max98396_i2c_probe,
1630b5858113SRyan Lee 	.id_table = max98396_i2c_id,
1631b5858113SRyan Lee };
1632b5858113SRyan Lee 
1633b5858113SRyan Lee module_i2c_driver(max98396_i2c_driver)
1634b5858113SRyan Lee 
1635b5858113SRyan Lee MODULE_DESCRIPTION("ALSA SoC MAX98396 driver");
1636b5858113SRyan Lee MODULE_AUTHOR("Ryan Lee <ryans.lee@analog.com>");
1637b5858113SRyan Lee MODULE_LICENSE("GPL");
1638