Lines Matching +full:dsp +full:- +full:aif1 +full:- +full:bclk

1 // SPDX-License-Identifier: GPL-2.0-only
3 * rt5651.c -- RT5651 ALSA SoC audio codec driver
23 #include <sound/soc-dapm.h>
296 ucontrol->value.integer.value[0] = rt5651->asrc_en; in rt5651_asrc_get()
307 rt5651->asrc_en = ucontrol->value.integer.value[0]; in rt5651_asrc_put()
308 if (rt5651->asrc_en) { in rt5651_asrc_put()
309 regmap_write(rt5651->regmap, 0x80, 0x4000); in rt5651_asrc_put()
310 regmap_write(rt5651->regmap, 0x81, 0x0302); in rt5651_asrc_put()
311 regmap_write(rt5651->regmap, 0x82, 0x0800); in rt5651_asrc_put()
312 regmap_write(rt5651->regmap, 0x73, 0x1004); in rt5651_asrc_put()
313 regmap_write(rt5651->regmap, 0x83, 0x1000); in rt5651_asrc_put()
314 regmap_write(rt5651->regmap, 0x84, 0x7000); in rt5651_asrc_put()
318 regmap_write(rt5651->regmap, 0x83, 0x0); in rt5651_asrc_put()
319 regmap_write(rt5651->regmap, 0x84, 0x0); in rt5651_asrc_put()
324 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
325 static const DECLARE_TLV_DB_MINMAX(dac_vol_tlv, -6562, 0);
326 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
327 static const DECLARE_TLV_DB_MINMAX(adc_vol_tlv, -1762, 3000);
413 * set_dmic_clk - Set parameter of dmic.
423 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in set_dmic_clk()
427 rate = rt5651->sysclk / rl6231_get_pre_div(rt5651->regmap, in set_dmic_clk()
431 dev_err(component->dev, "Failed to set DMIC clock\n"); in set_dmic_clk()
728 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in rt5651_amp_power_event()
734 regmap_update_bits(rt5651->regmap, RT5651_PR_BASE + in rt5651_amp_power_event()
736 regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M2, in rt5651_amp_power_event()
738 regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M1, in rt5651_amp_power_event()
742 regmap_write(rt5651->regmap, RT5651_PR_BASE + in rt5651_amp_power_event()
745 regmap_update_bits(rt5651->regmap, RT5651_PWR_ANLG1, in rt5651_amp_power_event()
747 regmap_update_bits(rt5651->regmap, RT5651_PWR_ANLG1, in rt5651_amp_power_event()
751 regmap_update_bits(rt5651->regmap, RT5651_PWR_ANLG1, in rt5651_amp_power_event()
766 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in rt5651_hp_event()
772 regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M2, in rt5651_hp_event()
775 regmap_update_bits(rt5651->regmap, RT5651_CHARGE_PUMP, in rt5651_hp_event()
778 regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M3, in rt5651_hp_event()
785 regmap_write(rt5651->regmap, RT5651_PR_BASE + in rt5651_hp_event()
787 regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M1, in rt5651_hp_event()
790 regmap_update_bits(rt5651->regmap, RT5651_PR_BASE + in rt5651_hp_event()
792 rt5651->hp_mute = false; in rt5651_hp_event()
796 rt5651->hp_mute = true; in rt5651_hp_event()
811 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in rt5651_hp_post_event()
816 if (!rt5651->hp_mute) in rt5651_hp_post_event()
831 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in rt5651_bst1_event()
854 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in rt5651_bst2_event()
877 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in rt5651_bst3_event()
1020 SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1021 SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
1025 /* Audio DSP */
1026 SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),
1235 {"Audio DSP", NULL, "DAC MIXL"},
1236 {"Audio DSP", NULL, "DAC MIXR"},
1246 {"Stereo DAC MIXL", "DAC L1 Switch", "Audio DSP"},
1251 {"Stereo DAC MIXR", "DAC R1 Switch", "Audio DSP"},
1328 struct snd_soc_component *component = dai->component; in rt5651_hw_params()
1333 rt5651->lrck[dai->id] = params_rate(params); in rt5651_hw_params()
1334 pre_div = rl6231_get_clk_info(rt5651->sysclk, rt5651->lrck[dai->id]); in rt5651_hw_params()
1337 dev_err(component->dev, "Unsupported clock setting\n"); in rt5651_hw_params()
1338 return -EINVAL; in rt5651_hw_params()
1342 dev_err(component->dev, "Unsupported frame size: %d\n", frame_size); in rt5651_hw_params()
1343 return -EINVAL; in rt5651_hw_params()
1346 rt5651->bclk[dai->id] = rt5651->lrck[dai->id] * (32 << bclk_ms); in rt5651_hw_params()
1348 dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n", in rt5651_hw_params()
1349 rt5651->bclk[dai->id], rt5651->lrck[dai->id]); in rt5651_hw_params()
1350 dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n", in rt5651_hw_params()
1351 bclk_ms, pre_div, dai->id); in rt5651_hw_params()
1366 return -EINVAL; in rt5651_hw_params()
1369 switch (dai->id) { in rt5651_hw_params()
1385 dev_err(component->dev, "Wrong dai->id: %d\n", dai->id); in rt5651_hw_params()
1386 return -EINVAL; in rt5651_hw_params()
1394 struct snd_soc_component *component = dai->component; in rt5651_set_dai_fmt()
1400 rt5651->master[dai->id] = 1; in rt5651_set_dai_fmt()
1404 rt5651->master[dai->id] = 0; in rt5651_set_dai_fmt()
1407 return -EINVAL; in rt5651_set_dai_fmt()
1417 return -EINVAL; in rt5651_set_dai_fmt()
1433 return -EINVAL; in rt5651_set_dai_fmt()
1436 switch (dai->id) { in rt5651_set_dai_fmt()
1448 dev_err(component->dev, "Wrong dai->id: %d\n", dai->id); in rt5651_set_dai_fmt()
1449 return -EINVAL; in rt5651_set_dai_fmt()
1457 struct snd_soc_component *component = dai->component; in rt5651_set_dai_sysclk()
1462 if (freq == rt5651->sysclk && clk_id == rt5651->sysclk_src) in rt5651_set_dai_sysclk()
1477 dev_err(component->dev, "Invalid clock id (%d)\n", clk_id); in rt5651_set_dai_sysclk()
1478 return -EINVAL; in rt5651_set_dai_sysclk()
1484 rt5651->sysclk = freq; in rt5651_set_dai_sysclk()
1485 rt5651->sysclk_src = clk_id; in rt5651_set_dai_sysclk()
1487 dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id); in rt5651_set_dai_sysclk()
1495 struct snd_soc_component *component = dai->component; in rt5651_set_dai_pll()
1500 if (source == rt5651->pll_src && freq_in == rt5651->pll_in && in rt5651_set_dai_pll()
1501 freq_out == rt5651->pll_out) in rt5651_set_dai_pll()
1505 dev_dbg(component->dev, "PLL disabled\n"); in rt5651_set_dai_pll()
1507 rt5651->pll_in = 0; in rt5651_set_dai_pll()
1508 rt5651->pll_out = 0; in rt5651_set_dai_pll()
1528 dev_err(component->dev, "Unknown PLL source %d\n", source); in rt5651_set_dai_pll()
1529 return -EINVAL; in rt5651_set_dai_pll()
1534 dev_err(component->dev, "Unsupport input clock %d\n", freq_in); in rt5651_set_dai_pll()
1538 dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n", in rt5651_set_dai_pll()
1548 rt5651->pll_in = freq_in; in rt5651_set_dai_pll()
1549 rt5651->pll_out = freq_out; in rt5651_set_dai_pll()
1550 rt5651->pll_src = source; in rt5651_set_dai_pll()
1563 if (!IS_ERR(rt5651->mclk)) in rt5651_set_bias_level()
1564 clk_prepare_enable(rt5651->mclk); in rt5651_set_bias_level()
1583 if (!IS_ERR(rt5651->mclk)) in rt5651_set_bias_level()
1584 clk_disable_unprepare(rt5651->mclk); in rt5651_set_bias_level()
1594 /* Do not touch the LDO voltage select bits on bias-off */ in rt5651_set_bias_level()
1597 /* Leave PLL1 and jack-detect power as is, all others off */ in rt5651_set_bias_level()
1616 /* OVCD is unreliable when used with RCCLK as sysclk-source */ in rt5651_enable_micbias1_for_ovcd()
1640 rt5651->ovcd_irq_enabled = true; in rt5651_enable_micbias1_ovcd_irq()
1649 rt5651->ovcd_irq_enabled = false; in rt5651_disable_micbias1_ovcd_irq()
1663 dev_dbg(component->dev, "irq ctrl2 %#04x\n", val); in rt5651_micbias1_ovcd()
1673 if (rt5651->gpiod_hp_det) { in rt5651_jack_inserted()
1674 val = gpiod_get_value_cansleep(rt5651->gpiod_hp_det); in rt5651_jack_inserted()
1675 dev_dbg(component->dev, "jack-detect gpio %d\n", val); in rt5651_jack_inserted()
1680 dev_dbg(component->dev, "irq status %#04x\n", val); in rt5651_jack_inserted()
1682 switch (rt5651->jd_src) { in rt5651_jack_inserted()
1696 if (rt5651->jd_active_high) in rt5651_jack_inserted()
1702 /* Jack detect and button-press timings */
1715 rt5651->poll_count = 0; in rt5651_start_button_press_work()
1716 rt5651->press_count = 0; in rt5651_start_button_press_work()
1717 rt5651->release_count = 0; in rt5651_start_button_press_work()
1718 rt5651->pressed = false; in rt5651_start_button_press_work()
1719 rt5651->press_reported = false; in rt5651_start_button_press_work()
1721 schedule_delayed_work(&rt5651->bp_work, msecs_to_jiffies(BP_POLL_TIME)); in rt5651_start_button_press_work()
1728 struct snd_soc_component *component = rt5651->component; in rt5651_button_press_work()
1735 rt5651->release_count = 0; in rt5651_button_press_work()
1736 rt5651->press_count++; in rt5651_button_press_work()
1738 if (rt5651->press_count >= BP_THRESHOLD) in rt5651_button_press_work()
1739 rt5651->pressed = true; in rt5651_button_press_work()
1742 rt5651->press_count = 0; in rt5651_button_press_work()
1743 rt5651->release_count++; in rt5651_button_press_work()
1748 * at least JACK_UNPLUG_TIME milli-seconds before reporting a press. in rt5651_button_press_work()
1750 rt5651->poll_count++; in rt5651_button_press_work()
1751 if (rt5651->poll_count < (JACK_UNPLUG_TIME / BP_POLL_TIME)) { in rt5651_button_press_work()
1752 schedule_delayed_work(&rt5651->bp_work, in rt5651_button_press_work()
1757 if (rt5651->pressed && !rt5651->press_reported) { in rt5651_button_press_work()
1758 dev_dbg(component->dev, "headset button press\n"); in rt5651_button_press_work()
1759 snd_soc_jack_report(rt5651->hp_jack, SND_JACK_BTN_0, in rt5651_button_press_work()
1761 rt5651->press_reported = true; in rt5651_button_press_work()
1764 if (rt5651->release_count >= BP_THRESHOLD) { in rt5651_button_press_work()
1765 if (rt5651->press_reported) { in rt5651_button_press_work()
1766 dev_dbg(component->dev, "headset button release\n"); in rt5651_button_press_work()
1767 snd_soc_jack_report(rt5651->hp_jack, 0, SND_JACK_BTN_0); in rt5651_button_press_work()
1769 /* Re-enable OVCD IRQ to detect next press */ in rt5651_button_press_work()
1774 schedule_delayed_work(&rt5651->bp_work, msecs_to_jiffies(BP_POLL_TIME)); in rt5651_button_press_work()
1789 /* Clear any previous over-current status flag */ in rt5651_detect_headset()
1804 dev_dbg(component->dev, "mic-gnd shorted\n"); in rt5651_detect_headset()
1810 dev_dbg(component->dev, "mic-gnd open\n"); in rt5651_detect_headset()
1818 …dev_err(component->dev, "Error detecting headset vs headphones, bad contact?, assuming headphones\… in rt5651_detect_headset()
1824 if (!rt5651->hp_jack) in rt5651_support_button_press()
1827 /* Button press support only works with internal jack-detection */ in rt5651_support_button_press()
1828 return (rt5651->hp_jack->status & SND_JACK_MICROPHONE) && in rt5651_support_button_press()
1829 rt5651->gpiod_hp_det == NULL; in rt5651_support_button_press()
1836 struct snd_soc_component *component = rt5651->component; in rt5651_jack_detect_work()
1841 if (rt5651->hp_jack->status & SND_JACK_HEADPHONE) { in rt5651_jack_detect_work()
1842 if (rt5651->hp_jack->status & SND_JACK_MICROPHONE) { in rt5651_jack_detect_work()
1843 cancel_delayed_work_sync(&rt5651->bp_work); in rt5651_jack_detect_work()
1847 snd_soc_jack_report(rt5651->hp_jack, 0, in rt5651_jack_detect_work()
1849 dev_dbg(component->dev, "jack unplugged\n"); in rt5651_jack_detect_work()
1851 } else if (!(rt5651->hp_jack->status & SND_JACK_HEADPHONE)) { in rt5651_jack_detect_work()
1853 WARN_ON(rt5651->ovcd_irq_enabled); in rt5651_jack_detect_work()
1856 dev_dbg(component->dev, "detect report %#02x\n", report); in rt5651_jack_detect_work()
1857 snd_soc_jack_report(rt5651->hp_jack, report, SND_JACK_HEADSET); in rt5651_jack_detect_work()
1865 } else if (rt5651->ovcd_irq_enabled && rt5651_micbias1_ovcd(component)) { in rt5651_jack_detect_work()
1866 dev_dbg(component->dev, "OVCD IRQ\n"); in rt5651_jack_detect_work()
1881 * If the jack-detect IRQ flag goes high (unplug) after our in rt5651_jack_detect_work()
1884 * we react to edges, we miss the unplug event -> recheck. in rt5651_jack_detect_work()
1886 queue_work(system_long_wq, &rt5651->jack_detect_work); in rt5651_jack_detect_work()
1894 queue_work(system_power_efficient_wq, &rt5651->jack_detect_work); in rt5651_irq()
1903 cancel_work_sync(&rt5651->jack_detect_work); in rt5651_cancel_work()
1904 cancel_delayed_work_sync(&rt5651->bp_work); in rt5651_cancel_work()
1915 switch (rt5651->jd_src) { in rt5651_enable_jack_detect()
1917 rt5651->gpiod_hp_det = gpiod_hp_det; in rt5651_enable_jack_detect()
1918 if (!rt5651->gpiod_hp_det) in rt5651_enable_jack_detect()
1925 /* active-low is normal, set inv flag for active-high */ in rt5651_enable_jack_detect()
1926 if (rt5651->jd_active_high) in rt5651_enable_jack_detect()
1940 /* active-low is normal, set inv flag for active-high */ in rt5651_enable_jack_detect()
1941 if (rt5651->jd_active_high) in rt5651_enable_jack_detect()
1955 /* active-low is normal, set inv flag for active-high */ in rt5651_enable_jack_detect()
1956 if (rt5651->jd_active_high) in rt5651_enable_jack_detect()
1968 dev_err(component->dev, "Currently only JD1_1 / JD1_2 / JD2 are supported\n"); in rt5651_enable_jack_detect()
1982 /* Set OVCD threshold current and scale-factor */ in rt5651_enable_jack_detect()
1984 0xa800 | rt5651->ovcd_sf); in rt5651_enable_jack_detect()
1992 rt5651->ovcd_th | in rt5651_enable_jack_detect()
1997 * The over-current-detect is only reliable in detecting the absence in rt5651_enable_jack_detect()
1998 * of over-current, when the mic-contact in the jack is short-circuited, in rt5651_enable_jack_detect()
1999 * the hardware periodically retries if it can apply the bias-current in rt5651_enable_jack_detect()
2000 * leading to the ovcd status flip-flopping 1-0-1 with it being 0 about in rt5651_enable_jack_detect()
2008 rt5651->hp_jack = hp_jack; in rt5651_enable_jack_detect()
2014 enable_irq(rt5651->irq); in rt5651_enable_jack_detect()
2016 queue_work(system_power_efficient_wq, &rt5651->jack_detect_work); in rt5651_enable_jack_detect()
2023 disable_irq(rt5651->irq); in rt5651_disable_jack_detect()
2029 snd_soc_jack_report(rt5651->hp_jack, 0, SND_JACK_BTN_0); in rt5651_disable_jack_detect()
2032 rt5651->hp_jack = NULL; in rt5651_disable_jack_detect()
2047 * Note on some platforms the platform code may need to add device-properties,
2050 * rather then from the i2c driver's probe function, so that the platform-code
2058 if (device_property_read_bool(component->dev, "realtek,in2-differential")) in rt5651_apply_properties()
2062 if (device_property_read_bool(component->dev, "realtek,dmic-en")) in rt5651_apply_properties()
2066 if (device_property_read_u32(component->dev, in rt5651_apply_properties()
2067 "realtek,jack-detect-source", &val) == 0) in rt5651_apply_properties()
2068 rt5651->jd_src = val; in rt5651_apply_properties()
2070 if (device_property_read_bool(component->dev, "realtek,jack-detect-not-inverted")) in rt5651_apply_properties()
2071 rt5651->jd_active_high = true; in rt5651_apply_properties()
2075 * threshold and scale-factor are 2000µA and 0.75. For an effective in rt5651_apply_properties()
2078 rt5651->ovcd_th = RT5651_MIC1_OVTH_2000UA; in rt5651_apply_properties()
2079 rt5651->ovcd_sf = RT5651_MIC_OVCD_SF_0P75; in rt5651_apply_properties()
2081 if (device_property_read_u32(component->dev, in rt5651_apply_properties()
2082 "realtek,over-current-threshold-microamp", &val) == 0) { in rt5651_apply_properties()
2085 rt5651->ovcd_th = RT5651_MIC1_OVTH_600UA; in rt5651_apply_properties()
2088 rt5651->ovcd_th = RT5651_MIC1_OVTH_1500UA; in rt5651_apply_properties()
2091 rt5651->ovcd_th = RT5651_MIC1_OVTH_2000UA; in rt5651_apply_properties()
2094 …dev_warn(component->dev, "Warning: Invalid over-current-threshold-microamp value: %d, defaulting t… in rt5651_apply_properties()
2099 if (device_property_read_u32(component->dev, in rt5651_apply_properties()
2100 "realtek,over-current-scale-factor", &val) == 0) { in rt5651_apply_properties()
2102 rt5651->ovcd_sf = val << RT5651_MIC_OVCD_SF_SFT; in rt5651_apply_properties()
2104 …dev_warn(component->dev, "Warning: Invalid over-current-scale-factor value: %d, defaulting to 0.75… in rt5651_apply_properties()
2113 rt5651->component = component; in rt5651_probe()
2115 rt5651->mclk = devm_clk_get(component->dev, "mclk"); in rt5651_probe()
2116 if (PTR_ERR(rt5651->mclk) == -EPROBE_DEFER) in rt5651_probe()
2117 return -EPROBE_DEFER; in rt5651_probe()
2131 if (!rt5651 || !rt5651->gpiod_spk_ctl) in rt5651_enable_spk()
2133 gpiod_set_value(rt5651->gpiod_spk_ctl, enable); in rt5651_enable_spk()
2138 struct snd_soc_component *component = dai->component; in rt5651_mute()
2153 regcache_cache_only(rt5651->regmap, true); in rt5651_suspend()
2154 regcache_mark_dirty(rt5651->regmap); in rt5651_suspend()
2162 regcache_cache_only(rt5651->regmap, false); in rt5651_resume()
2187 .name = "rt5651-aif1",
2190 .stream_name = "AIF1 Playback",
2197 .stream_name = "AIF1 Capture",
2206 .name = "rt5651-aif2",
2285 * Note this function MUST not look at device-properties, see the comment
2295 rt5651 = devm_kzalloc(&i2c->dev, sizeof(*rt5651), in rt5651_i2c_probe()
2298 return -ENOMEM; in rt5651_i2c_probe()
2302 rt5651->regmap = devm_regmap_init_i2c(i2c, &rt5651_regmap); in rt5651_i2c_probe()
2303 if (IS_ERR(rt5651->regmap)) { in rt5651_i2c_probe()
2304 ret = PTR_ERR(rt5651->regmap); in rt5651_i2c_probe()
2305 dev_err(&i2c->dev, "Failed to allocate register map: %d\n", in rt5651_i2c_probe()
2310 err = regmap_read(rt5651->regmap, RT5651_DEVICE_ID, &ret); in rt5651_i2c_probe()
2315 dev_err(&i2c->dev, in rt5651_i2c_probe()
2317 return -ENODEV; in rt5651_i2c_probe()
2320 regmap_write(rt5651->regmap, RT5651_RESET, 0); in rt5651_i2c_probe()
2322 ret = regmap_register_patch(rt5651->regmap, init_list, in rt5651_i2c_probe()
2325 dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret); in rt5651_i2c_probe()
2327 rt5651->irq = i2c->irq; in rt5651_i2c_probe()
2328 rt5651->hp_mute = true; in rt5651_i2c_probe()
2330 INIT_DELAYED_WORK(&rt5651->bp_work, rt5651_button_press_work); in rt5651_i2c_probe()
2331 INIT_WORK(&rt5651->jack_detect_work, rt5651_jack_detect_work); in rt5651_i2c_probe()
2333 /* Make sure work is stopped on probe-error / remove */ in rt5651_i2c_probe()
2334 ret = devm_add_action_or_reset(&i2c->dev, rt5651_cancel_work, rt5651); in rt5651_i2c_probe()
2338 ret = devm_request_irq(&i2c->dev, rt5651->irq, rt5651_irq, in rt5651_i2c_probe()
2342 /* Gets re-enabled by rt5651_set_jack() */ in rt5651_i2c_probe()
2343 disable_irq(rt5651->irq); in rt5651_i2c_probe()
2345 dev_warn(&i2c->dev, "Failed to reguest IRQ %d: %d\n", in rt5651_i2c_probe()
2346 rt5651->irq, ret); in rt5651_i2c_probe()
2347 rt5651->irq = -ENXIO; in rt5651_i2c_probe()
2349 rt5651->gpiod_spk_ctl = devm_gpiod_get(&i2c->dev, in rt5651_i2c_probe()
2350 "spk-con", in rt5651_i2c_probe()
2352 if (IS_ERR(rt5651->gpiod_spk_ctl)) { in rt5651_i2c_probe()
2353 ret = IS_ERR(rt5651->gpiod_spk_ctl); in rt5651_i2c_probe()
2354 rt5651->gpiod_spk_ctl = NULL; in rt5651_i2c_probe()
2355 dev_warn(&i2c->dev, "cannot get spk-con-gpio %d\n", ret); in rt5651_i2c_probe()
2357 ret = devm_snd_soc_register_component(&i2c->dev, in rt5651_i2c_probe()