blob: 962558c9539e00fcfa2a942f4860d7a1b59b8ee4 [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>
Joonwoo Parkf6574c72012-10-10 17:29:57 -070021#include <linux/mfd/wcd9xxx/wcd9xxx-slimslave.h>
22#include <linux/delay.h>
23#include <linux/irqdomain.h>
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053024#include <linux/interrupt.h>
Joonwoo Parkf6574c72012-10-10 17:29:57 -070025#include <linux/of.h>
26#include <linux/of_irq.h>
27#include <linux/slab.h>
Stephen Boyd2fcabf92012-05-30 10:41:11 -070028#include <mach/cpuidle.h>
29
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053030#define BYTE_BIT_MASK(nr) (1UL << ((nr) % BITS_PER_BYTE))
31#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
32
Joonwoo Park18383dc2012-09-20 17:45:57 -070033#define WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS 100
34
Joonwoo Parkf6574c72012-10-10 17:29:57 -070035#ifdef CONFIG_OF
36struct wcd9xxx_irq_drv_data {
37 struct irq_domain *domain;
38 int irq;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053039};
Joonwoo Parkf6574c72012-10-10 17:29:57 -070040#endif
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053041
Joonwoo Parkf6574c72012-10-10 17:29:57 -070042static int virq_to_phyirq(struct wcd9xxx *wcd9xxx, int virq);
43static int phyirq_to_virq(struct wcd9xxx *wcd9xxx, int irq);
44static unsigned int wcd9xxx_irq_get_upstream_irq(struct wcd9xxx *wcd9xxx);
45static void wcd9xxx_irq_put_upstream_irq(struct wcd9xxx *wcd9xxx);
46static int wcd9xxx_map_irq(struct wcd9xxx *wcd9xxx, int irq);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053047
48static void wcd9xxx_irq_lock(struct irq_data *data)
49{
50 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
51 mutex_lock(&wcd9xxx->irq_lock);
52}
53
54static void wcd9xxx_irq_sync_unlock(struct irq_data *data)
55{
56 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
57 int i;
58
Simmi Pateriya4fd69932012-10-26 00:57:06 +053059 if (ARRAY_SIZE(wcd9xxx->irq_masks_cur) > WCD9XXX_NUM_IRQ_REGS ||
60 ARRAY_SIZE(wcd9xxx->irq_masks_cache) > WCD9XXX_NUM_IRQ_REGS) {
61 pr_err("%s: Array Size out of bound\n", __func__);
62 return;
63 }
64
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053065 for (i = 0; i < ARRAY_SIZE(wcd9xxx->irq_masks_cur); i++) {
66 /* If there's been a change in the mask write it back
67 * to the hardware.
68 */
69 if (wcd9xxx->irq_masks_cur[i] != wcd9xxx->irq_masks_cache[i]) {
70 wcd9xxx->irq_masks_cache[i] = wcd9xxx->irq_masks_cur[i];
Joonwoo Parkf6574c72012-10-10 17:29:57 -070071 wcd9xxx_reg_write(wcd9xxx,
72 WCD9XXX_A_INTR_MASK0 + i,
73 wcd9xxx->irq_masks_cur[i]);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053074 }
75 }
76
77 mutex_unlock(&wcd9xxx->irq_lock);
78}
79
80static void wcd9xxx_irq_enable(struct irq_data *data)
81{
82 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
Joonwoo Parkf6574c72012-10-10 17:29:57 -070083 int wcd9xxx_irq = virq_to_phyirq(wcd9xxx, data->irq);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053084 wcd9xxx->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)] &=
85 ~(BYTE_BIT_MASK(wcd9xxx_irq));
86}
87
88static void wcd9xxx_irq_disable(struct irq_data *data)
89{
90 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
Joonwoo Parkf6574c72012-10-10 17:29:57 -070091 int wcd9xxx_irq = virq_to_phyirq(wcd9xxx, data->irq);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053092 wcd9xxx->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)]
Joonwoo Parkf6574c72012-10-10 17:29:57 -070093 |= BYTE_BIT_MASK(wcd9xxx_irq);
94}
95
96static void wcd9xxx_irq_mask(struct irq_data *d)
97{
98 /* do nothing but required as linux calls irq_mask without NULL check */
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053099}
100
101static struct irq_chip wcd9xxx_irq_chip = {
102 .name = "wcd9xxx",
103 .irq_bus_lock = wcd9xxx_irq_lock,
104 .irq_bus_sync_unlock = wcd9xxx_irq_sync_unlock,
105 .irq_disable = wcd9xxx_irq_disable,
106 .irq_enable = wcd9xxx_irq_enable,
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700107 .irq_mask = wcd9xxx_irq_mask,
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530108};
109
110enum wcd9xxx_pm_state wcd9xxx_pm_cmpxchg(struct wcd9xxx *wcd9xxx,
111 enum wcd9xxx_pm_state o,
112 enum wcd9xxx_pm_state n)
113{
114 enum wcd9xxx_pm_state old;
115 mutex_lock(&wcd9xxx->pm_lock);
116 old = wcd9xxx->pm_state;
117 if (old == o)
118 wcd9xxx->pm_state = n;
119 mutex_unlock(&wcd9xxx->pm_lock);
120 return old;
121}
122EXPORT_SYMBOL_GPL(wcd9xxx_pm_cmpxchg);
123
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700124bool wcd9xxx_lock_sleep(struct wcd9xxx *wcd9xxx)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530125{
126 enum wcd9xxx_pm_state os;
127
Joonwoo Park18383dc2012-09-20 17:45:57 -0700128 /*
129 * wcd9xxx_{lock/unlock}_sleep will be called by wcd9xxx_irq_thread
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530130 * and its subroutines only motly.
131 * but btn0_lpress_fn is not wcd9xxx_irq_thread's subroutine and
Joonwoo Park18383dc2012-09-20 17:45:57 -0700132 * It can race with wcd9xxx_irq_thread.
133 * So need to embrace wlock_holders with mutex.
134 *
135 * If system didn't resume, we can simply return false so codec driver's
136 * IRQ handler can return without handling IRQ.
137 * As interrupt line is still active, codec will have another IRQ to
138 * retry shortly.
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530139 */
140 mutex_lock(&wcd9xxx->pm_lock);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700141 if (wcd9xxx->wlock_holders++ == 0) {
142 pr_debug("%s: holding wake lock\n", __func__);
Stephen Boyd2fcabf92012-05-30 10:41:11 -0700143 pm_qos_update_request(&wcd9xxx->pm_qos_req,
144 msm_cpuidle_get_deep_idle_latency());
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700145 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530146 mutex_unlock(&wcd9xxx->pm_lock);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700147 if (!wait_event_timeout(wcd9xxx->pm_wq,
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530148 ((os = wcd9xxx_pm_cmpxchg(wcd9xxx, WCD9XXX_PM_SLEEPABLE,
149 WCD9XXX_PM_AWAKE)) ==
150 WCD9XXX_PM_SLEEPABLE ||
151 (os == WCD9XXX_PM_AWAKE)),
Joonwoo Park18383dc2012-09-20 17:45:57 -0700152 msecs_to_jiffies(WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS))) {
153 pr_warn("%s: system didn't resume within %dms, s %d, w %d\n",
154 __func__,
155 WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS, wcd9xxx->pm_state,
156 wcd9xxx->wlock_holders);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700157 wcd9xxx_unlock_sleep(wcd9xxx);
158 return false;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530159 }
160 wake_up_all(&wcd9xxx->pm_wq);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700161 return true;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530162}
163EXPORT_SYMBOL_GPL(wcd9xxx_lock_sleep);
164
165void wcd9xxx_unlock_sleep(struct wcd9xxx *wcd9xxx)
166{
167 mutex_lock(&wcd9xxx->pm_lock);
168 if (--wcd9xxx->wlock_holders == 0) {
Joonwoo Park18383dc2012-09-20 17:45:57 -0700169 pr_debug("%s: releasing wake lock pm_state %d -> %d\n",
170 __func__, wcd9xxx->pm_state, WCD9XXX_PM_SLEEPABLE);
171 /*
172 * if wcd9xxx_lock_sleep failed, pm_state would be still
173 * WCD9XXX_PM_ASLEEP, don't overwrite
174 */
175 if (likely(wcd9xxx->pm_state == WCD9XXX_PM_AWAKE))
176 wcd9xxx->pm_state = WCD9XXX_PM_SLEEPABLE;
Stephen Boyd2fcabf92012-05-30 10:41:11 -0700177 pm_qos_update_request(&wcd9xxx->pm_qos_req,
178 PM_QOS_DEFAULT_VALUE);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530179 }
180 mutex_unlock(&wcd9xxx->pm_lock);
181 wake_up_all(&wcd9xxx->pm_wq);
182}
183EXPORT_SYMBOL_GPL(wcd9xxx_unlock_sleep);
184
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800185void wcd9xxx_nested_irq_lock(struct wcd9xxx *wcd9xxx)
186{
187 mutex_lock(&wcd9xxx->nested_irq_lock);
188}
189
190void wcd9xxx_nested_irq_unlock(struct wcd9xxx *wcd9xxx)
191{
192 mutex_unlock(&wcd9xxx->nested_irq_lock);
193}
194
Joonwoo Park03324832012-03-19 19:36:16 -0700195static void wcd9xxx_irq_dispatch(struct wcd9xxx *wcd9xxx, int irqbit)
196{
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700197 if ((irqbit <= WCD9XXX_IRQ_MBHC_INSERTION) &&
198 (irqbit >= WCD9XXX_IRQ_MBHC_REMOVAL)) {
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800199 wcd9xxx_nested_irq_lock(wcd9xxx);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700200 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_CLEAR0 +
201 BIT_BYTE(irqbit),
202 BYTE_BIT_MASK(irqbit));
Joonwoo Park03324832012-03-19 19:36:16 -0700203 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700204 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MODE, 0x02);
205 handle_nested_irq(phyirq_to_virq(wcd9xxx, irqbit));
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800206 wcd9xxx_nested_irq_unlock(wcd9xxx);
Joonwoo Park03324832012-03-19 19:36:16 -0700207 } else {
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800208 wcd9xxx_nested_irq_lock(wcd9xxx);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700209 handle_nested_irq(phyirq_to_virq(wcd9xxx, irqbit));
210 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_CLEAR0 +
211 BIT_BYTE(irqbit),
212 BYTE_BIT_MASK(irqbit));
Joonwoo Park03324832012-03-19 19:36:16 -0700213 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700214 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MODE, 0x02);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800215 wcd9xxx_nested_irq_unlock(wcd9xxx);
Joonwoo Park03324832012-03-19 19:36:16 -0700216 }
217}
218
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700219static int wcd9xxx_num_irq_regs(const struct wcd9xxx *wcd9xxx)
220{
221 return (wcd9xxx->num_irqs / 8) + ((wcd9xxx->num_irqs % 8) ? 1 : 0);
222}
223
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530224static irqreturn_t wcd9xxx_irq_thread(int irq, void *data)
225{
226 int ret;
Joonwoo Park03324832012-03-19 19:36:16 -0700227 int i;
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700228 struct wcd9xxx *wcd9xxx = data;
229 int num_irq_regs = wcd9xxx_num_irq_regs(wcd9xxx);
230 u8 status[num_irq_regs];
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530231
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700232 if (unlikely(wcd9xxx_lock_sleep(wcd9xxx) == false)) {
233 dev_err(wcd9xxx->dev, "Failed to hold suspend\n");
234 return IRQ_NONE;
235 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700236 ret = wcd9xxx_bulk_read(wcd9xxx, WCD9XXX_A_INTR_STATUS0,
237 num_irq_regs, status);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530238 if (ret < 0) {
239 dev_err(wcd9xxx->dev, "Failed to read interrupt status: %d\n",
240 ret);
Joonwoo Park07329192012-10-23 13:08:37 -0700241 dev_err(wcd9xxx->dev, "Disable irq %d\n", wcd9xxx->irq);
Joonwoo Park6183dbd2012-10-26 16:39:20 -0700242 disable_irq_wake(wcd9xxx->irq);
Joonwoo Park07329192012-10-23 13:08:37 -0700243 disable_irq_nosync(wcd9xxx->irq);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530244 wcd9xxx_unlock_sleep(wcd9xxx);
245 return IRQ_NONE;
246 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700247
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530248 /* Apply masking */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700249 for (i = 0; i < num_irq_regs; i++)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530250 status[i] &= ~wcd9xxx->irq_masks_cur[i];
251
252 /* Find out which interrupt was triggered and call that interrupt's
253 * handler function
254 */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700255 if (status[BIT_BYTE(WCD9XXX_IRQ_SLIMBUS)] &
256 BYTE_BIT_MASK(WCD9XXX_IRQ_SLIMBUS))
257 wcd9xxx_irq_dispatch(wcd9xxx, WCD9XXX_IRQ_SLIMBUS);
Joonwoo Park03324832012-03-19 19:36:16 -0700258
259 /* Since codec has only one hardware irq line which is shared by
260 * codec's different internal interrupts, so it's possible master irq
261 * handler dispatches multiple nested irq handlers after breaking
262 * order. Dispatch MBHC interrupts order to follow MBHC state
263 * machine's order */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700264 for (i = WCD9XXX_IRQ_MBHC_INSERTION;
265 i >= WCD9XXX_IRQ_MBHC_REMOVAL; i--) {
Joonwoo Park03324832012-03-19 19:36:16 -0700266 if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i))
267 wcd9xxx_irq_dispatch(wcd9xxx, i);
268 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700269 for (i = WCD9XXX_IRQ_BG_PRECHARGE; i < wcd9xxx->num_irqs; i++) {
Joonwoo Park03324832012-03-19 19:36:16 -0700270 if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i))
271 wcd9xxx_irq_dispatch(wcd9xxx, i);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530272 }
273 wcd9xxx_unlock_sleep(wcd9xxx);
274
275 return IRQ_HANDLED;
276}
277
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700278void wcd9xxx_free_irq(struct wcd9xxx *wcd9xxx, int irq, void *data)
279{
280 free_irq(phyirq_to_virq(wcd9xxx, irq), data);
281}
282
283void wcd9xxx_enable_irq(struct wcd9xxx *wcd9xxx, int irq)
284{
285 enable_irq(phyirq_to_virq(wcd9xxx, irq));
286}
287
288void wcd9xxx_disable_irq(struct wcd9xxx *wcd9xxx, int irq)
289{
290 disable_irq_nosync(phyirq_to_virq(wcd9xxx, irq));
291}
292
293void wcd9xxx_disable_irq_sync(struct wcd9xxx *wcd9xxx, int irq)
294{
295 disable_irq(phyirq_to_virq(wcd9xxx, irq));
296}
297
298static int wcd9xxx_irq_setup_downstream_irq(struct wcd9xxx *wcd9xxx)
299{
300 int irq, virq, ret;
301
302 pr_debug("%s: enter\n", __func__);
303
304 for (irq = 0; irq < wcd9xxx->num_irqs; irq++) {
305 /* Map OF irq */
306 virq = wcd9xxx_map_irq(wcd9xxx, irq);
307 pr_debug("%s: irq %d -> %d\n", __func__, irq, virq);
308 if (virq == NO_IRQ) {
309 pr_err("%s, No interrupt specifier for irq %d\n",
310 __func__, irq);
311 return NO_IRQ;
312 }
313
314 ret = irq_set_chip_data(virq, wcd9xxx);
315 if (ret) {
316 pr_err("%s: Failed to configure irq %d (%d)\n",
317 __func__, irq, ret);
318 return ret;
319 }
320
321 if (wcd9xxx->irq_level_high[irq])
322 irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip,
323 handle_level_irq);
324 else
325 irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip,
326 handle_edge_irq);
327
328 irq_set_nested_thread(virq, 1);
329 }
330
331 pr_debug("%s: leave\n", __func__);
332
333 return 0;
334}
335
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530336int wcd9xxx_irq_init(struct wcd9xxx *wcd9xxx)
337{
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700338 int i, ret;
339 u8 irq_level[wcd9xxx_num_irq_regs(wcd9xxx)];
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530340
341 mutex_init(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800342 mutex_init(&wcd9xxx->nested_irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530343
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700344 wcd9xxx->irq = wcd9xxx_irq_get_upstream_irq(wcd9xxx);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530345 if (!wcd9xxx->irq) {
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700346 pr_warn("%s: irq driver is not yet initialized\n", __func__);
347 mutex_destroy(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800348 mutex_destroy(&wcd9xxx->nested_irq_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700349 return -EPROBE_DEFER;
350 }
351 pr_debug("%s: probed irq %d\n", __func__, wcd9xxx->irq);
352
353 /* Setup downstream IRQs */
354 ret = wcd9xxx_irq_setup_downstream_irq(wcd9xxx);
355 if (ret) {
356 pr_err("%s: Failed to setup downstream IRQ\n", __func__);
357 wcd9xxx_irq_put_upstream_irq(wcd9xxx);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800358 mutex_destroy(&wcd9xxx->irq_lock);
359 mutex_destroy(&wcd9xxx->nested_irq_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700360 return ret;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530361 }
362
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700363 /* All other wcd9xxx interrupts are edge triggered */
364 wcd9xxx->irq_level_high[0] = true;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530365
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700366 /* mask all the interrupts */
367 memset(irq_level, 0, wcd9xxx_num_irq_regs(wcd9xxx));
368 for (i = 0; i < wcd9xxx->num_irqs; i++) {
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530369 wcd9xxx->irq_masks_cur[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
370 wcd9xxx->irq_masks_cache[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700371 irq_level[BIT_BYTE(i)] |=
372 wcd9xxx->irq_level_high[i] << (i % BITS_PER_BYTE);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530373 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700374
375 for (i = 0; i < wcd9xxx_num_irq_regs(wcd9xxx); i++) {
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530376 /* Initialize interrupt mask and level registers */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700377 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_LEVEL0 + i,
378 irq_level[i]);
379 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MASK0 + i,
380 wcd9xxx->irq_masks_cur[i]);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530381 }
382
383 ret = request_threaded_irq(wcd9xxx->irq, NULL, wcd9xxx_irq_thread,
384 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
385 "wcd9xxx", wcd9xxx);
386 if (ret != 0)
387 dev_err(wcd9xxx->dev, "Failed to request IRQ %d: %d\n",
388 wcd9xxx->irq, ret);
389 else {
390 ret = enable_irq_wake(wcd9xxx->irq);
391 if (ret == 0) {
392 ret = device_init_wakeup(wcd9xxx->dev, 1);
393 if (ret) {
394 dev_err(wcd9xxx->dev, "Failed to init device"
395 "wakeup : %d\n", ret);
396 disable_irq_wake(wcd9xxx->irq);
397 }
398 } else
399 dev_err(wcd9xxx->dev, "Failed to set wake interrupt on"
400 " IRQ %d: %d\n", wcd9xxx->irq, ret);
401 if (ret)
402 free_irq(wcd9xxx->irq, wcd9xxx);
403 }
404
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700405 if (ret) {
406 pr_err("%s: Failed to init wcd9xxx irq\n", __func__);
407 wcd9xxx_irq_put_upstream_irq(wcd9xxx);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530408 mutex_destroy(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800409 mutex_destroy(&wcd9xxx->nested_irq_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700410 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530411
412 return ret;
413}
414
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700415int wcd9xxx_request_irq(struct wcd9xxx *wcd9xxx, int irq, irq_handler_t handler,
416 const char *name, void *data)
417{
418 int virq;
419
420 virq = phyirq_to_virq(wcd9xxx, irq);
421
422 /*
423 * ARM needs us to explicitly flag the IRQ as valid
424 * and will set them noprobe when we do so.
425 */
426#ifdef CONFIG_ARM
427 set_irq_flags(virq, IRQF_VALID);
428#else
429 set_irq_noprobe(virq);
430#endif
431
432 return request_threaded_irq(virq, NULL, handler, IRQF_TRIGGER_RISING,
433 name, data);
434}
435
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530436void wcd9xxx_irq_exit(struct wcd9xxx *wcd9xxx)
437{
438 if (wcd9xxx->irq) {
439 disable_irq_wake(wcd9xxx->irq);
440 free_irq(wcd9xxx->irq, wcd9xxx);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700441 /* Release parent's of node */
442 wcd9xxx_irq_put_upstream_irq(wcd9xxx);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530443 device_init_wakeup(wcd9xxx->dev, 0);
444 }
445 mutex_destroy(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800446 mutex_destroy(&wcd9xxx->nested_irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530447}
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700448
449#ifndef CONFIG_OF
450static int phyirq_to_virq(struct wcd9xxx *wcd9xxx, int offset)
451{
452 return wcd9xxx->irq_base + offset;
453}
454
455static int virq_to_phyirq(struct wcd9xxx *wcd9xxx, int virq)
456{
457 return virq - wcd9xxx->irq_base;
458}
459
460static unsigned int wcd9xxx_irq_get_upstream_irq(struct wcd9xxx *wcd9xxx)
461{
462 return wcd9xxx->irq;
463}
464
465static void wcd9xxx_irq_put_upstream_irq(struct wcd9xxx *wcd9xxx)
466{
467 /* Do nothing */
468}
469
470static int wcd9xxx_map_irq(struct wcd9xxx *wcd9xxx, int irq)
471{
472 return phyirq_to_virq(wcd9xxx, irq);
473}
474#else
475int __init wcd9xxx_irq_of_init(struct device_node *node,
476 struct device_node *parent)
477{
478 struct wcd9xxx_irq_drv_data *data;
479
480 pr_debug("%s: node %s, node parent %s\n", __func__,
481 node->name, node->parent->name);
482
483 data = kzalloc(sizeof(*data), GFP_KERNEL);
484 if (!data)
485 return -ENOMEM;
486
487 /*
488 * wcd9xxx_intc interrupt controller supports N to N irq mapping with
489 * single cell binding with irq numbers(offsets) only.
490 * Use irq_domain_simple_ops that has irq_domain_simple_map and
491 * irq_domain_xlate_onetwocell.
492 */
493 data->domain = irq_domain_add_linear(node, WCD9XXX_MAX_NUM_IRQS,
494 &irq_domain_simple_ops, data);
495 if (!data->domain) {
496 kfree(data);
497 return -ENOMEM;
498 }
499
500 return 0;
501}
502
503static struct wcd9xxx_irq_drv_data *
504wcd9xxx_get_irq_drv_d(const struct wcd9xxx *wcd9xxx)
505{
506 struct device_node *pnode;
507 struct irq_domain *domain;
508
509 pnode = of_irq_find_parent(wcd9xxx->dev->of_node);
510 /* Shouldn't happen */
511 if (unlikely(!pnode))
512 return NULL;
513
514 domain = irq_find_host(pnode);
515 return (struct wcd9xxx_irq_drv_data *)domain->host_data;
516}
517
518static int phyirq_to_virq(struct wcd9xxx *wcd9xxx, int offset)
519{
520 struct wcd9xxx_irq_drv_data *data;
521
522 data = wcd9xxx_get_irq_drv_d(wcd9xxx);
523 if (!data) {
524 pr_warn("%s: not registered to interrupt controller\n",
525 __func__);
526 return -EINVAL;
527 }
528 return irq_linear_revmap(data->domain, offset);
529}
530
531static int virq_to_phyirq(struct wcd9xxx *wcd9xxx, int virq)
532{
533 struct irq_data *irq_data = irq_get_irq_data(virq);
534 return irq_data->hwirq;
535}
536
537static unsigned int wcd9xxx_irq_get_upstream_irq(struct wcd9xxx *wcd9xxx)
538{
539 struct wcd9xxx_irq_drv_data *data;
540
541 /* Hold parent's of node */
542 if (!of_node_get(of_irq_find_parent(wcd9xxx->dev->of_node)))
543 return -EINVAL;
544
545 data = wcd9xxx_get_irq_drv_d(wcd9xxx);
546 if (!data) {
547 pr_err("%s: interrupt controller is not registerd\n", __func__);
548 return 0;
549 }
550
551 rmb();
552 return data->irq;
553}
554
555static void wcd9xxx_irq_put_upstream_irq(struct wcd9xxx *wcd9xxx)
556{
557 /* Hold parent's of node */
558 of_node_put(of_irq_find_parent(wcd9xxx->dev->of_node));
559}
560
561static int wcd9xxx_map_irq(struct wcd9xxx *wcd9xxx, int irq)
562{
563 return of_irq_to_resource(wcd9xxx->dev->of_node, irq, NULL);
564}
565
566static int __devinit wcd9xxx_irq_probe(struct platform_device *pdev)
567{
568 int irq;
569 struct irq_domain *domain;
570 struct wcd9xxx_irq_drv_data *data;
571 int ret = -EINVAL;
572
573 irq = platform_get_irq_byname(pdev, "cdc-int");
574 if (irq < 0) {
575 dev_err(&pdev->dev, "%s: Couldn't find cdc-int node(%d)\n",
576 __func__, irq);
577 return -EINVAL;
578 } else {
579 dev_dbg(&pdev->dev, "%s: virq = %d\n", __func__, irq);
580 domain = irq_find_host(pdev->dev.of_node);
581 data = (struct wcd9xxx_irq_drv_data *)domain->host_data;
582 data->irq = irq;
583 wmb();
584 ret = 0;
585 }
586
587 return ret;
588}
589
590static int wcd9xxx_irq_remove(struct platform_device *pdev)
591{
592 struct irq_domain *domain;
593 struct wcd9xxx_irq_drv_data *data;
594
595 domain = irq_find_host(pdev->dev.of_node);
596 data = (struct wcd9xxx_irq_drv_data *)domain->host_data;
597 data->irq = 0;
598 wmb();
599
600 return 0;
601}
602
603static const struct of_device_id of_match[] = {
604 { .compatible = "qcom,wcd9xxx-irq" },
605 { }
606};
607
608static struct platform_driver wcd9xxx_irq_driver = {
609 .probe = wcd9xxx_irq_probe,
610 .remove = wcd9xxx_irq_remove,
611 .driver = {
612 .name = "wcd9xxx_intc",
613 .owner = THIS_MODULE,
614 .of_match_table = of_match_ptr(of_match),
615 },
616};
617
618static int wcd9xxx_irq_drv_init(void)
619{
620 return platform_driver_register(&wcd9xxx_irq_driver);
621}
622subsys_initcall(wcd9xxx_irq_drv_init);
623
624static void wcd9xxx_irq_drv_exit(void)
625{
626 platform_driver_unregister(&wcd9xxx_irq_driver);
627}
628module_exit(wcd9xxx_irq_drv_exit);
629#endif /* CONFIG_OF */