xref: /linux/drivers/mfd/lp8788-irq.c (revision 22aab38e7b59fd79ce1045006be69a9abab58e5a)
1eea6b7ccSMilo Kim /*
2eea6b7ccSMilo Kim  * TI LP8788 MFD - interrupt handler
3eea6b7ccSMilo Kim  *
4eea6b7ccSMilo Kim  * Copyright 2012 Texas Instruments
5eea6b7ccSMilo Kim  *
6eea6b7ccSMilo Kim  * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
7eea6b7ccSMilo Kim  *
8eea6b7ccSMilo Kim  * This program is free software; you can redistribute it and/or modify
9eea6b7ccSMilo Kim  * it under the terms of the GNU General Public License version 2 as
10eea6b7ccSMilo Kim  * published by the Free Software Foundation.
11eea6b7ccSMilo Kim  *
12eea6b7ccSMilo Kim  */
13eea6b7ccSMilo Kim 
14eea6b7ccSMilo Kim #include <linux/delay.h>
15eea6b7ccSMilo Kim #include <linux/err.h>
16eea6b7ccSMilo Kim #include <linux/interrupt.h>
17eea6b7ccSMilo Kim #include <linux/irq.h>
18eea6b7ccSMilo Kim #include <linux/irqdomain.h>
19eea6b7ccSMilo Kim #include <linux/device.h>
20eea6b7ccSMilo Kim #include <linux/mfd/lp8788.h>
21eea6b7ccSMilo Kim #include <linux/module.h>
22eea6b7ccSMilo Kim #include <linux/slab.h>
23eea6b7ccSMilo Kim 
24eea6b7ccSMilo Kim /* register address */
25eea6b7ccSMilo Kim #define LP8788_INT_1			0x00
26eea6b7ccSMilo Kim #define LP8788_INTEN_1			0x03
27eea6b7ccSMilo Kim 
28eea6b7ccSMilo Kim #define BASE_INTEN_ADDR			LP8788_INTEN_1
29eea6b7ccSMilo Kim #define SIZE_REG			8
30eea6b7ccSMilo Kim #define NUM_REGS			3
31eea6b7ccSMilo Kim 
32eea6b7ccSMilo Kim /*
33eea6b7ccSMilo Kim  * struct lp8788_irq_data
34eea6b7ccSMilo Kim  * @lp               : used for accessing to lp8788 registers
35eea6b7ccSMilo Kim  * @irq_lock         : mutex for enabling/disabling the interrupt
36eea6b7ccSMilo Kim  * @domain           : IRQ domain for handling nested interrupt
37eea6b7ccSMilo Kim  * @enabled          : status of enabled interrupt
38eea6b7ccSMilo Kim  */
39eea6b7ccSMilo Kim struct lp8788_irq_data {
40eea6b7ccSMilo Kim 	struct lp8788 *lp;
41eea6b7ccSMilo Kim 	struct mutex irq_lock;
42eea6b7ccSMilo Kim 	struct irq_domain *domain;
43eea6b7ccSMilo Kim 	int enabled[LP8788_INT_MAX];
44eea6b7ccSMilo Kim };
45eea6b7ccSMilo Kim 
46eea6b7ccSMilo Kim static inline u8 _irq_to_addr(enum lp8788_int_id id)
47eea6b7ccSMilo Kim {
48eea6b7ccSMilo Kim 	return id / SIZE_REG;
49eea6b7ccSMilo Kim }
50eea6b7ccSMilo Kim 
51eea6b7ccSMilo Kim static inline u8 _irq_to_enable_addr(enum lp8788_int_id id)
52eea6b7ccSMilo Kim {
53eea6b7ccSMilo Kim 	return _irq_to_addr(id) + BASE_INTEN_ADDR;
54eea6b7ccSMilo Kim }
55eea6b7ccSMilo Kim 
56eea6b7ccSMilo Kim static inline u8 _irq_to_mask(enum lp8788_int_id id)
57eea6b7ccSMilo Kim {
58eea6b7ccSMilo Kim 	return 1 << (id % SIZE_REG);
59eea6b7ccSMilo Kim }
60eea6b7ccSMilo Kim 
61eea6b7ccSMilo Kim static inline u8 _irq_to_val(enum lp8788_int_id id, int enable)
62eea6b7ccSMilo Kim {
63eea6b7ccSMilo Kim 	return enable << (id % SIZE_REG);
64eea6b7ccSMilo Kim }
65eea6b7ccSMilo Kim 
66eea6b7ccSMilo Kim static void lp8788_irq_enable(struct irq_data *data)
67eea6b7ccSMilo Kim {
68eea6b7ccSMilo Kim 	struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data);
6952149ad2SLee Jones 
70eea6b7ccSMilo Kim 	irqd->enabled[data->hwirq] = 1;
71eea6b7ccSMilo Kim }
72eea6b7ccSMilo Kim 
73eea6b7ccSMilo Kim static void lp8788_irq_disable(struct irq_data *data)
74eea6b7ccSMilo Kim {
75eea6b7ccSMilo Kim 	struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data);
7652149ad2SLee Jones 
77eea6b7ccSMilo Kim 	irqd->enabled[data->hwirq] = 0;
78eea6b7ccSMilo Kim }
79eea6b7ccSMilo Kim 
80eea6b7ccSMilo Kim static void lp8788_irq_bus_lock(struct irq_data *data)
81eea6b7ccSMilo Kim {
82eea6b7ccSMilo Kim 	struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data);
83eea6b7ccSMilo Kim 
84eea6b7ccSMilo Kim 	mutex_lock(&irqd->irq_lock);
85eea6b7ccSMilo Kim }
86eea6b7ccSMilo Kim 
87eea6b7ccSMilo Kim static void lp8788_irq_bus_sync_unlock(struct irq_data *data)
88eea6b7ccSMilo Kim {
89eea6b7ccSMilo Kim 	struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data);
90eea6b7ccSMilo Kim 	enum lp8788_int_id irq = data->hwirq;
91eea6b7ccSMilo Kim 	u8 addr, mask, val;
92eea6b7ccSMilo Kim 
93eea6b7ccSMilo Kim 	addr = _irq_to_enable_addr(irq);
94eea6b7ccSMilo Kim 	mask = _irq_to_mask(irq);
95eea6b7ccSMilo Kim 	val = _irq_to_val(irq, irqd->enabled[irq]);
96eea6b7ccSMilo Kim 
97eea6b7ccSMilo Kim 	lp8788_update_bits(irqd->lp, addr, mask, val);
98eea6b7ccSMilo Kim 
99eea6b7ccSMilo Kim 	mutex_unlock(&irqd->irq_lock);
100eea6b7ccSMilo Kim }
101eea6b7ccSMilo Kim 
102eea6b7ccSMilo Kim static struct irq_chip lp8788_irq_chip = {
103eea6b7ccSMilo Kim 	.name			= "lp8788",
104eea6b7ccSMilo Kim 	.irq_enable		= lp8788_irq_enable,
105eea6b7ccSMilo Kim 	.irq_disable		= lp8788_irq_disable,
106eea6b7ccSMilo Kim 	.irq_bus_lock		= lp8788_irq_bus_lock,
107eea6b7ccSMilo Kim 	.irq_bus_sync_unlock	= lp8788_irq_bus_sync_unlock,
108eea6b7ccSMilo Kim };
109eea6b7ccSMilo Kim 
110eea6b7ccSMilo Kim static irqreturn_t lp8788_irq_handler(int irq, void *ptr)
111eea6b7ccSMilo Kim {
112eea6b7ccSMilo Kim 	struct lp8788_irq_data *irqd = ptr;
113eea6b7ccSMilo Kim 	struct lp8788 *lp = irqd->lp;
114eea6b7ccSMilo Kim 	u8 status[NUM_REGS], addr, mask;
115*22aab38eSDan Carpenter 	bool handled = false;
116eea6b7ccSMilo Kim 	int i;
117eea6b7ccSMilo Kim 
118eea6b7ccSMilo Kim 	if (lp8788_read_multi_bytes(lp, LP8788_INT_1, status, NUM_REGS))
119eea6b7ccSMilo Kim 		return IRQ_NONE;
120eea6b7ccSMilo Kim 
121eea6b7ccSMilo Kim 	for (i = 0 ; i < LP8788_INT_MAX ; i++) {
122eea6b7ccSMilo Kim 		addr = _irq_to_addr(i);
123eea6b7ccSMilo Kim 		mask = _irq_to_mask(i);
124eea6b7ccSMilo Kim 
125eea6b7ccSMilo Kim 		/* reporting only if the irq is enabled */
126eea6b7ccSMilo Kim 		if (status[addr] & mask) {
127eea6b7ccSMilo Kim 			handle_nested_irq(irq_find_mapping(irqd->domain, i));
128eea6b7ccSMilo Kim 			handled = true;
129eea6b7ccSMilo Kim 		}
130eea6b7ccSMilo Kim 	}
131eea6b7ccSMilo Kim 
132eea6b7ccSMilo Kim 	return handled ? IRQ_HANDLED : IRQ_NONE;
133eea6b7ccSMilo Kim }
134eea6b7ccSMilo Kim 
135eea6b7ccSMilo Kim static int lp8788_irq_map(struct irq_domain *d, unsigned int virq,
136eea6b7ccSMilo Kim 			irq_hw_number_t hwirq)
137eea6b7ccSMilo Kim {
138eea6b7ccSMilo Kim 	struct lp8788_irq_data *irqd = d->host_data;
139eea6b7ccSMilo Kim 	struct irq_chip *chip = &lp8788_irq_chip;
140eea6b7ccSMilo Kim 
141eea6b7ccSMilo Kim 	irq_set_chip_data(virq, irqd);
142eea6b7ccSMilo Kim 	irq_set_chip_and_handler(virq, chip, handle_edge_irq);
143eea6b7ccSMilo Kim 	irq_set_nested_thread(virq, 1);
144eea6b7ccSMilo Kim 	irq_set_noprobe(virq);
145eea6b7ccSMilo Kim 
146eea6b7ccSMilo Kim 	return 0;
147eea6b7ccSMilo Kim }
148eea6b7ccSMilo Kim 
1497ce7b26fSKrzysztof Kozlowski static const struct irq_domain_ops lp8788_domain_ops = {
150eea6b7ccSMilo Kim 	.map = lp8788_irq_map,
151eea6b7ccSMilo Kim };
152eea6b7ccSMilo Kim 
153eea6b7ccSMilo Kim int lp8788_irq_init(struct lp8788 *lp, int irq)
154eea6b7ccSMilo Kim {
155eea6b7ccSMilo Kim 	struct lp8788_irq_data *irqd;
156eea6b7ccSMilo Kim 	int ret;
157eea6b7ccSMilo Kim 
158eea6b7ccSMilo Kim 	if (irq <= 0) {
159eea6b7ccSMilo Kim 		dev_warn(lp->dev, "invalid irq number: %d\n", irq);
160eea6b7ccSMilo Kim 		return 0;
161eea6b7ccSMilo Kim 	}
162eea6b7ccSMilo Kim 
163eea6b7ccSMilo Kim 	irqd = devm_kzalloc(lp->dev, sizeof(*irqd), GFP_KERNEL);
164eea6b7ccSMilo Kim 	if (!irqd)
165eea6b7ccSMilo Kim 		return -ENOMEM;
166eea6b7ccSMilo Kim 
167eea6b7ccSMilo Kim 	irqd->lp = lp;
168eea6b7ccSMilo Kim 	irqd->domain = irq_domain_add_linear(lp->dev->of_node, LP8788_INT_MAX,
169eea6b7ccSMilo Kim 					&lp8788_domain_ops, irqd);
170eea6b7ccSMilo Kim 	if (!irqd->domain) {
171eea6b7ccSMilo Kim 		dev_err(lp->dev, "failed to add irq domain err\n");
172eea6b7ccSMilo Kim 		return -EINVAL;
173eea6b7ccSMilo Kim 	}
174eea6b7ccSMilo Kim 
175eea6b7ccSMilo Kim 	lp->irqdm = irqd->domain;
176eea6b7ccSMilo Kim 	mutex_init(&irqd->irq_lock);
177eea6b7ccSMilo Kim 
178eea6b7ccSMilo Kim 	ret = request_threaded_irq(irq, NULL, lp8788_irq_handler,
179eea6b7ccSMilo Kim 				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
180eea6b7ccSMilo Kim 				"lp8788-irq", irqd);
181eea6b7ccSMilo Kim 	if (ret) {
182eea6b7ccSMilo Kim 		dev_err(lp->dev, "failed to create a thread for IRQ_N\n");
183eea6b7ccSMilo Kim 		return ret;
184eea6b7ccSMilo Kim 	}
185eea6b7ccSMilo Kim 
186eea6b7ccSMilo Kim 	lp->irq = irq;
187eea6b7ccSMilo Kim 
188eea6b7ccSMilo Kim 	return 0;
189eea6b7ccSMilo Kim }
190eea6b7ccSMilo Kim 
191eea6b7ccSMilo Kim void lp8788_irq_exit(struct lp8788 *lp)
192eea6b7ccSMilo Kim {
193eea6b7ccSMilo Kim 	if (lp->irq)
194eea6b7ccSMilo Kim 		free_irq(lp->irq, lp->irqdm);
195eea6b7ccSMilo Kim }
196