gpio: add generic single-register fixed-direction GPIO driver
authorRussell King <rmk+kernel@armlinux.org.uk>
Wed, 31 Aug 2016 07:49:44 +0000 (08:49 +0100)
committerLinus Walleij <linus.walleij@linaro.org>
Fri, 24 Mar 2017 13:04:46 +0000 (14:04 +0100)
Add a simple, generic, single register fixed-direction GPIO driver.
This is able to support a single register with a mixture of inputs
and outputs.

This is different from gpio-mmio and gpio-74xx-mmio:
* gpio-mmio doesn't allow a fixed direction, it assumes there is always
  a direction register.
* gpio-74xx-mmio only supports all-in or all-out setups
* gpio-74xx-mmio is DT only, this needs to support legacy too
* they don't double-read when getting the GPIO value, as required by
  some implementations that this driver supports
* we need to always do 32-bit reads, which bgpio doesn't guarantee
* the current output state may not be readable from the hardware
  register - reading may reflect input status but not output status.

Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
drivers/gpio/Kconfig
drivers/gpio/Makefile
drivers/gpio/gpio-reg.c [new file with mode: 0644]
include/linux/gpio/gpio-reg.h [new file with mode: 0644]

index d6e3cfd..63ceed2 100644 (file)
@@ -380,6 +380,12 @@ config GPIO_RCAR
        help
          Say yes here to support GPIO on Renesas R-Car SoCs.
 
+config GPIO_REG
+       bool
+       help
+         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_SPEAR_SPICS
        bool "ST SPEAr13xx SPI Chip Select as GPIO support"
        depends on PLAT_SPEAR
index bd995dc..095598e 100644 (file)
@@ -98,6 +98,7 @@ obj-$(CONFIG_GPIO_PXA)                += gpio-pxa.o
 obj-$(CONFIG_GPIO_RC5T583)     += gpio-rc5t583.o
 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_SCH)         += gpio-sch.o
 obj-$(CONFIG_GPIO_SCH311X)     += gpio-sch311x.o
diff --git a/drivers/gpio/gpio-reg.c b/drivers/gpio/gpio-reg.c
new file mode 100644 (file)
index 0000000..209f736
--- /dev/null
@@ -0,0 +1,164 @@
+/*
+ * gpio-reg: single register individually fixed-direction GPIOs
+ *
+ * Copyright (C) 2016 Russell King
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ */
+#include <linux/gpio/driver.h>
+#include <linux/gpio/gpio-reg.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+
+struct gpio_reg {
+       struct gpio_chip gc;
+       spinlock_t lock;
+       u32 direction;
+       u32 out;
+       void __iomem *reg;
+};
+
+#define to_gpio_reg(x) container_of(x, struct gpio_reg, gc)
+
+static int gpio_reg_get_direction(struct gpio_chip *gc, unsigned offset)
+{
+       struct gpio_reg *r = to_gpio_reg(gc);
+
+       return r->direction & BIT(offset) ? 1 : 0;
+}
+
+static int gpio_reg_direction_output(struct gpio_chip *gc, unsigned offset,
+       int value)
+{
+       struct gpio_reg *r = to_gpio_reg(gc);
+
+       if (r->direction & BIT(offset))
+               return -ENOTSUPP;
+
+       gc->set(gc, offset, value);
+       return 0;
+}
+
+static int gpio_reg_direction_input(struct gpio_chip *gc, unsigned offset)
+{
+       struct gpio_reg *r = to_gpio_reg(gc);
+
+       return r->direction & BIT(offset) ? 0 : -ENOTSUPP;
+}
+
+static void gpio_reg_set(struct gpio_chip *gc, unsigned offset, int value)
+{
+       struct gpio_reg *r = to_gpio_reg(gc);
+       unsigned long flags;
+       u32 val, mask = BIT(offset);
+
+       spin_lock_irqsave(&r->lock, flags);
+       val = r->out;
+       if (value)
+               val |= mask;
+       else
+               val &= ~mask;
+       r->out = val;
+       writel_relaxed(val, r->reg);
+       spin_unlock_irqrestore(&r->lock, flags);
+}
+
+static int gpio_reg_get(struct gpio_chip *gc, unsigned offset)
+{
+       struct gpio_reg *r = to_gpio_reg(gc);
+       u32 val, mask = BIT(offset);
+
+       if (r->direction & mask) {
+               /*
+                * double-read the value, some registers latch after the
+                * first read.
+                */
+               readl_relaxed(r->reg);
+               val = readl_relaxed(r->reg);
+       } else {
+               val = r->out;
+       }
+       return !!(val & mask);
+}
+
+static void gpio_reg_set_multiple(struct gpio_chip *gc, unsigned long *mask,
+       unsigned long *bits)
+{
+       struct gpio_reg *r = to_gpio_reg(gc);
+       unsigned long flags;
+
+       spin_lock_irqsave(&r->lock, flags);
+       r->out = (r->out & ~*mask) | (*bits & *mask);
+       writel_relaxed(r->out, r->reg);
+       spin_unlock_irqrestore(&r->lock, flags);
+}
+
+/**
+ * gpio_reg_init - add a fixed in/out register as gpio
+ * @dev: optional struct device associated with this register
+ * @base: start gpio number, or -1 to allocate
+ * @num: number of GPIOs, maximum 32
+ * @label: GPIO chip label
+ * @direction: bitmask of fixed direction, one per GPIO signal, 1 = in
+ * @def_out: initial GPIO output value
+ * @names: array of %num strings describing each GPIO signal
+ *
+ * Add a single-register GPIO device containing up to 32 GPIO signals,
+ * where each GPIO has a fixed input or output configuration.  Only
+ * input GPIOs are assumed to be readable from the register, and only
+ * then after a double-read.  Output values are assumed not to be
+ * readable.
+ */
+struct gpio_chip *gpio_reg_init(struct device *dev, void __iomem *reg,
+       int base, int num, const char *label, u32 direction, u32 def_out,
+       const char *const *names)
+{
+       struct gpio_reg *r;
+       int ret;
+
+       if (dev)
+               r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
+       else
+               r = kzalloc(sizeof(*r), GFP_KERNEL);
+
+       if (!r)
+               return ERR_PTR(-ENOMEM);
+
+       spin_lock_init(&r->lock);
+
+       r->gc.label = label;
+       r->gc.get_direction = gpio_reg_get_direction;
+       r->gc.direction_input = gpio_reg_direction_input;
+       r->gc.direction_output = gpio_reg_direction_output;
+       r->gc.set = gpio_reg_set;
+       r->gc.get = gpio_reg_get;
+       r->gc.set_multiple = gpio_reg_set_multiple;
+       r->gc.base = base;
+       r->gc.ngpio = num;
+       r->gc.names = names;
+       r->direction = direction;
+       r->out = def_out;
+       r->reg = reg;
+
+       if (dev)
+               ret = devm_gpiochip_add_data(dev, &r->gc, r);
+       else
+               ret = gpiochip_add_data(&r->gc, r);
+
+       return ret ? ERR_PTR(ret) : &r->gc;
+}
+
+int gpio_reg_resume(struct gpio_chip *gc)
+{
+       struct gpio_reg *r = to_gpio_reg(gc);
+       unsigned long flags;
+
+       spin_lock_irqsave(&r->lock, flags);
+       writel_relaxed(r->out, r->reg);
+       spin_unlock_irqrestore(&r->lock, flags);
+
+       return 0;
+}
diff --git a/include/linux/gpio/gpio-reg.h b/include/linux/gpio/gpio-reg.h
new file mode 100644 (file)
index 0000000..0352bec
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef GPIO_REG_H
+#define GPIO_REG_H
+
+struct device;
+
+struct gpio_chip *gpio_reg_init(struct device *dev, void __iomem *reg,
+       int base, int num, const char *label, u32 direction, u32 def_out,
+       const char *const *names);
+
+int gpio_reg_resume(struct gpio_chip *gc);
+
+#endif