blob: 9ebe77c3784a8d5e44d4da1d26b58ee309b71e71 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * es8316.c -- es8316 ALSA SoC audio driver
3 * Copyright Everest Semiconductor Co.,Ltd
4 *
5 * Authors: David Yang <yangxiaohua@everest-semi.com>,
6 * Daniel Drake <drake@endlessm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/acpi.h>
15#include <linux/delay.h>
16#include <linux/i2c.h>
17#include <linux/mod_devicetable.h>
18#include <linux/regmap.h>
19#include <sound/pcm.h>
20#include <sound/pcm_params.h>
21#include <sound/soc.h>
22#include <sound/soc-dapm.h>
23#include <sound/tlv.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 */
30#define NR_SUPPORTED_MCLK_LRCK_RATIOS 6
31static const unsigned int supported_mclk_lrck_ratios[] = {
32 256, 384, 400, 512, 768, 1024
33};
34
35struct es8316_priv {
36 unsigned int sysclk;
37 unsigned int allowed_rates[NR_SUPPORTED_MCLK_LRCK_RATIOS];
38 struct snd_pcm_hw_constraint_list sysclk_constraints;
39};
40
41/*
42 * ES8316 controls
43 */
44static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9600, 50, 1);
45static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9600, 50, 1);
46static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_max_gain_tlv, -650, 150, 0);
47static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_min_gain_tlv, -1200, 150, 0);
48static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_target_tlv, -1650, 150, 0);
49static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpmixer_gain_tlv,
50 0, 4, TLV_DB_SCALE_ITEM(-1200, 150, 0),
51 8, 11, TLV_DB_SCALE_ITEM(-450, 150, 0),
52);
53
54static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(adc_pga_gain_tlv,
55 0, 0, TLV_DB_SCALE_ITEM(-350, 0, 0),
56 1, 1, TLV_DB_SCALE_ITEM(0, 0, 0),
57 2, 2, TLV_DB_SCALE_ITEM(250, 0, 0),
58 3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
59 4, 4, TLV_DB_SCALE_ITEM(700, 0, 0),
60 5, 5, TLV_DB_SCALE_ITEM(1000, 0, 0),
61 6, 6, TLV_DB_SCALE_ITEM(1300, 0, 0),
62 7, 7, TLV_DB_SCALE_ITEM(1600, 0, 0),
63 8, 8, TLV_DB_SCALE_ITEM(1800, 0, 0),
64 9, 9, TLV_DB_SCALE_ITEM(2100, 0, 0),
65 10, 10, TLV_DB_SCALE_ITEM(2400, 0, 0),
66);
67
68static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpout_vol_tlv,
69 0, 0, TLV_DB_SCALE_ITEM(-4800, 0, 0),
70 1, 3, TLV_DB_SCALE_ITEM(-2400, 1200, 0),
71);
72
73static const char * const ng_type_txt[] =
74 { "Constant PGA Gain", "Mute ADC Output" };
75static const struct soc_enum ng_type =
76 SOC_ENUM_SINGLE(ES8316_ADC_ALC_NG, 6, 2, ng_type_txt);
77
78static const char * const adcpol_txt[] = { "Normal", "Invert" };
79static const struct soc_enum adcpol =
80 SOC_ENUM_SINGLE(ES8316_ADC_MUTE, 1, 2, adcpol_txt);
81static const char *const dacpol_txt[] =
82 { "Normal", "R Invert", "L Invert", "L + R Invert" };
83static const struct soc_enum dacpol =
84 SOC_ENUM_SINGLE(ES8316_DAC_SET1, 0, 4, dacpol_txt);
85
86static const struct snd_kcontrol_new es8316_snd_controls[] = {
87 SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL,
88 4, 0, 3, 1, hpout_vol_tlv),
89 SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL,
90 0, 4, 11, 0, hpmixer_gain_tlv),
91
92 SOC_ENUM("Playback Polarity", dacpol),
93 SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL,
94 ES8316_DAC_VOLR, 0, 0xc0, 1, dac_vol_tlv),
95 SOC_SINGLE("DAC Soft Ramp Switch", ES8316_DAC_SET1, 4, 1, 1),
96 SOC_SINGLE("DAC Soft Ramp Rate", ES8316_DAC_SET1, 2, 4, 0),
97 SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0),
98 SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0),
99 SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0),
100
101 SOC_ENUM("Capture Polarity", adcpol),
102 SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0),
103 SOC_SINGLE_TLV("ADC Capture Volume", ES8316_ADC_VOLUME,
104 0, 0xc0, 1, adc_vol_tlv),
105 SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8316_ADC_PGAGAIN,
106 4, 10, 0, adc_pga_gain_tlv),
107 SOC_SINGLE("ADC Soft Ramp Switch", ES8316_ADC_MUTE, 4, 1, 0),
108 SOC_SINGLE("ADC Double Fs Switch", ES8316_ADC_DMIC, 4, 1, 0),
109
110 SOC_SINGLE("ALC Capture Switch", ES8316_ADC_ALC1, 6, 1, 0),
111 SOC_SINGLE_TLV("ALC Capture Max Volume", ES8316_ADC_ALC1, 0, 28, 0,
112 alc_max_gain_tlv),
113 SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0,
114 alc_min_gain_tlv),
115 SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 10, 0,
116 alc_target_tlv),
117 SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3, 0, 10, 0),
118 SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4, 4, 10, 0),
119 SOC_SINGLE("ALC Capture Attack Time", ES8316_ADC_ALC4, 0, 10, 0),
120 SOC_SINGLE("ALC Capture Noise Gate Switch", ES8316_ADC_ALC_NG,
121 5, 1, 0),
122 SOC_SINGLE("ALC Capture Noise Gate Threshold", ES8316_ADC_ALC_NG,
123 0, 31, 0),
124 SOC_ENUM("ALC Capture Noise Gate Type", ng_type),
125};
126
127/* Analog Input Mux */
128static const char * const es8316_analog_in_txt[] = {
129 "lin1-rin1",
130 "lin2-rin2",
131 "lin1-rin1 with 20db Boost",
132 "lin2-rin2 with 20db Boost"
133};
134static const unsigned int es8316_analog_in_values[] = { 0, 1, 2, 3 };
135static const struct soc_enum es8316_analog_input_enum =
136 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_PDN_LINSEL, 4, 3,
137 ARRAY_SIZE(es8316_analog_in_txt),
138 es8316_analog_in_txt,
139 es8316_analog_in_values);
140static const struct snd_kcontrol_new es8316_analog_in_mux_controls =
141 SOC_DAPM_ENUM("Route", es8316_analog_input_enum);
142
143static const char * const es8316_dmic_txt[] = {
144 "dmic disable",
145 "dmic data at high level",
146 "dmic data at low level",
147};
148static const unsigned int es8316_dmic_values[] = { 0, 1, 2 };
149static const struct soc_enum es8316_dmic_src_enum =
150 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC, 0, 3,
151 ARRAY_SIZE(es8316_dmic_txt),
152 es8316_dmic_txt,
153 es8316_dmic_values);
154static const struct snd_kcontrol_new es8316_dmic_src_controls =
155 SOC_DAPM_ENUM("Route", es8316_dmic_src_enum);
156
157/* hp mixer mux */
158static const char * const es8316_hpmux_texts[] = {
159 "lin1-rin1",
160 "lin2-rin2",
161 "lin-rin with Boost",
162 "lin-rin with Boost and PGA"
163};
164
165static const unsigned int es8316_hpmux_values[] = { 0, 1, 2, 3 };
166
167static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL,
168 4, es8316_hpmux_texts);
169
170static const struct snd_kcontrol_new es8316_left_hpmux_controls =
171 SOC_DAPM_ENUM("Route", es8316_left_hpmux_enum);
172
173static SOC_ENUM_SINGLE_DECL(es8316_right_hpmux_enum, ES8316_HPMIX_SEL,
174 0, es8316_hpmux_texts);
175
176static const struct snd_kcontrol_new es8316_right_hpmux_controls =
177 SOC_DAPM_ENUM("Route", es8316_right_hpmux_enum);
178
179/* headphone Output Mixer */
180static const struct snd_kcontrol_new es8316_out_left_mix[] = {
181 SOC_DAPM_SINGLE("LLIN Switch", ES8316_HPMIX_SWITCH, 6, 1, 0),
182 SOC_DAPM_SINGLE("Left DAC Switch", ES8316_HPMIX_SWITCH, 7, 1, 0),
183};
184static const struct snd_kcontrol_new es8316_out_right_mix[] = {
185 SOC_DAPM_SINGLE("RLIN Switch", ES8316_HPMIX_SWITCH, 2, 1, 0),
186 SOC_DAPM_SINGLE("Right DAC Switch", ES8316_HPMIX_SWITCH, 3, 1, 0),
187};
188
189/* DAC data source mux */
190static const char * const es8316_dacsrc_texts[] = {
191 "LDATA TO LDAC, RDATA TO RDAC",
192 "LDATA TO LDAC, LDATA TO RDAC",
193 "RDATA TO LDAC, RDATA TO RDAC",
194 "RDATA TO LDAC, LDATA TO RDAC",
195};
196
197static const unsigned int es8316_dacsrc_values[] = { 0, 1, 2, 3 };
198
199static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1,
200 6, es8316_dacsrc_texts);
201
202static const struct snd_kcontrol_new es8316_dacsrc_mux_controls =
203 SOC_DAPM_ENUM("Route", es8316_dacsrc_mux_enum);
204
205static const struct snd_soc_dapm_widget es8316_dapm_widgets[] = {
206 SND_SOC_DAPM_SUPPLY("Bias", ES8316_SYS_PDN, 3, 1, NULL, 0),
207 SND_SOC_DAPM_SUPPLY("Analog power", ES8316_SYS_PDN, 4, 1, NULL, 0),
208 SND_SOC_DAPM_SUPPLY("Mic Bias", ES8316_SYS_PDN, 5, 1, NULL, 0),
209
210 SND_SOC_DAPM_INPUT("DMIC"),
211 SND_SOC_DAPM_INPUT("MIC1"),
212 SND_SOC_DAPM_INPUT("MIC2"),
213
214 /* Input Mux */
215 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
216 &es8316_analog_in_mux_controls),
217
218 SND_SOC_DAPM_SUPPLY("ADC Vref", ES8316_SYS_PDN, 1, 1, NULL, 0),
219 SND_SOC_DAPM_SUPPLY("ADC bias", ES8316_SYS_PDN, 2, 1, NULL, 0),
220 SND_SOC_DAPM_SUPPLY("ADC Clock", ES8316_CLKMGR_CLKSW, 3, 0, NULL, 0),
221 SND_SOC_DAPM_PGA("Line input PGA", ES8316_ADC_PDN_LINSEL,
222 7, 1, NULL, 0),
223 SND_SOC_DAPM_ADC("Mono ADC", NULL, ES8316_ADC_PDN_LINSEL, 6, 1),
224 SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0,
225 &es8316_dmic_src_controls),
226
227 /* Digital Interface */
228 SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 1,
229 ES8316_SERDATA_ADC, 6, 1),
230 SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0,
231 SND_SOC_NOPM, 0, 0),
232
233 SND_SOC_DAPM_MUX("DAC Source Mux", SND_SOC_NOPM, 0, 0,
234 &es8316_dacsrc_mux_controls),
235
236 SND_SOC_DAPM_SUPPLY("DAC Vref", ES8316_SYS_PDN, 0, 1, NULL, 0),
237 SND_SOC_DAPM_SUPPLY("DAC Clock", ES8316_CLKMGR_CLKSW, 2, 0, NULL, 0),
238 SND_SOC_DAPM_DAC("Right DAC", NULL, ES8316_DAC_PDN, 0, 1),
239 SND_SOC_DAPM_DAC("Left DAC", NULL, ES8316_DAC_PDN, 4, 1),
240
241 /* Headphone Output Side */
242 SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
243 &es8316_left_hpmux_controls),
244 SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
245 &es8316_right_hpmux_controls),
246 SND_SOC_DAPM_MIXER("Left Headphone Mixer", ES8316_HPMIX_PDN,
247 5, 1, &es8316_out_left_mix[0],
248 ARRAY_SIZE(es8316_out_left_mix)),
249 SND_SOC_DAPM_MIXER("Right Headphone Mixer", ES8316_HPMIX_PDN,
250 1, 1, &es8316_out_right_mix[0],
251 ARRAY_SIZE(es8316_out_right_mix)),
252 SND_SOC_DAPM_PGA("Left Headphone Mixer Out", ES8316_HPMIX_PDN,
253 4, 1, NULL, 0),
254 SND_SOC_DAPM_PGA("Right Headphone Mixer Out", ES8316_HPMIX_PDN,
255 0, 1, NULL, 0),
256
257 SND_SOC_DAPM_OUT_DRV("Left Headphone Charge Pump", ES8316_CPHP_OUTEN,
258 6, 0, NULL, 0),
259 SND_SOC_DAPM_OUT_DRV("Right Headphone Charge Pump", ES8316_CPHP_OUTEN,
260 2, 0, NULL, 0),
261 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8316_CPHP_PDN2,
262 5, 1, NULL, 0),
263 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump Clock", ES8316_CLKMGR_CLKSW,
264 4, 0, NULL, 0),
265
266 SND_SOC_DAPM_OUT_DRV("Left Headphone Driver", ES8316_CPHP_OUTEN,
267 5, 0, NULL, 0),
268 SND_SOC_DAPM_OUT_DRV("Right Headphone Driver", ES8316_CPHP_OUTEN,
269 1, 0, NULL, 0),
270 SND_SOC_DAPM_SUPPLY("Headphone Out", ES8316_CPHP_PDN1, 2, 1, NULL, 0),
271
272 /* pdn_Lical and pdn_Rical bits are documented as Reserved, but must
273 * be explicitly unset in order to enable HP output
274 */
275 SND_SOC_DAPM_SUPPLY("Left Headphone ical", ES8316_CPHP_ICAL_VOL,
276 7, 1, NULL, 0),
277 SND_SOC_DAPM_SUPPLY("Right Headphone ical", ES8316_CPHP_ICAL_VOL,
278 3, 1, NULL, 0),
279
280 SND_SOC_DAPM_OUTPUT("HPOL"),
281 SND_SOC_DAPM_OUTPUT("HPOR"),
282};
283
284static const struct snd_soc_dapm_route es8316_dapm_routes[] = {
285 /* Recording */
286 {"MIC1", NULL, "Mic Bias"},
287 {"MIC2", NULL, "Mic Bias"},
288 {"MIC1", NULL, "Bias"},
289 {"MIC2", NULL, "Bias"},
290 {"MIC1", NULL, "Analog power"},
291 {"MIC2", NULL, "Analog power"},
292
293 {"Differential Mux", "lin1-rin1", "MIC1"},
294 {"Differential Mux", "lin2-rin2", "MIC2"},
295 {"Line input PGA", NULL, "Differential Mux"},
296
297 {"Mono ADC", NULL, "ADC Clock"},
298 {"Mono ADC", NULL, "ADC Vref"},
299 {"Mono ADC", NULL, "ADC bias"},
300 {"Mono ADC", NULL, "Line input PGA"},
301
302 /* It's not clear why, but to avoid recording only silence,
303 * the DAC clock must be running for the ADC to work.
304 */
305 {"Mono ADC", NULL, "DAC Clock"},
306
307 {"Digital Mic Mux", "dmic disable", "Mono ADC"},
308
309 {"I2S OUT", NULL, "Digital Mic Mux"},
310
311 /* Playback */
312 {"DAC Source Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
313
314 {"Left DAC", NULL, "DAC Clock"},
315 {"Right DAC", NULL, "DAC Clock"},
316
317 {"Left DAC", NULL, "DAC Vref"},
318 {"Right DAC", NULL, "DAC Vref"},
319
320 {"Left DAC", NULL, "DAC Source Mux"},
321 {"Right DAC", NULL, "DAC Source Mux"},
322
323 {"Left Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
324 {"Right Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
325
326 {"Left Headphone Mixer", "LLIN Switch", "Left Headphone Mux"},
327 {"Left Headphone Mixer", "Left DAC Switch", "Left DAC"},
328
329 {"Right Headphone Mixer", "RLIN Switch", "Right Headphone Mux"},
330 {"Right Headphone Mixer", "Right DAC Switch", "Right DAC"},
331
332 {"Left Headphone Mixer Out", NULL, "Left Headphone Mixer"},
333 {"Right Headphone Mixer Out", NULL, "Right Headphone Mixer"},
334
335 {"Left Headphone Charge Pump", NULL, "Left Headphone Mixer Out"},
336 {"Right Headphone Charge Pump", NULL, "Right Headphone Mixer Out"},
337
338 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump"},
339 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump"},
340
341 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
342 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
343
344 {"Left Headphone Driver", NULL, "Left Headphone Charge Pump"},
345 {"Right Headphone Driver", NULL, "Right Headphone Charge Pump"},
346
347 {"HPOL", NULL, "Left Headphone Driver"},
348 {"HPOR", NULL, "Right Headphone Driver"},
349
350 {"HPOL", NULL, "Left Headphone ical"},
351 {"HPOR", NULL, "Right Headphone ical"},
352
353 {"Headphone Out", NULL, "Bias"},
354 {"Headphone Out", NULL, "Analog power"},
355 {"HPOL", NULL, "Headphone Out"},
356 {"HPOR", NULL, "Headphone Out"},
357};
358
359static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai,
360 int clk_id, unsigned int freq, int dir)
361{
362 struct snd_soc_component *component = codec_dai->component;
363 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
364 int i;
365 int count = 0;
366
367 es8316->sysclk = freq;
368
369 if (freq == 0)
370 return 0;
371
372 /* Limit supported sample rates to ones that can be autodetected
373 * by the codec running in slave mode.
374 */
375 for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
376 const unsigned int ratio = supported_mclk_lrck_ratios[i];
377
378 if (freq % ratio == 0)
379 es8316->allowed_rates[count++] = freq / ratio;
380 }
381
382 es8316->sysclk_constraints.list = es8316->allowed_rates;
383 es8316->sysclk_constraints.count = count;
384
385 return 0;
386}
387
388static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai,
389 unsigned int fmt)
390{
391 struct snd_soc_component *component = codec_dai->component;
392 u8 serdata1 = 0;
393 u8 serdata2 = 0;
394 u8 clksw;
395 u8 mask;
396
397 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) {
398 dev_err(component->dev, "Codec driver only supports slave mode\n");
399 return -EINVAL;
400 }
401
402 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) {
403 dev_err(component->dev, "Codec driver only supports I2S format\n");
404 return -EINVAL;
405 }
406
407 /* Clock inversion */
408 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
409 case SND_SOC_DAIFMT_NB_NF:
410 break;
411 case SND_SOC_DAIFMT_IB_IF:
412 serdata1 |= ES8316_SERDATA1_BCLK_INV;
413 serdata2 |= ES8316_SERDATA2_ADCLRP;
414 break;
415 case SND_SOC_DAIFMT_IB_NF:
416 serdata1 |= ES8316_SERDATA1_BCLK_INV;
417 break;
418 case SND_SOC_DAIFMT_NB_IF:
419 serdata2 |= ES8316_SERDATA2_ADCLRP;
420 break;
421 default:
422 return -EINVAL;
423 }
424
425 mask = ES8316_SERDATA1_MASTER | ES8316_SERDATA1_BCLK_INV;
426 snd_soc_component_update_bits(component, ES8316_SERDATA1, mask, serdata1);
427
428 mask = ES8316_SERDATA2_FMT_MASK | ES8316_SERDATA2_ADCLRP;
429 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, mask, serdata2);
430 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, mask, serdata2);
431
432 /* Enable BCLK and MCLK inputs in slave mode */
433 clksw = ES8316_CLKMGR_CLKSW_MCLK_ON | ES8316_CLKMGR_CLKSW_BCLK_ON;
434 snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, clksw, clksw);
435
436 return 0;
437}
438
439static int es8316_pcm_startup(struct snd_pcm_substream *substream,
440 struct snd_soc_dai *dai)
441{
442 struct snd_soc_component *component = dai->component;
443 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
444
445 if (es8316->sysclk == 0) {
446 dev_err(component->dev, "No sysclk provided\n");
447 return -EINVAL;
448 }
449
450 /* The set of sample rates that can be supported depends on the
451 * MCLK supplied to the CODEC.
452 */
453 snd_pcm_hw_constraint_list(substream->runtime, 0,
454 SNDRV_PCM_HW_PARAM_RATE,
455 &es8316->sysclk_constraints);
456
457 return 0;
458}
459
460static int es8316_pcm_hw_params(struct snd_pcm_substream *substream,
461 struct snd_pcm_hw_params *params,
462 struct snd_soc_dai *dai)
463{
464 struct snd_soc_component *component = dai->component;
465 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
466 u8 wordlen = 0;
467
468 if (!es8316->sysclk) {
469 dev_err(component->dev, "No MCLK configured\n");
470 return -EINVAL;
471 }
472
473 switch (params_format(params)) {
474 case SNDRV_PCM_FORMAT_S16_LE:
475 wordlen = ES8316_SERDATA2_LEN_16;
476 break;
477 case SNDRV_PCM_FORMAT_S20_3LE:
478 wordlen = ES8316_SERDATA2_LEN_20;
479 break;
480 case SNDRV_PCM_FORMAT_S24_LE:
481 wordlen = ES8316_SERDATA2_LEN_24;
482 break;
483 case SNDRV_PCM_FORMAT_S32_LE:
484 wordlen = ES8316_SERDATA2_LEN_32;
485 break;
486 default:
487 return -EINVAL;
488 }
489
490 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC,
491 ES8316_SERDATA2_LEN_MASK, wordlen);
492 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC,
493 ES8316_SERDATA2_LEN_MASK, wordlen);
494 return 0;
495}
496
497static int es8316_mute(struct snd_soc_dai *dai, int mute)
498{
499 snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20,
500 mute ? 0x20 : 0);
501 return 0;
502}
503
504#define ES8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
505 SNDRV_PCM_FMTBIT_S24_LE)
506
507static const struct snd_soc_dai_ops es8316_ops = {
508 .startup = es8316_pcm_startup,
509 .hw_params = es8316_pcm_hw_params,
510 .set_fmt = es8316_set_dai_fmt,
511 .set_sysclk = es8316_set_dai_sysclk,
512 .digital_mute = es8316_mute,
513};
514
515static struct snd_soc_dai_driver es8316_dai = {
516 .name = "ES8316 HiFi",
517 .playback = {
518 .stream_name = "Playback",
519 .channels_min = 1,
520 .channels_max = 2,
521 .rates = SNDRV_PCM_RATE_8000_48000,
522 .formats = ES8316_FORMATS,
523 },
524 .capture = {
525 .stream_name = "Capture",
526 .channels_min = 1,
527 .channels_max = 2,
528 .rates = SNDRV_PCM_RATE_8000_48000,
529 .formats = ES8316_FORMATS,
530 },
531 .ops = &es8316_ops,
532 .symmetric_rates = 1,
533};
534
535static int es8316_probe(struct snd_soc_component *component)
536{
537 /* Reset codec and enable current state machine */
538 snd_soc_component_write(component, ES8316_RESET, 0x3f);
539 usleep_range(5000, 5500);
540 snd_soc_component_write(component, ES8316_RESET, ES8316_RESET_CSM_ON);
541 msleep(30);
542
543 /*
544 * Documentation is unclear, but this value from the vendor driver is
545 * needed otherwise audio output is silent.
546 */
547 snd_soc_component_write(component, ES8316_SYS_VMIDSEL, 0xff);
548
549 /*
550 * Documentation for this register is unclear and incomplete,
551 * but here is a vendor-provided value that improves volume
552 * and quality for Intel CHT platforms.
553 */
554 snd_soc_component_write(component, ES8316_CLKMGR_ADCOSR, 0x32);
555
556 return 0;
557}
558
559static const struct snd_soc_component_driver soc_component_dev_es8316 = {
560 .probe = es8316_probe,
561 .controls = es8316_snd_controls,
562 .num_controls = ARRAY_SIZE(es8316_snd_controls),
563 .dapm_widgets = es8316_dapm_widgets,
564 .num_dapm_widgets = ARRAY_SIZE(es8316_dapm_widgets),
565 .dapm_routes = es8316_dapm_routes,
566 .num_dapm_routes = ARRAY_SIZE(es8316_dapm_routes),
567 .use_pmdown_time = 1,
568 .endianness = 1,
569 .non_legacy_dai_naming = 1,
570};
571
572static const struct regmap_config es8316_regmap = {
573 .reg_bits = 8,
574 .val_bits = 8,
575 .max_register = 0x53,
576 .cache_type = REGCACHE_RBTREE,
577};
578
579static int es8316_i2c_probe(struct i2c_client *i2c_client,
580 const struct i2c_device_id *id)
581{
582 struct es8316_priv *es8316;
583 struct regmap *regmap;
584
585 es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv),
586 GFP_KERNEL);
587 if (es8316 == NULL)
588 return -ENOMEM;
589
590 i2c_set_clientdata(i2c_client, es8316);
591
592 regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap);
593 if (IS_ERR(regmap))
594 return PTR_ERR(regmap);
595
596 return devm_snd_soc_register_component(&i2c_client->dev,
597 &soc_component_dev_es8316,
598 &es8316_dai, 1);
599}
600
601static const struct i2c_device_id es8316_i2c_id[] = {
602 {"es8316", 0 },
603 {}
604};
605MODULE_DEVICE_TABLE(i2c, es8316_i2c_id);
606
607static const struct of_device_id es8316_of_match[] = {
608 { .compatible = "everest,es8316", },
609 {},
610};
611MODULE_DEVICE_TABLE(of, es8316_of_match);
612
613static const struct acpi_device_id es8316_acpi_match[] = {
614 {"ESSX8316", 0},
615 {},
616};
617MODULE_DEVICE_TABLE(acpi, es8316_acpi_match);
618
619static struct i2c_driver es8316_i2c_driver = {
620 .driver = {
621 .name = "es8316",
622 .acpi_match_table = ACPI_PTR(es8316_acpi_match),
623 .of_match_table = of_match_ptr(es8316_of_match),
624 },
625 .probe = es8316_i2c_probe,
626 .id_table = es8316_i2c_id,
627};
628module_i2c_driver(es8316_i2c_driver);
629
630MODULE_DESCRIPTION("Everest Semi ES8316 ALSA SoC Codec Driver");
631MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
632MODULE_LICENSE("GPL v2");