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/mfd/hi6421-spmi-pmic.h>
14 #include <linux/module.h>
15 #include <linux/of_gpio.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/irqdomain.h>
19 #include <linux/regmap.h>
21 struct hi6421v600_irq {
23 struct irq_domain *domain;
26 struct regmap *regmap;
28 /* Protect IRQ mask changes */
32 enum hi6421v600_irq_list {
51 #define HISI_IRQ_BANK_SIZE 2
54 * IRQ number for the power key button and mask for both UP and DOWN IRQs
56 #define HISI_POWERKEY_IRQ_NUM 0
57 #define HISI_IRQ_POWERKEY_UP_DOWN (BIT(POWERKEY_DOWN) | BIT(POWERKEY_UP))
60 * Registers for IRQ address and IRQ mask bits
62 * Please notice that we need to regmap a larger region, as other
63 * registers are used by the irqs.
64 * See drivers/irq/hi6421-irq.c.
66 #define SOC_PMIC_IRQ_MASK_0_ADDR 0x0202
67 #define SOC_PMIC_IRQ0_ADDR 0x0212
70 * The IRQs are mapped as:
72 * ====================== ============= ============ =====
73 * IRQ MASK REGISTER IRQ REGISTER BIT
74 * ====================== ============= ============ =====
75 * OTMP 0x0202 0x212 bit 0
76 * VBUS_CONNECT 0x0202 0x212 bit 1
77 * VBUS_DISCONNECT 0x0202 0x212 bit 2
78 * ALARMON_R 0x0202 0x212 bit 3
79 * HOLD_6S 0x0202 0x212 bit 4
80 * HOLD_1S 0x0202 0x212 bit 5
81 * POWERKEY_UP 0x0202 0x212 bit 6
82 * POWERKEY_DOWN 0x0202 0x212 bit 7
84 * OCP_SCP_R 0x0203 0x213 bit 0
85 * COUL_R 0x0203 0x213 bit 1
86 * SIM0_HPD_R 0x0203 0x213 bit 2
87 * SIM0_HPD_F 0x0203 0x213 bit 3
88 * SIM1_HPD_R 0x0203 0x213 bit 4
89 * SIM1_HPD_F 0x0203 0x213 bit 5
90 * ====================== ============= ============ =====
92 * Each mask register contains 8 bits. The ancillary macros below
93 * convert a number from 0 to 14 into a register address and a bit mask
95 #define HISI_IRQ_MASK_REG(irq_data) (SOC_PMIC_IRQ_MASK_0_ADDR + \
96 (irqd_to_hwirq(irq_data) / BITS_PER_BYTE))
97 #define HISI_IRQ_MASK_BIT(irq_data) BIT(irqd_to_hwirq(irq_data) & (BITS_PER_BYTE - 1))
98 #define HISI_8BITS_MASK 0xff
100 static irqreturn_t hi6421v600_irq_handler(int irq, void *__priv)
102 struct hi6421v600_irq *priv = __priv;
103 unsigned long pending;
107 for (i = 0; i < HISI_IRQ_BANK_SIZE; i++) {
108 regmap_read(priv->regmap, SOC_PMIC_IRQ0_ADDR + i, &in);
110 /* Mark pending IRQs as handled */
111 regmap_write(priv->regmap, SOC_PMIC_IRQ0_ADDR + i, in);
113 pending = in & HISI_8BITS_MASK;
115 if (i == HISI_POWERKEY_IRQ_NUM &&
116 (pending & HISI_IRQ_POWERKEY_UP_DOWN) == HISI_IRQ_POWERKEY_UP_DOWN) {
118 * If both powerkey down and up IRQs are received,
119 * handle them at the right order
121 generic_handle_irq(priv->irqs[POWERKEY_DOWN]);
122 generic_handle_irq(priv->irqs[POWERKEY_UP]);
123 pending &= ~HISI_IRQ_POWERKEY_UP_DOWN;
129 for_each_set_bit(offset, &pending, BITS_PER_BYTE) {
130 generic_handle_irq(priv->irqs[offset + i * BITS_PER_BYTE]);
137 static void hi6421v600_irq_mask(struct irq_data *d)
139 struct hi6421v600_irq *priv = irq_data_get_irq_chip_data(d);
144 offset = HISI_IRQ_MASK_REG(d);
146 spin_lock_irqsave(&priv->lock, flags);
148 regmap_read(priv->regmap, offset, &data);
149 data |= HISI_IRQ_MASK_BIT(d);
150 regmap_write(priv->regmap, offset, data);
152 spin_unlock_irqrestore(&priv->lock, flags);
155 static void hi6421v600_irq_unmask(struct irq_data *d)
157 struct hi6421v600_irq *priv = irq_data_get_irq_chip_data(d);
161 offset = HISI_IRQ_MASK_REG(d);
163 spin_lock_irqsave(&priv->lock, flags);
165 regmap_read(priv->regmap, offset, &data);
166 data &= ~HISI_IRQ_MASK_BIT(d);
167 regmap_write(priv->regmap, offset, data);
169 spin_unlock_irqrestore(&priv->lock, flags);
172 static struct irq_chip hi6421v600_pmu_irqchip = {
173 .name = "hi6421v600-irq",
174 .irq_mask = hi6421v600_irq_mask,
175 .irq_unmask = hi6421v600_irq_unmask,
176 .irq_disable = hi6421v600_irq_mask,
177 .irq_enable = hi6421v600_irq_unmask,
180 static int hi6421v600_irq_map(struct irq_domain *d, unsigned int virq,
183 struct hi6421v600_irq *priv = d->host_data;
185 irq_set_chip_and_handler_name(virq, &hi6421v600_pmu_irqchip,
186 handle_simple_irq, "hi6421v600");
187 irq_set_chip_data(virq, priv);
188 irq_set_irq_type(virq, IRQ_TYPE_NONE);
193 static const struct irq_domain_ops hi6421v600_domain_ops = {
194 .map = hi6421v600_irq_map,
195 .xlate = irq_domain_xlate_twocell,
198 static void hi6421v600_irq_init(struct hi6421v600_irq *priv)
201 unsigned int pending;
204 for (i = 0; i < HISI_IRQ_BANK_SIZE; i++)
205 regmap_write(priv->regmap, SOC_PMIC_IRQ_MASK_0_ADDR + i,
208 /* Mark all IRQs as handled */
209 for (i = 0; i < HISI_IRQ_BANK_SIZE; i++) {
210 regmap_read(priv->regmap, SOC_PMIC_IRQ0_ADDR + i, &pending);
211 regmap_write(priv->regmap, SOC_PMIC_IRQ0_ADDR + i,
216 static int hi6421v600_irq_probe(struct platform_device *pdev)
218 struct device *pmic_dev = pdev->dev.parent;
219 struct device_node *np = pmic_dev->of_node;
220 struct platform_device *pmic_pdev;
221 struct device *dev = &pdev->dev;
222 struct hi6421v600_irq *priv;
223 struct hi6421_spmi_pmic *pmic;
228 * This driver is meant to be called by hi6421-spmi-core,
229 * which should first set drvdata. If this doesn't happen, hit
230 * a warn on and return.
232 pmic = dev_get_drvdata(pmic_dev);
236 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
241 priv->regmap = pmic->regmap;
243 spin_lock_init(&priv->lock);
245 pmic_pdev = container_of(pmic_dev, struct platform_device, dev);
247 priv->irq = platform_get_irq(pmic_pdev, 0);
249 dev_err(dev, "Error %d when getting IRQs\n", priv->irq);
253 platform_set_drvdata(pdev, priv);
255 hi6421v600_irq_init(priv);
257 priv->irqs = devm_kzalloc(dev, PMIC_IRQ_LIST_MAX * sizeof(int), GFP_KERNEL);
261 priv->domain = irq_domain_add_simple(np, PMIC_IRQ_LIST_MAX, 0,
262 &hi6421v600_domain_ops, priv);
264 dev_err(dev, "Failed to create IRQ domain\n");
268 for (i = 0; i < PMIC_IRQ_LIST_MAX; i++) {
269 virq = irq_create_mapping(priv->domain, i);
271 dev_err(dev, "Failed to map H/W IRQ\n");
274 priv->irqs[i] = virq;
277 ret = devm_request_threaded_irq(dev,
278 priv->irq, hi6421v600_irq_handler,
280 IRQF_TRIGGER_LOW | IRQF_SHARED | IRQF_NO_SUSPEND,
283 dev_err(dev, "Failed to start IRQ handling thread: error %d\n",
291 static const struct platform_device_id hi6421v600_irq_table[] = {
292 { .name = "hi6421v600-irq" },
295 MODULE_DEVICE_TABLE(platform, hi6421v600_irq_table);
297 static struct platform_driver hi6421v600_irq_driver = {
298 .id_table = hi6421v600_irq_table,
300 .name = "hi6421v600-irq",
302 .probe = hi6421v600_irq_probe,
304 module_platform_driver(hi6421v600_irq_driver);
306 MODULE_DESCRIPTION("HiSilicon Hi6421v600 IRQ driver");
307 MODULE_LICENSE("GPL v2");