Skip to content

Commit f436bc2

Browse files
Andreas Wernerlinusw
authored andcommitted
gpio: add driver for MEN 16Z127 GPIO controller
The 16Z127 is a 32bit GPIO controller on a MCB FPGA. Every single line can be configured as input and output. Push pull and open drain are supported as well as setting a debounce value for the input lines. Signed-off-by: Andreas Werner <andy@wernerandy.de> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 3fab91e commit f436bc2

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

drivers/gpio/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ config GPIO_MB86S7X
270270
help
271271
Say yes here to support the GPIO controller in Fujitsu MB86S70 SoCs.
272272

273+
config GPIO_MENZ127
274+
tristate "MEN 16Z127 GPIO support"
275+
depends on MCB
276+
select GPIO_GENERIC
277+
help
278+
Say yes here to support the MEN 16Z127 GPIO Controller
279+
273280
config GPIO_MM_LANTIQ
274281
bool "Lantiq Memory mapped GPIOs"
275282
depends on LANTIQ && SOC_XWAY

drivers/gpio/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ obj-$(CONFIG_GPIO_MAX7300) += gpio-max7300.o
5959
obj-$(CONFIG_GPIO_MAX7301) += gpio-max7301.o
6060
obj-$(CONFIG_GPIO_MAX732X) += gpio-max732x.o
6161
obj-$(CONFIG_GPIO_MB86S7X) += gpio-mb86s7x.o
62+
obj-$(CONFIG_GPIO_MENZ127) += gpio-menz127.o
6263
obj-$(CONFIG_GPIO_MC33880) += gpio-mc33880.o
6364
obj-$(CONFIG_GPIO_MC9S08DZ60) += gpio-mc9s08dz60.o
6465
obj-$(CONFIG_GPIO_MCP23S08) += gpio-mcp23s08.o

drivers/gpio/gpio-menz127.c

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* MEN 16Z127 GPIO driver
3+
*
4+
* Copyright (C) 2016 MEN Mikroelektronik GmbH (www.men.de)
5+
*
6+
* This program is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the Free
8+
* Software Foundation; version 2 of the License.
9+
*/
10+
11+
#include <linux/kernel.h>
12+
#include <linux/module.h>
13+
#include <linux/io.h>
14+
#include <linux/err.h>
15+
#include <linux/mcb.h>
16+
#include <linux/bitops.h>
17+
#include <linux/gpio/driver.h>
18+
19+
#define MEN_Z127_CTRL 0x00
20+
#define MEN_Z127_PSR 0x04
21+
#define MEN_Z127_IRQR 0x08
22+
#define MEN_Z127_GPIODR 0x0c
23+
#define MEN_Z127_IER1 0x10
24+
#define MEN_Z127_IER2 0x14
25+
#define MEN_Z127_DBER 0x18
26+
#define MEN_Z127_ODER 0x1C
27+
#define GPIO_TO_DBCNT_REG(gpio) ((gpio * 4) + 0x80)
28+
29+
#define MEN_Z127_DB_MIN_US 50
30+
/* 16 bit compare register. Each bit represents 50us */
31+
#define MEN_Z127_DB_MAX_US (0xffff * MEN_Z127_DB_MIN_US)
32+
#define MEN_Z127_DB_IN_RANGE(db) ((db >= MEN_Z127_DB_MIN_US) && \
33+
(db <= MEN_Z127_DB_MAX_US))
34+
35+
struct men_z127_gpio {
36+
struct gpio_chip gc;
37+
void __iomem *reg_base;
38+
struct mcb_device *mdev;
39+
struct resource *mem;
40+
spinlock_t lock;
41+
};
42+
43+
static int men_z127_debounce(struct gpio_chip *gc, unsigned gpio,
44+
unsigned debounce)
45+
{
46+
struct men_z127_gpio *priv = gpiochip_get_data(gc);
47+
struct device *dev = &priv->mdev->dev;
48+
unsigned int rnd;
49+
u32 db_en, db_cnt;
50+
51+
if (!MEN_Z127_DB_IN_RANGE(debounce)) {
52+
dev_err(dev, "debounce value %u out of range", debounce);
53+
return -EINVAL;
54+
}
55+
56+
if (debounce > 0) {
57+
/* round up or down depending on MSB-1 */
58+
rnd = fls(debounce) - 1;
59+
60+
if (rnd && (debounce & BIT(rnd - 1)))
61+
debounce = round_up(debounce, MEN_Z127_DB_MIN_US);
62+
else
63+
debounce = round_down(debounce, MEN_Z127_DB_MIN_US);
64+
65+
if (debounce > MEN_Z127_DB_MAX_US)
66+
debounce = MEN_Z127_DB_MAX_US;
67+
68+
/* 50us per register unit */
69+
debounce /= 50;
70+
}
71+
72+
spin_lock(&priv->lock);
73+
74+
db_en = readl(priv->reg_base + MEN_Z127_DBER);
75+
76+
if (debounce == 0) {
77+
db_en &= ~BIT(gpio);
78+
db_cnt = 0;
79+
} else {
80+
db_en |= BIT(gpio);
81+
db_cnt = debounce;
82+
}
83+
84+
writel(db_en, priv->reg_base + MEN_Z127_DBER);
85+
writel(db_cnt, priv->reg_base + GPIO_TO_DBCNT_REG(gpio));
86+
87+
spin_unlock(&priv->lock);
88+
89+
return 0;
90+
}
91+
92+
static int men_z127_request(struct gpio_chip *gc, unsigned gpio_pin)
93+
{
94+
struct men_z127_gpio *priv = gpiochip_get_data(gc);
95+
u32 od_en;
96+
97+
if (gpio_pin >= gc->ngpio)
98+
return -EINVAL;
99+
100+
spin_lock(&priv->lock);
101+
od_en = readl(priv->reg_base + MEN_Z127_ODER);
102+
103+
if (gpiochip_line_is_open_drain(gc, gpio_pin))
104+
od_en |= BIT(gpio_pin);
105+
else
106+
od_en &= ~BIT(gpio_pin);
107+
108+
writel(od_en, priv->reg_base + MEN_Z127_ODER);
109+
spin_unlock(&priv->lock);
110+
111+
return 0;
112+
}
113+
114+
static int men_z127_probe(struct mcb_device *mdev,
115+
const struct mcb_device_id *id)
116+
{
117+
struct men_z127_gpio *men_z127_gpio;
118+
struct device *dev = &mdev->dev;
119+
int ret;
120+
121+
men_z127_gpio = devm_kzalloc(dev, sizeof(struct men_z127_gpio),
122+
GFP_KERNEL);
123+
if (!men_z127_gpio)
124+
return -ENOMEM;
125+
126+
men_z127_gpio->mem = mcb_request_mem(mdev, dev_name(dev));
127+
if (IS_ERR(men_z127_gpio->mem)) {
128+
dev_err(dev, "failed to request device memory");
129+
return PTR_ERR(men_z127_gpio->mem);
130+
}
131+
132+
men_z127_gpio->reg_base = ioremap(men_z127_gpio->mem->start,
133+
resource_size(men_z127_gpio->mem));
134+
if (men_z127_gpio->reg_base == NULL) {
135+
ret = -ENXIO;
136+
goto err_release;
137+
}
138+
139+
men_z127_gpio->mdev = mdev;
140+
mcb_set_drvdata(mdev, men_z127_gpio);
141+
142+
ret = bgpio_init(&men_z127_gpio->gc, &mdev->dev, 4,
143+
men_z127_gpio->reg_base + MEN_Z127_PSR,
144+
men_z127_gpio->reg_base + MEN_Z127_CTRL,
145+
NULL,
146+
men_z127_gpio->reg_base + MEN_Z127_GPIODR,
147+
NULL, 0);
148+
if (ret)
149+
goto err_unmap;
150+
151+
men_z127_gpio->gc.set_debounce = men_z127_debounce;
152+
men_z127_gpio->gc.request = men_z127_request;
153+
154+
ret = gpiochip_add_data(&men_z127_gpio->gc, men_z127_gpio);
155+
if (ret) {
156+
dev_err(dev, "failed to register MEN 16Z127 GPIO controller");
157+
goto err_unmap;
158+
}
159+
160+
dev_info(dev, "MEN 16Z127 GPIO driver registered");
161+
162+
return 0;
163+
164+
err_unmap:
165+
iounmap(men_z127_gpio->reg_base);
166+
err_release:
167+
mcb_release_mem(men_z127_gpio->mem);
168+
return ret;
169+
}
170+
171+
static void men_z127_remove(struct mcb_device *mdev)
172+
{
173+
struct men_z127_gpio *men_z127_gpio = mcb_get_drvdata(mdev);
174+
175+
gpiochip_remove(&men_z127_gpio->gc);
176+
iounmap(men_z127_gpio->reg_base);
177+
mcb_release_mem(men_z127_gpio->mem);
178+
}
179+
180+
static const struct mcb_device_id men_z127_ids[] = {
181+
{ .device = 0x7f },
182+
{ }
183+
};
184+
MODULE_DEVICE_TABLE(mcb, men_z127_ids);
185+
186+
static struct mcb_driver men_z127_driver = {
187+
.driver = {
188+
.name = "z127-gpio",
189+
.owner = THIS_MODULE,
190+
},
191+
.probe = men_z127_probe,
192+
.remove = men_z127_remove,
193+
.id_table = men_z127_ids,
194+
};
195+
module_mcb_driver(men_z127_driver);
196+
197+
MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
198+
MODULE_DESCRIPTION("MEN 16z127 GPIO Controller");
199+
MODULE_LICENSE("GPL v2");
200+
MODULE_ALIAS("mcb:16z127");

0 commit comments

Comments
 (0)