U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Stanley Chu <stanley.chuys@gmail.com>
To: lukma@denx.de, jagan@amarulasolutions.com,
	andre.przywara@arm.com, festevam@denx.de,
	narmstrong@baylibre.com, pbrobinson@gmail.com,
	tharvey@gateworks.com, christianshewitt@gmail.com,
	lokeshvutla@ti.com, sjg@chromium.org, sr@denx.de,
	michal.simek@xilinx.com, hs@denx.de, weijie.gao@mediatek.com,
	hannes.schmelzer@br-automation.com, harm.berntsen@nedap.com,
	sebastian.reichel@collabora.com, stephan@gerhold.net,
	fangyuanseu@gmail.com, kettenis@openbsd.org, seanga2@gmail.com,
	dsankouski@gmail.com, vabhav.sharma@nxp.com, bmeng.cn@gmail.com,
	patrick@blueri.se, samuel@sholland.org,
	giulio.benetti@benettiengineering.com, mr.bossman075@gmail.com,
	yschu@nuvoton.com, kwliu@nuvoton.com, ctcchien@nuvoton.com,
	avifishman70@gmail.com, tmaimon77@gmail.com
Cc: u-boot@lists.denx.de, openbmc@lists.ozlabs.org
Subject: [PATCH v1 5/9] gpio: npcm: Add support for Nuvoton NPCM SoCs
Date: Wed, 15 Dec 2021 10:57:56 +0800	[thread overview]
Message-ID: <20211215025800.26918-6-yschu@nuvoton.com> (raw)
In-Reply-To: <20211215025800.26918-1-yschu@nuvoton.com>

Add Nuvoton BMC NPCM7xx/NPCM8xx gpio driver

Signed-off-by: Stanley Chu <yschu@nuvoton.com>
---
 drivers/gpio/Kconfig     |   7 +++
 drivers/gpio/Makefile    |   1 +
 drivers/gpio/npcm_gpio.c | 133 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 141 insertions(+)
 create mode 100644 drivers/gpio/npcm_gpio.c

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 40abc33772..2aed8fdae3 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -523,4 +523,11 @@ config NOMADIK_GPIO
 	  into a number of banks each with 32 GPIOs. The GPIOs for a device are
 	  defined in the device tree with one node for each bank.
 
+config NPCM_GPIO
+	bool "NuvoTon NPCM GPIO driver"
+	depends on DM_GPIO
+	help
+	  Support GPIO controllers on NuvoTon NPCM SoCs.
+	  It contains eight GPIO modules with total 256 pins.
+
 endif
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 3c851b38c7..d0206580c4 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -69,3 +69,4 @@ obj-$(CONFIG_NX_GPIO)		+= nx_gpio.o
 obj-$(CONFIG_SIFIVE_GPIO)	+= sifive-gpio.o
 obj-$(CONFIG_NOMADIK_GPIO)	+= nmk_gpio.o
 obj-$(CONFIG_MAX7320_GPIO)	+= max7320_gpio.o
+obj-$(CONFIG_NPCM_GPIO)      += npcm_gpio.o
diff --git a/drivers/gpio/npcm_gpio.c b/drivers/gpio/npcm_gpio.c
new file mode 100644
index 0000000000..703d2e4815
--- /dev/null
+++ b/drivers/gpio/npcm_gpio.c
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2021 Nuvoton Technology.
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <errno.h>
+#include <asm/gpio.h>
+#include <dm/device.h>
+#include <linux/bitops.h>
+#include <linux/io.h>
+#include <linux/sizes.h>
+
+#define NPCM_GPIO_PORTS_PER_BANK    32
+
+#define NPCM_GPIO_REG_DIN		0x04	/* RO - Data In */
+#define NPCM_GPIO_REG_DOUT		0x0C	/* RW - Data Out */
+#define NPCM_GPIO_REG_IEM		0x58	/* RW - Input Enable Mask */
+#define NPCM_GPIO_REG_OE		0x10	/* RW - Output Enable */
+#define NPCM_GPIO_REG_OES		0x70	/* WO - Output Enable Register Set */
+#define NPCM_GPIO_REG_OEC		0x74	/* WO - Output Enable Register Clear */
+
+struct npcm_gpio_priv {
+	void __iomem *base;
+};
+
+static void npcm_gpio_offset_write(struct udevice *dev, unsigned int offset,
+				   unsigned int reg, int value)
+{
+	struct npcm_gpio_priv *priv = dev_get_priv(dev);
+	u32 tmp;
+
+	tmp = readl(priv->base + reg);
+
+	if (value)
+		tmp |= BIT(offset);
+	else
+		tmp &= ~BIT(offset);
+
+	writel(tmp, priv->base + reg);
+}
+
+static int npcm_gpio_offset_read(struct udevice *dev, unsigned int offset,
+				 unsigned int reg)
+{
+	struct npcm_gpio_priv *priv = dev_get_priv(dev);
+
+	return !!(readl(priv->base + reg) & BIT(offset));
+}
+
+static int npcm_gpio_direction_input(struct udevice *dev, unsigned int offset)
+{
+	npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_OEC, 1);
+	npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_IEM, 1);
+
+	return 0;
+}
+
+static int npcm_gpio_direction_output(struct udevice *dev, unsigned int offset,
+				      int value)
+{
+	npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_IEM, 0);
+	npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_OES, 1);
+	npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_DOUT, value);
+
+	return 0;
+}
+
+static int npcm_gpio_get_value(struct udevice *dev, unsigned int offset)
+{
+	if (npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_IEM))
+		return npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_DIN);
+
+	if (npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_OE))
+		return npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_DOUT);
+
+	return -EINVAL;
+}
+
+static int npcm_gpio_set_value(struct udevice *dev, unsigned int offset,
+			       int value)
+{
+	npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_DOUT, value);
+
+	return 0;
+}
+
+static int npcm_gpio_get_function(struct udevice *dev, unsigned int offset)
+{
+	if (npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_IEM))
+		return GPIOF_INPUT;
+
+	if (npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_OE))
+		return GPIOF_OUTPUT;
+
+	return GPIOF_FUNC;
+}
+
+static const struct dm_gpio_ops npcm_gpio_ops = {
+	.direction_input	= npcm_gpio_direction_input,
+	.direction_output	= npcm_gpio_direction_output,
+	.get_value		= npcm_gpio_get_value,
+	.set_value		= npcm_gpio_set_value,
+	.get_function		= npcm_gpio_get_function,
+};
+
+static int npcm_gpio_probe(struct udevice *dev)
+{
+	struct npcm_gpio_priv *priv = dev_get_priv(dev);
+	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
+
+	priv->base = dev_read_addr_ptr(dev);
+	uc_priv->gpio_count = NPCM_GPIO_PORTS_PER_BANK;
+	uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
+
+	return 0;
+}
+
+static const struct udevice_id npcm_gpio_match[] = {
+	{ .compatible = "nuvoton,npcm845-gpio" },
+	{ .compatible = "nuvoton,npcm750-gpio" },
+	{ }
+};
+
+U_BOOT_DRIVER(npcm_gpio) = {
+	.name	= "npcm_gpio",
+	.id	= UCLASS_GPIO,
+	.of_match = npcm_gpio_match,
+	.probe	= npcm_gpio_probe,
+	.priv_auto = sizeof(struct npcm_gpio_priv),
+	.ops	= &npcm_gpio_ops,
+};
-- 
2.17.1


  parent reply	other threads:[~2021-12-15  3:02 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-15  2:57 [PATCH v1 0/9] Add Nuvoton NPCM845 support Stanley Chu
2021-12-15  2:57 ` [PATCH v1 1/9] arm: nuvoton: Add support for Nuvoton NPCM845 BMC Stanley Chu
2021-12-15 12:12   ` Giulio Benetti
2022-03-11  0:50     ` Stanley Chu
2022-03-11  1:06       ` Giulio Benetti
2022-03-11  1:07         ` Giulio Benetti
2021-12-15 18:32   ` Sean Anderson
2022-03-11  0:55     ` Stanley Chu
     [not found]     ` <d23d2925-d5ea-e23b-fb99-7856aedfb328@gmail.com>
     [not found]       ` <0fee2e28-b3c8-d649-0921-52e6fb098e71@gmail.com>
2022-03-11  0:39         ` Stanley Chu
2022-03-11  0:57       ` Stanley Chu
2022-03-10 18:49   ` Tom Rini
2022-03-11  2:13     ` Stanley Chu
2022-03-11  2:50       ` Tom Rini
2022-03-11  2:53       ` Giulio Benetti
2022-03-11  4:07         ` Stanley Chu
2021-12-15  2:57 ` [PATCH v1 2/9] clk: nuvoton: Add support for NPCM845 Stanley Chu
2021-12-15 18:32   ` Sean Anderson
2022-02-16 16:25   ` Tom Rini
2022-02-17  2:08     ` Stanley
2021-12-15  2:57 ` [PATCH v1 3/9] timer: npcm: Add NPCM timer support Stanley Chu
2021-12-15  2:57 ` [PATCH v1 4/9] serial: npcm: Add support for Nuvoton NPCM SoCs Stanley Chu
2021-12-15  2:57 ` Stanley Chu [this message]
2021-12-15  2:57 ` [PATCH v1 6/9] pinctrl: nuvoton: Add NPCM8xx pinctrl driver Stanley Chu
2021-12-15  2:57 ` [PATCH v1 7/9] spi: npcm-fiu: add NPCM8xx FIU controller driver Stanley Chu
2021-12-15  2:57 ` [PATCH v1 8/9] ARM: dts: Add Nuvoton NPCM845 device tree Stanley Chu
2021-12-15 13:07   ` Tom Rini
2021-12-15 13:32     ` 
2021-12-15 13:35       ` Tom Rini
2021-12-15 15:32         ` Jesse Taube
2021-12-15 15:39           ` Tom Rini
2021-12-15  2:58 ` [PATCH v1 9/9] ARM: configs: Add defconfig for Nuvoton NPCM845 Stanley Chu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211215025800.26918-6-yschu@nuvoton.com \
    --to=stanley.chuys@gmail.com \
    --cc=andre.przywara@arm.com \
    --cc=avifishman70@gmail.com \
    --cc=bmeng.cn@gmail.com \
    --cc=christianshewitt@gmail.com \
    --cc=ctcchien@nuvoton.com \
    --cc=dsankouski@gmail.com \
    --cc=fangyuanseu@gmail.com \
    --cc=festevam@denx.de \
    --cc=giulio.benetti@benettiengineering.com \
    --cc=hannes.schmelzer@br-automation.com \
    --cc=harm.berntsen@nedap.com \
    --cc=hs@denx.de \
    --cc=jagan@amarulasolutions.com \
    --cc=kettenis@openbsd.org \
    --cc=kwliu@nuvoton.com \
    --cc=lokeshvutla@ti.com \
    --cc=lukma@denx.de \
    --cc=michal.simek@xilinx.com \
    --cc=mr.bossman075@gmail.com \
    --cc=narmstrong@baylibre.com \
    --cc=openbmc@lists.ozlabs.org \
    --cc=patrick@blueri.se \
    --cc=pbrobinson@gmail.com \
    --cc=samuel@sholland.org \
    --cc=seanga2@gmail.com \
    --cc=sebastian.reichel@collabora.com \
    --cc=sjg@chromium.org \
    --cc=sr@denx.de \
    --cc=stephan@gerhold.net \
    --cc=tharvey@gateworks.com \
    --cc=tmaimon77@gmail.com \
    --cc=u-boot@lists.denx.de \
    --cc=vabhav.sharma@nxp.com \
    --cc=weijie.gao@mediatek.com \
    --cc=yschu@nuvoton.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox