1 // SPDX-License-Identifier: GPL-2.0-only
3 * Atheros AR71XX/AR724X/AR913X GPIO API support
5 * Copyright (C) 2015 Alban Bedel <albeu@free.fr>
6 * Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com>
7 * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
8 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
11 #include <linux/gpio/driver.h>
12 #include <linux/platform_data/gpio-ath79.h>
13 #include <linux/of_device.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/irq.h>
18 #define AR71XX_GPIO_REG_OE 0x00
19 #define AR71XX_GPIO_REG_IN 0x04
20 #define AR71XX_GPIO_REG_SET 0x0c
21 #define AR71XX_GPIO_REG_CLEAR 0x10
23 #define AR71XX_GPIO_REG_INT_ENABLE 0x14
24 #define AR71XX_GPIO_REG_INT_TYPE 0x18
25 #define AR71XX_GPIO_REG_INT_POLARITY 0x1c
26 #define AR71XX_GPIO_REG_INT_PENDING 0x20
27 #define AR71XX_GPIO_REG_INT_MASK 0x24
29 struct ath79_gpio_ctrl
{
33 unsigned long both_edges
;
36 static struct ath79_gpio_ctrl
*irq_data_to_ath79_gpio(struct irq_data
*data
)
38 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(data
);
40 return container_of(gc
, struct ath79_gpio_ctrl
, gc
);
43 static u32
ath79_gpio_read(struct ath79_gpio_ctrl
*ctrl
, unsigned reg
)
45 return readl(ctrl
->base
+ reg
);
48 static void ath79_gpio_write(struct ath79_gpio_ctrl
*ctrl
,
49 unsigned reg
, u32 val
)
51 writel(val
, ctrl
->base
+ reg
);
54 static bool ath79_gpio_update_bits(
55 struct ath79_gpio_ctrl
*ctrl
, unsigned reg
, u32 mask
, u32 bits
)
59 old_val
= ath79_gpio_read(ctrl
, reg
);
60 new_val
= (old_val
& ~mask
) | (bits
& mask
);
62 if (new_val
!= old_val
)
63 ath79_gpio_write(ctrl
, reg
, new_val
);
65 return new_val
!= old_val
;
68 static void ath79_gpio_irq_unmask(struct irq_data
*data
)
70 struct ath79_gpio_ctrl
*ctrl
= irq_data_to_ath79_gpio(data
);
71 u32 mask
= BIT(irqd_to_hwirq(data
));
74 raw_spin_lock_irqsave(&ctrl
->lock
, flags
);
75 ath79_gpio_update_bits(ctrl
, AR71XX_GPIO_REG_INT_MASK
, mask
, mask
);
76 raw_spin_unlock_irqrestore(&ctrl
->lock
, flags
);
79 static void ath79_gpio_irq_mask(struct irq_data
*data
)
81 struct ath79_gpio_ctrl
*ctrl
= irq_data_to_ath79_gpio(data
);
82 u32 mask
= BIT(irqd_to_hwirq(data
));
85 raw_spin_lock_irqsave(&ctrl
->lock
, flags
);
86 ath79_gpio_update_bits(ctrl
, AR71XX_GPIO_REG_INT_MASK
, mask
, 0);
87 raw_spin_unlock_irqrestore(&ctrl
->lock
, flags
);
90 static void ath79_gpio_irq_enable(struct irq_data
*data
)
92 struct ath79_gpio_ctrl
*ctrl
= irq_data_to_ath79_gpio(data
);
93 u32 mask
= BIT(irqd_to_hwirq(data
));
96 raw_spin_lock_irqsave(&ctrl
->lock
, flags
);
97 ath79_gpio_update_bits(ctrl
, AR71XX_GPIO_REG_INT_ENABLE
, mask
, mask
);
98 ath79_gpio_update_bits(ctrl
, AR71XX_GPIO_REG_INT_MASK
, mask
, mask
);
99 raw_spin_unlock_irqrestore(&ctrl
->lock
, flags
);
102 static void ath79_gpio_irq_disable(struct irq_data
*data
)
104 struct ath79_gpio_ctrl
*ctrl
= irq_data_to_ath79_gpio(data
);
105 u32 mask
= BIT(irqd_to_hwirq(data
));
108 raw_spin_lock_irqsave(&ctrl
->lock
, flags
);
109 ath79_gpio_update_bits(ctrl
, AR71XX_GPIO_REG_INT_MASK
, mask
, 0);
110 ath79_gpio_update_bits(ctrl
, AR71XX_GPIO_REG_INT_ENABLE
, mask
, 0);
111 raw_spin_unlock_irqrestore(&ctrl
->lock
, flags
);
114 static int ath79_gpio_irq_set_type(struct irq_data
*data
,
115 unsigned int flow_type
)
117 struct ath79_gpio_ctrl
*ctrl
= irq_data_to_ath79_gpio(data
);
118 u32 mask
= BIT(irqd_to_hwirq(data
));
119 u32 type
= 0, polarity
= 0;
124 case IRQ_TYPE_EDGE_RISING
:
126 case IRQ_TYPE_EDGE_FALLING
:
127 case IRQ_TYPE_EDGE_BOTH
:
130 case IRQ_TYPE_LEVEL_HIGH
:
133 case IRQ_TYPE_LEVEL_LOW
:
141 raw_spin_lock_irqsave(&ctrl
->lock
, flags
);
143 if (flow_type
== IRQ_TYPE_EDGE_BOTH
) {
144 ctrl
->both_edges
|= mask
;
145 polarity
= ~ath79_gpio_read(ctrl
, AR71XX_GPIO_REG_IN
);
147 ctrl
->both_edges
&= ~mask
;
150 /* As the IRQ configuration can't be loaded atomically we
151 * have to disable the interrupt while the configuration state
154 disabled
= ath79_gpio_update_bits(
155 ctrl
, AR71XX_GPIO_REG_INT_ENABLE
, mask
, 0);
157 ath79_gpio_update_bits(
158 ctrl
, AR71XX_GPIO_REG_INT_TYPE
, mask
, type
);
159 ath79_gpio_update_bits(
160 ctrl
, AR71XX_GPIO_REG_INT_POLARITY
, mask
, polarity
);
163 ath79_gpio_update_bits(
164 ctrl
, AR71XX_GPIO_REG_INT_ENABLE
, mask
, mask
);
166 raw_spin_unlock_irqrestore(&ctrl
->lock
, flags
);
171 static struct irq_chip ath79_gpio_irqchip
= {
172 .name
= "gpio-ath79",
173 .irq_enable
= ath79_gpio_irq_enable
,
174 .irq_disable
= ath79_gpio_irq_disable
,
175 .irq_mask
= ath79_gpio_irq_mask
,
176 .irq_unmask
= ath79_gpio_irq_unmask
,
177 .irq_set_type
= ath79_gpio_irq_set_type
,
180 static void ath79_gpio_irq_handler(struct irq_desc
*desc
)
182 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
183 struct irq_chip
*irqchip
= irq_desc_get_chip(desc
);
184 struct ath79_gpio_ctrl
*ctrl
=
185 container_of(gc
, struct ath79_gpio_ctrl
, gc
);
186 unsigned long flags
, pending
;
187 u32 both_edges
, state
;
190 chained_irq_enter(irqchip
, desc
);
192 raw_spin_lock_irqsave(&ctrl
->lock
, flags
);
194 pending
= ath79_gpio_read(ctrl
, AR71XX_GPIO_REG_INT_PENDING
);
196 /* Update the polarity of the both edges irqs */
197 both_edges
= ctrl
->both_edges
& pending
;
199 state
= ath79_gpio_read(ctrl
, AR71XX_GPIO_REG_IN
);
200 ath79_gpio_update_bits(ctrl
, AR71XX_GPIO_REG_INT_POLARITY
,
204 raw_spin_unlock_irqrestore(&ctrl
->lock
, flags
);
207 for_each_set_bit(irq
, &pending
, gc
->ngpio
)
209 irq_linear_revmap(gc
->irq
.domain
, irq
));
212 chained_irq_exit(irqchip
, desc
);
215 static const struct of_device_id ath79_gpio_of_match
[] = {
216 { .compatible
= "qca,ar7100-gpio" },
217 { .compatible
= "qca,ar9340-gpio" },
220 MODULE_DEVICE_TABLE(of
, ath79_gpio_of_match
);
222 static int ath79_gpio_probe(struct platform_device
*pdev
)
224 struct ath79_gpio_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
225 struct device
*dev
= &pdev
->dev
;
226 struct device_node
*np
= dev
->of_node
;
227 struct ath79_gpio_ctrl
*ctrl
;
228 struct gpio_irq_chip
*girq
;
229 struct resource
*res
;
230 u32 ath79_gpio_count
;
234 ctrl
= devm_kzalloc(dev
, sizeof(*ctrl
), GFP_KERNEL
);
237 platform_set_drvdata(pdev
, ctrl
);
240 err
= of_property_read_u32(np
, "ngpios", &ath79_gpio_count
);
242 dev_err(dev
, "ngpios property is not valid\n");
245 oe_inverted
= of_device_is_compatible(np
, "qca,ar9340-gpio");
247 ath79_gpio_count
= pdata
->ngpios
;
248 oe_inverted
= pdata
->oe_inverted
;
250 dev_err(dev
, "No DT node or platform data found\n");
254 if (ath79_gpio_count
>= 32) {
255 dev_err(dev
, "ngpios must be less than 32\n");
259 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
262 ctrl
->base
= devm_ioremap_nocache(dev
, res
->start
, resource_size(res
));
266 raw_spin_lock_init(&ctrl
->lock
);
267 err
= bgpio_init(&ctrl
->gc
, dev
, 4,
268 ctrl
->base
+ AR71XX_GPIO_REG_IN
,
269 ctrl
->base
+ AR71XX_GPIO_REG_SET
,
270 ctrl
->base
+ AR71XX_GPIO_REG_CLEAR
,
271 oe_inverted
? NULL
: ctrl
->base
+ AR71XX_GPIO_REG_OE
,
272 oe_inverted
? ctrl
->base
+ AR71XX_GPIO_REG_OE
: NULL
,
275 dev_err(dev
, "bgpio_init failed\n");
278 /* Use base 0 to stay compatible with legacy platforms */
281 /* Optional interrupt setup */
282 if (!np
|| of_property_read_bool(np
, "interrupt-controller")) {
283 girq
= &ctrl
->gc
.irq
;
284 girq
->chip
= &ath79_gpio_irqchip
;
285 girq
->parent_handler
= ath79_gpio_irq_handler
;
286 girq
->num_parents
= 1;
287 girq
->parents
= devm_kcalloc(dev
, 1, sizeof(*girq
->parents
),
291 girq
->parents
[0] = platform_get_irq(pdev
, 0);
292 girq
->default_type
= IRQ_TYPE_NONE
;
293 girq
->handler
= handle_simple_irq
;
296 err
= devm_gpiochip_add_data(dev
, &ctrl
->gc
, ctrl
);
299 "cannot add AR71xx GPIO chip, error=%d", err
);
305 static struct platform_driver ath79_gpio_driver
= {
307 .name
= "ath79-gpio",
308 .of_match_table
= ath79_gpio_of_match
,
310 .probe
= ath79_gpio_probe
,
313 module_platform_driver(ath79_gpio_driver
);
315 MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X GPIO API support");
316 MODULE_LICENSE("GPL v2");