blob: c1febcfdc707b18023da6aee04fbd5eeb6a3048c [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
Joonwoo Park03324832012-03-19 19:36:16 -0700144static void wcd9xxx_irq_dispatch(struct wcd9xxx *wcd9xxx, int irqbit)
145{
146 if ((irqbit <= TABLA_IRQ_MBHC_INSERTION) &&
147 (irqbit >= TABLA_IRQ_MBHC_REMOVAL)) {
148 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_CLEAR0 +
149 BIT_BYTE(irqbit), BYTE_BIT_MASK(irqbit));
150 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
151 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_MODE, 0x02);
152 handle_nested_irq(wcd9xxx->irq_base + irqbit);
153 } else {
154 handle_nested_irq(wcd9xxx->irq_base + irqbit);
155 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_CLEAR0 +
156 BIT_BYTE(irqbit), BYTE_BIT_MASK(irqbit));
157 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
158 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_MODE, 0x02);
159 }
160}
161
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530162static irqreturn_t wcd9xxx_irq_thread(int irq, void *data)
163{
164 int ret;
165 struct wcd9xxx *wcd9xxx = data;
166 u8 status[WCD9XXX_NUM_IRQ_REGS];
Joonwoo Park03324832012-03-19 19:36:16 -0700167 int i;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530168
169 wcd9xxx_lock_sleep(wcd9xxx);
170 ret = wcd9xxx_bulk_read(wcd9xxx, TABLA_A_INTR_STATUS0,
171 WCD9XXX_NUM_IRQ_REGS, status);
172 if (ret < 0) {
173 dev_err(wcd9xxx->dev, "Failed to read interrupt status: %d\n",
174 ret);
175 wcd9xxx_unlock_sleep(wcd9xxx);
176 return IRQ_NONE;
177 }
178 /* Apply masking */
179 for (i = 0; i < WCD9XXX_NUM_IRQ_REGS; i++)
180 status[i] &= ~wcd9xxx->irq_masks_cur[i];
181
182 /* Find out which interrupt was triggered and call that interrupt's
183 * handler function
184 */
Joonwoo Park03324832012-03-19 19:36:16 -0700185 if (status[BIT_BYTE(TABLA_IRQ_SLIMBUS)] &
186 BYTE_BIT_MASK(TABLA_IRQ_SLIMBUS))
187 wcd9xxx_irq_dispatch(wcd9xxx, TABLA_IRQ_SLIMBUS);
188
189 /* Since codec has only one hardware irq line which is shared by
190 * codec's different internal interrupts, so it's possible master irq
191 * handler dispatches multiple nested irq handlers after breaking
192 * order. Dispatch MBHC interrupts order to follow MBHC state
193 * machine's order */
194 for (i = TABLA_IRQ_MBHC_INSERTION; i >= TABLA_IRQ_MBHC_REMOVAL; i--) {
195 if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i))
196 wcd9xxx_irq_dispatch(wcd9xxx, i);
197 }
198 for (i = TABLA_IRQ_BG_PRECHARGE; i < TABLA_NUM_IRQS; i++) {
199 if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i))
200 wcd9xxx_irq_dispatch(wcd9xxx, i);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530201 }
202 wcd9xxx_unlock_sleep(wcd9xxx);
203
204 return IRQ_HANDLED;
205}
206
207int wcd9xxx_irq_init(struct wcd9xxx *wcd9xxx)
208{
209 int ret;
210 unsigned int i, cur_irq;
211
212 mutex_init(&wcd9xxx->irq_lock);
213
214 if (!wcd9xxx->irq) {
215 dev_warn(wcd9xxx->dev,
216 "No interrupt specified, no interrupts\n");
217 wcd9xxx->irq_base = 0;
218 return 0;
219 }
220
221 if (!wcd9xxx->irq_base) {
222 dev_err(wcd9xxx->dev,
223 "No interrupt base specified, no interrupts\n");
224 return 0;
225 }
226 /* Mask the individual interrupt sources */
227 for (i = 0, cur_irq = wcd9xxx->irq_base; i < TABLA_NUM_IRQS; i++,
228 cur_irq++) {
229
230 irq_set_chip_data(cur_irq, wcd9xxx);
231
232 if (wcd9xxx_irqs[i].level)
233 irq_set_chip_and_handler(cur_irq, &wcd9xxx_irq_chip,
234 handle_level_irq);
235 else
236 irq_set_chip_and_handler(cur_irq, &wcd9xxx_irq_chip,
237 handle_edge_irq);
238
239 irq_set_nested_thread(cur_irq, 1);
240
241 /* ARM needs us to explicitly flag the IRQ as valid
242 * and will set them noprobe when we do so. */
243#ifdef CONFIG_ARM
244 set_irq_flags(cur_irq, IRQF_VALID);
245#else
246 set_irq_noprobe(cur_irq);
247#endif
248
249 wcd9xxx->irq_masks_cur[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
250 wcd9xxx->irq_masks_cache[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
251 wcd9xxx->irq_level[BIT_BYTE(i)] |= wcd9xxx_irqs[i].level <<
252 (i % BITS_PER_BYTE);
253 }
254 for (i = 0; i < WCD9XXX_NUM_IRQ_REGS; i++) {
255 /* Initialize interrupt mask and level registers */
256 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_LEVEL0 + i,
257 wcd9xxx->irq_level[i]);
258 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_MASK0 + i,
259 wcd9xxx->irq_masks_cur[i]);
260 }
261
262 ret = request_threaded_irq(wcd9xxx->irq, NULL, wcd9xxx_irq_thread,
263 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
264 "wcd9xxx", wcd9xxx);
265 if (ret != 0)
266 dev_err(wcd9xxx->dev, "Failed to request IRQ %d: %d\n",
267 wcd9xxx->irq, ret);
268 else {
269 ret = enable_irq_wake(wcd9xxx->irq);
270 if (ret == 0) {
271 ret = device_init_wakeup(wcd9xxx->dev, 1);
272 if (ret) {
273 dev_err(wcd9xxx->dev, "Failed to init device"
274 "wakeup : %d\n", ret);
275 disable_irq_wake(wcd9xxx->irq);
276 }
277 } else
278 dev_err(wcd9xxx->dev, "Failed to set wake interrupt on"
279 " IRQ %d: %d\n", wcd9xxx->irq, ret);
280 if (ret)
281 free_irq(wcd9xxx->irq, wcd9xxx);
282 }
283
284 if (ret)
285 mutex_destroy(&wcd9xxx->irq_lock);
286
287 return ret;
288}
289
290void wcd9xxx_irq_exit(struct wcd9xxx *wcd9xxx)
291{
292 if (wcd9xxx->irq) {
293 disable_irq_wake(wcd9xxx->irq);
294 free_irq(wcd9xxx->irq, wcd9xxx);
295 device_init_wakeup(wcd9xxx->dev, 0);
296 }
297 mutex_destroy(&wcd9xxx->irq_lock);
298}