xref: /linux/drivers/pinctrl/mediatek/mtk-eint.c (revision b40b760aa2a9587cdcde62759642b4e99c40dedc)
1e46df235SSean Wang // SPDX-License-Identifier: GPL-2.0
2e46df235SSean Wang // Copyright (c) 2014-2018 MediaTek Inc.
3e46df235SSean Wang 
4e46df235SSean Wang /*
5e46df235SSean Wang  * Library for MediaTek External Interrupt Support
6e46df235SSean Wang  *
7e46df235SSean Wang  * Author: Maoguang Meng <maoguang.meng@mediatek.com>
8e46df235SSean Wang  *	   Sean Wang <sean.wang@mediatek.com>
9e46df235SSean Wang  *
10e46df235SSean Wang  */
11e46df235SSean Wang 
12e46df235SSean Wang #include <linux/delay.h>
13e46df235SSean Wang #include <linux/err.h>
141c5fb66aSLinus Walleij #include <linux/gpio/driver.h>
15e46df235SSean Wang #include <linux/io.h>
16a8cfcf15SArnd Bergmann #include <linux/irqchip/chained_irq.h>
17e46df235SSean Wang #include <linux/irqdomain.h>
188174a851SLight Hsieh #include <linux/module.h>
19e46df235SSean Wang #include <linux/of_irq.h>
20e46df235SSean Wang #include <linux/platform_device.h>
21e46df235SSean Wang 
22e46df235SSean Wang #include "mtk-eint.h"
23e46df235SSean Wang 
24e46df235SSean Wang #define MTK_EINT_EDGE_SENSITIVE           0
25e46df235SSean Wang #define MTK_EINT_LEVEL_SENSITIVE          1
26e46df235SSean Wang #define MTK_EINT_DBNC_SET_DBNC_BITS	  4
27e46df235SSean Wang #define MTK_EINT_DBNC_RST_BIT		  (0x1 << 1)
28e46df235SSean Wang #define MTK_EINT_DBNC_SET_EN		  (0x1 << 0)
29e46df235SSean Wang 
30e46df235SSean Wang static const struct mtk_eint_regs mtk_generic_eint_regs = {
31e46df235SSean Wang 	.stat      = 0x000,
32e46df235SSean Wang 	.ack       = 0x040,
33e46df235SSean Wang 	.mask      = 0x080,
34e46df235SSean Wang 	.mask_set  = 0x0c0,
35e46df235SSean Wang 	.mask_clr  = 0x100,
36e46df235SSean Wang 	.sens      = 0x140,
37e46df235SSean Wang 	.sens_set  = 0x180,
38e46df235SSean Wang 	.sens_clr  = 0x1c0,
39e46df235SSean Wang 	.soft      = 0x200,
40e46df235SSean Wang 	.soft_set  = 0x240,
41e46df235SSean Wang 	.soft_clr  = 0x280,
42e46df235SSean Wang 	.pol       = 0x300,
43e46df235SSean Wang 	.pol_set   = 0x340,
44e46df235SSean Wang 	.pol_clr   = 0x380,
45e46df235SSean Wang 	.dom_en    = 0x400,
46e46df235SSean Wang 	.dbnc_ctrl = 0x500,
47e46df235SSean Wang 	.dbnc_set  = 0x600,
48e46df235SSean Wang 	.dbnc_clr  = 0x700,
49e46df235SSean Wang };
50e46df235SSean Wang 
51e46df235SSean Wang static void __iomem *mtk_eint_get_offset(struct mtk_eint *eint,
52e46df235SSean Wang 					 unsigned int eint_num,
53e46df235SSean Wang 					 unsigned int offset)
54e46df235SSean Wang {
55e46df235SSean Wang 	unsigned int eint_base = 0;
56e46df235SSean Wang 	void __iomem *reg;
57e46df235SSean Wang 
58e46df235SSean Wang 	if (eint_num >= eint->hw->ap_num)
59e46df235SSean Wang 		eint_base = eint->hw->ap_num;
60e46df235SSean Wang 
61e46df235SSean Wang 	reg = eint->base + offset + ((eint_num - eint_base) / 32) * 4;
62e46df235SSean Wang 
63e46df235SSean Wang 	return reg;
64e46df235SSean Wang }
65e46df235SSean Wang 
66e46df235SSean Wang static unsigned int mtk_eint_can_en_debounce(struct mtk_eint *eint,
67e46df235SSean Wang 					     unsigned int eint_num)
68e46df235SSean Wang {
69e46df235SSean Wang 	unsigned int sens;
70e46df235SSean Wang 	unsigned int bit = BIT(eint_num % 32);
71e46df235SSean Wang 	void __iomem *reg = mtk_eint_get_offset(eint, eint_num,
72e46df235SSean Wang 						eint->regs->sens);
73e46df235SSean Wang 
74e46df235SSean Wang 	if (readl(reg) & bit)
75e46df235SSean Wang 		sens = MTK_EINT_LEVEL_SENSITIVE;
76e46df235SSean Wang 	else
77e46df235SSean Wang 		sens = MTK_EINT_EDGE_SENSITIVE;
78e46df235SSean Wang 
79e46df235SSean Wang 	if (eint_num < eint->hw->db_cnt && sens != MTK_EINT_EDGE_SENSITIVE)
80e46df235SSean Wang 		return 1;
81e46df235SSean Wang 	else
82e46df235SSean Wang 		return 0;
83e46df235SSean Wang }
84e46df235SSean Wang 
85e46df235SSean Wang static int mtk_eint_flip_edge(struct mtk_eint *eint, int hwirq)
86e46df235SSean Wang {
87e46df235SSean Wang 	int start_level, curr_level;
88e46df235SSean Wang 	unsigned int reg_offset;
89e46df235SSean Wang 	u32 mask = BIT(hwirq & 0x1f);
90e46df235SSean Wang 	u32 port = (hwirq >> 5) & eint->hw->port_mask;
91e46df235SSean Wang 	void __iomem *reg = eint->base + (port << 2);
92e46df235SSean Wang 
93e46df235SSean Wang 	curr_level = eint->gpio_xlate->get_gpio_state(eint->pctl, hwirq);
94e46df235SSean Wang 
95e46df235SSean Wang 	do {
96e46df235SSean Wang 		start_level = curr_level;
97e46df235SSean Wang 		if (start_level)
98e46df235SSean Wang 			reg_offset = eint->regs->pol_clr;
99e46df235SSean Wang 		else
100e46df235SSean Wang 			reg_offset = eint->regs->pol_set;
101e46df235SSean Wang 		writel(mask, reg + reg_offset);
102e46df235SSean Wang 
103e46df235SSean Wang 		curr_level = eint->gpio_xlate->get_gpio_state(eint->pctl,
104e46df235SSean Wang 							      hwirq);
105e46df235SSean Wang 	} while (start_level != curr_level);
106e46df235SSean Wang 
107e46df235SSean Wang 	return start_level;
108e46df235SSean Wang }
109e46df235SSean Wang 
110e46df235SSean Wang static void mtk_eint_mask(struct irq_data *d)
111e46df235SSean Wang {
112e46df235SSean Wang 	struct mtk_eint *eint = irq_data_get_irq_chip_data(d);
113e46df235SSean Wang 	u32 mask = BIT(d->hwirq & 0x1f);
114e46df235SSean Wang 	void __iomem *reg = mtk_eint_get_offset(eint, d->hwirq,
115e46df235SSean Wang 						eint->regs->mask_set);
116e46df235SSean Wang 
1179d957a95SNicolas Boichat 	eint->cur_mask[d->hwirq >> 5] &= ~mask;
1189d957a95SNicolas Boichat 
119e46df235SSean Wang 	writel(mask, reg);
120e46df235SSean Wang }
121e46df235SSean Wang 
122e46df235SSean Wang static void mtk_eint_unmask(struct irq_data *d)
123e46df235SSean Wang {
124e46df235SSean Wang 	struct mtk_eint *eint = irq_data_get_irq_chip_data(d);
125e46df235SSean Wang 	u32 mask = BIT(d->hwirq & 0x1f);
126e46df235SSean Wang 	void __iomem *reg = mtk_eint_get_offset(eint, d->hwirq,
127e46df235SSean Wang 						eint->regs->mask_clr);
128e46df235SSean Wang 
1299d957a95SNicolas Boichat 	eint->cur_mask[d->hwirq >> 5] |= mask;
1309d957a95SNicolas Boichat 
131e46df235SSean Wang 	writel(mask, reg);
132e46df235SSean Wang 
133e46df235SSean Wang 	if (eint->dual_edge[d->hwirq])
134e46df235SSean Wang 		mtk_eint_flip_edge(eint, d->hwirq);
135e46df235SSean Wang }
136e46df235SSean Wang 
137e46df235SSean Wang static unsigned int mtk_eint_get_mask(struct mtk_eint *eint,
138e46df235SSean Wang 				      unsigned int eint_num)
139e46df235SSean Wang {
140e46df235SSean Wang 	unsigned int bit = BIT(eint_num % 32);
141e46df235SSean Wang 	void __iomem *reg = mtk_eint_get_offset(eint, eint_num,
142e46df235SSean Wang 						eint->regs->mask);
143e46df235SSean Wang 
144e46df235SSean Wang 	return !!(readl(reg) & bit);
145e46df235SSean Wang }
146e46df235SSean Wang 
147e46df235SSean Wang static void mtk_eint_ack(struct irq_data *d)
148e46df235SSean Wang {
149e46df235SSean Wang 	struct mtk_eint *eint = irq_data_get_irq_chip_data(d);
150e46df235SSean Wang 	u32 mask = BIT(d->hwirq & 0x1f);
151e46df235SSean Wang 	void __iomem *reg = mtk_eint_get_offset(eint, d->hwirq,
152e46df235SSean Wang 						eint->regs->ack);
153e46df235SSean Wang 
154e46df235SSean Wang 	writel(mask, reg);
155e46df235SSean Wang }
156e46df235SSean Wang 
157e46df235SSean Wang static int mtk_eint_set_type(struct irq_data *d, unsigned int type)
158e46df235SSean Wang {
159e46df235SSean Wang 	struct mtk_eint *eint = irq_data_get_irq_chip_data(d);
160*b40b760aSHailong Fan 	bool masked;
161e46df235SSean Wang 	u32 mask = BIT(d->hwirq & 0x1f);
162e46df235SSean Wang 	void __iomem *reg;
163e46df235SSean Wang 
164e46df235SSean Wang 	if (((type & IRQ_TYPE_EDGE_BOTH) && (type & IRQ_TYPE_LEVEL_MASK)) ||
165e46df235SSean Wang 	    ((type & IRQ_TYPE_LEVEL_MASK) == IRQ_TYPE_LEVEL_MASK)) {
166e46df235SSean Wang 		dev_err(eint->dev,
167e46df235SSean Wang 			"Can't configure IRQ%d (EINT%lu) for type 0x%X\n",
168e46df235SSean Wang 			d->irq, d->hwirq, type);
169e46df235SSean Wang 		return -EINVAL;
170e46df235SSean Wang 	}
171e46df235SSean Wang 
172e46df235SSean Wang 	if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
173e46df235SSean Wang 		eint->dual_edge[d->hwirq] = 1;
174e46df235SSean Wang 	else
175e46df235SSean Wang 		eint->dual_edge[d->hwirq] = 0;
176e46df235SSean Wang 
177*b40b760aSHailong Fan 	if (!mtk_eint_get_mask(eint, d->hwirq)) {
178*b40b760aSHailong Fan 		mtk_eint_mask(d);
179*b40b760aSHailong Fan 		masked = false;
180*b40b760aSHailong Fan 	} else {
181*b40b760aSHailong Fan 		masked = true;
182*b40b760aSHailong Fan 	}
183*b40b760aSHailong Fan 
184e46df235SSean Wang 	if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING)) {
185e46df235SSean Wang 		reg = mtk_eint_get_offset(eint, d->hwirq, eint->regs->pol_clr);
186e46df235SSean Wang 		writel(mask, reg);
187e46df235SSean Wang 	} else {
188e46df235SSean Wang 		reg = mtk_eint_get_offset(eint, d->hwirq, eint->regs->pol_set);
189e46df235SSean Wang 		writel(mask, reg);
190e46df235SSean Wang 	}
191e46df235SSean Wang 
192e46df235SSean Wang 	if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
193e46df235SSean Wang 		reg = mtk_eint_get_offset(eint, d->hwirq, eint->regs->sens_clr);
194e46df235SSean Wang 		writel(mask, reg);
195e46df235SSean Wang 	} else {
196e46df235SSean Wang 		reg = mtk_eint_get_offset(eint, d->hwirq, eint->regs->sens_set);
197e46df235SSean Wang 		writel(mask, reg);
198e46df235SSean Wang 	}
199e46df235SSean Wang 
200*b40b760aSHailong Fan 	mtk_eint_ack(d);
201*b40b760aSHailong Fan 	if (!masked)
202*b40b760aSHailong Fan 		mtk_eint_unmask(d);
203e46df235SSean Wang 
204e46df235SSean Wang 	return 0;
205e46df235SSean Wang }
206e46df235SSean Wang 
207e46df235SSean Wang static int mtk_eint_irq_set_wake(struct irq_data *d, unsigned int on)
208e46df235SSean Wang {
209e46df235SSean Wang 	struct mtk_eint *eint = irq_data_get_irq_chip_data(d);
210e46df235SSean Wang 	int shift = d->hwirq & 0x1f;
211e46df235SSean Wang 	int reg = d->hwirq >> 5;
212e46df235SSean Wang 
213e46df235SSean Wang 	if (on)
214e46df235SSean Wang 		eint->wake_mask[reg] |= BIT(shift);
215e46df235SSean Wang 	else
216e46df235SSean Wang 		eint->wake_mask[reg] &= ~BIT(shift);
217e46df235SSean Wang 
218e46df235SSean Wang 	return 0;
219e46df235SSean Wang }
220e46df235SSean Wang 
221e46df235SSean Wang static void mtk_eint_chip_write_mask(const struct mtk_eint *eint,
222e46df235SSean Wang 				     void __iomem *base, u32 *buf)
223e46df235SSean Wang {
224e46df235SSean Wang 	int port;
225e46df235SSean Wang 	void __iomem *reg;
226e46df235SSean Wang 
227e46df235SSean Wang 	for (port = 0; port < eint->hw->ports; port++) {
228e46df235SSean Wang 		reg = base + (port << 2);
229e46df235SSean Wang 		writel_relaxed(~buf[port], reg + eint->regs->mask_set);
230e46df235SSean Wang 		writel_relaxed(buf[port], reg + eint->regs->mask_clr);
231e46df235SSean Wang 	}
232e46df235SSean Wang }
233e46df235SSean Wang 
234e46df235SSean Wang static int mtk_eint_irq_request_resources(struct irq_data *d)
235e46df235SSean Wang {
236e46df235SSean Wang 	struct mtk_eint *eint = irq_data_get_irq_chip_data(d);
237e46df235SSean Wang 	struct gpio_chip *gpio_c;
238e46df235SSean Wang 	unsigned int gpio_n;
239e46df235SSean Wang 	int err;
240e46df235SSean Wang 
241e46df235SSean Wang 	err = eint->gpio_xlate->get_gpio_n(eint->pctl, d->hwirq,
242e46df235SSean Wang 					   &gpio_n, &gpio_c);
243e46df235SSean Wang 	if (err < 0) {
244e46df235SSean Wang 		dev_err(eint->dev, "Can not find pin\n");
245e46df235SSean Wang 		return err;
246e46df235SSean Wang 	}
247e46df235SSean Wang 
248e46df235SSean Wang 	err = gpiochip_lock_as_irq(gpio_c, gpio_n);
249e46df235SSean Wang 	if (err < 0) {
250e46df235SSean Wang 		dev_err(eint->dev, "unable to lock HW IRQ %lu for IRQ\n",
251e46df235SSean Wang 			irqd_to_hwirq(d));
252e46df235SSean Wang 		return err;
253e46df235SSean Wang 	}
254e46df235SSean Wang 
255e46df235SSean Wang 	err = eint->gpio_xlate->set_gpio_as_eint(eint->pctl, d->hwirq);
256e46df235SSean Wang 	if (err < 0) {
257e46df235SSean Wang 		dev_err(eint->dev, "Can not eint mode\n");
258e46df235SSean Wang 		return err;
259e46df235SSean Wang 	}
260e46df235SSean Wang 
261e46df235SSean Wang 	return 0;
262e46df235SSean Wang }
263e46df235SSean Wang 
264e46df235SSean Wang static void mtk_eint_irq_release_resources(struct irq_data *d)
265e46df235SSean Wang {
266e46df235SSean Wang 	struct mtk_eint *eint = irq_data_get_irq_chip_data(d);
267e46df235SSean Wang 	struct gpio_chip *gpio_c;
268e46df235SSean Wang 	unsigned int gpio_n;
269e46df235SSean Wang 
270e46df235SSean Wang 	eint->gpio_xlate->get_gpio_n(eint->pctl, d->hwirq, &gpio_n,
271e46df235SSean Wang 				     &gpio_c);
272e46df235SSean Wang 
273e46df235SSean Wang 	gpiochip_unlock_as_irq(gpio_c, gpio_n);
274e46df235SSean Wang }
275e46df235SSean Wang 
276e46df235SSean Wang static struct irq_chip mtk_eint_irq_chip = {
277e46df235SSean Wang 	.name = "mt-eint",
278e46df235SSean Wang 	.irq_disable = mtk_eint_mask,
279e46df235SSean Wang 	.irq_mask = mtk_eint_mask,
280e46df235SSean Wang 	.irq_unmask = mtk_eint_unmask,
281e46df235SSean Wang 	.irq_ack = mtk_eint_ack,
282e46df235SSean Wang 	.irq_set_type = mtk_eint_set_type,
283e46df235SSean Wang 	.irq_set_wake = mtk_eint_irq_set_wake,
284e46df235SSean Wang 	.irq_request_resources = mtk_eint_irq_request_resources,
285e46df235SSean Wang 	.irq_release_resources = mtk_eint_irq_release_resources,
286e46df235SSean Wang };
287e46df235SSean Wang 
288e46df235SSean Wang static unsigned int mtk_eint_hw_init(struct mtk_eint *eint)
289e46df235SSean Wang {
290e46df235SSean Wang 	void __iomem *reg = eint->base + eint->regs->dom_en;
291e46df235SSean Wang 	unsigned int i;
292e46df235SSean Wang 
293e46df235SSean Wang 	for (i = 0; i < eint->hw->ap_num; i += 32) {
294e46df235SSean Wang 		writel(0xffffffff, reg);
295e46df235SSean Wang 		reg += 4;
296e46df235SSean Wang 	}
297e46df235SSean Wang 
298e46df235SSean Wang 	return 0;
299e46df235SSean Wang }
300e46df235SSean Wang 
301e46df235SSean Wang static inline void
302e46df235SSean Wang mtk_eint_debounce_process(struct mtk_eint *eint, int index)
303e46df235SSean Wang {
304e46df235SSean Wang 	unsigned int rst, ctrl_offset;
305e46df235SSean Wang 	unsigned int bit, dbnc;
306e46df235SSean Wang 
307e46df235SSean Wang 	ctrl_offset = (index / 4) * 4 + eint->regs->dbnc_ctrl;
308e46df235SSean Wang 	dbnc = readl(eint->base + ctrl_offset);
309e46df235SSean Wang 	bit = MTK_EINT_DBNC_SET_EN << ((index % 4) * 8);
310e46df235SSean Wang 	if ((bit & dbnc) > 0) {
311e46df235SSean Wang 		ctrl_offset = (index / 4) * 4 + eint->regs->dbnc_set;
312e46df235SSean Wang 		rst = MTK_EINT_DBNC_RST_BIT << ((index % 4) * 8);
313e46df235SSean Wang 		writel(rst, eint->base + ctrl_offset);
314e46df235SSean Wang 	}
315e46df235SSean Wang }
316e46df235SSean Wang 
317e46df235SSean Wang static void mtk_eint_irq_handler(struct irq_desc *desc)
318e46df235SSean Wang {
319e46df235SSean Wang 	struct irq_chip *chip = irq_desc_get_chip(desc);
320e46df235SSean Wang 	struct mtk_eint *eint = irq_desc_get_handler_data(desc);
321e46df235SSean Wang 	unsigned int status, eint_num;
32235594bc7SNicolas Boichat 	int offset, mask_offset, index, virq;
323e46df235SSean Wang 	void __iomem *reg =  mtk_eint_get_offset(eint, 0, eint->regs->stat);
324e46df235SSean Wang 	int dual_edge, start_level, curr_level;
325e46df235SSean Wang 
326e46df235SSean Wang 	chained_irq_enter(chip, desc);
327e46df235SSean Wang 	for (eint_num = 0; eint_num < eint->hw->ap_num; eint_num += 32,
328e46df235SSean Wang 	     reg += 4) {
329e46df235SSean Wang 		status = readl(reg);
330e46df235SSean Wang 		while (status) {
331e46df235SSean Wang 			offset = __ffs(status);
33235594bc7SNicolas Boichat 			mask_offset = eint_num >> 5;
333e46df235SSean Wang 			index = eint_num + offset;
334e46df235SSean Wang 			virq = irq_find_mapping(eint->domain, index);
335e46df235SSean Wang 			status &= ~BIT(offset);
336e46df235SSean Wang 
33735594bc7SNicolas Boichat 			/*
33835594bc7SNicolas Boichat 			 * If we get an interrupt on pin that was only required
33935594bc7SNicolas Boichat 			 * for wake (but no real interrupt requested), mask the
34035594bc7SNicolas Boichat 			 * interrupt (as would mtk_eint_resume do anyway later
34135594bc7SNicolas Boichat 			 * in the resume sequence).
34235594bc7SNicolas Boichat 			 */
34335594bc7SNicolas Boichat 			if (eint->wake_mask[mask_offset] & BIT(offset) &&
34435594bc7SNicolas Boichat 			    !(eint->cur_mask[mask_offset] & BIT(offset))) {
34535594bc7SNicolas Boichat 				writel_relaxed(BIT(offset), reg -
34635594bc7SNicolas Boichat 					eint->regs->stat +
34735594bc7SNicolas Boichat 					eint->regs->mask_set);
34835594bc7SNicolas Boichat 			}
34935594bc7SNicolas Boichat 
350e46df235SSean Wang 			dual_edge = eint->dual_edge[index];
351e46df235SSean Wang 			if (dual_edge) {
352e46df235SSean Wang 				/*
353e46df235SSean Wang 				 * Clear soft-irq in case we raised it last
354e46df235SSean Wang 				 * time.
355e46df235SSean Wang 				 */
356e46df235SSean Wang 				writel(BIT(offset), reg - eint->regs->stat +
357e46df235SSean Wang 				       eint->regs->soft_clr);
358e46df235SSean Wang 
359e46df235SSean Wang 				start_level =
360e46df235SSean Wang 				eint->gpio_xlate->get_gpio_state(eint->pctl,
361e46df235SSean Wang 								 index);
362e46df235SSean Wang 			}
363e46df235SSean Wang 
364e46df235SSean Wang 			generic_handle_irq(virq);
365e46df235SSean Wang 
366e46df235SSean Wang 			if (dual_edge) {
367e46df235SSean Wang 				curr_level = mtk_eint_flip_edge(eint, index);
368e46df235SSean Wang 
369e46df235SSean Wang 				/*
370e46df235SSean Wang 				 * If level changed, we might lost one edge
371e46df235SSean Wang 				 * interrupt, raised it through soft-irq.
372e46df235SSean Wang 				 */
373e46df235SSean Wang 				if (start_level != curr_level)
374e46df235SSean Wang 					writel(BIT(offset), reg -
375e46df235SSean Wang 					       eint->regs->stat +
376e46df235SSean Wang 					       eint->regs->soft_set);
377e46df235SSean Wang 			}
378e46df235SSean Wang 
379e46df235SSean Wang 			if (index < eint->hw->db_cnt)
380e46df235SSean Wang 				mtk_eint_debounce_process(eint, index);
381e46df235SSean Wang 		}
382e46df235SSean Wang 	}
383e46df235SSean Wang 	chained_irq_exit(chip, desc);
384e46df235SSean Wang }
385e46df235SSean Wang 
386e46df235SSean Wang int mtk_eint_do_suspend(struct mtk_eint *eint)
387e46df235SSean Wang {
388e46df235SSean Wang 	mtk_eint_chip_write_mask(eint, eint->base, eint->wake_mask);
389e46df235SSean Wang 
390e46df235SSean Wang 	return 0;
391e46df235SSean Wang }
3928174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_eint_do_suspend);
393e46df235SSean Wang 
394e46df235SSean Wang int mtk_eint_do_resume(struct mtk_eint *eint)
395e46df235SSean Wang {
396e46df235SSean Wang 	mtk_eint_chip_write_mask(eint, eint->base, eint->cur_mask);
397e46df235SSean Wang 
398e46df235SSean Wang 	return 0;
399e46df235SSean Wang }
4008174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_eint_do_resume);
401e46df235SSean Wang 
402e46df235SSean Wang int mtk_eint_set_debounce(struct mtk_eint *eint, unsigned long eint_num,
403e46df235SSean Wang 			  unsigned int debounce)
404e46df235SSean Wang {
405e46df235SSean Wang 	int virq, eint_offset;
406e46df235SSean Wang 	unsigned int set_offset, bit, clr_bit, clr_offset, rst, i, unmask,
407e46df235SSean Wang 		     dbnc;
408e46df235SSean Wang 	static const unsigned int debounce_time[] = {500, 1000, 16000, 32000,
409e46df235SSean Wang 						     64000, 128000, 256000};
410e46df235SSean Wang 	struct irq_data *d;
411e46df235SSean Wang 
412e46df235SSean Wang 	virq = irq_find_mapping(eint->domain, eint_num);
413e46df235SSean Wang 	eint_offset = (eint_num % 4) * 8;
414e46df235SSean Wang 	d = irq_get_irq_data(virq);
415e46df235SSean Wang 
416e46df235SSean Wang 	set_offset = (eint_num / 4) * 4 + eint->regs->dbnc_set;
417e46df235SSean Wang 	clr_offset = (eint_num / 4) * 4 + eint->regs->dbnc_clr;
418e46df235SSean Wang 
419e46df235SSean Wang 	if (!mtk_eint_can_en_debounce(eint, eint_num))
420e46df235SSean Wang 		return -EINVAL;
421e46df235SSean Wang 
422e46df235SSean Wang 	dbnc = ARRAY_SIZE(debounce_time);
423e46df235SSean Wang 	for (i = 0; i < ARRAY_SIZE(debounce_time); i++) {
424e46df235SSean Wang 		if (debounce <= debounce_time[i]) {
425e46df235SSean Wang 			dbnc = i;
426e46df235SSean Wang 			break;
427e46df235SSean Wang 		}
428e46df235SSean Wang 	}
429e46df235SSean Wang 
430e46df235SSean Wang 	if (!mtk_eint_get_mask(eint, eint_num)) {
431e46df235SSean Wang 		mtk_eint_mask(d);
432e46df235SSean Wang 		unmask = 1;
433e46df235SSean Wang 	} else {
434e46df235SSean Wang 		unmask = 0;
435e46df235SSean Wang 	}
436e46df235SSean Wang 
437e46df235SSean Wang 	clr_bit = 0xff << eint_offset;
438e46df235SSean Wang 	writel(clr_bit, eint->base + clr_offset);
439e46df235SSean Wang 
440e46df235SSean Wang 	bit = ((dbnc << MTK_EINT_DBNC_SET_DBNC_BITS) | MTK_EINT_DBNC_SET_EN) <<
441e46df235SSean Wang 		eint_offset;
442e46df235SSean Wang 	rst = MTK_EINT_DBNC_RST_BIT << eint_offset;
443e46df235SSean Wang 	writel(rst | bit, eint->base + set_offset);
444e46df235SSean Wang 
445e46df235SSean Wang 	/*
446e46df235SSean Wang 	 * Delay a while (more than 2T) to wait for hw debounce counter reset
447e46df235SSean Wang 	 * work correctly.
448e46df235SSean Wang 	 */
449e46df235SSean Wang 	udelay(1);
450e46df235SSean Wang 	if (unmask == 1)
451e46df235SSean Wang 		mtk_eint_unmask(d);
452e46df235SSean Wang 
453e46df235SSean Wang 	return 0;
454e46df235SSean Wang }
4558174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_eint_set_debounce);
456e46df235SSean Wang 
457e46df235SSean Wang int mtk_eint_find_irq(struct mtk_eint *eint, unsigned long eint_n)
458e46df235SSean Wang {
459e46df235SSean Wang 	int irq;
460e46df235SSean Wang 
461e46df235SSean Wang 	irq = irq_find_mapping(eint->domain, eint_n);
462e46df235SSean Wang 	if (!irq)
463e46df235SSean Wang 		return -EINVAL;
464e46df235SSean Wang 
465e46df235SSean Wang 	return irq;
466e46df235SSean Wang }
4678174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_eint_find_irq);
468e46df235SSean Wang 
469e46df235SSean Wang int mtk_eint_do_init(struct mtk_eint *eint)
470e46df235SSean Wang {
471e46df235SSean Wang 	int i;
472e46df235SSean Wang 
473e46df235SSean Wang 	/* If clients don't assign a specific regs, let's use generic one */
474e46df235SSean Wang 	if (!eint->regs)
475e46df235SSean Wang 		eint->regs = &mtk_generic_eint_regs;
476e46df235SSean Wang 
477e46df235SSean Wang 	eint->wake_mask = devm_kcalloc(eint->dev, eint->hw->ports,
478e46df235SSean Wang 				       sizeof(*eint->wake_mask), GFP_KERNEL);
479e46df235SSean Wang 	if (!eint->wake_mask)
480e46df235SSean Wang 		return -ENOMEM;
481e46df235SSean Wang 
482e46df235SSean Wang 	eint->cur_mask = devm_kcalloc(eint->dev, eint->hw->ports,
483e46df235SSean Wang 				      sizeof(*eint->cur_mask), GFP_KERNEL);
484e46df235SSean Wang 	if (!eint->cur_mask)
485e46df235SSean Wang 		return -ENOMEM;
486e46df235SSean Wang 
487e46df235SSean Wang 	eint->dual_edge = devm_kcalloc(eint->dev, eint->hw->ap_num,
488e46df235SSean Wang 				       sizeof(int), GFP_KERNEL);
489e46df235SSean Wang 	if (!eint->dual_edge)
490e46df235SSean Wang 		return -ENOMEM;
491e46df235SSean Wang 
492e46df235SSean Wang 	eint->domain = irq_domain_add_linear(eint->dev->of_node,
493e46df235SSean Wang 					     eint->hw->ap_num,
494e46df235SSean Wang 					     &irq_domain_simple_ops, NULL);
495e46df235SSean Wang 	if (!eint->domain)
496e46df235SSean Wang 		return -ENOMEM;
497e46df235SSean Wang 
498e46df235SSean Wang 	mtk_eint_hw_init(eint);
499e46df235SSean Wang 	for (i = 0; i < eint->hw->ap_num; i++) {
500e46df235SSean Wang 		int virq = irq_create_mapping(eint->domain, i);
501e46df235SSean Wang 
502e46df235SSean Wang 		irq_set_chip_and_handler(virq, &mtk_eint_irq_chip,
503e46df235SSean Wang 					 handle_level_irq);
504e46df235SSean Wang 		irq_set_chip_data(virq, eint);
505e46df235SSean Wang 	}
506e46df235SSean Wang 
507e46df235SSean Wang 	irq_set_chained_handler_and_data(eint->irq, mtk_eint_irq_handler,
508e46df235SSean Wang 					 eint);
509e46df235SSean Wang 
510e46df235SSean Wang 	return 0;
511e46df235SSean Wang }
5128174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_eint_do_init);
5138174a851SLight Hsieh 
5148174a851SLight Hsieh MODULE_LICENSE("GPL v2");
5158174a851SLight Hsieh MODULE_DESCRIPTION("MediaTek EINT Driver");
516