xref: /linux/sound/soc/codecs/wsa883x.c (revision 3551e679c3eefb7756fc220acf951ad7591ae99c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/bitops.h>
7 #include <linux/device.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/hwmon.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/printk.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/slab.h>
18 #include <linux/soundwire/sdw.h>
19 #include <linux/soundwire/sdw_registers.h>
20 #include <linux/soundwire/sdw_type.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc-dapm.h>
24 #include <sound/soc.h>
25 #include <sound/tlv.h>
26 
27 #define WSA883X_BASE                    0x3000
28 #define WSA883X_ANA_BG_TSADC_BASE       (WSA883X_BASE + 0x00000001)
29 #define WSA883X_REF_CTRL                (WSA883X_ANA_BG_TSADC_BASE + 0x0000)
30 #define WSA883X_TEST_CTL_0              (WSA883X_ANA_BG_TSADC_BASE + 0x0001)
31 #define WSA883X_BIAS_0                  (WSA883X_ANA_BG_TSADC_BASE + 0x0002)
32 #define WSA883X_OP_CTL                  (WSA883X_ANA_BG_TSADC_BASE + 0x0003)
33 #define WSA883X_IREF_CTL                (WSA883X_ANA_BG_TSADC_BASE + 0x0004)
34 #define WSA883X_ISENS_CTL               (WSA883X_ANA_BG_TSADC_BASE + 0x0005)
35 #define WSA883X_CLK_CTL                 (WSA883X_ANA_BG_TSADC_BASE + 0x0006)
36 #define WSA883X_TEST_CTL_1              (WSA883X_ANA_BG_TSADC_BASE + 0x0007)
37 #define WSA883X_BIAS_1                  (WSA883X_ANA_BG_TSADC_BASE + 0x0008)
38 #define WSA883X_ADC_CTL                 (WSA883X_ANA_BG_TSADC_BASE + 0x0009)
39 #define WSA883X_DOUT_MSB                (WSA883X_ANA_BG_TSADC_BASE + 0x000A)
40 #define WSA883X_DOUT_LSB                (WSA883X_ANA_BG_TSADC_BASE + 0x000B)
41 #define WSA883X_VBAT_SNS                (WSA883X_ANA_BG_TSADC_BASE + 0x000C)
42 #define WSA883X_ITRIM_CODE              (WSA883X_ANA_BG_TSADC_BASE + 0x000D)
43 
44 #define WSA883X_ANA_IVSENSE_BASE        (WSA883X_BASE + 0x0000000F)
45 #define WSA883X_EN                      (WSA883X_ANA_IVSENSE_BASE + 0x0000)
46 #define WSA883X_OVERRIDE1               (WSA883X_ANA_IVSENSE_BASE + 0x0001)
47 #define WSA883X_OVERRIDE2               (WSA883X_ANA_IVSENSE_BASE + 0x0002)
48 #define WSA883X_VSENSE1                 (WSA883X_ANA_IVSENSE_BASE + 0x0003)
49 #define WSA883X_ISENSE1                 (WSA883X_ANA_IVSENSE_BASE + 0x0004)
50 #define WSA883X_ISENSE2                 (WSA883X_ANA_IVSENSE_BASE + 0x0005)
51 #define WSA883X_ISENSE_CAL              (WSA883X_ANA_IVSENSE_BASE + 0x0006)
52 #define WSA883X_MISC                    (WSA883X_ANA_IVSENSE_BASE + 0x0007)
53 #define WSA883X_ADC_0                   (WSA883X_ANA_IVSENSE_BASE + 0x0008)
54 #define WSA883X_ADC_1                   (WSA883X_ANA_IVSENSE_BASE + 0x0009)
55 #define WSA883X_ADC_2                   (WSA883X_ANA_IVSENSE_BASE + 0x000A)
56 #define WSA883X_ADC_3                   (WSA883X_ANA_IVSENSE_BASE + 0x000B)
57 #define WSA883X_ADC_4                   (WSA883X_ANA_IVSENSE_BASE + 0x000C)
58 #define WSA883X_ADC_5                   (WSA883X_ANA_IVSENSE_BASE + 0x000D)
59 #define WSA883X_ADC_6                   (WSA883X_ANA_IVSENSE_BASE + 0x000E)
60 #define WSA883X_ADC_7                   (WSA883X_ANA_IVSENSE_BASE + 0x000F)
61 #define WSA883X_STATUS                  (WSA883X_ANA_IVSENSE_BASE + 0x0010)
62 
63 #define WSA883X_ANA_SPK_TOP_BASE        (WSA883X_BASE + 0x00000025)
64 #define WSA883X_DAC_CTRL_REG            (WSA883X_ANA_SPK_TOP_BASE + 0x0000)
65 #define WSA883X_DAC_EN_DEBUG_REG        (WSA883X_ANA_SPK_TOP_BASE + 0x0001)
66 #define WSA883X_DAC_OPAMP_BIAS1_REG     (WSA883X_ANA_SPK_TOP_BASE + 0x0002)
67 #define WSA883X_DAC_OPAMP_BIAS2_REG     (WSA883X_ANA_SPK_TOP_BASE + 0x0003)
68 #define WSA883X_DAC_VCM_CTRL_REG        (WSA883X_ANA_SPK_TOP_BASE + 0x0004)
69 #define WSA883X_DAC_VOLTAGE_CTRL_REG    (WSA883X_ANA_SPK_TOP_BASE + 0x0005)
70 #define WSA883X_ATEST1_REG              (WSA883X_ANA_SPK_TOP_BASE + 0x0006)
71 #define WSA883X_ATEST2_REG              (WSA883X_ANA_SPK_TOP_BASE + 0x0007)
72 #define WSA883X_SPKR_TOP_BIAS_REG1      (WSA883X_ANA_SPK_TOP_BASE + 0x0008)
73 #define WSA883X_SPKR_TOP_BIAS_REG2      (WSA883X_ANA_SPK_TOP_BASE + 0x0009)
74 #define WSA883X_SPKR_TOP_BIAS_REG3      (WSA883X_ANA_SPK_TOP_BASE + 0x000A)
75 #define WSA883X_SPKR_TOP_BIAS_REG4      (WSA883X_ANA_SPK_TOP_BASE + 0x000B)
76 #define WSA883X_SPKR_CLIP_DET_REG       (WSA883X_ANA_SPK_TOP_BASE + 0x000C)
77 #define WSA883X_SPKR_DRV_LF_BLK_EN      (WSA883X_ANA_SPK_TOP_BASE + 0x000D)
78 #define WSA883X_SPKR_DRV_LF_EN          (WSA883X_ANA_SPK_TOP_BASE + 0x000E)
79 #define WSA883X_SPKR_DRV_LF_MASK_DCC_CTL (WSA883X_ANA_SPK_TOP_BASE + 0x000F)
80 #define WSA883X_SPKR_DRV_LF_MISC_CTL    (WSA883X_ANA_SPK_TOP_BASE + 0x0010)
81 #define WSA883X_SPKR_DRV_LF_REG_GAIN    (WSA883X_ANA_SPK_TOP_BASE + 0x0011)
82 #define WSA883X_SPKR_DRV_OS_CAL_CTL     (WSA883X_ANA_SPK_TOP_BASE + 0x0012)
83 #define WSA883X_SPKR_DRV_OS_CAL_CTL1     (WSA883X_ANA_SPK_TOP_BASE + 0x0013)
84 #define WSA883X_SPKR_PWM_CLK_CTL        (WSA883X_ANA_SPK_TOP_BASE + 0x0014)
85 #define WSA883X_SPKR_PWM_FREQ_SEL_MASK	BIT(3)
86 #define WSA883X_SPKR_PWM_FREQ_F300KHZ	0
87 #define WSA883X_SPKR_PWM_FREQ_F600KHZ	1
88 #define WSA883X_SPKR_PDRV_HS_CTL        (WSA883X_ANA_SPK_TOP_BASE + 0x0015)
89 #define WSA883X_SPKR_PDRV_LS_CTL        (WSA883X_ANA_SPK_TOP_BASE + 0x0016)
90 #define WSA883X_SPKR_PWRSTG_DBG         (WSA883X_ANA_SPK_TOP_BASE + 0x0017)
91 #define WSA883X_SPKR_OCP_CTL            (WSA883X_ANA_SPK_TOP_BASE + 0x0018)
92 #define WSA883X_SPKR_BBM_CTL            (WSA883X_ANA_SPK_TOP_BASE + 0x0019)
93 #define WSA883X_PA_STATUS0              (WSA883X_ANA_SPK_TOP_BASE + 0x001A)
94 #define WSA883X_PA_STATUS1              (WSA883X_ANA_SPK_TOP_BASE + 0x001B)
95 #define WSA883X_PA_STATUS2              (WSA883X_ANA_SPK_TOP_BASE + 0x001C)
96 
97 #define WSA883X_ANA_BOOST_BASE          (WSA883X_BASE + 0x00000043)
98 #define WSA883X_EN_CTRL                 (WSA883X_ANA_BOOST_BASE + 0x0000)
99 #define WSA883X_CURRENT_LIMIT           (WSA883X_ANA_BOOST_BASE + 0x0001)
100 #define WSA883X_IBIAS1                  (WSA883X_ANA_BOOST_BASE + 0x0002)
101 #define WSA883X_IBIAS2                  (WSA883X_ANA_BOOST_BASE + 0x0003)
102 #define WSA883X_IBIAS3                  (WSA883X_ANA_BOOST_BASE + 0x0004)
103 #define WSA883X_LDO_PROG                (WSA883X_ANA_BOOST_BASE + 0x0005)
104 #define WSA883X_STABILITY_CTRL1         (WSA883X_ANA_BOOST_BASE + 0x0006)
105 #define WSA883X_STABILITY_CTRL2         (WSA883X_ANA_BOOST_BASE + 0x0007)
106 #define WSA883X_PWRSTAGE_CTRL1          (WSA883X_ANA_BOOST_BASE + 0x0008)
107 #define WSA883X_PWRSTAGE_CTRL2          (WSA883X_ANA_BOOST_BASE + 0x0009)
108 #define WSA883X_BYPASS_1                (WSA883X_ANA_BOOST_BASE + 0x000A)
109 #define WSA883X_BYPASS_2                (WSA883X_ANA_BOOST_BASE + 0x000B)
110 #define WSA883X_ZX_CTRL_1               (WSA883X_ANA_BOOST_BASE + 0x000C)
111 #define WSA883X_ZX_CTRL_2               (WSA883X_ANA_BOOST_BASE + 0x000D)
112 #define WSA883X_MISC1                   (WSA883X_ANA_BOOST_BASE + 0x000E)
113 #define WSA883X_MISC2                   (WSA883X_ANA_BOOST_BASE + 0x000F)
114 #define WSA883X_GMAMP_SUP1              (WSA883X_ANA_BOOST_BASE + 0x0010)
115 #define WSA883X_PWRSTAGE_CTRL3          (WSA883X_ANA_BOOST_BASE + 0x0011)
116 #define WSA883X_PWRSTAGE_CTRL4          (WSA883X_ANA_BOOST_BASE + 0x0012)
117 #define WSA883X_TEST1                   (WSA883X_ANA_BOOST_BASE + 0x0013)
118 #define WSA883X_SPARE1                  (WSA883X_ANA_BOOST_BASE + 0x0014)
119 #define WSA883X_SPARE2                  (WSA883X_ANA_BOOST_BASE + 0x0015)
120 
121 #define WSA883X_ANA_PON_LDOL_BASE       (WSA883X_BASE + 0x00000059)
122 #define WSA883X_PON_CTL_0               (WSA883X_ANA_PON_LDOL_BASE + 0x0000)
123 #define WSA883X_PON_CLT_1               (WSA883X_ANA_PON_LDOL_BASE + 0x0001)
124 #define WSA883X_PON_CTL_2               (WSA883X_ANA_PON_LDOL_BASE + 0x0002)
125 #define WSA883X_PON_CTL_3               (WSA883X_ANA_PON_LDOL_BASE + 0x0003)
126 #define WSA883X_CKWD_CTL_0              (WSA883X_ANA_PON_LDOL_BASE + 0x0004)
127 #define WSA883X_CKWD_CTL_1              (WSA883X_ANA_PON_LDOL_BASE + 0x0005)
128 #define WSA883X_CKWD_CTL_2              (WSA883X_ANA_PON_LDOL_BASE + 0x0006)
129 #define WSA883X_CKSK_CTL_0              (WSA883X_ANA_PON_LDOL_BASE + 0x0007)
130 #define WSA883X_PADSW_CTL_0             (WSA883X_ANA_PON_LDOL_BASE + 0x0008)
131 #define WSA883X_TEST_0                  (WSA883X_ANA_PON_LDOL_BASE + 0x0009)
132 #define WSA883X_TEST_1                  (WSA883X_ANA_PON_LDOL_BASE + 0x000A)
133 #define WSA883X_STATUS_0                (WSA883X_ANA_PON_LDOL_BASE + 0x000B)
134 #define WSA883X_STATUS_1                (WSA883X_ANA_PON_LDOL_BASE + 0x000C)
135 
136 #define WSA883X_DIG_CTRL_BASE           (WSA883X_BASE + 0x00000400)
137 #define WSA883X_CHIP_ID0                (WSA883X_DIG_CTRL_BASE + 0x0001)
138 #define WSA883X_CHIP_ID1                (WSA883X_DIG_CTRL_BASE + 0x0002)
139 #define WSA883X_CHIP_ID2                (WSA883X_DIG_CTRL_BASE + 0x0003)
140 #define WSA883X_CHIP_ID3                (WSA883X_DIG_CTRL_BASE + 0x0004)
141 #define WSA883X_BUS_ID                  (WSA883X_DIG_CTRL_BASE + 0x0005)
142 #define WSA883X_CDC_RST_CTL             (WSA883X_DIG_CTRL_BASE + 0x0006)
143 #define WSA883X_TOP_CLK_CFG             (WSA883X_DIG_CTRL_BASE + 0x0007)
144 #define WSA883X_CDC_PATH_MODE           (WSA883X_DIG_CTRL_BASE + 0x0008)
145 #define WSA883X_RXD_MODE_MASK		BIT(1)
146 #define WSA883X_RXD_MODE_NORMAL		0
147 #define WSA883X_RXD_MODE_HIFI		1
148 #define WSA883X_CDC_CLK_CTL             (WSA883X_DIG_CTRL_BASE + 0x0009)
149 #define WSA883X_SWR_RESET_EN            (WSA883X_DIG_CTRL_BASE + 0x000A)
150 #define WSA883X_RESET_CTL               (WSA883X_DIG_CTRL_BASE + 0x000B)
151 #define WSA883X_PA_FSM_CTL              (WSA883X_DIG_CTRL_BASE + 0x0010)
152 #define WSA883X_GLOBAL_PA_EN_MASK	BIT(0)
153 #define WSA883X_GLOBAL_PA_ENABLE	1
154 #define WSA883X_PA_FSM_TIMER0           (WSA883X_DIG_CTRL_BASE + 0x0011)
155 #define WSA883X_PA_FSM_TIMER1           (WSA883X_DIG_CTRL_BASE + 0x0012)
156 #define WSA883X_PA_FSM_STA              (WSA883X_DIG_CTRL_BASE + 0x0013)
157 #define WSA883X_PA_FSM_ERR_COND         (WSA883X_DIG_CTRL_BASE + 0x0014)
158 #define WSA883X_PA_FSM_MSK              (WSA883X_DIG_CTRL_BASE + 0x0015)
159 #define WSA883X_PA_FSM_BYP              (WSA883X_DIG_CTRL_BASE + 0x0016)
160 #define WSA883X_PA_FSM_BYP_DC_CAL_EN_MASK		0x01
161 #define WSA883X_PA_FSM_BYP_DC_CAL_EN_SHIFT		0
162 #define WSA883X_PA_FSM_BYP_CLK_WD_EN_MASK		0x02
163 #define WSA883X_PA_FSM_BYP_CLK_WD_EN_SHIFT		1
164 #define WSA883X_PA_FSM_BYP_BG_EN_MASK			0x04
165 #define WSA883X_PA_FSM_BYP_BG_EN_SHIFT			2
166 #define WSA883X_PA_FSM_BYP_BOOST_EN_MASK		0x08
167 #define WSA883X_PA_FSM_BYP_BOOST_EN_SHIFT		3
168 #define WSA883X_PA_FSM_BYP_PA_EN_MASK			0x10
169 #define WSA883X_PA_FSM_BYP_PA_EN_SHIFT			4
170 #define WSA883X_PA_FSM_BYP_D_UNMUTE_MASK		0x20
171 #define WSA883X_PA_FSM_BYP_D_UNMUTE_SHIFT		5
172 #define WSA883X_PA_FSM_BYP_SPKR_PROT_EN_MASK		0x40
173 #define WSA883X_PA_FSM_BYP_SPKR_PROT_EN_SHIFT		6
174 #define WSA883X_PA_FSM_BYP_TSADC_EN_MASK		0x80
175 #define WSA883X_PA_FSM_BYP_TSADC_EN_SHIFT		7
176 #define WSA883X_PA_FSM_DBG              (WSA883X_DIG_CTRL_BASE + 0x0017)
177 #define WSA883X_TADC_VALUE_CTL          (WSA883X_DIG_CTRL_BASE + 0x0020)
178 #define WSA883X_TADC_VALUE_CTL_TEMP_VALUE_RD_EN_MASK	0x01
179 #define WSA883X_TADC_VALUE_CTL_TEMP_VALUE_RD_EN_SHIFT	0
180 #define WSA883X_TADC_VALUE_CTL_VBAT_VALUE_RD_EN_MASK	0x02
181 #define WSA883X_TADC_VALUE_CTL_VBAT_VALUE_RD_EN_SHIFT	1
182 #define WSA883X_TEMP_DETECT_CTL         (WSA883X_DIG_CTRL_BASE + 0x0021)
183 #define WSA883X_TEMP_MSB                (WSA883X_DIG_CTRL_BASE + 0x0022)
184 #define WSA883X_TEMP_LSB                (WSA883X_DIG_CTRL_BASE + 0x0023)
185 #define WSA883X_TEMP_CONFIG0            (WSA883X_DIG_CTRL_BASE + 0x0024)
186 #define WSA883X_TEMP_CONFIG1            (WSA883X_DIG_CTRL_BASE + 0x0025)
187 #define WSA883X_VBAT_ADC_FLT_CTL        (WSA883X_DIG_CTRL_BASE + 0x0026)
188 #define WSA883X_VBAT_ADC_FLT_EN_MASK	BIT(0)
189 #define WSA883X_VBAT_ADC_COEF_SEL_MASK	GENMASK(3, 1)
190 #define WSA883X_VBAT_ADC_COEF_F_1DIV2	0x0
191 #define WSA883X_VBAT_ADC_COEF_F_1DIV16	0x3
192 #define WSA883X_VBAT_DIN_MSB            (WSA883X_DIG_CTRL_BASE + 0x0027)
193 #define WSA883X_VBAT_DIN_LSB            (WSA883X_DIG_CTRL_BASE + 0x0028)
194 #define WSA883X_VBAT_DOUT               (WSA883X_DIG_CTRL_BASE + 0x0029)
195 #define WSA883X_SDM_PDM9_LSB            (WSA883X_DIG_CTRL_BASE + 0x002A)
196 #define WSA883X_SDM_PDM9_MSB            (WSA883X_DIG_CTRL_BASE + 0x002B)
197 #define WSA883X_CDC_RX_CTL              (WSA883X_DIG_CTRL_BASE + 0x0030)
198 #define WSA883X_CDC_SPK_DSM_A1_0        (WSA883X_DIG_CTRL_BASE + 0x0031)
199 #define WSA883X_CDC_SPK_DSM_A1_1        (WSA883X_DIG_CTRL_BASE + 0x0032)
200 #define WSA883X_CDC_SPK_DSM_A2_0        (WSA883X_DIG_CTRL_BASE + 0x0033)
201 #define WSA883X_CDC_SPK_DSM_A2_1        (WSA883X_DIG_CTRL_BASE + 0x0034)
202 #define WSA883X_CDC_SPK_DSM_A3_0        (WSA883X_DIG_CTRL_BASE + 0x0035)
203 #define WSA883X_CDC_SPK_DSM_A3_1        (WSA883X_DIG_CTRL_BASE + 0x0036)
204 #define WSA883X_CDC_SPK_DSM_A4_0        (WSA883X_DIG_CTRL_BASE + 0x0037)
205 #define WSA883X_CDC_SPK_DSM_A4_1        (WSA883X_DIG_CTRL_BASE + 0x0038)
206 #define WSA883X_CDC_SPK_DSM_A5_0        (WSA883X_DIG_CTRL_BASE + 0x0039)
207 #define WSA883X_CDC_SPK_DSM_A5_1        (WSA883X_DIG_CTRL_BASE + 0x003A)
208 #define WSA883X_CDC_SPK_DSM_A6_0        (WSA883X_DIG_CTRL_BASE + 0x003B)
209 #define WSA883X_CDC_SPK_DSM_A7_0        (WSA883X_DIG_CTRL_BASE + 0x003C)
210 #define WSA883X_CDC_SPK_DSM_C_0         (WSA883X_DIG_CTRL_BASE + 0x003D)
211 #define WSA883X_CDC_SPK_DSM_C_1         (WSA883X_DIG_CTRL_BASE + 0x003E)
212 #define WSA883X_CDC_SPK_DSM_C_2         (WSA883X_DIG_CTRL_BASE + 0x003F)
213 #define WSA883X_CDC_SPK_DSM_C_3         (WSA883X_DIG_CTRL_BASE + 0x0040)
214 #define WSA883X_CDC_SPK_DSM_R1          (WSA883X_DIG_CTRL_BASE + 0x0041)
215 #define WSA883X_CDC_SPK_DSM_R2          (WSA883X_DIG_CTRL_BASE + 0x0042)
216 #define WSA883X_CDC_SPK_DSM_R3          (WSA883X_DIG_CTRL_BASE + 0x0043)
217 #define WSA883X_CDC_SPK_DSM_R4          (WSA883X_DIG_CTRL_BASE + 0x0044)
218 #define WSA883X_CDC_SPK_DSM_R5          (WSA883X_DIG_CTRL_BASE + 0x0045)
219 #define WSA883X_CDC_SPK_DSM_R6          (WSA883X_DIG_CTRL_BASE + 0x0046)
220 #define WSA883X_CDC_SPK_DSM_R7          (WSA883X_DIG_CTRL_BASE + 0x0047)
221 #define WSA883X_CDC_SPK_GAIN_PDM_0      (WSA883X_DIG_CTRL_BASE + 0x0048)
222 #define WSA883X_CDC_SPK_GAIN_PDM_1      (WSA883X_DIG_CTRL_BASE + 0x0049)
223 #define WSA883X_CDC_SPK_GAIN_PDM_2      (WSA883X_DIG_CTRL_BASE + 0x004A)
224 #define WSA883X_PDM_WD_CTL              (WSA883X_DIG_CTRL_BASE + 0x004B)
225 #define WSA883X_PDM_EN_MASK		BIT(0)
226 #define WSA883X_PDM_ENABLE		BIT(0)
227 #define WSA883X_DEM_BYPASS_DATA0        (WSA883X_DIG_CTRL_BASE + 0x004C)
228 #define WSA883X_DEM_BYPASS_DATA1        (WSA883X_DIG_CTRL_BASE + 0x004D)
229 #define WSA883X_DEM_BYPASS_DATA2        (WSA883X_DIG_CTRL_BASE + 0x004E)
230 #define WSA883X_DEM_BYPASS_DATA3        (WSA883X_DIG_CTRL_BASE + 0x004F)
231 #define WSA883X_WAVG_CTL                (WSA883X_DIG_CTRL_BASE + 0x0050)
232 #define WSA883X_WAVG_LRA_PER_0          (WSA883X_DIG_CTRL_BASE + 0x0051)
233 #define WSA883X_WAVG_LRA_PER_1          (WSA883X_DIG_CTRL_BASE + 0x0052)
234 #define WSA883X_WAVG_DELTA_THETA_0      (WSA883X_DIG_CTRL_BASE + 0x0053)
235 #define WSA883X_WAVG_DELTA_THETA_1      (WSA883X_DIG_CTRL_BASE + 0x0054)
236 #define WSA883X_WAVG_DIRECT_AMP_0       (WSA883X_DIG_CTRL_BASE + 0x0055)
237 #define WSA883X_WAVG_DIRECT_AMP_1       (WSA883X_DIG_CTRL_BASE + 0x0056)
238 #define WSA883X_WAVG_PTRN_AMP0_0        (WSA883X_DIG_CTRL_BASE + 0x0057)
239 #define WSA883X_WAVG_PTRN_AMP0_1        (WSA883X_DIG_CTRL_BASE + 0x0058)
240 #define WSA883X_WAVG_PTRN_AMP1_0        (WSA883X_DIG_CTRL_BASE + 0x0059)
241 #define WSA883X_WAVG_PTRN_AMP1_1        (WSA883X_DIG_CTRL_BASE + 0x005A)
242 #define WSA883X_WAVG_PTRN_AMP2_0        (WSA883X_DIG_CTRL_BASE + 0x005B)
243 #define WSA883X_WAVG_PTRN_AMP2_1        (WSA883X_DIG_CTRL_BASE + 0x005C)
244 #define WSA883X_WAVG_PTRN_AMP3_0        (WSA883X_DIG_CTRL_BASE + 0x005D)
245 #define WSA883X_WAVG_PTRN_AMP3_1        (WSA883X_DIG_CTRL_BASE + 0x005E)
246 #define WSA883X_WAVG_PTRN_AMP4_0        (WSA883X_DIG_CTRL_BASE + 0x005F)
247 #define WSA883X_WAVG_PTRN_AMP4_1        (WSA883X_DIG_CTRL_BASE + 0x0060)
248 #define WSA883X_WAVG_PTRN_AMP5_0        (WSA883X_DIG_CTRL_BASE + 0x0061)
249 #define WSA883X_WAVG_PTRN_AMP5_1        (WSA883X_DIG_CTRL_BASE + 0x0062)
250 #define WSA883X_WAVG_PTRN_AMP6_0        (WSA883X_DIG_CTRL_BASE + 0x0063)
251 #define WSA883X_WAVG_PTRN_AMP6_1        (WSA883X_DIG_CTRL_BASE + 0x0064)
252 #define WSA883X_WAVG_PTRN_AMP7_0        (WSA883X_DIG_CTRL_BASE + 0x0065)
253 #define WSA883X_WAVG_PTRN_AMP7_1        (WSA883X_DIG_CTRL_BASE + 0x0066)
254 #define WSA883X_WAVG_PER_0_1            (WSA883X_DIG_CTRL_BASE + 0x0067)
255 #define WSA883X_WAVG_PER_2_3            (WSA883X_DIG_CTRL_BASE + 0x0068)
256 #define WSA883X_WAVG_PER_4_5            (WSA883X_DIG_CTRL_BASE + 0x0069)
257 #define WSA883X_WAVG_PER_6_7            (WSA883X_DIG_CTRL_BASE + 0x006A)
258 #define WSA883X_WAVG_STA                (WSA883X_DIG_CTRL_BASE + 0x006B)
259 #define WSA883X_DRE_CTL_0               (WSA883X_DIG_CTRL_BASE + 0x006C)
260 #define WSA883X_DRE_OFFSET_MASK		GENMASK(2, 0)
261 #define WSA883X_DRE_PROG_DELAY_MASK	GENMASK(7, 4)
262 #define WSA883X_DRE_CTL_1               (WSA883X_DIG_CTRL_BASE + 0x006D)
263 #define WSA883X_DRE_GAIN_EN_MASK	BIT(0)
264 #define WSA883X_DRE_GAIN_FROM_CSR	1
265 #define WSA883X_DRE_IDLE_DET_CTL        (WSA883X_DIG_CTRL_BASE + 0x006E)
266 #define WSA883X_CLSH_CTL_0              (WSA883X_DIG_CTRL_BASE + 0x0070)
267 #define WSA883X_CLSH_CTL_1              (WSA883X_DIG_CTRL_BASE + 0x0071)
268 #define WSA883X_CLSH_V_HD_PA            (WSA883X_DIG_CTRL_BASE + 0x0072)
269 #define WSA883X_CLSH_V_PA_MIN           (WSA883X_DIG_CTRL_BASE + 0x0073)
270 #define WSA883X_CLSH_OVRD_VAL           (WSA883X_DIG_CTRL_BASE + 0x0074)
271 #define WSA883X_CLSH_HARD_MAX           (WSA883X_DIG_CTRL_BASE + 0x0075)
272 #define WSA883X_CLSH_SOFT_MAX           (WSA883X_DIG_CTRL_BASE + 0x0076)
273 #define WSA883X_CLSH_SIG_DP             (WSA883X_DIG_CTRL_BASE + 0x0077)
274 #define WSA883X_TAGC_CTL                (WSA883X_DIG_CTRL_BASE + 0x0078)
275 #define WSA883X_TAGC_TIME               (WSA883X_DIG_CTRL_BASE + 0x0079)
276 #define WSA883X_TAGC_E2E_GAIN           (WSA883X_DIG_CTRL_BASE + 0x007A)
277 #define WSA883X_TAGC_FORCE_VAL          (WSA883X_DIG_CTRL_BASE + 0x007B)
278 #define WSA883X_VAGC_CTL                (WSA883X_DIG_CTRL_BASE + 0x007C)
279 #define WSA883X_VAGC_TIME               (WSA883X_DIG_CTRL_BASE + 0x007D)
280 #define WSA883X_VAGC_ATTN_LVL_1_2       (WSA883X_DIG_CTRL_BASE + 0x007E)
281 #define WSA883X_VAGC_ATTN_LVL_3         (WSA883X_DIG_CTRL_BASE + 0x007F)
282 #define WSA883X_INTR_MODE               (WSA883X_DIG_CTRL_BASE + 0x0080)
283 #define WSA883X_INTR_MASK0              (WSA883X_DIG_CTRL_BASE + 0x0081)
284 #define WSA883X_INTR_MASK1              (WSA883X_DIG_CTRL_BASE + 0x0082)
285 #define WSA883X_INTR_STATUS0            (WSA883X_DIG_CTRL_BASE + 0x0083)
286 #define WSA883X_INTR_STATUS1            (WSA883X_DIG_CTRL_BASE + 0x0084)
287 #define WSA883X_INTR_CLEAR0             (WSA883X_DIG_CTRL_BASE + 0x0085)
288 #define WSA883X_INTR_CLEAR1             (WSA883X_DIG_CTRL_BASE + 0x0086)
289 #define WSA883X_INTR_LEVEL0             (WSA883X_DIG_CTRL_BASE + 0x0087)
290 #define WSA883X_INTR_LEVEL1             (WSA883X_DIG_CTRL_BASE + 0x0088)
291 #define WSA883X_INTR_SET0               (WSA883X_DIG_CTRL_BASE + 0x0089)
292 #define WSA883X_INTR_SET1               (WSA883X_DIG_CTRL_BASE + 0x008A)
293 #define WSA883X_INTR_TEST0              (WSA883X_DIG_CTRL_BASE + 0x008B)
294 #define WSA883X_INTR_TEST1              (WSA883X_DIG_CTRL_BASE + 0x008C)
295 #define WSA883X_OTP_CTRL0               (WSA883X_DIG_CTRL_BASE + 0x0090)
296 #define WSA883X_OTP_CTRL1               (WSA883X_DIG_CTRL_BASE + 0x0091)
297 #define WSA883X_HDRIVE_CTL_GROUP1       (WSA883X_DIG_CTRL_BASE + 0x0092)
298 #define WSA883X_PIN_CTL                 (WSA883X_DIG_CTRL_BASE + 0x0093)
299 #define WSA883X_PIN_CTL_OE              (WSA883X_DIG_CTRL_BASE + 0x0094)
300 #define WSA883X_PIN_WDATA_IOPAD         (WSA883X_DIG_CTRL_BASE + 0x0095)
301 #define WSA883X_PIN_STATUS              (WSA883X_DIG_CTRL_BASE + 0x0096)
302 #define WSA883X_I2C_SLAVE_CTL           (WSA883X_DIG_CTRL_BASE + 0x0097)
303 #define WSA883X_PDM_TEST_MODE           (WSA883X_DIG_CTRL_BASE + 0x00A0)
304 #define WSA883X_ATE_TEST_MODE           (WSA883X_DIG_CTRL_BASE + 0x00A1)
305 #define WSA883X_DIG_DEBUG_MODE          (WSA883X_DIG_CTRL_BASE + 0x00A3)
306 #define WSA883X_DIG_DEBUG_SEL           (WSA883X_DIG_CTRL_BASE + 0x00A4)
307 #define WSA883X_DIG_DEBUG_EN            (WSA883X_DIG_CTRL_BASE + 0x00A5)
308 #define WSA883X_SWR_HM_TEST0            (WSA883X_DIG_CTRL_BASE + 0x00A6)
309 #define WSA883X_SWR_HM_TEST1            (WSA883X_DIG_CTRL_BASE + 0x00A7)
310 #define WSA883X_SWR_PAD_CTL             (WSA883X_DIG_CTRL_BASE + 0x00A8)
311 #define WSA883X_TADC_DETECT_DBG_CTL     (WSA883X_DIG_CTRL_BASE + 0x00A9)
312 #define WSA883X_TADC_DEBUG_MSB          (WSA883X_DIG_CTRL_BASE + 0x00AA)
313 #define WSA883X_TADC_DEBUG_LSB          (WSA883X_DIG_CTRL_BASE + 0x00AB)
314 #define WSA883X_SAMPLE_EDGE_SEL         (WSA883X_DIG_CTRL_BASE + 0x00AC)
315 #define WSA883X_SWR_EDGE_SEL            (WSA883X_DIG_CTRL_BASE + 0x00AD)
316 #define WSA883X_TEST_MODE_CTL           (WSA883X_DIG_CTRL_BASE + 0x00AE)
317 #define WSA883X_IOPAD_CTL               (WSA883X_DIG_CTRL_BASE + 0x00AF)
318 #define WSA883X_ANA_CSR_DBG_ADD         (WSA883X_DIG_CTRL_BASE + 0x00B0)
319 #define WSA883X_ANA_CSR_DBG_CTL         (WSA883X_DIG_CTRL_BASE + 0x00B1)
320 #define WSA883X_SPARE_R                 (WSA883X_DIG_CTRL_BASE + 0x00BC)
321 #define WSA883X_SPARE_0                 (WSA883X_DIG_CTRL_BASE + 0x00BD)
322 #define WSA883X_SPARE_1                 (WSA883X_DIG_CTRL_BASE + 0x00BE)
323 #define WSA883X_SPARE_2                 (WSA883X_DIG_CTRL_BASE + 0x00BF)
324 #define WSA883X_SCODE                   (WSA883X_DIG_CTRL_BASE + 0x00C0)
325 
326 #define WSA883X_DIG_TRIM_BASE           (WSA883X_BASE + 0x00000500)
327 #define WSA883X_OTP_REG_0               (WSA883X_DIG_TRIM_BASE + 0x0080)
328 #define WSA883X_ID_MASK			GENMASK(3, 0)
329 #define WSA883X_OTP_REG_1               (WSA883X_DIG_TRIM_BASE + 0x0081)
330 #define WSA883X_OTP_REG_2               (WSA883X_DIG_TRIM_BASE + 0x0082)
331 #define WSA883X_OTP_REG_3               (WSA883X_DIG_TRIM_BASE + 0x0083)
332 #define WSA883X_OTP_REG_4               (WSA883X_DIG_TRIM_BASE + 0x0084)
333 #define WSA883X_OTP_REG_5               (WSA883X_DIG_TRIM_BASE + 0x0085)
334 #define WSA883X_OTP_REG_6               (WSA883X_DIG_TRIM_BASE + 0x0086)
335 #define WSA883X_OTP_REG_7               (WSA883X_DIG_TRIM_BASE + 0x0087)
336 #define WSA883X_OTP_REG_8               (WSA883X_DIG_TRIM_BASE + 0x0088)
337 #define WSA883X_OTP_REG_9               (WSA883X_DIG_TRIM_BASE + 0x0089)
338 #define WSA883X_OTP_REG_10              (WSA883X_DIG_TRIM_BASE + 0x008A)
339 #define WSA883X_OTP_REG_11              (WSA883X_DIG_TRIM_BASE + 0x008B)
340 #define WSA883X_OTP_REG_12              (WSA883X_DIG_TRIM_BASE + 0x008C)
341 #define WSA883X_OTP_REG_13              (WSA883X_DIG_TRIM_BASE + 0x008D)
342 #define WSA883X_OTP_REG_14              (WSA883X_DIG_TRIM_BASE + 0x008E)
343 #define WSA883X_OTP_REG_15              (WSA883X_DIG_TRIM_BASE + 0x008F)
344 #define WSA883X_OTP_REG_16              (WSA883X_DIG_TRIM_BASE + 0x0090)
345 #define WSA883X_OTP_REG_17              (WSA883X_DIG_TRIM_BASE + 0x0091)
346 #define WSA883X_OTP_REG_18              (WSA883X_DIG_TRIM_BASE + 0x0092)
347 #define WSA883X_OTP_REG_19              (WSA883X_DIG_TRIM_BASE + 0x0093)
348 #define WSA883X_OTP_REG_20              (WSA883X_DIG_TRIM_BASE + 0x0094)
349 #define WSA883X_OTP_REG_21              (WSA883X_DIG_TRIM_BASE + 0x0095)
350 #define WSA883X_OTP_REG_22              (WSA883X_DIG_TRIM_BASE + 0x0096)
351 #define WSA883X_OTP_REG_23              (WSA883X_DIG_TRIM_BASE + 0x0097)
352 #define WSA883X_OTP_REG_24              (WSA883X_DIG_TRIM_BASE + 0x0098)
353 #define WSA883X_OTP_REG_25              (WSA883X_DIG_TRIM_BASE + 0x0099)
354 #define WSA883X_OTP_REG_26              (WSA883X_DIG_TRIM_BASE + 0x009A)
355 #define WSA883X_OTP_REG_27              (WSA883X_DIG_TRIM_BASE + 0x009B)
356 #define WSA883X_OTP_REG_28              (WSA883X_DIG_TRIM_BASE + 0x009C)
357 #define WSA883X_OTP_REG_29              (WSA883X_DIG_TRIM_BASE + 0x009D)
358 #define WSA883X_OTP_REG_30              (WSA883X_DIG_TRIM_BASE + 0x009E)
359 #define WSA883X_OTP_REG_31              (WSA883X_DIG_TRIM_BASE + 0x009F)
360 #define WSA883X_OTP_REG_32              (WSA883X_DIG_TRIM_BASE + 0x00A0)
361 #define WSA883X_OTP_REG_33              (WSA883X_DIG_TRIM_BASE + 0x00A1)
362 #define WSA883X_OTP_REG_34              (WSA883X_DIG_TRIM_BASE + 0x00A2)
363 #define WSA883X_OTP_REG_35              (WSA883X_DIG_TRIM_BASE + 0x00A3)
364 #define WSA883X_OTP_REG_63              (WSA883X_DIG_TRIM_BASE + 0x00BF)
365 
366 #define WSA883X_DIG_EMEM_BASE           (WSA883X_BASE + 0x000005C0)
367 #define WSA883X_EMEM_0                  (WSA883X_DIG_EMEM_BASE + 0x0000)
368 #define WSA883X_EMEM_1                  (WSA883X_DIG_EMEM_BASE + 0x0001)
369 #define WSA883X_EMEM_2                  (WSA883X_DIG_EMEM_BASE + 0x0002)
370 #define WSA883X_EMEM_3                  (WSA883X_DIG_EMEM_BASE + 0x0003)
371 #define WSA883X_EMEM_4                  (WSA883X_DIG_EMEM_BASE + 0x0004)
372 #define WSA883X_EMEM_5                  (WSA883X_DIG_EMEM_BASE + 0x0005)
373 #define WSA883X_EMEM_6                  (WSA883X_DIG_EMEM_BASE + 0x0006)
374 #define WSA883X_EMEM_7                  (WSA883X_DIG_EMEM_BASE + 0x0007)
375 #define WSA883X_EMEM_8                  (WSA883X_DIG_EMEM_BASE + 0x0008)
376 #define WSA883X_EMEM_9                  (WSA883X_DIG_EMEM_BASE + 0x0009)
377 #define WSA883X_EMEM_10                 (WSA883X_DIG_EMEM_BASE + 0x000A)
378 #define WSA883X_EMEM_11                 (WSA883X_DIG_EMEM_BASE + 0x000B)
379 #define WSA883X_EMEM_12                 (WSA883X_DIG_EMEM_BASE + 0x000C)
380 #define WSA883X_EMEM_13                 (WSA883X_DIG_EMEM_BASE + 0x000D)
381 #define WSA883X_EMEM_14                 (WSA883X_DIG_EMEM_BASE + 0x000E)
382 #define WSA883X_EMEM_15                 (WSA883X_DIG_EMEM_BASE + 0x000F)
383 #define WSA883X_EMEM_16                 (WSA883X_DIG_EMEM_BASE + 0x0010)
384 #define WSA883X_EMEM_17                 (WSA883X_DIG_EMEM_BASE + 0x0011)
385 #define WSA883X_EMEM_18                 (WSA883X_DIG_EMEM_BASE + 0x0012)
386 #define WSA883X_EMEM_19                 (WSA883X_DIG_EMEM_BASE + 0x0013)
387 #define WSA883X_EMEM_20                 (WSA883X_DIG_EMEM_BASE + 0x0014)
388 #define WSA883X_EMEM_21                 (WSA883X_DIG_EMEM_BASE + 0x0015)
389 #define WSA883X_EMEM_22                 (WSA883X_DIG_EMEM_BASE + 0x0016)
390 #define WSA883X_EMEM_23                 (WSA883X_DIG_EMEM_BASE + 0x0017)
391 #define WSA883X_EMEM_24                 (WSA883X_DIG_EMEM_BASE + 0x0018)
392 #define WSA883X_EMEM_25                 (WSA883X_DIG_EMEM_BASE + 0x0019)
393 #define WSA883X_EMEM_26                 (WSA883X_DIG_EMEM_BASE + 0x001A)
394 #define WSA883X_EMEM_27                 (WSA883X_DIG_EMEM_BASE + 0x001B)
395 #define WSA883X_EMEM_28                 (WSA883X_DIG_EMEM_BASE + 0x001C)
396 #define WSA883X_EMEM_29                 (WSA883X_DIG_EMEM_BASE + 0x001D)
397 #define WSA883X_EMEM_30                 (WSA883X_DIG_EMEM_BASE + 0x001E)
398 #define WSA883X_EMEM_31                 (WSA883X_DIG_EMEM_BASE + 0x001F)
399 #define WSA883X_EMEM_32                 (WSA883X_DIG_EMEM_BASE + 0x0020)
400 #define WSA883X_EMEM_33                 (WSA883X_DIG_EMEM_BASE + 0x0021)
401 #define WSA883X_EMEM_34                 (WSA883X_DIG_EMEM_BASE + 0x0022)
402 #define WSA883X_EMEM_35                 (WSA883X_DIG_EMEM_BASE + 0x0023)
403 #define WSA883X_EMEM_36                 (WSA883X_DIG_EMEM_BASE + 0x0024)
404 #define WSA883X_EMEM_37                 (WSA883X_DIG_EMEM_BASE + 0x0025)
405 #define WSA883X_EMEM_38                 (WSA883X_DIG_EMEM_BASE + 0x0026)
406 #define WSA883X_EMEM_39                 (WSA883X_DIG_EMEM_BASE + 0x0027)
407 #define WSA883X_EMEM_40                 (WSA883X_DIG_EMEM_BASE + 0x0028)
408 #define WSA883X_EMEM_41                 (WSA883X_DIG_EMEM_BASE + 0x0029)
409 #define WSA883X_EMEM_42                 (WSA883X_DIG_EMEM_BASE + 0x002A)
410 #define WSA883X_EMEM_43                 (WSA883X_DIG_EMEM_BASE + 0x002B)
411 #define WSA883X_EMEM_44                 (WSA883X_DIG_EMEM_BASE + 0x002C)
412 #define WSA883X_EMEM_45                 (WSA883X_DIG_EMEM_BASE + 0x002D)
413 #define WSA883X_EMEM_46                 (WSA883X_DIG_EMEM_BASE + 0x002E)
414 #define WSA883X_EMEM_47                 (WSA883X_DIG_EMEM_BASE + 0x002F)
415 #define WSA883X_EMEM_48                 (WSA883X_DIG_EMEM_BASE + 0x0030)
416 #define WSA883X_EMEM_49                 (WSA883X_DIG_EMEM_BASE + 0x0031)
417 #define WSA883X_EMEM_50                 (WSA883X_DIG_EMEM_BASE + 0x0032)
418 #define WSA883X_EMEM_51                 (WSA883X_DIG_EMEM_BASE + 0x0033)
419 #define WSA883X_EMEM_52                 (WSA883X_DIG_EMEM_BASE + 0x0034)
420 #define WSA883X_EMEM_53                 (WSA883X_DIG_EMEM_BASE + 0x0035)
421 #define WSA883X_EMEM_54                 (WSA883X_DIG_EMEM_BASE + 0x0036)
422 #define WSA883X_EMEM_55                 (WSA883X_DIG_EMEM_BASE + 0x0037)
423 #define WSA883X_EMEM_56                 (WSA883X_DIG_EMEM_BASE + 0x0038)
424 #define WSA883X_EMEM_57                 (WSA883X_DIG_EMEM_BASE + 0x0039)
425 #define WSA883X_EMEM_58                 (WSA883X_DIG_EMEM_BASE + 0x003A)
426 #define WSA883X_EMEM_59                 (WSA883X_DIG_EMEM_BASE + 0x003B)
427 #define WSA883X_EMEM_60                 (WSA883X_DIG_EMEM_BASE + 0x003C)
428 #define WSA883X_EMEM_61                 (WSA883X_DIG_EMEM_BASE + 0x003D)
429 #define WSA883X_EMEM_62                 (WSA883X_DIG_EMEM_BASE + 0x003E)
430 #define WSA883X_EMEM_63                 (WSA883X_DIG_EMEM_BASE + 0x003F)
431 
432 #define WSA883X_NUM_REGISTERS           (WSA883X_EMEM_63 + 1)
433 #define WSA883X_MAX_REGISTER            (WSA883X_NUM_REGISTERS - 1)
434 
435 #define WSA883X_VERSION_1_0 0
436 #define WSA883X_VERSION_1_1 1
437 
438 #define WSA883X_MAX_SWR_PORTS   4
439 #define WSA883X_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
440 			SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000 |\
441 			SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000 |\
442 			SNDRV_PCM_RATE_384000)
443 /* Fractional Rates */
444 #define WSA883X_FRAC_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |\
445 				SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_352800)
446 
447 #define WSA883X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
448 		SNDRV_PCM_FMTBIT_S24_LE |\
449 		SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
450 
451 /* Two-point trimming for temperature calibration */
452 #define WSA883X_T1_TEMP			-10L
453 #define WSA883X_T2_TEMP			150L
454 
455 /*
456  * Device will report senseless data in many cases, so discard any measurements
457  * outside of valid range.
458  */
459 #define WSA883X_LOW_TEMP_THRESHOLD	5
460 #define WSA883X_HIGH_TEMP_THRESHOLD	45
461 
462 struct wsa883x_priv {
463 	struct regmap *regmap;
464 	struct device *dev;
465 	struct regulator *vdd;
466 	struct sdw_slave *slave;
467 	struct sdw_stream_config sconfig;
468 	struct sdw_stream_runtime *sruntime;
469 	struct sdw_port_config port_config[WSA883X_MAX_SWR_PORTS];
470 	struct gpio_desc *sd_n;
471 	bool port_prepared[WSA883X_MAX_SWR_PORTS];
472 	bool port_enable[WSA883X_MAX_SWR_PORTS];
473 	int active_ports;
474 	int dev_mode;
475 	int comp_offset;
476 	/*
477 	 * Protects temperature reading code (related to speaker protection) and
478 	 * fields: temperature and pa_on.
479 	 */
480 	struct mutex sp_lock;
481 	unsigned int temperature;
482 	bool pa_on;
483 };
484 
485 enum {
486 	WSA8830 = 0,
487 	WSA8835,
488 	WSA8832,
489 	WSA8835_V2 = 5,
490 };
491 
492 enum {
493 	COMP_OFFSET0,
494 	COMP_OFFSET1,
495 	COMP_OFFSET2,
496 	COMP_OFFSET3,
497 	COMP_OFFSET4,
498 };
499 
500 enum wsa_port_ids {
501 	WSA883X_PORT_DAC,
502 	WSA883X_PORT_COMP,
503 	WSA883X_PORT_BOOST,
504 	WSA883X_PORT_VISENSE,
505 };
506 
507 static const char * const wsa_dev_mode_text[] = {
508 	"Speaker", "Receiver", "Ultrasound"
509 };
510 
511 enum {
512 	SPEAKER,
513 	RECEIVER,
514 	ULTRASOUND,
515 };
516 
517 static const struct soc_enum wsa_dev_mode_enum =
518 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_dev_mode_text), wsa_dev_mode_text);
519 
520 /* 4 ports */
521 static struct sdw_dpn_prop wsa_sink_dpn_prop[WSA883X_MAX_SWR_PORTS] = {
522 	[WSA883X_PORT_DAC] = {
523 		.num = WSA883X_PORT_DAC + 1,
524 		.type = SDW_DPN_SIMPLE,
525 		.min_ch = 1,
526 		.max_ch = 1,
527 		.simple_ch_prep_sm = true,
528 		.read_only_wordlength = true,
529 	},
530 	[WSA883X_PORT_COMP] = {
531 		.num = WSA883X_PORT_COMP + 1,
532 		.type = SDW_DPN_SIMPLE,
533 		.min_ch = 1,
534 		.max_ch = 1,
535 		.simple_ch_prep_sm = true,
536 		.read_only_wordlength = true,
537 	},
538 	[WSA883X_PORT_BOOST] = {
539 		.num = WSA883X_PORT_BOOST + 1,
540 		.type = SDW_DPN_SIMPLE,
541 		.min_ch = 1,
542 		.max_ch = 1,
543 		.simple_ch_prep_sm = true,
544 		.read_only_wordlength = true,
545 	},
546 	[WSA883X_PORT_VISENSE] = {
547 		.num = WSA883X_PORT_VISENSE + 1,
548 		.type = SDW_DPN_SIMPLE,
549 		.min_ch = 1,
550 		.max_ch = 1,
551 		.simple_ch_prep_sm = true,
552 		.read_only_wordlength = true,
553 	}
554 };
555 
556 static const struct sdw_port_config wsa883x_pconfig[WSA883X_MAX_SWR_PORTS] = {
557 	[WSA883X_PORT_DAC] = {
558 		.num = WSA883X_PORT_DAC + 1,
559 		.ch_mask = 0x1,
560 	},
561 	[WSA883X_PORT_COMP] = {
562 		.num = WSA883X_PORT_COMP + 1,
563 		.ch_mask = 0xf,
564 	},
565 	[WSA883X_PORT_BOOST] = {
566 		.num = WSA883X_PORT_BOOST + 1,
567 		.ch_mask = 0x3,
568 	},
569 	[WSA883X_PORT_VISENSE] = {
570 		.num = WSA883X_PORT_VISENSE + 1,
571 		.ch_mask = 0x1,
572 	},
573 };
574 
575 static struct reg_default wsa883x_defaults[] = {
576 	{ WSA883X_REF_CTRL, 0xD5 },
577 	{ WSA883X_TEST_CTL_0, 0x06 },
578 	{ WSA883X_BIAS_0, 0xD2 },
579 	{ WSA883X_OP_CTL, 0xE0 },
580 	{ WSA883X_IREF_CTL, 0x57 },
581 	{ WSA883X_ISENS_CTL, 0x47 },
582 	{ WSA883X_CLK_CTL, 0x87 },
583 	{ WSA883X_TEST_CTL_1, 0x00 },
584 	{ WSA883X_BIAS_1, 0x51 },
585 	{ WSA883X_ADC_CTL, 0x01 },
586 	{ WSA883X_DOUT_MSB, 0x00 },
587 	{ WSA883X_DOUT_LSB, 0x00 },
588 	{ WSA883X_VBAT_SNS, 0x40 },
589 	{ WSA883X_ITRIM_CODE, 0x9F },
590 	{ WSA883X_EN, 0x20 },
591 	{ WSA883X_OVERRIDE1, 0x00 },
592 	{ WSA883X_OVERRIDE2, 0x08 },
593 	{ WSA883X_VSENSE1, 0xD3 },
594 	{ WSA883X_ISENSE1, 0xD4 },
595 	{ WSA883X_ISENSE2, 0x20 },
596 	{ WSA883X_ISENSE_CAL, 0x00 },
597 	{ WSA883X_MISC, 0x08 },
598 	{ WSA883X_ADC_0, 0x00 },
599 	{ WSA883X_ADC_1, 0x00 },
600 	{ WSA883X_ADC_2, 0x40 },
601 	{ WSA883X_ADC_3, 0x80 },
602 	{ WSA883X_ADC_4, 0x25 },
603 	{ WSA883X_ADC_5, 0x25 },
604 	{ WSA883X_ADC_6, 0x08 },
605 	{ WSA883X_ADC_7, 0x81 },
606 	{ WSA883X_STATUS, 0x00 },
607 	{ WSA883X_DAC_CTRL_REG, 0x53 },
608 	{ WSA883X_DAC_EN_DEBUG_REG, 0x00 },
609 	{ WSA883X_DAC_OPAMP_BIAS1_REG, 0x48 },
610 	{ WSA883X_DAC_OPAMP_BIAS2_REG, 0x48 },
611 	{ WSA883X_DAC_VCM_CTRL_REG, 0x88 },
612 	{ WSA883X_DAC_VOLTAGE_CTRL_REG, 0xA5 },
613 	{ WSA883X_ATEST1_REG, 0x00 },
614 	{ WSA883X_ATEST2_REG, 0x00 },
615 	{ WSA883X_SPKR_TOP_BIAS_REG1, 0x6A },
616 	{ WSA883X_SPKR_TOP_BIAS_REG2, 0x65 },
617 	{ WSA883X_SPKR_TOP_BIAS_REG3, 0x55 },
618 	{ WSA883X_SPKR_TOP_BIAS_REG4, 0xA9 },
619 	{ WSA883X_SPKR_CLIP_DET_REG, 0x9C },
620 	{ WSA883X_SPKR_DRV_LF_BLK_EN, 0x0F },
621 	{ WSA883X_SPKR_DRV_LF_EN, 0x0A },
622 	{ WSA883X_SPKR_DRV_LF_MASK_DCC_CTL, 0x00 },
623 	{ WSA883X_SPKR_DRV_LF_MISC_CTL, 0x3A },
624 	{ WSA883X_SPKR_DRV_LF_REG_GAIN, 0x00 },
625 	{ WSA883X_SPKR_DRV_OS_CAL_CTL, 0x00 },
626 	{ WSA883X_SPKR_DRV_OS_CAL_CTL1, 0x90 },
627 	{ WSA883X_SPKR_PWM_CLK_CTL, 0x00 },
628 	{ WSA883X_SPKR_PDRV_HS_CTL, 0x52 },
629 	{ WSA883X_SPKR_PDRV_LS_CTL, 0x48 },
630 	{ WSA883X_SPKR_PWRSTG_DBG, 0x08 },
631 	{ WSA883X_SPKR_OCP_CTL, 0xE2 },
632 	{ WSA883X_SPKR_BBM_CTL, 0x92 },
633 	{ WSA883X_PA_STATUS0, 0x00 },
634 	{ WSA883X_PA_STATUS1, 0x00 },
635 	{ WSA883X_PA_STATUS2, 0x80 },
636 	{ WSA883X_EN_CTRL, 0x44 },
637 	{ WSA883X_CURRENT_LIMIT, 0xCC },
638 	{ WSA883X_IBIAS1, 0x00 },
639 	{ WSA883X_IBIAS2, 0x00 },
640 	{ WSA883X_IBIAS3, 0x00 },
641 	{ WSA883X_LDO_PROG, 0x02 },
642 	{ WSA883X_STABILITY_CTRL1, 0x8E },
643 	{ WSA883X_STABILITY_CTRL2, 0x10 },
644 	{ WSA883X_PWRSTAGE_CTRL1, 0x06 },
645 	{ WSA883X_PWRSTAGE_CTRL2, 0x00 },
646 	{ WSA883X_BYPASS_1, 0x19 },
647 	{ WSA883X_BYPASS_2, 0x13 },
648 	{ WSA883X_ZX_CTRL_1, 0xF0 },
649 	{ WSA883X_ZX_CTRL_2, 0x04 },
650 	{ WSA883X_MISC1, 0x06 },
651 	{ WSA883X_MISC2, 0xA0 },
652 	{ WSA883X_GMAMP_SUP1, 0x82 },
653 	{ WSA883X_PWRSTAGE_CTRL3, 0x39 },
654 	{ WSA883X_PWRSTAGE_CTRL4, 0x5F },
655 	{ WSA883X_TEST1, 0x00 },
656 	{ WSA883X_SPARE1, 0x00 },
657 	{ WSA883X_SPARE2, 0x00 },
658 	{ WSA883X_PON_CTL_0, 0x10 },
659 	{ WSA883X_PON_CLT_1, 0xE0 },
660 	{ WSA883X_PON_CTL_2, 0x90 },
661 	{ WSA883X_PON_CTL_3, 0x70 },
662 	{ WSA883X_CKWD_CTL_0, 0x34 },
663 	{ WSA883X_CKWD_CTL_1, 0x0F },
664 	{ WSA883X_CKWD_CTL_2, 0x00 },
665 	{ WSA883X_CKSK_CTL_0, 0x00 },
666 	{ WSA883X_PADSW_CTL_0, 0x00 },
667 	{ WSA883X_TEST_0, 0x00 },
668 	{ WSA883X_TEST_1, 0x00 },
669 	{ WSA883X_STATUS_0, 0x00 },
670 	{ WSA883X_STATUS_1, 0x00 },
671 	{ WSA883X_CHIP_ID0, 0x00 },
672 	{ WSA883X_CHIP_ID1, 0x00 },
673 	{ WSA883X_CHIP_ID2, 0x02 },
674 	{ WSA883X_CHIP_ID3, 0x02 },
675 	{ WSA883X_BUS_ID, 0x00 },
676 	{ WSA883X_CDC_RST_CTL, 0x01 },
677 	{ WSA883X_TOP_CLK_CFG, 0x00 },
678 	{ WSA883X_CDC_PATH_MODE, 0x00 },
679 	{ WSA883X_CDC_CLK_CTL, 0xFF },
680 	{ WSA883X_SWR_RESET_EN, 0x00 },
681 	{ WSA883X_RESET_CTL, 0x00 },
682 	{ WSA883X_PA_FSM_CTL, 0x00 },
683 	{ WSA883X_PA_FSM_TIMER0, 0x80 },
684 	{ WSA883X_PA_FSM_TIMER1, 0x80 },
685 	{ WSA883X_PA_FSM_STA, 0x00 },
686 	{ WSA883X_PA_FSM_ERR_COND, 0x00 },
687 	{ WSA883X_PA_FSM_MSK, 0x00 },
688 	{ WSA883X_PA_FSM_BYP, 0x01 },
689 	{ WSA883X_PA_FSM_DBG, 0x00 },
690 	{ WSA883X_TADC_VALUE_CTL, 0x03 },
691 	{ WSA883X_TEMP_DETECT_CTL, 0x01 },
692 	{ WSA883X_TEMP_MSB, 0x00 },
693 	{ WSA883X_TEMP_LSB, 0x00 },
694 	{ WSA883X_TEMP_CONFIG0, 0x00 },
695 	{ WSA883X_TEMP_CONFIG1, 0x00 },
696 	{ WSA883X_VBAT_ADC_FLT_CTL, 0x00 },
697 	{ WSA883X_VBAT_DIN_MSB, 0x00 },
698 	{ WSA883X_VBAT_DIN_LSB, 0x00 },
699 	{ WSA883X_VBAT_DOUT, 0x00 },
700 	{ WSA883X_SDM_PDM9_LSB, 0x00 },
701 	{ WSA883X_SDM_PDM9_MSB, 0x00 },
702 	{ WSA883X_CDC_RX_CTL, 0xFE },
703 	{ WSA883X_CDC_SPK_DSM_A1_0, 0x00 },
704 	{ WSA883X_CDC_SPK_DSM_A1_1, 0x01 },
705 	{ WSA883X_CDC_SPK_DSM_A2_0, 0x96 },
706 	{ WSA883X_CDC_SPK_DSM_A2_1, 0x09 },
707 	{ WSA883X_CDC_SPK_DSM_A3_0, 0xAB },
708 	{ WSA883X_CDC_SPK_DSM_A3_1, 0x05 },
709 	{ WSA883X_CDC_SPK_DSM_A4_0, 0x1C },
710 	{ WSA883X_CDC_SPK_DSM_A4_1, 0x02 },
711 	{ WSA883X_CDC_SPK_DSM_A5_0, 0x17 },
712 	{ WSA883X_CDC_SPK_DSM_A5_1, 0x02 },
713 	{ WSA883X_CDC_SPK_DSM_A6_0, 0xAA },
714 	{ WSA883X_CDC_SPK_DSM_A7_0, 0xE3 },
715 	{ WSA883X_CDC_SPK_DSM_C_0, 0x69 },
716 	{ WSA883X_CDC_SPK_DSM_C_1, 0x54 },
717 	{ WSA883X_CDC_SPK_DSM_C_2, 0x02 },
718 	{ WSA883X_CDC_SPK_DSM_C_3, 0x15 },
719 	{ WSA883X_CDC_SPK_DSM_R1, 0xA4 },
720 	{ WSA883X_CDC_SPK_DSM_R2, 0xB5 },
721 	{ WSA883X_CDC_SPK_DSM_R3, 0x86 },
722 	{ WSA883X_CDC_SPK_DSM_R4, 0x85 },
723 	{ WSA883X_CDC_SPK_DSM_R5, 0xAA },
724 	{ WSA883X_CDC_SPK_DSM_R6, 0xE2 },
725 	{ WSA883X_CDC_SPK_DSM_R7, 0x62 },
726 	{ WSA883X_CDC_SPK_GAIN_PDM_0, 0x00 },
727 	{ WSA883X_CDC_SPK_GAIN_PDM_1, 0xFC },
728 	{ WSA883X_CDC_SPK_GAIN_PDM_2, 0x05 },
729 	{ WSA883X_PDM_WD_CTL, 0x00 },
730 	{ WSA883X_DEM_BYPASS_DATA0, 0x00 },
731 	{ WSA883X_DEM_BYPASS_DATA1, 0x00 },
732 	{ WSA883X_DEM_BYPASS_DATA2, 0x00 },
733 	{ WSA883X_DEM_BYPASS_DATA3, 0x00 },
734 	{ WSA883X_WAVG_CTL, 0x06 },
735 	{ WSA883X_WAVG_LRA_PER_0, 0xD1 },
736 	{ WSA883X_WAVG_LRA_PER_1, 0x00 },
737 	{ WSA883X_WAVG_DELTA_THETA_0, 0xE6 },
738 	{ WSA883X_WAVG_DELTA_THETA_1, 0x04 },
739 	{ WSA883X_WAVG_DIRECT_AMP_0, 0x50 },
740 	{ WSA883X_WAVG_DIRECT_AMP_1, 0x00 },
741 	{ WSA883X_WAVG_PTRN_AMP0_0, 0x50 },
742 	{ WSA883X_WAVG_PTRN_AMP0_1, 0x00 },
743 	{ WSA883X_WAVG_PTRN_AMP1_0, 0x50 },
744 	{ WSA883X_WAVG_PTRN_AMP1_1, 0x00 },
745 	{ WSA883X_WAVG_PTRN_AMP2_0, 0x50 },
746 	{ WSA883X_WAVG_PTRN_AMP2_1, 0x00 },
747 	{ WSA883X_WAVG_PTRN_AMP3_0, 0x50 },
748 	{ WSA883X_WAVG_PTRN_AMP3_1, 0x00 },
749 	{ WSA883X_WAVG_PTRN_AMP4_0, 0x50 },
750 	{ WSA883X_WAVG_PTRN_AMP4_1, 0x00 },
751 	{ WSA883X_WAVG_PTRN_AMP5_0, 0x50 },
752 	{ WSA883X_WAVG_PTRN_AMP5_1, 0x00 },
753 	{ WSA883X_WAVG_PTRN_AMP6_0, 0x50 },
754 	{ WSA883X_WAVG_PTRN_AMP6_1, 0x00 },
755 	{ WSA883X_WAVG_PTRN_AMP7_0, 0x50 },
756 	{ WSA883X_WAVG_PTRN_AMP7_1, 0x00 },
757 	{ WSA883X_WAVG_PER_0_1, 0x88 },
758 	{ WSA883X_WAVG_PER_2_3, 0x88 },
759 	{ WSA883X_WAVG_PER_4_5, 0x88 },
760 	{ WSA883X_WAVG_PER_6_7, 0x88 },
761 	{ WSA883X_WAVG_STA, 0x00 },
762 	{ WSA883X_DRE_CTL_0, 0x70 },
763 	{ WSA883X_DRE_CTL_1, 0x08 },
764 	{ WSA883X_DRE_IDLE_DET_CTL, 0x1F },
765 	{ WSA883X_CLSH_CTL_0, 0x37 },
766 	{ WSA883X_CLSH_CTL_1, 0x81 },
767 	{ WSA883X_CLSH_V_HD_PA, 0x0F },
768 	{ WSA883X_CLSH_V_PA_MIN, 0x00 },
769 	{ WSA883X_CLSH_OVRD_VAL, 0x00 },
770 	{ WSA883X_CLSH_HARD_MAX, 0xFF },
771 	{ WSA883X_CLSH_SOFT_MAX, 0xF5 },
772 	{ WSA883X_CLSH_SIG_DP, 0x00 },
773 	{ WSA883X_TAGC_CTL, 0x10 },
774 	{ WSA883X_TAGC_TIME, 0x20 },
775 	{ WSA883X_TAGC_E2E_GAIN, 0x02 },
776 	{ WSA883X_TAGC_FORCE_VAL, 0x00 },
777 	{ WSA883X_VAGC_CTL, 0x00 },
778 	{ WSA883X_VAGC_TIME, 0x08 },
779 	{ WSA883X_VAGC_ATTN_LVL_1_2, 0x21 },
780 	{ WSA883X_VAGC_ATTN_LVL_3, 0x03 },
781 	{ WSA883X_INTR_MODE, 0x00 },
782 	{ WSA883X_INTR_MASK0, 0x90 },
783 	{ WSA883X_INTR_MASK1, 0x00 },
784 	{ WSA883X_INTR_STATUS0, 0x00 },
785 	{ WSA883X_INTR_STATUS1, 0x00 },
786 	{ WSA883X_INTR_CLEAR0, 0x00 },
787 	{ WSA883X_INTR_CLEAR1, 0x00 },
788 	{ WSA883X_INTR_LEVEL0, 0x00 },
789 	{ WSA883X_INTR_LEVEL1, 0x00 },
790 	{ WSA883X_INTR_SET0, 0x00 },
791 	{ WSA883X_INTR_SET1, 0x00 },
792 	{ WSA883X_INTR_TEST0, 0x00 },
793 	{ WSA883X_INTR_TEST1, 0x00 },
794 	{ WSA883X_OTP_CTRL0, 0x00 },
795 	{ WSA883X_OTP_CTRL1, 0x00 },
796 	{ WSA883X_HDRIVE_CTL_GROUP1, 0x00 },
797 	{ WSA883X_PIN_CTL, 0x04 },
798 	{ WSA883X_PIN_CTL_OE, 0x00 },
799 	{ WSA883X_PIN_WDATA_IOPAD, 0x00 },
800 	{ WSA883X_PIN_STATUS, 0x00 },
801 	{ WSA883X_I2C_SLAVE_CTL, 0x00 },
802 	{ WSA883X_PDM_TEST_MODE, 0x00 },
803 	{ WSA883X_ATE_TEST_MODE, 0x00 },
804 	{ WSA883X_DIG_DEBUG_MODE, 0x00 },
805 	{ WSA883X_DIG_DEBUG_SEL, 0x00 },
806 	{ WSA883X_DIG_DEBUG_EN, 0x00 },
807 	{ WSA883X_SWR_HM_TEST0, 0x08 },
808 	{ WSA883X_SWR_HM_TEST1, 0x00 },
809 	{ WSA883X_SWR_PAD_CTL, 0x37 },
810 	{ WSA883X_TADC_DETECT_DBG_CTL, 0x00 },
811 	{ WSA883X_TADC_DEBUG_MSB, 0x00 },
812 	{ WSA883X_TADC_DEBUG_LSB, 0x00 },
813 	{ WSA883X_SAMPLE_EDGE_SEL, 0x7F },
814 	{ WSA883X_SWR_EDGE_SEL, 0x00 },
815 	{ WSA883X_TEST_MODE_CTL, 0x04 },
816 	{ WSA883X_IOPAD_CTL, 0x00 },
817 	{ WSA883X_ANA_CSR_DBG_ADD, 0x00 },
818 	{ WSA883X_ANA_CSR_DBG_CTL, 0x12 },
819 	{ WSA883X_SPARE_R, 0x00 },
820 	{ WSA883X_SPARE_0, 0x00 },
821 	{ WSA883X_SPARE_1, 0x00 },
822 	{ WSA883X_SPARE_2, 0x00 },
823 	{ WSA883X_SCODE, 0x00 },
824 	{ WSA883X_OTP_REG_0, 0x05 },
825 	{ WSA883X_OTP_REG_1, 0xFF },
826 	{ WSA883X_OTP_REG_2, 0xC0 },
827 	{ WSA883X_OTP_REG_3, 0xFF },
828 	{ WSA883X_OTP_REG_4, 0xC0 },
829 	{ WSA883X_OTP_REG_5, 0xFF },
830 	{ WSA883X_OTP_REG_6, 0xFF },
831 	{ WSA883X_OTP_REG_7, 0xFF },
832 	{ WSA883X_OTP_REG_8, 0xFF },
833 	{ WSA883X_OTP_REG_9, 0xFF },
834 	{ WSA883X_OTP_REG_10, 0xFF },
835 	{ WSA883X_OTP_REG_11, 0xFF },
836 	{ WSA883X_OTP_REG_12, 0xFF },
837 	{ WSA883X_OTP_REG_13, 0xFF },
838 	{ WSA883X_OTP_REG_14, 0xFF },
839 	{ WSA883X_OTP_REG_15, 0xFF },
840 	{ WSA883X_OTP_REG_16, 0xFF },
841 	{ WSA883X_OTP_REG_17, 0xFF },
842 	{ WSA883X_OTP_REG_18, 0xFF },
843 	{ WSA883X_OTP_REG_19, 0xFF },
844 	{ WSA883X_OTP_REG_20, 0xFF },
845 	{ WSA883X_OTP_REG_21, 0xFF },
846 	{ WSA883X_OTP_REG_22, 0xFF },
847 	{ WSA883X_OTP_REG_23, 0xFF },
848 	{ WSA883X_OTP_REG_24, 0x37 },
849 	{ WSA883X_OTP_REG_25, 0x3F },
850 	{ WSA883X_OTP_REG_26, 0x03 },
851 	{ WSA883X_OTP_REG_27, 0x00 },
852 	{ WSA883X_OTP_REG_28, 0x00 },
853 	{ WSA883X_OTP_REG_29, 0x00 },
854 	{ WSA883X_OTP_REG_30, 0x00 },
855 	{ WSA883X_OTP_REG_31, 0x03 },
856 	{ WSA883X_OTP_REG_32, 0x00 },
857 	{ WSA883X_OTP_REG_33, 0xFF },
858 	{ WSA883X_OTP_REG_34, 0x00 },
859 	{ WSA883X_OTP_REG_35, 0x00 },
860 	{ WSA883X_OTP_REG_63, 0x40 },
861 	{ WSA883X_EMEM_0, 0x00 },
862 	{ WSA883X_EMEM_1, 0x00 },
863 	{ WSA883X_EMEM_2, 0x00 },
864 	{ WSA883X_EMEM_3, 0x00 },
865 	{ WSA883X_EMEM_4, 0x00 },
866 	{ WSA883X_EMEM_5, 0x00 },
867 	{ WSA883X_EMEM_6, 0x00 },
868 	{ WSA883X_EMEM_7, 0x00 },
869 	{ WSA883X_EMEM_8, 0x00 },
870 	{ WSA883X_EMEM_9, 0x00 },
871 	{ WSA883X_EMEM_10, 0x00 },
872 	{ WSA883X_EMEM_11, 0x00 },
873 	{ WSA883X_EMEM_12, 0x00 },
874 	{ WSA883X_EMEM_13, 0x00 },
875 	{ WSA883X_EMEM_14, 0x00 },
876 	{ WSA883X_EMEM_15, 0x00 },
877 	{ WSA883X_EMEM_16, 0x00 },
878 	{ WSA883X_EMEM_17, 0x00 },
879 	{ WSA883X_EMEM_18, 0x00 },
880 	{ WSA883X_EMEM_19, 0x00 },
881 	{ WSA883X_EMEM_20, 0x00 },
882 	{ WSA883X_EMEM_21, 0x00 },
883 	{ WSA883X_EMEM_22, 0x00 },
884 	{ WSA883X_EMEM_23, 0x00 },
885 	{ WSA883X_EMEM_24, 0x00 },
886 	{ WSA883X_EMEM_25, 0x00 },
887 	{ WSA883X_EMEM_26, 0x00 },
888 	{ WSA883X_EMEM_27, 0x00 },
889 	{ WSA883X_EMEM_28, 0x00 },
890 	{ WSA883X_EMEM_29, 0x00 },
891 	{ WSA883X_EMEM_30, 0x00 },
892 	{ WSA883X_EMEM_31, 0x00 },
893 	{ WSA883X_EMEM_32, 0x00 },
894 	{ WSA883X_EMEM_33, 0x00 },
895 	{ WSA883X_EMEM_34, 0x00 },
896 	{ WSA883X_EMEM_35, 0x00 },
897 	{ WSA883X_EMEM_36, 0x00 },
898 	{ WSA883X_EMEM_37, 0x00 },
899 	{ WSA883X_EMEM_38, 0x00 },
900 	{ WSA883X_EMEM_39, 0x00 },
901 	{ WSA883X_EMEM_40, 0x00 },
902 	{ WSA883X_EMEM_41, 0x00 },
903 	{ WSA883X_EMEM_42, 0x00 },
904 	{ WSA883X_EMEM_43, 0x00 },
905 	{ WSA883X_EMEM_44, 0x00 },
906 	{ WSA883X_EMEM_45, 0x00 },
907 	{ WSA883X_EMEM_46, 0x00 },
908 	{ WSA883X_EMEM_47, 0x00 },
909 	{ WSA883X_EMEM_48, 0x00 },
910 	{ WSA883X_EMEM_49, 0x00 },
911 	{ WSA883X_EMEM_50, 0x00 },
912 	{ WSA883X_EMEM_51, 0x00 },
913 	{ WSA883X_EMEM_52, 0x00 },
914 	{ WSA883X_EMEM_53, 0x00 },
915 	{ WSA883X_EMEM_54, 0x00 },
916 	{ WSA883X_EMEM_55, 0x00 },
917 	{ WSA883X_EMEM_56, 0x00 },
918 	{ WSA883X_EMEM_57, 0x00 },
919 	{ WSA883X_EMEM_58, 0x00 },
920 	{ WSA883X_EMEM_59, 0x00 },
921 	{ WSA883X_EMEM_60, 0x00 },
922 	{ WSA883X_EMEM_61, 0x00 },
923 	{ WSA883X_EMEM_62, 0x00 },
924 	{ WSA883X_EMEM_63, 0x00 },
925 };
926 
wsa883x_readonly_register(struct device * dev,unsigned int reg)927 static bool wsa883x_readonly_register(struct device *dev, unsigned int reg)
928 {
929 	switch (reg) {
930 	case WSA883X_DOUT_MSB:
931 	case WSA883X_DOUT_LSB:
932 	case WSA883X_STATUS:
933 	case WSA883X_PA_STATUS0:
934 	case WSA883X_PA_STATUS1:
935 	case WSA883X_PA_STATUS2:
936 	case WSA883X_STATUS_0:
937 	case WSA883X_STATUS_1:
938 	case WSA883X_CHIP_ID0:
939 	case WSA883X_CHIP_ID1:
940 	case WSA883X_CHIP_ID2:
941 	case WSA883X_CHIP_ID3:
942 	case WSA883X_BUS_ID:
943 	case WSA883X_PA_FSM_STA:
944 	case WSA883X_PA_FSM_ERR_COND:
945 	case WSA883X_TEMP_MSB:
946 	case WSA883X_TEMP_LSB:
947 	case WSA883X_VBAT_DIN_MSB:
948 	case WSA883X_VBAT_DIN_LSB:
949 	case WSA883X_VBAT_DOUT:
950 	case WSA883X_SDM_PDM9_LSB:
951 	case WSA883X_SDM_PDM9_MSB:
952 	case WSA883X_WAVG_STA:
953 	case WSA883X_INTR_STATUS0:
954 	case WSA883X_INTR_STATUS1:
955 	case WSA883X_OTP_CTRL1:
956 	case WSA883X_PIN_STATUS:
957 	case WSA883X_ATE_TEST_MODE:
958 	case WSA883X_SWR_HM_TEST1:
959 	case WSA883X_SPARE_R:
960 	case WSA883X_OTP_REG_0:
961 		return true;
962 	}
963 	return false;
964 }
965 
wsa883x_writeable_register(struct device * dev,unsigned int reg)966 static bool wsa883x_writeable_register(struct device *dev, unsigned int reg)
967 {
968 	return !wsa883x_readonly_register(dev, reg);
969 }
970 
wsa883x_volatile_register(struct device * dev,unsigned int reg)971 static bool wsa883x_volatile_register(struct device *dev, unsigned int reg)
972 {
973 	return wsa883x_readonly_register(dev, reg);
974 }
975 
976 static const struct regmap_config wsa883x_regmap_config = {
977 	.reg_bits = 32,
978 	.val_bits = 8,
979 	.cache_type = REGCACHE_MAPLE,
980 	.reg_defaults = wsa883x_defaults,
981 	.max_register = WSA883X_MAX_REGISTER,
982 	.num_reg_defaults = ARRAY_SIZE(wsa883x_defaults),
983 	.volatile_reg = wsa883x_volatile_register,
984 	.writeable_reg = wsa883x_writeable_register,
985 	.reg_format_endian = REGMAP_ENDIAN_NATIVE,
986 	.val_format_endian = REGMAP_ENDIAN_NATIVE,
987 	.use_single_read = true,
988 };
989 
990 static const struct reg_sequence reg_init[] = {
991 	{WSA883X_PA_FSM_BYP, 0x00},
992 	{WSA883X_ADC_6, 0x02},
993 	{WSA883X_CDC_SPK_DSM_A2_0, 0x0A},
994 	{WSA883X_CDC_SPK_DSM_A2_1, 0x08},
995 	{WSA883X_CDC_SPK_DSM_A3_0, 0xF3},
996 	{WSA883X_CDC_SPK_DSM_A3_1, 0x07},
997 	{WSA883X_CDC_SPK_DSM_A4_0, 0x79},
998 	{WSA883X_CDC_SPK_DSM_A4_1, 0x02},
999 	{WSA883X_CDC_SPK_DSM_A5_0, 0x0B},
1000 	{WSA883X_CDC_SPK_DSM_A5_1, 0x02},
1001 	{WSA883X_CDC_SPK_DSM_A6_0, 0x8A},
1002 	{WSA883X_CDC_SPK_DSM_A7_0, 0x9B},
1003 	{WSA883X_CDC_SPK_DSM_C_0, 0x68},
1004 	{WSA883X_CDC_SPK_DSM_C_1, 0x54},
1005 	{WSA883X_CDC_SPK_DSM_C_2, 0xF2},
1006 	{WSA883X_CDC_SPK_DSM_C_3, 0x20},
1007 	{WSA883X_CDC_SPK_DSM_R1, 0x83},
1008 	{WSA883X_CDC_SPK_DSM_R2, 0x7F},
1009 	{WSA883X_CDC_SPK_DSM_R3, 0x9D},
1010 	{WSA883X_CDC_SPK_DSM_R4, 0x82},
1011 	{WSA883X_CDC_SPK_DSM_R5, 0x8B},
1012 	{WSA883X_CDC_SPK_DSM_R6, 0x9B},
1013 	{WSA883X_CDC_SPK_DSM_R7, 0x3F},
1014 	{WSA883X_VBAT_SNS, 0x20},
1015 	{WSA883X_DRE_CTL_0, 0x92},
1016 	{WSA883X_DRE_IDLE_DET_CTL, 0x0F},
1017 	{WSA883X_CURRENT_LIMIT, 0xC4},
1018 	{WSA883X_VAGC_TIME, 0x0F},
1019 	{WSA883X_VAGC_ATTN_LVL_1_2, 0x00},
1020 	{WSA883X_VAGC_ATTN_LVL_3, 0x01},
1021 	{WSA883X_VAGC_CTL, 0x01},
1022 	{WSA883X_TAGC_CTL, 0x1A},
1023 	{WSA883X_TAGC_TIME, 0x2C},
1024 	{WSA883X_TEMP_CONFIG0, 0x02},
1025 	{WSA883X_TEMP_CONFIG1, 0x02},
1026 	{WSA883X_OTP_REG_1, 0x49},
1027 	{WSA883X_OTP_REG_2, 0x80},
1028 	{WSA883X_OTP_REG_3, 0xC9},
1029 	{WSA883X_OTP_REG_4, 0x40},
1030 	{WSA883X_TAGC_CTL, 0x1B},
1031 	{WSA883X_ADC_2, 0x00},
1032 	{WSA883X_ADC_7, 0x85},
1033 	{WSA883X_ADC_7, 0x87},
1034 	{WSA883X_CKWD_CTL_0, 0x14},
1035 	{WSA883X_CKWD_CTL_1, 0x1B},
1036 	{WSA883X_GMAMP_SUP1, 0xE2},
1037 };
1038 
wsa883x_init(struct wsa883x_priv * wsa883x)1039 static int wsa883x_init(struct wsa883x_priv *wsa883x)
1040 {
1041 	struct regmap *regmap = wsa883x->regmap;
1042 	int variant, version, ret;
1043 
1044 	ret = regmap_read(regmap, WSA883X_OTP_REG_0, &variant);
1045 	if (ret)
1046 		return ret;
1047 	variant = variant & WSA883X_ID_MASK;
1048 
1049 	ret = regmap_read(regmap, WSA883X_CHIP_ID0, &version);
1050 	if (ret)
1051 		return ret;
1052 
1053 	switch (variant) {
1054 	case WSA8830:
1055 		dev_info(wsa883x->dev, "WSA883X Version 1_%d, Variant: WSA8830\n",
1056 			 version);
1057 		break;
1058 	case WSA8835:
1059 		dev_info(wsa883x->dev, "WSA883X Version 1_%d, Variant: WSA8835\n",
1060 			 version);
1061 		break;
1062 	case WSA8832:
1063 		dev_info(wsa883x->dev, "WSA883X Version 1_%d, Variant: WSA8832\n",
1064 			 version);
1065 		break;
1066 	case WSA8835_V2:
1067 		dev_info(wsa883x->dev, "WSA883X Version 1_%d, Variant: WSA8835_V2\n",
1068 			 version);
1069 		break;
1070 	default:
1071 		break;
1072 	}
1073 
1074 	wsa883x->comp_offset = COMP_OFFSET2;
1075 
1076 	/* Initial settings */
1077 	regmap_multi_reg_write(regmap, reg_init, ARRAY_SIZE(reg_init));
1078 
1079 	if (variant == WSA8830 || variant == WSA8832) {
1080 		wsa883x->comp_offset = COMP_OFFSET3;
1081 		regmap_update_bits(regmap, WSA883X_DRE_CTL_0,
1082 				   WSA883X_DRE_OFFSET_MASK,
1083 				   wsa883x->comp_offset);
1084 	}
1085 
1086 	return 0;
1087 }
1088 
wsa883x_update_status(struct sdw_slave * slave,enum sdw_slave_status status)1089 static int wsa883x_update_status(struct sdw_slave *slave,
1090 				 enum sdw_slave_status status)
1091 {
1092 	struct wsa883x_priv *wsa883x = dev_get_drvdata(&slave->dev);
1093 
1094 	if (status == SDW_SLAVE_ATTACHED && slave->dev_num > 0)
1095 		return wsa883x_init(wsa883x);
1096 
1097 	return 0;
1098 }
1099 
wsa883x_port_prep(struct sdw_slave * slave,struct sdw_prepare_ch * prepare_ch,enum sdw_port_prep_ops state)1100 static int wsa883x_port_prep(struct sdw_slave *slave,
1101 			     struct sdw_prepare_ch *prepare_ch,
1102 			     enum sdw_port_prep_ops state)
1103 {
1104 	struct wsa883x_priv *wsa883x = dev_get_drvdata(&slave->dev);
1105 
1106 	if (state == SDW_OPS_PORT_POST_PREP)
1107 		wsa883x->port_prepared[prepare_ch->num - 1] = true;
1108 	else
1109 		wsa883x->port_prepared[prepare_ch->num - 1] = false;
1110 
1111 	return 0;
1112 }
1113 
1114 static const struct sdw_slave_ops wsa883x_slave_ops = {
1115 	.update_status = wsa883x_update_status,
1116 	.port_prep = wsa883x_port_prep,
1117 };
1118 
wsa_dev_mode_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1119 static int wsa_dev_mode_get(struct snd_kcontrol *kcontrol,
1120 			    struct snd_ctl_elem_value *ucontrol)
1121 {
1122 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1123 	struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
1124 
1125 	ucontrol->value.enumerated.item[0] = wsa883x->dev_mode;
1126 
1127 	return 0;
1128 }
1129 
wsa_dev_mode_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1130 static int wsa_dev_mode_put(struct snd_kcontrol *kcontrol,
1131 			    struct snd_ctl_elem_value *ucontrol)
1132 {
1133 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1134 	struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
1135 
1136 	if (wsa883x->dev_mode == ucontrol->value.enumerated.item[0])
1137 		return 0;
1138 
1139 	wsa883x->dev_mode = ucontrol->value.enumerated.item[0];
1140 
1141 	return 1;
1142 }
1143 
1144 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(pa_gain,
1145 	0, 14, TLV_DB_SCALE_ITEM(-300, 0, 0),
1146 	15, 29, TLV_DB_SCALE_ITEM(-300, 150, 0),
1147 	30, 31, TLV_DB_SCALE_ITEM(1800, 0, 0),
1148 );
1149 
wsa883x_get_swr_port(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1150 static int wsa883x_get_swr_port(struct snd_kcontrol *kcontrol,
1151 				struct snd_ctl_elem_value *ucontrol)
1152 {
1153 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
1154 	struct wsa883x_priv *data = snd_soc_component_get_drvdata(comp);
1155 	struct soc_mixer_control *mixer = (struct soc_mixer_control *)kcontrol->private_value;
1156 	int portidx = mixer->reg;
1157 
1158 	ucontrol->value.integer.value[0] = data->port_enable[portidx];
1159 
1160 	return 0;
1161 }
1162 
wsa883x_set_swr_port(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1163 static int wsa883x_set_swr_port(struct snd_kcontrol *kcontrol,
1164 				struct snd_ctl_elem_value *ucontrol)
1165 {
1166 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
1167 	struct wsa883x_priv *data = snd_soc_component_get_drvdata(comp);
1168 	struct soc_mixer_control *mixer = (struct soc_mixer_control *)kcontrol->private_value;
1169 	int portidx = mixer->reg;
1170 
1171 	if (ucontrol->value.integer.value[0]) {
1172 		if (data->port_enable[portidx])
1173 			return 0;
1174 
1175 		data->port_enable[portidx] = true;
1176 	} else {
1177 		if (!data->port_enable[portidx])
1178 			return 0;
1179 
1180 		data->port_enable[portidx] = false;
1181 	}
1182 
1183 	return 1;
1184 }
1185 
wsa883x_get_comp_offset(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1186 static int wsa883x_get_comp_offset(struct snd_kcontrol *kcontrol,
1187 				   struct snd_ctl_elem_value *ucontrol)
1188 {
1189 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1190 	struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
1191 
1192 	ucontrol->value.integer.value[0] = wsa883x->comp_offset;
1193 
1194 	return 0;
1195 }
1196 
wsa883x_set_comp_offset(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1197 static int wsa883x_set_comp_offset(struct snd_kcontrol *kcontrol,
1198 				   struct snd_ctl_elem_value *ucontrol)
1199 {
1200 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1201 	struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
1202 
1203 	if (wsa883x->comp_offset == ucontrol->value.integer.value[0])
1204 		return 0;
1205 
1206 	wsa883x->comp_offset = ucontrol->value.integer.value[0];
1207 
1208 	return 1;
1209 }
1210 
wsa883x_codec_probe(struct snd_soc_component * comp)1211 static int wsa883x_codec_probe(struct snd_soc_component *comp)
1212 {
1213 	struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(comp);
1214 
1215 	snd_soc_component_init_regmap(comp, wsa883x->regmap);
1216 
1217 	return 0;
1218 }
1219 
wsa883x_spkr_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1220 static int wsa883x_spkr_event(struct snd_soc_dapm_widget *w,
1221 			      struct snd_kcontrol *kcontrol, int event)
1222 {
1223 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1224 	struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
1225 
1226 	switch (event) {
1227 	case SND_SOC_DAPM_POST_PMU:
1228 		mutex_lock(&wsa883x->sp_lock);
1229 		wsa883x->pa_on = true;
1230 		mutex_unlock(&wsa883x->sp_lock);
1231 
1232 		switch (wsa883x->dev_mode) {
1233 		case RECEIVER:
1234 			snd_soc_component_write_field(component, WSA883X_CDC_PATH_MODE,
1235 						      WSA883X_RXD_MODE_MASK,
1236 						      WSA883X_RXD_MODE_HIFI);
1237 			snd_soc_component_write_field(component, WSA883X_SPKR_PWM_CLK_CTL,
1238 						      WSA883X_SPKR_PWM_FREQ_SEL_MASK,
1239 						      WSA883X_SPKR_PWM_FREQ_F600KHZ);
1240 			snd_soc_component_write_field(component, WSA883X_DRE_CTL_0,
1241 						       WSA883X_DRE_PROG_DELAY_MASK, 0x0);
1242 			break;
1243 		case SPEAKER:
1244 			snd_soc_component_write_field(component, WSA883X_CDC_PATH_MODE,
1245 						      WSA883X_RXD_MODE_MASK,
1246 						      WSA883X_RXD_MODE_NORMAL);
1247 			snd_soc_component_write_field(component, WSA883X_SPKR_PWM_CLK_CTL,
1248 						      WSA883X_SPKR_PWM_FREQ_SEL_MASK,
1249 						      WSA883X_SPKR_PWM_FREQ_F300KHZ);
1250 			snd_soc_component_write_field(component, WSA883X_DRE_CTL_0,
1251 						       WSA883X_DRE_PROG_DELAY_MASK, 0x9);
1252 			break;
1253 		default:
1254 			break;
1255 		}
1256 
1257 		if (wsa883x->port_enable[WSA883X_PORT_COMP])
1258 			snd_soc_component_write_field(component, WSA883X_DRE_CTL_0,
1259 						      WSA883X_DRE_OFFSET_MASK,
1260 						      wsa883x->comp_offset);
1261 		snd_soc_component_write_field(component, WSA883X_VBAT_ADC_FLT_CTL,
1262 					      WSA883X_VBAT_ADC_COEF_SEL_MASK,
1263 					      WSA883X_VBAT_ADC_COEF_F_1DIV16);
1264 		snd_soc_component_write_field(component, WSA883X_VBAT_ADC_FLT_CTL,
1265 					      WSA883X_VBAT_ADC_FLT_EN_MASK, 0x1);
1266 		snd_soc_component_write_field(component, WSA883X_PDM_WD_CTL,
1267 					      WSA883X_PDM_EN_MASK,
1268 					      WSA883X_PDM_ENABLE);
1269 
1270 		break;
1271 	case SND_SOC_DAPM_PRE_PMD:
1272 		snd_soc_component_write_field(component, WSA883X_VBAT_ADC_FLT_CTL,
1273 					      WSA883X_VBAT_ADC_FLT_EN_MASK, 0x0);
1274 		snd_soc_component_write_field(component, WSA883X_VBAT_ADC_FLT_CTL,
1275 					      WSA883X_VBAT_ADC_COEF_SEL_MASK,
1276 					      WSA883X_VBAT_ADC_COEF_F_1DIV2);
1277 		snd_soc_component_write_field(component, WSA883X_PA_FSM_CTL,
1278 					      WSA883X_GLOBAL_PA_EN_MASK, 0);
1279 		snd_soc_component_write_field(component, WSA883X_PDM_WD_CTL,
1280 					      WSA883X_PDM_EN_MASK, 0);
1281 		mutex_lock(&wsa883x->sp_lock);
1282 		wsa883x->pa_on = false;
1283 		mutex_unlock(&wsa883x->sp_lock);
1284 		break;
1285 	}
1286 	return 0;
1287 }
1288 
1289 static const struct snd_soc_dapm_widget wsa883x_dapm_widgets[] = {
1290 	SND_SOC_DAPM_INPUT("IN"),
1291 	SND_SOC_DAPM_SPK("SPKR", wsa883x_spkr_event),
1292 };
1293 
1294 static const struct snd_kcontrol_new wsa883x_snd_controls[] = {
1295 	SOC_SINGLE_RANGE_TLV("PA Volume", WSA883X_DRE_CTL_1, 1,
1296 			     0x0, 0x1f, 1, pa_gain),
1297 	SOC_ENUM_EXT("WSA MODE", wsa_dev_mode_enum,
1298 		     wsa_dev_mode_get, wsa_dev_mode_put),
1299 	SOC_SINGLE_EXT("COMP Offset", SND_SOC_NOPM, 0, 4, 0,
1300 		       wsa883x_get_comp_offset, wsa883x_set_comp_offset),
1301 	SOC_SINGLE_EXT("DAC Switch", WSA883X_PORT_DAC, 0, 1, 0,
1302 		       wsa883x_get_swr_port, wsa883x_set_swr_port),
1303 	SOC_SINGLE_EXT("COMP Switch", WSA883X_PORT_COMP, 0, 1, 0,
1304 		       wsa883x_get_swr_port, wsa883x_set_swr_port),
1305 	SOC_SINGLE_EXT("BOOST Switch", WSA883X_PORT_BOOST, 0, 1, 0,
1306 		       wsa883x_get_swr_port, wsa883x_set_swr_port),
1307 	SOC_SINGLE_EXT("VISENSE Switch", WSA883X_PORT_VISENSE, 0, 1, 0,
1308 		       wsa883x_get_swr_port, wsa883x_set_swr_port),
1309 };
1310 
1311 static const struct snd_soc_dapm_route wsa883x_audio_map[] = {
1312 	{"SPKR", NULL, "IN"},
1313 };
1314 
1315 static const struct snd_soc_component_driver wsa883x_component_drv = {
1316 	.name = "WSA883x",
1317 	.probe = wsa883x_codec_probe,
1318 	.controls = wsa883x_snd_controls,
1319 	.num_controls = ARRAY_SIZE(wsa883x_snd_controls),
1320 	.dapm_widgets = wsa883x_dapm_widgets,
1321 	.num_dapm_widgets = ARRAY_SIZE(wsa883x_dapm_widgets),
1322 	.dapm_routes = wsa883x_audio_map,
1323 	.num_dapm_routes = ARRAY_SIZE(wsa883x_audio_map),
1324 };
1325 
wsa883x_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)1326 static int wsa883x_hw_params(struct snd_pcm_substream *substream,
1327 			     struct snd_pcm_hw_params *params,
1328 			     struct snd_soc_dai *dai)
1329 {
1330 	struct wsa883x_priv *wsa883x = dev_get_drvdata(dai->dev);
1331 	int i;
1332 
1333 	wsa883x->active_ports = 0;
1334 	for (i = 0; i < WSA883X_MAX_SWR_PORTS; i++) {
1335 		if (!wsa883x->port_enable[i])
1336 			continue;
1337 
1338 		wsa883x->port_config[wsa883x->active_ports] = wsa883x_pconfig[i];
1339 		wsa883x->active_ports++;
1340 	}
1341 
1342 	wsa883x->sconfig.frame_rate = params_rate(params);
1343 
1344 	return sdw_stream_add_slave(wsa883x->slave, &wsa883x->sconfig,
1345 				    wsa883x->port_config, wsa883x->active_ports,
1346 				    wsa883x->sruntime);
1347 }
1348 
wsa883x_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1349 static int wsa883x_hw_free(struct snd_pcm_substream *substream,
1350 			   struct snd_soc_dai *dai)
1351 {
1352 	struct wsa883x_priv *wsa883x = dev_get_drvdata(dai->dev);
1353 
1354 	sdw_stream_remove_slave(wsa883x->slave, wsa883x->sruntime);
1355 
1356 	return 0;
1357 }
1358 
wsa883x_set_sdw_stream(struct snd_soc_dai * dai,void * stream,int direction)1359 static int wsa883x_set_sdw_stream(struct snd_soc_dai *dai,
1360 				  void *stream, int direction)
1361 {
1362 	struct wsa883x_priv *wsa883x = dev_get_drvdata(dai->dev);
1363 
1364 	wsa883x->sruntime = stream;
1365 
1366 	return 0;
1367 }
1368 
wsa883x_digital_mute(struct snd_soc_dai * dai,int mute,int stream)1369 static int wsa883x_digital_mute(struct snd_soc_dai *dai, int mute, int stream)
1370 {
1371 	struct snd_soc_component *component = dai->component;
1372 
1373 	if (mute) {
1374 		snd_soc_component_write_field(component, WSA883X_DRE_CTL_1,
1375 					      WSA883X_DRE_GAIN_EN_MASK, 0);
1376 		snd_soc_component_write_field(component, WSA883X_PA_FSM_CTL,
1377 					      WSA883X_GLOBAL_PA_EN_MASK, 0);
1378 
1379 	} else {
1380 		snd_soc_component_write_field(component, WSA883X_DRE_CTL_1,
1381 					      WSA883X_DRE_GAIN_EN_MASK,
1382 					      WSA883X_DRE_GAIN_FROM_CSR);
1383 		snd_soc_component_write_field(component, WSA883X_PA_FSM_CTL,
1384 					      WSA883X_GLOBAL_PA_EN_MASK,
1385 					      WSA883X_GLOBAL_PA_ENABLE);
1386 
1387 	}
1388 
1389 	return 0;
1390 }
1391 
1392 static const struct snd_soc_dai_ops wsa883x_dai_ops = {
1393 	.hw_params = wsa883x_hw_params,
1394 	.hw_free = wsa883x_hw_free,
1395 	.mute_stream = wsa883x_digital_mute,
1396 	.set_stream = wsa883x_set_sdw_stream,
1397 	.mute_unmute_on_trigger = true,
1398 };
1399 
1400 static struct snd_soc_dai_driver wsa883x_dais[] = {
1401 	{
1402 		.name = "SPKR",
1403 		.playback = {
1404 			.stream_name = "SPKR Playback",
1405 			.rates = WSA883X_RATES | WSA883X_FRAC_RATES,
1406 			.formats = WSA883X_FORMATS,
1407 			.rate_min = 8000,
1408 			.rate_max = 352800,
1409 			.channels_min = 1,
1410 			.channels_max = 1,
1411 		},
1412 		.ops = &wsa883x_dai_ops,
1413 	},
1414 };
1415 
wsa883x_get_temp(struct wsa883x_priv * wsa883x,long * temp)1416 static int wsa883x_get_temp(struct wsa883x_priv *wsa883x, long *temp)
1417 {
1418 	unsigned int d1_msb = 0, d1_lsb = 0, d2_msb = 0, d2_lsb = 0;
1419 	unsigned int dmeas_msb = 0, dmeas_lsb = 0;
1420 	int d1, d2, dmeas;
1421 	unsigned int mask;
1422 	int ret, range;
1423 	long val;
1424 
1425 	guard(mutex)(&wsa883x->sp_lock);
1426 
1427 	if (wsa883x->pa_on) {
1428 		/*
1429 		 * Reading temperature is possible only when Power Amplifier is
1430 		 * off. Report last cached data.
1431 		 */
1432 		*temp = wsa883x->temperature * 1000;
1433 		return 0;
1434 	}
1435 
1436 	ret = pm_runtime_resume_and_get(wsa883x->dev);
1437 	if (ret < 0)
1438 		return ret;
1439 
1440 	mask = WSA883X_PA_FSM_BYP_DC_CAL_EN_MASK |
1441 	       WSA883X_PA_FSM_BYP_CLK_WD_EN_MASK |
1442 	       WSA883X_PA_FSM_BYP_BG_EN_MASK |
1443 	       WSA883X_PA_FSM_BYP_D_UNMUTE_MASK |
1444 	       WSA883X_PA_FSM_BYP_SPKR_PROT_EN_MASK |
1445 	       WSA883X_PA_FSM_BYP_TSADC_EN_MASK;
1446 
1447 	/*
1448 	 * Here and further do not care about read or update failures.
1449 	 * For example, before turning the amplifier on for the first
1450 	 * time, reading WSA883X_TEMP_DIN_MSB will always return 0.
1451 	 * Instead, check if returned value is within reasonable
1452 	 * thresholds.
1453 	 */
1454 	regmap_update_bits(wsa883x->regmap, WSA883X_PA_FSM_BYP, mask, mask);
1455 
1456 	regmap_update_bits(wsa883x->regmap, WSA883X_TADC_VALUE_CTL,
1457 			   WSA883X_TADC_VALUE_CTL_TEMP_VALUE_RD_EN_MASK,
1458 			   FIELD_PREP(WSA883X_TADC_VALUE_CTL_TEMP_VALUE_RD_EN_MASK, 0x0));
1459 
1460 	regmap_read(wsa883x->regmap, WSA883X_TEMP_MSB, &dmeas_msb);
1461 	regmap_read(wsa883x->regmap, WSA883X_TEMP_LSB, &dmeas_lsb);
1462 
1463 	regmap_update_bits(wsa883x->regmap, WSA883X_TADC_VALUE_CTL,
1464 			   WSA883X_TADC_VALUE_CTL_TEMP_VALUE_RD_EN_MASK,
1465 			   FIELD_PREP(WSA883X_TADC_VALUE_CTL_TEMP_VALUE_RD_EN_MASK, 0x1));
1466 
1467 	regmap_read(wsa883x->regmap, WSA883X_OTP_REG_1, &d1_msb);
1468 	regmap_read(wsa883x->regmap, WSA883X_OTP_REG_2, &d1_lsb);
1469 	regmap_read(wsa883x->regmap, WSA883X_OTP_REG_3, &d2_msb);
1470 	regmap_read(wsa883x->regmap, WSA883X_OTP_REG_4, &d2_lsb);
1471 
1472 	regmap_update_bits(wsa883x->regmap, WSA883X_PA_FSM_BYP, mask, 0x0);
1473 
1474 	dmeas = (((dmeas_msb & 0xff) << 0x8) | (dmeas_lsb & 0xff)) >> 0x6;
1475 	d1 = (((d1_msb & 0xff) << 0x8) | (d1_lsb & 0xff)) >> 0x6;
1476 	d2 = (((d2_msb & 0xff) << 0x8) | (d2_lsb & 0xff)) >> 0x6;
1477 
1478 	if (d1 == d2) {
1479 		/* Incorrect data in OTP? */
1480 		ret = -EINVAL;
1481 		goto out;
1482 	}
1483 
1484 	val = WSA883X_T1_TEMP + (((dmeas - d1) * (WSA883X_T2_TEMP - WSA883X_T1_TEMP)) / (d2 - d1));
1485 	range = WSA883X_HIGH_TEMP_THRESHOLD - WSA883X_LOW_TEMP_THRESHOLD;
1486 	if (in_range(val, WSA883X_LOW_TEMP_THRESHOLD, range)) {
1487 		wsa883x->temperature = val;
1488 		*temp = val * 1000;
1489 		ret = 0;
1490 	} else {
1491 		ret = -EAGAIN;
1492 	}
1493 out:
1494 	pm_runtime_mark_last_busy(wsa883x->dev);
1495 	pm_runtime_put_autosuspend(wsa883x->dev);
1496 
1497 	return ret;
1498 }
1499 
wsa883x_hwmon_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)1500 static umode_t wsa883x_hwmon_is_visible(const void *data,
1501 					enum hwmon_sensor_types type, u32 attr,
1502 					int channel)
1503 {
1504 	if (type != hwmon_temp)
1505 		return 0;
1506 
1507 	switch (attr) {
1508 	case hwmon_temp_input:
1509 		return 0444;
1510 	default:
1511 		break;
1512 	}
1513 
1514 	return 0;
1515 }
1516 
wsa883x_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * temp)1517 static int wsa883x_hwmon_read(struct device *dev,
1518 			      enum hwmon_sensor_types type,
1519 			      u32 attr, int channel, long *temp)
1520 {
1521 	int ret;
1522 
1523 	switch (attr) {
1524 	case hwmon_temp_input:
1525 		ret = wsa883x_get_temp(dev_get_drvdata(dev), temp);
1526 		break;
1527 	default:
1528 		ret = -EOPNOTSUPP;
1529 		break;
1530 	}
1531 
1532 	return ret;
1533 }
1534 
1535 static const struct hwmon_channel_info *const wsa883x_hwmon_info[] = {
1536 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
1537 	NULL
1538 };
1539 
1540 static const struct hwmon_ops wsa883x_hwmon_ops = {
1541 	.is_visible	= wsa883x_hwmon_is_visible,
1542 	.read		= wsa883x_hwmon_read,
1543 };
1544 
1545 static const struct hwmon_chip_info wsa883x_hwmon_chip_info = {
1546 	.ops	= &wsa883x_hwmon_ops,
1547 	.info	= wsa883x_hwmon_info,
1548 };
1549 
wsa883x_probe(struct sdw_slave * pdev,const struct sdw_device_id * id)1550 static int wsa883x_probe(struct sdw_slave *pdev,
1551 			 const struct sdw_device_id *id)
1552 {
1553 	struct wsa883x_priv *wsa883x;
1554 	struct device *dev = &pdev->dev;
1555 	int ret;
1556 
1557 	wsa883x = devm_kzalloc(dev, sizeof(*wsa883x), GFP_KERNEL);
1558 	if (!wsa883x)
1559 		return -ENOMEM;
1560 
1561 	wsa883x->vdd = devm_regulator_get(dev, "vdd");
1562 	if (IS_ERR(wsa883x->vdd))
1563 		return dev_err_probe(dev, PTR_ERR(wsa883x->vdd),
1564 				     "No vdd regulator found\n");
1565 
1566 	ret = regulator_enable(wsa883x->vdd);
1567 	if (ret)
1568 		return dev_err_probe(dev, ret, "Failed to enable vdd regulator\n");
1569 
1570 	wsa883x->sd_n = devm_gpiod_get_optional(dev, "powerdown",
1571 						GPIOD_FLAGS_BIT_NONEXCLUSIVE | GPIOD_OUT_HIGH);
1572 	if (IS_ERR(wsa883x->sd_n)) {
1573 		ret = dev_err_probe(dev, PTR_ERR(wsa883x->sd_n),
1574 				    "Shutdown Control GPIO not found\n");
1575 		goto err;
1576 	}
1577 
1578 	dev_set_drvdata(dev, wsa883x);
1579 	wsa883x->slave = pdev;
1580 	wsa883x->dev = dev;
1581 	wsa883x->sconfig.ch_count = 1;
1582 	wsa883x->sconfig.bps = 1;
1583 	wsa883x->sconfig.direction = SDW_DATA_DIR_RX;
1584 	wsa883x->sconfig.type = SDW_STREAM_PDM;
1585 	mutex_init(&wsa883x->sp_lock);
1586 
1587 	/**
1588 	 * Port map index starts with 0, however the data port for this codec
1589 	 * are from index 1
1590 	 */
1591 	if (of_property_read_u32_array(dev->of_node, "qcom,port-mapping", &pdev->m_port_map[1],
1592 					WSA883X_MAX_SWR_PORTS))
1593 		dev_dbg(dev, "Static Port mapping not specified\n");
1594 
1595 	pdev->prop.sink_ports = GENMASK(WSA883X_MAX_SWR_PORTS - 1, 0);
1596 	pdev->prop.simple_clk_stop_capable = true;
1597 	pdev->prop.sink_dpn_prop = wsa_sink_dpn_prop;
1598 	pdev->prop.scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
1599 	gpiod_direction_output(wsa883x->sd_n, 0);
1600 
1601 	wsa883x->regmap = devm_regmap_init_sdw(pdev, &wsa883x_regmap_config);
1602 	if (IS_ERR(wsa883x->regmap)) {
1603 		gpiod_direction_output(wsa883x->sd_n, 1);
1604 		ret = dev_err_probe(dev, PTR_ERR(wsa883x->regmap),
1605 				    "regmap_init failed\n");
1606 		goto err;
1607 	}
1608 
1609 	if (IS_REACHABLE(CONFIG_HWMON)) {
1610 		struct device *hwmon;
1611 
1612 		hwmon = devm_hwmon_device_register_with_info(dev, "wsa883x",
1613 							     wsa883x,
1614 							     &wsa883x_hwmon_chip_info,
1615 							     NULL);
1616 		if (IS_ERR(hwmon))
1617 			return dev_err_probe(dev, PTR_ERR(hwmon),
1618 					     "Failed to register hwmon sensor\n");
1619 	}
1620 
1621 	pm_runtime_set_autosuspend_delay(dev, 3000);
1622 	pm_runtime_use_autosuspend(dev);
1623 	pm_runtime_mark_last_busy(dev);
1624 	pm_runtime_set_active(dev);
1625 	pm_runtime_enable(dev);
1626 
1627 	ret = devm_snd_soc_register_component(dev,
1628 					      &wsa883x_component_drv,
1629 					       wsa883x_dais,
1630 					       ARRAY_SIZE(wsa883x_dais));
1631 err:
1632 	if (ret)
1633 		regulator_disable(wsa883x->vdd);
1634 
1635 	return ret;
1636 
1637 }
1638 
wsa883x_runtime_suspend(struct device * dev)1639 static int wsa883x_runtime_suspend(struct device *dev)
1640 {
1641 	struct regmap *regmap = dev_get_regmap(dev, NULL);
1642 
1643 	regcache_cache_only(regmap, true);
1644 	regcache_mark_dirty(regmap);
1645 
1646 	return 0;
1647 }
1648 
wsa883x_runtime_resume(struct device * dev)1649 static int wsa883x_runtime_resume(struct device *dev)
1650 {
1651 	struct regmap *regmap = dev_get_regmap(dev, NULL);
1652 
1653 	regcache_cache_only(regmap, false);
1654 	regcache_sync(regmap);
1655 
1656 	return 0;
1657 }
1658 
1659 static const struct dev_pm_ops wsa883x_pm_ops = {
1660 	RUNTIME_PM_OPS(wsa883x_runtime_suspend, wsa883x_runtime_resume, NULL)
1661 };
1662 
1663 static const struct sdw_device_id wsa883x_swr_id[] = {
1664 	SDW_SLAVE_ENTRY(0x0217, 0x0202, 0),
1665 	{},
1666 };
1667 
1668 MODULE_DEVICE_TABLE(sdw, wsa883x_swr_id);
1669 
1670 static struct sdw_driver wsa883x_codec_driver = {
1671 	.driver = {
1672 		.name = "wsa883x-codec",
1673 		.pm = pm_ptr(&wsa883x_pm_ops),
1674 		.suppress_bind_attrs = true,
1675 	},
1676 	.probe = wsa883x_probe,
1677 	.ops = &wsa883x_slave_ops,
1678 	.id_table = wsa883x_swr_id,
1679 };
1680 
1681 module_sdw_driver(wsa883x_codec_driver);
1682 
1683 MODULE_DESCRIPTION("WSA883x codec driver");
1684 MODULE_LICENSE("GPL");
1685