1 // SPDX-License-Identifier: GPL-2.0
3 // MAX9867 ALSA SoC codec driver
5 // Copyright 2013-2015 Maxim Integrated Products
6 // Copyright 2018 Ladislav Michl <ladis@linux-mips.org>
9 #include <linux/delay.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15 #include <sound/tlv.h>
18 static const char *const max9867_spmode
[] = {
19 "Stereo Diff", "Mono Diff",
20 "Stereo Cap", "Mono Cap",
21 "Stereo Single", "Mono Single",
22 "Stereo Single Fast", "Mono Single Fast"
24 static const char *const max9867_filter_text
[] = {"IIR", "FIR"};
26 static SOC_ENUM_SINGLE_DECL(max9867_filter
, MAX9867_CODECFLTR
, 7,
28 static SOC_ENUM_SINGLE_DECL(max9867_spkmode
, MAX9867_MODECONFIG
, 0,
30 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(max9867_master_tlv
,
31 0, 2, TLV_DB_SCALE_ITEM(-8600, 200, 1),
32 3, 17, TLV_DB_SCALE_ITEM(-7800, 400, 0),
33 18, 25, TLV_DB_SCALE_ITEM(-2000, 200, 0),
34 26, 34, TLV_DB_SCALE_ITEM( -500, 100, 0),
35 35, 40, TLV_DB_SCALE_ITEM( 350, 50, 0),
37 static DECLARE_TLV_DB_SCALE(max9867_mic_tlv
, 0, 100, 0);
38 static DECLARE_TLV_DB_SCALE(max9867_line_tlv
, -600, 200, 0);
39 static DECLARE_TLV_DB_SCALE(max9867_adc_tlv
, -1200, 100, 0);
40 static DECLARE_TLV_DB_SCALE(max9867_dac_tlv
, -1500, 100, 0);
41 static DECLARE_TLV_DB_SCALE(max9867_dacboost_tlv
, 0, 600, 0);
42 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(max9867_micboost_tlv
,
43 0, 2, TLV_DB_SCALE_ITEM(-2000, 2000, 1),
44 3, 3, TLV_DB_SCALE_ITEM(3000, 0, 0),
47 static const struct snd_kcontrol_new max9867_snd_controls
[] = {
48 SOC_DOUBLE_R_TLV("Master Playback Volume", MAX9867_LEFTVOL
,
49 MAX9867_RIGHTVOL
, 0, 41, 1, max9867_master_tlv
),
50 SOC_DOUBLE_R_TLV("Line Capture Volume", MAX9867_LEFTLINELVL
,
51 MAX9867_RIGHTLINELVL
, 0, 15, 1, max9867_line_tlv
),
52 SOC_DOUBLE_R_TLV("Mic Capture Volume", MAX9867_LEFTMICGAIN
,
53 MAX9867_RIGHTMICGAIN
, 0, 20, 1, max9867_mic_tlv
),
54 SOC_DOUBLE_R_TLV("Mic Boost Capture Volume", MAX9867_LEFTMICGAIN
,
55 MAX9867_RIGHTMICGAIN
, 5, 4, 0, max9867_micboost_tlv
),
56 SOC_SINGLE("Digital Sidetone Volume", MAX9867_SIDETONE
, 0, 31, 1),
57 SOC_SINGLE_TLV("Digital Playback Volume", MAX9867_DACLEVEL
, 0, 15, 1,
59 SOC_SINGLE_TLV("Digital Boost Playback Volume", MAX9867_DACLEVEL
, 4, 3, 0,
60 max9867_dacboost_tlv
),
61 SOC_DOUBLE_TLV("Digital Capture Volume", MAX9867_ADCLEVEL
, 0, 4, 15, 1,
63 SOC_ENUM("Speaker Mode", max9867_spkmode
),
64 SOC_SINGLE("Volume Smoothing Switch", MAX9867_MODECONFIG
, 6, 1, 0),
65 SOC_SINGLE("Line ZC Switch", MAX9867_MODECONFIG
, 5, 1, 0),
66 SOC_ENUM("DSP Filter", max9867_filter
),
70 static const struct snd_kcontrol_new max9867_input_mixer_controls
[] = {
71 SOC_DAPM_DOUBLE("Line Capture Switch", MAX9867_INPUTCONFIG
, 7, 5, 1, 0),
72 SOC_DAPM_DOUBLE("Mic Capture Switch", MAX9867_INPUTCONFIG
, 6, 4, 1, 0),
76 static const struct snd_kcontrol_new max9867_output_mixer_controls
[] = {
77 SOC_DAPM_DOUBLE_R("Line Bypass Switch",
78 MAX9867_LEFTLINELVL
, MAX9867_RIGHTLINELVL
, 6, 1, 1),
82 static const struct snd_kcontrol_new max9867_sidetone_mixer_controls
[] = {
83 SOC_DAPM_DOUBLE("Sidetone Switch", MAX9867_SIDETONE
, 6, 7, 1, 0),
87 static const struct snd_kcontrol_new max9867_line_out_control
=
88 SOC_DAPM_DOUBLE_R("Switch",
89 MAX9867_LEFTVOL
, MAX9867_RIGHTVOL
, 6, 1, 1);
92 static const struct snd_soc_dapm_widget max9867_dapm_widgets
[] = {
93 SND_SOC_DAPM_INPUT("MICL"),
94 SND_SOC_DAPM_INPUT("MICR"),
95 SND_SOC_DAPM_INPUT("LINL"),
96 SND_SOC_DAPM_INPUT("LINR"),
98 SND_SOC_DAPM_PGA("Left Line Input", MAX9867_PWRMAN
, 6, 0, NULL
, 0),
99 SND_SOC_DAPM_PGA("Right Line Input", MAX9867_PWRMAN
, 5, 0, NULL
, 0),
100 SND_SOC_DAPM_MIXER_NAMED_CTL("Input Mixer", SND_SOC_NOPM
, 0, 0,
101 max9867_input_mixer_controls
,
102 ARRAY_SIZE(max9867_input_mixer_controls
)),
103 SND_SOC_DAPM_ADC("ADCL", "HiFi Capture", MAX9867_PWRMAN
, 1, 0),
104 SND_SOC_DAPM_ADC("ADCR", "HiFi Capture", MAX9867_PWRMAN
, 0, 0),
106 SND_SOC_DAPM_MIXER("Digital", SND_SOC_NOPM
, 0, 0,
107 max9867_sidetone_mixer_controls
,
108 ARRAY_SIZE(max9867_sidetone_mixer_controls
)),
109 SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", SND_SOC_NOPM
, 0, 0,
110 max9867_output_mixer_controls
,
111 ARRAY_SIZE(max9867_output_mixer_controls
)),
112 SND_SOC_DAPM_DAC("DACL", "HiFi Playback", MAX9867_PWRMAN
, 3, 0),
113 SND_SOC_DAPM_DAC("DACR", "HiFi Playback", MAX9867_PWRMAN
, 2, 0),
114 SND_SOC_DAPM_SWITCH("Master Playback", SND_SOC_NOPM
, 0, 0,
115 &max9867_line_out_control
),
116 SND_SOC_DAPM_OUTPUT("LOUT"),
117 SND_SOC_DAPM_OUTPUT("ROUT"),
120 static const struct snd_soc_dapm_route max9867_audio_map
[] = {
121 {"Left Line Input", NULL
, "LINL"},
122 {"Right Line Input", NULL
, "LINR"},
123 {"Input Mixer", "Mic Capture Switch", "MICL"},
124 {"Input Mixer", "Mic Capture Switch", "MICR"},
125 {"Input Mixer", "Line Capture Switch", "Left Line Input"},
126 {"Input Mixer", "Line Capture Switch", "Right Line Input"},
127 {"ADCL", NULL
, "Input Mixer"},
128 {"ADCR", NULL
, "Input Mixer"},
130 {"Digital", "Sidetone Switch", "ADCL"},
131 {"Digital", "Sidetone Switch", "ADCR"},
132 {"DACL", NULL
, "Digital"},
133 {"DACR", NULL
, "Digital"},
135 {"Output Mixer", "Line Bypass Switch", "Left Line Input"},
136 {"Output Mixer", "Line Bypass Switch", "Right Line Input"},
137 {"Output Mixer", NULL
, "DACL"},
138 {"Output Mixer", NULL
, "DACR"},
139 {"Master Playback", "Switch", "Output Mixer"},
140 {"LOUT", NULL
, "Master Playback"},
141 {"ROUT", NULL
, "Master Playback"},
144 static const unsigned int max9867_rates_44k1
[] = {
148 static const struct snd_pcm_hw_constraint_list max9867_constraints_44k1
= {
149 .list
= max9867_rates_44k1
,
150 .count
= ARRAY_SIZE(max9867_rates_44k1
),
153 static const unsigned int max9867_rates_48k
[] = {
154 8000, 16000, 32000, 48000,
157 static const struct snd_pcm_hw_constraint_list max9867_constraints_48k
= {
158 .list
= max9867_rates_48k
,
159 .count
= ARRAY_SIZE(max9867_rates_48k
),
162 struct max9867_priv
{
163 struct regmap
*regmap
;
164 const struct snd_pcm_hw_constraint_list
*constraints
;
165 unsigned int sysclk
, pclk
;
169 static int max9867_startup(struct snd_pcm_substream
*substream
,
170 struct snd_soc_dai
*dai
)
172 struct max9867_priv
*max9867
=
173 snd_soc_component_get_drvdata(dai
->component
);
175 if (max9867
->constraints
)
176 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
177 SNDRV_PCM_HW_PARAM_RATE
, max9867
->constraints
);
182 static int max9867_dai_hw_params(struct snd_pcm_substream
*substream
,
183 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
186 unsigned long int rate
, ratio
;
187 struct snd_soc_component
*component
= dai
->component
;
188 struct max9867_priv
*max9867
= snd_soc_component_get_drvdata(component
);
189 unsigned int ni
= DIV_ROUND_CLOSEST_ULL(96ULL * 0x10000 * params_rate(params
),
192 /* set up the ni value */
193 regmap_update_bits(max9867
->regmap
, MAX9867_AUDIOCLKHIGH
,
194 MAX9867_NI_HIGH_MASK
, (0xFF00 & ni
) >> 8);
195 regmap_update_bits(max9867
->regmap
, MAX9867_AUDIOCLKLOW
,
196 MAX9867_NI_LOW_MASK
, 0x00FF & ni
);
197 if (max9867
->master
) {
198 if (max9867
->dsp_a
) {
199 value
= MAX9867_IFC1B_48X
;
201 rate
= params_rate(params
) * 2 * params_width(params
);
202 ratio
= max9867
->pclk
/ rate
;
203 switch (params_width(params
)) {
208 value
= MAX9867_IFC1B_PCLK_2
;
211 value
= MAX9867_IFC1B_PCLK_4
;
214 value
= MAX9867_IFC1B_PCLK_8
;
217 value
= MAX9867_IFC1B_PCLK_16
;
224 value
= MAX9867_IFC1B_48X
;
227 value
= MAX9867_IFC1B_64X
;
233 regmap_update_bits(max9867
->regmap
, MAX9867_IFC1B
,
234 MAX9867_IFC1B_BCLK_MASK
, value
);
237 * digital pll locks on to any externally supplied LRCLK signal
238 * and also enable rapid lock mode.
240 regmap_update_bits(max9867
->regmap
, MAX9867_AUDIOCLKLOW
,
241 MAX9867_RAPID_LOCK
, MAX9867_RAPID_LOCK
);
242 regmap_update_bits(max9867
->regmap
, MAX9867_AUDIOCLKHIGH
,
243 MAX9867_PLL
, MAX9867_PLL
);
248 static int max9867_mute(struct snd_soc_dai
*dai
, int mute
)
250 struct snd_soc_component
*component
= dai
->component
;
251 struct max9867_priv
*max9867
= snd_soc_component_get_drvdata(component
);
253 return regmap_update_bits(max9867
->regmap
, MAX9867_DACLEVEL
,
254 1 << 6, !!mute
<< 6);
257 static int max9867_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
258 int clk_id
, unsigned int freq
, int dir
)
260 struct snd_soc_component
*component
= codec_dai
->component
;
261 struct max9867_priv
*max9867
= snd_soc_component_get_drvdata(component
);
264 /* Set the prescaler based on the master clock frequency*/
265 if (freq
>= 10000000 && freq
<= 20000000) {
266 value
|= MAX9867_PSCLK_10_20
;
267 max9867
->pclk
= freq
;
268 } else if (freq
>= 20000000 && freq
<= 40000000) {
269 value
|= MAX9867_PSCLK_20_40
;
270 max9867
->pclk
= freq
/ 2;
271 } else if (freq
>= 40000000 && freq
<= 60000000) {
272 value
|= MAX9867_PSCLK_40_60
;
273 max9867
->pclk
= freq
/ 4;
275 dev_err(component
->dev
,
276 "Invalid clock frequency %uHz (required 10-60MHz)\n",
280 if (freq
% 48000 == 0)
281 max9867
->constraints
= &max9867_constraints_48k
;
282 else if (freq
% 44100 == 0)
283 max9867
->constraints
= &max9867_constraints_44k1
;
285 dev_warn(component
->dev
,
286 "Unable to set exact rate with %uHz clock frequency\n",
288 max9867
->sysclk
= freq
;
289 value
= value
<< MAX9867_PSCLK_SHIFT
;
290 /* exact integer mode is not supported */
291 value
&= ~MAX9867_FREQ_MASK
;
292 regmap_update_bits(max9867
->regmap
, MAX9867_SYSCLK
,
293 MAX9867_PSCLK_MASK
, value
);
297 static int max9867_dai_set_fmt(struct snd_soc_dai
*codec_dai
,
300 struct snd_soc_component
*component
= codec_dai
->component
;
301 struct max9867_priv
*max9867
= snd_soc_component_get_drvdata(component
);
304 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
305 case SND_SOC_DAIFMT_CBM_CFM
:
306 max9867
->master
= true;
307 iface1A
= MAX9867_MASTER
;
308 iface1B
= MAX9867_IFC1B_48X
;
310 case SND_SOC_DAIFMT_CBS_CFS
:
311 max9867
->master
= false;
312 iface1A
= iface1B
= 0;
318 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
319 case SND_SOC_DAIFMT_I2S
:
320 max9867
->dsp_a
= false;
321 iface1A
|= MAX9867_I2S_DLY
;
323 case SND_SOC_DAIFMT_DSP_A
:
324 max9867
->dsp_a
= true;
325 iface1A
|= MAX9867_TDM_MODE
| MAX9867_SDOUT_HIZ
;
331 /* Clock inversion bits, BCI and WCI */
332 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
333 case SND_SOC_DAIFMT_NB_NF
:
335 case SND_SOC_DAIFMT_IB_IF
:
336 iface1A
|= MAX9867_WCI_MODE
| MAX9867_BCI_MODE
;
338 case SND_SOC_DAIFMT_IB_NF
:
339 iface1A
|= MAX9867_BCI_MODE
;
341 case SND_SOC_DAIFMT_NB_IF
:
342 iface1A
|= MAX9867_WCI_MODE
;
348 regmap_write(max9867
->regmap
, MAX9867_IFC1A
, iface1A
);
349 regmap_write(max9867
->regmap
, MAX9867_IFC1B
, iface1B
);
354 static const struct snd_soc_dai_ops max9867_dai_ops
= {
355 .set_sysclk
= max9867_set_dai_sysclk
,
356 .set_fmt
= max9867_dai_set_fmt
,
357 .digital_mute
= max9867_mute
,
358 .startup
= max9867_startup
,
359 .hw_params
= max9867_dai_hw_params
,
362 static struct snd_soc_dai_driver max9867_dai
[] = {
364 .name
= "max9867-aif1",
366 .stream_name
= "HiFi Playback",
369 .rates
= SNDRV_PCM_RATE_8000_48000
,
370 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
373 .stream_name
= "HiFi Capture",
376 .rates
= SNDRV_PCM_RATE_8000_48000
,
377 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
379 .ops
= &max9867_dai_ops
,
380 .symmetric_rates
= 1,
385 static int max9867_suspend(struct snd_soc_component
*component
)
387 snd_soc_component_force_bias_level(component
, SND_SOC_BIAS_OFF
);
392 static int max9867_resume(struct snd_soc_component
*component
)
394 snd_soc_component_force_bias_level(component
, SND_SOC_BIAS_STANDBY
);
399 #define max9867_suspend NULL
400 #define max9867_resume NULL
403 static int max9867_set_bias_level(struct snd_soc_component
*component
,
404 enum snd_soc_bias_level level
)
407 struct max9867_priv
*max9867
= snd_soc_component_get_drvdata(component
);
410 case SND_SOC_BIAS_STANDBY
:
411 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
) {
412 err
= regcache_sync(max9867
->regmap
);
416 err
= regmap_update_bits(max9867
->regmap
, MAX9867_PWRMAN
,
417 MAX9867_SHTDOWN
, MAX9867_SHTDOWN
);
422 case SND_SOC_BIAS_OFF
:
423 err
= regmap_update_bits(max9867
->regmap
, MAX9867_PWRMAN
,
428 regcache_mark_dirty(max9867
->regmap
);
437 static const struct snd_soc_component_driver max9867_component
= {
438 .controls
= max9867_snd_controls
,
439 .num_controls
= ARRAY_SIZE(max9867_snd_controls
),
440 .dapm_routes
= max9867_audio_map
,
441 .num_dapm_routes
= ARRAY_SIZE(max9867_audio_map
),
442 .dapm_widgets
= max9867_dapm_widgets
,
443 .num_dapm_widgets
= ARRAY_SIZE(max9867_dapm_widgets
),
444 .suspend
= max9867_suspend
,
445 .resume
= max9867_resume
,
446 .set_bias_level
= max9867_set_bias_level
,
448 .use_pmdown_time
= 1,
450 .non_legacy_dai_naming
= 1,
453 static bool max9867_volatile_register(struct device
*dev
, unsigned int reg
)
457 case MAX9867_JACKSTATUS
:
458 case MAX9867_AUXHIGH
:
466 static const struct reg_default max9867_reg
[] = {
489 static const struct regmap_config max9867_regmap
= {
492 .max_register
= MAX9867_REVISION
,
493 .reg_defaults
= max9867_reg
,
494 .num_reg_defaults
= ARRAY_SIZE(max9867_reg
),
495 .volatile_reg
= max9867_volatile_register
,
496 .cache_type
= REGCACHE_RBTREE
,
499 static int max9867_i2c_probe(struct i2c_client
*i2c
,
500 const struct i2c_device_id
*id
)
502 struct max9867_priv
*max9867
;
505 max9867
= devm_kzalloc(&i2c
->dev
, sizeof(*max9867
), GFP_KERNEL
);
509 i2c_set_clientdata(i2c
, max9867
);
510 max9867
->regmap
= devm_regmap_init_i2c(i2c
, &max9867_regmap
);
511 if (IS_ERR(max9867
->regmap
)) {
512 ret
= PTR_ERR(max9867
->regmap
);
513 dev_err(&i2c
->dev
, "Failed to allocate regmap: %d\n", ret
);
516 ret
= regmap_read(max9867
->regmap
, MAX9867_REVISION
, ®
);
518 dev_err(&i2c
->dev
, "Failed to read: %d\n", ret
);
521 dev_info(&i2c
->dev
, "device revision: %x\n", reg
);
522 ret
= devm_snd_soc_register_component(&i2c
->dev
, &max9867_component
,
523 max9867_dai
, ARRAY_SIZE(max9867_dai
));
525 dev_err(&i2c
->dev
, "Failed to register component: %d\n", ret
);
529 static const struct i2c_device_id max9867_i2c_id
[] = {
533 MODULE_DEVICE_TABLE(i2c
, max9867_i2c_id
);
535 static const struct of_device_id max9867_of_match
[] = {
536 { .compatible
= "maxim,max9867", },
539 MODULE_DEVICE_TABLE(of
, max9867_of_match
);
541 static struct i2c_driver max9867_i2c_driver
= {
544 .of_match_table
= of_match_ptr(max9867_of_match
),
546 .probe
= max9867_i2c_probe
,
547 .id_table
= max9867_i2c_id
,
550 module_i2c_driver(max9867_i2c_driver
);
552 MODULE_AUTHOR("Ladislav Michl <ladis@linux-mips.org>");
553 MODULE_DESCRIPTION("ASoC MAX9867 driver");
554 MODULE_LICENSE("GPL");