| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * max98088.c -- MAX98088 ALSA SoC Audio driver |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright 2010 Maxim Integrated Products |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 8 | | - * published by the Free Software Foundation. |
|---|
| 9 | 6 | */ |
|---|
| 10 | 7 | |
|---|
| 11 | 8 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 16 | 13 | #include <linux/pm.h> |
|---|
| 17 | 14 | #include <linux/i2c.h> |
|---|
| 18 | 15 | #include <linux/regmap.h> |
|---|
| 16 | +#include <linux/clk.h> |
|---|
| 19 | 17 | #include <sound/core.h> |
|---|
| 20 | 18 | #include <sound/pcm.h> |
|---|
| 21 | 19 | #include <sound/pcm_params.h> |
|---|
| .. | .. |
|---|
| 42 | 40 | struct regmap *regmap; |
|---|
| 43 | 41 | enum max98088_type devtype; |
|---|
| 44 | 42 | struct max98088_pdata *pdata; |
|---|
| 43 | + struct clk *mclk; |
|---|
| 44 | + unsigned char mclk_prescaler; |
|---|
| 45 | 45 | unsigned int sysclk; |
|---|
| 46 | 46 | struct max98088_cdata dai[2]; |
|---|
| 47 | 47 | int eq_textcnt; |
|---|
| .. | .. |
|---|
| 997 | 997 | cdata->rate = rate; |
|---|
| 998 | 998 | |
|---|
| 999 | 999 | /* Configure NI when operating as master */ |
|---|
| 1000 | | - if (snd_soc_component_read32(component, M98088_REG_14_DAI1_FORMAT) |
|---|
| 1000 | + if (snd_soc_component_read(component, M98088_REG_14_DAI1_FORMAT) |
|---|
| 1001 | 1001 | & M98088_DAI_MAS) { |
|---|
| 1002 | + unsigned long pclk; |
|---|
| 1003 | + |
|---|
| 1002 | 1004 | if (max98088->sysclk == 0) { |
|---|
| 1003 | 1005 | dev_err(component->dev, "Invalid system clock frequency\n"); |
|---|
| 1004 | 1006 | return -EINVAL; |
|---|
| 1005 | 1007 | } |
|---|
| 1006 | 1008 | ni = 65536ULL * (rate < 50000 ? 96ULL : 48ULL) |
|---|
| 1007 | 1009 | * (unsigned long long int)rate; |
|---|
| 1008 | | - do_div(ni, (unsigned long long int)max98088->sysclk); |
|---|
| 1010 | + pclk = DIV_ROUND_CLOSEST(max98088->sysclk, max98088->mclk_prescaler); |
|---|
| 1011 | + ni = DIV_ROUND_CLOSEST_ULL(ni, pclk); |
|---|
| 1009 | 1012 | snd_soc_component_write(component, M98088_REG_12_DAI1_CLKCFG_HI, |
|---|
| 1010 | 1013 | (ni >> 8) & 0x7F); |
|---|
| 1011 | 1014 | snd_soc_component_write(component, M98088_REG_13_DAI1_CLKCFG_LO, |
|---|
| .. | .. |
|---|
| 1064 | 1067 | cdata->rate = rate; |
|---|
| 1065 | 1068 | |
|---|
| 1066 | 1069 | /* Configure NI when operating as master */ |
|---|
| 1067 | | - if (snd_soc_component_read32(component, M98088_REG_1C_DAI2_FORMAT) |
|---|
| 1070 | + if (snd_soc_component_read(component, M98088_REG_1C_DAI2_FORMAT) |
|---|
| 1068 | 1071 | & M98088_DAI_MAS) { |
|---|
| 1072 | + unsigned long pclk; |
|---|
| 1073 | + |
|---|
| 1069 | 1074 | if (max98088->sysclk == 0) { |
|---|
| 1070 | 1075 | dev_err(component->dev, "Invalid system clock frequency\n"); |
|---|
| 1071 | 1076 | return -EINVAL; |
|---|
| 1072 | 1077 | } |
|---|
| 1073 | 1078 | ni = 65536ULL * (rate < 50000 ? 96ULL : 48ULL) |
|---|
| 1074 | 1079 | * (unsigned long long int)rate; |
|---|
| 1075 | | - do_div(ni, (unsigned long long int)max98088->sysclk); |
|---|
| 1080 | + pclk = DIV_ROUND_CLOSEST(max98088->sysclk, max98088->mclk_prescaler); |
|---|
| 1081 | + ni = DIV_ROUND_CLOSEST_ULL(ni, pclk); |
|---|
| 1076 | 1082 | snd_soc_component_write(component, M98088_REG_1A_DAI2_CLKCFG_HI, |
|---|
| 1077 | 1083 | (ni >> 8) & 0x7F); |
|---|
| 1078 | 1084 | snd_soc_component_write(component, M98088_REG_1B_DAI2_CLKCFG_LO, |
|---|
| .. | .. |
|---|
| 1103 | 1109 | if (freq == max98088->sysclk) |
|---|
| 1104 | 1110 | return 0; |
|---|
| 1105 | 1111 | |
|---|
| 1112 | + if (!IS_ERR(max98088->mclk)) { |
|---|
| 1113 | + freq = clk_round_rate(max98088->mclk, freq); |
|---|
| 1114 | + clk_set_rate(max98088->mclk, freq); |
|---|
| 1115 | + } |
|---|
| 1116 | + |
|---|
| 1106 | 1117 | /* Setup clocks for slave mode, and using the PLL |
|---|
| 1107 | 1118 | * PSCLK = 0x01 (when master clk is 10MHz to 20MHz) |
|---|
| 1108 | 1119 | * 0x02 (when master clk is 20MHz to 30MHz).. |
|---|
| 1109 | 1120 | */ |
|---|
| 1110 | 1121 | if ((freq >= 10000000) && (freq < 20000000)) { |
|---|
| 1111 | 1122 | snd_soc_component_write(component, M98088_REG_10_SYS_CLK, 0x10); |
|---|
| 1123 | + max98088->mclk_prescaler = 1; |
|---|
| 1112 | 1124 | } else if ((freq >= 20000000) && (freq < 30000000)) { |
|---|
| 1113 | 1125 | snd_soc_component_write(component, M98088_REG_10_SYS_CLK, 0x20); |
|---|
| 1126 | + max98088->mclk_prescaler = 2; |
|---|
| 1114 | 1127 | } else { |
|---|
| 1115 | 1128 | dev_err(component->dev, "Invalid master clock frequency\n"); |
|---|
| 1116 | 1129 | return -EINVAL; |
|---|
| 1117 | 1130 | } |
|---|
| 1118 | 1131 | |
|---|
| 1119 | | - if (snd_soc_component_read32(component, M98088_REG_51_PWR_SYS) & M98088_SHDNRUN) { |
|---|
| 1132 | + if (snd_soc_component_read(component, M98088_REG_51_PWR_SYS) & M98088_SHDNRUN) { |
|---|
| 1120 | 1133 | snd_soc_component_update_bits(component, M98088_REG_51_PWR_SYS, |
|---|
| 1121 | 1134 | M98088_SHDNRUN, 0); |
|---|
| 1122 | 1135 | snd_soc_component_update_bits(component, M98088_REG_51_PWR_SYS, |
|---|
| .. | .. |
|---|
| 1270 | 1283 | return 0; |
|---|
| 1271 | 1284 | } |
|---|
| 1272 | 1285 | |
|---|
| 1273 | | -static int max98088_dai1_digital_mute(struct snd_soc_dai *codec_dai, int mute) |
|---|
| 1286 | +static int max98088_dai1_mute(struct snd_soc_dai *codec_dai, int mute, |
|---|
| 1287 | + int direction) |
|---|
| 1274 | 1288 | { |
|---|
| 1275 | 1289 | struct snd_soc_component *component = codec_dai->component; |
|---|
| 1276 | 1290 | int reg; |
|---|
| .. | .. |
|---|
| 1285 | 1299 | return 0; |
|---|
| 1286 | 1300 | } |
|---|
| 1287 | 1301 | |
|---|
| 1288 | | -static int max98088_dai2_digital_mute(struct snd_soc_dai *codec_dai, int mute) |
|---|
| 1302 | +static int max98088_dai2_mute(struct snd_soc_dai *codec_dai, int mute, |
|---|
| 1303 | + int direction) |
|---|
| 1289 | 1304 | { |
|---|
| 1290 | 1305 | struct snd_soc_component *component = codec_dai->component; |
|---|
| 1291 | 1306 | int reg; |
|---|
| .. | .. |
|---|
| 1310 | 1325 | break; |
|---|
| 1311 | 1326 | |
|---|
| 1312 | 1327 | case SND_SOC_BIAS_PREPARE: |
|---|
| 1328 | + /* |
|---|
| 1329 | + * SND_SOC_BIAS_PREPARE is called while preparing for a |
|---|
| 1330 | + * transition to ON or away from ON. If current bias_level |
|---|
| 1331 | + * is SND_SOC_BIAS_ON, then it is preparing for a transition |
|---|
| 1332 | + * away from ON. Disable the clock in that case, otherwise |
|---|
| 1333 | + * enable it. |
|---|
| 1334 | + */ |
|---|
| 1335 | + if (!IS_ERR(max98088->mclk)) { |
|---|
| 1336 | + if (snd_soc_component_get_bias_level(component) == |
|---|
| 1337 | + SND_SOC_BIAS_ON) |
|---|
| 1338 | + clk_disable_unprepare(max98088->mclk); |
|---|
| 1339 | + else |
|---|
| 1340 | + clk_prepare_enable(max98088->mclk); |
|---|
| 1341 | + } |
|---|
| 1313 | 1342 | break; |
|---|
| 1314 | 1343 | |
|---|
| 1315 | 1344 | case SND_SOC_BIAS_STANDBY: |
|---|
| .. | .. |
|---|
| 1336 | 1365 | .set_sysclk = max98088_dai_set_sysclk, |
|---|
| 1337 | 1366 | .set_fmt = max98088_dai1_set_fmt, |
|---|
| 1338 | 1367 | .hw_params = max98088_dai1_hw_params, |
|---|
| 1339 | | - .digital_mute = max98088_dai1_digital_mute, |
|---|
| 1368 | + .mute_stream = max98088_dai1_mute, |
|---|
| 1369 | + .no_capture_mute = 1, |
|---|
| 1340 | 1370 | }; |
|---|
| 1341 | 1371 | |
|---|
| 1342 | 1372 | static const struct snd_soc_dai_ops max98088_dai2_ops = { |
|---|
| 1343 | 1373 | .set_sysclk = max98088_dai_set_sysclk, |
|---|
| 1344 | 1374 | .set_fmt = max98088_dai2_set_fmt, |
|---|
| 1345 | 1375 | .hw_params = max98088_dai2_hw_params, |
|---|
| 1346 | | - .digital_mute = max98088_dai2_digital_mute, |
|---|
| 1376 | + .mute_stream = max98088_dai2_mute, |
|---|
| 1377 | + .no_capture_mute = 1, |
|---|
| 1347 | 1378 | }; |
|---|
| 1348 | 1379 | |
|---|
| 1349 | 1380 | static struct snd_soc_dai_driver max98088_dai[] = { |
|---|
| .. | .. |
|---|
| 1422 | 1453 | pdata->eq_cfg[best].rate, fs); |
|---|
| 1423 | 1454 | |
|---|
| 1424 | 1455 | /* Disable EQ while configuring, and save current on/off state */ |
|---|
| 1425 | | - save = snd_soc_component_read32(component, M98088_REG_49_CFG_LEVEL); |
|---|
| 1456 | + save = snd_soc_component_read(component, M98088_REG_49_CFG_LEVEL); |
|---|
| 1426 | 1457 | snd_soc_component_update_bits(component, M98088_REG_49_CFG_LEVEL, M98088_EQ1EN, 0); |
|---|
| 1427 | 1458 | |
|---|
| 1428 | 1459 | coef_set = &pdata->eq_cfg[sel]; |
|---|
| .. | .. |
|---|
| 1469 | 1500 | pdata->eq_cfg[best].rate, fs); |
|---|
| 1470 | 1501 | |
|---|
| 1471 | 1502 | /* Disable EQ while configuring, and save current on/off state */ |
|---|
| 1472 | | - save = snd_soc_component_read32(component, M98088_REG_49_CFG_LEVEL); |
|---|
| 1503 | + save = snd_soc_component_read(component, M98088_REG_49_CFG_LEVEL); |
|---|
| 1473 | 1504 | snd_soc_component_update_bits(component, M98088_REG_49_CFG_LEVEL, M98088_EQ2EN, 0); |
|---|
| 1474 | 1505 | |
|---|
| 1475 | 1506 | coef_set = &pdata->eq_cfg[sel]; |
|---|
| .. | .. |
|---|
| 1655 | 1686 | max98088->mic1pre = 0; |
|---|
| 1656 | 1687 | max98088->mic2pre = 0; |
|---|
| 1657 | 1688 | |
|---|
| 1658 | | - ret = snd_soc_component_read32(component, M98088_REG_FF_REV_ID); |
|---|
| 1689 | + ret = snd_soc_component_read(component, M98088_REG_FF_REV_ID); |
|---|
| 1659 | 1690 | if (ret < 0) { |
|---|
| 1660 | 1691 | dev_err(component->dev, "Failed to read device revision: %d\n", |
|---|
| 1661 | 1692 | ret); |
|---|
| .. | .. |
|---|
| 1725 | 1756 | if (IS_ERR(max98088->regmap)) |
|---|
| 1726 | 1757 | return PTR_ERR(max98088->regmap); |
|---|
| 1727 | 1758 | |
|---|
| 1759 | + max98088->mclk = devm_clk_get(&i2c->dev, "mclk"); |
|---|
| 1760 | + if (IS_ERR(max98088->mclk)) |
|---|
| 1761 | + if (PTR_ERR(max98088->mclk) == -EPROBE_DEFER) |
|---|
| 1762 | + return PTR_ERR(max98088->mclk); |
|---|
| 1763 | + |
|---|
| 1728 | 1764 | max98088->devtype = id->driver_data; |
|---|
| 1729 | 1765 | |
|---|
| 1730 | 1766 | i2c_set_clientdata(i2c, max98088); |
|---|
| .. | .. |
|---|
| 1742 | 1778 | }; |
|---|
| 1743 | 1779 | MODULE_DEVICE_TABLE(i2c, max98088_i2c_id); |
|---|
| 1744 | 1780 | |
|---|
| 1781 | +#if defined(CONFIG_OF) |
|---|
| 1782 | +static const struct of_device_id max98088_of_match[] = { |
|---|
| 1783 | + { .compatible = "maxim,max98088" }, |
|---|
| 1784 | + { .compatible = "maxim,max98089" }, |
|---|
| 1785 | + { } |
|---|
| 1786 | +}; |
|---|
| 1787 | +MODULE_DEVICE_TABLE(of, max98088_of_match); |
|---|
| 1788 | +#endif |
|---|
| 1789 | + |
|---|
| 1745 | 1790 | static struct i2c_driver max98088_i2c_driver = { |
|---|
| 1746 | 1791 | .driver = { |
|---|
| 1747 | 1792 | .name = "max98088", |
|---|
| 1793 | + .of_match_table = of_match_ptr(max98088_of_match), |
|---|
| 1748 | 1794 | }, |
|---|
| 1749 | 1795 | .probe = max98088_i2c_probe, |
|---|
| 1750 | 1796 | .id_table = max98088_i2c_id, |
|---|