2 * Renesas INTC External IRQ Pin Driver
4 * Copyright (C) 2013 Magnus Damm
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
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.
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
20 #include <linux/clk.h>
21 #include <linux/init.h>
23 #include <linux/platform_device.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <linux/ioport.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>
33 #include <linux/of_device.h>
34 #include <linux/platform_data/irq-renesas-intc-irqpin.h>
35 #include <linux/pm_runtime.h>
37 #define INTC_IRQPIN_MAX 8 /* maximum 8 interrupts per driver instance */
39 #define INTC_IRQPIN_REG_SENSE 0 /* ICRn */
40 #define INTC_IRQPIN_REG_PRIO 1 /* INTPRInn */
41 #define INTC_IRQPIN_REG_SOURCE 2 /* INTREQnn */
42 #define INTC_IRQPIN_REG_MASK 3 /* INTMSKnn */
43 #define INTC_IRQPIN_REG_CLEAR 4 /* INTMSKCLRnn */
44 #define INTC_IRQPIN_REG_NR_MANDATORY 5
45 #define INTC_IRQPIN_REG_IRLM 5 /* ICR0 with IRLM bit (optional) */
46 #define INTC_IRQPIN_REG_NR 6
48 /* INTC external IRQ PIN hardware register access:
50 * SENSE is read-write 32-bit with 2-bits or 4-bits per IRQ (*)
51 * PRIO is read-write 32-bit with 4-bits per IRQ (**)
52 * SOURCE is read-only 32-bit or 8-bit with 1-bit per IRQ (***)
53 * MASK is write-only 32-bit or 8-bit with 1-bit per IRQ (***)
54 * CLEAR is write-only 32-bit or 8-bit with 1-bit per IRQ (***)
56 * (*) May be accessed by more than one driver instance - lock needed
57 * (**) Read-modify-write access by one driver instance - lock needed
58 * (***) Accessed by one driver instance only - no locking needed
61 struct intc_irqpin_iomem
{
63 unsigned long (*read
)(void __iomem
*iomem
);
64 void (*write
)(void __iomem
*iomem
, unsigned long data
);
68 struct intc_irqpin_irq
{
72 struct intc_irqpin_priv
*p
;
75 struct intc_irqpin_priv
{
76 struct intc_irqpin_iomem iomem
[INTC_IRQPIN_REG_NR
];
77 struct intc_irqpin_irq irq
[INTC_IRQPIN_MAX
];
78 struct renesas_intc_irqpin_config config
;
79 unsigned int number_of_irqs
;
80 struct platform_device
*pdev
;
81 struct irq_chip irq_chip
;
82 struct irq_domain
*irq_domain
;
88 struct intc_irqpin_irlm_config
{
89 unsigned int irlm_bit
;
92 static unsigned long intc_irqpin_read32(void __iomem
*iomem
)
94 return ioread32(iomem
);
97 static unsigned long intc_irqpin_read8(void __iomem
*iomem
)
99 return ioread8(iomem
);
102 static void intc_irqpin_write32(void __iomem
*iomem
, unsigned long data
)
104 iowrite32(data
, iomem
);
107 static void intc_irqpin_write8(void __iomem
*iomem
, unsigned long data
)
109 iowrite8(data
, iomem
);
112 static inline unsigned long intc_irqpin_read(struct intc_irqpin_priv
*p
,
115 struct intc_irqpin_iomem
*i
= &p
->iomem
[reg
];
117 return i
->read(i
->iomem
);
120 static inline void intc_irqpin_write(struct intc_irqpin_priv
*p
,
121 int reg
, unsigned long data
)
123 struct intc_irqpin_iomem
*i
= &p
->iomem
[reg
];
125 i
->write(i
->iomem
, data
);
128 static inline unsigned long intc_irqpin_hwirq_mask(struct intc_irqpin_priv
*p
,
131 return BIT((p
->iomem
[reg
].width
- 1) - hw_irq
);
134 static inline void intc_irqpin_irq_write_hwirq(struct intc_irqpin_priv
*p
,
137 intc_irqpin_write(p
, reg
, intc_irqpin_hwirq_mask(p
, reg
, hw_irq
));
140 static DEFINE_RAW_SPINLOCK(intc_irqpin_lock
); /* only used by slow path */
142 static void intc_irqpin_read_modify_write(struct intc_irqpin_priv
*p
,
144 int width
, int value
)
149 raw_spin_lock_irqsave(&intc_irqpin_lock
, flags
);
151 tmp
= intc_irqpin_read(p
, reg
);
152 tmp
&= ~(((1 << width
) - 1) << shift
);
153 tmp
|= value
<< shift
;
154 intc_irqpin_write(p
, reg
, tmp
);
156 raw_spin_unlock_irqrestore(&intc_irqpin_lock
, flags
);
159 static void intc_irqpin_mask_unmask_prio(struct intc_irqpin_priv
*p
,
160 int irq
, int do_mask
)
162 /* The PRIO register is assumed to be 32-bit with fixed 4-bit fields. */
163 int bitfield_width
= 4;
164 int shift
= 32 - (irq
+ 1) * bitfield_width
;
166 intc_irqpin_read_modify_write(p
, INTC_IRQPIN_REG_PRIO
,
167 shift
, bitfield_width
,
168 do_mask
? 0 : (1 << bitfield_width
) - 1);
171 static int intc_irqpin_set_sense(struct intc_irqpin_priv
*p
, int irq
, int value
)
173 /* The SENSE register is assumed to be 32-bit. */
174 int bitfield_width
= p
->config
.sense_bitfield_width
;
175 int shift
= 32 - (irq
+ 1) * bitfield_width
;
177 dev_dbg(&p
->pdev
->dev
, "sense irq = %d, mode = %d\n", irq
, value
);
179 if (value
>= (1 << bitfield_width
))
182 intc_irqpin_read_modify_write(p
, INTC_IRQPIN_REG_SENSE
, shift
,
183 bitfield_width
, value
);
187 static void intc_irqpin_dbg(struct intc_irqpin_irq
*i
, char *str
)
189 dev_dbg(&i
->p
->pdev
->dev
, "%s (%d:%d:%d)\n",
190 str
, i
->requested_irq
, i
->hw_irq
, i
->domain_irq
);
193 static void intc_irqpin_irq_enable(struct irq_data
*d
)
195 struct intc_irqpin_priv
*p
= irq_data_get_irq_chip_data(d
);
196 int hw_irq
= irqd_to_hwirq(d
);
198 intc_irqpin_dbg(&p
->irq
[hw_irq
], "enable");
199 intc_irqpin_irq_write_hwirq(p
, INTC_IRQPIN_REG_CLEAR
, hw_irq
);
202 static void intc_irqpin_irq_disable(struct irq_data
*d
)
204 struct intc_irqpin_priv
*p
= irq_data_get_irq_chip_data(d
);
205 int hw_irq
= irqd_to_hwirq(d
);
207 intc_irqpin_dbg(&p
->irq
[hw_irq
], "disable");
208 intc_irqpin_irq_write_hwirq(p
, INTC_IRQPIN_REG_MASK
, hw_irq
);
211 static void intc_irqpin_shared_irq_enable(struct irq_data
*d
)
213 struct intc_irqpin_priv
*p
= irq_data_get_irq_chip_data(d
);
214 int hw_irq
= irqd_to_hwirq(d
);
216 intc_irqpin_dbg(&p
->irq
[hw_irq
], "shared enable");
217 intc_irqpin_irq_write_hwirq(p
, INTC_IRQPIN_REG_CLEAR
, hw_irq
);
219 p
->shared_irq_mask
&= ~BIT(hw_irq
);
222 static void intc_irqpin_shared_irq_disable(struct irq_data
*d
)
224 struct intc_irqpin_priv
*p
= irq_data_get_irq_chip_data(d
);
225 int hw_irq
= irqd_to_hwirq(d
);
227 intc_irqpin_dbg(&p
->irq
[hw_irq
], "shared disable");
228 intc_irqpin_irq_write_hwirq(p
, INTC_IRQPIN_REG_MASK
, hw_irq
);
230 p
->shared_irq_mask
|= BIT(hw_irq
);
233 static void intc_irqpin_irq_enable_force(struct irq_data
*d
)
235 struct intc_irqpin_priv
*p
= irq_data_get_irq_chip_data(d
);
236 int irq
= p
->irq
[irqd_to_hwirq(d
)].requested_irq
;
238 intc_irqpin_irq_enable(d
);
240 /* enable interrupt through parent interrupt controller,
241 * assumes non-shared interrupt with 1:1 mapping
242 * needed for busted IRQs on some SoCs like sh73a0
244 irq_get_chip(irq
)->irq_unmask(irq_get_irq_data(irq
));
247 static void intc_irqpin_irq_disable_force(struct irq_data
*d
)
249 struct intc_irqpin_priv
*p
= irq_data_get_irq_chip_data(d
);
250 int irq
= p
->irq
[irqd_to_hwirq(d
)].requested_irq
;
252 /* disable interrupt through parent interrupt controller,
253 * assumes non-shared interrupt with 1:1 mapping
254 * needed for busted IRQs on some SoCs like sh73a0
256 irq_get_chip(irq
)->irq_mask(irq_get_irq_data(irq
));
257 intc_irqpin_irq_disable(d
);
260 #define INTC_IRQ_SENSE_VALID 0x10
261 #define INTC_IRQ_SENSE(x) (x + INTC_IRQ_SENSE_VALID)
263 static unsigned char intc_irqpin_sense
[IRQ_TYPE_SENSE_MASK
+ 1] = {
264 [IRQ_TYPE_EDGE_FALLING
] = INTC_IRQ_SENSE(0x00),
265 [IRQ_TYPE_EDGE_RISING
] = INTC_IRQ_SENSE(0x01),
266 [IRQ_TYPE_LEVEL_LOW
] = INTC_IRQ_SENSE(0x02),
267 [IRQ_TYPE_LEVEL_HIGH
] = INTC_IRQ_SENSE(0x03),
268 [IRQ_TYPE_EDGE_BOTH
] = INTC_IRQ_SENSE(0x04),
271 static int intc_irqpin_irq_set_type(struct irq_data
*d
, unsigned int type
)
273 unsigned char value
= intc_irqpin_sense
[type
& IRQ_TYPE_SENSE_MASK
];
274 struct intc_irqpin_priv
*p
= irq_data_get_irq_chip_data(d
);
276 if (!(value
& INTC_IRQ_SENSE_VALID
))
279 return intc_irqpin_set_sense(p
, irqd_to_hwirq(d
),
280 value
^ INTC_IRQ_SENSE_VALID
);
283 static int intc_irqpin_irq_set_wake(struct irq_data
*d
, unsigned int on
)
285 struct intc_irqpin_priv
*p
= irq_data_get_irq_chip_data(d
);
286 int hw_irq
= irqd_to_hwirq(d
);
288 irq_set_irq_wake(p
->irq
[hw_irq
].requested_irq
, on
);
301 static irqreturn_t
intc_irqpin_irq_handler(int irq
, void *dev_id
)
303 struct intc_irqpin_irq
*i
= dev_id
;
304 struct intc_irqpin_priv
*p
= i
->p
;
307 intc_irqpin_dbg(i
, "demux1");
308 bit
= intc_irqpin_hwirq_mask(p
, INTC_IRQPIN_REG_SOURCE
, i
->hw_irq
);
310 if (intc_irqpin_read(p
, INTC_IRQPIN_REG_SOURCE
) & bit
) {
311 intc_irqpin_write(p
, INTC_IRQPIN_REG_SOURCE
, ~bit
);
312 intc_irqpin_dbg(i
, "demux2");
313 generic_handle_irq(i
->domain_irq
);
319 static irqreturn_t
intc_irqpin_shared_irq_handler(int irq
, void *dev_id
)
321 struct intc_irqpin_priv
*p
= dev_id
;
322 unsigned int reg_source
= intc_irqpin_read(p
, INTC_IRQPIN_REG_SOURCE
);
323 irqreturn_t status
= IRQ_NONE
;
326 for (k
= 0; k
< 8; k
++) {
327 if (reg_source
& BIT(7 - k
)) {
328 if (BIT(k
) & p
->shared_irq_mask
)
331 status
|= intc_irqpin_irq_handler(irq
, &p
->irq
[k
]);
339 * This lock class tells lockdep that INTC External IRQ Pin irqs are in a
340 * different category than their parents, so it won't report false recursion.
342 static struct lock_class_key intc_irqpin_irq_lock_class
;
344 static int intc_irqpin_irq_domain_map(struct irq_domain
*h
, unsigned int virq
,
347 struct intc_irqpin_priv
*p
= h
->host_data
;
349 p
->irq
[hw
].domain_irq
= virq
;
350 p
->irq
[hw
].hw_irq
= hw
;
352 intc_irqpin_dbg(&p
->irq
[hw
], "map");
353 irq_set_chip_data(virq
, h
->host_data
);
354 irq_set_lockdep_class(virq
, &intc_irqpin_irq_lock_class
);
355 irq_set_chip_and_handler(virq
, &p
->irq_chip
, handle_level_irq
);
359 static const struct irq_domain_ops intc_irqpin_irq_domain_ops
= {
360 .map
= intc_irqpin_irq_domain_map
,
361 .xlate
= irq_domain_xlate_twocell
,
364 static const struct intc_irqpin_irlm_config intc_irqpin_irlm_r8a777x
= {
365 .irlm_bit
= 23, /* ICR0.IRLM0 */
368 static const struct of_device_id intc_irqpin_dt_ids
[] = {
369 { .compatible
= "renesas,intc-irqpin", },
370 { .compatible
= "renesas,intc-irqpin-r8a7778",
371 .data
= &intc_irqpin_irlm_r8a777x
},
372 { .compatible
= "renesas,intc-irqpin-r8a7779",
373 .data
= &intc_irqpin_irlm_r8a777x
},
376 MODULE_DEVICE_TABLE(of
, intc_irqpin_dt_ids
);
378 static int intc_irqpin_probe(struct platform_device
*pdev
)
380 struct device
*dev
= &pdev
->dev
;
381 struct renesas_intc_irqpin_config
*pdata
= dev
->platform_data
;
382 const struct of_device_id
*of_id
;
383 struct intc_irqpin_priv
*p
;
384 struct intc_irqpin_iomem
*i
;
385 struct resource
*io
[INTC_IRQPIN_REG_NR
];
386 struct resource
*irq
;
387 struct irq_chip
*irq_chip
;
388 void (*enable_fn
)(struct irq_data
*d
);
389 void (*disable_fn
)(struct irq_data
*d
);
390 const char *name
= dev_name(dev
);
395 p
= devm_kzalloc(dev
, sizeof(*p
), GFP_KERNEL
);
397 dev_err(dev
, "failed to allocate driver data\n");
401 /* deal with driver instance configuration */
403 memcpy(&p
->config
, pdata
, sizeof(*pdata
));
405 of_property_read_u32(dev
->of_node
, "sense-bitfield-width",
406 &p
->config
.sense_bitfield_width
);
407 p
->config
.control_parent
= of_property_read_bool(dev
->of_node
,
410 if (!p
->config
.sense_bitfield_width
)
411 p
->config
.sense_bitfield_width
= 4; /* default to 4 bits */
414 platform_set_drvdata(pdev
, p
);
416 p
->clk
= devm_clk_get(dev
, NULL
);
417 if (IS_ERR(p
->clk
)) {
418 dev_warn(dev
, "unable to get clock\n");
422 pm_runtime_enable(dev
);
423 pm_runtime_get_sync(dev
);
425 /* get hold of register banks */
426 memset(io
, 0, sizeof(io
));
427 for (k
= 0; k
< INTC_IRQPIN_REG_NR
; k
++) {
428 io
[k
] = platform_get_resource(pdev
, IORESOURCE_MEM
, k
);
429 if (!io
[k
] && k
< INTC_IRQPIN_REG_NR_MANDATORY
) {
430 dev_err(dev
, "not enough IOMEM resources\n");
436 /* allow any number of IRQs between 1 and INTC_IRQPIN_MAX */
437 for (k
= 0; k
< INTC_IRQPIN_MAX
; k
++) {
438 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, k
);
443 p
->irq
[k
].requested_irq
= irq
->start
;
446 p
->number_of_irqs
= k
;
447 if (p
->number_of_irqs
< 1) {
448 dev_err(dev
, "not enough IRQ resources\n");
453 /* ioremap IOMEM and setup read/write callbacks */
454 for (k
= 0; k
< INTC_IRQPIN_REG_NR
; k
++) {
457 /* handle optional registers */
461 switch (resource_size(io
[k
])) {
464 i
->read
= intc_irqpin_read8
;
465 i
->write
= intc_irqpin_write8
;
469 i
->read
= intc_irqpin_read32
;
470 i
->write
= intc_irqpin_write32
;
473 dev_err(dev
, "IOMEM size mismatch\n");
478 i
->iomem
= devm_ioremap_nocache(dev
, io
[k
]->start
,
479 resource_size(io
[k
]));
481 dev_err(dev
, "failed to remap IOMEM\n");
487 /* configure "individual IRQ mode" where needed */
488 of_id
= of_match_device(intc_irqpin_dt_ids
, dev
);
489 if (of_id
&& of_id
->data
) {
490 const struct intc_irqpin_irlm_config
*irlm_config
= of_id
->data
;
492 if (io
[INTC_IRQPIN_REG_IRLM
])
493 intc_irqpin_read_modify_write(p
, INTC_IRQPIN_REG_IRLM
,
494 irlm_config
->irlm_bit
,
497 dev_warn(dev
, "unable to select IRLM mode\n");
500 /* mask all interrupts using priority */
501 for (k
= 0; k
< p
->number_of_irqs
; k
++)
502 intc_irqpin_mask_unmask_prio(p
, k
, 1);
504 /* clear all pending interrupts */
505 intc_irqpin_write(p
, INTC_IRQPIN_REG_SOURCE
, 0x0);
507 /* scan for shared interrupt lines */
508 ref_irq
= p
->irq
[0].requested_irq
;
509 p
->shared_irqs
= true;
510 for (k
= 1; k
< p
->number_of_irqs
; k
++) {
511 if (ref_irq
!= p
->irq
[k
].requested_irq
) {
512 p
->shared_irqs
= false;
517 /* use more severe masking method if requested */
518 if (p
->config
.control_parent
) {
519 enable_fn
= intc_irqpin_irq_enable_force
;
520 disable_fn
= intc_irqpin_irq_disable_force
;
521 } else if (!p
->shared_irqs
) {
522 enable_fn
= intc_irqpin_irq_enable
;
523 disable_fn
= intc_irqpin_irq_disable
;
525 enable_fn
= intc_irqpin_shared_irq_enable
;
526 disable_fn
= intc_irqpin_shared_irq_disable
;
529 irq_chip
= &p
->irq_chip
;
530 irq_chip
->name
= name
;
531 irq_chip
->irq_mask
= disable_fn
;
532 irq_chip
->irq_unmask
= enable_fn
;
533 irq_chip
->irq_set_type
= intc_irqpin_irq_set_type
;
534 irq_chip
->irq_set_wake
= intc_irqpin_irq_set_wake
;
535 irq_chip
->flags
= IRQCHIP_MASK_ON_SUSPEND
;
537 p
->irq_domain
= irq_domain_add_simple(dev
->of_node
,
540 &intc_irqpin_irq_domain_ops
, p
);
541 if (!p
->irq_domain
) {
543 dev_err(dev
, "cannot initialize irq domain\n");
547 if (p
->shared_irqs
) {
548 /* request one shared interrupt */
549 if (devm_request_irq(dev
, p
->irq
[0].requested_irq
,
550 intc_irqpin_shared_irq_handler
,
551 IRQF_SHARED
, name
, p
)) {
552 dev_err(dev
, "failed to request low IRQ\n");
557 /* request interrupts one by one */
558 for (k
= 0; k
< p
->number_of_irqs
; k
++) {
559 if (devm_request_irq(dev
, p
->irq
[k
].requested_irq
,
560 intc_irqpin_irq_handler
, 0, name
,
562 dev_err(dev
, "failed to request low IRQ\n");
569 /* unmask all interrupts on prio level */
570 for (k
= 0; k
< p
->number_of_irqs
; k
++)
571 intc_irqpin_mask_unmask_prio(p
, k
, 0);
573 dev_info(dev
, "driving %d irqs\n", p
->number_of_irqs
);
575 /* warn in case of mismatch if irq base is specified */
576 if (p
->config
.irq_base
) {
577 if (p
->config
.irq_base
!= p
->irq
[0].domain_irq
)
578 dev_warn(dev
, "irq base mismatch (%d/%d)\n",
579 p
->config
.irq_base
, p
->irq
[0].domain_irq
);
585 irq_domain_remove(p
->irq_domain
);
588 pm_runtime_disable(dev
);
592 static int intc_irqpin_remove(struct platform_device
*pdev
)
594 struct intc_irqpin_priv
*p
= platform_get_drvdata(pdev
);
596 irq_domain_remove(p
->irq_domain
);
597 pm_runtime_put(&pdev
->dev
);
598 pm_runtime_disable(&pdev
->dev
);
602 static struct platform_driver intc_irqpin_device_driver
= {
603 .probe
= intc_irqpin_probe
,
604 .remove
= intc_irqpin_remove
,
606 .name
= "renesas_intc_irqpin",
607 .of_match_table
= intc_irqpin_dt_ids
,
611 static int __init
intc_irqpin_init(void)
613 return platform_driver_register(&intc_irqpin_device_driver
);
615 postcore_initcall(intc_irqpin_init
);
617 static void __exit
intc_irqpin_exit(void)
619 platform_driver_unregister(&intc_irqpin_device_driver
);
621 module_exit(intc_irqpin_exit
);
623 MODULE_AUTHOR("Magnus Damm");
624 MODULE_DESCRIPTION("Renesas INTC External IRQ Pin Driver");
625 MODULE_LICENSE("GPL v2");