Lines Matching +full:digital +full:- +full:input +full:- +full:short +full:- +full:circuit +full:- +full:detection
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * wm8996.c - WM8996 audio codec interface
5 * Copyright 2011-2 Wolfson Microelectronics PLC.
108 regcache_mark_dirty(wm8996->regmap); \
299 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 150, 0);
300 static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
301 static const DECLARE_TLV_DB_SCALE(out_digital_tlv, -1200, 150, 0);
302 static const DECLARE_TLV_DB_SCALE(out_tlv, -900, 75, 0);
303 static const DECLARE_TLV_DB_SCALE(spk_tlv, -900, 150, 0);
304 static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
305 static const DECLARE_TLV_DB_SCALE(threedstereo_tlv, -1600, 183, 1);
337 struct wm8996_pdata *pdata = &wm8996->pdata;
340 if (!wm8996->num_retune_mobile_texts)
366 cfg = wm8996->retune_mobile_cfg[block];
369 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
370 if (strcmp(pdata->retune_mobile_cfgs[i].name,
371 wm8996->retune_mobile_texts[cfg]) == 0 &&
372 abs(pdata->retune_mobile_cfgs[i].rate
373 - wm8996->rx_rate[iface]) < best_val) {
375 best_val = abs(pdata->retune_mobile_cfgs[i].rate
376 - wm8996->rx_rate[iface]);
380 dev_dbg(component->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
382 pdata->retune_mobile_cfgs[best].name,
383 pdata->retune_mobile_cfgs[best].rate,
384 wm8996->rx_rate[iface]);
392 for (i = 0; i < ARRAY_SIZE(pdata->retune_mobile_cfgs[best].regs); i++)
394 pdata->retune_mobile_cfgs[best].regs[i]);
406 return -EINVAL;
414 struct wm8996_pdata *pdata = &wm8996->pdata;
415 int block = wm8996_get_retune_mobile_block(kcontrol->id.name);
416 int value = ucontrol->value.enumerated.item[0];
421 if (value >= pdata->num_retune_mobile_cfgs)
422 return -EINVAL;
424 wm8996->retune_mobile_cfg[block] = value;
436 int block = wm8996_get_retune_mobile_block(kcontrol->id.name);
440 ucontrol->value.enumerated.item[0] = wm8996->retune_mobile_cfg[block];
456 SOC_ENUM("Sidetone HPF Cut-off", sidetone_hpf),
510 SOC_DOUBLE_TLV("Digital Output 1 Volume", WM8996_DAC1_HPOUT1_VOLUME, 0, 4,
512 SOC_DOUBLE_TLV("Digital Output 2 Volume", WM8996_DAC2_HPOUT2_VOLUME, 0, 4,
578 wm8996->bg_ena++;
579 if (wm8996->bg_ena == 1) {
590 wm8996->bg_ena--;
591 if (!wm8996->bg_ena)
599 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
611 ret = -EINVAL;
634 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
640 wm8996->hpout_pending &= ~w->shift;
643 wm8996->hpout_pending |= w->shift;
647 return -EINVAL;
655 struct i2c_client *i2c = to_i2c_client(component->dev);
664 if (i2c->irq) {
665 time_left = wait_for_completion_timeout(&wm8996->dcs_done,
668 dev_err(component->dev, "DC servo timed out\n");
672 time_left--;
676 dev_dbg(component->dev, "DC servo state: %x\n", ret);
680 dev_err(component->dev, "DC servo timed out for %x\n", mask);
682 dev_dbg(component->dev, "DC servo complete for %x\n", mask);
692 if (wm8996->dcs_pending) {
693 dev_dbg(component->dev, "Starting DC servo for %x\n",
694 wm8996->dcs_pending);
697 wait_for_dc_servo(component, wm8996->dcs_pending
700 wm8996->dcs_pending = 0;
703 if (wm8996->hpout_pending != wm8996->hpout_ena) {
704 dev_dbg(component->dev, "Applying RMV_SHORTs %x->%x\n",
705 wm8996->hpout_ena, wm8996->hpout_pending);
709 if (wm8996->hpout_pending & HPOUT1L) {
718 if (wm8996->hpout_pending & HPOUT1R) {
731 if (wm8996->hpout_pending & HPOUT2L) {
740 if (wm8996->hpout_pending & HPOUT2R) {
751 wm8996->hpout_ena = wm8996->hpout_pending;
758 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
763 wm8996->dcs_pending |= 1 << w->shift;
767 return -EINVAL;
1539 if (wm8996->sysclk < 64000)
1552 bclk_rate = wm8996->bclk_rate[aif];
1557 cur_val = (wm8996->sysclk / bclk_divs[i]) - bclk_rate;
1562 bclk_rate = wm8996->sysclk / bclk_divs[best];
1563 dev_dbg(component->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
1590 ret = regulator_bulk_enable(ARRAY_SIZE(wm8996->supplies),
1591 wm8996->supplies);
1593 dev_err(component->dev,
1599 if (wm8996->ldo_ena) {
1600 gpiod_set_value_cansleep(wm8996->ldo_ena,
1605 regcache_cache_only(wm8996->regmap, false);
1606 regcache_sync(wm8996->regmap);
1617 regcache_cache_only(wm8996->regmap, true);
1618 if (wm8996->ldo_ena) {
1619 gpiod_set_value_cansleep(wm8996->ldo_ena, 0);
1620 regcache_cache_only(wm8996->regmap, true);
1622 regulator_bulk_disable(ARRAY_SIZE(wm8996->supplies),
1623 wm8996->supplies);
1632 struct snd_soc_component *component = dai->component;
1639 switch (dai->id) {
1653 WARN(1, "Invalid dai id %d\n", dai->id);
1654 return -EINVAL;
1690 return -EINVAL;
1706 return -EINVAL;
1733 struct snd_soc_component *component = dai->component;
1741 switch (dai->id) {
1743 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
1754 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
1765 WARN(1, "Invalid dai id %d\n", dai->id);
1766 return -EINVAL;
1771 dev_err(component->dev, "Unsupported BCLK rate: %d\n", bclk_rate);
1775 wm8996->bclk_rate[dai->id] = bclk_rate;
1776 wm8996->rx_rate[dai->id] = params_rate(params);
1786 if (abs(dsp_divs[i] - params_rate(params)) <
1787 abs(dsp_divs[best] - params_rate(params)))
1795 dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
1813 struct snd_soc_component *component = dai->component;
1821 if (freq == wm8996->sysclk && clk_id == wm8996->sysclk_src)
1831 wm8996->sysclk = freq;
1835 wm8996->sysclk = freq;
1839 wm8996->sysclk = freq;
1843 dev_err(component->dev, "Unsupported clock source %d\n", clk_id);
1844 return -EINVAL;
1847 switch (wm8996->sysclk) {
1856 wm8996->sysclk /= 2;
1869 dev_warn(component->dev, "Unsupported clock rate %dHz\n",
1870 wm8996->sysclk);
1871 return -EINVAL;
1885 wm8996->sysclk_src = clk_id;
1924 fll_div->fll_refclk_div = 0;
1927 fll_div->fll_refclk_div++;
1930 pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
1932 return -EINVAL;
1942 fll_div->fll_loop_gain = 5;
1944 fll_div->fll_loop_gain = 0;
1947 fll_div->fll_ref_freq = 0;
1949 fll_div->fll_ref_freq = 1;
1951 /* Fvco should be 90-100MHz; don't check the upper bound */
1958 return -EINVAL;
1962 fll_div->fll_outdiv = div - 1;
1969 fll_div->fll_fratio = fll_fratios[i].fll_fratio;
1976 return -EINVAL;
1979 fll_div->n = target / (fratio * Fref);
1982 fll_div->theta = 0;
1983 fll_div->lambda = 0;
1987 fll_div->theta = (target - (fll_div->n * fratio * Fref))
1989 fll_div->lambda = (fratio * Fref) / gcd_fll;
1993 fll_div->n, fll_div->theta, fll_div->lambda);
1995 fll_div->fll_fratio, fll_div->fll_outdiv,
1996 fll_div->fll_refclk_div);
2005 struct i2c_client *i2c = to_i2c_client(component->dev);
2011 if (source == wm8996->fll_src && Fref == wm8996->fll_fref &&
2012 Fout == wm8996->fll_fout)
2016 dev_dbg(component->dev, "FLL disabled\n");
2018 wm8996->fll_fref = 0;
2019 wm8996->fll_fout = 0;
2047 dev_err(component->dev, "Unknown FLL source %d\n", ret);
2048 return -EINVAL;
2086 try_wait_for_completion(&wm8996->fll_lock);
2091 /* The FLL supports live reconfiguration - kick that in case we were
2105 if (i2c->irq)
2112 time_left = wait_for_completion_timeout(&wm8996->fll_lock,
2115 WARN_ON(!i2c->irq);
2125 dev_err(component->dev, "Timed out waiting for FLL\n");
2126 ret = -ETIMEDOUT;
2129 dev_dbg(component->dev, "FLL configured for %dHz->%dHz\n", Fref, Fout);
2131 wm8996->fll_fref = Fref;
2132 wm8996->fll_fout = Fout;
2133 wm8996->fll_src = source;
2143 regmap_update_bits(wm8996->regmap, WM8996_GPIO_1 + offset,
2155 return regmap_update_bits(wm8996->regmap, WM8996_GPIO_1 + offset,
2166 ret = regmap_read(wm8996->regmap, WM8996_GPIO_1 + offset, ®);
2177 return regmap_update_bits(wm8996->regmap, WM8996_GPIO_1 + offset,
2192 .base = -1,
2199 wm8996->gpio_chip = wm8996_template_chip;
2200 wm8996->gpio_chip.parent = wm8996->dev;
2202 ret = gpiochip_add_data(&wm8996->gpio_chip, wm8996);
2204 dev_err(wm8996->dev, "Failed to add GPIOs: %d\n", ret);
2209 gpiochip_remove(&wm8996->gpio_chip);
2222 * wm8996_detect - Enable default WM8996 jack detection
2227 * The WM8996 has advanced accessory detection support for headsets.
2231 * This will detect headset, headphone and short circuit button and
2241 wm8996->jack = jack;
2242 wm8996->detecting = true;
2243 wm8996->polarity_cb = polarity_cb;
2244 wm8996->jack_flips = 0;
2246 if (wm8996->polarity_cb)
2247 wm8996->polarity_cb(component, 0);
2249 /* Clear discarge to avoid noise during detection */
2255 /* LDO2 powers the microphones, SYSCLK clocks detection */
2263 /* We start off just enabling microphone detection - even a
2264 * plain headphone will trigger detection.
2269 /* Slowest detection rate, gives debounce for initial detection */
2295 dev_err(component->dev, "Failed to read HPDET status\n");
2300 dev_err(component->dev, "Got HPDET IRQ but HPDET is busy\n");
2306 dev_dbg(component->dev, "HPDET measured %d ohms\n", val);
2317 if (wm8996->jack_mic)
2320 snd_soc_jack_report(wm8996->jack, report,
2323 wm8996->detecting = false;
2325 /* If the output isn't running re-clamp it */
2369 dev_dbg(component->dev, "Headphone detected\n");
2372 /* Increase the detection rate a bit for responsiveness. */
2387 dev_dbg(component->dev, "Microphone event: %x\n", val);
2390 dev_warn(component->dev, "Microphone detection state invalid\n");
2396 dev_dbg(component->dev, "Jack removal detected\n");
2397 wm8996->jack_mic = false;
2398 wm8996->detecting = true;
2399 wm8996->jack_flips = 0;
2400 snd_soc_jack_report(wm8996->jack, 0,
2417 if (wm8996->detecting) {
2418 dev_dbg(component->dev, "Microphone detected\n");
2419 wm8996->jack_mic = true;
2430 dev_dbg(component->dev, "Mic button up\n");
2431 snd_soc_jack_report(wm8996->jack, 0, SND_JACK_BTN_0);
2439 * do this for the lowest impedences to speed up detection of
2443 if (wm8996->detecting && (val & 0x3f0)) {
2444 wm8996->jack_flips++;
2446 if (wm8996->jack_flips > 1) {
2458 if (wm8996->polarity_cb)
2459 wm8996->polarity_cb(component,
2462 dev_dbg(component->dev, "Set microphone polarity to %d\n",
2472 if (wm8996->jack_mic) {
2473 dev_dbg(component->dev, "Mic button detected\n");
2474 snd_soc_jack_report(wm8996->jack, SND_JACK_BTN_0,
2476 } else if (wm8996->detecting) {
2490 dev_err(component->dev, "Failed to read IRQ status: %d\n",
2502 dev_dbg(component->dev, "DC servo IRQ\n");
2503 complete(&wm8996->dcs_done);
2507 dev_err(component->dev, "Digital core FIFO error\n");
2510 dev_dbg(component->dev, "FLL locked\n");
2511 complete(&wm8996->fll_lock);
2540 struct wm8996_pdata *pdata = &wm8996->pdata;
2544 wm8996->retune_mobile_enum,
2548 wm8996->retune_mobile_enum,
2559 wm8996->num_retune_mobile_texts = 0;
2560 wm8996->retune_mobile_texts = NULL;
2561 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
2562 for (j = 0; j < wm8996->num_retune_mobile_texts; j++) {
2563 if (strcmp(pdata->retune_mobile_cfgs[i].name,
2564 wm8996->retune_mobile_texts[j]) == 0)
2568 if (j != wm8996->num_retune_mobile_texts)
2572 t = krealloc(wm8996->retune_mobile_texts,
2574 (wm8996->num_retune_mobile_texts + 1),
2580 t[wm8996->num_retune_mobile_texts] =
2581 pdata->retune_mobile_cfgs[i].name;
2584 wm8996->num_retune_mobile_texts++;
2585 wm8996->retune_mobile_texts = t;
2588 dev_dbg(component->dev, "Allocated %d unique ReTune Mobile names\n",
2589 wm8996->num_retune_mobile_texts);
2591 wm8996->retune_mobile_enum.items = wm8996->num_retune_mobile_texts;
2592 wm8996->retune_mobile_enum.texts = wm8996->retune_mobile_texts;
2596 dev_err(component->dev,
2616 struct i2c_client *i2c = to_i2c_client(component->dev);
2619 wm8996->component = component;
2621 init_completion(&wm8996->dcs_done);
2622 init_completion(&wm8996->fll_lock);
2624 if (wm8996->pdata.num_retune_mobile_cfgs)
2630 if (i2c->irq) {
2631 if (wm8996->pdata.irq_flags)
2632 irq_flags = wm8996->pdata.irq_flags;
2639 ret = request_threaded_irq(i2c->irq, NULL,
2643 ret = request_threaded_irq(i2c->irq, NULL, wm8996_irq,
2660 dev_err(component->dev, "Failed to request IRQ: %d\n",
2671 struct i2c_client *i2c = to_i2c_client(component->dev);
2676 if (i2c->irq)
2677 free_irq(i2c->irq, component);
2711 .name = "wm8996-aif1",
2731 .name = "wm8996-aif2",
2758 wm8996 = devm_kzalloc(&i2c->dev, sizeof(struct wm8996_priv),
2761 return -ENOMEM;
2764 wm8996->dev = &i2c->dev;
2766 if (dev_get_platdata(&i2c->dev))
2767 memcpy(&wm8996->pdata, dev_get_platdata(&i2c->dev),
2768 sizeof(wm8996->pdata));
2770 wm8996->ldo_ena = devm_gpiod_get_optional(&i2c->dev, "wlf,ldo1ena",
2772 if (IS_ERR(wm8996->ldo_ena)) {
2773 ret = PTR_ERR(wm8996->ldo_ena);
2774 dev_err(&i2c->dev, "Failed to request LDO ENA GPIO: %d\n",
2778 gpiod_set_consumer_name(wm8996->ldo_ena, "WM8996 ENA");
2780 for (i = 0; i < ARRAY_SIZE(wm8996->supplies); i++)
2781 wm8996->supplies[i].supply = wm8996_supply_names[i];
2783 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8996->supplies),
2784 wm8996->supplies);
2786 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
2790 wm8996->disable_nb[0].notifier_call = wm8996_regulator_event_0;
2791 wm8996->disable_nb[1].notifier_call = wm8996_regulator_event_1;
2792 wm8996->disable_nb[2].notifier_call = wm8996_regulator_event_2;
2795 for (i = 0; i < ARRAY_SIZE(wm8996->supplies); i++) {
2797 wm8996->supplies[i].consumer,
2798 &wm8996->disable_nb[i]);
2800 dev_err(&i2c->dev,
2806 ret = regulator_bulk_enable(ARRAY_SIZE(wm8996->supplies),
2807 wm8996->supplies);
2809 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
2813 if (wm8996->ldo_ena) {
2814 gpiod_set_value_cansleep(wm8996->ldo_ena, 1);
2818 wm8996->regmap = devm_regmap_init_i2c(i2c, &wm8996_regmap);
2819 if (IS_ERR(wm8996->regmap)) {
2820 ret = PTR_ERR(wm8996->regmap);
2821 dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
2825 ret = regmap_read(wm8996->regmap, WM8996_SOFTWARE_RESET, ®);
2827 dev_err(&i2c->dev, "Failed to read ID register: %d\n", ret);
2831 dev_err(&i2c->dev, "Device is not a WM8996, ID %x\n", reg);
2832 ret = -EINVAL;
2836 ret = regmap_read(wm8996->regmap, WM8996_CHIP_REVISION, ®);
2838 dev_err(&i2c->dev, "Failed to read device revision: %d\n",
2843 dev_info(&i2c->dev, "revision %c\n",
2846 if (wm8996->ldo_ena) {
2847 gpiod_set_value_cansleep(wm8996->ldo_ena, 0);
2848 regcache_cache_only(wm8996->regmap, true);
2850 ret = regmap_write(wm8996->regmap, WM8996_SOFTWARE_RESET,
2853 dev_err(&i2c->dev, "Failed to issue reset: %d\n", ret);
2858 regulator_bulk_disable(ARRAY_SIZE(wm8996->supplies), wm8996->supplies);
2861 regmap_update_bits(wm8996->regmap, WM8996_LINE_INPUT_CONTROL,
2863 wm8996->pdata.inl_mode << WM8996_INL_MODE_SHIFT |
2864 wm8996->pdata.inr_mode);
2866 for (i = 0; i < ARRAY_SIZE(wm8996->pdata.gpio_default); i++) {
2867 if (!wm8996->pdata.gpio_default[i])
2870 regmap_write(wm8996->regmap, WM8996_GPIO_1 + i,
2871 wm8996->pdata.gpio_default[i] & 0xffff);
2874 if (wm8996->pdata.spkmute_seq)
2875 regmap_update_bits(wm8996->regmap,
2879 wm8996->pdata.spkmute_seq);
2881 regmap_update_bits(wm8996->regmap, WM8996_ACCESSORY_DETECT_MODE_2,
2883 WM8996_MICD_SRC, wm8996->pdata.micdet_def);
2886 regmap_update_bits(wm8996->regmap, WM8996_LEFT_LINE_INPUT_VOLUME,
2888 regmap_update_bits(wm8996->regmap, WM8996_RIGHT_LINE_INPUT_VOLUME,
2891 regmap_update_bits(wm8996->regmap, WM8996_DAC1_LEFT_VOLUME,
2893 regmap_update_bits(wm8996->regmap, WM8996_DAC1_RIGHT_VOLUME,
2895 regmap_update_bits(wm8996->regmap, WM8996_DAC2_LEFT_VOLUME,
2897 regmap_update_bits(wm8996->regmap, WM8996_DAC2_RIGHT_VOLUME,
2900 regmap_update_bits(wm8996->regmap, WM8996_OUTPUT1_LEFT_VOLUME,
2902 regmap_update_bits(wm8996->regmap, WM8996_OUTPUT1_RIGHT_VOLUME,
2904 regmap_update_bits(wm8996->regmap, WM8996_OUTPUT2_LEFT_VOLUME,
2906 regmap_update_bits(wm8996->regmap, WM8996_OUTPUT2_RIGHT_VOLUME,
2909 regmap_update_bits(wm8996->regmap, WM8996_DSP1_TX_LEFT_VOLUME,
2911 regmap_update_bits(wm8996->regmap, WM8996_DSP1_TX_RIGHT_VOLUME,
2913 regmap_update_bits(wm8996->regmap, WM8996_DSP2_TX_LEFT_VOLUME,
2915 regmap_update_bits(wm8996->regmap, WM8996_DSP2_TX_RIGHT_VOLUME,
2918 regmap_update_bits(wm8996->regmap, WM8996_DSP1_RX_LEFT_VOLUME,
2920 regmap_update_bits(wm8996->regmap, WM8996_DSP1_RX_RIGHT_VOLUME,
2922 regmap_update_bits(wm8996->regmap, WM8996_DSP2_RX_LEFT_VOLUME,
2924 regmap_update_bits(wm8996->regmap, WM8996_DSP2_RX_RIGHT_VOLUME,
2930 regmap_update_bits(wm8996->regmap,
2935 regmap_update_bits(wm8996->regmap,
2940 regmap_update_bits(wm8996->regmap,
2945 regmap_update_bits(wm8996->regmap,
2950 regmap_update_bits(wm8996->regmap,
2955 regmap_update_bits(wm8996->regmap,
2961 regmap_update_bits(wm8996->regmap,
2966 regmap_update_bits(wm8996->regmap,
2972 regmap_update_bits(wm8996->regmap,
2977 regmap_update_bits(wm8996->regmap,
2982 regmap_update_bits(wm8996->regmap,
2987 regmap_update_bits(wm8996->regmap,
2992 regmap_update_bits(wm8996->regmap,
2997 regmap_update_bits(wm8996->regmap,
3003 regmap_update_bits(wm8996->regmap,
3008 regmap_update_bits(wm8996->regmap,
3017 ret = regmap_read(wm8996->regmap, WM8996_GPIO_1, ®);
3019 dev_err(&i2c->dev, "Failed to read GPIO1: %d\n", ret);
3024 regmap_update_bits(wm8996->regmap, WM8996_AIF1_TX_LRCLK_2,
3028 ret = regmap_read(wm8996->regmap, WM8996_GPIO_2, ®);
3030 dev_err(&i2c->dev, "Failed to read GPIO2: %d\n", ret);
3035 regmap_update_bits(wm8996->regmap, WM8996_AIF2_TX_LRCLK_2,
3041 ret = devm_snd_soc_register_component(&i2c->dev,
3053 if (wm8996->ldo_ena)
3054 gpiod_set_value_cansleep(wm8996->ldo_ena, 0);
3055 regulator_bulk_disable(ARRAY_SIZE(wm8996->supplies), wm8996->supplies);
3067 if (wm8996->ldo_ena)
3068 gpiod_set_value_cansleep(wm8996->ldo_ena, 0);