xref: /openbmc/linux/drivers/irqchip/irq-st.c (revision fa0dadde)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (C) 2014 STMicroelectronics – All Rights Reserved
4  *
5  *  Author: Lee Jones <lee.jones@linaro.org>
6  *
7  *  This is a re-write of Christophe Kerello's PMU driver.
8  */
9 
10 #include <dt-bindings/interrupt-controller/irq-st.h>
11 #include <linux/err.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 
18 #define STIH407_SYSCFG_5102		0x198
19 
20 #define ST_A9_IRQ_MASK			0x001FFFFF
21 #define ST_A9_IRQ_MAX_CHANS		2
22 
23 #define ST_A9_IRQ_EN_CTI_0		BIT(0)
24 #define ST_A9_IRQ_EN_CTI_1		BIT(1)
25 #define ST_A9_IRQ_EN_PMU_0		BIT(2)
26 #define ST_A9_IRQ_EN_PMU_1		BIT(3)
27 #define ST_A9_IRQ_EN_PL310_L2		BIT(4)
28 #define ST_A9_IRQ_EN_EXT_0		BIT(5)
29 #define ST_A9_IRQ_EN_EXT_1		BIT(6)
30 #define ST_A9_IRQ_EN_EXT_2		BIT(7)
31 
32 #define ST_A9_FIQ_N_SEL(dev, chan)	(dev << (8  + (chan * 3)))
33 #define ST_A9_IRQ_N_SEL(dev, chan)	(dev << (14 + (chan * 3)))
34 #define ST_A9_EXTIRQ_INV_SEL(dev)	(dev << 20)
35 
36 struct st_irq_syscfg {
37 	struct regmap *regmap;
38 	unsigned int syscfg;
39 	unsigned int config;
40 	bool ext_inverted;
41 };
42 
43 static const struct of_device_id st_irq_syscfg_match[] = {
44 	{
45 		.compatible = "st,stih407-irq-syscfg",
46 		.data = (void *)STIH407_SYSCFG_5102,
47 	},
48 	{}
49 };
50 
51 static int st_irq_xlate(struct platform_device *pdev,
52 			int device, int channel, bool irq)
53 {
54 	struct st_irq_syscfg *ddata = dev_get_drvdata(&pdev->dev);
55 
56 	/* Set the device enable bit. */
57 	switch (device) {
58 	case ST_IRQ_SYSCFG_EXT_0:
59 		ddata->config |= ST_A9_IRQ_EN_EXT_0;
60 		break;
61 	case ST_IRQ_SYSCFG_EXT_1:
62 		ddata->config |= ST_A9_IRQ_EN_EXT_1;
63 		break;
64 	case ST_IRQ_SYSCFG_EXT_2:
65 		ddata->config |= ST_A9_IRQ_EN_EXT_2;
66 		break;
67 	case ST_IRQ_SYSCFG_CTI_0:
68 		ddata->config |= ST_A9_IRQ_EN_CTI_0;
69 		break;
70 	case ST_IRQ_SYSCFG_CTI_1:
71 		ddata->config |= ST_A9_IRQ_EN_CTI_1;
72 		break;
73 	case ST_IRQ_SYSCFG_PMU_0:
74 		ddata->config |= ST_A9_IRQ_EN_PMU_0;
75 		break;
76 	case ST_IRQ_SYSCFG_PMU_1:
77 		ddata->config |= ST_A9_IRQ_EN_PMU_1;
78 		break;
79 	case ST_IRQ_SYSCFG_pl310_L2:
80 		ddata->config |= ST_A9_IRQ_EN_PL310_L2;
81 		break;
82 	case ST_IRQ_SYSCFG_DISABLED:
83 		return 0;
84 	default:
85 		dev_err(&pdev->dev, "Unrecognised device %d\n", device);
86 		return -EINVAL;
87 	}
88 
89 	/* Select IRQ/FIQ channel for device. */
90 	ddata->config |= irq ?
91 		ST_A9_IRQ_N_SEL(device, channel) :
92 		ST_A9_FIQ_N_SEL(device, channel);
93 
94 	return 0;
95 }
96 
97 static int st_irq_syscfg_enable(struct platform_device *pdev)
98 {
99 	struct device_node *np = pdev->dev.of_node;
100 	struct st_irq_syscfg *ddata = dev_get_drvdata(&pdev->dev);
101 	int channels, ret, i;
102 	u32 device, invert;
103 
104 	channels = of_property_count_u32_elems(np, "st,irq-device");
105 	if (channels != ST_A9_IRQ_MAX_CHANS) {
106 		dev_err(&pdev->dev, "st,enable-irq-device must have 2 elems\n");
107 		return -EINVAL;
108 	}
109 
110 	channels = of_property_count_u32_elems(np, "st,fiq-device");
111 	if (channels != ST_A9_IRQ_MAX_CHANS) {
112 		dev_err(&pdev->dev, "st,enable-fiq-device must have 2 elems\n");
113 		return -EINVAL;
114 	}
115 
116 	for (i = 0; i < ST_A9_IRQ_MAX_CHANS; i++) {
117 		of_property_read_u32_index(np, "st,irq-device", i, &device);
118 
119 		ret = st_irq_xlate(pdev, device, i, true);
120 		if (ret)
121 			return ret;
122 
123 		of_property_read_u32_index(np, "st,fiq-device", i, &device);
124 
125 		ret = st_irq_xlate(pdev, device, i, false);
126 		if (ret)
127 			return ret;
128 	}
129 
130 	/* External IRQs may be inverted. */
131 	of_property_read_u32(np, "st,invert-ext", &invert);
132 	ddata->config |= ST_A9_EXTIRQ_INV_SEL(invert);
133 
134 	return regmap_update_bits(ddata->regmap, ddata->syscfg,
135 				  ST_A9_IRQ_MASK, ddata->config);
136 }
137 
138 static int st_irq_syscfg_probe(struct platform_device *pdev)
139 {
140 	struct device_node *np = pdev->dev.of_node;
141 	struct st_irq_syscfg *ddata;
142 
143 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
144 	if (!ddata)
145 		return -ENOMEM;
146 
147 	ddata->syscfg = (unsigned int) device_get_match_data(&pdev->dev);
148 
149 	ddata->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
150 	if (IS_ERR(ddata->regmap)) {
151 		dev_err(&pdev->dev, "syscfg phandle missing\n");
152 		return PTR_ERR(ddata->regmap);
153 	}
154 
155 	dev_set_drvdata(&pdev->dev, ddata);
156 
157 	return st_irq_syscfg_enable(pdev);
158 }
159 
160 static int __maybe_unused st_irq_syscfg_resume(struct device *dev)
161 {
162 	struct st_irq_syscfg *ddata = dev_get_drvdata(dev);
163 
164 	return regmap_update_bits(ddata->regmap, ddata->syscfg,
165 				  ST_A9_IRQ_MASK, ddata->config);
166 }
167 
168 static SIMPLE_DEV_PM_OPS(st_irq_syscfg_pm_ops, NULL, st_irq_syscfg_resume);
169 
170 static struct platform_driver st_irq_syscfg_driver = {
171 	.driver = {
172 		.name = "st_irq_syscfg",
173 		.pm = &st_irq_syscfg_pm_ops,
174 		.of_match_table = st_irq_syscfg_match,
175 	},
176 	.probe = st_irq_syscfg_probe,
177 };
178 
179 static int __init st_irq_syscfg_init(void)
180 {
181 	return platform_driver_register(&st_irq_syscfg_driver);
182 }
183 core_initcall(st_irq_syscfg_init);
184