1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2020 MediaTek Inc.
5 #include <linux/interrupt.h>
6 #include <linux/mfd/mt6358/core.h>
7 #include <linux/mfd/mt6358/registers.h>
8 #include <linux/mfd/mt6359/core.h>
9 #include <linux/mfd/mt6359/registers.h>
10 #include <linux/mfd/mt6397/core.h>
11 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/of_irq.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
18 #define MTK_PMIC_REG_WIDTH 16
20 static const struct irq_top_t mt6358_ints[] = {
31 static const struct irq_top_t mt6359_ints[] = {
42 static struct pmic_irq_data mt6358_irqd = {
43 .num_top = ARRAY_SIZE(mt6358_ints),
44 .num_pmic_irqs = MT6358_IRQ_NR,
45 .top_int_status_reg = MT6358_TOP_INT_STATUS0,
46 .pmic_ints = mt6358_ints,
49 static struct pmic_irq_data mt6359_irqd = {
50 .num_top = ARRAY_SIZE(mt6359_ints),
51 .num_pmic_irqs = MT6359_IRQ_NR,
52 .top_int_status_reg = MT6359_TOP_INT_STATUS0,
53 .pmic_ints = mt6359_ints,
56 static void pmic_irq_enable(struct irq_data *data)
58 unsigned int hwirq = irqd_to_hwirq(data);
59 struct mt6397_chip *chip = irq_data_get_irq_chip_data(data);
60 struct pmic_irq_data *irqd = chip->irq_data;
62 irqd->enable_hwirq[hwirq] = true;
65 static void pmic_irq_disable(struct irq_data *data)
67 unsigned int hwirq = irqd_to_hwirq(data);
68 struct mt6397_chip *chip = irq_data_get_irq_chip_data(data);
69 struct pmic_irq_data *irqd = chip->irq_data;
71 irqd->enable_hwirq[hwirq] = false;
74 static void pmic_irq_lock(struct irq_data *data)
76 struct mt6397_chip *chip = irq_data_get_irq_chip_data(data);
78 mutex_lock(&chip->irqlock);
81 static void pmic_irq_sync_unlock(struct irq_data *data)
83 unsigned int i, top_gp, gp_offset, en_reg, int_regs, shift;
84 struct mt6397_chip *chip = irq_data_get_irq_chip_data(data);
85 struct pmic_irq_data *irqd = chip->irq_data;
87 for (i = 0; i < irqd->num_pmic_irqs; i++) {
88 if (irqd->enable_hwirq[i] == irqd->cache_hwirq[i])
91 /* Find out the IRQ group */
93 while ((top_gp + 1) < irqd->num_top &&
94 i >= irqd->pmic_ints[top_gp + 1].hwirq_base)
97 /* Find the IRQ registers */
98 gp_offset = i - irqd->pmic_ints[top_gp].hwirq_base;
99 int_regs = gp_offset / MTK_PMIC_REG_WIDTH;
100 shift = gp_offset % MTK_PMIC_REG_WIDTH;
101 en_reg = irqd->pmic_ints[top_gp].en_reg +
102 (irqd->pmic_ints[top_gp].en_reg_shift * int_regs);
104 regmap_update_bits(chip->regmap, en_reg, BIT(shift),
105 irqd->enable_hwirq[i] << shift);
107 irqd->cache_hwirq[i] = irqd->enable_hwirq[i];
109 mutex_unlock(&chip->irqlock);
112 static struct irq_chip mt6358_irq_chip = {
113 .name = "mt6358-irq",
114 .flags = IRQCHIP_SKIP_SET_WAKE,
115 .irq_enable = pmic_irq_enable,
116 .irq_disable = pmic_irq_disable,
117 .irq_bus_lock = pmic_irq_lock,
118 .irq_bus_sync_unlock = pmic_irq_sync_unlock,
121 static void mt6358_irq_sp_handler(struct mt6397_chip *chip,
124 unsigned int irq_status, sta_reg, status;
125 unsigned int hwirq, virq;
127 struct pmic_irq_data *irqd = chip->irq_data;
129 for (i = 0; i < irqd->pmic_ints[top_gp].num_int_regs; i++) {
130 sta_reg = irqd->pmic_ints[top_gp].sta_reg +
131 irqd->pmic_ints[top_gp].sta_reg_shift * i;
133 ret = regmap_read(chip->regmap, sta_reg, &irq_status);
136 "Failed to read IRQ status, ret=%d\n", ret);
147 hwirq = irqd->pmic_ints[top_gp].hwirq_base +
148 MTK_PMIC_REG_WIDTH * i + j;
150 virq = irq_find_mapping(chip->irq_domain, hwirq);
152 handle_nested_irq(virq);
157 regmap_write(chip->regmap, sta_reg, irq_status);
161 static irqreturn_t mt6358_irq_handler(int irq, void *data)
163 struct mt6397_chip *chip = data;
164 struct pmic_irq_data *irqd = chip->irq_data;
165 unsigned int bit, i, top_irq_status = 0;
168 ret = regmap_read(chip->regmap,
169 irqd->top_int_status_reg,
173 "Failed to read status from the device, ret=%d\n", ret);
177 for (i = 0; i < irqd->num_top; i++) {
178 bit = BIT(irqd->pmic_ints[i].top_offset);
179 if (top_irq_status & bit) {
180 mt6358_irq_sp_handler(chip, i);
181 top_irq_status &= ~bit;
190 static int pmic_irq_domain_map(struct irq_domain *d, unsigned int irq,
193 struct mt6397_chip *mt6397 = d->host_data;
195 irq_set_chip_data(irq, mt6397);
196 irq_set_chip_and_handler(irq, &mt6358_irq_chip, handle_level_irq);
197 irq_set_nested_thread(irq, 1);
198 irq_set_noprobe(irq);
203 static const struct irq_domain_ops mt6358_irq_domain_ops = {
204 .map = pmic_irq_domain_map,
205 .xlate = irq_domain_xlate_twocell,
208 int mt6358_irq_init(struct mt6397_chip *chip)
211 struct pmic_irq_data *irqd;
213 switch (chip->chip_id) {
215 chip->irq_data = &mt6358_irqd;
219 chip->irq_data = &mt6359_irqd;
223 dev_err(chip->dev, "unsupported chip: 0x%x\n", chip->chip_id);
227 mutex_init(&chip->irqlock);
228 irqd = chip->irq_data;
229 irqd->enable_hwirq = devm_kcalloc(chip->dev,
231 sizeof(*irqd->enable_hwirq),
233 if (!irqd->enable_hwirq)
236 irqd->cache_hwirq = devm_kcalloc(chip->dev,
238 sizeof(*irqd->cache_hwirq),
240 if (!irqd->cache_hwirq)
243 /* Disable all interrupts for initializing */
244 for (i = 0; i < irqd->num_top; i++) {
245 for (j = 0; j < irqd->pmic_ints[i].num_int_regs; j++)
246 regmap_write(chip->regmap,
247 irqd->pmic_ints[i].en_reg +
248 irqd->pmic_ints[i].en_reg_shift * j, 0);
251 chip->irq_domain = irq_domain_add_linear(chip->dev->of_node,
253 &mt6358_irq_domain_ops, chip);
254 if (!chip->irq_domain) {
255 dev_err(chip->dev, "Could not create IRQ domain\n");
259 ret = devm_request_threaded_irq(chip->dev, chip->irq, NULL,
260 mt6358_irq_handler, IRQF_ONESHOT,
261 mt6358_irq_chip.name, chip);
263 dev_err(chip->dev, "Failed to register IRQ=%d, ret=%d\n",
268 enable_irq_wake(chip->irq);