2 * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
4 * Author: Lee Jones <lee.jones@linaro.org>
6 * This is a re-write of Christophe Kerello's PMU driver.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <dt-bindings/interrupt-controller/irq-st.h>
14 #include <linux/err.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
21 #define STIH415_SYSCFG_642 0x0a8
22 #define STIH416_SYSCFG_7543 0x87c
23 #define STIH407_SYSCFG_5102 0x198
24 #define STID127_SYSCFG_734 0x088
26 #define ST_A9_IRQ_MASK 0x001FFFFF
27 #define ST_A9_IRQ_MAX_CHANS 2
29 #define ST_A9_IRQ_EN_CTI_0 BIT(0)
30 #define ST_A9_IRQ_EN_CTI_1 BIT(1)
31 #define ST_A9_IRQ_EN_PMU_0 BIT(2)
32 #define ST_A9_IRQ_EN_PMU_1 BIT(3)
33 #define ST_A9_IRQ_EN_PL310_L2 BIT(4)
34 #define ST_A9_IRQ_EN_EXT_0 BIT(5)
35 #define ST_A9_IRQ_EN_EXT_1 BIT(6)
36 #define ST_A9_IRQ_EN_EXT_2 BIT(7)
38 #define ST_A9_FIQ_N_SEL(dev, chan) (dev << (8 + (chan * 3)))
39 #define ST_A9_IRQ_N_SEL(dev, chan) (dev << (14 + (chan * 3)))
40 #define ST_A9_EXTIRQ_INV_SEL(dev) (dev << 20)
42 struct st_irq_syscfg
{
43 struct regmap
*regmap
;
49 static const struct of_device_id st_irq_syscfg_match
[] = {
51 .compatible
= "st,stih415-irq-syscfg",
52 .data
= (void *)STIH415_SYSCFG_642
,
55 .compatible
= "st,stih416-irq-syscfg",
56 .data
= (void *)STIH416_SYSCFG_7543
,
59 .compatible
= "st,stih407-irq-syscfg",
60 .data
= (void *)STIH407_SYSCFG_5102
,
63 .compatible
= "st,stid127-irq-syscfg",
64 .data
= (void *)STID127_SYSCFG_734
,
69 static int st_irq_xlate(struct platform_device
*pdev
,
70 int device
, int channel
, bool irq
)
72 struct st_irq_syscfg
*ddata
= dev_get_drvdata(&pdev
->dev
);
74 /* Set the device enable bit. */
76 case ST_IRQ_SYSCFG_EXT_0
:
77 ddata
->config
|= ST_A9_IRQ_EN_EXT_0
;
79 case ST_IRQ_SYSCFG_EXT_1
:
80 ddata
->config
|= ST_A9_IRQ_EN_EXT_1
;
82 case ST_IRQ_SYSCFG_EXT_2
:
83 ddata
->config
|= ST_A9_IRQ_EN_EXT_2
;
85 case ST_IRQ_SYSCFG_CTI_0
:
86 ddata
->config
|= ST_A9_IRQ_EN_CTI_0
;
88 case ST_IRQ_SYSCFG_CTI_1
:
89 ddata
->config
|= ST_A9_IRQ_EN_CTI_1
;
91 case ST_IRQ_SYSCFG_PMU_0
:
92 ddata
->config
|= ST_A9_IRQ_EN_PMU_0
;
94 case ST_IRQ_SYSCFG_PMU_1
:
95 ddata
->config
|= ST_A9_IRQ_EN_PMU_1
;
97 case ST_IRQ_SYSCFG_pl310_L2
:
98 ddata
->config
|= ST_A9_IRQ_EN_PL310_L2
;
100 case ST_IRQ_SYSCFG_DISABLED
:
103 dev_err(&pdev
->dev
, "Unrecognised device %d\n", device
);
107 /* Select IRQ/FIQ channel for device. */
108 ddata
->config
|= irq
?
109 ST_A9_IRQ_N_SEL(device
, channel
) :
110 ST_A9_FIQ_N_SEL(device
, channel
);
115 static int st_irq_syscfg_enable(struct platform_device
*pdev
)
117 struct device_node
*np
= pdev
->dev
.of_node
;
118 struct st_irq_syscfg
*ddata
= dev_get_drvdata(&pdev
->dev
);
119 int channels
, ret
, i
;
122 channels
= of_property_count_u32_elems(np
, "st,irq-device");
123 if (channels
!= ST_A9_IRQ_MAX_CHANS
) {
124 dev_err(&pdev
->dev
, "st,enable-irq-device must have 2 elems\n");
128 channels
= of_property_count_u32_elems(np
, "st,fiq-device");
129 if (channels
!= ST_A9_IRQ_MAX_CHANS
) {
130 dev_err(&pdev
->dev
, "st,enable-fiq-device must have 2 elems\n");
134 for (i
= 0; i
< ST_A9_IRQ_MAX_CHANS
; i
++) {
135 of_property_read_u32_index(np
, "st,irq-device", i
, &device
);
137 ret
= st_irq_xlate(pdev
, device
, i
, true);
141 of_property_read_u32_index(np
, "st,fiq-device", i
, &device
);
143 ret
= st_irq_xlate(pdev
, device
, i
, false);
148 /* External IRQs may be inverted. */
149 of_property_read_u32(np
, "st,invert-ext", &invert
);
150 ddata
->config
|= ST_A9_EXTIRQ_INV_SEL(invert
);
152 return regmap_update_bits(ddata
->regmap
, ddata
->syscfg
,
153 ST_A9_IRQ_MASK
, ddata
->config
);
156 static int st_irq_syscfg_probe(struct platform_device
*pdev
)
158 struct device_node
*np
= pdev
->dev
.of_node
;
159 const struct of_device_id
*match
;
160 struct st_irq_syscfg
*ddata
;
162 ddata
= devm_kzalloc(&pdev
->dev
, sizeof(*ddata
), GFP_KERNEL
);
166 match
= of_match_device(st_irq_syscfg_match
, &pdev
->dev
);
170 ddata
->syscfg
= (unsigned int)match
->data
;
172 ddata
->regmap
= syscon_regmap_lookup_by_phandle(np
, "st,syscfg");
173 if (IS_ERR(ddata
->regmap
)) {
174 dev_err(&pdev
->dev
, "syscfg phandle missing\n");
175 return PTR_ERR(ddata
->regmap
);
178 dev_set_drvdata(&pdev
->dev
, ddata
);
180 return st_irq_syscfg_enable(pdev
);
183 static int st_irq_syscfg_resume(struct device
*dev
)
185 struct st_irq_syscfg
*ddata
= dev_get_drvdata(dev
);
187 return regmap_update_bits(ddata
->regmap
, ddata
->syscfg
,
188 ST_A9_IRQ_MASK
, ddata
->config
);
191 static SIMPLE_DEV_PM_OPS(st_irq_syscfg_pm_ops
, NULL
, st_irq_syscfg_resume
);
193 static struct platform_driver st_irq_syscfg_driver
= {
195 .name
= "st_irq_syscfg",
196 .pm
= &st_irq_syscfg_pm_ops
,
197 .of_match_table
= st_irq_syscfg_match
,
199 .probe
= st_irq_syscfg_probe
,
202 static int __init
st_irq_syscfg_init(void)
204 return platform_driver_register(&st_irq_syscfg_driver
);
206 core_initcall(st_irq_syscfg_init
);