blob: 8637adb6bc205c16c6ed3768f5ae9d6cd39e0b1a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jun Niee7aa6d82015-06-29 10:35:57 +08002/*
Paul Gortmaker18fb0a92016-03-27 11:44:49 -04003 * ZTE ZX296702 GPIO driver
4 *
5 * Author: Jun Nie <jun.nie@linaro.org>
6 *
Jun Niee7aa6d82015-06-29 10:35:57 +08007 * Copyright (C) 2015 Linaro Ltd.
Jun Niee7aa6d82015-06-29 10:35:57 +08008 */
9#include <linux/bitops.h>
10#include <linux/device.h>
11#include <linux/errno.h>
12#include <linux/gpio/driver.h>
13#include <linux/irqchip/chained_irq.h>
Paul Gortmaker18fb0a92016-03-27 11:44:49 -040014#include <linux/init.h>
Jun Niee7aa6d82015-06-29 10:35:57 +080015#include <linux/of.h>
16#include <linux/pinctrl/consumer.h>
17#include <linux/platform_device.h>
18#include <linux/pm.h>
19#include <linux/slab.h>
20#include <linux/spinlock.h>
21
22#define ZX_GPIO_DIR 0x00
23#define ZX_GPIO_IVE 0x04
24#define ZX_GPIO_IV 0x08
25#define ZX_GPIO_IEP 0x0C
26#define ZX_GPIO_IEN 0x10
27#define ZX_GPIO_DI 0x14
28#define ZX_GPIO_DO1 0x18
29#define ZX_GPIO_DO0 0x1C
30#define ZX_GPIO_DO 0x20
31
32#define ZX_GPIO_IM 0x28
33#define ZX_GPIO_IE 0x2C
34
35#define ZX_GPIO_MIS 0x30
36#define ZX_GPIO_IC 0x34
37
38#define ZX_GPIO_NR 16
39
40struct zx_gpio {
Julia Cartwright8c41ab42017-03-09 10:21:58 -060041 raw_spinlock_t lock;
Jun Niee7aa6d82015-06-29 10:35:57 +080042
43 void __iomem *base;
44 struct gpio_chip gc;
Jun Niee7aa6d82015-06-29 10:35:57 +080045};
46
Jun Niee7aa6d82015-06-29 10:35:57 +080047static int zx_direction_input(struct gpio_chip *gc, unsigned offset)
48{
Linus Walleij17758b02015-12-07 15:27:49 +010049 struct zx_gpio *chip = gpiochip_get_data(gc);
Jun Niee7aa6d82015-06-29 10:35:57 +080050 unsigned long flags;
51 u16 gpiodir;
52
53 if (offset >= gc->ngpio)
54 return -EINVAL;
55
Julia Cartwright8c41ab42017-03-09 10:21:58 -060056 raw_spin_lock_irqsave(&chip->lock, flags);
Jun Niee7aa6d82015-06-29 10:35:57 +080057 gpiodir = readw_relaxed(chip->base + ZX_GPIO_DIR);
58 gpiodir &= ~BIT(offset);
59 writew_relaxed(gpiodir, chip->base + ZX_GPIO_DIR);
Julia Cartwright8c41ab42017-03-09 10:21:58 -060060 raw_spin_unlock_irqrestore(&chip->lock, flags);
Jun Niee7aa6d82015-06-29 10:35:57 +080061
62 return 0;
63}
64
65static int zx_direction_output(struct gpio_chip *gc, unsigned offset,
66 int value)
67{
Linus Walleij17758b02015-12-07 15:27:49 +010068 struct zx_gpio *chip = gpiochip_get_data(gc);
Jun Niee7aa6d82015-06-29 10:35:57 +080069 unsigned long flags;
70 u16 gpiodir;
71
72 if (offset >= gc->ngpio)
73 return -EINVAL;
74
Julia Cartwright8c41ab42017-03-09 10:21:58 -060075 raw_spin_lock_irqsave(&chip->lock, flags);
Jun Niee7aa6d82015-06-29 10:35:57 +080076 gpiodir = readw_relaxed(chip->base + ZX_GPIO_DIR);
77 gpiodir |= BIT(offset);
78 writew_relaxed(gpiodir, chip->base + ZX_GPIO_DIR);
79
80 if (value)
81 writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO1);
82 else
83 writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO0);
Julia Cartwright8c41ab42017-03-09 10:21:58 -060084 raw_spin_unlock_irqrestore(&chip->lock, flags);
Jun Niee7aa6d82015-06-29 10:35:57 +080085
86 return 0;
87}
88
89static int zx_get_value(struct gpio_chip *gc, unsigned offset)
90{
Linus Walleij17758b02015-12-07 15:27:49 +010091 struct zx_gpio *chip = gpiochip_get_data(gc);
Jun Niee7aa6d82015-06-29 10:35:57 +080092
93 return !!(readw_relaxed(chip->base + ZX_GPIO_DI) & BIT(offset));
94}
95
96static void zx_set_value(struct gpio_chip *gc, unsigned offset, int value)
97{
Linus Walleij17758b02015-12-07 15:27:49 +010098 struct zx_gpio *chip = gpiochip_get_data(gc);
Jun Niee7aa6d82015-06-29 10:35:57 +080099
100 if (value)
101 writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO1);
102 else
103 writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO0);
104}
105
106static int zx_irq_type(struct irq_data *d, unsigned trigger)
107{
108 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij17758b02015-12-07 15:27:49 +0100109 struct zx_gpio *chip = gpiochip_get_data(gc);
Jun Niee7aa6d82015-06-29 10:35:57 +0800110 int offset = irqd_to_hwirq(d);
111 unsigned long flags;
112 u16 gpiois, gpioi_epos, gpioi_eneg, gpioiev;
113 u16 bit = BIT(offset);
114
115 if (offset < 0 || offset >= ZX_GPIO_NR)
116 return -EINVAL;
117
Julia Cartwright8c41ab42017-03-09 10:21:58 -0600118 raw_spin_lock_irqsave(&chip->lock, flags);
Jun Niee7aa6d82015-06-29 10:35:57 +0800119
120 gpioiev = readw_relaxed(chip->base + ZX_GPIO_IV);
121 gpiois = readw_relaxed(chip->base + ZX_GPIO_IVE);
122 gpioi_epos = readw_relaxed(chip->base + ZX_GPIO_IEP);
123 gpioi_eneg = readw_relaxed(chip->base + ZX_GPIO_IEN);
124
125 if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
126 gpiois |= bit;
127 if (trigger & IRQ_TYPE_LEVEL_HIGH)
128 gpioiev |= bit;
129 else
130 gpioiev &= ~bit;
131 } else
132 gpiois &= ~bit;
133
134 if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
135 gpioi_epos |= bit;
136 gpioi_eneg |= bit;
137 } else {
138 if (trigger & IRQ_TYPE_EDGE_RISING) {
139 gpioi_epos |= bit;
140 gpioi_eneg &= ~bit;
141 } else if (trigger & IRQ_TYPE_EDGE_FALLING) {
142 gpioi_eneg |= bit;
143 gpioi_epos &= ~bit;
144 }
145 }
146
147 writew_relaxed(gpiois, chip->base + ZX_GPIO_IVE);
148 writew_relaxed(gpioi_epos, chip->base + ZX_GPIO_IEP);
149 writew_relaxed(gpioi_eneg, chip->base + ZX_GPIO_IEN);
150 writew_relaxed(gpioiev, chip->base + ZX_GPIO_IV);
Julia Cartwright8c41ab42017-03-09 10:21:58 -0600151 raw_spin_unlock_irqrestore(&chip->lock, flags);
Jun Niee7aa6d82015-06-29 10:35:57 +0800152
153 return 0;
154}
155
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200156static void zx_irq_handler(struct irq_desc *desc)
Jun Niee7aa6d82015-06-29 10:35:57 +0800157{
158 unsigned long pending;
159 int offset;
160 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
Linus Walleij17758b02015-12-07 15:27:49 +0100161 struct zx_gpio *chip = gpiochip_get_data(gc);
Jun Niee7aa6d82015-06-29 10:35:57 +0800162 struct irq_chip *irqchip = irq_desc_get_chip(desc);
163
164 chained_irq_enter(irqchip, desc);
165
166 pending = readw_relaxed(chip->base + ZX_GPIO_MIS);
167 writew_relaxed(pending, chip->base + ZX_GPIO_IC);
168 if (pending) {
169 for_each_set_bit(offset, &pending, ZX_GPIO_NR)
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100170 generic_handle_irq(irq_find_mapping(gc->irq.domain,
Jun Niee7aa6d82015-06-29 10:35:57 +0800171 offset));
172 }
173
174 chained_irq_exit(irqchip, desc);
175}
176
177static void zx_irq_mask(struct irq_data *d)
178{
179 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij17758b02015-12-07 15:27:49 +0100180 struct zx_gpio *chip = gpiochip_get_data(gc);
Jun Niee7aa6d82015-06-29 10:35:57 +0800181 u16 mask = BIT(irqd_to_hwirq(d) % ZX_GPIO_NR);
182 u16 gpioie;
183
Julia Cartwright8c41ab42017-03-09 10:21:58 -0600184 raw_spin_lock(&chip->lock);
Jun Niee7aa6d82015-06-29 10:35:57 +0800185 gpioie = readw_relaxed(chip->base + ZX_GPIO_IM) | mask;
186 writew_relaxed(gpioie, chip->base + ZX_GPIO_IM);
187 gpioie = readw_relaxed(chip->base + ZX_GPIO_IE) & ~mask;
188 writew_relaxed(gpioie, chip->base + ZX_GPIO_IE);
Julia Cartwright8c41ab42017-03-09 10:21:58 -0600189 raw_spin_unlock(&chip->lock);
Jun Niee7aa6d82015-06-29 10:35:57 +0800190}
191
192static void zx_irq_unmask(struct irq_data *d)
193{
194 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij17758b02015-12-07 15:27:49 +0100195 struct zx_gpio *chip = gpiochip_get_data(gc);
Jun Niee7aa6d82015-06-29 10:35:57 +0800196 u16 mask = BIT(irqd_to_hwirq(d) % ZX_GPIO_NR);
197 u16 gpioie;
198
Julia Cartwright8c41ab42017-03-09 10:21:58 -0600199 raw_spin_lock(&chip->lock);
Jun Niee7aa6d82015-06-29 10:35:57 +0800200 gpioie = readw_relaxed(chip->base + ZX_GPIO_IM) & ~mask;
201 writew_relaxed(gpioie, chip->base + ZX_GPIO_IM);
202 gpioie = readw_relaxed(chip->base + ZX_GPIO_IE) | mask;
203 writew_relaxed(gpioie, chip->base + ZX_GPIO_IE);
Julia Cartwright8c41ab42017-03-09 10:21:58 -0600204 raw_spin_unlock(&chip->lock);
Jun Niee7aa6d82015-06-29 10:35:57 +0800205}
206
207static struct irq_chip zx_irqchip = {
208 .name = "zx-gpio",
209 .irq_mask = zx_irq_mask,
210 .irq_unmask = zx_irq_unmask,
211 .irq_set_type = zx_irq_type,
212};
213
214static int zx_gpio_probe(struct platform_device *pdev)
215{
216 struct device *dev = &pdev->dev;
217 struct zx_gpio *chip;
Jun Niee7aa6d82015-06-29 10:35:57 +0800218 int irq, id, ret;
219
220 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
221 if (!chip)
222 return -ENOMEM;
223
Enrico Weigelt, metux IT consult8d86f982019-03-11 19:55:19 +0100224 chip->base = devm_platform_ioremap_resource(pdev, 0);
Jun Niee7aa6d82015-06-29 10:35:57 +0800225 if (IS_ERR(chip->base))
226 return PTR_ERR(chip->base);
227
Julia Cartwright8c41ab42017-03-09 10:21:58 -0600228 raw_spin_lock_init(&chip->lock);
Jonas Gorskida4002e2015-10-11 17:34:17 +0200229 if (of_property_read_bool(dev->of_node, "gpio-ranges")) {
230 chip->gc.request = gpiochip_generic_request;
231 chip->gc.free = gpiochip_generic_free;
232 }
Jun Niee7aa6d82015-06-29 10:35:57 +0800233
234 id = of_alias_get_id(dev->of_node, "gpio");
Jun Niee7aa6d82015-06-29 10:35:57 +0800235 chip->gc.direction_input = zx_direction_input;
236 chip->gc.direction_output = zx_direction_output;
237 chip->gc.get = zx_get_value;
238 chip->gc.set = zx_set_value;
239 chip->gc.base = ZX_GPIO_NR * id;
240 chip->gc.ngpio = ZX_GPIO_NR;
241 chip->gc.label = dev_name(dev);
Linus Walleij58383c782015-11-04 09:56:26 +0100242 chip->gc.parent = dev;
Jun Niee7aa6d82015-06-29 10:35:57 +0800243 chip->gc.owner = THIS_MODULE;
244
Linus Walleij17758b02015-12-07 15:27:49 +0100245 ret = gpiochip_add_data(&chip->gc, chip);
Jun Niee7aa6d82015-06-29 10:35:57 +0800246 if (ret)
247 return ret;
248
249 /*
250 * irq_chip support
251 */
252 writew_relaxed(0xffff, chip->base + ZX_GPIO_IM);
253 writew_relaxed(0, chip->base + ZX_GPIO_IE);
254 irq = platform_get_irq(pdev, 0);
255 if (irq < 0) {
256 dev_err(dev, "invalid IRQ\n");
257 gpiochip_remove(&chip->gc);
258 return -ENODEV;
259 }
260
261 ret = gpiochip_irqchip_add(&chip->gc, &zx_irqchip,
262 0, handle_simple_irq,
263 IRQ_TYPE_NONE);
264 if (ret) {
265 dev_err(dev, "could not add irqchip\n");
266 gpiochip_remove(&chip->gc);
267 return ret;
268 }
269 gpiochip_set_chained_irqchip(&chip->gc, &zx_irqchip,
270 irq, zx_irq_handler);
271
272 platform_set_drvdata(pdev, chip);
273 dev_info(dev, "ZX GPIO chip registered\n");
274
275 return 0;
276}
277
278static const struct of_device_id zx_gpio_match[] = {
279 {
280 .compatible = "zte,zx296702-gpio",
281 },
282 { },
283};
Jun Niee7aa6d82015-06-29 10:35:57 +0800284
285static struct platform_driver zx_gpio_driver = {
286 .probe = zx_gpio_probe,
287 .driver = {
288 .name = "zx_gpio",
289 .of_match_table = of_match_ptr(zx_gpio_match),
290 },
291};
Paul Gortmaker18fb0a92016-03-27 11:44:49 -0400292builtin_platform_driver(zx_gpio_driver)