blob: 11abc09ef76c62b552bbb538d77c41bfe8c5ab66 [file] [log] [blame]
Kuninori Morimotoe25a96d2018-12-05 08:24:48 +00001// SPDX-License-Identifier: GPL-2.0
Magnus Dammfbc83b72013-02-27 17:15:01 +09002/*
3 * Renesas IRQC Driver
4 *
5 * Copyright (C) 2013 Magnus Damm
Magnus Dammfbc83b72013-02-27 17:15:01 +09006 */
7
8#include <linux/init.h>
9#include <linux/platform_device.h>
Magnus Dammfbc83b72013-02-27 17:15:01 +090010#include <linux/interrupt.h>
11#include <linux/ioport.h>
12#include <linux/io.h>
13#include <linux/irq.h>
14#include <linux/irqdomain.h>
15#include <linux/err.h>
16#include <linux/slab.h>
17#include <linux/module.h>
Geert Uytterhoeven51b05f62015-03-18 19:55:56 +010018#include <linux/pm_runtime.h>
Magnus Dammfbc83b72013-02-27 17:15:01 +090019
Geert Uytterhoeven1cd5ec732015-03-18 19:55:55 +010020#define IRQC_IRQ_MAX 32 /* maximum 32 interrupts per driver instance */
Magnus Dammfbc83b72013-02-27 17:15:01 +090021
Geert Uytterhoeven1cd5ec732015-03-18 19:55:55 +010022#define IRQC_REQ_STS 0x00 /* Interrupt Request Status Register */
23#define IRQC_EN_STS 0x04 /* Interrupt Enable Status Register */
24#define IRQC_EN_SET 0x08 /* Interrupt Enable Set Register */
Magnus Dammfbc83b72013-02-27 17:15:01 +090025#define IRQC_INT_CPU_BASE(n) (0x000 + ((n) * 0x10))
Geert Uytterhoeven1cd5ec732015-03-18 19:55:55 +010026 /* SYS-CPU vs. RT-CPU */
27#define DETECT_STATUS 0x100 /* IRQn Detect Status Register */
28#define MONITOR 0x104 /* IRQn Signal Level Monitor Register */
29#define HLVL_STS 0x108 /* IRQn High Level Detect Status Register */
30#define LLVL_STS 0x10c /* IRQn Low Level Detect Status Register */
31#define S_R_EDGE_STS 0x110 /* IRQn Sync Rising Edge Detect Status Reg. */
32#define S_F_EDGE_STS 0x114 /* IRQn Sync Falling Edge Detect Status Reg. */
33#define A_R_EDGE_STS 0x118 /* IRQn Async Rising Edge Detect Status Reg. */
34#define A_F_EDGE_STS 0x11c /* IRQn Async Falling Edge Detect Status Reg. */
35#define CHTEN_STS 0x120 /* Chattering Reduction Status Register */
Magnus Dammfbc83b72013-02-27 17:15:01 +090036#define IRQC_CONFIG(n) (0x180 + ((n) * 0x04))
Geert Uytterhoeven1cd5ec732015-03-18 19:55:55 +010037 /* IRQn Configuration Register */
Magnus Dammfbc83b72013-02-27 17:15:01 +090038
39struct irqc_irq {
40 int hw_irq;
41 int requested_irq;
Magnus Dammfbc83b72013-02-27 17:15:01 +090042 struct irqc_priv *p;
43};
44
45struct irqc_priv {
46 void __iomem *iomem;
47 void __iomem *cpu_int_base;
48 struct irqc_irq irq[IRQC_IRQ_MAX];
Magnus Dammfbc83b72013-02-27 17:15:01 +090049 unsigned int number_of_irqs;
Geert Uytterhoeven000e20c2019-05-27 14:04:11 +020050 struct device *dev;
Magnus Damm99c221d2015-09-28 18:42:37 +090051 struct irq_chip_generic *gc;
Magnus Dammfbc83b72013-02-27 17:15:01 +090052 struct irq_domain *irq_domain;
Geert Uytterhoeven734e0362018-02-12 14:55:12 +010053 atomic_t wakeup_path;
Magnus Dammfbc83b72013-02-27 17:15:01 +090054};
55
Magnus Damm99c221d2015-09-28 18:42:37 +090056static struct irqc_priv *irq_data_to_priv(struct irq_data *data)
57{
58 return data->domain->host_data;
59}
60
Magnus Dammfbc83b72013-02-27 17:15:01 +090061static void irqc_dbg(struct irqc_irq *i, char *str)
62{
Geert Uytterhoeven000e20c2019-05-27 14:04:11 +020063 dev_dbg(i->p->dev, "%s (%d:%d)\n", str, i->requested_irq, i->hw_irq);
Magnus Dammfbc83b72013-02-27 17:15:01 +090064}
65
Magnus Dammfbc83b72013-02-27 17:15:01 +090066static unsigned char irqc_sense[IRQ_TYPE_SENSE_MASK + 1] = {
Sergei Shtylyovce70af12013-12-14 03:09:31 +030067 [IRQ_TYPE_LEVEL_LOW] = 0x01,
68 [IRQ_TYPE_LEVEL_HIGH] = 0x02,
69 [IRQ_TYPE_EDGE_FALLING] = 0x04, /* Synchronous */
70 [IRQ_TYPE_EDGE_RISING] = 0x08, /* Synchronous */
71 [IRQ_TYPE_EDGE_BOTH] = 0x0c, /* Synchronous */
Magnus Dammfbc83b72013-02-27 17:15:01 +090072};
73
74static int irqc_irq_set_type(struct irq_data *d, unsigned int type)
75{
Magnus Damm99c221d2015-09-28 18:42:37 +090076 struct irqc_priv *p = irq_data_to_priv(d);
Magnus Dammfbc83b72013-02-27 17:15:01 +090077 int hw_irq = irqd_to_hwirq(d);
78 unsigned char value = irqc_sense[type & IRQ_TYPE_SENSE_MASK];
Geert Uytterhoevenf791e3c2015-02-26 11:43:32 +010079 u32 tmp;
Magnus Dammfbc83b72013-02-27 17:15:01 +090080
81 irqc_dbg(&p->irq[hw_irq], "sense");
82
Sergei Shtylyovce70af12013-12-14 03:09:31 +030083 if (!value)
Magnus Dammfbc83b72013-02-27 17:15:01 +090084 return -EINVAL;
85
86 tmp = ioread32(p->iomem + IRQC_CONFIG(hw_irq));
87 tmp &= ~0x3f;
Sergei Shtylyovce70af12013-12-14 03:09:31 +030088 tmp |= value;
Magnus Dammfbc83b72013-02-27 17:15:01 +090089 iowrite32(tmp, p->iomem + IRQC_CONFIG(hw_irq));
90 return 0;
91}
92
Geert Uytterhoeven6f46aed2015-04-01 14:00:06 +020093static int irqc_irq_set_wake(struct irq_data *d, unsigned int on)
94{
Magnus Damm99c221d2015-09-28 18:42:37 +090095 struct irqc_priv *p = irq_data_to_priv(d);
Geert Uytterhoeven4cd78632015-09-08 19:00:36 +020096 int hw_irq = irqd_to_hwirq(d);
97
98 irq_set_irq_wake(p->irq[hw_irq].requested_irq, on);
Geert Uytterhoeven6f46aed2015-04-01 14:00:06 +020099 if (on)
Geert Uytterhoeven734e0362018-02-12 14:55:12 +0100100 atomic_inc(&p->wakeup_path);
Geert Uytterhoeven6f46aed2015-04-01 14:00:06 +0200101 else
Geert Uytterhoeven734e0362018-02-12 14:55:12 +0100102 atomic_dec(&p->wakeup_path);
Geert Uytterhoeven6f46aed2015-04-01 14:00:06 +0200103
104 return 0;
105}
106
Magnus Dammfbc83b72013-02-27 17:15:01 +0900107static irqreturn_t irqc_irq_handler(int irq, void *dev_id)
108{
109 struct irqc_irq *i = dev_id;
110 struct irqc_priv *p = i->p;
Geert Uytterhoevenf791e3c2015-02-26 11:43:32 +0100111 u32 bit = BIT(i->hw_irq);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900112
113 irqc_dbg(i, "demux1");
114
115 if (ioread32(p->iomem + DETECT_STATUS) & bit) {
116 iowrite32(bit, p->iomem + DETECT_STATUS);
117 irqc_dbg(i, "demux2");
Magnus Damme10fc032015-07-20 19:06:35 +0900118 generic_handle_irq(irq_find_mapping(p->irq_domain, i->hw_irq));
Magnus Dammfbc83b72013-02-27 17:15:01 +0900119 return IRQ_HANDLED;
120 }
121 return IRQ_NONE;
122}
123
Magnus Dammfbc83b72013-02-27 17:15:01 +0900124static int irqc_probe(struct platform_device *pdev)
125{
Geert Uytterhoeven9ae48bc2019-05-27 14:04:10 +0200126 struct device *dev = &pdev->dev;
127 const char *name = dev_name(dev);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900128 struct irqc_priv *p;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900129 struct resource *irq;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900130 int ret;
131 int k;
132
Geert Uytterhoeven47705332019-05-27 14:04:12 +0200133 p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
134 if (!p)
135 return -ENOMEM;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900136
Geert Uytterhoeven000e20c2019-05-27 14:04:11 +0200137 p->dev = dev;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900138 platform_set_drvdata(pdev, p);
139
Geert Uytterhoeven9ae48bc2019-05-27 14:04:10 +0200140 pm_runtime_enable(dev);
141 pm_runtime_get_sync(dev);
Geert Uytterhoeven51b05f62015-03-18 19:55:56 +0100142
Magnus Dammfbc83b72013-02-27 17:15:01 +0900143 /* allow any number of IRQs between 1 and IRQC_IRQ_MAX */
144 for (k = 0; k < IRQC_IRQ_MAX; k++) {
145 irq = platform_get_resource(pdev, IORESOURCE_IRQ, k);
146 if (!irq)
147 break;
148
149 p->irq[k].p = p;
Magnus Damme10fc032015-07-20 19:06:35 +0900150 p->irq[k].hw_irq = k;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900151 p->irq[k].requested_irq = irq->start;
152 }
153
154 p->number_of_irqs = k;
155 if (p->number_of_irqs < 1) {
Geert Uytterhoeven9ae48bc2019-05-27 14:04:10 +0200156 dev_err(dev, "not enough IRQ resources\n");
Magnus Dammfbc83b72013-02-27 17:15:01 +0900157 ret = -EINVAL;
Geert Uytterhoeven47705332019-05-27 14:04:12 +0200158 goto err_runtime_pm_disable;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900159 }
160
161 /* ioremap IOMEM and setup read/write callbacks */
Geert Uytterhoeven47705332019-05-27 14:04:12 +0200162 p->iomem = devm_platform_ioremap_resource(pdev, 0);
163 if (IS_ERR(p->iomem)) {
164 ret = PTR_ERR(p->iomem);
165 goto err_runtime_pm_disable;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900166 }
167
168 p->cpu_int_base = p->iomem + IRQC_INT_CPU_BASE(0); /* SYS-SPI */
169
Geert Uytterhoeven9ae48bc2019-05-27 14:04:10 +0200170 p->irq_domain = irq_domain_add_linear(dev->of_node, p->number_of_irqs,
Magnus Damm99c221d2015-09-28 18:42:37 +0900171 &irq_generic_chip_ops, p);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900172 if (!p->irq_domain) {
173 ret = -ENXIO;
Geert Uytterhoeven9ae48bc2019-05-27 14:04:10 +0200174 dev_err(dev, "cannot initialize irq domain\n");
Geert Uytterhoeven47705332019-05-27 14:04:12 +0200175 goto err_runtime_pm_disable;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900176 }
177
Magnus Damm99c221d2015-09-28 18:42:37 +0900178 ret = irq_alloc_domain_generic_chips(p->irq_domain, p->number_of_irqs,
Geert Uytterhoeven17c88892019-06-07 11:58:56 +0200179 1, "irqc", handle_level_irq,
Magnus Damm99c221d2015-09-28 18:42:37 +0900180 0, 0, IRQ_GC_INIT_NESTED_LOCK);
181 if (ret) {
Geert Uytterhoeven9ae48bc2019-05-27 14:04:10 +0200182 dev_err(dev, "cannot allocate generic chip\n");
Geert Uytterhoeven47705332019-05-27 14:04:12 +0200183 goto err_remove_domain;
Magnus Damm99c221d2015-09-28 18:42:37 +0900184 }
185
186 p->gc = irq_get_domain_generic_chip(p->irq_domain, 0);
187 p->gc->reg_base = p->cpu_int_base;
188 p->gc->chip_types[0].regs.enable = IRQC_EN_SET;
189 p->gc->chip_types[0].regs.disable = IRQC_EN_STS;
Geert Uytterhoeven17c88892019-06-07 11:58:56 +0200190 p->gc->chip_types[0].chip.parent_device = dev;
Magnus Damm99c221d2015-09-28 18:42:37 +0900191 p->gc->chip_types[0].chip.irq_mask = irq_gc_mask_disable_reg;
192 p->gc->chip_types[0].chip.irq_unmask = irq_gc_unmask_enable_reg;
193 p->gc->chip_types[0].chip.irq_set_type = irqc_irq_set_type;
194 p->gc->chip_types[0].chip.irq_set_wake = irqc_irq_set_wake;
195 p->gc->chip_types[0].chip.flags = IRQCHIP_MASK_ON_SUSPEND;
196
Magnus Dammfbc83b72013-02-27 17:15:01 +0900197 /* request interrupts one by one */
198 for (k = 0; k < p->number_of_irqs; k++) {
Geert Uytterhoeven47705332019-05-27 14:04:12 +0200199 if (devm_request_irq(dev, p->irq[k].requested_irq,
200 irqc_irq_handler, 0, name, &p->irq[k])) {
Geert Uytterhoeven9ae48bc2019-05-27 14:04:10 +0200201 dev_err(dev, "failed to request IRQ\n");
Magnus Dammfbc83b72013-02-27 17:15:01 +0900202 ret = -ENOENT;
Geert Uytterhoeven47705332019-05-27 14:04:12 +0200203 goto err_remove_domain;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900204 }
205 }
206
Geert Uytterhoeven9ae48bc2019-05-27 14:04:10 +0200207 dev_info(dev, "driving %d irqs\n", p->number_of_irqs);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900208
Magnus Dammfbc83b72013-02-27 17:15:01 +0900209 return 0;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900210
Geert Uytterhoeven47705332019-05-27 14:04:12 +0200211err_remove_domain:
Magnus Dammfbc83b72013-02-27 17:15:01 +0900212 irq_domain_remove(p->irq_domain);
Geert Uytterhoeven47705332019-05-27 14:04:12 +0200213err_runtime_pm_disable:
Geert Uytterhoeven9ae48bc2019-05-27 14:04:10 +0200214 pm_runtime_put(dev);
215 pm_runtime_disable(dev);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900216 return ret;
217}
218
219static int irqc_remove(struct platform_device *pdev)
220{
221 struct irqc_priv *p = platform_get_drvdata(pdev);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900222
223 irq_domain_remove(p->irq_domain);
Geert Uytterhoeven51b05f62015-03-18 19:55:56 +0100224 pm_runtime_put(&pdev->dev);
225 pm_runtime_disable(&pdev->dev);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900226 return 0;
227}
228
Geert Uytterhoeven734e0362018-02-12 14:55:12 +0100229static int __maybe_unused irqc_suspend(struct device *dev)
230{
231 struct irqc_priv *p = dev_get_drvdata(dev);
232
233 if (atomic_read(&p->wakeup_path))
234 device_set_wakeup_path(dev);
235
236 return 0;
237}
238
239static SIMPLE_DEV_PM_OPS(irqc_pm_ops, irqc_suspend, NULL);
240
Magnus Damm3b8dfa72013-03-06 15:23:39 +0900241static const struct of_device_id irqc_dt_ids[] = {
242 { .compatible = "renesas,irqc", },
243 {},
244};
245MODULE_DEVICE_TABLE(of, irqc_dt_ids);
246
Magnus Dammfbc83b72013-02-27 17:15:01 +0900247static struct platform_driver irqc_device_driver = {
248 .probe = irqc_probe,
249 .remove = irqc_remove,
250 .driver = {
251 .name = "renesas_irqc",
Magnus Damm3b8dfa72013-03-06 15:23:39 +0900252 .of_match_table = irqc_dt_ids,
Geert Uytterhoeven734e0362018-02-12 14:55:12 +0100253 .pm = &irqc_pm_ops,
Magnus Dammfbc83b72013-02-27 17:15:01 +0900254 }
255};
256
257static int __init irqc_init(void)
258{
259 return platform_driver_register(&irqc_device_driver);
260}
261postcore_initcall(irqc_init);
262
263static void __exit irqc_exit(void)
264{
265 platform_driver_unregister(&irqc_device_driver);
266}
267module_exit(irqc_exit);
268
269MODULE_AUTHOR("Magnus Damm");
270MODULE_DESCRIPTION("Renesas IRQC Driver");
271MODULE_LICENSE("GPL v2");