1 // SPDX-License-Identifier: GPL-2.0-only
3 * max98926.c -- ALSA SoC MAX98926 driver
4 * Copyright 2013-15 Maxim Integrated Products
6 #include <linux/delay.h>
8 #include <linux/module.h>
9 #include <linux/regmap.h>
10 #include <linux/slab.h>
11 #include <linux/cdev.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15 #include <sound/tlv.h>
18 static const char * const max98926_boost_voltage_txt
[] = {
19 "8.5V", "8.25V", "8.0V", "7.75V", "7.5V", "7.25V", "7.0V", "6.75V",
20 "6.5V", "6.5V", "6.5V", "6.5V", "6.5V", "6.5V", "6.5V", "6.5V"
23 static const char *const max98926_pdm_ch_text
[] = {
27 static const char *const max98926_hpf_cutoff_txt
[] = {
28 "Disable", "DC Block", "100Hz",
29 "200Hz", "400Hz", "800Hz",
32 static const struct reg_default max98926_reg
[] = {
33 { 0x0B, 0x00 }, /* IRQ Enable0 */
34 { 0x0C, 0x00 }, /* IRQ Enable1 */
35 { 0x0D, 0x00 }, /* IRQ Enable2 */
36 { 0x0E, 0x00 }, /* IRQ Clear0 */
37 { 0x0F, 0x00 }, /* IRQ Clear1 */
38 { 0x10, 0x00 }, /* IRQ Clear2 */
39 { 0x11, 0xC0 }, /* Map0 */
40 { 0x12, 0x00 }, /* Map1 */
41 { 0x13, 0x00 }, /* Map2 */
42 { 0x14, 0xF0 }, /* Map3 */
43 { 0x15, 0x00 }, /* Map4 */
44 { 0x16, 0xAB }, /* Map5 */
45 { 0x17, 0x89 }, /* Map6 */
46 { 0x18, 0x00 }, /* Map7 */
47 { 0x19, 0x00 }, /* Map8 */
48 { 0x1A, 0x04 }, /* DAI Clock Mode 1 */
49 { 0x1B, 0x00 }, /* DAI Clock Mode 2 */
50 { 0x1C, 0x00 }, /* DAI Clock Divider Denominator MSBs */
51 { 0x1D, 0x00 }, /* DAI Clock Divider Denominator LSBs */
52 { 0x1E, 0xF0 }, /* DAI Clock Divider Numerator MSBs */
53 { 0x1F, 0x00 }, /* DAI Clock Divider Numerator LSBs */
54 { 0x20, 0x50 }, /* Format */
55 { 0x21, 0x00 }, /* TDM Slot Select */
56 { 0x22, 0x00 }, /* DOUT Configuration VMON */
57 { 0x23, 0x00 }, /* DOUT Configuration IMON */
58 { 0x24, 0x00 }, /* DOUT Configuration VBAT */
59 { 0x25, 0x00 }, /* DOUT Configuration VBST */
60 { 0x26, 0x00 }, /* DOUT Configuration FLAG */
61 { 0x27, 0xFF }, /* DOUT HiZ Configuration 1 */
62 { 0x28, 0xFF }, /* DOUT HiZ Configuration 2 */
63 { 0x29, 0xFF }, /* DOUT HiZ Configuration 3 */
64 { 0x2A, 0xFF }, /* DOUT HiZ Configuration 4 */
65 { 0x2B, 0x02 }, /* DOUT Drive Strength */
66 { 0x2C, 0x90 }, /* Filters */
67 { 0x2D, 0x00 }, /* Gain */
68 { 0x2E, 0x02 }, /* Gain Ramping */
69 { 0x2F, 0x00 }, /* Speaker Amplifier */
70 { 0x30, 0x0A }, /* Threshold */
71 { 0x31, 0x00 }, /* ALC Attack */
72 { 0x32, 0x80 }, /* ALC Atten and Release */
73 { 0x33, 0x00 }, /* ALC Infinite Hold Release */
74 { 0x34, 0x92 }, /* ALC Configuration */
75 { 0x35, 0x01 }, /* Boost Converter */
76 { 0x36, 0x00 }, /* Block Enable */
77 { 0x37, 0x00 }, /* Configuration */
78 { 0x38, 0x00 }, /* Global Enable */
79 { 0x3A, 0x00 }, /* Boost Limiter */
82 static const struct soc_enum max98926_voltage_enum
[] = {
83 SOC_ENUM_SINGLE(MAX98926_DAI_CLK_DIV_N_LSBS
, 0,
84 ARRAY_SIZE(max98926_pdm_ch_text
),
85 max98926_pdm_ch_text
),
88 static const struct snd_kcontrol_new max98926_voltage_control
=
89 SOC_DAPM_ENUM("Route", max98926_voltage_enum
);
91 static const struct soc_enum max98926_current_enum
[] = {
92 SOC_ENUM_SINGLE(MAX98926_DAI_CLK_DIV_N_LSBS
,
93 MAX98926_PDM_SOURCE_1_SHIFT
,
94 ARRAY_SIZE(max98926_pdm_ch_text
),
95 max98926_pdm_ch_text
),
98 static const struct snd_kcontrol_new max98926_current_control
=
99 SOC_DAPM_ENUM("Route", max98926_current_enum
);
101 static const struct snd_kcontrol_new max98926_mixer_controls
[] = {
102 SOC_DAPM_SINGLE("PCM Single Switch", MAX98926_SPK_AMP
,
103 MAX98926_INSELECT_MODE_SHIFT
, 0, 0),
104 SOC_DAPM_SINGLE("PDM Single Switch", MAX98926_SPK_AMP
,
105 MAX98926_INSELECT_MODE_SHIFT
, 1, 0),
108 static const struct snd_kcontrol_new max98926_dai_controls
[] = {
109 SOC_DAPM_SINGLE("Left", MAX98926_GAIN
,
110 MAX98926_DAC_IN_SEL_SHIFT
, 0, 0),
111 SOC_DAPM_SINGLE("Right", MAX98926_GAIN
,
112 MAX98926_DAC_IN_SEL_SHIFT
, 1, 0),
113 SOC_DAPM_SINGLE("LeftRight", MAX98926_GAIN
,
114 MAX98926_DAC_IN_SEL_SHIFT
, 2, 0),
115 SOC_DAPM_SINGLE("(Left+Right)/2 Switch", MAX98926_GAIN
,
116 MAX98926_DAC_IN_SEL_SHIFT
, 3, 0),
119 static const struct snd_soc_dapm_widget max98926_dapm_widgets
[] = {
120 SND_SOC_DAPM_AIF_IN("DAI_OUT", "HiFi Playback", 0,
122 SND_SOC_DAPM_DAC("Amp Enable", NULL
, MAX98926_BLOCK_ENABLE
,
123 MAX98926_SPK_EN_SHIFT
, 0),
124 SND_SOC_DAPM_SUPPLY("Global Enable", MAX98926_GLOBAL_ENABLE
,
125 MAX98926_EN_SHIFT
, 0, NULL
, 0),
126 SND_SOC_DAPM_SUPPLY("VI Enable", MAX98926_BLOCK_ENABLE
,
127 MAX98926_ADC_IMON_EN_WIDTH
|
128 MAX98926_ADC_VMON_EN_SHIFT
,
130 SND_SOC_DAPM_PGA("BST Enable", MAX98926_BLOCK_ENABLE
,
131 MAX98926_BST_EN_SHIFT
, 0, NULL
, 0),
132 SND_SOC_DAPM_OUTPUT("BE_OUT"),
133 SND_SOC_DAPM_MIXER("PCM Sel", MAX98926_SPK_AMP
,
134 MAX98926_INSELECT_MODE_SHIFT
, 0,
135 &max98926_mixer_controls
[0],
136 ARRAY_SIZE(max98926_mixer_controls
)),
137 SND_SOC_DAPM_MIXER("DAI Sel",
138 MAX98926_GAIN
, MAX98926_DAC_IN_SEL_SHIFT
, 0,
139 &max98926_dai_controls
[0],
140 ARRAY_SIZE(max98926_dai_controls
)),
141 SND_SOC_DAPM_MUX("PDM CH1 Source",
142 MAX98926_DAI_CLK_DIV_N_LSBS
,
143 MAX98926_PDM_CURRENT_SHIFT
,
144 0, &max98926_current_control
),
145 SND_SOC_DAPM_MUX("PDM CH0 Source",
146 MAX98926_DAI_CLK_DIV_N_LSBS
,
147 MAX98926_PDM_VOLTAGE_SHIFT
,
148 0, &max98926_voltage_control
),
151 static const struct snd_soc_dapm_route max98926_audio_map
[] = {
152 {"VI Enable", NULL
, "DAI_OUT"},
153 {"DAI Sel", "Left", "VI Enable"},
154 {"DAI Sel", "Right", "VI Enable"},
155 {"DAI Sel", "LeftRight", "VI Enable"},
156 {"DAI Sel", "LeftRightDiv2", "VI Enable"},
157 {"PCM Sel", "PCM", "DAI Sel"},
159 {"PDM CH1 Source", "Current", "DAI_OUT"},
160 {"PDM CH1 Source", "Voltage", "DAI_OUT"},
161 {"PDM CH0 Source", "Current", "DAI_OUT"},
162 {"PDM CH0 Source", "Voltage", "DAI_OUT"},
163 {"PCM Sel", "Analog", "PDM CH1 Source"},
164 {"PCM Sel", "Analog", "PDM CH0 Source"},
165 {"Amp Enable", NULL
, "PCM Sel"},
167 {"BST Enable", NULL
, "Amp Enable"},
168 {"BE_OUT", NULL
, "BST Enable"},
171 static bool max98926_volatile_register(struct device
*dev
, unsigned int reg
)
174 case MAX98926_VBAT_DATA
:
175 case MAX98926_VBST_DATA
:
176 case MAX98926_LIVE_STATUS0
:
177 case MAX98926_LIVE_STATUS1
:
178 case MAX98926_LIVE_STATUS2
:
179 case MAX98926_STATE0
:
180 case MAX98926_STATE1
:
181 case MAX98926_STATE2
:
185 case MAX98926_VERSION
:
192 static bool max98926_readable_register(struct device
*dev
, unsigned int reg
)
195 case MAX98926_IRQ_CLEAR0
:
196 case MAX98926_IRQ_CLEAR1
:
197 case MAX98926_IRQ_CLEAR2
:
198 case MAX98926_ALC_HOLD_RLS
:
205 static DECLARE_TLV_DB_SCALE(max98926_spk_tlv
, -600, 100, 0);
206 static DECLARE_TLV_DB_RANGE(max98926_current_tlv
,
207 0, 11, TLV_DB_SCALE_ITEM(20, 20, 0),
208 12, 15, TLV_DB_SCALE_ITEM(320, 40, 0),
211 static SOC_ENUM_SINGLE_DECL(max98926_dac_hpf_cutoff
,
212 MAX98926_FILTERS
, MAX98926_DAC_HPF_SHIFT
,
213 max98926_hpf_cutoff_txt
);
215 static SOC_ENUM_SINGLE_DECL(max98926_boost_voltage
,
216 MAX98926_CONFIGURATION
, MAX98926_BST_VOUT_SHIFT
,
217 max98926_boost_voltage_txt
);
219 static const struct snd_kcontrol_new max98926_snd_controls
[] = {
220 SOC_SINGLE_TLV("Speaker Volume", MAX98926_GAIN
,
221 MAX98926_SPK_GAIN_SHIFT
,
222 (1<<MAX98926_SPK_GAIN_WIDTH
)-1, 0,
224 SOC_SINGLE("Ramp Switch", MAX98926_GAIN_RAMPING
,
225 MAX98926_SPK_RMP_EN_SHIFT
, 1, 0),
226 SOC_SINGLE("ZCD Switch", MAX98926_GAIN_RAMPING
,
227 MAX98926_SPK_ZCD_EN_SHIFT
, 1, 0),
228 SOC_SINGLE("ALC Switch", MAX98926_THRESHOLD
,
229 MAX98926_ALC_EN_SHIFT
, 1, 0),
230 SOC_SINGLE("ALC Threshold", MAX98926_THRESHOLD
,
231 MAX98926_ALC_TH_SHIFT
,
232 (1<<MAX98926_ALC_TH_WIDTH
)-1, 0),
233 SOC_ENUM("Boost Output Voltage", max98926_boost_voltage
),
234 SOC_SINGLE_TLV("Boost Current Limit", MAX98926_BOOST_LIMITER
,
235 MAX98926_BST_ILIM_SHIFT
,
236 (1<<MAX98926_BST_ILIM_SHIFT
)-1, 0,
237 max98926_current_tlv
),
238 SOC_ENUM("DAC HPF Cutoff", max98926_dac_hpf_cutoff
),
239 SOC_DOUBLE("PDM Channel One", MAX98926_DAI_CLK_DIV_N_LSBS
,
240 MAX98926_PDM_CHANNEL_1_SHIFT
,
241 MAX98926_PDM_CHANNEL_1_HIZ
, 1, 0),
242 SOC_DOUBLE("PDM Channel Zero", MAX98926_DAI_CLK_DIV_N_LSBS
,
243 MAX98926_PDM_CHANNEL_0_SHIFT
,
244 MAX98926_PDM_CHANNEL_0_HIZ
, 1, 0),
247 static const struct {
289 static void max98926_set_sense_data(struct max98926_priv
*max98926
)
291 regmap_update_bits(max98926
->regmap
,
292 MAX98926_DOUT_CFG_VMON
,
293 MAX98926_DAI_VMON_EN_MASK
,
294 MAX98926_DAI_VMON_EN_MASK
);
295 regmap_update_bits(max98926
->regmap
,
296 MAX98926_DOUT_CFG_IMON
,
297 MAX98926_DAI_IMON_EN_MASK
,
298 MAX98926_DAI_IMON_EN_MASK
);
300 if (!max98926
->interleave_mode
) {
302 regmap_update_bits(max98926
->regmap
,
303 MAX98926_DOUT_CFG_VMON
,
304 MAX98926_DAI_VMON_SLOT_MASK
,
307 regmap_update_bits(max98926
->regmap
,
308 MAX98926_DOUT_CFG_IMON
,
309 MAX98926_DAI_IMON_SLOT_MASK
,
312 /* enable interleave mode */
313 regmap_update_bits(max98926
->regmap
,
315 MAX98926_DAI_INTERLEAVE_MASK
,
316 MAX98926_DAI_INTERLEAVE_MASK
);
317 /* set interleave slots */
318 regmap_update_bits(max98926
->regmap
,
319 MAX98926_DOUT_CFG_VBAT
,
320 MAX98926_DAI_INTERLEAVE_SLOT_MASK
,
325 static int max98926_dai_set_fmt(struct snd_soc_dai
*codec_dai
,
328 struct snd_soc_component
*component
= codec_dai
->component
;
329 struct max98926_priv
*max98926
= snd_soc_component_get_drvdata(component
);
330 unsigned int invert
= 0;
332 dev_dbg(component
->dev
, "%s: fmt 0x%08X\n", __func__
, fmt
);
334 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
335 case SND_SOC_DAIFMT_CBS_CFS
:
336 max98926_set_sense_data(max98926
);
339 dev_err(component
->dev
, "DAI clock mode unsupported\n");
343 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
344 case SND_SOC_DAIFMT_NB_NF
:
346 case SND_SOC_DAIFMT_NB_IF
:
347 invert
= MAX98926_DAI_WCI_MASK
;
349 case SND_SOC_DAIFMT_IB_NF
:
350 invert
= MAX98926_DAI_BCI_MASK
;
352 case SND_SOC_DAIFMT_IB_IF
:
353 invert
= MAX98926_DAI_BCI_MASK
| MAX98926_DAI_WCI_MASK
;
356 dev_err(component
->dev
, "DAI invert mode unsupported\n");
360 regmap_write(max98926
->regmap
,
361 MAX98926_FORMAT
, MAX98926_DAI_DLY_MASK
);
362 regmap_update_bits(max98926
->regmap
, MAX98926_FORMAT
,
363 MAX98926_DAI_BCI_MASK
, invert
);
367 static int max98926_dai_hw_params(struct snd_pcm_substream
*substream
,
368 struct snd_pcm_hw_params
*params
,
369 struct snd_soc_dai
*dai
)
371 int dai_sr
= -EINVAL
;
372 int rate
= params_rate(params
), i
;
373 struct snd_soc_component
*component
= dai
->component
;
374 struct max98926_priv
*max98926
= snd_soc_component_get_drvdata(component
);
377 switch (params_format(params
)) {
378 case SNDRV_PCM_FORMAT_S16_LE
:
379 regmap_update_bits(max98926
->regmap
,
381 MAX98926_DAI_CHANSZ_MASK
,
382 MAX98926_DAI_CHANSZ_16
);
383 max98926
->ch_size
= 16;
385 case SNDRV_PCM_FORMAT_S24_LE
:
386 regmap_update_bits(max98926
->regmap
,
388 MAX98926_DAI_CHANSZ_MASK
,
389 MAX98926_DAI_CHANSZ_24
);
390 max98926
->ch_size
= 24;
392 case SNDRV_PCM_FORMAT_S32_LE
:
393 regmap_update_bits(max98926
->regmap
,
395 MAX98926_DAI_CHANSZ_MASK
,
396 MAX98926_DAI_CHANSZ_32
);
397 max98926
->ch_size
= 32;
400 dev_dbg(component
->dev
, "format unsupported %d\n",
401 params_format(params
));
405 /* BCLK/LRCLK ratio calculation */
406 blr_clk_ratio
= params_channels(params
) * max98926
->ch_size
;
408 switch (blr_clk_ratio
) {
410 regmap_update_bits(max98926
->regmap
,
411 MAX98926_DAI_CLK_MODE2
,
412 MAX98926_DAI_BSEL_MASK
,
413 MAX98926_DAI_BSEL_32
);
416 regmap_update_bits(max98926
->regmap
,
417 MAX98926_DAI_CLK_MODE2
,
418 MAX98926_DAI_BSEL_MASK
,
419 MAX98926_DAI_BSEL_48
);
422 regmap_update_bits(max98926
->regmap
,
423 MAX98926_DAI_CLK_MODE2
,
424 MAX98926_DAI_BSEL_MASK
,
425 MAX98926_DAI_BSEL_64
);
431 /* find the closest rate */
432 for (i
= 0; i
< ARRAY_SIZE(rate_table
); i
++) {
433 if (rate_table
[i
].rate
>= rate
) {
434 dai_sr
= rate_table
[i
].sr
;
441 /* set DAI_SR to correct LRCLK frequency */
442 regmap_update_bits(max98926
->regmap
,
443 MAX98926_DAI_CLK_MODE2
,
444 MAX98926_DAI_SR_MASK
, dai_sr
<< MAX98926_DAI_SR_SHIFT
);
448 #define MAX98926_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
449 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
451 static const struct snd_soc_dai_ops max98926_dai_ops
= {
452 .set_fmt
= max98926_dai_set_fmt
,
453 .hw_params
= max98926_dai_hw_params
,
456 static struct snd_soc_dai_driver max98926_dai
[] = {
458 .name
= "max98926-aif1",
460 .stream_name
= "HiFi Playback",
463 .rates
= SNDRV_PCM_RATE_8000_48000
,
464 .formats
= MAX98926_FORMATS
,
467 .stream_name
= "HiFi Capture",
470 .rates
= SNDRV_PCM_RATE_8000_48000
,
471 .formats
= MAX98926_FORMATS
,
473 .ops
= &max98926_dai_ops
,
477 static int max98926_probe(struct snd_soc_component
*component
)
479 struct max98926_priv
*max98926
= snd_soc_component_get_drvdata(component
);
481 max98926
->component
= component
;
483 /* Hi-Z all the slots */
484 regmap_write(max98926
->regmap
, MAX98926_DOUT_HIZ_CFG4
, 0xF0);
488 static const struct snd_soc_component_driver soc_component_dev_max98926
= {
489 .probe
= max98926_probe
,
490 .controls
= max98926_snd_controls
,
491 .num_controls
= ARRAY_SIZE(max98926_snd_controls
),
492 .dapm_routes
= max98926_audio_map
,
493 .num_dapm_routes
= ARRAY_SIZE(max98926_audio_map
),
494 .dapm_widgets
= max98926_dapm_widgets
,
495 .num_dapm_widgets
= ARRAY_SIZE(max98926_dapm_widgets
),
497 .use_pmdown_time
= 1,
499 .non_legacy_dai_naming
= 1,
502 static const struct regmap_config max98926_regmap
= {
505 .max_register
= MAX98926_VERSION
,
506 .reg_defaults
= max98926_reg
,
507 .num_reg_defaults
= ARRAY_SIZE(max98926_reg
),
508 .volatile_reg
= max98926_volatile_register
,
509 .readable_reg
= max98926_readable_register
,
510 .cache_type
= REGCACHE_RBTREE
,
513 static int max98926_i2c_probe(struct i2c_client
*i2c
,
514 const struct i2c_device_id
*id
)
518 struct max98926_priv
*max98926
;
520 max98926
= devm_kzalloc(&i2c
->dev
,
521 sizeof(*max98926
), GFP_KERNEL
);
525 i2c_set_clientdata(i2c
, max98926
);
526 max98926
->regmap
= devm_regmap_init_i2c(i2c
, &max98926_regmap
);
527 if (IS_ERR(max98926
->regmap
)) {
528 ret
= PTR_ERR(max98926
->regmap
);
530 "Failed to allocate regmap: %d\n", ret
);
533 if (of_property_read_bool(i2c
->dev
.of_node
, "interleave-mode"))
534 max98926
->interleave_mode
= true;
536 if (!of_property_read_u32(i2c
->dev
.of_node
, "vmon-slot-no", &value
)) {
537 if (value
> MAX98926_DAI_VMON_SLOT_1E_1F
) {
538 dev_err(&i2c
->dev
, "vmon slot number is wrong:\n");
541 max98926
->v_slot
= value
;
543 if (!of_property_read_u32(i2c
->dev
.of_node
, "imon-slot-no", &value
)) {
544 if (value
> MAX98926_DAI_IMON_SLOT_1E_1F
) {
545 dev_err(&i2c
->dev
, "imon slot number is wrong:\n");
548 max98926
->i_slot
= value
;
550 ret
= regmap_read(max98926
->regmap
,
551 MAX98926_VERSION
, ®
);
553 dev_err(&i2c
->dev
, "Failed to read: %x\n", reg
);
557 ret
= devm_snd_soc_register_component(&i2c
->dev
,
558 &soc_component_dev_max98926
,
559 max98926_dai
, ARRAY_SIZE(max98926_dai
));
562 "Failed to register component: %d\n", ret
);
563 dev_info(&i2c
->dev
, "device version: %x\n", reg
);
568 static const struct i2c_device_id max98926_i2c_id
[] = {
572 MODULE_DEVICE_TABLE(i2c
, max98926_i2c_id
);
574 static const struct of_device_id max98926_of_match
[] = {
575 { .compatible
= "maxim,max98926", },
578 MODULE_DEVICE_TABLE(of
, max98926_of_match
);
580 static struct i2c_driver max98926_i2c_driver
= {
583 .of_match_table
= of_match_ptr(max98926_of_match
),
586 .probe
= max98926_i2c_probe
,
587 .id_table
= max98926_i2c_id
,
590 module_i2c_driver(max98926_i2c_driver
)
591 MODULE_DESCRIPTION("ALSA SoC MAX98926 driver");
592 MODULE_AUTHOR("Anish kumar <anish.kumar@maximintegrated.com>");
593 MODULE_LICENSE("GPL");