Lines Matching +full:da7219 +full:- +full:dai +full:- +full:bclk

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * da7219.c - DA7219 ALSA SoC Codec Driver
13 #include <linux/clk-provider.h>
26 #include <sound/soc-dapm.h>
31 #include <sound/da7219.h>
32 #include "da7219.h"
33 #include "da7219-aad.h"
41 static const DECLARE_TLV_DB_SCALE(da7219_mic_gain_tlv, -600, 600, 0);
42 static const DECLARE_TLV_DB_SCALE(da7219_mixin_gain_tlv, -450, 150, 0);
43 static const DECLARE_TLV_DB_SCALE(da7219_adc_dig_gain_tlv, -8325, 75, 0);
44 static const DECLARE_TLV_DB_SCALE(da7219_alc_threshold_tlv, -9450, 150, 0);
47 static const DECLARE_TLV_DB_SCALE(da7219_sidetone_gain_tlv, -4200, 300, 0);
48 static const DECLARE_TLV_DB_SCALE(da7219_tonegen_gain_tlv, -4500, 300, 0);
51 static const DECLARE_TLV_DB_SCALE(da7219_dac_eq_band_tlv, -1050, 150, 0);
55 /* -77.25dB to 12dB */
56 0x08, 0x7f, TLV_DB_SCALE_ITEM(-7725, 75, 0)
59 static const DECLARE_TLV_DB_SCALE(da7219_dac_ng_threshold_tlv, -10200, 600, 0);
60 static const DECLARE_TLV_DB_SCALE(da7219_hp_gain_tlv, -5700, 100, 0);
184 "Sum", "SWG1", "SWG2", "SWG1_1-Cos"
258 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
261 mutex_lock(&da7219->ctrl_lock);
263 mutex_unlock(&da7219->ctrl_lock);
272 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
275 mutex_lock(&da7219->ctrl_lock);
277 mutex_unlock(&da7219->ctrl_lock);
286 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
289 mutex_lock(&da7219->ctrl_lock);
291 mutex_unlock(&da7219->ctrl_lock);
300 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
303 mutex_lock(&da7219->ctrl_lock);
305 mutex_unlock(&da7219->ctrl_lock);
352 dev_warn(component->dev,
380 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
389 if ((ret == 1) && (da7219->alc_en))
399 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
403 if ((ucontrol->value.integer.value[0]) && (!da7219->alc_en)) {
405 da7219->alc_en = true;
407 da7219->alc_en = false;
418 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
420 (struct soc_mixer_control *) kcontrol->private_value;
421 unsigned int reg = mixer_ctrl->reg;
425 mutex_lock(&da7219->ctrl_lock);
426 ret = regmap_raw_read(da7219->regmap, reg, &val, sizeof(val));
427 mutex_unlock(&da7219->ctrl_lock);
433 * Frequency value spans two 8-bit registers, lower then upper byte.
436 ucontrol->value.integer.value[0] = le16_to_cpu(val);
445 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
447 (struct soc_mixer_control *) kcontrol->private_value;
448 unsigned int reg = mixer_ctrl->reg;
453 * Frequency value spans two 8-bit registers, lower then upper byte.
457 val_new = cpu_to_le16(ucontrol->value.integer.value[0]);
459 mutex_lock(&da7219->ctrl_lock);
460 ret = regmap_raw_read(da7219->regmap, reg, &val_old, sizeof(val_old));
462 ret = regmap_raw_write(da7219->regmap, reg,
464 mutex_unlock(&da7219->ctrl_lock);
551 /* Input High-Pass Filters */
594 /* DAC High-Pass Filter */
600 /* DAC 5-Band Equaliser */
776 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
777 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
781 if (da7219->micbias_on_event) {
786 da7219->micbias_on_event = false;
787 msleep(da7219->mic_pga_delay);
800 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
801 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
802 struct clk *bclk = da7219->dai_clks[DA7219_DAI_BCLK_IDX];
809 if (da7219->master) {
810 /* Enable DAI clks for master mode */
811 if (bclk) {
812 ret = clk_prepare_enable(bclk);
814 dev_err(component->dev,
815 "Failed to enable DAI clks\n");
826 /* PC synchronised to DAI */
847 dev_warn(component->dev, "SRM failed to lock\n");
851 /* PC free-running */
856 /* Disable DAI clks if in master mode */
857 if (da7219->master) {
858 if (bclk)
859 clk_disable_unprepare(bclk);
869 return -EINVAL;
891 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
894 switch (w->reg) {
904 return -EINVAL;
929 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
930 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
936 da7219->gain_ramp_ctrl =
945 da7219->gain_ramp_ctrl);
1003 /* DAI Supply */
1004 SND_SOC_DAPM_SUPPLY("DAI", DA7219_DAI_CTRL, DA7219_DAI_EN_SHIFT,
1008 /* DAI */
1128 {"DAIOUT", NULL, "DAI"},
1131 {"DAIIN", NULL, "DAI"},
1160 * DAI operations
1166 struct snd_soc_component *component = codec_dai->component;
1167 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1170 mutex_lock(&da7219->pll_lock);
1172 if ((da7219->clk_src == clk_id) && (da7219->mclk_rate == freq)) {
1173 mutex_unlock(&da7219->pll_lock);
1178 mutex_unlock(&da7219->pll_lock);
1179 dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
1181 return -EINVAL;
1195 dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id);
1196 mutex_unlock(&da7219->pll_lock);
1197 return -EINVAL;
1200 da7219->clk_src = clk_id;
1202 if (da7219->mclk) {
1203 freq = clk_round_rate(da7219->mclk, freq);
1204 ret = clk_set_rate(da7219->mclk, freq);
1206 dev_err(codec_dai->dev, "Failed to set clock rate %d\n",
1208 mutex_unlock(&da7219->pll_lock);
1213 da7219->mclk_rate = freq;
1215 mutex_unlock(&da7219->pll_lock);
1222 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1229 /* Verify 2MHz - 54MHz MCLK provided, and set input divider */
1230 if (da7219->mclk_rate < 2000000) {
1231 dev_err(component->dev, "PLL input clock %d below valid range\n",
1232 da7219->mclk_rate);
1233 return -EINVAL;
1234 } else if (da7219->mclk_rate <= 4500000) {
1237 } else if (da7219->mclk_rate <= 9000000) {
1240 } else if (da7219->mclk_rate <= 18000000) {
1243 } else if (da7219->mclk_rate <= 36000000) {
1246 } else if (da7219->mclk_rate <= 54000000) {
1250 dev_err(component->dev, "PLL input clock %d above valid range\n",
1251 da7219->mclk_rate);
1252 return -EINVAL;
1254 freq_ref = (da7219->mclk_rate / indiv);
1272 dev_err(component->dev, "Invalid PLL config\n");
1273 return -EINVAL;
1297 struct snd_soc_component *component = codec_dai->component;
1298 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1301 mutex_lock(&da7219->pll_lock);
1303 mutex_unlock(&da7219->pll_lock);
1310 struct snd_soc_component *component = codec_dai->component;
1311 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1316 da7219->master = true;
1319 da7219->master = false;
1322 return -EINVAL;
1343 return -EINVAL;
1361 return -EINVAL;
1365 return -EINVAL;
1382 return -EINVAL;
1413 return -EINVAL;
1423 static int da7219_set_dai_tdm_slot(struct snd_soc_dai *dai,
1427 struct snd_soc_component *component = dai->component;
1428 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1429 struct clk *wclk = da7219->dai_clks[DA7219_DAI_WCLK_IDX];
1430 struct clk *bclk = da7219->dai_clks[DA7219_DAI_BCLK_IDX];
1444 da7219->tdm_en = false;
1449 slot_offset = ffs(tx_mask) - 1;
1452 dev_err(component->dev,
1455 return -EINVAL;
1464 dev_err(component->dev, "Invalid frame offset %d\n", offset);
1465 return -EINVAL;
1472 if (da7219->master) {
1475 if (bclk) {
1478 ret = clk_set_rate(bclk, bclk_rate);
1480 dev_err(component->dev,
1481 "Failed to set TDM BCLK rate %lu: %d\n",
1488 dev_err(component->dev,
1497 regmap_bulk_write(da7219->regmap, DA7219_DAI_OFFSET_LOWER,
1506 da7219->tdm_en = true;
1551 return -EINVAL;
1561 struct snd_soc_dai *dai)
1563 struct snd_soc_component *component = dai->component;
1564 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1565 struct clk *wclk = da7219->dai_clks[DA7219_DAI_WCLK_IDX];
1566 struct clk *bclk = da7219->dai_clks[DA7219_DAI_BCLK_IDX];
1587 return -EINVAL;
1592 dev_err(component->dev,
1595 return -EINVAL;
1600 if (da7219->master && wclk) {
1603 dev_err(component->dev,
1610 dev_err(component->dev,
1617 * If we're master, then we have a limited set of BCLK rates we
1619 * the BCLK rate automatically.
1621 if (da7219->master && !da7219->tdm_en) {
1627 if (bclk) {
1631 * new rate on an already enabled bclk. In that
1634 * problem, as long as the BCLK rate is suitable for the
1637 bclk_rate = clk_round_rate(bclk, bclk_rate);
1639 dev_err(component->dev,
1640 "BCLK rate mismatch against frame size");
1641 return -EINVAL;
1644 ret = clk_set_rate(bclk, bclk_rate);
1646 dev_err(component->dev,
1647 "Failed to set BCLK rate %lu: %d\n",
1654 dev_err(component->dev,
1688 .name = "da7219-hifi",
1716 { .compatible = "dlg,da7219", },
1777 pdata->wakeup_source = device_property_read_bool(dev, "wakeup-source");
1779 pdata->dai_clk_names[DA7219_DAI_WCLK_IDX] = "da7219-dai-wclk";
1780 pdata->dai_clk_names[DA7219_DAI_BCLK_IDX] = "da7219-dai-bclk";
1781 if (device_property_read_string_array(dev, "clock-output-names",
1782 pdata->dai_clk_names,
1784 dev_warn(dev, "Using default DAI clk names: %s, %s\n",
1785 pdata->dai_clk_names[DA7219_DAI_WCLK_IDX],
1786 pdata->dai_clk_names[DA7219_DAI_BCLK_IDX]);
1788 if (device_property_read_u32(dev, "dlg,micbias-lvl", &of_val32) >= 0)
1789 pdata->micbias_lvl = da7219_fw_micbias_lvl(dev, of_val32);
1791 pdata->micbias_lvl = DA7219_MICBIAS_2_2V;
1793 if (!device_property_read_string(dev, "dlg,mic-amp-in-sel", &of_str))
1794 pdata->mic_amp_in_sel = da7219_fw_mic_amp_in_sel(dev, of_str);
1796 pdata->mic_amp_in_sel = DA7219_MIC_AMP_IN_SEL_DIFF;
1809 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1819 if (da7219->mclk) {
1820 ret = clk_prepare_enable(da7219->mclk);
1822 dev_err(component->dev,
1839 if (da7219->mclk)
1840 clk_disable_unprepare(da7219->mclk);
1844 /* Only disable master bias if we're not a wake-up source */
1845 if (!da7219->wakeup_source)
1864 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1870 da7219->supplies[i].supply = da7219_supply_names[i];
1872 ret = regulator_bulk_get(component->dev, DA7219_NUM_SUPPLIES,
1873 da7219->supplies);
1875 dev_err(component->dev, "Failed to get supplies");
1883 vddio = da7219->supplies[DA7219_SUPPLY_VDDIO].consumer;
1886 dev_warn(component->dev, "Invalid VDDIO voltage\n");
1891 ret = regulator_bulk_enable(DA7219_NUM_SUPPLIES, da7219->supplies);
1893 dev_err(component->dev, "Failed to enable supplies");
1894 regulator_bulk_free(DA7219_NUM_SUPPLIES, da7219->supplies);
1904 struct da7219_priv *da7219 =
1907 struct snd_soc_component *component = da7219->component;
1909 if (!da7219->master)
1910 return -EINVAL;
1921 struct da7219_priv *da7219 =
1924 struct snd_soc_component *component = da7219->component;
1926 if (!da7219->master)
1935 struct da7219_priv *da7219 =
1938 struct snd_soc_component *component = da7219->component;
1941 if (!da7219->master)
1942 return -EINVAL;
1952 struct da7219_priv *da7219 =
1955 struct snd_soc_component *component = da7219->component;
1989 struct da7219_priv *da7219 =
1993 if (!da7219->master)
1994 return -EINVAL;
1996 if (req->rate < 11025)
1997 req->rate = 8000;
1998 else if (req->rate < 12000)
1999 req->rate = 11025;
2000 else if (req->rate < 16000)
2001 req->rate = 12000;
2002 else if (req->rate < 22050)
2003 req->rate = 16000;
2004 else if (req->rate < 24000)
2005 req->rate = 22050;
2006 else if (req->rate < 32000)
2007 req->rate = 24000;
2008 else if (req->rate < 44100)
2009 req->rate = 32000;
2010 else if (req->rate < 48000)
2011 req->rate = 44100;
2012 else if (req->rate < 88200)
2013 req->rate = 48000;
2014 else if (req->rate < 96000)
2015 req->rate = 88200;
2017 req->rate = 96000;
2025 struct da7219_priv *da7219 =
2028 struct snd_soc_component *component = da7219->component;
2030 if (!da7219->master)
2031 return -EINVAL;
2039 struct da7219_priv *da7219 =
2042 struct snd_soc_component *component = da7219->component;
2079 struct da7219_priv *da7219 =
2084 if (!req->best_parent_rate || !da7219->master)
2085 return -EINVAL;
2088 * We don't allow changing the parent rate as some BCLK rates can be
2089 * derived from multiple parent WCLK rates (BCLK rates are set as a
2091 * parent WCLK rate set and find the appropriate multiplier of BCLK to
2092 * get the rounded down BCLK value.
2094 factor = da7219_bclk_get_factor(req->rate, req->best_parent_rate);
2096 req->rate = req->best_parent_rate * factor;
2104 struct da7219_priv *da7219 =
2107 struct snd_soc_component *component = da7219->component;
2110 if (!da7219->master)
2111 return -EINVAL;
2136 struct device *dev = component->dev;
2137 struct device_node *np = dev->of_node;
2138 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
2139 struct da7219_pdata *pdata = da7219->pdata;
2149 return -ENOMEM;
2151 clk_data->num = DA7219_DAI_NUM_CLKS;
2152 da7219->clk_hw_data = clk_data;
2158 struct clk_hw *dai_clk_hw = &da7219->dai_clks_hw[i];
2166 if (da7219->mclk) {
2167 parent_name = __clk_get_name(da7219->mclk);
2176 /* Make WCLK the parent of BCLK */
2177 parent_name = __clk_get_name(da7219->dai_clks[DA7219_DAI_WCLK_IDX]);
2183 ret = -EINVAL;
2187 init.name = pdata->dai_clk_names[i];
2190 dai_clk_hw->init = &init;
2198 da7219->dai_clks[i] = dai_clk_hw->clk;
2202 da7219->clk_hw_data->hws[i] = dai_clk_hw;
2208 ret = -ENOMEM;
2211 da7219->dai_clks_lookup[i] = dai_clk_lookup;
2218 ret = of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
2219 da7219->clk_hw_data);
2229 while (--i >= 0) {
2230 if (da7219->dai_clks_lookup[i])
2231 clkdev_drop(da7219->dai_clks_lookup[i]);
2233 clk_hw_unregister(&da7219->dai_clks_hw[i]);
2237 kfree(da7219->clk_hw_data);
2244 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
2245 struct device_node *np = component->dev->of_node;
2251 for (i = DA7219_DAI_NUM_CLKS - 1; i >= 0; --i) {
2252 if (da7219->dai_clks_lookup[i])
2253 clkdev_drop(da7219->dai_clks_lookup[i]);
2255 clk_hw_unregister(&da7219->dai_clks_hw[i]);
2259 kfree(da7219->clk_hw_data);
2272 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
2273 struct da7219_pdata *pdata = da7219->pdata;
2278 da7219->wakeup_source = pdata->wakeup_source;
2281 switch (pdata->micbias_lvl) {
2288 micbias_lvl |= (pdata->micbias_lvl <<
2299 da7219->mic_pga_delay = DA7219_MIC_PGA_BASE_DELAY +
2300 (pdata->micbias_lvl *
2304 switch (pdata->mic_amp_in_sel) {
2309 pdata->mic_amp_in_sel);
2457 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
2462 da7219->component = component;
2463 mutex_init(&da7219->ctrl_lock);
2464 mutex_init(&da7219->pll_lock);
2471 regcache_cache_bypass(da7219->regmap, true);
2474 regmap_read(da7219->regmap, DA7219_SYSTEM_ACTIVE, &system_active);
2476 regmap_write(da7219->regmap, DA7219_GAIN_RAMP_CTRL,
2478 regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_INPUT, 0x00);
2479 regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_OUTPUT, 0x01);
2482 regmap_read(da7219->regmap, DA7219_SYSTEM_STATUS,
2492 regmap_write_bits(da7219->regmap, DA7219_ACCDET_CONFIG_1,
2494 regmap_write_bits(da7219->regmap, DA7219_CIF_CTRL,
2497 regmap_write_bits(da7219->regmap, DA7219_SYSTEM_ACTIVE,
2499 regmap_write_bits(da7219->regmap, DA7219_SYSTEM_ACTIVE,
2502 regcache_cache_bypass(da7219->regmap, false);
2503 regmap_reinit_cache(da7219->regmap, &da7219_regmap_config);
2508 ret = regmap_read(da7219->regmap, DA7219_CHIP_REVISION, &rev);
2510 dev_err(component->dev, "Failed to read chip revision: %d\n", ret);
2516 ret = regmap_register_patch(da7219->regmap, da7219_rev_aa_patch,
2519 dev_err(component->dev, "Failed to register AA patch: %d\n",
2532 da7219->mclk = clk_get(component->dev, "mclk");
2533 if (IS_ERR(da7219->mclk)) {
2534 if (PTR_ERR(da7219->mclk) != -ENOENT) {
2535 ret = PTR_ERR(da7219->mclk);
2538 da7219->mclk = NULL;
2542 /* Register CCF DAI clock control */
2547 /* Default PC counter to free-running */
2590 clk_put(da7219->mclk);
2593 regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies);
2594 regulator_bulk_free(DA7219_NUM_SUPPLIES, da7219->supplies);
2601 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
2606 clk_put(da7219->mclk);
2609 regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies);
2610 regulator_bulk_free(DA7219_NUM_SUPPLIES, da7219->supplies);
2616 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
2619 /* Suspend AAD if we're not a wake-up source */
2620 if (!da7219->wakeup_source)
2630 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
2636 if (!da7219->wakeup_source)
2679 struct device *dev = &i2c->dev;
2680 struct da7219_priv *da7219;
2683 da7219 = devm_kzalloc(dev, sizeof(struct da7219_priv),
2685 if (!da7219)
2686 return -ENOMEM;
2688 i2c_set_clientdata(i2c, da7219);
2690 da7219->regmap = devm_regmap_init_i2c(i2c, &da7219_regmap_config);
2691 if (IS_ERR(da7219->regmap)) {
2692 ret = PTR_ERR(da7219->regmap);
2698 da7219->pdata = dev_get_platdata(dev);
2699 if (!da7219->pdata)
2700 da7219->pdata = da7219_fw_to_pdata(dev);
2710 dev_err(dev, "Failed to register da7219 component: %d\n", ret);
2716 { "da7219", },
2723 .name = "da7219",
2733 MODULE_DESCRIPTION("ASoC DA7219 Codec Driver");