[PATCH] ASoC: rt5682s: Handle lowercase supply name and fallback if needed

From: Nícolas F. R. A. Prado
Date: Thu Oct 27 2022 - 16:10:22 EST


Supply names provided by devicetree are conventionally lowercase. In
order to be able to use lowercase names without breaking existing
setups, detect if any of the older names are used and if so fallback to
them.

Signed-off-by: N�las F. R. A. Prado <nfraprado@xxxxxxxxxxxxx>
---
sound/soc/codecs/rt5682s.c | 40 ++++++++++++++++++++++++++++++--------
1 file changed, 32 insertions(+), 8 deletions(-)

diff --git a/sound/soc/codecs/rt5682s.c b/sound/soc/codecs/rt5682s.c
index 466a37f3500c..3cefa016be77 100644
--- a/sound/soc/codecs/rt5682s.c
+++ b/sound/soc/codecs/rt5682s.c
@@ -41,11 +41,16 @@ static const struct rt5682s_platform_data i2s_default_platform_data = {
.dai_clk_names[RT5682S_DAI_BCLK_IDX] = "rt5682-dai-bclk",
};

-static const char *rt5682s_supply_names[RT5682S_NUM_SUPPLIES] = {
+static const char *rt5682s_legacy_supply_names[RT5682S_NUM_SUPPLIES] = {
[RT5682S_SUPPLY_AVDD] = "AVDD",
[RT5682S_SUPPLY_MICVDD] = "MICVDD",
};

+static const char *rt5682s_supply_names[RT5682S_NUM_SUPPLIES] = {
+ [RT5682S_SUPPLY_AVDD] = "avdd",
+ [RT5682S_SUPPLY_MICVDD] = "micvdd",
+};
+
static const struct reg_sequence patch_list[] = {
{RT5682S_I2C_CTRL, 0x0007},
{RT5682S_DIG_IN_CTRL_1, 0x0000},
@@ -3090,7 +3095,9 @@ static int rt5682s_i2c_probe(struct i2c_client *i2c)
struct rt5682s_platform_data *pdata = dev_get_platdata(&i2c->dev);
struct rt5682s_priv *rt5682s;
int i, ret;
+ struct regulator *reg;
unsigned int val;
+ bool using_legacy_supply_names = false;

rt5682s = devm_kzalloc(&i2c->dev, sizeof(struct rt5682s_priv), GFP_KERNEL);
if (!rt5682s)
@@ -3112,14 +3119,31 @@ static int rt5682s_i2c_probe(struct i2c_client *i2c)
return ret;
}

- for (i = 0; i < ARRAY_SIZE(rt5682s->supplies); i++)
- rt5682s->supplies[i].supply = rt5682s_supply_names[i];
+ for (i = 0; i < ARRAY_SIZE(rt5682s_supply_names); i++) {
+ reg = devm_regulator_get_optional(&i2c->dev, rt5682s_supply_names[i]);
+ if (IS_ERR(reg)) {
+ if (PTR_ERR(reg) == -ENODEV) {
+ using_legacy_supply_names = true;
+ break;
+ } else {
+ dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
+ return PTR_ERR(reg);
+ }
+ }

- ret = devm_regulator_bulk_get(&i2c->dev,
- ARRAY_SIZE(rt5682s->supplies), rt5682s->supplies);
- if (ret) {
- dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
- return ret;
+ rt5682s->supplies[i].consumer = reg;
+ }
+
+ if (using_legacy_supply_names) {
+ for (i = 0; i < ARRAY_SIZE(rt5682s->supplies); i++)
+ rt5682s->supplies[i].supply = rt5682s_legacy_supply_names[i];
+
+ ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(rt5682s->supplies),
+ rt5682s->supplies);
+ if (ret) {
+ dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
+ return ret;
+ }
}

ret = devm_add_action_or_reset(&i2c->dev, rt5682s_i2c_disable_regulators, rt5682s);
--
2.38.1