blob: 86c01ee5b1c71178afce1806b48f060d4276e23a [file] [log] [blame]
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#include <linux/bitops.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/sched.h>
16#include <linux/irq.h>
17#include <linux/mfd/core.h>
18#include <linux/mfd/wcd9xxx/core.h>
19#include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
20#include <linux/mfd/wcd9xxx/wcd9310_registers.h>
21#include <linux/interrupt.h>
22
23#define BYTE_BIT_MASK(nr) (1UL << ((nr) % BITS_PER_BYTE))
24#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
25
26struct wcd9xxx_irq {
27 bool level;
28};
29
30static struct wcd9xxx_irq wcd9xxx_irqs[TABLA_NUM_IRQS] = {
31 [0] = { .level = 1},
32/* All other wcd9xxx interrupts are edge triggered */
33};
34
35static inline int irq_to_wcd9xxx_irq(struct wcd9xxx *wcd9xxx, int irq)
36{
37 return irq - wcd9xxx->irq_base;
38}
39
40static void wcd9xxx_irq_lock(struct irq_data *data)
41{
42 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
43 mutex_lock(&wcd9xxx->irq_lock);
44}
45
46static void wcd9xxx_irq_sync_unlock(struct irq_data *data)
47{
48 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
49 int i;
50
51 for (i = 0; i < ARRAY_SIZE(wcd9xxx->irq_masks_cur); i++) {
52 /* If there's been a change in the mask write it back
53 * to the hardware.
54 */
55 if (wcd9xxx->irq_masks_cur[i] != wcd9xxx->irq_masks_cache[i]) {
56 wcd9xxx->irq_masks_cache[i] = wcd9xxx->irq_masks_cur[i];
57 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_MASK0+i,
58 wcd9xxx->irq_masks_cur[i]);
59 }
60 }
61
62 mutex_unlock(&wcd9xxx->irq_lock);
63}
64
65static void wcd9xxx_irq_enable(struct irq_data *data)
66{
67 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
68 int wcd9xxx_irq = irq_to_wcd9xxx_irq(wcd9xxx, data->irq);
69 wcd9xxx->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)] &=
70 ~(BYTE_BIT_MASK(wcd9xxx_irq));
71}
72
73static void wcd9xxx_irq_disable(struct irq_data *data)
74{
75 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
76 int wcd9xxx_irq = irq_to_wcd9xxx_irq(wcd9xxx, data->irq);
77 wcd9xxx->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)]
78 |= BYTE_BIT_MASK(wcd9xxx_irq);
79}
80
81static struct irq_chip wcd9xxx_irq_chip = {
82 .name = "wcd9xxx",
83 .irq_bus_lock = wcd9xxx_irq_lock,
84 .irq_bus_sync_unlock = wcd9xxx_irq_sync_unlock,
85 .irq_disable = wcd9xxx_irq_disable,
86 .irq_enable = wcd9xxx_irq_enable,
87};
88
89enum wcd9xxx_pm_state wcd9xxx_pm_cmpxchg(struct wcd9xxx *wcd9xxx,
90 enum wcd9xxx_pm_state o,
91 enum wcd9xxx_pm_state n)
92{
93 enum wcd9xxx_pm_state old;
94 mutex_lock(&wcd9xxx->pm_lock);
95 old = wcd9xxx->pm_state;
96 if (old == o)
97 wcd9xxx->pm_state = n;
98 mutex_unlock(&wcd9xxx->pm_lock);
99 return old;
100}
101EXPORT_SYMBOL_GPL(wcd9xxx_pm_cmpxchg);
102
103void wcd9xxx_lock_sleep(struct wcd9xxx *wcd9xxx)
104{
105 enum wcd9xxx_pm_state os;
106
107 /* wcd9xxx_{lock/unlock}_sleep will be called by wcd9xxx_irq_thread
108 * and its subroutines only motly.
109 * but btn0_lpress_fn is not wcd9xxx_irq_thread's subroutine and
110 * it can race with wcd9xxx_irq_thread.
111 * so need to embrace wlock_holders with mutex.
112 */
113 mutex_lock(&wcd9xxx->pm_lock);
114 if (wcd9xxx->wlock_holders++ == 0)
115 wake_lock(&wcd9xxx->wlock);
116 mutex_unlock(&wcd9xxx->pm_lock);
117 while (!wait_event_timeout(wcd9xxx->pm_wq,
118 ((os = wcd9xxx_pm_cmpxchg(wcd9xxx, WCD9XXX_PM_SLEEPABLE,
119 WCD9XXX_PM_AWAKE)) ==
120 WCD9XXX_PM_SLEEPABLE ||
121 (os == WCD9XXX_PM_AWAKE)),
122 5 * HZ)) {
123 pr_err("%s: system didn't resume within 5000ms, state %d, "
124 "wlock %d\n", __func__, wcd9xxx->pm_state,
125 wcd9xxx->wlock_holders);
126 WARN_ON_ONCE(1);
127 }
128 wake_up_all(&wcd9xxx->pm_wq);
129}
130EXPORT_SYMBOL_GPL(wcd9xxx_lock_sleep);
131
132void wcd9xxx_unlock_sleep(struct wcd9xxx *wcd9xxx)
133{
134 mutex_lock(&wcd9xxx->pm_lock);
135 if (--wcd9xxx->wlock_holders == 0) {
136 wcd9xxx->pm_state = WCD9XXX_PM_SLEEPABLE;
137 wake_unlock(&wcd9xxx->wlock);
138 }
139 mutex_unlock(&wcd9xxx->pm_lock);
140 wake_up_all(&wcd9xxx->pm_wq);
141}
142EXPORT_SYMBOL_GPL(wcd9xxx_unlock_sleep);
143
144static irqreturn_t wcd9xxx_irq_thread(int irq, void *data)
145{
146 int ret;
147 struct wcd9xxx *wcd9xxx = data;
148 u8 status[WCD9XXX_NUM_IRQ_REGS];
149 unsigned int i;
150
151 wcd9xxx_lock_sleep(wcd9xxx);
152 ret = wcd9xxx_bulk_read(wcd9xxx, TABLA_A_INTR_STATUS0,
153 WCD9XXX_NUM_IRQ_REGS, status);
154 if (ret < 0) {
155 dev_err(wcd9xxx->dev, "Failed to read interrupt status: %d\n",
156 ret);
157 wcd9xxx_unlock_sleep(wcd9xxx);
158 return IRQ_NONE;
159 }
160 /* Apply masking */
161 for (i = 0; i < WCD9XXX_NUM_IRQ_REGS; i++)
162 status[i] &= ~wcd9xxx->irq_masks_cur[i];
163
164 /* Find out which interrupt was triggered and call that interrupt's
165 * handler function
166 */
167 for (i = 0; i < TABLA_NUM_IRQS; i++) {
168 if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i)) {
169 if ((i <= TABLA_IRQ_MBHC_INSERTION) &&
170 (i >= TABLA_IRQ_MBHC_REMOVAL)) {
171 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_CLEAR0 +
172 BIT_BYTE(i), BYTE_BIT_MASK(i));
173 if (wcd9xxx_get_intf_type() ==
174 WCD9XXX_INTERFACE_TYPE_I2C)
175 wcd9xxx_reg_write(wcd9xxx,
176 TABLA_A_INTR_MODE, 0x02);
177 handle_nested_irq(wcd9xxx->irq_base + i);
178 } else {
179 handle_nested_irq(wcd9xxx->irq_base + i);
180 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_CLEAR0 +
181 BIT_BYTE(i), BYTE_BIT_MASK(i));
182 if (wcd9xxx_get_intf_type() ==
183 WCD9XXX_INTERFACE_TYPE_I2C)
184 wcd9xxx_reg_write(wcd9xxx,
185 TABLA_A_INTR_MODE, 0x02);
186 }
187 break;
188 }
189 }
190 wcd9xxx_unlock_sleep(wcd9xxx);
191
192 return IRQ_HANDLED;
193}
194
195int wcd9xxx_irq_init(struct wcd9xxx *wcd9xxx)
196{
197 int ret;
198 unsigned int i, cur_irq;
199
200 mutex_init(&wcd9xxx->irq_lock);
201
202 if (!wcd9xxx->irq) {
203 dev_warn(wcd9xxx->dev,
204 "No interrupt specified, no interrupts\n");
205 wcd9xxx->irq_base = 0;
206 return 0;
207 }
208
209 if (!wcd9xxx->irq_base) {
210 dev_err(wcd9xxx->dev,
211 "No interrupt base specified, no interrupts\n");
212 return 0;
213 }
214 /* Mask the individual interrupt sources */
215 for (i = 0, cur_irq = wcd9xxx->irq_base; i < TABLA_NUM_IRQS; i++,
216 cur_irq++) {
217
218 irq_set_chip_data(cur_irq, wcd9xxx);
219
220 if (wcd9xxx_irqs[i].level)
221 irq_set_chip_and_handler(cur_irq, &wcd9xxx_irq_chip,
222 handle_level_irq);
223 else
224 irq_set_chip_and_handler(cur_irq, &wcd9xxx_irq_chip,
225 handle_edge_irq);
226
227 irq_set_nested_thread(cur_irq, 1);
228
229 /* ARM needs us to explicitly flag the IRQ as valid
230 * and will set them noprobe when we do so. */
231#ifdef CONFIG_ARM
232 set_irq_flags(cur_irq, IRQF_VALID);
233#else
234 set_irq_noprobe(cur_irq);
235#endif
236
237 wcd9xxx->irq_masks_cur[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
238 wcd9xxx->irq_masks_cache[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
239 wcd9xxx->irq_level[BIT_BYTE(i)] |= wcd9xxx_irqs[i].level <<
240 (i % BITS_PER_BYTE);
241 }
242 for (i = 0; i < WCD9XXX_NUM_IRQ_REGS; i++) {
243 /* Initialize interrupt mask and level registers */
244 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_LEVEL0 + i,
245 wcd9xxx->irq_level[i]);
246 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_MASK0 + i,
247 wcd9xxx->irq_masks_cur[i]);
248 }
249
250 ret = request_threaded_irq(wcd9xxx->irq, NULL, wcd9xxx_irq_thread,
251 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
252 "wcd9xxx", wcd9xxx);
253 if (ret != 0)
254 dev_err(wcd9xxx->dev, "Failed to request IRQ %d: %d\n",
255 wcd9xxx->irq, ret);
256 else {
257 ret = enable_irq_wake(wcd9xxx->irq);
258 if (ret == 0) {
259 ret = device_init_wakeup(wcd9xxx->dev, 1);
260 if (ret) {
261 dev_err(wcd9xxx->dev, "Failed to init device"
262 "wakeup : %d\n", ret);
263 disable_irq_wake(wcd9xxx->irq);
264 }
265 } else
266 dev_err(wcd9xxx->dev, "Failed to set wake interrupt on"
267 " IRQ %d: %d\n", wcd9xxx->irq, ret);
268 if (ret)
269 free_irq(wcd9xxx->irq, wcd9xxx);
270 }
271
272 if (ret)
273 mutex_destroy(&wcd9xxx->irq_lock);
274
275 return ret;
276}
277
278void wcd9xxx_irq_exit(struct wcd9xxx *wcd9xxx)
279{
280 if (wcd9xxx->irq) {
281 disable_irq_wake(wcd9xxx->irq);
282 free_irq(wcd9xxx->irq, wcd9xxx);
283 device_init_wakeup(wcd9xxx->dev, 0);
284 }
285 mutex_destroy(&wcd9xxx->irq_lock);
286}