mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] mfd: regulator: initial support for pca9450
@ 2023-07-24  7:52 Holger Assmann
  2023-07-26  9:40 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Holger Assmann @ 2023-07-24  7:52 UTC (permalink / raw)
  To: barebox; +Cc: Holger Assmann

The PCA9450 PMIC by NXP can be communicated with via I2C; this driver uses
that do determine the reset source that caused the PMIC to perform its
latest power cycle.

Signed-off-by: Holger Assmann <h.assmann@pengutronix.de>
---
 drivers/mfd/Kconfig   |   7 +++
 drivers/mfd/Makefile  |   1 +
 drivers/mfd/pca9450.c | 106 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
 create mode 100644 drivers/mfd/pca9450.c

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index 39a40c2a3f..5189364c2c 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -69,6 +69,13 @@ config MFD_STPMIC1
 	help
 	  Select this to support communication with the STPMIC1.
 
+config MFD_PCA9450
+	depends on I2C
+	select REGMAP_I2C
+	bool "PCA9450 MFD driver"
+	help
+	  Select this to support communication with the PCA9450 PMIC.
+
 config MFD_RN568PMIC
 	depends on I2C
 	select REGMAP_I2C
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index f19bf53655..00f3eacf3c 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -25,3 +25,4 @@ obj-$(CONFIG_MFD_RK808)		+= rk808.o
 obj-$(CONFIG_MFD_AXP20X_I2C)	+= axp20x-i2c.o axp20x.o
 obj-$(CONFIG_MFD_ATMEL_SMC)	+= atmel-smc.o
 obj-$(CONFIG_MFD_ROHM_BD718XX)	+= rohm-bd718x7.o
+obj-$(CONFIG_MFD_PCA9450)	+= pca9450.o
diff --git a/drivers/mfd/pca9450.c b/drivers/mfd/pca9450.c
new file mode 100644
index 0000000000..3e4a63f398
--- /dev/null
+++ b/drivers/mfd/pca9450.c
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2023 Holger Assmann, Pengutronix
+ */
+
+#include <common.h>
+#include <driver.h>
+#include <errno.h>
+#include <i2c/i2c.h>
+#include <init.h>
+#include <mfd/pca9450.h>
+#include <of.h>
+#include <regmap.h>
+#include <reset_source.h>
+
+#define REASON_PMIC_RST		0x10
+#define REASON_PWON		0x80
+#define REASON_SW_RST		0x20
+#define REASON_WDOG		0x40
+
+static const struct regmap_config pca9450_regmap_i2c_config = {
+	.reg_bits = 8,
+	.val_bits = 8,
+	.max_register = 0x2E,
+};
+
+static int pca9450_get_reset_source(struct device *dev, struct regmap *map)
+{
+	enum reset_src_type type;
+	int reg;
+	int ret;
+
+	ret = regmap_read(map, PCA9450_PWRON_STAT, &reg);
+	if (ret)
+		return ret;
+	
+	switch (reg) {
+		case REASON_PWON:
+			dev_dbg(dev, "Power ON triggered by PMIC_ON_REQ.\n");
+			type = RESET_POR;
+			break;
+		case REASON_WDOG:
+			dev_dbg(dev, "Detected cold reset by WDOGB pin\n");
+			type = RESET_WDG;
+			break;
+		case REASON_SW_RST:
+			dev_dbg(dev, "Detected cold reset by SW_RST\n");
+			type = RESET_RST;
+			break;
+		case REASON_PMIC_RST:
+			dev_dbg(dev, "Detected cold reset by PMIC_RST_B\n");
+			type = RESET_EXT;
+			break;
+		default:
+			dev_warn(dev, "Could not determine reset reason.\n");
+			type = RESET_UKWN;
+	}
+
+	reset_source_set_device(dev, type);
+
+	return 0;
+};
+	
+static int __init pca9450_probe(struct device *dev)
+{
+	struct regmap *regmap;
+	int reg;
+	int ret;
+
+	regmap = regmap_init_i2c(to_i2c_client(dev), &pca9450_regmap_i2c_config);
+	if (IS_ERR(regmap))
+		return PTR_ERR(regmap);
+
+	ret = regmap_register_cdev(regmap, NULL);
+	if (ret)
+		return ret;
+
+	ret = regmap_read(regmap, PCA9450_REG_DEV_ID, &reg);
+	if (ret) {
+		dev_err(dev, "Unable to read PMIC Chip ID\n");
+		return ret;
+	}
+
+	/* Chip ID defined in bits [7:4] */
+	dev_info(dev, "PMIC Chip ID: 0x%x\n", (reg >> 4));
+
+	pca9450_get_reset_source(dev,regmap);
+
+	return of_platform_populate(dev->of_node, NULL, dev);
+}
+
+static __maybe_unused struct of_device_id pca9450_dt_ids[] = {
+	{ .compatible = "nxp,pca9450a" },
+	{ .compatible = "nxp,pca9450b" },
+	{ .compatible = "nxp,pca9450c" },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, pca9450_dt_ids);
+
+static struct driver pca9450_i2c_driver = {
+	.name		= "pca9450-i2c",
+	.probe		= pca9450_probe,
+	.of_compatible	= DRV_OF_COMPAT(pca9450_dt_ids),
+};
+
+coredevice_i2c_driver(pca9450_i2c_driver);
-- 
2.39.2




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

end of thread, other threads:[~2023-07-26  9:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-24  7:52 [PATCH] mfd: regulator: initial support for pca9450 Holger Assmann
2023-07-26  9:40 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox