Lines Matching +full:codec +full:- +full:aif2 +full:- +full:bclk

1 // SPDX-License-Identifier: GPL-2.0-only
3 * rt5668.c -- RT5668B ALSA SoC audio component driver
26 #include <sound/soc-dapm.h>
56 int bclk[RT5668_AIFS]; member
746 static const DECLARE_TLV_DB_SCALE(hp_vol_tlv, -2250, 150, 0);
747 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -65625, 375, 0);
748 static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -17625, 375, 0);
803 * rt5668_sel_asrc_clk_src - select ASRC clock source for a set of filters
812 * for codec. This function provides an API to select the clock source for a
827 return -EINVAL; in rt5668_sel_asrc_clk_src()
887 * rt5668_headset_detect - Detect headset.
922 rt5668->jack_type = SND_JACK_HEADSET; in rt5668_headset_detect()
926 rt5668->jack_type = SND_JACK_HEADPHONE; in rt5668_headset_detect()
936 rt5668->jack_type = 0; in rt5668_headset_detect()
939 dev_dbg(component->dev, "jack_type = %d\n", rt5668->jack_type); in rt5668_headset_detect()
940 return rt5668->jack_type; in rt5668_headset_detect()
948 &rt5668->jack_detect_work, msecs_to_jiffies(250)); in rt5668_irq()
958 if (snd_soc_component_read(rt5668->component, RT5668_AJD1_CTRL) in rt5668_jd_check_handler()
961 rt5668->jack_type = rt5668_headset_detect(rt5668->component, 0); in rt5668_jd_check_handler()
963 snd_soc_jack_report(rt5668->hs_jack, rt5668->jack_type, in rt5668_jd_check_handler()
968 schedule_delayed_work(&rt5668->jd_check_work, 500); in rt5668_jd_check_handler()
977 switch (rt5668->pdata.jd_src) { in rt5668_set_jack_detect()
986 regmap_update_bits(rt5668->regmap, RT5668_GPIO_CTRL_1, in rt5668_set_jack_detect()
988 regmap_update_bits(rt5668->regmap, RT5668_RC_CLK_CTRL, in rt5668_set_jack_detect()
992 regmap_update_bits(rt5668->regmap, RT5668_PWR_ANLG_2, in rt5668_set_jack_detect()
995 regmap_update_bits(rt5668->regmap, RT5668_IRQ_CTRL_2, in rt5668_set_jack_detect()
999 &rt5668->jack_detect_work, msecs_to_jiffies(250)); in rt5668_set_jack_detect()
1003 regmap_update_bits(rt5668->regmap, RT5668_IRQ_CTRL_2, in rt5668_set_jack_detect()
1005 regmap_update_bits(rt5668->regmap, RT5668_RC_CLK_CTRL, in rt5668_set_jack_detect()
1010 dev_warn(component->dev, "Wrong JD source\n"); in rt5668_set_jack_detect()
1014 rt5668->hs_jack = hs_jack; in rt5668_set_jack_detect()
1025 if (!rt5668->component || in rt5668_jack_detect_handler()
1026 !snd_soc_card_is_instantiated(rt5668->component->card)) { in rt5668_jack_detect_handler()
1029 &rt5668->jack_detect_work, msecs_to_jiffies(15)); in rt5668_jack_detect_handler()
1033 mutex_lock(&rt5668->calibrate_mutex); in rt5668_jack_detect_handler()
1035 val = snd_soc_component_read(rt5668->component, RT5668_AJD1_CTRL) in rt5668_jack_detect_handler()
1039 if (rt5668->jack_type == 0) { in rt5668_jack_detect_handler()
1041 rt5668->jack_type = in rt5668_jack_detect_handler()
1042 rt5668_headset_detect(rt5668->component, 1); in rt5668_jack_detect_handler()
1045 rt5668->jack_type = SND_JACK_HEADSET; in rt5668_jack_detect_handler()
1046 btn_type = rt5668_button_detect(rt5668->component); in rt5668_jack_detect_handler()
1058 rt5668->jack_type |= SND_JACK_BTN_0; in rt5668_jack_detect_handler()
1063 rt5668->jack_type |= SND_JACK_BTN_1; in rt5668_jack_detect_handler()
1068 rt5668->jack_type |= SND_JACK_BTN_2; in rt5668_jack_detect_handler()
1073 rt5668->jack_type |= SND_JACK_BTN_3; in rt5668_jack_detect_handler()
1079 dev_err(rt5668->component->dev, in rt5668_jack_detect_handler()
1087 rt5668->jack_type = rt5668_headset_detect(rt5668->component, 0); in rt5668_jack_detect_handler()
1090 snd_soc_jack_report(rt5668->hs_jack, rt5668->jack_type, in rt5668_jack_detect_handler()
1095 if (rt5668->jack_type & (SND_JACK_BTN_0 | SND_JACK_BTN_1 | in rt5668_jack_detect_handler()
1097 schedule_delayed_work(&rt5668->jd_check_work, 0); in rt5668_jack_detect_handler()
1099 cancel_delayed_work_sync(&rt5668->jd_check_work); in rt5668_jack_detect_handler()
1101 mutex_unlock(&rt5668->calibrate_mutex); in rt5668_jack_detect_handler()
1135 if (rt5668->sysclk < target) { in rt5668_div_sel()
1137 rt5668->sysclk); in rt5668_div_sel()
1141 for (i = 0; i < size - 1; i++) { in rt5668_div_sel()
1143 if (target * div[i] == rt5668->sysclk) in rt5668_div_sel()
1145 if (target * div[i + 1] > rt5668->sysclk) { in rt5668_div_sel()
1147 rt5668->sysclk); in rt5668_div_sel()
1152 if (target * div[i] < rt5668->sysclk) in rt5668_div_sel()
1154 rt5668->sysclk); in rt5668_div_sel()
1156 return size - 1; in rt5668_div_sel()
1161 * set_dmic_clk - Set parameter of dmic.
1174 snd_soc_dapm_to_component(w->dapm); in set_dmic_clk()
1191 snd_soc_dapm_to_component(w->dapm); in set_filter_clk()
1198 if (w->shift == RT5668_PWR_ADC_S1F_BIT && in set_filter_clk()
1200 ref = 256 * rt5668->lrck[RT5668_AIF2]; in set_filter_clk()
1202 ref = 256 * rt5668->lrck[RT5668_AIF1]; in set_filter_clk()
1206 if (w->shift == RT5668_PWR_ADC_S1F_BIT) in set_filter_clk()
1222 snd_soc_dapm_to_component(w->dapm); in is_sys_clk_from_pll1()
1237 snd_soc_dapm_to_component(w->dapm); in is_using_asrc()
1239 switch (w->shift) { in is_using_asrc()
1313 /* MX-26 [13] [5] */
1333 /* MX-26 [11:10] [3:2] */
1353 /* MX-26 [12] [4] */
1372 /* MX-79 [6:4] I2S1 ADC data location */
1392 /* MX-2B [4], MX-2B [0]*/
1423 snd_soc_dapm_to_component(w->dapm); in rt5668_hp_event()
1472 snd_soc_dapm_to_component(w->dapm); in rt5655_set_verf()
1476 switch (w->shift) { in rt5655_set_verf()
1494 switch (w->shift) { in rt5655_set_verf()
1660 SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0,
1870 struct snd_soc_component *component = dai->component; in rt5668_set_tdm_slot()
1889 return -EINVAL; in rt5668_set_tdm_slot()
1909 return -EINVAL; in rt5668_set_tdm_slot()
1922 struct snd_soc_component *component = dai->component; in rt5668_hw_params()
1927 rt5668->lrck[dai->id] = params_rate(params); in rt5668_hw_params()
1928 pre_div = rl6231_get_clk_info(rt5668->sysclk, rt5668->lrck[dai->id]); in rt5668_hw_params()
1932 dev_err(component->dev, "Unsupported frame size: %d\n", in rt5668_hw_params()
1934 return -EINVAL; in rt5668_hw_params()
1937 dev_dbg(dai->dev, "lrck is %dHz and pre_div is %d for iis %d\n", in rt5668_hw_params()
1938 rt5668->lrck[dai->id], pre_div, dai->id); in rt5668_hw_params()
1960 return -EINVAL; in rt5668_hw_params()
1963 switch (dai->id) { in rt5668_hw_params()
1967 if (rt5668->master[RT5668_AIF1]) { in rt5668_hw_params()
1984 if (rt5668->master[RT5668_AIF2]) { in rt5668_hw_params()
1999 dev_err(component->dev, "Invalid dai->id: %d\n", dai->id); in rt5668_hw_params()
2000 return -EINVAL; in rt5668_hw_params()
2008 struct snd_soc_component *component = dai->component; in rt5668_set_dai_fmt()
2014 rt5668->master[dai->id] = 1; in rt5668_set_dai_fmt()
2017 rt5668->master[dai->id] = 0; in rt5668_set_dai_fmt()
2020 return -EINVAL; in rt5668_set_dai_fmt()
2031 if (dai->id == RT5668_AIF1) in rt5668_set_dai_fmt()
2034 return -EINVAL; in rt5668_set_dai_fmt()
2037 if (dai->id == RT5668_AIF1) in rt5668_set_dai_fmt()
2041 return -EINVAL; in rt5668_set_dai_fmt()
2044 return -EINVAL; in rt5668_set_dai_fmt()
2063 return -EINVAL; in rt5668_set_dai_fmt()
2066 switch (dai->id) { in rt5668_set_dai_fmt()
2074 tdm_ctrl | rt5668->master[dai->id]); in rt5668_set_dai_fmt()
2077 if (rt5668->master[dai->id] == 0) in rt5668_set_dai_fmt()
2084 dev_err(component->dev, "Invalid dai->id: %d\n", dai->id); in rt5668_set_dai_fmt()
2085 return -EINVAL; in rt5668_set_dai_fmt()
2096 if (freq == rt5668->sysclk && clk_id == rt5668->sysclk_src) in rt5668_set_component_sysclk()
2117 dev_err(component->dev, "Invalid clock id (%d)\n", clk_id); in rt5668_set_component_sysclk()
2118 return -EINVAL; in rt5668_set_component_sysclk()
2123 if (rt5668->master[RT5668_AIF2]) { in rt5668_set_component_sysclk()
2129 rt5668->sysclk = freq; in rt5668_set_component_sysclk()
2130 rt5668->sysclk_src = clk_id; in rt5668_set_component_sysclk()
2132 dev_dbg(component->dev, "Sysclk is %dHz and clock id is %d\n", in rt5668_set_component_sysclk()
2146 if (source == rt5668->pll_src && freq_in == rt5668->pll_in && in rt5668_set_component_pll()
2147 freq_out == rt5668->pll_out) in rt5668_set_component_pll()
2151 dev_dbg(component->dev, "PLL disabled\n"); in rt5668_set_component_pll()
2153 rt5668->pll_in = 0; in rt5668_set_component_pll()
2154 rt5668->pll_out = 0; in rt5668_set_component_pll()
2170 dev_err(component->dev, "Unknown PLL Source %d\n", source); in rt5668_set_component_pll()
2171 return -EINVAL; in rt5668_set_component_pll()
2176 dev_err(component->dev, "Unsupported input clock %d\n", freq_in); in rt5668_set_component_pll()
2180 dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n", in rt5668_set_component_pll()
2190 rt5668->pll_in = freq_in; in rt5668_set_component_pll()
2191 rt5668->pll_out = freq_out; in rt5668_set_component_pll()
2192 rt5668->pll_src = source; in rt5668_set_component_pll()
2199 struct snd_soc_component *component = dai->component; in rt5668_set_bclk_ratio()
2202 rt5668->bclk[dai->id] = ratio; in rt5668_set_bclk_ratio()
2216 dev_err(dai->dev, "Invalid bclk ratio %d\n", ratio); in rt5668_set_bclk_ratio()
2217 return -EINVAL; in rt5668_set_bclk_ratio()
2230 regmap_update_bits(rt5668->regmap, RT5668_PWR_ANLG_1, in rt5668_set_bias_level()
2233 regmap_update_bits(rt5668->regmap, RT5668_PWR_DIG_1, in rt5668_set_bias_level()
2239 regmap_update_bits(rt5668->regmap, RT5668_PWR_ANLG_1, in rt5668_set_bias_level()
2241 regmap_update_bits(rt5668->regmap, RT5668_PWR_DIG_1, in rt5668_set_bias_level()
2245 regmap_update_bits(rt5668->regmap, RT5668_PWR_DIG_1, in rt5668_set_bias_level()
2247 regmap_update_bits(rt5668->regmap, RT5668_PWR_ANLG_1, in rt5668_set_bias_level()
2262 rt5668->component = component; in rt5668_probe()
2271 rt5668_reset(rt5668->regmap); in rt5668_remove()
2279 regcache_cache_only(rt5668->regmap, true); in rt5668_suspend()
2280 regcache_mark_dirty(rt5668->regmap); in rt5668_suspend()
2288 regcache_cache_only(rt5668->regmap, false); in rt5668_resume()
2289 regcache_sync(rt5668->regmap); in rt5668_resume()
2316 .name = "rt5668-aif1",
2335 .name = "rt5668-aif2",
2338 .stream_name = "AIF2 Capture",
2389 of_property_read_u32(dev->of_node, "realtek,dmic1-data-pin", in rt5668_parse_dt()
2390 &rt5668->pdata.dmic1_data_pin); in rt5668_parse_dt()
2391 of_property_read_u32(dev->of_node, "realtek,dmic1-clk-pin", in rt5668_parse_dt()
2392 &rt5668->pdata.dmic1_clk_pin); in rt5668_parse_dt()
2393 of_property_read_u32(dev->of_node, "realtek,jd-src", in rt5668_parse_dt()
2394 &rt5668->pdata.jd_src); in rt5668_parse_dt()
2403 mutex_lock(&rt5668->calibrate_mutex); in rt5668_calibrate()
2405 rt5668_reset(rt5668->regmap); in rt5668_calibrate()
2406 regmap_write(rt5668->regmap, RT5668_PWR_ANLG_1, 0xa2bf); in rt5668_calibrate()
2408 regmap_write(rt5668->regmap, RT5668_PWR_ANLG_1, 0xf2bf); in rt5668_calibrate()
2409 regmap_write(rt5668->regmap, RT5668_MICBIAS_2, 0x0380); in rt5668_calibrate()
2410 regmap_write(rt5668->regmap, RT5668_PWR_DIG_1, 0x8001); in rt5668_calibrate()
2411 regmap_write(rt5668->regmap, RT5668_TEST_MODE_CTRL_1, 0x0000); in rt5668_calibrate()
2412 regmap_write(rt5668->regmap, RT5668_STO1_DAC_MIXER, 0x2080); in rt5668_calibrate()
2413 regmap_write(rt5668->regmap, RT5668_STO1_ADC_MIXER, 0x4040); in rt5668_calibrate()
2414 regmap_write(rt5668->regmap, RT5668_DEPOP_1, 0x0069); in rt5668_calibrate()
2415 regmap_write(rt5668->regmap, RT5668_CHOP_DAC, 0x3000); in rt5668_calibrate()
2416 regmap_write(rt5668->regmap, RT5668_HP_CTRL_2, 0x6000); in rt5668_calibrate()
2417 regmap_write(rt5668->regmap, RT5668_HP_CHARGE_PUMP_1, 0x0f26); in rt5668_calibrate()
2418 regmap_write(rt5668->regmap, RT5668_CALIB_ADC_CTRL, 0x7f05); in rt5668_calibrate()
2419 regmap_write(rt5668->regmap, RT5668_STO1_ADC_MIXER, 0x686c); in rt5668_calibrate()
2420 regmap_write(rt5668->regmap, RT5668_CAL_REC, 0x0d0d); in rt5668_calibrate()
2421 regmap_write(rt5668->regmap, RT5668_HP_CALIB_CTRL_9, 0x000f); in rt5668_calibrate()
2422 regmap_write(rt5668->regmap, RT5668_PWR_DIG_1, 0x8d01); in rt5668_calibrate()
2423 regmap_write(rt5668->regmap, RT5668_HP_CALIB_CTRL_2, 0x0321); in rt5668_calibrate()
2424 regmap_write(rt5668->regmap, RT5668_HP_LOGIC_CTRL_2, 0x0004); in rt5668_calibrate()
2425 regmap_write(rt5668->regmap, RT5668_HP_CALIB_CTRL_1, 0x7c00); in rt5668_calibrate()
2426 regmap_write(rt5668->regmap, RT5668_HP_CALIB_CTRL_3, 0x06a1); in rt5668_calibrate()
2427 regmap_write(rt5668->regmap, RT5668_A_DAC1_MUX, 0x0311); in rt5668_calibrate()
2428 regmap_write(rt5668->regmap, RT5668_RESET_HPF_CTRL, 0x0000); in rt5668_calibrate()
2429 regmap_write(rt5668->regmap, RT5668_ADC_STO1_HP_CTRL_1, 0x3320); in rt5668_calibrate()
2431 regmap_write(rt5668->regmap, RT5668_HP_CALIB_CTRL_1, 0xfc00); in rt5668_calibrate()
2434 regmap_read(rt5668->regmap, RT5668_HP_CALIB_STA_1, &value); in rt5668_calibrate()
2445 regmap_write(rt5668->regmap, RT5668_STO1_ADC_MIXER, 0xc0c4); in rt5668_calibrate()
2446 regmap_write(rt5668->regmap, RT5668_PWR_DIG_1, 0x0000); in rt5668_calibrate()
2448 mutex_unlock(&rt5668->calibrate_mutex); in rt5668_calibrate()
2454 struct rt5668_platform_data *pdata = dev_get_platdata(&i2c->dev); in rt5668_i2c_probe()
2459 rt5668 = devm_kzalloc(&i2c->dev, sizeof(struct rt5668_priv), in rt5668_i2c_probe()
2463 return -ENOMEM; in rt5668_i2c_probe()
2468 rt5668->pdata = *pdata; in rt5668_i2c_probe()
2470 rt5668_parse_dt(rt5668, &i2c->dev); in rt5668_i2c_probe()
2472 rt5668->regmap = devm_regmap_init_i2c(i2c, &rt5668_regmap); in rt5668_i2c_probe()
2473 if (IS_ERR(rt5668->regmap)) { in rt5668_i2c_probe()
2474 ret = PTR_ERR(rt5668->regmap); in rt5668_i2c_probe()
2475 dev_err(&i2c->dev, "Failed to allocate register map: %d\n", in rt5668_i2c_probe()
2480 for (i = 0; i < ARRAY_SIZE(rt5668->supplies); i++) in rt5668_i2c_probe()
2481 rt5668->supplies[i].supply = rt5668_supply_names[i]; in rt5668_i2c_probe()
2483 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(rt5668->supplies), in rt5668_i2c_probe()
2484 rt5668->supplies); in rt5668_i2c_probe()
2486 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret); in rt5668_i2c_probe()
2490 ret = regulator_bulk_enable(ARRAY_SIZE(rt5668->supplies), in rt5668_i2c_probe()
2491 rt5668->supplies); in rt5668_i2c_probe()
2493 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret); in rt5668_i2c_probe()
2497 rt5668->ldo1_en = devm_gpiod_get_optional(&i2c->dev, in rt5668_i2c_probe()
2498 "realtek,ldo1-en", in rt5668_i2c_probe()
2500 if (IS_ERR(rt5668->ldo1_en)) { in rt5668_i2c_probe()
2501 dev_err(&i2c->dev, "Fail gpio request ldo1_en\n"); in rt5668_i2c_probe()
2502 return PTR_ERR(rt5668->ldo1_en); in rt5668_i2c_probe()
2508 regmap_write(rt5668->regmap, RT5668_I2C_MODE, 0x1); in rt5668_i2c_probe()
2511 regmap_read(rt5668->regmap, RT5668_DEVICE_ID, &val); in rt5668_i2c_probe()
2514 return -ENODEV; in rt5668_i2c_probe()
2517 rt5668_reset(rt5668->regmap); in rt5668_i2c_probe()
2521 regmap_write(rt5668->regmap, RT5668_DEPOP_1, 0x0000); in rt5668_i2c_probe()
2524 if (rt5668->pdata.dmic1_data_pin != RT5668_DMIC1_NULL) { in rt5668_i2c_probe()
2525 switch (rt5668->pdata.dmic1_data_pin) { in rt5668_i2c_probe()
2527 regmap_update_bits(rt5668->regmap, RT5668_DMIC_CTRL_1, in rt5668_i2c_probe()
2529 regmap_update_bits(rt5668->regmap, RT5668_GPIO_CTRL_1, in rt5668_i2c_probe()
2534 regmap_update_bits(rt5668->regmap, RT5668_DMIC_CTRL_1, in rt5668_i2c_probe()
2536 regmap_update_bits(rt5668->regmap, RT5668_GPIO_CTRL_1, in rt5668_i2c_probe()
2541 dev_dbg(&i2c->dev, "invalid DMIC_DAT pin\n"); in rt5668_i2c_probe()
2545 switch (rt5668->pdata.dmic1_clk_pin) { in rt5668_i2c_probe()
2547 regmap_update_bits(rt5668->regmap, RT5668_GPIO_CTRL_1, in rt5668_i2c_probe()
2552 regmap_update_bits(rt5668->regmap, RT5668_GPIO_CTRL_1, in rt5668_i2c_probe()
2557 dev_dbg(&i2c->dev, "invalid DMIC_CLK pin\n"); in rt5668_i2c_probe()
2562 regmap_update_bits(rt5668->regmap, RT5668_PWR_ANLG_1, in rt5668_i2c_probe()
2565 regmap_write(rt5668->regmap, RT5668_MICBIAS_2, 0x0380); in rt5668_i2c_probe()
2566 regmap_update_bits(rt5668->regmap, RT5668_GPIO_CTRL_1, in rt5668_i2c_probe()
2569 regmap_write(rt5668->regmap, RT5668_TEST_MODE_CTRL_1, 0x0000); in rt5668_i2c_probe()
2571 INIT_DELAYED_WORK(&rt5668->jack_detect_work, in rt5668_i2c_probe()
2573 INIT_DELAYED_WORK(&rt5668->jd_check_work, in rt5668_i2c_probe()
2576 mutex_init(&rt5668->calibrate_mutex); in rt5668_i2c_probe()
2578 if (i2c->irq) { in rt5668_i2c_probe()
2579 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, in rt5668_i2c_probe()
2583 dev_err(&i2c->dev, "Failed to request IRQ: %d\n", ret); in rt5668_i2c_probe()
2587 return devm_snd_soc_register_component(&i2c->dev, &soc_component_dev_rt5668, in rt5668_i2c_probe()
2595 rt5668_reset(rt5668->regmap); in rt5668_i2c_shutdown()