blob: 8c1ab8e1974fc2d5480e6c62057ebcd389786699 [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;
Andreas Wernerf436bc22016-02-29 19:44:43 +010038 struct resource *mem;
39 spinlock_t lock;
40};
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);
Axel Lin86d3f362016-03-09 20:39:57 +080046 struct device *dev = gc->parent;
Andreas Wernerf436bc22016-02-29 19:44:43 +010047 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
71 spin_lock(&priv->lock);
72
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
86 spin_unlock(&priv->lock);
87
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
99 spin_lock(&priv->lock);
100 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);
108 spin_unlock(&priv->lock);
109
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
Andreas Wernerf436bc22016-02-29 19:44:43 +0100138 mcb_set_drvdata(mdev, men_z127_gpio);
139
140 ret = bgpio_init(&men_z127_gpio->gc, &mdev->dev, 4,
141 men_z127_gpio->reg_base + MEN_Z127_PSR,
142 men_z127_gpio->reg_base + MEN_Z127_CTRL,
143 NULL,
144 men_z127_gpio->reg_base + MEN_Z127_GPIODR,
145 NULL, 0);
146 if (ret)
147 goto err_unmap;
148
149 men_z127_gpio->gc.set_debounce = men_z127_debounce;
150 men_z127_gpio->gc.request = men_z127_request;
151
152 ret = gpiochip_add_data(&men_z127_gpio->gc, men_z127_gpio);
153 if (ret) {
154 dev_err(dev, "failed to register MEN 16Z127 GPIO controller");
155 goto err_unmap;
156 }
157
158 dev_info(dev, "MEN 16Z127 GPIO driver registered");
159
160 return 0;
161
162err_unmap:
163 iounmap(men_z127_gpio->reg_base);
164err_release:
165 mcb_release_mem(men_z127_gpio->mem);
166 return ret;
167}
168
169static void men_z127_remove(struct mcb_device *mdev)
170{
171 struct men_z127_gpio *men_z127_gpio = mcb_get_drvdata(mdev);
172
173 gpiochip_remove(&men_z127_gpio->gc);
174 iounmap(men_z127_gpio->reg_base);
175 mcb_release_mem(men_z127_gpio->mem);
176}
177
178static const struct mcb_device_id men_z127_ids[] = {
179 { .device = 0x7f },
180 { }
181};
182MODULE_DEVICE_TABLE(mcb, men_z127_ids);
183
184static struct mcb_driver men_z127_driver = {
185 .driver = {
186 .name = "z127-gpio",
187 .owner = THIS_MODULE,
188 },
189 .probe = men_z127_probe,
190 .remove = men_z127_remove,
191 .id_table = men_z127_ids,
192};
193module_mcb_driver(men_z127_driver);
194
195MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
196MODULE_DESCRIPTION("MEN 16z127 GPIO Controller");
197MODULE_LICENSE("GPL v2");
198MODULE_ALIAS("mcb:16z127");