blob: 06f29cf5018a151f7d35641d90b8d043aec85516 [file] [log] [blame]
Magnus Damm44358042013-02-18 23:28:34 +09001/*
2 * Renesas INTC External IRQ Pin 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 Uytterhoeven705bc962014-09-12 15:15:18 +020020#include <linux/clk.h>
Magnus Damm44358042013-02-18 23:28:34 +090021#include <linux/init.h>
Guennadi Liakhovetski894db162013-06-13 11:23:38 +020022#include <linux/of.h>
Magnus Damm44358042013-02-18 23:28:34 +090023#include <linux/platform_device.h>
24#include <linux/spinlock.h>
25#include <linux/interrupt.h>
26#include <linux/ioport.h>
27#include <linux/io.h>
28#include <linux/irq.h>
29#include <linux/irqdomain.h>
30#include <linux/err.h>
31#include <linux/slab.h>
32#include <linux/module.h>
Magnus Damme03f9082014-12-03 21:18:03 +090033#include <linux/of_device.h>
Geert Uytterhoeven705bc962014-09-12 15:15:18 +020034#include <linux/pm_runtime.h>
Magnus Damm44358042013-02-18 23:28:34 +090035
36#define INTC_IRQPIN_MAX 8 /* maximum 8 interrupts per driver instance */
37
38#define INTC_IRQPIN_REG_SENSE 0 /* ICRn */
39#define INTC_IRQPIN_REG_PRIO 1 /* INTPRInn */
40#define INTC_IRQPIN_REG_SOURCE 2 /* INTREQnn */
41#define INTC_IRQPIN_REG_MASK 3 /* INTMSKnn */
42#define INTC_IRQPIN_REG_CLEAR 4 /* INTMSKCLRnn */
Magnus Damme03f9082014-12-03 21:18:03 +090043#define INTC_IRQPIN_REG_NR_MANDATORY 5
44#define INTC_IRQPIN_REG_IRLM 5 /* ICR0 with IRLM bit (optional) */
45#define INTC_IRQPIN_REG_NR 6
Magnus Damm44358042013-02-18 23:28:34 +090046
47/* INTC external IRQ PIN hardware register access:
48 *
49 * SENSE is read-write 32-bit with 2-bits or 4-bits per IRQ (*)
50 * PRIO is read-write 32-bit with 4-bits per IRQ (**)
51 * SOURCE is read-only 32-bit or 8-bit with 1-bit per IRQ (***)
52 * MASK is write-only 32-bit or 8-bit with 1-bit per IRQ (***)
53 * CLEAR is write-only 32-bit or 8-bit with 1-bit per IRQ (***)
54 *
55 * (*) May be accessed by more than one driver instance - lock needed
56 * (**) Read-modify-write access by one driver instance - lock needed
57 * (***) Accessed by one driver instance only - no locking needed
58 */
59
60struct intc_irqpin_iomem {
61 void __iomem *iomem;
62 unsigned long (*read)(void __iomem *iomem);
63 void (*write)(void __iomem *iomem, unsigned long data);
64 int width;
Magnus Damm862d3092013-02-26 20:58:44 +090065};
Magnus Damm44358042013-02-18 23:28:34 +090066
67struct intc_irqpin_irq {
68 int hw_irq;
Magnus Damm33f958f2013-02-26 20:58:54 +090069 int requested_irq;
70 int domain_irq;
Magnus Damm44358042013-02-18 23:28:34 +090071 struct intc_irqpin_priv *p;
Magnus Damm862d3092013-02-26 20:58:44 +090072};
Magnus Damm44358042013-02-18 23:28:34 +090073
74struct intc_irqpin_priv {
75 struct intc_irqpin_iomem iomem[INTC_IRQPIN_REG_NR];
76 struct intc_irqpin_irq irq[INTC_IRQPIN_MAX];
Geert Uytterhoevenf9551a92015-11-24 15:49:40 +010077 unsigned int sense_bitfield_width;
Magnus Damm44358042013-02-18 23:28:34 +090078 struct platform_device *pdev;
79 struct irq_chip irq_chip;
80 struct irq_domain *irq_domain;
Geert Uytterhoeven705bc962014-09-12 15:15:18 +020081 struct clk *clk;
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +010082 unsigned shared_irqs:1;
83 unsigned needs_clk:1;
Bastian Hecht427cc722013-03-27 14:54:03 +010084 u8 shared_irq_mask;
Magnus Damm44358042013-02-18 23:28:34 +090085};
86
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +010087struct intc_irqpin_config {
Magnus Damme03f9082014-12-03 21:18:03 +090088 unsigned int irlm_bit;
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +010089 unsigned needs_irlm:1;
90 unsigned needs_clk:1;
Magnus Damme03f9082014-12-03 21:18:03 +090091};
92
Magnus Damm44358042013-02-18 23:28:34 +090093static unsigned long intc_irqpin_read32(void __iomem *iomem)
94{
95 return ioread32(iomem);
96}
97
98static unsigned long intc_irqpin_read8(void __iomem *iomem)
99{
100 return ioread8(iomem);
101}
102
103static void intc_irqpin_write32(void __iomem *iomem, unsigned long data)
104{
105 iowrite32(data, iomem);
106}
107
108static void intc_irqpin_write8(void __iomem *iomem, unsigned long data)
109{
110 iowrite8(data, iomem);
111}
112
113static inline unsigned long intc_irqpin_read(struct intc_irqpin_priv *p,
114 int reg)
115{
116 struct intc_irqpin_iomem *i = &p->iomem[reg];
Magnus Damm862d3092013-02-26 20:58:44 +0900117
Magnus Damm44358042013-02-18 23:28:34 +0900118 return i->read(i->iomem);
119}
120
121static inline void intc_irqpin_write(struct intc_irqpin_priv *p,
122 int reg, unsigned long data)
123{
124 struct intc_irqpin_iomem *i = &p->iomem[reg];
Magnus Damm862d3092013-02-26 20:58:44 +0900125
Magnus Damm44358042013-02-18 23:28:34 +0900126 i->write(i->iomem, data);
127}
128
129static inline unsigned long intc_irqpin_hwirq_mask(struct intc_irqpin_priv *p,
130 int reg, int hw_irq)
131{
132 return BIT((p->iomem[reg].width - 1) - hw_irq);
133}
134
135static inline void intc_irqpin_irq_write_hwirq(struct intc_irqpin_priv *p,
136 int reg, int hw_irq)
137{
138 intc_irqpin_write(p, reg, intc_irqpin_hwirq_mask(p, reg, hw_irq));
139}
140
141static DEFINE_RAW_SPINLOCK(intc_irqpin_lock); /* only used by slow path */
142
143static void intc_irqpin_read_modify_write(struct intc_irqpin_priv *p,
144 int reg, int shift,
145 int width, int value)
146{
147 unsigned long flags;
148 unsigned long tmp;
149
150 raw_spin_lock_irqsave(&intc_irqpin_lock, flags);
151
152 tmp = intc_irqpin_read(p, reg);
153 tmp &= ~(((1 << width) - 1) << shift);
154 tmp |= value << shift;
155 intc_irqpin_write(p, reg, tmp);
156
157 raw_spin_unlock_irqrestore(&intc_irqpin_lock, flags);
158}
159
160static void intc_irqpin_mask_unmask_prio(struct intc_irqpin_priv *p,
161 int irq, int do_mask)
162{
Laurent Pincharte55bc552013-11-09 13:18:01 +0100163 /* The PRIO register is assumed to be 32-bit with fixed 4-bit fields. */
164 int bitfield_width = 4;
165 int shift = 32 - (irq + 1) * bitfield_width;
Magnus Damm44358042013-02-18 23:28:34 +0900166
167 intc_irqpin_read_modify_write(p, INTC_IRQPIN_REG_PRIO,
168 shift, bitfield_width,
169 do_mask ? 0 : (1 << bitfield_width) - 1);
170}
171
172static int intc_irqpin_set_sense(struct intc_irqpin_priv *p, int irq, int value)
173{
Laurent Pincharte55bc552013-11-09 13:18:01 +0100174 /* The SENSE register is assumed to be 32-bit. */
Geert Uytterhoevenf9551a92015-11-24 15:49:40 +0100175 int bitfield_width = p->sense_bitfield_width;
Laurent Pincharte55bc552013-11-09 13:18:01 +0100176 int shift = 32 - (irq + 1) * bitfield_width;
Magnus Damm44358042013-02-18 23:28:34 +0900177
178 dev_dbg(&p->pdev->dev, "sense irq = %d, mode = %d\n", irq, value);
179
180 if (value >= (1 << bitfield_width))
181 return -EINVAL;
182
183 intc_irqpin_read_modify_write(p, INTC_IRQPIN_REG_SENSE, shift,
184 bitfield_width, value);
185 return 0;
186}
187
188static void intc_irqpin_dbg(struct intc_irqpin_irq *i, char *str)
189{
190 dev_dbg(&i->p->pdev->dev, "%s (%d:%d:%d)\n",
Magnus Damm33f958f2013-02-26 20:58:54 +0900191 str, i->requested_irq, i->hw_irq, i->domain_irq);
Magnus Damm44358042013-02-18 23:28:34 +0900192}
193
194static void intc_irqpin_irq_enable(struct irq_data *d)
195{
196 struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
197 int hw_irq = irqd_to_hwirq(d);
198
199 intc_irqpin_dbg(&p->irq[hw_irq], "enable");
200 intc_irqpin_irq_write_hwirq(p, INTC_IRQPIN_REG_CLEAR, hw_irq);
201}
202
203static void intc_irqpin_irq_disable(struct irq_data *d)
204{
205 struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
206 int hw_irq = irqd_to_hwirq(d);
207
208 intc_irqpin_dbg(&p->irq[hw_irq], "disable");
209 intc_irqpin_irq_write_hwirq(p, INTC_IRQPIN_REG_MASK, hw_irq);
210}
211
Bastian Hecht427cc722013-03-27 14:54:03 +0100212static void intc_irqpin_shared_irq_enable(struct irq_data *d)
213{
214 struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
215 int hw_irq = irqd_to_hwirq(d);
216
217 intc_irqpin_dbg(&p->irq[hw_irq], "shared enable");
218 intc_irqpin_irq_write_hwirq(p, INTC_IRQPIN_REG_CLEAR, hw_irq);
219
220 p->shared_irq_mask &= ~BIT(hw_irq);
221}
222
223static void intc_irqpin_shared_irq_disable(struct irq_data *d)
224{
225 struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
226 int hw_irq = irqd_to_hwirq(d);
227
228 intc_irqpin_dbg(&p->irq[hw_irq], "shared disable");
229 intc_irqpin_irq_write_hwirq(p, INTC_IRQPIN_REG_MASK, hw_irq);
230
231 p->shared_irq_mask |= BIT(hw_irq);
232}
233
Magnus Damm44358042013-02-18 23:28:34 +0900234static void intc_irqpin_irq_enable_force(struct irq_data *d)
235{
236 struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
Magnus Damm33f958f2013-02-26 20:58:54 +0900237 int irq = p->irq[irqd_to_hwirq(d)].requested_irq;
Magnus Damm44358042013-02-18 23:28:34 +0900238
239 intc_irqpin_irq_enable(d);
Magnus Dammd1b6aec2013-02-26 20:59:04 +0900240
241 /* enable interrupt through parent interrupt controller,
242 * assumes non-shared interrupt with 1:1 mapping
243 * needed for busted IRQs on some SoCs like sh73a0
244 */
Magnus Damm44358042013-02-18 23:28:34 +0900245 irq_get_chip(irq)->irq_unmask(irq_get_irq_data(irq));
246}
247
248static void intc_irqpin_irq_disable_force(struct irq_data *d)
249{
250 struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
Magnus Damm33f958f2013-02-26 20:58:54 +0900251 int irq = p->irq[irqd_to_hwirq(d)].requested_irq;
Magnus Damm44358042013-02-18 23:28:34 +0900252
Magnus Dammd1b6aec2013-02-26 20:59:04 +0900253 /* disable interrupt through parent interrupt controller,
254 * assumes non-shared interrupt with 1:1 mapping
255 * needed for busted IRQs on some SoCs like sh73a0
256 */
Magnus Damm44358042013-02-18 23:28:34 +0900257 irq_get_chip(irq)->irq_mask(irq_get_irq_data(irq));
258 intc_irqpin_irq_disable(d);
259}
260
261#define INTC_IRQ_SENSE_VALID 0x10
262#define INTC_IRQ_SENSE(x) (x + INTC_IRQ_SENSE_VALID)
263
264static unsigned char intc_irqpin_sense[IRQ_TYPE_SENSE_MASK + 1] = {
265 [IRQ_TYPE_EDGE_FALLING] = INTC_IRQ_SENSE(0x00),
266 [IRQ_TYPE_EDGE_RISING] = INTC_IRQ_SENSE(0x01),
267 [IRQ_TYPE_LEVEL_LOW] = INTC_IRQ_SENSE(0x02),
268 [IRQ_TYPE_LEVEL_HIGH] = INTC_IRQ_SENSE(0x03),
269 [IRQ_TYPE_EDGE_BOTH] = INTC_IRQ_SENSE(0x04),
270};
271
272static int intc_irqpin_irq_set_type(struct irq_data *d, unsigned int type)
273{
274 unsigned char value = intc_irqpin_sense[type & IRQ_TYPE_SENSE_MASK];
275 struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
276
277 if (!(value & INTC_IRQ_SENSE_VALID))
278 return -EINVAL;
279
280 return intc_irqpin_set_sense(p, irqd_to_hwirq(d),
281 value ^ INTC_IRQ_SENSE_VALID);
282}
283
Geert Uytterhoeven705bc962014-09-12 15:15:18 +0200284static int intc_irqpin_irq_set_wake(struct irq_data *d, unsigned int on)
285{
286 struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
Geert Uytterhoevenf4e209c2015-09-08 19:00:35 +0200287 int hw_irq = irqd_to_hwirq(d);
288
289 irq_set_irq_wake(p->irq[hw_irq].requested_irq, on);
Geert Uytterhoeven705bc962014-09-12 15:15:18 +0200290
291 if (!p->clk)
292 return 0;
293
294 if (on)
295 clk_enable(p->clk);
296 else
297 clk_disable(p->clk);
298
299 return 0;
300}
301
Magnus Damm44358042013-02-18 23:28:34 +0900302static irqreturn_t intc_irqpin_irq_handler(int irq, void *dev_id)
303{
304 struct intc_irqpin_irq *i = dev_id;
305 struct intc_irqpin_priv *p = i->p;
306 unsigned long bit;
307
308 intc_irqpin_dbg(i, "demux1");
309 bit = intc_irqpin_hwirq_mask(p, INTC_IRQPIN_REG_SOURCE, i->hw_irq);
310
311 if (intc_irqpin_read(p, INTC_IRQPIN_REG_SOURCE) & bit) {
312 intc_irqpin_write(p, INTC_IRQPIN_REG_SOURCE, ~bit);
313 intc_irqpin_dbg(i, "demux2");
Magnus Damm33f958f2013-02-26 20:58:54 +0900314 generic_handle_irq(i->domain_irq);
Magnus Damm44358042013-02-18 23:28:34 +0900315 return IRQ_HANDLED;
316 }
317 return IRQ_NONE;
318}
319
Bastian Hecht427cc722013-03-27 14:54:03 +0100320static irqreturn_t intc_irqpin_shared_irq_handler(int irq, void *dev_id)
321{
322 struct intc_irqpin_priv *p = dev_id;
323 unsigned int reg_source = intc_irqpin_read(p, INTC_IRQPIN_REG_SOURCE);
324 irqreturn_t status = IRQ_NONE;
325 int k;
326
327 for (k = 0; k < 8; k++) {
328 if (reg_source & BIT(7 - k)) {
329 if (BIT(k) & p->shared_irq_mask)
330 continue;
331
332 status |= intc_irqpin_irq_handler(irq, &p->irq[k]);
333 }
334 }
335
336 return status;
337}
338
Geert Uytterhoeven769b5cf2015-09-09 13:42:54 +0200339/*
340 * This lock class tells lockdep that INTC External IRQ Pin irqs are in a
341 * different category than their parents, so it won't report false recursion.
342 */
343static struct lock_class_key intc_irqpin_irq_lock_class;
344
Magnus Damm44358042013-02-18 23:28:34 +0900345static int intc_irqpin_irq_domain_map(struct irq_domain *h, unsigned int virq,
346 irq_hw_number_t hw)
347{
348 struct intc_irqpin_priv *p = h->host_data;
349
Magnus Damm33f958f2013-02-26 20:58:54 +0900350 p->irq[hw].domain_irq = virq;
351 p->irq[hw].hw_irq = hw;
352
Magnus Damm44358042013-02-18 23:28:34 +0900353 intc_irqpin_dbg(&p->irq[hw], "map");
354 irq_set_chip_data(virq, h->host_data);
Geert Uytterhoeven769b5cf2015-09-09 13:42:54 +0200355 irq_set_lockdep_class(virq, &intc_irqpin_irq_lock_class);
Magnus Damm44358042013-02-18 23:28:34 +0900356 irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
Magnus Damm44358042013-02-18 23:28:34 +0900357 return 0;
358}
359
Krzysztof Kozlowski96009732015-04-27 21:54:24 +0900360static const struct irq_domain_ops intc_irqpin_irq_domain_ops = {
Magnus Damm44358042013-02-18 23:28:34 +0900361 .map = intc_irqpin_irq_domain_map,
Magnus Damm9d833bbe2013-03-06 15:16:08 +0900362 .xlate = irq_domain_xlate_twocell,
Magnus Damm44358042013-02-18 23:28:34 +0900363};
364
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +0100365static const struct intc_irqpin_config intc_irqpin_irlm_r8a777x = {
Magnus Damme03f9082014-12-03 21:18:03 +0900366 .irlm_bit = 23, /* ICR0.IRLM0 */
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +0100367 .needs_irlm = 1,
368 .needs_clk = 0,
369};
370
371static const struct intc_irqpin_config intc_irqpin_rmobile = {
372 .needs_irlm = 0,
373 .needs_clk = 1,
Magnus Damme03f9082014-12-03 21:18:03 +0900374};
375
376static const struct of_device_id intc_irqpin_dt_ids[] = {
377 { .compatible = "renesas,intc-irqpin", },
Ulrich Hecht26c21dd2015-09-30 12:03:07 +0200378 { .compatible = "renesas,intc-irqpin-r8a7778",
379 .data = &intc_irqpin_irlm_r8a777x },
Magnus Damme03f9082014-12-03 21:18:03 +0900380 { .compatible = "renesas,intc-irqpin-r8a7779",
Ulrich Hecht26c21dd2015-09-30 12:03:07 +0200381 .data = &intc_irqpin_irlm_r8a777x },
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +0100382 { .compatible = "renesas,intc-irqpin-r8a7740",
383 .data = &intc_irqpin_rmobile },
384 { .compatible = "renesas,intc-irqpin-sh73a0",
385 .data = &intc_irqpin_rmobile },
Magnus Damme03f9082014-12-03 21:18:03 +0900386 {},
387};
388MODULE_DEVICE_TABLE(of, intc_irqpin_dt_ids);
389
Magnus Damm44358042013-02-18 23:28:34 +0900390static int intc_irqpin_probe(struct platform_device *pdev)
391{
Geert Uytterhoeven42a59682017-10-04 14:17:58 +0200392 const struct intc_irqpin_config *config;
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200393 struct device *dev = &pdev->dev;
Magnus Damm44358042013-02-18 23:28:34 +0900394 struct intc_irqpin_priv *p;
395 struct intc_irqpin_iomem *i;
396 struct resource *io[INTC_IRQPIN_REG_NR];
397 struct resource *irq;
398 struct irq_chip *irq_chip;
399 void (*enable_fn)(struct irq_data *d);
400 void (*disable_fn)(struct irq_data *d);
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200401 const char *name = dev_name(dev);
Geert Uytterhoevenf9551a92015-11-24 15:49:40 +0100402 bool control_parent;
Geert Uytterhoeven1affe592015-11-24 15:49:41 +0100403 unsigned int nirqs;
Bastian Hecht427cc722013-03-27 14:54:03 +0100404 int ref_irq;
Magnus Damm44358042013-02-18 23:28:34 +0900405 int ret;
406 int k;
407
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200408 p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
Magnus Damm44358042013-02-18 23:28:34 +0900409 if (!p) {
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200410 dev_err(dev, "failed to allocate driver data\n");
Geert Uytterhoeven705bc962014-09-12 15:15:18 +0200411 return -ENOMEM;
Magnus Damm44358042013-02-18 23:28:34 +0900412 }
413
414 /* deal with driver instance configuration */
Geert Uytterhoevenf9551a92015-11-24 15:49:40 +0100415 of_property_read_u32(dev->of_node, "sense-bitfield-width",
416 &p->sense_bitfield_width);
417 control_parent = of_property_read_bool(dev->of_node, "control-parent");
418 if (!p->sense_bitfield_width)
419 p->sense_bitfield_width = 4; /* default to 4 bits */
Magnus Damm44358042013-02-18 23:28:34 +0900420
421 p->pdev = pdev;
422 platform_set_drvdata(pdev, p);
423
Geert Uytterhoeven42a59682017-10-04 14:17:58 +0200424 config = of_device_get_match_data(dev);
425 if (config)
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +0100426 p->needs_clk = config->needs_clk;
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +0100427
Geert Uytterhoeven705bc962014-09-12 15:15:18 +0200428 p->clk = devm_clk_get(dev, NULL);
429 if (IS_ERR(p->clk)) {
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +0100430 if (p->needs_clk) {
431 dev_err(dev, "unable to get clock\n");
432 ret = PTR_ERR(p->clk);
433 goto err0;
434 }
Geert Uytterhoeven705bc962014-09-12 15:15:18 +0200435 p->clk = NULL;
436 }
437
438 pm_runtime_enable(dev);
439 pm_runtime_get_sync(dev);
440
Magnus Damme03f9082014-12-03 21:18:03 +0900441 /* get hold of register banks */
442 memset(io, 0, sizeof(io));
Magnus Damm44358042013-02-18 23:28:34 +0900443 for (k = 0; k < INTC_IRQPIN_REG_NR; k++) {
444 io[k] = platform_get_resource(pdev, IORESOURCE_MEM, k);
Magnus Damme03f9082014-12-03 21:18:03 +0900445 if (!io[k] && k < INTC_IRQPIN_REG_NR_MANDATORY) {
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200446 dev_err(dev, "not enough IOMEM resources\n");
Magnus Damm44358042013-02-18 23:28:34 +0900447 ret = -EINVAL;
Magnus Damm08eba5b2013-02-26 20:59:13 +0900448 goto err0;
Magnus Damm44358042013-02-18 23:28:34 +0900449 }
450 }
451
452 /* allow any number of IRQs between 1 and INTC_IRQPIN_MAX */
453 for (k = 0; k < INTC_IRQPIN_MAX; k++) {
454 irq = platform_get_resource(pdev, IORESOURCE_IRQ, k);
455 if (!irq)
456 break;
457
Magnus Damm44358042013-02-18 23:28:34 +0900458 p->irq[k].p = p;
Magnus Damm33f958f2013-02-26 20:58:54 +0900459 p->irq[k].requested_irq = irq->start;
Magnus Damm44358042013-02-18 23:28:34 +0900460 }
461
Geert Uytterhoeven1affe592015-11-24 15:49:41 +0100462 nirqs = k;
463 if (nirqs < 1) {
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200464 dev_err(dev, "not enough IRQ resources\n");
Magnus Damm44358042013-02-18 23:28:34 +0900465 ret = -EINVAL;
Magnus Damm08eba5b2013-02-26 20:59:13 +0900466 goto err0;
Magnus Damm44358042013-02-18 23:28:34 +0900467 }
468
469 /* ioremap IOMEM and setup read/write callbacks */
470 for (k = 0; k < INTC_IRQPIN_REG_NR; k++) {
471 i = &p->iomem[k];
472
Magnus Damme03f9082014-12-03 21:18:03 +0900473 /* handle optional registers */
474 if (!io[k])
475 continue;
476
Magnus Damm44358042013-02-18 23:28:34 +0900477 switch (resource_size(io[k])) {
478 case 1:
479 i->width = 8;
480 i->read = intc_irqpin_read8;
481 i->write = intc_irqpin_write8;
482 break;
483 case 4:
484 i->width = 32;
485 i->read = intc_irqpin_read32;
486 i->write = intc_irqpin_write32;
487 break;
488 default:
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200489 dev_err(dev, "IOMEM size mismatch\n");
Magnus Damm44358042013-02-18 23:28:34 +0900490 ret = -EINVAL;
Magnus Damm08eba5b2013-02-26 20:59:13 +0900491 goto err0;
Magnus Damm44358042013-02-18 23:28:34 +0900492 }
493
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200494 i->iomem = devm_ioremap_nocache(dev, io[k]->start,
Magnus Damm08eba5b2013-02-26 20:59:13 +0900495 resource_size(io[k]));
Magnus Damm44358042013-02-18 23:28:34 +0900496 if (!i->iomem) {
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200497 dev_err(dev, "failed to remap IOMEM\n");
Magnus Damm44358042013-02-18 23:28:34 +0900498 ret = -ENXIO;
Magnus Damm08eba5b2013-02-26 20:59:13 +0900499 goto err0;
Magnus Damm44358042013-02-18 23:28:34 +0900500 }
501 }
502
Magnus Damme03f9082014-12-03 21:18:03 +0900503 /* configure "individual IRQ mode" where needed */
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +0100504 if (config && config->needs_irlm) {
Magnus Damme03f9082014-12-03 21:18:03 +0900505 if (io[INTC_IRQPIN_REG_IRLM])
506 intc_irqpin_read_modify_write(p, INTC_IRQPIN_REG_IRLM,
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +0100507 config->irlm_bit, 1, 1);
Magnus Damme03f9082014-12-03 21:18:03 +0900508 else
509 dev_warn(dev, "unable to select IRLM mode\n");
510 }
511
Magnus Damm44358042013-02-18 23:28:34 +0900512 /* mask all interrupts using priority */
Geert Uytterhoeven1affe592015-11-24 15:49:41 +0100513 for (k = 0; k < nirqs; k++)
Magnus Damm44358042013-02-18 23:28:34 +0900514 intc_irqpin_mask_unmask_prio(p, k, 1);
515
Bastian Hecht427cc722013-03-27 14:54:03 +0100516 /* clear all pending interrupts */
517 intc_irqpin_write(p, INTC_IRQPIN_REG_SOURCE, 0x0);
518
519 /* scan for shared interrupt lines */
520 ref_irq = p->irq[0].requested_irq;
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +0100521 p->shared_irqs = 1;
Geert Uytterhoeven1affe592015-11-24 15:49:41 +0100522 for (k = 1; k < nirqs; k++) {
Bastian Hecht427cc722013-03-27 14:54:03 +0100523 if (ref_irq != p->irq[k].requested_irq) {
Geert Uytterhoeven86e57ca2015-11-24 16:08:13 +0100524 p->shared_irqs = 0;
Bastian Hecht427cc722013-03-27 14:54:03 +0100525 break;
526 }
527 }
528
Magnus Damm44358042013-02-18 23:28:34 +0900529 /* use more severe masking method if requested */
Geert Uytterhoevenf9551a92015-11-24 15:49:40 +0100530 if (control_parent) {
Magnus Damm44358042013-02-18 23:28:34 +0900531 enable_fn = intc_irqpin_irq_enable_force;
532 disable_fn = intc_irqpin_irq_disable_force;
Bastian Hecht427cc722013-03-27 14:54:03 +0100533 } else if (!p->shared_irqs) {
Magnus Damm44358042013-02-18 23:28:34 +0900534 enable_fn = intc_irqpin_irq_enable;
535 disable_fn = intc_irqpin_irq_disable;
Bastian Hecht427cc722013-03-27 14:54:03 +0100536 } else {
537 enable_fn = intc_irqpin_shared_irq_enable;
538 disable_fn = intc_irqpin_shared_irq_disable;
Magnus Damm44358042013-02-18 23:28:34 +0900539 }
540
541 irq_chip = &p->irq_chip;
542 irq_chip->name = name;
543 irq_chip->irq_mask = disable_fn;
544 irq_chip->irq_unmask = enable_fn;
Magnus Damm44358042013-02-18 23:28:34 +0900545 irq_chip->irq_set_type = intc_irqpin_irq_set_type;
Geert Uytterhoeven705bc962014-09-12 15:15:18 +0200546 irq_chip->irq_set_wake = intc_irqpin_irq_set_wake;
547 irq_chip->flags = IRQCHIP_MASK_ON_SUSPEND;
Magnus Damm44358042013-02-18 23:28:34 +0900548
Geert Uytterhoeven1affe592015-11-24 15:49:41 +0100549 p->irq_domain = irq_domain_add_simple(dev->of_node, nirqs, 0,
550 &intc_irqpin_irq_domain_ops, p);
Magnus Damm44358042013-02-18 23:28:34 +0900551 if (!p->irq_domain) {
552 ret = -ENXIO;
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200553 dev_err(dev, "cannot initialize irq domain\n");
Magnus Damm08eba5b2013-02-26 20:59:13 +0900554 goto err0;
Magnus Damm44358042013-02-18 23:28:34 +0900555 }
556
Bastian Hecht427cc722013-03-27 14:54:03 +0100557 if (p->shared_irqs) {
558 /* request one shared interrupt */
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200559 if (devm_request_irq(dev, p->irq[0].requested_irq,
Bastian Hecht427cc722013-03-27 14:54:03 +0100560 intc_irqpin_shared_irq_handler,
561 IRQF_SHARED, name, p)) {
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200562 dev_err(dev, "failed to request low IRQ\n");
Magnus Damm44358042013-02-18 23:28:34 +0900563 ret = -ENOENT;
Magnus Damm08eba5b2013-02-26 20:59:13 +0900564 goto err1;
Magnus Damm44358042013-02-18 23:28:34 +0900565 }
Bastian Hecht427cc722013-03-27 14:54:03 +0100566 } else {
567 /* request interrupts one by one */
Geert Uytterhoeven1affe592015-11-24 15:49:41 +0100568 for (k = 0; k < nirqs; k++) {
Geert Uytterhoeven36845f12014-09-12 15:15:17 +0200569 if (devm_request_irq(dev, p->irq[k].requested_irq,
570 intc_irqpin_irq_handler, 0, name,
571 &p->irq[k])) {
572 dev_err(dev, "failed to request low IRQ\n");
Bastian Hecht427cc722013-03-27 14:54:03 +0100573 ret = -ENOENT;
574 goto err1;
575 }
576 }
Magnus Damm44358042013-02-18 23:28:34 +0900577 }
578
Bastian Hecht427cc722013-03-27 14:54:03 +0100579 /* unmask all interrupts on prio level */
Geert Uytterhoeven1affe592015-11-24 15:49:41 +0100580 for (k = 0; k < nirqs; k++)
Bastian Hecht427cc722013-03-27 14:54:03 +0100581 intc_irqpin_mask_unmask_prio(p, k, 0);
582
Geert Uytterhoeven1affe592015-11-24 15:49:41 +0100583 dev_info(dev, "driving %d irqs\n", nirqs);
Magnus Damm44358042013-02-18 23:28:34 +0900584
Magnus Damm44358042013-02-18 23:28:34 +0900585 return 0;
586
Magnus Damm44358042013-02-18 23:28:34 +0900587err1:
Magnus Damm08eba5b2013-02-26 20:59:13 +0900588 irq_domain_remove(p->irq_domain);
Magnus Damm44358042013-02-18 23:28:34 +0900589err0:
Geert Uytterhoeven705bc962014-09-12 15:15:18 +0200590 pm_runtime_put(dev);
591 pm_runtime_disable(dev);
Magnus Damm44358042013-02-18 23:28:34 +0900592 return ret;
593}
594
595static int intc_irqpin_remove(struct platform_device *pdev)
596{
597 struct intc_irqpin_priv *p = platform_get_drvdata(pdev);
Magnus Damm44358042013-02-18 23:28:34 +0900598
599 irq_domain_remove(p->irq_domain);
Geert Uytterhoeven705bc962014-09-12 15:15:18 +0200600 pm_runtime_put(&pdev->dev);
601 pm_runtime_disable(&pdev->dev);
Magnus Damm44358042013-02-18 23:28:34 +0900602 return 0;
603}
604
605static struct platform_driver intc_irqpin_device_driver = {
606 .probe = intc_irqpin_probe,
607 .remove = intc_irqpin_remove,
608 .driver = {
609 .name = "renesas_intc_irqpin",
Magnus Damm9d833bbe2013-03-06 15:16:08 +0900610 .of_match_table = intc_irqpin_dt_ids,
Magnus Damm44358042013-02-18 23:28:34 +0900611 }
612};
613
614static int __init intc_irqpin_init(void)
615{
616 return platform_driver_register(&intc_irqpin_device_driver);
617}
618postcore_initcall(intc_irqpin_init);
619
620static void __exit intc_irqpin_exit(void)
621{
622 platform_driver_unregister(&intc_irqpin_device_driver);
623}
624module_exit(intc_irqpin_exit);
625
626MODULE_AUTHOR("Magnus Damm");
627MODULE_DESCRIPTION("Renesas INTC External IRQ Pin Driver");
628MODULE_LICENSE("GPL v2");