Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * es8316.c -- es8316 ALSA SoC audio driver
4 * Copyright Everest Semiconductor Co.,Ltd
5 *
6 * Authors: David Yang <yangxiaohua@everest-semi.com>,
7 * Daniel Drake <drake@endlessm.com>
8 */
9
10#include <linux/module.h>
11#include <linux/acpi.h>
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/i2c.h>
15#include <linux/mod_devicetable.h>
16#include <linux/mutex.h>
17#include <linux/regmap.h>
18#include <sound/pcm.h>
19#include <sound/pcm_params.h>
20#include <sound/soc.h>
21#include <sound/soc-dapm.h>
22#include <sound/tlv.h>
23#include <sound/jack.h>
24#include "es8316.h"
25
26/* In slave mode at single speed, the codec is documented as accepting 5
27 * MCLK/LRCK ratios, but we also add ratio 400, which is commonly used on
28 * Intel Cherry Trail platforms (19.2MHz MCLK, 48kHz LRCK).
29 */
30static const unsigned int supported_mclk_lrck_ratios[] = {
31 256, 384, 400, 500, 512, 768, 1024
32};
33
34struct es8316_priv {
35 struct mutex lock;
36 struct clk *mclk;
37 struct regmap *regmap;
38 struct snd_soc_component *component;
39 struct snd_soc_jack *jack;
40 int irq;
41 unsigned int sysclk;
42 /* ES83xx supports halving the MCLK so it supports twice as many rates
43 */
44 unsigned int allowed_rates[ARRAY_SIZE(supported_mclk_lrck_ratios) * 2];
45 struct snd_pcm_hw_constraint_list sysclk_constraints;
46 bool jd_inverted;
47};
48
49/*
50 * ES8316 controls
51 */
52static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9600, 50, 1);
53static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9600, 50, 1);
54static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_max_gain_tlv, -650, 150, 0);
55static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_min_gain_tlv, -1200, 150, 0);
56
57static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(alc_target_tlv,
58 0, 10, TLV_DB_SCALE_ITEM(-1650, 150, 0),
59 11, 11, TLV_DB_SCALE_ITEM(-150, 0, 0),
60);
61
62static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpmixer_gain_tlv,
63 0, 4, TLV_DB_SCALE_ITEM(-1200, 150, 0),
64 8, 11, TLV_DB_SCALE_ITEM(-450, 150, 0),
65);
66
67static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(adc_pga_gain_tlv,
68 0, 0, TLV_DB_SCALE_ITEM(-350, 0, 0),
69 1, 1, TLV_DB_SCALE_ITEM(0, 0, 0),
70 2, 2, TLV_DB_SCALE_ITEM(250, 0, 0),
71 3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
72 4, 7, TLV_DB_SCALE_ITEM(700, 300, 0),
73 8, 10, TLV_DB_SCALE_ITEM(1800, 300, 0),
74);
75
76static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpout_vol_tlv,
77 0, 0, TLV_DB_SCALE_ITEM(-4800, 0, 0),
78 1, 3, TLV_DB_SCALE_ITEM(-2400, 1200, 0),
79);
80
81static const char * const ng_type_txt[] =
82 { "Constant PGA Gain", "Mute ADC Output" };
83static const struct soc_enum ng_type =
84 SOC_ENUM_SINGLE(ES8316_ADC_ALC_NG, 6, 2, ng_type_txt);
85
86static const char * const adcpol_txt[] = { "Normal", "Invert" };
87static const struct soc_enum adcpol =
88 SOC_ENUM_SINGLE(ES8316_ADC_MUTE, 1, 2, adcpol_txt);
89static const char *const dacpol_txt[] =
90 { "Normal", "R Invert", "L Invert", "L + R Invert" };
91static const struct soc_enum dacpol =
92 SOC_ENUM_SINGLE(ES8316_DAC_SET1, 0, 4, dacpol_txt);
93
94static const struct snd_kcontrol_new es8316_snd_controls[] = {
95 SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL,
96 4, 0, 3, 1, hpout_vol_tlv),
97 SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL,
98 4, 0, 11, 0, hpmixer_gain_tlv),
99
100 SOC_ENUM("Playback Polarity", dacpol),
101 SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL,
102 ES8316_DAC_VOLR, 0, 0xc0, 1, dac_vol_tlv),
103 SOC_SINGLE("DAC Soft Ramp Switch", ES8316_DAC_SET1, 4, 1, 1),
104 SOC_SINGLE("DAC Soft Ramp Rate", ES8316_DAC_SET1, 2, 4, 0),
105 SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0),
106 SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0),
107 SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0),
108 SOC_SINGLE("DAC Mono Mix Switch", ES8316_DAC_SET3, 3, 1, 0),
109
110 SOC_ENUM("Capture Polarity", adcpol),
111 SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0),
112 SOC_SINGLE_TLV("ADC Capture Volume", ES8316_ADC_VOLUME,
113 0, 0xc0, 1, adc_vol_tlv),
114 SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8316_ADC_PGAGAIN,
115 4, 10, 0, adc_pga_gain_tlv),
116 SOC_SINGLE("ADC Soft Ramp Switch", ES8316_ADC_MUTE, 4, 1, 0),
117 SOC_SINGLE("ADC Double Fs Switch", ES8316_ADC_DMIC, 4, 1, 0),
118
119 SOC_SINGLE("ALC Capture Switch", ES8316_ADC_ALC1, 6, 1, 0),
120 SOC_SINGLE_TLV("ALC Capture Max Volume", ES8316_ADC_ALC1, 0, 28, 0,
121 alc_max_gain_tlv),
122 SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0,
123 alc_min_gain_tlv),
124 SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 11, 0,
125 alc_target_tlv),
126 SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3, 0, 10, 0),
127 SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4, 4, 10, 0),
128 SOC_SINGLE("ALC Capture Attack Time", ES8316_ADC_ALC4, 0, 10, 0),
129 SOC_SINGLE("ALC Capture Noise Gate Switch", ES8316_ADC_ALC_NG,
130 5, 1, 0),
131 SOC_SINGLE("ALC Capture Noise Gate Threshold", ES8316_ADC_ALC_NG,
132 0, 31, 0),
133 SOC_ENUM("ALC Capture Noise Gate Type", ng_type),
134};
135
136/* Analog Input Mux */
137static const char * const es8316_analog_in_txt[] = {
138 "lin1-rin1",
139 "lin2-rin2",
140 "lin1-rin1 with 20db Boost",
141 "lin2-rin2 with 20db Boost"
142};
143static const unsigned int es8316_analog_in_values[] = { 0, 1, 2, 3 };
144static const struct soc_enum es8316_analog_input_enum =
145 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_PDN_LINSEL, 4, 3,
146 ARRAY_SIZE(es8316_analog_in_txt),
147 es8316_analog_in_txt,
148 es8316_analog_in_values);
149static const struct snd_kcontrol_new es8316_analog_in_mux_controls =
150 SOC_DAPM_ENUM("Route", es8316_analog_input_enum);
151
152static const char * const es8316_dmic_txt[] = {
153 "dmic disable",
154 "dmic data at high level",
155 "dmic data at low level",
156};
157static const unsigned int es8316_dmic_values[] = { 0, 2, 3 };
158static const struct soc_enum es8316_dmic_src_enum =
159 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC, 0, 3,
160 ARRAY_SIZE(es8316_dmic_txt),
161 es8316_dmic_txt,
162 es8316_dmic_values);
163static const struct snd_kcontrol_new es8316_dmic_src_controls =
164 SOC_DAPM_ENUM("Route", es8316_dmic_src_enum);
165
166/* hp mixer mux */
167static const char * const es8316_hpmux_texts[] = {
168 "lin1-rin1",
169 "lin2-rin2",
170 "lin-rin with Boost",
171 "lin-rin with Boost and PGA"
172};
173
174static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL,
175 4, es8316_hpmux_texts);
176
177static const struct snd_kcontrol_new es8316_left_hpmux_controls =
178 SOC_DAPM_ENUM("Route", es8316_left_hpmux_enum);
179
180static SOC_ENUM_SINGLE_DECL(es8316_right_hpmux_enum, ES8316_HPMIX_SEL,
181 0, es8316_hpmux_texts);
182
183static const struct snd_kcontrol_new es8316_right_hpmux_controls =
184 SOC_DAPM_ENUM("Route", es8316_right_hpmux_enum);
185
186/* headphone Output Mixer */
187static const struct snd_kcontrol_new es8316_out_left_mix[] = {
188 SOC_DAPM_SINGLE("LLIN Switch", ES8316_HPMIX_SWITCH, 6, 1, 0),
189 SOC_DAPM_SINGLE("Left DAC Switch", ES8316_HPMIX_SWITCH, 7, 1, 0),
190};
191static const struct snd_kcontrol_new es8316_out_right_mix[] = {
192 SOC_DAPM_SINGLE("RLIN Switch", ES8316_HPMIX_SWITCH, 2, 1, 0),
193 SOC_DAPM_SINGLE("Right DAC Switch", ES8316_HPMIX_SWITCH, 3, 1, 0),
194};
195
196/* DAC data source mux */
197static const char * const es8316_dacsrc_texts[] = {
198 "LDATA TO LDAC, RDATA TO RDAC",
199 "LDATA TO LDAC, LDATA TO RDAC",
200 "RDATA TO LDAC, RDATA TO RDAC",
201 "RDATA TO LDAC, LDATA TO RDAC",
202};
203
204static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1,
205 6, es8316_dacsrc_texts);
206
207static const struct snd_kcontrol_new es8316_dacsrc_mux_controls =
208 SOC_DAPM_ENUM("Route", es8316_dacsrc_mux_enum);
209
210static const struct snd_soc_dapm_widget es8316_dapm_widgets[] = {
211 SND_SOC_DAPM_SUPPLY("Bias", ES8316_SYS_PDN, 3, 1, NULL, 0),
212 SND_SOC_DAPM_SUPPLY("Analog power", ES8316_SYS_PDN, 4, 1, NULL, 0),
213 SND_SOC_DAPM_SUPPLY("Mic Bias", ES8316_SYS_PDN, 5, 1, NULL, 0),
214
215 SND_SOC_DAPM_INPUT("DMIC"),
216 SND_SOC_DAPM_INPUT("MIC1"),
217 SND_SOC_DAPM_INPUT("MIC2"),
218
219 /* Input Mux */
220 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
221 &es8316_analog_in_mux_controls),
222
223 SND_SOC_DAPM_SUPPLY("ADC Vref", ES8316_SYS_PDN, 1, 1, NULL, 0),
224 SND_SOC_DAPM_SUPPLY("ADC bias", ES8316_SYS_PDN, 2, 1, NULL, 0),
225 SND_SOC_DAPM_SUPPLY("ADC Clock", ES8316_CLKMGR_CLKSW, 3, 0, NULL, 0),
226 SND_SOC_DAPM_PGA("Line input PGA", ES8316_ADC_PDN_LINSEL,
227 7, 1, NULL, 0),
228 SND_SOC_DAPM_ADC("Mono ADC", NULL, ES8316_ADC_PDN_LINSEL, 6, 1),
229 SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0,
230 &es8316_dmic_src_controls),
231
232 /* Digital Interface */
233 SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 1,
234 ES8316_SERDATA_ADC, 6, 1),
235 SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0,
236 SND_SOC_NOPM, 0, 0),
237
238 SND_SOC_DAPM_MUX("DAC Source Mux", SND_SOC_NOPM, 0, 0,
239 &es8316_dacsrc_mux_controls),
240
241 SND_SOC_DAPM_SUPPLY("DAC Vref", ES8316_SYS_PDN, 0, 1, NULL, 0),
242 SND_SOC_DAPM_SUPPLY("DAC Clock", ES8316_CLKMGR_CLKSW, 2, 0, NULL, 0),
243 SND_SOC_DAPM_DAC("Right DAC", NULL, ES8316_DAC_PDN, 0, 1),
244 SND_SOC_DAPM_DAC("Left DAC", NULL, ES8316_DAC_PDN, 4, 1),
245
246 /* Headphone Output Side */
247 SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
248 &es8316_left_hpmux_controls),
249 SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
250 &es8316_right_hpmux_controls),
251 SND_SOC_DAPM_MIXER("Left Headphone Mixer", ES8316_HPMIX_PDN,
252 5, 1, &es8316_out_left_mix[0],
253 ARRAY_SIZE(es8316_out_left_mix)),
254 SND_SOC_DAPM_MIXER("Right Headphone Mixer", ES8316_HPMIX_PDN,
255 1, 1, &es8316_out_right_mix[0],
256 ARRAY_SIZE(es8316_out_right_mix)),
257 SND_SOC_DAPM_PGA("Left Headphone Mixer Out", ES8316_HPMIX_PDN,
258 4, 1, NULL, 0),
259 SND_SOC_DAPM_PGA("Right Headphone Mixer Out", ES8316_HPMIX_PDN,
260 0, 1, NULL, 0),
261
262 SND_SOC_DAPM_OUT_DRV("Left Headphone Charge Pump", ES8316_CPHP_OUTEN,
263 6, 0, NULL, 0),
264 SND_SOC_DAPM_OUT_DRV("Right Headphone Charge Pump", ES8316_CPHP_OUTEN,
265 2, 0, NULL, 0),
266 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8316_CPHP_PDN2,
267 5, 1, NULL, 0),
268 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump Clock", ES8316_CLKMGR_CLKSW,
269 4, 0, NULL, 0),
270
271 SND_SOC_DAPM_OUT_DRV("Left Headphone Driver", ES8316_CPHP_OUTEN,
272 5, 0, NULL, 0),
273 SND_SOC_DAPM_OUT_DRV("Right Headphone Driver", ES8316_CPHP_OUTEN,
274 1, 0, NULL, 0),
275 SND_SOC_DAPM_SUPPLY("Headphone Out", ES8316_CPHP_PDN1, 2, 1, NULL, 0),
276
277 /* pdn_Lical and pdn_Rical bits are documented as Reserved, but must
278 * be explicitly unset in order to enable HP output
279 */
280 SND_SOC_DAPM_SUPPLY("Left Headphone ical", ES8316_CPHP_ICAL_VOL,
281 7, 1, NULL, 0),
282 SND_SOC_DAPM_SUPPLY("Right Headphone ical", ES8316_CPHP_ICAL_VOL,
283 3, 1, NULL, 0),
284
285 SND_SOC_DAPM_OUTPUT("HPOL"),
286 SND_SOC_DAPM_OUTPUT("HPOR"),
287};
288
289static const struct snd_soc_dapm_route es8316_dapm_routes[] = {
290 /* Recording */
291 {"MIC1", NULL, "Mic Bias"},
292 {"MIC2", NULL, "Mic Bias"},
293 {"MIC1", NULL, "Bias"},
294 {"MIC2", NULL, "Bias"},
295 {"MIC1", NULL, "Analog power"},
296 {"MIC2", NULL, "Analog power"},
297
298 {"Differential Mux", "lin1-rin1", "MIC1"},
299 {"Differential Mux", "lin2-rin2", "MIC2"},
300 {"Line input PGA", NULL, "Differential Mux"},
301
302 {"Mono ADC", NULL, "ADC Clock"},
303 {"Mono ADC", NULL, "ADC Vref"},
304 {"Mono ADC", NULL, "ADC bias"},
305 {"Mono ADC", NULL, "Line input PGA"},
306
307 /* It's not clear why, but to avoid recording only silence,
308 * the DAC clock must be running for the ADC to work.
309 */
310 {"Mono ADC", NULL, "DAC Clock"},
311
312 {"Digital Mic Mux", "dmic disable", "Mono ADC"},
313
314 {"I2S OUT", NULL, "Digital Mic Mux"},
315
316 /* Playback */
317 {"DAC Source Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
318
319 {"Left DAC", NULL, "DAC Clock"},
320 {"Right DAC", NULL, "DAC Clock"},
321
322 {"Left DAC", NULL, "DAC Vref"},
323 {"Right DAC", NULL, "DAC Vref"},
324
325 {"Left DAC", NULL, "DAC Source Mux"},
326 {"Right DAC", NULL, "DAC Source Mux"},
327
328 {"Left Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
329 {"Right Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
330
331 {"Left Headphone Mixer", "LLIN Switch", "Left Headphone Mux"},
332 {"Left Headphone Mixer", "Left DAC Switch", "Left DAC"},
333
334 {"Right Headphone Mixer", "RLIN Switch", "Right Headphone Mux"},
335 {"Right Headphone Mixer", "Right DAC Switch", "Right DAC"},
336
337 {"Left Headphone Mixer Out", NULL, "Left Headphone Mixer"},
338 {"Right Headphone Mixer Out", NULL, "Right Headphone Mixer"},
339
340 {"Left Headphone Charge Pump", NULL, "Left Headphone Mixer Out"},
341 {"Right Headphone Charge Pump", NULL, "Right Headphone Mixer Out"},
342
343 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump"},
344 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump"},
345
346 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
347 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
348
349 {"Left Headphone Driver", NULL, "Left Headphone Charge Pump"},
350 {"Right Headphone Driver", NULL, "Right Headphone Charge Pump"},
351
352 {"HPOL", NULL, "Left Headphone Driver"},
353 {"HPOR", NULL, "Right Headphone Driver"},
354
355 {"HPOL", NULL, "Left Headphone ical"},
356 {"HPOR", NULL, "Right Headphone ical"},
357
358 {"Headphone Out", NULL, "Bias"},
359 {"Headphone Out", NULL, "Analog power"},
360 {"HPOL", NULL, "Headphone Out"},
361 {"HPOR", NULL, "Headphone Out"},
362};
363
364static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai,
365 int clk_id, unsigned int freq, int dir)
366{
367 struct snd_soc_component *component = codec_dai->component;
368 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
369 int i, ret;
370 int count = 0;
371
372 es8316->sysclk = freq;
373 es8316->sysclk_constraints.list = NULL;
374 es8316->sysclk_constraints.count = 0;
375
376 if (freq == 0)
377 return 0;
378
379 ret = clk_set_rate(es8316->mclk, freq);
380 if (ret)
381 return ret;
382
383 /* Limit supported sample rates to ones that can be autodetected
384 * by the codec running in slave mode.
385 */
386 for (i = 0; i < ARRAY_SIZE(supported_mclk_lrck_ratios); i++) {
387 const unsigned int ratio = supported_mclk_lrck_ratios[i];
388
389 if (freq % ratio == 0)
390 es8316->allowed_rates[count++] = freq / ratio;
391
392 /* We also check if the halved MCLK produces a valid rate
393 * since the codec supports halving the MCLK.
394 */
395 if ((freq / ratio) % 2 == 0)
396 es8316->allowed_rates[count++] = freq / ratio / 2;
397 }
398
399 if (count) {
400 es8316->sysclk_constraints.list = es8316->allowed_rates;
401 es8316->sysclk_constraints.count = count;
402 }
403
404 return 0;
405}
406
407static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai,
408 unsigned int fmt)
409{
410 struct snd_soc_component *component = codec_dai->component;
411 u8 serdata1 = 0;
412 u8 serdata2 = 0;
413 u8 clksw;
414 u8 mask;
415
416 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBP_CFP)
417 serdata1 |= ES8316_SERDATA1_MASTER;
418
419 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) {
420 dev_err(component->dev, "Codec driver only supports I2S format\n");
421 return -EINVAL;
422 }
423
424 /* Clock inversion */
425 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
426 case SND_SOC_DAIFMT_NB_NF:
427 break;
428 case SND_SOC_DAIFMT_IB_IF:
429 serdata1 |= ES8316_SERDATA1_BCLK_INV;
430 serdata2 |= ES8316_SERDATA2_ADCLRP;
431 break;
432 case SND_SOC_DAIFMT_IB_NF:
433 serdata1 |= ES8316_SERDATA1_BCLK_INV;
434 break;
435 case SND_SOC_DAIFMT_NB_IF:
436 serdata2 |= ES8316_SERDATA2_ADCLRP;
437 break;
438 default:
439 return -EINVAL;
440 }
441
442 mask = ES8316_SERDATA1_MASTER | ES8316_SERDATA1_BCLK_INV;
443 snd_soc_component_update_bits(component, ES8316_SERDATA1, mask, serdata1);
444
445 mask = ES8316_SERDATA2_FMT_MASK | ES8316_SERDATA2_ADCLRP;
446 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, mask, serdata2);
447 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, mask, serdata2);
448
449 /* Enable BCLK and MCLK inputs in slave mode */
450 clksw = ES8316_CLKMGR_CLKSW_MCLK_ON | ES8316_CLKMGR_CLKSW_BCLK_ON;
451 snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, clksw, clksw);
452
453 return 0;
454}
455
456static int es8316_pcm_startup(struct snd_pcm_substream *substream,
457 struct snd_soc_dai *dai)
458{
459 struct snd_soc_component *component = dai->component;
460 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
461
462 if (es8316->sysclk_constraints.list)
463 snd_pcm_hw_constraint_list(substream->runtime, 0,
464 SNDRV_PCM_HW_PARAM_RATE,
465 &es8316->sysclk_constraints);
466
467 return 0;
468}
469
470static int es8316_pcm_hw_params(struct snd_pcm_substream *substream,
471 struct snd_pcm_hw_params *params,
472 struct snd_soc_dai *dai)
473{
474 struct snd_soc_component *component = dai->component;
475 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
476 u8 wordlen = 0;
477 u8 bclk_divider;
478 u16 lrck_divider;
479 int i;
480 unsigned int clk = es8316->sysclk / 2;
481 bool clk_valid = false;
482
483 /* We will start with halved sysclk and see if we can use it
484 * for proper clocking. This is to minimise the risk of running
485 * the CODEC with a too high frequency. We have an SKU where
486 * the sysclk frequency is 48Mhz and this causes the sound to be
487 * sped up. If we can run with a halved sysclk, we will use it,
488 * if we can't use it, then full sysclk will be used.
489 */
490 do {
491 /* Validate supported sample rates that are autodetected from MCLK */
492 for (i = 0; i < ARRAY_SIZE(supported_mclk_lrck_ratios); i++) {
493 const unsigned int ratio = supported_mclk_lrck_ratios[i];
494
495 if (clk % ratio != 0)
496 continue;
497 if (clk / ratio == params_rate(params))
498 break;
499 }
500 if (i == ARRAY_SIZE(supported_mclk_lrck_ratios)) {
501 if (clk == es8316->sysclk)
502 return -EINVAL;
503 clk = es8316->sysclk;
504 } else {
505 clk_valid = true;
506 }
507 } while (!clk_valid);
508
509 if (clk != es8316->sysclk) {
510 snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW,
511 ES8316_CLKMGR_CLKSW_MCLK_DIV,
512 ES8316_CLKMGR_CLKSW_MCLK_DIV);
513 }
514
515 lrck_divider = clk / params_rate(params);
516 bclk_divider = lrck_divider / 4;
517 switch (params_format(params)) {
518 case SNDRV_PCM_FORMAT_S16_LE:
519 wordlen = ES8316_SERDATA2_LEN_16;
520 bclk_divider /= 16;
521 break;
522 case SNDRV_PCM_FORMAT_S20_3LE:
523 wordlen = ES8316_SERDATA2_LEN_20;
524 bclk_divider /= 20;
525 break;
526 case SNDRV_PCM_FORMAT_S24_LE:
527 case SNDRV_PCM_FORMAT_S24_3LE:
528 wordlen = ES8316_SERDATA2_LEN_24;
529 bclk_divider /= 24;
530 break;
531 case SNDRV_PCM_FORMAT_S32_LE:
532 wordlen = ES8316_SERDATA2_LEN_32;
533 bclk_divider /= 32;
534 break;
535 default:
536 return -EINVAL;
537 }
538
539 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC,
540 ES8316_SERDATA2_LEN_MASK, wordlen);
541 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC,
542 ES8316_SERDATA2_LEN_MASK, wordlen);
543 snd_soc_component_update_bits(component, ES8316_SERDATA1, 0x1f, bclk_divider);
544 snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV1, 0x0f, lrck_divider >> 8);
545 snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV2, 0xff, lrck_divider & 0xff);
546 snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV1, 0x0f, lrck_divider >> 8);
547 snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV2, 0xff, lrck_divider & 0xff);
548 return 0;
549}
550
551static int es8316_mute(struct snd_soc_dai *dai, int mute, int direction)
552{
553 snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20,
554 mute ? 0x20 : 0);
555 return 0;
556}
557
558#define ES8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
559 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
560
561static const struct snd_soc_dai_ops es8316_ops = {
562 .startup = es8316_pcm_startup,
563 .hw_params = es8316_pcm_hw_params,
564 .set_fmt = es8316_set_dai_fmt,
565 .set_sysclk = es8316_set_dai_sysclk,
566 .mute_stream = es8316_mute,
567 .no_capture_mute = 1,
568};
569
570static struct snd_soc_dai_driver es8316_dai = {
571 .name = "ES8316 HiFi",
572 .playback = {
573 .stream_name = "Playback",
574 .channels_min = 1,
575 .channels_max = 2,
576 .rates = SNDRV_PCM_RATE_8000_48000,
577 .formats = ES8316_FORMATS,
578 },
579 .capture = {
580 .stream_name = "Capture",
581 .channels_min = 1,
582 .channels_max = 2,
583 .rates = SNDRV_PCM_RATE_8000_48000,
584 .formats = ES8316_FORMATS,
585 },
586 .ops = &es8316_ops,
587 .symmetric_rate = 1,
588};
589
590static void es8316_enable_micbias_for_mic_gnd_short_detect(
591 struct snd_soc_component *component)
592{
593 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
594
595 snd_soc_dapm_mutex_lock(dapm);
596 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Bias");
597 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Analog power");
598 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Mic Bias");
599 snd_soc_dapm_sync_unlocked(dapm);
600 snd_soc_dapm_mutex_unlock(dapm);
601
602 msleep(20);
603}
604
605static void es8316_disable_micbias_for_mic_gnd_short_detect(
606 struct snd_soc_component *component)
607{
608 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
609
610 snd_soc_dapm_mutex_lock(dapm);
611 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Bias");
612 snd_soc_dapm_disable_pin_unlocked(dapm, "Analog power");
613 snd_soc_dapm_disable_pin_unlocked(dapm, "Bias");
614 snd_soc_dapm_sync_unlocked(dapm);
615 snd_soc_dapm_mutex_unlock(dapm);
616}
617
618static irqreturn_t es8316_irq(int irq, void *data)
619{
620 struct es8316_priv *es8316 = data;
621 struct snd_soc_component *comp = es8316->component;
622 unsigned int flags;
623
624 mutex_lock(&es8316->lock);
625
626 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
627 if (flags == 0x00)
628 goto out; /* Powered-down / reset */
629
630 /* Catch spurious IRQ before set_jack is called */
631 if (!es8316->jack)
632 goto out;
633
634 if (es8316->jd_inverted)
635 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
636
637 dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
638 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
639 /* Jack removed, or spurious IRQ? */
640 if (es8316->jack->status & SND_JACK_MICROPHONE)
641 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
642
643 if (es8316->jack->status & SND_JACK_HEADPHONE) {
644 snd_soc_jack_report(es8316->jack, 0,
645 SND_JACK_HEADSET | SND_JACK_BTN_0);
646 dev_dbg(comp->dev, "jack unplugged\n");
647 }
648 } else if (!(es8316->jack->status & SND_JACK_HEADPHONE)) {
649 /* Jack inserted, determine type */
650 es8316_enable_micbias_for_mic_gnd_short_detect(comp);
651 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
652 if (es8316->jd_inverted)
653 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
654 dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
655 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
656 /* Jack unplugged underneath us */
657 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
658 } else if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
659 /* Open, headset */
660 snd_soc_jack_report(es8316->jack,
661 SND_JACK_HEADSET,
662 SND_JACK_HEADSET);
663 /* Keep mic-gnd-short detection on for button press */
664 } else {
665 /* Shorted, headphones */
666 snd_soc_jack_report(es8316->jack,
667 SND_JACK_HEADPHONE,
668 SND_JACK_HEADSET);
669 /* No longer need mic-gnd-short detection */
670 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
671 }
672 } else if (es8316->jack->status & SND_JACK_MICROPHONE) {
673 /* Interrupt while jack inserted, report button state */
674 if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
675 /* Open, button release */
676 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
677 } else {
678 /* Short, button press */
679 snd_soc_jack_report(es8316->jack,
680 SND_JACK_BTN_0,
681 SND_JACK_BTN_0);
682 }
683 }
684
685out:
686 mutex_unlock(&es8316->lock);
687 return IRQ_HANDLED;
688}
689
690static void es8316_enable_jack_detect(struct snd_soc_component *component,
691 struct snd_soc_jack *jack)
692{
693 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
694
695 /*
696 * Init es8316->jd_inverted here and not in the probe, as we cannot
697 * guarantee that the bytchr-es8316 driver, which might set this
698 * property, will probe before us.
699 */
700 es8316->jd_inverted = device_property_read_bool(component->dev,
701 "everest,jack-detect-inverted");
702
703 mutex_lock(&es8316->lock);
704
705 es8316->jack = jack;
706
707 if (es8316->jack->status & SND_JACK_MICROPHONE)
708 es8316_enable_micbias_for_mic_gnd_short_detect(component);
709
710 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
711 ES8316_GPIO_ENABLE_INTERRUPT,
712 ES8316_GPIO_ENABLE_INTERRUPT);
713
714 mutex_unlock(&es8316->lock);
715
716 /* Enable irq and sync initial jack state */
717 enable_irq(es8316->irq);
718 es8316_irq(es8316->irq, es8316);
719}
720
721static void es8316_disable_jack_detect(struct snd_soc_component *component)
722{
723 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
724
725 if (!es8316->jack)
726 return; /* Already disabled (or never enabled) */
727
728 disable_irq(es8316->irq);
729
730 mutex_lock(&es8316->lock);
731
732 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
733 ES8316_GPIO_ENABLE_INTERRUPT, 0);
734
735 if (es8316->jack->status & SND_JACK_MICROPHONE) {
736 es8316_disable_micbias_for_mic_gnd_short_detect(component);
737 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
738 }
739
740 es8316->jack = NULL;
741
742 mutex_unlock(&es8316->lock);
743}
744
745static int es8316_set_jack(struct snd_soc_component *component,
746 struct snd_soc_jack *jack, void *data)
747{
748 if (jack)
749 es8316_enable_jack_detect(component, jack);
750 else
751 es8316_disable_jack_detect(component);
752
753 return 0;
754}
755
756static int es8316_probe(struct snd_soc_component *component)
757{
758 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
759 int ret;
760
761 es8316->component = component;
762
763 es8316->mclk = devm_clk_get_optional(component->dev, "mclk");
764 if (IS_ERR(es8316->mclk)) {
765 dev_err(component->dev, "unable to get mclk\n");
766 return PTR_ERR(es8316->mclk);
767 }
768 if (!es8316->mclk)
769 dev_warn(component->dev, "assuming static mclk\n");
770
771 ret = clk_prepare_enable(es8316->mclk);
772 if (ret) {
773 dev_err(component->dev, "unable to enable mclk\n");
774 return ret;
775 }
776
777 /* Reset codec and enable current state machine */
778 snd_soc_component_write(component, ES8316_RESET, 0x3f);
779 usleep_range(5000, 5500);
780 snd_soc_component_write(component, ES8316_RESET, ES8316_RESET_CSM_ON);
781 msleep(30);
782
783 /*
784 * Documentation is unclear, but this value from the vendor driver is
785 * needed otherwise audio output is silent.
786 */
787 snd_soc_component_write(component, ES8316_SYS_VMIDSEL, 0xff);
788
789 /*
790 * Documentation for this register is unclear and incomplete,
791 * but here is a vendor-provided value that improves volume
792 * and quality for Intel CHT platforms.
793 */
794 snd_soc_component_write(component, ES8316_CLKMGR_ADCOSR, 0x32);
795
796 return 0;
797}
798
799static void es8316_remove(struct snd_soc_component *component)
800{
801 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
802
803 clk_disable_unprepare(es8316->mclk);
804}
805
806static int es8316_resume(struct snd_soc_component *component)
807{
808 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
809
810 regcache_cache_only(es8316->regmap, false);
811 regcache_sync(es8316->regmap);
812
813 return 0;
814}
815
816static int es8316_suspend(struct snd_soc_component *component)
817{
818 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
819
820 regcache_cache_only(es8316->regmap, true);
821 regcache_mark_dirty(es8316->regmap);
822
823 return 0;
824}
825
826static const struct snd_soc_component_driver soc_component_dev_es8316 = {
827 .probe = es8316_probe,
828 .remove = es8316_remove,
829 .resume = es8316_resume,
830 .suspend = es8316_suspend,
831 .set_jack = es8316_set_jack,
832 .controls = es8316_snd_controls,
833 .num_controls = ARRAY_SIZE(es8316_snd_controls),
834 .dapm_widgets = es8316_dapm_widgets,
835 .num_dapm_widgets = ARRAY_SIZE(es8316_dapm_widgets),
836 .dapm_routes = es8316_dapm_routes,
837 .num_dapm_routes = ARRAY_SIZE(es8316_dapm_routes),
838 .use_pmdown_time = 1,
839 .endianness = 1,
840};
841
842static bool es8316_volatile_reg(struct device *dev, unsigned int reg)
843{
844 switch (reg) {
845 case ES8316_GPIO_FLAG:
846 return true;
847 default:
848 return false;
849 }
850}
851
852static const struct regmap_config es8316_regmap = {
853 .reg_bits = 8,
854 .val_bits = 8,
855 .use_single_read = true,
856 .use_single_write = true,
857 .max_register = 0x53,
858 .volatile_reg = es8316_volatile_reg,
859 .cache_type = REGCACHE_MAPLE,
860};
861
862static int es8316_i2c_probe(struct i2c_client *i2c_client)
863{
864 struct device *dev = &i2c_client->dev;
865 struct es8316_priv *es8316;
866 int ret;
867
868 es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv),
869 GFP_KERNEL);
870 if (es8316 == NULL)
871 return -ENOMEM;
872
873 i2c_set_clientdata(i2c_client, es8316);
874
875 es8316->regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap);
876 if (IS_ERR(es8316->regmap))
877 return PTR_ERR(es8316->regmap);
878
879 es8316->irq = i2c_client->irq;
880 mutex_init(&es8316->lock);
881
882 if (es8316->irq > 0) {
883 ret = devm_request_threaded_irq(dev, es8316->irq, NULL, es8316_irq,
884 IRQF_TRIGGER_HIGH | IRQF_ONESHOT | IRQF_NO_AUTOEN,
885 "es8316", es8316);
886 if (ret) {
887 dev_warn(dev, "Failed to get IRQ %d: %d\n", es8316->irq, ret);
888 es8316->irq = -ENXIO;
889 }
890 }
891
892 return devm_snd_soc_register_component(&i2c_client->dev,
893 &soc_component_dev_es8316,
894 &es8316_dai, 1);
895}
896
897static const struct i2c_device_id es8316_i2c_id[] = {
898 {"es8316" },
899 {}
900};
901MODULE_DEVICE_TABLE(i2c, es8316_i2c_id);
902
903#ifdef CONFIG_OF
904static const struct of_device_id es8316_of_match[] = {
905 { .compatible = "everest,es8316", },
906 {},
907};
908MODULE_DEVICE_TABLE(of, es8316_of_match);
909#endif
910
911#ifdef CONFIG_ACPI
912static const struct acpi_device_id es8316_acpi_match[] = {
913 {"ESSX8316", 0},
914 {"ESSX8336", 0},
915 {},
916};
917MODULE_DEVICE_TABLE(acpi, es8316_acpi_match);
918#endif
919
920static struct i2c_driver es8316_i2c_driver = {
921 .driver = {
922 .name = "es8316",
923 .acpi_match_table = ACPI_PTR(es8316_acpi_match),
924 .of_match_table = of_match_ptr(es8316_of_match),
925 },
926 .probe = es8316_i2c_probe,
927 .id_table = es8316_i2c_id,
928};
929module_i2c_driver(es8316_i2c_driver);
930
931MODULE_DESCRIPTION("Everest Semi ES8316 ALSA SoC Codec Driver");
932MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
933MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * es8316.c -- es8316 ALSA SoC audio driver
4 * Copyright Everest Semiconductor Co.,Ltd
5 *
6 * Authors: David Yang <yangxiaohua@everest-semi.com>,
7 * Daniel Drake <drake@endlessm.com>
8 */
9
10#include <linux/module.h>
11#include <linux/acpi.h>
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/i2c.h>
15#include <linux/mod_devicetable.h>
16#include <linux/mutex.h>
17#include <linux/regmap.h>
18#include <sound/pcm.h>
19#include <sound/pcm_params.h>
20#include <sound/soc.h>
21#include <sound/soc-dapm.h>
22#include <sound/tlv.h>
23#include <sound/jack.h>
24#include "es8316.h"
25
26/* In slave mode at single speed, the codec is documented as accepting 5
27 * MCLK/LRCK ratios, but we also add ratio 400, which is commonly used on
28 * Intel Cherry Trail platforms (19.2MHz MCLK, 48kHz LRCK).
29 */
30static const unsigned int supported_mclk_lrck_ratios[] = {
31 256, 384, 400, 500, 512, 768, 1024
32};
33
34struct es8316_priv {
35 struct mutex lock;
36 struct clk *mclk;
37 struct regmap *regmap;
38 struct snd_soc_component *component;
39 struct snd_soc_jack *jack;
40 int irq;
41 unsigned int sysclk;
42 unsigned int allowed_rates[ARRAY_SIZE(supported_mclk_lrck_ratios)];
43 struct snd_pcm_hw_constraint_list sysclk_constraints;
44 bool jd_inverted;
45};
46
47/*
48 * ES8316 controls
49 */
50static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9600, 50, 1);
51static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9600, 50, 1);
52static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_max_gain_tlv, -650, 150, 0);
53static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_min_gain_tlv, -1200, 150, 0);
54
55static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(alc_target_tlv,
56 0, 10, TLV_DB_SCALE_ITEM(-1650, 150, 0),
57 11, 11, TLV_DB_SCALE_ITEM(-150, 0, 0),
58);
59
60static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpmixer_gain_tlv,
61 0, 4, TLV_DB_SCALE_ITEM(-1200, 150, 0),
62 8, 11, TLV_DB_SCALE_ITEM(-450, 150, 0),
63);
64
65static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(adc_pga_gain_tlv,
66 0, 0, TLV_DB_SCALE_ITEM(-350, 0, 0),
67 1, 1, TLV_DB_SCALE_ITEM(0, 0, 0),
68 2, 2, TLV_DB_SCALE_ITEM(250, 0, 0),
69 3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
70 4, 7, TLV_DB_SCALE_ITEM(700, 300, 0),
71 8, 10, TLV_DB_SCALE_ITEM(1800, 300, 0),
72);
73
74static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpout_vol_tlv,
75 0, 0, TLV_DB_SCALE_ITEM(-4800, 0, 0),
76 1, 3, TLV_DB_SCALE_ITEM(-2400, 1200, 0),
77);
78
79static const char * const ng_type_txt[] =
80 { "Constant PGA Gain", "Mute ADC Output" };
81static const struct soc_enum ng_type =
82 SOC_ENUM_SINGLE(ES8316_ADC_ALC_NG, 6, 2, ng_type_txt);
83
84static const char * const adcpol_txt[] = { "Normal", "Invert" };
85static const struct soc_enum adcpol =
86 SOC_ENUM_SINGLE(ES8316_ADC_MUTE, 1, 2, adcpol_txt);
87static const char *const dacpol_txt[] =
88 { "Normal", "R Invert", "L Invert", "L + R Invert" };
89static const struct soc_enum dacpol =
90 SOC_ENUM_SINGLE(ES8316_DAC_SET1, 0, 4, dacpol_txt);
91
92static const struct snd_kcontrol_new es8316_snd_controls[] = {
93 SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL,
94 4, 0, 3, 1, hpout_vol_tlv),
95 SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL,
96 4, 0, 11, 0, hpmixer_gain_tlv),
97
98 SOC_ENUM("Playback Polarity", dacpol),
99 SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL,
100 ES8316_DAC_VOLR, 0, 0xc0, 1, dac_vol_tlv),
101 SOC_SINGLE("DAC Soft Ramp Switch", ES8316_DAC_SET1, 4, 1, 1),
102 SOC_SINGLE("DAC Soft Ramp Rate", ES8316_DAC_SET1, 2, 4, 0),
103 SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0),
104 SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0),
105 SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0),
106 SOC_SINGLE("DAC Mono Mix Switch", ES8316_DAC_SET3, 3, 1, 0),
107
108 SOC_ENUM("Capture Polarity", adcpol),
109 SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0),
110 SOC_SINGLE_TLV("ADC Capture Volume", ES8316_ADC_VOLUME,
111 0, 0xc0, 1, adc_vol_tlv),
112 SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8316_ADC_PGAGAIN,
113 4, 10, 0, adc_pga_gain_tlv),
114 SOC_SINGLE("ADC Soft Ramp Switch", ES8316_ADC_MUTE, 4, 1, 0),
115 SOC_SINGLE("ADC Double Fs Switch", ES8316_ADC_DMIC, 4, 1, 0),
116
117 SOC_SINGLE("ALC Capture Switch", ES8316_ADC_ALC1, 6, 1, 0),
118 SOC_SINGLE_TLV("ALC Capture Max Volume", ES8316_ADC_ALC1, 0, 28, 0,
119 alc_max_gain_tlv),
120 SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0,
121 alc_min_gain_tlv),
122 SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 11, 0,
123 alc_target_tlv),
124 SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3, 0, 10, 0),
125 SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4, 4, 10, 0),
126 SOC_SINGLE("ALC Capture Attack Time", ES8316_ADC_ALC4, 0, 10, 0),
127 SOC_SINGLE("ALC Capture Noise Gate Switch", ES8316_ADC_ALC_NG,
128 5, 1, 0),
129 SOC_SINGLE("ALC Capture Noise Gate Threshold", ES8316_ADC_ALC_NG,
130 0, 31, 0),
131 SOC_ENUM("ALC Capture Noise Gate Type", ng_type),
132};
133
134/* Analog Input Mux */
135static const char * const es8316_analog_in_txt[] = {
136 "lin1-rin1",
137 "lin2-rin2",
138 "lin1-rin1 with 20db Boost",
139 "lin2-rin2 with 20db Boost"
140};
141static const unsigned int es8316_analog_in_values[] = { 0, 1, 2, 3 };
142static const struct soc_enum es8316_analog_input_enum =
143 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_PDN_LINSEL, 4, 3,
144 ARRAY_SIZE(es8316_analog_in_txt),
145 es8316_analog_in_txt,
146 es8316_analog_in_values);
147static const struct snd_kcontrol_new es8316_analog_in_mux_controls =
148 SOC_DAPM_ENUM("Route", es8316_analog_input_enum);
149
150static const char * const es8316_dmic_txt[] = {
151 "dmic disable",
152 "dmic data at high level",
153 "dmic data at low level",
154};
155static const unsigned int es8316_dmic_values[] = { 0, 2, 3 };
156static const struct soc_enum es8316_dmic_src_enum =
157 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC, 0, 3,
158 ARRAY_SIZE(es8316_dmic_txt),
159 es8316_dmic_txt,
160 es8316_dmic_values);
161static const struct snd_kcontrol_new es8316_dmic_src_controls =
162 SOC_DAPM_ENUM("Route", es8316_dmic_src_enum);
163
164/* hp mixer mux */
165static const char * const es8316_hpmux_texts[] = {
166 "lin1-rin1",
167 "lin2-rin2",
168 "lin-rin with Boost",
169 "lin-rin with Boost and PGA"
170};
171
172static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL,
173 4, es8316_hpmux_texts);
174
175static const struct snd_kcontrol_new es8316_left_hpmux_controls =
176 SOC_DAPM_ENUM("Route", es8316_left_hpmux_enum);
177
178static SOC_ENUM_SINGLE_DECL(es8316_right_hpmux_enum, ES8316_HPMIX_SEL,
179 0, es8316_hpmux_texts);
180
181static const struct snd_kcontrol_new es8316_right_hpmux_controls =
182 SOC_DAPM_ENUM("Route", es8316_right_hpmux_enum);
183
184/* headphone Output Mixer */
185static const struct snd_kcontrol_new es8316_out_left_mix[] = {
186 SOC_DAPM_SINGLE("LLIN Switch", ES8316_HPMIX_SWITCH, 6, 1, 0),
187 SOC_DAPM_SINGLE("Left DAC Switch", ES8316_HPMIX_SWITCH, 7, 1, 0),
188};
189static const struct snd_kcontrol_new es8316_out_right_mix[] = {
190 SOC_DAPM_SINGLE("RLIN Switch", ES8316_HPMIX_SWITCH, 2, 1, 0),
191 SOC_DAPM_SINGLE("Right DAC Switch", ES8316_HPMIX_SWITCH, 3, 1, 0),
192};
193
194/* DAC data source mux */
195static const char * const es8316_dacsrc_texts[] = {
196 "LDATA TO LDAC, RDATA TO RDAC",
197 "LDATA TO LDAC, LDATA TO RDAC",
198 "RDATA TO LDAC, RDATA TO RDAC",
199 "RDATA TO LDAC, LDATA TO RDAC",
200};
201
202static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1,
203 6, es8316_dacsrc_texts);
204
205static const struct snd_kcontrol_new es8316_dacsrc_mux_controls =
206 SOC_DAPM_ENUM("Route", es8316_dacsrc_mux_enum);
207
208static const struct snd_soc_dapm_widget es8316_dapm_widgets[] = {
209 SND_SOC_DAPM_SUPPLY("Bias", ES8316_SYS_PDN, 3, 1, NULL, 0),
210 SND_SOC_DAPM_SUPPLY("Analog power", ES8316_SYS_PDN, 4, 1, NULL, 0),
211 SND_SOC_DAPM_SUPPLY("Mic Bias", ES8316_SYS_PDN, 5, 1, NULL, 0),
212
213 SND_SOC_DAPM_INPUT("DMIC"),
214 SND_SOC_DAPM_INPUT("MIC1"),
215 SND_SOC_DAPM_INPUT("MIC2"),
216
217 /* Input Mux */
218 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
219 &es8316_analog_in_mux_controls),
220
221 SND_SOC_DAPM_SUPPLY("ADC Vref", ES8316_SYS_PDN, 1, 1, NULL, 0),
222 SND_SOC_DAPM_SUPPLY("ADC bias", ES8316_SYS_PDN, 2, 1, NULL, 0),
223 SND_SOC_DAPM_SUPPLY("ADC Clock", ES8316_CLKMGR_CLKSW, 3, 0, NULL, 0),
224 SND_SOC_DAPM_PGA("Line input PGA", ES8316_ADC_PDN_LINSEL,
225 7, 1, NULL, 0),
226 SND_SOC_DAPM_ADC("Mono ADC", NULL, ES8316_ADC_PDN_LINSEL, 6, 1),
227 SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0,
228 &es8316_dmic_src_controls),
229
230 /* Digital Interface */
231 SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 1,
232 ES8316_SERDATA_ADC, 6, 1),
233 SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0,
234 SND_SOC_NOPM, 0, 0),
235
236 SND_SOC_DAPM_MUX("DAC Source Mux", SND_SOC_NOPM, 0, 0,
237 &es8316_dacsrc_mux_controls),
238
239 SND_SOC_DAPM_SUPPLY("DAC Vref", ES8316_SYS_PDN, 0, 1, NULL, 0),
240 SND_SOC_DAPM_SUPPLY("DAC Clock", ES8316_CLKMGR_CLKSW, 2, 0, NULL, 0),
241 SND_SOC_DAPM_DAC("Right DAC", NULL, ES8316_DAC_PDN, 0, 1),
242 SND_SOC_DAPM_DAC("Left DAC", NULL, ES8316_DAC_PDN, 4, 1),
243
244 /* Headphone Output Side */
245 SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
246 &es8316_left_hpmux_controls),
247 SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
248 &es8316_right_hpmux_controls),
249 SND_SOC_DAPM_MIXER("Left Headphone Mixer", ES8316_HPMIX_PDN,
250 5, 1, &es8316_out_left_mix[0],
251 ARRAY_SIZE(es8316_out_left_mix)),
252 SND_SOC_DAPM_MIXER("Right Headphone Mixer", ES8316_HPMIX_PDN,
253 1, 1, &es8316_out_right_mix[0],
254 ARRAY_SIZE(es8316_out_right_mix)),
255 SND_SOC_DAPM_PGA("Left Headphone Mixer Out", ES8316_HPMIX_PDN,
256 4, 1, NULL, 0),
257 SND_SOC_DAPM_PGA("Right Headphone Mixer Out", ES8316_HPMIX_PDN,
258 0, 1, NULL, 0),
259
260 SND_SOC_DAPM_OUT_DRV("Left Headphone Charge Pump", ES8316_CPHP_OUTEN,
261 6, 0, NULL, 0),
262 SND_SOC_DAPM_OUT_DRV("Right Headphone Charge Pump", ES8316_CPHP_OUTEN,
263 2, 0, NULL, 0),
264 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8316_CPHP_PDN2,
265 5, 1, NULL, 0),
266 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump Clock", ES8316_CLKMGR_CLKSW,
267 4, 0, NULL, 0),
268
269 SND_SOC_DAPM_OUT_DRV("Left Headphone Driver", ES8316_CPHP_OUTEN,
270 5, 0, NULL, 0),
271 SND_SOC_DAPM_OUT_DRV("Right Headphone Driver", ES8316_CPHP_OUTEN,
272 1, 0, NULL, 0),
273 SND_SOC_DAPM_SUPPLY("Headphone Out", ES8316_CPHP_PDN1, 2, 1, NULL, 0),
274
275 /* pdn_Lical and pdn_Rical bits are documented as Reserved, but must
276 * be explicitly unset in order to enable HP output
277 */
278 SND_SOC_DAPM_SUPPLY("Left Headphone ical", ES8316_CPHP_ICAL_VOL,
279 7, 1, NULL, 0),
280 SND_SOC_DAPM_SUPPLY("Right Headphone ical", ES8316_CPHP_ICAL_VOL,
281 3, 1, NULL, 0),
282
283 SND_SOC_DAPM_OUTPUT("HPOL"),
284 SND_SOC_DAPM_OUTPUT("HPOR"),
285};
286
287static const struct snd_soc_dapm_route es8316_dapm_routes[] = {
288 /* Recording */
289 {"MIC1", NULL, "Mic Bias"},
290 {"MIC2", NULL, "Mic Bias"},
291 {"MIC1", NULL, "Bias"},
292 {"MIC2", NULL, "Bias"},
293 {"MIC1", NULL, "Analog power"},
294 {"MIC2", NULL, "Analog power"},
295
296 {"Differential Mux", "lin1-rin1", "MIC1"},
297 {"Differential Mux", "lin2-rin2", "MIC2"},
298 {"Line input PGA", NULL, "Differential Mux"},
299
300 {"Mono ADC", NULL, "ADC Clock"},
301 {"Mono ADC", NULL, "ADC Vref"},
302 {"Mono ADC", NULL, "ADC bias"},
303 {"Mono ADC", NULL, "Line input PGA"},
304
305 /* It's not clear why, but to avoid recording only silence,
306 * the DAC clock must be running for the ADC to work.
307 */
308 {"Mono ADC", NULL, "DAC Clock"},
309
310 {"Digital Mic Mux", "dmic disable", "Mono ADC"},
311
312 {"I2S OUT", NULL, "Digital Mic Mux"},
313
314 /* Playback */
315 {"DAC Source Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
316
317 {"Left DAC", NULL, "DAC Clock"},
318 {"Right DAC", NULL, "DAC Clock"},
319
320 {"Left DAC", NULL, "DAC Vref"},
321 {"Right DAC", NULL, "DAC Vref"},
322
323 {"Left DAC", NULL, "DAC Source Mux"},
324 {"Right DAC", NULL, "DAC Source Mux"},
325
326 {"Left Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
327 {"Right Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
328
329 {"Left Headphone Mixer", "LLIN Switch", "Left Headphone Mux"},
330 {"Left Headphone Mixer", "Left DAC Switch", "Left DAC"},
331
332 {"Right Headphone Mixer", "RLIN Switch", "Right Headphone Mux"},
333 {"Right Headphone Mixer", "Right DAC Switch", "Right DAC"},
334
335 {"Left Headphone Mixer Out", NULL, "Left Headphone Mixer"},
336 {"Right Headphone Mixer Out", NULL, "Right Headphone Mixer"},
337
338 {"Left Headphone Charge Pump", NULL, "Left Headphone Mixer Out"},
339 {"Right Headphone Charge Pump", NULL, "Right Headphone Mixer Out"},
340
341 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump"},
342 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump"},
343
344 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
345 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
346
347 {"Left Headphone Driver", NULL, "Left Headphone Charge Pump"},
348 {"Right Headphone Driver", NULL, "Right Headphone Charge Pump"},
349
350 {"HPOL", NULL, "Left Headphone Driver"},
351 {"HPOR", NULL, "Right Headphone Driver"},
352
353 {"HPOL", NULL, "Left Headphone ical"},
354 {"HPOR", NULL, "Right Headphone ical"},
355
356 {"Headphone Out", NULL, "Bias"},
357 {"Headphone Out", NULL, "Analog power"},
358 {"HPOL", NULL, "Headphone Out"},
359 {"HPOR", NULL, "Headphone Out"},
360};
361
362static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai,
363 int clk_id, unsigned int freq, int dir)
364{
365 struct snd_soc_component *component = codec_dai->component;
366 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
367 int i, ret;
368 int count = 0;
369
370 es8316->sysclk = freq;
371 es8316->sysclk_constraints.list = NULL;
372 es8316->sysclk_constraints.count = 0;
373
374 if (freq == 0)
375 return 0;
376
377 ret = clk_set_rate(es8316->mclk, freq);
378 if (ret)
379 return ret;
380
381 /* Limit supported sample rates to ones that can be autodetected
382 * by the codec running in slave mode.
383 */
384 for (i = 0; i < ARRAY_SIZE(supported_mclk_lrck_ratios); i++) {
385 const unsigned int ratio = supported_mclk_lrck_ratios[i];
386
387 if (freq % ratio == 0)
388 es8316->allowed_rates[count++] = freq / ratio;
389 }
390
391 if (count) {
392 es8316->sysclk_constraints.list = es8316->allowed_rates;
393 es8316->sysclk_constraints.count = count;
394 }
395
396 return 0;
397}
398
399static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai,
400 unsigned int fmt)
401{
402 struct snd_soc_component *component = codec_dai->component;
403 u8 serdata1 = 0;
404 u8 serdata2 = 0;
405 u8 clksw;
406 u8 mask;
407
408 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBP_CFP)
409 serdata1 |= ES8316_SERDATA1_MASTER;
410
411 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) {
412 dev_err(component->dev, "Codec driver only supports I2S format\n");
413 return -EINVAL;
414 }
415
416 /* Clock inversion */
417 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
418 case SND_SOC_DAIFMT_NB_NF:
419 break;
420 case SND_SOC_DAIFMT_IB_IF:
421 serdata1 |= ES8316_SERDATA1_BCLK_INV;
422 serdata2 |= ES8316_SERDATA2_ADCLRP;
423 break;
424 case SND_SOC_DAIFMT_IB_NF:
425 serdata1 |= ES8316_SERDATA1_BCLK_INV;
426 break;
427 case SND_SOC_DAIFMT_NB_IF:
428 serdata2 |= ES8316_SERDATA2_ADCLRP;
429 break;
430 default:
431 return -EINVAL;
432 }
433
434 mask = ES8316_SERDATA1_MASTER | ES8316_SERDATA1_BCLK_INV;
435 snd_soc_component_update_bits(component, ES8316_SERDATA1, mask, serdata1);
436
437 mask = ES8316_SERDATA2_FMT_MASK | ES8316_SERDATA2_ADCLRP;
438 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, mask, serdata2);
439 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, mask, serdata2);
440
441 /* Enable BCLK and MCLK inputs in slave mode */
442 clksw = ES8316_CLKMGR_CLKSW_MCLK_ON | ES8316_CLKMGR_CLKSW_BCLK_ON;
443 snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, clksw, clksw);
444
445 return 0;
446}
447
448static int es8316_pcm_startup(struct snd_pcm_substream *substream,
449 struct snd_soc_dai *dai)
450{
451 struct snd_soc_component *component = dai->component;
452 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
453
454 if (es8316->sysclk_constraints.list)
455 snd_pcm_hw_constraint_list(substream->runtime, 0,
456 SNDRV_PCM_HW_PARAM_RATE,
457 &es8316->sysclk_constraints);
458
459 return 0;
460}
461
462static int es8316_pcm_hw_params(struct snd_pcm_substream *substream,
463 struct snd_pcm_hw_params *params,
464 struct snd_soc_dai *dai)
465{
466 struct snd_soc_component *component = dai->component;
467 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
468 u8 wordlen = 0;
469 u8 bclk_divider;
470 u16 lrck_divider;
471 int i;
472 unsigned int clk = es8316->sysclk / 2;
473 bool clk_valid = false;
474
475 /* We will start with halved sysclk and see if we can use it
476 * for proper clocking. This is to minimise the risk of running
477 * the CODEC with a too high frequency. We have an SKU where
478 * the sysclk frequency is 48Mhz and this causes the sound to be
479 * sped up. If we can run with a halved sysclk, we will use it,
480 * if we can't use it, then full sysclk will be used.
481 */
482 do {
483 /* Validate supported sample rates that are autodetected from MCLK */
484 for (i = 0; i < ARRAY_SIZE(supported_mclk_lrck_ratios); i++) {
485 const unsigned int ratio = supported_mclk_lrck_ratios[i];
486
487 if (clk % ratio != 0)
488 continue;
489 if (clk / ratio == params_rate(params))
490 break;
491 }
492 if (i == ARRAY_SIZE(supported_mclk_lrck_ratios)) {
493 if (clk == es8316->sysclk)
494 return -EINVAL;
495 clk = es8316->sysclk;
496 } else {
497 clk_valid = true;
498 }
499 } while (!clk_valid);
500
501 if (clk != es8316->sysclk) {
502 snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW,
503 ES8316_CLKMGR_CLKSW_MCLK_DIV,
504 ES8316_CLKMGR_CLKSW_MCLK_DIV);
505 }
506
507 lrck_divider = clk / params_rate(params);
508 bclk_divider = lrck_divider / 4;
509 switch (params_format(params)) {
510 case SNDRV_PCM_FORMAT_S16_LE:
511 wordlen = ES8316_SERDATA2_LEN_16;
512 bclk_divider /= 16;
513 break;
514 case SNDRV_PCM_FORMAT_S20_3LE:
515 wordlen = ES8316_SERDATA2_LEN_20;
516 bclk_divider /= 20;
517 break;
518 case SNDRV_PCM_FORMAT_S24_LE:
519 case SNDRV_PCM_FORMAT_S24_3LE:
520 wordlen = ES8316_SERDATA2_LEN_24;
521 bclk_divider /= 24;
522 break;
523 case SNDRV_PCM_FORMAT_S32_LE:
524 wordlen = ES8316_SERDATA2_LEN_32;
525 bclk_divider /= 32;
526 break;
527 default:
528 return -EINVAL;
529 }
530
531 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC,
532 ES8316_SERDATA2_LEN_MASK, wordlen);
533 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC,
534 ES8316_SERDATA2_LEN_MASK, wordlen);
535 snd_soc_component_update_bits(component, ES8316_SERDATA1, 0x1f, bclk_divider);
536 snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV1, 0x0f, lrck_divider >> 8);
537 snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV2, 0xff, lrck_divider & 0xff);
538 snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV1, 0x0f, lrck_divider >> 8);
539 snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV2, 0xff, lrck_divider & 0xff);
540 return 0;
541}
542
543static int es8316_mute(struct snd_soc_dai *dai, int mute, int direction)
544{
545 snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20,
546 mute ? 0x20 : 0);
547 return 0;
548}
549
550#define ES8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
551 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
552
553static const struct snd_soc_dai_ops es8316_ops = {
554 .startup = es8316_pcm_startup,
555 .hw_params = es8316_pcm_hw_params,
556 .set_fmt = es8316_set_dai_fmt,
557 .set_sysclk = es8316_set_dai_sysclk,
558 .mute_stream = es8316_mute,
559 .no_capture_mute = 1,
560};
561
562static struct snd_soc_dai_driver es8316_dai = {
563 .name = "ES8316 HiFi",
564 .playback = {
565 .stream_name = "Playback",
566 .channels_min = 1,
567 .channels_max = 2,
568 .rates = SNDRV_PCM_RATE_8000_48000,
569 .formats = ES8316_FORMATS,
570 },
571 .capture = {
572 .stream_name = "Capture",
573 .channels_min = 1,
574 .channels_max = 2,
575 .rates = SNDRV_PCM_RATE_8000_48000,
576 .formats = ES8316_FORMATS,
577 },
578 .ops = &es8316_ops,
579 .symmetric_rate = 1,
580};
581
582static void es8316_enable_micbias_for_mic_gnd_short_detect(
583 struct snd_soc_component *component)
584{
585 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
586
587 snd_soc_dapm_mutex_lock(dapm);
588 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Bias");
589 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Analog power");
590 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Mic Bias");
591 snd_soc_dapm_sync_unlocked(dapm);
592 snd_soc_dapm_mutex_unlock(dapm);
593
594 msleep(20);
595}
596
597static void es8316_disable_micbias_for_mic_gnd_short_detect(
598 struct snd_soc_component *component)
599{
600 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
601
602 snd_soc_dapm_mutex_lock(dapm);
603 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Bias");
604 snd_soc_dapm_disable_pin_unlocked(dapm, "Analog power");
605 snd_soc_dapm_disable_pin_unlocked(dapm, "Bias");
606 snd_soc_dapm_sync_unlocked(dapm);
607 snd_soc_dapm_mutex_unlock(dapm);
608}
609
610static irqreturn_t es8316_irq(int irq, void *data)
611{
612 struct es8316_priv *es8316 = data;
613 struct snd_soc_component *comp = es8316->component;
614 unsigned int flags;
615
616 mutex_lock(&es8316->lock);
617
618 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
619 if (flags == 0x00)
620 goto out; /* Powered-down / reset */
621
622 /* Catch spurious IRQ before set_jack is called */
623 if (!es8316->jack)
624 goto out;
625
626 if (es8316->jd_inverted)
627 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
628
629 dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
630 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
631 /* Jack removed, or spurious IRQ? */
632 if (es8316->jack->status & SND_JACK_MICROPHONE)
633 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
634
635 if (es8316->jack->status & SND_JACK_HEADPHONE) {
636 snd_soc_jack_report(es8316->jack, 0,
637 SND_JACK_HEADSET | SND_JACK_BTN_0);
638 dev_dbg(comp->dev, "jack unplugged\n");
639 }
640 } else if (!(es8316->jack->status & SND_JACK_HEADPHONE)) {
641 /* Jack inserted, determine type */
642 es8316_enable_micbias_for_mic_gnd_short_detect(comp);
643 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
644 if (es8316->jd_inverted)
645 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
646 dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
647 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
648 /* Jack unplugged underneath us */
649 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
650 } else if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
651 /* Open, headset */
652 snd_soc_jack_report(es8316->jack,
653 SND_JACK_HEADSET,
654 SND_JACK_HEADSET);
655 /* Keep mic-gnd-short detection on for button press */
656 } else {
657 /* Shorted, headphones */
658 snd_soc_jack_report(es8316->jack,
659 SND_JACK_HEADPHONE,
660 SND_JACK_HEADSET);
661 /* No longer need mic-gnd-short detection */
662 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
663 }
664 } else if (es8316->jack->status & SND_JACK_MICROPHONE) {
665 /* Interrupt while jack inserted, report button state */
666 if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
667 /* Open, button release */
668 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
669 } else {
670 /* Short, button press */
671 snd_soc_jack_report(es8316->jack,
672 SND_JACK_BTN_0,
673 SND_JACK_BTN_0);
674 }
675 }
676
677out:
678 mutex_unlock(&es8316->lock);
679 return IRQ_HANDLED;
680}
681
682static void es8316_enable_jack_detect(struct snd_soc_component *component,
683 struct snd_soc_jack *jack)
684{
685 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
686
687 /*
688 * Init es8316->jd_inverted here and not in the probe, as we cannot
689 * guarantee that the bytchr-es8316 driver, which might set this
690 * property, will probe before us.
691 */
692 es8316->jd_inverted = device_property_read_bool(component->dev,
693 "everest,jack-detect-inverted");
694
695 mutex_lock(&es8316->lock);
696
697 es8316->jack = jack;
698
699 if (es8316->jack->status & SND_JACK_MICROPHONE)
700 es8316_enable_micbias_for_mic_gnd_short_detect(component);
701
702 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
703 ES8316_GPIO_ENABLE_INTERRUPT,
704 ES8316_GPIO_ENABLE_INTERRUPT);
705
706 mutex_unlock(&es8316->lock);
707
708 /* Enable irq and sync initial jack state */
709 enable_irq(es8316->irq);
710 es8316_irq(es8316->irq, es8316);
711}
712
713static void es8316_disable_jack_detect(struct snd_soc_component *component)
714{
715 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
716
717 if (!es8316->jack)
718 return; /* Already disabled (or never enabled) */
719
720 disable_irq(es8316->irq);
721
722 mutex_lock(&es8316->lock);
723
724 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
725 ES8316_GPIO_ENABLE_INTERRUPT, 0);
726
727 if (es8316->jack->status & SND_JACK_MICROPHONE) {
728 es8316_disable_micbias_for_mic_gnd_short_detect(component);
729 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
730 }
731
732 es8316->jack = NULL;
733
734 mutex_unlock(&es8316->lock);
735}
736
737static int es8316_set_jack(struct snd_soc_component *component,
738 struct snd_soc_jack *jack, void *data)
739{
740 if (jack)
741 es8316_enable_jack_detect(component, jack);
742 else
743 es8316_disable_jack_detect(component);
744
745 return 0;
746}
747
748static int es8316_probe(struct snd_soc_component *component)
749{
750 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
751 int ret;
752
753 es8316->component = component;
754
755 es8316->mclk = devm_clk_get_optional(component->dev, "mclk");
756 if (IS_ERR(es8316->mclk)) {
757 dev_err(component->dev, "unable to get mclk\n");
758 return PTR_ERR(es8316->mclk);
759 }
760 if (!es8316->mclk)
761 dev_warn(component->dev, "assuming static mclk\n");
762
763 ret = clk_prepare_enable(es8316->mclk);
764 if (ret) {
765 dev_err(component->dev, "unable to enable mclk\n");
766 return ret;
767 }
768
769 /* Reset codec and enable current state machine */
770 snd_soc_component_write(component, ES8316_RESET, 0x3f);
771 usleep_range(5000, 5500);
772 snd_soc_component_write(component, ES8316_RESET, ES8316_RESET_CSM_ON);
773 msleep(30);
774
775 /*
776 * Documentation is unclear, but this value from the vendor driver is
777 * needed otherwise audio output is silent.
778 */
779 snd_soc_component_write(component, ES8316_SYS_VMIDSEL, 0xff);
780
781 /*
782 * Documentation for this register is unclear and incomplete,
783 * but here is a vendor-provided value that improves volume
784 * and quality for Intel CHT platforms.
785 */
786 snd_soc_component_write(component, ES8316_CLKMGR_ADCOSR, 0x32);
787
788 return 0;
789}
790
791static void es8316_remove(struct snd_soc_component *component)
792{
793 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
794
795 clk_disable_unprepare(es8316->mclk);
796}
797
798static int es8316_resume(struct snd_soc_component *component)
799{
800 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
801
802 regcache_cache_only(es8316->regmap, false);
803 regcache_sync(es8316->regmap);
804
805 return 0;
806}
807
808static int es8316_suspend(struct snd_soc_component *component)
809{
810 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
811
812 regcache_cache_only(es8316->regmap, true);
813 regcache_mark_dirty(es8316->regmap);
814
815 return 0;
816}
817
818static const struct snd_soc_component_driver soc_component_dev_es8316 = {
819 .probe = es8316_probe,
820 .remove = es8316_remove,
821 .resume = es8316_resume,
822 .suspend = es8316_suspend,
823 .set_jack = es8316_set_jack,
824 .controls = es8316_snd_controls,
825 .num_controls = ARRAY_SIZE(es8316_snd_controls),
826 .dapm_widgets = es8316_dapm_widgets,
827 .num_dapm_widgets = ARRAY_SIZE(es8316_dapm_widgets),
828 .dapm_routes = es8316_dapm_routes,
829 .num_dapm_routes = ARRAY_SIZE(es8316_dapm_routes),
830 .use_pmdown_time = 1,
831 .endianness = 1,
832};
833
834static bool es8316_volatile_reg(struct device *dev, unsigned int reg)
835{
836 switch (reg) {
837 case ES8316_GPIO_FLAG:
838 return true;
839 default:
840 return false;
841 }
842}
843
844static const struct regmap_config es8316_regmap = {
845 .reg_bits = 8,
846 .val_bits = 8,
847 .use_single_read = true,
848 .use_single_write = true,
849 .max_register = 0x53,
850 .volatile_reg = es8316_volatile_reg,
851 .cache_type = REGCACHE_MAPLE,
852};
853
854static int es8316_i2c_probe(struct i2c_client *i2c_client)
855{
856 struct device *dev = &i2c_client->dev;
857 struct es8316_priv *es8316;
858 int ret;
859
860 es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv),
861 GFP_KERNEL);
862 if (es8316 == NULL)
863 return -ENOMEM;
864
865 i2c_set_clientdata(i2c_client, es8316);
866
867 es8316->regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap);
868 if (IS_ERR(es8316->regmap))
869 return PTR_ERR(es8316->regmap);
870
871 es8316->irq = i2c_client->irq;
872 mutex_init(&es8316->lock);
873
874 if (es8316->irq > 0) {
875 ret = devm_request_threaded_irq(dev, es8316->irq, NULL, es8316_irq,
876 IRQF_TRIGGER_HIGH | IRQF_ONESHOT | IRQF_NO_AUTOEN,
877 "es8316", es8316);
878 if (ret) {
879 dev_warn(dev, "Failed to get IRQ %d: %d\n", es8316->irq, ret);
880 es8316->irq = -ENXIO;
881 }
882 }
883
884 return devm_snd_soc_register_component(&i2c_client->dev,
885 &soc_component_dev_es8316,
886 &es8316_dai, 1);
887}
888
889static const struct i2c_device_id es8316_i2c_id[] = {
890 {"es8316", 0 },
891 {}
892};
893MODULE_DEVICE_TABLE(i2c, es8316_i2c_id);
894
895#ifdef CONFIG_OF
896static const struct of_device_id es8316_of_match[] = {
897 { .compatible = "everest,es8316", },
898 {},
899};
900MODULE_DEVICE_TABLE(of, es8316_of_match);
901#endif
902
903#ifdef CONFIG_ACPI
904static const struct acpi_device_id es8316_acpi_match[] = {
905 {"ESSX8316", 0},
906 {"ESSX8336", 0},
907 {},
908};
909MODULE_DEVICE_TABLE(acpi, es8316_acpi_match);
910#endif
911
912static struct i2c_driver es8316_i2c_driver = {
913 .driver = {
914 .name = "es8316",
915 .acpi_match_table = ACPI_PTR(es8316_acpi_match),
916 .of_match_table = of_match_ptr(es8316_of_match),
917 },
918 .probe = es8316_i2c_probe,
919 .id_table = es8316_i2c_id,
920};
921module_i2c_driver(es8316_i2c_driver);
922
923MODULE_DESCRIPTION("Everest Semi ES8316 ALSA SoC Codec Driver");
924MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
925MODULE_LICENSE("GPL v2");