1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
* Copyright (C) 2013 John Crispin <blogic@openwrt.org>
*/
#include <linux/io.h>
#include <linux/err.h>
#include <linux/gpio/driver.h>
#include <linux/module.h>
#include <linux/of_irq.h>
#include <linux/spinlock.h>
#include <linux/irqdomain.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#define MTK_BANK_CNT 3
#define MTK_BANK_WIDTH 32
enum mediatek_gpio_reg {
GPIO_REG_CTRL = 0,
GPIO_REG_POL,
GPIO_REG_DATA,
GPIO_REG_DSET,
GPIO_REG_DCLR,
GPIO_REG_REDGE,
GPIO_REG_FEDGE,
GPIO_REG_HLVL,
GPIO_REG_LLVL,
GPIO_REG_STAT,
GPIO_REG_EDGE,
};
struct mtk_gc {
struct gpio_chip chip;
spinlock_t lock;
int bank;
u32 rising;
u32 falling;
};
struct mtk_data {
void __iomem *gpio_membase;
int gpio_irq;
struct irq_domain *gpio_irq_domain;
struct mtk_gc *gc_map[MTK_BANK_CNT];
};
static inline struct mtk_gc
*to_mediatek_gpio(struct gpio_chip *chip)
{
struct mtk_gc *mgc;
mgc = container_of(chip, struct mtk_gc, chip);
return mgc;
}
static inline void
mtk_gpio_w32(struct mtk_gc *rg, u8 reg, u32 val)
{
struct mtk_data *gpio_data = gpiochip_get_data(&rg->chip);
u32 offset = (reg * 0x10) + (rg->bank * 0x4);
iowrite32(val, gpio_data->gpio_membase + offset);
}
static inline u32
mtk_gpio_r32(struct mtk_gc *rg, u8 reg)
{
struct mtk_data *gpio_data = gpiochip_get_data(&rg->chip);
u32 offset = (reg * 0x10) + (rg->bank * 0x4);
return ioread32(gpio_data->gpio_membase + offset);
}
static void
mediatek_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
{
struct mtk_gc *rg = to_mediatek_gpio(chip);
mtk_gpio_w32(rg, (value) ? GPIO_REG_DSET : GPIO_REG_DCLR, BIT(offset));
}
static int
mediatek_gpio_get(struct gpio_chip *chip, unsigned int offset)
{
struct mtk_gc *rg = to_mediatek_gpio(chip);
return !!(mtk_gpio_r32(rg, GPIO_REG_DATA) & BIT(offset));
}
static int
mediatek_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
{
struct mtk_gc *rg = to_mediatek_gpio(chip);
unsigned long flags;
u32 t;
spin_lock_irqsave(&rg->lock, flags);
t = mtk_gpio_r32(rg, GPIO_REG_CTRL);
t &= ~BIT(offset);
mtk_gpio_w32(rg, GPIO_REG_CTRL, t);
spin_unlock_irqrestore(&rg->lock, flags);
return 0;
}
static int
mediatek_gpio_direction_output(struct gpio_chip *chip,
unsigned int offset, int value)
{
struct mtk_gc *rg = to_mediatek_gpio(chip);
unsigned long flags;
u32 t;
spin_lock_irqsave(&rg->lock, flags);
t = mtk_gpio_r32(rg, GPIO_REG_CTRL);
t |= BIT(offset);
mtk_gpio_w32(rg, GPIO_REG_CTRL, t);
mediatek_gpio_set(chip, offset, value);
spin_unlock_irqrestore(&rg->lock, flags);
return 0;
}
static int
mediatek_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
{
struct mtk_gc *rg = to_mediatek_gpio(chip);
unsigned long flags;
u32 t;
spin_lock_irqsave(&rg->lock, flags);
t = mtk_gpio_r32(rg, GPIO_REG_CTRL);
spin_unlock_irqrestore(&rg->lock, flags);
return (t & BIT(offset)) ? 0 : 1;
}
static int
mediatek_gpio_to_irq(struct gpio_chip *chip, unsigned int pin)
{
struct mtk_data *gpio_data = gpiochip_get_data(chip);
struct mtk_gc *rg = to_mediatek_gpio(chip);
return irq_create_mapping(gpio_data->gpio_irq_domain,
pin + (rg->bank * MTK_BANK_WIDTH));
}
static int
mediatek_gpio_bank_probe(struct platform_device *pdev, struct device_node *bank)
{
struct mtk_data *gpio_data = dev_get_drvdata(&pdev->dev);
const __be32 *id = of_get_property(bank, "reg", NULL);
struct mtk_gc *rg;
int ret;
if (!id || be32_to_cpu(*id) >= MTK_BANK_CNT)
return -EINVAL;
rg = devm_kzalloc(&pdev->dev, sizeof(struct mtk_gc), GFP_KERNEL);
if (!rg)
return -ENOMEM;
gpio_data->gc_map[be32_to_cpu(*id)] = rg;
spin_lock_init(&rg->lock);
rg->chip.parent = &pdev->dev;
rg->chip.label = dev_name(&pdev->dev);
rg->chip.of_node = bank;
rg->chip.base = MTK_BANK_WIDTH * be32_to_cpu(*id);
rg->chip.ngpio = MTK_BANK_WIDTH;
rg->chip.direction_input = mediatek_gpio_direction_input;
rg->chip.direction_output = mediatek_gpio_direction_output;
rg->chip.get_direction = mediatek_gpio_get_direction;
rg->chip.get = mediatek_gpio_get;
rg->chip.set = mediatek_gpio_set;
if (gpio_data->gpio_irq_domain)
rg->chip.to_irq = mediatek_gpio_to_irq;
rg->bank = be32_to_cpu(*id);
ret = devm_gpiochip_add_data(&pdev->dev, &rg->chip, gpio_data);
if (ret < 0) {
dev_err(&pdev->dev, "Could not register gpio %d, ret=%d\n",
rg->chip.ngpio, ret);
return ret;
}
/* set polarity to low for all gpios */
mtk_gpio_w32(rg, GPIO_REG_POL, 0);
dev_info(&pdev->dev, "registering %d gpios\n", rg->chip.ngpio);
return 0;
}
static void
mediatek_gpio_irq_handler(struct irq_desc *desc)
{
struct mtk_data *gpio_data = irq_desc_get_handler_data(desc);
int i;
for (i = 0; i < MTK_BANK_CNT; i++) {
struct mtk_gc *rg = gpio_data->gc_map[i];
unsigned long pending;
int bit;
if (!rg)
continue;
pending = mtk_gpio_r32(rg, GPIO_REG_STAT);
for_each_set_bit(bit, &pending, MTK_BANK_WIDTH) {
u32 map = irq_find_mapping(gpio_data->gpio_irq_domain,
(MTK_BANK_WIDTH * i) + bit);
generic_handle_irq(map);
mtk_gpio_w32(rg, GPIO_REG_STAT, BIT(bit));
}
}
}
static void
mediatek_gpio_irq_unmask(struct irq_data *d)
{
struct mtk_data *gpio_data = irq_data_get_irq_chip_data(d);
int pin = d->hwirq;
int bank = pin / MTK_BANK_WIDTH;
struct mtk_gc *rg = gpio_data->gc_map[bank];
unsigned long flags;
u32 rise, fall;
if (!rg)
return;
rise = mtk_gpio_r32(rg, GPIO_REG_REDGE);
fall = mtk_gpio_r32(rg, GPIO_REG_FEDGE);
spin_lock_irqsave(&rg->lock, flags);
mtk_gpio_w32(rg, GPIO_REG_REDGE, rise | (BIT(d->hwirq) & rg->rising));
mtk_gpio_w32(rg, GPIO_REG_FEDGE, fall | (BIT(d->hwirq) & rg->falling));
spin_unlock_irqrestore(&rg->lock, flags);
}
static void
mediatek_gpio_irq_mask(struct irq_data *d)
{
struct mtk_data *gpio_data = irq_data_get_irq_chip_data(d);
int pin = d->hwirq;
int bank = pin / MTK_BANK_WIDTH;
struct mtk_gc *rg = gpio_data->gc_map[bank];
unsigned long flags;
u32 rise, fall;
if (!rg)
return;
rise = mtk_gpio_r32(rg, GPIO_REG_REDGE);
fall = mtk_gpio_r32(rg, GPIO_REG_FEDGE);
spin_lock_irqsave(&rg->lock, flags);
mtk_gpio_w32(rg, GPIO_REG_FEDGE, fall & ~BIT(d->hwirq));
mtk_gpio_w32(rg, GPIO_REG_REDGE, rise & ~BIT(d->hwirq));
spin_unlock_irqrestore(&rg->lock, flags);
}
static int
mediatek_gpio_irq_type(struct irq_data *d, unsigned int type)
{
struct mtk_data *gpio_data = irq_data_get_irq_chip_data(d);
int pin = d->hwirq;
int bank = pin / MTK_BANK_WIDTH;
struct mtk_gc *rg = gpio_data->gc_map[bank];
u32 mask = BIT(d->hwirq);
if (!rg)
return -1;
if (type == IRQ_TYPE_PROBE) {
if ((rg->rising | rg->falling) & mask)
return 0;
type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
}
if (type & IRQ_TYPE_EDGE_RISING)
rg->rising |= mask;
else
rg->rising &= ~mask;
if (type & IRQ_TYPE_EDGE_FALLING)
rg->falling |= mask;
else
rg->falling &= ~mask;
return 0;
}
static struct irq_chip mediatek_gpio_irq_chip = {
.name = "GPIO",
.irq_unmask = mediatek_gpio_irq_unmask,
.irq_mask = mediatek_gpio_irq_mask,
.irq_mask_ack = mediatek_gpio_irq_mask,
.irq_set_type = mediatek_gpio_irq_type,
};
static int
mediatek_gpio_gpio_map(struct irq_domain *d, unsigned int irq,
irq_hw_number_t hw)
{
int ret;
ret = irq_set_chip_data(irq, d->host_data);
if (ret < 0)
return ret;
irq_set_chip_and_handler(irq, &mediatek_gpio_irq_chip,
handle_level_irq);
irq_set_handler_data(irq, d);
return 0;
}
static const struct irq_domain_ops irq_domain_ops = {
.xlate = irq_domain_xlate_onecell,
.map = mediatek_gpio_gpio_map,
};
static int
mediatek_gpio_probe(struct platform_device *pdev)
{
struct device_node *bank, *np = pdev->dev.of_node;
struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
struct mtk_data *gpio_data;
gpio_data = devm_kzalloc(&pdev->dev, sizeof(*gpio_data), GFP_KERNEL);
if (!gpio_data)
return -ENOMEM;
gpio_data->gpio_membase = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR(gpio_data->gpio_membase))
return PTR_ERR(gpio_data->gpio_membase);
gpio_data->gpio_irq = irq_of_parse_and_map(np, 0);
if (gpio_data->gpio_irq) {
gpio_data->gpio_irq_domain = irq_domain_add_linear(np,
MTK_BANK_CNT * MTK_BANK_WIDTH,
&irq_domain_ops, gpio_data);
if (!gpio_data->gpio_irq_domain)
dev_err(&pdev->dev, "irq_domain_add_linear failed\n");
}
platform_set_drvdata(pdev, gpio_data);
for_each_child_of_node(np, bank)
if (of_device_is_compatible(bank, "mediatek,mt7621-gpio-bank"))
mediatek_gpio_bank_probe(pdev, bank);
if (gpio_data->gpio_irq_domain)
irq_set_chained_handler_and_data(gpio_data->gpio_irq,
mediatek_gpio_irq_handler,
gpio_data);
return 0;
}
static const struct of_device_id mediatek_gpio_match[] = {
{ .compatible = "mediatek,mt7621-gpio" },
{},
};
MODULE_DEVICE_TABLE(of, mediatek_gpio_match);
static struct platform_driver mediatek_gpio_driver = {
.probe = mediatek_gpio_probe,
.driver = {
.name = "mt7621_gpio",
.of_match_table = mediatek_gpio_match,
},
};
module_platform_driver(mediatek_gpio_driver);
|