1 // SPDX-License-Identifier: GPL-2.0
3 * Device driver for irqs in HISI PMIC IC
5 * Copyright (c) 2013 Linaro Ltd.
6 * Copyright (c) 2011 Hisilicon.
7 * Copyright (c) 2020-2021 Huawei Technologies Co., Ltd.
10 #include <linux/bitops.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/irqdomain.h>
17 #include <linux/regmap.h>
19 struct hi6421v600_irq
{
21 struct irq_domain
*domain
;
24 struct regmap
*regmap
;
26 /* Protect IRQ mask changes */
30 enum hi6421v600_irq_list
{
49 #define HISI_IRQ_BANK_SIZE 2
52 * IRQ number for the power key button and mask for both UP and DOWN IRQs
54 #define HISI_POWERKEY_IRQ_NUM 0
55 #define HISI_IRQ_POWERKEY_UP_DOWN (BIT(POWERKEY_DOWN) | BIT(POWERKEY_UP))
58 * Registers for IRQ address and IRQ mask bits
60 * Please notice that we need to regmap a larger region, as other
61 * registers are used by the irqs.
62 * See drivers/irq/hi6421-irq.c.
64 #define SOC_PMIC_IRQ_MASK_0_ADDR 0x0202
65 #define SOC_PMIC_IRQ0_ADDR 0x0212
68 * The IRQs are mapped as:
70 * ====================== ============= ============ =====
71 * IRQ MASK REGISTER IRQ REGISTER BIT
72 * ====================== ============= ============ =====
73 * OTMP 0x0202 0x212 bit 0
74 * VBUS_CONNECT 0x0202 0x212 bit 1
75 * VBUS_DISCONNECT 0x0202 0x212 bit 2
76 * ALARMON_R 0x0202 0x212 bit 3
77 * HOLD_6S 0x0202 0x212 bit 4
78 * HOLD_1S 0x0202 0x212 bit 5
79 * POWERKEY_UP 0x0202 0x212 bit 6
80 * POWERKEY_DOWN 0x0202 0x212 bit 7
82 * OCP_SCP_R 0x0203 0x213 bit 0
83 * COUL_R 0x0203 0x213 bit 1
84 * SIM0_HPD_R 0x0203 0x213 bit 2
85 * SIM0_HPD_F 0x0203 0x213 bit 3
86 * SIM1_HPD_R 0x0203 0x213 bit 4
87 * SIM1_HPD_F 0x0203 0x213 bit 5
88 * ====================== ============= ============ =====
90 * Each mask register contains 8 bits. The ancillary macros below
91 * convert a number from 0 to 14 into a register address and a bit mask
93 #define HISI_IRQ_MASK_REG(irq_data) (SOC_PMIC_IRQ_MASK_0_ADDR + \
94 (irqd_to_hwirq(irq_data) / BITS_PER_BYTE))
95 #define HISI_IRQ_MASK_BIT(irq_data) BIT(irqd_to_hwirq(irq_data) & (BITS_PER_BYTE - 1))
96 #define HISI_8BITS_MASK 0xff
98 static irqreturn_t
hi6421v600_irq_handler(int irq
, void *__priv
)
100 struct hi6421v600_irq
*priv
= __priv
;
101 unsigned long pending
;
105 for (i
= 0; i
< HISI_IRQ_BANK_SIZE
; i
++) {
106 regmap_read(priv
->regmap
, SOC_PMIC_IRQ0_ADDR
+ i
, &in
);
108 /* Mark pending IRQs as handled */
109 regmap_write(priv
->regmap
, SOC_PMIC_IRQ0_ADDR
+ i
, in
);
111 pending
= in
& HISI_8BITS_MASK
;
113 if (i
== HISI_POWERKEY_IRQ_NUM
&&
114 (pending
& HISI_IRQ_POWERKEY_UP_DOWN
) == HISI_IRQ_POWERKEY_UP_DOWN
) {
116 * If both powerkey down and up IRQs are received,
117 * handle them at the right order
119 generic_handle_irq_safe(priv
->irqs
[POWERKEY_DOWN
]);
120 generic_handle_irq_safe(priv
->irqs
[POWERKEY_UP
]);
121 pending
&= ~HISI_IRQ_POWERKEY_UP_DOWN
;
127 for_each_set_bit(offset
, &pending
, BITS_PER_BYTE
) {
128 generic_handle_irq_safe(priv
->irqs
[offset
+ i
* BITS_PER_BYTE
]);
135 static void hi6421v600_irq_mask(struct irq_data
*d
)
137 struct hi6421v600_irq
*priv
= irq_data_get_irq_chip_data(d
);
142 offset
= HISI_IRQ_MASK_REG(d
);
144 spin_lock_irqsave(&priv
->lock
, flags
);
146 regmap_read(priv
->regmap
, offset
, &data
);
147 data
|= HISI_IRQ_MASK_BIT(d
);
148 regmap_write(priv
->regmap
, offset
, data
);
150 spin_unlock_irqrestore(&priv
->lock
, flags
);
153 static void hi6421v600_irq_unmask(struct irq_data
*d
)
155 struct hi6421v600_irq
*priv
= irq_data_get_irq_chip_data(d
);
159 offset
= HISI_IRQ_MASK_REG(d
);
161 spin_lock_irqsave(&priv
->lock
, flags
);
163 regmap_read(priv
->regmap
, offset
, &data
);
164 data
&= ~HISI_IRQ_MASK_BIT(d
);
165 regmap_write(priv
->regmap
, offset
, data
);
167 spin_unlock_irqrestore(&priv
->lock
, flags
);
170 static struct irq_chip hi6421v600_pmu_irqchip
= {
171 .name
= "hi6421v600-irq",
172 .irq_mask
= hi6421v600_irq_mask
,
173 .irq_unmask
= hi6421v600_irq_unmask
,
174 .irq_disable
= hi6421v600_irq_mask
,
175 .irq_enable
= hi6421v600_irq_unmask
,
178 static int hi6421v600_irq_map(struct irq_domain
*d
, unsigned int virq
,
181 struct hi6421v600_irq
*priv
= d
->host_data
;
183 irq_set_chip_and_handler_name(virq
, &hi6421v600_pmu_irqchip
,
184 handle_simple_irq
, "hi6421v600");
185 irq_set_chip_data(virq
, priv
);
186 irq_set_irq_type(virq
, IRQ_TYPE_NONE
);
191 static const struct irq_domain_ops hi6421v600_domain_ops
= {
192 .map
= hi6421v600_irq_map
,
193 .xlate
= irq_domain_xlate_twocell
,
196 static void hi6421v600_irq_init(struct hi6421v600_irq
*priv
)
199 unsigned int pending
;
202 for (i
= 0; i
< HISI_IRQ_BANK_SIZE
; i
++)
203 regmap_write(priv
->regmap
, SOC_PMIC_IRQ_MASK_0_ADDR
+ i
,
206 /* Mark all IRQs as handled */
207 for (i
= 0; i
< HISI_IRQ_BANK_SIZE
; i
++) {
208 regmap_read(priv
->regmap
, SOC_PMIC_IRQ0_ADDR
+ i
, &pending
);
209 regmap_write(priv
->regmap
, SOC_PMIC_IRQ0_ADDR
+ i
,
214 static int hi6421v600_irq_probe(struct platform_device
*pdev
)
216 struct device
*pmic_dev
= pdev
->dev
.parent
;
217 struct device_node
*np
= pmic_dev
->of_node
;
218 struct platform_device
*pmic_pdev
;
219 struct device
*dev
= &pdev
->dev
;
220 struct hi6421v600_irq
*priv
;
221 struct regmap
*regmap
;
226 * This driver is meant to be called by hi6421-spmi-core,
227 * which should first set drvdata. If this doesn't happen, hit
228 * a warn on and return.
230 regmap
= dev_get_drvdata(pmic_dev
);
231 if (WARN_ON(!regmap
))
234 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
239 priv
->regmap
= regmap
;
241 spin_lock_init(&priv
->lock
);
243 pmic_pdev
= container_of(pmic_dev
, struct platform_device
, dev
);
245 priv
->irq
= platform_get_irq(pmic_pdev
, 0);
249 platform_set_drvdata(pdev
, priv
);
251 hi6421v600_irq_init(priv
);
253 priv
->irqs
= devm_kzalloc(dev
, PMIC_IRQ_LIST_MAX
* sizeof(int), GFP_KERNEL
);
257 priv
->domain
= irq_domain_add_simple(np
, PMIC_IRQ_LIST_MAX
, 0,
258 &hi6421v600_domain_ops
, priv
);
260 dev_err(dev
, "Failed to create IRQ domain\n");
264 for (i
= 0; i
< PMIC_IRQ_LIST_MAX
; i
++) {
265 virq
= irq_create_mapping(priv
->domain
, i
);
267 dev_err(dev
, "Failed to map H/W IRQ\n");
270 priv
->irqs
[i
] = virq
;
273 ret
= devm_request_threaded_irq(dev
,
274 priv
->irq
, hi6421v600_irq_handler
,
276 IRQF_TRIGGER_LOW
| IRQF_SHARED
| IRQF_NO_SUSPEND
,
279 dev_err(dev
, "Failed to start IRQ handling thread: error %d\n",
287 static const struct platform_device_id hi6421v600_irq_table
[] = {
288 { .name
= "hi6421v600-irq" },
291 MODULE_DEVICE_TABLE(platform
, hi6421v600_irq_table
);
293 static struct platform_driver hi6421v600_irq_driver
= {
294 .id_table
= hi6421v600_irq_table
,
296 .name
= "hi6421v600-irq",
298 .probe
= hi6421v600_irq_probe
,
300 module_platform_driver(hi6421v600_irq_driver
);
302 MODULE_DESCRIPTION("HiSilicon Hi6421v600 IRQ driver");
303 MODULE_LICENSE("GPL v2");