xref: /linux/sound/soc/codecs/cs35l36.c (revision 16e5ac127d8d18adf85fe5ba847d77b58d1ed418)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // cs35l36.c -- CS35L36 ALSA SoC audio driver
4 //
5 // Copyright 2018 Cirrus Logic, Inc.
6 //
7 // Author: James Schulman <james.schulman@cirrus.com>
8 
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/of.h>
21 #include <linux/of_gpio.h>
22 #include <linux/regmap.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dapm.h>
28 #include <linux/gpio.h>
29 #include <sound/initval.h>
30 #include <sound/tlv.h>
31 #include <sound/cs35l36.h>
32 #include <linux/completion.h>
33 
34 #include "cs35l36.h"
35 
36 /*
37  * Some fields take zero as a valid value so use a high bit flag that won't
38  * get written to the device to mark those.
39  */
40 #define CS35L36_VALID_PDATA 0x80000000
41 
42 static const char * const cs35l36_supplies[] = {
43 	"VA",
44 	"VP",
45 };
46 
47 struct  cs35l36_private {
48 	struct device *dev;
49 	struct cs35l36_platform_data pdata;
50 	struct regmap *regmap;
51 	struct regulator_bulk_data supplies[2];
52 	int num_supplies;
53 	int clksrc;
54 	int chip_version;
55 	int rev_id;
56 	int ldm_mode_sel;
57 	struct gpio_desc *reset_gpio;
58 };
59 
60 struct cs35l36_pll_config {
61 	int freq;
62 	int clk_cfg;
63 	int fll_igain;
64 };
65 
66 static const struct cs35l36_pll_config cs35l36_pll_sysclk[] = {
67 	{32768,		0x00, 0x05},
68 	{8000,		0x01, 0x03},
69 	{11025,		0x02, 0x03},
70 	{12000,		0x03, 0x03},
71 	{16000,		0x04, 0x04},
72 	{22050,		0x05, 0x04},
73 	{24000,		0x06, 0x04},
74 	{32000,		0x07, 0x05},
75 	{44100,		0x08, 0x05},
76 	{48000,		0x09, 0x05},
77 	{88200,		0x0A, 0x06},
78 	{96000,		0x0B, 0x06},
79 	{128000,	0x0C, 0x07},
80 	{176400,	0x0D, 0x07},
81 	{192000,	0x0E, 0x07},
82 	{256000,	0x0F, 0x08},
83 	{352800,	0x10, 0x08},
84 	{384000,	0x11, 0x08},
85 	{512000,	0x12, 0x09},
86 	{705600,	0x13, 0x09},
87 	{750000,	0x14, 0x09},
88 	{768000,	0x15, 0x09},
89 	{1000000,	0x16, 0x0A},
90 	{1024000,	0x17, 0x0A},
91 	{1200000,	0x18, 0x0A},
92 	{1411200,	0x19, 0x0A},
93 	{1500000,	0x1A, 0x0A},
94 	{1536000,	0x1B, 0x0A},
95 	{2000000,	0x1C, 0x0A},
96 	{2048000,	0x1D, 0x0A},
97 	{2400000,	0x1E, 0x0A},
98 	{2822400,	0x1F, 0x0A},
99 	{3000000,	0x20, 0x0A},
100 	{3072000,	0x21, 0x0A},
101 	{3200000,	0x22, 0x0A},
102 	{4000000,	0x23, 0x0A},
103 	{4096000,	0x24, 0x0A},
104 	{4800000,	0x25, 0x0A},
105 	{5644800,	0x26, 0x0A},
106 	{6000000,	0x27, 0x0A},
107 	{6144000,	0x28, 0x0A},
108 	{6250000,	0x29, 0x08},
109 	{6400000,	0x2A, 0x0A},
110 	{6500000,	0x2B, 0x08},
111 	{6750000,	0x2C, 0x09},
112 	{7526400,	0x2D, 0x0A},
113 	{8000000,	0x2E, 0x0A},
114 	{8192000,	0x2F, 0x0A},
115 	{9600000,	0x30, 0x0A},
116 	{11289600,	0x31, 0x0A},
117 	{12000000,	0x32, 0x0A},
118 	{12288000,	0x33, 0x0A},
119 	{12500000,	0x34, 0x08},
120 	{12800000,	0x35, 0x0A},
121 	{13000000,	0x36, 0x0A},
122 	{13500000,	0x37, 0x0A},
123 	{19200000,	0x38, 0x0A},
124 	{22579200,	0x39, 0x0A},
125 	{24000000,	0x3A, 0x0A},
126 	{24576000,	0x3B, 0x0A},
127 	{25000000,	0x3C, 0x0A},
128 	{25600000,	0x3D, 0x0A},
129 	{26000000,	0x3E, 0x0A},
130 	{27000000,	0x3F, 0x0A},
131 };
132 
133 static struct reg_default cs35l36_reg[] = {
134 	{CS35L36_TESTKEY_CTRL,			0x00000000},
135 	{CS35L36_USERKEY_CTL,			0x00000000},
136 	{CS35L36_OTP_CTRL1,			0x00002460},
137 	{CS35L36_OTP_CTRL2,			0x00000000},
138 	{CS35L36_OTP_CTRL3,			0x00000000},
139 	{CS35L36_OTP_CTRL4,			0x00000000},
140 	{CS35L36_OTP_CTRL5,			0x00000000},
141 	{CS35L36_PAC_CTL1,			0x00000004},
142 	{CS35L36_PAC_CTL2,			0x00000000},
143 	{CS35L36_PAC_CTL3,			0x00000000},
144 	{CS35L36_PWR_CTRL1,			0x00000000},
145 	{CS35L36_PWR_CTRL2,			0x00003321},
146 	{CS35L36_PWR_CTRL3,			0x01000010},
147 	{CS35L36_CTRL_OVRRIDE,			0x00000002},
148 	{CS35L36_AMP_OUT_MUTE,			0x00000000},
149 	{CS35L36_OTP_TRIM_STATUS,		0x00000000},
150 	{CS35L36_DISCH_FILT,			0x00000000},
151 	{CS35L36_PROTECT_REL_ERR,		0x00000000},
152 	{CS35L36_PAD_INTERFACE,			0x00000038},
153 	{CS35L36_PLL_CLK_CTRL,			0x00000010},
154 	{CS35L36_GLOBAL_CLK_CTRL,		0x00000003},
155 	{CS35L36_ADC_CLK_CTRL,			0x00000000},
156 	{CS35L36_SWIRE_CLK_CTRL,		0x00000000},
157 	{CS35L36_SP_SCLK_CLK_CTRL,		0x00000000},
158 	{CS35L36_MDSYNC_EN,			0x00000000},
159 	{CS35L36_MDSYNC_TX_ID,			0x00000000},
160 	{CS35L36_MDSYNC_PWR_CTRL,		0x00000000},
161 	{CS35L36_MDSYNC_DATA_TX,		0x00000000},
162 	{CS35L36_MDSYNC_TX_STATUS,		0x00000002},
163 	{CS35L36_MDSYNC_RX_STATUS,		0x00000000},
164 	{CS35L36_MDSYNC_ERR_STATUS,		0x00000000},
165 	{CS35L36_BSTCVRT_VCTRL1,		0x00000000},
166 	{CS35L36_BSTCVRT_VCTRL2,		0x00000001},
167 	{CS35L36_BSTCVRT_PEAK_CUR,		0x0000004A},
168 	{CS35L36_BSTCVRT_SFT_RAMP,		0x00000003},
169 	{CS35L36_BSTCVRT_COEFF,			0x00002424},
170 	{CS35L36_BSTCVRT_SLOPE_LBST,		0x00005800},
171 	{CS35L36_BSTCVRT_SW_FREQ,		0x00010000},
172 	{CS35L36_BSTCVRT_DCM_CTRL,		0x00002001},
173 	{CS35L36_BSTCVRT_DCM_MODE_FORCE,	0x00000000},
174 	{CS35L36_BSTCVRT_OVERVOLT_CTRL,		0x00000130},
175 	{CS35L36_VPI_LIMIT_MODE,		0x00000000},
176 	{CS35L36_VPI_LIMIT_MINMAX,		0x00003000},
177 	{CS35L36_VPI_VP_THLD,			0x00101010},
178 	{CS35L36_VPI_TRACK_CTRL,		0x00000000},
179 	{CS35L36_VPI_TRIG_MODE_CTRL,		0x00000000},
180 	{CS35L36_VPI_TRIG_STEPS,		0x00000000},
181 	{CS35L36_VI_SPKMON_FILT,		0x00000003},
182 	{CS35L36_VI_SPKMON_GAIN,		0x00000909},
183 	{CS35L36_VI_SPKMON_IP_SEL,		0x00000000},
184 	{CS35L36_DTEMP_WARN_THLD,		0x00000002},
185 	{CS35L36_DTEMP_STATUS,			0x00000000},
186 	{CS35L36_VPVBST_FS_SEL,			0x00000001},
187 	{CS35L36_VPVBST_VP_CTRL,		0x000001C0},
188 	{CS35L36_VPVBST_VBST_CTRL,		0x000001C0},
189 	{CS35L36_ASP_TX_PIN_CTRL,		0x00000028},
190 	{CS35L36_ASP_RATE_CTRL,			0x00090000},
191 	{CS35L36_ASP_FORMAT,			0x00000002},
192 	{CS35L36_ASP_FRAME_CTRL,		0x00180018},
193 	{CS35L36_ASP_TX1_TX2_SLOT,		0x00010000},
194 	{CS35L36_ASP_TX3_TX4_SLOT,		0x00030002},
195 	{CS35L36_ASP_TX5_TX6_SLOT,		0x00050004},
196 	{CS35L36_ASP_TX7_TX8_SLOT,		0x00070006},
197 	{CS35L36_ASP_RX1_SLOT,			0x00000000},
198 	{CS35L36_ASP_RX_TX_EN,			0x00000000},
199 	{CS35L36_ASP_RX1_SEL,			0x00000008},
200 	{CS35L36_ASP_TX1_SEL,			0x00000018},
201 	{CS35L36_ASP_TX2_SEL,			0x00000019},
202 	{CS35L36_ASP_TX3_SEL,			0x00000028},
203 	{CS35L36_ASP_TX4_SEL,			0x00000029},
204 	{CS35L36_ASP_TX5_SEL,			0x00000020},
205 	{CS35L36_ASP_TX6_SEL,			0x00000000},
206 	{CS35L36_SWIRE_P1_TX1_SEL,		0x00000018},
207 	{CS35L36_SWIRE_P1_TX2_SEL,		0x00000019},
208 	{CS35L36_SWIRE_P2_TX1_SEL,		0x00000028},
209 	{CS35L36_SWIRE_P2_TX2_SEL,		0x00000029},
210 	{CS35L36_SWIRE_P2_TX3_SEL,		0x00000020},
211 	{CS35L36_SWIRE_DP1_FIFO_CFG,		0x0000001B},
212 	{CS35L36_SWIRE_DP2_FIFO_CFG,		0x0000001B},
213 	{CS35L36_SWIRE_DP3_FIFO_CFG,		0x0000001B},
214 	{CS35L36_SWIRE_PCM_RX_DATA,		0x00000000},
215 	{CS35L36_SWIRE_FS_SEL,			0x00000001},
216 	{CS35L36_AMP_DIG_VOL_CTRL,		0x00008000},
217 	{CS35L36_VPBR_CFG,			0x02AA1905},
218 	{CS35L36_VBBR_CFG,			0x02AA1905},
219 	{CS35L36_VPBR_STATUS,			0x00000000},
220 	{CS35L36_VBBR_STATUS,			0x00000000},
221 	{CS35L36_OVERTEMP_CFG,			0x00000001},
222 	{CS35L36_AMP_ERR_VOL,			0x00000000},
223 	{CS35L36_CLASSH_CFG,			0x000B0405},
224 	{CS35L36_CLASSH_FET_DRV_CFG,		0x00000111},
225 	{CS35L36_NG_CFG,			0x00000033},
226 	{CS35L36_AMP_GAIN_CTRL,			0x00000273},
227 	{CS35L36_PWM_MOD_IO_CTRL,		0x00000000},
228 	{CS35L36_PWM_MOD_STATUS,		0x00000000},
229 	{CS35L36_DAC_MSM_CFG,			0x00000000},
230 	{CS35L36_AMP_SLOPE_CTRL,		0x00000B00},
231 	{CS35L36_AMP_PDM_VOLUME,		0x00000000},
232 	{CS35L36_AMP_PDM_RATE_CTRL,		0x00000000},
233 	{CS35L36_PDM_CH_SEL,			0x00000000},
234 	{CS35L36_AMP_NG_CTRL,			0x0000212F},
235 	{CS35L36_PDM_HIGHFILT_CTRL,		0x00000000},
236 	{CS35L36_PAC_INT0_CTRL,			0x00000001},
237 	{CS35L36_PAC_INT1_CTRL,			0x00000001},
238 	{CS35L36_PAC_INT2_CTRL,			0x00000001},
239 	{CS35L36_PAC_INT3_CTRL,			0x00000001},
240 	{CS35L36_PAC_INT4_CTRL,			0x00000001},
241 	{CS35L36_PAC_INT5_CTRL,			0x00000001},
242 	{CS35L36_PAC_INT6_CTRL,			0x00000001},
243 	{CS35L36_PAC_INT7_CTRL,			0x00000001},
244 };
245 
246 static bool cs35l36_readable_reg(struct device *dev, unsigned int reg)
247 {
248 	switch (reg) {
249 	case CS35L36_SW_RESET:
250 	case CS35L36_SW_REV:
251 	case CS35L36_HW_REV:
252 	case CS35L36_TESTKEY_CTRL:
253 	case CS35L36_USERKEY_CTL:
254 	case CS35L36_OTP_MEM30:
255 	case CS35L36_OTP_CTRL1:
256 	case CS35L36_OTP_CTRL2:
257 	case CS35L36_OTP_CTRL3:
258 	case CS35L36_OTP_CTRL4:
259 	case CS35L36_OTP_CTRL5:
260 	case CS35L36_PAC_CTL1:
261 	case CS35L36_PAC_CTL2:
262 	case CS35L36_PAC_CTL3:
263 	case CS35L36_DEVICE_ID:
264 	case CS35L36_FAB_ID:
265 	case CS35L36_REV_ID:
266 	case CS35L36_PWR_CTRL1:
267 	case CS35L36_PWR_CTRL2:
268 	case CS35L36_PWR_CTRL3:
269 	case CS35L36_CTRL_OVRRIDE:
270 	case CS35L36_AMP_OUT_MUTE:
271 	case CS35L36_OTP_TRIM_STATUS:
272 	case CS35L36_DISCH_FILT:
273 	case CS35L36_PROTECT_REL_ERR:
274 	case CS35L36_PAD_INTERFACE:
275 	case CS35L36_PLL_CLK_CTRL:
276 	case CS35L36_GLOBAL_CLK_CTRL:
277 	case CS35L36_ADC_CLK_CTRL:
278 	case CS35L36_SWIRE_CLK_CTRL:
279 	case CS35L36_SP_SCLK_CLK_CTRL:
280 	case CS35L36_TST_FS_MON0:
281 	case CS35L36_MDSYNC_EN:
282 	case CS35L36_MDSYNC_TX_ID:
283 	case CS35L36_MDSYNC_PWR_CTRL:
284 	case CS35L36_MDSYNC_DATA_TX:
285 	case CS35L36_MDSYNC_TX_STATUS:
286 	case CS35L36_MDSYNC_RX_STATUS:
287 	case CS35L36_MDSYNC_ERR_STATUS:
288 	case CS35L36_BSTCVRT_VCTRL1:
289 	case CS35L36_BSTCVRT_VCTRL2:
290 	case CS35L36_BSTCVRT_PEAK_CUR:
291 	case CS35L36_BSTCVRT_SFT_RAMP:
292 	case CS35L36_BSTCVRT_COEFF:
293 	case CS35L36_BSTCVRT_SLOPE_LBST:
294 	case CS35L36_BSTCVRT_SW_FREQ:
295 	case CS35L36_BSTCVRT_DCM_CTRL:
296 	case CS35L36_BSTCVRT_DCM_MODE_FORCE:
297 	case CS35L36_BSTCVRT_OVERVOLT_CTRL:
298 	case CS35L36_BST_TST_MANUAL:
299 	case CS35L36_BST_ANA2_TEST:
300 	case CS35L36_VPI_LIMIT_MODE:
301 	case CS35L36_VPI_LIMIT_MINMAX:
302 	case CS35L36_VPI_VP_THLD:
303 	case CS35L36_VPI_TRACK_CTRL:
304 	case CS35L36_VPI_TRIG_MODE_CTRL:
305 	case CS35L36_VPI_TRIG_STEPS:
306 	case CS35L36_VI_SPKMON_FILT:
307 	case CS35L36_VI_SPKMON_GAIN:
308 	case CS35L36_VI_SPKMON_IP_SEL:
309 	case CS35L36_DTEMP_WARN_THLD:
310 	case CS35L36_DTEMP_STATUS:
311 	case CS35L36_VPVBST_FS_SEL:
312 	case CS35L36_VPVBST_VP_CTRL:
313 	case CS35L36_VPVBST_VBST_CTRL:
314 	case CS35L36_ASP_TX_PIN_CTRL:
315 	case CS35L36_ASP_RATE_CTRL:
316 	case CS35L36_ASP_FORMAT:
317 	case CS35L36_ASP_FRAME_CTRL:
318 	case CS35L36_ASP_TX1_TX2_SLOT:
319 	case CS35L36_ASP_TX3_TX4_SLOT:
320 	case CS35L36_ASP_TX5_TX6_SLOT:
321 	case CS35L36_ASP_TX7_TX8_SLOT:
322 	case CS35L36_ASP_RX1_SLOT:
323 	case CS35L36_ASP_RX_TX_EN:
324 	case CS35L36_ASP_RX1_SEL:
325 	case CS35L36_ASP_TX1_SEL:
326 	case CS35L36_ASP_TX2_SEL:
327 	case CS35L36_ASP_TX3_SEL:
328 	case CS35L36_ASP_TX4_SEL:
329 	case CS35L36_ASP_TX5_SEL:
330 	case CS35L36_ASP_TX6_SEL:
331 	case CS35L36_SWIRE_P1_TX1_SEL:
332 	case CS35L36_SWIRE_P1_TX2_SEL:
333 	case CS35L36_SWIRE_P2_TX1_SEL:
334 	case CS35L36_SWIRE_P2_TX2_SEL:
335 	case CS35L36_SWIRE_P2_TX3_SEL:
336 	case CS35L36_SWIRE_DP1_FIFO_CFG:
337 	case CS35L36_SWIRE_DP2_FIFO_CFG:
338 	case CS35L36_SWIRE_DP3_FIFO_CFG:
339 	case CS35L36_SWIRE_PCM_RX_DATA:
340 	case CS35L36_SWIRE_FS_SEL:
341 	case CS35L36_AMP_DIG_VOL_CTRL:
342 	case CS35L36_VPBR_CFG:
343 	case CS35L36_VBBR_CFG:
344 	case CS35L36_VPBR_STATUS:
345 	case CS35L36_VBBR_STATUS:
346 	case CS35L36_OVERTEMP_CFG:
347 	case CS35L36_AMP_ERR_VOL:
348 	case CS35L36_CLASSH_CFG:
349 	case CS35L36_CLASSH_FET_DRV_CFG:
350 	case CS35L36_NG_CFG:
351 	case CS35L36_AMP_GAIN_CTRL:
352 	case CS35L36_PWM_MOD_IO_CTRL:
353 	case CS35L36_PWM_MOD_STATUS:
354 	case CS35L36_DAC_MSM_CFG:
355 	case CS35L36_AMP_SLOPE_CTRL:
356 	case CS35L36_AMP_PDM_VOLUME:
357 	case CS35L36_AMP_PDM_RATE_CTRL:
358 	case CS35L36_PDM_CH_SEL:
359 	case CS35L36_AMP_NG_CTRL:
360 	case CS35L36_PDM_HIGHFILT_CTRL:
361 	case CS35L36_INT1_STATUS:
362 	case CS35L36_INT2_STATUS:
363 	case CS35L36_INT3_STATUS:
364 	case CS35L36_INT4_STATUS:
365 	case CS35L36_INT1_RAW_STATUS:
366 	case CS35L36_INT2_RAW_STATUS:
367 	case CS35L36_INT3_RAW_STATUS:
368 	case CS35L36_INT4_RAW_STATUS:
369 	case CS35L36_INT1_MASK:
370 	case CS35L36_INT2_MASK:
371 	case CS35L36_INT3_MASK:
372 	case CS35L36_INT4_MASK:
373 	case CS35L36_INT1_EDGE_LVL_CTRL:
374 	case CS35L36_INT3_EDGE_LVL_CTRL:
375 	case CS35L36_PAC_INT_STATUS:
376 	case CS35L36_PAC_INT_RAW_STATUS:
377 	case CS35L36_PAC_INT_FLUSH_CTRL:
378 	case CS35L36_PAC_INT0_CTRL:
379 	case CS35L36_PAC_INT1_CTRL:
380 	case CS35L36_PAC_INT2_CTRL:
381 	case CS35L36_PAC_INT3_CTRL:
382 	case CS35L36_PAC_INT4_CTRL:
383 	case CS35L36_PAC_INT5_CTRL:
384 	case CS35L36_PAC_INT6_CTRL:
385 	case CS35L36_PAC_INT7_CTRL:
386 		return true;
387 	default:
388 		if (reg >= CS35L36_PAC_PMEM_WORD0 &&
389 			reg <= CS35L36_PAC_PMEM_WORD1023)
390 			return true;
391 		else
392 			return false;
393 	}
394 }
395 
396 static bool cs35l36_precious_reg(struct device *dev, unsigned int reg)
397 {
398 	switch (reg) {
399 	case CS35L36_TESTKEY_CTRL:
400 	case CS35L36_USERKEY_CTL:
401 	case CS35L36_TST_FS_MON0:
402 		return true;
403 	default:
404 		return false;
405 	}
406 }
407 
408 static bool cs35l36_volatile_reg(struct device *dev, unsigned int reg)
409 {
410 	switch (reg) {
411 	case CS35L36_SW_RESET:
412 	case CS35L36_SW_REV:
413 	case CS35L36_HW_REV:
414 	case CS35L36_TESTKEY_CTRL:
415 	case CS35L36_USERKEY_CTL:
416 	case CS35L36_DEVICE_ID:
417 	case CS35L36_FAB_ID:
418 	case CS35L36_REV_ID:
419 	case CS35L36_INT1_STATUS:
420 	case CS35L36_INT2_STATUS:
421 	case CS35L36_INT3_STATUS:
422 	case CS35L36_INT4_STATUS:
423 	case CS35L36_INT1_RAW_STATUS:
424 	case CS35L36_INT2_RAW_STATUS:
425 	case CS35L36_INT3_RAW_STATUS:
426 	case CS35L36_INT4_RAW_STATUS:
427 	case CS35L36_INT1_MASK:
428 	case CS35L36_INT2_MASK:
429 	case CS35L36_INT3_MASK:
430 	case CS35L36_INT4_MASK:
431 	case CS35L36_INT1_EDGE_LVL_CTRL:
432 	case CS35L36_INT3_EDGE_LVL_CTRL:
433 	case CS35L36_PAC_INT_STATUS:
434 	case CS35L36_PAC_INT_RAW_STATUS:
435 	case CS35L36_PAC_INT_FLUSH_CTRL:
436 		return true;
437 	default:
438 		if (reg >= CS35L36_PAC_PMEM_WORD0 &&
439 			reg <= CS35L36_PAC_PMEM_WORD1023)
440 			return true;
441 		else
442 			return false;
443 	}
444 }
445 
446 static const DECLARE_TLV_DB_RANGE(dig_vol_tlv, 0, 912,
447 				  TLV_DB_MINMAX_ITEM(-10200, 1200));
448 static DECLARE_TLV_DB_SCALE(amp_gain_tlv, 0, 1, 1);
449 
450 static const char * const cs35l36_pcm_sftramp_text[] =  {
451 	"Off", ".5ms", "1ms", "2ms", "4ms", "8ms", "15ms", "30ms"};
452 
453 static SOC_ENUM_SINGLE_DECL(pcm_sft_ramp, CS35L36_AMP_DIG_VOL_CTRL, 0,
454 			    cs35l36_pcm_sftramp_text);
455 
456 static int cs35l36_ldm_sel_get(struct snd_kcontrol *kcontrol,
457 			       struct snd_ctl_elem_value *ucontrol)
458 {
459 	struct snd_soc_component *component =
460 			snd_soc_kcontrol_component(kcontrol);
461 	struct cs35l36_private *cs35l36 =
462 			snd_soc_component_get_drvdata(component);
463 
464 	ucontrol->value.integer.value[0] = cs35l36->ldm_mode_sel;
465 
466 	return 0;
467 }
468 
469 static int cs35l36_ldm_sel_put(struct snd_kcontrol *kcontrol,
470 			       struct snd_ctl_elem_value *ucontrol)
471 {
472 	struct snd_soc_component *component =
473 			snd_soc_kcontrol_component(kcontrol);
474 	struct cs35l36_private *cs35l36 =
475 			snd_soc_component_get_drvdata(component);
476 	int val = (ucontrol->value.integer.value[0]) ? CS35L36_NG_AMP_EN_MASK :
477 						       0;
478 
479 	cs35l36->ldm_mode_sel = val;
480 
481 	regmap_update_bits(cs35l36->regmap, CS35L36_NG_CFG,
482 			   CS35L36_NG_AMP_EN_MASK, val);
483 
484 	return 0;
485 }
486 
487 static const struct snd_kcontrol_new cs35l36_aud_controls[] = {
488 	SOC_SINGLE_SX_TLV("Digital PCM Volume", CS35L36_AMP_DIG_VOL_CTRL,
489 		3, 0x4D0, 0x390, dig_vol_tlv),
490 	SOC_SINGLE_TLV("Analog PCM Volume", CS35L36_AMP_GAIN_CTRL, 5, 0x13, 0,
491 		amp_gain_tlv),
492 	SOC_ENUM("PCM Soft Ramp", pcm_sft_ramp),
493 	SOC_SINGLE("Amp Gain Zero-Cross Switch", CS35L36_AMP_GAIN_CTRL,
494 		CS35L36_AMP_ZC_SHIFT, 1, 0),
495 	SOC_SINGLE("PDM LDM Enter Ramp Switch", CS35L36_DAC_MSM_CFG,
496 		CS35L36_PDM_LDM_ENTER_SHIFT, 1, 0),
497 	SOC_SINGLE("PDM LDM Exit Ramp Switch", CS35L36_DAC_MSM_CFG,
498 		CS35L36_PDM_LDM_EXIT_SHIFT, 1, 0),
499 	SOC_SINGLE_BOOL_EXT("LDM Select Switch", 0, cs35l36_ldm_sel_get,
500 		cs35l36_ldm_sel_put),
501 };
502 
503 static int cs35l36_main_amp_event(struct snd_soc_dapm_widget *w,
504 				  struct snd_kcontrol *kcontrol, int event)
505 {
506 	struct snd_soc_component *component =
507 			snd_soc_dapm_to_component(w->dapm);
508 	struct cs35l36_private *cs35l36 =
509 			snd_soc_component_get_drvdata(component);
510 	u32 reg;
511 
512 	switch (event) {
513 	case SND_SOC_DAPM_POST_PMU:
514 		regmap_update_bits(cs35l36->regmap, CS35L36_PWR_CTRL1,
515 				   CS35L36_GLOBAL_EN_MASK,
516 				   1 << CS35L36_GLOBAL_EN_SHIFT);
517 
518 		usleep_range(2000, 2100);
519 
520 		regmap_read(cs35l36->regmap, CS35L36_INT4_RAW_STATUS, &reg);
521 
522 		if (WARN_ON_ONCE(reg & CS35L36_PLL_UNLOCK_MASK))
523 			dev_crit(cs35l36->dev, "PLL Unlocked\n");
524 
525 		regmap_update_bits(cs35l36->regmap, CS35L36_ASP_RX1_SEL,
526 				   CS35L36_PCM_RX_SEL_MASK,
527 				   CS35L36_PCM_RX_SEL_PCM);
528 		regmap_update_bits(cs35l36->regmap, CS35L36_AMP_OUT_MUTE,
529 				   CS35L36_AMP_MUTE_MASK,
530 				   0 << CS35L36_AMP_MUTE_SHIFT);
531 		break;
532 	case SND_SOC_DAPM_PRE_PMD:
533 		regmap_update_bits(cs35l36->regmap, CS35L36_ASP_RX1_SEL,
534 				   CS35L36_PCM_RX_SEL_MASK,
535 				   CS35L36_PCM_RX_SEL_ZERO);
536 		regmap_update_bits(cs35l36->regmap, CS35L36_AMP_OUT_MUTE,
537 				   CS35L36_AMP_MUTE_MASK,
538 				   1 << CS35L36_AMP_MUTE_SHIFT);
539 		break;
540 	case SND_SOC_DAPM_POST_PMD:
541 		regmap_update_bits(cs35l36->regmap, CS35L36_PWR_CTRL1,
542 				   CS35L36_GLOBAL_EN_MASK,
543 				   0 << CS35L36_GLOBAL_EN_SHIFT);
544 
545 		usleep_range(2000, 2100);
546 		break;
547 	default:
548 		dev_dbg(component->dev, "Invalid event = 0x%x\n", event);
549 		return -EINVAL;
550 	}
551 
552 	return 0;
553 }
554 
555 static int cs35l36_boost_event(struct snd_soc_dapm_widget *w,
556 			       struct snd_kcontrol *kcontrol, int event)
557 {
558 	struct snd_soc_component *component =
559 			snd_soc_dapm_to_component(w->dapm);
560 	struct cs35l36_private *cs35l36 =
561 			snd_soc_component_get_drvdata(component);
562 
563 	switch (event) {
564 	case SND_SOC_DAPM_POST_PMU:
565 		if (!cs35l36->pdata.extern_boost)
566 			regmap_update_bits(cs35l36->regmap, CS35L36_PWR_CTRL2,
567 					   CS35L36_BST_EN_MASK,
568 					   CS35L36_BST_EN <<
569 					   CS35L36_BST_EN_SHIFT);
570 		break;
571 	case SND_SOC_DAPM_POST_PMD:
572 		if (!cs35l36->pdata.extern_boost)
573 			regmap_update_bits(cs35l36->regmap, CS35L36_PWR_CTRL2,
574 					   CS35L36_BST_EN_MASK,
575 					   CS35L36_BST_DIS_VP <<
576 					   CS35L36_BST_EN_SHIFT);
577 		break;
578 	default:
579 		dev_dbg(component->dev, "Invalid event = 0x%x\n", event);
580 		return -EINVAL;
581 	}
582 
583 	return 0;
584 }
585 
586 static const char * const cs35l36_chan_text[] = {
587 	"RX1",
588 	"RX2",
589 };
590 
591 static SOC_ENUM_SINGLE_DECL(chansel_enum, CS35L36_ASP_RX1_SLOT, 0,
592 			    cs35l36_chan_text);
593 
594 static const struct snd_kcontrol_new cs35l36_chan_mux =
595 		SOC_DAPM_ENUM("Input Mux", chansel_enum);
596 
597 static const struct snd_kcontrol_new amp_enable_ctrl =
598 		SOC_DAPM_SINGLE_AUTODISABLE("Switch", CS35L36_AMP_OUT_MUTE,
599 					    CS35L36_AMP_MUTE_SHIFT, 1, 1);
600 
601 static const struct snd_kcontrol_new boost_ctrl =
602 		SOC_DAPM_SINGLE_VIRT("Switch", 1);
603 
604 static const char * const asp_tx_src_text[] = {
605 	"Zero Fill", "ASPRX1", "VMON", "IMON", "ERRVOL", "VPMON", "VBSTMON"
606 };
607 
608 static const unsigned int asp_tx_src_values[] = {
609 	0x00, 0x08, 0x18, 0x19, 0x20, 0x28, 0x29
610 };
611 
612 static SOC_VALUE_ENUM_SINGLE_DECL(asp_tx1_src_enum, CS35L36_ASP_TX1_SEL, 0,
613 				  CS35L36_APS_TX_SEL_MASK, asp_tx_src_text,
614 				  asp_tx_src_values);
615 
616 static const struct snd_kcontrol_new asp_tx1_src =
617 		SOC_DAPM_ENUM("ASPTX1SRC", asp_tx1_src_enum);
618 
619 static SOC_VALUE_ENUM_SINGLE_DECL(asp_tx2_src_enum, CS35L36_ASP_TX2_SEL, 0,
620 				  CS35L36_APS_TX_SEL_MASK, asp_tx_src_text,
621 				  asp_tx_src_values);
622 
623 static const struct snd_kcontrol_new asp_tx2_src =
624 		SOC_DAPM_ENUM("ASPTX2SRC", asp_tx2_src_enum);
625 
626 static SOC_VALUE_ENUM_SINGLE_DECL(asp_tx3_src_enum, CS35L36_ASP_TX3_SEL, 0,
627 				  CS35L36_APS_TX_SEL_MASK, asp_tx_src_text,
628 				  asp_tx_src_values);
629 
630 static const struct snd_kcontrol_new asp_tx3_src =
631 		SOC_DAPM_ENUM("ASPTX3SRC", asp_tx3_src_enum);
632 
633 static SOC_VALUE_ENUM_SINGLE_DECL(asp_tx4_src_enum, CS35L36_ASP_TX4_SEL, 0,
634 				  CS35L36_APS_TX_SEL_MASK, asp_tx_src_text,
635 				  asp_tx_src_values);
636 
637 static const struct snd_kcontrol_new asp_tx4_src =
638 		SOC_DAPM_ENUM("ASPTX4SRC", asp_tx4_src_enum);
639 
640 static SOC_VALUE_ENUM_SINGLE_DECL(asp_tx5_src_enum, CS35L36_ASP_TX5_SEL, 0,
641 				  CS35L36_APS_TX_SEL_MASK, asp_tx_src_text,
642 				  asp_tx_src_values);
643 
644 static const struct snd_kcontrol_new asp_tx5_src =
645 		SOC_DAPM_ENUM("ASPTX5SRC", asp_tx5_src_enum);
646 
647 static SOC_VALUE_ENUM_SINGLE_DECL(asp_tx6_src_enum, CS35L36_ASP_TX6_SEL, 0,
648 				  CS35L36_APS_TX_SEL_MASK, asp_tx_src_text,
649 				  asp_tx_src_values);
650 
651 static const struct snd_kcontrol_new asp_tx6_src =
652 		SOC_DAPM_ENUM("ASPTX6SRC", asp_tx6_src_enum);
653 
654 static const struct snd_soc_dapm_widget cs35l36_dapm_widgets[] = {
655 	SND_SOC_DAPM_MUX("Channel Mux", SND_SOC_NOPM, 0, 0, &cs35l36_chan_mux),
656 	SND_SOC_DAPM_AIF_IN("SDIN", NULL, 0, CS35L36_ASP_RX_TX_EN, 16, 0),
657 
658 	SND_SOC_DAPM_OUT_DRV_E("Main AMP", CS35L36_PWR_CTRL2, 0, 0, NULL, 0,
659 			       cs35l36_main_amp_event, SND_SOC_DAPM_POST_PMD |
660 			       SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
661 
662 	SND_SOC_DAPM_OUTPUT("SPK"),
663 	SND_SOC_DAPM_SWITCH("AMP Enable", SND_SOC_NOPM, 0, 1, &amp_enable_ctrl),
664 	SND_SOC_DAPM_MIXER("CLASS H", CS35L36_PWR_CTRL3, 4, 0, NULL, 0),
665 	SND_SOC_DAPM_SWITCH_E("BOOST Enable", SND_SOC_NOPM, 0, 0, &boost_ctrl,
666 			      cs35l36_boost_event, SND_SOC_DAPM_POST_PMD |
667 			      SND_SOC_DAPM_POST_PMU),
668 
669 	SND_SOC_DAPM_AIF_OUT("ASPTX1", NULL, 0, CS35L36_ASP_RX_TX_EN, 0, 0),
670 	SND_SOC_DAPM_AIF_OUT("ASPTX2", NULL, 1, CS35L36_ASP_RX_TX_EN, 1, 0),
671 	SND_SOC_DAPM_AIF_OUT("ASPTX3", NULL, 2, CS35L36_ASP_RX_TX_EN, 2, 0),
672 	SND_SOC_DAPM_AIF_OUT("ASPTX4", NULL, 3, CS35L36_ASP_RX_TX_EN, 3, 0),
673 	SND_SOC_DAPM_AIF_OUT("ASPTX5", NULL, 4, CS35L36_ASP_RX_TX_EN, 4, 0),
674 	SND_SOC_DAPM_AIF_OUT("ASPTX6", NULL, 5, CS35L36_ASP_RX_TX_EN, 5, 0),
675 
676 	SND_SOC_DAPM_MUX("ASPTX1SRC", SND_SOC_NOPM, 0, 0, &asp_tx1_src),
677 	SND_SOC_DAPM_MUX("ASPTX2SRC", SND_SOC_NOPM, 0, 0, &asp_tx2_src),
678 	SND_SOC_DAPM_MUX("ASPTX3SRC", SND_SOC_NOPM, 0, 0, &asp_tx3_src),
679 	SND_SOC_DAPM_MUX("ASPTX4SRC", SND_SOC_NOPM, 0, 0, &asp_tx4_src),
680 	SND_SOC_DAPM_MUX("ASPTX5SRC", SND_SOC_NOPM, 0, 0, &asp_tx5_src),
681 	SND_SOC_DAPM_MUX("ASPTX6SRC", SND_SOC_NOPM, 0, 0, &asp_tx6_src),
682 
683 	SND_SOC_DAPM_ADC("VMON ADC", NULL, CS35L36_PWR_CTRL2, 12, 0),
684 	SND_SOC_DAPM_ADC("IMON ADC", NULL, CS35L36_PWR_CTRL2, 13, 0),
685 	SND_SOC_DAPM_ADC("VPMON ADC", NULL, CS35L36_PWR_CTRL2, 8, 0),
686 	SND_SOC_DAPM_ADC("VBSTMON ADC", NULL, CS35L36_PWR_CTRL2, 9, 0),
687 
688 	SND_SOC_DAPM_INPUT("VP"),
689 	SND_SOC_DAPM_INPUT("VBST"),
690 	SND_SOC_DAPM_INPUT("VSENSE"),
691 };
692 
693 static const struct snd_soc_dapm_route cs35l36_audio_map[] = {
694 	{"VPMON ADC", NULL, "VP"},
695 	{"VBSTMON ADC", NULL, "VBST"},
696 	{"IMON ADC", NULL, "VSENSE"},
697 	{"VMON ADC", NULL, "VSENSE"},
698 
699 	{"ASPTX1SRC", "IMON", "IMON ADC"},
700 	{"ASPTX1SRC", "VMON", "VMON ADC"},
701 	{"ASPTX1SRC", "VBSTMON", "VBSTMON ADC"},
702 	{"ASPTX1SRC", "VPMON", "VPMON ADC"},
703 
704 	{"ASPTX2SRC", "IMON", "IMON ADC"},
705 	{"ASPTX2SRC", "VMON", "VMON ADC"},
706 	{"ASPTX2SRC", "VBSTMON", "VBSTMON ADC"},
707 	{"ASPTX2SRC", "VPMON", "VPMON ADC"},
708 
709 	{"ASPTX3SRC", "IMON", "IMON ADC"},
710 	{"ASPTX3SRC", "VMON", "VMON ADC"},
711 	{"ASPTX3SRC", "VBSTMON", "VBSTMON ADC"},
712 	{"ASPTX3SRC", "VPMON", "VPMON ADC"},
713 
714 	{"ASPTX4SRC", "IMON", "IMON ADC"},
715 	{"ASPTX4SRC", "VMON", "VMON ADC"},
716 	{"ASPTX4SRC", "VBSTMON", "VBSTMON ADC"},
717 	{"ASPTX4SRC", "VPMON", "VPMON ADC"},
718 
719 	{"ASPTX5SRC", "IMON", "IMON ADC"},
720 	{"ASPTX5SRC", "VMON", "VMON ADC"},
721 	{"ASPTX5SRC", "VBSTMON", "VBSTMON ADC"},
722 	{"ASPTX5SRC", "VPMON", "VPMON ADC"},
723 
724 	{"ASPTX6SRC", "IMON", "IMON ADC"},
725 	{"ASPTX6SRC", "VMON", "VMON ADC"},
726 	{"ASPTX6SRC", "VBSTMON", "VBSTMON ADC"},
727 	{"ASPTX6SRC", "VPMON", "VPMON ADC"},
728 
729 	{"ASPTX1", NULL, "ASPTX1SRC"},
730 	{"ASPTX2", NULL, "ASPTX2SRC"},
731 	{"ASPTX3", NULL, "ASPTX3SRC"},
732 	{"ASPTX4", NULL, "ASPTX4SRC"},
733 	{"ASPTX5", NULL, "ASPTX5SRC"},
734 	{"ASPTX6", NULL, "ASPTX6SRC"},
735 
736 	{"AMP Capture", NULL, "ASPTX1"},
737 	{"AMP Capture", NULL, "ASPTX2"},
738 	{"AMP Capture", NULL, "ASPTX3"},
739 	{"AMP Capture", NULL, "ASPTX4"},
740 	{"AMP Capture", NULL, "ASPTX5"},
741 	{"AMP Capture", NULL, "ASPTX6"},
742 
743 	{"AMP Enable", "Switch", "AMP Playback"},
744 	{"SDIN", NULL, "AMP Enable"},
745 	{"Channel Mux", "RX1", "SDIN"},
746 	{"Channel Mux", "RX2", "SDIN"},
747 	{"BOOST Enable", "Switch", "Channel Mux"},
748 	{"CLASS H", NULL, "BOOST Enable"},
749 	{"Main AMP", NULL, "Channel Mux"},
750 	{"Main AMP", NULL, "CLASS H"},
751 	{"SPK", NULL, "Main AMP"},
752 };
753 
754 static int cs35l36_set_dai_fmt(struct snd_soc_dai *component_dai,
755 			       unsigned int fmt)
756 {
757 	struct cs35l36_private *cs35l36 =
758 			snd_soc_component_get_drvdata(component_dai->component);
759 	unsigned int asp_fmt, lrclk_fmt, sclk_fmt, clock_provider, clk_frc;
760 
761 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
762 	case SND_SOC_DAIFMT_CBP_CFP:
763 		clock_provider = 1;
764 		break;
765 	case SND_SOC_DAIFMT_CBC_CFC:
766 		clock_provider = 0;
767 		break;
768 	default:
769 		return -EINVAL;
770 	}
771 
772 	regmap_update_bits(cs35l36->regmap, CS35L36_ASP_TX_PIN_CTRL,
773 				CS35L36_SCLK_MSTR_MASK,
774 				clock_provider << CS35L36_SCLK_MSTR_SHIFT);
775 	regmap_update_bits(cs35l36->regmap, CS35L36_ASP_RATE_CTRL,
776 				CS35L36_LRCLK_MSTR_MASK,
777 				clock_provider << CS35L36_LRCLK_MSTR_SHIFT);
778 
779 	switch (fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
780 	case SND_SOC_DAIFMT_CONT:
781 		clk_frc = 1;
782 		break;
783 	case SND_SOC_DAIFMT_GATED:
784 		clk_frc = 0;
785 		break;
786 	default:
787 		return -EINVAL;
788 	}
789 
790 	regmap_update_bits(cs35l36->regmap, CS35L36_ASP_TX_PIN_CTRL,
791 			   CS35L36_SCLK_FRC_MASK, clk_frc <<
792 			   CS35L36_SCLK_FRC_SHIFT);
793 	regmap_update_bits(cs35l36->regmap, CS35L36_ASP_RATE_CTRL,
794 			   CS35L36_LRCLK_FRC_MASK, clk_frc <<
795 			   CS35L36_LRCLK_FRC_SHIFT);
796 
797 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
798 	case SND_SOC_DAIFMT_DSP_A:
799 		asp_fmt = 0;
800 		break;
801 	case SND_SOC_DAIFMT_I2S:
802 		asp_fmt = 2;
803 		break;
804 	default:
805 		return -EINVAL;
806 	}
807 
808 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
809 	case SND_SOC_DAIFMT_NB_IF:
810 		lrclk_fmt = 1;
811 		sclk_fmt = 0;
812 		break;
813 	case SND_SOC_DAIFMT_IB_NF:
814 		lrclk_fmt = 0;
815 		sclk_fmt = 1;
816 		break;
817 	case SND_SOC_DAIFMT_IB_IF:
818 		lrclk_fmt = 1;
819 		sclk_fmt = 1;
820 		break;
821 	case SND_SOC_DAIFMT_NB_NF:
822 		lrclk_fmt = 0;
823 		sclk_fmt = 0;
824 		break;
825 	default:
826 		return -EINVAL;
827 	}
828 
829 	regmap_update_bits(cs35l36->regmap, CS35L36_ASP_RATE_CTRL,
830 			   CS35L36_LRCLK_INV_MASK,
831 			   lrclk_fmt << CS35L36_LRCLK_INV_SHIFT);
832 	regmap_update_bits(cs35l36->regmap, CS35L36_ASP_TX_PIN_CTRL,
833 			   CS35L36_SCLK_INV_MASK,
834 			   sclk_fmt << CS35L36_SCLK_INV_SHIFT);
835 	regmap_update_bits(cs35l36->regmap, CS35L36_ASP_FORMAT,
836 			   CS35L36_ASP_FMT_MASK, asp_fmt);
837 
838 	return 0;
839 }
840 
841 struct cs35l36_global_fs_config {
842 	int rate;
843 	int fs_cfg;
844 };
845 
846 static const struct cs35l36_global_fs_config cs35l36_fs_rates[] = {
847 	{12000, 0x01},
848 	{24000, 0x02},
849 	{48000, 0x03},
850 	{96000, 0x04},
851 	{192000, 0x05},
852 	{384000, 0x06},
853 	{11025, 0x09},
854 	{22050, 0x0A},
855 	{44100, 0x0B},
856 	{88200, 0x0C},
857 	{176400, 0x0D},
858 	{8000, 0x11},
859 	{16000, 0x12},
860 	{32000, 0x13},
861 };
862 
863 static int cs35l36_pcm_hw_params(struct snd_pcm_substream *substream,
864 				 struct snd_pcm_hw_params *params,
865 				 struct snd_soc_dai *dai)
866 {
867 	struct cs35l36_private *cs35l36 =
868 			snd_soc_component_get_drvdata(dai->component);
869 	unsigned int asp_width, global_fs = params_rate(params);
870 	int i;
871 
872 	for (i = 0; i < ARRAY_SIZE(cs35l36_fs_rates); i++) {
873 		if (global_fs == cs35l36_fs_rates[i].rate)
874 			regmap_update_bits(cs35l36->regmap,
875 					   CS35L36_GLOBAL_CLK_CTRL,
876 					   CS35L36_GLOBAL_FS_MASK,
877 					   cs35l36_fs_rates[i].fs_cfg <<
878 					   CS35L36_GLOBAL_FS_SHIFT);
879 	}
880 
881 	switch (params_width(params)) {
882 	case 16:
883 		asp_width = CS35L36_ASP_WIDTH_16;
884 		break;
885 	case 24:
886 		asp_width = CS35L36_ASP_WIDTH_24;
887 		break;
888 	case 32:
889 		asp_width = CS35L36_ASP_WIDTH_32;
890 		break;
891 	default:
892 		return -EINVAL;
893 	}
894 
895 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
896 		regmap_update_bits(cs35l36->regmap, CS35L36_ASP_FRAME_CTRL,
897 				   CS35L36_ASP_RX_WIDTH_MASK,
898 				   asp_width << CS35L36_ASP_RX_WIDTH_SHIFT);
899 	} else {
900 		regmap_update_bits(cs35l36->regmap, CS35L36_ASP_FRAME_CTRL,
901 				   CS35L36_ASP_TX_WIDTH_MASK,
902 				   asp_width << CS35L36_ASP_TX_WIDTH_SHIFT);
903 	}
904 
905 	return 0;
906 }
907 
908 static int cs35l36_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
909 				  unsigned int freq, int dir)
910 {
911 	struct snd_soc_component *component = dai->component;
912 	struct cs35l36_private *cs35l36 =
913 			snd_soc_component_get_drvdata(component);
914 	int fs1, fs2;
915 
916 	if (freq > CS35L36_FS_NOM_6MHZ) {
917 		fs1 = CS35L36_FS1_DEFAULT_VAL;
918 		fs2 = CS35L36_FS2_DEFAULT_VAL;
919 	} else {
920 		fs1 = 3 * DIV_ROUND_UP(CS35L36_FS_NOM_6MHZ * 4, freq) + 4;
921 		fs2 = 5 * DIV_ROUND_UP(CS35L36_FS_NOM_6MHZ * 4, freq) + 4;
922 	}
923 
924 	regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
925 			CS35L36_TEST_UNLOCK1);
926 	regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
927 			CS35L36_TEST_UNLOCK2);
928 
929 	regmap_update_bits(cs35l36->regmap, CS35L36_TST_FS_MON0,
930 			   CS35L36_FS1_WINDOW_MASK | CS35L36_FS2_WINDOW_MASK,
931 			   fs1 | (fs2 << CS35L36_FS2_WINDOW_SHIFT));
932 
933 	regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
934 			CS35L36_TEST_LOCK1);
935 	regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
936 			CS35L36_TEST_LOCK2);
937 	return 0;
938 }
939 
940 static const struct cs35l36_pll_config *cs35l36_get_clk_config(
941 		struct cs35l36_private *cs35l36, int freq)
942 {
943 	int i;
944 
945 	for (i = 0; i < ARRAY_SIZE(cs35l36_pll_sysclk); i++) {
946 		if (cs35l36_pll_sysclk[i].freq == freq)
947 			return &cs35l36_pll_sysclk[i];
948 	}
949 
950 	return NULL;
951 }
952 
953 static const unsigned int cs35l36_src_rates[] = {
954 	8000, 12000, 11025, 16000, 22050, 24000, 32000,
955 	44100, 48000, 88200, 96000, 176400, 192000, 384000
956 };
957 
958 static const struct snd_pcm_hw_constraint_list cs35l36_constraints = {
959 	.count  = ARRAY_SIZE(cs35l36_src_rates),
960 	.list   = cs35l36_src_rates,
961 };
962 
963 static int cs35l36_pcm_startup(struct snd_pcm_substream *substream,
964 			       struct snd_soc_dai *dai)
965 {
966 	snd_pcm_hw_constraint_list(substream->runtime, 0,
967 				SNDRV_PCM_HW_PARAM_RATE, &cs35l36_constraints);
968 
969 	return 0;
970 }
971 
972 static const struct snd_soc_dai_ops cs35l36_ops = {
973 	.startup = cs35l36_pcm_startup,
974 	.set_fmt = cs35l36_set_dai_fmt,
975 	.hw_params = cs35l36_pcm_hw_params,
976 	.set_sysclk = cs35l36_dai_set_sysclk,
977 };
978 
979 static struct snd_soc_dai_driver cs35l36_dai[] = {
980 	{
981 		.name = "cs35l36-pcm",
982 		.id = 0,
983 		.playback = {
984 			.stream_name = "AMP Playback",
985 			.channels_min = 1,
986 			.channels_max = 8,
987 			.rates = SNDRV_PCM_RATE_KNOT,
988 			.formats = CS35L36_RX_FORMATS,
989 		},
990 		.capture = {
991 			.stream_name = "AMP Capture",
992 			.channels_min = 1,
993 			.channels_max = 8,
994 			.rates = SNDRV_PCM_RATE_KNOT,
995 			.formats = CS35L36_TX_FORMATS,
996 		},
997 		.ops = &cs35l36_ops,
998 		.symmetric_rate = 1,
999 	},
1000 };
1001 
1002 static int cs35l36_component_set_sysclk(struct snd_soc_component *component,
1003 				int clk_id, int source, unsigned int freq,
1004 				int dir)
1005 {
1006 	struct cs35l36_private *cs35l36 =
1007 			snd_soc_component_get_drvdata(component);
1008 	const struct cs35l36_pll_config *clk_cfg;
1009 	int prev_clksrc;
1010 	bool pdm_switch;
1011 
1012 	prev_clksrc = cs35l36->clksrc;
1013 
1014 	switch (clk_id) {
1015 	case 0:
1016 		cs35l36->clksrc = CS35L36_PLLSRC_SCLK;
1017 		break;
1018 	case 1:
1019 		cs35l36->clksrc = CS35L36_PLLSRC_LRCLK;
1020 		break;
1021 	case 2:
1022 		cs35l36->clksrc = CS35L36_PLLSRC_PDMCLK;
1023 		break;
1024 	case 3:
1025 		cs35l36->clksrc = CS35L36_PLLSRC_SELF;
1026 		break;
1027 	case 4:
1028 		cs35l36->clksrc = CS35L36_PLLSRC_MCLK;
1029 		break;
1030 	default:
1031 		return -EINVAL;
1032 	}
1033 
1034 	clk_cfg = cs35l36_get_clk_config(cs35l36, freq);
1035 	if (clk_cfg == NULL) {
1036 		dev_err(component->dev, "Invalid CLK Config Freq: %d\n", freq);
1037 		return -EINVAL;
1038 	}
1039 
1040 	regmap_update_bits(cs35l36->regmap, CS35L36_PLL_CLK_CTRL,
1041 			   CS35L36_PLL_OPENLOOP_MASK,
1042 			   1 << CS35L36_PLL_OPENLOOP_SHIFT);
1043 	regmap_update_bits(cs35l36->regmap, CS35L36_PLL_CLK_CTRL,
1044 			   CS35L36_REFCLK_FREQ_MASK,
1045 			   clk_cfg->clk_cfg << CS35L36_REFCLK_FREQ_SHIFT);
1046 	regmap_update_bits(cs35l36->regmap, CS35L36_PLL_CLK_CTRL,
1047 			   CS35L36_PLL_REFCLK_EN_MASK,
1048 			   0 << CS35L36_PLL_REFCLK_EN_SHIFT);
1049 	regmap_update_bits(cs35l36->regmap, CS35L36_PLL_CLK_CTRL,
1050 			   CS35L36_PLL_CLK_SEL_MASK,
1051 			   cs35l36->clksrc);
1052 	regmap_update_bits(cs35l36->regmap, CS35L36_PLL_CLK_CTRL,
1053 			   CS35L36_PLL_OPENLOOP_MASK,
1054 			   0 << CS35L36_PLL_OPENLOOP_SHIFT);
1055 	regmap_update_bits(cs35l36->regmap, CS35L36_PLL_CLK_CTRL,
1056 			   CS35L36_PLL_REFCLK_EN_MASK,
1057 			   1 << CS35L36_PLL_REFCLK_EN_SHIFT);
1058 
1059 	if (cs35l36->rev_id == CS35L36_REV_A0) {
1060 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1061 			     CS35L36_TEST_UNLOCK1);
1062 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1063 			     CS35L36_TEST_UNLOCK2);
1064 
1065 		regmap_write(cs35l36->regmap, CS35L36_DCO_CTRL, 0x00036DA8);
1066 		regmap_write(cs35l36->regmap, CS35L36_MISC_CTRL, 0x0100EE0E);
1067 
1068 		regmap_update_bits(cs35l36->regmap, CS35L36_PLL_LOOP_PARAMS,
1069 				   CS35L36_PLL_IGAIN_MASK,
1070 				   CS35L36_PLL_IGAIN <<
1071 				   CS35L36_PLL_IGAIN_SHIFT);
1072 		regmap_update_bits(cs35l36->regmap, CS35L36_PLL_LOOP_PARAMS,
1073 				   CS35L36_PLL_FFL_IGAIN_MASK,
1074 				   clk_cfg->fll_igain);
1075 
1076 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1077 			     CS35L36_TEST_LOCK1);
1078 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1079 			     CS35L36_TEST_LOCK2);
1080 	}
1081 
1082 	if (cs35l36->clksrc == CS35L36_PLLSRC_PDMCLK) {
1083 		pdm_switch = cs35l36->ldm_mode_sel &&
1084 			     (prev_clksrc != CS35L36_PLLSRC_PDMCLK);
1085 
1086 		if (pdm_switch)
1087 			regmap_update_bits(cs35l36->regmap, CS35L36_NG_CFG,
1088 					   CS35L36_NG_DELAY_MASK,
1089 					   0 << CS35L36_NG_DELAY_SHIFT);
1090 
1091 		regmap_update_bits(cs35l36->regmap, CS35L36_DAC_MSM_CFG,
1092 				   CS35L36_PDM_MODE_MASK,
1093 				   1 << CS35L36_PDM_MODE_SHIFT);
1094 
1095 		if (pdm_switch)
1096 			regmap_update_bits(cs35l36->regmap, CS35L36_NG_CFG,
1097 					   CS35L36_NG_DELAY_MASK,
1098 					   3 << CS35L36_NG_DELAY_SHIFT);
1099 	} else {
1100 		pdm_switch = cs35l36->ldm_mode_sel &&
1101 			     (prev_clksrc == CS35L36_PLLSRC_PDMCLK);
1102 
1103 		if (pdm_switch)
1104 			regmap_update_bits(cs35l36->regmap, CS35L36_NG_CFG,
1105 					   CS35L36_NG_DELAY_MASK,
1106 					   0 << CS35L36_NG_DELAY_SHIFT);
1107 
1108 		regmap_update_bits(cs35l36->regmap, CS35L36_DAC_MSM_CFG,
1109 				   CS35L36_PDM_MODE_MASK,
1110 				   0 << CS35L36_PDM_MODE_SHIFT);
1111 
1112 		if (pdm_switch)
1113 			regmap_update_bits(cs35l36->regmap, CS35L36_NG_CFG,
1114 					   CS35L36_NG_DELAY_MASK,
1115 					   3 << CS35L36_NG_DELAY_SHIFT);
1116 	}
1117 
1118 	return 0;
1119 }
1120 
1121 static int cs35l36_boost_inductor(struct cs35l36_private *cs35l36, int inductor)
1122 {
1123 	regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_COEFF,
1124 			   CS35L36_BSTCVRT_K1_MASK, 0x3C);
1125 	regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_COEFF,
1126 			   CS35L36_BSTCVRT_K2_MASK,
1127 			   0x3C << CS35L36_BSTCVRT_K2_SHIFT);
1128 	regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_SW_FREQ,
1129 			   CS35L36_BSTCVRT_CCMFREQ_MASK, 0x00);
1130 
1131 	switch (inductor) {
1132 	case 1000: /* 1 uH */
1133 		regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_SLOPE_LBST,
1134 				   CS35L36_BSTCVRT_SLOPE_MASK,
1135 				   0x75 << CS35L36_BSTCVRT_SLOPE_SHIFT);
1136 		regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_SLOPE_LBST,
1137 				   CS35L36_BSTCVRT_LBSTVAL_MASK, 0x00);
1138 		break;
1139 	case 1200: /* 1.2 uH */
1140 		regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_SLOPE_LBST,
1141 				   CS35L36_BSTCVRT_SLOPE_MASK,
1142 				   0x6B << CS35L36_BSTCVRT_SLOPE_SHIFT);
1143 		regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_SLOPE_LBST,
1144 				   CS35L36_BSTCVRT_LBSTVAL_MASK, 0x01);
1145 		break;
1146 	default:
1147 		dev_err(cs35l36->dev, "%s Invalid Inductor Value %d uH\n",
1148 			__func__, inductor);
1149 		return -EINVAL;
1150 	}
1151 
1152 	return 0;
1153 }
1154 
1155 static int cs35l36_component_probe(struct snd_soc_component *component)
1156 {
1157 	struct cs35l36_private *cs35l36 =
1158 			snd_soc_component_get_drvdata(component);
1159 	int ret;
1160 
1161 	if ((cs35l36->rev_id == CS35L36_REV_A0) && cs35l36->pdata.dcm_mode) {
1162 		regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_DCM_CTRL,
1163 				   CS35L36_DCM_AUTO_MASK,
1164 				   CS35L36_DCM_AUTO_MASK);
1165 
1166 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1167 			     CS35L36_TEST_UNLOCK1);
1168 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1169 			     CS35L36_TEST_UNLOCK2);
1170 
1171 		regmap_update_bits(cs35l36->regmap, CS35L36_BST_TST_MANUAL,
1172 				   CS35L36_BST_MAN_IPKCOMP_MASK,
1173 				   0 << CS35L36_BST_MAN_IPKCOMP_SHIFT);
1174 		regmap_update_bits(cs35l36->regmap, CS35L36_BST_TST_MANUAL,
1175 				   CS35L36_BST_MAN_IPKCOMP_EN_MASK,
1176 				   CS35L36_BST_MAN_IPKCOMP_EN_MASK);
1177 
1178 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1179 				CS35L36_TEST_LOCK1);
1180 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1181 				CS35L36_TEST_LOCK2);
1182 	}
1183 
1184 	if (cs35l36->pdata.amp_pcm_inv)
1185 		regmap_update_bits(cs35l36->regmap, CS35L36_AMP_DIG_VOL_CTRL,
1186 				   CS35L36_AMP_PCM_INV_MASK,
1187 				   CS35L36_AMP_PCM_INV_MASK);
1188 
1189 	if (cs35l36->pdata.multi_amp_mode)
1190 		regmap_update_bits(cs35l36->regmap, CS35L36_ASP_TX_PIN_CTRL,
1191 				   CS35L36_ASP_TX_HIZ_MASK,
1192 				   CS35L36_ASP_TX_HIZ_MASK);
1193 
1194 	if (cs35l36->pdata.imon_pol_inv)
1195 		regmap_update_bits(cs35l36->regmap, CS35L36_VI_SPKMON_FILT,
1196 				   CS35L36_IMON_POL_MASK, 0);
1197 
1198 	if (cs35l36->pdata.vmon_pol_inv)
1199 		regmap_update_bits(cs35l36->regmap, CS35L36_VI_SPKMON_FILT,
1200 				   CS35L36_VMON_POL_MASK, 0);
1201 
1202 	if (cs35l36->pdata.bst_vctl)
1203 		regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_VCTRL1,
1204 				   CS35L35_BSTCVRT_CTL_MASK,
1205 				   cs35l36->pdata.bst_vctl);
1206 
1207 	if (cs35l36->pdata.bst_vctl_sel)
1208 		regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_VCTRL2,
1209 				   CS35L35_BSTCVRT_CTL_SEL_MASK,
1210 				   cs35l36->pdata.bst_vctl_sel);
1211 
1212 	if (cs35l36->pdata.bst_ipk)
1213 		regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_PEAK_CUR,
1214 				   CS35L36_BST_IPK_MASK,
1215 				   cs35l36->pdata.bst_ipk);
1216 
1217 	if (cs35l36->pdata.boost_ind) {
1218 		ret = cs35l36_boost_inductor(cs35l36, cs35l36->pdata.boost_ind);
1219 		if (ret < 0) {
1220 			dev_err(cs35l36->dev,
1221 				"Boost inductor config failed(%d)\n", ret);
1222 			return ret;
1223 		}
1224 	}
1225 
1226 	if (cs35l36->pdata.temp_warn_thld)
1227 		regmap_update_bits(cs35l36->regmap, CS35L36_DTEMP_WARN_THLD,
1228 				   CS35L36_TEMP_THLD_MASK,
1229 				   cs35l36->pdata.temp_warn_thld);
1230 
1231 	if (cs35l36->pdata.irq_drv_sel)
1232 		regmap_update_bits(cs35l36->regmap, CS35L36_PAD_INTERFACE,
1233 				   CS35L36_INT_DRV_SEL_MASK,
1234 				   cs35l36->pdata.irq_drv_sel <<
1235 				   CS35L36_INT_DRV_SEL_SHIFT);
1236 
1237 	if (cs35l36->pdata.irq_gpio_sel)
1238 		regmap_update_bits(cs35l36->regmap, CS35L36_PAD_INTERFACE,
1239 				   CS35L36_INT_GPIO_SEL_MASK,
1240 				   cs35l36->pdata.irq_gpio_sel <<
1241 				   CS35L36_INT_GPIO_SEL_SHIFT);
1242 
1243 	/*
1244 	 * Rev B0 has 2 versions
1245 	 * L36 is 10V
1246 	 * L37 is 12V
1247 	 * If L36 we need to clamp some values for safety
1248 	 * after probe has setup dt values. We want to make
1249 	 * sure we dont miss any values set in probe
1250 	 */
1251 	if (cs35l36->chip_version == CS35L36_10V_L36) {
1252 		regmap_update_bits(cs35l36->regmap,
1253 				   CS35L36_BSTCVRT_OVERVOLT_CTRL,
1254 				   CS35L36_BST_OVP_THLD_MASK,
1255 				   CS35L36_BST_OVP_THLD_11V);
1256 
1257 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1258 			     CS35L36_TEST_UNLOCK1);
1259 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1260 			     CS35L36_TEST_UNLOCK2);
1261 
1262 		regmap_update_bits(cs35l36->regmap, CS35L36_BST_ANA2_TEST,
1263 				   CS35L36_BST_OVP_TRIM_MASK,
1264 				   CS35L36_BST_OVP_TRIM_11V <<
1265 				   CS35L36_BST_OVP_TRIM_SHIFT);
1266 		regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_VCTRL2,
1267 				   CS35L36_BST_CTRL_LIM_MASK,
1268 				   1 << CS35L36_BST_CTRL_LIM_SHIFT);
1269 		regmap_update_bits(cs35l36->regmap, CS35L36_BSTCVRT_VCTRL1,
1270 				   CS35L35_BSTCVRT_CTL_MASK,
1271 				   CS35L36_BST_CTRL_10V_CLAMP);
1272 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1273 			     CS35L36_TEST_LOCK1);
1274 		regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1275 			     CS35L36_TEST_LOCK2);
1276 	}
1277 
1278 	/*
1279 	 * RevA and B require the disabling of
1280 	 * SYNC_GLOBAL_OVR when GLOBAL_EN = 0.
1281 	 * Just turn it off from default
1282 	 */
1283 	regmap_update_bits(cs35l36->regmap, CS35L36_CTRL_OVRRIDE,
1284 			   CS35L36_SYNC_GLOBAL_OVR_MASK,
1285 			   0 << CS35L36_SYNC_GLOBAL_OVR_SHIFT);
1286 
1287 	return 0;
1288 }
1289 
1290 static const struct snd_soc_component_driver soc_component_dev_cs35l36 = {
1291 	.probe			= &cs35l36_component_probe,
1292 	.set_sysclk		= cs35l36_component_set_sysclk,
1293 	.dapm_widgets		= cs35l36_dapm_widgets,
1294 	.num_dapm_widgets	= ARRAY_SIZE(cs35l36_dapm_widgets),
1295 	.dapm_routes		= cs35l36_audio_map,
1296 	.num_dapm_routes	= ARRAY_SIZE(cs35l36_audio_map),
1297 	.controls		= cs35l36_aud_controls,
1298 	.num_controls		= ARRAY_SIZE(cs35l36_aud_controls),
1299 	.idle_bias_on		= 1,
1300 	.use_pmdown_time	= 1,
1301 	.endianness		= 1,
1302 };
1303 
1304 static struct regmap_config cs35l36_regmap = {
1305 	.reg_bits = 32,
1306 	.val_bits = 32,
1307 	.reg_stride = 4,
1308 	.max_register = CS35L36_PAC_PMEM_WORD1023,
1309 	.reg_defaults = cs35l36_reg,
1310 	.num_reg_defaults = ARRAY_SIZE(cs35l36_reg),
1311 	.precious_reg = cs35l36_precious_reg,
1312 	.volatile_reg = cs35l36_volatile_reg,
1313 	.readable_reg = cs35l36_readable_reg,
1314 	.cache_type = REGCACHE_MAPLE,
1315 };
1316 
1317 static irqreturn_t cs35l36_irq(int irq, void *data)
1318 {
1319 	struct cs35l36_private *cs35l36 = data;
1320 	unsigned int status[4];
1321 	unsigned int masks[4];
1322 	int ret = IRQ_NONE;
1323 
1324 	/* ack the irq by reading all status registers */
1325 	regmap_bulk_read(cs35l36->regmap, CS35L36_INT1_STATUS, status,
1326 			 ARRAY_SIZE(status));
1327 
1328 	regmap_bulk_read(cs35l36->regmap, CS35L36_INT1_MASK, masks,
1329 			 ARRAY_SIZE(masks));
1330 
1331 	/* Check to see if unmasked bits are active */
1332 	if (!(status[0] & ~masks[0]) && !(status[1] & ~masks[1]) &&
1333 		!(status[2] & ~masks[2]) && !(status[3] & ~masks[3])) {
1334 		return IRQ_NONE;
1335 	}
1336 
1337 	/*
1338 	 * The following interrupts require a
1339 	 * protection release cycle to get the
1340 	 * speaker out of Safe-Mode.
1341 	 */
1342 	if (status[2] & CS35L36_AMP_SHORT_ERR) {
1343 		dev_crit(cs35l36->dev, "Amp short error\n");
1344 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1345 				   CS35L36_AMP_SHORT_ERR_RLS, 0);
1346 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1347 				   CS35L36_AMP_SHORT_ERR_RLS,
1348 				   CS35L36_AMP_SHORT_ERR_RLS);
1349 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1350 				   CS35L36_AMP_SHORT_ERR_RLS, 0);
1351 		regmap_update_bits(cs35l36->regmap, CS35L36_INT3_STATUS,
1352 				   CS35L36_AMP_SHORT_ERR,
1353 				   CS35L36_AMP_SHORT_ERR);
1354 		ret = IRQ_HANDLED;
1355 	}
1356 
1357 	if (status[0] & CS35L36_TEMP_WARN) {
1358 		dev_crit(cs35l36->dev, "Over temperature warning\n");
1359 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1360 				   CS35L36_TEMP_WARN_ERR_RLS, 0);
1361 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1362 				   CS35L36_TEMP_WARN_ERR_RLS,
1363 				   CS35L36_TEMP_WARN_ERR_RLS);
1364 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1365 				   CS35L36_TEMP_WARN_ERR_RLS, 0);
1366 		regmap_update_bits(cs35l36->regmap, CS35L36_INT1_STATUS,
1367 				   CS35L36_TEMP_WARN, CS35L36_TEMP_WARN);
1368 		ret = IRQ_HANDLED;
1369 	}
1370 
1371 	if (status[0] & CS35L36_TEMP_ERR) {
1372 		dev_crit(cs35l36->dev, "Over temperature error\n");
1373 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1374 				   CS35L36_TEMP_ERR_RLS, 0);
1375 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1376 				   CS35L36_TEMP_ERR_RLS, CS35L36_TEMP_ERR_RLS);
1377 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1378 				   CS35L36_TEMP_ERR_RLS, 0);
1379 		regmap_update_bits(cs35l36->regmap, CS35L36_INT1_STATUS,
1380 				   CS35L36_TEMP_ERR, CS35L36_TEMP_ERR);
1381 		ret = IRQ_HANDLED;
1382 	}
1383 
1384 	if (status[0] & CS35L36_BST_OVP_ERR) {
1385 		dev_crit(cs35l36->dev, "VBST Over Voltage error\n");
1386 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1387 				   CS35L36_TEMP_ERR_RLS, 0);
1388 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1389 				   CS35L36_TEMP_ERR_RLS, CS35L36_TEMP_ERR_RLS);
1390 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1391 				   CS35L36_TEMP_ERR_RLS, 0);
1392 		regmap_update_bits(cs35l36->regmap, CS35L36_INT1_STATUS,
1393 				   CS35L36_BST_OVP_ERR, CS35L36_BST_OVP_ERR);
1394 		ret = IRQ_HANDLED;
1395 	}
1396 
1397 	if (status[0] & CS35L36_BST_DCM_UVP_ERR) {
1398 		dev_crit(cs35l36->dev, "DCM VBST Under Voltage Error\n");
1399 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1400 				   CS35L36_BST_UVP_ERR_RLS, 0);
1401 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1402 				   CS35L36_BST_UVP_ERR_RLS,
1403 				   CS35L36_BST_UVP_ERR_RLS);
1404 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1405 				   CS35L36_BST_UVP_ERR_RLS, 0);
1406 		regmap_update_bits(cs35l36->regmap, CS35L36_INT1_STATUS,
1407 				   CS35L36_BST_DCM_UVP_ERR,
1408 				   CS35L36_BST_DCM_UVP_ERR);
1409 		ret = IRQ_HANDLED;
1410 	}
1411 
1412 	if (status[0] & CS35L36_BST_SHORT_ERR) {
1413 		dev_crit(cs35l36->dev, "LBST SHORT error!\n");
1414 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1415 				   CS35L36_BST_SHORT_ERR_RLS, 0);
1416 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1417 				   CS35L36_BST_SHORT_ERR_RLS,
1418 				   CS35L36_BST_SHORT_ERR_RLS);
1419 		regmap_update_bits(cs35l36->regmap, CS35L36_PROTECT_REL_ERR,
1420 				   CS35L36_BST_SHORT_ERR_RLS, 0);
1421 		regmap_update_bits(cs35l36->regmap, CS35L36_INT1_STATUS,
1422 				   CS35L36_BST_SHORT_ERR,
1423 				   CS35L36_BST_SHORT_ERR);
1424 		ret = IRQ_HANDLED;
1425 	}
1426 
1427 	return ret;
1428 }
1429 
1430 static int cs35l36_handle_of_data(struct i2c_client *i2c_client,
1431 				struct cs35l36_platform_data *pdata)
1432 {
1433 	struct device_node *np = i2c_client->dev.of_node;
1434 	struct cs35l36_vpbr_cfg *vpbr_config = &pdata->vpbr_config;
1435 	struct device_node *vpbr_node;
1436 	unsigned int val;
1437 	int ret;
1438 
1439 	if (!np)
1440 		return 0;
1441 
1442 	ret = of_property_read_u32(np, "cirrus,boost-ctl-millivolt", &val);
1443 	if (!ret) {
1444 		if (val < 2550 || val > 12000) {
1445 			dev_err(&i2c_client->dev,
1446 				"Invalid Boost Voltage %d mV\n", val);
1447 			return -EINVAL;
1448 		}
1449 		pdata->bst_vctl = (((val - 2550) / 100) + 1) << 1;
1450 	} else {
1451 		dev_err(&i2c_client->dev,
1452 			"Unable to find required parameter 'cirrus,boost-ctl-millivolt'");
1453 		return -EINVAL;
1454 	}
1455 
1456 	ret = of_property_read_u32(np, "cirrus,boost-ctl-select", &val);
1457 	if (!ret)
1458 		pdata->bst_vctl_sel = val | CS35L36_VALID_PDATA;
1459 
1460 	ret = of_property_read_u32(np, "cirrus,boost-peak-milliamp", &val);
1461 	if (!ret) {
1462 		if (val < 1600 || val > 4500) {
1463 			dev_err(&i2c_client->dev,
1464 				"Invalid Boost Peak Current %u mA\n", val);
1465 			return -EINVAL;
1466 		}
1467 
1468 		pdata->bst_ipk = (val - 1600) / 50;
1469 	} else {
1470 		dev_err(&i2c_client->dev,
1471 			"Unable to find required parameter 'cirrus,boost-peak-milliamp'");
1472 		return -EINVAL;
1473 	}
1474 
1475 	pdata->multi_amp_mode = of_property_read_bool(np,
1476 					"cirrus,multi-amp-mode");
1477 
1478 	pdata->dcm_mode = of_property_read_bool(np,
1479 					"cirrus,dcm-mode-enable");
1480 
1481 	pdata->amp_pcm_inv = of_property_read_bool(np,
1482 					"cirrus,amp-pcm-inv");
1483 
1484 	pdata->imon_pol_inv = of_property_read_bool(np,
1485 					"cirrus,imon-pol-inv");
1486 
1487 	pdata->vmon_pol_inv = of_property_read_bool(np,
1488 					"cirrus,vmon-pol-inv");
1489 
1490 	if (of_property_read_u32(np, "cirrus,temp-warn-threshold", &val) >= 0)
1491 		pdata->temp_warn_thld = val | CS35L36_VALID_PDATA;
1492 
1493 	if (of_property_read_u32(np, "cirrus,boost-ind-nanohenry", &val) >= 0) {
1494 		pdata->boost_ind = val;
1495 	} else {
1496 		dev_err(&i2c_client->dev, "Inductor not specified.\n");
1497 		return -EINVAL;
1498 	}
1499 
1500 	if (of_property_read_u32(np, "cirrus,irq-drive-select", &val) >= 0)
1501 		pdata->irq_drv_sel = val | CS35L36_VALID_PDATA;
1502 
1503 	if (of_property_read_u32(np, "cirrus,irq-gpio-select", &val) >= 0)
1504 		pdata->irq_gpio_sel = val | CS35L36_VALID_PDATA;
1505 
1506 	/* VPBR Config */
1507 	vpbr_node = of_get_child_by_name(np, "cirrus,vpbr-config");
1508 	vpbr_config->is_present = vpbr_node ? true : false;
1509 	if (vpbr_config->is_present) {
1510 		if (of_property_read_u32(vpbr_node, "cirrus,vpbr-en",
1511 					 &val) >= 0)
1512 			vpbr_config->vpbr_en = val;
1513 		if (of_property_read_u32(vpbr_node, "cirrus,vpbr-thld",
1514 					 &val) >= 0)
1515 			vpbr_config->vpbr_thld = val;
1516 		if (of_property_read_u32(vpbr_node, "cirrus,vpbr-atk-rate",
1517 					 &val) >= 0)
1518 			vpbr_config->vpbr_atk_rate = val;
1519 		if (of_property_read_u32(vpbr_node, "cirrus,vpbr-atk-vol",
1520 					 &val) >= 0)
1521 			vpbr_config->vpbr_atk_vol = val;
1522 		if (of_property_read_u32(vpbr_node, "cirrus,vpbr-max-attn",
1523 					 &val) >= 0)
1524 			vpbr_config->vpbr_max_attn = val;
1525 		if (of_property_read_u32(vpbr_node, "cirrus,vpbr-wait",
1526 					 &val) >= 0)
1527 			vpbr_config->vpbr_wait = val;
1528 		if (of_property_read_u32(vpbr_node, "cirrus,vpbr-rel-rate",
1529 					 &val) >= 0)
1530 			vpbr_config->vpbr_rel_rate = val;
1531 		if (of_property_read_u32(vpbr_node, "cirrus,vpbr-mute-en",
1532 					 &val) >= 0)
1533 			vpbr_config->vpbr_mute_en = val;
1534 	}
1535 	of_node_put(vpbr_node);
1536 
1537 	return 0;
1538 }
1539 
1540 static int cs35l36_pac(struct cs35l36_private *cs35l36)
1541 {
1542 	int ret, count;
1543 	unsigned int val;
1544 
1545 	if (cs35l36->rev_id != CS35L36_REV_B0)
1546 		return 0;
1547 
1548 	/*
1549 	 * Magic code for internal PAC
1550 	 */
1551 	regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1552 		     CS35L36_TEST_UNLOCK1);
1553 	regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1554 		     CS35L36_TEST_UNLOCK2);
1555 
1556 	usleep_range(9500, 10500);
1557 
1558 	regmap_write(cs35l36->regmap, CS35L36_PAC_CTL1,
1559 		     CS35L36_PAC_RESET);
1560 	regmap_write(cs35l36->regmap, CS35L36_PAC_CTL3,
1561 		     CS35L36_PAC_MEM_ACCESS);
1562 	regmap_write(cs35l36->regmap, CS35L36_PAC_PMEM_WORD0,
1563 		     CS35L36_B0_PAC_PATCH);
1564 
1565 	regmap_write(cs35l36->regmap, CS35L36_PAC_CTL3,
1566 		     CS35L36_PAC_MEM_ACCESS_CLR);
1567 	regmap_write(cs35l36->regmap, CS35L36_PAC_CTL1,
1568 		     CS35L36_PAC_ENABLE_MASK);
1569 
1570 	usleep_range(9500, 10500);
1571 
1572 	ret = regmap_read(cs35l36->regmap, CS35L36_INT4_STATUS, &val);
1573 	if (ret < 0) {
1574 		dev_err(cs35l36->dev, "Failed to read int4_status %d\n", ret);
1575 		return ret;
1576 	}
1577 
1578 	count = 0;
1579 	while (!(val & CS35L36_MCU_CONFIG_CLR)) {
1580 		usleep_range(100, 200);
1581 		count++;
1582 
1583 		ret = regmap_read(cs35l36->regmap, CS35L36_INT4_STATUS,
1584 				  &val);
1585 		if (ret < 0) {
1586 			dev_err(cs35l36->dev, "Failed to read int4_status %d\n",
1587 				ret);
1588 			return ret;
1589 		}
1590 
1591 		if (count >= 100)
1592 			return -EINVAL;
1593 	}
1594 
1595 	regmap_write(cs35l36->regmap, CS35L36_INT4_STATUS,
1596 		     CS35L36_MCU_CONFIG_CLR);
1597 	regmap_update_bits(cs35l36->regmap, CS35L36_PAC_CTL1,
1598 			   CS35L36_PAC_ENABLE_MASK, 0);
1599 
1600 	regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1601 		     CS35L36_TEST_LOCK1);
1602 	regmap_write(cs35l36->regmap, CS35L36_TESTKEY_CTRL,
1603 		     CS35L36_TEST_LOCK2);
1604 
1605 	return 0;
1606 }
1607 
1608 static void cs35l36_apply_vpbr_config(struct cs35l36_private *cs35l36)
1609 {
1610 	struct cs35l36_platform_data *pdata = &cs35l36->pdata;
1611 	struct cs35l36_vpbr_cfg *vpbr_config = &pdata->vpbr_config;
1612 
1613 	regmap_update_bits(cs35l36->regmap, CS35L36_PWR_CTRL3,
1614 			   CS35L36_VPBR_EN_MASK,
1615 			   vpbr_config->vpbr_en <<
1616 			   CS35L36_VPBR_EN_SHIFT);
1617 	regmap_update_bits(cs35l36->regmap, CS35L36_VPBR_CFG,
1618 			   CS35L36_VPBR_THLD_MASK,
1619 			   vpbr_config->vpbr_thld <<
1620 			   CS35L36_VPBR_THLD_SHIFT);
1621 	regmap_update_bits(cs35l36->regmap, CS35L36_VPBR_CFG,
1622 			   CS35L36_VPBR_MAX_ATTN_MASK,
1623 			   vpbr_config->vpbr_max_attn <<
1624 			   CS35L36_VPBR_MAX_ATTN_SHIFT);
1625 	regmap_update_bits(cs35l36->regmap, CS35L36_VPBR_CFG,
1626 			   CS35L36_VPBR_ATK_VOL_MASK,
1627 			   vpbr_config->vpbr_atk_vol <<
1628 			   CS35L36_VPBR_ATK_VOL_SHIFT);
1629 	regmap_update_bits(cs35l36->regmap, CS35L36_VPBR_CFG,
1630 			   CS35L36_VPBR_ATK_RATE_MASK,
1631 			   vpbr_config->vpbr_atk_rate <<
1632 			   CS35L36_VPBR_ATK_RATE_SHIFT);
1633 	regmap_update_bits(cs35l36->regmap, CS35L36_VPBR_CFG,
1634 			   CS35L36_VPBR_WAIT_MASK,
1635 			   vpbr_config->vpbr_wait <<
1636 			   CS35L36_VPBR_WAIT_SHIFT);
1637 	regmap_update_bits(cs35l36->regmap, CS35L36_VPBR_CFG,
1638 			   CS35L36_VPBR_REL_RATE_MASK,
1639 			   vpbr_config->vpbr_rel_rate <<
1640 			   CS35L36_VPBR_REL_RATE_SHIFT);
1641 	regmap_update_bits(cs35l36->regmap, CS35L36_VPBR_CFG,
1642 			   CS35L36_VPBR_MUTE_EN_MASK,
1643 			   vpbr_config->vpbr_mute_en <<
1644 			   CS35L36_VPBR_MUTE_EN_SHIFT);
1645 }
1646 
1647 static const struct reg_sequence cs35l36_reva0_errata_patch[] = {
1648 	{ CS35L36_TESTKEY_CTRL,		CS35L36_TEST_UNLOCK1 },
1649 	{ CS35L36_TESTKEY_CTRL,		CS35L36_TEST_UNLOCK2 },
1650 	/* Errata Writes */
1651 	{ CS35L36_OTP_CTRL1,		0x00002060 },
1652 	{ CS35L36_OTP_CTRL2,		0x00000001 },
1653 	{ CS35L36_OTP_CTRL1,		0x00002460 },
1654 	{ CS35L36_OTP_CTRL2,		0x00000001 },
1655 	{ 0x00002088,			0x012A1838 },
1656 	{ 0x00003014,			0x0100EE0E },
1657 	{ 0x00003008,			0x0008184A },
1658 	{ 0x00007418,			0x509001C8 },
1659 	{ 0x00007064,			0x0929A800 },
1660 	{ 0x00002D10,			0x0002C01C },
1661 	{ 0x0000410C,			0x00000A11 },
1662 	{ 0x00006E08,			0x8B19140C },
1663 	{ 0x00006454,			0x0300000A },
1664 	{ CS35L36_AMP_NG_CTRL,		0x000020EF },
1665 	{ 0x00007E34,			0x0000000E },
1666 	{ 0x0000410C,			0x00000A11 },
1667 	{ 0x00007410,			0x20514B00 },
1668 	/* PAC Config */
1669 	{ CS35L36_CTRL_OVRRIDE,		0x00000000 },
1670 	{ CS35L36_PAC_INT0_CTRL,	0x00860001 },
1671 	{ CS35L36_PAC_INT1_CTRL,	0x00860001 },
1672 	{ CS35L36_PAC_INT2_CTRL,	0x00860001 },
1673 	{ CS35L36_PAC_INT3_CTRL,	0x00860001 },
1674 	{ CS35L36_PAC_INT4_CTRL,	0x00860001 },
1675 	{ CS35L36_PAC_INT5_CTRL,	0x00860001 },
1676 	{ CS35L36_PAC_INT6_CTRL,	0x00860001 },
1677 	{ CS35L36_PAC_INT7_CTRL,	0x00860001 },
1678 	{ CS35L36_PAC_INT_FLUSH_CTRL,	0x000000FF },
1679 	{ CS35L36_TESTKEY_CTRL,		CS35L36_TEST_LOCK1 },
1680 	{ CS35L36_TESTKEY_CTRL,		CS35L36_TEST_LOCK2 },
1681 };
1682 
1683 static const struct reg_sequence cs35l36_revb0_errata_patch[] = {
1684 	{ CS35L36_TESTKEY_CTRL,	CS35L36_TEST_UNLOCK1 },
1685 	{ CS35L36_TESTKEY_CTRL, CS35L36_TEST_UNLOCK2 },
1686 	{ 0x00007064,		0x0929A800 },
1687 	{ 0x00007850,		0x00002FA9 },
1688 	{ 0x00007854,		0x0003F1D5 },
1689 	{ 0x00007858,		0x0003F5E3 },
1690 	{ 0x0000785C,		0x00001137 },
1691 	{ 0x00007860,		0x0001A7A5 },
1692 	{ 0x00007864,		0x0002F16A },
1693 	{ 0x00007868,		0x00003E21 },
1694 	{ 0x00007848,		0x00000001 },
1695 	{ 0x00003854,		0x05180240 },
1696 	{ 0x00007418,		0x509001C8 },
1697 	{ 0x0000394C,		0x028764BD },
1698 	{ CS35L36_TESTKEY_CTRL,	CS35L36_TEST_LOCK1 },
1699 	{ CS35L36_TESTKEY_CTRL, CS35L36_TEST_LOCK2 },
1700 };
1701 
1702 static int cs35l36_i2c_probe(struct i2c_client *i2c_client)
1703 {
1704 	struct cs35l36_private *cs35l36;
1705 	struct device *dev = &i2c_client->dev;
1706 	struct cs35l36_platform_data *pdata = dev_get_platdata(dev);
1707 	struct irq_data *irq_d;
1708 	int ret, irq_pol, chip_irq_pol, i;
1709 	u32 reg_id, reg_revid, l37_id_reg;
1710 
1711 	cs35l36 = devm_kzalloc(dev, sizeof(struct cs35l36_private), GFP_KERNEL);
1712 	if (!cs35l36)
1713 		return -ENOMEM;
1714 
1715 	cs35l36->dev = dev;
1716 
1717 	i2c_set_clientdata(i2c_client, cs35l36);
1718 	cs35l36->regmap = devm_regmap_init_i2c(i2c_client, &cs35l36_regmap);
1719 	if (IS_ERR(cs35l36->regmap)) {
1720 		ret = PTR_ERR(cs35l36->regmap);
1721 		dev_err(dev, "regmap_init() failed: %d\n", ret);
1722 		return ret;
1723 	}
1724 
1725 	cs35l36->num_supplies = ARRAY_SIZE(cs35l36_supplies);
1726 	for (i = 0; i < ARRAY_SIZE(cs35l36_supplies); i++)
1727 		cs35l36->supplies[i].supply = cs35l36_supplies[i];
1728 
1729 	ret = devm_regulator_bulk_get(dev, cs35l36->num_supplies,
1730 				      cs35l36->supplies);
1731 	if (ret != 0) {
1732 		dev_err(dev, "Failed to request core supplies: %d\n", ret);
1733 		return ret;
1734 	}
1735 
1736 	if (pdata) {
1737 		cs35l36->pdata = *pdata;
1738 	} else {
1739 		pdata = devm_kzalloc(dev, sizeof(struct cs35l36_platform_data),
1740 				     GFP_KERNEL);
1741 		if (!pdata)
1742 			return -ENOMEM;
1743 
1744 		if (i2c_client->dev.of_node) {
1745 			ret = cs35l36_handle_of_data(i2c_client, pdata);
1746 			if (ret != 0)
1747 				return ret;
1748 
1749 		}
1750 
1751 		cs35l36->pdata = *pdata;
1752 	}
1753 
1754 	ret = regulator_bulk_enable(cs35l36->num_supplies, cs35l36->supplies);
1755 	if (ret != 0) {
1756 		dev_err(dev, "Failed to enable core supplies: %d\n", ret);
1757 		return ret;
1758 	}
1759 
1760 	/* returning NULL can be an option if in stereo mode */
1761 	cs35l36->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1762 						      GPIOD_OUT_LOW);
1763 	if (IS_ERR(cs35l36->reset_gpio)) {
1764 		ret = PTR_ERR(cs35l36->reset_gpio);
1765 		cs35l36->reset_gpio = NULL;
1766 		if (ret == -EBUSY) {
1767 			dev_info(dev, "Reset line busy, assuming shared reset\n");
1768 		} else {
1769 			dev_err(dev, "Failed to get reset GPIO: %d\n", ret);
1770 			goto err_disable_regs;
1771 		}
1772 	}
1773 
1774 	if (cs35l36->reset_gpio)
1775 		gpiod_set_value_cansleep(cs35l36->reset_gpio, 1);
1776 
1777 	usleep_range(2000, 2100);
1778 
1779 	/* initialize amplifier */
1780 	ret = regmap_read(cs35l36->regmap, CS35L36_SW_RESET, &reg_id);
1781 	if (ret < 0) {
1782 		dev_err(dev, "Get Device ID failed %d\n", ret);
1783 		goto err;
1784 	}
1785 
1786 	if (reg_id != CS35L36_CHIP_ID) {
1787 		dev_err(dev, "Device ID (%X). Expected ID %X\n", reg_id,
1788 			CS35L36_CHIP_ID);
1789 		ret = -ENODEV;
1790 		goto err;
1791 	}
1792 
1793 	ret = regmap_read(cs35l36->regmap, CS35L36_REV_ID, &reg_revid);
1794 	if (ret < 0) {
1795 		dev_err(&i2c_client->dev, "Get Revision ID failed %d\n", ret);
1796 		goto err;
1797 	}
1798 
1799 	cs35l36->rev_id = reg_revid >> 8;
1800 
1801 	ret = regmap_read(cs35l36->regmap, CS35L36_OTP_MEM30, &l37_id_reg);
1802 	if (ret < 0) {
1803 		dev_err(&i2c_client->dev, "Failed to read otp_id Register %d\n",
1804 			ret);
1805 		goto err;
1806 	}
1807 
1808 	if ((l37_id_reg & CS35L36_OTP_REV_MASK) == CS35L36_OTP_REV_L37)
1809 		cs35l36->chip_version = CS35L36_12V_L37;
1810 	else
1811 		cs35l36->chip_version = CS35L36_10V_L36;
1812 
1813 	switch (cs35l36->rev_id) {
1814 	case CS35L36_REV_A0:
1815 		ret = regmap_register_patch(cs35l36->regmap,
1816 				cs35l36_reva0_errata_patch,
1817 				ARRAY_SIZE(cs35l36_reva0_errata_patch));
1818 		if (ret < 0) {
1819 			dev_err(dev, "Failed to apply A0 errata patch %d\n",
1820 				ret);
1821 			goto err;
1822 		}
1823 		break;
1824 	case CS35L36_REV_B0:
1825 		ret = cs35l36_pac(cs35l36);
1826 		if (ret < 0) {
1827 			dev_err(dev, "Failed to Trim OTP %d\n", ret);
1828 			goto err;
1829 		}
1830 
1831 		ret = regmap_register_patch(cs35l36->regmap,
1832 				cs35l36_revb0_errata_patch,
1833 				ARRAY_SIZE(cs35l36_revb0_errata_patch));
1834 		if (ret < 0) {
1835 			dev_err(dev, "Failed to apply B0 errata patch %d\n",
1836 				ret);
1837 			goto err;
1838 		}
1839 		break;
1840 	}
1841 
1842 	if (pdata->vpbr_config.is_present)
1843 		cs35l36_apply_vpbr_config(cs35l36);
1844 
1845 	irq_d = irq_get_irq_data(i2c_client->irq);
1846 	if (!irq_d) {
1847 		dev_err(&i2c_client->dev, "Invalid IRQ: %d\n", i2c_client->irq);
1848 		ret = -ENODEV;
1849 		goto err;
1850 	}
1851 
1852 	irq_pol = irqd_get_trigger_type(irq_d);
1853 
1854 	switch (irq_pol) {
1855 	case IRQF_TRIGGER_FALLING:
1856 	case IRQF_TRIGGER_LOW:
1857 		chip_irq_pol = 0;
1858 		break;
1859 	case IRQF_TRIGGER_RISING:
1860 	case IRQF_TRIGGER_HIGH:
1861 		chip_irq_pol = 1;
1862 		break;
1863 	default:
1864 		dev_err(cs35l36->dev, "Invalid IRQ polarity: %d\n", irq_pol);
1865 		ret = -EINVAL;
1866 		goto err;
1867 	}
1868 
1869 	regmap_update_bits(cs35l36->regmap, CS35L36_PAD_INTERFACE,
1870 			   CS35L36_INT_POL_SEL_MASK,
1871 			   chip_irq_pol << CS35L36_INT_POL_SEL_SHIFT);
1872 
1873 	ret = devm_request_threaded_irq(dev, i2c_client->irq, NULL, cs35l36_irq,
1874 					IRQF_ONESHOT | irq_pol, "cs35l36",
1875 					cs35l36);
1876 	if (ret != 0) {
1877 		dev_err(dev, "Failed to request IRQ: %d\n", ret);
1878 		goto err;
1879 	}
1880 
1881 	regmap_update_bits(cs35l36->regmap, CS35L36_PAD_INTERFACE,
1882 			   CS35L36_INT_OUTPUT_EN_MASK, 1);
1883 
1884 	/* Set interrupt masks for critical errors */
1885 	regmap_write(cs35l36->regmap, CS35L36_INT1_MASK,
1886 		     CS35L36_INT1_MASK_DEFAULT);
1887 	regmap_write(cs35l36->regmap, CS35L36_INT3_MASK,
1888 		     CS35L36_INT3_MASK_DEFAULT);
1889 
1890 	dev_info(&i2c_client->dev, "Cirrus Logic CS35L%d, Revision: %02X\n",
1891 		 cs35l36->chip_version, reg_revid >> 8);
1892 
1893 	ret =  devm_snd_soc_register_component(dev, &soc_component_dev_cs35l36,
1894 					       cs35l36_dai,
1895 					       ARRAY_SIZE(cs35l36_dai));
1896 	if (ret < 0) {
1897 		dev_err(dev, "%s: Register component failed %d\n", __func__,
1898 			ret);
1899 		goto err;
1900 	}
1901 
1902 	return 0;
1903 
1904 err:
1905 	gpiod_set_value_cansleep(cs35l36->reset_gpio, 0);
1906 
1907 err_disable_regs:
1908 	regulator_bulk_disable(cs35l36->num_supplies, cs35l36->supplies);
1909 	return ret;
1910 }
1911 
1912 static void cs35l36_i2c_remove(struct i2c_client *client)
1913 {
1914 	struct cs35l36_private *cs35l36 = i2c_get_clientdata(client);
1915 
1916 	/* Reset interrupt masks for device removal */
1917 	regmap_write(cs35l36->regmap, CS35L36_INT1_MASK,
1918 		     CS35L36_INT1_MASK_RESET);
1919 	regmap_write(cs35l36->regmap, CS35L36_INT3_MASK,
1920 		     CS35L36_INT3_MASK_RESET);
1921 
1922 	if (cs35l36->reset_gpio)
1923 		gpiod_set_value_cansleep(cs35l36->reset_gpio, 0);
1924 
1925 	regulator_bulk_disable(cs35l36->num_supplies, cs35l36->supplies);
1926 }
1927 static const struct of_device_id cs35l36_of_match[] = {
1928 	{.compatible = "cirrus,cs35l36"},
1929 	{},
1930 };
1931 MODULE_DEVICE_TABLE(of, cs35l36_of_match);
1932 
1933 static const struct i2c_device_id cs35l36_id[] = {
1934 	{"cs35l36", 0},
1935 	{}
1936 };
1937 
1938 MODULE_DEVICE_TABLE(i2c, cs35l36_id);
1939 
1940 static struct i2c_driver cs35l36_i2c_driver = {
1941 	.driver = {
1942 		.name = "cs35l36",
1943 		.of_match_table = cs35l36_of_match,
1944 	},
1945 	.id_table = cs35l36_id,
1946 	.probe = cs35l36_i2c_probe,
1947 	.remove = cs35l36_i2c_remove,
1948 };
1949 module_i2c_driver(cs35l36_i2c_driver);
1950 
1951 MODULE_DESCRIPTION("ASoC CS35L36 driver");
1952 MODULE_AUTHOR("James Schulman, Cirrus Logic Inc, <james.schulman@cirrus.com>");
1953 MODULE_LICENSE("GPL");
1954