xref: /openbmc/linux/drivers/irqchip/irq-renesas-irqc.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1e25a96d7SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
2fbc83b7fSMagnus Damm /*
3fbc83b7fSMagnus Damm  * Renesas IRQC Driver
4fbc83b7fSMagnus Damm  *
5fbc83b7fSMagnus Damm  *  Copyright (C) 2013 Magnus Damm
6fbc83b7fSMagnus Damm  */
7fbc83b7fSMagnus Damm 
8fbc83b7fSMagnus Damm #include <linux/init.h>
9fbc83b7fSMagnus Damm #include <linux/platform_device.h>
10fbc83b7fSMagnus Damm #include <linux/interrupt.h>
11fbc83b7fSMagnus Damm #include <linux/ioport.h>
12fbc83b7fSMagnus Damm #include <linux/io.h>
13fbc83b7fSMagnus Damm #include <linux/irq.h>
14fbc83b7fSMagnus Damm #include <linux/irqdomain.h>
15fbc83b7fSMagnus Damm #include <linux/err.h>
16fbc83b7fSMagnus Damm #include <linux/slab.h>
17fbc83b7fSMagnus Damm #include <linux/module.h>
1851b05f6bSGeert Uytterhoeven #include <linux/pm_runtime.h>
19fbc83b7fSMagnus Damm 
20fbc83b7fSMagnus Damm #define IRQC_IRQ_MAX	32	/* maximum 32 interrupts per driver instance */
21fbc83b7fSMagnus Damm 
221cd5ec73SGeert Uytterhoeven #define IRQC_REQ_STS	0x00	/* Interrupt Request Status Register */
231cd5ec73SGeert Uytterhoeven #define IRQC_EN_STS	0x04	/* Interrupt Enable Status Register */
241cd5ec73SGeert Uytterhoeven #define IRQC_EN_SET	0x08	/* Interrupt Enable Set Register */
25fbc83b7fSMagnus Damm #define IRQC_INT_CPU_BASE(n) (0x000 + ((n) * 0x10))
261cd5ec73SGeert Uytterhoeven 				/* SYS-CPU vs. RT-CPU */
271cd5ec73SGeert Uytterhoeven #define DETECT_STATUS	0x100	/* IRQn Detect Status Register */
281cd5ec73SGeert Uytterhoeven #define MONITOR		0x104	/* IRQn Signal Level Monitor Register */
291cd5ec73SGeert Uytterhoeven #define HLVL_STS	0x108	/* IRQn High Level Detect Status Register */
301cd5ec73SGeert Uytterhoeven #define LLVL_STS	0x10c	/* IRQn Low Level Detect Status Register */
311cd5ec73SGeert Uytterhoeven #define S_R_EDGE_STS	0x110	/* IRQn Sync Rising Edge Detect Status Reg. */
321cd5ec73SGeert Uytterhoeven #define S_F_EDGE_STS	0x114	/* IRQn Sync Falling Edge Detect Status Reg. */
331cd5ec73SGeert Uytterhoeven #define A_R_EDGE_STS	0x118	/* IRQn Async Rising Edge Detect Status Reg. */
341cd5ec73SGeert Uytterhoeven #define A_F_EDGE_STS	0x11c	/* IRQn Async Falling Edge Detect Status Reg. */
351cd5ec73SGeert Uytterhoeven #define CHTEN_STS	0x120	/* Chattering Reduction Status Register */
36fbc83b7fSMagnus Damm #define IRQC_CONFIG(n) (0x180 + ((n) * 0x04))
371cd5ec73SGeert Uytterhoeven 				/* IRQn Configuration Register */
38fbc83b7fSMagnus Damm 
39fbc83b7fSMagnus Damm struct irqc_irq {
40fbc83b7fSMagnus Damm 	int hw_irq;
41fbc83b7fSMagnus Damm 	int requested_irq;
42fbc83b7fSMagnus Damm 	struct irqc_priv *p;
43fbc83b7fSMagnus Damm };
44fbc83b7fSMagnus Damm 
45fbc83b7fSMagnus Damm struct irqc_priv {
46fbc83b7fSMagnus Damm 	void __iomem *iomem;
47fbc83b7fSMagnus Damm 	void __iomem *cpu_int_base;
48fbc83b7fSMagnus Damm 	struct irqc_irq irq[IRQC_IRQ_MAX];
49fbc83b7fSMagnus Damm 	unsigned int number_of_irqs;
50000e20c5SGeert Uytterhoeven 	struct device *dev;
5199c221dfSMagnus Damm 	struct irq_chip_generic *gc;
52fbc83b7fSMagnus Damm 	struct irq_domain *irq_domain;
53734e036aSGeert Uytterhoeven 	atomic_t wakeup_path;
54fbc83b7fSMagnus Damm };
55fbc83b7fSMagnus Damm 
irq_data_to_priv(struct irq_data * data)5699c221dfSMagnus Damm static struct irqc_priv *irq_data_to_priv(struct irq_data *data)
5799c221dfSMagnus Damm {
5899c221dfSMagnus Damm 	return data->domain->host_data;
5999c221dfSMagnus Damm }
6099c221dfSMagnus Damm 
irqc_dbg(struct irqc_irq * i,char * str)61fbc83b7fSMagnus Damm static void irqc_dbg(struct irqc_irq *i, char *str)
62fbc83b7fSMagnus Damm {
63000e20c5SGeert Uytterhoeven 	dev_dbg(i->p->dev, "%s (%d:%d)\n", str, i->requested_irq, i->hw_irq);
64fbc83b7fSMagnus Damm }
65fbc83b7fSMagnus Damm 
66fbc83b7fSMagnus Damm static unsigned char irqc_sense[IRQ_TYPE_SENSE_MASK + 1] = {
67ce70af18SSergei Shtylyov 	[IRQ_TYPE_LEVEL_LOW]	= 0x01,
68ce70af18SSergei Shtylyov 	[IRQ_TYPE_LEVEL_HIGH]	= 0x02,
69ce70af18SSergei Shtylyov 	[IRQ_TYPE_EDGE_FALLING]	= 0x04,	/* Synchronous */
70ce70af18SSergei Shtylyov 	[IRQ_TYPE_EDGE_RISING]	= 0x08,	/* Synchronous */
71ce70af18SSergei Shtylyov 	[IRQ_TYPE_EDGE_BOTH]	= 0x0c,	/* Synchronous */
72fbc83b7fSMagnus Damm };
73fbc83b7fSMagnus Damm 
irqc_irq_set_type(struct irq_data * d,unsigned int type)74fbc83b7fSMagnus Damm static int irqc_irq_set_type(struct irq_data *d, unsigned int type)
75fbc83b7fSMagnus Damm {
7699c221dfSMagnus Damm 	struct irqc_priv *p = irq_data_to_priv(d);
77fbc83b7fSMagnus Damm 	int hw_irq = irqd_to_hwirq(d);
78fbc83b7fSMagnus Damm 	unsigned char value = irqc_sense[type & IRQ_TYPE_SENSE_MASK];
79f791e3c1SGeert Uytterhoeven 	u32 tmp;
80fbc83b7fSMagnus Damm 
81fbc83b7fSMagnus Damm 	irqc_dbg(&p->irq[hw_irq], "sense");
82fbc83b7fSMagnus Damm 
83ce70af18SSergei Shtylyov 	if (!value)
84fbc83b7fSMagnus Damm 		return -EINVAL;
85fbc83b7fSMagnus Damm 
86fbc83b7fSMagnus Damm 	tmp = ioread32(p->iomem + IRQC_CONFIG(hw_irq));
87fbc83b7fSMagnus Damm 	tmp &= ~0x3f;
88ce70af18SSergei Shtylyov 	tmp |= value;
89fbc83b7fSMagnus Damm 	iowrite32(tmp, p->iomem + IRQC_CONFIG(hw_irq));
90fbc83b7fSMagnus Damm 	return 0;
91fbc83b7fSMagnus Damm }
92fbc83b7fSMagnus Damm 
irqc_irq_set_wake(struct irq_data * d,unsigned int on)936f46aedbSGeert Uytterhoeven static int irqc_irq_set_wake(struct irq_data *d, unsigned int on)
946f46aedbSGeert Uytterhoeven {
9599c221dfSMagnus Damm 	struct irqc_priv *p = irq_data_to_priv(d);
964cd7863eSGeert Uytterhoeven 	int hw_irq = irqd_to_hwirq(d);
974cd7863eSGeert Uytterhoeven 
984cd7863eSGeert Uytterhoeven 	irq_set_irq_wake(p->irq[hw_irq].requested_irq, on);
996f46aedbSGeert Uytterhoeven 	if (on)
100734e036aSGeert Uytterhoeven 		atomic_inc(&p->wakeup_path);
1016f46aedbSGeert Uytterhoeven 	else
102734e036aSGeert Uytterhoeven 		atomic_dec(&p->wakeup_path);
1036f46aedbSGeert Uytterhoeven 
1046f46aedbSGeert Uytterhoeven 	return 0;
1056f46aedbSGeert Uytterhoeven }
1066f46aedbSGeert Uytterhoeven 
irqc_irq_handler(int irq,void * dev_id)107fbc83b7fSMagnus Damm static irqreturn_t irqc_irq_handler(int irq, void *dev_id)
108fbc83b7fSMagnus Damm {
109fbc83b7fSMagnus Damm 	struct irqc_irq *i = dev_id;
110fbc83b7fSMagnus Damm 	struct irqc_priv *p = i->p;
111f791e3c1SGeert Uytterhoeven 	u32 bit = BIT(i->hw_irq);
112fbc83b7fSMagnus Damm 
113fbc83b7fSMagnus Damm 	irqc_dbg(i, "demux1");
114fbc83b7fSMagnus Damm 
115fbc83b7fSMagnus Damm 	if (ioread32(p->iomem + DETECT_STATUS) & bit) {
116fbc83b7fSMagnus Damm 		iowrite32(bit, p->iomem + DETECT_STATUS);
117fbc83b7fSMagnus Damm 		irqc_dbg(i, "demux2");
118046a6ee2SMarc Zyngier 		generic_handle_domain_irq(p->irq_domain, i->hw_irq);
119fbc83b7fSMagnus Damm 		return IRQ_HANDLED;
120fbc83b7fSMagnus Damm 	}
121fbc83b7fSMagnus Damm 	return IRQ_NONE;
122fbc83b7fSMagnus Damm }
123fbc83b7fSMagnus Damm 
irqc_probe(struct platform_device * pdev)124fbc83b7fSMagnus Damm static int irqc_probe(struct platform_device *pdev)
125fbc83b7fSMagnus Damm {
1269ae48bc0SGeert Uytterhoeven 	struct device *dev = &pdev->dev;
1279ae48bc0SGeert Uytterhoeven 	const char *name = dev_name(dev);
128fbc83b7fSMagnus Damm 	struct irqc_priv *p;
129fbc83b7fSMagnus Damm 	int ret;
130fbc83b7fSMagnus Damm 	int k;
131fbc83b7fSMagnus Damm 
1324770533fSGeert Uytterhoeven 	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
1334770533fSGeert Uytterhoeven 	if (!p)
1344770533fSGeert Uytterhoeven 		return -ENOMEM;
135fbc83b7fSMagnus Damm 
136000e20c5SGeert Uytterhoeven 	p->dev = dev;
137fbc83b7fSMagnus Damm 	platform_set_drvdata(pdev, p);
138fbc83b7fSMagnus Damm 
1399ae48bc0SGeert Uytterhoeven 	pm_runtime_enable(dev);
1409ae48bc0SGeert Uytterhoeven 	pm_runtime_get_sync(dev);
14151b05f6bSGeert Uytterhoeven 
142fbc83b7fSMagnus Damm 	/* allow any number of IRQs between 1 and IRQC_IRQ_MAX */
143fbc83b7fSMagnus Damm 	for (k = 0; k < IRQC_IRQ_MAX; k++) {
144befbfe6fSLad Prabhakar 		ret = platform_get_irq_optional(pdev, k);
145befbfe6fSLad Prabhakar 		if (ret == -ENXIO)
146fbc83b7fSMagnus Damm 			break;
147befbfe6fSLad Prabhakar 		if (ret < 0)
148befbfe6fSLad Prabhakar 			goto err_runtime_pm_disable;
149fbc83b7fSMagnus Damm 
150fbc83b7fSMagnus Damm 		p->irq[k].p = p;
151e10fc03cSMagnus Damm 		p->irq[k].hw_irq = k;
152befbfe6fSLad Prabhakar 		p->irq[k].requested_irq = ret;
153fbc83b7fSMagnus Damm 	}
154fbc83b7fSMagnus Damm 
155fbc83b7fSMagnus Damm 	p->number_of_irqs = k;
156fbc83b7fSMagnus Damm 	if (p->number_of_irqs < 1) {
1579ae48bc0SGeert Uytterhoeven 		dev_err(dev, "not enough IRQ resources\n");
158fbc83b7fSMagnus Damm 		ret = -EINVAL;
1594770533fSGeert Uytterhoeven 		goto err_runtime_pm_disable;
160fbc83b7fSMagnus Damm 	}
161fbc83b7fSMagnus Damm 
162fbc83b7fSMagnus Damm 	/* ioremap IOMEM and setup read/write callbacks */
1634770533fSGeert Uytterhoeven 	p->iomem = devm_platform_ioremap_resource(pdev, 0);
1644770533fSGeert Uytterhoeven 	if (IS_ERR(p->iomem)) {
1654770533fSGeert Uytterhoeven 		ret = PTR_ERR(p->iomem);
1664770533fSGeert Uytterhoeven 		goto err_runtime_pm_disable;
167fbc83b7fSMagnus Damm 	}
168fbc83b7fSMagnus Damm 
169fbc83b7fSMagnus Damm 	p->cpu_int_base = p->iomem + IRQC_INT_CPU_BASE(0); /* SYS-SPI */
170fbc83b7fSMagnus Damm 
1719ae48bc0SGeert Uytterhoeven 	p->irq_domain = irq_domain_add_linear(dev->of_node, p->number_of_irqs,
17299c221dfSMagnus Damm 					      &irq_generic_chip_ops, p);
173fbc83b7fSMagnus Damm 	if (!p->irq_domain) {
174fbc83b7fSMagnus Damm 		ret = -ENXIO;
1759ae48bc0SGeert Uytterhoeven 		dev_err(dev, "cannot initialize irq domain\n");
1764770533fSGeert Uytterhoeven 		goto err_runtime_pm_disable;
177fbc83b7fSMagnus Damm 	}
178fbc83b7fSMagnus Damm 
17999c221dfSMagnus Damm 	ret = irq_alloc_domain_generic_chips(p->irq_domain, p->number_of_irqs,
18017c88892SGeert Uytterhoeven 					     1, "irqc", handle_level_irq,
18199c221dfSMagnus Damm 					     0, 0, IRQ_GC_INIT_NESTED_LOCK);
18299c221dfSMagnus Damm 	if (ret) {
1839ae48bc0SGeert Uytterhoeven 		dev_err(dev, "cannot allocate generic chip\n");
1844770533fSGeert Uytterhoeven 		goto err_remove_domain;
18599c221dfSMagnus Damm 	}
18699c221dfSMagnus Damm 
18799c221dfSMagnus Damm 	p->gc = irq_get_domain_generic_chip(p->irq_domain, 0);
18899c221dfSMagnus Damm 	p->gc->reg_base = p->cpu_int_base;
18999c221dfSMagnus Damm 	p->gc->chip_types[0].regs.enable = IRQC_EN_SET;
19099c221dfSMagnus Damm 	p->gc->chip_types[0].regs.disable = IRQC_EN_STS;
19199c221dfSMagnus Damm 	p->gc->chip_types[0].chip.irq_mask = irq_gc_mask_disable_reg;
19299c221dfSMagnus Damm 	p->gc->chip_types[0].chip.irq_unmask = irq_gc_unmask_enable_reg;
19399c221dfSMagnus Damm 	p->gc->chip_types[0].chip.irq_set_type	= irqc_irq_set_type;
19499c221dfSMagnus Damm 	p->gc->chip_types[0].chip.irq_set_wake	= irqc_irq_set_wake;
19599c221dfSMagnus Damm 	p->gc->chip_types[0].chip.flags	= IRQCHIP_MASK_ON_SUSPEND;
19699c221dfSMagnus Damm 
197*c3ec838eSMarc Zyngier 	irq_domain_set_pm_device(p->irq_domain, dev);
198*c3ec838eSMarc Zyngier 
199fbc83b7fSMagnus Damm 	/* request interrupts one by one */
200fbc83b7fSMagnus Damm 	for (k = 0; k < p->number_of_irqs; k++) {
2014770533fSGeert Uytterhoeven 		if (devm_request_irq(dev, p->irq[k].requested_irq,
2024770533fSGeert Uytterhoeven 				     irqc_irq_handler, 0, name, &p->irq[k])) {
2039ae48bc0SGeert Uytterhoeven 			dev_err(dev, "failed to request IRQ\n");
204fbc83b7fSMagnus Damm 			ret = -ENOENT;
2054770533fSGeert Uytterhoeven 			goto err_remove_domain;
206fbc83b7fSMagnus Damm 		}
207fbc83b7fSMagnus Damm 	}
208fbc83b7fSMagnus Damm 
2099ae48bc0SGeert Uytterhoeven 	dev_info(dev, "driving %d irqs\n", p->number_of_irqs);
210fbc83b7fSMagnus Damm 
211fbc83b7fSMagnus Damm 	return 0;
212fbc83b7fSMagnus Damm 
2134770533fSGeert Uytterhoeven err_remove_domain:
214fbc83b7fSMagnus Damm 	irq_domain_remove(p->irq_domain);
2154770533fSGeert Uytterhoeven err_runtime_pm_disable:
2169ae48bc0SGeert Uytterhoeven 	pm_runtime_put(dev);
2179ae48bc0SGeert Uytterhoeven 	pm_runtime_disable(dev);
218fbc83b7fSMagnus Damm 	return ret;
219fbc83b7fSMagnus Damm }
220fbc83b7fSMagnus Damm 
irqc_remove(struct platform_device * pdev)221fbc83b7fSMagnus Damm static int irqc_remove(struct platform_device *pdev)
222fbc83b7fSMagnus Damm {
223fbc83b7fSMagnus Damm 	struct irqc_priv *p = platform_get_drvdata(pdev);
224fbc83b7fSMagnus Damm 
225fbc83b7fSMagnus Damm 	irq_domain_remove(p->irq_domain);
22651b05f6bSGeert Uytterhoeven 	pm_runtime_put(&pdev->dev);
22751b05f6bSGeert Uytterhoeven 	pm_runtime_disable(&pdev->dev);
228fbc83b7fSMagnus Damm 	return 0;
229fbc83b7fSMagnus Damm }
230fbc83b7fSMagnus Damm 
irqc_suspend(struct device * dev)231734e036aSGeert Uytterhoeven static int __maybe_unused irqc_suspend(struct device *dev)
232734e036aSGeert Uytterhoeven {
233734e036aSGeert Uytterhoeven 	struct irqc_priv *p = dev_get_drvdata(dev);
234734e036aSGeert Uytterhoeven 
235734e036aSGeert Uytterhoeven 	if (atomic_read(&p->wakeup_path))
236734e036aSGeert Uytterhoeven 		device_set_wakeup_path(dev);
237734e036aSGeert Uytterhoeven 
238734e036aSGeert Uytterhoeven 	return 0;
239734e036aSGeert Uytterhoeven }
240734e036aSGeert Uytterhoeven 
241734e036aSGeert Uytterhoeven static SIMPLE_DEV_PM_OPS(irqc_pm_ops, irqc_suspend, NULL);
242734e036aSGeert Uytterhoeven 
2433b8dfa7cSMagnus Damm static const struct of_device_id irqc_dt_ids[] = {
2443b8dfa7cSMagnus Damm 	{ .compatible = "renesas,irqc", },
2453b8dfa7cSMagnus Damm 	{},
2463b8dfa7cSMagnus Damm };
2473b8dfa7cSMagnus Damm MODULE_DEVICE_TABLE(of, irqc_dt_ids);
2483b8dfa7cSMagnus Damm 
249fbc83b7fSMagnus Damm static struct platform_driver irqc_device_driver = {
250fbc83b7fSMagnus Damm 	.probe		= irqc_probe,
251fbc83b7fSMagnus Damm 	.remove		= irqc_remove,
252fbc83b7fSMagnus Damm 	.driver		= {
253fbc83b7fSMagnus Damm 		.name	= "renesas_irqc",
2543b8dfa7cSMagnus Damm 		.of_match_table	= irqc_dt_ids,
255734e036aSGeert Uytterhoeven 		.pm	= &irqc_pm_ops,
256fbc83b7fSMagnus Damm 	}
257fbc83b7fSMagnus Damm };
258fbc83b7fSMagnus Damm 
irqc_init(void)259fbc83b7fSMagnus Damm static int __init irqc_init(void)
260fbc83b7fSMagnus Damm {
261fbc83b7fSMagnus Damm 	return platform_driver_register(&irqc_device_driver);
262fbc83b7fSMagnus Damm }
263fbc83b7fSMagnus Damm postcore_initcall(irqc_init);
264fbc83b7fSMagnus Damm 
irqc_exit(void)265fbc83b7fSMagnus Damm static void __exit irqc_exit(void)
266fbc83b7fSMagnus Damm {
267fbc83b7fSMagnus Damm 	platform_driver_unregister(&irqc_device_driver);
268fbc83b7fSMagnus Damm }
269fbc83b7fSMagnus Damm module_exit(irqc_exit);
270fbc83b7fSMagnus Damm 
271fbc83b7fSMagnus Damm MODULE_AUTHOR("Magnus Damm");
272fbc83b7fSMagnus Damm MODULE_DESCRIPTION("Renesas IRQC Driver");
273