]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
dm: button: add an uclass for button
authorPhilippe Reynes <philippe.reynes@softathome.com>
Fri, 24 Jul 2020 16:19:45 +0000 (18:19 +0200)
committerSimon Glass <sjg@chromium.org>
Wed, 29 Jul 2020 01:30:39 +0000 (19:30 -0600)
Add a new uclass for button that implements two functions:
- button_get_by_label
- button_get_status

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Philippe Reynes <philippe.reynes@softathome.com>
drivers/Kconfig
drivers/Makefile
drivers/button/Kconfig [new file with mode: 0644]
drivers/button/Makefile [new file with mode: 0644]
drivers/button/button-uclass.c [new file with mode: 0644]
include/button.h [new file with mode: 0644]
include/dm/uclass-id.h

index 7a839fa1aad78f662fd2ad4dc56cfa73bb19854d..119e412849f64bed6e23d399c2b28289aa038828 100644 (file)
@@ -16,6 +16,8 @@ source "drivers/block/Kconfig"
 
 source "drivers/bootcount/Kconfig"
 
+source "drivers/button/Kconfig"
+
 source "drivers/cache/Kconfig"
 
 source "drivers/clk/Kconfig"
index afd159e903a3c6575c97c8b1ca6b3c10838f77f0..2178871bfb5fa965f87ce31823d3a279d5c64a08 100644 (file)
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0+
 
+obj-$(CONFIG_$(SPL_TPL_)BUTTON) += button/
 obj-$(CONFIG_$(SPL_TPL_)CACHE) += cache/
 obj-$(CONFIG_$(SPL_TPL_)CLK) += clk/
 obj-$(CONFIG_$(SPL_TPL_)DM) += core/
diff --git a/drivers/button/Kconfig b/drivers/button/Kconfig
new file mode 100644 (file)
index 0000000..8301858
--- /dev/null
@@ -0,0 +1,12 @@
+menu "Button Support"
+
+config BUTTON
+       bool "Enable button support"
+       depends on DM
+       help
+         Many boards have buttons which can be used to change behaviour (reset, ...).
+         U-Boot provides a uclass API to implement this feature. Button drivers
+         can provide access to board-specific buttons. Use of the device tree
+         for configuration is encouraged.
+
+endmenu
diff --git a/drivers/button/Makefile b/drivers/button/Makefile
new file mode 100644 (file)
index 0000000..0b4c128
--- /dev/null
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com>
+
+obj-$(CONFIG_BUTTON) += button-uclass.o
diff --git a/drivers/button/button-uclass.c b/drivers/button/button-uclass.c
new file mode 100644 (file)
index 0000000..1c742c2
--- /dev/null
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com>
+ *
+ * Based on led-uclass.c
+ */
+
+#include <common.h>
+#include <button.h>
+#include <dm.h>
+#include <dm/uclass-internal.h>
+
+int button_get_by_label(const char *label, struct udevice **devp)
+{
+       struct udevice *dev;
+       struct uclass *uc;
+
+       uclass_id_foreach_dev(UCLASS_BUTTON, dev, uc) {
+               struct button_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
+
+               /* Ignore the top-level button node */
+               if (uc_plat->label && !strcmp(label, uc_plat->label))
+                       return uclass_get_device_tail(dev, 0, devp);
+       }
+
+       return -ENODEV;
+}
+
+enum button_state_t button_get_state(struct udevice *dev)
+{
+       struct button_ops *ops = button_get_ops(dev);
+
+       if (!ops->get_state)
+               return -ENOSYS;
+
+       return ops->get_state(dev);
+}
+
+UCLASS_DRIVER(button) = {
+       .id             = UCLASS_BUTTON,
+       .name           = "button",
+       .per_device_platdata_auto_alloc_size = sizeof(struct button_uc_plat),
+};
diff --git a/include/button.h b/include/button.h
new file mode 100644 (file)
index 0000000..688b63b
--- /dev/null
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com>
+ */
+
+#ifndef __BUTTON_H
+#define __BUTTON_H
+
+/**
+ * struct button_uc_plat - Platform data the uclass stores about each device
+ *
+ * @label:     Button label
+ */
+struct button_uc_plat {
+       const char *label;
+};
+
+/**
+ * enum button_state_t - State used for button
+ * - BUTTON_OFF - Button is not pressed
+ * - BUTTON_ON - Button is pressed
+ * - BUTTON_COUNT - Number of button state
+ */
+enum button_state_t {
+       BUTTON_OFF = 0,
+       BUTTON_ON = 1,
+       BUTTON_COUNT,
+};
+
+struct button_ops {
+       /**
+        * get_state() - get the state of a button
+        *
+        * @dev:        button device to change
+        * @return button state button_state_t, or -ve on error
+        */
+       enum button_state_t (*get_state)(struct udevice *dev);
+};
+
+#define button_get_ops(dev)    ((struct button_ops *)(dev)->driver->ops)
+
+/**
+ * button_get_by_label() - Find a button device by label
+ *
+ * @label:     button label to look up
+ * @devp:      Returns the associated device, if found
+ * @return 0 if found, -ENODEV if not found, other -ve on error
+ */
+int button_get_by_label(const char *label, struct udevice **devp);
+
+/**
+ * button_get_state() - get the state of a button
+ *
+ * @dev:       button device to change
+ * @return button state button_state_t, or -ve on error
+ */
+enum button_state_t button_get_state(struct udevice *dev);
+
+#endif
index 690a8ed4df49c1c900ad5055daf6e1ef9537de13..dbc14ec34294984ba463a90dea9cdb9670e05249 100644 (file)
@@ -38,6 +38,7 @@ enum uclass_id {
        UCLASS_BLK,             /* Block device */
        UCLASS_BOARD,           /* Device information from hardware */
        UCLASS_BOOTCOUNT,       /* Bootcount backing store */
+       UCLASS_BUTTON,          /* Button */
        UCLASS_CACHE,           /* Cache controller */
        UCLASS_CLK,             /* Clock source, e.g. used by peripherals */
        UCLASS_CPU,             /* CPU, typically part of an SoC */