2 * max98926.c -- ALSA SoC MAX98926 driver
3 * Copyright 2013-15 Maxim Integrated Products
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/regmap.h>
12 #include <linux/slab.h>
13 #include <linux/cdev.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
16 #include <sound/soc.h>
17 #include <sound/tlv.h>
20 static const char * const max98926_boost_voltage_txt
[] = {
21 "8.5V", "8.25V", "8.0V", "7.75V", "7.5V", "7.25V", "7.0V", "6.75V",
22 "6.5V", "6.5V", "6.5V", "6.5V", "6.5V", "6.5V", "6.5V", "6.5V"
25 static const char * const max98926_boost_current_txt
[] = {
26 "0.6", "0.8", "1.0", "1.2", "1.4", "1.6", "1.8", "2.0",
27 "2.2", "2.4", "2.6", "2.8", "3.2", "3.6", "4.0", "4.4"
30 static const char *const max98926_dai_txt
[] = {
31 "Left", "Right", "LeftRight", "LeftRightDiv2",
34 static const char *const max98926_pdm_ch_text
[] = {
38 static const char *const max98926_hpf_cutoff_txt
[] = {
39 "Disable", "DC Block", "100Hz",
40 "200Hz", "400Hz", "800Hz",
43 static const struct reg_default max98926_reg
[] = {
44 { 0x0B, 0x00 }, /* IRQ Enable0 */
45 { 0x0C, 0x00 }, /* IRQ Enable1 */
46 { 0x0D, 0x00 }, /* IRQ Enable2 */
47 { 0x0E, 0x00 }, /* IRQ Clear0 */
48 { 0x0F, 0x00 }, /* IRQ Clear1 */
49 { 0x10, 0x00 }, /* IRQ Clear2 */
50 { 0x11, 0xC0 }, /* Map0 */
51 { 0x12, 0x00 }, /* Map1 */
52 { 0x13, 0x00 }, /* Map2 */
53 { 0x14, 0xF0 }, /* Map3 */
54 { 0x15, 0x00 }, /* Map4 */
55 { 0x16, 0xAB }, /* Map5 */
56 { 0x17, 0x89 }, /* Map6 */
57 { 0x18, 0x00 }, /* Map7 */
58 { 0x19, 0x00 }, /* Map8 */
59 { 0x1A, 0x04 }, /* DAI Clock Mode 1 */
60 { 0x1B, 0x00 }, /* DAI Clock Mode 2 */
61 { 0x1C, 0x00 }, /* DAI Clock Divider Denominator MSBs */
62 { 0x1D, 0x00 }, /* DAI Clock Divider Denominator LSBs */
63 { 0x1E, 0xF0 }, /* DAI Clock Divider Numerator MSBs */
64 { 0x1F, 0x00 }, /* DAI Clock Divider Numerator LSBs */
65 { 0x20, 0x50 }, /* Format */
66 { 0x21, 0x00 }, /* TDM Slot Select */
67 { 0x22, 0x00 }, /* DOUT Configuration VMON */
68 { 0x23, 0x00 }, /* DOUT Configuration IMON */
69 { 0x24, 0x00 }, /* DOUT Configuration VBAT */
70 { 0x25, 0x00 }, /* DOUT Configuration VBST */
71 { 0x26, 0x00 }, /* DOUT Configuration FLAG */
72 { 0x27, 0xFF }, /* DOUT HiZ Configuration 1 */
73 { 0x28, 0xFF }, /* DOUT HiZ Configuration 2 */
74 { 0x29, 0xFF }, /* DOUT HiZ Configuration 3 */
75 { 0x2A, 0xFF }, /* DOUT HiZ Configuration 4 */
76 { 0x2B, 0x02 }, /* DOUT Drive Strength */
77 { 0x2C, 0x90 }, /* Filters */
78 { 0x2D, 0x00 }, /* Gain */
79 { 0x2E, 0x02 }, /* Gain Ramping */
80 { 0x2F, 0x00 }, /* Speaker Amplifier */
81 { 0x30, 0x0A }, /* Threshold */
82 { 0x31, 0x00 }, /* ALC Attack */
83 { 0x32, 0x80 }, /* ALC Atten and Release */
84 { 0x33, 0x00 }, /* ALC Infinite Hold Release */
85 { 0x34, 0x92 }, /* ALC Configuration */
86 { 0x35, 0x01 }, /* Boost Converter */
87 { 0x36, 0x00 }, /* Block Enable */
88 { 0x37, 0x00 }, /* Configuration */
89 { 0x38, 0x00 }, /* Global Enable */
90 { 0x3A, 0x00 }, /* Boost Limiter */
93 static const struct soc_enum max98926_voltage_enum
[] = {
94 SOC_ENUM_SINGLE(MAX98926_DAI_CLK_DIV_N_LSBS
, 0,
95 ARRAY_SIZE(max98926_pdm_ch_text
),
96 max98926_pdm_ch_text
),
99 static const struct snd_kcontrol_new max98926_voltage_control
=
100 SOC_DAPM_ENUM("Route", max98926_voltage_enum
);
102 static const struct soc_enum max98926_current_enum
[] = {
103 SOC_ENUM_SINGLE(MAX98926_DAI_CLK_DIV_N_LSBS
,
104 MAX98926_PDM_SOURCE_1_SHIFT
,
105 ARRAY_SIZE(max98926_pdm_ch_text
),
106 max98926_pdm_ch_text
),
109 static const struct snd_kcontrol_new max98926_current_control
=
110 SOC_DAPM_ENUM("Route", max98926_current_enum
);
112 static const struct snd_kcontrol_new max98926_mixer_controls
[] = {
113 SOC_DAPM_SINGLE("PCM Single Switch", MAX98926_SPK_AMP
,
114 MAX98926_INSELECT_MODE_SHIFT
, 0, 0),
115 SOC_DAPM_SINGLE("PDM Single Switch", MAX98926_SPK_AMP
,
116 MAX98926_INSELECT_MODE_SHIFT
, 1, 0),
119 static const struct snd_kcontrol_new max98926_dai_controls
[] = {
120 SOC_DAPM_SINGLE("Left", MAX98926_GAIN
,
121 MAX98926_DAC_IN_SEL_SHIFT
, 0, 0),
122 SOC_DAPM_SINGLE("Right", MAX98926_GAIN
,
123 MAX98926_DAC_IN_SEL_SHIFT
, 1, 0),
124 SOC_DAPM_SINGLE("LeftRight", MAX98926_GAIN
,
125 MAX98926_DAC_IN_SEL_SHIFT
, 2, 0),
126 SOC_DAPM_SINGLE("(Left+Right)/2 Switch", MAX98926_GAIN
,
127 MAX98926_DAC_IN_SEL_SHIFT
, 3, 0),
130 static const struct snd_soc_dapm_widget max98926_dapm_widgets
[] = {
131 SND_SOC_DAPM_AIF_IN("DAI_OUT", "HiFi Playback", 0,
133 SND_SOC_DAPM_DAC("Amp Enable", NULL
, MAX98926_BLOCK_ENABLE
,
134 MAX98926_SPK_EN_SHIFT
, 0),
135 SND_SOC_DAPM_SUPPLY("Global Enable", MAX98926_GLOBAL_ENABLE
,
136 MAX98926_EN_SHIFT
, 0, NULL
, 0),
137 SND_SOC_DAPM_SUPPLY("VI Enable", MAX98926_BLOCK_ENABLE
,
138 MAX98926_ADC_IMON_EN_WIDTH
|
139 MAX98926_ADC_VMON_EN_SHIFT
,
141 SND_SOC_DAPM_PGA("BST Enable", MAX98926_BLOCK_ENABLE
,
142 MAX98926_BST_EN_SHIFT
, 0, NULL
, 0),
143 SND_SOC_DAPM_OUTPUT("BE_OUT"),
144 SND_SOC_DAPM_MIXER("PCM Sel", MAX98926_SPK_AMP
,
145 MAX98926_INSELECT_MODE_SHIFT
, 0,
146 &max98926_mixer_controls
[0],
147 ARRAY_SIZE(max98926_mixer_controls
)),
148 SND_SOC_DAPM_MIXER("DAI Sel",
149 MAX98926_GAIN
, MAX98926_DAC_IN_SEL_SHIFT
, 0,
150 &max98926_dai_controls
[0],
151 ARRAY_SIZE(max98926_dai_controls
)),
152 SND_SOC_DAPM_MUX("PDM CH1 Source",
153 MAX98926_DAI_CLK_DIV_N_LSBS
,
154 MAX98926_PDM_CURRENT_SHIFT
,
155 0, &max98926_current_control
),
156 SND_SOC_DAPM_MUX("PDM CH0 Source",
157 MAX98926_DAI_CLK_DIV_N_LSBS
,
158 MAX98926_PDM_VOLTAGE_SHIFT
,
159 0, &max98926_voltage_control
),
162 static const struct snd_soc_dapm_route max98926_audio_map
[] = {
163 {"VI Enable", NULL
, "DAI_OUT"},
164 {"DAI Sel", "Left", "VI Enable"},
165 {"DAI Sel", "Right", "VI Enable"},
166 {"DAI Sel", "LeftRight", "VI Enable"},
167 {"DAI Sel", "LeftRightDiv2", "VI Enable"},
168 {"PCM Sel", "PCM", "DAI Sel"},
170 {"PDM CH1 Source", "Current", "DAI_OUT"},
171 {"PDM CH1 Source", "Voltage", "DAI_OUT"},
172 {"PDM CH0 Source", "Current", "DAI_OUT"},
173 {"PDM CH0 Source", "Voltage", "DAI_OUT"},
174 {"PCM Sel", "Analog", "PDM CH1 Source"},
175 {"PCM Sel", "Analog", "PDM CH0 Source"},
176 {"Amp Enable", NULL
, "PCM Sel"},
178 {"BST Enable", NULL
, "Amp Enable"},
179 {"BE_OUT", NULL
, "BST Enable"},
182 static bool max98926_volatile_register(struct device
*dev
, unsigned int reg
)
185 case MAX98926_VBAT_DATA
:
186 case MAX98926_VBST_DATA
:
187 case MAX98926_LIVE_STATUS0
:
188 case MAX98926_LIVE_STATUS1
:
189 case MAX98926_LIVE_STATUS2
:
190 case MAX98926_STATE0
:
191 case MAX98926_STATE1
:
192 case MAX98926_STATE2
:
196 case MAX98926_VERSION
:
203 static bool max98926_readable_register(struct device
*dev
, unsigned int reg
)
206 case MAX98926_IRQ_CLEAR0
:
207 case MAX98926_IRQ_CLEAR1
:
208 case MAX98926_IRQ_CLEAR2
:
209 case MAX98926_ALC_HOLD_RLS
:
216 static DECLARE_TLV_DB_SCALE(max98926_spk_tlv
, -600, 100, 0);
217 static DECLARE_TLV_DB_RANGE(max98926_current_tlv
,
218 0, 11, TLV_DB_SCALE_ITEM(20, 20, 0),
219 12, 15, TLV_DB_SCALE_ITEM(320, 40, 0),
222 static SOC_ENUM_SINGLE_DECL(max98926_dac_hpf_cutoff
,
223 MAX98926_FILTERS
, MAX98926_DAC_HPF_SHIFT
,
224 max98926_hpf_cutoff_txt
);
226 static SOC_ENUM_SINGLE_DECL(max98926_boost_voltage
,
227 MAX98926_CONFIGURATION
, MAX98926_BST_VOUT_SHIFT
,
228 max98926_boost_voltage_txt
);
230 static const struct snd_kcontrol_new max98926_snd_controls
[] = {
231 SOC_SINGLE_TLV("Speaker Volume", MAX98926_GAIN
,
232 MAX98926_SPK_GAIN_SHIFT
,
233 (1<<MAX98926_SPK_GAIN_WIDTH
)-1, 0,
235 SOC_SINGLE("Ramp Switch", MAX98926_GAIN_RAMPING
,
236 MAX98926_SPK_RMP_EN_SHIFT
, 1, 0),
237 SOC_SINGLE("ZCD Switch", MAX98926_GAIN_RAMPING
,
238 MAX98926_SPK_ZCD_EN_SHIFT
, 1, 0),
239 SOC_SINGLE("ALC Switch", MAX98926_THRESHOLD
,
240 MAX98926_ALC_EN_SHIFT
, 1, 0),
241 SOC_SINGLE("ALC Threshold", MAX98926_THRESHOLD
,
242 MAX98926_ALC_TH_SHIFT
,
243 (1<<MAX98926_ALC_TH_WIDTH
)-1, 0),
244 SOC_ENUM("Boost Output Voltage", max98926_boost_voltage
),
245 SOC_SINGLE_TLV("Boost Current Limit", MAX98926_BOOST_LIMITER
,
246 MAX98926_BST_ILIM_SHIFT
,
247 (1<<MAX98926_BST_ILIM_SHIFT
)-1, 0,
248 max98926_current_tlv
),
249 SOC_ENUM("DAC HPF Cutoff", max98926_dac_hpf_cutoff
),
250 SOC_DOUBLE("PDM Channel One", MAX98926_DAI_CLK_DIV_N_LSBS
,
251 MAX98926_PDM_CHANNEL_1_SHIFT
,
252 MAX98926_PDM_CHANNEL_1_HIZ
, 1, 0),
253 SOC_DOUBLE("PDM Channel Zero", MAX98926_DAI_CLK_DIV_N_LSBS
,
254 MAX98926_PDM_CHANNEL_0_SHIFT
,
255 MAX98926_PDM_CHANNEL_0_HIZ
, 1, 0),
258 static const struct {
300 static void max98926_set_sense_data(struct max98926_priv
*max98926
)
302 regmap_update_bits(max98926
->regmap
,
303 MAX98926_DOUT_CFG_VMON
,
304 MAX98926_DAI_VMON_EN_MASK
,
305 MAX98926_DAI_VMON_EN_MASK
);
306 regmap_update_bits(max98926
->regmap
,
307 MAX98926_DOUT_CFG_IMON
,
308 MAX98926_DAI_IMON_EN_MASK
,
309 MAX98926_DAI_IMON_EN_MASK
);
311 if (!max98926
->interleave_mode
) {
313 regmap_update_bits(max98926
->regmap
,
314 MAX98926_DOUT_CFG_VMON
,
315 MAX98926_DAI_VMON_SLOT_MASK
,
318 regmap_update_bits(max98926
->regmap
,
319 MAX98926_DOUT_CFG_IMON
,
320 MAX98926_DAI_IMON_SLOT_MASK
,
323 /* enable interleave mode */
324 regmap_update_bits(max98926
->regmap
,
326 MAX98926_DAI_INTERLEAVE_MASK
,
327 MAX98926_DAI_INTERLEAVE_MASK
);
328 /* set interleave slots */
329 regmap_update_bits(max98926
->regmap
,
330 MAX98926_DOUT_CFG_VBAT
,
331 MAX98926_DAI_INTERLEAVE_SLOT_MASK
,
336 static int max98926_dai_set_fmt(struct snd_soc_dai
*codec_dai
,
339 struct snd_soc_component
*component
= codec_dai
->component
;
340 struct max98926_priv
*max98926
= snd_soc_component_get_drvdata(component
);
341 unsigned int invert
= 0;
343 dev_dbg(component
->dev
, "%s: fmt 0x%08X\n", __func__
, fmt
);
345 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
346 case SND_SOC_DAIFMT_CBS_CFS
:
347 max98926_set_sense_data(max98926
);
350 dev_err(component
->dev
, "DAI clock mode unsupported\n");
354 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
355 case SND_SOC_DAIFMT_NB_NF
:
357 case SND_SOC_DAIFMT_NB_IF
:
358 invert
= MAX98926_DAI_WCI_MASK
;
360 case SND_SOC_DAIFMT_IB_NF
:
361 invert
= MAX98926_DAI_BCI_MASK
;
363 case SND_SOC_DAIFMT_IB_IF
:
364 invert
= MAX98926_DAI_BCI_MASK
| MAX98926_DAI_WCI_MASK
;
367 dev_err(component
->dev
, "DAI invert mode unsupported\n");
371 regmap_write(max98926
->regmap
,
372 MAX98926_FORMAT
, MAX98926_DAI_DLY_MASK
);
373 regmap_update_bits(max98926
->regmap
, MAX98926_FORMAT
,
374 MAX98926_DAI_BCI_MASK
, invert
);
378 static int max98926_dai_hw_params(struct snd_pcm_substream
*substream
,
379 struct snd_pcm_hw_params
*params
,
380 struct snd_soc_dai
*dai
)
382 int dai_sr
= -EINVAL
;
383 int rate
= params_rate(params
), i
;
384 struct snd_soc_component
*component
= dai
->component
;
385 struct max98926_priv
*max98926
= snd_soc_component_get_drvdata(component
);
388 switch (params_format(params
)) {
389 case SNDRV_PCM_FORMAT_S16_LE
:
390 regmap_update_bits(max98926
->regmap
,
392 MAX98926_DAI_CHANSZ_MASK
,
393 MAX98926_DAI_CHANSZ_16
);
394 max98926
->ch_size
= 16;
396 case SNDRV_PCM_FORMAT_S24_LE
:
397 regmap_update_bits(max98926
->regmap
,
399 MAX98926_DAI_CHANSZ_MASK
,
400 MAX98926_DAI_CHANSZ_24
);
401 max98926
->ch_size
= 24;
403 case SNDRV_PCM_FORMAT_S32_LE
:
404 regmap_update_bits(max98926
->regmap
,
406 MAX98926_DAI_CHANSZ_MASK
,
407 MAX98926_DAI_CHANSZ_32
);
408 max98926
->ch_size
= 32;
411 dev_dbg(component
->dev
, "format unsupported %d\n",
412 params_format(params
));
416 /* BCLK/LRCLK ratio calculation */
417 blr_clk_ratio
= params_channels(params
) * max98926
->ch_size
;
419 switch (blr_clk_ratio
) {
421 regmap_update_bits(max98926
->regmap
,
422 MAX98926_DAI_CLK_MODE2
,
423 MAX98926_DAI_BSEL_MASK
,
424 MAX98926_DAI_BSEL_32
);
427 regmap_update_bits(max98926
->regmap
,
428 MAX98926_DAI_CLK_MODE2
,
429 MAX98926_DAI_BSEL_MASK
,
430 MAX98926_DAI_BSEL_48
);
433 regmap_update_bits(max98926
->regmap
,
434 MAX98926_DAI_CLK_MODE2
,
435 MAX98926_DAI_BSEL_MASK
,
436 MAX98926_DAI_BSEL_64
);
442 /* find the closest rate */
443 for (i
= 0; i
< ARRAY_SIZE(rate_table
); i
++) {
444 if (rate_table
[i
].rate
>= rate
) {
445 dai_sr
= rate_table
[i
].sr
;
452 /* set DAI_SR to correct LRCLK frequency */
453 regmap_update_bits(max98926
->regmap
,
454 MAX98926_DAI_CLK_MODE2
,
455 MAX98926_DAI_SR_MASK
, dai_sr
<< MAX98926_DAI_SR_SHIFT
);
459 #define MAX98926_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
460 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
462 static const struct snd_soc_dai_ops max98926_dai_ops
= {
463 .set_fmt
= max98926_dai_set_fmt
,
464 .hw_params
= max98926_dai_hw_params
,
467 static struct snd_soc_dai_driver max98926_dai
[] = {
469 .name
= "max98926-aif1",
471 .stream_name
= "HiFi Playback",
474 .rates
= SNDRV_PCM_RATE_8000_48000
,
475 .formats
= MAX98926_FORMATS
,
478 .stream_name
= "HiFi Capture",
481 .rates
= SNDRV_PCM_RATE_8000_48000
,
482 .formats
= MAX98926_FORMATS
,
484 .ops
= &max98926_dai_ops
,
488 static int max98926_probe(struct snd_soc_component
*component
)
490 struct max98926_priv
*max98926
= snd_soc_component_get_drvdata(component
);
492 max98926
->component
= component
;
494 /* Hi-Z all the slots */
495 regmap_write(max98926
->regmap
, MAX98926_DOUT_HIZ_CFG4
, 0xF0);
499 static const struct snd_soc_component_driver soc_component_dev_max98926
= {
500 .probe
= max98926_probe
,
501 .controls
= max98926_snd_controls
,
502 .num_controls
= ARRAY_SIZE(max98926_snd_controls
),
503 .dapm_routes
= max98926_audio_map
,
504 .num_dapm_routes
= ARRAY_SIZE(max98926_audio_map
),
505 .dapm_widgets
= max98926_dapm_widgets
,
506 .num_dapm_widgets
= ARRAY_SIZE(max98926_dapm_widgets
),
508 .use_pmdown_time
= 1,
510 .non_legacy_dai_naming
= 1,
513 static const struct regmap_config max98926_regmap
= {
516 .max_register
= MAX98926_VERSION
,
517 .reg_defaults
= max98926_reg
,
518 .num_reg_defaults
= ARRAY_SIZE(max98926_reg
),
519 .volatile_reg
= max98926_volatile_register
,
520 .readable_reg
= max98926_readable_register
,
521 .cache_type
= REGCACHE_RBTREE
,
524 static int max98926_i2c_probe(struct i2c_client
*i2c
,
525 const struct i2c_device_id
*id
)
529 struct max98926_priv
*max98926
;
531 max98926
= devm_kzalloc(&i2c
->dev
,
532 sizeof(*max98926
), GFP_KERNEL
);
536 i2c_set_clientdata(i2c
, max98926
);
537 max98926
->regmap
= devm_regmap_init_i2c(i2c
, &max98926_regmap
);
538 if (IS_ERR(max98926
->regmap
)) {
539 ret
= PTR_ERR(max98926
->regmap
);
541 "Failed to allocate regmap: %d\n", ret
);
544 if (of_property_read_bool(i2c
->dev
.of_node
, "interleave-mode"))
545 max98926
->interleave_mode
= true;
547 if (!of_property_read_u32(i2c
->dev
.of_node
, "vmon-slot-no", &value
)) {
548 if (value
> MAX98926_DAI_VMON_SLOT_1E_1F
) {
549 dev_err(&i2c
->dev
, "vmon slot number is wrong:\n");
552 max98926
->v_slot
= value
;
554 if (!of_property_read_u32(i2c
->dev
.of_node
, "imon-slot-no", &value
)) {
555 if (value
> MAX98926_DAI_IMON_SLOT_1E_1F
) {
556 dev_err(&i2c
->dev
, "imon slot number is wrong:\n");
559 max98926
->i_slot
= value
;
561 ret
= regmap_read(max98926
->regmap
,
562 MAX98926_VERSION
, ®
);
564 dev_err(&i2c
->dev
, "Failed to read: %x\n", reg
);
568 ret
= devm_snd_soc_register_component(&i2c
->dev
,
569 &soc_component_dev_max98926
,
570 max98926_dai
, ARRAY_SIZE(max98926_dai
));
573 "Failed to register component: %d\n", ret
);
574 dev_info(&i2c
->dev
, "device version: %x\n", reg
);
579 static const struct i2c_device_id max98926_i2c_id
[] = {
583 MODULE_DEVICE_TABLE(i2c
, max98926_i2c_id
);
585 static const struct of_device_id max98926_of_match
[] = {
586 { .compatible
= "maxim,max98926", },
589 MODULE_DEVICE_TABLE(of
, max98926_of_match
);
591 static struct i2c_driver max98926_i2c_driver
= {
594 .of_match_table
= of_match_ptr(max98926_of_match
),
597 .probe
= max98926_i2c_probe
,
598 .id_table
= max98926_i2c_id
,
601 module_i2c_driver(max98926_i2c_driver
)
602 MODULE_DESCRIPTION("ALSA SoC MAX98926 driver");
603 MODULE_AUTHOR("Anish kumar <anish.kumar@maximintegrated.com>");
604 MODULE_LICENSE("GPL");