Re: [PATCH v4 2/2] gpio: add driver for SAMA5D2 PIOBU pins

2018-12-21 Thread Linus Walleij
On Wed, Dec 12, 2018 at 12:57 PM  wrote:

> PIOBU pins do not lose their voltage during Backup/Self-refresh.
> This patch adds a simple GPIO controller for them and a
> maintainer for the driver.
>
> This driver adds support for using the pins as GPIO
> offering the possibility to read/set the voltage.
>
> Signed-off-by: Andrei Stefanescu 

Patch applied for v4.21, sorry for taking so long to reply!

Good work on this driver Andrei, it looks really nice now.

Yours,
Linus Walleij


[PATCH v4 2/2] gpio: add driver for SAMA5D2 PIOBU pins

2018-12-12 Thread Andrei.Stefanescu
PIOBU pins do not lose their voltage during Backup/Self-refresh.
This patch adds a simple GPIO controller for them and a
maintainer for the driver.

This driver adds support for using the pins as GPIO
offering the possibility to read/set the voltage.

Signed-off-by: Andrei Stefanescu 
---
 MAINTAINERS   |   6 +
 drivers/gpio/Kconfig  |  11 ++
 drivers/gpio/Makefile |   1 +
 drivers/gpio/gpio-sama5d2-piobu.c | 253 ++
 4 files changed, 271 insertions(+)
 create mode 100644 drivers/gpio/gpio-sama5d2-piobu.c

diff --git a/MAINTAINERS b/MAINTAINERS
index f485597..fadc96d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -9760,6 +9760,12 @@ M:   Nicolas Ferre 
 S: Supported
 F: drivers/power/reset/at91-sama5d2_shdwc.c
 
+MICROCHIP SAMA5D2-COMPATIBLE PIOBU GPIO
+M: Andrei Stefanescu 
+L: linux-arm-ker...@lists.infradead.org (moderated for non-subscribers)
+L: linux-g...@vger.kernel.org
+F: drivers/gpio/gpio-sama5d2-piobu.c
+
 MICROCHIP SPI DRIVER
 M: Nicolas Ferre 
 S: Supported
diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 833a1b5..1c41fac 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -429,6 +429,17 @@ config GPIO_REG
  A 32-bit single register GPIO fixed in/out implementation.  This
  can be used to represent any register as a set of GPIO signals.
 
+config GPIO_SAMA5D2_PIOBU
+   tristate "SAMA5D2 PIOBU GPIO support"
+   depends on MFD_SYSCON
+   select GPIO_SYSCON
+   help
+ Say yes here to use the PIOBU pins as GPIOs.
+
+ PIOBU pins on the SAMA5D2 can be used as GPIOs.
+ The difference from regular GPIOs is that they
+ maintain their value during backup/self-refresh.
+
 config GPIO_SIOX
tristate "SIOX GPIO support"
depends on SIOX
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 671c447..f18d345 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -108,6 +108,7 @@ obj-$(CONFIG_GPIO_RDC321X)  += gpio-rdc321x.o
 obj-$(CONFIG_GPIO_RCAR)+= gpio-rcar.o
 obj-$(CONFIG_GPIO_REG) += gpio-reg.o
 obj-$(CONFIG_ARCH_SA1100)  += gpio-sa1100.o
+obj-$(CONFIG_GPIO_SAMA5D2_PIOBU)   += gpio-sama5d2-piobu.o
 obj-$(CONFIG_GPIO_SCH) += gpio-sch.o
 obj-$(CONFIG_GPIO_SCH311X) += gpio-sch311x.o
 obj-$(CONFIG_GPIO_SNPS_CREG)   += gpio-creg-snps.o
diff --git a/drivers/gpio/gpio-sama5d2-piobu.c 
b/drivers/gpio/gpio-sama5d2-piobu.c
new file mode 100644
index 000..03a0006
--- /dev/null
+++ b/drivers/gpio/gpio-sama5d2-piobu.c
@@ -0,0 +1,253 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SAMA5D2 PIOBU GPIO controller
+ *
+ * Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries
+ *
+ * Author: Andrei Stefanescu 
+ *
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define PIOBU_NUM 8
+#define PIOBU_REG_SIZE 4
+
+/*
+ * backup mode protection register for tamper detection
+ * normal mode protection register for tamper detection
+ * wakeup signal generation
+ */
+#define PIOBU_BMPR 0x7C
+#define PIOBU_NMPR 0x80
+#define PIOBU_WKPR 0x90
+
+#define PIOBU_BASE 0x18 /* PIOBU offset from SECUMOD base register address. */
+
+#define PIOBU_DET_OFFSET 16
+
+/* In the datasheet this bit is called OUTPUT */
+#define PIOBU_DIRECTION BIT(8)
+#define PIOBU_OUT BIT(8)
+#define PIOBU_IN 0
+
+#define PIOBU_SOD BIT(9)
+#define PIOBU_PDS BIT(10)
+
+#define PIOBU_HIGH BIT(9)
+#define PIOBU_LOW 0
+
+struct sama5d2_piobu {
+   struct gpio_chip chip;
+   struct regmap *regmap;
+};
+
+/**
+ * sama5d2_piobu_setup_pin() - prepares a pin for set_direction call
+ *
+ * Do not consider pin for tamper detection (normal and backup modes)
+ * Do not consider pin as tamper wakeup interrupt source
+ */
+static int sama5d2_piobu_setup_pin(struct gpio_chip *chip, unsigned int pin)
+{
+   int ret;
+   struct sama5d2_piobu *piobu = container_of(chip, struct sama5d2_piobu,
+  chip);
+   unsigned int mask = BIT(PIOBU_DET_OFFSET + pin);
+
+   ret = regmap_update_bits(piobu->regmap, PIOBU_BMPR, mask, 0);
+   if (ret)
+   return ret;
+
+   ret = regmap_update_bits(piobu->regmap, PIOBU_NMPR, mask, 0);
+   if (ret)
+   return ret;
+
+   return regmap_update_bits(piobu->regmap, PIOBU_WKPR, mask, 0);
+}
+
+/**
+ * sama5d2_piobu_write_value() - writes value & mask at the pin's PIOBU 
register
+ */
+static int sama5d2_piobu_write_value(struct gpio_chip *chip, unsigned int pin,
+unsigned int mask, unsigned int value)
+{
+   int reg;
+   struct sama5d2_piobu *piobu = container_of(chip, struct sama5d2_piobu,
+  chip);
+
+   reg = PIOBU_BASE + pin * PIOBU_REG_SIZE;
+
+   return regmap_update_bits(piobu->regmap, reg,