blob: 914978e1b62e6aac971e4ec7b8f97b1b0417d8a6 [file] [log] [blame]
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -05001/*
2 * Interrupt controller support for TWL6040
3 *
4 * Author: Misael Lopez Cruz <misael.lopez@ti.com>
5 *
6 * Copyright: (C) 2011 Texas Instruments, Inc.
7 *
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.
11 *
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.
16 *
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
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
Peter Ujfalusi67124192012-05-16 14:11:56 +030026#include <linux/err.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050027#include <linux/irq.h>
28#include <linux/interrupt.h>
29#include <linux/mfd/core.h>
30#include <linux/mfd/twl6040.h>
31
32struct twl6040_irq_data {
33 int mask;
34 int status;
35};
36
37static struct twl6040_irq_data twl6040_irqs[] = {
38 {
39 .mask = TWL6040_THMSK,
40 .status = TWL6040_THINT,
41 },
42 {
43 .mask = TWL6040_PLUGMSK,
44 .status = TWL6040_PLUGINT | TWL6040_UNPLUGINT,
45 },
46 {
47 .mask = TWL6040_HOOKMSK,
48 .status = TWL6040_HOOKINT,
49 },
50 {
51 .mask = TWL6040_HFMSK,
52 .status = TWL6040_HFINT,
53 },
54 {
55 .mask = TWL6040_VIBMSK,
56 .status = TWL6040_VIBINT,
57 },
58 {
59 .mask = TWL6040_READYMSK,
60 .status = TWL6040_READYINT,
61 },
62};
63
64static inline
65struct twl6040_irq_data *irq_to_twl6040_irq(struct twl6040 *twl6040,
66 int irq)
67{
68 return &twl6040_irqs[irq - twl6040->irq_base];
69}
70
71static void twl6040_irq_lock(struct irq_data *data)
72{
73 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
74
75 mutex_lock(&twl6040->irq_mutex);
76}
77
78static void twl6040_irq_sync_unlock(struct irq_data *data)
79{
80 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
81
82 /* write back to hardware any change in irq mask */
83 if (twl6040->irq_masks_cur != twl6040->irq_masks_cache) {
84 twl6040->irq_masks_cache = twl6040->irq_masks_cur;
85 twl6040_reg_write(twl6040, TWL6040_REG_INTMR,
86 twl6040->irq_masks_cur);
87 }
88
89 mutex_unlock(&twl6040->irq_mutex);
90}
91
92static void twl6040_irq_enable(struct irq_data *data)
93{
94 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
95 struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040,
96 data->irq);
97
98 twl6040->irq_masks_cur &= ~irq_data->mask;
99}
100
101static void twl6040_irq_disable(struct irq_data *data)
102{
103 struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
104 struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040,
105 data->irq);
106
107 twl6040->irq_masks_cur |= irq_data->mask;
108}
109
110static struct irq_chip twl6040_irq_chip = {
111 .name = "twl6040",
112 .irq_bus_lock = twl6040_irq_lock,
113 .irq_bus_sync_unlock = twl6040_irq_sync_unlock,
114 .irq_enable = twl6040_irq_enable,
115 .irq_disable = twl6040_irq_disable,
116};
117
118static irqreturn_t twl6040_irq_thread(int irq, void *data)
119{
120 struct twl6040 *twl6040 = data;
121 u8 intid;
122 int i;
123
124 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
125
126 /* apply masking and report (backwards to handle READYINT first) */
127 for (i = ARRAY_SIZE(twl6040_irqs) - 1; i >= 0; i--) {
128 if (twl6040->irq_masks_cur & twl6040_irqs[i].mask)
129 intid &= ~twl6040_irqs[i].status;
130 if (intid & twl6040_irqs[i].status)
131 handle_nested_irq(twl6040->irq_base + i);
132 }
133
134 /* ack unmasked irqs */
135 twl6040_reg_write(twl6040, TWL6040_REG_INTID, intid);
136
137 return IRQ_HANDLED;
138}
139
140int twl6040_irq_init(struct twl6040 *twl6040)
141{
Peter Ujfalusi67124192012-05-16 14:11:56 +0300142 int i, nr_irqs, irq_base, ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500143 u8 val;
144
145 mutex_init(&twl6040->irq_mutex);
146
147 /* mask the individual interrupt sources */
148 twl6040->irq_masks_cur = TWL6040_ALLINT_MSK;
149 twl6040->irq_masks_cache = TWL6040_ALLINT_MSK;
150 twl6040_reg_write(twl6040, TWL6040_REG_INTMR, TWL6040_ALLINT_MSK);
151
Peter Ujfalusi68029c62012-05-16 14:11:55 +0300152 nr_irqs = ARRAY_SIZE(twl6040_irqs);
Peter Ujfalusi67124192012-05-16 14:11:56 +0300153
154 irq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
155 if (IS_ERR_VALUE(irq_base)) {
156 dev_err(twl6040->dev, "Fail to allocate IRQ descs\n");
157 return irq_base;
158 }
159 twl6040->irq_base = irq_base;
160
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500161 /* Register them with genirq */
Peter Ujfalusi67124192012-05-16 14:11:56 +0300162 for (i = irq_base; i < irq_base + nr_irqs; i++) {
Peter Ujfalusi68029c62012-05-16 14:11:55 +0300163 irq_set_chip_data(i, twl6040);
164 irq_set_chip_and_handler(i, &twl6040_irq_chip,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500165 handle_level_irq);
Peter Ujfalusi68029c62012-05-16 14:11:55 +0300166 irq_set_nested_thread(i, 1);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500167
168 /* ARM needs us to explicitly flag the IRQ as valid
169 * and will set them noprobe when we do so. */
170#ifdef CONFIG_ARM
Peter Ujfalusi68029c62012-05-16 14:11:55 +0300171 set_irq_flags(i, IRQF_VALID);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500172#else
Peter Ujfalusi68029c62012-05-16 14:11:55 +0300173 irq_set_noprobe(i);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500174#endif
175 }
176
177 ret = request_threaded_irq(twl6040->irq, NULL, twl6040_irq_thread,
178 IRQF_ONESHOT, "twl6040", twl6040);
179 if (ret) {
180 dev_err(twl6040->dev, "failed to request IRQ %d: %d\n",
181 twl6040->irq, ret);
182 return ret;
183 }
184
185 /* reset interrupts */
186 val = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
187
188 /* interrupts cleared on write */
189 twl6040_clear_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_INTCLRMODE);
190
191 return 0;
192}
193EXPORT_SYMBOL(twl6040_irq_init);
194
195void twl6040_irq_exit(struct twl6040 *twl6040)
196{
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300197 free_irq(twl6040->irq, twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500198}
199EXPORT_SYMBOL(twl6040_irq_exit);