From 83b8042140a9020686f592ea013ef92a333d942c Mon Sep 17 00:00:00 2001
From: Ondrej Jirman <megi@xff.cz>
Date: Thu, 4 Apr 2024 21:43:13 +0200
Subject: [PATCH 095/463] iio: stk3310: Fix regulator disable/enable order

Signed-off-by: Ondrej Jirman <megi@xff.cz>
[Aren: Added delay after vdd regulator enable]
Signed-off-by: Aren Moynihan <aren@peacevolution.org>
---
 drivers/iio/light/stk3310.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/drivers/iio/light/stk3310.c b/drivers/iio/light/stk3310.c
index 665f298f61491..9813bebcdbd9b 100644
--- a/drivers/iio/light/stk3310.c
+++ b/drivers/iio/light/stk3310.c
@@ -719,12 +719,12 @@ static int stk3310_suspend(struct device *dev)
 	if (ret)
 		return ret;
 
-	if (data->vdd_reg) {
+	if (data->vdd_reg)
 		regcache_mark_dirty(data->regmap);
-		regulator_disable(data->vdd_reg);
-	}
 
 	regulator_disable(data->i2c_reg);
+	if (data->vdd_reg)
+		regulator_disable(data->vdd_reg);
 
 	return 0;
 }
@@ -737,23 +737,24 @@ static int stk3310_resume(struct device *dev)
 
 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
 
+	ret = regulator_enable(data->i2c_reg);
+	if (ret) {
+		dev_err(dev, "Failed to re-enable regulator i2c\n");
+		return ret;
+	}
+
 	if (data->vdd_reg) {
 		ret = regulator_enable(data->vdd_reg);
 		if (ret) {
+			regulator_disable(data->i2c_reg);
 			dev_err(dev, "Failed to re-enable regulator vdd\n");
 			return ret;
 		}
 
+		usleep_range(1000, 2000);
 		regcache_sync(data->regmap);
 	}
 
-	ret = regulator_enable(data->i2c_reg);
-	if (ret) {
-		dev_err(dev, "Failed to re-enable regulator i2c\n");
-		regulator_disable(data->vdd_reg);
-		return ret;
-	}
-
 	if (data->ps_enabled)
 		state |= STK3310_STATE_EN_PS;
 	if (data->als_enabled)
-- 
2.46.0

