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