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/mt6397/core.h>
16 #include <linux/mfd/mt6397/registers.h>
18 static void mt6397_irq_lock(struct irq_data
*data
)
20 struct mt6397_chip
*mt6397
= irq_data_get_irq_chip_data(data
);
22 mutex_lock(&mt6397
->irqlock
);
25 static void mt6397_irq_sync_unlock(struct irq_data
*data
)
27 struct mt6397_chip
*mt6397
= irq_data_get_irq_chip_data(data
);
29 regmap_write(mt6397
->regmap
, mt6397
->int_con
[0],
30 mt6397
->irq_masks_cur
[0]);
31 regmap_write(mt6397
->regmap
, mt6397
->int_con
[1],
32 mt6397
->irq_masks_cur
[1]);
34 mutex_unlock(&mt6397
->irqlock
);
37 static void mt6397_irq_disable(struct irq_data
*data
)
39 struct mt6397_chip
*mt6397
= irq_data_get_irq_chip_data(data
);
40 int shift
= data
->hwirq
& 0xf;
41 int reg
= data
->hwirq
>> 4;
43 mt6397
->irq_masks_cur
[reg
] &= ~BIT(shift
);
46 static void mt6397_irq_enable(struct irq_data
*data
)
48 struct mt6397_chip
*mt6397
= irq_data_get_irq_chip_data(data
);
49 int shift
= data
->hwirq
& 0xf;
50 int reg
= data
->hwirq
>> 4;
52 mt6397
->irq_masks_cur
[reg
] |= BIT(shift
);
55 #ifdef CONFIG_PM_SLEEP
56 static int mt6397_irq_set_wake(struct irq_data
*irq_data
, unsigned int on
)
58 struct mt6397_chip
*mt6397
= irq_data_get_irq_chip_data(irq_data
);
59 int shift
= irq_data
->hwirq
& 0xf;
60 int reg
= irq_data
->hwirq
>> 4;
63 mt6397
->wake_mask
[reg
] |= BIT(shift
);
65 mt6397
->wake_mask
[reg
] &= ~BIT(shift
);
70 #define mt6397_irq_set_wake NULL
73 static struct irq_chip mt6397_irq_chip
= {
75 .irq_bus_lock
= mt6397_irq_lock
,
76 .irq_bus_sync_unlock
= mt6397_irq_sync_unlock
,
77 .irq_enable
= mt6397_irq_enable
,
78 .irq_disable
= mt6397_irq_disable
,
79 .irq_set_wake
= mt6397_irq_set_wake
,
82 static void mt6397_irq_handle_reg(struct mt6397_chip
*mt6397
, int reg
,
85 unsigned int status
= 0;
88 ret
= regmap_read(mt6397
->regmap
, reg
, &status
);
90 dev_err(mt6397
->dev
, "Failed to read irq status: %d\n", ret
);
94 for (i
= 0; i
< 16; i
++) {
95 if (status
& BIT(i
)) {
96 irq
= irq_find_mapping(mt6397
->irq_domain
, irqbase
+ i
);
98 handle_nested_irq(irq
);
102 regmap_write(mt6397
->regmap
, reg
, status
);
105 static irqreturn_t
mt6397_irq_thread(int irq
, void *data
)
107 struct mt6397_chip
*mt6397
= data
;
109 mt6397_irq_handle_reg(mt6397
, mt6397
->int_status
[0], 0);
110 mt6397_irq_handle_reg(mt6397
, mt6397
->int_status
[1], 16);
115 static int mt6397_irq_domain_map(struct irq_domain
*d
, unsigned int irq
,
118 struct mt6397_chip
*mt6397
= d
->host_data
;
120 irq_set_chip_data(irq
, mt6397
);
121 irq_set_chip_and_handler(irq
, &mt6397_irq_chip
, handle_level_irq
);
122 irq_set_nested_thread(irq
, 1);
123 irq_set_noprobe(irq
);
128 static const struct irq_domain_ops mt6397_irq_domain_ops
= {
129 .map
= mt6397_irq_domain_map
,
132 static int mt6397_irq_pm_notifier(struct notifier_block
*notifier
,
133 unsigned long pm_event
, void *unused
)
135 struct mt6397_chip
*chip
=
136 container_of(notifier
, struct mt6397_chip
, pm_nb
);
139 case PM_SUSPEND_PREPARE
:
140 regmap_write(chip
->regmap
,
141 chip
->int_con
[0], chip
->wake_mask
[0]);
142 regmap_write(chip
->regmap
,
143 chip
->int_con
[1], chip
->wake_mask
[1]);
144 enable_irq_wake(chip
->irq
);
147 case PM_POST_SUSPEND
:
148 regmap_write(chip
->regmap
,
149 chip
->int_con
[0], chip
->irq_masks_cur
[0]);
150 regmap_write(chip
->regmap
,
151 chip
->int_con
[1], chip
->irq_masks_cur
[1]);
152 disable_irq_wake(chip
->irq
);
162 int mt6397_irq_init(struct mt6397_chip
*chip
)
166 mutex_init(&chip
->irqlock
);
168 switch (chip
->chip_id
) {
170 chip
->int_con
[0] = MT6323_INT_CON0
;
171 chip
->int_con
[1] = MT6323_INT_CON1
;
172 chip
->int_status
[0] = MT6323_INT_STATUS0
;
173 chip
->int_status
[1] = MT6323_INT_STATUS1
;
178 chip
->int_con
[0] = MT6397_INT_CON0
;
179 chip
->int_con
[1] = MT6397_INT_CON1
;
180 chip
->int_status
[0] = MT6397_INT_STATUS0
;
181 chip
->int_status
[1] = MT6397_INT_STATUS1
;
185 dev_err(chip
->dev
, "unsupported chip: 0x%x\n", chip
->chip_id
);
189 /* Mask all interrupt sources */
190 regmap_write(chip
->regmap
, chip
->int_con
[0], 0x0);
191 regmap_write(chip
->regmap
, chip
->int_con
[1], 0x0);
193 chip
->pm_nb
.notifier_call
= mt6397_irq_pm_notifier
;
194 chip
->irq_domain
= irq_domain_add_linear(chip
->dev
->of_node
,
196 &mt6397_irq_domain_ops
,
198 if (!chip
->irq_domain
) {
199 dev_err(chip
->dev
, "could not create irq domain\n");
203 ret
= devm_request_threaded_irq(chip
->dev
, chip
->irq
, NULL
,
204 mt6397_irq_thread
, IRQF_ONESHOT
,
205 "mt6397-pmic", chip
);
207 dev_err(chip
->dev
, "failed to register irq=%d; err: %d\n",
212 register_pm_notifier(&chip
->pm_nb
);