Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Nuvoton NAU8825 audio codec driver
0004  *
0005  * Copyright 2015 Google Chromium project.
0006  *  Author: Anatol Pomozov <anatol@chromium.org>
0007  * Copyright 2015 Nuvoton Technology Corp.
0008  *  Co-author: Meng-Huang Kuo <mhkuo@nuvoton.com>
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/delay.h>
0013 #include <linux/init.h>
0014 #include <linux/i2c.h>
0015 #include <linux/regmap.h>
0016 #include <linux/slab.h>
0017 #include <linux/clk.h>
0018 #include <linux/acpi.h>
0019 #include <linux/math64.h>
0020 #include <linux/semaphore.h>
0021 
0022 #include <sound/initval.h>
0023 #include <sound/tlv.h>
0024 #include <sound/core.h>
0025 #include <sound/pcm.h>
0026 #include <sound/pcm_params.h>
0027 #include <sound/soc.h>
0028 #include <sound/jack.h>
0029 
0030 
0031 #include "nau8825.h"
0032 
0033 
0034 #define NUVOTON_CODEC_DAI "nau8825-hifi"
0035 
0036 #define NAU_FREF_MAX 13500000
0037 #define NAU_FVCO_MAX 124000000
0038 #define NAU_FVCO_MIN 90000000
0039 
0040 /* cross talk suppression detection */
0041 #define LOG10_MAGIC 646456993
0042 #define GAIN_AUGMENT 22500
0043 #define SIDETONE_BASE 207000
0044 
0045 /* the maximum frequency of CLK_ADC and CLK_DAC */
0046 #define CLK_DA_AD_MAX 6144000
0047 
0048 static int nau8825_configure_sysclk(struct nau8825 *nau8825,
0049         int clk_id, unsigned int freq);
0050 static bool nau8825_is_jack_inserted(struct regmap *regmap);
0051 
0052 struct nau8825_fll {
0053     int mclk_src;
0054     int ratio;
0055     int fll_frac;
0056     int fll_int;
0057     int clk_ref_div;
0058 };
0059 
0060 struct nau8825_fll_attr {
0061     unsigned int param;
0062     unsigned int val;
0063 };
0064 
0065 /* scaling for mclk from sysclk_src output */
0066 static const struct nau8825_fll_attr mclk_src_scaling[] = {
0067     { 1, 0x0 },
0068     { 2, 0x2 },
0069     { 4, 0x3 },
0070     { 8, 0x4 },
0071     { 16, 0x5 },
0072     { 32, 0x6 },
0073     { 3, 0x7 },
0074     { 6, 0xa },
0075     { 12, 0xb },
0076     { 24, 0xc },
0077     { 48, 0xd },
0078     { 96, 0xe },
0079     { 5, 0xf },
0080 };
0081 
0082 /* ratio for input clk freq */
0083 static const struct nau8825_fll_attr fll_ratio[] = {
0084     { 512000, 0x01 },
0085     { 256000, 0x02 },
0086     { 128000, 0x04 },
0087     { 64000, 0x08 },
0088     { 32000, 0x10 },
0089     { 8000, 0x20 },
0090     { 4000, 0x40 },
0091 };
0092 
0093 static const struct nau8825_fll_attr fll_pre_scalar[] = {
0094     { 1, 0x0 },
0095     { 2, 0x1 },
0096     { 4, 0x2 },
0097     { 8, 0x3 },
0098 };
0099 
0100 /* over sampling rate */
0101 struct nau8825_osr_attr {
0102     unsigned int osr;
0103     unsigned int clk_src;
0104 };
0105 
0106 static const struct nau8825_osr_attr osr_dac_sel[] = {
0107     { 64, 2 },  /* OSR 64, SRC 1/4 */
0108     { 256, 0 }, /* OSR 256, SRC 1 */
0109     { 128, 1 }, /* OSR 128, SRC 1/2 */
0110     { 0, 0 },
0111     { 32, 3 },  /* OSR 32, SRC 1/8 */
0112 };
0113 
0114 static const struct nau8825_osr_attr osr_adc_sel[] = {
0115     { 32, 3 },  /* OSR 32, SRC 1/8 */
0116     { 64, 2 },  /* OSR 64, SRC 1/4 */
0117     { 128, 1 }, /* OSR 128, SRC 1/2 */
0118     { 256, 0 }, /* OSR 256, SRC 1 */
0119 };
0120 
0121 static const struct reg_default nau8825_reg_defaults[] = {
0122     { NAU8825_REG_ENA_CTRL, 0x00ff },
0123     { NAU8825_REG_IIC_ADDR_SET, 0x0 },
0124     { NAU8825_REG_CLK_DIVIDER, 0x0050 },
0125     { NAU8825_REG_FLL1, 0x0 },
0126     { NAU8825_REG_FLL2, 0x3126 },
0127     { NAU8825_REG_FLL3, 0x0008 },
0128     { NAU8825_REG_FLL4, 0x0010 },
0129     { NAU8825_REG_FLL5, 0x0 },
0130     { NAU8825_REG_FLL6, 0x6000 },
0131     { NAU8825_REG_FLL_VCO_RSV, 0xf13c },
0132     { NAU8825_REG_HSD_CTRL, 0x000c },
0133     { NAU8825_REG_JACK_DET_CTRL, 0x0 },
0134     { NAU8825_REG_INTERRUPT_MASK, 0x0 },
0135     { NAU8825_REG_INTERRUPT_DIS_CTRL, 0xffff },
0136     { NAU8825_REG_SAR_CTRL, 0x0015 },
0137     { NAU8825_REG_KEYDET_CTRL, 0x0110 },
0138     { NAU8825_REG_VDET_THRESHOLD_1, 0x0 },
0139     { NAU8825_REG_VDET_THRESHOLD_2, 0x0 },
0140     { NAU8825_REG_VDET_THRESHOLD_3, 0x0 },
0141     { NAU8825_REG_VDET_THRESHOLD_4, 0x0 },
0142     { NAU8825_REG_GPIO34_CTRL, 0x0 },
0143     { NAU8825_REG_GPIO12_CTRL, 0x0 },
0144     { NAU8825_REG_TDM_CTRL, 0x0 },
0145     { NAU8825_REG_I2S_PCM_CTRL1, 0x000b },
0146     { NAU8825_REG_I2S_PCM_CTRL2, 0x8010 },
0147     { NAU8825_REG_LEFT_TIME_SLOT, 0x0 },
0148     { NAU8825_REG_RIGHT_TIME_SLOT, 0x0 },
0149     { NAU8825_REG_BIQ_CTRL, 0x0 },
0150     { NAU8825_REG_BIQ_COF1, 0x0 },
0151     { NAU8825_REG_BIQ_COF2, 0x0 },
0152     { NAU8825_REG_BIQ_COF3, 0x0 },
0153     { NAU8825_REG_BIQ_COF4, 0x0 },
0154     { NAU8825_REG_BIQ_COF5, 0x0 },
0155     { NAU8825_REG_BIQ_COF6, 0x0 },
0156     { NAU8825_REG_BIQ_COF7, 0x0 },
0157     { NAU8825_REG_BIQ_COF8, 0x0 },
0158     { NAU8825_REG_BIQ_COF9, 0x0 },
0159     { NAU8825_REG_BIQ_COF10, 0x0 },
0160     { NAU8825_REG_ADC_RATE, 0x0010 },
0161     { NAU8825_REG_DAC_CTRL1, 0x0001 },
0162     { NAU8825_REG_DAC_CTRL2, 0x0 },
0163     { NAU8825_REG_DAC_DGAIN_CTRL, 0x0 },
0164     { NAU8825_REG_ADC_DGAIN_CTRL, 0x00cf },
0165     { NAU8825_REG_MUTE_CTRL, 0x0 },
0166     { NAU8825_REG_HSVOL_CTRL, 0x0 },
0167     { NAU8825_REG_DACL_CTRL, 0x02cf },
0168     { NAU8825_REG_DACR_CTRL, 0x00cf },
0169     { NAU8825_REG_ADC_DRC_KNEE_IP12, 0x1486 },
0170     { NAU8825_REG_ADC_DRC_KNEE_IP34, 0x0f12 },
0171     { NAU8825_REG_ADC_DRC_SLOPES, 0x25ff },
0172     { NAU8825_REG_ADC_DRC_ATKDCY, 0x3457 },
0173     { NAU8825_REG_DAC_DRC_KNEE_IP12, 0x1486 },
0174     { NAU8825_REG_DAC_DRC_KNEE_IP34, 0x0f12 },
0175     { NAU8825_REG_DAC_DRC_SLOPES, 0x25f9 },
0176     { NAU8825_REG_DAC_DRC_ATKDCY, 0x3457 },
0177     { NAU8825_REG_IMM_MODE_CTRL, 0x0 },
0178     { NAU8825_REG_CLASSG_CTRL, 0x0 },
0179     { NAU8825_REG_OPT_EFUSE_CTRL, 0x0 },
0180     { NAU8825_REG_MISC_CTRL, 0x0 },
0181     { NAU8825_REG_BIAS_ADJ, 0x0 },
0182     { NAU8825_REG_TRIM_SETTINGS, 0x0 },
0183     { NAU8825_REG_ANALOG_CONTROL_1, 0x0 },
0184     { NAU8825_REG_ANALOG_CONTROL_2, 0x0 },
0185     { NAU8825_REG_ANALOG_ADC_1, 0x0011 },
0186     { NAU8825_REG_ANALOG_ADC_2, 0x0020 },
0187     { NAU8825_REG_RDAC, 0x0008 },
0188     { NAU8825_REG_MIC_BIAS, 0x0006 },
0189     { NAU8825_REG_BOOST, 0x0 },
0190     { NAU8825_REG_FEPGA, 0x0 },
0191     { NAU8825_REG_POWER_UP_CONTROL, 0x0 },
0192     { NAU8825_REG_CHARGE_PUMP, 0x0 },
0193 };
0194 
0195 /* register backup table when cross talk detection */
0196 static struct reg_default nau8825_xtalk_baktab[] = {
0197     { NAU8825_REG_ADC_DGAIN_CTRL, 0x00cf },
0198     { NAU8825_REG_HSVOL_CTRL, 0 },
0199     { NAU8825_REG_DACL_CTRL, 0x00cf },
0200     { NAU8825_REG_DACR_CTRL, 0x02cf },
0201 };
0202 
0203 static const unsigned short logtable[256] = {
0204     0x0000, 0x0171, 0x02e0, 0x044e, 0x05ba, 0x0725, 0x088e, 0x09f7,
0205     0x0b5d, 0x0cc3, 0x0e27, 0x0f8a, 0x10eb, 0x124b, 0x13aa, 0x1508,
0206     0x1664, 0x17bf, 0x1919, 0x1a71, 0x1bc8, 0x1d1e, 0x1e73, 0x1fc6,
0207     0x2119, 0x226a, 0x23ba, 0x2508, 0x2656, 0x27a2, 0x28ed, 0x2a37,
0208     0x2b80, 0x2cc8, 0x2e0f, 0x2f54, 0x3098, 0x31dc, 0x331e, 0x345f,
0209     0x359f, 0x36de, 0x381b, 0x3958, 0x3a94, 0x3bce, 0x3d08, 0x3e41,
0210     0x3f78, 0x40af, 0x41e4, 0x4319, 0x444c, 0x457f, 0x46b0, 0x47e1,
0211     0x4910, 0x4a3f, 0x4b6c, 0x4c99, 0x4dc5, 0x4eef, 0x5019, 0x5142,
0212     0x526a, 0x5391, 0x54b7, 0x55dc, 0x5700, 0x5824, 0x5946, 0x5a68,
0213     0x5b89, 0x5ca8, 0x5dc7, 0x5ee5, 0x6003, 0x611f, 0x623a, 0x6355,
0214     0x646f, 0x6588, 0x66a0, 0x67b7, 0x68ce, 0x69e4, 0x6af8, 0x6c0c,
0215     0x6d20, 0x6e32, 0x6f44, 0x7055, 0x7165, 0x7274, 0x7383, 0x7490,
0216     0x759d, 0x76aa, 0x77b5, 0x78c0, 0x79ca, 0x7ad3, 0x7bdb, 0x7ce3,
0217     0x7dea, 0x7ef0, 0x7ff6, 0x80fb, 0x81ff, 0x8302, 0x8405, 0x8507,
0218     0x8608, 0x8709, 0x8809, 0x8908, 0x8a06, 0x8b04, 0x8c01, 0x8cfe,
0219     0x8dfa, 0x8ef5, 0x8fef, 0x90e9, 0x91e2, 0x92db, 0x93d2, 0x94ca,
0220     0x95c0, 0x96b6, 0x97ab, 0x98a0, 0x9994, 0x9a87, 0x9b7a, 0x9c6c,
0221     0x9d5e, 0x9e4f, 0x9f3f, 0xa02e, 0xa11e, 0xa20c, 0xa2fa, 0xa3e7,
0222     0xa4d4, 0xa5c0, 0xa6ab, 0xa796, 0xa881, 0xa96a, 0xaa53, 0xab3c,
0223     0xac24, 0xad0c, 0xadf2, 0xaed9, 0xafbe, 0xb0a4, 0xb188, 0xb26c,
0224     0xb350, 0xb433, 0xb515, 0xb5f7, 0xb6d9, 0xb7ba, 0xb89a, 0xb97a,
0225     0xba59, 0xbb38, 0xbc16, 0xbcf4, 0xbdd1, 0xbead, 0xbf8a, 0xc065,
0226     0xc140, 0xc21b, 0xc2f5, 0xc3cf, 0xc4a8, 0xc580, 0xc658, 0xc730,
0227     0xc807, 0xc8de, 0xc9b4, 0xca8a, 0xcb5f, 0xcc34, 0xcd08, 0xcddc,
0228     0xceaf, 0xcf82, 0xd054, 0xd126, 0xd1f7, 0xd2c8, 0xd399, 0xd469,
0229     0xd538, 0xd607, 0xd6d6, 0xd7a4, 0xd872, 0xd93f, 0xda0c, 0xdad9,
0230     0xdba5, 0xdc70, 0xdd3b, 0xde06, 0xded0, 0xdf9a, 0xe063, 0xe12c,
0231     0xe1f5, 0xe2bd, 0xe385, 0xe44c, 0xe513, 0xe5d9, 0xe69f, 0xe765,
0232     0xe82a, 0xe8ef, 0xe9b3, 0xea77, 0xeb3b, 0xebfe, 0xecc1, 0xed83,
0233     0xee45, 0xef06, 0xefc8, 0xf088, 0xf149, 0xf209, 0xf2c8, 0xf387,
0234     0xf446, 0xf505, 0xf5c3, 0xf680, 0xf73e, 0xf7fb, 0xf8b7, 0xf973,
0235     0xfa2f, 0xfaea, 0xfba5, 0xfc60, 0xfd1a, 0xfdd4, 0xfe8e, 0xff47
0236 };
0237 
0238 /**
0239  * nau8825_sema_acquire - acquire the semaphore of nau88l25
0240  * @nau8825:  component to register the codec private data with
0241  * @timeout: how long in jiffies to wait before failure or zero to wait
0242  * until release
0243  *
0244  * Attempts to acquire the semaphore with number of jiffies. If no more
0245  * tasks are allowed to acquire the semaphore, calling this function will
0246  * put the task to sleep. If the semaphore is not released within the
0247  * specified number of jiffies, this function returns.
0248  * If the semaphore is not released within the specified number of jiffies,
0249  * this function returns -ETIME. If the sleep is interrupted by a signal,
0250  * this function will return -EINTR. It returns 0 if the semaphore was
0251  * acquired successfully.
0252  *
0253  * Acquires the semaphore without jiffies. Try to acquire the semaphore
0254  * atomically. Returns 0 if the semaphore has been acquired successfully
0255  * or 1 if it cannot be acquired.
0256  */
0257 static int nau8825_sema_acquire(struct nau8825 *nau8825, long timeout)
0258 {
0259     int ret;
0260 
0261     if (timeout) {
0262         ret = down_timeout(&nau8825->xtalk_sem, timeout);
0263         if (ret < 0)
0264             dev_warn(nau8825->dev, "Acquire semaphore timeout\n");
0265     } else {
0266         ret = down_trylock(&nau8825->xtalk_sem);
0267         if (ret)
0268             dev_warn(nau8825->dev, "Acquire semaphore fail\n");
0269     }
0270 
0271     return ret;
0272 }
0273 
0274 /**
0275  * nau8825_sema_release - release the semaphore of nau88l25
0276  * @nau8825:  component to register the codec private data with
0277  *
0278  * Release the semaphore which may be called from any context and
0279  * even by tasks which have never called down().
0280  */
0281 static inline void nau8825_sema_release(struct nau8825 *nau8825)
0282 {
0283     up(&nau8825->xtalk_sem);
0284 }
0285 
0286 /**
0287  * nau8825_sema_reset - reset the semaphore for nau88l25
0288  * @nau8825:  component to register the codec private data with
0289  *
0290  * Reset the counter of the semaphore. Call this function to restart
0291  * a new round task management.
0292  */
0293 static inline void nau8825_sema_reset(struct nau8825 *nau8825)
0294 {
0295     nau8825->xtalk_sem.count = 1;
0296 }
0297 
0298 /**
0299  * nau8825_hpvol_ramp - Ramp up the headphone volume change gradually to target level.
0300  *
0301  * @nau8825:  component to register the codec private data with
0302  * @vol_from: the volume to start up
0303  * @vol_to: the target volume
0304  * @step: the volume span to move on
0305  *
0306  * The headphone volume is from 0dB to minimum -54dB and -1dB per step.
0307  * If the volume changes sharp, there is a pop noise heard in headphone. We
0308  * provide the function to ramp up the volume up or down by delaying 10ms
0309  * per step.
0310  */
0311 static void nau8825_hpvol_ramp(struct nau8825 *nau8825,
0312     unsigned int vol_from, unsigned int vol_to, unsigned int step)
0313 {
0314     unsigned int value, volume, ramp_up, from, to;
0315 
0316     if (vol_from == vol_to || step == 0) {
0317         return;
0318     } else if (vol_from < vol_to) {
0319         ramp_up = true;
0320         from = vol_from;
0321         to = vol_to;
0322     } else {
0323         ramp_up = false;
0324         from = vol_to;
0325         to = vol_from;
0326     }
0327     /* only handle volume from 0dB to minimum -54dB */
0328     if (to > NAU8825_HP_VOL_MIN)
0329         to = NAU8825_HP_VOL_MIN;
0330 
0331     for (volume = from; volume < to; volume += step) {
0332         if (ramp_up)
0333             value = volume;
0334         else
0335             value = to - volume + from;
0336         regmap_update_bits(nau8825->regmap, NAU8825_REG_HSVOL_CTRL,
0337             NAU8825_HPL_VOL_MASK | NAU8825_HPR_VOL_MASK,
0338             (value << NAU8825_HPL_VOL_SFT) | value);
0339         usleep_range(10000, 10500);
0340     }
0341     if (ramp_up)
0342         value = to;
0343     else
0344         value = from;
0345     regmap_update_bits(nau8825->regmap, NAU8825_REG_HSVOL_CTRL,
0346         NAU8825_HPL_VOL_MASK | NAU8825_HPR_VOL_MASK,
0347         (value << NAU8825_HPL_VOL_SFT) | value);
0348 }
0349 
0350 /**
0351  * nau8825_intlog10_dec3 - Computes log10 of a value
0352  * the result is round off to 3 decimal. This function takes reference to
0353  * dvb-math. The source code locates as the following.
0354  * Linux/drivers/media/dvb-core/dvb_math.c
0355  * @value:  input for log10
0356  *
0357  * return log10(value) * 1000
0358  */
0359 static u32 nau8825_intlog10_dec3(u32 value)
0360 {
0361     u32 msb, logentry, significand, interpolation, log10val;
0362     u64 log2val;
0363 
0364     /* first detect the msb (count begins at 0) */
0365     msb = fls(value) - 1;
0366     /**
0367      *      now we use a logtable after the following method:
0368      *
0369      *      log2(2^x * y) * 2^24 = x * 2^24 + log2(y) * 2^24
0370      *      where x = msb and therefore 1 <= y < 2
0371      *      first y is determined by shifting the value left
0372      *      so that msb is bit 31
0373      *              0x00231f56 -> 0x8C7D5800
0374      *      the result is y * 2^31 -> "significand"
0375      *      then the highest 9 bits are used for a table lookup
0376      *      the highest bit is discarded because it's always set
0377      *      the highest nine bits in our example are 100011000
0378      *      so we would use the entry 0x18
0379      */
0380     significand = value << (31 - msb);
0381     logentry = (significand >> 23) & 0xff;
0382     /**
0383      *      last step we do is interpolation because of the
0384      *      limitations of the log table the error is that part of
0385      *      the significand which isn't used for lookup then we
0386      *      compute the ratio between the error and the next table entry
0387      *      and interpolate it between the log table entry used and the
0388      *      next one the biggest error possible is 0x7fffff
0389      *      (in our example it's 0x7D5800)
0390      *      needed value for next table entry is 0x800000
0391      *      so the interpolation is
0392      *      (error / 0x800000) * (logtable_next - logtable_current)
0393      *      in the implementation the division is moved to the end for
0394      *      better accuracy there is also an overflow correction if
0395      *      logtable_next is 256
0396      */
0397     interpolation = ((significand & 0x7fffff) *
0398         ((logtable[(logentry + 1) & 0xff] -
0399         logtable[logentry]) & 0xffff)) >> 15;
0400 
0401     log2val = ((msb << 24) + (logtable[logentry] << 8) + interpolation);
0402     /**
0403      *      log10(x) = log2(x) * log10(2)
0404      */
0405     log10val = (log2val * LOG10_MAGIC) >> 31;
0406     /**
0407      *      the result is round off to 3 decimal
0408      */
0409     return log10val / ((1 << 24) / 1000);
0410 }
0411 
0412 /**
0413  * nau8825_xtalk_sidetone - computes cross talk suppression sidetone gain.
0414  *
0415  * @sig_org: orignal signal level
0416  * @sig_cros: cross talk signal level
0417  *
0418  * The orignal and cross talk signal vlues need to be characterized.
0419  * Once these values have been characterized, this sidetone value
0420  * can be converted to decibel with the equation below.
0421  * sidetone = 20 * log (original signal level / crosstalk signal level)
0422  *
0423  * return cross talk sidetone gain
0424  */
0425 static u32 nau8825_xtalk_sidetone(u32 sig_org, u32 sig_cros)
0426 {
0427     u32 gain, sidetone;
0428 
0429     if (WARN_ON(sig_org == 0 || sig_cros == 0))
0430         return 0;
0431 
0432     sig_org = nau8825_intlog10_dec3(sig_org);
0433     sig_cros = nau8825_intlog10_dec3(sig_cros);
0434     if (sig_org >= sig_cros)
0435         gain = (sig_org - sig_cros) * 20 + GAIN_AUGMENT;
0436     else
0437         gain = (sig_cros - sig_org) * 20 + GAIN_AUGMENT;
0438     sidetone = SIDETONE_BASE - gain * 2;
0439     sidetone /= 1000;
0440 
0441     return sidetone;
0442 }
0443 
0444 static int nau8825_xtalk_baktab_index_by_reg(unsigned int reg)
0445 {
0446     int index;
0447 
0448     for (index = 0; index < ARRAY_SIZE(nau8825_xtalk_baktab); index++)
0449         if (nau8825_xtalk_baktab[index].reg == reg)
0450             return index;
0451     return -EINVAL;
0452 }
0453 
0454 static void nau8825_xtalk_backup(struct nau8825 *nau8825)
0455 {
0456     int i;
0457 
0458     if (nau8825->xtalk_baktab_initialized)
0459         return;
0460 
0461     /* Backup some register values to backup table */
0462     for (i = 0; i < ARRAY_SIZE(nau8825_xtalk_baktab); i++)
0463         regmap_read(nau8825->regmap, nau8825_xtalk_baktab[i].reg,
0464                 &nau8825_xtalk_baktab[i].def);
0465 
0466     nau8825->xtalk_baktab_initialized = true;
0467 }
0468 
0469 static void nau8825_xtalk_restore(struct nau8825 *nau8825, bool cause_cancel)
0470 {
0471     int i, volume;
0472 
0473     if (!nau8825->xtalk_baktab_initialized)
0474         return;
0475 
0476     /* Restore register values from backup table; When the driver restores
0477      * the headphone volume in XTALK_DONE state, it needs recover to
0478      * original level gradually with 3dB per step for less pop noise.
0479      * Otherwise, the restore should do ASAP.
0480      */
0481     for (i = 0; i < ARRAY_SIZE(nau8825_xtalk_baktab); i++) {
0482         if (!cause_cancel && nau8825_xtalk_baktab[i].reg ==
0483             NAU8825_REG_HSVOL_CTRL) {
0484             /* Ramping up the volume change to reduce pop noise */
0485             volume = nau8825_xtalk_baktab[i].def &
0486                 NAU8825_HPR_VOL_MASK;
0487             nau8825_hpvol_ramp(nau8825, 0, volume, 3);
0488             continue;
0489         }
0490         regmap_write(nau8825->regmap, nau8825_xtalk_baktab[i].reg,
0491                 nau8825_xtalk_baktab[i].def);
0492     }
0493 
0494     nau8825->xtalk_baktab_initialized = false;
0495 }
0496 
0497 static void nau8825_xtalk_prepare_dac(struct nau8825 *nau8825)
0498 {
0499     /* Enable power of DAC path */
0500     regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL,
0501         NAU8825_ENABLE_DACR | NAU8825_ENABLE_DACL |
0502         NAU8825_ENABLE_ADC | NAU8825_ENABLE_ADC_CLK |
0503         NAU8825_ENABLE_DAC_CLK, NAU8825_ENABLE_DACR |
0504         NAU8825_ENABLE_DACL | NAU8825_ENABLE_ADC |
0505         NAU8825_ENABLE_ADC_CLK | NAU8825_ENABLE_DAC_CLK);
0506     /* Prevent startup click by letting charge pump to ramp up and
0507      * change bump enable
0508      */
0509     regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
0510         NAU8825_JAMNODCLOW | NAU8825_CHANRGE_PUMP_EN,
0511         NAU8825_JAMNODCLOW | NAU8825_CHANRGE_PUMP_EN);
0512     /* Enable clock sync of DAC and DAC clock */
0513     regmap_update_bits(nau8825->regmap, NAU8825_REG_RDAC,
0514         NAU8825_RDAC_EN | NAU8825_RDAC_CLK_EN |
0515         NAU8825_RDAC_FS_BCLK_ENB,
0516         NAU8825_RDAC_EN | NAU8825_RDAC_CLK_EN);
0517     /* Power up output driver with 2 stage */
0518     regmap_update_bits(nau8825->regmap, NAU8825_REG_POWER_UP_CONTROL,
0519         NAU8825_POWERUP_INTEGR_R | NAU8825_POWERUP_INTEGR_L |
0520         NAU8825_POWERUP_DRV_IN_R | NAU8825_POWERUP_DRV_IN_L,
0521         NAU8825_POWERUP_INTEGR_R | NAU8825_POWERUP_INTEGR_L |
0522         NAU8825_POWERUP_DRV_IN_R | NAU8825_POWERUP_DRV_IN_L);
0523     regmap_update_bits(nau8825->regmap, NAU8825_REG_POWER_UP_CONTROL,
0524         NAU8825_POWERUP_HP_DRV_R | NAU8825_POWERUP_HP_DRV_L,
0525         NAU8825_POWERUP_HP_DRV_R | NAU8825_POWERUP_HP_DRV_L);
0526     /* HP outputs not shouted to ground  */
0527     regmap_update_bits(nau8825->regmap, NAU8825_REG_HSD_CTRL,
0528         NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L, 0);
0529     /* Enable HP boost driver */
0530     regmap_update_bits(nau8825->regmap, NAU8825_REG_BOOST,
0531         NAU8825_HP_BOOST_DIS, NAU8825_HP_BOOST_DIS);
0532     /* Enable class G compare path to supply 1.8V or 0.9V. */
0533     regmap_update_bits(nau8825->regmap, NAU8825_REG_CLASSG_CTRL,
0534         NAU8825_CLASSG_LDAC_EN | NAU8825_CLASSG_RDAC_EN,
0535         NAU8825_CLASSG_LDAC_EN | NAU8825_CLASSG_RDAC_EN);
0536 }
0537 
0538 static void nau8825_xtalk_prepare_adc(struct nau8825 *nau8825)
0539 {
0540     /* Power up left ADC and raise 5dB than Vmid for Vref  */
0541     regmap_update_bits(nau8825->regmap, NAU8825_REG_ANALOG_ADC_2,
0542         NAU8825_POWERUP_ADCL | NAU8825_ADC_VREFSEL_MASK,
0543         NAU8825_POWERUP_ADCL | NAU8825_ADC_VREFSEL_VMID_PLUS_0_5DB);
0544 }
0545 
0546 static void nau8825_xtalk_clock(struct nau8825 *nau8825)
0547 {
0548     /* Recover FLL default value */
0549     regmap_write(nau8825->regmap, NAU8825_REG_FLL1, 0x0);
0550     regmap_write(nau8825->regmap, NAU8825_REG_FLL2, 0x3126);
0551     regmap_write(nau8825->regmap, NAU8825_REG_FLL3, 0x0008);
0552     regmap_write(nau8825->regmap, NAU8825_REG_FLL4, 0x0010);
0553     regmap_write(nau8825->regmap, NAU8825_REG_FLL5, 0x0);
0554     regmap_write(nau8825->regmap, NAU8825_REG_FLL6, 0x6000);
0555     /* Enable internal VCO clock for detection signal generated */
0556     regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
0557         NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO);
0558     regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL6, NAU8825_DCO_EN,
0559         NAU8825_DCO_EN);
0560     /* Given specific clock frequency of internal clock to
0561      * generate signal.
0562      */
0563     regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
0564         NAU8825_CLK_MCLK_SRC_MASK, 0xf);
0565     regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL1,
0566         NAU8825_FLL_RATIO_MASK, 0x10);
0567 }
0568 
0569 static void nau8825_xtalk_prepare(struct nau8825 *nau8825)
0570 {
0571     int volume, index;
0572 
0573     /* Backup those registers changed by cross talk detection */
0574     nau8825_xtalk_backup(nau8825);
0575     /* Config IIS as master to output signal by codec */
0576     regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2,
0577         NAU8825_I2S_MS_MASK | NAU8825_I2S_LRC_DIV_MASK |
0578         NAU8825_I2S_BLK_DIV_MASK, NAU8825_I2S_MS_MASTER |
0579         (0x2 << NAU8825_I2S_LRC_DIV_SFT) | 0x1);
0580     /* Ramp up headphone volume to 0dB to get better performance and
0581      * avoid pop noise in headphone.
0582      */
0583     index = nau8825_xtalk_baktab_index_by_reg(NAU8825_REG_HSVOL_CTRL);
0584     if (index != -EINVAL) {
0585         volume = nau8825_xtalk_baktab[index].def &
0586                 NAU8825_HPR_VOL_MASK;
0587         nau8825_hpvol_ramp(nau8825, volume, 0, 3);
0588     }
0589     nau8825_xtalk_clock(nau8825);
0590     nau8825_xtalk_prepare_dac(nau8825);
0591     nau8825_xtalk_prepare_adc(nau8825);
0592     /* Config channel path and digital gain */
0593     regmap_update_bits(nau8825->regmap, NAU8825_REG_DACL_CTRL,
0594         NAU8825_DACL_CH_SEL_MASK | NAU8825_DACL_CH_VOL_MASK,
0595         NAU8825_DACL_CH_SEL_L | 0xab);
0596     regmap_update_bits(nau8825->regmap, NAU8825_REG_DACR_CTRL,
0597         NAU8825_DACR_CH_SEL_MASK | NAU8825_DACR_CH_VOL_MASK,
0598         NAU8825_DACR_CH_SEL_R | 0xab);
0599     /* Config cross talk parameters and generate the 23Hz sine wave with
0600      * 1/16 full scale of signal level for impedance measurement.
0601      */
0602     regmap_update_bits(nau8825->regmap, NAU8825_REG_IMM_MODE_CTRL,
0603         NAU8825_IMM_THD_MASK | NAU8825_IMM_GEN_VOL_MASK |
0604         NAU8825_IMM_CYC_MASK | NAU8825_IMM_DAC_SRC_MASK,
0605         (0x9 << NAU8825_IMM_THD_SFT) | NAU8825_IMM_GEN_VOL_1_16th |
0606         NAU8825_IMM_CYC_8192 | NAU8825_IMM_DAC_SRC_SIN);
0607     /* RMS intrruption enable */
0608     regmap_update_bits(nau8825->regmap,
0609         NAU8825_REG_INTERRUPT_MASK, NAU8825_IRQ_RMS_EN, 0);
0610     /* Power up left and right DAC */
0611     regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
0612         NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL, 0);
0613 }
0614 
0615 static void nau8825_xtalk_clean_dac(struct nau8825 *nau8825)
0616 {
0617     /* Disable HP boost driver */
0618     regmap_update_bits(nau8825->regmap, NAU8825_REG_BOOST,
0619         NAU8825_HP_BOOST_DIS, 0);
0620     /* HP outputs shouted to ground  */
0621     regmap_update_bits(nau8825->regmap, NAU8825_REG_HSD_CTRL,
0622         NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L,
0623         NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L);
0624     /* Power down left and right DAC */
0625     regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
0626         NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL,
0627         NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL);
0628     /* Enable the TESTDAC and  disable L/R HP impedance */
0629     regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
0630         NAU8825_BIAS_HPR_IMP | NAU8825_BIAS_HPL_IMP |
0631         NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN);
0632     /* Power down output driver with 2 stage */
0633     regmap_update_bits(nau8825->regmap, NAU8825_REG_POWER_UP_CONTROL,
0634         NAU8825_POWERUP_HP_DRV_R | NAU8825_POWERUP_HP_DRV_L, 0);
0635     regmap_update_bits(nau8825->regmap, NAU8825_REG_POWER_UP_CONTROL,
0636         NAU8825_POWERUP_INTEGR_R | NAU8825_POWERUP_INTEGR_L |
0637         NAU8825_POWERUP_DRV_IN_R | NAU8825_POWERUP_DRV_IN_L, 0);
0638     /* Disable clock sync of DAC and DAC clock */
0639     regmap_update_bits(nau8825->regmap, NAU8825_REG_RDAC,
0640         NAU8825_RDAC_EN | NAU8825_RDAC_CLK_EN, 0);
0641     /* Disable charge pump ramp up function and change bump */
0642     regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
0643         NAU8825_JAMNODCLOW | NAU8825_CHANRGE_PUMP_EN, 0);
0644     /* Disable power of DAC path */
0645     regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL,
0646         NAU8825_ENABLE_DACR | NAU8825_ENABLE_DACL |
0647         NAU8825_ENABLE_ADC_CLK | NAU8825_ENABLE_DAC_CLK, 0);
0648     if (!nau8825->irq)
0649         regmap_update_bits(nau8825->regmap,
0650             NAU8825_REG_ENA_CTRL, NAU8825_ENABLE_ADC, 0);
0651 }
0652 
0653 static void nau8825_xtalk_clean_adc(struct nau8825 *nau8825)
0654 {
0655     /* Power down left ADC and restore voltage to Vmid */
0656     regmap_update_bits(nau8825->regmap, NAU8825_REG_ANALOG_ADC_2,
0657         NAU8825_POWERUP_ADCL | NAU8825_ADC_VREFSEL_MASK, 0);
0658 }
0659 
0660 static void nau8825_xtalk_clean(struct nau8825 *nau8825, bool cause_cancel)
0661 {
0662     /* Enable internal VCO needed for interruptions */
0663     nau8825_configure_sysclk(nau8825, NAU8825_CLK_INTERNAL, 0);
0664     nau8825_xtalk_clean_dac(nau8825);
0665     nau8825_xtalk_clean_adc(nau8825);
0666     /* Clear cross talk parameters and disable */
0667     regmap_write(nau8825->regmap, NAU8825_REG_IMM_MODE_CTRL, 0);
0668     /* RMS intrruption disable */
0669     regmap_update_bits(nau8825->regmap, NAU8825_REG_INTERRUPT_MASK,
0670         NAU8825_IRQ_RMS_EN, NAU8825_IRQ_RMS_EN);
0671     /* Recover default value for IIS */
0672     regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2,
0673         NAU8825_I2S_MS_MASK | NAU8825_I2S_LRC_DIV_MASK |
0674         NAU8825_I2S_BLK_DIV_MASK, NAU8825_I2S_MS_SLAVE);
0675     /* Restore value of specific register for cross talk */
0676     nau8825_xtalk_restore(nau8825, cause_cancel);
0677 }
0678 
0679 static void nau8825_xtalk_imm_start(struct nau8825 *nau8825, int vol)
0680 {
0681     /* Apply ADC volume for better cross talk performance */
0682     regmap_update_bits(nau8825->regmap, NAU8825_REG_ADC_DGAIN_CTRL,
0683                 NAU8825_ADC_DIG_VOL_MASK, vol);
0684     /* Disables JKTIP(HPL) DAC channel for right to left measurement.
0685      * Do it before sending signal in order to erase pop noise.
0686      */
0687     regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
0688         NAU8825_BIAS_TESTDACR_EN | NAU8825_BIAS_TESTDACL_EN,
0689         NAU8825_BIAS_TESTDACL_EN);
0690     switch (nau8825->xtalk_state) {
0691     case NAU8825_XTALK_HPR_R2L:
0692         /* Enable right headphone impedance */
0693         regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
0694             NAU8825_BIAS_HPR_IMP | NAU8825_BIAS_HPL_IMP,
0695             NAU8825_BIAS_HPR_IMP);
0696         break;
0697     case NAU8825_XTALK_HPL_R2L:
0698         /* Enable left headphone impedance */
0699         regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
0700             NAU8825_BIAS_HPR_IMP | NAU8825_BIAS_HPL_IMP,
0701             NAU8825_BIAS_HPL_IMP);
0702         break;
0703     default:
0704         break;
0705     }
0706     msleep(100);
0707     /* Impedance measurement mode enable */
0708     regmap_update_bits(nau8825->regmap, NAU8825_REG_IMM_MODE_CTRL,
0709                 NAU8825_IMM_EN, NAU8825_IMM_EN);
0710 }
0711 
0712 static void nau8825_xtalk_imm_stop(struct nau8825 *nau8825)
0713 {
0714     /* Impedance measurement mode disable */
0715     regmap_update_bits(nau8825->regmap,
0716         NAU8825_REG_IMM_MODE_CTRL, NAU8825_IMM_EN, 0);
0717 }
0718 
0719 /* The cross talk measurement function can reduce cross talk across the
0720  * JKTIP(HPL) and JKR1(HPR) outputs which measures the cross talk signal
0721  * level to determine what cross talk reduction gain is. This system works by
0722  * sending a 23Hz -24dBV sine wave into the headset output DAC and through
0723  * the PGA. The output of the PGA is then connected to an internal current
0724  * sense which measures the attenuated 23Hz signal and passing the output to
0725  * an ADC which converts the measurement to a binary code. With two separated
0726  * measurement, one for JKR1(HPR) and the other JKTIP(HPL), measurement data
0727  * can be separated read in IMM_RMS_L for HSR and HSL after each measurement.
0728  * Thus, the measurement function has four states to complete whole sequence.
0729  * 1. Prepare state : Prepare the resource for detection and transfer to HPR
0730  *     IMM stat to make JKR1(HPR) impedance measure.
0731  * 2. HPR IMM state : Read out orignal signal level of JKR1(HPR) and transfer
0732  *     to HPL IMM state to make JKTIP(HPL) impedance measure.
0733  * 3. HPL IMM state : Read out cross talk signal level of JKTIP(HPL) and
0734  *     transfer to IMM state to determine suppression sidetone gain.
0735  * 4. IMM state : Computes cross talk suppression sidetone gain with orignal
0736  *     and cross talk signal level. Apply this gain and then restore codec
0737  *     configuration. Then transfer to Done state for ending.
0738  */
0739 static void nau8825_xtalk_measure(struct nau8825 *nau8825)
0740 {
0741     u32 sidetone;
0742 
0743     switch (nau8825->xtalk_state) {
0744     case NAU8825_XTALK_PREPARE:
0745         /* In prepare state, set up clock, intrruption, DAC path, ADC
0746          * path and cross talk detection parameters for preparation.
0747          */
0748         nau8825_xtalk_prepare(nau8825);
0749         msleep(280);
0750         /* Trigger right headphone impedance detection */
0751         nau8825->xtalk_state = NAU8825_XTALK_HPR_R2L;
0752         nau8825_xtalk_imm_start(nau8825, 0x00d2);
0753         break;
0754     case NAU8825_XTALK_HPR_R2L:
0755         /* In right headphone IMM state, read out right headphone
0756          * impedance measure result, and then start up left side.
0757          */
0758         regmap_read(nau8825->regmap, NAU8825_REG_IMM_RMS_L,
0759             &nau8825->imp_rms[NAU8825_XTALK_HPR_R2L]);
0760         dev_dbg(nau8825->dev, "HPR_R2L imm: %x\n",
0761             nau8825->imp_rms[NAU8825_XTALK_HPR_R2L]);
0762         /* Disable then re-enable IMM mode to update */
0763         nau8825_xtalk_imm_stop(nau8825);
0764         /* Trigger left headphone impedance detection */
0765         nau8825->xtalk_state = NAU8825_XTALK_HPL_R2L;
0766         nau8825_xtalk_imm_start(nau8825, 0x00ff);
0767         break;
0768     case NAU8825_XTALK_HPL_R2L:
0769         /* In left headphone IMM state, read out left headphone
0770          * impedance measure result, and delay some time to wait
0771          * detection sine wave output finish. Then, we can calculate
0772          * the cross talk suppresstion side tone according to the L/R
0773          * headphone imedance.
0774          */
0775         regmap_read(nau8825->regmap, NAU8825_REG_IMM_RMS_L,
0776             &nau8825->imp_rms[NAU8825_XTALK_HPL_R2L]);
0777         dev_dbg(nau8825->dev, "HPL_R2L imm: %x\n",
0778             nau8825->imp_rms[NAU8825_XTALK_HPL_R2L]);
0779         nau8825_xtalk_imm_stop(nau8825);
0780         msleep(150);
0781         nau8825->xtalk_state = NAU8825_XTALK_IMM;
0782         break;
0783     case NAU8825_XTALK_IMM:
0784         /* In impedance measure state, the orignal and cross talk
0785          * signal level vlues are ready. The side tone gain is deter-
0786          * mined with these signal level. After all, restore codec
0787          * configuration.
0788          */
0789         sidetone = nau8825_xtalk_sidetone(
0790             nau8825->imp_rms[NAU8825_XTALK_HPR_R2L],
0791             nau8825->imp_rms[NAU8825_XTALK_HPL_R2L]);
0792         dev_dbg(nau8825->dev, "cross talk sidetone: %x\n", sidetone);
0793         regmap_write(nau8825->regmap, NAU8825_REG_DAC_DGAIN_CTRL,
0794                     (sidetone << 8) | sidetone);
0795         nau8825_xtalk_clean(nau8825, false);
0796         nau8825->xtalk_state = NAU8825_XTALK_DONE;
0797         break;
0798     default:
0799         break;
0800     }
0801 }
0802 
0803 static void nau8825_xtalk_work(struct work_struct *work)
0804 {
0805     struct nau8825 *nau8825 = container_of(
0806         work, struct nau8825, xtalk_work);
0807 
0808     nau8825_xtalk_measure(nau8825);
0809     /* To determine the cross talk side tone gain when reach
0810      * the impedance measure state.
0811      */
0812     if (nau8825->xtalk_state == NAU8825_XTALK_IMM)
0813         nau8825_xtalk_measure(nau8825);
0814 
0815     /* Delay jack report until cross talk detection process
0816      * completed. It can avoid application to do playback
0817      * preparation before cross talk detection is still working.
0818      * Meanwhile, the protection of the cross talk detection
0819      * is released.
0820      */
0821     if (nau8825->xtalk_state == NAU8825_XTALK_DONE) {
0822         snd_soc_jack_report(nau8825->jack, nau8825->xtalk_event,
0823                 nau8825->xtalk_event_mask);
0824         nau8825_sema_release(nau8825);
0825         nau8825->xtalk_protect = false;
0826     }
0827 }
0828 
0829 static void nau8825_xtalk_cancel(struct nau8825 *nau8825)
0830 {
0831     /* If the crosstalk is eanbled and the process is on going,
0832      * the driver forces to cancel the crosstalk task and
0833      * restores the configuration to original status.
0834      */
0835     if (nau8825->xtalk_enable && nau8825->xtalk_state !=
0836         NAU8825_XTALK_DONE) {
0837         cancel_work_sync(&nau8825->xtalk_work);
0838         nau8825_xtalk_clean(nau8825, true);
0839     }
0840     /* Reset parameters for cross talk suppression function */
0841     nau8825_sema_reset(nau8825);
0842     nau8825->xtalk_state = NAU8825_XTALK_DONE;
0843     nau8825->xtalk_protect = false;
0844 }
0845 
0846 static bool nau8825_readable_reg(struct device *dev, unsigned int reg)
0847 {
0848     switch (reg) {
0849     case NAU8825_REG_ENA_CTRL ... NAU8825_REG_FLL_VCO_RSV:
0850     case NAU8825_REG_HSD_CTRL ... NAU8825_REG_JACK_DET_CTRL:
0851     case NAU8825_REG_INTERRUPT_MASK ... NAU8825_REG_KEYDET_CTRL:
0852     case NAU8825_REG_VDET_THRESHOLD_1 ... NAU8825_REG_DACR_CTRL:
0853     case NAU8825_REG_ADC_DRC_KNEE_IP12 ... NAU8825_REG_ADC_DRC_ATKDCY:
0854     case NAU8825_REG_DAC_DRC_KNEE_IP12 ... NAU8825_REG_DAC_DRC_ATKDCY:
0855     case NAU8825_REG_IMM_MODE_CTRL ... NAU8825_REG_IMM_RMS_R:
0856     case NAU8825_REG_CLASSG_CTRL ... NAU8825_REG_OPT_EFUSE_CTRL:
0857     case NAU8825_REG_MISC_CTRL:
0858     case NAU8825_REG_I2C_DEVICE_ID ... NAU8825_REG_SARDOUT_RAM_STATUS:
0859     case NAU8825_REG_BIAS_ADJ:
0860     case NAU8825_REG_TRIM_SETTINGS ... NAU8825_REG_ANALOG_CONTROL_2:
0861     case NAU8825_REG_ANALOG_ADC_1 ... NAU8825_REG_MIC_BIAS:
0862     case NAU8825_REG_BOOST ... NAU8825_REG_FEPGA:
0863     case NAU8825_REG_POWER_UP_CONTROL ... NAU8825_REG_GENERAL_STATUS:
0864         return true;
0865     default:
0866         return false;
0867     }
0868 
0869 }
0870 
0871 static bool nau8825_writeable_reg(struct device *dev, unsigned int reg)
0872 {
0873     switch (reg) {
0874     case NAU8825_REG_RESET ... NAU8825_REG_FLL_VCO_RSV:
0875     case NAU8825_REG_HSD_CTRL ... NAU8825_REG_JACK_DET_CTRL:
0876     case NAU8825_REG_INTERRUPT_MASK:
0877     case NAU8825_REG_INT_CLR_KEY_STATUS ... NAU8825_REG_KEYDET_CTRL:
0878     case NAU8825_REG_VDET_THRESHOLD_1 ... NAU8825_REG_DACR_CTRL:
0879     case NAU8825_REG_ADC_DRC_KNEE_IP12 ... NAU8825_REG_ADC_DRC_ATKDCY:
0880     case NAU8825_REG_DAC_DRC_KNEE_IP12 ... NAU8825_REG_DAC_DRC_ATKDCY:
0881     case NAU8825_REG_IMM_MODE_CTRL:
0882     case NAU8825_REG_CLASSG_CTRL ... NAU8825_REG_OPT_EFUSE_CTRL:
0883     case NAU8825_REG_MISC_CTRL:
0884     case NAU8825_REG_BIAS_ADJ:
0885     case NAU8825_REG_TRIM_SETTINGS ... NAU8825_REG_ANALOG_CONTROL_2:
0886     case NAU8825_REG_ANALOG_ADC_1 ... NAU8825_REG_MIC_BIAS:
0887     case NAU8825_REG_BOOST ... NAU8825_REG_FEPGA:
0888     case NAU8825_REG_POWER_UP_CONTROL ... NAU8825_REG_CHARGE_PUMP:
0889         return true;
0890     default:
0891         return false;
0892     }
0893 }
0894 
0895 static bool nau8825_volatile_reg(struct device *dev, unsigned int reg)
0896 {
0897     switch (reg) {
0898     case NAU8825_REG_RESET:
0899     case NAU8825_REG_IRQ_STATUS:
0900     case NAU8825_REG_INT_CLR_KEY_STATUS:
0901     case NAU8825_REG_IMM_RMS_L:
0902     case NAU8825_REG_IMM_RMS_R:
0903     case NAU8825_REG_I2C_DEVICE_ID:
0904     case NAU8825_REG_SARDOUT_RAM_STATUS:
0905     case NAU8825_REG_CHARGE_PUMP_INPUT_READ:
0906     case NAU8825_REG_GENERAL_STATUS:
0907     case NAU8825_REG_BIQ_CTRL ... NAU8825_REG_BIQ_COF10:
0908         return true;
0909     default:
0910         return false;
0911     }
0912 }
0913 
0914 static int nau8825_adc_event(struct snd_soc_dapm_widget *w,
0915         struct snd_kcontrol *kcontrol, int event)
0916 {
0917     struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
0918     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
0919 
0920     switch (event) {
0921     case SND_SOC_DAPM_POST_PMU:
0922         msleep(125);
0923         regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL,
0924             NAU8825_ENABLE_ADC, NAU8825_ENABLE_ADC);
0925         break;
0926     case SND_SOC_DAPM_POST_PMD:
0927         if (!nau8825->irq)
0928             regmap_update_bits(nau8825->regmap,
0929                 NAU8825_REG_ENA_CTRL, NAU8825_ENABLE_ADC, 0);
0930         break;
0931     default:
0932         return -EINVAL;
0933     }
0934 
0935     return 0;
0936 }
0937 
0938 static int nau8825_pump_event(struct snd_soc_dapm_widget *w,
0939     struct snd_kcontrol *kcontrol, int event)
0940 {
0941     struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
0942     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
0943 
0944     switch (event) {
0945     case SND_SOC_DAPM_POST_PMU:
0946         /* Prevent startup click by letting charge pump to ramp up */
0947         msleep(10);
0948         regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
0949             NAU8825_JAMNODCLOW, NAU8825_JAMNODCLOW);
0950         break;
0951     case SND_SOC_DAPM_PRE_PMD:
0952         regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
0953             NAU8825_JAMNODCLOW, 0);
0954         break;
0955     default:
0956         return -EINVAL;
0957     }
0958 
0959     return 0;
0960 }
0961 
0962 static int nau8825_output_dac_event(struct snd_soc_dapm_widget *w,
0963     struct snd_kcontrol *kcontrol, int event)
0964 {
0965     struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
0966     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
0967 
0968     switch (event) {
0969     case SND_SOC_DAPM_PRE_PMU:
0970         /* Disables the TESTDAC to let DAC signal pass through. */
0971         regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
0972             NAU8825_BIAS_TESTDAC_EN, 0);
0973         break;
0974     case SND_SOC_DAPM_POST_PMD:
0975         regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
0976             NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN);
0977         break;
0978     default:
0979         return -EINVAL;
0980     }
0981 
0982     return 0;
0983 }
0984 
0985 static int system_clock_control(struct snd_soc_dapm_widget *w,
0986                 struct snd_kcontrol *k, int  event)
0987 {
0988     struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
0989     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
0990     struct regmap *regmap = nau8825->regmap;
0991 
0992     if (SND_SOC_DAPM_EVENT_OFF(event)) {
0993         dev_dbg(nau8825->dev, "system clock control : POWER OFF\n");
0994         /* Set clock source to disable or internal clock before the
0995          * playback or capture end. Codec needs clock for Jack
0996          * detection and button press if jack inserted; otherwise,
0997          * the clock should be closed.
0998          */
0999         if (nau8825_is_jack_inserted(regmap)) {
1000             nau8825_configure_sysclk(nau8825,
1001                          NAU8825_CLK_INTERNAL, 0);
1002         } else {
1003             nau8825_configure_sysclk(nau8825, NAU8825_CLK_DIS, 0);
1004         }
1005     }
1006 
1007     return 0;
1008 }
1009 
1010 static int nau8825_biq_coeff_get(struct snd_kcontrol *kcontrol,
1011                      struct snd_ctl_elem_value *ucontrol)
1012 {
1013     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1014     struct soc_bytes_ext *params = (void *)kcontrol->private_value;
1015 
1016     if (!component->regmap)
1017         return -EINVAL;
1018 
1019     regmap_raw_read(component->regmap, NAU8825_REG_BIQ_COF1,
1020         ucontrol->value.bytes.data, params->max);
1021     return 0;
1022 }
1023 
1024 static int nau8825_biq_coeff_put(struct snd_kcontrol *kcontrol,
1025                      struct snd_ctl_elem_value *ucontrol)
1026 {
1027     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1028     struct soc_bytes_ext *params = (void *)kcontrol->private_value;
1029     void *data;
1030 
1031     if (!component->regmap)
1032         return -EINVAL;
1033 
1034     data = kmemdup(ucontrol->value.bytes.data,
1035         params->max, GFP_KERNEL | GFP_DMA);
1036     if (!data)
1037         return -ENOMEM;
1038 
1039     regmap_update_bits(component->regmap, NAU8825_REG_BIQ_CTRL,
1040         NAU8825_BIQ_WRT_EN, 0);
1041     regmap_raw_write(component->regmap, NAU8825_REG_BIQ_COF1,
1042         data, params->max);
1043     regmap_update_bits(component->regmap, NAU8825_REG_BIQ_CTRL,
1044         NAU8825_BIQ_WRT_EN, NAU8825_BIQ_WRT_EN);
1045 
1046     kfree(data);
1047     return 0;
1048 }
1049 
1050 static const char * const nau8825_biq_path[] = {
1051     "ADC", "DAC"
1052 };
1053 
1054 static const struct soc_enum nau8825_biq_path_enum =
1055     SOC_ENUM_SINGLE(NAU8825_REG_BIQ_CTRL, NAU8825_BIQ_PATH_SFT,
1056         ARRAY_SIZE(nau8825_biq_path), nau8825_biq_path);
1057 
1058 static const char * const nau8825_adc_decimation[] = {
1059     "32", "64", "128", "256"
1060 };
1061 
1062 static const struct soc_enum nau8825_adc_decimation_enum =
1063     SOC_ENUM_SINGLE(NAU8825_REG_ADC_RATE, NAU8825_ADC_SYNC_DOWN_SFT,
1064         ARRAY_SIZE(nau8825_adc_decimation), nau8825_adc_decimation);
1065 
1066 static const char * const nau8825_dac_oversampl[] = {
1067     "64", "256", "128", "", "32"
1068 };
1069 
1070 static const struct soc_enum nau8825_dac_oversampl_enum =
1071     SOC_ENUM_SINGLE(NAU8825_REG_DAC_CTRL1, NAU8825_DAC_OVERSAMPLE_SFT,
1072         ARRAY_SIZE(nau8825_dac_oversampl), nau8825_dac_oversampl);
1073 
1074 static const DECLARE_TLV_DB_MINMAX_MUTE(adc_vol_tlv, -10300, 2400);
1075 static const DECLARE_TLV_DB_MINMAX_MUTE(sidetone_vol_tlv, -4200, 0);
1076 static const DECLARE_TLV_DB_MINMAX(dac_vol_tlv, -5400, 0);
1077 static const DECLARE_TLV_DB_MINMAX(fepga_gain_tlv, -100, 3600);
1078 static const DECLARE_TLV_DB_MINMAX_MUTE(crosstalk_vol_tlv, -9600, 2400);
1079 
1080 static const struct snd_kcontrol_new nau8825_controls[] = {
1081     SOC_SINGLE_TLV("Mic Volume", NAU8825_REG_ADC_DGAIN_CTRL,
1082         0, 0xff, 0, adc_vol_tlv),
1083     SOC_DOUBLE_TLV("Headphone Bypass Volume", NAU8825_REG_ADC_DGAIN_CTRL,
1084         12, 8, 0x0f, 0, sidetone_vol_tlv),
1085     SOC_DOUBLE_TLV("Headphone Volume", NAU8825_REG_HSVOL_CTRL,
1086         6, 0, 0x3f, 1, dac_vol_tlv),
1087     SOC_SINGLE_TLV("Frontend PGA Volume", NAU8825_REG_POWER_UP_CONTROL,
1088         8, 37, 0, fepga_gain_tlv),
1089     SOC_DOUBLE_TLV("Headphone Crosstalk Volume", NAU8825_REG_DAC_DGAIN_CTRL,
1090         0, 8, 0xff, 0, crosstalk_vol_tlv),
1091 
1092     SOC_ENUM("ADC Decimation Rate", nau8825_adc_decimation_enum),
1093     SOC_ENUM("DAC Oversampling Rate", nau8825_dac_oversampl_enum),
1094     /* programmable biquad filter */
1095     SOC_ENUM("BIQ Path Select", nau8825_biq_path_enum),
1096     SND_SOC_BYTES_EXT("BIQ Coefficients", 20,
1097           nau8825_biq_coeff_get, nau8825_biq_coeff_put),
1098 };
1099 
1100 /* DAC Mux 0x33[9] and 0x34[9] */
1101 static const char * const nau8825_dac_src[] = {
1102     "DACL", "DACR",
1103 };
1104 
1105 static SOC_ENUM_SINGLE_DECL(
1106     nau8825_dacl_enum, NAU8825_REG_DACL_CTRL,
1107     NAU8825_DACL_CH_SEL_SFT, nau8825_dac_src);
1108 
1109 static SOC_ENUM_SINGLE_DECL(
1110     nau8825_dacr_enum, NAU8825_REG_DACR_CTRL,
1111     NAU8825_DACR_CH_SEL_SFT, nau8825_dac_src);
1112 
1113 static const struct snd_kcontrol_new nau8825_dacl_mux =
1114     SOC_DAPM_ENUM("DACL Source", nau8825_dacl_enum);
1115 
1116 static const struct snd_kcontrol_new nau8825_dacr_mux =
1117     SOC_DAPM_ENUM("DACR Source", nau8825_dacr_enum);
1118 
1119 
1120 static const struct snd_soc_dapm_widget nau8825_dapm_widgets[] = {
1121     SND_SOC_DAPM_AIF_OUT("AIFTX", "Capture", 0, NAU8825_REG_I2S_PCM_CTRL2,
1122         15, 1),
1123     SND_SOC_DAPM_AIF_IN("AIFRX", "Playback", 0, SND_SOC_NOPM, 0, 0),
1124     SND_SOC_DAPM_SUPPLY("System Clock", SND_SOC_NOPM, 0, 0,
1125                 system_clock_control, SND_SOC_DAPM_POST_PMD),
1126 
1127     SND_SOC_DAPM_INPUT("MIC"),
1128     SND_SOC_DAPM_MICBIAS("MICBIAS", NAU8825_REG_MIC_BIAS, 8, 0),
1129 
1130     SND_SOC_DAPM_PGA("Frontend PGA", NAU8825_REG_POWER_UP_CONTROL, 14, 0,
1131         NULL, 0),
1132 
1133     SND_SOC_DAPM_ADC_E("ADC", NULL, SND_SOC_NOPM, 0, 0,
1134         nau8825_adc_event, SND_SOC_DAPM_POST_PMU |
1135         SND_SOC_DAPM_POST_PMD),
1136     SND_SOC_DAPM_SUPPLY("ADC Clock", NAU8825_REG_ENA_CTRL, 7, 0, NULL, 0),
1137     SND_SOC_DAPM_SUPPLY("ADC Power", NAU8825_REG_ANALOG_ADC_2, 6, 0, NULL,
1138         0),
1139 
1140     /* ADC for button press detection. A dapm supply widget is used to
1141      * prevent dapm_power_widgets keeping the codec at SND_SOC_BIAS_ON
1142      * during suspend.
1143      */
1144     SND_SOC_DAPM_SUPPLY("SAR", NAU8825_REG_SAR_CTRL,
1145         NAU8825_SAR_ADC_EN_SFT, 0, NULL, 0),
1146 
1147     SND_SOC_DAPM_PGA_S("ADACL", 2, NAU8825_REG_RDAC, 12, 0, NULL, 0),
1148     SND_SOC_DAPM_PGA_S("ADACR", 2, NAU8825_REG_RDAC, 13, 0, NULL, 0),
1149     SND_SOC_DAPM_PGA_S("ADACL Clock", 3, NAU8825_REG_RDAC, 8, 0, NULL, 0),
1150     SND_SOC_DAPM_PGA_S("ADACR Clock", 3, NAU8825_REG_RDAC, 9, 0, NULL, 0),
1151 
1152     SND_SOC_DAPM_DAC("DDACR", NULL, NAU8825_REG_ENA_CTRL,
1153         NAU8825_ENABLE_DACR_SFT, 0),
1154     SND_SOC_DAPM_DAC("DDACL", NULL, NAU8825_REG_ENA_CTRL,
1155         NAU8825_ENABLE_DACL_SFT, 0),
1156     SND_SOC_DAPM_SUPPLY("DDAC Clock", NAU8825_REG_ENA_CTRL, 6, 0, NULL, 0),
1157 
1158     SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &nau8825_dacl_mux),
1159     SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &nau8825_dacr_mux),
1160 
1161     SND_SOC_DAPM_PGA_S("HP amp L", 0,
1162         NAU8825_REG_CLASSG_CTRL, 1, 0, NULL, 0),
1163     SND_SOC_DAPM_PGA_S("HP amp R", 0,
1164         NAU8825_REG_CLASSG_CTRL, 2, 0, NULL, 0),
1165 
1166     SND_SOC_DAPM_PGA_S("Charge Pump", 1, NAU8825_REG_CHARGE_PUMP, 5, 0,
1167         nau8825_pump_event, SND_SOC_DAPM_POST_PMU |
1168         SND_SOC_DAPM_PRE_PMD),
1169 
1170     SND_SOC_DAPM_PGA_S("Output Driver R Stage 1", 4,
1171         NAU8825_REG_POWER_UP_CONTROL, 5, 0, NULL, 0),
1172     SND_SOC_DAPM_PGA_S("Output Driver L Stage 1", 4,
1173         NAU8825_REG_POWER_UP_CONTROL, 4, 0, NULL, 0),
1174     SND_SOC_DAPM_PGA_S("Output Driver R Stage 2", 5,
1175         NAU8825_REG_POWER_UP_CONTROL, 3, 0, NULL, 0),
1176     SND_SOC_DAPM_PGA_S("Output Driver L Stage 2", 5,
1177         NAU8825_REG_POWER_UP_CONTROL, 2, 0, NULL, 0),
1178     SND_SOC_DAPM_PGA_S("Output Driver R Stage 3", 6,
1179         NAU8825_REG_POWER_UP_CONTROL, 1, 0, NULL, 0),
1180     SND_SOC_DAPM_PGA_S("Output Driver L Stage 3", 6,
1181         NAU8825_REG_POWER_UP_CONTROL, 0, 0, NULL, 0),
1182 
1183     SND_SOC_DAPM_PGA_S("Output DACL", 7,
1184         NAU8825_REG_CHARGE_PUMP, 8, 1, nau8825_output_dac_event,
1185         SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1186     SND_SOC_DAPM_PGA_S("Output DACR", 7,
1187         NAU8825_REG_CHARGE_PUMP, 9, 1, nau8825_output_dac_event,
1188         SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1189 
1190     /* HPOL/R are ungrounded by disabling 16 Ohm pull-downs on playback */
1191     SND_SOC_DAPM_PGA_S("HPOL Pulldown", 8,
1192         NAU8825_REG_HSD_CTRL, 0, 1, NULL, 0),
1193     SND_SOC_DAPM_PGA_S("HPOR Pulldown", 8,
1194         NAU8825_REG_HSD_CTRL, 1, 1, NULL, 0),
1195 
1196     /* High current HPOL/R boost driver */
1197     SND_SOC_DAPM_PGA_S("HP Boost Driver", 9,
1198         NAU8825_REG_BOOST, 9, 1, NULL, 0),
1199 
1200     /* Class G operation control*/
1201     SND_SOC_DAPM_PGA_S("Class G", 10,
1202         NAU8825_REG_CLASSG_CTRL, 0, 0, NULL, 0),
1203 
1204     SND_SOC_DAPM_OUTPUT("HPOL"),
1205     SND_SOC_DAPM_OUTPUT("HPOR"),
1206 };
1207 
1208 static const struct snd_soc_dapm_route nau8825_dapm_routes[] = {
1209     {"Frontend PGA", NULL, "MIC"},
1210     {"ADC", NULL, "Frontend PGA"},
1211     {"ADC", NULL, "ADC Clock"},
1212     {"ADC", NULL, "ADC Power"},
1213     {"AIFTX", NULL, "ADC"},
1214     {"AIFTX", NULL, "System Clock"},
1215 
1216     {"AIFRX", NULL, "System Clock"},
1217     {"DDACL", NULL, "AIFRX"},
1218     {"DDACR", NULL, "AIFRX"},
1219     {"DDACL", NULL, "DDAC Clock"},
1220     {"DDACR", NULL, "DDAC Clock"},
1221     {"DACL Mux", "DACL", "DDACL"},
1222     {"DACL Mux", "DACR", "DDACR"},
1223     {"DACR Mux", "DACL", "DDACL"},
1224     {"DACR Mux", "DACR", "DDACR"},
1225     {"HP amp L", NULL, "DACL Mux"},
1226     {"HP amp R", NULL, "DACR Mux"},
1227     {"Charge Pump", NULL, "HP amp L"},
1228     {"Charge Pump", NULL, "HP amp R"},
1229     {"ADACL", NULL, "Charge Pump"},
1230     {"ADACR", NULL, "Charge Pump"},
1231     {"ADACL Clock", NULL, "ADACL"},
1232     {"ADACR Clock", NULL, "ADACR"},
1233     {"Output Driver L Stage 1", NULL, "ADACL Clock"},
1234     {"Output Driver R Stage 1", NULL, "ADACR Clock"},
1235     {"Output Driver L Stage 2", NULL, "Output Driver L Stage 1"},
1236     {"Output Driver R Stage 2", NULL, "Output Driver R Stage 1"},
1237     {"Output Driver L Stage 3", NULL, "Output Driver L Stage 2"},
1238     {"Output Driver R Stage 3", NULL, "Output Driver R Stage 2"},
1239     {"Output DACL", NULL, "Output Driver L Stage 3"},
1240     {"Output DACR", NULL, "Output Driver R Stage 3"},
1241     {"HPOL Pulldown", NULL, "Output DACL"},
1242     {"HPOR Pulldown", NULL, "Output DACR"},
1243     {"HP Boost Driver", NULL, "HPOL Pulldown"},
1244     {"HP Boost Driver", NULL, "HPOR Pulldown"},
1245     {"Class G", NULL, "HP Boost Driver"},
1246     {"HPOL", NULL, "Class G"},
1247     {"HPOR", NULL, "Class G"},
1248 };
1249 
1250 static const struct nau8825_osr_attr *
1251 nau8825_get_osr(struct nau8825 *nau8825, int stream)
1252 {
1253     unsigned int osr;
1254 
1255     if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1256         regmap_read(nau8825->regmap,
1257                 NAU8825_REG_DAC_CTRL1, &osr);
1258         osr &= NAU8825_DAC_OVERSAMPLE_MASK;
1259         if (osr >= ARRAY_SIZE(osr_dac_sel))
1260             return NULL;
1261         return &osr_dac_sel[osr];
1262     } else {
1263         regmap_read(nau8825->regmap,
1264                 NAU8825_REG_ADC_RATE, &osr);
1265         osr &= NAU8825_ADC_SYNC_DOWN_MASK;
1266         if (osr >= ARRAY_SIZE(osr_adc_sel))
1267             return NULL;
1268         return &osr_adc_sel[osr];
1269     }
1270 }
1271 
1272 static int nau8825_dai_startup(struct snd_pcm_substream *substream,
1273                    struct snd_soc_dai *dai)
1274 {
1275     struct snd_soc_component *component = dai->component;
1276     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
1277     const struct nau8825_osr_attr *osr;
1278 
1279     osr = nau8825_get_osr(nau8825, substream->stream);
1280     if (!osr || !osr->osr)
1281         return -EINVAL;
1282 
1283     return snd_pcm_hw_constraint_minmax(substream->runtime,
1284                         SNDRV_PCM_HW_PARAM_RATE,
1285                         0, CLK_DA_AD_MAX / osr->osr);
1286 }
1287 
1288 static int nau8825_hw_params(struct snd_pcm_substream *substream,
1289                 struct snd_pcm_hw_params *params,
1290                 struct snd_soc_dai *dai)
1291 {
1292     struct snd_soc_component *component = dai->component;
1293     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
1294     unsigned int val_len = 0, ctrl_val, bclk_fs, bclk_div;
1295     const struct nau8825_osr_attr *osr;
1296     int err = -EINVAL;
1297 
1298     nau8825_sema_acquire(nau8825, 3 * HZ);
1299 
1300     /* CLK_DAC or CLK_ADC = OSR * FS
1301      * DAC or ADC clock frequency is defined as Over Sampling Rate (OSR)
1302      * multiplied by the audio sample rate (Fs). Note that the OSR and Fs
1303      * values must be selected such that the maximum frequency is less
1304      * than 6.144 MHz.
1305      */
1306     osr = nau8825_get_osr(nau8825, substream->stream);
1307     if (!osr || !osr->osr)
1308         goto error;
1309     if (params_rate(params) * osr->osr > CLK_DA_AD_MAX)
1310         goto error;
1311     if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1312         regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
1313             NAU8825_CLK_DAC_SRC_MASK,
1314             osr->clk_src << NAU8825_CLK_DAC_SRC_SFT);
1315     else
1316         regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
1317             NAU8825_CLK_ADC_SRC_MASK,
1318             osr->clk_src << NAU8825_CLK_ADC_SRC_SFT);
1319 
1320     /* make BCLK and LRC divde configuration if the codec as master. */
1321     regmap_read(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2, &ctrl_val);
1322     if (ctrl_val & NAU8825_I2S_MS_MASTER) {
1323         /* get the bclk and fs ratio */
1324         bclk_fs = snd_soc_params_to_bclk(params) / params_rate(params);
1325         if (bclk_fs <= 32)
1326             bclk_div = 2;
1327         else if (bclk_fs <= 64)
1328             bclk_div = 1;
1329         else if (bclk_fs <= 128)
1330             bclk_div = 0;
1331         else
1332             goto error;
1333         regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2,
1334             NAU8825_I2S_LRC_DIV_MASK | NAU8825_I2S_BLK_DIV_MASK,
1335             ((bclk_div + 1) << NAU8825_I2S_LRC_DIV_SFT) | bclk_div);
1336     }
1337 
1338     switch (params_width(params)) {
1339     case 16:
1340         val_len |= NAU8825_I2S_DL_16;
1341         break;
1342     case 20:
1343         val_len |= NAU8825_I2S_DL_20;
1344         break;
1345     case 24:
1346         val_len |= NAU8825_I2S_DL_24;
1347         break;
1348     case 32:
1349         val_len |= NAU8825_I2S_DL_32;
1350         break;
1351     default:
1352         goto error;
1353     }
1354 
1355     regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1,
1356         NAU8825_I2S_DL_MASK, val_len);
1357     err = 0;
1358 
1359  error:
1360     /* Release the semaphore. */
1361     nau8825_sema_release(nau8825);
1362 
1363     return err;
1364 }
1365 
1366 static int nau8825_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
1367 {
1368     struct snd_soc_component *component = codec_dai->component;
1369     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
1370     unsigned int ctrl1_val = 0, ctrl2_val = 0;
1371 
1372     switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1373     case SND_SOC_DAIFMT_CBM_CFM:
1374         ctrl2_val |= NAU8825_I2S_MS_MASTER;
1375         break;
1376     case SND_SOC_DAIFMT_CBS_CFS:
1377         break;
1378     default:
1379         return -EINVAL;
1380     }
1381 
1382     switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1383     case SND_SOC_DAIFMT_NB_NF:
1384         break;
1385     case SND_SOC_DAIFMT_IB_NF:
1386         ctrl1_val |= NAU8825_I2S_BP_INV;
1387         break;
1388     default:
1389         return -EINVAL;
1390     }
1391 
1392     switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1393     case SND_SOC_DAIFMT_I2S:
1394         ctrl1_val |= NAU8825_I2S_DF_I2S;
1395         break;
1396     case SND_SOC_DAIFMT_LEFT_J:
1397         ctrl1_val |= NAU8825_I2S_DF_LEFT;
1398         break;
1399     case SND_SOC_DAIFMT_RIGHT_J:
1400         ctrl1_val |= NAU8825_I2S_DF_RIGTH;
1401         break;
1402     case SND_SOC_DAIFMT_DSP_A:
1403         ctrl1_val |= NAU8825_I2S_DF_PCM_AB;
1404         break;
1405     case SND_SOC_DAIFMT_DSP_B:
1406         ctrl1_val |= NAU8825_I2S_DF_PCM_AB;
1407         ctrl1_val |= NAU8825_I2S_PCMB_EN;
1408         break;
1409     default:
1410         return -EINVAL;
1411     }
1412 
1413     nau8825_sema_acquire(nau8825, 3 * HZ);
1414 
1415     regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1,
1416         NAU8825_I2S_DL_MASK | NAU8825_I2S_DF_MASK |
1417         NAU8825_I2S_BP_MASK | NAU8825_I2S_PCMB_MASK,
1418         ctrl1_val);
1419     regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2,
1420         NAU8825_I2S_MS_MASK, ctrl2_val);
1421 
1422     /* Release the semaphore. */
1423     nau8825_sema_release(nau8825);
1424 
1425     return 0;
1426 }
1427 
1428 static const struct snd_soc_dai_ops nau8825_dai_ops = {
1429     .startup    = nau8825_dai_startup,
1430     .hw_params  = nau8825_hw_params,
1431     .set_fmt    = nau8825_set_dai_fmt,
1432 };
1433 
1434 #define NAU8825_RATES   SNDRV_PCM_RATE_8000_192000
1435 #define NAU8825_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
1436              | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
1437 
1438 static struct snd_soc_dai_driver nau8825_dai = {
1439     .name = "nau8825-hifi",
1440     .playback = {
1441         .stream_name     = "Playback",
1442         .channels_min    = 1,
1443         .channels_max    = 2,
1444         .rates       = NAU8825_RATES,
1445         .formats     = NAU8825_FORMATS,
1446     },
1447     .capture = {
1448         .stream_name     = "Capture",
1449         .channels_min    = 1,
1450         .channels_max    = 2,   /* Only 1 channel of data */
1451         .rates       = NAU8825_RATES,
1452         .formats     = NAU8825_FORMATS,
1453     },
1454     .ops = &nau8825_dai_ops,
1455 };
1456 
1457 /**
1458  * nau8825_enable_jack_detect - Specify a jack for event reporting
1459  *
1460  * @component:  component to register the jack with
1461  * @jack: jack to use to report headset and button events on
1462  *
1463  * After this function has been called the headset insert/remove and button
1464  * events will be routed to the given jack.  Jack can be null to stop
1465  * reporting.
1466  */
1467 int nau8825_enable_jack_detect(struct snd_soc_component *component,
1468                 struct snd_soc_jack *jack)
1469 {
1470     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
1471     struct regmap *regmap = nau8825->regmap;
1472 
1473     nau8825->jack = jack;
1474 
1475     if (!nau8825->jack) {
1476         regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL,
1477                    NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R |
1478                    NAU8825_SPKR_DWN1L, 0);
1479         return 0;
1480     }
1481     /* Ground HP Outputs[1:0], needed for headset auto detection
1482      * Enable Automatic Mic/Gnd switching reading on insert interrupt[6]
1483      */
1484     regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL,
1485         NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L,
1486         NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L);
1487 
1488     return 0;
1489 }
1490 EXPORT_SYMBOL_GPL(nau8825_enable_jack_detect);
1491 
1492 
1493 static bool nau8825_is_jack_inserted(struct regmap *regmap)
1494 {
1495     bool active_high, is_high;
1496     int status, jkdet;
1497 
1498     regmap_read(regmap, NAU8825_REG_JACK_DET_CTRL, &jkdet);
1499     active_high = jkdet & NAU8825_JACK_POLARITY;
1500     regmap_read(regmap, NAU8825_REG_I2C_DEVICE_ID, &status);
1501     is_high = status & NAU8825_GPIO2JD1;
1502     /* return jack connection status according to jack insertion logic
1503      * active high or active low.
1504      */
1505     return active_high == is_high;
1506 }
1507 
1508 static void nau8825_restart_jack_detection(struct regmap *regmap)
1509 {
1510     /* this will restart the entire jack detection process including MIC/GND
1511      * switching and create interrupts. We have to go from 0 to 1 and back
1512      * to 0 to restart.
1513      */
1514     regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
1515         NAU8825_JACK_DET_RESTART, NAU8825_JACK_DET_RESTART);
1516     regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
1517         NAU8825_JACK_DET_RESTART, 0);
1518 }
1519 
1520 static void nau8825_int_status_clear_all(struct regmap *regmap)
1521 {
1522     int active_irq, clear_irq, i;
1523 
1524     /* Reset the intrruption status from rightmost bit if the corres-
1525      * ponding irq event occurs.
1526      */
1527     regmap_read(regmap, NAU8825_REG_IRQ_STATUS, &active_irq);
1528     for (i = 0; i < NAU8825_REG_DATA_LEN; i++) {
1529         clear_irq = (0x1 << i);
1530         if (active_irq & clear_irq)
1531             regmap_write(regmap,
1532                 NAU8825_REG_INT_CLR_KEY_STATUS, clear_irq);
1533     }
1534 }
1535 
1536 static void nau8825_eject_jack(struct nau8825 *nau8825)
1537 {
1538     struct snd_soc_dapm_context *dapm = nau8825->dapm;
1539     struct regmap *regmap = nau8825->regmap;
1540 
1541     /* Force to cancel the cross talk detection process */
1542     nau8825_xtalk_cancel(nau8825);
1543 
1544     snd_soc_dapm_disable_pin(dapm, "SAR");
1545     snd_soc_dapm_disable_pin(dapm, "MICBIAS");
1546     /* Detach 2kOhm Resistors from MICBIAS to MICGND1/2 */
1547     regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
1548         NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2, 0);
1549     /* ground HPL/HPR, MICGRND1/2 */
1550     regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 0xf, 0xf);
1551 
1552     snd_soc_dapm_sync(dapm);
1553 
1554     /* Clear all interruption status */
1555     nau8825_int_status_clear_all(regmap);
1556 
1557     /* Enable the insertion interruption, disable the ejection inter-
1558      * ruption, and then bypass de-bounce circuit.
1559      */
1560     regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_DIS_CTRL,
1561         NAU8825_IRQ_EJECT_DIS | NAU8825_IRQ_INSERT_DIS,
1562         NAU8825_IRQ_EJECT_DIS);
1563     regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
1564         NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_EJECT_EN |
1565         NAU8825_IRQ_HEADSET_COMPLETE_EN | NAU8825_IRQ_INSERT_EN,
1566         NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_EJECT_EN |
1567         NAU8825_IRQ_HEADSET_COMPLETE_EN);
1568     regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
1569         NAU8825_JACK_DET_DB_BYPASS, NAU8825_JACK_DET_DB_BYPASS);
1570 
1571     /* Disable ADC needed for interruptions at audo mode */
1572     regmap_update_bits(regmap, NAU8825_REG_ENA_CTRL,
1573         NAU8825_ENABLE_ADC, 0);
1574 
1575     /* Close clock for jack type detection at manual mode */
1576     nau8825_configure_sysclk(nau8825, NAU8825_CLK_DIS, 0);
1577 }
1578 
1579 /* Enable audo mode interruptions with internal clock. */
1580 static void nau8825_setup_auto_irq(struct nau8825 *nau8825)
1581 {
1582     struct regmap *regmap = nau8825->regmap;
1583 
1584     /* Enable headset jack type detection complete interruption and
1585      * jack ejection interruption.
1586      */
1587     regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
1588         NAU8825_IRQ_HEADSET_COMPLETE_EN | NAU8825_IRQ_EJECT_EN, 0);
1589 
1590     /* Enable internal VCO needed for interruptions */
1591     nau8825_configure_sysclk(nau8825, NAU8825_CLK_INTERNAL, 0);
1592 
1593     /* Enable ADC needed for interruptions */
1594     regmap_update_bits(regmap, NAU8825_REG_ENA_CTRL,
1595         NAU8825_ENABLE_ADC, NAU8825_ENABLE_ADC);
1596 
1597     /* Chip needs one FSCLK cycle in order to generate interruptions,
1598      * as we cannot guarantee one will be provided by the system. Turning
1599      * master mode on then off enables us to generate that FSCLK cycle
1600      * with a minimum of contention on the clock bus.
1601      */
1602     regmap_update_bits(regmap, NAU8825_REG_I2S_PCM_CTRL2,
1603         NAU8825_I2S_MS_MASK, NAU8825_I2S_MS_MASTER);
1604     regmap_update_bits(regmap, NAU8825_REG_I2S_PCM_CTRL2,
1605         NAU8825_I2S_MS_MASK, NAU8825_I2S_MS_SLAVE);
1606 
1607     /* Not bypass de-bounce circuit */
1608     regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
1609         NAU8825_JACK_DET_DB_BYPASS, 0);
1610 
1611     /* Unmask all interruptions */
1612     regmap_write(regmap, NAU8825_REG_INTERRUPT_DIS_CTRL, 0);
1613 
1614     /* Restart the jack detection process at auto mode */
1615     nau8825_restart_jack_detection(regmap);
1616 }
1617 
1618 static int nau8825_button_decode(int value)
1619 {
1620     int buttons = 0;
1621 
1622     /* The chip supports up to 8 buttons, but ALSA defines only 6 buttons */
1623     if (value & BIT(0))
1624         buttons |= SND_JACK_BTN_0;
1625     if (value & BIT(1))
1626         buttons |= SND_JACK_BTN_1;
1627     if (value & BIT(2))
1628         buttons |= SND_JACK_BTN_2;
1629     if (value & BIT(3))
1630         buttons |= SND_JACK_BTN_3;
1631     if (value & BIT(4))
1632         buttons |= SND_JACK_BTN_4;
1633     if (value & BIT(5))
1634         buttons |= SND_JACK_BTN_5;
1635 
1636     return buttons;
1637 }
1638 
1639 static int nau8825_jack_insert(struct nau8825 *nau8825)
1640 {
1641     struct regmap *regmap = nau8825->regmap;
1642     struct snd_soc_dapm_context *dapm = nau8825->dapm;
1643     int jack_status_reg, mic_detected;
1644     int type = 0;
1645 
1646     regmap_read(regmap, NAU8825_REG_GENERAL_STATUS, &jack_status_reg);
1647     mic_detected = (jack_status_reg >> 10) & 3;
1648     /* The JKSLV and JKR2 all detected in high impedance headset */
1649     if (mic_detected == 0x3)
1650         nau8825->high_imped = true;
1651     else
1652         nau8825->high_imped = false;
1653 
1654     switch (mic_detected) {
1655     case 0:
1656         /* no mic */
1657         type = SND_JACK_HEADPHONE;
1658         break;
1659     case 1:
1660         dev_dbg(nau8825->dev, "OMTP (micgnd1) mic connected\n");
1661         type = SND_JACK_HEADSET;
1662 
1663         /* Unground MICGND1 */
1664         regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 3 << 2,
1665             1 << 2);
1666         /* Attach 2kOhm Resistor from MICBIAS to MICGND1 */
1667         regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
1668             NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2,
1669             NAU8825_MICBIAS_JKR2);
1670         /* Attach SARADC to MICGND1 */
1671         regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
1672             NAU8825_SAR_INPUT_MASK,
1673             NAU8825_SAR_INPUT_JKR2);
1674 
1675         snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
1676         snd_soc_dapm_force_enable_pin(dapm, "SAR");
1677         snd_soc_dapm_sync(dapm);
1678         break;
1679     case 2:
1680         dev_dbg(nau8825->dev, "CTIA (micgnd2) mic connected\n");
1681         type = SND_JACK_HEADSET;
1682 
1683         /* Unground MICGND2 */
1684         regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 3 << 2,
1685             2 << 2);
1686         /* Attach 2kOhm Resistor from MICBIAS to MICGND2 */
1687         regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
1688             NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2,
1689             NAU8825_MICBIAS_JKSLV);
1690         /* Attach SARADC to MICGND2 */
1691         regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
1692             NAU8825_SAR_INPUT_MASK,
1693             NAU8825_SAR_INPUT_JKSLV);
1694 
1695         snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
1696         snd_soc_dapm_force_enable_pin(dapm, "SAR");
1697         snd_soc_dapm_sync(dapm);
1698         break;
1699     case 3:
1700         /* detect error case */
1701         dev_err(nau8825->dev, "detection error; disable mic function\n");
1702         type = SND_JACK_HEADPHONE;
1703         break;
1704     }
1705 
1706     /* Leaving HPOL/R grounded after jack insert by default. They will be
1707      * ungrounded as part of the widget power up sequence at the beginning
1708      * of playback to reduce pop.
1709      */
1710     return type;
1711 }
1712 
1713 #define NAU8825_BUTTONS (SND_JACK_BTN_0 | SND_JACK_BTN_1 | \
1714         SND_JACK_BTN_2 | SND_JACK_BTN_3)
1715 
1716 static irqreturn_t nau8825_interrupt(int irq, void *data)
1717 {
1718     struct nau8825 *nau8825 = (struct nau8825 *)data;
1719     struct regmap *regmap = nau8825->regmap;
1720     int active_irq, clear_irq = 0, event = 0, event_mask = 0;
1721 
1722     if (regmap_read(regmap, NAU8825_REG_IRQ_STATUS, &active_irq)) {
1723         dev_err(nau8825->dev, "failed to read irq status\n");
1724         return IRQ_NONE;
1725     }
1726 
1727     if ((active_irq & NAU8825_JACK_EJECTION_IRQ_MASK) ==
1728         NAU8825_JACK_EJECTION_DETECTED) {
1729 
1730         nau8825_eject_jack(nau8825);
1731         event_mask |= SND_JACK_HEADSET;
1732         clear_irq = NAU8825_JACK_EJECTION_IRQ_MASK;
1733     } else if (active_irq & NAU8825_KEY_SHORT_PRESS_IRQ) {
1734         int key_status;
1735 
1736         regmap_read(regmap, NAU8825_REG_INT_CLR_KEY_STATUS,
1737             &key_status);
1738 
1739         /* upper 8 bits of the register are for short pressed keys,
1740          * lower 8 bits - for long pressed buttons
1741          */
1742         nau8825->button_pressed = nau8825_button_decode(
1743             key_status >> 8);
1744 
1745         event |= nau8825->button_pressed;
1746         event_mask |= NAU8825_BUTTONS;
1747         clear_irq = NAU8825_KEY_SHORT_PRESS_IRQ;
1748     } else if (active_irq & NAU8825_KEY_RELEASE_IRQ) {
1749         event_mask = NAU8825_BUTTONS;
1750         clear_irq = NAU8825_KEY_RELEASE_IRQ;
1751     } else if (active_irq & NAU8825_HEADSET_COMPLETION_IRQ) {
1752         if (nau8825_is_jack_inserted(regmap)) {
1753             event |= nau8825_jack_insert(nau8825);
1754             if (nau8825->xtalk_enable && !nau8825->high_imped) {
1755                 /* Apply the cross talk suppression in the
1756                  * headset without high impedance.
1757                  */
1758                 if (!nau8825->xtalk_protect) {
1759                     /* Raise protection for cross talk de-
1760                      * tection if no protection before.
1761                      * The driver has to cancel the pro-
1762                      * cess and restore changes if process
1763                      * is ongoing when ejection.
1764                      */
1765                     int ret;
1766                     nau8825->xtalk_protect = true;
1767                     ret = nau8825_sema_acquire(nau8825, 0);
1768                     if (ret)
1769                         nau8825->xtalk_protect = false;
1770                 }
1771                 /* Startup cross talk detection process */
1772                 if (nau8825->xtalk_protect) {
1773                     nau8825->xtalk_state =
1774                         NAU8825_XTALK_PREPARE;
1775                     schedule_work(&nau8825->xtalk_work);
1776                 }
1777             } else {
1778                 /* The cross talk suppression shouldn't apply
1779                  * in the headset with high impedance. Thus,
1780                  * relieve the protection raised before.
1781                  */
1782                 if (nau8825->xtalk_protect) {
1783                     nau8825_sema_release(nau8825);
1784                     nau8825->xtalk_protect = false;
1785                 }
1786             }
1787         } else {
1788             dev_warn(nau8825->dev, "Headset completion IRQ fired but no headset connected\n");
1789             nau8825_eject_jack(nau8825);
1790         }
1791 
1792         event_mask |= SND_JACK_HEADSET;
1793         clear_irq = NAU8825_HEADSET_COMPLETION_IRQ;
1794         /* Record the interruption report event for driver to report
1795          * the event later. The jack report will delay until cross
1796          * talk detection process is done.
1797          */
1798         if (nau8825->xtalk_state == NAU8825_XTALK_PREPARE) {
1799             nau8825->xtalk_event = event;
1800             nau8825->xtalk_event_mask = event_mask;
1801         }
1802     } else if (active_irq & NAU8825_IMPEDANCE_MEAS_IRQ) {
1803         /* crosstalk detection enable and process on going */
1804         if (nau8825->xtalk_enable && nau8825->xtalk_protect)
1805             schedule_work(&nau8825->xtalk_work);
1806         clear_irq = NAU8825_IMPEDANCE_MEAS_IRQ;
1807     } else if ((active_irq & NAU8825_JACK_INSERTION_IRQ_MASK) ==
1808         NAU8825_JACK_INSERTION_DETECTED) {
1809         /* One more step to check GPIO status directly. Thus, the
1810          * driver can confirm the real insertion interruption because
1811          * the intrruption at manual mode has bypassed debounce
1812          * circuit which can get rid of unstable status.
1813          */
1814         if (nau8825_is_jack_inserted(regmap)) {
1815             /* Turn off insertion interruption at manual mode */
1816             regmap_update_bits(regmap,
1817                 NAU8825_REG_INTERRUPT_DIS_CTRL,
1818                 NAU8825_IRQ_INSERT_DIS,
1819                 NAU8825_IRQ_INSERT_DIS);
1820             regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
1821                 NAU8825_IRQ_INSERT_EN, NAU8825_IRQ_INSERT_EN);
1822             /* Enable interruption for jack type detection at audo
1823              * mode which can detect microphone and jack type.
1824              */
1825             nau8825_setup_auto_irq(nau8825);
1826         }
1827     }
1828 
1829     if (!clear_irq)
1830         clear_irq = active_irq;
1831     /* clears the rightmost interruption */
1832     regmap_write(regmap, NAU8825_REG_INT_CLR_KEY_STATUS, clear_irq);
1833 
1834     /* Delay jack report until cross talk detection is done. It can avoid
1835      * application to do playback preparation when cross talk detection
1836      * process is still working. Otherwise, the resource like clock and
1837      * power will be issued by them at the same time and conflict happens.
1838      */
1839     if (event_mask && nau8825->xtalk_state == NAU8825_XTALK_DONE)
1840         snd_soc_jack_report(nau8825->jack, event, event_mask);
1841 
1842     return IRQ_HANDLED;
1843 }
1844 
1845 static void nau8825_setup_buttons(struct nau8825 *nau8825)
1846 {
1847     struct regmap *regmap = nau8825->regmap;
1848 
1849     regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
1850         NAU8825_SAR_TRACKING_GAIN_MASK,
1851         nau8825->sar_voltage << NAU8825_SAR_TRACKING_GAIN_SFT);
1852     regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
1853         NAU8825_SAR_COMPARE_TIME_MASK,
1854         nau8825->sar_compare_time << NAU8825_SAR_COMPARE_TIME_SFT);
1855     regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
1856         NAU8825_SAR_SAMPLING_TIME_MASK,
1857         nau8825->sar_sampling_time << NAU8825_SAR_SAMPLING_TIME_SFT);
1858 
1859     regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
1860         NAU8825_KEYDET_LEVELS_NR_MASK,
1861         (nau8825->sar_threshold_num - 1) << NAU8825_KEYDET_LEVELS_NR_SFT);
1862     regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
1863         NAU8825_KEYDET_HYSTERESIS_MASK,
1864         nau8825->sar_hysteresis << NAU8825_KEYDET_HYSTERESIS_SFT);
1865     regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
1866         NAU8825_KEYDET_SHORTKEY_DEBOUNCE_MASK,
1867         nau8825->key_debounce << NAU8825_KEYDET_SHORTKEY_DEBOUNCE_SFT);
1868 
1869     regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_1,
1870         (nau8825->sar_threshold[0] << 8) | nau8825->sar_threshold[1]);
1871     regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_2,
1872         (nau8825->sar_threshold[2] << 8) | nau8825->sar_threshold[3]);
1873     regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_3,
1874         (nau8825->sar_threshold[4] << 8) | nau8825->sar_threshold[5]);
1875     regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_4,
1876         (nau8825->sar_threshold[6] << 8) | nau8825->sar_threshold[7]);
1877 
1878     /* Enable short press and release interruptions */
1879     regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
1880         NAU8825_IRQ_KEY_SHORT_PRESS_EN | NAU8825_IRQ_KEY_RELEASE_EN,
1881         0);
1882 }
1883 
1884 static void nau8825_init_regs(struct nau8825 *nau8825)
1885 {
1886     struct regmap *regmap = nau8825->regmap;
1887 
1888     /* Latch IIC LSB value */
1889     regmap_write(regmap, NAU8825_REG_IIC_ADDR_SET, 0x0001);
1890     /* Enable Bias/Vmid */
1891     regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
1892         NAU8825_BIAS_VMID, NAU8825_BIAS_VMID);
1893     regmap_update_bits(nau8825->regmap, NAU8825_REG_BOOST,
1894         NAU8825_GLOBAL_BIAS_EN, NAU8825_GLOBAL_BIAS_EN);
1895 
1896     /* VMID Tieoff */
1897     regmap_update_bits(regmap, NAU8825_REG_BIAS_ADJ,
1898         NAU8825_BIAS_VMID_SEL_MASK,
1899         nau8825->vref_impedance << NAU8825_BIAS_VMID_SEL_SFT);
1900     /* Disable Boost Driver, Automatic Short circuit protection enable */
1901     regmap_update_bits(regmap, NAU8825_REG_BOOST,
1902         NAU8825_PRECHARGE_DIS | NAU8825_HP_BOOST_DIS |
1903         NAU8825_HP_BOOST_G_DIS | NAU8825_SHORT_SHUTDOWN_EN,
1904         NAU8825_PRECHARGE_DIS | NAU8825_HP_BOOST_DIS |
1905         NAU8825_HP_BOOST_G_DIS | NAU8825_SHORT_SHUTDOWN_EN);
1906 
1907     regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
1908         NAU8825_JKDET_OUTPUT_EN,
1909         nau8825->jkdet_enable ? 0 : NAU8825_JKDET_OUTPUT_EN);
1910     regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
1911         NAU8825_JKDET_PULL_EN,
1912         nau8825->jkdet_pull_enable ? 0 : NAU8825_JKDET_PULL_EN);
1913     regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
1914         NAU8825_JKDET_PULL_UP,
1915         nau8825->jkdet_pull_up ? NAU8825_JKDET_PULL_UP : 0);
1916     regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
1917         NAU8825_JACK_POLARITY,
1918         /* jkdet_polarity - 1  is for active-low */
1919         nau8825->jkdet_polarity ? 0 : NAU8825_JACK_POLARITY);
1920 
1921     regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
1922         NAU8825_JACK_INSERT_DEBOUNCE_MASK,
1923         nau8825->jack_insert_debounce << NAU8825_JACK_INSERT_DEBOUNCE_SFT);
1924     regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
1925         NAU8825_JACK_EJECT_DEBOUNCE_MASK,
1926         nau8825->jack_eject_debounce << NAU8825_JACK_EJECT_DEBOUNCE_SFT);
1927 
1928     /* Pull up IRQ pin */
1929     regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
1930         NAU8825_IRQ_PIN_PULLUP | NAU8825_IRQ_PIN_PULL_EN,
1931         NAU8825_IRQ_PIN_PULLUP | NAU8825_IRQ_PIN_PULL_EN);
1932     /* Mask unneeded IRQs: 1 - disable, 0 - enable */
1933     regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK, 0x7ff, 0x7ff);
1934 
1935     regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
1936         NAU8825_MICBIAS_VOLTAGE_MASK, nau8825->micbias_voltage);
1937 
1938     if (nau8825->sar_threshold_num)
1939         nau8825_setup_buttons(nau8825);
1940 
1941     /* Default oversampling/decimations settings are unusable
1942      * (audible hiss). Set it to something better.
1943      */
1944     regmap_update_bits(regmap, NAU8825_REG_ADC_RATE,
1945         NAU8825_ADC_SYNC_DOWN_MASK | NAU8825_ADC_SINC4_EN,
1946         NAU8825_ADC_SYNC_DOWN_64);
1947     regmap_update_bits(regmap, NAU8825_REG_DAC_CTRL1,
1948         NAU8825_DAC_OVERSAMPLE_MASK, NAU8825_DAC_OVERSAMPLE_64);
1949     /* Disable DACR/L power */
1950     regmap_update_bits(regmap, NAU8825_REG_CHARGE_PUMP,
1951         NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL,
1952         NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL);
1953     /* Enable TESTDAC. This sets the analog DAC inputs to a '0' input
1954      * signal to avoid any glitches due to power up transients in both
1955      * the analog and digital DAC circuit.
1956      */
1957     regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
1958         NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN);
1959     /* CICCLP off */
1960     regmap_update_bits(regmap, NAU8825_REG_DAC_CTRL1,
1961         NAU8825_DAC_CLIP_OFF, NAU8825_DAC_CLIP_OFF);
1962 
1963     /* Class AB bias current to 2x, DAC Capacitor enable MSB/LSB */
1964     regmap_update_bits(regmap, NAU8825_REG_ANALOG_CONTROL_2,
1965         NAU8825_HP_NON_CLASSG_CURRENT_2xADJ |
1966         NAU8825_DAC_CAPACITOR_MSB | NAU8825_DAC_CAPACITOR_LSB,
1967         NAU8825_HP_NON_CLASSG_CURRENT_2xADJ |
1968         NAU8825_DAC_CAPACITOR_MSB | NAU8825_DAC_CAPACITOR_LSB);
1969     /* Class G timer 64ms */
1970     regmap_update_bits(regmap, NAU8825_REG_CLASSG_CTRL,
1971         NAU8825_CLASSG_TIMER_MASK,
1972         0x20 << NAU8825_CLASSG_TIMER_SFT);
1973     /* DAC clock delay 2ns, VREF */
1974     regmap_update_bits(regmap, NAU8825_REG_RDAC,
1975         NAU8825_RDAC_CLK_DELAY_MASK | NAU8825_RDAC_VREF_MASK,
1976         (0x2 << NAU8825_RDAC_CLK_DELAY_SFT) |
1977         (0x3 << NAU8825_RDAC_VREF_SFT));
1978     /* Config L/R channel */
1979     regmap_update_bits(nau8825->regmap, NAU8825_REG_DACL_CTRL,
1980         NAU8825_DACL_CH_SEL_MASK, NAU8825_DACL_CH_SEL_L);
1981     regmap_update_bits(nau8825->regmap, NAU8825_REG_DACR_CTRL,
1982         NAU8825_DACL_CH_SEL_MASK, NAU8825_DACL_CH_SEL_R);
1983     /* Disable short Frame Sync detection logic */
1984     regmap_update_bits(regmap, NAU8825_REG_LEFT_TIME_SLOT,
1985         NAU8825_DIS_FS_SHORT_DET, NAU8825_DIS_FS_SHORT_DET);
1986 }
1987 
1988 static const struct regmap_config nau8825_regmap_config = {
1989     .val_bits = NAU8825_REG_DATA_LEN,
1990     .reg_bits = NAU8825_REG_ADDR_LEN,
1991 
1992     .max_register = NAU8825_REG_MAX,
1993     .readable_reg = nau8825_readable_reg,
1994     .writeable_reg = nau8825_writeable_reg,
1995     .volatile_reg = nau8825_volatile_reg,
1996 
1997     .cache_type = REGCACHE_RBTREE,
1998     .reg_defaults = nau8825_reg_defaults,
1999     .num_reg_defaults = ARRAY_SIZE(nau8825_reg_defaults),
2000 };
2001 
2002 static int nau8825_component_probe(struct snd_soc_component *component)
2003 {
2004     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
2005     struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
2006 
2007     nau8825->dapm = dapm;
2008 
2009     return 0;
2010 }
2011 
2012 static void nau8825_component_remove(struct snd_soc_component *component)
2013 {
2014     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
2015 
2016     /* Cancel and reset cross tak suppresstion detection funciton */
2017     nau8825_xtalk_cancel(nau8825);
2018 }
2019 
2020 /**
2021  * nau8825_calc_fll_param - Calculate FLL parameters.
2022  * @fll_in: external clock provided to codec.
2023  * @fs: sampling rate.
2024  * @fll_param: Pointer to structure of FLL parameters.
2025  *
2026  * Calculate FLL parameters to configure codec.
2027  *
2028  * Returns 0 for success or negative error code.
2029  */
2030 static int nau8825_calc_fll_param(unsigned int fll_in, unsigned int fs,
2031         struct nau8825_fll *fll_param)
2032 {
2033     u64 fvco, fvco_max;
2034     unsigned int fref, i, fvco_sel;
2035 
2036     /* Ensure the reference clock frequency (FREF) is <= 13.5MHz by dividing
2037      * freq_in by 1, 2, 4, or 8 using FLL pre-scalar.
2038      * FREF = freq_in / NAU8825_FLL_REF_DIV_MASK
2039      */
2040     for (i = 0; i < ARRAY_SIZE(fll_pre_scalar); i++) {
2041         fref = fll_in / fll_pre_scalar[i].param;
2042         if (fref <= NAU_FREF_MAX)
2043             break;
2044     }
2045     if (i == ARRAY_SIZE(fll_pre_scalar))
2046         return -EINVAL;
2047     fll_param->clk_ref_div = fll_pre_scalar[i].val;
2048 
2049     /* Choose the FLL ratio based on FREF */
2050     for (i = 0; i < ARRAY_SIZE(fll_ratio); i++) {
2051         if (fref >= fll_ratio[i].param)
2052             break;
2053     }
2054     if (i == ARRAY_SIZE(fll_ratio))
2055         return -EINVAL;
2056     fll_param->ratio = fll_ratio[i].val;
2057 
2058     /* Calculate the frequency of DCO (FDCO) given freq_out = 256 * Fs.
2059      * FDCO must be within the 90MHz - 124MHz or the FFL cannot be
2060      * guaranteed across the full range of operation.
2061      * FDCO = freq_out * 2 * mclk_src_scaling
2062      */
2063     fvco_max = 0;
2064     fvco_sel = ARRAY_SIZE(mclk_src_scaling);
2065     for (i = 0; i < ARRAY_SIZE(mclk_src_scaling); i++) {
2066         fvco = 256ULL * fs * 2 * mclk_src_scaling[i].param;
2067         if (fvco > NAU_FVCO_MIN && fvco < NAU_FVCO_MAX &&
2068             fvco_max < fvco) {
2069             fvco_max = fvco;
2070             fvco_sel = i;
2071         }
2072     }
2073     if (ARRAY_SIZE(mclk_src_scaling) == fvco_sel)
2074         return -EINVAL;
2075     fll_param->mclk_src = mclk_src_scaling[fvco_sel].val;
2076 
2077     /* Calculate the FLL 10-bit integer input and the FLL 16-bit fractional
2078      * input based on FDCO, FREF and FLL ratio.
2079      */
2080     fvco = div_u64(fvco_max << 16, fref * fll_param->ratio);
2081     fll_param->fll_int = (fvco >> 16) & 0x3FF;
2082     fll_param->fll_frac = fvco & 0xFFFF;
2083     return 0;
2084 }
2085 
2086 static void nau8825_fll_apply(struct nau8825 *nau8825,
2087         struct nau8825_fll *fll_param)
2088 {
2089     regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
2090         NAU8825_CLK_SRC_MASK | NAU8825_CLK_MCLK_SRC_MASK,
2091         NAU8825_CLK_SRC_MCLK | fll_param->mclk_src);
2092     /* Make DSP operate at high speed for better performance. */
2093     regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL1,
2094         NAU8825_FLL_RATIO_MASK | NAU8825_ICTRL_LATCH_MASK,
2095         fll_param->ratio | (0x6 << NAU8825_ICTRL_LATCH_SFT));
2096     /* FLL 16-bit fractional input */
2097     regmap_write(nau8825->regmap, NAU8825_REG_FLL2, fll_param->fll_frac);
2098     /* FLL 10-bit integer input */
2099     regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL3,
2100             NAU8825_FLL_INTEGER_MASK, fll_param->fll_int);
2101     /* FLL pre-scaler */
2102     regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL4,
2103             NAU8825_FLL_REF_DIV_MASK,
2104             fll_param->clk_ref_div << NAU8825_FLL_REF_DIV_SFT);
2105     /* select divided VCO input */
2106     regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
2107         NAU8825_FLL_CLK_SW_MASK, NAU8825_FLL_CLK_SW_REF);
2108     /* Disable free-running mode */
2109     regmap_update_bits(nau8825->regmap,
2110         NAU8825_REG_FLL6, NAU8825_DCO_EN, 0);
2111     if (fll_param->fll_frac) {
2112         /* set FLL loop filter enable and cutoff frequency at 500Khz */
2113         regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
2114             NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
2115             NAU8825_FLL_FTR_SW_MASK,
2116             NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
2117             NAU8825_FLL_FTR_SW_FILTER);
2118         regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL6,
2119             NAU8825_SDM_EN | NAU8825_CUTOFF500,
2120             NAU8825_SDM_EN | NAU8825_CUTOFF500);
2121     } else {
2122         /* disable FLL loop filter and cutoff frequency */
2123         regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
2124             NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
2125             NAU8825_FLL_FTR_SW_MASK, NAU8825_FLL_FTR_SW_ACCU);
2126         regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL6,
2127             NAU8825_SDM_EN | NAU8825_CUTOFF500, 0);
2128     }
2129 }
2130 
2131 /* freq_out must be 256*Fs in order to achieve the best performance */
2132 static int nau8825_set_pll(struct snd_soc_component *component, int pll_id, int source,
2133         unsigned int freq_in, unsigned int freq_out)
2134 {
2135     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
2136     struct nau8825_fll fll_param;
2137     int ret, fs;
2138 
2139     fs = freq_out / 256;
2140     ret = nau8825_calc_fll_param(freq_in, fs, &fll_param);
2141     if (ret < 0) {
2142         dev_err(component->dev, "Unsupported input clock %d\n", freq_in);
2143         return ret;
2144     }
2145     dev_dbg(component->dev, "mclk_src=%x ratio=%x fll_frac=%x fll_int=%x clk_ref_div=%x\n",
2146         fll_param.mclk_src, fll_param.ratio, fll_param.fll_frac,
2147         fll_param.fll_int, fll_param.clk_ref_div);
2148 
2149     nau8825_fll_apply(nau8825, &fll_param);
2150     mdelay(2);
2151     regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
2152             NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO);
2153     return 0;
2154 }
2155 
2156 static int nau8825_mclk_prepare(struct nau8825 *nau8825, unsigned int freq)
2157 {
2158     int ret;
2159 
2160     nau8825->mclk = devm_clk_get(nau8825->dev, "mclk");
2161     if (IS_ERR(nau8825->mclk)) {
2162         dev_info(nau8825->dev, "No 'mclk' clock found, assume MCLK is managed externally");
2163         return 0;
2164     }
2165 
2166     if (!nau8825->mclk_freq) {
2167         ret = clk_prepare_enable(nau8825->mclk);
2168         if (ret) {
2169             dev_err(nau8825->dev, "Unable to prepare codec mclk\n");
2170             return ret;
2171         }
2172     }
2173 
2174     if (nau8825->mclk_freq != freq) {
2175         freq = clk_round_rate(nau8825->mclk, freq);
2176         ret = clk_set_rate(nau8825->mclk, freq);
2177         if (ret) {
2178             dev_err(nau8825->dev, "Unable to set mclk rate\n");
2179             return ret;
2180         }
2181         nau8825->mclk_freq = freq;
2182     }
2183 
2184     return 0;
2185 }
2186 
2187 static void nau8825_configure_mclk_as_sysclk(struct regmap *regmap)
2188 {
2189     regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
2190         NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_MCLK);
2191     regmap_update_bits(regmap, NAU8825_REG_FLL6,
2192         NAU8825_DCO_EN, 0);
2193     /* Make DSP operate as default setting for power saving. */
2194     regmap_update_bits(regmap, NAU8825_REG_FLL1,
2195         NAU8825_ICTRL_LATCH_MASK, 0);
2196 }
2197 
2198 static int nau8825_configure_sysclk(struct nau8825 *nau8825, int clk_id,
2199     unsigned int freq)
2200 {
2201     struct regmap *regmap = nau8825->regmap;
2202     int ret;
2203 
2204     switch (clk_id) {
2205     case NAU8825_CLK_DIS:
2206         /* Clock provided externally and disable internal VCO clock */
2207         nau8825_configure_mclk_as_sysclk(regmap);
2208         if (nau8825->mclk_freq) {
2209             clk_disable_unprepare(nau8825->mclk);
2210             nau8825->mclk_freq = 0;
2211         }
2212 
2213         break;
2214     case NAU8825_CLK_MCLK:
2215         /* Acquire the semaphore to synchronize the playback and
2216          * interrupt handler. In order to avoid the playback inter-
2217          * fered by cross talk process, the driver make the playback
2218          * preparation halted until cross talk process finish.
2219          */
2220         nau8825_sema_acquire(nau8825, 3 * HZ);
2221         nau8825_configure_mclk_as_sysclk(regmap);
2222         /* MCLK not changed by clock tree */
2223         regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
2224             NAU8825_CLK_MCLK_SRC_MASK, 0);
2225         /* Release the semaphore. */
2226         nau8825_sema_release(nau8825);
2227 
2228         ret = nau8825_mclk_prepare(nau8825, freq);
2229         if (ret)
2230             return ret;
2231 
2232         break;
2233     case NAU8825_CLK_INTERNAL:
2234         if (nau8825_is_jack_inserted(nau8825->regmap)) {
2235             regmap_update_bits(regmap, NAU8825_REG_FLL6,
2236                 NAU8825_DCO_EN, NAU8825_DCO_EN);
2237             regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
2238                 NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO);
2239             /* Decrease the VCO frequency and make DSP operate
2240              * as default setting for power saving.
2241              */
2242             regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
2243                 NAU8825_CLK_MCLK_SRC_MASK, 0xf);
2244             regmap_update_bits(regmap, NAU8825_REG_FLL1,
2245                 NAU8825_ICTRL_LATCH_MASK |
2246                 NAU8825_FLL_RATIO_MASK, 0x10);
2247             regmap_update_bits(regmap, NAU8825_REG_FLL6,
2248                 NAU8825_SDM_EN, NAU8825_SDM_EN);
2249         } else {
2250             /* The clock turns off intentionally for power saving
2251              * when no headset connected.
2252              */
2253             nau8825_configure_mclk_as_sysclk(regmap);
2254             dev_warn(nau8825->dev, "Disable clock for power saving when no headset connected\n");
2255         }
2256         if (nau8825->mclk_freq) {
2257             clk_disable_unprepare(nau8825->mclk);
2258             nau8825->mclk_freq = 0;
2259         }
2260 
2261         break;
2262     case NAU8825_CLK_FLL_MCLK:
2263         /* Acquire the semaphore to synchronize the playback and
2264          * interrupt handler. In order to avoid the playback inter-
2265          * fered by cross talk process, the driver make the playback
2266          * preparation halted until cross talk process finish.
2267          */
2268         nau8825_sema_acquire(nau8825, 3 * HZ);
2269         /* Higher FLL reference input frequency can only set lower
2270          * gain error, such as 0000 for input reference from MCLK
2271          * 12.288Mhz.
2272          */
2273         regmap_update_bits(regmap, NAU8825_REG_FLL3,
2274             NAU8825_FLL_CLK_SRC_MASK | NAU8825_GAIN_ERR_MASK,
2275             NAU8825_FLL_CLK_SRC_MCLK | 0);
2276         /* Release the semaphore. */
2277         nau8825_sema_release(nau8825);
2278 
2279         ret = nau8825_mclk_prepare(nau8825, freq);
2280         if (ret)
2281             return ret;
2282 
2283         break;
2284     case NAU8825_CLK_FLL_BLK:
2285         /* Acquire the semaphore to synchronize the playback and
2286          * interrupt handler. In order to avoid the playback inter-
2287          * fered by cross talk process, the driver make the playback
2288          * preparation halted until cross talk process finish.
2289          */
2290         nau8825_sema_acquire(nau8825, 3 * HZ);
2291         /* If FLL reference input is from low frequency source,
2292          * higher error gain can apply such as 0xf which has
2293          * the most sensitive gain error correction threshold,
2294          * Therefore, FLL has the most accurate DCO to
2295          * target frequency.
2296          */
2297         regmap_update_bits(regmap, NAU8825_REG_FLL3,
2298             NAU8825_FLL_CLK_SRC_MASK | NAU8825_GAIN_ERR_MASK,
2299             NAU8825_FLL_CLK_SRC_BLK |
2300             (0xf << NAU8825_GAIN_ERR_SFT));
2301         /* Release the semaphore. */
2302         nau8825_sema_release(nau8825);
2303 
2304         if (nau8825->mclk_freq) {
2305             clk_disable_unprepare(nau8825->mclk);
2306             nau8825->mclk_freq = 0;
2307         }
2308 
2309         break;
2310     case NAU8825_CLK_FLL_FS:
2311         /* Acquire the semaphore to synchronize the playback and
2312          * interrupt handler. In order to avoid the playback inter-
2313          * fered by cross talk process, the driver make the playback
2314          * preparation halted until cross talk process finish.
2315          */
2316         nau8825_sema_acquire(nau8825, 3 * HZ);
2317         /* If FLL reference input is from low frequency source,
2318          * higher error gain can apply such as 0xf which has
2319          * the most sensitive gain error correction threshold,
2320          * Therefore, FLL has the most accurate DCO to
2321          * target frequency.
2322          */
2323         regmap_update_bits(regmap, NAU8825_REG_FLL3,
2324             NAU8825_FLL_CLK_SRC_MASK | NAU8825_GAIN_ERR_MASK,
2325             NAU8825_FLL_CLK_SRC_FS |
2326             (0xf << NAU8825_GAIN_ERR_SFT));
2327         /* Release the semaphore. */
2328         nau8825_sema_release(nau8825);
2329 
2330         if (nau8825->mclk_freq) {
2331             clk_disable_unprepare(nau8825->mclk);
2332             nau8825->mclk_freq = 0;
2333         }
2334 
2335         break;
2336     default:
2337         dev_err(nau8825->dev, "Invalid clock id (%d)\n", clk_id);
2338         return -EINVAL;
2339     }
2340 
2341     dev_dbg(nau8825->dev, "Sysclk is %dHz and clock id is %d\n", freq,
2342         clk_id);
2343     return 0;
2344 }
2345 
2346 static int nau8825_set_sysclk(struct snd_soc_component *component, int clk_id,
2347     int source, unsigned int freq, int dir)
2348 {
2349     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
2350 
2351     return nau8825_configure_sysclk(nau8825, clk_id, freq);
2352 }
2353 
2354 static int nau8825_resume_setup(struct nau8825 *nau8825)
2355 {
2356     struct regmap *regmap = nau8825->regmap;
2357 
2358     /* Close clock when jack type detection at manual mode */
2359     nau8825_configure_sysclk(nau8825, NAU8825_CLK_DIS, 0);
2360 
2361     /* Clear all interruption status */
2362     nau8825_int_status_clear_all(regmap);
2363 
2364     /* Enable both insertion and ejection interruptions, and then
2365      * bypass de-bounce circuit.
2366      */
2367     regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
2368         NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_HEADSET_COMPLETE_EN |
2369         NAU8825_IRQ_EJECT_EN | NAU8825_IRQ_INSERT_EN,
2370         NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_HEADSET_COMPLETE_EN);
2371     regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
2372         NAU8825_JACK_DET_DB_BYPASS, NAU8825_JACK_DET_DB_BYPASS);
2373     regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_DIS_CTRL,
2374         NAU8825_IRQ_INSERT_DIS | NAU8825_IRQ_EJECT_DIS, 0);
2375 
2376     return 0;
2377 }
2378 
2379 static int nau8825_set_bias_level(struct snd_soc_component *component,
2380                    enum snd_soc_bias_level level)
2381 {
2382     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
2383     int ret;
2384 
2385     switch (level) {
2386     case SND_SOC_BIAS_ON:
2387         break;
2388 
2389     case SND_SOC_BIAS_PREPARE:
2390         break;
2391 
2392     case SND_SOC_BIAS_STANDBY:
2393         if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
2394             if (nau8825->mclk_freq) {
2395                 ret = clk_prepare_enable(nau8825->mclk);
2396                 if (ret) {
2397                     dev_err(nau8825->dev, "Unable to prepare component mclk\n");
2398                     return ret;
2399                 }
2400             }
2401             /* Setup codec configuration after resume */
2402             nau8825_resume_setup(nau8825);
2403         }
2404         break;
2405 
2406     case SND_SOC_BIAS_OFF:
2407         /* Reset the configuration of jack type for detection */
2408         /* Detach 2kOhm Resistors from MICBIAS to MICGND1/2 */
2409         regmap_update_bits(nau8825->regmap, NAU8825_REG_MIC_BIAS,
2410             NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2, 0);
2411         /* ground HPL/HPR, MICGRND1/2 */
2412         regmap_update_bits(nau8825->regmap,
2413             NAU8825_REG_HSD_CTRL, 0xf, 0xf);
2414         /* Cancel and reset cross talk detection funciton */
2415         nau8825_xtalk_cancel(nau8825);
2416         /* Turn off all interruptions before system shutdown. Keep the
2417          * interruption quiet before resume setup completes.
2418          */
2419         regmap_write(nau8825->regmap,
2420             NAU8825_REG_INTERRUPT_DIS_CTRL, 0xffff);
2421         /* Disable ADC needed for interruptions at audo mode */
2422         regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL,
2423             NAU8825_ENABLE_ADC, 0);
2424         if (nau8825->mclk_freq)
2425             clk_disable_unprepare(nau8825->mclk);
2426         break;
2427     }
2428     return 0;
2429 }
2430 
2431 static int __maybe_unused nau8825_suspend(struct snd_soc_component *component)
2432 {
2433     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
2434 
2435     disable_irq(nau8825->irq);
2436     snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
2437     /* Power down codec power; don't suppoet button wakeup */
2438     snd_soc_dapm_disable_pin(nau8825->dapm, "SAR");
2439     snd_soc_dapm_disable_pin(nau8825->dapm, "MICBIAS");
2440     snd_soc_dapm_sync(nau8825->dapm);
2441     regcache_cache_only(nau8825->regmap, true);
2442     regcache_mark_dirty(nau8825->regmap);
2443 
2444     return 0;
2445 }
2446 
2447 static int __maybe_unused nau8825_resume(struct snd_soc_component *component)
2448 {
2449     struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
2450     int ret;
2451 
2452     regcache_cache_only(nau8825->regmap, false);
2453     regcache_sync(nau8825->regmap);
2454     nau8825->xtalk_protect = true;
2455     ret = nau8825_sema_acquire(nau8825, 0);
2456     if (ret)
2457         nau8825->xtalk_protect = false;
2458     enable_irq(nau8825->irq);
2459 
2460     return 0;
2461 }
2462 
2463 static int nau8825_set_jack(struct snd_soc_component *component,
2464                 struct snd_soc_jack *jack, void *data)
2465 {
2466     return nau8825_enable_jack_detect(component, jack);
2467 }
2468 
2469 static const struct snd_soc_component_driver nau8825_component_driver = {
2470     .probe          = nau8825_component_probe,
2471     .remove         = nau8825_component_remove,
2472     .set_sysclk     = nau8825_set_sysclk,
2473     .set_pll        = nau8825_set_pll,
2474     .set_bias_level     = nau8825_set_bias_level,
2475     .suspend        = nau8825_suspend,
2476     .resume         = nau8825_resume,
2477     .controls       = nau8825_controls,
2478     .num_controls       = ARRAY_SIZE(nau8825_controls),
2479     .dapm_widgets       = nau8825_dapm_widgets,
2480     .num_dapm_widgets   = ARRAY_SIZE(nau8825_dapm_widgets),
2481     .dapm_routes        = nau8825_dapm_routes,
2482     .num_dapm_routes    = ARRAY_SIZE(nau8825_dapm_routes),
2483     .set_jack       = nau8825_set_jack,
2484     .suspend_bias_off   = 1,
2485     .idle_bias_on       = 1,
2486     .use_pmdown_time    = 1,
2487     .endianness     = 1,
2488 };
2489 
2490 static void nau8825_reset_chip(struct regmap *regmap)
2491 {
2492     regmap_write(regmap, NAU8825_REG_RESET, 0x00);
2493     regmap_write(regmap, NAU8825_REG_RESET, 0x00);
2494 }
2495 
2496 static void nau8825_print_device_properties(struct nau8825 *nau8825)
2497 {
2498     int i;
2499     struct device *dev = nau8825->dev;
2500 
2501     dev_dbg(dev, "jkdet-enable:         %d\n", nau8825->jkdet_enable);
2502     dev_dbg(dev, "jkdet-pull-enable:    %d\n", nau8825->jkdet_pull_enable);
2503     dev_dbg(dev, "jkdet-pull-up:        %d\n", nau8825->jkdet_pull_up);
2504     dev_dbg(dev, "jkdet-polarity:       %d\n", nau8825->jkdet_polarity);
2505     dev_dbg(dev, "micbias-voltage:      %d\n", nau8825->micbias_voltage);
2506     dev_dbg(dev, "vref-impedance:       %d\n", nau8825->vref_impedance);
2507 
2508     dev_dbg(dev, "sar-threshold-num:    %d\n", nau8825->sar_threshold_num);
2509     for (i = 0; i < nau8825->sar_threshold_num; i++)
2510         dev_dbg(dev, "sar-threshold[%d]=%d\n", i,
2511                 nau8825->sar_threshold[i]);
2512 
2513     dev_dbg(dev, "sar-hysteresis:       %d\n", nau8825->sar_hysteresis);
2514     dev_dbg(dev, "sar-voltage:          %d\n", nau8825->sar_voltage);
2515     dev_dbg(dev, "sar-compare-time:     %d\n", nau8825->sar_compare_time);
2516     dev_dbg(dev, "sar-sampling-time:    %d\n", nau8825->sar_sampling_time);
2517     dev_dbg(dev, "short-key-debounce:   %d\n", nau8825->key_debounce);
2518     dev_dbg(dev, "jack-insert-debounce: %d\n",
2519             nau8825->jack_insert_debounce);
2520     dev_dbg(dev, "jack-eject-debounce:  %d\n",
2521             nau8825->jack_eject_debounce);
2522     dev_dbg(dev, "crosstalk-enable:     %d\n",
2523             nau8825->xtalk_enable);
2524 }
2525 
2526 static int nau8825_read_device_properties(struct device *dev,
2527     struct nau8825 *nau8825) {
2528     int ret;
2529 
2530     nau8825->jkdet_enable = device_property_read_bool(dev,
2531         "nuvoton,jkdet-enable");
2532     nau8825->jkdet_pull_enable = device_property_read_bool(dev,
2533         "nuvoton,jkdet-pull-enable");
2534     nau8825->jkdet_pull_up = device_property_read_bool(dev,
2535         "nuvoton,jkdet-pull-up");
2536     ret = device_property_read_u32(dev, "nuvoton,jkdet-polarity",
2537         &nau8825->jkdet_polarity);
2538     if (ret)
2539         nau8825->jkdet_polarity = 1;
2540     ret = device_property_read_u32(dev, "nuvoton,micbias-voltage",
2541         &nau8825->micbias_voltage);
2542     if (ret)
2543         nau8825->micbias_voltage = 6;
2544     ret = device_property_read_u32(dev, "nuvoton,vref-impedance",
2545         &nau8825->vref_impedance);
2546     if (ret)
2547         nau8825->vref_impedance = 2;
2548     ret = device_property_read_u32(dev, "nuvoton,sar-threshold-num",
2549         &nau8825->sar_threshold_num);
2550     if (ret)
2551         nau8825->sar_threshold_num = 4;
2552     ret = device_property_read_u32_array(dev, "nuvoton,sar-threshold",
2553         nau8825->sar_threshold, nau8825->sar_threshold_num);
2554     if (ret) {
2555         nau8825->sar_threshold[0] = 0x08;
2556         nau8825->sar_threshold[1] = 0x12;
2557         nau8825->sar_threshold[2] = 0x26;
2558         nau8825->sar_threshold[3] = 0x73;
2559     }
2560     ret = device_property_read_u32(dev, "nuvoton,sar-hysteresis",
2561         &nau8825->sar_hysteresis);
2562     if (ret)
2563         nau8825->sar_hysteresis = 0;
2564     ret = device_property_read_u32(dev, "nuvoton,sar-voltage",
2565         &nau8825->sar_voltage);
2566     if (ret)
2567         nau8825->sar_voltage = 6;
2568     ret = device_property_read_u32(dev, "nuvoton,sar-compare-time",
2569         &nau8825->sar_compare_time);
2570     if (ret)
2571         nau8825->sar_compare_time = 1;
2572     ret = device_property_read_u32(dev, "nuvoton,sar-sampling-time",
2573         &nau8825->sar_sampling_time);
2574     if (ret)
2575         nau8825->sar_sampling_time = 1;
2576     ret = device_property_read_u32(dev, "nuvoton,short-key-debounce",
2577         &nau8825->key_debounce);
2578     if (ret)
2579         nau8825->key_debounce = 3;
2580     ret = device_property_read_u32(dev, "nuvoton,jack-insert-debounce",
2581         &nau8825->jack_insert_debounce);
2582     if (ret)
2583         nau8825->jack_insert_debounce = 7;
2584     ret = device_property_read_u32(dev, "nuvoton,jack-eject-debounce",
2585         &nau8825->jack_eject_debounce);
2586     if (ret)
2587         nau8825->jack_eject_debounce = 0;
2588     nau8825->xtalk_enable = device_property_read_bool(dev,
2589         "nuvoton,crosstalk-enable");
2590 
2591     nau8825->mclk = devm_clk_get(dev, "mclk");
2592     if (PTR_ERR(nau8825->mclk) == -EPROBE_DEFER) {
2593         return -EPROBE_DEFER;
2594     } else if (PTR_ERR(nau8825->mclk) == -ENOENT) {
2595         /* The MCLK is managed externally or not used at all */
2596         nau8825->mclk = NULL;
2597         dev_info(dev, "No 'mclk' clock found, assume MCLK is managed externally");
2598     } else if (IS_ERR(nau8825->mclk)) {
2599         return -EINVAL;
2600     }
2601 
2602     return 0;
2603 }
2604 
2605 static int nau8825_setup_irq(struct nau8825 *nau8825)
2606 {
2607     int ret;
2608 
2609     ret = devm_request_threaded_irq(nau8825->dev, nau8825->irq, NULL,
2610         nau8825_interrupt, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
2611         "nau8825", nau8825);
2612 
2613     if (ret) {
2614         dev_err(nau8825->dev, "Cannot request irq %d (%d)\n",
2615             nau8825->irq, ret);
2616         return ret;
2617     }
2618 
2619     return 0;
2620 }
2621 
2622 static int nau8825_i2c_probe(struct i2c_client *i2c)
2623 {
2624     struct device *dev = &i2c->dev;
2625     struct nau8825 *nau8825 = dev_get_platdata(&i2c->dev);
2626     int ret, value;
2627 
2628     if (!nau8825) {
2629         nau8825 = devm_kzalloc(dev, sizeof(*nau8825), GFP_KERNEL);
2630         if (!nau8825)
2631             return -ENOMEM;
2632         ret = nau8825_read_device_properties(dev, nau8825);
2633         if (ret)
2634             return ret;
2635     }
2636 
2637     i2c_set_clientdata(i2c, nau8825);
2638 
2639     nau8825->regmap = devm_regmap_init_i2c(i2c, &nau8825_regmap_config);
2640     if (IS_ERR(nau8825->regmap))
2641         return PTR_ERR(nau8825->regmap);
2642     nau8825->dev = dev;
2643     nau8825->irq = i2c->irq;
2644     /* Initiate parameters, semaphore and work queue which are needed in
2645      * cross talk suppression measurment function.
2646      */
2647     nau8825->xtalk_state = NAU8825_XTALK_DONE;
2648     nau8825->xtalk_protect = false;
2649     nau8825->xtalk_baktab_initialized = false;
2650     sema_init(&nau8825->xtalk_sem, 1);
2651     INIT_WORK(&nau8825->xtalk_work, nau8825_xtalk_work);
2652 
2653     nau8825_print_device_properties(nau8825);
2654 
2655     nau8825_reset_chip(nau8825->regmap);
2656     ret = regmap_read(nau8825->regmap, NAU8825_REG_I2C_DEVICE_ID, &value);
2657     if (ret < 0) {
2658         dev_err(dev, "Failed to read device id from the NAU8825: %d\n",
2659             ret);
2660         return ret;
2661     }
2662     if ((value & NAU8825_SOFTWARE_ID_MASK) !=
2663             NAU8825_SOFTWARE_ID_NAU8825) {
2664         dev_err(dev, "Not a NAU8825 chip\n");
2665         return -ENODEV;
2666     }
2667 
2668     nau8825_init_regs(nau8825);
2669 
2670     if (i2c->irq)
2671         nau8825_setup_irq(nau8825);
2672 
2673     return devm_snd_soc_register_component(&i2c->dev,
2674         &nau8825_component_driver,
2675         &nau8825_dai, 1);
2676 }
2677 
2678 static int nau8825_i2c_remove(struct i2c_client *client)
2679 {
2680     return 0;
2681 }
2682 
2683 static const struct i2c_device_id nau8825_i2c_ids[] = {
2684     { "nau8825", 0 },
2685     { }
2686 };
2687 MODULE_DEVICE_TABLE(i2c, nau8825_i2c_ids);
2688 
2689 #ifdef CONFIG_OF
2690 static const struct of_device_id nau8825_of_ids[] = {
2691     { .compatible = "nuvoton,nau8825", },
2692     {}
2693 };
2694 MODULE_DEVICE_TABLE(of, nau8825_of_ids);
2695 #endif
2696 
2697 #ifdef CONFIG_ACPI
2698 static const struct acpi_device_id nau8825_acpi_match[] = {
2699     { "10508825", 0 },
2700     {},
2701 };
2702 MODULE_DEVICE_TABLE(acpi, nau8825_acpi_match);
2703 #endif
2704 
2705 static struct i2c_driver nau8825_driver = {
2706     .driver = {
2707         .name = "nau8825",
2708         .of_match_table = of_match_ptr(nau8825_of_ids),
2709         .acpi_match_table = ACPI_PTR(nau8825_acpi_match),
2710     },
2711     .probe_new = nau8825_i2c_probe,
2712     .remove = nau8825_i2c_remove,
2713     .id_table = nau8825_i2c_ids,
2714 };
2715 module_i2c_driver(nau8825_driver);
2716 
2717 MODULE_DESCRIPTION("ASoC nau8825 driver");
2718 MODULE_AUTHOR("Anatol Pomozov <anatol@chromium.org>");
2719 MODULE_LICENSE("GPL");