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 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 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 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 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
97 static void idt_gpio_unmask(struct irq_data *d)
99 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
100 struct idt_gpio_ctrl *ctrl = gpiochip_get_data(gc);
103 spin_lock_irqsave(&gc->bgpio_lock, flags);
105 ctrl->mask_cache &= ~BIT(d->hwirq);
106 writel(ctrl->mask_cache, ctrl->pic + IDT_PIC_IRQ_MASK);
108 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
111 static int idt_gpio_irq_init_hw(struct gpio_chip *gc)
113 struct idt_gpio_ctrl *ctrl = gpiochip_get_data(gc);
115 /* Mask interrupts. */
116 ctrl->mask_cache = 0xffffffff;
117 writel(ctrl->mask_cache, ctrl->pic + IDT_PIC_IRQ_MASK);
122 static struct irq_chip idt_gpio_irqchip = {
124 .irq_mask = idt_gpio_mask,
125 .irq_ack = idt_gpio_ack,
126 .irq_unmask = idt_gpio_unmask,
127 .irq_set_type = idt_gpio_irq_set_type
130 static int idt_gpio_probe(struct platform_device *pdev)
132 struct device *dev = &pdev->dev;
133 struct gpio_irq_chip *girq;
134 struct idt_gpio_ctrl *ctrl;
135 unsigned int parent_irq;
140 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
144 ctrl->gpio = devm_platform_ioremap_resource_byname(pdev, "gpio");
145 if (IS_ERR(ctrl->gpio))
146 return PTR_ERR(ctrl->gpio);
148 ctrl->gc.parent = dev;
150 ret = bgpio_init(&ctrl->gc, &pdev->dev, 4, ctrl->gpio + IDT_GPIO_DATA,
151 NULL, NULL, ctrl->gpio + IDT_GPIO_DIR, NULL, 0);
153 dev_err(dev, "bgpio_init failed\n");
157 ret = device_property_read_u32(dev, "ngpios", &ngpios);
159 ctrl->gc.ngpio = ngpios;
161 if (device_property_read_bool(dev, "interrupt-controller")) {
162 ctrl->pic = devm_platform_ioremap_resource_byname(pdev, "pic");
163 if (IS_ERR(ctrl->pic))
164 return PTR_ERR(ctrl->pic);
166 parent_irq = platform_get_irq(pdev, 0);
170 girq = &ctrl->gc.irq;
171 girq->chip = &idt_gpio_irqchip;
172 girq->init_hw = idt_gpio_irq_init_hw;
173 girq->parent_handler = idt_gpio_dispatch;
174 girq->num_parents = 1;
175 girq->parents = devm_kcalloc(dev, girq->num_parents,
176 sizeof(*girq->parents),
181 girq->parents[0] = parent_irq;
182 girq->default_type = IRQ_TYPE_NONE;
183 girq->handler = handle_bad_irq;
186 return devm_gpiochip_add_data(&pdev->dev, &ctrl->gc, ctrl);
189 static const struct of_device_id idt_gpio_of_match[] = {
190 { .compatible = "idt,32434-gpio" },
193 MODULE_DEVICE_TABLE(of, idt_gpio_of_match);
195 static struct platform_driver idt_gpio_driver = {
196 .probe = idt_gpio_probe,
198 .name = "idt3243x-gpio",
199 .of_match_table = idt_gpio_of_match,
202 module_platform_driver(idt_gpio_driver);
204 MODULE_DESCRIPTION("IDT 79RC3243x GPIO/PIC Driver");
205 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
206 MODULE_LICENSE("GPL");