blob: a5f9fadbf82e1d060c4b26be3c4d65f92497da63 [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05302 *
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
Stephen Boyd2fcabf92012-05-30 10:41:11 -070023#include <mach/cpuidle.h>
24
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053025#define BYTE_BIT_MASK(nr) (1UL << ((nr) % BITS_PER_BYTE))
26#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
27
28struct wcd9xxx_irq {
29 bool level;
30};
31
32static struct wcd9xxx_irq wcd9xxx_irqs[TABLA_NUM_IRQS] = {
33 [0] = { .level = 1},
34/* All other wcd9xxx interrupts are edge triggered */
35};
36
37static inline int irq_to_wcd9xxx_irq(struct wcd9xxx *wcd9xxx, int irq)
38{
39 return irq - wcd9xxx->irq_base;
40}
41
42static void wcd9xxx_irq_lock(struct irq_data *data)
43{
44 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
45 mutex_lock(&wcd9xxx->irq_lock);
46}
47
48static void wcd9xxx_irq_sync_unlock(struct irq_data *data)
49{
50 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
51 int i;
52
53 for (i = 0; i < ARRAY_SIZE(wcd9xxx->irq_masks_cur); i++) {
54 /* If there's been a change in the mask write it back
55 * to the hardware.
56 */
57 if (wcd9xxx->irq_masks_cur[i] != wcd9xxx->irq_masks_cache[i]) {
58 wcd9xxx->irq_masks_cache[i] = wcd9xxx->irq_masks_cur[i];
59 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_MASK0+i,
60 wcd9xxx->irq_masks_cur[i]);
61 }
62 }
63
64 mutex_unlock(&wcd9xxx->irq_lock);
65}
66
67static void wcd9xxx_irq_enable(struct irq_data *data)
68{
69 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
70 int wcd9xxx_irq = irq_to_wcd9xxx_irq(wcd9xxx, data->irq);
71 wcd9xxx->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)] &=
72 ~(BYTE_BIT_MASK(wcd9xxx_irq));
73}
74
75static void wcd9xxx_irq_disable(struct irq_data *data)
76{
77 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
78 int wcd9xxx_irq = irq_to_wcd9xxx_irq(wcd9xxx, data->irq);
79 wcd9xxx->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)]
80 |= BYTE_BIT_MASK(wcd9xxx_irq);
81}
82
83static struct irq_chip wcd9xxx_irq_chip = {
84 .name = "wcd9xxx",
85 .irq_bus_lock = wcd9xxx_irq_lock,
86 .irq_bus_sync_unlock = wcd9xxx_irq_sync_unlock,
87 .irq_disable = wcd9xxx_irq_disable,
88 .irq_enable = wcd9xxx_irq_enable,
89};
90
91enum wcd9xxx_pm_state wcd9xxx_pm_cmpxchg(struct wcd9xxx *wcd9xxx,
92 enum wcd9xxx_pm_state o,
93 enum wcd9xxx_pm_state n)
94{
95 enum wcd9xxx_pm_state old;
96 mutex_lock(&wcd9xxx->pm_lock);
97 old = wcd9xxx->pm_state;
98 if (old == o)
99 wcd9xxx->pm_state = n;
100 mutex_unlock(&wcd9xxx->pm_lock);
101 return old;
102}
103EXPORT_SYMBOL_GPL(wcd9xxx_pm_cmpxchg);
104
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700105bool wcd9xxx_lock_sleep(struct wcd9xxx *wcd9xxx)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530106{
107 enum wcd9xxx_pm_state os;
108
109 /* wcd9xxx_{lock/unlock}_sleep will be called by wcd9xxx_irq_thread
110 * and its subroutines only motly.
111 * but btn0_lpress_fn is not wcd9xxx_irq_thread's subroutine and
112 * it can race with wcd9xxx_irq_thread.
113 * so need to embrace wlock_holders with mutex.
114 */
115 mutex_lock(&wcd9xxx->pm_lock);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700116 if (wcd9xxx->wlock_holders++ == 0) {
117 pr_debug("%s: holding wake lock\n", __func__);
Stephen Boyd2fcabf92012-05-30 10:41:11 -0700118 pm_qos_update_request(&wcd9xxx->pm_qos_req,
119 msm_cpuidle_get_deep_idle_latency());
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700120 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530121 mutex_unlock(&wcd9xxx->pm_lock);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700122 if (!wait_event_timeout(wcd9xxx->pm_wq,
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530123 ((os = wcd9xxx_pm_cmpxchg(wcd9xxx, WCD9XXX_PM_SLEEPABLE,
124 WCD9XXX_PM_AWAKE)) ==
125 WCD9XXX_PM_SLEEPABLE ||
126 (os == WCD9XXX_PM_AWAKE)),
127 5 * HZ)) {
128 pr_err("%s: system didn't resume within 5000ms, state %d, "
129 "wlock %d\n", __func__, wcd9xxx->pm_state,
130 wcd9xxx->wlock_holders);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700131 WARN_ON(1);
132 wcd9xxx_unlock_sleep(wcd9xxx);
133 return false;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530134 }
135 wake_up_all(&wcd9xxx->pm_wq);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700136 return true;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530137}
138EXPORT_SYMBOL_GPL(wcd9xxx_lock_sleep);
139
140void wcd9xxx_unlock_sleep(struct wcd9xxx *wcd9xxx)
141{
142 mutex_lock(&wcd9xxx->pm_lock);
143 if (--wcd9xxx->wlock_holders == 0) {
144 wcd9xxx->pm_state = WCD9XXX_PM_SLEEPABLE;
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700145 pr_debug("%s: releasing wake lock\n", __func__);
Stephen Boyd2fcabf92012-05-30 10:41:11 -0700146 pm_qos_update_request(&wcd9xxx->pm_qos_req,
147 PM_QOS_DEFAULT_VALUE);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530148 }
149 mutex_unlock(&wcd9xxx->pm_lock);
150 wake_up_all(&wcd9xxx->pm_wq);
151}
152EXPORT_SYMBOL_GPL(wcd9xxx_unlock_sleep);
153
Joonwoo Parkd8796592013-01-07 12:40:03 -0800154void wcd9xxx_nested_irq_lock(struct wcd9xxx *wcd9xxx)
155{
156 mutex_lock(&wcd9xxx->nested_irq_lock);
157}
158
159void wcd9xxx_nested_irq_unlock(struct wcd9xxx *wcd9xxx)
160{
161 mutex_unlock(&wcd9xxx->nested_irq_lock);
162}
163
Joonwoo Park03324832012-03-19 19:36:16 -0700164static void wcd9xxx_irq_dispatch(struct wcd9xxx *wcd9xxx, int irqbit)
165{
166 if ((irqbit <= TABLA_IRQ_MBHC_INSERTION) &&
167 (irqbit >= TABLA_IRQ_MBHC_REMOVAL)) {
Joonwoo Parkd8796592013-01-07 12:40:03 -0800168 wcd9xxx_nested_irq_lock(wcd9xxx);
Joonwoo Park03324832012-03-19 19:36:16 -0700169 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_CLEAR0 +
170 BIT_BYTE(irqbit), BYTE_BIT_MASK(irqbit));
171 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
172 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_MODE, 0x02);
173 handle_nested_irq(wcd9xxx->irq_base + irqbit);
Joonwoo Parkd8796592013-01-07 12:40:03 -0800174 wcd9xxx_nested_irq_unlock(wcd9xxx);
Joonwoo Park03324832012-03-19 19:36:16 -0700175 } else {
Joonwoo Parkd8796592013-01-07 12:40:03 -0800176 wcd9xxx_nested_irq_lock(wcd9xxx);
Joonwoo Park03324832012-03-19 19:36:16 -0700177 handle_nested_irq(wcd9xxx->irq_base + irqbit);
178 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_CLEAR0 +
179 BIT_BYTE(irqbit), BYTE_BIT_MASK(irqbit));
180 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
181 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_MODE, 0x02);
Joonwoo Parkd8796592013-01-07 12:40:03 -0800182 wcd9xxx_nested_irq_unlock(wcd9xxx);
Joonwoo Park03324832012-03-19 19:36:16 -0700183 }
184}
185
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530186static irqreturn_t wcd9xxx_irq_thread(int irq, void *data)
187{
188 int ret;
189 struct wcd9xxx *wcd9xxx = data;
190 u8 status[WCD9XXX_NUM_IRQ_REGS];
Joonwoo Park03324832012-03-19 19:36:16 -0700191 int i;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530192
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700193 if (unlikely(wcd9xxx_lock_sleep(wcd9xxx) == false)) {
194 dev_err(wcd9xxx->dev, "Failed to hold suspend\n");
195 return IRQ_NONE;
196 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530197 ret = wcd9xxx_bulk_read(wcd9xxx, TABLA_A_INTR_STATUS0,
198 WCD9XXX_NUM_IRQ_REGS, status);
199 if (ret < 0) {
200 dev_err(wcd9xxx->dev, "Failed to read interrupt status: %d\n",
201 ret);
202 wcd9xxx_unlock_sleep(wcd9xxx);
203 return IRQ_NONE;
204 }
205 /* Apply masking */
206 for (i = 0; i < WCD9XXX_NUM_IRQ_REGS; i++)
207 status[i] &= ~wcd9xxx->irq_masks_cur[i];
208
209 /* Find out which interrupt was triggered and call that interrupt's
210 * handler function
211 */
Joonwoo Park03324832012-03-19 19:36:16 -0700212 if (status[BIT_BYTE(TABLA_IRQ_SLIMBUS)] &
213 BYTE_BIT_MASK(TABLA_IRQ_SLIMBUS))
214 wcd9xxx_irq_dispatch(wcd9xxx, TABLA_IRQ_SLIMBUS);
215
216 /* Since codec has only one hardware irq line which is shared by
217 * codec's different internal interrupts, so it's possible master irq
218 * handler dispatches multiple nested irq handlers after breaking
219 * order. Dispatch MBHC interrupts order to follow MBHC state
220 * machine's order */
221 for (i = TABLA_IRQ_MBHC_INSERTION; i >= TABLA_IRQ_MBHC_REMOVAL; i--) {
222 if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i))
223 wcd9xxx_irq_dispatch(wcd9xxx, i);
224 }
225 for (i = TABLA_IRQ_BG_PRECHARGE; i < TABLA_NUM_IRQS; i++) {
226 if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i))
227 wcd9xxx_irq_dispatch(wcd9xxx, i);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530228 }
229 wcd9xxx_unlock_sleep(wcd9xxx);
230
231 return IRQ_HANDLED;
232}
233
234int wcd9xxx_irq_init(struct wcd9xxx *wcd9xxx)
235{
236 int ret;
237 unsigned int i, cur_irq;
238
239 mutex_init(&wcd9xxx->irq_lock);
Joonwoo Parkd8796592013-01-07 12:40:03 -0800240 mutex_init(&wcd9xxx->nested_irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530241
242 if (!wcd9xxx->irq) {
243 dev_warn(wcd9xxx->dev,
244 "No interrupt specified, no interrupts\n");
245 wcd9xxx->irq_base = 0;
246 return 0;
247 }
248
249 if (!wcd9xxx->irq_base) {
250 dev_err(wcd9xxx->dev,
251 "No interrupt base specified, no interrupts\n");
252 return 0;
Joonwoo Parkd8796592013-01-07 12:40:03 -0800253 mutex_destroy(&wcd9xxx->nested_irq_lock);
254 mutex_destroy(&wcd9xxx->irq_lock);
255 mutex_destroy(&wcd9xxx->nested_irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530256 }
257 /* Mask the individual interrupt sources */
258 for (i = 0, cur_irq = wcd9xxx->irq_base; i < TABLA_NUM_IRQS; i++,
259 cur_irq++) {
260
261 irq_set_chip_data(cur_irq, wcd9xxx);
262
263 if (wcd9xxx_irqs[i].level)
264 irq_set_chip_and_handler(cur_irq, &wcd9xxx_irq_chip,
265 handle_level_irq);
266 else
267 irq_set_chip_and_handler(cur_irq, &wcd9xxx_irq_chip,
268 handle_edge_irq);
269
270 irq_set_nested_thread(cur_irq, 1);
271
272 /* ARM needs us to explicitly flag the IRQ as valid
273 * and will set them noprobe when we do so. */
274#ifdef CONFIG_ARM
275 set_irq_flags(cur_irq, IRQF_VALID);
276#else
277 set_irq_noprobe(cur_irq);
278#endif
279
280 wcd9xxx->irq_masks_cur[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
281 wcd9xxx->irq_masks_cache[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
282 wcd9xxx->irq_level[BIT_BYTE(i)] |= wcd9xxx_irqs[i].level <<
283 (i % BITS_PER_BYTE);
284 }
285 for (i = 0; i < WCD9XXX_NUM_IRQ_REGS; i++) {
286 /* Initialize interrupt mask and level registers */
287 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_LEVEL0 + i,
288 wcd9xxx->irq_level[i]);
289 wcd9xxx_reg_write(wcd9xxx, TABLA_A_INTR_MASK0 + i,
290 wcd9xxx->irq_masks_cur[i]);
291 }
292
293 ret = request_threaded_irq(wcd9xxx->irq, NULL, wcd9xxx_irq_thread,
294 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
295 "wcd9xxx", wcd9xxx);
296 if (ret != 0)
297 dev_err(wcd9xxx->dev, "Failed to request IRQ %d: %d\n",
298 wcd9xxx->irq, ret);
299 else {
300 ret = enable_irq_wake(wcd9xxx->irq);
301 if (ret == 0) {
302 ret = device_init_wakeup(wcd9xxx->dev, 1);
303 if (ret) {
304 dev_err(wcd9xxx->dev, "Failed to init device"
305 "wakeup : %d\n", ret);
306 disable_irq_wake(wcd9xxx->irq);
307 }
308 } else
309 dev_err(wcd9xxx->dev, "Failed to set wake interrupt on"
310 " IRQ %d: %d\n", wcd9xxx->irq, ret);
311 if (ret)
312 free_irq(wcd9xxx->irq, wcd9xxx);
313 }
314
Joonwoo Park3ee2d712013-01-07 12:40:03 -0800315 if (ret) {
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530316 mutex_destroy(&wcd9xxx->irq_lock);
Joonwoo Parkd8796592013-01-07 12:40:03 -0800317 mutex_destroy(&wcd9xxx->nested_irq_lock);
Joonwoo Park3ee2d712013-01-07 12:40:03 -0800318 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530319
320 return ret;
321}
322
323void wcd9xxx_irq_exit(struct wcd9xxx *wcd9xxx)
324{
325 if (wcd9xxx->irq) {
326 disable_irq_wake(wcd9xxx->irq);
327 free_irq(wcd9xxx->irq, wcd9xxx);
328 device_init_wakeup(wcd9xxx->dev, 0);
329 }
330 mutex_destroy(&wcd9xxx->irq_lock);
Joonwoo Parkd8796592013-01-07 12:40:03 -0800331 mutex_destroy(&wcd9xxx->nested_irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530332}