xref: /openbmc/linux/drivers/irqchip/irq-mtk-cirq.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29dbbbd33SYoulin Pei /*
39dbbbd33SYoulin Pei  * Copyright (c) 2016 MediaTek Inc.
49dbbbd33SYoulin Pei  * Author: Youlin.Pei <youlin.pei@mediatek.com>
59dbbbd33SYoulin Pei  */
69dbbbd33SYoulin Pei 
79dbbbd33SYoulin Pei #include <linux/interrupt.h>
89dbbbd33SYoulin Pei #include <linux/io.h>
99dbbbd33SYoulin Pei #include <linux/irq.h>
109dbbbd33SYoulin Pei #include <linux/irqchip.h>
119dbbbd33SYoulin Pei #include <linux/irqdomain.h>
129dbbbd33SYoulin Pei #include <linux/of.h>
139dbbbd33SYoulin Pei #include <linux/of_irq.h>
149dbbbd33SYoulin Pei #include <linux/of_address.h>
159dbbbd33SYoulin Pei #include <linux/slab.h>
169dbbbd33SYoulin Pei #include <linux/syscore_ops.h>
179dbbbd33SYoulin Pei 
1845ac0195SAngeloGioacchino Del Regno enum mtk_cirq_regoffs_index {
1945ac0195SAngeloGioacchino Del Regno 	CIRQ_STA,
2045ac0195SAngeloGioacchino Del Regno 	CIRQ_ACK,
2145ac0195SAngeloGioacchino Del Regno 	CIRQ_MASK_SET,
2245ac0195SAngeloGioacchino Del Regno 	CIRQ_MASK_CLR,
2345ac0195SAngeloGioacchino Del Regno 	CIRQ_SENS_SET,
2445ac0195SAngeloGioacchino Del Regno 	CIRQ_SENS_CLR,
2545ac0195SAngeloGioacchino Del Regno 	CIRQ_POL_SET,
2645ac0195SAngeloGioacchino Del Regno 	CIRQ_POL_CLR,
2745ac0195SAngeloGioacchino Del Regno 	CIRQ_CONTROL
2845ac0195SAngeloGioacchino Del Regno };
2945ac0195SAngeloGioacchino Del Regno 
3045ac0195SAngeloGioacchino Del Regno static const u32 mtk_cirq_regoffs_v1[] = {
3145ac0195SAngeloGioacchino Del Regno 	[CIRQ_STA]	= 0x0,
3245ac0195SAngeloGioacchino Del Regno 	[CIRQ_ACK]	= 0x40,
3345ac0195SAngeloGioacchino Del Regno 	[CIRQ_MASK_SET]	= 0xc0,
3445ac0195SAngeloGioacchino Del Regno 	[CIRQ_MASK_CLR]	= 0x100,
3545ac0195SAngeloGioacchino Del Regno 	[CIRQ_SENS_SET]	= 0x180,
3645ac0195SAngeloGioacchino Del Regno 	[CIRQ_SENS_CLR]	= 0x1c0,
3745ac0195SAngeloGioacchino Del Regno 	[CIRQ_POL_SET]	= 0x240,
3845ac0195SAngeloGioacchino Del Regno 	[CIRQ_POL_CLR]	= 0x280,
3945ac0195SAngeloGioacchino Del Regno 	[CIRQ_CONTROL]	= 0x300,
4045ac0195SAngeloGioacchino Del Regno };
419dbbbd33SYoulin Pei 
42*5c4e0aacSAngeloGioacchino Del Regno static const u32 mtk_cirq_regoffs_v2[] = {
43*5c4e0aacSAngeloGioacchino Del Regno 	[CIRQ_STA]	= 0x0,
44*5c4e0aacSAngeloGioacchino Del Regno 	[CIRQ_ACK]	= 0x80,
45*5c4e0aacSAngeloGioacchino Del Regno 	[CIRQ_MASK_SET]	= 0x180,
46*5c4e0aacSAngeloGioacchino Del Regno 	[CIRQ_MASK_CLR]	= 0x200,
47*5c4e0aacSAngeloGioacchino Del Regno 	[CIRQ_SENS_SET]	= 0x300,
48*5c4e0aacSAngeloGioacchino Del Regno 	[CIRQ_SENS_CLR]	= 0x380,
49*5c4e0aacSAngeloGioacchino Del Regno 	[CIRQ_POL_SET]	= 0x480,
50*5c4e0aacSAngeloGioacchino Del Regno 	[CIRQ_POL_CLR]	= 0x500,
51*5c4e0aacSAngeloGioacchino Del Regno 	[CIRQ_CONTROL]	= 0x600,
52*5c4e0aacSAngeloGioacchino Del Regno };
53*5c4e0aacSAngeloGioacchino Del Regno 
549dbbbd33SYoulin Pei #define CIRQ_EN	0x1
559dbbbd33SYoulin Pei #define CIRQ_EDGE	0x2
569dbbbd33SYoulin Pei #define CIRQ_FLUSH	0x4
579dbbbd33SYoulin Pei 
589dbbbd33SYoulin Pei struct mtk_cirq_chip_data {
599dbbbd33SYoulin Pei 	void __iomem *base;
609dbbbd33SYoulin Pei 	unsigned int ext_irq_start;
619dbbbd33SYoulin Pei 	unsigned int ext_irq_end;
6245ac0195SAngeloGioacchino Del Regno 	const u32 *offsets;
639dbbbd33SYoulin Pei 	struct irq_domain *domain;
649dbbbd33SYoulin Pei };
659dbbbd33SYoulin Pei 
669dbbbd33SYoulin Pei static struct mtk_cirq_chip_data *cirq_data;
679dbbbd33SYoulin Pei 
mtk_cirq_reg(struct mtk_cirq_chip_data * chip_data,enum mtk_cirq_regoffs_index idx)6845ac0195SAngeloGioacchino Del Regno static void __iomem *mtk_cirq_reg(struct mtk_cirq_chip_data *chip_data,
6945ac0195SAngeloGioacchino Del Regno 				  enum mtk_cirq_regoffs_index idx)
7045ac0195SAngeloGioacchino Del Regno {
7145ac0195SAngeloGioacchino Del Regno 	return chip_data->base + chip_data->offsets[idx];
7245ac0195SAngeloGioacchino Del Regno }
7345ac0195SAngeloGioacchino Del Regno 
mtk_cirq_irq_reg(struct mtk_cirq_chip_data * chip_data,enum mtk_cirq_regoffs_index idx,unsigned int cirq_num)7445ac0195SAngeloGioacchino Del Regno static void __iomem *mtk_cirq_irq_reg(struct mtk_cirq_chip_data *chip_data,
7545ac0195SAngeloGioacchino Del Regno 				      enum mtk_cirq_regoffs_index idx,
7645ac0195SAngeloGioacchino Del Regno 				      unsigned int cirq_num)
7745ac0195SAngeloGioacchino Del Regno {
7845ac0195SAngeloGioacchino Del Regno 	return mtk_cirq_reg(chip_data, idx) + (cirq_num / 32) * 4;
7945ac0195SAngeloGioacchino Del Regno }
8045ac0195SAngeloGioacchino Del Regno 
mtk_cirq_write_mask(struct irq_data * data,enum mtk_cirq_regoffs_index idx)8145ac0195SAngeloGioacchino Del Regno static void mtk_cirq_write_mask(struct irq_data *data, enum mtk_cirq_regoffs_index idx)
829dbbbd33SYoulin Pei {
839dbbbd33SYoulin Pei 	struct mtk_cirq_chip_data *chip_data = data->chip_data;
849dbbbd33SYoulin Pei 	unsigned int cirq_num = data->hwirq;
859dbbbd33SYoulin Pei 	u32 mask = 1 << (cirq_num % 32);
869dbbbd33SYoulin Pei 
8745ac0195SAngeloGioacchino Del Regno 	writel_relaxed(mask, mtk_cirq_irq_reg(chip_data, idx, cirq_num));
889dbbbd33SYoulin Pei }
899dbbbd33SYoulin Pei 
mtk_cirq_mask(struct irq_data * data)909dbbbd33SYoulin Pei static void mtk_cirq_mask(struct irq_data *data)
919dbbbd33SYoulin Pei {
929dbbbd33SYoulin Pei 	mtk_cirq_write_mask(data, CIRQ_MASK_SET);
939dbbbd33SYoulin Pei 	irq_chip_mask_parent(data);
949dbbbd33SYoulin Pei }
959dbbbd33SYoulin Pei 
mtk_cirq_unmask(struct irq_data * data)969dbbbd33SYoulin Pei static void mtk_cirq_unmask(struct irq_data *data)
979dbbbd33SYoulin Pei {
989dbbbd33SYoulin Pei 	mtk_cirq_write_mask(data, CIRQ_MASK_CLR);
999dbbbd33SYoulin Pei 	irq_chip_unmask_parent(data);
1009dbbbd33SYoulin Pei }
1019dbbbd33SYoulin Pei 
mtk_cirq_set_type(struct irq_data * data,unsigned int type)1029dbbbd33SYoulin Pei static int mtk_cirq_set_type(struct irq_data *data, unsigned int type)
1039dbbbd33SYoulin Pei {
1049dbbbd33SYoulin Pei 	int ret;
1059dbbbd33SYoulin Pei 
1069dbbbd33SYoulin Pei 	switch (type & IRQ_TYPE_SENSE_MASK) {
1079dbbbd33SYoulin Pei 	case IRQ_TYPE_EDGE_FALLING:
1089dbbbd33SYoulin Pei 		mtk_cirq_write_mask(data, CIRQ_POL_CLR);
1099dbbbd33SYoulin Pei 		mtk_cirq_write_mask(data, CIRQ_SENS_CLR);
1109dbbbd33SYoulin Pei 		break;
1119dbbbd33SYoulin Pei 	case IRQ_TYPE_EDGE_RISING:
1129dbbbd33SYoulin Pei 		mtk_cirq_write_mask(data, CIRQ_POL_SET);
1139dbbbd33SYoulin Pei 		mtk_cirq_write_mask(data, CIRQ_SENS_CLR);
1149dbbbd33SYoulin Pei 		break;
1159dbbbd33SYoulin Pei 	case IRQ_TYPE_LEVEL_LOW:
1169dbbbd33SYoulin Pei 		mtk_cirq_write_mask(data, CIRQ_POL_CLR);
1179dbbbd33SYoulin Pei 		mtk_cirq_write_mask(data, CIRQ_SENS_SET);
1189dbbbd33SYoulin Pei 		break;
1199dbbbd33SYoulin Pei 	case IRQ_TYPE_LEVEL_HIGH:
1209dbbbd33SYoulin Pei 		mtk_cirq_write_mask(data, CIRQ_POL_SET);
1219dbbbd33SYoulin Pei 		mtk_cirq_write_mask(data, CIRQ_SENS_SET);
1229dbbbd33SYoulin Pei 		break;
1239dbbbd33SYoulin Pei 	default:
1249dbbbd33SYoulin Pei 		break;
1259dbbbd33SYoulin Pei 	}
1269dbbbd33SYoulin Pei 
1279dbbbd33SYoulin Pei 	data = data->parent_data;
1289dbbbd33SYoulin Pei 	ret = data->chip->irq_set_type(data, type);
1299dbbbd33SYoulin Pei 	return ret;
1309dbbbd33SYoulin Pei }
1319dbbbd33SYoulin Pei 
1329dbbbd33SYoulin Pei static struct irq_chip mtk_cirq_chip = {
1339dbbbd33SYoulin Pei 	.name			= "MT_CIRQ",
1349dbbbd33SYoulin Pei 	.irq_mask		= mtk_cirq_mask,
1359dbbbd33SYoulin Pei 	.irq_unmask		= mtk_cirq_unmask,
1369dbbbd33SYoulin Pei 	.irq_eoi		= irq_chip_eoi_parent,
1379dbbbd33SYoulin Pei 	.irq_set_type		= mtk_cirq_set_type,
1389dbbbd33SYoulin Pei 	.irq_retrigger		= irq_chip_retrigger_hierarchy,
1399dbbbd33SYoulin Pei #ifdef CONFIG_SMP
1409dbbbd33SYoulin Pei 	.irq_set_affinity	= irq_chip_set_affinity_parent,
1419dbbbd33SYoulin Pei #endif
1429dbbbd33SYoulin Pei };
1439dbbbd33SYoulin Pei 
mtk_cirq_domain_translate(struct irq_domain * d,struct irq_fwspec * fwspec,unsigned long * hwirq,unsigned int * type)1449dbbbd33SYoulin Pei static int mtk_cirq_domain_translate(struct irq_domain *d,
1459dbbbd33SYoulin Pei 				     struct irq_fwspec *fwspec,
1469dbbbd33SYoulin Pei 				     unsigned long *hwirq,
1479dbbbd33SYoulin Pei 				     unsigned int *type)
1489dbbbd33SYoulin Pei {
1499dbbbd33SYoulin Pei 	if (is_of_node(fwspec->fwnode)) {
1509dbbbd33SYoulin Pei 		if (fwspec->param_count != 3)
1519dbbbd33SYoulin Pei 			return -EINVAL;
1529dbbbd33SYoulin Pei 
1539dbbbd33SYoulin Pei 		/* No PPI should point to this domain */
1549dbbbd33SYoulin Pei 		if (fwspec->param[0] != 0)
1559dbbbd33SYoulin Pei 			return -EINVAL;
1569dbbbd33SYoulin Pei 
1579dbbbd33SYoulin Pei 		/* cirq support irq number check */
1589dbbbd33SYoulin Pei 		if (fwspec->param[1] < cirq_data->ext_irq_start ||
1599dbbbd33SYoulin Pei 		    fwspec->param[1] > cirq_data->ext_irq_end)
1609dbbbd33SYoulin Pei 			return -EINVAL;
1619dbbbd33SYoulin Pei 
1629dbbbd33SYoulin Pei 		*hwirq = fwspec->param[1] - cirq_data->ext_irq_start;
1639dbbbd33SYoulin Pei 		*type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
1649dbbbd33SYoulin Pei 		return 0;
1659dbbbd33SYoulin Pei 	}
1669dbbbd33SYoulin Pei 
1679dbbbd33SYoulin Pei 	return -EINVAL;
1689dbbbd33SYoulin Pei }
1699dbbbd33SYoulin Pei 
mtk_cirq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)1709dbbbd33SYoulin Pei static int mtk_cirq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1719dbbbd33SYoulin Pei 				 unsigned int nr_irqs, void *arg)
1729dbbbd33SYoulin Pei {
1739dbbbd33SYoulin Pei 	int ret;
1749dbbbd33SYoulin Pei 	irq_hw_number_t hwirq;
1759dbbbd33SYoulin Pei 	unsigned int type;
1769dbbbd33SYoulin Pei 	struct irq_fwspec *fwspec = arg;
1779dbbbd33SYoulin Pei 	struct irq_fwspec parent_fwspec = *fwspec;
1789dbbbd33SYoulin Pei 
1799dbbbd33SYoulin Pei 	ret = mtk_cirq_domain_translate(domain, fwspec, &hwirq, &type);
1809dbbbd33SYoulin Pei 	if (ret)
1819dbbbd33SYoulin Pei 		return ret;
1829dbbbd33SYoulin Pei 
1839dbbbd33SYoulin Pei 	if (WARN_ON(nr_irqs != 1))
1849dbbbd33SYoulin Pei 		return -EINVAL;
1859dbbbd33SYoulin Pei 
1869dbbbd33SYoulin Pei 	irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
1879dbbbd33SYoulin Pei 				      &mtk_cirq_chip,
1889dbbbd33SYoulin Pei 				      domain->host_data);
1899dbbbd33SYoulin Pei 
1909dbbbd33SYoulin Pei 	parent_fwspec.fwnode = domain->parent->fwnode;
1919dbbbd33SYoulin Pei 	return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
1929dbbbd33SYoulin Pei 					    &parent_fwspec);
1939dbbbd33SYoulin Pei }
1949dbbbd33SYoulin Pei 
1959dbbbd33SYoulin Pei static const struct irq_domain_ops cirq_domain_ops = {
1969dbbbd33SYoulin Pei 	.translate	= mtk_cirq_domain_translate,
1979dbbbd33SYoulin Pei 	.alloc		= mtk_cirq_domain_alloc,
1989dbbbd33SYoulin Pei 	.free		= irq_domain_free_irqs_common,
1999dbbbd33SYoulin Pei };
2009dbbbd33SYoulin Pei 
2019dbbbd33SYoulin Pei #ifdef CONFIG_PM_SLEEP
mtk_cirq_suspend(void)2029dbbbd33SYoulin Pei static int mtk_cirq_suspend(void)
2039dbbbd33SYoulin Pei {
20445ac0195SAngeloGioacchino Del Regno 	void __iomem *reg;
2059dbbbd33SYoulin Pei 	u32 value, mask;
2069dbbbd33SYoulin Pei 	unsigned int irq, hwirq_num;
2079dbbbd33SYoulin Pei 	bool pending, masked;
2089dbbbd33SYoulin Pei 	int i, pendret, maskret;
2099dbbbd33SYoulin Pei 
2109dbbbd33SYoulin Pei 	/*
2119dbbbd33SYoulin Pei 	 * When external interrupts happened, CIRQ will record the status
2129dbbbd33SYoulin Pei 	 * even CIRQ is not enabled. When execute flush command, CIRQ will
2139dbbbd33SYoulin Pei 	 * resend the signals according to the status. So if don't clear the
2149dbbbd33SYoulin Pei 	 * status, CIRQ will resend the wrong signals.
2159dbbbd33SYoulin Pei 	 *
2169dbbbd33SYoulin Pei 	 * arch_suspend_disable_irqs() will be called before CIRQ suspend
2179dbbbd33SYoulin Pei 	 * callback. If clear all the status simply, the external interrupts
2189dbbbd33SYoulin Pei 	 * which happened between arch_suspend_disable_irqs and CIRQ suspend
2199dbbbd33SYoulin Pei 	 * callback will be lost. Using following steps to avoid this issue;
2209dbbbd33SYoulin Pei 	 *
2219dbbbd33SYoulin Pei 	 * - Iterate over all the CIRQ supported interrupts;
2229dbbbd33SYoulin Pei 	 * - For each interrupt, inspect its pending and masked status at GIC
2239dbbbd33SYoulin Pei 	 *   level;
2249dbbbd33SYoulin Pei 	 * - If pending and unmasked, it happened between
2259dbbbd33SYoulin Pei 	 *   arch_suspend_disable_irqs and CIRQ suspend callback, don't ACK
2269dbbbd33SYoulin Pei 	 *   it. Otherwise, ACK it.
2279dbbbd33SYoulin Pei 	 */
2289dbbbd33SYoulin Pei 	hwirq_num = cirq_data->ext_irq_end - cirq_data->ext_irq_start + 1;
2299dbbbd33SYoulin Pei 	for (i = 0; i < hwirq_num; i++) {
2309dbbbd33SYoulin Pei 		irq = irq_find_mapping(cirq_data->domain, i);
2319dbbbd33SYoulin Pei 		if (irq) {
2329dbbbd33SYoulin Pei 			pendret = irq_get_irqchip_state(irq,
2339dbbbd33SYoulin Pei 							IRQCHIP_STATE_PENDING,
2349dbbbd33SYoulin Pei 							&pending);
2359dbbbd33SYoulin Pei 
2369dbbbd33SYoulin Pei 			maskret = irq_get_irqchip_state(irq,
2379dbbbd33SYoulin Pei 							IRQCHIP_STATE_MASKED,
2389dbbbd33SYoulin Pei 							&masked);
2399dbbbd33SYoulin Pei 
2409dbbbd33SYoulin Pei 			if (pendret == 0 && maskret == 0 &&
2419dbbbd33SYoulin Pei 			    (pending && !masked))
2429dbbbd33SYoulin Pei 				continue;
2439dbbbd33SYoulin Pei 		}
2449dbbbd33SYoulin Pei 
24545ac0195SAngeloGioacchino Del Regno 		reg = mtk_cirq_irq_reg(cirq_data, CIRQ_ACK, i);
2469dbbbd33SYoulin Pei 		mask = 1 << (i % 32);
24745ac0195SAngeloGioacchino Del Regno 		writel_relaxed(mask, reg);
2489dbbbd33SYoulin Pei 	}
2499dbbbd33SYoulin Pei 
2509dbbbd33SYoulin Pei 	/* set edge_only mode, record edge-triggerd interrupts */
2519dbbbd33SYoulin Pei 	/* enable cirq */
25245ac0195SAngeloGioacchino Del Regno 	reg = mtk_cirq_reg(cirq_data, CIRQ_CONTROL);
25345ac0195SAngeloGioacchino Del Regno 	value = readl_relaxed(reg);
2549dbbbd33SYoulin Pei 	value |= (CIRQ_EDGE | CIRQ_EN);
25545ac0195SAngeloGioacchino Del Regno 	writel_relaxed(value, reg);
2569dbbbd33SYoulin Pei 
2579dbbbd33SYoulin Pei 	return 0;
2589dbbbd33SYoulin Pei }
2599dbbbd33SYoulin Pei 
mtk_cirq_resume(void)2609dbbbd33SYoulin Pei static void mtk_cirq_resume(void)
2619dbbbd33SYoulin Pei {
26245ac0195SAngeloGioacchino Del Regno 	void __iomem *reg = mtk_cirq_reg(cirq_data, CIRQ_CONTROL);
2639dbbbd33SYoulin Pei 	u32 value;
2649dbbbd33SYoulin Pei 
265a359f757SIngo Molnar 	/* flush recorded interrupts, will send signals to parent controller */
26645ac0195SAngeloGioacchino Del Regno 	value = readl_relaxed(reg);
26745ac0195SAngeloGioacchino Del Regno 	writel_relaxed(value | CIRQ_FLUSH, reg);
2689dbbbd33SYoulin Pei 
2699dbbbd33SYoulin Pei 	/* disable cirq */
27045ac0195SAngeloGioacchino Del Regno 	value = readl_relaxed(reg);
2719dbbbd33SYoulin Pei 	value &= ~(CIRQ_EDGE | CIRQ_EN);
27245ac0195SAngeloGioacchino Del Regno 	writel_relaxed(value, reg);
2739dbbbd33SYoulin Pei }
2749dbbbd33SYoulin Pei 
2759dbbbd33SYoulin Pei static struct syscore_ops mtk_cirq_syscore_ops = {
2769dbbbd33SYoulin Pei 	.suspend	= mtk_cirq_suspend,
2779dbbbd33SYoulin Pei 	.resume		= mtk_cirq_resume,
2789dbbbd33SYoulin Pei };
2799dbbbd33SYoulin Pei 
mtk_cirq_syscore_init(void)2809dbbbd33SYoulin Pei static void mtk_cirq_syscore_init(void)
2819dbbbd33SYoulin Pei {
2829dbbbd33SYoulin Pei 	register_syscore_ops(&mtk_cirq_syscore_ops);
2839dbbbd33SYoulin Pei }
2849dbbbd33SYoulin Pei #else
mtk_cirq_syscore_init(void)2859dbbbd33SYoulin Pei static inline void mtk_cirq_syscore_init(void) {}
2869dbbbd33SYoulin Pei #endif
2879dbbbd33SYoulin Pei 
28845ac0195SAngeloGioacchino Del Regno static const struct of_device_id mtk_cirq_of_match[] = {
28945ac0195SAngeloGioacchino Del Regno 	{ .compatible = "mediatek,mt2701-cirq", .data = &mtk_cirq_regoffs_v1 },
29045ac0195SAngeloGioacchino Del Regno 	{ .compatible = "mediatek,mt8135-cirq", .data = &mtk_cirq_regoffs_v1 },
29145ac0195SAngeloGioacchino Del Regno 	{ .compatible = "mediatek,mt8173-cirq", .data = &mtk_cirq_regoffs_v1 },
292*5c4e0aacSAngeloGioacchino Del Regno 	{ .compatible = "mediatek,mt8192-cirq", .data = &mtk_cirq_regoffs_v2 },
29345ac0195SAngeloGioacchino Del Regno 	{ /* sentinel */ }
29445ac0195SAngeloGioacchino Del Regno };
29545ac0195SAngeloGioacchino Del Regno 
mtk_cirq_of_init(struct device_node * node,struct device_node * parent)2969dbbbd33SYoulin Pei static int __init mtk_cirq_of_init(struct device_node *node,
2979dbbbd33SYoulin Pei 				   struct device_node *parent)
2989dbbbd33SYoulin Pei {
2999dbbbd33SYoulin Pei 	struct irq_domain *domain, *domain_parent;
30045ac0195SAngeloGioacchino Del Regno 	const struct of_device_id *match;
3019dbbbd33SYoulin Pei 	unsigned int irq_num;
3029dbbbd33SYoulin Pei 	int ret;
3039dbbbd33SYoulin Pei 
3049dbbbd33SYoulin Pei 	domain_parent = irq_find_host(parent);
3059dbbbd33SYoulin Pei 	if (!domain_parent) {
3069dbbbd33SYoulin Pei 		pr_err("mtk_cirq: interrupt-parent not found\n");
3079dbbbd33SYoulin Pei 		return -EINVAL;
3089dbbbd33SYoulin Pei 	}
3099dbbbd33SYoulin Pei 
3109dbbbd33SYoulin Pei 	cirq_data = kzalloc(sizeof(*cirq_data), GFP_KERNEL);
3119dbbbd33SYoulin Pei 	if (!cirq_data)
3129dbbbd33SYoulin Pei 		return -ENOMEM;
3139dbbbd33SYoulin Pei 
3149dbbbd33SYoulin Pei 	cirq_data->base = of_iomap(node, 0);
3159dbbbd33SYoulin Pei 	if (!cirq_data->base) {
3169dbbbd33SYoulin Pei 		pr_err("mtk_cirq: unable to map cirq register\n");
3179dbbbd33SYoulin Pei 		ret = -ENXIO;
3189dbbbd33SYoulin Pei 		goto out_free;
3199dbbbd33SYoulin Pei 	}
3209dbbbd33SYoulin Pei 
3219dbbbd33SYoulin Pei 	ret = of_property_read_u32_index(node, "mediatek,ext-irq-range", 0,
3229dbbbd33SYoulin Pei 					 &cirq_data->ext_irq_start);
3239dbbbd33SYoulin Pei 	if (ret)
3249dbbbd33SYoulin Pei 		goto out_unmap;
3259dbbbd33SYoulin Pei 
3269dbbbd33SYoulin Pei 	ret = of_property_read_u32_index(node, "mediatek,ext-irq-range", 1,
3279dbbbd33SYoulin Pei 					 &cirq_data->ext_irq_end);
3289dbbbd33SYoulin Pei 	if (ret)
3299dbbbd33SYoulin Pei 		goto out_unmap;
3309dbbbd33SYoulin Pei 
33145ac0195SAngeloGioacchino Del Regno 	match = of_match_node(mtk_cirq_of_match, node);
33245ac0195SAngeloGioacchino Del Regno 	if (!match) {
33345ac0195SAngeloGioacchino Del Regno 		ret = -ENODEV;
33445ac0195SAngeloGioacchino Del Regno 		goto out_unmap;
33545ac0195SAngeloGioacchino Del Regno 	}
33645ac0195SAngeloGioacchino Del Regno 	cirq_data->offsets = match->data;
33745ac0195SAngeloGioacchino Del Regno 
3389dbbbd33SYoulin Pei 	irq_num = cirq_data->ext_irq_end - cirq_data->ext_irq_start + 1;
3399dbbbd33SYoulin Pei 	domain = irq_domain_add_hierarchy(domain_parent, 0,
3409dbbbd33SYoulin Pei 					  irq_num, node,
3419dbbbd33SYoulin Pei 					  &cirq_domain_ops, cirq_data);
3429dbbbd33SYoulin Pei 	if (!domain) {
3439dbbbd33SYoulin Pei 		ret = -ENOMEM;
3449dbbbd33SYoulin Pei 		goto out_unmap;
3459dbbbd33SYoulin Pei 	}
3469dbbbd33SYoulin Pei 	cirq_data->domain = domain;
3479dbbbd33SYoulin Pei 
3489dbbbd33SYoulin Pei 	mtk_cirq_syscore_init();
3499dbbbd33SYoulin Pei 
3509dbbbd33SYoulin Pei 	return 0;
3519dbbbd33SYoulin Pei 
3529dbbbd33SYoulin Pei out_unmap:
3539dbbbd33SYoulin Pei 	iounmap(cirq_data->base);
3549dbbbd33SYoulin Pei out_free:
3559dbbbd33SYoulin Pei 	kfree(cirq_data);
3569dbbbd33SYoulin Pei 	return ret;
3579dbbbd33SYoulin Pei }
3589dbbbd33SYoulin Pei 
359a150dac5SMarc Zyngier IRQCHIP_DECLARE(mtk_cirq, "mediatek,mtk-cirq", mtk_cirq_of_init);
360