Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Renesas IRQC Driver |
| 3 | * |
| 4 | * Copyright (C) 2013 Magnus Damm |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/ioport.h> |
| 25 | #include <linux/io.h> |
| 26 | #include <linux/irq.h> |
| 27 | #include <linux/irqdomain.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/platform_data/irq-renesas-irqc.h> |
Geert Uytterhoeven | 51b05f6 | 2015-03-18 19:55:56 +0100 | [diff] [blame^] | 32 | #include <linux/pm_runtime.h> |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 33 | |
Geert Uytterhoeven | 1cd5ec73 | 2015-03-18 19:55:55 +0100 | [diff] [blame] | 34 | #define IRQC_IRQ_MAX 32 /* maximum 32 interrupts per driver instance */ |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 35 | |
Geert Uytterhoeven | 1cd5ec73 | 2015-03-18 19:55:55 +0100 | [diff] [blame] | 36 | #define IRQC_REQ_STS 0x00 /* Interrupt Request Status Register */ |
| 37 | #define IRQC_EN_STS 0x04 /* Interrupt Enable Status Register */ |
| 38 | #define IRQC_EN_SET 0x08 /* Interrupt Enable Set Register */ |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 39 | #define IRQC_INT_CPU_BASE(n) (0x000 + ((n) * 0x10)) |
Geert Uytterhoeven | 1cd5ec73 | 2015-03-18 19:55:55 +0100 | [diff] [blame] | 40 | /* SYS-CPU vs. RT-CPU */ |
| 41 | #define DETECT_STATUS 0x100 /* IRQn Detect Status Register */ |
| 42 | #define MONITOR 0x104 /* IRQn Signal Level Monitor Register */ |
| 43 | #define HLVL_STS 0x108 /* IRQn High Level Detect Status Register */ |
| 44 | #define LLVL_STS 0x10c /* IRQn Low Level Detect Status Register */ |
| 45 | #define S_R_EDGE_STS 0x110 /* IRQn Sync Rising Edge Detect Status Reg. */ |
| 46 | #define S_F_EDGE_STS 0x114 /* IRQn Sync Falling Edge Detect Status Reg. */ |
| 47 | #define A_R_EDGE_STS 0x118 /* IRQn Async Rising Edge Detect Status Reg. */ |
| 48 | #define A_F_EDGE_STS 0x11c /* IRQn Async Falling Edge Detect Status Reg. */ |
| 49 | #define CHTEN_STS 0x120 /* Chattering Reduction Status Register */ |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 50 | #define IRQC_CONFIG(n) (0x180 + ((n) * 0x04)) |
Geert Uytterhoeven | 1cd5ec73 | 2015-03-18 19:55:55 +0100 | [diff] [blame] | 51 | /* IRQn Configuration Register */ |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 52 | |
| 53 | struct irqc_irq { |
| 54 | int hw_irq; |
| 55 | int requested_irq; |
| 56 | int domain_irq; |
| 57 | struct irqc_priv *p; |
| 58 | }; |
| 59 | |
| 60 | struct irqc_priv { |
| 61 | void __iomem *iomem; |
| 62 | void __iomem *cpu_int_base; |
| 63 | struct irqc_irq irq[IRQC_IRQ_MAX]; |
| 64 | struct renesas_irqc_config config; |
| 65 | unsigned int number_of_irqs; |
| 66 | struct platform_device *pdev; |
| 67 | struct irq_chip irq_chip; |
| 68 | struct irq_domain *irq_domain; |
| 69 | }; |
| 70 | |
| 71 | static void irqc_dbg(struct irqc_irq *i, char *str) |
| 72 | { |
| 73 | dev_dbg(&i->p->pdev->dev, "%s (%d:%d:%d)\n", |
| 74 | str, i->requested_irq, i->hw_irq, i->domain_irq); |
| 75 | } |
| 76 | |
| 77 | static void irqc_irq_enable(struct irq_data *d) |
| 78 | { |
| 79 | struct irqc_priv *p = irq_data_get_irq_chip_data(d); |
| 80 | int hw_irq = irqd_to_hwirq(d); |
| 81 | |
| 82 | irqc_dbg(&p->irq[hw_irq], "enable"); |
| 83 | iowrite32(BIT(hw_irq), p->cpu_int_base + IRQC_EN_SET); |
| 84 | } |
| 85 | |
| 86 | static void irqc_irq_disable(struct irq_data *d) |
| 87 | { |
| 88 | struct irqc_priv *p = irq_data_get_irq_chip_data(d); |
| 89 | int hw_irq = irqd_to_hwirq(d); |
| 90 | |
| 91 | irqc_dbg(&p->irq[hw_irq], "disable"); |
| 92 | iowrite32(BIT(hw_irq), p->cpu_int_base + IRQC_EN_STS); |
| 93 | } |
| 94 | |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 95 | static unsigned char irqc_sense[IRQ_TYPE_SENSE_MASK + 1] = { |
Sergei Shtylyov | ce70af1 | 2013-12-14 03:09:31 +0300 | [diff] [blame] | 96 | [IRQ_TYPE_LEVEL_LOW] = 0x01, |
| 97 | [IRQ_TYPE_LEVEL_HIGH] = 0x02, |
| 98 | [IRQ_TYPE_EDGE_FALLING] = 0x04, /* Synchronous */ |
| 99 | [IRQ_TYPE_EDGE_RISING] = 0x08, /* Synchronous */ |
| 100 | [IRQ_TYPE_EDGE_BOTH] = 0x0c, /* Synchronous */ |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static int irqc_irq_set_type(struct irq_data *d, unsigned int type) |
| 104 | { |
| 105 | struct irqc_priv *p = irq_data_get_irq_chip_data(d); |
| 106 | int hw_irq = irqd_to_hwirq(d); |
| 107 | unsigned char value = irqc_sense[type & IRQ_TYPE_SENSE_MASK]; |
| 108 | unsigned long tmp; |
| 109 | |
| 110 | irqc_dbg(&p->irq[hw_irq], "sense"); |
| 111 | |
Sergei Shtylyov | ce70af1 | 2013-12-14 03:09:31 +0300 | [diff] [blame] | 112 | if (!value) |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 113 | return -EINVAL; |
| 114 | |
| 115 | tmp = ioread32(p->iomem + IRQC_CONFIG(hw_irq)); |
| 116 | tmp &= ~0x3f; |
Sergei Shtylyov | ce70af1 | 2013-12-14 03:09:31 +0300 | [diff] [blame] | 117 | tmp |= value; |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 118 | iowrite32(tmp, p->iomem + IRQC_CONFIG(hw_irq)); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static irqreturn_t irqc_irq_handler(int irq, void *dev_id) |
| 123 | { |
| 124 | struct irqc_irq *i = dev_id; |
| 125 | struct irqc_priv *p = i->p; |
| 126 | unsigned long bit = BIT(i->hw_irq); |
| 127 | |
| 128 | irqc_dbg(i, "demux1"); |
| 129 | |
| 130 | if (ioread32(p->iomem + DETECT_STATUS) & bit) { |
| 131 | iowrite32(bit, p->iomem + DETECT_STATUS); |
| 132 | irqc_dbg(i, "demux2"); |
| 133 | generic_handle_irq(i->domain_irq); |
| 134 | return IRQ_HANDLED; |
| 135 | } |
| 136 | return IRQ_NONE; |
| 137 | } |
| 138 | |
| 139 | static int irqc_irq_domain_map(struct irq_domain *h, unsigned int virq, |
| 140 | irq_hw_number_t hw) |
| 141 | { |
| 142 | struct irqc_priv *p = h->host_data; |
| 143 | |
| 144 | p->irq[hw].domain_irq = virq; |
| 145 | p->irq[hw].hw_irq = hw; |
| 146 | |
| 147 | irqc_dbg(&p->irq[hw], "map"); |
| 148 | irq_set_chip_data(virq, h->host_data); |
| 149 | irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq); |
| 150 | set_irq_flags(virq, IRQF_VALID); /* kill me now */ |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static struct irq_domain_ops irqc_irq_domain_ops = { |
| 155 | .map = irqc_irq_domain_map, |
Magnus Damm | 3b8dfa7 | 2013-03-06 15:23:39 +0900 | [diff] [blame] | 156 | .xlate = irq_domain_xlate_twocell, |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | static int irqc_probe(struct platform_device *pdev) |
| 160 | { |
| 161 | struct renesas_irqc_config *pdata = pdev->dev.platform_data; |
| 162 | struct irqc_priv *p; |
| 163 | struct resource *io; |
| 164 | struct resource *irq; |
| 165 | struct irq_chip *irq_chip; |
| 166 | const char *name = dev_name(&pdev->dev); |
| 167 | int ret; |
| 168 | int k; |
| 169 | |
| 170 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 171 | if (!p) { |
| 172 | dev_err(&pdev->dev, "failed to allocate driver data\n"); |
| 173 | ret = -ENOMEM; |
| 174 | goto err0; |
| 175 | } |
| 176 | |
| 177 | /* deal with driver instance configuration */ |
| 178 | if (pdata) |
| 179 | memcpy(&p->config, pdata, sizeof(*pdata)); |
| 180 | |
| 181 | p->pdev = pdev; |
| 182 | platform_set_drvdata(pdev, p); |
| 183 | |
Geert Uytterhoeven | 51b05f6 | 2015-03-18 19:55:56 +0100 | [diff] [blame^] | 184 | pm_runtime_enable(&pdev->dev); |
| 185 | pm_runtime_get_sync(&pdev->dev); |
| 186 | |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 187 | /* get hold of manadatory IOMEM */ |
| 188 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 189 | if (!io) { |
| 190 | dev_err(&pdev->dev, "not enough IOMEM resources\n"); |
| 191 | ret = -EINVAL; |
| 192 | goto err1; |
| 193 | } |
| 194 | |
| 195 | /* allow any number of IRQs between 1 and IRQC_IRQ_MAX */ |
| 196 | for (k = 0; k < IRQC_IRQ_MAX; k++) { |
| 197 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, k); |
| 198 | if (!irq) |
| 199 | break; |
| 200 | |
| 201 | p->irq[k].p = p; |
| 202 | p->irq[k].requested_irq = irq->start; |
| 203 | } |
| 204 | |
| 205 | p->number_of_irqs = k; |
| 206 | if (p->number_of_irqs < 1) { |
| 207 | dev_err(&pdev->dev, "not enough IRQ resources\n"); |
| 208 | ret = -EINVAL; |
| 209 | goto err1; |
| 210 | } |
| 211 | |
| 212 | /* ioremap IOMEM and setup read/write callbacks */ |
| 213 | p->iomem = ioremap_nocache(io->start, resource_size(io)); |
| 214 | if (!p->iomem) { |
| 215 | dev_err(&pdev->dev, "failed to remap IOMEM\n"); |
| 216 | ret = -ENXIO; |
| 217 | goto err2; |
| 218 | } |
| 219 | |
| 220 | p->cpu_int_base = p->iomem + IRQC_INT_CPU_BASE(0); /* SYS-SPI */ |
| 221 | |
| 222 | irq_chip = &p->irq_chip; |
| 223 | irq_chip->name = name; |
| 224 | irq_chip->irq_mask = irqc_irq_disable; |
| 225 | irq_chip->irq_unmask = irqc_irq_enable; |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 226 | irq_chip->irq_set_type = irqc_irq_set_type; |
Magnus Damm | 6a7e3b3 | 2013-12-04 21:05:56 +0900 | [diff] [blame] | 227 | irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND; |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 228 | |
| 229 | p->irq_domain = irq_domain_add_simple(pdev->dev.of_node, |
| 230 | p->number_of_irqs, |
| 231 | p->config.irq_base, |
| 232 | &irqc_irq_domain_ops, p); |
| 233 | if (!p->irq_domain) { |
| 234 | ret = -ENXIO; |
| 235 | dev_err(&pdev->dev, "cannot initialize irq domain\n"); |
| 236 | goto err2; |
| 237 | } |
| 238 | |
| 239 | /* request interrupts one by one */ |
| 240 | for (k = 0; k < p->number_of_irqs; k++) { |
| 241 | if (request_irq(p->irq[k].requested_irq, irqc_irq_handler, |
| 242 | 0, name, &p->irq[k])) { |
| 243 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
| 244 | ret = -ENOENT; |
| 245 | goto err3; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | dev_info(&pdev->dev, "driving %d irqs\n", p->number_of_irqs); |
| 250 | |
| 251 | /* warn in case of mismatch if irq base is specified */ |
| 252 | if (p->config.irq_base) { |
| 253 | if (p->config.irq_base != p->irq[0].domain_irq) |
| 254 | dev_warn(&pdev->dev, "irq base mismatch (%d/%d)\n", |
| 255 | p->config.irq_base, p->irq[0].domain_irq); |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | err3: |
Axel Lin | dfaf820 | 2013-05-06 17:03:32 +0800 | [diff] [blame] | 260 | while (--k >= 0) |
| 261 | free_irq(p->irq[k].requested_irq, &p->irq[k]); |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 262 | |
| 263 | irq_domain_remove(p->irq_domain); |
| 264 | err2: |
| 265 | iounmap(p->iomem); |
| 266 | err1: |
Geert Uytterhoeven | 51b05f6 | 2015-03-18 19:55:56 +0100 | [diff] [blame^] | 267 | pm_runtime_put(&pdev->dev); |
| 268 | pm_runtime_disable(&pdev->dev); |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 269 | kfree(p); |
| 270 | err0: |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | static int irqc_remove(struct platform_device *pdev) |
| 275 | { |
| 276 | struct irqc_priv *p = platform_get_drvdata(pdev); |
| 277 | int k; |
| 278 | |
| 279 | for (k = 0; k < p->number_of_irqs; k++) |
| 280 | free_irq(p->irq[k].requested_irq, &p->irq[k]); |
| 281 | |
| 282 | irq_domain_remove(p->irq_domain); |
| 283 | iounmap(p->iomem); |
Geert Uytterhoeven | 51b05f6 | 2015-03-18 19:55:56 +0100 | [diff] [blame^] | 284 | pm_runtime_put(&pdev->dev); |
| 285 | pm_runtime_disable(&pdev->dev); |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 286 | kfree(p); |
| 287 | return 0; |
| 288 | } |
| 289 | |
Magnus Damm | 3b8dfa7 | 2013-03-06 15:23:39 +0900 | [diff] [blame] | 290 | static const struct of_device_id irqc_dt_ids[] = { |
| 291 | { .compatible = "renesas,irqc", }, |
| 292 | {}, |
| 293 | }; |
| 294 | MODULE_DEVICE_TABLE(of, irqc_dt_ids); |
| 295 | |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 296 | static struct platform_driver irqc_device_driver = { |
| 297 | .probe = irqc_probe, |
| 298 | .remove = irqc_remove, |
| 299 | .driver = { |
| 300 | .name = "renesas_irqc", |
Magnus Damm | 3b8dfa7 | 2013-03-06 15:23:39 +0900 | [diff] [blame] | 301 | .of_match_table = irqc_dt_ids, |
Magnus Damm | fbc83b7 | 2013-02-27 17:15:01 +0900 | [diff] [blame] | 302 | } |
| 303 | }; |
| 304 | |
| 305 | static int __init irqc_init(void) |
| 306 | { |
| 307 | return platform_driver_register(&irqc_device_driver); |
| 308 | } |
| 309 | postcore_initcall(irqc_init); |
| 310 | |
| 311 | static void __exit irqc_exit(void) |
| 312 | { |
| 313 | platform_driver_unregister(&irqc_device_driver); |
| 314 | } |
| 315 | module_exit(irqc_exit); |
| 316 | |
| 317 | MODULE_AUTHOR("Magnus Damm"); |
| 318 | MODULE_DESCRIPTION("Renesas IRQC Driver"); |
| 319 | MODULE_LICENSE("GPL v2"); |