Damien Riegel | d01f863 | 2015-12-21 15:11:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Multiplexed-IRQs driver for TS-4800's FPGA |
| 3 | * |
| 4 | * Copyright (c) 2015 - Savoir-faire Linux |
| 5 | * |
| 6 | * This file is licensed under the terms of the GNU General Public |
| 7 | * License version 2. This program is licensed "as is" without any |
| 8 | * warranty of any kind, whether express or implied. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/irq.h> |
| 14 | #include <linux/irqchip.h> |
| 15 | #include <linux/irqchip/chained_irq.h> |
| 16 | #include <linux/irqdomain.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/of_address.h> |
| 20 | #include <linux/of_irq.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | |
| 23 | #define IRQ_MASK 0x4 |
| 24 | #define IRQ_STATUS 0x8 |
| 25 | |
| 26 | struct ts4800_irq_data { |
| 27 | void __iomem *base; |
| 28 | struct irq_domain *domain; |
| 29 | struct irq_chip irq_chip; |
| 30 | }; |
| 31 | |
| 32 | static void ts4800_irq_mask(struct irq_data *d) |
| 33 | { |
| 34 | struct ts4800_irq_data *data = irq_data_get_irq_chip_data(d); |
| 35 | u16 reg = readw(data->base + IRQ_MASK); |
| 36 | u16 mask = 1 << d->hwirq; |
| 37 | |
| 38 | writew(reg | mask, data->base + IRQ_MASK); |
| 39 | } |
| 40 | |
| 41 | static void ts4800_irq_unmask(struct irq_data *d) |
| 42 | { |
| 43 | struct ts4800_irq_data *data = irq_data_get_irq_chip_data(d); |
| 44 | u16 reg = readw(data->base + IRQ_MASK); |
| 45 | u16 mask = 1 << d->hwirq; |
| 46 | |
| 47 | writew(reg & ~mask, data->base + IRQ_MASK); |
| 48 | } |
| 49 | |
| 50 | static int ts4800_irqdomain_map(struct irq_domain *d, unsigned int irq, |
| 51 | irq_hw_number_t hwirq) |
| 52 | { |
| 53 | struct ts4800_irq_data *data = d->host_data; |
| 54 | |
| 55 | irq_set_chip_and_handler(irq, &data->irq_chip, handle_simple_irq); |
| 56 | irq_set_chip_data(irq, data); |
| 57 | irq_set_noprobe(irq); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Axel Lin | e4e1c0e | 2016-02-14 21:50:04 +0800 | [diff] [blame] | 62 | static const struct irq_domain_ops ts4800_ic_ops = { |
Damien Riegel | d01f863 | 2015-12-21 15:11:23 -0500 | [diff] [blame] | 63 | .map = ts4800_irqdomain_map, |
| 64 | .xlate = irq_domain_xlate_onecell, |
| 65 | }; |
| 66 | |
| 67 | static void ts4800_ic_chained_handle_irq(struct irq_desc *desc) |
| 68 | { |
| 69 | struct ts4800_irq_data *data = irq_desc_get_handler_data(desc); |
| 70 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 71 | u16 status = readw(data->base + IRQ_STATUS); |
| 72 | |
| 73 | chained_irq_enter(chip, desc); |
| 74 | |
| 75 | if (unlikely(status == 0)) { |
| 76 | handle_bad_irq(desc); |
| 77 | goto out; |
| 78 | } |
| 79 | |
| 80 | do { |
| 81 | unsigned int bit = __ffs(status); |
Damien Riegel | d01f863 | 2015-12-21 15:11:23 -0500 | [diff] [blame] | 82 | |
Marc Zyngier | 046a6ee | 2021-05-04 17:42:18 +0100 | [diff] [blame] | 83 | generic_handle_domain_irq(data->domain, bit); |
Damien Riegel | d01f863 | 2015-12-21 15:11:23 -0500 | [diff] [blame] | 84 | status &= ~(1 << bit); |
Damien Riegel | d01f863 | 2015-12-21 15:11:23 -0500 | [diff] [blame] | 85 | } while (status); |
| 86 | |
| 87 | out: |
| 88 | chained_irq_exit(chip, desc); |
| 89 | } |
| 90 | |
| 91 | static int ts4800_ic_probe(struct platform_device *pdev) |
| 92 | { |
| 93 | struct device_node *node = pdev->dev.of_node; |
| 94 | struct ts4800_irq_data *data; |
| 95 | struct irq_chip *irq_chip; |
Damien Riegel | d01f863 | 2015-12-21 15:11:23 -0500 | [diff] [blame] | 96 | int parent_irq; |
| 97 | |
| 98 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 99 | if (!data) |
| 100 | return -ENOMEM; |
| 101 | |
Cai Huoqing | 2687bf8 | 2021-09-08 18:57:07 +0800 | [diff] [blame] | 102 | data->base = devm_platform_ioremap_resource(pdev, 0); |
Damien Riegel | d01f863 | 2015-12-21 15:11:23 -0500 | [diff] [blame] | 103 | if (IS_ERR(data->base)) |
| 104 | return PTR_ERR(data->base); |
| 105 | |
| 106 | writew(0xFFFF, data->base + IRQ_MASK); |
| 107 | |
| 108 | parent_irq = irq_of_parse_and_map(node, 0); |
| 109 | if (!parent_irq) { |
| 110 | dev_err(&pdev->dev, "failed to get parent IRQ\n"); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
| 114 | irq_chip = &data->irq_chip; |
| 115 | irq_chip->name = dev_name(&pdev->dev); |
| 116 | irq_chip->irq_mask = ts4800_irq_mask; |
| 117 | irq_chip->irq_unmask = ts4800_irq_unmask; |
| 118 | |
| 119 | data->domain = irq_domain_add_linear(node, 8, &ts4800_ic_ops, data); |
| 120 | if (!data->domain) { |
| 121 | dev_err(&pdev->dev, "cannot add IRQ domain\n"); |
| 122 | return -ENOMEM; |
| 123 | } |
| 124 | |
| 125 | irq_set_chained_handler_and_data(parent_irq, |
| 126 | ts4800_ic_chained_handle_irq, data); |
| 127 | |
| 128 | platform_set_drvdata(pdev, data); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int ts4800_ic_remove(struct platform_device *pdev) |
| 134 | { |
| 135 | struct ts4800_irq_data *data = platform_get_drvdata(pdev); |
| 136 | |
| 137 | irq_domain_remove(data->domain); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static const struct of_device_id ts4800_ic_of_match[] = { |
| 143 | { .compatible = "technologic,ts4800-irqc", }, |
| 144 | {}, |
| 145 | }; |
| 146 | MODULE_DEVICE_TABLE(of, ts4800_ic_of_match); |
| 147 | |
| 148 | static struct platform_driver ts4800_ic_driver = { |
| 149 | .probe = ts4800_ic_probe, |
| 150 | .remove = ts4800_ic_remove, |
| 151 | .driver = { |
| 152 | .name = "ts4800-irqc", |
| 153 | .of_match_table = ts4800_ic_of_match, |
| 154 | }, |
| 155 | }; |
| 156 | module_platform_driver(ts4800_ic_driver); |
| 157 | |
| 158 | MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>"); |
| 159 | MODULE_LICENSE("GPL v2"); |
| 160 | MODULE_ALIAS("platform:ts4800_irqc"); |