Lines Matching +full:headset +full:- +full:detect +full:- +full:gpios
1 // SPDX-License-Identifier: GPL-2.0-only
3 * rt5640.c -- RT5640/RT5639 ALSA SoC audio codec driver
28 #include <sound/soc-dapm.h>
341 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
342 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -65625, 375, 0);
343 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
344 static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -17625, 375, 0);
452 * set_dmic_clk - Set parameter of dmic.
462 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in set_dmic_clk()
466 rate = rt5640->sysclk / rl6231_get_pre_div(rt5640->regmap, in set_dmic_clk()
470 dev_err(component->dev, "Failed to set DMIC clock\n"); in set_dmic_clk()
480 struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm); in is_using_asrc()
483 if (!rt5640->asrc_en) in is_using_asrc()
922 regmap_update_bits(rt5640->regmap, RT5640_PR_BASE + in hp_amp_power_on()
924 regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M2, in hp_amp_power_on()
926 regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M1, in hp_amp_power_on()
929 regmap_write(rt5640->regmap, RT5640_PR_BASE + RT5640_HP_DCC_INT1, in hp_amp_power_on()
932 regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1, in hp_amp_power_on()
934 regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1, in hp_amp_power_on()
938 regmap_update_bits(rt5640->regmap, RT5640_PWR_ANLG1, in hp_amp_power_on()
947 regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M2, in rt5640_pmu_depop()
950 regmap_update_bits(rt5640->regmap, RT5640_CHARGE_PUMP, in rt5640_pmu_depop()
953 regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M3, in rt5640_pmu_depop()
959 regmap_write(rt5640->regmap, RT5640_PR_BASE + in rt5640_pmu_depop()
961 regmap_update_bits(rt5640->regmap, RT5640_DEPOP_M1, in rt5640_pmu_depop()
964 regmap_update_bits(rt5640->regmap, RT5640_PR_BASE + in rt5640_pmu_depop()
971 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in rt5640_hp_event()
977 rt5640->hp_mute = false; in rt5640_hp_event()
981 rt5640->hp_mute = true; in rt5640_hp_event()
995 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in rt5640_lout_event()
1024 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in rt5640_hp_power_event()
1040 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); in rt5640_hp_post_event()
1045 if (!rt5640->hp_mute) in rt5640_hp_post_event()
1652 return -EINVAL; in get_sdp_info()
1690 ret = -EINVAL; in get_sdp_info()
1700 struct snd_soc_component *component = dai->component; in rt5640_hw_params()
1705 rt5640->lrck[dai->id] = params_rate(params); in rt5640_hw_params()
1706 pre_div = rl6231_get_clk_info(rt5640->sysclk, rt5640->lrck[dai->id]); in rt5640_hw_params()
1708 dev_err(component->dev, "Unsupported clock setting %d for DAI %d\n", in rt5640_hw_params()
1709 rt5640->lrck[dai->id], dai->id); in rt5640_hw_params()
1710 return -EINVAL; in rt5640_hw_params()
1714 dev_err(component->dev, "Unsupported frame size: %d\n", frame_size); in rt5640_hw_params()
1721 rt5640->bclk[dai->id] = rt5640->lrck[dai->id] * (32 << bclk_ms); in rt5640_hw_params()
1723 dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n", in rt5640_hw_params()
1724 rt5640->bclk[dai->id], rt5640->lrck[dai->id]); in rt5640_hw_params()
1725 dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n", in rt5640_hw_params()
1726 bclk_ms, pre_div, dai->id); in rt5640_hw_params()
1741 return -EINVAL; in rt5640_hw_params()
1744 dai_sel = get_sdp_info(component, dai->id); in rt5640_hw_params()
1746 dev_err(component->dev, "Failed to get sdp info: %d\n", dai_sel); in rt5640_hw_params()
1747 return -EINVAL; in rt5640_hw_params()
1771 struct snd_soc_component *component = dai->component; in rt5640_set_dai_fmt()
1778 rt5640->master[dai->id] = 1; in rt5640_set_dai_fmt()
1782 rt5640->master[dai->id] = 0; in rt5640_set_dai_fmt()
1785 return -EINVAL; in rt5640_set_dai_fmt()
1795 return -EINVAL; in rt5640_set_dai_fmt()
1811 return -EINVAL; in rt5640_set_dai_fmt()
1814 dai_sel = get_sdp_info(component, dai->id); in rt5640_set_dai_fmt()
1816 dev_err(component->dev, "Failed to get sdp info: %d\n", dai_sel); in rt5640_set_dai_fmt()
1817 return -EINVAL; in rt5640_set_dai_fmt()
1836 struct snd_soc_component *component = dai->component; in rt5640_set_dai_sysclk()
1841 if (freq == rt5640->sysclk && clk_id == rt5640->sysclk_src) in rt5640_set_dai_sysclk()
1856 dev_err(component->dev, "Invalid clock id (%d)\n", clk_id); in rt5640_set_dai_sysclk()
1857 return -EINVAL; in rt5640_set_dai_sysclk()
1863 rt5640->sysclk = freq; in rt5640_set_dai_sysclk()
1864 rt5640->sysclk_src = clk_id; in rt5640_set_dai_sysclk()
1866 dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id); in rt5640_set_dai_sysclk()
1873 struct snd_soc_component *component = dai->component; in rt5640_set_dai_pll()
1878 if (source == rt5640->pll_src && freq_in == rt5640->pll_in && in rt5640_set_dai_pll()
1879 freq_out == rt5640->pll_out) in rt5640_set_dai_pll()
1883 dev_dbg(component->dev, "PLL disabled\n"); in rt5640_set_dai_pll()
1885 rt5640->pll_in = 0; in rt5640_set_dai_pll()
1886 rt5640->pll_out = 0; in rt5640_set_dai_pll()
1906 dev_err(component->dev, "Unknown PLL source %d\n", source); in rt5640_set_dai_pll()
1907 return -EINVAL; in rt5640_set_dai_pll()
1912 dev_err(component->dev, "Unsupport input clock %d\n", freq_in); in rt5640_set_dai_pll()
1916 dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n", in rt5640_set_dai_pll()
1926 rt5640->pll_in = freq_in; in rt5640_set_dai_pll()
1927 rt5640->pll_out = freq_out; in rt5640_set_dai_pll()
1928 rt5640->pll_src = source; in rt5640_set_dai_pll()
1951 if (IS_ERR(rt5640->mclk)) in rt5640_set_bias_level()
1955 clk_disable_unprepare(rt5640->mclk); in rt5640_set_bias_level()
1957 ret = clk_prepare_enable(rt5640->mclk); in rt5640_set_bias_level()
2005 regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1, in rt5640_dmic_enable()
2009 regmap_update_bits(rt5640->regmap, RT5640_DMIC, in rt5640_dmic_enable()
2011 regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1, in rt5640_dmic_enable()
2016 regmap_update_bits(rt5640->regmap, RT5640_DMIC, in rt5640_dmic_enable()
2018 regmap_update_bits(rt5640->regmap, RT5640_GPIO_CTRL1, in rt5640_dmic_enable()
2039 return -EINVAL; in rt5640_sel_asrc_clk_src()
2043 return -EINVAL; in rt5640_sel_asrc_clk_src()
2085 rt5640->asrc_en = true; in rt5640_sel_asrc_clk_src()
2088 rt5640->asrc_en = false; in rt5640_sel_asrc_clk_src()
2103 /* OVCD is unreliable when used with RCCLK as sysclk-source */ in rt5640_enable_micbias1_for_ovcd()
2127 rt5640->ovcd_irq_enabled = true; in rt5640_enable_micbias1_ovcd_irq()
2136 rt5640->ovcd_irq_enabled = false; in rt5640_disable_micbias1_ovcd_irq()
2150 dev_dbg(component->dev, "irq ctrl2 %#04x\n", val); in rt5640_micbias1_ovcd()
2161 dev_dbg(component->dev, "irq status %#04x\n", val); in rt5640_jack_inserted()
2163 if (rt5640->jd_inverted) in rt5640_jack_inserted()
2169 /* Jack detect and button-press timings */
2182 rt5640->poll_count = 0; in rt5640_start_button_press_work()
2183 rt5640->press_count = 0; in rt5640_start_button_press_work()
2184 rt5640->release_count = 0; in rt5640_start_button_press_work()
2185 rt5640->pressed = false; in rt5640_start_button_press_work()
2186 rt5640->press_reported = false; in rt5640_start_button_press_work()
2188 schedule_delayed_work(&rt5640->bp_work, msecs_to_jiffies(BP_POLL_TIME)); in rt5640_start_button_press_work()
2195 struct snd_soc_component *component = rt5640->component; in rt5640_button_press_work()
2202 rt5640->release_count = 0; in rt5640_button_press_work()
2203 rt5640->press_count++; in rt5640_button_press_work()
2205 if (rt5640->press_count >= BP_THRESHOLD) in rt5640_button_press_work()
2206 rt5640->pressed = true; in rt5640_button_press_work()
2209 rt5640->press_count = 0; in rt5640_button_press_work()
2210 rt5640->release_count++; in rt5640_button_press_work()
2215 * at least JACK_UNPLUG_TIME milli-seconds before reporting a press. in rt5640_button_press_work()
2217 rt5640->poll_count++; in rt5640_button_press_work()
2218 if (rt5640->poll_count < (JACK_UNPLUG_TIME / BP_POLL_TIME)) { in rt5640_button_press_work()
2219 schedule_delayed_work(&rt5640->bp_work, in rt5640_button_press_work()
2224 if (rt5640->pressed && !rt5640->press_reported) { in rt5640_button_press_work()
2225 dev_dbg(component->dev, "headset button press\n"); in rt5640_button_press_work()
2226 snd_soc_jack_report(rt5640->jack, SND_JACK_BTN_0, in rt5640_button_press_work()
2228 rt5640->press_reported = true; in rt5640_button_press_work()
2231 if (rt5640->release_count >= BP_THRESHOLD) { in rt5640_button_press_work()
2232 if (rt5640->press_reported) { in rt5640_button_press_work()
2233 dev_dbg(component->dev, "headset button release\n"); in rt5640_button_press_work()
2234 snd_soc_jack_report(rt5640->jack, 0, SND_JACK_BTN_0); in rt5640_button_press_work()
2236 /* Re-enable OVCD IRQ to detect next press */ in rt5640_button_press_work()
2241 schedule_delayed_work(&rt5640->bp_work, msecs_to_jiffies(BP_POLL_TIME)); in rt5640_button_press_work()
2253 * detect the same type JACK_DETECT_COUNT times in a row. in rt5640_detect_headset()
2256 /* Clear any previous over-current status flag */ in rt5640_detect_headset()
2271 dev_dbg(component->dev, "jack mic-gnd shorted\n"); in rt5640_detect_headset()
2277 dev_dbg(component->dev, "jack mic-gnd open\n"); in rt5640_detect_headset()
2285 …dev_err(component->dev, "Error detecting headset vs headphones, bad contact?, assuming headphones\… in rt5640_detect_headset()
2293 struct snd_soc_component *component = rt5640->component; in rt5640_jack_work()
2298 if (rt5640->jack->status & SND_JACK_HEADPHONE) { in rt5640_jack_work()
2299 if (rt5640->jack->status & SND_JACK_MICROPHONE) { in rt5640_jack_work()
2300 cancel_delayed_work_sync(&rt5640->bp_work); in rt5640_jack_work()
2304 snd_soc_jack_report(rt5640->jack, 0, in rt5640_jack_work()
2306 dev_dbg(component->dev, "jack unplugged\n"); in rt5640_jack_work()
2308 } else if (!(rt5640->jack->status & SND_JACK_HEADPHONE)) { in rt5640_jack_work()
2310 WARN_ON(rt5640->ovcd_irq_enabled); in rt5640_jack_work()
2314 /* Enable ovcd IRQ for button press detect. */ in rt5640_jack_work()
2317 /* No more need for overcurrent detect. */ in rt5640_jack_work()
2320 dev_dbg(component->dev, "detect status %#02x\n", status); in rt5640_jack_work()
2321 snd_soc_jack_report(rt5640->jack, status, SND_JACK_HEADSET); in rt5640_jack_work()
2322 } else if (rt5640->ovcd_irq_enabled && rt5640_micbias1_ovcd(component)) { in rt5640_jack_work()
2323 dev_dbg(component->dev, "OVCD IRQ\n"); in rt5640_jack_work()
2330 * IRQs on both edges (so as to detect both jack plugin and in rt5640_jack_work()
2338 * If the jack-detect IRQ flag goes high (unplug) after our in rt5640_jack_work()
2341 * we react to edges, we miss the unplug event -> recheck. in rt5640_jack_work()
2343 queue_work(system_long_wq, &rt5640->jack_work); in rt5640_jack_work()
2351 if (rt5640->jack) in rt5640_irq()
2352 queue_work(system_long_wq, &rt5640->jack_work); in rt5640_irq()
2361 cancel_work_sync(&rt5640->jack_work); in rt5640_cancel_work()
2362 cancel_delayed_work_sync(&rt5640->bp_work); in rt5640_cancel_work()
2370 /* Select JD-source */ in rt5640_enable_jack_detect()
2372 RT5640_JD_MASK, rt5640->jd_src); in rt5640_enable_jack_detect()
2389 0xa800 | rt5640->ovcd_sf); in rt5640_enable_jack_detect()
2393 rt5640->ovcd_th | RT5640_MIC1_OVCD_EN); in rt5640_enable_jack_detect()
2396 * The over-current-detect is only reliable in detecting the absence in rt5640_enable_jack_detect()
2397 * of over-current, when the mic-contact in the jack is short-circuited, in rt5640_enable_jack_detect()
2398 * the hardware periodically retries if it can apply the bias-current in rt5640_enable_jack_detect()
2399 * leading to the ovcd status flip-flopping 1-0-1 with it being 0 about in rt5640_enable_jack_detect()
2408 * All IRQs get or-ed together, so we need the jack IRQ to report 0 in rt5640_enable_jack_detect()
2413 if (rt5640->jd_inverted) in rt5640_enable_jack_detect()
2420 rt5640->jack = jack; in rt5640_enable_jack_detect()
2421 if (rt5640->jack->status & SND_JACK_MICROPHONE) { in rt5640_enable_jack_detect()
2426 enable_irq(rt5640->irq); in rt5640_enable_jack_detect()
2428 queue_work(system_long_wq, &rt5640->jack_work); in rt5640_enable_jack_detect()
2436 * soc_remove_component() force-disables jack and thus rt5640->jack in rt5640_disable_jack_detect()
2439 if (!rt5640->jack) in rt5640_disable_jack_detect()
2442 disable_irq(rt5640->irq); in rt5640_disable_jack_detect()
2445 if (rt5640->jack->status & SND_JACK_MICROPHONE) { in rt5640_disable_jack_detect()
2448 snd_soc_jack_report(rt5640->jack, 0, SND_JACK_BTN_0); in rt5640_disable_jack_detect()
2451 rt5640->jack = NULL; in rt5640_disable_jack_detect()
2475 rt5640->mclk = devm_clk_get(component->dev, "mclk"); in rt5640_probe()
2476 if (PTR_ERR(rt5640->mclk) == -EPROBE_DEFER) in rt5640_probe()
2477 return -EPROBE_DEFER; in rt5640_probe()
2479 rt5640->component = component; in rt5640_probe()
2509 dev_err(component->dev, in rt5640_probe()
2511 return -ENODEV; in rt5640_probe()
2515 * Note on some platforms the platform code may need to add device-props in rt5640_probe()
2518 * rt5640_i2c_probe(), so that the platform-code can attach extra in rt5640_probe()
2521 if (device_property_read_bool(component->dev, "realtek,in1-differential")) in rt5640_probe()
2525 if (device_property_read_bool(component->dev, "realtek,in2-differential")) in rt5640_probe()
2529 if (device_property_read_bool(component->dev, "realtek,in3-differential")) in rt5640_probe()
2533 if (device_property_read_u32(component->dev, "realtek,dmic1-data-pin", in rt5640_probe()
2535 dmic1_data_pin = val - 1; in rt5640_probe()
2539 if (device_property_read_u32(component->dev, "realtek,dmic2-data-pin", in rt5640_probe()
2541 dmic2_data_pin = val - 1; in rt5640_probe()
2548 if (device_property_read_u32(component->dev, in rt5640_probe()
2549 "realtek,jack-detect-source", &val) == 0) { in rt5640_probe()
2551 rt5640->jd_src = val << RT5640_JD_SFT; in rt5640_probe()
2553 …dev_warn(component->dev, "Warning: Invalid jack-detect-source value: %d, leaving jack-detect disab… in rt5640_probe()
2557 if (!device_property_read_bool(component->dev, "realtek,jack-detect-not-inverted")) in rt5640_probe()
2558 rt5640->jd_inverted = true; in rt5640_probe()
2562 * threshold and scale-factor are 2000µA and 0.75. For an effective in rt5640_probe()
2565 rt5640->ovcd_th = RT5640_MIC1_OVTH_2000UA; in rt5640_probe()
2566 rt5640->ovcd_sf = RT5640_MIC_OVCD_SF_0P75; in rt5640_probe()
2568 if (device_property_read_u32(component->dev, in rt5640_probe()
2569 "realtek,over-current-threshold-microamp", &val) == 0) { in rt5640_probe()
2572 rt5640->ovcd_th = RT5640_MIC1_OVTH_600UA; in rt5640_probe()
2575 rt5640->ovcd_th = RT5640_MIC1_OVTH_1500UA; in rt5640_probe()
2578 rt5640->ovcd_th = RT5640_MIC1_OVTH_2000UA; in rt5640_probe()
2581 …dev_warn(component->dev, "Warning: Invalid over-current-threshold-microamp value: %d, defaulting t… in rt5640_probe()
2586 if (device_property_read_u32(component->dev, in rt5640_probe()
2587 "realtek,over-current-scale-factor", &val) == 0) { in rt5640_probe()
2589 rt5640->ovcd_sf = val << RT5640_MIC_OVCD_SF_SFT; in rt5640_probe()
2591 …dev_warn(component->dev, "Warning: Invalid over-current-scale-factor value: %d, defaulting to 0.75… in rt5640_probe()
2610 regcache_cache_only(rt5640->regmap, true); in rt5640_suspend()
2611 regcache_mark_dirty(rt5640->regmap); in rt5640_suspend()
2612 if (gpio_is_valid(rt5640->ldo1_en)) in rt5640_suspend()
2613 gpio_set_value_cansleep(rt5640->ldo1_en, 0); in rt5640_suspend()
2622 if (gpio_is_valid(rt5640->ldo1_en)) { in rt5640_resume()
2623 gpio_set_value_cansleep(rt5640->ldo1_en, 1); in rt5640_resume()
2627 regcache_cache_only(rt5640->regmap, false); in rt5640_resume()
2628 regcache_sync(rt5640->regmap); in rt5640_resume()
2650 .name = "rt5640-aif1",
2669 .name = "rt5640-aif2",
2757 rt5640->ldo1_en = of_get_named_gpio(np, "realtek,ldo1-en-gpios", 0); in rt5640_parse_dt()
2760 * -ENOENT means that the property doesn't exist, i.e. there is no in rt5640_parse_dt()
2764 if (!gpio_is_valid(rt5640->ldo1_en) && in rt5640_parse_dt()
2765 (rt5640->ldo1_en != -ENOENT)) in rt5640_parse_dt()
2766 return rt5640->ldo1_en; in rt5640_parse_dt()
2778 rt5640 = devm_kzalloc(&i2c->dev, in rt5640_i2c_probe()
2782 return -ENOMEM; in rt5640_i2c_probe()
2785 if (i2c->dev.of_node) { in rt5640_i2c_probe()
2786 ret = rt5640_parse_dt(rt5640, i2c->dev.of_node); in rt5640_i2c_probe()
2790 rt5640->ldo1_en = -EINVAL; in rt5640_i2c_probe()
2792 rt5640->regmap = devm_regmap_init_i2c(i2c, &rt5640_regmap); in rt5640_i2c_probe()
2793 if (IS_ERR(rt5640->regmap)) { in rt5640_i2c_probe()
2794 ret = PTR_ERR(rt5640->regmap); in rt5640_i2c_probe()
2795 dev_err(&i2c->dev, "Failed to allocate register map: %d\n", in rt5640_i2c_probe()
2800 if (gpio_is_valid(rt5640->ldo1_en)) { in rt5640_i2c_probe()
2801 ret = devm_gpio_request_one(&i2c->dev, rt5640->ldo1_en, in rt5640_i2c_probe()
2805 dev_err(&i2c->dev, "Failed to request LDO1_EN %d: %d\n", in rt5640_i2c_probe()
2806 rt5640->ldo1_en, ret); in rt5640_i2c_probe()
2812 regmap_read(rt5640->regmap, RT5640_VENDOR_ID2, &val); in rt5640_i2c_probe()
2814 dev_err(&i2c->dev, in rt5640_i2c_probe()
2816 return -ENODEV; in rt5640_i2c_probe()
2819 regmap_write(rt5640->regmap, RT5640_RESET, 0); in rt5640_i2c_probe()
2821 ret = regmap_register_patch(rt5640->regmap, init_list, in rt5640_i2c_probe()
2824 dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret); in rt5640_i2c_probe()
2826 regmap_update_bits(rt5640->regmap, RT5640_DUMMY1, in rt5640_i2c_probe()
2829 rt5640->hp_mute = true; in rt5640_i2c_probe()
2830 rt5640->irq = i2c->irq; in rt5640_i2c_probe()
2831 INIT_DELAYED_WORK(&rt5640->bp_work, rt5640_button_press_work); in rt5640_i2c_probe()
2832 INIT_WORK(&rt5640->jack_work, rt5640_jack_work); in rt5640_i2c_probe()
2834 /* Make sure work is stopped on probe-error / remove */ in rt5640_i2c_probe()
2835 ret = devm_add_action_or_reset(&i2c->dev, rt5640_cancel_work, rt5640); in rt5640_i2c_probe()
2839 ret = devm_request_irq(&i2c->dev, rt5640->irq, rt5640_irq, in rt5640_i2c_probe()
2843 /* Gets re-enabled by rt5640_set_jack() */ in rt5640_i2c_probe()
2844 disable_irq(rt5640->irq); in rt5640_i2c_probe()
2846 dev_warn(&i2c->dev, "Failed to reguest IRQ %d: %d\n", in rt5640_i2c_probe()
2847 rt5640->irq, ret); in rt5640_i2c_probe()
2848 rt5640->irq = -ENXIO; in rt5640_i2c_probe()
2851 return devm_snd_soc_register_component(&i2c->dev, in rt5640_i2c_probe()