xref: /linux/sound/soc/codecs/nau8821.c (revision cf5071876baf995f8f98e86ef06f85a58feda63c)
1aab1ad11SSeven Lee // SPDX-License-Identifier: GPL-2.0-only
2aab1ad11SSeven Lee //
3aab1ad11SSeven Lee // nau8821.c -- Nuvoton NAU88L21 audio codec driver
4aab1ad11SSeven Lee //
5aab1ad11SSeven Lee // Copyright 2021 Nuvoton Technology Corp.
6aab1ad11SSeven Lee // Author: John Hsu <kchsu0@nuvoton.com>
7aab1ad11SSeven Lee // Co-author: Seven Lee <wtli@nuvoton.com>
8aab1ad11SSeven Lee //
9aab1ad11SSeven Lee 
10aab1ad11SSeven Lee #include <linux/acpi.h>
11aab1ad11SSeven Lee #include <linux/clk.h>
12aab1ad11SSeven Lee #include <linux/delay.h>
13aab1ad11SSeven Lee #include <linux/init.h>
14aab1ad11SSeven Lee #include <linux/i2c.h>
15aab1ad11SSeven Lee #include <linux/module.h>
16aab1ad11SSeven Lee #include <linux/math64.h>
17aab1ad11SSeven Lee #include <linux/regmap.h>
18aab1ad11SSeven Lee #include <linux/slab.h>
19aab1ad11SSeven Lee #include <sound/core.h>
20aab1ad11SSeven Lee #include <sound/initval.h>
21aab1ad11SSeven Lee #include <sound/jack.h>
22aab1ad11SSeven Lee #include <sound/pcm.h>
23aab1ad11SSeven Lee #include <sound/pcm_params.h>
24aab1ad11SSeven Lee #include <sound/soc.h>
25aab1ad11SSeven Lee #include <sound/tlv.h>
26aab1ad11SSeven Lee #include "nau8821.h"
27aab1ad11SSeven Lee 
28aab1ad11SSeven Lee #define NAU_FREF_MAX 13500000
29aab1ad11SSeven Lee #define NAU_FVCO_MAX 100000000
30aab1ad11SSeven Lee #define NAU_FVCO_MIN 90000000
31aab1ad11SSeven Lee 
322551b6e8SSeven Lee #define NAU8821_BUTTON SND_JACK_BTN_0
332551b6e8SSeven Lee 
34aab1ad11SSeven Lee /* the maximum frequency of CLK_ADC and CLK_DAC */
35aab1ad11SSeven Lee #define CLK_DA_AD_MAX 6144000
36aab1ad11SSeven Lee 
37aab1ad11SSeven Lee static int nau8821_configure_sysclk(struct nau8821 *nau8821,
38aab1ad11SSeven Lee 	int clk_id, unsigned int freq);
390cf470c0SWallace Lin static bool nau8821_is_jack_inserted(struct regmap *regmap);
40aab1ad11SSeven Lee 
41aab1ad11SSeven Lee struct nau8821_fll {
42aab1ad11SSeven Lee 	int mclk_src;
43aab1ad11SSeven Lee 	int ratio;
44aab1ad11SSeven Lee 	int fll_frac;
45aab1ad11SSeven Lee 	int fll_int;
46aab1ad11SSeven Lee 	int clk_ref_div;
47aab1ad11SSeven Lee };
48aab1ad11SSeven Lee 
49aab1ad11SSeven Lee struct nau8821_fll_attr {
50aab1ad11SSeven Lee 	unsigned int param;
51aab1ad11SSeven Lee 	unsigned int val;
52aab1ad11SSeven Lee };
53aab1ad11SSeven Lee 
54aab1ad11SSeven Lee /* scaling for mclk from sysclk_src output */
55aab1ad11SSeven Lee static const struct nau8821_fll_attr mclk_src_scaling[] = {
56aab1ad11SSeven Lee 	{ 1, 0x0 },
57aab1ad11SSeven Lee 	{ 2, 0x2 },
58aab1ad11SSeven Lee 	{ 4, 0x3 },
59aab1ad11SSeven Lee 	{ 8, 0x4 },
60aab1ad11SSeven Lee 	{ 16, 0x5 },
61aab1ad11SSeven Lee 	{ 32, 0x6 },
62aab1ad11SSeven Lee 	{ 3, 0x7 },
63aab1ad11SSeven Lee 	{ 6, 0xa },
64aab1ad11SSeven Lee 	{ 12, 0xb },
65aab1ad11SSeven Lee 	{ 24, 0xc },
66aab1ad11SSeven Lee 	{ 48, 0xd },
67aab1ad11SSeven Lee 	{ 96, 0xe },
68aab1ad11SSeven Lee 	{ 5, 0xf },
69aab1ad11SSeven Lee };
70aab1ad11SSeven Lee 
71aab1ad11SSeven Lee /* ratio for input clk freq */
72aab1ad11SSeven Lee static const struct nau8821_fll_attr fll_ratio[] = {
73aab1ad11SSeven Lee 	{ 512000, 0x01 },
74aab1ad11SSeven Lee 	{ 256000, 0x02 },
75aab1ad11SSeven Lee 	{ 128000, 0x04 },
76aab1ad11SSeven Lee 	{ 64000, 0x08 },
77aab1ad11SSeven Lee 	{ 32000, 0x10 },
78aab1ad11SSeven Lee 	{ 8000, 0x20 },
79aab1ad11SSeven Lee 	{ 4000, 0x40 },
80aab1ad11SSeven Lee };
81aab1ad11SSeven Lee 
82aab1ad11SSeven Lee static const struct nau8821_fll_attr fll_pre_scalar[] = {
83aab1ad11SSeven Lee 	{ 0, 0x0 },
84aab1ad11SSeven Lee 	{ 1, 0x1 },
85aab1ad11SSeven Lee 	{ 2, 0x2 },
86aab1ad11SSeven Lee 	{ 3, 0x3 },
87aab1ad11SSeven Lee };
88aab1ad11SSeven Lee 
89aab1ad11SSeven Lee /* over sampling rate */
90aab1ad11SSeven Lee struct nau8821_osr_attr {
91aab1ad11SSeven Lee 	unsigned int osr;
92aab1ad11SSeven Lee 	unsigned int clk_src;
93aab1ad11SSeven Lee };
94aab1ad11SSeven Lee 
95aab1ad11SSeven Lee static const struct nau8821_osr_attr osr_dac_sel[] = {
96aab1ad11SSeven Lee 	{ 64, 2 },	/* OSR 64, SRC 1/4 */
97aab1ad11SSeven Lee 	{ 256, 0 },	/* OSR 256, SRC 1 */
98aab1ad11SSeven Lee 	{ 128, 1 },	/* OSR 128, SRC 1/2 */
99aab1ad11SSeven Lee 	{ 0, 0 },
100aab1ad11SSeven Lee 	{ 32, 3 },	/* OSR 32, SRC 1/8 */
101aab1ad11SSeven Lee };
102aab1ad11SSeven Lee 
103aab1ad11SSeven Lee static const struct nau8821_osr_attr osr_adc_sel[] = {
104aab1ad11SSeven Lee 	{ 32, 3 },	/* OSR 32, SRC 1/8 */
105aab1ad11SSeven Lee 	{ 64, 2 },	/* OSR 64, SRC 1/4 */
106aab1ad11SSeven Lee 	{ 128, 1 },	/* OSR 128, SRC 1/2 */
107aab1ad11SSeven Lee 	{ 256, 0 },	/* OSR 256, SRC 1 */
108aab1ad11SSeven Lee };
109aab1ad11SSeven Lee 
110aab1ad11SSeven Lee struct nau8821_dmic_speed {
111aab1ad11SSeven Lee 	unsigned int param;
112aab1ad11SSeven Lee 	unsigned int val;
113aab1ad11SSeven Lee };
114aab1ad11SSeven Lee 
115aab1ad11SSeven Lee static const struct nau8821_dmic_speed dmic_speed_sel[] = {
116aab1ad11SSeven Lee 	{ 0, 0x0 },	/*SPEED 1, SRC 1 */
117aab1ad11SSeven Lee 	{ 1, 0x1 },	/*SPEED 2, SRC 1/2 */
118aab1ad11SSeven Lee 	{ 2, 0x2 },	/*SPEED 4, SRC 1/4 */
119aab1ad11SSeven Lee 	{ 3, 0x3 },	/*SPEED 8, SRC 1/8 */
120aab1ad11SSeven Lee };
121aab1ad11SSeven Lee 
122aab1ad11SSeven Lee static const struct reg_default nau8821_reg_defaults[] = {
123aab1ad11SSeven Lee 	{ NAU8821_R01_ENA_CTRL, 0x00ff },
124aab1ad11SSeven Lee 	{ NAU8821_R03_CLK_DIVIDER, 0x0050 },
125aab1ad11SSeven Lee 	{ NAU8821_R04_FLL1, 0x0 },
126aab1ad11SSeven Lee 	{ NAU8821_R05_FLL2, 0x00bc },
127aab1ad11SSeven Lee 	{ NAU8821_R06_FLL3, 0x0008 },
128aab1ad11SSeven Lee 	{ NAU8821_R07_FLL4, 0x0010 },
129aab1ad11SSeven Lee 	{ NAU8821_R08_FLL5, 0x4000 },
130aab1ad11SSeven Lee 	{ NAU8821_R09_FLL6, 0x6900 },
131aab1ad11SSeven Lee 	{ NAU8821_R0A_FLL7, 0x0031 },
132aab1ad11SSeven Lee 	{ NAU8821_R0B_FLL8, 0x26e9 },
133aab1ad11SSeven Lee 	{ NAU8821_R0D_JACK_DET_CTRL, 0x0 },
134aab1ad11SSeven Lee 	{ NAU8821_R0F_INTERRUPT_MASK, 0x0 },
135aab1ad11SSeven Lee 	{ NAU8821_R12_INTERRUPT_DIS_CTRL, 0xffff },
136aab1ad11SSeven Lee 	{ NAU8821_R13_DMIC_CTRL, 0x0 },
137aab1ad11SSeven Lee 	{ NAU8821_R1A_GPIO12_CTRL, 0x0 },
138aab1ad11SSeven Lee 	{ NAU8821_R1B_TDM_CTRL, 0x0 },
139aab1ad11SSeven Lee 	{ NAU8821_R1C_I2S_PCM_CTRL1, 0x000a },
140aab1ad11SSeven Lee 	{ NAU8821_R1D_I2S_PCM_CTRL2, 0x8010 },
141aab1ad11SSeven Lee 	{ NAU8821_R1E_LEFT_TIME_SLOT, 0x0 },
142aab1ad11SSeven Lee 	{ NAU8821_R1F_RIGHT_TIME_SLOT, 0x0 },
143aab1ad11SSeven Lee 	{ NAU8821_R21_BIQ0_COF1, 0x0 },
144aab1ad11SSeven Lee 	{ NAU8821_R22_BIQ0_COF2, 0x0 },
145aab1ad11SSeven Lee 	{ NAU8821_R23_BIQ0_COF3, 0x0 },
146aab1ad11SSeven Lee 	{ NAU8821_R24_BIQ0_COF4, 0x0 },
147aab1ad11SSeven Lee 	{ NAU8821_R25_BIQ0_COF5, 0x0 },
148aab1ad11SSeven Lee 	{ NAU8821_R26_BIQ0_COF6, 0x0 },
149aab1ad11SSeven Lee 	{ NAU8821_R27_BIQ0_COF7, 0x0 },
150aab1ad11SSeven Lee 	{ NAU8821_R28_BIQ0_COF8, 0x0 },
151aab1ad11SSeven Lee 	{ NAU8821_R29_BIQ0_COF9, 0x0 },
152aab1ad11SSeven Lee 	{ NAU8821_R2A_BIQ0_COF10, 0x0 },
153aab1ad11SSeven Lee 	{ NAU8821_R2B_ADC_RATE, 0x0002 },
154aab1ad11SSeven Lee 	{ NAU8821_R2C_DAC_CTRL1, 0x0082 },
155aab1ad11SSeven Lee 	{ NAU8821_R2D_DAC_CTRL2, 0x0 },
156aab1ad11SSeven Lee 	{ NAU8821_R2F_DAC_DGAIN_CTRL, 0x0 },
157aab1ad11SSeven Lee 	{ NAU8821_R30_ADC_DGAIN_CTRL, 0x0 },
158aab1ad11SSeven Lee 	{ NAU8821_R31_MUTE_CTRL, 0x0 },
159aab1ad11SSeven Lee 	{ NAU8821_R32_HSVOL_CTRL, 0x0 },
160aab1ad11SSeven Lee 	{ NAU8821_R34_DACR_CTRL, 0xcfcf },
161aab1ad11SSeven Lee 	{ NAU8821_R35_ADC_DGAIN_CTRL1, 0xcfcf },
162aab1ad11SSeven Lee 	{ NAU8821_R36_ADC_DRC_KNEE_IP12, 0x1486 },
163aab1ad11SSeven Lee 	{ NAU8821_R37_ADC_DRC_KNEE_IP34, 0x0f12 },
164aab1ad11SSeven Lee 	{ NAU8821_R38_ADC_DRC_SLOPES, 0x25ff },
165aab1ad11SSeven Lee 	{ NAU8821_R39_ADC_DRC_ATKDCY, 0x3457 },
166aab1ad11SSeven Lee 	{ NAU8821_R3A_DAC_DRC_KNEE_IP12, 0x1486 },
167aab1ad11SSeven Lee 	{ NAU8821_R3B_DAC_DRC_KNEE_IP34, 0x0f12 },
168aab1ad11SSeven Lee 	{ NAU8821_R3C_DAC_DRC_SLOPES, 0x25f9 },
169aab1ad11SSeven Lee 	{ NAU8821_R3D_DAC_DRC_ATKDCY, 0x3457 },
170aab1ad11SSeven Lee 	{ NAU8821_R41_BIQ1_COF1, 0x0 },
171aab1ad11SSeven Lee 	{ NAU8821_R42_BIQ1_COF2, 0x0 },
172aab1ad11SSeven Lee 	{ NAU8821_R43_BIQ1_COF3, 0x0 },
173aab1ad11SSeven Lee 	{ NAU8821_R44_BIQ1_COF4, 0x0 },
174aab1ad11SSeven Lee 	{ NAU8821_R45_BIQ1_COF5, 0x0 },
175aab1ad11SSeven Lee 	{ NAU8821_R46_BIQ1_COF6, 0x0 },
176aab1ad11SSeven Lee 	{ NAU8821_R47_BIQ1_COF7, 0x0 },
177aab1ad11SSeven Lee 	{ NAU8821_R48_BIQ1_COF8, 0x0 },
178aab1ad11SSeven Lee 	{ NAU8821_R49_BIQ1_COF9, 0x0 },
179aab1ad11SSeven Lee 	{ NAU8821_R4A_BIQ1_COF10, 0x0 },
180aab1ad11SSeven Lee 	{ NAU8821_R4B_CLASSG_CTRL, 0x0 },
181aab1ad11SSeven Lee 	{ NAU8821_R4C_IMM_MODE_CTRL, 0x0 },
182aab1ad11SSeven Lee 	{ NAU8821_R4D_IMM_RMS_L, 0x0 },
183aab1ad11SSeven Lee 	{ NAU8821_R53_OTPDOUT_1, 0xaad8 },
184aab1ad11SSeven Lee 	{ NAU8821_R54_OTPDOUT_2, 0x0002 },
185aab1ad11SSeven Lee 	{ NAU8821_R55_MISC_CTRL, 0x0 },
186aab1ad11SSeven Lee 	{ NAU8821_R66_BIAS_ADJ, 0x0 },
187aab1ad11SSeven Lee 	{ NAU8821_R68_TRIM_SETTINGS, 0x0 },
188aab1ad11SSeven Lee 	{ NAU8821_R69_ANALOG_CONTROL_1, 0x0 },
189aab1ad11SSeven Lee 	{ NAU8821_R6A_ANALOG_CONTROL_2, 0x0 },
190aab1ad11SSeven Lee 	{ NAU8821_R6B_PGA_MUTE, 0x0 },
191aab1ad11SSeven Lee 	{ NAU8821_R71_ANALOG_ADC_1, 0x0011 },
192aab1ad11SSeven Lee 	{ NAU8821_R72_ANALOG_ADC_2, 0x0020 },
193aab1ad11SSeven Lee 	{ NAU8821_R73_RDAC, 0x0008 },
194aab1ad11SSeven Lee 	{ NAU8821_R74_MIC_BIAS, 0x0006 },
195aab1ad11SSeven Lee 	{ NAU8821_R76_BOOST, 0x0 },
196aab1ad11SSeven Lee 	{ NAU8821_R77_FEPGA, 0x0 },
197aab1ad11SSeven Lee 	{ NAU8821_R7E_PGA_GAIN, 0x0 },
198aab1ad11SSeven Lee 	{ NAU8821_R7F_POWER_UP_CONTROL, 0x0 },
199aab1ad11SSeven Lee 	{ NAU8821_R80_CHARGE_PUMP, 0x0 },
200aab1ad11SSeven Lee };
201aab1ad11SSeven Lee 
202aab1ad11SSeven Lee static bool nau8821_readable_reg(struct device *dev, unsigned int reg)
203aab1ad11SSeven Lee {
204aab1ad11SSeven Lee 	switch (reg) {
205aab1ad11SSeven Lee 	case NAU8821_R00_RESET ... NAU8821_R01_ENA_CTRL:
206aab1ad11SSeven Lee 	case NAU8821_R03_CLK_DIVIDER ... NAU8821_R0B_FLL8:
207aab1ad11SSeven Lee 	case NAU8821_R0D_JACK_DET_CTRL:
208aab1ad11SSeven Lee 	case NAU8821_R0F_INTERRUPT_MASK ... NAU8821_R13_DMIC_CTRL:
209aab1ad11SSeven Lee 	case NAU8821_R1A_GPIO12_CTRL ... NAU8821_R1F_RIGHT_TIME_SLOT:
210aab1ad11SSeven Lee 	case NAU8821_R21_BIQ0_COF1 ... NAU8821_R2D_DAC_CTRL2:
211aab1ad11SSeven Lee 	case NAU8821_R2F_DAC_DGAIN_CTRL ... NAU8821_R32_HSVOL_CTRL:
212aab1ad11SSeven Lee 	case NAU8821_R34_DACR_CTRL ... NAU8821_R3D_DAC_DRC_ATKDCY:
213aab1ad11SSeven Lee 	case NAU8821_R41_BIQ1_COF1 ... NAU8821_R4F_FUSE_CTRL3:
214aab1ad11SSeven Lee 	case NAU8821_R51_FUSE_CTRL1:
215aab1ad11SSeven Lee 	case NAU8821_R53_OTPDOUT_1 ... NAU8821_R55_MISC_CTRL:
216aab1ad11SSeven Lee 	case NAU8821_R58_I2C_DEVICE_ID ... NAU8821_R5A_SOFTWARE_RST:
217aab1ad11SSeven Lee 	case NAU8821_R66_BIAS_ADJ:
218aab1ad11SSeven Lee 	case NAU8821_R68_TRIM_SETTINGS ... NAU8821_R6B_PGA_MUTE:
219aab1ad11SSeven Lee 	case NAU8821_R71_ANALOG_ADC_1 ... NAU8821_R74_MIC_BIAS:
220aab1ad11SSeven Lee 	case NAU8821_R76_BOOST ... NAU8821_R77_FEPGA:
221aab1ad11SSeven Lee 	case NAU8821_R7E_PGA_GAIN ... NAU8821_R82_GENERAL_STATUS:
222aab1ad11SSeven Lee 		return true;
223aab1ad11SSeven Lee 	default:
224aab1ad11SSeven Lee 		return false;
225aab1ad11SSeven Lee 	}
226aab1ad11SSeven Lee }
227aab1ad11SSeven Lee 
228aab1ad11SSeven Lee static bool nau8821_writeable_reg(struct device *dev, unsigned int reg)
229aab1ad11SSeven Lee {
230aab1ad11SSeven Lee 	switch (reg) {
231aab1ad11SSeven Lee 	case NAU8821_R00_RESET ... NAU8821_R01_ENA_CTRL:
232aab1ad11SSeven Lee 	case NAU8821_R03_CLK_DIVIDER ... NAU8821_R0B_FLL8:
233aab1ad11SSeven Lee 	case NAU8821_R0D_JACK_DET_CTRL:
234aab1ad11SSeven Lee 	case NAU8821_R0F_INTERRUPT_MASK:
235aab1ad11SSeven Lee 	case NAU8821_R11_INT_CLR_KEY_STATUS ... NAU8821_R13_DMIC_CTRL:
236aab1ad11SSeven Lee 	case NAU8821_R1A_GPIO12_CTRL ... NAU8821_R1F_RIGHT_TIME_SLOT:
237aab1ad11SSeven Lee 	case NAU8821_R21_BIQ0_COF1 ... NAU8821_R2D_DAC_CTRL2:
238aab1ad11SSeven Lee 	case NAU8821_R2F_DAC_DGAIN_CTRL ... NAU8821_R32_HSVOL_CTRL:
239aab1ad11SSeven Lee 	case NAU8821_R34_DACR_CTRL ... NAU8821_R3D_DAC_DRC_ATKDCY:
240aab1ad11SSeven Lee 	case NAU8821_R41_BIQ1_COF1 ... NAU8821_R4C_IMM_MODE_CTRL:
241aab1ad11SSeven Lee 	case NAU8821_R4E_FUSE_CTRL2 ... NAU8821_R4F_FUSE_CTRL3:
242aab1ad11SSeven Lee 	case NAU8821_R51_FUSE_CTRL1:
243aab1ad11SSeven Lee 	case NAU8821_R55_MISC_CTRL:
244aab1ad11SSeven Lee 	case NAU8821_R5A_SOFTWARE_RST:
245aab1ad11SSeven Lee 	case NAU8821_R66_BIAS_ADJ:
246aab1ad11SSeven Lee 	case NAU8821_R68_TRIM_SETTINGS ... NAU8821_R6B_PGA_MUTE:
247aab1ad11SSeven Lee 	case NAU8821_R71_ANALOG_ADC_1 ... NAU8821_R74_MIC_BIAS:
248aab1ad11SSeven Lee 	case NAU8821_R76_BOOST ... NAU8821_R77_FEPGA:
249aab1ad11SSeven Lee 	case NAU8821_R7E_PGA_GAIN ... NAU8821_R80_CHARGE_PUMP:
250aab1ad11SSeven Lee 		return true;
251aab1ad11SSeven Lee 	default:
252aab1ad11SSeven Lee 		return false;
253aab1ad11SSeven Lee 	}
254aab1ad11SSeven Lee }
255aab1ad11SSeven Lee 
256aab1ad11SSeven Lee static bool nau8821_volatile_reg(struct device *dev, unsigned int reg)
257aab1ad11SSeven Lee {
258aab1ad11SSeven Lee 	switch (reg) {
259aab1ad11SSeven Lee 	case NAU8821_R00_RESET:
260aab1ad11SSeven Lee 	case NAU8821_R10_IRQ_STATUS ... NAU8821_R11_INT_CLR_KEY_STATUS:
261aab1ad11SSeven Lee 	case NAU8821_R21_BIQ0_COF1 ... NAU8821_R2A_BIQ0_COF10:
262aab1ad11SSeven Lee 	case NAU8821_R41_BIQ1_COF1 ... NAU8821_R4A_BIQ1_COF10:
263aab1ad11SSeven Lee 	case NAU8821_R4D_IMM_RMS_L:
264aab1ad11SSeven Lee 	case NAU8821_R53_OTPDOUT_1 ... NAU8821_R54_OTPDOUT_2:
265aab1ad11SSeven Lee 	case NAU8821_R58_I2C_DEVICE_ID ... NAU8821_R5A_SOFTWARE_RST:
266aab1ad11SSeven Lee 	case NAU8821_R81_CHARGE_PUMP_INPUT_READ ... NAU8821_R82_GENERAL_STATUS:
267aab1ad11SSeven Lee 		return true;
268aab1ad11SSeven Lee 	default:
269aab1ad11SSeven Lee 		return false;
270aab1ad11SSeven Lee 	}
271aab1ad11SSeven Lee }
272aab1ad11SSeven Lee 
273aab1ad11SSeven Lee static int nau8821_biq_coeff_get(struct snd_kcontrol *kcontrol,
274aab1ad11SSeven Lee 	struct snd_ctl_elem_value *ucontrol)
275aab1ad11SSeven Lee {
276aab1ad11SSeven Lee 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
277aab1ad11SSeven Lee 	struct soc_bytes_ext *params = (void *)kcontrol->private_value;
278aab1ad11SSeven Lee 
279aab1ad11SSeven Lee 	if (!component->regmap)
280aab1ad11SSeven Lee 		return -EINVAL;
281aab1ad11SSeven Lee 
282aab1ad11SSeven Lee 	regmap_raw_read(component->regmap, NAU8821_R21_BIQ0_COF1,
283aab1ad11SSeven Lee 		ucontrol->value.bytes.data, params->max);
284aab1ad11SSeven Lee 
285aab1ad11SSeven Lee 	return 0;
286aab1ad11SSeven Lee }
287aab1ad11SSeven Lee 
288aab1ad11SSeven Lee static int nau8821_biq_coeff_put(struct snd_kcontrol *kcontrol,
289aab1ad11SSeven Lee 	struct snd_ctl_elem_value *ucontrol)
290aab1ad11SSeven Lee {
291aab1ad11SSeven Lee 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
292aab1ad11SSeven Lee 	struct soc_bytes_ext *params = (void *)kcontrol->private_value;
293aab1ad11SSeven Lee 	void *data;
294aab1ad11SSeven Lee 
295aab1ad11SSeven Lee 	if (!component->regmap)
296aab1ad11SSeven Lee 		return -EINVAL;
297aab1ad11SSeven Lee 
298aab1ad11SSeven Lee 	data = kmemdup(ucontrol->value.bytes.data,
299aab1ad11SSeven Lee 		params->max, GFP_KERNEL | GFP_DMA);
300aab1ad11SSeven Lee 	if (!data)
301aab1ad11SSeven Lee 		return -ENOMEM;
302aab1ad11SSeven Lee 
303aab1ad11SSeven Lee 	regmap_raw_write(component->regmap, NAU8821_R21_BIQ0_COF1,
304aab1ad11SSeven Lee 		data, params->max);
305aab1ad11SSeven Lee 
306aab1ad11SSeven Lee 	kfree(data);
307aab1ad11SSeven Lee 
308aab1ad11SSeven Lee 	return 0;
309aab1ad11SSeven Lee }
310aab1ad11SSeven Lee 
311aab1ad11SSeven Lee static const char * const nau8821_adc_decimation[] = {
312aab1ad11SSeven Lee 	"32", "64", "128", "256" };
313aab1ad11SSeven Lee 
314aab1ad11SSeven Lee static const struct soc_enum nau8821_adc_decimation_enum =
315aab1ad11SSeven Lee 	SOC_ENUM_SINGLE(NAU8821_R2B_ADC_RATE, NAU8821_ADC_SYNC_DOWN_SFT,
316aab1ad11SSeven Lee 		ARRAY_SIZE(nau8821_adc_decimation), nau8821_adc_decimation);
317aab1ad11SSeven Lee 
318aab1ad11SSeven Lee static const char * const nau8821_dac_oversampl[] = {
319aab1ad11SSeven Lee 	"64", "256", "128", "", "32" };
320aab1ad11SSeven Lee 
321aab1ad11SSeven Lee static const struct soc_enum nau8821_dac_oversampl_enum =
322aab1ad11SSeven Lee 	SOC_ENUM_SINGLE(NAU8821_R2C_DAC_CTRL1, NAU8821_DAC_OVERSAMPLE_SFT,
323aab1ad11SSeven Lee 		ARRAY_SIZE(nau8821_dac_oversampl), nau8821_dac_oversampl);
324aab1ad11SSeven Lee 
325aab1ad11SSeven Lee static const DECLARE_TLV_DB_MINMAX_MUTE(adc_vol_tlv, -6600, 2400);
326aab1ad11SSeven Lee static const DECLARE_TLV_DB_MINMAX_MUTE(sidetone_vol_tlv, -4200, 0);
327aab1ad11SSeven Lee static const DECLARE_TLV_DB_MINMAX(hp_vol_tlv, -900, 0);
328aab1ad11SSeven Lee static const DECLARE_TLV_DB_SCALE(playback_vol_tlv, -6600, 50, 1);
329aab1ad11SSeven Lee static const DECLARE_TLV_DB_MINMAX(fepga_gain_tlv, -100, 3600);
330aab1ad11SSeven Lee static const DECLARE_TLV_DB_MINMAX_MUTE(crosstalk_vol_tlv, -7000, 2400);
331aab1ad11SSeven Lee 
332aab1ad11SSeven Lee static const struct snd_kcontrol_new nau8821_controls[] = {
333aab1ad11SSeven Lee 	SOC_DOUBLE_TLV("Mic Volume", NAU8821_R35_ADC_DGAIN_CTRL1,
334aab1ad11SSeven Lee 		NAU8821_ADCL_CH_VOL_SFT, NAU8821_ADCR_CH_VOL_SFT,
335aab1ad11SSeven Lee 		0xff, 0, adc_vol_tlv),
336aab1ad11SSeven Lee 	SOC_DOUBLE_TLV("Headphone Bypass Volume", NAU8821_R30_ADC_DGAIN_CTRL,
337aab1ad11SSeven Lee 		12, 8, 0x0f, 0, sidetone_vol_tlv),
338aab1ad11SSeven Lee 	SOC_DOUBLE_TLV("Headphone Volume", NAU8821_R32_HSVOL_CTRL,
339aab1ad11SSeven Lee 		NAU8821_HPL_VOL_SFT, NAU8821_HPR_VOL_SFT, 0x3, 1, hp_vol_tlv),
340aab1ad11SSeven Lee 	SOC_DOUBLE_TLV("Digital Playback Volume", NAU8821_R34_DACR_CTRL,
341aab1ad11SSeven Lee 		NAU8821_DACL_CH_VOL_SFT, NAU8821_DACR_CH_VOL_SFT,
342aab1ad11SSeven Lee 		0xcf, 0, playback_vol_tlv),
343aab1ad11SSeven Lee 	SOC_DOUBLE_TLV("Frontend PGA Volume", NAU8821_R7E_PGA_GAIN,
344aab1ad11SSeven Lee 		NAU8821_PGA_GAIN_L_SFT, NAU8821_PGA_GAIN_R_SFT,
345aab1ad11SSeven Lee 		37, 0, fepga_gain_tlv),
346aab1ad11SSeven Lee 	SOC_DOUBLE_TLV("Headphone Crosstalk Volume",
347aab1ad11SSeven Lee 		NAU8821_R2F_DAC_DGAIN_CTRL,
348aab1ad11SSeven Lee 		0, 8, 0xff, 0, crosstalk_vol_tlv),
349aab1ad11SSeven Lee 
350aab1ad11SSeven Lee 	SOC_ENUM("ADC Decimation Rate", nau8821_adc_decimation_enum),
351aab1ad11SSeven Lee 	SOC_ENUM("DAC Oversampling Rate", nau8821_dac_oversampl_enum),
352aab1ad11SSeven Lee 	SND_SOC_BYTES_EXT("BIQ Coefficients", 20,
353aab1ad11SSeven Lee 		nau8821_biq_coeff_get, nau8821_biq_coeff_put),
354aab1ad11SSeven Lee 	SOC_SINGLE("ADC Phase Switch", NAU8821_R1B_TDM_CTRL,
355aab1ad11SSeven Lee 		NAU8821_ADCPHS_SFT, 1, 0),
356aab1ad11SSeven Lee };
357aab1ad11SSeven Lee 
358aab1ad11SSeven Lee static const struct snd_kcontrol_new nau8821_dmic_mode_switch =
359aab1ad11SSeven Lee 	SOC_DAPM_SINGLE("Switch", NAU8821_R13_DMIC_CTRL,
360aab1ad11SSeven Lee 		NAU8821_DMIC_EN_SFT, 1, 0);
361aab1ad11SSeven Lee 
362aab1ad11SSeven Lee static int dmic_clock_control(struct snd_soc_dapm_widget *w,
363aab1ad11SSeven Lee 		struct snd_kcontrol *k, int  event)
364aab1ad11SSeven Lee {
365aab1ad11SSeven Lee 	struct snd_soc_component *component =
366aab1ad11SSeven Lee 		snd_soc_dapm_to_component(w->dapm);
367aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
368aab1ad11SSeven Lee 	int i, speed_selection = -1, clk_adc_src, clk_adc;
369aab1ad11SSeven Lee 	unsigned int clk_divider_r03;
370aab1ad11SSeven Lee 
371aab1ad11SSeven Lee 	/* The DMIC clock is gotten from adc clock divided by
372aab1ad11SSeven Lee 	 * CLK_DMIC_SRC (1, 2, 4, 8). The clock has to be equal or
373aab1ad11SSeven Lee 	 * less than nau8821->dmic_clk_threshold.
374aab1ad11SSeven Lee 	 */
375aab1ad11SSeven Lee 	regmap_read(nau8821->regmap, NAU8821_R03_CLK_DIVIDER,
376aab1ad11SSeven Lee 		&clk_divider_r03);
377aab1ad11SSeven Lee 	clk_adc_src = (clk_divider_r03 & NAU8821_CLK_ADC_SRC_MASK)
378aab1ad11SSeven Lee 		>> NAU8821_CLK_ADC_SRC_SFT;
379aab1ad11SSeven Lee 	clk_adc = (nau8821->fs * 256) >> clk_adc_src;
380aab1ad11SSeven Lee 
381aab1ad11SSeven Lee 	for (i = 0 ; i < 4 ; i++)
382aab1ad11SSeven Lee 		if ((clk_adc >> dmic_speed_sel[i].param) <=
383aab1ad11SSeven Lee 			nau8821->dmic_clk_threshold) {
384aab1ad11SSeven Lee 			speed_selection = dmic_speed_sel[i].val;
385aab1ad11SSeven Lee 			break;
386aab1ad11SSeven Lee 		}
38746ae0b3fSPierre-Louis Bossart 	if (i == 4)
388aab1ad11SSeven Lee 		return -EINVAL;
389aab1ad11SSeven Lee 
390aab1ad11SSeven Lee 	dev_dbg(nau8821->dev,
391aab1ad11SSeven Lee 		"clk_adc=%d, dmic_clk_threshold = %d, param=%d, val = %d\n",
392aab1ad11SSeven Lee 		clk_adc, nau8821->dmic_clk_threshold,
393aab1ad11SSeven Lee 		dmic_speed_sel[i].param, dmic_speed_sel[i].val);
394aab1ad11SSeven Lee 	regmap_update_bits(nau8821->regmap, NAU8821_R13_DMIC_CTRL,
395aab1ad11SSeven Lee 		NAU8821_DMIC_SRC_MASK,
396aab1ad11SSeven Lee 		(speed_selection << NAU8821_DMIC_SRC_SFT));
397aab1ad11SSeven Lee 
398aab1ad11SSeven Lee 	return 0;
399aab1ad11SSeven Lee }
400aab1ad11SSeven Lee 
401aab1ad11SSeven Lee static int nau8821_left_adc_event(struct snd_soc_dapm_widget *w,
402aab1ad11SSeven Lee 	struct snd_kcontrol *kcontrol, int event)
403aab1ad11SSeven Lee {
404aab1ad11SSeven Lee 	struct snd_soc_component *component =
405aab1ad11SSeven Lee 		snd_soc_dapm_to_component(w->dapm);
406aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
407aab1ad11SSeven Lee 
408aab1ad11SSeven Lee 	switch (event) {
409aab1ad11SSeven Lee 	case SND_SOC_DAPM_POST_PMU:
410aab1ad11SSeven Lee 		msleep(125);
411aab1ad11SSeven Lee 		regmap_update_bits(nau8821->regmap, NAU8821_R01_ENA_CTRL,
412aab1ad11SSeven Lee 			NAU8821_EN_ADCL, NAU8821_EN_ADCL);
413aab1ad11SSeven Lee 		break;
414aab1ad11SSeven Lee 	case SND_SOC_DAPM_POST_PMD:
415aab1ad11SSeven Lee 		regmap_update_bits(nau8821->regmap,
416aab1ad11SSeven Lee 			NAU8821_R01_ENA_CTRL, NAU8821_EN_ADCL, 0);
417aab1ad11SSeven Lee 		break;
418aab1ad11SSeven Lee 	default:
419aab1ad11SSeven Lee 		return -EINVAL;
420aab1ad11SSeven Lee 	}
421aab1ad11SSeven Lee 
422aab1ad11SSeven Lee 	return 0;
423aab1ad11SSeven Lee }
424aab1ad11SSeven Lee 
425aab1ad11SSeven Lee static int nau8821_right_adc_event(struct snd_soc_dapm_widget *w,
426aab1ad11SSeven Lee 	struct snd_kcontrol *kcontrol, int event)
427aab1ad11SSeven Lee {
428aab1ad11SSeven Lee 	struct snd_soc_component *component =
429aab1ad11SSeven Lee 		snd_soc_dapm_to_component(w->dapm);
430aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
431aab1ad11SSeven Lee 
432aab1ad11SSeven Lee 	switch (event) {
433aab1ad11SSeven Lee 	case SND_SOC_DAPM_POST_PMU:
434aab1ad11SSeven Lee 		msleep(125);
435aab1ad11SSeven Lee 		regmap_update_bits(nau8821->regmap, NAU8821_R01_ENA_CTRL,
436aab1ad11SSeven Lee 			NAU8821_EN_ADCR, NAU8821_EN_ADCR);
437aab1ad11SSeven Lee 		break;
438aab1ad11SSeven Lee 	case SND_SOC_DAPM_POST_PMD:
439aab1ad11SSeven Lee 		regmap_update_bits(nau8821->regmap,
440aab1ad11SSeven Lee 			NAU8821_R01_ENA_CTRL, NAU8821_EN_ADCR, 0);
441aab1ad11SSeven Lee 		break;
442aab1ad11SSeven Lee 	default:
443aab1ad11SSeven Lee 		return -EINVAL;
444aab1ad11SSeven Lee 	}
445aab1ad11SSeven Lee 
446aab1ad11SSeven Lee 	return 0;
447aab1ad11SSeven Lee }
448aab1ad11SSeven Lee 
449aab1ad11SSeven Lee static int nau8821_pump_event(struct snd_soc_dapm_widget *w,
450aab1ad11SSeven Lee 	struct snd_kcontrol *kcontrol, int event)
451aab1ad11SSeven Lee {
452aab1ad11SSeven Lee 	struct snd_soc_component *component =
453aab1ad11SSeven Lee 		snd_soc_dapm_to_component(w->dapm);
454aab1ad11SSeven Lee 	struct nau8821 *nau8821 =
455aab1ad11SSeven Lee 		snd_soc_component_get_drvdata(component);
456aab1ad11SSeven Lee 
457aab1ad11SSeven Lee 	switch (event) {
458aab1ad11SSeven Lee 	case SND_SOC_DAPM_POST_PMU:
459aab1ad11SSeven Lee 		/* Prevent startup click by letting charge pump to ramp up */
460aab1ad11SSeven Lee 		msleep(20);
461aab1ad11SSeven Lee 		regmap_update_bits(nau8821->regmap, NAU8821_R80_CHARGE_PUMP,
462aab1ad11SSeven Lee 			NAU8821_JAMNODCLOW, NAU8821_JAMNODCLOW);
463aab1ad11SSeven Lee 		break;
464aab1ad11SSeven Lee 	case SND_SOC_DAPM_PRE_PMD:
465aab1ad11SSeven Lee 		regmap_update_bits(nau8821->regmap, NAU8821_R80_CHARGE_PUMP,
466aab1ad11SSeven Lee 			NAU8821_JAMNODCLOW, 0);
467aab1ad11SSeven Lee 		break;
468aab1ad11SSeven Lee 	default:
469aab1ad11SSeven Lee 		return -EINVAL;
470aab1ad11SSeven Lee 	}
471aab1ad11SSeven Lee 
472aab1ad11SSeven Lee 	return 0;
473aab1ad11SSeven Lee }
474aab1ad11SSeven Lee 
475aab1ad11SSeven Lee static int nau8821_output_dac_event(struct snd_soc_dapm_widget *w,
476aab1ad11SSeven Lee 	struct snd_kcontrol *kcontrol, int event)
477aab1ad11SSeven Lee {
478aab1ad11SSeven Lee 	struct snd_soc_component *component =
479aab1ad11SSeven Lee 		snd_soc_dapm_to_component(w->dapm);
480aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
481aab1ad11SSeven Lee 
482aab1ad11SSeven Lee 	switch (event) {
483aab1ad11SSeven Lee 	case SND_SOC_DAPM_PRE_PMU:
484aab1ad11SSeven Lee 		/* Disables the TESTDAC to let DAC signal pass through. */
485aab1ad11SSeven Lee 		regmap_update_bits(nau8821->regmap, NAU8821_R66_BIAS_ADJ,
486aab1ad11SSeven Lee 			NAU8821_BIAS_TESTDAC_EN, 0);
487aab1ad11SSeven Lee 		break;
488aab1ad11SSeven Lee 	case SND_SOC_DAPM_POST_PMD:
489aab1ad11SSeven Lee 		regmap_update_bits(nau8821->regmap, NAU8821_R66_BIAS_ADJ,
490aab1ad11SSeven Lee 			NAU8821_BIAS_TESTDAC_EN, NAU8821_BIAS_TESTDAC_EN);
491aab1ad11SSeven Lee 		break;
492aab1ad11SSeven Lee 	default:
493aab1ad11SSeven Lee 		return -EINVAL;
494aab1ad11SSeven Lee 	}
495aab1ad11SSeven Lee 
496aab1ad11SSeven Lee 	return 0;
497aab1ad11SSeven Lee }
498aab1ad11SSeven Lee 
4990cf470c0SWallace Lin static int system_clock_control(struct snd_soc_dapm_widget *w,
5000cf470c0SWallace Lin 				struct snd_kcontrol *k, int  event)
5010cf470c0SWallace Lin {
5020cf470c0SWallace Lin 	struct snd_soc_component *component =
5030cf470c0SWallace Lin 		snd_soc_dapm_to_component(w->dapm);
5040cf470c0SWallace Lin 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
5050cf470c0SWallace Lin 
5060cf470c0SWallace Lin 	if (SND_SOC_DAPM_EVENT_OFF(event)) {
5070cf470c0SWallace Lin 		dev_dbg(nau8821->dev, "system clock control : POWER OFF\n");
5080cf470c0SWallace Lin 		/* Set clock source to disable or internal clock before the
5090cf470c0SWallace Lin 		 * playback or capture end. Codec needs clock for Jack
5100cf470c0SWallace Lin 		 * detection and button press if jack inserted; otherwise,
5110cf470c0SWallace Lin 		 * the clock should be closed.
5120cf470c0SWallace Lin 		 */
5130cf470c0SWallace Lin 		if (nau8821_is_jack_inserted(nau8821->regmap)) {
5140cf470c0SWallace Lin 			nau8821_configure_sysclk(nau8821,
5150cf470c0SWallace Lin 				NAU8821_CLK_INTERNAL, 0);
5160cf470c0SWallace Lin 		} else {
5170cf470c0SWallace Lin 			nau8821_configure_sysclk(nau8821, NAU8821_CLK_DIS, 0);
5180cf470c0SWallace Lin 		}
5190cf470c0SWallace Lin 	}
5200cf470c0SWallace Lin 	return 0;
5210cf470c0SWallace Lin }
5220cf470c0SWallace Lin 
523aab1ad11SSeven Lee static const struct snd_soc_dapm_widget nau8821_dapm_widgets[] = {
5240cf470c0SWallace Lin 	SND_SOC_DAPM_SUPPLY("System Clock", SND_SOC_NOPM, 0, 0,
5250cf470c0SWallace Lin 		system_clock_control, SND_SOC_DAPM_POST_PMD),
526aab1ad11SSeven Lee 	SND_SOC_DAPM_SUPPLY("MICBIAS", NAU8821_R74_MIC_BIAS,
527aab1ad11SSeven Lee 		NAU8821_MICBIAS_POWERUP_SFT, 0, NULL, 0),
528aab1ad11SSeven Lee 	SND_SOC_DAPM_SUPPLY("DMIC Clock", SND_SOC_NOPM, 0, 0,
529aab1ad11SSeven Lee 		dmic_clock_control, SND_SOC_DAPM_POST_PMU),
530aab1ad11SSeven Lee 	SND_SOC_DAPM_ADC("ADCL Power", NULL, NAU8821_R72_ANALOG_ADC_2,
531aab1ad11SSeven Lee 		NAU8821_POWERUP_ADCL_SFT, 0),
532aab1ad11SSeven Lee 	SND_SOC_DAPM_ADC("ADCR Power", NULL, NAU8821_R72_ANALOG_ADC_2,
533aab1ad11SSeven Lee 		NAU8821_POWERUP_ADCR_SFT, 0),
534aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("Frontend PGA L", 1, NAU8821_R7F_POWER_UP_CONTROL,
535aab1ad11SSeven Lee 		NAU8821_PUP_PGA_L_SFT, 0, NULL, 0),
536aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("Frontend PGA R", 1, NAU8821_R7F_POWER_UP_CONTROL,
537aab1ad11SSeven Lee 		NAU8821_PUP_PGA_R_SFT, 0, NULL, 0),
538aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("ADCL Digital path", 0, NAU8821_R01_ENA_CTRL,
539aab1ad11SSeven Lee 		NAU8821_EN_ADCL_SFT, 0, nau8821_left_adc_event,
540aab1ad11SSeven Lee 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
541aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("ADCR Digital path", 0, NAU8821_R01_ENA_CTRL,
542aab1ad11SSeven Lee 		NAU8821_EN_ADCR_SFT, 0, nau8821_right_adc_event,
543aab1ad11SSeven Lee 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
544aab1ad11SSeven Lee 	SND_SOC_DAPM_SWITCH("DMIC Enable", SND_SOC_NOPM,
545aab1ad11SSeven Lee 		0, 0, &nau8821_dmic_mode_switch),
546aab1ad11SSeven Lee 	SND_SOC_DAPM_AIF_OUT("AIFTX", "Capture", 0, NAU8821_R1D_I2S_PCM_CTRL2,
547aab1ad11SSeven Lee 		NAU8821_I2S_TRISTATE_SFT, 1),
548aab1ad11SSeven Lee 	SND_SOC_DAPM_AIF_IN("AIFRX", "Playback", 0, SND_SOC_NOPM, 0, 0),
549aab1ad11SSeven Lee 
550aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("ADACL", 2, NAU8821_R73_RDAC,
551aab1ad11SSeven Lee 		NAU8821_DACL_EN_SFT, 0, NULL, 0),
552aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("ADACR", 2, NAU8821_R73_RDAC,
553aab1ad11SSeven Lee 		NAU8821_DACR_EN_SFT, 0, NULL, 0),
554aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("ADACL Clock", 3, NAU8821_R73_RDAC,
555aab1ad11SSeven Lee 		NAU8821_DACL_CLK_EN_SFT, 0, NULL, 0),
556aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("ADACR Clock", 3, NAU8821_R73_RDAC,
557aab1ad11SSeven Lee 		NAU8821_DACR_CLK_EN_SFT, 0, NULL, 0),
558aab1ad11SSeven Lee 	SND_SOC_DAPM_DAC("DDACR", NULL, NAU8821_R01_ENA_CTRL,
559aab1ad11SSeven Lee 		NAU8821_EN_DACR_SFT, 0),
560aab1ad11SSeven Lee 	SND_SOC_DAPM_DAC("DDACL", NULL, NAU8821_R01_ENA_CTRL,
561aab1ad11SSeven Lee 		NAU8821_EN_DACL_SFT, 0),
562aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("HP amp L", 0, NAU8821_R4B_CLASSG_CTRL,
563aab1ad11SSeven Lee 		NAU8821_CLASSG_LDAC_EN_SFT, 0, NULL, 0),
564aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("HP amp R", 0, NAU8821_R4B_CLASSG_CTRL,
565aab1ad11SSeven Lee 		NAU8821_CLASSG_RDAC_EN_SFT, 0, NULL, 0),
566aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("Charge Pump", 1, NAU8821_R80_CHARGE_PUMP,
567aab1ad11SSeven Lee 		NAU8821_CHANRGE_PUMP_EN_SFT, 0, nau8821_pump_event,
568aab1ad11SSeven Lee 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
569aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("Output Driver R Stage 1", 4,
570aab1ad11SSeven Lee 		NAU8821_R7F_POWER_UP_CONTROL,
571aab1ad11SSeven Lee 		NAU8821_PUP_INTEG_R_SFT, 0, NULL, 0),
572aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("Output Driver L Stage 1", 4,
573aab1ad11SSeven Lee 		NAU8821_R7F_POWER_UP_CONTROL,
574aab1ad11SSeven Lee 		NAU8821_PUP_INTEG_L_SFT, 0, NULL, 0),
575aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("Output Driver R Stage 2", 5,
576aab1ad11SSeven Lee 		NAU8821_R7F_POWER_UP_CONTROL,
577aab1ad11SSeven Lee 		NAU8821_PUP_DRV_INSTG_R_SFT, 0, NULL, 0),
578aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("Output Driver L Stage 2", 5,
579aab1ad11SSeven Lee 		NAU8821_R7F_POWER_UP_CONTROL,
580aab1ad11SSeven Lee 		NAU8821_PUP_DRV_INSTG_L_SFT, 0, NULL, 0),
581aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("Output Driver R Stage 3", 6,
582aab1ad11SSeven Lee 		NAU8821_R7F_POWER_UP_CONTROL,
583aab1ad11SSeven Lee 		NAU8821_PUP_MAIN_DRV_R_SFT, 0, NULL, 0),
584aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("Output Driver L Stage 3", 6,
585aab1ad11SSeven Lee 		NAU8821_R7F_POWER_UP_CONTROL,
586aab1ad11SSeven Lee 		NAU8821_PUP_MAIN_DRV_L_SFT, 0, NULL, 0),
587aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("Output DACL", 7,
588aab1ad11SSeven Lee 		NAU8821_R80_CHARGE_PUMP, NAU8821_POWER_DOWN_DACL_SFT,
589aab1ad11SSeven Lee 		0, nau8821_output_dac_event,
590aab1ad11SSeven Lee 		SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
591aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("Output DACR", 7,
592aab1ad11SSeven Lee 		NAU8821_R80_CHARGE_PUMP, NAU8821_POWER_DOWN_DACR_SFT,
593aab1ad11SSeven Lee 		0, nau8821_output_dac_event,
594aab1ad11SSeven Lee 		SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
595aab1ad11SSeven Lee 
596aab1ad11SSeven Lee 	/* HPOL/R are ungrounded by disabling 16 Ohm pull-downs on playback */
597aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("HPOL Pulldown", 8,
598aab1ad11SSeven Lee 		NAU8821_R0D_JACK_DET_CTRL,
599aab1ad11SSeven Lee 		NAU8821_SPKR_DWN1L_SFT, 0, NULL, 0),
600aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("HPOR Pulldown", 8,
601aab1ad11SSeven Lee 		NAU8821_R0D_JACK_DET_CTRL,
602aab1ad11SSeven Lee 		NAU8821_SPKR_DWN1R_SFT, 0, NULL, 0),
603aab1ad11SSeven Lee 
604aab1ad11SSeven Lee 	/* High current HPOL/R boost driver */
605aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA_S("HP Boost Driver", 9,
606aab1ad11SSeven Lee 		NAU8821_R76_BOOST, NAU8821_HP_BOOST_DIS_SFT, 1, NULL, 0),
607aab1ad11SSeven Lee 	SND_SOC_DAPM_PGA("Class G", NAU8821_R4B_CLASSG_CTRL,
608aab1ad11SSeven Lee 		NAU8821_CLASSG_EN_SFT, 0, NULL, 0),
609aab1ad11SSeven Lee 
610aab1ad11SSeven Lee 	SND_SOC_DAPM_INPUT("MICL"),
611aab1ad11SSeven Lee 	SND_SOC_DAPM_INPUT("MICR"),
612aab1ad11SSeven Lee 	SND_SOC_DAPM_INPUT("DMIC"),
613aab1ad11SSeven Lee 	SND_SOC_DAPM_OUTPUT("HPOL"),
614aab1ad11SSeven Lee 	SND_SOC_DAPM_OUTPUT("HPOR"),
615aab1ad11SSeven Lee };
616aab1ad11SSeven Lee 
617aab1ad11SSeven Lee static const struct snd_soc_dapm_route nau8821_dapm_routes[] = {
618aab1ad11SSeven Lee 	{"DMIC Enable", "Switch", "DMIC"},
619aab1ad11SSeven Lee 	{"DMIC Enable", NULL, "DMIC Clock"},
620aab1ad11SSeven Lee 
621aab1ad11SSeven Lee 	{"Frontend PGA L", NULL, "MICL"},
622aab1ad11SSeven Lee 	{"Frontend PGA R", NULL, "MICR"},
623aab1ad11SSeven Lee 	{"Frontend PGA L", NULL, "MICBIAS"},
624aab1ad11SSeven Lee 	{"Frontend PGA R", NULL, "MICBIAS"},
625aab1ad11SSeven Lee 
626aab1ad11SSeven Lee 	{"ADCL Power", NULL, "Frontend PGA L"},
627aab1ad11SSeven Lee 	{"ADCR Power", NULL, "Frontend PGA R"},
628aab1ad11SSeven Lee 
629aab1ad11SSeven Lee 	{"ADCL Digital path", NULL, "ADCL Power"},
630aab1ad11SSeven Lee 	{"ADCR Digital path", NULL, "ADCR Power"},
631aab1ad11SSeven Lee 	{"ADCL Digital path", NULL, "DMIC Enable"},
632aab1ad11SSeven Lee 	{"ADCR Digital path", NULL, "DMIC Enable"},
633aab1ad11SSeven Lee 
634aab1ad11SSeven Lee 	{"AIFTX", NULL, "ADCL Digital path"},
635aab1ad11SSeven Lee 	{"AIFTX", NULL, "ADCR Digital path"},
636aab1ad11SSeven Lee 
6370cf470c0SWallace Lin 	{"AIFTX", NULL, "System Clock"},
6380cf470c0SWallace Lin 	{"AIFRX", NULL, "System Clock"},
6390cf470c0SWallace Lin 
640aab1ad11SSeven Lee 	{"DDACL", NULL, "AIFRX"},
641aab1ad11SSeven Lee 	{"DDACR", NULL, "AIFRX"},
642aab1ad11SSeven Lee 
643aab1ad11SSeven Lee 	{"HP amp L", NULL, "DDACL"},
644aab1ad11SSeven Lee 	{"HP amp R", NULL, "DDACR"},
645aab1ad11SSeven Lee 
646aab1ad11SSeven Lee 	{"Charge Pump", NULL, "HP amp L"},
647aab1ad11SSeven Lee 	{"Charge Pump", NULL, "HP amp R"},
648aab1ad11SSeven Lee 
649aab1ad11SSeven Lee 	{"ADACL", NULL, "Charge Pump"},
650aab1ad11SSeven Lee 	{"ADACR", NULL, "Charge Pump"},
651aab1ad11SSeven Lee 	{"ADACL Clock", NULL, "ADACL"},
652aab1ad11SSeven Lee 	{"ADACR Clock", NULL, "ADACR"},
653aab1ad11SSeven Lee 
654aab1ad11SSeven Lee 	{"Output Driver L Stage 1", NULL, "ADACL Clock"},
655aab1ad11SSeven Lee 	{"Output Driver R Stage 1", NULL, "ADACR Clock"},
656aab1ad11SSeven Lee 	{"Output Driver L Stage 2", NULL, "Output Driver L Stage 1"},
657aab1ad11SSeven Lee 	{"Output Driver R Stage 2", NULL, "Output Driver R Stage 1"},
658aab1ad11SSeven Lee 	{"Output Driver L Stage 3", NULL, "Output Driver L Stage 2"},
659aab1ad11SSeven Lee 	{"Output Driver R Stage 3", NULL, "Output Driver R Stage 2"},
660aab1ad11SSeven Lee 	{"Output DACL", NULL, "Output Driver L Stage 3"},
661aab1ad11SSeven Lee 	{"Output DACR", NULL, "Output Driver R Stage 3"},
662aab1ad11SSeven Lee 
663aab1ad11SSeven Lee 	{"HPOL Pulldown", NULL, "Output DACL"},
664aab1ad11SSeven Lee 	{"HPOR Pulldown", NULL, "Output DACR"},
665aab1ad11SSeven Lee 	{"HP Boost Driver", NULL, "HPOL Pulldown"},
666aab1ad11SSeven Lee 	{"HP Boost Driver", NULL, "HPOR Pulldown"},
667aab1ad11SSeven Lee 
668aab1ad11SSeven Lee 	{"Class G", NULL, "HP Boost Driver"},
669aab1ad11SSeven Lee 	{"HPOL", NULL, "Class G"},
670aab1ad11SSeven Lee 	{"HPOR", NULL, "Class G"},
671aab1ad11SSeven Lee };
672aab1ad11SSeven Lee 
673*cf507187STakashi Iwai static const struct nau8821_osr_attr *
674*cf507187STakashi Iwai nau8821_get_osr(struct nau8821 *nau8821, int stream)
675aab1ad11SSeven Lee {
676*cf507187STakashi Iwai 	unsigned int osr;
677aab1ad11SSeven Lee 
678aab1ad11SSeven Lee 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
679*cf507187STakashi Iwai 		regmap_read(nau8821->regmap, NAU8821_R2C_DAC_CTRL1, &osr);
680*cf507187STakashi Iwai 		osr &= NAU8821_DAC_OVERSAMPLE_MASK;
681aab1ad11SSeven Lee 		if (osr >= ARRAY_SIZE(osr_dac_sel))
682*cf507187STakashi Iwai 			return NULL;
683*cf507187STakashi Iwai 		return &osr_dac_sel[osr];
684aab1ad11SSeven Lee 	} else {
685*cf507187STakashi Iwai 		regmap_read(nau8821->regmap, NAU8821_R2B_ADC_RATE, &osr);
686*cf507187STakashi Iwai 		osr &= NAU8821_ADC_SYNC_DOWN_MASK;
687aab1ad11SSeven Lee 		if (osr >= ARRAY_SIZE(osr_adc_sel))
688*cf507187STakashi Iwai 			return NULL;
689*cf507187STakashi Iwai 		return &osr_adc_sel[osr];
690*cf507187STakashi Iwai 	}
691aab1ad11SSeven Lee }
692aab1ad11SSeven Lee 
693*cf507187STakashi Iwai static int nau8821_dai_startup(struct snd_pcm_substream *substream,
694*cf507187STakashi Iwai 			       struct snd_soc_dai *dai)
695*cf507187STakashi Iwai {
696*cf507187STakashi Iwai 	struct snd_soc_component *component = dai->component;
697*cf507187STakashi Iwai 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
698*cf507187STakashi Iwai 	const struct nau8821_osr_attr *osr;
699aab1ad11SSeven Lee 
700*cf507187STakashi Iwai 	osr = nau8821_get_osr(nau8821, substream->stream);
701*cf507187STakashi Iwai 	if (!osr || !osr->osr)
702*cf507187STakashi Iwai 		return -EINVAL;
703*cf507187STakashi Iwai 
704*cf507187STakashi Iwai 	return snd_pcm_hw_constraint_minmax(substream->runtime,
705*cf507187STakashi Iwai 					    SNDRV_PCM_HW_PARAM_RATE,
706*cf507187STakashi Iwai 					    0, CLK_DA_AD_MAX / osr->osr);
707aab1ad11SSeven Lee }
708aab1ad11SSeven Lee 
709aab1ad11SSeven Lee static int nau8821_hw_params(struct snd_pcm_substream *substream,
710aab1ad11SSeven Lee 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
711aab1ad11SSeven Lee {
712aab1ad11SSeven Lee 	struct snd_soc_component *component = dai->component;
713aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
714*cf507187STakashi Iwai 	unsigned int val_len = 0, ctrl_val, bclk_fs, clk_div;
715*cf507187STakashi Iwai 	const struct nau8821_osr_attr *osr;
716aab1ad11SSeven Lee 
717aab1ad11SSeven Lee 	nau8821->fs = params_rate(params);
718aab1ad11SSeven Lee 	/* CLK_DAC or CLK_ADC = OSR * FS
719aab1ad11SSeven Lee 	 * DAC or ADC clock frequency is defined as Over Sampling Rate (OSR)
720aab1ad11SSeven Lee 	 * multiplied by the audio sample rate (Fs). Note that the OSR and Fs
721aab1ad11SSeven Lee 	 * values must be selected such that the maximum frequency is less
722aab1ad11SSeven Lee 	 * than 6.144 MHz.
723aab1ad11SSeven Lee 	 */
724*cf507187STakashi Iwai 	osr = nau8821_get_osr(nau8821, substream->stream);
725*cf507187STakashi Iwai 	if (!osr || !osr->osr)
726aab1ad11SSeven Lee 		return -EINVAL;
727*cf507187STakashi Iwai 	if (nau8821->fs * osr->osr > CLK_DA_AD_MAX)
728*cf507187STakashi Iwai 		return -EINVAL;
729*cf507187STakashi Iwai 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
730aab1ad11SSeven Lee 		regmap_update_bits(nau8821->regmap, NAU8821_R03_CLK_DIVIDER,
731aab1ad11SSeven Lee 			NAU8821_CLK_DAC_SRC_MASK,
732*cf507187STakashi Iwai 			osr->clk_src << NAU8821_CLK_DAC_SRC_SFT);
733*cf507187STakashi Iwai 	else
734aab1ad11SSeven Lee 		regmap_update_bits(nau8821->regmap, NAU8821_R03_CLK_DIVIDER,
735aab1ad11SSeven Lee 			NAU8821_CLK_ADC_SRC_MASK,
736*cf507187STakashi Iwai 			osr->clk_src << NAU8821_CLK_ADC_SRC_SFT);
737aab1ad11SSeven Lee 
738aab1ad11SSeven Lee 	/* make BCLK and LRC divde configuration if the codec as master. */
739aab1ad11SSeven Lee 	regmap_read(nau8821->regmap, NAU8821_R1D_I2S_PCM_CTRL2, &ctrl_val);
740aab1ad11SSeven Lee 	if (ctrl_val & NAU8821_I2S_MS_MASTER) {
741aab1ad11SSeven Lee 		/* get the bclk and fs ratio */
742aab1ad11SSeven Lee 		bclk_fs = snd_soc_params_to_bclk(params) / nau8821->fs;
743aab1ad11SSeven Lee 		if (bclk_fs <= 32)
744aab1ad11SSeven Lee 			clk_div = 3;
745aab1ad11SSeven Lee 		else if (bclk_fs <= 64)
746aab1ad11SSeven Lee 			clk_div = 2;
747aab1ad11SSeven Lee 		else if (bclk_fs <= 128)
748aab1ad11SSeven Lee 			clk_div = 1;
749aab1ad11SSeven Lee 		else {
750aab1ad11SSeven Lee 			return -EINVAL;
751aab1ad11SSeven Lee 		}
752aab1ad11SSeven Lee 		regmap_update_bits(nau8821->regmap, NAU8821_R1D_I2S_PCM_CTRL2,
753aab1ad11SSeven Lee 			NAU8821_I2S_LRC_DIV_MASK | NAU8821_I2S_BLK_DIV_MASK,
754aab1ad11SSeven Lee 			(clk_div << NAU8821_I2S_LRC_DIV_SFT) | clk_div);
755aab1ad11SSeven Lee 	}
756aab1ad11SSeven Lee 
757aab1ad11SSeven Lee 	switch (params_width(params)) {
758aab1ad11SSeven Lee 	case 16:
759aab1ad11SSeven Lee 		val_len |= NAU8821_I2S_DL_16;
760aab1ad11SSeven Lee 		break;
761aab1ad11SSeven Lee 	case 20:
762aab1ad11SSeven Lee 		val_len |= NAU8821_I2S_DL_20;
763aab1ad11SSeven Lee 		break;
764aab1ad11SSeven Lee 	case 24:
765aab1ad11SSeven Lee 		val_len |= NAU8821_I2S_DL_24;
766aab1ad11SSeven Lee 		break;
767aab1ad11SSeven Lee 	case 32:
768aab1ad11SSeven Lee 		val_len |= NAU8821_I2S_DL_32;
769aab1ad11SSeven Lee 		break;
770aab1ad11SSeven Lee 	default:
771aab1ad11SSeven Lee 		return -EINVAL;
772aab1ad11SSeven Lee 	}
773aab1ad11SSeven Lee 
774aab1ad11SSeven Lee 	regmap_update_bits(nau8821->regmap, NAU8821_R1C_I2S_PCM_CTRL1,
775aab1ad11SSeven Lee 		NAU8821_I2S_DL_MASK, val_len);
776aab1ad11SSeven Lee 
777aab1ad11SSeven Lee 	return 0;
778aab1ad11SSeven Lee }
779aab1ad11SSeven Lee 
780aab1ad11SSeven Lee static int nau8821_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
781aab1ad11SSeven Lee {
782aab1ad11SSeven Lee 	struct snd_soc_component *component = codec_dai->component;
783aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
784aab1ad11SSeven Lee 	unsigned int ctrl1_val = 0, ctrl2_val = 0;
785aab1ad11SSeven Lee 
786aab1ad11SSeven Lee 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
787aab1ad11SSeven Lee 	case SND_SOC_DAIFMT_CBP_CFP:
788aab1ad11SSeven Lee 		ctrl2_val |= NAU8821_I2S_MS_MASTER;
789aab1ad11SSeven Lee 		break;
790aab1ad11SSeven Lee 	case SND_SOC_DAIFMT_CBC_CFC:
791aab1ad11SSeven Lee 		break;
792aab1ad11SSeven Lee 	default:
793aab1ad11SSeven Lee 		return -EINVAL;
794aab1ad11SSeven Lee 	}
795aab1ad11SSeven Lee 
796aab1ad11SSeven Lee 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
797aab1ad11SSeven Lee 	case SND_SOC_DAIFMT_NB_NF:
798aab1ad11SSeven Lee 		break;
799aab1ad11SSeven Lee 	case SND_SOC_DAIFMT_IB_NF:
800aab1ad11SSeven Lee 		ctrl1_val |= NAU8821_I2S_BP_INV;
801aab1ad11SSeven Lee 		break;
802aab1ad11SSeven Lee 	default:
803aab1ad11SSeven Lee 		return -EINVAL;
804aab1ad11SSeven Lee 	}
805aab1ad11SSeven Lee 
806aab1ad11SSeven Lee 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
807aab1ad11SSeven Lee 	case SND_SOC_DAIFMT_I2S:
808aab1ad11SSeven Lee 		ctrl1_val |= NAU8821_I2S_DF_I2S;
809aab1ad11SSeven Lee 		break;
810aab1ad11SSeven Lee 	case SND_SOC_DAIFMT_LEFT_J:
811aab1ad11SSeven Lee 		ctrl1_val |= NAU8821_I2S_DF_LEFT;
812aab1ad11SSeven Lee 		break;
813aab1ad11SSeven Lee 	case SND_SOC_DAIFMT_RIGHT_J:
814aab1ad11SSeven Lee 		ctrl1_val |= NAU8821_I2S_DF_RIGTH;
815aab1ad11SSeven Lee 		break;
816aab1ad11SSeven Lee 	case SND_SOC_DAIFMT_DSP_A:
817aab1ad11SSeven Lee 		ctrl1_val |= NAU8821_I2S_DF_PCM_AB;
818aab1ad11SSeven Lee 		break;
819aab1ad11SSeven Lee 	case SND_SOC_DAIFMT_DSP_B:
820aab1ad11SSeven Lee 		ctrl1_val |= NAU8821_I2S_DF_PCM_AB;
821aab1ad11SSeven Lee 		ctrl1_val |= NAU8821_I2S_PCMB_EN;
822aab1ad11SSeven Lee 		break;
823aab1ad11SSeven Lee 	default:
824aab1ad11SSeven Lee 		return -EINVAL;
825aab1ad11SSeven Lee 	}
826aab1ad11SSeven Lee 
827aab1ad11SSeven Lee 	regmap_update_bits(nau8821->regmap, NAU8821_R1C_I2S_PCM_CTRL1,
828aab1ad11SSeven Lee 		NAU8821_I2S_DL_MASK | NAU8821_I2S_DF_MASK |
829aab1ad11SSeven Lee 		NAU8821_I2S_BP_MASK | NAU8821_I2S_PCMB_MASK, ctrl1_val);
830aab1ad11SSeven Lee 	regmap_update_bits(nau8821->regmap, NAU8821_R1D_I2S_PCM_CTRL2,
831aab1ad11SSeven Lee 		NAU8821_I2S_MS_MASK, ctrl2_val);
832aab1ad11SSeven Lee 
833aab1ad11SSeven Lee 	return 0;
834aab1ad11SSeven Lee }
835aab1ad11SSeven Lee 
836aab1ad11SSeven Lee static int nau8821_digital_mute(struct snd_soc_dai *dai, int mute,
837aab1ad11SSeven Lee 		int direction)
838aab1ad11SSeven Lee {
839aab1ad11SSeven Lee 	struct snd_soc_component *component = dai->component;
840aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
841aab1ad11SSeven Lee 	unsigned int val = 0;
842aab1ad11SSeven Lee 
843aab1ad11SSeven Lee 	if (mute)
844aab1ad11SSeven Lee 		val = NAU8821_DAC_SOFT_MUTE;
845aab1ad11SSeven Lee 
846aab1ad11SSeven Lee 	return regmap_update_bits(nau8821->regmap,
847aab1ad11SSeven Lee 		NAU8821_R31_MUTE_CTRL, NAU8821_DAC_SOFT_MUTE, val);
848aab1ad11SSeven Lee }
849aab1ad11SSeven Lee 
850aab1ad11SSeven Lee static const struct snd_soc_dai_ops nau8821_dai_ops = {
851*cf507187STakashi Iwai 	.startup = nau8821_dai_startup,
852aab1ad11SSeven Lee 	.hw_params = nau8821_hw_params,
853aab1ad11SSeven Lee 	.set_fmt = nau8821_set_dai_fmt,
854aab1ad11SSeven Lee 	.mute_stream = nau8821_digital_mute,
855aa9753a4SVijendar Mukunda 	.no_capture_mute = 1,
856aab1ad11SSeven Lee };
857aab1ad11SSeven Lee 
858aab1ad11SSeven Lee #define NAU8821_RATES SNDRV_PCM_RATE_8000_192000
859aab1ad11SSeven Lee #define NAU8821_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
860aab1ad11SSeven Lee 	| SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
861aab1ad11SSeven Lee 
862aab1ad11SSeven Lee static struct snd_soc_dai_driver nau8821_dai = {
863aab1ad11SSeven Lee 	.name = NUVOTON_CODEC_DAI,
864aab1ad11SSeven Lee 	.playback = {
865aab1ad11SSeven Lee 		.stream_name = "Playback",
866aab1ad11SSeven Lee 		.channels_min = 1,
867aab1ad11SSeven Lee 		.channels_max = 2,
868aab1ad11SSeven Lee 		.rates = NAU8821_RATES,
869aab1ad11SSeven Lee 		.formats = NAU8821_FORMATS,
870aab1ad11SSeven Lee 	},
871aab1ad11SSeven Lee 	.capture = {
872aab1ad11SSeven Lee 		.stream_name = "Capture",
873aab1ad11SSeven Lee 		.channels_min = 1,
874aab1ad11SSeven Lee 		.channels_max = 2,
875aab1ad11SSeven Lee 		.rates = NAU8821_RATES,
876aab1ad11SSeven Lee 		.formats = NAU8821_FORMATS,
877aab1ad11SSeven Lee 	},
878aab1ad11SSeven Lee 	.ops = &nau8821_dai_ops,
879aab1ad11SSeven Lee };
880aab1ad11SSeven Lee 
881aab1ad11SSeven Lee 
882aab1ad11SSeven Lee static bool nau8821_is_jack_inserted(struct regmap *regmap)
883aab1ad11SSeven Lee {
884aab1ad11SSeven Lee 	bool active_high, is_high;
885aab1ad11SSeven Lee 	int status, jkdet;
886aab1ad11SSeven Lee 
887aab1ad11SSeven Lee 	regmap_read(regmap, NAU8821_R0D_JACK_DET_CTRL, &jkdet);
888aab1ad11SSeven Lee 	active_high = jkdet & NAU8821_JACK_POLARITY;
889aab1ad11SSeven Lee 	regmap_read(regmap, NAU8821_R82_GENERAL_STATUS, &status);
890aab1ad11SSeven Lee 	is_high = status & NAU8821_GPIO2_IN;
891aab1ad11SSeven Lee 	/* return jack connection status according to jack insertion logic
892aab1ad11SSeven Lee 	 * active high or active low.
893aab1ad11SSeven Lee 	 */
894aab1ad11SSeven Lee 	return active_high == is_high;
895aab1ad11SSeven Lee }
896aab1ad11SSeven Lee 
897aab1ad11SSeven Lee static void nau8821_int_status_clear_all(struct regmap *regmap)
898aab1ad11SSeven Lee {
899aab1ad11SSeven Lee 	int active_irq, clear_irq, i;
900aab1ad11SSeven Lee 
901aab1ad11SSeven Lee 	/* Reset the intrruption status from rightmost bit if the corres-
902aab1ad11SSeven Lee 	 * ponding irq event occurs.
903aab1ad11SSeven Lee 	 */
904aab1ad11SSeven Lee 	regmap_read(regmap, NAU8821_R10_IRQ_STATUS, &active_irq);
905aab1ad11SSeven Lee 	for (i = 0; i < NAU8821_REG_DATA_LEN; i++) {
906aab1ad11SSeven Lee 		clear_irq = (0x1 << i);
907aab1ad11SSeven Lee 		if (active_irq & clear_irq)
908aab1ad11SSeven Lee 			regmap_write(regmap,
909aab1ad11SSeven Lee 				NAU8821_R11_INT_CLR_KEY_STATUS, clear_irq);
910aab1ad11SSeven Lee 	}
911aab1ad11SSeven Lee }
912aab1ad11SSeven Lee 
913aab1ad11SSeven Lee static void nau8821_eject_jack(struct nau8821 *nau8821)
914aab1ad11SSeven Lee {
915aab1ad11SSeven Lee 	struct snd_soc_dapm_context *dapm = nau8821->dapm;
916aab1ad11SSeven Lee 	struct regmap *regmap = nau8821->regmap;
917aab1ad11SSeven Lee 	struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
918aab1ad11SSeven Lee 
919aab1ad11SSeven Lee 	/* Detach 2kOhm Resistors from MICBIAS to MICGND */
920aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS,
921aab1ad11SSeven Lee 		NAU8821_MICBIAS_JKR2, 0);
922aab1ad11SSeven Lee 	/* HPL/HPR short to ground */
923aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
924aab1ad11SSeven Lee 		NAU8821_SPKR_DWN1R | NAU8821_SPKR_DWN1L, 0);
925aab1ad11SSeven Lee 	snd_soc_component_disable_pin(component, "MICBIAS");
926aab1ad11SSeven Lee 	snd_soc_dapm_sync(dapm);
927aab1ad11SSeven Lee 
928aab1ad11SSeven Lee 	/* Clear all interruption status */
929aab1ad11SSeven Lee 	nau8821_int_status_clear_all(regmap);
930aab1ad11SSeven Lee 
931aab1ad11SSeven Lee 	/* Enable the insertion interruption, disable the ejection inter-
932aab1ad11SSeven Lee 	 * ruption, and then bypass de-bounce circuit.
933aab1ad11SSeven Lee 	 */
934aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R12_INTERRUPT_DIS_CTRL,
935aab1ad11SSeven Lee 		NAU8821_IRQ_EJECT_DIS | NAU8821_IRQ_INSERT_DIS,
936aab1ad11SSeven Lee 		NAU8821_IRQ_EJECT_DIS);
937aab1ad11SSeven Lee 	/* Mask unneeded IRQs: 1 - disable, 0 - enable */
938aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
939aab1ad11SSeven Lee 		NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN,
940aab1ad11SSeven Lee 		NAU8821_IRQ_EJECT_EN);
941aab1ad11SSeven Lee 
942aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
943aab1ad11SSeven Lee 		NAU8821_JACK_DET_DB_BYPASS, NAU8821_JACK_DET_DB_BYPASS);
944aab1ad11SSeven Lee 
945aab1ad11SSeven Lee 	/* Close clock for jack type detection at manual mode */
946aab1ad11SSeven Lee 	if (dapm->bias_level < SND_SOC_BIAS_PREPARE)
947aab1ad11SSeven Lee 		nau8821_configure_sysclk(nau8821, NAU8821_CLK_DIS, 0);
948aab1ad11SSeven Lee 
949aab1ad11SSeven Lee 	/* Recover to normal channel input */
950aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R2B_ADC_RATE,
951aab1ad11SSeven Lee 			NAU8821_ADC_R_SRC_EN, 0);
9522551b6e8SSeven Lee 	if (nau8821->key_enable) {
9532551b6e8SSeven Lee 		regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
9542551b6e8SSeven Lee 			NAU8821_IRQ_KEY_RELEASE_EN |
9552551b6e8SSeven Lee 			NAU8821_IRQ_KEY_PRESS_EN,
9562551b6e8SSeven Lee 			NAU8821_IRQ_KEY_RELEASE_EN |
9572551b6e8SSeven Lee 			NAU8821_IRQ_KEY_PRESS_EN);
9582551b6e8SSeven Lee 		regmap_update_bits(regmap,
9592551b6e8SSeven Lee 			NAU8821_R12_INTERRUPT_DIS_CTRL,
9602551b6e8SSeven Lee 			NAU8821_IRQ_KEY_RELEASE_DIS |
9612551b6e8SSeven Lee 			NAU8821_IRQ_KEY_PRESS_DIS,
9622551b6e8SSeven Lee 			NAU8821_IRQ_KEY_RELEASE_DIS |
9632551b6e8SSeven Lee 			NAU8821_IRQ_KEY_PRESS_DIS);
9642551b6e8SSeven Lee 	}
9652551b6e8SSeven Lee 
966aab1ad11SSeven Lee }
967aab1ad11SSeven Lee 
968aab1ad11SSeven Lee static void nau8821_jdet_work(struct work_struct *work)
969aab1ad11SSeven Lee {
970aab1ad11SSeven Lee 	struct nau8821 *nau8821 =
971aab1ad11SSeven Lee 		container_of(work, struct nau8821, jdet_work);
972aab1ad11SSeven Lee 	struct snd_soc_dapm_context *dapm = nau8821->dapm;
973aab1ad11SSeven Lee 	struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
974aab1ad11SSeven Lee 	struct regmap *regmap = nau8821->regmap;
975aab1ad11SSeven Lee 	int jack_status_reg, mic_detected, event = 0, event_mask = 0;
976aab1ad11SSeven Lee 
977aab1ad11SSeven Lee 	snd_soc_component_force_enable_pin(component, "MICBIAS");
978aab1ad11SSeven Lee 	snd_soc_dapm_sync(dapm);
979aab1ad11SSeven Lee 	msleep(20);
980aab1ad11SSeven Lee 
981aab1ad11SSeven Lee 	regmap_read(regmap, NAU8821_R58_I2C_DEVICE_ID, &jack_status_reg);
982aab1ad11SSeven Lee 	mic_detected = !(jack_status_reg & NAU8821_KEYDET);
983aab1ad11SSeven Lee 	if (mic_detected) {
984aab1ad11SSeven Lee 		dev_dbg(nau8821->dev, "Headset connected\n");
985aab1ad11SSeven Lee 		event |= SND_JACK_HEADSET;
986aab1ad11SSeven Lee 
987aab1ad11SSeven Lee 		/* 2kOhm Resistor from MICBIAS to MICGND1 */
988aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS,
989aab1ad11SSeven Lee 			NAU8821_MICBIAS_JKR2, NAU8821_MICBIAS_JKR2);
990aab1ad11SSeven Lee 		/* Latch Right Channel Analog data
991aab1ad11SSeven Lee 		 * input into the Right Channel Filter
992aab1ad11SSeven Lee 		 */
993aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R2B_ADC_RATE,
994aab1ad11SSeven Lee 			NAU8821_ADC_R_SRC_EN, NAU8821_ADC_R_SRC_EN);
9952551b6e8SSeven Lee 		if (nau8821->key_enable) {
9962551b6e8SSeven Lee 			regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
9972551b6e8SSeven Lee 				NAU8821_IRQ_KEY_RELEASE_EN |
9982551b6e8SSeven Lee 				NAU8821_IRQ_KEY_PRESS_EN, 0);
9992551b6e8SSeven Lee 			regmap_update_bits(regmap,
10002551b6e8SSeven Lee 				NAU8821_R12_INTERRUPT_DIS_CTRL,
10012551b6e8SSeven Lee 				NAU8821_IRQ_KEY_RELEASE_DIS |
10022551b6e8SSeven Lee 				NAU8821_IRQ_KEY_PRESS_DIS, 0);
10032551b6e8SSeven Lee 		}
1004aab1ad11SSeven Lee 	} else {
1005aab1ad11SSeven Lee 		dev_dbg(nau8821->dev, "Headphone connected\n");
1006aab1ad11SSeven Lee 		event |= SND_JACK_HEADPHONE;
1007aab1ad11SSeven Lee 		snd_soc_component_disable_pin(component, "MICBIAS");
1008aab1ad11SSeven Lee 		snd_soc_dapm_sync(dapm);
1009aab1ad11SSeven Lee 	}
1010aab1ad11SSeven Lee 	event_mask |= SND_JACK_HEADSET;
1011aab1ad11SSeven Lee 	snd_soc_jack_report(nau8821->jack, event, event_mask);
1012aab1ad11SSeven Lee }
1013aab1ad11SSeven Lee 
1014aab1ad11SSeven Lee /* Enable interruptions with internal clock. */
1015aab1ad11SSeven Lee static void nau8821_setup_inserted_irq(struct nau8821 *nau8821)
1016aab1ad11SSeven Lee {
1017aab1ad11SSeven Lee 	struct regmap *regmap = nau8821->regmap;
1018aab1ad11SSeven Lee 
1019aab1ad11SSeven Lee 	/* Enable internal VCO needed for interruptions */
1020aab1ad11SSeven Lee 	if (nau8821->dapm->bias_level < SND_SOC_BIAS_PREPARE)
1021aab1ad11SSeven Lee 		nau8821_configure_sysclk(nau8821, NAU8821_CLK_INTERNAL, 0);
1022aab1ad11SSeven Lee 
1023aab1ad11SSeven Lee 	/* Chip needs one FSCLK cycle in order to generate interruptions,
1024aab1ad11SSeven Lee 	 * as we cannot guarantee one will be provided by the system. Turning
1025aab1ad11SSeven Lee 	 * master mode on then off enables us to generate that FSCLK cycle
1026aab1ad11SSeven Lee 	 * with a minimum of contention on the clock bus.
1027aab1ad11SSeven Lee 	 */
1028aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R1D_I2S_PCM_CTRL2,
1029aab1ad11SSeven Lee 		NAU8821_I2S_MS_MASK, NAU8821_I2S_MS_MASTER);
1030aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R1D_I2S_PCM_CTRL2,
1031aab1ad11SSeven Lee 		NAU8821_I2S_MS_MASK, NAU8821_I2S_MS_SLAVE);
1032aab1ad11SSeven Lee 
1033aab1ad11SSeven Lee 	/* Not bypass de-bounce circuit */
1034aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
1035aab1ad11SSeven Lee 		NAU8821_JACK_DET_DB_BYPASS, 0);
1036aab1ad11SSeven Lee 
1037aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
1038aab1ad11SSeven Lee 		NAU8821_IRQ_EJECT_EN, 0);
1039aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R12_INTERRUPT_DIS_CTRL,
1040aab1ad11SSeven Lee 		NAU8821_IRQ_EJECT_DIS, 0);
1041aab1ad11SSeven Lee }
1042aab1ad11SSeven Lee 
1043aab1ad11SSeven Lee static irqreturn_t nau8821_interrupt(int irq, void *data)
1044aab1ad11SSeven Lee {
1045aab1ad11SSeven Lee 	struct nau8821 *nau8821 = (struct nau8821 *)data;
1046aab1ad11SSeven Lee 	struct regmap *regmap = nau8821->regmap;
1047aab1ad11SSeven Lee 	int active_irq, clear_irq = 0, event = 0, event_mask = 0;
1048aab1ad11SSeven Lee 
1049aab1ad11SSeven Lee 	if (regmap_read(regmap, NAU8821_R10_IRQ_STATUS, &active_irq)) {
1050aab1ad11SSeven Lee 		dev_err(nau8821->dev, "failed to read irq status\n");
1051aab1ad11SSeven Lee 		return IRQ_NONE;
1052aab1ad11SSeven Lee 	}
1053aab1ad11SSeven Lee 
1054aab1ad11SSeven Lee 	dev_dbg(nau8821->dev, "IRQ %d\n", active_irq);
1055aab1ad11SSeven Lee 
1056aab1ad11SSeven Lee 	if ((active_irq & NAU8821_JACK_EJECT_IRQ_MASK) ==
1057aab1ad11SSeven Lee 		NAU8821_JACK_EJECT_DETECTED) {
1058aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R71_ANALOG_ADC_1,
1059aab1ad11SSeven Lee 			NAU8821_MICDET_MASK, NAU8821_MICDET_DIS);
1060aab1ad11SSeven Lee 		nau8821_eject_jack(nau8821);
1061aab1ad11SSeven Lee 		event_mask |= SND_JACK_HEADSET;
1062aab1ad11SSeven Lee 		clear_irq = NAU8821_JACK_EJECT_IRQ_MASK;
10632551b6e8SSeven Lee 	} else if (active_irq & NAU8821_KEY_SHORT_PRESS_IRQ) {
10642551b6e8SSeven Lee 		event |= NAU8821_BUTTON;
10652551b6e8SSeven Lee 		event_mask |= NAU8821_BUTTON;
10662551b6e8SSeven Lee 		clear_irq = NAU8821_KEY_SHORT_PRESS_IRQ;
10672551b6e8SSeven Lee 	} else if (active_irq & NAU8821_KEY_RELEASE_IRQ) {
10682551b6e8SSeven Lee 		event_mask = NAU8821_BUTTON;
10692551b6e8SSeven Lee 		clear_irq = NAU8821_KEY_RELEASE_IRQ;
1070aab1ad11SSeven Lee 	} else if ((active_irq & NAU8821_JACK_INSERT_IRQ_MASK) ==
1071aab1ad11SSeven Lee 		NAU8821_JACK_INSERT_DETECTED) {
1072aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R71_ANALOG_ADC_1,
1073aab1ad11SSeven Lee 			NAU8821_MICDET_MASK, NAU8821_MICDET_EN);
1074aab1ad11SSeven Lee 		if (nau8821_is_jack_inserted(regmap)) {
1075aab1ad11SSeven Lee 			/* detect microphone and jack type */
1076aab1ad11SSeven Lee 			cancel_work_sync(&nau8821->jdet_work);
1077aab1ad11SSeven Lee 			schedule_work(&nau8821->jdet_work);
1078aab1ad11SSeven Lee 			/* Turn off insertion interruption at manual mode */
1079aab1ad11SSeven Lee 			regmap_update_bits(regmap,
1080aab1ad11SSeven Lee 				NAU8821_R12_INTERRUPT_DIS_CTRL,
1081aab1ad11SSeven Lee 				NAU8821_IRQ_INSERT_DIS,
1082aab1ad11SSeven Lee 				NAU8821_IRQ_INSERT_DIS);
1083aab1ad11SSeven Lee 			regmap_update_bits(regmap,
1084aab1ad11SSeven Lee 				NAU8821_R0F_INTERRUPT_MASK,
1085aab1ad11SSeven Lee 				NAU8821_IRQ_INSERT_EN,
1086aab1ad11SSeven Lee 				NAU8821_IRQ_INSERT_EN);
1087aab1ad11SSeven Lee 			nau8821_setup_inserted_irq(nau8821);
1088aab1ad11SSeven Lee 		} else {
1089aab1ad11SSeven Lee 			dev_warn(nau8821->dev,
1090aab1ad11SSeven Lee 				"Inserted IRQ fired but not connected\n");
1091aab1ad11SSeven Lee 			nau8821_eject_jack(nau8821);
1092aab1ad11SSeven Lee 		}
1093aab1ad11SSeven Lee 	}
1094aab1ad11SSeven Lee 
1095aab1ad11SSeven Lee 	if (!clear_irq)
1096aab1ad11SSeven Lee 		clear_irq = active_irq;
1097aab1ad11SSeven Lee 	/* clears the rightmost interruption */
1098aab1ad11SSeven Lee 	regmap_write(regmap, NAU8821_R11_INT_CLR_KEY_STATUS, clear_irq);
1099aab1ad11SSeven Lee 
1100aab1ad11SSeven Lee 	if (event_mask)
1101aab1ad11SSeven Lee 		snd_soc_jack_report(nau8821->jack, event, event_mask);
1102aab1ad11SSeven Lee 
1103aab1ad11SSeven Lee 	return IRQ_HANDLED;
1104aab1ad11SSeven Lee }
1105aab1ad11SSeven Lee 
1106aab1ad11SSeven Lee static const struct regmap_config nau8821_regmap_config = {
1107aab1ad11SSeven Lee 	.val_bits = NAU8821_REG_DATA_LEN,
1108aab1ad11SSeven Lee 	.reg_bits = NAU8821_REG_ADDR_LEN,
1109aab1ad11SSeven Lee 
1110aab1ad11SSeven Lee 	.max_register = NAU8821_REG_MAX,
1111aab1ad11SSeven Lee 	.readable_reg = nau8821_readable_reg,
1112aab1ad11SSeven Lee 	.writeable_reg = nau8821_writeable_reg,
1113aab1ad11SSeven Lee 	.volatile_reg = nau8821_volatile_reg,
1114aab1ad11SSeven Lee 
1115aab1ad11SSeven Lee 	.cache_type = REGCACHE_RBTREE,
1116aab1ad11SSeven Lee 	.reg_defaults = nau8821_reg_defaults,
1117aab1ad11SSeven Lee 	.num_reg_defaults = ARRAY_SIZE(nau8821_reg_defaults),
1118aab1ad11SSeven Lee };
1119aab1ad11SSeven Lee 
1120aab1ad11SSeven Lee static int nau8821_component_probe(struct snd_soc_component *component)
1121aab1ad11SSeven Lee {
1122aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
1123aab1ad11SSeven Lee 	struct snd_soc_dapm_context *dapm =
1124aab1ad11SSeven Lee 		snd_soc_component_get_dapm(component);
1125aab1ad11SSeven Lee 
1126aab1ad11SSeven Lee 	nau8821->dapm = dapm;
1127aab1ad11SSeven Lee 
1128aab1ad11SSeven Lee 	return 0;
1129aab1ad11SSeven Lee }
1130aab1ad11SSeven Lee 
1131aab1ad11SSeven Lee /**
1132aab1ad11SSeven Lee  * nau8821_calc_fll_param - Calculate FLL parameters.
1133aab1ad11SSeven Lee  * @fll_in: external clock provided to codec.
1134aab1ad11SSeven Lee  * @fs: sampling rate.
1135aab1ad11SSeven Lee  * @fll_param: Pointer to structure of FLL parameters.
1136aab1ad11SSeven Lee  *
1137aab1ad11SSeven Lee  * Calculate FLL parameters to configure codec.
1138aab1ad11SSeven Lee  *
1139aab1ad11SSeven Lee  * Returns 0 for success or negative error code.
1140aab1ad11SSeven Lee  */
1141aab1ad11SSeven Lee static int nau8821_calc_fll_param(unsigned int fll_in,
1142aab1ad11SSeven Lee 	unsigned int fs, struct nau8821_fll *fll_param)
1143aab1ad11SSeven Lee {
1144aab1ad11SSeven Lee 	u64 fvco, fvco_max;
1145aab1ad11SSeven Lee 	unsigned int fref, i, fvco_sel;
1146aab1ad11SSeven Lee 
1147aab1ad11SSeven Lee 	/* Ensure the reference clock frequency (FREF) is <= 13.5MHz by
1148aab1ad11SSeven Lee 	 * dividing freq_in by 1, 2, 4, or 8 using FLL pre-scalar.
1149aab1ad11SSeven Lee 	 * FREF = freq_in / NAU8821_FLL_REF_DIV_MASK
1150aab1ad11SSeven Lee 	 */
1151aab1ad11SSeven Lee 	for (i = 0; i < ARRAY_SIZE(fll_pre_scalar); i++) {
1152aab1ad11SSeven Lee 		fref = fll_in >> fll_pre_scalar[i].param;
1153aab1ad11SSeven Lee 		if (fref <= NAU_FREF_MAX)
1154aab1ad11SSeven Lee 			break;
1155aab1ad11SSeven Lee 	}
1156aab1ad11SSeven Lee 	if (i == ARRAY_SIZE(fll_pre_scalar))
1157aab1ad11SSeven Lee 		return -EINVAL;
1158aab1ad11SSeven Lee 	fll_param->clk_ref_div = fll_pre_scalar[i].val;
1159aab1ad11SSeven Lee 
1160aab1ad11SSeven Lee 	/* Choose the FLL ratio based on FREF */
1161aab1ad11SSeven Lee 	for (i = 0; i < ARRAY_SIZE(fll_ratio); i++) {
1162aab1ad11SSeven Lee 		if (fref >= fll_ratio[i].param)
1163aab1ad11SSeven Lee 			break;
1164aab1ad11SSeven Lee 	}
1165aab1ad11SSeven Lee 	if (i == ARRAY_SIZE(fll_ratio))
1166aab1ad11SSeven Lee 		return -EINVAL;
1167aab1ad11SSeven Lee 	fll_param->ratio = fll_ratio[i].val;
1168aab1ad11SSeven Lee 
1169aab1ad11SSeven Lee 	/* Calculate the frequency of DCO (FDCO) given freq_out = 256 * Fs.
1170aab1ad11SSeven Lee 	 * FDCO must be within the 90MHz - 100MHz or the FFL cannot be
1171aab1ad11SSeven Lee 	 * guaranteed across the full range of operation.
1172aab1ad11SSeven Lee 	 * FDCO = freq_out * 2 * mclk_src_scaling
1173aab1ad11SSeven Lee 	 */
1174aab1ad11SSeven Lee 	fvco_max = 0;
1175aab1ad11SSeven Lee 	fvco_sel = ARRAY_SIZE(mclk_src_scaling);
1176aab1ad11SSeven Lee 	for (i = 0; i < ARRAY_SIZE(mclk_src_scaling); i++) {
1177aab1ad11SSeven Lee 		fvco = 256ULL * fs * 2 * mclk_src_scaling[i].param;
1178aab1ad11SSeven Lee 		if (fvco > NAU_FVCO_MIN && fvco < NAU_FVCO_MAX &&
1179aab1ad11SSeven Lee 			fvco_max < fvco) {
1180aab1ad11SSeven Lee 			fvco_max = fvco;
1181aab1ad11SSeven Lee 			fvco_sel = i;
1182aab1ad11SSeven Lee 		}
1183aab1ad11SSeven Lee 	}
1184aab1ad11SSeven Lee 	if (ARRAY_SIZE(mclk_src_scaling) == fvco_sel)
1185aab1ad11SSeven Lee 		return -EINVAL;
1186aab1ad11SSeven Lee 	fll_param->mclk_src = mclk_src_scaling[fvco_sel].val;
1187aab1ad11SSeven Lee 
1188aab1ad11SSeven Lee 	/* Calculate the FLL 10-bit integer input and the FLL 24-bit fractional
1189aab1ad11SSeven Lee 	 * input based on FDCO, FREF and FLL ratio.
1190aab1ad11SSeven Lee 	 */
1191aab1ad11SSeven Lee 	fvco = div_u64(fvco_max << 24, fref * fll_param->ratio);
1192aab1ad11SSeven Lee 	fll_param->fll_int = (fvco >> 24) & 0x3ff;
1193aab1ad11SSeven Lee 	fll_param->fll_frac = fvco & 0xffffff;
1194aab1ad11SSeven Lee 
1195aab1ad11SSeven Lee 	return 0;
1196aab1ad11SSeven Lee }
1197aab1ad11SSeven Lee 
1198aab1ad11SSeven Lee static void nau8821_fll_apply(struct nau8821 *nau8821,
1199aab1ad11SSeven Lee 		struct nau8821_fll *fll_param)
1200aab1ad11SSeven Lee {
1201aab1ad11SSeven Lee 	struct regmap *regmap = nau8821->regmap;
1202aab1ad11SSeven Lee 
1203aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER,
1204aab1ad11SSeven Lee 		NAU8821_CLK_SRC_MASK | NAU8821_CLK_MCLK_SRC_MASK,
1205aab1ad11SSeven Lee 		NAU8821_CLK_SRC_MCLK | fll_param->mclk_src);
1206aab1ad11SSeven Lee 	/* Make DSP operate at high speed for better performance. */
1207aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R04_FLL1,
1208aab1ad11SSeven Lee 		NAU8821_FLL_RATIO_MASK | NAU8821_ICTRL_LATCH_MASK,
1209aab1ad11SSeven Lee 		fll_param->ratio | (0x6 << NAU8821_ICTRL_LATCH_SFT));
1210aab1ad11SSeven Lee 	/* FLL 24-bit fractional input */
1211aab1ad11SSeven Lee 	regmap_write(regmap, NAU8821_R0A_FLL7,
1212aab1ad11SSeven Lee 		(fll_param->fll_frac >> 16) & 0xff);
1213aab1ad11SSeven Lee 	regmap_write(regmap, NAU8821_R0B_FLL8, fll_param->fll_frac & 0xffff);
1214aab1ad11SSeven Lee 	/* FLL 10-bit integer input */
1215aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R06_FLL3,
1216aab1ad11SSeven Lee 		NAU8821_FLL_INTEGER_MASK, fll_param->fll_int);
1217aab1ad11SSeven Lee 	/* FLL pre-scaler */
1218aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R07_FLL4,
1219aab1ad11SSeven Lee 		NAU8821_HIGHBW_EN | NAU8821_FLL_REF_DIV_MASK,
1220aab1ad11SSeven Lee 		NAU8821_HIGHBW_EN |
1221aab1ad11SSeven Lee 		(fll_param->clk_ref_div << NAU8821_FLL_REF_DIV_SFT));
1222aab1ad11SSeven Lee 	/* select divided VCO input */
1223aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R08_FLL5,
1224aab1ad11SSeven Lee 		NAU8821_FLL_CLK_SW_MASK, NAU8821_FLL_CLK_SW_REF);
1225aab1ad11SSeven Lee 	/* Disable free-running mode */
1226aab1ad11SSeven Lee 	regmap_update_bits(regmap,
1227aab1ad11SSeven Lee 		NAU8821_R09_FLL6, NAU8821_DCO_EN, 0);
1228aab1ad11SSeven Lee 	if (fll_param->fll_frac) {
1229aab1ad11SSeven Lee 		/* set FLL loop filter enable and cutoff frequency at 500Khz */
1230aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R08_FLL5,
1231aab1ad11SSeven Lee 			NAU8821_FLL_PDB_DAC_EN | NAU8821_FLL_LOOP_FTR_EN |
1232aab1ad11SSeven Lee 			NAU8821_FLL_FTR_SW_MASK,
1233aab1ad11SSeven Lee 			NAU8821_FLL_PDB_DAC_EN | NAU8821_FLL_LOOP_FTR_EN |
1234aab1ad11SSeven Lee 			NAU8821_FLL_FTR_SW_FILTER);
1235aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R09_FLL6,
1236aab1ad11SSeven Lee 			NAU8821_SDM_EN | NAU8821_CUTOFF500,
1237aab1ad11SSeven Lee 			NAU8821_SDM_EN | NAU8821_CUTOFF500);
1238aab1ad11SSeven Lee 	} else {
1239aab1ad11SSeven Lee 		/* disable FLL loop filter and cutoff frequency */
1240aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R08_FLL5,
1241aab1ad11SSeven Lee 			NAU8821_FLL_PDB_DAC_EN | NAU8821_FLL_LOOP_FTR_EN |
1242aab1ad11SSeven Lee 			NAU8821_FLL_FTR_SW_MASK, NAU8821_FLL_FTR_SW_ACCU);
1243aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R09_FLL6,
1244aab1ad11SSeven Lee 			NAU8821_SDM_EN | NAU8821_CUTOFF500, 0);
1245aab1ad11SSeven Lee 	}
1246aab1ad11SSeven Lee }
1247aab1ad11SSeven Lee 
1248aab1ad11SSeven Lee /**
1249aab1ad11SSeven Lee  * nau8821_set_fll - FLL configuration of nau8821
1250765e08bdSPierre-Louis Bossart  * @component:  codec component
1251765e08bdSPierre-Louis Bossart  * @pll_id:  PLL requested
1252765e08bdSPierre-Louis Bossart  * @source:  clock source
1253aab1ad11SSeven Lee  * @freq_in:  frequency of input clock source
1254aab1ad11SSeven Lee  * @freq_out:  must be 256*Fs in order to achieve the best performance
1255aab1ad11SSeven Lee  *
1256aab1ad11SSeven Lee  * The FLL function can select BCLK or MCLK as the input clock source.
1257aab1ad11SSeven Lee  *
1258aab1ad11SSeven Lee  * Returns 0 if the parameters have been applied successfully
1259aab1ad11SSeven Lee  * or negative error code.
1260aab1ad11SSeven Lee  */
1261aab1ad11SSeven Lee static int nau8821_set_fll(struct snd_soc_component *component,
1262aab1ad11SSeven Lee 	int pll_id, int source, unsigned int freq_in, unsigned int freq_out)
1263aab1ad11SSeven Lee {
1264aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
1265aab1ad11SSeven Lee 	struct nau8821_fll fll_set_param, *fll_param = &fll_set_param;
1266aab1ad11SSeven Lee 	int ret, fs;
1267aab1ad11SSeven Lee 
1268aab1ad11SSeven Lee 	fs = freq_out >> 8;
1269aab1ad11SSeven Lee 	ret = nau8821_calc_fll_param(freq_in, fs, fll_param);
1270aab1ad11SSeven Lee 	if (ret) {
1271aab1ad11SSeven Lee 		dev_err(nau8821->dev,
1272aab1ad11SSeven Lee 			"Unsupported input clock %d to output clock %d\n",
1273aab1ad11SSeven Lee 			freq_in, freq_out);
1274aab1ad11SSeven Lee 		return ret;
1275aab1ad11SSeven Lee 	}
1276aab1ad11SSeven Lee 	dev_dbg(nau8821->dev,
1277aab1ad11SSeven Lee 		"mclk_src=%x ratio=%x fll_frac=%x fll_int=%x clk_ref_div=%x\n",
1278aab1ad11SSeven Lee 		fll_param->mclk_src, fll_param->ratio, fll_param->fll_frac,
1279aab1ad11SSeven Lee 		fll_param->fll_int, fll_param->clk_ref_div);
1280aab1ad11SSeven Lee 
1281aab1ad11SSeven Lee 	nau8821_fll_apply(nau8821, fll_param);
1282aab1ad11SSeven Lee 	mdelay(2);
1283aab1ad11SSeven Lee 	regmap_update_bits(nau8821->regmap, NAU8821_R03_CLK_DIVIDER,
1284aab1ad11SSeven Lee 		NAU8821_CLK_SRC_MASK, NAU8821_CLK_SRC_VCO);
1285aab1ad11SSeven Lee 
1286aab1ad11SSeven Lee 	return 0;
1287aab1ad11SSeven Lee }
1288aab1ad11SSeven Lee 
1289aab1ad11SSeven Lee static void nau8821_configure_mclk_as_sysclk(struct regmap *regmap)
1290aab1ad11SSeven Lee {
1291aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER,
1292aab1ad11SSeven Lee 		NAU8821_CLK_SRC_MASK, NAU8821_CLK_SRC_MCLK);
1293aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R09_FLL6,
1294aab1ad11SSeven Lee 		NAU8821_DCO_EN, 0);
1295aab1ad11SSeven Lee 	/* Make DSP operate as default setting for power saving. */
1296aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R04_FLL1,
1297aab1ad11SSeven Lee 		NAU8821_ICTRL_LATCH_MASK, 0);
1298aab1ad11SSeven Lee }
1299aab1ad11SSeven Lee 
1300aab1ad11SSeven Lee static int nau8821_configure_sysclk(struct nau8821 *nau8821,
1301aab1ad11SSeven Lee 	int clk_id, unsigned int freq)
1302aab1ad11SSeven Lee {
1303aab1ad11SSeven Lee 	struct regmap *regmap = nau8821->regmap;
1304aab1ad11SSeven Lee 
1305aab1ad11SSeven Lee 	switch (clk_id) {
1306aab1ad11SSeven Lee 	case NAU8821_CLK_DIS:
1307aab1ad11SSeven Lee 		/* Clock provided externally and disable internal VCO clock */
1308aab1ad11SSeven Lee 		nau8821_configure_mclk_as_sysclk(regmap);
1309aab1ad11SSeven Lee 		break;
1310aab1ad11SSeven Lee 	case NAU8821_CLK_MCLK:
1311aab1ad11SSeven Lee 		nau8821_configure_mclk_as_sysclk(regmap);
1312aab1ad11SSeven Lee 		/* MCLK not changed by clock tree */
1313aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER,
1314aab1ad11SSeven Lee 			NAU8821_CLK_MCLK_SRC_MASK, 0);
1315aab1ad11SSeven Lee 		break;
1316aab1ad11SSeven Lee 	case NAU8821_CLK_INTERNAL:
1317aab1ad11SSeven Lee 		if (nau8821_is_jack_inserted(regmap)) {
1318aab1ad11SSeven Lee 			regmap_update_bits(regmap, NAU8821_R09_FLL6,
1319aab1ad11SSeven Lee 				NAU8821_DCO_EN, NAU8821_DCO_EN);
1320aab1ad11SSeven Lee 			regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER,
1321aab1ad11SSeven Lee 				NAU8821_CLK_SRC_MASK, NAU8821_CLK_SRC_VCO);
1322aab1ad11SSeven Lee 			/* Decrease the VCO frequency and make DSP operate
1323aab1ad11SSeven Lee 			 * as default setting for power saving.
1324aab1ad11SSeven Lee 			 */
1325aab1ad11SSeven Lee 			regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER,
1326aab1ad11SSeven Lee 				NAU8821_CLK_MCLK_SRC_MASK, 0xf);
1327aab1ad11SSeven Lee 			regmap_update_bits(regmap, NAU8821_R04_FLL1,
1328aab1ad11SSeven Lee 				NAU8821_ICTRL_LATCH_MASK |
1329aab1ad11SSeven Lee 				NAU8821_FLL_RATIO_MASK, 0x10);
1330aab1ad11SSeven Lee 			regmap_update_bits(regmap, NAU8821_R09_FLL6,
1331aab1ad11SSeven Lee 				NAU8821_SDM_EN, NAU8821_SDM_EN);
1332aab1ad11SSeven Lee 		}
1333aab1ad11SSeven Lee 		break;
1334aab1ad11SSeven Lee 	case NAU8821_CLK_FLL_MCLK:
1335aab1ad11SSeven Lee 		/* Higher FLL reference input frequency can only set lower
1336aab1ad11SSeven Lee 		 * gain error, such as 0000 for input reference from MCLK
1337aab1ad11SSeven Lee 		 * 12.288Mhz.
1338aab1ad11SSeven Lee 		 */
1339aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R06_FLL3,
1340aab1ad11SSeven Lee 			NAU8821_FLL_CLK_SRC_MASK | NAU8821_GAIN_ERR_MASK,
1341aab1ad11SSeven Lee 			NAU8821_FLL_CLK_SRC_MCLK | 0);
1342aab1ad11SSeven Lee 		break;
1343aab1ad11SSeven Lee 	case NAU8821_CLK_FLL_BLK:
1344aab1ad11SSeven Lee 		/* If FLL reference input is from low frequency source,
1345aab1ad11SSeven Lee 		 * higher error gain can apply such as 0xf which has
1346aab1ad11SSeven Lee 		 * the most sensitive gain error correction threshold,
1347aab1ad11SSeven Lee 		 * Therefore, FLL has the most accurate DCO to
1348aab1ad11SSeven Lee 		 * target frequency.
1349aab1ad11SSeven Lee 		 */
1350aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R06_FLL3,
1351aab1ad11SSeven Lee 			NAU8821_FLL_CLK_SRC_MASK | NAU8821_GAIN_ERR_MASK,
1352aab1ad11SSeven Lee 			NAU8821_FLL_CLK_SRC_BLK |
1353aab1ad11SSeven Lee 			(0xf << NAU8821_GAIN_ERR_SFT));
1354aab1ad11SSeven Lee 		break;
1355aab1ad11SSeven Lee 	case NAU8821_CLK_FLL_FS:
1356aab1ad11SSeven Lee 		/* If FLL reference input is from low frequency source,
1357aab1ad11SSeven Lee 		 * higher error gain can apply such as 0xf which has
1358aab1ad11SSeven Lee 		 * the most sensitive gain error correction threshold,
1359aab1ad11SSeven Lee 		 * Therefore, FLL has the most accurate DCO to
1360aab1ad11SSeven Lee 		 * target frequency.
1361aab1ad11SSeven Lee 		 */
1362aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R06_FLL3,
1363aab1ad11SSeven Lee 			NAU8821_FLL_CLK_SRC_MASK | NAU8821_GAIN_ERR_MASK,
1364aab1ad11SSeven Lee 			NAU8821_FLL_CLK_SRC_FS |
1365aab1ad11SSeven Lee 			(0xf << NAU8821_GAIN_ERR_SFT));
1366aab1ad11SSeven Lee 		break;
1367aab1ad11SSeven Lee 	default:
1368aab1ad11SSeven Lee 		dev_err(nau8821->dev, "Invalid clock id (%d)\n", clk_id);
1369aab1ad11SSeven Lee 		return -EINVAL;
1370aab1ad11SSeven Lee 	}
1371aab1ad11SSeven Lee 	nau8821->clk_id = clk_id;
1372aab1ad11SSeven Lee 	dev_dbg(nau8821->dev, "Sysclk is %dHz and clock id is %d\n", freq,
1373aab1ad11SSeven Lee 		nau8821->clk_id);
1374aab1ad11SSeven Lee 
1375aab1ad11SSeven Lee 	return 0;
1376aab1ad11SSeven Lee }
1377aab1ad11SSeven Lee 
1378aab1ad11SSeven Lee static int nau8821_set_sysclk(struct snd_soc_component *component, int clk_id,
1379aab1ad11SSeven Lee 	int source, unsigned int freq, int dir)
1380aab1ad11SSeven Lee {
1381aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
1382aab1ad11SSeven Lee 
1383aab1ad11SSeven Lee 	return nau8821_configure_sysclk(nau8821, clk_id, freq);
1384aab1ad11SSeven Lee }
1385aab1ad11SSeven Lee 
1386aab1ad11SSeven Lee static int nau8821_resume_setup(struct nau8821 *nau8821)
1387aab1ad11SSeven Lee {
1388aab1ad11SSeven Lee 	struct regmap *regmap = nau8821->regmap;
1389aab1ad11SSeven Lee 
1390aab1ad11SSeven Lee 	/* Close clock when jack type detection at manual mode */
1391aab1ad11SSeven Lee 	nau8821_configure_sysclk(nau8821, NAU8821_CLK_DIS, 0);
1392aab1ad11SSeven Lee 	if (nau8821->irq) {
1393aab1ad11SSeven Lee 		/* Clear all interruption status */
1394aab1ad11SSeven Lee 		nau8821_int_status_clear_all(regmap);
1395aab1ad11SSeven Lee 
1396aab1ad11SSeven Lee 		/* Enable both insertion and ejection interruptions, and then
1397aab1ad11SSeven Lee 		 * bypass de-bounce circuit.
1398aab1ad11SSeven Lee 		 */
1399aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
1400aab1ad11SSeven Lee 			NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN, 0);
1401aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
1402aab1ad11SSeven Lee 			NAU8821_JACK_DET_DB_BYPASS,
1403aab1ad11SSeven Lee 			NAU8821_JACK_DET_DB_BYPASS);
1404aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R12_INTERRUPT_DIS_CTRL,
1405aab1ad11SSeven Lee 			NAU8821_IRQ_INSERT_DIS | NAU8821_IRQ_EJECT_DIS, 0);
1406aab1ad11SSeven Lee 	}
1407aab1ad11SSeven Lee 
1408aab1ad11SSeven Lee 	return 0;
1409aab1ad11SSeven Lee }
1410aab1ad11SSeven Lee 
1411aab1ad11SSeven Lee static int nau8821_set_bias_level(struct snd_soc_component *component,
1412aab1ad11SSeven Lee 		enum snd_soc_bias_level level)
1413aab1ad11SSeven Lee {
1414aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
1415aab1ad11SSeven Lee 	struct regmap *regmap = nau8821->regmap;
1416aab1ad11SSeven Lee 
1417aab1ad11SSeven Lee 	switch (level) {
1418aab1ad11SSeven Lee 	case SND_SOC_BIAS_ON:
1419aab1ad11SSeven Lee 		break;
1420aab1ad11SSeven Lee 
1421aab1ad11SSeven Lee 	case SND_SOC_BIAS_PREPARE:
1422aab1ad11SSeven Lee 		break;
1423aab1ad11SSeven Lee 
1424aab1ad11SSeven Lee 	case SND_SOC_BIAS_STANDBY:
1425aab1ad11SSeven Lee 		/* Setup codec configuration after resume */
1426aab1ad11SSeven Lee 		if (snd_soc_component_get_bias_level(component) ==
1427aab1ad11SSeven Lee 			SND_SOC_BIAS_OFF)
1428aab1ad11SSeven Lee 			nau8821_resume_setup(nau8821);
1429aab1ad11SSeven Lee 		break;
1430aab1ad11SSeven Lee 
1431aab1ad11SSeven Lee 	case SND_SOC_BIAS_OFF:
1432aab1ad11SSeven Lee 		/* HPL/HPR short to ground */
1433aab1ad11SSeven Lee 		regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
1434aab1ad11SSeven Lee 			NAU8821_SPKR_DWN1R | NAU8821_SPKR_DWN1L, 0);
1435aab1ad11SSeven Lee 		if (nau8821->irq) {
1436aab1ad11SSeven Lee 			/* Reset the configuration of jack type for detection.
1437aab1ad11SSeven Lee 			 * Detach 2kOhm Resistors from MICBIAS to MICGND1/2.
1438aab1ad11SSeven Lee 			 */
1439aab1ad11SSeven Lee 			regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS,
1440aab1ad11SSeven Lee 				NAU8821_MICBIAS_JKR2, 0);
1441aab1ad11SSeven Lee 			/* Turn off all interruptions before system shutdown.
1442aab1ad11SSeven Lee 			 * Keep theinterruption quiet before resume
1443aab1ad11SSeven Lee 			 * setup completes.
1444aab1ad11SSeven Lee 			 */
1445aab1ad11SSeven Lee 			regmap_write(regmap,
1446aab1ad11SSeven Lee 				NAU8821_R12_INTERRUPT_DIS_CTRL, 0xffff);
1447aab1ad11SSeven Lee 			regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
1448aab1ad11SSeven Lee 				NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN,
1449aab1ad11SSeven Lee 				NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN);
1450aab1ad11SSeven Lee 		}
1451aab1ad11SSeven Lee 		break;
1452aab1ad11SSeven Lee 	default:
1453aab1ad11SSeven Lee 		break;
1454aab1ad11SSeven Lee 	}
1455aab1ad11SSeven Lee 
1456aab1ad11SSeven Lee 	return 0;
1457aab1ad11SSeven Lee }
1458aab1ad11SSeven Lee 
1459aab1ad11SSeven Lee static int __maybe_unused nau8821_suspend(struct snd_soc_component *component)
1460aab1ad11SSeven Lee {
1461aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
1462aab1ad11SSeven Lee 
1463aab1ad11SSeven Lee 	if (nau8821->irq)
1464aab1ad11SSeven Lee 		disable_irq(nau8821->irq);
1465aab1ad11SSeven Lee 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
1466aab1ad11SSeven Lee 	/* Power down codec power; don't support button wakeup */
1467aab1ad11SSeven Lee 	snd_soc_component_disable_pin(component, "MICBIAS");
1468aab1ad11SSeven Lee 	snd_soc_dapm_sync(nau8821->dapm);
1469aab1ad11SSeven Lee 	regcache_cache_only(nau8821->regmap, true);
1470aab1ad11SSeven Lee 	regcache_mark_dirty(nau8821->regmap);
1471aab1ad11SSeven Lee 
1472aab1ad11SSeven Lee 	return 0;
1473aab1ad11SSeven Lee }
1474aab1ad11SSeven Lee 
1475aab1ad11SSeven Lee static int __maybe_unused nau8821_resume(struct snd_soc_component *component)
1476aab1ad11SSeven Lee {
1477aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
1478aab1ad11SSeven Lee 
1479aab1ad11SSeven Lee 	regcache_cache_only(nau8821->regmap, false);
1480aab1ad11SSeven Lee 	regcache_sync(nau8821->regmap);
1481aab1ad11SSeven Lee 	if (nau8821->irq)
1482aab1ad11SSeven Lee 		enable_irq(nau8821->irq);
1483aab1ad11SSeven Lee 
1484aab1ad11SSeven Lee 	return 0;
1485aab1ad11SSeven Lee }
1486aab1ad11SSeven Lee 
1487aab1ad11SSeven Lee static const struct snd_soc_component_driver nau8821_component_driver = {
1488aab1ad11SSeven Lee 	.probe			= nau8821_component_probe,
1489aab1ad11SSeven Lee 	.set_sysclk		= nau8821_set_sysclk,
1490aab1ad11SSeven Lee 	.set_pll		= nau8821_set_fll,
1491aab1ad11SSeven Lee 	.set_bias_level		= nau8821_set_bias_level,
1492aab1ad11SSeven Lee 	.suspend		= nau8821_suspend,
1493aab1ad11SSeven Lee 	.resume			= nau8821_resume,
1494aab1ad11SSeven Lee 	.controls		= nau8821_controls,
1495aab1ad11SSeven Lee 	.num_controls		= ARRAY_SIZE(nau8821_controls),
1496aab1ad11SSeven Lee 	.dapm_widgets		= nau8821_dapm_widgets,
1497aab1ad11SSeven Lee 	.num_dapm_widgets	= ARRAY_SIZE(nau8821_dapm_widgets),
1498aab1ad11SSeven Lee 	.dapm_routes		= nau8821_dapm_routes,
1499aab1ad11SSeven Lee 	.num_dapm_routes	= ARRAY_SIZE(nau8821_dapm_routes),
1500aab1ad11SSeven Lee 	.suspend_bias_off	= 1,
1501aab1ad11SSeven Lee 	.idle_bias_on		= 1,
1502aab1ad11SSeven Lee 	.use_pmdown_time	= 1,
1503aab1ad11SSeven Lee 	.endianness		= 1,
1504aab1ad11SSeven Lee };
1505aab1ad11SSeven Lee 
1506aab1ad11SSeven Lee /**
1507aab1ad11SSeven Lee  * nau8821_enable_jack_detect - Specify a jack for event reporting
1508aab1ad11SSeven Lee  *
1509aab1ad11SSeven Lee  * @component:  component to register the jack with
1510aab1ad11SSeven Lee  * @jack: jack to use to report headset and button events on
1511aab1ad11SSeven Lee  *
1512aab1ad11SSeven Lee  * After this function has been called the headset insert/remove and button
1513aab1ad11SSeven Lee  * events will be routed to the given jack.  Jack can be null to stop
1514aab1ad11SSeven Lee  * reporting.
1515aab1ad11SSeven Lee  */
1516aab1ad11SSeven Lee int nau8821_enable_jack_detect(struct snd_soc_component *component,
1517aab1ad11SSeven Lee 	struct snd_soc_jack *jack)
1518aab1ad11SSeven Lee {
1519aab1ad11SSeven Lee 	struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component);
1520aab1ad11SSeven Lee 	int ret;
1521aab1ad11SSeven Lee 
1522aab1ad11SSeven Lee 	nau8821->jack = jack;
1523aab1ad11SSeven Lee 	/* Initiate jack detection work queue */
1524aab1ad11SSeven Lee 	INIT_WORK(&nau8821->jdet_work, nau8821_jdet_work);
1525aab1ad11SSeven Lee 	ret = devm_request_threaded_irq(nau8821->dev, nau8821->irq, NULL,
1526aab1ad11SSeven Lee 		nau8821_interrupt, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1527aab1ad11SSeven Lee 		"nau8821", nau8821);
1528aab1ad11SSeven Lee 	if (ret) {
1529aab1ad11SSeven Lee 		dev_err(nau8821->dev, "Cannot request irq %d (%d)\n",
1530aab1ad11SSeven Lee 			nau8821->irq, ret);
1531aab1ad11SSeven Lee 		return ret;
1532aab1ad11SSeven Lee 	}
1533aab1ad11SSeven Lee 
1534aab1ad11SSeven Lee 	return ret;
1535aab1ad11SSeven Lee }
1536aab1ad11SSeven Lee EXPORT_SYMBOL_GPL(nau8821_enable_jack_detect);
1537aab1ad11SSeven Lee 
1538aab1ad11SSeven Lee static void nau8821_reset_chip(struct regmap *regmap)
1539aab1ad11SSeven Lee {
1540aab1ad11SSeven Lee 	regmap_write(regmap, NAU8821_R00_RESET, 0xffff);
1541aab1ad11SSeven Lee 	regmap_write(regmap, NAU8821_R00_RESET, 0xffff);
1542aab1ad11SSeven Lee }
1543aab1ad11SSeven Lee 
1544aab1ad11SSeven Lee static void nau8821_print_device_properties(struct nau8821 *nau8821)
1545aab1ad11SSeven Lee {
1546aab1ad11SSeven Lee 	struct device *dev = nau8821->dev;
1547aab1ad11SSeven Lee 
1548aab1ad11SSeven Lee 	dev_dbg(dev, "jkdet-enable:         %d\n", nau8821->jkdet_enable);
1549aab1ad11SSeven Lee 	dev_dbg(dev, "jkdet-pull-enable:    %d\n", nau8821->jkdet_pull_enable);
1550aab1ad11SSeven Lee 	dev_dbg(dev, "jkdet-pull-up:        %d\n", nau8821->jkdet_pull_up);
1551aab1ad11SSeven Lee 	dev_dbg(dev, "jkdet-polarity:       %d\n", nau8821->jkdet_polarity);
1552aab1ad11SSeven Lee 	dev_dbg(dev, "micbias-voltage:      %d\n", nau8821->micbias_voltage);
1553aab1ad11SSeven Lee 	dev_dbg(dev, "vref-impedance:       %d\n", nau8821->vref_impedance);
1554aab1ad11SSeven Lee 	dev_dbg(dev, "jack-insert-debounce: %d\n",
1555aab1ad11SSeven Lee 		nau8821->jack_insert_debounce);
1556aab1ad11SSeven Lee 	dev_dbg(dev, "jack-eject-debounce:  %d\n",
1557aab1ad11SSeven Lee 		nau8821->jack_eject_debounce);
1558aab1ad11SSeven Lee 	dev_dbg(dev, "dmic-clk-threshold:       %d\n",
1559aab1ad11SSeven Lee 		nau8821->dmic_clk_threshold);
15602551b6e8SSeven Lee 	dev_dbg(dev, "key_enable:       %d\n", nau8821->key_enable);
1561aab1ad11SSeven Lee }
1562aab1ad11SSeven Lee 
1563aab1ad11SSeven Lee static int nau8821_read_device_properties(struct device *dev,
1564aab1ad11SSeven Lee 	struct nau8821 *nau8821)
1565aab1ad11SSeven Lee {
1566aab1ad11SSeven Lee 	int ret;
1567aab1ad11SSeven Lee 
1568aab1ad11SSeven Lee 	nau8821->jkdet_enable = device_property_read_bool(dev,
1569aab1ad11SSeven Lee 		"nuvoton,jkdet-enable");
1570aab1ad11SSeven Lee 	nau8821->jkdet_pull_enable = device_property_read_bool(dev,
1571aab1ad11SSeven Lee 		"nuvoton,jkdet-pull-enable");
1572aab1ad11SSeven Lee 	nau8821->jkdet_pull_up = device_property_read_bool(dev,
1573aab1ad11SSeven Lee 		"nuvoton,jkdet-pull-up");
15742551b6e8SSeven Lee 	nau8821->key_enable = device_property_read_bool(dev,
15752551b6e8SSeven Lee 		"nuvoton,key-enable");
1576aab1ad11SSeven Lee 	ret = device_property_read_u32(dev, "nuvoton,jkdet-polarity",
1577aab1ad11SSeven Lee 		&nau8821->jkdet_polarity);
1578aab1ad11SSeven Lee 	if (ret)
1579aab1ad11SSeven Lee 		nau8821->jkdet_polarity = 1;
1580aab1ad11SSeven Lee 	ret = device_property_read_u32(dev, "nuvoton,micbias-voltage",
1581aab1ad11SSeven Lee 		&nau8821->micbias_voltage);
1582aab1ad11SSeven Lee 	if (ret)
1583aab1ad11SSeven Lee 		nau8821->micbias_voltage = 6;
1584aab1ad11SSeven Lee 	ret = device_property_read_u32(dev, "nuvoton,vref-impedance",
1585aab1ad11SSeven Lee 		&nau8821->vref_impedance);
1586aab1ad11SSeven Lee 	if (ret)
1587aab1ad11SSeven Lee 		nau8821->vref_impedance = 2;
1588aab1ad11SSeven Lee 	ret = device_property_read_u32(dev, "nuvoton,jack-insert-debounce",
1589aab1ad11SSeven Lee 		&nau8821->jack_insert_debounce);
1590aab1ad11SSeven Lee 	if (ret)
1591aab1ad11SSeven Lee 		nau8821->jack_insert_debounce = 7;
1592aab1ad11SSeven Lee 	ret = device_property_read_u32(dev, "nuvoton,jack-eject-debounce",
1593aab1ad11SSeven Lee 		&nau8821->jack_eject_debounce);
1594aab1ad11SSeven Lee 	if (ret)
1595aab1ad11SSeven Lee 		nau8821->jack_eject_debounce = 0;
1596aab1ad11SSeven Lee 	ret = device_property_read_u32(dev, "nuvoton,dmic-clk-threshold",
1597aab1ad11SSeven Lee 		&nau8821->dmic_clk_threshold);
1598aab1ad11SSeven Lee 	if (ret)
1599aab1ad11SSeven Lee 		nau8821->dmic_clk_threshold = 3072000;
1600aab1ad11SSeven Lee 
1601aab1ad11SSeven Lee 	return 0;
1602aab1ad11SSeven Lee }
1603aab1ad11SSeven Lee 
1604aab1ad11SSeven Lee static void nau8821_init_regs(struct nau8821 *nau8821)
1605aab1ad11SSeven Lee {
1606aab1ad11SSeven Lee 	struct regmap *regmap = nau8821->regmap;
1607aab1ad11SSeven Lee 
1608aab1ad11SSeven Lee 	/* Enable Bias/Vmid */
1609aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R66_BIAS_ADJ,
1610aab1ad11SSeven Lee 		NAU8821_BIAS_VMID, NAU8821_BIAS_VMID);
1611aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R76_BOOST,
1612aab1ad11SSeven Lee 		NAU8821_GLOBAL_BIAS_EN, NAU8821_GLOBAL_BIAS_EN);
1613aab1ad11SSeven Lee 	/* VMID Tieoff setting and enable TESTDAC.
1614aab1ad11SSeven Lee 	 * This sets the analog DAC inputs to a '0' input signal to avoid
1615aab1ad11SSeven Lee 	 * any glitches due to power up transients in both the analog and
1616aab1ad11SSeven Lee 	 * digital DAC circuit.
1617aab1ad11SSeven Lee 	 */
1618aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R66_BIAS_ADJ,
1619aab1ad11SSeven Lee 		NAU8821_BIAS_VMID_SEL_MASK | NAU8821_BIAS_TESTDAC_EN,
1620aab1ad11SSeven Lee 		(nau8821->vref_impedance << NAU8821_BIAS_VMID_SEL_SFT) |
1621aab1ad11SSeven Lee 		NAU8821_BIAS_TESTDAC_EN);
1622aab1ad11SSeven Lee 	/* Disable short Frame Sync detection logic */
1623aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R1E_LEFT_TIME_SLOT,
1624aab1ad11SSeven Lee 		NAU8821_DIS_FS_SHORT_DET, NAU8821_DIS_FS_SHORT_DET);
1625aab1ad11SSeven Lee 	/* Disable Boost Driver, Automatic Short circuit protection enable */
1626aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R76_BOOST,
1627aab1ad11SSeven Lee 		NAU8821_PRECHARGE_DIS | NAU8821_HP_BOOST_DIS |
1628aab1ad11SSeven Lee 		NAU8821_HP_BOOST_G_DIS | NAU8821_SHORT_SHUTDOWN_EN,
1629aab1ad11SSeven Lee 		NAU8821_PRECHARGE_DIS | NAU8821_HP_BOOST_DIS |
1630aab1ad11SSeven Lee 		NAU8821_HP_BOOST_G_DIS | NAU8821_SHORT_SHUTDOWN_EN);
1631aab1ad11SSeven Lee 	/* Class G timer 64ms */
1632aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R4B_CLASSG_CTRL,
1633aab1ad11SSeven Lee 		NAU8821_CLASSG_TIMER_MASK,
1634aab1ad11SSeven Lee 		0x20 << NAU8821_CLASSG_TIMER_SFT);
1635aab1ad11SSeven Lee 	/* Class AB bias current to 2x, DAC Capacitor enable MSB/LSB */
1636aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R6A_ANALOG_CONTROL_2,
1637aab1ad11SSeven Lee 		NAU8821_HP_NON_CLASSG_CURRENT_2xADJ |
1638aab1ad11SSeven Lee 		NAU8821_DAC_CAPACITOR_MSB | NAU8821_DAC_CAPACITOR_LSB,
1639aab1ad11SSeven Lee 		NAU8821_HP_NON_CLASSG_CURRENT_2xADJ |
1640aab1ad11SSeven Lee 		NAU8821_DAC_CAPACITOR_MSB | NAU8821_DAC_CAPACITOR_LSB);
1641aab1ad11SSeven Lee 	/* Disable DACR/L power */
1642aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R80_CHARGE_PUMP,
1643aab1ad11SSeven Lee 		NAU8821_POWER_DOWN_DACR | NAU8821_POWER_DOWN_DACL, 0);
1644aab1ad11SSeven Lee 	/* DAC clock delay 2ns, VREF */
1645aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R73_RDAC,
1646aab1ad11SSeven Lee 		NAU8821_DAC_CLK_DELAY_MASK | NAU8821_DAC_VREF_MASK,
1647aab1ad11SSeven Lee 		(0x2 << NAU8821_DAC_CLK_DELAY_SFT) |
1648aab1ad11SSeven Lee 		(0x3 << NAU8821_DAC_VREF_SFT));
1649aab1ad11SSeven Lee 
1650aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS,
1651aab1ad11SSeven Lee 		NAU8821_MICBIAS_VOLTAGE_MASK, nau8821->micbias_voltage);
1652aab1ad11SSeven Lee 	/* Default oversampling/decimations settings are unusable
1653aab1ad11SSeven Lee 	 * (audible hiss). Set it to something better.
1654aab1ad11SSeven Lee 	 */
1655aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R2B_ADC_RATE,
1656aab1ad11SSeven Lee 		NAU8821_ADC_SYNC_DOWN_MASK, NAU8821_ADC_SYNC_DOWN_64);
1657aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R2C_DAC_CTRL1,
1658aab1ad11SSeven Lee 		NAU8821_DAC_OVERSAMPLE_MASK, NAU8821_DAC_OVERSAMPLE_64);
1659aab1ad11SSeven Lee }
1660aab1ad11SSeven Lee 
1661aab1ad11SSeven Lee static int nau8821_setup_irq(struct nau8821 *nau8821)
1662aab1ad11SSeven Lee {
1663aab1ad11SSeven Lee 	struct regmap *regmap = nau8821->regmap;
1664aab1ad11SSeven Lee 
1665aab1ad11SSeven Lee 	/* Jack detection */
1666aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R1A_GPIO12_CTRL,
1667aab1ad11SSeven Lee 		NAU8821_JKDET_OUTPUT_EN,
1668aab1ad11SSeven Lee 		nau8821->jkdet_enable ? 0 : NAU8821_JKDET_OUTPUT_EN);
1669aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R1A_GPIO12_CTRL,
1670aab1ad11SSeven Lee 		NAU8821_JKDET_PULL_EN,
1671aab1ad11SSeven Lee 		nau8821->jkdet_pull_enable ? 0 : NAU8821_JKDET_PULL_EN);
1672aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R1A_GPIO12_CTRL,
1673aab1ad11SSeven Lee 		NAU8821_JKDET_PULL_UP,
1674aab1ad11SSeven Lee 		nau8821->jkdet_pull_up ? NAU8821_JKDET_PULL_UP : 0);
1675aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
1676aab1ad11SSeven Lee 		NAU8821_JACK_POLARITY,
1677aab1ad11SSeven Lee 		/* jkdet_polarity - 1  is for active-low */
1678aab1ad11SSeven Lee 		nau8821->jkdet_polarity ? 0 : NAU8821_JACK_POLARITY);
1679aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
1680aab1ad11SSeven Lee 		NAU8821_JACK_INSERT_DEBOUNCE_MASK,
1681aab1ad11SSeven Lee 		nau8821->jack_insert_debounce <<
1682aab1ad11SSeven Lee 		NAU8821_JACK_INSERT_DEBOUNCE_SFT);
1683aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL,
1684aab1ad11SSeven Lee 		NAU8821_JACK_EJECT_DEBOUNCE_MASK,
1685aab1ad11SSeven Lee 		nau8821->jack_eject_debounce <<
1686aab1ad11SSeven Lee 		NAU8821_JACK_EJECT_DEBOUNCE_SFT);
1687aab1ad11SSeven Lee 	/* Pull up IRQ pin */
1688aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK,
1689aab1ad11SSeven Lee 		NAU8821_IRQ_PIN_PULL_UP | NAU8821_IRQ_PIN_PULL_EN |
1690aab1ad11SSeven Lee 		NAU8821_IRQ_OUTPUT_EN, NAU8821_IRQ_PIN_PULL_UP |
1691aab1ad11SSeven Lee 		NAU8821_IRQ_PIN_PULL_EN | NAU8821_IRQ_OUTPUT_EN);
1692aab1ad11SSeven Lee 	/* Disable interruption before codec initiation done */
1693aab1ad11SSeven Lee 	/* Mask unneeded IRQs: 1 - disable, 0 - enable */
1694aab1ad11SSeven Lee 	regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK, 0x3f5, 0x3f5);
1695aab1ad11SSeven Lee 
1696aab1ad11SSeven Lee 	return 0;
1697aab1ad11SSeven Lee }
1698aab1ad11SSeven Lee 
16997325ed4dSStephen Kitt static int nau8821_i2c_probe(struct i2c_client *i2c)
1700aab1ad11SSeven Lee {
1701aab1ad11SSeven Lee 	struct device *dev = &i2c->dev;
1702aab1ad11SSeven Lee 	struct nau8821 *nau8821 = dev_get_platdata(&i2c->dev);
1703aab1ad11SSeven Lee 	int ret, value;
1704aab1ad11SSeven Lee 
1705aab1ad11SSeven Lee 	if (!nau8821) {
1706aab1ad11SSeven Lee 		nau8821 = devm_kzalloc(dev, sizeof(*nau8821), GFP_KERNEL);
1707aab1ad11SSeven Lee 		if (!nau8821)
1708aab1ad11SSeven Lee 			return -ENOMEM;
1709aab1ad11SSeven Lee 		nau8821_read_device_properties(dev, nau8821);
1710aab1ad11SSeven Lee 	}
1711aab1ad11SSeven Lee 	i2c_set_clientdata(i2c, nau8821);
1712aab1ad11SSeven Lee 
1713aab1ad11SSeven Lee 	nau8821->regmap = devm_regmap_init_i2c(i2c, &nau8821_regmap_config);
1714aab1ad11SSeven Lee 	if (IS_ERR(nau8821->regmap))
1715aab1ad11SSeven Lee 		return PTR_ERR(nau8821->regmap);
1716aab1ad11SSeven Lee 
1717aab1ad11SSeven Lee 	nau8821->dev = dev;
1718aab1ad11SSeven Lee 	nau8821->irq = i2c->irq;
1719aab1ad11SSeven Lee 	nau8821_print_device_properties(nau8821);
1720aab1ad11SSeven Lee 
1721aab1ad11SSeven Lee 	nau8821_reset_chip(nau8821->regmap);
1722aab1ad11SSeven Lee 	ret = regmap_read(nau8821->regmap, NAU8821_R58_I2C_DEVICE_ID, &value);
1723aab1ad11SSeven Lee 	if (ret) {
1724aab1ad11SSeven Lee 		dev_err(dev, "Failed to read device id (%d)\n", ret);
1725aab1ad11SSeven Lee 		return ret;
1726aab1ad11SSeven Lee 	}
1727aab1ad11SSeven Lee 	nau8821_init_regs(nau8821);
1728aab1ad11SSeven Lee 
1729aab1ad11SSeven Lee 	if (i2c->irq)
1730aab1ad11SSeven Lee 		nau8821_setup_irq(nau8821);
1731aab1ad11SSeven Lee 
1732aab1ad11SSeven Lee 	ret = devm_snd_soc_register_component(&i2c->dev,
1733aab1ad11SSeven Lee 		&nau8821_component_driver, &nau8821_dai, 1);
1734aab1ad11SSeven Lee 
1735aab1ad11SSeven Lee 	return ret;
1736aab1ad11SSeven Lee }
1737aab1ad11SSeven Lee 
1738aab1ad11SSeven Lee static const struct i2c_device_id nau8821_i2c_ids[] = {
1739aab1ad11SSeven Lee 	{ "nau8821", 0 },
1740aab1ad11SSeven Lee 	{ }
1741aab1ad11SSeven Lee };
1742aab1ad11SSeven Lee MODULE_DEVICE_TABLE(i2c, nau8821_i2c_ids);
1743aab1ad11SSeven Lee 
1744aab1ad11SSeven Lee #ifdef CONFIG_OF
1745aab1ad11SSeven Lee static const struct of_device_id nau8821_of_ids[] = {
1746aab1ad11SSeven Lee 	{ .compatible = "nuvoton,nau8821", },
1747aab1ad11SSeven Lee 	{}
1748aab1ad11SSeven Lee };
1749aab1ad11SSeven Lee MODULE_DEVICE_TABLE(of, nau8821_of_ids);
1750aab1ad11SSeven Lee #endif
1751aab1ad11SSeven Lee 
1752aab1ad11SSeven Lee #ifdef CONFIG_ACPI
1753aab1ad11SSeven Lee static const struct acpi_device_id nau8821_acpi_match[] = {
1754aab1ad11SSeven Lee 	{ "NVTN2020", 0 },
1755aab1ad11SSeven Lee 	{},
1756aab1ad11SSeven Lee };
1757aab1ad11SSeven Lee MODULE_DEVICE_TABLE(acpi, nau8821_acpi_match);
1758aab1ad11SSeven Lee #endif
1759aab1ad11SSeven Lee 
1760aab1ad11SSeven Lee static struct i2c_driver nau8821_driver = {
1761aab1ad11SSeven Lee 	.driver = {
1762aab1ad11SSeven Lee 		.name = "nau8821",
1763aab1ad11SSeven Lee 		.of_match_table = of_match_ptr(nau8821_of_ids),
1764aab1ad11SSeven Lee 		.acpi_match_table = ACPI_PTR(nau8821_acpi_match),
1765aab1ad11SSeven Lee 	},
17667325ed4dSStephen Kitt 	.probe_new = nau8821_i2c_probe,
1767aab1ad11SSeven Lee 	.id_table = nau8821_i2c_ids,
1768aab1ad11SSeven Lee };
1769aab1ad11SSeven Lee module_i2c_driver(nau8821_driver);
1770aab1ad11SSeven Lee 
1771aab1ad11SSeven Lee MODULE_DESCRIPTION("ASoC nau8821 driver");
1772aab1ad11SSeven Lee MODULE_AUTHOR("John Hsu <kchsu0@nuvoton.com>");
1773aab1ad11SSeven Lee MODULE_AUTHOR("Seven Lee <wtli@nuvoton.com>");
1774aab1ad11SSeven Lee MODULE_LICENSE("GPL");
1775