[PATCH] drivers: mfd: da9063: Add restart notifier implementation

From: Alexandre Ghiti
Date: Tue Sep 21 2021 - 01:34:06 EST


The SiFive Unmatched board uses the da9063 PMIC for reset: add a restart
notifier that will execute a small i2c sequence allowing to reset the
board.

The original implementation comes from Marcus Comstedt and Anders Montonen
(https://forums.sifive.com/t/reboot-command/4721/28).

Signed-off-by: Alexandre Ghiti <alexandre.ghiti@xxxxxxxxxxxxx>
---
drivers/mfd/da9063-core.c | 25 +++++++++++++++++++++++++
include/linux/mfd/da9063/core.h | 3 +++
2 files changed, 28 insertions(+)

diff --git a/drivers/mfd/da9063-core.c b/drivers/mfd/da9063-core.c
index df407c3afce3..c87b8d611f20 100644
--- a/drivers/mfd/da9063-core.c
+++ b/drivers/mfd/da9063-core.c
@@ -20,6 +20,7 @@
#include <linux/mutex.h>
#include <linux/mfd/core.h>
#include <linux/regmap.h>
+#include <linux/reboot.h>

#include <linux/mfd/da9063/core.h>
#include <linux/mfd/da9063/registers.h>
@@ -158,6 +159,18 @@ static int da9063_clear_fault_log(struct da9063 *da9063)
return ret;
}

+static int da9063_restart_notify(struct notifier_block *this,
+ unsigned long mode, void *cmd)
+{
+ struct da9063 *da9063 = container_of(this, struct da9063, restart_handler);
+
+ regmap_write(da9063->regmap, DA9063_REG_PAGE_CON, 0x00);
+ regmap_write(da9063->regmap, DA9063_REG_CONTROL_F, 0x04);
+ regmap_write(da9063->regmap, DA9063_REG_CONTROL_A, 0x68);
+
+ return NOTIFY_DONE;
+}
+
int da9063_device_init(struct da9063 *da9063, unsigned int irq)
{
int ret;
@@ -197,6 +210,18 @@ int da9063_device_init(struct da9063 *da9063, unsigned int irq)
}
}

+ da9063->restart_handler.notifier_call = da9063_restart_notify;
+ da9063->restart_handler.priority = 128;
+ ret = register_restart_handler(&da9063->restart_handler);
+ if (ret) {
+ dev_err(da9063->dev, "Failed to register restart handler\n");
+ return ret;
+ }
+
+ devm_add_action(da9063->dev,
+ (void (*)(void *))unregister_restart_handler,
+ &da9063->restart_handler);
+
return ret;
}

diff --git a/include/linux/mfd/da9063/core.h b/include/linux/mfd/da9063/core.h
index fa7a43f02f27..1b20c498e340 100644
--- a/include/linux/mfd/da9063/core.h
+++ b/include/linux/mfd/da9063/core.h
@@ -85,6 +85,9 @@ struct da9063 {
int chip_irq;
unsigned int irq_base;
struct regmap_irq_chip_data *regmap_irq;
+
+ /* Restart */
+ struct notifier_block restart_handler;
};

int da9063_device_init(struct da9063 *da9063, unsigned int irq);
--
2.30.2