xref: /linux/drivers/irqchip/qcom-pdc.c (revision 4acd8a4be614a6c191273f2247aff7374a92f318)
1f55c73aeSArchana Sathyakumar // SPDX-License-Identifier: GPL-2.0
2f55c73aeSArchana Sathyakumar /*
3b2bb01edSLina Iyer  * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
4f55c73aeSArchana Sathyakumar  */
5f55c73aeSArchana Sathyakumar 
6f55c73aeSArchana Sathyakumar #include <linux/err.h>
7f55c73aeSArchana Sathyakumar #include <linux/init.h>
8e71374c0SMaulik Shah #include <linux/interrupt.h>
9f55c73aeSArchana Sathyakumar #include <linux/irq.h>
10f55c73aeSArchana Sathyakumar #include <linux/irqchip.h>
11f55c73aeSArchana Sathyakumar #include <linux/irqdomain.h>
12f55c73aeSArchana Sathyakumar #include <linux/io.h>
13f55c73aeSArchana Sathyakumar #include <linux/kernel.h>
14*4acd8a4bSSaravana Kannan #include <linux/module.h>
15f55c73aeSArchana Sathyakumar #include <linux/of.h>
16f55c73aeSArchana Sathyakumar #include <linux/of_address.h>
17f55c73aeSArchana Sathyakumar #include <linux/of_device.h>
18*4acd8a4bSSaravana Kannan #include <linux/of_irq.h>
1981ef8bf8SLina Iyer #include <linux/soc/qcom/irq.h>
20f55c73aeSArchana Sathyakumar #include <linux/spinlock.h>
21f55c73aeSArchana Sathyakumar #include <linux/slab.h>
22f55c73aeSArchana Sathyakumar #include <linux/types.h>
23f55c73aeSArchana Sathyakumar 
24b2bb01edSLina Iyer #define PDC_MAX_IRQS		168
2581ef8bf8SLina Iyer #define PDC_MAX_GPIO_IRQS	256
26f55c73aeSArchana Sathyakumar 
27f55c73aeSArchana Sathyakumar #define CLEAR_INTR(reg, intr)	(reg & ~(1 << intr))
28f55c73aeSArchana Sathyakumar #define ENABLE_INTR(reg, intr)	(reg | (1 << intr))
29f55c73aeSArchana Sathyakumar 
30f55c73aeSArchana Sathyakumar #define IRQ_ENABLE_BANK		0x10
31f55c73aeSArchana Sathyakumar #define IRQ_i_CFG		0x110
32f55c73aeSArchana Sathyakumar 
3381ef8bf8SLina Iyer #define PDC_NO_PARENT_IRQ	~0UL
3481ef8bf8SLina Iyer 
35f55c73aeSArchana Sathyakumar struct pdc_pin_region {
36f55c73aeSArchana Sathyakumar 	u32 pin_base;
37f55c73aeSArchana Sathyakumar 	u32 parent_base;
38f55c73aeSArchana Sathyakumar 	u32 cnt;
39f55c73aeSArchana Sathyakumar };
40f55c73aeSArchana Sathyakumar 
41f55c73aeSArchana Sathyakumar static DEFINE_RAW_SPINLOCK(pdc_lock);
42f55c73aeSArchana Sathyakumar static void __iomem *pdc_base;
43f55c73aeSArchana Sathyakumar static struct pdc_pin_region *pdc_region;
44f55c73aeSArchana Sathyakumar static int pdc_region_cnt;
45f55c73aeSArchana Sathyakumar 
46f55c73aeSArchana Sathyakumar static void pdc_reg_write(int reg, u32 i, u32 val)
47f55c73aeSArchana Sathyakumar {
48f55c73aeSArchana Sathyakumar 	writel_relaxed(val, pdc_base + reg + i * sizeof(u32));
49f55c73aeSArchana Sathyakumar }
50f55c73aeSArchana Sathyakumar 
51f55c73aeSArchana Sathyakumar static u32 pdc_reg_read(int reg, u32 i)
52f55c73aeSArchana Sathyakumar {
53f55c73aeSArchana Sathyakumar 	return readl_relaxed(pdc_base + reg + i * sizeof(u32));
54f55c73aeSArchana Sathyakumar }
55f55c73aeSArchana Sathyakumar 
56e71374c0SMaulik Shah static int qcom_pdc_gic_get_irqchip_state(struct irq_data *d,
57e71374c0SMaulik Shah 					  enum irqchip_irq_state which,
58e71374c0SMaulik Shah 					  bool *state)
59e71374c0SMaulik Shah {
60e71374c0SMaulik Shah 	if (d->hwirq == GPIO_NO_WAKE_IRQ)
61e71374c0SMaulik Shah 		return 0;
62e71374c0SMaulik Shah 
63e71374c0SMaulik Shah 	return irq_chip_get_parent_state(d, which, state);
64e71374c0SMaulik Shah }
65e71374c0SMaulik Shah 
66e71374c0SMaulik Shah static int qcom_pdc_gic_set_irqchip_state(struct irq_data *d,
67e71374c0SMaulik Shah 					  enum irqchip_irq_state which,
68e71374c0SMaulik Shah 					  bool value)
69e71374c0SMaulik Shah {
70e71374c0SMaulik Shah 	if (d->hwirq == GPIO_NO_WAKE_IRQ)
71e71374c0SMaulik Shah 		return 0;
72e71374c0SMaulik Shah 
73e71374c0SMaulik Shah 	return irq_chip_set_parent_state(d, which, value);
74e71374c0SMaulik Shah }
75e71374c0SMaulik Shah 
76f55c73aeSArchana Sathyakumar static void pdc_enable_intr(struct irq_data *d, bool on)
77f55c73aeSArchana Sathyakumar {
78f55c73aeSArchana Sathyakumar 	int pin_out = d->hwirq;
79f55c73aeSArchana Sathyakumar 	u32 index, mask;
80f55c73aeSArchana Sathyakumar 	u32 enable;
81f55c73aeSArchana Sathyakumar 
82f55c73aeSArchana Sathyakumar 	index = pin_out / 32;
83f55c73aeSArchana Sathyakumar 	mask = pin_out % 32;
84f55c73aeSArchana Sathyakumar 
85f55c73aeSArchana Sathyakumar 	raw_spin_lock(&pdc_lock);
86f55c73aeSArchana Sathyakumar 	enable = pdc_reg_read(IRQ_ENABLE_BANK, index);
87f55c73aeSArchana Sathyakumar 	enable = on ? ENABLE_INTR(enable, mask) : CLEAR_INTR(enable, mask);
88f55c73aeSArchana Sathyakumar 	pdc_reg_write(IRQ_ENABLE_BANK, index, enable);
89f55c73aeSArchana Sathyakumar 	raw_spin_unlock(&pdc_lock);
90f55c73aeSArchana Sathyakumar }
91f55c73aeSArchana Sathyakumar 
92da3f875aSLina Iyer static void qcom_pdc_gic_disable(struct irq_data *d)
93f55c73aeSArchana Sathyakumar {
9481ef8bf8SLina Iyer 	if (d->hwirq == GPIO_NO_WAKE_IRQ)
9581ef8bf8SLina Iyer 		return;
9681ef8bf8SLina Iyer 
97f55c73aeSArchana Sathyakumar 	pdc_enable_intr(d, false);
98da3f875aSLina Iyer 	irq_chip_disable_parent(d);
99da3f875aSLina Iyer }
100da3f875aSLina Iyer 
101da3f875aSLina Iyer static void qcom_pdc_gic_enable(struct irq_data *d)
102da3f875aSLina Iyer {
10381ef8bf8SLina Iyer 	if (d->hwirq == GPIO_NO_WAKE_IRQ)
10481ef8bf8SLina Iyer 		return;
10581ef8bf8SLina Iyer 
106da3f875aSLina Iyer 	pdc_enable_intr(d, true);
107da3f875aSLina Iyer 	irq_chip_enable_parent(d);
108da3f875aSLina Iyer }
109da3f875aSLina Iyer 
110da3f875aSLina Iyer static void qcom_pdc_gic_mask(struct irq_data *d)
111da3f875aSLina Iyer {
11281ef8bf8SLina Iyer 	if (d->hwirq == GPIO_NO_WAKE_IRQ)
11381ef8bf8SLina Iyer 		return;
11481ef8bf8SLina Iyer 
115f55c73aeSArchana Sathyakumar 	irq_chip_mask_parent(d);
116f55c73aeSArchana Sathyakumar }
117f55c73aeSArchana Sathyakumar 
118f55c73aeSArchana Sathyakumar static void qcom_pdc_gic_unmask(struct irq_data *d)
119f55c73aeSArchana Sathyakumar {
12081ef8bf8SLina Iyer 	if (d->hwirq == GPIO_NO_WAKE_IRQ)
12181ef8bf8SLina Iyer 		return;
12281ef8bf8SLina Iyer 
123f55c73aeSArchana Sathyakumar 	irq_chip_unmask_parent(d);
124f55c73aeSArchana Sathyakumar }
125f55c73aeSArchana Sathyakumar 
126f55c73aeSArchana Sathyakumar /*
127f55c73aeSArchana Sathyakumar  * GIC does not handle falling edge or active low. To allow falling edge and
128f55c73aeSArchana Sathyakumar  * active low interrupts to be handled at GIC, PDC has an inverter that inverts
129f55c73aeSArchana Sathyakumar  * falling edge into a rising edge and active low into an active high.
130f55c73aeSArchana Sathyakumar  * For the inverter to work, the polarity bit in the IRQ_CONFIG register has to
131f55c73aeSArchana Sathyakumar  * set as per the table below.
132f55c73aeSArchana Sathyakumar  * Level sensitive active low    LOW
133f55c73aeSArchana Sathyakumar  * Rising edge sensitive         NOT USED
134f55c73aeSArchana Sathyakumar  * Falling edge sensitive        LOW
135f55c73aeSArchana Sathyakumar  * Dual Edge sensitive           NOT USED
136f55c73aeSArchana Sathyakumar  * Level sensitive active High   HIGH
137f55c73aeSArchana Sathyakumar  * Falling Edge sensitive        NOT USED
138f55c73aeSArchana Sathyakumar  * Rising edge sensitive         HIGH
139f55c73aeSArchana Sathyakumar  * Dual Edge sensitive           HIGH
140f55c73aeSArchana Sathyakumar  */
141f55c73aeSArchana Sathyakumar enum pdc_irq_config_bits {
142f55c73aeSArchana Sathyakumar 	PDC_LEVEL_LOW		= 0b000,
143f55c73aeSArchana Sathyakumar 	PDC_EDGE_FALLING	= 0b010,
144f55c73aeSArchana Sathyakumar 	PDC_LEVEL_HIGH		= 0b100,
145f55c73aeSArchana Sathyakumar 	PDC_EDGE_RISING		= 0b110,
146f55c73aeSArchana Sathyakumar 	PDC_EDGE_DUAL		= 0b111,
147f55c73aeSArchana Sathyakumar };
148f55c73aeSArchana Sathyakumar 
149f55c73aeSArchana Sathyakumar /**
150f55c73aeSArchana Sathyakumar  * qcom_pdc_gic_set_type: Configure PDC for the interrupt
151f55c73aeSArchana Sathyakumar  *
152f55c73aeSArchana Sathyakumar  * @d: the interrupt data
153f55c73aeSArchana Sathyakumar  * @type: the interrupt type
154f55c73aeSArchana Sathyakumar  *
155f55c73aeSArchana Sathyakumar  * If @type is edge triggered, forward that as Rising edge as PDC
156f55c73aeSArchana Sathyakumar  * takes care of converting falling edge to rising edge signal
157f55c73aeSArchana Sathyakumar  * If @type is level, then forward that as level high as PDC
158f55c73aeSArchana Sathyakumar  * takes care of converting falling edge to rising edge signal
159f55c73aeSArchana Sathyakumar  */
160f55c73aeSArchana Sathyakumar static int qcom_pdc_gic_set_type(struct irq_data *d, unsigned int type)
161f55c73aeSArchana Sathyakumar {
162f55c73aeSArchana Sathyakumar 	int pin_out = d->hwirq;
163f55c73aeSArchana Sathyakumar 	enum pdc_irq_config_bits pdc_type;
1642f5fbc43SDouglas Anderson 	enum pdc_irq_config_bits old_pdc_type;
1652f5fbc43SDouglas Anderson 	int ret;
166f55c73aeSArchana Sathyakumar 
16781ef8bf8SLina Iyer 	if (pin_out == GPIO_NO_WAKE_IRQ)
16881ef8bf8SLina Iyer 		return 0;
16981ef8bf8SLina Iyer 
170f55c73aeSArchana Sathyakumar 	switch (type) {
171f55c73aeSArchana Sathyakumar 	case IRQ_TYPE_EDGE_RISING:
172f55c73aeSArchana Sathyakumar 		pdc_type = PDC_EDGE_RISING;
173f55c73aeSArchana Sathyakumar 		break;
174f55c73aeSArchana Sathyakumar 	case IRQ_TYPE_EDGE_FALLING:
175f55c73aeSArchana Sathyakumar 		pdc_type = PDC_EDGE_FALLING;
176f55c73aeSArchana Sathyakumar 		type = IRQ_TYPE_EDGE_RISING;
177f55c73aeSArchana Sathyakumar 		break;
178f55c73aeSArchana Sathyakumar 	case IRQ_TYPE_EDGE_BOTH:
179f55c73aeSArchana Sathyakumar 		pdc_type = PDC_EDGE_DUAL;
1807bae48b2SLina Iyer 		type = IRQ_TYPE_EDGE_RISING;
181f55c73aeSArchana Sathyakumar 		break;
182f55c73aeSArchana Sathyakumar 	case IRQ_TYPE_LEVEL_HIGH:
183f55c73aeSArchana Sathyakumar 		pdc_type = PDC_LEVEL_HIGH;
184f55c73aeSArchana Sathyakumar 		break;
185f55c73aeSArchana Sathyakumar 	case IRQ_TYPE_LEVEL_LOW:
186f55c73aeSArchana Sathyakumar 		pdc_type = PDC_LEVEL_LOW;
187f55c73aeSArchana Sathyakumar 		type = IRQ_TYPE_LEVEL_HIGH;
188f55c73aeSArchana Sathyakumar 		break;
189f55c73aeSArchana Sathyakumar 	default:
190f55c73aeSArchana Sathyakumar 		WARN_ON(1);
191f55c73aeSArchana Sathyakumar 		return -EINVAL;
192f55c73aeSArchana Sathyakumar 	}
193f55c73aeSArchana Sathyakumar 
1942f5fbc43SDouglas Anderson 	old_pdc_type = pdc_reg_read(IRQ_i_CFG, pin_out);
195f55c73aeSArchana Sathyakumar 	pdc_reg_write(IRQ_i_CFG, pin_out, pdc_type);
196f55c73aeSArchana Sathyakumar 
1972f5fbc43SDouglas Anderson 	ret = irq_chip_set_type_parent(d, type);
1982f5fbc43SDouglas Anderson 	if (ret)
1992f5fbc43SDouglas Anderson 		return ret;
2002f5fbc43SDouglas Anderson 
2012f5fbc43SDouglas Anderson 	/*
2022f5fbc43SDouglas Anderson 	 * When we change types the PDC can give a phantom interrupt.
2032f5fbc43SDouglas Anderson 	 * Clear it.  Specifically the phantom shows up when reconfiguring
2042f5fbc43SDouglas Anderson 	 * polarity of interrupt without changing the state of the signal
2052f5fbc43SDouglas Anderson 	 * but let's be consistent and clear it always.
2062f5fbc43SDouglas Anderson 	 *
2072f5fbc43SDouglas Anderson 	 * Doing this works because we have IRQCHIP_SET_TYPE_MASKED so the
2082f5fbc43SDouglas Anderson 	 * interrupt will be cleared before the rest of the system sees it.
2092f5fbc43SDouglas Anderson 	 */
2102f5fbc43SDouglas Anderson 	if (old_pdc_type != pdc_type)
2112f5fbc43SDouglas Anderson 		irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
2122f5fbc43SDouglas Anderson 
2132f5fbc43SDouglas Anderson 	return 0;
214f55c73aeSArchana Sathyakumar }
215f55c73aeSArchana Sathyakumar 
216f55c73aeSArchana Sathyakumar static struct irq_chip qcom_pdc_gic_chip = {
217f55c73aeSArchana Sathyakumar 	.name			= "PDC",
218f55c73aeSArchana Sathyakumar 	.irq_eoi		= irq_chip_eoi_parent,
219f55c73aeSArchana Sathyakumar 	.irq_mask		= qcom_pdc_gic_mask,
220f55c73aeSArchana Sathyakumar 	.irq_unmask		= qcom_pdc_gic_unmask,
221da3f875aSLina Iyer 	.irq_disable		= qcom_pdc_gic_disable,
222da3f875aSLina Iyer 	.irq_enable		= qcom_pdc_gic_enable,
223e71374c0SMaulik Shah 	.irq_get_irqchip_state	= qcom_pdc_gic_get_irqchip_state,
224e71374c0SMaulik Shah 	.irq_set_irqchip_state	= qcom_pdc_gic_set_irqchip_state,
225f55c73aeSArchana Sathyakumar 	.irq_retrigger		= irq_chip_retrigger_hierarchy,
226f55c73aeSArchana Sathyakumar 	.irq_set_type		= qcom_pdc_gic_set_type,
227f55c73aeSArchana Sathyakumar 	.flags			= IRQCHIP_MASK_ON_SUSPEND |
228f55c73aeSArchana Sathyakumar 				  IRQCHIP_SET_TYPE_MASKED |
229299d7890SMaulik Shah 				  IRQCHIP_SKIP_SET_WAKE |
230299d7890SMaulik Shah 				  IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND,
231f55c73aeSArchana Sathyakumar 	.irq_set_vcpu_affinity	= irq_chip_set_vcpu_affinity_parent,
232f55c73aeSArchana Sathyakumar 	.irq_set_affinity	= irq_chip_set_affinity_parent,
233f55c73aeSArchana Sathyakumar };
234f55c73aeSArchana Sathyakumar 
235f55c73aeSArchana Sathyakumar static irq_hw_number_t get_parent_hwirq(int pin)
236f55c73aeSArchana Sathyakumar {
237f55c73aeSArchana Sathyakumar 	int i;
238f55c73aeSArchana Sathyakumar 	struct pdc_pin_region *region;
239f55c73aeSArchana Sathyakumar 
240f55c73aeSArchana Sathyakumar 	for (i = 0; i < pdc_region_cnt; i++) {
241f55c73aeSArchana Sathyakumar 		region = &pdc_region[i];
242f55c73aeSArchana Sathyakumar 		if (pin >= region->pin_base &&
243f55c73aeSArchana Sathyakumar 		    pin < region->pin_base + region->cnt)
244f55c73aeSArchana Sathyakumar 			return (region->parent_base + pin - region->pin_base);
245f55c73aeSArchana Sathyakumar 	}
246f55c73aeSArchana Sathyakumar 
24781ef8bf8SLina Iyer 	return PDC_NO_PARENT_IRQ;
248f55c73aeSArchana Sathyakumar }
249f55c73aeSArchana Sathyakumar 
250f55c73aeSArchana Sathyakumar static int qcom_pdc_translate(struct irq_domain *d, struct irq_fwspec *fwspec,
251f55c73aeSArchana Sathyakumar 			      unsigned long *hwirq, unsigned int *type)
252f55c73aeSArchana Sathyakumar {
253f55c73aeSArchana Sathyakumar 	if (is_of_node(fwspec->fwnode)) {
254f55c73aeSArchana Sathyakumar 		if (fwspec->param_count != 2)
255f55c73aeSArchana Sathyakumar 			return -EINVAL;
256f55c73aeSArchana Sathyakumar 
257f55c73aeSArchana Sathyakumar 		*hwirq = fwspec->param[0];
258f55c73aeSArchana Sathyakumar 		*type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
259f55c73aeSArchana Sathyakumar 		return 0;
260f55c73aeSArchana Sathyakumar 	}
261f55c73aeSArchana Sathyakumar 
262f55c73aeSArchana Sathyakumar 	return -EINVAL;
263f55c73aeSArchana Sathyakumar }
264f55c73aeSArchana Sathyakumar 
265f55c73aeSArchana Sathyakumar static int qcom_pdc_alloc(struct irq_domain *domain, unsigned int virq,
266f55c73aeSArchana Sathyakumar 			  unsigned int nr_irqs, void *data)
267f55c73aeSArchana Sathyakumar {
268f55c73aeSArchana Sathyakumar 	struct irq_fwspec *fwspec = data;
269f55c73aeSArchana Sathyakumar 	struct irq_fwspec parent_fwspec;
270f55c73aeSArchana Sathyakumar 	irq_hw_number_t hwirq, parent_hwirq;
271f55c73aeSArchana Sathyakumar 	unsigned int type;
272f55c73aeSArchana Sathyakumar 	int ret;
273f55c73aeSArchana Sathyakumar 
274f55c73aeSArchana Sathyakumar 	ret = qcom_pdc_translate(domain, fwspec, &hwirq, &type);
275f55c73aeSArchana Sathyakumar 	if (ret)
27681ef8bf8SLina Iyer 		return ret;
277f55c73aeSArchana Sathyakumar 
278f55c73aeSArchana Sathyakumar 	ret  = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
279f55c73aeSArchana Sathyakumar 					     &qcom_pdc_gic_chip, NULL);
280f55c73aeSArchana Sathyakumar 	if (ret)
281f55c73aeSArchana Sathyakumar 		return ret;
282f55c73aeSArchana Sathyakumar 
28381ef8bf8SLina Iyer 	parent_hwirq = get_parent_hwirq(hwirq);
28481ef8bf8SLina Iyer 	if (parent_hwirq == PDC_NO_PARENT_IRQ)
28581ef8bf8SLina Iyer 		return 0;
28681ef8bf8SLina Iyer 
287f55c73aeSArchana Sathyakumar 	if (type & IRQ_TYPE_EDGE_BOTH)
288f55c73aeSArchana Sathyakumar 		type = IRQ_TYPE_EDGE_RISING;
289f55c73aeSArchana Sathyakumar 
290f55c73aeSArchana Sathyakumar 	if (type & IRQ_TYPE_LEVEL_MASK)
291f55c73aeSArchana Sathyakumar 		type = IRQ_TYPE_LEVEL_HIGH;
292f55c73aeSArchana Sathyakumar 
293f55c73aeSArchana Sathyakumar 	parent_fwspec.fwnode      = domain->parent->fwnode;
294f55c73aeSArchana Sathyakumar 	parent_fwspec.param_count = 3;
295f55c73aeSArchana Sathyakumar 	parent_fwspec.param[0]    = 0;
296f55c73aeSArchana Sathyakumar 	parent_fwspec.param[1]    = parent_hwirq;
297f55c73aeSArchana Sathyakumar 	parent_fwspec.param[2]    = type;
298f55c73aeSArchana Sathyakumar 
299f55c73aeSArchana Sathyakumar 	return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
300f55c73aeSArchana Sathyakumar 					    &parent_fwspec);
301f55c73aeSArchana Sathyakumar }
302f55c73aeSArchana Sathyakumar 
303f55c73aeSArchana Sathyakumar static const struct irq_domain_ops qcom_pdc_ops = {
304f55c73aeSArchana Sathyakumar 	.translate	= qcom_pdc_translate,
305f55c73aeSArchana Sathyakumar 	.alloc		= qcom_pdc_alloc,
306f55c73aeSArchana Sathyakumar 	.free		= irq_domain_free_irqs_common,
307f55c73aeSArchana Sathyakumar };
308f55c73aeSArchana Sathyakumar 
30981ef8bf8SLina Iyer static int qcom_pdc_gpio_alloc(struct irq_domain *domain, unsigned int virq,
31081ef8bf8SLina Iyer 			       unsigned int nr_irqs, void *data)
31181ef8bf8SLina Iyer {
31281ef8bf8SLina Iyer 	struct irq_fwspec *fwspec = data;
31381ef8bf8SLina Iyer 	struct irq_fwspec parent_fwspec;
31481ef8bf8SLina Iyer 	irq_hw_number_t hwirq, parent_hwirq;
31581ef8bf8SLina Iyer 	unsigned int type;
31681ef8bf8SLina Iyer 	int ret;
31781ef8bf8SLina Iyer 
31881ef8bf8SLina Iyer 	ret = qcom_pdc_translate(domain, fwspec, &hwirq, &type);
31981ef8bf8SLina Iyer 	if (ret)
32081ef8bf8SLina Iyer 		return ret;
32181ef8bf8SLina Iyer 
32281ef8bf8SLina Iyer 	ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
32381ef8bf8SLina Iyer 					    &qcom_pdc_gic_chip, NULL);
32481ef8bf8SLina Iyer 	if (ret)
32581ef8bf8SLina Iyer 		return ret;
32681ef8bf8SLina Iyer 
32781ef8bf8SLina Iyer 	if (hwirq == GPIO_NO_WAKE_IRQ)
32881ef8bf8SLina Iyer 		return 0;
32981ef8bf8SLina Iyer 
33081ef8bf8SLina Iyer 	parent_hwirq = get_parent_hwirq(hwirq);
33181ef8bf8SLina Iyer 	if (parent_hwirq == PDC_NO_PARENT_IRQ)
33281ef8bf8SLina Iyer 		return 0;
33381ef8bf8SLina Iyer 
33481ef8bf8SLina Iyer 	if (type & IRQ_TYPE_EDGE_BOTH)
33581ef8bf8SLina Iyer 		type = IRQ_TYPE_EDGE_RISING;
33681ef8bf8SLina Iyer 
33781ef8bf8SLina Iyer 	if (type & IRQ_TYPE_LEVEL_MASK)
33881ef8bf8SLina Iyer 		type = IRQ_TYPE_LEVEL_HIGH;
33981ef8bf8SLina Iyer 
34081ef8bf8SLina Iyer 	parent_fwspec.fwnode      = domain->parent->fwnode;
34181ef8bf8SLina Iyer 	parent_fwspec.param_count = 3;
34281ef8bf8SLina Iyer 	parent_fwspec.param[0]    = 0;
34381ef8bf8SLina Iyer 	parent_fwspec.param[1]    = parent_hwirq;
34481ef8bf8SLina Iyer 	parent_fwspec.param[2]    = type;
34581ef8bf8SLina Iyer 
34681ef8bf8SLina Iyer 	return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
34781ef8bf8SLina Iyer 					    &parent_fwspec);
34881ef8bf8SLina Iyer }
34981ef8bf8SLina Iyer 
35081ef8bf8SLina Iyer static int qcom_pdc_gpio_domain_select(struct irq_domain *d,
35181ef8bf8SLina Iyer 				       struct irq_fwspec *fwspec,
35281ef8bf8SLina Iyer 				       enum irq_domain_bus_token bus_token)
35381ef8bf8SLina Iyer {
35481ef8bf8SLina Iyer 	return bus_token == DOMAIN_BUS_WAKEUP;
35581ef8bf8SLina Iyer }
35681ef8bf8SLina Iyer 
35781ef8bf8SLina Iyer static const struct irq_domain_ops qcom_pdc_gpio_ops = {
35881ef8bf8SLina Iyer 	.select		= qcom_pdc_gpio_domain_select,
35981ef8bf8SLina Iyer 	.alloc		= qcom_pdc_gpio_alloc,
36081ef8bf8SLina Iyer 	.free		= irq_domain_free_irqs_common,
36181ef8bf8SLina Iyer };
36281ef8bf8SLina Iyer 
363f55c73aeSArchana Sathyakumar static int pdc_setup_pin_mapping(struct device_node *np)
364f55c73aeSArchana Sathyakumar {
365d7bc63faSMaulik Shah 	int ret, n, i;
366d7bc63faSMaulik Shah 	u32 irq_index, reg_index, val;
367f55c73aeSArchana Sathyakumar 
368f55c73aeSArchana Sathyakumar 	n = of_property_count_elems_of_size(np, "qcom,pdc-ranges", sizeof(u32));
369f55c73aeSArchana Sathyakumar 	if (n <= 0 || n % 3)
370f55c73aeSArchana Sathyakumar 		return -EINVAL;
371f55c73aeSArchana Sathyakumar 
372f55c73aeSArchana Sathyakumar 	pdc_region_cnt = n / 3;
373f55c73aeSArchana Sathyakumar 	pdc_region = kcalloc(pdc_region_cnt, sizeof(*pdc_region), GFP_KERNEL);
374f55c73aeSArchana Sathyakumar 	if (!pdc_region) {
375f55c73aeSArchana Sathyakumar 		pdc_region_cnt = 0;
376f55c73aeSArchana Sathyakumar 		return -ENOMEM;
377f55c73aeSArchana Sathyakumar 	}
378f55c73aeSArchana Sathyakumar 
379f55c73aeSArchana Sathyakumar 	for (n = 0; n < pdc_region_cnt; n++) {
380f55c73aeSArchana Sathyakumar 		ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
381f55c73aeSArchana Sathyakumar 						 n * 3 + 0,
382f55c73aeSArchana Sathyakumar 						 &pdc_region[n].pin_base);
383f55c73aeSArchana Sathyakumar 		if (ret)
384f55c73aeSArchana Sathyakumar 			return ret;
385f55c73aeSArchana Sathyakumar 		ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
386f55c73aeSArchana Sathyakumar 						 n * 3 + 1,
387f55c73aeSArchana Sathyakumar 						 &pdc_region[n].parent_base);
388f55c73aeSArchana Sathyakumar 		if (ret)
389f55c73aeSArchana Sathyakumar 			return ret;
390f55c73aeSArchana Sathyakumar 		ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
391f55c73aeSArchana Sathyakumar 						 n * 3 + 2,
392f55c73aeSArchana Sathyakumar 						 &pdc_region[n].cnt);
393f55c73aeSArchana Sathyakumar 		if (ret)
394f55c73aeSArchana Sathyakumar 			return ret;
395d7bc63faSMaulik Shah 
396d7bc63faSMaulik Shah 		for (i = 0; i < pdc_region[n].cnt; i++) {
397d7bc63faSMaulik Shah 			reg_index = (i + pdc_region[n].pin_base) >> 5;
398d7bc63faSMaulik Shah 			irq_index = (i + pdc_region[n].pin_base) & 0x1f;
399d7bc63faSMaulik Shah 			val = pdc_reg_read(IRQ_ENABLE_BANK, reg_index);
400d7bc63faSMaulik Shah 			val &= ~BIT(irq_index);
401d7bc63faSMaulik Shah 			pdc_reg_write(IRQ_ENABLE_BANK, reg_index, val);
402d7bc63faSMaulik Shah 		}
403f55c73aeSArchana Sathyakumar 	}
404f55c73aeSArchana Sathyakumar 
405f55c73aeSArchana Sathyakumar 	return 0;
406f55c73aeSArchana Sathyakumar }
407f55c73aeSArchana Sathyakumar 
408f55c73aeSArchana Sathyakumar static int qcom_pdc_init(struct device_node *node, struct device_node *parent)
409f55c73aeSArchana Sathyakumar {
41081ef8bf8SLina Iyer 	struct irq_domain *parent_domain, *pdc_domain, *pdc_gpio_domain;
411f55c73aeSArchana Sathyakumar 	int ret;
412f55c73aeSArchana Sathyakumar 
413f55c73aeSArchana Sathyakumar 	pdc_base = of_iomap(node, 0);
414f55c73aeSArchana Sathyakumar 	if (!pdc_base) {
415f55c73aeSArchana Sathyakumar 		pr_err("%pOF: unable to map PDC registers\n", node);
416f55c73aeSArchana Sathyakumar 		return -ENXIO;
417f55c73aeSArchana Sathyakumar 	}
418f55c73aeSArchana Sathyakumar 
419f55c73aeSArchana Sathyakumar 	parent_domain = irq_find_host(parent);
420f55c73aeSArchana Sathyakumar 	if (!parent_domain) {
421f55c73aeSArchana Sathyakumar 		pr_err("%pOF: unable to find PDC's parent domain\n", node);
422f55c73aeSArchana Sathyakumar 		ret = -ENXIO;
423f55c73aeSArchana Sathyakumar 		goto fail;
424f55c73aeSArchana Sathyakumar 	}
425f55c73aeSArchana Sathyakumar 
426f55c73aeSArchana Sathyakumar 	ret = pdc_setup_pin_mapping(node);
427f55c73aeSArchana Sathyakumar 	if (ret) {
428f55c73aeSArchana Sathyakumar 		pr_err("%pOF: failed to init PDC pin-hwirq mapping\n", node);
429f55c73aeSArchana Sathyakumar 		goto fail;
430f55c73aeSArchana Sathyakumar 	}
431f55c73aeSArchana Sathyakumar 
432f55c73aeSArchana Sathyakumar 	pdc_domain = irq_domain_create_hierarchy(parent_domain, 0, PDC_MAX_IRQS,
433f55c73aeSArchana Sathyakumar 						 of_fwnode_handle(node),
434f55c73aeSArchana Sathyakumar 						 &qcom_pdc_ops, NULL);
435f55c73aeSArchana Sathyakumar 	if (!pdc_domain) {
436f55c73aeSArchana Sathyakumar 		pr_err("%pOF: GIC domain add failed\n", node);
437f55c73aeSArchana Sathyakumar 		ret = -ENOMEM;
438f55c73aeSArchana Sathyakumar 		goto fail;
439f55c73aeSArchana Sathyakumar 	}
440f55c73aeSArchana Sathyakumar 
44181ef8bf8SLina Iyer 	pdc_gpio_domain = irq_domain_create_hierarchy(parent_domain,
44281ef8bf8SLina Iyer 					IRQ_DOMAIN_FLAG_QCOM_PDC_WAKEUP,
44381ef8bf8SLina Iyer 					PDC_MAX_GPIO_IRQS,
44481ef8bf8SLina Iyer 					of_fwnode_handle(node),
44581ef8bf8SLina Iyer 					&qcom_pdc_gpio_ops, NULL);
44681ef8bf8SLina Iyer 	if (!pdc_gpio_domain) {
44781ef8bf8SLina Iyer 		pr_err("%pOF: PDC domain add failed for GPIO domain\n", node);
44881ef8bf8SLina Iyer 		ret = -ENOMEM;
44981ef8bf8SLina Iyer 		goto remove;
45081ef8bf8SLina Iyer 	}
45181ef8bf8SLina Iyer 
45281ef8bf8SLina Iyer 	irq_domain_update_bus_token(pdc_gpio_domain, DOMAIN_BUS_WAKEUP);
45381ef8bf8SLina Iyer 
454f55c73aeSArchana Sathyakumar 	return 0;
455f55c73aeSArchana Sathyakumar 
45681ef8bf8SLina Iyer remove:
45781ef8bf8SLina Iyer 	irq_domain_remove(pdc_domain);
458f55c73aeSArchana Sathyakumar fail:
459f55c73aeSArchana Sathyakumar 	kfree(pdc_region);
460f55c73aeSArchana Sathyakumar 	iounmap(pdc_base);
461f55c73aeSArchana Sathyakumar 	return ret;
462f55c73aeSArchana Sathyakumar }
463f55c73aeSArchana Sathyakumar 
464*4acd8a4bSSaravana Kannan IRQCHIP_PLATFORM_DRIVER_BEGIN(qcom_pdc)
465*4acd8a4bSSaravana Kannan IRQCHIP_MATCH("qcom,pdc", qcom_pdc_init)
466*4acd8a4bSSaravana Kannan IRQCHIP_PLATFORM_DRIVER_END(qcom_pdc)
467*4acd8a4bSSaravana Kannan MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Power Domain Controller");
468*4acd8a4bSSaravana Kannan MODULE_LICENSE("GPL v2");
469