1 // SPDX-License-Identifier: GPL-2.0-only
3 * es8311.c -- es8311 ALSA SoC audio driver
10 #include "linux/array_size.h"
11 #include "sound/pcm.h"
12 #include <linux/clk.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <sound/core.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
19 #include <sound/tlv.h>
22 #define ES8311_NUM_RATES 10
23 #define ES8311_RATES (SNDRV_PCM_RATE_8000_96000)
24 #define ES8311_FORMATS \
25 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE | \
26 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_3LE | \
27 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
30 struct regmap *regmap;
32 unsigned long mclk_freq;
34 unsigned int rates[ES8311_NUM_RATES];
35 struct snd_pcm_hw_constraint_list constraints;
38 static const DECLARE_TLV_DB_SCALE(es8311_adc_vol_tlv, -9550, 50, 0);
39 static const DECLARE_TLV_DB_SCALE(es8311_pga_gain_tlv, 0, 300, 0);
40 static const DECLARE_TLV_DB_SCALE(es8311_adc_scale_tlv, 0, 600, 0);
42 #define ES8311_DB_LRCK_STEPS \
59 static const char *const es8311_level_winsize_txt[] = {
64 static SOC_ENUM_SINGLE_DECL(
65 es8311_alc_winsize, ES8311_ADC4,
66 ES8311_ADC4_ALC_WINSIZE_SHIFT, es8311_level_winsize_txt);
67 static const DECLARE_TLV_DB_RANGE(es8311_level_tlv,
68 0, 1, TLV_DB_SCALE_ITEM(-3010, 600, 0),
69 2, 3, TLV_DB_SCALE_ITEM(-2060, 250, 0),
70 4, 5, TLV_DB_SCALE_ITEM(-1610, 160, 0),
71 6, 7, TLV_DB_SCALE_ITEM(-1320, 120, 0),
72 8, 9, TLV_DB_SCALE_ITEM(-1100, 90, 0),
73 10, 11, TLV_DB_SCALE_ITEM(-930, 80, 0),
74 12, 15, TLV_DB_SCALE_ITEM(-780, 60, 0),
77 static const char *const es8311_ramprate_txt[] = {
81 static SOC_ENUM_SINGLE_DECL(
82 es8311_adc_ramprate, ES8311_ADC1,
83 ES8311_ADC1_RAMPRATE_SHIFT, es8311_ramprate_txt);
85 static const char *const es8311_automute_winsize_txt[] = {
103 static SOC_ENUM_SINGLE_DECL(
104 es8311_automute_winsize, ES8311_ADC6,
105 ES8311_ADC6_AUTOMUTE_WS_SHIFT, es8311_automute_winsize_txt);
106 static const DECLARE_TLV_DB_RANGE(es8311_automute_ng_tlv,
107 0, 7, TLV_DB_SCALE_ITEM(-9600, 600, 0),
108 8, 15, TLV_DB_SCALE_ITEM(-5100, 300, 0),
110 static const DECLARE_TLV_DB_SCALE(es8311_automute_vol_tlv, -2800, 400, 0);
112 static const DECLARE_TLV_DB_SCALE(es8311_dac_vol_tlv, -9550, 50, 0);
113 static SOC_ENUM_SINGLE_DECL(
114 es8311_drc_winsize, ES8311_DAC4,
115 ES8311_DAC4_DRC_WINSIZE_SHIFT, es8311_level_winsize_txt);
116 static SOC_ENUM_SINGLE_DECL(
117 es8311_dac_ramprate, ES8311_DAC6,
118 ES8311_DAC6_RAMPRATE_SHIFT, es8311_ramprate_txt);
120 static const char *const es8311_out_mode_txt[] = {
124 static SOC_ENUM_SINGLE_DECL(
125 es8311_out_mode, ES8311_SYS9,
126 ES8311_SYS9_HPSW_SHIFT, es8311_out_mode_txt);
128 static const struct snd_kcontrol_new es8311_snd_controls[] = {
130 SOC_SINGLE_TLV("PGA Capture Volume", ES8311_SYS10,
131 ES8311_SYS10_PGAGAIN_SHIFT, ES8311_SYS10_PGAGAIN_MAX, 0,
132 es8311_pga_gain_tlv),
133 SOC_SINGLE("ADC Polarity Invert Capture Switch", ES8311_ADC2,
134 ES8311_ADC2_INV_SHIFT, 1, 0),
135 SOC_SINGLE_TLV("ADC Scale Capture Volume", ES8311_ADC2,
136 ES8311_ADC2_SCALE_SHIFT, ES8311_ADC2_SCALE_MAX, 0,
137 es8311_adc_scale_tlv),
138 SOC_SINGLE_TLV("ADC Capture Volume", ES8311_ADC3,
139 ES8311_ADC3_VOLUME_SHIFT, ES8311_ADC3_VOLUME_MAX, 0,
141 SOC_ENUM("ADC Capture Ramp Rate", es8311_adc_ramprate),
142 SOC_SINGLE("ADC Automute Capture Switch", ES8311_ADC4,
143 ES8311_ADC4_AUTOMUTE_EN_SHIFT, 1, 0),
144 SOC_ENUM("ADC Automute Capture Winsize", es8311_automute_winsize),
145 SOC_SINGLE_TLV("ADC Automute Noise Gate Capture Volume", ES8311_ADC6,
146 ES8311_ADC6_AUTOMUTE_NG_SHIFT,
147 ES8311_ADC6_AUTOMUTE_NG_MAX, 0, es8311_automute_ng_tlv),
148 SOC_SINGLE_TLV("ADC Automute Capture Volume", ES8311_ADC7,
149 ES8311_ADC7_AUTOMUTE_VOL_SHIFT,
150 ES8311_ADC7_AUTOMUTE_VOL_MAX, 0,
151 es8311_automute_vol_tlv),
152 SOC_SINGLE("ADC HPF Capture Switch", ES8311_ADC8, ES8311_ADC8_HPF_SHIFT,
154 SOC_SINGLE("ADC EQ Capture Switch", ES8311_ADC8,
155 ES8311_ADC8_EQBYPASS_SHIFT, 1, 1),
156 SOC_SINGLE("ALC Capture Switch", ES8311_ADC4, ES8311_ADC4_ALC_EN_SHIFT,
158 SOC_SINGLE_TLV("ALC Capture Max Volume", ES8311_ADC5,
159 ES8311_ADC5_ALC_MAXLEVEL_SHIFT,
160 ES8311_ADC5_ALC_MAXLEVEL_MAX, 0, es8311_level_tlv),
161 SOC_SINGLE_TLV("ALC Capture Min Volume", ES8311_ADC5,
162 ES8311_ADC5_ALC_MINLEVEL_SHIFT,
163 ES8311_ADC5_ALC_MINLEVEL_MAX, 0, es8311_level_tlv),
164 SOC_ENUM("ALC Capture Winsize", es8311_alc_winsize),
167 SOC_SINGLE_TLV("DAC Playback Volume", ES8311_DAC2, 0,
168 ES8311_DAC2_VOLUME_MAX, 0, es8311_dac_vol_tlv),
169 SOC_SINGLE("DRC Playback Switch", ES8311_DAC4, ES8311_DAC4_DRC_EN_SHIFT,
171 SOC_SINGLE_TLV("DRC Playback Max Volume", ES8311_DAC5,
172 ES8311_DAC5_DRC_MAXLEVEL_SHIFT,
173 ES8311_DAC5_DRC_MAXLEVEL_MAX, 0, es8311_level_tlv),
174 SOC_SINGLE_TLV("DRC Playback Min Volume", ES8311_DAC5,
175 ES8311_DAC5_DRC_MINLEVEL_SHIFT,
176 ES8311_DAC5_DRC_MINLEVEL_MAX, 0, es8311_level_tlv),
177 SOC_ENUM("DRC Playback Winsize", es8311_drc_winsize),
178 SOC_ENUM("DAC Playback Ramp Rate", es8311_dac_ramprate),
179 SOC_SINGLE("DAC EQ Playback Switch", ES8311_DAC6,
180 ES8311_DAC6_EQBYPASS_SHIFT, 1, 1),
182 SOC_ENUM("Output Mode", es8311_out_mode),
185 static const char *const es8311_diff_src_txt[] = {
189 static SOC_ENUM_SINGLE_DECL(
190 es8311_diff_src_enum, ES8311_SYS10,
191 ES8311_SYS10_LINESEL_SHIFT, es8311_diff_src_txt);
192 static const struct snd_kcontrol_new es8311_diff_src_mux =
193 SOC_DAPM_ENUM("Differential Source", es8311_diff_src_enum);
195 static const char *const es8311_dmic_src_txt[] = {
199 static SOC_ENUM_SINGLE_DECL(
200 es8311_dmic_src_enum, ES8311_SYS10,
201 ES8311_SYS10_DMIC_ON_SHIFT, es8311_dmic_src_txt);
202 static const struct snd_kcontrol_new es8311_dmic_src_mux =
203 SOC_DAPM_ENUM("Digital Mic Source", es8311_dmic_src_enum);
205 static const char * const es8311_aif1tx_src_txt[] = {
214 static SOC_ENUM_SINGLE_DECL(
215 es8311_aif1tx_src_enum, ES8311_GPIO,
216 ES8311_GPIO_ADCDAT_SEL_SHIFT, es8311_aif1tx_src_txt);
217 static const struct snd_kcontrol_new es8311_aif1tx_src_mux =
218 SOC_DAPM_ENUM("AIF1TX Source", es8311_aif1tx_src_enum);
220 static const char * const es8311_dac_src_txt[] = {
224 static SOC_ENUM_SINGLE_DECL(
225 es8311_dac_src_enum, ES8311_SDP_IN,
226 ES8311_SDP_IN_SEL_SHIFT, es8311_dac_src_txt);
227 static const struct snd_kcontrol_new es8311_dac_src_mux =
228 SOC_DAPM_ENUM("Mono DAC Source", es8311_dac_src_enum);
230 static const struct snd_soc_dapm_widget es8311_dapm_widgets[] = {
231 SND_SOC_DAPM_SUPPLY("Bias", ES8311_SYS3, ES8311_SYS3_PDN_IBIASGEN_SHIFT,
233 SND_SOC_DAPM_SUPPLY("Analog power", ES8311_SYS3,
234 ES8311_SYS3_PDN_ANA_SHIFT, 1, NULL, 0),
235 SND_SOC_DAPM_SUPPLY("Vref", ES8311_SYS3, ES8311_SYS3_PDN_VREF_SHIFT, 1,
239 SND_SOC_DAPM_INPUT("DMIC"),
240 SND_SOC_DAPM_INPUT("MIC1"),
241 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
242 &es8311_diff_src_mux),
243 SND_SOC_DAPM_SUPPLY("ADC Bias Gen", ES8311_SYS3,
244 ES8311_SYS3_PDN_ADCBIASGEN_SHIFT, 1, NULL, 0),
245 SND_SOC_DAPM_SUPPLY("ADC Vref Gen", ES8311_SYS3,
246 ES8311_SYS3_PDN_ADCVREFGEN_SHIFT, 1, NULL, 0),
247 SND_SOC_DAPM_SUPPLY("ADC Clock", ES8311_CLKMGR1,
248 ES8311_CLKMGR1_CLKADC_ON_SHIFT, 0, NULL, 0),
249 SND_SOC_DAPM_SUPPLY("ADC Analog Clock", ES8311_CLKMGR1,
250 ES8311_CLKMGR1_ANACLKADC_ON_SHIFT, 0, NULL, 0),
251 SND_SOC_DAPM_PGA("PGA", ES8311_SYS4, ES8311_SYS4_PDN_PGA_SHIFT, 1, NULL,
253 SND_SOC_DAPM_ADC("Mono ADC", NULL, ES8311_SYS4,
254 ES8311_SYS4_PDN_MOD_SHIFT, 1),
255 SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0,
256 &es8311_dmic_src_mux),
257 SND_SOC_DAPM_MUX("AIF1TX Source Mux", SND_SOC_NOPM, 0, 0,
258 &es8311_aif1tx_src_mux),
259 SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, ES8311_SDP_OUT,
260 ES8311_SDP_MUTE_SHIFT, 1),
263 SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, ES8311_SDP_IN,
264 ES8311_SDP_MUTE_SHIFT, 1),
265 SND_SOC_DAPM_MUX("Mono DAC Source Mux", SND_SOC_NOPM, 0, 0,
266 &es8311_dac_src_mux),
267 SND_SOC_DAPM_DAC("Mono DAC", NULL, ES8311_SYS8,
268 ES8311_SYS8_PDN_DAC_SHIFT, 1),
269 SND_SOC_DAPM_SUPPLY("DAC Clock", ES8311_CLKMGR1,
270 ES8311_CLKMGR1_CLKDAC_ON_SHIFT, 0, NULL, 0),
271 SND_SOC_DAPM_SUPPLY("DAC Analog Clock", ES8311_CLKMGR1,
272 ES8311_CLKMGR1_ANACLKDAC_ON_SHIFT, 0, NULL, 0),
273 SND_SOC_DAPM_SUPPLY("DAC Vref Gen", ES8311_SYS3,
274 ES8311_SYS3_PDN_DACVREFGEN_SHIFT, 1, NULL, 0),
275 SND_SOC_DAPM_OUTPUT("OUT"),
278 static const struct snd_soc_dapm_route es8311_dapm_routes[] = {
280 { "MIC1", NULL, "Bias" },
281 { "MIC1", NULL, "Analog power" },
282 { "MIC1", NULL, "Vref" },
283 { "Differential Mux", "MIC1P-MIC1N", "MIC1" },
284 { "PGA", NULL, "Differential Mux" },
285 { "Mono ADC", NULL, "PGA" },
286 { "Mono ADC", NULL, "ADC Bias Gen" },
287 { "Mono ADC", NULL, "ADC Vref Gen" },
288 { "Mono ADC", NULL, "ADC Clock" },
289 { "Mono ADC", NULL, "ADC Analog Clock" },
290 { "Digital Mic Mux", "Disabled", "Mono ADC" },
291 { "Digital Mic Mux", "DMIC from MIC1P", "DMIC" },
293 { "AIF1TX Source Mux", "ADC + ADC", "Digital Mic Mux" },
294 { "AIF1TX Source Mux", "ADC + 0", "Digital Mic Mux" },
295 { "AIF1TX Source Mux", "0 + ADC", "Digital Mic Mux" },
296 { "AIF1TX Source Mux", "DACL + ADC", "Digital Mic Mux" },
297 { "AIF1TX Source Mux", "ADC + DACR", "Digital Mic Mux" },
299 { "AIF1TX", NULL, "AIF1TX Source Mux" },
302 { "Mono DAC Source Mux", "Left", "AIF1RX" },
303 { "Mono DAC Source Mux", "Right", "AIF1RX" },
304 { "Mono DAC", NULL, "Mono DAC Source Mux" },
305 { "Mono DAC", NULL, "DAC Clock" },
306 { "Mono DAC", NULL, "DAC Analog Clock" },
307 { "OUT", NULL, "Mono DAC" },
308 { "OUT", NULL, "Bias" },
309 { "OUT", NULL, "Analog power" },
310 { "OUT", NULL, "Vref" },
311 { "OUT", NULL, "DAC Vref Gen" },
314 /* Bit clock divider values:
315 * from 1 to 20: the register takes the div value - 1
316 * above 20: the register takes the corresponding idx of the div value
317 * in the following table + 20
319 #define ES8311_BCLK_DIV_IDX_OFFSET 20
320 static const unsigned int es8311_bclk_divs[] = {
321 22, 24, 25, 30, 32, 33, 34, 36, 44, 48, 66, 72
324 struct es8311_mclk_coeff {
329 unsigned int div_adc_dac;
332 #define ES8311_MCLK_MAX_FREQ 49200000
334 /* Coefficients for common master clock frequencies based on clock table from
335 * documentation. Limited to have a ratio of adc (or dac) clock to lrclk equal
336 * to 256. This to keep the default adc and dac oversampling and adc scale
337 * settings. Internal mclk dividers and multipliers are dynamically adjusted to
338 * support, respectively, multiples (up to x8) and factors (/2,4,8) of listed
339 * mclks frequencies (see es8311_cmp_adj_mclk_coeff).
340 * All rates are supported when mclk/rate ratio is 32, 64, 128, 256, 384 or 512
341 * (upper limit due to max mclk freq of 49.2MHz).
343 static const struct es8311_mclk_coeff es8311_mclk_coeffs[] = {
344 { 8000, 2048000, 1, 1, 1 },
345 { 8000, 6144000, 3, 1, 1 },
346 { 8000, 18432000, 3, 1, 3 },
347 { 11025, 2822400, 1, 1, 1 },
348 { 11025, 8467200, 3, 1, 1 },
349 { 16000, 4096000, 1, 1, 1 },
350 { 16000, 12288000, 3, 1, 1 },
351 { 16000, 18432000, 3, 2, 3 },
352 { 22050, 5644800, 1, 1, 1 },
353 { 22050, 16934400, 3, 1, 1 },
354 { 32000, 8192000, 1, 1, 1 },
355 { 32000, 12288000, 3, 2, 1 },
356 { 32000, 18432000, 3, 4, 3 },
357 { 44100, 11289600, 1, 1, 1 },
358 { 44100, 33868800, 3, 1, 1 },
359 { 48000, 12288000, 1, 1, 1 },
360 { 48000, 18432000, 3, 2, 1 },
361 { 64000, 8192000, 1, 2, 1 },
362 { 64000, 12288000, 3, 4, 1 },
363 { 88200, 11289600, 1, 2, 1 },
364 { 88200, 33868800, 3, 2, 1 },
365 { 96000, 12288000, 1, 2, 1 },
366 { 96000, 18432000, 3, 4, 1 },
369 /* Compare coeff with provided mclk_freq and adjust it if needed.
370 * If frequencies match, return 0 and the unaltered coeff copy into out_coeff.
371 * If mclk_freq is a valid multiple or factor of coeff mclk freq, return 0 and
372 * the adjusted coeff copy into out_coeff.
373 * Return -EINVAL otherwise.
375 static int es8311_cmp_adj_mclk_coeff(unsigned int mclk_freq,
376 const struct es8311_mclk_coeff *coeff,
377 struct es8311_mclk_coeff *out_coeff)
379 if (WARN_ON_ONCE(!coeff))
382 unsigned int div = coeff->div;
383 unsigned int mult = coeff->mult;
386 if (coeff->mclk == mclk_freq) {
388 } else if (mclk_freq % coeff->mclk == 0) {
389 div = mclk_freq / coeff->mclk;
393 } else if (coeff->mclk % mclk_freq == 0) {
394 mult = coeff->mclk / mclk_freq;
395 if (mult == 2 || mult == 4 || mult == 8) {
405 out_coeff->div = div;
406 out_coeff->mult = mult;
411 static int es8311_get_mclk_coeff(unsigned int mclk_freq, unsigned int rate,
412 struct es8311_mclk_coeff *out_coeff)
414 for (unsigned int i = 0; i < ARRAY_SIZE(es8311_mclk_coeffs); i++) {
415 const struct es8311_mclk_coeff *coeff = &es8311_mclk_coeffs[i];
417 if (coeff->rate != rate)
421 es8311_cmp_adj_mclk_coeff(mclk_freq, coeff, out_coeff);
428 static void es8311_set_sysclk_constraints(unsigned int mclk_freq,
429 struct es8311_priv *es8311)
431 unsigned int count = 0;
433 for (unsigned int i = 0; i < ARRAY_SIZE(es8311_mclk_coeffs) &&
434 count < ARRAY_SIZE(es8311->rates); i++) {
435 const struct es8311_mclk_coeff *coeff = &es8311_mclk_coeffs[i];
437 if (count > 0 && coeff->rate == es8311->rates[count - 1])
440 int ret = es8311_cmp_adj_mclk_coeff(mclk_freq, coeff, NULL);
442 es8311->rates[count++] = coeff->rate;
445 es8311->constraints.list = es8311->rates;
446 es8311->constraints.count = count;
450 static int es8311_mute(struct snd_soc_dai *dai, int mute, int direction)
452 struct snd_soc_component *component = dai->component;
453 struct es8311_priv *es8311 = snd_soc_component_get_drvdata(component);
455 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
456 unsigned int mask = ES8311_DAC1_DAC_DSMMUTE |
457 ES8311_DAC1_DAC_DEMMUTE;
458 unsigned int val = mute ? mask : 0;
460 regmap_update_bits(es8311->regmap, ES8311_DAC1, mask, val);
466 static int es8311_startup(struct snd_pcm_substream *substream,
467 struct snd_soc_dai *dai)
469 struct snd_soc_component *component = dai->component;
470 struct es8311_priv *es8311 = snd_soc_component_get_drvdata(component);
472 if (es8311->constraints.list) {
473 snd_pcm_hw_constraint_list(substream->runtime, 0,
474 SNDRV_PCM_HW_PARAM_RATE,
475 &es8311->constraints);
481 static int es8311_hw_params(struct snd_pcm_substream *substream,
482 struct snd_pcm_hw_params *params,
483 struct snd_soc_dai *dai)
485 struct snd_soc_component *component = dai->component;
486 struct es8311_priv *es8311 = snd_soc_component_get_drvdata(component);
488 int par_width = params_width(params);
492 wl = ES8311_SDP_WL_16;
495 wl = ES8311_SDP_WL_18;
498 wl = ES8311_SDP_WL_20;
501 wl = ES8311_SDP_WL_24;
504 wl = ES8311_SDP_WL_32;
509 unsigned int width = (unsigned int)par_width;
511 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
512 snd_soc_component_update_bits(component, ES8311_SDP_IN,
514 wl << ES8311_SDP_WL_SHIFT);
516 snd_soc_component_update_bits(component, ES8311_SDP_OUT,
518 wl << ES8311_SDP_WL_SHIFT);
521 if (es8311->mclk_freq > ES8311_MCLK_MAX_FREQ) {
522 dev_err(component->dev, "mclk frequency %lu too high\n",
527 unsigned int mclk_freq = es8311->mclk_freq;
528 unsigned int rate = params_rate(params);
529 unsigned int clkmgr = ES8311_CLKMGR1_MCLK_ON;
532 if (es8311->provider) {
533 dev_err(component->dev,
534 "mclk not configured, cannot run as master\n");
537 dev_dbg(component->dev,
538 "mclk not configured, use bclk as internal mclk\n");
540 clkmgr = ES8311_CLKMGR1_MCLK_SEL;
542 mclk_freq = rate * width * 2;
545 struct es8311_mclk_coeff coeff;
546 int ret = es8311_get_mclk_coeff(mclk_freq, rate, &coeff);
548 dev_err(component->dev, "unable to find mclk coefficient\n");
552 unsigned int mask = ES8311_CLKMGR1_MCLK_SEL | ES8311_CLKMGR1_MCLK_ON |
553 ES8311_CLKMGR1_BCLK_ON;
555 clkmgr |= ES8311_CLKMGR1_BCLK_ON;
556 snd_soc_component_update_bits(component, ES8311_CLKMGR1, mask, clkmgr);
558 if (WARN_ON_ONCE(coeff.div == 0 || coeff.div > 8 ||
559 coeff.div_adc_dac == 0 || coeff.div_adc_dac > 8))
564 switch (coeff.mult) {
582 mask = ES8311_CLKMGR2_DIV_PRE_MASK | ES8311_CLKMGR2_MULT_PRE_MASK;
583 clkmgr = (coeff.div - 1) << ES8311_CLKMGR2_DIV_PRE_SHIFT |
584 mult << ES8311_CLKMGR2_MULT_PRE_SHIFT;
585 snd_soc_component_update_bits(component, ES8311_CLKMGR2, mask, clkmgr);
587 mask = ES8311_CLKMGR5_ADC_DIV_MASK | ES8311_CLKMGR5_DAC_DIV_MASK;
588 clkmgr = (coeff.div_adc_dac - 1) << ES8311_CLKMGR5_ADC_DIV_SHIFT |
589 (coeff.div_adc_dac - 1) << ES8311_CLKMGR5_DAC_DIV_SHIFT;
590 snd_soc_component_update_bits(component, ES8311_CLKMGR5, mask, clkmgr);
592 if (es8311->provider) {
593 unsigned int div_lrclk = mclk_freq / rate;
595 if (WARN_ON_ONCE(div_lrclk == 0 ||
596 div_lrclk > ES8311_CLKMGR_LRCLK_DIV_MAX + 1))
599 mask = ES8311_CLKMGR7_LRCLK_DIV_H_MASK;
600 clkmgr = (div_lrclk - 1) >> 8;
601 snd_soc_component_update_bits(component, ES8311_CLKMGR7, mask,
603 clkmgr = (div_lrclk - 1) & 0xFF;
604 snd_soc_component_write(component, ES8311_CLKMGR8, clkmgr);
606 if (div_lrclk % (2 * width) != 0) {
607 dev_err(component->dev,
608 "unable to divide mclk %u to generate bclk\n",
613 unsigned int div_bclk = div_lrclk / (2 * width);
615 mask = ES8311_CLKMGR6_DIV_BCLK_MASK;
616 if (div_bclk <= ES8311_BCLK_DIV_IDX_OFFSET) {
617 clkmgr = div_bclk - 1;
621 for (i = 0; i < ARRAY_SIZE(es8311_bclk_divs); i++) {
622 if (es8311_bclk_divs[i] == div_bclk)
625 if (i == ARRAY_SIZE(es8311_bclk_divs)) {
626 dev_err(component->dev,
627 "bclk divider %u not supported\n",
632 clkmgr = i + ES8311_BCLK_DIV_IDX_OFFSET;
634 snd_soc_component_update_bits(component, ES8311_CLKMGR6, mask,
641 static int es8311_set_sysclk(struct snd_soc_dai *codec_dai, int clk_id,
642 unsigned int freq, int dir)
644 struct snd_soc_component *component = codec_dai->component;
645 struct es8311_priv *es8311 = snd_soc_component_get_drvdata(component);
647 if (freq > ES8311_MCLK_MAX_FREQ) {
648 dev_err(component->dev, "invalid frequency %u: too high\n",
653 if (es8311->mclk_freq == freq)
656 es8311->mclk_freq = freq;
657 es8311->constraints.list = NULL;
658 es8311->constraints.count = 0;
663 int ret = clk_set_rate(es8311->mclk, freq);
665 dev_err(component->dev, "unable to set mclk rate\n");
669 es8311_set_sysclk_constraints(freq, es8311);
674 static int es8311_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
676 struct snd_soc_component *component = codec_dai->component;
677 struct es8311_priv *es8311 = snd_soc_component_get_drvdata(component);
679 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
680 case SND_SOC_DAIFMT_CBP_CFP:
682 es8311->provider = true;
684 snd_soc_component_update_bits(component, ES8311_RESET,
688 case SND_SOC_DAIFMT_CBC_CFC:
690 es8311->provider = false;
691 snd_soc_component_update_bits(component, ES8311_RESET,
692 ES8311_RESET_MSC, 0);
698 unsigned int sdp = 0;
700 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
701 case SND_SOC_DAIFMT_I2S:
702 sdp |= ES8311_SDP_FMT_I2S;
704 case SND_SOC_DAIFMT_LEFT_J:
705 sdp |= ES8311_SDP_FMT_LEFT_J;
707 case SND_SOC_DAIFMT_RIGHT_J:
708 dev_err(component->dev, "right justified mode not supported\n");
710 case SND_SOC_DAIFMT_DSP_B:
711 sdp |= ES8311_SDP_LRP;
713 case SND_SOC_DAIFMT_DSP_A:
714 sdp |= ES8311_SDP_FMT_DSP;
715 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
716 case SND_SOC_DAIFMT_NB_NF:
717 case SND_SOC_DAIFMT_IB_NF:
720 dev_err(component->dev,
721 "inverted fsync not supported in dsp mode\n");
729 unsigned int clkmgr = 0;
731 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
732 case SND_SOC_DAIFMT_NB_NF:
734 case SND_SOC_DAIFMT_NB_IF:
735 sdp |= ES8311_SDP_LRP;
737 case SND_SOC_DAIFMT_IB_NF:
738 clkmgr |= ES8311_CLKMGR6_BCLK_INV;
740 case SND_SOC_DAIFMT_IB_IF:
741 clkmgr |= ES8311_CLKMGR6_BCLK_INV;
742 sdp |= ES8311_SDP_LRP;
748 unsigned int mask = ES8311_CLKMGR6_BCLK_INV;
750 snd_soc_component_update_bits(component, ES8311_CLKMGR6, mask, clkmgr);
752 mask = ES8311_SDP_FMT_MASK | ES8311_SDP_LRP;
753 snd_soc_component_update_bits(component, ES8311_SDP_IN, mask, sdp);
754 snd_soc_component_update_bits(component, ES8311_SDP_OUT, mask, sdp);
759 static int es8311_set_bias_level(struct snd_soc_component *component,
760 enum snd_soc_bias_level level)
762 struct es8311_priv *es8311 = snd_soc_component_get_drvdata(component);
765 case SND_SOC_BIAS_ON:
767 case SND_SOC_BIAS_PREPARE:
769 case SND_SOC_BIAS_STANDBY:
770 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
771 int ret = clk_prepare_enable(es8311->mclk);
773 dev_err(component->dev,
774 "unable to prepare mclk\n");
778 snd_soc_component_update_bits(
779 component, ES8311_SYS3,
780 ES8311_SYS3_PDN_VMIDSEL_MASK,
781 ES8311_SYS3_PDN_VMIDSEL_STARTUP_NORMAL_SPEED);
785 case SND_SOC_BIAS_OFF:
786 clk_disable_unprepare(es8311->mclk);
787 snd_soc_component_update_bits(
788 component, ES8311_SYS3, ES8311_SYS3_PDN_VMIDSEL_MASK,
789 ES8311_SYS3_PDN_VMIDSEL_POWER_DOWN);
797 static const struct snd_soc_dai_ops es8311_dai_ops = {
798 .startup = es8311_startup,
799 .hw_params = es8311_hw_params,
800 .mute_stream = es8311_mute,
801 .set_sysclk = es8311_set_sysclk,
802 .set_fmt = es8311_set_dai_fmt,
803 .no_capture_mute = 1,
806 static struct snd_soc_dai_driver es8311_dai = {
809 .stream_name = "AIF1 Playback",
812 .rates = ES8311_RATES,
813 .formats = ES8311_FORMATS,
816 .stream_name = "AIF1 Capture",
819 .rates = ES8311_RATES,
820 .formats = ES8311_FORMATS,
822 .ops = &es8311_dai_ops,
826 static void es8311_reset(struct snd_soc_component *component, bool reset)
829 * (1) power down state machine and reset codec blocks then,
830 * (2) after a short delay, power up state machine and leave reset mode.
831 * Specific delay is not documented, using the same as es8316.
833 unsigned int mask = ES8311_RESET_CSM_ON | ES8311_RESET_RST_MASK;
836 /* Enter reset mode */
837 snd_soc_component_update_bits(component, ES8311_RESET, mask,
838 ES8311_RESET_RST_MASK);
840 /* Leave reset mode */
841 usleep_range(5000, 5500);
842 snd_soc_component_update_bits(component, ES8311_RESET, mask,
843 ES8311_RESET_CSM_ON);
847 static int es8311_suspend(struct snd_soc_component *component)
849 struct es8311_priv *es8311;
851 es8311 = snd_soc_component_get_drvdata(component);
853 es8311_reset(component, true);
855 regcache_cache_only(es8311->regmap, true);
856 regcache_mark_dirty(es8311->regmap);
861 static int es8311_resume(struct snd_soc_component *component)
863 struct es8311_priv *es8311;
865 es8311 = snd_soc_component_get_drvdata(component);
867 es8311_reset(component, false);
869 regcache_cache_only(es8311->regmap, false);
870 regcache_sync(es8311->regmap);
875 static int es8311_component_probe(struct snd_soc_component *component)
877 struct es8311_priv *es8311;
879 es8311 = snd_soc_component_get_drvdata(component);
881 es8311->mclk = devm_clk_get_optional(component->dev, "mclk");
882 if (IS_ERR(es8311->mclk)) {
883 dev_err(component->dev, "invalid mclk\n");
884 return PTR_ERR(es8311->mclk);
887 es8311->mclk_freq = clk_get_rate(es8311->mclk);
888 if (es8311->mclk_freq > 0 && es8311->mclk_freq < ES8311_MCLK_MAX_FREQ)
889 es8311_set_sysclk_constraints(es8311->mclk_freq, es8311);
891 es8311_reset(component, true);
892 es8311_reset(component, false);
894 /* Set minimal power up time */
895 snd_soc_component_write(component, ES8311_SYS1, 0);
896 snd_soc_component_write(component, ES8311_SYS2, 0);
901 static const struct regmap_config es8311_regmap_config = {
904 .max_register = ES8311_REG_MAX,
905 .cache_type = REGCACHE_MAPLE,
906 .use_single_read = true,
907 .use_single_write = true,
910 static const struct snd_soc_component_driver es8311_component_driver = {
911 .probe = es8311_component_probe,
912 .suspend = es8311_suspend,
913 .resume = es8311_resume,
914 .set_bias_level = es8311_set_bias_level,
915 .controls = es8311_snd_controls,
916 .num_controls = ARRAY_SIZE(es8311_snd_controls),
917 .dapm_widgets = es8311_dapm_widgets,
918 .num_dapm_widgets = ARRAY_SIZE(es8311_dapm_widgets),
919 .dapm_routes = es8311_dapm_routes,
920 .num_dapm_routes = ARRAY_SIZE(es8311_dapm_routes),
921 .use_pmdown_time = 1,
925 static int es8311_i2c_probe(struct i2c_client *i2c_client)
927 struct es8311_priv *es8311;
929 struct device *dev = &i2c_client->dev;
931 es8311 = devm_kzalloc(dev, sizeof(*es8311), GFP_KERNEL);
936 devm_regmap_init_i2c(i2c_client, &es8311_regmap_config);
937 if (IS_ERR(es8311->regmap))
938 return PTR_ERR(es8311->regmap);
940 i2c_set_clientdata(i2c_client, es8311);
942 return devm_snd_soc_register_component(dev, &es8311_component_driver,
946 static const struct i2c_device_id es8311_id[] = {
950 MODULE_DEVICE_TABLE(i2c, es8311_id);
952 static const struct of_device_id es8311_of_match[] = {
954 .compatible = "everest,es8311",
958 MODULE_DEVICE_TABLE(of, es8311_of_match);
960 static struct i2c_driver es8311_i2c_driver = {
963 .of_match_table = es8311_of_match,
965 .probe = es8311_i2c_probe,
966 .id_table = es8311_id,
969 module_i2c_driver(es8311_i2c_driver);
971 MODULE_DESCRIPTION("ASoC ES8311 driver");
973 MODULE_LICENSE("GPL");