Skip to contentSkip to conversation

Input: elan_i2c - Move regulator_[en|dis]able() out of elan_[en|dis]able_power()

PATCH New Hans de Goede 1 participant 2 messages Jan 31, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 Input: elan_i2c - Move regulator_[en|dis]able() out of elan_[en|dis]able_power()

elan_disable_power() is called conditionally on suspend, where as elan_enable_power() is always called on resume. This leads to an imbalance in the regulator's enable count.

Move the regulator_[en|dis]able() calls out of elan_[en|dis]able_power() in preparation of fixing this.

No functional changes intended.

---
Changes in v2:
- New patch in v2 of this patch-set

---
 drivers/input/mouse/elan_i2c_core.c | 36 ++++++++++++++---------------
 1 file changed, 17 insertions(+), 19 deletions(-)
drivers/input/mouse/elan_i2c_core.c +17 -19
index 47af62c12267..d8965f3c6bf5 100644
@@ -191,13 +191,6 @@ static int elan_enable_power(struct elan_tp_data *data)
 	int repeat = ETP_RETRY_COUNT;
 	int error;
 
-	error = regulator_enable(data->vcc);
-	if (error) {
-		dev_err(&data->client->dev,
-			"failed to enable regulator: %d\n", error);
-		return error;
-	}
-
 	do {
 		error = data->ops->power_control(data->client, true);
 		if (error >= 0)
@@ -217,19 +210,8 @@ static int elan_disable_power(struct elan_tp_data *data)
 
 	do {
 		error = data->ops->power_control(data->client, false);
-		if (!error) {
-			error = regulator_disable(data->vcc);
-			if (error) {
-				dev_err(&data->client->dev,
-					"failed to disable regulator: %d\n",
-					error);
-				/* Attempt to power the chip back up */
-				data->ops->power_control(data->client, true);
-				break;
-			}
-
+		if (!error)
 			return 0;
-		}
 
 		msleep(30);
 	} while (--repeat > 0);
@@ -1400,8 +1382,18 @@ static int __maybe_unused elan_suspend(struct device *dev)
 		data->irq_wake = (enable_irq_wake(client->irq) == 0);
 	} else {
 		ret = elan_disable_power(data);
+		if (ret)
+			goto err;
+
+		ret = regulator_disable(data->vcc);
+		if (ret) {
+			dev_err(dev, "error %d disabling regulator\n", ret);
+			/* Attempt to power the chip back up */
+			elan_enable_power(data);
+		}
 	}
 
+err:
 	mutex_unlock(&data->sysfs_mutex);
 	return ret;
 }
@@ -1417,6 +1409,12 @@ static int __maybe_unused elan_resume(struct device *dev)
 		data->irq_wake = false;
 	}
 
+	error = regulator_enable(data->vcc);
+	if (error) {
+		dev_err(dev, "error %d enabling regulator\n", error);
+		goto err;
+	}
+
 	error = elan_enable_power(data);
 	if (error) {
 		dev_err(dev, "power up when resuming failed: %d\n", error);
Signature · 1 line
2.33.1
#1 lore raw

PATCH v2 2/2 Input: elan_i2c - Fix regulator enable count imbalance after suspend/resume

Before these changes elan_suspend() would only disable the regulator when device_may_wakeup() returns false; whereas elan_resume() would unconditionally enable it, leading to an enable count imbalance when device_may_wakeup() returns true.

This triggers the "WARN_ON(regulator->enable_count)" in regulator_put() when the elan_i2c driver gets unbound, this happens e.g. with the hot-plugable dock with Elan I2C touchpad for the Asus TF103C 2-in-1.

Fix this by making the regulator_enable() call also be conditional on device_may_wakeup() returning false.

---
Changes in v2:
-Only make the regulator_enable() conditional, leaving the behavior
of always calling elan_enable_power() on resume intact

---
 drivers/input/mouse/elan_i2c_core.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/input/mouse/elan_i2c_core.c +7 -7
index d8965f3c6bf5..4dbf4dc78aa4 100644
@@ -1404,17 +1404,17 @@ static int __maybe_unused elan_resume(struct device *dev)
 	struct elan_tp_data *data = i2c_get_clientdata(client);
 	int error;
 
-	if (device_may_wakeup(dev) && data->irq_wake) {
+	if (!device_may_wakeup(dev)) {
+		error = regulator_enable(data->vcc);
+		if (error) {
+			dev_err(dev, "error %d enabling regulator\n", error);
+			goto err;
+		}
+	} else if (data->irq_wake) {
 		disable_irq_wake(client->irq);
 		data->irq_wake = false;
 	}
 
-	error = regulator_enable(data->vcc);
-	if (error) {
-		dev_err(dev, "error %d enabling regulator\n", error);
-		goto err;
-	}
-
 	error = elan_enable_power(data);
 	if (error) {
 		dev_err(dev, "power up when resuming failed: %d\n", error);
Signature · 1 line
2.33.1