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/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
24 #include <linux/ioport.h>
26 #include <linux/irq.h>
27 #include <linux/irqdomain.h>
28 #include <linux/err.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
31 #include <linux/platform_data/irq-renesas-irqc.h>
33 #define IRQC_IRQ_MAX 32 /* maximum 32 interrupts per driver instance */
35 #define IRQC_REQ_STS 0x00
36 #define IRQC_EN_STS 0x04
37 #define IRQC_EN_SET 0x08
38 #define IRQC_INT_CPU_BASE(n) (0x000 + ((n) * 0x10))
39 #define DETECT_STATUS 0x100
40 #define IRQC_CONFIG(n) (0x180 + ((n) * 0x04))
51 void __iomem
*cpu_int_base
;
52 struct irqc_irq irq
[IRQC_IRQ_MAX
];
53 struct renesas_irqc_config config
;
54 unsigned int number_of_irqs
;
55 struct platform_device
*pdev
;
56 struct irq_chip irq_chip
;
57 struct irq_domain
*irq_domain
;
60 static void irqc_dbg(struct irqc_irq
*i
, char *str
)
62 dev_dbg(&i
->p
->pdev
->dev
, "%s (%d:%d:%d)\n",
63 str
, i
->requested_irq
, i
->hw_irq
, i
->domain_irq
);
66 static void irqc_irq_enable(struct irq_data
*d
)
68 struct irqc_priv
*p
= irq_data_get_irq_chip_data(d
);
69 int hw_irq
= irqd_to_hwirq(d
);
71 irqc_dbg(&p
->irq
[hw_irq
], "enable");
72 iowrite32(BIT(hw_irq
), p
->cpu_int_base
+ IRQC_EN_SET
);
75 static void irqc_irq_disable(struct irq_data
*d
)
77 struct irqc_priv
*p
= irq_data_get_irq_chip_data(d
);
78 int hw_irq
= irqd_to_hwirq(d
);
80 irqc_dbg(&p
->irq
[hw_irq
], "disable");
81 iowrite32(BIT(hw_irq
), p
->cpu_int_base
+ IRQC_EN_STS
);
84 #define INTC_IRQ_SENSE_VALID 0x10
85 #define INTC_IRQ_SENSE(x) (x + INTC_IRQ_SENSE_VALID)
87 static unsigned char irqc_sense
[IRQ_TYPE_SENSE_MASK
+ 1] = {
88 [IRQ_TYPE_LEVEL_LOW
] = INTC_IRQ_SENSE(0x01),
89 [IRQ_TYPE_LEVEL_HIGH
] = INTC_IRQ_SENSE(0x02),
90 [IRQ_TYPE_EDGE_FALLING
] = INTC_IRQ_SENSE(0x04), /* Synchronous */
91 [IRQ_TYPE_EDGE_RISING
] = INTC_IRQ_SENSE(0x08), /* Synchronous */
92 [IRQ_TYPE_EDGE_BOTH
] = INTC_IRQ_SENSE(0x0c), /* Synchronous */
95 static int irqc_irq_set_type(struct irq_data
*d
, unsigned int type
)
97 struct irqc_priv
*p
= irq_data_get_irq_chip_data(d
);
98 int hw_irq
= irqd_to_hwirq(d
);
99 unsigned char value
= irqc_sense
[type
& IRQ_TYPE_SENSE_MASK
];
102 irqc_dbg(&p
->irq
[hw_irq
], "sense");
104 if (!(value
& INTC_IRQ_SENSE_VALID
))
107 tmp
= ioread32(p
->iomem
+ IRQC_CONFIG(hw_irq
));
109 tmp
|= value
^ INTC_IRQ_SENSE_VALID
;
110 iowrite32(tmp
, p
->iomem
+ IRQC_CONFIG(hw_irq
));
114 static irqreturn_t
irqc_irq_handler(int irq
, void *dev_id
)
116 struct irqc_irq
*i
= dev_id
;
117 struct irqc_priv
*p
= i
->p
;
118 unsigned long bit
= BIT(i
->hw_irq
);
120 irqc_dbg(i
, "demux1");
122 if (ioread32(p
->iomem
+ DETECT_STATUS
) & bit
) {
123 iowrite32(bit
, p
->iomem
+ DETECT_STATUS
);
124 irqc_dbg(i
, "demux2");
125 generic_handle_irq(i
->domain_irq
);
131 static int irqc_irq_domain_map(struct irq_domain
*h
, unsigned int virq
,
134 struct irqc_priv
*p
= h
->host_data
;
136 p
->irq
[hw
].domain_irq
= virq
;
137 p
->irq
[hw
].hw_irq
= hw
;
139 irqc_dbg(&p
->irq
[hw
], "map");
140 irq_set_chip_data(virq
, h
->host_data
);
141 irq_set_chip_and_handler(virq
, &p
->irq_chip
, handle_level_irq
);
142 set_irq_flags(virq
, IRQF_VALID
); /* kill me now */
146 static struct irq_domain_ops irqc_irq_domain_ops
= {
147 .map
= irqc_irq_domain_map
,
148 .xlate
= irq_domain_xlate_twocell
,
151 static int irqc_probe(struct platform_device
*pdev
)
153 struct renesas_irqc_config
*pdata
= pdev
->dev
.platform_data
;
156 struct resource
*irq
;
157 struct irq_chip
*irq_chip
;
158 const char *name
= dev_name(&pdev
->dev
);
162 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
164 dev_err(&pdev
->dev
, "failed to allocate driver data\n");
169 /* deal with driver instance configuration */
171 memcpy(&p
->config
, pdata
, sizeof(*pdata
));
174 platform_set_drvdata(pdev
, p
);
176 /* get hold of manadatory IOMEM */
177 io
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
179 dev_err(&pdev
->dev
, "not enough IOMEM resources\n");
184 /* allow any number of IRQs between 1 and IRQC_IRQ_MAX */
185 for (k
= 0; k
< IRQC_IRQ_MAX
; k
++) {
186 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, k
);
191 p
->irq
[k
].requested_irq
= irq
->start
;
194 p
->number_of_irqs
= k
;
195 if (p
->number_of_irqs
< 1) {
196 dev_err(&pdev
->dev
, "not enough IRQ resources\n");
201 /* ioremap IOMEM and setup read/write callbacks */
202 p
->iomem
= ioremap_nocache(io
->start
, resource_size(io
));
204 dev_err(&pdev
->dev
, "failed to remap IOMEM\n");
209 p
->cpu_int_base
= p
->iomem
+ IRQC_INT_CPU_BASE(0); /* SYS-SPI */
211 irq_chip
= &p
->irq_chip
;
212 irq_chip
->name
= name
;
213 irq_chip
->irq_mask
= irqc_irq_disable
;
214 irq_chip
->irq_unmask
= irqc_irq_enable
;
215 irq_chip
->irq_enable
= irqc_irq_enable
;
216 irq_chip
->irq_disable
= irqc_irq_disable
;
217 irq_chip
->irq_set_type
= irqc_irq_set_type
;
218 irq_chip
->flags
= IRQCHIP_SKIP_SET_WAKE
;
220 p
->irq_domain
= irq_domain_add_simple(pdev
->dev
.of_node
,
223 &irqc_irq_domain_ops
, p
);
224 if (!p
->irq_domain
) {
226 dev_err(&pdev
->dev
, "cannot initialize irq domain\n");
230 /* request interrupts one by one */
231 for (k
= 0; k
< p
->number_of_irqs
; k
++) {
232 if (request_irq(p
->irq
[k
].requested_irq
, irqc_irq_handler
,
233 0, name
, &p
->irq
[k
])) {
234 dev_err(&pdev
->dev
, "failed to request IRQ\n");
240 dev_info(&pdev
->dev
, "driving %d irqs\n", p
->number_of_irqs
);
242 /* warn in case of mismatch if irq base is specified */
243 if (p
->config
.irq_base
) {
244 if (p
->config
.irq_base
!= p
->irq
[0].domain_irq
)
245 dev_warn(&pdev
->dev
, "irq base mismatch (%d/%d)\n",
246 p
->config
.irq_base
, p
->irq
[0].domain_irq
);
252 free_irq(p
->irq
[k
].requested_irq
, &p
->irq
[k
]);
254 irq_domain_remove(p
->irq_domain
);
263 static int irqc_remove(struct platform_device
*pdev
)
265 struct irqc_priv
*p
= platform_get_drvdata(pdev
);
268 for (k
= 0; k
< p
->number_of_irqs
; k
++)
269 free_irq(p
->irq
[k
].requested_irq
, &p
->irq
[k
]);
271 irq_domain_remove(p
->irq_domain
);
277 static const struct of_device_id irqc_dt_ids
[] = {
278 { .compatible
= "renesas,irqc", },
281 MODULE_DEVICE_TABLE(of
, irqc_dt_ids
);
283 static struct platform_driver irqc_device_driver
= {
285 .remove
= irqc_remove
,
287 .name
= "renesas_irqc",
288 .of_match_table
= irqc_dt_ids
,
289 .owner
= THIS_MODULE
,
293 static int __init
irqc_init(void)
295 return platform_driver_register(&irqc_device_driver
);
297 postcore_initcall(irqc_init
);
299 static void __exit
irqc_exit(void)
301 platform_driver_unregister(&irqc_device_driver
);
303 module_exit(irqc_exit
);
305 MODULE_AUTHOR("Magnus Damm");
306 MODULE_DESCRIPTION("Renesas IRQC Driver");
307 MODULE_LICENSE("GPL v2");