1 // SPDX-License-Identifier: GPL-2.0
2 /* Driver for IDT/Renesas 79RC3243x Interrupt Controller */
4 #include <linux/bitops.h>
5 #include <linux/gpio/driver.h>
7 #include <linux/module.h>
8 #include <linux/mod_devicetable.h>
9 #include <linux/platform_device.h>
10 #include <linux/spinlock.h>
12 #define IDT_PIC_IRQ_PEND 0x00
13 #define IDT_PIC_IRQ_MASK 0x08
15 #define IDT_GPIO_DIR 0x00
16 #define IDT_GPIO_DATA 0x04
17 #define IDT_GPIO_ILEVEL 0x08
18 #define IDT_GPIO_ISTAT 0x0C
20 struct idt_gpio_ctrl
{
27 static void idt_gpio_dispatch(struct irq_desc
*desc
)
29 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
30 struct idt_gpio_ctrl
*ctrl
= gpiochip_get_data(gc
);
31 struct irq_chip
*host_chip
= irq_desc_get_chip(desc
);
32 unsigned int bit
, virq
;
33 unsigned long pending
;
35 chained_irq_enter(host_chip
, desc
);
37 pending
= readl(ctrl
->pic
+ IDT_PIC_IRQ_PEND
);
38 pending
&= ~ctrl
->mask_cache
;
39 for_each_set_bit(bit
, &pending
, gc
->ngpio
) {
40 virq
= irq_linear_revmap(gc
->irq
.domain
, bit
);
42 generic_handle_irq(virq
);
45 chained_irq_exit(host_chip
, desc
);
48 static int idt_gpio_irq_set_type(struct irq_data
*d
, unsigned int flow_type
)
50 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
51 struct idt_gpio_ctrl
*ctrl
= gpiochip_get_data(gc
);
52 unsigned int sense
= flow_type
& IRQ_TYPE_SENSE_MASK
;
56 /* hardware only supports level triggered */
57 if (sense
== IRQ_TYPE_NONE
|| (sense
& IRQ_TYPE_EDGE_BOTH
))
60 raw_spin_lock_irqsave(&gc
->bgpio_lock
, flags
);
62 ilevel
= readl(ctrl
->gpio
+ IDT_GPIO_ILEVEL
);
63 if (sense
& IRQ_TYPE_LEVEL_HIGH
)
64 ilevel
|= BIT(d
->hwirq
);
65 else if (sense
& IRQ_TYPE_LEVEL_LOW
)
66 ilevel
&= ~BIT(d
->hwirq
);
68 writel(ilevel
, ctrl
->gpio
+ IDT_GPIO_ILEVEL
);
69 irq_set_handler_locked(d
, handle_level_irq
);
71 raw_spin_unlock_irqrestore(&gc
->bgpio_lock
, flags
);
75 static void idt_gpio_ack(struct irq_data
*d
)
77 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
78 struct idt_gpio_ctrl
*ctrl
= gpiochip_get_data(gc
);
80 writel(~BIT(d
->hwirq
), ctrl
->gpio
+ IDT_GPIO_ISTAT
);
83 static void idt_gpio_mask(struct irq_data
*d
)
85 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
86 struct idt_gpio_ctrl
*ctrl
= gpiochip_get_data(gc
);
89 raw_spin_lock_irqsave(&gc
->bgpio_lock
, flags
);
91 ctrl
->mask_cache
|= BIT(d
->hwirq
);
92 writel(ctrl
->mask_cache
, ctrl
->pic
+ IDT_PIC_IRQ_MASK
);
94 raw_spin_unlock_irqrestore(&gc
->bgpio_lock
, flags
);
96 gpiochip_disable_irq(gc
, irqd_to_hwirq(d
));
99 static void idt_gpio_unmask(struct irq_data
*d
)
101 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
102 struct idt_gpio_ctrl
*ctrl
= gpiochip_get_data(gc
);
105 gpiochip_enable_irq(gc
, irqd_to_hwirq(d
));
106 raw_spin_lock_irqsave(&gc
->bgpio_lock
, flags
);
108 ctrl
->mask_cache
&= ~BIT(d
->hwirq
);
109 writel(ctrl
->mask_cache
, ctrl
->pic
+ IDT_PIC_IRQ_MASK
);
111 raw_spin_unlock_irqrestore(&gc
->bgpio_lock
, flags
);
114 static int idt_gpio_irq_init_hw(struct gpio_chip
*gc
)
116 struct idt_gpio_ctrl
*ctrl
= gpiochip_get_data(gc
);
118 /* Mask interrupts. */
119 ctrl
->mask_cache
= 0xffffffff;
120 writel(ctrl
->mask_cache
, ctrl
->pic
+ IDT_PIC_IRQ_MASK
);
125 static const struct irq_chip idt_gpio_irqchip
= {
127 .irq_mask
= idt_gpio_mask
,
128 .irq_ack
= idt_gpio_ack
,
129 .irq_unmask
= idt_gpio_unmask
,
130 .irq_set_type
= idt_gpio_irq_set_type
,
131 .flags
= IRQCHIP_IMMUTABLE
,
132 GPIOCHIP_IRQ_RESOURCE_HELPERS
,
135 static int idt_gpio_probe(struct platform_device
*pdev
)
137 struct device
*dev
= &pdev
->dev
;
138 struct gpio_irq_chip
*girq
;
139 struct idt_gpio_ctrl
*ctrl
;
145 ctrl
= devm_kzalloc(dev
, sizeof(*ctrl
), GFP_KERNEL
);
149 ctrl
->gpio
= devm_platform_ioremap_resource_byname(pdev
, "gpio");
150 if (IS_ERR(ctrl
->gpio
))
151 return PTR_ERR(ctrl
->gpio
);
153 ctrl
->gc
.parent
= dev
;
155 ret
= bgpio_init(&ctrl
->gc
, &pdev
->dev
, 4, ctrl
->gpio
+ IDT_GPIO_DATA
,
156 NULL
, NULL
, ctrl
->gpio
+ IDT_GPIO_DIR
, NULL
, 0);
158 dev_err(dev
, "bgpio_init failed\n");
162 ret
= device_property_read_u32(dev
, "ngpios", &ngpios
);
164 ctrl
->gc
.ngpio
= ngpios
;
166 if (device_property_read_bool(dev
, "interrupt-controller")) {
167 ctrl
->pic
= devm_platform_ioremap_resource_byname(pdev
, "pic");
168 if (IS_ERR(ctrl
->pic
))
169 return PTR_ERR(ctrl
->pic
);
171 parent_irq
= platform_get_irq(pdev
, 0);
175 girq
= &ctrl
->gc
.irq
;
176 gpio_irq_chip_set_chip(girq
, &idt_gpio_irqchip
);
177 girq
->init_hw
= idt_gpio_irq_init_hw
;
178 girq
->parent_handler
= idt_gpio_dispatch
;
179 girq
->num_parents
= 1;
180 girq
->parents
= devm_kcalloc(dev
, girq
->num_parents
,
181 sizeof(*girq
->parents
),
186 girq
->parents
[0] = parent_irq
;
187 girq
->default_type
= IRQ_TYPE_NONE
;
188 girq
->handler
= handle_bad_irq
;
191 return devm_gpiochip_add_data(&pdev
->dev
, &ctrl
->gc
, ctrl
);
194 static const struct of_device_id idt_gpio_of_match
[] = {
195 { .compatible
= "idt,32434-gpio" },
198 MODULE_DEVICE_TABLE(of
, idt_gpio_of_match
);
200 static struct platform_driver idt_gpio_driver
= {
201 .probe
= idt_gpio_probe
,
203 .name
= "idt3243x-gpio",
204 .of_match_table
= idt_gpio_of_match
,
207 module_platform_driver(idt_gpio_driver
);
209 MODULE_DESCRIPTION("IDT 79RC3243x GPIO/PIC Driver");
210 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
211 MODULE_LICENSE("GPL");