hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/sound/soc/codecs/da7213.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * DA7213 ALSA SoC Codec Driver
34 *
....@@ -5,11 +6,6 @@
56 *
67 * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
78 * Based on DA9055 ALSA SoC codec driver.
8
- *
9
- * This program is free software; you can redistribute it and/or modify it
10
- * under the terms of the GNU General Public License as published by the
11
- * Free Software Foundation; either version 2 of the License, or (at your
12
- * option) any later version.
139 */
1410
1511 #include <linux/acpi.h>
....@@ -23,6 +19,7 @@
2319 #include <linux/module.h>
2420 #include <sound/pcm.h>
2521 #include <sound/pcm_params.h>
22
+#include <linux/pm_runtime.h>
2623 #include <sound/soc.h>
2724 #include <sound/initval.h>
2825 #include <sound/tlv.h>
....@@ -208,12 +205,12 @@
208205 /* Select middle 8 bits for read back from data register */
209206 snd_soc_component_write(component, DA7213_ALC_CIC_OP_LVL_CTRL,
210207 reg_val | DA7213_ALC_DATA_MIDDLE);
211
- mid_data = snd_soc_component_read32(component, DA7213_ALC_CIC_OP_LVL_DATA);
208
+ mid_data = snd_soc_component_read(component, DA7213_ALC_CIC_OP_LVL_DATA);
212209
213210 /* Select top 8 bits for read back from data register */
214211 snd_soc_component_write(component, DA7213_ALC_CIC_OP_LVL_CTRL,
215212 reg_val | DA7213_ALC_DATA_TOP);
216
- top_data = snd_soc_component_read32(component, DA7213_ALC_CIC_OP_LVL_DATA);
213
+ top_data = snd_soc_component_read(component, DA7213_ALC_CIC_OP_LVL_DATA);
217214
218215 sum += ((mid_data << 8) | (top_data << 16));
219216 }
....@@ -262,7 +259,7 @@
262259 snd_soc_component_update_bits(component, DA7213_ALC_CTRL1, DA7213_ALC_AUTO_CALIB_EN,
263260 DA7213_ALC_AUTO_CALIB_EN);
264261 do {
265
- alc_ctrl1 = snd_soc_component_read32(component, DA7213_ALC_CTRL1);
262
+ alc_ctrl1 = snd_soc_component_read(component, DA7213_ALC_CTRL1);
266263 } while (alc_ctrl1 & DA7213_ALC_AUTO_CALIB_EN);
267264
268265 /* If auto calibration fails, fall back to digital gain only mode */
....@@ -289,16 +286,16 @@
289286 u8 mic_1_ctrl, mic_2_ctrl;
290287
291288 /* Save current values from ADC control registers */
292
- adc_l_ctrl = snd_soc_component_read32(component, DA7213_ADC_L_CTRL);
293
- adc_r_ctrl = snd_soc_component_read32(component, DA7213_ADC_R_CTRL);
289
+ adc_l_ctrl = snd_soc_component_read(component, DA7213_ADC_L_CTRL);
290
+ adc_r_ctrl = snd_soc_component_read(component, DA7213_ADC_R_CTRL);
294291
295292 /* Save current values from MIXIN_L/R_SELECT registers */
296
- mixin_l_sel = snd_soc_component_read32(component, DA7213_MIXIN_L_SELECT);
297
- mixin_r_sel = snd_soc_component_read32(component, DA7213_MIXIN_R_SELECT);
293
+ mixin_l_sel = snd_soc_component_read(component, DA7213_MIXIN_L_SELECT);
294
+ mixin_r_sel = snd_soc_component_read(component, DA7213_MIXIN_R_SELECT);
298295
299296 /* Save current values from MIC control registers */
300
- mic_1_ctrl = snd_soc_component_read32(component, DA7213_MIC_1_CTRL);
301
- mic_2_ctrl = snd_soc_component_read32(component, DA7213_MIC_2_CTRL);
297
+ mic_1_ctrl = snd_soc_component_read(component, DA7213_MIC_1_CTRL);
298
+ mic_2_ctrl = snd_soc_component_read(component, DA7213_MIC_2_CTRL);
302299
303300 /* Enable ADC Left and Right */
304301 snd_soc_component_update_bits(component, DA7213_ADC_L_CTRL, DA7213_ADC_EN,
....@@ -754,7 +751,7 @@
754751 DA7213_PC_FREERUN_MASK, 0);
755752
756753 /* If SRM not enabled then nothing more to do */
757
- pll_ctrl = snd_soc_component_read32(component, DA7213_PLL_CTRL);
754
+ pll_ctrl = snd_soc_component_read(component, DA7213_PLL_CTRL);
758755 if (!(pll_ctrl & DA7213_PLL_SRM_EN))
759756 return 0;
760757
....@@ -767,7 +764,7 @@
767764
768765 /* Check SRM has locked */
769766 do {
770
- pll_status = snd_soc_component_read32(component, DA7213_PLL_STATUS);
767
+ pll_status = snd_soc_component_read(component, DA7213_PLL_STATUS);
771768 if (pll_status & DA7219_PLL_SRM_LOCK) {
772769 srm_lock = true;
773770 } else {
....@@ -782,7 +779,7 @@
782779 return 0;
783780 case SND_SOC_DAPM_POST_PMD:
784781 /* Revert 32KHz PLL lock udpates if applied previously */
785
- pll_ctrl = snd_soc_component_read32(component, DA7213_PLL_CTRL);
782
+ pll_ctrl = snd_soc_component_read(component, DA7213_PLL_CTRL);
786783 if (pll_ctrl & DA7213_PLL_32K_MODE) {
787784 snd_soc_component_write(component, 0xF0, 0x8B);
788785 snd_soc_component_write(component, 0xF2, 0x01);
....@@ -810,6 +807,11 @@
810807 */
811808
812809 static const struct snd_soc_dapm_widget da7213_dapm_widgets[] = {
810
+ /*
811
+ * Power Supply
812
+ */
813
+ SND_SOC_DAPM_REGULATOR_SUPPLY("VDDMIC", 0, 0),
814
+
813815 /*
814816 * Input & Output
815817 */
....@@ -936,6 +938,9 @@
936938 /* Dest Connecting Widget source */
937939
938940 /* Input path */
941
+ {"Mic Bias 1", NULL, "VDDMIC"},
942
+ {"Mic Bias 2", NULL, "VDDMIC"},
943
+
939944 {"MIC1", NULL, "Mic Bias 1"},
940945 {"MIC2", NULL, "Mic Bias 2"},
941946
....@@ -1151,6 +1156,7 @@
11511156 struct snd_soc_dai *dai)
11521157 {
11531158 struct snd_soc_component *component = dai->component;
1159
+ struct da7213_priv *da7213 = snd_soc_component_get_drvdata(component);
11541160 u8 dai_ctrl = 0;
11551161 u8 fs;
11561162
....@@ -1176,33 +1182,43 @@
11761182 switch (params_rate(params)) {
11771183 case 8000:
11781184 fs = DA7213_SR_8000;
1185
+ da7213->out_rate = DA7213_PLL_FREQ_OUT_98304000;
11791186 break;
11801187 case 11025:
11811188 fs = DA7213_SR_11025;
1189
+ da7213->out_rate = DA7213_PLL_FREQ_OUT_90316800;
11821190 break;
11831191 case 12000:
11841192 fs = DA7213_SR_12000;
1193
+ da7213->out_rate = DA7213_PLL_FREQ_OUT_98304000;
11851194 break;
11861195 case 16000:
11871196 fs = DA7213_SR_16000;
1197
+ da7213->out_rate = DA7213_PLL_FREQ_OUT_98304000;
11881198 break;
11891199 case 22050:
11901200 fs = DA7213_SR_22050;
1201
+ da7213->out_rate = DA7213_PLL_FREQ_OUT_90316800;
11911202 break;
11921203 case 32000:
11931204 fs = DA7213_SR_32000;
1205
+ da7213->out_rate = DA7213_PLL_FREQ_OUT_98304000;
11941206 break;
11951207 case 44100:
11961208 fs = DA7213_SR_44100;
1209
+ da7213->out_rate = DA7213_PLL_FREQ_OUT_90316800;
11971210 break;
11981211 case 48000:
11991212 fs = DA7213_SR_48000;
1213
+ da7213->out_rate = DA7213_PLL_FREQ_OUT_98304000;
12001214 break;
12011215 case 88200:
12021216 fs = DA7213_SR_88200;
1217
+ da7213->out_rate = DA7213_PLL_FREQ_OUT_90316800;
12031218 break;
12041219 case 96000:
12051220 fs = DA7213_SR_96000;
1221
+ da7213->out_rate = DA7213_PLL_FREQ_OUT_98304000;
12061222 break;
12071223 default:
12081224 return -EINVAL;
....@@ -1305,7 +1321,10 @@
13051321 /* By default only 64 BCLK per WCLK is supported */
13061322 dai_clk_mode |= DA7213_DAI_BCLKS_PER_WCLK_64;
13071323
1308
- snd_soc_component_write(component, DA7213_DAI_CLK_MODE, dai_clk_mode);
1324
+ snd_soc_component_update_bits(component, DA7213_DAI_CLK_MODE,
1325
+ DA7213_DAI_BCLKS_PER_WCLK_MASK |
1326
+ DA7213_DAI_CLK_POL_MASK | DA7213_DAI_WCLK_POL_MASK,
1327
+ dai_clk_mode);
13091328 snd_soc_component_update_bits(component, DA7213_DAI_CTRL, DA7213_DAI_FORMAT_MASK,
13101329 dai_ctrl);
13111330 snd_soc_component_write(component, DA7213_DAI_OFFSET, dai_offset);
....@@ -1313,7 +1332,7 @@
13131332 return 0;
13141333 }
13151334
1316
-static int da7213_mute(struct snd_soc_dai *dai, int mute)
1335
+static int da7213_mute(struct snd_soc_dai *dai, int mute, int direction)
13171336 {
13181337 struct snd_soc_component *component = dai->component;
13191338
....@@ -1335,10 +1354,10 @@
13351354 #define DA7213_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
13361355 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
13371356
1338
-static int da7213_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1339
- int clk_id, unsigned int freq, int dir)
1357
+static int da7213_set_component_sysclk(struct snd_soc_component *component,
1358
+ int clk_id, int source,
1359
+ unsigned int freq, int dir)
13401360 {
1341
- struct snd_soc_component *component = codec_dai->component;
13421361 struct da7213_priv *da7213 = snd_soc_component_get_drvdata(component);
13431362 int ret = 0;
13441363
....@@ -1346,7 +1365,7 @@
13461365 return 0;
13471366
13481367 if (((freq < 5000000) && (freq != 32768)) || (freq > 54000000)) {
1349
- dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
1368
+ dev_err(component->dev, "Unsupported MCLK value %d\n",
13501369 freq);
13511370 return -EINVAL;
13521371 }
....@@ -1362,7 +1381,7 @@
13621381 DA7213_PLL_MCLK_SQR_EN);
13631382 break;
13641383 default:
1365
- dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id);
1384
+ dev_err(component->dev, "Unknown clock source %d\n", clk_id);
13661385 return -EINVAL;
13671386 }
13681387
....@@ -1372,7 +1391,7 @@
13721391 freq = clk_round_rate(da7213->mclk, freq);
13731392 ret = clk_set_rate(da7213->mclk, freq);
13741393 if (ret) {
1375
- dev_err(codec_dai->dev, "Failed to set clock rate %d\n",
1394
+ dev_err(component->dev, "Failed to set clock rate %d\n",
13761395 freq);
13771396 return ret;
13781397 }
....@@ -1384,10 +1403,10 @@
13841403 }
13851404
13861405 /* Supported PLL input frequencies are 32KHz, 5MHz - 54MHz. */
1387
-static int da7213_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
1388
- int source, unsigned int fref, unsigned int fout)
1406
+static int _da7213_set_component_pll(struct snd_soc_component *component,
1407
+ int pll_id, int source,
1408
+ unsigned int fref, unsigned int fout)
13891409 {
1390
- struct snd_soc_component *component = codec_dai->component;
13911410 struct da7213_priv *da7213 = snd_soc_component_get_drvdata(component);
13921411
13931412 u8 pll_ctrl, indiv_bits, indiv;
....@@ -1495,13 +1514,22 @@
14951514 return 0;
14961515 }
14971516
1517
+static int da7213_set_component_pll(struct snd_soc_component *component,
1518
+ int pll_id, int source,
1519
+ unsigned int fref, unsigned int fout)
1520
+{
1521
+ struct da7213_priv *da7213 = snd_soc_component_get_drvdata(component);
1522
+ da7213->fixed_clk_auto_pll = false;
1523
+
1524
+ return _da7213_set_component_pll(component, pll_id, source, fref, fout);
1525
+}
1526
+
14981527 /* DAI operations */
14991528 static const struct snd_soc_dai_ops da7213_dai_ops = {
15001529 .hw_params = da7213_hw_params,
15011530 .set_fmt = da7213_set_dai_fmt,
1502
- .set_sysclk = da7213_set_dai_sysclk,
1503
- .set_pll = da7213_set_dai_pll,
1504
- .digital_mute = da7213_mute,
1531
+ .mute_stream = da7213_mute,
1532
+ .no_capture_mute = 1,
15051533 };
15061534
15071535 static struct snd_soc_dai_driver da7213_dai = {
....@@ -1526,6 +1554,50 @@
15261554 .symmetric_rates = 1,
15271555 };
15281556
1557
+static int da7213_set_auto_pll(struct snd_soc_component *component, bool enable)
1558
+{
1559
+ struct da7213_priv *da7213 = snd_soc_component_get_drvdata(component);
1560
+ int mode;
1561
+
1562
+ if (!da7213->fixed_clk_auto_pll)
1563
+ return 0;
1564
+
1565
+ da7213->mclk_rate = clk_get_rate(da7213->mclk);
1566
+
1567
+ if (enable) {
1568
+ /* Slave mode needs SRM for non-harmonic frequencies */
1569
+ if (da7213->master)
1570
+ mode = DA7213_SYSCLK_PLL;
1571
+ else
1572
+ mode = DA7213_SYSCLK_PLL_SRM;
1573
+
1574
+ /* PLL is not required for harmonic frequencies */
1575
+ switch (da7213->out_rate) {
1576
+ case DA7213_PLL_FREQ_OUT_90316800:
1577
+ if (da7213->mclk_rate == 11289600 ||
1578
+ da7213->mclk_rate == 22579200 ||
1579
+ da7213->mclk_rate == 45158400)
1580
+ mode = DA7213_SYSCLK_MCLK;
1581
+ break;
1582
+ case DA7213_PLL_FREQ_OUT_98304000:
1583
+ if (da7213->mclk_rate == 12288000 ||
1584
+ da7213->mclk_rate == 24576000 ||
1585
+ da7213->mclk_rate == 49152000)
1586
+ mode = DA7213_SYSCLK_MCLK;
1587
+
1588
+ break;
1589
+ default:
1590
+ return -1;
1591
+ }
1592
+ } else {
1593
+ /* Disable PLL in standby */
1594
+ mode = DA7213_SYSCLK_MCLK;
1595
+ }
1596
+
1597
+ return _da7213_set_component_pll(component, 0, mode,
1598
+ da7213->mclk_rate, da7213->out_rate);
1599
+}
1600
+
15291601 static int da7213_set_bias_level(struct snd_soc_component *component,
15301602 enum snd_soc_bias_level level)
15311603 {
....@@ -1545,6 +1617,8 @@
15451617 "Failed to enable mclk\n");
15461618 return ret;
15471619 }
1620
+
1621
+ da7213_set_auto_pll(component, true);
15481622 }
15491623 }
15501624 break;
....@@ -1556,8 +1630,10 @@
15561630 DA7213_VMID_EN | DA7213_BIAS_EN);
15571631 } else {
15581632 /* Remove MCLK */
1559
- if (da7213->mclk)
1633
+ if (da7213->mclk) {
1634
+ da7213_set_auto_pll(component, false);
15601635 clk_disable_unprepare(da7213->mclk);
1636
+ }
15611637 }
15621638 break;
15631639 case SND_SOC_BIAS_OFF:
....@@ -1572,6 +1648,7 @@
15721648 #if defined(CONFIG_OF)
15731649 /* DT */
15741650 static const struct of_device_id da7213_of_match[] = {
1651
+ { .compatible = "dlg,da7212", },
15751652 { .compatible = "dlg,da7213", },
15761653 { }
15771654 };
....@@ -1686,10 +1763,11 @@
16861763 return pdata;
16871764 }
16881765
1689
-
16901766 static int da7213_probe(struct snd_soc_component *component)
16911767 {
16921768 struct da7213_priv *da7213 = snd_soc_component_get_drvdata(component);
1769
+
1770
+ pm_runtime_get_sync(component->dev);
16931771
16941772 /* Default to using ALC auto offset calibration mode. */
16951773 snd_soc_component_update_bits(component, DA7213_ALC_CTRL1,
....@@ -1811,6 +1889,8 @@
18111889 DA7213_DMIC_CLK_RATE_MASK, dmic_cfg);
18121890 }
18131891
1892
+ pm_runtime_put_sync(component->dev);
1893
+
18141894 /* Check if MCLK provided */
18151895 da7213->mclk = devm_clk_get(component->dev, "mclk");
18161896 if (IS_ERR(da7213->mclk)) {
....@@ -1818,6 +1898,11 @@
18181898 return PTR_ERR(da7213->mclk);
18191899 else
18201900 da7213->mclk = NULL;
1901
+ } else {
1902
+ /* Do automatic PLL handling assuming fixed clock until
1903
+ * set_pll() has been called. This makes the codec usable
1904
+ * with the simple-audio-card driver. */
1905
+ da7213->fixed_clk_auto_pll = true;
18211906 }
18221907
18231908 return 0;
....@@ -1832,6 +1917,8 @@
18321917 .num_dapm_widgets = ARRAY_SIZE(da7213_dapm_widgets),
18331918 .dapm_routes = da7213_audio_map,
18341919 .num_dapm_routes = ARRAY_SIZE(da7213_audio_map),
1920
+ .set_sysclk = da7213_set_component_sysclk,
1921
+ .set_pll = da7213_set_component_pll,
18351922 .idle_bias_on = 1,
18361923 .use_pmdown_time = 1,
18371924 .endianness = 1,
....@@ -1848,11 +1935,22 @@
18481935 .cache_type = REGCACHE_RBTREE,
18491936 };
18501937
1938
+static void da7213_power_off(void *data)
1939
+{
1940
+ struct da7213_priv *da7213 = data;
1941
+ regulator_bulk_disable(DA7213_NUM_SUPPLIES, da7213->supplies);
1942
+}
1943
+
1944
+static const char *da7213_supply_names[DA7213_NUM_SUPPLIES] = {
1945
+ [DA7213_SUPPLY_VDDA] = "VDDA",
1946
+ [DA7213_SUPPLY_VDDIO] = "VDDIO",
1947
+};
1948
+
18511949 static int da7213_i2c_probe(struct i2c_client *i2c,
18521950 const struct i2c_device_id *id)
18531951 {
18541952 struct da7213_priv *da7213;
1855
- int ret;
1953
+ int i, ret;
18561954
18571955 da7213 = devm_kzalloc(&i2c->dev, sizeof(*da7213), GFP_KERNEL);
18581956 if (!da7213)
....@@ -1860,12 +1958,36 @@
18601958
18611959 i2c_set_clientdata(i2c, da7213);
18621960
1961
+ /* Get required supplies */
1962
+ for (i = 0; i < DA7213_NUM_SUPPLIES; ++i)
1963
+ da7213->supplies[i].supply = da7213_supply_names[i];
1964
+
1965
+ ret = devm_regulator_bulk_get(&i2c->dev, DA7213_NUM_SUPPLIES,
1966
+ da7213->supplies);
1967
+ if (ret) {
1968
+ dev_err(&i2c->dev, "Failed to get supplies: %d\n", ret);
1969
+ return ret;
1970
+ }
1971
+
1972
+ ret = regulator_bulk_enable(DA7213_NUM_SUPPLIES, da7213->supplies);
1973
+ if (ret < 0)
1974
+ return ret;
1975
+
1976
+ ret = devm_add_action_or_reset(&i2c->dev, da7213_power_off, da7213);
1977
+ if (ret < 0)
1978
+ return ret;
1979
+
18631980 da7213->regmap = devm_regmap_init_i2c(i2c, &da7213_regmap_config);
18641981 if (IS_ERR(da7213->regmap)) {
18651982 ret = PTR_ERR(da7213->regmap);
18661983 dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
18671984 return ret;
18681985 }
1986
+
1987
+ pm_runtime_set_autosuspend_delay(&i2c->dev, 100);
1988
+ pm_runtime_use_autosuspend(&i2c->dev);
1989
+ pm_runtime_set_active(&i2c->dev);
1990
+ pm_runtime_enable(&i2c->dev);
18691991
18701992 ret = devm_snd_soc_register_component(&i2c->dev,
18711993 &soc_component_dev_da7213, &da7213_dai, 1);
....@@ -1875,6 +1997,34 @@
18751997 }
18761998 return ret;
18771999 }
2000
+
2001
+static int __maybe_unused da7213_runtime_suspend(struct device *dev)
2002
+{
2003
+ struct da7213_priv *da7213 = dev_get_drvdata(dev);
2004
+
2005
+ regcache_cache_only(da7213->regmap, true);
2006
+ regcache_mark_dirty(da7213->regmap);
2007
+ regulator_bulk_disable(DA7213_NUM_SUPPLIES, da7213->supplies);
2008
+
2009
+ return 0;
2010
+}
2011
+
2012
+static int __maybe_unused da7213_runtime_resume(struct device *dev)
2013
+{
2014
+ struct da7213_priv *da7213 = dev_get_drvdata(dev);
2015
+ int ret;
2016
+
2017
+ ret = regulator_bulk_enable(DA7213_NUM_SUPPLIES, da7213->supplies);
2018
+ if (ret < 0)
2019
+ return ret;
2020
+ regcache_cache_only(da7213->regmap, false);
2021
+ regcache_sync(da7213->regmap);
2022
+ return 0;
2023
+}
2024
+
2025
+static const struct dev_pm_ops da7213_pm = {
2026
+ SET_RUNTIME_PM_OPS(da7213_runtime_suspend, da7213_runtime_resume, NULL)
2027
+};
18782028
18792029 static const struct i2c_device_id da7213_i2c_id[] = {
18802030 { "da7213", 0 },
....@@ -1888,6 +2038,7 @@
18882038 .name = "da7213",
18892039 .of_match_table = of_match_ptr(da7213_of_match),
18902040 .acpi_match_table = ACPI_PTR(da7213_acpi_match),
2041
+ .pm = &da7213_pm,
18912042 },
18922043 .probe = da7213_i2c_probe,
18932044 .id_table = da7213_i2c_id,