Browse Source

dm: button: add an uclass for button

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>
Philippe Reynes 3 years ago
parent
commit
30d66db787
7 changed files with 123 additions and 0 deletions
  1. 2 0
      drivers/Kconfig
  2. 1 0
      drivers/Makefile
  3. 12 0
      drivers/button/Kconfig
  4. 5 0
      drivers/button/Makefile
  5. 43 0
      drivers/button/button-uclass.c
  6. 59 0
      include/button.h
  7. 1 0
      include/dm/uclass-id.h

+ 2 - 0
drivers/Kconfig

@@ -16,6 +16,8 @@ source "drivers/block/Kconfig"
 
 source "drivers/bootcount/Kconfig"
 
+source "drivers/button/Kconfig"
+
 source "drivers/cache/Kconfig"
 
 source "drivers/clk/Kconfig"

+ 1 - 0
drivers/Makefile

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

+ 12 - 0
drivers/button/Kconfig

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

+ 5 - 0
drivers/button/Makefile

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

+ 43 - 0
drivers/button/button-uclass.c

@@ -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),
+};

+ 59 - 0
include/button.h

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

+ 1 - 0
include/dm/uclass-id.h

@@ -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 */