2 * Interrupt controller support for TWL6040
4 * Author: Misael Lopez Cruz <misael.lopez@ti.com>
6 * Copyright: (C) 2011 Texas Instruments, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/err.h>
27 #include <linux/irq.h>
29 #include <linux/irqdomain.h>
30 #include <linux/interrupt.h>
31 #include <linux/mfd/core.h>
32 #include <linux/mfd/twl6040.h>
34 struct twl6040_irq_data {
39 static struct twl6040_irq_data twl6040_irqs[] = {
41 .mask = TWL6040_THMSK,
42 .status = TWL6040_THINT,
45 .mask = TWL6040_PLUGMSK,
46 .status = TWL6040_PLUGINT | TWL6040_UNPLUGINT,
49 .mask = TWL6040_HOOKMSK,
50 .status = TWL6040_HOOKINT,
53 .mask = TWL6040_HFMSK,
54 .status = TWL6040_HFINT,
57 .mask = TWL6040_VIBMSK,
58 .status = TWL6040_VIBINT,
61 .mask = TWL6040_READYMSK,
62 .status = TWL6040_READYINT,
67 struct twl6040_irq_data *irq_to_twl6040_irq(struct twl6040 *twl6040,
70 return &twl6040_irqs[irq - twl6040->irq_base];
73 static void twl6040_irq_lock(struct irq_data *data)
75 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
77 mutex_lock(&twl6040->irq_mutex);
80 static void twl6040_irq_sync_unlock(struct irq_data *data)
82 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
84 /* write back to hardware any change in irq mask */
85 if (twl6040->irq_masks_cur != twl6040->irq_masks_cache) {
86 twl6040->irq_masks_cache = twl6040->irq_masks_cur;
87 twl6040_reg_write(twl6040, TWL6040_REG_INTMR,
88 twl6040->irq_masks_cur);
91 mutex_unlock(&twl6040->irq_mutex);
94 static void twl6040_irq_enable(struct irq_data *data)
96 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
97 struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040,
100 twl6040->irq_masks_cur &= ~irq_data->mask;
103 static void twl6040_irq_disable(struct irq_data *data)
105 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
106 struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040,
109 twl6040->irq_masks_cur |= irq_data->mask;
112 static struct irq_chip twl6040_irq_chip = {
114 .irq_bus_lock = twl6040_irq_lock,
115 .irq_bus_sync_unlock = twl6040_irq_sync_unlock,
116 .irq_enable = twl6040_irq_enable,
117 .irq_disable = twl6040_irq_disable,
120 static irqreturn_t twl6040_irq_thread(int irq, void *data)
122 struct twl6040 *twl6040 = data;
126 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
128 /* apply masking and report (backwards to handle READYINT first) */
129 for (i = ARRAY_SIZE(twl6040_irqs) - 1; i >= 0; i--) {
130 if (twl6040->irq_masks_cur & twl6040_irqs[i].mask)
131 intid &= ~twl6040_irqs[i].status;
132 if (intid & twl6040_irqs[i].status)
133 handle_nested_irq(twl6040->irq_base + i);
136 /* ack unmasked irqs */
137 twl6040_reg_write(twl6040, TWL6040_REG_INTID, intid);
142 int twl6040_irq_init(struct twl6040 *twl6040)
144 struct device_node *node = twl6040->dev->of_node;
145 int i, nr_irqs, irq_base, ret;
148 mutex_init(&twl6040->irq_mutex);
150 /* mask the individual interrupt sources */
151 twl6040->irq_masks_cur = TWL6040_ALLINT_MSK;
152 twl6040->irq_masks_cache = TWL6040_ALLINT_MSK;
153 twl6040_reg_write(twl6040, TWL6040_REG_INTMR, TWL6040_ALLINT_MSK);
155 nr_irqs = ARRAY_SIZE(twl6040_irqs);
157 irq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
158 if (IS_ERR_VALUE(irq_base)) {
159 dev_err(twl6040->dev, "Fail to allocate IRQ descs\n");
162 twl6040->irq_base = irq_base;
164 irq_domain_add_legacy(node, ARRAY_SIZE(twl6040_irqs), irq_base, 0,
165 &irq_domain_simple_ops, NULL);
167 /* Register them with genirq */
168 for (i = irq_base; i < irq_base + nr_irqs; i++) {
169 irq_set_chip_data(i, twl6040);
170 irq_set_chip_and_handler(i, &twl6040_irq_chip,
172 irq_set_nested_thread(i, 1);
174 /* ARM needs us to explicitly flag the IRQ as valid
175 * and will set them noprobe when we do so. */
177 set_irq_flags(i, IRQF_VALID);
183 ret = request_threaded_irq(twl6040->irq, NULL, twl6040_irq_thread,
184 IRQF_ONESHOT, "twl6040", twl6040);
186 dev_err(twl6040->dev, "failed to request IRQ %d: %d\n",
191 /* reset interrupts */
192 val = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
194 /* interrupts cleared on write */
195 twl6040_clear_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_INTCLRMODE);
199 EXPORT_SYMBOL(twl6040_irq_init);
201 void twl6040_irq_exit(struct twl6040 *twl6040)
203 free_irq(twl6040->irq, twl6040);
205 EXPORT_SYMBOL(twl6040_irq_exit);