1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2019 MediaTek Inc.
5 #include <linux/interrupt.h>
6 #include <linux/module.h>
8 #include <linux/of_device.h>
9 #include <linux/of_irq.h>
10 #include <linux/platform_device.h>
11 #include <linux/regmap.h>
12 #include <linux/suspend.h>
13 #include <linux/mfd/mt6323/core.h>
14 #include <linux/mfd/mt6323/registers.h>
15 #include <linux/mfd/mt6331/core.h>
16 #include <linux/mfd/mt6331/registers.h>
17 #include <linux/mfd/mt6397/core.h>
18 #include <linux/mfd/mt6397/registers.h>
20 static void mt6397_irq_lock(struct irq_data *data)
22 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
24 mutex_lock(&mt6397->irqlock);
27 static void mt6397_irq_sync_unlock(struct irq_data *data)
29 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
31 regmap_write(mt6397->regmap, mt6397->int_con[0],
32 mt6397->irq_masks_cur[0]);
33 regmap_write(mt6397->regmap, mt6397->int_con[1],
34 mt6397->irq_masks_cur[1]);
36 mutex_unlock(&mt6397->irqlock);
39 static void mt6397_irq_disable(struct irq_data *data)
41 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
42 int shift = data->hwirq & 0xf;
43 int reg = data->hwirq >> 4;
45 mt6397->irq_masks_cur[reg] &= ~BIT(shift);
48 static void mt6397_irq_enable(struct irq_data *data)
50 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
51 int shift = data->hwirq & 0xf;
52 int reg = data->hwirq >> 4;
54 mt6397->irq_masks_cur[reg] |= BIT(shift);
57 #ifdef CONFIG_PM_SLEEP
58 static int mt6397_irq_set_wake(struct irq_data *irq_data, unsigned int on)
60 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(irq_data);
61 int shift = irq_data->hwirq & 0xf;
62 int reg = irq_data->hwirq >> 4;
65 mt6397->wake_mask[reg] |= BIT(shift);
67 mt6397->wake_mask[reg] &= ~BIT(shift);
72 #define mt6397_irq_set_wake NULL
75 static struct irq_chip mt6397_irq_chip = {
77 .irq_bus_lock = mt6397_irq_lock,
78 .irq_bus_sync_unlock = mt6397_irq_sync_unlock,
79 .irq_enable = mt6397_irq_enable,
80 .irq_disable = mt6397_irq_disable,
81 .irq_set_wake = mt6397_irq_set_wake,
84 static void mt6397_irq_handle_reg(struct mt6397_chip *mt6397, int reg,
87 unsigned int status = 0;
90 ret = regmap_read(mt6397->regmap, reg, &status);
92 dev_err(mt6397->dev, "Failed to read irq status: %d\n", ret);
96 for (i = 0; i < 16; i++) {
97 if (status & BIT(i)) {
98 irq = irq_find_mapping(mt6397->irq_domain, irqbase + i);
100 handle_nested_irq(irq);
104 regmap_write(mt6397->regmap, reg, status);
107 static irqreturn_t mt6397_irq_thread(int irq, void *data)
109 struct mt6397_chip *mt6397 = data;
111 mt6397_irq_handle_reg(mt6397, mt6397->int_status[0], 0);
112 mt6397_irq_handle_reg(mt6397, mt6397->int_status[1], 16);
117 static int mt6397_irq_domain_map(struct irq_domain *d, unsigned int irq,
120 struct mt6397_chip *mt6397 = d->host_data;
122 irq_set_chip_data(irq, mt6397);
123 irq_set_chip_and_handler(irq, &mt6397_irq_chip, handle_level_irq);
124 irq_set_nested_thread(irq, 1);
125 irq_set_noprobe(irq);
130 static const struct irq_domain_ops mt6397_irq_domain_ops = {
131 .map = mt6397_irq_domain_map,
134 static int mt6397_irq_pm_notifier(struct notifier_block *notifier,
135 unsigned long pm_event, void *unused)
137 struct mt6397_chip *chip =
138 container_of(notifier, struct mt6397_chip, pm_nb);
141 case PM_SUSPEND_PREPARE:
142 regmap_write(chip->regmap,
143 chip->int_con[0], chip->wake_mask[0]);
144 regmap_write(chip->regmap,
145 chip->int_con[1], chip->wake_mask[1]);
146 enable_irq_wake(chip->irq);
149 case PM_POST_SUSPEND:
150 regmap_write(chip->regmap,
151 chip->int_con[0], chip->irq_masks_cur[0]);
152 regmap_write(chip->regmap,
153 chip->int_con[1], chip->irq_masks_cur[1]);
154 disable_irq_wake(chip->irq);
164 int mt6397_irq_init(struct mt6397_chip *chip)
168 mutex_init(&chip->irqlock);
170 switch (chip->chip_id) {
172 chip->int_con[0] = MT6323_INT_CON0;
173 chip->int_con[1] = MT6323_INT_CON1;
174 chip->int_status[0] = MT6323_INT_STATUS0;
175 chip->int_status[1] = MT6323_INT_STATUS1;
178 chip->int_con[0] = MT6331_INT_CON0;
179 chip->int_con[1] = MT6331_INT_CON1;
180 chip->int_status[0] = MT6331_INT_STATUS_CON0;
181 chip->int_status[1] = MT6331_INT_STATUS_CON1;
185 chip->int_con[0] = MT6397_INT_CON0;
186 chip->int_con[1] = MT6397_INT_CON1;
187 chip->int_status[0] = MT6397_INT_STATUS0;
188 chip->int_status[1] = MT6397_INT_STATUS1;
192 dev_err(chip->dev, "unsupported chip: 0x%x\n", chip->chip_id);
196 /* Mask all interrupt sources */
197 regmap_write(chip->regmap, chip->int_con[0], 0x0);
198 regmap_write(chip->regmap, chip->int_con[1], 0x0);
200 chip->pm_nb.notifier_call = mt6397_irq_pm_notifier;
201 chip->irq_domain = irq_domain_add_linear(chip->dev->of_node,
203 &mt6397_irq_domain_ops,
205 if (!chip->irq_domain) {
206 dev_err(chip->dev, "could not create irq domain\n");
210 ret = devm_request_threaded_irq(chip->dev, chip->irq, NULL,
211 mt6397_irq_thread, IRQF_ONESHOT,
212 "mt6397-pmic", chip);
214 dev_err(chip->dev, "failed to register irq=%d; err: %d\n",
219 register_pm_notifier(&chip->pm_nb);