All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/2] pmic: pca9450: Add upstream regulators subnode match
@ 2022-05-20  3:10 Marek Vasut
  2022-05-20  3:10 ` [PATCH v3 2/2] pmic: pca9450: Add regulator driver Marek Vasut
  2022-05-20 13:44 ` [PATCH v3 1/2] pmic: pca9450: Add upstream regulators subnode match sbabic
  0 siblings, 2 replies; 9+ messages in thread
From: Marek Vasut @ 2022-05-20  3:10 UTC (permalink / raw)
  To: u-boot, sbabic; +Cc: Marek Vasut, Fabio Estevam, Peng Fan

The upstream DT regulators node subnodes are named BUCKn and LDOn,
the downstream DT regulators node subnodes are named buckn and ldon,
add the upstream match.

Reviewed-by: Fabio Estevam <festevam@denx.de>
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Fabio Estevam <festevam@denx.de>
Cc: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
---
V2: Add RB by Fabio
V3: No change
---
 drivers/power/pmic/pca9450.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/power/pmic/pca9450.c b/drivers/power/pmic/pca9450.c
index 2394b196c56..26c876c9c45 100644
--- a/drivers/power/pmic/pca9450.c
+++ b/drivers/power/pmic/pca9450.c
@@ -19,8 +19,10 @@ DECLARE_GLOBAL_DATA_PTR;
 static const struct pmic_child_info pmic_children_info[] = {
 	/* buck */
 	{ .prefix = "b", .driver = PCA9450_REGULATOR_DRIVER},
+	{ .prefix = "B", .driver = PCA9450_REGULATOR_DRIVER},
 	/* ldo */
 	{ .prefix = "l", .driver = PCA9450_REGULATOR_DRIVER},
+	{ .prefix = "L", .driver = PCA9450_REGULATOR_DRIVER},
 	{ },
 };
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v3 2/2] pmic: pca9450: Add regulator driver
  2022-05-20  3:10 [PATCH v3 1/2] pmic: pca9450: Add upstream regulators subnode match Marek Vasut
@ 2022-05-20  3:10 ` Marek Vasut
  2022-05-20 13:44   ` sbabic
  2022-05-20 13:44 ` [PATCH v3 1/2] pmic: pca9450: Add upstream regulators subnode match sbabic
  1 sibling, 1 reply; 9+ messages in thread
From: Marek Vasut @ 2022-05-20  3:10 UTC (permalink / raw)
  To: u-boot, sbabic; +Cc: Marek Vasut, Fabio Estevam, Peng Fan

Add PCA9450 regulator driver. This is complementary driver for the BUCKn
and LDOn regulators provided by the PCA9450 PMIC driver. Currently the
driver permits reading the settngs and configuring the BUCKn and LDOn
regulators.

Reviewed-by: Fabio Estevam <festevam@denx.de>
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Fabio Estevam <festevam@denx.de>
Cc: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
---
V2: Add RB by Fabio
V3: Rename DVS_BUCK_RUN_MASK to PCA9450_DVS_BUCK_RUN_MASK
---
 drivers/power/pmic/pca9450.c      |   6 +-
 drivers/power/regulator/Kconfig   |  15 ++
 drivers/power/regulator/Makefile  |   1 +
 drivers/power/regulator/pca9450.c | 333 ++++++++++++++++++++++++++++++
 include/power/pca9450.h           |  11 +
 5 files changed, 363 insertions(+), 3 deletions(-)
 create mode 100644 drivers/power/regulator/pca9450.c

diff --git a/drivers/power/pmic/pca9450.c b/drivers/power/pmic/pca9450.c
index 26c876c9c45..116ac49a8db 100644
--- a/drivers/power/pmic/pca9450.c
+++ b/drivers/power/pmic/pca9450.c
@@ -83,9 +83,9 @@ static struct dm_pmic_ops pca9450_ops = {
 };
 
 static const struct udevice_id pca9450_ids[] = {
-	{ .compatible = "nxp,pca9450a", .data = 0x25, },
-	{ .compatible = "nxp,pca9450b", .data = 0x25, },
-	{ .compatible = "nxp,pca9450c", .data = 0x25, },
+	{ .compatible = "nxp,pca9450a", .data = NXP_CHIP_TYPE_PCA9450A, },
+	{ .compatible = "nxp,pca9450b", .data = NXP_CHIP_TYPE_PCA9450BC, },
+	{ .compatible = "nxp,pca9450c", .data = NXP_CHIP_TYPE_PCA9450BC, },
 	{ }
 };
 
diff --git a/drivers/power/regulator/Kconfig b/drivers/power/regulator/Kconfig
index 9145408b3c9..d6cea8ec666 100644
--- a/drivers/power/regulator/Kconfig
+++ b/drivers/power/regulator/Kconfig
@@ -60,6 +60,21 @@ config SPL_DM_REGULATOR_BD71837
 	This config enables implementation of driver-model regulator uclass
 	features for regulators on ROHM BD71837 and BD71847 in SPL.
 
+config DM_REGULATOR_PCA9450
+	bool "Enable Driver Model for NXP PCA9450 regulators"
+	depends on DM_REGULATOR && DM_PMIC_PCA9450
+	help
+	This config enables implementation of driver-model regulator uclass
+	features for regulators on NXP PCA9450 PMICs. PCA9450 contains 6 bucks
+	and 5 LDOS. The driver implements get/set api for value and enable.
+
+config SPL_DM_REGULATOR_PCA9450
+	bool "Enable Driver Model for NXP PCA9450 regulators in SPL"
+	depends on DM_REGULATOR_PCA9450
+	help
+	This config enables implementation of driver-model regulator uclass
+	features for regulators on ROHM PCA9450 in SPL.
+
 config DM_REGULATOR_DA9063
 	bool "Enable Driver Model for REGULATOR DA9063"
 	depends on DM_REGULATOR && DM_PMIC_DA9063
diff --git a/drivers/power/regulator/Makefile b/drivers/power/regulator/Makefile
index b9883df9281..bc736068bca 100644
--- a/drivers/power/regulator/Makefile
+++ b/drivers/power/regulator/Makefile
@@ -11,6 +11,7 @@ obj-$(CONFIG_$(SPL_)DM_REGULATOR_DA9063) += da9063.o
 obj-$(CONFIG_DM_REGULATOR_MAX77686) += max77686.o
 obj-$(CONFIG_$(SPL_)DM_PMIC_PFUZE100) += pfuze100.o
 obj-$(CONFIG_$(SPL_)DM_REGULATOR_BD71837) += bd71837.o
+obj-$(CONFIG_$(SPL_)DM_REGULATOR_PCA9450) += pca9450.o
 obj-$(CONFIG_$(SPL_)REGULATOR_PWM) += pwm_regulator.o
 obj-$(CONFIG_$(SPL_)DM_REGULATOR_FAN53555) += fan53555.o
 obj-$(CONFIG_$(SPL_)DM_REGULATOR_COMMON) += regulator_common.o
diff --git a/drivers/power/regulator/pca9450.c b/drivers/power/regulator/pca9450.c
new file mode 100644
index 00000000000..23badaa3326
--- /dev/null
+++ b/drivers/power/regulator/pca9450.c
@@ -0,0 +1,333 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * NXP PCA9450 regulator driver
+ * Copyright (C) 2022 Marek Vasut <marex@denx.de>
+ *
+ * Largely based on:
+ * ROHM BD71837 regulator driver
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <log.h>
+#include <linux/bitops.h>
+#include <power/pca9450.h>
+#include <power/pmic.h>
+#include <power/regulator.h>
+
+#define HW_STATE_CONTROL 0
+#define DEBUG
+
+/**
+ * struct pca9450_vrange - describe linear range of voltages
+ *
+ * @min_volt:	smallest voltage in range
+ * @step:	how much voltage changes at each selector step
+ * @min_sel:	smallest selector in the range
+ * @max_sel:	maximum selector in the range
+ */
+struct pca9450_vrange {
+	unsigned int	min_volt;
+	unsigned int	step;
+	u8		min_sel;
+	u8		max_sel;
+};
+
+/**
+ * struct pca9450_plat - describe regulator control registers
+ *
+ * @name:	name of the regulator. Used for matching the dt-entry
+ * @enable_reg:	register address used to enable/disable regulator
+ * @enablemask:	register mask used to enable/disable regulator
+ * @volt_reg:	register address used to configure regulator voltage
+ * @volt_mask:	register mask used to configure regulator voltage
+ * @ranges:	pointer to ranges of regulator voltages and matching register
+ *		values
+ * @numranges:	number of voltage ranges pointed by ranges
+ * @dvs:	whether the voltage can be changed when regulator is enabled
+ */
+struct pca9450_plat {
+	const char		*name;
+	u8			enable_reg;
+	u8			enablemask;
+	u8			volt_reg;
+	u8			volt_mask;
+	struct pca9450_vrange	*ranges;
+	unsigned int		numranges;
+	bool			dvs;
+};
+
+#define PCA_RANGE(_min, _vstep, _sel_low, _sel_hi) \
+{ \
+	.min_volt = (_min), .step = (_vstep), \
+	.min_sel = (_sel_low), .max_sel = (_sel_hi), \
+}
+
+#define PCA_DATA(_name, enreg, enmask, vreg, vmask, _range, _dvs) \
+{ \
+	.name = (_name), .enable_reg = (enreg), .enablemask = (enmask), \
+	.volt_reg = (vreg), .volt_mask = (vmask), .ranges = (_range), \
+	.numranges = ARRAY_SIZE(_range), .dvs = (_dvs), \
+}
+
+static struct pca9450_vrange pca9450_buck123_vranges[] = {
+	PCA_RANGE(600000, 12500, 0, 0x7f),
+};
+
+static struct pca9450_vrange pca9450_buck456_vranges[] = {
+	PCA_RANGE(600000, 25000, 0, 0x70),
+	PCA_RANGE(3400000, 0, 0x71, 0x7f),
+};
+
+static struct pca9450_vrange pca9450_ldo1_vranges[] = {
+	PCA_RANGE(1600000, 100000, 0x0, 0x3),
+	PCA_RANGE(3000000, 100000, 0x4, 0x7),
+};
+
+static struct pca9450_vrange pca9450_ldo2_vranges[] = {
+	PCA_RANGE(800000, 50000, 0x0, 0x7),
+};
+
+static struct pca9450_vrange pca9450_ldo34_vranges[] = {
+	PCA_RANGE(800000, 100000, 0x0, 0x19),
+	PCA_RANGE(3300000, 0, 0x1a, 0x1f),
+};
+
+static struct pca9450_vrange pca9450_ldo5_vranges[] = {
+	PCA_RANGE(1800000, 100000, 0x0, 0xf),
+};
+
+/*
+ * We use enable mask 'HW_STATE_CONTROL' to indicate that this regulator
+ * must not be enabled or disabled by SW. The typical use-case for PCA9450
+ * is powering NXP i.MX8. In this use-case we (for now) only allow control
+ * for BUCK4, BUCK5, BUCK6 which are not boot critical.
+ */
+static struct pca9450_plat pca9450_reg_data[] = {
+	/* Bucks 1-3 which support dynamic voltage scaling */
+	PCA_DATA("BUCK1", PCA9450_BUCK1CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK1OUT_DVS0, PCA9450_DVS_BUCK_RUN_MASK,
+		 pca9450_buck123_vranges, true),
+	PCA_DATA("BUCK2", PCA9450_BUCK2CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK2OUT_DVS0, PCA9450_DVS_BUCK_RUN_MASK,
+		 pca9450_buck123_vranges, true),
+	PCA_DATA("BUCK3", PCA9450_BUCK3CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK3OUT_DVS0, PCA9450_DVS_BUCK_RUN_MASK,
+		 pca9450_buck123_vranges, true),
+	/* Bucks 4-6 which do not support dynamic voltage scaling */
+	PCA_DATA("BUCK4", PCA9450_BUCK4CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK4OUT, PCA9450_DVS_BUCK_RUN_MASK,
+		 pca9450_buck456_vranges, false),
+	PCA_DATA("BUCK5", PCA9450_BUCK5CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK5OUT, PCA9450_DVS_BUCK_RUN_MASK,
+		 pca9450_buck456_vranges, false),
+	PCA_DATA("BUCK6", PCA9450_BUCK6CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK6OUT, PCA9450_DVS_BUCK_RUN_MASK,
+		 pca9450_buck456_vranges, false),
+	/* LDOs */
+	PCA_DATA("LDO1", PCA9450_LDO1CTRL, HW_STATE_CONTROL,
+		 PCA9450_LDO1CTRL, PCA9450_LDO12_MASK,
+		 pca9450_ldo1_vranges, false),
+	PCA_DATA("LDO2", PCA9450_LDO2CTRL, HW_STATE_CONTROL,
+		 PCA9450_LDO2CTRL, PCA9450_LDO12_MASK,
+		 pca9450_ldo2_vranges, false),
+	PCA_DATA("LDO3", PCA9450_LDO3CTRL, HW_STATE_CONTROL,
+		 PCA9450_LDO3CTRL, PCA9450_LDO34_MASK,
+		 pca9450_ldo34_vranges, false),
+	PCA_DATA("LDO4", PCA9450_LDO4CTRL, HW_STATE_CONTROL,
+		 PCA9450_LDO4CTRL, PCA9450_LDO34_MASK,
+		 pca9450_ldo34_vranges, false),
+	PCA_DATA("LDO5", PCA9450_LDO5CTRL_H, HW_STATE_CONTROL,
+		 PCA9450_LDO5CTRL_H, PCA9450_LDO5_MASK,
+		 pca9450_ldo5_vranges, false),
+};
+
+static int vrange_find_value(struct pca9450_vrange *r, unsigned int sel,
+			     unsigned int *val)
+{
+	if (!val || sel < r->min_sel || sel > r->max_sel)
+		return -EINVAL;
+
+	*val = r->min_volt + r->step * (sel - r->min_sel);
+	return 0;
+}
+
+static int vrange_find_selector(struct pca9450_vrange *r, int val,
+				unsigned int *sel)
+{
+	int ret = -EINVAL;
+	int num_vals = r->max_sel - r->min_sel + 1;
+
+	if (val >= r->min_volt &&
+	    val <= r->min_volt + r->step * (num_vals - 1)) {
+		if (r->step) {
+			*sel = r->min_sel + ((val - r->min_volt) / r->step);
+			ret = 0;
+		} else {
+			*sel = r->min_sel;
+			ret = 0;
+		}
+	}
+	return ret;
+}
+
+static int pca9450_get_enable(struct udevice *dev)
+{
+	struct pca9450_plat *plat = dev_get_plat(dev);
+	int val;
+
+	/*
+	 * boot critical regulators on pca9450 must not be controlled by sw
+	 * due to the 'feature' which leaves power rails down if pca9450 is
+	 * reseted to snvs state. hence we can't get the state here.
+	 *
+	 * if we are alive it means we probably are on run state and
+	 * if the regulator can't be controlled we can assume it is
+	 * enabled.
+	 */
+	if (plat->enablemask == HW_STATE_CONTROL)
+		return 1;
+
+	val = pmic_reg_read(dev->parent, plat->enable_reg);
+	if (val < 0)
+		return val;
+
+	return (val & plat->enablemask);
+}
+
+static int pca9450_set_enable(struct udevice *dev, bool enable)
+{
+	int val = 0;
+	struct pca9450_plat *plat = dev_get_plat(dev);
+
+	/*
+	 * boot critical regulators on pca9450 must not be controlled by sw
+	 * due to the 'feature' which leaves power rails down if pca9450 is
+	 * reseted to snvs state. Hence we can't set the state here.
+	 */
+	if (plat->enablemask == HW_STATE_CONTROL)
+		return enable ? 0 : -EINVAL;
+
+	if (enable)
+		val = plat->enablemask;
+
+	return pmic_clrsetbits(dev->parent, plat->enable_reg, plat->enablemask,
+			       val);
+}
+
+static int pca9450_get_value(struct udevice *dev)
+{
+	struct pca9450_plat *plat = dev_get_plat(dev);
+	unsigned int reg, tmp;
+	int i, ret;
+
+	ret = pmic_reg_read(dev->parent, plat->volt_reg);
+	if (ret < 0)
+		return ret;
+
+	reg = ret;
+	reg &= plat->volt_mask;
+
+	for (i = 0; i < plat->numranges; i++) {
+		struct pca9450_vrange *r = &plat->ranges[i];
+
+		if (!vrange_find_value(r, reg, &tmp))
+			return tmp;
+	}
+
+	pr_err("Unknown voltage value read from pmic\n");
+
+	return -EINVAL;
+}
+
+static int pca9450_set_value(struct udevice *dev, int uvolt)
+{
+	struct pca9450_plat *plat = dev_get_plat(dev);
+	unsigned int sel;
+	int i, found = 0;
+
+	/*
+	 * An under/overshooting may occur if voltage is changed for other
+	 * regulators but buck 1,2,3 or 4 when regulator is enabled. Prevent
+	 * change to protect the HW
+	 */
+	if (!plat->dvs)
+		if (pca9450_get_enable(dev)) {
+			/* If the value is already set, skip the warning. */
+			if (pca9450_get_value(dev) == uvolt)
+				return 0;
+			pr_err("Only DVS bucks can be changed when enabled\n");
+			return -EINVAL;
+		}
+
+	for (i = 0; i < plat->numranges; i++) {
+		struct pca9450_vrange *r = &plat->ranges[i];
+
+		found = !vrange_find_selector(r, uvolt, &sel);
+		if (found) {
+			unsigned int tmp;
+
+			/*
+			 * We require exactly the requested value to be
+			 * supported - this can be changed later if needed
+			 */
+			found = !vrange_find_value(r, sel, &tmp);
+			if (found && tmp == uvolt)
+				break;
+			found = 0;
+		}
+	}
+
+	if (!found)
+		return -EINVAL;
+
+	return pmic_clrsetbits(dev->parent, plat->volt_reg,
+			       plat->volt_mask, sel);
+}
+
+static int pca9450_regulator_probe(struct udevice *dev)
+{
+	struct pca9450_plat *plat = dev_get_plat(dev);
+	int i, type;
+
+	type = dev_get_driver_data(dev_get_parent(dev));
+
+	if (type != NXP_CHIP_TYPE_PCA9450A && type != NXP_CHIP_TYPE_PCA9450BC) {
+		debug("Unknown PMIC type\n");
+		return -EINVAL;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(pca9450_reg_data); i++) {
+		if (strcmp(dev->name, pca9450_reg_data[i].name))
+			continue;
+
+		/* PCA9450B/PCA9450C uses BUCK1 and BUCK3 in dual-phase */
+		if (type == NXP_CHIP_TYPE_PCA9450BC &&
+		    !strcmp(pca9450_reg_data[i].name, "BUCK3")) {
+			continue;
+		}
+
+		*plat = pca9450_reg_data[i];
+
+		return 0;
+	}
+
+	pr_err("Unknown regulator '%s'\n", dev->name);
+
+	return -ENOENT;
+}
+
+static const struct dm_regulator_ops pca9450_regulator_ops = {
+	.get_value	= pca9450_get_value,
+	.set_value	= pca9450_set_value,
+	.get_enable	= pca9450_get_enable,
+	.set_enable	= pca9450_set_enable,
+};
+
+U_BOOT_DRIVER(pca9450_regulator) = {
+	.name		= PCA9450_REGULATOR_DRIVER,
+	.id		= UCLASS_REGULATOR,
+	.ops		= &pca9450_regulator_ops,
+	.probe		= pca9450_regulator_probe,
+	.plat_auto	= sizeof(struct pca9450_plat),
+};
diff --git a/include/power/pca9450.h b/include/power/pca9450.h
index 27703bb1f91..fa0405fcb87 100644
--- a/include/power/pca9450.h
+++ b/include/power/pca9450.h
@@ -56,4 +56,15 @@ enum {
 
 int power_pca9450_init(unsigned char bus, unsigned char addr);
 
+enum {
+	NXP_CHIP_TYPE_PCA9450A = 0,
+	NXP_CHIP_TYPE_PCA9450BC,
+	NXP_CHIP_TYPE_AMOUNT
+};
+
+#define PCA9450_DVS_BUCK_RUN_MASK	0x7f
+#define PCA9450_LDO12_MASK		0x07
+#define PCA9450_LDO34_MASK		0x1f
+#define PCA9450_LDO5_MASK		0x0f
+
 #endif
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v3 1/2] pmic: pca9450: Add upstream regulators subnode match
  2022-05-20  3:10 [PATCH v3 1/2] pmic: pca9450: Add upstream regulators subnode match Marek Vasut
  2022-05-20  3:10 ` [PATCH v3 2/2] pmic: pca9450: Add regulator driver Marek Vasut
@ 2022-05-20 13:44 ` sbabic
  1 sibling, 0 replies; 9+ messages in thread
From: sbabic @ 2022-05-20 13:44 UTC (permalink / raw)
  To: Marek Vasut, u-boot

> The upstream DT regulators node subnodes are named BUCKn and LDOn,
> the downstream DT regulators node subnodes are named buckn and ldon,
> add the upstream match.
> Reviewed-by: Fabio Estevam <festevam@denx.de>
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Fabio Estevam <festevam@denx.de>
> Cc: Peng Fan <peng.fan@nxp.com>
> Cc: Stefano Babic <sbabic@denx.de>
Applied to u-boot-imx, master, thanks !

Best regards,
Stefano Babic

-- 
=====================================================================
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic@denx.de
=====================================================================

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v3 2/2] pmic: pca9450: Add regulator driver
  2022-05-20  3:10 ` [PATCH v3 2/2] pmic: pca9450: Add regulator driver Marek Vasut
@ 2022-05-20 13:44   ` sbabic
  0 siblings, 0 replies; 9+ messages in thread
From: sbabic @ 2022-05-20 13:44 UTC (permalink / raw)
  To: Marek Vasut, u-boot

> Add PCA9450 regulator driver. This is complementary driver for the BUCKn
> and LDOn regulators provided by the PCA9450 PMIC driver. Currently the
> driver permits reading the settngs and configuring the BUCKn and LDOn
> regulators.
> Reviewed-by: Fabio Estevam <festevam@denx.de>
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Fabio Estevam <festevam@denx.de>
> Cc: Peng Fan <peng.fan@nxp.com>
> Cc: Stefano Babic <sbabic@denx.de>
Applied to u-boot-imx, master, thanks !

Best regards,
Stefano Babic

-- 
=====================================================================
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic@denx.de
=====================================================================

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] pmic: pca9450: Add regulator driver
  2022-06-20  2:47       ` Marek Vasut
@ 2022-06-20  5:03         ` Vaittinen, Matti
  0 siblings, 0 replies; 9+ messages in thread
From: Vaittinen, Matti @ 2022-06-20  5:03 UTC (permalink / raw)
  To: Marek Vasut, Heiko Thiery
  Cc: u-boot, sbabic, Fabio Estevam, Peng Fan, Michael Nazzareno Trimarchi

On 6/20/22 05:47, Marek Vasut wrote:
> On 6/19/22 18:01, Heiko Thiery wrote:
> 
> [...]
> 
>>>> +static int pca9450_set_value(struct udevice *dev, int uvolt)
>>>> +{
>>>> +       struct pca9450_plat *plat = dev_get_plat(dev);
>>>> +       unsigned int sel;
>>>> +       int i, found = 0;
>>>> +
>>>> +       /*
>>>> +        * An under/overshooting may occur if voltage is changed for 
>>>> other
>>>> +        * regulators but buck 1,2,3 or 4 when regulator is enabled. 
>>>> Prevent
>>>> +        * change to protect the HW
>>>> +        */
>>>
>>> To be able to configure the pmic/regulators during startup by the dm
>>> (regulators_enable_boot_on) I think we have to
>>> remove the checks for the permit. Otherwise the automatic 
>>> configuration on hand
>>> of the dtb settings won't work.
>>>
>>
>> I tried to find the root of the comment above. Since the driver is
>> probably based
>> on the ROHM BD71837 and this comment can be found there as well, the
>> limitation probably comes from this type.
>>
>> Can this be?
> 
> Most likely this comment is bogus copy-paste from the ROHM PMIC driver.
> 
> [...]

I've authored this commit in the ROHM BD71837 driver. The ROHM PMIC do 
really have this limitation - only BUCKs 1,2,3 and 4 have been designed 
for changing the voltage when enabled.

I do not know about pca9450 - but I can assure that if copy-paste has 
happened the direction has indeed been ROHM driver => pca9450 :)

Best Regards
	-- Matti Vaittinen

-- 
The Linux Kernel guy at ROHM Semiconductors

Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~ this year is the year of a signature writers block ~~

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] pmic: pca9450: Add regulator driver
  2022-06-19 16:01     ` Heiko Thiery
@ 2022-06-20  2:47       ` Marek Vasut
  2022-06-20  5:03         ` Vaittinen, Matti
  0 siblings, 1 reply; 9+ messages in thread
From: Marek Vasut @ 2022-06-20  2:47 UTC (permalink / raw)
  To: Heiko Thiery
  Cc: u-boot, sbabic, Fabio Estevam, Peng Fan,
	Michael Nazzareno Trimarchi, Vaittinen, Matti

On 6/19/22 18:01, Heiko Thiery wrote:

[...]

>>> +static int pca9450_set_value(struct udevice *dev, int uvolt)
>>> +{
>>> +       struct pca9450_plat *plat = dev_get_plat(dev);
>>> +       unsigned int sel;
>>> +       int i, found = 0;
>>> +
>>> +       /*
>>> +        * An under/overshooting may occur if voltage is changed for other
>>> +        * regulators but buck 1,2,3 or 4 when regulator is enabled. Prevent
>>> +        * change to protect the HW
>>> +        */
>>
>> To be able to configure the pmic/regulators during startup by the dm
>> (regulators_enable_boot_on) I think we have to
>> remove the checks for the permit. Otherwise the automatic configuration on hand
>> of the dtb settings won't work.
>>
> 
> I tried to find the root of the comment above. Since the driver is
> probably based
> on the ROHM BD71837 and this comment can be found there as well, the
> limitation probably comes from this type.
> 
> Can this be?

Most likely this comment is bogus copy-paste from the ROHM PMIC driver.

[...]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] pmic: pca9450: Add regulator driver
  2022-06-19 15:16   ` Heiko Thiery
@ 2022-06-19 16:01     ` Heiko Thiery
  2022-06-20  2:47       ` Marek Vasut
  0 siblings, 1 reply; 9+ messages in thread
From: Heiko Thiery @ 2022-06-19 16:01 UTC (permalink / raw)
  To: Marek Vasut
  Cc: u-boot, sbabic, Fabio Estevam, Peng Fan, Michael Nazzareno Trimarchi

Am So., 19. Juni 2022 um 17:16 Uhr schrieb Heiko Thiery
<heiko.thiery@gmail.com>:
>
> Hi Marek,
>
>
> Am Mi., 13. Apr. 2022 um 00:40 Uhr schrieb Marek Vasut <marex@denx.de>:
> >
> > Add PCA9450 regulator driver. This is complementary driver for the BUCKn
> > and LDOn regulators provided by the PCA9450 PMIC driver. Currently the
> > driver permits reading the settngs and configuring the BUCKn and LDOn
> > regulators.
> >
> > Reviewed-by: Fabio Estevam <festevam@denx.de>
> > Signed-off-by: Marek Vasut <marex@denx.de>
> > Cc: Fabio Estevam <festevam@denx.de>
> > Cc: Peng Fan <peng.fan@nxp.com>
> > Cc: Stefano Babic <sbabic@denx.de>
> > ---
> > V2: Add RB by Fabio
> > V3: Rebase on u-boot-imx/master
> > ---
> >  drivers/power/pmic/pca9450.c      |   6 +-
> >  drivers/power/regulator/Kconfig   |  15 ++
> >  drivers/power/regulator/Makefile  |   1 +
> >  drivers/power/regulator/pca9450.c | 333 ++++++++++++++++++++++++++++++
> >  include/power/pca9450.h           |  11 +
> >  5 files changed, 363 insertions(+), 3 deletions(-)
> >  create mode 100644 drivers/power/regulator/pca9450.c
> >
> > diff --git a/drivers/power/pmic/pca9450.c b/drivers/power/pmic/pca9450.c
> > index 26c876c9c45..116ac49a8db 100644
> > --- a/drivers/power/pmic/pca9450.c
> > +++ b/drivers/power/pmic/pca9450.c
> > @@ -83,9 +83,9 @@ static struct dm_pmic_ops pca9450_ops = {
> >  };
> >
> >  static const struct udevice_id pca9450_ids[] = {
> > -       { .compatible = "nxp,pca9450a", .data = 0x25, },
> > -       { .compatible = "nxp,pca9450b", .data = 0x25, },
> > -       { .compatible = "nxp,pca9450c", .data = 0x25, },
> > +       { .compatible = "nxp,pca9450a", .data = NXP_CHIP_TYPE_PCA9450A, },
> > +       { .compatible = "nxp,pca9450b", .data = NXP_CHIP_TYPE_PCA9450BC, },
> > +       { .compatible = "nxp,pca9450c", .data = NXP_CHIP_TYPE_PCA9450BC, },
> >         { }
> >  };
> >
> > diff --git a/drivers/power/regulator/Kconfig b/drivers/power/regulator/Kconfig
> > index cd253b95f2f..d486bad6bdc 100644
> > --- a/drivers/power/regulator/Kconfig
> > +++ b/drivers/power/regulator/Kconfig
> > @@ -60,6 +60,21 @@ config SPL_DM_REGULATOR_BD71837
> >         This config enables implementation of driver-model regulator uclass
> >         features for regulators on ROHM BD71837 and BD71847 in SPL.
> >
> > +config DM_REGULATOR_PCA9450
> > +       bool "Enable Driver Model for NXP PCA9450 regulators"
> > +       depends on DM_REGULATOR && DM_PMIC_PCA9450
> > +       help
> > +       This config enables implementation of driver-model regulator uclass
> > +       features for regulators on NXP PCA9450 PMICs. PCA9450 contains 6 bucks
> > +       and 5 LDOS. The driver implements get/set api for value and enable.
> > +
> > +config SPL_DM_REGULATOR_PCA9450
> > +       bool "Enable Driver Model for NXP PCA9450 regulators in SPL"
> > +       depends on DM_REGULATOR_PCA9450
> > +       help
> > +       This config enables implementation of driver-model regulator uclass
> > +       features for regulators on ROHM PCA9450 in SPL.
> > +
> >  config DM_REGULATOR_DA9063
> >         bool "Enable Driver Model for REGULATOR DA9063"
> >         depends on DM_REGULATOR && DM_PMIC_DA9063
> > diff --git a/drivers/power/regulator/Makefile b/drivers/power/regulator/Makefile
> > index 4efb32a3228..d2d17f7aed0 100644
> > --- a/drivers/power/regulator/Makefile
> > +++ b/drivers/power/regulator/Makefile
> > @@ -11,6 +11,7 @@ obj-$(CONFIG_$(SPL_)DM_REGULATOR_DA9063) += da9063.o
> >  obj-$(CONFIG_DM_REGULATOR_MAX77686) += max77686.o
> >  obj-$(CONFIG_$(SPL_)DM_PMIC_PFUZE100) += pfuze100.o
> >  obj-$(CONFIG_$(SPL_)DM_REGULATOR_BD71837) += bd71837.o
> > +obj-$(CONFIG_$(SPL_)DM_REGULATOR_PCA9450) += pca9450.o
> >  obj-$(CONFIG_$(SPL_)REGULATOR_PWM) += pwm_regulator.o
> >  obj-$(CONFIG_$(SPL_)DM_REGULATOR_FAN53555) += fan53555.o
> >  obj-$(CONFIG_$(SPL_)DM_REGULATOR_COMMON) += regulator_common.o
> > diff --git a/drivers/power/regulator/pca9450.c b/drivers/power/regulator/pca9450.c
> > new file mode 100644
> > index 00000000000..4847c9f90f0
> > --- /dev/null
> > +++ b/drivers/power/regulator/pca9450.c
> > @@ -0,0 +1,333 @@
> > +// SPDX-License-Identifier: GPL-2.0-or-later
> > +/*
> > + * NXP PCA9450 regulator driver
> > + * Copyright (C) 2022 Marek Vasut <marex@denx.de>
> > + *
> > + * Largely based on:
> > + * ROHM BD71837 regulator driver
> > + */
> > +
> > +#include <common.h>
> > +#include <dm.h>
> > +#include <log.h>
> > +#include <linux/bitops.h>
> > +#include <power/pca9450.h>
> > +#include <power/pmic.h>
> > +#include <power/regulator.h>
> > +
> > +#define HW_STATE_CONTROL 0
> > +#define DEBUG
> > +
> > +/**
> > + * struct pca9450_vrange - describe linear range of voltages
> > + *
> > + * @min_volt:  smallest voltage in range
> > + * @step:      how much voltage changes at each selector step
> > + * @min_sel:   smallest selector in the range
> > + * @max_sel:   maximum selector in the range
> > + */
> > +struct pca9450_vrange {
> > +       unsigned int    min_volt;
> > +       unsigned int    step;
> > +       u8              min_sel;
> > +       u8              max_sel;
> > +};
> > +
> > +/**
> > + * struct pca9450_plat - describe regulator control registers
> > + *
> > + * @name:      name of the regulator. Used for matching the dt-entry
> > + * @enable_reg:        register address used to enable/disable regulator
> > + * @enablemask:        register mask used to enable/disable regulator
> > + * @volt_reg:  register address used to configure regulator voltage
> > + * @volt_mask: register mask used to configure regulator voltage
> > + * @ranges:    pointer to ranges of regulator voltages and matching register
> > + *             values
> > + * @numranges: number of voltage ranges pointed by ranges
> > + * @dvs:       whether the voltage can be changed when regulator is enabled
> > + */
> > +struct pca9450_plat {
> > +       const char              *name;
> > +       u8                      enable_reg;
> > +       u8                      enablemask;
> > +       u8                      volt_reg;
> > +       u8                      volt_mask;
> > +       struct pca9450_vrange   *ranges;
> > +       unsigned int            numranges;
> > +       bool                    dvs;
> > +};
> > +
> > +#define PCA_RANGE(_min, _vstep, _sel_low, _sel_hi) \
> > +{ \
> > +       .min_volt = (_min), .step = (_vstep), \
> > +       .min_sel = (_sel_low), .max_sel = (_sel_hi), \
> > +}
> > +
> > +#define PCA_DATA(_name, enreg, enmask, vreg, vmask, _range, _dvs) \
> > +{ \
> > +       .name = (_name), .enable_reg = (enreg), .enablemask = (enmask), \
> > +       .volt_reg = (vreg), .volt_mask = (vmask), .ranges = (_range), \
> > +       .numranges = ARRAY_SIZE(_range), .dvs = (_dvs), \
> > +}
> > +
> > +static struct pca9450_vrange pca9450_buck123_vranges[] = {
> > +       PCA_RANGE(600000, 12500, 0, 0x7f),
> > +};
> > +
> > +static struct pca9450_vrange pca9450_buck456_vranges[] = {
> > +       PCA_RANGE(600000, 25000, 0, 0x70),
> > +       PCA_RANGE(3400000, 0, 0x71, 0x7f),
> > +};
> > +
> > +static struct pca9450_vrange pca9450_ldo1_vranges[] = {
> > +       PCA_RANGE(1600000, 100000, 0x0, 0x3),
> > +       PCA_RANGE(3000000, 100000, 0x4, 0x7),
> > +};
> > +
> > +static struct pca9450_vrange pca9450_ldo2_vranges[] = {
> > +       PCA_RANGE(800000, 50000, 0x0, 0x7),
> > +};
> > +
> > +static struct pca9450_vrange pca9450_ldo34_vranges[] = {
> > +       PCA_RANGE(800000, 100000, 0x0, 0x19),
> > +       PCA_RANGE(3300000, 0, 0x1a, 0x1f),
> > +};
> > +
> > +static struct pca9450_vrange pca9450_ldo5_vranges[] = {
> > +       PCA_RANGE(1800000, 100000, 0x0, 0xf),
> > +};
> > +
> > +/*
> > + * We use enable mask 'HW_STATE_CONTROL' to indicate that this regulator
> > + * must not be enabled or disabled by SW. The typical use-case for PCA9450
> > + * is powering NXP i.MX8. In this use-case we (for now) only allow control
> > + * for BUCK4, BUCK5, BUCK6 which are not boot critical.
> > + */
> > +static struct pca9450_plat pca9450_reg_data[] = {
> > +       /* Bucks 1-3 which support dynamic voltage scaling */
> > +       PCA_DATA("BUCK1", PCA9450_BUCK1CTRL, HW_STATE_CONTROL,
> > +                PCA9450_BUCK1OUT_DVS0, DVS_BUCK_RUN_MASK,
> > +                pca9450_buck123_vranges, true),
> > +       PCA_DATA("BUCK2", PCA9450_BUCK2CTRL, HW_STATE_CONTROL,
> > +                PCA9450_BUCK2OUT_DVS0, DVS_BUCK_RUN_MASK,
> > +                pca9450_buck123_vranges, true),
> > +       PCA_DATA("BUCK3", PCA9450_BUCK3CTRL, HW_STATE_CONTROL,
> > +                PCA9450_BUCK3OUT_DVS0, DVS_BUCK_RUN_MASK,
> > +                pca9450_buck123_vranges, true),
> > +       /* Bucks 4-6 which do not support dynamic voltage scaling */
> > +       PCA_DATA("BUCK4", PCA9450_BUCK4CTRL, HW_STATE_CONTROL,
> > +                PCA9450_BUCK4OUT, DVS_BUCK_RUN_MASK,
> > +                pca9450_buck456_vranges, false),
> > +       PCA_DATA("BUCK5", PCA9450_BUCK5CTRL, HW_STATE_CONTROL,
> > +                PCA9450_BUCK5OUT, DVS_BUCK_RUN_MASK,
> > +                pca9450_buck456_vranges, false),
> > +       PCA_DATA("BUCK6", PCA9450_BUCK6CTRL, HW_STATE_CONTROL,
> > +                PCA9450_BUCK6OUT, DVS_BUCK_RUN_MASK,
> > +                pca9450_buck456_vranges, false),
> > +       /* LDOs */
> > +       PCA_DATA("LDO1", PCA9450_LDO1CTRL, HW_STATE_CONTROL,
> > +                PCA9450_LDO1CTRL, PCA9450_LDO12_MASK,
> > +                pca9450_ldo1_vranges, false),
> > +       PCA_DATA("LDO2", PCA9450_LDO2CTRL, HW_STATE_CONTROL,
> > +                PCA9450_LDO2CTRL, PCA9450_LDO12_MASK,
> > +                pca9450_ldo2_vranges, false),
> > +       PCA_DATA("LDO3", PCA9450_LDO3CTRL, HW_STATE_CONTROL,
> > +                PCA9450_LDO3CTRL, PCA9450_LDO34_MASK,
> > +                pca9450_ldo34_vranges, false),
> > +       PCA_DATA("LDO4", PCA9450_LDO4CTRL, HW_STATE_CONTROL,
> > +                PCA9450_LDO4CTRL, PCA9450_LDO34_MASK,
> > +                pca9450_ldo34_vranges, false),
> > +       PCA_DATA("LDO5", PCA9450_LDO5CTRL_H, HW_STATE_CONTROL,
> > +                PCA9450_LDO5CTRL_H, PCA9450_LDO5_MASK,
> > +                pca9450_ldo5_vranges, false),
> > +};
> > +
> > +static int vrange_find_value(struct pca9450_vrange *r, unsigned int sel,
> > +                            unsigned int *val)
> > +{
> > +       if (!val || sel < r->min_sel || sel > r->max_sel)
> > +               return -EINVAL;
> > +
> > +       *val = r->min_volt + r->step * (sel - r->min_sel);
> > +       return 0;
> > +}
> > +
> > +static int vrange_find_selector(struct pca9450_vrange *r, int val,
> > +                               unsigned int *sel)
> > +{
> > +       int ret = -EINVAL;
> > +       int num_vals = r->max_sel - r->min_sel + 1;
> > +
> > +       if (val >= r->min_volt &&
> > +           val <= r->min_volt + r->step * (num_vals - 1)) {
> > +               if (r->step) {
> > +                       *sel = r->min_sel + ((val - r->min_volt) / r->step);
> > +                       ret = 0;
> > +               } else {
> > +                       *sel = r->min_sel;
> > +                       ret = 0;
> > +               }
> > +       }
> > +       return ret;
> > +}
> > +
> > +static int pca9450_get_enable(struct udevice *dev)
> > +{
> > +       struct pca9450_plat *plat = dev_get_plat(dev);
> > +       int val;
> > +
> > +       /*
> > +        * boot critical regulators on pca9450 must not be controlled by sw
> > +        * due to the 'feature' which leaves power rails down if pca9450 is
> > +        * reseted to snvs state. hence we can't get the state here.
> > +        *
> > +        * if we are alive it means we probably are on run state and
> > +        * if the regulator can't be controlled we can assume it is
> > +        * enabled.
> > +        */
> > +       if (plat->enablemask == HW_STATE_CONTROL)
> > +               return 1;
> > +
> > +       val = pmic_reg_read(dev->parent, plat->enable_reg);
> > +       if (val < 0)
> > +               return val;
> > +
> > +       return (val & plat->enablemask);
> > +}
> > +
> > +static int pca9450_set_enable(struct udevice *dev, bool enable)
> > +{
> > +       int val = 0;
> > +       struct pca9450_plat *plat = dev_get_plat(dev);
> > +
> > +       /*
> > +        * boot critical regulators on pca9450 must not be controlled by sw
> > +        * due to the 'feature' which leaves power rails down if pca9450 is
> > +        * reseted to snvs state. Hence we can't set the state here.
> > +        */
> > +       if (plat->enablemask == HW_STATE_CONTROL)
> > +               return enable ? 0 : -EINVAL;
> > +
> > +       if (enable)
> > +               val = plat->enablemask;
> > +
> > +       return pmic_clrsetbits(dev->parent, plat->enable_reg, plat->enablemask,
> > +                              val);
> > +}
> > +
> > +static int pca9450_get_value(struct udevice *dev)
> > +{
> > +       struct pca9450_plat *plat = dev_get_plat(dev);
> > +       unsigned int reg, tmp;
> > +       int i, ret;
> > +
> > +       ret = pmic_reg_read(dev->parent, plat->volt_reg);
> > +       if (ret < 0)
> > +               return ret;
> > +
> > +       reg = ret;
> > +       reg &= plat->volt_mask;
> > +
> > +       for (i = 0; i < plat->numranges; i++) {
> > +               struct pca9450_vrange *r = &plat->ranges[i];
> > +
> > +               if (!vrange_find_value(r, reg, &tmp))
> > +                       return tmp;
> > +       }
> > +
> > +       pr_err("Unknown voltage value read from pmic\n");
> > +
> > +       return -EINVAL;
> > +}
> > +
> > +static int pca9450_set_value(struct udevice *dev, int uvolt)
> > +{
> > +       struct pca9450_plat *plat = dev_get_plat(dev);
> > +       unsigned int sel;
> > +       int i, found = 0;
> > +
> > +       /*
> > +        * An under/overshooting may occur if voltage is changed for other
> > +        * regulators but buck 1,2,3 or 4 when regulator is enabled. Prevent
> > +        * change to protect the HW
> > +        */
>
> To be able to configure the pmic/regulators during startup by the dm
> (regulators_enable_boot_on) I think we have to
> remove the checks for the permit. Otherwise the automatic configuration on hand
> of the dtb settings won't work.
>

I tried to find the root of the comment above. Since the driver is
probably based
on the ROHM BD71837 and this comment can be found there as well, the
limitation probably comes from this type.

Can this be?

>
> > +       if (!plat->dvs)
> > +               if (pca9450_get_enable(dev)) {
> > +                       /* If the value is already set, skip the warning. */
> > +                       if (pca9450_get_value(dev) == uvolt)
> > +                               return 0;
> > +                       pr_err("Only DVS bucks can be changed when enabled\n");
> > +                       return -EINVAL;
> > +               }
> > +
> > +       for (i = 0; i < plat->numranges; i++) {
> > +               struct pca9450_vrange *r = &plat->ranges[i];
> > +
> > +               found = !vrange_find_selector(r, uvolt, &sel);
> > +               if (found) {
> > +                       unsigned int tmp;
> > +
> > +                       /*
> > +                        * We require exactly the requested value to be
> > +                        * supported - this can be changed later if needed
> > +                        */
> > +                       found = !vrange_find_value(r, sel, &tmp);
> > +                       if (found && tmp == uvolt)
> > +                               break;
> > +                       found = 0;
> > +               }
> > +       }
> > +
> > +       if (!found)
> > +               return -EINVAL;
> > +
> > +       return pmic_clrsetbits(dev->parent, plat->volt_reg,
> > +                              plat->volt_mask, sel);
> > +}
> > +
> > +static int pca9450_regulator_probe(struct udevice *dev)
> > +{
> > +       struct pca9450_plat *plat = dev_get_plat(dev);
> > +       int i, type;
> > +
> > +       type = dev_get_driver_data(dev_get_parent(dev));
> > +
> > +       if (type != NXP_CHIP_TYPE_PCA9450A && type != NXP_CHIP_TYPE_PCA9450BC) {
> > +               debug("Unknown PMIC type\n");
> > +               return -EINVAL;
> > +       }
> > +
> > +       for (i = 0; i < ARRAY_SIZE(pca9450_reg_data); i++) {
> > +               if (strcmp(dev->name, pca9450_reg_data[i].name))
> > +                       continue;
> > +
> > +               /* PCA9450B/PCA9450C uses BUCK1 and BUCK3 in dual-phase */
> > +               if (type == NXP_CHIP_TYPE_PCA9450BC &&
> > +                   !strcmp(pca9450_reg_data[i].name, "BUCK3")) {
> > +                       continue;
> > +               }
> > +
> > +               *plat = pca9450_reg_data[i];
> > +
> > +               return 0;
> > +       }
> > +
> > +       pr_err("Unknown regulator '%s'\n", dev->name);
> > +
> > +       return -ENOENT;
> > +}
> > +
> > +static const struct dm_regulator_ops pca9450_regulator_ops = {
> > +       .get_value      = pca9450_get_value,
> > +       .set_value      = pca9450_set_value,
> > +       .get_enable     = pca9450_get_enable,
> > +       .set_enable     = pca9450_set_enable,
> > +};
> > +
> > +U_BOOT_DRIVER(pca9450_regulator) = {
> > +       .name           = PCA9450_REGULATOR_DRIVER,
> > +       .id             = UCLASS_REGULATOR,
> > +       .ops            = &pca9450_regulator_ops,
> > +       .probe          = pca9450_regulator_probe,
> > +       .plat_auto      = sizeof(struct pca9450_plat),
> > +};
> > diff --git a/include/power/pca9450.h b/include/power/pca9450.h
> > index 27703bb1f91..b714fc3477d 100644
> > --- a/include/power/pca9450.h
> > +++ b/include/power/pca9450.h
> > @@ -56,4 +56,15 @@ enum {
> >
> >  int power_pca9450_init(unsigned char bus, unsigned char addr);
> >
> > +enum {
> > +       NXP_CHIP_TYPE_PCA9450A = 0,
> > +       NXP_CHIP_TYPE_PCA9450BC,
> > +       NXP_CHIP_TYPE_AMOUNT
> > +};
> > +
> > +#define DVS_BUCK_RUN_MASK              0x7f
> > +#define PCA9450_LDO12_MASK             0x07
> > +#define PCA9450_LDO34_MASK             0x1f
> > +#define PCA9450_LDO5_MASK              0x0f
> > +
> >  #endif
> > --
> > 2.35.1
> >
>
> --
> Heiko

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] pmic: pca9450: Add regulator driver
  2022-04-12 22:39 ` [PATCH v3 2/2] pmic: pca9450: Add regulator driver Marek Vasut
@ 2022-06-19 15:16   ` Heiko Thiery
  2022-06-19 16:01     ` Heiko Thiery
  0 siblings, 1 reply; 9+ messages in thread
From: Heiko Thiery @ 2022-06-19 15:16 UTC (permalink / raw)
  To: Marek Vasut
  Cc: u-boot, sbabic, Fabio Estevam, Peng Fan, Michael Nazzareno Trimarchi

Hi Marek,


Am Mi., 13. Apr. 2022 um 00:40 Uhr schrieb Marek Vasut <marex@denx.de>:
>
> Add PCA9450 regulator driver. This is complementary driver for the BUCKn
> and LDOn regulators provided by the PCA9450 PMIC driver. Currently the
> driver permits reading the settngs and configuring the BUCKn and LDOn
> regulators.
>
> Reviewed-by: Fabio Estevam <festevam@denx.de>
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Fabio Estevam <festevam@denx.de>
> Cc: Peng Fan <peng.fan@nxp.com>
> Cc: Stefano Babic <sbabic@denx.de>
> ---
> V2: Add RB by Fabio
> V3: Rebase on u-boot-imx/master
> ---
>  drivers/power/pmic/pca9450.c      |   6 +-
>  drivers/power/regulator/Kconfig   |  15 ++
>  drivers/power/regulator/Makefile  |   1 +
>  drivers/power/regulator/pca9450.c | 333 ++++++++++++++++++++++++++++++
>  include/power/pca9450.h           |  11 +
>  5 files changed, 363 insertions(+), 3 deletions(-)
>  create mode 100644 drivers/power/regulator/pca9450.c
>
> diff --git a/drivers/power/pmic/pca9450.c b/drivers/power/pmic/pca9450.c
> index 26c876c9c45..116ac49a8db 100644
> --- a/drivers/power/pmic/pca9450.c
> +++ b/drivers/power/pmic/pca9450.c
> @@ -83,9 +83,9 @@ static struct dm_pmic_ops pca9450_ops = {
>  };
>
>  static const struct udevice_id pca9450_ids[] = {
> -       { .compatible = "nxp,pca9450a", .data = 0x25, },
> -       { .compatible = "nxp,pca9450b", .data = 0x25, },
> -       { .compatible = "nxp,pca9450c", .data = 0x25, },
> +       { .compatible = "nxp,pca9450a", .data = NXP_CHIP_TYPE_PCA9450A, },
> +       { .compatible = "nxp,pca9450b", .data = NXP_CHIP_TYPE_PCA9450BC, },
> +       { .compatible = "nxp,pca9450c", .data = NXP_CHIP_TYPE_PCA9450BC, },
>         { }
>  };
>
> diff --git a/drivers/power/regulator/Kconfig b/drivers/power/regulator/Kconfig
> index cd253b95f2f..d486bad6bdc 100644
> --- a/drivers/power/regulator/Kconfig
> +++ b/drivers/power/regulator/Kconfig
> @@ -60,6 +60,21 @@ config SPL_DM_REGULATOR_BD71837
>         This config enables implementation of driver-model regulator uclass
>         features for regulators on ROHM BD71837 and BD71847 in SPL.
>
> +config DM_REGULATOR_PCA9450
> +       bool "Enable Driver Model for NXP PCA9450 regulators"
> +       depends on DM_REGULATOR && DM_PMIC_PCA9450
> +       help
> +       This config enables implementation of driver-model regulator uclass
> +       features for regulators on NXP PCA9450 PMICs. PCA9450 contains 6 bucks
> +       and 5 LDOS. The driver implements get/set api for value and enable.
> +
> +config SPL_DM_REGULATOR_PCA9450
> +       bool "Enable Driver Model for NXP PCA9450 regulators in SPL"
> +       depends on DM_REGULATOR_PCA9450
> +       help
> +       This config enables implementation of driver-model regulator uclass
> +       features for regulators on ROHM PCA9450 in SPL.
> +
>  config DM_REGULATOR_DA9063
>         bool "Enable Driver Model for REGULATOR DA9063"
>         depends on DM_REGULATOR && DM_PMIC_DA9063
> diff --git a/drivers/power/regulator/Makefile b/drivers/power/regulator/Makefile
> index 4efb32a3228..d2d17f7aed0 100644
> --- a/drivers/power/regulator/Makefile
> +++ b/drivers/power/regulator/Makefile
> @@ -11,6 +11,7 @@ obj-$(CONFIG_$(SPL_)DM_REGULATOR_DA9063) += da9063.o
>  obj-$(CONFIG_DM_REGULATOR_MAX77686) += max77686.o
>  obj-$(CONFIG_$(SPL_)DM_PMIC_PFUZE100) += pfuze100.o
>  obj-$(CONFIG_$(SPL_)DM_REGULATOR_BD71837) += bd71837.o
> +obj-$(CONFIG_$(SPL_)DM_REGULATOR_PCA9450) += pca9450.o
>  obj-$(CONFIG_$(SPL_)REGULATOR_PWM) += pwm_regulator.o
>  obj-$(CONFIG_$(SPL_)DM_REGULATOR_FAN53555) += fan53555.o
>  obj-$(CONFIG_$(SPL_)DM_REGULATOR_COMMON) += regulator_common.o
> diff --git a/drivers/power/regulator/pca9450.c b/drivers/power/regulator/pca9450.c
> new file mode 100644
> index 00000000000..4847c9f90f0
> --- /dev/null
> +++ b/drivers/power/regulator/pca9450.c
> @@ -0,0 +1,333 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * NXP PCA9450 regulator driver
> + * Copyright (C) 2022 Marek Vasut <marex@denx.de>
> + *
> + * Largely based on:
> + * ROHM BD71837 regulator driver
> + */
> +
> +#include <common.h>
> +#include <dm.h>
> +#include <log.h>
> +#include <linux/bitops.h>
> +#include <power/pca9450.h>
> +#include <power/pmic.h>
> +#include <power/regulator.h>
> +
> +#define HW_STATE_CONTROL 0
> +#define DEBUG
> +
> +/**
> + * struct pca9450_vrange - describe linear range of voltages
> + *
> + * @min_volt:  smallest voltage in range
> + * @step:      how much voltage changes at each selector step
> + * @min_sel:   smallest selector in the range
> + * @max_sel:   maximum selector in the range
> + */
> +struct pca9450_vrange {
> +       unsigned int    min_volt;
> +       unsigned int    step;
> +       u8              min_sel;
> +       u8              max_sel;
> +};
> +
> +/**
> + * struct pca9450_plat - describe regulator control registers
> + *
> + * @name:      name of the regulator. Used for matching the dt-entry
> + * @enable_reg:        register address used to enable/disable regulator
> + * @enablemask:        register mask used to enable/disable regulator
> + * @volt_reg:  register address used to configure regulator voltage
> + * @volt_mask: register mask used to configure regulator voltage
> + * @ranges:    pointer to ranges of regulator voltages and matching register
> + *             values
> + * @numranges: number of voltage ranges pointed by ranges
> + * @dvs:       whether the voltage can be changed when regulator is enabled
> + */
> +struct pca9450_plat {
> +       const char              *name;
> +       u8                      enable_reg;
> +       u8                      enablemask;
> +       u8                      volt_reg;
> +       u8                      volt_mask;
> +       struct pca9450_vrange   *ranges;
> +       unsigned int            numranges;
> +       bool                    dvs;
> +};
> +
> +#define PCA_RANGE(_min, _vstep, _sel_low, _sel_hi) \
> +{ \
> +       .min_volt = (_min), .step = (_vstep), \
> +       .min_sel = (_sel_low), .max_sel = (_sel_hi), \
> +}
> +
> +#define PCA_DATA(_name, enreg, enmask, vreg, vmask, _range, _dvs) \
> +{ \
> +       .name = (_name), .enable_reg = (enreg), .enablemask = (enmask), \
> +       .volt_reg = (vreg), .volt_mask = (vmask), .ranges = (_range), \
> +       .numranges = ARRAY_SIZE(_range), .dvs = (_dvs), \
> +}
> +
> +static struct pca9450_vrange pca9450_buck123_vranges[] = {
> +       PCA_RANGE(600000, 12500, 0, 0x7f),
> +};
> +
> +static struct pca9450_vrange pca9450_buck456_vranges[] = {
> +       PCA_RANGE(600000, 25000, 0, 0x70),
> +       PCA_RANGE(3400000, 0, 0x71, 0x7f),
> +};
> +
> +static struct pca9450_vrange pca9450_ldo1_vranges[] = {
> +       PCA_RANGE(1600000, 100000, 0x0, 0x3),
> +       PCA_RANGE(3000000, 100000, 0x4, 0x7),
> +};
> +
> +static struct pca9450_vrange pca9450_ldo2_vranges[] = {
> +       PCA_RANGE(800000, 50000, 0x0, 0x7),
> +};
> +
> +static struct pca9450_vrange pca9450_ldo34_vranges[] = {
> +       PCA_RANGE(800000, 100000, 0x0, 0x19),
> +       PCA_RANGE(3300000, 0, 0x1a, 0x1f),
> +};
> +
> +static struct pca9450_vrange pca9450_ldo5_vranges[] = {
> +       PCA_RANGE(1800000, 100000, 0x0, 0xf),
> +};
> +
> +/*
> + * We use enable mask 'HW_STATE_CONTROL' to indicate that this regulator
> + * must not be enabled or disabled by SW. The typical use-case for PCA9450
> + * is powering NXP i.MX8. In this use-case we (for now) only allow control
> + * for BUCK4, BUCK5, BUCK6 which are not boot critical.
> + */
> +static struct pca9450_plat pca9450_reg_data[] = {
> +       /* Bucks 1-3 which support dynamic voltage scaling */
> +       PCA_DATA("BUCK1", PCA9450_BUCK1CTRL, HW_STATE_CONTROL,
> +                PCA9450_BUCK1OUT_DVS0, DVS_BUCK_RUN_MASK,
> +                pca9450_buck123_vranges, true),
> +       PCA_DATA("BUCK2", PCA9450_BUCK2CTRL, HW_STATE_CONTROL,
> +                PCA9450_BUCK2OUT_DVS0, DVS_BUCK_RUN_MASK,
> +                pca9450_buck123_vranges, true),
> +       PCA_DATA("BUCK3", PCA9450_BUCK3CTRL, HW_STATE_CONTROL,
> +                PCA9450_BUCK3OUT_DVS0, DVS_BUCK_RUN_MASK,
> +                pca9450_buck123_vranges, true),
> +       /* Bucks 4-6 which do not support dynamic voltage scaling */
> +       PCA_DATA("BUCK4", PCA9450_BUCK4CTRL, HW_STATE_CONTROL,
> +                PCA9450_BUCK4OUT, DVS_BUCK_RUN_MASK,
> +                pca9450_buck456_vranges, false),
> +       PCA_DATA("BUCK5", PCA9450_BUCK5CTRL, HW_STATE_CONTROL,
> +                PCA9450_BUCK5OUT, DVS_BUCK_RUN_MASK,
> +                pca9450_buck456_vranges, false),
> +       PCA_DATA("BUCK6", PCA9450_BUCK6CTRL, HW_STATE_CONTROL,
> +                PCA9450_BUCK6OUT, DVS_BUCK_RUN_MASK,
> +                pca9450_buck456_vranges, false),
> +       /* LDOs */
> +       PCA_DATA("LDO1", PCA9450_LDO1CTRL, HW_STATE_CONTROL,
> +                PCA9450_LDO1CTRL, PCA9450_LDO12_MASK,
> +                pca9450_ldo1_vranges, false),
> +       PCA_DATA("LDO2", PCA9450_LDO2CTRL, HW_STATE_CONTROL,
> +                PCA9450_LDO2CTRL, PCA9450_LDO12_MASK,
> +                pca9450_ldo2_vranges, false),
> +       PCA_DATA("LDO3", PCA9450_LDO3CTRL, HW_STATE_CONTROL,
> +                PCA9450_LDO3CTRL, PCA9450_LDO34_MASK,
> +                pca9450_ldo34_vranges, false),
> +       PCA_DATA("LDO4", PCA9450_LDO4CTRL, HW_STATE_CONTROL,
> +                PCA9450_LDO4CTRL, PCA9450_LDO34_MASK,
> +                pca9450_ldo34_vranges, false),
> +       PCA_DATA("LDO5", PCA9450_LDO5CTRL_H, HW_STATE_CONTROL,
> +                PCA9450_LDO5CTRL_H, PCA9450_LDO5_MASK,
> +                pca9450_ldo5_vranges, false),
> +};
> +
> +static int vrange_find_value(struct pca9450_vrange *r, unsigned int sel,
> +                            unsigned int *val)
> +{
> +       if (!val || sel < r->min_sel || sel > r->max_sel)
> +               return -EINVAL;
> +
> +       *val = r->min_volt + r->step * (sel - r->min_sel);
> +       return 0;
> +}
> +
> +static int vrange_find_selector(struct pca9450_vrange *r, int val,
> +                               unsigned int *sel)
> +{
> +       int ret = -EINVAL;
> +       int num_vals = r->max_sel - r->min_sel + 1;
> +
> +       if (val >= r->min_volt &&
> +           val <= r->min_volt + r->step * (num_vals - 1)) {
> +               if (r->step) {
> +                       *sel = r->min_sel + ((val - r->min_volt) / r->step);
> +                       ret = 0;
> +               } else {
> +                       *sel = r->min_sel;
> +                       ret = 0;
> +               }
> +       }
> +       return ret;
> +}
> +
> +static int pca9450_get_enable(struct udevice *dev)
> +{
> +       struct pca9450_plat *plat = dev_get_plat(dev);
> +       int val;
> +
> +       /*
> +        * boot critical regulators on pca9450 must not be controlled by sw
> +        * due to the 'feature' which leaves power rails down if pca9450 is
> +        * reseted to snvs state. hence we can't get the state here.
> +        *
> +        * if we are alive it means we probably are on run state and
> +        * if the regulator can't be controlled we can assume it is
> +        * enabled.
> +        */
> +       if (plat->enablemask == HW_STATE_CONTROL)
> +               return 1;
> +
> +       val = pmic_reg_read(dev->parent, plat->enable_reg);
> +       if (val < 0)
> +               return val;
> +
> +       return (val & plat->enablemask);
> +}
> +
> +static int pca9450_set_enable(struct udevice *dev, bool enable)
> +{
> +       int val = 0;
> +       struct pca9450_plat *plat = dev_get_plat(dev);
> +
> +       /*
> +        * boot critical regulators on pca9450 must not be controlled by sw
> +        * due to the 'feature' which leaves power rails down if pca9450 is
> +        * reseted to snvs state. Hence we can't set the state here.
> +        */
> +       if (plat->enablemask == HW_STATE_CONTROL)
> +               return enable ? 0 : -EINVAL;
> +
> +       if (enable)
> +               val = plat->enablemask;
> +
> +       return pmic_clrsetbits(dev->parent, plat->enable_reg, plat->enablemask,
> +                              val);
> +}
> +
> +static int pca9450_get_value(struct udevice *dev)
> +{
> +       struct pca9450_plat *plat = dev_get_plat(dev);
> +       unsigned int reg, tmp;
> +       int i, ret;
> +
> +       ret = pmic_reg_read(dev->parent, plat->volt_reg);
> +       if (ret < 0)
> +               return ret;
> +
> +       reg = ret;
> +       reg &= plat->volt_mask;
> +
> +       for (i = 0; i < plat->numranges; i++) {
> +               struct pca9450_vrange *r = &plat->ranges[i];
> +
> +               if (!vrange_find_value(r, reg, &tmp))
> +                       return tmp;
> +       }
> +
> +       pr_err("Unknown voltage value read from pmic\n");
> +
> +       return -EINVAL;
> +}
> +
> +static int pca9450_set_value(struct udevice *dev, int uvolt)
> +{
> +       struct pca9450_plat *plat = dev_get_plat(dev);
> +       unsigned int sel;
> +       int i, found = 0;
> +
> +       /*
> +        * An under/overshooting may occur if voltage is changed for other
> +        * regulators but buck 1,2,3 or 4 when regulator is enabled. Prevent
> +        * change to protect the HW
> +        */

To be able to configure the pmic/regulators during startup by the dm
(regulators_enable_boot_on) I think we have to
remove the checks for the permit. Otherwise the automatic configuration on hand
of the dtb settings won't work.


> +       if (!plat->dvs)
> +               if (pca9450_get_enable(dev)) {
> +                       /* If the value is already set, skip the warning. */
> +                       if (pca9450_get_value(dev) == uvolt)
> +                               return 0;
> +                       pr_err("Only DVS bucks can be changed when enabled\n");
> +                       return -EINVAL;
> +               }
> +
> +       for (i = 0; i < plat->numranges; i++) {
> +               struct pca9450_vrange *r = &plat->ranges[i];
> +
> +               found = !vrange_find_selector(r, uvolt, &sel);
> +               if (found) {
> +                       unsigned int tmp;
> +
> +                       /*
> +                        * We require exactly the requested value to be
> +                        * supported - this can be changed later if needed
> +                        */
> +                       found = !vrange_find_value(r, sel, &tmp);
> +                       if (found && tmp == uvolt)
> +                               break;
> +                       found = 0;
> +               }
> +       }
> +
> +       if (!found)
> +               return -EINVAL;
> +
> +       return pmic_clrsetbits(dev->parent, plat->volt_reg,
> +                              plat->volt_mask, sel);
> +}
> +
> +static int pca9450_regulator_probe(struct udevice *dev)
> +{
> +       struct pca9450_plat *plat = dev_get_plat(dev);
> +       int i, type;
> +
> +       type = dev_get_driver_data(dev_get_parent(dev));
> +
> +       if (type != NXP_CHIP_TYPE_PCA9450A && type != NXP_CHIP_TYPE_PCA9450BC) {
> +               debug("Unknown PMIC type\n");
> +               return -EINVAL;
> +       }
> +
> +       for (i = 0; i < ARRAY_SIZE(pca9450_reg_data); i++) {
> +               if (strcmp(dev->name, pca9450_reg_data[i].name))
> +                       continue;
> +
> +               /* PCA9450B/PCA9450C uses BUCK1 and BUCK3 in dual-phase */
> +               if (type == NXP_CHIP_TYPE_PCA9450BC &&
> +                   !strcmp(pca9450_reg_data[i].name, "BUCK3")) {
> +                       continue;
> +               }
> +
> +               *plat = pca9450_reg_data[i];
> +
> +               return 0;
> +       }
> +
> +       pr_err("Unknown regulator '%s'\n", dev->name);
> +
> +       return -ENOENT;
> +}
> +
> +static const struct dm_regulator_ops pca9450_regulator_ops = {
> +       .get_value      = pca9450_get_value,
> +       .set_value      = pca9450_set_value,
> +       .get_enable     = pca9450_get_enable,
> +       .set_enable     = pca9450_set_enable,
> +};
> +
> +U_BOOT_DRIVER(pca9450_regulator) = {
> +       .name           = PCA9450_REGULATOR_DRIVER,
> +       .id             = UCLASS_REGULATOR,
> +       .ops            = &pca9450_regulator_ops,
> +       .probe          = pca9450_regulator_probe,
> +       .plat_auto      = sizeof(struct pca9450_plat),
> +};
> diff --git a/include/power/pca9450.h b/include/power/pca9450.h
> index 27703bb1f91..b714fc3477d 100644
> --- a/include/power/pca9450.h
> +++ b/include/power/pca9450.h
> @@ -56,4 +56,15 @@ enum {
>
>  int power_pca9450_init(unsigned char bus, unsigned char addr);
>
> +enum {
> +       NXP_CHIP_TYPE_PCA9450A = 0,
> +       NXP_CHIP_TYPE_PCA9450BC,
> +       NXP_CHIP_TYPE_AMOUNT
> +};
> +
> +#define DVS_BUCK_RUN_MASK              0x7f
> +#define PCA9450_LDO12_MASK             0x07
> +#define PCA9450_LDO34_MASK             0x1f
> +#define PCA9450_LDO5_MASK              0x0f
> +
>  #endif
> --
> 2.35.1
>

--
Heiko

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v3 2/2] pmic: pca9450: Add regulator driver
  2022-04-12 22:39 Marek Vasut
@ 2022-04-12 22:39 ` Marek Vasut
  2022-06-19 15:16   ` Heiko Thiery
  0 siblings, 1 reply; 9+ messages in thread
From: Marek Vasut @ 2022-04-12 22:39 UTC (permalink / raw)
  To: u-boot, sbabic; +Cc: Marek Vasut, Fabio Estevam, Peng Fan

Add PCA9450 regulator driver. This is complementary driver for the BUCKn
and LDOn regulators provided by the PCA9450 PMIC driver. Currently the
driver permits reading the settngs and configuring the BUCKn and LDOn
regulators.

Reviewed-by: Fabio Estevam <festevam@denx.de>
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Fabio Estevam <festevam@denx.de>
Cc: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
---
V2: Add RB by Fabio
V3: Rebase on u-boot-imx/master
---
 drivers/power/pmic/pca9450.c      |   6 +-
 drivers/power/regulator/Kconfig   |  15 ++
 drivers/power/regulator/Makefile  |   1 +
 drivers/power/regulator/pca9450.c | 333 ++++++++++++++++++++++++++++++
 include/power/pca9450.h           |  11 +
 5 files changed, 363 insertions(+), 3 deletions(-)
 create mode 100644 drivers/power/regulator/pca9450.c

diff --git a/drivers/power/pmic/pca9450.c b/drivers/power/pmic/pca9450.c
index 26c876c9c45..116ac49a8db 100644
--- a/drivers/power/pmic/pca9450.c
+++ b/drivers/power/pmic/pca9450.c
@@ -83,9 +83,9 @@ static struct dm_pmic_ops pca9450_ops = {
 };
 
 static const struct udevice_id pca9450_ids[] = {
-	{ .compatible = "nxp,pca9450a", .data = 0x25, },
-	{ .compatible = "nxp,pca9450b", .data = 0x25, },
-	{ .compatible = "nxp,pca9450c", .data = 0x25, },
+	{ .compatible = "nxp,pca9450a", .data = NXP_CHIP_TYPE_PCA9450A, },
+	{ .compatible = "nxp,pca9450b", .data = NXP_CHIP_TYPE_PCA9450BC, },
+	{ .compatible = "nxp,pca9450c", .data = NXP_CHIP_TYPE_PCA9450BC, },
 	{ }
 };
 
diff --git a/drivers/power/regulator/Kconfig b/drivers/power/regulator/Kconfig
index cd253b95f2f..d486bad6bdc 100644
--- a/drivers/power/regulator/Kconfig
+++ b/drivers/power/regulator/Kconfig
@@ -60,6 +60,21 @@ config SPL_DM_REGULATOR_BD71837
 	This config enables implementation of driver-model regulator uclass
 	features for regulators on ROHM BD71837 and BD71847 in SPL.
 
+config DM_REGULATOR_PCA9450
+	bool "Enable Driver Model for NXP PCA9450 regulators"
+	depends on DM_REGULATOR && DM_PMIC_PCA9450
+	help
+	This config enables implementation of driver-model regulator uclass
+	features for regulators on NXP PCA9450 PMICs. PCA9450 contains 6 bucks
+	and 5 LDOS. The driver implements get/set api for value and enable.
+
+config SPL_DM_REGULATOR_PCA9450
+	bool "Enable Driver Model for NXP PCA9450 regulators in SPL"
+	depends on DM_REGULATOR_PCA9450
+	help
+	This config enables implementation of driver-model regulator uclass
+	features for regulators on ROHM PCA9450 in SPL.
+
 config DM_REGULATOR_DA9063
 	bool "Enable Driver Model for REGULATOR DA9063"
 	depends on DM_REGULATOR && DM_PMIC_DA9063
diff --git a/drivers/power/regulator/Makefile b/drivers/power/regulator/Makefile
index 4efb32a3228..d2d17f7aed0 100644
--- a/drivers/power/regulator/Makefile
+++ b/drivers/power/regulator/Makefile
@@ -11,6 +11,7 @@ obj-$(CONFIG_$(SPL_)DM_REGULATOR_DA9063) += da9063.o
 obj-$(CONFIG_DM_REGULATOR_MAX77686) += max77686.o
 obj-$(CONFIG_$(SPL_)DM_PMIC_PFUZE100) += pfuze100.o
 obj-$(CONFIG_$(SPL_)DM_REGULATOR_BD71837) += bd71837.o
+obj-$(CONFIG_$(SPL_)DM_REGULATOR_PCA9450) += pca9450.o
 obj-$(CONFIG_$(SPL_)REGULATOR_PWM) += pwm_regulator.o
 obj-$(CONFIG_$(SPL_)DM_REGULATOR_FAN53555) += fan53555.o
 obj-$(CONFIG_$(SPL_)DM_REGULATOR_COMMON) += regulator_common.o
diff --git a/drivers/power/regulator/pca9450.c b/drivers/power/regulator/pca9450.c
new file mode 100644
index 00000000000..4847c9f90f0
--- /dev/null
+++ b/drivers/power/regulator/pca9450.c
@@ -0,0 +1,333 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * NXP PCA9450 regulator driver
+ * Copyright (C) 2022 Marek Vasut <marex@denx.de>
+ *
+ * Largely based on:
+ * ROHM BD71837 regulator driver
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <log.h>
+#include <linux/bitops.h>
+#include <power/pca9450.h>
+#include <power/pmic.h>
+#include <power/regulator.h>
+
+#define HW_STATE_CONTROL 0
+#define DEBUG
+
+/**
+ * struct pca9450_vrange - describe linear range of voltages
+ *
+ * @min_volt:	smallest voltage in range
+ * @step:	how much voltage changes at each selector step
+ * @min_sel:	smallest selector in the range
+ * @max_sel:	maximum selector in the range
+ */
+struct pca9450_vrange {
+	unsigned int	min_volt;
+	unsigned int	step;
+	u8		min_sel;
+	u8		max_sel;
+};
+
+/**
+ * struct pca9450_plat - describe regulator control registers
+ *
+ * @name:	name of the regulator. Used for matching the dt-entry
+ * @enable_reg:	register address used to enable/disable regulator
+ * @enablemask:	register mask used to enable/disable regulator
+ * @volt_reg:	register address used to configure regulator voltage
+ * @volt_mask:	register mask used to configure regulator voltage
+ * @ranges:	pointer to ranges of regulator voltages and matching register
+ *		values
+ * @numranges:	number of voltage ranges pointed by ranges
+ * @dvs:	whether the voltage can be changed when regulator is enabled
+ */
+struct pca9450_plat {
+	const char		*name;
+	u8			enable_reg;
+	u8			enablemask;
+	u8			volt_reg;
+	u8			volt_mask;
+	struct pca9450_vrange	*ranges;
+	unsigned int		numranges;
+	bool			dvs;
+};
+
+#define PCA_RANGE(_min, _vstep, _sel_low, _sel_hi) \
+{ \
+	.min_volt = (_min), .step = (_vstep), \
+	.min_sel = (_sel_low), .max_sel = (_sel_hi), \
+}
+
+#define PCA_DATA(_name, enreg, enmask, vreg, vmask, _range, _dvs) \
+{ \
+	.name = (_name), .enable_reg = (enreg), .enablemask = (enmask), \
+	.volt_reg = (vreg), .volt_mask = (vmask), .ranges = (_range), \
+	.numranges = ARRAY_SIZE(_range), .dvs = (_dvs), \
+}
+
+static struct pca9450_vrange pca9450_buck123_vranges[] = {
+	PCA_RANGE(600000, 12500, 0, 0x7f),
+};
+
+static struct pca9450_vrange pca9450_buck456_vranges[] = {
+	PCA_RANGE(600000, 25000, 0, 0x70),
+	PCA_RANGE(3400000, 0, 0x71, 0x7f),
+};
+
+static struct pca9450_vrange pca9450_ldo1_vranges[] = {
+	PCA_RANGE(1600000, 100000, 0x0, 0x3),
+	PCA_RANGE(3000000, 100000, 0x4, 0x7),
+};
+
+static struct pca9450_vrange pca9450_ldo2_vranges[] = {
+	PCA_RANGE(800000, 50000, 0x0, 0x7),
+};
+
+static struct pca9450_vrange pca9450_ldo34_vranges[] = {
+	PCA_RANGE(800000, 100000, 0x0, 0x19),
+	PCA_RANGE(3300000, 0, 0x1a, 0x1f),
+};
+
+static struct pca9450_vrange pca9450_ldo5_vranges[] = {
+	PCA_RANGE(1800000, 100000, 0x0, 0xf),
+};
+
+/*
+ * We use enable mask 'HW_STATE_CONTROL' to indicate that this regulator
+ * must not be enabled or disabled by SW. The typical use-case for PCA9450
+ * is powering NXP i.MX8. In this use-case we (for now) only allow control
+ * for BUCK4, BUCK5, BUCK6 which are not boot critical.
+ */
+static struct pca9450_plat pca9450_reg_data[] = {
+	/* Bucks 1-3 which support dynamic voltage scaling */
+	PCA_DATA("BUCK1", PCA9450_BUCK1CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK1OUT_DVS0, DVS_BUCK_RUN_MASK,
+		 pca9450_buck123_vranges, true),
+	PCA_DATA("BUCK2", PCA9450_BUCK2CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK2OUT_DVS0, DVS_BUCK_RUN_MASK,
+		 pca9450_buck123_vranges, true),
+	PCA_DATA("BUCK3", PCA9450_BUCK3CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK3OUT_DVS0, DVS_BUCK_RUN_MASK,
+		 pca9450_buck123_vranges, true),
+	/* Bucks 4-6 which do not support dynamic voltage scaling */
+	PCA_DATA("BUCK4", PCA9450_BUCK4CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK4OUT, DVS_BUCK_RUN_MASK,
+		 pca9450_buck456_vranges, false),
+	PCA_DATA("BUCK5", PCA9450_BUCK5CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK5OUT, DVS_BUCK_RUN_MASK,
+		 pca9450_buck456_vranges, false),
+	PCA_DATA("BUCK6", PCA9450_BUCK6CTRL, HW_STATE_CONTROL,
+		 PCA9450_BUCK6OUT, DVS_BUCK_RUN_MASK,
+		 pca9450_buck456_vranges, false),
+	/* LDOs */
+	PCA_DATA("LDO1", PCA9450_LDO1CTRL, HW_STATE_CONTROL,
+		 PCA9450_LDO1CTRL, PCA9450_LDO12_MASK,
+		 pca9450_ldo1_vranges, false),
+	PCA_DATA("LDO2", PCA9450_LDO2CTRL, HW_STATE_CONTROL,
+		 PCA9450_LDO2CTRL, PCA9450_LDO12_MASK,
+		 pca9450_ldo2_vranges, false),
+	PCA_DATA("LDO3", PCA9450_LDO3CTRL, HW_STATE_CONTROL,
+		 PCA9450_LDO3CTRL, PCA9450_LDO34_MASK,
+		 pca9450_ldo34_vranges, false),
+	PCA_DATA("LDO4", PCA9450_LDO4CTRL, HW_STATE_CONTROL,
+		 PCA9450_LDO4CTRL, PCA9450_LDO34_MASK,
+		 pca9450_ldo34_vranges, false),
+	PCA_DATA("LDO5", PCA9450_LDO5CTRL_H, HW_STATE_CONTROL,
+		 PCA9450_LDO5CTRL_H, PCA9450_LDO5_MASK,
+		 pca9450_ldo5_vranges, false),
+};
+
+static int vrange_find_value(struct pca9450_vrange *r, unsigned int sel,
+			     unsigned int *val)
+{
+	if (!val || sel < r->min_sel || sel > r->max_sel)
+		return -EINVAL;
+
+	*val = r->min_volt + r->step * (sel - r->min_sel);
+	return 0;
+}
+
+static int vrange_find_selector(struct pca9450_vrange *r, int val,
+				unsigned int *sel)
+{
+	int ret = -EINVAL;
+	int num_vals = r->max_sel - r->min_sel + 1;
+
+	if (val >= r->min_volt &&
+	    val <= r->min_volt + r->step * (num_vals - 1)) {
+		if (r->step) {
+			*sel = r->min_sel + ((val - r->min_volt) / r->step);
+			ret = 0;
+		} else {
+			*sel = r->min_sel;
+			ret = 0;
+		}
+	}
+	return ret;
+}
+
+static int pca9450_get_enable(struct udevice *dev)
+{
+	struct pca9450_plat *plat = dev_get_plat(dev);
+	int val;
+
+	/*
+	 * boot critical regulators on pca9450 must not be controlled by sw
+	 * due to the 'feature' which leaves power rails down if pca9450 is
+	 * reseted to snvs state. hence we can't get the state here.
+	 *
+	 * if we are alive it means we probably are on run state and
+	 * if the regulator can't be controlled we can assume it is
+	 * enabled.
+	 */
+	if (plat->enablemask == HW_STATE_CONTROL)
+		return 1;
+
+	val = pmic_reg_read(dev->parent, plat->enable_reg);
+	if (val < 0)
+		return val;
+
+	return (val & plat->enablemask);
+}
+
+static int pca9450_set_enable(struct udevice *dev, bool enable)
+{
+	int val = 0;
+	struct pca9450_plat *plat = dev_get_plat(dev);
+
+	/*
+	 * boot critical regulators on pca9450 must not be controlled by sw
+	 * due to the 'feature' which leaves power rails down if pca9450 is
+	 * reseted to snvs state. Hence we can't set the state here.
+	 */
+	if (plat->enablemask == HW_STATE_CONTROL)
+		return enable ? 0 : -EINVAL;
+
+	if (enable)
+		val = plat->enablemask;
+
+	return pmic_clrsetbits(dev->parent, plat->enable_reg, plat->enablemask,
+			       val);
+}
+
+static int pca9450_get_value(struct udevice *dev)
+{
+	struct pca9450_plat *plat = dev_get_plat(dev);
+	unsigned int reg, tmp;
+	int i, ret;
+
+	ret = pmic_reg_read(dev->parent, plat->volt_reg);
+	if (ret < 0)
+		return ret;
+
+	reg = ret;
+	reg &= plat->volt_mask;
+
+	for (i = 0; i < plat->numranges; i++) {
+		struct pca9450_vrange *r = &plat->ranges[i];
+
+		if (!vrange_find_value(r, reg, &tmp))
+			return tmp;
+	}
+
+	pr_err("Unknown voltage value read from pmic\n");
+
+	return -EINVAL;
+}
+
+static int pca9450_set_value(struct udevice *dev, int uvolt)
+{
+	struct pca9450_plat *plat = dev_get_plat(dev);
+	unsigned int sel;
+	int i, found = 0;
+
+	/*
+	 * An under/overshooting may occur if voltage is changed for other
+	 * regulators but buck 1,2,3 or 4 when regulator is enabled. Prevent
+	 * change to protect the HW
+	 */
+	if (!plat->dvs)
+		if (pca9450_get_enable(dev)) {
+			/* If the value is already set, skip the warning. */
+			if (pca9450_get_value(dev) == uvolt)
+				return 0;
+			pr_err("Only DVS bucks can be changed when enabled\n");
+			return -EINVAL;
+		}
+
+	for (i = 0; i < plat->numranges; i++) {
+		struct pca9450_vrange *r = &plat->ranges[i];
+
+		found = !vrange_find_selector(r, uvolt, &sel);
+		if (found) {
+			unsigned int tmp;
+
+			/*
+			 * We require exactly the requested value to be
+			 * supported - this can be changed later if needed
+			 */
+			found = !vrange_find_value(r, sel, &tmp);
+			if (found && tmp == uvolt)
+				break;
+			found = 0;
+		}
+	}
+
+	if (!found)
+		return -EINVAL;
+
+	return pmic_clrsetbits(dev->parent, plat->volt_reg,
+			       plat->volt_mask, sel);
+}
+
+static int pca9450_regulator_probe(struct udevice *dev)
+{
+	struct pca9450_plat *plat = dev_get_plat(dev);
+	int i, type;
+
+	type = dev_get_driver_data(dev_get_parent(dev));
+
+	if (type != NXP_CHIP_TYPE_PCA9450A && type != NXP_CHIP_TYPE_PCA9450BC) {
+		debug("Unknown PMIC type\n");
+		return -EINVAL;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(pca9450_reg_data); i++) {
+		if (strcmp(dev->name, pca9450_reg_data[i].name))
+			continue;
+
+		/* PCA9450B/PCA9450C uses BUCK1 and BUCK3 in dual-phase */
+		if (type == NXP_CHIP_TYPE_PCA9450BC &&
+		    !strcmp(pca9450_reg_data[i].name, "BUCK3")) {
+			continue;
+		}
+
+		*plat = pca9450_reg_data[i];
+
+		return 0;
+	}
+
+	pr_err("Unknown regulator '%s'\n", dev->name);
+
+	return -ENOENT;
+}
+
+static const struct dm_regulator_ops pca9450_regulator_ops = {
+	.get_value	= pca9450_get_value,
+	.set_value	= pca9450_set_value,
+	.get_enable	= pca9450_get_enable,
+	.set_enable	= pca9450_set_enable,
+};
+
+U_BOOT_DRIVER(pca9450_regulator) = {
+	.name		= PCA9450_REGULATOR_DRIVER,
+	.id		= UCLASS_REGULATOR,
+	.ops		= &pca9450_regulator_ops,
+	.probe		= pca9450_regulator_probe,
+	.plat_auto	= sizeof(struct pca9450_plat),
+};
diff --git a/include/power/pca9450.h b/include/power/pca9450.h
index 27703bb1f91..b714fc3477d 100644
--- a/include/power/pca9450.h
+++ b/include/power/pca9450.h
@@ -56,4 +56,15 @@ enum {
 
 int power_pca9450_init(unsigned char bus, unsigned char addr);
 
+enum {
+	NXP_CHIP_TYPE_PCA9450A = 0,
+	NXP_CHIP_TYPE_PCA9450BC,
+	NXP_CHIP_TYPE_AMOUNT
+};
+
+#define DVS_BUCK_RUN_MASK		0x7f
+#define PCA9450_LDO12_MASK		0x07
+#define PCA9450_LDO34_MASK		0x1f
+#define PCA9450_LDO5_MASK		0x0f
+
 #endif
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-06-20  5:03 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-20  3:10 [PATCH v3 1/2] pmic: pca9450: Add upstream regulators subnode match Marek Vasut
2022-05-20  3:10 ` [PATCH v3 2/2] pmic: pca9450: Add regulator driver Marek Vasut
2022-05-20 13:44   ` sbabic
2022-05-20 13:44 ` [PATCH v3 1/2] pmic: pca9450: Add upstream regulators subnode match sbabic
  -- strict thread matches above, loose matches on Subject: below --
2022-04-12 22:39 Marek Vasut
2022-04-12 22:39 ` [PATCH v3 2/2] pmic: pca9450: Add regulator driver Marek Vasut
2022-06-19 15:16   ` Heiko Thiery
2022-06-19 16:01     ` Heiko Thiery
2022-06-20  2:47       ` Marek Vasut
2022-06-20  5:03         ` Vaittinen, Matti

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.