mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Holger Assmann <h.assmann@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Holger Assmann <h.assmann@pengutronix.de>
Subject: [PATCH v2] mfd: regulator: initial support for pca9450
Date: Fri, 29 Sep 2023 10:57:26 +0200	[thread overview]
Message-ID: <20230929085726.1897008-1-h.assmann@pengutronix.de> (raw)

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>
---

Changes v1 -> v2:

1. removed unnecessary whitespaces
2. code re-arrangement

---
 drivers/mfd/Kconfig   |   7 +++
 drivers/mfd/Makefile  |   1 +
 drivers/mfd/pca9450.c | 107 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 115 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..929c4f68da
--- /dev/null
+++ b/drivers/mfd/pca9450.c
@@ -0,0 +1,107 @@
+// 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_SW_RST		0x20
+#define REASON_WDOG		0x40
+#define REASON_PWON		0x80
+
+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,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




             reply	other threads:[~2023-09-29  8:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-29  8:57 Holger Assmann [this message]
2023-10-04  8:23 ` Sascha Hauer

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20230929085726.1897008-1-h.assmann@pengutronix.de \
    --to=h.assmann@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

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

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