devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shenwei Wang <shenwei.wang@nxp.com>
To: Rob Herring <robh+dt@kernel.org>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	Conor Dooley <conor+dt@kernel.org>
Cc: Shenwei Wang <shenwei.wang@nxp.com>,
	Ulf Hansson <ulf.hansson@linaro.org>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	imx@lists.linux.dev, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-imx@nxp.com
Subject: [PATCH 2/2] genpd: regulator-pd: add regulator-pd driver
Date: Fri, 18 Aug 2023 10:34:46 -0500	[thread overview]
Message-ID: <20230818153446.1076027-2-shenwei.wang@nxp.com> (raw)
In-Reply-To: <20230818153446.1076027-1-shenwei.wang@nxp.com>

This driver implements a power domain by wrapping a group of regulators
into a unified genpd abstraction. The power domain is defined in the
device tree, allowing the driver framework to automatically manage
the power for the those devices which use the power domain. This removes
the need to manually adjust each individual driver to directly control
the regulators. The solution can also take the benefit of runtime PM
subsystem, and the device's power can be managed automatically when the
device is idle.

Signed-off-by: Shenwei Wang <shenwei.wang@nxp.com>
---
 drivers/genpd/Makefile       |   1 +
 drivers/genpd/regulator-pd.c | 145 +++++++++++++++++++++++++++++++++++
 2 files changed, 146 insertions(+)
 create mode 100644 drivers/genpd/regulator-pd.c

diff --git a/drivers/genpd/Makefile b/drivers/genpd/Makefile
index 666753676e5c..5eacd7ea2a2d 100644
--- a/drivers/genpd/Makefile
+++ b/drivers/genpd/Makefile
@@ -15,3 +15,4 @@ obj-y					+= sunxi/
 obj-y					+= tegra/
 obj-y					+= ti/
 obj-y					+= xilinx/
+obj-y					+= regulator-pd.o
diff --git a/drivers/genpd/regulator-pd.c b/drivers/genpd/regulator-pd.c
new file mode 100644
index 000000000000..dc30a81d7e65
--- /dev/null
+++ b/drivers/genpd/regulator-pd.c
@@ -0,0 +1,145 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2023 NXP Semiconductor, Inc.
+ */
+
+#include <linux/platform_device.h>
+#include <linux/pm_domain.h>
+#include <linux/regulator/consumer.h>
+
+struct regulator_power_domain {
+	struct generic_pm_domain genpd;
+	struct device *dev;
+	struct regulator *reg;
+	int idx;
+};
+
+#define to_regulator_power_domain(_genpd) container_of(_genpd, struct regulator_power_domain, genpd)
+
+static int regulator_power_domain_on(struct generic_pm_domain *genpd)
+{
+	struct regulator_power_domain *domain = to_regulator_power_domain(genpd);
+
+	if (IS_ERR_OR_NULL(domain->reg))
+		return -ENODEV;
+
+	return regulator_enable(domain->reg);
+}
+
+static int regulator_power_domain_off(struct generic_pm_domain *genpd)
+{
+	struct regulator_power_domain *domain = to_regulator_power_domain(genpd);
+
+	if (IS_ERR_OR_NULL(domain->reg))
+		return -ENODEV;
+
+	return regulator_disable(domain->reg);
+}
+
+static struct generic_pm_domain *
+regulator_power_domain_xlate(struct of_phandle_args *spec, void *data)
+{
+	struct generic_pm_domain *domain = ERR_PTR(-ENOENT);
+	struct genpd_onecell_data *pd_data = data;
+	struct regulator_power_domain *rpd;
+	unsigned int i;
+
+	for (i = 0; i < pd_data->num_domains; i++) {
+		rpd = to_regulator_power_domain(pd_data->domains[i]);
+		if (rpd->idx == spec->args[0]) {
+			domain = &rpd->genpd;
+			break;
+		}
+	}
+
+	return domain;
+}
+
+static int regulator_power_domain_probe(struct platform_device *pdev)
+{
+	struct regulator_power_domain *gpd;
+	struct genpd_onecell_data *pd_data;
+	struct device *dev = &pdev->dev;
+	struct generic_pm_domain **pds;
+	char name[16];
+	u32 num;
+	int i;
+
+	if (of_property_read_u32(dev->of_node, "regulator-number", &num))
+		return -ENODEV;
+
+	if (num < 1)
+		return -EINVAL;
+
+	/* Limit the regulator count to <=100 */
+	if (num > 100)
+		num = 100;
+
+	pd_data = devm_kzalloc(dev, sizeof(*pd_data), GFP_KERNEL);
+	pds = devm_kcalloc(dev, num, sizeof(*pds), GFP_KERNEL);
+	gpd = devm_kcalloc(dev, num, sizeof(*gpd), GFP_KERNEL);
+
+	if (!pds || !pd_data || !gpd)
+		return -ENOMEM;
+
+	for (i = 0; i < num; i++) {
+		bool is_off = 1;
+
+		snprintf(name, 16, "regulator-%d", i);
+		gpd->reg = devm_regulator_get_optional(dev, name);
+
+		/* Let the initial pd state as is as the regulator's */
+		if (!(IS_ERR_OR_NULL(gpd->reg)))
+			is_off = (regulator_is_enabled(gpd->reg) > 0) ? 0 : 1;
+
+		pm_genpd_init(&gpd->genpd, NULL, is_off);
+
+		gpd->genpd.power_off = regulator_power_domain_off;
+		gpd->genpd.power_on = regulator_power_domain_on;
+		gpd->genpd.name = dev_name(dev);
+		gpd->dev = dev;
+		gpd->idx = i;
+		pds[i] = &gpd->genpd;
+		gpd++;
+	}
+
+	pd_data->domains = pds;
+	pd_data->num_domains = num;
+	pd_data->xlate = regulator_power_domain_xlate;
+	platform_set_drvdata(pdev, pd_data);
+
+	return of_genpd_add_provider_onecell(dev->of_node, pd_data);
+}
+
+static int regulator_power_domain_remove(struct platform_device *pdev)
+{
+	struct genpd_onecell_data *pd_data = platform_get_drvdata(pdev);
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	int i;
+
+	of_genpd_del_provider(np);
+	for (i = 0; i < pd_data->num_domains; i++)
+		pm_genpd_remove(pd_data->domains[i]);
+
+	return 0;
+}
+
+static const struct of_device_id regulator_power_domain_dt_ids[] = {
+	{ .compatible = "regulator-power-domain"},
+	{ }
+};
+
+static struct platform_driver regulator_power_domain_driver = {
+	.driver = {
+		.name = "regulator2pd",
+		.of_match_table = regulator_power_domain_dt_ids,
+	},
+	.probe = regulator_power_domain_probe,
+	.remove = regulator_power_domain_remove,
+};
+builtin_platform_driver(regulator_power_domain_driver)
+
+MODULE_AUTHOR("Shenwei Wang <shenwei.wang@nxp.com>");
+MODULE_DESCRIPTION("regulator power domain driver");
+MODULE_LICENSE("GPL");
-- 
2.34.1


  reply	other threads:[~2023-08-18 15:36 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-18 15:34 [PATCH 1/2] dt-bindings: power: Add regulator-pd yaml file Shenwei Wang
2023-08-18 15:34 ` Shenwei Wang [this message]
2023-08-18 20:51 ` Rob Herring
2023-08-18 21:06   ` Shenwei Wang
2023-08-19  8:04     ` Krzysztof Kozlowski
2023-08-21 13:22       ` Shenwei Wang
2023-08-21 18:49         ` Rob Herring
2023-08-22 15:18           ` Shenwei Wang
2023-08-22 15:25             ` Krzysztof Kozlowski
2023-08-22 15:50               ` [EXT] " Shenwei Wang
2023-08-22 15:57                 ` Krzysztof Kozlowski
2023-08-22 16:14                   ` Shenwei Wang
2023-08-24  9:26 ` Ulf Hansson
2023-08-24 16:35   ` Shenwei Wang
2023-08-25 12:24     ` Ulf Hansson
2023-08-25 15:44       ` Shenwei Wang
2023-08-26 17:31         ` Krzysztof Kozlowski
2023-08-28  9:59           ` Ulf Hansson
2023-08-28 10:45             ` Krzysztof Kozlowski
2023-08-28 14:14             ` Shenwei Wang
2023-08-28 14:04           ` Shenwei Wang
2023-08-28 17:10             ` Krzysztof Kozlowski
2023-08-28 18:39               ` Shenwei Wang
2023-08-28 18:42                 ` Krzysztof Kozlowski
2023-08-28 18:50                   ` Shenwei Wang
2023-08-28 18:52                     ` Krzysztof Kozlowski
2023-08-28 19:09                       ` Shenwei Wang
2023-08-28 19:11                         ` Krzysztof Kozlowski
2023-08-28 19:23                           ` [EXT] " Shenwei Wang
2023-08-28 19:30                             ` Krzysztof Kozlowski
2023-08-28 19:49                               ` Shenwei Wang
2023-08-28 21:13                                 ` Rob Herring
2023-08-29 13:21                                   ` Shenwei Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230818153446.1076027-2-shenwei.wang@nxp.com \
    --to=shenwei.wang@nxp.com \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=imx@lists.linux.dev \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-imx@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=ulf.hansson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).