Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / mfd / twl6040-irq.c
blobb3f8ddaa28a8b792c14d2b5c785ffed4b099a735
1 /*
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
20 * 02110-1301 USA
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/irq.h>
27 #include <linux/interrupt.h>
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/twl6040.h>
31 struct twl6040_irq_data {
32 int mask;
33 int status;
36 static struct twl6040_irq_data twl6040_irqs[] = {
38 .mask = TWL6040_THMSK,
39 .status = TWL6040_THINT,
42 .mask = TWL6040_PLUGMSK,
43 .status = TWL6040_PLUGINT | TWL6040_UNPLUGINT,
46 .mask = TWL6040_HOOKMSK,
47 .status = TWL6040_HOOKINT,
50 .mask = TWL6040_HFMSK,
51 .status = TWL6040_HFINT,
54 .mask = TWL6040_VIBMSK,
55 .status = TWL6040_VIBINT,
58 .mask = TWL6040_READYMSK,
59 .status = TWL6040_READYINT,
63 static inline
64 struct twl6040_irq_data *irq_to_twl6040_irq(struct twl6040 *twl6040,
65 int irq)
67 return &twl6040_irqs[irq - twl6040->irq_base];
70 static void twl6040_irq_lock(struct irq_data *data)
72 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
74 mutex_lock(&twl6040->irq_mutex);
77 static void twl6040_irq_sync_unlock(struct irq_data *data)
79 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
81 /* write back to hardware any change in irq mask */
82 if (twl6040->irq_masks_cur != twl6040->irq_masks_cache) {
83 twl6040->irq_masks_cache = twl6040->irq_masks_cur;
84 twl6040_reg_write(twl6040, TWL6040_REG_INTMR,
85 twl6040->irq_masks_cur);
88 mutex_unlock(&twl6040->irq_mutex);
91 static void twl6040_irq_enable(struct irq_data *data)
93 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
94 struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040,
95 data->irq);
97 twl6040->irq_masks_cur &= ~irq_data->mask;
100 static void twl6040_irq_disable(struct irq_data *data)
102 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
103 struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040,
104 data->irq);
106 twl6040->irq_masks_cur |= irq_data->mask;
109 static struct irq_chip twl6040_irq_chip = {
110 .name = "twl6040",
111 .irq_bus_lock = twl6040_irq_lock,
112 .irq_bus_sync_unlock = twl6040_irq_sync_unlock,
113 .irq_enable = twl6040_irq_enable,
114 .irq_disable = twl6040_irq_disable,
117 static irqreturn_t twl6040_irq_thread(int irq, void *data)
119 struct twl6040 *twl6040 = data;
120 u8 intid;
121 int i;
123 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
125 /* apply masking and report (backwards to handle READYINT first) */
126 for (i = ARRAY_SIZE(twl6040_irqs) - 1; i >= 0; i--) {
127 if (twl6040->irq_masks_cur & twl6040_irqs[i].mask)
128 intid &= ~twl6040_irqs[i].status;
129 if (intid & twl6040_irqs[i].status)
130 handle_nested_irq(twl6040->irq_base + i);
133 /* ack unmasked irqs */
134 twl6040_reg_write(twl6040, TWL6040_REG_INTID, intid);
136 return IRQ_HANDLED;
139 int twl6040_irq_init(struct twl6040 *twl6040)
141 int cur_irq, ret;
142 u8 val;
144 mutex_init(&twl6040->irq_mutex);
146 /* mask the individual interrupt sources */
147 twl6040->irq_masks_cur = TWL6040_ALLINT_MSK;
148 twl6040->irq_masks_cache = TWL6040_ALLINT_MSK;
149 twl6040_reg_write(twl6040, TWL6040_REG_INTMR, TWL6040_ALLINT_MSK);
151 /* Register them with genirq */
152 for (cur_irq = twl6040->irq_base;
153 cur_irq < twl6040->irq_base + ARRAY_SIZE(twl6040_irqs);
154 cur_irq++) {
155 irq_set_chip_data(cur_irq, twl6040);
156 irq_set_chip_and_handler(cur_irq, &twl6040_irq_chip,
157 handle_level_irq);
158 irq_set_nested_thread(cur_irq, 1);
160 /* ARM needs us to explicitly flag the IRQ as valid
161 * and will set them noprobe when we do so. */
162 #ifdef CONFIG_ARM
163 set_irq_flags(cur_irq, IRQF_VALID);
164 #else
165 irq_set_noprobe(cur_irq);
166 #endif
169 ret = request_threaded_irq(twl6040->irq, NULL, twl6040_irq_thread,
170 IRQF_ONESHOT, "twl6040", twl6040);
171 if (ret) {
172 dev_err(twl6040->dev, "failed to request IRQ %d: %d\n",
173 twl6040->irq, ret);
174 return ret;
177 /* reset interrupts */
178 val = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
180 /* interrupts cleared on write */
181 twl6040_clear_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_INTCLRMODE);
183 return 0;
185 EXPORT_SYMBOL(twl6040_irq_init);
187 void twl6040_irq_exit(struct twl6040 *twl6040)
189 free_irq(twl6040->irq, twl6040);
191 EXPORT_SYMBOL(twl6040_irq_exit);