From af1a266670d040d2f4083ff309d732d648afba2a Mon Sep 17 00:00:00 2001
From: Angelos Mouzakitis <a.mouzakitis@virtualopensystems.com>
Date: Tue, 10 Oct 2023 14:33:42 +0000
Subject: Add submodule dependency files

Change-Id: Iaf8d18082d3991dec7c0ebbea540f092188eb4ec
---
 roms/u-boot/drivers/button/button-gpio.c | 112 +++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
 create mode 100644 roms/u-boot/drivers/button/button-gpio.c

(limited to 'roms/u-boot/drivers/button/button-gpio.c')

diff --git a/roms/u-boot/drivers/button/button-gpio.c b/roms/u-boot/drivers/button/button-gpio.c
new file mode 100644
index 000000000..dbb000622
--- /dev/null
+++ b/roms/u-boot/drivers/button/button-gpio.c
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com>
+ */
+
+#include <common.h>
+#include <button.h>
+#include <dm.h>
+#include <dm/lists.h>
+#include <dm/uclass-internal.h>
+#include <log.h>
+#include <asm/gpio.h>
+
+struct button_gpio_priv {
+	struct gpio_desc gpio;
+};
+
+static enum button_state_t button_gpio_get_state(struct udevice *dev)
+{
+	struct button_gpio_priv *priv = dev_get_priv(dev);
+	int ret;
+
+	if (!dm_gpio_is_valid(&priv->gpio))
+		return -EREMOTEIO;
+	ret = dm_gpio_get_value(&priv->gpio);
+	if (ret < 0)
+		return ret;
+
+	return ret ? BUTTON_ON : BUTTON_OFF;
+}
+
+static int button_gpio_probe(struct udevice *dev)
+{
+	struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev);
+	struct button_gpio_priv *priv = dev_get_priv(dev);
+	int ret;
+
+	/* Ignore the top-level button node */
+	if (!uc_plat->label)
+		return 0;
+
+	ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_IN);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static int button_gpio_remove(struct udevice *dev)
+{
+	/*
+	 * The GPIO driver may have already been removed. We will need to
+	 * address this more generally.
+	 */
+	if (!IS_ENABLED(CONFIG_SANDBOX)) {
+		struct button_gpio_priv *priv = dev_get_priv(dev);
+
+		if (dm_gpio_is_valid(&priv->gpio))
+			dm_gpio_free(dev, &priv->gpio);
+	}
+
+	return 0;
+}
+
+static int button_gpio_bind(struct udevice *parent)
+{
+	struct udevice *dev;
+	ofnode node;
+	int ret;
+
+	dev_for_each_subnode(node, parent) {
+		struct button_uc_plat *uc_plat;
+		const char *label;
+
+		label = ofnode_read_string(node, "label");
+		if (!label) {
+			debug("%s: node %s has no label\n", __func__,
+			      ofnode_get_name(node));
+			return -EINVAL;
+		}
+		ret = device_bind_driver_to_node(parent, "button_gpio",
+						 ofnode_get_name(node),
+						 node, &dev);
+		if (ret)
+			return ret;
+		uc_plat = dev_get_uclass_plat(dev);
+		uc_plat->label = label;
+	}
+
+	return 0;
+}
+
+static const struct button_ops button_gpio_ops = {
+	.get_state	= button_gpio_get_state,
+};
+
+static const struct udevice_id button_gpio_ids[] = {
+	{ .compatible = "gpio-keys" },
+	{ .compatible = "gpio-keys-polled" },
+	{ }
+};
+
+U_BOOT_DRIVER(button_gpio) = {
+	.name		= "button_gpio",
+	.id		= UCLASS_BUTTON,
+	.of_match	= button_gpio_ids,
+	.ops		= &button_gpio_ops,
+	.priv_auto	= sizeof(struct button_gpio_priv),
+	.bind		= button_gpio_bind,
+	.probe		= button_gpio_probe,
+	.remove		= button_gpio_remove,
+};
-- 
cgit