xref: /openbmc/linux/sound/soc/codecs/max98373.c (revision f220d3eb)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017, Maxim Integrated
3 
4 #include <linux/acpi.h>
5 #include <linux/i2c.h>
6 #include <linux/module.h>
7 #include <linux/regmap.h>
8 #include <linux/slab.h>
9 #include <linux/cdev.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc.h>
13 #include <linux/gpio.h>
14 #include <linux/of_gpio.h>
15 #include <sound/tlv.h>
16 #include "max98373.h"
17 
18 static struct reg_default max98373_reg[] = {
19 	{MAX98373_R2000_SW_RESET, 0x00},
20 	{MAX98373_R2001_INT_RAW1, 0x00},
21 	{MAX98373_R2002_INT_RAW2, 0x00},
22 	{MAX98373_R2003_INT_RAW3, 0x00},
23 	{MAX98373_R2004_INT_STATE1, 0x00},
24 	{MAX98373_R2005_INT_STATE2, 0x00},
25 	{MAX98373_R2006_INT_STATE3, 0x00},
26 	{MAX98373_R2007_INT_FLAG1, 0x00},
27 	{MAX98373_R2008_INT_FLAG2, 0x00},
28 	{MAX98373_R2009_INT_FLAG3, 0x00},
29 	{MAX98373_R200A_INT_EN1, 0x00},
30 	{MAX98373_R200B_INT_EN2, 0x00},
31 	{MAX98373_R200C_INT_EN3, 0x00},
32 	{MAX98373_R200D_INT_FLAG_CLR1, 0x00},
33 	{MAX98373_R200E_INT_FLAG_CLR2, 0x00},
34 	{MAX98373_R200F_INT_FLAG_CLR3, 0x00},
35 	{MAX98373_R2010_IRQ_CTRL, 0x00},
36 	{MAX98373_R2014_THERM_WARN_THRESH, 0x10},
37 	{MAX98373_R2015_THERM_SHDN_THRESH, 0x27},
38 	{MAX98373_R2016_THERM_HYSTERESIS, 0x01},
39 	{MAX98373_R2017_THERM_FOLDBACK_SET, 0xC0},
40 	{MAX98373_R2018_THERM_FOLDBACK_EN, 0x00},
41 	{MAX98373_R201E_PIN_DRIVE_STRENGTH, 0x55},
42 	{MAX98373_R2020_PCM_TX_HIZ_EN_1, 0xFE},
43 	{MAX98373_R2021_PCM_TX_HIZ_EN_2, 0xFF},
44 	{MAX98373_R2022_PCM_TX_SRC_1, 0x00},
45 	{MAX98373_R2023_PCM_TX_SRC_2, 0x00},
46 	{MAX98373_R2024_PCM_DATA_FMT_CFG, 0xC0},
47 	{MAX98373_R2025_AUDIO_IF_MODE, 0x00},
48 	{MAX98373_R2026_PCM_CLOCK_RATIO, 0x04},
49 	{MAX98373_R2027_PCM_SR_SETUP_1, 0x08},
50 	{MAX98373_R2028_PCM_SR_SETUP_2, 0x88},
51 	{MAX98373_R2029_PCM_TO_SPK_MONO_MIX_1, 0x00},
52 	{MAX98373_R202A_PCM_TO_SPK_MONO_MIX_2, 0x00},
53 	{MAX98373_R202B_PCM_RX_EN, 0x00},
54 	{MAX98373_R202C_PCM_TX_EN, 0x00},
55 	{MAX98373_R202E_ICC_RX_CH_EN_1, 0x00},
56 	{MAX98373_R202F_ICC_RX_CH_EN_2, 0x00},
57 	{MAX98373_R2030_ICC_TX_HIZ_EN_1, 0xFF},
58 	{MAX98373_R2031_ICC_TX_HIZ_EN_2, 0xFF},
59 	{MAX98373_R2032_ICC_LINK_EN_CFG, 0x30},
60 	{MAX98373_R2034_ICC_TX_CNTL, 0x00},
61 	{MAX98373_R2035_ICC_TX_EN, 0x00},
62 	{MAX98373_R2036_SOUNDWIRE_CTRL, 0x05},
63 	{MAX98373_R203D_AMP_DIG_VOL_CTRL, 0x00},
64 	{MAX98373_R203E_AMP_PATH_GAIN, 0x08},
65 	{MAX98373_R203F_AMP_DSP_CFG, 0x02},
66 	{MAX98373_R2040_TONE_GEN_CFG, 0x00},
67 	{MAX98373_R2041_AMP_CFG, 0x03},
68 	{MAX98373_R2042_AMP_EDGE_RATE_CFG, 0x00},
69 	{MAX98373_R2043_AMP_EN, 0x00},
70 	{MAX98373_R2046_IV_SENSE_ADC_DSP_CFG, 0x04},
71 	{MAX98373_R2047_IV_SENSE_ADC_EN, 0x00},
72 	{MAX98373_R2051_MEAS_ADC_SAMPLING_RATE, 0x00},
73 	{MAX98373_R2052_MEAS_ADC_PVDD_FLT_CFG, 0x00},
74 	{MAX98373_R2053_MEAS_ADC_THERM_FLT_CFG, 0x00},
75 	{MAX98373_R2054_MEAS_ADC_PVDD_CH_READBACK, 0x00},
76 	{MAX98373_R2055_MEAS_ADC_THERM_CH_READBACK, 0x00},
77 	{MAX98373_R2056_MEAS_ADC_PVDD_CH_EN, 0x00},
78 	{MAX98373_R2090_BDE_LVL_HOLD, 0x00},
79 	{MAX98373_R2091_BDE_GAIN_ATK_REL_RATE, 0x00},
80 	{MAX98373_R2092_BDE_CLIPPER_MODE, 0x00},
81 	{MAX98373_R2097_BDE_L1_THRESH, 0x00},
82 	{MAX98373_R2098_BDE_L2_THRESH, 0x00},
83 	{MAX98373_R2099_BDE_L3_THRESH, 0x00},
84 	{MAX98373_R209A_BDE_L4_THRESH, 0x00},
85 	{MAX98373_R209B_BDE_THRESH_HYST, 0x00},
86 	{MAX98373_R20A8_BDE_L1_CFG_1, 0x00},
87 	{MAX98373_R20A9_BDE_L1_CFG_2, 0x00},
88 	{MAX98373_R20AA_BDE_L1_CFG_3, 0x00},
89 	{MAX98373_R20AB_BDE_L2_CFG_1, 0x00},
90 	{MAX98373_R20AC_BDE_L2_CFG_2, 0x00},
91 	{MAX98373_R20AD_BDE_L2_CFG_3, 0x00},
92 	{MAX98373_R20AE_BDE_L3_CFG_1, 0x00},
93 	{MAX98373_R20AF_BDE_L3_CFG_2, 0x00},
94 	{MAX98373_R20B0_BDE_L3_CFG_3, 0x00},
95 	{MAX98373_R20B1_BDE_L4_CFG_1, 0x00},
96 	{MAX98373_R20B2_BDE_L4_CFG_2, 0x00},
97 	{MAX98373_R20B3_BDE_L4_CFG_3, 0x00},
98 	{MAX98373_R20B4_BDE_INFINITE_HOLD_RELEASE, 0x00},
99 	{MAX98373_R20B5_BDE_EN, 0x00},
100 	{MAX98373_R20B6_BDE_CUR_STATE_READBACK, 0x00},
101 	{MAX98373_R20D1_DHT_CFG, 0x01},
102 	{MAX98373_R20D2_DHT_ATTACK_CFG, 0x02},
103 	{MAX98373_R20D3_DHT_RELEASE_CFG, 0x03},
104 	{MAX98373_R20D4_DHT_EN, 0x00},
105 	{MAX98373_R20E0_LIMITER_THRESH_CFG, 0x00},
106 	{MAX98373_R20E1_LIMITER_ATK_REL_RATES, 0x00},
107 	{MAX98373_R20E2_LIMITER_EN, 0x00},
108 	{MAX98373_R20FE_DEVICE_AUTO_RESTART_CFG, 0x00},
109 	{MAX98373_R20FF_GLOBAL_SHDN, 0x00},
110 	{MAX98373_R21FF_REV_ID, 0x42},
111 };
112 
113 static int max98373_dai_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
114 {
115 	struct snd_soc_component *component = codec_dai->component;
116 	struct max98373_priv *max98373 = snd_soc_component_get_drvdata(component);
117 	unsigned int format = 0;
118 	unsigned int invert = 0;
119 
120 	dev_dbg(component->dev, "%s: fmt 0x%08X\n", __func__, fmt);
121 
122 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
123 	case SND_SOC_DAIFMT_NB_NF:
124 		break;
125 	case SND_SOC_DAIFMT_IB_NF:
126 		invert = MAX98373_PCM_MODE_CFG_PCM_BCLKEDGE;
127 		break;
128 	default:
129 		dev_err(component->dev, "DAI invert mode unsupported\n");
130 		return -EINVAL;
131 	}
132 
133 	regmap_update_bits(max98373->regmap,
134 		MAX98373_R2026_PCM_CLOCK_RATIO,
135 		MAX98373_PCM_MODE_CFG_PCM_BCLKEDGE,
136 		invert);
137 
138 	/* interface format */
139 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
140 	case SND_SOC_DAIFMT_I2S:
141 		format = MAX98373_PCM_FORMAT_I2S;
142 		break;
143 	case SND_SOC_DAIFMT_LEFT_J:
144 		format = MAX98373_PCM_FORMAT_LJ;
145 		break;
146 	case SND_SOC_DAIFMT_DSP_A:
147 		format = MAX98373_PCM_FORMAT_TDM_MODE1;
148 		break;
149 	case SND_SOC_DAIFMT_DSP_B:
150 		format = MAX98373_PCM_FORMAT_TDM_MODE0;
151 		break;
152 	default:
153 		return -EINVAL;
154 	}
155 
156 	regmap_update_bits(max98373->regmap,
157 		MAX98373_R2024_PCM_DATA_FMT_CFG,
158 		MAX98373_PCM_MODE_CFG_FORMAT_MASK,
159 		format << MAX98373_PCM_MODE_CFG_FORMAT_SHIFT);
160 
161 	return 0;
162 }
163 
164 /* BCLKs per LRCLK */
165 static const int bclk_sel_table[] = {
166 	32, 48, 64, 96, 128, 192, 256, 384, 512, 320,
167 };
168 
169 static int max98373_get_bclk_sel(int bclk)
170 {
171 	int i;
172 	/* match BCLKs per LRCLK */
173 	for (i = 0; i < ARRAY_SIZE(bclk_sel_table); i++) {
174 		if (bclk_sel_table[i] == bclk)
175 			return i + 2;
176 	}
177 	return 0;
178 }
179 
180 static int max98373_set_clock(struct snd_soc_component *component,
181 	struct snd_pcm_hw_params *params)
182 {
183 	struct max98373_priv *max98373 = snd_soc_component_get_drvdata(component);
184 	/* BCLK/LRCLK ratio calculation */
185 	int blr_clk_ratio = params_channels(params) * max98373->ch_size;
186 	int value;
187 
188 	if (!max98373->tdm_mode) {
189 		/* BCLK configuration */
190 		value = max98373_get_bclk_sel(blr_clk_ratio);
191 		if (!value) {
192 			dev_err(component->dev, "format unsupported %d\n",
193 				params_format(params));
194 			return -EINVAL;
195 		}
196 
197 		regmap_update_bits(max98373->regmap,
198 			MAX98373_R2026_PCM_CLOCK_RATIO,
199 			MAX98373_PCM_CLK_SETUP_BSEL_MASK,
200 			value);
201 	}
202 	return 0;
203 }
204 
205 static int max98373_dai_hw_params(struct snd_pcm_substream *substream,
206 	struct snd_pcm_hw_params *params,
207 	struct snd_soc_dai *dai)
208 {
209 	struct snd_soc_component *component = dai->component;
210 	struct max98373_priv *max98373 = snd_soc_component_get_drvdata(component);
211 	unsigned int sampling_rate = 0;
212 	unsigned int chan_sz = 0;
213 
214 	/* pcm mode configuration */
215 	switch (snd_pcm_format_width(params_format(params))) {
216 	case 16:
217 		chan_sz = MAX98373_PCM_MODE_CFG_CHANSZ_16;
218 		break;
219 	case 24:
220 		chan_sz = MAX98373_PCM_MODE_CFG_CHANSZ_24;
221 		break;
222 	case 32:
223 		chan_sz = MAX98373_PCM_MODE_CFG_CHANSZ_32;
224 		break;
225 	default:
226 		dev_err(component->dev, "format unsupported %d\n",
227 			params_format(params));
228 		goto err;
229 	}
230 
231 	max98373->ch_size = snd_pcm_format_width(params_format(params));
232 
233 	regmap_update_bits(max98373->regmap,
234 		MAX98373_R2024_PCM_DATA_FMT_CFG,
235 		MAX98373_PCM_MODE_CFG_CHANSZ_MASK, chan_sz);
236 
237 	dev_dbg(component->dev, "format supported %d",
238 		params_format(params));
239 
240 	/* sampling rate configuration */
241 	switch (params_rate(params)) {
242 	case 8000:
243 		sampling_rate = MAX98373_PCM_SR_SET1_SR_8000;
244 		break;
245 	case 11025:
246 		sampling_rate = MAX98373_PCM_SR_SET1_SR_11025;
247 		break;
248 	case 12000:
249 		sampling_rate = MAX98373_PCM_SR_SET1_SR_12000;
250 		break;
251 	case 16000:
252 		sampling_rate = MAX98373_PCM_SR_SET1_SR_16000;
253 		break;
254 	case 22050:
255 		sampling_rate = MAX98373_PCM_SR_SET1_SR_22050;
256 		break;
257 	case 24000:
258 		sampling_rate = MAX98373_PCM_SR_SET1_SR_24000;
259 		break;
260 	case 32000:
261 		sampling_rate = MAX98373_PCM_SR_SET1_SR_32000;
262 		break;
263 	case 44100:
264 		sampling_rate = MAX98373_PCM_SR_SET1_SR_44100;
265 		break;
266 	case 48000:
267 		sampling_rate = MAX98373_PCM_SR_SET1_SR_48000;
268 		break;
269 	default:
270 		dev_err(component->dev, "rate %d not supported\n",
271 			params_rate(params));
272 		goto err;
273 	}
274 
275 	/* set DAI_SR to correct LRCLK frequency */
276 	regmap_update_bits(max98373->regmap,
277 		MAX98373_R2027_PCM_SR_SETUP_1,
278 		MAX98373_PCM_SR_SET1_SR_MASK,
279 		sampling_rate);
280 	regmap_update_bits(max98373->regmap,
281 		MAX98373_R2028_PCM_SR_SETUP_2,
282 		MAX98373_PCM_SR_SET2_SR_MASK,
283 		sampling_rate << MAX98373_PCM_SR_SET2_SR_SHIFT);
284 
285 	/* set sampling rate of IV */
286 	if (max98373->interleave_mode &&
287 	    sampling_rate > MAX98373_PCM_SR_SET1_SR_16000)
288 		regmap_update_bits(max98373->regmap,
289 			MAX98373_R2028_PCM_SR_SETUP_2,
290 			MAX98373_PCM_SR_SET2_IVADC_SR_MASK,
291 			sampling_rate - 3);
292 	else
293 		regmap_update_bits(max98373->regmap,
294 			MAX98373_R2028_PCM_SR_SETUP_2,
295 			MAX98373_PCM_SR_SET2_IVADC_SR_MASK,
296 			sampling_rate);
297 
298 	return max98373_set_clock(component, params);
299 err:
300 	return -EINVAL;
301 }
302 
303 static int max98373_dai_tdm_slot(struct snd_soc_dai *dai,
304 	unsigned int tx_mask, unsigned int rx_mask,
305 	int slots, int slot_width)
306 {
307 	struct snd_soc_component *component = dai->component;
308 	struct max98373_priv *max98373 = snd_soc_component_get_drvdata(component);
309 	int bsel = 0;
310 	unsigned int chan_sz = 0;
311 	unsigned int mask;
312 	int x, slot_found;
313 
314 	if (!tx_mask && !rx_mask && !slots && !slot_width)
315 		max98373->tdm_mode = false;
316 	else
317 		max98373->tdm_mode = true;
318 
319 	/* BCLK configuration */
320 	bsel = max98373_get_bclk_sel(slots * slot_width);
321 	if (bsel == 0) {
322 		dev_err(component->dev, "BCLK %d not supported\n",
323 			slots * slot_width);
324 		return -EINVAL;
325 	}
326 
327 	regmap_update_bits(max98373->regmap,
328 		MAX98373_R2026_PCM_CLOCK_RATIO,
329 		MAX98373_PCM_CLK_SETUP_BSEL_MASK,
330 		bsel);
331 
332 	/* Channel size configuration */
333 	switch (slot_width) {
334 	case 16:
335 		chan_sz = MAX98373_PCM_MODE_CFG_CHANSZ_16;
336 		break;
337 	case 24:
338 		chan_sz = MAX98373_PCM_MODE_CFG_CHANSZ_24;
339 		break;
340 	case 32:
341 		chan_sz = MAX98373_PCM_MODE_CFG_CHANSZ_32;
342 		break;
343 	default:
344 		dev_err(component->dev, "format unsupported %d\n",
345 			slot_width);
346 		return -EINVAL;
347 	}
348 
349 	regmap_update_bits(max98373->regmap,
350 		MAX98373_R2024_PCM_DATA_FMT_CFG,
351 		MAX98373_PCM_MODE_CFG_CHANSZ_MASK, chan_sz);
352 
353 	/* Rx slot configuration */
354 	slot_found = 0;
355 	mask = rx_mask;
356 	for (x = 0 ; x < 16 ; x++, mask >>= 1) {
357 		if (mask & 0x1) {
358 			if (slot_found == 0)
359 				regmap_update_bits(max98373->regmap,
360 					MAX98373_R2029_PCM_TO_SPK_MONO_MIX_1,
361 					MAX98373_PCM_TO_SPK_CH0_SRC_MASK, x);
362 			else
363 				regmap_write(max98373->regmap,
364 					MAX98373_R202A_PCM_TO_SPK_MONO_MIX_2,
365 					x);
366 			slot_found++;
367 			if (slot_found > 1)
368 				break;
369 		}
370 	}
371 
372 	/* Tx slot Hi-Z configuration */
373 	regmap_write(max98373->regmap,
374 		MAX98373_R2020_PCM_TX_HIZ_EN_1,
375 		~tx_mask & 0xFF);
376 	regmap_write(max98373->regmap,
377 		MAX98373_R2021_PCM_TX_HIZ_EN_2,
378 		(~tx_mask & 0xFF00) >> 8);
379 
380 	return 0;
381 }
382 
383 #define MAX98373_RATES SNDRV_PCM_RATE_8000_96000
384 
385 #define MAX98373_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
386 	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
387 
388 static const struct snd_soc_dai_ops max98373_dai_ops = {
389 	.set_fmt = max98373_dai_set_fmt,
390 	.hw_params = max98373_dai_hw_params,
391 	.set_tdm_slot = max98373_dai_tdm_slot,
392 };
393 
394 static int max98373_dac_event(struct snd_soc_dapm_widget *w,
395 	struct snd_kcontrol *kcontrol, int event)
396 {
397 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
398 	struct max98373_priv *max98373 = snd_soc_component_get_drvdata(component);
399 
400 	switch (event) {
401 	case SND_SOC_DAPM_POST_PMU:
402 		regmap_update_bits(max98373->regmap,
403 			MAX98373_R20FF_GLOBAL_SHDN,
404 			MAX98373_GLOBAL_EN_MASK, 1);
405 		break;
406 	case SND_SOC_DAPM_POST_PMD:
407 		regmap_update_bits(max98373->regmap,
408 			MAX98373_R20FF_GLOBAL_SHDN,
409 			MAX98373_GLOBAL_EN_MASK, 0);
410 		max98373->tdm_mode = 0;
411 		break;
412 	default:
413 		return 0;
414 	}
415 	return 0;
416 }
417 
418 static const char * const max98373_switch_text[] = {
419 	"Left", "Right", "LeftRight"};
420 
421 static const struct soc_enum dai_sel_enum =
422 	SOC_ENUM_SINGLE(MAX98373_R2029_PCM_TO_SPK_MONO_MIX_1,
423 		MAX98373_PCM_TO_SPK_MONOMIX_CFG_SHIFT,
424 		3, max98373_switch_text);
425 
426 static const struct snd_kcontrol_new max98373_dai_controls =
427 	SOC_DAPM_ENUM("DAI Sel", dai_sel_enum);
428 
429 static const struct snd_kcontrol_new max98373_vi_control =
430 	SOC_DAPM_SINGLE("Switch", MAX98373_R202C_PCM_TX_EN, 0, 1, 0);
431 
432 static const struct snd_kcontrol_new max98373_spkfb_control =
433 	SOC_DAPM_SINGLE("Switch", MAX98373_R2043_AMP_EN, 1, 1, 0);
434 
435 static const struct snd_soc_dapm_widget max98373_dapm_widgets[] = {
436 SND_SOC_DAPM_DAC_E("Amp Enable", "HiFi Playback",
437 	MAX98373_R202B_PCM_RX_EN, 0, 0, max98373_dac_event,
438 	SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
439 SND_SOC_DAPM_MUX("DAI Sel Mux", SND_SOC_NOPM, 0, 0,
440 	&max98373_dai_controls),
441 SND_SOC_DAPM_OUTPUT("BE_OUT"),
442 SND_SOC_DAPM_AIF_OUT("Voltage Sense", "HiFi Capture", 0,
443 	MAX98373_R2047_IV_SENSE_ADC_EN, 0, 0),
444 SND_SOC_DAPM_AIF_OUT("Current Sense", "HiFi Capture", 0,
445 	MAX98373_R2047_IV_SENSE_ADC_EN, 1, 0),
446 SND_SOC_DAPM_AIF_OUT("Speaker FB Sense", "HiFi Capture", 0,
447 	SND_SOC_NOPM, 0, 0),
448 SND_SOC_DAPM_SWITCH("VI Sense", SND_SOC_NOPM, 0, 0,
449 	&max98373_vi_control),
450 SND_SOC_DAPM_SWITCH("SpkFB Sense", SND_SOC_NOPM, 0, 0,
451 	&max98373_spkfb_control),
452 SND_SOC_DAPM_SIGGEN("VMON"),
453 SND_SOC_DAPM_SIGGEN("IMON"),
454 SND_SOC_DAPM_SIGGEN("FBMON"),
455 };
456 
457 static DECLARE_TLV_DB_SCALE(max98373_digital_tlv, 0, -50, 0);
458 static const DECLARE_TLV_DB_RANGE(max98373_spk_tlv,
459 	0, 8, TLV_DB_SCALE_ITEM(0, 50, 0),
460 	9, 10, TLV_DB_SCALE_ITEM(500, 100, 0),
461 );
462 static const DECLARE_TLV_DB_RANGE(max98373_spkgain_max_tlv,
463 	0, 9, TLV_DB_SCALE_ITEM(800, 100, 0),
464 );
465 static const DECLARE_TLV_DB_RANGE(max98373_dht_step_size_tlv,
466 	0, 1, TLV_DB_SCALE_ITEM(25, 25, 0),
467 	2, 4, TLV_DB_SCALE_ITEM(100, 100, 0),
468 );
469 static const DECLARE_TLV_DB_RANGE(max98373_dht_spkgain_min_tlv,
470 	0, 9, TLV_DB_SCALE_ITEM(800, 100, 0),
471 );
472 static const DECLARE_TLV_DB_RANGE(max98373_dht_rotation_point_tlv,
473 	0, 1, TLV_DB_SCALE_ITEM(-50, -50, 0),
474 	2, 7, TLV_DB_SCALE_ITEM(-200, -100, 0),
475 	8, 9, TLV_DB_SCALE_ITEM(-1000, -200, 0),
476 	10, 11, TLV_DB_SCALE_ITEM(-1500, -300, 0),
477 	12, 13, TLV_DB_SCALE_ITEM(-2000, -200, 0),
478 	14, 15, TLV_DB_SCALE_ITEM(-2500, -500, 0),
479 );
480 static const DECLARE_TLV_DB_RANGE(max98373_limiter_thresh_tlv,
481 	0, 15, TLV_DB_SCALE_ITEM(0, -100, 0),
482 );
483 
484 static const DECLARE_TLV_DB_RANGE(max98373_bde_gain_tlv,
485 	0, 60, TLV_DB_SCALE_ITEM(0, -25, 0),
486 );
487 
488 static bool max98373_readable_register(struct device *dev, unsigned int reg)
489 {
490 	switch (reg) {
491 	case MAX98373_R2000_SW_RESET:
492 	case MAX98373_R2001_INT_RAW1 ... MAX98373_R200C_INT_EN3:
493 	case MAX98373_R2010_IRQ_CTRL:
494 	case MAX98373_R2014_THERM_WARN_THRESH
495 		... MAX98373_R2018_THERM_FOLDBACK_EN:
496 	case MAX98373_R201E_PIN_DRIVE_STRENGTH
497 		... MAX98373_R2036_SOUNDWIRE_CTRL:
498 	case MAX98373_R203D_AMP_DIG_VOL_CTRL ... MAX98373_R2043_AMP_EN:
499 	case MAX98373_R2046_IV_SENSE_ADC_DSP_CFG
500 		... MAX98373_R2047_IV_SENSE_ADC_EN:
501 	case MAX98373_R2051_MEAS_ADC_SAMPLING_RATE
502 		... MAX98373_R2056_MEAS_ADC_PVDD_CH_EN:
503 	case MAX98373_R2090_BDE_LVL_HOLD ... MAX98373_R2092_BDE_CLIPPER_MODE:
504 	case MAX98373_R2097_BDE_L1_THRESH
505 		... MAX98373_R209B_BDE_THRESH_HYST:
506 	case MAX98373_R20A8_BDE_L1_CFG_1 ... MAX98373_R20B3_BDE_L4_CFG_3:
507 	case MAX98373_R20B5_BDE_EN ... MAX98373_R20B6_BDE_CUR_STATE_READBACK:
508 	case MAX98373_R20D1_DHT_CFG ... MAX98373_R20D4_DHT_EN:
509 	case MAX98373_R20E0_LIMITER_THRESH_CFG ... MAX98373_R20E2_LIMITER_EN:
510 	case MAX98373_R20FE_DEVICE_AUTO_RESTART_CFG
511 		... MAX98373_R20FF_GLOBAL_SHDN:
512 	case MAX98373_R21FF_REV_ID:
513 		return true;
514 	default:
515 		return false;
516 	}
517 };
518 
519 static bool max98373_volatile_reg(struct device *dev, unsigned int reg)
520 {
521 	switch (reg) {
522 	case MAX98373_R2000_SW_RESET ... MAX98373_R2009_INT_FLAG3:
523 	case MAX98373_R2054_MEAS_ADC_PVDD_CH_READBACK:
524 	case MAX98373_R2055_MEAS_ADC_THERM_CH_READBACK:
525 	case MAX98373_R20B6_BDE_CUR_STATE_READBACK:
526 	case MAX98373_R21FF_REV_ID:
527 		return true;
528 	default:
529 		return false;
530 	}
531 }
532 
533 static const char * const max98373_output_voltage_lvl_text[] = {
534 	"5.43V", "6.09V", "6.83V", "7.67V", "8.60V",
535 	"9.65V", "10.83V", "12.15V", "13.63V", "15.29V"
536 };
537 
538 static SOC_ENUM_SINGLE_DECL(max98373_out_volt_enum,
539 			    MAX98373_R203E_AMP_PATH_GAIN, 0,
540 			    max98373_output_voltage_lvl_text);
541 
542 static const char * const max98373_dht_attack_rate_text[] = {
543 	"17.5us", "35us", "70us", "140us",
544 	"280us", "560us", "1120us", "2240us"
545 };
546 
547 static SOC_ENUM_SINGLE_DECL(max98373_dht_attack_rate_enum,
548 			    MAX98373_R20D2_DHT_ATTACK_CFG, 0,
549 			    max98373_dht_attack_rate_text);
550 
551 static const char * const max98373_dht_release_rate_text[] = {
552 	"45ms", "225ms", "450ms", "1150ms",
553 	"2250ms", "3100ms", "4500ms", "6750ms"
554 };
555 
556 static SOC_ENUM_SINGLE_DECL(max98373_dht_release_rate_enum,
557 			    MAX98373_R20D3_DHT_RELEASE_CFG, 0,
558 			    max98373_dht_release_rate_text);
559 
560 static const char * const max98373_limiter_attack_rate_text[] = {
561 	"10us", "20us", "40us", "80us",
562 	"160us", "320us", "640us", "1.28ms",
563 	"2.56ms", "5.12ms", "10.24ms", "20.48ms",
564 	"40.96ms", "81.92ms", "16.384ms", "32.768ms"
565 };
566 
567 static SOC_ENUM_SINGLE_DECL(max98373_limiter_attack_rate_enum,
568 			    MAX98373_R20E1_LIMITER_ATK_REL_RATES, 4,
569 			    max98373_limiter_attack_rate_text);
570 
571 static const char * const max98373_limiter_release_rate_text[] = {
572 	"40us", "80us", "160us", "320us",
573 	"640us", "1.28ms", "2.56ms", "5.120ms",
574 	"10.24ms", "20.48ms", "40.96ms", "81.92ms",
575 	"163.84ms", "327.68ms", "655.36ms", "1310.72ms"
576 };
577 
578 static SOC_ENUM_SINGLE_DECL(max98373_limiter_release_rate_enum,
579 			    MAX98373_R20E1_LIMITER_ATK_REL_RATES, 0,
580 			    max98373_limiter_release_rate_text);
581 
582 static const char * const max98373_ADC_samplerate_text[] = {
583 	"333kHz", "192kHz", "64kHz", "48kHz"
584 };
585 
586 static SOC_ENUM_SINGLE_DECL(max98373_adc_samplerate_enum,
587 			    MAX98373_R2051_MEAS_ADC_SAMPLING_RATE, 0,
588 			    max98373_ADC_samplerate_text);
589 
590 static const struct snd_kcontrol_new max98373_snd_controls[] = {
591 SOC_SINGLE("Digital Vol Sel Switch", MAX98373_R203F_AMP_DSP_CFG,
592 	MAX98373_AMP_VOL_SEL_SHIFT, 1, 0),
593 SOC_SINGLE("Volume Location Switch", MAX98373_R203F_AMP_DSP_CFG,
594 	MAX98373_AMP_VOL_SEL_SHIFT, 1, 0),
595 SOC_SINGLE("Ramp Up Switch", MAX98373_R203F_AMP_DSP_CFG,
596 	MAX98373_AMP_DSP_CFG_RMP_UP_SHIFT, 1, 0),
597 SOC_SINGLE("Ramp Down Switch", MAX98373_R203F_AMP_DSP_CFG,
598 	MAX98373_AMP_DSP_CFG_RMP_DN_SHIFT, 1, 0),
599 SOC_SINGLE("CLK Monitor Switch", MAX98373_R20FE_DEVICE_AUTO_RESTART_CFG,
600 	MAX98373_CLOCK_MON_SHIFT, 1, 0),
601 SOC_SINGLE("Dither Switch", MAX98373_R203F_AMP_DSP_CFG,
602 	MAX98373_AMP_DSP_CFG_DITH_SHIFT, 1, 0),
603 SOC_SINGLE("DC Blocker Switch", MAX98373_R203F_AMP_DSP_CFG,
604 	MAX98373_AMP_DSP_CFG_DCBLK_SHIFT, 1, 0),
605 SOC_SINGLE_TLV("Digital Volume", MAX98373_R203D_AMP_DIG_VOL_CTRL,
606 	0, 0x7F, 0, max98373_digital_tlv),
607 SOC_SINGLE_TLV("Speaker Volume", MAX98373_R203E_AMP_PATH_GAIN,
608 	MAX98373_SPK_DIGI_GAIN_SHIFT, 10, 0, max98373_spk_tlv),
609 SOC_SINGLE_TLV("FS Max Volume", MAX98373_R203E_AMP_PATH_GAIN,
610 	MAX98373_FS_GAIN_MAX_SHIFT, 9, 0, max98373_spkgain_max_tlv),
611 SOC_ENUM("Output Voltage", max98373_out_volt_enum),
612 /* Dynamic Headroom Tracking */
613 SOC_SINGLE("DHT Switch", MAX98373_R20D4_DHT_EN,
614 	MAX98373_DHT_EN_SHIFT, 1, 0),
615 SOC_SINGLE_TLV("DHT Min Volume", MAX98373_R20D1_DHT_CFG,
616 	MAX98373_DHT_SPK_GAIN_MIN_SHIFT, 9, 0, max98373_dht_spkgain_min_tlv),
617 SOC_SINGLE_TLV("DHT Rot Pnt Volume", MAX98373_R20D1_DHT_CFG,
618 	MAX98373_DHT_ROT_PNT_SHIFT, 15, 0, max98373_dht_rotation_point_tlv),
619 SOC_SINGLE_TLV("DHT Attack Step Volume", MAX98373_R20D2_DHT_ATTACK_CFG,
620 	MAX98373_DHT_ATTACK_STEP_SHIFT, 4, 0, max98373_dht_step_size_tlv),
621 SOC_SINGLE_TLV("DHT Release Step Volume", MAX98373_R20D3_DHT_RELEASE_CFG,
622 	MAX98373_DHT_RELEASE_STEP_SHIFT, 4, 0, max98373_dht_step_size_tlv),
623 SOC_ENUM("DHT Attack Rate", max98373_dht_attack_rate_enum),
624 SOC_ENUM("DHT Release Rate", max98373_dht_release_rate_enum),
625 /* ADC configuration */
626 SOC_SINGLE("ADC PVDD CH Switch", MAX98373_R2056_MEAS_ADC_PVDD_CH_EN, 0, 1, 0),
627 SOC_SINGLE("ADC PVDD FLT Switch", MAX98373_R2052_MEAS_ADC_PVDD_FLT_CFG,
628 	MAX98373_FLT_EN_SHIFT, 1, 0),
629 SOC_SINGLE("ADC TEMP FLT Switch", MAX98373_R2053_MEAS_ADC_THERM_FLT_CFG,
630 	MAX98373_FLT_EN_SHIFT, 1, 0),
631 SOC_SINGLE("ADC PVDD", MAX98373_R2054_MEAS_ADC_PVDD_CH_READBACK, 0, 0xFF, 0),
632 SOC_SINGLE("ADC TEMP", MAX98373_R2055_MEAS_ADC_THERM_CH_READBACK, 0, 0xFF, 0),
633 SOC_SINGLE("ADC PVDD FLT Coeff", MAX98373_R2052_MEAS_ADC_PVDD_FLT_CFG,
634 	0, 0x3, 0),
635 SOC_SINGLE("ADC TEMP FLT Coeff", MAX98373_R2053_MEAS_ADC_THERM_FLT_CFG,
636 	0, 0x3, 0),
637 SOC_ENUM("ADC SampleRate", max98373_adc_samplerate_enum),
638 /* Brownout Detection Engine */
639 SOC_SINGLE("BDE Switch", MAX98373_R20B5_BDE_EN, MAX98373_BDE_EN_SHIFT, 1, 0),
640 SOC_SINGLE("BDE LVL4 Mute Switch", MAX98373_R20B2_BDE_L4_CFG_2,
641 	MAX98373_LVL4_MUTE_EN_SHIFT, 1, 0),
642 SOC_SINGLE("BDE LVL4 Hold Switch", MAX98373_R20B2_BDE_L4_CFG_2,
643 	MAX98373_LVL4_HOLD_EN_SHIFT, 1, 0),
644 SOC_SINGLE("BDE LVL1 Thresh", MAX98373_R2097_BDE_L1_THRESH, 0, 0xFF, 0),
645 SOC_SINGLE("BDE LVL2 Thresh", MAX98373_R2098_BDE_L2_THRESH, 0, 0xFF, 0),
646 SOC_SINGLE("BDE LVL3 Thresh", MAX98373_R2099_BDE_L3_THRESH, 0, 0xFF, 0),
647 SOC_SINGLE("BDE LVL4 Thresh", MAX98373_R209A_BDE_L4_THRESH, 0, 0xFF, 0),
648 SOC_SINGLE("BDE Active Level", MAX98373_R20B6_BDE_CUR_STATE_READBACK, 0, 8, 0),
649 SOC_SINGLE("BDE Clip Mode Switch", MAX98373_R2092_BDE_CLIPPER_MODE, 0, 1, 0),
650 SOC_SINGLE("BDE Thresh Hysteresis", MAX98373_R209B_BDE_THRESH_HYST, 0, 0xFF, 0),
651 SOC_SINGLE("BDE Hold Time", MAX98373_R2090_BDE_LVL_HOLD, 0, 0xFF, 0),
652 SOC_SINGLE("BDE Attack Rate", MAX98373_R2091_BDE_GAIN_ATK_REL_RATE, 4, 0xF, 0),
653 SOC_SINGLE("BDE Release Rate", MAX98373_R2091_BDE_GAIN_ATK_REL_RATE, 0, 0xF, 0),
654 SOC_SINGLE_TLV("BDE LVL1 Clip Thresh Volume", MAX98373_R20A9_BDE_L1_CFG_2,
655 	0, 0x3C, 0, max98373_bde_gain_tlv),
656 SOC_SINGLE_TLV("BDE LVL2 Clip Thresh Volume", MAX98373_R20AC_BDE_L2_CFG_2,
657 	0, 0x3C, 0, max98373_bde_gain_tlv),
658 SOC_SINGLE_TLV("BDE LVL3 Clip Thresh Volume", MAX98373_R20AF_BDE_L3_CFG_2,
659 	0, 0x3C, 0, max98373_bde_gain_tlv),
660 SOC_SINGLE_TLV("BDE LVL4 Clip Thresh Volume", MAX98373_R20B2_BDE_L4_CFG_2,
661 	0, 0x3C, 0, max98373_bde_gain_tlv),
662 SOC_SINGLE_TLV("BDE LVL1 Clip Reduction Volume", MAX98373_R20AA_BDE_L1_CFG_3,
663 	0, 0x3C, 0, max98373_bde_gain_tlv),
664 SOC_SINGLE_TLV("BDE LVL2 Clip Reduction Volume", MAX98373_R20AD_BDE_L2_CFG_3,
665 	0, 0x3C, 0, max98373_bde_gain_tlv),
666 SOC_SINGLE_TLV("BDE LVL3 Clip Reduction Volume", MAX98373_R20B0_BDE_L3_CFG_3,
667 	0, 0x3C, 0, max98373_bde_gain_tlv),
668 SOC_SINGLE_TLV("BDE LVL4 Clip Reduction Volume", MAX98373_R20B3_BDE_L4_CFG_3,
669 	0, 0x3C, 0, max98373_bde_gain_tlv),
670 SOC_SINGLE_TLV("BDE LVL1 Limiter Thresh Volume", MAX98373_R20A8_BDE_L1_CFG_1,
671 	0, 0xF, 0, max98373_limiter_thresh_tlv),
672 SOC_SINGLE_TLV("BDE LVL2 Limiter Thresh Volume", MAX98373_R20AB_BDE_L2_CFG_1,
673 	0, 0xF, 0, max98373_limiter_thresh_tlv),
674 SOC_SINGLE_TLV("BDE LVL3 Limiter Thresh Volume", MAX98373_R20AE_BDE_L3_CFG_1,
675 	0, 0xF, 0, max98373_limiter_thresh_tlv),
676 SOC_SINGLE_TLV("BDE LVL4 Limiter Thresh Volume", MAX98373_R20B1_BDE_L4_CFG_1,
677 	0, 0xF, 0, max98373_limiter_thresh_tlv),
678 /* Limiter */
679 SOC_SINGLE("Limiter Switch", MAX98373_R20E2_LIMITER_EN,
680 	MAX98373_LIMITER_EN_SHIFT, 1, 0),
681 SOC_SINGLE("Limiter Src Switch", MAX98373_R20E0_LIMITER_THRESH_CFG,
682 	MAX98373_LIMITER_THRESH_SRC_SHIFT, 1, 0),
683 SOC_SINGLE_TLV("Limiter Thresh Volume", MAX98373_R20E0_LIMITER_THRESH_CFG,
684 	MAX98373_LIMITER_THRESH_SHIFT, 15, 0, max98373_limiter_thresh_tlv),
685 SOC_ENUM("Limiter Attack Rate", max98373_limiter_attack_rate_enum),
686 SOC_ENUM("Limiter Release Rate", max98373_limiter_release_rate_enum),
687 };
688 
689 static const struct snd_soc_dapm_route max98373_audio_map[] = {
690 	/* Plabyack */
691 	{"DAI Sel Mux", "Left", "Amp Enable"},
692 	{"DAI Sel Mux", "Right", "Amp Enable"},
693 	{"DAI Sel Mux", "LeftRight", "Amp Enable"},
694 	{"BE_OUT", NULL, "DAI Sel Mux"},
695 	/* Capture */
696 	{ "VI Sense", "Switch", "VMON" },
697 	{ "VI Sense", "Switch", "IMON" },
698 	{ "SpkFB Sense", "Switch", "FBMON" },
699 	{ "Voltage Sense", NULL, "VI Sense" },
700 	{ "Current Sense", NULL, "VI Sense" },
701 	{ "Speaker FB Sense", NULL, "SpkFB Sense" },
702 };
703 
704 static struct snd_soc_dai_driver max98373_dai[] = {
705 	{
706 		.name = "max98373-aif1",
707 		.playback = {
708 			.stream_name = "HiFi Playback",
709 			.channels_min = 1,
710 			.channels_max = 2,
711 			.rates = MAX98373_RATES,
712 			.formats = MAX98373_FORMATS,
713 		},
714 		.capture = {
715 			.stream_name = "HiFi Capture",
716 			.channels_min = 1,
717 			.channels_max = 2,
718 			.rates = MAX98373_RATES,
719 			.formats = MAX98373_FORMATS,
720 		},
721 		.ops = &max98373_dai_ops,
722 	}
723 };
724 
725 static int max98373_probe(struct snd_soc_component *component)
726 {
727 	struct max98373_priv *max98373 = snd_soc_component_get_drvdata(component);
728 
729 	/* Software Reset */
730 	regmap_write(max98373->regmap,
731 		MAX98373_R2000_SW_RESET, MAX98373_SOFT_RESET);
732 
733 	/* IV default slot configuration */
734 	regmap_write(max98373->regmap,
735 		MAX98373_R2020_PCM_TX_HIZ_EN_1,
736 		0xFF);
737 	regmap_write(max98373->regmap,
738 		MAX98373_R2021_PCM_TX_HIZ_EN_2,
739 		0xFF);
740 	/* L/R mix configuration */
741 	regmap_write(max98373->regmap,
742 		MAX98373_R2029_PCM_TO_SPK_MONO_MIX_1,
743 		0x80);
744 	regmap_write(max98373->regmap,
745 		MAX98373_R202A_PCM_TO_SPK_MONO_MIX_2,
746 		0x1);
747 	/* Set inital volume (0dB) */
748 	regmap_write(max98373->regmap,
749 		MAX98373_R203D_AMP_DIG_VOL_CTRL,
750 		0x00);
751 	regmap_write(max98373->regmap,
752 		MAX98373_R203E_AMP_PATH_GAIN,
753 		0x00);
754 	/* Enable DC blocker */
755 	regmap_write(max98373->regmap,
756 		MAX98373_R203F_AMP_DSP_CFG,
757 		0x3);
758 	/* Enable IMON VMON DC blocker */
759 	regmap_write(max98373->regmap,
760 		MAX98373_R2046_IV_SENSE_ADC_DSP_CFG,
761 		0x7);
762 	/* voltage, current slot configuration */
763 	regmap_write(max98373->regmap,
764 		MAX98373_R2022_PCM_TX_SRC_1,
765 		(max98373->i_slot << MAX98373_PCM_TX_CH_SRC_A_I_SHIFT |
766 		max98373->v_slot) & 0xFF);
767 	if (max98373->v_slot < 8)
768 		regmap_update_bits(max98373->regmap,
769 			MAX98373_R2020_PCM_TX_HIZ_EN_1,
770 			1 << max98373->v_slot, 0);
771 	else
772 		regmap_update_bits(max98373->regmap,
773 			MAX98373_R2021_PCM_TX_HIZ_EN_2,
774 			1 << (max98373->v_slot - 8), 0);
775 
776 	if (max98373->i_slot < 8)
777 		regmap_update_bits(max98373->regmap,
778 			MAX98373_R2020_PCM_TX_HIZ_EN_1,
779 			1 << max98373->i_slot, 0);
780 	else
781 		regmap_update_bits(max98373->regmap,
782 			MAX98373_R2021_PCM_TX_HIZ_EN_2,
783 			1 << (max98373->i_slot - 8), 0);
784 
785 	/* speaker feedback slot configuration */
786 	regmap_write(max98373->regmap,
787 		MAX98373_R2023_PCM_TX_SRC_2,
788 		max98373->spkfb_slot & 0xFF);
789 
790 	/* Set interleave mode */
791 	if (max98373->interleave_mode)
792 		regmap_update_bits(max98373->regmap,
793 			MAX98373_R2024_PCM_DATA_FMT_CFG,
794 			MAX98373_PCM_TX_CH_INTERLEAVE_MASK,
795 			MAX98373_PCM_TX_CH_INTERLEAVE_MASK);
796 
797 	/* Speaker enable */
798 	regmap_update_bits(max98373->regmap,
799 		MAX98373_R2043_AMP_EN,
800 		MAX98373_SPK_EN_MASK, 1);
801 
802 	return 0;
803 }
804 
805 #ifdef CONFIG_PM_SLEEP
806 static int max98373_suspend(struct device *dev)
807 {
808 	struct max98373_priv *max98373 = dev_get_drvdata(dev);
809 
810 	regcache_cache_only(max98373->regmap, true);
811 	regcache_mark_dirty(max98373->regmap);
812 	return 0;
813 }
814 static int max98373_resume(struct device *dev)
815 {
816 	struct max98373_priv *max98373 = dev_get_drvdata(dev);
817 
818 	regmap_write(max98373->regmap,
819 		MAX98373_R2000_SW_RESET, MAX98373_SOFT_RESET);
820 	regcache_cache_only(max98373->regmap, false);
821 	regcache_sync(max98373->regmap);
822 	return 0;
823 }
824 #endif
825 
826 static const struct dev_pm_ops max98373_pm = {
827 	SET_SYSTEM_SLEEP_PM_OPS(max98373_suspend, max98373_resume)
828 };
829 
830 static const struct snd_soc_component_driver soc_codec_dev_max98373 = {
831 	.probe			= max98373_probe,
832 	.controls		= max98373_snd_controls,
833 	.num_controls		= ARRAY_SIZE(max98373_snd_controls),
834 	.dapm_widgets		= max98373_dapm_widgets,
835 	.num_dapm_widgets	= ARRAY_SIZE(max98373_dapm_widgets),
836 	.dapm_routes		= max98373_audio_map,
837 	.num_dapm_routes	= ARRAY_SIZE(max98373_audio_map),
838 	.idle_bias_on		= 1,
839 	.use_pmdown_time	= 1,
840 	.endianness		= 1,
841 	.non_legacy_dai_naming	= 1,
842 };
843 
844 static const struct regmap_config max98373_regmap = {
845 	.reg_bits = 16,
846 	.val_bits = 8,
847 	.max_register = MAX98373_R21FF_REV_ID,
848 	.reg_defaults  = max98373_reg,
849 	.num_reg_defaults = ARRAY_SIZE(max98373_reg),
850 	.readable_reg = max98373_readable_register,
851 	.volatile_reg = max98373_volatile_reg,
852 	.cache_type = REGCACHE_RBTREE,
853 };
854 
855 static void max98373_slot_config(struct i2c_client *i2c,
856 	struct max98373_priv *max98373)
857 {
858 	int value;
859 	struct device *dev = &i2c->dev;
860 
861 	if (!device_property_read_u32(dev, "maxim,vmon-slot-no", &value))
862 		max98373->v_slot = value & 0xF;
863 	else
864 		max98373->v_slot = 0;
865 
866 	if (!device_property_read_u32(dev, "maxim,imon-slot-no", &value))
867 		max98373->i_slot = value & 0xF;
868 	else
869 		max98373->i_slot = 1;
870 
871 	if (!device_property_read_u32(dev, "maxim,spkfb-slot-no", &value))
872 		max98373->spkfb_slot = value & 0xF;
873 	else
874 		max98373->spkfb_slot = 2;
875 }
876 
877 static int max98373_i2c_probe(struct i2c_client *i2c,
878 	const struct i2c_device_id *id)
879 {
880 
881 	int ret = 0;
882 	int reg = 0;
883 	struct max98373_priv *max98373 = NULL;
884 
885 	max98373 = devm_kzalloc(&i2c->dev, sizeof(*max98373), GFP_KERNEL);
886 
887 	if (!max98373) {
888 		ret = -ENOMEM;
889 		return ret;
890 	}
891 	i2c_set_clientdata(i2c, max98373);
892 
893 	/* update interleave mode info */
894 	if (device_property_read_bool(&i2c->dev, "maxim,interleave_mode"))
895 		max98373->interleave_mode = 1;
896 	else
897 		max98373->interleave_mode = 0;
898 
899 
900 	/* regmap initialization */
901 	max98373->regmap
902 		= devm_regmap_init_i2c(i2c, &max98373_regmap);
903 	if (IS_ERR(max98373->regmap)) {
904 		ret = PTR_ERR(max98373->regmap);
905 		dev_err(&i2c->dev,
906 			"Failed to allocate regmap: %d\n", ret);
907 		return ret;
908 	}
909 
910 	/* Check Revision ID */
911 	ret = regmap_read(max98373->regmap,
912 		MAX98373_R21FF_REV_ID, &reg);
913 	if (ret < 0) {
914 		dev_err(&i2c->dev,
915 			"Failed to read: 0x%02X\n", MAX98373_R21FF_REV_ID);
916 		return ret;
917 	}
918 	dev_info(&i2c->dev, "MAX98373 revisionID: 0x%02X\n", reg);
919 
920 	/* voltage/current slot configuration */
921 	max98373_slot_config(i2c, max98373);
922 
923 	/* codec registeration */
924 	ret = devm_snd_soc_register_component(&i2c->dev, &soc_codec_dev_max98373,
925 		max98373_dai, ARRAY_SIZE(max98373_dai));
926 	if (ret < 0)
927 		dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
928 
929 	return ret;
930 }
931 
932 static const struct i2c_device_id max98373_i2c_id[] = {
933 	{ "max98373", 0},
934 	{ },
935 };
936 
937 MODULE_DEVICE_TABLE(i2c, max98373_i2c_id);
938 
939 #if defined(CONFIG_OF)
940 static const struct of_device_id max98373_of_match[] = {
941 	{ .compatible = "maxim,max98373", },
942 	{ }
943 };
944 MODULE_DEVICE_TABLE(of, max98373_of_match);
945 #endif
946 
947 #ifdef CONFIG_ACPI
948 static const struct acpi_device_id max98373_acpi_match[] = {
949 	{ "MX98373", 0 },
950 	{},
951 };
952 MODULE_DEVICE_TABLE(acpi, max98373_acpi_match);
953 #endif
954 
955 static struct i2c_driver max98373_i2c_driver = {
956 	.driver = {
957 		.name = "max98373",
958 		.of_match_table = of_match_ptr(max98373_of_match),
959 		.acpi_match_table = ACPI_PTR(max98373_acpi_match),
960 		.pm = &max98373_pm,
961 	},
962 	.probe = max98373_i2c_probe,
963 	.id_table = max98373_i2c_id,
964 };
965 
966 module_i2c_driver(max98373_i2c_driver)
967 
968 MODULE_DESCRIPTION("ALSA SoC MAX98373 driver");
969 MODULE_AUTHOR("Ryan Lee <ryans.lee@maximintegrated.com>");
970 MODULE_LICENSE("GPL");
971