9e16aa3c4d93eb2c546b3b0a66e41394b59020f9
[librecmc/librecmc.git] / target / linux / ramips / patches-4.3 / 0028-GPIO-ralink-add-mt7621-gpio-controller.patch
1 From 61ac7d9b4228de8c332900902c2b93189b042eab Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 27 Jul 2014 11:00:32 +0100
4 Subject: [PATCH 28/53] GPIO: ralink: add mt7621 gpio controller
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 ---
8  arch/mips/Kconfig          |    3 +
9  drivers/gpio/Kconfig       |    6 +
10  drivers/gpio/Makefile      |    1 +
11  drivers/gpio/gpio-mt7621.c |  354 ++++++++++++++++++++++++++++++++++++++++++++
12  4 files changed, 364 insertions(+)
13  create mode 100644 drivers/gpio/gpio-mt7621.c
14
15 diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
16 index 0098bff..94ea345 100644
17 --- a/arch/mips/Kconfig
18 +++ b/arch/mips/Kconfig
19 @@ -559,6 +559,9 @@ config RALINK
20         select RESET_CONTROLLER
21         select PINCTRL
22         select PINCTRL_RT2880
23 +       select ARCH_HAS_RESET_CONTROLLER
24 +       select RESET_CONTROLLER
25 +       select ARCH_REQUIRE_GPIOLIB
26  
27  config SGI_IP22
28         bool "SGI IP22 (Indy/Indigo2)"
29 diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
30 index 4a3e7df..13f860c 100644
31 --- a/drivers/gpio/Kconfig
32 +++ b/drivers/gpio/Kconfig
33 @@ -269,6 +269,12 @@ config GPIO_MB86S7X
34         help
35           Say yes here to support the GPIO controller in Fujitsu MB86S70 SoCs.
36  
37 +config GPIO_MT7621
38 +       bool "Mediatek GPIO Support"
39 +       depends on SOC_MT7620 || SOC_MT7621
40 +       help
41 +         Say yes here to support the Mediatek SoC GPIO device
42 +
43  config GPIO_MM_LANTIQ
44         bool "Lantiq Memory mapped GPIOs"
45         depends on LANTIQ && SOC_XWAY
46 diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
47 index 13448d78..5563d6e 100644
48 --- a/drivers/gpio/Makefile
49 +++ b/drivers/gpio/Makefile
50 @@ -119,3 +119,4 @@ obj-$(CONFIG_GPIO_XTENSA)   += gpio-xtensa.o
51  obj-$(CONFIG_GPIO_ZEVIO)       += gpio-zevio.o
52  obj-$(CONFIG_GPIO_ZYNQ)                += gpio-zynq.o
53  obj-$(CONFIG_GPIO_ZX)          += gpio-zx.o
54 +obj-$(CONFIG_GPIO_MT7621)      += gpio-mt7621.o
55 diff --git a/drivers/gpio/gpio-mt7621.c b/drivers/gpio/gpio-mt7621.c
56 new file mode 100644
57 index 0000000..7a98b94
58 --- /dev/null
59 +++ b/drivers/gpio/gpio-mt7621.c
60 @@ -0,0 +1,354 @@
61 +/*
62 + * This program is free software; you can redistribute it and/or modify it
63 + * under the terms of the GNU General Public License version 2 as published
64 + * by the Free Software Foundation.
65 + *
66 + * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
67 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
68 + */
69 +
70 +#include <linux/io.h>
71 +#include <linux/err.h>
72 +#include <linux/gpio.h>
73 +#include <linux/module.h>
74 +#include <linux/of_irq.h>
75 +#include <linux/spinlock.h>
76 +#include <linux/irqdomain.h>
77 +#include <linux/interrupt.h>
78 +#include <linux/platform_device.h>
79 +
80 +#define MTK_MAX_BANK           3
81 +#define MTK_BANK_WIDTH         32
82 +
83 +enum mediatek_gpio_reg {
84 +       GPIO_REG_CTRL = 0,
85 +       GPIO_REG_POL,
86 +       GPIO_REG_DATA,
87 +       GPIO_REG_DSET,
88 +       GPIO_REG_DCLR,
89 +       GPIO_REG_REDGE,
90 +       GPIO_REG_FEDGE,
91 +       GPIO_REG_HLVL,
92 +       GPIO_REG_LLVL,
93 +       GPIO_REG_STAT,
94 +       GPIO_REG_EDGE,
95 +};
96 +
97 +static void __iomem *mediatek_gpio_membase;
98 +static int mediatek_gpio_irq;
99 +static struct irq_domain *mediatek_gpio_irq_domain;
100 +static atomic_t irq_refcount = ATOMIC_INIT(0);
101 +
102 +struct mtk_gc {
103 +       struct gpio_chip chip;
104 +       spinlock_t lock;
105 +       int bank;
106 +       u32 rising;
107 +       u32 falling;
108 +} *gc_map[MTK_MAX_BANK];
109 +
110 +static inline struct mtk_gc
111 +*to_mediatek_gpio(struct gpio_chip *chip)
112 +{
113 +       struct mtk_gc *mgc;
114 +
115 +       mgc = container_of(chip, struct mtk_gc, chip);
116 +
117 +       return mgc;
118 +}
119 +
120 +static inline void
121 +mtk_gpio_w32(struct mtk_gc *rg, u8 reg, u32 val)
122 +{
123 +       iowrite32(val, mediatek_gpio_membase + (reg * 0x10) + (rg->bank * 0x4));
124 +}
125 +
126 +static inline u32
127 +mtk_gpio_r32(struct mtk_gc *rg, u8 reg)
128 +{
129 +       return ioread32(mediatek_gpio_membase + (reg * 0x10) + (rg->bank * 0x4));
130 +}
131 +
132 +static void
133 +mediatek_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
134 +{
135 +       struct mtk_gc *rg = to_mediatek_gpio(chip);
136 +
137 +       mtk_gpio_w32(rg, (value) ? GPIO_REG_DSET : GPIO_REG_DCLR, BIT(offset));
138 +}
139 +
140 +static int
141 +mediatek_gpio_get(struct gpio_chip *chip, unsigned offset)
142 +{
143 +       struct mtk_gc *rg = to_mediatek_gpio(chip);
144 +
145 +       return !!(mtk_gpio_r32(rg, GPIO_REG_DATA) & BIT(offset));
146 +}
147 +
148 +static int
149 +mediatek_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
150 +{
151 +       struct mtk_gc *rg = to_mediatek_gpio(chip);
152 +       unsigned long flags;
153 +       u32 t;
154 +
155 +       spin_lock_irqsave(&rg->lock, flags);
156 +       t = mtk_gpio_r32(rg, GPIO_REG_CTRL);
157 +       t &= ~BIT(offset);
158 +       mtk_gpio_w32(rg, GPIO_REG_CTRL, t);
159 +       spin_unlock_irqrestore(&rg->lock, flags);
160 +
161 +       return 0;
162 +}
163 +
164 +static int
165 +mediatek_gpio_direction_output(struct gpio_chip *chip,
166 +                                       unsigned offset, int value)
167 +{
168 +       struct mtk_gc *rg = to_mediatek_gpio(chip);
169 +       unsigned long flags;
170 +       u32 t;
171 +
172 +       spin_lock_irqsave(&rg->lock, flags);
173 +       t = mtk_gpio_r32(rg, GPIO_REG_CTRL);
174 +       t |= BIT(offset);
175 +       mtk_gpio_w32(rg, GPIO_REG_CTRL, t);
176 +       mediatek_gpio_set(chip, offset, value);
177 +       spin_unlock_irqrestore(&rg->lock, flags);
178 +
179 +       return 0;
180 +}
181 +
182 +static int
183 +mediatek_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
184 +{
185 +       struct mtk_gc *rg = to_mediatek_gpio(chip);
186 +       unsigned long flags;
187 +       u32 t;
188 +
189 +       spin_lock_irqsave(&rg->lock, flags);
190 +       t = mtk_gpio_r32(rg, GPIO_REG_CTRL);
191 +       spin_unlock_irqrestore(&rg->lock, flags);
192 +
193 +       if (t & BIT(offset))
194 +               return 0;
195 +
196 +       return 1;
197 +}
198 +
199 +static int
200 +mediatek_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
201 +{
202 +       struct mtk_gc *rg = to_mediatek_gpio(chip);
203 +
204 +       return irq_create_mapping(mediatek_gpio_irq_domain, pin + (rg->bank * MTK_BANK_WIDTH));
205 +}
206 +
207 +static int
208 +mediatek_gpio_bank_probe(struct platform_device *pdev, struct device_node *bank)
209 +{
210 +       const __be32 *id = of_get_property(bank, "reg", NULL);
211 +       struct mtk_gc *rg = devm_kzalloc(&pdev->dev,
212 +                               sizeof(struct mtk_gc), GFP_KERNEL);
213 +
214 +       if (!rg || !id || be32_to_cpu(*id) > MTK_MAX_BANK)
215 +               return -ENOMEM;
216 +
217 +       gc_map[be32_to_cpu(*id)] = rg;
218 +
219 +       memset(rg, 0, sizeof(struct mtk_gc));
220 +
221 +       spin_lock_init(&rg->lock);
222 +
223 +       rg->chip.dev = &pdev->dev;
224 +       rg->chip.label = dev_name(&pdev->dev);
225 +       rg->chip.of_node = bank;
226 +       rg->chip.base = MTK_BANK_WIDTH * be32_to_cpu(*id);
227 +       rg->chip.ngpio = MTK_BANK_WIDTH;
228 +       rg->chip.direction_input = mediatek_gpio_direction_input;
229 +       rg->chip.direction_output = mediatek_gpio_direction_output;
230 +       rg->chip.get_direction = mediatek_gpio_get_direction;
231 +       rg->chip.get = mediatek_gpio_get;
232 +       rg->chip.set = mediatek_gpio_set;
233 +       if (mediatek_gpio_irq_domain)
234 +               rg->chip.to_irq = mediatek_gpio_to_irq;
235 +       rg->bank = be32_to_cpu(*id);
236 +
237 +       /* set polarity to low for all gpios */
238 +       mtk_gpio_w32(rg, GPIO_REG_POL, 0);
239 +
240 +       dev_info(&pdev->dev, "registering %d gpios\n", rg->chip.ngpio);
241 +
242 +       return gpiochip_add(&rg->chip);
243 +}
244 +
245 +static void
246 +mediatek_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
247 +{
248 +       int i;
249 +
250 +       for (i = 0; i < MTK_MAX_BANK; i++) {
251 +               struct mtk_gc *rg = gc_map[i];
252 +               unsigned long pending;
253 +               int bit;
254 +
255 +               if (!rg)
256 +                       continue;
257 +
258 +               pending = mtk_gpio_r32(rg, GPIO_REG_STAT);
259 +
260 +               for_each_set_bit(bit, &pending, MTK_BANK_WIDTH) {
261 +                       u32 map = irq_find_mapping(mediatek_gpio_irq_domain, (MTK_BANK_WIDTH * i) + bit);
262 +
263 +                       generic_handle_irq(map);
264 +                       mtk_gpio_w32(rg, GPIO_REG_STAT, BIT(bit));
265 +               }
266 +       }
267 +}
268 +
269 +static void
270 +mediatek_gpio_irq_unmask(struct irq_data *d)
271 +{
272 +       int pin = d->hwirq;
273 +       int bank = pin / 32;
274 +       struct mtk_gc *rg = gc_map[bank];
275 +       unsigned long flags;
276 +       u32 rise, fall;
277 +
278 +       if (!rg)
279 +               return;
280 +
281 +       rise = mtk_gpio_r32(rg, GPIO_REG_REDGE);
282 +       fall = mtk_gpio_r32(rg, GPIO_REG_FEDGE);
283 +
284 +       spin_lock_irqsave(&rg->lock, flags);
285 +       mtk_gpio_w32(rg, GPIO_REG_REDGE, rise | (BIT(d->hwirq) & rg->rising));
286 +       mtk_gpio_w32(rg, GPIO_REG_FEDGE, fall | (BIT(d->hwirq) & rg->falling));
287 +       spin_unlock_irqrestore(&rg->lock, flags);
288 +}
289 +
290 +static void
291 +mediatek_gpio_irq_mask(struct irq_data *d)
292 +{
293 +       int pin = d->hwirq;
294 +       int bank = pin / 32;
295 +       struct mtk_gc *rg = gc_map[bank];
296 +       unsigned long flags;
297 +       u32 rise, fall;
298 +
299 +       if (!rg)
300 +               return;
301 +
302 +       rise = mtk_gpio_r32(rg, GPIO_REG_REDGE);
303 +       fall = mtk_gpio_r32(rg, GPIO_REG_FEDGE);
304 +
305 +       spin_lock_irqsave(&rg->lock, flags);
306 +       mtk_gpio_w32(rg, GPIO_REG_FEDGE, fall & ~BIT(d->hwirq));
307 +       mtk_gpio_w32(rg, GPIO_REG_REDGE, rise & ~BIT(d->hwirq));
308 +       spin_unlock_irqrestore(&rg->lock, flags);
309 +}
310 +
311 +static int
312 +mediatek_gpio_irq_type(struct irq_data *d, unsigned int type)
313 +{
314 +       int pin = d->hwirq;
315 +       int bank = pin / 32;
316 +       struct mtk_gc *rg = gc_map[bank];
317 +       u32 mask = BIT(d->hwirq);
318 +
319 +       if (!rg)
320 +               return -1;
321 +
322 +       if (type == IRQ_TYPE_PROBE) {
323 +               if ((rg->rising | rg->falling) & mask)
324 +                       return 0;
325 +
326 +               type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
327 +       }
328 +
329 +       if (type & IRQ_TYPE_EDGE_RISING)
330 +               rg->rising |= mask;
331 +       else
332 +               rg->rising &= ~mask;
333 +
334 +       if (type & IRQ_TYPE_EDGE_FALLING)
335 +               rg->falling |= mask;
336 +       else
337 +               rg->falling &= ~mask;
338 +
339 +       return 0;
340 +}
341 +
342 +static struct irq_chip mediatek_gpio_irq_chip = {
343 +       .name           = "GPIO",
344 +       .irq_unmask     = mediatek_gpio_irq_unmask,
345 +       .irq_mask       = mediatek_gpio_irq_mask,
346 +       .irq_mask_ack   = mediatek_gpio_irq_mask,
347 +       .irq_set_type   = mediatek_gpio_irq_type,
348 +};
349 +
350 +static int
351 +mediatek_gpio_gpio_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
352 +{
353 +       irq_set_chip_and_handler(irq, &mediatek_gpio_irq_chip, handle_level_irq);
354 +       irq_set_handler_data(irq, d);
355 +
356 +       return 0;
357 +}
358 +
359 +static const struct irq_domain_ops irq_domain_ops = {
360 +       .xlate = irq_domain_xlate_onecell,
361 +       .map = mediatek_gpio_gpio_map,
362 +};
363 +
364 +static int
365 +mediatek_gpio_probe(struct platform_device *pdev)
366 +{
367 +       struct device_node *bank, *np = pdev->dev.of_node;
368 +       struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
369 +
370 +       mediatek_gpio_membase = devm_ioremap_resource(&pdev->dev, res);
371 +       if (IS_ERR(mediatek_gpio_membase))
372 +               return PTR_ERR(mediatek_gpio_membase);
373 +
374 +       mediatek_gpio_irq = irq_of_parse_and_map(np, 0);
375 +       if (mediatek_gpio_irq) {
376 +               mediatek_gpio_irq_domain = irq_domain_add_linear(np,
377 +                       MTK_MAX_BANK * MTK_BANK_WIDTH,
378 +                       &irq_domain_ops, NULL);
379 +               if (!mediatek_gpio_irq_domain)
380 +                       dev_err(&pdev->dev, "irq_domain_add_linear failed\n");
381 +       }
382 +
383 +       for_each_child_of_node(np, bank)
384 +               if (of_device_is_compatible(bank, "mtk,mt7621-gpio-bank"))
385 +                       mediatek_gpio_bank_probe(pdev, bank);
386 +
387 +       if (mediatek_gpio_irq_domain)
388 +               irq_set_chained_handler(mediatek_gpio_irq, mediatek_gpio_irq_handler);
389 +
390 +       return 0;
391 +}
392 +
393 +static const struct of_device_id mediatek_gpio_match[] = {
394 +       { .compatible = "mtk,mt7621-gpio" },
395 +       {},
396 +};
397 +MODULE_DEVICE_TABLE(of, mediatek_gpio_match);
398 +
399 +static struct platform_driver mediatek_gpio_driver = {
400 +       .probe = mediatek_gpio_probe,
401 +       .driver = {
402 +               .name = "mt7621_gpio",
403 +               .owner = THIS_MODULE,
404 +               .of_match_table = mediatek_gpio_match,
405 +       },
406 +};
407 +
408 +static int __init
409 +mediatek_gpio_init(void)
410 +{
411 +       return platform_driver_register(&mediatek_gpio_driver);
412 +}
413 +
414 +subsys_initcall(mediatek_gpio_init);
415 -- 
416 1.7.10.4
417