blob: c5c9599a3a71b3182d2e269fa01173b6cdf570cc [file] [log] [blame]
Andreas Wernerf436bc22016-02-29 19:44:43 +01001/*
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
35struct men_z127_gpio {
36 struct gpio_chip gc;
37 void __iomem *reg_base;
38 struct mcb_device *mdev;
39 struct resource *mem;
Andreas Wernerf436bc22016-02-29 19:44:43 +010040};
41
42static int men_z127_debounce(struct gpio_chip *gc, unsigned gpio,
43 unsigned debounce)
44{
45 struct men_z127_gpio *priv = gpiochip_get_data(gc);
46 struct device *dev = &priv->mdev->dev;
47 unsigned int rnd;
48 u32 db_en, db_cnt;
49
50 if (!MEN_Z127_DB_IN_RANGE(debounce)) {
51 dev_err(dev, "debounce value %u out of range", debounce);
52 return -EINVAL;
53 }
54
55 if (debounce > 0) {
56 /* round up or down depending on MSB-1 */
57 rnd = fls(debounce) - 1;
58
59 if (rnd && (debounce & BIT(rnd - 1)))
60 debounce = round_up(debounce, MEN_Z127_DB_MIN_US);
61 else
62 debounce = round_down(debounce, MEN_Z127_DB_MIN_US);
63
64 if (debounce > MEN_Z127_DB_MAX_US)
65 debounce = MEN_Z127_DB_MAX_US;
66
67 /* 50us per register unit */
68 debounce /= 50;
69 }
70
Axel Linfd975a72016-03-09 20:38:55 +080071 spin_lock(&gc->bgpio_lock);
Andreas Wernerf436bc22016-02-29 19:44:43 +010072
73 db_en = readl(priv->reg_base + MEN_Z127_DBER);
74
75 if (debounce == 0) {
76 db_en &= ~BIT(gpio);
77 db_cnt = 0;
78 } else {
79 db_en |= BIT(gpio);
80 db_cnt = debounce;
81 }
82
83 writel(db_en, priv->reg_base + MEN_Z127_DBER);
84 writel(db_cnt, priv->reg_base + GPIO_TO_DBCNT_REG(gpio));
85
Axel Linfd975a72016-03-09 20:38:55 +080086 spin_unlock(&gc->bgpio_lock);
Andreas Wernerf436bc22016-02-29 19:44:43 +010087
88 return 0;
89}
90
91static int men_z127_request(struct gpio_chip *gc, unsigned gpio_pin)
92{
93 struct men_z127_gpio *priv = gpiochip_get_data(gc);
94 u32 od_en;
95
96 if (gpio_pin >= gc->ngpio)
97 return -EINVAL;
98
Axel Linfd975a72016-03-09 20:38:55 +080099 spin_lock(&gc->bgpio_lock);
Andreas Wernerf436bc22016-02-29 19:44:43 +0100100 od_en = readl(priv->reg_base + MEN_Z127_ODER);
101
102 if (gpiochip_line_is_open_drain(gc, gpio_pin))
103 od_en |= BIT(gpio_pin);
104 else
105 od_en &= ~BIT(gpio_pin);
106
107 writel(od_en, priv->reg_base + MEN_Z127_ODER);
Axel Linfd975a72016-03-09 20:38:55 +0800108 spin_unlock(&gc->bgpio_lock);
Andreas Wernerf436bc22016-02-29 19:44:43 +0100109
110 return 0;
111}
112
113static int men_z127_probe(struct mcb_device *mdev,
114 const struct mcb_device_id *id)
115{
116 struct men_z127_gpio *men_z127_gpio;
117 struct device *dev = &mdev->dev;
118 int ret;
119
120 men_z127_gpio = devm_kzalloc(dev, sizeof(struct men_z127_gpio),
121 GFP_KERNEL);
122 if (!men_z127_gpio)
123 return -ENOMEM;
124
125 men_z127_gpio->mem = mcb_request_mem(mdev, dev_name(dev));
126 if (IS_ERR(men_z127_gpio->mem)) {
127 dev_err(dev, "failed to request device memory");
128 return PTR_ERR(men_z127_gpio->mem);
129 }
130
131 men_z127_gpio->reg_base = ioremap(men_z127_gpio->mem->start,
132 resource_size(men_z127_gpio->mem));
133 if (men_z127_gpio->reg_base == NULL) {
134 ret = -ENXIO;
135 goto err_release;
136 }
137
138 men_z127_gpio->mdev = mdev;
139 mcb_set_drvdata(mdev, men_z127_gpio);
140
141 ret = bgpio_init(&men_z127_gpio->gc, &mdev->dev, 4,
142 men_z127_gpio->reg_base + MEN_Z127_PSR,
143 men_z127_gpio->reg_base + MEN_Z127_CTRL,
144 NULL,
145 men_z127_gpio->reg_base + MEN_Z127_GPIODR,
146 NULL, 0);
147 if (ret)
148 goto err_unmap;
149
150 men_z127_gpio->gc.set_debounce = men_z127_debounce;
151 men_z127_gpio->gc.request = men_z127_request;
152
153 ret = gpiochip_add_data(&men_z127_gpio->gc, men_z127_gpio);
154 if (ret) {
155 dev_err(dev, "failed to register MEN 16Z127 GPIO controller");
156 goto err_unmap;
157 }
158
159 dev_info(dev, "MEN 16Z127 GPIO driver registered");
160
161 return 0;
162
163err_unmap:
164 iounmap(men_z127_gpio->reg_base);
165err_release:
166 mcb_release_mem(men_z127_gpio->mem);
167 return ret;
168}
169
170static void men_z127_remove(struct mcb_device *mdev)
171{
172 struct men_z127_gpio *men_z127_gpio = mcb_get_drvdata(mdev);
173
174 gpiochip_remove(&men_z127_gpio->gc);
175 iounmap(men_z127_gpio->reg_base);
176 mcb_release_mem(men_z127_gpio->mem);
177}
178
179static const struct mcb_device_id men_z127_ids[] = {
180 { .device = 0x7f },
181 { }
182};
183MODULE_DEVICE_TABLE(mcb, men_z127_ids);
184
185static struct mcb_driver men_z127_driver = {
186 .driver = {
187 .name = "z127-gpio",
188 .owner = THIS_MODULE,
189 },
190 .probe = men_z127_probe,
191 .remove = men_z127_remove,
192 .id_table = men_z127_ids,
193};
194module_mcb_driver(men_z127_driver);
195
196MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
197MODULE_DESCRIPTION("MEN 16z127 GPIO Controller");
198MODULE_LICENSE("GPL v2");
199MODULE_ALIAS("mcb:16z127");