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