Devicetree
 help / color / mirror / Atom feed
From: David Yang <mmyangfl@gmail.com>
To: Liam Girdwood <lgirdwood@gmail.com>, Mark Brown <broonie@kernel.org>
Cc: David Yang <mmyangfl@gmail.com>, Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	linux-kernel@vger.kernel.org, devicetree@vger.kernel.org
Subject: [PATCH 2/2] regulator: histb: add voltage regulator driver
Date: Thu,  8 Oct 2026 23:57:25 +0800	[thread overview]
Message-ID: <20261008155727.3801095-3-mmyangfl@gmail.com> (raw)
In-Reply-To: <20261008155727.3801095-1-mmyangfl@gmail.com>

Add a driver for the voltage rails on HiSilicon STB SoCs which are
controlled by the PMOC block through a PWM signal, as described by the
hisilicon,histb-volt binding.

Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 MAINTAINERS                         |   7 ++
 drivers/regulator/Kconfig           |   7 ++
 drivers/regulator/Makefile          |   1 +
 drivers/regulator/histb-regulator.c | 163 ++++++++++++++++++++++++++++
 4 files changed, 178 insertions(+)
 create mode 100644 drivers/regulator/histb-regulator.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 65e8a4b5c90b..a77731922891 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11910,6 +11910,13 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/mfd/hisilicon,hi6421-spmi-pmic.yaml
 F:	drivers/mfd/hi6421-spmi-pmic.c
 
+HISILICON STB VOLTAGE REGULATOR DRIVER
+M:	David Yang <mmyangfl@gmail.com>
+L:	linux-kernel@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/regulator/hisilicon,histb-volt.yaml
+F:	drivers/regulator/histb-regulator.c
+
 HISILICON TRUE RANDOM NUMBER GENERATOR V2 SUPPORT
 M:	Weili Qian <qianweili@huawei.com>
 S:	Maintained
diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
index 89789ac7a786..210130783f72 100644
--- a/drivers/regulator/Kconfig
+++ b/drivers/regulator/Kconfig
@@ -487,6 +487,13 @@ config REGULATOR_HI6421V600
 	  HiSilicon Hi6421v600 PMU / Codec IC.
 	  This is used on Kirin 3670 boards, like HiKey 970.
 
+config REGULATOR_HISTB
+	tristate "HiSilicon STB voltage regulator support"
+	depends on ARCH_HISI || COMPILE_TEST
+	help
+	  This driver provides support for the voltage regulators of the
+	  HiSilicon STB SoCs, including Hi3798cv200, Hi3798mv100, Hi3798mv200.
+
 config REGULATOR_ISL9305
 	tristate "Intersil ISL9305 regulator"
 	depends on I2C
diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile
index 5a764cec8df8..9fa44d180458 100644
--- a/drivers/regulator/Makefile
+++ b/drivers/regulator/Makefile
@@ -57,6 +57,7 @@ obj-$(CONFIG_REGULATOR_HI6421) += hi6421-regulator.o
 obj-$(CONFIG_REGULATOR_HI6421V530) += hi6421v530-regulator.o
 obj-$(CONFIG_REGULATOR_HI6421V600) += hi6421v600-regulator.o
 obj-$(CONFIG_REGULATOR_HI655X) += hi655x-regulator.o
+obj-$(CONFIG_REGULATOR_HISTB) += histb-regulator.o
 obj-$(CONFIG_REGULATOR_ISL6271A) += isl6271a-regulator.o
 obj-$(CONFIG_REGULATOR_ISL9305) += isl9305.o
 obj-$(CONFIG_REGULATOR_FP9931) += fp9931.o
diff --git a/drivers/regulator/histb-regulator.c b/drivers/regulator/histb-regulator.c
new file mode 100644
index 000000000000..77073a646e49
--- /dev/null
+++ b/drivers/regulator/histb-regulator.c
@@ -0,0 +1,163 @@
+// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
+/*
+ * Regulator driver for the PWM controlled voltage regulators of HiSilicon STB
+ * SoCs
+ *
+ * May work on Hi3716mv410/420 Hi3796mv100/200 Hi3798cv200/mv100/mv200 but not
+ * throughly tested
+ *
+ * Copyright (c) 2026 David Yang
+ */
+
+#include <linux/bitfield.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/math64.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regulator/driver.h>
+#include <linux/regulator/machine.h>
+#include <linux/regulator/of_regulator.h>
+
+/*
+ * Duty cycle + 1 and period + 1 are counted in periods of the oscillator
+ * clock (usually 24 MHz), and a larger duty cycle means a lower output
+ * voltage.
+ */
+#define HISTB_DUTY		GENMASK(31, 16)
+#define HISTB_PERIOD		GENMASK(15, 0)
+
+/* 24 MHz / 240 = 100 kHz, arbitrarily chosen but should be enough */
+#define HISTB_PWM_PERIOD	240
+
+struct histb_regulator {
+	struct regulator_desc desc;
+	void __iomem *base;
+	int min_uV;
+	int max_uV;
+};
+
+static int histb_regulator_get_voltage(struct regulator_dev *rdev)
+{
+	struct histb_regulator *priv = rdev_get_drvdata(rdev);
+	u32 val;
+	u32 period;
+	u32 duty;
+	u64 uV;
+
+	val = readl_relaxed(priv->base);
+	period = FIELD_GET(HISTB_PERIOD, val);
+	duty = FIELD_GET(HISTB_DUTY, val);
+
+	/* an empty counter range leaves the output high */
+	if (duty <= 1 || period <= 1)
+		return priv->max_uV;
+
+	duty--;
+	period--;
+	if (duty >= period)
+		return priv->min_uV;
+
+	uV = (u64)(priv->max_uV - priv->min_uV) * duty;
+
+	return priv->max_uV - DIV_ROUND_CLOSEST_ULL(uV, period);
+}
+
+static int histb_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
+				       int max_uV, unsigned int *selector)
+{
+	struct histb_regulator *priv = rdev_get_drvdata(rdev);
+	unsigned int duty;
+	u32 val;
+
+	if (min_uV >= priv->max_uV)
+		duty = 0;
+	else if (min_uV <= priv->min_uV)
+		duty = HISTB_PWM_PERIOD;
+	else
+		/* rounding down keeps the output at or above the request */
+		duty = DIV_ROUND_DOWN_ULL((u64)(priv->max_uV - min_uV) *
+					  HISTB_PWM_PERIOD,
+					  priv->max_uV - priv->min_uV);
+
+	val = FIELD_PREP(HISTB_DUTY, duty + 1) |
+	      FIELD_PREP(HISTB_PERIOD, HISTB_PWM_PERIOD + 1);
+	writel_relaxed(val, priv->base);
+
+	return 0;
+}
+
+static const struct regulator_ops histb_regulator_ops = {
+	.get_voltage = histb_regulator_get_voltage,
+	.set_voltage = histb_regulator_set_voltage,
+};
+
+static int histb_regulator_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct histb_regulator *priv;
+	struct regulator_init_data *init_data;
+	struct regulator_config config = {};
+	struct regulator_dev *rdev;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	init_data = of_get_regulator_init_data(dev, dev->of_node, &priv->desc);
+	if (!init_data)
+		return -ENOMEM;
+
+	priv->min_uV = init_data->constraints.min_uV;
+	priv->max_uV = init_data->constraints.max_uV;
+	if (priv->min_uV <= 0 || priv->max_uV <= priv->min_uV) {
+		dev_err(dev, "invalid voltage range (%d..%d uV)\n",
+			priv->min_uV, priv->max_uV);
+		return -EINVAL;
+	}
+
+	priv->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(priv->base))
+		return PTR_ERR(priv->base);
+
+	priv->desc.name = dev_name(dev);
+	priv->desc.type = REGULATOR_VOLTAGE;
+	priv->desc.ops = &histb_regulator_ops;
+	priv->desc.owner = THIS_MODULE;
+	priv->desc.continuous_voltage_range = true;
+
+	config.dev = dev;
+	config.init_data = init_data;
+	config.driver_data = priv;
+	config.of_node = dev->of_node;
+
+	rdev = devm_regulator_register(dev, &priv->desc, &config);
+	if (IS_ERR(rdev))
+		return dev_err_probe(dev, PTR_ERR(rdev),
+				     "failed to register %s\n",
+				     priv->desc.name);
+
+	return 0;
+}
+
+static const struct of_device_id histb_regulator_of_match[] = {
+	{ .compatible = "hisilicon,hi3798cv200-volt" },
+	{ .compatible = "hisilicon,hi3798mv100-volt" },
+	{ .compatible = "hisilicon,hi3798mv200-volt" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, histb_regulator_of_match);
+
+static struct platform_driver histb_regulator_driver = {
+	.probe = histb_regulator_probe,
+	.driver = {
+		.name = "histb-regulator",
+		.of_match_table = histb_regulator_of_match,
+	},
+};
+module_platform_driver(histb_regulator_driver);
+
+MODULE_DESCRIPTION("HiSilicon STB PWM voltage regulator driver");
+MODULE_AUTHOR("David Yang <mmyangfl@gmail.com>");
+MODULE_LICENSE("Dual MIT/GPL");
-- 
2.53.0


  parent reply	other threads:[~2026-10-08 15:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-08 15:57 [PATCH 0/2] regulator: support the HiSilicon STB PMOC voltage rails David Yang
2026-10-08 15:57 ` [PATCH 1/2] dt-bindings: regulator: add HiSilicon STB voltage regulator David Yang
2026-10-09 18:50   ` Conor Dooley
2026-10-08 15:57 ` David Yang [this message]
2026-10-08 16:06   ` [PATCH 2/2] regulator: histb: add voltage regulator driver sashiko-bot
2026-10-08 16:37     ` David Yang

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=20261008155727.3801095-3-mmyangfl@gmail.com \
    --to=mmyangfl@gmail.com \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh@kernel.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