Skip to content

Commit 380639c

Browse files
Russell Kinglinusw
authored andcommitted
gpio: add generic single-register fixed-direction GPIO driver
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>
1 parent c65d1fd commit 380639c

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

drivers/gpio/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ config GPIO_RCAR
380380
help
381381
Say yes here to support GPIO on Renesas R-Car SoCs.
382382

383+
config GPIO_REG
384+
bool
385+
help
386+
A 32-bit single register GPIO fixed in/out implementation. This
387+
can be used to represent any register as a set of GPIO signals.
388+
383389
config GPIO_SPEAR_SPICS
384390
bool "ST SPEAr13xx SPI Chip Select as GPIO support"
385391
depends on PLAT_SPEAR

drivers/gpio/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ obj-$(CONFIG_GPIO_PXA) += gpio-pxa.o
9898
obj-$(CONFIG_GPIO_RC5T583) += gpio-rc5t583.o
9999
obj-$(CONFIG_GPIO_RDC321X) += gpio-rdc321x.o
100100
obj-$(CONFIG_GPIO_RCAR) += gpio-rcar.o
101+
obj-$(CONFIG_GPIO_REG) += gpio-reg.o
101102
obj-$(CONFIG_ARCH_SA1100) += gpio-sa1100.o
102103
obj-$(CONFIG_GPIO_SCH) += gpio-sch.o
103104
obj-$(CONFIG_GPIO_SCH311X) += gpio-sch311x.o

drivers/gpio/gpio-reg.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* gpio-reg: single register individually fixed-direction GPIOs
3+
*
4+
* Copyright (C) 2016 Russell King
5+
*
6+
* This software is licensed under the terms of the GNU General Public
7+
* License version 2, as published by the Free Software Foundation, and
8+
* may be copied, distributed, and modified under those terms.
9+
*/
10+
#include <linux/gpio/driver.h>
11+
#include <linux/gpio/gpio-reg.h>
12+
#include <linux/io.h>
13+
#include <linux/slab.h>
14+
#include <linux/spinlock.h>
15+
16+
struct gpio_reg {
17+
struct gpio_chip gc;
18+
spinlock_t lock;
19+
u32 direction;
20+
u32 out;
21+
void __iomem *reg;
22+
};
23+
24+
#define to_gpio_reg(x) container_of(x, struct gpio_reg, gc)
25+
26+
static int gpio_reg_get_direction(struct gpio_chip *gc, unsigned offset)
27+
{
28+
struct gpio_reg *r = to_gpio_reg(gc);
29+
30+
return r->direction & BIT(offset) ? 1 : 0;
31+
}
32+
33+
static int gpio_reg_direction_output(struct gpio_chip *gc, unsigned offset,
34+
int value)
35+
{
36+
struct gpio_reg *r = to_gpio_reg(gc);
37+
38+
if (r->direction & BIT(offset))
39+
return -ENOTSUPP;
40+
41+
gc->set(gc, offset, value);
42+
return 0;
43+
}
44+
45+
static int gpio_reg_direction_input(struct gpio_chip *gc, unsigned offset)
46+
{
47+
struct gpio_reg *r = to_gpio_reg(gc);
48+
49+
return r->direction & BIT(offset) ? 0 : -ENOTSUPP;
50+
}
51+
52+
static void gpio_reg_set(struct gpio_chip *gc, unsigned offset, int value)
53+
{
54+
struct gpio_reg *r = to_gpio_reg(gc);
55+
unsigned long flags;
56+
u32 val, mask = BIT(offset);
57+
58+
spin_lock_irqsave(&r->lock, flags);
59+
val = r->out;
60+
if (value)
61+
val |= mask;
62+
else
63+
val &= ~mask;
64+
r->out = val;
65+
writel_relaxed(val, r->reg);
66+
spin_unlock_irqrestore(&r->lock, flags);
67+
}
68+
69+
static int gpio_reg_get(struct gpio_chip *gc, unsigned offset)
70+
{
71+
struct gpio_reg *r = to_gpio_reg(gc);
72+
u32 val, mask = BIT(offset);
73+
74+
if (r->direction & mask) {
75+
/*
76+
* double-read the value, some registers latch after the
77+
* first read.
78+
*/
79+
readl_relaxed(r->reg);
80+
val = readl_relaxed(r->reg);
81+
} else {
82+
val = r->out;
83+
}
84+
return !!(val & mask);
85+
}
86+
87+
static void gpio_reg_set_multiple(struct gpio_chip *gc, unsigned long *mask,
88+
unsigned long *bits)
89+
{
90+
struct gpio_reg *r = to_gpio_reg(gc);
91+
unsigned long flags;
92+
93+
spin_lock_irqsave(&r->lock, flags);
94+
r->out = (r->out & ~*mask) | (*bits & *mask);
95+
writel_relaxed(r->out, r->reg);
96+
spin_unlock_irqrestore(&r->lock, flags);
97+
}
98+
99+
/**
100+
* gpio_reg_init - add a fixed in/out register as gpio
101+
* @dev: optional struct device associated with this register
102+
* @base: start gpio number, or -1 to allocate
103+
* @num: number of GPIOs, maximum 32
104+
* @label: GPIO chip label
105+
* @direction: bitmask of fixed direction, one per GPIO signal, 1 = in
106+
* @def_out: initial GPIO output value
107+
* @names: array of %num strings describing each GPIO signal
108+
*
109+
* Add a single-register GPIO device containing up to 32 GPIO signals,
110+
* where each GPIO has a fixed input or output configuration. Only
111+
* input GPIOs are assumed to be readable from the register, and only
112+
* then after a double-read. Output values are assumed not to be
113+
* readable.
114+
*/
115+
struct gpio_chip *gpio_reg_init(struct device *dev, void __iomem *reg,
116+
int base, int num, const char *label, u32 direction, u32 def_out,
117+
const char *const *names)
118+
{
119+
struct gpio_reg *r;
120+
int ret;
121+
122+
if (dev)
123+
r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
124+
else
125+
r = kzalloc(sizeof(*r), GFP_KERNEL);
126+
127+
if (!r)
128+
return ERR_PTR(-ENOMEM);
129+
130+
spin_lock_init(&r->lock);
131+
132+
r->gc.label = label;
133+
r->gc.get_direction = gpio_reg_get_direction;
134+
r->gc.direction_input = gpio_reg_direction_input;
135+
r->gc.direction_output = gpio_reg_direction_output;
136+
r->gc.set = gpio_reg_set;
137+
r->gc.get = gpio_reg_get;
138+
r->gc.set_multiple = gpio_reg_set_multiple;
139+
r->gc.base = base;
140+
r->gc.ngpio = num;
141+
r->gc.names = names;
142+
r->direction = direction;
143+
r->out = def_out;
144+
r->reg = reg;
145+
146+
if (dev)
147+
ret = devm_gpiochip_add_data(dev, &r->gc, r);
148+
else
149+
ret = gpiochip_add_data(&r->gc, r);
150+
151+
return ret ? ERR_PTR(ret) : &r->gc;
152+
}
153+
154+
int gpio_reg_resume(struct gpio_chip *gc)
155+
{
156+
struct gpio_reg *r = to_gpio_reg(gc);
157+
unsigned long flags;
158+
159+
spin_lock_irqsave(&r->lock, flags);
160+
writel_relaxed(r->out, r->reg);
161+
spin_unlock_irqrestore(&r->lock, flags);
162+
163+
return 0;
164+
}

include/linux/gpio/gpio-reg.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef GPIO_REG_H
2+
#define GPIO_REG_H
3+
4+
struct device;
5+
6+
struct gpio_chip *gpio_reg_init(struct device *dev, void __iomem *reg,
7+
int base, int num, const char *label, u32 direction, u32 def_out,
8+
const char *const *names);
9+
10+
int gpio_reg_resume(struct gpio_chip *gc);
11+
12+
#endif

0 commit comments

Comments
 (0)