1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
4 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
8 #include <linux/gpio/driver.h>
9 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/spinlock.h>
15 #define MTK_BANK_CNT 3
16 #define MTK_BANK_WIDTH 32
18 #define GPIO_BANK_STRIDE 0x04
19 #define GPIO_REG_CTRL 0x00
20 #define GPIO_REG_POL 0x10
21 #define GPIO_REG_DATA 0x20
22 #define GPIO_REG_DSET 0x30
23 #define GPIO_REG_DCLR 0x40
24 #define GPIO_REG_REDGE 0x50
25 #define GPIO_REG_FEDGE 0x60
26 #define GPIO_REG_HLVL 0x70
27 #define GPIO_REG_LLVL 0x80
28 #define GPIO_REG_STAT 0x90
29 #define GPIO_REG_EDGE 0xA0
32 struct irq_chip irq_chip
;
33 struct gpio_chip chip
;
43 * struct mtk - state container for
44 * data of the platform driver. It is 3
45 * separate gpio-chip each one with its
47 * @dev: device instance
48 * @base: memory base address
49 * @gpio_irq: irq number from the device tree
50 * @gc_map: array of the gpio chips
56 struct mtk_gc gc_map
[MTK_BANK_CNT
];
59 static inline struct mtk_gc
*
60 to_mediatek_gpio(struct gpio_chip
*chip
)
62 return container_of(chip
, struct mtk_gc
, chip
);
66 mtk_gpio_w32(struct mtk_gc
*rg
, u32 offset
, u32 val
)
68 struct gpio_chip
*gc
= &rg
->chip
;
69 struct mtk
*mtk
= gpiochip_get_data(gc
);
71 offset
= (rg
->bank
* GPIO_BANK_STRIDE
) + offset
;
72 gc
->write_reg(mtk
->base
+ offset
, val
);
76 mtk_gpio_r32(struct mtk_gc
*rg
, u32 offset
)
78 struct gpio_chip
*gc
= &rg
->chip
;
79 struct mtk
*mtk
= gpiochip_get_data(gc
);
81 offset
= (rg
->bank
* GPIO_BANK_STRIDE
) + offset
;
82 return gc
->read_reg(mtk
->base
+ offset
);
86 mediatek_gpio_irq_handler(int irq
, void *data
)
88 struct gpio_chip
*gc
= data
;
89 struct mtk_gc
*rg
= to_mediatek_gpio(gc
);
90 irqreturn_t ret
= IRQ_NONE
;
91 unsigned long pending
;
94 pending
= mtk_gpio_r32(rg
, GPIO_REG_STAT
);
96 for_each_set_bit(bit
, &pending
, MTK_BANK_WIDTH
) {
97 generic_handle_domain_irq(gc
->irq
.domain
, bit
);
98 mtk_gpio_w32(rg
, GPIO_REG_STAT
, BIT(bit
));
106 mediatek_gpio_irq_unmask(struct irq_data
*d
)
108 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
109 struct mtk_gc
*rg
= to_mediatek_gpio(gc
);
112 u32 rise
, fall
, high
, low
;
114 gpiochip_enable_irq(gc
, d
->hwirq
);
116 spin_lock_irqsave(&rg
->lock
, flags
);
117 rise
= mtk_gpio_r32(rg
, GPIO_REG_REDGE
);
118 fall
= mtk_gpio_r32(rg
, GPIO_REG_FEDGE
);
119 high
= mtk_gpio_r32(rg
, GPIO_REG_HLVL
);
120 low
= mtk_gpio_r32(rg
, GPIO_REG_LLVL
);
121 mtk_gpio_w32(rg
, GPIO_REG_REDGE
, rise
| (BIT(pin
) & rg
->rising
));
122 mtk_gpio_w32(rg
, GPIO_REG_FEDGE
, fall
| (BIT(pin
) & rg
->falling
));
123 mtk_gpio_w32(rg
, GPIO_REG_HLVL
, high
| (BIT(pin
) & rg
->hlevel
));
124 mtk_gpio_w32(rg
, GPIO_REG_LLVL
, low
| (BIT(pin
) & rg
->llevel
));
125 spin_unlock_irqrestore(&rg
->lock
, flags
);
129 mediatek_gpio_irq_mask(struct irq_data
*d
)
131 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
132 struct mtk_gc
*rg
= to_mediatek_gpio(gc
);
135 u32 rise
, fall
, high
, low
;
137 spin_lock_irqsave(&rg
->lock
, flags
);
138 rise
= mtk_gpio_r32(rg
, GPIO_REG_REDGE
);
139 fall
= mtk_gpio_r32(rg
, GPIO_REG_FEDGE
);
140 high
= mtk_gpio_r32(rg
, GPIO_REG_HLVL
);
141 low
= mtk_gpio_r32(rg
, GPIO_REG_LLVL
);
142 mtk_gpio_w32(rg
, GPIO_REG_FEDGE
, fall
& ~BIT(pin
));
143 mtk_gpio_w32(rg
, GPIO_REG_REDGE
, rise
& ~BIT(pin
));
144 mtk_gpio_w32(rg
, GPIO_REG_HLVL
, high
& ~BIT(pin
));
145 mtk_gpio_w32(rg
, GPIO_REG_LLVL
, low
& ~BIT(pin
));
146 spin_unlock_irqrestore(&rg
->lock
, flags
);
148 gpiochip_disable_irq(gc
, d
->hwirq
);
152 mediatek_gpio_irq_type(struct irq_data
*d
, unsigned int type
)
154 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
155 struct mtk_gc
*rg
= to_mediatek_gpio(gc
);
159 if (type
== IRQ_TYPE_PROBE
) {
160 if ((rg
->rising
| rg
->falling
|
161 rg
->hlevel
| rg
->llevel
) & mask
)
164 type
= IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
;
168 rg
->falling
&= ~mask
;
172 switch (type
& IRQ_TYPE_SENSE_MASK
) {
173 case IRQ_TYPE_EDGE_BOTH
:
177 case IRQ_TYPE_EDGE_RISING
:
180 case IRQ_TYPE_EDGE_FALLING
:
183 case IRQ_TYPE_LEVEL_HIGH
:
186 case IRQ_TYPE_LEVEL_LOW
:
195 mediatek_gpio_xlate(struct gpio_chip
*chip
,
196 const struct of_phandle_args
*spec
, u32
*flags
)
198 int gpio
= spec
->args
[0];
199 struct mtk_gc
*rg
= to_mediatek_gpio(chip
);
201 if (rg
->bank
!= gpio
/ MTK_BANK_WIDTH
)
205 *flags
= spec
->args
[1];
207 return gpio
% MTK_BANK_WIDTH
;
210 static const struct irq_chip mt7621_irq_chip
= {
211 .name
= "mt7621-gpio",
212 .irq_mask_ack
= mediatek_gpio_irq_mask
,
213 .irq_mask
= mediatek_gpio_irq_mask
,
214 .irq_unmask
= mediatek_gpio_irq_unmask
,
215 .irq_set_type
= mediatek_gpio_irq_type
,
216 .flags
= IRQCHIP_IMMUTABLE
,
217 GPIOCHIP_IRQ_RESOURCE_HELPERS
,
221 mediatek_gpio_bank_probe(struct device
*dev
, int bank
)
223 struct mtk
*mtk
= dev_get_drvdata(dev
);
225 void __iomem
*dat
, *set
, *ctrl
, *diro
;
228 rg
= &mtk
->gc_map
[bank
];
229 memset(rg
, 0, sizeof(*rg
));
231 spin_lock_init(&rg
->lock
);
234 dat
= mtk
->base
+ GPIO_REG_DATA
+ (rg
->bank
* GPIO_BANK_STRIDE
);
235 set
= mtk
->base
+ GPIO_REG_DSET
+ (rg
->bank
* GPIO_BANK_STRIDE
);
236 ctrl
= mtk
->base
+ GPIO_REG_DCLR
+ (rg
->bank
* GPIO_BANK_STRIDE
);
237 diro
= mtk
->base
+ GPIO_REG_CTRL
+ (rg
->bank
* GPIO_BANK_STRIDE
);
239 ret
= bgpio_init(&rg
->chip
, dev
, 4, dat
, set
, ctrl
, diro
, NULL
,
240 BGPIOF_NO_SET_ON_INPUT
);
242 dev_err(dev
, "bgpio_init() failed\n");
246 rg
->chip
.of_gpio_n_cells
= 2;
247 rg
->chip
.of_xlate
= mediatek_gpio_xlate
;
248 rg
->chip
.label
= devm_kasprintf(dev
, GFP_KERNEL
, "%s-bank%d",
249 dev_name(dev
), bank
);
253 rg
->chip
.offset
= bank
* MTK_BANK_WIDTH
;
256 struct gpio_irq_chip
*girq
;
259 * Directly request the irq here instead of passing
260 * a flow-handler because the irq is shared.
262 ret
= devm_request_irq(dev
, mtk
->gpio_irq
,
263 mediatek_gpio_irq_handler
, IRQF_SHARED
,
264 rg
->chip
.label
, &rg
->chip
);
267 dev_err(dev
, "Error requesting IRQ %d: %d\n",
272 girq
= &rg
->chip
.irq
;
273 gpio_irq_chip_set_chip(girq
, &mt7621_irq_chip
);
274 /* This will let us handle the parent IRQ in the driver */
275 girq
->parent_handler
= NULL
;
276 girq
->num_parents
= 0;
277 girq
->parents
= NULL
;
278 girq
->default_type
= IRQ_TYPE_NONE
;
279 girq
->handler
= handle_simple_irq
;
282 ret
= devm_gpiochip_add_data(dev
, &rg
->chip
, mtk
);
284 dev_err(dev
, "Could not register gpio %d, ret=%d\n",
285 rg
->chip
.ngpio
, ret
);
289 /* set polarity to low for all gpios */
290 mtk_gpio_w32(rg
, GPIO_REG_POL
, 0);
292 dev_info(dev
, "registering %d gpios\n", rg
->chip
.ngpio
);
298 mediatek_gpio_probe(struct platform_device
*pdev
)
300 struct device
*dev
= &pdev
->dev
;
305 mtk
= devm_kzalloc(dev
, sizeof(*mtk
), GFP_KERNEL
);
309 mtk
->base
= devm_platform_ioremap_resource(pdev
, 0);
310 if (IS_ERR(mtk
->base
))
311 return PTR_ERR(mtk
->base
);
313 mtk
->gpio_irq
= platform_get_irq(pdev
, 0);
314 if (mtk
->gpio_irq
< 0)
315 return mtk
->gpio_irq
;
318 platform_set_drvdata(pdev
, mtk
);
320 for (i
= 0; i
< MTK_BANK_CNT
; i
++) {
321 ret
= mediatek_gpio_bank_probe(dev
, i
);
329 static const struct of_device_id mediatek_gpio_match
[] = {
330 { .compatible
= "mediatek,mt7621-gpio" },
333 MODULE_DEVICE_TABLE(of
, mediatek_gpio_match
);
335 static struct platform_driver mediatek_gpio_driver
= {
336 .probe
= mediatek_gpio_probe
,
338 .name
= "mt7621_gpio",
339 .of_match_table
= mediatek_gpio_match
,
343 builtin_platform_driver(mediatek_gpio_driver
);