diff mbox series

[v1,2/5] pm: add regulator power domain controller

Message ID 20220609150851.23084-3-max.oss.09@gmail.com
State New
Headers show
Series [v1,1/5] dt-bindings: power: Add bindings for a power domain controlled by a regulator | expand

Commit Message

Max Krummenacher June 9, 2022, 3:08 p.m. UTC
From: Max Krummenacher <max.krummenacher@toradex.com>

Acts as a power domain controller which switches its domain power by
enabling/disabling a regulator.

Signed-off-by: Max Krummenacher <max.krummenacher@toradex.com>
---

 drivers/power/Kconfig                |   1 +
 drivers/power/Makefile               |   5 +-
 drivers/power/domain/Kconfig         |   7 ++
 drivers/power/domain/Makefile        |   2 +
 drivers/power/domain/regulator-pdc.c | 112 +++++++++++++++++++++++++++
 5 files changed, 125 insertions(+), 2 deletions(-)
 create mode 100644 drivers/power/domain/Kconfig
 create mode 100644 drivers/power/domain/Makefile
 create mode 100644 drivers/power/domain/regulator-pdc.c
diff mbox series

Patch

diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
index 696bf77a7042..e8eebee629d5 100644
--- a/drivers/power/Kconfig
+++ b/drivers/power/Kconfig
@@ -1,3 +1,4 @@ 
 # SPDX-License-Identifier: GPL-2.0-only
+source "drivers/power/domain/Kconfig"
 source "drivers/power/reset/Kconfig"
 source "drivers/power/supply/Kconfig"
diff --git a/drivers/power/Makefile b/drivers/power/Makefile
index effbf0377f32..4323a8c8c328 100644
--- a/drivers/power/Makefile
+++ b/drivers/power/Makefile
@@ -1,3 +1,4 @@ 
 # SPDX-License-Identifier: GPL-2.0-only
-obj-$(CONFIG_POWER_RESET)	+= reset/
-obj-$(CONFIG_POWER_SUPPLY)	+= supply/
+obj-$(CONFIG_PM_GENERIC_DOMAINS)	+= domain/
+obj-$(CONFIG_POWER_RESET)		+= reset/
+obj-$(CONFIG_POWER_SUPPLY)		+= supply/
diff --git a/drivers/power/domain/Kconfig b/drivers/power/domain/Kconfig
new file mode 100644
index 000000000000..74c50baf2df0
--- /dev/null
+++ b/drivers/power/domain/Kconfig
@@ -0,0 +1,7 @@ 
+# SPDX-License-Identifier: GPL-2.0-only
+
+config POWER_DOMAIN_REGULATOR
+	bool "A power domain controller using a regulator for power control"
+	help
+	  Say Y here to enable a power domain controller which uses a regulator
+	  to control its power domain.
diff --git a/drivers/power/domain/Makefile b/drivers/power/domain/Makefile
new file mode 100644
index 000000000000..13b3378fb11f
--- /dev/null
+++ b/drivers/power/domain/Makefile
@@ -0,0 +1,2 @@ 
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_POWER_DOMAIN_REGULATOR)	+= regulator-pdc.o
diff --git a/drivers/power/domain/regulator-pdc.c b/drivers/power/domain/regulator-pdc.c
new file mode 100644
index 000000000000..e03cfe901d70
--- /dev/null
+++ b/drivers/power/domain/regulator-pdc.c
@@ -0,0 +1,112 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A power domain controller which uses a regulator to control its power domain.
+ *
+ * Copyright 2022 Toradex
+ */
+
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/pm_domain.h>
+#include <linux/regulator/consumer.h>
+
+struct reg_pd {
+	struct device *dev;
+	struct generic_pm_domain pd;
+	struct regulator *supply;
+};
+
+static int reg_pd_power_off(struct generic_pm_domain *domain)
+{
+	struct reg_pd *pd = container_of(domain, struct reg_pd, pd);
+
+	return regulator_disable(pd->supply);
+}
+
+static int reg_pd_power_on(struct generic_pm_domain *domain)
+{
+	struct reg_pd *pd = container_of(domain, struct reg_pd, pd);
+
+	return regulator_enable(pd->supply);
+}
+
+static const struct of_device_id reg_pd_of_match[] = {
+	{
+		.compatible = "regulator-pm-pd",
+	},
+	{ },
+};
+
+static int reg_pd_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	struct of_phandle_args child, parent;
+	struct reg_pd *pd;
+	const char *name;
+	int ret;
+
+	pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
+	if (!pd) {
+		of_node_put(np);
+		return -ENOMEM;
+	}
+	pd->dev = dev;
+
+	if (of_property_read_string(np, "label", &name) < 0)
+		name = kbasename(np->full_name);
+	pd->pd.name = kstrdup_const(name, GFP_KERNEL);
+	if (!pd->pd.name)
+		return -ENOMEM;
+
+	pd->supply = devm_regulator_get(dev, "power");
+	if (IS_ERR(pd->supply))
+		return dev_err_probe(dev, PTR_ERR(pd->supply),
+				     "failed to get regulator\n");
+
+	pd->pd.power_off = reg_pd_power_off;
+	pd->pd.power_on = reg_pd_power_on;
+
+	ret = pm_genpd_init(&pd->pd, NULL, true);
+	if (ret)
+		goto err_out;
+	ret = of_genpd_add_provider_simple(np, &pd->pd);
+	if (ret)
+		goto err_out;
+
+	if (of_parse_phandle_with_args(np, "power-domains",
+				      "#power-domain-cells", 0, &parent) == 0) {
+		child.np = np;
+		child.args_count = 0;
+
+		if (of_genpd_add_subdomain(&parent, &child))
+			pr_warn("%pOF failed to add subdomain: %pOF\n",
+				parent.np, child.np);
+		else
+			pr_info("%pOF has as child subdomain: %pOF.\n",
+				parent.np, child.np);
+	}
+
+	return 0;
+
+err_out:
+	dev_warn(dev, "failed to probe pd %s", pd->pd.name);
+	return ret;
+}
+
+static struct platform_driver reg_pd_driver = {
+	.driver = {
+		.name	= "regulator_power_domain",
+		.of_match_table = reg_pd_of_match,
+		.owner	= THIS_MODULE,
+	},
+	.probe = reg_pd_probe,
+};
+
+static __init int reg_pd_init(void)
+{
+	return platform_driver_register(&reg_pd_driver);
+}
+core_initcall(reg_pd_init);