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>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
18 #define STIH415_SYSCFG_642 0x0a8
19 #define STIH416_SYSCFG_7543 0x87c
20 #define STIH407_SYSCFG_5102 0x198
21 #define STID127_SYSCFG_734 0x088
23 #define ST_A9_IRQ_MASK 0x001FFFFF
24 #define ST_A9_IRQ_MAX_CHANS 2
26 #define ST_A9_IRQ_EN_CTI_0 BIT(0)
27 #define ST_A9_IRQ_EN_CTI_1 BIT(1)
28 #define ST_A9_IRQ_EN_PMU_0 BIT(2)
29 #define ST_A9_IRQ_EN_PMU_1 BIT(3)
30 #define ST_A9_IRQ_EN_PL310_L2 BIT(4)
31 #define ST_A9_IRQ_EN_EXT_0 BIT(5)
32 #define ST_A9_IRQ_EN_EXT_1 BIT(6)
33 #define ST_A9_IRQ_EN_EXT_2 BIT(7)
35 #define ST_A9_FIQ_N_SEL(dev, chan) (dev << (8 + (chan * 3)))
36 #define ST_A9_IRQ_N_SEL(dev, chan) (dev << (14 + (chan * 3)))
37 #define ST_A9_EXTIRQ_INV_SEL(dev) (dev << 20)
39 struct st_irq_syscfg
{
40 struct regmap
*regmap
;
46 static const struct of_device_id st_irq_syscfg_match
[] = {
48 .compatible
= "st,stih415-irq-syscfg",
49 .data
= (void *)STIH415_SYSCFG_642
,
52 .compatible
= "st,stih416-irq-syscfg",
53 .data
= (void *)STIH416_SYSCFG_7543
,
56 .compatible
= "st,stih407-irq-syscfg",
57 .data
= (void *)STIH407_SYSCFG_5102
,
60 .compatible
= "st,stid127-irq-syscfg",
61 .data
= (void *)STID127_SYSCFG_734
,
66 static int st_irq_xlate(struct platform_device
*pdev
,
67 int device
, int channel
, bool irq
)
69 struct st_irq_syscfg
*ddata
= dev_get_drvdata(&pdev
->dev
);
71 /* Set the device enable bit. */
73 case ST_IRQ_SYSCFG_EXT_0
:
74 ddata
->config
|= ST_A9_IRQ_EN_EXT_0
;
76 case ST_IRQ_SYSCFG_EXT_1
:
77 ddata
->config
|= ST_A9_IRQ_EN_EXT_1
;
79 case ST_IRQ_SYSCFG_EXT_2
:
80 ddata
->config
|= ST_A9_IRQ_EN_EXT_2
;
82 case ST_IRQ_SYSCFG_CTI_0
:
83 ddata
->config
|= ST_A9_IRQ_EN_CTI_0
;
85 case ST_IRQ_SYSCFG_CTI_1
:
86 ddata
->config
|= ST_A9_IRQ_EN_CTI_1
;
88 case ST_IRQ_SYSCFG_PMU_0
:
89 ddata
->config
|= ST_A9_IRQ_EN_PMU_0
;
91 case ST_IRQ_SYSCFG_PMU_1
:
92 ddata
->config
|= ST_A9_IRQ_EN_PMU_1
;
94 case ST_IRQ_SYSCFG_pl310_L2
:
95 ddata
->config
|= ST_A9_IRQ_EN_PL310_L2
;
97 case ST_IRQ_SYSCFG_DISABLED
:
100 dev_err(&pdev
->dev
, "Unrecognised device %d\n", device
);
104 /* Select IRQ/FIQ channel for device. */
105 ddata
->config
|= irq
?
106 ST_A9_IRQ_N_SEL(device
, channel
) :
107 ST_A9_FIQ_N_SEL(device
, channel
);
112 static int st_irq_syscfg_enable(struct platform_device
*pdev
)
114 struct device_node
*np
= pdev
->dev
.of_node
;
115 struct st_irq_syscfg
*ddata
= dev_get_drvdata(&pdev
->dev
);
116 int channels
, ret
, i
;
119 channels
= of_property_count_u32_elems(np
, "st,irq-device");
120 if (channels
!= ST_A9_IRQ_MAX_CHANS
) {
121 dev_err(&pdev
->dev
, "st,enable-irq-device must have 2 elems\n");
125 channels
= of_property_count_u32_elems(np
, "st,fiq-device");
126 if (channels
!= ST_A9_IRQ_MAX_CHANS
) {
127 dev_err(&pdev
->dev
, "st,enable-fiq-device must have 2 elems\n");
131 for (i
= 0; i
< ST_A9_IRQ_MAX_CHANS
; i
++) {
132 of_property_read_u32_index(np
, "st,irq-device", i
, &device
);
134 ret
= st_irq_xlate(pdev
, device
, i
, true);
138 of_property_read_u32_index(np
, "st,fiq-device", i
, &device
);
140 ret
= st_irq_xlate(pdev
, device
, i
, false);
145 /* External IRQs may be inverted. */
146 of_property_read_u32(np
, "st,invert-ext", &invert
);
147 ddata
->config
|= ST_A9_EXTIRQ_INV_SEL(invert
);
149 return regmap_update_bits(ddata
->regmap
, ddata
->syscfg
,
150 ST_A9_IRQ_MASK
, ddata
->config
);
153 static int st_irq_syscfg_probe(struct platform_device
*pdev
)
155 struct device_node
*np
= pdev
->dev
.of_node
;
156 const struct of_device_id
*match
;
157 struct st_irq_syscfg
*ddata
;
159 ddata
= devm_kzalloc(&pdev
->dev
, sizeof(*ddata
), GFP_KERNEL
);
163 match
= of_match_device(st_irq_syscfg_match
, &pdev
->dev
);
167 ddata
->syscfg
= (unsigned int)match
->data
;
169 ddata
->regmap
= syscon_regmap_lookup_by_phandle(np
, "st,syscfg");
170 if (IS_ERR(ddata
->regmap
)) {
171 dev_err(&pdev
->dev
, "syscfg phandle missing\n");
172 return PTR_ERR(ddata
->regmap
);
175 dev_set_drvdata(&pdev
->dev
, ddata
);
177 return st_irq_syscfg_enable(pdev
);
180 static int __maybe_unused
st_irq_syscfg_resume(struct device
*dev
)
182 struct st_irq_syscfg
*ddata
= dev_get_drvdata(dev
);
184 return regmap_update_bits(ddata
->regmap
, ddata
->syscfg
,
185 ST_A9_IRQ_MASK
, ddata
->config
);
188 static SIMPLE_DEV_PM_OPS(st_irq_syscfg_pm_ops
, NULL
, st_irq_syscfg_resume
);
190 static struct platform_driver st_irq_syscfg_driver
= {
192 .name
= "st_irq_syscfg",
193 .pm
= &st_irq_syscfg_pm_ops
,
194 .of_match_table
= st_irq_syscfg_match
,
196 .probe
= st_irq_syscfg_probe
,
199 static int __init
st_irq_syscfg_init(void)
201 return platform_driver_register(&st_irq_syscfg_driver
);
203 core_initcall(st_irq_syscfg_init
);