ralink: add 3.18 support
[openwrt.git] / target / linux / ramips / patches-3.18 / 0048-GPIO-ralink-add-mt7621-gpio-controller.patch
1 From 8481cdf6f96dc16cbcc129d046c021d17a891274 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 48/57] 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 |  177 ++++++++++++++++++++++++++++++++++++++++++++
12  4 files changed, 187 insertions(+)
13  create mode 100644 drivers/gpio/gpio-mt7621.c
14
15 --- a/arch/mips/Kconfig
16 +++ b/arch/mips/Kconfig
17 @@ -455,6 +455,9 @@ config RALINK
18         select RESET_CONTROLLER
19         select PINCTRL
20         select PINCTRL_RT2880
21 +       select ARCH_HAS_RESET_CONTROLLER
22 +       select RESET_CONTROLLER
23 +       select ARCH_REQUIRE_GPIOLIB
24  
25  config SGI_IP22
26         bool "SGI IP22 (Indy/Indigo2)"
27 --- a/drivers/gpio/Kconfig
28 +++ b/drivers/gpio/Kconfig
29 @@ -898,6 +898,12 @@ config GPIO_BCM_KONA
30         help
31           Turn on GPIO support for Broadcom "Kona" chips.
32  
33 +config GPIO_MT7621
34 +       bool "Mediatek GPIO Support"
35 +       depends on SOC_MT7620 || SOC_MT7621
36 +       help
37 +         Say yes here to support the Mediatek SoC GPIO device
38 +
39  comment "USB GPIO expanders:"
40  
41  config GPIO_VIPERBOARD
42 --- a/drivers/gpio/Makefile
43 +++ b/drivers/gpio/Makefile
44 @@ -107,3 +107,5 @@ obj-$(CONFIG_GPIO_XILINX)   += gpio-xilinx
45  obj-$(CONFIG_GPIO_XTENSA)      += gpio-xtensa.o
46  obj-$(CONFIG_GPIO_ZEVIO)       += gpio-zevio.o
47  obj-$(CONFIG_GPIO_ZYNQ)                += gpio-zynq.o
48 +obj-$(CONFIG_GPIO_MT7621)      += gpio-mt7621.o
49 +
50 --- /dev/null
51 +++ b/drivers/gpio/gpio-mt7621.c
52 @@ -0,0 +1,178 @@
53 +/*
54 + * This program is free software; you can redistribute it and/or modify it
55 + * under the terms of the GNU General Public License version 2 as published
56 + * by the Free Software Foundation.
57 + *
58 + * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
59 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
60 + */
61 +
62 +#include <linux/io.h>
63 +#include <linux/err.h>
64 +#include <linux/gpio.h>
65 +#include <linux/module.h>
66 +#include <linux/of_irq.h>
67 +#include <linux/spinlock.h>
68 +#include <linux/irqdomain.h>
69 +#include <linux/interrupt.h>
70 +#include <linux/platform_device.h>
71 +
72 +#define MTK_BANK_WIDTH         32
73 +
74 +enum mediatek_gpio_reg {
75 +       GPIO_REG_CTRL = 0,
76 +       GPIO_REG_POL,
77 +       GPIO_REG_DATA,
78 +       GPIO_REG_DSET,
79 +       GPIO_REG_DCLR,
80 +};
81 +
82 +static void __iomem *mtk_gc_membase;
83 +
84 +struct mtk_gc {
85 +       struct gpio_chip chip;
86 +       spinlock_t lock;
87 +       int bank;
88 +};
89 +
90 +static inline struct mtk_gc
91 +*to_mediatek_gpio(struct gpio_chip *chip)
92 +{
93 +       struct mtk_gc *mgc;
94 +
95 +       mgc = container_of(chip, struct mtk_gc, chip);
96 +
97 +       return mgc;
98 +}
99 +
100 +static inline void
101 +mtk_gpio_w32(struct mtk_gc *rg, u8 reg, u32 val)
102 +{
103 +       iowrite32(val, mtk_gc_membase + (reg * 0x10) + (rg->bank * 0x4));
104 +}
105 +
106 +static inline u32
107 +mtk_gpio_r32(struct mtk_gc *rg, u8 reg)
108 +{
109 +       return ioread32(mtk_gc_membase + (reg * 0x10) + (rg->bank * 0x4));
110 +}
111 +
112 +static void
113 +mediatek_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
114 +{
115 +       struct mtk_gc *rg = to_mediatek_gpio(chip);
116 +
117 +       mtk_gpio_w32(rg, (value) ? GPIO_REG_DSET : GPIO_REG_DCLR, BIT(offset));
118 +}
119 +
120 +static int
121 +mediatek_gpio_get(struct gpio_chip *chip, unsigned offset)
122 +{
123 +       struct mtk_gc *rg = to_mediatek_gpio(chip);
124 +
125 +       return !!(mtk_gpio_r32(rg, GPIO_REG_DATA) & BIT(offset));
126 +}
127 +
128 +static int
129 +mediatek_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
130 +{
131 +       struct mtk_gc *rg = to_mediatek_gpio(chip);
132 +       unsigned long flags;
133 +       u32 t;
134 +
135 +       spin_lock_irqsave(&rg->lock, flags);
136 +       t = mtk_gpio_r32(rg, GPIO_REG_CTRL);
137 +       t &= ~BIT(offset);
138 +       mtk_gpio_w32(rg, GPIO_REG_CTRL, t);
139 +       spin_unlock_irqrestore(&rg->lock, flags);
140 +
141 +       return 0;
142 +}
143 +
144 +static int
145 +mediatek_gpio_direction_output(struct gpio_chip *chip,
146 +                                       unsigned offset, int value)
147 +{
148 +       struct mtk_gc *rg = to_mediatek_gpio(chip);
149 +       unsigned long flags;
150 +       u32 t;
151 +
152 +       spin_lock_irqsave(&rg->lock, flags);
153 +       t = mtk_gpio_r32(rg, GPIO_REG_CTRL);
154 +       t |= BIT(offset);
155 +       mtk_gpio_w32(rg, GPIO_REG_CTRL, t);
156 +       mediatek_gpio_set(chip, offset, value);
157 +       spin_unlock_irqrestore(&rg->lock, flags);
158 +
159 +       return 0;
160 +}
161 +
162 +static int
163 +mediatek_gpio_bank_probe(struct platform_device *pdev, struct device_node *bank)
164 +{
165 +       const __be32 *id = of_get_property(bank, "reg", NULL);
166 +       struct mtk_gc *rg = devm_kzalloc(&pdev->dev,
167 +                               sizeof(struct mtk_gc), GFP_KERNEL);
168 +       if (!rg || !id)
169 +               return -ENOMEM;
170 +
171 +       spin_lock_init(&rg->lock);
172 +
173 +       rg->chip.dev = &pdev->dev;
174 +       rg->chip.label = dev_name(&pdev->dev);
175 +       rg->chip.of_node = bank;
176 +       rg->chip.base = MTK_BANK_WIDTH * be32_to_cpu(*id);
177 +       rg->chip.ngpio = MTK_BANK_WIDTH;
178 +       rg->chip.direction_input = mediatek_gpio_direction_input;
179 +       rg->chip.direction_output = mediatek_gpio_direction_output;
180 +       rg->chip.get = mediatek_gpio_get;
181 +       rg->chip.set = mediatek_gpio_set;
182 +       rg->bank = be32_to_cpu(*id);
183 +
184 +       /* set polarity to low for all gpios */
185 +       mtk_gpio_w32(rg, GPIO_REG_POL, 0);
186 +
187 +       dev_info(&pdev->dev, "registering %d gpios\n", rg->chip.ngpio);
188 +
189 +       return gpiochip_add(&rg->chip);
190 +}
191 +
192 +static int
193 +mediatek_gpio_probe(struct platform_device *pdev)
194 +{
195 +       struct device_node *bank, *np = pdev->dev.of_node;
196 +       struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
197 +
198 +       mtk_gc_membase = devm_ioremap_resource(&pdev->dev, res);
199 +       if (IS_ERR(mtk_gc_membase))
200 +               return PTR_ERR(mtk_gc_membase);
201 +
202 +       for_each_child_of_node(np, bank)
203 +               if (of_device_is_compatible(bank, "mtk,mt7621-gpio-bank"))
204 +                       mediatek_gpio_bank_probe(pdev, bank);
205 +
206 +       return 0;
207 +}
208 +
209 +static const struct of_device_id mediatek_gpio_match[] = {
210 +       { .compatible = "mtk,mt7621-gpio" },
211 +       {},
212 +};
213 +MODULE_DEVICE_TABLE(of, mediatek_gpio_match);
214 +
215 +static struct platform_driver mediatek_gpio_driver = {
216 +       .probe = mediatek_gpio_probe,
217 +       .driver = {
218 +               .name = "mt7621_gpio",
219 +               .owner = THIS_MODULE,
220 +               .of_match_table = mediatek_gpio_match,
221 +       },
222 +};
223 +
224 +static int __init
225 +mediatek_gpio_init(void)
226 +{
227 +       return platform_driver_register(&mediatek_gpio_driver);
228 +}
229 +
230 +subsys_initcall(mediatek_gpio_init);