blob: 062351d29f7af5291fdb66b3e1f995db3abb07c5 [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 Park055ae4f2013-05-28 19:09:56 -0700196static bool wcd9xxx_is_mbhc_irq(struct wcd9xxx *wcd9xxx, int irqbit)
Joonwoo Park03324832012-03-19 19:36:16 -0700197{
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700198 if ((irqbit <= WCD9XXX_IRQ_MBHC_INSERTION) &&
Joonwoo Park055ae4f2013-05-28 19:09:56 -0700199 (irqbit >= WCD9XXX_IRQ_MBHC_REMOVAL))
200 return true;
201 else if (wcd9xxx->codec_type->id_major == TAIKO_MAJOR &&
202 irqbit == WCD9320_IRQ_MBHC_JACK_SWITCH)
203 return true;
204 else if (wcd9xxx->codec_type->id_major == TAPAN_MAJOR &&
205 irqbit == WCD9306_IRQ_MBHC_JACK_SWITCH)
206 return true;
207 else
208 return false;
209}
210
211static void wcd9xxx_irq_dispatch(struct wcd9xxx *wcd9xxx, int irqbit)
212{
213 if (wcd9xxx_is_mbhc_irq(wcd9xxx, irqbit)) {
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800214 wcd9xxx_nested_irq_lock(wcd9xxx);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700215 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_CLEAR0 +
216 BIT_BYTE(irqbit),
217 BYTE_BIT_MASK(irqbit));
Joonwoo Park03324832012-03-19 19:36:16 -0700218 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700219 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MODE, 0x02);
220 handle_nested_irq(phyirq_to_virq(wcd9xxx, irqbit));
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800221 wcd9xxx_nested_irq_unlock(wcd9xxx);
Joonwoo Park03324832012-03-19 19:36:16 -0700222 } else {
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800223 wcd9xxx_nested_irq_lock(wcd9xxx);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700224 handle_nested_irq(phyirq_to_virq(wcd9xxx, irqbit));
225 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_CLEAR0 +
226 BIT_BYTE(irqbit),
227 BYTE_BIT_MASK(irqbit));
Joonwoo Park03324832012-03-19 19:36:16 -0700228 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700229 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MODE, 0x02);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800230 wcd9xxx_nested_irq_unlock(wcd9xxx);
Joonwoo Park03324832012-03-19 19:36:16 -0700231 }
232}
233
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700234static int wcd9xxx_num_irq_regs(const struct wcd9xxx *wcd9xxx)
235{
Joonwoo Park1277cb62013-03-19 14:16:51 -0700236 return (wcd9xxx->codec_type->num_irqs / 8) +
237 ((wcd9xxx->codec_type->num_irqs % 8) ? 1 : 0);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700238}
239
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530240static irqreturn_t wcd9xxx_irq_thread(int irq, void *data)
241{
242 int ret;
Joonwoo Park03324832012-03-19 19:36:16 -0700243 int i;
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700244 char linebuf[128];
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700245 struct wcd9xxx *wcd9xxx = data;
246 int num_irq_regs = wcd9xxx_num_irq_regs(wcd9xxx);
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700247 u8 status[num_irq_regs], status1[num_irq_regs];
248 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 1);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530249
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700250 if (unlikely(wcd9xxx_lock_sleep(wcd9xxx) == false)) {
251 dev_err(wcd9xxx->dev, "Failed to hold suspend\n");
252 return IRQ_NONE;
253 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700254 ret = wcd9xxx_bulk_read(wcd9xxx, WCD9XXX_A_INTR_STATUS0,
255 num_irq_regs, status);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530256 if (ret < 0) {
257 dev_err(wcd9xxx->dev, "Failed to read interrupt status: %d\n",
258 ret);
Joonwoo Park07329192012-10-23 13:08:37 -0700259 dev_err(wcd9xxx->dev, "Disable irq %d\n", wcd9xxx->irq);
Joonwoo Park6183dbd2012-10-26 16:39:20 -0700260 disable_irq_wake(wcd9xxx->irq);
Joonwoo Park07329192012-10-23 13:08:37 -0700261 disable_irq_nosync(wcd9xxx->irq);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530262 wcd9xxx_unlock_sleep(wcd9xxx);
263 return IRQ_NONE;
264 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700265
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530266 /* Apply masking */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700267 for (i = 0; i < num_irq_regs; i++)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530268 status[i] &= ~wcd9xxx->irq_masks_cur[i];
269
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700270 memcpy(status1, status, sizeof(status1));
271
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530272 /* Find out which interrupt was triggered and call that interrupt's
273 * handler function
274 */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700275 if (status[BIT_BYTE(WCD9XXX_IRQ_SLIMBUS)] &
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700276 BYTE_BIT_MASK(WCD9XXX_IRQ_SLIMBUS)) {
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700277 wcd9xxx_irq_dispatch(wcd9xxx, WCD9XXX_IRQ_SLIMBUS);
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700278 status1[BIT_BYTE(WCD9XXX_IRQ_SLIMBUS)] &=
279 ~BYTE_BIT_MASK(WCD9XXX_IRQ_SLIMBUS);
280 }
Joonwoo Park03324832012-03-19 19:36:16 -0700281
282 /* Since codec has only one hardware irq line which is shared by
283 * codec's different internal interrupts, so it's possible master irq
284 * handler dispatches multiple nested irq handlers after breaking
285 * order. Dispatch MBHC interrupts order to follow MBHC state
286 * machine's order */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700287 for (i = WCD9XXX_IRQ_MBHC_INSERTION;
288 i >= WCD9XXX_IRQ_MBHC_REMOVAL; i--) {
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700289 if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i)) {
Joonwoo Park03324832012-03-19 19:36:16 -0700290 wcd9xxx_irq_dispatch(wcd9xxx, i);
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700291 status1[BIT_BYTE(i)] &= ~BYTE_BIT_MASK(i);
292 }
Joonwoo Park03324832012-03-19 19:36:16 -0700293 }
Joonwoo Park1277cb62013-03-19 14:16:51 -0700294 for (i = WCD9XXX_IRQ_BG_PRECHARGE; i < wcd9xxx->codec_type->num_irqs;
295 i++) {
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700296 if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i)) {
Joonwoo Park03324832012-03-19 19:36:16 -0700297 wcd9xxx_irq_dispatch(wcd9xxx, i);
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700298 status1[BIT_BYTE(i)] &= ~BYTE_BIT_MASK(i);
299 }
300 }
301
302 /*
303 * As a failsafe if unhandled irq is found, clear it to prevent
304 * interrupt storm.
305 * Note that we can say there was an unhandled irq only when no irq
306 * handled by nested irq handler since Taiko supports qdsp as irqs'
307 * destination for few irqs. Therefore driver shouldn't clear pending
308 * irqs when few handled while few others not.
309 */
310 if (unlikely(!memcmp(status, status1, sizeof(status)))) {
311 if (__ratelimit(&ratelimit)) {
312 pr_warn("%s: Unhandled irq found\n", __func__);
313 hex_dump_to_buffer(status, sizeof(status), 16, 1,
314 linebuf, sizeof(linebuf), false);
315 pr_warn("%s: status0 : %s\n", __func__, linebuf);
316 hex_dump_to_buffer(status1, sizeof(status1), 16, 1,
317 linebuf, sizeof(linebuf), false);
318 pr_warn("%s: status1 : %s\n", __func__, linebuf);
319 }
320
Joonwoo Park66485a92013-04-25 13:06:34 -0700321 memset(status, 0xff, num_irq_regs);
322 wcd9xxx_bulk_write(wcd9xxx, WCD9XXX_A_INTR_CLEAR0,
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700323 num_irq_regs, status);
Joonwoo Park66485a92013-04-25 13:06:34 -0700324 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
325 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MODE, 0x02);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530326 }
327 wcd9xxx_unlock_sleep(wcd9xxx);
328
329 return IRQ_HANDLED;
330}
331
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700332void wcd9xxx_free_irq(struct wcd9xxx *wcd9xxx, int irq, void *data)
333{
334 free_irq(phyirq_to_virq(wcd9xxx, irq), data);
335}
336
337void wcd9xxx_enable_irq(struct wcd9xxx *wcd9xxx, int irq)
338{
339 enable_irq(phyirq_to_virq(wcd9xxx, irq));
340}
341
342void wcd9xxx_disable_irq(struct wcd9xxx *wcd9xxx, int irq)
343{
344 disable_irq_nosync(phyirq_to_virq(wcd9xxx, irq));
345}
346
347void wcd9xxx_disable_irq_sync(struct wcd9xxx *wcd9xxx, int irq)
348{
349 disable_irq(phyirq_to_virq(wcd9xxx, irq));
350}
351
352static int wcd9xxx_irq_setup_downstream_irq(struct wcd9xxx *wcd9xxx)
353{
354 int irq, virq, ret;
355
356 pr_debug("%s: enter\n", __func__);
357
Joonwoo Park1277cb62013-03-19 14:16:51 -0700358 for (irq = 0; irq < wcd9xxx->codec_type->num_irqs; irq++) {
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700359 /* Map OF irq */
360 virq = wcd9xxx_map_irq(wcd9xxx, irq);
361 pr_debug("%s: irq %d -> %d\n", __func__, irq, virq);
362 if (virq == NO_IRQ) {
363 pr_err("%s, No interrupt specifier for irq %d\n",
364 __func__, irq);
365 return NO_IRQ;
366 }
367
368 ret = irq_set_chip_data(virq, wcd9xxx);
369 if (ret) {
370 pr_err("%s: Failed to configure irq %d (%d)\n",
371 __func__, irq, ret);
372 return ret;
373 }
374
375 if (wcd9xxx->irq_level_high[irq])
376 irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip,
377 handle_level_irq);
378 else
379 irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip,
380 handle_edge_irq);
381
382 irq_set_nested_thread(virq, 1);
383 }
384
385 pr_debug("%s: leave\n", __func__);
386
387 return 0;
388}
389
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530390int wcd9xxx_irq_init(struct wcd9xxx *wcd9xxx)
391{
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700392 int i, ret;
393 u8 irq_level[wcd9xxx_num_irq_regs(wcd9xxx)];
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530394
395 mutex_init(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800396 mutex_init(&wcd9xxx->nested_irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530397
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700398 wcd9xxx->irq = wcd9xxx_irq_get_upstream_irq(wcd9xxx);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530399 if (!wcd9xxx->irq) {
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700400 pr_warn("%s: irq driver is not yet initialized\n", __func__);
401 mutex_destroy(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800402 mutex_destroy(&wcd9xxx->nested_irq_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700403 return -EPROBE_DEFER;
404 }
405 pr_debug("%s: probed irq %d\n", __func__, wcd9xxx->irq);
406
407 /* Setup downstream IRQs */
408 ret = wcd9xxx_irq_setup_downstream_irq(wcd9xxx);
409 if (ret) {
410 pr_err("%s: Failed to setup downstream IRQ\n", __func__);
411 wcd9xxx_irq_put_upstream_irq(wcd9xxx);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800412 mutex_destroy(&wcd9xxx->irq_lock);
413 mutex_destroy(&wcd9xxx->nested_irq_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700414 return ret;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530415 }
416
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700417 /* All other wcd9xxx interrupts are edge triggered */
418 wcd9xxx->irq_level_high[0] = true;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530419
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700420 /* mask all the interrupts */
421 memset(irq_level, 0, wcd9xxx_num_irq_regs(wcd9xxx));
Joonwoo Park1277cb62013-03-19 14:16:51 -0700422 for (i = 0; i < wcd9xxx->codec_type->num_irqs; i++) {
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530423 wcd9xxx->irq_masks_cur[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
424 wcd9xxx->irq_masks_cache[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700425 irq_level[BIT_BYTE(i)] |=
426 wcd9xxx->irq_level_high[i] << (i % BITS_PER_BYTE);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530427 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700428
429 for (i = 0; i < wcd9xxx_num_irq_regs(wcd9xxx); i++) {
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530430 /* Initialize interrupt mask and level registers */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700431 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_LEVEL0 + i,
432 irq_level[i]);
433 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MASK0 + i,
434 wcd9xxx->irq_masks_cur[i]);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530435 }
436
437 ret = request_threaded_irq(wcd9xxx->irq, NULL, wcd9xxx_irq_thread,
438 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
439 "wcd9xxx", wcd9xxx);
440 if (ret != 0)
441 dev_err(wcd9xxx->dev, "Failed to request IRQ %d: %d\n",
442 wcd9xxx->irq, ret);
443 else {
444 ret = enable_irq_wake(wcd9xxx->irq);
445 if (ret == 0) {
446 ret = device_init_wakeup(wcd9xxx->dev, 1);
447 if (ret) {
448 dev_err(wcd9xxx->dev, "Failed to init device"
449 "wakeup : %d\n", ret);
450 disable_irq_wake(wcd9xxx->irq);
451 }
452 } else
453 dev_err(wcd9xxx->dev, "Failed to set wake interrupt on"
454 " IRQ %d: %d\n", wcd9xxx->irq, ret);
455 if (ret)
456 free_irq(wcd9xxx->irq, wcd9xxx);
457 }
458
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700459 if (ret) {
460 pr_err("%s: Failed to init wcd9xxx irq\n", __func__);
461 wcd9xxx_irq_put_upstream_irq(wcd9xxx);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530462 mutex_destroy(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800463 mutex_destroy(&wcd9xxx->nested_irq_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700464 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530465
466 return ret;
467}
468
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700469int wcd9xxx_request_irq(struct wcd9xxx *wcd9xxx, int irq, irq_handler_t handler,
470 const char *name, void *data)
471{
472 int virq;
473
474 virq = phyirq_to_virq(wcd9xxx, irq);
475
476 /*
477 * ARM needs us to explicitly flag the IRQ as valid
478 * and will set them noprobe when we do so.
479 */
480#ifdef CONFIG_ARM
481 set_irq_flags(virq, IRQF_VALID);
482#else
483 set_irq_noprobe(virq);
484#endif
485
486 return request_threaded_irq(virq, NULL, handler, IRQF_TRIGGER_RISING,
487 name, data);
488}
489
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530490void wcd9xxx_irq_exit(struct wcd9xxx *wcd9xxx)
491{
492 if (wcd9xxx->irq) {
493 disable_irq_wake(wcd9xxx->irq);
494 free_irq(wcd9xxx->irq, wcd9xxx);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700495 /* Release parent's of node */
496 wcd9xxx_irq_put_upstream_irq(wcd9xxx);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530497 device_init_wakeup(wcd9xxx->dev, 0);
498 }
499 mutex_destroy(&wcd9xxx->irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800500 mutex_destroy(&wcd9xxx->nested_irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530501}
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700502
503#ifndef CONFIG_OF
504static int phyirq_to_virq(struct wcd9xxx *wcd9xxx, int offset)
505{
506 return wcd9xxx->irq_base + offset;
507}
508
509static int virq_to_phyirq(struct wcd9xxx *wcd9xxx, int virq)
510{
511 return virq - wcd9xxx->irq_base;
512}
513
514static unsigned int wcd9xxx_irq_get_upstream_irq(struct wcd9xxx *wcd9xxx)
515{
516 return wcd9xxx->irq;
517}
518
519static void wcd9xxx_irq_put_upstream_irq(struct wcd9xxx *wcd9xxx)
520{
521 /* Do nothing */
522}
523
524static int wcd9xxx_map_irq(struct wcd9xxx *wcd9xxx, int irq)
525{
526 return phyirq_to_virq(wcd9xxx, irq);
527}
528#else
529int __init wcd9xxx_irq_of_init(struct device_node *node,
530 struct device_node *parent)
531{
532 struct wcd9xxx_irq_drv_data *data;
533
534 pr_debug("%s: node %s, node parent %s\n", __func__,
535 node->name, node->parent->name);
536
537 data = kzalloc(sizeof(*data), GFP_KERNEL);
538 if (!data)
539 return -ENOMEM;
540
541 /*
542 * wcd9xxx_intc interrupt controller supports N to N irq mapping with
543 * single cell binding with irq numbers(offsets) only.
544 * Use irq_domain_simple_ops that has irq_domain_simple_map and
545 * irq_domain_xlate_onetwocell.
546 */
547 data->domain = irq_domain_add_linear(node, WCD9XXX_MAX_NUM_IRQS,
548 &irq_domain_simple_ops, data);
549 if (!data->domain) {
550 kfree(data);
551 return -ENOMEM;
552 }
553
554 return 0;
555}
556
557static struct wcd9xxx_irq_drv_data *
558wcd9xxx_get_irq_drv_d(const struct wcd9xxx *wcd9xxx)
559{
560 struct device_node *pnode;
561 struct irq_domain *domain;
562
563 pnode = of_irq_find_parent(wcd9xxx->dev->of_node);
564 /* Shouldn't happen */
565 if (unlikely(!pnode))
566 return NULL;
567
568 domain = irq_find_host(pnode);
569 return (struct wcd9xxx_irq_drv_data *)domain->host_data;
570}
571
572static int phyirq_to_virq(struct wcd9xxx *wcd9xxx, int offset)
573{
574 struct wcd9xxx_irq_drv_data *data;
575
576 data = wcd9xxx_get_irq_drv_d(wcd9xxx);
577 if (!data) {
578 pr_warn("%s: not registered to interrupt controller\n",
579 __func__);
580 return -EINVAL;
581 }
582 return irq_linear_revmap(data->domain, offset);
583}
584
585static int virq_to_phyirq(struct wcd9xxx *wcd9xxx, int virq)
586{
587 struct irq_data *irq_data = irq_get_irq_data(virq);
588 return irq_data->hwirq;
589}
590
591static unsigned int wcd9xxx_irq_get_upstream_irq(struct wcd9xxx *wcd9xxx)
592{
593 struct wcd9xxx_irq_drv_data *data;
594
595 /* Hold parent's of node */
596 if (!of_node_get(of_irq_find_parent(wcd9xxx->dev->of_node)))
597 return -EINVAL;
598
599 data = wcd9xxx_get_irq_drv_d(wcd9xxx);
600 if (!data) {
601 pr_err("%s: interrupt controller is not registerd\n", __func__);
602 return 0;
603 }
604
605 rmb();
606 return data->irq;
607}
608
609static void wcd9xxx_irq_put_upstream_irq(struct wcd9xxx *wcd9xxx)
610{
611 /* Hold parent's of node */
612 of_node_put(of_irq_find_parent(wcd9xxx->dev->of_node));
613}
614
615static int wcd9xxx_map_irq(struct wcd9xxx *wcd9xxx, int irq)
616{
617 return of_irq_to_resource(wcd9xxx->dev->of_node, irq, NULL);
618}
619
620static int __devinit wcd9xxx_irq_probe(struct platform_device *pdev)
621{
622 int irq;
623 struct irq_domain *domain;
624 struct wcd9xxx_irq_drv_data *data;
625 int ret = -EINVAL;
626
627 irq = platform_get_irq_byname(pdev, "cdc-int");
628 if (irq < 0) {
629 dev_err(&pdev->dev, "%s: Couldn't find cdc-int node(%d)\n",
630 __func__, irq);
631 return -EINVAL;
632 } else {
633 dev_dbg(&pdev->dev, "%s: virq = %d\n", __func__, irq);
634 domain = irq_find_host(pdev->dev.of_node);
635 data = (struct wcd9xxx_irq_drv_data *)domain->host_data;
636 data->irq = irq;
637 wmb();
638 ret = 0;
639 }
640
641 return ret;
642}
643
644static int wcd9xxx_irq_remove(struct platform_device *pdev)
645{
646 struct irq_domain *domain;
647 struct wcd9xxx_irq_drv_data *data;
648
649 domain = irq_find_host(pdev->dev.of_node);
650 data = (struct wcd9xxx_irq_drv_data *)domain->host_data;
651 data->irq = 0;
652 wmb();
653
654 return 0;
655}
656
657static const struct of_device_id of_match[] = {
658 { .compatible = "qcom,wcd9xxx-irq" },
659 { }
660};
661
662static struct platform_driver wcd9xxx_irq_driver = {
663 .probe = wcd9xxx_irq_probe,
664 .remove = wcd9xxx_irq_remove,
665 .driver = {
666 .name = "wcd9xxx_intc",
667 .owner = THIS_MODULE,
668 .of_match_table = of_match_ptr(of_match),
669 },
670};
671
672static int wcd9xxx_irq_drv_init(void)
673{
674 return platform_driver_register(&wcd9xxx_irq_driver);
675}
676subsys_initcall(wcd9xxx_irq_drv_init);
677
678static void wcd9xxx_irq_drv_exit(void)
679{
680 platform_driver_unregister(&wcd9xxx_irq_driver);
681}
682module_exit(wcd9xxx_irq_drv_exit);
683#endif /* CONFIG_OF */