1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
7 #include <linux/init.h>
8 #include <linux/interrupt.h>
10 #include <linux/irqchip.h>
11 #include <linux/irqdomain.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/soc/qcom/irq.h>
19 #include <linux/spinlock.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
23 #define PDC_MAX_GPIO_IRQS 256
25 /* Valid only on HW version < 3.2 */
26 #define IRQ_ENABLE_BANK 0x10
27 #define IRQ_i_CFG 0x110
29 /* Valid only on HW version >= 3.2 */
30 #define IRQ_i_CFG_IRQ_ENABLE 3
32 #define IRQ_i_CFG_TYPE_MASK GENMASK(2, 0)
34 #define PDC_VERSION_REG 0x1000
36 /* Notable PDC versions */
37 #define PDC_VERSION_3_2 0x30200
39 struct pdc_pin_region
{
45 #define pin_to_hwirq(r, p) ((r)->parent_base + (p) - (r)->pin_base)
47 static DEFINE_RAW_SPINLOCK(pdc_lock
);
48 static void __iomem
*pdc_base
;
49 static struct pdc_pin_region
*pdc_region
;
50 static int pdc_region_cnt
;
51 static unsigned int pdc_version
;
53 static void pdc_reg_write(int reg
, u32 i
, u32 val
)
55 writel_relaxed(val
, pdc_base
+ reg
+ i
* sizeof(u32
));
58 static u32
pdc_reg_read(int reg
, u32 i
)
60 return readl_relaxed(pdc_base
+ reg
+ i
* sizeof(u32
));
63 static void __pdc_enable_intr(int pin_out
, bool on
)
67 if (pdc_version
< PDC_VERSION_3_2
) {
73 enable
= pdc_reg_read(IRQ_ENABLE_BANK
, index
);
74 __assign_bit(mask
, &enable
, on
);
75 pdc_reg_write(IRQ_ENABLE_BANK
, index
, enable
);
77 enable
= pdc_reg_read(IRQ_i_CFG
, pin_out
);
78 __assign_bit(IRQ_i_CFG_IRQ_ENABLE
, &enable
, on
);
79 pdc_reg_write(IRQ_i_CFG
, pin_out
, enable
);
83 static void pdc_enable_intr(struct irq_data
*d
, bool on
)
87 raw_spin_lock_irqsave(&pdc_lock
, flags
);
88 __pdc_enable_intr(d
->hwirq
, on
);
89 raw_spin_unlock_irqrestore(&pdc_lock
, flags
);
92 static void qcom_pdc_gic_disable(struct irq_data
*d
)
94 pdc_enable_intr(d
, false);
95 irq_chip_disable_parent(d
);
98 static void qcom_pdc_gic_enable(struct irq_data
*d
)
100 pdc_enable_intr(d
, true);
101 irq_chip_enable_parent(d
);
105 * GIC does not handle falling edge or active low. To allow falling edge and
106 * active low interrupts to be handled at GIC, PDC has an inverter that inverts
107 * falling edge into a rising edge and active low into an active high.
108 * For the inverter to work, the polarity bit in the IRQ_CONFIG register has to
109 * set as per the table below.
110 * Level sensitive active low LOW
111 * Rising edge sensitive NOT USED
112 * Falling edge sensitive LOW
113 * Dual Edge sensitive NOT USED
114 * Level sensitive active High HIGH
115 * Falling Edge sensitive NOT USED
116 * Rising edge sensitive HIGH
117 * Dual Edge sensitive HIGH
119 enum pdc_irq_config_bits
{
120 PDC_LEVEL_LOW
= 0b000,
121 PDC_EDGE_FALLING
= 0b010,
122 PDC_LEVEL_HIGH
= 0b100,
123 PDC_EDGE_RISING
= 0b110,
124 PDC_EDGE_DUAL
= 0b111,
128 * qcom_pdc_gic_set_type: Configure PDC for the interrupt
130 * @d: the interrupt data
131 * @type: the interrupt type
133 * If @type is edge triggered, forward that as Rising edge as PDC
134 * takes care of converting falling edge to rising edge signal
135 * If @type is level, then forward that as level high as PDC
136 * takes care of converting falling edge to rising edge signal
138 static int qcom_pdc_gic_set_type(struct irq_data
*d
, unsigned int type
)
140 enum pdc_irq_config_bits pdc_type
;
141 enum pdc_irq_config_bits old_pdc_type
;
145 case IRQ_TYPE_EDGE_RISING
:
146 pdc_type
= PDC_EDGE_RISING
;
148 case IRQ_TYPE_EDGE_FALLING
:
149 pdc_type
= PDC_EDGE_FALLING
;
150 type
= IRQ_TYPE_EDGE_RISING
;
152 case IRQ_TYPE_EDGE_BOTH
:
153 pdc_type
= PDC_EDGE_DUAL
;
154 type
= IRQ_TYPE_EDGE_RISING
;
156 case IRQ_TYPE_LEVEL_HIGH
:
157 pdc_type
= PDC_LEVEL_HIGH
;
159 case IRQ_TYPE_LEVEL_LOW
:
160 pdc_type
= PDC_LEVEL_LOW
;
161 type
= IRQ_TYPE_LEVEL_HIGH
;
168 old_pdc_type
= pdc_reg_read(IRQ_i_CFG
, d
->hwirq
);
169 pdc_type
|= (old_pdc_type
& ~IRQ_i_CFG_TYPE_MASK
);
170 pdc_reg_write(IRQ_i_CFG
, d
->hwirq
, pdc_type
);
172 ret
= irq_chip_set_type_parent(d
, type
);
177 * When we change types the PDC can give a phantom interrupt.
178 * Clear it. Specifically the phantom shows up when reconfiguring
179 * polarity of interrupt without changing the state of the signal
180 * but let's be consistent and clear it always.
182 * Doing this works because we have IRQCHIP_SET_TYPE_MASKED so the
183 * interrupt will be cleared before the rest of the system sees it.
185 if (old_pdc_type
!= pdc_type
)
186 irq_chip_set_parent_state(d
, IRQCHIP_STATE_PENDING
, false);
191 static struct irq_chip qcom_pdc_gic_chip
= {
193 .irq_eoi
= irq_chip_eoi_parent
,
194 .irq_mask
= irq_chip_mask_parent
,
195 .irq_unmask
= irq_chip_unmask_parent
,
196 .irq_disable
= qcom_pdc_gic_disable
,
197 .irq_enable
= qcom_pdc_gic_enable
,
198 .irq_get_irqchip_state
= irq_chip_get_parent_state
,
199 .irq_set_irqchip_state
= irq_chip_set_parent_state
,
200 .irq_retrigger
= irq_chip_retrigger_hierarchy
,
201 .irq_set_type
= qcom_pdc_gic_set_type
,
202 .flags
= IRQCHIP_MASK_ON_SUSPEND
|
203 IRQCHIP_SET_TYPE_MASKED
|
204 IRQCHIP_SKIP_SET_WAKE
|
205 IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND
,
206 .irq_set_vcpu_affinity
= irq_chip_set_vcpu_affinity_parent
,
207 .irq_set_affinity
= irq_chip_set_affinity_parent
,
210 static struct pdc_pin_region
*get_pin_region(int pin
)
214 for (i
= 0; i
< pdc_region_cnt
; i
++) {
215 if (pin
>= pdc_region
[i
].pin_base
&&
216 pin
< pdc_region
[i
].pin_base
+ pdc_region
[i
].cnt
)
217 return &pdc_region
[i
];
223 static int qcom_pdc_alloc(struct irq_domain
*domain
, unsigned int virq
,
224 unsigned int nr_irqs
, void *data
)
226 struct irq_fwspec
*fwspec
= data
;
227 struct irq_fwspec parent_fwspec
;
228 struct pdc_pin_region
*region
;
229 irq_hw_number_t hwirq
;
233 ret
= irq_domain_translate_twocell(domain
, fwspec
, &hwirq
, &type
);
237 if (hwirq
== GPIO_NO_WAKE_IRQ
)
238 return irq_domain_disconnect_hierarchy(domain
, virq
);
240 ret
= irq_domain_set_hwirq_and_chip(domain
, virq
, hwirq
,
241 &qcom_pdc_gic_chip
, NULL
);
245 region
= get_pin_region(hwirq
);
247 return irq_domain_disconnect_hierarchy(domain
->parent
, virq
);
249 if (type
& IRQ_TYPE_EDGE_BOTH
)
250 type
= IRQ_TYPE_EDGE_RISING
;
252 if (type
& IRQ_TYPE_LEVEL_MASK
)
253 type
= IRQ_TYPE_LEVEL_HIGH
;
255 parent_fwspec
.fwnode
= domain
->parent
->fwnode
;
256 parent_fwspec
.param_count
= 3;
257 parent_fwspec
.param
[0] = 0;
258 parent_fwspec
.param
[1] = pin_to_hwirq(region
, hwirq
);
259 parent_fwspec
.param
[2] = type
;
261 return irq_domain_alloc_irqs_parent(domain
, virq
, nr_irqs
,
265 static const struct irq_domain_ops qcom_pdc_ops
= {
266 .translate
= irq_domain_translate_twocell
,
267 .alloc
= qcom_pdc_alloc
,
268 .free
= irq_domain_free_irqs_common
,
271 static int pdc_setup_pin_mapping(struct device_node
*np
)
275 n
= of_property_count_elems_of_size(np
, "qcom,pdc-ranges", sizeof(u32
));
279 pdc_region_cnt
= n
/ 3;
280 pdc_region
= kcalloc(pdc_region_cnt
, sizeof(*pdc_region
), GFP_KERNEL
);
286 for (n
= 0; n
< pdc_region_cnt
; n
++) {
287 ret
= of_property_read_u32_index(np
, "qcom,pdc-ranges",
289 &pdc_region
[n
].pin_base
);
292 ret
= of_property_read_u32_index(np
, "qcom,pdc-ranges",
294 &pdc_region
[n
].parent_base
);
297 ret
= of_property_read_u32_index(np
, "qcom,pdc-ranges",
303 for (i
= 0; i
< pdc_region
[n
].cnt
; i
++)
304 __pdc_enable_intr(i
+ pdc_region
[n
].pin_base
, 0);
310 #define QCOM_PDC_SIZE 0x30000
312 static int qcom_pdc_init(struct device_node
*node
, struct device_node
*parent
)
314 struct irq_domain
*parent_domain
, *pdc_domain
;
315 resource_size_t res_size
;
319 /* compat with old sm8150 DT which had very small region for PDC */
320 if (of_address_to_resource(node
, 0, &res
))
323 res_size
= max_t(resource_size_t
, resource_size(&res
), QCOM_PDC_SIZE
);
324 if (res_size
> resource_size(&res
))
325 pr_warn("%pOF: invalid reg size, please fix DT\n", node
);
327 pdc_base
= ioremap(res
.start
, res_size
);
329 pr_err("%pOF: unable to map PDC registers\n", node
);
333 pdc_version
= pdc_reg_read(PDC_VERSION_REG
, 0);
335 parent_domain
= irq_find_host(parent
);
336 if (!parent_domain
) {
337 pr_err("%pOF: unable to find PDC's parent domain\n", node
);
342 ret
= pdc_setup_pin_mapping(node
);
344 pr_err("%pOF: failed to init PDC pin-hwirq mapping\n", node
);
348 pdc_domain
= irq_domain_create_hierarchy(parent_domain
,
349 IRQ_DOMAIN_FLAG_QCOM_PDC_WAKEUP
,
351 of_fwnode_handle(node
),
352 &qcom_pdc_ops
, NULL
);
354 pr_err("%pOF: PDC domain add failed\n", node
);
359 irq_domain_update_bus_token(pdc_domain
, DOMAIN_BUS_WAKEUP
);
369 IRQCHIP_PLATFORM_DRIVER_BEGIN(qcom_pdc
)
370 IRQCHIP_MATCH("qcom,pdc", qcom_pdc_init
)
371 IRQCHIP_PLATFORM_DRIVER_END(qcom_pdc
)
372 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Power Domain Controller");
373 MODULE_LICENSE("GPL v2");