Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Nuvoton NAU8825 audio codec driver |
| 3 | * |
| 4 | * Copyright 2015 Google Chromium project. |
| 5 | * Author: Anatol Pomozov <anatol@chromium.org> |
| 6 | * Copyright 2015 Nuvoton Technology Corp. |
| 7 | * Co-author: Meng-Huang Kuo <mhkuo@nuvoton.com> |
| 8 | * |
| 9 | * Licensed under the GPL-2. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/regmap.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/clk.h> |
Fang, Yang A | b368130 | 2015-10-07 14:33:57 -0700 | [diff] [blame] | 19 | #include <linux/acpi.h> |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 20 | #include <linux/math64.h> |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 21 | |
| 22 | #include <sound/initval.h> |
| 23 | #include <sound/tlv.h> |
| 24 | #include <sound/core.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include <sound/pcm_params.h> |
| 27 | #include <sound/soc.h> |
| 28 | #include <sound/jack.h> |
| 29 | |
| 30 | |
| 31 | #include "nau8825.h" |
| 32 | |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 33 | #define NAU_FREF_MAX 13500000 |
John Hsu | 407c71b | 2016-03-15 12:09:36 +0800 | [diff] [blame] | 34 | #define NAU_FVCO_MAX 124000000 |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 35 | #define NAU_FVCO_MIN 90000000 |
| 36 | |
| 37 | struct nau8825_fll { |
| 38 | int mclk_src; |
| 39 | int ratio; |
| 40 | int fll_frac; |
| 41 | int fll_int; |
| 42 | int clk_ref_div; |
| 43 | }; |
| 44 | |
| 45 | struct nau8825_fll_attr { |
| 46 | unsigned int param; |
| 47 | unsigned int val; |
| 48 | }; |
| 49 | |
| 50 | /* scaling for mclk from sysclk_src output */ |
| 51 | static const struct nau8825_fll_attr mclk_src_scaling[] = { |
| 52 | { 1, 0x0 }, |
| 53 | { 2, 0x2 }, |
| 54 | { 4, 0x3 }, |
| 55 | { 8, 0x4 }, |
| 56 | { 16, 0x5 }, |
| 57 | { 32, 0x6 }, |
| 58 | { 3, 0x7 }, |
| 59 | { 6, 0xa }, |
| 60 | { 12, 0xb }, |
| 61 | { 24, 0xc }, |
| 62 | { 48, 0xd }, |
| 63 | { 96, 0xe }, |
| 64 | { 5, 0xf }, |
| 65 | }; |
| 66 | |
| 67 | /* ratio for input clk freq */ |
| 68 | static const struct nau8825_fll_attr fll_ratio[] = { |
| 69 | { 512000, 0x01 }, |
| 70 | { 256000, 0x02 }, |
| 71 | { 128000, 0x04 }, |
| 72 | { 64000, 0x08 }, |
| 73 | { 32000, 0x10 }, |
| 74 | { 8000, 0x20 }, |
| 75 | { 4000, 0x40 }, |
| 76 | }; |
| 77 | |
| 78 | static const struct nau8825_fll_attr fll_pre_scalar[] = { |
| 79 | { 1, 0x0 }, |
| 80 | { 2, 0x1 }, |
| 81 | { 4, 0x2 }, |
| 82 | { 8, 0x3 }, |
| 83 | }; |
| 84 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 85 | static const struct reg_default nau8825_reg_defaults[] = { |
| 86 | { NAU8825_REG_ENA_CTRL, 0x00ff }, |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 87 | { NAU8825_REG_IIC_ADDR_SET, 0x0 }, |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 88 | { NAU8825_REG_CLK_DIVIDER, 0x0050 }, |
| 89 | { NAU8825_REG_FLL1, 0x0 }, |
| 90 | { NAU8825_REG_FLL2, 0x3126 }, |
| 91 | { NAU8825_REG_FLL3, 0x0008 }, |
| 92 | { NAU8825_REG_FLL4, 0x0010 }, |
| 93 | { NAU8825_REG_FLL5, 0x0 }, |
| 94 | { NAU8825_REG_FLL6, 0x6000 }, |
| 95 | { NAU8825_REG_FLL_VCO_RSV, 0xf13c }, |
| 96 | { NAU8825_REG_HSD_CTRL, 0x000c }, |
| 97 | { NAU8825_REG_JACK_DET_CTRL, 0x0 }, |
| 98 | { NAU8825_REG_INTERRUPT_MASK, 0x0 }, |
| 99 | { NAU8825_REG_INTERRUPT_DIS_CTRL, 0xffff }, |
| 100 | { NAU8825_REG_SAR_CTRL, 0x0015 }, |
| 101 | { NAU8825_REG_KEYDET_CTRL, 0x0110 }, |
| 102 | { NAU8825_REG_VDET_THRESHOLD_1, 0x0 }, |
| 103 | { NAU8825_REG_VDET_THRESHOLD_2, 0x0 }, |
| 104 | { NAU8825_REG_VDET_THRESHOLD_3, 0x0 }, |
| 105 | { NAU8825_REG_VDET_THRESHOLD_4, 0x0 }, |
| 106 | { NAU8825_REG_GPIO34_CTRL, 0x0 }, |
| 107 | { NAU8825_REG_GPIO12_CTRL, 0x0 }, |
| 108 | { NAU8825_REG_TDM_CTRL, 0x0 }, |
| 109 | { NAU8825_REG_I2S_PCM_CTRL1, 0x000b }, |
| 110 | { NAU8825_REG_I2S_PCM_CTRL2, 0x8010 }, |
| 111 | { NAU8825_REG_LEFT_TIME_SLOT, 0x0 }, |
| 112 | { NAU8825_REG_RIGHT_TIME_SLOT, 0x0 }, |
| 113 | { NAU8825_REG_BIQ_CTRL, 0x0 }, |
| 114 | { NAU8825_REG_BIQ_COF1, 0x0 }, |
| 115 | { NAU8825_REG_BIQ_COF2, 0x0 }, |
| 116 | { NAU8825_REG_BIQ_COF3, 0x0 }, |
| 117 | { NAU8825_REG_BIQ_COF4, 0x0 }, |
| 118 | { NAU8825_REG_BIQ_COF5, 0x0 }, |
| 119 | { NAU8825_REG_BIQ_COF6, 0x0 }, |
| 120 | { NAU8825_REG_BIQ_COF7, 0x0 }, |
| 121 | { NAU8825_REG_BIQ_COF8, 0x0 }, |
| 122 | { NAU8825_REG_BIQ_COF9, 0x0 }, |
| 123 | { NAU8825_REG_BIQ_COF10, 0x0 }, |
| 124 | { NAU8825_REG_ADC_RATE, 0x0010 }, |
| 125 | { NAU8825_REG_DAC_CTRL1, 0x0001 }, |
| 126 | { NAU8825_REG_DAC_CTRL2, 0x0 }, |
| 127 | { NAU8825_REG_DAC_DGAIN_CTRL, 0x0 }, |
| 128 | { NAU8825_REG_ADC_DGAIN_CTRL, 0x00cf }, |
| 129 | { NAU8825_REG_MUTE_CTRL, 0x0 }, |
| 130 | { NAU8825_REG_HSVOL_CTRL, 0x0 }, |
| 131 | { NAU8825_REG_DACL_CTRL, 0x02cf }, |
| 132 | { NAU8825_REG_DACR_CTRL, 0x00cf }, |
| 133 | { NAU8825_REG_ADC_DRC_KNEE_IP12, 0x1486 }, |
| 134 | { NAU8825_REG_ADC_DRC_KNEE_IP34, 0x0f12 }, |
| 135 | { NAU8825_REG_ADC_DRC_SLOPES, 0x25ff }, |
| 136 | { NAU8825_REG_ADC_DRC_ATKDCY, 0x3457 }, |
| 137 | { NAU8825_REG_DAC_DRC_KNEE_IP12, 0x1486 }, |
| 138 | { NAU8825_REG_DAC_DRC_KNEE_IP34, 0x0f12 }, |
| 139 | { NAU8825_REG_DAC_DRC_SLOPES, 0x25f9 }, |
| 140 | { NAU8825_REG_DAC_DRC_ATKDCY, 0x3457 }, |
| 141 | { NAU8825_REG_IMM_MODE_CTRL, 0x0 }, |
| 142 | { NAU8825_REG_CLASSG_CTRL, 0x0 }, |
| 143 | { NAU8825_REG_OPT_EFUSE_CTRL, 0x0 }, |
| 144 | { NAU8825_REG_MISC_CTRL, 0x0 }, |
| 145 | { NAU8825_REG_BIAS_ADJ, 0x0 }, |
| 146 | { NAU8825_REG_TRIM_SETTINGS, 0x0 }, |
| 147 | { NAU8825_REG_ANALOG_CONTROL_1, 0x0 }, |
| 148 | { NAU8825_REG_ANALOG_CONTROL_2, 0x0 }, |
| 149 | { NAU8825_REG_ANALOG_ADC_1, 0x0011 }, |
| 150 | { NAU8825_REG_ANALOG_ADC_2, 0x0020 }, |
| 151 | { NAU8825_REG_RDAC, 0x0008 }, |
| 152 | { NAU8825_REG_MIC_BIAS, 0x0006 }, |
| 153 | { NAU8825_REG_BOOST, 0x0 }, |
| 154 | { NAU8825_REG_FEPGA, 0x0 }, |
| 155 | { NAU8825_REG_POWER_UP_CONTROL, 0x0 }, |
| 156 | { NAU8825_REG_CHARGE_PUMP, 0x0 }, |
| 157 | }; |
| 158 | |
| 159 | static bool nau8825_readable_reg(struct device *dev, unsigned int reg) |
| 160 | { |
| 161 | switch (reg) { |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 162 | case NAU8825_REG_ENA_CTRL ... NAU8825_REG_FLL_VCO_RSV: |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 163 | case NAU8825_REG_HSD_CTRL ... NAU8825_REG_JACK_DET_CTRL: |
| 164 | case NAU8825_REG_INTERRUPT_MASK ... NAU8825_REG_KEYDET_CTRL: |
| 165 | case NAU8825_REG_VDET_THRESHOLD_1 ... NAU8825_REG_DACR_CTRL: |
| 166 | case NAU8825_REG_ADC_DRC_KNEE_IP12 ... NAU8825_REG_ADC_DRC_ATKDCY: |
| 167 | case NAU8825_REG_DAC_DRC_KNEE_IP12 ... NAU8825_REG_DAC_DRC_ATKDCY: |
| 168 | case NAU8825_REG_IMM_MODE_CTRL ... NAU8825_REG_IMM_RMS_R: |
| 169 | case NAU8825_REG_CLASSG_CTRL ... NAU8825_REG_OPT_EFUSE_CTRL: |
| 170 | case NAU8825_REG_MISC_CTRL: |
| 171 | case NAU8825_REG_I2C_DEVICE_ID ... NAU8825_REG_SARDOUT_RAM_STATUS: |
| 172 | case NAU8825_REG_BIAS_ADJ: |
| 173 | case NAU8825_REG_TRIM_SETTINGS ... NAU8825_REG_ANALOG_CONTROL_2: |
| 174 | case NAU8825_REG_ANALOG_ADC_1 ... NAU8825_REG_MIC_BIAS: |
| 175 | case NAU8825_REG_BOOST ... NAU8825_REG_FEPGA: |
| 176 | case NAU8825_REG_POWER_UP_CONTROL ... NAU8825_REG_GENERAL_STATUS: |
| 177 | return true; |
| 178 | default: |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | } |
| 183 | |
| 184 | static bool nau8825_writeable_reg(struct device *dev, unsigned int reg) |
| 185 | { |
| 186 | switch (reg) { |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 187 | case NAU8825_REG_RESET ... NAU8825_REG_FLL_VCO_RSV: |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 188 | case NAU8825_REG_HSD_CTRL ... NAU8825_REG_JACK_DET_CTRL: |
| 189 | case NAU8825_REG_INTERRUPT_MASK: |
| 190 | case NAU8825_REG_INT_CLR_KEY_STATUS ... NAU8825_REG_KEYDET_CTRL: |
| 191 | case NAU8825_REG_VDET_THRESHOLD_1 ... NAU8825_REG_DACR_CTRL: |
| 192 | case NAU8825_REG_ADC_DRC_KNEE_IP12 ... NAU8825_REG_ADC_DRC_ATKDCY: |
| 193 | case NAU8825_REG_DAC_DRC_KNEE_IP12 ... NAU8825_REG_DAC_DRC_ATKDCY: |
| 194 | case NAU8825_REG_IMM_MODE_CTRL: |
| 195 | case NAU8825_REG_CLASSG_CTRL ... NAU8825_REG_OPT_EFUSE_CTRL: |
| 196 | case NAU8825_REG_MISC_CTRL: |
| 197 | case NAU8825_REG_BIAS_ADJ: |
| 198 | case NAU8825_REG_TRIM_SETTINGS ... NAU8825_REG_ANALOG_CONTROL_2: |
| 199 | case NAU8825_REG_ANALOG_ADC_1 ... NAU8825_REG_MIC_BIAS: |
| 200 | case NAU8825_REG_BOOST ... NAU8825_REG_FEPGA: |
| 201 | case NAU8825_REG_POWER_UP_CONTROL ... NAU8825_REG_CHARGE_PUMP: |
| 202 | return true; |
| 203 | default: |
| 204 | return false; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static bool nau8825_volatile_reg(struct device *dev, unsigned int reg) |
| 209 | { |
| 210 | switch (reg) { |
| 211 | case NAU8825_REG_RESET: |
| 212 | case NAU8825_REG_IRQ_STATUS: |
| 213 | case NAU8825_REG_INT_CLR_KEY_STATUS: |
| 214 | case NAU8825_REG_IMM_RMS_L: |
| 215 | case NAU8825_REG_IMM_RMS_R: |
| 216 | case NAU8825_REG_I2C_DEVICE_ID: |
| 217 | case NAU8825_REG_SARDOUT_RAM_STATUS: |
| 218 | case NAU8825_REG_CHARGE_PUMP_INPUT_READ: |
| 219 | case NAU8825_REG_GENERAL_STATUS: |
John Hsu | 18d8306 | 2016-05-31 11:57:41 +0800 | [diff] [blame^] | 220 | case NAU8825_REG_BIQ_CTRL ... NAU8825_REG_BIQ_COF10: |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 221 | return true; |
| 222 | default: |
| 223 | return false; |
| 224 | } |
| 225 | } |
| 226 | |
John Hsu | eeef16a | 2016-03-22 11:57:20 +0800 | [diff] [blame] | 227 | static int nau8825_adc_event(struct snd_soc_dapm_widget *w, |
| 228 | struct snd_kcontrol *kcontrol, int event) |
| 229 | { |
| 230 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm); |
| 231 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 232 | |
| 233 | switch (event) { |
| 234 | case SND_SOC_DAPM_POST_PMU: |
| 235 | regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL, |
| 236 | NAU8825_ENABLE_ADC, NAU8825_ENABLE_ADC); |
| 237 | break; |
| 238 | case SND_SOC_DAPM_POST_PMD: |
| 239 | if (!nau8825->irq) |
| 240 | regmap_update_bits(nau8825->regmap, |
| 241 | NAU8825_REG_ENA_CTRL, NAU8825_ENABLE_ADC, 0); |
| 242 | break; |
| 243 | default: |
| 244 | return -EINVAL; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 250 | static int nau8825_pump_event(struct snd_soc_dapm_widget *w, |
| 251 | struct snd_kcontrol *kcontrol, int event) |
| 252 | { |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 253 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm); |
| 254 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 255 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 256 | switch (event) { |
| 257 | case SND_SOC_DAPM_POST_PMU: |
| 258 | /* Prevent startup click by letting charge pump to ramp up */ |
| 259 | msleep(10); |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 260 | regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP, |
| 261 | NAU8825_JAMNODCLOW, NAU8825_JAMNODCLOW); |
| 262 | break; |
| 263 | case SND_SOC_DAPM_PRE_PMD: |
| 264 | regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP, |
| 265 | NAU8825_JAMNODCLOW, 0); |
| 266 | break; |
| 267 | default: |
| 268 | return -EINVAL; |
| 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static int nau8825_output_dac_event(struct snd_soc_dapm_widget *w, |
| 275 | struct snd_kcontrol *kcontrol, int event) |
| 276 | { |
| 277 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm); |
| 278 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 279 | |
| 280 | switch (event) { |
| 281 | case SND_SOC_DAPM_PRE_PMU: |
| 282 | /* Disables the TESTDAC to let DAC signal pass through. */ |
| 283 | regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ, |
| 284 | NAU8825_BIAS_TESTDAC_EN, 0); |
| 285 | break; |
| 286 | case SND_SOC_DAPM_POST_PMD: |
| 287 | regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ, |
| 288 | NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN); |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 289 | break; |
| 290 | default: |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
John Hsu | 18d8306 | 2016-05-31 11:57:41 +0800 | [diff] [blame^] | 297 | static int nau8825_biq_coeff_get(struct snd_kcontrol *kcontrol, |
| 298 | struct snd_ctl_elem_value *ucontrol) |
| 299 | { |
| 300 | struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 301 | struct soc_bytes_ext *params = (void *)kcontrol->private_value; |
| 302 | |
| 303 | if (!component->regmap) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | regmap_raw_read(component->regmap, NAU8825_REG_BIQ_COF1, |
| 307 | ucontrol->value.bytes.data, params->max); |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static int nau8825_biq_coeff_put(struct snd_kcontrol *kcontrol, |
| 312 | struct snd_ctl_elem_value *ucontrol) |
| 313 | { |
| 314 | struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 315 | struct soc_bytes_ext *params = (void *)kcontrol->private_value; |
| 316 | void *data; |
| 317 | |
| 318 | if (!component->regmap) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | data = kmemdup(ucontrol->value.bytes.data, |
| 322 | params->max, GFP_KERNEL | GFP_DMA); |
| 323 | if (!data) |
| 324 | return -ENOMEM; |
| 325 | |
| 326 | regmap_update_bits(component->regmap, NAU8825_REG_BIQ_CTRL, |
| 327 | NAU8825_BIQ_WRT_EN, 0); |
| 328 | regmap_raw_write(component->regmap, NAU8825_REG_BIQ_COF1, |
| 329 | data, params->max); |
| 330 | regmap_update_bits(component->regmap, NAU8825_REG_BIQ_CTRL, |
| 331 | NAU8825_BIQ_WRT_EN, NAU8825_BIQ_WRT_EN); |
| 332 | |
| 333 | kfree(data); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static const char * const nau8825_biq_path[] = { |
| 338 | "ADC", "DAC" |
| 339 | }; |
| 340 | |
| 341 | static const struct soc_enum nau8825_biq_path_enum = |
| 342 | SOC_ENUM_SINGLE(NAU8825_REG_BIQ_CTRL, NAU8825_BIQ_PATH_SFT, |
| 343 | ARRAY_SIZE(nau8825_biq_path), nau8825_biq_path); |
| 344 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 345 | static const char * const nau8825_adc_decimation[] = { |
| 346 | "32", "64", "128", "256" |
| 347 | }; |
| 348 | |
| 349 | static const struct soc_enum nau8825_adc_decimation_enum = |
| 350 | SOC_ENUM_SINGLE(NAU8825_REG_ADC_RATE, NAU8825_ADC_SYNC_DOWN_SFT, |
| 351 | ARRAY_SIZE(nau8825_adc_decimation), nau8825_adc_decimation); |
| 352 | |
| 353 | static const char * const nau8825_dac_oversampl[] = { |
| 354 | "64", "256", "128", "", "32" |
| 355 | }; |
| 356 | |
| 357 | static const struct soc_enum nau8825_dac_oversampl_enum = |
| 358 | SOC_ENUM_SINGLE(NAU8825_REG_DAC_CTRL1, NAU8825_DAC_OVERSAMPLE_SFT, |
| 359 | ARRAY_SIZE(nau8825_dac_oversampl), nau8825_dac_oversampl); |
| 360 | |
| 361 | static const DECLARE_TLV_DB_MINMAX_MUTE(adc_vol_tlv, -10300, 2400); |
| 362 | static const DECLARE_TLV_DB_MINMAX_MUTE(sidetone_vol_tlv, -4200, 0); |
| 363 | static const DECLARE_TLV_DB_MINMAX(dac_vol_tlv, -5400, 0); |
| 364 | static const DECLARE_TLV_DB_MINMAX(fepga_gain_tlv, -100, 3600); |
| 365 | static const DECLARE_TLV_DB_MINMAX_MUTE(crosstalk_vol_tlv, -9600, 2400); |
| 366 | |
| 367 | static const struct snd_kcontrol_new nau8825_controls[] = { |
| 368 | SOC_SINGLE_TLV("Mic Volume", NAU8825_REG_ADC_DGAIN_CTRL, |
| 369 | 0, 0xff, 0, adc_vol_tlv), |
| 370 | SOC_DOUBLE_TLV("Headphone Bypass Volume", NAU8825_REG_ADC_DGAIN_CTRL, |
| 371 | 12, 8, 0x0f, 0, sidetone_vol_tlv), |
| 372 | SOC_DOUBLE_TLV("Headphone Volume", NAU8825_REG_HSVOL_CTRL, |
| 373 | 6, 0, 0x3f, 1, dac_vol_tlv), |
| 374 | SOC_SINGLE_TLV("Frontend PGA Volume", NAU8825_REG_POWER_UP_CONTROL, |
| 375 | 8, 37, 0, fepga_gain_tlv), |
| 376 | SOC_DOUBLE_TLV("Headphone Crosstalk Volume", NAU8825_REG_DAC_DGAIN_CTRL, |
| 377 | 0, 8, 0xff, 0, crosstalk_vol_tlv), |
| 378 | |
| 379 | SOC_ENUM("ADC Decimation Rate", nau8825_adc_decimation_enum), |
| 380 | SOC_ENUM("DAC Oversampling Rate", nau8825_dac_oversampl_enum), |
John Hsu | 18d8306 | 2016-05-31 11:57:41 +0800 | [diff] [blame^] | 381 | /* programmable biquad filter */ |
| 382 | SOC_ENUM("BIQ Path Select", nau8825_biq_path_enum), |
| 383 | SND_SOC_BYTES_EXT("BIQ Coefficeints", 20, |
| 384 | nau8825_biq_coeff_get, nau8825_biq_coeff_put), |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | /* DAC Mux 0x33[9] and 0x34[9] */ |
| 388 | static const char * const nau8825_dac_src[] = { |
| 389 | "DACL", "DACR", |
| 390 | }; |
| 391 | |
| 392 | static SOC_ENUM_SINGLE_DECL( |
| 393 | nau8825_dacl_enum, NAU8825_REG_DACL_CTRL, |
| 394 | NAU8825_DACL_CH_SEL_SFT, nau8825_dac_src); |
| 395 | |
| 396 | static SOC_ENUM_SINGLE_DECL( |
| 397 | nau8825_dacr_enum, NAU8825_REG_DACR_CTRL, |
| 398 | NAU8825_DACR_CH_SEL_SFT, nau8825_dac_src); |
| 399 | |
| 400 | static const struct snd_kcontrol_new nau8825_dacl_mux = |
| 401 | SOC_DAPM_ENUM("DACL Source", nau8825_dacl_enum); |
| 402 | |
| 403 | static const struct snd_kcontrol_new nau8825_dacr_mux = |
| 404 | SOC_DAPM_ENUM("DACR Source", nau8825_dacr_enum); |
| 405 | |
| 406 | |
| 407 | static const struct snd_soc_dapm_widget nau8825_dapm_widgets[] = { |
| 408 | SND_SOC_DAPM_AIF_OUT("AIFTX", "Capture", 0, NAU8825_REG_I2S_PCM_CTRL2, |
| 409 | 15, 1), |
| 410 | |
| 411 | SND_SOC_DAPM_INPUT("MIC"), |
| 412 | SND_SOC_DAPM_MICBIAS("MICBIAS", NAU8825_REG_MIC_BIAS, 8, 0), |
| 413 | |
| 414 | SND_SOC_DAPM_PGA("Frontend PGA", NAU8825_REG_POWER_UP_CONTROL, 14, 0, |
| 415 | NULL, 0), |
| 416 | |
John Hsu | eeef16a | 2016-03-22 11:57:20 +0800 | [diff] [blame] | 417 | SND_SOC_DAPM_ADC_E("ADC", NULL, SND_SOC_NOPM, 0, 0, |
| 418 | nau8825_adc_event, SND_SOC_DAPM_POST_PMU | |
| 419 | SND_SOC_DAPM_POST_PMD), |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 420 | SND_SOC_DAPM_SUPPLY("ADC Clock", NAU8825_REG_ENA_CTRL, 7, 0, NULL, 0), |
| 421 | SND_SOC_DAPM_SUPPLY("ADC Power", NAU8825_REG_ANALOG_ADC_2, 6, 0, NULL, |
| 422 | 0), |
| 423 | |
Ben Zhang | e6cee90 | 2016-03-25 16:10:39 -0700 | [diff] [blame] | 424 | /* ADC for button press detection. A dapm supply widget is used to |
| 425 | * prevent dapm_power_widgets keeping the codec at SND_SOC_BIAS_ON |
| 426 | * during suspend. |
| 427 | */ |
| 428 | SND_SOC_DAPM_SUPPLY("SAR", NAU8825_REG_SAR_CTRL, |
| 429 | NAU8825_SAR_ADC_EN_SFT, 0, NULL, 0), |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 430 | |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 431 | SND_SOC_DAPM_PGA_S("ADACL", 2, NAU8825_REG_RDAC, 12, 0, NULL, 0), |
| 432 | SND_SOC_DAPM_PGA_S("ADACR", 2, NAU8825_REG_RDAC, 13, 0, NULL, 0), |
| 433 | SND_SOC_DAPM_PGA_S("ADACL Clock", 3, NAU8825_REG_RDAC, 8, 0, NULL, 0), |
| 434 | SND_SOC_DAPM_PGA_S("ADACR Clock", 3, NAU8825_REG_RDAC, 9, 0, NULL, 0), |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 435 | |
| 436 | SND_SOC_DAPM_DAC("DDACR", NULL, NAU8825_REG_ENA_CTRL, |
| 437 | NAU8825_ENABLE_DACR_SFT, 0), |
| 438 | SND_SOC_DAPM_DAC("DDACL", NULL, NAU8825_REG_ENA_CTRL, |
| 439 | NAU8825_ENABLE_DACL_SFT, 0), |
| 440 | SND_SOC_DAPM_SUPPLY("DDAC Clock", NAU8825_REG_ENA_CTRL, 6, 0, NULL, 0), |
| 441 | |
| 442 | SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &nau8825_dacl_mux), |
| 443 | SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &nau8825_dacr_mux), |
| 444 | |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 445 | SND_SOC_DAPM_PGA_S("HP amp L", 0, |
| 446 | NAU8825_REG_CLASSG_CTRL, 1, 0, NULL, 0), |
| 447 | SND_SOC_DAPM_PGA_S("HP amp R", 0, |
| 448 | NAU8825_REG_CLASSG_CTRL, 2, 0, NULL, 0), |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 449 | |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 450 | SND_SOC_DAPM_PGA_S("Charge Pump", 1, NAU8825_REG_CHARGE_PUMP, 5, 0, |
| 451 | nau8825_pump_event, SND_SOC_DAPM_POST_PMU | |
| 452 | SND_SOC_DAPM_PRE_PMD), |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 453 | |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 454 | SND_SOC_DAPM_PGA_S("Output Driver R Stage 1", 4, |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 455 | NAU8825_REG_POWER_UP_CONTROL, 5, 0, NULL, 0), |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 456 | SND_SOC_DAPM_PGA_S("Output Driver L Stage 1", 4, |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 457 | NAU8825_REG_POWER_UP_CONTROL, 4, 0, NULL, 0), |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 458 | SND_SOC_DAPM_PGA_S("Output Driver R Stage 2", 5, |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 459 | NAU8825_REG_POWER_UP_CONTROL, 3, 0, NULL, 0), |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 460 | SND_SOC_DAPM_PGA_S("Output Driver L Stage 2", 5, |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 461 | NAU8825_REG_POWER_UP_CONTROL, 2, 0, NULL, 0), |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 462 | SND_SOC_DAPM_PGA_S("Output Driver R Stage 3", 6, |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 463 | NAU8825_REG_POWER_UP_CONTROL, 1, 0, NULL, 0), |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 464 | SND_SOC_DAPM_PGA_S("Output Driver L Stage 3", 6, |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 465 | NAU8825_REG_POWER_UP_CONTROL, 0, 0, NULL, 0), |
| 466 | |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 467 | SND_SOC_DAPM_PGA_S("Output DACL", 7, |
| 468 | NAU8825_REG_CHARGE_PUMP, 8, 1, nau8825_output_dac_event, |
| 469 | SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), |
| 470 | SND_SOC_DAPM_PGA_S("Output DACR", 7, |
| 471 | NAU8825_REG_CHARGE_PUMP, 9, 1, nau8825_output_dac_event, |
| 472 | SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), |
| 473 | |
| 474 | /* HPOL/R are ungrounded by disabling 16 Ohm pull-downs on playback */ |
| 475 | SND_SOC_DAPM_PGA_S("HPOL Pulldown", 8, |
| 476 | NAU8825_REG_HSD_CTRL, 0, 1, NULL, 0), |
| 477 | SND_SOC_DAPM_PGA_S("HPOR Pulldown", 8, |
| 478 | NAU8825_REG_HSD_CTRL, 1, 1, NULL, 0), |
| 479 | |
| 480 | /* High current HPOL/R boost driver */ |
| 481 | SND_SOC_DAPM_PGA_S("HP Boost Driver", 9, |
| 482 | NAU8825_REG_BOOST, 9, 1, NULL, 0), |
| 483 | |
| 484 | /* Class G operation control*/ |
| 485 | SND_SOC_DAPM_PGA_S("Class G", 10, |
| 486 | NAU8825_REG_CLASSG_CTRL, 0, 0, NULL, 0), |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 487 | |
| 488 | SND_SOC_DAPM_OUTPUT("HPOL"), |
| 489 | SND_SOC_DAPM_OUTPUT("HPOR"), |
| 490 | }; |
| 491 | |
| 492 | static const struct snd_soc_dapm_route nau8825_dapm_routes[] = { |
| 493 | {"Frontend PGA", NULL, "MIC"}, |
| 494 | {"ADC", NULL, "Frontend PGA"}, |
| 495 | {"ADC", NULL, "ADC Clock"}, |
| 496 | {"ADC", NULL, "ADC Power"}, |
| 497 | {"AIFTX", NULL, "ADC"}, |
| 498 | |
| 499 | {"DDACL", NULL, "Playback"}, |
| 500 | {"DDACR", NULL, "Playback"}, |
| 501 | {"DDACL", NULL, "DDAC Clock"}, |
| 502 | {"DDACR", NULL, "DDAC Clock"}, |
| 503 | {"DACL Mux", "DACL", "DDACL"}, |
| 504 | {"DACL Mux", "DACR", "DDACR"}, |
| 505 | {"DACR Mux", "DACL", "DDACL"}, |
| 506 | {"DACR Mux", "DACR", "DDACR"}, |
| 507 | {"HP amp L", NULL, "DACL Mux"}, |
| 508 | {"HP amp R", NULL, "DACR Mux"}, |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 509 | {"Charge Pump", NULL, "HP amp L"}, |
| 510 | {"Charge Pump", NULL, "HP amp R"}, |
| 511 | {"ADACL", NULL, "Charge Pump"}, |
| 512 | {"ADACR", NULL, "Charge Pump"}, |
| 513 | {"ADACL Clock", NULL, "ADACL"}, |
| 514 | {"ADACR Clock", NULL, "ADACR"}, |
| 515 | {"Output Driver L Stage 1", NULL, "ADACL Clock"}, |
| 516 | {"Output Driver R Stage 1", NULL, "ADACR Clock"}, |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 517 | {"Output Driver L Stage 2", NULL, "Output Driver L Stage 1"}, |
| 518 | {"Output Driver R Stage 2", NULL, "Output Driver R Stage 1"}, |
| 519 | {"Output Driver L Stage 3", NULL, "Output Driver L Stage 2"}, |
| 520 | {"Output Driver R Stage 3", NULL, "Output Driver R Stage 2"}, |
| 521 | {"Output DACL", NULL, "Output Driver L Stage 3"}, |
| 522 | {"Output DACR", NULL, "Output Driver R Stage 3"}, |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 523 | {"HPOL Pulldown", NULL, "Output DACL"}, |
| 524 | {"HPOR Pulldown", NULL, "Output DACR"}, |
| 525 | {"HP Boost Driver", NULL, "HPOL Pulldown"}, |
| 526 | {"HP Boost Driver", NULL, "HPOR Pulldown"}, |
| 527 | {"Class G", NULL, "HP Boost Driver"}, |
| 528 | {"HPOL", NULL, "Class G"}, |
| 529 | {"HPOR", NULL, "Class G"}, |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 530 | }; |
| 531 | |
| 532 | static int nau8825_hw_params(struct snd_pcm_substream *substream, |
| 533 | struct snd_pcm_hw_params *params, |
| 534 | struct snd_soc_dai *dai) |
| 535 | { |
| 536 | struct snd_soc_codec *codec = dai->codec; |
| 537 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 538 | unsigned int val_len = 0; |
| 539 | |
| 540 | switch (params_width(params)) { |
| 541 | case 16: |
| 542 | val_len |= NAU8825_I2S_DL_16; |
| 543 | break; |
| 544 | case 20: |
| 545 | val_len |= NAU8825_I2S_DL_20; |
| 546 | break; |
| 547 | case 24: |
| 548 | val_len |= NAU8825_I2S_DL_24; |
| 549 | break; |
| 550 | case 32: |
| 551 | val_len |= NAU8825_I2S_DL_32; |
| 552 | break; |
| 553 | default: |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | |
| 557 | regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1, |
| 558 | NAU8825_I2S_DL_MASK, val_len); |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int nau8825_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt) |
| 564 | { |
| 565 | struct snd_soc_codec *codec = codec_dai->codec; |
| 566 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 567 | unsigned int ctrl1_val = 0, ctrl2_val = 0; |
| 568 | |
| 569 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 570 | case SND_SOC_DAIFMT_CBM_CFM: |
| 571 | ctrl2_val |= NAU8825_I2S_MS_MASTER; |
| 572 | break; |
| 573 | case SND_SOC_DAIFMT_CBS_CFS: |
| 574 | break; |
| 575 | default: |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | |
| 579 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 580 | case SND_SOC_DAIFMT_NB_NF: |
| 581 | break; |
| 582 | case SND_SOC_DAIFMT_IB_NF: |
| 583 | ctrl1_val |= NAU8825_I2S_BP_INV; |
| 584 | break; |
| 585 | default: |
| 586 | return -EINVAL; |
| 587 | } |
| 588 | |
| 589 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 590 | case SND_SOC_DAIFMT_I2S: |
| 591 | ctrl1_val |= NAU8825_I2S_DF_I2S; |
| 592 | break; |
| 593 | case SND_SOC_DAIFMT_LEFT_J: |
| 594 | ctrl1_val |= NAU8825_I2S_DF_LEFT; |
| 595 | break; |
| 596 | case SND_SOC_DAIFMT_RIGHT_J: |
| 597 | ctrl1_val |= NAU8825_I2S_DF_RIGTH; |
| 598 | break; |
| 599 | case SND_SOC_DAIFMT_DSP_A: |
| 600 | ctrl1_val |= NAU8825_I2S_DF_PCM_AB; |
| 601 | break; |
| 602 | case SND_SOC_DAIFMT_DSP_B: |
| 603 | ctrl1_val |= NAU8825_I2S_DF_PCM_AB; |
| 604 | ctrl1_val |= NAU8825_I2S_PCMB_EN; |
| 605 | break; |
| 606 | default: |
| 607 | return -EINVAL; |
| 608 | } |
| 609 | |
| 610 | regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1, |
| 611 | NAU8825_I2S_DL_MASK | NAU8825_I2S_DF_MASK | |
| 612 | NAU8825_I2S_BP_MASK | NAU8825_I2S_PCMB_MASK, |
| 613 | ctrl1_val); |
| 614 | regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2, |
| 615 | NAU8825_I2S_MS_MASK, ctrl2_val); |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static const struct snd_soc_dai_ops nau8825_dai_ops = { |
| 621 | .hw_params = nau8825_hw_params, |
| 622 | .set_fmt = nau8825_set_dai_fmt, |
| 623 | }; |
| 624 | |
| 625 | #define NAU8825_RATES SNDRV_PCM_RATE_8000_192000 |
| 626 | #define NAU8825_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \ |
| 627 | | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE) |
| 628 | |
| 629 | static struct snd_soc_dai_driver nau8825_dai = { |
| 630 | .name = "nau8825-hifi", |
| 631 | .playback = { |
| 632 | .stream_name = "Playback", |
| 633 | .channels_min = 1, |
| 634 | .channels_max = 2, |
| 635 | .rates = NAU8825_RATES, |
| 636 | .formats = NAU8825_FORMATS, |
| 637 | }, |
| 638 | .capture = { |
| 639 | .stream_name = "Capture", |
| 640 | .channels_min = 1, |
| 641 | .channels_max = 1, |
| 642 | .rates = NAU8825_RATES, |
| 643 | .formats = NAU8825_FORMATS, |
| 644 | }, |
| 645 | .ops = &nau8825_dai_ops, |
| 646 | }; |
| 647 | |
| 648 | /** |
| 649 | * nau8825_enable_jack_detect - Specify a jack for event reporting |
| 650 | * |
| 651 | * @component: component to register the jack with |
| 652 | * @jack: jack to use to report headset and button events on |
| 653 | * |
| 654 | * After this function has been called the headset insert/remove and button |
| 655 | * events will be routed to the given jack. Jack can be null to stop |
| 656 | * reporting. |
| 657 | */ |
| 658 | int nau8825_enable_jack_detect(struct snd_soc_codec *codec, |
| 659 | struct snd_soc_jack *jack) |
| 660 | { |
| 661 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 662 | struct regmap *regmap = nau8825->regmap; |
| 663 | |
| 664 | nau8825->jack = jack; |
| 665 | |
| 666 | /* Ground HP Outputs[1:0], needed for headset auto detection |
| 667 | * Enable Automatic Mic/Gnd switching reading on insert interrupt[6] |
| 668 | */ |
| 669 | regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, |
| 670 | NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L, |
| 671 | NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L); |
| 672 | |
| 673 | regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK, |
| 674 | NAU8825_IRQ_HEADSET_COMPLETE_EN | NAU8825_IRQ_EJECT_EN, 0); |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | EXPORT_SYMBOL_GPL(nau8825_enable_jack_detect); |
| 679 | |
| 680 | |
| 681 | static bool nau8825_is_jack_inserted(struct regmap *regmap) |
| 682 | { |
| 683 | int status; |
| 684 | |
| 685 | regmap_read(regmap, NAU8825_REG_I2C_DEVICE_ID, &status); |
| 686 | return !(status & NAU8825_GPIO2JD1); |
| 687 | } |
| 688 | |
| 689 | static void nau8825_restart_jack_detection(struct regmap *regmap) |
| 690 | { |
Ben Zhang | e6cee90 | 2016-03-25 16:10:39 -0700 | [diff] [blame] | 691 | /* Chip needs one FSCLK cycle in order to generate interrupts, |
| 692 | * as we cannot guarantee one will be provided by the system. Turning |
| 693 | * master mode on then off enables us to generate that FSCLK cycle |
| 694 | * with a minimum of contention on the clock bus. |
| 695 | */ |
| 696 | regmap_update_bits(regmap, NAU8825_REG_I2S_PCM_CTRL2, |
| 697 | NAU8825_I2S_MS_MASK, NAU8825_I2S_MS_MASTER); |
| 698 | regmap_update_bits(regmap, NAU8825_REG_I2S_PCM_CTRL2, |
| 699 | NAU8825_I2S_MS_MASK, NAU8825_I2S_MS_SLAVE); |
| 700 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 701 | /* this will restart the entire jack detection process including MIC/GND |
| 702 | * switching and create interrupts. We have to go from 0 to 1 and back |
| 703 | * to 0 to restart. |
| 704 | */ |
| 705 | regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL, |
| 706 | NAU8825_JACK_DET_RESTART, NAU8825_JACK_DET_RESTART); |
| 707 | regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL, |
| 708 | NAU8825_JACK_DET_RESTART, 0); |
| 709 | } |
| 710 | |
| 711 | static void nau8825_eject_jack(struct nau8825 *nau8825) |
| 712 | { |
| 713 | struct snd_soc_dapm_context *dapm = nau8825->dapm; |
| 714 | struct regmap *regmap = nau8825->regmap; |
| 715 | |
| 716 | snd_soc_dapm_disable_pin(dapm, "SAR"); |
| 717 | snd_soc_dapm_disable_pin(dapm, "MICBIAS"); |
| 718 | /* Detach 2kOhm Resistors from MICBIAS to MICGND1/2 */ |
| 719 | regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS, |
| 720 | NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2, 0); |
| 721 | /* ground HPL/HPR, MICGRND1/2 */ |
| 722 | regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 0xf, 0xf); |
| 723 | |
| 724 | snd_soc_dapm_sync(dapm); |
| 725 | } |
| 726 | |
| 727 | static int nau8825_button_decode(int value) |
| 728 | { |
| 729 | int buttons = 0; |
| 730 | |
| 731 | /* The chip supports up to 8 buttons, but ALSA defines only 6 buttons */ |
| 732 | if (value & BIT(0)) |
| 733 | buttons |= SND_JACK_BTN_0; |
| 734 | if (value & BIT(1)) |
| 735 | buttons |= SND_JACK_BTN_1; |
| 736 | if (value & BIT(2)) |
| 737 | buttons |= SND_JACK_BTN_2; |
| 738 | if (value & BIT(3)) |
| 739 | buttons |= SND_JACK_BTN_3; |
| 740 | if (value & BIT(4)) |
| 741 | buttons |= SND_JACK_BTN_4; |
| 742 | if (value & BIT(5)) |
| 743 | buttons |= SND_JACK_BTN_5; |
| 744 | |
| 745 | return buttons; |
| 746 | } |
| 747 | |
| 748 | static int nau8825_jack_insert(struct nau8825 *nau8825) |
| 749 | { |
| 750 | struct regmap *regmap = nau8825->regmap; |
| 751 | struct snd_soc_dapm_context *dapm = nau8825->dapm; |
| 752 | int jack_status_reg, mic_detected; |
| 753 | int type = 0; |
| 754 | |
| 755 | regmap_read(regmap, NAU8825_REG_GENERAL_STATUS, &jack_status_reg); |
| 756 | mic_detected = (jack_status_reg >> 10) & 3; |
| 757 | |
| 758 | switch (mic_detected) { |
| 759 | case 0: |
| 760 | /* no mic */ |
| 761 | type = SND_JACK_HEADPHONE; |
| 762 | break; |
| 763 | case 1: |
| 764 | dev_dbg(nau8825->dev, "OMTP (micgnd1) mic connected\n"); |
| 765 | type = SND_JACK_HEADSET; |
| 766 | |
| 767 | /* Unground MICGND1 */ |
| 768 | regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 3 << 2, |
| 769 | 1 << 2); |
| 770 | /* Attach 2kOhm Resistor from MICBIAS to MICGND1 */ |
| 771 | regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS, |
| 772 | NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2, |
| 773 | NAU8825_MICBIAS_JKR2); |
| 774 | /* Attach SARADC to MICGND1 */ |
| 775 | regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL, |
| 776 | NAU8825_SAR_INPUT_MASK, |
| 777 | NAU8825_SAR_INPUT_JKR2); |
| 778 | |
| 779 | snd_soc_dapm_force_enable_pin(dapm, "MICBIAS"); |
| 780 | snd_soc_dapm_force_enable_pin(dapm, "SAR"); |
| 781 | snd_soc_dapm_sync(dapm); |
| 782 | break; |
| 783 | case 2: |
| 784 | case 3: |
| 785 | dev_dbg(nau8825->dev, "CTIA (micgnd2) mic connected\n"); |
| 786 | type = SND_JACK_HEADSET; |
| 787 | |
| 788 | /* Unground MICGND2 */ |
| 789 | regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 3 << 2, |
| 790 | 2 << 2); |
| 791 | /* Attach 2kOhm Resistor from MICBIAS to MICGND2 */ |
| 792 | regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS, |
| 793 | NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2, |
| 794 | NAU8825_MICBIAS_JKSLV); |
| 795 | /* Attach SARADC to MICGND2 */ |
| 796 | regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL, |
| 797 | NAU8825_SAR_INPUT_MASK, |
| 798 | NAU8825_SAR_INPUT_JKSLV); |
| 799 | |
| 800 | snd_soc_dapm_force_enable_pin(dapm, "MICBIAS"); |
| 801 | snd_soc_dapm_force_enable_pin(dapm, "SAR"); |
| 802 | snd_soc_dapm_sync(dapm); |
| 803 | break; |
| 804 | } |
| 805 | |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 806 | /* Leaving HPOL/R grounded after jack insert by default. They will be |
| 807 | * ungrounded as part of the widget power up sequence at the beginning |
| 808 | * of playback to reduce pop. |
| 809 | */ |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 810 | return type; |
| 811 | } |
| 812 | |
| 813 | #define NAU8825_BUTTONS (SND_JACK_BTN_0 | SND_JACK_BTN_1 | \ |
| 814 | SND_JACK_BTN_2 | SND_JACK_BTN_3) |
| 815 | |
| 816 | static irqreturn_t nau8825_interrupt(int irq, void *data) |
| 817 | { |
| 818 | struct nau8825 *nau8825 = (struct nau8825 *)data; |
| 819 | struct regmap *regmap = nau8825->regmap; |
| 820 | int active_irq, clear_irq = 0, event = 0, event_mask = 0; |
| 821 | |
Ben Zhang | e6cee90 | 2016-03-25 16:10:39 -0700 | [diff] [blame] | 822 | if (regmap_read(regmap, NAU8825_REG_IRQ_STATUS, &active_irq)) { |
| 823 | dev_err(nau8825->dev, "failed to read irq status\n"); |
| 824 | return IRQ_NONE; |
| 825 | } |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 826 | |
| 827 | if ((active_irq & NAU8825_JACK_EJECTION_IRQ_MASK) == |
| 828 | NAU8825_JACK_EJECTION_DETECTED) { |
| 829 | |
| 830 | nau8825_eject_jack(nau8825); |
| 831 | event_mask |= SND_JACK_HEADSET; |
| 832 | clear_irq = NAU8825_JACK_EJECTION_IRQ_MASK; |
| 833 | } else if (active_irq & NAU8825_KEY_SHORT_PRESS_IRQ) { |
| 834 | int key_status; |
| 835 | |
| 836 | regmap_read(regmap, NAU8825_REG_INT_CLR_KEY_STATUS, |
| 837 | &key_status); |
| 838 | |
| 839 | /* upper 8 bits of the register are for short pressed keys, |
| 840 | * lower 8 bits - for long pressed buttons |
| 841 | */ |
| 842 | nau8825->button_pressed = nau8825_button_decode( |
| 843 | key_status >> 8); |
| 844 | |
| 845 | event |= nau8825->button_pressed; |
| 846 | event_mask |= NAU8825_BUTTONS; |
| 847 | clear_irq = NAU8825_KEY_SHORT_PRESS_IRQ; |
| 848 | } else if (active_irq & NAU8825_KEY_RELEASE_IRQ) { |
| 849 | event_mask = NAU8825_BUTTONS; |
| 850 | clear_irq = NAU8825_KEY_RELEASE_IRQ; |
| 851 | } else if (active_irq & NAU8825_HEADSET_COMPLETION_IRQ) { |
| 852 | if (nau8825_is_jack_inserted(regmap)) { |
| 853 | event |= nau8825_jack_insert(nau8825); |
| 854 | } else { |
| 855 | dev_warn(nau8825->dev, "Headset completion IRQ fired but no headset connected\n"); |
| 856 | nau8825_eject_jack(nau8825); |
| 857 | } |
| 858 | |
| 859 | event_mask |= SND_JACK_HEADSET; |
| 860 | clear_irq = NAU8825_HEADSET_COMPLETION_IRQ; |
| 861 | } |
| 862 | |
| 863 | if (!clear_irq) |
| 864 | clear_irq = active_irq; |
| 865 | /* clears the rightmost interruption */ |
| 866 | regmap_write(regmap, NAU8825_REG_INT_CLR_KEY_STATUS, clear_irq); |
| 867 | |
| 868 | if (event_mask) |
| 869 | snd_soc_jack_report(nau8825->jack, event, event_mask); |
| 870 | |
| 871 | return IRQ_HANDLED; |
| 872 | } |
| 873 | |
| 874 | static void nau8825_setup_buttons(struct nau8825 *nau8825) |
| 875 | { |
| 876 | struct regmap *regmap = nau8825->regmap; |
| 877 | |
| 878 | regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL, |
| 879 | NAU8825_SAR_TRACKING_GAIN_MASK, |
| 880 | nau8825->sar_voltage << NAU8825_SAR_TRACKING_GAIN_SFT); |
| 881 | regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL, |
| 882 | NAU8825_SAR_COMPARE_TIME_MASK, |
| 883 | nau8825->sar_compare_time << NAU8825_SAR_COMPARE_TIME_SFT); |
| 884 | regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL, |
| 885 | NAU8825_SAR_SAMPLING_TIME_MASK, |
| 886 | nau8825->sar_sampling_time << NAU8825_SAR_SAMPLING_TIME_SFT); |
| 887 | |
| 888 | regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL, |
| 889 | NAU8825_KEYDET_LEVELS_NR_MASK, |
| 890 | (nau8825->sar_threshold_num - 1) << NAU8825_KEYDET_LEVELS_NR_SFT); |
| 891 | regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL, |
| 892 | NAU8825_KEYDET_HYSTERESIS_MASK, |
| 893 | nau8825->sar_hysteresis << NAU8825_KEYDET_HYSTERESIS_SFT); |
| 894 | regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL, |
| 895 | NAU8825_KEYDET_SHORTKEY_DEBOUNCE_MASK, |
| 896 | nau8825->key_debounce << NAU8825_KEYDET_SHORTKEY_DEBOUNCE_SFT); |
| 897 | |
| 898 | regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_1, |
| 899 | (nau8825->sar_threshold[0] << 8) | nau8825->sar_threshold[1]); |
| 900 | regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_2, |
| 901 | (nau8825->sar_threshold[2] << 8) | nau8825->sar_threshold[3]); |
| 902 | regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_3, |
| 903 | (nau8825->sar_threshold[4] << 8) | nau8825->sar_threshold[5]); |
| 904 | regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_4, |
| 905 | (nau8825->sar_threshold[6] << 8) | nau8825->sar_threshold[7]); |
| 906 | |
| 907 | /* Enable short press and release interruptions */ |
| 908 | regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK, |
| 909 | NAU8825_IRQ_KEY_SHORT_PRESS_EN | NAU8825_IRQ_KEY_RELEASE_EN, |
| 910 | 0); |
| 911 | } |
| 912 | |
| 913 | static void nau8825_init_regs(struct nau8825 *nau8825) |
| 914 | { |
| 915 | struct regmap *regmap = nau8825->regmap; |
| 916 | |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 917 | /* Latch IIC LSB value */ |
| 918 | regmap_write(regmap, NAU8825_REG_IIC_ADDR_SET, 0x0001); |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 919 | /* Enable Bias/Vmid */ |
| 920 | regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ, |
| 921 | NAU8825_BIAS_VMID, NAU8825_BIAS_VMID); |
| 922 | regmap_update_bits(nau8825->regmap, NAU8825_REG_BOOST, |
| 923 | NAU8825_GLOBAL_BIAS_EN, NAU8825_GLOBAL_BIAS_EN); |
| 924 | |
| 925 | /* VMID Tieoff */ |
| 926 | regmap_update_bits(regmap, NAU8825_REG_BIAS_ADJ, |
| 927 | NAU8825_BIAS_VMID_SEL_MASK, |
| 928 | nau8825->vref_impedance << NAU8825_BIAS_VMID_SEL_SFT); |
| 929 | /* Disable Boost Driver, Automatic Short circuit protection enable */ |
| 930 | regmap_update_bits(regmap, NAU8825_REG_BOOST, |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 931 | NAU8825_PRECHARGE_DIS | NAU8825_HP_BOOST_DIS | |
| 932 | NAU8825_HP_BOOST_G_DIS | NAU8825_SHORT_SHUTDOWN_EN, |
| 933 | NAU8825_PRECHARGE_DIS | NAU8825_HP_BOOST_DIS | |
| 934 | NAU8825_HP_BOOST_G_DIS | NAU8825_SHORT_SHUTDOWN_EN); |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 935 | |
| 936 | regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL, |
| 937 | NAU8825_JKDET_OUTPUT_EN, |
| 938 | nau8825->jkdet_enable ? 0 : NAU8825_JKDET_OUTPUT_EN); |
| 939 | regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL, |
| 940 | NAU8825_JKDET_PULL_EN, |
| 941 | nau8825->jkdet_pull_enable ? 0 : NAU8825_JKDET_PULL_EN); |
| 942 | regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL, |
| 943 | NAU8825_JKDET_PULL_UP, |
| 944 | nau8825->jkdet_pull_up ? NAU8825_JKDET_PULL_UP : 0); |
| 945 | regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL, |
| 946 | NAU8825_JACK_POLARITY, |
| 947 | /* jkdet_polarity - 1 is for active-low */ |
| 948 | nau8825->jkdet_polarity ? 0 : NAU8825_JACK_POLARITY); |
| 949 | |
| 950 | regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL, |
| 951 | NAU8825_JACK_INSERT_DEBOUNCE_MASK, |
| 952 | nau8825->jack_insert_debounce << NAU8825_JACK_INSERT_DEBOUNCE_SFT); |
| 953 | regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL, |
| 954 | NAU8825_JACK_EJECT_DEBOUNCE_MASK, |
| 955 | nau8825->jack_eject_debounce << NAU8825_JACK_EJECT_DEBOUNCE_SFT); |
| 956 | |
| 957 | /* Mask unneeded IRQs: 1 - disable, 0 - enable */ |
| 958 | regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK, 0x7ff, 0x7ff); |
| 959 | |
| 960 | regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS, |
| 961 | NAU8825_MICBIAS_VOLTAGE_MASK, nau8825->micbias_voltage); |
| 962 | |
| 963 | if (nau8825->sar_threshold_num) |
| 964 | nau8825_setup_buttons(nau8825); |
| 965 | |
| 966 | /* Default oversampling/decimations settings are unusable |
| 967 | * (audible hiss). Set it to something better. |
| 968 | */ |
| 969 | regmap_update_bits(regmap, NAU8825_REG_ADC_RATE, |
| 970 | NAU8825_ADC_SYNC_DOWN_MASK, NAU8825_ADC_SYNC_DOWN_128); |
| 971 | regmap_update_bits(regmap, NAU8825_REG_DAC_CTRL1, |
| 972 | NAU8825_DAC_OVERSAMPLE_MASK, NAU8825_DAC_OVERSAMPLE_128); |
John Hsu | 45d5eb3 | 2016-03-11 17:33:58 -0800 | [diff] [blame] | 973 | /* Disable DACR/L power */ |
| 974 | regmap_update_bits(regmap, NAU8825_REG_CHARGE_PUMP, |
| 975 | NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL, |
| 976 | NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL); |
| 977 | /* Enable TESTDAC. This sets the analog DAC inputs to a '0' input |
| 978 | * signal to avoid any glitches due to power up transients in both |
| 979 | * the analog and digital DAC circuit. |
| 980 | */ |
| 981 | regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ, |
| 982 | NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN); |
| 983 | /* CICCLP off */ |
| 984 | regmap_update_bits(regmap, NAU8825_REG_DAC_CTRL1, |
| 985 | NAU8825_DAC_CLIP_OFF, NAU8825_DAC_CLIP_OFF); |
| 986 | |
| 987 | /* Class AB bias current to 2x, DAC Capacitor enable MSB/LSB */ |
| 988 | regmap_update_bits(regmap, NAU8825_REG_ANALOG_CONTROL_2, |
| 989 | NAU8825_HP_NON_CLASSG_CURRENT_2xADJ | |
| 990 | NAU8825_DAC_CAPACITOR_MSB | NAU8825_DAC_CAPACITOR_LSB, |
| 991 | NAU8825_HP_NON_CLASSG_CURRENT_2xADJ | |
| 992 | NAU8825_DAC_CAPACITOR_MSB | NAU8825_DAC_CAPACITOR_LSB); |
| 993 | /* Class G timer 64ms */ |
| 994 | regmap_update_bits(regmap, NAU8825_REG_CLASSG_CTRL, |
| 995 | NAU8825_CLASSG_TIMER_MASK, |
| 996 | 0x20 << NAU8825_CLASSG_TIMER_SFT); |
| 997 | /* DAC clock delay 2ns, VREF */ |
| 998 | regmap_update_bits(regmap, NAU8825_REG_RDAC, |
| 999 | NAU8825_RDAC_CLK_DELAY_MASK | NAU8825_RDAC_VREF_MASK, |
| 1000 | (0x2 << NAU8825_RDAC_CLK_DELAY_SFT) | |
| 1001 | (0x3 << NAU8825_RDAC_VREF_SFT)); |
John Hsu | 3f03916 | 2016-03-30 14:57:11 +0800 | [diff] [blame] | 1002 | /* Config L/R channel */ |
| 1003 | regmap_update_bits(nau8825->regmap, NAU8825_REG_DACL_CTRL, |
| 1004 | NAU8825_DACL_CH_SEL_MASK, NAU8825_DACL_CH_SEL_L); |
| 1005 | regmap_update_bits(nau8825->regmap, NAU8825_REG_DACR_CTRL, |
| 1006 | NAU8825_DACL_CH_SEL_MASK, NAU8825_DACL_CH_SEL_R); |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static const struct regmap_config nau8825_regmap_config = { |
| 1010 | .val_bits = 16, |
| 1011 | .reg_bits = 16, |
| 1012 | |
| 1013 | .max_register = NAU8825_REG_MAX, |
| 1014 | .readable_reg = nau8825_readable_reg, |
| 1015 | .writeable_reg = nau8825_writeable_reg, |
| 1016 | .volatile_reg = nau8825_volatile_reg, |
| 1017 | |
| 1018 | .cache_type = REGCACHE_RBTREE, |
| 1019 | .reg_defaults = nau8825_reg_defaults, |
| 1020 | .num_reg_defaults = ARRAY_SIZE(nau8825_reg_defaults), |
| 1021 | }; |
| 1022 | |
| 1023 | static int nau8825_codec_probe(struct snd_soc_codec *codec) |
| 1024 | { |
| 1025 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 1026 | struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec); |
| 1027 | |
| 1028 | nau8825->dapm = dapm; |
| 1029 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1030 | /* Unmask interruptions. Handler uses dapm object so we can enable |
| 1031 | * interruptions only after dapm is fully initialized. |
| 1032 | */ |
| 1033 | regmap_write(nau8825->regmap, NAU8825_REG_INTERRUPT_DIS_CTRL, 0); |
| 1034 | nau8825_restart_jack_detection(nau8825->regmap); |
| 1035 | |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 1039 | /** |
| 1040 | * nau8825_calc_fll_param - Calculate FLL parameters. |
| 1041 | * @fll_in: external clock provided to codec. |
| 1042 | * @fs: sampling rate. |
| 1043 | * @fll_param: Pointer to structure of FLL parameters. |
| 1044 | * |
| 1045 | * Calculate FLL parameters to configure codec. |
| 1046 | * |
| 1047 | * Returns 0 for success or negative error code. |
| 1048 | */ |
| 1049 | static int nau8825_calc_fll_param(unsigned int fll_in, unsigned int fs, |
| 1050 | struct nau8825_fll *fll_param) |
| 1051 | { |
John Hsu | 407c71b | 2016-03-15 12:09:36 +0800 | [diff] [blame] | 1052 | u64 fvco, fvco_max; |
| 1053 | unsigned int fref, i, fvco_sel; |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 1054 | |
| 1055 | /* Ensure the reference clock frequency (FREF) is <= 13.5MHz by dividing |
| 1056 | * freq_in by 1, 2, 4, or 8 using FLL pre-scalar. |
| 1057 | * FREF = freq_in / NAU8825_FLL_REF_DIV_MASK |
| 1058 | */ |
| 1059 | for (i = 0; i < ARRAY_SIZE(fll_pre_scalar); i++) { |
| 1060 | fref = fll_in / fll_pre_scalar[i].param; |
| 1061 | if (fref <= NAU_FREF_MAX) |
| 1062 | break; |
| 1063 | } |
| 1064 | if (i == ARRAY_SIZE(fll_pre_scalar)) |
| 1065 | return -EINVAL; |
| 1066 | fll_param->clk_ref_div = fll_pre_scalar[i].val; |
| 1067 | |
| 1068 | /* Choose the FLL ratio based on FREF */ |
| 1069 | for (i = 0; i < ARRAY_SIZE(fll_ratio); i++) { |
| 1070 | if (fref >= fll_ratio[i].param) |
| 1071 | break; |
| 1072 | } |
| 1073 | if (i == ARRAY_SIZE(fll_ratio)) |
| 1074 | return -EINVAL; |
| 1075 | fll_param->ratio = fll_ratio[i].val; |
| 1076 | |
| 1077 | /* Calculate the frequency of DCO (FDCO) given freq_out = 256 * Fs. |
John Hsu | 407c71b | 2016-03-15 12:09:36 +0800 | [diff] [blame] | 1078 | * FDCO must be within the 90MHz - 124MHz or the FFL cannot be |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 1079 | * guaranteed across the full range of operation. |
| 1080 | * FDCO = freq_out * 2 * mclk_src_scaling |
| 1081 | */ |
John Hsu | 407c71b | 2016-03-15 12:09:36 +0800 | [diff] [blame] | 1082 | fvco_max = 0; |
| 1083 | fvco_sel = ARRAY_SIZE(mclk_src_scaling); |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 1084 | for (i = 0; i < ARRAY_SIZE(mclk_src_scaling); i++) { |
| 1085 | fvco = 256 * fs * 2 * mclk_src_scaling[i].param; |
John Hsu | 407c71b | 2016-03-15 12:09:36 +0800 | [diff] [blame] | 1086 | if (fvco > NAU_FVCO_MIN && fvco < NAU_FVCO_MAX && |
| 1087 | fvco_max < fvco) { |
| 1088 | fvco_max = fvco; |
| 1089 | fvco_sel = i; |
| 1090 | } |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 1091 | } |
John Hsu | 407c71b | 2016-03-15 12:09:36 +0800 | [diff] [blame] | 1092 | if (ARRAY_SIZE(mclk_src_scaling) == fvco_sel) |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 1093 | return -EINVAL; |
John Hsu | 407c71b | 2016-03-15 12:09:36 +0800 | [diff] [blame] | 1094 | fll_param->mclk_src = mclk_src_scaling[fvco_sel].val; |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 1095 | |
| 1096 | /* Calculate the FLL 10-bit integer input and the FLL 16-bit fractional |
| 1097 | * input based on FDCO, FREF and FLL ratio. |
| 1098 | */ |
| 1099 | fvco = div_u64(fvco << 16, fref * fll_param->ratio); |
| 1100 | fll_param->fll_int = (fvco >> 16) & 0x3FF; |
| 1101 | fll_param->fll_frac = fvco & 0xFFFF; |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | static void nau8825_fll_apply(struct nau8825 *nau8825, |
| 1106 | struct nau8825_fll *fll_param) |
| 1107 | { |
| 1108 | regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER, |
John Hsu | 407c71b | 2016-03-15 12:09:36 +0800 | [diff] [blame] | 1109 | NAU8825_CLK_SRC_MASK | NAU8825_CLK_MCLK_SRC_MASK, |
| 1110 | NAU8825_CLK_SRC_MCLK | fll_param->mclk_src); |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 1111 | regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL1, |
| 1112 | NAU8825_FLL_RATIO_MASK, fll_param->ratio); |
| 1113 | /* FLL 16-bit fractional input */ |
| 1114 | regmap_write(nau8825->regmap, NAU8825_REG_FLL2, fll_param->fll_frac); |
| 1115 | /* FLL 10-bit integer input */ |
| 1116 | regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL3, |
| 1117 | NAU8825_FLL_INTEGER_MASK, fll_param->fll_int); |
| 1118 | /* FLL pre-scaler */ |
| 1119 | regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL4, |
| 1120 | NAU8825_FLL_REF_DIV_MASK, fll_param->clk_ref_div); |
| 1121 | /* select divided VCO input */ |
| 1122 | regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5, |
John Hsu | 407c71b | 2016-03-15 12:09:36 +0800 | [diff] [blame] | 1123 | NAU8825_FLL_CLK_SW_MASK, NAU8825_FLL_CLK_SW_REF); |
| 1124 | /* Disable free-running mode */ |
| 1125 | regmap_update_bits(nau8825->regmap, |
| 1126 | NAU8825_REG_FLL6, NAU8825_DCO_EN, 0); |
| 1127 | if (fll_param->fll_frac) { |
| 1128 | regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5, |
| 1129 | NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN | |
| 1130 | NAU8825_FLL_FTR_SW_MASK, |
| 1131 | NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN | |
| 1132 | NAU8825_FLL_FTR_SW_FILTER); |
| 1133 | regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL6, |
| 1134 | NAU8825_SDM_EN, NAU8825_SDM_EN); |
| 1135 | } else { |
| 1136 | regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5, |
| 1137 | NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN | |
| 1138 | NAU8825_FLL_FTR_SW_MASK, NAU8825_FLL_FTR_SW_ACCU); |
| 1139 | regmap_update_bits(nau8825->regmap, |
| 1140 | NAU8825_REG_FLL6, NAU8825_SDM_EN, 0); |
| 1141 | } |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /* freq_out must be 256*Fs in order to achieve the best performance */ |
| 1145 | static int nau8825_set_pll(struct snd_soc_codec *codec, int pll_id, int source, |
| 1146 | unsigned int freq_in, unsigned int freq_out) |
| 1147 | { |
| 1148 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 1149 | struct nau8825_fll fll_param; |
| 1150 | int ret, fs; |
| 1151 | |
| 1152 | fs = freq_out / 256; |
| 1153 | ret = nau8825_calc_fll_param(freq_in, fs, &fll_param); |
| 1154 | if (ret < 0) { |
| 1155 | dev_err(codec->dev, "Unsupported input clock %d\n", freq_in); |
| 1156 | return ret; |
| 1157 | } |
| 1158 | dev_dbg(codec->dev, "mclk_src=%x ratio=%x fll_frac=%x fll_int=%x clk_ref_div=%x\n", |
| 1159 | fll_param.mclk_src, fll_param.ratio, fll_param.fll_frac, |
| 1160 | fll_param.fll_int, fll_param.clk_ref_div); |
| 1161 | |
| 1162 | nau8825_fll_apply(nau8825, &fll_param); |
| 1163 | mdelay(2); |
| 1164 | regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER, |
| 1165 | NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO); |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
John Hsu | 70543c3 | 2016-03-15 12:08:21 +0800 | [diff] [blame] | 1169 | static int nau8825_mclk_prepare(struct nau8825 *nau8825, unsigned int freq) |
| 1170 | { |
| 1171 | int ret = 0; |
| 1172 | |
| 1173 | nau8825->mclk = devm_clk_get(nau8825->dev, "mclk"); |
| 1174 | if (IS_ERR(nau8825->mclk)) { |
| 1175 | dev_info(nau8825->dev, "No 'mclk' clock found, assume MCLK is managed externally"); |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
| 1179 | if (!nau8825->mclk_freq) { |
| 1180 | ret = clk_prepare_enable(nau8825->mclk); |
| 1181 | if (ret) { |
| 1182 | dev_err(nau8825->dev, "Unable to prepare codec mclk\n"); |
| 1183 | return ret; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | if (nau8825->mclk_freq != freq) { |
| 1188 | freq = clk_round_rate(nau8825->mclk, freq); |
| 1189 | ret = clk_set_rate(nau8825->mclk, freq); |
| 1190 | if (ret) { |
| 1191 | dev_err(nau8825->dev, "Unable to set mclk rate\n"); |
| 1192 | return ret; |
| 1193 | } |
| 1194 | nau8825->mclk_freq = freq; |
| 1195 | } |
| 1196 | |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1200 | static int nau8825_configure_sysclk(struct nau8825 *nau8825, int clk_id, |
| 1201 | unsigned int freq) |
| 1202 | { |
| 1203 | struct regmap *regmap = nau8825->regmap; |
| 1204 | int ret; |
| 1205 | |
| 1206 | switch (clk_id) { |
| 1207 | case NAU8825_CLK_MCLK: |
| 1208 | regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER, |
| 1209 | NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_MCLK); |
| 1210 | regmap_update_bits(regmap, NAU8825_REG_FLL6, NAU8825_DCO_EN, 0); |
John Hsu | 3a56103 | 2016-03-22 11:57:05 +0800 | [diff] [blame] | 1211 | /* MCLK not changed by clock tree */ |
| 1212 | regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER, |
| 1213 | NAU8825_CLK_MCLK_SRC_MASK, 0); |
John Hsu | 70543c3 | 2016-03-15 12:08:21 +0800 | [diff] [blame] | 1214 | ret = nau8825_mclk_prepare(nau8825, freq); |
| 1215 | if (ret) |
| 1216 | return ret; |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1217 | |
| 1218 | break; |
| 1219 | case NAU8825_CLK_INTERNAL: |
| 1220 | regmap_update_bits(regmap, NAU8825_REG_FLL6, NAU8825_DCO_EN, |
| 1221 | NAU8825_DCO_EN); |
| 1222 | regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER, |
| 1223 | NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO); |
John Hsu | 3a56103 | 2016-03-22 11:57:05 +0800 | [diff] [blame] | 1224 | /* Decrease the VCO frequency for power saving */ |
| 1225 | regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER, |
| 1226 | NAU8825_CLK_MCLK_SRC_MASK, 0xf); |
| 1227 | regmap_update_bits(regmap, NAU8825_REG_FLL1, |
| 1228 | NAU8825_FLL_RATIO_MASK, 0x10); |
| 1229 | regmap_update_bits(regmap, NAU8825_REG_FLL6, |
| 1230 | NAU8825_SDM_EN, NAU8825_SDM_EN); |
John Hsu | 70543c3 | 2016-03-15 12:08:21 +0800 | [diff] [blame] | 1231 | if (nau8825->mclk_freq) { |
| 1232 | clk_disable_unprepare(nau8825->mclk); |
| 1233 | nau8825->mclk_freq = 0; |
| 1234 | } |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1235 | |
John Hsu | 70543c3 | 2016-03-15 12:08:21 +0800 | [diff] [blame] | 1236 | break; |
| 1237 | case NAU8825_CLK_FLL_MCLK: |
| 1238 | regmap_update_bits(regmap, NAU8825_REG_FLL3, |
| 1239 | NAU8825_FLL_CLK_SRC_MASK, NAU8825_FLL_CLK_SRC_MCLK); |
| 1240 | ret = nau8825_mclk_prepare(nau8825, freq); |
| 1241 | if (ret) |
| 1242 | return ret; |
| 1243 | |
| 1244 | break; |
| 1245 | case NAU8825_CLK_FLL_BLK: |
| 1246 | regmap_update_bits(regmap, NAU8825_REG_FLL3, |
| 1247 | NAU8825_FLL_CLK_SRC_MASK, NAU8825_FLL_CLK_SRC_BLK); |
| 1248 | if (nau8825->mclk_freq) { |
| 1249 | clk_disable_unprepare(nau8825->mclk); |
| 1250 | nau8825->mclk_freq = 0; |
| 1251 | } |
| 1252 | |
| 1253 | break; |
| 1254 | case NAU8825_CLK_FLL_FS: |
| 1255 | regmap_update_bits(regmap, NAU8825_REG_FLL3, |
| 1256 | NAU8825_FLL_CLK_SRC_MASK, NAU8825_FLL_CLK_SRC_FS); |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1257 | if (nau8825->mclk_freq) { |
| 1258 | clk_disable_unprepare(nau8825->mclk); |
| 1259 | nau8825->mclk_freq = 0; |
| 1260 | } |
| 1261 | |
| 1262 | break; |
| 1263 | default: |
| 1264 | dev_err(nau8825->dev, "Invalid clock id (%d)\n", clk_id); |
| 1265 | return -EINVAL; |
| 1266 | } |
| 1267 | |
| 1268 | dev_dbg(nau8825->dev, "Sysclk is %dHz and clock id is %d\n", freq, |
| 1269 | clk_id); |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | static int nau8825_set_sysclk(struct snd_soc_codec *codec, int clk_id, |
| 1274 | int source, unsigned int freq, int dir) |
| 1275 | { |
| 1276 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 1277 | |
| 1278 | return nau8825_configure_sysclk(nau8825, clk_id, freq); |
| 1279 | } |
| 1280 | |
| 1281 | static int nau8825_set_bias_level(struct snd_soc_codec *codec, |
| 1282 | enum snd_soc_bias_level level) |
| 1283 | { |
| 1284 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 1285 | int ret; |
| 1286 | |
| 1287 | switch (level) { |
| 1288 | case SND_SOC_BIAS_ON: |
| 1289 | break; |
| 1290 | |
| 1291 | case SND_SOC_BIAS_PREPARE: |
| 1292 | break; |
| 1293 | |
| 1294 | case SND_SOC_BIAS_STANDBY: |
| 1295 | if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) { |
| 1296 | if (nau8825->mclk_freq) { |
| 1297 | ret = clk_prepare_enable(nau8825->mclk); |
| 1298 | if (ret) { |
| 1299 | dev_err(nau8825->dev, "Unable to prepare codec mclk\n"); |
| 1300 | return ret; |
| 1301 | } |
| 1302 | } |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1303 | } |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1304 | break; |
| 1305 | |
| 1306 | case SND_SOC_BIAS_OFF: |
| 1307 | if (nau8825->mclk_freq) |
| 1308 | clk_disable_unprepare(nau8825->mclk); |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1309 | break; |
| 1310 | } |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
Ben Zhang | e6cee90 | 2016-03-25 16:10:39 -0700 | [diff] [blame] | 1314 | #ifdef CONFIG_PM |
| 1315 | static int nau8825_suspend(struct snd_soc_codec *codec) |
| 1316 | { |
| 1317 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 1318 | |
| 1319 | disable_irq(nau8825->irq); |
| 1320 | regcache_cache_only(nau8825->regmap, true); |
| 1321 | regcache_mark_dirty(nau8825->regmap); |
| 1322 | |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
| 1326 | static int nau8825_resume(struct snd_soc_codec *codec) |
| 1327 | { |
| 1328 | struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec); |
| 1329 | |
| 1330 | /* The chip may lose power and reset in S3. regcache_sync restores |
| 1331 | * register values including configurations for sysclk, irq, and |
| 1332 | * jack/button detection. |
| 1333 | */ |
| 1334 | regcache_cache_only(nau8825->regmap, false); |
| 1335 | regcache_sync(nau8825->regmap); |
| 1336 | |
| 1337 | /* Check the jack plug status directly. If the headset is unplugged |
| 1338 | * during S3 when the chip has no power, there will be no jack |
| 1339 | * detection irq even after the nau8825_restart_jack_detection below, |
| 1340 | * because the chip just thinks no headset has ever been plugged in. |
| 1341 | */ |
| 1342 | if (!nau8825_is_jack_inserted(nau8825->regmap)) { |
| 1343 | nau8825_eject_jack(nau8825); |
| 1344 | snd_soc_jack_report(nau8825->jack, 0, SND_JACK_HEADSET); |
| 1345 | } |
| 1346 | |
| 1347 | enable_irq(nau8825->irq); |
| 1348 | |
| 1349 | /* Run jack detection to check the type (OMTP or CTIA) of the headset |
| 1350 | * if there is one. This handles the case where a different type of |
| 1351 | * headset is plugged in during S3. This triggers an IRQ iff a headset |
| 1352 | * is already plugged in. |
| 1353 | */ |
| 1354 | nau8825_restart_jack_detection(nau8825->regmap); |
| 1355 | |
| 1356 | return 0; |
| 1357 | } |
| 1358 | #else |
| 1359 | #define nau8825_suspend NULL |
| 1360 | #define nau8825_resume NULL |
| 1361 | #endif |
| 1362 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1363 | static struct snd_soc_codec_driver nau8825_codec_driver = { |
| 1364 | .probe = nau8825_codec_probe, |
| 1365 | .set_sysclk = nau8825_set_sysclk, |
Ben Zhang | c86ba61 | 2015-10-19 16:49:05 -0700 | [diff] [blame] | 1366 | .set_pll = nau8825_set_pll, |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1367 | .set_bias_level = nau8825_set_bias_level, |
| 1368 | .suspend_bias_off = true, |
Ben Zhang | e6cee90 | 2016-03-25 16:10:39 -0700 | [diff] [blame] | 1369 | .suspend = nau8825_suspend, |
| 1370 | .resume = nau8825_resume, |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1371 | |
| 1372 | .controls = nau8825_controls, |
| 1373 | .num_controls = ARRAY_SIZE(nau8825_controls), |
| 1374 | .dapm_widgets = nau8825_dapm_widgets, |
| 1375 | .num_dapm_widgets = ARRAY_SIZE(nau8825_dapm_widgets), |
| 1376 | .dapm_routes = nau8825_dapm_routes, |
| 1377 | .num_dapm_routes = ARRAY_SIZE(nau8825_dapm_routes), |
| 1378 | }; |
| 1379 | |
| 1380 | static void nau8825_reset_chip(struct regmap *regmap) |
| 1381 | { |
| 1382 | regmap_write(regmap, NAU8825_REG_RESET, 0x00); |
| 1383 | regmap_write(regmap, NAU8825_REG_RESET, 0x00); |
| 1384 | } |
| 1385 | |
Ben Zhang | 218d2ce | 2015-10-19 16:49:06 -0700 | [diff] [blame] | 1386 | static void nau8825_print_device_properties(struct nau8825 *nau8825) |
| 1387 | { |
| 1388 | int i; |
| 1389 | struct device *dev = nau8825->dev; |
| 1390 | |
| 1391 | dev_dbg(dev, "jkdet-enable: %d\n", nau8825->jkdet_enable); |
| 1392 | dev_dbg(dev, "jkdet-pull-enable: %d\n", nau8825->jkdet_pull_enable); |
| 1393 | dev_dbg(dev, "jkdet-pull-up: %d\n", nau8825->jkdet_pull_up); |
| 1394 | dev_dbg(dev, "jkdet-polarity: %d\n", nau8825->jkdet_polarity); |
| 1395 | dev_dbg(dev, "micbias-voltage: %d\n", nau8825->micbias_voltage); |
| 1396 | dev_dbg(dev, "vref-impedance: %d\n", nau8825->vref_impedance); |
| 1397 | |
| 1398 | dev_dbg(dev, "sar-threshold-num: %d\n", nau8825->sar_threshold_num); |
| 1399 | for (i = 0; i < nau8825->sar_threshold_num; i++) |
| 1400 | dev_dbg(dev, "sar-threshold[%d]=%d\n", i, |
| 1401 | nau8825->sar_threshold[i]); |
| 1402 | |
| 1403 | dev_dbg(dev, "sar-hysteresis: %d\n", nau8825->sar_hysteresis); |
| 1404 | dev_dbg(dev, "sar-voltage: %d\n", nau8825->sar_voltage); |
| 1405 | dev_dbg(dev, "sar-compare-time: %d\n", nau8825->sar_compare_time); |
| 1406 | dev_dbg(dev, "sar-sampling-time: %d\n", nau8825->sar_sampling_time); |
| 1407 | dev_dbg(dev, "short-key-debounce: %d\n", nau8825->key_debounce); |
| 1408 | dev_dbg(dev, "jack-insert-debounce: %d\n", |
| 1409 | nau8825->jack_insert_debounce); |
| 1410 | dev_dbg(dev, "jack-eject-debounce: %d\n", |
| 1411 | nau8825->jack_eject_debounce); |
| 1412 | } |
| 1413 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1414 | static int nau8825_read_device_properties(struct device *dev, |
| 1415 | struct nau8825 *nau8825) { |
| 1416 | |
| 1417 | nau8825->jkdet_enable = device_property_read_bool(dev, |
| 1418 | "nuvoton,jkdet-enable"); |
| 1419 | nau8825->jkdet_pull_enable = device_property_read_bool(dev, |
| 1420 | "nuvoton,jkdet-pull-enable"); |
| 1421 | nau8825->jkdet_pull_up = device_property_read_bool(dev, |
| 1422 | "nuvoton,jkdet-pull-up"); |
| 1423 | device_property_read_u32(dev, "nuvoton,jkdet-polarity", |
| 1424 | &nau8825->jkdet_polarity); |
| 1425 | device_property_read_u32(dev, "nuvoton,micbias-voltage", |
| 1426 | &nau8825->micbias_voltage); |
| 1427 | device_property_read_u32(dev, "nuvoton,vref-impedance", |
| 1428 | &nau8825->vref_impedance); |
| 1429 | device_property_read_u32(dev, "nuvoton,sar-threshold-num", |
| 1430 | &nau8825->sar_threshold_num); |
| 1431 | device_property_read_u32_array(dev, "nuvoton,sar-threshold", |
| 1432 | nau8825->sar_threshold, nau8825->sar_threshold_num); |
| 1433 | device_property_read_u32(dev, "nuvoton,sar-hysteresis", |
| 1434 | &nau8825->sar_hysteresis); |
| 1435 | device_property_read_u32(dev, "nuvoton,sar-voltage", |
| 1436 | &nau8825->sar_voltage); |
| 1437 | device_property_read_u32(dev, "nuvoton,sar-compare-time", |
| 1438 | &nau8825->sar_compare_time); |
| 1439 | device_property_read_u32(dev, "nuvoton,sar-sampling-time", |
| 1440 | &nau8825->sar_sampling_time); |
| 1441 | device_property_read_u32(dev, "nuvoton,short-key-debounce", |
| 1442 | &nau8825->key_debounce); |
| 1443 | device_property_read_u32(dev, "nuvoton,jack-insert-debounce", |
| 1444 | &nau8825->jack_insert_debounce); |
| 1445 | device_property_read_u32(dev, "nuvoton,jack-eject-debounce", |
| 1446 | &nau8825->jack_eject_debounce); |
| 1447 | |
| 1448 | nau8825->mclk = devm_clk_get(dev, "mclk"); |
| 1449 | if (PTR_ERR(nau8825->mclk) == -EPROBE_DEFER) { |
| 1450 | return -EPROBE_DEFER; |
| 1451 | } else if (PTR_ERR(nau8825->mclk) == -ENOENT) { |
| 1452 | /* The MCLK is managed externally or not used at all */ |
| 1453 | nau8825->mclk = NULL; |
| 1454 | dev_info(dev, "No 'mclk' clock found, assume MCLK is managed externally"); |
| 1455 | } else if (IS_ERR(nau8825->mclk)) { |
| 1456 | return -EINVAL; |
| 1457 | } |
| 1458 | |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | static int nau8825_setup_irq(struct nau8825 *nau8825) |
| 1463 | { |
| 1464 | struct regmap *regmap = nau8825->regmap; |
| 1465 | int ret; |
| 1466 | |
| 1467 | /* IRQ Output Enable */ |
| 1468 | regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK, |
| 1469 | NAU8825_IRQ_OUTPUT_EN, NAU8825_IRQ_OUTPUT_EN); |
| 1470 | |
| 1471 | /* Enable internal VCO needed for interruptions */ |
| 1472 | nau8825_configure_sysclk(nau8825, NAU8825_CLK_INTERNAL, 0); |
| 1473 | |
John Hsu | eeef16a | 2016-03-22 11:57:20 +0800 | [diff] [blame] | 1474 | /* Enable ADC needed for interrupts */ |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1475 | regmap_update_bits(regmap, NAU8825_REG_ENA_CTRL, |
John Hsu | eeef16a | 2016-03-22 11:57:20 +0800 | [diff] [blame] | 1476 | NAU8825_ENABLE_ADC, NAU8825_ENABLE_ADC); |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1477 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1478 | ret = devm_request_threaded_irq(nau8825->dev, nau8825->irq, NULL, |
| 1479 | nau8825_interrupt, IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 1480 | "nau8825", nau8825); |
| 1481 | |
| 1482 | if (ret) { |
| 1483 | dev_err(nau8825->dev, "Cannot request irq %d (%d)\n", |
| 1484 | nau8825->irq, ret); |
| 1485 | return ret; |
| 1486 | } |
| 1487 | |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
| 1491 | static int nau8825_i2c_probe(struct i2c_client *i2c, |
| 1492 | const struct i2c_device_id *id) |
| 1493 | { |
| 1494 | struct device *dev = &i2c->dev; |
| 1495 | struct nau8825 *nau8825 = dev_get_platdata(&i2c->dev); |
| 1496 | int ret, value; |
| 1497 | |
| 1498 | if (!nau8825) { |
| 1499 | nau8825 = devm_kzalloc(dev, sizeof(*nau8825), GFP_KERNEL); |
| 1500 | if (!nau8825) |
| 1501 | return -ENOMEM; |
| 1502 | ret = nau8825_read_device_properties(dev, nau8825); |
| 1503 | if (ret) |
| 1504 | return ret; |
| 1505 | } |
| 1506 | |
| 1507 | i2c_set_clientdata(i2c, nau8825); |
| 1508 | |
| 1509 | nau8825->regmap = devm_regmap_init_i2c(i2c, &nau8825_regmap_config); |
| 1510 | if (IS_ERR(nau8825->regmap)) |
| 1511 | return PTR_ERR(nau8825->regmap); |
| 1512 | nau8825->dev = dev; |
| 1513 | nau8825->irq = i2c->irq; |
| 1514 | |
Ben Zhang | 218d2ce | 2015-10-19 16:49:06 -0700 | [diff] [blame] | 1515 | nau8825_print_device_properties(nau8825); |
| 1516 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1517 | nau8825_reset_chip(nau8825->regmap); |
| 1518 | ret = regmap_read(nau8825->regmap, NAU8825_REG_I2C_DEVICE_ID, &value); |
| 1519 | if (ret < 0) { |
| 1520 | dev_err(dev, "Failed to read device id from the NAU8825: %d\n", |
| 1521 | ret); |
| 1522 | return ret; |
| 1523 | } |
| 1524 | if ((value & NAU8825_SOFTWARE_ID_MASK) != |
| 1525 | NAU8825_SOFTWARE_ID_NAU8825) { |
| 1526 | dev_err(dev, "Not a NAU8825 chip\n"); |
| 1527 | return -ENODEV; |
| 1528 | } |
| 1529 | |
| 1530 | nau8825_init_regs(nau8825); |
| 1531 | |
| 1532 | if (i2c->irq) |
| 1533 | nau8825_setup_irq(nau8825); |
| 1534 | |
| 1535 | return snd_soc_register_codec(&i2c->dev, &nau8825_codec_driver, |
| 1536 | &nau8825_dai, 1); |
| 1537 | } |
| 1538 | |
| 1539 | static int nau8825_i2c_remove(struct i2c_client *client) |
| 1540 | { |
| 1541 | snd_soc_unregister_codec(&client->dev); |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | static const struct i2c_device_id nau8825_i2c_ids[] = { |
| 1546 | { "nau8825", 0 }, |
| 1547 | { } |
| 1548 | }; |
Javier Martinez Canillas | ffd7250 | 2016-05-17 12:00:09 -0400 | [diff] [blame] | 1549 | MODULE_DEVICE_TABLE(i2c, nau8825_i2c_ids); |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1550 | |
| 1551 | #ifdef CONFIG_OF |
| 1552 | static const struct of_device_id nau8825_of_ids[] = { |
| 1553 | { .compatible = "nuvoton,nau8825", }, |
| 1554 | {} |
| 1555 | }; |
| 1556 | MODULE_DEVICE_TABLE(of, nau8825_of_ids); |
| 1557 | #endif |
| 1558 | |
Fang, Yang A | b368130 | 2015-10-07 14:33:57 -0700 | [diff] [blame] | 1559 | #ifdef CONFIG_ACPI |
| 1560 | static const struct acpi_device_id nau8825_acpi_match[] = { |
| 1561 | { "10508825", 0 }, |
| 1562 | {}, |
| 1563 | }; |
| 1564 | MODULE_DEVICE_TABLE(acpi, nau8825_acpi_match); |
| 1565 | #endif |
| 1566 | |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1567 | static struct i2c_driver nau8825_driver = { |
| 1568 | .driver = { |
| 1569 | .name = "nau8825", |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1570 | .of_match_table = of_match_ptr(nau8825_of_ids), |
Fang, Yang A | b368130 | 2015-10-07 14:33:57 -0700 | [diff] [blame] | 1571 | .acpi_match_table = ACPI_PTR(nau8825_acpi_match), |
Anatol Pomozov | 34ca27f | 2015-10-02 09:49:14 -0700 | [diff] [blame] | 1572 | }, |
| 1573 | .probe = nau8825_i2c_probe, |
| 1574 | .remove = nau8825_i2c_remove, |
| 1575 | .id_table = nau8825_i2c_ids, |
| 1576 | }; |
| 1577 | module_i2c_driver(nau8825_driver); |
| 1578 | |
| 1579 | MODULE_DESCRIPTION("ASoC nau8825 driver"); |
| 1580 | MODULE_AUTHOR("Anatol Pomozov <anatol@chromium.org>"); |
| 1581 | MODULE_LICENSE("GPL"); |