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