blob: 2aa3add711a6612af6c1653c976a043e743d9058 [file] [log] [blame]
Magnus Dammfbc83b72013-02-27 17:15:01 +09001/*
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
Geert Uytterhoeven6f46aed2015-04-01 14:00:06 +020020#include <linux/clk.h>
Magnus Dammfbc83b72013-02-27 17:15:01 +090021#include <linux/init.h>
22#include <linux/platform_device.h>
23#include <linux/spinlock.h>
24#include <linux/interrupt.h>
25#include <linux/ioport.h>
26#include <linux/io.h>
27#include <linux/irq.h>
28#include <linux/irqdomain.h>
29#include <linux/err.h>
30#include <linux/slab.h>
31#include <linux/module.h>
Geert Uytterhoeven51b05f62015-03-18 19:55:56 +010032#include <linux/pm_runtime.h>
Magnus Dammfbc83b72013-02-27 17:15:01 +090033
Geert Uytterhoeven1cd5ec732015-03-18 19:55:55 +010034#define IRQC_IRQ_MAX 32 /* maximum 32 interrupts per driver instance */
Magnus Dammfbc83b72013-02-27 17:15:01 +090035
Geert Uytterhoeven1cd5ec732015-03-18 19:55:55 +010036#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 Dammfbc83b72013-02-27 17:15:01 +090039#define IRQC_INT_CPU_BASE(n) (0x000 + ((n) * 0x10))
Geert Uytterhoeven1cd5ec732015-03-18 19:55:55 +010040 /* 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 Dammfbc83b72013-02-27 17:15:01 +090050#define IRQC_CONFIG(n) (0x180 + ((n) * 0x04))
Geert Uytterhoeven1cd5ec732015-03-18 19:55:55 +010051 /* IRQn Configuration Register */
Magnus Dammfbc83b72013-02-27 17:15:01 +090052
53struct irqc_irq {
54 int hw_irq;
55 int requested_irq;
Magnus Dammfbc83b72013-02-27 17:15:01 +090056 struct irqc_priv *p;
57};
58
59struct irqc_priv {
60 void __iomem *iomem;
61 void __iomem *cpu_int_base;
62 struct irqc_irq irq[IRQC_IRQ_MAX];
Magnus Dammfbc83b72013-02-27 17:15:01 +090063 unsigned int number_of_irqs;
64 struct platform_device *pdev;
65 struct irq_chip irq_chip;
66 struct irq_domain *irq_domain;
Geert Uytterhoeven6f46aed2015-04-01 14:00:06 +020067 struct clk *clk;
Magnus Dammfbc83b72013-02-27 17:15:01 +090068};
69
70static void irqc_dbg(struct irqc_irq *i, char *str)
71{
Magnus Damme10fc032015-07-20 19:06:35 +090072 dev_dbg(&i->p->pdev->dev, "%s (%d:%d)\n",
73 str, i->requested_irq, i->hw_irq);
Magnus Dammfbc83b72013-02-27 17:15:01 +090074}
75
76static void irqc_irq_enable(struct irq_data *d)
77{
78 struct irqc_priv *p = irq_data_get_irq_chip_data(d);
79 int hw_irq = irqd_to_hwirq(d);
80
81 irqc_dbg(&p->irq[hw_irq], "enable");
82 iowrite32(BIT(hw_irq), p->cpu_int_base + IRQC_EN_SET);
83}
84
85static void irqc_irq_disable(struct irq_data *d)
86{
87 struct irqc_priv *p = irq_data_get_irq_chip_data(d);
88 int hw_irq = irqd_to_hwirq(d);
89
90 irqc_dbg(&p->irq[hw_irq], "disable");
91 iowrite32(BIT(hw_irq), p->cpu_int_base + IRQC_EN_STS);
92}
93
Magnus Dammfbc83b72013-02-27 17:15:01 +090094static unsigned char irqc_sense[IRQ_TYPE_SENSE_MASK + 1] = {
Sergei Shtylyovce70af12013-12-14 03:09:31 +030095 [IRQ_TYPE_LEVEL_LOW] = 0x01,
96 [IRQ_TYPE_LEVEL_HIGH] = 0x02,
97 [IRQ_TYPE_EDGE_FALLING] = 0x04, /* Synchronous */
98 [IRQ_TYPE_EDGE_RISING] = 0x08, /* Synchronous */
99 [IRQ_TYPE_EDGE_BOTH] = 0x0c, /* Synchronous */
Magnus Dammfbc83b72013-02-27 17:15:01 +0900100};
101
102static int irqc_irq_set_type(struct irq_data *d, unsigned int type)
103{
104 struct irqc_priv *p = irq_data_get_irq_chip_data(d);
105 int hw_irq = irqd_to_hwirq(d);
106 unsigned char value = irqc_sense[type & IRQ_TYPE_SENSE_MASK];
Geert Uytterhoevenf791e3c2015-02-26 11:43:32 +0100107 u32 tmp;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900108
109 irqc_dbg(&p->irq[hw_irq], "sense");
110
Sergei Shtylyovce70af12013-12-14 03:09:31 +0300111 if (!value)
Magnus Dammfbc83b72013-02-27 17:15:01 +0900112 return -EINVAL;
113
114 tmp = ioread32(p->iomem + IRQC_CONFIG(hw_irq));
115 tmp &= ~0x3f;
Sergei Shtylyovce70af12013-12-14 03:09:31 +0300116 tmp |= value;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900117 iowrite32(tmp, p->iomem + IRQC_CONFIG(hw_irq));
118 return 0;
119}
120
Geert Uytterhoeven6f46aed2015-04-01 14:00:06 +0200121static int irqc_irq_set_wake(struct irq_data *d, unsigned int on)
122{
123 struct irqc_priv *p = irq_data_get_irq_chip_data(d);
124
125 if (!p->clk)
126 return 0;
127
128 if (on)
129 clk_enable(p->clk);
130 else
131 clk_disable(p->clk);
132
133 return 0;
134}
135
Magnus Dammfbc83b72013-02-27 17:15:01 +0900136static irqreturn_t irqc_irq_handler(int irq, void *dev_id)
137{
138 struct irqc_irq *i = dev_id;
139 struct irqc_priv *p = i->p;
Geert Uytterhoevenf791e3c2015-02-26 11:43:32 +0100140 u32 bit = BIT(i->hw_irq);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900141
142 irqc_dbg(i, "demux1");
143
144 if (ioread32(p->iomem + DETECT_STATUS) & bit) {
145 iowrite32(bit, p->iomem + DETECT_STATUS);
146 irqc_dbg(i, "demux2");
Magnus Damme10fc032015-07-20 19:06:35 +0900147 generic_handle_irq(irq_find_mapping(p->irq_domain, i->hw_irq));
Magnus Dammfbc83b72013-02-27 17:15:01 +0900148 return IRQ_HANDLED;
149 }
150 return IRQ_NONE;
151}
152
153static int irqc_irq_domain_map(struct irq_domain *h, unsigned int virq,
154 irq_hw_number_t hw)
155{
156 struct irqc_priv *p = h->host_data;
157
Magnus Dammfbc83b72013-02-27 17:15:01 +0900158 irqc_dbg(&p->irq[hw], "map");
159 irq_set_chip_data(virq, h->host_data);
160 irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900161 return 0;
162}
163
Krzysztof Kozlowski96009732015-04-27 21:54:24 +0900164static const struct irq_domain_ops irqc_irq_domain_ops = {
Magnus Dammfbc83b72013-02-27 17:15:01 +0900165 .map = irqc_irq_domain_map,
Magnus Damm3b8dfa72013-03-06 15:23:39 +0900166 .xlate = irq_domain_xlate_twocell,
Magnus Dammfbc83b72013-02-27 17:15:01 +0900167};
168
169static int irqc_probe(struct platform_device *pdev)
170{
Magnus Dammfbc83b72013-02-27 17:15:01 +0900171 struct irqc_priv *p;
172 struct resource *io;
173 struct resource *irq;
174 struct irq_chip *irq_chip;
175 const char *name = dev_name(&pdev->dev);
176 int ret;
177 int k;
178
179 p = kzalloc(sizeof(*p), GFP_KERNEL);
180 if (!p) {
181 dev_err(&pdev->dev, "failed to allocate driver data\n");
182 ret = -ENOMEM;
183 goto err0;
184 }
185
Magnus Dammfbc83b72013-02-27 17:15:01 +0900186 p->pdev = pdev;
187 platform_set_drvdata(pdev, p);
188
Geert Uytterhoeven6f46aed2015-04-01 14:00:06 +0200189 p->clk = devm_clk_get(&pdev->dev, NULL);
190 if (IS_ERR(p->clk)) {
191 dev_warn(&pdev->dev, "unable to get clock\n");
192 p->clk = NULL;
193 }
194
Geert Uytterhoeven51b05f62015-03-18 19:55:56 +0100195 pm_runtime_enable(&pdev->dev);
196 pm_runtime_get_sync(&pdev->dev);
197
Magnus Dammfbc83b72013-02-27 17:15:01 +0900198 /* get hold of manadatory IOMEM */
199 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200 if (!io) {
201 dev_err(&pdev->dev, "not enough IOMEM resources\n");
202 ret = -EINVAL;
203 goto err1;
204 }
205
206 /* allow any number of IRQs between 1 and IRQC_IRQ_MAX */
207 for (k = 0; k < IRQC_IRQ_MAX; k++) {
208 irq = platform_get_resource(pdev, IORESOURCE_IRQ, k);
209 if (!irq)
210 break;
211
212 p->irq[k].p = p;
Magnus Damme10fc032015-07-20 19:06:35 +0900213 p->irq[k].hw_irq = k;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900214 p->irq[k].requested_irq = irq->start;
215 }
216
217 p->number_of_irqs = k;
218 if (p->number_of_irqs < 1) {
219 dev_err(&pdev->dev, "not enough IRQ resources\n");
220 ret = -EINVAL;
221 goto err1;
222 }
223
224 /* ioremap IOMEM and setup read/write callbacks */
225 p->iomem = ioremap_nocache(io->start, resource_size(io));
226 if (!p->iomem) {
227 dev_err(&pdev->dev, "failed to remap IOMEM\n");
228 ret = -ENXIO;
229 goto err2;
230 }
231
232 p->cpu_int_base = p->iomem + IRQC_INT_CPU_BASE(0); /* SYS-SPI */
233
234 irq_chip = &p->irq_chip;
235 irq_chip->name = name;
236 irq_chip->irq_mask = irqc_irq_disable;
237 irq_chip->irq_unmask = irqc_irq_enable;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900238 irq_chip->irq_set_type = irqc_irq_set_type;
Geert Uytterhoeven6f46aed2015-04-01 14:00:06 +0200239 irq_chip->irq_set_wake = irqc_irq_set_wake;
240 irq_chip->flags = IRQCHIP_MASK_ON_SUSPEND;
Magnus Dammfbc83b72013-02-27 17:15:01 +0900241
Magnus Damm7d153752015-07-20 19:06:25 +0900242 p->irq_domain = irq_domain_add_linear(pdev->dev.of_node,
243 p->number_of_irqs,
Magnus Dammfbc83b72013-02-27 17:15:01 +0900244 &irqc_irq_domain_ops, p);
245 if (!p->irq_domain) {
246 ret = -ENXIO;
247 dev_err(&pdev->dev, "cannot initialize irq domain\n");
248 goto err2;
249 }
250
251 /* request interrupts one by one */
252 for (k = 0; k < p->number_of_irqs; k++) {
253 if (request_irq(p->irq[k].requested_irq, irqc_irq_handler,
254 0, name, &p->irq[k])) {
255 dev_err(&pdev->dev, "failed to request IRQ\n");
256 ret = -ENOENT;
257 goto err3;
258 }
259 }
260
261 dev_info(&pdev->dev, "driving %d irqs\n", p->number_of_irqs);
262
Magnus Dammfbc83b72013-02-27 17:15:01 +0900263 return 0;
264err3:
Axel Lindfaf8202013-05-06 17:03:32 +0800265 while (--k >= 0)
266 free_irq(p->irq[k].requested_irq, &p->irq[k]);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900267
268 irq_domain_remove(p->irq_domain);
269err2:
270 iounmap(p->iomem);
271err1:
Geert Uytterhoeven51b05f62015-03-18 19:55:56 +0100272 pm_runtime_put(&pdev->dev);
273 pm_runtime_disable(&pdev->dev);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900274 kfree(p);
275err0:
276 return ret;
277}
278
279static int irqc_remove(struct platform_device *pdev)
280{
281 struct irqc_priv *p = platform_get_drvdata(pdev);
282 int k;
283
284 for (k = 0; k < p->number_of_irqs; k++)
285 free_irq(p->irq[k].requested_irq, &p->irq[k]);
286
287 irq_domain_remove(p->irq_domain);
288 iounmap(p->iomem);
Geert Uytterhoeven51b05f62015-03-18 19:55:56 +0100289 pm_runtime_put(&pdev->dev);
290 pm_runtime_disable(&pdev->dev);
Magnus Dammfbc83b72013-02-27 17:15:01 +0900291 kfree(p);
292 return 0;
293}
294
Magnus Damm3b8dfa72013-03-06 15:23:39 +0900295static const struct of_device_id irqc_dt_ids[] = {
296 { .compatible = "renesas,irqc", },
297 {},
298};
299MODULE_DEVICE_TABLE(of, irqc_dt_ids);
300
Magnus Dammfbc83b72013-02-27 17:15:01 +0900301static struct platform_driver irqc_device_driver = {
302 .probe = irqc_probe,
303 .remove = irqc_remove,
304 .driver = {
305 .name = "renesas_irqc",
Magnus Damm3b8dfa72013-03-06 15:23:39 +0900306 .of_match_table = irqc_dt_ids,
Magnus Dammfbc83b72013-02-27 17:15:01 +0900307 }
308};
309
310static int __init irqc_init(void)
311{
312 return platform_driver_register(&irqc_device_driver);
313}
314postcore_initcall(irqc_init);
315
316static void __exit irqc_exit(void)
317{
318 platform_driver_unregister(&irqc_device_driver);
319}
320module_exit(irqc_exit);
321
322MODULE_AUTHOR("Magnus Damm");
323MODULE_DESCRIPTION("Renesas IRQC Driver");
324MODULE_LICENSE("GPL v2");