Skip to contentSkip to conversation

add support for MCP16502 PMIC

PATCH Superseded [email protected] 2 participants 8 messages Nov 13, 2018 – Nov 23, 2018

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover add support for MCP16502 PMIC

MCP16502 is a Power Management IC from Microchip.
It has 4 Buck outputs and 2 LDOs. The buck regulators can be used in two modes: normal(FPWM) and low-power(Auto PFM).

This patch series adds support for the MCP16502 PMIC.

Andrei Stefanescu (3):
regulator: dt-bindings: add MCP16502 regulator bindings
MAINTAINERS: add maintainer for MCP16502 PMIC driver
regulator: mcp16502: add regulator driver for MCP16502

 .../bindings/regulator/mcp16502-regulator.txt      | 107 +++++
 MAINTAINERS                                        |   7 +
 drivers/regulator/Kconfig                          |   9 +
 drivers/regulator/Makefile                         |   1 +
 drivers/regulator/mcp16502.c                       | 524 +++++++++++++++++++++
 5 files changed, 648 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/regulator/mcp16502-regulator.txt
 create mode 100644 drivers/regulator/mcp16502.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/3 regulator: dt-bindings: add MCP16502 regulator bindings

This patch describes the compatible and the device tree bindings necessary for the MCP16502 PMIC.

---
 .../bindings/regulator/mcp16502-regulator.txt      | 107 +++++++++++++++++++++
 1 file changed, 107 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/regulator/mcp16502-regulator.txt
Documentation/devicetree/bindings/regulator/mcp16502-regulator.txt new file +107 -0
new file mode 100644
index 0000000..bb3d787
@@ -0,0 +1,107 @@
+MCP16502 PMIC
+
+Required properties:
+- compatible: "microchip,mcp16502"
+- reg: I2C slave address
+- lpm-gpio: GPIO for LPM pin. Note that this GPIO *must* remain high during
+	     suspend-to-ram, keeping the PMIC into HIBERNATE mode.
+- regulators: A node that houses a sub-node for each regulator within
+              the device. Each sub-node is identified using the node's
+              name. The content of each sub-node is defined by the
+              standard binding for regulators; see regulator.txt.
+
+Regulator modes:
+2 - FPWM: higher precision, higher consumption
+4 - AutoPFM: lower precision, lower consumption
+
+Each regulator is defined using the standard binding for regulators.
+
+Example:
+
+mcp16502 at 5b {
+	compatible = "microchip,mcp16502";
+	reg = <0x5b>;
+	status = "okay";
+	lpm-gpios = <&pioBU 7 GPIO_ACTIVE_HIGH>;
+
+	regulators {
+		VDD_IO {
+			regulator-name = "VDD_IO";
+			regulator-min-microvolt = <1200000>;
+			regulator-max-microvolt = <3700000>;
+			regulator-initial-mode = <2>;
+			regulator-allowed-modes = <2>, <4>;
+			regulator-always-on;
+
+			regulator-state-mem {
+				regulator-off-in-suspend;
+				regulator-mode = <4>;
+			};
+		};
+
+		VDD_DDR {
+			regulator-name = "VDD_DDR";
+			regulator-min-microvolt = <600000>;
+			regulator-max-microvolt = <1850000>;
+			regulator-initial-mode = <2>;
+			regulator-allowed-modes = <2>, <4>;
+			regulator-always-on;
+
+			regulator-state-mem {
+				regulator-on-in-suspend;
+				regulator-mode = <4>;
+			};
+		};
+
+		VDD_CORE {
+			regulator-name = "VDD_CORE";
+			regulator-min-microvolt = <600000>;
+			regulator-max-microvolt = <1850000>;
+			regulator-initial-mode = <2>;
+			regulator-allowed-modes = <2>, <4>;
+			regulator-always-on;
+
+			regulator-state-mem {
+				regulator-off-in-suspend;
+				regulator-mode = <4>;
+			};
+		};
+
+		VDD_OTHER {
+			regulator-name = "VDD_OTHER";
+			regulator-min-microvolt = <600000>;
+			regulator-max-microvolt = <1850000>;
+			regulator-initial-mode = <2>;
+			regulator-allowed-modes = <2>, <4>;
+			regulator-always-on;
+
+			regulator-state-mem {
+				regulator-off-in-suspend;
+				regulator-mode = <4>;
+			};
+		};
+
+		LDO1 {
+			regulator-name = "LDO1";
+			regulator-min-microvolt = <1200000>;
+			regulator-max-microvolt = <3700000>;
+			regulator-always-on;
+
+			regulator-state-mem {
+				regulator-off-in-suspend;
+			};
+		};
+
+		LDO2 {
+			regulator-name = "LDO2";
+			regulator-min-microvolt = <1200000>;
+			regulator-max-microvolt = <3700000>;
+			regulator-always-on;
+
+			regulator-state-mem {
+				regulator-off-in-suspend;
+			};
+		};
+
+	};
+};
Signature · 1 line
2.7.4
#3 lore raw

PATCH 2/3 MAINTAINERS: add maintainer for MCP16502 PMIC driver

In reply to [email protected]

This patch adds a maintainer for the MCP16502 PMIC driver.

---
 MAINTAINERS | 7 +++++++
 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
index f485597..edfad02 100644
@@ -9723,6 +9723,13 @@ M:	Ludovic Desroches <[email protected]>
 S:	Maintained
 F:	drivers/mmc/host/atmel-mci.c
 
+MICROCHIP MCP16502 PMIC DRIVER
+M:	Andrei Stefanescu <[email protected]>
+L:	linux-arm-kernel at lists.infradead.org (moderated for non-subscribers)
+S:	Maintained
+F:	Documentation/devicetree/bindings/regulator/mcp16502-regulator.txt
+F:	drivers/regulator/mcp16502.c
+
 MICROCHIP MCP3911 ADC DRIVER
 M:	Marcus Folkesson <[email protected]>
 M:	Kent Gustavsson <[email protected]>
Signature · 1 line
2.7.4
#4 lore raw

PATCH 3/3 regulator: mcp16502: add regulator driver for MCP16502

In reply to [email protected]

This patch adds a regulator driver for the MCP16502 PMIC. This drivers supports basic operations through the regulator interface such as:
- setting/reading voltage
- setting/reading operating mode
- reading current status
- transitioning to/from suspend-to-ram and standby power states

---
 drivers/regulator/Kconfig    |   9 +
 drivers/regulator/Makefile   |   1 +
 drivers/regulator/mcp16502.c | 524 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 534 insertions(+)
 create mode 100644 drivers/regulator/mcp16502.c
drivers/regulator/Kconfig +9 -0
index 926cee0..719d9d6 100644
@@ -567,6 +567,15 @@ config REGULATOR_MC13892
 	  Say y here to support the regulators found on the Freescale MC13892
 	  PMIC.
 
+config REGULATOR_MCP16502
+	tristate "Microchip MCP16502 PMIC"
+	depends on I2C && OF
+	help
+	  Say y here to support the MCP16502 PMIC. This driver supports
+	  basic operations (get/set voltage, get/set operating mode)
+	  through the regulator interface. In addition it enables
+	  suspend-to-ram/standby transition.
+
 config REGULATOR_MT6311
 	tristate "MediaTek MT6311 PMIC"
 	depends on I2C
drivers/regulator/Makefile +1 -0
index 72488ef..b12e1c9 100644
@@ -74,6 +74,7 @@ obj-$(CONFIG_REGULATOR_MAX77802) += max77802-regulator.o
 obj-$(CONFIG_REGULATOR_MC13783) += mc13783-regulator.o
 obj-$(CONFIG_REGULATOR_MC13892) += mc13892-regulator.o
 obj-$(CONFIG_REGULATOR_MC13XXX_CORE) +=  mc13xxx-regulator-core.o
+obj-$(CONFIG_REGULATOR_MCP16502) += mcp16502.o
 obj-$(CONFIG_REGULATOR_MT6311) += mt6311-regulator.o
 obj-$(CONFIG_REGULATOR_MT6323)	+= mt6323-regulator.o
 obj-$(CONFIG_REGULATOR_MT6380)	+= mt6380-regulator.o
drivers/regulator/mcp16502.c new file +524 -0

524 lines not on this page · raw message

Signature · 1 line
2.7.4
maintainer
#2 lore raw

Re: [PATCH 1/3] regulator: dt-bindings: add MCP16502 regulator bindings

In reply to [email protected]

Documentation/devicetree/bindings/regulator/mcp16502-regulator.txt line 7

Quoted 2 lines · Andrei.Stefanescu at microchip.com

+- lpm-gpio: GPIO for LPM pin. Note that this GPIO *must* remain high during

Documentation/devicetree/bindings/regulator/mcp16502-regulator.txt new file +1 -0
+	     suspend-to-ram, keeping the PMIC into HIBERNATE mode.

All GPIO properties should be -gpios even if they can only ever have one GPIO.

Documentation/devicetree/bindings/regulator/mcp16502-regulator.txt line 11

Quoted 4 lines

+- regulators: A node that houses a sub-node for each regulator within

Documentation/devicetree/bindings/regulator/mcp16502-regulator.txt new file +3 -0
+              the device. Each sub-node is identified using the node's
+              name. The content of each sub-node is defined by the
+              standard binding for regulators; see regulator.txt.

You should describe which regulators exist for the device.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20181113/a80a64a4/attachment.sig>

maintainer
#5 lore raw

Re: [PATCH 3/3] regulator: mcp16502: add regulator driver for MCP16502

In reply to [email protected]

drivers/regulator/mcp16502.c line 3

Quoted 7 lines · Andrei.Stefanescu at microchip.com

index 0000000..29c72d3

drivers/regulator/mcp16502.c new file +3 -0
@@ -0,0 +1,524 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * MCP16502 PMIC driver

Please make the entire comment a C++ comment so it looks more intentional.

drivers/regulator/mcp16502.c line 58

Quoted 5 lines
drivers/regulator/mcp16502.c new file +5 -0
+/*
+ * This macro is useful for iterating over all regulators and accessing their
+ * registers in a generic way or accessing a regulator device by its id.
+ */
+#define BASE(i) (((i) + 1) << 4)

This macro name is likely to run into collisions@some point, a prefix would be better.

drivers/regulator/mcp16502.c line 130

Quoted 8 lines
drivers/regulator/mcp16502.c new file +8 -0
+static int mcp16502_update_regulator(struct regulator_dev *rdev,
+				     bool hibernate,
+				     unsigned int mask, unsigned int val)
+{
+	struct mcp16502 *mcp = rdev_get_drvdata(rdev);
+	unsigned int reg = BASE(rdev_get_id(rdev));
+
+	reg += (hibernate) ? OFFSET_MODE_HIB : OFFSET_MODE_A;

Please write this as a normal if statement to improve legibility.

Quoted 5 lines
drivers/regulator/mcp16502.c new file +5 -0
+static int mcp16502_read(struct regulator_dev *rdev, bool hibernate,
+			 unsigned int mask)
+{
+	struct mcp16502 *mcp = rdev_get_drvdata(rdev);
+	unsigned int reg = BASE(rdev_get_id(rdev));

drivers/regulator/mcp16502.c line 156

Quoted 10 lines
drivers/regulator/mcp16502.c new file +10 -0
+	int ret, val;
+
+	reg += (hibernate) ? OFFSET_MODE_HIB : OFFSET_MODE_A;
+
+	ret = regmap_read(mcp->rmap, reg, &val);
+	if (ret)
+		return ret;
+
+	return val & mask;
+}

I'm not convinced that these custom read/write functions are adding much compared to just using the standard regmap helpers for things - they're adding a bunch of code instead of data. If you need extra helpers for suspend mode just add those, I'm sure they'd be useful for other drivers.

Quoted 5 lines
drivers/regulator/mcp16502.c new file +5 -0
+static unsigned int mcp16502_get_mode(struct regulator_dev *rdev)
+{
+	int val;
+
+	val = mcp16502_read(rdev, false, MCP16502_MODE_MASK);

drivers/regulator/mcp16502.c line 193

Quoted 10 lines
drivers/regulator/mcp16502.c new file +10 -0
+	if (val < 0)
+		return val;
+
+	if (val == MCP16502_MODE_FPWM)
+		return REGULATOR_MODE_NORMAL;
+	else if (val == MCP16502_MODE_AUTO_PFM)
+		return REGULATOR_MODE_IDLE;
+
+	return REGULATOR_MODE_INVALID;
+}

Please just write a switch statement to improve legibility.

drivers/regulator/mcp16502.c line 207

Quoted 13 lines
drivers/regulator/mcp16502.c new file +13 -0
+/*
+ * mcp16502_is_enabled - regulator_ops is_enabled
+ */
+static int mcp16502_is_enabled(struct regulator_dev *rdev)
+{
+	int val;
+
+	val = mcp16502_read(rdev, false, MCP16502_EN_MASK);
+	if (val < 0)
+		return val;
+
+	return !!val;
+}

This can use regulator_is_enabled_regmap().

drivers/regulator/mcp16502.c line 232

Quoted 8 lines
drivers/regulator/mcp16502.c new file +8 -0
+	if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE)
+		return -EINVAL;
+
+	val = (mode == REGULATOR_MODE_NORMAL) ? MCP16502_MODE_FPWM :
+						MCP16502_MODE_AUTO_PFM;
+
+	return mcp16502_update_regulator(rdev, hibernate, MCP16502_MODE_MASK,
+					 val);

Again a switch statement would be clearer.

drivers/regulator/mcp16502.c line 285

Quoted 13 lines
drivers/regulator/mcp16502.c new file +13 -0
+static int mcp16502_get_status(struct regulator_dev *rdev)
+{
+	int mode = mcp16502_get_mode(rdev);
+
+	if (!mcp16502_is_enabled(rdev))
+		return REGULATOR_STATUS_OFF;
+	else if (mode == REGULATOR_MODE_NORMAL)
+		return REGULATOR_STATUS_NORMAL;
+	else if (mode == REGULATOR_MODE_IDLE)
+		return REGULATOR_STATUS_IDLE;
+
+	return REGULATOR_STATUS_UNDEFINED;
+}

The _status() function should only be implemented if there's hardware support for reading back the actual status from the device, we already know what we set through software.

drivers/regulator/mcp16502.c line 475

Quoted 10 lines
drivers/regulator/mcp16502.c new file +10 -0
+#ifdef CONFIG_PM_SLEEP
+static int mcp16502_suspend(struct device *dev)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct mcp16502 *mcp = i2c_get_clientdata(client);
+
+	mcp16502_gpio_set_mode(mcp, MCP16502_MODE_HIB);
+
+	return 0;
+}

This will put the regulators into hibernate mode before the system has finished suspending which is likely to cause breakage - hibernate mode in the PMIC is expected to be triggered by the SoC as it shuts down.

drivers/regulator/mcp16502.c line 512

Quoted 4 lines
drivers/regulator/mcp16502.c new file +4 -0
+static int __init mcp16502_init(void)
+{
+	return i2c_add_driver(&mcp16502_drv);
+}

module_i2c_driver.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20181113/fc30dea7/attachment.sig>

#6 lore raw

Re: [PATCH 3/3] regulator: mcp16502: add regulator driver for MCP16502

Hello Mark,

Thank you for your review. While working on the next version I realized that the implementation should change a bit. The mcp16502 PMIC has separate registers
for each of its operating modes (Performance, Active, Low-power, Hibernate). So, it is possible to setup the values for Low-power (Linux standby) and Hibernate (Linux suspend-to-ram) and these values would not be affected by changes made during runtime (which are made to the ACTIVE registers).

This means that the calls to suspend_set_* are not necessary to be made each time the board suspends. My idea is to add three functions to the regulator_ops
(setup_suspend_standby/mem/max) which would be called after the regulator is registered and which would set the values found inside the devicetree in the regulator-state-standby/mem/disk subnodes.

However, I am not sure whether this is in fact a good idea or which is the best
approach.

Other possible suggestions for this setup_suspend* functions:
- Break each function into smaller pieces(set_voltage, set_mode, enable/disable)
- Add support for regmap helpers, which would mean adding reg and mask members
? to regulator_desc. However, I am not sure which naming scheme you prefer ? (enable_suspend_mem_reg for example seems not to be a great idea).
Perhaps use
? arrays (e.g. enable_suspend[] and index it via PM_SUSPEND_*)?

Best regards,
Andrei

Quoted 129 lines · Mark Brown

drivers/regulator/mcp16502.c line 3

Quoted 7 lines · Andrei.Stefanescu at microchip.com

index 0000000..29c72d3

drivers/regulator/mcp16502.c new file +3 -0
@@ -0,0 +1,524 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * MCP16502 PMIC driver

Please make the entire comment a C++ comment so it looks more intentional.

drivers/regulator/mcp16502.c line 58

Quoted 5 lines
drivers/regulator/mcp16502.c new file +5 -0
+/*
+ * This macro is useful for iterating over all regulators and accessing their
+ * registers in a generic way or accessing a regulator device by its id.
+ */
+#define BASE(i) (((i) + 1) << 4)

This macro name is likely to run into collisions at some point, a prefix would be better.

drivers/regulator/mcp16502.c line 130

Quoted 8 lines
drivers/regulator/mcp16502.c new file +8 -0
+static int mcp16502_update_regulator(struct regulator_dev *rdev,
+				     bool hibernate,
+				     unsigned int mask, unsigned int val)
+{
+	struct mcp16502 *mcp = rdev_get_drvdata(rdev);
+	unsigned int reg = BASE(rdev_get_id(rdev));
+
+	reg += (hibernate) ? OFFSET_MODE_HIB : OFFSET_MODE_A;

Please write this as a normal if statement to improve legibility.

Quoted 5 lines
drivers/regulator/mcp16502.c new file +5 -0
+static int mcp16502_read(struct regulator_dev *rdev, bool hibernate,
+			 unsigned int mask)
+{
+	struct mcp16502 *mcp = rdev_get_drvdata(rdev);
+	unsigned int reg = BASE(rdev_get_id(rdev));

drivers/regulator/mcp16502.c line 156

Quoted 10 lines
drivers/regulator/mcp16502.c new file +10 -0
+	int ret, val;
+
+	reg += (hibernate) ? OFFSET_MODE_HIB : OFFSET_MODE_A;
+
+	ret = regmap_read(mcp->rmap, reg, &val);
+	if (ret)
+		return ret;
+
+	return val & mask;
+}

I'm not convinced that these custom read/write functions are adding much compared to just using the standard regmap helpers for things - they're adding a bunch of code instead of data. If you need extra helpers for suspend mode just add those, I'm sure they'd be useful for other drivers.

Quoted 5 lines
drivers/regulator/mcp16502.c new file +5 -0
+static unsigned int mcp16502_get_mode(struct regulator_dev *rdev)
+{
+	int val;
+
+	val = mcp16502_read(rdev, false, MCP16502_MODE_MASK);

drivers/regulator/mcp16502.c line 193

Quoted 10 lines
drivers/regulator/mcp16502.c new file +10 -0
+	if (val < 0)
+		return val;
+
+	if (val == MCP16502_MODE_FPWM)
+		return REGULATOR_MODE_NORMAL;
+	else if (val == MCP16502_MODE_AUTO_PFM)
+		return REGULATOR_MODE_IDLE;
+
+	return REGULATOR_MODE_INVALID;
+}

Please just write a switch statement to improve legibility.

drivers/regulator/mcp16502.c line 207

Quoted 13 lines
drivers/regulator/mcp16502.c new file +13 -0
+/*
+ * mcp16502_is_enabled - regulator_ops is_enabled
+ */
+static int mcp16502_is_enabled(struct regulator_dev *rdev)
+{
+	int val;
+
+	val = mcp16502_read(rdev, false, MCP16502_EN_MASK);
+	if (val < 0)
+		return val;
+
+	return !!val;
+}

This can use regulator_is_enabled_regmap().

drivers/regulator/mcp16502.c line 232

Quoted 8 lines
drivers/regulator/mcp16502.c new file +8 -0
+	if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE)
+		return -EINVAL;
+
+	val = (mode == REGULATOR_MODE_NORMAL) ? MCP16502_MODE_FPWM :
+						MCP16502_MODE_AUTO_PFM;
+
+	return mcp16502_update_regulator(rdev, hibernate, MCP16502_MODE_MASK,
+					 val);

Again a switch statement would be clearer.

drivers/regulator/mcp16502.c line 285

Quoted 13 lines
drivers/regulator/mcp16502.c new file +13 -0
+static int mcp16502_get_status(struct regulator_dev *rdev)
+{
+	int mode = mcp16502_get_mode(rdev);
+
+	if (!mcp16502_is_enabled(rdev))
+		return REGULATOR_STATUS_OFF;
+	else if (mode == REGULATOR_MODE_NORMAL)
+		return REGULATOR_STATUS_NORMAL;
+	else if (mode == REGULATOR_MODE_IDLE)
+		return REGULATOR_STATUS_IDLE;
+
+	return REGULATOR_STATUS_UNDEFINED;
+}

The _status() function should only be implemented if there's hardware support for reading back the actual status from the device, we already know what we set through software.

drivers/regulator/mcp16502.c line 475

Quoted 10 lines
drivers/regulator/mcp16502.c new file +10 -0
+#ifdef CONFIG_PM_SLEEP
+static int mcp16502_suspend(struct device *dev)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct mcp16502 *mcp = i2c_get_clientdata(client);
+
+	mcp16502_gpio_set_mode(mcp, MCP16502_MODE_HIB);
+
+	return 0;
+}

This will put the regulators into hibernate mode before the system has finished suspending which is likely to cause breakage - hibernate mode in the PMIC is expected to be triggered by the SoC as it shuts down.

drivers/regulator/mcp16502.c line 512

Quoted 4 lines
drivers/regulator/mcp16502.c new file +4 -0
+static int __init mcp16502_init(void)
+{
+	return i2c_add_driver(&mcp16502_drv);
+}

module_i2c_driver.

maintainer
#7 lore raw

Re: [PATCH 3/3] regulator: mcp16502: add regulator driver for MCP16502

Quoted 7 lines · Andrei.Stefanescu at microchip.com

Thank you for your review. While working on the next version I realized that the implementation should change a bit. The mcp16502 PMIC has separate registers
for each of its operating modes (Performance, Active, Low-power, Hibernate). So, it is possible to setup the values for Low-power (Linux standby) and Hibernate (Linux suspend-to-ram) and these values would not be affected by changes made during runtime (which are made to the ACTIVE registers).

This is totally normal for regulators, it's the only way that suspend modes can function sensibly since suspend mode configuration is typically not capable off supplying the processor. If your driver is currently trying to use the normal operating registers then it's buggy.

Quoted 6 lines

This means that the calls to suspend_set_* are not necessary to be made each time the board suspends. My idea is to add three functions to the regulator_ops
(setup_suspend_standby/mem/max) which would be called after the regulator is registered and which would set the values found inside the devicetree in the regulator-state-standby/mem/disk subnodes.

There is no need to add new operations, we already have a number of
suspend specific operations covering enable state, modes and voltage.
You should just use those, there's already code to call them.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20181123/f7dcec59/attachment-0001.sig>