This adds infrastructure for DM based poweroff drivers
including a generic poweroff command, that is exposed via
existing CONFIG_CMD_POWEROFF and uses the first registered
poweroff driver.

Signed-off-by: Sebastian Reichel <sebastian.reic...@collabora.com>
---
 drivers/power/Kconfig           |  9 +++++++
 drivers/power/Makefile          |  2 ++
 drivers/power/poweroff-uclass.c | 43 +++++++++++++++++++++++++++++++++
 include/dm/uclass-id.h          |  1 +
 include/power/poweroff.h        | 32 ++++++++++++++++++++++++
 5 files changed, 87 insertions(+)
 create mode 100644 drivers/power/poweroff-uclass.c
 create mode 100644 include/power/poweroff.h

diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
index 5910926faca4..5a76e23b5fb0 100644
--- a/drivers/power/Kconfig
+++ b/drivers/power/Kconfig
@@ -373,4 +373,13 @@ config POWER_MT6323
          This adds poweroff driver for mt6323
          this pmic is used on mt7623 / Bananapi R2
 
+config POWEROFF
+       bool "power-off support"
+       depends on DM
+       help
+         Most boards have support to power-off themself. U-Boot provides
+         a uclass API to implement this feature. Poweroff drivers can
+         provide access to board-specific implementations. Using device
+         tree for configuration is recommended.
+
 endmenu
diff --git a/drivers/power/Makefile b/drivers/power/Makefile
index 2dcc7bb99d02..924e99114ccd 100644
--- a/drivers/power/Makefile
+++ b/drivers/power/Makefile
@@ -21,3 +21,5 @@ obj-$(CONFIG_POWER_FSL) += power_fsl.o
 obj-$(CONFIG_POWER_I2C) += power_i2c.o
 obj-$(CONFIG_POWER_SPI) += power_spi.o
 obj-$(CONFIG_POWER_MT6323) += mt6323.o
+
+obj-$(CONFIG_POWEROFF) += poweroff-uclass.o
diff --git a/drivers/power/poweroff-uclass.c b/drivers/power/poweroff-uclass.c
new file mode 100644
index 000000000000..c7ffaaca9bee
--- /dev/null
+++ b/drivers/power/poweroff-uclass.c
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2019 Collabora Ltd.
+ * Copyright (c) 2019 GE Inc.
+ *
+ * Written by Sebastian Reichel <sebastian.reic...@collabora.com>
+ */
+
+#include <command.h>
+#include <dm.h>
+#include <errno.h>
+#include <power/poweroff.h>
+
+#ifdef CONFIG_CMD_POWEROFF
+int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
+{
+       struct udevice *dev;
+       int ret;
+
+       ret = uclass_get_device(UCLASS_POWEROFF, 0, &dev);
+       if (ret) {
+               printf("Could not find poweroff device: %d\n", ret);
+               return ret;
+       }
+
+       return dm_poweroff(dev);
+}
+#endif
+
+int dm_poweroff(struct udevice *dev)
+{
+       struct poweroff_ops *ops = poweroff_get_ops(dev);
+
+       if (!ops->poweroff)
+               return -ENOSYS;
+
+       return ops->poweroff(dev);
+}
+
+UCLASS_DRIVER(poweroff) = {
+       .id             = UCLASS_POWEROFF,
+       .name           = "poweroff",
+};
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index 7837d459f18c..4adc415ffcfe 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -84,6 +84,7 @@ enum uclass_id {
        UCLASS_PINCONFIG,       /* Pin configuration node device */
        UCLASS_PINCTRL,         /* Pinctrl (pin muxing/configuration) device */
        UCLASS_PMIC,            /* PMIC I/O device */
+       UCLASS_POWEROFF,        /* Poweroff, e.g. via gpio or a special 
register */
        UCLASS_POWER_DOMAIN,    /* (SoC) Power domains */
        UCLASS_PWM,             /* Pulse-width modulator */
        UCLASS_PWRSEQ,          /* Power sequence device */
diff --git a/include/power/poweroff.h b/include/power/poweroff.h
new file mode 100644
index 000000000000..ffaff24649a6
--- /dev/null
+++ b/include/power/poweroff.h
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2019 Collabora Ltd.
+ * Copyright (c) 2019 GE Inc.
+ *
+ * Written by Sebastian Reichel <sebastian.reic...@collabora.com>
+ */
+
+#ifndef __POWEROFF_H
+#define __POWEROFF_H
+
+struct poweroff_ops {
+       /**
+        * poweroff() - poweroff the system
+        *
+        * @dev:        Device used for powering off the system
+        * @return -ve on error (system is powered off otherwise)
+        */
+       int (*poweroff)(struct udevice *dev);
+};
+
+#define poweroff_get_ops(dev)  ((struct poweroff_ops *)(dev)->driver->ops)
+
+/**
+ * dm_poweroff() - Poweroff system via DM poweroff driver
+ *
+ * @dev:       Device used for powering off the system
+ * @return -ve on error (system is powered off otherwise)
+ */
+int dm_poweroff(struct udevice *dev);
+
+#endif
-- 
2.27.0

Reply via email to