Lines Matching +full:ena +full:- +full:gpios
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),
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++;
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;
2144 return regmap_update_bits(wm8996->regmap, WM8996_GPIO_1 + offset,
2157 return regmap_update_bits(wm8996->regmap, WM8996_GPIO_1 + offset,
2168 ret = regmap_read(wm8996->regmap, WM8996_GPIO_1 + offset, ®);
2179 return regmap_update_bits(wm8996->regmap, WM8996_GPIO_1 + offset,
2194 .base = -1,
2201 wm8996->gpio_chip = wm8996_template_chip;
2202 wm8996->gpio_chip.parent = wm8996->dev;
2204 ret = gpiochip_add_data(&wm8996->gpio_chip, wm8996);
2206 dev_err(wm8996->dev, "Failed to add GPIOs: %d\n", ret);
2211 gpiochip_remove(&wm8996->gpio_chip);
2224 * wm8996_detect - Enable default WM8996 jack detection
2243 wm8996->jack = jack;
2244 wm8996->detecting = true;
2245 wm8996->polarity_cb = polarity_cb;
2246 wm8996->jack_flips = 0;
2248 if (wm8996->polarity_cb)
2249 wm8996->polarity_cb(component, 0);
2265 /* We start off just enabling microphone detection - even a
2297 dev_err(component->dev, "Failed to read HPDET status\n");
2302 dev_err(component->dev, "Got HPDET IRQ but HPDET is busy\n");
2308 dev_dbg(component->dev, "HPDET measured %d ohms\n", val);
2319 if (wm8996->jack_mic)
2322 snd_soc_jack_report(wm8996->jack, report,
2325 wm8996->detecting = false;
2327 /* If the output isn't running re-clamp it */
2371 dev_dbg(component->dev, "Headphone detected\n");
2389 dev_dbg(component->dev, "Microphone event: %x\n", val);
2392 dev_warn(component->dev, "Microphone detection state invalid\n");
2398 dev_dbg(component->dev, "Jack removal detected\n");
2399 wm8996->jack_mic = false;
2400 wm8996->detecting = true;
2401 wm8996->jack_flips = 0;
2402 snd_soc_jack_report(wm8996->jack, 0,
2419 if (wm8996->detecting) {
2420 dev_dbg(component->dev, "Microphone detected\n");
2421 wm8996->jack_mic = true;
2432 dev_dbg(component->dev, "Mic button up\n");
2433 snd_soc_jack_report(wm8996->jack, 0, SND_JACK_BTN_0);
2445 if (wm8996->detecting && (val & 0x3f0)) {
2446 wm8996->jack_flips++;
2448 if (wm8996->jack_flips > 1) {
2460 if (wm8996->polarity_cb)
2461 wm8996->polarity_cb(component,
2464 dev_dbg(component->dev, "Set microphone polarity to %d\n",
2474 if (wm8996->jack_mic) {
2475 dev_dbg(component->dev, "Mic button detected\n");
2476 snd_soc_jack_report(wm8996->jack, SND_JACK_BTN_0,
2478 } else if (wm8996->detecting) {
2492 dev_err(component->dev, "Failed to read IRQ status: %d\n",
2504 dev_dbg(component->dev, "DC servo IRQ\n");
2505 complete(&wm8996->dcs_done);
2509 dev_err(component->dev, "Digital core FIFO error\n");
2512 dev_dbg(component->dev, "FLL locked\n");
2513 complete(&wm8996->fll_lock);
2542 struct wm8996_pdata *pdata = &wm8996->pdata;
2546 wm8996->retune_mobile_enum,
2550 wm8996->retune_mobile_enum,
2561 wm8996->num_retune_mobile_texts = 0;
2562 wm8996->retune_mobile_texts = NULL;
2563 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
2564 for (j = 0; j < wm8996->num_retune_mobile_texts; j++) {
2565 if (strcmp(pdata->retune_mobile_cfgs[i].name,
2566 wm8996->retune_mobile_texts[j]) == 0)
2570 if (j != wm8996->num_retune_mobile_texts)
2574 t = krealloc(wm8996->retune_mobile_texts,
2576 (wm8996->num_retune_mobile_texts + 1),
2582 t[wm8996->num_retune_mobile_texts] =
2583 pdata->retune_mobile_cfgs[i].name;
2586 wm8996->num_retune_mobile_texts++;
2587 wm8996->retune_mobile_texts = t;
2590 dev_dbg(component->dev, "Allocated %d unique ReTune Mobile names\n",
2591 wm8996->num_retune_mobile_texts);
2593 wm8996->retune_mobile_enum.items = wm8996->num_retune_mobile_texts;
2594 wm8996->retune_mobile_enum.texts = wm8996->retune_mobile_texts;
2598 dev_err(component->dev,
2618 struct i2c_client *i2c = to_i2c_client(component->dev);
2621 wm8996->component = component;
2623 init_completion(&wm8996->dcs_done);
2624 init_completion(&wm8996->fll_lock);
2626 if (wm8996->pdata.num_retune_mobile_cfgs)
2632 if (i2c->irq) {
2633 if (wm8996->pdata.irq_flags)
2634 irq_flags = wm8996->pdata.irq_flags;
2641 ret = request_threaded_irq(i2c->irq, NULL,
2645 ret = request_threaded_irq(i2c->irq, NULL, wm8996_irq,
2662 dev_err(component->dev, "Failed to request IRQ: %d\n",
2673 struct i2c_client *i2c = to_i2c_client(component->dev);
2678 if (i2c->irq)
2679 free_irq(i2c->irq, component);
2713 .name = "wm8996-aif1",
2733 .name = "wm8996-aif2",
2760 wm8996 = devm_kzalloc(&i2c->dev, sizeof(struct wm8996_priv),
2763 return -ENOMEM;
2766 wm8996->dev = &i2c->dev;
2768 if (dev_get_platdata(&i2c->dev))
2769 memcpy(&wm8996->pdata, dev_get_platdata(&i2c->dev),
2770 sizeof(wm8996->pdata));
2772 wm8996->ldo_ena = devm_gpiod_get_optional(&i2c->dev, "wlf,ldo1ena",
2774 if (IS_ERR(wm8996->ldo_ena)) {
2775 ret = PTR_ERR(wm8996->ldo_ena);
2776 dev_err(&i2c->dev, "Failed to request LDO ENA GPIO: %d\n",
2780 gpiod_set_consumer_name(wm8996->ldo_ena, "WM8996 ENA");
2782 for (i = 0; i < ARRAY_SIZE(wm8996->supplies); i++)
2783 wm8996->supplies[i].supply = wm8996_supply_names[i];
2785 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8996->supplies),
2786 wm8996->supplies);
2788 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
2792 wm8996->disable_nb[0].notifier_call = wm8996_regulator_event_0;
2793 wm8996->disable_nb[1].notifier_call = wm8996_regulator_event_1;
2794 wm8996->disable_nb[2].notifier_call = wm8996_regulator_event_2;
2797 for (i = 0; i < ARRAY_SIZE(wm8996->supplies); i++) {
2799 wm8996->supplies[i].consumer,
2800 &wm8996->disable_nb[i]);
2802 dev_err(&i2c->dev,
2808 ret = regulator_bulk_enable(ARRAY_SIZE(wm8996->supplies),
2809 wm8996->supplies);
2811 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
2815 if (wm8996->ldo_ena) {
2816 gpiod_set_value_cansleep(wm8996->ldo_ena, 1);
2820 wm8996->regmap = devm_regmap_init_i2c(i2c, &wm8996_regmap);
2821 if (IS_ERR(wm8996->regmap)) {
2822 ret = PTR_ERR(wm8996->regmap);
2823 dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
2827 ret = regmap_read(wm8996->regmap, WM8996_SOFTWARE_RESET, ®);
2829 dev_err(&i2c->dev, "Failed to read ID register: %d\n", ret);
2833 dev_err(&i2c->dev, "Device is not a WM8996, ID %x\n", reg);
2834 ret = -EINVAL;
2838 ret = regmap_read(wm8996->regmap, WM8996_CHIP_REVISION, ®);
2840 dev_err(&i2c->dev, "Failed to read device revision: %d\n",
2845 dev_info(&i2c->dev, "revision %c\n",
2848 if (wm8996->ldo_ena) {
2849 gpiod_set_value_cansleep(wm8996->ldo_ena, 0);
2850 regcache_cache_only(wm8996->regmap, true);
2852 ret = regmap_write(wm8996->regmap, WM8996_SOFTWARE_RESET,
2855 dev_err(&i2c->dev, "Failed to issue reset: %d\n", ret);
2860 regulator_bulk_disable(ARRAY_SIZE(wm8996->supplies), wm8996->supplies);
2863 regmap_update_bits(wm8996->regmap, WM8996_LINE_INPUT_CONTROL,
2865 wm8996->pdata.inl_mode << WM8996_INL_MODE_SHIFT |
2866 wm8996->pdata.inr_mode);
2868 for (i = 0; i < ARRAY_SIZE(wm8996->pdata.gpio_default); i++) {
2869 if (!wm8996->pdata.gpio_default[i])
2872 regmap_write(wm8996->regmap, WM8996_GPIO_1 + i,
2873 wm8996->pdata.gpio_default[i] & 0xffff);
2876 if (wm8996->pdata.spkmute_seq)
2877 regmap_update_bits(wm8996->regmap,
2881 wm8996->pdata.spkmute_seq);
2883 regmap_update_bits(wm8996->regmap, WM8996_ACCESSORY_DETECT_MODE_2,
2885 WM8996_MICD_SRC, wm8996->pdata.micdet_def);
2888 regmap_update_bits(wm8996->regmap, WM8996_LEFT_LINE_INPUT_VOLUME,
2890 regmap_update_bits(wm8996->regmap, WM8996_RIGHT_LINE_INPUT_VOLUME,
2893 regmap_update_bits(wm8996->regmap, WM8996_DAC1_LEFT_VOLUME,
2895 regmap_update_bits(wm8996->regmap, WM8996_DAC1_RIGHT_VOLUME,
2897 regmap_update_bits(wm8996->regmap, WM8996_DAC2_LEFT_VOLUME,
2899 regmap_update_bits(wm8996->regmap, WM8996_DAC2_RIGHT_VOLUME,
2902 regmap_update_bits(wm8996->regmap, WM8996_OUTPUT1_LEFT_VOLUME,
2904 regmap_update_bits(wm8996->regmap, WM8996_OUTPUT1_RIGHT_VOLUME,
2906 regmap_update_bits(wm8996->regmap, WM8996_OUTPUT2_LEFT_VOLUME,
2908 regmap_update_bits(wm8996->regmap, WM8996_OUTPUT2_RIGHT_VOLUME,
2911 regmap_update_bits(wm8996->regmap, WM8996_DSP1_TX_LEFT_VOLUME,
2913 regmap_update_bits(wm8996->regmap, WM8996_DSP1_TX_RIGHT_VOLUME,
2915 regmap_update_bits(wm8996->regmap, WM8996_DSP2_TX_LEFT_VOLUME,
2917 regmap_update_bits(wm8996->regmap, WM8996_DSP2_TX_RIGHT_VOLUME,
2920 regmap_update_bits(wm8996->regmap, WM8996_DSP1_RX_LEFT_VOLUME,
2922 regmap_update_bits(wm8996->regmap, WM8996_DSP1_RX_RIGHT_VOLUME,
2924 regmap_update_bits(wm8996->regmap, WM8996_DSP2_RX_LEFT_VOLUME,
2926 regmap_update_bits(wm8996->regmap, WM8996_DSP2_RX_RIGHT_VOLUME,
2932 regmap_update_bits(wm8996->regmap,
2937 regmap_update_bits(wm8996->regmap,
2942 regmap_update_bits(wm8996->regmap,
2947 regmap_update_bits(wm8996->regmap,
2952 regmap_update_bits(wm8996->regmap,
2957 regmap_update_bits(wm8996->regmap,
2963 regmap_update_bits(wm8996->regmap,
2968 regmap_update_bits(wm8996->regmap,
2974 regmap_update_bits(wm8996->regmap,
2979 regmap_update_bits(wm8996->regmap,
2984 regmap_update_bits(wm8996->regmap,
2989 regmap_update_bits(wm8996->regmap,
2994 regmap_update_bits(wm8996->regmap,
2999 regmap_update_bits(wm8996->regmap,
3005 regmap_update_bits(wm8996->regmap,
3010 regmap_update_bits(wm8996->regmap,
3019 ret = regmap_read(wm8996->regmap, WM8996_GPIO_1, ®);
3021 dev_err(&i2c->dev, "Failed to read GPIO1: %d\n", ret);
3026 regmap_update_bits(wm8996->regmap, WM8996_AIF1_TX_LRCLK_2,
3030 ret = regmap_read(wm8996->regmap, WM8996_GPIO_2, ®);
3032 dev_err(&i2c->dev, "Failed to read GPIO2: %d\n", ret);
3037 regmap_update_bits(wm8996->regmap, WM8996_AIF2_TX_LRCLK_2,
3043 ret = devm_snd_soc_register_component(&i2c->dev,
3055 if (wm8996->ldo_ena)
3056 gpiod_set_value_cansleep(wm8996->ldo_ena, 0);
3057 regulator_bulk_disable(ARRAY_SIZE(wm8996->supplies), wm8996->supplies);
3069 if (wm8996->ldo_ena)
3070 gpiod_set_value_cansleep(wm8996->ldo_ena, 0);