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/of_irq.h>
13 #include <linux/platform_device.h>
14 #include <linux/spinlock.h>
16 #define MTK_BANK_CNT 3
17 #define MTK_BANK_WIDTH 32
19 #define GPIO_BANK_STRIDE 0x04
20 #define GPIO_REG_CTRL 0x00
21 #define GPIO_REG_POL 0x10
22 #define GPIO_REG_DATA 0x20
23 #define GPIO_REG_DSET 0x30
24 #define GPIO_REG_DCLR 0x40
25 #define GPIO_REG_REDGE 0x50
26 #define GPIO_REG_FEDGE 0x60
27 #define GPIO_REG_HLVL 0x70
28 #define GPIO_REG_LLVL 0x80
29 #define GPIO_REG_STAT 0x90
30 #define GPIO_REG_EDGE 0xA0
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 u32 map
= irq_find_mapping(gc
->irq
.domain
, bit
);
99 generic_handle_irq(map
);
100 mtk_gpio_w32(rg
, GPIO_REG_STAT
, BIT(bit
));
108 mediatek_gpio_irq_unmask(struct irq_data
*d
)
110 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
111 struct mtk_gc
*rg
= to_mediatek_gpio(gc
);
114 u32 rise
, fall
, high
, low
;
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
);
150 mediatek_gpio_irq_type(struct irq_data
*d
, unsigned int type
)
152 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
153 struct mtk_gc
*rg
= to_mediatek_gpio(gc
);
157 if (type
== IRQ_TYPE_PROBE
) {
158 if ((rg
->rising
| rg
->falling
|
159 rg
->hlevel
| rg
->llevel
) & mask
)
162 type
= IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
;
166 rg
->falling
&= ~mask
;
170 switch (type
& IRQ_TYPE_SENSE_MASK
) {
171 case IRQ_TYPE_EDGE_BOTH
:
175 case IRQ_TYPE_EDGE_RISING
:
178 case IRQ_TYPE_EDGE_FALLING
:
181 case IRQ_TYPE_LEVEL_HIGH
:
184 case IRQ_TYPE_LEVEL_LOW
:
192 static struct irq_chip mediatek_gpio_irq_chip
= {
193 .irq_unmask
= mediatek_gpio_irq_unmask
,
194 .irq_mask
= mediatek_gpio_irq_mask
,
195 .irq_mask_ack
= mediatek_gpio_irq_mask
,
196 .irq_set_type
= mediatek_gpio_irq_type
,
200 mediatek_gpio_xlate(struct gpio_chip
*chip
,
201 const struct of_phandle_args
*spec
, u32
*flags
)
203 int gpio
= spec
->args
[0];
204 struct mtk_gc
*rg
= to_mediatek_gpio(chip
);
206 if (rg
->bank
!= gpio
/ MTK_BANK_WIDTH
)
210 *flags
= spec
->args
[1];
212 return gpio
% MTK_BANK_WIDTH
;
216 mediatek_gpio_bank_probe(struct device
*dev
,
217 struct device_node
*node
, int bank
)
219 struct mtk
*mtk
= dev_get_drvdata(dev
);
221 void __iomem
*dat
, *set
, *ctrl
, *diro
;
224 rg
= &mtk
->gc_map
[bank
];
225 memset(rg
, 0, sizeof(*rg
));
227 spin_lock_init(&rg
->lock
);
228 rg
->chip
.of_node
= node
;
231 dat
= mtk
->base
+ GPIO_REG_DATA
+ (rg
->bank
* GPIO_BANK_STRIDE
);
232 set
= mtk
->base
+ GPIO_REG_DSET
+ (rg
->bank
* GPIO_BANK_STRIDE
);
233 ctrl
= mtk
->base
+ GPIO_REG_DCLR
+ (rg
->bank
* GPIO_BANK_STRIDE
);
234 diro
= mtk
->base
+ GPIO_REG_CTRL
+ (rg
->bank
* GPIO_BANK_STRIDE
);
236 ret
= bgpio_init(&rg
->chip
, dev
, 4,
237 dat
, set
, ctrl
, diro
, NULL
, 0);
239 dev_err(dev
, "bgpio_init() failed\n");
243 rg
->chip
.of_gpio_n_cells
= 2;
244 rg
->chip
.of_xlate
= mediatek_gpio_xlate
;
245 rg
->chip
.label
= devm_kasprintf(dev
, GFP_KERNEL
, "%s-bank%d",
246 dev_name(dev
), bank
);
248 ret
= devm_gpiochip_add_data(dev
, &rg
->chip
, mtk
);
250 dev_err(dev
, "Could not register gpio %d, ret=%d\n",
251 rg
->chip
.ngpio
, ret
);
257 * Manually request the irq here instead of passing
258 * a flow-handler to gpiochip_set_chained_irqchip,
259 * because the irq is shared.
261 ret
= devm_request_irq(dev
, mtk
->gpio_irq
,
262 mediatek_gpio_irq_handler
, IRQF_SHARED
,
263 rg
->chip
.label
, &rg
->chip
);
266 dev_err(dev
, "Error requesting IRQ %d: %d\n",
271 ret
= gpiochip_irqchip_add(&rg
->chip
, &mediatek_gpio_irq_chip
,
272 0, handle_simple_irq
, IRQ_TYPE_NONE
);
274 dev_err(dev
, "failed to add gpiochip_irqchip\n");
278 gpiochip_set_chained_irqchip(&rg
->chip
, &mediatek_gpio_irq_chip
,
279 mtk
->gpio_irq
, NULL
);
282 /* set polarity to low for all gpios */
283 mtk_gpio_w32(rg
, GPIO_REG_POL
, 0);
285 dev_info(dev
, "registering %d gpios\n", rg
->chip
.ngpio
);
291 mediatek_gpio_probe(struct platform_device
*pdev
)
293 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
294 struct device
*dev
= &pdev
->dev
;
295 struct device_node
*np
= dev
->of_node
;
299 mtk
= devm_kzalloc(dev
, sizeof(*mtk
), GFP_KERNEL
);
303 mtk
->base
= devm_ioremap_resource(dev
, res
);
304 if (IS_ERR(mtk
->base
))
305 return PTR_ERR(mtk
->base
);
307 mtk
->gpio_irq
= irq_of_parse_and_map(np
, 0);
309 platform_set_drvdata(pdev
, mtk
);
310 mediatek_gpio_irq_chip
.name
= dev_name(dev
);
312 for (i
= 0; i
< MTK_BANK_CNT
; i
++)
313 mediatek_gpio_bank_probe(dev
, np
, i
);
318 static const struct of_device_id mediatek_gpio_match
[] = {
319 { .compatible
= "mediatek,mt7621-gpio" },
322 MODULE_DEVICE_TABLE(of
, mediatek_gpio_match
);
324 static struct platform_driver mediatek_gpio_driver
= {
325 .probe
= mediatek_gpio_probe
,
327 .name
= "mt7621_gpio",
328 .of_match_table
= mediatek_gpio_match
,
332 builtin_platform_driver(mediatek_gpio_driver
);