[RFC 3/4] gpio: scmi: add SCMI pinctrl based gpio driver

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



SCMI pin control protocol supports not only pin controllers, but also
gpio controllers by design. This patch includes a generic gpio driver
which allows consumer drivers to access gpio pins that are handled
through SCMI interfaces.

Signed-off-by: AKASHI Takahiro <takahiro.akashi@xxxxxxxxxx>
---
 drivers/gpio/Kconfig     |   8 ++
 drivers/gpio/Makefile    |   1 +
 drivers/gpio/gpio-scmi.c | 154 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 163 insertions(+)
 create mode 100644 drivers/gpio/gpio-scmi.c

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 673bafb8be58..1a968b950f3a 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -566,6 +566,14 @@ config GPIO_SAMA5D2_PIOBU
 	  The difference from regular GPIOs is that they
 	  maintain their value during backup/self-refresh.
 
+config GPIO_SCMI
+	tristate "GPIO support based on SCMI pinctrl"
+	depends on OF_GPIO
+	depends on PINCTRL_SCMI
+	help
+	  Select this option to support GPIO devices based on SCMI pin
+	  control protocol.
+
 config GPIO_SIFIVE
 	tristate "SiFive GPIO support"
 	depends on OF_GPIO
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index eb73b5d633eb..2abe1e9d5e77 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -141,6 +141,7 @@ obj-$(CONFIG_ARCH_SA1100)		+= gpio-sa1100.o
 obj-$(CONFIG_GPIO_SAMA5D2_PIOBU)	+= gpio-sama5d2-piobu.o
 obj-$(CONFIG_GPIO_SCH311X)		+= gpio-sch311x.o
 obj-$(CONFIG_GPIO_SCH)			+= gpio-sch.o
+obj-$(CONFIG_GPIO_SCMI)			+= gpio-scmi.o
 obj-$(CONFIG_GPIO_SIFIVE)		+= gpio-sifive.o
 obj-$(CONFIG_GPIO_SIM)			+= gpio-sim.o
 obj-$(CONFIG_GPIO_SIOX)			+= gpio-siox.o
diff --git a/drivers/gpio/gpio-scmi.c b/drivers/gpio/gpio-scmi.c
new file mode 100644
index 000000000000..ece63ea62b70
--- /dev/null
+++ b/drivers/gpio/gpio-scmi.c
@@ -0,0 +1,154 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// Copyright (C) 2023 Linaro Inc.
+//   Author: AKASHI takahiro <takahiro.akashi@xxxxxxxxxx>
+
+#include <linux/gpio/driver.h>
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/pinctrl/consumer.h>
+#include <linux/platform_device.h>
+#include <linux/types.h>
+#include "gpiolib.h"
+
+struct scmi_gpio_priv {
+	struct gpio_chip chip;
+};
+
+static int scmi_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
+{
+	unsigned long config;
+
+	config = PIN_CONFIG_OUTPUT_ENABLE;
+	if (pinctrl_gpio_get_config(chip->gpiodev->base + offset, &config))
+		return -1;
+	if (config)
+		return GPIO_LINE_DIRECTION_OUT;
+
+	config = PIN_CONFIG_INPUT_ENABLE;
+	if (pinctrl_gpio_get_config(chip->gpiodev->base + offset, &config))
+		return -1;
+	if (config)
+		return GPIO_LINE_DIRECTION_IN;
+
+	return -1;
+}
+
+static int scmi_gpio_direction_input(struct gpio_chip *chip,
+				     unsigned int offset)
+{
+	return pinctrl_gpio_direction_input(chip->gpiodev->base + offset);
+}
+
+static int scmi_gpio_direction_output(struct gpio_chip *chip,
+				      unsigned int offset, int val)
+{
+	return pinctrl_gpio_direction_output(chip->gpiodev->base + offset);
+}
+
+static int scmi_gpio_get(struct gpio_chip *chip, unsigned int offset)
+{
+	unsigned long config;
+
+	/* FIXME: currently, PIN_CONFIG_INPUT not defined */
+	config = PIN_CONFIG_INPUT;
+	if (pinctrl_gpio_get_config(chip->gpiodev->base + offset, &config))
+		return -1;
+
+	/* FIXME: the packed format not defined */
+	if (config >> 8)
+		return 1;
+
+	return 0;
+}
+
+static void scmi_gpio_set(struct gpio_chip *chip, unsigned int offset, int val)
+{
+	unsigned long config;
+
+	config = PIN_CONF_PACKED(PIN_CONFIG_OUTPUT, val & 0x1);
+;
+	pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
+}
+
+static u16 sum_up_ngpios(struct gpio_chip *chip)
+{
+	struct gpio_pin_range *range;
+	struct gpio_device *gdev = chip->gpiodev;
+	u16 ngpios = 0;
+
+	list_for_each_entry(range, &gdev->pin_ranges, node) {
+		ngpios += range->range.npins;
+	}
+
+	return ngpios;
+}
+
+static int scmi_gpio_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *parent_np;
+	struct scmi_gpio_priv *priv;
+	struct gpio_chip *chip;
+	int ret;
+
+	/* FIXME: who should be the parent */
+	parent_np = NULL;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	chip = &priv->chip;
+	chip->label = dev_name(dev);
+	chip->parent = dev;
+	chip->base = -1;
+
+	chip->request = gpiochip_generic_request;
+	chip->free = gpiochip_generic_free;
+	chip->get_direction = scmi_gpio_get_direction;
+	chip->direction_input = scmi_gpio_direction_input;
+	chip->direction_output = scmi_gpio_direction_output;
+	chip->get = scmi_gpio_get;
+	chip->set = scmi_gpio_set;
+
+	ret = devm_gpiochip_add_data(dev, chip, priv);
+	if (ret)
+		return ret;
+
+	chip->ngpio = sum_up_ngpios(chip);
+
+	platform_set_drvdata(pdev, priv);
+
+	return 0;
+}
+
+static int scmi_gpio_remove(struct platform_device *pdev)
+{
+	struct scmi_gpio_priv *priv = platform_get_drvdata(pdev);
+
+	gpiochip_remove(&priv->chip);
+
+	return 0;
+}
+
+static const struct of_device_id scmi_gpio_match[] = {
+	{ .compatible = "arm,scmi-gpio-generic" },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, scmi_gpio_match);
+
+static struct platform_driver scmi_gpio_driver = {
+	.probe = scmi_gpio_probe,
+	.remove = scmi_gpio_remove,
+	.driver = {
+		.name = "scmi-gpio",
+		.of_match_table = scmi_gpio_match,
+	},
+};
+module_platform_driver(scmi_gpio_driver);
+
+MODULE_AUTHOR("AKASHI Takahiro <takahiro.akashi@xxxxxxxxxx>");
+MODULE_DESCRIPTION("SCMI Pinctrl based GPIO driver");
+MODULE_LICENSE("GPL");
-- 
2.34.1





[Index of Archives]     [Device Tree Compilter]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux PCI Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Yosemite Backpacking]


  Powered by Linux