Lines Matching +full:asrc +full:- +full:clk +full:- +full:map

1 // SPDX-License-Identifier: GPL-2.0-only
3 * rt5651.c -- RT5651 ALSA SoC audio codec driver
23 #include <sound/soc-dapm.h>
287 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
288 static const DECLARE_TLV_DB_MINMAX(dac_vol_tlv, -6562, 0);
289 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
290 static const DECLARE_TLV_DB_MINMAX(adc_vol_tlv, -1762, 3000);
356 /* ASRC */
357 SOC_SINGLE("IF1 ASRC Switch", RT5651_PLL_MODE_1,
359 SOC_SINGLE("IF2 ASRC Switch", RT5651_PLL_MODE_1,
361 SOC_SINGLE("DMIC ASRC Switch", RT5651_PLL_MODE_1,
369 * set_dmic_clk - Set parameter of dmic.
379 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
383 rate = rt5651->sysclk / rl6231_get_pre_div(rt5651->regmap,
387 dev_err(component->dev, "Failed to set DMIC clock\n");
684 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
690 regmap_update_bits(rt5651->regmap, RT5651_PR_BASE +
692 regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M2,
694 regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M1,
698 regmap_write(rt5651->regmap, RT5651_PR_BASE +
701 regmap_update_bits(rt5651->regmap, RT5651_PWR_ANLG1,
703 regmap_update_bits(rt5651->regmap, RT5651_PWR_ANLG1,
707 regmap_update_bits(rt5651->regmap, RT5651_PWR_ANLG1,
722 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
728 regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M2,
731 regmap_update_bits(rt5651->regmap, RT5651_CHARGE_PUMP,
734 regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M3,
741 regmap_write(rt5651->regmap, RT5651_PR_BASE +
743 regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M1,
746 regmap_update_bits(rt5651->regmap, RT5651_PR_BASE +
748 rt5651->hp_mute = false;
752 rt5651->hp_mute = true;
767 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
772 if (!rt5651->hp_mute)
787 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
810 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
833 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
854 /* ASRC */
855 SND_SOC_DAPM_SUPPLY_S("I2S1 ASRC", 1, RT5651_PLL_MODE_2,
857 SND_SOC_DAPM_SUPPLY_S("I2S2 ASRC", 1, RT5651_PLL_MODE_2,
859 SND_SOC_DAPM_SUPPLY_S("STO1 DAC ASRC", 1, RT5651_PLL_MODE_2,
861 SND_SOC_DAPM_SUPPLY_S("STO2 DAC ASRC", 1, RT5651_PLL_MODE_2,
863 SND_SOC_DAPM_SUPPLY_S("ADC ASRC", 1, RT5651_PLL_MODE_2,
882 SND_SOC_DAPM_SUPPLY("DMIC CLK", RT5651_DMIC, RT5651_DMIC_1_EN_SFT,
1081 {"Stero1 DAC Power", NULL, "STO1 DAC ASRC"},
1082 {"Stero2 DAC Power", NULL, "STO2 DAC ASRC"},
1083 {"I2S1", NULL, "I2S1 ASRC"},
1084 {"I2S2", NULL, "I2S2 ASRC"},
1118 {"DMIC L1", NULL, "DMIC CLK"},
1119 {"DMIC R1", NULL, "DMIC CLK"},
1144 {"Stereo1 Filter", NULL, "ADC ASRC"},
1153 {"Stereo2 Filter", NULL, "ADC ASRC"},
1284 struct snd_soc_component *component = dai->component;
1289 rt5651->lrck[dai->id] = params_rate(params);
1290 pre_div = rl6231_get_clk_info(rt5651->sysclk, rt5651->lrck[dai->id]);
1293 dev_err(component->dev, "Unsupported clock setting\n");
1294 return -EINVAL;
1298 dev_err(component->dev, "Unsupported frame size: %d\n", frame_size);
1299 return -EINVAL;
1302 rt5651->bclk[dai->id] = rt5651->lrck[dai->id] * (32 << bclk_ms);
1304 dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
1305 rt5651->bclk[dai->id], rt5651->lrck[dai->id]);
1306 dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
1307 bclk_ms, pre_div, dai->id);
1322 return -EINVAL;
1325 switch (dai->id) {
1341 dev_err(component->dev, "Wrong dai->id: %d\n", dai->id);
1342 return -EINVAL;
1350 struct snd_soc_component *component = dai->component;
1356 rt5651->master[dai->id] = 1;
1360 rt5651->master[dai->id] = 0;
1363 return -EINVAL;
1373 return -EINVAL;
1389 return -EINVAL;
1392 switch (dai->id) {
1404 dev_err(component->dev, "Wrong dai->id: %d\n", dai->id);
1405 return -EINVAL;
1413 struct snd_soc_component *component = dai->component;
1418 if (freq == rt5651->sysclk && clk_id == rt5651->sysclk_src)
1433 dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
1434 return -EINVAL;
1440 rt5651->sysclk = freq;
1441 rt5651->sysclk_src = clk_id;
1443 dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
1451 struct snd_soc_component *component = dai->component;
1456 if (source == rt5651->pll_src && freq_in == rt5651->pll_in &&
1457 freq_out == rt5651->pll_out)
1461 dev_dbg(component->dev, "PLL disabled\n");
1463 rt5651->pll_in = 0;
1464 rt5651->pll_out = 0;
1484 dev_err(component->dev, "Unknown PLL source %d\n", source);
1485 return -EINVAL;
1490 dev_err(component->dev, "Unsupported input clock %d\n", freq_in);
1494 dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
1504 rt5651->pll_in = freq_in;
1505 rt5651->pll_out = freq_out;
1506 rt5651->pll_src = source;
1543 /* Do not touch the LDO voltage select bits on bias-off */
1546 /* Leave PLL1 and jack-detect power as is, all others off */
1565 /* OVCD is unreliable when used with RCCLK as sysclk-source */
1589 rt5651->ovcd_irq_enabled = true;
1598 rt5651->ovcd_irq_enabled = false;
1612 dev_dbg(component->dev, "irq ctrl2 %#04x\n", val);
1622 if (rt5651->gpiod_hp_det) {
1623 val = gpiod_get_value_cansleep(rt5651->gpiod_hp_det);
1624 dev_dbg(component->dev, "jack-detect gpio %d\n", val);
1629 dev_dbg(component->dev, "irq status %#04x\n", val);
1631 switch (rt5651->jd_src) {
1645 if (rt5651->jd_active_high)
1651 /* Jack detect and button-press timings */
1664 rt5651->poll_count = 0;
1665 rt5651->press_count = 0;
1666 rt5651->release_count = 0;
1667 rt5651->pressed = false;
1668 rt5651->press_reported = false;
1670 schedule_delayed_work(&rt5651->bp_work, msecs_to_jiffies(BP_POLL_TIME));
1677 struct snd_soc_component *component = rt5651->component;
1684 rt5651->release_count = 0;
1685 rt5651->press_count++;
1687 if (rt5651->press_count >= BP_THRESHOLD)
1688 rt5651->pressed = true;
1691 rt5651->press_count = 0;
1692 rt5651->release_count++;
1697 * at least JACK_UNPLUG_TIME milli-seconds before reporting a press.
1699 rt5651->poll_count++;
1700 if (rt5651->poll_count < (JACK_UNPLUG_TIME / BP_POLL_TIME)) {
1701 schedule_delayed_work(&rt5651->bp_work,
1706 if (rt5651->pressed && !rt5651->press_reported) {
1707 dev_dbg(component->dev, "headset button press\n");
1708 snd_soc_jack_report(rt5651->hp_jack, SND_JACK_BTN_0,
1710 rt5651->press_reported = true;
1713 if (rt5651->release_count >= BP_THRESHOLD) {
1714 if (rt5651->press_reported) {
1715 dev_dbg(component->dev, "headset button release\n");
1716 snd_soc_jack_report(rt5651->hp_jack, 0, SND_JACK_BTN_0);
1718 /* Re-enable OVCD IRQ to detect next press */
1723 schedule_delayed_work(&rt5651->bp_work, msecs_to_jiffies(BP_POLL_TIME));
1738 /* Clear any previous over-current status flag */
1753 dev_dbg(component->dev, "mic-gnd shorted\n");
1759 dev_dbg(component->dev, "mic-gnd open\n");
1767 dev_err(component->dev, "Error detecting headset vs headphones, bad contact?, assuming headphones\n");
1773 if (!rt5651->hp_jack)
1776 /* Button press support only works with internal jack-detection */
1777 return (rt5651->hp_jack->status & SND_JACK_MICROPHONE) &&
1778 rt5651->gpiod_hp_det == NULL;
1785 struct snd_soc_component *component = rt5651->component;
1790 if (rt5651->hp_jack->status & SND_JACK_HEADPHONE) {
1791 if (rt5651->hp_jack->status & SND_JACK_MICROPHONE) {
1792 cancel_delayed_work_sync(&rt5651->bp_work);
1796 snd_soc_jack_report(rt5651->hp_jack, 0,
1798 dev_dbg(component->dev, "jack unplugged\n");
1800 } else if (!(rt5651->hp_jack->status & SND_JACK_HEADPHONE)) {
1802 WARN_ON(rt5651->ovcd_irq_enabled);
1805 dev_dbg(component->dev, "detect report %#02x\n", report);
1806 snd_soc_jack_report(rt5651->hp_jack, report, SND_JACK_HEADSET);
1814 } else if (rt5651->ovcd_irq_enabled && rt5651_micbias1_ovcd(component)) {
1815 dev_dbg(component->dev, "OVCD IRQ\n");
1830 * If the jack-detect IRQ flag goes high (unplug) after our
1833 * we react to edges, we miss the unplug event -> recheck.
1835 queue_work(system_long_wq, &rt5651->jack_detect_work);
1843 queue_work(system_power_efficient_wq, &rt5651->jack_detect_work);
1852 cancel_work_sync(&rt5651->jack_detect_work);
1853 cancel_delayed_work_sync(&rt5651->bp_work);
1864 switch (rt5651->jd_src) {
1866 rt5651->gpiod_hp_det = gpiod_hp_det;
1867 if (!rt5651->gpiod_hp_det)
1874 /* active-low is normal, set inv flag for active-high */
1875 if (rt5651->jd_active_high)
1889 /* active-low is normal, set inv flag for active-high */
1890 if (rt5651->jd_active_high)
1904 /* active-low is normal, set inv flag for active-high */
1905 if (rt5651->jd_active_high)
1917 dev_err(component->dev, "Currently only JD1_1 / JD1_2 / JD2 are supported\n");
1931 /* Set OVCD threshold current and scale-factor */
1933 0xa800 | rt5651->ovcd_sf);
1941 rt5651->ovcd_th |
1946 * The over-current-detect is only reliable in detecting the absence
1947 * of over-current, when the mic-contact in the jack is short-circuited,
1948 * the hardware periodically retries if it can apply the bias-current
1949 * leading to the ovcd status flip-flopping 1-0-1 with it being 0 about
1957 rt5651->hp_jack = hp_jack;
1963 enable_irq(rt5651->irq);
1965 queue_work(system_power_efficient_wq, &rt5651->jack_detect_work);
1972 disable_irq(rt5651->irq);
1978 snd_soc_jack_report(rt5651->hp_jack, 0, SND_JACK_BTN_0);
1981 rt5651->hp_jack = NULL;
1996 * Note on some platforms the platform code may need to add device-properties,
1999 * rather then from the i2c driver's probe function, so that the platform-code
2007 if (device_property_read_bool(component->dev, "realtek,in2-differential"))
2011 if (device_property_read_bool(component->dev, "realtek,dmic-en"))
2015 if (device_property_read_u32(component->dev,
2016 "realtek,jack-detect-source", &val) == 0)
2017 rt5651->jd_src = val;
2019 if (device_property_read_bool(component->dev, "realtek,jack-detect-not-inverted"))
2020 rt5651->jd_active_high = true;
2024 * threshold and scale-factor are 2000µA and 0.75. For an effective
2027 rt5651->ovcd_th = RT5651_MIC1_OVTH_2000UA;
2028 rt5651->ovcd_sf = RT5651_MIC_OVCD_SF_0P75;
2030 if (device_property_read_u32(component->dev,
2031 "realtek,over-current-threshold-microamp", &val) == 0) {
2034 rt5651->ovcd_th = RT5651_MIC1_OVTH_600UA;
2037 rt5651->ovcd_th = RT5651_MIC1_OVTH_1500UA;
2040 rt5651->ovcd_th = RT5651_MIC1_OVTH_2000UA;
2043 dev_warn(component->dev, "Warning: Invalid over-current-threshold-microamp value: %d, defaulting to 2000uA\n",
2048 if (device_property_read_u32(component->dev,
2049 "realtek,over-current-scale-factor", &val) == 0) {
2051 rt5651->ovcd_sf = val << RT5651_MIC_OVCD_SF_SFT;
2053 dev_warn(component->dev, "Warning: Invalid over-current-scale-factor value: %d, defaulting to 0.75\n",
2062 rt5651->component = component;
2079 regcache_cache_only(rt5651->regmap, true);
2080 regcache_mark_dirty(rt5651->regmap);
2088 regcache_cache_only(rt5651->regmap, false);
2111 .name = "rt5651-aif1",
2130 .name = "rt5651-aif2",
2208 * Note this function MUST not look at device-properties, see the comment
2217 rt5651 = devm_kzalloc(&i2c->dev, sizeof(*rt5651),
2220 return -ENOMEM;
2224 rt5651->regmap = devm_regmap_init_i2c(i2c, &rt5651_regmap);
2225 if (IS_ERR(rt5651->regmap)) {
2226 ret = PTR_ERR(rt5651->regmap);
2227 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
2232 err = regmap_read(rt5651->regmap, RT5651_DEVICE_ID, &ret);
2237 dev_err(&i2c->dev,
2239 return -ENODEV;
2242 regmap_write(rt5651->regmap, RT5651_RESET, 0);
2244 ret = regmap_register_patch(rt5651->regmap, init_list,
2247 dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
2249 rt5651->irq = i2c->irq;
2250 rt5651->hp_mute = true;
2252 INIT_DELAYED_WORK(&rt5651->bp_work, rt5651_button_press_work);
2253 INIT_WORK(&rt5651->jack_detect_work, rt5651_jack_detect_work);
2255 /* Make sure work is stopped on probe-error / remove */
2256 ret = devm_add_action_or_reset(&i2c->dev, rt5651_cancel_work, rt5651);
2260 ret = devm_request_irq(&i2c->dev, rt5651->irq, rt5651_irq,
2264 dev_warn(&i2c->dev, "Failed to request IRQ %d: %d\n",
2265 rt5651->irq, ret);
2266 rt5651->irq = -ENXIO;
2269 ret = devm_snd_soc_register_component(&i2c->dev,