1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
5 * Author: Lee Jones <lee.jones@linaro.org>
7 * This is a re-write of Christophe Kerello's PMU driver.
10 #include <dt-bindings/interrupt-controller/irq-st.h>
11 #include <linux/err.h>
12 #include <linux/mfd/syscon.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
18 #define STIH407_SYSCFG_5102 0x198
20 #define ST_A9_IRQ_MASK 0x001FFFFF
21 #define ST_A9_IRQ_MAX_CHANS 2
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)
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)
36 struct st_irq_syscfg
{
37 struct regmap
*regmap
;
43 static const struct of_device_id st_irq_syscfg_match
[] = {
45 .compatible
= "st,stih407-irq-syscfg",
46 .data
= (void *)STIH407_SYSCFG_5102
,
51 static int st_irq_xlate(struct platform_device
*pdev
,
52 int device
, int channel
, bool irq
)
54 struct st_irq_syscfg
*ddata
= dev_get_drvdata(&pdev
->dev
);
56 /* Set the device enable bit. */
58 case ST_IRQ_SYSCFG_EXT_0
:
59 ddata
->config
|= ST_A9_IRQ_EN_EXT_0
;
61 case ST_IRQ_SYSCFG_EXT_1
:
62 ddata
->config
|= ST_A9_IRQ_EN_EXT_1
;
64 case ST_IRQ_SYSCFG_EXT_2
:
65 ddata
->config
|= ST_A9_IRQ_EN_EXT_2
;
67 case ST_IRQ_SYSCFG_CTI_0
:
68 ddata
->config
|= ST_A9_IRQ_EN_CTI_0
;
70 case ST_IRQ_SYSCFG_CTI_1
:
71 ddata
->config
|= ST_A9_IRQ_EN_CTI_1
;
73 case ST_IRQ_SYSCFG_PMU_0
:
74 ddata
->config
|= ST_A9_IRQ_EN_PMU_0
;
76 case ST_IRQ_SYSCFG_PMU_1
:
77 ddata
->config
|= ST_A9_IRQ_EN_PMU_1
;
79 case ST_IRQ_SYSCFG_pl310_L2
:
80 ddata
->config
|= ST_A9_IRQ_EN_PL310_L2
;
82 case ST_IRQ_SYSCFG_DISABLED
:
85 dev_err(&pdev
->dev
, "Unrecognised device %d\n", device
);
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
);
97 static int st_irq_syscfg_enable(struct platform_device
*pdev
)
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
;
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");
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");
116 for (i
= 0; i
< ST_A9_IRQ_MAX_CHANS
; i
++) {
117 of_property_read_u32_index(np
, "st,irq-device", i
, &device
);
119 ret
= st_irq_xlate(pdev
, device
, i
, true);
123 of_property_read_u32_index(np
, "st,fiq-device", i
, &device
);
125 ret
= st_irq_xlate(pdev
, device
, i
, false);
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
);
134 return regmap_update_bits(ddata
->regmap
, ddata
->syscfg
,
135 ST_A9_IRQ_MASK
, ddata
->config
);
138 static int st_irq_syscfg_probe(struct platform_device
*pdev
)
140 struct device_node
*np
= pdev
->dev
.of_node
;
141 struct st_irq_syscfg
*ddata
;
143 ddata
= devm_kzalloc(&pdev
->dev
, sizeof(*ddata
), GFP_KERNEL
);
147 ddata
->syscfg
= (unsigned int) device_get_match_data(&pdev
->dev
);
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
);
155 dev_set_drvdata(&pdev
->dev
, ddata
);
157 return st_irq_syscfg_enable(pdev
);
160 static int __maybe_unused
st_irq_syscfg_resume(struct device
*dev
)
162 struct st_irq_syscfg
*ddata
= dev_get_drvdata(dev
);
164 return regmap_update_bits(ddata
->regmap
, ddata
->syscfg
,
165 ST_A9_IRQ_MASK
, ddata
->config
);
168 static SIMPLE_DEV_PM_OPS(st_irq_syscfg_pm_ops
, NULL
, st_irq_syscfg_resume
);
170 static struct platform_driver st_irq_syscfg_driver
= {
172 .name
= "st_irq_syscfg",
173 .pm
= &st_irq_syscfg_pm_ops
,
174 .of_match_table
= st_irq_syscfg_match
,
176 .probe
= st_irq_syscfg_probe
,
179 static int __init
st_irq_syscfg_init(void)
181 return platform_driver_register(&st_irq_syscfg_driver
);
183 core_initcall(st_irq_syscfg_init
);