blob: 111131ab7a3e8ac6e0675a46c7491ad53e3a6862 [file] [log] [blame]
Joonwoo Park1277cb62013-03-19 14:16:51 -07001/* Copyright (c) 2011-2013, 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>
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>
Joonwoo Park1c958f1a2013-04-23 19:41:46 -070028#include <linux/ratelimit.h>
Stephen Boyd2fcabf92012-05-30 10:41:11 -070029#include <mach/cpuidle.h>
30
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053031#define BYTE_BIT_MASK(nr) (1UL << ((nr) % BITS_PER_BYTE))
32#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
33
Joonwoo Park18383dc2012-09-20 17:45:57 -070034#define WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS 100
35
Joonwoo Parkf6574c72012-10-10 17:29:57 -070036#ifdef CONFIG_OF
37struct wcd9xxx_irq_drv_data {
38 struct irq_domain *domain;
39 int irq;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053040};
Joonwoo Parkf6574c72012-10-10 17:29:57 -070041#endif
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053042
Joonwoo Parkf6574c72012-10-10 17:29:57 -070043static int virq_to_phyirq(struct wcd9xxx *wcd9xxx, int virq);
44static int phyirq_to_virq(struct wcd9xxx *wcd9xxx, int irq);
45static unsigned int wcd9xxx_irq_get_upstream_irq(struct wcd9xxx *wcd9xxx);
46static void wcd9xxx_irq_put_upstream_irq(struct wcd9xxx *wcd9xxx);
47static int wcd9xxx_map_irq(struct wcd9xxx *wcd9xxx, int irq);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053048
49static void wcd9xxx_irq_lock(struct irq_data *data)
50{
51 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
52 mutex_lock(&wcd9xxx->irq_lock);
53}
54
55static void wcd9xxx_irq_sync_unlock(struct irq_data *data)
56{
57 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
58 int i;
59
Simmi Pateriya4fd69932012-10-26 00:57:06 +053060 if (ARRAY_SIZE(wcd9xxx->irq_masks_cur) > WCD9XXX_NUM_IRQ_REGS ||
61 ARRAY_SIZE(wcd9xxx->irq_masks_cache) > WCD9XXX_NUM_IRQ_REGS) {
62 pr_err("%s: Array Size out of bound\n", __func__);
63 return;
64 }
65
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053066 for (i = 0; i < ARRAY_SIZE(wcd9xxx->irq_masks_cur); i++) {
67 /* If there's been a change in the mask write it back
68 * to the hardware.
69 */
70 if (wcd9xxx->irq_masks_cur[i] != wcd9xxx->irq_masks_cache[i]) {
71 wcd9xxx->irq_masks_cache[i] = wcd9xxx->irq_masks_cur[i];
Joonwoo Parkf6574c72012-10-10 17:29:57 -070072 wcd9xxx_reg_write(wcd9xxx,
73 WCD9XXX_A_INTR_MASK0 + i,
74 wcd9xxx->irq_masks_cur[i]);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053075 }
76 }
77
78 mutex_unlock(&wcd9xxx->irq_lock);
79}
80
81static void wcd9xxx_irq_enable(struct irq_data *data)
82{
83 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
Joonwoo Parkf6574c72012-10-10 17:29:57 -070084 int wcd9xxx_irq = virq_to_phyirq(wcd9xxx, data->irq);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053085 wcd9xxx->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)] &=
86 ~(BYTE_BIT_MASK(wcd9xxx_irq));
87}
88
89static void wcd9xxx_irq_disable(struct irq_data *data)
90{
91 struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data);
Joonwoo Parkf6574c72012-10-10 17:29:57 -070092 int wcd9xxx_irq = virq_to_phyirq(wcd9xxx, data->irq);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053093 wcd9xxx->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)]
Joonwoo Parkf6574c72012-10-10 17:29:57 -070094 |= BYTE_BIT_MASK(wcd9xxx_irq);
95}
96
97static void wcd9xxx_irq_mask(struct irq_data *d)
98{
99 /* do nothing but required as linux calls irq_mask without NULL check */
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530100}
101
102static struct irq_chip wcd9xxx_irq_chip = {
103 .name = "wcd9xxx",
104 .irq_bus_lock = wcd9xxx_irq_lock,
105 .irq_bus_sync_unlock = wcd9xxx_irq_sync_unlock,
106 .irq_disable = wcd9xxx_irq_disable,
107 .irq_enable = wcd9xxx_irq_enable,
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700108 .irq_mask = wcd9xxx_irq_mask,
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530109};
110
111enum wcd9xxx_pm_state wcd9xxx_pm_cmpxchg(struct wcd9xxx *wcd9xxx,
112 enum wcd9xxx_pm_state o,
113 enum wcd9xxx_pm_state n)
114{
115 enum wcd9xxx_pm_state old;
116 mutex_lock(&wcd9xxx->pm_lock);
117 old = wcd9xxx->pm_state;
118 if (old == o)
119 wcd9xxx->pm_state = n;
120 mutex_unlock(&wcd9xxx->pm_lock);
121 return old;
122}
123EXPORT_SYMBOL_GPL(wcd9xxx_pm_cmpxchg);
124
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700125bool wcd9xxx_lock_sleep(struct wcd9xxx *wcd9xxx)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530126{
127 enum wcd9xxx_pm_state os;
128
Joonwoo Park18383dc2012-09-20 17:45:57 -0700129 /*
130 * wcd9xxx_{lock/unlock}_sleep will be called by wcd9xxx_irq_thread
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530131 * and its subroutines only motly.
132 * but btn0_lpress_fn is not wcd9xxx_irq_thread's subroutine and
Joonwoo Park18383dc2012-09-20 17:45:57 -0700133 * It can race with wcd9xxx_irq_thread.
134 * So need to embrace wlock_holders with mutex.
135 *
136 * If system didn't resume, we can simply return false so codec driver's
137 * IRQ handler can return without handling IRQ.
138 * As interrupt line is still active, codec will have another IRQ to
139 * retry shortly.
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530140 */
141 mutex_lock(&wcd9xxx->pm_lock);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700142 if (wcd9xxx->wlock_holders++ == 0) {
143 pr_debug("%s: holding wake lock\n", __func__);
Stephen Boyd2fcabf92012-05-30 10:41:11 -0700144 pm_qos_update_request(&wcd9xxx->pm_qos_req,
145 msm_cpuidle_get_deep_idle_latency());
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700146 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530147 mutex_unlock(&wcd9xxx->pm_lock);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700148 if (!wait_event_timeout(wcd9xxx->pm_wq,
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530149 ((os = wcd9xxx_pm_cmpxchg(wcd9xxx, WCD9XXX_PM_SLEEPABLE,
150 WCD9XXX_PM_AWAKE)) ==
151 WCD9XXX_PM_SLEEPABLE ||
152 (os == WCD9XXX_PM_AWAKE)),
Joonwoo Park18383dc2012-09-20 17:45:57 -0700153 msecs_to_jiffies(WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS))) {
154 pr_warn("%s: system didn't resume within %dms, s %d, w %d\n",
155 __func__,
156 WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS, wcd9xxx->pm_state,
157 wcd9xxx->wlock_holders);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700158 wcd9xxx_unlock_sleep(wcd9xxx);
159 return false;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530160 }
161 wake_up_all(&wcd9xxx->pm_wq);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700162 return true;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530163}
164EXPORT_SYMBOL_GPL(wcd9xxx_lock_sleep);
165
166void wcd9xxx_unlock_sleep(struct wcd9xxx *wcd9xxx)
167{
168 mutex_lock(&wcd9xxx->pm_lock);
169 if (--wcd9xxx->wlock_holders == 0) {
Joonwoo Park18383dc2012-09-20 17:45:57 -0700170 pr_debug("%s: releasing wake lock pm_state %d -> %d\n",
171 __func__, wcd9xxx->pm_state, WCD9XXX_PM_SLEEPABLE);
172 /*
173 * if wcd9xxx_lock_sleep failed, pm_state would be still
174 * WCD9XXX_PM_ASLEEP, don't overwrite
175 */
176 if (likely(wcd9xxx->pm_state == WCD9XXX_PM_AWAKE))
177 wcd9xxx->pm_state = WCD9XXX_PM_SLEEPABLE;
Stephen Boyd2fcabf92012-05-30 10:41:11 -0700178 pm_qos_update_request(&wcd9xxx->pm_qos_req,
179 PM_QOS_DEFAULT_VALUE);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530180 }
181 mutex_unlock(&wcd9xxx->pm_lock);
182 wake_up_all(&wcd9xxx->pm_wq);
183}
184EXPORT_SYMBOL_GPL(wcd9xxx_unlock_sleep);
185
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800186void wcd9xxx_nested_irq_lock(struct wcd9xxx *wcd9xxx)
187{
188 mutex_lock(&wcd9xxx->nested_irq_lock);
189}
190
191void wcd9xxx_nested_irq_unlock(struct wcd9xxx *wcd9xxx)
192{
193 mutex_unlock(&wcd9xxx->nested_irq_lock);
194}
195
Joonwoo Park03324832012-03-19 19:36:16 -0700196static void wcd9xxx_irq_dispatch(struct wcd9xxx *wcd9xxx, int irqbit)
197{
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700198 if ((irqbit <= WCD9XXX_IRQ_MBHC_INSERTION) &&
199 (irqbit >= WCD9XXX_IRQ_MBHC_REMOVAL)) {
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800200 wcd9xxx_nested_irq_lock(wcd9xxx);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700201 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_CLEAR0 +
202 BIT_BYTE(irqbit),
203 BYTE_BIT_MASK(irqbit));
Joonwoo Park03324832012-03-19 19:36:16 -0700204 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700205 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MODE, 0x02);
206 handle_nested_irq(phyirq_to_virq(wcd9xxx, irqbit));
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800207 wcd9xxx_nested_irq_unlock(wcd9xxx);
Joonwoo Park03324832012-03-19 19:36:16 -0700208 } else {
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800209 wcd9xxx_nested_irq_lock(wcd9xxx);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700210 handle_nested_irq(phyirq_to_virq(wcd9xxx, irqbit));
211 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_CLEAR0 +
212 BIT_BYTE(irqbit),
213 BYTE_BIT_MASK(irqbit));
Joonwoo Park03324832012-03-19 19:36:16 -0700214 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700215 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MODE, 0x02);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800216 wcd9xxx_nested_irq_unlock(wcd9xxx);
Joonwoo Park03324832012-03-19 19:36:16 -0700217 }
218}
219
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700220static int wcd9xxx_num_irq_regs(const struct wcd9xxx *wcd9xxx)
221{
Joonwoo Park1277cb62013-03-19 14:16:51 -0700222 return (wcd9xxx->codec_type->num_irqs / 8) +
223 ((wcd9xxx->codec_type->num_irqs % 8) ? 1 : 0);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700224}
225
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530226static irqreturn_t wcd9xxx_irq_thread(int irq, void *data)
227{
228 int ret;
Joonwoo Park03324832012-03-19 19:36:16 -0700229 int i;
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700230 char linebuf[128];
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700231 struct wcd9xxx *wcd9xxx = data;
232 int num_irq_regs = wcd9xxx_num_irq_regs(wcd9xxx);
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700233 u8 status[num_irq_regs], status1[num_irq_regs];
234 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 1);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530235
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700236 if (unlikely(wcd9xxx_lock_sleep(wcd9xxx) == false)) {
237 dev_err(wcd9xxx->dev, "Failed to hold suspend\n");
238 return IRQ_NONE;
239 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700240 ret = wcd9xxx_bulk_read(wcd9xxx, WCD9XXX_A_INTR_STATUS0,
241 num_irq_regs, status);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530242 if (ret < 0) {
243 dev_err(wcd9xxx->dev, "Failed to read interrupt status: %d\n",
244 ret);
Joonwoo Park07329192012-10-23 13:08:37 -0700245 dev_err(wcd9xxx->dev, "Disable irq %d\n", wcd9xxx->irq);
Joonwoo Park6183dbd2012-10-26 16:39:20 -0700246 disable_irq_wake(wcd9xxx->irq);
Joonwoo Park07329192012-10-23 13:08:37 -0700247 disable_irq_nosync(wcd9xxx->irq);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530248 wcd9xxx_unlock_sleep(wcd9xxx);
249 return IRQ_NONE;
250 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700251
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530252 /* Apply masking */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700253 for (i = 0; i < num_irq_regs; i++)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530254 status[i] &= ~wcd9xxx->irq_masks_cur[i];
255
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700256 memcpy(status1, status, sizeof(status1));
257
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530258 /* Find out which interrupt was triggered and call that interrupt's
259 * handler function
260 */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700261 if (status[BIT_BYTE(WCD9XXX_IRQ_SLIMBUS)] &
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700262 BYTE_BIT_MASK(WCD9XXX_IRQ_SLIMBUS)) {
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700263 wcd9xxx_irq_dispatch(wcd9xxx, WCD9XXX_IRQ_SLIMBUS);
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700264 status1[BIT_BYTE(WCD9XXX_IRQ_SLIMBUS)] &=
265 ~BYTE_BIT_MASK(WCD9XXX_IRQ_SLIMBUS);
266 }
Joonwoo Park03324832012-03-19 19:36:16 -0700267
268 /* Since codec has only one hardware irq line which is shared by
269 * codec's different internal interrupts, so it's possible master irq
270 * handler dispatches multiple nested irq handlers after breaking
271 * order. Dispatch MBHC interrupts order to follow MBHC state
272 * machine's order */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700273 for (i = WCD9XXX_IRQ_MBHC_INSERTION;
274 i >= WCD9XXX_IRQ_MBHC_REMOVAL; i--) {
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700275 if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i)) {
Joonwoo Park03324832012-03-19 19:36:16 -0700276 wcd9xxx_irq_dispatch(wcd9xxx, i);
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700277 status1[BIT_BYTE(i)] &= ~BYTE_BIT_MASK(i);
278 }
Joonwoo Park03324832012-03-19 19:36:16 -0700279 }
Joonwoo Park1277cb62013-03-19 14:16:51 -0700280 for (i = WCD9XXX_IRQ_BG_PRECHARGE; i < wcd9xxx->codec_type->num_irqs;
281 i++) {
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700282 if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i)) {
Joonwoo Park03324832012-03-19 19:36:16 -0700283 wcd9xxx_irq_dispatch(wcd9xxx, i);
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700284 status1[BIT_BYTE(i)] &= ~BYTE_BIT_MASK(i);
285 }
286 }
287
288 /*
289 * As a failsafe if unhandled irq is found, clear it to prevent
290 * interrupt storm.
291 * Note that we can say there was an unhandled irq only when no irq
292 * handled by nested irq handler since Taiko supports qdsp as irqs'
293 * destination for few irqs. Therefore driver shouldn't clear pending
294 * irqs when few handled while few others not.
295 */
296 if (unlikely(!memcmp(status, status1, sizeof(status)))) {
297 if (__ratelimit(&ratelimit)) {
298 pr_warn("%s: Unhandled irq found\n", __func__);
299 hex_dump_to_buffer(status, sizeof(status), 16, 1,
300 linebuf, sizeof(linebuf), false);
301 pr_warn("%s: status0 : %s\n", __func__, linebuf);
302 hex_dump_to_buffer(status1, sizeof(status1), 16, 1,
303 linebuf, sizeof(linebuf), false);
304 pr_warn("%s: status1 : %s\n", __func__, linebuf);
305 }
306
307 memset(status, 0, num_irq_regs);
308 wcd9xxx_bulk_write(wcd9xxx, WCD9XXX_A_INTR_STATUS0,
309 num_irq_regs, status);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530310 }
311 wcd9xxx_unlock_sleep(wcd9xxx);
312
313 return IRQ_HANDLED;
314}
315
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700316void wcd9xxx_free_irq(struct wcd9xxx *wcd9xxx, int irq, void *data)
317{
318 free_irq(phyirq_to_virq(wcd9xxx, irq), data);
319}
320
321void wcd9xxx_enable_irq(struct wcd9xxx *wcd9xxx, int irq)
322{
323 enable_irq(phyirq_to_virq(wcd9xxx, irq));
324}
325
326void wcd9xxx_disable_irq(struct wcd9xxx *wcd9xxx, int irq)
327{
328 disable_irq_nosync(phyirq_to_virq(wcd9xxx, irq));
329}
330
331void wcd9xxx_disable_irq_sync(struct wcd9xxx *wcd9xxx, int irq)
332{
333 disable_irq(phyirq_to_virq(wcd9xxx, irq));
334}
335
336static int wcd9xxx_irq_setup_downstream_irq(struct wcd9xxx *wcd9xxx)
337{
338 int irq, virq, ret;
339
340 pr_debug("%s: enter\n", __func__);
341
Joonwoo Park1277cb62013-03-19 14:16:51 -0700342 for (irq = 0; irq < wcd9xxx->codec_type->num_irqs; irq++) {
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700343 /* Map OF irq */
344 virq = wcd9xxx_map_irq(wcd9xxx, irq);
345 pr_debug("%s: irq %d -> %d\n", __func__, irq, virq);
346 if (virq == NO_IRQ) {
347 pr_err("%s, No interrupt specifier for irq %d\n",
348 __func__, irq);
349 return NO_IRQ;
350 }
351
352 ret = irq_set_chip_data(virq, wcd9xxx);
353 if (ret) {
354 pr_err("%s: Failed to configure irq %d (%d)\n",
355 __func__, irq, ret);
356 return ret;
357 }
358
359 if (wcd9xxx->irq_level_high[irq])
360 irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip,
361 handle_level_irq);
362 else
363 irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip,
364 handle_edge_irq);
365
366 irq_set_nested_thread(virq, 1);
367 }
368
369 pr_debug("%s: leave\n", __func__);
370
371 return 0;
372}
373
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530374int wcd9xxx_irq_init(struct wcd9xxx *wcd9xxx)
375{
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700376 int i, ret;
377 u8 irq_level[wcd9xxx_num_irq_regs(wcd9xxx)];
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530378
379 mutex_init(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800380 mutex_init(&wcd9xxx->nested_irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530381
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700382 wcd9xxx->irq = wcd9xxx_irq_get_upstream_irq(wcd9xxx);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530383 if (!wcd9xxx->irq) {
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700384 pr_warn("%s: irq driver is not yet initialized\n", __func__);
385 mutex_destroy(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800386 mutex_destroy(&wcd9xxx->nested_irq_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700387 return -EPROBE_DEFER;
388 }
389 pr_debug("%s: probed irq %d\n", __func__, wcd9xxx->irq);
390
391 /* Setup downstream IRQs */
392 ret = wcd9xxx_irq_setup_downstream_irq(wcd9xxx);
393 if (ret) {
394 pr_err("%s: Failed to setup downstream IRQ\n", __func__);
395 wcd9xxx_irq_put_upstream_irq(wcd9xxx);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800396 mutex_destroy(&wcd9xxx->irq_lock);
397 mutex_destroy(&wcd9xxx->nested_irq_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700398 return ret;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530399 }
400
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700401 /* All other wcd9xxx interrupts are edge triggered */
402 wcd9xxx->irq_level_high[0] = true;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530403
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700404 /* mask all the interrupts */
405 memset(irq_level, 0, wcd9xxx_num_irq_regs(wcd9xxx));
Joonwoo Park1277cb62013-03-19 14:16:51 -0700406 for (i = 0; i < wcd9xxx->codec_type->num_irqs; i++) {
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530407 wcd9xxx->irq_masks_cur[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
408 wcd9xxx->irq_masks_cache[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700409 irq_level[BIT_BYTE(i)] |=
410 wcd9xxx->irq_level_high[i] << (i % BITS_PER_BYTE);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530411 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700412
413 for (i = 0; i < wcd9xxx_num_irq_regs(wcd9xxx); i++) {
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530414 /* Initialize interrupt mask and level registers */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700415 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_LEVEL0 + i,
416 irq_level[i]);
417 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MASK0 + i,
418 wcd9xxx->irq_masks_cur[i]);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530419 }
420
421 ret = request_threaded_irq(wcd9xxx->irq, NULL, wcd9xxx_irq_thread,
422 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
423 "wcd9xxx", wcd9xxx);
424 if (ret != 0)
425 dev_err(wcd9xxx->dev, "Failed to request IRQ %d: %d\n",
426 wcd9xxx->irq, ret);
427 else {
428 ret = enable_irq_wake(wcd9xxx->irq);
429 if (ret == 0) {
430 ret = device_init_wakeup(wcd9xxx->dev, 1);
431 if (ret) {
432 dev_err(wcd9xxx->dev, "Failed to init device"
433 "wakeup : %d\n", ret);
434 disable_irq_wake(wcd9xxx->irq);
435 }
436 } else
437 dev_err(wcd9xxx->dev, "Failed to set wake interrupt on"
438 " IRQ %d: %d\n", wcd9xxx->irq, ret);
439 if (ret)
440 free_irq(wcd9xxx->irq, wcd9xxx);
441 }
442
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700443 if (ret) {
444 pr_err("%s: Failed to init wcd9xxx irq\n", __func__);
445 wcd9xxx_irq_put_upstream_irq(wcd9xxx);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530446 mutex_destroy(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800447 mutex_destroy(&wcd9xxx->nested_irq_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700448 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530449
450 return ret;
451}
452
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700453int wcd9xxx_request_irq(struct wcd9xxx *wcd9xxx, int irq, irq_handler_t handler,
454 const char *name, void *data)
455{
456 int virq;
457
458 virq = phyirq_to_virq(wcd9xxx, irq);
459
460 /*
461 * ARM needs us to explicitly flag the IRQ as valid
462 * and will set them noprobe when we do so.
463 */
464#ifdef CONFIG_ARM
465 set_irq_flags(virq, IRQF_VALID);
466#else
467 set_irq_noprobe(virq);
468#endif
469
470 return request_threaded_irq(virq, NULL, handler, IRQF_TRIGGER_RISING,
471 name, data);
472}
473
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530474void wcd9xxx_irq_exit(struct wcd9xxx *wcd9xxx)
475{
476 if (wcd9xxx->irq) {
477 disable_irq_wake(wcd9xxx->irq);
478 free_irq(wcd9xxx->irq, wcd9xxx);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700479 /* Release parent's of node */
480 wcd9xxx_irq_put_upstream_irq(wcd9xxx);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530481 device_init_wakeup(wcd9xxx->dev, 0);
482 }
483 mutex_destroy(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800484 mutex_destroy(&wcd9xxx->nested_irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530485}
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700486
487#ifndef CONFIG_OF
488static int phyirq_to_virq(struct wcd9xxx *wcd9xxx, int offset)
489{
490 return wcd9xxx->irq_base + offset;
491}
492
493static int virq_to_phyirq(struct wcd9xxx *wcd9xxx, int virq)
494{
495 return virq - wcd9xxx->irq_base;
496}
497
498static unsigned int wcd9xxx_irq_get_upstream_irq(struct wcd9xxx *wcd9xxx)
499{
500 return wcd9xxx->irq;
501}
502
503static void wcd9xxx_irq_put_upstream_irq(struct wcd9xxx *wcd9xxx)
504{
505 /* Do nothing */
506}
507
508static int wcd9xxx_map_irq(struct wcd9xxx *wcd9xxx, int irq)
509{
510 return phyirq_to_virq(wcd9xxx, irq);
511}
512#else
513int __init wcd9xxx_irq_of_init(struct device_node *node,
514 struct device_node *parent)
515{
516 struct wcd9xxx_irq_drv_data *data;
517
518 pr_debug("%s: node %s, node parent %s\n", __func__,
519 node->name, node->parent->name);
520
521 data = kzalloc(sizeof(*data), GFP_KERNEL);
522 if (!data)
523 return -ENOMEM;
524
525 /*
526 * wcd9xxx_intc interrupt controller supports N to N irq mapping with
527 * single cell binding with irq numbers(offsets) only.
528 * Use irq_domain_simple_ops that has irq_domain_simple_map and
529 * irq_domain_xlate_onetwocell.
530 */
531 data->domain = irq_domain_add_linear(node, WCD9XXX_MAX_NUM_IRQS,
532 &irq_domain_simple_ops, data);
533 if (!data->domain) {
534 kfree(data);
535 return -ENOMEM;
536 }
537
538 return 0;
539}
540
541static struct wcd9xxx_irq_drv_data *
542wcd9xxx_get_irq_drv_d(const struct wcd9xxx *wcd9xxx)
543{
544 struct device_node *pnode;
545 struct irq_domain *domain;
546
547 pnode = of_irq_find_parent(wcd9xxx->dev->of_node);
548 /* Shouldn't happen */
549 if (unlikely(!pnode))
550 return NULL;
551
552 domain = irq_find_host(pnode);
553 return (struct wcd9xxx_irq_drv_data *)domain->host_data;
554}
555
556static int phyirq_to_virq(struct wcd9xxx *wcd9xxx, int offset)
557{
558 struct wcd9xxx_irq_drv_data *data;
559
560 data = wcd9xxx_get_irq_drv_d(wcd9xxx);
561 if (!data) {
562 pr_warn("%s: not registered to interrupt controller\n",
563 __func__);
564 return -EINVAL;
565 }
566 return irq_linear_revmap(data->domain, offset);
567}
568
569static int virq_to_phyirq(struct wcd9xxx *wcd9xxx, int virq)
570{
571 struct irq_data *irq_data = irq_get_irq_data(virq);
572 return irq_data->hwirq;
573}
574
575static unsigned int wcd9xxx_irq_get_upstream_irq(struct wcd9xxx *wcd9xxx)
576{
577 struct wcd9xxx_irq_drv_data *data;
578
579 /* Hold parent's of node */
580 if (!of_node_get(of_irq_find_parent(wcd9xxx->dev->of_node)))
581 return -EINVAL;
582
583 data = wcd9xxx_get_irq_drv_d(wcd9xxx);
584 if (!data) {
585 pr_err("%s: interrupt controller is not registerd\n", __func__);
586 return 0;
587 }
588
589 rmb();
590 return data->irq;
591}
592
593static void wcd9xxx_irq_put_upstream_irq(struct wcd9xxx *wcd9xxx)
594{
595 /* Hold parent's of node */
596 of_node_put(of_irq_find_parent(wcd9xxx->dev->of_node));
597}
598
599static int wcd9xxx_map_irq(struct wcd9xxx *wcd9xxx, int irq)
600{
601 return of_irq_to_resource(wcd9xxx->dev->of_node, irq, NULL);
602}
603
604static int __devinit wcd9xxx_irq_probe(struct platform_device *pdev)
605{
606 int irq;
607 struct irq_domain *domain;
608 struct wcd9xxx_irq_drv_data *data;
609 int ret = -EINVAL;
610
611 irq = platform_get_irq_byname(pdev, "cdc-int");
612 if (irq < 0) {
613 dev_err(&pdev->dev, "%s: Couldn't find cdc-int node(%d)\n",
614 __func__, irq);
615 return -EINVAL;
616 } else {
617 dev_dbg(&pdev->dev, "%s: virq = %d\n", __func__, irq);
618 domain = irq_find_host(pdev->dev.of_node);
619 data = (struct wcd9xxx_irq_drv_data *)domain->host_data;
620 data->irq = irq;
621 wmb();
622 ret = 0;
623 }
624
625 return ret;
626}
627
628static int wcd9xxx_irq_remove(struct platform_device *pdev)
629{
630 struct irq_domain *domain;
631 struct wcd9xxx_irq_drv_data *data;
632
633 domain = irq_find_host(pdev->dev.of_node);
634 data = (struct wcd9xxx_irq_drv_data *)domain->host_data;
635 data->irq = 0;
636 wmb();
637
638 return 0;
639}
640
641static const struct of_device_id of_match[] = {
642 { .compatible = "qcom,wcd9xxx-irq" },
643 { }
644};
645
646static struct platform_driver wcd9xxx_irq_driver = {
647 .probe = wcd9xxx_irq_probe,
648 .remove = wcd9xxx_irq_remove,
649 .driver = {
650 .name = "wcd9xxx_intc",
651 .owner = THIS_MODULE,
652 .of_match_table = of_match_ptr(of_match),
653 },
654};
655
656static int wcd9xxx_irq_drv_init(void)
657{
658 return platform_driver_register(&wcd9xxx_irq_driver);
659}
660subsys_initcall(wcd9xxx_irq_drv_init);
661
662static void wcd9xxx_irq_drv_exit(void)
663{
664 platform_driver_unregister(&wcd9xxx_irq_driver);
665}
666module_exit(wcd9xxx_irq_drv_exit);
667#endif /* CONFIG_OF */