power: domain: Introduce dev_power_domain_off

Add dev_power_domain_off() api to disable all the power-domains
corresponding to a device

Signed-off-by: Lokesh Vutla <lokeshvutla@ti.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
diff --git a/drivers/power/domain/power-domain-uclass.c b/drivers/power/domain/power-domain-uclass.c
index c961436..80df5af 100644
--- a/drivers/power/domain/power-domain-uclass.c
+++ b/drivers/power/domain/power-domain-uclass.c
@@ -7,6 +7,7 @@
 #include <dm.h>
 #include <power-domain.h>
 #include <power-domain-uclass.h>
+#include <dm/device-internal.h>
 
 static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev)
 {
@@ -107,11 +108,11 @@
 	return ops->off(power_domain);
 }
 
-#if !CONFIG_IS_ENABLED(OF_PLATDATA)
-int dev_power_domain_on(struct udevice *dev)
+#if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
+static int dev_power_domain_ctrl(struct udevice *dev, bool on)
 {
 	struct power_domain pd;
-	int i, count, ret;
+	int i, count, ret = 0;
 
 	count = dev_count_phandle_with_args(dev, "power-domains",
 					    "#power-domain-cells");
@@ -119,12 +120,32 @@
 		ret = power_domain_get_by_index(dev, &pd, i);
 		if (ret)
 			return ret;
-		ret = power_domain_on(&pd);
-		if (ret)
-			return ret;
+		if (on)
+			ret = power_domain_on(&pd);
+		else
+			ret = power_domain_off(&pd);
 	}
 
-	return 0;
+	/*
+	 * power_domain_get() bound the device, thus
+	 * we must remove it again to prevent unbinding
+	 * active devices (which would result in unbind
+	 * error).
+	 */
+	if (count > 0 && !on)
+		device_remove(pd.dev, DM_REMOVE_NORMAL);
+
+	return ret;
+}
+
+int dev_power_domain_on(struct udevice *dev)
+{
+	return dev_power_domain_ctrl(dev, true);
+}
+
+int dev_power_domain_off(struct udevice *dev)
+{
+	return dev_power_domain_ctrl(dev, false);
 }
 #endif
 
diff --git a/include/power-domain.h b/include/power-domain.h
index 490fedb..72ff2ff 100644
--- a/include/power-domain.h
+++ b/include/power-domain.h
@@ -172,4 +172,21 @@
 }
 #endif
 
+/**
+ * dev_power_domain_off - Disable power domains for a device .
+ *
+ * @dev:		The client device.
+ *
+ * @return 0 if OK, or a negative error code.
+ */
+#if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) && \
+	CONFIG_IS_ENABLED(POWER_DOMAIN)
+int dev_power_domain_off(struct udevice *dev);
+#else
+static inline int dev_power_domain_off(struct udevice *dev)
+{
+	return 0;
+}
+#endif
+
 #endif