@@ -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"
@@ -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/
new file mode 100644
@@ -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.
new file mode 100644
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_POWER_DOMAIN_REGULATOR) += regulator-pdc.o
new file mode 100644
@@ -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(®_pd_driver);
+}
+core_initcall(reg_pd_init);