1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2019 MediaTek Inc.
5 #include <linux/interrupt.h>
7 #include <linux/irqdomain.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
11 #include <linux/suspend.h>
12 #include <linux/mfd/mt6323/core.h>
13 #include <linux/mfd/mt6323/registers.h>
14 #include <linux/mfd/mt6328/core.h>
15 #include <linux/mfd/mt6328/registers.h>
16 #include <linux/mfd/mt6331/core.h>
17 #include <linux/mfd/mt6331/registers.h>
18 #include <linux/mfd/mt6397/core.h>
19 #include <linux/mfd/mt6397/registers.h>
21 static void mt6397_irq_lock(struct irq_data
*data
)
23 struct mt6397_chip
*mt6397
= irq_data_get_irq_chip_data(data
);
25 mutex_lock(&mt6397
->irqlock
);
28 static void mt6397_irq_sync_unlock(struct irq_data
*data
)
30 struct mt6397_chip
*mt6397
= irq_data_get_irq_chip_data(data
);
32 regmap_write(mt6397
->regmap
, mt6397
->int_con
[0],
33 mt6397
->irq_masks_cur
[0]);
34 regmap_write(mt6397
->regmap
, mt6397
->int_con
[1],
35 mt6397
->irq_masks_cur
[1]);
36 if (mt6397
->int_con
[2])
37 regmap_write(mt6397
->regmap
, mt6397
->int_con
[2],
38 mt6397
->irq_masks_cur
[2]);
40 mutex_unlock(&mt6397
->irqlock
);
43 static void mt6397_irq_disable(struct irq_data
*data
)
45 struct mt6397_chip
*mt6397
= irq_data_get_irq_chip_data(data
);
46 int shift
= data
->hwirq
& 0xf;
47 int reg
= data
->hwirq
>> 4;
49 mt6397
->irq_masks_cur
[reg
] &= ~BIT(shift
);
52 static void mt6397_irq_enable(struct irq_data
*data
)
54 struct mt6397_chip
*mt6397
= irq_data_get_irq_chip_data(data
);
55 int shift
= data
->hwirq
& 0xf;
56 int reg
= data
->hwirq
>> 4;
58 mt6397
->irq_masks_cur
[reg
] |= BIT(shift
);
61 static int mt6397_irq_set_wake(struct irq_data
*irq_data
, unsigned int on
)
63 struct mt6397_chip
*mt6397
= irq_data_get_irq_chip_data(irq_data
);
64 int shift
= irq_data
->hwirq
& 0xf;
65 int reg
= irq_data
->hwirq
>> 4;
68 mt6397
->wake_mask
[reg
] |= BIT(shift
);
70 mt6397
->wake_mask
[reg
] &= ~BIT(shift
);
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
= pm_sleep_ptr(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);
113 if (mt6397
->int_status
[2])
114 mt6397_irq_handle_reg(mt6397
, mt6397
->int_status
[2], 32);
119 static int mt6397_irq_domain_map(struct irq_domain
*d
, unsigned int irq
,
122 struct mt6397_chip
*mt6397
= d
->host_data
;
124 irq_set_chip_data(irq
, mt6397
);
125 irq_set_chip_and_handler(irq
, &mt6397_irq_chip
, handle_level_irq
);
126 irq_set_nested_thread(irq
, 1);
127 irq_set_noprobe(irq
);
132 static const struct irq_domain_ops mt6397_irq_domain_ops
= {
133 .map
= mt6397_irq_domain_map
,
136 static int mt6397_irq_pm_notifier(struct notifier_block
*notifier
,
137 unsigned long pm_event
, void *unused
)
139 struct mt6397_chip
*chip
=
140 container_of(notifier
, struct mt6397_chip
, pm_nb
);
143 case PM_SUSPEND_PREPARE
:
144 regmap_write(chip
->regmap
,
145 chip
->int_con
[0], chip
->wake_mask
[0]);
146 regmap_write(chip
->regmap
,
147 chip
->int_con
[1], chip
->wake_mask
[1]);
148 if (chip
->int_con
[2])
149 regmap_write(chip
->regmap
,
150 chip
->int_con
[2], chip
->wake_mask
[2]);
151 enable_irq_wake(chip
->irq
);
154 case PM_POST_SUSPEND
:
155 regmap_write(chip
->regmap
,
156 chip
->int_con
[0], chip
->irq_masks_cur
[0]);
157 regmap_write(chip
->regmap
,
158 chip
->int_con
[1], chip
->irq_masks_cur
[1]);
159 if (chip
->int_con
[2])
160 regmap_write(chip
->regmap
,
161 chip
->int_con
[2], chip
->irq_masks_cur
[2]);
162 disable_irq_wake(chip
->irq
);
172 int mt6397_irq_init(struct mt6397_chip
*chip
)
176 mutex_init(&chip
->irqlock
);
178 switch (chip
->chip_id
) {
180 chip
->int_con
[0] = MT6323_INT_CON0
;
181 chip
->int_con
[1] = MT6323_INT_CON1
;
182 chip
->int_status
[0] = MT6323_INT_STATUS0
;
183 chip
->int_status
[1] = MT6323_INT_STATUS1
;
186 chip
->int_con
[0] = MT6328_INT_CON0
;
187 chip
->int_con
[1] = MT6328_INT_CON1
;
188 chip
->int_con
[2] = MT6328_INT_CON2
;
189 chip
->int_status
[0] = MT6328_INT_STATUS0
;
190 chip
->int_status
[1] = MT6328_INT_STATUS1
;
191 chip
->int_status
[2] = MT6328_INT_STATUS2
;
194 chip
->int_con
[0] = MT6331_INT_CON0
;
195 chip
->int_con
[1] = MT6331_INT_CON1
;
196 chip
->int_status
[0] = MT6331_INT_STATUS_CON0
;
197 chip
->int_status
[1] = MT6331_INT_STATUS_CON1
;
201 chip
->int_con
[0] = MT6397_INT_CON0
;
202 chip
->int_con
[1] = MT6397_INT_CON1
;
203 chip
->int_status
[0] = MT6397_INT_STATUS0
;
204 chip
->int_status
[1] = MT6397_INT_STATUS1
;
208 dev_err(chip
->dev
, "unsupported chip: 0x%x\n", chip
->chip_id
);
212 /* Mask all interrupt sources */
213 regmap_write(chip
->regmap
, chip
->int_con
[0], 0x0);
214 regmap_write(chip
->regmap
, chip
->int_con
[1], 0x0);
215 if (chip
->int_con
[2])
216 regmap_write(chip
->regmap
, chip
->int_con
[2], 0x0);
218 chip
->pm_nb
.notifier_call
= mt6397_irq_pm_notifier
;
219 chip
->irq_domain
= irq_domain_add_linear(chip
->dev
->of_node
,
221 &mt6397_irq_domain_ops
,
223 if (!chip
->irq_domain
) {
224 dev_err(chip
->dev
, "could not create irq domain\n");
228 ret
= devm_request_threaded_irq(chip
->dev
, chip
->irq
, NULL
,
229 mt6397_irq_thread
, IRQF_ONESHOT
,
230 "mt6397-pmic", chip
);
232 dev_err(chip
->dev
, "failed to register irq=%d; err: %d\n",
237 register_pm_notifier(&chip
->pm_nb
);