Mention branches and keyring.
[releases.git] / codecs / nau8540.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NAU85L40 ALSA SoC audio driver
4  *
5  * Copyright 2016 Nuvoton Technology Corp.
6  * Author: John Hsu <KCHSU0@nuvoton.com>
7  */
8
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/pm.h>
14 #include <linux/i2c.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/spi/spi.h>
18 #include <linux/slab.h>
19 #include <linux/of_device.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include <sound/soc-dapm.h>
25 #include <sound/initval.h>
26 #include <sound/tlv.h>
27 #include "nau8540.h"
28
29
30 #define NAU_FREF_MAX 13500000
31 #define NAU_FVCO_MAX 100000000
32 #define NAU_FVCO_MIN 90000000
33
34 /* the maximum frequency of CLK_ADC */
35 #define CLK_ADC_MAX 6144000
36
37 /* scaling for mclk from sysclk_src output */
38 static const struct nau8540_fll_attr mclk_src_scaling[] = {
39         { 1, 0x0 },
40         { 2, 0x2 },
41         { 4, 0x3 },
42         { 8, 0x4 },
43         { 16, 0x5 },
44         { 32, 0x6 },
45         { 3, 0x7 },
46         { 6, 0xa },
47         { 12, 0xb },
48         { 24, 0xc },
49 };
50
51 /* ratio for input clk freq */
52 static const struct nau8540_fll_attr fll_ratio[] = {
53         { 512000, 0x01 },
54         { 256000, 0x02 },
55         { 128000, 0x04 },
56         { 64000, 0x08 },
57         { 32000, 0x10 },
58         { 8000, 0x20 },
59         { 4000, 0x40 },
60 };
61
62 static const struct nau8540_fll_attr fll_pre_scalar[] = {
63         { 1, 0x0 },
64         { 2, 0x1 },
65         { 4, 0x2 },
66         { 8, 0x3 },
67 };
68
69 /* over sampling rate */
70 static const struct nau8540_osr_attr osr_adc_sel[] = {
71         { 32, 3 },      /* OSR 32, SRC 1/8 */
72         { 64, 2 },      /* OSR 64, SRC 1/4 */
73         { 128, 1 },     /* OSR 128, SRC 1/2 */
74         { 256, 0 },     /* OSR 256, SRC 1 */
75 };
76
77 static const struct reg_default nau8540_reg_defaults[] = {
78         {NAU8540_REG_POWER_MANAGEMENT, 0x0000},
79         {NAU8540_REG_CLOCK_CTRL, 0x0000},
80         {NAU8540_REG_CLOCK_SRC, 0x0000},
81         {NAU8540_REG_FLL1, 0x0001},
82         {NAU8540_REG_FLL2, 0x3126},
83         {NAU8540_REG_FLL3, 0x0008},
84         {NAU8540_REG_FLL4, 0x0010},
85         {NAU8540_REG_FLL5, 0xC000},
86         {NAU8540_REG_FLL6, 0x6000},
87         {NAU8540_REG_FLL_VCO_RSV, 0xF13C},
88         {NAU8540_REG_PCM_CTRL0, 0x000B},
89         {NAU8540_REG_PCM_CTRL1, 0x3010},
90         {NAU8540_REG_PCM_CTRL2, 0x0800},
91         {NAU8540_REG_PCM_CTRL3, 0x0000},
92         {NAU8540_REG_PCM_CTRL4, 0x000F},
93         {NAU8540_REG_ALC_CONTROL_1, 0x0000},
94         {NAU8540_REG_ALC_CONTROL_2, 0x700B},
95         {NAU8540_REG_ALC_CONTROL_3, 0x0022},
96         {NAU8540_REG_ALC_CONTROL_4, 0x1010},
97         {NAU8540_REG_ALC_CONTROL_5, 0x1010},
98         {NAU8540_REG_NOTCH_FIL1_CH1, 0x0000},
99         {NAU8540_REG_NOTCH_FIL2_CH1, 0x0000},
100         {NAU8540_REG_NOTCH_FIL1_CH2, 0x0000},
101         {NAU8540_REG_NOTCH_FIL2_CH2, 0x0000},
102         {NAU8540_REG_NOTCH_FIL1_CH3, 0x0000},
103         {NAU8540_REG_NOTCH_FIL2_CH3, 0x0000},
104         {NAU8540_REG_NOTCH_FIL1_CH4, 0x0000},
105         {NAU8540_REG_NOTCH_FIL2_CH4, 0x0000},
106         {NAU8540_REG_HPF_FILTER_CH12, 0x0000},
107         {NAU8540_REG_HPF_FILTER_CH34, 0x0000},
108         {NAU8540_REG_ADC_SAMPLE_RATE, 0x0002},
109         {NAU8540_REG_DIGITAL_GAIN_CH1, 0x0400},
110         {NAU8540_REG_DIGITAL_GAIN_CH2, 0x0400},
111         {NAU8540_REG_DIGITAL_GAIN_CH3, 0x0400},
112         {NAU8540_REG_DIGITAL_GAIN_CH4, 0x0400},
113         {NAU8540_REG_DIGITAL_MUX, 0x00E4},
114         {NAU8540_REG_GPIO_CTRL, 0x0000},
115         {NAU8540_REG_MISC_CTRL, 0x0000},
116         {NAU8540_REG_I2C_CTRL, 0xEFFF},
117         {NAU8540_REG_VMID_CTRL, 0x0000},
118         {NAU8540_REG_MUTE, 0x0000},
119         {NAU8540_REG_ANALOG_ADC1, 0x0011},
120         {NAU8540_REG_ANALOG_ADC2, 0x0020},
121         {NAU8540_REG_ANALOG_PWR, 0x0000},
122         {NAU8540_REG_MIC_BIAS, 0x0004},
123         {NAU8540_REG_REFERENCE, 0x0000},
124         {NAU8540_REG_FEPGA1, 0x0000},
125         {NAU8540_REG_FEPGA2, 0x0000},
126         {NAU8540_REG_FEPGA3, 0x0101},
127         {NAU8540_REG_FEPGA4, 0x0101},
128         {NAU8540_REG_PWR, 0x0000},
129 };
130
131 static bool nau8540_readable_reg(struct device *dev, unsigned int reg)
132 {
133         switch (reg) {
134         case NAU8540_REG_POWER_MANAGEMENT ... NAU8540_REG_FLL_VCO_RSV:
135         case NAU8540_REG_PCM_CTRL0 ... NAU8540_REG_PCM_CTRL4:
136         case NAU8540_REG_ALC_CONTROL_1 ... NAU8540_REG_ALC_CONTROL_5:
137         case NAU8540_REG_ALC_GAIN_CH12 ... NAU8540_REG_ADC_SAMPLE_RATE:
138         case NAU8540_REG_DIGITAL_GAIN_CH1 ... NAU8540_REG_DIGITAL_MUX:
139         case NAU8540_REG_P2P_CH1 ... NAU8540_REG_I2C_CTRL:
140         case NAU8540_REG_I2C_DEVICE_ID:
141         case NAU8540_REG_VMID_CTRL ... NAU8540_REG_MUTE:
142         case NAU8540_REG_ANALOG_ADC1 ... NAU8540_REG_PWR:
143                 return true;
144         default:
145                 return false;
146         }
147
148 }
149
150 static bool nau8540_writeable_reg(struct device *dev, unsigned int reg)
151 {
152         switch (reg) {
153         case NAU8540_REG_SW_RESET ... NAU8540_REG_FLL_VCO_RSV:
154         case NAU8540_REG_PCM_CTRL0 ... NAU8540_REG_PCM_CTRL4:
155         case NAU8540_REG_ALC_CONTROL_1 ... NAU8540_REG_ALC_CONTROL_5:
156         case NAU8540_REG_NOTCH_FIL1_CH1 ... NAU8540_REG_ADC_SAMPLE_RATE:
157         case NAU8540_REG_DIGITAL_GAIN_CH1 ... NAU8540_REG_DIGITAL_MUX:
158         case NAU8540_REG_GPIO_CTRL ... NAU8540_REG_I2C_CTRL:
159         case NAU8540_REG_RST:
160         case NAU8540_REG_VMID_CTRL ... NAU8540_REG_MUTE:
161         case NAU8540_REG_ANALOG_ADC1 ... NAU8540_REG_PWR:
162                 return true;
163         default:
164                 return false;
165         }
166 }
167
168 static bool nau8540_volatile_reg(struct device *dev, unsigned int reg)
169 {
170         switch (reg) {
171         case NAU8540_REG_SW_RESET:
172         case NAU8540_REG_ALC_GAIN_CH12 ... NAU8540_REG_ALC_STATUS:
173         case NAU8540_REG_P2P_CH1 ... NAU8540_REG_PEAK_CH4:
174         case NAU8540_REG_I2C_DEVICE_ID:
175         case NAU8540_REG_RST:
176                 return true;
177         default:
178                 return false;
179         }
180 }
181
182
183 static const DECLARE_TLV_DB_MINMAX(adc_vol_tlv, -12800, 3600);
184 static const DECLARE_TLV_DB_MINMAX(fepga_gain_tlv, -100, 3600);
185
186 static const struct snd_kcontrol_new nau8540_snd_controls[] = {
187         SOC_SINGLE_TLV("Mic1 Volume", NAU8540_REG_DIGITAL_GAIN_CH1,
188                 0, 0x520, 0, adc_vol_tlv),
189         SOC_SINGLE_TLV("Mic2 Volume", NAU8540_REG_DIGITAL_GAIN_CH2,
190                 0, 0x520, 0, adc_vol_tlv),
191         SOC_SINGLE_TLV("Mic3 Volume", NAU8540_REG_DIGITAL_GAIN_CH3,
192                 0, 0x520, 0, adc_vol_tlv),
193         SOC_SINGLE_TLV("Mic4 Volume", NAU8540_REG_DIGITAL_GAIN_CH4,
194                 0, 0x520, 0, adc_vol_tlv),
195
196         SOC_SINGLE_TLV("Frontend PGA1 Volume", NAU8540_REG_FEPGA3,
197                 0, 0x25, 0, fepga_gain_tlv),
198         SOC_SINGLE_TLV("Frontend PGA2 Volume", NAU8540_REG_FEPGA3,
199                 8, 0x25, 0, fepga_gain_tlv),
200         SOC_SINGLE_TLV("Frontend PGA3 Volume", NAU8540_REG_FEPGA4,
201                 0, 0x25, 0, fepga_gain_tlv),
202         SOC_SINGLE_TLV("Frontend PGA4 Volume", NAU8540_REG_FEPGA4,
203                 8, 0x25, 0, fepga_gain_tlv),
204 };
205
206 static const char * const adc_channel[] = {
207         "ADC channel 1", "ADC channel 2", "ADC channel 3", "ADC channel 4"
208 };
209 static SOC_ENUM_SINGLE_DECL(
210         digital_ch4_enum, NAU8540_REG_DIGITAL_MUX, 6, adc_channel);
211
212 static const struct snd_kcontrol_new digital_ch4_mux =
213         SOC_DAPM_ENUM("Digital CH4 Select", digital_ch4_enum);
214
215 static SOC_ENUM_SINGLE_DECL(
216         digital_ch3_enum, NAU8540_REG_DIGITAL_MUX, 4, adc_channel);
217
218 static const struct snd_kcontrol_new digital_ch3_mux =
219         SOC_DAPM_ENUM("Digital CH3 Select", digital_ch3_enum);
220
221 static SOC_ENUM_SINGLE_DECL(
222         digital_ch2_enum, NAU8540_REG_DIGITAL_MUX, 2, adc_channel);
223
224 static const struct snd_kcontrol_new digital_ch2_mux =
225         SOC_DAPM_ENUM("Digital CH2 Select", digital_ch2_enum);
226
227 static SOC_ENUM_SINGLE_DECL(
228         digital_ch1_enum, NAU8540_REG_DIGITAL_MUX, 0, adc_channel);
229
230 static const struct snd_kcontrol_new digital_ch1_mux =
231         SOC_DAPM_ENUM("Digital CH1 Select", digital_ch1_enum);
232
233 static int adc_power_control(struct snd_soc_dapm_widget *w,
234                 struct snd_kcontrol *k, int  event)
235 {
236         struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
237         struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
238
239         if (SND_SOC_DAPM_EVENT_ON(event)) {
240                 msleep(300);
241                 /* DO12 and DO34 pad output enable */
242                 regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL1,
243                         NAU8540_I2S_DO12_TRI, 0);
244                 regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL2,
245                         NAU8540_I2S_DO34_TRI, 0);
246         } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
247                 regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL1,
248                         NAU8540_I2S_DO12_TRI, NAU8540_I2S_DO12_TRI);
249                 regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL2,
250                         NAU8540_I2S_DO34_TRI, NAU8540_I2S_DO34_TRI);
251         }
252         return 0;
253 }
254
255 static int aiftx_power_control(struct snd_soc_dapm_widget *w,
256                 struct snd_kcontrol *k, int  event)
257 {
258         struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
259         struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
260
261         if (SND_SOC_DAPM_EVENT_OFF(event)) {
262                 regmap_write(nau8540->regmap, NAU8540_REG_RST, 0x0001);
263                 regmap_write(nau8540->regmap, NAU8540_REG_RST, 0x0000);
264         }
265         return 0;
266 }
267
268 static const struct snd_soc_dapm_widget nau8540_dapm_widgets[] = {
269         SND_SOC_DAPM_SUPPLY("MICBIAS2", NAU8540_REG_MIC_BIAS, 11, 0, NULL, 0),
270         SND_SOC_DAPM_SUPPLY("MICBIAS1", NAU8540_REG_MIC_BIAS, 10, 0, NULL, 0),
271
272         SND_SOC_DAPM_INPUT("MIC1"),
273         SND_SOC_DAPM_INPUT("MIC2"),
274         SND_SOC_DAPM_INPUT("MIC3"),
275         SND_SOC_DAPM_INPUT("MIC4"),
276
277         SND_SOC_DAPM_PGA("Frontend PGA1", NAU8540_REG_PWR, 12, 0, NULL, 0),
278         SND_SOC_DAPM_PGA("Frontend PGA2", NAU8540_REG_PWR, 13, 0, NULL, 0),
279         SND_SOC_DAPM_PGA("Frontend PGA3", NAU8540_REG_PWR, 14, 0, NULL, 0),
280         SND_SOC_DAPM_PGA("Frontend PGA4", NAU8540_REG_PWR, 15, 0, NULL, 0),
281
282         SND_SOC_DAPM_ADC_E("ADC1", NULL,
283                 NAU8540_REG_POWER_MANAGEMENT, 0, 0, adc_power_control,
284                 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
285         SND_SOC_DAPM_ADC_E("ADC2", NULL,
286                 NAU8540_REG_POWER_MANAGEMENT, 1, 0, adc_power_control,
287                 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
288         SND_SOC_DAPM_ADC_E("ADC3", NULL,
289                 NAU8540_REG_POWER_MANAGEMENT, 2, 0, adc_power_control,
290                 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
291         SND_SOC_DAPM_ADC_E("ADC4", NULL,
292                 NAU8540_REG_POWER_MANAGEMENT, 3, 0, adc_power_control,
293                 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
294
295         SND_SOC_DAPM_PGA("ADC CH1", NAU8540_REG_ANALOG_PWR, 0, 0, NULL, 0),
296         SND_SOC_DAPM_PGA("ADC CH2", NAU8540_REG_ANALOG_PWR, 1, 0, NULL, 0),
297         SND_SOC_DAPM_PGA("ADC CH3", NAU8540_REG_ANALOG_PWR, 2, 0, NULL, 0),
298         SND_SOC_DAPM_PGA("ADC CH4", NAU8540_REG_ANALOG_PWR, 3, 0, NULL, 0),
299
300         SND_SOC_DAPM_MUX("Digital CH4 Mux",
301                 SND_SOC_NOPM, 0, 0, &digital_ch4_mux),
302         SND_SOC_DAPM_MUX("Digital CH3 Mux",
303                 SND_SOC_NOPM, 0, 0, &digital_ch3_mux),
304         SND_SOC_DAPM_MUX("Digital CH2 Mux",
305                 SND_SOC_NOPM, 0, 0, &digital_ch2_mux),
306         SND_SOC_DAPM_MUX("Digital CH1 Mux",
307                 SND_SOC_NOPM, 0, 0, &digital_ch1_mux),
308
309         SND_SOC_DAPM_AIF_OUT_E("AIFTX", "Capture", 0, SND_SOC_NOPM, 0, 0,
310                 aiftx_power_control, SND_SOC_DAPM_POST_PMD),
311 };
312
313 static const struct snd_soc_dapm_route nau8540_dapm_routes[] = {
314         {"Frontend PGA1", NULL, "MIC1"},
315         {"Frontend PGA2", NULL, "MIC2"},
316         {"Frontend PGA3", NULL, "MIC3"},
317         {"Frontend PGA4", NULL, "MIC4"},
318
319         {"ADC1", NULL, "Frontend PGA1"},
320         {"ADC2", NULL, "Frontend PGA2"},
321         {"ADC3", NULL, "Frontend PGA3"},
322         {"ADC4", NULL, "Frontend PGA4"},
323
324         {"ADC CH1", NULL, "ADC1"},
325         {"ADC CH2", NULL, "ADC2"},
326         {"ADC CH3", NULL, "ADC3"},
327         {"ADC CH4", NULL, "ADC4"},
328
329         {"ADC1", NULL, "MICBIAS1"},
330         {"ADC2", NULL, "MICBIAS1"},
331         {"ADC3", NULL, "MICBIAS2"},
332         {"ADC4", NULL, "MICBIAS2"},
333
334         {"Digital CH1 Mux", "ADC channel 1", "ADC CH1"},
335         {"Digital CH1 Mux", "ADC channel 2", "ADC CH2"},
336         {"Digital CH1 Mux", "ADC channel 3", "ADC CH3"},
337         {"Digital CH1 Mux", "ADC channel 4", "ADC CH4"},
338
339         {"Digital CH2 Mux", "ADC channel 1", "ADC CH1"},
340         {"Digital CH2 Mux", "ADC channel 2", "ADC CH2"},
341         {"Digital CH2 Mux", "ADC channel 3", "ADC CH3"},
342         {"Digital CH2 Mux", "ADC channel 4", "ADC CH4"},
343
344         {"Digital CH3 Mux", "ADC channel 1", "ADC CH1"},
345         {"Digital CH3 Mux", "ADC channel 2", "ADC CH2"},
346         {"Digital CH3 Mux", "ADC channel 3", "ADC CH3"},
347         {"Digital CH3 Mux", "ADC channel 4", "ADC CH4"},
348
349         {"Digital CH4 Mux", "ADC channel 1", "ADC CH1"},
350         {"Digital CH4 Mux", "ADC channel 2", "ADC CH2"},
351         {"Digital CH4 Mux", "ADC channel 3", "ADC CH3"},
352         {"Digital CH4 Mux", "ADC channel 4", "ADC CH4"},
353
354         {"AIFTX", NULL, "Digital CH1 Mux"},
355         {"AIFTX", NULL, "Digital CH2 Mux"},
356         {"AIFTX", NULL, "Digital CH3 Mux"},
357         {"AIFTX", NULL, "Digital CH4 Mux"},
358 };
359
360 static const struct nau8540_osr_attr *
361 nau8540_get_osr(struct nau8540 *nau8540)
362 {
363         unsigned int osr;
364
365         regmap_read(nau8540->regmap, NAU8540_REG_ADC_SAMPLE_RATE, &osr);
366         osr &= NAU8540_ADC_OSR_MASK;
367         if (osr >= ARRAY_SIZE(osr_adc_sel))
368                 return NULL;
369         return &osr_adc_sel[osr];
370 }
371
372 static int nau8540_dai_startup(struct snd_pcm_substream *substream,
373                                struct snd_soc_dai *dai)
374 {
375         struct snd_soc_component *component = dai->component;
376         struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
377         const struct nau8540_osr_attr *osr;
378
379         osr = nau8540_get_osr(nau8540);
380         if (!osr || !osr->osr)
381                 return -EINVAL;
382
383         return snd_pcm_hw_constraint_minmax(substream->runtime,
384                                             SNDRV_PCM_HW_PARAM_RATE,
385                                             0, CLK_ADC_MAX / osr->osr);
386 }
387
388 static int nau8540_hw_params(struct snd_pcm_substream *substream,
389         struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
390 {
391         struct snd_soc_component *component = dai->component;
392         struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
393         unsigned int val_len = 0;
394         const struct nau8540_osr_attr *osr;
395
396         /* CLK_ADC = OSR * FS
397          * ADC clock frequency is defined as Over Sampling Rate (OSR)
398          * multiplied by the audio sample rate (Fs). Note that the OSR and Fs
399          * values must be selected such that the maximum frequency is less
400          * than 6.144 MHz.
401          */
402         osr = nau8540_get_osr(nau8540);
403         if (!osr || !osr->osr)
404                 return -EINVAL;
405         if (params_rate(params) * osr->osr > CLK_ADC_MAX)
406                 return -EINVAL;
407         regmap_update_bits(nau8540->regmap, NAU8540_REG_CLOCK_SRC,
408                 NAU8540_CLK_ADC_SRC_MASK,
409                 osr->clk_src << NAU8540_CLK_ADC_SRC_SFT);
410
411         switch (params_width(params)) {
412         case 16:
413                 val_len |= NAU8540_I2S_DL_16;
414                 break;
415         case 20:
416                 val_len |= NAU8540_I2S_DL_20;
417                 break;
418         case 24:
419                 val_len |= NAU8540_I2S_DL_24;
420                 break;
421         case 32:
422                 val_len |= NAU8540_I2S_DL_32;
423                 break;
424         default:
425                 return -EINVAL;
426         }
427
428         regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL0,
429                 NAU8540_I2S_DL_MASK, val_len);
430
431         return 0;
432 }
433
434 static int nau8540_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
435 {
436         struct snd_soc_component *component = dai->component;
437         struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
438         unsigned int ctrl1_val = 0, ctrl2_val = 0;
439
440         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
441         case SND_SOC_DAIFMT_CBM_CFM:
442                 ctrl2_val |= NAU8540_I2S_MS_MASTER;
443                 break;
444         case SND_SOC_DAIFMT_CBS_CFS:
445                 break;
446         default:
447                 return -EINVAL;
448         }
449
450         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
451         case SND_SOC_DAIFMT_NB_NF:
452                 break;
453         case SND_SOC_DAIFMT_IB_NF:
454                 ctrl1_val |= NAU8540_I2S_BP_INV;
455                 break;
456         default:
457                 return -EINVAL;
458         }
459
460         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
461         case SND_SOC_DAIFMT_I2S:
462                 ctrl1_val |= NAU8540_I2S_DF_I2S;
463                 break;
464         case SND_SOC_DAIFMT_LEFT_J:
465                 ctrl1_val |= NAU8540_I2S_DF_LEFT;
466                 break;
467         case SND_SOC_DAIFMT_RIGHT_J:
468                 ctrl1_val |= NAU8540_I2S_DF_RIGTH;
469                 break;
470         case SND_SOC_DAIFMT_DSP_A:
471                 ctrl1_val |= NAU8540_I2S_DF_PCM_AB;
472                 break;
473         case SND_SOC_DAIFMT_DSP_B:
474                 ctrl1_val |= NAU8540_I2S_DF_PCM_AB;
475                 ctrl1_val |= NAU8540_I2S_PCMB_EN;
476                 break;
477         default:
478                 return -EINVAL;
479         }
480
481         regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL0,
482                 NAU8540_I2S_DL_MASK | NAU8540_I2S_DF_MASK |
483                 NAU8540_I2S_BP_INV | NAU8540_I2S_PCMB_EN, ctrl1_val);
484         regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL1,
485                 NAU8540_I2S_MS_MASK | NAU8540_I2S_DO12_OE, ctrl2_val);
486         regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL2,
487                 NAU8540_I2S_DO34_OE, 0);
488
489         return 0;
490 }
491
492 /**
493  * nau8540_set_tdm_slot - configure DAI TX TDM.
494  * @dai: DAI
495  * @tx_mask: bitmask representing active TX slots. Ex.
496  *                 0xf for normal 4 channel TDM.
497  *                 0xf0 for shifted 4 channel TDM
498  * @rx_mask: no used.
499  * @slots: Number of slots in use.
500  * @slot_width: Width in bits for each slot.
501  *
502  * Configures a DAI for TDM operation. Only support 4 slots TDM.
503  */
504 static int nau8540_set_tdm_slot(struct snd_soc_dai *dai,
505         unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
506 {
507         struct snd_soc_component *component = dai->component;
508         struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
509         unsigned int ctrl2_val = 0, ctrl4_val = 0;
510
511         if (slots > 4 || ((tx_mask & 0xf0) && (tx_mask & 0xf)))
512                 return -EINVAL;
513
514         ctrl4_val |= (NAU8540_TDM_MODE | NAU8540_TDM_OFFSET_EN);
515         if (tx_mask & 0xf0) {
516                 ctrl2_val = 4 * slot_width;
517                 ctrl4_val |= (tx_mask >> 4);
518         } else {
519                 ctrl4_val |= tx_mask;
520         }
521         regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL4,
522                 NAU8540_TDM_MODE | NAU8540_TDM_OFFSET_EN |
523                 NAU8540_TDM_TX_MASK, ctrl4_val);
524         regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL1,
525                 NAU8540_I2S_DO12_OE, NAU8540_I2S_DO12_OE);
526         regmap_update_bits(nau8540->regmap, NAU8540_REG_PCM_CTRL2,
527                 NAU8540_I2S_DO34_OE | NAU8540_I2S_TSLOT_L_MASK,
528                 NAU8540_I2S_DO34_OE | ctrl2_val);
529
530         return 0;
531 }
532
533
534 static const struct snd_soc_dai_ops nau8540_dai_ops = {
535         .startup = nau8540_dai_startup,
536         .hw_params = nau8540_hw_params,
537         .set_fmt = nau8540_set_fmt,
538         .set_tdm_slot = nau8540_set_tdm_slot,
539 };
540
541 #define NAU8540_RATES SNDRV_PCM_RATE_8000_48000
542 #define NAU8540_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
543          | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
544
545 static struct snd_soc_dai_driver nau8540_dai = {
546         .name = "nau8540-hifi",
547         .capture = {
548                 .stream_name = "Capture",
549                 .channels_min = 1,
550                 .channels_max = 4,
551                 .rates = NAU8540_RATES,
552                 .formats = NAU8540_FORMATS,
553         },
554         .ops = &nau8540_dai_ops,
555 };
556
557 /**
558  * nau8540_calc_fll_param - Calculate FLL parameters.
559  * @fll_in: external clock provided to codec.
560  * @fs: sampling rate.
561  * @fll_param: Pointer to structure of FLL parameters.
562  *
563  * Calculate FLL parameters to configure codec.
564  *
565  * Returns 0 for success or negative error code.
566  */
567 static int nau8540_calc_fll_param(unsigned int fll_in,
568         unsigned int fs, struct nau8540_fll *fll_param)
569 {
570         u64 fvco, fvco_max;
571         unsigned int fref, i, fvco_sel;
572
573         /* Ensure the reference clock frequency (FREF) is <= 13.5MHz by dividing
574          * freq_in by 1, 2, 4, or 8 using FLL pre-scalar.
575          * FREF = freq_in / NAU8540_FLL_REF_DIV_MASK
576          */
577         for (i = 0; i < ARRAY_SIZE(fll_pre_scalar); i++) {
578                 fref = fll_in / fll_pre_scalar[i].param;
579                 if (fref <= NAU_FREF_MAX)
580                         break;
581         }
582         if (i == ARRAY_SIZE(fll_pre_scalar))
583                 return -EINVAL;
584         fll_param->clk_ref_div = fll_pre_scalar[i].val;
585
586         /* Choose the FLL ratio based on FREF */
587         for (i = 0; i < ARRAY_SIZE(fll_ratio); i++) {
588                 if (fref >= fll_ratio[i].param)
589                         break;
590         }
591         if (i == ARRAY_SIZE(fll_ratio))
592                 return -EINVAL;
593         fll_param->ratio = fll_ratio[i].val;
594
595         /* Calculate the frequency of DCO (FDCO) given freq_out = 256 * Fs.
596          * FDCO must be within the 90MHz - 124MHz or the FFL cannot be
597          * guaranteed across the full range of operation.
598          * FDCO = freq_out * 2 * mclk_src_scaling
599          */
600         fvco_max = 0;
601         fvco_sel = ARRAY_SIZE(mclk_src_scaling);
602         for (i = 0; i < ARRAY_SIZE(mclk_src_scaling); i++) {
603                 fvco = 256ULL * fs * 2 * mclk_src_scaling[i].param;
604                 if (fvco > NAU_FVCO_MIN && fvco < NAU_FVCO_MAX &&
605                         fvco_max < fvco) {
606                         fvco_max = fvco;
607                         fvco_sel = i;
608                 }
609         }
610         if (ARRAY_SIZE(mclk_src_scaling) == fvco_sel)
611                 return -EINVAL;
612         fll_param->mclk_src = mclk_src_scaling[fvco_sel].val;
613
614         /* Calculate the FLL 10-bit integer input and the FLL 16-bit fractional
615          * input based on FDCO, FREF and FLL ratio.
616          */
617         fvco = div_u64(fvco_max << 16, fref * fll_param->ratio);
618         fll_param->fll_int = (fvco >> 16) & 0x3FF;
619         fll_param->fll_frac = fvco & 0xFFFF;
620         return 0;
621 }
622
623 static void nau8540_fll_apply(struct regmap *regmap,
624         struct nau8540_fll *fll_param)
625 {
626         regmap_update_bits(regmap, NAU8540_REG_CLOCK_SRC,
627                 NAU8540_CLK_SRC_MASK | NAU8540_CLK_MCLK_SRC_MASK,
628                 NAU8540_CLK_SRC_MCLK | fll_param->mclk_src);
629         regmap_update_bits(regmap, NAU8540_REG_FLL1,
630                 NAU8540_FLL_RATIO_MASK | NAU8540_ICTRL_LATCH_MASK,
631                 fll_param->ratio | (0x6 << NAU8540_ICTRL_LATCH_SFT));
632         /* FLL 16-bit fractional input */
633         regmap_write(regmap, NAU8540_REG_FLL2, fll_param->fll_frac);
634         /* FLL 10-bit integer input */
635         regmap_update_bits(regmap, NAU8540_REG_FLL3,
636                 NAU8540_FLL_INTEGER_MASK, fll_param->fll_int);
637         /* FLL pre-scaler */
638         regmap_update_bits(regmap, NAU8540_REG_FLL4,
639                 NAU8540_FLL_REF_DIV_MASK,
640                 fll_param->clk_ref_div << NAU8540_FLL_REF_DIV_SFT);
641         regmap_update_bits(regmap, NAU8540_REG_FLL5,
642                 NAU8540_FLL_CLK_SW_MASK, NAU8540_FLL_CLK_SW_REF);
643         regmap_update_bits(regmap,
644                 NAU8540_REG_FLL6, NAU8540_DCO_EN, 0);
645         if (fll_param->fll_frac) {
646                 regmap_update_bits(regmap, NAU8540_REG_FLL5,
647                         NAU8540_FLL_PDB_DAC_EN | NAU8540_FLL_LOOP_FTR_EN |
648                         NAU8540_FLL_FTR_SW_MASK,
649                         NAU8540_FLL_PDB_DAC_EN | NAU8540_FLL_LOOP_FTR_EN |
650                         NAU8540_FLL_FTR_SW_FILTER);
651                 regmap_update_bits(regmap, NAU8540_REG_FLL6,
652                         NAU8540_SDM_EN | NAU8540_CUTOFF500,
653                         NAU8540_SDM_EN | NAU8540_CUTOFF500);
654         } else {
655                 regmap_update_bits(regmap, NAU8540_REG_FLL5,
656                         NAU8540_FLL_PDB_DAC_EN | NAU8540_FLL_LOOP_FTR_EN |
657                         NAU8540_FLL_FTR_SW_MASK, NAU8540_FLL_FTR_SW_ACCU);
658                 regmap_update_bits(regmap, NAU8540_REG_FLL6,
659                         NAU8540_SDM_EN | NAU8540_CUTOFF500, 0);
660         }
661 }
662
663 /* freq_out must be 256*Fs in order to achieve the best performance */
664 static int nau8540_set_pll(struct snd_soc_component *component, int pll_id, int source,
665                 unsigned int freq_in, unsigned int freq_out)
666 {
667         struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
668         struct nau8540_fll fll_param;
669         int ret, fs;
670
671         switch (pll_id) {
672         case NAU8540_CLK_FLL_MCLK:
673                 regmap_update_bits(nau8540->regmap, NAU8540_REG_FLL3,
674                         NAU8540_FLL_CLK_SRC_MASK | NAU8540_GAIN_ERR_MASK,
675                         NAU8540_FLL_CLK_SRC_MCLK | 0);
676                 break;
677
678         case NAU8540_CLK_FLL_BLK:
679                 regmap_update_bits(nau8540->regmap, NAU8540_REG_FLL3,
680                         NAU8540_FLL_CLK_SRC_MASK | NAU8540_GAIN_ERR_MASK,
681                         NAU8540_FLL_CLK_SRC_BLK |
682                         (0xf << NAU8540_GAIN_ERR_SFT));
683                 break;
684
685         case NAU8540_CLK_FLL_FS:
686                 regmap_update_bits(nau8540->regmap, NAU8540_REG_FLL3,
687                         NAU8540_FLL_CLK_SRC_MASK | NAU8540_GAIN_ERR_MASK,
688                         NAU8540_FLL_CLK_SRC_FS |
689                         (0xf << NAU8540_GAIN_ERR_SFT));
690                 break;
691
692         default:
693                 dev_err(nau8540->dev, "Invalid clock id (%d)\n", pll_id);
694                 return -EINVAL;
695         }
696         dev_dbg(nau8540->dev, "Sysclk is %dHz and clock id is %d\n",
697                 freq_out, pll_id);
698
699         fs = freq_out / 256;
700         ret = nau8540_calc_fll_param(freq_in, fs, &fll_param);
701         if (ret < 0) {
702                 dev_err(nau8540->dev, "Unsupported input clock %d\n", freq_in);
703                 return ret;
704         }
705         dev_dbg(nau8540->dev, "mclk_src=%x ratio=%x fll_frac=%x fll_int=%x clk_ref_div=%x\n",
706                 fll_param.mclk_src, fll_param.ratio, fll_param.fll_frac,
707                 fll_param.fll_int, fll_param.clk_ref_div);
708
709         nau8540_fll_apply(nau8540->regmap, &fll_param);
710         mdelay(2);
711         regmap_update_bits(nau8540->regmap, NAU8540_REG_CLOCK_SRC,
712                 NAU8540_CLK_SRC_MASK, NAU8540_CLK_SRC_VCO);
713
714         return 0;
715 }
716
717 static int nau8540_set_sysclk(struct snd_soc_component *component,
718         int clk_id, int source, unsigned int freq, int dir)
719 {
720         struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
721
722         switch (clk_id) {
723         case NAU8540_CLK_DIS:
724         case NAU8540_CLK_MCLK:
725                 regmap_update_bits(nau8540->regmap, NAU8540_REG_CLOCK_SRC,
726                         NAU8540_CLK_SRC_MASK, NAU8540_CLK_SRC_MCLK);
727                 regmap_update_bits(nau8540->regmap, NAU8540_REG_FLL6,
728                         NAU8540_DCO_EN, 0);
729                 break;
730
731         case NAU8540_CLK_INTERNAL:
732                 regmap_update_bits(nau8540->regmap, NAU8540_REG_FLL6,
733                         NAU8540_DCO_EN, NAU8540_DCO_EN);
734                 regmap_update_bits(nau8540->regmap, NAU8540_REG_CLOCK_SRC,
735                         NAU8540_CLK_SRC_MASK, NAU8540_CLK_SRC_VCO);
736                 break;
737
738         default:
739                 dev_err(nau8540->dev, "Invalid clock id (%d)\n", clk_id);
740                 return -EINVAL;
741         }
742
743         dev_dbg(nau8540->dev, "Sysclk is %dHz and clock id is %d\n",
744                 freq, clk_id);
745
746         return 0;
747 }
748
749 static void nau8540_reset_chip(struct regmap *regmap)
750 {
751         regmap_write(regmap, NAU8540_REG_SW_RESET, 0x00);
752         regmap_write(regmap, NAU8540_REG_SW_RESET, 0x00);
753 }
754
755 static void nau8540_init_regs(struct nau8540 *nau8540)
756 {
757         struct regmap *regmap = nau8540->regmap;
758
759         /* Enable Bias/VMID/VMID Tieoff */
760         regmap_update_bits(regmap, NAU8540_REG_VMID_CTRL,
761                 NAU8540_VMID_EN | NAU8540_VMID_SEL_MASK,
762                 NAU8540_VMID_EN | (0x2 << NAU8540_VMID_SEL_SFT));
763         regmap_update_bits(regmap, NAU8540_REG_REFERENCE,
764                 NAU8540_PRECHARGE_DIS | NAU8540_GLOBAL_BIAS_EN,
765                 NAU8540_PRECHARGE_DIS | NAU8540_GLOBAL_BIAS_EN);
766         mdelay(2);
767         regmap_update_bits(regmap, NAU8540_REG_MIC_BIAS,
768                 NAU8540_PU_PRE, NAU8540_PU_PRE);
769         regmap_update_bits(regmap, NAU8540_REG_CLOCK_CTRL,
770                 NAU8540_CLK_ADC_EN | NAU8540_CLK_I2S_EN,
771                 NAU8540_CLK_ADC_EN | NAU8540_CLK_I2S_EN);
772         /* ADC OSR selection, CLK_ADC = Fs * OSR;
773          * Channel time alignment enable.
774          */
775         regmap_update_bits(regmap, NAU8540_REG_ADC_SAMPLE_RATE,
776                 NAU8540_CH_SYNC | NAU8540_ADC_OSR_MASK,
777                 NAU8540_CH_SYNC | NAU8540_ADC_OSR_64);
778         /* PGA input mode selection */
779         regmap_update_bits(regmap, NAU8540_REG_FEPGA1,
780                 NAU8540_FEPGA1_MODCH2_SHT | NAU8540_FEPGA1_MODCH1_SHT,
781                 NAU8540_FEPGA1_MODCH2_SHT | NAU8540_FEPGA1_MODCH1_SHT);
782         regmap_update_bits(regmap, NAU8540_REG_FEPGA2,
783                 NAU8540_FEPGA2_MODCH4_SHT | NAU8540_FEPGA2_MODCH3_SHT,
784                 NAU8540_FEPGA2_MODCH4_SHT | NAU8540_FEPGA2_MODCH3_SHT);
785         /* DO12 and DO34 pad output disable */
786         regmap_update_bits(regmap, NAU8540_REG_PCM_CTRL1,
787                 NAU8540_I2S_DO12_TRI, NAU8540_I2S_DO12_TRI);
788         regmap_update_bits(regmap, NAU8540_REG_PCM_CTRL2,
789                 NAU8540_I2S_DO34_TRI, NAU8540_I2S_DO34_TRI);
790 }
791
792 static int __maybe_unused nau8540_suspend(struct snd_soc_component *component)
793 {
794         struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
795
796         regcache_cache_only(nau8540->regmap, true);
797         regcache_mark_dirty(nau8540->regmap);
798
799         return 0;
800 }
801
802 static int __maybe_unused nau8540_resume(struct snd_soc_component *component)
803 {
804         struct nau8540 *nau8540 = snd_soc_component_get_drvdata(component);
805
806         regcache_cache_only(nau8540->regmap, false);
807         regcache_sync(nau8540->regmap);
808
809         return 0;
810 }
811
812 static const struct snd_soc_component_driver nau8540_component_driver = {
813         .set_sysclk             = nau8540_set_sysclk,
814         .set_pll                = nau8540_set_pll,
815         .suspend                = nau8540_suspend,
816         .resume                 = nau8540_resume,
817         .controls               = nau8540_snd_controls,
818         .num_controls           = ARRAY_SIZE(nau8540_snd_controls),
819         .dapm_widgets           = nau8540_dapm_widgets,
820         .num_dapm_widgets       = ARRAY_SIZE(nau8540_dapm_widgets),
821         .dapm_routes            = nau8540_dapm_routes,
822         .num_dapm_routes        = ARRAY_SIZE(nau8540_dapm_routes),
823         .suspend_bias_off       = 1,
824         .idle_bias_on           = 1,
825         .use_pmdown_time        = 1,
826         .endianness             = 1,
827 };
828
829 static const struct regmap_config nau8540_regmap_config = {
830         .val_bits = 16,
831         .reg_bits = 16,
832
833         .max_register = NAU8540_REG_MAX,
834         .readable_reg = nau8540_readable_reg,
835         .writeable_reg = nau8540_writeable_reg,
836         .volatile_reg = nau8540_volatile_reg,
837
838         .cache_type = REGCACHE_RBTREE,
839         .reg_defaults = nau8540_reg_defaults,
840         .num_reg_defaults = ARRAY_SIZE(nau8540_reg_defaults),
841 };
842
843 static int nau8540_i2c_probe(struct i2c_client *i2c)
844 {
845         struct device *dev = &i2c->dev;
846         struct nau8540 *nau8540 = dev_get_platdata(dev);
847         int ret, value;
848
849         if (!nau8540) {
850                 nau8540 = devm_kzalloc(dev, sizeof(*nau8540), GFP_KERNEL);
851                 if (!nau8540)
852                         return -ENOMEM;
853         }
854         i2c_set_clientdata(i2c, nau8540);
855
856         nau8540->regmap = devm_regmap_init_i2c(i2c, &nau8540_regmap_config);
857         if (IS_ERR(nau8540->regmap))
858                 return PTR_ERR(nau8540->regmap);
859         ret = regmap_read(nau8540->regmap, NAU8540_REG_I2C_DEVICE_ID, &value);
860         if (ret < 0) {
861                 dev_err(dev, "Failed to read device id from the NAU85L40: %d\n",
862                         ret);
863                 return ret;
864         }
865
866         nau8540->dev = dev;
867         nau8540_reset_chip(nau8540->regmap);
868         nau8540_init_regs(nau8540);
869
870         return devm_snd_soc_register_component(dev,
871                 &nau8540_component_driver, &nau8540_dai, 1);
872 }
873
874 static const struct i2c_device_id nau8540_i2c_ids[] = {
875         { "nau8540", 0 },
876         { }
877 };
878 MODULE_DEVICE_TABLE(i2c, nau8540_i2c_ids);
879
880 #ifdef CONFIG_OF
881 static const struct of_device_id nau8540_of_ids[] = {
882         { .compatible = "nuvoton,nau8540", },
883         {}
884 };
885 MODULE_DEVICE_TABLE(of, nau8540_of_ids);
886 #endif
887
888 static struct i2c_driver nau8540_i2c_driver = {
889         .driver = {
890                 .name = "nau8540",
891                 .of_match_table = of_match_ptr(nau8540_of_ids),
892         },
893         .probe_new = nau8540_i2c_probe,
894         .id_table = nau8540_i2c_ids,
895 };
896 module_i2c_driver(nau8540_i2c_driver);
897
898 MODULE_DESCRIPTION("ASoC NAU85L40 driver");
899 MODULE_AUTHOR("John Hsu <KCHSU0@nuvoton.com>");
900 MODULE_LICENSE("GPL v2");